1 // SPDX-License-Identifier: GPL-2.0-only
3 * Module for modifying the secmark field of the skb, for use by
6 * Based on the nfmark match by:
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/security.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter/xt_SECMARK.h>
18 MODULE_LICENSE("GPL");
20 MODULE_DESCRIPTION("Xtables: packet security mark modification");
21 MODULE_ALIAS("ipt_SECMARK");
22 MODULE_ALIAS("ip6t_SECMARK");
27 secmark_tg(struct sk_buff *skb, const struct xt_secmark_target_info_v1 *info)
32 case SECMARK_MODE_SEL:
33 secmark = info->secid;
39 skb->secmark = secmark;
43 static int checkentry_lsm(struct xt_secmark_target_info_v1 *info)
47 info->secctx[SECMARK_SECCTX_MAX - 1] = '\0';
50 err = security_secctx_to_secid(info->secctx, strlen(info->secctx),
54 pr_info_ratelimited("invalid security context \'%s\'\n",
60 pr_info_ratelimited("unable to map security context \'%s\'\n",
65 err = security_secmark_relabel_packet(info->secid);
67 pr_info_ratelimited("unable to obtain relabeling permission\n");
71 security_secmark_refcount_inc();
76 secmark_tg_check(const char *table, struct xt_secmark_target_info_v1 *info)
80 if (strcmp(table, "mangle") != 0 &&
81 strcmp(table, "security") != 0) {
82 pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n",
87 if (mode && mode != info->mode) {
88 pr_info_ratelimited("mode already set to %hu cannot mix with rules for mode %hu\n",
94 case SECMARK_MODE_SEL:
97 pr_info_ratelimited("invalid mode: %hu\n", info->mode);
101 err = checkentry_lsm(info);
110 static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
113 case SECMARK_MODE_SEL:
114 security_secmark_refcount_dec();
118 static int secmark_tg_check_v0(const struct xt_tgchk_param *par)
120 struct xt_secmark_target_info *info = par->targinfo;
121 struct xt_secmark_target_info_v1 newinfo = {
126 memcpy(newinfo.secctx, info->secctx, SECMARK_SECCTX_MAX);
128 ret = secmark_tg_check(par->table, &newinfo);
129 info->secid = newinfo.secid;
135 secmark_tg_v0(struct sk_buff *skb, const struct xt_action_param *par)
137 const struct xt_secmark_target_info *info = par->targinfo;
138 struct xt_secmark_target_info_v1 newinfo = {
139 .secid = info->secid,
142 return secmark_tg(skb, &newinfo);
145 static int secmark_tg_check_v1(const struct xt_tgchk_param *par)
147 return secmark_tg_check(par->table, par->targinfo);
151 secmark_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
153 return secmark_tg(skb, par->targinfo);
156 static struct xt_target secmark_tg_reg[] __read_mostly = {
160 .family = NFPROTO_UNSPEC,
161 .checkentry = secmark_tg_check_v0,
162 .destroy = secmark_tg_destroy,
163 .target = secmark_tg_v0,
164 .targetsize = sizeof(struct xt_secmark_target_info),
170 .family = NFPROTO_UNSPEC,
171 .checkentry = secmark_tg_check_v1,
172 .destroy = secmark_tg_destroy,
173 .target = secmark_tg_v1,
174 .targetsize = sizeof(struct xt_secmark_target_info_v1),
175 .usersize = offsetof(struct xt_secmark_target_info_v1, secid),
180 static int __init secmark_tg_init(void)
182 return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
185 static void __exit secmark_tg_exit(void)
187 xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
190 module_init(secmark_tg_init);
191 module_exit(secmark_tg_exit);