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

嵌入式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. #include <linux/module.h>
  7. #include <linux/netfilter_ipv4/ip_tables.h>
  8. #define FILTER_VALID_HOOKS ((1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT))
  9. /* Standard entry. */
  10. struct ipt_standard
  11. {
  12. struct ipt_entry entry;
  13. struct ipt_standard_target target;
  14. };
  15. struct ipt_error_target
  16. {
  17. struct ipt_entry_target target;
  18. char errorname[IPT_FUNCTION_MAXNAMELEN];
  19. };
  20. struct ipt_error
  21. {
  22. struct ipt_entry entry;
  23. struct ipt_error_target target;
  24. };
  25. static struct
  26. {
  27. struct ipt_replace repl;
  28. struct ipt_standard entries[3];
  29. struct ipt_error term;
  30. } initial_table __initdata
  31. = { { "filter", FILTER_VALID_HOOKS, 4,
  32.       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
  33.       { [NF_IP_LOCAL_IN] 0,
  34. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  35. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  36.       { [NF_IP_LOCAL_IN] 0,
  37. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  38. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  39.       0, NULL, { } },
  40.     {
  41.     /* LOCAL_IN */
  42.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  43. 0,
  44. sizeof(struct ipt_entry),
  45. sizeof(struct ipt_standard),
  46. 0, { 0, 0 }, { } },
  47.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  48. -NF_ACCEPT - 1 } },
  49.     /* FORWARD */
  50.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  51. 0,
  52. sizeof(struct ipt_entry),
  53. sizeof(struct ipt_standard),
  54. 0, { 0, 0 }, { } },
  55.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  56. -NF_ACCEPT - 1 } },
  57.     /* LOCAL_OUT */
  58.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  59. 0,
  60. sizeof(struct ipt_entry),
  61. sizeof(struct ipt_standard),
  62. 0, { 0, 0 }, { } },
  63.       { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  64. -NF_ACCEPT - 1 } }
  65.     },
  66.     /* ERROR */
  67.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  68. 0,
  69. sizeof(struct ipt_entry),
  70. sizeof(struct ipt_error),
  71. 0, { 0, 0 }, { } },
  72.       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
  73.   { } },
  74. "ERROR"
  75.       }
  76.     }
  77. };
  78. static struct ipt_table packet_filter
  79. = { { NULL, NULL }, "filter", &initial_table.repl,
  80.     FILTER_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
  81. /* The work comes in here from netfilter.c. */
  82. static unsigned int
  83. ipt_hook(unsigned int hook,
  84.  struct sk_buff **pskb,
  85.  const struct net_device *in,
  86.  const struct net_device *out,
  87.  int (*okfn)(struct sk_buff *))
  88. {
  89. return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
  90. }
  91. static unsigned int
  92. ipt_local_out_hook(unsigned int hook,
  93.    struct sk_buff **pskb,
  94.    const struct net_device *in,
  95.    const struct net_device *out,
  96.    int (*okfn)(struct sk_buff *))
  97. {
  98. /* root is playing with raw sockets. */
  99. if ((*pskb)->len < sizeof(struct iphdr)
  100.     || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
  101. if (net_ratelimit())
  102. printk("ipt_hook: happy cracking.n");
  103. return NF_ACCEPT;
  104. }
  105. return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
  106. }
  107. static struct nf_hook_ops ipt_ops[]
  108. = { { { NULL, NULL }, ipt_hook, PF_INET, NF_IP_LOCAL_IN, NF_IP_PRI_FILTER },
  109.     { { NULL, NULL }, ipt_hook, PF_INET, NF_IP_FORWARD, NF_IP_PRI_FILTER },
  110.     { { NULL, NULL }, ipt_local_out_hook, PF_INET, NF_IP_LOCAL_OUT,
  111. NF_IP_PRI_FILTER }
  112. };
  113. /* Default to forward because I got too much mail already. */
  114. static int forward = NF_ACCEPT;
  115. MODULE_PARM(forward, "i");
  116. static int __init init(void)
  117. {
  118. int ret;
  119. if (forward < 0 || forward > NF_MAX_VERDICT) {
  120. printk("iptables forward must be 0 or 1n");
  121. return -EINVAL;
  122. }
  123. /* Entry 1 is the FORWARD hook */
  124. initial_table.entries[1].target.verdict = -forward - 1;
  125. /* Register table */
  126. ret = ipt_register_table(&packet_filter);
  127. if (ret < 0)
  128. return ret;
  129. /* Register hooks */
  130. ret = nf_register_hook(&ipt_ops[0]);
  131. if (ret < 0)
  132. goto cleanup_table;
  133. ret = nf_register_hook(&ipt_ops[1]);
  134. if (ret < 0)
  135. goto cleanup_hook0;
  136. ret = nf_register_hook(&ipt_ops[2]);
  137. if (ret < 0)
  138. goto cleanup_hook1;
  139. return ret;
  140.  cleanup_hook1:
  141. nf_unregister_hook(&ipt_ops[1]);
  142.  cleanup_hook0:
  143. nf_unregister_hook(&ipt_ops[0]);
  144.  cleanup_table:
  145. ipt_unregister_table(&packet_filter);
  146. return ret;
  147. }
  148. static void __exit fini(void)
  149. {
  150. unsigned int i;
  151. for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
  152. nf_unregister_hook(&ipt_ops[i]);
  153. ipt_unregister_table(&packet_filter);
  154. }
  155. module_init(init);
  156. module_exit(fini);
  157. MODULE_LICENSE("GPL");