Skip to content

Commit

Permalink
netfilter: nfnetlink_queue: enable classid socket info retrieval
Browse files Browse the repository at this point in the history
This enables associating a socket with a v1 net_cls cgroup. Useful for
applying a per-cgroup policy when processing packets in userspace.

Signed-off-by: Eric Sage <eric_sage@apple.com>
  • Loading branch information
Eric Sage authored and intel-lab-lkp committed Mar 23, 2023
1 parent aabef97 commit 5337cbc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/uapi/linux/netfilter/nfnetlink_queue.h
Expand Up @@ -62,6 +62,7 @@ enum nfqnl_attr_type {
NFQA_VLAN, /* nested attribute: packet vlan info */
NFQA_L2HDR, /* full L2 header */
NFQA_PRIORITY, /* skb->priority */
NFQA_CGROUP_CLASSID, /* __u32 cgroup classid */

__NFQA_MAX
};
Expand Down
20 changes: 20 additions & 0 deletions net/netfilter/nfnetlink_queue.c
Expand Up @@ -29,6 +29,7 @@
#include <linux/netfilter/nfnetlink_queue.h>
#include <linux/netfilter/nf_conntrack_common.h>
#include <linux/list.h>
#include <linux/cgroup-defs.h>
#include <net/sock.h>
#include <net/tcp_states.h>
#include <net/netfilter/nf_queue.h>
Expand Down Expand Up @@ -301,6 +302,19 @@ static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
return -1;
}

static int nfqnl_put_sk_classid(struct sk_buff *skb, struct sock *sk)
{
#if IS_ENABLED(CONFIG_CGROUP_NET_CLASSID)
if (sk && sk_fullsock(sk)) {
u32 classid = sock_cgroup_classid(&sk->sk_cgrp_data);

if (classid && nla_put_be32(skb, NFQA_CGROUP_CLASSID, htonl(classid)))
return -1;
}
#endif
return 0;
}

static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
{
u32 seclen = 0;
Expand Down Expand Up @@ -407,6 +421,9 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
+ nla_total_size(sizeof(u_int32_t)) /* skbinfo */
+ nla_total_size(sizeof(u_int32_t)); /* cap_len */
#if IS_ENABLED(CONFIG_CGROUP_NET_CLASSID)
+ nla_total_size(sizeof(u_int32_t)); /* classid */
#endif

tstamp = skb_tstamp_cond(entskb, false);
if (tstamp)
Expand Down Expand Up @@ -599,6 +616,9 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
nfqnl_put_sk_uidgid(skb, entskb->sk) < 0)
goto nla_put_failure;

if (nfqnl_put_sk_classid(skb, entskb->sk) < 0)
goto nla_put_failure;

if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata))
goto nla_put_failure;

Expand Down

0 comments on commit 5337cbc

Please sign in to comment.