ip6_input.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #include <net/inet_ecn.h>
  44. #include <net/dst_metadata.h>
  45. int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  46. {
  47. /* if ingress device is enslaved to an L3 master device pass the
  48. * skb to its handler for processing
  49. */
  50. skb = l3mdev_ip6_rcv(skb);
  51. if (!skb)
  52. return NET_RX_SUCCESS;
  53. if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
  54. const struct inet6_protocol *ipprot;
  55. ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]);
  56. if (ipprot && ipprot->early_demux)
  57. ipprot->early_demux(skb);
  58. }
  59. if (!skb_valid_dst(skb))
  60. ip6_route_input(skb);
  61. return dst_input(skb);
  62. }
  63. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  64. {
  65. const struct ipv6hdr *hdr;
  66. u32 pkt_len;
  67. struct inet6_dev *idev;
  68. struct net *net = dev_net(skb->dev);
  69. if (skb->pkt_type == PACKET_OTHERHOST) {
  70. kfree_skb(skb);
  71. return NET_RX_DROP;
  72. }
  73. rcu_read_lock();
  74. idev = __in6_dev_get(skb->dev);
  75. __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
  76. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
  77. !idev || unlikely(idev->cnf.disable_ipv6)) {
  78. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  79. goto drop;
  80. }
  81. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  82. /*
  83. * Store incoming device index. When the packet will
  84. * be queued, we cannot refer to skb->dev anymore.
  85. *
  86. * BTW, when we send a packet for our own local address on a
  87. * non-loopback interface (e.g. ethX), it is being delivered
  88. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  89. * It, however, should be considered as if it is being
  90. * arrived via the sending interface (ethX), because of the
  91. * nature of scoping architecture. --yoshfuji
  92. */
  93. IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
  94. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  95. goto err;
  96. hdr = ipv6_hdr(skb);
  97. if (hdr->version != 6)
  98. goto err;
  99. __IP6_ADD_STATS(net, idev,
  100. IPSTATS_MIB_NOECTPKTS +
  101. (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
  102. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  103. /*
  104. * RFC4291 2.5.3
  105. * A packet received on an interface with a destination address
  106. * of loopback must be dropped.
  107. */
  108. if (!(dev->flags & IFF_LOOPBACK) &&
  109. ipv6_addr_loopback(&hdr->daddr))
  110. goto err;
  111. /* RFC4291 Errata ID: 3480
  112. * Interface-Local scope spans only a single interface on a
  113. * node and is useful only for loopback transmission of
  114. * multicast. Packets with interface-local scope received
  115. * from another node must be discarded.
  116. */
  117. if (!(skb->pkt_type == PACKET_LOOPBACK ||
  118. dev->flags & IFF_LOOPBACK) &&
  119. ipv6_addr_is_multicast(&hdr->daddr) &&
  120. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
  121. goto err;
  122. /* If enabled, drop unicast packets that were encapsulated in link-layer
  123. * multicast or broadcast to protected against the so-called "hole-196"
  124. * attack in 802.11 wireless.
  125. */
  126. if (!ipv6_addr_is_multicast(&hdr->daddr) &&
  127. (skb->pkt_type == PACKET_BROADCAST ||
  128. skb->pkt_type == PACKET_MULTICAST) &&
  129. idev->cnf.drop_unicast_in_l2_multicast)
  130. goto err;
  131. /* RFC4291 2.7
  132. * Nodes must not originate a packet to a multicast address whose scope
  133. * field contains the reserved value 0; if such a packet is received, it
  134. * must be silently dropped.
  135. */
  136. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  137. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
  138. goto err;
  139. /*
  140. * RFC4291 2.7
  141. * Multicast addresses must not be used as source addresses in IPv6
  142. * packets or appear in any Routing header.
  143. */
  144. if (ipv6_addr_is_multicast(&hdr->saddr))
  145. goto err;
  146. skb->transport_header = skb->network_header + sizeof(*hdr);
  147. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  148. pkt_len = ntohs(hdr->payload_len);
  149. /* pkt_len may be zero if Jumbo payload option is present */
  150. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  151. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  152. __IP6_INC_STATS(net,
  153. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  154. goto drop;
  155. }
  156. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  157. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  158. goto drop;
  159. }
  160. hdr = ipv6_hdr(skb);
  161. }
  162. if (hdr->nexthdr == NEXTHDR_HOP) {
  163. if (ipv6_parse_hopopts(skb) < 0) {
  164. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  165. rcu_read_unlock();
  166. return NET_RX_DROP;
  167. }
  168. }
  169. rcu_read_unlock();
  170. /* Must drop socket now because of tproxy. */
  171. skb_orphan(skb);
  172. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  173. net, NULL, skb, dev, NULL,
  174. ip6_rcv_finish);
  175. err:
  176. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  177. drop:
  178. rcu_read_unlock();
  179. kfree_skb(skb);
  180. return NET_RX_DROP;
  181. }
  182. /*
  183. * Deliver the packet to the host
  184. */
  185. static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  186. {
  187. const struct inet6_protocol *ipprot;
  188. struct inet6_dev *idev;
  189. unsigned int nhoff;
  190. int nexthdr;
  191. bool raw;
  192. bool have_final = false;
  193. /*
  194. * Parse extension headers
  195. */
  196. rcu_read_lock();
  197. resubmit:
  198. idev = ip6_dst_idev(skb_dst(skb));
  199. if (!pskb_pull(skb, skb_transport_offset(skb)))
  200. goto discard;
  201. nhoff = IP6CB(skb)->nhoff;
  202. nexthdr = skb_network_header(skb)[nhoff];
  203. resubmit_final:
  204. raw = raw6_local_deliver(skb, nexthdr);
  205. ipprot = rcu_dereference(inet6_protos[nexthdr]);
  206. if (ipprot) {
  207. int ret;
  208. if (have_final) {
  209. if (!(ipprot->flags & INET6_PROTO_FINAL)) {
  210. /* Once we've seen a final protocol don't
  211. * allow encapsulation on any non-final
  212. * ones. This allows foo in UDP encapsulation
  213. * to work.
  214. */
  215. goto discard;
  216. }
  217. } else if (ipprot->flags & INET6_PROTO_FINAL) {
  218. const struct ipv6hdr *hdr;
  219. /* Only do this once for first final protocol */
  220. have_final = true;
  221. /* Free reference early: we don't need it any more,
  222. and it may hold ip_conntrack module loaded
  223. indefinitely. */
  224. nf_reset(skb);
  225. skb_postpull_rcsum(skb, skb_network_header(skb),
  226. skb_network_header_len(skb));
  227. hdr = ipv6_hdr(skb);
  228. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  229. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  230. &hdr->saddr) &&
  231. !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
  232. goto discard;
  233. }
  234. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  235. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  236. goto discard;
  237. ret = ipprot->handler(skb);
  238. if (ret > 0) {
  239. if (ipprot->flags & INET6_PROTO_FINAL) {
  240. /* Not an extension header, most likely UDP
  241. * encapsulation. Use return value as nexthdr
  242. * protocol not nhoff (which presumably is
  243. * not set by handler).
  244. */
  245. nexthdr = ret;
  246. goto resubmit_final;
  247. } else {
  248. goto resubmit;
  249. }
  250. } else if (ret == 0) {
  251. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  252. }
  253. } else {
  254. if (!raw) {
  255. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  256. __IP6_INC_STATS(net, idev,
  257. IPSTATS_MIB_INUNKNOWNPROTOS);
  258. icmpv6_send(skb, ICMPV6_PARAMPROB,
  259. ICMPV6_UNK_NEXTHDR, nhoff);
  260. }
  261. kfree_skb(skb);
  262. } else {
  263. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  264. consume_skb(skb);
  265. }
  266. }
  267. rcu_read_unlock();
  268. return 0;
  269. discard:
  270. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  271. rcu_read_unlock();
  272. kfree_skb(skb);
  273. return 0;
  274. }
  275. int ip6_input(struct sk_buff *skb)
  276. {
  277. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
  278. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  279. ip6_input_finish);
  280. }
  281. EXPORT_SYMBOL_GPL(ip6_input);
  282. int ip6_mc_input(struct sk_buff *skb)
  283. {
  284. const struct ipv6hdr *hdr;
  285. bool deliver;
  286. __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
  287. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
  288. skb->len);
  289. hdr = ipv6_hdr(skb);
  290. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  291. #ifdef CONFIG_IPV6_MROUTE
  292. /*
  293. * IPv6 multicast router mode is now supported ;)
  294. */
  295. if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
  296. !(ipv6_addr_type(&hdr->daddr) &
  297. (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
  298. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  299. /*
  300. * Okay, we try to forward - split and duplicate
  301. * packets.
  302. */
  303. struct sk_buff *skb2;
  304. struct inet6_skb_parm *opt = IP6CB(skb);
  305. /* Check for MLD */
  306. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  307. /* Check if this is a mld message */
  308. u8 nexthdr = hdr->nexthdr;
  309. __be16 frag_off;
  310. int offset;
  311. /* Check if the value of Router Alert
  312. * is for MLD (0x0000).
  313. */
  314. if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
  315. deliver = false;
  316. if (!ipv6_ext_hdr(nexthdr)) {
  317. /* BUG */
  318. goto out;
  319. }
  320. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  321. &nexthdr, &frag_off);
  322. if (offset < 0)
  323. goto out;
  324. if (ipv6_is_mld(skb, nexthdr, offset))
  325. deliver = true;
  326. goto out;
  327. }
  328. /* unknown RA - process it normally */
  329. }
  330. if (deliver)
  331. skb2 = skb_clone(skb, GFP_ATOMIC);
  332. else {
  333. skb2 = skb;
  334. skb = NULL;
  335. }
  336. if (skb2) {
  337. ip6_mr_input(skb2);
  338. }
  339. }
  340. out:
  341. #endif
  342. if (likely(deliver))
  343. ip6_input(skb);
  344. else {
  345. /* discard */
  346. kfree_skb(skb);
  347. }
  348. return 0;
  349. }