blob: 235806eb6ecd725c1b43d3598483922d339bd6e0 [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
Herbert Xu3db05fe2007-10-15 00:53:15 -070031static unsigned int target(struct sk_buff *skb, const struct net_device *in,
James Morris5e6874cd2006-06-09 00:30:57 -070032 const struct net_device *out, unsigned int hooknum,
33 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070034 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 Engelhardte1931b72007-07-07 22:16:26 -070084static bool checkentry(const char *tablename, const void *entry,
85 const struct xt_target *target, void *targinfo,
86 unsigned int hook_mask)
James Morris5e6874cd2006-06-09 00:30:57 -070087{
88 struct xt_secmark_target_info *info = targinfo;
89
90 if (mode && mode != info->mode) {
91 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
92 "rules for mode %hu\n", mode, info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -070093 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070094 }
95
96 switch (info->mode) {
97 case SECMARK_MODE_SEL:
98 if (!checkentry_selinux(info))
Jan Engelhardte1931b72007-07-07 22:16:26 -070099 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700100 break;
101
102 default:
103 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700104 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700105 }
106
107 if (!mode)
108 mode = info->mode;
Jan Engelhardte1931b72007-07-07 22:16:26 -0700109 return true;
James Morris5e6874cd2006-06-09 00:30:57 -0700110}
111
Patrick McHardy9f15c532007-07-07 22:22:02 -0700112static struct xt_target xt_secmark_target[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700113 {
114 .name = "SECMARK",
115 .family = AF_INET,
116 .checkentry = checkentry,
117 .target = target,
118 .targetsize = sizeof(struct xt_secmark_target_info),
119 .table = "mangle",
120 .me = THIS_MODULE,
121 },
122 {
123 .name = "SECMARK",
124 .family = AF_INET6,
125 .checkentry = checkentry,
126 .target = target,
127 .targetsize = sizeof(struct xt_secmark_target_info),
128 .table = "mangle",
129 .me = THIS_MODULE,
130 },
James Morris5e6874cd2006-06-09 00:30:57 -0700131};
132
133static int __init xt_secmark_init(void)
134{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700135 return xt_register_targets(xt_secmark_target,
136 ARRAY_SIZE(xt_secmark_target));
James Morris5e6874cd2006-06-09 00:30:57 -0700137}
138
139static void __exit xt_secmark_fini(void)
140{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700141 xt_unregister_targets(xt_secmark_target, ARRAY_SIZE(xt_secmark_target));
James Morris5e6874cd2006-06-09 00:30:57 -0700142}
143
144module_init(xt_secmark_init);
145module_exit(xt_secmark_fini);