YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 9 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 11 | #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 13 | static const struct xt_table packet_raw = { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 14 | .name = "raw", |
| 15 | .valid_hooks = RAW_VALID_HOOKS, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 17 | .af = NFPROTO_IPV4, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 18 | .priority = NF_IP_PRI_RAW, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | /* The work comes in here from netfilter.c. */ |
| 22 | static unsigned int |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 23 | iptable_raw_hook(unsigned int hook, struct sk_buff *skb, |
| 24 | const struct net_device *in, const struct net_device *out, |
| 25 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Jan Engelhardt | 2b21e05 | 2009-06-13 06:57:10 +0200 | [diff] [blame] | 27 | const struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Jan Engelhardt | 2b21e05 | 2009-06-13 06:57:10 +0200 | [diff] [blame] | 29 | if (hook == NF_INET_LOCAL_OUT && |
| 30 | (skb->len < sizeof(struct iphdr) || |
| 31 | ip_hdrlen(skb) < sizeof(struct iphdr))) |
| 32 | /* root is playing with raw sockets. */ |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 33 | return NF_ACCEPT; |
Jan Engelhardt | 2b21e05 | 2009-06-13 06:57:10 +0200 | [diff] [blame] | 34 | |
| 35 | net = dev_net((in != NULL) ? in : out); |
| 36 | return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_raw); |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 39 | static struct nf_hook_ops *rawtable_ops __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 41 | static int __net_init iptable_raw_net_init(struct net *net) |
| 42 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 43 | struct ipt_replace *repl; |
| 44 | |
| 45 | repl = ipt_alloc_initial_table(&packet_raw); |
| 46 | if (repl == NULL) |
| 47 | return -ENOMEM; |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 48 | net->ipv4.iptable_raw = |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 49 | ipt_register_table(net, &packet_raw, repl); |
| 50 | kfree(repl); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 51 | if (IS_ERR(net->ipv4.iptable_raw)) |
| 52 | return PTR_ERR(net->ipv4.iptable_raw); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void __net_exit iptable_raw_net_exit(struct net *net) |
| 57 | { |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 58 | ipt_unregister_table(net, net->ipv4.iptable_raw); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static struct pernet_operations iptable_raw_net_ops = { |
| 62 | .init = iptable_raw_net_init, |
| 63 | .exit = iptable_raw_net_exit, |
| 64 | }; |
| 65 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 66 | static int __init iptable_raw_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | int ret; |
| 69 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 70 | ret = register_pernet_subsys(&iptable_raw_net_ops); |
| 71 | if (ret < 0) |
| 72 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* Register hooks */ |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 75 | rawtable_ops = xt_hook_link(&packet_raw, iptable_raw_hook); |
| 76 | if (IS_ERR(rawtable_ops)) { |
| 77 | ret = PTR_ERR(rawtable_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | goto cleanup_table; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return ret; |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | cleanup_table: |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 84 | unregister_pernet_subsys(&iptable_raw_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return ret; |
| 86 | } |
| 87 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 88 | static void __exit iptable_raw_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 90 | xt_hook_unlink(&packet_raw, rawtable_ops); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 91 | unregister_pernet_subsys(&iptable_raw_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 94 | module_init(iptable_raw_init); |
| 95 | module_exit(iptable_raw_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | MODULE_LICENSE("GPL"); |