iptable_raw.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * 'raw' table, which is the very first hooked in at PRE_ROUTING and LOCAL_OUT .
  3. *
  4. * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/netfilter_ipv4/ip_tables.h>
  8. #include <linux/slab.h>
  9. #include <net/ip.h>
  10. #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
  11. static const struct xt_table packet_raw = {
  12. .name = "raw",
  13. .valid_hooks = RAW_VALID_HOOKS,
  14. .me = THIS_MODULE,
  15. .af = NFPROTO_IPV4,
  16. .priority = NF_IP_PRI_RAW,
  17. };
  18. /* The work comes in here from netfilter.c. */
  19. static unsigned int
  20. iptable_raw_hook(void *priv, struct sk_buff *skb,
  21. const struct nf_hook_state *state)
  22. {
  23. if (state->hook == NF_INET_LOCAL_OUT &&
  24. (skb->len < sizeof(struct iphdr) ||
  25. ip_hdrlen(skb) < sizeof(struct iphdr)))
  26. /* root is playing with raw sockets. */
  27. return NF_ACCEPT;
  28. return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
  29. }
  30. static struct nf_hook_ops *rawtable_ops __read_mostly;
  31. static int __net_init iptable_raw_net_init(struct net *net)
  32. {
  33. struct ipt_replace *repl;
  34. repl = ipt_alloc_initial_table(&packet_raw);
  35. if (repl == NULL)
  36. return -ENOMEM;
  37. net->ipv4.iptable_raw =
  38. ipt_register_table(net, &packet_raw, repl);
  39. kfree(repl);
  40. return PTR_ERR_OR_ZERO(net->ipv4.iptable_raw);
  41. }
  42. static void __net_exit iptable_raw_net_exit(struct net *net)
  43. {
  44. ipt_unregister_table(net, net->ipv4.iptable_raw);
  45. }
  46. static struct pernet_operations iptable_raw_net_ops = {
  47. .init = iptable_raw_net_init,
  48. .exit = iptable_raw_net_exit,
  49. };
  50. static int __init iptable_raw_init(void)
  51. {
  52. int ret;
  53. ret = register_pernet_subsys(&iptable_raw_net_ops);
  54. if (ret < 0)
  55. return ret;
  56. /* Register hooks */
  57. rawtable_ops = xt_hook_link(&packet_raw, iptable_raw_hook);
  58. if (IS_ERR(rawtable_ops)) {
  59. ret = PTR_ERR(rawtable_ops);
  60. unregister_pernet_subsys(&iptable_raw_net_ops);
  61. }
  62. return ret;
  63. }
  64. static void __exit iptable_raw_fini(void)
  65. {
  66. xt_hook_unlink(&packet_raw, rawtable_ops);
  67. unregister_pernet_subsys(&iptable_raw_net_ops);
  68. }
  69. module_init(iptable_raw_init);
  70. module_exit(iptable_raw_fini);
  71. MODULE_LICENSE("GPL");