staging: batman-adv meshing protocol
[linux-2.6-block.git] / drivers / staging / batman-adv / proc.c
CommitLineData
5beef3c9
AL
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 "log.h"
25#include "routing.h"
26#include "translation-table.h"
27#include "hard-interface.h"
28#include "types.h"
29#include "hash.h"
30#include "vis.h"
31#include "compat.h"
32
33static uint8_t vis_format = DOT_DRAW;
34
35static struct proc_dir_entry *proc_batman_dir, *proc_interface_file;
36static struct proc_dir_entry *proc_orig_interval_file, *proc_originators_file;
37static struct proc_dir_entry *proc_log_file, *proc_log_level_file;
38static struct proc_dir_entry *proc_transt_local_file;
39static struct proc_dir_entry *proc_transt_global_file;
40static struct proc_dir_entry *proc_vis_file, *proc_vis_format_file;
41static struct proc_dir_entry *proc_aggr_file;
42
43static int proc_interfaces_read(struct seq_file *seq, void *offset)
44{
45 struct batman_if *batman_if;
46
47 rcu_read_lock();
48 list_for_each_entry_rcu(batman_if, &if_list, list) {
49 seq_printf(seq, "[%8s] %s %s \n",
50 (batman_if->if_active == IF_ACTIVE ?
51 "active" : "inactive"),
52 batman_if->dev,
53 (batman_if->if_active == IF_ACTIVE ?
54 batman_if->addr_str : " "));
55 }
56 rcu_read_unlock();
57
58 return 0;
59}
60
61static int proc_interfaces_open(struct inode *inode, struct file *file)
62{
63 return single_open(file, proc_interfaces_read, NULL);
64}
65
66static ssize_t proc_interfaces_write(struct file *instance,
67 const char __user *userbuffer,
68 size_t count, loff_t *data)
69{
70 char *if_string, *colon_ptr = NULL, *cr_ptr = NULL;
71 int not_copied = 0, if_num = 0;
72 struct batman_if *batman_if = NULL;
73
74 if_string = kmalloc(count, GFP_KERNEL);
75
76 if (!if_string)
77 return -ENOMEM;
78
79 if (count > IFNAMSIZ - 1) {
80 debug_log(LOG_TYPE_WARN,
81 "Can't add interface: device name is too long\n");
82 goto end;
83 }
84
85 not_copied = copy_from_user(if_string, userbuffer, count);
86 if_string[count - not_copied - 1] = 0;
87
88 colon_ptr = strchr(if_string, ':');
89 if (colon_ptr)
90 *colon_ptr = 0;
91
92 if (!colon_ptr) {
93 cr_ptr = strchr(if_string, '\n');
94 if (cr_ptr)
95 *cr_ptr = 0;
96 }
97
98 if (strlen(if_string) == 0) {
99 shutdown_module();
100 num_ifs = 0;
101 goto end;
102 }
103
104 /* add interface */
105 rcu_read_lock();
106 list_for_each_entry_rcu(batman_if, &if_list, list) {
107 if (strncmp(batman_if->dev, if_string, count) == 0) {
108 debug_log(LOG_TYPE_WARN, "Given interface is already active: %s\n", if_string);
109 rcu_read_unlock();
110 goto end;
111
112 }
113
114 if_num++;
115 }
116 rcu_read_unlock();
117
118 hardif_add_interface(if_string, if_num);
119
120 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
121 (hardif_get_active_if_num() > 0))
122 activate_module();
123
124 rcu_read_lock();
125 if (list_empty(&if_list)) {
126 rcu_read_unlock();
127 goto end;
128 }
129 rcu_read_unlock();
130
131 num_ifs = if_num + 1;
132 return count;
133
134end:
135 kfree(if_string);
136 return count;
137}
138
139static int proc_orig_interval_read(struct seq_file *seq, void *offset)
140{
141 seq_printf(seq, "%i\n", atomic_read(&originator_interval));
142
143 return 0;
144}
145
146static ssize_t proc_orig_interval_write(struct file *file,
147 const char __user *buffer,
148 size_t count, loff_t *ppos)
149{
150 char *interval_string;
151 int not_copied = 0;
152 unsigned long originator_interval_tmp;
153 int retval;
154
155 interval_string = kmalloc(count, GFP_KERNEL);
156
157 if (!interval_string)
158 return -ENOMEM;
159
160 not_copied = copy_from_user(interval_string, buffer, count);
161 interval_string[count - not_copied - 1] = 0;
162
163 retval = strict_strtoul(interval_string, 10, &originator_interval_tmp);
164 if (retval) {
165 debug_log(LOG_TYPE_WARN, "New originator interval invalid\n");
166 goto end;
167 }
168
169 if (originator_interval_tmp <= JITTER * 2) {
170 debug_log(LOG_TYPE_WARN,
171 "New originator interval too small: %i (min: %i)\n",
172 originator_interval_tmp, JITTER * 2);
173 goto end;
174 }
175
176 debug_log(LOG_TYPE_NOTICE,
177 "Changing originator interval from: %i to: %i\n",
178 atomic_read(&originator_interval), originator_interval_tmp);
179
180 atomic_set(&originator_interval, originator_interval_tmp);
181
182end:
183 kfree(interval_string);
184 return count;
185}
186
187static int proc_orig_interval_open(struct inode *inode, struct file *file)
188{
189 return single_open(file, proc_orig_interval_read, NULL);
190}
191
192static int proc_originators_read(struct seq_file *seq, void *offset)
193{
194 struct hash_it_t *hashit = NULL;
195 struct orig_node *orig_node;
196 struct neigh_node *neigh_node;
197 int batman_count = 0;
198 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
199
200 rcu_read_lock();
201 if (list_empty(&if_list)) {
202 rcu_read_unlock();
203 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
204 goto end;
205 }
206
207 if (((struct batman_if *)if_list.next)->if_active != IF_ACTIVE) {
208 rcu_read_unlock();
209 seq_printf(seq, "BATMAN disabled - primary interface not active \n");
210 goto end;
211 }
212
213 seq_printf(seq,
214 " %-14s (%s/%i) %17s [%10s]: %20s ... [B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s] \n",
215 "Originator", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
216 "Potential nexthops", SOURCE_VERSION, REVISION_VERSION_STR,
217 ((struct batman_if *)if_list.next)->dev,
218 ((struct batman_if *)if_list.next)->addr_str);
219
220 rcu_read_unlock();
221 spin_lock(&orig_hash_lock);
222
223 while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
224
225 orig_node = hashit->bucket->data;
226
227 if (!orig_node->router)
228 continue;
229
230 if (orig_node->router->tq_avg == 0)
231 continue;
232
233 batman_count++;
234
235 addr_to_string(orig_str, orig_node->orig);
236 addr_to_string(router_str, orig_node->router->addr);
237
238 seq_printf(seq, "%-17s (%3i) %17s [%10s]:",
239 orig_str, orig_node->router->tq_avg,
240 router_str, orig_node->router->if_incoming->dev);
241
242 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
243 addr_to_string(orig_str, neigh_node->addr);
244 seq_printf(seq, " %17s (%3i)",
245 orig_str, neigh_node->tq_avg);
246 }
247
248 seq_printf(seq, "\n");
249
250 }
251
252 spin_unlock(&orig_hash_lock);
253
254 if (batman_count == 0)
255 seq_printf(seq, "No batman nodes in range ... \n");
256
257end:
258 return 0;
259}
260
261static int proc_originators_open(struct inode *inode, struct file *file)
262{
263 return single_open(file, proc_originators_read, NULL);
264}
265
266static int proc_log_level_read(struct seq_file *seq, void *offset)
267{
268
269 seq_printf(seq, "[x] %s (%d)\n", LOG_TYPE_CRIT_NAME, LOG_TYPE_CRIT);
270 seq_printf(seq, "[%c] %s (%d)\n",
271 (LOG_TYPE_WARN & log_level) ? 'x' : ' ',
272 LOG_TYPE_WARN_NAME, LOG_TYPE_WARN);
273 seq_printf(seq, "[%c] %s (%d)\n",
274 (LOG_TYPE_NOTICE & log_level) ? 'x' : ' ',
275 LOG_TYPE_NOTICE_NAME, LOG_TYPE_NOTICE);
276 seq_printf(seq, "[%c] %s (%d)\n",
277 (LOG_TYPE_BATMAN & log_level) ? 'x' : ' ',
278 LOG_TYPE_BATMAN_NAME, LOG_TYPE_BATMAN);
279 seq_printf(seq, "[%c] %s (%d)\n",
280 (LOG_TYPE_ROUTES & log_level) ? 'x' : ' ',
281 LOG_TYPE_ROUTES_NAME, LOG_TYPE_ROUTES);
282 return 0;
283}
284
285static int proc_log_level_open(struct inode *inode, struct file *file)
286{
287 return single_open(file, proc_log_level_read, NULL);
288}
289
290static ssize_t proc_log_level_write(struct file *instance,
291 const char __user *userbuffer,
292 size_t count, loff_t *data)
293{
294 char *log_level_string, *tokptr, *cp;
295 int finished, not_copied = 0;
296 unsigned long log_level_tmp = 0;
297
298 log_level_string = kmalloc(count, GFP_KERNEL);
299
300 if (!log_level_string)
301 return -ENOMEM;
302
303 not_copied = copy_from_user(log_level_string, userbuffer, count);
304 log_level_string[count - not_copied - 1] = 0;
305
306 if (strict_strtoul(log_level_string, 10, &log_level_tmp) < 0) {
307 /* was not a number, doing textual parsing */
308 log_level_tmp = 0;
309 tokptr = log_level_string;
310
311 for (cp = log_level_string, finished = 0; !finished; cp++) {
312 switch (*cp) {
313 case 0:
314 finished = 1;
315 case ' ':
316 case '\n':
317 case '\t':
318 *cp = 0;
319 /* compare */
320 if (strcmp(tokptr, LOG_TYPE_WARN_NAME) == 0)
321 log_level_tmp |= LOG_TYPE_WARN;
322 if (strcmp(tokptr, LOG_TYPE_NOTICE_NAME) == 0)
323 log_level_tmp |= LOG_TYPE_NOTICE;
324 if (strcmp(tokptr, LOG_TYPE_BATMAN_NAME) == 0)
325 log_level_tmp |= LOG_TYPE_BATMAN;
326 if (strcmp(tokptr, LOG_TYPE_ROUTES_NAME) == 0)
327 log_level_tmp |= LOG_TYPE_ROUTES;
328 tokptr = cp + 1;
329 break;
330 default:
331 ;
332 }
333 }
334 }
335
336 debug_log(LOG_TYPE_CRIT, "Changing log_level from: %i to: %i\n",
337 log_level, log_level_tmp);
338 log_level = log_level_tmp;
339
340 kfree(log_level_string);
341 return count;
342}
343
344static int proc_transt_local_read(struct seq_file *seq, void *offset)
345{
346 char *buf;
347
348 buf = kmalloc(4096, GFP_KERNEL);
349 if (!buf)
350 return 0;
351
352 rcu_read_lock();
353 if (list_empty(&if_list)) {
354 rcu_read_unlock();
355 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
356 goto end;
357 }
358
359 rcu_read_unlock();
360
361 seq_printf(seq, "Locally retrieved addresses (from %s) announced via HNA:\n", soft_device->name);
362
363 hna_local_fill_buffer_text(buf, 4096);
364 seq_printf(seq, "%s", buf);
365
366end:
367 kfree(buf);
368 return 0;
369}
370
371static int proc_transt_local_open(struct inode *inode, struct file *file)
372{
373 return single_open(file, proc_transt_local_read, NULL);
374}
375
376static int proc_transt_global_read(struct seq_file *seq, void *offset)
377{
378 char *buf;
379
380 buf = kmalloc(4096, GFP_KERNEL);
381 if (!buf)
382 return 0;
383
384 rcu_read_lock();
385 if (list_empty(&if_list)) {
386 rcu_read_unlock();
387 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
388 goto end;
389 }
390 rcu_read_unlock();
391
392
393 seq_printf(seq, "Globally announced HNAs received via the mesh (translation table):\n");
394
395 hna_global_fill_buffer_text(buf, 4096);
396 seq_printf(seq, "%s", buf);
397
398end:
399 kfree(buf);
400 return 0;
401}
402
403static int proc_transt_global_open(struct inode *inode, struct file *file)
404{
405 return single_open(file, proc_transt_global_read, NULL);
406}
407
408/* insert interface to the list of interfaces of one originator */
409
410static void proc_vis_insert_interface(const uint8_t *interface,
411 struct vis_if_list **if_entry,
412 bool primary)
413{
414 /* Did we get an empty list? (then insert imediately) */
415 if(*if_entry == NULL) {
416 *if_entry = kmalloc(sizeof(struct vis_if_list), GFP_KERNEL);
417 if (*if_entry == NULL)
418 return;
419
420 (*if_entry)->primary = primary;
421 (*if_entry)->next = NULL;
422 memcpy((*if_entry)->addr, interface, ETH_ALEN);
423 } else {
424 struct vis_if_list *head_if_entry = *if_entry;
425 /* Do we already have this interface in our list? */
426 while (!compare_orig((*if_entry)->addr, (void *)interface)) {
427
428 /* Or did we reach the end (then append the interface) */
429 if ((*if_entry)->next == NULL) {
430 (*if_entry)->next = kmalloc(sizeof(struct vis_if_list), GFP_KERNEL);
431 if ((*if_entry)->next == NULL)
432 return;
433
434 memcpy((*if_entry)->next->addr, interface, ETH_ALEN);
435 (*if_entry)->next->primary = primary;
436 (*if_entry)->next->next = NULL;
437 break;
438 }
439 *if_entry = (*if_entry)->next;
440 }
441 /* Rewind the list to its head */
442 *if_entry = head_if_entry;
443 }
444}
445/* read an entry */
446
447static void proc_vis_read_entry(struct seq_file *seq,
448 struct vis_info_entry *entry,
449 struct vis_if_list **if_entry,
450 uint8_t *vis_orig,
451 uint8_t current_format,
452 uint8_t first_line)
453{
454 char from[40];
455 char to[40];
456 int int_part, frac_part;
457
458 addr_to_string(to, entry->dest);
459 if (entry->quality == 0) {
460#ifndef VIS_SUBCLUSTERS_DISABLED
461 proc_vis_insert_interface(vis_orig, if_entry, true);
462#endif /* VIS_SUBCLUSTERS_DISABLED */
463 addr_to_string(from, vis_orig);
464 if (current_format == DOT_DRAW) {
465 seq_printf(seq, "\t\"%s\" -> \"%s\" [label=\"HNA\"]\n",
466 from, to);
467 } else {
468 seq_printf(seq,
469 "%s\t{ router : \"%s\", gateway : \"%s\", label : \"HNA\" }",
470 (first_line ? "" : ",\n"), from, to);
471 }
472 } else {
473#ifndef VIS_SUBCLUSTERS_DISABLED
474 proc_vis_insert_interface(entry->src, if_entry, compare_orig(entry->src, vis_orig));
475#endif /* VIS_SUBCLUSTERS_DISABLED */
476 addr_to_string(from, entry->src);
477
478 /* kernel has no printf-support for %f? it'd be better to return
479 * this in float. */
480
481 int_part = TQ_MAX_VALUE / entry->quality;
482 frac_part = 1000 * TQ_MAX_VALUE / entry->quality - int_part * 1000;
483
484 if (current_format == DOT_DRAW) {
485 seq_printf(seq,
486 "\t\"%s\" -> \"%s\" [label=\"%d.%d\"]\n",
487 from, to, int_part, frac_part);
488 } else {
489 seq_printf(seq,
490 "%s\t{ router : \"%s\", neighbour : \"%s\", label : %d.%d }",
491 (first_line ? "" : ",\n"), from, to, int_part, frac_part);
492 }
493 }
494}
495
496
497static int proc_vis_read(struct seq_file *seq, void *offset)
498{
499 struct hash_it_t *hashit = NULL;
500 struct vis_info *info;
501 struct vis_info_entry *entries;
502 struct vis_if_list *if_entries = NULL;
503 int i;
504 uint8_t current_format, first_line = 1;
505#ifndef VIS_SUBCLUSTERS_DISABLED
506 char tmp_addr_str[ETH_STR_LEN];
507 struct vis_if_list *tmp_if_next;
508#endif /* VIS_SUBCLUSTERS_DISABLED */
509
510 current_format = vis_format;
511
512 rcu_read_lock();
513 if (list_empty(&if_list) || (!is_vis_server())) {
514 rcu_read_unlock();
515 if (current_format == DOT_DRAW)
516 seq_printf(seq, "digraph {\n}\n");
517 goto end;
518 }
519
520 rcu_read_unlock();
521
522 if (current_format == DOT_DRAW)
523 seq_printf(seq, "digraph {\n");
524
525 spin_lock(&vis_hash_lock);
526 while (NULL != (hashit = hash_iterate(vis_hash, hashit))) {
527 info = hashit->bucket->data;
528 entries = (struct vis_info_entry *)
529 ((char *)info + sizeof(struct vis_info));
530
531 for (i = 0; i < info->packet.entries; i++) {
532 proc_vis_read_entry(seq, &entries[i], &if_entries,
533 info->packet.vis_orig,
534 current_format, first_line);
535 if (first_line)
536 first_line = 0;
537 }
538
539#ifndef VIS_SUBCLUSTERS_DISABLED
540 /* Generate subgraphs from the collected items */
541 if (current_format == DOT_DRAW) {
542
543 addr_to_string(tmp_addr_str, info->packet.vis_orig);
544 seq_printf(seq, "\tsubgraph \"cluster_%s\" {\n", tmp_addr_str);
545 while (if_entries != NULL) {
546
547 addr_to_string(tmp_addr_str, if_entries->addr);
548 if (if_entries->primary)
549 seq_printf(seq, "\t\t\"%s\" [peripheries=2]\n", tmp_addr_str);
550 else
551 seq_printf(seq, "\t\t\"%s\"\n", tmp_addr_str);
552
553 /* ... and empty the list while doing this */
554 tmp_if_next = if_entries->next;
555 kfree(if_entries);
556 if_entries = tmp_if_next;
557 }
558 seq_printf(seq, "\t}\n");
559 }
560#endif /* VIS_SUBCLUSTERS_DISABLED */
561 }
562 spin_unlock(&vis_hash_lock);
563
564 if (current_format == DOT_DRAW)
565 seq_printf(seq, "}\n");
566 else
567 seq_printf(seq, "\n");
568end:
569 return 0;
570}
571
572/* setting the mode of the vis server by the user */
573static ssize_t proc_vis_write(struct file *file, const char __user * buffer,
574 size_t count, loff_t *ppos)
575{
576 char *vis_mode_string;
577 int not_copied = 0;
578
579 vis_mode_string = kmalloc(count, GFP_KERNEL);
580
581 if (!vis_mode_string)
582 return -ENOMEM;
583
584 not_copied = copy_from_user(vis_mode_string, buffer, count);
585 vis_mode_string[count - not_copied - 1] = 0;
586
587 if (strcmp(vis_mode_string, "client") == 0) {
588 debug_log(LOG_TYPE_NOTICE, "Setting VIS mode to client\n");
589 vis_set_mode(VIS_TYPE_CLIENT_UPDATE);
590 } else if (strcmp(vis_mode_string, "server") == 0) {
591 debug_log(LOG_TYPE_NOTICE, "Setting VIS mode to server\n");
592 vis_set_mode(VIS_TYPE_SERVER_SYNC);
593 } else
594 debug_log(LOG_TYPE_WARN, "Unknown VIS mode: %s\n",
595 vis_mode_string);
596
597 kfree(vis_mode_string);
598 return count;
599}
600
601static int proc_vis_open(struct inode *inode, struct file *file)
602{
603 return single_open(file, proc_vis_read, NULL);
604}
605
606static int proc_vis_format_read(struct seq_file *seq, void *offset)
607{
608 uint8_t current_format = vis_format;
609
610 seq_printf(seq, "[%c] %s\n",
611 (current_format == DOT_DRAW) ? 'x' : ' ',
612 VIS_FORMAT_DD_NAME);
613 seq_printf(seq, "[%c] %s\n",
614 (current_format == JSON) ? 'x' : ' ',
615 VIS_FORMAT_JSON_NAME);
616 return 0;
617}
618
619static int proc_vis_format_open(struct inode *inode, struct file *file)
620{
621 return single_open(file, proc_vis_format_read, NULL);
622}
623
624static ssize_t proc_vis_format_write(struct file *file,
625 const char __user *buffer,
626 size_t count, loff_t *ppos)
627{
628 char *vis_format_string;
629 int not_copied = 0;
630
631 vis_format_string = kmalloc(count, GFP_KERNEL);
632
633 if (!vis_format_string)
634 return -ENOMEM;
635
636 not_copied = copy_from_user(vis_format_string, buffer, count);
637 vis_format_string[count - not_copied - 1] = 0;
638
639 if (strcmp(vis_format_string, VIS_FORMAT_DD_NAME) == 0) {
640 debug_log(LOG_TYPE_NOTICE, "Setting VIS output format to: %s\n",
641 VIS_FORMAT_DD_NAME);
642 vis_format = DOT_DRAW;
643 } else if (strcmp(vis_format_string, VIS_FORMAT_JSON_NAME) == 0) {
644 debug_log(LOG_TYPE_NOTICE, "Setting VIS output format to: %s\n",
645 VIS_FORMAT_JSON_NAME);
646 vis_format = JSON;
647 } else
648 debug_log(LOG_TYPE_WARN, "Unknown VIS output format: %s\n",
649 vis_format_string);
650
651 kfree(vis_format_string);
652 return count;
653}
654
655static int proc_aggr_read(struct seq_file *seq, void *offset)
656{
657 seq_printf(seq, "%i\n", atomic_read(&aggregation_enabled));
658
659 return 0;
660}
661
662static ssize_t proc_aggr_write(struct file *file, const char __user *buffer,
663 size_t count, loff_t *ppos)
664{
665 char *aggr_string;
666 int not_copied = 0;
667 unsigned long aggregation_enabled_tmp;
668
669 aggr_string = kmalloc(count, GFP_KERNEL);
670
671 if (!aggr_string)
672 return -ENOMEM;
673
674 not_copied = copy_from_user(aggr_string, buffer, count);
675 aggr_string[count - not_copied - 1] = 0;
676
677 strict_strtoul(aggr_string, 10, &aggregation_enabled_tmp);
678
679 if ((aggregation_enabled_tmp != 0) && (aggregation_enabled_tmp != 1)) {
680 debug_log(LOG_TYPE_WARN, "Aggregation can only be enabled (1) or disabled (0), given value: %li\n", aggregation_enabled_tmp);
681 goto end;
682 }
683
684 debug_log(LOG_TYPE_NOTICE, "Changing aggregation from: %s (%i) to: %s (%li)\n",
685 (atomic_read(&aggregation_enabled) == 1 ?
686 "enabled" : "disabled"),
687 atomic_read(&aggregation_enabled),
688 (aggregation_enabled_tmp == 1 ? "enabled" : "disabled"),
689 aggregation_enabled_tmp);
690
691 atomic_set(&aggregation_enabled, (unsigned)aggregation_enabled_tmp);
692end:
693 kfree(aggr_string);
694 return count;
695}
696
697static int proc_aggr_open(struct inode *inode, struct file *file)
698{
699 return single_open(file, proc_aggr_read, NULL);
700}
701
702/* satisfying different prototypes ... */
703static ssize_t proc_dummy_write(struct file *file, const char __user *buffer,
704 size_t count, loff_t *ppos)
705{
706 return count;
707}
708
709static const struct file_operations proc_aggr_fops = {
710 .owner = THIS_MODULE,
711 .open = proc_aggr_open,
712 .read = seq_read,
713 .write = proc_aggr_write,
714 .llseek = seq_lseek,
715 .release = single_release,
716};
717
718static const struct file_operations proc_vis_format_fops = {
719 .owner = THIS_MODULE,
720 .open = proc_vis_format_open,
721 .read = seq_read,
722 .write = proc_vis_format_write,
723 .llseek = seq_lseek,
724 .release = single_release,
725};
726
727static const struct file_operations proc_vis_fops = {
728 .owner = THIS_MODULE,
729 .open = proc_vis_open,
730 .read = seq_read,
731 .write = proc_vis_write,
732 .llseek = seq_lseek,
733 .release = single_release,
734};
735
736static const struct file_operations proc_originators_fops = {
737 .owner = THIS_MODULE,
738 .open = proc_originators_open,
739 .read = seq_read,
740 .write = proc_dummy_write,
741 .llseek = seq_lseek,
742 .release = single_release,
743};
744
745static const struct file_operations proc_transt_local_fops = {
746 .owner = THIS_MODULE,
747 .open = proc_transt_local_open,
748 .read = seq_read,
749 .write = proc_dummy_write,
750 .llseek = seq_lseek,
751 .release = single_release,
752};
753
754static const struct file_operations proc_transt_global_fops = {
755 .owner = THIS_MODULE,
756 .open = proc_transt_global_open,
757 .read = seq_read,
758 .write = proc_dummy_write,
759 .llseek = seq_lseek,
760 .release = single_release,
761};
762
763static const struct file_operations proc_log_level_fops = {
764 .owner = THIS_MODULE,
765 .open = proc_log_level_open,
766 .read = seq_read,
767 .write = proc_log_level_write,
768 .llseek = seq_lseek,
769 .release = single_release,
770};
771
772static const struct file_operations proc_interfaces_fops = {
773 .owner = THIS_MODULE,
774 .open = proc_interfaces_open,
775 .read = seq_read,
776 .write = proc_interfaces_write,
777 .llseek = seq_lseek,
778 .release = single_release,
779};
780
781static const struct file_operations proc_orig_interval_fops = {
782 .owner = THIS_MODULE,
783 .open = proc_orig_interval_open,
784 .read = seq_read,
785 .write = proc_orig_interval_write,
786 .llseek = seq_lseek,
787 .release = single_release,
788};
789
790void cleanup_procfs(void)
791{
792 if (proc_transt_global_file)
793 remove_proc_entry(PROC_FILE_TRANST_GLOBAL, proc_batman_dir);
794
795 if (proc_transt_local_file)
796 remove_proc_entry(PROC_FILE_TRANST_LOCAL, proc_batman_dir);
797
798 if (proc_log_file)
799 remove_proc_entry(PROC_FILE_LOG, proc_batman_dir);
800
801 if (proc_log_level_file)
802 remove_proc_entry(PROC_FILE_LOG_LEVEL, proc_batman_dir);
803
804 if (proc_originators_file)
805 remove_proc_entry(PROC_FILE_ORIGINATORS, proc_batman_dir);
806
807 if (proc_orig_interval_file)
808 remove_proc_entry(PROC_FILE_ORIG_INTERVAL, proc_batman_dir);
809
810 if (proc_interface_file)
811 remove_proc_entry(PROC_FILE_INTERFACES, proc_batman_dir);
812
813 if (proc_vis_file)
814 remove_proc_entry(PROC_FILE_VIS, proc_batman_dir);
815
816 if (proc_vis_format_file)
817 remove_proc_entry(PROC_FILE_VIS_FORMAT, proc_batman_dir);
818
819 if (proc_aggr_file)
820 remove_proc_entry(PROC_FILE_AGGR, proc_batman_dir);
821
822 if (proc_batman_dir)
823#ifdef __NET_NET_NAMESPACE_H
824 remove_proc_entry(PROC_ROOT_DIR, init_net.proc_net);
825#else
826 remove_proc_entry(PROC_ROOT_DIR, proc_net);
827#endif
828}
829
830int setup_procfs(void)
831{
832#ifdef __NET_NET_NAMESPACE_H
833 proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, init_net.proc_net);
834#else
835 proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, proc_net);
836#endif
837
838 if (!proc_batman_dir) {
839 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s' folder failed\n", PROC_ROOT_DIR);
840 return -EFAULT;
841 }
842
843 proc_interface_file = create_proc_entry(PROC_FILE_INTERFACES,
844 S_IWUSR | S_IRUGO,
845 proc_batman_dir);
846 if (proc_interface_file) {
847 proc_interface_file->proc_fops = &proc_interfaces_fops;
848 } else {
849 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_INTERFACES);
850 cleanup_procfs();
851 return -EFAULT;
852 }
853
854 proc_orig_interval_file = create_proc_entry(PROC_FILE_ORIG_INTERVAL,
855 S_IWUSR | S_IRUGO,
856 proc_batman_dir);
857 if (proc_orig_interval_file) {
858 proc_orig_interval_file->proc_fops = &proc_orig_interval_fops;
859 } else {
860 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIG_INTERVAL);
861 cleanup_procfs();
862 return -EFAULT;
863 }
864
865 proc_log_level_file = create_proc_entry(PROC_FILE_LOG_LEVEL,
866 S_IWUSR | S_IRUGO,
867 proc_batman_dir);
868 if (proc_log_level_file) {
869 proc_log_level_file->proc_fops = &proc_log_level_fops;
870 } else {
871 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_LOG_LEVEL);
872 cleanup_procfs();
873 return -EFAULT;
874 }
875
876 proc_originators_file = create_proc_entry(PROC_FILE_ORIGINATORS,
877 S_IRUGO, proc_batman_dir);
878 if (proc_originators_file) {
879 proc_originators_file->proc_fops = &proc_originators_fops;
880 } else {
881 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIGINATORS);
882 cleanup_procfs();
883 return -EFAULT;
884 }
885
886 proc_log_file = create_proc_entry(PROC_FILE_LOG,
887 S_IRUGO, proc_batman_dir);
888 if (proc_log_file) {
889 proc_log_file->proc_fops = &proc_log_operations;
890 } else {
891 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_FILE_LOG, PROC_FILE_GATEWAYS);
892 cleanup_procfs();
893 return -EFAULT;
894 }
895
896 proc_transt_local_file = create_proc_entry(PROC_FILE_TRANST_LOCAL,
897 S_IRUGO, proc_batman_dir);
898 if (proc_transt_local_file) {
899 proc_transt_local_file->proc_fops = &proc_transt_local_fops;
900 } else {
901 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_LOCAL);
902 cleanup_procfs();
903 return -EFAULT;
904 }
905
906 proc_transt_global_file = create_proc_entry(PROC_FILE_TRANST_GLOBAL,
907 S_IRUGO, proc_batman_dir);
908 if (proc_transt_global_file) {
909 proc_transt_global_file->proc_fops = &proc_transt_global_fops;
910 } else {
911 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_GLOBAL);
912 cleanup_procfs();
913 return -EFAULT;
914 }
915
916 proc_vis_file = create_proc_entry(PROC_FILE_VIS, S_IWUSR | S_IRUGO,
917 proc_batman_dir);
918 if (proc_vis_file) {
919 proc_vis_file->proc_fops = &proc_vis_fops;
920 } else {
921 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS);
922 cleanup_procfs();
923 return -EFAULT;
924 }
925
926 proc_vis_format_file = create_proc_entry(PROC_FILE_VIS_FORMAT,
927 S_IWUSR | S_IRUGO,
928 proc_batman_dir);
929 if (proc_vis_format_file) {
930 proc_vis_format_file->proc_fops = &proc_vis_format_fops;
931 } else {
932 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_FORMAT);
933 cleanup_procfs();
934 return -EFAULT;
935 }
936
937 proc_aggr_file = create_proc_entry(PROC_FILE_AGGR, S_IWUSR | S_IRUGO,
938 proc_batman_dir);
939 if (proc_aggr_file) {
940 proc_aggr_file->proc_fops = &proc_aggr_fops;
941 } else {
942 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_AGGR);
943 cleanup_procfs();
944 return -EFAULT;
945 }
946
947 return 0;
948}
949
950