blob: 1c666bab3269e667c6cb04a23ebd79dea816c6be [file] [log] [blame]
James Morris560ee652008-06-09 15:57:24 -07001/*
2 * "security" table
3 *
4 * This is for use by Mandatory Access Control (MAC) security models,
5 * which need to be able to manage security policy in separate context
6 * to DAC.
7 *
8 * Based on iptable_mangle.c
9 *
10 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
11 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam <at> netfilter.org>
12 * Copyright (C) 2008 Red Hat, Inc., James Morris <jmorris <at> redhat.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/module.h>
19#include <linux/netfilter_ipv4/ip_tables.h>
20#include <net/ip.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("James Morris <jmorris <at> redhat.com>");
24MODULE_DESCRIPTION("iptables security table, for MAC rules");
25
26#define SECURITY_VALID_HOOKS (1 << NF_INET_LOCAL_IN) | \
27 (1 << NF_INET_FORWARD) | \
28 (1 << NF_INET_LOCAL_OUT)
29
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020030static const struct
James Morris560ee652008-06-09 15:57:24 -070031{
32 struct ipt_replace repl;
33 struct ipt_standard entries[3];
34 struct ipt_error term;
Alexey Dobriyanf858b482008-07-26 17:48:38 -070035} initial_table __net_initdata = {
James Morris560ee652008-06-09 15:57:24 -070036 .repl = {
37 .name = "security",
38 .valid_hooks = SECURITY_VALID_HOOKS,
39 .num_entries = 4,
40 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
41 .hook_entry = {
42 [NF_INET_LOCAL_IN] = 0,
43 [NF_INET_FORWARD] = sizeof(struct ipt_standard),
44 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
45 },
46 .underflow = {
47 [NF_INET_LOCAL_IN] = 0,
48 [NF_INET_FORWARD] = sizeof(struct ipt_standard),
49 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
50 },
51 },
52 .entries = {
53 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
54 IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
55 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
56 },
57 .term = IPT_ERROR_INIT, /* ERROR */
58};
59
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020060static const struct xt_table security_table = {
James Morris560ee652008-06-09 15:57:24 -070061 .name = "security",
62 .valid_hooks = SECURITY_VALID_HOOKS,
James Morris560ee652008-06-09 15:57:24 -070063 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020064 .af = NFPROTO_IPV4,
James Morris560ee652008-06-09 15:57:24 -070065};
66
67static unsigned int
Jan Engelhardt737535c2009-06-13 06:46:36 +020068iptable_security_hook(unsigned int hook, struct sk_buff *skb,
69 const struct net_device *in,
70 const struct net_device *out,
71 int (*okfn)(struct sk_buff *))
James Morris560ee652008-06-09 15:57:24 -070072{
Jan Engelhardt737535c2009-06-13 06:46:36 +020073 if (hook == NF_INET_LOCAL_OUT) {
74 if (skb->len < sizeof(struct iphdr) ||
75 ip_hdrlen(skb) < sizeof(struct iphdr))
76 /* Somebody is playing with raw sockets. */
77 return NF_ACCEPT;
78
79 return ipt_do_table(skb, hook, in, out,
80 dev_net(out)->ipv4.iptable_security);
81 }
82
83 /* INPUT/FORWARD: */
James Morris560ee652008-06-09 15:57:24 -070084 return ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020085 dev_net(in)->ipv4.iptable_security);
James Morris560ee652008-06-09 15:57:24 -070086}
87
James Morris560ee652008-06-09 15:57:24 -070088static struct nf_hook_ops ipt_ops[] __read_mostly = {
89 {
Jan Engelhardt737535c2009-06-13 06:46:36 +020090 .hook = iptable_security_hook,
James Morris560ee652008-06-09 15:57:24 -070091 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +020092 .pf = NFPROTO_IPV4,
James Morris560ee652008-06-09 15:57:24 -070093 .hooknum = NF_INET_LOCAL_IN,
94 .priority = NF_IP_PRI_SECURITY,
95 },
96 {
Jan Engelhardt737535c2009-06-13 06:46:36 +020097 .hook = iptable_security_hook,
James Morris560ee652008-06-09 15:57:24 -070098 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +020099 .pf = NFPROTO_IPV4,
James Morris560ee652008-06-09 15:57:24 -0700100 .hooknum = NF_INET_FORWARD,
101 .priority = NF_IP_PRI_SECURITY,
102 },
103 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200104 .hook = iptable_security_hook,
James Morris560ee652008-06-09 15:57:24 -0700105 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200106 .pf = NFPROTO_IPV4,
James Morris560ee652008-06-09 15:57:24 -0700107 .hooknum = NF_INET_LOCAL_OUT,
108 .priority = NF_IP_PRI_SECURITY,
109 },
110};
111
112static int __net_init iptable_security_net_init(struct net *net)
113{
114 net->ipv4.iptable_security =
115 ipt_register_table(net, &security_table, &initial_table.repl);
116
117 if (IS_ERR(net->ipv4.iptable_security))
118 return PTR_ERR(net->ipv4.iptable_security);
119
120 return 0;
121}
122
123static void __net_exit iptable_security_net_exit(struct net *net)
124{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100125 ipt_unregister_table(net, net->ipv4.iptable_security);
James Morris560ee652008-06-09 15:57:24 -0700126}
127
128static struct pernet_operations iptable_security_net_ops = {
129 .init = iptable_security_net_init,
130 .exit = iptable_security_net_exit,
131};
132
133static int __init iptable_security_init(void)
134{
135 int ret;
136
137 ret = register_pernet_subsys(&iptable_security_net_ops);
138 if (ret < 0)
139 return ret;
140
141 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
142 if (ret < 0)
143 goto cleanup_table;
144
145 return ret;
146
147cleanup_table:
148 unregister_pernet_subsys(&iptable_security_net_ops);
149 return ret;
150}
151
152static void __exit iptable_security_fini(void)
153{
154 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
155 unregister_pernet_subsys(&iptable_security_net_ops);
156}
157
158module_init(iptable_security_init);
159module_exit(iptable_security_fini);