12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
220dcb110STom Parkin /* L2TP subsystem debugfs
30ad66140SJames Chapman *
40ad66140SJames Chapman * Copyright (c) 2010 Katalix Systems Ltd
50ad66140SJames Chapman */
60ad66140SJames Chapman
7a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8a4ca44faSJoe Perches
90ad66140SJames Chapman #include <linux/module.h>
100ad66140SJames Chapman #include <linux/skbuff.h>
110ad66140SJames Chapman #include <linux/socket.h>
120ad66140SJames Chapman #include <linux/hash.h>
130ad66140SJames Chapman #include <linux/l2tp.h>
140ad66140SJames Chapman #include <linux/in.h>
150ad66140SJames Chapman #include <linux/etherdevice.h>
160ad66140SJames Chapman #include <linux/spinlock.h>
170ad66140SJames Chapman #include <linux/debugfs.h>
180ad66140SJames Chapman #include <net/sock.h>
190ad66140SJames Chapman #include <net/ip.h>
200ad66140SJames Chapman #include <net/icmp.h>
210ad66140SJames Chapman #include <net/udp.h>
220ad66140SJames Chapman #include <net/inet_common.h>
230ad66140SJames Chapman #include <net/inet_hashtables.h>
240ad66140SJames Chapman #include <net/tcp_states.h>
250ad66140SJames Chapman #include <net/protocol.h>
260ad66140SJames Chapman #include <net/xfrm.h>
270ad66140SJames Chapman #include <net/net_namespace.h>
280ad66140SJames Chapman #include <net/netns/generic.h>
290ad66140SJames Chapman
300ad66140SJames Chapman #include "l2tp_core.h"
310ad66140SJames Chapman
320ad66140SJames Chapman static struct dentry *rootdir;
330ad66140SJames Chapman
340ad66140SJames Chapman struct l2tp_dfs_seq_data {
350ad66140SJames Chapman struct net *net;
36285ec2feSEric Dumazet netns_tracker ns_tracker;
371f4c3dceSJames Chapman unsigned long tkey; /* lookup key of current tunnel */
381f4c3dceSJames Chapman unsigned long skey; /* lookup key of current session */
390ad66140SJames Chapman struct l2tp_tunnel *tunnel;
400ad66140SJames Chapman struct l2tp_session *session; /* NULL means get next tunnel */
410ad66140SJames Chapman };
420ad66140SJames Chapman
l2tp_dfs_next_tunnel(struct l2tp_dfs_seq_data * pd)430ad66140SJames Chapman static void l2tp_dfs_next_tunnel(struct l2tp_dfs_seq_data *pd)
440ad66140SJames Chapman {
45f726214dSGuillaume Nault /* Drop reference taken during previous invocation */
46f726214dSGuillaume Nault if (pd->tunnel)
47*abe7a1a7SJames Chapman l2tp_tunnel_put(pd->tunnel);
48f726214dSGuillaume Nault
491f4c3dceSJames Chapman pd->tunnel = l2tp_tunnel_get_next(pd->net, &pd->tkey);
501f4c3dceSJames Chapman pd->tkey++;
510ad66140SJames Chapman }
520ad66140SJames Chapman
l2tp_dfs_next_session(struct l2tp_dfs_seq_data * pd)530ad66140SJames Chapman static void l2tp_dfs_next_session(struct l2tp_dfs_seq_data *pd)
540ad66140SJames Chapman {
5583494407SGuillaume Nault /* Drop reference taken during previous invocation */
5683494407SGuillaume Nault if (pd->session)
57*abe7a1a7SJames Chapman l2tp_session_put(pd->session);
5883494407SGuillaume Nault
591f4c3dceSJames Chapman pd->session = l2tp_session_get_next(pd->net, pd->tunnel->sock,
601f4c3dceSJames Chapman pd->tunnel->version,
611f4c3dceSJames Chapman pd->tunnel->tunnel_id, &pd->skey);
621f4c3dceSJames Chapman pd->skey++;
630ad66140SJames Chapman
640febc7b3STom Parkin if (!pd->session) {
651f4c3dceSJames Chapman pd->skey = 0;
660ad66140SJames Chapman l2tp_dfs_next_tunnel(pd);
670ad66140SJames Chapman }
680ad66140SJames Chapman }
690ad66140SJames Chapman
l2tp_dfs_seq_start(struct seq_file * m,loff_t * offs)700ad66140SJames Chapman static void *l2tp_dfs_seq_start(struct seq_file *m, loff_t *offs)
710ad66140SJames Chapman {
720ad66140SJames Chapman struct l2tp_dfs_seq_data *pd = SEQ_START_TOKEN;
730ad66140SJames Chapman loff_t pos = *offs;
740ad66140SJames Chapman
750ad66140SJames Chapman if (!pos)
760ad66140SJames Chapman goto out;
770ad66140SJames Chapman
787a379558STom Parkin if (WARN_ON(!m->private)) {
797a379558STom Parkin pd = NULL;
807a379558STom Parkin goto out;
817a379558STom Parkin }
820ad66140SJames Chapman pd = m->private;
830ad66140SJames Chapman
840febc7b3STom Parkin if (!pd->tunnel)
850ad66140SJames Chapman l2tp_dfs_next_tunnel(pd);
860ad66140SJames Chapman else
870ad66140SJames Chapman l2tp_dfs_next_session(pd);
880ad66140SJames Chapman
890ad66140SJames Chapman /* NULL tunnel and session indicates end of list */
900febc7b3STom Parkin if (!pd->tunnel && !pd->session)
910ad66140SJames Chapman pd = NULL;
920ad66140SJames Chapman
930ad66140SJames Chapman out:
940ad66140SJames Chapman return pd;
950ad66140SJames Chapman }
960ad66140SJames Chapman
l2tp_dfs_seq_next(struct seq_file * m,void * v,loff_t * pos)970ad66140SJames Chapman static void *l2tp_dfs_seq_next(struct seq_file *m, void *v, loff_t *pos)
980ad66140SJames Chapman {
990ad66140SJames Chapman (*pos)++;
1000ad66140SJames Chapman return NULL;
1010ad66140SJames Chapman }
1020ad66140SJames Chapman
l2tp_dfs_seq_stop(struct seq_file * p,void * v)1030ad66140SJames Chapman static void l2tp_dfs_seq_stop(struct seq_file *p, void *v)
1040ad66140SJames Chapman {
105f726214dSGuillaume Nault struct l2tp_dfs_seq_data *pd = v;
106f726214dSGuillaume Nault
107f726214dSGuillaume Nault if (!pd || pd == SEQ_START_TOKEN)
108f726214dSGuillaume Nault return;
109f726214dSGuillaume Nault
11083494407SGuillaume Nault /* Drop reference taken by last invocation of l2tp_dfs_next_session()
11183494407SGuillaume Nault * or l2tp_dfs_next_tunnel().
11283494407SGuillaume Nault */
11383494407SGuillaume Nault if (pd->session) {
114*abe7a1a7SJames Chapman l2tp_session_put(pd->session);
11583494407SGuillaume Nault pd->session = NULL;
11683494407SGuillaume Nault }
1175411b618SGuillaume Nault if (pd->tunnel) {
118*abe7a1a7SJames Chapman l2tp_tunnel_put(pd->tunnel);
1195411b618SGuillaume Nault pd->tunnel = NULL;
1205411b618SGuillaume Nault }
1210ad66140SJames Chapman }
1220ad66140SJames Chapman
l2tp_dfs_seq_tunnel_show(struct seq_file * m,void * v)1230ad66140SJames Chapman static void l2tp_dfs_seq_tunnel_show(struct seq_file *m, void *v)
1240ad66140SJames Chapman {
1250ad66140SJames Chapman struct l2tp_tunnel *tunnel = v;
12607b8ca37STom Parkin struct l2tp_session *session;
1270ad66140SJames Chapman int session_count = 0;
1280ad66140SJames Chapman
12907b8ca37STom Parkin rcu_read_lock_bh();
130d18d3f0aSJames Chapman list_for_each_entry_rcu(session, &tunnel->session_list, list) {
13107b8ca37STom Parkin /* Session ID of zero is a dummy/reserved value used by pppol2tp */
1320ad66140SJames Chapman if (session->session_id == 0)
1330ad66140SJames Chapman continue;
1340ad66140SJames Chapman
1350ad66140SJames Chapman session_count++;
1360ad66140SJames Chapman }
13707b8ca37STom Parkin rcu_read_unlock_bh();
1380ad66140SJames Chapman
1390ad66140SJames Chapman seq_printf(m, "\nTUNNEL %u peer %u", tunnel->tunnel_id, tunnel->peer_tunnel_id);
1400ad66140SJames Chapman if (tunnel->sock) {
1410ad66140SJames Chapman struct inet_sock *inet = inet_sk(tunnel->sock);
1422121c3f5SChris Elston
1432121c3f5SChris Elston #if IS_ENABLED(CONFIG_IPV6)
1442121c3f5SChris Elston if (tunnel->sock->sk_family == AF_INET6) {
145efe4208fSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
146efe4208fSEric Dumazet
1472121c3f5SChris Elston seq_printf(m, " from %pI6c to %pI6c\n",
148efe4208fSEric Dumazet &np->saddr, &tunnel->sock->sk_v6_daddr);
14926d9a271STom Parkin }
1502121c3f5SChris Elston #endif
15126d9a271STom Parkin if (tunnel->sock->sk_family == AF_INET)
152a4fbf841SJoe Perches seq_printf(m, " from %pI4 to %pI4\n",
153a4fbf841SJoe Perches &inet->inet_saddr, &inet->inet_daddr);
15426d9a271STom Parkin
1550ad66140SJames Chapman if (tunnel->encap == L2TP_ENCAPTYPE_UDP)
1560ad66140SJames Chapman seq_printf(m, " source port %hu, dest port %hu\n",
1570ad66140SJames Chapman ntohs(inet->inet_sport), ntohs(inet->inet_dport));
1580ad66140SJames Chapman }
1590ad66140SJames Chapman seq_printf(m, " L2TPv%d, %s\n", tunnel->version,
1600ad66140SJames Chapman tunnel->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
1610ad66140SJames Chapman tunnel->encap == L2TP_ENCAPTYPE_IP ? "IP" :
1620ad66140SJames Chapman "");
1630ad66140SJames Chapman seq_printf(m, " %d sessions, refcnt %d/%d\n", session_count,
16441c6d650SReshetova, Elena tunnel->sock ? refcount_read(&tunnel->sock->sk_refcnt) : 0,
165fbea9e07SReshetova, Elena refcount_read(&tunnel->ref_count));
1667b7c0719STom Parkin seq_printf(m, " %08x rx %ld/%ld/%ld rx %ld/%ld/%ld\n",
167eee049c0STom Parkin 0,
1687b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_packets),
1697b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_bytes),
1707b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_errors),
1717b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_packets),
1727b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_bytes),
1737b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_errors));
1740ad66140SJames Chapman }
1750ad66140SJames Chapman
l2tp_dfs_seq_session_show(struct seq_file * m,void * v)1760ad66140SJames Chapman static void l2tp_dfs_seq_session_show(struct seq_file *m, void *v)
1770ad66140SJames Chapman {
1780ad66140SJames Chapman struct l2tp_session *session = v;
1790ad66140SJames Chapman
1800ad66140SJames Chapman seq_printf(m, " SESSION %u, peer %u, %s\n", session->session_id,
1810ad66140SJames Chapman session->peer_session_id,
1820ad66140SJames Chapman session->pwtype == L2TP_PWTYPE_ETH ? "ETH" :
1830ad66140SJames Chapman session->pwtype == L2TP_PWTYPE_PPP ? "PPP" :
1840ad66140SJames Chapman "");
1850ad66140SJames Chapman if (session->send_seq || session->recv_seq)
1869d899dbeSJustin Stitt seq_printf(m, " nr %u, ns %u\n", session->nr, session->ns);
187fbea9e07SReshetova, Elena seq_printf(m, " refcnt %d\n", refcount_read(&session->ref_count));
188e9697e2eSGuillaume Nault seq_printf(m, " config 0/0/%c/%c/-/%s %08x %u\n",
1890ad66140SJames Chapman session->recv_seq ? 'R' : '-',
1900ad66140SJames Chapman session->send_seq ? 'S' : '-',
1910ad66140SJames Chapman session->lns_mode ? "LNS" : "LAC",
192eee049c0STom Parkin 0,
1930ad66140SJames Chapman jiffies_to_msecs(session->reorder_timeout));
1949d899dbeSJustin Stitt seq_printf(m, " offset 0 l2specific %hu/%d\n",
1959afa6585SLorenzo Bianconi session->l2specific_type, l2tp_get_l2specific_len(session));
1960ad66140SJames Chapman if (session->cookie_len) {
1970ad66140SJames Chapman seq_printf(m, " cookie %02x%02x%02x%02x",
1980ad66140SJames Chapman session->cookie[0], session->cookie[1],
1990ad66140SJames Chapman session->cookie[2], session->cookie[3]);
2000ad66140SJames Chapman if (session->cookie_len == 8)
2010ad66140SJames Chapman seq_printf(m, "%02x%02x%02x%02x",
2020ad66140SJames Chapman session->cookie[4], session->cookie[5],
2030ad66140SJames Chapman session->cookie[6], session->cookie[7]);
204bdf9866eSTom Parkin seq_puts(m, "\n");
2050ad66140SJames Chapman }
2060ad66140SJames Chapman if (session->peer_cookie_len) {
2070ad66140SJames Chapman seq_printf(m, " peer cookie %02x%02x%02x%02x",
2080ad66140SJames Chapman session->peer_cookie[0], session->peer_cookie[1],
2090ad66140SJames Chapman session->peer_cookie[2], session->peer_cookie[3]);
2100ad66140SJames Chapman if (session->peer_cookie_len == 8)
2110ad66140SJames Chapman seq_printf(m, "%02x%02x%02x%02x",
2120ad66140SJames Chapman session->peer_cookie[4], session->peer_cookie[5],
2130ad66140SJames Chapman session->peer_cookie[6], session->peer_cookie[7]);
214bdf9866eSTom Parkin seq_puts(m, "\n");
2150ad66140SJames Chapman }
2160ad66140SJames Chapman
2179d899dbeSJustin Stitt seq_printf(m, " %u/%u tx %ld/%ld/%ld rx %ld/%ld/%ld\n",
2180ad66140SJames Chapman session->nr, session->ns,
2197b7c0719STom Parkin atomic_long_read(&session->stats.tx_packets),
2207b7c0719STom Parkin atomic_long_read(&session->stats.tx_bytes),
2217b7c0719STom Parkin atomic_long_read(&session->stats.tx_errors),
2227b7c0719STom Parkin atomic_long_read(&session->stats.rx_packets),
2237b7c0719STom Parkin atomic_long_read(&session->stats.rx_bytes),
2247b7c0719STom Parkin atomic_long_read(&session->stats.rx_errors));
2250ad66140SJames Chapman
2260febc7b3STom Parkin if (session->show)
2270ad66140SJames Chapman session->show(m, session);
2280ad66140SJames Chapman }
2290ad66140SJames Chapman
l2tp_dfs_seq_show(struct seq_file * m,void * v)2300ad66140SJames Chapman static int l2tp_dfs_seq_show(struct seq_file *m, void *v)
2310ad66140SJames Chapman {
2320ad66140SJames Chapman struct l2tp_dfs_seq_data *pd = v;
2330ad66140SJames Chapman
2340ad66140SJames Chapman /* display header on line 1 */
2350ad66140SJames Chapman if (v == SEQ_START_TOKEN) {
2360ad66140SJames Chapman seq_puts(m, "TUNNEL ID, peer ID from IP to IP\n");
2370ad66140SJames Chapman seq_puts(m, " L2TPv2/L2TPv3, UDP/IP\n");
2380ad66140SJames Chapman seq_puts(m, " sessions session-count, refcnt refcnt/sk->refcnt\n");
2390ad66140SJames Chapman seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2400ad66140SJames Chapman seq_puts(m, " SESSION ID, peer ID, PWTYPE\n");
2410ad66140SJames Chapman seq_puts(m, " refcnt cnt\n");
242863def15SJames Chapman seq_puts(m, " offset OFFSET l2specific TYPE/LEN\n");
2430ad66140SJames Chapman seq_puts(m, " [ cookie ]\n");
2440ad66140SJames Chapman seq_puts(m, " [ peer cookie ]\n");
2450ad66140SJames Chapman seq_puts(m, " config mtu/mru/rcvseq/sendseq/dataseq/lns debug reorderto\n");
2460ad66140SJames Chapman seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2470ad66140SJames Chapman goto out;
2480ad66140SJames Chapman }
2490ad66140SJames Chapman
25083494407SGuillaume Nault if (!pd->session)
2510ad66140SJames Chapman l2tp_dfs_seq_tunnel_show(m, pd->tunnel);
25283494407SGuillaume Nault else
2530ad66140SJames Chapman l2tp_dfs_seq_session_show(m, pd->session);
2540ad66140SJames Chapman
2550ad66140SJames Chapman out:
2560ad66140SJames Chapman return 0;
2570ad66140SJames Chapman }
2580ad66140SJames Chapman
2590ad66140SJames Chapman static const struct seq_operations l2tp_dfs_seq_ops = {
2600ad66140SJames Chapman .start = l2tp_dfs_seq_start,
2610ad66140SJames Chapman .next = l2tp_dfs_seq_next,
2620ad66140SJames Chapman .stop = l2tp_dfs_seq_stop,
2630ad66140SJames Chapman .show = l2tp_dfs_seq_show,
2640ad66140SJames Chapman };
2650ad66140SJames Chapman
l2tp_dfs_seq_open(struct inode * inode,struct file * file)2660ad66140SJames Chapman static int l2tp_dfs_seq_open(struct inode *inode, struct file *file)
2670ad66140SJames Chapman {
2680ad66140SJames Chapman struct l2tp_dfs_seq_data *pd;
2690ad66140SJames Chapman struct seq_file *seq;
2700ad66140SJames Chapman int rc = -ENOMEM;
2710ad66140SJames Chapman
2726f9b9018SDr. David Alan Gilbert pd = kzalloc(sizeof(*pd), GFP_KERNEL);
2730febc7b3STom Parkin if (!pd)
2740ad66140SJames Chapman goto out;
2750ad66140SJames Chapman
2760ad66140SJames Chapman /* Derive the network namespace from the pid opening the
2770ad66140SJames Chapman * file.
2780ad66140SJames Chapman */
2790ad66140SJames Chapman pd->net = get_net_ns_by_pid(current->pid);
2800ad66140SJames Chapman if (IS_ERR(pd->net)) {
281b8f07a06SAl Viro rc = PTR_ERR(pd->net);
2820ad66140SJames Chapman goto err_free_pd;
2830ad66140SJames Chapman }
284285ec2feSEric Dumazet netns_tracker_alloc(pd->net, &pd->ns_tracker, GFP_KERNEL);
2850ad66140SJames Chapman rc = seq_open(file, &l2tp_dfs_seq_ops);
2860ad66140SJames Chapman if (rc)
2870ad66140SJames Chapman goto err_free_net;
2880ad66140SJames Chapman
2890ad66140SJames Chapman seq = file->private_data;
2900ad66140SJames Chapman seq->private = pd;
2910ad66140SJames Chapman
2920ad66140SJames Chapman out:
2930ad66140SJames Chapman return rc;
2940ad66140SJames Chapman
2950ad66140SJames Chapman err_free_net:
296285ec2feSEric Dumazet put_net_track(pd->net, &pd->ns_tracker);
2970ad66140SJames Chapman err_free_pd:
2980ad66140SJames Chapman kfree(pd);
2990ad66140SJames Chapman goto out;
3000ad66140SJames Chapman }
3010ad66140SJames Chapman
l2tp_dfs_seq_release(struct inode * inode,struct file * file)3020ad66140SJames Chapman static int l2tp_dfs_seq_release(struct inode *inode, struct file *file)
3030ad66140SJames Chapman {
3040ad66140SJames Chapman struct l2tp_dfs_seq_data *pd;
3050ad66140SJames Chapman struct seq_file *seq;
3060ad66140SJames Chapman
3070ad66140SJames Chapman seq = file->private_data;
3080ad66140SJames Chapman pd = seq->private;
3090ad66140SJames Chapman if (pd->net)
310285ec2feSEric Dumazet put_net_track(pd->net, &pd->ns_tracker);
3110ad66140SJames Chapman kfree(pd);
3120ad66140SJames Chapman seq_release(inode, file);
3130ad66140SJames Chapman
3140ad66140SJames Chapman return 0;
3150ad66140SJames Chapman }
3160ad66140SJames Chapman
3170ad66140SJames Chapman static const struct file_operations l2tp_dfs_fops = {
3180ad66140SJames Chapman .owner = THIS_MODULE,
3190ad66140SJames Chapman .open = l2tp_dfs_seq_open,
3200ad66140SJames Chapman .read = seq_read,
3210ad66140SJames Chapman .llseek = seq_lseek,
3220ad66140SJames Chapman .release = l2tp_dfs_seq_release,
3230ad66140SJames Chapman };
3240ad66140SJames Chapman
l2tp_debugfs_init(void)3250ad66140SJames Chapman static int __init l2tp_debugfs_init(void)
3260ad66140SJames Chapman {
3270ad66140SJames Chapman rootdir = debugfs_create_dir("l2tp", NULL);
3280ad66140SJames Chapman
3293adcfa44SGreg Kroah-Hartman debugfs_create_file("tunnels", 0600, rootdir, NULL, &l2tp_dfs_fops);
3300ad66140SJames Chapman
331a4ca44faSJoe Perches pr_info("L2TP debugfs support\n");
3320ad66140SJames Chapman
3333adcfa44SGreg Kroah-Hartman return 0;
3340ad66140SJames Chapman }
3350ad66140SJames Chapman
l2tp_debugfs_exit(void)3360ad66140SJames Chapman static void __exit l2tp_debugfs_exit(void)
3370ad66140SJames Chapman {
3383adcfa44SGreg Kroah-Hartman debugfs_remove_recursive(rootdir);
3390ad66140SJames Chapman }
3400ad66140SJames Chapman
3410ad66140SJames Chapman module_init(l2tp_debugfs_init);
3420ad66140SJames Chapman module_exit(l2tp_debugfs_exit);
3430ad66140SJames Chapman
3440ad66140SJames Chapman MODULE_LICENSE("GPL");
3450ad66140SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
3460ad66140SJames Chapman MODULE_DESCRIPTION("L2TP debugfs driver");
3470ad66140SJames Chapman MODULE_VERSION("1.0");
348