netfilter.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <linux/config.h>
  2. #include <linux/init.h>
  3. #ifdef CONFIG_NETFILTER
  4. #include <linux/kernel.h>
  5. #include <linux/ipv6.h>
  6. #include <linux/netfilter.h>
  7. #include <linux/netfilter_ipv6.h>
  8. #include <net/dst.h>
  9. #include <net/ipv6.h>
  10. #include <net/ip6_route.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. .proto = iph->nexthdr,
  22. };
  23. dst = ip6_route_output(skb->sk, &fl);
  24. if (dst->error) {
  25. IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
  26. LIMIT_NETDEBUG(
  27. printk(KERN_DEBUG "ip6_route_me_harder: No more route.\n"));
  28. dst_release(dst);
  29. return -EINVAL;
  30. }
  31. /* Drop old route. */
  32. dst_release(skb->dst);
  33. skb->dst = dst;
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(ip6_route_me_harder);
  37. /*
  38. * Extra routing may needed on local out, as the QUEUE target never
  39. * returns control to the table.
  40. */
  41. struct ip6_rt_info {
  42. struct in6_addr daddr;
  43. struct in6_addr saddr;
  44. };
  45. static void save(const struct sk_buff *skb, struct nf_info *info)
  46. {
  47. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  48. if (info->hook == NF_IP6_LOCAL_OUT) {
  49. struct ipv6hdr *iph = skb->nh.ipv6h;
  50. rt_info->daddr = iph->daddr;
  51. rt_info->saddr = iph->saddr;
  52. }
  53. }
  54. static int reroute(struct sk_buff **pskb, const struct nf_info *info)
  55. {
  56. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  57. if (info->hook == NF_IP6_LOCAL_OUT) {
  58. struct ipv6hdr *iph = (*pskb)->nh.ipv6h;
  59. if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
  60. !ipv6_addr_equal(&iph->saddr, &rt_info->saddr))
  61. return ip6_route_me_harder(*pskb);
  62. }
  63. return 0;
  64. }
  65. static struct nf_queue_rerouter ip6_reroute = {
  66. .rer_size = sizeof(struct ip6_rt_info),
  67. .save = &save,
  68. .reroute = &reroute,
  69. };
  70. int __init ipv6_netfilter_init(void)
  71. {
  72. return nf_register_queue_rerouter(PF_INET6, &ip6_reroute);
  73. }
  74. void ipv6_netfilter_fini(void)
  75. {
  76. nf_unregister_queue_rerouter(PF_INET6);
  77. }
  78. #else /* CONFIG_NETFILTER */
  79. int __init ipv6_netfilter_init(void)
  80. {
  81. return 0;
  82. }
  83. void ipv6_netfilter_fini(void)
  84. {
  85. }
  86. #endif /* CONFIG_NETFILTER */