Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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