blob: 16d0960062be9a5fd0bb8c031e224c04595313c2 [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
27 return inet_sk_diag_fill(sk, NULL, skb, req, NETLINK_CB(cb->skb).pid,
28 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
29}
30
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000031static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000032 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000033{
Pavel Emelyanova925aa02011-12-09 06:24:06 +000034 int err = -EINVAL;
35 struct sock *sk;
36 struct sk_buff *rep;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000037 struct net *net = sock_net(in_skb->sk);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000038
39 if (req->sdiag_family == AF_INET)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000040 sk = __udp4_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000041 req->id.idiag_src[0], req->id.idiag_sport,
42 req->id.idiag_dst[0], req->id.idiag_dport,
43 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000044#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanova925aa02011-12-09 06:24:06 +000045 else if (req->sdiag_family == AF_INET6)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000046 sk = __udp6_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000047 (struct in6_addr *)req->id.idiag_src,
48 req->id.idiag_sport,
49 (struct in6_addr *)req->id.idiag_dst,
50 req->id.idiag_dport,
51 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000052#endif
Pavel Emelyanova925aa02011-12-09 06:24:06 +000053 else
54 goto out_nosk;
55
56 err = -ENOENT;
57 if (sk == NULL)
58 goto out_nosk;
59
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000060 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000061 if (err)
62 goto out;
63
64 err = -ENOMEM;
65 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
66 sizeof(struct inet_diag_meminfo) +
67 64)), GFP_KERNEL);
68 if (!rep)
69 goto out;
70
71 err = inet_sk_diag_fill(sk, NULL, rep, req,
72 NETLINK_CB(in_skb).pid,
73 nlh->nlmsg_seq, 0, nlh);
74 if (err < 0) {
75 WARN_ON(err == -EMSGSIZE);
76 kfree_skb(rep);
77 goto out;
78 }
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000079 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).pid,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000080 MSG_DONTWAIT);
81 if (err > 0)
82 err = 0;
83out:
84 if (sk)
85 sock_put(sk);
86out_nosk:
87 return err;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000088}
89
90static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000091 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000092{
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000093 int num, s_num, slot, s_slot;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000094 struct net *net = sock_net(skb->sk);
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000095
96 s_slot = cb->args[0];
97 num = s_num = cb->args[1];
98
99 for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
100 struct sock *sk;
101 struct hlist_nulls_node *node;
102 struct udp_hslot *hslot = &table->hash[slot];
103
104 if (hlist_nulls_empty(&hslot->head))
105 continue;
106
107 spin_lock_bh(&hslot->lock);
108 sk_nulls_for_each(sk, node, &hslot->head) {
109 struct inet_sock *inet = inet_sk(sk);
110
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000111 if (!net_eq(sock_net(sk), net))
112 continue;
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000113 if (num < s_num)
114 goto next;
115 if (!(r->idiag_states & (1 << sk->sk_state)))
116 goto next;
117 if (r->sdiag_family != AF_UNSPEC &&
118 sk->sk_family != r->sdiag_family)
119 goto next;
120 if (r->id.idiag_sport != inet->inet_sport &&
121 r->id.idiag_sport)
122 goto next;
123 if (r->id.idiag_dport != inet->inet_dport &&
124 r->id.idiag_dport)
125 goto next;
126
127 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
128 spin_unlock_bh(&hslot->lock);
129 goto done;
130 }
131next:
132 num++;
133 }
134 spin_unlock_bh(&hslot->lock);
135 }
136done:
137 cb->args[0] = slot;
138 cb->args[1] = num;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000139}
140
141static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000142 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000143{
144 udp_dump(&udp_table, skb, cb, r, bc);
145}
146
147static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000148 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000149{
150 return udp_dump_one(&udp_table, in_skb, nlh, req);
151}
152
Shan Wei62ad6fc2012-04-24 18:15:41 +0000153static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
154 void *info)
155{
156 r->idiag_rqueue = sk_rmem_alloc_get(sk);
157 r->idiag_wqueue = sk_wmem_alloc_get(sk);
158}
159
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000160static const struct inet_diag_handler udp_diag_handler = {
161 .dump = udp_diag_dump,
162 .dump_one = udp_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000163 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000164 .idiag_type = IPPROTO_UDP,
165};
166
167static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000168 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000169{
170 udp_dump(&udplite_table, skb, cb, r, bc);
171}
172
173static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000174 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000175{
176 return udp_dump_one(&udplite_table, in_skb, nlh, req);
177}
178
179static const struct inet_diag_handler udplite_diag_handler = {
180 .dump = udplite_diag_dump,
181 .dump_one = udplite_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000182 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000183 .idiag_type = IPPROTO_UDPLITE,
184};
185
186static int __init udp_diag_init(void)
187{
188 int err;
189
190 err = inet_diag_register(&udp_diag_handler);
191 if (err)
192 goto out;
193 err = inet_diag_register(&udplite_diag_handler);
194 if (err)
195 goto out_lite;
196out:
197 return err;
198out_lite:
199 inet_diag_unregister(&udp_diag_handler);
200 goto out;
201}
202
203static void __exit udp_diag_exit(void)
204{
205 inet_diag_unregister(&udplite_diag_handler);
206 inet_diag_unregister(&udp_diag_handler);
207}
208
209module_init(udp_diag_init);
210module_exit(udp_diag_exit);
211MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000212MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
213MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);