2 * udp_diag.c Module for monitoring UDP transport protocols sockets.
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>
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, struct inet_diag_req_v2 *req,
24 if (!inet_diag_bc_sk(bc, sk))
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);
31 static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
32 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
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);
43 #if IS_ENABLED(CONFIG_IPV6)
44 else if (req->sdiag_family == AF_INET6)
45 sk = __udp6_lib_lookup(&init_net,
46 (struct in6_addr *)req->id.idiag_src,
48 (struct in6_addr *)req->id.idiag_dst,
50 req->id.idiag_if, tbl);
59 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
64 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
65 sizeof(struct inet_diag_meminfo) +
70 err = inet_sk_diag_fill(sk, NULL, rep, req,
71 NETLINK_CB(in_skb).pid,
72 nlh->nlmsg_seq, 0, nlh);
74 WARN_ON(err == -EMSGSIZE);
78 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
89 static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
90 struct inet_diag_req_v2 *r, struct nlattr *bc)
92 int num, s_num, slot, s_slot;
95 num = s_num = cb->args[1];
97 for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
99 struct hlist_nulls_node *node;
100 struct udp_hslot *hslot = &table->hash[slot];
102 if (hlist_nulls_empty(&hslot->head))
105 spin_lock_bh(&hslot->lock);
106 sk_nulls_for_each(sk, node, &hslot->head) {
107 struct inet_sock *inet = inet_sk(sk);
111 if (!(r->idiag_states & (1 << sk->sk_state)))
113 if (r->sdiag_family != AF_UNSPEC &&
114 sk->sk_family != r->sdiag_family)
116 if (r->id.idiag_sport != inet->inet_sport &&
119 if (r->id.idiag_dport != inet->inet_dport &&
123 if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
124 spin_unlock_bh(&hslot->lock);
130 spin_unlock_bh(&hslot->lock);
137 static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
138 struct inet_diag_req_v2 *r, struct nlattr *bc)
140 udp_dump(&udp_table, skb, cb, r, bc);
143 static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
144 struct inet_diag_req_v2 *req)
146 return udp_dump_one(&udp_table, in_skb, nlh, req);
149 static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
152 r->idiag_rqueue = sk_rmem_alloc_get(sk);
153 r->idiag_wqueue = sk_wmem_alloc_get(sk);
156 static const struct inet_diag_handler udp_diag_handler = {
157 .dump = udp_diag_dump,
158 .dump_one = udp_diag_dump_one,
159 .idiag_get_info = udp_diag_get_info,
160 .idiag_type = IPPROTO_UDP,
163 static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
164 struct inet_diag_req_v2 *r, struct nlattr *bc)
166 udp_dump(&udplite_table, skb, cb, r, bc);
169 static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
170 struct inet_diag_req_v2 *req)
172 return udp_dump_one(&udplite_table, in_skb, nlh, req);
175 static const struct inet_diag_handler udplite_diag_handler = {
176 .dump = udplite_diag_dump,
177 .dump_one = udplite_diag_dump_one,
178 .idiag_get_info = udp_diag_get_info,
179 .idiag_type = IPPROTO_UDPLITE,
182 static int __init udp_diag_init(void)
186 err = inet_diag_register(&udp_diag_handler);
189 err = inet_diag_register(&udplite_diag_handler);
195 inet_diag_unregister(&udp_diag_handler);
199 static void __exit udp_diag_exit(void)
201 inet_diag_unregister(&udplite_diag_handler);
202 inet_diag_unregister(&udp_diag_handler);
205 module_init(udp_diag_init);
206 module_exit(udp_diag_exit);
207 MODULE_LICENSE("GPL");
208 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
209 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);