ip6_input.c 6.1 KB

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