source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/lwip-2.1.2/src/core/raw.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

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