xref: /openbmc/linux/net/ipv4/netfilter/iptable_raw.c (revision a8da474e)
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 
11 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
12 
13 static const struct xt_table packet_raw = {
14 	.name = "raw",
15 	.valid_hooks =  RAW_VALID_HOOKS,
16 	.me = THIS_MODULE,
17 	.af = NFPROTO_IPV4,
18 	.priority = NF_IP_PRI_RAW,
19 };
20 
21 /* The work comes in here from netfilter.c. */
22 static unsigned int
23 iptable_raw_hook(void *priv, struct sk_buff *skb,
24 		 const struct nf_hook_state *state)
25 {
26 	if (state->hook == NF_INET_LOCAL_OUT &&
27 	    (skb->len < sizeof(struct iphdr) ||
28 	     ip_hdrlen(skb) < sizeof(struct iphdr)))
29 		/* root is playing with raw sockets. */
30 		return NF_ACCEPT;
31 
32 	return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
33 }
34 
35 static struct nf_hook_ops *rawtable_ops __read_mostly;
36 
37 static int __net_init iptable_raw_net_init(struct net *net)
38 {
39 	struct ipt_replace *repl;
40 
41 	repl = ipt_alloc_initial_table(&packet_raw);
42 	if (repl == NULL)
43 		return -ENOMEM;
44 	net->ipv4.iptable_raw =
45 		ipt_register_table(net, &packet_raw, repl);
46 	kfree(repl);
47 	return PTR_ERR_OR_ZERO(net->ipv4.iptable_raw);
48 }
49 
50 static void __net_exit iptable_raw_net_exit(struct net *net)
51 {
52 	ipt_unregister_table(net, net->ipv4.iptable_raw);
53 }
54 
55 static struct pernet_operations iptable_raw_net_ops = {
56 	.init = iptable_raw_net_init,
57 	.exit = iptable_raw_net_exit,
58 };
59 
60 static int __init iptable_raw_init(void)
61 {
62 	int ret;
63 
64 	ret = register_pernet_subsys(&iptable_raw_net_ops);
65 	if (ret < 0)
66 		return ret;
67 
68 	/* Register hooks */
69 	rawtable_ops = xt_hook_link(&packet_raw, iptable_raw_hook);
70 	if (IS_ERR(rawtable_ops)) {
71 		ret = PTR_ERR(rawtable_ops);
72 		unregister_pernet_subsys(&iptable_raw_net_ops);
73 	}
74 
75 	return ret;
76 }
77 
78 static void __exit iptable_raw_fini(void)
79 {
80 	xt_hook_unlink(&packet_raw, rawtable_ops);
81 	unregister_pernet_subsys(&iptable_raw_net_ops);
82 }
83 
84 module_init(iptable_raw_init);
85 module_exit(iptable_raw_fini);
86 MODULE_LICENSE("GPL");
87