ip_nat_rule.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Everything about the rules for NAT. */
  2. #define __NO_VERSION__
  3. #include <linux/types.h>
  4. #include <linux/ip.h>
  5. #include <linux/netfilter.h>
  6. #include <linux/netfilter_ipv4.h>
  7. #include <linux/module.h>
  8. #include <linux/kmod.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/proc_fs.h>
  11. #include <net/checksum.h>
  12. #include <linux/bitops.h>
  13. #include <linux/version.h>
  14. #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
  15. #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
  16. #include <linux/netfilter_ipv4/ip_tables.h>
  17. #include <linux/netfilter_ipv4/ip_nat.h>
  18. #include <linux/netfilter_ipv4/ip_nat_core.h>
  19. #include <linux/netfilter_ipv4/ip_nat_rule.h>
  20. #include <linux/netfilter_ipv4/listhelp.h>
  21. #if 0
  22. #define DEBUGP printk
  23. #else
  24. #define DEBUGP(format, args...)
  25. #endif
  26. #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
  27. /* Standard entry. */
  28. struct ipt_standard
  29. {
  30. struct ipt_entry entry;
  31. struct ipt_standard_target target;
  32. };
  33. struct ipt_error_target
  34. {
  35. struct ipt_entry_target target;
  36. char errorname[IPT_FUNCTION_MAXNAMELEN];
  37. };
  38. struct ipt_error
  39. {
  40. struct ipt_entry entry;
  41. struct ipt_error_target target;
  42. };
  43. static struct
  44. {
  45. struct ipt_replace repl;
  46. struct ipt_standard entries[3];
  47. struct ipt_error term;
  48. } nat_initial_table __initdata
  49. = { { "nat", NAT_VALID_HOOKS, 4,
  50.       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
  51.       { [NF_IP_PRE_ROUTING] 0,
  52. [NF_IP_POST_ROUTING] sizeof(struct ipt_standard),
  53. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  54.       { [NF_IP_PRE_ROUTING] 0,
  55. [NF_IP_POST_ROUTING] sizeof(struct ipt_standard),
  56. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  57.       0, NULL, { } },
  58.     {
  59.     /* PRE_ROUTING */
  60.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  61. 0,
  62. sizeof(struct ipt_entry),
  63. sizeof(struct ipt_standard),
  64. 0, { 0, 0 }, { } },
  65.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  66. -NF_ACCEPT - 1 } },
  67.     /* POST_ROUTING */
  68.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  69. 0,
  70. sizeof(struct ipt_entry),
  71. sizeof(struct ipt_standard),
  72. 0, { 0, 0 }, { } },
  73.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  74. -NF_ACCEPT - 1 } },
  75.     /* LOCAL_OUT */
  76.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  77. 0,
  78. sizeof(struct ipt_entry),
  79. sizeof(struct ipt_standard),
  80. 0, { 0, 0 }, { } },
  81.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  82. -NF_ACCEPT - 1 } }
  83.     },
  84.     /* ERROR */
  85.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  86. 0,
  87. sizeof(struct ipt_entry),
  88. sizeof(struct ipt_error),
  89. 0, { 0, 0 }, { } },
  90.       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
  91.   { } },
  92. "ERROR"
  93.       }
  94.     }
  95. };
  96. static struct ipt_table nat_table
  97. = { { NULL, NULL }, "nat", &nat_initial_table.repl,
  98.     NAT_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
  99. /* Source NAT */
  100. static unsigned int ipt_snat_target(struct sk_buff **pskb,
  101.     unsigned int hooknum,
  102.     const struct net_device *in,
  103.     const struct net_device *out,
  104.     const void *targinfo,
  105.     void *userinfo)
  106. {
  107. struct ip_conntrack *ct;
  108. enum ip_conntrack_info ctinfo;
  109. IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
  110. ct = ip_conntrack_get(*pskb, &ctinfo);
  111. /* Connection must be valid and new. */
  112. IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
  113. IP_NF_ASSERT(out);
  114. return ip_nat_setup_info(ct, targinfo, hooknum);
  115. }
  116. static unsigned int ipt_dnat_target(struct sk_buff **pskb,
  117.     unsigned int hooknum,
  118.     const struct net_device *in,
  119.     const struct net_device *out,
  120.     const void *targinfo,
  121.     void *userinfo)
  122. {
  123. struct ip_conntrack *ct;
  124. enum ip_conntrack_info ctinfo;
  125. #ifdef CONFIG_IP_NF_NAT_LOCAL
  126. IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
  127.      || hooknum == NF_IP_LOCAL_OUT);
  128. #else
  129. IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING);
  130. #endif
  131. ct = ip_conntrack_get(*pskb, &ctinfo);
  132. /* Connection must be valid and new. */
  133. IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
  134. return ip_nat_setup_info(ct, targinfo, hooknum);
  135. }
  136. static int ipt_snat_checkentry(const char *tablename,
  137.        const struct ipt_entry *e,
  138.        void *targinfo,
  139.        unsigned int targinfosize,
  140.        unsigned int hook_mask)
  141. {
  142. struct ip_nat_multi_range *mr = targinfo;
  143. /* Must be a valid range */
  144. if (targinfosize < sizeof(struct ip_nat_multi_range)) {
  145. DEBUGP("SNAT: Target size %u too smalln", targinfosize);
  146. return 0;
  147. }
  148. if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
  149.        + (sizeof(struct ip_nat_range)
  150.   * (mr->rangesize - 1))))) {
  151. DEBUGP("SNAT: Target size %u wrong for %u rangesn",
  152.        targinfosize, mr->rangesize);
  153. return 0;
  154. }
  155. /* Only allow these for NAT. */
  156. if (strcmp(tablename, "nat") != 0) {
  157. DEBUGP("SNAT: wrong table %sn", tablename);
  158. return 0;
  159. }
  160. if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
  161. DEBUGP("SNAT: hook mask 0x%x badn", hook_mask);
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. static int ipt_dnat_checkentry(const char *tablename,
  167.        const struct ipt_entry *e,
  168.        void *targinfo,
  169.        unsigned int targinfosize,
  170.        unsigned int hook_mask)
  171. {
  172. struct ip_nat_multi_range *mr = targinfo;
  173. /* Must be a valid range */
  174. if (targinfosize < sizeof(struct ip_nat_multi_range)) {
  175. DEBUGP("DNAT: Target size %u too smalln", targinfosize);
  176. return 0;
  177. }
  178. if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
  179.        + (sizeof(struct ip_nat_range)
  180.   * (mr->rangesize - 1))))) {
  181. DEBUGP("DNAT: Target size %u wrong for %u rangesn",
  182.        targinfosize, mr->rangesize);
  183. return 0;
  184. }
  185. /* Only allow these for NAT. */
  186. if (strcmp(tablename, "nat") != 0) {
  187. DEBUGP("DNAT: wrong table %sn", tablename);
  188. return 0;
  189. }
  190. if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
  191. DEBUGP("DNAT: hook mask 0x%x badn", hook_mask);
  192. return 0;
  193. }
  194. #ifndef CONFIG_IP_NF_NAT_LOCAL
  195. if (hook_mask & (1 << NF_IP_LOCAL_OUT)) {
  196. DEBUGP("DNAT: CONFIG_IP_NF_NAT_LOCAL not enabledn");
  197. return 0;
  198. }
  199. #endif
  200. return 1;
  201. }
  202. static inline unsigned int
  203. alloc_null_binding(struct ip_conntrack *conntrack,
  204.    struct ip_nat_info *info,
  205.    unsigned int hooknum)
  206. {
  207. /* Force range to this IP; let proto decide mapping for
  208.    per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
  209.    Use reply in case it's already been mangled (eg local packet).
  210. */
  211. u_int32_t ip
  212. = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
  213.    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
  214.    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
  215. struct ip_nat_multi_range mr
  216. = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
  217. DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)n", conntrack,
  218.        NIPQUAD(ip));
  219. return ip_nat_setup_info(conntrack, &mr, hooknum);
  220. }
  221. int ip_nat_rule_find(struct sk_buff **pskb,
  222.      unsigned int hooknum,
  223.      const struct net_device *in,
  224.      const struct net_device *out,
  225.      struct ip_conntrack *ct,
  226.      struct ip_nat_info *info)
  227. {
  228. int ret;
  229. ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
  230. if (ret == NF_ACCEPT) {
  231. if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
  232. /* NUL mapping */
  233. ret = alloc_null_binding(ct, info, hooknum);
  234. }
  235. return ret;
  236. }
  237. static struct ipt_target ipt_snat_reg
  238. = { { NULL, NULL }, "SNAT", ipt_snat_target, ipt_snat_checkentry, NULL };
  239. static struct ipt_target ipt_dnat_reg
  240. = { { NULL, NULL }, "DNAT", ipt_dnat_target, ipt_dnat_checkentry, NULL };
  241. int __init ip_nat_rule_init(void)
  242. {
  243. int ret;
  244. ret = ipt_register_table(&nat_table);
  245. if (ret != 0)
  246. return ret;
  247. ret = ipt_register_target(&ipt_snat_reg);
  248. if (ret != 0)
  249. goto unregister_table;
  250. ret = ipt_register_target(&ipt_dnat_reg);
  251. if (ret != 0)
  252. goto unregister_snat;
  253. return ret;
  254.  unregister_snat:
  255. ipt_unregister_target(&ipt_snat_reg);
  256.  unregister_table:
  257. ipt_unregister_table(&nat_table);
  258. return ret;
  259. }
  260. void ip_nat_rule_cleanup(void)
  261. {
  262. ipt_unregister_target(&ipt_dnat_reg);
  263. ipt_unregister_target(&ipt_snat_reg);
  264. ipt_unregister_table(&nat_table);
  265. }