xref: /reactos/drivers/network/tcpip/lwip/src/core/raw.c (revision d6eebaa4)
1 /**
2  * @file
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.<br>
6  * See also @ref raw_raw
7  *
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.<br>
13  * @see @ref api
14  */
15 
16 /*
17  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without modification,
21  * are permitted provided that the following conditions are met:
22  *
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.
30  *
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
40  * OF SUCH DAMAGE.
41  *
42  * This file is part of the lwIP TCP/IP stack.
43  *
44  * Author: Adam Dunkels <adam@sics.se>
45  *
46  */
47 
48 #include "lwip/opt.h"
49 
50 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
51 
52 #include "lwip/def.h"
53 #include "lwip/memp.h"
54 #include "lwip/ip_addr.h"
55 #include "lwip/netif.h"
56 #include "lwip/raw.h"
57 #include "lwip/priv/raw_priv.h"
58 #include "lwip/stats.h"
59 #include "lwip/ip6.h"
60 #include "lwip/ip6_addr.h"
61 #include "lwip/inet_chksum.h"
62 
63 #include <string.h>
64 
65 /** The list of RAW PCBs */
66 static struct raw_pcb *raw_pcbs;
67 
68 static u8_t
raw_input_local_match(struct raw_pcb * pcb,u8_t broadcast)69 raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
70 {
71   LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
72 
73   /* check if PCB is bound to specific netif */
74   if ((pcb->netif_idx != NETIF_NO_INDEX) &&
75       (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
76     return 0;
77   }
78 
79 #if LWIP_IPV4 && LWIP_IPV6
80   /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
81   if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
82 #if IP_SOF_BROADCAST_RECV
83     if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
84       return 0;
85     }
86 #endif /* IP_SOF_BROADCAST_RECV */
87     return 1;
88   }
89 #endif /* LWIP_IPV4 && LWIP_IPV6 */
90 
91   /* Only need to check PCB if incoming IP version matches PCB IP version */
92   if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
93 #if LWIP_IPV4
94     /* Special case: IPv4 broadcast: receive all broadcasts
95      * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
96     if (broadcast != 0) {
97 #if IP_SOF_BROADCAST_RECV
98       if (ip_get_option(pcb, SOF_BROADCAST))
99 #endif /* IP_SOF_BROADCAST_RECV */
100       {
101         if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
102           return 1;
103         }
104       }
105     } else
106 #endif /* LWIP_IPV4 */
107       /* Handle IPv4 and IPv6: catch all or exact match */
108       if (ip_addr_isany(&pcb->local_ip) ||
109           ip_addr_eq(&pcb->local_ip, ip_current_dest_addr())) {
110         return 1;
111       }
112   }
113 
114   return 0;
115 }
116 
117 /**
118  * Determine if in incoming IP packet is covered by a RAW PCB
119  * and if so, pass it to a user-provided receive callback function.
120  *
121  * Given an incoming IP datagram (as a chain of pbufs) this function
122  * finds a corresponding RAW PCB and calls the corresponding receive
123  * callback function.
124  *
125  * @param p pbuf to be demultiplexed to a RAW PCB.
126  * @param inp network interface on which the datagram was received.
127  * @return - 1 if the packet has been eaten by a RAW PCB receive
128  *           callback function. The caller MAY NOT not reference the
129  *           packet any longer, and MAY NOT call pbuf_free().
130  * @return - 0 if packet is not eaten (pbuf is still referenced by the
131  *           caller).
132  *
133  */
134 raw_input_state_t
raw_input(struct pbuf * p,struct netif * inp)135 raw_input(struct pbuf *p, struct netif *inp)
136 {
137   struct raw_pcb *pcb, *prev;
138   s16_t proto;
139   raw_input_state_t ret = RAW_INPUT_NONE;
140   u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
141 
142   LWIP_UNUSED_ARG(inp);
143 
144 #if LWIP_IPV6
145 #if LWIP_IPV4
146   if (IP_HDR_GET_VERSION(p->payload) == 6)
147 #endif /* LWIP_IPV4 */
148   {
149     struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
150     proto = IP6H_NEXTH(ip6hdr);
151   }
152 #if LWIP_IPV4
153   else
154 #endif /* LWIP_IPV4 */
155 #endif /* LWIP_IPV6 */
156 #if LWIP_IPV4
157   {
158     proto = IPH_PROTO((struct ip_hdr *)p->payload);
159   }
160 #endif /* LWIP_IPV4 */
161 
162   prev = NULL;
163   pcb = raw_pcbs;
164   /* loop through all raw pcbs until the packet is eaten by one */
165   /* this allows multiple pcbs to match against the packet by design */
166   while (pcb != NULL) {
167     if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
168         (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
169          ip_addr_eq(&pcb->remote_ip, ip_current_src_addr()))) {
170       /* receive callback function available? */
171       if (pcb->recv != NULL) {
172         u8_t eaten;
173 #ifndef LWIP_NOASSERT
174         void *old_payload = p->payload;
175 #endif
176         ret = RAW_INPUT_DELIVERED;
177         /* the receive callback function did not eat the packet? */
178         eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
179         if (eaten != 0) {
180           /* receive function ate the packet */
181           p = NULL;
182           if (prev != NULL) {
183             /* move the pcb to the front of raw_pcbs so that is
184                found faster next time */
185             prev->next = pcb->next;
186             pcb->next = raw_pcbs;
187             raw_pcbs = pcb;
188           }
189           return RAW_INPUT_EATEN;
190         } else {
191           /* sanity-check that the receive callback did not alter the pbuf */
192           LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
193                       p->payload == old_payload);
194         }
195       }
196       /* no receive callback function was set for this raw PCB */
197     }
198     /* drop the packet */
199     prev = pcb;
200     pcb = pcb->next;
201   }
202   return ret;
203 }
204 
205 /**
206  * @ingroup raw_raw
207  * Bind a RAW PCB.
208  *
209  * @param pcb RAW PCB to be bound with a local address ipaddr.
210  * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
211  * bind to all local interfaces.
212  *
213  * @return lwIP error code.
214  * - ERR_OK. Successful. No error occurred.
215  * - ERR_USE. The specified IP address is already bound to by
216  * another RAW PCB.
217  *
218  * @see raw_disconnect()
219  */
220 err_t
raw_bind(struct raw_pcb * pcb,const ip_addr_t * ipaddr)221 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
222 {
223   LWIP_ASSERT_CORE_LOCKED();
224   if ((pcb == NULL) || (ipaddr == NULL)) {
225     return ERR_VAL;
226   }
227   ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
228 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
229   /* If the given IP address should have a zone but doesn't, assign one now.
230    * This is legacy support: scope-aware callers should always provide properly
231    * zoned source addresses. */
232   if (IP_IS_V6(&pcb->local_ip) &&
233       ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) {
234     ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip));
235   }
236 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
237   return ERR_OK;
238 }
239 
240 /**
241  * @ingroup raw_raw
242  * Bind an RAW PCB to a specific netif.
243  * After calling this function, all packets received via this PCB
244  * are guaranteed to have come in via the specified netif, and all
245  * outgoing packets will go out via the specified netif.
246  *
247  * @param pcb RAW PCB to be bound with netif.
248  * @param netif netif to bind to. Can be NULL.
249  *
250  * @see raw_disconnect()
251  */
252 void
raw_bind_netif(struct raw_pcb * pcb,const struct netif * netif)253 raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
254 {
255   LWIP_ASSERT_CORE_LOCKED();
256   if (netif != NULL) {
257     pcb->netif_idx = netif_get_index(netif);
258   } else {
259     pcb->netif_idx = NETIF_NO_INDEX;
260   }
261 }
262 
263 /**
264  * @ingroup raw_raw
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
267  *
268  * This will associate the RAW PCB with the remote address.
269  *
270  * @param pcb RAW PCB to be connected with remote address ipaddr and port.
271  * @param ipaddr remote IP address to connect with.
272  *
273  * @return lwIP error code
274  *
275  * @see raw_disconnect() and raw_sendto()
276  */
277 err_t
raw_connect(struct raw_pcb * pcb,const ip_addr_t * ipaddr)278 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
279 {
280   LWIP_ASSERT_CORE_LOCKED();
281   if ((pcb == NULL) || (ipaddr == NULL)) {
282     return ERR_VAL;
283   }
284   ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
285 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
286   /* If the given IP address should have a zone but doesn't, assign one now,
287    * using the bound address to make a more informed decision when possible. */
288   if (IP_IS_V6(&pcb->remote_ip) &&
289       ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
290     ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
291   }
292 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
293   raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
294   return ERR_OK;
295 }
296 
297 /**
298  * @ingroup raw_raw
299  * Disconnect a RAW PCB.
300  *
301  * @param pcb the raw pcb to disconnect.
302  */
303 void
raw_disconnect(struct raw_pcb * pcb)304 raw_disconnect(struct raw_pcb *pcb)
305 {
306   LWIP_ASSERT_CORE_LOCKED();
307   /* reset remote address association */
308 #if LWIP_IPV4 && LWIP_IPV6
309   if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
310     ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
311   } else {
312 #endif
313     ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
314 #if LWIP_IPV4 && LWIP_IPV6
315   }
316 #endif
317   pcb->netif_idx = NETIF_NO_INDEX;
318   /* mark PCB as unconnected */
319   raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
320 }
321 
322 /**
323  * @ingroup raw_raw
324  * Set the callback function for received packets that match the
325  * raw PCB's protocol and binding.
326  *
327  * The callback function MUST either
328  * - eat the packet by calling pbuf_free() and returning non-zero. The
329  *   packet will not be passed to other raw PCBs or other protocol layers.
330  * - not free the packet, and return zero. The packet will be matched
331  *   against further PCBs and/or forwarded to another protocol layers.
332  */
333 void
raw_recv(struct raw_pcb * pcb,raw_recv_fn recv,void * recv_arg)334 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
335 {
336   LWIP_ASSERT_CORE_LOCKED();
337   /* remember recv() callback and user data */
338   pcb->recv = recv;
339   pcb->recv_arg = recv_arg;
340 }
341 
342 /**
343  * @ingroup raw_raw
344  * Send the raw IP packet to the given address. An IP header will be prepended
345  * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that
346  * case, the packet must include an IP header, which will then be sent as is.
347  *
348  * @param pcb the raw pcb which to send
349  * @param p the IP payload to send
350  * @param ipaddr the destination address of the IP packet
351  *
352  */
353 err_t
raw_sendto(struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * ipaddr)354 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
355 {
356   struct netif *netif;
357   const ip_addr_t *src_ip;
358 
359   if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
360     return ERR_VAL;
361   }
362 
363   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
364 
365   if (pcb->netif_idx != NETIF_NO_INDEX) {
366     netif = netif_get_by_index(pcb->netif_idx);
367   } else {
368 #if LWIP_MULTICAST_TX_OPTIONS
369     netif = NULL;
370     if (ip_addr_ismulticast(ipaddr)) {
371       /* For multicast-destined packets, use the user-provided interface index to
372        * determine the outgoing interface, if an interface index is set and a
373        * matching netif can be found. Otherwise, fall back to regular routing. */
374       netif = netif_get_by_index(pcb->mcast_ifindex);
375     }
376 
377     if (netif == NULL)
378 #endif /* LWIP_MULTICAST_TX_OPTIONS */
379     {
380       netif = ip_route(&pcb->local_ip, ipaddr);
381     }
382   }
383 
384   if (netif == NULL) {
385     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
386     ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
387     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("\n"));
388     return ERR_RTE;
389   }
390 
391   if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
392     /* use outgoing network interface IP address as source address */
393     src_ip = ip_netif_get_local_ip(netif, ipaddr);
394 #if LWIP_IPV6
395     if (src_ip == NULL) {
396       return ERR_RTE;
397     }
398 #endif /* LWIP_IPV6 */
399   } else {
400     /* use RAW PCB local IP address as source address */
401     src_ip = &pcb->local_ip;
402   }
403 
404   return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
405 }
406 
407 /**
408  * @ingroup raw_raw
409  * Send the raw IP packet to the given address, using a particular outgoing
410  * netif and source IP address. An IP header will be prepended to the packet,
411  * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
412  * packet must include an IP header, which will then be sent as is.
413  *
414  * @param pcb RAW PCB used to send the data
415  * @param p chain of pbufs to be sent
416  * @param dst_ip destination IP address
417  * @param netif the netif used for sending
418  * @param src_ip source IP address
419  */
420 err_t
raw_sendto_if_src(struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,struct netif * netif,const ip_addr_t * src_ip)421 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
422                   struct netif *netif, const ip_addr_t *src_ip)
423 {
424   err_t err;
425   struct pbuf *q; /* q will be sent down the stack */
426   u16_t header_size;
427   u8_t ttl;
428 
429   LWIP_ASSERT_CORE_LOCKED();
430 
431   if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
432       !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
433     return ERR_VAL;
434   }
435 
436   header_size = (
437 #if LWIP_IPV4 && LWIP_IPV6
438                   IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
439 #elif LWIP_IPV4
440                   IP_HLEN);
441 #else
442                   IP6_HLEN);
443 #endif
444 
445   /* Handle the HDRINCL option as an exception: none of the code below applies
446    * to this case, and sending the packet needs to be done differently too. */
447   if (pcb->flags & RAW_FLAGS_HDRINCL) {
448     /* A full header *must* be present in the first pbuf of the chain, as the
449      * output routines may access its fields directly. */
450     if (p->len < header_size) {
451       return ERR_VAL;
452     }
453     /* @todo multicast loop support, if at all desired for this scenario.. */
454     NETIF_SET_HINTS(netif, &pcb->netif_hints);
455     err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
456     NETIF_RESET_HINTS(netif);
457     return err;
458   }
459 
460   /* packet too large to add an IP header without causing an overflow? */
461   if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
462     return ERR_MEM;
463   }
464   /* not enough space to add an IP header to first pbuf in given p chain? */
465   if (pbuf_add_header(p, header_size)) {
466     /* allocate header in new pbuf */
467     q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
468     /* new header pbuf could not be allocated? */
469     if (q == NULL) {
470       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
471       return ERR_MEM;
472     }
473     if (p->tot_len != 0) {
474       /* chain header q in front of given pbuf p */
475       pbuf_chain(q, p);
476     }
477     /* { first pbuf q points to header pbuf } */
478     LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
479   } else {
480     /* first pbuf q equals given pbuf */
481     q = p;
482     if (pbuf_remove_header(q, header_size)) {
483       LWIP_ASSERT("Can't restore header we just removed!", 0);
484       return ERR_MEM;
485     }
486   }
487 
488 #if IP_SOF_BROADCAST
489   if (IP_IS_V4(dst_ip)) {
490     /* broadcast filter? */
491     if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
492       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
493       /* free any temporary header pbuf allocated by pbuf_header() */
494       if (q != p) {
495         pbuf_free(q);
496       }
497       return ERR_VAL;
498     }
499   }
500 #endif /* IP_SOF_BROADCAST */
501 
502   /* Multicast Loop? */
503 #if LWIP_MULTICAST_TX_OPTIONS
504   if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
505     q->flags |= PBUF_FLAG_MCASTLOOP;
506   }
507 #endif /* LWIP_MULTICAST_TX_OPTIONS */
508 
509 #if LWIP_IPV6
510   /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
511      compute the checksum and update the checksum in the payload. */
512   if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
513     u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
514     LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
515     SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
516   }
517 #endif
518 
519   /* Determine TTL to use */
520 #if LWIP_MULTICAST_TX_OPTIONS
521   ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
522 #else /* LWIP_MULTICAST_TX_OPTIONS */
523   ttl = pcb->ttl;
524 #endif /* LWIP_MULTICAST_TX_OPTIONS */
525 
526   NETIF_SET_HINTS(netif, &pcb->netif_hints);
527   err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
528   NETIF_RESET_HINTS(netif);
529 
530   /* did we chain a header earlier? */
531   if (q != p) {
532     /* free the header */
533     pbuf_free(q);
534   }
535   return err;
536 }
537 
538 /**
539  * @ingroup raw_raw
540  * Send the raw IP packet to the address given by raw_connect()
541  *
542  * @param pcb the raw pcb which to send
543  * @param p the IP payload to send
544  *
545  */
546 err_t
raw_send(struct raw_pcb * pcb,struct pbuf * p)547 raw_send(struct raw_pcb *pcb, struct pbuf *p)
548 {
549   return raw_sendto(pcb, p, &pcb->remote_ip);
550 }
551 
552 /**
553  * @ingroup raw_raw
554  * Remove an RAW PCB.
555  *
556  * @param pcb RAW PCB to be removed. The PCB is removed from the list of
557  * RAW PCB's and the data structure is freed from memory.
558  *
559  * @see raw_new()
560  */
561 void
raw_remove(struct raw_pcb * pcb)562 raw_remove(struct raw_pcb *pcb)
563 {
564   struct raw_pcb *pcb2;
565   LWIP_ASSERT_CORE_LOCKED();
566   /* pcb to be removed is first in list? */
567   if (raw_pcbs == pcb) {
568     /* make list start at 2nd pcb */
569     raw_pcbs = raw_pcbs->next;
570     /* pcb not 1st in list */
571   } else {
572     for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
573       /* find pcb in raw_pcbs list */
574       if (pcb2->next != NULL && pcb2->next == pcb) {
575         /* remove pcb from list */
576         pcb2->next = pcb->next;
577         break;
578       }
579     }
580   }
581   memp_free(MEMP_RAW_PCB, pcb);
582 }
583 
584 /**
585  * @ingroup raw_raw
586  * Create a RAW PCB.
587  *
588  * @return The RAW PCB which was created. NULL if the PCB data structure
589  * could not be allocated.
590  *
591  * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
592  *
593  * @see raw_remove()
594  */
595 struct raw_pcb *
raw_new(u8_t proto)596 raw_new(u8_t proto)
597 {
598   struct raw_pcb *pcb;
599 
600   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
601   LWIP_ASSERT_CORE_LOCKED();
602 
603   pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
604   /* could allocate RAW PCB? */
605   if (pcb != NULL) {
606     /* initialize PCB to all zeroes */
607     memset(pcb, 0, sizeof(struct raw_pcb));
608     pcb->protocol = proto;
609     pcb->ttl = RAW_TTL;
610 #if LWIP_MULTICAST_TX_OPTIONS
611     raw_set_multicast_ttl(pcb, RAW_TTL);
612 #endif /* LWIP_MULTICAST_TX_OPTIONS */
613     pcb_tci_init(pcb);
614     pcb->next = raw_pcbs;
615     raw_pcbs = pcb;
616   }
617   return pcb;
618 }
619 
620 /**
621  * @ingroup raw_raw
622  * Create a RAW PCB for specific IP type.
623  *
624  * @return The RAW PCB which was created. NULL if the PCB data structure
625  * could not be allocated.
626  *
627  * @param type IP address type, see @ref lwip_ip_addr_type definitions.
628  * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
629  * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
630  * @param proto the protocol number (next header) of the IPv6 packet payload
631  *              (e.g. IP6_NEXTH_ICMP6)
632  *
633  * @see raw_remove()
634  */
635 struct raw_pcb *
raw_new_ip_type(u8_t type,u8_t proto)636 raw_new_ip_type(u8_t type, u8_t proto)
637 {
638   struct raw_pcb *pcb;
639   LWIP_ASSERT_CORE_LOCKED();
640   pcb = raw_new(proto);
641 #if LWIP_IPV4 && LWIP_IPV6
642   if (pcb != NULL) {
643     IP_SET_TYPE_VAL(pcb->local_ip,  type);
644     IP_SET_TYPE_VAL(pcb->remote_ip, type);
645   }
646 #else /* LWIP_IPV4 && LWIP_IPV6 */
647   LWIP_UNUSED_ARG(type);
648 #endif /* LWIP_IPV4 && LWIP_IPV6 */
649   return pcb;
650 }
651 
652 /** This function is called from netif.c when address is changed
653  *
654  * @param old_addr IP address of the netif before change
655  * @param new_addr IP address of the netif after change
656  */
raw_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)657 void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
658 {
659   struct raw_pcb *rpcb;
660 
661   if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
662     for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
663       /* PCB bound to current local interface address? */
664       if (ip_addr_eq(&rpcb->local_ip, old_addr)) {
665         /* The PCB is bound to the old ipaddr and
666          * is set to bound to the new one instead */
667         ip_addr_copy(rpcb->local_ip, *new_addr);
668       }
669     }
670   }
671 }
672 
673 #endif /* LWIP_RAW */
674