netfilter.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
  27. dst_release(dst);
  28. return -EINVAL;
  29. }
  30. /* Drop old route. */
  31. dst_release(skb->dst);
  32. skb->dst = dst;
  33. return 0;
  34. }
  35. EXPORT_SYMBOL(ip6_route_me_harder);
  36. /*
  37. * Extra routing may needed on local out, as the QUEUE target never
  38. * returns control to the table.
  39. */
  40. struct ip6_rt_info {
  41. struct in6_addr daddr;
  42. struct in6_addr saddr;
  43. };
  44. static void save(const struct sk_buff *skb, struct nf_info *info)
  45. {
  46. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  47. if (info->hook == NF_IP6_LOCAL_OUT) {
  48. struct ipv6hdr *iph = skb->nh.ipv6h;
  49. rt_info->daddr = iph->daddr;
  50. rt_info->saddr = iph->saddr;
  51. }
  52. }
  53. static int reroute(struct sk_buff **pskb, const struct nf_info *info)
  54. {
  55. struct ip6_rt_info *rt_info = nf_info_reroute(info);
  56. if (info->hook == NF_IP6_LOCAL_OUT) {
  57. struct ipv6hdr *iph = (*pskb)->nh.ipv6h;
  58. if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
  59. !ipv6_addr_equal(&iph->saddr, &rt_info->saddr))
  60. return ip6_route_me_harder(*pskb);
  61. }
  62. return 0;
  63. }
  64. static struct nf_queue_rerouter ip6_reroute = {
  65. .rer_size = sizeof(struct ip6_rt_info),
  66. .save = &save,
  67. .reroute = &reroute,
  68. };
  69. int __init ipv6_netfilter_init(void)
  70. {
  71. return nf_register_queue_rerouter(PF_INET6, &ip6_reroute);
  72. }
  73. void ipv6_netfilter_fini(void)
  74. {
  75. nf_unregister_queue_rerouter(PF_INET6);
  76. }
  77. #else /* CONFIG_NETFILTER */
  78. int __init ipv6_netfilter_init(void)
  79. {
  80. return 0;
  81. }
  82. void ipv6_netfilter_fini(void)
  83. {
  84. }
  85. #endif /* CONFIG_NETFILTER */