blob: 0d856fedfeb0c2d1ddc97cb5372fc33eda2f102b [file] [log] [blame]
James Morris17e6e592008-06-09 15:58:05 -07001/*
2 * "security" table for IPv6
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_ipv6/ip6_tables.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
James Morris17e6e592008-06-09 15:58:05 -070021
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("James Morris <jmorris <at> redhat.com>");
24MODULE_DESCRIPTION("ip6tables 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 xt_table security_table = {
James Morris17e6e592008-06-09 15:58:05 -070031 .name = "security",
32 .valid_hooks = SECURITY_VALID_HOOKS,
James Morris17e6e592008-06-09 15:58:05 -070033 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020034 .af = NFPROTO_IPV6,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020035 .priority = NF_IP6_PRI_SECURITY,
James Morris17e6e592008-06-09 15:58:05 -070036};
37
38static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -050039ip6table_security_hook(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040040 const struct nf_hook_state *state)
James Morris17e6e592008-06-09 15:58:05 -070041{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -050042 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_security);
James Morris17e6e592008-06-09 15:58:05 -070043}
44
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020045static struct nf_hook_ops *sectbl_ops __read_mostly;
James Morris17e6e592008-06-09 15:58:05 -070046
47static int __net_init ip6table_security_net_init(struct net *net)
48{
Jan Engelhardte3eaa992009-06-17 22:14:54 +020049 struct ip6t_replace *repl;
James Morris17e6e592008-06-09 15:58:05 -070050
Jan Engelhardte3eaa992009-06-17 22:14:54 +020051 repl = ip6t_alloc_initial_table(&security_table);
52 if (repl == NULL)
53 return -ENOMEM;
54 net->ipv6.ip6table_security =
55 ip6t_register_table(net, &security_table, repl);
56 kfree(repl);
Rusty Russell8c6ffba2013-07-15 11:20:32 +093057 return PTR_ERR_OR_ZERO(net->ipv6.ip6table_security);
James Morris17e6e592008-06-09 15:58:05 -070058}
59
60static void __net_exit ip6table_security_net_exit(struct net *net)
61{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +010062 ip6t_unregister_table(net, net->ipv6.ip6table_security);
James Morris17e6e592008-06-09 15:58:05 -070063}
64
65static struct pernet_operations ip6table_security_net_ops = {
66 .init = ip6table_security_net_init,
67 .exit = ip6table_security_net_exit,
68};
69
70static int __init ip6table_security_init(void)
71{
72 int ret;
73
74 ret = register_pernet_subsys(&ip6table_security_net_ops);
75 if (ret < 0)
76 return ret;
77
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020078 sectbl_ops = xt_hook_link(&security_table, ip6table_security_hook);
79 if (IS_ERR(sectbl_ops)) {
80 ret = PTR_ERR(sectbl_ops);
James Morris17e6e592008-06-09 15:58:05 -070081 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020082 }
James Morris17e6e592008-06-09 15:58:05 -070083
84 return ret;
85
86cleanup_table:
87 unregister_pernet_subsys(&ip6table_security_net_ops);
88 return ret;
89}
90
91static void __exit ip6table_security_fini(void)
92{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020093 xt_hook_unlink(&security_table, sectbl_ops);
James Morris17e6e592008-06-09 15:58:05 -070094 unregister_pernet_subsys(&ip6table_security_net_ops);
95}
96
97module_init(ip6table_security_init);
98module_exit(ip6table_security_fini);