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/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 */ 35 36 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf, 37 size_t count, loff_t *pos); 38 39 static const struct file_operations proc_atm_dev_ops = { 40 .owner = THIS_MODULE, 41 .read = proc_dev_atm_read, 42 .llseek = noop_llseek, 43 }; 44 45 static void add_stats(struct seq_file *seq, const char *aal, 46 const struct k_atm_aal_stats *stats) 47 { 48 seq_printf(seq, "%s ( %d %d %d %d %d )", aal, 49 atomic_read(&stats->tx), atomic_read(&stats->tx_err), 50 atomic_read(&stats->rx), atomic_read(&stats->rx_err), 51 atomic_read(&stats->rx_drop)); 52 } 53 54 static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev) 55 { 56 int i; 57 58 seq_printf(seq, "%3d %-8s", dev->number, dev->type); 59 for (i = 0; i < ESI_LEN; i++) 60 seq_printf(seq, "%02x", dev->esi[i]); 61 seq_puts(seq, " "); 62 add_stats(seq, "0", &dev->stats.aal0); 63 seq_puts(seq, " "); 64 add_stats(seq, "5", &dev->stats.aal5); 65 seq_printf(seq, "\t[%d]", refcount_read(&dev->refcnt)); 66 seq_putc(seq, '\n'); 67 } 68 69 struct vcc_state { 70 int bucket; 71 struct sock *sk; 72 int family; 73 }; 74 75 static inline int compare_family(struct sock *sk, int family) 76 { 77 return !family || (sk->sk_family == family); 78 } 79 80 static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l) 81 { 82 struct sock *sk = *sock; 83 84 if (sk == SEQ_START_TOKEN) { 85 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) { 86 struct hlist_head *head = &vcc_hash[*bucket]; 87 88 sk = hlist_empty(head) ? NULL : __sk_head(head); 89 if (sk) 90 break; 91 } 92 l--; 93 } 94 try_again: 95 for (; sk; sk = sk_next(sk)) { 96 l -= compare_family(sk, family); 97 if (l < 0) 98 goto out; 99 } 100 if (!sk && ++*bucket < VCC_HTABLE_SIZE) { 101 sk = sk_head(&vcc_hash[*bucket]); 102 goto try_again; 103 } 104 sk = SEQ_START_TOKEN; 105 out: 106 *sock = sk; 107 return (l < 0); 108 } 109 110 static inline void *vcc_walk(struct vcc_state *state, loff_t l) 111 { 112 return __vcc_walk(&state->sk, state->family, &state->bucket, l) ? 113 state : NULL; 114 } 115 116 static int __vcc_seq_open(struct inode *inode, struct file *file, 117 int family, const struct seq_operations *ops) 118 { 119 struct vcc_state *state; 120 121 state = __seq_open_private(file, ops, sizeof(*state)); 122 if (state == NULL) 123 return -ENOMEM; 124 125 state->family = family; 126 return 0; 127 } 128 129 static void *vcc_seq_start(struct seq_file *seq, loff_t *pos) 130 __acquires(vcc_sklist_lock) 131 { 132 struct vcc_state *state = seq->private; 133 loff_t left = *pos; 134 135 read_lock(&vcc_sklist_lock); 136 state->sk = SEQ_START_TOKEN; 137 return left ? vcc_walk(state, left) : SEQ_START_TOKEN; 138 } 139 140 static void vcc_seq_stop(struct seq_file *seq, void *v) 141 __releases(vcc_sklist_lock) 142 { 143 read_unlock(&vcc_sklist_lock); 144 } 145 146 static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 147 { 148 struct vcc_state *state = seq->private; 149 150 v = vcc_walk(state, 1); 151 *pos += !!PTR_ERR(v); 152 return v; 153 } 154 155 static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc) 156 { 157 static const char *const class_name[] = { 158 "off", "UBR", "CBR", "VBR", "ABR"}; 159 static const char *const aal_name[] = { 160 "---", "1", "2", "3/4", /* 0- 3 */ 161 "???", "5", "???", "???", /* 4- 7 */ 162 "???", "???", "???", "???", /* 8-11 */ 163 "???", "0", "???", "???"}; /* 12-15 */ 164 165 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s", 166 vcc->dev->number, vcc->vpi, vcc->vci, 167 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" : 168 aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr, 169 class_name[vcc->qos.rxtp.traffic_class], 170 vcc->qos.txtp.min_pcr, 171 class_name[vcc->qos.txtp.traffic_class]); 172 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) { 173 struct clip_vcc *clip_vcc = CLIP_VCC(vcc); 174 struct net_device *dev; 175 176 dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL; 177 seq_printf(seq, "CLIP, Itf:%s, Encap:", 178 dev ? dev->name : "none?"); 179 seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None"); 180 } 181 seq_putc(seq, '\n'); 182 } 183 184 static const char *vcc_state(struct atm_vcc *vcc) 185 { 186 static const char *const map[] = { ATM_VS2TXT_MAP }; 187 188 return map[ATM_VF2VS(vcc->flags)]; 189 } 190 191 static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc) 192 { 193 struct sock *sk = sk_atm(vcc); 194 195 seq_printf(seq, "%pK ", vcc); 196 if (!vcc->dev) 197 seq_printf(seq, "Unassigned "); 198 else 199 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi, 200 vcc->vci); 201 switch (sk->sk_family) { 202 case AF_ATMPVC: 203 seq_printf(seq, "PVC"); 204 break; 205 case AF_ATMSVC: 206 seq_printf(seq, "SVC"); 207 break; 208 default: 209 seq_printf(seq, "%3d", sk->sk_family); 210 } 211 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n", 212 vcc->flags, sk->sk_err, 213 sk_wmem_alloc_get(sk), sk->sk_sndbuf, 214 sk_rmem_alloc_get(sk), sk->sk_rcvbuf, 215 refcount_read(&sk->sk_refcnt)); 216 } 217 218 static void svc_info(struct seq_file *seq, struct atm_vcc *vcc) 219 { 220 if (!vcc->dev) 221 seq_printf(seq, sizeof(void *) == 4 ? 222 "N/A@%pK%10s" : "N/A@%pK%2s", vcc, ""); 223 else 224 seq_printf(seq, "%3d %3d %5d ", 225 vcc->dev->number, vcc->vpi, vcc->vci); 226 seq_printf(seq, "%-10s ", vcc_state(vcc)); 227 seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub, 228 *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : ""); 229 if (*vcc->remote.sas_addr.prv) { 230 int i; 231 232 for (i = 0; i < ATM_ESA_LEN; i++) 233 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]); 234 } 235 seq_putc(seq, '\n'); 236 } 237 238 static int atm_dev_seq_show(struct seq_file *seq, void *v) 239 { 240 static char atm_dev_banner[] = 241 "Itf Type ESI/\"MAC\"addr " 242 "AAL(TX,err,RX,err,drop) ... [refcnt]\n"; 243 244 if (v == &atm_devs) 245 seq_puts(seq, atm_dev_banner); 246 else { 247 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list); 248 249 atm_dev_info(seq, dev); 250 } 251 return 0; 252 } 253 254 static const struct seq_operations atm_dev_seq_ops = { 255 .start = atm_dev_seq_start, 256 .next = atm_dev_seq_next, 257 .stop = atm_dev_seq_stop, 258 .show = atm_dev_seq_show, 259 }; 260 261 static int atm_dev_seq_open(struct inode *inode, struct file *file) 262 { 263 return seq_open(file, &atm_dev_seq_ops); 264 } 265 266 static const struct file_operations devices_seq_fops = { 267 .open = atm_dev_seq_open, 268 .read = seq_read, 269 .llseek = seq_lseek, 270 .release = seq_release, 271 }; 272 273 static int pvc_seq_show(struct seq_file *seq, void *v) 274 { 275 static char atm_pvc_banner[] = 276 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n"; 277 278 if (v == SEQ_START_TOKEN) 279 seq_puts(seq, atm_pvc_banner); 280 else { 281 struct vcc_state *state = seq->private; 282 struct atm_vcc *vcc = atm_sk(state->sk); 283 284 pvc_info(seq, vcc); 285 } 286 return 0; 287 } 288 289 static const struct seq_operations pvc_seq_ops = { 290 .start = vcc_seq_start, 291 .next = vcc_seq_next, 292 .stop = vcc_seq_stop, 293 .show = pvc_seq_show, 294 }; 295 296 static int pvc_seq_open(struct inode *inode, struct file *file) 297 { 298 return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops); 299 } 300 301 static const struct file_operations pvc_seq_fops = { 302 .open = pvc_seq_open, 303 .read = seq_read, 304 .llseek = seq_lseek, 305 .release = seq_release_private, 306 }; 307 308 static int vcc_seq_show(struct seq_file *seq, void *v) 309 { 310 if (v == SEQ_START_TOKEN) { 311 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s", 312 "Address ", "Itf VPI VCI Fam Flags Reply " 313 "Send buffer Recv buffer [refcnt]\n"); 314 } else { 315 struct vcc_state *state = seq->private; 316 struct atm_vcc *vcc = atm_sk(state->sk); 317 318 vcc_info(seq, vcc); 319 } 320 return 0; 321 } 322 323 static const struct seq_operations vcc_seq_ops = { 324 .start = vcc_seq_start, 325 .next = vcc_seq_next, 326 .stop = vcc_seq_stop, 327 .show = vcc_seq_show, 328 }; 329 330 static int vcc_seq_open(struct inode *inode, struct file *file) 331 { 332 return __vcc_seq_open(inode, file, 0, &vcc_seq_ops); 333 } 334 335 static const struct file_operations vcc_seq_fops = { 336 .open = vcc_seq_open, 337 .read = seq_read, 338 .llseek = seq_lseek, 339 .release = seq_release_private, 340 }; 341 342 static int svc_seq_show(struct seq_file *seq, void *v) 343 { 344 static const char atm_svc_banner[] = 345 "Itf VPI VCI State Remote\n"; 346 347 if (v == SEQ_START_TOKEN) 348 seq_puts(seq, atm_svc_banner); 349 else { 350 struct vcc_state *state = seq->private; 351 struct atm_vcc *vcc = atm_sk(state->sk); 352 353 svc_info(seq, vcc); 354 } 355 return 0; 356 } 357 358 static const struct seq_operations svc_seq_ops = { 359 .start = vcc_seq_start, 360 .next = vcc_seq_next, 361 .stop = vcc_seq_stop, 362 .show = svc_seq_show, 363 }; 364 365 static int svc_seq_open(struct inode *inode, struct file *file) 366 { 367 return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops); 368 } 369 370 static const struct file_operations svc_seq_fops = { 371 .open = svc_seq_open, 372 .read = seq_read, 373 .llseek = seq_lseek, 374 .release = seq_release_private, 375 }; 376 377 static ssize_t proc_dev_atm_read(struct file *file, char __user *buf, 378 size_t count, loff_t *pos) 379 { 380 struct atm_dev *dev; 381 unsigned long page; 382 int length; 383 384 if (count == 0) 385 return 0; 386 page = get_zeroed_page(GFP_KERNEL); 387 if (!page) 388 return -ENOMEM; 389 dev = PDE_DATA(file_inode(file)); 390 if (!dev->ops->proc_read) 391 length = -EINVAL; 392 else { 393 length = dev->ops->proc_read(dev, pos, (char *)page); 394 if (length > count) 395 length = -EINVAL; 396 } 397 if (length >= 0) { 398 if (copy_to_user(buf, (char *)page, length)) 399 length = -EFAULT; 400 (*pos)++; 401 } 402 free_page(page); 403 return length; 404 } 405 406 struct proc_dir_entry *atm_proc_root; 407 EXPORT_SYMBOL(atm_proc_root); 408 409 410 int atm_proc_dev_register(struct atm_dev *dev) 411 { 412 int error; 413 414 /* No proc info */ 415 if (!dev->ops->proc_read) 416 return 0; 417 418 error = -ENOMEM; 419 dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number); 420 if (!dev->proc_name) 421 goto err_out; 422 423 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root, 424 &proc_atm_dev_ops, dev); 425 if (!dev->proc_entry) 426 goto err_free_name; 427 return 0; 428 429 err_free_name: 430 kfree(dev->proc_name); 431 err_out: 432 return error; 433 } 434 435 void atm_proc_dev_deregister(struct atm_dev *dev) 436 { 437 if (!dev->ops->proc_read) 438 return; 439 440 remove_proc_entry(dev->proc_name, atm_proc_root); 441 kfree(dev->proc_name); 442 } 443 444 static struct atm_proc_entry { 445 char *name; 446 const struct file_operations *proc_fops; 447 struct proc_dir_entry *dirent; 448 } atm_proc_ents[] = { 449 { .name = "devices", .proc_fops = &devices_seq_fops }, 450 { .name = "pvc", .proc_fops = &pvc_seq_fops }, 451 { .name = "svc", .proc_fops = &svc_seq_fops }, 452 { .name = "vc", .proc_fops = &vcc_seq_fops }, 453 { .name = NULL, .proc_fops = NULL } 454 }; 455 456 static void atm_proc_dirs_remove(void) 457 { 458 static struct atm_proc_entry *e; 459 460 for (e = atm_proc_ents; e->name; e++) { 461 if (e->dirent) 462 remove_proc_entry(e->name, atm_proc_root); 463 } 464 remove_proc_entry("atm", init_net.proc_net); 465 } 466 467 int __init atm_proc_init(void) 468 { 469 static struct atm_proc_entry *e; 470 int ret; 471 472 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net); 473 if (!atm_proc_root) 474 goto err_out; 475 for (e = atm_proc_ents; e->name; e++) { 476 struct proc_dir_entry *dirent; 477 478 dirent = proc_create(e->name, S_IRUGO, 479 atm_proc_root, e->proc_fops); 480 if (!dirent) 481 goto err_out_remove; 482 e->dirent = dirent; 483 } 484 ret = 0; 485 out: 486 return ret; 487 488 err_out_remove: 489 atm_proc_dirs_remove(); 490 err_out: 491 ret = -ENOMEM; 492 goto out; 493 } 494 495 void atm_proc_exit(void) 496 { 497 atm_proc_dirs_remove(); 498 } 499