l2tp_debugfs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. }
  152. static void l2tp_dfs_seq_session_show(struct seq_file *m, void *v)
  153. {
  154. struct l2tp_session *session = v;
  155. seq_printf(m, " SESSION %u, peer %u, %s\n", session->session_id,
  156. session->peer_session_id,
  157. session->pwtype == L2TP_PWTYPE_ETH ? "ETH" :
  158. session->pwtype == L2TP_PWTYPE_PPP ? "PPP" :
  159. "");
  160. if (session->send_seq || session->recv_seq)
  161. seq_printf(m, " nr %hu, ns %hu\n", session->nr, session->ns);
  162. seq_printf(m, " refcnt %d\n", refcount_read(&session->ref_count));
  163. seq_printf(m, " config 0/0/%c/%c/-/%s %08x %u\n",
  164. session->recv_seq ? 'R' : '-',
  165. session->send_seq ? 'S' : '-',
  166. session->lns_mode ? "LNS" : "LAC",
  167. session->debug,
  168. jiffies_to_msecs(session->reorder_timeout));
  169. seq_printf(m, " offset 0 l2specific %hu/%hu\n",
  170. session->l2specific_type, l2tp_get_l2specific_len(session));
  171. if (session->cookie_len) {
  172. seq_printf(m, " cookie %02x%02x%02x%02x",
  173. session->cookie[0], session->cookie[1],
  174. session->cookie[2], session->cookie[3]);
  175. if (session->cookie_len == 8)
  176. seq_printf(m, "%02x%02x%02x%02x",
  177. session->cookie[4], session->cookie[5],
  178. session->cookie[6], session->cookie[7]);
  179. seq_printf(m, "\n");
  180. }
  181. if (session->peer_cookie_len) {
  182. seq_printf(m, " peer cookie %02x%02x%02x%02x",
  183. session->peer_cookie[0], session->peer_cookie[1],
  184. session->peer_cookie[2], session->peer_cookie[3]);
  185. if (session->peer_cookie_len == 8)
  186. seq_printf(m, "%02x%02x%02x%02x",
  187. session->peer_cookie[4], session->peer_cookie[5],
  188. session->peer_cookie[6], session->peer_cookie[7]);
  189. seq_printf(m, "\n");
  190. }
  191. seq_printf(m, " %hu/%hu tx %ld/%ld/%ld rx %ld/%ld/%ld\n",
  192. session->nr, session->ns,
  193. atomic_long_read(&session->stats.tx_packets),
  194. atomic_long_read(&session->stats.tx_bytes),
  195. atomic_long_read(&session->stats.tx_errors),
  196. atomic_long_read(&session->stats.rx_packets),
  197. atomic_long_read(&session->stats.rx_bytes),
  198. atomic_long_read(&session->stats.rx_errors));
  199. if (session->show != NULL)
  200. session->show(m, session);
  201. }
  202. static int l2tp_dfs_seq_show(struct seq_file *m, void *v)
  203. {
  204. struct l2tp_dfs_seq_data *pd = v;
  205. /* display header on line 1 */
  206. if (v == SEQ_START_TOKEN) {
  207. seq_puts(m, "TUNNEL ID, peer ID from IP to IP\n");
  208. seq_puts(m, " L2TPv2/L2TPv3, UDP/IP\n");
  209. seq_puts(m, " sessions session-count, refcnt refcnt/sk->refcnt\n");
  210. seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  211. seq_puts(m, " SESSION ID, peer ID, PWTYPE\n");
  212. seq_puts(m, " refcnt cnt\n");
  213. seq_puts(m, " offset OFFSET l2specific TYPE/LEN\n");
  214. seq_puts(m, " [ cookie ]\n");
  215. seq_puts(m, " [ peer cookie ]\n");
  216. seq_puts(m, " config mtu/mru/rcvseq/sendseq/dataseq/lns debug reorderto\n");
  217. seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  218. goto out;
  219. }
  220. if (!pd->session)
  221. l2tp_dfs_seq_tunnel_show(m, pd->tunnel);
  222. else
  223. l2tp_dfs_seq_session_show(m, pd->session);
  224. out:
  225. return 0;
  226. }
  227. static const struct seq_operations l2tp_dfs_seq_ops = {
  228. .start = l2tp_dfs_seq_start,
  229. .next = l2tp_dfs_seq_next,
  230. .stop = l2tp_dfs_seq_stop,
  231. .show = l2tp_dfs_seq_show,
  232. };
  233. static int l2tp_dfs_seq_open(struct inode *inode, struct file *file)
  234. {
  235. struct l2tp_dfs_seq_data *pd;
  236. struct seq_file *seq;
  237. int rc = -ENOMEM;
  238. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  239. if (pd == NULL)
  240. goto out;
  241. /* Derive the network namespace from the pid opening the
  242. * file.
  243. */
  244. pd->net = get_net_ns_by_pid(current->pid);
  245. if (IS_ERR(pd->net)) {
  246. rc = PTR_ERR(pd->net);
  247. goto err_free_pd;
  248. }
  249. rc = seq_open(file, &l2tp_dfs_seq_ops);
  250. if (rc)
  251. goto err_free_net;
  252. seq = file->private_data;
  253. seq->private = pd;
  254. out:
  255. return rc;
  256. err_free_net:
  257. put_net(pd->net);
  258. err_free_pd:
  259. kfree(pd);
  260. goto out;
  261. }
  262. static int l2tp_dfs_seq_release(struct inode *inode, struct file *file)
  263. {
  264. struct l2tp_dfs_seq_data *pd;
  265. struct seq_file *seq;
  266. seq = file->private_data;
  267. pd = seq->private;
  268. if (pd->net)
  269. put_net(pd->net);
  270. kfree(pd);
  271. seq_release(inode, file);
  272. return 0;
  273. }
  274. static const struct file_operations l2tp_dfs_fops = {
  275. .owner = THIS_MODULE,
  276. .open = l2tp_dfs_seq_open,
  277. .read = seq_read,
  278. .llseek = seq_lseek,
  279. .release = l2tp_dfs_seq_release,
  280. };
  281. static int __init l2tp_debugfs_init(void)
  282. {
  283. int rc = 0;
  284. rootdir = debugfs_create_dir("l2tp", NULL);
  285. if (IS_ERR(rootdir)) {
  286. rc = PTR_ERR(rootdir);
  287. rootdir = NULL;
  288. goto out;
  289. }
  290. tunnels = debugfs_create_file("tunnels", 0600, rootdir, NULL, &l2tp_dfs_fops);
  291. if (tunnels == NULL)
  292. rc = -EIO;
  293. pr_info("L2TP debugfs support\n");
  294. out:
  295. if (rc)
  296. pr_warn("unable to init\n");
  297. return rc;
  298. }
  299. static void __exit l2tp_debugfs_exit(void)
  300. {
  301. debugfs_remove(tunnels);
  302. debugfs_remove(rootdir);
  303. }
  304. module_init(l2tp_debugfs_init);
  305. module_exit(l2tp_debugfs_exit);
  306. MODULE_LICENSE("GPL");
  307. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  308. MODULE_DESCRIPTION("L2TP debugfs driver");
  309. MODULE_VERSION("1.0");