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