Skip to content

Commit 13ccf04

Browse files
committed
Merge branch 'pktdiag'
Nicolas Dichtel says: ==================== The goal of this patchset is to be able to get all infos exported via the /proc/net/packet and also beeing able to get filter associated to af_packet sockets. As usual, the patch against iproute2 will be sent once the patches are included and net-next merged. I can send it on demand. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents fdd5f43 + e8d9612 commit 13ccf04

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

include/linux/sock_diag.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __SOCK_DIAG_H__
22
#define __SOCK_DIAG_H__
33

4+
#include <linux/user_namespace.h>
45
#include <uapi/linux/sock_diag.h>
56

67
struct sk_buff;
@@ -22,5 +23,7 @@ int sock_diag_check_cookie(void *sk, __u32 *cookie);
2223
void sock_diag_save_cookie(void *sk, __u32 *cookie);
2324

2425
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
26+
int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
27+
struct sk_buff *skb, int attrtype);
2528

2629
#endif

include/uapi/linux/packet_diag.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct packet_diag_req {
1616
#define PACKET_SHOW_MCLIST0x00000002 /* A set of packet_diag_mclist-s */
1717
#define PACKET_SHOW_RING_CFG0x00000004 /* Rings configuration parameters */
1818
#define PACKET_SHOW_FANOUT0x00000008
19+
#define PACKET_SHOW_MEMINFO0x00000010
20+
#define PACKET_SHOW_FILTER0x00000020
1921

2022
struct packet_diag_msg {
2123
__u8pdiag_family;
@@ -32,6 +34,9 @@ enum {
3234
PACKET_DIAG_RX_RING,
3335
PACKET_DIAG_TX_RING,
3436
PACKET_DIAG_FANOUT,
37+
PACKET_DIAG_UID,
38+
PACKET_DIAG_MEMINFO,
39+
PACKET_DIAG_FILTER,
3540

3641
__PACKET_DIAG_MAX,
3742
};

net/core/sock_diag.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@ int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
4949
}
5050
EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
5151

52+
int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
53+
struct sk_buff *skb, int attrtype)
54+
{
55+
struct nlattr *attr;
56+
struct sk_filter *filter;
57+
unsigned int len;
58+
int err = 0;
59+
60+
if (!ns_capable(user_ns, CAP_NET_ADMIN)) {
61+
nla_reserve(skb, attrtype, 0);
62+
return 0;
63+
}
64+
65+
rcu_read_lock();
66+
67+
filter = rcu_dereference(sk->sk_filter);
68+
len = filter ? filter->len * sizeof(struct sock_filter) : 0;
69+
70+
attr = nla_reserve(skb, attrtype, len);
71+
if (attr == NULL) {
72+
err = -EMSGSIZE;
73+
goto out;
74+
}
75+
76+
if (filter)
77+
memcpy(nla_data(attr), filter->insns, len);
78+
79+
out:
80+
rcu_read_unlock();
81+
return err;
82+
}
83+
EXPORT_SYMBOL(sock_diag_put_filterinfo);
84+
5285
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
5386
{
5487
mutex_lock(&sock_diag_table_mutex);

net/packet/diag.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)
125125
return ret;
126126
}
127127

128-
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag_req *req,
129-
u32 portid, u32 seq, u32 flags, int sk_ino)
128+
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
129+
struct packet_diag_req *req,
130+
struct user_namespace *user_ns,
131+
u32 portid, u32 seq, u32 flags, int sk_ino)
130132
{
131133
struct nlmsghdr *nlh;
132134
struct packet_diag_msg *rp;
@@ -147,6 +149,11 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag
147149
pdiag_put_info(po, skb))
148150
goto out_nlmsg_trim;
149151

152+
if ((req->pdiag_show & PACKET_SHOW_INFO) &&
153+
nla_put_u32(skb, PACKET_DIAG_UID,
154+
from_kuid_munged(user_ns, sock_i_uid(sk))))
155+
goto out_nlmsg_trim;
156+
150157
if ((req->pdiag_show & PACKET_SHOW_MCLIST) &&
151158
pdiag_put_mclist(po, skb))
152159
goto out_nlmsg_trim;
@@ -159,6 +166,14 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag
159166
pdiag_put_fanout(po, skb))
160167
goto out_nlmsg_trim;
161168

169+
if ((req->pdiag_show & PACKET_SHOW_MEMINFO) &&
170+
sock_diag_put_meminfo(sk, skb, PACKET_DIAG_MEMINFO))
171+
goto out_nlmsg_trim;
172+
173+
if ((req->pdiag_show & PACKET_SHOW_FILTER) &&
174+
sock_diag_put_filterinfo(user_ns, sk, skb, PACKET_DIAG_FILTER))
175+
goto out_nlmsg_trim;
176+
162177
return nlmsg_end(skb, nlh);
163178

164179
out_nlmsg_trim:
@@ -183,9 +198,11 @@ static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
183198
if (num < s_num)
184199
goto next;
185200

186-
if (sk_diag_fill(sk, skb, req, NETLINK_CB(cb->skb).portid,
187-
cb->nlh->nlmsg_seq, NLM_F_MULTI,
188-
sock_i_ino(sk)) < 0)
201+
if (sk_diag_fill(sk, skb, req,
202+
sk_user_ns(NETLINK_CB(cb->skb).sk),
203+
NETLINK_CB(cb->skb).portid,
204+
cb->nlh->nlmsg_seq, NLM_F_MULTI,
205+
sock_i_ino(sk)) < 0)
189206
goto done;
190207
next:
191208
num++;

0 commit comments

Comments
 (0)