Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * atalk_proc.c - proc support for Appletalk |
| 3 | * |
| 4 | * Copyright(c) Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation, version 2. |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/proc_fs.h> |
| 13 | #include <linux/seq_file.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 14 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <net/sock.h> |
| 16 | #include <linux/atalk.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | static __inline__ struct atalk_iface *atalk_get_interface_idx(loff_t pos) |
| 21 | { |
| 22 | struct atalk_iface *i; |
| 23 | |
| 24 | for (i = atalk_interfaces; pos && i; i = i->next) |
| 25 | --pos; |
| 26 | |
| 27 | return i; |
| 28 | } |
| 29 | |
| 30 | static void *atalk_seq_interface_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 31 | __acquires(atalk_interfaces_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | loff_t l = *pos; |
| 34 | |
| 35 | read_lock_bh(&atalk_interfaces_lock); |
| 36 | return l ? atalk_get_interface_idx(--l) : SEQ_START_TOKEN; |
| 37 | } |
| 38 | |
| 39 | static void *atalk_seq_interface_next(struct seq_file *seq, void *v, loff_t *pos) |
| 40 | { |
| 41 | struct atalk_iface *i; |
| 42 | |
| 43 | ++*pos; |
| 44 | if (v == SEQ_START_TOKEN) { |
| 45 | i = NULL; |
| 46 | if (atalk_interfaces) |
| 47 | i = atalk_interfaces; |
| 48 | goto out; |
| 49 | } |
| 50 | i = v; |
| 51 | i = i->next; |
| 52 | out: |
| 53 | return i; |
| 54 | } |
| 55 | |
| 56 | static void atalk_seq_interface_stop(struct seq_file *seq, void *v) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 57 | __releases(atalk_interfaces_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | read_unlock_bh(&atalk_interfaces_lock); |
| 60 | } |
| 61 | |
| 62 | static int atalk_seq_interface_show(struct seq_file *seq, void *v) |
| 63 | { |
| 64 | struct atalk_iface *iface; |
| 65 | |
| 66 | if (v == SEQ_START_TOKEN) { |
| 67 | seq_puts(seq, "Interface Address Networks " |
| 68 | "Status\n"); |
| 69 | goto out; |
| 70 | } |
| 71 | |
| 72 | iface = v; |
| 73 | seq_printf(seq, "%-16s %04X:%02X %04X-%04X %d\n", |
| 74 | iface->dev->name, ntohs(iface->address.s_net), |
| 75 | iface->address.s_node, ntohs(iface->nets.nr_firstnet), |
| 76 | ntohs(iface->nets.nr_lastnet), iface->status); |
| 77 | out: |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static __inline__ struct atalk_route *atalk_get_route_idx(loff_t pos) |
| 82 | { |
| 83 | struct atalk_route *r; |
| 84 | |
| 85 | for (r = atalk_routes; pos && r; r = r->next) |
| 86 | --pos; |
| 87 | |
| 88 | return r; |
| 89 | } |
| 90 | |
| 91 | static void *atalk_seq_route_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 92 | __acquires(atalk_routes_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | loff_t l = *pos; |
| 95 | |
| 96 | read_lock_bh(&atalk_routes_lock); |
| 97 | return l ? atalk_get_route_idx(--l) : SEQ_START_TOKEN; |
| 98 | } |
| 99 | |
| 100 | static void *atalk_seq_route_next(struct seq_file *seq, void *v, loff_t *pos) |
| 101 | { |
| 102 | struct atalk_route *r; |
| 103 | |
| 104 | ++*pos; |
| 105 | if (v == SEQ_START_TOKEN) { |
| 106 | r = NULL; |
| 107 | if (atalk_routes) |
| 108 | r = atalk_routes; |
| 109 | goto out; |
| 110 | } |
| 111 | r = v; |
| 112 | r = r->next; |
| 113 | out: |
| 114 | return r; |
| 115 | } |
| 116 | |
| 117 | static void atalk_seq_route_stop(struct seq_file *seq, void *v) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 118 | __releases(atalk_routes_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | read_unlock_bh(&atalk_routes_lock); |
| 121 | } |
| 122 | |
| 123 | static int atalk_seq_route_show(struct seq_file *seq, void *v) |
| 124 | { |
| 125 | struct atalk_route *rt; |
| 126 | |
| 127 | if (v == SEQ_START_TOKEN) { |
| 128 | seq_puts(seq, "Target Router Flags Dev\n"); |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | if (atrtr_default.dev) { |
| 133 | rt = &atrtr_default; |
| 134 | seq_printf(seq, "Default %04X:%02X %-4d %s\n", |
| 135 | ntohs(rt->gateway.s_net), rt->gateway.s_node, |
| 136 | rt->flags, rt->dev->name); |
| 137 | } |
| 138 | |
| 139 | rt = v; |
| 140 | seq_printf(seq, "%04X:%02X %04X:%02X %-4d %s\n", |
| 141 | ntohs(rt->target.s_net), rt->target.s_node, |
| 142 | ntohs(rt->gateway.s_net), rt->gateway.s_node, |
| 143 | rt->flags, rt->dev->name); |
| 144 | out: |
| 145 | return 0; |
| 146 | } |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | static void *atalk_seq_socket_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 149 | __acquires(atalk_sockets_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | read_lock_bh(&atalk_sockets_lock); |
Li Zefan | efaffb7 | 2010-02-08 23:20:15 +0000 | [diff] [blame] | 152 | return seq_hlist_start_head(&atalk_sockets, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void *atalk_seq_socket_next(struct seq_file *seq, void *v, loff_t *pos) |
| 156 | { |
Li Zefan | efaffb7 | 2010-02-08 23:20:15 +0000 | [diff] [blame] | 157 | return seq_hlist_next(v, &atalk_sockets, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void atalk_seq_socket_stop(struct seq_file *seq, void *v) |
Eric Dumazet | ca629f24 | 2008-01-15 03:28:43 -0800 | [diff] [blame] | 161 | __releases(atalk_sockets_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | read_unlock_bh(&atalk_sockets_lock); |
| 164 | } |
| 165 | |
| 166 | static int atalk_seq_socket_show(struct seq_file *seq, void *v) |
| 167 | { |
| 168 | struct sock *s; |
| 169 | struct atalk_sock *at; |
| 170 | |
| 171 | if (v == SEQ_START_TOKEN) { |
| 172 | seq_printf(seq, "Type Local_addr Remote_addr Tx_queue " |
| 173 | "Rx_queue St UID\n"); |
| 174 | goto out; |
| 175 | } |
| 176 | |
Li Zefan | efaffb7 | 2010-02-08 23:20:15 +0000 | [diff] [blame] | 177 | s = sk_entry(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | at = at_sk(s); |
| 179 | |
| 180 | seq_printf(seq, "%02X %04X:%02X:%02X %04X:%02X:%02X %08X:%08X " |
| 181 | "%02X %d\n", |
| 182 | s->sk_type, ntohs(at->src_net), at->src_node, at->src_port, |
| 183 | ntohs(at->dest_net), at->dest_node, at->dest_port, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 184 | sk_wmem_alloc_get(s), |
| 185 | sk_rmem_alloc_get(s), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | s->sk_state, SOCK_INODE(s->sk_socket)->i_uid); |
| 187 | out: |
| 188 | return 0; |
| 189 | } |
| 190 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 191 | static const struct seq_operations atalk_seq_interface_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .start = atalk_seq_interface_start, |
| 193 | .next = atalk_seq_interface_next, |
| 194 | .stop = atalk_seq_interface_stop, |
| 195 | .show = atalk_seq_interface_show, |
| 196 | }; |
| 197 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 198 | static const struct seq_operations atalk_seq_route_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | .start = atalk_seq_route_start, |
| 200 | .next = atalk_seq_route_next, |
| 201 | .stop = atalk_seq_route_stop, |
| 202 | .show = atalk_seq_route_show, |
| 203 | }; |
| 204 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 205 | static const struct seq_operations atalk_seq_socket_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | .start = atalk_seq_socket_start, |
| 207 | .next = atalk_seq_socket_next, |
| 208 | .stop = atalk_seq_socket_stop, |
| 209 | .show = atalk_seq_socket_show, |
| 210 | }; |
| 211 | |
| 212 | static int atalk_seq_interface_open(struct inode *inode, struct file *file) |
| 213 | { |
| 214 | return seq_open(file, &atalk_seq_interface_ops); |
| 215 | } |
| 216 | |
| 217 | static int atalk_seq_route_open(struct inode *inode, struct file *file) |
| 218 | { |
| 219 | return seq_open(file, &atalk_seq_route_ops); |
| 220 | } |
| 221 | |
| 222 | static int atalk_seq_socket_open(struct inode *inode, struct file *file) |
| 223 | { |
| 224 | return seq_open(file, &atalk_seq_socket_ops); |
| 225 | } |
| 226 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 227 | static const struct file_operations atalk_seq_interface_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | .owner = THIS_MODULE, |
| 229 | .open = atalk_seq_interface_open, |
| 230 | .read = seq_read, |
| 231 | .llseek = seq_lseek, |
| 232 | .release = seq_release, |
| 233 | }; |
| 234 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 235 | static const struct file_operations atalk_seq_route_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | .owner = THIS_MODULE, |
| 237 | .open = atalk_seq_route_open, |
| 238 | .read = seq_read, |
| 239 | .llseek = seq_lseek, |
| 240 | .release = seq_release, |
| 241 | }; |
| 242 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 243 | static const struct file_operations atalk_seq_socket_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | .owner = THIS_MODULE, |
| 245 | .open = atalk_seq_socket_open, |
| 246 | .read = seq_read, |
| 247 | .llseek = seq_lseek, |
| 248 | .release = seq_release, |
| 249 | }; |
| 250 | |
| 251 | static struct proc_dir_entry *atalk_proc_dir; |
| 252 | |
| 253 | int __init atalk_proc_init(void) |
| 254 | { |
| 255 | struct proc_dir_entry *p; |
| 256 | int rc = -ENOMEM; |
| 257 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 258 | atalk_proc_dir = proc_mkdir("atalk", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (!atalk_proc_dir) |
| 260 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Wang Chen | ed2b5b4 | 2008-02-28 12:53:32 -0800 | [diff] [blame] | 262 | p = proc_create("interface", S_IRUGO, atalk_proc_dir, |
| 263 | &atalk_seq_interface_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (!p) |
| 265 | goto out_interface; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Wang Chen | ed2b5b4 | 2008-02-28 12:53:32 -0800 | [diff] [blame] | 267 | p = proc_create("route", S_IRUGO, atalk_proc_dir, |
| 268 | &atalk_seq_route_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | if (!p) |
| 270 | goto out_route; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Wang Chen | ed2b5b4 | 2008-02-28 12:53:32 -0800 | [diff] [blame] | 272 | p = proc_create("socket", S_IRUGO, atalk_proc_dir, |
| 273 | &atalk_seq_socket_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (!p) |
| 275 | goto out_socket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Wang Chen | ed2b5b4 | 2008-02-28 12:53:32 -0800 | [diff] [blame] | 277 | p = proc_create("arp", S_IRUGO, atalk_proc_dir, &atalk_seq_arp_fops); |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 278 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | goto out_arp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | rc = 0; |
| 282 | out: |
| 283 | return rc; |
| 284 | out_arp: |
| 285 | remove_proc_entry("socket", atalk_proc_dir); |
| 286 | out_socket: |
| 287 | remove_proc_entry("route", atalk_proc_dir); |
| 288 | out_route: |
| 289 | remove_proc_entry("interface", atalk_proc_dir); |
| 290 | out_interface: |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 291 | remove_proc_entry("atalk", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | void __exit atalk_proc_exit(void) |
| 296 | { |
| 297 | remove_proc_entry("interface", atalk_proc_dir); |
| 298 | remove_proc_entry("route", atalk_proc_dir); |
| 299 | remove_proc_entry("socket", atalk_proc_dir); |
| 300 | remove_proc_entry("arp", atalk_proc_dir); |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 301 | remove_proc_entry("atalk", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |