ip6_input.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * IPv6 input
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Ian P. Morris <I.P.Morris@soton.ac.uk>
  8. *
  9. * $Id: ip6_input.c,v 1.1.1.1 2007/06/12 07:27:14 eyryu Exp $
  10. *
  11. * Based in linux/net/ipv4/ip_input.c
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. /* Changes
  19. *
  20. * Mitsuru KANDA @USAGI and
  21. * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/in6.h>
  30. #include <linux/icmpv6.h>
  31. #include <linux/netfilter.h>
  32. #include <linux/netfilter_ipv6.h>
  33. #include <net/sock.h>
  34. #include <net/snmp.h>
  35. #include <net/ipv6.h>
  36. #include <net/protocol.h>
  37. #include <net/transp_v6.h>
  38. #include <net/rawv6.h>
  39. #include <net/ndisc.h>
  40. #include <net/ip6_route.h>
  41. #include <net/addrconf.h>
  42. #include <net/xfrm.h>
  43. inline int ip6_rcv_finish( struct sk_buff *skb)
  44. {
  45. if (skb->dst == NULL)
  46. ip6_route_input(skb);
  47. return dst_input(skb);
  48. }
  49. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  50. {
  51. struct ipv6hdr *hdr;
  52. u32 pkt_len;
  53. struct inet6_dev *idev;
  54. if (skb->pkt_type == PACKET_OTHERHOST) {
  55. kfree_skb(skb);
  56. return 0;
  57. }
  58. rcu_read_lock();
  59. idev = __in6_dev_get(skb->dev);
  60. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INRECEIVES);
  61. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  62. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  63. rcu_read_unlock();
  64. goto out;
  65. }
  66. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  67. /*
  68. * Store incoming device index. When the packet will
  69. * be queued, we cannot refer to skb->dev anymore.
  70. *
  71. * BTW, when we send a packet for our own local address on a
  72. * non-loopback interface (e.g. ethX), it is being delivered
  73. * via the loopback interface (lo) here; skb->dev = &loopback_dev.
  74. * It, however, should be considered as if it is being
  75. * arrived via the sending interface (ethX), because of the
  76. * nature of scoping architecture. --yoshfuji
  77. */
  78. IP6CB(skb)->iif = skb->dst ? ip6_dst_idev(skb->dst)->dev->ifindex : dev->ifindex;
  79. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  80. goto err;
  81. hdr = skb->nh.ipv6h;
  82. if (hdr->version != 6)
  83. goto err;
  84. skb->h.raw = (u8 *)(hdr + 1);
  85. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  86. pkt_len = ntohs(hdr->payload_len);
  87. /* pkt_len may be zero if Jumbo payload option is present */
  88. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  89. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  90. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  91. goto drop;
  92. }
  93. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  94. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  95. goto drop;
  96. }
  97. hdr = skb->nh.ipv6h;
  98. }
  99. if (hdr->nexthdr == NEXTHDR_HOP) {
  100. if (ipv6_parse_hopopts(&skb) < 0) {
  101. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  102. rcu_read_unlock();
  103. return 0;
  104. }
  105. }
  106. rcu_read_unlock();
  107. return NF_HOOK(PF_INET6,NF_IP6_PRE_ROUTING, skb, dev, NULL, ip6_rcv_finish);
  108. err:
  109. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  110. drop:
  111. rcu_read_unlock();
  112. kfree_skb(skb);
  113. out:
  114. return 0;
  115. }
  116. /*
  117. * Deliver the packet to the host
  118. */
  119. static inline int ip6_input_finish(struct sk_buff *skb)
  120. {
  121. struct inet6_protocol *ipprot;
  122. struct sock *raw_sk;
  123. unsigned int nhoff;
  124. int nexthdr;
  125. u8 hash;
  126. struct inet6_dev *idev;
  127. /*
  128. * Parse extension headers
  129. */
  130. rcu_read_lock();
  131. resubmit:
  132. idev = ip6_dst_idev(skb->dst);
  133. if (!pskb_pull(skb, skb->h.raw - skb->data))
  134. goto discard;
  135. nhoff = IP6CB(skb)->nhoff;
  136. nexthdr = skb->nh.raw[nhoff];
  137. raw_sk = sk_head(&raw_v6_htable[nexthdr & (MAX_INET_PROTOS - 1)]);
  138. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  139. raw_sk = NULL;
  140. hash = nexthdr & (MAX_INET_PROTOS - 1);
  141. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  142. int ret;
  143. if (ipprot->flags & INET6_PROTO_FINAL) {
  144. struct ipv6hdr *hdr;
  145. /* Free reference early: we don't need it any more,
  146. and it may hold ip_conntrack module loaded
  147. indefinitely. */
  148. nf_reset(skb);
  149. skb_postpull_rcsum(skb, skb->nh.raw,
  150. skb->h.raw - skb->nh.raw);
  151. hdr = skb->nh.ipv6h;
  152. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  153. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  154. &hdr->saddr) &&
  155. !ipv6_is_mld(skb, nexthdr))
  156. goto discard;
  157. }
  158. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  159. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  160. goto discard;
  161. ret = ipprot->handler(&skb);
  162. if (ret > 0)
  163. goto resubmit;
  164. else if (ret == 0)
  165. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  166. } else {
  167. if (!raw_sk) {
  168. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  169. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  170. icmpv6_send(skb, ICMPV6_PARAMPROB,
  171. ICMPV6_UNK_NEXTHDR, nhoff,
  172. skb->dev);
  173. }
  174. } else
  175. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  176. kfree_skb(skb);
  177. }
  178. rcu_read_unlock();
  179. return 0;
  180. discard:
  181. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  182. rcu_read_unlock();
  183. kfree_skb(skb);
  184. return 0;
  185. }
  186. int ip6_input(struct sk_buff *skb)
  187. {
  188. return NF_HOOK(PF_INET6,NF_IP6_LOCAL_IN, skb, skb->dev, NULL, ip6_input_finish);
  189. }
  190. int ip6_mc_input(struct sk_buff *skb)
  191. {
  192. struct ipv6hdr *hdr;
  193. int deliver;
  194. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  195. hdr = skb->nh.ipv6h;
  196. deliver = unlikely(skb->dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) ||
  197. ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  198. /*
  199. * IPv6 multicast router mode isnt currently supported.
  200. */
  201. #if 0
  202. if (ipv6_config.multicast_route) {
  203. int addr_type;
  204. addr_type = ipv6_addr_type(&hdr->daddr);
  205. if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) {
  206. struct sk_buff *skb2;
  207. struct dst_entry *dst;
  208. dst = skb->dst;
  209. if (deliver) {
  210. skb2 = skb_clone(skb, GFP_ATOMIC);
  211. dst_output(skb2);
  212. } else {
  213. dst_output(skb);
  214. return 0;
  215. }
  216. }
  217. }
  218. #endif
  219. if (likely(deliver)) {
  220. ip6_input(skb);
  221. return 0;
  222. }
  223. /* discard */
  224. kfree_skb(skb);
  225. return 0;
  226. }