Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | |
| 4 | #include <linux/inet_diag.h> |
| 5 | #include <linux/sock_diag.h> |
| 6 | |
Arnd Bergmann | f8da977 | 2016-10-25 17:53:22 +0200 | [diff] [blame] | 7 | #include <net/inet_sock.h> |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 8 | #include <net/raw.h> |
| 9 | #include <net/rawv6.h> |
| 10 | |
| 11 | #ifdef pr_fmt |
| 12 | # undef pr_fmt |
| 13 | #endif |
| 14 | |
| 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
| 17 | static struct raw_hashinfo * |
| 18 | raw_get_hashinfo(const struct inet_diag_req_v2 *r) |
| 19 | { |
| 20 | if (r->sdiag_family == AF_INET) { |
| 21 | return &raw_v4_hashinfo; |
| 22 | #if IS_ENABLED(CONFIG_IPV6) |
| 23 | } else if (r->sdiag_family == AF_INET6) { |
| 24 | return &raw_v6_hashinfo; |
| 25 | #endif |
| 26 | } else { |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 27 | return ERR_PTR(-EINVAL); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Due to requirement of not breaking user API we can't simply |
| 33 | * rename @pad field in inet_diag_req_v2 structure, instead |
| 34 | * use helper to figure it out. |
| 35 | */ |
| 36 | |
| 37 | static struct sock *raw_lookup(struct net *net, struct sock *from, |
| 38 | const struct inet_diag_req_v2 *req) |
| 39 | { |
| 40 | struct inet_diag_req_raw *r = (void *)req; |
| 41 | struct sock *sk = NULL; |
| 42 | |
| 43 | if (r->sdiag_family == AF_INET) |
| 44 | sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol, |
| 45 | r->id.idiag_dst[0], |
| 46 | r->id.idiag_src[0], |
David Ahern | 6735993 | 2017-08-07 08:44:18 -0700 | [diff] [blame] | 47 | r->id.idiag_if, 0); |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 48 | #if IS_ENABLED(CONFIG_IPV6) |
| 49 | else |
| 50 | sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol, |
| 51 | (const struct in6_addr *)r->id.idiag_src, |
| 52 | (const struct in6_addr *)r->id.idiag_dst, |
David Ahern | 5108ab4 | 2017-08-07 08:44:22 -0700 | [diff] [blame] | 53 | r->id.idiag_if, 0); |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 54 | #endif |
| 55 | return sk; |
| 56 | } |
| 57 | |
| 58 | static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r) |
| 59 | { |
| 60 | struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); |
| 61 | struct sock *sk = NULL, *s; |
| 62 | int slot; |
| 63 | |
| 64 | if (IS_ERR(hashinfo)) |
| 65 | return ERR_CAST(hashinfo); |
| 66 | |
| 67 | read_lock(&hashinfo->lock); |
| 68 | for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) { |
| 69 | sk_for_each(s, &hashinfo->ht[slot]) { |
| 70 | sk = raw_lookup(net, s, r); |
| 71 | if (sk) { |
| 72 | /* |
| 73 | * Grab it and keep until we fill |
| 74 | * diag meaage to be reported, so |
| 75 | * caller should call sock_put then. |
| 76 | * We can do that because we're keeping |
| 77 | * hashinfo->lock here. |
| 78 | */ |
| 79 | sock_hold(sk); |
Cyrill Gorcunov | 9999370 | 2016-11-02 15:36:32 +0300 | [diff] [blame] | 80 | goto out_unlock; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
Cyrill Gorcunov | 9999370 | 2016-11-02 15:36:32 +0300 | [diff] [blame] | 84 | out_unlock: |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 85 | read_unlock(&hashinfo->lock); |
| 86 | |
| 87 | return sk ? sk : ERR_PTR(-ENOENT); |
| 88 | } |
| 89 | |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 90 | static int raw_diag_dump_one(struct netlink_callback *cb, |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 91 | const struct inet_diag_req_v2 *r) |
| 92 | { |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 93 | struct sk_buff *in_skb = cb->skb; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 94 | struct sk_buff *rep; |
| 95 | struct sock *sk; |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 96 | struct net *net; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 97 | int err; |
| 98 | |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 99 | net = sock_net(in_skb->sk); |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 100 | sk = raw_sock_get(net, r); |
| 101 | if (IS_ERR(sk)) |
| 102 | return PTR_ERR(sk); |
| 103 | |
Dmitry Yakunin | 83f73c5b | 2020-03-05 15:33:12 +0300 | [diff] [blame] | 104 | rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) + |
| 105 | inet_diag_msg_attrs_size() + |
| 106 | nla_total_size(sizeof(struct inet_diag_meminfo)) + 64, |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 107 | GFP_KERNEL); |
| 108 | if (!rep) { |
| 109 | sock_put(sk); |
| 110 | return -ENOMEM; |
| 111 | } |
| 112 | |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 113 | err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0, |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 114 | netlink_net_capable(in_skb, CAP_NET_ADMIN)); |
| 115 | sock_put(sk); |
| 116 | |
| 117 | if (err < 0) { |
| 118 | kfree_skb(rep); |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | err = netlink_unicast(net->diag_nlsk, rep, |
| 123 | NETLINK_CB(in_skb).portid, |
| 124 | MSG_DONTWAIT); |
| 125 | if (err > 0) |
| 126 | err = 0; |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, |
| 131 | struct netlink_callback *cb, |
| 132 | const struct inet_diag_req_v2 *r, |
| 133 | struct nlattr *bc, bool net_admin) |
| 134 | { |
| 135 | if (!inet_diag_bc_sk(bc, sk)) |
| 136 | return 0; |
| 137 | |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 138 | return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin); |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
Martin KaFai Lau | 0df6d32 | 2020-02-25 15:04:15 -0800 | [diff] [blame] | 142 | const struct inet_diag_req_v2 *r) |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 143 | { |
| 144 | bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); |
| 145 | struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); |
| 146 | struct net *net = sock_net(skb->sk); |
Martin KaFai Lau | 0df6d32 | 2020-02-25 15:04:15 -0800 | [diff] [blame] | 147 | struct inet_diag_dump_data *cb_data; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 148 | int num, s_num, slot, s_slot; |
| 149 | struct sock *sk = NULL; |
Martin KaFai Lau | 0df6d32 | 2020-02-25 15:04:15 -0800 | [diff] [blame] | 150 | struct nlattr *bc; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 151 | |
| 152 | if (IS_ERR(hashinfo)) |
| 153 | return; |
| 154 | |
Martin KaFai Lau | 0df6d32 | 2020-02-25 15:04:15 -0800 | [diff] [blame] | 155 | cb_data = cb->data; |
| 156 | bc = cb_data->inet_diag_nla_bc; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 157 | s_slot = cb->args[0]; |
| 158 | num = s_num = cb->args[1]; |
| 159 | |
| 160 | read_lock(&hashinfo->lock); |
| 161 | for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) { |
| 162 | num = 0; |
| 163 | |
| 164 | sk_for_each(sk, &hashinfo->ht[slot]) { |
| 165 | struct inet_sock *inet = inet_sk(sk); |
| 166 | |
| 167 | if (!net_eq(sock_net(sk), net)) |
| 168 | continue; |
| 169 | if (num < s_num) |
| 170 | goto next; |
| 171 | if (sk->sk_family != r->sdiag_family) |
| 172 | goto next; |
| 173 | if (r->id.idiag_sport != inet->inet_sport && |
| 174 | r->id.idiag_sport) |
| 175 | goto next; |
| 176 | if (r->id.idiag_dport != inet->inet_dport && |
| 177 | r->id.idiag_dport) |
| 178 | goto next; |
| 179 | if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) |
| 180 | goto out_unlock; |
| 181 | next: |
| 182 | num++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | out_unlock: |
| 187 | read_unlock(&hashinfo->lock); |
| 188 | |
| 189 | cb->args[0] = slot; |
| 190 | cb->args[1] = num; |
| 191 | } |
| 192 | |
| 193 | static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r, |
| 194 | void *info) |
| 195 | { |
| 196 | r->idiag_rqueue = sk_rmem_alloc_get(sk); |
| 197 | r->idiag_wqueue = sk_wmem_alloc_get(sk); |
| 198 | } |
| 199 | |
| 200 | #ifdef CONFIG_INET_DIAG_DESTROY |
| 201 | static int raw_diag_destroy(struct sk_buff *in_skb, |
| 202 | const struct inet_diag_req_v2 *r) |
| 203 | { |
| 204 | struct net *net = sock_net(in_skb->sk); |
| 205 | struct sock *sk; |
Cyrill Gorcunov | cd05a0e | 2016-11-02 15:36:31 +0300 | [diff] [blame] | 206 | int err; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 207 | |
| 208 | sk = raw_sock_get(net, r); |
| 209 | if (IS_ERR(sk)) |
| 210 | return PTR_ERR(sk); |
Cyrill Gorcunov | cd05a0e | 2016-11-02 15:36:31 +0300 | [diff] [blame] | 211 | err = sock_diag_destroy(sk, ECONNABORTED); |
| 212 | sock_put(sk); |
| 213 | return err; |
Cyrill Gorcunov | 432490f | 2016-10-21 13:03:44 +0300 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | |
| 217 | static const struct inet_diag_handler raw_diag_handler = { |
| 218 | .dump = raw_diag_dump, |
| 219 | .dump_one = raw_diag_dump_one, |
| 220 | .idiag_get_info = raw_diag_get_info, |
| 221 | .idiag_type = IPPROTO_RAW, |
| 222 | .idiag_info_size = 0, |
| 223 | #ifdef CONFIG_INET_DIAG_DESTROY |
| 224 | .destroy = raw_diag_destroy, |
| 225 | #endif |
| 226 | }; |
| 227 | |
| 228 | static void __always_unused __check_inet_diag_req_raw(void) |
| 229 | { |
| 230 | /* |
| 231 | * Make sure the two structures are identical, |
| 232 | * except the @pad field. |
| 233 | */ |
| 234 | #define __offset_mismatch(m1, m2) \ |
| 235 | (offsetof(struct inet_diag_req_v2, m1) != \ |
| 236 | offsetof(struct inet_diag_req_raw, m2)) |
| 237 | |
| 238 | BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) != |
| 239 | sizeof(struct inet_diag_req_raw)); |
| 240 | BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family)); |
| 241 | BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol)); |
| 242 | BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext)); |
| 243 | BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol)); |
| 244 | BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states)); |
| 245 | BUILD_BUG_ON(__offset_mismatch(id, id)); |
| 246 | #undef __offset_mismatch |
| 247 | } |
| 248 | |
| 249 | static int __init raw_diag_init(void) |
| 250 | { |
| 251 | return inet_diag_register(&raw_diag_handler); |
| 252 | } |
| 253 | |
| 254 | static void __exit raw_diag_exit(void) |
| 255 | { |
| 256 | inet_diag_unregister(&raw_diag_handler); |
| 257 | } |
| 258 | |
| 259 | module_init(raw_diag_init); |
| 260 | module_exit(raw_diag_exit); |
| 261 | MODULE_LICENSE("GPL"); |
| 262 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */); |
| 263 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */); |