1 /**
2 * @file
3 * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
4 *
5 * @see ip_frag.c
6 *
7 */
8
9 /*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <[email protected]>
38 *
39 */
40
41 #include "lwip/opt.h"
42 #include "lwip/ip.h"
43 #include "lwip/def.h"
44 #include "lwip/mem.h"
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet_chksum.h"
47 #include "lwip/netif.h"
48 #include "lwip/icmp.h"
49 #include "lwip/igmp.h"
50 #include "lwip/raw.h"
51 #include "lwip/udp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/snmp.h"
54 #include "lwip/dhcp.h"
55 #include "lwip/autoip.h"
56 #include "lwip/stats.h"
57 #include "arch/perf.h"
58
59 #include <string.h>
60
61 /** Set this to 0 in the rare case of wanting to call an extra function to
62 * generate the IP checksum (in contrast to calculating it on-the-fly). */
63 #ifndef LWIP_INLINE_IP_CHKSUM
64 #define LWIP_INLINE_IP_CHKSUM 1
65 #endif
66 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
67 #define CHECKSUM_GEN_IP_INLINE 1
68 #else
69 #define CHECKSUM_GEN_IP_INLINE 0
70 #endif
71
72 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
73 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
74
75 /** Some defines for DHCP to let link-layer-addressed packets through while the
76 * netif is down.
77 * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT
78 * to return 1 if the port is accepted and 0 if the port is not accepted.
79 */
80 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
81 /* accept DHCP client port and custom port */
82 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
83 || (LWIP_IP_ACCEPT_UDP_PORT(port)))
84 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
85 /* accept custom port only */
86 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
87 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
88 /* accept DHCP client port only */
89 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
90 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
91
92 #else /* LWIP_DHCP */
93 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
94 #endif /* LWIP_DHCP */
95
96 /**
97 * The interface that provided the packet for the current callback
98 * invocation.
99 */
100 struct netif *current_netif;
101
102 /**
103 * Header of the input packet currently being processed.
104 */
105 const struct ip_hdr *current_header;
106 /** Source IP address of current_header */
107 ip_addr_t current_iphdr_src;
108 /** Destination IP address of current_header */
109 ip_addr_t current_iphdr_dest;
110
111 /** The IP header ID of the next outgoing IP packet */
112 static u16_t ip_id;
113
114 /**
115 * Finds the appropriate network interface for a given IP address. It
116 * searches the list of network interfaces linearly. A match is found
117 * if the masked IP address of the network interface equals the masked
118 * IP address given to the function.
119 *
120 * @param dest the destination IP address for which to find the route
121 * @return the netif on which to send to reach dest
122 */
123 struct netif *
ip_route(ip_addr_t * dest)124 ip_route(ip_addr_t *dest)
125 {
126 struct netif *netif;
127
128 #ifdef LWIP_HOOK_IP4_ROUTE
129 netif = LWIP_HOOK_IP4_ROUTE(dest);
130 if (netif != NULL) {
131 return netif;
132 }
133 #endif
134
135 /* iterate through netifs */
136 for (netif = netif_list; netif != NULL; netif = netif->next) {
137 /* network mask matches? */
138 if (netif_is_up(netif)) {
139 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
140 /* return netif on which to forward IP packet */
141 return netif;
142 }
143 }
144 }
145 if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
146 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
147 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
148 IP_STATS_INC(ip.rterr);
149 snmp_inc_ipoutnoroutes();
150 return NULL;
151 }
152 /* no matching netif found, use default netif */
153 return netif_default;
154 }
155
156 #if IP_FORWARD
157 /**
158 * Determine whether an IP address is in a reserved set of addresses
159 * that may not be forwarded, or whether datagrams to that destination
160 * may be forwarded.
161 * @param p the packet to forward
162 * @param dest the destination IP address
163 * @return 1: can forward 0: discard
164 */
165 static int
ip_canforward(struct pbuf * p)166 ip_canforward(struct pbuf *p)
167 {
168 u32_t addr = ip4_addr_get_u32(ip_current_dest_addr());
169
170 if (p->flags & PBUF_FLAG_LLBCAST) {
171 /* don't route link-layer broadcasts */
172 return 0;
173 }
174 if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) {
175 /* don't route link-layer multicasts unless the destination address is an IP
176 multicast address */
177 return 0;
178 }
179 if (IP_EXPERIMENTAL(addr)) {
180 return 0;
181 }
182 if (IP_CLASSA(addr)) {
183 u32_t net = addr & IP_CLASSA_NET;
184 if ((net == 0) || (net == (IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
185 /* don't route loopback packets */
186 return 0;
187 }
188 }
189 return 1;
190 }
191
192 /**
193 * Forwards an IP packet. It finds an appropriate route for the
194 * packet, decrements the TTL value of the packet, adjusts the
195 * checksum and outputs the packet on the appropriate interface.
196 *
197 * @param p the packet to forward (p->payload points to IP header)
198 * @param iphdr the IP header of the input packet
199 * @param inp the netif on which this packet was received
200 */
201 static void
ip_forward(struct pbuf * p,struct ip_hdr * iphdr,struct netif * inp)202 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
203 {
204 struct netif *netif;
205
206 PERF_START;
207
208 if (!ip_canforward(p)) {
209 goto return_noroute;
210 }
211
212 /* RFC3927 2.7: do not forward link-local addresses */
213 if (ip_addr_islinklocal(¤t_iphdr_dest)) {
214 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
215 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
216 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
217 goto return_noroute;
218 }
219
220 /* Find network interface where to forward this IP packet to. */
221 netif = ip_route(¤t_iphdr_dest);
222 if (netif == NULL) {
223 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
224 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
225 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
226 /* @todo: send ICMP_DUR_NET? */
227 goto return_noroute;
228 }
229 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
230 /* Do not forward packets onto the same network interface on which
231 * they arrived. */
232 if (netif == inp) {
233 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
234 goto return_noroute;
235 }
236 #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
237
238 /* decrement TTL */
239 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
240 /* send ICMP if TTL == 0 */
241 if (IPH_TTL(iphdr) == 0) {
242 snmp_inc_ipinhdrerrors();
243 #if LWIP_ICMP
244 /* Don't send ICMP messages in response to ICMP messages */
245 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
246 icmp_time_exceeded(p, ICMP_TE_TTL);
247 }
248 #endif /* LWIP_ICMP */
249 return;
250 }
251
252 /* Incrementally update the IP checksum. */
253 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
254 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
255 } else {
256 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
257 }
258
259 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
260 ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
261 ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
262
263 IP_STATS_INC(ip.fw);
264 IP_STATS_INC(ip.xmit);
265 snmp_inc_ipforwdatagrams();
266
267 PERF_STOP("ip_forward");
268 /* don't fragment if interface has mtu set to 0 [loopif] */
269 if (netif->mtu && (p->tot_len > netif->mtu)) {
270 if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
271 #if IP_FRAG
272 ip_frag(p, netif, ip_current_dest_addr());
273 #else /* IP_FRAG */
274 /* @todo: send ICMP Destination Unreacheable code 13 "Communication administratively prohibited"? */
275 #endif /* IP_FRAG */
276 } else {
277 /* send ICMP Destination Unreacheable code 4: "Fragmentation Needed and DF Set" */
278 icmp_dest_unreach(p, ICMP_DUR_FRAG);
279 }
280 return;
281 }
282 /* transmit pbuf on chosen interface */
283 netif->output(netif, p, ¤t_iphdr_dest);
284 return;
285 return_noroute:
286 snmp_inc_ipoutnoroutes();
287 }
288 #endif /* IP_FORWARD */
289
290 /**
291 * This function is called by the network interface device driver when
292 * an IP packet is received. The function does the basic checks of the
293 * IP header such as packet size being at least larger than the header
294 * size etc. If the packet was not destined for us, the packet is
295 * forwarded (using ip_forward). The IP checksum is always checked.
296 *
297 * Finally, the packet is sent to the upper layer protocol input function.
298 *
299 * @param p the received IP packet (p->payload points to IP header)
300 * @param inp the netif on which this packet was received
301 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
302 * processed, but currently always returns ERR_OK)
303 */
304 err_t
ip_input(struct pbuf * p,struct netif * inp)305 ip_input(struct pbuf *p, struct netif *inp)
306 {
307 struct ip_hdr *iphdr;
308 struct netif *netif;
309 u16_t iphdr_hlen;
310 u16_t iphdr_len;
311 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
312 int check_ip_src=1;
313 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
314
315 #if IP_NAT
316 extern u8_t ip_nat_input(struct pbuf *p);
317 extern u8_t ip_nat_out(struct pbuf *p);
318 #endif
319
320 IP_STATS_INC(ip.recv);
321 snmp_inc_ipinreceives();
322
323 /* identify the IP header */
324 iphdr = (struct ip_hdr *)p->payload;
325 if (IPH_V(iphdr) != 4) {
326 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
327 ip_debug_print(p);
328 pbuf_free(p);
329 IP_STATS_INC(ip.err);
330 IP_STATS_INC(ip.drop);
331 snmp_inc_ipinhdrerrors();
332 return ERR_OK;
333 }
334
335 #ifdef LWIP_HOOK_IP4_INPUT
336 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
337 /* the packet has been eaten */
338 return ERR_OK;
339 }
340 #endif
341
342 /* obtain IP header length in number of 32-bit words */
343 iphdr_hlen = IPH_HL(iphdr);
344 /* calculate IP header length in bytes */
345 iphdr_hlen *= 4;
346 /* obtain ip length in bytes */
347 iphdr_len = ntohs(IPH_LEN(iphdr));
348
349 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
350 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
351 if (iphdr_hlen > p->len) {
352 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
353 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
354 iphdr_hlen, p->len));
355 }
356 if (iphdr_len > p->tot_len) {
357 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
358 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
359 iphdr_len, p->tot_len));
360 }
361 /* free (drop) packet pbufs */
362 pbuf_free(p);
363 IP_STATS_INC(ip.lenerr);
364 IP_STATS_INC(ip.drop);
365 snmp_inc_ipindiscards();
366 return ERR_OK;
367 }
368
369 /* verify checksum */
370 #if CHECKSUM_CHECK_IP
371 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
372
373 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
374 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
375 ip_debug_print(p);
376 pbuf_free(p);
377 IP_STATS_INC(ip.chkerr);
378 IP_STATS_INC(ip.drop);
379 snmp_inc_ipinhdrerrors();
380 return ERR_OK;
381 }
382 #endif
383
384 /* Trim pbuf. This should have been done at the netif layer,
385 * but we'll do it anyway just to be sure that its done. */
386 pbuf_realloc(p, iphdr_len);
387
388 /* copy IP addresses to aligned ip_addr_t */
389 ip_addr_copy(current_iphdr_dest, iphdr->dest);
390 ip_addr_copy(current_iphdr_src, iphdr->src);
391
392 /* match packet against an interface, i.e. is this packet for us? */
393 #if LWIP_IGMP
394 if (ip_addr_ismulticast(¤t_iphdr_dest)) {
395 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ¤t_iphdr_dest))) {
396 netif = inp;
397 } else {
398 netif = NULL;
399 }
400 } else
401 #endif /* LWIP_IGMP */
402 {
403 /* start trying with inp. if that's not acceptable, start walking the
404 list of configured netifs.
405 'first' is used as a boolean to mark whether we started walking the list */
406 int first = 1;
407 netif = inp;
408 do {
409 LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n",
410 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
411 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
412 ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
413 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
414
415 /* interface is up and configured? */
416 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
417 /* unicast to this interface address? */
418 if (ip_addr_cmp(¤t_iphdr_dest, &(netif->ip_addr)) ||
419 /* or broadcast on this interface network address? */
420 ip_addr_isbroadcast(¤t_iphdr_dest, netif)) {
421 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
422 netif->name[0], netif->name[1]));
423 /* break out of for loop */
424 break;
425 }
426 #if LWIP_AUTOIP
427 /* connections to link-local addresses must persist after changing
428 the netif's address (RFC3927 ch. 1.9) */
429 if ((netif->autoip != NULL) &&
430 ip_addr_cmp(¤t_iphdr_dest, &(netif->autoip->llipaddr))) {
431 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
432 netif->name[0], netif->name[1]));
433 /* break out of for loop */
434 break;
435 }
436 #endif /* LWIP_AUTOIP */
437 }
438 if (first) {
439 first = 0;
440 netif = netif_list;
441 } else {
442 netif = netif->next;
443 }
444 if (netif == inp) {
445 netif = netif->next;
446 }
447 } while(netif != NULL);
448 }
449
450 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
451 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
452 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
453 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
454 *
455 * If you want to accept private broadcast communication while a netif is down,
456 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
457 *
458 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
459 */
460 if (netif == NULL) {
461 /* remote port is DHCP server? */
462 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
463 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
464 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
465 ntohs(udphdr->dest)));
466 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
467 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
468 netif = inp;
469 check_ip_src = 0;
470 }
471 }
472 }
473 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
474
475 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
476 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
477 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
478 if (check_ip_src && !ip_addr_isany(¤t_iphdr_src))
479 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
480 { if ((ip_addr_isbroadcast(¤t_iphdr_src, inp)) ||
481 (ip_addr_ismulticast(¤t_iphdr_src))) {
482 /* packet source is not valid */
483 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
484 /* free (drop) packet pbufs */
485 pbuf_free(p);
486 IP_STATS_INC(ip.drop);
487 snmp_inc_ipinaddrerrors();
488 snmp_inc_ipindiscards();
489 return ERR_OK;
490 }
491 }
492
493 /* packet not for us? */
494 if (netif == NULL) {
495 #if IP_FORWARD || IP_NAT
496 u8_t taken = 0;
497 #endif /* IP_FORWARD || IP_NAT */
498 /* packet not for us, route or discard */
499 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
500 #if IP_FORWARD || IP_NAT
501 /* non-broadcast packet? */
502 if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
503 #if IP_NAT
504 /* check if we want to perform NAT with this packet. */
505 taken = ip_nat_out(p);
506 if (!taken)
507 #endif /* IP_NAT */
508 {
509 #if IP_FORWARD
510 /* try to forward IP packet on (other) interfaces */
511 if (ip_forward(p, iphdr, inp) != NULL) {
512 taken = 1;
513 }
514 #endif /* IP_FORWARD */
515 }
516 }
517 if (!taken)
518 #endif /* IP_FORWARD || IP_NAT */
519 {
520 snmp_inc_ipinaddrerrors();
521 snmp_inc_ipindiscards();
522 }
523 pbuf_free(p);
524 return ERR_OK;
525 }
526 /* packet consists of multiple fragments? */
527 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
528 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
529 LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip_reass()\n",
530 ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
531 /* reassemble the packet*/
532 p = ip_reass(p);
533 /* packet not fully reassembled yet? */
534 if (p == NULL) {
535 return ERR_OK;
536 }
537 iphdr = (struct ip_hdr *)p->payload;
538 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
539 pbuf_free(p);
540 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
541 ntohs(IPH_OFFSET(iphdr))));
542 IP_STATS_INC(ip.opterr);
543 IP_STATS_INC(ip.drop);
544 /* unsupported protocol feature */
545 snmp_inc_ipinunknownprotos();
546 return ERR_OK;
547 #endif /* IP_REASSEMBLY */
548 }
549
550 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
551
552 #if LWIP_IGMP
553 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
554 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
555 #else
556 if (iphdr_hlen > IP_HLEN) {
557 #endif /* LWIP_IGMP */
558 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
559 pbuf_free(p);
560 IP_STATS_INC(ip.opterr);
561 IP_STATS_INC(ip.drop);
562 /* unsupported protocol feature */
563 snmp_inc_ipinunknownprotos();
564 return ERR_OK;
565 }
566 #endif /* IP_OPTIONS_ALLOWED == 0 */
567
568 /* send to upper layers */
569 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
570 ip_debug_print(p);
571 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
572
573 current_netif = inp;
574 current_header = iphdr;
575
576 #if IP_NAT
577 if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
578 (ip_nat_input(p) != 0)) {
579 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet consumed by nat layer\n"));
580 } else
581 #endif /* IP_NAT */
582
583 #if LWIP_RAW
584 /* raw input did not eat the packet? */
585 if (raw_input(p, inp) == 0)
586 #endif /* LWIP_RAW */
587 {
588 switch (IPH_PROTO(iphdr)) {
589 #if LWIP_UDP
590 case IP_PROTO_UDP:
591 #if LWIP_UDPLITE
592 case IP_PROTO_UDPLITE:
593 #endif /* LWIP_UDPLITE */
594 snmp_inc_ipindelivers();
595 udp_input(p, inp);
596 break;
597 #endif /* LWIP_UDP */
598 #if LWIP_TCP
599 case IP_PROTO_TCP:
600 snmp_inc_ipindelivers();
601 tcp_input(p, inp);
602 break;
603 #endif /* LWIP_TCP */
604 #if LWIP_ICMP
605 case IP_PROTO_ICMP:
606 snmp_inc_ipindelivers();
607 icmp_input(p, inp);
608 break;
609 #endif /* LWIP_ICMP */
610 #if LWIP_IGMP
611 case IP_PROTO_IGMP:
612 igmp_input(p, inp, ¤t_iphdr_dest);
613 break;
614 #endif /* LWIP_IGMP */
615 default:
616 #if LWIP_ICMP
617 /* send ICMP destination protocol unreachable unless is was a broadcast */
618 if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp) &&
619 !ip_addr_ismulticast(¤t_iphdr_dest)) {
620 p->payload = iphdr;
621 icmp_dest_unreach(p, ICMP_DUR_PROTO);
622 }
623 #endif /* LWIP_ICMP */
624 pbuf_free(p);
625
626 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
627
628 IP_STATS_INC(ip.proterr);
629 IP_STATS_INC(ip.drop);
630 snmp_inc_ipinunknownprotos();
631 }
632 }
633
634 current_netif = NULL;
635 current_header = NULL;
636 ip_addr_set_any(¤t_iphdr_src);
637 ip_addr_set_any(¤t_iphdr_dest);
638
639 return ERR_OK;
640 }
641
642 /**
643 * Sends an IP packet on a network interface. This function constructs
644 * the IP header and calculates the IP header checksum. If the source
645 * IP address is NULL, the IP address of the outgoing network
646 * interface is filled in as source address.
647 * If the destination IP address is IP_HDRINCL, p is assumed to already
648 * include an IP header and p->payload points to it instead of the data.
649 *
650 * @param p the packet to send (p->payload points to the data, e.g. next
651 protocol header; if dest == IP_HDRINCL, p already includes an IP
652 header and p->payload points to that IP header)
653 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
654 * IP address of the netif used to send is used as source address)
655 * @param dest the destination IP address to send the packet to
656 * @param ttl the TTL value to be set in the IP header
657 * @param tos the TOS value to be set in the IP header
658 * @param proto the PROTOCOL to be set in the IP header
659 * @param netif the netif on which to send this packet
660 * @return ERR_OK if the packet was sent OK
661 * ERR_BUF if p doesn't have enough space for IP/LINK headers
662 * returns errors returned by netif->output
663 *
664 * @note ip_id: RFC791 "some host may be able to simply use
665 * unique identifiers independent of destination"
666 */
667 err_t
668 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
669 u8_t ttl, u8_t tos,
670 u8_t proto, struct netif *netif)
671 {
672 #if IP_OPTIONS_SEND
673 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
674 }
675
676 /**
677 * Same as ip_output_if() but with the possibility to include IP options:
678 *
679 * @ param ip_options pointer to the IP options, copied into the IP header
680 * @ param optlen length of ip_options
681 */
682 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
683 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
684 u16_t optlen)
685 {
686 #endif /* IP_OPTIONS_SEND */
687 struct ip_hdr *iphdr;
688 ip_addr_t dest_addr;
689 #if CHECKSUM_GEN_IP_INLINE
690 u32_t chk_sum = 0;
691 #endif /* CHECKSUM_GEN_IP_INLINE */
692
693 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
694 gets altered as the packet is passed down the stack */
695 LWIP_ASSERT("p->ref == 1", p->ref == 1);
696
697 snmp_inc_ipoutrequests();
698
699 /* Should the IP header be generated or is it already included in p? */
700 if (dest != IP_HDRINCL) {
701 u16_t ip_hlen = IP_HLEN;
702 #if IP_OPTIONS_SEND
703 u16_t optlen_aligned = 0;
704 if (optlen != 0) {
705 #if CHECKSUM_GEN_IP_INLINE
706 int i;
707 #endif /* CHECKSUM_GEN_IP_INLINE */
708 /* round up to a multiple of 4 */
709 optlen_aligned = ((optlen + 3) & ~3);
710 ip_hlen += optlen_aligned;
711 /* First write in the IP options */
712 if (pbuf_header(p, optlen_aligned)) {
713 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
714 IP_STATS_INC(ip.err);
715 snmp_inc_ipoutdiscards();
716 return ERR_BUF;
717 }
718 MEMCPY(p->payload, ip_options, optlen);
719 if (optlen < optlen_aligned) {
720 /* zero the remaining bytes */
721 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
722 }
723 #if CHECKSUM_GEN_IP_INLINE
724 for (i = 0; i < optlen_aligned/2; i++) {
725 chk_sum += ((u16_t*)p->payload)[i];
726 }
727 #endif /* CHECKSUM_GEN_IP_INLINE */
728 }
729 #endif /* IP_OPTIONS_SEND */
730 /* generate IP header */
731 if (pbuf_header(p, IP_HLEN)) {
732 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
733
734 IP_STATS_INC(ip.err);
735 snmp_inc_ipoutdiscards();
736 return ERR_BUF;
737 }
738
739 iphdr = (struct ip_hdr *)p->payload;
740 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
741 (p->len >= sizeof(struct ip_hdr)));
742
743 IPH_TTL_SET(iphdr, ttl);
744 IPH_PROTO_SET(iphdr, proto);
745 #if CHECKSUM_GEN_IP_INLINE
746 chk_sum += LWIP_MAKE_U16(proto, ttl);
747 #endif /* CHECKSUM_GEN_IP_INLINE */
748
749 /* dest cannot be NULL here */
750 ip_addr_copy(iphdr->dest, *dest);
751 #if CHECKSUM_GEN_IP_INLINE
752 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
753 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
754 #endif /* CHECKSUM_GEN_IP_INLINE */
755
756 IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
757 IPH_TOS_SET(iphdr, tos);
758 #if CHECKSUM_GEN_IP_INLINE
759 chk_sum += LWIP_MAKE_U16(tos, iphdr->_v_hl);
760 #endif /* CHECKSUM_GEN_IP_INLINE */
761 IPH_LEN_SET(iphdr, htons(p->tot_len));
762 #if CHECKSUM_GEN_IP_INLINE
763 chk_sum += iphdr->_len;
764 #endif /* CHECKSUM_GEN_IP_INLINE */
765 IPH_OFFSET_SET(iphdr, 0);
766 IPH_ID_SET(iphdr, htons(ip_id));
767 #if CHECKSUM_GEN_IP_INLINE
768 chk_sum += iphdr->_id;
769 #endif /* CHECKSUM_GEN_IP_INLINE */
770 ++ip_id;
771
772 if (ip_addr_isany(src)) {
773 ip_addr_copy(iphdr->src, netif->ip_addr);
774 } else {
775 /* src cannot be NULL here */
776 ip_addr_copy(iphdr->src, *src);
777 }
778
779 #if CHECKSUM_GEN_IP_INLINE
780 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
781 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
782 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
783 chk_sum = (chk_sum >> 16) + chk_sum;
784 chk_sum = ~chk_sum;
785 iphdr->_chksum = chk_sum; /* network order */
786 #else /* CHECKSUM_GEN_IP_INLINE */
787 IPH_CHKSUM_SET(iphdr, 0);
788 #if CHECKSUM_GEN_IP
789 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
790 #endif
791 #endif /* CHECKSUM_GEN_IP_INLINE */
792 } else {
793 /* IP header already included in p */
794 iphdr = (struct ip_hdr *)p->payload;
795 ip_addr_copy(dest_addr, iphdr->dest);
796 dest = &dest_addr;
797 }
798
799 IP_STATS_INC(ip.xmit);
800
801 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
802 ip_debug_print(p);
803
804 #if ENABLE_LOOPBACK
805 if (ip_addr_cmp(dest, &netif->ip_addr)) {
806 /* Packet to self, enqueue it for loopback */
807 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
808 return netif_loop_output(netif, p, dest);
809 }
810 #if LWIP_IGMP
811 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
812 netif_loop_output(netif, p, dest);
813 }
814 #endif /* LWIP_IGMP */
815 #endif /* ENABLE_LOOPBACK */
816 #if IP_FRAG
817 /* don't fragment if interface has mtu set to 0 [loopif] */
818 if (netif->mtu && (p->tot_len > netif->mtu)) {
819 return ip_frag(p, netif, dest);
820 }
821 #endif /* IP_FRAG */
822
823 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
824 return netif->output(netif, p, dest);
825 }
826
827 /**
828 * Simple interface to ip_output_if. It finds the outgoing network
829 * interface and calls upon ip_output_if to do the actual work.
830 *
831 * @param p the packet to send (p->payload points to the data, e.g. next
832 protocol header; if dest == IP_HDRINCL, p already includes an IP
833 header and p->payload points to that IP header)
834 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
835 * IP address of the netif used to send is used as source address)
836 * @param dest the destination IP address to send the packet to
837 * @param ttl the TTL value to be set in the IP header
838 * @param tos the TOS value to be set in the IP header
839 * @param proto the PROTOCOL to be set in the IP header
840 *
841 * @return ERR_RTE if no route is found
842 * see ip_output_if() for more return values
843 */
844 err_t
845 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
846 u8_t ttl, u8_t tos, u8_t proto)
847 {
848 struct netif *netif;
849
850 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
851 gets altered as the packet is passed down the stack */
852 LWIP_ASSERT("p->ref == 1", p->ref == 1);
853
854 if ((netif = ip_route(dest)) == NULL) {
855 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
856 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
857 IP_STATS_INC(ip.rterr);
858 return ERR_RTE;
859 }
860
861 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
862 }
863
864 #if LWIP_NETIF_HWADDRHINT
865 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
866 * before calling ip_output_if.
867 *
868 * @param p the packet to send (p->payload points to the data, e.g. next
869 protocol header; if dest == IP_HDRINCL, p already includes an IP
870 header and p->payload points to that IP header)
871 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
872 * IP address of the netif used to send is used as source address)
873 * @param dest the destination IP address to send the packet to
874 * @param ttl the TTL value to be set in the IP header
875 * @param tos the TOS value to be set in the IP header
876 * @param proto the PROTOCOL to be set in the IP header
877 * @param addr_hint address hint pointer set to netif->addr_hint before
878 * calling ip_output_if()
879 *
880 * @return ERR_RTE if no route is found
881 * see ip_output_if() for more return values
882 */
883 err_t
884 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
885 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
886 {
887 struct netif *netif;
888 err_t err;
889
890 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
891 gets altered as the packet is passed down the stack */
892 LWIP_ASSERT("p->ref == 1", p->ref == 1);
893
894 if ((netif = ip_route(dest)) == NULL) {
895 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
896 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
897 IP_STATS_INC(ip.rterr);
898 return ERR_RTE;
899 }
900
901 NETIF_SET_HWADDRHINT(netif, addr_hint);
902 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
903 NETIF_SET_HWADDRHINT(netif, NULL);
904
905 return err;
906 }
907 #endif /* LWIP_NETIF_HWADDRHINT*/
908
909 #if IP_DEBUG
910 /* Print an IP header by using LWIP_DEBUGF
911 * @param p an IP packet, p->payload pointing to the IP header
912 */
913 void
914 ip_debug_print(struct pbuf *p)
915 {
916 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
917
918 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
919 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
920 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
921 IPH_V(iphdr),
922 IPH_HL(iphdr),
923 IPH_TOS(iphdr),
924 ntohs(IPH_LEN(iphdr))));
925 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
926 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
927 ntohs(IPH_ID(iphdr)),
928 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
929 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
930 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
931 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
932 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
933 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
934 IPH_TTL(iphdr),
935 IPH_PROTO(iphdr),
936 ntohs(IPH_CHKSUM(iphdr))));
937 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
938 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
939 ip4_addr1_16(&iphdr->src),
940 ip4_addr2_16(&iphdr->src),
941 ip4_addr3_16(&iphdr->src),
942 ip4_addr4_16(&iphdr->src)));
943 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
944 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
945 ip4_addr1_16(&iphdr->dest),
946 ip4_addr2_16(&iphdr->dest),
947 ip4_addr3_16(&iphdr->dest),
948 ip4_addr4_16(&iphdr->dest)));
949 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
950 }
951 #endif /* IP_DEBUG */
952