ip6_input.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * Based in linux/net/ipv4/ip_input.c
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. /* Changes
  17. *
  18. * Mitsuru KANDA @USAGI and
  19. * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/in6.h>
  28. #include <linux/icmpv6.h>
  29. #include <linux/mroute6.h>
  30. #include <linux/slab.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(skb) == 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. const struct ipv6hdr *hdr;
  52. u32 pkt_len;
  53. struct inet6_dev *idev;
  54. struct net *net = dev_net(skb->dev);
  55. if (skb->pkt_type == PACKET_OTHERHOST) {
  56. kfree_skb(skb);
  57. return NET_RX_DROP;
  58. }
  59. rcu_read_lock();
  60. idev = __in6_dev_get(skb->dev);
  61. IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_IN, skb->len);
  62. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
  63. !idev || unlikely(idev->cnf.disable_ipv6)) {
  64. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDISCARDS);
  65. goto drop;
  66. }
  67. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  68. /*
  69. * Store incoming device index. When the packet will
  70. * be queued, we cannot refer to skb->dev anymore.
  71. *
  72. * BTW, when we send a packet for our own local address on a
  73. * non-loopback interface (e.g. ethX), it is being delivered
  74. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  75. * It, however, should be considered as if it is being
  76. * arrived via the sending interface (ethX), because of the
  77. * nature of scoping architecture. --yoshfuji
  78. */
  79. IP6CB(skb)->iif = skb_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
  80. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  81. goto err;
  82. hdr = ipv6_hdr(skb);
  83. if (hdr->version != 6)
  84. goto err;
  85. /*
  86. * RFC4291 2.5.3
  87. * A packet received on an interface with a destination address
  88. * of loopback must be dropped.
  89. */
  90. if (!(dev->flags & IFF_LOOPBACK) &&
  91. ipv6_addr_loopback(&hdr->daddr))
  92. goto err;
  93. /*
  94. * RFC4291 2.7
  95. * Multicast addresses must not be used as source addresses in IPv6
  96. * packets or appear in any Routing header.
  97. */
  98. if (ipv6_addr_is_multicast(&hdr->saddr))
  99. goto err;
  100. skb->transport_header = skb->network_header + sizeof(*hdr);
  101. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  102. pkt_len = ntohs(hdr->payload_len);
  103. /* pkt_len may be zero if Jumbo payload option is present */
  104. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  105. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  106. IP6_INC_STATS_BH(net,
  107. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  108. goto drop;
  109. }
  110. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  111. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  112. goto drop;
  113. }
  114. hdr = ipv6_hdr(skb);
  115. }
  116. if (hdr->nexthdr == NEXTHDR_HOP) {
  117. if (ipv6_parse_hopopts(skb) < 0) {
  118. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  119. rcu_read_unlock();
  120. return NET_RX_DROP;
  121. }
  122. }
  123. rcu_read_unlock();
  124. /* Must drop socket now because of tproxy. */
  125. skb_orphan(skb);
  126. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  127. ip6_rcv_finish);
  128. err:
  129. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  130. drop:
  131. rcu_read_unlock();
  132. kfree_skb(skb);
  133. return NET_RX_DROP;
  134. }
  135. /*
  136. * Deliver the packet to the host
  137. */
  138. static int ip6_input_finish(struct sk_buff *skb)
  139. {
  140. const struct inet6_protocol *ipprot;
  141. unsigned int nhoff;
  142. int nexthdr, raw;
  143. u8 hash;
  144. struct inet6_dev *idev;
  145. struct net *net = dev_net(skb_dst(skb)->dev);
  146. /*
  147. * Parse extension headers
  148. */
  149. rcu_read_lock();
  150. resubmit:
  151. idev = ip6_dst_idev(skb_dst(skb));
  152. if (!pskb_pull(skb, skb_transport_offset(skb)))
  153. goto discard;
  154. nhoff = IP6CB(skb)->nhoff;
  155. nexthdr = skb_network_header(skb)[nhoff];
  156. raw = raw6_local_deliver(skb, nexthdr);
  157. hash = nexthdr & (MAX_INET_PROTOS - 1);
  158. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  159. int ret;
  160. if (ipprot->flags & INET6_PROTO_FINAL) {
  161. const struct ipv6hdr *hdr;
  162. /* Free reference early: we don't need it any more,
  163. and it may hold ip_conntrack module loaded
  164. indefinitely. */
  165. nf_reset(skb);
  166. skb_postpull_rcsum(skb, skb_network_header(skb),
  167. skb_network_header_len(skb));
  168. hdr = ipv6_hdr(skb);
  169. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  170. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  171. &hdr->saddr) &&
  172. !ipv6_is_mld(skb, nexthdr))
  173. goto discard;
  174. }
  175. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  176. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  177. goto discard;
  178. ret = ipprot->handler(skb);
  179. if (ret > 0)
  180. goto resubmit;
  181. else if (ret == 0)
  182. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  183. } else {
  184. if (!raw) {
  185. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  186. IP6_INC_STATS_BH(net, idev,
  187. IPSTATS_MIB_INUNKNOWNPROTOS);
  188. icmpv6_send(skb, ICMPV6_PARAMPROB,
  189. ICMPV6_UNK_NEXTHDR, nhoff);
  190. }
  191. } else
  192. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  193. kfree_skb(skb);
  194. }
  195. rcu_read_unlock();
  196. return 0;
  197. discard:
  198. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDISCARDS);
  199. rcu_read_unlock();
  200. kfree_skb(skb);
  201. return 0;
  202. }
  203. int ip6_input(struct sk_buff *skb)
  204. {
  205. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  206. ip6_input_finish);
  207. }
  208. int ip6_mc_input(struct sk_buff *skb)
  209. {
  210. const struct ipv6hdr *hdr;
  211. int deliver;
  212. IP6_UPD_PO_STATS_BH(dev_net(skb_dst(skb)->dev),
  213. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
  214. skb->len);
  215. hdr = ipv6_hdr(skb);
  216. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  217. #ifdef CONFIG_IPV6_MROUTE
  218. /*
  219. * IPv6 multicast router mode is now supported ;)
  220. */
  221. if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
  222. !(ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) &&
  223. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  224. /*
  225. * Okay, we try to forward - split and duplicate
  226. * packets.
  227. */
  228. struct sk_buff *skb2;
  229. struct inet6_skb_parm *opt = IP6CB(skb);
  230. /* Check for MLD */
  231. if (unlikely(opt->ra)) {
  232. /* Check if this is a mld message */
  233. u8 *ptr = skb_network_header(skb) + opt->ra;
  234. struct icmp6hdr *icmp6;
  235. u8 nexthdr = hdr->nexthdr;
  236. __be16 frag_off;
  237. int offset;
  238. /* Check if the value of Router Alert
  239. * is for MLD (0x0000).
  240. */
  241. if ((ptr[2] | ptr[3]) == 0) {
  242. deliver = 0;
  243. if (!ipv6_ext_hdr(nexthdr)) {
  244. /* BUG */
  245. goto out;
  246. }
  247. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  248. &nexthdr, &frag_off);
  249. if (offset < 0)
  250. goto out;
  251. if (nexthdr != IPPROTO_ICMPV6)
  252. goto out;
  253. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  254. offset + 1 - skb->data)))
  255. goto out;
  256. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  257. switch (icmp6->icmp6_type) {
  258. case ICMPV6_MGM_QUERY:
  259. case ICMPV6_MGM_REPORT:
  260. case ICMPV6_MGM_REDUCTION:
  261. case ICMPV6_MLD2_REPORT:
  262. deliver = 1;
  263. break;
  264. }
  265. goto out;
  266. }
  267. /* unknown RA - process it normally */
  268. }
  269. if (deliver)
  270. skb2 = skb_clone(skb, GFP_ATOMIC);
  271. else {
  272. skb2 = skb;
  273. skb = NULL;
  274. }
  275. if (skb2) {
  276. ip6_mr_input(skb2);
  277. }
  278. }
  279. out:
  280. #endif
  281. if (likely(deliver))
  282. ip6_input(skb);
  283. else {
  284. /* discard */
  285. kfree_skb(skb);
  286. }
  287. return 0;
  288. }