ip_nat_rule.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:9k
源码类别:

嵌入式Linux

开发平台:

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. LIST_HEAD(nat_expect_list);
  100. /* Source NAT */
  101. static unsigned int ipt_snat_target(struct sk_buff **pskb,
  102.     unsigned int hooknum,
  103.     const struct net_device *in,
  104.     const struct net_device *out,
  105.     const void *targinfo,
  106.     void *userinfo)
  107. {
  108. struct ip_conntrack *ct;
  109. enum ip_conntrack_info ctinfo;
  110. IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
  111. ct = ip_conntrack_get(*pskb, &ctinfo);
  112. /* Connection must be valid and new. */
  113. IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
  114. IP_NF_ASSERT(out);
  115. return ip_nat_setup_info(ct, targinfo, hooknum);
  116. }
  117. static unsigned int ipt_dnat_target(struct sk_buff **pskb,
  118.     unsigned int hooknum,
  119.     const struct net_device *in,
  120.     const struct net_device *out,
  121.     const void *targinfo,
  122.     void *userinfo)
  123. {
  124. struct ip_conntrack *ct;
  125. enum ip_conntrack_info ctinfo;
  126. IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
  127.      || hooknum == NF_IP_LOCAL_OUT);
  128. ct = ip_conntrack_get(*pskb, &ctinfo);
  129. /* Connection must be valid and new. */
  130. IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
  131. return ip_nat_setup_info(ct, targinfo, hooknum);
  132. }
  133. static int ipt_snat_checkentry(const char *tablename,
  134.        const struct ipt_entry *e,
  135.        void *targinfo,
  136.        unsigned int targinfosize,
  137.        unsigned int hook_mask)
  138. {
  139. struct ip_nat_multi_range *mr = targinfo;
  140. /* Must be a valid range */
  141. if (targinfosize < sizeof(struct ip_nat_multi_range)) {
  142. DEBUGP("SNAT: Target size %u too smalln", targinfosize);
  143. return 0;
  144. }
  145. if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
  146.        + (sizeof(struct ip_nat_range)
  147.   * (mr->rangesize - 1))))) {
  148. DEBUGP("SNAT: Target size %u wrong for %u rangesn",
  149.        targinfosize, mr->rangesize);
  150. return 0;
  151. }
  152. /* Only allow these for NAT. */
  153. if (strcmp(tablename, "nat") != 0) {
  154. DEBUGP("SNAT: wrong table %sn", tablename);
  155. return 0;
  156. }
  157. if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
  158. DEBUGP("SNAT: hook mask 0x%x badn", hook_mask);
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. static int ipt_dnat_checkentry(const char *tablename,
  164.        const struct ipt_entry *e,
  165.        void *targinfo,
  166.        unsigned int targinfosize,
  167.        unsigned int hook_mask)
  168. {
  169. struct ip_nat_multi_range *mr = targinfo;
  170. /* Must be a valid range */
  171. if (targinfosize < sizeof(struct ip_nat_multi_range)) {
  172. DEBUGP("DNAT: Target size %u too smalln", targinfosize);
  173. return 0;
  174. }
  175. if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
  176.        + (sizeof(struct ip_nat_range)
  177.   * (mr->rangesize - 1))))) {
  178. DEBUGP("DNAT: Target size %u wrong for %u rangesn",
  179.        targinfosize, mr->rangesize);
  180. return 0;
  181. }
  182. /* Only allow these for NAT. */
  183. if (strcmp(tablename, "nat") != 0) {
  184. DEBUGP("SNAT: wrong table %sn", tablename);
  185. return 0;
  186. }
  187. if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
  188. DEBUGP("DNAT: hook mask 0x%x badn", hook_mask);
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. static inline unsigned int
  194. alloc_null_binding(struct ip_conntrack *conntrack,
  195.    struct ip_nat_info *info,
  196.    unsigned int hooknum)
  197. {
  198. /* Force range to this IP; let proto decide mapping for
  199.    per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
  200.    Use reply in case it's already been mangled (eg local packet).
  201. */
  202. u_int32_t ip
  203. = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
  204.    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
  205.    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
  206. struct ip_nat_multi_range mr
  207. = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
  208. DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)n", conntrack,
  209.        NIPQUAD(ip));
  210. return ip_nat_setup_info(conntrack, &mr, hooknum);
  211. }
  212. static inline int call_expect(const struct ip_nat_expect *i,
  213.       struct sk_buff **pskb,
  214.       unsigned int hooknum,
  215.       struct ip_conntrack *ct,
  216.       struct ip_nat_info *info,
  217.       struct ip_conntrack *master,
  218.       struct ip_nat_info *masterinfo,
  219.       unsigned int *verdict)
  220. {
  221. return i->expect(pskb, hooknum, ct, info, master, masterinfo,
  222.  verdict);
  223. }
  224. int ip_nat_rule_find(struct sk_buff **pskb,
  225.      unsigned int hooknum,
  226.      const struct net_device *in,
  227.      const struct net_device *out,
  228.      struct ip_conntrack *ct,
  229.      struct ip_nat_info *info)
  230. {
  231. int ret;
  232. /* Master won't vanish while this ctrack still alive */
  233. if (ct->master.master) {
  234. struct ip_conntrack *master;
  235. master = (struct ip_conntrack *)ct->master.master;
  236. if (LIST_FIND(&nat_expect_list,
  237.       call_expect,
  238.       struct ip_nat_expect *,
  239.       pskb, hooknum, ct, info,
  240.       master, &master->nat.info, &ret))
  241. return ret;
  242. }
  243. ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
  244. if (ret == NF_ACCEPT) {
  245. if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
  246. /* NUL mapping */
  247. ret = alloc_null_binding(ct, info, hooknum);
  248. }
  249. return ret;
  250. }
  251. int ip_nat_expect_register(struct ip_nat_expect *expect)
  252. {
  253. WRITE_LOCK(&ip_nat_lock);
  254. list_prepend(&nat_expect_list, expect);
  255. WRITE_UNLOCK(&ip_nat_lock);
  256. return 0;
  257. }
  258. void ip_nat_expect_unregister(struct ip_nat_expect *expect)
  259. {
  260. WRITE_LOCK(&ip_nat_lock);
  261. LIST_DELETE(&nat_expect_list, expect);
  262. WRITE_UNLOCK(&ip_nat_lock);
  263. }
  264. static struct ipt_target ipt_snat_reg
  265. = { { NULL, NULL }, "SNAT", ipt_snat_target, ipt_snat_checkentry, NULL };
  266. static struct ipt_target ipt_dnat_reg
  267. = { { NULL, NULL }, "DNAT", ipt_dnat_target, ipt_dnat_checkentry, NULL };
  268. int __init ip_nat_rule_init(void)
  269. {
  270. int ret;
  271. ret = ipt_register_table(&nat_table);
  272. if (ret != 0)
  273. return ret;
  274. ret = ipt_register_target(&ipt_snat_reg);
  275. if (ret != 0)
  276. goto unregister_table;
  277. ret = ipt_register_target(&ipt_dnat_reg);
  278. if (ret != 0)
  279. goto unregister_snat;
  280. return ret;
  281.  unregister_snat:
  282. ipt_unregister_target(&ipt_snat_reg);
  283.  unregister_table:
  284. ipt_unregister_table(&nat_table);
  285. return ret;
  286. }
  287. void ip_nat_rule_cleanup(void)
  288. {
  289. ipt_unregister_target(&ipt_dnat_reg);
  290. ipt_unregister_target(&ipt_snat_reg);
  291. ipt_unregister_table(&nat_table);
  292. }