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
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_cmp(&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_cmp(&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 return ERR_RTE;
411 }
412
413 if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
414 /* use outgoing network interface IP address as source address */
415 src_ip = ip_netif_get_local_ip(netif, ipaddr);
416 #if LWIP_IPV6
417 if (src_ip == NULL) {
418 return ERR_RTE;
419 }
420 #endif /* LWIP_IPV6 */
421 } else {
422 /* use RAW PCB local IP address as source address */
423 src_ip = &pcb->local_ip;
424 }
425
426 return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
427 }
428
429 /**
430 * @ingroup raw_raw
431 * Send the raw IP packet to the given address, using a particular outgoing
432 * netif and source IP address. An IP header will be prepended to the packet,
433 * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
434 * packet must include an IP header, which will then be sent as is.
435 *
436 * @param pcb RAW PCB used to send the data
437 * @param p chain of pbufs to be sent
438 * @param dst_ip destination IP address
439 * @param netif the netif used for sending
440 * @param src_ip source IP address
441 */
442 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)443 raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
444 struct netif *netif, const ip_addr_t *src_ip)
445 {
446 err_t err;
447 struct pbuf *q; /* q will be sent down the stack */
448 u16_t header_size;
449 u8_t ttl;
450
451 LWIP_ASSERT_CORE_LOCKED();
452
453 if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
454 !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
455 return ERR_VAL;
456 }
457
458 header_size = (
459 #if LWIP_IPV4 && LWIP_IPV6
460 IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
461 #elif LWIP_IPV4
462 IP_HLEN);
463 #else
464 IP6_HLEN);
465 #endif
466
467 /* Handle the HDRINCL option as an exception: none of the code below applies
468 * to this case, and sending the packet needs to be done differently too. */
469 if (pcb->flags & RAW_FLAGS_HDRINCL) {
470 /* A full header *must* be present in the first pbuf of the chain, as the
471 * output routines may access its fields directly. */
472 if (p->len < header_size) {
473 return ERR_VAL;
474 }
475 /* @todo multicast loop support, if at all desired for this scenario.. */
476 NETIF_SET_HINTS(netif, &pcb->netif_hints);
477 err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
478 NETIF_RESET_HINTS(netif);
479 return err;
480 }
481
482 /* packet too large to add an IP header without causing an overflow? */
483 if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
484 return ERR_MEM;
485 }
486 /* not enough space to add an IP header to first pbuf in given p chain? */
487 if (pbuf_add_header(p, header_size)) {
488 /* allocate header in new pbuf */
489 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
490 /* new header pbuf could not be allocated? */
491 if (q == NULL) {
492 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
493 return ERR_MEM;
494 }
495 if (p->tot_len != 0) {
496 /* chain header q in front of given pbuf p */
497 pbuf_chain(q, p);
498 }
499 /* { first pbuf q points to header pbuf } */
500 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
501 } else {
502 /* first pbuf q equals given pbuf */
503 q = p;
504 if (pbuf_remove_header(q, header_size)) {
505 LWIP_ASSERT("Can't restore header we just removed!", 0);
506 return ERR_MEM;
507 }
508 }
509
510 #if IP_SOF_BROADCAST
511 if (IP_IS_V4(dst_ip)) {
512 /* broadcast filter? */
513 if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
514 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
515 /* free any temporary header pbuf allocated by pbuf_header() */
516 if (q != p) {
517 pbuf_free(q);
518 }
519 return ERR_VAL;
520 }
521 }
522 #endif /* IP_SOF_BROADCAST */
523
524 /* Multicast Loop? */
525 #if LWIP_MULTICAST_TX_OPTIONS
526 if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
527 q->flags |= PBUF_FLAG_MCASTLOOP;
528 }
529 #endif /* LWIP_MULTICAST_TX_OPTIONS */
530
531 #if LWIP_IPV6
532 /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
533 compute the checksum and update the checksum in the payload. */
534 if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
535 u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
536 LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
537 SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
538 }
539 #endif
540
541 /* Determine TTL to use */
542 #if LWIP_MULTICAST_TX_OPTIONS
543 ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
544 #else /* LWIP_MULTICAST_TX_OPTIONS */
545 ttl = pcb->ttl;
546 #endif /* LWIP_MULTICAST_TX_OPTIONS */
547
548 NETIF_SET_HINTS(netif, &pcb->netif_hints);
549 err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
550 NETIF_RESET_HINTS(netif);
551
552 /* did we chain a header earlier? */
553 if (q != p) {
554 /* free the header */
555 pbuf_free(q);
556 }
557 return err;
558 }
559
560 /**
561 * @ingroup raw_raw
562 * Send the raw IP packet to the address given by raw_connect()
563 *
564 * @param pcb the raw pcb which to send
565 * @param p the IP payload to send
566 *
567 */
568 err_t
raw_send(struct raw_pcb * pcb,struct pbuf * p)569 raw_send(struct raw_pcb *pcb, struct pbuf *p)
570 {
571 return raw_sendto(pcb, p, &pcb->remote_ip);
572 }
573
574 /**
575 * @ingroup raw_raw
576 * Remove an RAW PCB.
577 *
578 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
579 * RAW PCB's and the data structure is freed from memory.
580 *
581 * @see raw_new()
582 */
583 void
raw_remove(struct raw_pcb * pcb)584 raw_remove(struct raw_pcb *pcb)
585 {
586 struct raw_pcb *pcb2;
587 LWIP_ASSERT_CORE_LOCKED();
588 /* pcb to be removed is first in list? */
589 if (raw_pcbs == pcb) {
590 /* make list start at 2nd pcb */
591 raw_pcbs = raw_pcbs->next;
592 /* pcb not 1st in list */
593 } else {
594 for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
595 /* find pcb in raw_pcbs list */
596 if (pcb2->next != NULL && pcb2->next == pcb) {
597 /* remove pcb from list */
598 pcb2->next = pcb->next;
599 break;
600 }
601 }
602 }
603 memp_free(MEMP_RAW_PCB, pcb);
604 }
605
606 #ifdef LOSCFG_NET_CONTAINER
set_raw_pcb_net_group(struct raw_pcb * pcb,struct net_group * group)607 void set_raw_pcb_net_group(struct raw_pcb *pcb, struct net_group *group)
608 {
609 set_ippcb_net_group((struct ip_pcb *)pcb, group);
610 }
611
get_net_group_from_raw_pcb(struct raw_pcb * pcb)612 struct net_group *get_net_group_from_raw_pcb(struct raw_pcb *pcb) {
613 return get_net_group_from_ippcb((struct ip_pcb *)pcb);
614 }
615 #endif
616 /**
617 * @ingroup raw_raw
618 * Create a RAW PCB.
619 *
620 * @return The RAW PCB which was created. NULL if the PCB data structure
621 * could not be allocated.
622 *
623 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
624 *
625 * @see raw_remove()
626 */
627 struct raw_pcb *
raw_new(u8_t proto)628 raw_new(u8_t proto)
629 {
630 struct raw_pcb *pcb;
631
632 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
633 LWIP_ASSERT_CORE_LOCKED();
634
635 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
636 /* could allocate RAW PCB? */
637 if (pcb != NULL) {
638 /* initialize PCB to all zeroes */
639 memset(pcb, 0, sizeof(struct raw_pcb));
640 pcb->protocol = proto;
641 pcb->ttl = RAW_TTL;
642 #if LWIP_MULTICAST_TX_OPTIONS
643 raw_set_multicast_ttl(pcb, RAW_TTL);
644 #endif /* LWIP_MULTICAST_TX_OPTIONS */
645 pcb->next = raw_pcbs;
646 raw_pcbs = pcb;
647 }
648 return pcb;
649 }
650
651 /**
652 * @ingroup raw_raw
653 * Create a RAW PCB for specific IP type.
654 *
655 * @return The RAW PCB which was created. NULL if the PCB data structure
656 * could not be allocated.
657 *
658 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
659 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
660 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
661 * @param proto the protocol number (next header) of the IPv6 packet payload
662 * (e.g. IP6_NEXTH_ICMP6)
663 *
664 * @see raw_remove()
665 */
666 struct raw_pcb *
raw_new_ip_type(u8_t type,u8_t proto)667 raw_new_ip_type(u8_t type, u8_t proto)
668 {
669 struct raw_pcb *pcb;
670 LWIP_ASSERT_CORE_LOCKED();
671 pcb = raw_new(proto);
672 #if LWIP_IPV4 && LWIP_IPV6
673 if (pcb != NULL) {
674 IP_SET_TYPE_VAL(pcb->local_ip, type);
675 IP_SET_TYPE_VAL(pcb->remote_ip, type);
676 }
677 #else /* LWIP_IPV4 && LWIP_IPV6 */
678 LWIP_UNUSED_ARG(type);
679 #endif /* LWIP_IPV4 && LWIP_IPV6 */
680 return pcb;
681 }
682
683 /** This function is called from netif.c when address is changed
684 *
685 * @param old_addr IP address of the netif before change
686 * @param new_addr IP address of the netif after change
687 */
raw_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)688 void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
689 {
690 struct raw_pcb *rpcb;
691
692 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
693 for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
694 /* PCB bound to current local interface address? */
695 if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
696 /* The PCB is bound to the old ipaddr and
697 * is set to bound to the new one instead */
698 ip_addr_copy(rpcb->local_ip, *new_addr);
699 }
700 }
701 }
702 }
703
704 #endif /* LWIP_RAW */
705