iptable_filter.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/netfilter_ipv4/ip_tables.h>
  15. #include <linux/slab.h>
  16. #include <net/ip.h>
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  19. MODULE_DESCRIPTION("iptables filter table");
  20. #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
  21. (1 << NF_INET_FORWARD) | \
  22. (1 << NF_INET_LOCAL_OUT))
  23. static const struct xt_table packet_filter = {
  24. .name = "filter",
  25. .valid_hooks = FILTER_VALID_HOOKS,
  26. .me = THIS_MODULE,
  27. .af = NFPROTO_IPV4,
  28. .priority = NF_IP_PRI_FILTER,
  29. };
  30. static unsigned int
  31. iptable_filter_hook(void *priv, struct sk_buff *skb,
  32. const struct nf_hook_state *state)
  33. {
  34. if (state->hook == NF_INET_LOCAL_OUT &&
  35. (skb->len < sizeof(struct iphdr) ||
  36. ip_hdrlen(skb) < sizeof(struct iphdr)))
  37. /* root is playing with raw sockets. */
  38. return NF_ACCEPT;
  39. return ipt_do_table(skb, state, state->net->ipv4.iptable_filter);
  40. }
  41. static struct nf_hook_ops *filter_ops __read_mostly;
  42. /* Default to forward because I got too much mail already. */
  43. static bool forward = true;
  44. module_param(forward, bool, 0000);
  45. static int __net_init iptable_filter_net_init(struct net *net)
  46. {
  47. struct ipt_replace *repl;
  48. repl = ipt_alloc_initial_table(&packet_filter);
  49. if (repl == NULL)
  50. return -ENOMEM;
  51. /* Entry 1 is the FORWARD hook */
  52. ((struct ipt_standard *)repl->entries)[1].target.verdict =
  53. forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
  54. net->ipv4.iptable_filter =
  55. ipt_register_table(net, &packet_filter, repl);
  56. kfree(repl);
  57. return PTR_ERR_OR_ZERO(net->ipv4.iptable_filter);
  58. }
  59. static void __net_exit iptable_filter_net_exit(struct net *net)
  60. {
  61. ipt_unregister_table(net, net->ipv4.iptable_filter);
  62. }
  63. static struct pernet_operations iptable_filter_net_ops = {
  64. .init = iptable_filter_net_init,
  65. .exit = iptable_filter_net_exit,
  66. };
  67. static int __init iptable_filter_init(void)
  68. {
  69. int ret;
  70. ret = register_pernet_subsys(&iptable_filter_net_ops);
  71. if (ret < 0)
  72. return ret;
  73. /* Register hooks */
  74. filter_ops = xt_hook_link(&packet_filter, iptable_filter_hook);
  75. if (IS_ERR(filter_ops)) {
  76. ret = PTR_ERR(filter_ops);
  77. unregister_pernet_subsys(&iptable_filter_net_ops);
  78. }
  79. return ret;
  80. }
  81. static void __exit iptable_filter_fini(void)
  82. {
  83. xt_hook_unlink(&packet_filter, filter_ops);
  84. unregister_pernet_subsys(&iptable_filter_net_ops);
  85. }
  86. module_init(iptable_filter_init);
  87. module_exit(iptable_filter_fini);