1/* SCTP kernel implementation
2 * Copyright (c) 2003 International Business Machines, Corp.
3 *
4 * This file is part of the SCTP kernel implementation
5 *
6 * This SCTP implementation is free software;
7 * you can redistribute it and/or modify it under the terms of
8 * the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This SCTP implementation is distributed in the hope that it
13 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 *                 ************************
15 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU CC; see the file COPYING.  If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * Please send any bug reports or fixes you make to the
23 * email address(es):
24 *    lksctp developers <linux-sctp@vger.kernel.org>
25 *
26 * Written or modified by:
27 *    Sridhar Samudrala <sri@us.ibm.com>
28 */
29
30#include <linux/types.h>
31#include <linux/seq_file.h>
32#include <linux/init.h>
33#include <linux/export.h>
34#include <net/sctp/sctp.h>
35#include <net/ip.h> /* for snmp_fold_field */
36
37static const struct snmp_mib sctp_snmp_list[] = {
38	SNMP_MIB_ITEM("SctpCurrEstab", SCTP_MIB_CURRESTAB),
39	SNMP_MIB_ITEM("SctpActiveEstabs", SCTP_MIB_ACTIVEESTABS),
40	SNMP_MIB_ITEM("SctpPassiveEstabs", SCTP_MIB_PASSIVEESTABS),
41	SNMP_MIB_ITEM("SctpAborteds", SCTP_MIB_ABORTEDS),
42	SNMP_MIB_ITEM("SctpShutdowns", SCTP_MIB_SHUTDOWNS),
43	SNMP_MIB_ITEM("SctpOutOfBlues", SCTP_MIB_OUTOFBLUES),
44	SNMP_MIB_ITEM("SctpChecksumErrors", SCTP_MIB_CHECKSUMERRORS),
45	SNMP_MIB_ITEM("SctpOutCtrlChunks", SCTP_MIB_OUTCTRLCHUNKS),
46	SNMP_MIB_ITEM("SctpOutOrderChunks", SCTP_MIB_OUTORDERCHUNKS),
47	SNMP_MIB_ITEM("SctpOutUnorderChunks", SCTP_MIB_OUTUNORDERCHUNKS),
48	SNMP_MIB_ITEM("SctpInCtrlChunks", SCTP_MIB_INCTRLCHUNKS),
49	SNMP_MIB_ITEM("SctpInOrderChunks", SCTP_MIB_INORDERCHUNKS),
50	SNMP_MIB_ITEM("SctpInUnorderChunks", SCTP_MIB_INUNORDERCHUNKS),
51	SNMP_MIB_ITEM("SctpFragUsrMsgs", SCTP_MIB_FRAGUSRMSGS),
52	SNMP_MIB_ITEM("SctpReasmUsrMsgs", SCTP_MIB_REASMUSRMSGS),
53	SNMP_MIB_ITEM("SctpOutSCTPPacks", SCTP_MIB_OUTSCTPPACKS),
54	SNMP_MIB_ITEM("SctpInSCTPPacks", SCTP_MIB_INSCTPPACKS),
55	SNMP_MIB_ITEM("SctpT1InitExpireds", SCTP_MIB_T1_INIT_EXPIREDS),
56	SNMP_MIB_ITEM("SctpT1CookieExpireds", SCTP_MIB_T1_COOKIE_EXPIREDS),
57	SNMP_MIB_ITEM("SctpT2ShutdownExpireds", SCTP_MIB_T2_SHUTDOWN_EXPIREDS),
58	SNMP_MIB_ITEM("SctpT3RtxExpireds", SCTP_MIB_T3_RTX_EXPIREDS),
59	SNMP_MIB_ITEM("SctpT4RtoExpireds", SCTP_MIB_T4_RTO_EXPIREDS),
60	SNMP_MIB_ITEM("SctpT5ShutdownGuardExpireds", SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS),
61	SNMP_MIB_ITEM("SctpDelaySackExpireds", SCTP_MIB_DELAY_SACK_EXPIREDS),
62	SNMP_MIB_ITEM("SctpAutocloseExpireds", SCTP_MIB_AUTOCLOSE_EXPIREDS),
63	SNMP_MIB_ITEM("SctpT3Retransmits", SCTP_MIB_T3_RETRANSMITS),
64	SNMP_MIB_ITEM("SctpPmtudRetransmits", SCTP_MIB_PMTUD_RETRANSMITS),
65	SNMP_MIB_ITEM("SctpFastRetransmits", SCTP_MIB_FAST_RETRANSMITS),
66	SNMP_MIB_ITEM("SctpInPktSoftirq", SCTP_MIB_IN_PKT_SOFTIRQ),
67	SNMP_MIB_ITEM("SctpInPktBacklog", SCTP_MIB_IN_PKT_BACKLOG),
68	SNMP_MIB_ITEM("SctpInPktDiscards", SCTP_MIB_IN_PKT_DISCARDS),
69	SNMP_MIB_ITEM("SctpInDataChunkDiscards", SCTP_MIB_IN_DATA_CHUNK_DISCARDS),
70	SNMP_MIB_SENTINEL
71};
72
73/* Display sctp snmp mib statistics(/proc/net/sctp/snmp). */
74static int sctp_snmp_seq_show(struct seq_file *seq, void *v)
75{
76	struct net *net = seq->private;
77	int i;
78
79	for (i = 0; sctp_snmp_list[i].name != NULL; i++)
80		seq_printf(seq, "%-32s\t%ld\n", sctp_snmp_list[i].name,
81			   snmp_fold_field(net->sctp.sctp_statistics,
82				      sctp_snmp_list[i].entry));
83
84	return 0;
85}
86
87/* Initialize the seq file operations for 'snmp' object. */
88static int sctp_snmp_seq_open(struct inode *inode, struct file *file)
89{
90	return single_open_net(inode, file, sctp_snmp_seq_show);
91}
92
93static const struct file_operations sctp_snmp_seq_fops = {
94	.owner	 = THIS_MODULE,
95	.open	 = sctp_snmp_seq_open,
96	.read	 = seq_read,
97	.llseek	 = seq_lseek,
98	.release = single_release_net,
99};
100
101/* Set up the proc fs entry for 'snmp' object. */
102int __net_init sctp_snmp_proc_init(struct net *net)
103{
104	struct proc_dir_entry *p;
105
106	p = proc_create("snmp", S_IRUGO, net->sctp.proc_net_sctp,
107			&sctp_snmp_seq_fops);
108	if (!p)
109		return -ENOMEM;
110
111	return 0;
112}
113
114/* Cleanup the proc fs entry for 'snmp' object. */
115void sctp_snmp_proc_exit(struct net *net)
116{
117	remove_proc_entry("snmp", net->sctp.proc_net_sctp);
118}
119
120/* Dump local addresses of an association/endpoint. */
121static void sctp_seq_dump_local_addrs(struct seq_file *seq, struct sctp_ep_common *epb)
122{
123	struct sctp_association *asoc;
124	struct sctp_sockaddr_entry *laddr;
125	struct sctp_transport *peer;
126	union sctp_addr *addr, *primary = NULL;
127	struct sctp_af *af;
128
129	if (epb->type == SCTP_EP_TYPE_ASSOCIATION) {
130		asoc = sctp_assoc(epb);
131
132		peer = asoc->peer.primary_path;
133		if (unlikely(peer == NULL)) {
134			WARN(1, "Association %p with NULL primary path!\n", asoc);
135			return;
136		}
137
138		primary = &peer->saddr;
139	}
140
141	rcu_read_lock();
142	list_for_each_entry_rcu(laddr, &epb->bind_addr.address_list, list) {
143		if (!laddr->valid)
144			continue;
145
146		addr = &laddr->a;
147		af = sctp_get_af_specific(addr->sa.sa_family);
148		if (primary && af->cmp_addr(addr, primary)) {
149			seq_printf(seq, "*");
150		}
151		af->seq_dump_addr(seq, addr);
152	}
153	rcu_read_unlock();
154}
155
156/* Dump remote addresses of an association. */
157static void sctp_seq_dump_remote_addrs(struct seq_file *seq, struct sctp_association *assoc)
158{
159	struct sctp_transport *transport;
160	union sctp_addr *addr, *primary;
161	struct sctp_af *af;
162
163	primary = &assoc->peer.primary_addr;
164	rcu_read_lock();
165	list_for_each_entry_rcu(transport, &assoc->peer.transport_addr_list,
166			transports) {
167		addr = &transport->ipaddr;
168		if (transport->dead)
169			continue;
170
171		af = sctp_get_af_specific(addr->sa.sa_family);
172		if (af->cmp_addr(addr, primary)) {
173			seq_printf(seq, "*");
174		}
175		af->seq_dump_addr(seq, addr);
176	}
177	rcu_read_unlock();
178}
179
180static void *sctp_eps_seq_start(struct seq_file *seq, loff_t *pos)
181{
182	if (*pos >= sctp_ep_hashsize)
183		return NULL;
184
185	if (*pos < 0)
186		*pos = 0;
187
188	if (*pos == 0)
189		seq_printf(seq, " ENDPT     SOCK   STY SST HBKT LPORT   UID INODE LADDRS\n");
190
191	return (void *)pos;
192}
193
194static void sctp_eps_seq_stop(struct seq_file *seq, void *v)
195{
196}
197
198
199static void *sctp_eps_seq_next(struct seq_file *seq, void *v, loff_t *pos)
200{
201	if (++*pos >= sctp_ep_hashsize)
202		return NULL;
203
204	return pos;
205}
206
207
208/* Display sctp endpoints (/proc/net/sctp/eps). */
209static int sctp_eps_seq_show(struct seq_file *seq, void *v)
210{
211	struct sctp_hashbucket *head;
212	struct sctp_ep_common *epb;
213	struct sctp_endpoint *ep;
214	struct sock *sk;
215	int    hash = *(loff_t *)v;
216
217	if (hash >= sctp_ep_hashsize)
218		return -ENOMEM;
219
220	head = &sctp_ep_hashtable[hash];
221	local_bh_disable();
222	read_lock(&head->lock);
223	sctp_for_each_hentry(epb, &head->chain) {
224		ep = sctp_ep(epb);
225		sk = epb->sk;
226		if (!net_eq(sock_net(sk), seq_file_net(seq)))
227			continue;
228		seq_printf(seq, "%8pK %8pK %-3d %-3d %-4d %-5d %5u %5lu ", ep, sk,
229			   sctp_sk(sk)->type, sk->sk_state, hash,
230			   epb->bind_addr.port,
231			   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
232			   sock_i_ino(sk));
233
234		sctp_seq_dump_local_addrs(seq, epb);
235		seq_printf(seq, "\n");
236	}
237	read_unlock(&head->lock);
238	local_bh_enable();
239
240	return 0;
241}
242
243static const struct seq_operations sctp_eps_ops = {
244	.start = sctp_eps_seq_start,
245	.next  = sctp_eps_seq_next,
246	.stop  = sctp_eps_seq_stop,
247	.show  = sctp_eps_seq_show,
248};
249
250
251/* Initialize the seq file operations for 'eps' object. */
252static int sctp_eps_seq_open(struct inode *inode, struct file *file)
253{
254	return seq_open_net(inode, file, &sctp_eps_ops,
255			    sizeof(struct seq_net_private));
256}
257
258static const struct file_operations sctp_eps_seq_fops = {
259	.open	 = sctp_eps_seq_open,
260	.read	 = seq_read,
261	.llseek	 = seq_lseek,
262	.release = seq_release_net,
263};
264
265/* Set up the proc fs entry for 'eps' object. */
266int __net_init sctp_eps_proc_init(struct net *net)
267{
268	struct proc_dir_entry *p;
269
270	p = proc_create("eps", S_IRUGO, net->sctp.proc_net_sctp,
271			&sctp_eps_seq_fops);
272	if (!p)
273		return -ENOMEM;
274
275	return 0;
276}
277
278/* Cleanup the proc fs entry for 'eps' object. */
279void sctp_eps_proc_exit(struct net *net)
280{
281	remove_proc_entry("eps", net->sctp.proc_net_sctp);
282}
283
284
285static void *sctp_assocs_seq_start(struct seq_file *seq, loff_t *pos)
286{
287	if (*pos >= sctp_assoc_hashsize)
288		return NULL;
289
290	if (*pos < 0)
291		*pos = 0;
292
293	if (*pos == 0)
294		seq_printf(seq, " ASSOC     SOCK   STY SST ST HBKT "
295				"ASSOC-ID TX_QUEUE RX_QUEUE UID INODE LPORT "
296				"RPORT LADDRS <-> RADDRS "
297				"HBINT INS OUTS MAXRT T1X T2X RTXC "
298				"wmema wmemq sndbuf rcvbuf\n");
299
300	return (void *)pos;
301}
302
303static void sctp_assocs_seq_stop(struct seq_file *seq, void *v)
304{
305}
306
307
308static void *sctp_assocs_seq_next(struct seq_file *seq, void *v, loff_t *pos)
309{
310	if (++*pos >= sctp_assoc_hashsize)
311		return NULL;
312
313	return pos;
314}
315
316/* Display sctp associations (/proc/net/sctp/assocs). */
317static int sctp_assocs_seq_show(struct seq_file *seq, void *v)
318{
319	struct sctp_hashbucket *head;
320	struct sctp_ep_common *epb;
321	struct sctp_association *assoc;
322	struct sock *sk;
323	int    hash = *(loff_t *)v;
324
325	if (hash >= sctp_assoc_hashsize)
326		return -ENOMEM;
327
328	head = &sctp_assoc_hashtable[hash];
329	local_bh_disable();
330	read_lock(&head->lock);
331	sctp_for_each_hentry(epb, &head->chain) {
332		assoc = sctp_assoc(epb);
333		sk = epb->sk;
334		if (!net_eq(sock_net(sk), seq_file_net(seq)))
335			continue;
336		seq_printf(seq,
337			   "%8pK %8pK %-3d %-3d %-2d %-4d "
338			   "%4d %8d %8d %7u %5lu %-5d %5d ",
339			   assoc, sk, sctp_sk(sk)->type, sk->sk_state,
340			   assoc->state, hash,
341			   assoc->assoc_id,
342			   assoc->sndbuf_used,
343			   atomic_read(&assoc->rmem_alloc),
344			   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
345			   sock_i_ino(sk),
346			   epb->bind_addr.port,
347			   assoc->peer.port);
348		seq_printf(seq, " ");
349		sctp_seq_dump_local_addrs(seq, epb);
350		seq_printf(seq, "<-> ");
351		sctp_seq_dump_remote_addrs(seq, assoc);
352		seq_printf(seq, "\t%8lu %5d %5d %4d %4d %4d %8d "
353			   "%8d %8d %8d %8d",
354			assoc->hbinterval, assoc->c.sinit_max_instreams,
355			assoc->c.sinit_num_ostreams, assoc->max_retrans,
356			assoc->init_retries, assoc->shutdown_retries,
357			assoc->rtx_data_chunks,
358			atomic_read(&sk->sk_wmem_alloc),
359			sk->sk_wmem_queued,
360			sk->sk_sndbuf,
361			sk->sk_rcvbuf);
362		seq_printf(seq, "\n");
363	}
364	read_unlock(&head->lock);
365	local_bh_enable();
366
367	return 0;
368}
369
370static const struct seq_operations sctp_assoc_ops = {
371	.start = sctp_assocs_seq_start,
372	.next  = sctp_assocs_seq_next,
373	.stop  = sctp_assocs_seq_stop,
374	.show  = sctp_assocs_seq_show,
375};
376
377/* Initialize the seq file operations for 'assocs' object. */
378static int sctp_assocs_seq_open(struct inode *inode, struct file *file)
379{
380	return seq_open_net(inode, file, &sctp_assoc_ops,
381			    sizeof(struct seq_net_private));
382}
383
384static const struct file_operations sctp_assocs_seq_fops = {
385	.open	 = sctp_assocs_seq_open,
386	.read	 = seq_read,
387	.llseek	 = seq_lseek,
388	.release = seq_release_net,
389};
390
391/* Set up the proc fs entry for 'assocs' object. */
392int __net_init sctp_assocs_proc_init(struct net *net)
393{
394	struct proc_dir_entry *p;
395
396	p = proc_create("assocs", S_IRUGO, net->sctp.proc_net_sctp,
397			&sctp_assocs_seq_fops);
398	if (!p)
399		return -ENOMEM;
400
401	return 0;
402}
403
404/* Cleanup the proc fs entry for 'assocs' object. */
405void sctp_assocs_proc_exit(struct net *net)
406{
407	remove_proc_entry("assocs", net->sctp.proc_net_sctp);
408}
409
410static void *sctp_remaddr_seq_start(struct seq_file *seq, loff_t *pos)
411{
412	if (*pos >= sctp_assoc_hashsize)
413		return NULL;
414
415	if (*pos < 0)
416		*pos = 0;
417
418	if (*pos == 0)
419		seq_printf(seq, "ADDR ASSOC_ID HB_ACT RTO MAX_PATH_RTX "
420				"REM_ADDR_RTX START STATE\n");
421
422	return (void *)pos;
423}
424
425static void *sctp_remaddr_seq_next(struct seq_file *seq, void *v, loff_t *pos)
426{
427	if (++*pos >= sctp_assoc_hashsize)
428		return NULL;
429
430	return pos;
431}
432
433static void sctp_remaddr_seq_stop(struct seq_file *seq, void *v)
434{
435}
436
437static int sctp_remaddr_seq_show(struct seq_file *seq, void *v)
438{
439	struct sctp_hashbucket *head;
440	struct sctp_ep_common *epb;
441	struct sctp_association *assoc;
442	struct sctp_transport *tsp;
443	int    hash = *(loff_t *)v;
444
445	if (hash >= sctp_assoc_hashsize)
446		return -ENOMEM;
447
448	head = &sctp_assoc_hashtable[hash];
449	local_bh_disable();
450	read_lock(&head->lock);
451	rcu_read_lock();
452	sctp_for_each_hentry(epb, &head->chain) {
453		if (!net_eq(sock_net(epb->sk), seq_file_net(seq)))
454			continue;
455		assoc = sctp_assoc(epb);
456		list_for_each_entry_rcu(tsp, &assoc->peer.transport_addr_list,
457					transports) {
458			if (tsp->dead)
459				continue;
460
461			/*
462			 * The remote address (ADDR)
463			 */
464			tsp->af_specific->seq_dump_addr(seq, &tsp->ipaddr);
465			seq_printf(seq, " ");
466
467			/*
468			 * The association ID (ASSOC_ID)
469			 */
470			seq_printf(seq, "%d ", tsp->asoc->assoc_id);
471
472			/*
473			 * If the Heartbeat is active (HB_ACT)
474			 * Note: 1 = Active, 0 = Inactive
475			 */
476			seq_printf(seq, "%d ", timer_pending(&tsp->hb_timer));
477
478			/*
479			 * Retransmit time out (RTO)
480			 */
481			seq_printf(seq, "%lu ", tsp->rto);
482
483			/*
484			 * Maximum path retransmit count (PATH_MAX_RTX)
485			 */
486			seq_printf(seq, "%d ", tsp->pathmaxrxt);
487
488			/*
489			 * remote address retransmit count (REM_ADDR_RTX)
490			 * Note: We don't have a way to tally this at the moment
491			 * so lets just leave it as zero for the moment
492			 */
493			seq_puts(seq, "0 ");
494
495			/*
496			 * remote address start time (START).  This is also not
497			 * currently implemented, but we can record it with a
498			 * jiffies marker in a subsequent patch
499			 */
500			seq_puts(seq, "0 ");
501
502			/*
503			 * The current state of this destination. I.e.
504			 * SCTP_ACTIVE, SCTP_INACTIVE, ...
505			 */
506			seq_printf(seq, "%d", tsp->state);
507
508			seq_printf(seq, "\n");
509		}
510	}
511
512	rcu_read_unlock();
513	read_unlock(&head->lock);
514	local_bh_enable();
515
516	return 0;
517
518}
519
520static const struct seq_operations sctp_remaddr_ops = {
521	.start = sctp_remaddr_seq_start,
522	.next  = sctp_remaddr_seq_next,
523	.stop  = sctp_remaddr_seq_stop,
524	.show  = sctp_remaddr_seq_show,
525};
526
527/* Cleanup the proc fs entry for 'remaddr' object. */
528void sctp_remaddr_proc_exit(struct net *net)
529{
530	remove_proc_entry("remaddr", net->sctp.proc_net_sctp);
531}
532
533static int sctp_remaddr_seq_open(struct inode *inode, struct file *file)
534{
535	return seq_open_net(inode, file, &sctp_remaddr_ops,
536			    sizeof(struct seq_net_private));
537}
538
539static const struct file_operations sctp_remaddr_seq_fops = {
540	.open = sctp_remaddr_seq_open,
541	.read = seq_read,
542	.llseek = seq_lseek,
543	.release = seq_release_net,
544};
545
546int __net_init sctp_remaddr_proc_init(struct net *net)
547{
548	struct proc_dir_entry *p;
549
550	p = proc_create("remaddr", S_IRUGO, net->sctp.proc_net_sctp,
551			&sctp_remaddr_seq_fops);
552	if (!p)
553		return -ENOMEM;
554	return 0;
555}
556