netfilter.c 3.5 KB

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