ip6_input.c 13 KB

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