Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / xt_CONNSECMARK.c
CommitLineData
100468e9
JM
1/*
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 *
560ee653 11 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
100468e9
JM
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 */
8bee4bad 18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
100468e9
JM
19#include <linux/module.h>
20#include <linux/skbuff.h>
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_CONNSECMARK.h>
587aa641 23#include <net/netfilter/nf_conntrack.h>
37fccd85 24#include <net/netfilter/nf_conntrack_ecache.h>
100468e9 25
100468e9
JM
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
2ae15b64 28MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
100468e9
JM
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 */
a47362a2 36static void secmark_save(const struct sk_buff *skb)
100468e9
JM
37{
38 if (skb->secmark) {
587aa641 39 struct nf_conn *ct;
100468e9
JM
40 enum ip_conntrack_info ctinfo;
41
587aa641 42 ct = nf_ct_get(skb, &ctinfo);
37fccd85 43 if (ct && !ct->secmark) {
587aa641 44 ct->secmark = skb->secmark;
a71996fc 45 nf_conntrack_event_cache(IPCT_SECMARK, ct);
37fccd85 46 }
100468e9
JM
47 }
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) {
3cf93c96 57 const struct nf_conn *ct;
100468e9
JM
58 enum ip_conntrack_info ctinfo;
59
587aa641
PM
60 ct = nf_ct_get(skb, &ctinfo);
61 if (ct && ct->secmark)
62 skb->secmark = ct->secmark;
100468e9
JM
63 }
64}
65
d3c5ee6d 66static unsigned int
4b560b44 67connsecmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
100468e9 68{
7eb35586 69 const struct xt_connsecmark_target_info *info = par->targinfo;
100468e9
JM
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
135367b8 87static int connsecmark_tg_check(const struct xt_tgchk_param *par)
100468e9 88{
af5d6dc2 89 const struct xt_connsecmark_target_info *info = par->targinfo;
4a5a5c73 90 int ret;
100468e9 91
af5d6dc2
JE
92 if (strcmp(par->table, "mangle") != 0 &&
93 strcmp(par->table, "security") != 0) {
8bee4bad
JE
94 pr_info("target only valid in the \'mangle\' "
95 "or \'security\' tables, not \'%s\'.\n", par->table);
d6b00a53 96 return -EINVAL;
560ee653
JM
97 }
98
100468e9
JM
99 switch (info->mode) {
100 case CONNSECMARK_SAVE:
101 case CONNSECMARK_RESTORE:
102 break;
103
104 default:
8bee4bad 105 pr_info("invalid mode: %hu\n", info->mode);
4a5a5c73 106 return -EINVAL;
100468e9
JM
107 }
108
4a5a5c73 109 ret = nf_ct_l3proto_try_module_get(par->family);
f95c74e3 110 if (ret < 0)
8bee4bad
JE
111 pr_info("cannot load conntrack support for proto=%u\n",
112 par->family);
f95c74e3 113 return ret;
100468e9
JM
114}
115
a2df1648 116static void connsecmark_tg_destroy(const struct xt_tgdtor_param *par)
11078c37 117{
92f3b2b1 118 nf_ct_l3proto_module_put(par->family);
11078c37
YK
119}
120
92f3b2b1
JE
121static 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,
100468e9
JM
130};
131
d3c5ee6d 132static int __init connsecmark_tg_init(void)
100468e9 133{
92f3b2b1 134 return xt_register_target(&connsecmark_tg_reg);
100468e9
JM
135}
136
d3c5ee6d 137static void __exit connsecmark_tg_exit(void)
100468e9 138{
92f3b2b1 139 xt_unregister_target(&connsecmark_tg_reg);
100468e9
JM
140}
141
d3c5ee6d
JE
142module_init(connsecmark_tg_init);
143module_exit(connsecmark_tg_exit);