1 /* 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>
11 enum nft_socket_keys key:8;
18 static void nft_socket_wildcard(const struct nft_pktinfo *pkt,
19 struct nft_regs *regs, struct sock *sk,
22 switch (nft_pf(pkt)) {
24 nft_reg_store8(dest, inet_sk(sk)->inet_rcv_saddr == 0);
26 #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
28 nft_reg_store8(dest, ipv6_addr_any(&sk->sk_v6_rcv_saddr));
32 regs->verdict.code = NFT_BREAK;
39 nft_sock_get_eval_cgroupv2(u32 *dest, const struct nft_pktinfo *pkt, u32 level)
41 struct sock *sk = skb_to_full_sk(pkt->skb);
44 if (!sk || !sk_fullsock(sk) || !net_eq(nft_net(pkt), sock_net(sk)))
47 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
48 if (level > cgrp->level)
51 memcpy(dest, &cgrp->ancestor_ids[level], sizeof(u64));
57 static void nft_socket_eval(const struct nft_expr *expr,
58 struct nft_regs *regs,
59 const struct nft_pktinfo *pkt)
61 const struct nft_socket *priv = nft_expr_priv(expr);
62 struct sk_buff *skb = pkt->skb;
63 struct sock *sk = skb->sk;
64 u32 *dest = ®s->data[priv->dreg];
66 if (sk && !net_eq(nft_net(pkt), sock_net(sk)))
72 sk = nf_sk_lookup_slow_v4(nft_net(pkt), skb, nft_in(pkt));
74 #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
76 sk = nf_sk_lookup_slow_v6(nft_net(pkt), skb, nft_in(pkt));
81 regs->verdict.code = NFT_BREAK;
86 regs->verdict.code = NFT_BREAK;
91 case NFT_SOCKET_TRANSPARENT:
92 nft_reg_store8(dest, inet_sk_transparent(sk));
95 if (sk_fullsock(sk)) {
98 regs->verdict.code = NFT_BREAK;
102 case NFT_SOCKET_WILDCARD:
103 if (!sk_fullsock(sk)) {
104 regs->verdict.code = NFT_BREAK;
107 nft_socket_wildcard(pkt, regs, sk, dest);
109 #ifdef CONFIG_CGROUPS
110 case NFT_SOCKET_CGROUPV2:
111 if (!nft_sock_get_eval_cgroupv2(dest, pkt, priv->level)) {
112 regs->verdict.code = NFT_BREAK;
119 regs->verdict.code = NFT_BREAK;
126 static const struct nla_policy nft_socket_policy[NFTA_SOCKET_MAX + 1] = {
127 [NFTA_SOCKET_KEY] = { .type = NLA_U32 },
128 [NFTA_SOCKET_DREG] = { .type = NLA_U32 },
129 [NFTA_SOCKET_LEVEL] = { .type = NLA_U32 },
132 static int nft_socket_init(const struct nft_ctx *ctx,
133 const struct nft_expr *expr,
134 const struct nlattr * const tb[])
136 struct nft_socket *priv = nft_expr_priv(expr);
137 unsigned int len, level;
139 if (!tb[NFTA_SOCKET_DREG] || !tb[NFTA_SOCKET_KEY])
142 switch(ctx->family) {
144 #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
153 priv->key = ntohl(nla_get_u32(tb[NFTA_SOCKET_KEY]));
155 case NFT_SOCKET_TRANSPARENT:
156 case NFT_SOCKET_WILDCARD:
159 case NFT_SOCKET_MARK:
162 #ifdef CONFIG_CGROUPS
163 case NFT_SOCKET_CGROUPV2:
164 if (!tb[NFTA_SOCKET_LEVEL])
167 level = ntohl(nla_get_u32(tb[NFTA_SOCKET_LEVEL]));
179 return nft_parse_register_store(ctx, tb[NFTA_SOCKET_DREG], &priv->dreg,
180 NULL, NFT_DATA_VALUE, len);
183 static int nft_socket_dump(struct sk_buff *skb,
184 const struct nft_expr *expr)
186 const struct nft_socket *priv = nft_expr_priv(expr);
188 if (nla_put_u32(skb, NFTA_SOCKET_KEY, htonl(priv->key)))
190 if (nft_dump_register(skb, NFTA_SOCKET_DREG, priv->dreg))
192 if (priv->key == NFT_SOCKET_CGROUPV2 &&
193 nla_put_u32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level)))
198 static struct nft_expr_type nft_socket_type;
199 static const struct nft_expr_ops nft_socket_ops = {
200 .type = &nft_socket_type,
201 .size = NFT_EXPR_SIZE(sizeof(struct nft_socket)),
202 .eval = nft_socket_eval,
203 .init = nft_socket_init,
204 .dump = nft_socket_dump,
207 static struct nft_expr_type nft_socket_type __read_mostly = {
209 .ops = &nft_socket_ops,
210 .policy = nft_socket_policy,
211 .maxattr = NFTA_SOCKET_MAX,
212 .owner = THIS_MODULE,
215 static int __init nft_socket_module_init(void)
217 return nft_register_expr(&nft_socket_type);
220 static void __exit nft_socket_module_exit(void)
222 nft_unregister_expr(&nft_socket_type);
225 module_init(nft_socket_module_init);
226 module_exit(nft_socket_module_exit);
228 MODULE_LICENSE("GPL");
229 MODULE_AUTHOR("Máté Eckl");
230 MODULE_DESCRIPTION("nf_tables socket match module");
231 MODULE_ALIAS_NFT_EXPR("socket");