]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/xt_SECMARK.c
netfilter: xtables: move extension arguments into compound structure (4/6)
[karo-tx-linux.git] / net / netfilter / xt_SECMARK.c
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  *
8  * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
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
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
23 MODULE_DESCRIPTION("Xtables: packet security mark modification");
24 MODULE_ALIAS("ipt_SECMARK");
25 MODULE_ALIAS("ip6t_SECMARK");
26
27 #define PFX "SECMARK: "
28
29 static u8 mode;
30
31 static unsigned int
32 secmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
33 {
34         u32 secmark = 0;
35         const struct xt_secmark_target_info *info = par->targinfo;
36
37         BUG_ON(info->mode != mode);
38
39         switch (mode) {
40         case SECMARK_MODE_SEL:
41                 secmark = info->u.sel.selsid;
42                 break;
43
44         default:
45                 BUG();
46         }
47
48         skb->secmark = secmark;
49         return XT_CONTINUE;
50 }
51
52 static bool checkentry_selinux(struct xt_secmark_target_info *info)
53 {
54         int err;
55         struct xt_secmark_target_selinux_info *sel = &info->u.sel;
56
57         sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
58
59         err = selinux_string_to_sid(sel->selctx, &sel->selsid);
60         if (err) {
61                 if (err == -EINVAL)
62                         printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
63                                sel->selctx);
64                 return false;
65         }
66
67         if (!sel->selsid) {
68                 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
69                        sel->selctx);
70                 return false;
71         }
72
73         err = selinux_secmark_relabel_packet_permission(sel->selsid);
74         if (err) {
75                 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
76                 return false;
77         }
78
79         selinux_secmark_refcount_inc();
80         return true;
81 }
82
83 static bool
84 secmark_tg_check(const char *tablename, const void *entry,
85                  const struct xt_target *target, void *targinfo,
86                  unsigned int hook_mask)
87 {
88         struct xt_secmark_target_info *info = targinfo;
89
90         if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
91                 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
92                        "or \'security\' tables, not \'%s\'.\n", tablename);
93                 return false;
94         }
95
96         if (mode && mode != info->mode) {
97                 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
98                        "rules for mode %hu\n", mode, info->mode);
99                 return false;
100         }
101
102         switch (info->mode) {
103         case SECMARK_MODE_SEL:
104                 if (!checkentry_selinux(info))
105                         return false;
106                 break;
107
108         default:
109                 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
110                 return false;
111         }
112
113         if (!mode)
114                 mode = info->mode;
115         return true;
116 }
117
118 static void secmark_tg_destroy(const struct xt_target *target, void *targinfo)
119 {
120         switch (mode) {
121         case SECMARK_MODE_SEL:
122                 selinux_secmark_refcount_dec();
123         }
124 }
125
126 static struct xt_target secmark_tg_reg __read_mostly = {
127         .name       = "SECMARK",
128         .revision   = 0,
129         .family     = NFPROTO_UNSPEC,
130         .checkentry = secmark_tg_check,
131         .destroy    = secmark_tg_destroy,
132         .target     = secmark_tg,
133         .targetsize = sizeof(struct xt_secmark_target_info),
134         .me         = THIS_MODULE,
135 };
136
137 static int __init secmark_tg_init(void)
138 {
139         return xt_register_target(&secmark_tg_reg);
140 }
141
142 static void __exit secmark_tg_exit(void)
143 {
144         xt_unregister_target(&secmark_tg_reg);
145 }
146
147 module_init(secmark_tg_init);
148 module_exit(secmark_tg_exit);