blob: f5da0c1775f2e7fd5ba108b70facc06cbf7180e1 [file] [log] [blame]
Máté Eckl554ced02018-05-28 09:15:33 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#include <linux/module.h>
3#include <linux/netfilter/nf_tables.h>
4#include <net/netfilter/nf_tables.h>
5#include <net/netfilter/nf_tables_core.h>
6#include <net/netfilter/nf_socket.h>
7#include <net/inet_sock.h>
Máté Eckl8d6e5552018-06-01 14:54:07 +02008#include <net/tcp.h>
Máté Eckl554ced02018-05-28 09:15:33 +02009
10struct nft_socket {
11 enum nft_socket_keys key:8;
Florian Westphal7f3287d2024-09-07 16:07:49 +020012 u8 level; /* cgroupv2 level to extract */
13 u8 level_user; /* cgroupv2 level provided by userspace */
Pablo Neira Ayusod77a7212022-03-14 18:23:09 +010014 u8 len;
Máté Eckl554ced02018-05-28 09:15:33 +020015 union {
Pablo Neira Ayuso345023b2021-01-25 18:27:22 +010016 u8 dreg;
Máté Eckl554ced02018-05-28 09:15:33 +020017 };
18};
19
Balazs Scheidler67407a42020-08-29 08:19:15 +020020static void nft_socket_wildcard(const struct nft_pktinfo *pkt,
21 struct nft_regs *regs, struct sock *sk,
22 u32 *dest)
23{
24 switch (nft_pf(pkt)) {
25 case NFPROTO_IPV4:
26 nft_reg_store8(dest, inet_sk(sk)->inet_rcv_saddr == 0);
27 break;
28#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
29 case NFPROTO_IPV6:
30 nft_reg_store8(dest, ipv6_addr_any(&sk->sk_v6_rcv_saddr));
31 break;
32#endif
33 default:
34 regs->verdict.code = NFT_BREAK;
35 return;
36 }
37}
38
Arnd Bergmann7acc0bb2021-04-27 21:45:19 +020039#ifdef CONFIG_SOCK_CGROUP_DATA
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020040static noinline bool
Florian Westphal05ae2fb2022-04-09 13:20:19 +020041nft_sock_get_eval_cgroupv2(u32 *dest, struct sock *sk, const struct nft_pktinfo *pkt, u32 level)
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020042{
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020043 struct cgroup *cgrp;
Tejun Heo7f203bc2022-07-29 13:10:16 -100044 u64 cgid;
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020045
Florian Westphal05ae2fb2022-04-09 13:20:19 +020046 if (!sk_fullsock(sk))
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020047 return false;
48
Tejun Heo7f203bc2022-07-29 13:10:16 -100049 cgrp = cgroup_ancestor(sock_cgroup_ptr(&sk->sk_cgrp_data), level);
50 if (!cgrp)
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020051 return false;
52
Tejun Heo7f203bc2022-07-29 13:10:16 -100053 cgid = cgroup_id(cgrp);
54 memcpy(dest, &cgid, sizeof(u64));
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020055 return true;
56}
Florian Westphal7f3287d2024-09-07 16:07:49 +020057
58/* process context only, uses current->nsproxy. */
59static noinline int nft_socket_cgroup_subtree_level(void)
60{
61 struct cgroup *cgrp = cgroup_get_from_path("/");
62 int level;
63
Dan Carpenter70526222024-09-14 12:56:51 +030064 if (IS_ERR(cgrp))
65 return PTR_ERR(cgrp);
Florian Westphal7f3287d2024-09-07 16:07:49 +020066
67 level = cgrp->level;
68
69 cgroup_put(cgrp);
70
71 if (WARN_ON_ONCE(level > 255))
72 return -ERANGE;
73
74 if (WARN_ON_ONCE(level < 0))
75 return -EINVAL;
76
77 return level;
78}
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +020079#endif
80
Florian Westphal743b83f2022-04-28 09:39:21 +020081static struct sock *nft_socket_do_lookup(const struct nft_pktinfo *pkt)
82{
83 const struct net_device *indev = nft_in(pkt);
84 const struct sk_buff *skb = pkt->skb;
85 struct sock *sk = NULL;
86
87 if (!indev)
88 return NULL;
89
90 switch (nft_pf(pkt)) {
91 case NFPROTO_IPV4:
92 sk = nf_sk_lookup_slow_v4(nft_net(pkt), skb, indev);
93 break;
94#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
95 case NFPROTO_IPV6:
96 sk = nf_sk_lookup_slow_v6(nft_net(pkt), skb, indev);
97 break;
98#endif
99 default:
100 WARN_ON_ONCE(1);
101 break;
102 }
103
104 return sk;
105}
106
Máté Eckl554ced02018-05-28 09:15:33 +0200107static void nft_socket_eval(const struct nft_expr *expr,
108 struct nft_regs *regs,
109 const struct nft_pktinfo *pkt)
110{
111 const struct nft_socket *priv = nft_expr_priv(expr);
112 struct sk_buff *skb = pkt->skb;
113 struct sock *sk = skb->sk;
114 u32 *dest = &regs->data[priv->dreg];
115
Flavio Leitnerf5646502018-06-27 10:34:25 -0300116 if (sk && !net_eq(nft_net(pkt), sock_net(sk)))
117 sk = NULL;
118
Máté Eckl554ced02018-05-28 09:15:33 +0200119 if (!sk)
Florian Westphal743b83f2022-04-28 09:39:21 +0200120 sk = nft_socket_do_lookup(pkt);
Máté Eckl554ced02018-05-28 09:15:33 +0200121
Flavio Leitnerf5646502018-06-27 10:34:25 -0300122 if (!sk) {
Máté Eckl365b5a32018-07-12 17:18:46 +0200123 regs->verdict.code = NFT_BREAK;
Máté Eckl554ced02018-05-28 09:15:33 +0200124 return;
125 }
126
Máté Eckl554ced02018-05-28 09:15:33 +0200127 switch(priv->key) {
128 case NFT_SOCKET_TRANSPARENT:
Máté Eckl8d6e5552018-06-01 14:54:07 +0200129 nft_reg_store8(dest, inet_sk_transparent(sk));
Máté Eckl554ced02018-05-28 09:15:33 +0200130 break;
Máté Eckl7d25f882018-07-12 17:48:06 +0200131 case NFT_SOCKET_MARK:
132 if (sk_fullsock(sk)) {
Eric Dumazet3c5b4d62023-07-28 15:03:15 +0000133 *dest = READ_ONCE(sk->sk_mark);
Máté Eckl7d25f882018-07-12 17:48:06 +0200134 } else {
135 regs->verdict.code = NFT_BREAK;
Florian Westphal8b26ff72024-09-05 12:54:46 +0200136 goto out_put_sk;
Máté Eckl7d25f882018-07-12 17:48:06 +0200137 }
138 break;
Balazs Scheidler67407a42020-08-29 08:19:15 +0200139 case NFT_SOCKET_WILDCARD:
140 if (!sk_fullsock(sk)) {
141 regs->verdict.code = NFT_BREAK;
Florian Westphal8b26ff72024-09-05 12:54:46 +0200142 goto out_put_sk;
Balazs Scheidler67407a42020-08-29 08:19:15 +0200143 }
144 nft_socket_wildcard(pkt, regs, sk, dest);
145 break;
Arnd Bergmann7acc0bb2021-04-27 21:45:19 +0200146#ifdef CONFIG_SOCK_CGROUP_DATA
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200147 case NFT_SOCKET_CGROUPV2:
Florian Westphal05ae2fb2022-04-09 13:20:19 +0200148 if (!nft_sock_get_eval_cgroupv2(dest, sk, pkt, priv->level)) {
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200149 regs->verdict.code = NFT_BREAK;
Florian Westphal8b26ff72024-09-05 12:54:46 +0200150 goto out_put_sk;
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200151 }
152 break;
153#endif
Máté Eckl554ced02018-05-28 09:15:33 +0200154 default:
155 WARN_ON(1);
156 regs->verdict.code = NFT_BREAK;
157 }
Fernando Fernandez Mancera039b1f42019-09-01 11:48:08 +0200158
Florian Westphal8b26ff72024-09-05 12:54:46 +0200159out_put_sk:
Fernando Fernandez Mancera039b1f42019-09-01 11:48:08 +0200160 if (sk != skb->sk)
161 sock_gen_put(sk);
Máté Eckl554ced02018-05-28 09:15:33 +0200162}
163
164static const struct nla_policy nft_socket_policy[NFTA_SOCKET_MAX + 1] = {
Florian Westphala412dbf2023-06-21 21:11:03 +0200165 [NFTA_SOCKET_KEY] = NLA_POLICY_MAX(NLA_BE32, 255),
Máté Eckl554ced02018-05-28 09:15:33 +0200166 [NFTA_SOCKET_DREG] = { .type = NLA_U32 },
Florian Westphala412dbf2023-06-21 21:11:03 +0200167 [NFTA_SOCKET_LEVEL] = NLA_POLICY_MAX(NLA_BE32, 255),
Máté Eckl554ced02018-05-28 09:15:33 +0200168};
169
170static int nft_socket_init(const struct nft_ctx *ctx,
171 const struct nft_expr *expr,
172 const struct nlattr * const tb[])
173{
174 struct nft_socket *priv = nft_expr_priv(expr);
Arnd Bergmann8a7363f2021-04-27 21:45:18 +0200175 unsigned int len;
Máté Eckl554ced02018-05-28 09:15:33 +0200176
177 if (!tb[NFTA_SOCKET_DREG] || !tb[NFTA_SOCKET_KEY])
178 return -EINVAL;
179
180 switch(ctx->family) {
181 case NFPROTO_IPV4:
Máté Eckl5d400a42018-07-10 16:01:28 +0200182#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
Máté Eckl554ced02018-05-28 09:15:33 +0200183 case NFPROTO_IPV6:
184#endif
185 case NFPROTO_INET:
186 break;
187 default:
188 return -EOPNOTSUPP;
189 }
190
Florian Westphald86473b2022-06-23 15:05:11 +0200191 priv->key = ntohl(nla_get_be32(tb[NFTA_SOCKET_KEY]));
Máté Eckl554ced02018-05-28 09:15:33 +0200192 switch(priv->key) {
193 case NFT_SOCKET_TRANSPARENT:
Balazs Scheidler67407a42020-08-29 08:19:15 +0200194 case NFT_SOCKET_WILDCARD:
Máté Eckl554ced02018-05-28 09:15:33 +0200195 len = sizeof(u8);
196 break;
Máté Eckl7d25f882018-07-12 17:48:06 +0200197 case NFT_SOCKET_MARK:
198 len = sizeof(u32);
199 break;
Florian Westphal7f3287d2024-09-07 16:07:49 +0200200#ifdef CONFIG_SOCK_CGROUP_DATA
Arnd Bergmann8a7363f2021-04-27 21:45:18 +0200201 case NFT_SOCKET_CGROUPV2: {
202 unsigned int level;
Florian Westphal7f3287d2024-09-07 16:07:49 +0200203 int err;
Arnd Bergmann8a7363f2021-04-27 21:45:18 +0200204
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200205 if (!tb[NFTA_SOCKET_LEVEL])
206 return -EINVAL;
207
Florian Westphald86473b2022-06-23 15:05:11 +0200208 level = ntohl(nla_get_be32(tb[NFTA_SOCKET_LEVEL]));
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200209 if (level > 255)
210 return -EOPNOTSUPP;
211
Florian Westphal7f3287d2024-09-07 16:07:49 +0200212 err = nft_socket_cgroup_subtree_level();
213 if (err < 0)
214 return err;
215
216 priv->level_user = level;
217
218 level += err;
219 /* Implies a giant cgroup tree */
220 if (WARN_ON_ONCE(level > 255))
221 return -EOPNOTSUPP;
222
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200223 priv->level = level;
224 len = sizeof(u64);
225 break;
Arnd Bergmann8a7363f2021-04-27 21:45:18 +0200226 }
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200227#endif
Máté Eckl554ced02018-05-28 09:15:33 +0200228 default:
229 return -EOPNOTSUPP;
230 }
231
Pablo Neira Ayusod77a7212022-03-14 18:23:09 +0100232 priv->len = len;
Pablo Neira Ayuso345023b2021-01-25 18:27:22 +0100233 return nft_parse_register_store(ctx, tb[NFTA_SOCKET_DREG], &priv->dreg,
234 NULL, NFT_DATA_VALUE, len);
Máté Eckl554ced02018-05-28 09:15:33 +0200235}
236
237static int nft_socket_dump(struct sk_buff *skb,
Phil Sutter7d34aa32022-10-14 23:45:58 +0200238 const struct nft_expr *expr, bool reset)
Máté Eckl554ced02018-05-28 09:15:33 +0200239{
240 const struct nft_socket *priv = nft_expr_priv(expr);
241
Florian Westphald86473b2022-06-23 15:05:11 +0200242 if (nla_put_be32(skb, NFTA_SOCKET_KEY, htonl(priv->key)))
Máté Eckl554ced02018-05-28 09:15:33 +0200243 return -1;
244 if (nft_dump_register(skb, NFTA_SOCKET_DREG, priv->dreg))
245 return -1;
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200246 if (priv->key == NFT_SOCKET_CGROUPV2 &&
Florian Westphal7f3287d2024-09-07 16:07:49 +0200247 nla_put_be32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level_user)))
Pablo Neira Ayusoe0bb96d2021-04-21 01:12:44 +0200248 return -1;
Máté Eckl554ced02018-05-28 09:15:33 +0200249 return 0;
250}
251
Pablo Neira Ayusod77a7212022-03-14 18:23:09 +0100252static bool nft_socket_reduce(struct nft_regs_track *track,
253 const struct nft_expr *expr)
254{
255 const struct nft_socket *priv = nft_expr_priv(expr);
256 const struct nft_socket *socket;
257
258 if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
259 nft_reg_track_update(track, expr, priv->dreg, priv->len);
260 return false;
261 }
262
263 socket = nft_expr_priv(track->regs[priv->dreg].selector);
264 if (priv->key != socket->key ||
265 priv->dreg != socket->dreg ||
266 priv->level != socket->level) {
267 nft_reg_track_update(track, expr, priv->dreg, priv->len);
268 return false;
269 }
270
271 if (!track->regs[priv->dreg].bitwise)
272 return true;
273
274 return nft_expr_reduce_bitwise(track, expr);
275}
276
Florian Westphal743b83f2022-04-28 09:39:21 +0200277static int nft_socket_validate(const struct nft_ctx *ctx,
Florian Westphaleaf9b2c2024-08-28 11:34:02 +0200278 const struct nft_expr *expr)
Florian Westphal743b83f2022-04-28 09:39:21 +0200279{
Pablo Neira Ayusod0009ef2024-01-23 16:38:25 +0100280 if (ctx->family != NFPROTO_IPV4 &&
281 ctx->family != NFPROTO_IPV6 &&
282 ctx->family != NFPROTO_INET)
283 return -EOPNOTSUPP;
284
Florian Westphal743b83f2022-04-28 09:39:21 +0200285 return nft_chain_validate_hooks(ctx->chain,
286 (1 << NF_INET_PRE_ROUTING) |
287 (1 << NF_INET_LOCAL_IN) |
288 (1 << NF_INET_LOCAL_OUT));
289}
290
Máté Eckl554ced02018-05-28 09:15:33 +0200291static struct nft_expr_type nft_socket_type;
292static const struct nft_expr_ops nft_socket_ops = {
293 .type = &nft_socket_type,
294 .size = NFT_EXPR_SIZE(sizeof(struct nft_socket)),
295 .eval = nft_socket_eval,
296 .init = nft_socket_init,
297 .dump = nft_socket_dump,
Florian Westphal743b83f2022-04-28 09:39:21 +0200298 .validate = nft_socket_validate,
Pablo Neira Ayusod77a7212022-03-14 18:23:09 +0100299 .reduce = nft_socket_reduce,
Máté Eckl554ced02018-05-28 09:15:33 +0200300};
301
302static struct nft_expr_type nft_socket_type __read_mostly = {
303 .name = "socket",
304 .ops = &nft_socket_ops,
305 .policy = nft_socket_policy,
306 .maxattr = NFTA_SOCKET_MAX,
307 .owner = THIS_MODULE,
308};
309
310static int __init nft_socket_module_init(void)
311{
312 return nft_register_expr(&nft_socket_type);
313}
314
315static void __exit nft_socket_module_exit(void)
316{
317 nft_unregister_expr(&nft_socket_type);
318}
319
320module_init(nft_socket_module_init);
321module_exit(nft_socket_module_exit);
322
323MODULE_LICENSE("GPL");
324MODULE_AUTHOR("Máté Eckl");
325MODULE_DESCRIPTION("nf_tables socket match module");
Pablo Neira Ayuso3fb61ec2018-06-07 17:56:08 +0200326MODULE_ALIAS_NFT_EXPR("socket");