blob: b54c3756fdc3b942be616d6905b2ca62cb4cc0b4 [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 */
18#include <linux/module.h>
19#include <linux/skbuff.h>
20#include <linux/netfilter/x_tables.h>
21#include <linux/netfilter/xt_CONNSECMARK.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070022#include <net/netfilter/nf_conntrack.h>
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080023#include <net/netfilter/nf_conntrack_ecache.h>
James Morris100468e2006-06-09 00:32:39 -070024
25#define PFX "CONNSECMARK: "
26
27MODULE_LICENSE("GPL");
28MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080029MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
James Morris100468e2006-06-09 00:32:39 -070030MODULE_ALIAS("ipt_CONNSECMARK");
31MODULE_ALIAS("ip6t_CONNSECMARK");
32
33/*
34 * If the packet has a security mark and the connection does not, copy
35 * the security mark from the packet to the connection.
36 */
Jan Engelhardta47362a2007-07-07 22:16:55 -070037static void secmark_save(const struct sk_buff *skb)
James Morris100468e2006-06-09 00:32:39 -070038{
39 if (skb->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070040 struct nf_conn *ct;
James Morris100468e2006-06-09 00:32:39 -070041 enum ip_conntrack_info ctinfo;
42
Patrick McHardy587aa642007-03-14 16:37:25 -070043 ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080044 if (ct && !ct->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070045 ct->secmark = skb->secmark;
Alexey Dobriyana71996f2008-10-08 11:35:07 +020046 nf_conntrack_event_cache(IPCT_SECMARK, ct);
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080047 }
James Morris100468e2006-06-09 00:32:39 -070048 }
49}
50
51/*
52 * If packet has no security mark, and the connection does, restore the
53 * security mark from the connection to the packet.
54 */
55static void secmark_restore(struct sk_buff *skb)
56{
57 if (!skb->secmark) {
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020058 const struct nf_conn *ct;
James Morris100468e2006-06-09 00:32:39 -070059 enum ip_conntrack_info ctinfo;
60
Patrick McHardy587aa642007-03-14 16:37:25 -070061 ct = nf_ct_get(skb, &ctinfo);
62 if (ct && ct->secmark)
63 skb->secmark = ct->secmark;
James Morris100468e2006-06-09 00:32:39 -070064 }
65}
66
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080067static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020068connsecmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
James Morris100468e2006-06-09 00:32:39 -070069{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020070 const struct xt_connsecmark_target_info *info = par->targinfo;
James Morris100468e2006-06-09 00:32:39 -070071
72 switch (info->mode) {
73 case CONNSECMARK_SAVE:
74 secmark_save(skb);
75 break;
76
77 case CONNSECMARK_RESTORE:
78 secmark_restore(skb);
79 break;
80
81 default:
82 BUG();
83 }
84
85 return XT_CONTINUE;
86}
87
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020088static bool connsecmark_tg_check(const struct xt_tgchk_param *par)
James Morris100468e2006-06-09 00:32:39 -070089{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020090 const struct xt_connsecmark_target_info *info = par->targinfo;
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) {
James Morris560ee652008-06-09 15:57:24 -070094 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020095 "or \'security\' tables, not \'%s\'.\n", par->table);
James Morris560ee652008-06-09 15:57:24 -070096 return false;
97 }
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:
105 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700106 return false;
James Morris100468e2006-06-09 00:32:39 -0700107 }
108
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200109 if (nf_ct_l3proto_try_module_get(par->family) < 0) {
Jan Engelhardt67b4af22007-12-01 00:01:50 +1100110 printk(KERN_WARNING "can't load conntrack support for "
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200111 "proto=%u\n", par->family);
Jan Engelhardt67b4af22007-12-01 00:01:50 +1100112 return false;
113 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700114 return true;
James Morris100468e2006-06-09 00:32:39 -0700115}
116
Jan Engelhardta2df1642008-10-08 11:35:19 +0200117static void connsecmark_tg_destroy(const struct xt_tgdtor_param *par)
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800118{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200119 nf_ct_l3proto_module_put(par->family);
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800120}
121
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200122static struct xt_target connsecmark_tg_reg __read_mostly = {
123 .name = "CONNSECMARK",
124 .revision = 0,
125 .family = NFPROTO_UNSPEC,
126 .checkentry = connsecmark_tg_check,
127 .destroy = connsecmark_tg_destroy,
128 .target = connsecmark_tg,
129 .targetsize = sizeof(struct xt_connsecmark_target_info),
130 .me = THIS_MODULE,
James Morris100468e2006-06-09 00:32:39 -0700131};
132
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800133static int __init connsecmark_tg_init(void)
James Morris100468e2006-06-09 00:32:39 -0700134{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200135 return xt_register_target(&connsecmark_tg_reg);
James Morris100468e2006-06-09 00:32:39 -0700136}
137
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800138static void __exit connsecmark_tg_exit(void)
James Morris100468e2006-06-09 00:32:39 -0700139{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200140 xt_unregister_target(&connsecmark_tg_reg);
James Morris100468e2006-06-09 00:32:39 -0700141}
142
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800143module_init(connsecmark_tg_init);
144module_exit(connsecmark_tg_exit);