Thomas Gleixner | 47505b8 | 2019-05-23 11:14:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Xin Long | 6f68dc9 | 2018-02-13 19:33:21 +0800 | [diff] [blame] | 2 | /* SCTP kernel implementation |
| 3 | * (C) Copyright Red Hat Inc. 2017 |
| 4 | * |
| 5 | * This file is part of the SCTP kernel implementation |
| 6 | * |
| 7 | * These functions implement sctp diag support. |
| 8 | * |
Xin Long | 6f68dc9 | 2018-02-13 19:33:21 +0800 | [diff] [blame] | 9 | * Please send any bug reports or fixes you make to the |
| 10 | * email addresched(es): |
| 11 | * lksctp developers <linux-sctp@vger.kernel.org> |
| 12 | * |
| 13 | * Written or modified by: |
| 14 | * Xin Long <lucien.xin@gmail.com> |
| 15 | */ |
| 16 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/inet_diag.h> |
| 19 | #include <linux/sock_diag.h> |
| 20 | #include <net/sctp/sctp.h> |
| 21 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 22 | static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, |
| 23 | void *info); |
| 24 | |
| 25 | /* define some functions to make asoc/ep fill look clean */ |
| 26 | static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg *r, |
| 27 | struct sock *sk, |
| 28 | struct sctp_association *asoc) |
| 29 | { |
| 30 | union sctp_addr laddr, paddr; |
| 31 | struct dst_entry *dst; |
Phil Sutter | 12474e8 | 2016-08-04 12:11:56 +0200 | [diff] [blame] | 32 | struct timer_list *t3_rtx = &asoc->peer.primary_path->T3_rtx_timer; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 33 | |
| 34 | laddr = list_entry(asoc->base.bind_addr.address_list.next, |
| 35 | struct sctp_sockaddr_entry, list)->a; |
| 36 | paddr = asoc->peer.primary_path->ipaddr; |
| 37 | dst = asoc->peer.primary_path->dst; |
| 38 | |
| 39 | r->idiag_family = sk->sk_family; |
| 40 | r->id.idiag_sport = htons(asoc->base.bind_addr.port); |
| 41 | r->id.idiag_dport = htons(asoc->peer.port); |
| 42 | r->id.idiag_if = dst ? dst->dev->ifindex : 0; |
| 43 | sock_diag_save_cookie(sk, r->id.idiag_cookie); |
| 44 | |
| 45 | #if IS_ENABLED(CONFIG_IPV6) |
| 46 | if (sk->sk_family == AF_INET6) { |
| 47 | *(struct in6_addr *)r->id.idiag_src = laddr.v6.sin6_addr; |
| 48 | *(struct in6_addr *)r->id.idiag_dst = paddr.v6.sin6_addr; |
| 49 | } else |
| 50 | #endif |
| 51 | { |
| 52 | memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); |
| 53 | memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); |
| 54 | |
| 55 | r->id.idiag_src[0] = laddr.v4.sin_addr.s_addr; |
| 56 | r->id.idiag_dst[0] = paddr.v4.sin_addr.s_addr; |
| 57 | } |
| 58 | |
| 59 | r->idiag_state = asoc->state; |
Phil Sutter | 12474e8 | 2016-08-04 12:11:56 +0200 | [diff] [blame] | 60 | if (timer_pending(t3_rtx)) { |
| 61 | r->idiag_timer = SCTP_EVENT_TIMEOUT_T3_RTX; |
| 62 | r->idiag_retrans = asoc->rtx_data_chunks; |
| 63 | r->idiag_expires = jiffies_to_msecs(t3_rtx->expires - jiffies); |
| 64 | } else { |
| 65 | r->idiag_timer = 0; |
| 66 | r->idiag_retrans = 0; |
| 67 | r->idiag_expires = 0; |
| 68 | } |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, |
| 72 | struct list_head *address_list) |
| 73 | { |
| 74 | struct sctp_sockaddr_entry *laddr; |
| 75 | int addrlen = sizeof(struct sockaddr_storage); |
| 76 | int addrcnt = 0; |
| 77 | struct nlattr *attr; |
| 78 | void *info = NULL; |
| 79 | |
| 80 | list_for_each_entry_rcu(laddr, address_list, list) |
| 81 | addrcnt++; |
| 82 | |
| 83 | attr = nla_reserve(skb, INET_DIAG_LOCALS, addrlen * addrcnt); |
| 84 | if (!attr) |
| 85 | return -EMSGSIZE; |
| 86 | |
| 87 | info = nla_data(attr); |
| 88 | list_for_each_entry_rcu(laddr, address_list, list) { |
Stefano Brivio | ee6c88b | 2017-08-23 13:27:13 +0200 | [diff] [blame] | 89 | memcpy(info, &laddr->a, sizeof(laddr->a)); |
| 90 | memset(info + sizeof(laddr->a), 0, addrlen - sizeof(laddr->a)); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 91 | info += addrlen; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb, |
| 98 | struct sctp_association *asoc) |
| 99 | { |
| 100 | int addrlen = sizeof(struct sockaddr_storage); |
| 101 | struct sctp_transport *from; |
| 102 | struct nlattr *attr; |
| 103 | void *info = NULL; |
| 104 | |
| 105 | attr = nla_reserve(skb, INET_DIAG_PEERS, |
| 106 | addrlen * asoc->peer.transport_count); |
| 107 | if (!attr) |
| 108 | return -EMSGSIZE; |
| 109 | |
| 110 | info = nla_data(attr); |
| 111 | list_for_each_entry(from, &asoc->peer.transport_addr_list, |
| 112 | transports) { |
Stefano Brivio | ee6c88b | 2017-08-23 13:27:13 +0200 | [diff] [blame] | 113 | memcpy(info, &from->ipaddr, sizeof(from->ipaddr)); |
| 114 | memset(info + sizeof(from->ipaddr), 0, |
| 115 | addrlen - sizeof(from->ipaddr)); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 116 | info += addrlen; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* sctp asoc/ep fill*/ |
| 123 | static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc, |
| 124 | struct sk_buff *skb, |
| 125 | const struct inet_diag_req_v2 *req, |
| 126 | struct user_namespace *user_ns, |
| 127 | int portid, u32 seq, u16 nlmsg_flags, |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 128 | const struct nlmsghdr *unlh, |
| 129 | bool net_admin) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 130 | { |
| 131 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
| 132 | struct list_head *addr_list; |
| 133 | struct inet_diag_msg *r; |
| 134 | struct nlmsghdr *nlh; |
| 135 | int ext = req->idiag_ext; |
| 136 | struct sctp_infox infox; |
| 137 | void *info = NULL; |
| 138 | |
| 139 | nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), |
| 140 | nlmsg_flags); |
| 141 | if (!nlh) |
| 142 | return -EMSGSIZE; |
| 143 | |
| 144 | r = nlmsg_data(nlh); |
| 145 | BUG_ON(!sk_fullsock(sk)); |
| 146 | |
| 147 | if (asoc) { |
| 148 | inet_diag_msg_sctpasoc_fill(r, sk, asoc); |
| 149 | } else { |
| 150 | inet_diag_msg_common_fill(r, sk); |
| 151 | r->idiag_state = sk->sk_state; |
| 152 | r->idiag_timer = 0; |
| 153 | r->idiag_retrans = 0; |
| 154 | } |
| 155 | |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 156 | if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin)) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 157 | goto errout; |
| 158 | |
| 159 | if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) { |
| 160 | u32 mem[SK_MEMINFO_VARS]; |
| 161 | int amt; |
| 162 | |
| 163 | if (asoc && asoc->ep->sndbuf_policy) |
| 164 | amt = asoc->sndbuf_used; |
| 165 | else |
| 166 | amt = sk_wmem_alloc_get(sk); |
| 167 | mem[SK_MEMINFO_WMEM_ALLOC] = amt; |
Xin Long | f052f20 | 2016-04-24 23:21:22 +0800 | [diff] [blame] | 168 | if (asoc && asoc->ep->rcvbuf_policy) |
| 169 | amt = atomic_read(&asoc->rmem_alloc); |
| 170 | else |
| 171 | amt = sk_rmem_alloc_get(sk); |
| 172 | mem[SK_MEMINFO_RMEM_ALLOC] = amt; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 173 | mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; |
| 174 | mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; |
| 175 | mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; |
| 176 | mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; |
| 177 | mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); |
Eric Dumazet | 70c2655 | 2019-10-09 15:41:03 -0700 | [diff] [blame] | 178 | mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 179 | mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops); |
| 180 | |
| 181 | if (nla_put(skb, INET_DIAG_SKMEMINFO, sizeof(mem), &mem) < 0) |
| 182 | goto errout; |
| 183 | } |
| 184 | |
| 185 | if (ext & (1 << (INET_DIAG_INFO - 1))) { |
| 186 | struct nlattr *attr; |
| 187 | |
Nicolas Dichtel | 6ed46d1 | 2016-04-26 10:06:14 +0200 | [diff] [blame] | 188 | attr = nla_reserve_64bit(skb, INET_DIAG_INFO, |
| 189 | sizeof(struct sctp_info), |
| 190 | INET_DIAG_PAD); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 191 | if (!attr) |
| 192 | goto errout; |
| 193 | |
| 194 | info = nla_data(attr); |
| 195 | } |
| 196 | infox.sctpinfo = (struct sctp_info *)info; |
| 197 | infox.asoc = asoc; |
| 198 | sctp_diag_get_info(sk, r, &infox); |
| 199 | |
| 200 | addr_list = asoc ? &asoc->base.bind_addr.address_list |
| 201 | : &ep->base.bind_addr.address_list; |
| 202 | if (inet_diag_msg_sctpladdrs_fill(skb, addr_list)) |
| 203 | goto errout; |
| 204 | |
| 205 | if (asoc && (ext & (1 << (INET_DIAG_CONG - 1)))) |
| 206 | if (nla_put_string(skb, INET_DIAG_CONG, "reno") < 0) |
| 207 | goto errout; |
| 208 | |
| 209 | if (asoc && inet_diag_msg_sctpaddrs_fill(skb, asoc)) |
| 210 | goto errout; |
| 211 | |
| 212 | nlmsg_end(skb, nlh); |
| 213 | return 0; |
| 214 | |
| 215 | errout: |
| 216 | nlmsg_cancel(skb, nlh); |
| 217 | return -EMSGSIZE; |
| 218 | } |
| 219 | |
| 220 | /* callback and param */ |
| 221 | struct sctp_comm_param { |
| 222 | struct sk_buff *skb; |
| 223 | struct netlink_callback *cb; |
| 224 | const struct inet_diag_req_v2 *r; |
| 225 | const struct nlmsghdr *nlh; |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 226 | bool net_admin; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | static size_t inet_assoc_attr_size(struct sctp_association *asoc) |
| 230 | { |
| 231 | int addrlen = sizeof(struct sockaddr_storage); |
| 232 | int addrcnt = 0; |
| 233 | struct sctp_sockaddr_entry *laddr; |
| 234 | |
| 235 | list_for_each_entry_rcu(laddr, &asoc->base.bind_addr.address_list, |
| 236 | list) |
| 237 | addrcnt++; |
| 238 | |
| 239 | return nla_total_size(sizeof(struct sctp_info)) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 240 | + nla_total_size(addrlen * asoc->peer.transport_count) |
| 241 | + nla_total_size(addrlen * addrcnt) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 242 | + nla_total_size(sizeof(struct inet_diag_msg)) |
Dmitry Yakunin | 83f73c5b | 2020-03-05 15:33:12 +0300 | [diff] [blame] | 243 | + inet_diag_msg_attrs_size() |
| 244 | + nla_total_size(sizeof(struct inet_diag_meminfo)) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 245 | + 64; |
| 246 | } |
| 247 | |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 248 | static int sctp_sock_dump_one(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 249 | { |
| 250 | struct sctp_association *assoc = tsp->asoc; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 251 | struct sctp_comm_param *commp = p; |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 252 | struct sock *sk = ep->base.sk; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 253 | const struct inet_diag_req_v2 *req = commp->r; |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 254 | struct sk_buff *skb = commp->skb; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 255 | struct sk_buff *rep; |
| 256 | int err; |
| 257 | |
| 258 | err = sock_diag_check_cookie(sk, req->id.idiag_cookie); |
| 259 | if (err) |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 260 | return err; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 261 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 262 | rep = nlmsg_new(inet_assoc_attr_size(assoc), GFP_KERNEL); |
| 263 | if (!rep) |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 264 | return -ENOMEM; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 265 | |
| 266 | lock_sock(sk); |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 267 | if (ep != assoc->ep) { |
| 268 | err = -EAGAIN; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 269 | goto out; |
| 270 | } |
| 271 | |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 272 | err = inet_sctp_diag_fill(sk, assoc, rep, req, sk_user_ns(NETLINK_CB(skb).sk), |
| 273 | NETLINK_CB(skb).portid, commp->nlh->nlmsg_seq, 0, |
| 274 | commp->nlh, commp->net_admin); |
| 275 | if (err < 0) { |
| 276 | WARN_ON(err == -EMSGSIZE); |
| 277 | goto out; |
| 278 | } |
| 279 | release_sock(sk); |
| 280 | |
| 281 | return nlmsg_unicast(sock_net(skb->sk)->diag_nlsk, rep, NETLINK_CB(skb).portid); |
Yajun Deng | 01757f5 | 2021-07-13 10:48:24 +0800 | [diff] [blame] | 282 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 283 | out: |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 284 | release_sock(sk); |
| 285 | kfree_skb(rep); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 286 | return err; |
| 287 | } |
| 288 | |
Xin Long | 5ec7d18 | 2021-12-23 13:04:30 -0500 | [diff] [blame] | 289 | static int sctp_sock_dump(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 290 | { |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 291 | struct sctp_comm_param *commp = p; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 292 | struct sock *sk = ep->base.sk; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 293 | struct sk_buff *skb = commp->skb; |
| 294 | struct netlink_callback *cb = commp->cb; |
| 295 | const struct inet_diag_req_v2 *r = commp->r; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 296 | struct sctp_association *assoc; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 297 | int err = 0; |
| 298 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 299 | lock_sock(sk); |
Xin Long | 5ec7d18 | 2021-12-23 13:04:30 -0500 | [diff] [blame] | 300 | if (ep != tsp->asoc->ep) |
| 301 | goto release; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 302 | list_for_each_entry(assoc, &ep->asocs, asocs) { |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 303 | if (cb->args[4] < cb->args[1]) |
| 304 | goto next; |
| 305 | |
| 306 | if (r->id.idiag_sport != htons(assoc->base.bind_addr.port) && |
| 307 | r->id.idiag_sport) |
| 308 | goto next; |
| 309 | if (r->id.idiag_dport != htons(assoc->peer.port) && |
| 310 | r->id.idiag_dport) |
| 311 | goto next; |
| 312 | |
| 313 | if (!cb->args[3] && |
| 314 | inet_sctp_diag_fill(sk, NULL, skb, r, |
| 315 | sk_user_ns(NETLINK_CB(cb->skb).sk), |
| 316 | NETLINK_CB(cb->skb).portid, |
| 317 | cb->nlh->nlmsg_seq, |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 318 | NLM_F_MULTI, cb->nlh, |
| 319 | commp->net_admin) < 0) { |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 320 | err = 1; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 321 | goto release; |
| 322 | } |
| 323 | cb->args[3] = 1; |
| 324 | |
| 325 | if (inet_sctp_diag_fill(sk, assoc, skb, r, |
| 326 | sk_user_ns(NETLINK_CB(cb->skb).sk), |
| 327 | NETLINK_CB(cb->skb).portid, |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 328 | cb->nlh->nlmsg_seq, 0, cb->nlh, |
| 329 | commp->net_admin) < 0) { |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 330 | err = 1; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 331 | goto release; |
| 332 | } |
| 333 | next: |
| 334 | cb->args[4]++; |
| 335 | } |
| 336 | cb->args[1] = 0; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 337 | cb->args[3] = 0; |
| 338 | cb->args[4] = 0; |
| 339 | release: |
| 340 | release_sock(sk); |
| 341 | return err; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Xin Long | 5ec7d18 | 2021-12-23 13:04:30 -0500 | [diff] [blame] | 344 | static int sctp_sock_filter(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p) |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 345 | { |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 346 | struct sctp_comm_param *commp = p; |
| 347 | struct sock *sk = ep->base.sk; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 348 | const struct inet_diag_req_v2 *r = commp->r; |
| 349 | struct sctp_association *assoc = |
| 350 | list_entry(ep->asocs.next, struct sctp_association, asocs); |
| 351 | |
| 352 | /* find the ep only once through the transports by this condition */ |
| 353 | if (tsp->asoc != assoc) |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 354 | return 0; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 355 | |
| 356 | if (r->sdiag_family != AF_UNSPEC && sk->sk_family != r->sdiag_family) |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 357 | return 0; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 358 | |
| 359 | return 1; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static int sctp_ep_dump(struct sctp_endpoint *ep, void *p) |
| 363 | { |
| 364 | struct sctp_comm_param *commp = p; |
| 365 | struct sock *sk = ep->base.sk; |
| 366 | struct sk_buff *skb = commp->skb; |
| 367 | struct netlink_callback *cb = commp->cb; |
| 368 | const struct inet_diag_req_v2 *r = commp->r; |
| 369 | struct net *net = sock_net(skb->sk); |
| 370 | struct inet_sock *inet = inet_sk(sk); |
| 371 | int err = 0; |
| 372 | |
| 373 | if (!net_eq(sock_net(sk), net)) |
| 374 | goto out; |
| 375 | |
| 376 | if (cb->args[4] < cb->args[1]) |
| 377 | goto next; |
| 378 | |
Phil Sutter | 1ba8d77 | 2016-08-04 12:11:57 +0200 | [diff] [blame] | 379 | if (!(r->idiag_states & TCPF_LISTEN) && !list_empty(&ep->asocs)) |
Xin Long | bed187b | 2016-05-26 03:09:23 +0800 | [diff] [blame] | 380 | goto next; |
| 381 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 382 | if (r->sdiag_family != AF_UNSPEC && |
| 383 | sk->sk_family != r->sdiag_family) |
| 384 | goto next; |
| 385 | |
| 386 | if (r->id.idiag_sport != inet->inet_sport && |
| 387 | r->id.idiag_sport) |
| 388 | goto next; |
| 389 | |
| 390 | if (r->id.idiag_dport != inet->inet_dport && |
| 391 | r->id.idiag_dport) |
| 392 | goto next; |
| 393 | |
| 394 | if (inet_sctp_diag_fill(sk, NULL, skb, r, |
| 395 | sk_user_ns(NETLINK_CB(cb->skb).sk), |
| 396 | NETLINK_CB(cb->skb).portid, |
| 397 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 398 | cb->nlh, commp->net_admin) < 0) { |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 399 | err = 2; |
| 400 | goto out; |
| 401 | } |
| 402 | next: |
| 403 | cb->args[4]++; |
| 404 | out: |
| 405 | return err; |
| 406 | } |
| 407 | |
| 408 | /* define the functions for sctp_diag_handler*/ |
| 409 | static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, |
| 410 | void *info) |
| 411 | { |
| 412 | struct sctp_infox *infox = (struct sctp_infox *)info; |
| 413 | |
| 414 | if (infox->asoc) { |
| 415 | r->idiag_rqueue = atomic_read(&infox->asoc->rmem_alloc); |
| 416 | r->idiag_wqueue = infox->asoc->sndbuf_used; |
| 417 | } else { |
Eric Dumazet | 288efe8 | 2019-11-05 14:11:53 -0800 | [diff] [blame] | 418 | r->idiag_rqueue = READ_ONCE(sk->sk_ack_backlog); |
Eric Dumazet | 099ecf5 | 2019-11-05 14:11:54 -0800 | [diff] [blame] | 419 | r->idiag_wqueue = READ_ONCE(sk->sk_max_ack_backlog); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 420 | } |
| 421 | if (infox->sctpinfo) |
| 422 | sctp_get_sctp_info(sk, infox->asoc, infox->sctpinfo); |
| 423 | } |
| 424 | |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 425 | static int sctp_diag_dump_one(struct netlink_callback *cb, |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 426 | const struct inet_diag_req_v2 *req) |
| 427 | { |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 428 | struct sk_buff *skb = cb->skb; |
| 429 | struct net *net = sock_net(skb->sk); |
Martin KaFai Lau | 5682d39 | 2020-02-25 15:04:09 -0800 | [diff] [blame] | 430 | const struct nlmsghdr *nlh = cb->nlh; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 431 | union sctp_addr laddr, paddr; |
| 432 | struct sctp_comm_param commp = { |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 433 | .skb = skb, |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 434 | .r = req, |
| 435 | .nlh = nlh, |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 436 | .net_admin = netlink_net_capable(skb, CAP_NET_ADMIN), |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | if (req->sdiag_family == AF_INET) { |
| 440 | laddr.v4.sin_port = req->id.idiag_sport; |
| 441 | laddr.v4.sin_addr.s_addr = req->id.idiag_src[0]; |
| 442 | laddr.v4.sin_family = AF_INET; |
| 443 | |
| 444 | paddr.v4.sin_port = req->id.idiag_dport; |
| 445 | paddr.v4.sin_addr.s_addr = req->id.idiag_dst[0]; |
| 446 | paddr.v4.sin_family = AF_INET; |
| 447 | } else { |
| 448 | laddr.v6.sin6_port = req->id.idiag_sport; |
Lance Richardson | 232cb53 | 2016-08-23 11:40:52 -0400 | [diff] [blame] | 449 | memcpy(&laddr.v6.sin6_addr, req->id.idiag_src, |
| 450 | sizeof(laddr.v6.sin6_addr)); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 451 | laddr.v6.sin6_family = AF_INET6; |
| 452 | |
| 453 | paddr.v6.sin6_port = req->id.idiag_dport; |
Lance Richardson | 232cb53 | 2016-08-23 11:40:52 -0400 | [diff] [blame] | 454 | memcpy(&paddr.v6.sin6_addr, req->id.idiag_dst, |
| 455 | sizeof(paddr.v6.sin6_addr)); |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 456 | paddr.v6.sin6_family = AF_INET6; |
| 457 | } |
| 458 | |
Xin Long | f9d31c4 | 2021-12-31 18:37:37 -0500 | [diff] [blame] | 459 | return sctp_transport_lookup_process(sctp_sock_dump_one, |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 460 | net, &laddr, &paddr, &commp); |
| 461 | } |
| 462 | |
| 463 | static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
Martin KaFai Lau | 0df6d32 | 2020-02-25 15:04:15 -0800 | [diff] [blame] | 464 | const struct inet_diag_req_v2 *r) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 465 | { |
| 466 | u32 idiag_states = r->idiag_states; |
| 467 | struct net *net = sock_net(skb->sk); |
| 468 | struct sctp_comm_param commp = { |
| 469 | .skb = skb, |
| 470 | .cb = cb, |
| 471 | .r = r, |
Lorenzo Colitti | d545cac | 2016-09-08 00:42:25 +0900 | [diff] [blame] | 472 | .net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN), |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 473 | }; |
Dan Carpenter | c2cc187 | 2017-09-25 13:19:26 +0300 | [diff] [blame] | 474 | int pos = cb->args[2]; |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 475 | |
| 476 | /* eps hashtable dumps |
| 477 | * args: |
| 478 | * 0 : if it will traversal listen sock |
| 479 | * 1 : to record the sock pos of this time's traversal |
| 480 | * 4 : to work as a temporary variable to traversal list |
| 481 | */ |
| 482 | if (cb->args[0] == 0) { |
| 483 | if (!(idiag_states & TCPF_LISTEN)) |
| 484 | goto skip; |
| 485 | if (sctp_for_each_endpoint(sctp_ep_dump, &commp)) |
| 486 | goto done; |
| 487 | skip: |
| 488 | cb->args[0] = 1; |
| 489 | cb->args[1] = 0; |
| 490 | cb->args[4] = 0; |
| 491 | } |
| 492 | |
| 493 | /* asocs by transport hashtable dump |
| 494 | * args: |
| 495 | * 1 : to record the assoc pos of this time's traversal |
| 496 | * 2 : to record the transport pos of this time's traversal |
| 497 | * 3 : to mark if we have dumped the ep info of the current asoc |
| 498 | * 4 : to work as a temporary variable to traversal list |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 499 | * 5 : to save the sk we get from travelsing the tsp list. |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 500 | */ |
Phil Sutter | 1ba8d77 | 2016-08-04 12:11:57 +0200 | [diff] [blame] | 501 | if (!(idiag_states & ~(TCPF_LISTEN | TCPF_CLOSE))) |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 502 | goto done; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 503 | |
Xin Long | 5ec7d18 | 2021-12-23 13:04:30 -0500 | [diff] [blame] | 504 | sctp_transport_traverse_process(sctp_sock_filter, sctp_sock_dump, |
| 505 | net, &pos, &commp); |
Dan Carpenter | c2cc187 | 2017-09-25 13:19:26 +0300 | [diff] [blame] | 506 | cb->args[2] = pos; |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 507 | |
Xin Long | 8f840e4 | 2016-04-14 15:35:33 +0800 | [diff] [blame] | 508 | done: |
| 509 | cb->args[1] = cb->args[4]; |
| 510 | cb->args[4] = 0; |
| 511 | } |
| 512 | |
| 513 | static const struct inet_diag_handler sctp_diag_handler = { |
| 514 | .dump = sctp_diag_dump, |
| 515 | .dump_one = sctp_diag_dump_one, |
| 516 | .idiag_get_info = sctp_diag_get_info, |
| 517 | .idiag_type = IPPROTO_SCTP, |
| 518 | .idiag_info_size = sizeof(struct sctp_info), |
| 519 | }; |
| 520 | |
| 521 | static int __init sctp_diag_init(void) |
| 522 | { |
| 523 | return inet_diag_register(&sctp_diag_handler); |
| 524 | } |
| 525 | |
| 526 | static void __exit sctp_diag_exit(void) |
| 527 | { |
| 528 | inet_diag_unregister(&sctp_diag_handler); |
| 529 | } |
| 530 | |
| 531 | module_init(sctp_diag_init); |
| 532 | module_exit(sctp_diag_exit); |
| 533 | MODULE_LICENSE("GPL"); |
| 534 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-132); |