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
6 * seq_file api usage by romieu@fr.zoreil.com
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
= {
40 .read
= proc_dev_atm_read
,
41 .llseek
= noop_llseek
,
44 static void add_stats(struct seq_file
*seq
, const char *aal
,
45 const struct k_atm_aal_stats
*stats
)
47 seq_printf(seq
, "%s ( %d %d %d %d %d )", aal
,
48 atomic_read(&stats
->tx
), atomic_read(&stats
->tx_err
),
49 atomic_read(&stats
->rx
), atomic_read(&stats
->rx_err
),
50 atomic_read(&stats
->rx_drop
));
53 static void atm_dev_info(struct seq_file
*seq
, const struct atm_dev
*dev
)
57 seq_printf(seq
, "%3d %-8s", dev
->number
, dev
->type
);
58 for (i
= 0; i
< ESI_LEN
; i
++)
59 seq_printf(seq
, "%02x", dev
->esi
[i
]);
61 add_stats(seq
, "0", &dev
->stats
.aal0
);
63 add_stats(seq
, "5", &dev
->stats
.aal5
);
64 seq_printf(seq
, "\t[%d]", refcount_read(&dev
->refcnt
));
74 static inline int compare_family(struct sock
*sk
, int family
)
76 return !family
|| (sk
->sk_family
== family
);
79 static int __vcc_walk(struct sock
**sock
, int family
, int *bucket
, loff_t l
)
81 struct sock
*sk
= *sock
;
83 if (sk
== SEQ_START_TOKEN
) {
84 for (*bucket
= 0; *bucket
< VCC_HTABLE_SIZE
; ++*bucket
) {
85 struct hlist_head
*head
= &vcc_hash
[*bucket
];
87 sk
= hlist_empty(head
) ? NULL
: __sk_head(head
);
94 for (; sk
; sk
= sk_next(sk
)) {
95 l
-= compare_family(sk
, family
);
99 if (!sk
&& ++*bucket
< VCC_HTABLE_SIZE
) {
100 sk
= sk_head(&vcc_hash
[*bucket
]);
103 sk
= SEQ_START_TOKEN
;
109 static inline void *vcc_walk(struct vcc_state
*state
, loff_t l
)
111 return __vcc_walk(&state
->sk
, state
->family
, &state
->bucket
, l
) ?
115 static int __vcc_seq_open(struct inode
*inode
, struct file
*file
,
116 int family
, const struct seq_operations
*ops
)
118 struct vcc_state
*state
;
120 state
= __seq_open_private(file
, ops
, sizeof(*state
));
124 state
->family
= family
;
128 static void *vcc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
129 __acquires(vcc_sklist_lock
)
131 struct vcc_state
*state
= seq
->private;
134 read_lock(&vcc_sklist_lock
);
135 state
->sk
= SEQ_START_TOKEN
;
136 return left
? vcc_walk(state
, left
) : SEQ_START_TOKEN
;
139 static void vcc_seq_stop(struct seq_file
*seq
, void *v
)
140 __releases(vcc_sklist_lock
)
142 read_unlock(&vcc_sklist_lock
);
145 static void *vcc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
147 struct vcc_state
*state
= seq
->private;
149 v
= vcc_walk(state
, 1);
150 *pos
+= !!PTR_ERR(v
);
154 static void pvc_info(struct seq_file
*seq
, struct atm_vcc
*vcc
)
156 static const char *const class_name
[] = {
157 "off", "UBR", "CBR", "VBR", "ABR"};
158 static const char *const aal_name
[] = {
159 "---", "1", "2", "3/4", /* 0- 3 */
160 "???", "5", "???", "???", /* 4- 7 */
161 "???", "???", "???", "???", /* 8-11 */
162 "???", "0", "???", "???"}; /* 12-15 */
164 seq_printf(seq
, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
165 vcc
->dev
->number
, vcc
->vpi
, vcc
->vci
,
166 vcc
->qos
.aal
>= ARRAY_SIZE(aal_name
) ? "err" :
167 aal_name
[vcc
->qos
.aal
], vcc
->qos
.rxtp
.min_pcr
,
168 class_name
[vcc
->qos
.rxtp
.traffic_class
],
169 vcc
->qos
.txtp
.min_pcr
,
170 class_name
[vcc
->qos
.txtp
.traffic_class
]);
171 if (test_bit(ATM_VF_IS_CLIP
, &vcc
->flags
)) {
172 struct clip_vcc
*clip_vcc
= CLIP_VCC(vcc
);
173 struct net_device
*dev
;
175 dev
= clip_vcc
->entry
? clip_vcc
->entry
->neigh
->dev
: NULL
;
176 seq_printf(seq
, "CLIP, Itf:%s, Encap:",
177 dev
? dev
->name
: "none?");
178 seq_printf(seq
, "%s", clip_vcc
->encap
? "LLC/SNAP" : "None");
183 static const char *vcc_state(struct atm_vcc
*vcc
)
185 static const char *const map
[] = { ATM_VS2TXT_MAP
};
187 return map
[ATM_VF2VS(vcc
->flags
)];
190 static void vcc_info(struct seq_file
*seq
, struct atm_vcc
*vcc
)
192 struct sock
*sk
= sk_atm(vcc
);
194 seq_printf(seq
, "%pK ", vcc
);
196 seq_printf(seq
, "Unassigned ");
198 seq_printf(seq
, "%3d %3d %5d ", vcc
->dev
->number
, vcc
->vpi
,
200 switch (sk
->sk_family
) {
202 seq_printf(seq
, "PVC");
205 seq_printf(seq
, "SVC");
208 seq_printf(seq
, "%3d", sk
->sk_family
);
210 seq_printf(seq
, " %04lx %5d %7d/%7d %7d/%7d [%d]\n",
211 vcc
->flags
, sk
->sk_err
,
212 sk_wmem_alloc_get(sk
), sk
->sk_sndbuf
,
213 sk_rmem_alloc_get(sk
), sk
->sk_rcvbuf
,
214 refcount_read(&sk
->sk_refcnt
));
217 static void svc_info(struct seq_file
*seq
, struct atm_vcc
*vcc
)
220 seq_printf(seq
, sizeof(void *) == 4 ?
221 "N/A@%pK%10s" : "N/A@%pK%2s", vcc
, "");
223 seq_printf(seq
, "%3d %3d %5d ",
224 vcc
->dev
->number
, vcc
->vpi
, vcc
->vci
);
225 seq_printf(seq
, "%-10s ", vcc_state(vcc
));
226 seq_printf(seq
, "%s%s", vcc
->remote
.sas_addr
.pub
,
227 *vcc
->remote
.sas_addr
.pub
&& *vcc
->remote
.sas_addr
.prv
? "+" : "");
228 if (*vcc
->remote
.sas_addr
.prv
) {
231 for (i
= 0; i
< ATM_ESA_LEN
; i
++)
232 seq_printf(seq
, "%02x", vcc
->remote
.sas_addr
.prv
[i
]);
237 static int atm_dev_seq_show(struct seq_file
*seq
, void *v
)
239 static char atm_dev_banner
[] =
240 "Itf Type ESI/\"MAC\"addr "
241 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
244 seq_puts(seq
, atm_dev_banner
);
246 struct atm_dev
*dev
= list_entry(v
, struct atm_dev
, dev_list
);
248 atm_dev_info(seq
, dev
);
253 static const struct seq_operations atm_dev_seq_ops
= {
254 .start
= atm_dev_seq_start
,
255 .next
= atm_dev_seq_next
,
256 .stop
= atm_dev_seq_stop
,
257 .show
= atm_dev_seq_show
,
260 static int atm_dev_seq_open(struct inode
*inode
, struct file
*file
)
262 return seq_open(file
, &atm_dev_seq_ops
);
265 static const struct file_operations devices_seq_fops
= {
266 .open
= atm_dev_seq_open
,
269 .release
= seq_release
,
272 static int pvc_seq_show(struct seq_file
*seq
, void *v
)
274 static char atm_pvc_banner
[] =
275 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
277 if (v
== SEQ_START_TOKEN
)
278 seq_puts(seq
, atm_pvc_banner
);
280 struct vcc_state
*state
= seq
->private;
281 struct atm_vcc
*vcc
= atm_sk(state
->sk
);
288 static const struct seq_operations pvc_seq_ops
= {
289 .start
= vcc_seq_start
,
290 .next
= vcc_seq_next
,
291 .stop
= vcc_seq_stop
,
292 .show
= pvc_seq_show
,
295 static int pvc_seq_open(struct inode
*inode
, struct file
*file
)
297 return __vcc_seq_open(inode
, file
, PF_ATMPVC
, &pvc_seq_ops
);
300 static const struct file_operations pvc_seq_fops
= {
301 .open
= pvc_seq_open
,
304 .release
= seq_release_private
,
307 static int vcc_seq_show(struct seq_file
*seq
, void *v
)
309 if (v
== SEQ_START_TOKEN
) {
310 seq_printf(seq
, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
311 "Address ", "Itf VPI VCI Fam Flags Reply "
312 "Send buffer Recv buffer [refcnt]\n");
314 struct vcc_state
*state
= seq
->private;
315 struct atm_vcc
*vcc
= atm_sk(state
->sk
);
322 static const struct seq_operations vcc_seq_ops
= {
323 .start
= vcc_seq_start
,
324 .next
= vcc_seq_next
,
325 .stop
= vcc_seq_stop
,
326 .show
= vcc_seq_show
,
329 static int vcc_seq_open(struct inode
*inode
, struct file
*file
)
331 return __vcc_seq_open(inode
, file
, 0, &vcc_seq_ops
);
334 static const struct file_operations vcc_seq_fops
= {
335 .open
= vcc_seq_open
,
338 .release
= seq_release_private
,
341 static int svc_seq_show(struct seq_file
*seq
, void *v
)
343 static const char atm_svc_banner
[] =
344 "Itf VPI VCI State Remote\n";
346 if (v
== SEQ_START_TOKEN
)
347 seq_puts(seq
, atm_svc_banner
);
349 struct vcc_state
*state
= seq
->private;
350 struct atm_vcc
*vcc
= atm_sk(state
->sk
);
357 static const struct seq_operations svc_seq_ops
= {
358 .start
= vcc_seq_start
,
359 .next
= vcc_seq_next
,
360 .stop
= vcc_seq_stop
,
361 .show
= svc_seq_show
,
364 static int svc_seq_open(struct inode
*inode
, struct file
*file
)
366 return __vcc_seq_open(inode
, file
, PF_ATMSVC
, &svc_seq_ops
);
369 static const struct file_operations svc_seq_fops
= {
370 .open
= svc_seq_open
,
373 .release
= seq_release_private
,
376 static ssize_t
proc_dev_atm_read(struct file
*file
, char __user
*buf
,
377 size_t count
, loff_t
*pos
)
385 page
= get_zeroed_page(GFP_KERNEL
);
388 dev
= PDE_DATA(file_inode(file
));
389 if (!dev
->ops
->proc_read
)
392 length
= dev
->ops
->proc_read(dev
, pos
, (char *)page
);
397 if (copy_to_user(buf
, (char *)page
, length
))
405 struct proc_dir_entry
*atm_proc_root
;
406 EXPORT_SYMBOL(atm_proc_root
);
409 int atm_proc_dev_register(struct atm_dev
*dev
)
414 if (!dev
->ops
->proc_read
)
418 dev
->proc_name
= kasprintf(GFP_KERNEL
, "%s:%d", dev
->type
, dev
->number
);
422 dev
->proc_entry
= proc_create_data(dev
->proc_name
, 0, atm_proc_root
,
423 &proc_atm_dev_ops
, dev
);
424 if (!dev
->proc_entry
)
429 kfree(dev
->proc_name
);
434 void atm_proc_dev_deregister(struct atm_dev
*dev
)
436 if (!dev
->ops
->proc_read
)
439 remove_proc_entry(dev
->proc_name
, atm_proc_root
);
440 kfree(dev
->proc_name
);
443 static struct atm_proc_entry
{
445 const struct file_operations
*proc_fops
;
446 struct proc_dir_entry
*dirent
;
447 } atm_proc_ents
[] = {
448 { .name
= "devices", .proc_fops
= &devices_seq_fops
},
449 { .name
= "pvc", .proc_fops
= &pvc_seq_fops
},
450 { .name
= "svc", .proc_fops
= &svc_seq_fops
},
451 { .name
= "vc", .proc_fops
= &vcc_seq_fops
},
452 { .name
= NULL
, .proc_fops
= NULL
}
455 static void atm_proc_dirs_remove(void)
457 static struct atm_proc_entry
*e
;
459 for (e
= atm_proc_ents
; e
->name
; e
++) {
461 remove_proc_entry(e
->name
, atm_proc_root
);
463 remove_proc_entry("atm", init_net
.proc_net
);
466 int __init
atm_proc_init(void)
468 static struct atm_proc_entry
*e
;
471 atm_proc_root
= proc_net_mkdir(&init_net
, "atm", init_net
.proc_net
);
474 for (e
= atm_proc_ents
; e
->name
; e
++) {
475 struct proc_dir_entry
*dirent
;
477 dirent
= proc_create(e
->name
, S_IRUGO
,
478 atm_proc_root
, e
->proc_fops
);
488 atm_proc_dirs_remove();
494 void atm_proc_exit(void)
496 atm_proc_dirs_remove();