3 * Implementation of raw protocol PCBs for low-level handling of
4 * different types of protocols besides (or overriding) those
5 * already available in lwIP.\n
6 * See also @ref raw_raw
8 * @defgroup raw_raw RAW
9 * @ingroup callbackstyle_api
10 * Implementation of raw protocol PCBs for low-level handling of
11 * different types of protocols besides (or overriding) those
12 * already available in lwIP.\n
17 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
23 * 1. Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
34 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
42 * This file is part of the lwIP TCP/IP stack.
44 * Author: Adam Dunkels <adam@sics.se>
50 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
53 #include "lwip/memp.h"
54 #include "lwip/ip_addr.h"
55 #include "lwip/netif.h"
57 #include "lwip/stats.h"
59 #include "lwip/ip6_addr.h"
60 #include "lwip/inet_chksum.h"
64 /** The list of RAW PCBs */
65 static struct raw_pcb *raw_pcbs;
68 raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
70 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
72 /* check if PCB is bound to specific netif */
73 if ((pcb->netif_idx != NETIF_NO_INDEX) &&
74 (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
78 #if LWIP_IPV4 && LWIP_IPV6
79 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
80 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
81 #if IP_SOF_BROADCAST_RECV
82 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
85 #endif /* IP_SOF_BROADCAST_RECV */
88 #endif /* LWIP_IPV4 && LWIP_IPV6 */
90 /* Only need to check PCB if incoming IP version matches PCB IP version */
91 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
93 /* Special case: IPv4 broadcast: receive all broadcasts
94 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
96 #if IP_SOF_BROADCAST_RECV
97 if (ip_get_option(pcb, SOF_BROADCAST))
98 #endif /* IP_SOF_BROADCAST_RECV */
100 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
105 #endif /* LWIP_IPV4 */
106 /* Handle IPv4 and IPv6: catch all or exact match */
107 if (ip_addr_isany(&pcb->local_ip) ||
108 ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
117 * Determine if in incoming IP packet is covered by a RAW PCB
118 * and if so, pass it to a user-provided receive callback function.
120 * Given an incoming IP datagram (as a chain of pbufs) this function
121 * finds a corresponding RAW PCB and calls the corresponding receive
124 * @param p pbuf to be demultiplexed to a RAW PCB.
125 * @param inp network interface on which the datagram was received.
126 * @return - 1 if the packet has been eaten by a RAW PCB receive
127 * callback function. The caller MAY NOT not reference the
128 * packet any longer, and MAY NOT call pbuf_free().
129 * @return - 0 if packet is not eaten (pbuf is still referenced by the
134 raw_input(struct pbuf *p, struct netif *inp)
136 struct raw_pcb *pcb, *prev;
139 u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
141 LWIP_UNUSED_ARG(inp);
145 if (IP_HDR_GET_VERSION(p->payload) == 6)
146 #endif /* LWIP_IPV4 */
148 struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
149 proto = IP6H_NEXTH(ip6hdr);
153 #endif /* LWIP_IPV4 */
154 #endif /* LWIP_IPV6 */
157 proto = IPH_PROTO((struct ip_hdr *)p->payload);
159 #endif /* LWIP_IPV4 */
163 /* loop through all raw pcbs until the packet is eaten by one */
164 /* this allows multiple pcbs to match against the packet by design */
165 while ((eaten == 0) && (pcb != NULL)) {
167 if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
168 (((pcb->intf_filter == NULL || pcb->intf_filter == inp) &&
169 (ip_addr_isany(&pcb->local_ip) ||
170 ip_addr_cmp(&(pcb->local_ip), ip_current_dest_addr()))) ||
171 (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
172 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr())))
174 /* receive callback function available? */
175 #if IP_SOF_BROADCAST_RECV
176 /* broadcast filter? */
177 if ((ip_get_option(pcb, SOF_BROADCAST) || !ip_addr_isbroadcast(ip_current_dest_addr(), inp))
180 #endif /* LWIP_IPV6 */
182 #endif /* IP_SOF_BROADCAST_RECV */
184 #ifndef LWIP_NOASSERT
185 void* old_payload = p->payload;
187 /* receive callback function available? */
188 if (pcb->recv != NULL) {
189 /* the receive callback function did not eat the packet? */
190 eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
192 /* receive function ate the packet */
196 /* move the pcb to the front of raw_pcbs so that is
197 found faster next time */
198 prev->next = pcb->next;
199 pcb->next = raw_pcbs;
204 /* sanity-check that the receive callback did not alter the pbuf */
205 LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
206 p->payload == old_payload);
209 /* no receive callback function was set for this raw PCB */
211 /* drop the packet */
222 * @param pcb RAW PCB to be bound with a local address ipaddr.
223 * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
224 * bind to all local interfaces.
226 * @return lwIP error code.
227 * - ERR_OK. Successful. No error occurred.
228 * - ERR_USE. The specified IP address is already bound to by
231 * @see raw_disconnect()
234 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
236 LWIP_ASSERT_CORE_LOCKED();
237 if ((pcb == NULL) || (ipaddr == NULL)) {
240 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
248 * @param pcb RAW PCB to be bound with netif.
249 * @param netif netif to bind to. Can be NULL.
251 * @see raw_disconnect()
254 raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
257 pcb->netif_idx = netif_get_index(netif);
259 pcb->netif_idx = NETIF_NO_INDEX;
265 * Connect an RAW PCB. This function is required by upper layers
266 * of lwip. Using the raw api you could use raw_sendto() instead
268 * This will associate the RAW PCB with the remote address.
270 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
271 * @param ipaddr remote IP address to connect with.
273 * @return lwIP error code
275 * @see raw_disconnect() and raw_sendto()
278 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
280 LWIP_ASSERT_CORE_LOCKED();
281 if ((pcb == NULL) || (ipaddr == NULL)) {
284 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
285 raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
291 * Disconnect a RAW PCB.
293 * @param pcb the raw pcb to disconnect.
296 raw_disconnect(struct raw_pcb *pcb)
298 LWIP_ASSERT_CORE_LOCKED();
299 /* reset remote address association */
300 #if LWIP_IPV4 && LWIP_IPV6
301 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
302 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
305 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
306 #if LWIP_IPV4 && LWIP_IPV6
309 pcb->netif_idx = NETIF_NO_INDEX;
310 /* mark PCB as unconnected */
311 raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
316 * Set the callback function for received packets that match the
317 * raw PCB's protocol and binding.
319 * The callback function MUST either
320 * - eat the packet by calling pbuf_free() and returning non-zero. The
321 * packet will not be passed to other raw PCBs or other protocol layers.
322 * - not free the packet, and return zero. The packet will be matched
323 * against further PCBs and/or forwarded to another protocol layers.
326 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
328 LWIP_ASSERT_CORE_LOCKED();
329 /* remember recv() callback and user data */
331 pcb->recv_arg = recv_arg;
336 * Send the raw IP packet to the given address. Note that actually you cannot
337 * modify the IP headers (this is inconsistent with the receive callback where
338 * you actually get the IP headers), you can only specify the IP payload here.
339 * It requires some more changes in lwIP. (there will be a raw_send() function
342 * @param pcb the raw pcb which to send
343 * @param p the IP payload to send
344 * @param ipaddr the destination address of the IP packet
348 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
351 const ip_addr_t *src_ip;
353 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
357 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
359 if (pcb->intf_filter != NULL) {
360 netif = pcb->intf_filter;
361 } else if (pcb->netif_idx != NETIF_NO_INDEX) {
362 netif = netif_get_by_index(pcb->netif_idx);
364 #if LWIP_MULTICAST_TX_OPTIONS
366 if (ip_addr_ismulticast(ipaddr)) {
367 /* For multicast-destined packets, use the user-provided interface index to
368 * determine the outgoing interface, if an interface index is set and a
369 * matching netif can be found. Otherwise, fall back to regular routing. */
370 netif = netif_get_by_index(pcb->mcast_ifindex);
374 #endif /* LWIP_MULTICAST_TX_OPTIONS */
376 netif = ip_route(&pcb->local_ip, ipaddr);
381 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
382 ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
386 if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
387 /* use outgoing network interface IP address as source address */
388 src_ip = ip_netif_get_local_ip(netif, ipaddr);
390 if (src_ip == NULL) {
393 #endif /* LWIP_IPV6 */
395 /* use RAW PCB local IP address as source address */
396 src_ip = &pcb->local_ip;
399 return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
404 * Send the raw IP packet to the given address, using a particular outgoing
405 * netif and source IP address. An IP header will be prepended to the packet.
407 * @param pcb RAW PCB used to send the data
408 * @param p chain of pbufs to be sent
409 * @param dst_ip destination IP address
410 * @param netif the netif used for sending
411 * @param src_ip source IP address
414 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
415 struct netif *netif, const ip_addr_t *src_ip)
418 struct pbuf *q; /* q will be sent down the stack */
422 if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
423 !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
428 #if LWIP_IPV4 && LWIP_IPV6
429 IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
436 /* not enough space to add an IP header to first pbuf in given p chain? */
437 if (pbuf_header(p, header_size)) {
438 /* allocate header in new pbuf */
439 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
440 /* new header pbuf could not be allocated? */
442 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
445 if (p->tot_len != 0) {
446 /* chain header q in front of given pbuf p */
449 /* { first pbuf q points to header pbuf } */
450 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
452 /* first pbuf q equals given pbuf */
454 if (pbuf_header(q, (s16_t)-header_size)) {
455 LWIP_ASSERT("Can't restore header we just removed!", 0);
461 if (IP_IS_V4(dst_ip))
463 /* broadcast filter? */
464 if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
465 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
466 /* free any temporary header pbuf allocated by pbuf_header() */
473 #endif /* IP_SOF_BROADCAST */
475 /* Multicast Loop? */
476 #if LWIP_MULTICAST_TX_OPTIONS
477 if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
478 q->flags |= PBUF_FLAG_MCASTLOOP;
480 #endif /* LWIP_MULTICAST_TX_OPTIONS */
483 /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
484 compute the checksum and update the checksum in the payload. */
485 if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
486 u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
487 LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
488 SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
492 /* Determine TTL to use */
493 #if LWIP_MULTICAST_TX_OPTIONS
494 ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
495 #else /* LWIP_MULTICAST_TX_OPTIONS */
497 #endif /* LWIP_MULTICAST_TX_OPTIONS */
499 netif_apply_pcb(netif, (struct ip_pcb *)pcb);
500 err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
501 netif_apply_pcb(netif, NULL);
503 /* did we chain a header earlier? */
505 /* free the header */
513 * Send the raw IP packet to the address given by raw_connect()
515 * @param pcb the raw pcb which to send
516 * @param p the IP payload to send
520 raw_send(struct raw_pcb *pcb, struct pbuf *p)
522 return raw_sendto(pcb, p, &pcb->remote_ip);
529 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
530 * RAW PCB's and the data structure is freed from memory.
535 raw_remove(struct raw_pcb *pcb)
537 struct raw_pcb *pcb2;
538 LWIP_ASSERT_CORE_LOCKED();
539 /* pcb to be removed is first in list? */
540 if (raw_pcbs == pcb) {
541 /* make list start at 2nd pcb */
542 raw_pcbs = raw_pcbs->next;
543 /* pcb not 1st in list */
545 for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
546 /* find pcb in raw_pcbs list */
547 if (pcb2->next != NULL && pcb2->next == pcb) {
548 /* remove pcb from list */
549 pcb2->next = pcb->next;
554 memp_free(MEMP_RAW_PCB, pcb);
561 * @return The RAW PCB which was created. NULL if the PCB data structure
562 * could not be allocated.
564 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
573 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
574 LWIP_ASSERT_CORE_LOCKED();
576 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
577 /* could allocate RAW PCB? */
579 /* initialize PCB to all zeroes */
580 memset(pcb, 0, sizeof(struct raw_pcb));
581 pcb->protocol = proto;
583 #if LWIP_MULTICAST_TX_OPTIONS
584 raw_set_multicast_ttl(pcb, RAW_TTL);
585 #endif /* LWIP_MULTICAST_TX_OPTIONS */
586 pcb->next = raw_pcbs;
594 * Create a RAW PCB for specific IP type.
596 * @return The RAW PCB which was created. NULL if the PCB data structure
597 * could not be allocated.
599 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
600 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
601 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
602 * @param proto the protocol number (next header) of the IPv6 packet payload
603 * (e.g. IP6_NEXTH_ICMP6)
608 raw_new_ip_type(u8_t type, u8_t proto)
611 LWIP_ASSERT_CORE_LOCKED();
612 pcb = raw_new(proto);
613 #if LWIP_IPV4 && LWIP_IPV6
615 IP_SET_TYPE_VAL(pcb->local_ip, type);
616 IP_SET_TYPE_VAL(pcb->remote_ip, type);
618 #else /* LWIP_IPV4 && LWIP_IPV6 */
619 LWIP_UNUSED_ARG(type);
620 #endif /* LWIP_IPV4 && LWIP_IPV6 */
624 /** This function is called from netif.c when address is changed
626 * @param old_addr IP address of the netif before change
627 * @param new_addr IP address of the netif after change
629 void raw_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
631 struct raw_pcb* rpcb;
633 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
634 for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
635 /* PCB bound to current local interface address? */
636 if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
637 /* The PCB is bound to the old ipaddr and
638 * is set to bound to the new one instead */
639 ip_addr_copy(rpcb->local_ip, *new_addr);
645 #endif /* LWIP_RAW */