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> |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 8 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 10 | #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] | 11 | |
| 12 | static struct |
| 13 | { |
| 14 | struct ipt_replace repl; |
| 15 | struct ipt_standard entries[2]; |
| 16 | struct ipt_error term; |
| 17 | } initial_table __initdata = { |
| 18 | .repl = { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 19 | .name = "raw", |
| 20 | .valid_hooks = RAW_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | .num_entries = 3, |
| 22 | .size = sizeof(struct ipt_standard) * 2 + sizeof(struct ipt_error), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 23 | .hook_entry = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 24 | [NF_INET_PRE_ROUTING] = 0, |
| 25 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 26 | }, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 27 | .underflow = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 28 | [NF_INET_PRE_ROUTING] = 0, |
| 29 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 30 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }, |
| 32 | .entries = { |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 33 | IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */ |
| 34 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }, |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 36 | .term = IPT_ERROR_INIT, /* ERROR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 39 | static struct xt_table packet_raw = { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 40 | .name = "raw", |
| 41 | .valid_hooks = RAW_VALID_HOOKS, |
| 42 | .lock = RW_LOCK_UNLOCKED, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | .me = THIS_MODULE, |
| 44 | .af = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* The work comes in here from netfilter.c. */ |
| 48 | static unsigned int |
| 49 | ipt_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 50 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | const struct net_device *in, |
| 52 | const struct net_device *out, |
| 53 | int (*okfn)(struct sk_buff *)) |
| 54 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 55 | return ipt_do_table(skb, hook, in, out, &packet_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 58 | static unsigned int |
| 59 | ipt_local_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 60 | struct sk_buff *skb, |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 61 | const struct net_device *in, |
| 62 | const struct net_device *out, |
| 63 | int (*okfn)(struct sk_buff *)) |
| 64 | { |
| 65 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 66 | if (skb->len < sizeof(struct iphdr) || |
| 67 | ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 68 | if (net_ratelimit()) |
Joe Perches | 464c4f1 | 2007-11-19 23:46:29 -0800 | [diff] [blame] | 69 | printk("iptable_raw: ignoring short SOCK_RAW " |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 70 | "packet.\n"); |
| 71 | return NF_ACCEPT; |
| 72 | } |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 73 | return ipt_do_table(skb, hook, in, out, &packet_raw); |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* 'raw' is the very first table. */ |
| 77 | static struct nf_hook_ops ipt_ops[] = { |
| 78 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 79 | .hook = ipt_hook, |
| 80 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 81 | .hooknum = NF_INET_PRE_ROUTING, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 82 | .priority = NF_IP_PRI_RAW, |
| 83 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }, |
| 85 | { |
Patrick McHardy | 802169a | 2007-05-10 14:17:36 -0700 | [diff] [blame] | 86 | .hook = ipt_local_hook, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 87 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 88 | .hooknum = NF_INET_LOCAL_OUT, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 89 | .priority = NF_IP_PRI_RAW, |
| 90 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }, |
| 92 | }; |
| 93 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 94 | static int __init iptable_raw_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | /* Register table */ |
| 99 | ret = ipt_register_table(&packet_raw, &initial_table.repl); |
| 100 | if (ret < 0) |
| 101 | return ret; |
| 102 | |
| 103 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 104 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (ret < 0) |
| 106 | goto cleanup_table; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return ret; |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | cleanup_table: |
| 111 | ipt_unregister_table(&packet_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return ret; |
| 113 | } |
| 114 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 115 | static void __exit iptable_raw_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 117 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | ipt_unregister_table(&packet_raw); |
| 119 | } |
| 120 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 121 | module_init(iptable_raw_init); |
| 122 | module_exit(iptable_raw_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | MODULE_LICENSE("GPL"); |