blob: d79221fd4dae2e8b13ac0725899a1b26a559db44 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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/atmclip.h>
25#include <linux/init.h> /* for __init */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020027#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/atmclip.h>
Joe Perches07367ad2010-01-26 11:40:13 +000029#include <linux/uaccess.h>
30#include <linux/param.h> /* for HZ */
Arun Sharma600634972011-07-26 16:09:06 -070031#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "resources.h"
33#include "common.h" /* atm_proc_init prototype */
34#include "signaling.h" /* to get sigd - ugly too */
35
Joe Perches07367ad2010-01-26 11:40:13 +000036static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
37 size_t count, loff_t *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Arjan van de Ven9a321442007-02-12 00:55:35 -080039static const struct file_operations proc_atm_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .read = proc_dev_atm_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +020041 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44static void add_stats(struct seq_file *seq, const char *aal,
45 const struct k_atm_aal_stats *stats)
46{
47 seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
Joe Perches07367ad2010-01-26 11:40:13 +000048 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
54{
55 int i;
56
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]);
60 seq_puts(seq, " ");
61 add_stats(seq, "0", &dev->stats.aal0);
62 seq_puts(seq, " ");
63 add_stats(seq, "5", &dev->stats.aal5);
Reshetova, Elena458bc302017-07-04 15:53:01 +030064 seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 seq_putc(seq, '\n');
66}
67
68struct vcc_state {
69 int bucket;
70 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73static inline int compare_family(struct sock *sk, int family)
74{
75 return !family || (sk->sk_family == family);
76}
77
78static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
79{
80 struct sock *sk = *sock;
81
Joe Perches2e1e9842008-04-10 03:33:03 -070082 if (sk == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
84 struct hlist_head *head = &vcc_hash[*bucket];
85
86 sk = hlist_empty(head) ? NULL : __sk_head(head);
87 if (sk)
88 break;
89 }
90 l--;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092try_again:
93 for (; sk; sk = sk_next(sk)) {
94 l -= compare_family(sk, family);
95 if (l < 0)
96 goto out;
97 }
98 if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
99 sk = sk_head(&vcc_hash[*bucket]);
100 goto try_again;
101 }
Joe Perches2e1e9842008-04-10 03:33:03 -0700102 sk = SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103out:
104 *sock = sk;
105 return (l < 0);
106}
107
Christoph Hellwiga2d03aa2018-04-18 06:09:21 +0200108static inline void *vcc_walk(struct seq_file *seq, loff_t l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Christoph Hellwiga2d03aa2018-04-18 06:09:21 +0200110 struct vcc_state *state = seq->private;
111 int family = (uintptr_t)(PDE_DATA(file_inode(seq->file)));
112
113 return __vcc_walk(&state->sk, family, &state->bucket, l) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 state : NULL;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800118 __acquires(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct vcc_state *state = seq->private;
121 loff_t left = *pos;
122
123 read_lock(&vcc_sklist_lock);
Joe Perches2e1e9842008-04-10 03:33:03 -0700124 state->sk = SEQ_START_TOKEN;
Christoph Hellwiga2d03aa2018-04-18 06:09:21 +0200125 return left ? vcc_walk(seq, left) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void vcc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800129 __releases(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 read_unlock(&vcc_sklist_lock);
132}
133
134static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
135{
Christoph Hellwiga2d03aa2018-04-18 06:09:21 +0200136 v = vcc_walk(seq, 1);
Dan Carpenterd0edde82019-02-14 09:56:35 +0300137 if (v)
138 (*pos)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return v;
140}
141
142static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
143{
Joe Perches07367ad2010-01-26 11:40:13 +0000144 static const char *const class_name[] = {
145 "off", "UBR", "CBR", "VBR", "ABR"};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700146 static const char *const aal_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 "---", "1", "2", "3/4", /* 0- 3 */
148 "???", "5", "???", "???", /* 4- 7 */
149 "???", "???", "???", "???", /* 8-11 */
150 "???", "0", "???", "???"}; /* 12-15 */
151
152 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
Joe Perches07367ad2010-01-26 11:40:13 +0000153 vcc->dev->number, vcc->vpi, vcc->vci,
154 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
155 aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr,
156 class_name[vcc->qos.rxtp.traffic_class],
157 vcc->qos.txtp.min_pcr,
158 class_name[vcc->qos.txtp.traffic_class]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
160 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
161 struct net_device *dev;
162
163 dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
164 seq_printf(seq, "CLIP, Itf:%s, Encap:",
165 dev ? dev->name : "none?");
166 seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
167 }
168 seq_putc(seq, '\n');
169}
170
171static const char *vcc_state(struct atm_vcc *vcc)
172{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700173 static const char *const map[] = { ATM_VS2TXT_MAP };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return map[ATM_VF2VS(vcc->flags)];
176}
177
178static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
179{
180 struct sock *sk = sk_atm(vcc);
181
Dan Rosenberg71338aa2011-05-23 12:17:35 +0000182 seq_printf(seq, "%pK ", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!vcc->dev)
184 seq_printf(seq, "Unassigned ");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900185 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
187 vcc->vci);
188 switch (sk->sk_family) {
Joe Perches07367ad2010-01-26 11:40:13 +0000189 case AF_ATMPVC:
190 seq_printf(seq, "PVC");
191 break;
192 case AF_ATMSVC:
193 seq_printf(seq, "SVC");
194 break;
195 default:
196 seq_printf(seq, "%3d", sk->sk_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Joe Perches07367ad2010-01-26 11:40:13 +0000198 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n",
199 vcc->flags, sk->sk_err,
200 sk_wmem_alloc_get(sk), sk->sk_sndbuf,
201 sk_rmem_alloc_get(sk), sk->sk_rcvbuf,
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300202 refcount_read(&sk->sk_refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
206{
207 if (!vcc->dev)
208 seq_printf(seq, sizeof(void *) == 4 ?
Dan Rosenberg71338aa2011-05-23 12:17:35 +0000209 "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 else
211 seq_printf(seq, "%3d %3d %5d ",
212 vcc->dev->number, vcc->vpi, vcc->vci);
213 seq_printf(seq, "%-10s ", vcc_state(vcc));
214 seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
215 *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
216 if (*vcc->remote.sas_addr.prv) {
217 int i;
218
219 for (i = 0; i < ATM_ESA_LEN; i++)
220 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
221 }
222 seq_putc(seq, '\n');
223}
224
225static int atm_dev_seq_show(struct seq_file *seq, void *v)
226{
227 static char atm_dev_banner[] =
228 "Itf Type ESI/\"MAC\"addr "
229 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900230
Li Zefan67de7922010-02-08 23:21:51 +0000231 if (v == &atm_devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 seq_puts(seq, atm_dev_banner);
233 else {
234 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
235
236 atm_dev_info(seq, dev);
237 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900240
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700241static const struct seq_operations atm_dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .start = atm_dev_seq_start,
243 .next = atm_dev_seq_next,
244 .stop = atm_dev_seq_stop,
245 .show = atm_dev_seq_show,
246};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static int pvc_seq_show(struct seq_file *seq, void *v)
249{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900250 static char atm_pvc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
252
Joe Perches2e1e9842008-04-10 03:33:03 -0700253 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 seq_puts(seq, atm_pvc_banner);
255 else {
256 struct vcc_state *state = seq->private;
257 struct atm_vcc *vcc = atm_sk(state->sk);
258
259 pvc_info(seq, vcc);
260 }
261 return 0;
262}
263
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700264static const struct seq_operations pvc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .start = vcc_seq_start,
266 .next = vcc_seq_next,
267 .stop = vcc_seq_stop,
268 .show = pvc_seq_show,
269};
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int vcc_seq_show(struct seq_file *seq, void *v)
272{
Joe Perches2e1e9842008-04-10 03:33:03 -0700273 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900274 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
275 "Address ", "Itf VPI VCI Fam Flags Reply "
276 "Send buffer Recv buffer [refcnt]\n");
277 } else {
278 struct vcc_state *state = seq->private;
279 struct atm_vcc *vcc = atm_sk(state->sk);
280
281 vcc_info(seq, vcc);
282 }
283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900285
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700286static const struct seq_operations vcc_seq_ops = {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900287 .start = vcc_seq_start,
288 .next = vcc_seq_next,
289 .stop = vcc_seq_stop,
290 .show = vcc_seq_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int svc_seq_show(struct seq_file *seq, void *v)
294{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700295 static const char atm_svc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 "Itf VPI VCI State Remote\n";
297
Joe Perches2e1e9842008-04-10 03:33:03 -0700298 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 seq_puts(seq, atm_svc_banner);
300 else {
301 struct vcc_state *state = seq->private;
302 struct atm_vcc *vcc = atm_sk(state->sk);
303
304 svc_info(seq, vcc);
305 }
306 return 0;
307}
308
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700309static const struct seq_operations svc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .start = vcc_seq_start,
311 .next = vcc_seq_next,
312 .stop = vcc_seq_stop,
313 .show = svc_seq_show,
314};
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
317 size_t count, loff_t *pos)
318{
319 struct atm_dev *dev;
320 unsigned long page;
321 int length;
322
Joe Perches07367ad2010-01-26 11:40:13 +0000323 if (count == 0)
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 page = get_zeroed_page(GFP_KERNEL);
Joe Perches07367ad2010-01-26 11:40:13 +0000326 if (!page)
327 return -ENOMEM;
Al Virod9dda782013-03-31 18:16:14 -0400328 dev = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!dev->ops->proc_read)
330 length = -EINVAL;
331 else {
Joe Perches07367ad2010-01-26 11:40:13 +0000332 length = dev->ops->proc_read(dev, pos, (char *)page);
333 if (length > count)
334 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 if (length >= 0) {
Joe Perches07367ad2010-01-26 11:40:13 +0000337 if (copy_to_user(buf, (char *)page, length))
338 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 (*pos)++;
340 }
341 free_page(page);
342 return length;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345struct proc_dir_entry *atm_proc_root;
346EXPORT_SYMBOL(atm_proc_root);
347
348
349int atm_proc_dev_register(struct atm_dev *dev)
350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int error;
352
353 /* No proc info */
354 if (!dev->ops->proc_read)
355 return 0;
356
357 error = -ENOMEM;
Eric Dumazet62c97ac2010-03-18 13:48:26 +0000358 dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!dev->proc_name)
360 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Denis V. Lunev0c896522008-05-02 04:08:30 -0700362 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
363 &proc_atm_dev_ops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!dev->proc_entry)
365 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
Joe Perches07367ad2010-01-26 11:40:13 +0000367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368err_free_name:
369 kfree(dev->proc_name);
370err_out:
371 return error;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374void atm_proc_dev_deregister(struct atm_dev *dev)
375{
376 if (!dev->ops->proc_read)
377 return;
378
379 remove_proc_entry(dev->proc_name, atm_proc_root);
380 kfree(dev->proc_name);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383int __init atm_proc_init(void)
384{
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800385 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!atm_proc_root)
Christoph Hellwigce9fe432018-04-15 10:53:36 +0200387 return -ENOMEM;
388 proc_create_seq("devices", 0444, atm_proc_root, &atm_dev_seq_ops);
Christoph Hellwiga2d03aa2018-04-18 06:09:21 +0200389 proc_create_seq_private("pvc", 0444, atm_proc_root, &pvc_seq_ops,
390 sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMPVC);
391 proc_create_seq_private("svc", 0444, atm_proc_root, &svc_seq_ops,
392 sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMSVC);
393 proc_create_seq_private("vc", 0444, atm_proc_root, &vcc_seq_ops,
394 sizeof(struct vcc_state), NULL);
Christoph Hellwigce9fe432018-04-15 10:53:36 +0200395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Kevin Hilmanb9c6e3e2006-08-15 02:02:33 -0700398void atm_proc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Christoph Hellwigce9fe432018-04-15 10:53:36 +0200400 remove_proc_subtree("atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}