ip6_input.c 8.7 KB

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