• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.\n
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.\n
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 #ifdef LWIP_IPV6
63 #include "lwip/icmp6.h"
64 #include "lwip/prot/udp.h"
65 #include "lwip/prot/tcp.h"
66 #include "lwip/api.h"
67 #endif
68 #include "lwip/etharp.h"
69 #include "lwip/prot/ethernet.h"
70 #include <string.h>
71 
72 #if PF_PKT_SUPPORT
73 const struct eth_hdr *g_lwip_current_eth_hdr;
74 const struct netif *g_lwip_current_netif;
75 #endif
76 
77 /** The list of RAW PCBs */
78 struct raw_pcb *raw_pcbs;
79 #if PF_PKT_SUPPORT
80 struct raw_pcb *pkt_raw_pcbs;
81 struct raw_pcb *all_pkt_raw_pcbs;
82 #endif
83 
84 static u8_t
raw_input_local_match(struct raw_pcb * pcb,u8_t broadcast)85 raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
86 {
87   LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
88 
89   /* check if PCB is bound to specific netif */
90   if ((pcb->netif_idx != NETIF_NO_INDEX) &&
91       (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
92     return 0;
93   }
94 
95 #if LWIP_IPV4 && LWIP_IPV6
96   /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
97   if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
98 #if IP_SOF_BROADCAST_RECV
99     if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
100       return 0;
101     }
102 #endif /* IP_SOF_BROADCAST_RECV */
103     return 1;
104   }
105 #endif /* LWIP_IPV4 && LWIP_IPV6 */
106 
107   /* Only need to check PCB if incoming IP version matches PCB IP version */
108   if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
109 #if LWIP_IPV4
110     /* Special case: IPv4 broadcast: receive all broadcasts
111      * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
112     if (broadcast != 0) {
113 #if IP_SOF_BROADCAST_RECV
114       if (ip_get_option(pcb, SOF_BROADCAST))
115 #endif /* IP_SOF_BROADCAST_RECV */
116       {
117         if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
118           return 1;
119         }
120       }
121     } else
122 #endif /* LWIP_IPV4 */
123       /* Handle IPv4 and IPv6: catch all or exact match */
124       if (ip_addr_isany(&pcb->local_ip) ||
125           ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
126         return 1;
127       }
128   }
129 
130   return 0;
131 }
132 
133 #if LWIP_IPV6
134 #if LWIP_SOCK_OPT_ICMP6_FILTER
icmpv6_filter_check(struct pbuf * p,struct raw_pcb * pcb,s16_t proto,u16_t * typep)135 static u32_t icmpv6_filter_check(struct pbuf *p, struct raw_pcb *pcb, s16_t proto, u16_t *typep)
136 {
137   u8_t type;
138   struct icmpv6_hdr *icmp6_tmphdr = NULL;
139   /*
140    * extract the icmp6 header type and check if it is present in icmp6_filter
141    * filter structure.Use the ICMP6_FILTER_WILLBLOCK macros to check
142    * if this icmpv6 message need to be blocked/filtered at application.
143    * The current Macros are slightly reversed to rfc2292 macros. Macros are in compliance
144    * with the litos linux header files.
145    */
146   if (proto == IPPROTO_ICMPV6) {
147     u32_t *data = &pcb->icmp6_filter.icmp6_filt[0];
148     icmp6_tmphdr = (struct icmpv6_hdr *)(p->payload);
149     type = icmp6_tmphdr->type;
150     *typep = type;
151     return (u32_t)((data[(type) >> ICMP6_FILTER_VAL]) & (1U << ((type) & ICMP6_FILTER_INTVAL)));
152   }
153 
154   return 0;
155 }
156 #endif /* LWIP_SOCK_OPT_ICMP6_FILTER */
157 
158 #if LWIP_IPV6_PER_PROTO_CHKSUM
lwip_ipv6checksum_validate(struct pbuf * p,const struct raw_pcb * pcb,s16_t proto)159 static u32_t lwip_ipv6checksum_validate(struct pbuf *p, const struct raw_pcb *pcb, s16_t proto)
160 {
161   u32_t ret = 0;
162 
163   if (proto == IP6_NEXTH_ICMP6) {
164     /* checksum will be from 3rd byte. so */
165     if (p->len < sizeof(struct icmpv6_hdr)) {
166       /* drop short packets and dont give to application */
167       LWIP_DEBUGF(RAW_DEBUG, ("icmp6_input: length mismatch failed .\n"));
168       return 1;
169     }
170 
171     /* if ret value is 0 it mean checksum is Ok. */
172     ret = ip6_chksum_pseudo(p, pcb->raw_proto,  p->tot_len, ip6_current_src_addr(), ip6_current_dest_addr());
173   } else {
174     if (pcb->chksum_reqd == 0) {
175       /* returning 0 , as the checksum validation is not enabled so need to give to app layer */
176       return 0;
177     }
178 
179     if ((proto == IP6_NEXTH_UDP) && (p->len < UDP_HLEN)) {
180       /*
181        * In this case it will be given to recv callback
182        * in raw_input() if length is not proper
183        * drop short packets
184        */
185       LWIP_DEBUGF(RAW_DEBUG,
186                   ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
187       return 1;
188     } else if ((proto == IP6_NEXTH_TCP) && (p->len < TCP_HLEN)) {
189       /* drop short packets */
190       LWIP_DEBUGF(RAW_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
191       return 1;
192     }
193 
194     /* if ret value is 0 it mean checksum is Ok. */
195     ret = ip6_chksum_pseudo(p, (u8_t)proto, p->tot_len,
196                             ip6_current_src_addr(),
197                             ip6_current_dest_addr());
198   }
199   return ret;
200 }
201 #endif /* LWIP_IPV6_PER_PROTO_CHKSUM */
202 #endif
203 
204 /**
205  * Determine if in incoming IP packet is covered by a RAW PCB
206  * and if so, pass it to a user-provided receive callback function.
207  *
208  * Given an incoming IP datagram (as a chain of pbufs) this function
209  * finds a corresponding RAW PCB and calls the corresponding receive
210  * callback function.
211  *
212  * @param p pbuf to be demultiplexed to a RAW PCB.
213  * @param inp network interface on which the datagram was received.
214  * @return - RAW_INPUT_EATEN if the packet has been processed by a RAW PCB receive
215  *           callback function.
216  * @return - RAW_INPUT_DELIVERED if the packet has been processed by a RAW PCB receive
217  *           callback function but fail to delivered to upper layer.
218  * @return - RAW_INPUT_NONE if packet is not been processed.
219  *
220  */
221 raw_input_state_t
raw_input(struct pbuf * p,struct netif * inp)222 raw_input(struct pbuf *p, struct netif *inp)
223 {
224   struct raw_pcb *pcb;
225   s16_t proto;
226   raw_input_state_t ret = RAW_INPUT_NONE;
227   u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
228 
229   LWIP_UNUSED_ARG(inp);
230 
231 #if LWIP_IPV6
232 #if LWIP_IPV4
233   if (IP_HDR_GET_VERSION(p->payload) == 6)
234 #endif /* LWIP_IPV4 */
235   {
236     struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
237     proto = IP6H_NEXTH(ip6hdr);
238   }
239 #if LWIP_IPV4
240   else
241 #endif /* LWIP_IPV4 */
242 #endif /* LWIP_IPV6 */
243 #if LWIP_IPV4
244   {
245     proto = IPH_PROTO((struct ip_hdr *)p->payload);
246   }
247 #endif /* LWIP_IPV4 */
248 
249   pcb = raw_pcbs;
250   /* loop through all raw pcbs until the packet is eaten by one */
251   /* this allows multiple pcbs to match against the packet by design */
252   while (pcb != NULL) {
253     if ((pcb->raw_proto == proto) && raw_input_local_match(pcb, broadcast) &&
254         (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
255          ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
256       /* receive callback function available? */
257       if (pcb->recv != NULL) {
258         u8_t eaten;
259 
260 #if LWIP_IPV4 && LWIP_IPV6
261         struct netconn *conn;
262         conn = (struct netconn *)pcb->recv_arg;
263         if (NETCONNTYPE_ISIPV6(NETCONN_TYPE(conn)) && IP_IS_V4_VAL(*ip_current_src_addr())) {
264           pcb = pcb->next;
265           continue;
266         }
267 #endif
268         /* the receive callback function did not eat the packet? */
269         eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
270         if (eaten != 0) {
271           /* receive function ate the packet */
272           ret = RAW_INPUT_EATEN;
273         } else {
274           if (ret == RAW_INPUT_NONE) {
275             ret = RAW_INPUT_DELIVERED;
276           }
277         }
278       }
279       /* no receive callback function was set for this raw PCB */
280     }
281     /* drop the packet */
282     pcb = pcb->next;
283   }
284   return ret;
285 }
286 
287 #if LWIP_IPV6
288 /*
289  * Determine if in incoming IP packet is covered by a RAW PCB
290  * and if so, pass it to a user-provided receive callback function.
291  *
292  * Given an incoming IP datagram (as a chain of pbufs) this function
293  * finds a corresponding RAW PCB and calls the corresponding receive
294  * callback function.
295  *
296  * @param p pbuf to be demultiplexed to a RAW PCB.
297  * @param inp network interface on which the datagram was received.
298  * @return - RAW_INPUT_EATEN if the packet has been processed by a RAW PCB receive
299  *           callback function.
300  * @return - RAW_INPUT_DELIVERED if the packet has been processed by a RAW PCB receive
301  *           callback function but fail to delivered to upper layer.
302  * @return - RAW_INPUT_NONE if packet is not been processed.
303  *
304  */
305 raw_input_state_t
raw_input6(struct pbuf * p,s16_t proto,s8_t * is_checksuminvalid)306 raw_input6(struct pbuf *p, s16_t proto, s8_t *is_checksuminvalid)
307 {
308   struct raw_pcb *pcb = NULL;
309 #if LWIP_SOCK_OPT_ICMP6_FILTER
310   u16_t type;
311 #endif
312   raw_input_state_t eaten = RAW_INPUT_NONE;
313 
314   u8_t broadcast = (u8_t)(ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()));
315   *is_checksuminvalid = 0;
316 
317   pcb = raw_pcbs;
318   /* loop through all raw pcbs */
319   /* this allows multiple pcbs to match against the packet by design */
320   while (pcb != NULL) {
321     if ((pcb->raw_proto == proto) && raw_input_local_match(pcb, broadcast) &&
322         (((pcb->flags & RAW_FLAGS_CONNECTED) == 0))) {
323       /* receive callback function available? */
324       if (pcb->recv != NULL) {
325 #if LWIP_IPV6_PER_PROTO_CHKSUM
326         if (lwip_ipv6checksum_validate(p, pcb, proto)) {
327           LWIP_DEBUGF(RAW_DEBUG, ("checksum validation failed for proto = %"U16_F"\n", proto));
328           *is_checksuminvalid = 1;
329           pcb = pcb->next;
330           continue;
331         }
332 #endif
333 #if LWIP_SOCK_OPT_ICMP6_FILTER
334         type = 0;
335         if (icmpv6_filter_check(p, pcb, proto, &type) != 0) {
336           LWIP_DEBUGF(RAW_DEBUG, ("packet filtered of icmp6 type = %"U16_F"\n", type));
337           pcb = pcb->next;
338           continue;
339         }
340 #endif
341         /* the receive callback function did not eat the packet? */
342         if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr())) {
343           eaten = RAW_INPUT_EATEN;
344         } else {
345           if (eaten == RAW_INPUT_NONE) {
346             eaten = RAW_INPUT_DELIVERED;
347           }
348         }
349       }
350       /* no receive callback function was set for this raw PCB */
351     }
352 
353     pcb = pcb->next;
354   }
355   return eaten;
356 }
357 
358 #endif
359 
360 #if PF_PKT_SUPPORT
361 /*
362  * Determine if in incoming IP packet is covered by a RAW PCB
363  * and if so, pass it to a user-provided receive callback function.
364  *
365  * Given an incoming IP datagram (as a chain of pbufs) this function
366  * finds a corresponding RAW PCB and calls the corresponding receive
367  * callback function.
368  *
369  * @param p pbuf to be demultiplexed to a RAW PCB.
370  * @param inp network interface on which the datagram was received.
371   * @param from the pbuf is from which NETCONN_PKT_RAW type raw_pcb,
372  *           otherwise it should be NULL.
373  * @return- void
374  *
375  */
raw_pkt_input(struct pbuf * p,const struct netif * inp,const struct raw_pcb * from)376 void raw_pkt_input(struct pbuf *p, const struct netif *inp, const struct raw_pcb *from)
377 {
378   struct raw_pcb *pcb = NULL;
379   struct eth_hdr *ethhdr = NULL;
380   u16_t proto;
381 
382   LWIP_UNUSED_ARG(inp);
383 
384   ethhdr = (struct eth_hdr *)p->payload;
385   proto = ethhdr->type;
386 
387   g_lwip_current_eth_hdr = ethhdr;
388   g_lwip_current_netif = inp;
389 
390   pcb = pkt_raw_pcbs;
391   /* loop through all raw pcbs until the packet is eaten by one */
392   /* this allows multiple pcbs to match against the packet by design */
393   while (pcb != NULL) {
394     if (((pcb->proto.eth_proto == htons(ETHTYPE_ALL)) || ((p != NULL) && !(p->flags & PBUF_FLAG_OUTGOING) &&
395         pcb->proto.eth_proto == proto)) &&
396         ((!pcb->netifindex) || (pcb->netifindex == inp->ifindex)) && (pcb != from)) {
397       /* receive callback function available? */
398       if (pcb->recv != NULL) {
399         /* the receive callback function did not eat the packet? */
400         if (pcb->recv(pcb->recv_arg, pcb, p, NULL) != 0) {
401           LWIP_DEBUGF(RAW_DEBUG, ("raw_pkt_input: packets recved failed \n"));
402         }
403       }
404       /* no receive callback function was set for this raw PCB */
405     }
406 
407     pcb = pcb->next;
408   }
409 
410   g_lwip_current_eth_hdr = NULL;
411   return;
412 }
413 #endif
414 
415 /**
416  * @ingroup raw_raw
417  * Bind a RAW PCB.
418  *
419  * @param pcb RAW PCB to be bound with a local address ipaddr.
420  * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
421  * bind to all local interfaces.
422  *
423  * @return lwIP error code.
424  * - ERR_OK. Successful. No error occurred.
425  * - ERR_USE. The specified IP address is already bound to by
426  * another RAW PCB.
427  *
428  * @see raw_disconnect()
429  */
430 err_t
raw_bind(struct raw_pcb * pcb,const ip_addr_t * ipaddr)431 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
432 {
433   LWIP_ASSERT_CORE_LOCKED();
434   if ((pcb == NULL) || (ipaddr == NULL)) {
435     return ERR_VAL;
436   }
437   ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
438 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
439   /* If the given IP address should have a zone but doesn't, assign one now.
440    * This is legacy support: scope-aware callers should always provide properly
441    * zoned source addresses. */
442   if (IP_IS_V6(&pcb->local_ip) &&
443       ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) {
444     ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip));
445   }
446 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
447   if (netif_ipaddr_isbrdcast(ipaddr) || ip_addr_ismulticast(ipaddr)) {
448     ip_set_option(pcb, SOF_BINDNONUNICAST);
449   } else {
450     ip_reset_option(pcb, SOF_BINDNONUNICAST);
451   }
452   return ERR_OK;
453 }
454 
455 #if PF_PKT_SUPPORT
456 /*
457  * Bind a RAW PCB for Packet family.
458  *
459  * @param pcb RAW PCB to be bound with a local address ipaddr.
460  * @param ifindex Interface Index to bind with. Use IP_ADDR_ANY to
461  * bind to all local interfaces.
462  *
463  * @return lwIP error code.
464  * - ERR_OK. Successful. No error occured.
465  *
466  * @see raw_disconnect()
467  */
468 err_t
raw_pkt_bind(struct raw_pcb * pcb,u8_t ifindex,u16_t proto)469 raw_pkt_bind(struct raw_pcb *pcb, u8_t ifindex, u16_t proto)
470 {
471   struct netif *loc_netif = NULL;
472 
473   if (ifindex != 0) {
474     for (loc_netif = netif_list; loc_netif != NULL; loc_netif = loc_netif->next) {
475       if (ifindex == loc_netif->ifindex) {
476         break;
477       }
478     }
479 
480     /* Return if no matching netifs to bind */
481     if (loc_netif == NULL) {
482       LWIP_DEBUGF(RAW_DEBUG, ("raw_pkt_bind: No matching netif found for ifindex(%u)\n", ifindex));
483       return ERR_NODEV;
484     }
485   } else {
486     return ERR_NODEV;
487   }
488 
489 #if DRIVER_STATUS_CHECK
490   if (!netif_is_up(loc_netif) || !netif_is_ready(loc_netif))
491 #else
492   if (!netif_is_up(loc_netif))
493 #endif
494   {
495     LWIP_DEBUGF(RAW_DEBUG, ("raw_pkt_bind: bind failed as netif (index %u) was down\n", ifindex));
496     return ERR_NETDOWN;
497   }
498 
499   pcb->netifindex = ifindex;
500   pcb->proto.eth_proto = proto;
501 
502   return ERR_OK;
503 }
504 #endif
505 
506 /**
507  * @ingroup raw_raw
508  * Bind an RAW PCB to a specific netif.
509  * After calling this function, all packets received via this PCB
510  * are guaranteed to have come in via the specified netif, and all
511  * outgoing packets will go out via the specified netif.
512  *
513  * @param pcb RAW PCB to be bound with netif.
514  * @param netif netif to bind to. Can be NULL.
515  *
516  * @see raw_disconnect()
517  */
518 void
raw_bind_netif(struct raw_pcb * pcb,const struct netif * netif)519 raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
520 {
521   LWIP_ASSERT_CORE_LOCKED();
522   if (netif != NULL) {
523     pcb->netif_idx = netif_get_index(netif);
524   } else {
525     pcb->netif_idx = NETIF_NO_INDEX;
526   }
527 }
528 
529 /**
530  * @ingroup raw_raw
531  * Connect an RAW PCB. This function is required by upper layers
532  * of lwip. Using the raw api you could use raw_sendto() instead
533  *
534  * This will associate the RAW PCB with the remote address.
535  *
536  * @param pcb RAW PCB to be connected with remote address ipaddr and port.
537  * @param ipaddr remote IP address to connect with.
538  *
539  * @return lwIP error code
540  *
541  * @see raw_disconnect() and raw_sendto()
542  */
543 err_t
raw_connect(struct raw_pcb * pcb,const ip_addr_t * ipaddr)544 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
545 {
546   struct netif *netif = NULL;
547   LWIP_ASSERT_CORE_LOCKED();
548   if ((pcb == NULL) || (ipaddr == NULL)) {
549     return ERR_VAL;
550   }
551 
552   netif = ip_route_pcb(ipaddr, (struct ip_pcb*)pcb);
553   if (netif == NULL) {
554     return ERR_NETUNREACH;
555   }
556 
557   if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
558     return ERR_ACCESS;
559   }
560 
561   ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
562 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
563   /* If the given IP address should have a zone but doesn't, assign one now,
564    * using the bound address to make a more informed decision when possible. */
565   if (IP_IS_V6(&pcb->remote_ip) &&
566       ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
567     ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
568   }
569 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
570   raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
571   return ERR_OK;
572 }
573 
574 /**
575  * @ingroup raw_raw
576  * Disconnect a RAW PCB.
577  *
578  * @param pcb the raw pcb to disconnect.
579  */
580 void
raw_disconnect(struct raw_pcb * pcb)581 raw_disconnect(struct raw_pcb *pcb)
582 {
583   LWIP_ASSERT_CORE_LOCKED();
584   /* reset remote address association */
585 #if LWIP_IPV4 && LWIP_IPV6
586   if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
587     ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
588   } else {
589 #endif
590     ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
591 #if LWIP_IPV4 && LWIP_IPV6
592   }
593 #endif
594   pcb->netif_idx = NETIF_NO_INDEX;
595   /* mark PCB as unconnected */
596   raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
597 }
598 
599 /**
600  * @ingroup raw_raw
601  * Set the callback function for received packets that match the
602  * raw PCB's protocol and binding.
603  *
604  * The callback function MUST either
605  * - eat the packet by calling pbuf_free() and returning non-zero. The
606  *   packet will not be passed to other raw PCBs or other protocol layers.
607  * - not free the packet, and return zero. The packet will be matched
608  *   against further PCBs and/or forwarded to another protocol layers.
609  */
610 void
raw_recv(struct raw_pcb * pcb,raw_recv_fn recv,void * recv_arg)611 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
612 {
613   LWIP_ASSERT_CORE_LOCKED();
614   /* remember recv() callback and user data */
615   pcb->recv = recv;
616   pcb->recv_arg = recv_arg;
617 }
618 
619 #if PF_PKT_SUPPORT
620 /*
621  * Send the raw IP packet through the given netif driver. Note that actually you cannot
622  * modify the link layer header here. Packet need to be sent to driver as it is through the
623  * given netif
624  * @param pcb the raw pcb which to send
625  * @param p the ethernet packet to send
626  * @param ifindex the Interface index of the netif through which packet needs to be sent
627  */
628 err_t
raw_pkt_sendto(const struct raw_pcb * pcb,struct pbuf * p,u8_t ifindex)629 raw_pkt_sendto(const struct raw_pcb *pcb, struct pbuf *p, u8_t ifindex)
630 {
631   struct netif *netif = NULL;
632   u8_t netifindex;
633   LWIP_UNUSED_ARG(pcb);
634 
635   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_pkt_sendto: ifindex=%d\n", ifindex));
636   LWIP_ASSERT("p != NULL", p != NULL);
637 
638   netifindex = ifindex;
639 
640   if (ifindex == 0) {
641     if (pcb->netifindex != 0) {
642       netifindex = pcb->netifindex;
643     } else {
644       return ERR_NODEVADDR;
645     }
646   }
647 
648   /* Find the netif corresponding to the interface index */
649   netif = netif_find_by_ifindex(netifindex);
650   if (netif == NULL) {
651     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("netif not found for given ifindex (%u)\n", ifindex));
652     return ERR_NODEVADDR;
653   }
654 
655 #if DRIVER_STATUS_CHECK
656   if ((!netif_is_up(netif)) || (!netif_is_ready(netif)))
657 #else
658   if ((!netif_is_up(netif)))
659 #endif
660   {
661     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("netif was down for given ifindex (%u)\n", ifindex));
662     return ERR_NETDOWN;
663   }
664 
665   if ((p->tot_len - (SIZEOF_ETH_HDR - ETH_PAD_SIZE)) > netif->mtu) {
666     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("Message too long (%u)\n", p->tot_len));
667     return ERR_MSGSIZE;
668   }
669 
670   if (pbuf_header(p, ETH_PAD_SIZE) == 0) {
671     p->flags = (u16_t)(p->flags & ~(PBUF_FLAG_LLMCAST | PBUF_FLAG_LLBCAST | PBUF_FLAG_HOST));
672     p->flags |= PBUF_FLAG_OUTGOING;
673     raw_pkt_input(p, netif, pcb);
674     (void)pbuf_header(p, -ETH_PAD_SIZE);
675   }
676 
677   /* For RAW packets of PF_PACKET family, do not modify the packets as it is
678      already supposed to contain the link layer header. So send directly to the driver */
679   netif->drv_send(netif, p);
680   LINK_STATS_INC(link.xmit);
681   return ERR_OK;
682 }
683 #endif
684 
685 /**
686  * @ingroup raw_raw
687  * Send the raw IP packet to the given address. An IP header will be prepended
688  * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that
689  * case, the packet must include an IP header, which will then be sent as is.
690  *
691  * @param pcb the raw pcb which to send
692  * @param p the IP payload to send
693  * @param ipaddr the destination address of the IP packet
694  *
695  */
696 err_t
raw_sendto(struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * ipaddr)697 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
698 {
699   struct netif *netif;
700   const ip_addr_t *src_ip;
701 
702   if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
703     return ERR_VAL;
704   }
705 
706   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
707 
708   if (pcb->netif_idx != NETIF_NO_INDEX) {
709     netif = netif_get_by_index(pcb->netif_idx);
710   } else {
711 #if LWIP_MULTICAST_TX_OPTIONS
712     netif = NULL;
713     if (ip_addr_ismulticast(ipaddr)) {
714       /* For multicast-destined packets, use the user-provided interface index to
715        * determine the outgoing interface, if an interface index is set and a
716        * matching netif can be found. Otherwise, fall back to regular routing. */
717       netif = netif_get_by_index(pcb->mcast_ifindex);
718     }
719 
720     if (netif == NULL)
721 #endif /* LWIP_MULTICAST_TX_OPTIONS */
722     {
723       netif = ip_route_pcb(ipaddr, (struct ip_pcb*)pcb);
724     }
725   }
726 
727   if (netif == NULL) {
728     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
729     ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
730     return ERR_RTE;
731   }
732 
733   if (ip_addr_isany(&pcb->local_ip) || ip_get_option(pcb, SOF_BINDNONUNICAST)) {
734     /* use outgoing network interface IP address as source address */
735     src_ip = ip_netif_get_local_ip(netif, ipaddr);
736 #if LWIP_IPV6
737     if (src_ip == NULL) {
738       return ERR_RTE;
739     }
740 #endif /* LWIP_IPV6 */
741   } else {
742     /* use RAW PCB local IP address as source address */
743     src_ip = &pcb->local_ip;
744   }
745 
746   return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
747 }
748 
749 /**
750  * @ingroup raw_raw
751  * Send the raw IP packet to the given address, using a particular outgoing
752  * netif and source IP address. An IP header will be prepended to the packet,
753  * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
754  * packet must include an IP header, which will then be sent as is.
755  *
756  * @param pcb RAW PCB used to send the data
757  * @param p chain of pbufs to be sent
758  * @param dst_ip destination IP address
759  * @param netif the netif used for sending
760  * @param src_ip source IP address
761  */
762 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)763 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
764                   struct netif *netif, const ip_addr_t *src_ip)
765 {
766   err_t err;
767   struct pbuf *q; /* q will be sent down the stack */
768   u16_t header_size;
769   u8_t ttl;
770 
771   LWIP_ASSERT_CORE_LOCKED();
772 
773   if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
774       !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
775     return ERR_VAL;
776   }
777 
778 #if LWIP_IPV4 && IP_SOF_BROADCAST
779   /* broadcast filter? */
780   if (!ip_get_option(pcb, SOF_BROADCAST) &&
781 #if LWIP_IPV6
782       IP_IS_V4(dst_ip) &&
783 #endif /* LWIP_IPV6 */
784       ip_addr_isbroadcast(dst_ip, netif)) {
785     LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
786                 ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
787     return ERR_ACCESS;
788   }
789 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
790 
791   header_size = (
792 #if LWIP_IPV4 && LWIP_IPV6
793                   IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
794 #elif LWIP_IPV4
795                   IP_HLEN);
796 #else
797                   IP6_HLEN);
798 #endif
799 
800   /* Handle the HDRINCL option as an exception: none of the code below applies
801    * to this case, and sending the packet needs to be done differently too. */
802   if (pcb->flags & RAW_FLAGS_HDRINCL) {
803     if (netif->mtu && (p->tot_len > netif->mtu)) {
804       return ERR_VAL;
805     }
806 
807     /* A full header *must* be present in the first pbuf of the chain, as the
808      * output routines may access its fields directly. */
809     if (p->len < header_size) {
810       return ERR_VAL;
811     }
812 
813   /* Multicast Loop? */
814 #if LWIP_MULTICAST_TX_OPTIONS
815     if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
816       p->flags |= PBUF_FLAG_MCASTLOOP;
817     }
818 #endif /* LWIP_MULTICAST_TX_OPTIONS */
819 
820 #if LWIP_SO_DONTROUTE
821     if (ip_get_option((struct ip_pcb *)pcb, SOF_DONTROUTE)) {
822       p->flags |= PBUF_FLAG_IS_LINK_ONLY;
823     }
824 #endif /* LWIP_SO_DONTROUTE */
825 
826     if (IP_IS_V4(dst_ip)) {
827       struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
828       iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
829     }
830 #if LWIP_IPV6
831     else {
832       struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
833       ip6_addr_copy_to_packed(ip6hdr->dest, *ip_2_ip6(dst_ip));
834     }
835 #endif
836     /* @todo multicast loop support, if at all desired for this scenario.. */
837     NETIF_SET_HINTS(netif, &pcb->netif_hints);
838     err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
839     NETIF_RESET_HINTS(netif);
840     return err;
841   }
842 
843   /* packet too large to add an IP header without causing an overflow? */
844   if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
845     return ERR_MEM;
846   }
847   /* not enough space to add an IP header to first pbuf in given p chain? */
848   if (pbuf_add_header(p, header_size)) {
849     /* allocate header in new pbuf */
850     q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
851     /* new header pbuf could not be allocated? */
852     if (q == NULL) {
853       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
854       return ERR_MEM;
855     }
856     if (p->tot_len != 0) {
857 #if LWIP_SO_PRIORITY
858       q->priority = p->priority;
859 #endif /* LWIP_SO_PRIORITY */
860       /* chain header q in front of given pbuf p */
861       pbuf_chain(q, p);
862     }
863     /* { first pbuf q points to header pbuf } */
864     LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
865   } else {
866     /* first pbuf q equals given pbuf */
867     q = p;
868     if (pbuf_remove_header(q, header_size)) {
869       LWIP_ASSERT("Can't restore header we just removed!", 0);
870       return ERR_MEM;
871     }
872   }
873 
874 #if IP_SOF_BROADCAST
875   if (IP_IS_V4(dst_ip)) {
876     /* broadcast filter? */
877     if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
878       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
879       /* free any temporary header pbuf allocated by pbuf_header() */
880       if (q != p) {
881         pbuf_free(q);
882       }
883       return ERR_VAL;
884     }
885   }
886 #endif /* IP_SOF_BROADCAST */
887 
888   /* Multicast Loop? */
889 #if LWIP_MULTICAST_TX_OPTIONS
890   if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
891     q->flags |= PBUF_FLAG_MCASTLOOP;
892   }
893 #endif /* LWIP_MULTICAST_TX_OPTIONS */
894 
895 #if LWIP_SO_DONTROUTE
896   if (ip_get_option((struct ip_pcb *)pcb, SOF_DONTROUTE)) {
897     q->flags |= PBUF_FLAG_IS_LINK_ONLY;
898   }
899 #endif /* LWIP_SO_DONTROUTE */
900 
901 #if LWIP_IPV6
902   /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
903      compute the checksum and update the checksum in the payload. */
904   if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
905     if (p->len >= (pcb->chksum_offset + LWIP_IPV6_CHKSUM_LEN)) {
906       u16_t chksum;
907       switch (pcb->raw_proto) {
908         case IP6_NEXTH_ICMP6:
909           if (pcb->chksum_offset != IPV6_ICMP_CHKSUM_OFFSET) {
910             err = ERR_VAL;
911             LWIP_DEBUGF(RAW_DEBUG,
912                         ("raw_sendto: chksum offset = %"U16_F" value not matching to ICMP6 proto chksum offset = %"U16_F" \n",
913                          pcb->chksum_offset, IPV6_ICMP_CHKSUM_OFFSET));
914             goto check_and_free_header;
915           }
916           break;
917         case IP6_NEXTH_UDP:
918           if (pcb->chksum_offset != IPV6_UDP_CHKSUM_OFFSET) {
919             err = ERR_VAL;
920             LWIP_DEBUGF(RAW_DEBUG,
921                         ("raw_sendto: chksum offset = %"U16_F" value not matching to UDP proto chksum offset = %"U16_F" \n",
922                          pcb->chksum_offset, IPV6_UDP_CHKSUM_OFFSET));
923             goto check_and_free_header;
924           }
925           break;
926         case IP6_NEXTH_TCP:
927           if (pcb->chksum_offset != IPV6_TCP_CHKSUM_OFFSET) {
928             err = ERR_VAL;
929             LWIP_DEBUGF(RAW_DEBUG,
930                         ("raw_sendto: chksum offset = %"U16_F" value not matching to TCP proto chksum offset = %"U16_F" \n",
931                          pcb->chksum_offset, IPV6_TCP_CHKSUM_OFFSET));
932             goto check_and_free_header;
933           }
934           break;
935         default:
936           /* default proto will have to processed and the offset need to added */
937           if (q != p) {
938             (void)pbuf_free(q);
939             return ERR_VAL;
940           }
941           break;
942       }
943 
944       /* Clear the checksum field before inserting checksum */
945       (void)memset_s(((u8_t *)p->payload) + pcb->chksum_offset, sizeof(u16_t), 0, sizeof(u16_t));
946       chksum = ip6_chksum_pseudo(p, pcb->raw_proto, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
947       LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + RAW_CHKSUM_OFFSET));
948       SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
949     } else {
950       LWIP_DEBUGF(RAW_DEBUG,
951                   ("raw_sendto: chksum offset = %"U16_F" value is not within the packet length = %"U16_F" \n",
952                    pcb->chksum_offset, p->len));
953       err = ERR_VAL;
954       goto check_and_free_header;
955     }
956   }
957 #endif
958 #if LWIP_SO_PRIORITY
959   q->priority = pcb->priority;
960 #endif /* LWIP_SO_PRIORITY */
961 
962   /* Determine TTL to use */
963 #if LWIP_MULTICAST_TX_OPTIONS
964   ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
965 #else /* LWIP_MULTICAST_TX_OPTIONS */
966   ttl = pcb->ttl;
967 #endif /* LWIP_MULTICAST_TX_OPTIONS */
968 
969   NETIF_SET_HINTS(netif, &pcb->netif_hints);
970 
971   err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->raw_proto, netif);
972   NETIF_RESET_HINTS(netif);
973 
974 #if LWIP_IPV6
975 check_and_free_header:
976 #endif
977   /* did we chain a header earlier? */
978   if (q != p) {
979     /* free the header */
980     (void)pbuf_free(q);
981   }
982   return err;
983 }
984 
985 /**
986  * @ingroup raw_raw
987  * Send the raw IP packet to the address given by raw_connect()
988  *
989  * @param pcb the raw pcb which to send
990  * @param p the IP payload to send
991  *
992  */
993 err_t
raw_send(struct raw_pcb * pcb,struct pbuf * p)994 raw_send(struct raw_pcb *pcb, struct pbuf *p)
995 {
996   return raw_sendto(pcb, p, &pcb->remote_ip);
997 }
998 
999 /**
1000  * @ingroup raw_raw
1001  * Remove an RAW PCB.
1002  *
1003  * @param pcb RAW PCB to be removed. The PCB is removed from the list of
1004  * RAW PCB's and the data structure is freed from memory.
1005  *
1006  * @see raw_new()
1007  */
1008 void
raw_remove(struct raw_pcb * pcb)1009 raw_remove(struct raw_pcb *pcb)
1010 {
1011   struct raw_pcb *pcb2;
1012   LWIP_ASSERT_CORE_LOCKED();
1013   /* pcb to be removed is first in list? */
1014   if (raw_pcbs == pcb) {
1015     /* make list start at 2nd pcb */
1016     raw_pcbs = raw_pcbs->next;
1017     /* pcb not 1st in list */
1018   } else {
1019     for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1020       /* find pcb in raw_pcbs list */
1021       if (pcb2->next != NULL && pcb2->next == pcb) {
1022         /* remove pcb from list */
1023         pcb2->next = pcb->next;
1024         break;
1025       }
1026     }
1027   }
1028   memp_free(MEMP_RAW_PCB, pcb);
1029 }
1030 
1031 /**
1032  * @ingroup raw_raw
1033  * Create a RAW PCB.
1034  *
1035  * @return The RAW PCB which was created. NULL if the PCB data structure
1036  * could not be allocated.
1037  *
1038  * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
1039  *
1040  * @see raw_remove()
1041  */
1042 struct raw_pcb *
raw_new(u8_t proto)1043 raw_new(u8_t proto)
1044 {
1045   struct raw_pcb *pcb;
1046 
1047   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
1048   LWIP_ASSERT_CORE_LOCKED();
1049 
1050   pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
1051   /* could allocate RAW PCB? */
1052   if (pcb != NULL) {
1053     /* initialize PCB to all zeroes */
1054     memset(pcb, 0, sizeof(struct raw_pcb));
1055 #if PF_PKT_SUPPORT
1056     pcb->proto.protocol = proto;
1057 #else
1058     pcb->protocol = proto;
1059 #endif
1060 
1061     pcb->ttl = RAW_TTL;
1062 #if LWIP_MULTICAST_TX_OPTIONS
1063     raw_set_multicast_ttl(pcb, RAW_TTL);
1064 #endif /* LWIP_MULTICAST_TX_OPTIONS */
1065     pcb->next = raw_pcbs;
1066     raw_pcbs = pcb;
1067   }
1068   return pcb;
1069 }
1070 
1071 #if PF_PKT_SUPPORT
1072 /*
1073  * Create a RAW PCB for Packet family.
1074  *
1075  * @return The RAW PCB which was created. NULL if the PCB data structure
1076  * could not be allocated.
1077  *
1078  * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
1079  *
1080  * @see raw_remove()
1081  */
1082 struct raw_pcb *
raw_pkt_new(u16_t proto)1083 raw_pkt_new(u16_t proto)
1084 {
1085   struct raw_pcb *pcb = NULL;
1086 
1087   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_pkt_new\n"));
1088 
1089   pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
1090   /* could allocate RAW PCB? */
1091   if (pcb != NULL) {
1092     /* initialize PCB to all zeroes */
1093     (void)memset(pcb, 0, sizeof(struct raw_pcb));
1094     pcb->proto.eth_proto = proto;
1095     pcb->ttl = RAW_TTL;
1096     pcb->next = pkt_raw_pcbs;
1097     pkt_raw_pcbs = pcb;
1098 
1099     if (proto == htons(ETHTYPE_ALL)) {
1100       pcb->all_next = all_pkt_raw_pcbs;
1101       all_pkt_raw_pcbs = pcb;
1102     }
1103 
1104 #if LWIP_NETIF_PROMISC
1105     netif_start_promisc_mode(pcb->netifindex);
1106 #endif
1107   }
1108   return pcb;
1109 }
1110 
1111 /*
1112  * Remove an RAW PCB of packet family type
1113  *
1114  * @param pcb RAW PCB to be removed. The PCB is removed from the list of
1115  * RAW PCB's and the data structure is freed from memory.
1116  *
1117  * @see raw_pkt_new()
1118  */
1119 void
raw_pkt_remove(struct raw_pcb * pcb)1120 raw_pkt_remove(struct raw_pcb *pcb)
1121 {
1122   struct raw_pcb *pcb2 = NULL;
1123 
1124   /* NULL check */
1125   if (pcb == NULL) {
1126     return;
1127   }
1128 
1129   /* pcb to be removed is first in all_pkt list? */
1130   if (all_pkt_raw_pcbs == pcb) {
1131     /* make list start at 2nd pcb */
1132     all_pkt_raw_pcbs = all_pkt_raw_pcbs->all_next;
1133     /* pcb not 1st in list */
1134   } else {
1135     for (pcb2 = all_pkt_raw_pcbs; pcb2 != NULL; pcb2 = pcb2->all_next) {
1136       /* find pcb in all_pkt_raw_pcbs list */
1137       if (pcb2->all_next == pcb) {
1138         /* remove pcb from list */
1139         pcb2->all_next = pcb->all_next;
1140       }
1141     }
1142   }
1143 
1144   /* pcb to be removed is first in list? */
1145   if (pkt_raw_pcbs == pcb) {
1146     /* make list start at 2nd pcb */
1147     pkt_raw_pcbs = pkt_raw_pcbs->next;
1148     /* pcb not 1st in list */
1149   } else {
1150     for (pcb2 = pkt_raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1151       /* find pcb in raw_pcbs list */
1152       if (pcb2->next == pcb) {
1153         /* remove pcb from list */
1154         pcb2->next = pcb->next;
1155       }
1156     }
1157   }
1158 
1159 #if LWIP_NETIF_PROMISC
1160   netif_stop_promisc_mode(pcb->netifindex);
1161 #endif  /* LWIP_NETIF_PROMISC */
1162   memp_free(MEMP_RAW_PCB, pcb);
1163 }
1164 
1165 #if LWIP_NETIF_PROMISC
1166 /* provides the count of pkt_raw_pcbs using this netif */
pkt_raw_pcbs_using_netif(u8_t ifindex)1167 u8_t pkt_raw_pcbs_using_netif(u8_t ifindex)
1168 {
1169   struct raw_pcb *pcb = NULL;
1170   u8_t count = 0;
1171 
1172   for (pcb = pkt_raw_pcbs; pcb != NULL; pcb = pcb->next) {
1173     /* check for without bind and netif binded pakcet raw sockets */
1174     if (!pcb->netifindex || pcb->netifindex == ifindex) {
1175       count++;
1176     }
1177   }
1178   return count;
1179 }
1180 #endif /* LWIP_NETIF_PROMISC */
1181 #endif /* PF_PKT_SUPPORT */
1182 
1183 /**
1184  * @ingroup raw_raw
1185  * Create a RAW PCB for specific IP type.
1186  *
1187  * @return The RAW PCB which was created. NULL if the PCB data structure
1188  * could not be allocated.
1189  *
1190  * @param type IP address type, see @ref lwip_ip_addr_type definitions.
1191  * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
1192  * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
1193  * @param proto the protocol number (next header) of the IPv6 packet payload
1194  *              (e.g. IP6_NEXTH_ICMP6)
1195  *
1196  * @see raw_remove()
1197  */
1198 struct raw_pcb *
raw_new_ip_type(u8_t type,u8_t proto)1199 raw_new_ip_type(u8_t type, u8_t proto)
1200 {
1201   struct raw_pcb *pcb;
1202   LWIP_ASSERT_CORE_LOCKED();
1203   pcb = raw_new(proto);
1204 #if LWIP_IPV4 && LWIP_IPV6
1205   if (pcb != NULL) {
1206     IP_SET_TYPE_VAL(pcb->local_ip,  type);
1207     IP_SET_TYPE_VAL(pcb->remote_ip, type);
1208   }
1209 #else /* LWIP_IPV4 && LWIP_IPV6 */
1210   LWIP_UNUSED_ARG(type);
1211 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1212   return pcb;
1213 }
1214 
1215 /** This function is called from netif.c when address is changed
1216  *
1217  * @param old_addr IP address of the netif before change
1218  * @param new_addr IP address of the netif after change
1219  */
raw_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)1220 void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
1221 {
1222   struct raw_pcb *rpcb;
1223 
1224   if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
1225     for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
1226       /* PCB bound to current local interface address? */
1227       if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
1228         /* The PCB is bound to the old ipaddr and
1229          * is set to bound to the new one instead */
1230         ip_addr_copy(rpcb->local_ip, *new_addr);
1231       }
1232     }
1233   }
1234 }
1235 
1236 #endif /* LWIP_RAW */
1237