blob: a7f86a3cd5023e2a7489167fcdd84df0d75a26a0 [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;
37
38 if (req->sdiag_family == AF_INET)
39 sk = __udp4_lib_lookup(&init_net,
40 req->id.idiag_src[0], req->id.idiag_sport,
41 req->id.idiag_dst[0], req->id.idiag_dport,
42 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000043#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanova925aa02011-12-09 06:24:06 +000044 else if (req->sdiag_family == AF_INET6)
45 sk = __udp6_lib_lookup(&init_net,
46 (struct in6_addr *)req->id.idiag_src,
47 req->id.idiag_sport,
48 (struct in6_addr *)req->id.idiag_dst,
49 req->id.idiag_dport,
50 req->id.idiag_if, tbl);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000051#endif
Pavel Emelyanova925aa02011-12-09 06:24:06 +000052 else
53 goto out_nosk;
54
55 err = -ENOENT;
56 if (sk == NULL)
57 goto out_nosk;
58
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000059 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000060 if (err)
61 goto out;
62
63 err = -ENOMEM;
64 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
65 sizeof(struct inet_diag_meminfo) +
66 64)), GFP_KERNEL);
67 if (!rep)
68 goto out;
69
70 err = inet_sk_diag_fill(sk, NULL, rep, req,
71 NETLINK_CB(in_skb).pid,
72 nlh->nlmsg_seq, 0, nlh);
73 if (err < 0) {
74 WARN_ON(err == -EMSGSIZE);
75 kfree_skb(rep);
76 goto out;
77 }
78 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
79 MSG_DONTWAIT);
80 if (err > 0)
81 err = 0;
82out:
83 if (sk)
84 sock_put(sk);
85out_nosk:
86 return err;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000087}
88
89static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000090 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000091{
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000092 int num, s_num, slot, s_slot;
93
94 s_slot = cb->args[0];
95 num = s_num = cb->args[1];
96
97 for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
98 struct sock *sk;
99 struct hlist_nulls_node *node;
100 struct udp_hslot *hslot = &table->hash[slot];
101
102 if (hlist_nulls_empty(&hslot->head))
103 continue;
104
105 spin_lock_bh(&hslot->lock);
106 sk_nulls_for_each(sk, node, &hslot->head) {
107 struct inet_sock *inet = inet_sk(sk);
108
109 if (num < s_num)
110 goto next;
111 if (!(r->idiag_states & (1 << sk->sk_state)))
112 goto next;
113 if (r->sdiag_family != AF_UNSPEC &&
114 sk->sk_family != r->sdiag_family)
115 goto next;
116 if (r->id.idiag_sport != inet->inet_sport &&
117 r->id.idiag_sport)
118 goto next;
119 if (r->id.idiag_dport != inet->inet_dport &&
120 r->id.idiag_dport)
121 goto next;
122
123 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
124 spin_unlock_bh(&hslot->lock);
125 goto done;
126 }
127next:
128 num++;
129 }
130 spin_unlock_bh(&hslot->lock);
131 }
132done:
133 cb->args[0] = slot;
134 cb->args[1] = num;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000135}
136
137static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000138 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000139{
140 udp_dump(&udp_table, skb, cb, r, bc);
141}
142
143static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000144 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000145{
146 return udp_dump_one(&udp_table, in_skb, nlh, req);
147}
148
Shan Wei62ad6fc2012-04-24 18:15:41 +0000149static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
150 void *info)
151{
152 r->idiag_rqueue = sk_rmem_alloc_get(sk);
153 r->idiag_wqueue = sk_wmem_alloc_get(sk);
154}
155
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000156static const struct inet_diag_handler udp_diag_handler = {
157 .dump = udp_diag_dump,
158 .dump_one = udp_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000159 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000160 .idiag_type = IPPROTO_UDP,
161};
162
163static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000164 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000165{
166 udp_dump(&udplite_table, skb, cb, r, bc);
167}
168
169static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000170 struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000171{
172 return udp_dump_one(&udplite_table, in_skb, nlh, req);
173}
174
175static const struct inet_diag_handler udplite_diag_handler = {
176 .dump = udplite_diag_dump,
177 .dump_one = udplite_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000178 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000179 .idiag_type = IPPROTO_UDPLITE,
180};
181
182static int __init udp_diag_init(void)
183{
184 int err;
185
186 err = inet_diag_register(&udp_diag_handler);
187 if (err)
188 goto out;
189 err = inet_diag_register(&udplite_diag_handler);
190 if (err)
191 goto out_lite;
192out:
193 return err;
194out_lite:
195 inet_diag_unregister(&udp_diag_handler);
196 goto out;
197}
198
199static void __exit udp_diag_exit(void)
200{
201 inet_diag_unregister(&udplite_diag_handler);
202 inet_diag_unregister(&udp_diag_handler);
203}
204
205module_init(udp_diag_init);
206module_exit(udp_diag_exit);
207MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000208MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
209MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);