PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv6/netfilter.c

https://github.com/nobled/linux-2.6
C | 179 lines | 148 code | 21 blank | 10 comment | 19 complexity | b39e20746e0ac09da2de720cc6b7c916 MD5 | raw file
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/ipv6.h>
  4. #include <linux/netfilter.h>
  5. #include <linux/netfilter_ipv6.h>
  6. #include <net/dst.h>
  7. #include <net/ipv6.h>
  8. #include <net/ip6_route.h>
  9. #include <net/xfrm.h>
  10. #include <net/ip6_checksum.h>
  11. #include <net/netfilter/nf_queue.h>
  12. int ip6_route_me_harder(struct sk_buff *skb)
  13. {
  14. struct net *net = dev_net(skb_dst(skb)->dev);
  15. struct ipv6hdr *iph = ipv6_hdr(skb);
  16. struct dst_entry *dst;
  17. struct flowi6 fl6 = {
  18. .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
  19. .flowi6_mark = skb->mark,
  20. .daddr = iph->daddr,
  21. .saddr = iph->saddr,
  22. };
  23. dst = ip6_route_output(net, skb->sk, &fl6);
  24. if (dst->error) {
  25. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  26. LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
  27. dst_release(dst);
  28. return -EINVAL;
  29. }
  30. /* Drop old route. */
  31. skb_dst_drop(skb);
  32. skb_dst_set(skb, dst);
  33. #ifdef CONFIG_XFRM
  34. if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
  35. xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
  36. skb_dst_set(skb, NULL);
  37. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
  38. if (IS_ERR(dst))
  39. return -1;
  40. skb_dst_set(skb, dst);
  41. }
  42. #endif
  43. return 0;
  44. }
  45. EXPORT_SYMBOL(ip6_route_me_harder);
  46. /*
  47. * Extra routing may needed on local out, as the QUEUE target never
  48. * returns control to the table.
  49. */
  50. struct ip6_rt_info {
  51. struct in6_addr daddr;
  52. struct in6_addr saddr;
  53. u_int32_t mark;
  54. };
  55. static void nf_ip6_saveroute(const struct sk_buff *skb,
  56. struct nf_queue_entry *entry)
  57. {
  58. struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
  59. if (entry->hook == NF_INET_LOCAL_OUT) {
  60. struct ipv6hdr *iph = ipv6_hdr(skb);
  61. rt_info->daddr = iph->daddr;
  62. rt_info->saddr = iph->saddr;
  63. rt_info->mark = skb->mark;
  64. }
  65. }
  66. static int nf_ip6_reroute(struct sk_buff *skb,
  67. const struct nf_queue_entry *entry)
  68. {
  69. struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
  70. if (entry->hook == NF_INET_LOCAL_OUT) {
  71. struct ipv6hdr *iph = ipv6_hdr(skb);
  72. if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
  73. !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
  74. skb->mark != rt_info->mark)
  75. return ip6_route_me_harder(skb);
  76. }
  77. return 0;
  78. }
  79. static int nf_ip6_route(struct dst_entry **dst, struct flowi *fl)
  80. {
  81. *dst = ip6_route_output(&init_net, NULL, &fl->u.ip6);
  82. return (*dst)->error;
  83. }
  84. __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
  85. unsigned int dataoff, u_int8_t protocol)
  86. {
  87. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  88. __sum16 csum = 0;
  89. switch (skb->ip_summed) {
  90. case CHECKSUM_COMPLETE:
  91. if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
  92. break;
  93. if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  94. skb->len - dataoff, protocol,
  95. csum_sub(skb->csum,
  96. skb_checksum(skb, 0,
  97. dataoff, 0)))) {
  98. skb->ip_summed = CHECKSUM_UNNECESSARY;
  99. break;
  100. }
  101. /* fall through */
  102. case CHECKSUM_NONE:
  103. skb->csum = ~csum_unfold(
  104. csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  105. skb->len - dataoff,
  106. protocol,
  107. csum_sub(0,
  108. skb_checksum(skb, 0,
  109. dataoff, 0))));
  110. csum = __skb_checksum_complete(skb);
  111. }
  112. return csum;
  113. }
  114. EXPORT_SYMBOL(nf_ip6_checksum);
  115. static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
  116. unsigned int dataoff, unsigned int len,
  117. u_int8_t protocol)
  118. {
  119. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  120. __wsum hsum;
  121. __sum16 csum = 0;
  122. switch (skb->ip_summed) {
  123. case CHECKSUM_COMPLETE:
  124. if (len == skb->len - dataoff)
  125. return nf_ip6_checksum(skb, hook, dataoff, protocol);
  126. /* fall through */
  127. case CHECKSUM_NONE:
  128. hsum = skb_checksum(skb, 0, dataoff, 0);
  129. skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
  130. &ip6h->daddr,
  131. skb->len - dataoff,
  132. protocol,
  133. csum_sub(0, hsum)));
  134. skb->ip_summed = CHECKSUM_NONE;
  135. return __skb_checksum_complete_head(skb, dataoff + len);
  136. }
  137. return csum;
  138. };
  139. static const struct nf_afinfo nf_ip6_afinfo = {
  140. .family = AF_INET6,
  141. .checksum = nf_ip6_checksum,
  142. .checksum_partial = nf_ip6_checksum_partial,
  143. .route = nf_ip6_route,
  144. .saveroute = nf_ip6_saveroute,
  145. .reroute = nf_ip6_reroute,
  146. .route_key_size = sizeof(struct ip6_rt_info),
  147. };
  148. int __init ipv6_netfilter_init(void)
  149. {
  150. return nf_register_afinfo(&nf_ip6_afinfo);
  151. }
  152. /* This can be called from inet6_init() on errors, so it cannot
  153. * be marked __exit. -DaveM
  154. */
  155. void ipv6_netfilter_fini(void)
  156. {
  157. nf_unregister_afinfo(&nf_ip6_afinfo);
  158. }