1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* SCTP kernel implementation 1 /* SCTP kernel implementation 3 * (C) Copyright Red Hat Inc. 2017 2 * (C) Copyright Red Hat Inc. 2017 4 * 3 * 5 * This file is part of the SCTP kernel implem 4 * This file is part of the SCTP kernel implementation 6 * 5 * 7 * These functions implement sctp diag support 6 * These functions implement sctp diag support. 8 * 7 * >> 8 * This SCTP implementation is free software; >> 9 * you can redistribute it and/or modify it under the terms of >> 10 * the GNU General Public License as published by >> 11 * the Free Software Foundation; either version 2, or (at your option) >> 12 * any later version. >> 13 * >> 14 * This SCTP implementation is distributed in the hope that it >> 15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied >> 16 * ************************ >> 17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. >> 18 * See the GNU General Public License for more details. >> 19 * >> 20 * You should have received a copy of the GNU General Public License >> 21 * along with GNU CC; see the file COPYING. If not, see >> 22 * <http://www.gnu.org/licenses/>. >> 23 * 9 * Please send any bug reports or fixes you ma 24 * Please send any bug reports or fixes you make to the 10 * email addresched(es): 25 * email addresched(es): 11 * lksctp developers <linux-sctp@vger.kerne 26 * lksctp developers <linux-sctp@vger.kernel.org> 12 * 27 * 13 * Written or modified by: 28 * Written or modified by: 14 * Xin Long <lucien.xin@gmail.com> 29 * Xin Long <lucien.xin@gmail.com> 15 */ 30 */ 16 31 17 #include <linux/module.h> 32 #include <linux/module.h> 18 #include <linux/inet_diag.h> 33 #include <linux/inet_diag.h> 19 #include <linux/sock_diag.h> 34 #include <linux/sock_diag.h> 20 #include <net/sctp/sctp.h> 35 #include <net/sctp/sctp.h> 21 36 22 static void sctp_diag_get_info(struct sock *sk 37 static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, 23 void *info); 38 void *info); 24 39 25 /* define some functions to make asoc/ep fill 40 /* define some functions to make asoc/ep fill look clean */ 26 static void inet_diag_msg_sctpasoc_fill(struct 41 static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg *r, 27 struct 42 struct sock *sk, 28 struct 43 struct sctp_association *asoc) 29 { 44 { 30 union sctp_addr laddr, paddr; 45 union sctp_addr laddr, paddr; 31 struct dst_entry *dst; 46 struct dst_entry *dst; 32 struct timer_list *t3_rtx = &asoc->pee 47 struct timer_list *t3_rtx = &asoc->peer.primary_path->T3_rtx_timer; 33 48 34 laddr = list_entry(asoc->base.bind_add 49 laddr = list_entry(asoc->base.bind_addr.address_list.next, 35 struct sctp_sockadd 50 struct sctp_sockaddr_entry, list)->a; 36 paddr = asoc->peer.primary_path->ipadd 51 paddr = asoc->peer.primary_path->ipaddr; 37 dst = asoc->peer.primary_path->dst; 52 dst = asoc->peer.primary_path->dst; 38 53 39 r->idiag_family = sk->sk_family; 54 r->idiag_family = sk->sk_family; 40 r->id.idiag_sport = htons(asoc->base.b 55 r->id.idiag_sport = htons(asoc->base.bind_addr.port); 41 r->id.idiag_dport = htons(asoc->peer.p 56 r->id.idiag_dport = htons(asoc->peer.port); 42 r->id.idiag_if = dst ? dst->dev->ifind 57 r->id.idiag_if = dst ? dst->dev->ifindex : 0; 43 sock_diag_save_cookie(sk, r->id.idiag_ 58 sock_diag_save_cookie(sk, r->id.idiag_cookie); 44 59 45 #if IS_ENABLED(CONFIG_IPV6) 60 #if IS_ENABLED(CONFIG_IPV6) 46 if (sk->sk_family == AF_INET6) { 61 if (sk->sk_family == AF_INET6) { 47 *(struct in6_addr *)r->id.idia 62 *(struct in6_addr *)r->id.idiag_src = laddr.v6.sin6_addr; 48 *(struct in6_addr *)r->id.idia 63 *(struct in6_addr *)r->id.idiag_dst = paddr.v6.sin6_addr; 49 } else 64 } else 50 #endif 65 #endif 51 { 66 { 52 memset(&r->id.idiag_src, 0, si 67 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 53 memset(&r->id.idiag_dst, 0, si 68 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 54 69 55 r->id.idiag_src[0] = laddr.v4. 70 r->id.idiag_src[0] = laddr.v4.sin_addr.s_addr; 56 r->id.idiag_dst[0] = paddr.v4. 71 r->id.idiag_dst[0] = paddr.v4.sin_addr.s_addr; 57 } 72 } 58 73 59 r->idiag_state = asoc->state; 74 r->idiag_state = asoc->state; 60 if (timer_pending(t3_rtx)) { 75 if (timer_pending(t3_rtx)) { 61 r->idiag_timer = SCTP_EVENT_TI 76 r->idiag_timer = SCTP_EVENT_TIMEOUT_T3_RTX; 62 r->idiag_retrans = asoc->rtx_d 77 r->idiag_retrans = asoc->rtx_data_chunks; 63 r->idiag_expires = jiffies_to_ 78 r->idiag_expires = jiffies_to_msecs(t3_rtx->expires - jiffies); 64 } 79 } 65 } 80 } 66 81 67 static int inet_diag_msg_sctpladdrs_fill(struc 82 static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb, 68 struc 83 struct list_head *address_list) 69 { 84 { 70 struct sctp_sockaddr_entry *laddr; 85 struct sctp_sockaddr_entry *laddr; 71 int addrlen = sizeof(struct sockaddr_s 86 int addrlen = sizeof(struct sockaddr_storage); 72 int addrcnt = 0; 87 int addrcnt = 0; 73 struct nlattr *attr; 88 struct nlattr *attr; 74 void *info = NULL; 89 void *info = NULL; 75 90 76 list_for_each_entry_rcu(laddr, address 91 list_for_each_entry_rcu(laddr, address_list, list) 77 addrcnt++; 92 addrcnt++; 78 93 79 attr = nla_reserve(skb, INET_DIAG_LOCA 94 attr = nla_reserve(skb, INET_DIAG_LOCALS, addrlen * addrcnt); 80 if (!attr) 95 if (!attr) 81 return -EMSGSIZE; 96 return -EMSGSIZE; 82 97 83 info = nla_data(attr); 98 info = nla_data(attr); 84 list_for_each_entry_rcu(laddr, address 99 list_for_each_entry_rcu(laddr, address_list, list) { 85 memcpy(info, &laddr->a, sizeof 100 memcpy(info, &laddr->a, sizeof(laddr->a)); 86 memset(info + sizeof(laddr->a) 101 memset(info + sizeof(laddr->a), 0, addrlen - sizeof(laddr->a)); 87 info += addrlen; 102 info += addrlen; 88 } 103 } 89 104 90 return 0; 105 return 0; 91 } 106 } 92 107 93 static int inet_diag_msg_sctpaddrs_fill(struct 108 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb, 94 struct 109 struct sctp_association *asoc) 95 { 110 { 96 int addrlen = sizeof(struct sockaddr_s 111 int addrlen = sizeof(struct sockaddr_storage); 97 struct sctp_transport *from; 112 struct sctp_transport *from; 98 struct nlattr *attr; 113 struct nlattr *attr; 99 void *info = NULL; 114 void *info = NULL; 100 115 101 attr = nla_reserve(skb, INET_DIAG_PEER 116 attr = nla_reserve(skb, INET_DIAG_PEERS, 102 addrlen * asoc->pee 117 addrlen * asoc->peer.transport_count); 103 if (!attr) 118 if (!attr) 104 return -EMSGSIZE; 119 return -EMSGSIZE; 105 120 106 info = nla_data(attr); 121 info = nla_data(attr); 107 list_for_each_entry(from, &asoc->peer. 122 list_for_each_entry(from, &asoc->peer.transport_addr_list, 108 transports) { 123 transports) { 109 memcpy(info, &from->ipaddr, si 124 memcpy(info, &from->ipaddr, sizeof(from->ipaddr)); 110 memset(info + sizeof(from->ipa 125 memset(info + sizeof(from->ipaddr), 0, 111 addrlen - sizeof(from-> 126 addrlen - sizeof(from->ipaddr)); 112 info += addrlen; 127 info += addrlen; 113 } 128 } 114 129 115 return 0; 130 return 0; 116 } 131 } 117 132 118 /* sctp asoc/ep fill*/ 133 /* sctp asoc/ep fill*/ 119 static int inet_sctp_diag_fill(struct sock *sk 134 static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc, 120 struct sk_buff 135 struct sk_buff *skb, 121 const struct in 136 const struct inet_diag_req_v2 *req, 122 struct user_nam 137 struct user_namespace *user_ns, 123 int portid, u32 138 int portid, u32 seq, u16 nlmsg_flags, 124 const struct nl 139 const struct nlmsghdr *unlh, 125 bool net_admin) 140 bool net_admin) 126 { 141 { 127 struct sctp_endpoint *ep = sctp_sk(sk) 142 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 128 struct list_head *addr_list; 143 struct list_head *addr_list; 129 struct inet_diag_msg *r; 144 struct inet_diag_msg *r; 130 struct nlmsghdr *nlh; 145 struct nlmsghdr *nlh; 131 int ext = req->idiag_ext; 146 int ext = req->idiag_ext; 132 struct sctp_infox infox; 147 struct sctp_infox infox; 133 void *info = NULL; 148 void *info = NULL; 134 149 135 nlh = nlmsg_put(skb, portid, seq, unlh 150 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 136 nlmsg_flags); 151 nlmsg_flags); 137 if (!nlh) 152 if (!nlh) 138 return -EMSGSIZE; 153 return -EMSGSIZE; 139 154 140 r = nlmsg_data(nlh); 155 r = nlmsg_data(nlh); 141 BUG_ON(!sk_fullsock(sk)); 156 BUG_ON(!sk_fullsock(sk)); 142 157 143 r->idiag_timer = 0; 158 r->idiag_timer = 0; 144 r->idiag_retrans = 0; 159 r->idiag_retrans = 0; 145 r->idiag_expires = 0; 160 r->idiag_expires = 0; 146 if (asoc) { 161 if (asoc) { 147 inet_diag_msg_sctpasoc_fill(r, 162 inet_diag_msg_sctpasoc_fill(r, sk, asoc); 148 } else { 163 } else { 149 inet_diag_msg_common_fill(r, s 164 inet_diag_msg_common_fill(r, sk); 150 r->idiag_state = sk->sk_state; 165 r->idiag_state = sk->sk_state; 151 } 166 } 152 167 153 if (inet_diag_msg_attrs_fill(sk, skb, 168 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin)) 154 goto errout; 169 goto errout; 155 170 156 if (ext & (1 << (INET_DIAG_SKMEMINFO - 171 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) { 157 u32 mem[SK_MEMINFO_VARS]; 172 u32 mem[SK_MEMINFO_VARS]; 158 int amt; 173 int amt; 159 174 160 if (asoc && asoc->ep->sndbuf_p 175 if (asoc && asoc->ep->sndbuf_policy) 161 amt = asoc->sndbuf_use 176 amt = asoc->sndbuf_used; 162 else 177 else 163 amt = sk_wmem_alloc_ge 178 amt = sk_wmem_alloc_get(sk); 164 mem[SK_MEMINFO_WMEM_ALLOC] = a 179 mem[SK_MEMINFO_WMEM_ALLOC] = amt; 165 if (asoc && asoc->ep->rcvbuf_p 180 if (asoc && asoc->ep->rcvbuf_policy) 166 amt = atomic_read(&aso 181 amt = atomic_read(&asoc->rmem_alloc); 167 else 182 else 168 amt = sk_rmem_alloc_ge 183 amt = sk_rmem_alloc_get(sk); 169 mem[SK_MEMINFO_RMEM_ALLOC] = a 184 mem[SK_MEMINFO_RMEM_ALLOC] = amt; 170 mem[SK_MEMINFO_RCVBUF] = sk->s 185 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; 171 mem[SK_MEMINFO_SNDBUF] = sk->s 186 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; 172 mem[SK_MEMINFO_FWD_ALLOC] = sk 187 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; 173 mem[SK_MEMINFO_WMEM_QUEUED] = 188 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; 174 mem[SK_MEMINFO_OPTMEM] = atomi 189 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); 175 mem[SK_MEMINFO_BACKLOG] = READ !! 190 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; 176 mem[SK_MEMINFO_DROPS] = atomic 191 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops); 177 192 178 if (nla_put(skb, INET_DIAG_SKM 193 if (nla_put(skb, INET_DIAG_SKMEMINFO, sizeof(mem), &mem) < 0) 179 goto errout; 194 goto errout; 180 } 195 } 181 196 182 if (ext & (1 << (INET_DIAG_INFO - 1))) 197 if (ext & (1 << (INET_DIAG_INFO - 1))) { 183 struct nlattr *attr; 198 struct nlattr *attr; 184 199 185 attr = nla_reserve_64bit(skb, 200 attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 186 sizeo 201 sizeof(struct sctp_info), 187 INET_ 202 INET_DIAG_PAD); 188 if (!attr) 203 if (!attr) 189 goto errout; 204 goto errout; 190 205 191 info = nla_data(attr); 206 info = nla_data(attr); 192 } 207 } 193 infox.sctpinfo = (struct sctp_info *)i 208 infox.sctpinfo = (struct sctp_info *)info; 194 infox.asoc = asoc; 209 infox.asoc = asoc; 195 sctp_diag_get_info(sk, r, &infox); 210 sctp_diag_get_info(sk, r, &infox); 196 211 197 addr_list = asoc ? &asoc->base.bind_ad 212 addr_list = asoc ? &asoc->base.bind_addr.address_list 198 : &ep->base.bind_addr 213 : &ep->base.bind_addr.address_list; 199 if (inet_diag_msg_sctpladdrs_fill(skb, 214 if (inet_diag_msg_sctpladdrs_fill(skb, addr_list)) 200 goto errout; 215 goto errout; 201 216 202 if (asoc && (ext & (1 << (INET_DIAG_CO 217 if (asoc && (ext & (1 << (INET_DIAG_CONG - 1)))) 203 if (nla_put_string(skb, INET_D 218 if (nla_put_string(skb, INET_DIAG_CONG, "reno") < 0) 204 goto errout; 219 goto errout; 205 220 206 if (asoc && inet_diag_msg_sctpaddrs_fi 221 if (asoc && inet_diag_msg_sctpaddrs_fill(skb, asoc)) 207 goto errout; 222 goto errout; 208 223 209 nlmsg_end(skb, nlh); 224 nlmsg_end(skb, nlh); 210 return 0; 225 return 0; 211 226 212 errout: 227 errout: 213 nlmsg_cancel(skb, nlh); 228 nlmsg_cancel(skb, nlh); 214 return -EMSGSIZE; 229 return -EMSGSIZE; 215 } 230 } 216 231 217 /* callback and param */ 232 /* callback and param */ 218 struct sctp_comm_param { 233 struct sctp_comm_param { 219 struct sk_buff *skb; 234 struct sk_buff *skb; 220 struct netlink_callback *cb; 235 struct netlink_callback *cb; 221 const struct inet_diag_req_v2 *r; 236 const struct inet_diag_req_v2 *r; 222 const struct nlmsghdr *nlh; 237 const struct nlmsghdr *nlh; 223 bool net_admin; 238 bool net_admin; 224 }; 239 }; 225 240 226 static size_t inet_assoc_attr_size(struct sctp 241 static size_t inet_assoc_attr_size(struct sctp_association *asoc) 227 { 242 { 228 int addrlen = sizeof(struct sockaddr_s 243 int addrlen = sizeof(struct sockaddr_storage); 229 int addrcnt = 0; 244 int addrcnt = 0; 230 struct sctp_sockaddr_entry *laddr; 245 struct sctp_sockaddr_entry *laddr; 231 246 232 list_for_each_entry_rcu(laddr, &asoc-> 247 list_for_each_entry_rcu(laddr, &asoc->base.bind_addr.address_list, 233 list) 248 list) 234 addrcnt++; 249 addrcnt++; 235 250 236 return nla_total_size(sizeof(struct 251 return nla_total_size(sizeof(struct sctp_info)) 237 + nla_total_size(addrlen * aso 252 + nla_total_size(addrlen * asoc->peer.transport_count) 238 + nla_total_size(addrlen * add 253 + nla_total_size(addrlen * addrcnt) 239 + nla_total_size(sizeof(struct 254 + nla_total_size(sizeof(struct inet_diag_msg)) 240 + inet_diag_msg_attrs_size() 255 + inet_diag_msg_attrs_size() 241 + nla_total_size(sizeof(struct 256 + nla_total_size(sizeof(struct inet_diag_meminfo)) 242 + 64; 257 + 64; 243 } 258 } 244 259 245 static int sctp_sock_dump_one(struct sctp_endp !! 260 static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p) 246 { 261 { 247 struct sctp_association *assoc = tsp-> 262 struct sctp_association *assoc = tsp->asoc; >> 263 struct sock *sk = tsp->asoc->base.sk; 248 struct sctp_comm_param *commp = p; 264 struct sctp_comm_param *commp = p; 249 struct sock *sk = ep->base.sk; !! 265 struct sk_buff *in_skb = commp->skb; 250 const struct inet_diag_req_v2 *req = c 266 const struct inet_diag_req_v2 *req = commp->r; 251 struct sk_buff *skb = commp->skb; !! 267 const struct nlmsghdr *nlh = commp->nlh; >> 268 struct net *net = sock_net(in_skb->sk); 252 struct sk_buff *rep; 269 struct sk_buff *rep; 253 int err; 270 int err; 254 271 255 err = sock_diag_check_cookie(sk, req-> 272 err = sock_diag_check_cookie(sk, req->id.idiag_cookie); 256 if (err) 273 if (err) 257 return err; !! 274 goto out; 258 275 >> 276 err = -ENOMEM; 259 rep = nlmsg_new(inet_assoc_attr_size(a 277 rep = nlmsg_new(inet_assoc_attr_size(assoc), GFP_KERNEL); 260 if (!rep) 278 if (!rep) 261 return -ENOMEM; << 262 << 263 lock_sock(sk); << 264 if (ep != assoc->ep) { << 265 err = -EAGAIN; << 266 goto out; 279 goto out; 267 } << 268 280 269 err = inet_sctp_diag_fill(sk, assoc, r !! 281 lock_sock(sk); 270 NETLINK_CB(s !! 282 if (sk != assoc->base.sk) { 271 commp->nlh, !! 283 release_sock(sk); >> 284 sk = assoc->base.sk; >> 285 lock_sock(sk); >> 286 } >> 287 err = inet_sctp_diag_fill(sk, assoc, rep, req, >> 288 sk_user_ns(NETLINK_CB(in_skb).sk), >> 289 NETLINK_CB(in_skb).portid, >> 290 nlh->nlmsg_seq, 0, nlh, >> 291 commp->net_admin); >> 292 release_sock(sk); 272 if (err < 0) { 293 if (err < 0) { 273 WARN_ON(err == -EMSGSIZE); 294 WARN_ON(err == -EMSGSIZE); >> 295 kfree_skb(rep); 274 goto out; 296 goto out; 275 } 297 } 276 release_sock(sk); << 277 << 278 return nlmsg_unicast(sock_net(skb->sk) << 279 298 >> 299 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, >> 300 MSG_DONTWAIT); >> 301 if (err > 0) >> 302 err = 0; 280 out: 303 out: 281 release_sock(sk); << 282 kfree_skb(rep); << 283 return err; 304 return err; 284 } 305 } 285 306 286 static int sctp_sock_dump(struct sctp_endpoint 307 static int sctp_sock_dump(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p) 287 { 308 { 288 struct sctp_comm_param *commp = p; 309 struct sctp_comm_param *commp = p; 289 struct sock *sk = ep->base.sk; 310 struct sock *sk = ep->base.sk; 290 struct sk_buff *skb = commp->skb; 311 struct sk_buff *skb = commp->skb; 291 struct netlink_callback *cb = commp->c 312 struct netlink_callback *cb = commp->cb; 292 const struct inet_diag_req_v2 *r = com 313 const struct inet_diag_req_v2 *r = commp->r; 293 struct sctp_association *assoc; 314 struct sctp_association *assoc; 294 int err = 0; 315 int err = 0; 295 316 296 lock_sock(sk); 317 lock_sock(sk); 297 if (ep != tsp->asoc->ep) 318 if (ep != tsp->asoc->ep) 298 goto release; 319 goto release; 299 list_for_each_entry(assoc, &ep->asocs, 320 list_for_each_entry(assoc, &ep->asocs, asocs) { 300 if (cb->args[4] < cb->args[1]) 321 if (cb->args[4] < cb->args[1]) 301 goto next; 322 goto next; 302 323 303 if (r->id.idiag_sport != htons 324 if (r->id.idiag_sport != htons(assoc->base.bind_addr.port) && 304 r->id.idiag_sport) 325 r->id.idiag_sport) 305 goto next; 326 goto next; 306 if (r->id.idiag_dport != htons 327 if (r->id.idiag_dport != htons(assoc->peer.port) && 307 r->id.idiag_dport) 328 r->id.idiag_dport) 308 goto next; 329 goto next; 309 330 310 if (!cb->args[3] && 331 if (!cb->args[3] && 311 inet_sctp_diag_fill(sk, NU 332 inet_sctp_diag_fill(sk, NULL, skb, r, 312 sk_use 333 sk_user_ns(NETLINK_CB(cb->skb).sk), 313 NETLIN 334 NETLINK_CB(cb->skb).portid, 314 cb->nl 335 cb->nlh->nlmsg_seq, 315 NLM_F_ 336 NLM_F_MULTI, cb->nlh, 316 commp- 337 commp->net_admin) < 0) { 317 err = 1; 338 err = 1; 318 goto release; 339 goto release; 319 } 340 } 320 cb->args[3] = 1; 341 cb->args[3] = 1; 321 342 322 if (inet_sctp_diag_fill(sk, as 343 if (inet_sctp_diag_fill(sk, assoc, skb, r, 323 sk_use 344 sk_user_ns(NETLINK_CB(cb->skb).sk), 324 NETLIN 345 NETLINK_CB(cb->skb).portid, 325 cb->nl 346 cb->nlh->nlmsg_seq, 0, cb->nlh, 326 commp- 347 commp->net_admin) < 0) { 327 err = 1; 348 err = 1; 328 goto release; 349 goto release; 329 } 350 } 330 next: 351 next: 331 cb->args[4]++; 352 cb->args[4]++; 332 } 353 } 333 cb->args[1] = 0; 354 cb->args[1] = 0; 334 cb->args[3] = 0; 355 cb->args[3] = 0; 335 cb->args[4] = 0; 356 cb->args[4] = 0; 336 release: 357 release: 337 release_sock(sk); 358 release_sock(sk); 338 return err; 359 return err; 339 } 360 } 340 361 341 static int sctp_sock_filter(struct sctp_endpoi 362 static int sctp_sock_filter(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p) 342 { 363 { 343 struct sctp_comm_param *commp = p; 364 struct sctp_comm_param *commp = p; 344 struct sock *sk = ep->base.sk; 365 struct sock *sk = ep->base.sk; 345 const struct inet_diag_req_v2 *r = com 366 const struct inet_diag_req_v2 *r = commp->r; >> 367 struct sctp_association *assoc = >> 368 list_entry(ep->asocs.next, struct sctp_association, asocs); 346 369 347 /* find the ep only once through the t 370 /* find the ep only once through the transports by this condition */ 348 if (!list_is_first(&tsp->asoc->asocs, !! 371 if (tsp->asoc != assoc) 349 return 0; 372 return 0; 350 373 351 if (r->sdiag_family != AF_UNSPEC && sk 374 if (r->sdiag_family != AF_UNSPEC && sk->sk_family != r->sdiag_family) 352 return 0; 375 return 0; 353 376 354 return 1; 377 return 1; 355 } 378 } 356 379 357 static int sctp_ep_dump(struct sctp_endpoint * 380 static int sctp_ep_dump(struct sctp_endpoint *ep, void *p) 358 { 381 { 359 struct sctp_comm_param *commp = p; 382 struct sctp_comm_param *commp = p; 360 struct sock *sk = ep->base.sk; 383 struct sock *sk = ep->base.sk; 361 struct sk_buff *skb = commp->skb; 384 struct sk_buff *skb = commp->skb; 362 struct netlink_callback *cb = commp->c 385 struct netlink_callback *cb = commp->cb; 363 const struct inet_diag_req_v2 *r = com 386 const struct inet_diag_req_v2 *r = commp->r; 364 struct net *net = sock_net(skb->sk); 387 struct net *net = sock_net(skb->sk); 365 struct inet_sock *inet = inet_sk(sk); 388 struct inet_sock *inet = inet_sk(sk); 366 int err = 0; 389 int err = 0; 367 390 368 if (!net_eq(sock_net(sk), net)) 391 if (!net_eq(sock_net(sk), net)) 369 goto out; 392 goto out; 370 393 371 if (cb->args[4] < cb->args[1]) 394 if (cb->args[4] < cb->args[1]) 372 goto next; 395 goto next; 373 396 374 if (!(r->idiag_states & TCPF_LISTEN) & 397 if (!(r->idiag_states & TCPF_LISTEN) && !list_empty(&ep->asocs)) 375 goto next; 398 goto next; 376 399 377 if (r->sdiag_family != AF_UNSPEC && 400 if (r->sdiag_family != AF_UNSPEC && 378 sk->sk_family != r->sdiag_family) 401 sk->sk_family != r->sdiag_family) 379 goto next; 402 goto next; 380 403 381 if (r->id.idiag_sport != inet->inet_sp 404 if (r->id.idiag_sport != inet->inet_sport && 382 r->id.idiag_sport) 405 r->id.idiag_sport) 383 goto next; 406 goto next; 384 407 385 if (r->id.idiag_dport != inet->inet_dp 408 if (r->id.idiag_dport != inet->inet_dport && 386 r->id.idiag_dport) 409 r->id.idiag_dport) 387 goto next; 410 goto next; 388 411 389 if (inet_sctp_diag_fill(sk, NULL, skb, 412 if (inet_sctp_diag_fill(sk, NULL, skb, r, 390 sk_user_ns(NET 413 sk_user_ns(NETLINK_CB(cb->skb).sk), 391 NETLINK_CB(cb- 414 NETLINK_CB(cb->skb).portid, 392 cb->nlh->nlmsg 415 cb->nlh->nlmsg_seq, NLM_F_MULTI, 393 cb->nlh, commp 416 cb->nlh, commp->net_admin) < 0) { 394 err = 2; 417 err = 2; 395 goto out; 418 goto out; 396 } 419 } 397 next: 420 next: 398 cb->args[4]++; 421 cb->args[4]++; 399 out: 422 out: 400 return err; 423 return err; 401 } 424 } 402 425 403 /* define the functions for sctp_diag_handler* 426 /* define the functions for sctp_diag_handler*/ 404 static void sctp_diag_get_info(struct sock *sk 427 static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, 405 void *info) 428 void *info) 406 { 429 { 407 struct sctp_infox *infox = (struct sct 430 struct sctp_infox *infox = (struct sctp_infox *)info; 408 431 409 if (infox->asoc) { 432 if (infox->asoc) { 410 r->idiag_rqueue = atomic_read( 433 r->idiag_rqueue = atomic_read(&infox->asoc->rmem_alloc); 411 r->idiag_wqueue = infox->asoc- 434 r->idiag_wqueue = infox->asoc->sndbuf_used; 412 } else { 435 } else { 413 r->idiag_rqueue = READ_ONCE(sk !! 436 r->idiag_rqueue = sk->sk_ack_backlog; 414 r->idiag_wqueue = READ_ONCE(sk !! 437 r->idiag_wqueue = sk->sk_max_ack_backlog; 415 } 438 } 416 if (infox->sctpinfo) 439 if (infox->sctpinfo) 417 sctp_get_sctp_info(sk, infox-> 440 sctp_get_sctp_info(sk, infox->asoc, infox->sctpinfo); 418 } 441 } 419 442 420 static int sctp_diag_dump_one(struct netlink_c !! 443 static int sctp_diag_dump_one(struct sk_buff *in_skb, >> 444 const struct nlmsghdr *nlh, 421 const struct ine 445 const struct inet_diag_req_v2 *req) 422 { 446 { 423 struct sk_buff *skb = cb->skb; !! 447 struct net *net = sock_net(in_skb->sk); 424 struct net *net = sock_net(skb->sk); << 425 const struct nlmsghdr *nlh = cb->nlh; << 426 union sctp_addr laddr, paddr; 448 union sctp_addr laddr, paddr; 427 int dif = req->id.idiag_if; << 428 struct sctp_comm_param commp = { 449 struct sctp_comm_param commp = { 429 .skb = skb, !! 450 .skb = in_skb, 430 .r = req, 451 .r = req, 431 .nlh = nlh, 452 .nlh = nlh, 432 .net_admin = netlink_net_capab !! 453 .net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN), 433 }; 454 }; 434 455 435 if (req->sdiag_family == AF_INET) { 456 if (req->sdiag_family == AF_INET) { 436 laddr.v4.sin_port = req->id.id 457 laddr.v4.sin_port = req->id.idiag_sport; 437 laddr.v4.sin_addr.s_addr = req 458 laddr.v4.sin_addr.s_addr = req->id.idiag_src[0]; 438 laddr.v4.sin_family = AF_INET; 459 laddr.v4.sin_family = AF_INET; 439 460 440 paddr.v4.sin_port = req->id.id 461 paddr.v4.sin_port = req->id.idiag_dport; 441 paddr.v4.sin_addr.s_addr = req 462 paddr.v4.sin_addr.s_addr = req->id.idiag_dst[0]; 442 paddr.v4.sin_family = AF_INET; 463 paddr.v4.sin_family = AF_INET; 443 } else { 464 } else { 444 laddr.v6.sin6_port = req->id.i 465 laddr.v6.sin6_port = req->id.idiag_sport; 445 memcpy(&laddr.v6.sin6_addr, re 466 memcpy(&laddr.v6.sin6_addr, req->id.idiag_src, 446 sizeof(laddr.v6.sin6_ad 467 sizeof(laddr.v6.sin6_addr)); 447 laddr.v6.sin6_family = AF_INET 468 laddr.v6.sin6_family = AF_INET6; 448 469 449 paddr.v6.sin6_port = req->id.i 470 paddr.v6.sin6_port = req->id.idiag_dport; 450 memcpy(&paddr.v6.sin6_addr, re 471 memcpy(&paddr.v6.sin6_addr, req->id.idiag_dst, 451 sizeof(paddr.v6.sin6_ad 472 sizeof(paddr.v6.sin6_addr)); 452 paddr.v6.sin6_family = AF_INET 473 paddr.v6.sin6_family = AF_INET6; 453 } 474 } 454 475 455 return sctp_transport_lookup_process(s !! 476 return sctp_transport_lookup_process(sctp_tsp_dump_one, 456 n !! 477 net, &laddr, &paddr, &commp); 457 } 478 } 458 479 459 static void sctp_diag_dump(struct sk_buff *skb 480 static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 460 const struct inet_d !! 481 const struct inet_diag_req_v2 *r, struct nlattr *bc) 461 { 482 { 462 u32 idiag_states = r->idiag_states; 483 u32 idiag_states = r->idiag_states; 463 struct net *net = sock_net(skb->sk); 484 struct net *net = sock_net(skb->sk); 464 struct sctp_comm_param commp = { 485 struct sctp_comm_param commp = { 465 .skb = skb, 486 .skb = skb, 466 .cb = cb, 487 .cb = cb, 467 .r = r, 488 .r = r, 468 .net_admin = netlink_net_capab 489 .net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN), 469 }; 490 }; 470 int pos = cb->args[2]; 491 int pos = cb->args[2]; 471 492 472 /* eps hashtable dumps 493 /* eps hashtable dumps 473 * args: 494 * args: 474 * 0 : if it will traversal listen soc 495 * 0 : if it will traversal listen sock 475 * 1 : to record the sock pos of this 496 * 1 : to record the sock pos of this time's traversal 476 * 4 : to work as a temporary variable 497 * 4 : to work as a temporary variable to traversal list 477 */ 498 */ 478 if (cb->args[0] == 0) { 499 if (cb->args[0] == 0) { 479 if (!(idiag_states & TCPF_LIST 500 if (!(idiag_states & TCPF_LISTEN)) 480 goto skip; 501 goto skip; 481 if (sctp_for_each_endpoint(sct 502 if (sctp_for_each_endpoint(sctp_ep_dump, &commp)) 482 goto done; 503 goto done; 483 skip: 504 skip: 484 cb->args[0] = 1; 505 cb->args[0] = 1; 485 cb->args[1] = 0; 506 cb->args[1] = 0; 486 cb->args[4] = 0; 507 cb->args[4] = 0; 487 } 508 } 488 509 489 /* asocs by transport hashtable dump 510 /* asocs by transport hashtable dump 490 * args: 511 * args: 491 * 1 : to record the assoc pos of this 512 * 1 : to record the assoc pos of this time's traversal 492 * 2 : to record the transport pos of 513 * 2 : to record the transport pos of this time's traversal 493 * 3 : to mark if we have dumped the e 514 * 3 : to mark if we have dumped the ep info of the current asoc 494 * 4 : to work as a temporary variable 515 * 4 : to work as a temporary variable to traversal list 495 * 5 : to save the sk we get from trav 516 * 5 : to save the sk we get from travelsing the tsp list. 496 */ 517 */ 497 if (!(idiag_states & ~(TCPF_LISTEN | T 518 if (!(idiag_states & ~(TCPF_LISTEN | TCPF_CLOSE))) 498 goto done; 519 goto done; 499 520 500 sctp_transport_traverse_process(sctp_s 521 sctp_transport_traverse_process(sctp_sock_filter, sctp_sock_dump, 501 net, & 522 net, &pos, &commp); 502 cb->args[2] = pos; 523 cb->args[2] = pos; 503 524 504 done: 525 done: 505 cb->args[1] = cb->args[4]; 526 cb->args[1] = cb->args[4]; 506 cb->args[4] = 0; 527 cb->args[4] = 0; 507 } 528 } 508 529 509 static const struct inet_diag_handler sctp_dia 530 static const struct inet_diag_handler sctp_diag_handler = { 510 .owner = THIS_MODULE, << 511 .dump = sctp_diag_dump, 531 .dump = sctp_diag_dump, 512 .dump_one = sctp_diag_dump_one, 532 .dump_one = sctp_diag_dump_one, 513 .idiag_get_info = sctp_diag_get_info, 533 .idiag_get_info = sctp_diag_get_info, 514 .idiag_type = IPPROTO_SCTP, 534 .idiag_type = IPPROTO_SCTP, 515 .idiag_info_size = sizeof(struct sctp_ 535 .idiag_info_size = sizeof(struct sctp_info), 516 }; 536 }; 517 537 518 static int __init sctp_diag_init(void) 538 static int __init sctp_diag_init(void) 519 { 539 { 520 return inet_diag_register(&sctp_diag_h 540 return inet_diag_register(&sctp_diag_handler); 521 } 541 } 522 542 523 static void __exit sctp_diag_exit(void) 543 static void __exit sctp_diag_exit(void) 524 { 544 { 525 inet_diag_unregister(&sctp_diag_handle 545 inet_diag_unregister(&sctp_diag_handler); 526 } 546 } 527 547 528 module_init(sctp_diag_init); 548 module_init(sctp_diag_init); 529 module_exit(sctp_diag_exit); 549 module_exit(sctp_diag_exit); 530 MODULE_LICENSE("GPL"); 550 MODULE_LICENSE("GPL"); 531 MODULE_DESCRIPTION("SCTP socket monitoring via << 532 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NET 551 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-132); 533 552
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.