ip6_input.c 8.8 KB

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