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
raw_input_match(struct raw_pcb * pcb,u8_t broadcast)68 raw_input_match(struct raw_pcb *pcb, u8_t broadcast)
69 {
70 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
71
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)) {
77 return 0;
78 }
79 #endif /* IP_SOF_BROADCAST_RECV */
80 return 1;
81 }
82 #endif /* LWIP_IPV4 && LWIP_IPV6 */
83
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())) {
86 #if LWIP_IPV4
87 /* Special case: IPv4 broadcast: receive all broadcasts
88 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
89 if (broadcast != 0) {
90 #if IP_SOF_BROADCAST_RECV
91 if (ip_get_option(pcb, SOF_BROADCAST))
92 #endif /* IP_SOF_BROADCAST_RECV */
93 {
94 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
95 return 1;
96 }
97 }
98 } else
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())) {
103 return 1;
104 }
105 }
106
107 return 0;
108 }
109
110 /**
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.
113 *
114 * Given an incoming IP datagram (as a chain of pbufs) this function
115 * finds a corresponding RAW PCB and calls the corresponding receive
116 * callback function.
117 *
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
124 * caller).
125 *
126 */
127 u8_t
raw_input(struct pbuf * p,struct netif * inp)128 raw_input(struct pbuf *p, struct netif *inp)
129 {
130 struct raw_pcb *pcb, *prev;
131 s16_t proto;
132 u8_t eaten = 0;
133 u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
134
135 LWIP_UNUSED_ARG(inp);
136
137 #if LWIP_IPV6
138 #if LWIP_IPV4
139 if (IP_HDR_GET_VERSION(p->payload) == 6)
140 #endif /* LWIP_IPV4 */
141 {
142 struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
143 proto = IP6H_NEXTH(ip6hdr);
144 }
145 #if LWIP_IPV4
146 else
147 #endif /* LWIP_IPV4 */
148 #endif /* LWIP_IPV6 */
149 #if LWIP_IPV4
150 {
151 proto = IPH_PROTO((struct ip_hdr *)p->payload);
152 }
153 #endif /* LWIP_IPV4 */
154
155 prev = NULL;
156 pcb = raw_pcbs;
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_match(pcb, broadcast)) {
161 /* receive callback function available? */
162 if (pcb->recv != NULL) {
163 #ifndef LWIP_NOASSERT
164 void* old_payload = p->payload;
165 #endif
166 /* the receive callback function did not eat the packet? */
167 eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
168 if (eaten != 0) {
169 /* receive function ate the packet */
170 p = NULL;
171 eaten = 1;
172 if (prev != NULL) {
173 /* move the pcb to the front of raw_pcbs so that is
174 found faster next time */
175 prev->next = pcb->next;
176 pcb->next = raw_pcbs;
177 raw_pcbs = pcb;
178 }
179 } else {
180 /* sanity-check that the receive callback did not alter the pbuf */
181 LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
182 p->payload == old_payload);
183 }
184 }
185 /* no receive callback function was set for this raw PCB */
186 }
187 /* drop the packet */
188 prev = pcb;
189 pcb = pcb->next;
190 }
191 return eaten;
192 }
193
194 /**
195 * @ingroup raw_raw
196 * Bind a RAW PCB.
197 *
198 * @param pcb RAW PCB to be bound with a local address ipaddr.
199 * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
200 * bind to all local interfaces.
201 *
202 * @return lwIP error code.
203 * - ERR_OK. Successful. No error occurred.
204 * - ERR_USE. The specified IP address is already bound to by
205 * another RAW PCB.
206 *
207 * @see raw_disconnect()
208 */
209 err_t
raw_bind(struct raw_pcb * pcb,const ip_addr_t * ipaddr)210 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
211 {
212 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
213 return ERR_VAL;
214 }
215 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
216 return ERR_OK;
217 }
218
219 /**
220 * @ingroup raw_raw
221 * Connect an RAW PCB. This function is required by upper layers
222 * of lwip. Using the raw api you could use raw_sendto() instead
223 *
224 * This will associate the RAW PCB with the remote address.
225 *
226 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
227 * @param ipaddr remote IP address to connect with.
228 *
229 * @return lwIP error code
230 *
231 * @see raw_disconnect() and raw_sendto()
232 */
233 err_t
raw_connect(struct raw_pcb * pcb,const ip_addr_t * ipaddr)234 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
235 {
236 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
237 return ERR_VAL;
238 }
239 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
240 return ERR_OK;
241 }
242
243 /**
244 * @ingroup raw_raw
245 * Set the callback function for received packets that match the
246 * raw PCB's protocol and binding.
247 *
248 * The callback function MUST either
249 * - eat the packet by calling pbuf_free() and returning non-zero. The
250 * packet will not be passed to other raw PCBs or other protocol layers.
251 * - not free the packet, and return zero. The packet will be matched
252 * against further PCBs and/or forwarded to another protocol layers.
253 */
254 void
raw_recv(struct raw_pcb * pcb,raw_recv_fn recv,void * recv_arg)255 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
256 {
257 /* remember recv() callback and user data */
258 pcb->recv = recv;
259 pcb->recv_arg = recv_arg;
260 }
261
262 /**
263 * @ingroup raw_raw
264 * Send the raw IP packet to the given address. Note that actually you cannot
265 * modify the IP headers (this is inconsistent with the receive callback where
266 * you actually get the IP headers), you can only specify the IP payload here.
267 * It requires some more changes in lwIP. (there will be a raw_send() function
268 * then.)
269 *
270 * @param pcb the raw pcb which to send
271 * @param p the IP payload to send
272 * @param ipaddr the destination address of the IP packet
273 *
274 */
275 err_t
raw_sendto(struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * ipaddr)276 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
277 {
278 err_t err;
279 struct netif *netif;
280 const ip_addr_t *src_ip;
281 struct pbuf *q; /* q will be sent down the stack */
282 s16_t header_size;
283 const ip_addr_t *dst_ip = ipaddr;
284
285 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
286 return ERR_VAL;
287 }
288
289 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
290
291 header_size = (
292 #if LWIP_IPV4 && LWIP_IPV6
293 IP_IS_V6(ipaddr) ? IP6_HLEN : IP_HLEN);
294 #elif LWIP_IPV4
295 IP_HLEN);
296 #else
297 IP6_HLEN);
298 #endif
299
300 /* not enough space to add an IP header to first pbuf in given p chain? */
301 if (pbuf_header(p, header_size)) {
302 /* allocate header in new pbuf */
303 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
304 /* new header pbuf could not be allocated? */
305 if (q == NULL) {
306 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
307 return ERR_MEM;
308 }
309 if (p->tot_len != 0) {
310 /* chain header q in front of given pbuf p */
311 pbuf_chain(q, p);
312 }
313 /* { first pbuf q points to header pbuf } */
314 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
315 } else {
316 /* first pbuf q equals given pbuf */
317 q = p;
318 if (pbuf_header(q, -header_size)) {
319 LWIP_ASSERT("Can't restore header we just removed!", 0);
320 return ERR_MEM;
321 }
322 }
323
324 netif = ip_route(&pcb->local_ip, dst_ip);
325 if (netif == NULL) {
326 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
327 ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, dst_ip);
328 /* free any temporary header pbuf allocated by pbuf_header() */
329 if (q != p) {
330 pbuf_free(q);
331 }
332 return ERR_RTE;
333 }
334
335 #if IP_SOF_BROADCAST
336 if (IP_IS_V4(ipaddr))
337 {
338 /* broadcast filter? */
339 if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
340 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
341 /* free any temporary header pbuf allocated by pbuf_header() */
342 if (q != p) {
343 pbuf_free(q);
344 }
345 return ERR_VAL;
346 }
347 }
348 #endif /* IP_SOF_BROADCAST */
349
350 if (ip_addr_isany(&pcb->local_ip)) {
351 /* use outgoing network interface IP address as source address */
352 src_ip = ip_netif_get_local_ip(netif, dst_ip);
353 #if LWIP_IPV6
354 if (src_ip == NULL) {
355 if (q != p) {
356 pbuf_free(q);
357 }
358 return ERR_RTE;
359 }
360 #endif /* LWIP_IPV6 */
361 } else {
362 /* use RAW PCB local IP address as source address */
363 src_ip = &pcb->local_ip;
364 }
365
366 #if LWIP_IPV6
367 /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
368 compute the checksum and update the checksum in the payload. */
369 if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
370 u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
371 LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
372 SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
373 }
374 #endif
375
376 NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
377 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, pcb->protocol, netif);
378 NETIF_SET_HWADDRHINT(netif, NULL);
379
380 /* did we chain a header earlier? */
381 if (q != p) {
382 /* free the header */
383 pbuf_free(q);
384 }
385 return err;
386 }
387
388 /**
389 * @ingroup raw_raw
390 * Send the raw IP packet to the address given by raw_connect()
391 *
392 * @param pcb the raw pcb which to send
393 * @param p the IP payload to send
394 *
395 */
396 err_t
raw_send(struct raw_pcb * pcb,struct pbuf * p)397 raw_send(struct raw_pcb *pcb, struct pbuf *p)
398 {
399 return raw_sendto(pcb, p, &pcb->remote_ip);
400 }
401
402 /**
403 * @ingroup raw_raw
404 * Remove an RAW PCB.
405 *
406 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
407 * RAW PCB's and the data structure is freed from memory.
408 *
409 * @see raw_new()
410 */
411 void
raw_remove(struct raw_pcb * pcb)412 raw_remove(struct raw_pcb *pcb)
413 {
414 struct raw_pcb *pcb2;
415 /* pcb to be removed is first in list? */
416 if (raw_pcbs == pcb) {
417 /* make list start at 2nd pcb */
418 raw_pcbs = raw_pcbs->next;
419 /* pcb not 1st in list */
420 } else {
421 for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
422 /* find pcb in raw_pcbs list */
423 if (pcb2->next != NULL && pcb2->next == pcb) {
424 /* remove pcb from list */
425 pcb2->next = pcb->next;
426 break;
427 }
428 }
429 }
430 memp_free(MEMP_RAW_PCB, pcb);
431 }
432
433 /**
434 * @ingroup raw_raw
435 * Create a RAW PCB.
436 *
437 * @return The RAW PCB which was created. NULL if the PCB data structure
438 * could not be allocated.
439 *
440 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
441 *
442 * @see raw_remove()
443 */
444 struct raw_pcb *
raw_new(u8_t proto)445 raw_new(u8_t proto)
446 {
447 struct raw_pcb *pcb;
448
449 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
450
451 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
452 /* could allocate RAW PCB? */
453 if (pcb != NULL) {
454 /* initialize PCB to all zeroes */
455 memset(pcb, 0, sizeof(struct raw_pcb));
456 pcb->protocol = proto;
457 pcb->ttl = RAW_TTL;
458 pcb->next = raw_pcbs;
459 raw_pcbs = pcb;
460 }
461 return pcb;
462 }
463
464 /**
465 * @ingroup raw_raw
466 * Create a RAW PCB for specific IP type.
467 *
468 * @return The RAW PCB which was created. NULL if the PCB data structure
469 * could not be allocated.
470 *
471 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
472 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
473 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
474 * @param proto the protocol number (next header) of the IPv6 packet payload
475 * (e.g. IP6_NEXTH_ICMP6)
476 *
477 * @see raw_remove()
478 */
479 struct raw_pcb *
raw_new_ip_type(u8_t type,u8_t proto)480 raw_new_ip_type(u8_t type, u8_t proto)
481 {
482 struct raw_pcb *pcb;
483 pcb = raw_new(proto);
484 #if LWIP_IPV4 && LWIP_IPV6
485 if (pcb != NULL) {
486 IP_SET_TYPE_VAL(pcb->local_ip, type);
487 IP_SET_TYPE_VAL(pcb->remote_ip, type);
488 }
489 #else /* LWIP_IPV4 && LWIP_IPV6 */
490 LWIP_UNUSED_ARG(type);
491 #endif /* LWIP_IPV4 && LWIP_IPV6 */
492 return pcb;
493 }
494
495 /** This function is called from netif.c when address is changed
496 *
497 * @param old_addr IP address of the netif before change
498 * @param new_addr IP address of the netif after change
499 */
raw_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)500 void raw_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
501 {
502 struct raw_pcb* rpcb;
503
504 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
505 for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
506 /* PCB bound to current local interface address? */
507 if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
508 /* The PCB is bound to the old ipaddr and
509 * is set to bound to the new one instead */
510 ip_addr_copy(rpcb->local_ip, *new_addr);
511 }
512 }
513 }
514 }
515
516 #endif /* LWIP_RAW */
517