• 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.<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 #ifdef LOSCFG_NET_CONTAINER
139   struct net_group *inp_net_group = get_net_group_from_netif(inp);
140 #endif
141   s16_t proto;
142   raw_input_state_t ret = RAW_INPUT_NONE;
143   u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
144 
145   LWIP_UNUSED_ARG(inp);
146 
147 #if LWIP_IPV6
148 #if LWIP_IPV4
149   if (IP_HDR_GET_VERSION(p->payload) == 6)
150 #endif /* LWIP_IPV4 */
151   {
152     struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
153     proto = IP6H_NEXTH(ip6hdr);
154   }
155 #if LWIP_IPV4
156   else
157 #endif /* LWIP_IPV4 */
158 #endif /* LWIP_IPV6 */
159 #if LWIP_IPV4
160   {
161     proto = IPH_PROTO((struct ip_hdr *)p->payload);
162   }
163 #endif /* LWIP_IPV4 */
164 
165   prev = NULL;
166   pcb = raw_pcbs;
167   /* loop through all raw pcbs until the packet is eaten by one */
168   /* this allows multiple pcbs to match against the packet by design */
169   while (pcb != NULL) {
170 #ifdef LOSCFG_NET_CONTAINER
171     if (inp_net_group == get_net_group_from_raw_pcb(pcb) &&
172         (pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
173 #else
174     if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
175 #endif
176         (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
177          ip_addr_eq(&pcb->remote_ip, ip_current_src_addr()))) {
178       /* receive callback function available? */
179       if (pcb->recv != NULL) {
180         u8_t eaten;
181 #ifndef LWIP_NOASSERT
182         void *old_payload = p->payload;
183 #endif
184         ret = RAW_INPUT_DELIVERED;
185         /* the receive callback function did not eat the packet? */
186         eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
187         if (eaten != 0) {
188           /* receive function ate the packet */
189           p = NULL;
190           if (prev != NULL) {
191             /* move the pcb to the front of raw_pcbs so that is
192                found faster next time */
193             prev->next = pcb->next;
194             pcb->next = raw_pcbs;
195             raw_pcbs = pcb;
196           }
197           return RAW_INPUT_EATEN;
198         } else {
199           /* sanity-check that the receive callback did not alter the pbuf */
200           LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
201                       p->payload == old_payload);
202         }
203       }
204       /* no receive callback function was set for this raw PCB */
205     }
206     /* drop the packet */
207     prev = pcb;
208     pcb = pcb->next;
209   }
210   return ret;
211 }
212 
213 /**
214  * @ingroup raw_raw
215  * Bind a RAW PCB.
216  *
217  * @param pcb RAW PCB to be bound with a local address ipaddr.
218  * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
219  * bind to all local interfaces.
220  *
221  * @return lwIP error code.
222  * - ERR_OK. Successful. No error occurred.
223  * - ERR_USE. The specified IP address is already bound to by
224  * another RAW PCB.
225  *
226  * @see raw_disconnect()
227  */
228 err_t
raw_bind(struct raw_pcb * pcb,const ip_addr_t * ipaddr)229 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
230 {
231   LWIP_ASSERT_CORE_LOCKED();
232   if ((pcb == NULL) || (ipaddr == NULL)) {
233     return ERR_VAL;
234   }
235   ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
236 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
237   /* If the given IP address should have a zone but doesn't, assign one now.
238    * This is legacy support: scope-aware callers should always provide properly
239    * zoned source addresses. */
240   if (IP_IS_V6(&pcb->local_ip) &&
241       ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) {
242     ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip));
243   }
244 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
245   return ERR_OK;
246 }
247 
248 /**
249  * @ingroup raw_raw
250  * Bind an RAW PCB to a specific netif.
251  * After calling this function, all packets received via this PCB
252  * are guaranteed to have come in via the specified netif, and all
253  * outgoing packets will go out via the specified netif.
254  *
255  * @param pcb RAW PCB to be bound with netif.
256  * @param netif netif to bind to. Can be NULL.
257  *
258  * @see raw_disconnect()
259  */
260 void
raw_bind_netif(struct raw_pcb * pcb,const struct netif * netif)261 raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
262 {
263   LWIP_ASSERT_CORE_LOCKED();
264   if (netif != NULL) {
265     pcb->netif_idx = netif_get_index(netif);
266   } else {
267     pcb->netif_idx = NETIF_NO_INDEX;
268   }
269 }
270 
271 /**
272  * @ingroup raw_raw
273  * Connect an RAW PCB. This function is required by upper layers
274  * of lwip. Using the raw api you could use raw_sendto() instead
275  *
276  * This will associate the RAW PCB with the remote address.
277  *
278  * @param pcb RAW PCB to be connected with remote address ipaddr and port.
279  * @param ipaddr remote IP address to connect with.
280  *
281  * @return lwIP error code
282  *
283  * @see raw_disconnect() and raw_sendto()
284  */
285 err_t
raw_connect(struct raw_pcb * pcb,const ip_addr_t * ipaddr)286 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
287 {
288   LWIP_ASSERT_CORE_LOCKED();
289   if ((pcb == NULL) || (ipaddr == NULL)) {
290     return ERR_VAL;
291   }
292   ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
293 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
294   /* If the given IP address should have a zone but doesn't, assign one now,
295    * using the bound address to make a more informed decision when possible. */
296   if (IP_IS_V6(&pcb->remote_ip) &&
297       ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
298     ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
299   }
300 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
301   raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
302   return ERR_OK;
303 }
304 
305 /**
306  * @ingroup raw_raw
307  * Disconnect a RAW PCB.
308  *
309  * @param pcb the raw pcb to disconnect.
310  */
311 void
raw_disconnect(struct raw_pcb * pcb)312 raw_disconnect(struct raw_pcb *pcb)
313 {
314   LWIP_ASSERT_CORE_LOCKED();
315   /* reset remote address association */
316 #if LWIP_IPV4 && LWIP_IPV6
317   if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
318     ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
319   } else {
320 #endif
321     ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
322 #if LWIP_IPV4 && LWIP_IPV6
323   }
324 #endif
325   pcb->netif_idx = NETIF_NO_INDEX;
326   /* mark PCB as unconnected */
327   raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
328 }
329 
330 /**
331  * @ingroup raw_raw
332  * Set the callback function for received packets that match the
333  * raw PCB's protocol and binding.
334  *
335  * The callback function MUST either
336  * - eat the packet by calling pbuf_free() and returning non-zero. The
337  *   packet will not be passed to other raw PCBs or other protocol layers.
338  * - not free the packet, and return zero. The packet will be matched
339  *   against further PCBs and/or forwarded to another protocol layers.
340  */
341 void
raw_recv(struct raw_pcb * pcb,raw_recv_fn recv,void * recv_arg)342 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
343 {
344   LWIP_ASSERT_CORE_LOCKED();
345   /* remember recv() callback and user data */
346   pcb->recv = recv;
347   pcb->recv_arg = recv_arg;
348 }
349 
350 /**
351  * @ingroup raw_raw
352  * Send the raw IP packet to the given address. An IP header will be prepended
353  * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that
354  * case, the packet must include an IP header, which will then be sent as is.
355  *
356  * @param pcb the raw pcb which to send
357  * @param p the IP payload to send
358  * @param ipaddr the destination address of the IP packet
359  *
360  */
361 err_t
raw_sendto(struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * ipaddr)362 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
363 {
364   struct netif *netif;
365   const ip_addr_t *src_ip;
366 
367   if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
368     return ERR_VAL;
369   }
370 
371   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
372 
373 #ifdef LOSCFG_NET_CONTAINER
374   struct net_group *group = get_net_group_from_raw_pcb(pcb);
375 #endif
376   if (pcb->netif_idx != NETIF_NO_INDEX) {
377 #ifdef LOSCFG_NET_CONTAINER
378     netif = netif_get_by_index(pcb->netif_idx, group);
379 #else
380     netif = netif_get_by_index(pcb->netif_idx);
381 #endif
382   } else {
383 #if LWIP_MULTICAST_TX_OPTIONS
384     netif = NULL;
385     if (ip_addr_ismulticast(ipaddr)) {
386       /* For multicast-destined packets, use the user-provided interface index to
387        * determine the outgoing interface, if an interface index is set and a
388        * matching netif can be found. Otherwise, fall back to regular routing. */
389 #ifdef LOSCFG_NET_CONTAINER
390       netif = netif_get_by_index(pcb->mcast_ifindex, group);
391 #else
392       netif = netif_get_by_index(pcb->mcast_ifindex);
393 #endif
394     }
395 
396     if (netif == NULL)
397 #endif /* LWIP_MULTICAST_TX_OPTIONS */
398     {
399 #ifdef LOSCFG_NET_CONTAINER
400       netif = ip_route(&pcb->local_ip, ipaddr, group);
401 #else
402       netif = ip_route(&pcb->local_ip, ipaddr);
403 #endif
404     }
405   }
406 
407   if (netif == NULL) {
408     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
409     ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
410     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("\n"));
411     return ERR_RTE;
412   }
413 
414   if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
415     /* use outgoing network interface IP address as source address */
416     src_ip = ip_netif_get_local_ip(netif, ipaddr);
417 #if LWIP_IPV6
418     if (src_ip == NULL) {
419       return ERR_RTE;
420     }
421 #endif /* LWIP_IPV6 */
422   } else {
423     /* use RAW PCB local IP address as source address */
424     src_ip = &pcb->local_ip;
425   }
426 
427   return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
428 }
429 
430 /**
431  * @ingroup raw_raw
432  * Send the raw IP packet to the given address, using a particular outgoing
433  * netif and source IP address. An IP header will be prepended to the packet,
434  * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
435  * packet must include an IP header, which will then be sent as is.
436  *
437  * @param pcb RAW PCB used to send the data
438  * @param p chain of pbufs to be sent
439  * @param dst_ip destination IP address
440  * @param netif the netif used for sending
441  * @param src_ip source IP address
442  */
443 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)444 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
445                   struct netif *netif, const ip_addr_t *src_ip)
446 {
447   err_t err;
448   struct pbuf *q; /* q will be sent down the stack */
449   u16_t header_size;
450   u8_t ttl;
451 
452   LWIP_ASSERT_CORE_LOCKED();
453 
454   if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
455       !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
456     return ERR_VAL;
457   }
458 
459   header_size = (
460 #if LWIP_IPV4 && LWIP_IPV6
461                   IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
462 #elif LWIP_IPV4
463                   IP_HLEN);
464 #else
465                   IP6_HLEN);
466 #endif
467 
468   /* Handle the HDRINCL option as an exception: none of the code below applies
469    * to this case, and sending the packet needs to be done differently too. */
470   if (pcb->flags & RAW_FLAGS_HDRINCL) {
471     /* A full header *must* be present in the first pbuf of the chain, as the
472      * output routines may access its fields directly. */
473     if (p->len < header_size) {
474       return ERR_VAL;
475     }
476     /* @todo multicast loop support, if at all desired for this scenario.. */
477     NETIF_SET_HINTS(netif, &pcb->netif_hints);
478     err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
479     NETIF_RESET_HINTS(netif);
480     return err;
481   }
482 
483   /* packet too large to add an IP header without causing an overflow? */
484   if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
485     return ERR_MEM;
486   }
487   /* not enough space to add an IP header to first pbuf in given p chain? */
488   if (pbuf_add_header(p, header_size)) {
489     /* allocate header in new pbuf */
490     q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
491     /* new header pbuf could not be allocated? */
492     if (q == NULL) {
493       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
494       return ERR_MEM;
495     }
496     if (p->tot_len != 0) {
497       /* chain header q in front of given pbuf p */
498       pbuf_chain(q, p);
499     }
500     /* { first pbuf q points to header pbuf } */
501     LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
502   } else {
503     /* first pbuf q equals given pbuf */
504     q = p;
505     if (pbuf_remove_header(q, header_size)) {
506       LWIP_ASSERT("Can't restore header we just removed!", 0);
507       return ERR_MEM;
508     }
509   }
510 
511 #if IP_SOF_BROADCAST
512   if (IP_IS_V4(dst_ip)) {
513     /* broadcast filter? */
514     if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
515       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
516       /* free any temporary header pbuf allocated by pbuf_header() */
517       if (q != p) {
518         pbuf_free(q);
519       }
520       return ERR_VAL;
521     }
522   }
523 #endif /* IP_SOF_BROADCAST */
524 
525   /* Multicast Loop? */
526 #if LWIP_MULTICAST_TX_OPTIONS
527   if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
528     q->flags |= PBUF_FLAG_MCASTLOOP;
529   }
530 #endif /* LWIP_MULTICAST_TX_OPTIONS */
531 
532 #if LWIP_IPV6
533   /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
534      compute the checksum and update the checksum in the payload. */
535   if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
536     u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
537     LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
538     SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
539   }
540 #endif
541 
542   /* Determine TTL to use */
543 #if LWIP_MULTICAST_TX_OPTIONS
544   ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
545 #else /* LWIP_MULTICAST_TX_OPTIONS */
546   ttl = pcb->ttl;
547 #endif /* LWIP_MULTICAST_TX_OPTIONS */
548 
549   NETIF_SET_HINTS(netif, &pcb->netif_hints);
550   err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
551   NETIF_RESET_HINTS(netif);
552 
553   /* did we chain a header earlier? */
554   if (q != p) {
555     /* free the header */
556     pbuf_free(q);
557   }
558   return err;
559 }
560 
561 /**
562  * @ingroup raw_raw
563  * Send the raw IP packet to the address given by raw_connect()
564  *
565  * @param pcb the raw pcb which to send
566  * @param p the IP payload to send
567  *
568  */
569 err_t
raw_send(struct raw_pcb * pcb,struct pbuf * p)570 raw_send(struct raw_pcb *pcb, struct pbuf *p)
571 {
572   return raw_sendto(pcb, p, &pcb->remote_ip);
573 }
574 
575 /**
576  * @ingroup raw_raw
577  * Remove an RAW PCB.
578  *
579  * @param pcb RAW PCB to be removed. The PCB is removed from the list of
580  * RAW PCB's and the data structure is freed from memory.
581  *
582  * @see raw_new()
583  */
584 void
raw_remove(struct raw_pcb * pcb)585 raw_remove(struct raw_pcb *pcb)
586 {
587   struct raw_pcb *pcb2;
588   LWIP_ASSERT_CORE_LOCKED();
589   /* pcb to be removed is first in list? */
590   if (raw_pcbs == pcb) {
591     /* make list start at 2nd pcb */
592     raw_pcbs = raw_pcbs->next;
593     /* pcb not 1st in list */
594   } else {
595     for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
596       /* find pcb in raw_pcbs list */
597       if (pcb2->next != NULL && pcb2->next == pcb) {
598         /* remove pcb from list */
599         pcb2->next = pcb->next;
600         break;
601       }
602     }
603   }
604   memp_free(MEMP_RAW_PCB, pcb);
605 }
606 
607 #ifdef LOSCFG_NET_CONTAINER
set_raw_pcb_net_group(struct raw_pcb * pcb,struct net_group * group)608 void set_raw_pcb_net_group(struct raw_pcb *pcb, struct net_group *group)
609 {
610   set_ippcb_net_group((struct ip_pcb *)pcb, group);
611 }
612 
get_net_group_from_raw_pcb(struct raw_pcb * pcb)613 struct net_group *get_net_group_from_raw_pcb(struct raw_pcb *pcb) {
614   return get_net_group_from_ippcb((struct ip_pcb *)pcb);
615 }
616 #endif
617 /**
618  * @ingroup raw_raw
619  * Create a RAW PCB.
620  *
621  * @return The RAW PCB which was created. NULL if the PCB data structure
622  * could not be allocated.
623  *
624  * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
625  *
626  * @see raw_remove()
627  */
628 struct raw_pcb *
raw_new(u8_t proto)629 raw_new(u8_t proto)
630 {
631   struct raw_pcb *pcb;
632 
633   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
634   LWIP_ASSERT_CORE_LOCKED();
635 
636   pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
637   /* could allocate RAW PCB? */
638   if (pcb != NULL) {
639     /* initialize PCB to all zeroes */
640     memset(pcb, 0, sizeof(struct raw_pcb));
641     pcb->protocol = proto;
642     pcb->ttl = RAW_TTL;
643 #if LWIP_MULTICAST_TX_OPTIONS
644     raw_set_multicast_ttl(pcb, RAW_TTL);
645 #endif /* LWIP_MULTICAST_TX_OPTIONS */
646     pcb_tci_init(pcb);
647     pcb->next = raw_pcbs;
648     raw_pcbs = pcb;
649   }
650   return pcb;
651 }
652 
653 /**
654  * @ingroup raw_raw
655  * Create a RAW PCB for specific IP type.
656  *
657  * @return The RAW PCB which was created. NULL if the PCB data structure
658  * could not be allocated.
659  *
660  * @param type IP address type, see @ref lwip_ip_addr_type definitions.
661  * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
662  * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
663  * @param proto the protocol number (next header) of the IPv6 packet payload
664  *              (e.g. IP6_NEXTH_ICMP6)
665  *
666  * @see raw_remove()
667  */
668 struct raw_pcb *
raw_new_ip_type(u8_t type,u8_t proto)669 raw_new_ip_type(u8_t type, u8_t proto)
670 {
671   struct raw_pcb *pcb;
672   LWIP_ASSERT_CORE_LOCKED();
673   pcb = raw_new(proto);
674 #if LWIP_IPV4 && LWIP_IPV6
675   if (pcb != NULL) {
676     IP_SET_TYPE_VAL(pcb->local_ip,  type);
677     IP_SET_TYPE_VAL(pcb->remote_ip, type);
678   }
679 #else /* LWIP_IPV4 && LWIP_IPV6 */
680   LWIP_UNUSED_ARG(type);
681 #endif /* LWIP_IPV4 && LWIP_IPV6 */
682   return pcb;
683 }
684 
685 /** This function is called from netif.c when address is changed
686  *
687  * @param old_addr IP address of the netif before change
688  * @param new_addr IP address of the netif after change
689  */
raw_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)690 void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
691 {
692   struct raw_pcb *rpcb;
693 
694   if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
695     for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
696       /* PCB bound to current local interface address? */
697       if (ip_addr_eq(&rpcb->local_ip, old_addr)) {
698         /* The PCB is bound to the old ipaddr and
699          * is set to bound to the new one instead */
700         ip_addr_copy(rpcb->local_ip, *new_addr);
701       }
702     }
703   }
704 }
705 
706 #endif /* LWIP_RAW */
707