netfilter.c 3.1 KB

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