netfilter.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. int ip6_route_me_harder(struct sk_buff *skb)
  12. {
  13. struct ipv6hdr *iph = skb->nh.ipv6h;
  14. struct dst_entry *dst;
  15. struct flowi fl = {
  16. .oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
  17. .mark = skb->mark,
  18. .nl_u =
  19. { .ip6_u =
  20. { .daddr = iph->daddr,
  21. .saddr = iph->saddr, } },
  22. };
  23. dst = ip6_route_output(skb->sk, &fl);
  24. #ifdef CONFIG_XFRM
  25. if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
  26. xfrm_decode_session(skb, &fl, AF_INET6) == 0)
  27. if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
  28. return -1;
  29. #endif
  30. if (dst->error) {
  31. IP6_INC_STATS(ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  32. LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
  33. dst_release(dst);
  34. return -EINVAL;
  35. }
  36. /* Drop old route. */
  37. dst_release(skb->dst);
  38. skb->dst = dst;
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(ip6_route_me_harder);
  42. /*
  43. * Extra routing may needed on local out, as the QUEUE target never
  44. * returns control to the table.
  45. */
  46. struct ip6_rt_info {
  47. struct in6_addr daddr;
  48. struct in6_addr saddr;
  49. };
  50. static void nf_ip6_saveroute(const struct sk_buff *skb, struct nf_info *info)
  51. {
  52. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  53. if (info->hook == NF_IP6_LOCAL_OUT) {
  54. struct ipv6hdr *iph = skb->nh.ipv6h;
  55. rt_info->daddr = iph->daddr;
  56. rt_info->saddr = iph->saddr;
  57. }
  58. }
  59. static int nf_ip6_reroute(struct sk_buff **pskb, const struct nf_info *info)
  60. {
  61. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  62. if (info->hook == NF_IP6_LOCAL_OUT) {
  63. struct ipv6hdr *iph = (*pskb)->nh.ipv6h;
  64. if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
  65. !ipv6_addr_equal(&iph->saddr, &rt_info->saddr))
  66. return ip6_route_me_harder(*pskb);
  67. }
  68. return 0;
  69. }
  70. __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
  71. unsigned int dataoff, u_int8_t protocol)
  72. {
  73. struct ipv6hdr *ip6h = skb->nh.ipv6h;
  74. __sum16 csum = 0;
  75. switch (skb->ip_summed) {
  76. case CHECKSUM_COMPLETE:
  77. if (hook != NF_IP6_PRE_ROUTING && hook != NF_IP6_LOCAL_IN)
  78. break;
  79. if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  80. skb->len - dataoff, protocol,
  81. csum_sub(skb->csum,
  82. skb_checksum(skb, 0,
  83. dataoff, 0)))) {
  84. skb->ip_summed = CHECKSUM_UNNECESSARY;
  85. break;
  86. }
  87. /* fall through */
  88. case CHECKSUM_NONE:
  89. skb->csum = ~csum_unfold(
  90. csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  91. skb->len - dataoff,
  92. protocol,
  93. csum_sub(0,
  94. skb_checksum(skb, 0,
  95. dataoff, 0))));
  96. csum = __skb_checksum_complete(skb);
  97. }
  98. return csum;
  99. }
  100. EXPORT_SYMBOL(nf_ip6_checksum);
  101. static struct nf_afinfo nf_ip6_afinfo = {
  102. .family = AF_INET6,
  103. .checksum = nf_ip6_checksum,
  104. .saveroute = nf_ip6_saveroute,
  105. .reroute = nf_ip6_reroute,
  106. .route_key_size = sizeof(struct ip6_rt_info),
  107. };
  108. int __init ipv6_netfilter_init(void)
  109. {
  110. return nf_register_afinfo(&nf_ip6_afinfo);
  111. }
  112. /* This can be called from inet6_init() on errors, so it cannot
  113. * be marked __exit. -DaveM
  114. */
  115. void ipv6_netfilter_fini(void)
  116. {
  117. nf_unregister_afinfo(&nf_ip6_afinfo);
  118. }