BadVPN – Blame information for rev 1

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