drm/i915: Reduce locking inside swfinish ioctl
[linux/fpc-iii.git] / net / ipv4 / udp_diag.c
blob3d5ccf4b141202ec117f2466171f8b7404df06bf
1 /*
2 * udp_diag.c Module for monitoring UDP transport protocols sockets.
4 * Authors: Pavel Emelyanov, <xemul@parallels.com>
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.
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>
18 #include <linux/sock_diag.h>
20 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
21 struct netlink_callback *cb,
22 const struct inet_diag_req_v2 *req,
23 struct nlattr *bc)
25 if (!inet_diag_bc_sk(bc, sk))
26 return 0;
28 return inet_sk_diag_fill(sk, NULL, skb, req,
29 sk_user_ns(NETLINK_CB(cb->skb).sk),
30 NETLINK_CB(cb->skb).portid,
31 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
34 static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
35 const struct nlmsghdr *nlh,
36 const struct inet_diag_req_v2 *req)
38 int err = -EINVAL;
39 struct sock *sk = NULL;
40 struct sk_buff *rep;
41 struct net *net = sock_net(in_skb->sk);
43 rcu_read_lock();
44 if (req->sdiag_family == AF_INET)
45 sk = __udp4_lib_lookup(net,
46 req->id.idiag_src[0], req->id.idiag_sport,
47 req->id.idiag_dst[0], req->id.idiag_dport,
48 req->id.idiag_if, tbl, NULL);
49 #if IS_ENABLED(CONFIG_IPV6)
50 else if (req->sdiag_family == AF_INET6)
51 sk = __udp6_lib_lookup(net,
52 (struct in6_addr *)req->id.idiag_src,
53 req->id.idiag_sport,
54 (struct in6_addr *)req->id.idiag_dst,
55 req->id.idiag_dport,
56 req->id.idiag_if, tbl, NULL);
57 #endif
58 if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
59 sk = NULL;
60 rcu_read_unlock();
61 err = -ENOENT;
62 if (!sk)
63 goto out_nosk;
65 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
66 if (err)
67 goto out;
69 err = -ENOMEM;
70 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
71 sizeof(struct inet_diag_meminfo) + 64,
72 GFP_KERNEL);
73 if (!rep)
74 goto out;
76 err = inet_sk_diag_fill(sk, NULL, rep, req,
77 sk_user_ns(NETLINK_CB(in_skb).sk),
78 NETLINK_CB(in_skb).portid,
79 nlh->nlmsg_seq, 0, nlh);
80 if (err < 0) {
81 WARN_ON(err == -EMSGSIZE);
82 kfree_skb(rep);
83 goto out;
85 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
86 MSG_DONTWAIT);
87 if (err > 0)
88 err = 0;
89 out:
90 if (sk)
91 sock_put(sk);
92 out_nosk:
93 return err;
96 static void udp_dump(struct udp_table *table, struct sk_buff *skb,
97 struct netlink_callback *cb,
98 const struct inet_diag_req_v2 *r, struct nlattr *bc)
100 struct net *net = sock_net(skb->sk);
101 int num, s_num, slot, s_slot;
103 s_slot = cb->args[0];
104 num = s_num = cb->args[1];
106 for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
107 struct udp_hslot *hslot = &table->hash[slot];
108 struct sock *sk;
110 num = 0;
112 if (hlist_empty(&hslot->head))
113 continue;
115 spin_lock_bh(&hslot->lock);
116 sk_for_each(sk, &hslot->head) {
117 struct inet_sock *inet = inet_sk(sk);
119 if (!net_eq(sock_net(sk), net))
120 continue;
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;
135 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
136 spin_unlock_bh(&hslot->lock);
137 goto done;
139 next:
140 num++;
142 spin_unlock_bh(&hslot->lock);
144 done:
145 cb->args[0] = slot;
146 cb->args[1] = num;
149 static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
150 const struct inet_diag_req_v2 *r, struct nlattr *bc)
152 udp_dump(&udp_table, skb, cb, r, bc);
155 static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
156 const struct inet_diag_req_v2 *req)
158 return udp_dump_one(&udp_table, in_skb, nlh, req);
161 static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
162 void *info)
164 r->idiag_rqueue = sk_rmem_alloc_get(sk);
165 r->idiag_wqueue = sk_wmem_alloc_get(sk);
168 static const struct inet_diag_handler udp_diag_handler = {
169 .dump = udp_diag_dump,
170 .dump_one = udp_diag_dump_one,
171 .idiag_get_info = udp_diag_get_info,
172 .idiag_type = IPPROTO_UDP,
173 .idiag_info_size = 0,
176 static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
177 const struct inet_diag_req_v2 *r,
178 struct nlattr *bc)
180 udp_dump(&udplite_table, skb, cb, r, bc);
183 static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
184 const struct inet_diag_req_v2 *req)
186 return udp_dump_one(&udplite_table, in_skb, nlh, req);
189 static const struct inet_diag_handler udplite_diag_handler = {
190 .dump = udplite_diag_dump,
191 .dump_one = udplite_diag_dump_one,
192 .idiag_get_info = udp_diag_get_info,
193 .idiag_type = IPPROTO_UDPLITE,
194 .idiag_info_size = 0,
197 static int __init udp_diag_init(void)
199 int err;
201 err = inet_diag_register(&udp_diag_handler);
202 if (err)
203 goto out;
204 err = inet_diag_register(&udplite_diag_handler);
205 if (err)
206 goto out_lite;
207 out:
208 return err;
209 out_lite:
210 inet_diag_unregister(&udp_diag_handler);
211 goto out;
214 static void __exit udp_diag_exit(void)
216 inet_diag_unregister(&udplite_diag_handler);
217 inet_diag_unregister(&udp_diag_handler);
220 module_init(udp_diag_init);
221 module_exit(udp_diag_exit);
222 MODULE_LICENSE("GPL");
223 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
224 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);