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

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
  3.  *
  4.  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  5.  *
  6.  * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
  7.  */
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/netfilter_ipv4/ip_tables.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/skbuff.h>
  13. #include <net/sock.h>
  14. #include <net/route.h>
  15. #include <linux/ip.h>
  16. #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | 
  17.     (1 << NF_IP_LOCAL_IN) | 
  18.     (1 << NF_IP_FORWARD) | 
  19.     (1 << NF_IP_LOCAL_OUT) | 
  20.     (1 << NF_IP_POST_ROUTING))
  21. /* Standard entry. */
  22. struct ipt_standard
  23. {
  24. struct ipt_entry entry;
  25. struct ipt_standard_target target;
  26. };
  27. struct ipt_error_target
  28. {
  29. struct ipt_entry_target target;
  30. char errorname[IPT_FUNCTION_MAXNAMELEN];
  31. };
  32. struct ipt_error
  33. {
  34. struct ipt_entry entry;
  35. struct ipt_error_target target;
  36. };
  37. /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
  38. static struct
  39. {
  40. struct ipt_replace repl;
  41. struct ipt_standard entries[5];
  42. struct ipt_error term;
  43. } initial_table __initdata
  44. = { { "mangle", MANGLE_VALID_HOOKS, 6,
  45.       sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
  46.       { [NF_IP_PRE_ROUTING]  0,
  47. [NF_IP_LOCAL_IN]  sizeof(struct ipt_standard),
  48. [NF_IP_FORWARD]  sizeof(struct ipt_standard) * 2,
  49. [NF_IP_LOCAL_OUT]  sizeof(struct ipt_standard) * 3,
  50. [NF_IP_POST_ROUTING]  sizeof(struct ipt_standard) * 4 },
  51.       { [NF_IP_PRE_ROUTING]  0,
  52. [NF_IP_LOCAL_IN]  sizeof(struct ipt_standard),
  53. [NF_IP_FORWARD]  sizeof(struct ipt_standard) * 2,
  54. [NF_IP_LOCAL_OUT]  sizeof(struct ipt_standard) * 3,
  55. [NF_IP_POST_ROUTING] sizeof(struct ipt_standard) * 4 },
  56.       0, NULL, { } },
  57.     {
  58.     /* PRE_ROUTING */
  59.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  60. 0,
  61. sizeof(struct ipt_entry),
  62. sizeof(struct ipt_standard),
  63. 0, { 0, 0 }, { } },
  64.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  65. -NF_ACCEPT - 1 } },
  66.     /* LOCAL_IN */
  67.       { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  68. 0,
  69. sizeof(struct ipt_entry),
  70. sizeof(struct ipt_standard),
  71. 0, { 0, 0 }, { } },
  72.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  73. -NF_ACCEPT - 1 } },
  74.     /* FORWARD */
  75.       { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  76. 0,
  77. sizeof(struct ipt_entry),
  78. sizeof(struct ipt_standard),
  79. 0, { 0, 0 }, { } },
  80.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  81. -NF_ACCEPT - 1 } },
  82.     /* LOCAL_OUT */
  83.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  84. 0,
  85. sizeof(struct ipt_entry),
  86. sizeof(struct ipt_standard),
  87. 0, { 0, 0 }, { } },
  88.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  89. -NF_ACCEPT - 1 } },
  90.     /* POST_ROUTING */
  91.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  92. 0,
  93. sizeof(struct ipt_entry),
  94. sizeof(struct ipt_standard),
  95. 0, { 0, 0 }, { } },
  96.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  97. -NF_ACCEPT - 1 } },
  98.     },
  99.     /* ERROR */
  100.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  101. 0,
  102. sizeof(struct ipt_entry),
  103. sizeof(struct ipt_error),
  104. 0, { 0, 0 }, { } },
  105.       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
  106.   { } },
  107. "ERROR"
  108.       }
  109.     }
  110. };
  111. static struct ipt_table packet_mangler
  112. = { { NULL, NULL }, "mangle", &initial_table.repl,
  113.     MANGLE_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
  114. /* The work comes in here from netfilter.c. */
  115. static unsigned int
  116. ipt_route_hook(unsigned int hook,
  117.  struct sk_buff **pskb,
  118.  const struct net_device *in,
  119.  const struct net_device *out,
  120.  int (*okfn)(struct sk_buff *))
  121. {
  122. return ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
  123. }
  124. static unsigned int
  125. ipt_local_hook(unsigned int hook,
  126.    struct sk_buff **pskb,
  127.    const struct net_device *in,
  128.    const struct net_device *out,
  129.    int (*okfn)(struct sk_buff *))
  130. {
  131. unsigned int ret;
  132. u_int8_t tos;
  133. u_int32_t saddr, daddr;
  134. unsigned long nfmark;
  135. /* root is playing with raw sockets. */
  136. if ((*pskb)->len < sizeof(struct iphdr)
  137.     || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
  138. if (net_ratelimit())
  139. printk("ipt_hook: happy cracking.n");
  140. return NF_ACCEPT;
  141. }
  142. /* Save things which could affect route */
  143. nfmark = (*pskb)->nfmark;
  144. saddr = (*pskb)->nh.iph->saddr;
  145. daddr = (*pskb)->nh.iph->daddr;
  146. tos = (*pskb)->nh.iph->tos;
  147. ret = ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
  148. /* Reroute for ANY change. */
  149. if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
  150.     && ((*pskb)->nh.iph->saddr != saddr
  151. || (*pskb)->nh.iph->daddr != daddr
  152. || (*pskb)->nfmark != nfmark
  153. || (*pskb)->nh.iph->tos != tos))
  154. return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
  155. return ret;
  156. }
  157. static struct nf_hook_ops ipt_ops[]
  158. = { { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_PRE_ROUTING, 
  159. NF_IP_PRI_MANGLE },
  160.     { { NULL, NULL }, ipt_local_hook, PF_INET, NF_IP_LOCAL_IN,
  161. NF_IP_PRI_MANGLE },
  162.     { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_FORWARD,
  163. NF_IP_PRI_MANGLE },
  164.     { { NULL, NULL }, ipt_local_hook, PF_INET, NF_IP_LOCAL_OUT,
  165. NF_IP_PRI_MANGLE },
  166.     { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_POST_ROUTING,
  167. NF_IP_PRI_MANGLE }
  168. };
  169. static int __init init(void)
  170. {
  171. int ret;
  172. /* Register table */
  173. ret = ipt_register_table(&packet_mangler);
  174. if (ret < 0)
  175. return ret;
  176. /* Register hooks */
  177. ret = nf_register_hook(&ipt_ops[0]);
  178. if (ret < 0)
  179. goto cleanup_table;
  180. ret = nf_register_hook(&ipt_ops[1]);
  181. if (ret < 0)
  182. goto cleanup_hook0;
  183. ret = nf_register_hook(&ipt_ops[2]);
  184. if (ret < 0)
  185. goto cleanup_hook1;
  186. ret = nf_register_hook(&ipt_ops[3]);
  187. if (ret < 0)
  188. goto cleanup_hook2;
  189. ret = nf_register_hook(&ipt_ops[4]);
  190. if (ret < 0)
  191. goto cleanup_hook3;
  192. return ret;
  193.  cleanup_hook3:
  194.         nf_unregister_hook(&ipt_ops[3]);
  195.  cleanup_hook2:
  196.         nf_unregister_hook(&ipt_ops[2]);
  197.  cleanup_hook1:
  198. nf_unregister_hook(&ipt_ops[1]);
  199.  cleanup_hook0:
  200. nf_unregister_hook(&ipt_ops[0]);
  201.  cleanup_table:
  202. ipt_unregister_table(&packet_mangler);
  203. return ret;
  204. }
  205. static void __exit fini(void)
  206. {
  207. unsigned int i;
  208. for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
  209. nf_unregister_hook(&ipt_ops[i]);
  210. ipt_unregister_table(&packet_mangler);
  211. }
  212. module_init(init);
  213. module_exit(fini);
  214. MODULE_LICENSE("GPL");