ip6_input.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.19 2000/12/13 18:31:50 davem 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 (dev->nd_net != &init_net) {
  55. kfree_skb(skb);
  56. return 0;
  57. }
  58. if (skb->pkt_type == PACKET_OTHERHOST) {
  59. kfree_skb(skb);
  60. return 0;
  61. }
  62. rcu_read_lock();
  63. idev = __in6_dev_get(skb->dev);
  64. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INRECEIVES);
  65. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  66. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  67. rcu_read_unlock();
  68. goto out;
  69. }
  70. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  71. /*
  72. * Store incoming device index. When the packet will
  73. * be queued, we cannot refer to skb->dev anymore.
  74. *
  75. * BTW, when we send a packet for our own local address on a
  76. * non-loopback interface (e.g. ethX), it is being delivered
  77. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  78. * It, however, should be considered as if it is being
  79. * arrived via the sending interface (ethX), because of the
  80. * nature of scoping architecture. --yoshfuji
  81. */
  82. IP6CB(skb)->iif = skb->dst ? ip6_dst_idev(skb->dst)->dev->ifindex : dev->ifindex;
  83. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  84. goto err;
  85. hdr = ipv6_hdr(skb);
  86. if (hdr->version != 6)
  87. goto err;
  88. skb->transport_header = skb->network_header + sizeof(*hdr);
  89. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  90. pkt_len = ntohs(hdr->payload_len);
  91. /* pkt_len may be zero if Jumbo payload option is present */
  92. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  93. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  94. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  95. goto drop;
  96. }
  97. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  98. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  99. goto drop;
  100. }
  101. hdr = ipv6_hdr(skb);
  102. }
  103. if (hdr->nexthdr == NEXTHDR_HOP) {
  104. if (ipv6_parse_hopopts(skb) < 0) {
  105. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  106. rcu_read_unlock();
  107. return 0;
  108. }
  109. }
  110. rcu_read_unlock();
  111. return NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  112. ip6_rcv_finish);
  113. err:
  114. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  115. drop:
  116. rcu_read_unlock();
  117. kfree_skb(skb);
  118. out:
  119. return 0;
  120. }
  121. /*
  122. * Deliver the packet to the host
  123. */
  124. static int ip6_input_finish(struct sk_buff *skb)
  125. {
  126. struct inet6_protocol *ipprot;
  127. struct sock *raw_sk;
  128. unsigned int nhoff;
  129. int nexthdr;
  130. u8 hash;
  131. struct inet6_dev *idev;
  132. /*
  133. * Parse extension headers
  134. */
  135. rcu_read_lock();
  136. resubmit:
  137. idev = ip6_dst_idev(skb->dst);
  138. if (!pskb_pull(skb, skb_transport_offset(skb)))
  139. goto discard;
  140. nhoff = IP6CB(skb)->nhoff;
  141. nexthdr = skb_network_header(skb)[nhoff];
  142. raw_sk = sk_head(&raw_v6_htable[nexthdr & (MAX_INET_PROTOS - 1)]);
  143. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  144. raw_sk = NULL;
  145. hash = nexthdr & (MAX_INET_PROTOS - 1);
  146. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  147. int ret;
  148. if (ipprot->flags & INET6_PROTO_FINAL) {
  149. struct ipv6hdr *hdr;
  150. /* Free reference early: we don't need it any more,
  151. and it may hold ip_conntrack module loaded
  152. indefinitely. */
  153. nf_reset(skb);
  154. skb_postpull_rcsum(skb, skb_network_header(skb),
  155. skb_network_header_len(skb));
  156. hdr = ipv6_hdr(skb);
  157. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  158. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  159. &hdr->saddr) &&
  160. !ipv6_is_mld(skb, nexthdr))
  161. goto discard;
  162. }
  163. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  164. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  165. goto discard;
  166. ret = ipprot->handler(skb);
  167. if (ret > 0)
  168. goto resubmit;
  169. else if (ret == 0)
  170. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  171. } else {
  172. if (!raw_sk) {
  173. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  174. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  175. icmpv6_send(skb, ICMPV6_PARAMPROB,
  176. ICMPV6_UNK_NEXTHDR, nhoff,
  177. skb->dev);
  178. }
  179. } else
  180. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  181. kfree_skb(skb);
  182. }
  183. rcu_read_unlock();
  184. return 0;
  185. discard:
  186. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  187. rcu_read_unlock();
  188. kfree_skb(skb);
  189. return 0;
  190. }
  191. int ip6_input(struct sk_buff *skb)
  192. {
  193. return NF_HOOK(PF_INET6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  194. ip6_input_finish);
  195. }
  196. int ip6_mc_input(struct sk_buff *skb)
  197. {
  198. struct ipv6hdr *hdr;
  199. int deliver;
  200. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  201. hdr = ipv6_hdr(skb);
  202. deliver = unlikely(skb->dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) ||
  203. ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  204. /*
  205. * IPv6 multicast router mode isnt currently supported.
  206. */
  207. #if 0
  208. if (ipv6_config.multicast_route) {
  209. int addr_type;
  210. addr_type = ipv6_addr_type(&hdr->daddr);
  211. if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) {
  212. struct sk_buff *skb2;
  213. struct dst_entry *dst;
  214. dst = skb->dst;
  215. if (deliver) {
  216. skb2 = skb_clone(skb, GFP_ATOMIC);
  217. dst_output(skb2);
  218. } else {
  219. dst_output(skb);
  220. return 0;
  221. }
  222. }
  223. }
  224. #endif
  225. if (likely(deliver)) {
  226. ip6_input(skb);
  227. return 0;
  228. }
  229. /* discard */
  230. kfree_skb(skb);
  231. return 0;
  232. }