ip6_input.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/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 == 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. struct ipv6hdr *hdr;
  52. u32 pkt_len;
  53. struct inet6_dev *idev;
  54. if (dev->nd_net != &init_net) {
  55. kfree_skb(skb);
  56. return 0;
  57. }
  58. if (skb->pkt_type == PACKET_OTHERHOST) {
  59. kfree_skb(skb);
  60. return 0;
  61. }
  62. rcu_read_lock();
  63. idev = __in6_dev_get(skb->dev);
  64. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INRECEIVES);
  65. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  66. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  67. rcu_read_unlock();
  68. goto out;
  69. }
  70. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  71. /*
  72. * Store incoming device index. When the packet will
  73. * be queued, we cannot refer to skb->dev anymore.
  74. *
  75. * BTW, when we send a packet for our own local address on a
  76. * non-loopback interface (e.g. ethX), it is being delivered
  77. * via the loopback interface (lo) here; skb->dev = loopback_dev.
  78. * It, however, should be considered as if it is being
  79. * arrived via the sending interface (ethX), because of the
  80. * nature of scoping architecture. --yoshfuji
  81. */
  82. IP6CB(skb)->iif = skb->dst ? ip6_dst_idev(skb->dst)->dev->ifindex : dev->ifindex;
  83. if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
  84. goto err;
  85. hdr = ipv6_hdr(skb);
  86. if (hdr->version != 6)
  87. goto err;
  88. skb->transport_header = skb->network_header + sizeof(*hdr);
  89. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  90. pkt_len = ntohs(hdr->payload_len);
  91. /* pkt_len may be zero if Jumbo payload option is present */
  92. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  93. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  94. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  95. goto drop;
  96. }
  97. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  98. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  99. goto drop;
  100. }
  101. hdr = ipv6_hdr(skb);
  102. }
  103. if (hdr->nexthdr == NEXTHDR_HOP) {
  104. if (ipv6_parse_hopopts(&skb) < 0) {
  105. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  106. rcu_read_unlock();
  107. return 0;
  108. }
  109. }
  110. rcu_read_unlock();
  111. return NF_HOOK(PF_INET6,NF_IP6_PRE_ROUTING, skb, dev, NULL, ip6_rcv_finish);
  112. err:
  113. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  114. drop:
  115. rcu_read_unlock();
  116. kfree_skb(skb);
  117. out:
  118. return 0;
  119. }
  120. /*
  121. * Deliver the packet to the host
  122. */
  123. static inline int ip6_input_finish(struct sk_buff *skb)
  124. {
  125. struct inet6_protocol *ipprot;
  126. struct sock *raw_sk;
  127. unsigned int nhoff;
  128. int nexthdr;
  129. u8 hash;
  130. struct inet6_dev *idev;
  131. /*
  132. * Parse extension headers
  133. */
  134. rcu_read_lock();
  135. resubmit:
  136. idev = ip6_dst_idev(skb->dst);
  137. if (!pskb_pull(skb, skb_transport_offset(skb)))
  138. goto discard;
  139. nhoff = IP6CB(skb)->nhoff;
  140. nexthdr = skb_network_header(skb)[nhoff];
  141. raw_sk = sk_head(&raw_v6_htable[nexthdr & (MAX_INET_PROTOS - 1)]);
  142. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  143. raw_sk = NULL;
  144. hash = nexthdr & (MAX_INET_PROTOS - 1);
  145. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  146. int ret;
  147. if (ipprot->flags & INET6_PROTO_FINAL) {
  148. struct ipv6hdr *hdr;
  149. /* Free reference early: we don't need it any more,
  150. and it may hold ip_conntrack module loaded
  151. indefinitely. */
  152. nf_reset(skb);
  153. skb_postpull_rcsum(skb, skb_network_header(skb),
  154. skb_network_header_len(skb));
  155. hdr = ipv6_hdr(skb);
  156. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  157. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  158. &hdr->saddr) &&
  159. !ipv6_is_mld(skb, nexthdr))
  160. goto discard;
  161. }
  162. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  163. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  164. goto discard;
  165. ret = ipprot->handler(&skb);
  166. if (ret > 0)
  167. goto resubmit;
  168. else if (ret == 0)
  169. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  170. } else {
  171. if (!raw_sk) {
  172. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  173. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  174. icmpv6_send(skb, ICMPV6_PARAMPROB,
  175. ICMPV6_UNK_NEXTHDR, nhoff,
  176. skb->dev);
  177. }
  178. } else
  179. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  180. kfree_skb(skb);
  181. }
  182. rcu_read_unlock();
  183. return 0;
  184. discard:
  185. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  186. rcu_read_unlock();
  187. kfree_skb(skb);
  188. return 0;
  189. }
  190. int ip6_input(struct sk_buff *skb)
  191. {
  192. return NF_HOOK(PF_INET6,NF_IP6_LOCAL_IN, skb, skb->dev, NULL, ip6_input_finish);
  193. }
  194. int ip6_mc_input(struct sk_buff *skb)
  195. {
  196. struct ipv6hdr *hdr;
  197. int deliver;
  198. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  199. hdr = ipv6_hdr(skb);
  200. deliver = unlikely(skb->dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) ||
  201. ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  202. /*
  203. * IPv6 multicast router mode isnt currently supported.
  204. */
  205. #if 0
  206. if (ipv6_config.multicast_route) {
  207. int addr_type;
  208. addr_type = ipv6_addr_type(&hdr->daddr);
  209. if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) {
  210. struct sk_buff *skb2;
  211. struct dst_entry *dst;
  212. dst = skb->dst;
  213. if (deliver) {
  214. skb2 = skb_clone(skb, GFP_ATOMIC);
  215. dst_output(skb2);
  216. } else {
  217. dst_output(skb);
  218. return 0;
  219. }
  220. }
  221. }
  222. #endif
  223. if (likely(deliver)) {
  224. ip6_input(skb);
  225. return 0;
  226. }
  227. /* discard */
  228. kfree_skb(skb);
  229. return 0;
  230. }