Commit | Line | Data |
---|---|---|
96518518 PM |
1 | /* |
2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/netlink.h> | |
15 | #include <linux/netfilter.h> | |
16 | #include <linux/netfilter/nf_tables.h> | |
17 | #include <net/dst.h> | |
18 | #include <net/sock.h> | |
19 | #include <net/tcp_states.h> /* for TCP_TIME_WAIT */ | |
20 | #include <net/netfilter/nf_tables.h> | |
21 | ||
22 | struct nft_meta { | |
23 | enum nft_meta_keys key:8; | |
24 | enum nft_registers dreg:8; | |
25 | }; | |
26 | ||
27 | static void nft_meta_eval(const struct nft_expr *expr, | |
28 | struct nft_data data[NFT_REG_MAX + 1], | |
29 | const struct nft_pktinfo *pkt) | |
30 | { | |
31 | const struct nft_meta *priv = nft_expr_priv(expr); | |
32 | const struct sk_buff *skb = pkt->skb; | |
33 | const struct net_device *in = pkt->in, *out = pkt->out; | |
34 | struct nft_data *dest = &data[priv->dreg]; | |
35 | ||
36 | switch (priv->key) { | |
37 | case NFT_META_LEN: | |
38 | dest->data[0] = skb->len; | |
39 | break; | |
40 | case NFT_META_PROTOCOL: | |
41 | *(__be16 *)dest->data = skb->protocol; | |
42 | break; | |
43 | case NFT_META_PRIORITY: | |
44 | dest->data[0] = skb->priority; | |
45 | break; | |
46 | case NFT_META_MARK: | |
47 | dest->data[0] = skb->mark; | |
48 | break; | |
49 | case NFT_META_IIF: | |
50 | if (in == NULL) | |
51 | goto err; | |
52 | dest->data[0] = in->ifindex; | |
53 | break; | |
54 | case NFT_META_OIF: | |
55 | if (out == NULL) | |
56 | goto err; | |
57 | dest->data[0] = out->ifindex; | |
58 | break; | |
59 | case NFT_META_IIFNAME: | |
60 | if (in == NULL) | |
61 | goto err; | |
62 | strncpy((char *)dest->data, in->name, sizeof(dest->data)); | |
63 | break; | |
64 | case NFT_META_OIFNAME: | |
65 | if (out == NULL) | |
66 | goto err; | |
67 | strncpy((char *)dest->data, out->name, sizeof(dest->data)); | |
68 | break; | |
69 | case NFT_META_IIFTYPE: | |
70 | if (in == NULL) | |
71 | goto err; | |
72 | *(u16 *)dest->data = in->type; | |
73 | break; | |
74 | case NFT_META_OIFTYPE: | |
75 | if (out == NULL) | |
76 | goto err; | |
77 | *(u16 *)dest->data = out->type; | |
78 | break; | |
79 | case NFT_META_SKUID: | |
80 | if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT) | |
81 | goto err; | |
82 | ||
83 | read_lock_bh(&skb->sk->sk_callback_lock); | |
84 | if (skb->sk->sk_socket == NULL || | |
85 | skb->sk->sk_socket->file == NULL) { | |
86 | read_unlock_bh(&skb->sk->sk_callback_lock); | |
87 | goto err; | |
88 | } | |
89 | ||
90 | dest->data[0] = | |
91 | from_kuid_munged(&init_user_ns, | |
92 | skb->sk->sk_socket->file->f_cred->fsuid); | |
93 | read_unlock_bh(&skb->sk->sk_callback_lock); | |
94 | break; | |
95 | case NFT_META_SKGID: | |
96 | if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT) | |
97 | goto err; | |
98 | ||
99 | read_lock_bh(&skb->sk->sk_callback_lock); | |
100 | if (skb->sk->sk_socket == NULL || | |
101 | skb->sk->sk_socket->file == NULL) { | |
102 | read_unlock_bh(&skb->sk->sk_callback_lock); | |
103 | goto err; | |
104 | } | |
105 | dest->data[0] = | |
106 | from_kgid_munged(&init_user_ns, | |
107 | skb->sk->sk_socket->file->f_cred->fsgid); | |
108 | read_unlock_bh(&skb->sk->sk_callback_lock); | |
109 | break; | |
110 | #ifdef CONFIG_NET_CLS_ROUTE | |
111 | case NFT_META_RTCLASSID: { | |
112 | const struct dst_entry *dst = skb_dst(skb); | |
113 | ||
114 | if (dst == NULL) | |
115 | goto err; | |
116 | dest->data[0] = dst->tclassid; | |
117 | break; | |
118 | } | |
119 | #endif | |
120 | #ifdef CONFIG_NETWORK_SECMARK | |
121 | case NFT_META_SECMARK: | |
122 | dest->data[0] = skb->secmark; | |
123 | break; | |
124 | #endif | |
125 | default: | |
126 | WARN_ON(1); | |
127 | goto err; | |
128 | } | |
129 | return; | |
130 | ||
131 | err: | |
132 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; | |
133 | } | |
134 | ||
135 | static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = { | |
136 | [NFTA_META_DREG] = { .type = NLA_U32 }, | |
137 | [NFTA_META_KEY] = { .type = NLA_U32 }, | |
138 | }; | |
139 | ||
140 | static int nft_meta_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
141 | const struct nlattr * const tb[]) | |
142 | { | |
143 | struct nft_meta *priv = nft_expr_priv(expr); | |
144 | int err; | |
145 | ||
146 | if (tb[NFTA_META_DREG] == NULL || | |
147 | tb[NFTA_META_KEY] == NULL) | |
148 | return -EINVAL; | |
149 | ||
150 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); | |
151 | switch (priv->key) { | |
152 | case NFT_META_LEN: | |
153 | case NFT_META_PROTOCOL: | |
154 | case NFT_META_PRIORITY: | |
155 | case NFT_META_MARK: | |
156 | case NFT_META_IIF: | |
157 | case NFT_META_OIF: | |
158 | case NFT_META_IIFNAME: | |
159 | case NFT_META_OIFNAME: | |
160 | case NFT_META_IIFTYPE: | |
161 | case NFT_META_OIFTYPE: | |
162 | case NFT_META_SKUID: | |
163 | case NFT_META_SKGID: | |
164 | #ifdef CONFIG_NET_CLS_ROUTE | |
165 | case NFT_META_RTCLASSID: | |
166 | #endif | |
167 | #ifdef CONFIG_NETWORK_SECMARK | |
168 | case NFT_META_SECMARK: | |
169 | #endif | |
170 | break; | |
171 | default: | |
172 | return -EOPNOTSUPP; | |
173 | } | |
174 | ||
175 | priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG])); | |
176 | err = nft_validate_output_register(priv->dreg); | |
177 | if (err < 0) | |
178 | return err; | |
179 | return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE); | |
180 | } | |
181 | ||
182 | static int nft_meta_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
183 | { | |
184 | const struct nft_meta *priv = nft_expr_priv(expr); | |
185 | ||
186 | if (nla_put_be32(skb, NFTA_META_DREG, htonl(priv->dreg))) | |
187 | goto nla_put_failure; | |
188 | if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) | |
189 | goto nla_put_failure; | |
190 | return 0; | |
191 | ||
192 | nla_put_failure: | |
193 | return -1; | |
194 | } | |
195 | ||
196 | static struct nft_expr_ops nft_meta_ops __read_mostly = { | |
197 | .name = "meta", | |
198 | .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)), | |
199 | .owner = THIS_MODULE, | |
200 | .eval = nft_meta_eval, | |
201 | .init = nft_meta_init, | |
202 | .dump = nft_meta_dump, | |
203 | .policy = nft_meta_policy, | |
204 | .maxattr = NFTA_META_MAX, | |
205 | }; | |
206 | ||
207 | static int __init nft_meta_module_init(void) | |
208 | { | |
209 | return nft_register_expr(&nft_meta_ops); | |
210 | } | |
211 | ||
212 | static void __exit nft_meta_module_exit(void) | |
213 | { | |
214 | nft_unregister_expr(&nft_meta_ops); | |
215 | } | |
216 | ||
217 | module_init(nft_meta_module_init); | |
218 | module_exit(nft_meta_module_exit); | |
219 | ||
220 | MODULE_LICENSE("GPL"); | |
221 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
222 | MODULE_ALIAS_NFT_EXPR("meta"); |