PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src-rt/linux/linux-2.6/net/ipv6/ip6_input.c

https://gitlab.com/envieidoc/tomato
C | 322 lines | 211 code | 52 blank | 59 comment | 47 complexity | f9d18b248a14d5d909297fe3933b8cd5 MD5 | raw file
  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. !idev || unlikely(idev->cnf.disable_ipv6)) {
  64. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  65. goto drop;
  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. skb->transport_header = skb->network_header + sizeof(*hdr);
  86. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  87. pkt_len = ntohs(hdr->payload_len);
  88. /* pkt_len may be zero if Jumbo payload option is present */
  89. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  90. if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
  91. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INTRUNCATEDPKTS);
  92. goto drop;
  93. }
  94. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
  95. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  96. goto drop;
  97. }
  98. hdr = ipv6_hdr(skb);
  99. }
  100. if (hdr->nexthdr == NEXTHDR_HOP) {
  101. if (ipv6_parse_hopopts(skb) < 0) {
  102. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INHDRERRORS);
  103. rcu_read_unlock();
  104. return 0;
  105. }
  106. }
  107. rcu_read_unlock();
  108. return NF_HOOK(PF_INET6,NF_IP6_PRE_ROUTING, skb, dev, NULL, 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. return 0;
  115. }
  116. /*
  117. * Deliver the packet to the host
  118. */
  119. static int ip6_input_finish(struct sk_buff *skb)
  120. {
  121. struct inet6_protocol *ipprot;
  122. struct sock *raw_sk;
  123. unsigned int nhoff;
  124. int nexthdr;
  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_sk = sk_head(&raw_v6_htable[nexthdr & (MAX_INET_PROTOS - 1)]);
  138. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  139. raw_sk = NULL;
  140. hash = nexthdr & (MAX_INET_PROTOS - 1);
  141. if ((ipprot = rcu_dereference(inet6_protos[hash])) != NULL) {
  142. int ret;
  143. if (ipprot->flags & INET6_PROTO_FINAL) {
  144. struct ipv6hdr *hdr;
  145. /* Free reference early: we don't need it any more,
  146. and it may hold ip_conntrack module loaded
  147. indefinitely. */
  148. nf_reset(skb);
  149. skb_postpull_rcsum(skb, skb_network_header(skb),
  150. skb_network_header_len(skb));
  151. hdr = ipv6_hdr(skb);
  152. if (ipv6_addr_is_multicast(&hdr->daddr) &&
  153. !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
  154. &hdr->saddr) &&
  155. !ipv6_is_mld(skb, nexthdr))
  156. goto discard;
  157. }
  158. if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
  159. !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  160. goto discard;
  161. ret = ipprot->handler(skb);
  162. if (ret > 0)
  163. goto resubmit;
  164. else if (ret == 0)
  165. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  166. } else {
  167. if (!raw_sk) {
  168. if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  169. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INUNKNOWNPROTOS);
  170. icmpv6_send(skb, ICMPV6_PARAMPROB,
  171. ICMPV6_UNK_NEXTHDR, nhoff,
  172. skb->dev);
  173. }
  174. } else
  175. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDELIVERS);
  176. kfree_skb(skb);
  177. }
  178. rcu_read_unlock();
  179. return 0;
  180. discard:
  181. IP6_INC_STATS_BH(idev, IPSTATS_MIB_INDISCARDS);
  182. rcu_read_unlock();
  183. kfree_skb(skb);
  184. return 0;
  185. }
  186. int ip6_input(struct sk_buff *skb)
  187. {
  188. return NF_HOOK(PF_INET6,NF_IP6_LOCAL_IN, skb, skb->dev, NULL, ip6_input_finish);
  189. }
  190. int ip6_mc_input(struct sk_buff *skb)
  191. {
  192. struct ipv6hdr *hdr;
  193. int deliver;
  194. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INMCASTPKTS);
  195. hdr = ipv6_hdr(skb);
  196. deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
  197. #ifdef CONFIG_IPV6_MROUTE
  198. /*
  199. * IPv6 multicast router mode is now supported ;)
  200. */
  201. if (ipv6_devconf.mc_forwarding &&
  202. likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
  203. /*
  204. * Okay, we try to forward - split and duplicate
  205. * packets.
  206. */
  207. struct sk_buff *skb2;
  208. struct inet6_skb_parm *opt = IP6CB(skb);
  209. /* Check for MLD */
  210. if (unlikely(opt->ra)) {
  211. /* Check if this is a mld message */
  212. u8 *ptr = skb_network_header(skb) + opt->ra;
  213. struct icmp6hdr *icmp6;
  214. u8 nexthdr = hdr->nexthdr;
  215. int offset;
  216. /* Check if the value of Router Alert
  217. * is for MLD (0x0000).
  218. */
  219. if ((ptr[2] | ptr[3]) == 0) {
  220. if (!ipv6_ext_hdr(nexthdr)) {
  221. /* BUG */
  222. goto discard;
  223. }
  224. offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
  225. &nexthdr);
  226. if (offset < 0)
  227. goto discard;
  228. if (nexthdr != IPPROTO_ICMPV6)
  229. goto discard;
  230. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  231. offset + 1 - skb->data)))
  232. goto discard;
  233. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  234. switch (icmp6->icmp6_type) {
  235. case ICMPV6_MGM_QUERY:
  236. case ICMPV6_MGM_REPORT:
  237. case ICMPV6_MGM_REDUCTION:
  238. case ICMPV6_MLD2_REPORT:
  239. break;
  240. default:
  241. /* Bogus */
  242. goto discard;
  243. }
  244. deliver = 1;
  245. goto out;
  246. }
  247. /* unknown RA - process it normally */
  248. }
  249. if (deliver)
  250. skb2 = skb_clone(skb, GFP_ATOMIC);
  251. else {
  252. skb2 = skb;
  253. skb = NULL;
  254. }
  255. if (skb2) {
  256. skb2->dev = skb2->dst->dev;
  257. ip6_mr_input(skb2);
  258. }
  259. }
  260. out:
  261. #endif
  262. if (likely(deliver)) {
  263. ip6_input(skb);
  264. return 0;
  265. }
  266. discard:
  267. /* discard */
  268. kfree_skb(skb);
  269. return 0;
  270. }