2 * Copyright (c) 2001, 2002 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>
34 /*-----------------------------------------------------------------------------------*/
37 * This is the code for the IP layer for IPv6.
40 /*-----------------------------------------------------------------------------------*/
42 #include "lwip/debug.h"
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"
56 /*-----------------------------------------------------------------------------------*/
59 * Initializes the IP layer.
61 /*-----------------------------------------------------------------------------------*/
66 /*-----------------------------------------------------------------------------------*/
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.
73 /*-----------------------------------------------------------------------------------*/
75 ip_route(struct ip_addr *dest)
79 for(netif = netif_list; netif != NULL; netif = netif->next) {
80 if(ip_addr_maskcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
87 /*-----------------------------------------------------------------------------------*/
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.
94 /*-----------------------------------------------------------------------------------*/
96 ip_forward(struct pbuf *p, struct ip_hdr *iphdr)
102 if((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
104 DEBUGF(IP_DEBUG, ("ip_input: no forwarding route found for "));
106 ip_addr_debug_print(&(iphdr->dest));
107 #endif /* IP_DEBUG */
108 DEBUGF(IP_DEBUG, ("\n"));
112 /* Decrement TTL and send ICMP if ttl == 0. */
113 if(--iphdr->hoplim == 0) {
114 /* Don't send ICMP messages in response to ICMP messages */
115 if(iphdr->nexthdr != IP_PROTO_ICMP) {
116 icmp_time_exceeded(p, ICMP_TE_TTL);
122 /* Incremental update of the IP checksum. */
123 /* if (iphdr->chksum >= htons(0xffff - 0x100)) {
124 iphdr->chksum += htons(0x100) + 1;
126 iphdr->chksum += htons(0x100);
130 DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to "));
132 ip_addr_debug_print(&(iphdr->dest));
133 #endif /* IP_DEBUG */
134 DEBUGF(IP_DEBUG, ("\n"));
139 #endif /* IP_STATS */
141 PERF_STOP("ip_forward");
143 netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
145 /*-----------------------------------------------------------------------------------*/
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.
155 /*-----------------------------------------------------------------------------------*/
157 ip_input(struct pbuf *p, struct netif *inp) {
158 struct ip_hdr *iphdr;
166 #endif /* IP_DEBUG */
171 #endif /* IP_STATS */
173 /* identify the IP header */
178 DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number\n"));
181 #endif /* IP_DEBUG */
186 #endif /* IP_STATS */
190 /* is this packet for us? */
191 for(netif = netif_list; netif != NULL; netif = netif->next) {
193 DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest "));
194 ip_addr_debug_print(&(iphdr->dest));
195 DEBUGF(IP_DEBUG, ("netif->ip_addr "));
196 ip_addr_debug_print(&(netif->ip_addr));
197 DEBUGF(IP_DEBUG, ("\n"));
198 #endif /* IP_DEBUG */
199 if(ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))) {
206 /* packet not for us, route or discard */
208 ip_forward(p, iphdr);
214 pbuf_realloc(p, IP_HLEN + ntohs(iphdr->len));
216 /* send to upper layers */
218 /* DEBUGF("ip_input: \n");
220 DEBUGF("ip_input: p->len %d p->tot_len %d\n", p->len, p->tot_len);*/
221 #endif /* IP_DEBUG */
224 pbuf_header(p, -IP_HLEN);
226 switch(iphdr->nexthdr) {
237 /* send ICMP destination protocol unreachable */
238 icmp_dest_unreach(p, ICMP_DUR_PROTO);
240 DEBUGF(IP_DEBUG, ("Unsupported transportation protocol %d\n",
246 #endif /* IP_STATS */
249 PERF_STOP("ip_input");
252 /*-----------------------------------------------------------------------------------*/
255 * Sends an IP packet on a network interface. This function constructs the IP header
256 * and calculates the IP header checksum. If the source IP address is NULL,
257 * the IP address of the outgoing network interface is filled in as source address.
259 /*-----------------------------------------------------------------------------------*/
261 ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
263 u8_t proto, struct netif *netif)
265 struct ip_hdr *iphdr;
269 printf("len %d tot_len %d\n", p->len, p->tot_len);
270 if(pbuf_header(p, IP_HLEN)) {
271 DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
274 #endif /* IP_STATS */
278 printf("len %d tot_len %d\n", p->len, p->tot_len);
283 if(dest != IP_HDRINCL) {
284 printf("!IP_HDRLINCL\n");
286 iphdr->nexthdr = proto;
287 iphdr->len = htons(p->tot_len - IP_HLEN);
288 ip_addr_set(&(iphdr->dest), dest);
292 if(ip_addr_isany(src)) {
293 ip_addr_set(&(iphdr->src), &(netif->ip_addr));
295 ip_addr_set(&(iphdr->src), src);
299 dest = &(iphdr->dest);
304 #endif /* IP_STATS */
306 DEBUGF(IP_DEBUG, ("ip_output_if: %c%c (len %d)\n", netif->name[0], netif->name[1], p->tot_len));
309 #endif /* IP_DEBUG */
311 PERF_STOP("ip_output_if");
312 return netif->output(netif, p, dest);
314 /*-----------------------------------------------------------------------------------*/
317 * Simple interface to ip_output_if. It finds the outgoing network interface and
318 * calls upon ip_output_if to do the actual work.
320 /*-----------------------------------------------------------------------------------*/
322 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
323 u8_t ttl, u8_t proto)
326 if((netif = ip_route(dest)) == NULL) {
327 DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%lx\n", dest->addr));
330 #endif /* IP_STATS */
334 return ip_output_if(p, src, dest, ttl, proto, netif);
336 /*-----------------------------------------------------------------------------------*/
339 ip_debug_print(struct pbuf *p)
341 struct ip_hdr *iphdr = p->payload;
344 payload = (char *)iphdr + IP_HLEN;
346 DEBUGF(IP_DEBUG, ("IP header:\n"));
347 DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
348 DEBUGF(IP_DEBUG, ("|%2d | %x%x | %x%x | (v, traffic class, flow label)\n",
350 iphdr->tclass1, iphdr->tclass2,
351 iphdr->flow1, iphdr->flow2));
352 DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
353 DEBUGF(IP_DEBUG, ("| %5d | %2d | %2d | (len, nexthdr, hoplim)\n",
357 DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
358 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (src)\n",
359 ntohl(iphdr->src.addr[0]) >> 16 & 0xffff,
360 ntohl(iphdr->src.addr[0]) & 0xffff));
361 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (src)\n",
362 ntohl(iphdr->src.addr[1]) >> 16 & 0xffff,
363 ntohl(iphdr->src.addr[1]) & 0xffff));
364 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (src)\n",
365 ntohl(iphdr->src.addr[2]) >> 16 & 0xffff,
366 ntohl(iphdr->src.addr[2]) & 0xffff));
367 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (src)\n",
368 ntohl(iphdr->src.addr[3]) >> 16 & 0xffff,
369 ntohl(iphdr->src.addr[3]) & 0xffff));
370 DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
371 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (dest)\n",
372 ntohl(iphdr->dest.addr[0]) >> 16 & 0xffff,
373 ntohl(iphdr->dest.addr[0]) & 0xffff));
374 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (dest)\n",
375 ntohl(iphdr->dest.addr[1]) >> 16 & 0xffff,
376 ntohl(iphdr->dest.addr[1]) & 0xffff));
377 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (dest)\n",
378 ntohl(iphdr->dest.addr[2]) >> 16 & 0xffff,
379 ntohl(iphdr->dest.addr[2]) & 0xffff));
380 DEBUGF(IP_DEBUG, ("| %4lx | %4lx | (dest)\n",
381 ntohl(iphdr->dest.addr[3]) >> 16 & 0xffff,
382 ntohl(iphdr->dest.addr[3]) & 0xffff));
383 DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
385 #endif /* IP_DEBUG */
386 /*-----------------------------------------------------------------------------------*/