l2tp_debugfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * L2TP subsystem debugfs
  3. *
  4. * Copyright (c) 2010 Katalix Systems Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/socket.h>
  14. #include <linux/hash.h>
  15. #include <linux/l2tp.h>
  16. #include <linux/in.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/debugfs.h>
  20. #include <net/sock.h>
  21. #include <net/ip.h>
  22. #include <net/icmp.h>
  23. #include <net/udp.h>
  24. #include <net/inet_common.h>
  25. #include <net/inet_hashtables.h>
  26. #include <net/tcp_states.h>
  27. #include <net/protocol.h>
  28. #include <net/xfrm.h>
  29. #include <net/net_namespace.h>
  30. #include <net/netns/generic.h>
  31. #include "l2tp_core.h"
  32. static struct dentry *rootdir;
  33. static struct dentry *tunnels;
  34. struct l2tp_dfs_seq_data {
  35. struct net *net;
  36. int tunnel_idx; /* current tunnel */
  37. int session_idx; /* index of session within current tunnel */
  38. struct l2tp_tunnel *tunnel;
  39. struct l2tp_session *session; /* NULL means get next tunnel */
  40. };
  41. static void l2tp_dfs_next_tunnel(struct l2tp_dfs_seq_data *pd)
  42. {
  43. pd->tunnel = l2tp_tunnel_find_nth(pd->net, pd->tunnel_idx);
  44. pd->tunnel_idx++;
  45. }
  46. static void l2tp_dfs_next_session(struct l2tp_dfs_seq_data *pd)
  47. {
  48. pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
  49. pd->session_idx++;
  50. if (pd->session == NULL) {
  51. pd->session_idx = 0;
  52. l2tp_dfs_next_tunnel(pd);
  53. }
  54. }
  55. static void *l2tp_dfs_seq_start(struct seq_file *m, loff_t *offs)
  56. {
  57. struct l2tp_dfs_seq_data *pd = SEQ_START_TOKEN;
  58. loff_t pos = *offs;
  59. if (!pos)
  60. goto out;
  61. BUG_ON(m->private == NULL);
  62. pd = m->private;
  63. if (pd->tunnel == NULL)
  64. l2tp_dfs_next_tunnel(pd);
  65. else
  66. l2tp_dfs_next_session(pd);
  67. /* NULL tunnel and session indicates end of list */
  68. if ((pd->tunnel == NULL) && (pd->session == NULL))
  69. pd = NULL;
  70. out:
  71. return pd;
  72. }
  73. static void *l2tp_dfs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  74. {
  75. (*pos)++;
  76. return NULL;
  77. }
  78. static void l2tp_dfs_seq_stop(struct seq_file *p, void *v)
  79. {
  80. /* nothing to do */
  81. }
  82. static void l2tp_dfs_seq_tunnel_show(struct seq_file *m, void *v)
  83. {
  84. struct l2tp_tunnel *tunnel = v;
  85. int session_count = 0;
  86. int hash;
  87. struct hlist_node *walk;
  88. struct hlist_node *tmp;
  89. read_lock_bh(&tunnel->hlist_lock);
  90. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  91. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  92. struct l2tp_session *session;
  93. session = hlist_entry(walk, struct l2tp_session, hlist);
  94. if (session->session_id == 0)
  95. continue;
  96. session_count++;
  97. }
  98. }
  99. read_unlock_bh(&tunnel->hlist_lock);
  100. seq_printf(m, "\nTUNNEL %u peer %u", tunnel->tunnel_id, tunnel->peer_tunnel_id);
  101. if (tunnel->sock) {
  102. struct inet_sock *inet = inet_sk(tunnel->sock);
  103. seq_printf(m, " from %pI4 to %pI4\n",
  104. &inet->inet_saddr, &inet->inet_daddr);
  105. if (tunnel->encap == L2TP_ENCAPTYPE_UDP)
  106. seq_printf(m, " source port %hu, dest port %hu\n",
  107. ntohs(inet->inet_sport), ntohs(inet->inet_dport));
  108. }
  109. seq_printf(m, " L2TPv%d, %s\n", tunnel->version,
  110. tunnel->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
  111. tunnel->encap == L2TP_ENCAPTYPE_IP ? "IP" :
  112. "");
  113. seq_printf(m, " %d sessions, refcnt %d/%d\n", session_count,
  114. tunnel->sock ? atomic_read(&tunnel->sock->sk_refcnt) : 0,
  115. atomic_read(&tunnel->ref_count));
  116. seq_printf(m, " %08x rx %llu/%llu/%llu rx %llu/%llu/%llu\n",
  117. tunnel->debug,
  118. (unsigned long long)tunnel->stats.tx_packets,
  119. (unsigned long long)tunnel->stats.tx_bytes,
  120. (unsigned long long)tunnel->stats.tx_errors,
  121. (unsigned long long)tunnel->stats.rx_packets,
  122. (unsigned long long)tunnel->stats.rx_bytes,
  123. (unsigned long long)tunnel->stats.rx_errors);
  124. if (tunnel->show != NULL)
  125. tunnel->show(m, tunnel);
  126. }
  127. static void l2tp_dfs_seq_session_show(struct seq_file *m, void *v)
  128. {
  129. struct l2tp_session *session = v;
  130. seq_printf(m, " SESSION %u, peer %u, %s\n", session->session_id,
  131. session->peer_session_id,
  132. session->pwtype == L2TP_PWTYPE_ETH ? "ETH" :
  133. session->pwtype == L2TP_PWTYPE_PPP ? "PPP" :
  134. "");
  135. if (session->send_seq || session->recv_seq)
  136. seq_printf(m, " nr %hu, ns %hu\n", session->nr, session->ns);
  137. seq_printf(m, " refcnt %d\n", atomic_read(&session->ref_count));
  138. seq_printf(m, " config %d/%d/%c/%c/%s/%s %08x %u\n",
  139. session->mtu, session->mru,
  140. session->recv_seq ? 'R' : '-',
  141. session->send_seq ? 'S' : '-',
  142. session->data_seq == 1 ? "IPSEQ" :
  143. session->data_seq == 2 ? "DATASEQ" : "-",
  144. session->lns_mode ? "LNS" : "LAC",
  145. session->debug,
  146. jiffies_to_msecs(session->reorder_timeout));
  147. seq_printf(m, " offset %hu l2specific %hu/%hu\n",
  148. session->offset, session->l2specific_type, session->l2specific_len);
  149. if (session->cookie_len) {
  150. seq_printf(m, " cookie %02x%02x%02x%02x",
  151. session->cookie[0], session->cookie[1],
  152. session->cookie[2], session->cookie[3]);
  153. if (session->cookie_len == 8)
  154. seq_printf(m, "%02x%02x%02x%02x",
  155. session->cookie[4], session->cookie[5],
  156. session->cookie[6], session->cookie[7]);
  157. seq_printf(m, "\n");
  158. }
  159. if (session->peer_cookie_len) {
  160. seq_printf(m, " peer cookie %02x%02x%02x%02x",
  161. session->peer_cookie[0], session->peer_cookie[1],
  162. session->peer_cookie[2], session->peer_cookie[3]);
  163. if (session->peer_cookie_len == 8)
  164. seq_printf(m, "%02x%02x%02x%02x",
  165. session->peer_cookie[4], session->peer_cookie[5],
  166. session->peer_cookie[6], session->peer_cookie[7]);
  167. seq_printf(m, "\n");
  168. }
  169. seq_printf(m, " %hu/%hu tx %llu/%llu/%llu rx %llu/%llu/%llu\n",
  170. session->nr, session->ns,
  171. (unsigned long long)session->stats.tx_packets,
  172. (unsigned long long)session->stats.tx_bytes,
  173. (unsigned long long)session->stats.tx_errors,
  174. (unsigned long long)session->stats.rx_packets,
  175. (unsigned long long)session->stats.rx_bytes,
  176. (unsigned long long)session->stats.rx_errors);
  177. if (session->show != NULL)
  178. session->show(m, session);
  179. }
  180. static int l2tp_dfs_seq_show(struct seq_file *m, void *v)
  181. {
  182. struct l2tp_dfs_seq_data *pd = v;
  183. /* display header on line 1 */
  184. if (v == SEQ_START_TOKEN) {
  185. seq_puts(m, "TUNNEL ID, peer ID from IP to IP\n");
  186. seq_puts(m, " L2TPv2/L2TPv3, UDP/IP\n");
  187. seq_puts(m, " sessions session-count, refcnt refcnt/sk->refcnt\n");
  188. seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  189. seq_puts(m, " SESSION ID, peer ID, PWTYPE\n");
  190. seq_puts(m, " refcnt cnt\n");
  191. seq_puts(m, " offset OFFSET l2specific TYPE/LEN\n");
  192. seq_puts(m, " [ cookie ]\n");
  193. seq_puts(m, " [ peer cookie ]\n");
  194. seq_puts(m, " config mtu/mru/rcvseq/sendseq/dataseq/lns debug reorderto\n");
  195. seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  196. goto out;
  197. }
  198. /* Show the tunnel or session context */
  199. if (pd->session == NULL)
  200. l2tp_dfs_seq_tunnel_show(m, pd->tunnel);
  201. else
  202. l2tp_dfs_seq_session_show(m, pd->session);
  203. out:
  204. return 0;
  205. }
  206. static const struct seq_operations l2tp_dfs_seq_ops = {
  207. .start = l2tp_dfs_seq_start,
  208. .next = l2tp_dfs_seq_next,
  209. .stop = l2tp_dfs_seq_stop,
  210. .show = l2tp_dfs_seq_show,
  211. };
  212. static int l2tp_dfs_seq_open(struct inode *inode, struct file *file)
  213. {
  214. struct l2tp_dfs_seq_data *pd;
  215. struct seq_file *seq;
  216. int rc = -ENOMEM;
  217. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  218. if (pd == NULL)
  219. goto out;
  220. /* Derive the network namespace from the pid opening the
  221. * file.
  222. */
  223. pd->net = get_net_ns_by_pid(current->pid);
  224. if (IS_ERR(pd->net)) {
  225. rc = PTR_ERR(pd->net);
  226. goto err_free_pd;
  227. }
  228. rc = seq_open(file, &l2tp_dfs_seq_ops);
  229. if (rc)
  230. goto err_free_net;
  231. seq = file->private_data;
  232. seq->private = pd;
  233. out:
  234. return rc;
  235. err_free_net:
  236. put_net(pd->net);
  237. err_free_pd:
  238. kfree(pd);
  239. goto out;
  240. }
  241. static int l2tp_dfs_seq_release(struct inode *inode, struct file *file)
  242. {
  243. struct l2tp_dfs_seq_data *pd;
  244. struct seq_file *seq;
  245. seq = file->private_data;
  246. pd = seq->private;
  247. if (pd->net)
  248. put_net(pd->net);
  249. kfree(pd);
  250. seq_release(inode, file);
  251. return 0;
  252. }
  253. static const struct file_operations l2tp_dfs_fops = {
  254. .owner = THIS_MODULE,
  255. .open = l2tp_dfs_seq_open,
  256. .read = seq_read,
  257. .llseek = seq_lseek,
  258. .release = l2tp_dfs_seq_release,
  259. };
  260. static int __init l2tp_debugfs_init(void)
  261. {
  262. int rc = 0;
  263. rootdir = debugfs_create_dir("l2tp", NULL);
  264. if (IS_ERR(rootdir)) {
  265. rc = PTR_ERR(rootdir);
  266. rootdir = NULL;
  267. goto out;
  268. }
  269. tunnels = debugfs_create_file("tunnels", 0600, rootdir, NULL, &l2tp_dfs_fops);
  270. if (tunnels == NULL)
  271. rc = -EIO;
  272. printk(KERN_INFO "L2TP debugfs support\n");
  273. out:
  274. if (rc)
  275. printk(KERN_WARNING "l2tp debugfs: unable to init\n");
  276. return rc;
  277. }
  278. static void __exit l2tp_debugfs_exit(void)
  279. {
  280. debugfs_remove(tunnels);
  281. debugfs_remove(rootdir);
  282. }
  283. module_init(l2tp_debugfs_init);
  284. module_exit(l2tp_debugfs_exit);
  285. MODULE_LICENSE("GPL");
  286. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  287. MODULE_DESCRIPTION("L2TP debugfs driver");
  288. MODULE_VERSION("1.0");