1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	X.25 Packet Layer release 002
4  *
5  *	This is ALPHA test software. This code may break your machine,
6  *	randomly fail to work with new releases, misbehave and/or generally
7  *	screw up. It might even work.
8  *
9  *	This code REQUIRES 2.4 with seq_file support
10  *
11  *	History
12  *	2002/10/06	Arnaldo Carvalho de Melo  seq_file support
13  */
14 
15 #include <linux/init.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/export.h>
19 #include <net/net_namespace.h>
20 #include <net/sock.h>
21 #include <net/x25.h>
22 
23 #ifdef CONFIG_PROC_FS
24 
x25_seq_route_start(struct seq_file * seq,loff_t * pos)25 static void *x25_seq_route_start(struct seq_file *seq, loff_t *pos)
26 	__acquires(x25_route_list_lock)
27 {
28 	read_lock_bh(&x25_route_list_lock);
29 	return seq_list_start_head(&x25_route_list, *pos);
30 }
31 
x25_seq_route_next(struct seq_file * seq,void * v,loff_t * pos)32 static void *x25_seq_route_next(struct seq_file *seq, void *v, loff_t *pos)
33 {
34 	return seq_list_next(v, &x25_route_list, pos);
35 }
36 
x25_seq_route_stop(struct seq_file * seq,void * v)37 static void x25_seq_route_stop(struct seq_file *seq, void *v)
38 	__releases(x25_route_list_lock)
39 {
40 	read_unlock_bh(&x25_route_list_lock);
41 }
42 
x25_seq_route_show(struct seq_file * seq,void * v)43 static int x25_seq_route_show(struct seq_file *seq, void *v)
44 {
45 	struct x25_route *rt = list_entry(v, struct x25_route, node);
46 
47 	if (v == &x25_route_list) {
48 		seq_puts(seq, "Address          Digits  Device\n");
49 		goto out;
50 	}
51 
52 	rt = v;
53 	seq_printf(seq, "%-15s  %-6d  %-5s\n",
54 		   rt->address.x25_addr, rt->sigdigits,
55 		   rt->dev ? rt->dev->name : "???");
56 out:
57 	return 0;
58 }
59 
x25_seq_socket_start(struct seq_file * seq,loff_t * pos)60 static void *x25_seq_socket_start(struct seq_file *seq, loff_t *pos)
61 	__acquires(x25_list_lock)
62 {
63 	read_lock_bh(&x25_list_lock);
64 	return seq_hlist_start_head(&x25_list, *pos);
65 }
66 
x25_seq_socket_next(struct seq_file * seq,void * v,loff_t * pos)67 static void *x25_seq_socket_next(struct seq_file *seq, void *v, loff_t *pos)
68 {
69 	return seq_hlist_next(v, &x25_list, pos);
70 }
71 
x25_seq_socket_stop(struct seq_file * seq,void * v)72 static void x25_seq_socket_stop(struct seq_file *seq, void *v)
73 	__releases(x25_list_lock)
74 {
75 	read_unlock_bh(&x25_list_lock);
76 }
77 
x25_seq_socket_show(struct seq_file * seq,void * v)78 static int x25_seq_socket_show(struct seq_file *seq, void *v)
79 {
80 	struct sock *s;
81 	struct x25_sock *x25;
82 	const char *devname;
83 
84 	if (v == SEQ_START_TOKEN) {
85 		seq_printf(seq, "dest_addr  src_addr   dev   lci st vs vr "
86 				"va   t  t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
87 		goto out;
88 	}
89 
90 	s = sk_entry(v);
91 	x25 = x25_sk(s);
92 
93 	if (!x25->neighbour || !x25->neighbour->dev)
94 		devname = "???";
95 	else
96 		devname = x25->neighbour->dev->name;
97 
98 	seq_printf(seq, "%-10s %-10s %-5s %3.3X  %d  %d  %d  %d %3lu %3lu "
99 			"%3lu %3lu %3lu %5d %5d %ld\n",
100 		   !x25->dest_addr.x25_addr[0] ? "*" : x25->dest_addr.x25_addr,
101 		   !x25->source_addr.x25_addr[0] ? "*" : x25->source_addr.x25_addr,
102 		   devname, x25->lci & 0x0FFF, x25->state, x25->vs, x25->vr,
103 		   x25->va, x25_display_timer(s) / HZ, x25->t2  / HZ,
104 		   x25->t21 / HZ, x25->t22 / HZ, x25->t23 / HZ,
105 		   sk_wmem_alloc_get(s),
106 		   sk_rmem_alloc_get(s),
107 		   s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
108 out:
109 	return 0;
110 }
111 
x25_seq_forward_start(struct seq_file * seq,loff_t * pos)112 static void *x25_seq_forward_start(struct seq_file *seq, loff_t *pos)
113 	__acquires(x25_forward_list_lock)
114 {
115 	read_lock_bh(&x25_forward_list_lock);
116 	return seq_list_start_head(&x25_forward_list, *pos);
117 }
118 
x25_seq_forward_next(struct seq_file * seq,void * v,loff_t * pos)119 static void *x25_seq_forward_next(struct seq_file *seq, void *v, loff_t *pos)
120 {
121 	return seq_list_next(v, &x25_forward_list, pos);
122 }
123 
x25_seq_forward_stop(struct seq_file * seq,void * v)124 static void x25_seq_forward_stop(struct seq_file *seq, void *v)
125 	__releases(x25_forward_list_lock)
126 {
127 	read_unlock_bh(&x25_forward_list_lock);
128 }
129 
x25_seq_forward_show(struct seq_file * seq,void * v)130 static int x25_seq_forward_show(struct seq_file *seq, void *v)
131 {
132 	struct x25_forward *f = list_entry(v, struct x25_forward, node);
133 
134 	if (v == &x25_forward_list) {
135 		seq_printf(seq, "lci dev1       dev2\n");
136 		goto out;
137 	}
138 
139 	f = v;
140 
141 	seq_printf(seq, "%d %-10s %-10s\n",
142 			f->lci, f->dev1->name, f->dev2->name);
143 out:
144 	return 0;
145 }
146 
147 static const struct seq_operations x25_seq_route_ops = {
148 	.start  = x25_seq_route_start,
149 	.next   = x25_seq_route_next,
150 	.stop   = x25_seq_route_stop,
151 	.show   = x25_seq_route_show,
152 };
153 
154 static const struct seq_operations x25_seq_socket_ops = {
155 	.start  = x25_seq_socket_start,
156 	.next   = x25_seq_socket_next,
157 	.stop   = x25_seq_socket_stop,
158 	.show   = x25_seq_socket_show,
159 };
160 
161 static const struct seq_operations x25_seq_forward_ops = {
162 	.start  = x25_seq_forward_start,
163 	.next   = x25_seq_forward_next,
164 	.stop   = x25_seq_forward_stop,
165 	.show   = x25_seq_forward_show,
166 };
167 
x25_proc_init(void)168 int __init x25_proc_init(void)
169 {
170 	if (!proc_mkdir("x25", init_net.proc_net))
171 		return -ENOMEM;
172 
173 	if (!proc_create_seq("x25/route", 0444, init_net.proc_net,
174 			 &x25_seq_route_ops))
175 		goto out;
176 
177 	if (!proc_create_seq("x25/socket", 0444, init_net.proc_net,
178 			 &x25_seq_socket_ops))
179 		goto out;
180 
181 	if (!proc_create_seq("x25/forward", 0444, init_net.proc_net,
182 			 &x25_seq_forward_ops))
183 		goto out;
184 	return 0;
185 
186 out:
187 	remove_proc_subtree("x25", init_net.proc_net);
188 	return -ENOMEM;
189 }
190 
x25_proc_exit(void)191 void __exit x25_proc_exit(void)
192 {
193 	remove_proc_subtree("x25", init_net.proc_net);
194 }
195 
196 #else /* CONFIG_PROC_FS */
197 
x25_proc_init(void)198 int __init x25_proc_init(void)
199 {
200 	return 0;
201 }
202 
x25_proc_exit(void)203 void __exit x25_proc_exit(void)
204 {
205 }
206 #endif /* CONFIG_PROC_FS */
207