l2tp_debugfs.c 9.0 KB

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