2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
37 * This is the code for the IP layer for IPv6.
47 #include "lwip/inet.h"
48 #include "lwip/netif.h"
49 #include "lwip/icmp.h"
53 #include "lwip/stats.h"
55 #include "arch/perf.h"
59 * Initializes the IP layer.
69 * Finds the appropriate network interface for a given IP address. It searches the
70 * list of network interfaces linearly. A match is found if the masked IP address of
71 * the network interface equals the masked IP address given to the function.
75 ip_route(struct ip_addr *dest)
79 for(netif = netif_list; netif != NULL; netif = netif->next) {
80 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
90 * Forwards an IP packet. It finds an appropriate route for the packet, decrements
91 * the TTL value of the packet, adjusts the checksum and outputs the packet on the
92 * appropriate interface.
96 ip_forward(struct pbuf *p, struct ip_hdr *iphdr)
102 if ((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
104 LWIP_DEBUGF(IP_DEBUG, ("ip_input: no forwarding route found for "));
106 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
107 #endif /* IP_DEBUG */
108 LWIP_DEBUGF(IP_DEBUG, ("\n"));
112 /* Decrement TTL and send ICMP if ttl == 0. */
113 if (--iphdr->hoplim == 0) {
115 /* Don't send ICMP messages in response to ICMP messages */
116 if (iphdr->nexthdr != IP_PROTO_ICMP) {
117 icmp_time_exceeded(p, ICMP_TE_TTL);
119 #endif /* LWIP_ICMP */
124 /* Incremental update of the IP checksum. */
125 /* if (iphdr->chksum >= htons(0xffff - 0x100)) {
126 iphdr->chksum += htons(0x100) + 1;
128 iphdr->chksum += htons(0x100);
132 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to "));
134 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
135 #endif /* IP_DEBUG */
136 LWIP_DEBUGF(IP_DEBUG, ("\n"));
139 IP_STATS_INC(ip.xmit);
141 PERF_STOP("ip_forward");
143 netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
148 * This function is called by the network interface device driver when an IP packet is
149 * received. The function does the basic checks of the IP header such as packet size
150 * being at least larger than the header size etc. If the packet was not destined for
151 * us, the packet is forwarded (using ip_forward). The IP checksum is always checked.
153 * Finally, the packet is sent to the upper layer protocol input function.
157 ip_input(struct pbuf *p, struct netif *inp) {
158 struct ip_hdr *iphdr;
166 #endif /* IP_DEBUG */
169 IP_STATS_INC(ip.recv);
171 /* identify the IP header */
176 LWIP_DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number\n"));
179 #endif /* IP_DEBUG */
181 IP_STATS_INC(ip.err);
182 IP_STATS_INC(ip.drop);
186 /* is this packet for us? */
187 for(netif = netif_list; netif != NULL; netif = netif->next) {
189 LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest "));
190 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
191 LWIP_DEBUGF(IP_DEBUG, ("netif->ip_addr "));
192 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
193 LWIP_DEBUGF(IP_DEBUG, ("\n"));
194 #endif /* IP_DEBUG */
195 if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))) {
202 /* packet not for us, route or discard */
204 ip_forward(p, iphdr);
210 pbuf_realloc(p, IP_HLEN + ntohs(iphdr->len));
212 /* send to upper layers */
214 /* LWIP_DEBUGF("ip_input: \n");
216 LWIP_DEBUGF("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
217 #endif /* IP_DEBUG */
219 if(pbuf_header(p, -IP_HLEN)) {
220 LWIP_ASSERT("Can't move over header in packet", 0);
224 switch (iphdr->nexthdr) {
235 #endif /* LWIP_ICMP */
238 /* send ICMP destination protocol unreachable */
239 icmp_dest_unreach(p, ICMP_DUR_PROTO);
240 #endif /* LWIP_ICMP */
242 LWIP_DEBUGF(IP_DEBUG, ("Unsupported transport protocol %"U16_F"\n",
245 IP_STATS_INC(ip.proterr);
246 IP_STATS_INC(ip.drop);
248 PERF_STOP("ip_input");
254 * Sends an IP packet on a network interface. This function constructs the IP header
255 * and calculates the IP header checksum. If the source IP address is NULL,
256 * the IP address of the outgoing network interface is filled in as source address.
260 ip_output_if (struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
262 u8_t proto, struct netif *netif)
264 struct ip_hdr *iphdr;
268 LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
269 if (pbuf_header(p, IP_HLEN)) {
270 LWIP_DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
271 IP_STATS_INC(ip.err);
275 LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
280 if (dest != IP_HDRINCL) {
281 LWIP_DEBUGF(IP_DEBUG, ("!IP_HDRLINCL\n"));
283 iphdr->nexthdr = proto;
284 iphdr->len = htons(p->tot_len - IP_HLEN);
285 ip_addr_set(&(iphdr->dest), dest);
289 if (ip_addr_isany(src)) {
290 ip_addr_set(&(iphdr->src), &(netif->ip_addr));
292 ip_addr_set(&(iphdr->src), src);
296 dest = &(iphdr->dest);
299 IP_STATS_INC(ip.xmit);
301 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c (len %"U16_F")\n", netif->name[0], netif->name[1], p->tot_len));
304 #endif /* IP_DEBUG */
306 PERF_STOP("ip_output_if");
307 return netif->output(netif, p, dest);
312 * Simple interface to ip_output_if. It finds the outgoing network interface and
313 * calls upon ip_output_if to do the actual work.
317 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
318 u8_t ttl, u8_t proto)
321 if ((netif = ip_route(dest)) == NULL) {
322 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
323 IP_STATS_INC(ip.rterr);
327 return ip_output_if (p, src, dest, ttl, proto, netif);
332 ip_debug_print(struct pbuf *p)
334 struct ip_hdr *iphdr = p->payload;
336 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
337 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
338 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" | %"X16_F"%"X16_F" | %"X16_F"%"X16_F" | (v, traffic class, flow label)\n",
340 iphdr->tclass1, iphdr->tclass2,
341 iphdr->flow1, iphdr->flow2));
342 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
343 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" | %2"U16_F" | %2"U16_F" | (len, nexthdr, hoplim)\n",
347 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
348 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
349 (ntohl(iphdr->src.addr[0]) >> 16) & 0xffff,
350 ntohl(iphdr->src.addr[0]) & 0xffff));
351 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
352 (ntohl(iphdr->src.addr[1]) >> 16) & 0xffff,
353 ntohl(iphdr->src.addr[1]) & 0xffff));
354 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
355 (ntohl(iphdr->src.addr[2]) >> 16) & 0xffff,
356 ntohl(iphdr->src.addr[2]) & 0xffff));
357 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
358 (ntohl(iphdr->src.addr[3]) >> 16) & 0xffff,
359 ntohl(iphdr->src.addr[3]) & 0xffff));
360 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
361 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
362 (ntohl(iphdr->dest.addr[0]) >> 16) & 0xffff,
363 ntohl(iphdr->dest.addr[0]) & 0xffff));
364 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
365 (ntohl(iphdr->dest.addr[1]) >> 16) & 0xffff,
366 ntohl(iphdr->dest.addr[1]) & 0xffff));
367 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
368 (ntohl(iphdr->dest.addr[2]) >> 16) & 0xffff,
369 ntohl(iphdr->dest.addr[2]) & 0xffff));
370 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
371 (ntohl(iphdr->dest.addr[3]) >> 16) & 0xffff,
372 ntohl(iphdr->dest.addr[3]) & 0xffff));
373 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
375 #endif /* IP_DEBUG */