Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / net / ipv4 / udp_diag.c
CommitLineData
52b7c59b
PE
1/*
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>
52b7c59b
PE
18#include <linux/sock_diag.h>
19
b6d640c2 20static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
34160ea3
ED
21 struct netlink_callback *cb,
22 const struct inet_diag_req_v2 *req,
23 struct nlattr *bc)
b6d640c2
PE
24{
25 if (!inet_diag_bc_sk(bc, sk))
26 return 0;
27
d06ca956 28 return inet_sk_diag_fill(sk, NULL, skb, req,
e32123e5 29 sk_user_ns(NETLINK_CB(cb->skb).sk),
15e47304 30 NETLINK_CB(cb->skb).portid,
b6d640c2
PE
31 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
32}
33
52b7c59b 34static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
34160ea3
ED
35 const struct nlmsghdr *nlh,
36 const struct inet_diag_req_v2 *req)
52b7c59b 37{
a925aa00
PE
38 int err = -EINVAL;
39 struct sock *sk;
40 struct sk_buff *rep;
51d7cccf 41 struct net *net = sock_net(in_skb->sk);
a925aa00
PE
42
43 if (req->sdiag_family == AF_INET)
51d7cccf 44 sk = __udp4_lib_lookup(net,
a925aa00
PE
45 req->id.idiag_src[0], req->id.idiag_sport,
46 req->id.idiag_dst[0], req->id.idiag_dport,
47 req->id.idiag_if, tbl);
86e62ad6 48#if IS_ENABLED(CONFIG_IPV6)
a925aa00 49 else if (req->sdiag_family == AF_INET6)
51d7cccf 50 sk = __udp6_lib_lookup(net,
a925aa00
PE
51 (struct in6_addr *)req->id.idiag_src,
52 req->id.idiag_sport,
53 (struct in6_addr *)req->id.idiag_dst,
54 req->id.idiag_dport,
55 req->id.idiag_if, tbl);
86e62ad6 56#endif
a925aa00
PE
57 else
58 goto out_nosk;
59
60 err = -ENOENT;
51456b29 61 if (!sk)
a925aa00
PE
62 goto out_nosk;
63
f65c1b53 64 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
a925aa00
PE
65 if (err)
66 goto out;
67
68 err = -ENOMEM;
573ce260
H
69 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
70 sizeof(struct inet_diag_meminfo) + 64,
71 GFP_KERNEL);
a925aa00
PE
72 if (!rep)
73 goto out;
74
75 err = inet_sk_diag_fill(sk, NULL, rep, req,
e32123e5 76 sk_user_ns(NETLINK_CB(in_skb).sk),
15e47304 77 NETLINK_CB(in_skb).portid,
a925aa00
PE
78 nlh->nlmsg_seq, 0, nlh);
79 if (err < 0) {
80 WARN_ON(err == -EMSGSIZE);
81 kfree_skb(rep);
82 goto out;
83 }
15e47304 84 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
a925aa00
PE
85 MSG_DONTWAIT);
86 if (err > 0)
87 err = 0;
88out:
89 if (sk)
90 sock_put(sk);
91out_nosk:
92 return err;
52b7c59b
PE
93}
94
34160ea3
ED
95static void udp_dump(struct udp_table *table, struct sk_buff *skb,
96 struct netlink_callback *cb,
97 const struct inet_diag_req_v2 *r, struct nlattr *bc)
52b7c59b 98{
b6d640c2 99 int num, s_num, slot, s_slot;
51d7cccf 100 struct net *net = sock_net(skb->sk);
b6d640c2
PE
101
102 s_slot = cb->args[0];
103 num = s_num = cb->args[1];
104
86f3cddb 105 for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
b6d640c2
PE
106 struct sock *sk;
107 struct hlist_nulls_node *node;
108 struct udp_hslot *hslot = &table->hash[slot];
109
86f3cddb
HX
110 num = 0;
111
b6d640c2
PE
112 if (hlist_nulls_empty(&hslot->head))
113 continue;
114
115 spin_lock_bh(&hslot->lock);
116 sk_nulls_for_each(sk, node, &hslot->head) {
117 struct inet_sock *inet = inet_sk(sk);
118
51d7cccf
AV
119 if (!net_eq(sock_net(sk), net))
120 continue;
b6d640c2
PE
121 if (num < s_num)
122 goto next;
123 if (!(r->idiag_states & (1 << sk->sk_state)))
124 goto next;
125 if (r->sdiag_family != AF_UNSPEC &&
126 sk->sk_family != r->sdiag_family)
127 goto next;
128 if (r->id.idiag_sport != inet->inet_sport &&
129 r->id.idiag_sport)
130 goto next;
131 if (r->id.idiag_dport != inet->inet_dport &&
132 r->id.idiag_dport)
133 goto next;
134
135 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
136 spin_unlock_bh(&hslot->lock);
137 goto done;
138 }
139next:
140 num++;
141 }
142 spin_unlock_bh(&hslot->lock);
143 }
144done:
145 cb->args[0] = slot;
146 cb->args[1] = num;
52b7c59b
PE
147}
148
149static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
34160ea3 150 const struct inet_diag_req_v2 *r, struct nlattr *bc)
52b7c59b
PE
151{
152 udp_dump(&udp_table, skb, cb, r, bc);
153}
154
155static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
34160ea3 156 const struct inet_diag_req_v2 *req)
52b7c59b
PE
157{
158 return udp_dump_one(&udp_table, in_skb, nlh, req);
159}
160
62ad6fcd
SW
161static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
162 void *info)
163{
164 r->idiag_rqueue = sk_rmem_alloc_get(sk);
165 r->idiag_wqueue = sk_wmem_alloc_get(sk);
166}
167
52b7c59b
PE
168static const struct inet_diag_handler udp_diag_handler = {
169 .dump = udp_diag_dump,
170 .dump_one = udp_diag_dump_one,
62ad6fcd 171 .idiag_get_info = udp_diag_get_info,
52b7c59b
PE
172 .idiag_type = IPPROTO_UDP,
173};
174
175static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
34160ea3
ED
176 const struct inet_diag_req_v2 *r,
177 struct nlattr *bc)
52b7c59b
PE
178{
179 udp_dump(&udplite_table, skb, cb, r, bc);
180}
181
182static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
34160ea3 183 const struct inet_diag_req_v2 *req)
52b7c59b
PE
184{
185 return udp_dump_one(&udplite_table, in_skb, nlh, req);
186}
187
188static const struct inet_diag_handler udplite_diag_handler = {
189 .dump = udplite_diag_dump,
190 .dump_one = udplite_diag_dump_one,
62ad6fcd 191 .idiag_get_info = udp_diag_get_info,
52b7c59b
PE
192 .idiag_type = IPPROTO_UDPLITE,
193};
194
195static int __init udp_diag_init(void)
196{
197 int err;
198
199 err = inet_diag_register(&udp_diag_handler);
200 if (err)
201 goto out;
202 err = inet_diag_register(&udplite_diag_handler);
203 if (err)
204 goto out_lite;
205out:
206 return err;
207out_lite:
208 inet_diag_unregister(&udp_diag_handler);
209 goto out;
210}
211
212static void __exit udp_diag_exit(void)
213{
214 inet_diag_unregister(&udplite_diag_handler);
215 inet_diag_unregister(&udp_diag_handler);
216}
217
218module_init(udp_diag_init);
219module_exit(udp_diag_exit);
220MODULE_LICENSE("GPL");
aec8dc62
PE
221MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
222MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);