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 (netif_is_up(netif
)) {
77 if (ip_addr_netcmp(dest
, &(netif
->ip_addr
), &(netif
->netmask
))) {
78 /* return netif on which to forward IP packet */
83 if ((netif_default
== NULL
) || (!netif_is_up(netif_default
))) {
84 LWIP_DEBUGF(IP_DEBUG
| 2, ("ip_route: No route to 0x%"X32_F
"\n", dest
->addr
));
85 IP_STATS_INC(ip
.rterr
);
86 snmp_inc_ipoutnoroutes();
89 /* no matching netif found, use default netif */
95 * Forwards an IP packet. It finds an appropriate route for the
96 * packet, decrements the TTL value of the packet, adjusts the
97 * checksum and outputs the packet on the appropriate interface.
99 * @param p the packet to forward (p->payload points to IP header)
100 * @param iphdr the IP header of the input packet
101 * @param inp the netif on which this packet was received
102 * @return the netif on which the packet was sent (NULL if it wasn't sent)
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_ipoutnoroutes();
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_ipoutnoroutes();
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 snmp_inc_ipinhdrerrors();
132 /* Don't send ICMP messages in response to ICMP messages */
133 if (IPH_PROTO(iphdr
) != IP_PROTO_ICMP
) {
134 icmp_time_exceeded(p
, ICMP_TE_TTL
);
136 #endif /* LWIP_ICMP */
137 return (struct netif
*)NULL
;
140 /* Incrementally update the IP checksum. */
141 if (IPH_CHKSUM(iphdr
) >= htons(0xffff - 0x100)) {
142 IPH_CHKSUM_SET(iphdr
, IPH_CHKSUM(iphdr
) + htons(0x100) + 1);
144 IPH_CHKSUM_SET(iphdr
, IPH_CHKSUM(iphdr
) + htons(0x100));
147 LWIP_DEBUGF(IP_DEBUG
, ("ip_forward: forwarding packet to 0x%"X32_F
"\n",
151 IP_STATS_INC(ip
.xmit
);
152 snmp_inc_ipforwdatagrams();
154 PERF_STOP("ip_forward");
155 /* transmit pbuf on chosen interface */
156 netif
->output(netif
, p
, (struct ip_addr
*)&(iphdr
->dest
));
159 #endif /* IP_FORWARD */
162 * This function is called by the network interface device driver when
163 * an IP packet is received. The function does the basic checks of the
164 * IP header such as packet size being at least larger than the header
165 * size etc. If the packet was not destined for us, the packet is
166 * forwarded (using ip_forward). The IP checksum is always checked.
168 * Finally, the packet is sent to the upper layer protocol input function.
170 * @param p the received IP packet (p->payload points to IP header)
171 * @param inp the netif on which this packet was received
172 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
173 * processed, but currently always returns ERR_OK)
176 ip_input(struct pbuf
*p
, struct netif
*inp
)
178 struct ip_hdr
*iphdr
;
184 #endif /* LWIP_DHCP */
186 IP_STATS_INC(ip
.recv
);
187 snmp_inc_ipinreceives();
189 /* identify the IP header */
191 if (IPH_V(iphdr
) != 4) {
192 LWIP_DEBUGF(IP_DEBUG
| 1, ("IP packet dropped due to bad version number %"U16_F
"\n", IPH_V(iphdr
)));
195 IP_STATS_INC(ip
.err
);
196 IP_STATS_INC(ip
.drop
);
197 snmp_inc_ipinhdrerrors();
201 /* obtain IP header length in number of 32-bit words */
202 iphdr_hlen
= IPH_HL(iphdr
);
203 /* calculate IP header length in bytes */
205 /* obtain ip length in bytes */
206 iphdr_len
= ntohs(IPH_LEN(iphdr
));
208 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
209 if ((iphdr_hlen
> p
->len
) || (iphdr_len
> p
->tot_len
)) {
210 if (iphdr_hlen
> p
->len
)
211 LWIP_DEBUGF(IP_DEBUG
| 2, ("IP header (len %"U16_F
") does not fit in first pbuf (len %"U16_F
"), IP packet dropped.\n",
212 iphdr_hlen
, p
->len
));
213 if (iphdr_len
> p
->tot_len
)
214 LWIP_DEBUGF(IP_DEBUG
| 2, ("IP (len %"U16_F
") is longer than pbuf (len %"U16_F
"), "
215 "IP packet dropped.\n",
216 iphdr_len
, p
->tot_len
));
217 /* free (drop) packet pbufs */
219 IP_STATS_INC(ip
.lenerr
);
220 IP_STATS_INC(ip
.drop
);
221 snmp_inc_ipindiscards();
225 /* verify checksum */
226 #if CHECKSUM_CHECK_IP
227 if (inet_chksum(iphdr
, iphdr_hlen
) != 0) {
229 LWIP_DEBUGF(IP_DEBUG
| 2, ("Checksum (0x%"X16_F
") failed, IP packet dropped.\n", inet_chksum(iphdr
, iphdr_hlen
)));
232 IP_STATS_INC(ip
.chkerr
);
233 IP_STATS_INC(ip
.drop
);
234 snmp_inc_ipinhdrerrors();
239 /* Trim pbuf. This should have been done at the netif layer,
240 * but we'll do it anyway just to be sure that its done. */
241 pbuf_realloc(p
, iphdr_len
);
243 /* match packet against an interface, i.e. is this packet for us? */
245 if (ip_addr_ismulticast(&(iphdr
->dest
))) {
246 if ((inp
->flags
& NETIF_FLAG_IGMP
) && (igmp_lookfor_group(inp
, &(iphdr
->dest
)))) {
252 #endif /* LWIP_IGMP */
254 /* start trying with inp. if that's not acceptable, start walking the
255 list of configured netifs.
256 'first' is used as a boolean to mark whether we started walking the list */
260 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",
261 iphdr
->dest
.addr
, netif
->ip_addr
.addr
,
262 iphdr
->dest
.addr
& netif
->netmask
.addr
,
263 netif
->ip_addr
.addr
& netif
->netmask
.addr
,
264 iphdr
->dest
.addr
& ~(netif
->netmask
.addr
)));
266 /* interface is up and configured? */
267 if ((netif_is_up(netif
)) && (!ip_addr_isany(&(netif
->ip_addr
)))) {
268 /* unicast to this interface address? */
269 if (ip_addr_cmp(&(iphdr
->dest
), &(netif
->ip_addr
)) ||
270 /* or broadcast on this interface network address? */
271 ip_addr_isbroadcast(&(iphdr
->dest
), netif
)) {
272 LWIP_DEBUGF(IP_DEBUG
, ("ip_input: packet accepted on interface %c%c\n",
273 netif
->name
[0], netif
->name
[1]));
274 /* break out of for loop */
287 } while(netif
!= NULL
);
291 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
292 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
293 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
296 /* remote port is DHCP server? */
297 if (IPH_PROTO(iphdr
) == IP_PROTO_UDP
) {
298 LWIP_DEBUGF(IP_DEBUG
| LWIP_DBG_TRACE
| 1, ("ip_input: UDP packet to DHCP client port %"U16_F
"\n",
299 ntohs(((struct udp_hdr
*)((u8_t
*)iphdr
+ iphdr_hlen
))->dest
)));
300 if (ntohs(((struct udp_hdr
*)((u8_t
*)iphdr
+ iphdr_hlen
))->dest
) == DHCP_CLIENT_PORT
) {
301 LWIP_DEBUGF(IP_DEBUG
| LWIP_DBG_TRACE
| 1, ("ip_input: DHCP packet accepted.\n"));
307 #endif /* LWIP_DHCP */
309 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
312 #endif /* LWIP_DHCP */
313 { if ((ip_addr_isbroadcast(&(iphdr
->src
), inp
)) ||
314 (ip_addr_ismulticast(&(iphdr
->src
)))) {
315 /* packet source is not valid */
316 LWIP_DEBUGF(IP_DEBUG
| LWIP_DBG_TRACE
| 1, ("ip_input: packet source is not valid.\n"));
317 /* free (drop) packet pbufs */
319 IP_STATS_INC(ip
.drop
);
320 snmp_inc_ipinaddrerrors();
321 snmp_inc_ipindiscards();
326 /* packet not for us? */
328 /* packet not for us, route or discard */
329 LWIP_DEBUGF(IP_DEBUG
| LWIP_DBG_TRACE
| 1, ("ip_input: packet not for us.\n"));
331 /* non-broadcast packet? */
332 if (!ip_addr_isbroadcast(&(iphdr
->dest
), inp
)) {
333 /* try to forward IP packet on (other) interfaces */
334 ip_forward(p
, iphdr
, inp
);
336 #endif /* IP_FORWARD */
338 snmp_inc_ipinaddrerrors();
339 snmp_inc_ipindiscards();
344 /* packet consists of multiple fragments? */
345 if ((IPH_OFFSET(iphdr
) & htons(IP_OFFMASK
| IP_MF
)) != 0) {
346 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
347 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",
348 ntohs(IPH_ID(iphdr
)), p
->tot_len
, ntohs(IPH_LEN(iphdr
)), !!(IPH_OFFSET(iphdr
) & htons(IP_MF
)), (ntohs(IPH_OFFSET(iphdr
)) & IP_OFFMASK
)*8));
349 /* reassemble the packet*/
351 /* packet not fully reassembled yet? */
356 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
358 LWIP_DEBUGF(IP_DEBUG
| 2, ("IP packet dropped since it was fragmented (0x%"X16_F
") (while IP_REASSEMBLY == 0).\n",
359 ntohs(IPH_OFFSET(iphdr
))));
360 IP_STATS_INC(ip
.opterr
);
361 IP_STATS_INC(ip
.drop
);
362 /* unsupported protocol feature */
363 snmp_inc_ipinunknownprotos();
365 #endif /* IP_REASSEMBLY */
368 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
371 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
372 if((iphdr_hlen
> IP_HLEN
&& (IPH_PROTO(iphdr
) != IP_PROTO_IGMP
)) {
374 if (iphdr_hlen
> IP_HLEN
) {
375 #endif /* LWIP_IGMP */
376 LWIP_DEBUGF(IP_DEBUG
| 2, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
378 IP_STATS_INC(ip
.opterr
);
379 IP_STATS_INC(ip
.drop
);
380 /* unsupported protocol feature */
381 snmp_inc_ipinunknownprotos();
384 #endif /* IP_OPTIONS_ALLOWED == 0 */
386 /* send to upper layers */
387 LWIP_DEBUGF(IP_DEBUG
, ("ip_input: \n"));
389 LWIP_DEBUGF(IP_DEBUG
, ("ip_input: p->len %"U16_F
" p->tot_len %"U16_F
"\n", p
->len
, p
->tot_len
));
392 /* raw input did not eat the packet? */
393 if (raw_input(p
, inp
) == 0)
394 #endif /* LWIP_RAW */
397 switch (IPH_PROTO(iphdr
)) {
401 case IP_PROTO_UDPLITE
:
402 #endif /* LWIP_UDPLITE */
403 snmp_inc_ipindelivers();
406 #endif /* LWIP_UDP */
409 snmp_inc_ipindelivers();
412 #endif /* LWIP_TCP */
415 snmp_inc_ipindelivers();
418 #endif /* LWIP_ICMP */
421 igmp_input(p
,inp
,&(iphdr
->dest
));
423 #endif /* LWIP_IGMP */
426 /* send ICMP destination protocol unreachable unless is was a broadcast */
427 if (!ip_addr_isbroadcast(&(iphdr
->dest
), inp
) &&
428 !ip_addr_ismulticast(&(iphdr
->dest
))) {
430 icmp_dest_unreach(p
, ICMP_DUR_PROTO
);
432 #endif /* LWIP_ICMP */
435 LWIP_DEBUGF(IP_DEBUG
| 2, ("Unsupported transport protocol %"U16_F
"\n", IPH_PROTO(iphdr
)));
437 IP_STATS_INC(ip
.proterr
);
438 IP_STATS_INC(ip
.drop
);
439 snmp_inc_ipinunknownprotos();
447 * Sends an IP packet on a network interface. This function constructs
448 * the IP header and calculates the IP header checksum. If the source
449 * IP address is NULL, the IP address of the outgoing network
450 * interface is filled in as source address.
451 * If the destination IP address is IP_HDRINCL, p is assumed to already
452 * include an IP header and p->payload points to it instead of the data.
454 * @param p the packet to send (p->payload points to the data, e.g. next
455 protocol header; if dest == IP_HDRINCL, p already includes an IP
456 header and p->payload points to that IP header)
457 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
458 * IP address of the netif used to send is used as source address)
459 * @param dest the destination IP address to send the packet to
460 * @param ttl the TTL value to be set in the IP header
461 * @param tos the TOS value to be set in the IP header
462 * @param proto the PROTOCOL to be set in the IP header
463 * @param netif the netif on which to send this packet
464 * @return ERR_OK if the packet was sent OK
465 * ERR_BUF if p doesn't have enough space for IP/LINK headers
466 * returns errors returned by netif->output
468 * @note ip_id: RFC791 "some host may be able to simply use
469 * unique identifiers independent of destination"
472 ip_output_if(struct pbuf
*p
, struct ip_addr
*src
, struct ip_addr
*dest
,
474 u8_t proto
, struct netif
*netif
)
476 struct ip_hdr
*iphdr
;
477 static u16_t ip_id
= 0;
479 snmp_inc_ipoutrequests();
481 /* Should the IP header be generated or is it already included in p? */
482 if (dest
!= IP_HDRINCL
) {
483 /* generate IP header */
484 if (pbuf_header(p
, IP_HLEN
)) {
485 LWIP_DEBUGF(IP_DEBUG
| 2, ("ip_output: not enough room for IP header in pbuf\n"));
487 IP_STATS_INC(ip
.err
);
488 snmp_inc_ipoutdiscards();
493 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
494 (p
->len
>= sizeof(struct ip_hdr
)));
496 IPH_TTL_SET(iphdr
, ttl
);
497 IPH_PROTO_SET(iphdr
, proto
);
499 ip_addr_set(&(iphdr
->dest
), dest
);
501 IPH_VHLTOS_SET(iphdr
, 4, IP_HLEN
/ 4, tos
);
502 IPH_LEN_SET(iphdr
, htons(p
->tot_len
));
503 IPH_OFFSET_SET(iphdr
, 0);
504 IPH_ID_SET(iphdr
, htons(ip_id
));
507 if (ip_addr_isany(src
)) {
508 ip_addr_set(&(iphdr
->src
), &(netif
->ip_addr
));
510 ip_addr_set(&(iphdr
->src
), src
);
513 IPH_CHKSUM_SET(iphdr
, 0);
515 IPH_CHKSUM_SET(iphdr
, inet_chksum(iphdr
, IP_HLEN
));
518 /* IP header already included in p */
520 dest
= &(iphdr
->dest
);
524 /* don't fragment if interface has mtu set to 0 [loopif] */
525 if (netif
->mtu
&& (p
->tot_len
> netif
->mtu
))
526 return ip_frag(p
,netif
,dest
);
529 IP_STATS_INC(ip
.xmit
);
531 LWIP_DEBUGF(IP_DEBUG
, ("ip_output_if: %c%c%"U16_F
"\n", netif
->name
[0], netif
->name
[1], netif
->num
));
534 LWIP_DEBUGF(IP_DEBUG
, ("netif->output()"));
536 return netif
->output(netif
, p
, dest
);
540 * Simple interface to ip_output_if. It finds the outgoing network
541 * interface and calls upon ip_output_if to do the actual work.
543 * @param p the packet to send (p->payload points to the data, e.g. next
544 protocol header; if dest == IP_HDRINCL, p already includes an IP
545 header and p->payload points to that IP header)
546 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
547 * IP address of the netif used to send is used as source address)
548 * @param dest the destination IP address to send the packet to
549 * @param ttl the TTL value to be set in the IP header
550 * @param tos the TOS value to be set in the IP header
551 * @param proto the PROTOCOL to be set in the IP header
553 * @return ERR_RTE if no route is found
554 * see ip_output_if() for more return values
557 ip_output(struct pbuf
*p
, struct ip_addr
*src
, struct ip_addr
*dest
,
558 u8_t ttl
, u8_t tos
, u8_t proto
)
562 if ((netif
= ip_route(dest
)) == NULL
) {
566 return ip_output_if(p
, src
, dest
, ttl
, tos
, proto
, netif
);
570 /* Print an IP header by using LWIP_DEBUGF
571 * @param p an IP packet, p->payload pointing to the IP header
574 ip_debug_print(struct pbuf
*p
)
576 struct ip_hdr
*iphdr
= p
->payload
;
579 payload
= (u8_t
*)iphdr
+ IP_HLEN
;
581 LWIP_DEBUGF(IP_DEBUG
, ("IP header:\n"));
582 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
583 LWIP_DEBUGF(IP_DEBUG
, ("|%2"S16_F
" |%2"S16_F
" | 0x%02"X16_F
" | %5"U16_F
" | (v, hl, tos, len)\n",
587 ntohs(IPH_LEN(iphdr
))));
588 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
589 LWIP_DEBUGF(IP_DEBUG
, ("| %5"U16_F
" |%"U16_F
"%"U16_F
"%"U16_F
"| %4"U16_F
" | (id, flags, offset)\n",
590 ntohs(IPH_ID(iphdr
)),
591 ntohs(IPH_OFFSET(iphdr
)) >> 15 & 1,
592 ntohs(IPH_OFFSET(iphdr
)) >> 14 & 1,
593 ntohs(IPH_OFFSET(iphdr
)) >> 13 & 1,
594 ntohs(IPH_OFFSET(iphdr
)) & IP_OFFMASK
));
595 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
596 LWIP_DEBUGF(IP_DEBUG
, ("| %3"U16_F
" | %3"U16_F
" | 0x%04"X16_F
" | (ttl, proto, chksum)\n",
599 ntohs(IPH_CHKSUM(iphdr
))));
600 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
601 LWIP_DEBUGF(IP_DEBUG
, ("| %3"U16_F
" | %3"U16_F
" | %3"U16_F
" | %3"U16_F
" | (src)\n",
602 ip4_addr1(&iphdr
->src
),
603 ip4_addr2(&iphdr
->src
),
604 ip4_addr3(&iphdr
->src
),
605 ip4_addr4(&iphdr
->src
)));
606 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
607 LWIP_DEBUGF(IP_DEBUG
, ("| %3"U16_F
" | %3"U16_F
" | %3"U16_F
" | %3"U16_F
" | (dest)\n",
608 ip4_addr1(&iphdr
->dest
),
609 ip4_addr2(&iphdr
->dest
),
610 ip4_addr3(&iphdr
->dest
),
611 ip4_addr4(&iphdr
->dest
)));
612 LWIP_DEBUGF(IP_DEBUG
, ("+-------------------------------+\n"));
614 #endif /* IP_DEBUG */