proc.c 12 KB

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