l2tp_debugfs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. unsigned long tkey; /* lookup key of current tunnel */
  33. unsigned long skey; /* lookup key of current session */
  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_put(pd->tunnel);
  42. pd->tunnel = l2tp_tunnel_get_next(pd->net, &pd->tkey);
  43. pd->tkey++;
  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_put(pd->session);
  50. pd->session = l2tp_session_get_next(pd->net, pd->tunnel->sock,
  51. pd->tunnel->version,
  52. pd->tunnel->tunnel_id, &pd->skey);
  53. pd->skey++;
  54. if (!pd->session) {
  55. pd->skey = 0;
  56. l2tp_dfs_next_tunnel(pd);
  57. }
  58. }
  59. static void *l2tp_dfs_seq_start(struct seq_file *m, loff_t *offs)
  60. {
  61. struct l2tp_dfs_seq_data *pd = SEQ_START_TOKEN;
  62. loff_t pos = *offs;
  63. if (!pos)
  64. goto out;
  65. if (WARN_ON(!m->private)) {
  66. pd = NULL;
  67. goto out;
  68. }
  69. pd = m->private;
  70. if (!pd->tunnel)
  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 && !pd->session)
  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_put(pd->session);
  95. pd->session = NULL;
  96. }
  97. if (pd->tunnel) {
  98. l2tp_tunnel_put(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. struct l2tp_session *session;
  106. int session_count = 0;
  107. rcu_read_lock_bh();
  108. list_for_each_entry_rcu(session, &tunnel->session_list, list) {
  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. rcu_read_unlock_bh();
  115. seq_printf(m, "\nTUNNEL %u peer %u", tunnel->tunnel_id, tunnel->peer_tunnel_id);
  116. if (tunnel->sock) {
  117. struct inet_sock *inet = inet_sk(tunnel->sock);
  118. #if IS_ENABLED(CONFIG_IPV6)
  119. if (tunnel->sock->sk_family == AF_INET6) {
  120. const struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
  121. seq_printf(m, " from %pI6c to %pI6c\n",
  122. &np->saddr, &tunnel->sock->sk_v6_daddr);
  123. }
  124. #endif
  125. if (tunnel->sock->sk_family == AF_INET)
  126. seq_printf(m, " from %pI4 to %pI4\n",
  127. &inet->inet_saddr, &inet->inet_daddr);
  128. if (tunnel->encap == L2TP_ENCAPTYPE_UDP)
  129. seq_printf(m, " source port %hu, dest port %hu\n",
  130. ntohs(inet->inet_sport), ntohs(inet->inet_dport));
  131. }
  132. seq_printf(m, " L2TPv%d, %s\n", tunnel->version,
  133. tunnel->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
  134. tunnel->encap == L2TP_ENCAPTYPE_IP ? "IP" :
  135. "");
  136. seq_printf(m, " %d sessions, refcnt %d/%d\n", session_count,
  137. tunnel->sock ? refcount_read(&tunnel->sock->sk_refcnt) : 0,
  138. refcount_read(&tunnel->ref_count));
  139. seq_printf(m, " %08x rx %ld/%ld/%ld rx %ld/%ld/%ld\n",
  140. 0,
  141. atomic_long_read(&tunnel->stats.tx_packets),
  142. atomic_long_read(&tunnel->stats.tx_bytes),
  143. atomic_long_read(&tunnel->stats.tx_errors),
  144. atomic_long_read(&tunnel->stats.rx_packets),
  145. atomic_long_read(&tunnel->stats.rx_bytes),
  146. atomic_long_read(&tunnel->stats.rx_errors));
  147. }
  148. static void l2tp_dfs_seq_session_show(struct seq_file *m, void *v)
  149. {
  150. struct l2tp_session *session = v;
  151. seq_printf(m, " SESSION %u, peer %u, %s\n", session->session_id,
  152. session->peer_session_id,
  153. session->pwtype == L2TP_PWTYPE_ETH ? "ETH" :
  154. session->pwtype == L2TP_PWTYPE_PPP ? "PPP" :
  155. "");
  156. if (session->send_seq || session->recv_seq)
  157. seq_printf(m, " nr %u, ns %u\n", session->nr, session->ns);
  158. seq_printf(m, " refcnt %d\n", refcount_read(&session->ref_count));
  159. seq_printf(m, " config 0/0/%c/%c/-/%s %08x %u\n",
  160. session->recv_seq ? 'R' : '-',
  161. session->send_seq ? 'S' : '-',
  162. session->lns_mode ? "LNS" : "LAC",
  163. 0,
  164. jiffies_to_msecs(session->reorder_timeout));
  165. seq_printf(m, " offset 0 l2specific %hu/%d\n",
  166. session->l2specific_type, l2tp_get_l2specific_len(session));
  167. if (session->cookie_len) {
  168. seq_printf(m, " cookie %02x%02x%02x%02x",
  169. session->cookie[0], session->cookie[1],
  170. session->cookie[2], session->cookie[3]);
  171. if (session->cookie_len == 8)
  172. seq_printf(m, "%02x%02x%02x%02x",
  173. session->cookie[4], session->cookie[5],
  174. session->cookie[6], session->cookie[7]);
  175. seq_puts(m, "\n");
  176. }
  177. if (session->peer_cookie_len) {
  178. seq_printf(m, " peer cookie %02x%02x%02x%02x",
  179. session->peer_cookie[0], session->peer_cookie[1],
  180. session->peer_cookie[2], session->peer_cookie[3]);
  181. if (session->peer_cookie_len == 8)
  182. seq_printf(m, "%02x%02x%02x%02x",
  183. session->peer_cookie[4], session->peer_cookie[5],
  184. session->peer_cookie[6], session->peer_cookie[7]);
  185. seq_puts(m, "\n");
  186. }
  187. seq_printf(m, " %u/%u tx %ld/%ld/%ld rx %ld/%ld/%ld\n",
  188. session->nr, session->ns,
  189. atomic_long_read(&session->stats.tx_packets),
  190. atomic_long_read(&session->stats.tx_bytes),
  191. atomic_long_read(&session->stats.tx_errors),
  192. atomic_long_read(&session->stats.rx_packets),
  193. atomic_long_read(&session->stats.rx_bytes),
  194. atomic_long_read(&session->stats.rx_errors));
  195. if (session->show)
  196. session->show(m, session);
  197. }
  198. static int l2tp_dfs_seq_show(struct seq_file *m, void *v)
  199. {
  200. struct l2tp_dfs_seq_data *pd = v;
  201. /* display header on line 1 */
  202. if (v == SEQ_START_TOKEN) {
  203. seq_puts(m, "TUNNEL ID, peer ID from IP to IP\n");
  204. seq_puts(m, " L2TPv2/L2TPv3, UDP/IP\n");
  205. seq_puts(m, " sessions session-count, refcnt refcnt/sk->refcnt\n");
  206. seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  207. seq_puts(m, " SESSION ID, peer ID, PWTYPE\n");
  208. seq_puts(m, " refcnt cnt\n");
  209. seq_puts(m, " offset OFFSET l2specific TYPE/LEN\n");
  210. seq_puts(m, " [ cookie ]\n");
  211. seq_puts(m, " [ peer cookie ]\n");
  212. seq_puts(m, " config mtu/mru/rcvseq/sendseq/dataseq/lns debug reorderto\n");
  213. seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  214. goto out;
  215. }
  216. if (!pd->session)
  217. l2tp_dfs_seq_tunnel_show(m, pd->tunnel);
  218. else
  219. l2tp_dfs_seq_session_show(m, pd->session);
  220. out:
  221. return 0;
  222. }
  223. static const struct seq_operations l2tp_dfs_seq_ops = {
  224. .start = l2tp_dfs_seq_start,
  225. .next = l2tp_dfs_seq_next,
  226. .stop = l2tp_dfs_seq_stop,
  227. .show = l2tp_dfs_seq_show,
  228. };
  229. static int l2tp_dfs_seq_open(struct inode *inode, struct file *file)
  230. {
  231. struct l2tp_dfs_seq_data *pd;
  232. struct seq_file *seq;
  233. int rc = -ENOMEM;
  234. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  235. if (!pd)
  236. goto out;
  237. /* Derive the network namespace from the pid opening the
  238. * file.
  239. */
  240. pd->net = get_net_ns_by_pid(current->pid);
  241. if (IS_ERR(pd->net)) {
  242. rc = PTR_ERR(pd->net);
  243. goto err_free_pd;
  244. }
  245. netns_tracker_alloc(pd->net, &pd->ns_tracker, GFP_KERNEL);
  246. rc = seq_open(file, &l2tp_dfs_seq_ops);
  247. if (rc)
  248. goto err_free_net;
  249. seq = file->private_data;
  250. seq->private = pd;
  251. out:
  252. return rc;
  253. err_free_net:
  254. put_net_track(pd->net, &pd->ns_tracker);
  255. err_free_pd:
  256. kfree(pd);
  257. goto out;
  258. }
  259. static int l2tp_dfs_seq_release(struct inode *inode, struct file *file)
  260. {
  261. struct l2tp_dfs_seq_data *pd;
  262. struct seq_file *seq;
  263. seq = file->private_data;
  264. pd = seq->private;
  265. if (pd->net)
  266. put_net_track(pd->net, &pd->ns_tracker);
  267. kfree(pd);
  268. seq_release(inode, file);
  269. return 0;
  270. }
  271. static const struct file_operations l2tp_dfs_fops = {
  272. .owner = THIS_MODULE,
  273. .open = l2tp_dfs_seq_open,
  274. .read = seq_read,
  275. .llseek = seq_lseek,
  276. .release = l2tp_dfs_seq_release,
  277. };
  278. static int __init l2tp_debugfs_init(void)
  279. {
  280. rootdir = debugfs_create_dir("l2tp", NULL);
  281. debugfs_create_file("tunnels", 0600, rootdir, NULL, &l2tp_dfs_fops);
  282. pr_info("L2TP debugfs support\n");
  283. return 0;
  284. }
  285. static void __exit l2tp_debugfs_exit(void)
  286. {
  287. debugfs_remove_recursive(rootdir);
  288. }
  289. module_init(l2tp_debugfs_init);
  290. module_exit(l2tp_debugfs_exit);
  291. MODULE_LICENSE("GPL");
  292. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  293. MODULE_DESCRIPTION("L2TP debugfs driver");
  294. MODULE_VERSION("1.0");