netfilter: xtables: move extension arguments into compound structure (3/6)
[deliverable/linux.git] / net / netfilter / xt_SECMARK.c
CommitLineData
5e6874cd
JM
1/*
2 * Module for modifying the secmark field of the skb, for use by
3 * security subsystems.
4 *
5 * Based on the nfmark match by:
6 * (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
7 *
560ee653 8 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
5e6874cd
JM
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/selinux.h>
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter/xt_SECMARK.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
2ae15b64 23MODULE_DESCRIPTION("Xtables: packet security mark modification");
5e6874cd
JM
24MODULE_ALIAS("ipt_SECMARK");
25MODULE_ALIAS("ip6t_SECMARK");
26
27#define PFX "SECMARK: "
28
29static u8 mode;
30
d3c5ee6d
JE
31static unsigned int
32secmark_tg(struct sk_buff *skb, const struct net_device *in,
33 const struct net_device *out, unsigned int hooknum,
34 const struct xt_target *target, const void *targinfo)
5e6874cd
JM
35{
36 u32 secmark = 0;
37 const struct xt_secmark_target_info *info = targinfo;
38
39 BUG_ON(info->mode != mode);
40
41 switch (mode) {
42 case SECMARK_MODE_SEL:
43 secmark = info->u.sel.selsid;
44 break;
45
46 default:
47 BUG();
48 }
49
3db05fea 50 skb->secmark = secmark;
5e6874cd
JM
51 return XT_CONTINUE;
52}
53
e1931b78 54static bool checkentry_selinux(struct xt_secmark_target_info *info)
5e6874cd
JM
55{
56 int err;
57 struct xt_secmark_target_selinux_info *sel = &info->u.sel;
601e68e1 58
a280b899 59 sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
5e6874cd
JM
60
61 err = selinux_string_to_sid(sel->selctx, &sel->selsid);
62 if (err) {
63 if (err == -EINVAL)
64 printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
65 sel->selctx);
e1931b78 66 return false;
5e6874cd
JM
67 }
68
69 if (!sel->selsid) {
70 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
71 sel->selctx);
e1931b78 72 return false;
5e6874cd
JM
73 }
74
d621d35e 75 err = selinux_secmark_relabel_packet_permission(sel->selsid);
5e6874cd
JM
76 if (err) {
77 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
e1931b78 78 return false;
5e6874cd
JM
79 }
80
d621d35e 81 selinux_secmark_refcount_inc();
e1931b78 82 return true;
5e6874cd
JM
83}
84
d3c5ee6d
JE
85static bool
86secmark_tg_check(const char *tablename, const void *entry,
87 const struct xt_target *target, void *targinfo,
88 unsigned int hook_mask)
5e6874cd
JM
89{
90 struct xt_secmark_target_info *info = targinfo;
91
560ee653
JM
92 if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
93 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
94 "or \'security\' tables, not \'%s\'.\n", tablename);
95 return false;
96 }
97
5e6874cd
JM
98 if (mode && mode != info->mode) {
99 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
100 "rules for mode %hu\n", mode, info->mode);
e1931b78 101 return false;
5e6874cd
JM
102 }
103
104 switch (info->mode) {
105 case SECMARK_MODE_SEL:
106 if (!checkentry_selinux(info))
e1931b78 107 return false;
5e6874cd
JM
108 break;
109
110 default:
111 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
e1931b78 112 return false;
5e6874cd
JM
113 }
114
115 if (!mode)
116 mode = info->mode;
e1931b78 117 return true;
5e6874cd
JM
118}
119
f51f5ec6 120static void secmark_tg_destroy(const struct xt_target *target, void *targinfo)
d621d35e
PM
121{
122 switch (mode) {
123 case SECMARK_MODE_SEL:
124 selinux_secmark_refcount_dec();
125 }
126}
127
55b69e91
JE
128static struct xt_target secmark_tg_reg __read_mostly = {
129 .name = "SECMARK",
130 .revision = 0,
131 .family = NFPROTO_UNSPEC,
132 .checkentry = secmark_tg_check,
133 .destroy = secmark_tg_destroy,
134 .target = secmark_tg,
135 .targetsize = sizeof(struct xt_secmark_target_info),
136 .me = THIS_MODULE,
5e6874cd
JM
137};
138
d3c5ee6d 139static int __init secmark_tg_init(void)
5e6874cd 140{
55b69e91 141 return xt_register_target(&secmark_tg_reg);
5e6874cd
JM
142}
143
d3c5ee6d 144static void __exit secmark_tg_exit(void)
5e6874cd 145{
55b69e91 146 xt_unregister_target(&secmark_tg_reg);
5e6874cd
JM
147}
148
d3c5ee6d
JE
149module_init(secmark_tg_init);
150module_exit(secmark_tg_exit);
This page took 0.906694 seconds and 5 git commands to generate.