ip6_input.c 6.1 KB

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