Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* net/atm/proc.c - ATM /proc interface
0003  *
0004  * Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA
0005  *
0006  * seq_file api usage by romieu@fr.zoreil.com
0007  *
0008  * Evaluating the efficiency of the whole thing if left as an exercise to
0009  * the reader.
0010  */
0011 
0012 #include <linux/module.h> /* for EXPORT_SYMBOL */
0013 #include <linux/string.h>
0014 #include <linux/types.h>
0015 #include <linux/mm.h>
0016 #include <linux/fs.h>
0017 #include <linux/stat.h>
0018 #include <linux/proc_fs.h>
0019 #include <linux/seq_file.h>
0020 #include <linux/errno.h>
0021 #include <linux/atm.h>
0022 #include <linux/atmdev.h>
0023 #include <linux/netdevice.h>
0024 #include <linux/atmclip.h>
0025 #include <linux/init.h> /* for __init */
0026 #include <linux/slab.h>
0027 #include <net/net_namespace.h>
0028 #include <net/atmclip.h>
0029 #include <linux/uaccess.h>
0030 #include <linux/param.h> /* for HZ */
0031 #include <linux/atomic.h>
0032 #include "resources.h"
0033 #include "common.h" /* atm_proc_init prototype */
0034 #include "signaling.h" /* to get sigd - ugly too */
0035 
0036 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
0037                  size_t count, loff_t *pos);
0038 
0039 static const struct proc_ops atm_dev_proc_ops = {
0040     .proc_read  = proc_dev_atm_read,
0041     .proc_lseek = noop_llseek,
0042 };
0043 
0044 static void add_stats(struct seq_file *seq, const char *aal,
0045   const struct k_atm_aal_stats *stats)
0046 {
0047     seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
0048            atomic_read(&stats->tx), atomic_read(&stats->tx_err),
0049            atomic_read(&stats->rx), atomic_read(&stats->rx_err),
0050            atomic_read(&stats->rx_drop));
0051 }
0052 
0053 static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
0054 {
0055     int i;
0056 
0057     seq_printf(seq, "%3d %-8s", dev->number, dev->type);
0058     for (i = 0; i < ESI_LEN; i++)
0059         seq_printf(seq, "%02x", dev->esi[i]);
0060     seq_puts(seq, "  ");
0061     add_stats(seq, "0", &dev->stats.aal0);
0062     seq_puts(seq, "  ");
0063     add_stats(seq, "5", &dev->stats.aal5);
0064     seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt));
0065     seq_putc(seq, '\n');
0066 }
0067 
0068 struct vcc_state {
0069     int bucket;
0070     struct sock *sk;
0071 };
0072 
0073 static inline int compare_family(struct sock *sk, int family)
0074 {
0075     return !family || (sk->sk_family == family);
0076 }
0077 
0078 static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
0079 {
0080     struct sock *sk = *sock;
0081 
0082     if (sk == SEQ_START_TOKEN) {
0083         for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
0084             struct hlist_head *head = &vcc_hash[*bucket];
0085 
0086             sk = hlist_empty(head) ? NULL : __sk_head(head);
0087             if (sk)
0088                 break;
0089         }
0090         l--;
0091     }
0092 try_again:
0093     for (; sk; sk = sk_next(sk)) {
0094         l -= compare_family(sk, family);
0095         if (l < 0)
0096             goto out;
0097     }
0098     if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
0099         sk = sk_head(&vcc_hash[*bucket]);
0100         goto try_again;
0101     }
0102     sk = SEQ_START_TOKEN;
0103 out:
0104     *sock = sk;
0105     return (l < 0);
0106 }
0107 
0108 static inline void *vcc_walk(struct seq_file *seq, loff_t l)
0109 {
0110     struct vcc_state *state = seq->private;
0111     int family = (uintptr_t)(pde_data(file_inode(seq->file)));
0112 
0113     return __vcc_walk(&state->sk, family, &state->bucket, l) ?
0114            state : NULL;
0115 }
0116 
0117 static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
0118     __acquires(vcc_sklist_lock)
0119 {
0120     struct vcc_state *state = seq->private;
0121     loff_t left = *pos;
0122 
0123     read_lock(&vcc_sklist_lock);
0124     state->sk = SEQ_START_TOKEN;
0125     return left ? vcc_walk(seq, left) : SEQ_START_TOKEN;
0126 }
0127 
0128 static void vcc_seq_stop(struct seq_file *seq, void *v)
0129     __releases(vcc_sklist_lock)
0130 {
0131     read_unlock(&vcc_sklist_lock);
0132 }
0133 
0134 static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0135 {
0136     v = vcc_walk(seq, 1);
0137     (*pos)++;
0138     return v;
0139 }
0140 
0141 static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
0142 {
0143     static const char *const class_name[] = {
0144         "off", "UBR", "CBR", "VBR", "ABR"};
0145     static const char *const aal_name[] = {
0146         "---",  "1",    "2",    "3/4",  /*  0- 3 */
0147         "???",  "5",    "???",  "???",  /*  4- 7 */
0148         "???",  "???",  "???",  "???",  /*  8-11 */
0149         "???",  "0",    "???",  "???"}; /* 12-15 */
0150 
0151     seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
0152            vcc->dev->number, vcc->vpi, vcc->vci,
0153            vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
0154            aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr,
0155            class_name[vcc->qos.rxtp.traffic_class],
0156            vcc->qos.txtp.min_pcr,
0157            class_name[vcc->qos.txtp.traffic_class]);
0158     if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
0159         struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
0160         struct net_device *dev;
0161 
0162         dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
0163         seq_printf(seq, "CLIP, Itf:%s, Encap:",
0164             dev ? dev->name : "none?");
0165         seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
0166     }
0167     seq_putc(seq, '\n');
0168 }
0169 
0170 static const char *vcc_state(struct atm_vcc *vcc)
0171 {
0172     static const char *const map[] = { ATM_VS2TXT_MAP };
0173 
0174     return map[ATM_VF2VS(vcc->flags)];
0175 }
0176 
0177 static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
0178 {
0179     struct sock *sk = sk_atm(vcc);
0180 
0181     seq_printf(seq, "%pK ", vcc);
0182     if (!vcc->dev)
0183         seq_printf(seq, "Unassigned    ");
0184     else
0185         seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
0186             vcc->vci);
0187     switch (sk->sk_family) {
0188     case AF_ATMPVC:
0189         seq_printf(seq, "PVC");
0190         break;
0191     case AF_ATMSVC:
0192         seq_printf(seq, "SVC");
0193         break;
0194     default:
0195         seq_printf(seq, "%3d", sk->sk_family);
0196     }
0197     seq_printf(seq, " %04lx  %5d %7d/%7d %7d/%7d [%d]\n",
0198            vcc->flags, sk->sk_err,
0199            sk_wmem_alloc_get(sk), sk->sk_sndbuf,
0200            sk_rmem_alloc_get(sk), sk->sk_rcvbuf,
0201            refcount_read(&sk->sk_refcnt));
0202 }
0203 
0204 static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
0205 {
0206     if (!vcc->dev)
0207         seq_printf(seq, sizeof(void *) == 4 ?
0208                "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
0209     else
0210         seq_printf(seq, "%3d %3d %5d         ",
0211                vcc->dev->number, vcc->vpi, vcc->vci);
0212     seq_printf(seq, "%-10s ", vcc_state(vcc));
0213     seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
0214         *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
0215     if (*vcc->remote.sas_addr.prv) {
0216         int i;
0217 
0218         for (i = 0; i < ATM_ESA_LEN; i++)
0219             seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
0220     }
0221     seq_putc(seq, '\n');
0222 }
0223 
0224 static int atm_dev_seq_show(struct seq_file *seq, void *v)
0225 {
0226     static char atm_dev_banner[] =
0227         "Itf Type    ESI/\"MAC\"addr "
0228         "AAL(TX,err,RX,err,drop) ...               [refcnt]\n";
0229 
0230     if (v == &atm_devs)
0231         seq_puts(seq, atm_dev_banner);
0232     else {
0233         struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
0234 
0235         atm_dev_info(seq, dev);
0236     }
0237     return 0;
0238 }
0239 
0240 static const struct seq_operations atm_dev_seq_ops = {
0241     .start  = atm_dev_seq_start,
0242     .next   = atm_dev_seq_next,
0243     .stop   = atm_dev_seq_stop,
0244     .show   = atm_dev_seq_show,
0245 };
0246 
0247 static int pvc_seq_show(struct seq_file *seq, void *v)
0248 {
0249     static char atm_pvc_banner[] =
0250         "Itf VPI VCI   AAL RX(PCR,Class) TX(PCR,Class)\n";
0251 
0252     if (v == SEQ_START_TOKEN)
0253         seq_puts(seq, atm_pvc_banner);
0254     else {
0255         struct vcc_state *state = seq->private;
0256         struct atm_vcc *vcc = atm_sk(state->sk);
0257 
0258         pvc_info(seq, vcc);
0259     }
0260     return 0;
0261 }
0262 
0263 static const struct seq_operations pvc_seq_ops = {
0264     .start  = vcc_seq_start,
0265     .next   = vcc_seq_next,
0266     .stop   = vcc_seq_stop,
0267     .show   = pvc_seq_show,
0268 };
0269 
0270 static int vcc_seq_show(struct seq_file *seq, void *v)
0271 {
0272     if (v == SEQ_START_TOKEN) {
0273         seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
0274             "Address ", "Itf VPI VCI   Fam Flags Reply "
0275             "Send buffer     Recv buffer      [refcnt]\n");
0276     } else {
0277         struct vcc_state *state = seq->private;
0278         struct atm_vcc *vcc = atm_sk(state->sk);
0279 
0280         vcc_info(seq, vcc);
0281     }
0282     return 0;
0283 }
0284 
0285 static const struct seq_operations vcc_seq_ops = {
0286     .start  = vcc_seq_start,
0287     .next   = vcc_seq_next,
0288     .stop   = vcc_seq_stop,
0289     .show   = vcc_seq_show,
0290 };
0291 
0292 static int svc_seq_show(struct seq_file *seq, void *v)
0293 {
0294     static const char atm_svc_banner[] =
0295         "Itf VPI VCI           State      Remote\n";
0296 
0297     if (v == SEQ_START_TOKEN)
0298         seq_puts(seq, atm_svc_banner);
0299     else {
0300         struct vcc_state *state = seq->private;
0301         struct atm_vcc *vcc = atm_sk(state->sk);
0302 
0303         svc_info(seq, vcc);
0304     }
0305     return 0;
0306 }
0307 
0308 static const struct seq_operations svc_seq_ops = {
0309     .start  = vcc_seq_start,
0310     .next   = vcc_seq_next,
0311     .stop   = vcc_seq_stop,
0312     .show   = svc_seq_show,
0313 };
0314 
0315 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
0316                  size_t count, loff_t *pos)
0317 {
0318     struct atm_dev *dev;
0319     unsigned long page;
0320     int length;
0321 
0322     if (count == 0)
0323         return 0;
0324     page = get_zeroed_page(GFP_KERNEL);
0325     if (!page)
0326         return -ENOMEM;
0327     dev = pde_data(file_inode(file));
0328     if (!dev->ops->proc_read)
0329         length = -EINVAL;
0330     else {
0331         length = dev->ops->proc_read(dev, pos, (char *)page);
0332         if (length > count)
0333             length = -EINVAL;
0334     }
0335     if (length >= 0) {
0336         if (copy_to_user(buf, (char *)page, length))
0337             length = -EFAULT;
0338         (*pos)++;
0339     }
0340     free_page(page);
0341     return length;
0342 }
0343 
0344 struct proc_dir_entry *atm_proc_root;
0345 EXPORT_SYMBOL(atm_proc_root);
0346 
0347 
0348 int atm_proc_dev_register(struct atm_dev *dev)
0349 {
0350     int error;
0351 
0352     /* No proc info */
0353     if (!dev->ops->proc_read)
0354         return 0;
0355 
0356     error = -ENOMEM;
0357     dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
0358     if (!dev->proc_name)
0359         goto err_out;
0360 
0361     dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
0362                        &atm_dev_proc_ops, dev);
0363     if (!dev->proc_entry)
0364         goto err_free_name;
0365     return 0;
0366 
0367 err_free_name:
0368     kfree(dev->proc_name);
0369 err_out:
0370     return error;
0371 }
0372 
0373 void atm_proc_dev_deregister(struct atm_dev *dev)
0374 {
0375     if (!dev->ops->proc_read)
0376         return;
0377 
0378     remove_proc_entry(dev->proc_name, atm_proc_root);
0379     kfree(dev->proc_name);
0380 }
0381 
0382 int __init atm_proc_init(void)
0383 {
0384     atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
0385     if (!atm_proc_root)
0386         return -ENOMEM;
0387     proc_create_seq("devices", 0444, atm_proc_root, &atm_dev_seq_ops);
0388     proc_create_seq_private("pvc", 0444, atm_proc_root, &pvc_seq_ops,
0389             sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMPVC);
0390     proc_create_seq_private("svc", 0444, atm_proc_root, &svc_seq_ops,
0391             sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMSVC);
0392     proc_create_seq_private("vc", 0444, atm_proc_root, &vcc_seq_ops,
0393             sizeof(struct vcc_state), NULL);
0394     return 0;
0395 }
0396 
0397 void atm_proc_exit(void)
0398 {
0399     remove_proc_subtree("atm", init_net.proc_net);
0400 }