ip6_input.c 10 KB

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