ip6_input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
  165. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  166. goto err;
  167. hdr = ipv6_hdr(skb);
  168. if (hdr->version != 6) {
  169. SKB_DR_SET(reason, UNHANDLED_PROTO);
  170. goto err;
  171. }
  172. __IP6_ADD_STATS(net, idev,
  173. IPSTATS_MIB_NOECTPKTS +
  174. (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
  175. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  176. /*
  177. * RFC4291 2.5.3
  178. * The loopback address must not be used as the source address in IPv6
  179. * packets that are sent outside of a single node. [..]
  180. * A packet received on an interface with a destination address
  181. * of loopback must be dropped.
  182. */
  183. if ((ipv6_addr_loopback(&hdr->saddr) ||
  184. ipv6_addr_loopback(&hdr->daddr)) &&
  185. !(dev->flags & IFF_LOOPBACK) &&
  186. !netif_is_l3_master(dev))
  187. goto err;
  188. /* RFC4291 Errata ID: 3480
  189. * Interface-Local scope spans only a single interface on a
  190. * node and is useful only for loopback transmission of
  191. * multicast. Packets with interface-local scope received
  192. * from another node must be discarded.
  193. */
  194. if (!(skb->pkt_type == PACKET_LOOPBACK ||
  195. dev->flags & IFF_LOOPBACK) &&
  196. ipv6_addr_is_multicast(&hdr->daddr) &&
  197. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
  198. goto err;
  199. /* If enabled, drop unicast packets that were encapsulated in link-layer
  200. * multicast or broadcast to protected against the so-called "hole-196"
  201. * attack in 802.11 wireless.
  202. */
  203. if (!ipv6_addr_is_multicast(&hdr->daddr) &&
  204. (skb->pkt_type == PACKET_BROADCAST ||
  205. skb->pkt_type == PACKET_MULTICAST) &&
  206. READ_ONCE(idev->cnf.drop_unicast_in_l2_multicast)) {
  207. SKB_DR_SET(reason, UNICAST_IN_L2_MULTICAST);
  208. goto err;
  209. }
  210. /* RFC4291 2.7
  211. * Nodes must not originate a packet to a multicast address whose scope
  212. * field contains the reserved value 0; if such a packet is received, it
  213. * must be silently dropped.
  214. */
  215. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  216. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
  217. goto err;
  218. /*
  219. * RFC4291 2.7
  220. * Multicast addresses must not be used as source addresses in IPv6
  221. * packets or appear in any Routing header.
  222. */
  223. if (ipv6_addr_is_multicast(&hdr->saddr))
  224. goto err;
  225. skb->transport_header = skb->network_header + sizeof(*hdr);
  226. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  227. pkt_len = ntohs(hdr->payload_len);
  228. /* pkt_len may be zero if Jumbo payload option is present */
  229. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  230. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  231. __IP6_INC_STATS(net,
  232. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  233. SKB_DR_SET(reason, PKT_TOO_SMALL);
  234. goto drop;
  235. }
  236. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  237. goto err;
  238. hdr = ipv6_hdr(skb);
  239. }
  240. if (hdr->nexthdr == NEXTHDR_HOP) {
  241. if (ipv6_parse_hopopts(skb) < 0) {
  242. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  243. rcu_read_unlock();
  244. return NULL;
  245. }
  246. }
  247. rcu_read_unlock();
  248. /* Must drop socket now because of tproxy. */
  249. if (!skb_sk_is_prefetched(skb))
  250. skb_orphan(skb);
  251. return skb;
  252. err:
  253. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  254. SKB_DR_OR(reason, IP_INHDR);
  255. drop:
  256. rcu_read_unlock();
  257. kfree_skb_reason(skb, reason);
  258. return NULL;
  259. }
  260. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  261. {
  262. struct net *net = dev_net(skb->dev);
  263. skb = ip6_rcv_core(skb, dev, net);
  264. if (skb == NULL)
  265. return NET_RX_DROP;
  266. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  267. net, NULL, skb, dev, NULL,
  268. ip6_rcv_finish);
  269. }
  270. static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
  271. struct net *net)
  272. {
  273. NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
  274. head, dev, NULL, ip6_rcv_finish);
  275. ip6_list_rcv_finish(net, NULL, head);
  276. }
  277. /* Receive a list of IPv6 packets */
  278. void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
  279. struct net_device *orig_dev)
  280. {
  281. struct net_device *curr_dev = NULL;
  282. struct net *curr_net = NULL;
  283. struct sk_buff *skb, *next;
  284. LIST_HEAD(sublist);
  285. list_for_each_entry_safe(skb, next, head, list) {
  286. struct net_device *dev = skb->dev;
  287. struct net *net = dev_net(dev);
  288. skb_list_del_init(skb);
  289. skb = ip6_rcv_core(skb, dev, net);
  290. if (skb == NULL)
  291. continue;
  292. if (curr_dev != dev || curr_net != net) {
  293. /* dispatch old sublist */
  294. if (!list_empty(&sublist))
  295. ip6_sublist_rcv(&sublist, curr_dev, curr_net);
  296. /* start new sublist */
  297. INIT_LIST_HEAD(&sublist);
  298. curr_dev = dev;
  299. curr_net = net;
  300. }
  301. list_add_tail(&skb->list, &sublist);
  302. }
  303. /* dispatch final sublist */
  304. if (!list_empty(&sublist))
  305. ip6_sublist_rcv(&sublist, curr_dev, curr_net);
  306. }
  307. INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
  308. /*
  309. * Deliver the packet to the host
  310. */
  311. void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
  312. bool have_final)
  313. {
  314. const struct inet6_protocol *ipprot;
  315. struct inet6_dev *idev;
  316. unsigned int nhoff;
  317. SKB_DR(reason);
  318. bool raw;
  319. /*
  320. * Parse extension headers
  321. */
  322. resubmit:
  323. idev = ip6_dst_idev(skb_dst(skb));
  324. nhoff = IP6CB(skb)->nhoff;
  325. if (!have_final) {
  326. if (!pskb_pull(skb, skb_transport_offset(skb)))
  327. goto discard;
  328. nexthdr = skb_network_header(skb)[nhoff];
  329. }
  330. resubmit_final:
  331. raw = raw6_local_deliver(skb, nexthdr);
  332. ipprot = rcu_dereference(inet6_protos[nexthdr]);
  333. if (ipprot) {
  334. int ret;
  335. if (have_final) {
  336. if (!(ipprot->flags & INET6_PROTO_FINAL)) {
  337. /* Once we've seen a final protocol don't
  338. * allow encapsulation on any non-final
  339. * ones. This allows foo in UDP encapsulation
  340. * to work.
  341. */
  342. goto discard;
  343. }
  344. } else if (ipprot->flags & INET6_PROTO_FINAL) {
  345. const struct ipv6hdr *hdr;
  346. int sdif = inet6_sdif(skb);
  347. struct net_device *dev;
  348. /* Only do this once for first final protocol */
  349. have_final = true;
  350. skb_postpull_rcsum(skb, skb_network_header(skb),
  351. skb_network_header_len(skb));
  352. hdr = ipv6_hdr(skb);
  353. /* skb->dev passed may be master dev for vrfs. */
  354. if (sdif) {
  355. dev = dev_get_by_index_rcu(net, sdif);
  356. if (!dev)
  357. goto discard;
  358. } else {
  359. dev = skb->dev;
  360. }
  361. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  362. !ipv6_chk_mcast_addr(dev, &hdr->daddr,
  363. &hdr->saddr) &&
  364. !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) {
  365. SKB_DR_SET(reason, IP_INADDRERRORS);
  366. goto discard;
  367. }
  368. }
  369. if (!(ipprot->flags & INET6_PROTO_NOPOLICY)) {
  370. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  371. SKB_DR_SET(reason, XFRM_POLICY);
  372. goto discard;
  373. }
  374. nf_reset_ct(skb);
  375. }
  376. ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
  377. skb);
  378. if (ret > 0) {
  379. if (ipprot->flags & INET6_PROTO_FINAL) {
  380. /* Not an extension header, most likely UDP
  381. * encapsulation. Use return value as nexthdr
  382. * protocol not nhoff (which presumably is
  383. * not set by handler).
  384. */
  385. nexthdr = ret;
  386. goto resubmit_final;
  387. } else {
  388. goto resubmit;
  389. }
  390. } else if (ret == 0) {
  391. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  392. }
  393. } else {
  394. if (!raw) {
  395. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  396. __IP6_INC_STATS(net, idev,
  397. IPSTATS_MIB_INUNKNOWNPROTOS);
  398. icmpv6_send(skb, ICMPV6_PARAMPROB,
  399. ICMPV6_UNK_NEXTHDR, nhoff);
  400. SKB_DR_SET(reason, IP_NOPROTO);
  401. } else {
  402. SKB_DR_SET(reason, XFRM_POLICY);
  403. }
  404. kfree_skb_reason(skb, reason);
  405. } else {
  406. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
  407. consume_skb(skb);
  408. }
  409. }
  410. return;
  411. discard:
  412. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  413. kfree_skb_reason(skb, reason);
  414. }
  415. static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  416. {
  417. skb_clear_delivery_time(skb);
  418. rcu_read_lock();
  419. ip6_protocol_deliver_rcu(net, skb, 0, false);
  420. rcu_read_unlock();
  421. return 0;
  422. }
  423. int ip6_input(struct sk_buff *skb)
  424. {
  425. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
  426. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  427. ip6_input_finish);
  428. }
  429. EXPORT_SYMBOL_GPL(ip6_input);
  430. int ip6_mc_input(struct sk_buff *skb)
  431. {
  432. int sdif = inet6_sdif(skb);
  433. const struct ipv6hdr *hdr;
  434. struct net_device *dev;
  435. bool deliver;
  436. __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
  437. __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
  438. skb->len);
  439. /* skb->dev passed may be master dev for vrfs. */
  440. if (sdif) {
  441. rcu_read_lock();
  442. dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
  443. if (!dev) {
  444. rcu_read_unlock();
  445. kfree_skb(skb);
  446. return -ENODEV;
  447. }
  448. } else {
  449. dev = skb->dev;
  450. }
  451. hdr = ipv6_hdr(skb);
  452. deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
  453. if (sdif)
  454. rcu_read_unlock();
  455. #ifdef CONFIG_IPV6_MROUTE
  456. /*
  457. * IPv6 multicast router mode is now supported ;)
  458. */
  459. if (atomic_read(&dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding) &&
  460. !(ipv6_addr_type(&hdr->daddr) &
  461. (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
  462. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  463. /*
  464. * Okay, we try to forward - split and duplicate
  465. * packets.
  466. */
  467. struct sk_buff *skb2;
  468. struct inet6_skb_parm *opt = IP6CB(skb);
  469. /* Check for MLD */
  470. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  471. /* Check if this is a mld message */
  472. u8 nexthdr = hdr->nexthdr;
  473. __be16 frag_off;
  474. int offset;
  475. /* Check if the value of Router Alert
  476. * is for MLD (0x0000).
  477. */
  478. if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
  479. deliver = false;
  480. if (!ipv6_ext_hdr(nexthdr)) {
  481. /* BUG */
  482. goto out;
  483. }
  484. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  485. &nexthdr, &frag_off);
  486. if (offset < 0)
  487. goto out;
  488. if (ipv6_is_mld(skb, nexthdr, offset))
  489. deliver = true;
  490. goto out;
  491. }
  492. /* unknown RA - process it normally */
  493. }
  494. if (deliver)
  495. skb2 = skb_clone(skb, GFP_ATOMIC);
  496. else {
  497. skb2 = skb;
  498. skb = NULL;
  499. }
  500. if (skb2) {
  501. ip6_mr_input(skb2);
  502. }
  503. }
  504. out:
  505. #endif
  506. if (likely(deliver))
  507. ip6_input(skb);
  508. else {
  509. /* discard */
  510. kfree_skb(skb);
  511. }
  512. return 0;
  513. }