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
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
17 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
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.
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
42 * This file is part of the lwIP TCP/IP stack.
44 * Author: Adam Dunkels <adam@sics.se>
50 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
53 #include "lwip/memp.h"
54 #include "lwip/ip_addr.h"
55 #include "lwip/netif.h"
57 #include "lwip/stats.h"
59 #include "lwip/ip6_addr.h"
60 #include "lwip/inet_chksum.h"
64 /** The list of RAW PCBs */
65 static struct raw_pcb
*raw_pcbs
;
68 raw_input_local_match(struct raw_pcb
*pcb
, u8_t broadcast
)
70 LWIP_UNUSED_ARG(broadcast
); /* in IPv6 only case */
72 #if LWIP_IPV4 && LWIP_IPV6
73 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
74 if (IP_IS_ANY_TYPE_VAL(pcb
->local_ip
)) {
75 #if IP_SOF_BROADCAST_RECV
76 if ((broadcast
!= 0) && !ip_get_option(pcb
, SOF_BROADCAST
)) {
79 #endif /* IP_SOF_BROADCAST_RECV */
82 #endif /* LWIP_IPV4 && LWIP_IPV6 */
84 /* Only need to check PCB if incoming IP version matches PCB IP version */
85 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb
, ip_current_dest_addr())) {
87 /* Special case: IPv4 broadcast: receive all broadcasts
88 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
90 #if IP_SOF_BROADCAST_RECV
91 if (ip_get_option(pcb
, SOF_BROADCAST
))
92 #endif /* IP_SOF_BROADCAST_RECV */
94 if (ip4_addr_isany(ip_2_ip4(&pcb
->local_ip
))) {
99 #endif /* LWIP_IPV4 */
100 /* Handle IPv4 and IPv6: catch all or exact match */
101 if (ip_addr_isany(&pcb
->local_ip
) ||
102 ip_addr_cmp(&pcb
->local_ip
, ip_current_dest_addr())) {
111 * Determine if in incoming IP packet is covered by a RAW PCB
112 * and if so, pass it to a user-provided receive callback function.
114 * Given an incoming IP datagram (as a chain of pbufs) this function
115 * finds a corresponding RAW PCB and calls the corresponding receive
118 * @param p pbuf to be demultiplexed to a RAW PCB.
119 * @param inp network interface on which the datagram was received.
120 * @return - 1 if the packet has been eaten by a RAW PCB receive
121 * callback function. The caller MAY NOT not reference the
122 * packet any longer, and MAY NOT call pbuf_free().
123 * @return - 0 if packet is not eaten (pbuf is still referenced by the
128 raw_input(struct pbuf
*p
, struct netif
*inp
)
130 struct raw_pcb
*pcb
, *prev
;
133 u8_t broadcast
= ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
135 LWIP_UNUSED_ARG(inp
);
139 if (IP_HDR_GET_VERSION(p
->payload
) == 6)
140 #endif /* LWIP_IPV4 */
142 struct ip6_hdr
*ip6hdr
= (struct ip6_hdr
*)p
->payload
;
143 proto
= IP6H_NEXTH(ip6hdr
);
147 #endif /* LWIP_IPV4 */
148 #endif /* LWIP_IPV6 */
151 proto
= IPH_PROTO((struct ip_hdr
*)p
->payload
);
153 #endif /* LWIP_IPV4 */
157 /* loop through all raw pcbs until the packet is eaten by one */
158 /* this allows multiple pcbs to match against the packet by design */
159 while ((eaten
== 0) && (pcb
!= NULL
)) {
160 if ((pcb
->protocol
== proto
) && raw_input_local_match(pcb
, broadcast
) &&
161 (((pcb
->flags
& RAW_FLAGS_CONNECTED
) == 0) ||
162 ip_addr_cmp(&pcb
->remote_ip
, ip_current_src_addr()))) {
163 /* receive callback function available? */
164 if (pcb
->recv
!= NULL
) {
165 #ifndef LWIP_NOASSERT
166 void* old_payload
= p
->payload
;
168 /* the receive callback function did not eat the packet? */
169 eaten
= pcb
->recv(pcb
->recv_arg
, pcb
, p
, ip_current_src_addr());
171 /* receive function ate the packet */
175 /* move the pcb to the front of raw_pcbs so that is
176 found faster next time */
177 prev
->next
= pcb
->next
;
178 pcb
->next
= raw_pcbs
;
182 /* sanity-check that the receive callback did not alter the pbuf */
183 LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
184 p
->payload
== old_payload
);
187 /* no receive callback function was set for this raw PCB */
189 /* drop the packet */
200 * @param pcb RAW PCB to be bound with a local address ipaddr.
201 * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
202 * bind to all local interfaces.
204 * @return lwIP error code.
205 * - ERR_OK. Successful. No error occurred.
206 * - ERR_USE. The specified IP address is already bound to by
209 * @see raw_disconnect()
212 raw_bind(struct raw_pcb
*pcb
, const ip_addr_t
*ipaddr
)
214 if ((pcb
== NULL
) || (ipaddr
== NULL
)) {
217 ip_addr_set_ipaddr(&pcb
->local_ip
, ipaddr
);
218 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
219 /* If the given IP address should have a zone but doesn't, assign one now.
220 * This is legacy support: scope-aware callers should always provide properly
221 * zoned source addresses. */
222 if (IP_IS_V6(&pcb
->local_ip
) &&
223 ip6_addr_lacks_zone(ip_2_ip6(&pcb
->local_ip
), IP6_UNKNOWN
)) {
224 ip6_addr_select_zone(ip_2_ip6(&pcb
->local_ip
), ip_2_ip6(&pcb
->local_ip
));
226 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
232 * Connect an RAW PCB. This function is required by upper layers
233 * of lwip. Using the raw api you could use raw_sendto() instead
235 * This will associate the RAW PCB with the remote address.
237 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
238 * @param ipaddr remote IP address to connect with.
240 * @return lwIP error code
242 * @see raw_disconnect() and raw_sendto()
245 raw_connect(struct raw_pcb
*pcb
, const ip_addr_t
*ipaddr
)
247 if ((pcb
== NULL
) || (ipaddr
== NULL
)) {
250 ip_addr_set_ipaddr(&pcb
->remote_ip
, ipaddr
);
251 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
252 /* If the given IP address should have a zone but doesn't, assign one now,
253 * using the bound address to make a more informed decision when possible. */
254 if (IP_IS_V6(&pcb
->remote_ip
) &&
255 ip6_addr_lacks_zone(ip_2_ip6(&pcb
->remote_ip
), IP6_UNKNOWN
)) {
256 ip6_addr_select_zone(ip_2_ip6(&pcb
->remote_ip
), ip_2_ip6(&pcb
->local_ip
));
258 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
259 pcb
->flags
|= RAW_FLAGS_CONNECTED
;
265 * Disconnect a RAW PCB.
267 * @param pcb the raw pcb to disconnect.
270 raw_disconnect(struct raw_pcb
*pcb
)
272 /* reset remote address association */
273 #if LWIP_IPV4 && LWIP_IPV6
274 if (IP_IS_ANY_TYPE_VAL(pcb
->local_ip
)) {
275 ip_addr_copy(pcb
->remote_ip
, *IP_ANY_TYPE
);
278 ip_addr_set_any(IP_IS_V6_VAL(pcb
->remote_ip
), &pcb
->remote_ip
);
279 #if LWIP_IPV4 && LWIP_IPV6
282 /* mark PCB as unconnected */
283 pcb
->flags
&= ~RAW_FLAGS_CONNECTED
;
288 * Set the callback function for received packets that match the
289 * raw PCB's protocol and binding.
291 * The callback function MUST either
292 * - eat the packet by calling pbuf_free() and returning non-zero. The
293 * packet will not be passed to other raw PCBs or other protocol layers.
294 * - not free the packet, and return zero. The packet will be matched
295 * against further PCBs and/or forwarded to another protocol layers.
298 raw_recv(struct raw_pcb
*pcb
, raw_recv_fn recv
, void *recv_arg
)
300 /* remember recv() callback and user data */
302 pcb
->recv_arg
= recv_arg
;
307 * Send the raw IP packet to the given address. An IP header will be prepended
308 * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that
309 * case, the packet must include an IP header, which will then be sent as is.
311 * @param pcb the raw pcb which to send
312 * @param p the IP payload to send
313 * @param ipaddr the destination address of the IP packet
317 raw_sendto(struct raw_pcb
*pcb
, struct pbuf
*p
, const ip_addr_t
*ipaddr
)
320 const ip_addr_t
*src_ip
;
322 if ((pcb
== NULL
) || (ipaddr
== NULL
) || !IP_ADDR_PCB_VERSION_MATCH(pcb
, ipaddr
)) {
326 LWIP_DEBUGF(RAW_DEBUG
| LWIP_DBG_TRACE
, ("raw_sendto\n"));
328 #if LWIP_MULTICAST_TX_OPTIONS
330 if (ip_addr_ismulticast(ipaddr
)) {
331 /* For multicast-destined packets, use the user-provided interface index to
332 * determine the outgoing interface, if an interface index is set and a
333 * matching netif can be found. Otherwise, fall back to regular routing. */
334 netif
= netif_get_by_index(pcb
->mcast_ifindex
);
338 #endif /* LWIP_MULTICAST_TX_OPTIONS */
340 if (IP_IS_ANY_TYPE_VAL(pcb
->local_ip
)) {
341 /* Don't call ip_route() with IP_ANY_TYPE */
342 netif
= ip_route(IP46_ADDR_ANY(IP_GET_TYPE(ipaddr
)), ipaddr
);
344 netif
= ip_route(&pcb
->local_ip
, ipaddr
);
349 LWIP_DEBUGF(RAW_DEBUG
| LWIP_DBG_LEVEL_WARNING
, ("raw_sendto: No route to "));
350 ip_addr_debug_print(RAW_DEBUG
| LWIP_DBG_LEVEL_WARNING
, ipaddr
);
354 if (ip_addr_isany(&pcb
->local_ip
) || ip_addr_ismulticast(&pcb
->local_ip
)) {
355 /* use outgoing network interface IP address as source address */
356 src_ip
= ip_netif_get_local_ip(netif
, ipaddr
);
358 if (src_ip
== NULL
) {
361 #endif /* LWIP_IPV6 */
363 /* use RAW PCB local IP address as source address */
364 src_ip
= &pcb
->local_ip
;
367 return raw_sendto_if_src(pcb
, p
, ipaddr
, netif
, src_ip
);
372 * Send the raw IP packet to the given address, using a particular outgoing
373 * netif and source IP address. An IP header will be prepended to the packet,
374 * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
375 * packet must include an IP header, which will then be sent as is.
377 * @param pcb RAW PCB used to send the data
378 * @param p chain of pbufs to be sent
379 * @param dst_ip destination IP address
380 * @param netif the netif used for sending
381 * @param src_ip source IP address
384 raw_sendto_if_src(struct raw_pcb
*pcb
, struct pbuf
*p
, const ip_addr_t
*dst_ip
,
385 struct netif
*netif
, const ip_addr_t
*src_ip
)
388 struct pbuf
*q
; /* q will be sent down the stack */
392 if ((pcb
== NULL
) || (dst_ip
== NULL
) || (netif
== NULL
) || (src_ip
== NULL
) ||
393 !IP_ADDR_PCB_VERSION_MATCH(pcb
, src_ip
) || !IP_ADDR_PCB_VERSION_MATCH(pcb
, dst_ip
)) {
398 #if LWIP_IPV4 && LWIP_IPV6
399 IP_IS_V6(dst_ip
) ? IP6_HLEN
: IP_HLEN
);
406 /* Handle the HDRINCL option as an exception: none of the code below applies
407 * to this case, and sending the packet needs to be done differently too. */
408 if (pcb
->flags
& RAW_FLAGS_HDRINCL
) {
409 /* A full header *must* be present in the first pbuf of the chain, as the
410 * output routines may access its fields directly. */
411 if (p
->len
< header_size
) {
414 /* @todo multicast loop support, if at all desired for this scenario.. */
415 NETIF_SET_HWADDRHINT(netif
, &pcb
->addr_hint
);
416 err
= ip_output_if_hdrincl(p
, src_ip
, dst_ip
, netif
);
417 NETIF_SET_HWADDRHINT(netif
, NULL
);
421 /* packet too large to add an IP header without causing an overflow? */
422 if ((u16_t
)(p
->tot_len
+ header_size
) < p
->tot_len
) {
425 /* not enough space to add an IP header to first pbuf in given p chain? */
426 if (pbuf_header(p
, header_size
)) {
427 /* allocate header in new pbuf */
428 q
= pbuf_alloc(PBUF_IP
, 0, PBUF_RAM
);
429 /* new header pbuf could not be allocated? */
431 LWIP_DEBUGF(RAW_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_LEVEL_SERIOUS
, ("raw_sendto: could not allocate header\n"));
434 if (p
->tot_len
!= 0) {
435 /* chain header q in front of given pbuf p */
438 /* { first pbuf q points to header pbuf } */
439 LWIP_DEBUGF(RAW_DEBUG
, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q
, (void *)p
));
441 /* first pbuf q equals given pbuf */
443 if (pbuf_header(q
, -header_size
)) {
444 LWIP_ASSERT("Can't restore header we just removed!", 0);
450 if (IP_IS_V4(dst_ip
))
452 /* broadcast filter? */
453 if (!ip_get_option(pcb
, SOF_BROADCAST
) && ip_addr_isbroadcast(dst_ip
, netif
)) {
454 LWIP_DEBUGF(RAW_DEBUG
| LWIP_DBG_LEVEL_WARNING
, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb
));
455 /* free any temporary header pbuf allocated by pbuf_header() */
462 #endif /* IP_SOF_BROADCAST */
464 /* Multicast Loop? */
465 #if LWIP_MULTICAST_TX_OPTIONS
466 if (((pcb
->flags
& RAW_FLAGS_MULTICAST_LOOP
) != 0) && ip_addr_ismulticast(dst_ip
)) {
467 q
->flags
|= PBUF_FLAG_MCASTLOOP
;
469 #endif /* LWIP_MULTICAST_TX_OPTIONS */
472 /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
473 compute the checksum and update the checksum in the payload. */
474 if (IP_IS_V6(dst_ip
) && pcb
->chksum_reqd
) {
475 u16_t chksum
= ip6_chksum_pseudo(p
, pcb
->protocol
, p
->tot_len
, ip_2_ip6(src_ip
), ip_2_ip6(dst_ip
));
476 LWIP_ASSERT("Checksum must fit into first pbuf", p
->len
>= (pcb
->chksum_offset
+ 2));
477 SMEMCPY(((u8_t
*)p
->payload
) + pcb
->chksum_offset
, &chksum
, sizeof(u16_t
));
481 /* Determine TTL to use */
482 #if LWIP_MULTICAST_TX_OPTIONS
483 ttl
= (ip_addr_ismulticast(dst_ip
) ? raw_get_multicast_ttl(pcb
) : pcb
->ttl
);
484 #else /* LWIP_MULTICAST_TX_OPTIONS */
486 #endif /* LWIP_MULTICAST_TX_OPTIONS */
488 NETIF_SET_HWADDRHINT(netif
, &pcb
->addr_hint
);
489 err
= ip_output_if(q
, src_ip
, dst_ip
, ttl
, pcb
->tos
, pcb
->protocol
, netif
);
490 NETIF_SET_HWADDRHINT(netif
, NULL
);
492 /* did we chain a header earlier? */
494 /* free the header */
502 * Send the raw IP packet to the address given by raw_connect()
504 * @param pcb the raw pcb which to send
505 * @param p the IP payload to send
509 raw_send(struct raw_pcb
*pcb
, struct pbuf
*p
)
511 return raw_sendto(pcb
, p
, &pcb
->remote_ip
);
518 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
519 * RAW PCB's and the data structure is freed from memory.
524 raw_remove(struct raw_pcb
*pcb
)
526 struct raw_pcb
*pcb2
;
527 /* pcb to be removed is first in list? */
528 if (raw_pcbs
== pcb
) {
529 /* make list start at 2nd pcb */
530 raw_pcbs
= raw_pcbs
->next
;
531 /* pcb not 1st in list */
533 for (pcb2
= raw_pcbs
; pcb2
!= NULL
; pcb2
= pcb2
->next
) {
534 /* find pcb in raw_pcbs list */
535 if (pcb2
->next
!= NULL
&& pcb2
->next
== pcb
) {
536 /* remove pcb from list */
537 pcb2
->next
= pcb
->next
;
542 memp_free(MEMP_RAW_PCB
, pcb
);
549 * @return The RAW PCB which was created. NULL if the PCB data structure
550 * could not be allocated.
552 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
561 LWIP_DEBUGF(RAW_DEBUG
| LWIP_DBG_TRACE
, ("raw_new\n"));
563 pcb
= (struct raw_pcb
*)memp_malloc(MEMP_RAW_PCB
);
564 /* could allocate RAW PCB? */
566 /* initialize PCB to all zeroes */
567 memset(pcb
, 0, sizeof(struct raw_pcb
));
568 pcb
->protocol
= proto
;
570 #if LWIP_MULTICAST_TX_OPTIONS
571 raw_set_multicast_ttl(pcb
, RAW_TTL
);
572 #endif /* LWIP_MULTICAST_TX_OPTIONS */
573 pcb
->next
= raw_pcbs
;
581 * Create a RAW PCB for specific IP type.
583 * @return The RAW PCB which was created. NULL if the PCB data structure
584 * could not be allocated.
586 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
587 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
588 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
589 * @param proto the protocol number (next header) of the IPv6 packet payload
590 * (e.g. IP6_NEXTH_ICMP6)
595 raw_new_ip_type(u8_t type
, u8_t proto
)
598 pcb
= raw_new(proto
);
599 #if LWIP_IPV4 && LWIP_IPV6
601 IP_SET_TYPE_VAL(pcb
->local_ip
, type
);
602 IP_SET_TYPE_VAL(pcb
->remote_ip
, type
);
604 #else /* LWIP_IPV4 && LWIP_IPV6 */
605 LWIP_UNUSED_ARG(type
);
606 #endif /* LWIP_IPV4 && LWIP_IPV6 */
610 /** This function is called from netif.c when address is changed
612 * @param old_addr IP address of the netif before change
613 * @param new_addr IP address of the netif after change
615 void raw_netif_ip_addr_changed(const ip_addr_t
* old_addr
, const ip_addr_t
* new_addr
)
617 struct raw_pcb
* rpcb
;
619 if (!ip_addr_isany(old_addr
) && !ip_addr_isany(new_addr
)) {
620 for (rpcb
= raw_pcbs
; rpcb
!= NULL
; rpcb
= rpcb
->next
) {
621 /* PCB bound to current local interface address? */
622 if (ip_addr_cmp(&rpcb
->local_ip
, old_addr
)) {
623 /* The PCB is bound to the old ipaddr and
624 * is set to bound to the new one instead */
625 ip_addr_copy(rpcb
->local_ip
, *new_addr
);
631 #endif /* LWIP_RAW */