2006-06-09 07:30:57 +00:00
|
|
|
/*
|
|
|
|
* Module for modifying the secmark field of the skb, for use by
|
|
|
|
* security subsystems.
|
|
|
|
*
|
|
|
|
* Based on the nfmark match by:
|
|
|
|
* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
|
|
|
|
*
|
2008-06-09 22:57:24 +00:00
|
|
|
* (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
|
2006-06-09 07:30:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
2010-03-17 15:04:40 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-06-09 07:30:57 +00:00
|
|
|
#include <linux/module.h>
|
2010-10-13 20:24:41 +00:00
|
|
|
#include <linux/security.h>
|
2006-06-09 07:30:57 +00:00
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_SECMARK.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: packet security mark modification");
|
2006-06-09 07:30:57 +00:00
|
|
|
MODULE_ALIAS("ipt_SECMARK");
|
|
|
|
MODULE_ALIAS("ip6t_SECMARK");
|
|
|
|
|
|
|
|
#define PFX "SECMARK: "
|
|
|
|
|
|
|
|
static u8 mode;
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static unsigned int
|
2009-07-05 17:43:26 +00:00
|
|
|
secmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
u32 secmark = 0;
|
2008-10-08 09:35:19 +00:00
|
|
|
const struct xt_secmark_target_info *info = par->targinfo;
|
2006-06-09 07:30:57 +00:00
|
|
|
|
|
|
|
BUG_ON(info->mode != mode);
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
2010-10-13 20:24:41 +00:00
|
|
|
secmark = info->secid;
|
2006-06-09 07:30:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2007-10-15 07:53:15 +00:00
|
|
|
skb->secmark = secmark;
|
2006-06-09 07:30:57 +00:00
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
static int checkentry_lsm(struct xt_secmark_target_info *info)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
int err;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
info->secctx[SECMARK_SECCTX_MAX - 1] = '\0';
|
|
|
|
info->secid = 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = security_secctx_to_secid(info->secctx, strlen(info->secctx),
|
|
|
|
&info->secid);
|
2006-06-09 07:30:57 +00:00
|
|
|
if (err) {
|
|
|
|
if (err == -EINVAL)
|
2010-10-13 20:24:41 +00:00
|
|
|
pr_info("invalid security context \'%s\'\n", info->secctx);
|
2010-03-19 16:32:59 +00:00
|
|
|
return err;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
if (!info->secid) {
|
|
|
|
pr_info("unable to map security context \'%s\'\n", info->secctx);
|
2010-03-19 16:32:59 +00:00
|
|
|
return -ENOENT;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = security_secmark_relabel_packet(info->secid);
|
2006-06-09 07:30:57 +00:00
|
|
|
if (err) {
|
2010-03-17 15:04:40 +00:00
|
|
|
pr_info("unable to obtain relabeling permission\n");
|
2010-03-19 16:32:59 +00:00
|
|
|
return err;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
security_secmark_refcount_inc();
|
2010-03-19 16:32:59 +00:00
|
|
|
return 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 16:16:42 +00:00
|
|
|
static int secmark_tg_check(const struct xt_tgchk_param *par)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2008-10-08 09:35:19 +00:00
|
|
|
struct xt_secmark_target_info *info = par->targinfo;
|
2010-03-19 16:32:59 +00:00
|
|
|
int err;
|
2006-06-09 07:30:57 +00:00
|
|
|
|
2008-10-08 09:35:19 +00:00
|
|
|
if (strcmp(par->table, "mangle") != 0 &&
|
|
|
|
strcmp(par->table, "security") != 0) {
|
2010-03-17 15:04:40 +00:00
|
|
|
pr_info("target only valid in the \'mangle\' "
|
|
|
|
"or \'security\' tables, not \'%s\'.\n", par->table);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2008-06-09 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2006-06-09 07:30:57 +00:00
|
|
|
if (mode && mode != info->mode) {
|
2010-03-17 15:04:40 +00:00
|
|
|
pr_info("mode already set to %hu cannot mix with "
|
|
|
|
"rules for mode %hu\n", mode, info->mode);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (info->mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
|
|
|
break;
|
|
|
|
default:
|
2010-03-17 15:04:40 +00:00
|
|
|
pr_info("invalid mode: %hu\n", info->mode);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = checkentry_lsm(info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2006-06-09 07:30:57 +00:00
|
|
|
if (!mode)
|
|
|
|
mode = info->mode;
|
2010-03-25 15:34:45 +00:00
|
|
|
return 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:19 +00:00
|
|
|
static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
|
2008-01-29 13:43:36 +00:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
2010-10-13 20:24:41 +00:00
|
|
|
security_secmark_refcount_dec();
|
2008-01-29 13:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:01 +00:00
|
|
|
static struct xt_target secmark_tg_reg __read_mostly = {
|
|
|
|
.name = "SECMARK",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.checkentry = secmark_tg_check,
|
|
|
|
.destroy = secmark_tg_destroy,
|
|
|
|
.target = secmark_tg,
|
|
|
|
.targetsize = sizeof(struct xt_secmark_target_info),
|
|
|
|
.me = THIS_MODULE,
|
2006-06-09 07:30:57 +00:00
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init secmark_tg_init(void)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2008-10-08 09:35:01 +00:00
|
|
|
return xt_register_target(&secmark_tg_reg);
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit secmark_tg_exit(void)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2008-10-08 09:35:01 +00:00
|
|
|
xt_unregister_target(&secmark_tg_reg);
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(secmark_tg_init);
|
|
|
|
module_exit(secmark_tg_exit);
|