Merge branch 'inet_diag-add-cgroup-attribute-and-filter'

Dmitry Yakunin says:

====================
inet_diag: add cgroup attribute and filter

This patch series extends inet diag with cgroup v2 ID attribute and
filter. Which allows investigate sockets on per cgroup basis. Patch for
ss is already sent to iproute2-next mailing list.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
David S. Miller 2020-04-30 12:54:02 -07:00
commit 9f04960660
3 changed files with 45 additions and 1 deletions

View File

@ -71,7 +71,11 @@ static inline size_t inet_diag_msg_attrs_size(void)
+ nla_total_size(1) /* INET_DIAG_SKV6ONLY */
#endif
+ nla_total_size(4) /* INET_DIAG_MARK */
+ nla_total_size(4); /* INET_DIAG_CLASS_ID */
+ nla_total_size(4) /* INET_DIAG_CLASS_ID */
#ifdef CONFIG_SOCK_CGROUP_DATA
+ nla_total_size_64bit(sizeof(u64)) /* INET_DIAG_CGROUP_ID */
#endif
;
}
int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
struct inet_diag_msg *r, int ext,

View File

@ -96,6 +96,7 @@ enum {
INET_DIAG_BC_MARK_COND,
INET_DIAG_BC_S_EQ,
INET_DIAG_BC_D_EQ,
INET_DIAG_BC_CGROUP_COND, /* u64 cgroup v2 ID */
};
struct inet_diag_hostcond {
@ -157,6 +158,7 @@ enum {
INET_DIAG_MD5SIG,
INET_DIAG_ULP_INFO,
INET_DIAG_SK_BPF_STORAGES,
INET_DIAG_CGROUP_ID,
__INET_DIAG_MAX,
};

View File

@ -43,6 +43,9 @@ struct inet_diag_entry {
u16 userlocks;
u32 ifindex;
u32 mark;
#ifdef CONFIG_SOCK_CGROUP_DATA
u64 cgroup_id;
#endif
};
static DEFINE_MUTEX(inet_diag_table_mutex);
@ -162,6 +165,13 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
goto errout;
}
#ifdef CONFIG_SOCK_CGROUP_DATA
if (nla_put_u64_64bit(skb, INET_DIAG_CGROUP_ID,
cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)),
INET_DIAG_PAD))
goto errout;
#endif
r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
r->idiag_inode = sock_i_ino(sk);
@ -675,6 +685,16 @@ static int inet_diag_bc_run(const struct nlattr *_bc,
yes = 0;
break;
}
#ifdef CONFIG_SOCK_CGROUP_DATA
case INET_DIAG_BC_CGROUP_COND: {
u64 cgroup_id;
cgroup_id = get_unaligned((const u64 *)(op + 1));
if (cgroup_id != entry->cgroup_id)
yes = 0;
break;
}
#endif
}
if (yes) {
@ -725,6 +745,9 @@ int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
else
entry.mark = 0;
#ifdef CONFIG_SOCK_CGROUP_DATA
entry.cgroup_id = cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data));
#endif
return inet_diag_bc_run(bc, &entry);
}
@ -814,6 +837,15 @@ static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
return len >= *min_len;
}
#ifdef CONFIG_SOCK_CGROUP_DATA
static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len,
int *min_len)
{
*min_len += sizeof(u64);
return len >= *min_len;
}
#endif
static int inet_diag_bc_audit(const struct nlattr *attr,
const struct sk_buff *skb)
{
@ -856,6 +888,12 @@ static int inet_diag_bc_audit(const struct nlattr *attr,
if (!valid_markcond(bc, len, &min_len))
return -EINVAL;
break;
#ifdef CONFIG_SOCK_CGROUP_DATA
case INET_DIAG_BC_CGROUP_COND:
if (!valid_cgroupcond(bc, len, &min_len))
return -EINVAL;
break;
#endif
case INET_DIAG_BC_AUTO:
case INET_DIAG_BC_JMP:
case INET_DIAG_BC_NOP: