xref: /linux/net/atm/proc.c (revision 64edfa65062dc4509ba75978116b2f6d392346f5)
1 // SPDX-License-Identifier: GPL-2.0
2 /* net/atm/proc.c - ATM /proc interface
3  *
4  * Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA
5  *
6  * seq_file api usage by romieu@fr.zoreil.com
7  *
8  * Evaluating the efficiency of the whole thing if left as an exercise to
9  * the reader.
10  */
11 
12 #include <linux/module.h> /* for EXPORT_SYMBOL */
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/mm.h>
16 #include <linux/fs.h>
17 #include <linux/stat.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/errno.h>
21 #include <linux/atm.h>
22 #include <linux/atmdev.h>
23 #include <linux/netdevice.h>
24 #include <linux/init.h> /* for __init */
25 #include <linux/slab.h>
26 #include <net/net_namespace.h>
27 #include <linux/uaccess.h>
28 #include <linux/param.h> /* for HZ */
29 #include <linux/atomic.h>
30 #include "resources.h"
31 #include "common.h" /* atm_proc_init prototype */
32 #include "signaling.h" /* to get sigd - ugly too */
33 
34 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
35 				 size_t count, loff_t *pos);
36 
37 static const struct proc_ops atm_dev_proc_ops = {
38 	.proc_read	= proc_dev_atm_read,
39 	.proc_lseek	= noop_llseek,
40 };
41 
add_stats(struct seq_file * seq,const char * aal,const struct k_atm_aal_stats * stats)42 static void add_stats(struct seq_file *seq, const char *aal,
43   const struct k_atm_aal_stats *stats)
44 {
45 	seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
46 		   atomic_read(&stats->tx), atomic_read(&stats->tx_err),
47 		   atomic_read(&stats->rx), atomic_read(&stats->rx_err),
48 		   atomic_read(&stats->rx_drop));
49 }
50 
atm_dev_info(struct seq_file * seq,const struct atm_dev * dev)51 static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
52 {
53 	int i;
54 
55 	seq_printf(seq, "%3d %-8s", dev->number, dev->type);
56 	for (i = 0; i < ESI_LEN; i++)
57 		seq_printf(seq, "%02x", dev->esi[i]);
58 	seq_puts(seq, "  ");
59 	add_stats(seq, "0", &dev->stats.aal0);
60 	seq_puts(seq, "  ");
61 	add_stats(seq, "5", &dev->stats.aal5);
62 	seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt));
63 	seq_putc(seq, '\n');
64 }
65 
66 struct vcc_state {
67 	int bucket;
68 	struct sock *sk;
69 };
70 
compare_family(struct sock * sk,int family)71 static inline int compare_family(struct sock *sk, int family)
72 {
73 	return !family || (sk->sk_family == family);
74 }
75 
__vcc_walk(struct sock ** sock,int family,int * bucket,loff_t l)76 static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
77 {
78 	struct sock *sk = *sock;
79 
80 	if (sk == SEQ_START_TOKEN) {
81 		for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
82 			struct hlist_head *head = &vcc_hash[*bucket];
83 
84 			sk = hlist_empty(head) ? NULL : __sk_head(head);
85 			if (sk)
86 				break;
87 		}
88 		l--;
89 	}
90 try_again:
91 	for (; sk; sk = sk_next(sk)) {
92 		l -= compare_family(sk, family);
93 		if (l < 0)
94 			goto out;
95 	}
96 	if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
97 		sk = sk_head(&vcc_hash[*bucket]);
98 		goto try_again;
99 	}
100 	sk = SEQ_START_TOKEN;
101 out:
102 	*sock = sk;
103 	return (l < 0);
104 }
105 
vcc_walk(struct seq_file * seq,loff_t l)106 static inline void *vcc_walk(struct seq_file *seq, loff_t l)
107 {
108 	struct vcc_state *state = seq->private;
109 	int family = (uintptr_t)(pde_data(file_inode(seq->file)));
110 
111 	return __vcc_walk(&state->sk, family, &state->bucket, l) ?
112 	       state : NULL;
113 }
114 
vcc_seq_start(struct seq_file * seq,loff_t * pos)115 static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
116 	__acquires(vcc_sklist_lock)
117 {
118 	struct vcc_state *state = seq->private;
119 	loff_t left = *pos;
120 
121 	read_lock(&vcc_sklist_lock);
122 	state->sk = SEQ_START_TOKEN;
123 	return left ? vcc_walk(seq, left) : SEQ_START_TOKEN;
124 }
125 
vcc_seq_stop(struct seq_file * seq,void * v)126 static void vcc_seq_stop(struct seq_file *seq, void *v)
127 	__releases(vcc_sklist_lock)
128 {
129 	read_unlock(&vcc_sklist_lock);
130 }
131 
vcc_seq_next(struct seq_file * seq,void * v,loff_t * pos)132 static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
133 {
134 	v = vcc_walk(seq, 1);
135 	(*pos)++;
136 	return v;
137 }
138 
pvc_info(struct seq_file * seq,struct atm_vcc * vcc)139 static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
140 {
141 	static const char *const class_name[] = {
142 		"off", "UBR", "CBR", "VBR", "ABR"};
143 	static const char *const aal_name[] = {
144 		"---",	"1",	"2",	"3/4",	/*  0- 3 */
145 		"???",	"5",	"???",	"???",	/*  4- 7 */
146 		"???",	"???",	"???",	"???",	/*  8-11 */
147 		"???",	"0",	"???",	"???"};	/* 12-15 */
148 
149 	seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
150 		   vcc->dev->number, vcc->vpi, vcc->vci,
151 		   vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
152 		   aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr,
153 		   class_name[vcc->qos.rxtp.traffic_class],
154 		   vcc->qos.txtp.min_pcr,
155 		   class_name[vcc->qos.txtp.traffic_class]);
156 	seq_putc(seq, '\n');
157 }
158 
vcc_state(struct atm_vcc * vcc)159 static const char *vcc_state(struct atm_vcc *vcc)
160 {
161 	static const char *const map[] = { ATM_VS2TXT_MAP };
162 
163 	return map[ATM_VF2VS(vcc->flags)];
164 }
165 
vcc_info(struct seq_file * seq,struct atm_vcc * vcc)166 static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
167 {
168 	struct sock *sk = sk_atm(vcc);
169 
170 	seq_printf(seq, "%pK ", vcc);
171 	if (!vcc->dev)
172 		seq_printf(seq, "Unassigned    ");
173 	else
174 		seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
175 			vcc->vci);
176 	switch (sk->sk_family) {
177 	case AF_ATMPVC:
178 		seq_printf(seq, "PVC");
179 		break;
180 	case AF_ATMSVC:
181 		seq_printf(seq, "SVC");
182 		break;
183 	default:
184 		seq_printf(seq, "%3d", sk->sk_family);
185 	}
186 	seq_printf(seq, " %04lx  %5d %7d/%7d %7d/%7d [%d]\n",
187 		   vcc->flags, sk->sk_err,
188 		   sk_wmem_alloc_get(sk), sk->sk_sndbuf,
189 		   sk_rmem_alloc_get(sk), sk->sk_rcvbuf,
190 		   refcount_read(&sk->sk_refcnt));
191 }
192 
svc_info(struct seq_file * seq,struct atm_vcc * vcc)193 static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
194 {
195 	if (!vcc->dev)
196 		seq_printf(seq, sizeof(void *) == 4 ?
197 			   "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
198 	else
199 		seq_printf(seq, "%3d %3d %5d         ",
200 			   vcc->dev->number, vcc->vpi, vcc->vci);
201 	seq_printf(seq, "%-10s ", vcc_state(vcc));
202 	seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
203 	    *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
204 	if (*vcc->remote.sas_addr.prv) {
205 		int i;
206 
207 		for (i = 0; i < ATM_ESA_LEN; i++)
208 			seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
209 	}
210 	seq_putc(seq, '\n');
211 }
212 
atm_dev_seq_show(struct seq_file * seq,void * v)213 static int atm_dev_seq_show(struct seq_file *seq, void *v)
214 {
215 	static char atm_dev_banner[] =
216 		"Itf Type    ESI/\"MAC\"addr "
217 		"AAL(TX,err,RX,err,drop) ...               [refcnt]\n";
218 
219 	if (v == &atm_devs)
220 		seq_puts(seq, atm_dev_banner);
221 	else {
222 		struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
223 
224 		atm_dev_info(seq, dev);
225 	}
226 	return 0;
227 }
228 
229 static const struct seq_operations atm_dev_seq_ops = {
230 	.start	= atm_dev_seq_start,
231 	.next	= atm_dev_seq_next,
232 	.stop	= atm_dev_seq_stop,
233 	.show	= atm_dev_seq_show,
234 };
235 
pvc_seq_show(struct seq_file * seq,void * v)236 static int pvc_seq_show(struct seq_file *seq, void *v)
237 {
238 	static char atm_pvc_banner[] =
239 		"Itf VPI VCI   AAL RX(PCR,Class) TX(PCR,Class)\n";
240 
241 	if (v == SEQ_START_TOKEN)
242 		seq_puts(seq, atm_pvc_banner);
243 	else {
244 		struct vcc_state *state = seq->private;
245 		struct atm_vcc *vcc = atm_sk(state->sk);
246 
247 		pvc_info(seq, vcc);
248 	}
249 	return 0;
250 }
251 
252 static const struct seq_operations pvc_seq_ops = {
253 	.start	= vcc_seq_start,
254 	.next	= vcc_seq_next,
255 	.stop	= vcc_seq_stop,
256 	.show	= pvc_seq_show,
257 };
258 
vcc_seq_show(struct seq_file * seq,void * v)259 static int vcc_seq_show(struct seq_file *seq, void *v)
260 {
261 	if (v == SEQ_START_TOKEN) {
262 		seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
263 			"Address ", "Itf VPI VCI   Fam Flags Reply "
264 			"Send buffer     Recv buffer      [refcnt]\n");
265 	} else {
266 		struct vcc_state *state = seq->private;
267 		struct atm_vcc *vcc = atm_sk(state->sk);
268 
269 		vcc_info(seq, vcc);
270 	}
271 	return 0;
272 }
273 
274 static const struct seq_operations vcc_seq_ops = {
275 	.start	= vcc_seq_start,
276 	.next	= vcc_seq_next,
277 	.stop	= vcc_seq_stop,
278 	.show	= vcc_seq_show,
279 };
280 
svc_seq_show(struct seq_file * seq,void * v)281 static int svc_seq_show(struct seq_file *seq, void *v)
282 {
283 	static const char atm_svc_banner[] =
284 		"Itf VPI VCI           State      Remote\n";
285 
286 	if (v == SEQ_START_TOKEN)
287 		seq_puts(seq, atm_svc_banner);
288 	else {
289 		struct vcc_state *state = seq->private;
290 		struct atm_vcc *vcc = atm_sk(state->sk);
291 
292 		svc_info(seq, vcc);
293 	}
294 	return 0;
295 }
296 
297 static const struct seq_operations svc_seq_ops = {
298 	.start	= vcc_seq_start,
299 	.next	= vcc_seq_next,
300 	.stop	= vcc_seq_stop,
301 	.show	= svc_seq_show,
302 };
303 
proc_dev_atm_read(struct file * file,char __user * buf,size_t count,loff_t * pos)304 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
305 				 size_t count, loff_t *pos)
306 {
307 	struct atm_dev *dev;
308 	unsigned long page;
309 	int length;
310 
311 	if (count == 0)
312 		return 0;
313 	page = get_zeroed_page(GFP_KERNEL);
314 	if (!page)
315 		return -ENOMEM;
316 	dev = pde_data(file_inode(file));
317 	if (!dev->ops->proc_read)
318 		length = -EINVAL;
319 	else {
320 		length = dev->ops->proc_read(dev, pos, (char *)page);
321 		if (length > count)
322 			length = -EINVAL;
323 	}
324 	if (length >= 0) {
325 		if (copy_to_user(buf, (char *)page, length))
326 			length = -EFAULT;
327 		(*pos)++;
328 	}
329 	free_page(page);
330 	return length;
331 }
332 
333 struct proc_dir_entry *atm_proc_root;
334 EXPORT_SYMBOL(atm_proc_root);
335 
336 
atm_proc_dev_register(struct atm_dev * dev)337 int atm_proc_dev_register(struct atm_dev *dev)
338 {
339 	int error;
340 
341 	/* No proc info */
342 	if (!dev->ops->proc_read)
343 		return 0;
344 
345 	error = -ENOMEM;
346 	dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
347 	if (!dev->proc_name)
348 		goto err_out;
349 
350 	dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
351 					   &atm_dev_proc_ops, dev);
352 	if (!dev->proc_entry)
353 		goto err_free_name;
354 	return 0;
355 
356 err_free_name:
357 	kfree(dev->proc_name);
358 err_out:
359 	return error;
360 }
361 
atm_proc_dev_deregister(struct atm_dev * dev)362 void atm_proc_dev_deregister(struct atm_dev *dev)
363 {
364 	if (!dev->ops->proc_read)
365 		return;
366 
367 	remove_proc_entry(dev->proc_name, atm_proc_root);
368 	kfree(dev->proc_name);
369 }
370 
atm_proc_init(void)371 int __init atm_proc_init(void)
372 {
373 	atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
374 	if (!atm_proc_root)
375 		return -ENOMEM;
376 	proc_create_seq("devices", 0444, atm_proc_root, &atm_dev_seq_ops);
377 	proc_create_seq_private("pvc", 0444, atm_proc_root, &pvc_seq_ops,
378 			sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMPVC);
379 	proc_create_seq_private("svc", 0444, atm_proc_root, &svc_seq_ops,
380 			sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMSVC);
381 	proc_create_seq_private("vc", 0444, atm_proc_root, &vcc_seq_ops,
382 			sizeof(struct vcc_state), NULL);
383 	return 0;
384 }
385 
atm_proc_exit(void)386 void atm_proc_exit(void)
387 {
388 	remove_proc_subtree("atm", init_net.proc_net);
389 }
390