3 * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
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.
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
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet.h"
47 #include "lwip/inet_chksum.h"
48 #include "lwip/netif.h"
49 #include "lwip/icmp.h"
50 #include "lwip/igmp.h"
54 #include "lwip/snmp.h"
55 #include "lwip/dhcp.h"
56 #include "lwip/stats.h"
57 #include "arch/perf.h"
60 * Finds the appropriate network interface for a given IP address. It
61 * searches the list of network interfaces linearly. A match is found
62 * if the masked IP address of the network interface equals the masked
63 * IP address given to the function.
65 * @param dest the destination IP address for which to find the route
66 * @return the netif on which to send to reach dest
69 ip_route(struct ip_addr *dest)
73 /* iterate through netifs */
74 for(netif = netif_list; netif != NULL; netif = netif->next) {
75 /* network mask matches? */
76 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
77 /* return netif on which to forward IP packet */
81 if (netif_default == NULL) {
82 LWIP_DEBUGF(IP_DEBUG | 2, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));
83 IP_STATS_INC(ip.rterr);
84 snmp_inc_ipoutnoroutes();
86 /* no matching netif found, use default netif */
92 * Forwards an IP packet. It finds an appropriate route for the
93 * packet, decrements the TTL value of the packet, adjusts the
94 * checksum and outputs the packet on the appropriate interface.
96 * @param p the packet to forward (p->payload points to IP header)
97 * @param iphdr the IP header of the input packet
98 * @param inp the netif on which this packet was received
99 * @return the netif on which the packet was sent (NULL if it wasn't sent)
101 static struct netif *
102 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
107 /* Find network interface where to forward this IP packet to. */
108 netif = ip_route((struct ip_addr *)&(iphdr->dest));
110 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
112 snmp_inc_ipoutnoroutes();
113 return (struct netif *)NULL;
115 /* Do not forward packets onto the same network interface on which
118 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
119 snmp_inc_ipoutnoroutes();
120 return (struct netif *)NULL;
124 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
125 /* send ICMP if TTL == 0 */
126 if (IPH_TTL(iphdr) == 0) {
127 snmp_inc_ipinhdrerrors();
129 /* Don't send ICMP messages in response to ICMP messages */
130 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
131 icmp_time_exceeded(p, ICMP_TE_TTL);
133 #endif /* LWIP_ICMP */
134 return (struct netif *)NULL;
137 /* Incrementally update the IP checksum. */
138 if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
139 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
141 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
144 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
148 IP_STATS_INC(ip.xmit);
149 snmp_inc_ipforwdatagrams();
151 PERF_STOP("ip_forward");
152 /* transmit pbuf on chosen interface */
153 netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
156 #endif /* IP_FORWARD */
159 * This function is called by the network interface device driver when
160 * an IP packet is received. The function does the basic checks of the
161 * IP header such as packet size being at least larger than the header
162 * size etc. If the packet was not destined for us, the packet is
163 * forwarded (using ip_forward). The IP checksum is always checked.
165 * Finally, the packet is sent to the upper layer protocol input function.
167 * @param p the received IP packet (p->payload points to IP header)
168 * @param inp the netif on which this packet was received
169 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
170 * processed, but currently always returns ERR_OK)
173 ip_input(struct pbuf *p, struct netif *inp) {
174 struct ip_hdr *iphdr;
178 IP_STATS_INC(ip.recv);
179 snmp_inc_ipinreceives();
181 /* identify the IP header */
183 if (IPH_V(iphdr) != 4) {
184 LWIP_DEBUGF(IP_DEBUG | 1, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
187 IP_STATS_INC(ip.err);
188 IP_STATS_INC(ip.drop);
189 snmp_inc_ipinhdrerrors();
192 /* obtain IP header length in number of 32-bit words */
193 iphdrlen = IPH_HL(iphdr);
194 /* calculate IP header length in bytes */
197 /* header length exceeds first pbuf length? */
198 if (iphdrlen > p->len) {
199 LWIP_DEBUGF(IP_DEBUG | 2, ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet droppped.\n",
201 /* free (drop) packet pbufs */
203 IP_STATS_INC(ip.lenerr);
204 IP_STATS_INC(ip.drop);
205 snmp_inc_ipindiscards();
209 /* verify checksum */
210 #if CHECKSUM_CHECK_IP
211 if (inet_chksum(iphdr, iphdrlen) != 0) {
213 LWIP_DEBUGF(IP_DEBUG | 2, ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdrlen)));
216 IP_STATS_INC(ip.chkerr);
217 IP_STATS_INC(ip.drop);
218 snmp_inc_ipinhdrerrors();
223 /* Trim pbuf. This should have been done at the netif layer,
224 * but we'll do it anyway just to be sure that its done. */
225 pbuf_realloc(p, ntohs(IPH_LEN(iphdr)));
227 /* match packet against an interface, i.e. is this packet for us? */
229 if (ip_addr_ismulticast(&(iphdr->dest)))
230 { if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group( inp, &(iphdr->dest))))
239 #endif /* LWIP_IGMP */
240 for (netif = netif_list; netif != NULL; netif = netif->next) {
242 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",
243 iphdr->dest.addr, netif->ip_addr.addr,
244 iphdr->dest.addr & netif->netmask.addr,
245 netif->ip_addr.addr & netif->netmask.addr,
246 iphdr->dest.addr & ~(netif->netmask.addr)));
248 /* interface is up and configured? */
249 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr))))
251 /* unicast to this interface address? */
252 if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
253 /* or broadcast on this interface network address? */
254 ip_addr_isbroadcast(&(iphdr->dest), netif)) {
255 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
256 netif->name[0], netif->name[1]));
257 /* break out of for loop */
264 #endif /* LWIP_IGMP */
267 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
268 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
269 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
272 /* remote port is DHCP server? */
273 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
274 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | 1, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
275 ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdrlen))->dest)));
276 if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdrlen))->dest) == DHCP_CLIENT_PORT) {
277 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | 1, ("ip_input: DHCP packet accepted.\n"));
282 #endif /* LWIP_DHCP */
283 /* packet not for us? */
285 /* packet not for us, route or discard */
286 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | 1, ("ip_input: packet not for us.\n"));
288 /* non-broadcast packet? */
289 if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
290 /* try to forward IP packet on (other) interfaces */
291 ip_forward(p, iphdr, inp);
294 #endif /* IP_FORWARD */
296 snmp_inc_ipinaddrerrors();
297 snmp_inc_ipindiscards();
302 /* packet consists of multiple fragments? */
303 if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
304 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
305 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",
306 ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
307 /* reassemble the packet*/
309 /* packet not fully reassembled yet? */
314 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
316 LWIP_DEBUGF(IP_DEBUG | 2, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
317 ntohs(IPH_OFFSET(iphdr))));
318 IP_STATS_INC(ip.opterr);
319 IP_STATS_INC(ip.drop);
320 /* unsupported protocol feature */
321 snmp_inc_ipinunknownprotos();
323 #endif /* IP_REASSEMBLY */
326 #if IP_OPTIONS == 0 /* no support for IP options in the IP header? */
329 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
330 if((iphdrlen > IP_HLEN && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
332 if (iphdrlen > IP_HLEN) {
333 #endif /* LWIP_IGMP */
334 LWIP_DEBUGF(IP_DEBUG | 2, ("IP packet dropped since there were IP options (while IP_OPTIONS == 0).\n"));
336 IP_STATS_INC(ip.opterr);
337 IP_STATS_INC(ip.drop);
338 /* unsupported protocol feature */
339 snmp_inc_ipinunknownprotos();
342 #endif /* IP_OPTIONS == 0 */
344 /* send to upper layers */
345 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
347 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
350 /* raw input did not eat the packet? */
351 if (raw_input(p, inp) == 0) {
352 #endif /* LWIP_RAW */
354 switch (IPH_PROTO(iphdr)) {
358 case IP_PROTO_UDPLITE:
359 #endif /* LWIP_UDPLITE */
360 snmp_inc_ipindelivers();
363 #endif /* LWIP_UDP */
366 snmp_inc_ipindelivers();
369 #endif /* LWIP_TCP */
372 snmp_inc_ipindelivers();
375 #endif /* LWIP_ICMP */
378 igmp_input(p,inp,&(iphdr->dest));
380 #endif /* LWIP_IGMP */
383 /* send ICMP destination protocol unreachable unless is was a broadcast */
384 if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
385 !ip_addr_ismulticast(&(iphdr->dest))) {
387 icmp_dest_unreach(p, ICMP_DUR_PROTO);
389 #endif /* LWIP_ICMP */
392 LWIP_DEBUGF(IP_DEBUG | 2, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
394 IP_STATS_INC(ip.proterr);
395 IP_STATS_INC(ip.drop);
396 snmp_inc_ipinunknownprotos();
405 * Sends an IP packet on a network interface. This function constructs
406 * the IP header and calculates the IP header checksum. If the source
407 * IP address is NULL, the IP address of the outgoing network
408 * interface is filled in as source address.
409 * If the destination IP address is IP_HDRINCL, p is assumed to already
410 * include an IP header and p->payload points to it instead of the data.
412 * @param p the packet to send (p->payload points to the data, e.g. next
413 protocol header; if dest == IP_HDRINCL, p already includes an IP
414 header and p->payload points to that IP header)
415 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
416 * IP address of the netif used to send is used as source address)
417 * @param dest the destination IP address to send the packet to
418 * @param ttl the TTL value to be set in the IP header
419 * @param tos the TOS value to be set in the IP header
420 * @param proto the PROTOCOL to be set in the IP header
421 * @param netif the netif on which to send this packet
422 * @return ERR_OK if the packet was sent OK
423 * ERR_BUF if p doesn't have enough space for IP/LINK headers
424 * returns errors returned by netif->output
426 * @note ip_id: RFC791 "some host may be able to simply use
427 * unique identifiers independent of destination"
430 ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
432 u8_t proto, struct netif *netif)
434 struct ip_hdr *iphdr;
435 static u16_t ip_id = 0;
437 snmp_inc_ipoutrequests();
439 /* Should the IP header be generated or is it already included in p? */
440 if (dest != IP_HDRINCL) {
441 /* generate IP header */
442 if (pbuf_header(p, IP_HLEN)) {
443 LWIP_DEBUGF(IP_DEBUG | 2, ("ip_output: not enough room for IP header in pbuf\n"));
445 IP_STATS_INC(ip.err);
446 snmp_inc_ipoutdiscards();
451 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
452 (p->len >= sizeof(struct ip_hdr)));
454 IPH_TTL_SET(iphdr, ttl);
455 IPH_PROTO_SET(iphdr, proto);
457 ip_addr_set(&(iphdr->dest), dest);
459 IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, tos);
460 IPH_LEN_SET(iphdr, htons(p->tot_len));
461 IPH_OFFSET_SET(iphdr, 0);
462 IPH_ID_SET(iphdr, htons(ip_id));
465 if (ip_addr_isany(src)) {
466 ip_addr_set(&(iphdr->src), &(netif->ip_addr));
468 ip_addr_set(&(iphdr->src), src);
471 IPH_CHKSUM_SET(iphdr, 0);
473 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
476 /* IP header already included in p */
478 dest = &(iphdr->dest);
482 /* don't fragment if interface has mtu set to 0 [loopif] */
483 if (netif->mtu && (p->tot_len > netif->mtu))
484 return ip_frag(p,netif,dest);
487 IP_STATS_INC(ip.xmit);
489 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
492 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
494 return netif->output(netif, p, dest);
498 * Simple interface to ip_output_if. It finds the outgoing network
499 * interface and calls upon ip_output_if to do the actual work.
501 * @param p the packet to send (p->payload points to the data, e.g. next
502 protocol header; if dest == IP_HDRINCL, p already includes an IP
503 header and p->payload points to that IP header)
504 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
505 * IP address of the netif used to send is used as source address)
506 * @param dest the destination IP address to send the packet to
507 * @param ttl the TTL value to be set in the IP header
508 * @param tos the TOS value to be set in the IP header
509 * @param proto the PROTOCOL to be set in the IP header
511 * @return ERR_RTE if no route is found
512 * see ip_output_if() for more return values
515 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
516 u8_t ttl, u8_t tos, u8_t proto)
520 if ((netif = ip_route(dest)) == NULL) {
524 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
528 /* Print an IP header by using LWIP_DEBUGF
529 * @param p an IP packet, p->payload pointing to the IP header
532 ip_debug_print(struct pbuf *p)
534 struct ip_hdr *iphdr = p->payload;
537 payload = (u8_t *)iphdr + IP_HLEN;
539 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
540 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
541 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
545 ntohs(IPH_LEN(iphdr))));
546 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
547 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
548 ntohs(IPH_ID(iphdr)),
549 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
550 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
551 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
552 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
553 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
554 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
557 ntohs(IPH_CHKSUM(iphdr))));
558 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
559 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
560 ip4_addr1(&iphdr->src),
561 ip4_addr2(&iphdr->src),
562 ip4_addr3(&iphdr->src),
563 ip4_addr4(&iphdr->src)));
564 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
565 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
566 ip4_addr1(&iphdr->dest),
567 ip4_addr2(&iphdr->dest),
568 ip4_addr3(&iphdr->dest),
569 ip4_addr4(&iphdr->dest)));
570 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
572 #endif /* IP_DEBUG */