ip6_input.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. skb->transport_header = skb->network_header + sizeof(*hdr);
  94. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  95. pkt_len = ntohs(hdr->payload_len);
  96. /* pkt_len may be zero if Jumbo payload option is present */
  97. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  98. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  99. IP6_INC_STATS_BH(net,
  100. idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  101. goto drop;
  102. }
  103. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  104. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  105. goto drop;
  106. }
  107. hdr = ipv6_hdr(skb);
  108. }
  109. if (hdr->nexthdr == NEXTHDR_HOP) {
  110. if (ipv6_parse_hopopts(skb) < 0) {
  111. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  112. rcu_read_unlock();
  113. return NET_RX_DROP;
  114. }
  115. }
  116. rcu_read_unlock();
  117. /* Must drop socket now because of tproxy. */
  118. skb_orphan(skb);
  119. return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  120. ip6_rcv_finish);
  121. err:
  122. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
  123. drop:
  124. rcu_read_unlock();
  125. kfree_skb(skb);
  126. return NET_RX_DROP;
  127. }
  128. /*
  129. * Deliver the packet to the host
  130. */
  131. static int ip6_input_finish(struct sk_buff *skb)
  132. {
  133. const struct inet6_protocol *ipprot;
  134. unsigned int nhoff;
  135. int nexthdr, raw;
  136. u8 hash;
  137. struct inet6_dev *idev;
  138. struct net *net = dev_net(skb_dst(skb)->dev);
  139. /*
  140. * Parse extension headers
  141. */
  142. rcu_read_lock();
  143. resubmit:
  144. idev = ip6_dst_idev(skb_dst(skb));
  145. if (!pskb_pull(skb, skb_transport_offset(skb)))
  146. goto discard;
  147. nhoff = IP6CB(skb)->nhoff;
  148. nexthdr = skb_network_header(skb)[nhoff];
  149. raw = raw6_local_deliver(skb, nexthdr);
  150. hash = nexthdr & (MAX_INET_PROTOS - 1);
  151. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  152. int ret;
  153. if (ipprot->flags & INET6_PROTO_FINAL) {
  154. const struct ipv6hdr *hdr;
  155. /* Free reference early: we don't need it any more,
  156. and it may hold ip_conntrack module loaded
  157. indefinitely. */
  158. nf_reset(skb);
  159. skb_postpull_rcsum(skb, skb_network_header(skb),
  160. skb_network_header_len(skb));
  161. hdr = ipv6_hdr(skb);
  162. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  163. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  164. &hdr->saddr) &&
  165. !ipv6_is_mld(skb, nexthdr))
  166. goto discard;
  167. }
  168. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  169. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  170. goto discard;
  171. ret = ipprot->handler(skb);
  172. if (ret > 0)
  173. goto resubmit;
  174. else if (ret == 0)
  175. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  176. } else {
  177. if (!raw) {
  178. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  179. IP6_INC_STATS_BH(net, idev,
  180. IPSTATS_MIB_INUNKNOWNPROTOS);
  181. icmpv6_send(skb, ICMPV6_PARAMPROB,
  182. ICMPV6_UNK_NEXTHDR, nhoff);
  183. }
  184. } else
  185. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDELIVERS);
  186. kfree_skb(skb);
  187. }
  188. rcu_read_unlock();
  189. return 0;
  190. discard:
  191. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INDISCARDS);
  192. rcu_read_unlock();
  193. kfree_skb(skb);
  194. return 0;
  195. }
  196. int ip6_input(struct sk_buff *skb)
  197. {
  198. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  199. ip6_input_finish);
  200. }
  201. int ip6_mc_input(struct sk_buff *skb)
  202. {
  203. const struct ipv6hdr *hdr;
  204. int deliver;
  205. IP6_UPD_PO_STATS_BH(dev_net(skb_dst(skb)->dev),
  206. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
  207. skb->len);
  208. hdr = ipv6_hdr(skb);
  209. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  210. #ifdef CONFIG_IPV6_MROUTE
  211. /*
  212. * IPv6 multicast router mode is now supported ;)
  213. */
  214. if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
  215. !(ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) &&
  216. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  217. /*
  218. * Okay, we try to forward - split and duplicate
  219. * packets.
  220. */
  221. struct sk_buff *skb2;
  222. struct inet6_skb_parm *opt = IP6CB(skb);
  223. /* Check for MLD */
  224. if (unlikely(opt->ra)) {
  225. /* Check if this is a mld message */
  226. u8 *ptr = skb_network_header(skb) + opt->ra;
  227. struct icmp6hdr *icmp6;
  228. u8 nexthdr = hdr->nexthdr;
  229. int offset;
  230. /* Check if the value of Router Alert
  231. * is for MLD (0x0000).
  232. */
  233. if ((ptr[2] | ptr[3]) == 0) {
  234. deliver = 0;
  235. if (!ipv6_ext_hdr(nexthdr)) {
  236. /* BUG */
  237. goto out;
  238. }
  239. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  240. &nexthdr);
  241. if (offset < 0)
  242. goto out;
  243. if (nexthdr != IPPROTO_ICMPV6)
  244. goto out;
  245. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  246. offset + 1 - skb->data)))
  247. goto out;
  248. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  249. switch (icmp6->icmp6_type) {
  250. case ICMPV6_MGM_QUERY:
  251. case ICMPV6_MGM_REPORT:
  252. case ICMPV6_MGM_REDUCTION:
  253. case ICMPV6_MLD2_REPORT:
  254. deliver = 1;
  255. break;
  256. }
  257. goto out;
  258. }
  259. /* unknown RA - process it normally */
  260. }
  261. if (deliver)
  262. skb2 = skb_clone(skb, GFP_ATOMIC);
  263. else {
  264. skb2 = skb;
  265. skb = NULL;
  266. }
  267. if (skb2) {
  268. ip6_mr_input(skb2);
  269. }
  270. }
  271. out:
  272. #endif
  273. if (likely(deliver))
  274. ip6_input(skb);
  275. else {
  276. /* discard */
  277. kfree_skb(skb);
  278. }
  279. return 0;
  280. }