Microchip® Advanced Software Framework

raw.c File Reference

Implementation of raw protocol PCBs for low-level handling of different types of protocols besides (or overriding) those already available in lwIP.

#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/memp.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/raw.h"
#include "lwip/stats.h"
#include "arch/perf.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/inet_chksum.h"
#include <string.h>

Functions

err_t raw_bind (struct raw_pcb *pcb, ip_addr_t *ipaddr)
 Bind a RAW PCB. More...
 
err_t raw_connect (struct raw_pcb *pcb, ip_addr_t *ipaddr)
 Connect an RAW PCB. More...
 
u8_t raw_input (struct pbuf *p, struct netif *inp)
 Determine if in incoming IP packet is covered by a RAW PCB and if so, pass it to a user-provided receive callback function. More...
 
struct raw_pcbraw_new (u8_t proto)
 Create a RAW PCB. More...
 
struct raw_pcbraw_new_ip6 (u8_t proto)
 Create a RAW PCB for IPv6. More...
 
void raw_recv (struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
 Set the callback function for received packets that match the raw PCB's protocol and binding. More...
 
void raw_remove (struct raw_pcb *pcb)
 Remove an RAW PCB. More...
 
err_t raw_send (struct raw_pcb *pcb, struct pbuf *p)
 Send the raw IP packet to the address given by raw_connect() More...
 
err_t raw_sendto (struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
 Send the raw IP packet to the given address. More...
 

Variables

static struct raw_pcbraw_pcbs
 The list of RAW PCBs. More...
 

err_t raw_bind ( struct raw_pcb pcb,
ip_addr_t ipaddr 
)

Bind a RAW PCB.

Parameters
pcbRAW PCB to be bound with a local address ipaddr.
ipaddrlocal IP address to bind with. Use IP_ADDR_ANY to bind to all local interfaces.
Returns
lwIP error code.
  • ERR_OK. Successful. No error occurred.
  • ERR_USE. The specified IP address is already bound to by another RAW PCB.
See Also
raw_disconnect()

References ERR_OK, ipX_addr_set_ipaddr, and PCB_ISIPV6.

Referenced by lwip_netconn_do_bind(), and ping_raw_init().

err_t raw_connect ( struct raw_pcb pcb,
ip_addr_t ipaddr 
)

Connect an RAW PCB.

This function is required by upper layers of lwip. Using the raw api you could use raw_sendto() instead

This will associate the RAW PCB with the remote address.

Parameters
pcbRAW PCB to be connected with remote address ipaddr and port.
ipaddrremote IP address to connect with.
Returns
lwIP error code
See Also
raw_disconnect() and raw_sendto()

References ERR_OK, ipX_addr_set_ipaddr, and PCB_ISIPV6.

Referenced by lwip_netconn_do_connect().

u8_t raw_input ( struct pbuf p,
struct netif inp 
)

Determine if in incoming IP packet is covered by a RAW PCB and if so, pass it to a user-provided receive callback function.

Given an incoming IP datagram (as a chain of pbufs) this function finds a corresponding RAW PCB and calls the corresponding receive callback function.

Parameters
ppbuf to be demultiplexed to a RAW PCB.
inpnetwork interface on which the datagram was received.
Returns
- 1 if the packet has been eaten by a RAW PCB receive callback function. The caller MAY NOT not reference the packet any longer, and MAY NOT call pbuf_free().
- 0 if packet is not eaten (pbuf is still referenced by the caller).

References if(), raw_pcb::ip4, ip6_current_src_addr, IP6H_NEXTH, ip_addr_isbroadcast, ip_current_dest_addr, ip_current_src_addr, ip_get_option, IP_PCB_IPVER_INPUT_MATCH, IPH_PROTO, IPH_V, ipX_addr_cmp, ipX_addr_isany, ipX_current_dest_addr, LWIP_ASSERT, LWIP_IPV6, raw_pcb::next, NULL, pbuf::payload, PCB_ISIPV6, raw_pcb::protocol, raw_pcbs, raw_pcb::recv, raw_pcb::recv_arg, and SOF_BROADCAST.

Referenced by ip6_input(), and ip_input().

struct raw_pcb* raw_new ( u8_t  proto)

Create a RAW PCB.

Returns
The RAW PCB which was created. NULL if the PCB data structure could not be allocated.
Parameters
protothe protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
See Also
raw_remove()

References LWIP_DBG_TRACE, LWIP_DEBUGF, memp_malloc(), raw_pcb::next, NULL, raw_pcb::protocol, RAW_DEBUG, and raw_pcbs.

Referenced by pcb_new(), ping_raw_init(), and raw_new_ip6().

struct raw_pcb* raw_new_ip6 ( u8_t  proto)

Create a RAW PCB for IPv6.

Returns
The RAW PCB which was created. NULL if the PCB data structure could not be allocated.
Parameters
protothe protocol number (next header) of the IPv6 packet payload (e.g. IP6_NEXTH_ICMP6)
See Also
raw_remove()

References ip_set_v6, and raw_new().

void raw_recv ( struct raw_pcb pcb,
raw_recv_fn  recv,
void recv_arg 
)

Set the callback function for received packets that match the raw PCB's protocol and binding.

The callback function MUST either

  • eat the packet by calling pbuf_free() and returning non-zero. The packet will not be passed to other raw PCBs or other protocol layers.
  • not free the packet, and return zero. The packet will be matched against further PCBs and/or forwarded to another protocol layers.
Returns
non-zero if the packet was free()d, zero if the packet remains available for others.

References raw_pcb::ip4, raw_pcb::recv, and raw_pcb::recv_arg.

Referenced by pcb_new(), and ping_raw_init().

void raw_remove ( struct raw_pcb pcb)

Remove an RAW PCB.

Parameters
pcbRAW PCB to be removed. The PCB is removed from the list of RAW PCB's and the data structure is freed from memory.
See Also
raw_new()

References memp_free(), raw_pcb::next, and NULL.

Referenced by lwip_netconn_do_delconn().

err_t raw_send ( struct raw_pcb pcb,
struct pbuf p 
)

Send the raw IP packet to the address given by raw_connect()

Parameters
pcbthe raw pcb which to send
pthe IP payload to send

References ipX_2_ip, and raw_sendto().

Referenced by lwip_netconn_do_send().

err_t raw_sendto ( struct raw_pcb pcb,
struct pbuf p,
ip_addr_t ipaddr 
)

Send the raw IP packet to the given address.

Note that actually you cannot modify the IP headers (this is inconsistent with the receive callback where you actually get the IP headers), you can only specify the IP payload here. It requires some more changes in lwIP. (there will be a raw_send() function then.)

Parameters
pcbthe raw pcb which to send
pthe IP payload to send
ipaddrthe destination address of the IP packet

References raw_pcb::chksum_offset, raw_pcb::chksum_reqd, ERR_MEM, ERR_RTE, ERR_VAL, ip6_chksum_pseudo(), IP6_HLEN, ip_2_ipX, ip_addr_isbroadcast, ip_get_option, IP_HLEN, ipX_2_ip, ipX_2_ip6, ipX_addr_debug_print, ipX_addr_isany, ipX_netif_get_local_ipX, ipX_output_if, ipX_route, pbuf::len, LWIP_ASSERT, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_LEVEL_WARNING, LWIP_DBG_TRACE, LWIP_DEBUGF, NETIF_SET_HWADDRHINT, NULL, pbuf::payload, pbuf_alloc(), pbuf_chain(), pbuf_free(), pbuf_header(), PBUF_IP, PBUF_RAM, PCB_ISIPV6, raw_pcb::protocol, RAW_DEBUG, SOF_BROADCAST, and pbuf::tot_len.

Referenced by lwip_netconn_do_send(), ping_send(), and raw_send().

struct raw_pcb* raw_pcbs
static

The list of RAW PCBs.

Referenced by raw_input(), and raw_new().