3 * This is the IP layer implementation for incoming and outgoing IP traffic.
9 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 * This file is part of the lwIP TCP/IP stack.
36 * Author: Adam Dunkels <adam@sics.se>
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet.h"
47 #include "lwip/netif.h"
48 #include "lwip/icmp.h"
53 #include "lwip/stats.h"
55 #include "arch/perf.h"
57 #include "lwip/snmp.h"
59 # include "lwip/dhcp.h"
60 #endif /* LWIP_DHCP */
64 * Initializes the IP layer.
70 /* no initializations as of yet */
74 * Finds the appropriate network interface for a given IP address. It
75 * searches the list of network interfaces linearly. A match is found
76 * if the masked IP address of the network interface equals the masked
77 * IP address given to the function.
81 ip_route(struct ip_addr *dest)
85 /* iterate through netifs */
86 for(netif = netif_list; netif != NULL; netif = netif->next) {
87 /* network mask matches? */
88 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
89 /* return netif on which to forward IP packet */
93 /* no matching netif found, use default netif */
99 * Forwards an IP packet. It finds an appropriate route for the
100 * packet, decrements the TTL value of the packet, adjusts the
101 * checksum and outputs the packet on the appropriate interface.
104 static struct netif *
105 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
110 /* Find network interface where to forward this IP packet to. */
111 netif = ip_route((struct ip_addr *)&(iphdr->dest));
113 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
115 snmp_inc_ipnoroutes();
116 return (struct netif *)NULL;
118 /* Do not forward packets onto the same network interface on which
121 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
122 snmp_inc_ipnoroutes();
123 return (struct netif *)NULL;
127 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
128 /* send ICMP if TTL == 0 */
129 if (IPH_TTL(iphdr) == 0) {
130 /* Don't send ICMP messages in response to ICMP messages */
131 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
132 icmp_time_exceeded(p, ICMP_TE_TTL);
133 snmp_inc_icmpouttimeexcds();
135 return (struct netif *)NULL;
138 /* Incrementally update the IP checksum. */
139 if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
140 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
142 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
145 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
149 IP_STATS_INC(ip.xmit);
150 snmp_inc_ipforwdatagrams();
152 PERF_STOP("ip_forward");
153 /* transmit pbuf on chosen interface */
154 netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
157 #endif /* IP_FORWARD */
160 * This function is called by the network interface device driver when
161 * an IP packet is received. The function does the basic checks of the
162 * IP header such as packet size being at least larger than the header
163 * size etc. If the packet was not destined for us, the packet is
164 * forwarded (using ip_forward). The IP checksum is always checked.
166 * Finally, the packet is sent to the upper layer protocol input function.
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_ipunknownprotos();
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_ipindiscards();
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? */
228 for (netif = netif_list; netif != NULL; netif = netif->next) {
230 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",
231 iphdr->dest.addr, netif->ip_addr.addr,
232 iphdr->dest.addr & netif->netmask.addr,
233 netif->ip_addr.addr & netif->netmask.addr,
234 iphdr->dest.addr & ~(netif->netmask.addr)));
236 /* interface is up and configured? */
237 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr))))
239 /* unicast to this interface address? */
240 if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
241 /* or broadcast on this interface network address? */
242 ip_addr_isbroadcast(&(iphdr->dest), netif)) {
243 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
244 netif->name[0], netif->name[1]));
245 /* break out of for loop */
251 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
252 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
253 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
256 /* remote port is DHCP server? */
257 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
258 LWIP_DEBUGF(IP_DEBUG | DBG_TRACE | 1, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
259 ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdrlen))->dest)));
260 if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdrlen))->dest) == DHCP_CLIENT_PORT) {
261 LWIP_DEBUGF(IP_DEBUG | DBG_TRACE | 1, ("ip_input: DHCP packet accepted.\n"));
266 #endif /* LWIP_DHCP */
267 /* packet not for us? */
269 /* packet not for us, route or discard */
270 LWIP_DEBUGF(IP_DEBUG | DBG_TRACE | 1, ("ip_input: packet not for us.\n"));
272 /* non-broadcast packet? */
273 if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
274 /* try to forward IP packet on (other) interfaces */
275 ip_forward(p, iphdr, inp);
278 #endif /* IP_FORWARD */
280 snmp_inc_ipindiscards();
285 /* packet consists of multiple fragments? */
286 if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
287 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
288 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",
289 ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
290 /* reassemble the packet*/
292 /* packet not fully reassembled yet? */
297 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
299 LWIP_DEBUGF(IP_DEBUG | 2, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
300 ntohs(IPH_OFFSET(iphdr))));
301 IP_STATS_INC(ip.opterr);
302 IP_STATS_INC(ip.drop);
303 snmp_inc_ipunknownprotos();
305 #endif /* IP_REASSEMBLY */
308 #if IP_OPTIONS == 0 /* no support for IP options in the IP header? */
309 if (iphdrlen > IP_HLEN) {
310 LWIP_DEBUGF(IP_DEBUG | 2, ("IP packet dropped since there were IP options (while IP_OPTIONS == 0).\n"));
312 IP_STATS_INC(ip.opterr);
313 IP_STATS_INC(ip.drop);
314 snmp_inc_ipunknownprotos();
317 #endif /* IP_OPTIONS == 0 */
319 /* send to upper layers */
320 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
322 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
325 /* raw input did not eat the packet? */
326 if (raw_input(p, inp) == 0) {
327 #endif /* LWIP_RAW */
329 switch (IPH_PROTO(iphdr)) {
332 case IP_PROTO_UDPLITE:
333 snmp_inc_ipindelivers();
336 #endif /* LWIP_UDP */
339 snmp_inc_ipindelivers();
342 #endif /* LWIP_TCP */
344 snmp_inc_ipindelivers();
348 /* send ICMP destination protocol unreachable unless is was a broadcast */
349 if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
350 !ip_addr_ismulticast(&(iphdr->dest))) {
352 icmp_dest_unreach(p, ICMP_DUR_PROTO);
356 LWIP_DEBUGF(IP_DEBUG | 2, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
358 IP_STATS_INC(ip.proterr);
359 IP_STATS_INC(ip.drop);
360 snmp_inc_ipunknownprotos();
369 * Sends an IP packet on a network interface. This function constructs
370 * the IP header and calculates the IP header checksum. If the source
371 * IP address is NULL, the IP address of the outgoing network
372 * interface is filled in as source address.
376 ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
378 u8_t proto, struct netif *netif)
380 struct ip_hdr *iphdr;
383 snmp_inc_ipoutrequests();
385 if (dest != IP_HDRINCL) {
386 if (pbuf_header(p, IP_HLEN)) {
387 LWIP_DEBUGF(IP_DEBUG | 2, ("ip_output: not enough room for IP header in pbuf\n"));
389 IP_STATS_INC(ip.err);
390 snmp_inc_ipoutdiscards();
396 IPH_TTL_SET(iphdr, ttl);
397 IPH_PROTO_SET(iphdr, proto);
399 ip_addr_set(&(iphdr->dest), dest);
401 IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, tos);
402 IPH_LEN_SET(iphdr, htons(p->tot_len));
403 IPH_OFFSET_SET(iphdr, htons(IP_DF));
404 IPH_ID_SET(iphdr, htons(ip_id));
407 if (ip_addr_isany(src)) {
408 ip_addr_set(&(iphdr->src), &(netif->ip_addr));
410 ip_addr_set(&(iphdr->src), src);
413 IPH_CHKSUM_SET(iphdr, 0);
415 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
419 dest = &(iphdr->dest);
423 /* don't fragment if interface has mtu set to 0 [loopif] */
424 if (netif->mtu && (p->tot_len > netif->mtu))
425 return ip_frag(p,netif,dest);
428 IP_STATS_INC(ip.xmit);
430 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
433 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
435 return netif->output(netif, p, dest);
439 * Simple interface to ip_output_if. It finds the outgoing network
440 * interface and calls upon ip_output_if to do the actual work.
444 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
445 u8_t ttl, u8_t tos, u8_t proto)
449 if ((netif = ip_route(dest)) == NULL) {
450 LWIP_DEBUGF(IP_DEBUG | 2, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
452 IP_STATS_INC(ip.rterr);
453 snmp_inc_ipoutdiscards();
457 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
462 ip_debug_print(struct pbuf *p)
464 struct ip_hdr *iphdr = p->payload;
467 payload = (u8_t *)iphdr + IP_HLEN;
469 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
470 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
471 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
475 ntohs(IPH_LEN(iphdr))));
476 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
477 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
478 ntohs(IPH_ID(iphdr)),
479 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
480 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
481 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
482 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
483 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
484 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
487 ntohs(IPH_CHKSUM(iphdr))));
488 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
489 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
490 ip4_addr1(&iphdr->src),
491 ip4_addr2(&iphdr->src),
492 ip4_addr3(&iphdr->src),
493 ip4_addr4(&iphdr->src)));
494 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
495 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
496 ip4_addr1(&iphdr->dest),
497 ip4_addr2(&iphdr->dest),
498 ip4_addr3(&iphdr->dest),
499 ip4_addr4(&iphdr->dest)));
500 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
502 #endif /* IP_DEBUG */