blob: 1faa9136195dbd7a8fde07dad72a55b6c758788b [file] [log] [blame]
James Morris100468e92006-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>
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080023#include <net/netfilter/nf_conntrack_ecache.h>
James Morris100468e92006-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 Morris100468e92006-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 Morris100468e92006-06-09 00:32:39 -070038{
39 if (skb->secmark) {
Patrick McHardy587aa642007-03-14 16:37:25 -070040 struct nf_conn *ct;
James Morris100468e92006-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;
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -080046 nf_conntrack_event_cache(IPCT_SECMARK, skb);
47 }
James Morris100468e92006-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) {
Patrick McHardy587aa642007-03-14 16:37:25 -070058 struct nf_conn *ct;
James Morris100468e92006-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 Morris100468e92006-06-09 00:32:39 -070064 }
65}
66
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080067static unsigned int
68connsecmark_tg(struct sk_buff *skb, const struct net_device *in,
69 const struct net_device *out, unsigned int hooknum,
70 const struct xt_target *target, const void *targinfo)
James Morris100468e92006-06-09 00:32:39 -070071{
James Morris100468e92006-06-09 00:32:39 -070072 const struct xt_connsecmark_target_info *info = targinfo;
73
74 switch (info->mode) {
75 case CONNSECMARK_SAVE:
76 secmark_save(skb);
77 break;
78
79 case CONNSECMARK_RESTORE:
80 secmark_restore(skb);
81 break;
82
83 default:
84 BUG();
85 }
86
87 return XT_CONTINUE;
88}
89
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090static bool
91connsecmark_tg_check(const char *tablename, const void *entry,
92 const struct xt_target *target, void *targinfo,
93 unsigned int hook_mask)
James Morris100468e92006-06-09 00:32:39 -070094{
Jan Engelhardta47362a2007-07-07 22:16:55 -070095 const struct xt_connsecmark_target_info *info = targinfo;
James Morris100468e92006-06-09 00:32:39 -070096
97 switch (info->mode) {
98 case CONNSECMARK_SAVE:
99 case CONNSECMARK_RESTORE:
100 break;
101
102 default:
103 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700104 return false;
James Morris100468e92006-06-09 00:32:39 -0700105 }
106
Jan Engelhardt67b4af22007-12-01 00:01:50 +1100107 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
108 printk(KERN_WARNING "can't load conntrack support for "
Jan Engelhardtdf54aae2007-12-17 22:43:15 -0800109 "proto=%u\n", target->family);
Jan Engelhardt67b4af22007-12-01 00:01:50 +1100110 return false;
111 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700112 return true;
James Morris100468e92006-06-09 00:32:39 -0700113}
114
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800115static void
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800116connsecmark_tg_destroy(const struct xt_target *target, void *targinfo)
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800117{
118 nf_ct_l3proto_module_put(target->family);
119}
120
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800121static struct xt_target connsecmark_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700122 {
123 .name = "CONNSECMARK",
124 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125 .checkentry = connsecmark_tg_check,
126 .destroy = connsecmark_tg_destroy,
127 .target = connsecmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700128 .targetsize = sizeof(struct xt_connsecmark_target_info),
129 .table = "mangle",
130 .me = THIS_MODULE,
131 },
132 {
133 .name = "CONNSECMARK",
134 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800135 .checkentry = connsecmark_tg_check,
136 .destroy = connsecmark_tg_destroy,
137 .target = connsecmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700138 .targetsize = sizeof(struct xt_connsecmark_target_info),
139 .table = "mangle",
140 .me = THIS_MODULE,
141 },
James Morris100468e92006-06-09 00:32:39 -0700142};
143
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144static int __init connsecmark_tg_init(void)
James Morris100468e92006-06-09 00:32:39 -0700145{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800146 return xt_register_targets(connsecmark_tg_reg,
147 ARRAY_SIZE(connsecmark_tg_reg));
James Morris100468e92006-06-09 00:32:39 -0700148}
149
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800150static void __exit connsecmark_tg_exit(void)
James Morris100468e92006-06-09 00:32:39 -0700151{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800152 xt_unregister_targets(connsecmark_tg_reg,
153 ARRAY_SIZE(connsecmark_tg_reg));
James Morris100468e92006-06-09 00:32:39 -0700154}
155
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800156module_init(connsecmark_tg_init);
157module_exit(connsecmark_tg_exit);