ip6_input.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 input
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Ian P. Morris <I.P.Morris@soton.ac.uk>
  9. *
  10. * Based in linux/net/ipv4/ip_input.c
  11. */
  12. /* Changes
  13. *
  14. * Mitsuru KANDA @USAGI and
  15. * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/types.h>
  19. #include <linux/socket.h>
  20. #include <linux/sockios.h>
  21. #include <linux/net.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/in6.h>
  24. #include <linux/icmpv6.h>
  25. #include <linux/mroute6.h>
  26. #include <linux/slab.h>
  27. #include <linux/indirect_call_wrapper.h>
  28. #include <linux/netfilter.h>
  29. #include <linux/netfilter_ipv6.h>
  30. #include <net/sock.h>
  31. #include <net/snmp.h>
  32. #include <net/udp.h>
  33. #include <net/ipv6.h>
  34. #include <net/protocol.h>
  35. #include <net/transp_v6.h>
  36. #include <net/rawv6.h>
  37. #include <net/ndisc.h>
  38. #include <net/ip6_route.h>
  39. #include <net/addrconf.h>
  40. #include <net/xfrm.h>
  41. #include <net/inet_ecn.h>
  42. #include <net/dst_metadata.h>
  43. static void ip6_rcv_finish_core(struct net *net, struct sock *sk,
  44. struct sk_buff *skb)
  45. {
  46. if (READ_ONCE(net->ipv4.sysctl_ip_early_demux) &&
  47. !skb_dst(skb) && !skb->sk) {
  48. switch (ipv6_hdr(skb)->nexthdr) {
  49. case IPPROTO_TCP:
  50. if (READ_ONCE(net->ipv4.sysctl_tcp_early_demux))
  51. tcp_v6_early_demux(skb);
  52. break;
  53. case IPPROTO_UDP:
  54. if (READ_ONCE(net->ipv4.sysctl_udp_early_demux))
  55. udp_v6_early_demux(skb);
  56. break;
  57. }
  58. }
  59. if (!skb_valid_dst(skb))
  60. ip6_route_input(skb);
  61. }
  62. int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  63. {
  64. /* if ingress device is enslaved to an L3 master device pass the
  65. * skb to its handler for processing
  66. */
  67. skb = l3mdev_ip6_rcv(skb);
  68. if (!skb)
  69. return NET_RX_SUCCESS;
  70. ip6_rcv_finish_core(net, sk, skb);
  71. return dst_input(skb);
  72. }
  73. static void ip6_sublist_rcv_finish(struct list_head *head)
  74. {
  75. struct sk_buff *skb, *next;
  76. list_for_each_entry_safe(skb, next, head, list) {
  77. skb_list_del_init(skb);
  78. dst_input(skb);
  79. }
  80. }
  81. static bool ip6_can_use_hint(const struct sk_buff *skb,
  82. const struct sk_buff *hint)
  83. {
  84. return hint && !skb_dst(skb) &&
  85. ipv6_addr_equal(&ipv6_hdr(hint)->daddr, &ipv6_hdr(skb)->daddr);
  86. }
  87. static struct sk_buff *ip6_extract_route_hint(const struct net *net,
  88. struct sk_buff *skb)
  89. {
  90. if (fib6_routes_require_src(net) || fib6_has_custom_rules(net) ||
  91. IP6CB(skb)->flags & IP6SKB_MULTIPATH)
  92. return NULL;
  93. return skb;
  94. }
  95. static void ip6_list_rcv_finish(struct net *net, struct sock *sk,
  96. struct list_head *head)
  97. {
  98. struct sk_buff *skb, *next, *hint = NULL;
  99. struct dst_entry *curr_dst = NULL;
  100. LIST_HEAD(sublist);
  101. list_for_each_entry_safe(skb, next, head, list) {
  102. struct dst_entry *dst;
  103. skb_list_del_init(skb);
  104. /* if ingress device is enslaved to an L3 master device pass the
  105. * skb to its handler for processing
  106. */
  107. skb = l3mdev_ip6_rcv(skb);
  108. if (!skb)
  109. continue;
  110. if (ip6_can_use_hint(skb, hint))
  111. skb_dst_copy(skb, hint);
  112. else
  113. ip6_rcv_finish_core(net, sk, skb);
  114. dst = skb_dst(skb);
  115. if (curr_dst != dst) {
  116. hint = ip6_extract_route_hint(net, skb);
  117. /* dispatch old sublist */
  118. if (!list_empty(&sublist))
  119. ip6_sublist_rcv_finish(&sublist);
  120. /* start new sublist */
  121. INIT_LIST_HEAD(&sublist);
  122. curr_dst = dst;
  123. }
  124. list_add_tail(&skb->list, &sublist);
  125. }
  126. /* dispatch final sublist */
  127. ip6_sublist_rcv_finish(&sublist);
  128. }
  129. static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
  130. struct net *net)
  131. {
  132. enum skb_drop_reason reason;
  133. const struct ipv6hdr *hdr;
  134. u32 pkt_len;
  135. struct inet6_dev *idev;
  136. if (skb->pkt_type == PACKET_OTHERHOST) {
  137. dev_core_stats_rx_otherhost_dropped_inc(skb->dev);
  138. kfree_skb_reason(skb, SKB_DROP_REASON_OTHERHOST);
  139. return NULL;
  140. }
  141. rcu_read_lock();
  142. idev = __in6_dev_get(skb->dev);
  143. __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
  144. SKB_DR_SET(reason, NOT_SPECIFIED);
  145. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
  146. !idev || unlikely(READ_ONCE(idev->cnf.disable_ipv6))) {
  147. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  148. if (idev && unlikely(READ_ONCE(idev->cnf.disable_ipv6)))
  149. SKB_DR_SET(reason, IPV6DISABLED);
  150. goto drop;
  151. }
  152. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  153. /*
  154. * Store incoming device index. When the packet will
  155. * be queued, we cannot refer to skb->dev anymore.
  156. *
  157. * BTW, when we send a packet for our own local address on a
  158. * non-loopback interface (e.g. ethX), it is being delivered
  159. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  160. * It, however, should be considered as if it is being
  161. * arrived via the sending interface (ethX), because of the
  162. * nature of scoping architecture. --yoshfuji
  163. */
  164. IP6CB(skb)->iif = skb_valid_dst(skb) ?
  165. ip6_dst_idev(skb_dst(skb))->dev->ifindex :
  166. dev->ifindex;
  167. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  168. goto err;
  169. hdr = ipv6_hdr(skb);
  170. if (hdr->version != 6) {
  171. SKB_DR_SET(reason, UNHANDLED_PROTO);
  172. goto err;
  173. }
  174. __IP6_ADD_STATS(net, idev,
  175. IPSTATS_MIB_NOECTPKTS +
  176. (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
  177. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  178. /*
  179. * RFC4291 2.5.3
  180. * The loopback address must not be used as the source address in IPv6
  181. * packets that are sent outside of a single node. [..]
  182. * A packet received on an interface with a destination address
  183. * of loopback must be dropped.
  184. */
  185. if ((ipv6_addr_loopback(&hdr->saddr) ||
  186. ipv6_addr_loopback(&hdr->daddr)) &&
  187. !(dev->flags & IFF_LOOPBACK) &&
  188. !netif_is_l3_master(dev))
  189. goto err;
  190. /* RFC4291 Errata ID: 3480
  191. * Interface-Local scope spans only a single interface on a
  192. * node and is useful only for loopback transmission of
  193. * multicast. Packets with interface-local scope received
  194. * from another node must be discarded.
  195. */
  196. if (!(skb->pkt_type == PACKET_LOOPBACK ||
  197. dev->flags & IFF_LOOPBACK) &&
  198. ipv6_addr_is_multicast(&hdr->daddr) &&
  199. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
  200. goto err;
  201. /* If enabled, drop unicast packets that were encapsulated in link-layer
  202. * multicast or broadcast to protected against the so-called "hole-196"
  203. * attack in 802.11 wireless.
  204. */
  205. if (!ipv6_addr_is_multicast(&hdr->daddr) &&
  206. (skb->pkt_type == PACKET_BROADCAST ||
  207. skb->pkt_type == PACKET_MULTICAST) &&
  208. READ_ONCE(idev->cnf.drop_unicast_in_l2_multicast)) {
  209. SKB_DR_SET(reason, UNICAST_IN_L2_MULTICAST);
  210. goto err;
  211. }
  212. /* RFC4291 2.7
  213. * Nodes must not originate a packet to a multicast address whose scope
  214. * field contains the reserved value 0; if such a packet is received, it
  215. * must be silently dropped.
  216. */
  217. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  218. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
  219. goto err;
  220. /*
  221. * RFC4291 2.7
  222. * Multicast addresses must not be used as source addresses in IPv6
  223. * packets or appear in any Routing header.
  224. */
  225. if (ipv6_addr_is_multicast(&hdr->saddr))
  226. goto err;
  227. skb->transport_header = skb->network_header + sizeof(*hdr);
  228. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  229. pkt_len = ipv6_payload_len(skb, hdr);
  230. /* pkt_len may be zero if Jumbo payload option is present */
  231. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  232. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  233. __IP6_INC_STATS(net,
  234. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  235. SKB_DR_SET(reason, PKT_TOO_SMALL);
  236. goto drop;
  237. }
  238. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  239. goto err;
  240. hdr = ipv6_hdr(skb);
  241. }
  242. if (hdr->nexthdr == NEXTHDR_HOP) {
  243. if (ipv6_parse_hopopts(skb) < 0) {
  244. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  245. rcu_read_unlock();
  246. return NULL;
  247. }
  248. }
  249. rcu_read_unlock();
  250. /* Must drop socket now because of tproxy. */
  251. if (!skb_sk_is_prefetched(skb))
  252. skb_orphan(skb);
  253. return skb;
  254. err:
  255. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  256. SKB_DR_OR(reason, IP_INHDR);
  257. drop:
  258. rcu_read_unlock();
  259. kfree_skb_reason(skb, reason);
  260. return NULL;
  261. }
  262. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  263. {
  264. struct net *net = dev_net(skb->dev);
  265. skb = ip6_rcv_core(skb, dev, net);
  266. if (skb == NULL)
  267. return NET_RX_DROP;
  268. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  269. net, NULL, skb, dev, NULL,
  270. ip6_rcv_finish);
  271. }
  272. static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
  273. struct net *net)
  274. {
  275. NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
  276. head, dev, NULL, ip6_rcv_finish);
  277. ip6_list_rcv_finish(net, NULL, head);
  278. }
  279. /* Receive a list of IPv6 packets */
  280. void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
  281. struct net_device *orig_dev)
  282. {
  283. struct net_device *curr_dev = NULL;
  284. struct net *curr_net = NULL;
  285. struct sk_buff *skb, *next;
  286. LIST_HEAD(sublist);
  287. list_for_each_entry_safe(skb, next, head, list) {
  288. struct net_device *dev = skb->dev;
  289. struct net *net = dev_net(dev);
  290. skb_list_del_init(skb);
  291. skb = ip6_rcv_core(skb, dev, net);
  292. if (skb == NULL)
  293. continue;
  294. if (curr_dev != dev || curr_net != net) {
  295. /* dispatch old sublist */
  296. if (!list_empty(&sublist))
  297. ip6_sublist_rcv(&sublist, curr_dev, curr_net);
  298. /* start new sublist */
  299. INIT_LIST_HEAD(&sublist);
  300. curr_dev = dev;
  301. curr_net = net;
  302. }
  303. list_add_tail(&skb->list, &sublist);
  304. }
  305. /* dispatch final sublist */
  306. if (!list_empty(&sublist))
  307. ip6_sublist_rcv(&sublist, curr_dev, curr_net);
  308. }
  309. INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
  310. /*
  311. * Deliver the packet to the host
  312. */
  313. void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
  314. bool have_final)
  315. {
  316. const struct inet6_protocol *ipprot;
  317. struct inet6_dev *idev;
  318. unsigned int nhoff;
  319. SKB_DR(reason);
  320. bool raw;
  321. /*
  322. * Parse extension headers
  323. */
  324. resubmit:
  325. idev = ip6_dst_idev(skb_dst(skb));
  326. nhoff = IP6CB(skb)->nhoff;
  327. if (!have_final) {
  328. if (!pskb_pull(skb, skb_transport_offset(skb)))
  329. goto discard;
  330. nexthdr = skb_network_header(skb)[nhoff];
  331. }
  332. resubmit_final:
  333. raw = raw6_local_deliver(skb, nexthdr);
  334. ipprot = rcu_dereference(inet6_protos[nexthdr]);
  335. if (ipprot) {
  336. int ret;
  337. if (have_final) {
  338. if (!(ipprot->flags & INET6_PROTO_FINAL)) {
  339. /* Once we've seen a final protocol don't
  340. * allow encapsulation on any non-final
  341. * ones. This allows foo in UDP encapsulation
  342. * to work.
  343. */
  344. goto discard;
  345. }
  346. } else if (ipprot->flags & INET6_PROTO_FINAL) {
  347. const struct ipv6hdr *hdr;
  348. int sdif = inet6_sdif(skb);
  349. struct net_device *dev;
  350. /* Only do this once for first final protocol */
  351. have_final = true;
  352. skb_postpull_rcsum(skb, skb_network_header(skb),
  353. skb_network_header_len(skb));
  354. hdr = ipv6_hdr(skb);
  355. /* skb->dev passed may be master dev for vrfs. */
  356. if (sdif) {
  357. dev = dev_get_by_index_rcu(net, sdif);
  358. if (!dev)
  359. goto discard;
  360. } else {
  361. dev = skb->dev;
  362. }
  363. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  364. !ipv6_chk_mcast_addr(dev, &hdr->daddr,
  365. &hdr->saddr) &&
  366. !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) {
  367. SKB_DR_SET(reason, IP_INADDRERRORS);
  368. goto discard;
  369. }
  370. }
  371. if (!(ipprot->flags & INET6_PROTO_NOPOLICY)) {
  372. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  373. SKB_DR_SET(reason, XFRM_POLICY);
  374. goto discard;
  375. }
  376. nf_reset_ct(skb);
  377. }
  378. ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
  379. skb);
  380. if (ret > 0) {
  381. if (ipprot->flags & INET6_PROTO_FINAL) {
  382. /* Not an extension header, most likely UDP
  383. * encapsulation. Use return value as nexthdr
  384. * protocol not nhoff (which presumably is
  385. * not set by handler).
  386. */
  387. nexthdr = ret;
  388. goto resubmit_final;
  389. } else {
  390. goto resubmit;
  391. }
  392. } else if (ret == 0) {
  393. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  394. }
  395. } else {
  396. if (!raw) {
  397. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  398. __IP6_INC_STATS(net, idev,
  399. IPSTATS_MIB_INUNKNOWNPROTOS);
  400. icmpv6_send(skb, ICMPV6_PARAMPROB,
  401. ICMPV6_UNK_NEXTHDR, nhoff);
  402. SKB_DR_SET(reason, IP_NOPROTO);
  403. } else {
  404. SKB_DR_SET(reason, XFRM_POLICY);
  405. }
  406. kfree_skb_reason(skb, reason);
  407. } else {
  408. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  409. consume_skb(skb);
  410. }
  411. }
  412. return;
  413. discard:
  414. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  415. kfree_skb_reason(skb, reason);
  416. }
  417. static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  418. {
  419. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) {
  420. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  421. IPSTATS_MIB_INDISCARDS);
  422. kfree_skb_reason(skb, SKB_DROP_REASON_NOMEM);
  423. return 0;
  424. }
  425. skb_clear_delivery_time(skb);
  426. ip6_protocol_deliver_rcu(net, skb, 0, false);
  427. return 0;
  428. }
  429. int ip6_input(struct sk_buff *skb)
  430. {
  431. int res;
  432. rcu_read_lock();
  433. res = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
  434. dev_net_rcu(skb->dev), NULL, skb, skb->dev, NULL,
  435. ip6_input_finish);
  436. rcu_read_unlock();
  437. return res;
  438. }
  439. EXPORT_SYMBOL_GPL(ip6_input);
  440. int ip6_mc_input(struct sk_buff *skb)
  441. {
  442. struct net_device *dev = skb->dev;
  443. int sdif = inet6_sdif(skb);
  444. const struct ipv6hdr *hdr;
  445. bool deliver;
  446. __IP6_UPD_PO_STATS(skb_dst_dev_net_rcu(skb),
  447. __in6_dev_get_safely(dev), IPSTATS_MIB_INMCAST,
  448. skb->len);
  449. /* skb->dev passed may be master dev for vrfs. */
  450. if (sdif) {
  451. dev = dev_get_by_index_rcu(dev_net_rcu(dev), sdif);
  452. if (!dev) {
  453. kfree_skb(skb);
  454. return -ENODEV;
  455. }
  456. }
  457. hdr = ipv6_hdr(skb);
  458. deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
  459. #ifdef CONFIG_IPV6_MROUTE
  460. /*
  461. * IPv6 multicast router mode is now supported ;)
  462. */
  463. if (atomic_read(&dev_net_rcu(skb->dev)->ipv6.devconf_all->mc_forwarding) &&
  464. !(ipv6_addr_type(&hdr->daddr) &
  465. (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
  466. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  467. /*
  468. * Okay, we try to forward - split and duplicate
  469. * packets.
  470. */
  471. struct sk_buff *skb2;
  472. struct inet6_skb_parm *opt = IP6CB(skb);
  473. /* Check for MLD */
  474. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  475. /* Check if this is a mld message */
  476. u8 nexthdr = hdr->nexthdr;
  477. __be16 frag_off;
  478. int offset;
  479. /* Check if the value of Router Alert
  480. * is for MLD (0x0000).
  481. */
  482. if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
  483. deliver = false;
  484. if (!ipv6_ext_hdr(nexthdr)) {
  485. /* BUG */
  486. goto out;
  487. }
  488. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  489. &nexthdr, &frag_off);
  490. if (offset < 0)
  491. goto out;
  492. if (ipv6_is_mld(skb, nexthdr, offset))
  493. deliver = true;
  494. goto out;
  495. }
  496. /* unknown RA - process it normally */
  497. }
  498. if (deliver) {
  499. skb2 = skb_clone(skb, GFP_ATOMIC);
  500. } else {
  501. skb2 = skb;
  502. skb = NULL;
  503. }
  504. if (skb2)
  505. ip6_mr_input(skb2);
  506. }
  507. out:
  508. #endif
  509. if (likely(deliver)) {
  510. ip6_input(skb);
  511. } else {
  512. /* discard */
  513. kfree_skb(skb);
  514. }
  515. return 0;
  516. }