a4f19e1dfc7b506eaa693cb01c6ae9d559157500
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / proc.c
1 /*
2  * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
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.
9  *
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.
14  *
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
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "proc.h"
24 #include "routing.h"
25 #include "translation-table.h"
26 #include "hard-interface.h"
27 #include "types.h"
28 #include "hash.h"
29 #include "vis.h"
30 #include "compat.h"
31
32 static struct proc_dir_entry *proc_batman_dir, *proc_interface_file;
33 static struct proc_dir_entry *proc_orig_interval_file, *proc_originators_file;
34 static struct proc_dir_entry *proc_transt_local_file;
35 static struct proc_dir_entry *proc_transt_global_file;
36 static struct proc_dir_entry *proc_vis_srv_file, *proc_vis_data_file;
37 static struct proc_dir_entry *proc_aggr_file;
38
39 static int proc_interfaces_read(struct seq_file *seq, void *offset)
40 {
41         struct batman_if *batman_if;
42
43         rcu_read_lock();
44         list_for_each_entry_rcu(batman_if, &if_list, list) {
45                 seq_printf(seq, "[%8s] %s %s \n",
46                            (batman_if->if_active == IF_ACTIVE ?
47                             "active" : "inactive"),
48                            batman_if->dev,
49                            (batman_if->if_active == IF_ACTIVE ?
50                             batman_if->addr_str : " "));
51         }
52         rcu_read_unlock();
53
54         return 0;
55 }
56
57 static int proc_interfaces_open(struct inode *inode, struct file *file)
58 {
59         return single_open(file, proc_interfaces_read, NULL);
60 }
61
62 static ssize_t proc_interfaces_write(struct file *instance,
63                                      const char __user *userbuffer,
64                                      size_t count, loff_t *data)
65 {
66         char *if_string, *colon_ptr = NULL, *cr_ptr = NULL;
67         int not_copied = 0, if_num = 0;
68         struct batman_if *batman_if = NULL;
69
70         if_string = kmalloc(count, GFP_KERNEL);
71
72         if (!if_string)
73                 return -ENOMEM;
74
75         if (count > IFNAMSIZ - 1) {
76                 printk(KERN_WARNING "batman-adv:Can't add interface: device name is too long\n");
77                 goto end;
78         }
79
80         not_copied = copy_from_user(if_string, userbuffer, count);
81         if_string[count - not_copied - 1] = 0;
82
83         colon_ptr = strchr(if_string, ':');
84         if (colon_ptr)
85                 *colon_ptr = 0;
86
87         if (!colon_ptr) {
88                 cr_ptr = strchr(if_string, '\n');
89                 if (cr_ptr)
90                         *cr_ptr = 0;
91         }
92
93         if (strlen(if_string) == 0) {
94                 shutdown_module();
95                 num_ifs = 0;
96                 goto end;
97         }
98
99         /* add interface */
100         rcu_read_lock();
101         list_for_each_entry_rcu(batman_if, &if_list, list) {
102                 if (strncmp(batman_if->dev, if_string, count) == 0) {
103                         printk(KERN_ERR "batman-adv:Given interface is already active: %s\n", if_string);
104                         rcu_read_unlock();
105                         goto end;
106
107                 }
108
109                 if_num++;
110         }
111         rcu_read_unlock();
112
113         hardif_add_interface(if_string, if_num);
114
115         if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
116             (hardif_get_active_if_num() > 0))
117                 activate_module();
118
119         rcu_read_lock();
120         if (list_empty(&if_list)) {
121                 rcu_read_unlock();
122                 goto end;
123         }
124         rcu_read_unlock();
125
126         num_ifs = if_num + 1;
127         return count;
128
129 end:
130         kfree(if_string);
131         return count;
132 }
133
134 static int proc_orig_interval_read(struct seq_file *seq, void *offset)
135 {
136         seq_printf(seq, "%i\n", atomic_read(&originator_interval));
137
138         return 0;
139 }
140
141 static ssize_t proc_orig_interval_write(struct file *file,
142                                         const char __user *buffer,
143                                         size_t count, loff_t *ppos)
144 {
145         char *interval_string;
146         int not_copied = 0;
147         unsigned long originator_interval_tmp;
148         int retval;
149
150         interval_string = kmalloc(count, GFP_KERNEL);
151
152         if (!interval_string)
153                 return -ENOMEM;
154
155         not_copied = copy_from_user(interval_string, buffer, count);
156         interval_string[count - not_copied - 1] = 0;
157
158         retval = strict_strtoul(interval_string, 10, &originator_interval_tmp);
159         if (retval) {
160                 printk(KERN_ERR "batman-adv:New originator interval invalid\n");
161                 goto end;
162         }
163
164         if (originator_interval_tmp <= JITTER * 2) {
165                 printk(KERN_WARNING "batman-adv:New originator interval too small: %li (min: %i)\n",
166                        originator_interval_tmp, JITTER * 2);
167                 goto end;
168         }
169
170         printk(KERN_INFO "batman-adv:Changing originator interval from: %i to: %li\n",
171                atomic_read(&originator_interval), originator_interval_tmp);
172
173         atomic_set(&originator_interval, originator_interval_tmp);
174
175 end:
176         kfree(interval_string);
177         return count;
178 }
179
180 static int proc_orig_interval_open(struct inode *inode, struct file *file)
181 {
182         return single_open(file, proc_orig_interval_read, NULL);
183 }
184
185 static int proc_originators_read(struct seq_file *seq, void *offset)
186 {
187         HASHIT(hashit);
188         struct orig_node *orig_node;
189         struct neigh_node *neigh_node;
190         int batman_count = 0;
191         char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
192
193         rcu_read_lock();
194         if (list_empty(&if_list)) {
195                 rcu_read_unlock();
196                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
197                 goto end;
198         }
199
200         if (((struct batman_if *)if_list.next)->if_active != IF_ACTIVE) {
201                 rcu_read_unlock();
202                 seq_printf(seq, "BATMAN disabled - primary interface not active \n");
203                 goto end;
204         }
205
206         seq_printf(seq,
207                    "  %-14s (%s/%i) %17s [%10s]: %20s ... [B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s] \n",
208                    "Originator", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
209                    "Potential nexthops", SOURCE_VERSION, REVISION_VERSION_STR,
210                    ((struct batman_if *)if_list.next)->dev,
211                    ((struct batman_if *)if_list.next)->addr_str);
212
213         rcu_read_unlock();
214         spin_lock(&orig_hash_lock);
215
216         while (hash_iterate(orig_hash, &hashit)) {
217
218                 orig_node = hashit.bucket->data;
219
220                 if (!orig_node->router)
221                         continue;
222
223                 if (orig_node->router->tq_avg == 0)
224                         continue;
225
226                 batman_count++;
227
228                 addr_to_string(orig_str, orig_node->orig);
229                 addr_to_string(router_str, orig_node->router->addr);
230
231                 seq_printf(seq, "%-17s  (%3i) %17s [%10s]:",
232                            orig_str, orig_node->router->tq_avg,
233                            router_str, orig_node->router->if_incoming->dev);
234
235                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
236                         addr_to_string(orig_str, neigh_node->addr);
237                         seq_printf(seq, " %17s (%3i)",
238                                    orig_str, neigh_node->tq_avg);
239                 }
240
241                 seq_printf(seq, "\n");
242
243         }
244
245         spin_unlock(&orig_hash_lock);
246
247         if (batman_count == 0)
248                 seq_printf(seq, "No batman nodes in range ... \n");
249
250 end:
251         return 0;
252 }
253
254 static int proc_originators_open(struct inode *inode, struct file *file)
255 {
256         return single_open(file, proc_originators_read, NULL);
257 }
258
259 static int proc_transt_local_read(struct seq_file *seq, void *offset)
260 {
261         char *buf;
262
263         buf = kmalloc(4096, GFP_KERNEL);
264         if (!buf)
265                 return 0;
266
267         rcu_read_lock();
268         if (list_empty(&if_list)) {
269                 rcu_read_unlock();
270                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
271                 goto end;
272         }
273
274         rcu_read_unlock();
275
276         seq_printf(seq, "Locally retrieved addresses (from %s) announced via HNA:\n", soft_device->name);
277
278         hna_local_fill_buffer_text(buf, 4096);
279         seq_printf(seq, "%s", buf);
280
281 end:
282         kfree(buf);
283         return 0;
284 }
285
286 static int proc_transt_local_open(struct inode *inode, struct file *file)
287 {
288         return single_open(file, proc_transt_local_read, NULL);
289 }
290
291 static int proc_transt_global_read(struct seq_file *seq, void *offset)
292 {
293         char *buf;
294
295         buf = kmalloc(4096, GFP_KERNEL);
296         if (!buf)
297                 return 0;
298
299         rcu_read_lock();
300         if (list_empty(&if_list)) {
301                 rcu_read_unlock();
302                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
303                 goto end;
304         }
305         rcu_read_unlock();
306
307
308         seq_printf(seq, "Globally announced HNAs received via the mesh (translation table):\n");
309
310         hna_global_fill_buffer_text(buf, 4096);
311         seq_printf(seq, "%s", buf);
312
313 end:
314         kfree(buf);
315         return 0;
316 }
317
318 static int proc_transt_global_open(struct inode *inode, struct file *file)
319 {
320         return single_open(file, proc_transt_global_read, NULL);
321 }
322
323 /* setting the mode of the vis server by the user */
324 static ssize_t proc_vis_srv_write(struct file *file, const char __user * buffer,
325                               size_t count, loff_t *ppos)
326 {
327         char *vis_mode_string;
328         int not_copied = 0;
329
330         vis_mode_string = kmalloc(count, GFP_KERNEL);
331
332         if (!vis_mode_string)
333                 return -ENOMEM;
334
335         not_copied = copy_from_user(vis_mode_string, buffer, count);
336         vis_mode_string[count - not_copied - 1] = 0;
337
338         if ((strcmp(vis_mode_string, "client") == 0) ||
339                         (strcmp(vis_mode_string, "disabled") == 0)) {
340                 printk(KERN_INFO "batman-adv:Setting VIS mode to client (disabling vis server)\n");
341                 vis_set_mode(VIS_TYPE_CLIENT_UPDATE);
342         } else if ((strcmp(vis_mode_string, "server") == 0) ||
343                         (strcmp(vis_mode_string, "enabled") == 0)) {
344                 printk(KERN_INFO "batman-adv:Setting VIS mode to server (enabling vis server)\n");
345                 vis_set_mode(VIS_TYPE_SERVER_SYNC);
346         } else
347                 printk(KERN_ERR "batman-adv:Unknown VIS mode: %s\n",
348                        vis_mode_string);
349
350         kfree(vis_mode_string);
351         return count;
352 }
353
354 static int proc_vis_srv_read(struct seq_file *seq, void *offset)
355 {
356         int vis_server = is_vis_server();
357
358         seq_printf(seq, "[%c] client mode (server disabled) \n",
359                         (!vis_server) ? 'x' : ' ');
360         seq_printf(seq, "[%c] server mode (server enabled) \n",
361                         (vis_server) ? 'x' : ' ');
362
363         return 0;
364 }
365
366 static int proc_vis_srv_open(struct inode *inode, struct file *file)
367 {
368         return single_open(file, proc_vis_srv_read, NULL);
369 }
370
371 static int proc_vis_data_read(struct seq_file *seq, void *offset)
372 {
373         HASHIT(hashit);
374         struct vis_info *info;
375         struct vis_info_entry *entries;
376         HLIST_HEAD(vis_if_list);
377         int i;
378         char tmp_addr_str[ETH_STR_LEN];
379
380         rcu_read_lock();
381         if (list_empty(&if_list) || (!is_vis_server())) {
382                 rcu_read_unlock();
383                 goto end;
384         }
385
386         rcu_read_unlock();
387
388         spin_lock(&vis_hash_lock);
389         while (hash_iterate(vis_hash, &hashit)) {
390                 info = hashit.bucket->data;
391                 entries = (struct vis_info_entry *)
392                         ((char *)info + sizeof(struct vis_info));
393                 addr_to_string(tmp_addr_str, info->packet.vis_orig);
394                 seq_printf(seq, "%s,", tmp_addr_str);
395
396                 for (i = 0; i < info->packet.entries; i++) {
397                         proc_vis_read_entry(seq, &entries[i], &vis_if_list,
398                                             info->packet.vis_orig);
399                 }
400
401                 /* add primary/secondary records */
402                 proc_vis_read_prim_sec(seq, &vis_if_list);
403                 seq_printf(seq, "\n");
404         }
405         spin_unlock(&vis_hash_lock);
406
407 end:
408         return 0;
409 }
410
411 static int proc_vis_data_open(struct inode *inode, struct file *file)
412 {
413         return single_open(file, proc_vis_data_read, NULL);
414 }
415
416 static int proc_aggr_read(struct seq_file *seq, void *offset)
417 {
418         seq_printf(seq, "%i\n", atomic_read(&aggregation_enabled));
419
420         return 0;
421 }
422
423 static ssize_t proc_aggr_write(struct file *file, const char __user *buffer,
424                                size_t count, loff_t *ppos)
425 {
426         char *aggr_string;
427         int not_copied = 0;
428         unsigned long aggregation_enabled_tmp;
429
430         aggr_string = kmalloc(count, GFP_KERNEL);
431
432         if (!aggr_string)
433                 return -ENOMEM;
434
435         not_copied = copy_from_user(aggr_string, buffer, count);
436         aggr_string[count - not_copied - 1] = 0;
437
438         strict_strtoul(aggr_string, 10, &aggregation_enabled_tmp);
439
440         if ((aggregation_enabled_tmp != 0) && (aggregation_enabled_tmp != 1)) {
441                 printk(KERN_ERR "batman-adv:Aggregation can only be enabled (1) or disabled (0), given value: %li\n", aggregation_enabled_tmp);
442                 goto end;
443         }
444
445         printk(KERN_INFO "batman-adv:Changing aggregation from: %s (%i) to: %s (%li)\n",
446                (atomic_read(&aggregation_enabled) == 1 ?
447                 "enabled" : "disabled"),
448                atomic_read(&aggregation_enabled),
449                (aggregation_enabled_tmp == 1 ? "enabled" : "disabled"),
450                aggregation_enabled_tmp);
451
452         atomic_set(&aggregation_enabled, (unsigned)aggregation_enabled_tmp);
453 end:
454         kfree(aggr_string);
455         return count;
456 }
457
458 static int proc_aggr_open(struct inode *inode, struct file *file)
459 {
460         return single_open(file, proc_aggr_read, NULL);
461 }
462
463 /* satisfying different prototypes ... */
464 static ssize_t proc_dummy_write(struct file *file, const char __user *buffer,
465                                 size_t count, loff_t *ppos)
466 {
467         return count;
468 }
469
470 static const struct file_operations proc_aggr_fops = {
471         .owner          = THIS_MODULE,
472         .open           = proc_aggr_open,
473         .read           = seq_read,
474         .write          = proc_aggr_write,
475         .llseek         = seq_lseek,
476         .release        = single_release,
477 };
478
479 static const struct file_operations proc_vis_srv_fops = {
480         .owner          = THIS_MODULE,
481         .open           = proc_vis_srv_open,
482         .read           = seq_read,
483         .write          = proc_vis_srv_write,
484         .llseek         = seq_lseek,
485         .release        = single_release,
486 };
487
488 static const struct file_operations proc_vis_data_fops = {
489         .owner          = THIS_MODULE,
490         .open           = proc_vis_data_open,
491         .read           = seq_read,
492         .write          = proc_dummy_write,
493         .llseek         = seq_lseek,
494         .release        = single_release,
495 };
496
497 static const struct file_operations proc_originators_fops = {
498         .owner          = THIS_MODULE,
499         .open           = proc_originators_open,
500         .read           = seq_read,
501         .write          = proc_dummy_write,
502         .llseek         = seq_lseek,
503         .release        = single_release,
504 };
505
506 static const struct file_operations proc_transt_local_fops = {
507         .owner          = THIS_MODULE,
508         .open           = proc_transt_local_open,
509         .read           = seq_read,
510         .write          = proc_dummy_write,
511         .llseek         = seq_lseek,
512         .release        = single_release,
513 };
514
515 static const struct file_operations proc_transt_global_fops = {
516         .owner          = THIS_MODULE,
517         .open           = proc_transt_global_open,
518         .read           = seq_read,
519         .write          = proc_dummy_write,
520         .llseek         = seq_lseek,
521         .release        = single_release,
522 };
523
524 static const struct file_operations proc_interfaces_fops = {
525         .owner          = THIS_MODULE,
526         .open           = proc_interfaces_open,
527         .read           = seq_read,
528         .write          = proc_interfaces_write,
529         .llseek         = seq_lseek,
530         .release        = single_release,
531 };
532
533 static const struct file_operations proc_orig_interval_fops = {
534         .owner          = THIS_MODULE,
535         .open           = proc_orig_interval_open,
536         .read           = seq_read,
537         .write          = proc_orig_interval_write,
538         .llseek         = seq_lseek,
539         .release        = single_release,
540 };
541
542 void cleanup_procfs(void)
543 {
544         if (proc_transt_global_file)
545                 remove_proc_entry(PROC_FILE_TRANST_GLOBAL, proc_batman_dir);
546
547         if (proc_transt_local_file)
548                 remove_proc_entry(PROC_FILE_TRANST_LOCAL, proc_batman_dir);
549
550         if (proc_originators_file)
551                 remove_proc_entry(PROC_FILE_ORIGINATORS, proc_batman_dir);
552
553         if (proc_orig_interval_file)
554                 remove_proc_entry(PROC_FILE_ORIG_INTERVAL, proc_batman_dir);
555
556         if (proc_interface_file)
557                 remove_proc_entry(PROC_FILE_INTERFACES, proc_batman_dir);
558
559         if (proc_vis_data_file)
560                 remove_proc_entry(PROC_FILE_VIS_DATA, proc_batman_dir);
561
562         if (proc_vis_srv_file)
563                 remove_proc_entry(PROC_FILE_VIS_SRV, proc_batman_dir);
564
565         if (proc_aggr_file)
566                 remove_proc_entry(PROC_FILE_AGGR, proc_batman_dir);
567
568         if (proc_batman_dir)
569 #ifdef __NET_NET_NAMESPACE_H
570                 remove_proc_entry(PROC_ROOT_DIR, init_net.proc_net);
571 #else
572                 remove_proc_entry(PROC_ROOT_DIR, proc_net);
573 #endif
574 }
575
576 int setup_procfs(void)
577 {
578 #ifdef __NET_NET_NAMESPACE_H
579         proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, init_net.proc_net);
580 #else
581         proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, proc_net);
582 #endif
583
584         if (!proc_batman_dir) {
585                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s' folder failed\n", PROC_ROOT_DIR);
586                 return -EFAULT;
587         }
588
589         proc_interface_file = create_proc_entry(PROC_FILE_INTERFACES,
590                                                 S_IWUSR | S_IRUGO,
591                                                 proc_batman_dir);
592         if (proc_interface_file) {
593                 proc_interface_file->proc_fops = &proc_interfaces_fops;
594         } else {
595                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_INTERFACES);
596                 cleanup_procfs();
597                 return -EFAULT;
598         }
599
600         proc_orig_interval_file = create_proc_entry(PROC_FILE_ORIG_INTERVAL,
601                                                     S_IWUSR | S_IRUGO,
602                                                     proc_batman_dir);
603         if (proc_orig_interval_file) {
604                 proc_orig_interval_file->proc_fops = &proc_orig_interval_fops;
605         } else {
606                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIG_INTERVAL);
607                 cleanup_procfs();
608                 return -EFAULT;
609         }
610
611         proc_originators_file = create_proc_entry(PROC_FILE_ORIGINATORS,
612                                                   S_IRUGO, proc_batman_dir);
613         if (proc_originators_file) {
614                 proc_originators_file->proc_fops = &proc_originators_fops;
615         } else {
616                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIGINATORS);
617                 cleanup_procfs();
618                 return -EFAULT;
619         }
620
621         proc_transt_local_file = create_proc_entry(PROC_FILE_TRANST_LOCAL,
622                                                    S_IRUGO, proc_batman_dir);
623         if (proc_transt_local_file) {
624                 proc_transt_local_file->proc_fops = &proc_transt_local_fops;
625         } else {
626                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_LOCAL);
627                 cleanup_procfs();
628                 return -EFAULT;
629         }
630
631         proc_transt_global_file = create_proc_entry(PROC_FILE_TRANST_GLOBAL,
632                                                     S_IRUGO, proc_batman_dir);
633         if (proc_transt_global_file) {
634                 proc_transt_global_file->proc_fops = &proc_transt_global_fops;
635         } else {
636                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_GLOBAL);
637                 cleanup_procfs();
638                 return -EFAULT;
639         }
640
641         proc_vis_srv_file = create_proc_entry(PROC_FILE_VIS_SRV,
642                                                 S_IWUSR | S_IRUGO,
643                                                 proc_batman_dir);
644         if (proc_vis_srv_file) {
645                 proc_vis_srv_file->proc_fops = &proc_vis_srv_fops;
646         } else {
647                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_SRV);
648                 cleanup_procfs();
649                 return -EFAULT;
650         }
651
652         proc_vis_data_file = create_proc_entry(PROC_FILE_VIS_DATA, S_IRUGO,
653                                           proc_batman_dir);
654         if (proc_vis_data_file) {
655                 proc_vis_data_file->proc_fops = &proc_vis_data_fops;
656         } else {
657                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_DATA);
658                 cleanup_procfs();
659                 return -EFAULT;
660         }
661
662         proc_aggr_file = create_proc_entry(PROC_FILE_AGGR, S_IWUSR | S_IRUGO,
663                                            proc_batman_dir);
664         if (proc_aggr_file) {
665                 proc_aggr_file->proc_fops = &proc_aggr_fops;
666         } else {
667                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_AGGR);
668                 cleanup_procfs();
669                 return -EFAULT;
670         }
671
672         return 0;
673 }
674
675