blob: b1bf3ca2c6c7fd0631396e9a9acf39555aa9cc55 [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,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020065 .priority = NF_IP_PRI_SECURITY,
James Morris560ee652008-06-09 15:57:24 -070066};
67
68static unsigned int
Jan Engelhardt737535c2009-06-13 06:46:36 +020069iptable_security_hook(unsigned int hook, struct sk_buff *skb,
70 const struct net_device *in,
71 const struct net_device *out,
72 int (*okfn)(struct sk_buff *))
James Morris560ee652008-06-09 15:57:24 -070073{
Jan Engelhardt2b21e052009-06-13 06:57:10 +020074 const struct net *net;
Jan Engelhardt737535c2009-06-13 06:46:36 +020075
Jan Engelhardt2b21e052009-06-13 06:57:10 +020076 if (hook == NF_INET_LOCAL_OUT &&
77 (skb->len < sizeof(struct iphdr) ||
78 ip_hdrlen(skb) < sizeof(struct iphdr)))
79 /* Somebody is playing with raw sockets. */
80 return NF_ACCEPT;
Jan Engelhardt737535c2009-06-13 06:46:36 +020081
Jan Engelhardt2b21e052009-06-13 06:57:10 +020082 net = dev_net((in != NULL) ? in : out);
83 return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_security);
James Morris560ee652008-06-09 15:57:24 -070084}
85
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020086static struct nf_hook_ops *sectbl_ops __read_mostly;
James Morris560ee652008-06-09 15:57:24 -070087
88static int __net_init iptable_security_net_init(struct net *net)
89{
90 net->ipv4.iptable_security =
91 ipt_register_table(net, &security_table, &initial_table.repl);
92
93 if (IS_ERR(net->ipv4.iptable_security))
94 return PTR_ERR(net->ipv4.iptable_security);
95
96 return 0;
97}
98
99static void __net_exit iptable_security_net_exit(struct net *net)
100{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100101 ipt_unregister_table(net, net->ipv4.iptable_security);
James Morris560ee652008-06-09 15:57:24 -0700102}
103
104static struct pernet_operations iptable_security_net_ops = {
105 .init = iptable_security_net_init,
106 .exit = iptable_security_net_exit,
107};
108
109static int __init iptable_security_init(void)
110{
111 int ret;
112
113 ret = register_pernet_subsys(&iptable_security_net_ops);
114 if (ret < 0)
115 return ret;
116
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200117 sectbl_ops = xt_hook_link(&security_table, iptable_security_hook);
118 if (IS_ERR(sectbl_ops)) {
119 ret = PTR_ERR(sectbl_ops);
James Morris560ee652008-06-09 15:57:24 -0700120 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200121 }
James Morris560ee652008-06-09 15:57:24 -0700122
123 return ret;
124
125cleanup_table:
126 unregister_pernet_subsys(&iptable_security_net_ops);
127 return ret;
128}
129
130static void __exit iptable_security_fini(void)
131{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200132 xt_hook_unlink(&security_table, sectbl_ops);
James Morris560ee652008-06-09 15:57:24 -0700133 unregister_pernet_subsys(&iptable_security_net_ops);
134}
135
136module_init(iptable_security_init);
137module_exit(iptable_security_fini);