1 // SPDX-License-Identifier: GPL-2.0 2 /* 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 12 #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/init.h> /* for __init */ 25 #include <linux/slab.h> 26 #include <net/net_namespace.h> 27 #include <linux/uaccess.h> 28 #include <linux/param.h> /* for HZ */ 29 #include <linux/atomic.h> 30 #include "resources.h" 31 #include "common.h" /* atm_proc_init prototype */ 32 33 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf, 34 size_t count, loff_t *pos); 35 36 static const struct proc_ops atm_dev_proc_ops = { 37 .proc_read = proc_dev_atm_read, 38 .proc_lseek = noop_llseek, 39 }; 40 41 static void add_stats(struct seq_file *seq, const char *aal, 42 const struct k_atm_aal_stats *stats) 43 { 44 seq_printf(seq, "%s ( %d %d %d %d %d )", aal, 45 atomic_read(&stats->tx), atomic_read(&stats->tx_err), 46 atomic_read(&stats->rx), atomic_read(&stats->rx_err), 47 atomic_read(&stats->rx_drop)); 48 } 49 50 static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev) 51 { 52 int i; 53 54 seq_printf(seq, "%3d %-8s", dev->number, dev->type); 55 for (i = 0; i < ESI_LEN; i++) 56 seq_printf(seq, "%02x", dev->esi[i]); 57 seq_puts(seq, " "); 58 add_stats(seq, "0", &dev->stats.aal0); 59 seq_puts(seq, " "); 60 add_stats(seq, "5", &dev->stats.aal5); 61 seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt)); 62 seq_putc(seq, '\n'); 63 } 64 65 struct vcc_state { 66 int bucket; 67 struct sock *sk; 68 }; 69 70 static inline int compare_family(struct sock *sk, int family) 71 { 72 return !family || (sk->sk_family == family); 73 } 74 75 static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l) 76 { 77 struct sock *sk = *sock; 78 79 if (sk == SEQ_START_TOKEN) { 80 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) { 81 struct hlist_head *head = &vcc_hash[*bucket]; 82 83 sk = hlist_empty(head) ? NULL : __sk_head(head); 84 if (sk) 85 break; 86 } 87 l--; 88 } 89 try_again: 90 for (; sk; sk = sk_next(sk)) { 91 l -= compare_family(sk, family); 92 if (l < 0) 93 goto out; 94 } 95 if (!sk && ++*bucket < VCC_HTABLE_SIZE) { 96 sk = sk_head(&vcc_hash[*bucket]); 97 goto try_again; 98 } 99 sk = SEQ_START_TOKEN; 100 out: 101 *sock = sk; 102 return (l < 0); 103 } 104 105 static inline void *vcc_walk(struct seq_file *seq, loff_t l) 106 { 107 struct vcc_state *state = seq->private; 108 int family = (uintptr_t)(pde_data(file_inode(seq->file))); 109 110 return __vcc_walk(&state->sk, family, &state->bucket, l) ? 111 state : NULL; 112 } 113 114 static void *vcc_seq_start(struct seq_file *seq, loff_t *pos) 115 __acquires(vcc_sklist_lock) 116 { 117 struct vcc_state *state = seq->private; 118 loff_t left = *pos; 119 120 read_lock(&vcc_sklist_lock); 121 state->sk = SEQ_START_TOKEN; 122 return left ? vcc_walk(seq, left) : SEQ_START_TOKEN; 123 } 124 125 static void vcc_seq_stop(struct seq_file *seq, void *v) 126 __releases(vcc_sklist_lock) 127 { 128 read_unlock(&vcc_sklist_lock); 129 } 130 131 static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 132 { 133 v = vcc_walk(seq, 1); 134 (*pos)++; 135 return v; 136 } 137 138 static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc) 139 { 140 static const char *const class_name[] = { 141 "off", "UBR", "CBR", "VBR", "ABR"}; 142 static const char *const aal_name[] = { 143 "---", "1", "2", "3/4", /* 0- 3 */ 144 "???", "5", "???", "???", /* 4- 7 */ 145 "???", "???", "???", "???", /* 8-11 */ 146 "???", "0", "???", "???"}; /* 12-15 */ 147 148 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s", 149 vcc->dev->number, vcc->vpi, vcc->vci, 150 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" : 151 aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr, 152 class_name[vcc->qos.rxtp.traffic_class], 153 vcc->qos.txtp.min_pcr, 154 class_name[vcc->qos.txtp.traffic_class]); 155 seq_putc(seq, '\n'); 156 } 157 158 static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc) 159 { 160 struct sock *sk = sk_atm(vcc); 161 162 seq_printf(seq, "%pK ", vcc); 163 if (!vcc->dev) 164 seq_printf(seq, "Unassigned "); 165 else 166 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi, 167 vcc->vci); 168 switch (sk->sk_family) { 169 case AF_ATMPVC: 170 seq_printf(seq, "PVC"); 171 break; 172 default: 173 seq_printf(seq, "%3d", sk->sk_family); 174 } 175 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n", 176 vcc->flags, sk->sk_err, 177 sk_wmem_alloc_get(sk), sk->sk_sndbuf, 178 sk_rmem_alloc_get(sk), sk->sk_rcvbuf, 179 refcount_read(&sk->sk_refcnt)); 180 } 181 182 static int atm_dev_seq_show(struct seq_file *seq, void *v) 183 { 184 static char atm_dev_banner[] = 185 "Itf Type ESI/\"MAC\"addr " 186 "AAL(TX,err,RX,err,drop) ... [refcnt]\n"; 187 188 if (v == &atm_devs) 189 seq_puts(seq, atm_dev_banner); 190 else { 191 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list); 192 193 atm_dev_info(seq, dev); 194 } 195 return 0; 196 } 197 198 static const struct seq_operations atm_dev_seq_ops = { 199 .start = atm_dev_seq_start, 200 .next = atm_dev_seq_next, 201 .stop = atm_dev_seq_stop, 202 .show = atm_dev_seq_show, 203 }; 204 205 static int pvc_seq_show(struct seq_file *seq, void *v) 206 { 207 static char atm_pvc_banner[] = 208 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n"; 209 210 if (v == SEQ_START_TOKEN) 211 seq_puts(seq, atm_pvc_banner); 212 else { 213 struct vcc_state *state = seq->private; 214 struct atm_vcc *vcc = atm_sk(state->sk); 215 216 pvc_info(seq, vcc); 217 } 218 return 0; 219 } 220 221 static const struct seq_operations pvc_seq_ops = { 222 .start = vcc_seq_start, 223 .next = vcc_seq_next, 224 .stop = vcc_seq_stop, 225 .show = pvc_seq_show, 226 }; 227 228 static int vcc_seq_show(struct seq_file *seq, void *v) 229 { 230 if (v == SEQ_START_TOKEN) { 231 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s", 232 "Address ", "Itf VPI VCI Fam Flags Reply " 233 "Send buffer Recv buffer [refcnt]\n"); 234 } else { 235 struct vcc_state *state = seq->private; 236 struct atm_vcc *vcc = atm_sk(state->sk); 237 238 vcc_info(seq, vcc); 239 } 240 return 0; 241 } 242 243 static const struct seq_operations vcc_seq_ops = { 244 .start = vcc_seq_start, 245 .next = vcc_seq_next, 246 .stop = vcc_seq_stop, 247 .show = vcc_seq_show, 248 }; 249 250 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf, 251 size_t count, loff_t *pos) 252 { 253 struct atm_dev *dev; 254 unsigned long page; 255 int length; 256 257 if (count == 0) 258 return 0; 259 page = get_zeroed_page(GFP_KERNEL); 260 if (!page) 261 return -ENOMEM; 262 dev = pde_data(file_inode(file)); 263 if (!dev->ops->proc_read) 264 length = -EINVAL; 265 else { 266 length = dev->ops->proc_read(dev, pos, (char *)page); 267 if (length > count) 268 length = -EINVAL; 269 } 270 if (length >= 0) { 271 if (copy_to_user(buf, (char *)page, length)) 272 length = -EFAULT; 273 (*pos)++; 274 } 275 free_page(page); 276 return length; 277 } 278 279 struct proc_dir_entry *atm_proc_root; 280 EXPORT_SYMBOL(atm_proc_root); 281 282 283 int atm_proc_dev_register(struct atm_dev *dev) 284 { 285 int error; 286 287 /* No proc info */ 288 if (!dev->ops->proc_read) 289 return 0; 290 291 error = -ENOMEM; 292 dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number); 293 if (!dev->proc_name) 294 goto err_out; 295 296 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root, 297 &atm_dev_proc_ops, dev); 298 if (!dev->proc_entry) 299 goto err_free_name; 300 return 0; 301 302 err_free_name: 303 kfree(dev->proc_name); 304 err_out: 305 return error; 306 } 307 308 void atm_proc_dev_deregister(struct atm_dev *dev) 309 { 310 if (!dev->ops->proc_read) 311 return; 312 313 remove_proc_entry(dev->proc_name, atm_proc_root); 314 kfree(dev->proc_name); 315 } 316 317 int __init atm_proc_init(void) 318 { 319 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net); 320 if (!atm_proc_root) 321 return -ENOMEM; 322 proc_create_seq("devices", 0444, atm_proc_root, &atm_dev_seq_ops); 323 proc_create_seq_private("pvc", 0444, atm_proc_root, &pvc_seq_ops, 324 sizeof(struct vcc_state), (void *)(uintptr_t)PF_ATMPVC); 325 proc_create_seq_private("vc", 0444, atm_proc_root, &vcc_seq_ops, 326 sizeof(struct vcc_state), NULL); 327 return 0; 328 } 329 330 void atm_proc_exit(void) 331 { 332 remove_proc_subtree("atm", init_net.proc_net); 333 } 334