2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "translation-table.h"
26 #include "hard-interface.h"
31 static struct proc_dir_entry *proc_batman_dir, *proc_interface_file;
32 static struct proc_dir_entry *proc_orig_interval_file, *proc_originators_file;
33 static struct proc_dir_entry *proc_transt_local_file;
34 static struct proc_dir_entry *proc_transt_global_file;
35 static struct proc_dir_entry *proc_vis_srv_file, *proc_vis_data_file;
36 static struct proc_dir_entry *proc_aggr_file;
38 static int proc_interfaces_read(struct seq_file *seq, void *offset)
40 struct batman_if *batman_if;
43 list_for_each_entry_rcu(batman_if, &if_list, list) {
44 seq_printf(seq, "[%8s] %s %s\n",
45 (batman_if->if_active == IF_ACTIVE ?
46 "active" : "inactive"),
48 (batman_if->if_active == IF_ACTIVE ?
49 batman_if->addr_str : " "));
56 static int proc_interfaces_open(struct inode *inode, struct file *file)
58 return single_open(file, proc_interfaces_read, NULL);
61 static ssize_t proc_interfaces_write(struct file *instance,
62 const char __user *userbuffer,
63 size_t count, loff_t *data)
65 char *if_string, *colon_ptr = NULL, *cr_ptr = NULL;
66 int not_copied = 0, if_num = 0, add_success;
67 struct batman_if *batman_if = NULL;
69 if_string = kmalloc(count, GFP_KERNEL);
74 if (count > IFNAMSIZ - 1) {
75 printk(KERN_WARNING "batman-adv:Can't add interface: device name is too long\n");
79 not_copied = copy_from_user(if_string, userbuffer, count);
80 if_string[count - not_copied - 1] = 0;
82 colon_ptr = strchr(if_string, ':');
87 cr_ptr = strchr(if_string, '\n');
92 if (strlen(if_string) == 0) {
100 list_for_each_entry_rcu(batman_if, &if_list, list) {
101 if (strncmp(batman_if->dev, if_string, count) == 0) {
102 printk(KERN_ERR "batman-adv:Given interface is already active: %s\n", if_string);
112 add_success = hardif_add_interface(if_string, if_num);
116 num_ifs = if_num + 1;
118 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
119 (hardif_get_active_if_num() > 0))
128 static int proc_orig_interval_read(struct seq_file *seq, void *offset)
130 seq_printf(seq, "%i\n", atomic_read(&originator_interval));
135 static ssize_t proc_orig_interval_write(struct file *file,
136 const char __user *buffer,
137 size_t count, loff_t *ppos)
139 char *interval_string;
141 unsigned long originator_interval_tmp;
144 interval_string = kmalloc(count, GFP_KERNEL);
146 if (!interval_string)
149 not_copied = copy_from_user(interval_string, buffer, count);
150 interval_string[count - not_copied - 1] = 0;
152 retval = strict_strtoul(interval_string, 10, &originator_interval_tmp);
154 printk(KERN_ERR "batman-adv:New originator interval invalid\n");
158 if (originator_interval_tmp <= JITTER * 2) {
159 printk(KERN_WARNING "batman-adv:New originator interval too small: %li (min: %i)\n",
160 originator_interval_tmp, JITTER * 2);
164 printk(KERN_INFO "batman-adv:Changing originator interval from: %i to: %li\n",
165 atomic_read(&originator_interval), originator_interval_tmp);
167 atomic_set(&originator_interval, originator_interval_tmp);
170 kfree(interval_string);
174 static int proc_orig_interval_open(struct inode *inode, struct file *file)
176 return single_open(file, proc_orig_interval_read, NULL);
179 static int proc_originators_read(struct seq_file *seq, void *offset)
182 struct orig_node *orig_node;
183 struct neigh_node *neigh_node;
184 int batman_count = 0;
185 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
189 if (list_empty(&if_list)) {
191 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it\n");
195 if (((struct batman_if *)if_list.next)->if_active != IF_ACTIVE) {
197 seq_printf(seq, "BATMAN disabled - primary interface not active\n");
202 " %-14s (%s/%i) %17s [%10s]: %20s ... [B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s]\n",
203 "Originator", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
204 "Potential nexthops", SOURCE_VERSION, REVISION_VERSION_STR,
205 ((struct batman_if *)if_list.next)->dev,
206 ((struct batman_if *)if_list.next)->addr_str);
209 spin_lock_irqsave(&orig_hash_lock, flags);
211 while (hash_iterate(orig_hash, &hashit)) {
213 orig_node = hashit.bucket->data;
215 if (!orig_node->router)
218 if (orig_node->router->tq_avg == 0)
223 addr_to_string(orig_str, orig_node->orig);
224 addr_to_string(router_str, orig_node->router->addr);
226 seq_printf(seq, "%-17s (%3i) %17s [%10s]:",
227 orig_str, orig_node->router->tq_avg,
228 router_str, orig_node->router->if_incoming->dev);
230 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
231 addr_to_string(orig_str, neigh_node->addr);
232 seq_printf(seq, " %17s (%3i)",
233 orig_str, neigh_node->tq_avg);
236 seq_printf(seq, "\n");
240 spin_unlock_irqrestore(&orig_hash_lock, flags);
242 if (batman_count == 0)
243 seq_printf(seq, "No batman nodes in range ...\n");
249 static int proc_originators_open(struct inode *inode, struct file *file)
251 return single_open(file, proc_originators_read, NULL);
254 static int proc_transt_local_read(struct seq_file *seq, void *offset)
258 buf = kmalloc(4096, GFP_KERNEL);
263 if (list_empty(&if_list)) {
265 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it\n");
271 seq_printf(seq, "Locally retrieved addresses (from %s) announced via HNA:\n", soft_device->name);
273 hna_local_fill_buffer_text(buf, 4096);
274 seq_printf(seq, "%s", buf);
281 static int proc_transt_local_open(struct inode *inode, struct file *file)
283 return single_open(file, proc_transt_local_read, NULL);
286 static int proc_transt_global_read(struct seq_file *seq, void *offset)
290 buf = kmalloc(4096, GFP_KERNEL);
295 if (list_empty(&if_list)) {
297 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it\n");
303 seq_printf(seq, "Globally announced HNAs received via the mesh (translation table):\n");
305 hna_global_fill_buffer_text(buf, 4096);
306 seq_printf(seq, "%s", buf);
313 static int proc_transt_global_open(struct inode *inode, struct file *file)
315 return single_open(file, proc_transt_global_read, NULL);
318 /* setting the mode of the vis server by the user */
319 static ssize_t proc_vis_srv_write(struct file *file, const char __user * buffer,
320 size_t count, loff_t *ppos)
322 char *vis_mode_string;
325 vis_mode_string = kmalloc(count, GFP_KERNEL);
327 if (!vis_mode_string)
330 not_copied = copy_from_user(vis_mode_string, buffer, count);
331 vis_mode_string[count - not_copied - 1] = 0;
333 if ((strcmp(vis_mode_string, "client") == 0) ||
334 (strcmp(vis_mode_string, "disabled") == 0)) {
335 printk(KERN_INFO "batman-adv:Setting VIS mode to client (disabling vis server)\n");
336 atomic_set(&vis_mode, VIS_TYPE_CLIENT_UPDATE);
337 } else if ((strcmp(vis_mode_string, "server") == 0) ||
338 (strcmp(vis_mode_string, "enabled") == 0)) {
339 printk(KERN_INFO "batman-adv:Setting VIS mode to server (enabling vis server)\n");
340 atomic_set(&vis_mode, VIS_TYPE_SERVER_SYNC);
342 printk(KERN_ERR "batman-adv:Unknown VIS mode: %s\n",
345 kfree(vis_mode_string);
349 static int proc_vis_srv_read(struct seq_file *seq, void *offset)
351 int vis_server = atomic_read(&vis_mode);
353 seq_printf(seq, "[%c] client mode (server disabled)\n",
354 (vis_server == VIS_TYPE_CLIENT_UPDATE) ? 'x' : ' ');
355 seq_printf(seq, "[%c] server mode (server enabled)\n",
356 (vis_server == VIS_TYPE_SERVER_SYNC) ? 'x' : ' ');
361 static int proc_vis_srv_open(struct inode *inode, struct file *file)
363 return single_open(file, proc_vis_srv_read, NULL);
366 static int proc_vis_data_read(struct seq_file *seq, void *offset)
369 struct vis_info *info;
370 struct vis_info_entry *entries;
371 HLIST_HEAD(vis_if_list);
372 struct if_list_entry *entry;
373 struct hlist_node *pos, *n;
375 char tmp_addr_str[ETH_STR_LEN];
377 int vis_server = atomic_read(&vis_mode);
380 if (list_empty(&if_list) || (vis_server == VIS_TYPE_CLIENT_UPDATE)) {
387 spin_lock_irqsave(&vis_hash_lock, flags);
388 while (hash_iterate(vis_hash, &hashit)) {
389 info = hashit.bucket->data;
390 entries = (struct vis_info_entry *)
391 ((char *)info + sizeof(struct vis_info));
393 for (i = 0; i < info->packet.entries; i++) {
394 if (entries[i].quality == 0)
396 proc_vis_insert_interface(entries[i].src, &vis_if_list,
397 compare_orig(entries[i].src,
398 info->packet.vis_orig));
401 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
402 addr_to_string(tmp_addr_str, entry->addr);
403 seq_printf(seq, "%s,", tmp_addr_str);
405 for (i = 0; i < info->packet.entries; i++)
406 proc_vis_read_entry(seq, &entries[i],
407 entry->addr, entry->primary);
409 /* add primary/secondary records */
410 if (compare_orig(entry->addr, info->packet.vis_orig))
411 proc_vis_read_prim_sec(seq, &vis_if_list);
413 seq_printf(seq, "\n");
416 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
417 hlist_del(&entry->list);
421 spin_unlock_irqrestore(&vis_hash_lock, flags);
427 static int proc_vis_data_open(struct inode *inode, struct file *file)
429 return single_open(file, proc_vis_data_read, NULL);
432 static int proc_aggr_read(struct seq_file *seq, void *offset)
434 seq_printf(seq, "%i\n", atomic_read(&aggregation_enabled));
439 static ssize_t proc_aggr_write(struct file *file, const char __user *buffer,
440 size_t count, loff_t *ppos)
444 unsigned long aggregation_enabled_tmp;
447 aggr_string = kmalloc(count, GFP_KERNEL);
452 not_copied = copy_from_user(aggr_string, buffer, count);
453 aggr_string[count - not_copied - 1] = 0;
455 retval = strict_strtoul(aggr_string, 10, &aggregation_enabled_tmp);
457 if (retval || aggregation_enabled_tmp > 1) {
458 printk(KERN_ERR "batman-adv:Aggregation can only be enabled (1) or disabled (0), given value: %li\n", aggregation_enabled_tmp);
460 printk(KERN_INFO "batman-adv:Changing aggregation from: %s (%i) to: %s (%li)\n",
461 (atomic_read(&aggregation_enabled) == 1 ?
462 "enabled" : "disabled"),
463 atomic_read(&aggregation_enabled),
464 (aggregation_enabled_tmp == 1 ? "enabled" : "disabled"),
465 aggregation_enabled_tmp);
466 atomic_set(&aggregation_enabled,
467 (unsigned)aggregation_enabled_tmp);
474 static int proc_aggr_open(struct inode *inode, struct file *file)
476 return single_open(file, proc_aggr_read, NULL);
479 /* satisfying different prototypes ... */
480 static ssize_t proc_dummy_write(struct file *file, const char __user *buffer,
481 size_t count, loff_t *ppos)
486 static const struct file_operations proc_aggr_fops = {
487 .owner = THIS_MODULE,
488 .open = proc_aggr_open,
490 .write = proc_aggr_write,
492 .release = single_release,
495 static const struct file_operations proc_vis_srv_fops = {
496 .owner = THIS_MODULE,
497 .open = proc_vis_srv_open,
499 .write = proc_vis_srv_write,
501 .release = single_release,
504 static const struct file_operations proc_vis_data_fops = {
505 .owner = THIS_MODULE,
506 .open = proc_vis_data_open,
508 .write = proc_dummy_write,
510 .release = single_release,
513 static const struct file_operations proc_originators_fops = {
514 .owner = THIS_MODULE,
515 .open = proc_originators_open,
517 .write = proc_dummy_write,
519 .release = single_release,
522 static const struct file_operations proc_transt_local_fops = {
523 .owner = THIS_MODULE,
524 .open = proc_transt_local_open,
526 .write = proc_dummy_write,
528 .release = single_release,
531 static const struct file_operations proc_transt_global_fops = {
532 .owner = THIS_MODULE,
533 .open = proc_transt_global_open,
535 .write = proc_dummy_write,
537 .release = single_release,
540 static const struct file_operations proc_interfaces_fops = {
541 .owner = THIS_MODULE,
542 .open = proc_interfaces_open,
544 .write = proc_interfaces_write,
546 .release = single_release,
549 static const struct file_operations proc_orig_interval_fops = {
550 .owner = THIS_MODULE,
551 .open = proc_orig_interval_open,
553 .write = proc_orig_interval_write,
555 .release = single_release,
558 void cleanup_procfs(void)
560 if (proc_transt_global_file)
561 remove_proc_entry(PROC_FILE_TRANST_GLOBAL, proc_batman_dir);
563 if (proc_transt_local_file)
564 remove_proc_entry(PROC_FILE_TRANST_LOCAL, proc_batman_dir);
566 if (proc_originators_file)
567 remove_proc_entry(PROC_FILE_ORIGINATORS, proc_batman_dir);
569 if (proc_orig_interval_file)
570 remove_proc_entry(PROC_FILE_ORIG_INTERVAL, proc_batman_dir);
572 if (proc_interface_file)
573 remove_proc_entry(PROC_FILE_INTERFACES, proc_batman_dir);
575 if (proc_vis_data_file)
576 remove_proc_entry(PROC_FILE_VIS_DATA, proc_batman_dir);
578 if (proc_vis_srv_file)
579 remove_proc_entry(PROC_FILE_VIS_SRV, proc_batman_dir);
582 remove_proc_entry(PROC_FILE_AGGR, proc_batman_dir);
585 #ifdef __NET_NET_NAMESPACE_H
586 remove_proc_entry(PROC_ROOT_DIR, init_net.proc_net);
588 remove_proc_entry(PROC_ROOT_DIR, proc_net);
592 int setup_procfs(void)
594 #ifdef __NET_NET_NAMESPACE_H
595 proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, init_net.proc_net);
597 proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, proc_net);
600 if (!proc_batman_dir) {
601 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s' folder failed\n", PROC_ROOT_DIR);
605 proc_interface_file = create_proc_entry(PROC_FILE_INTERFACES,
608 if (proc_interface_file) {
609 proc_interface_file->proc_fops = &proc_interfaces_fops;
611 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_INTERFACES);
616 proc_orig_interval_file = create_proc_entry(PROC_FILE_ORIG_INTERVAL,
619 if (proc_orig_interval_file) {
620 proc_orig_interval_file->proc_fops = &proc_orig_interval_fops;
622 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIG_INTERVAL);
627 proc_originators_file = create_proc_entry(PROC_FILE_ORIGINATORS,
628 S_IRUGO, proc_batman_dir);
629 if (proc_originators_file) {
630 proc_originators_file->proc_fops = &proc_originators_fops;
632 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIGINATORS);
637 proc_transt_local_file = create_proc_entry(PROC_FILE_TRANST_LOCAL,
638 S_IRUGO, proc_batman_dir);
639 if (proc_transt_local_file) {
640 proc_transt_local_file->proc_fops = &proc_transt_local_fops;
642 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_LOCAL);
647 proc_transt_global_file = create_proc_entry(PROC_FILE_TRANST_GLOBAL,
648 S_IRUGO, proc_batman_dir);
649 if (proc_transt_global_file) {
650 proc_transt_global_file->proc_fops = &proc_transt_global_fops;
652 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_GLOBAL);
657 proc_vis_srv_file = create_proc_entry(PROC_FILE_VIS_SRV,
660 if (proc_vis_srv_file) {
661 proc_vis_srv_file->proc_fops = &proc_vis_srv_fops;
663 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_SRV);
668 proc_vis_data_file = create_proc_entry(PROC_FILE_VIS_DATA, S_IRUGO,
670 if (proc_vis_data_file) {
671 proc_vis_data_file->proc_fops = &proc_vis_data_fops;
673 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_DATA);
678 proc_aggr_file = create_proc_entry(PROC_FILE_AGGR, S_IWUSR | S_IRUGO,
680 if (proc_aggr_file) {
681 proc_aggr_file->proc_fops = &proc_aggr_fops;
683 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_AGGR);