blob: e04dc282e3bba104d361cbc07e450ffdf3c2c3a0 [file] [log] [blame]
James Morris100468e2006-06-09 00:32:39 -07001/*
2 * This module is used to copy security markings from packets
3 * to connections, and restore security markings from connections
4 * back to packets. This would normally be performed in conjunction
5 * with the SECMARK target and state match.
6 *
7 * Based somewhat on CONNMARK:
8 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
9 * by Henrik Nordstrom <hno@marasystems.com>
10 *
James Morris560ee652008-06-09 15:57:24 -070011 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
James Morris100468e2006-06-09 00:32:39 -070012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
James Morris100468e2006-06-09 00:32:39 -070019#include <linux/module.h>
20#include <linux/skbuff.h>
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_CONNSECMARK.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070023#include <net/netfilter/nf_conntrack.h>
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080024#include <net/netfilter/nf_conntrack_ecache.h>
James Morris100468e2006-06-09 00:32:39 -070025
James Morris100468e2006-06-09 00:32:39 -070026MODULE_LICENSE("GPL");
27MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080028MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
James Morris100468e2006-06-09 00:32:39 -070029MODULE_ALIAS("ipt_CONNSECMARK");
30MODULE_ALIAS("ip6t_CONNSECMARK");
31
32/*
33 * If the packet has a security mark and the connection does not, copy
34 * the security mark from the packet to the connection.
35 */
Jan Engelhardta47362a2007-07-07 22:16:55 -070036static void secmark_save(const struct sk_buff *skb)
James Morris100468e2006-06-09 00:32:39 -070037{
38 if (skb->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070039 struct nf_conn *ct;
James Morris100468e2006-06-09 00:32:39 -070040 enum ip_conntrack_info ctinfo;
41
Patrick McHardy587aa642007-03-14 16:37:25 -070042 ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080043 if (ct && !ct->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070044 ct->secmark = skb->secmark;
Alexey Dobriyana71996f2008-10-08 11:35:07 +020045 nf_conntrack_event_cache(IPCT_SECMARK, ct);
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080046 }
James Morris100468e2006-06-09 00:32:39 -070047 }
48}
49
50/*
51 * If packet has no security mark, and the connection does, restore the
52 * security mark from the connection to the packet.
53 */
54static void secmark_restore(struct sk_buff *skb)
55{
56 if (!skb->secmark) {
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020057 const struct nf_conn *ct;
James Morris100468e2006-06-09 00:32:39 -070058 enum ip_conntrack_info ctinfo;
59
Patrick McHardy587aa642007-03-14 16:37:25 -070060 ct = nf_ct_get(skb, &ctinfo);
61 if (ct && ct->secmark)
62 skb->secmark = ct->secmark;
James Morris100468e2006-06-09 00:32:39 -070063 }
64}
65
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080066static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020067connsecmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
James Morris100468e2006-06-09 00:32:39 -070068{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020069 const struct xt_connsecmark_target_info *info = par->targinfo;
James Morris100468e2006-06-09 00:32:39 -070070
71 switch (info->mode) {
72 case CONNSECMARK_SAVE:
73 secmark_save(skb);
74 break;
75
76 case CONNSECMARK_RESTORE:
77 secmark_restore(skb);
78 break;
79
80 default:
81 BUG();
82 }
83
84 return XT_CONTINUE;
85}
86
Jan Engelhardt135367b2010-03-19 17:16:42 +010087static int connsecmark_tg_check(const struct xt_tgchk_param *par)
James Morris100468e2006-06-09 00:32:39 -070088{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020089 const struct xt_connsecmark_target_info *info = par->targinfo;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010090 int ret;
James Morris100468e2006-06-09 00:32:39 -070091
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020092 if (strcmp(par->table, "mangle") != 0 &&
93 strcmp(par->table, "security") != 0) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010094 pr_info("target only valid in the \'mangle\' "
95 "or \'security\' tables, not \'%s\'.\n", par->table);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010096 return -EINVAL;
James Morris560ee652008-06-09 15:57:24 -070097 }
98
James Morris100468e2006-06-09 00:32:39 -070099 switch (info->mode) {
100 case CONNSECMARK_SAVE:
101 case CONNSECMARK_RESTORE:
102 break;
103
104 default:
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100105 pr_info("invalid mode: %hu\n", info->mode);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100106 return -EINVAL;
James Morris100468e2006-06-09 00:32:39 -0700107 }
108
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100109 ret = nf_ct_l3proto_try_module_get(par->family);
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100110 if (ret < 0)
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100111 pr_info("cannot load conntrack support for proto=%u\n",
112 par->family);
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100113 return ret;
James Morris100468e2006-06-09 00:32:39 -0700114}
115
Jan Engelhardta2df1642008-10-08 11:35:19 +0200116static void connsecmark_tg_destroy(const struct xt_tgdtor_param *par)
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800117{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200118 nf_ct_l3proto_module_put(par->family);
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800119}
120
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200121static struct xt_target connsecmark_tg_reg __read_mostly = {
122 .name = "CONNSECMARK",
123 .revision = 0,
124 .family = NFPROTO_UNSPEC,
125 .checkentry = connsecmark_tg_check,
126 .destroy = connsecmark_tg_destroy,
127 .target = connsecmark_tg,
128 .targetsize = sizeof(struct xt_connsecmark_target_info),
129 .me = THIS_MODULE,
James Morris100468e2006-06-09 00:32:39 -0700130};
131
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132static int __init connsecmark_tg_init(void)
James Morris100468e2006-06-09 00:32:39 -0700133{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200134 return xt_register_target(&connsecmark_tg_reg);
James Morris100468e2006-06-09 00:32:39 -0700135}
136
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800137static void __exit connsecmark_tg_exit(void)
James Morris100468e2006-06-09 00:32:39 -0700138{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200139 xt_unregister_target(&connsecmark_tg_reg);
James Morris100468e2006-06-09 00:32:39 -0700140}
141
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800142module_init(connsecmark_tg_init);
143module_exit(connsecmark_tg_exit);