ip6_input.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/netfilter.h>
  31. #include <linux/netfilter_ipv6.h>
  32. #include <net/sock.h>
  33. #include <net/snmp.h>
  34. #include <net/ipv6.h>
  35. #include <net/protocol.h>
  36. #include <net/transp_v6.h>
  37. #include <net/rawv6.h>
  38. #include <net/ndisc.h>
  39. #include <net/ip6_route.h>
  40. #include <net/addrconf.h>
  41. #include <net/xfrm.h>
  42. inline int ip6_rcv_finish( struct sk_buff *skb)
  43. {
  44. if (skb->dst == NULL)
  45. ip6_route_input(skb);
  46. return dst_input(skb);
  47. }
  48. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  49. {
  50. struct ipv6hdr *hdr;
  51. u32 pkt_len;
  52. struct inet6_dev *idev;
  53. if (skb->pkt_type == PACKET_OTHERHOST) {
  54. kfree_skb(skb);
  55. return 0;
  56. }
  57. rcu_read_lock();
  58. idev = __in6_dev_get(skb->dev);
  59. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INRECEIVES);
  60. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  61. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  62. rcu_read_unlock();
  63. goto out;
  64. }
  65. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  66. /*
  67. * Store incoming device index. When the packet will
  68. * be queued, we cannot refer to skb->dev anymore.
  69. *
  70. * BTW, when we send a packet for our own local address on a
  71. * non-loopback interface (e.g. ethX), it is being delivered
  72. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  73. * It, however, should be considered as if it is being
  74. * arrived via the sending interface (ethX), because of the
  75. * nature of scoping architecture. --yoshfuji
  76. */
  77. IP6CB(skb)->iif = skb->dst ? ip6_dst_idev(skb->dst)->dev->ifindex : dev->ifindex;
  78. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  79. goto err;
  80. hdr = ipv6_hdr(skb);
  81. if (hdr->version != 6)
  82. goto err;
  83. /*
  84. * RFC4291 2.5.3
  85. * A packet received on an interface with a destination address
  86. * of loopback must be dropped.
  87. */
  88. if (!(dev->flags & IFF_LOOPBACK) &&
  89. ipv6_addr_loopback(&hdr->daddr))
  90. goto err;
  91. skb->transport_header = skb->network_header + sizeof(*hdr);
  92. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  93. pkt_len = ntohs(hdr->payload_len);
  94. /* pkt_len may be zero if Jumbo payload option is present */
  95. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  96. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  97. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  98. goto drop;
  99. }
  100. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  101. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  102. goto drop;
  103. }
  104. hdr = ipv6_hdr(skb);
  105. }
  106. if (hdr->nexthdr == NEXTHDR_HOP) {
  107. if (ipv6_parse_hopopts(skb) < 0) {
  108. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  109. rcu_read_unlock();
  110. return 0;
  111. }
  112. }
  113. rcu_read_unlock();
  114. return NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  115. ip6_rcv_finish);
  116. err:
  117. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  118. drop:
  119. rcu_read_unlock();
  120. kfree_skb(skb);
  121. out:
  122. return 0;
  123. }
  124. /*
  125. * Deliver the packet to the host
  126. */
  127. static int ip6_input_finish(struct sk_buff *skb)
  128. {
  129. struct inet6_protocol *ipprot;
  130. unsigned int nhoff;
  131. int nexthdr, raw;
  132. u8 hash;
  133. struct inet6_dev *idev;
  134. /*
  135. * Parse extension headers
  136. */
  137. rcu_read_lock();
  138. resubmit:
  139. idev = ip6_dst_idev(skb->dst);
  140. if (!pskb_pull(skb, skb_transport_offset(skb)))
  141. goto discard;
  142. nhoff = IP6CB(skb)->nhoff;
  143. nexthdr = skb_network_header(skb)[nhoff];
  144. raw = raw6_local_deliver(skb, nexthdr);
  145. hash = nexthdr & (MAX_INET_PROTOS - 1);
  146. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  147. int ret;
  148. if (ipprot->flags & INET6_PROTO_FINAL) {
  149. struct ipv6hdr *hdr;
  150. /* Free reference early: we don't need it any more,
  151. and it may hold ip_conntrack module loaded
  152. indefinitely. */
  153. nf_reset(skb);
  154. skb_postpull_rcsum(skb, skb_network_header(skb),
  155. skb_network_header_len(skb));
  156. hdr = ipv6_hdr(skb);
  157. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  158. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  159. &hdr->saddr) &&
  160. !ipv6_is_mld(skb, nexthdr))
  161. goto discard;
  162. }
  163. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  164. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  165. goto discard;
  166. ret = ipprot->handler(skb);
  167. if (ret > 0)
  168. goto resubmit;
  169. else if (ret == 0)
  170. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  171. } else {
  172. if (!raw) {
  173. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  174. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  175. icmpv6_send(skb, ICMPV6_PARAMPROB,
  176. ICMPV6_UNK_NEXTHDR, nhoff,
  177. skb->dev);
  178. }
  179. } else
  180. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  181. kfree_skb(skb);
  182. }
  183. rcu_read_unlock();
  184. return 0;
  185. discard:
  186. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  187. rcu_read_unlock();
  188. kfree_skb(skb);
  189. return 0;
  190. }
  191. int ip6_input(struct sk_buff *skb)
  192. {
  193. return NF_HOOK(PF_INET6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  194. ip6_input_finish);
  195. }
  196. int ip6_mc_input(struct sk_buff *skb)
  197. {
  198. struct ipv6hdr *hdr;
  199. int deliver;
  200. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  201. hdr = ipv6_hdr(skb);
  202. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  203. #ifdef CONFIG_IPV6_MROUTE
  204. /*
  205. * IPv6 multicast router mode is now supported ;)
  206. */
  207. if (ipv6_devconf.mc_forwarding &&
  208. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  209. /*
  210. * Okay, we try to forward - split and duplicate
  211. * packets.
  212. */
  213. struct sk_buff *skb2;
  214. struct inet6_skb_parm *opt = IP6CB(skb);
  215. /* Check for MLD */
  216. if (unlikely(opt->ra)) {
  217. /* Check if this is a mld message */
  218. u8 *ptr = skb_network_header(skb) + opt->ra;
  219. struct icmp6hdr *icmp6;
  220. u8 nexthdr = hdr->nexthdr;
  221. int offset;
  222. /* Check if the value of Router Alert
  223. * is for MLD (0x0000).
  224. */
  225. if ((ptr[2] | ptr[3]) == 0) {
  226. deliver = 0;
  227. if (!ipv6_ext_hdr(nexthdr)) {
  228. /* BUG */
  229. goto out;
  230. }
  231. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  232. &nexthdr);
  233. if (offset < 0)
  234. goto out;
  235. if (nexthdr != IPPROTO_ICMPV6)
  236. goto out;
  237. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  238. offset + 1 - skb->data)))
  239. goto out;
  240. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  241. switch (icmp6->icmp6_type) {
  242. case ICMPV6_MGM_QUERY:
  243. case ICMPV6_MGM_REPORT:
  244. case ICMPV6_MGM_REDUCTION:
  245. case ICMPV6_MLD2_REPORT:
  246. deliver = 1;
  247. break;
  248. }
  249. goto out;
  250. }
  251. /* unknown RA - process it normally */
  252. }
  253. if (deliver)
  254. skb2 = skb_clone(skb, GFP_ATOMIC);
  255. else {
  256. skb2 = skb;
  257. skb = NULL;
  258. }
  259. if (skb2) {
  260. skb2->dev = skb2->dst->dev;
  261. ip6_mr_input(skb2);
  262. }
  263. }
  264. out:
  265. #endif
  266. if (likely(deliver))
  267. ip6_input(skb);
  268. else {
  269. /* discard */
  270. kfree_skb(skb);
  271. }
  272. return 0;
  273. }