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