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