2006-06-09 09:32:39 +02:00
|
|
|
/*
|
|
|
|
* This module is used to copy security markings from packets
|
|
|
|
* to connections, and restore security markings from connections
|
|
|
|
* back to packets. This would normally be performed in conjunction
|
|
|
|
* with the SECMARK target and state match.
|
|
|
|
*
|
|
|
|
* Based somewhat on CONNMARK:
|
|
|
|
* Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
|
|
|
|
* by Henrik Nordstrom <hno@marasystems.com>
|
|
|
|
*
|
2008-06-10 00:57:24 +02:00
|
|
|
* (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
|
2006-06-09 09:32:39 +02: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 16:04:40 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-06-09 09:32:39 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_CONNSECMARK.h>
|
2007-03-15 00:37:25 +01:00
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
2007-12-18 07:28:41 +01:00
|
|
|
#include <net/netfilter/nf_conntrack_ecache.h>
|
2006-06-09 09:32:39 +02:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
|
2006-06-09 09:32:39 +02:00
|
|
|
MODULE_ALIAS("ipt_CONNSECMARK");
|
|
|
|
MODULE_ALIAS("ip6t_CONNSECMARK");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the packet has a security mark and the connection does not, copy
|
|
|
|
* the security mark from the packet to the connection.
|
|
|
|
*/
|
2007-07-08 07:16:55 +02:00
|
|
|
static void secmark_save(const struct sk_buff *skb)
|
2006-06-09 09:32:39 +02:00
|
|
|
{
|
|
|
|
if (skb->secmark) {
|
2007-03-15 00:37:25 +01:00
|
|
|
struct nf_conn *ct;
|
2006-06-09 09:32:39 +02:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
|
2007-03-15 00:37:25 +01:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2007-12-18 07:28:41 +01:00
|
|
|
if (ct && !ct->secmark) {
|
2007-03-15 00:37:25 +01:00
|
|
|
ct->secmark = skb->secmark;
|
2008-10-08 11:35:07 +02:00
|
|
|
nf_conntrack_event_cache(IPCT_SECMARK, ct);
|
2007-12-18 07:28:41 +01:00
|
|
|
}
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If packet has no security mark, and the connection does, restore the
|
|
|
|
* security mark from the connection to the packet.
|
|
|
|
*/
|
|
|
|
static void secmark_restore(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
if (!skb->secmark) {
|
2008-04-14 09:56:05 +02:00
|
|
|
const struct nf_conn *ct;
|
2006-06-09 09:32:39 +02:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
|
2007-03-15 00:37:25 +01:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
if (ct && ct->secmark)
|
|
|
|
skb->secmark = ct->secmark;
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static unsigned int
|
2008-10-08 11:35:19 +02:00
|
|
|
connsecmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
|
2006-06-09 09:32:39 +02:00
|
|
|
{
|
2008-10-08 11:35:19 +02:00
|
|
|
const struct xt_connsecmark_target_info *info = par->targinfo;
|
2006-06-09 09:32:39 +02:00
|
|
|
|
|
|
|
switch (info->mode) {
|
|
|
|
case CONNSECMARK_SAVE:
|
|
|
|
secmark_save(skb);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNSECMARK_RESTORE:
|
|
|
|
secmark_restore(skb);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-03-19 17:16:42 +01:00
|
|
|
static int connsecmark_tg_check(const struct xt_tgchk_param *par)
|
2006-06-09 09:32:39 +02:00
|
|
|
{
|
2008-10-08 11:35:19 +02:00
|
|
|
const struct xt_connsecmark_target_info *info = par->targinfo;
|
2006-06-09 09:32:39 +02:00
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
if (strcmp(par->table, "mangle") != 0 &&
|
|
|
|
strcmp(par->table, "security") != 0) {
|
2010-03-17 16:04:40 +01:00
|
|
|
pr_info("target only valid in the \'mangle\' "
|
|
|
|
"or \'security\' tables, not \'%s\'.\n", par->table);
|
2010-03-25 16:34:45 +01:00
|
|
|
return -EINVAL;
|
2008-06-10 00:57:24 +02:00
|
|
|
}
|
|
|
|
|
2006-06-09 09:32:39 +02:00
|
|
|
switch (info->mode) {
|
|
|
|
case CONNSECMARK_SAVE:
|
|
|
|
case CONNSECMARK_RESTORE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-03-17 16:04:40 +01:00
|
|
|
pr_info("invalid mode: %hu\n", info->mode);
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:20 +02:00
|
|
|
if (nf_ct_l3proto_try_module_get(par->family) < 0) {
|
2010-03-17 16:04:40 +01:00
|
|
|
pr_info("cannot load conntrack support for proto=%u\n",
|
|
|
|
par->family);
|
2010-03-25 16:34:45 +01:00
|
|
|
return -EINVAL;
|
2007-11-30 14:01:50 +01:00
|
|
|
}
|
2010-03-25 16:34:45 +01:00
|
|
|
return 0;
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
static void connsecmark_tg_destroy(const struct xt_tgdtor_param *par)
|
2006-12-12 09:29:02 +01:00
|
|
|
{
|
2008-10-08 11:35:20 +02:00
|
|
|
nf_ct_l3proto_module_put(par->family);
|
2006-12-12 09:29:02 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:20 +02:00
|
|
|
static struct xt_target connsecmark_tg_reg __read_mostly = {
|
|
|
|
.name = "CONNSECMARK",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.checkentry = connsecmark_tg_check,
|
|
|
|
.destroy = connsecmark_tg_destroy,
|
|
|
|
.target = connsecmark_tg,
|
|
|
|
.targetsize = sizeof(struct xt_connsecmark_target_info),
|
|
|
|
.me = THIS_MODULE,
|
2006-06-09 09:32:39 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init connsecmark_tg_init(void)
|
2006-06-09 09:32:39 +02:00
|
|
|
{
|
2008-10-08 11:35:20 +02:00
|
|
|
return xt_register_target(&connsecmark_tg_reg);
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit connsecmark_tg_exit(void)
|
2006-06-09 09:32:39 +02:00
|
|
|
{
|
2008-10-08 11:35:20 +02:00
|
|
|
xt_unregister_target(&connsecmark_tg_reg);
|
2006-06-09 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(connsecmark_tg_init);
|
|
|
|
module_exit(connsecmark_tg_exit);
|