blob: 021b5c8d20e29a294b6df311850ced35c0743933 [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 *
11 * (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 *
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>
James Morris100468e2006-06-09 00:32:39 -070023
24#define PFX "CONNSECMARK: "
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
28MODULE_DESCRIPTION("ip[6]tables CONNSECMARK module");
29MODULE_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);
43 if (ct && !ct->secmark)
44 ct->secmark = skb->secmark;
James Morris100468e2006-06-09 00:32:39 -070045 }
46}
47
48/*
49 * If packet has no security mark, and the connection does, restore the
50 * security mark from the connection to the packet.
51 */
52static void secmark_restore(struct sk_buff *skb)
53{
54 if (!skb->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070055 struct nf_conn *ct;
James Morris100468e2006-06-09 00:32:39 -070056 enum ip_conntrack_info ctinfo;
57
Patrick McHardy587aa642007-03-14 16:37:25 -070058 ct = nf_ct_get(skb, &ctinfo);
59 if (ct && ct->secmark)
60 skb->secmark = ct->secmark;
James Morris100468e2006-06-09 00:32:39 -070061 }
62}
63
Herbert Xu3db05fe2007-10-15 00:53:15 -070064static unsigned int target(struct sk_buff *skb, const struct net_device *in,
James Morris100468e2006-06-09 00:32:39 -070065 const struct net_device *out, unsigned int hooknum,
66 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070067 const void *targinfo)
James Morris100468e2006-06-09 00:32:39 -070068{
James Morris100468e2006-06-09 00:32:39 -070069 const struct xt_connsecmark_target_info *info = targinfo;
70
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 Engelhardte1931b72007-07-07 22:16:26 -070087static bool checkentry(const char *tablename, const void *entry,
88 const struct xt_target *target, void *targinfo,
89 unsigned int hook_mask)
James Morris100468e2006-06-09 00:32:39 -070090{
Jan Engelhardta47362a2007-07-07 22:16:55 -070091 const struct xt_connsecmark_target_info *info = targinfo;
James Morris100468e2006-06-09 00:32:39 -070092
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -080093 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
94 printk(KERN_WARNING "can't load conntrack support for "
95 "proto=%d\n", target->family);
Jan Engelhardte1931b72007-07-07 22:16:26 -070096 return false;
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -080097 }
James Morris100468e2006-06-09 00:32:39 -070098 switch (info->mode) {
99 case CONNSECMARK_SAVE:
100 case CONNSECMARK_RESTORE:
101 break;
102
103 default:
104 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700105 return false;
James Morris100468e2006-06-09 00:32:39 -0700106 }
107
Jan Engelhardte1931b72007-07-07 22:16:26 -0700108 return true;
James Morris100468e2006-06-09 00:32:39 -0700109}
110
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800111static void
112destroy(const struct xt_target *target, void *targinfo)
113{
114 nf_ct_l3proto_module_put(target->family);
115}
116
Patrick McHardy9f15c532007-07-07 22:22:02 -0700117static struct xt_target xt_connsecmark_target[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700118 {
119 .name = "CONNSECMARK",
120 .family = AF_INET,
121 .checkentry = checkentry,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800122 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700123 .target = target,
124 .targetsize = sizeof(struct xt_connsecmark_target_info),
125 .table = "mangle",
126 .me = THIS_MODULE,
127 },
128 {
129 .name = "CONNSECMARK",
130 .family = AF_INET6,
131 .checkentry = checkentry,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800132 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700133 .target = target,
134 .targetsize = sizeof(struct xt_connsecmark_target_info),
135 .table = "mangle",
136 .me = THIS_MODULE,
137 },
James Morris100468e2006-06-09 00:32:39 -0700138};
139
140static int __init xt_connsecmark_init(void)
141{
Thomas Graf28094862006-08-22 13:52:17 -0700142 return xt_register_targets(xt_connsecmark_target,
143 ARRAY_SIZE(xt_connsecmark_target));
James Morris100468e2006-06-09 00:32:39 -0700144}
145
146static void __exit xt_connsecmark_fini(void)
147{
Thomas Graf28094862006-08-22 13:52:17 -0700148 xt_unregister_targets(xt_connsecmark_target,
149 ARRAY_SIZE(xt_connsecmark_target));
James Morris100468e2006-06-09 00:32:39 -0700150}
151
152module_init(xt_connsecmark_init);
153module_exit(xt_connsecmark_fini);