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
2323
2424 extern struct proto raw_prot;
2525
26+extern struct raw_hashinfo raw_v4_hashinfo;
27+struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
28+ unsigned short num, __be32 raddr,
29+ __be32 laddr, int dif);
30+
31+int raw_abort(struct sock *sk, int err);
2632 void raw_icmp_error(struct sk_buff *, int, u32);
2733 int raw_local_deliver(struct sk_buff *, int);
2834
include/net/rawv6.h+7−0
@@ -3,6 +3,13 @@ · 2 unmodified lines
33
44 #include <net/protocol.h>
55
6+extern struct raw_hashinfo raw_v6_hashinfo;
7+struct 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+
11+int raw_abort(struct sock *sk, int err);
12+
613 void raw6_icmp_error(struct sk_buff *, int nexthdr,
714 u8 type, u8 code, int inner_offset, __be32);
815 bool 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
4343 struct inet_diag_sockid id;
4444 };
4545
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+ */
54+struct 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+
4663 enum {
4764 INET_DIAG_REQ_NONE,
4865 INET_DIAG_REQ_BYTECODE,
net/ipv4/Kconfig+8−0
@@ -430,6 +430,14 @@ config INET_UDP_DIAG · 429 unmodified lines
430430 Support for UDP socket monitoring interface used by the ss tool.
431431 If unsure, say Y.
432432
433+config 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+
433441 config INET_DIAG_DESTROY
434442 bool "INET: allow privileged process to administratively close sockets"
435443 depends on INET_DIAG
net/ipv4/Makefile+1−0
@@ -40,6 +40,7 @@ obj-$(CONFIG_NETFILTER) += netfilter.o netfilter/ · 39 unmodified lines
4040 obj-$(CONFIG_INET_DIAG) += inet_diag.o
4141 obj-$(CONFIG_INET_TCP_DIAG) += tcp_diag.o
4242 obj-$(CONFIG_INET_UDP_DIAG) += udp_diag.o
43+obj-$(CONFIG_INET_RAW_DIAG) += raw_diag.o
4344 obj-$(CONFIG_NET_TCPPROBE) += tcp_probe.o
4445 obj-$(CONFIG_TCP_CONG_BBR) += tcp_bbr.o
4546 obj-$(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
200200 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
201201 goto errout;
202202
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+
203212 if (!icsk) {
204213 handler->idiag_get_info(sk, r, NULL);
205214 goto out;
net/ipv4/raw.c+19−2
@@ -89,9 +89,10 @@ struct raw_frag_vec { · 88 unmodified lines
8989 int hlen;
9090 };
9191
92−static struct raw_hashinfo raw_v4_hashinfo = {
92+struct raw_hashinfo raw_v4_hashinfo = {
9393 .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
9494 };
95+EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
9596
9697 int raw_hash_sk(struct sock *sk)
9798 {
@@ -120,7 +121,7 @@ void raw_unhash_sk(struct sock *sk) · 22 unmodified lines
120121 }
121122 EXPORT_SYMBOL_GPL(raw_unhash_sk);
122123
123−static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
124+struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
124125 unsigned short num, __be32 raddr, __be32 laddr, int dif)
125126 {
126127 sk_for_each_from(sk) {
@@ -136,6 +137,7 @@ static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk, · 9 unmodified lines
136137 found:
137138 return sk;
138139 }
140+EXPORT_SYMBOL_GPL(__raw_v4_lookup);
139141
140142 /*
141143 * 0 - deliver
@@ -912,6 +914,20 @@ static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg · 770 unmodified lines
912914 }
913915 #endif
914916
917+int 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+}
929+EXPORT_SYMBOL_GPL(raw_abort);
930+
915931 struct proto raw_prot = {
916932 .name = "RAW",
917933 .owner = THIS_MODULE,
@@ -937,6 +953,7 @@ struct proto raw_prot = { · 19 unmodified lines
937953 .compat_getsockopt = compat_raw_getsockopt,
938954 .compat_ioctl = compat_raw_ioctl,
939955 #endif
956+ .diag_destroy = raw_abort,
940957 };
941958
942959 #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+
15+static struct raw_hashinfo *
16+raw_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+
38+static 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+
59+static 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+
90+static 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+
131+static 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+
146+static 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;
182+next:
183+ num++;
184+ }
185+ }
186+
187+out_unlock:
188+ read_unlock(&hashinfo->lock);
189+
190+ cb->args[0] = slot;
191+ cb->args[1] = num;
192+}
193+
194+static 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
202+static 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+
215+static 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+
226+static 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+
247+static int __init raw_diag_init(void)
248+{
249+ return inet_diag_register(&raw_diag_handler);
250+}
251+
252+static void __exit raw_diag_exit(void)
253+{
254+ inet_diag_unregister(&raw_diag_handler);
255+}
256+
257+module_init(raw_diag_init);
258+module_exit(raw_diag_exit);
259+MODULE_LICENSE("GPL");
260+MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
261+MODULE_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
6565
6666 #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */
6767
68−static struct raw_hashinfo raw_v6_hashinfo = {
68+struct raw_hashinfo raw_v6_hashinfo = {
6969 .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
7070 };
71+EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
7172
72−static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
73+struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
7374 unsigned short num, const struct in6_addr *loc_addr,
7475 const struct in6_addr *rmt_addr, int dif)
7576 {
@@ -102,6 +103,7 @@ static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk, · 26 unmodified lines
102103 found:
103104 return sk;
104105 }
106+EXPORT_SYMBOL_GPL(__raw_v6_lookup);
105107
106108 /*
107109 * 0 - deliver
@@ -1259,6 +1261,7 @@ struct proto rawv6_prot = { · 1151 unmodified lines
12591261 .compat_getsockopt = compat_rawv6_getsockopt,
12601262 .compat_ioctl = compat_rawv6_ioctl,
12611263 #endif
1264+ .diag_destroy = raw_abort,
12621265 };
12631266
12641267 #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