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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Filtering ARP tables module.
  3.  *
  4.  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  5.  *
  6.  */
  7. #include <linux/module.h>
  8. #include <linux/netfilter_arp/arp_tables.h>
  9. #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT))
  10. /* Standard entry. */
  11. struct arpt_standard
  12. {
  13. struct arpt_entry entry;
  14. struct arpt_standard_target target;
  15. };
  16. struct arpt_error_target
  17. {
  18. struct arpt_entry_target target;
  19. char errorname[ARPT_FUNCTION_MAXNAMELEN];
  20. };
  21. struct arpt_error
  22. {
  23. struct arpt_entry entry;
  24. struct arpt_error_target target;
  25. };
  26. static struct
  27. {
  28. struct arpt_replace repl;
  29. struct arpt_standard entries[2];
  30. struct arpt_error term;
  31. } initial_table __initdata
  32. = { { "filter", FILTER_VALID_HOOKS, 3,
  33.       sizeof(struct arpt_standard) * 2 + sizeof(struct arpt_error),
  34.       { [NF_ARP_IN] 0,
  35. [NF_ARP_OUT] sizeof(struct arpt_standard) },
  36.       { [NF_ARP_IN] 0,
  37. [NF_ARP_OUT] sizeof(struct arpt_standard), },
  38.       0, NULL, { } },
  39.     {
  40.     /* ARP_IN */
  41.     {
  42.     {
  43.     {
  44.     { 0 }, { 0 }, { 0 }, { 0 },
  45.     0, 0,
  46.     { { 0, }, { 0, } },
  47.     { { 0, }, { 0, } },
  48.     0, 0,
  49.     0, 0,
  50.     0, 0,
  51.     "", "", { 0 }, { 0 },
  52.     0, 0
  53.     },
  54.     sizeof(struct arpt_entry),
  55.     sizeof(struct arpt_standard),
  56.     0,
  57.     { 0, 0 }, { } },
  58.     { { { { ARPT_ALIGN(sizeof(struct arpt_standard_target)), "" } }, { } },
  59.       -NF_ACCEPT - 1 }
  60.     },
  61.     /* ARP_OUT */
  62.     {
  63.     {
  64.     {
  65.     { 0 }, { 0 }, { 0 }, { 0 },
  66.     0, 0,
  67.     { { 0, }, { 0, } },
  68.     { { 0, }, { 0, } },
  69.     0, 0,
  70.     0, 0,
  71.     0, 0,
  72.     "", "", { 0 }, { 0 },
  73.     0, 0
  74.     },
  75.     sizeof(struct arpt_entry),
  76.     sizeof(struct arpt_standard),
  77.     0,
  78.     { 0, 0 }, { } },
  79.     { { { { ARPT_ALIGN(sizeof(struct arpt_standard_target)), "" } }, { } },
  80.       -NF_ACCEPT - 1 }
  81.     }
  82.     },
  83.     /* ERROR */
  84.     {
  85.     {
  86.     {
  87.     { 0 }, { 0 }, { 0 }, { 0 },
  88.     0, 0,
  89.     { { 0, }, { 0, } },
  90.     { { 0, }, { 0, } },
  91.     0, 0,
  92.     0, 0,
  93.     0, 0,
  94.     "", "", { 0 }, { 0 },
  95.     0, 0
  96.     },
  97.     sizeof(struct arpt_entry),
  98.     sizeof(struct arpt_error),
  99.     0,
  100.     { 0, 0 }, { } },
  101.     { { { { ARPT_ALIGN(sizeof(struct arpt_error_target)), ARPT_ERROR_TARGET } },
  102. { } },
  103.       "ERROR"
  104.     }
  105.     }
  106. };
  107. static struct arpt_table packet_filter
  108. = { { NULL, NULL }, "filter", &initial_table.repl,
  109.     FILTER_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
  110. /* The work comes in here from netfilter.c */
  111. static unsigned int arpt_hook(unsigned int hook,
  112.       struct sk_buff **pskb,
  113.       const struct net_device *in,
  114.       const struct net_device *out,
  115.       int (*okfn)(struct sk_buff *))
  116. {
  117. return arpt_do_table(pskb, hook, in, out, &packet_filter, NULL);
  118. }
  119. static struct nf_hook_ops arpt_ops[]
  120. = { { { NULL, NULL }, arpt_hook, NF_ARP, NF_ARP_IN, 0 },
  121.     { { NULL, NULL }, arpt_hook, NF_ARP, NF_ARP_OUT, 0 }
  122. };
  123. static int __init init(void)
  124. {
  125. int ret;
  126. /* Register table */
  127. ret = arpt_register_table(&packet_filter);
  128. if (ret < 0)
  129. return ret;
  130. /* Register hooks */
  131. ret = nf_register_hook(&arpt_ops[0]);
  132. if (ret < 0)
  133. goto cleanup_table;
  134. ret = nf_register_hook(&arpt_ops[1]);
  135. if (ret < 0)
  136. goto cleanup_hook0;
  137. return ret;
  138. cleanup_hook0:
  139. nf_unregister_hook(&arpt_ops[0]);
  140. cleanup_table:
  141. arpt_unregister_table(&packet_filter);
  142. return ret;
  143. }
  144. static void __exit fini(void)
  145. {
  146. unsigned int i;
  147. for (i = 0; i < sizeof(arpt_ops)/sizeof(struct nf_hook_ops); i++)
  148. nf_unregister_hook(&arpt_ops[i]);
  149. arpt_unregister_table(&packet_filter);
  150. }
  151. module_init(init);
  152. module_exit(fini);
  153. MODULE_LICENSE("GPL");