ip6_input.c 13 KB

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