netfilter.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* IPv4 specific functions of netfilter core */
  2. #include <linux/config.h>
  3. #ifdef CONFIG_NETFILTER
  4. #include <linux/kernel.h>
  5. #include <linux/netfilter.h>
  6. #include <linux/netfilter_ipv4.h>
  7. #include <linux/tcp.h>
  8. #include <linux/udp.h>
  9. #include <linux/icmp.h>
  10. #include <net/route.h>
  11. #include <linux/ip.h>
  12. /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
  13. int ip_route_me_harder(struct sk_buff **pskb)
  14. {
  15. struct iphdr *iph = (*pskb)->nh.iph;
  16. struct rtable *rt;
  17. struct flowi fl = {};
  18. struct dst_entry *odst;
  19. unsigned int hh_len;
  20. /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
  21. * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook.
  22. */
  23. if (inet_addr_type(iph->saddr) == RTN_LOCAL) {
  24. fl.nl_u.ip4_u.daddr = iph->daddr;
  25. fl.nl_u.ip4_u.saddr = iph->saddr;
  26. fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
  27. fl.oif = (*pskb)->sk ? (*pskb)->sk->sk_bound_dev_if : 0;
  28. #ifdef CONFIG_IP_ROUTE_FWMARK
  29. fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark;
  30. #endif
  31. fl.proto = iph->protocol;
  32. if (ip_route_output_key(&rt, &fl) != 0)
  33. return -1;
  34. /* Drop old route. */
  35. dst_release((*pskb)->dst);
  36. (*pskb)->dst = &rt->u.dst;
  37. } else {
  38. /* non-local src, find valid iif to satisfy
  39. * rp-filter when calling ip_route_input. */
  40. fl.nl_u.ip4_u.daddr = iph->saddr;
  41. if (ip_route_output_key(&rt, &fl) != 0)
  42. return -1;
  43. odst = (*pskb)->dst;
  44. if (ip_route_input(*pskb, iph->daddr, iph->saddr,
  45. RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
  46. dst_release(&rt->u.dst);
  47. return -1;
  48. }
  49. dst_release(&rt->u.dst);
  50. dst_release(odst);
  51. }
  52. if ((*pskb)->dst->error)
  53. return -1;
  54. /* Change in oif may mean change in hh_len. */
  55. hh_len = (*pskb)->dst->dev->hard_header_len;
  56. if (skb_headroom(*pskb) < hh_len) {
  57. struct sk_buff *nskb;
  58. nskb = skb_realloc_headroom(*pskb, hh_len);
  59. if (!nskb)
  60. return -1;
  61. if ((*pskb)->sk)
  62. skb_set_owner_w(nskb, (*pskb)->sk);
  63. kfree_skb(*pskb);
  64. *pskb = nskb;
  65. }
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(ip_route_me_harder);
  69. /*
  70. * Extra routing may needed on local out, as the QUEUE target never
  71. * returns control to the table.
  72. */
  73. struct ip_rt_info {
  74. u_int32_t daddr;
  75. u_int32_t saddr;
  76. u_int8_t tos;
  77. };
  78. static void queue_save(const struct sk_buff *skb, struct nf_info *info)
  79. {
  80. struct ip_rt_info *rt_info = nf_info_reroute(info);
  81. if (info->hook == NF_IP_LOCAL_OUT) {
  82. const struct iphdr *iph = skb->nh.iph;
  83. rt_info->tos = iph->tos;
  84. rt_info->daddr = iph->daddr;
  85. rt_info->saddr = iph->saddr;
  86. }
  87. }
  88. static int queue_reroute(struct sk_buff **pskb, const struct nf_info *info)
  89. {
  90. const struct ip_rt_info *rt_info = nf_info_reroute(info);
  91. if (info->hook == NF_IP_LOCAL_OUT) {
  92. struct iphdr *iph = (*pskb)->nh.iph;
  93. if (!(iph->tos == rt_info->tos
  94. && iph->daddr == rt_info->daddr
  95. && iph->saddr == rt_info->saddr))
  96. return ip_route_me_harder(pskb);
  97. }
  98. return 0;
  99. }
  100. static struct nf_queue_rerouter ip_reroute = {
  101. .rer_size = sizeof(struct ip_rt_info),
  102. .save = queue_save,
  103. .reroute = queue_reroute,
  104. };
  105. static int init(void)
  106. {
  107. return nf_register_queue_rerouter(PF_INET, &ip_reroute);
  108. }
  109. static void fini(void)
  110. {
  111. nf_unregister_queue_rerouter(PF_INET);
  112. }
  113. module_init(init);
  114. module_exit(fini);
  115. #endif /* CONFIG_NETFILTER */