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