ip6_input.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. inline int ip6_rcv_finish( struct sk_buff *skb)
  44. {
  45. if (skb_dst(skb) == NULL)
  46. ip6_route_input(skb);
  47. return dst_input(skb);
  48. }
  49. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  50. {
  51. const struct ipv6hdr *hdr;
  52. u32 pkt_len;
  53. struct inet6_dev *idev;
  54. struct net *net = dev_net(skb->dev);
  55. if (skb->pkt_type == PACKET_OTHERHOST) {
  56. kfree_skb(skb);
  57. return NET_RX_DROP;
  58. }
  59. rcu_read_lock();
  60. idev = __in6_dev_get(skb->dev);
  61. IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_IN, skb->len);
  62. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
  63. !idev || unlikely(idev->cnf.disable_ipv6)) {
  64. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDISCARDS);
  65. goto drop;
  66. }
  67. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  68. /*
  69. * Store incoming device index. When the packet will
  70. * be queued, we cannot refer to skb->dev anymore.
  71. *
  72. * BTW, when we send a packet for our own local address on a
  73. * non-loopback interface (e.g. ethX), it is being delivered
  74. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  75. * It, however, should be considered as if it is being
  76. * arrived via the sending interface (ethX), because of the
  77. * nature of scoping architecture. --yoshfuji
  78. */
  79. IP6CB(skb)->iif = skb_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
  80. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  81. goto err;
  82. hdr = ipv6_hdr(skb);
  83. if (hdr->version != 6)
  84. goto err;
  85. /*
  86. * RFC4291 2.5.3
  87. * A packet received on an interface with a destination address
  88. * of loopback must be dropped.
  89. */
  90. if (!(dev->flags & IFF_LOOPBACK) &&
  91. ipv6_addr_loopback(&hdr->daddr))
  92. goto err;
  93. /* RFC4291 Errata ID: 3480
  94. * Interface-Local scope spans only a single interface on a
  95. * node and is useful only for loopback transmission of
  96. * multicast. Packets with interface-local scope received
  97. * from another node must be discarded.
  98. */
  99. if (!(skb->pkt_type == PACKET_LOOPBACK ||
  100. dev->flags & IFF_LOOPBACK) &&
  101. ipv6_addr_is_multicast(&hdr->daddr) &&
  102. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
  103. goto err;
  104. /* If enabled, drop unicast packets that were encapsulated in link-layer
  105. * multicast or broadcast to protected against the so-called "hole-196"
  106. * attack in 802.11 wireless.
  107. */
  108. if (!ipv6_addr_is_multicast(&hdr->daddr) &&
  109. (skb->pkt_type == PACKET_BROADCAST ||
  110. skb->pkt_type == PACKET_MULTICAST) &&
  111. idev->cnf.drop_unicast_in_l2_multicast)
  112. goto err;
  113. /* RFC4291 2.7
  114. * Nodes must not originate a packet to a multicast address whose scope
  115. * field contains the reserved value 0; if such a packet is received, it
  116. * must be silently dropped.
  117. */
  118. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  119. IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
  120. goto err;
  121. /*
  122. * RFC4291 2.7
  123. * Multicast addresses must not be used as source addresses in IPv6
  124. * packets or appear in any Routing header.
  125. */
  126. if (ipv6_addr_is_multicast(&hdr->saddr))
  127. goto err;
  128. /* While RFC4291 is not explicit about v4mapped addresses
  129. * in IPv6 headers, it seems clear linux dual-stack
  130. * model can not deal properly with these.
  131. * Security models could be fooled by ::ffff:127.0.0.1 for example.
  132. *
  133. * https://tools.ietf.org/html/draft-itojun-v6ops-v4mapped-harmful-02
  134. */
  135. if (ipv6_addr_v4mapped(&hdr->saddr))
  136. goto err;
  137. skb->transport_header = skb->network_header + sizeof(*hdr);
  138. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  139. pkt_len = ntohs(hdr->payload_len);
  140. /* pkt_len may be zero if Jumbo payload option is present */
  141. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  142. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  143. IP6_INC_STATS_BH(net,
  144. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  145. goto drop;
  146. }
  147. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  148. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  149. goto drop;
  150. }
  151. hdr = ipv6_hdr(skb);
  152. }
  153. if (hdr->nexthdr == NEXTHDR_HOP) {
  154. if (ipv6_parse_hopopts(skb) < 0) {
  155. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  156. rcu_read_unlock();
  157. return NET_RX_DROP;
  158. }
  159. }
  160. rcu_read_unlock();
  161. /* Must drop socket now because of tproxy. */
  162. skb_orphan(skb);
  163. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  164. ip6_rcv_finish);
  165. err:
  166. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  167. drop:
  168. rcu_read_unlock();
  169. kfree_skb(skb);
  170. return NET_RX_DROP;
  171. }
  172. /*
  173. * Deliver the packet to the host
  174. */
  175. static int ip6_input_finish(struct sk_buff *skb)
  176. {
  177. const struct inet6_protocol *ipprot;
  178. unsigned int nhoff;
  179. int nexthdr, raw;
  180. u8 hash;
  181. struct inet6_dev *idev;
  182. struct net *net = dev_net(skb_dst(skb)->dev);
  183. /*
  184. * Parse extension headers
  185. */
  186. rcu_read_lock();
  187. resubmit:
  188. idev = ip6_dst_idev(skb_dst(skb));
  189. if (!pskb_pull(skb, skb_transport_offset(skb)))
  190. goto discard;
  191. nhoff = IP6CB(skb)->nhoff;
  192. nexthdr = skb_network_header(skb)[nhoff];
  193. raw = raw6_local_deliver(skb, nexthdr);
  194. hash = nexthdr & (MAX_INET_PROTOS - 1);
  195. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  196. int ret;
  197. if (ipprot->flags & INET6_PROTO_FINAL) {
  198. const struct ipv6hdr *hdr;
  199. /* Free reference early: we don't need it any more,
  200. and it may hold ip_conntrack module loaded
  201. indefinitely. */
  202. nf_reset(skb);
  203. skb_postpull_rcsum(skb, skb_network_header(skb),
  204. skb_network_header_len(skb));
  205. hdr = ipv6_hdr(skb);
  206. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  207. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  208. &hdr->saddr) &&
  209. !ipv6_is_mld(skb, nexthdr))
  210. goto discard;
  211. }
  212. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  213. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  214. goto discard;
  215. ret = ipprot->handler(skb);
  216. if (ret > 0)
  217. goto resubmit;
  218. else if (ret == 0)
  219. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  220. } else {
  221. if (!raw) {
  222. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  223. IP6_INC_STATS_BH(net, idev,
  224. IPSTATS_MIB_INUNKNOWNPROTOS);
  225. icmpv6_send(skb, ICMPV6_PARAMPROB,
  226. ICMPV6_UNK_NEXTHDR, nhoff);
  227. }
  228. } else
  229. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  230. kfree_skb(skb);
  231. }
  232. rcu_read_unlock();
  233. return 0;
  234. discard:
  235. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDISCARDS);
  236. rcu_read_unlock();
  237. kfree_skb(skb);
  238. return 0;
  239. }
  240. int ip6_input(struct sk_buff *skb)
  241. {
  242. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  243. ip6_input_finish);
  244. }
  245. int ip6_mc_input(struct sk_buff *skb)
  246. {
  247. const struct ipv6hdr *hdr;
  248. int deliver;
  249. IP6_UPD_PO_STATS_BH(dev_net(skb_dst(skb)->dev),
  250. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
  251. skb->len);
  252. hdr = ipv6_hdr(skb);
  253. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  254. #ifdef CONFIG_IPV6_MROUTE
  255. /*
  256. * IPv6 multicast router mode is now supported ;)
  257. */
  258. if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
  259. !(ipv6_addr_type(&hdr->daddr) &
  260. (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
  261. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  262. /*
  263. * Okay, we try to forward - split and duplicate
  264. * packets.
  265. */
  266. struct sk_buff *skb2;
  267. struct inet6_skb_parm *opt = IP6CB(skb);
  268. /* Check for MLD */
  269. if (unlikely(opt->ra)) {
  270. /* Check if this is a mld message */
  271. u8 *ptr = skb_network_header(skb) + opt->ra;
  272. struct icmp6hdr *icmp6;
  273. u8 nexthdr = hdr->nexthdr;
  274. __be16 frag_off;
  275. int offset;
  276. /* Check if the value of Router Alert
  277. * is for MLD (0x0000).
  278. */
  279. if ((ptr[2] | ptr[3]) == 0) {
  280. deliver = 0;
  281. if (!ipv6_ext_hdr(nexthdr)) {
  282. /* BUG */
  283. goto out;
  284. }
  285. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  286. &nexthdr, &frag_off);
  287. if (offset < 0)
  288. goto out;
  289. if (nexthdr != IPPROTO_ICMPV6)
  290. goto out;
  291. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  292. offset + 1 - skb->data)))
  293. goto out;
  294. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  295. switch (icmp6->icmp6_type) {
  296. case ICMPV6_MGM_QUERY:
  297. case ICMPV6_MGM_REPORT:
  298. case ICMPV6_MGM_REDUCTION:
  299. case ICMPV6_MLD2_REPORT:
  300. deliver = 1;
  301. break;
  302. }
  303. goto out;
  304. }
  305. /* unknown RA - process it normally */
  306. }
  307. if (deliver)
  308. skb2 = skb_clone(skb, GFP_ATOMIC);
  309. else {
  310. skb2 = skb;
  311. skb = NULL;
  312. }
  313. if (skb2) {
  314. ip6_mr_input(skb2);
  315. }
  316. }
  317. out:
  318. #endif
  319. if (likely(deliver))
  320. ip6_input(skb);
  321. else {
  322. /* discard */
  323. kfree_skb(skb);
  324. }
  325. return 0;
  326. }