ip6_input.c 8.1 KB

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