History

net: ip, diag -- Add diag interface for raw sockets

In criu we are actively using diag interface to collect sockets present in the system when dumping applications. And while for unix, tcp, udp[lite], packet, netlink it works as expected, the raw sockets do not have. Thus add it. v2: - add missing sock_put calls in raw_diag_dump_one (by eric.dumazet@) - implement @destroy for diag requests (by dsa@) v3: - add export of raw_abort for IPv6 (by dsa@) - pass net-admin flag into inet_sk_diag_fill due to changes in net-next branch (by dsa@) v4: - use @pad in struct inet_diag_req_v2 for raw socket protocol specification: raw module carries sockets which may have custom protocol passed from socket() syscall and sole @sdiag_protocol is not enough to match underlied ones - start reporting protocol specifed in socket() call when sockets are raw ones for the same reason: user space tools like ss may parse this attribute and use it for socket matching v5 (by eric.dumazet@): - use sock_hold in raw_sock_get instead of atomic_inc, we're holding (raw_v4_hashinfo|raw_v6_hashinfo)->lock when looking up so counter won't be zero here. v6: - use sdiag_raw_protocol() helper which will access @pad structure used for raw sockets protocol specification: we can't simply rename this member without breaking uapi v7: - sine sdiag_raw_protocol() helper is not suitable for uapi lets rather make an alias structure with proper names. __check_inet_diag_req_raw helper will catch if any of structure unintentionally changed.

Cyrill Gorcunov committed 10 years ago

Commit 432490f1 parent f76a9db9 files · +333 −4Browse files

Unified · Split

9 files
  1. include/net/raw.h +6 −0
  2. include/net/rawv6.h +7 −0
  3. include/uapi/linux/inet_diag.h +17 −0
  4. net/ipv4/Kconfig +8 −0
  5. net/ipv4/Makefile +1 −0
  6. net/ipv4/inet_diag.c +9 −0
  7. net/ipv4/raw.c +19 −2
  8. net/ipv4/raw_diag.c +261 −0
  9. net/ipv6/raw.c +5 −2
