ip6_input.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * IPv6 input
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <[email protected]>
  7. * Ian P. Morris <[email protected]>
  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. /* While RFC4291 is not explicit about v4mapped addresses
  147. * in IPv6 headers, it seems clear linux dual-stack
  148. * model can not deal properly with these.
  149. * Security models could be fooled by ::ffff:127.0.0.1 for example.
  150. *
  151. * https://tools.ietf.org/html/draft-itojun-v6ops-v4mapped-harmful-02
  152. */
  153. if (ipv6_addr_v4mapped(&hdr->saddr))
  154. goto err;
  155. skb->transport_header = skb->network_header + sizeof(*hdr);
  156. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  157. pkt_len = ntohs(hdr->payload_len);
  158. /* pkt_len may be zero if Jumbo payload option is present */
  159. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  160. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  161. __IP6_INC_STATS(net,
  162. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  163. goto drop;
  164. }
  165. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  166. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  167. goto drop;
  168. }
  169. hdr = ipv6_hdr(skb);
  170. }
  171. if (hdr->nexthdr == NEXTHDR_HOP) {
  172. if (ipv6_parse_hopopts(skb) < 0) {
  173. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  174. rcu_read_unlock();
  175. return NET_RX_DROP;
  176. }
  177. }
  178. rcu_read_unlock();
  179. /* Must drop socket now because of tproxy. */
  180. skb_orphan(skb);
  181. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  182. net, NULL, skb, dev, NULL,
  183. ip6_rcv_finish);
  184. err:
  185. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  186. drop:
  187. rcu_read_unlock();
  188. kfree_skb(skb);
  189. return NET_RX_DROP;
  190. }
  191. /*
  192. * Deliver the packet to the host
  193. */
  194. static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  195. {
  196. const struct inet6_protocol *ipprot;
  197. struct inet6_dev *idev;
  198. unsigned int nhoff;
  199. int nexthdr;
  200. bool raw;
  201. bool have_final = false;
  202. /*
  203. * Parse extension headers
  204. */
  205. rcu_read_lock();
  206. resubmit:
  207. idev = ip6_dst_idev(skb_dst(skb));
  208. if (!pskb_pull(skb, skb_transport_offset(skb)))
  209. goto discard;
  210. nhoff = IP6CB(skb)->nhoff;
  211. nexthdr = skb_network_header(skb)[nhoff];
  212. resubmit_final:
  213. raw = raw6_local_deliver(skb, nexthdr);
  214. ipprot = rcu_dereference(inet6_protos[nexthdr]);
  215. if (ipprot) {
  216. int ret;
  217. if (have_final) {
  218. if (!(ipprot->flags & INET6_PROTO_FINAL)) {
  219. /* Once we've seen a final protocol don't
  220. * allow encapsulation on any non-final
  221. * ones. This allows foo in UDP encapsulation
  222. * to work.
  223. */
  224. goto discard;
  225. }
  226. } else if (ipprot->flags & INET6_PROTO_FINAL) {
  227. const struct ipv6hdr *hdr;
  228. /* Only do this once for first final protocol */
  229. have_final = true;
  230. /* Free reference early: we don't need it any more,
  231. and it may hold ip_conntrack module loaded
  232. indefinitely. */
  233. nf_reset(skb);
  234. skb_postpull_rcsum(skb, skb_network_header(skb),
  235. skb_network_header_len(skb));
  236. hdr = ipv6_hdr(skb);
  237. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  238. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  239. &hdr->saddr) &&
  240. !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
  241. goto discard;
  242. }
  243. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  244. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  245. goto discard;
  246. ret = ipprot->handler(skb);
  247. if (ret > 0) {
  248. if (ipprot->flags & INET6_PROTO_FINAL) {
  249. /* Not an extension header, most likely UDP
  250. * encapsulation. Use return value as nexthdr
  251. * protocol not nhoff (which presumably is
  252. * not set by handler).
  253. */
  254. nexthdr = ret;
  255. goto resubmit_final;
  256. } else {
  257. goto resubmit;
  258. }
  259. } else if (ret == 0) {
  260. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  261. }
  262. } else {
  263. if (!raw) {
  264. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  265. __IP6_INC_STATS(net, idev,
  266. IPSTATS_MIB_INUNKNOWNPROTOS);
  267. icmpv6_send(skb, ICMPV6_PARAMPROB,
  268. ICMPV6_UNK_NEXTHDR, nhoff);
  269. }
  270. kfree_skb(skb);
  271. } else {
  272. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  273. consume_skb(skb);
  274. }
  275. }
  276. rcu_read_unlock();
  277. return 0;
  278. discard:
  279. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  280. rcu_read_unlock();
  281. kfree_skb(skb);
  282. return 0;
  283. }
  284. int ip6_input(struct sk_buff *skb)
  285. {
  286. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
  287. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  288. ip6_input_finish);
  289. }
  290. EXPORT_SYMBOL_GPL(ip6_input);
  291. int ip6_mc_input(struct sk_buff *skb)
  292. {
  293. const struct ipv6hdr *hdr;
  294. bool deliver;
  295. __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
  296. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
  297. skb->len);
  298. hdr = ipv6_hdr(skb);
  299. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  300. #ifdef CONFIG_IPV6_MROUTE
  301. /*
  302. * IPv6 multicast router mode is now supported ;)
  303. */
  304. if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
  305. !(ipv6_addr_type(&hdr->daddr) &
  306. (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
  307. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  308. /*
  309. * Okay, we try to forward - split and duplicate
  310. * packets.
  311. */
  312. struct sk_buff *skb2;
  313. struct inet6_skb_parm *opt = IP6CB(skb);
  314. /* Check for MLD */
  315. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  316. /* Check if this is a mld message */
  317. u8 nexthdr = hdr->nexthdr;
  318. __be16 frag_off;
  319. int offset;
  320. /* Check if the value of Router Alert
  321. * is for MLD (0x0000).
  322. */
  323. if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
  324. deliver = false;
  325. if (!ipv6_ext_hdr(nexthdr)) {
  326. /* BUG */
  327. goto out;
  328. }
  329. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  330. &nexthdr, &frag_off);
  331. if (offset < 0)
  332. goto out;
  333. if (ipv6_is_mld(skb, nexthdr, offset))
  334. deliver = true;
  335. goto out;
  336. }
  337. /* unknown RA - process it normally */
  338. }
  339. if (deliver)
  340. skb2 = skb_clone(skb, GFP_ATOMIC);
  341. else {
  342. skb2 = skb;
  343. skb = NULL;
  344. }
  345. if (skb2) {
  346. ip6_mr_input(skb2);
  347. }
  348. }
  349. out:
  350. #endif
  351. if (likely(deliver))
  352. ip6_input(skb);
  353. else {
  354. /* discard */
  355. kfree_skb(skb);
  356. }
  357. return 0;
  358. }