Staging: batman-adv: receive packets directly using skbs
[safe/jmp/linux-2.6] / 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         unsigned long flags;
193
194         rcu_read_lock();
195         if (list_empty(&if_list)) {
196                 rcu_read_unlock();
197                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
198                 goto end;
199         }
200
201         if (((struct batman_if *)if_list.next)->if_active != IF_ACTIVE) {
202                 rcu_read_unlock();
203                 seq_printf(seq, "BATMAN disabled - primary interface not active \n");
204                 goto end;
205         }
206
207         seq_printf(seq,
208                    "  %-14s (%s/%i) %17s [%10s]: %20s ... [B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s] \n",
209                    "Originator", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
210                    "Potential nexthops", SOURCE_VERSION, REVISION_VERSION_STR,
211                    ((struct batman_if *)if_list.next)->dev,
212                    ((struct batman_if *)if_list.next)->addr_str);
213
214         rcu_read_unlock();
215         spin_lock_irqsave(&orig_hash_lock, flags);
216
217         while (hash_iterate(orig_hash, &hashit)) {
218
219                 orig_node = hashit.bucket->data;
220
221                 if (!orig_node->router)
222                         continue;
223
224                 if (orig_node->router->tq_avg == 0)
225                         continue;
226
227                 batman_count++;
228
229                 addr_to_string(orig_str, orig_node->orig);
230                 addr_to_string(router_str, orig_node->router->addr);
231
232                 seq_printf(seq, "%-17s  (%3i) %17s [%10s]:",
233                            orig_str, orig_node->router->tq_avg,
234                            router_str, orig_node->router->if_incoming->dev);
235
236                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
237                         addr_to_string(orig_str, neigh_node->addr);
238                         seq_printf(seq, " %17s (%3i)",
239                                    orig_str, neigh_node->tq_avg);
240                 }
241
242                 seq_printf(seq, "\n");
243
244         }
245
246         spin_unlock_irqrestore(&orig_hash_lock, flags);
247
248         if (batman_count == 0)
249                 seq_printf(seq, "No batman nodes in range ... \n");
250
251 end:
252         return 0;
253 }
254
255 static int proc_originators_open(struct inode *inode, struct file *file)
256 {
257         return single_open(file, proc_originators_read, NULL);
258 }
259
260 static int proc_transt_local_read(struct seq_file *seq, void *offset)
261 {
262         char *buf;
263
264         buf = kmalloc(4096, GFP_KERNEL);
265         if (!buf)
266                 return 0;
267
268         rcu_read_lock();
269         if (list_empty(&if_list)) {
270                 rcu_read_unlock();
271                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
272                 goto end;
273         }
274
275         rcu_read_unlock();
276
277         seq_printf(seq, "Locally retrieved addresses (from %s) announced via HNA:\n", soft_device->name);
278
279         hna_local_fill_buffer_text(buf, 4096);
280         seq_printf(seq, "%s", buf);
281
282 end:
283         kfree(buf);
284         return 0;
285 }
286
287 static int proc_transt_local_open(struct inode *inode, struct file *file)
288 {
289         return single_open(file, proc_transt_local_read, NULL);
290 }
291
292 static int proc_transt_global_read(struct seq_file *seq, void *offset)
293 {
294         char *buf;
295
296         buf = kmalloc(4096, GFP_KERNEL);
297         if (!buf)
298                 return 0;
299
300         rcu_read_lock();
301         if (list_empty(&if_list)) {
302                 rcu_read_unlock();
303                 seq_printf(seq, "BATMAN disabled - please specify interfaces to enable it \n");
304                 goto end;
305         }
306         rcu_read_unlock();
307
308
309         seq_printf(seq, "Globally announced HNAs received via the mesh (translation table):\n");
310
311         hna_global_fill_buffer_text(buf, 4096);
312         seq_printf(seq, "%s", buf);
313
314 end:
315         kfree(buf);
316         return 0;
317 }
318
319 static int proc_transt_global_open(struct inode *inode, struct file *file)
320 {
321         return single_open(file, proc_transt_global_read, NULL);
322 }
323
324 /* setting the mode of the vis server by the user */
325 static ssize_t proc_vis_srv_write(struct file *file, const char __user * buffer,
326                               size_t count, loff_t *ppos)
327 {
328         char *vis_mode_string;
329         int not_copied = 0;
330
331         vis_mode_string = kmalloc(count, GFP_KERNEL);
332
333         if (!vis_mode_string)
334                 return -ENOMEM;
335
336         not_copied = copy_from_user(vis_mode_string, buffer, count);
337         vis_mode_string[count - not_copied - 1] = 0;
338
339         if ((strcmp(vis_mode_string, "client") == 0) ||
340                         (strcmp(vis_mode_string, "disabled") == 0)) {
341                 printk(KERN_INFO "batman-adv:Setting VIS mode to client (disabling vis server)\n");
342                 vis_set_mode(VIS_TYPE_CLIENT_UPDATE);
343         } else if ((strcmp(vis_mode_string, "server") == 0) ||
344                         (strcmp(vis_mode_string, "enabled") == 0)) {
345                 printk(KERN_INFO "batman-adv:Setting VIS mode to server (enabling vis server)\n");
346                 vis_set_mode(VIS_TYPE_SERVER_SYNC);
347         } else
348                 printk(KERN_ERR "batman-adv:Unknown VIS mode: %s\n",
349                        vis_mode_string);
350
351         kfree(vis_mode_string);
352         return count;
353 }
354
355 static int proc_vis_srv_read(struct seq_file *seq, void *offset)
356 {
357         int vis_server = is_vis_server();
358
359         seq_printf(seq, "[%c] client mode (server disabled) \n",
360                         (!vis_server) ? 'x' : ' ');
361         seq_printf(seq, "[%c] server mode (server enabled) \n",
362                         (vis_server) ? 'x' : ' ');
363
364         return 0;
365 }
366
367 static int proc_vis_srv_open(struct inode *inode, struct file *file)
368 {
369         return single_open(file, proc_vis_srv_read, NULL);
370 }
371
372 static int proc_vis_data_read(struct seq_file *seq, void *offset)
373 {
374         HASHIT(hashit);
375         struct vis_info *info;
376         struct vis_info_entry *entries;
377         HLIST_HEAD(vis_if_list);
378         int i;
379         char tmp_addr_str[ETH_STR_LEN];
380         unsigned long flags;
381
382         rcu_read_lock();
383         if (list_empty(&if_list) || (!is_vis_server())) {
384                 rcu_read_unlock();
385                 goto end;
386         }
387
388         rcu_read_unlock();
389
390         spin_lock_irqsave(&vis_hash_lock, flags);
391         while (hash_iterate(vis_hash, &hashit)) {
392                 info = hashit.bucket->data;
393                 entries = (struct vis_info_entry *)
394                         ((char *)info + sizeof(struct vis_info));
395                 addr_to_string(tmp_addr_str, info->packet.vis_orig);
396                 seq_printf(seq, "%s,", tmp_addr_str);
397
398                 for (i = 0; i < info->packet.entries; i++) {
399                         proc_vis_read_entry(seq, &entries[i], &vis_if_list,
400                                             info->packet.vis_orig);
401                 }
402
403                 /* add primary/secondary records */
404                 proc_vis_read_prim_sec(seq, &vis_if_list);
405                 seq_printf(seq, "\n");
406         }
407         spin_unlock_irqrestore(&vis_hash_lock, flags);
408
409 end:
410         return 0;
411 }
412
413 static int proc_vis_data_open(struct inode *inode, struct file *file)
414 {
415         return single_open(file, proc_vis_data_read, NULL);
416 }
417
418 static int proc_aggr_read(struct seq_file *seq, void *offset)
419 {
420         seq_printf(seq, "%i\n", atomic_read(&aggregation_enabled));
421
422         return 0;
423 }
424
425 static ssize_t proc_aggr_write(struct file *file, const char __user *buffer,
426                                size_t count, loff_t *ppos)
427 {
428         char *aggr_string;
429         int not_copied = 0;
430         unsigned long aggregation_enabled_tmp;
431
432         aggr_string = kmalloc(count, GFP_KERNEL);
433
434         if (!aggr_string)
435                 return -ENOMEM;
436
437         not_copied = copy_from_user(aggr_string, buffer, count);
438         aggr_string[count - not_copied - 1] = 0;
439
440         strict_strtoul(aggr_string, 10, &aggregation_enabled_tmp);
441
442         if ((aggregation_enabled_tmp != 0) && (aggregation_enabled_tmp != 1)) {
443                 printk(KERN_ERR "batman-adv:Aggregation can only be enabled (1) or disabled (0), given value: %li\n", aggregation_enabled_tmp);
444                 goto end;
445         }
446
447         printk(KERN_INFO "batman-adv:Changing aggregation from: %s (%i) to: %s (%li)\n",
448                (atomic_read(&aggregation_enabled) == 1 ?
449                 "enabled" : "disabled"),
450                atomic_read(&aggregation_enabled),
451                (aggregation_enabled_tmp == 1 ? "enabled" : "disabled"),
452                aggregation_enabled_tmp);
453
454         atomic_set(&aggregation_enabled, (unsigned)aggregation_enabled_tmp);
455 end:
456         kfree(aggr_string);
457         return count;
458 }
459
460 static int proc_aggr_open(struct inode *inode, struct file *file)
461 {
462         return single_open(file, proc_aggr_read, NULL);
463 }
464
465 /* satisfying different prototypes ... */
466 static ssize_t proc_dummy_write(struct file *file, const char __user *buffer,
467                                 size_t count, loff_t *ppos)
468 {
469         return count;
470 }
471
472 static const struct file_operations proc_aggr_fops = {
473         .owner          = THIS_MODULE,
474         .open           = proc_aggr_open,
475         .read           = seq_read,
476         .write          = proc_aggr_write,
477         .llseek         = seq_lseek,
478         .release        = single_release,
479 };
480
481 static const struct file_operations proc_vis_srv_fops = {
482         .owner          = THIS_MODULE,
483         .open           = proc_vis_srv_open,
484         .read           = seq_read,
485         .write          = proc_vis_srv_write,
486         .llseek         = seq_lseek,
487         .release        = single_release,
488 };
489
490 static const struct file_operations proc_vis_data_fops = {
491         .owner          = THIS_MODULE,
492         .open           = proc_vis_data_open,
493         .read           = seq_read,
494         .write          = proc_dummy_write,
495         .llseek         = seq_lseek,
496         .release        = single_release,
497 };
498
499 static const struct file_operations proc_originators_fops = {
500         .owner          = THIS_MODULE,
501         .open           = proc_originators_open,
502         .read           = seq_read,
503         .write          = proc_dummy_write,
504         .llseek         = seq_lseek,
505         .release        = single_release,
506 };
507
508 static const struct file_operations proc_transt_local_fops = {
509         .owner          = THIS_MODULE,
510         .open           = proc_transt_local_open,
511         .read           = seq_read,
512         .write          = proc_dummy_write,
513         .llseek         = seq_lseek,
514         .release        = single_release,
515 };
516
517 static const struct file_operations proc_transt_global_fops = {
518         .owner          = THIS_MODULE,
519         .open           = proc_transt_global_open,
520         .read           = seq_read,
521         .write          = proc_dummy_write,
522         .llseek         = seq_lseek,
523         .release        = single_release,
524 };
525
526 static const struct file_operations proc_interfaces_fops = {
527         .owner          = THIS_MODULE,
528         .open           = proc_interfaces_open,
529         .read           = seq_read,
530         .write          = proc_interfaces_write,
531         .llseek         = seq_lseek,
532         .release        = single_release,
533 };
534
535 static const struct file_operations proc_orig_interval_fops = {
536         .owner          = THIS_MODULE,
537         .open           = proc_orig_interval_open,
538         .read           = seq_read,
539         .write          = proc_orig_interval_write,
540         .llseek         = seq_lseek,
541         .release        = single_release,
542 };
543
544 void cleanup_procfs(void)
545 {
546         if (proc_transt_global_file)
547                 remove_proc_entry(PROC_FILE_TRANST_GLOBAL, proc_batman_dir);
548
549         if (proc_transt_local_file)
550                 remove_proc_entry(PROC_FILE_TRANST_LOCAL, proc_batman_dir);
551
552         if (proc_originators_file)
553                 remove_proc_entry(PROC_FILE_ORIGINATORS, proc_batman_dir);
554
555         if (proc_orig_interval_file)
556                 remove_proc_entry(PROC_FILE_ORIG_INTERVAL, proc_batman_dir);
557
558         if (proc_interface_file)
559                 remove_proc_entry(PROC_FILE_INTERFACES, proc_batman_dir);
560
561         if (proc_vis_data_file)
562                 remove_proc_entry(PROC_FILE_VIS_DATA, proc_batman_dir);
563
564         if (proc_vis_srv_file)
565                 remove_proc_entry(PROC_FILE_VIS_SRV, proc_batman_dir);
566
567         if (proc_aggr_file)
568                 remove_proc_entry(PROC_FILE_AGGR, proc_batman_dir);
569
570         if (proc_batman_dir)
571 #ifdef __NET_NET_NAMESPACE_H
572                 remove_proc_entry(PROC_ROOT_DIR, init_net.proc_net);
573 #else
574                 remove_proc_entry(PROC_ROOT_DIR, proc_net);
575 #endif
576 }
577
578 int setup_procfs(void)
579 {
580 #ifdef __NET_NET_NAMESPACE_H
581         proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, init_net.proc_net);
582 #else
583         proc_batman_dir = proc_mkdir(PROC_ROOT_DIR, proc_net);
584 #endif
585
586         if (!proc_batman_dir) {
587                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s' folder failed\n", PROC_ROOT_DIR);
588                 return -EFAULT;
589         }
590
591         proc_interface_file = create_proc_entry(PROC_FILE_INTERFACES,
592                                                 S_IWUSR | S_IRUGO,
593                                                 proc_batman_dir);
594         if (proc_interface_file) {
595                 proc_interface_file->proc_fops = &proc_interfaces_fops;
596         } else {
597                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_INTERFACES);
598                 cleanup_procfs();
599                 return -EFAULT;
600         }
601
602         proc_orig_interval_file = create_proc_entry(PROC_FILE_ORIG_INTERVAL,
603                                                     S_IWUSR | S_IRUGO,
604                                                     proc_batman_dir);
605         if (proc_orig_interval_file) {
606                 proc_orig_interval_file->proc_fops = &proc_orig_interval_fops;
607         } else {
608                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIG_INTERVAL);
609                 cleanup_procfs();
610                 return -EFAULT;
611         }
612
613         proc_originators_file = create_proc_entry(PROC_FILE_ORIGINATORS,
614                                                   S_IRUGO, proc_batman_dir);
615         if (proc_originators_file) {
616                 proc_originators_file->proc_fops = &proc_originators_fops;
617         } else {
618                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_ORIGINATORS);
619                 cleanup_procfs();
620                 return -EFAULT;
621         }
622
623         proc_transt_local_file = create_proc_entry(PROC_FILE_TRANST_LOCAL,
624                                                    S_IRUGO, proc_batman_dir);
625         if (proc_transt_local_file) {
626                 proc_transt_local_file->proc_fops = &proc_transt_local_fops;
627         } else {
628                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_LOCAL);
629                 cleanup_procfs();
630                 return -EFAULT;
631         }
632
633         proc_transt_global_file = create_proc_entry(PROC_FILE_TRANST_GLOBAL,
634                                                     S_IRUGO, proc_batman_dir);
635         if (proc_transt_global_file) {
636                 proc_transt_global_file->proc_fops = &proc_transt_global_fops;
637         } else {
638                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_TRANST_GLOBAL);
639                 cleanup_procfs();
640                 return -EFAULT;
641         }
642
643         proc_vis_srv_file = create_proc_entry(PROC_FILE_VIS_SRV,
644                                                 S_IWUSR | S_IRUGO,
645                                                 proc_batman_dir);
646         if (proc_vis_srv_file) {
647                 proc_vis_srv_file->proc_fops = &proc_vis_srv_fops;
648         } else {
649                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_SRV);
650                 cleanup_procfs();
651                 return -EFAULT;
652         }
653
654         proc_vis_data_file = create_proc_entry(PROC_FILE_VIS_DATA, S_IRUGO,
655                                           proc_batman_dir);
656         if (proc_vis_data_file) {
657                 proc_vis_data_file->proc_fops = &proc_vis_data_fops;
658         } else {
659                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_VIS_DATA);
660                 cleanup_procfs();
661                 return -EFAULT;
662         }
663
664         proc_aggr_file = create_proc_entry(PROC_FILE_AGGR, S_IWUSR | S_IRUGO,
665                                            proc_batman_dir);
666         if (proc_aggr_file) {
667                 proc_aggr_file->proc_fops = &proc_aggr_fops;
668         } else {
669                 printk(KERN_ERR "batman-adv: Registering the '/proc/net/%s/%s' file failed\n", PROC_ROOT_DIR, PROC_FILE_AGGR);
670                 cleanup_procfs();
671                 return -EFAULT;
672         }
673
674         return 0;
675 }
676
677