include/net/raw.h+6−0
@@ -23,6 +23,12 @@ · 22 unmodified lines
23 23
24extern struct proto raw_prot;24extern struct proto raw_prot;
25 25
26extern struct raw_hashinfo raw_v4_hashinfo;
27struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
28 unsigned short num, __be32 raddr,
29 __be32 laddr, int dif);
30
31int raw_abort(struct sock *sk, int err);
26void raw_icmp_error(struct sk_buff *, int, u32);32void raw_icmp_error(struct sk_buff *, int, u32);
27int raw_local_deliver(struct sk_buff *, int);33int raw_local_deliver(struct sk_buff *, int);
28 34
include/net/rawv6.h+7−0
@@ -3,6 +3,13 @@ · 2 unmodified lines
3 3
4#include <net/protocol.h>4#include <net/protocol.h>
5 5
6extern struct raw_hashinfo raw_v6_hashinfo;
7struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
8 unsigned short num, const struct in6_addr *loc_addr,
9 const struct in6_addr *rmt_addr, int dif);
10
11int raw_abort(struct sock *sk, int err);
12
6void raw6_icmp_error(struct sk_buff *, int nexthdr,13void raw6_icmp_error(struct sk_buff *, int nexthdr,
7 u8 type, u8 code, int inner_offset, __be32);14 u8 type, u8 code, int inner_offset, __be32);
8bool raw6_local_deliver(struct sk_buff *, int);15bool raw6_local_deliver(struct sk_buff *, int);
include/uapi/linux/inet_diag.h+17−0
@@ -43,6 +43,23 @@ struct inet_diag_req_v2 { · 42 unmodified lines
43 struct inet_diag_sockid id;43 struct inet_diag_sockid id;
44};44};
45 45
46/*
47 * SOCK_RAW sockets require the underlied protocol to be
48 * additionally specified so we can use @pad member for
49 * this, but we can't rename it because userspace programs
50 * still may depend on this name. Instead lets use another
51 * structure definition as an alias for struct
52 * @inet_diag_req_v2.
53 */
54struct inet_diag_req_raw {
55 __u8 sdiag_family;
56 __u8 sdiag_protocol;
57 __u8 idiag_ext;
58 __u8 sdiag_raw_protocol;
59 __u32 idiag_states;
60 struct inet_diag_sockid id;
61};
62
46enum {63enum {
47 INET_DIAG_REQ_NONE,64 INET_DIAG_REQ_NONE,
48 INET_DIAG_REQ_BYTECODE,65 INET_DIAG_REQ_BYTECODE,
net/ipv4/Kconfig+8−0
@@ -430,6 +430,14 @@ config INET_UDP_DIAG · 429 unmodified lines
430 Support for UDP socket monitoring interface used by the ss tool.430 Support for UDP socket monitoring interface used by the ss tool.
431 If unsure, say Y.431 If unsure, say Y.
432 432
433config INET_RAW_DIAG
434 tristate "RAW: socket monitoring interface"
435 depends on INET_DIAG && (IPV6 || IPV6=n)
436 default n
437 ---help---
438 Support for RAW socket monitoring interface used by the ss tool.
439 If unsure, say Y.
440
433config INET_DIAG_DESTROY441config INET_DIAG_DESTROY
434 bool "INET: allow privileged process to administratively close sockets"442 bool "INET: allow privileged process to administratively close sockets"
435 depends on INET_DIAG443 depends on INET_DIAG
net/ipv4/Makefile+1−0
@@ -40,6 +40,7 @@ obj-$(CONFIG_NETFILTER) += netfilter.o netfilter/ · 39 unmodified lines
40obj-$(CONFIG_INET_DIAG) += inet_diag.o 40obj-$(CONFIG_INET_DIAG) += inet_diag.o
41obj-$(CONFIG_INET_TCP_DIAG) += tcp_diag.o41obj-$(CONFIG_INET_TCP_DIAG) += tcp_diag.o
42obj-$(CONFIG_INET_UDP_DIAG) += udp_diag.o42obj-$(CONFIG_INET_UDP_DIAG) += udp_diag.o
43obj-$(CONFIG_INET_RAW_DIAG) += raw_diag.o
43obj-$(CONFIG_NET_TCPPROBE) += tcp_probe.o44obj-$(CONFIG_NET_TCPPROBE) += tcp_probe.o
44obj-$(CONFIG_TCP_CONG_BBR) += tcp_bbr.o45obj-$(CONFIG_TCP_CONG_BBR) += tcp_bbr.o
45obj-$(CONFIG_TCP_CONG_BIC) += tcp_bic.o46obj-$(CONFIG_TCP_CONG_BIC) += tcp_bic.o
net/ipv4/inet_diag.c+9−0
@@ -200,6 +200,15 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, · 199 unmodified lines
200 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))200 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
201 goto errout;201 goto errout;
202 202
203 /*
204 * RAW sockets might have user-defined protocols assigned,
205 * so report the one supplied on socket creation.
206 */
207 if (sk->sk_type == SOCK_RAW) {
208 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
209 goto errout;
210 }
211
203 if (!icsk) {212 if (!icsk) {
204 handler->idiag_get_info(sk, r, NULL);213 handler->idiag_get_info(sk, r, NULL);
205 goto out;214 goto out;
net/ipv4/raw.c+19−2
@@ -89,9 +89,10 @@ struct raw_frag_vec { · 88 unmodified lines
89 int hlen;89 int hlen;
90};90};
91 91
92static struct raw_hashinfo raw_v4_hashinfo = {92struct raw_hashinfo raw_v4_hashinfo = {
93 .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),93 .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
94};94};
95EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
95 96
96int raw_hash_sk(struct sock *sk)97int raw_hash_sk(struct sock *sk)
97{98{
@@ -120,7 +121,7 @@ void raw_unhash_sk(struct sock *sk) · 22 unmodified lines
120}121}
121EXPORT_SYMBOL_GPL(raw_unhash_sk);122EXPORT_SYMBOL_GPL(raw_unhash_sk);
122 123
123static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,124struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
124 unsigned short num, __be32 raddr, __be32 laddr, int dif)125 unsigned short num, __be32 raddr, __be32 laddr, int dif)
125{126{
126 sk_for_each_from(sk) {127 sk_for_each_from(sk) {
@@ -136,6 +137,7 @@ static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk, · 9 unmodified lines
136found:137found:
137 return sk;138 return sk;
138}139}
140EXPORT_SYMBOL_GPL(__raw_v4_lookup);
139 141
140/*142/*
141 * 0 - deliver143 * 0 - deliver
@@ -912,6 +914,20 @@ static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg · 770 unmodified lines
912}914}
913#endif915#endif
914 916
917int raw_abort(struct sock *sk, int err)
918{
919 lock_sock(sk);
920
921 sk->sk_err = err;
922 sk->sk_error_report(sk);
923 udp_disconnect(sk, 0);
924
925 release_sock(sk);
926
927 return 0;
928}
929EXPORT_SYMBOL_GPL(raw_abort);
930
915struct proto raw_prot = {931struct proto raw_prot = {
916 .name = "RAW",932 .name = "RAW",
917 .owner = THIS_MODULE,933 .owner = THIS_MODULE,
@@ -937,6 +953,7 @@ struct proto raw_prot = { · 19 unmodified lines
937 .compat_getsockopt = compat_raw_getsockopt,953 .compat_getsockopt = compat_raw_getsockopt,
938 .compat_ioctl = compat_raw_ioctl,954 .compat_ioctl = compat_raw_ioctl,
939#endif955#endif
956 .diag_destroy = raw_abort,
940};957};
941 958
942#ifdef CONFIG_PROC_FS959#ifdef CONFIG_PROC_FS
net/ipv4/raw_diag.c+261−0
@@ -0,0 +1,261 @@
1#include <linux/module.h>
2
3#include <linux/inet_diag.h>
4#include <linux/sock_diag.h>
5
6#include <net/raw.h>
7#include <net/rawv6.h>
8
9#ifdef pr_fmt
10# undef pr_fmt
11#endif
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15static struct raw_hashinfo *
16raw_get_hashinfo(const struct inet_diag_req_v2 *r)
17{
18 if (r->sdiag_family == AF_INET) {
19 return &raw_v4_hashinfo;
20#if IS_ENABLED(CONFIG_IPV6)
21 } else if (r->sdiag_family == AF_INET6) {
22 return &raw_v6_hashinfo;
23#endif
24 } else {
25 pr_warn_once("Unexpected inet family %d\n",
26 r->sdiag_family);
27 WARN_ON_ONCE(1);
28 return ERR_PTR(-EINVAL);
29 }
30}
31
32/*
33 * Due to requirement of not breaking user API we can't simply
34 * rename @pad field in inet_diag_req_v2 structure, instead
35 * use helper to figure it out.
36 */
37
38static struct sock *raw_lookup(struct net *net, struct sock *from,
39 const struct inet_diag_req_v2 *req)
40{
41 struct inet_diag_req_raw *r = (void *)req;
42 struct sock *sk = NULL;
43
44 if (r->sdiag_family == AF_INET)
45 sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol,
46 r->id.idiag_dst[0],
47 r->id.idiag_src[0],
48 r->id.idiag_if);
49#if IS_ENABLED(CONFIG_IPV6)
50 else
51 sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol,
52 (const struct in6_addr *)r->id.idiag_src,
53 (const struct in6_addr *)r->id.idiag_dst,
54 r->id.idiag_if);
55#endif
56 return sk;
57}
58
59static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
60{
61 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
62 struct sock *sk = NULL, *s;
63 int slot;
64
65 if (IS_ERR(hashinfo))
66 return ERR_CAST(hashinfo);
67
68 read_lock(&hashinfo->lock);
69 for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
70 sk_for_each(s, &hashinfo->ht[slot]) {
71 sk = raw_lookup(net, s, r);
72 if (sk) {
73 /*
74 * Grab it and keep until we fill
75 * diag meaage to be reported, so
76 * caller should call sock_put then.
77 * We can do that because we're keeping
78 * hashinfo->lock here.
79 */
80 sock_hold(sk);
81 break;
82 }
83 }
84 }
85 read_unlock(&hashinfo->lock);
86
87 return sk ? sk : ERR_PTR(-ENOENT);
88}
89
90static int raw_diag_dump_one(struct sk_buff *in_skb,
91 const struct nlmsghdr *nlh,
92 const struct inet_diag_req_v2 *r)
93{
94 struct net *net = sock_net(in_skb->sk);
95 struct sk_buff *rep;
96 struct sock *sk;
97 int err;
98
99 sk = raw_sock_get(net, r);
100 if (IS_ERR(sk))
101 return PTR_ERR(sk);
102
103 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
104 sizeof(struct inet_diag_meminfo) + 64,
105 GFP_KERNEL);
106 if (!rep) {
107 sock_put(sk);
108 return -ENOMEM;
109 }
110
111 err = inet_sk_diag_fill(sk, NULL, rep, r,
112 sk_user_ns(NETLINK_CB(in_skb).sk),
113 NETLINK_CB(in_skb).portid,
114 nlh->nlmsg_seq, 0, nlh,
115 netlink_net_capable(in_skb, CAP_NET_ADMIN));
116 sock_put(sk);
117
118 if (err < 0) {
119 kfree_skb(rep);
120 return err;
121 }
122
123 err = netlink_unicast(net->diag_nlsk, rep,
124 NETLINK_CB(in_skb).portid,
125 MSG_DONTWAIT);
126 if (err > 0)
127 err = 0;
128 return err;
129}
130
131static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
132 struct netlink_callback *cb,
133 const struct inet_diag_req_v2 *r,
134 struct nlattr *bc, bool net_admin)
135{
136 if (!inet_diag_bc_sk(bc, sk))
137 return 0;
138
139 return inet_sk_diag_fill(sk, NULL, skb, r,
140 sk_user_ns(NETLINK_CB(cb->skb).sk),
141 NETLINK_CB(cb->skb).portid,
142 cb->nlh->nlmsg_seq, NLM_F_MULTI,
143 cb->nlh, net_admin);
144}
145
146static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
147 const struct inet_diag_req_v2 *r, struct nlattr *bc)
148{
149 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
150 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
151 struct net *net = sock_net(skb->sk);
152 int num, s_num, slot, s_slot;
153 struct sock *sk = NULL;
154
155 if (IS_ERR(hashinfo))
156 return;
157
158 s_slot = cb->args[0];
159 num = s_num = cb->args[1];
160
161 read_lock(&hashinfo->lock);
162 for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
163 num = 0;
164
165 sk_for_each(sk, &hashinfo->ht[slot]) {
166 struct inet_sock *inet = inet_sk(sk);
167
168 if (!net_eq(sock_net(sk), net))
169 continue;
170 if (num < s_num)
171 goto next;
172 if (sk->sk_family != r->sdiag_family)
173 goto next;
174 if (r->id.idiag_sport != inet->inet_sport &&
175 r->id.idiag_sport)
176 goto next;
177 if (r->id.idiag_dport != inet->inet_dport &&
178 r->id.idiag_dport)
179 goto next;
180 if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
181 goto out_unlock;
182next:
183 num++;
184 }
185 }
186
187out_unlock:
188 read_unlock(&hashinfo->lock);
189
190 cb->args[0] = slot;
191 cb->args[1] = num;
192}
193
194static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
195 void *info)
196{
197 r->idiag_rqueue = sk_rmem_alloc_get(sk);
198 r->idiag_wqueue = sk_wmem_alloc_get(sk);
199}
200
201#ifdef CONFIG_INET_DIAG_DESTROY
202static int raw_diag_destroy(struct sk_buff *in_skb,
203 const struct inet_diag_req_v2 *r)
204{
205 struct net *net = sock_net(in_skb->sk);
206 struct sock *sk;
207
208 sk = raw_sock_get(net, r);
209 if (IS_ERR(sk))
210 return PTR_ERR(sk);
211 return sock_diag_destroy(sk, ECONNABORTED);
212}
213#endif
214
215static const struct inet_diag_handler raw_diag_handler = {
216 .dump = raw_diag_dump,
217 .dump_one = raw_diag_dump_one,
218 .idiag_get_info = raw_diag_get_info,
219 .idiag_type = IPPROTO_RAW,
220 .idiag_info_size = 0,
221#ifdef CONFIG_INET_DIAG_DESTROY
222 .destroy = raw_diag_destroy,
223#endif
224};
225
226static void __always_unused __check_inet_diag_req_raw(void)
227{
228 /*
229 * Make sure the two structures are identical,
230 * except the @pad field.
231 */
232#define __offset_mismatch(m1, m2) \
233 (offsetof(struct inet_diag_req_v2, m1) != \
234 offsetof(struct inet_diag_req_raw, m2))
235
236 BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
237 sizeof(struct inet_diag_req_raw));
238 BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
239 BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
240 BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
241 BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
242 BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
243 BUILD_BUG_ON(__offset_mismatch(id, id));
244#undef __offset_mismatch
245}
246
247static int __init raw_diag_init(void)
248{
249 return inet_diag_register(&raw_diag_handler);
250}
251
252static void __exit raw_diag_exit(void)
253{
254 inet_diag_unregister(&raw_diag_handler);
255}
256
257module_init(raw_diag_init);
258module_exit(raw_diag_exit);
259MODULE_LICENSE("GPL");
260MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
261MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);
net/ipv6/raw.c+5−2
@@ -65,11 +65,12 @@ · 64 unmodified lines
65 65
66#define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */66#define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */
67 67
68static struct raw_hashinfo raw_v6_hashinfo = {68struct raw_hashinfo raw_v6_hashinfo = {
69 .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),69 .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
70};70};
71EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
71 72
72static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,73struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
73 unsigned short num, const struct in6_addr *loc_addr,74 unsigned short num, const struct in6_addr *loc_addr,
74 const struct in6_addr *rmt_addr, int dif)75 const struct in6_addr *rmt_addr, int dif)
75{76{
@@ -102,6 +103,7 @@ static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk, · 26 unmodified lines
102found:103found:
103 return sk;104 return sk;
104}105}
106EXPORT_SYMBOL_GPL(__raw_v6_lookup);
105 107
106/*108/*
107 * 0 - deliver109 * 0 - deliver
@@ -1259,6 +1261,7 @@ struct proto rawv6_prot = { · 1151 unmodified lines
1259 .compat_getsockopt = compat_rawv6_getsockopt,1261 .compat_getsockopt = compat_rawv6_getsockopt,
1260 .compat_ioctl = compat_rawv6_ioctl,1262 .compat_ioctl = compat_rawv6_ioctl,
1261#endif1263#endif
1264 .diag_destroy = raw_abort,
1262};1265};
1263 1266
1264#ifdef CONFIG_PROC_FS1267#ifdef CONFIG_PROC_FS

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t