blob: 4a000f1dd75753833b792f6979bf697337f4dd7a [file] [log] [blame]
Pavel Emelyanov52b7c592011-12-09 06:23:51 +00001/*
2 * udp_diag.c Module for monitoring UDP transport protocols sockets.
3 *
4 * Authors: Pavel Emelyanov, <xemul@parallels.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
13#include <linux/module.h>
14#include <linux/inet_diag.h>
15#include <linux/udp.h>
16#include <net/udp.h>
17#include <net/udplite.h>
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000018#include <linux/sock_diag.h>
19
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000020static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000021 struct netlink_callback *cb, struct inet_diag_req_v2 *req,
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000022 struct nlattr *bc)
23{
24 if (!inet_diag_bc_sk(bc, sk))
25 return 0;
26
Eric W. Biedermand06ca952012-05-24 17:58:08 -060027 return inet_sk_diag_fill(sk, NULL, skb, req,
Patrick McHardye32123e2013-04-17 06:46:57 +000028 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +000029 NETLINK_CB(cb->skb).portid,
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000030 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
31}
32
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000033static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000034 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000035{
Pavel Emelyanova925aa02011-12-09 06:24:06 +000036 int err = -EINVAL;
37 struct sock *sk;
38 struct sk_buff *rep;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000039 struct net *net = sock_net(in_skb->sk);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000040
41 if (req->sdiag_family == AF_INET)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000042 sk = __udp4_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000043 req->id.idiag_src[0], req->id.idiag_sport,
44 req->id.idiag_dst[0], req->id.idiag_dport,
45 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000046#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanova925aa02011-12-09 06:24:06 +000047 else if (req->sdiag_family == AF_INET6)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000048 sk = __udp6_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000049 (struct in6_addr *)req->id.idiag_src,
50 req->id.idiag_sport,
51 (struct in6_addr *)req->id.idiag_dst,
52 req->id.idiag_dport,
53 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000054#endif
Pavel Emelyanova925aa02011-12-09 06:24:06 +000055 else
56 goto out_nosk;
57
58 err = -ENOENT;
59 if (sk == NULL)
60 goto out_nosk;
61
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000062 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000063 if (err)
64 goto out;
65
66 err = -ENOMEM;
Hong zhi guo573ce262013-03-27 06:47:04 +000067 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
68 sizeof(struct inet_diag_meminfo) + 64,
69 GFP_KERNEL);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000070 if (!rep)
71 goto out;
72
73 err = inet_sk_diag_fill(sk, NULL, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +000074 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +000075 NETLINK_CB(in_skb).portid,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000076 nlh->nlmsg_seq, 0, nlh);
77 if (err < 0) {
78 WARN_ON(err == -EMSGSIZE);
79 kfree_skb(rep);
80 goto out;
81 }
Eric W. Biederman15e47302012-09-07 20:12:54 +000082 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000083 MSG_DONTWAIT);
84 if (err > 0)
85 err = 0;
86out:
87 if (sk)
88 sock_put(sk);
89out_nosk:
90 return err;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000091}
92
93static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000094 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000095{
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000096 int num, s_num, slot, s_slot;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000097 struct net *net = sock_net(skb->sk);
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000098
99 s_slot = cb->args[0];
100 num = s_num = cb->args[1];
101
Herbert Xu86f3cdd2015-01-24 08:02:40 +1100102 for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000103 struct sock *sk;
104 struct hlist_nulls_node *node;
105 struct udp_hslot *hslot = &table->hash[slot];
106
Herbert Xu86f3cdd2015-01-24 08:02:40 +1100107 num = 0;
108
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000109 if (hlist_nulls_empty(&hslot->head))
110 continue;
111
112 spin_lock_bh(&hslot->lock);
113 sk_nulls_for_each(sk, node, &hslot->head) {
114 struct inet_sock *inet = inet_sk(sk);
115
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000116 if (!net_eq(sock_net(sk), net))
117 continue;
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000118 if (num < s_num)
119 goto next;
120 if (!(r->idiag_states & (1 << sk->sk_state)))
121 goto next;
122 if (r->sdiag_family != AF_UNSPEC &&
123 sk->sk_family != r->sdiag_family)
124 goto next;
125 if (r->id.idiag_sport != inet->inet_sport &&
126 r->id.idiag_sport)
127 goto next;
128 if (r->id.idiag_dport != inet->inet_dport &&
129 r->id.idiag_dport)
130 goto next;
131
132 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
133 spin_unlock_bh(&hslot->lock);
134 goto done;
135 }
136next:
137 num++;
138 }
139 spin_unlock_bh(&hslot->lock);
140 }
141done:
142 cb->args[0] = slot;
143 cb->args[1] = num;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000144}
145
146static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000147 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000148{
149 udp_dump(&udp_table, skb, cb, r, bc);
150}
151
152static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000153 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000154{
155 return udp_dump_one(&udp_table, in_skb, nlh, req);
156}
157
Shan Wei62ad6fc2012-04-24 18:15:41 +0000158static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
159 void *info)
160{
161 r->idiag_rqueue = sk_rmem_alloc_get(sk);
162 r->idiag_wqueue = sk_wmem_alloc_get(sk);
163}
164
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000165static const struct inet_diag_handler udp_diag_handler = {
166 .dump = udp_diag_dump,
167 .dump_one = udp_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000168 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000169 .idiag_type = IPPROTO_UDP,
170};
171
172static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000173 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000174{
175 udp_dump(&udplite_table, skb, cb, r, bc);
176}
177
178static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000179 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000180{
181 return udp_dump_one(&udplite_table, in_skb, nlh, req);
182}
183
184static const struct inet_diag_handler udplite_diag_handler = {
185 .dump = udplite_diag_dump,
186 .dump_one = udplite_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000187 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000188 .idiag_type = IPPROTO_UDPLITE,
189};
190
191static int __init udp_diag_init(void)
192{
193 int err;
194
195 err = inet_diag_register(&udp_diag_handler);
196 if (err)
197 goto out;
198 err = inet_diag_register(&udplite_diag_handler);
199 if (err)
200 goto out_lite;
201out:
202 return err;
203out_lite:
204 inet_diag_unregister(&udp_diag_handler);
205 goto out;
206}
207
208static void __exit udp_diag_exit(void)
209{
210 inet_diag_unregister(&udplite_diag_handler);
211 inet_diag_unregister(&udp_diag_handler);
212}
213
214module_init(udp_diag_init);
215module_exit(udp_diag_exit);
216MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc622011-12-15 02:43:27 +0000217MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
218MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);