• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <net/ip.h>
9 
10 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
11 
12 static struct
13 {
14 	struct ipt_replace repl;
15 	struct ipt_standard entries[2];
16 	struct ipt_error term;
17 } initial_table __net_initdata = {
18 	.repl = {
19 		.name = "raw",
20 		.valid_hooks = RAW_VALID_HOOKS,
21 		.num_entries = 3,
22 		.size = sizeof(struct ipt_standard) * 2 + sizeof(struct ipt_error),
23 		.hook_entry = {
24 			[NF_INET_PRE_ROUTING] = 0,
25 			[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
26 		},
27 		.underflow = {
28 			[NF_INET_PRE_ROUTING] = 0,
29 			[NF_INET_LOCAL_OUT]  = sizeof(struct ipt_standard)
30 		},
31 	},
32 	.entries = {
33 		IPT_STANDARD_INIT(NF_ACCEPT),	/* PRE_ROUTING */
34 		IPT_STANDARD_INIT(NF_ACCEPT),	/* LOCAL_OUT */
35 	},
36 	.term = IPT_ERROR_INIT,			/* ERROR */
37 };
38 
39 static struct xt_table packet_raw = {
40 	.name = "raw",
41 	.valid_hooks =  RAW_VALID_HOOKS,
42 	.lock = __RW_LOCK_UNLOCKED(packet_raw.lock),
43 	.me = THIS_MODULE,
44 	.af = AF_INET,
45 };
46 
47 /* The work comes in here from netfilter.c. */
48 static unsigned int
ipt_hook(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))49 ipt_hook(unsigned int hook,
50 	 struct sk_buff *skb,
51 	 const struct net_device *in,
52 	 const struct net_device *out,
53 	 int (*okfn)(struct sk_buff *))
54 {
55 	return ipt_do_table(skb, hook, in, out,
56 			    dev_net(in)->ipv4.iptable_raw);
57 }
58 
59 static unsigned int
ipt_local_hook(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))60 ipt_local_hook(unsigned int hook,
61 	       struct sk_buff *skb,
62 	       const struct net_device *in,
63 	       const struct net_device *out,
64 	       int (*okfn)(struct sk_buff *))
65 {
66 	/* root is playing with raw sockets. */
67 	if (skb->len < sizeof(struct iphdr) ||
68 	    ip_hdrlen(skb) < sizeof(struct iphdr))
69 		return NF_ACCEPT;
70 	return ipt_do_table(skb, hook, in, out,
71 			    dev_net(out)->ipv4.iptable_raw);
72 }
73 
74 /* 'raw' is the very first table. */
75 static struct nf_hook_ops ipt_ops[] __read_mostly = {
76 	{
77 		.hook = ipt_hook,
78 		.pf = PF_INET,
79 		.hooknum = NF_INET_PRE_ROUTING,
80 		.priority = NF_IP_PRI_RAW,
81 		.owner = THIS_MODULE,
82 	},
83 	{
84 		.hook = ipt_local_hook,
85 		.pf = PF_INET,
86 		.hooknum = NF_INET_LOCAL_OUT,
87 		.priority = NF_IP_PRI_RAW,
88 		.owner = THIS_MODULE,
89 	},
90 };
91 
iptable_raw_net_init(struct net * net)92 static int __net_init iptable_raw_net_init(struct net *net)
93 {
94 	/* Register table */
95 	net->ipv4.iptable_raw =
96 		ipt_register_table(net, &packet_raw, &initial_table.repl);
97 	if (IS_ERR(net->ipv4.iptable_raw))
98 		return PTR_ERR(net->ipv4.iptable_raw);
99 	return 0;
100 }
101 
iptable_raw_net_exit(struct net * net)102 static void __net_exit iptable_raw_net_exit(struct net *net)
103 {
104 	ipt_unregister_table(net->ipv4.iptable_raw);
105 }
106 
107 static struct pernet_operations iptable_raw_net_ops = {
108 	.init = iptable_raw_net_init,
109 	.exit = iptable_raw_net_exit,
110 };
111 
iptable_raw_init(void)112 static int __init iptable_raw_init(void)
113 {
114 	int ret;
115 
116 	ret = register_pernet_subsys(&iptable_raw_net_ops);
117 	if (ret < 0)
118 		return ret;
119 
120 	/* Register hooks */
121 	ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
122 	if (ret < 0)
123 		goto cleanup_table;
124 
125 	return ret;
126 
127  cleanup_table:
128 	unregister_pernet_subsys(&iptable_raw_net_ops);
129 	return ret;
130 }
131 
iptable_raw_fini(void)132 static void __exit iptable_raw_fini(void)
133 {
134 	nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
135 	unregister_pernet_subsys(&iptable_raw_net_ops);
136 }
137 
138 module_init(iptable_raw_init);
139 module_exit(iptable_raw_fini);
140 MODULE_LICENSE("GPL");
141