l2tp_debugfs.c 9.6 KB

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