cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

nft_socket.c (6451B)


      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>
      8#include <net/tcp.h>
      9
     10struct nft_socket {
     11	enum nft_socket_keys		key:8;
     12	u8				level;
     13	u8				len;
     14	union {
     15		u8			dreg;
     16	};
     17};
     18
     19static void nft_socket_wildcard(const struct nft_pktinfo *pkt,
     20				struct nft_regs *regs, struct sock *sk,
     21				u32 *dest)
     22{
     23	switch (nft_pf(pkt)) {
     24	case NFPROTO_IPV4:
     25		nft_reg_store8(dest, inet_sk(sk)->inet_rcv_saddr == 0);
     26		break;
     27#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
     28	case NFPROTO_IPV6:
     29		nft_reg_store8(dest, ipv6_addr_any(&sk->sk_v6_rcv_saddr));
     30		break;
     31#endif
     32	default:
     33		regs->verdict.code = NFT_BREAK;
     34		return;
     35	}
     36}
     37
     38#ifdef CONFIG_SOCK_CGROUP_DATA
     39static noinline bool
     40nft_sock_get_eval_cgroupv2(u32 *dest, struct sock *sk, const struct nft_pktinfo *pkt, u32 level)
     41{
     42	struct cgroup *cgrp;
     43
     44	if (!sk_fullsock(sk))
     45		return false;
     46
     47	cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     48	if (level > cgrp->level)
     49		return false;
     50
     51	memcpy(dest, &cgrp->ancestor_ids[level], sizeof(u64));
     52
     53	return true;
     54}
     55#endif
     56
     57static struct sock *nft_socket_do_lookup(const struct nft_pktinfo *pkt)
     58{
     59	const struct net_device *indev = nft_in(pkt);
     60	const struct sk_buff *skb = pkt->skb;
     61	struct sock *sk = NULL;
     62
     63	if (!indev)
     64		return NULL;
     65
     66	switch (nft_pf(pkt)) {
     67	case NFPROTO_IPV4:
     68		sk = nf_sk_lookup_slow_v4(nft_net(pkt), skb, indev);
     69		break;
     70#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
     71	case NFPROTO_IPV6:
     72		sk = nf_sk_lookup_slow_v6(nft_net(pkt), skb, indev);
     73		break;
     74#endif
     75	default:
     76		WARN_ON_ONCE(1);
     77		break;
     78	}
     79
     80	return sk;
     81}
     82
     83static void nft_socket_eval(const struct nft_expr *expr,
     84			    struct nft_regs *regs,
     85			    const struct nft_pktinfo *pkt)
     86{
     87	const struct nft_socket *priv = nft_expr_priv(expr);
     88	struct sk_buff *skb = pkt->skb;
     89	struct sock *sk = skb->sk;
     90	u32 *dest = &regs->data[priv->dreg];
     91
     92	if (sk && !net_eq(nft_net(pkt), sock_net(sk)))
     93		sk = NULL;
     94
     95	if (!sk)
     96		sk = nft_socket_do_lookup(pkt);
     97
     98	if (!sk) {
     99		regs->verdict.code = NFT_BREAK;
    100		return;
    101	}
    102
    103	switch(priv->key) {
    104	case NFT_SOCKET_TRANSPARENT:
    105		nft_reg_store8(dest, inet_sk_transparent(sk));
    106		break;
    107	case NFT_SOCKET_MARK:
    108		if (sk_fullsock(sk)) {
    109			*dest = sk->sk_mark;
    110		} else {
    111			regs->verdict.code = NFT_BREAK;
    112			return;
    113		}
    114		break;
    115	case NFT_SOCKET_WILDCARD:
    116		if (!sk_fullsock(sk)) {
    117			regs->verdict.code = NFT_BREAK;
    118			return;
    119		}
    120		nft_socket_wildcard(pkt, regs, sk, dest);
    121		break;
    122#ifdef CONFIG_SOCK_CGROUP_DATA
    123	case NFT_SOCKET_CGROUPV2:
    124		if (!nft_sock_get_eval_cgroupv2(dest, sk, pkt, priv->level)) {
    125			regs->verdict.code = NFT_BREAK;
    126			return;
    127		}
    128		break;
    129#endif
    130	default:
    131		WARN_ON(1);
    132		regs->verdict.code = NFT_BREAK;
    133	}
    134
    135	if (sk != skb->sk)
    136		sock_gen_put(sk);
    137}
    138
    139static const struct nla_policy nft_socket_policy[NFTA_SOCKET_MAX + 1] = {
    140	[NFTA_SOCKET_KEY]		= { .type = NLA_U32 },
    141	[NFTA_SOCKET_DREG]		= { .type = NLA_U32 },
    142	[NFTA_SOCKET_LEVEL]		= { .type = NLA_U32 },
    143};
    144
    145static int nft_socket_init(const struct nft_ctx *ctx,
    146			   const struct nft_expr *expr,
    147			   const struct nlattr * const tb[])
    148{
    149	struct nft_socket *priv = nft_expr_priv(expr);
    150	unsigned int len;
    151
    152	if (!tb[NFTA_SOCKET_DREG] || !tb[NFTA_SOCKET_KEY])
    153		return -EINVAL;
    154
    155	switch(ctx->family) {
    156	case NFPROTO_IPV4:
    157#if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
    158	case NFPROTO_IPV6:
    159#endif
    160	case NFPROTO_INET:
    161		break;
    162	default:
    163		return -EOPNOTSUPP;
    164	}
    165
    166	priv->key = ntohl(nla_get_u32(tb[NFTA_SOCKET_KEY]));
    167	switch(priv->key) {
    168	case NFT_SOCKET_TRANSPARENT:
    169	case NFT_SOCKET_WILDCARD:
    170		len = sizeof(u8);
    171		break;
    172	case NFT_SOCKET_MARK:
    173		len = sizeof(u32);
    174		break;
    175#ifdef CONFIG_CGROUPS
    176	case NFT_SOCKET_CGROUPV2: {
    177		unsigned int level;
    178
    179		if (!tb[NFTA_SOCKET_LEVEL])
    180			return -EINVAL;
    181
    182		level = ntohl(nla_get_u32(tb[NFTA_SOCKET_LEVEL]));
    183		if (level > 255)
    184			return -EOPNOTSUPP;
    185
    186		priv->level = level;
    187		len = sizeof(u64);
    188		break;
    189	}
    190#endif
    191	default:
    192		return -EOPNOTSUPP;
    193	}
    194
    195	priv->len = len;
    196	return nft_parse_register_store(ctx, tb[NFTA_SOCKET_DREG], &priv->dreg,
    197					NULL, NFT_DATA_VALUE, len);
    198}
    199
    200static int nft_socket_dump(struct sk_buff *skb,
    201			   const struct nft_expr *expr)
    202{
    203	const struct nft_socket *priv = nft_expr_priv(expr);
    204
    205	if (nla_put_u32(skb, NFTA_SOCKET_KEY, htonl(priv->key)))
    206		return -1;
    207	if (nft_dump_register(skb, NFTA_SOCKET_DREG, priv->dreg))
    208		return -1;
    209	if (priv->key == NFT_SOCKET_CGROUPV2 &&
    210	    nla_put_u32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level)))
    211		return -1;
    212	return 0;
    213}
    214
    215static bool nft_socket_reduce(struct nft_regs_track *track,
    216			      const struct nft_expr *expr)
    217{
    218	const struct nft_socket *priv = nft_expr_priv(expr);
    219	const struct nft_socket *socket;
    220
    221	if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
    222		nft_reg_track_update(track, expr, priv->dreg, priv->len);
    223		return false;
    224	}
    225
    226	socket = nft_expr_priv(track->regs[priv->dreg].selector);
    227	if (priv->key != socket->key ||
    228	    priv->dreg != socket->dreg ||
    229	    priv->level != socket->level) {
    230		nft_reg_track_update(track, expr, priv->dreg, priv->len);
    231		return false;
    232	}
    233
    234	if (!track->regs[priv->dreg].bitwise)
    235		return true;
    236
    237	return nft_expr_reduce_bitwise(track, expr);
    238}
    239
    240static int nft_socket_validate(const struct nft_ctx *ctx,
    241			       const struct nft_expr *expr,
    242			       const struct nft_data **data)
    243{
    244	return nft_chain_validate_hooks(ctx->chain,
    245					(1 << NF_INET_PRE_ROUTING) |
    246					(1 << NF_INET_LOCAL_IN) |
    247					(1 << NF_INET_LOCAL_OUT));
    248}
    249
    250static struct nft_expr_type nft_socket_type;
    251static const struct nft_expr_ops nft_socket_ops = {
    252	.type		= &nft_socket_type,
    253	.size		= NFT_EXPR_SIZE(sizeof(struct nft_socket)),
    254	.eval		= nft_socket_eval,
    255	.init		= nft_socket_init,
    256	.dump		= nft_socket_dump,
    257	.validate	= nft_socket_validate,
    258	.reduce		= nft_socket_reduce,
    259};
    260
    261static struct nft_expr_type nft_socket_type __read_mostly = {
    262	.name		= "socket",
    263	.ops		= &nft_socket_ops,
    264	.policy		= nft_socket_policy,
    265	.maxattr	= NFTA_SOCKET_MAX,
    266	.owner		= THIS_MODULE,
    267};
    268
    269static int __init nft_socket_module_init(void)
    270{
    271	return nft_register_expr(&nft_socket_type);
    272}
    273
    274static void __exit nft_socket_module_exit(void)
    275{
    276	nft_unregister_expr(&nft_socket_type);
    277}
    278
    279module_init(nft_socket_module_init);
    280module_exit(nft_socket_module_exit);
    281
    282MODULE_LICENSE("GPL");
    283MODULE_AUTHOR("Máté Eckl");
    284MODULE_DESCRIPTION("nf_tables socket match module");
    285MODULE_ALIAS_NFT_EXPR("socket");