blob: f272b0f59d82f4ff47fec0d1aac100979a244a51 [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;
71 int family;
72};
73
74static inline int compare_family(struct sock *sk, int family)
75{
76 return !family || (sk->sk_family == family);
77}
78
79static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
80{
81 struct sock *sk = *sock;
82
Joe Perches2e1e9842008-04-10 03:33:03 -070083 if (sk == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
85 struct hlist_head *head = &vcc_hash[*bucket];
86
87 sk = hlist_empty(head) ? NULL : __sk_head(head);
88 if (sk)
89 break;
90 }
91 l--;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093try_again:
94 for (; sk; sk = sk_next(sk)) {
95 l -= compare_family(sk, family);
96 if (l < 0)
97 goto out;
98 }
99 if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
100 sk = sk_head(&vcc_hash[*bucket]);
101 goto try_again;
102 }
Joe Perches2e1e9842008-04-10 03:33:03 -0700103 sk = SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104out:
105 *sock = sk;
106 return (l < 0);
107}
108
109static inline void *vcc_walk(struct vcc_state *state, loff_t l)
110{
111 return __vcc_walk(&state->sk, state->family, &state->bucket, l) ?
112 state : NULL;
113}
114
115static int __vcc_seq_open(struct inode *inode, struct file *file,
Al Viroececfde2007-07-15 21:01:12 +0100116 int family, const struct seq_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct vcc_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800120 state = __seq_open_private(file, ops, sizeof(*state));
121 if (state == NULL)
122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 state->family = family;
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800129 __acquires(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct vcc_state *state = seq->private;
132 loff_t left = *pos;
133
134 read_lock(&vcc_sklist_lock);
Joe Perches2e1e9842008-04-10 03:33:03 -0700135 state->sk = SEQ_START_TOKEN;
136 return left ? vcc_walk(state, left) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static void vcc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800140 __releases(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 read_unlock(&vcc_sklist_lock);
143}
144
145static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
146{
147 struct vcc_state *state = seq->private;
148
149 v = vcc_walk(state, 1);
150 *pos += !!PTR_ERR(v);
151 return v;
152}
153
154static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
155{
Joe Perches07367ad2010-01-26 11:40:13 +0000156 static const char *const class_name[] = {
157 "off", "UBR", "CBR", "VBR", "ABR"};
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700158 static const char *const aal_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 "---", "1", "2", "3/4", /* 0- 3 */
160 "???", "5", "???", "???", /* 4- 7 */
161 "???", "???", "???", "???", /* 8-11 */
162 "???", "0", "???", "???"}; /* 12-15 */
163
164 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
Joe Perches07367ad2010-01-26 11:40:13 +0000165 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
172 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
173 struct net_device *dev;
174
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");
179 }
180 seq_putc(seq, '\n');
181}
182
183static const char *vcc_state(struct atm_vcc *vcc)
184{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700185 static const char *const map[] = { ATM_VS2TXT_MAP };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 return map[ATM_VF2VS(vcc->flags)];
188}
189
190static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
191{
192 struct sock *sk = sk_atm(vcc);
193
Dan Rosenberg71338aa2011-05-23 12:17:35 +0000194 seq_printf(seq, "%pK ", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!vcc->dev)
196 seq_printf(seq, "Unassigned ");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900197 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
199 vcc->vci);
200 switch (sk->sk_family) {
Joe Perches07367ad2010-01-26 11:40:13 +0000201 case AF_ATMPVC:
202 seq_printf(seq, "PVC");
203 break;
204 case AF_ATMSVC:
205 seq_printf(seq, "SVC");
206 break;
207 default:
208 seq_printf(seq, "%3d", sk->sk_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Joe Perches07367ad2010-01-26 11:40:13 +0000210 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,
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300214 refcount_read(&sk->sk_refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
218{
219 if (!vcc->dev)
220 seq_printf(seq, sizeof(void *) == 4 ?
Dan Rosenberg71338aa2011-05-23 12:17:35 +0000221 "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 else
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) {
229 int i;
230
231 for (i = 0; i < ATM_ESA_LEN; i++)
232 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
233 }
234 seq_putc(seq, '\n');
235}
236
237static int atm_dev_seq_show(struct seq_file *seq, void *v)
238{
239 static char atm_dev_banner[] =
240 "Itf Type ESI/\"MAC\"addr "
241 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900242
Li Zefan67de7922010-02-08 23:21:51 +0000243 if (v == &atm_devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 seq_puts(seq, atm_dev_banner);
245 else {
246 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
247
248 atm_dev_info(seq, dev);
249 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900252
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700253static const struct seq_operations atm_dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .start = atm_dev_seq_start,
255 .next = atm_dev_seq_next,
256 .stop = atm_dev_seq_stop,
257 .show = atm_dev_seq_show,
258};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int pvc_seq_show(struct seq_file *seq, void *v)
261{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900262 static char atm_pvc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
264
Joe Perches2e1e9842008-04-10 03:33:03 -0700265 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 seq_puts(seq, atm_pvc_banner);
267 else {
268 struct vcc_state *state = seq->private;
269 struct atm_vcc *vcc = atm_sk(state->sk);
270
271 pvc_info(seq, vcc);
272 }
273 return 0;
274}
275
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700276static const struct seq_operations pvc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .start = vcc_seq_start,
278 .next = vcc_seq_next,
279 .stop = vcc_seq_stop,
280 .show = pvc_seq_show,
281};
282
283static int pvc_seq_open(struct inode *inode, struct file *file)
284{
285 return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops);
286}
287
Arjan van de Ven9a321442007-02-12 00:55:35 -0800288static const struct file_operations pvc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .open = pvc_seq_open,
290 .read = seq_read,
291 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800292 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
295static int vcc_seq_show(struct seq_file *seq, void *v)
296{
Joe Perches2e1e9842008-04-10 03:33:03 -0700297 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900298 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
299 "Address ", "Itf VPI VCI Fam Flags Reply "
300 "Send buffer Recv buffer [refcnt]\n");
301 } else {
302 struct vcc_state *state = seq->private;
303 struct atm_vcc *vcc = atm_sk(state->sk);
304
305 vcc_info(seq, vcc);
306 }
307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900309
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700310static const struct seq_operations vcc_seq_ops = {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900311 .start = vcc_seq_start,
312 .next = vcc_seq_next,
313 .stop = vcc_seq_stop,
314 .show = vcc_seq_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static int vcc_seq_open(struct inode *inode, struct file *file)
318{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900319 return __vcc_seq_open(inode, file, 0, &vcc_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900321
Arjan van de Ven9a321442007-02-12 00:55:35 -0800322static const struct file_operations vcc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .open = vcc_seq_open,
324 .read = seq_read,
325 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800326 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329static int svc_seq_show(struct seq_file *seq, void *v)
330{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700331 static const char atm_svc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 "Itf VPI VCI State Remote\n";
333
Joe Perches2e1e9842008-04-10 03:33:03 -0700334 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 seq_puts(seq, atm_svc_banner);
336 else {
337 struct vcc_state *state = seq->private;
338 struct atm_vcc *vcc = atm_sk(state->sk);
339
340 svc_info(seq, vcc);
341 }
342 return 0;
343}
344
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700345static const struct seq_operations svc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .start = vcc_seq_start,
347 .next = vcc_seq_next,
348 .stop = vcc_seq_stop,
349 .show = svc_seq_show,
350};
351
352static int svc_seq_open(struct inode *inode, struct file *file)
353{
354 return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops);
355}
356
Arjan van de Ven9a321442007-02-12 00:55:35 -0800357static const struct file_operations svc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .open = svc_seq_open,
359 .read = seq_read,
360 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800361 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
365 size_t count, loff_t *pos)
366{
367 struct atm_dev *dev;
368 unsigned long page;
369 int length;
370
Joe Perches07367ad2010-01-26 11:40:13 +0000371 if (count == 0)
372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 page = get_zeroed_page(GFP_KERNEL);
Joe Perches07367ad2010-01-26 11:40:13 +0000374 if (!page)
375 return -ENOMEM;
Al Virod9dda782013-03-31 18:16:14 -0400376 dev = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (!dev->ops->proc_read)
378 length = -EINVAL;
379 else {
Joe Perches07367ad2010-01-26 11:40:13 +0000380 length = dev->ops->proc_read(dev, pos, (char *)page);
381 if (length > count)
382 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 if (length >= 0) {
Joe Perches07367ad2010-01-26 11:40:13 +0000385 if (copy_to_user(buf, (char *)page, length))
386 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 (*pos)++;
388 }
389 free_page(page);
390 return length;
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393struct proc_dir_entry *atm_proc_root;
394EXPORT_SYMBOL(atm_proc_root);
395
396
397int atm_proc_dev_register(struct atm_dev *dev)
398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int error;
400
401 /* No proc info */
402 if (!dev->ops->proc_read)
403 return 0;
404
405 error = -ENOMEM;
Eric Dumazet62c97ac2010-03-18 13:48:26 +0000406 dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!dev->proc_name)
408 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Denis V. Lunev0c896522008-05-02 04:08:30 -0700410 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
411 &proc_atm_dev_ops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!dev->proc_entry)
413 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
Joe Perches07367ad2010-01-26 11:40:13 +0000415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416err_free_name:
417 kfree(dev->proc_name);
418err_out:
419 return error;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422void atm_proc_dev_deregister(struct atm_dev *dev)
423{
424 if (!dev->ops->proc_read)
425 return;
426
427 remove_proc_entry(dev->proc_name, atm_proc_root);
428 kfree(dev->proc_name);
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431int __init atm_proc_init(void)
432{
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800433 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!atm_proc_root)
Christoph Hellwigce9fe432018-04-15 10:53:36 +0200435 return -ENOMEM;
436 proc_create_seq("devices", 0444, atm_proc_root, &atm_dev_seq_ops);
437 proc_create("pvc", 0444, atm_proc_root, &pvc_seq_fops);
438 proc_create("svc", 0444, atm_proc_root, &svc_seq_fops);
439 proc_create("vc", 0444, atm_proc_root, &vcc_seq_fops);
440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Kevin Hilmanb9c6e3e2006-08-15 02:02:33 -0700443void atm_proc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Christoph Hellwigce9fe432018-04-15 10:53:36 +0200445 remove_proc_subtree("atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}