ip6_input.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * $Id: ip6_input.c,v 1.19 2000/12/13 18:31:50 davem Exp $
  10. *
  11. * Based in linux/net/ipv4/ip_input.c
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. /* Changes
  19. *
  20. * Mitsuru KANDA @USAGI and
  21. * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/in6.h>
  30. #include <linux/icmpv6.h>
  31. #include <linux/mroute6.h>
  32. #include <linux/netfilter.h>
  33. #include <linux/netfilter_ipv6.h>
  34. #include <net/sock.h>
  35. #include <net/snmp.h>
  36. #include <net/ipv6.h>
  37. #include <net/protocol.h>
  38. #include <net/transp_v6.h>
  39. #include <net/rawv6.h>
  40. #include <net/ndisc.h>
  41. #include <net/ip6_route.h>
  42. #include <net/addrconf.h>
  43. #include <net/xfrm.h>
  44. inline int ip6_rcv_finish( struct sk_buff *skb)
  45. {
  46. if (skb->dst == NULL)
  47. ip6_route_input(skb);
  48. return dst_input(skb);
  49. }
  50. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  51. {
  52. struct ipv6hdr *hdr;
  53. u32 pkt_len;
  54. struct inet6_dev *idev;
  55. if (skb->pkt_type == PACKET_OTHERHOST) {
  56. kfree_skb(skb);
  57. return 0;
  58. }
  59. rcu_read_lock();
  60. idev = __in6_dev_get(skb->dev);
  61. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INRECEIVES);
  62. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  63. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  64. rcu_read_unlock();
  65. goto out;
  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 ? ip6_dst_idev(skb->dst)->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(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  100. goto drop;
  101. }
  102. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  103. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  104. goto drop;
  105. }
  106. hdr = ipv6_hdr(skb);
  107. }
  108. if (hdr->nexthdr == NEXTHDR_HOP) {
  109. if (ipv6_parse_hopopts(skb) < 0) {
  110. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  111. rcu_read_unlock();
  112. return 0;
  113. }
  114. }
  115. rcu_read_unlock();
  116. return NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, dev, NULL,
  117. ip6_rcv_finish);
  118. err:
  119. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  120. drop:
  121. rcu_read_unlock();
  122. kfree_skb(skb);
  123. out:
  124. return 0;
  125. }
  126. /*
  127. * Deliver the packet to the host
  128. */
  129. static int ip6_input_finish(struct sk_buff *skb)
  130. {
  131. struct inet6_protocol *ipprot;
  132. unsigned int nhoff;
  133. int nexthdr, raw;
  134. u8 hash;
  135. struct inet6_dev *idev;
  136. /*
  137. * Parse extension headers
  138. */
  139. rcu_read_lock();
  140. resubmit:
  141. idev = ip6_dst_idev(skb->dst);
  142. if (!pskb_pull(skb, skb_transport_offset(skb)))
  143. goto discard;
  144. nhoff = IP6CB(skb)->nhoff;
  145. nexthdr = skb_network_header(skb)[nhoff];
  146. raw = raw6_local_deliver(skb, nexthdr);
  147. hash = nexthdr & (MAX_INET_PROTOS - 1);
  148. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  149. int ret;
  150. if (ipprot->flags & INET6_PROTO_FINAL) {
  151. struct ipv6hdr *hdr;
  152. /* Free reference early: we don't need it any more,
  153. and it may hold ip_conntrack module loaded
  154. indefinitely. */
  155. nf_reset(skb);
  156. skb_postpull_rcsum(skb, skb_network_header(skb),
  157. skb_network_header_len(skb));
  158. hdr = ipv6_hdr(skb);
  159. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  160. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  161. &hdr->saddr) &&
  162. !ipv6_is_mld(skb, nexthdr))
  163. goto discard;
  164. }
  165. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  166. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  167. goto discard;
  168. ret = ipprot->handler(skb);
  169. if (ret > 0)
  170. goto resubmit;
  171. else if (ret == 0)
  172. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  173. } else {
  174. if (!raw) {
  175. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  176. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  177. icmpv6_send(skb, ICMPV6_PARAMPROB,
  178. ICMPV6_UNK_NEXTHDR, nhoff,
  179. skb->dev);
  180. }
  181. } else
  182. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  183. kfree_skb(skb);
  184. }
  185. rcu_read_unlock();
  186. return 0;
  187. discard:
  188. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  189. rcu_read_unlock();
  190. kfree_skb(skb);
  191. return 0;
  192. }
  193. int ip6_input(struct sk_buff *skb)
  194. {
  195. return NF_HOOK(PF_INET6, NF_INET_LOCAL_IN, skb, skb->dev, NULL,
  196. ip6_input_finish);
  197. }
  198. int ip6_mc_input(struct sk_buff *skb)
  199. {
  200. struct ipv6hdr *hdr;
  201. int deliver;
  202. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  203. hdr = ipv6_hdr(skb);
  204. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  205. #ifdef CONFIG_IPV6_MROUTE
  206. /*
  207. * IPv6 multicast router mode is now supported ;)
  208. */
  209. if (ipv6_devconf.mc_forwarding &&
  210. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  211. /*
  212. * Okay, we try to forward - split and duplicate
  213. * packets.
  214. */
  215. struct sk_buff *skb2;
  216. struct inet6_skb_parm *opt = IP6CB(skb);
  217. /* Check for MLD */
  218. if (unlikely(opt->ra)) {
  219. /* Check if this is a mld message */
  220. u8 *ptr = skb_network_header(skb) + opt->ra;
  221. struct icmp6hdr *icmp6;
  222. u8 nexthdr = hdr->nexthdr;
  223. int offset;
  224. /* Check if the value of Router Alert
  225. * is for MLD (0x0000).
  226. */
  227. if ((ptr[2] | ptr[3]) == 0) {
  228. deliver = 0;
  229. if (!ipv6_ext_hdr(nexthdr)) {
  230. /* BUG */
  231. goto out;
  232. }
  233. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  234. &nexthdr);
  235. if (offset < 0)
  236. goto out;
  237. if (nexthdr != IPPROTO_ICMPV6)
  238. goto out;
  239. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  240. offset + 1 - skb->data)))
  241. goto out;
  242. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  243. switch (icmp6->icmp6_type) {
  244. case ICMPV6_MGM_QUERY:
  245. case ICMPV6_MGM_REPORT:
  246. case ICMPV6_MGM_REDUCTION:
  247. case ICMPV6_MLD2_REPORT:
  248. deliver = 1;
  249. break;
  250. }
  251. goto out;
  252. }
  253. /* unknown RA - process it normally */
  254. }
  255. if (deliver)
  256. skb2 = skb_clone(skb, GFP_ATOMIC);
  257. else {
  258. skb2 = skb;
  259. skb = NULL;
  260. }
  261. if (skb2) {
  262. skb2->dev = skb2->dst->dev;
  263. ip6_mr_input(skb2);
  264. }
  265. }
  266. out:
  267. #endif
  268. if (likely(deliver))
  269. ip6_input(skb);
  270. else {
  271. /* discard */
  272. kfree_skb(skb);
  273. }
  274. return 0;
  275. }