SAMV71 Xplained Ultra Software Package 1.3

ip6.c

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  *
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 
00033 
00034 
00035 /* ip.c
00036  *
00037  * This is the code for the IP layer for IPv6.
00038  *
00039  */
00040 
00041 
00042 #include "lwip/opt.h"
00043 
00044 #include "lwip/def.h"
00045 #include "lwip/mem.h"
00046 #include "lwip/ip.h"
00047 #include "lwip/inet.h"
00048 #include "lwip/netif.h"
00049 #include "lwip/icmp.h"
00050 #include "lwip/udp.h"
00051 #include "lwip/tcp.h"
00052 
00053 #include "lwip/stats.h"
00054 
00055 #include "arch/perf.h"
00056 
00057 /* ip_init:
00058  *
00059  * Initializes the IP layer.
00060  */
00061 
00062 void
00063 ip_init(void)
00064 {
00065 }
00066 
00067 /* ip_route:
00068  *
00069  * Finds the appropriate network interface for a given IP address. It searches the
00070  * list of network interfaces linearly. A match is found if the masked IP address of
00071  * the network interface equals the masked IP address given to the function.
00072  */
00073 
00074 struct netif *
00075 ip_route(struct ip_addr *dest)
00076 {
00077   struct netif *netif;
00078 
00079   for(netif = netif_list; netif != NULL; netif = netif->next) {
00080     if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
00081       return netif;
00082     }
00083   }
00084 
00085   return netif_default;
00086 }
00087 
00088 /* ip_forward:
00089  *
00090  * Forwards an IP packet. It finds an appropriate route for the packet, decrements
00091  * the TTL value of the packet, adjusts the checksum and outputs the packet on the
00092  * appropriate interface.
00093  */
00094 
00095 static void
00096 ip_forward(struct pbuf *p, struct ip_hdr *iphdr)
00097 {
00098   struct netif *netif;
00099 
00100   PERF_START;
00101 
00102   if ((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
00103 
00104     LWIP_DEBUGF(IP_DEBUG, ("ip_input: no forwarding route found for "));
00105 #if IP_DEBUG
00106     ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00107 #endif /* IP_DEBUG */
00108     LWIP_DEBUGF(IP_DEBUG, ("\n"));
00109     pbuf_free(p);
00110     return;
00111   }
00112   /* Decrement TTL and send ICMP if ttl == 0. */
00113   if (--iphdr->hoplim == 0) {
00114 #if LWIP_ICMP
00115     /* Don't send ICMP messages in response to ICMP messages */
00116     if (iphdr->nexthdr != IP_PROTO_ICMP) {
00117       icmp_time_exceeded(p, ICMP_TE_TTL);
00118     }
00119 #endif /* LWIP_ICMP */
00120     pbuf_free(p);
00121     return;
00122   }
00123 
00124   /* Incremental update of the IP checksum. */
00125   /*  if (iphdr->chksum >= htons(0xffff - 0x100)) {
00126     iphdr->chksum += htons(0x100) + 1;
00127   } else {
00128     iphdr->chksum += htons(0x100);
00129     }*/
00130 
00131 
00132   LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to "));
00133 #if IP_DEBUG
00134   ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00135 #endif /* IP_DEBUG */
00136   LWIP_DEBUGF(IP_DEBUG, ("\n"));
00137 
00138   IP_STATS_INC(ip.fw);
00139   IP_STATS_INC(ip.xmit);
00140 
00141   PERF_STOP("ip_forward");
00142 
00143   netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
00144 }
00145 
00146 /* ip_input:
00147  *
00148  * This function is called by the network interface device driver when an IP packet is
00149  * received. The function does the basic checks of the IP header such as packet size
00150  * being at least larger than the header size etc. If the packet was not destined for
00151  * us, the packet is forwarded (using ip_forward). The IP checksum is always checked.
00152  *
00153  * Finally, the packet is sent to the upper layer protocol input function.
00154  */
00155 
00156 void
00157 ip_input(struct pbuf *p, struct netif *inp) {
00158   struct ip_hdr *iphdr;
00159   struct netif *netif;
00160 
00161 
00162   PERF_START;
00163 
00164 #if IP_DEBUG
00165   ip_debug_print(p);
00166 #endif /* IP_DEBUG */
00167 
00168 
00169   IP_STATS_INC(ip.recv);
00170 
00171   /* identify the IP header */
00172   iphdr = p->payload;
00173 
00174 
00175   if (iphdr->v != 6) {
00176     LWIP_DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number\n"));
00177 #if IP_DEBUG
00178     ip_debug_print(p);
00179 #endif /* IP_DEBUG */
00180     pbuf_free(p);
00181     IP_STATS_INC(ip.err);
00182     IP_STATS_INC(ip.drop);
00183     return;
00184   }
00185 
00186   /* is this packet for us? */
00187   for(netif = netif_list; netif != NULL; netif = netif->next) {
00188 #if IP_DEBUG
00189     LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest "));
00190     ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00191     LWIP_DEBUGF(IP_DEBUG, ("netif->ip_addr "));
00192     ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00193     LWIP_DEBUGF(IP_DEBUG, ("\n"));
00194 #endif /* IP_DEBUG */
00195     if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))) {
00196       break;
00197     }
00198   }
00199 
00200 
00201   if (netif == NULL) {
00202     /* packet not for us, route or discard */
00203 #if IP_FORWARD
00204     ip_forward(p, iphdr);
00205 #endif
00206     pbuf_free(p);
00207     return;
00208   }
00209 
00210   pbuf_realloc(p, IP_HLEN + ntohs(iphdr->len));
00211 
00212   /* send to upper layers */
00213 #if IP_DEBUG
00214   /*  LWIP_DEBUGF("ip_input: \n");
00215   ip_debug_print(p);
00216   LWIP_DEBUGF("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
00217 #endif /* IP_DEBUG */
00218 
00219   if(pbuf_header(p, -IP_HLEN)) {
00220     LWIP_ASSERT("Can't move over header in packet", 0);
00221     return;
00222   }
00223 
00224   switch (iphdr->nexthdr) {
00225   case IP_PROTO_UDP:
00226     udp_input(p, inp);
00227     break;
00228   case IP_PROTO_TCP:
00229     tcp_input(p, inp);
00230     break;
00231 #if LWIP_ICMP
00232   case IP_PROTO_ICMP:
00233     icmp_input(p, inp);
00234     break;
00235 #endif /* LWIP_ICMP */
00236   default:
00237 #if LWIP_ICMP
00238     /* send ICMP destination protocol unreachable */
00239     icmp_dest_unreach(p, ICMP_DUR_PROTO);
00240 #endif /* LWIP_ICMP */
00241     pbuf_free(p);
00242     LWIP_DEBUGF(IP_DEBUG, ("Unsupported transport protocol %"U16_F"\n",
00243           iphdr->nexthdr));
00244 
00245     IP_STATS_INC(ip.proterr);
00246     IP_STATS_INC(ip.drop);
00247   }
00248   PERF_STOP("ip_input");
00249 }
00250 
00251 
00252 /* ip_output_if:
00253  *
00254  * Sends an IP packet on a network interface. This function constructs the IP header
00255  * and calculates the IP header checksum. If the source IP address is NULL,
00256  * the IP address of the outgoing network interface is filled in as source address.
00257  */
00258 
00259 err_t
00260 ip_output_if (struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00261        u8_t ttl,
00262        u8_t proto, struct netif *netif)
00263 {
00264   struct ip_hdr *iphdr;
00265 
00266   PERF_START;
00267 
00268   LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
00269   if (pbuf_header(p, IP_HLEN)) {
00270     LWIP_DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
00271     IP_STATS_INC(ip.err);
00272 
00273     return ERR_BUF;
00274   }
00275   LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
00276 
00277   iphdr = p->payload;
00278 
00279 
00280   if (dest != IP_HDRINCL) {
00281     LWIP_DEBUGF(IP_DEBUG, ("!IP_HDRLINCL\n"));
00282     iphdr->hoplim = ttl;
00283     iphdr->nexthdr = proto;
00284     iphdr->len = htons(p->tot_len - IP_HLEN);
00285     ip_addr_set(&(iphdr->dest), dest);
00286 
00287     iphdr->v = 6;
00288 
00289     if (ip_addr_isany(src)) {
00290       ip_addr_set(&(iphdr->src), &(netif->ip_addr));
00291     } else {
00292       ip_addr_set(&(iphdr->src), src);
00293     }
00294 
00295   } else {
00296     dest = &(iphdr->dest);
00297   }
00298 
00299   IP_STATS_INC(ip.xmit);
00300 
00301   LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c (len %"U16_F")\n", netif->name[0], netif->name[1], p->tot_len));
00302 #if IP_DEBUG
00303   ip_debug_print(p);
00304 #endif /* IP_DEBUG */
00305 
00306   PERF_STOP("ip_output_if");
00307   return netif->output(netif, p, dest);
00308 }
00309 
00310 /* ip_output:
00311  *
00312  * Simple interface to ip_output_if. It finds the outgoing network interface and
00313  * calls upon ip_output_if to do the actual work.
00314  */
00315 
00316 err_t
00317 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00318     u8_t ttl, u8_t proto)
00319 {
00320   struct netif *netif;
00321   if ((netif = ip_route(dest)) == NULL) {
00322     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00323     IP_STATS_INC(ip.rterr);
00324     return ERR_RTE;
00325   }
00326 
00327   return ip_output_if (p, src, dest, ttl, proto, netif);
00328 }
00329 
00330 #if LWIP_NETIF_HWADDRHINT
00331 err_t
00332 ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00333           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
00334 {
00335   struct netif *netif;
00336   err_t err;
00337 
00338   if ((netif = ip_route(dest)) == NULL) {
00339     LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00340     IP_STATS_INC(ip.rterr);
00341     return ERR_RTE;
00342   }
00343 
00344   netif->addr_hint = addr_hint;
00345   err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
00346   netif->addr_hint = NULL;
00347 
00348   return err;
00349 }
00350 #endif /* LWIP_NETIF_HWADDRHINT*/
00351 
00352 #if IP_DEBUG
00353 void
00354 ip_debug_print(struct pbuf *p)
00355 {
00356   struct ip_hdr *iphdr = p->payload;
00357 
00358   LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
00359   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00360   LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |  %"X16_F"%"X16_F"  |      %"X16_F"%"X16_F"           | (v, traffic class, flow label)\n",
00361         iphdr->v,
00362         iphdr->tclass1, iphdr->tclass2,
00363         iphdr->flow1, iphdr->flow2));
00364   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00365   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      | %2"U16_F"  |  %2"U16_F"   | (len, nexthdr, hoplim)\n",
00366         ntohs(iphdr->len),
00367         iphdr->nexthdr,
00368         iphdr->hoplim));
00369   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00370   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (src)\n",
00371         (ntohl(iphdr->src.addr[0]) >> 16) & 0xffff,
00372         ntohl(iphdr->src.addr[0]) & 0xffff));
00373   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (src)\n",
00374         (ntohl(iphdr->src.addr[1]) >> 16) & 0xffff,
00375         ntohl(iphdr->src.addr[1]) & 0xffff));
00376   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (src)\n",
00377         (ntohl(iphdr->src.addr[2]) >> 16) & 0xffff,
00378         ntohl(iphdr->src.addr[2]) & 0xffff));
00379   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (src)\n",
00380         (ntohl(iphdr->src.addr[3]) >> 16) & 0xffff,
00381         ntohl(iphdr->src.addr[3]) & 0xffff));
00382   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00383   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (dest)\n",
00384         (ntohl(iphdr->dest.addr[0]) >> 16) & 0xffff,
00385         ntohl(iphdr->dest.addr[0]) & 0xffff));
00386   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (dest)\n",
00387         (ntohl(iphdr->dest.addr[1]) >> 16) & 0xffff,
00388         ntohl(iphdr->dest.addr[1]) & 0xffff));
00389   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (dest)\n",
00390         (ntohl(iphdr->dest.addr[2]) >> 16) & 0xffff,
00391         ntohl(iphdr->dest.addr[2]) & 0xffff));
00392   LWIP_DEBUGF(IP_DEBUG, ("|       %4"X32_F"      |       %4"X32_F"     | (dest)\n",
00393         (ntohl(iphdr->dest.addr[3]) >> 16) & 0xffff,
00394         ntohl(iphdr->dest.addr[3]) & 0xffff));
00395   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00396 }
00397 #endif /* IP_DEBUG */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines