Commit | Line | Data |
---|---|---|
5e6874cd JM |
1 | /* |
2 | * Module for modifying the secmark field of the skb, for use by | |
3 | * security subsystems. | |
4 | * | |
5 | * Based on the nfmark match by: | |
6 | * (C) 1999-2001 Marc Boucher <marc@mbsi.ca> | |
7 | * | |
560ee653 | 8 | * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
5e6874cd JM |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | */ | |
8bee4bad | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
5e6874cd | 16 | #include <linux/module.h> |
2606fd1f | 17 | #include <linux/security.h> |
5e6874cd | 18 | #include <linux/skbuff.h> |
5e6874cd JM |
19 | #include <linux/netfilter/x_tables.h> |
20 | #include <linux/netfilter/xt_SECMARK.h> | |
21 | ||
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_AUTHOR("James Morris <jmorris@redhat.com>"); | |
2ae15b64 | 24 | MODULE_DESCRIPTION("Xtables: packet security mark modification"); |
5e6874cd JM |
25 | MODULE_ALIAS("ipt_SECMARK"); |
26 | MODULE_ALIAS("ip6t_SECMARK"); | |
27 | ||
28 | #define PFX "SECMARK: " | |
29 | ||
30 | static u8 mode; | |
31 | ||
d3c5ee6d | 32 | static unsigned int |
4b560b44 | 33 | secmark_tg(struct sk_buff *skb, const struct xt_action_param *par) |
5e6874cd JM |
34 | { |
35 | u32 secmark = 0; | |
7eb35586 | 36 | const struct xt_secmark_target_info *info = par->targinfo; |
5e6874cd | 37 | |
5e6874cd JM |
38 | switch (mode) { |
39 | case SECMARK_MODE_SEL: | |
2606fd1f | 40 | secmark = info->secid; |
5e6874cd | 41 | break; |
5e6874cd JM |
42 | default: |
43 | BUG(); | |
44 | } | |
45 | ||
3db05fea | 46 | skb->secmark = secmark; |
5e6874cd JM |
47 | return XT_CONTINUE; |
48 | } | |
49 | ||
2606fd1f | 50 | static int checkentry_lsm(struct xt_secmark_target_info *info) |
5e6874cd JM |
51 | { |
52 | int err; | |
601e68e1 | 53 | |
2606fd1f EP |
54 | info->secctx[SECMARK_SECCTX_MAX - 1] = '\0'; |
55 | info->secid = 0; | |
5e6874cd | 56 | |
2606fd1f EP |
57 | err = security_secctx_to_secid(info->secctx, strlen(info->secctx), |
58 | &info->secid); | |
5e6874cd JM |
59 | if (err) { |
60 | if (err == -EINVAL) | |
b2606644 FW |
61 | pr_info_ratelimited("invalid security context \'%s\'\n", |
62 | info->secctx); | |
4a5a5c73 | 63 | return err; |
5e6874cd JM |
64 | } |
65 | ||
2606fd1f | 66 | if (!info->secid) { |
b2606644 FW |
67 | pr_info_ratelimited("unable to map security context \'%s\'\n", |
68 | info->secctx); | |
4a5a5c73 | 69 | return -ENOENT; |
5e6874cd JM |
70 | } |
71 | ||
2606fd1f | 72 | err = security_secmark_relabel_packet(info->secid); |
5e6874cd | 73 | if (err) { |
b2606644 | 74 | pr_info_ratelimited("unable to obtain relabeling permission\n"); |
4a5a5c73 | 75 | return err; |
5e6874cd JM |
76 | } |
77 | ||
2606fd1f | 78 | security_secmark_refcount_inc(); |
4a5a5c73 | 79 | return 0; |
5e6874cd JM |
80 | } |
81 | ||
135367b8 | 82 | static int secmark_tg_check(const struct xt_tgchk_param *par) |
5e6874cd | 83 | { |
af5d6dc2 | 84 | struct xt_secmark_target_info *info = par->targinfo; |
4a5a5c73 | 85 | int err; |
5e6874cd | 86 | |
af5d6dc2 JE |
87 | if (strcmp(par->table, "mangle") != 0 && |
88 | strcmp(par->table, "security") != 0) { | |
cc48baef FW |
89 | pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n", |
90 | par->table); | |
d6b00a53 | 91 | return -EINVAL; |
560ee653 JM |
92 | } |
93 | ||
5e6874cd | 94 | if (mode && mode != info->mode) { |
b2606644 FW |
95 | pr_info_ratelimited("mode already set to %hu cannot mix with rules for mode %hu\n", |
96 | mode, info->mode); | |
d6b00a53 | 97 | return -EINVAL; |
5e6874cd JM |
98 | } |
99 | ||
100 | switch (info->mode) { | |
101 | case SECMARK_MODE_SEL: | |
5e6874cd | 102 | break; |
5e6874cd | 103 | default: |
b2606644 | 104 | pr_info_ratelimited("invalid mode: %hu\n", info->mode); |
d6b00a53 | 105 | return -EINVAL; |
5e6874cd JM |
106 | } |
107 | ||
2606fd1f EP |
108 | err = checkentry_lsm(info); |
109 | if (err) | |
110 | return err; | |
111 | ||
5e6874cd JM |
112 | if (!mode) |
113 | mode = info->mode; | |
d6b00a53 | 114 | return 0; |
5e6874cd JM |
115 | } |
116 | ||
a2df1648 | 117 | static void secmark_tg_destroy(const struct xt_tgdtor_param *par) |
d621d35e PM |
118 | { |
119 | switch (mode) { | |
120 | case SECMARK_MODE_SEL: | |
2606fd1f | 121 | security_secmark_refcount_dec(); |
d621d35e PM |
122 | } |
123 | } | |
124 | ||
55b69e91 JE |
125 | static struct xt_target secmark_tg_reg __read_mostly = { |
126 | .name = "SECMARK", | |
127 | .revision = 0, | |
128 | .family = NFPROTO_UNSPEC, | |
129 | .checkentry = secmark_tg_check, | |
130 | .destroy = secmark_tg_destroy, | |
131 | .target = secmark_tg, | |
132 | .targetsize = sizeof(struct xt_secmark_target_info), | |
133 | .me = THIS_MODULE, | |
5e6874cd JM |
134 | }; |
135 | ||
d3c5ee6d | 136 | static int __init secmark_tg_init(void) |
5e6874cd | 137 | { |
55b69e91 | 138 | return xt_register_target(&secmark_tg_reg); |
5e6874cd JM |
139 | } |
140 | ||
d3c5ee6d | 141 | static void __exit secmark_tg_exit(void) |
5e6874cd | 142 | { |
55b69e91 | 143 | xt_unregister_target(&secmark_tg_reg); |
5e6874cd JM |
144 | } |
145 | ||
d3c5ee6d JE |
146 | module_init(secmark_tg_init); |
147 | module_exit(secmark_tg_exit); |