1 // SPDX-License-Identifier: GPL-2.0
2 /* net/atm/proc.c - ATM /proc interface
4 * Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA
8 * Evaluating the efficiency of the whole thing if left as an exercise to
12 #include <linux/module.h> /* for EXPORT_SYMBOL */
13 #include <linux/string.h>
14 #include <linux/types.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/atmclip.h>
25 #include <linux/init.h> /* for __init */
26 #include <linux/slab.h>
27 #include <net/net_namespace.h>
28 #include <net/atmclip.h>
29 #include <linux/uaccess.h>
30 #include <linux/param.h> /* for HZ */
31 #include <linux/atomic.h>
32 #include "resources.h"
33 #include "common.h" /* atm_proc_init prototype */
34 #include "signaling.h" /* to get sigd - ugly too */
36 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
37 size_t count, loff_t *pos);
39 static const struct file_operations proc_atm_dev_ops = {
41 .read = proc_dev_atm_read,
42 .llseek = noop_llseek,
45 static void add_stats(struct seq_file *seq, const char *aal,
46 const struct k_atm_aal_stats *stats)
48 seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
49 atomic_read(&stats->tx), atomic_read(&stats->tx_err),
50 atomic_read(&stats->rx), atomic_read(&stats->rx_err),
51 atomic_read(&stats->rx_drop));
54 static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
58 seq_printf(seq, "%3d %-8s", dev->number, dev->type);
59 for (i = 0; i < ESI_LEN; i++)
60 seq_printf(seq, "%02x", dev->esi[i]);
62 add_stats(seq, "0", &dev->stats.aal0);
64 add_stats(seq, "5", &dev->stats.aal5);
65 seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt));
75 static inline int compare_family(struct sock *sk, int family)
77 return !family || (sk->sk_family == family);
80 static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
82 struct sock *sk = *sock;
84 if (sk == SEQ_START_TOKEN) {
85 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
86 struct hlist_head *head = &vcc_hash[*bucket];
88 sk = hlist_empty(head) ? NULL : __sk_head(head);
95 for (; sk; sk = sk_next(sk)) {
96 l -= compare_family(sk, family);
100 if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
101 sk = sk_head(&vcc_hash[*bucket]);
104 sk = SEQ_START_TOKEN;
110 static inline void *vcc_walk(struct vcc_state *state, loff_t l)
112 return __vcc_walk(&state->sk, state->family, &state->bucket, l) ?
116 static int __vcc_seq_open(struct inode *inode, struct file *file,
117 int family, const struct seq_operations *ops)
119 struct vcc_state *state;
121 state = __seq_open_private(file, ops, sizeof(*state));
125 state->family = family;
129 static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
130 __acquires(vcc_sklist_lock)
132 struct vcc_state *state = seq->private;
135 read_lock(&vcc_sklist_lock);
136 state->sk = SEQ_START_TOKEN;
137 return left ? vcc_walk(state, left) : SEQ_START_TOKEN;
140 static void vcc_seq_stop(struct seq_file *seq, void *v)
141 __releases(vcc_sklist_lock)
143 read_unlock(&vcc_sklist_lock);
146 static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
148 struct vcc_state *state = seq->private;
150 v = vcc_walk(state, 1);
151 *pos += !!PTR_ERR(v);
155 static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
157 static const char *const class_name[] = {
158 "off", "UBR", "CBR", "VBR", "ABR"};
159 static const char *const aal_name[] = {
160 "---", "1", "2", "3/4", /* 0- 3 */
161 "???", "5", "???", "???", /* 4- 7 */
162 "???", "???", "???", "???", /* 8-11 */
163 "???", "0", "???", "???"}; /* 12-15 */
165 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
166 vcc->dev->number, vcc->vpi, vcc->vci,
167 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
168 aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr,
169 class_name[vcc->qos.rxtp.traffic_class],
170 vcc->qos.txtp.min_pcr,
171 class_name[vcc->qos.txtp.traffic_class]);
172 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
173 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
174 struct net_device *dev;
176 dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
177 seq_printf(seq, "CLIP, Itf:%s, Encap:",
178 dev ? dev->name : "none?");
179 seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
184 static const char *vcc_state(struct atm_vcc *vcc)
186 static const char *const map[] = { ATM_VS2TXT_MAP };
188 return map[ATM_VF2VS(vcc->flags)];
191 static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
193 struct sock *sk = sk_atm(vcc);
195 seq_printf(seq, "%pK ", vcc);
197 seq_printf(seq, "Unassigned ");
199 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
201 switch (sk->sk_family) {
203 seq_printf(seq, "PVC");
206 seq_printf(seq, "SVC");
209 seq_printf(seq, "%3d", sk->sk_family);
211 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n",
212 vcc->flags, sk->sk_err,
213 sk_wmem_alloc_get(sk), sk->sk_sndbuf,
214 sk_rmem_alloc_get(sk), sk->sk_rcvbuf,
215 refcount_read(&sk->sk_refcnt));
218 static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
221 seq_printf(seq, sizeof(void *) == 4 ?
222 "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
224 seq_printf(seq, "%3d %3d %5d ",
225 vcc->dev->number, vcc->vpi, vcc->vci);
226 seq_printf(seq, "%-10s ", vcc_state(vcc));
227 seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
228 *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
229 if (*vcc->remote.sas_addr.prv) {
232 for (i = 0; i < ATM_ESA_LEN; i++)
233 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
238 static int atm_dev_seq_show(struct seq_file *seq, void *v)
240 static char atm_dev_banner[] =
241 "Itf Type ESI/\"MAC\"addr "
242 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
245 seq_puts(seq, atm_dev_banner);
247 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
249 atm_dev_info(seq, dev);
254 static const struct seq_operations atm_dev_seq_ops = {
255 .start = atm_dev_seq_start,
256 .next = atm_dev_seq_next,
257 .stop = atm_dev_seq_stop,
258 .show = atm_dev_seq_show,
261 static int atm_dev_seq_open(struct inode *inode, struct file *file)
263 return seq_open(file, &atm_dev_seq_ops);
266 static const struct file_operations devices_seq_fops = {
267 .open = atm_dev_seq_open,
270 .release = seq_release,
273 static int pvc_seq_show(struct seq_file *seq, void *v)
275 static char atm_pvc_banner[] =
276 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
278 if (v == SEQ_START_TOKEN)
279 seq_puts(seq, atm_pvc_banner);
281 struct vcc_state *state = seq->private;
282 struct atm_vcc *vcc = atm_sk(state->sk);
289 static const struct seq_operations pvc_seq_ops = {
290 .start = vcc_seq_start,
291 .next = vcc_seq_next,
292 .stop = vcc_seq_stop,
293 .show = pvc_seq_show,
296 static int pvc_seq_open(struct inode *inode, struct file *file)
298 return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops);
301 static const struct file_operations pvc_seq_fops = {
302 .open = pvc_seq_open,
305 .release = seq_release_private,
308 static int vcc_seq_show(struct seq_file *seq, void *v)
310 if (v == SEQ_START_TOKEN) {
311 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
312 "Address ", "Itf VPI VCI Fam Flags Reply "
313 "Send buffer Recv buffer [refcnt]\n");
315 struct vcc_state *state = seq->private;
316 struct atm_vcc *vcc = atm_sk(state->sk);
323 static const struct seq_operations vcc_seq_ops = {
324 .start = vcc_seq_start,
325 .next = vcc_seq_next,
326 .stop = vcc_seq_stop,
327 .show = vcc_seq_show,
330 static int vcc_seq_open(struct inode *inode, struct file *file)
332 return __vcc_seq_open(inode, file, 0, &vcc_seq_ops);
335 static const struct file_operations vcc_seq_fops = {
336 .open = vcc_seq_open,
339 .release = seq_release_private,
342 static int svc_seq_show(struct seq_file *seq, void *v)
344 static const char atm_svc_banner[] =
345 "Itf VPI VCI State Remote\n";
347 if (v == SEQ_START_TOKEN)
348 seq_puts(seq, atm_svc_banner);
350 struct vcc_state *state = seq->private;
351 struct atm_vcc *vcc = atm_sk(state->sk);
358 static const struct seq_operations svc_seq_ops = {
359 .start = vcc_seq_start,
360 .next = vcc_seq_next,
361 .stop = vcc_seq_stop,
362 .show = svc_seq_show,
365 static int svc_seq_open(struct inode *inode, struct file *file)
367 return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops);
370 static const struct file_operations svc_seq_fops = {
371 .open = svc_seq_open,
374 .release = seq_release_private,
377 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
378 size_t count, loff_t *pos)
386 page = get_zeroed_page(GFP_KERNEL);
389 dev = PDE_DATA(file_inode(file));
390 if (!dev->ops->proc_read)
393 length = dev->ops->proc_read(dev, pos, (char *)page);
398 if (copy_to_user(buf, (char *)page, length))
406 struct proc_dir_entry *atm_proc_root;
407 EXPORT_SYMBOL(atm_proc_root);
410 int atm_proc_dev_register(struct atm_dev *dev)
415 if (!dev->ops->proc_read)
419 dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
423 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
424 &proc_atm_dev_ops, dev);
425 if (!dev->proc_entry)
430 kfree(dev->proc_name);
435 void atm_proc_dev_deregister(struct atm_dev *dev)
437 if (!dev->ops->proc_read)
440 remove_proc_entry(dev->proc_name, atm_proc_root);
441 kfree(dev->proc_name);
444 static struct atm_proc_entry {
446 const struct file_operations *proc_fops;
447 struct proc_dir_entry *dirent;
448 } atm_proc_ents[] = {
449 { .name = "devices", .proc_fops = &devices_seq_fops },
450 { .name = "pvc", .proc_fops = &pvc_seq_fops },
451 { .name = "svc", .proc_fops = &svc_seq_fops },
452 { .name = "vc", .proc_fops = &vcc_seq_fops },
453 { .name = NULL, .proc_fops = NULL }
456 static void atm_proc_dirs_remove(void)
458 static struct atm_proc_entry *e;
460 for (e = atm_proc_ents; e->name; e++) {
462 remove_proc_entry(e->name, atm_proc_root);
464 remove_proc_entry("atm", init_net.proc_net);
467 int __init atm_proc_init(void)
469 static struct atm_proc_entry *e;
472 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
475 for (e = atm_proc_ents; e->name; e++) {
476 struct proc_dir_entry *dirent;
478 dirent = proc_create(e->name, S_IRUGO,
479 atm_proc_root, e->proc_fops);
489 atm_proc_dirs_remove();
495 void atm_proc_exit(void)
497 atm_proc_dirs_remove();