blob: 9faf5e050b796186b3204a02ece181726a26cb1a [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 *
James Morris560ee652008-06-09 15:57:24 -07008 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
James Morris5e6874cd2006-06-09 00:30:57 -07009 *
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 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
James Morris5e6874cd2006-06-09 00:30:57 -070016#include <linux/module.h>
Eric Paris2606fd12010-10-13 16:24:41 -040017#include <linux/security.h>
James Morris5e6874cd2006-06-09 00:30:57 -070018#include <linux/skbuff.h>
James Morris5e6874cd2006-06-09 00:30:57 -070019#include <linux/netfilter/x_tables.h>
20#include <linux/netfilter/xt_SECMARK.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080024MODULE_DESCRIPTION("Xtables: packet security mark modification");
James Morris5e6874cd2006-06-09 00:30:57 -070025MODULE_ALIAS("ipt_SECMARK");
26MODULE_ALIAS("ip6t_SECMARK");
27
28#define PFX "SECMARK: "
29
30static u8 mode;
31
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080032static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020033secmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
James Morris5e6874cd2006-06-09 00:30:57 -070034{
35 u32 secmark = 0;
Jan Engelhardt7eb35582008-10-08 11:35:19 +020036 const struct xt_secmark_target_info *info = par->targinfo;
James Morris5e6874cd2006-06-09 00:30:57 -070037
38 BUG_ON(info->mode != mode);
39
40 switch (mode) {
41 case SECMARK_MODE_SEL:
Eric Paris2606fd12010-10-13 16:24:41 -040042 secmark = info->secid;
James Morris5e6874cd2006-06-09 00:30:57 -070043 break;
James Morris5e6874cd2006-06-09 00:30:57 -070044 default:
45 BUG();
46 }
47
Herbert Xu3db05fe2007-10-15 00:53:15 -070048 skb->secmark = secmark;
James Morris5e6874cd2006-06-09 00:30:57 -070049 return XT_CONTINUE;
50}
51
Eric Paris2606fd12010-10-13 16:24:41 -040052static int checkentry_lsm(struct xt_secmark_target_info *info)
James Morris5e6874cd2006-06-09 00:30:57 -070053{
54 int err;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080055
Eric Paris2606fd12010-10-13 16:24:41 -040056 info->secctx[SECMARK_SECCTX_MAX - 1] = '\0';
57 info->secid = 0;
James Morris5e6874cd2006-06-09 00:30:57 -070058
Eric Paris2606fd12010-10-13 16:24:41 -040059 err = security_secctx_to_secid(info->secctx, strlen(info->secctx),
60 &info->secid);
James Morris5e6874cd2006-06-09 00:30:57 -070061 if (err) {
62 if (err == -EINVAL)
Eric Paris2606fd12010-10-13 16:24:41 -040063 pr_info("invalid security context \'%s\'\n", info->secctx);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010064 return err;
James Morris5e6874cd2006-06-09 00:30:57 -070065 }
66
Eric Paris2606fd12010-10-13 16:24:41 -040067 if (!info->secid) {
68 pr_info("unable to map security context \'%s\'\n", info->secctx);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010069 return -ENOENT;
James Morris5e6874cd2006-06-09 00:30:57 -070070 }
71
Eric Paris2606fd12010-10-13 16:24:41 -040072 err = security_secmark_relabel_packet(info->secid);
James Morris5e6874cd2006-06-09 00:30:57 -070073 if (err) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010074 pr_info("unable to obtain relabeling permission\n");
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010075 return err;
James Morris5e6874cd2006-06-09 00:30:57 -070076 }
77
Eric Paris2606fd12010-10-13 16:24:41 -040078 security_secmark_refcount_inc();
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010079 return 0;
James Morris5e6874cd2006-06-09 00:30:57 -070080}
81
Jan Engelhardt135367b2010-03-19 17:16:42 +010082static int secmark_tg_check(const struct xt_tgchk_param *par)
James Morris5e6874cd2006-06-09 00:30:57 -070083{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020084 struct xt_secmark_target_info *info = par->targinfo;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010085 int err;
James Morris5e6874cd2006-06-09 00:30:57 -070086
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020087 if (strcmp(par->table, "mangle") != 0 &&
88 strcmp(par->table, "security") != 0) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010089 pr_info("target only valid in the \'mangle\' "
90 "or \'security\' tables, not \'%s\'.\n", par->table);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010091 return -EINVAL;
James Morris560ee652008-06-09 15:57:24 -070092 }
93
James Morris5e6874cd2006-06-09 00:30:57 -070094 if (mode && mode != info->mode) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010095 pr_info("mode already set to %hu cannot mix with "
96 "rules for mode %hu\n", mode, info->mode);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010097 return -EINVAL;
James Morris5e6874cd2006-06-09 00:30:57 -070098 }
99
100 switch (info->mode) {
101 case SECMARK_MODE_SEL:
James Morris5e6874cd2006-06-09 00:30:57 -0700102 break;
James Morris5e6874cd2006-06-09 00:30:57 -0700103 default:
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100104 pr_info("invalid mode: %hu\n", info->mode);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100105 return -EINVAL;
James Morris5e6874cd2006-06-09 00:30:57 -0700106 }
107
Eric Paris2606fd12010-10-13 16:24:41 -0400108 err = checkentry_lsm(info);
109 if (err)
110 return err;
111
James Morris5e6874cd2006-06-09 00:30:57 -0700112 if (!mode)
113 mode = info->mode;
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100114 return 0;
James Morris5e6874cd2006-06-09 00:30:57 -0700115}
116
Jan Engelhardta2df1642008-10-08 11:35:19 +0200117static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
Paul Moored621d352008-01-29 08:43:36 -0500118{
119 switch (mode) {
120 case SECMARK_MODE_SEL:
Eric Paris2606fd12010-10-13 16:24:41 -0400121 security_secmark_refcount_dec();
Paul Moored621d352008-01-29 08:43:36 -0500122 }
123}
124
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200125static struct xt_target secmark_tg_reg __read_mostly = {
126 .name = "SECMARK",
127 .revision = 0,
128 .family = NFPROTO_UNSPEC,
129 .checkentry = secmark_tg_check,
130 .destroy = secmark_tg_destroy,
131 .target = secmark_tg,
132 .targetsize = sizeof(struct xt_secmark_target_info),
133 .me = THIS_MODULE,
James Morris5e6874cd2006-06-09 00:30:57 -0700134};
135
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800136static int __init secmark_tg_init(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700137{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200138 return xt_register_target(&secmark_tg_reg);
James Morris5e6874cd2006-06-09 00:30:57 -0700139}
140
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141static void __exit secmark_tg_exit(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700142{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200143 xt_unregister_target(&secmark_tg_reg);
James Morris5e6874cd2006-06-09 00:30:57 -0700144}
145
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800146module_init(secmark_tg_init);
147module_exit(secmark_tg_exit);