ip6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. /* ip.c
  33. *
  34. * This is the code for the IP layer for IPv6.
  35. *
  36. */
  37. #include "lwip/opt.h"
  38. #include "lwip/def.h"
  39. #include "lwip/mem.h"
  40. #include "lwip/ip.h"
  41. #include "lwip/inet.h"
  42. #include "lwip/netif.h"
  43. #include "lwip/icmp.h"
  44. #include "lwip/udp.h"
  45. #include "lwip/tcp.h"
  46. #include "lwip/stats.h"
  47. #include "arch/perf.h"
  48. /* ip_init:
  49. *
  50. * Initializes the IP layer.
  51. */
  52. void
  53. ip_init(void)
  54. {
  55. }
  56. /* ip_route:
  57. *
  58. * Finds the appropriate network interface for a given IP address. It searches the
  59. * list of network interfaces linearly. A match is found if the masked IP address of
  60. * the network interface equals the masked IP address given to the function.
  61. */
  62. struct netif *
  63. ip_route(struct ip_addr *dest)
  64. {
  65. struct netif *netif;
  66. for(netif = netif_list; netif != NULL; netif = netif->next) {
  67. if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
  68. return netif;
  69. }
  70. }
  71. return netif_default;
  72. }
  73. /* ip_forward:
  74. *
  75. * Forwards an IP packet. It finds an appropriate route for the packet, decrements
  76. * the TTL value of the packet, adjusts the checksum and outputs the packet on the
  77. * appropriate interface.
  78. */
  79. static void
  80. ip_forward(struct pbuf *p, struct ip_hdr *iphdr)
  81. {
  82. struct netif *netif;
  83. PERF_START;
  84. if ((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
  85. LWIP_DEBUGF(IP_DEBUG, ("ip_input: no forwarding route found for "));
  86. #if IP_DEBUG
  87. ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
  88. #endif /* IP_DEBUG */
  89. LWIP_DEBUGF(IP_DEBUG, ("\n"));
  90. pbuf_free(p);
  91. return;
  92. }
  93. /* Decrement TTL and send ICMP if ttl == 0. */
  94. if (--iphdr->hoplim == 0) {
  95. #if LWIP_ICMP
  96. /* Don't send ICMP messages in response to ICMP messages */
  97. if (iphdr->nexthdr != IP_PROTO_ICMP) {
  98. icmp_time_exceeded(p, ICMP_TE_TTL);
  99. }
  100. #endif /* LWIP_ICMP */
  101. pbuf_free(p);
  102. return;
  103. }
  104. /* Incremental update of the IP checksum. */
  105. /* if (iphdr->chksum >= htons(0xffff - 0x100)) {
  106. iphdr->chksum += htons(0x100) + 1;
  107. } else {
  108. iphdr->chksum += htons(0x100);
  109. }*/
  110. LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to "));
  111. #if IP_DEBUG
  112. ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
  113. #endif /* IP_DEBUG */
  114. LWIP_DEBUGF(IP_DEBUG, ("\n"));
  115. IP_STATS_INC(ip.fw);
  116. IP_STATS_INC(ip.xmit);
  117. PERF_STOP("ip_forward");
  118. netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
  119. }
  120. /* ip_input:
  121. *
  122. * This function is called by the network interface device driver when an IP packet is
  123. * received. The function does the basic checks of the IP header such as packet size
  124. * being at least larger than the header size etc. If the packet was not destined for
  125. * us, the packet is forwarded (using ip_forward). The IP checksum is always checked.
  126. *
  127. * Finally, the packet is sent to the upper layer protocol input function.
  128. */
  129. void
  130. ip_input(struct pbuf *p, struct netif *inp) {
  131. struct ip_hdr *iphdr;
  132. struct netif *netif;
  133. PERF_START;
  134. #if IP_DEBUG
  135. ip_debug_print(p);
  136. #endif /* IP_DEBUG */
  137. IP_STATS_INC(ip.recv);
  138. /* identify the IP header */
  139. iphdr = p->payload;
  140. if (iphdr->v != 6) {
  141. LWIP_DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number\n"));
  142. #if IP_DEBUG
  143. ip_debug_print(p);
  144. #endif /* IP_DEBUG */
  145. pbuf_free(p);
  146. IP_STATS_INC(ip.err);
  147. IP_STATS_INC(ip.drop);
  148. return;
  149. }
  150. /* is this packet for us? */
  151. for(netif = netif_list; netif != NULL; netif = netif->next) {
  152. #if IP_DEBUG
  153. LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest "));
  154. ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
  155. LWIP_DEBUGF(IP_DEBUG, ("netif->ip_addr "));
  156. ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
  157. LWIP_DEBUGF(IP_DEBUG, ("\n"));
  158. #endif /* IP_DEBUG */
  159. if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))) {
  160. break;
  161. }
  162. }
  163. if (netif == NULL) {
  164. /* packet not for us, route or discard */
  165. #if IP_FORWARD
  166. ip_forward(p, iphdr);
  167. #endif
  168. pbuf_free(p);
  169. return;
  170. }
  171. pbuf_realloc(p, IP_HLEN + ntohs(iphdr->len));
  172. /* send to upper layers */
  173. #if IP_DEBUG
  174. /* LWIP_DEBUGF("ip_input: \n");
  175. ip_debug_print(p);
  176. LWIP_DEBUGF("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
  177. #endif /* IP_DEBUG */
  178. if(pbuf_header(p, -IP_HLEN)) {
  179. LWIP_ASSERT("Can't move over header in packet", 0);
  180. return;
  181. }
  182. switch (iphdr->nexthdr) {
  183. case IP_PROTO_UDP:
  184. udp_input(p, inp);
  185. break;
  186. case IP_PROTO_TCP:
  187. tcp_input(p, inp);
  188. break;
  189. #if LWIP_ICMP
  190. case IP_PROTO_ICMP:
  191. icmp_input(p, inp);
  192. break;
  193. #endif /* LWIP_ICMP */
  194. default:
  195. #if LWIP_ICMP
  196. /* send ICMP destination protocol unreachable */
  197. icmp_dest_unreach(p, ICMP_DUR_PROTO);
  198. #endif /* LWIP_ICMP */
  199. pbuf_free(p);
  200. LWIP_DEBUGF(IP_DEBUG, ("Unsupported transport protocol %"U16_F"\n",
  201. iphdr->nexthdr));
  202. IP_STATS_INC(ip.proterr);
  203. IP_STATS_INC(ip.drop);
  204. }
  205. PERF_STOP("ip_input");
  206. }
  207. /* ip_output_if:
  208. *
  209. * Sends an IP packet on a network interface. This function constructs the IP header
  210. * and calculates the IP header checksum. If the source IP address is NULL,
  211. * the IP address of the outgoing network interface is filled in as source address.
  212. */
  213. err_t
  214. ip_output_if (struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
  215. u8_t ttl,
  216. u8_t proto, struct netif *netif)
  217. {
  218. struct ip_hdr *iphdr;
  219. PERF_START;
  220. LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
  221. if (pbuf_header(p, IP_HLEN)) {
  222. LWIP_DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
  223. IP_STATS_INC(ip.err);
  224. return ERR_BUF;
  225. }
  226. LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
  227. iphdr = p->payload;
  228. if (dest != IP_HDRINCL) {
  229. LWIP_DEBUGF(IP_DEBUG, ("!IP_HDRLINCL\n"));
  230. iphdr->hoplim = ttl;
  231. iphdr->nexthdr = proto;
  232. iphdr->len = htons(p->tot_len - IP_HLEN);
  233. ip_addr_set(&(iphdr->dest), dest);
  234. iphdr->v = 6;
  235. if (ip_addr_isany(src)) {
  236. ip_addr_set(&(iphdr->src), &(netif->ip_addr));
  237. } else {
  238. ip_addr_set(&(iphdr->src), src);
  239. }
  240. } else {
  241. dest = &(iphdr->dest);
  242. }
  243. IP_STATS_INC(ip.xmit);
  244. LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c (len %"U16_F")\n", netif->name[0], netif->name[1], p->tot_len));
  245. #if IP_DEBUG
  246. ip_debug_print(p);
  247. #endif /* IP_DEBUG */
  248. PERF_STOP("ip_output_if");
  249. return netif->output(netif, p, dest);
  250. }
  251. /* ip_output:
  252. *
  253. * Simple interface to ip_output_if. It finds the outgoing network interface and
  254. * calls upon ip_output_if to do the actual work.
  255. */
  256. err_t
  257. ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
  258. u8_t ttl, u8_t proto)
  259. {
  260. struct netif *netif;
  261. if ((netif = ip_route(dest)) == NULL) {
  262. LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
  263. IP_STATS_INC(ip.rterr);
  264. return ERR_RTE;
  265. }
  266. return ip_output_if (p, src, dest, ttl, proto, netif);
  267. }
  268. #if LWIP_NETIF_HWADDRHINT
  269. err_t
  270. ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
  271. u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
  272. {
  273. struct netif *netif;
  274. err_t err;
  275. if ((netif = ip_route(dest)) == NULL) {
  276. LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
  277. IP_STATS_INC(ip.rterr);
  278. return ERR_RTE;
  279. }
  280. netif->addr_hint = addr_hint;
  281. err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
  282. netif->addr_hint = NULL;
  283. return err;
  284. }
  285. #endif /* LWIP_NETIF_HWADDRHINT*/
  286. #if IP_DEBUG
  287. void
  288. ip_debug_print(struct pbuf *p)
  289. {
  290. struct ip_hdr *iphdr = p->payload;
  291. LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
  292. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  293. LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" | %"X16_F"%"X16_F" | %"X16_F"%"X16_F" | (v, traffic class, flow label)\n",
  294. iphdr->v,
  295. iphdr->tclass1, iphdr->tclass2,
  296. iphdr->flow1, iphdr->flow2));
  297. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  298. LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" | %2"U16_F" | %2"U16_F" | (len, nexthdr, hoplim)\n",
  299. ntohs(iphdr->len),
  300. iphdr->nexthdr,
  301. iphdr->hoplim));
  302. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  303. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
  304. (ntohl(iphdr->src.addr[0]) >> 16) & 0xffff,
  305. ntohl(iphdr->src.addr[0]) & 0xffff));
  306. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
  307. (ntohl(iphdr->src.addr[1]) >> 16) & 0xffff,
  308. ntohl(iphdr->src.addr[1]) & 0xffff));
  309. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
  310. (ntohl(iphdr->src.addr[2]) >> 16) & 0xffff,
  311. ntohl(iphdr->src.addr[2]) & 0xffff));
  312. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
  313. (ntohl(iphdr->src.addr[3]) >> 16) & 0xffff,
  314. ntohl(iphdr->src.addr[3]) & 0xffff));
  315. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  316. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
  317. (ntohl(iphdr->dest.addr[0]) >> 16) & 0xffff,
  318. ntohl(iphdr->dest.addr[0]) & 0xffff));
  319. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
  320. (ntohl(iphdr->dest.addr[1]) >> 16) & 0xffff,
  321. ntohl(iphdr->dest.addr[1]) & 0xffff));
  322. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
  323. (ntohl(iphdr->dest.addr[2]) >> 16) & 0xffff,
  324. ntohl(iphdr->dest.addr[2]) & 0xffff));
  325. LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
  326. (ntohl(iphdr->dest.addr[3]) >> 16) & 0xffff,
  327. ntohl(iphdr->dest.addr[3]) & 0xffff));
  328. LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
  329. }
  330. #endif /* IP_DEBUG */