ip6_input.c 13 KB

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