Staging: batman-adv: fix module initialization
[safe/jmp/linux-2.6] / drivers / staging / batman-adv / hard-interface.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 "hard-interface.h"
24 #include "soft-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "routing.h"
28 #include "hash.h"
29
30 #define MIN(x, y) ((x) < (y) ? (x) : (y))
31
32 static char avail_ifs;
33 static char active_ifs;
34
35 static void hardif_free_interface(struct rcu_head *rcu);
36
37 static struct batman_if *get_batman_if_by_name(char *name)
38 {
39         struct batman_if *batman_if;
40
41         rcu_read_lock();
42         list_for_each_entry_rcu(batman_if, &if_list, list) {
43                 if (strncmp(batman_if->dev, name, IFNAMSIZ) == 0)
44                         goto out;
45         }
46
47         batman_if = NULL;
48
49 out:
50         rcu_read_unlock();
51         return batman_if;
52 }
53
54 int hardif_min_mtu(void)
55 {
56         struct batman_if *batman_if;
57         /* allow big frames if all devices are capable to do so
58          * (have MTU > 1500 + BAT_HEADER_LEN) */
59         int min_mtu = ETH_DATA_LEN;
60
61         rcu_read_lock();
62         list_for_each_entry_rcu(batman_if, &if_list, list) {
63                 if ((batman_if->if_active == IF_ACTIVE) ||
64                     (batman_if->if_active == IF_TO_BE_ACTIVATED))
65                         min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
66                                       min_mtu);
67         }
68         rcu_read_unlock();
69
70         return min_mtu;
71 }
72
73 static void check_known_mac_addr(uint8_t *addr)
74 {
75         struct batman_if *batman_if;
76
77         rcu_read_lock();
78         list_for_each_entry_rcu(batman_if, &if_list, list) {
79                 if ((batman_if->if_active != IF_ACTIVE) &&
80                     (batman_if->if_active != IF_TO_BE_ACTIVATED))
81                         continue;
82
83                 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
84                         continue;
85
86                 printk(KERN_WARNING "batman-adv:The newly added mac address (%pM) already exists on: %s\n",
87                        addr, batman_if->dev);
88                 printk(KERN_WARNING "batman-adv:It is strongly recommended to keep mac addresses unique to avoid problems!\n");
89         }
90         rcu_read_unlock();
91 }
92
93 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
94 void update_min_mtu(void)
95 {
96         int min_mtu;
97
98         min_mtu = hardif_min_mtu();
99         if (soft_device->mtu != min_mtu)
100                 soft_device->mtu = min_mtu;
101 }
102
103 /* checks if the interface is up. (returns 1 if it is) */
104 static int hardif_is_interface_up(char *dev)
105 {
106         struct net_device *net_dev;
107
108         /**
109          * if we already have an interface in our interface list and
110          * the current interface is not the primary interface and
111          * the primary interface is not up and
112          * the primary interface has never been up - don't activate any
113          * secondary interface !
114          */
115
116         rcu_read_lock();
117         if ((!list_empty(&if_list)) &&
118             strncmp(((struct batman_if *)if_list.next)->dev, dev, IFNAMSIZ) &&
119             !(((struct batman_if *)if_list.next)->if_active == IF_ACTIVE) &&
120             !(((struct batman_if *)if_list.next)->if_active == IF_TO_BE_ACTIVATED) &&
121             (!main_if_was_up())) {
122                 rcu_read_unlock();
123                 goto end;
124         }
125         rcu_read_unlock();
126
127 #ifdef __NET_NET_NAMESPACE_H
128         net_dev = dev_get_by_name(&init_net, dev);
129 #else
130         net_dev = dev_get_by_name(dev);
131 #endif
132         if (!net_dev)
133                 goto end;
134
135         if (!(net_dev->flags & IFF_UP))
136                 goto failure;
137
138         dev_put(net_dev);
139         return 1;
140
141 failure:
142         dev_put(net_dev);
143 end:
144         return 0;
145 }
146
147 /* deactivates the interface. */
148 void hardif_deactivate_interface(struct batman_if *batman_if)
149 {
150         if (batman_if->if_active != IF_ACTIVE)
151                 return;
152
153         /**
154          * batman_if->net_dev has been acquired by dev_get_by_name() in
155          * proc_interfaces_write() and has to be unreferenced.
156          */
157
158         if (batman_if->net_dev)
159                 dev_put(batman_if->net_dev);
160
161         batman_if->if_active = IF_INACTIVE;
162         active_ifs--;
163
164         printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
165                   batman_if->dev);
166 }
167
168 /* (re)activate given interface. */
169 static void hardif_activate_interface(struct batman_if *batman_if)
170 {
171         if (batman_if->if_active != IF_INACTIVE)
172                 return;
173
174 #ifdef __NET_NET_NAMESPACE_H
175         batman_if->net_dev = dev_get_by_name(&init_net, batman_if->dev);
176 #else
177         batman_if->net_dev = dev_get_by_name(batman_if->dev);
178 #endif
179         if (!batman_if->net_dev)
180                 goto dev_err;
181
182         check_known_mac_addr(batman_if->net_dev->dev_addr);
183
184         addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
185
186         memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
187                batman_if->net_dev->dev_addr, ETH_ALEN);
188         memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
189                batman_if->net_dev->dev_addr, ETH_ALEN);
190
191         batman_if->if_active = IF_TO_BE_ACTIVATED;
192         active_ifs++;
193
194         /* save the mac address if it is our primary interface */
195         if (batman_if->if_num == 0)
196                 set_main_if_addr(batman_if->net_dev->dev_addr);
197
198         printk(KERN_INFO "batman-adv:Interface activated: %s\n",
199                   batman_if->dev);
200
201         return;
202
203 dev_err:
204         batman_if->net_dev = NULL;
205 }
206
207 static void hardif_free_interface(struct rcu_head *rcu)
208 {
209         struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
210
211         kfree(batman_if->packet_buff);
212         kfree(batman_if->dev);
213         kfree(batman_if);
214 }
215
216 /**
217  * called by
218  *  - echo '' > /proc/.../interfaces
219  *  - modprobe -r batman-adv-core
220  */
221 /* removes and frees all interfaces */
222 void hardif_remove_interfaces(void)
223 {
224         struct batman_if *batman_if = NULL;
225
226         avail_ifs = 0;
227
228         /* no lock needed - we don't delete somewhere else */
229         list_for_each_entry(batman_if, &if_list, list) {
230
231                 list_del_rcu(&batman_if->list);
232
233                 /* first deactivate interface */
234                 if (batman_if->if_active != IF_INACTIVE)
235                         hardif_deactivate_interface(batman_if);
236
237                 call_rcu(&batman_if->rcu, hardif_free_interface);
238         }
239 }
240
241 static int resize_orig(struct orig_node *orig_node, int if_num)
242 {
243         void *data_ptr;
244
245         data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
246                            GFP_ATOMIC);
247         if (!data_ptr) {
248                 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
249                 return -1;
250         }
251
252         memcpy(data_ptr, orig_node->bcast_own,
253                if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS);
254         kfree(orig_node->bcast_own);
255         orig_node->bcast_own = data_ptr;
256
257         data_ptr = kmalloc((if_num + 1) * sizeof(uint8_t), GFP_ATOMIC);
258         if (!data_ptr) {
259                 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
260                 return -1;
261         }
262
263         memcpy(data_ptr, orig_node->bcast_own_sum, if_num * sizeof(uint8_t));
264         kfree(orig_node->bcast_own_sum);
265         orig_node->bcast_own_sum = data_ptr;
266
267         return 0;
268 }
269
270
271 /* adds an interface the interface list and activate it, if possible */
272 int hardif_add_interface(char *dev, int if_num)
273 {
274         struct batman_if *batman_if;
275         struct batman_packet *batman_packet;
276         struct orig_node *orig_node;
277         unsigned long flags;
278         HASHIT(hashit);
279
280         batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
281
282         if (!batman_if) {
283                 printk(KERN_ERR "batman-adv:Can't add interface (%s): out of memory\n", dev);
284                 return -1;
285         }
286
287         batman_if->net_dev = NULL;
288
289         if ((if_num == 0) && (num_hna > 0))
290                 batman_if->packet_len = BAT_PACKET_LEN + num_hna * ETH_ALEN;
291         else
292                 batman_if->packet_len = BAT_PACKET_LEN;
293
294         batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_KERNEL);
295
296         if (!batman_if->packet_buff) {
297                 printk(KERN_ERR "batman-adv:Can't add interface packet (%s): out of memory\n", dev);
298                 goto out;
299         }
300
301         batman_if->if_num = if_num;
302         batman_if->dev = dev;
303         batman_if->if_active = IF_INACTIVE;
304         INIT_RCU_HEAD(&batman_if->rcu);
305
306         printk(KERN_INFO "batman-adv:Adding interface: %s\n", dev);
307         avail_ifs++;
308
309         INIT_LIST_HEAD(&batman_if->list);
310
311         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
312         batman_packet->packet_type = BAT_PACKET;
313         batman_packet->version = COMPAT_VERSION;
314         batman_packet->flags = 0x00;
315         batman_packet->ttl = (batman_if->if_num > 0 ? 2 : TTL);
316         batman_packet->flags = 0;
317         batman_packet->tq = TQ_MAX_VALUE;
318         batman_packet->num_hna = 0;
319
320         if (batman_if->packet_len != BAT_PACKET_LEN) {
321                 unsigned char *hna_buff;
322                 int hna_len;
323
324                 hna_buff = batman_if->packet_buff + BAT_PACKET_LEN;
325                 hna_len = batman_if->packet_len - BAT_PACKET_LEN;
326                 batman_packet->num_hna = hna_local_fill_buffer(hna_buff,
327                                                                hna_len);
328         }
329
330         atomic_set(&batman_if->seqno, 1);
331
332         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
333          * if_num */
334         spin_lock_irqsave(&orig_hash_lock, flags);
335
336         while (hash_iterate(orig_hash, &hashit)) {
337                 orig_node = hashit.bucket->data;
338                 if (resize_orig(orig_node, if_num) == -1) {
339                         spin_unlock_irqrestore(&orig_hash_lock, flags);
340                         goto out;
341                 }
342         }
343
344         spin_unlock_irqrestore(&orig_hash_lock, flags);
345
346         if (!hardif_is_interface_up(batman_if->dev))
347                 printk(KERN_ERR "batman-adv:Not using interface %s (retrying later): interface not active\n", batman_if->dev);
348         else
349                 hardif_activate_interface(batman_if);
350
351         list_add_tail_rcu(&batman_if->list, &if_list);
352
353         /* begin sending originator messages on that interface */
354         schedule_own_packet(batman_if);
355         return 1;
356
357 out:
358         kfree(batman_if->packet_buff);
359         kfree(batman_if);
360         kfree(dev);
361         return -1;
362 }
363
364 char hardif_get_active_if_num(void)
365 {
366         return active_ifs;
367 }
368
369 static int hard_if_event(struct notifier_block *this,
370                             unsigned long event, void *ptr)
371 {
372         struct net_device *dev = (struct net_device *)ptr;
373         struct batman_if *batman_if = get_batman_if_by_name(dev->name);
374
375         if (!batman_if)
376                 goto out;
377
378         switch (event) {
379         case NETDEV_GOING_DOWN:
380         case NETDEV_DOWN:
381         case NETDEV_UNREGISTER:
382                 hardif_deactivate_interface(batman_if);
383                 break;
384         case NETDEV_UP:
385                 hardif_activate_interface(batman_if);
386                 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
387                     (hardif_get_active_if_num() > 0)) {
388                         activate_module();
389                 }
390                 break;
391         /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
392         default:
393                 break;
394         };
395
396         update_min_mtu();
397
398 out:
399         return NOTIFY_DONE;
400 }
401
402 /* find batman interface by netdev. assumes rcu_read_lock on */
403 static struct batman_if *find_batman_if(struct net_device *dev)
404 {
405         struct batman_if *batman_if;
406
407         rcu_read_lock();
408         list_for_each_entry_rcu(batman_if, &if_list, list) {
409                 if (batman_if->net_dev == dev) {
410                         rcu_read_unlock();
411                         return batman_if;
412                 }
413         }
414         rcu_read_unlock();
415         return NULL;
416 }
417
418
419 /* receive a packet with the batman ethertype coming on a hard
420  * interface */
421 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
422         struct packet_type *ptype, struct net_device *orig_dev)
423 {
424         struct batman_packet *batman_packet;
425         struct batman_if *batman_if;
426         struct net_device_stats *stats;
427         int ret;
428
429         skb = skb_share_check(skb, GFP_ATOMIC);
430
431         /* skb was released by skb_share_check() */
432         if (!skb)
433                 goto err_out;
434
435         if (atomic_read(&module_state) != MODULE_ACTIVE)
436                 goto err_free;
437
438         /* packet should hold at least type and version */
439         if (unlikely(skb_headlen(skb) < 2))
440                 goto err_free;
441
442         /* expect a valid ethernet header here. */
443         if (unlikely(skb->mac_len != sizeof(struct ethhdr)
444                                 || !skb_mac_header(skb)))
445                 goto err_free;
446
447         batman_if = find_batman_if(skb->dev);
448         if (!batman_if)
449                 goto err_free;
450
451         /* discard frames on not active interfaces */
452         if (batman_if->if_active != IF_ACTIVE)
453                 goto err_free;
454
455         stats = (struct net_device_stats *)dev_get_stats(skb->dev);
456         if (stats) {
457                 stats->rx_packets++;
458                 stats->rx_bytes += skb->len;
459         }
460
461         batman_packet = (struct batman_packet *)skb->data;
462
463         if (batman_packet->version != COMPAT_VERSION) {
464                 bat_dbg(DBG_BATMAN,
465                         "Drop packet: incompatible batman version (%i)\n",
466                         batman_packet->version);
467                 goto err_free;
468         }
469
470         /* all receive handlers return whether they received or reused
471          * the supplied skb. if not, we have to free the skb. */
472
473         switch (batman_packet->packet_type) {
474                 /* batman originator packet */
475         case BAT_PACKET:
476                 ret = recv_bat_packet(skb, batman_if);
477                 break;
478
479                 /* batman icmp packet */
480         case BAT_ICMP:
481                 ret = recv_icmp_packet(skb);
482                 break;
483
484                 /* unicast packet */
485         case BAT_UNICAST:
486                 ret = recv_unicast_packet(skb);
487                 break;
488
489                 /* broadcast packet */
490         case BAT_BCAST:
491                 ret = recv_bcast_packet(skb);
492                 break;
493
494                 /* vis packet */
495         case BAT_VIS:
496                 ret = recv_vis_packet(skb);
497                 break;
498         default:
499                 ret = NET_RX_DROP;
500         }
501
502         if (ret == NET_RX_DROP)
503                 kfree_skb(skb);
504
505         /* return NET_RX_SUCCESS in any case as we
506          * most probably dropped the packet for
507          * routing-logical reasons. */
508
509         return NET_RX_SUCCESS;
510
511 err_free:
512         kfree_skb(skb);
513 err_out:
514         return NET_RX_DROP;
515 }
516
517
518 struct notifier_block hard_if_notifier = {
519         .notifier_call = hard_if_event,
520 };