blob: 7d5439c9f181d38726b21088d89575d3ba3c81aa [file] [log] [blame]
James Morris5e6874cd2006-06-09 00:30:57 -07001/*
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 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
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
23MODULE_DESCRIPTION("ip[6]tables SECMARK modification module");
24MODULE_ALIAS("ipt_SECMARK");
25MODULE_ALIAS("ip6t_SECMARK");
26
27#define PFX "SECMARK: "
28
29static u8 mode;
30
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080031static 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)
James Morris5e6874cd2006-06-09 00:30:57 -070035{
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
Herbert Xu3db05fe2007-10-15 00:53:15 -070050 skb->secmark = secmark;
James Morris5e6874cd2006-06-09 00:30:57 -070051 return XT_CONTINUE;
52}
53
Jan Engelhardte1931b72007-07-07 22:16:26 -070054static bool checkentry_selinux(struct xt_secmark_target_info *info)
James Morris5e6874cd2006-06-09 00:30:57 -070055{
56 int err;
57 struct xt_secmark_target_selinux_info *sel = &info->u.sel;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080058
James Morrisa280b892006-07-30 20:46:38 -070059 sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
James Morris5e6874cd2006-06-09 00:30:57 -070060
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);
Jan Engelhardte1931b72007-07-07 22:16:26 -070066 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070067 }
68
69 if (!sel->selsid) {
70 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
71 sel->selctx);
Jan Engelhardte1931b72007-07-07 22:16:26 -070072 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070073 }
74
75 err = selinux_relabel_packet_permission(sel->selsid);
76 if (err) {
77 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070078 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070079 }
80
Jan Engelhardte1931b72007-07-07 22:16:26 -070081 return true;
James Morris5e6874cd2006-06-09 00:30:57 -070082}
83
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084static bool
85secmark_tg_check(const char *tablename, const void *entry,
86 const struct xt_target *target, void *targinfo,
87 unsigned int hook_mask)
James Morris5e6874cd2006-06-09 00:30:57 -070088{
89 struct xt_secmark_target_info *info = targinfo;
90
91 if (mode && mode != info->mode) {
92 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
93 "rules for mode %hu\n", mode, info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -070094 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070095 }
96
97 switch (info->mode) {
98 case SECMARK_MODE_SEL:
99 if (!checkentry_selinux(info))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700100 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700101 break;
102
103 default:
104 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700105 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700106 }
107
108 if (!mode)
109 mode = info->mode;
Jan Engelhardte1931b72007-07-07 22:16:26 -0700110 return true;
James Morris5e6874cd2006-06-09 00:30:57 -0700111}
112
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800113static struct xt_target secmark_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700114 {
115 .name = "SECMARK",
116 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117 .checkentry = secmark_tg_check,
118 .target = secmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700119 .targetsize = sizeof(struct xt_secmark_target_info),
120 .table = "mangle",
121 .me = THIS_MODULE,
122 },
123 {
124 .name = "SECMARK",
125 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800126 .checkentry = secmark_tg_check,
127 .target = secmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700128 .targetsize = sizeof(struct xt_secmark_target_info),
129 .table = "mangle",
130 .me = THIS_MODULE,
131 },
James Morris5e6874cd2006-06-09 00:30:57 -0700132};
133
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134static int __init secmark_tg_init(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700135{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800136 return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
James Morris5e6874cd2006-06-09 00:30:57 -0700137}
138
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800139static void __exit secmark_tg_exit(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700140{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141 xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
James Morris5e6874cd2006-06-09 00:30:57 -0700142}
143
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144module_init(secmark_tg_init);
145module_exit(secmark_tg_exit);