l2tp_debugfs.c 9.2 KB

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