Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / batman-adv / hard-interface.c
1 /*
2  * Copyright (C) 2007-2010 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 "bat_sysfs.h"
29 #include "originator.h"
30 #include "hash.h"
31
32 #include <linux/if_arp.h>
33
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
35
36 /* protect update critical side of if_list - but not the content */
37 static DEFINE_SPINLOCK(if_list_lock);
38
39 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
40 {
41         struct batman_if *batman_if;
42
43         rcu_read_lock();
44         list_for_each_entry_rcu(batman_if, &if_list, list) {
45                 if (batman_if->net_dev == net_dev)
46                         goto out;
47         }
48
49         batman_if = NULL;
50
51 out:
52         if (batman_if)
53                 hardif_hold(batman_if);
54
55         rcu_read_unlock();
56         return batman_if;
57 }
58
59 static int is_valid_iface(struct net_device *net_dev)
60 {
61         if (net_dev->flags & IFF_LOOPBACK)
62                 return 0;
63
64         if (net_dev->type != ARPHRD_ETHER)
65                 return 0;
66
67         if (net_dev->addr_len != ETH_ALEN)
68                 return 0;
69
70         /* no batman over batman */
71 #ifdef HAVE_NET_DEVICE_OPS
72         if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
73                 return 0;
74 #else
75         if (net_dev->hard_start_xmit == interface_tx)
76                 return 0;
77 #endif
78
79         /* Device is being bridged */
80         /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
81                 return 0; */
82
83         return 1;
84 }
85
86 static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
87 {
88         struct batman_if *batman_if;
89
90         rcu_read_lock();
91         list_for_each_entry_rcu(batman_if, &if_list, list) {
92                 if (batman_if->soft_iface != soft_iface)
93                         continue;
94
95                 if (batman_if->if_status == IF_ACTIVE)
96                         goto out;
97         }
98
99         batman_if = NULL;
100
101 out:
102         if (batman_if)
103                 hardif_hold(batman_if);
104
105         rcu_read_unlock();
106         return batman_if;
107 }
108
109 static void update_primary_addr(struct bat_priv *bat_priv)
110 {
111         struct vis_packet *vis_packet;
112
113         vis_packet = (struct vis_packet *)
114                                 bat_priv->my_vis_info->skb_packet->data;
115         memcpy(vis_packet->vis_orig,
116                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
117         memcpy(vis_packet->sender_orig,
118                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
119 }
120
121 static void set_primary_if(struct bat_priv *bat_priv,
122                            struct batman_if *batman_if)
123 {
124         struct batman_packet *batman_packet;
125         struct batman_if *old_if;
126
127         if (batman_if)
128                 hardif_hold(batman_if);
129
130         old_if = bat_priv->primary_if;
131         bat_priv->primary_if = batman_if;
132
133         if (old_if)
134                 hardif_put(old_if);
135
136         if (!bat_priv->primary_if)
137                 return;
138
139         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
140         batman_packet->flags = PRIMARIES_FIRST_HOP;
141         batman_packet->ttl = TTL;
142
143         update_primary_addr(bat_priv);
144
145         /***
146          * hacky trick to make sure that we send the HNA information via
147          * our new primary interface
148          */
149         atomic_set(&bat_priv->hna_local_changed, 1);
150 }
151
152 static bool hardif_is_iface_up(struct batman_if *batman_if)
153 {
154         if (batman_if->net_dev->flags & IFF_UP)
155                 return true;
156
157         return false;
158 }
159
160 static void update_mac_addresses(struct batman_if *batman_if)
161 {
162         memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
163                batman_if->net_dev->dev_addr, ETH_ALEN);
164         memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
165                batman_if->net_dev->dev_addr, ETH_ALEN);
166 }
167
168 static void check_known_mac_addr(uint8_t *addr)
169 {
170         struct batman_if *batman_if;
171
172         rcu_read_lock();
173         list_for_each_entry_rcu(batman_if, &if_list, list) {
174                 if ((batman_if->if_status != IF_ACTIVE) &&
175                     (batman_if->if_status != IF_TO_BE_ACTIVATED))
176                         continue;
177
178                 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
179                         continue;
180
181                 pr_warning("The newly added mac address (%pM) already exists "
182                            "on: %s\n", addr, batman_if->net_dev->name);
183                 pr_warning("It is strongly recommended to keep mac addresses "
184                            "unique to avoid problems!\n");
185         }
186         rcu_read_unlock();
187 }
188
189 int hardif_min_mtu(struct net_device *soft_iface)
190 {
191         struct bat_priv *bat_priv = netdev_priv(soft_iface);
192         struct batman_if *batman_if;
193         /* allow big frames if all devices are capable to do so
194          * (have MTU > 1500 + BAT_HEADER_LEN) */
195         int min_mtu = ETH_DATA_LEN;
196
197         if (atomic_read(&bat_priv->frag_enabled))
198                 goto out;
199
200         rcu_read_lock();
201         list_for_each_entry_rcu(batman_if, &if_list, list) {
202                 if ((batman_if->if_status != IF_ACTIVE) &&
203                     (batman_if->if_status != IF_TO_BE_ACTIVATED))
204                         continue;
205
206                 if (batman_if->soft_iface != soft_iface)
207                         continue;
208
209                 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
210                               min_mtu);
211         }
212         rcu_read_unlock();
213 out:
214         return min_mtu;
215 }
216
217 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
218 void update_min_mtu(struct net_device *soft_iface)
219 {
220         int min_mtu;
221
222         min_mtu = hardif_min_mtu(soft_iface);
223         if (soft_iface->mtu != min_mtu)
224                 soft_iface->mtu = min_mtu;
225 }
226
227 static void hardif_activate_interface(struct batman_if *batman_if)
228 {
229         struct bat_priv *bat_priv;
230
231         if (batman_if->if_status != IF_INACTIVE)
232                 return;
233
234         bat_priv = netdev_priv(batman_if->soft_iface);
235
236         update_mac_addresses(batman_if);
237         batman_if->if_status = IF_TO_BE_ACTIVATED;
238
239         /**
240          * the first active interface becomes our primary interface or
241          * the next active interface after the old primay interface was removed
242          */
243         if (!bat_priv->primary_if)
244                 set_primary_if(bat_priv, batman_if);
245
246         bat_info(batman_if->soft_iface, "Interface activated: %s\n",
247                  batman_if->net_dev->name);
248
249         update_min_mtu(batman_if->soft_iface);
250         return;
251 }
252
253 static void hardif_deactivate_interface(struct batman_if *batman_if)
254 {
255         if ((batman_if->if_status != IF_ACTIVE) &&
256            (batman_if->if_status != IF_TO_BE_ACTIVATED))
257                 return;
258
259         batman_if->if_status = IF_INACTIVE;
260
261         bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
262                  batman_if->net_dev->name);
263
264         update_min_mtu(batman_if->soft_iface);
265 }
266
267 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
268 {
269         struct bat_priv *bat_priv;
270         struct batman_packet *batman_packet;
271
272         if (batman_if->if_status != IF_NOT_IN_USE)
273                 goto out;
274
275         batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
276
277         if (!batman_if->soft_iface) {
278                 batman_if->soft_iface = softif_create(iface_name);
279
280                 if (!batman_if->soft_iface)
281                         goto err;
282
283                 /* dev_get_by_name() increases the reference counter for us */
284                 dev_hold(batman_if->soft_iface);
285         }
286
287         bat_priv = netdev_priv(batman_if->soft_iface);
288         batman_if->packet_len = BAT_PACKET_LEN;
289         batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
290
291         if (!batman_if->packet_buff) {
292                 bat_err(batman_if->soft_iface, "Can't add interface packet "
293                         "(%s): out of memory\n", batman_if->net_dev->name);
294                 goto err;
295         }
296
297         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
298         batman_packet->packet_type = BAT_PACKET;
299         batman_packet->version = COMPAT_VERSION;
300         batman_packet->flags = 0;
301         batman_packet->ttl = 2;
302         batman_packet->tq = TQ_MAX_VALUE;
303         batman_packet->num_hna = 0;
304
305         batman_if->if_num = bat_priv->num_ifaces;
306         bat_priv->num_ifaces++;
307         batman_if->if_status = IF_INACTIVE;
308         orig_hash_add_if(batman_if, bat_priv->num_ifaces);
309
310         batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
311         batman_if->batman_adv_ptype.func = batman_skb_recv;
312         batman_if->batman_adv_ptype.dev = batman_if->net_dev;
313         hardif_hold(batman_if);
314         dev_add_pack(&batman_if->batman_adv_ptype);
315
316         atomic_set(&batman_if->seqno, 1);
317         atomic_set(&batman_if->frag_seqno, 1);
318         bat_info(batman_if->soft_iface, "Adding interface: %s\n",
319                  batman_if->net_dev->name);
320
321         if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
322                 ETH_DATA_LEN + BAT_HEADER_LEN)
323                 bat_info(batman_if->soft_iface,
324                         "The MTU of interface %s is too small (%i) to handle "
325                         "the transport of batman-adv packets. Packets going "
326                         "over this interface will be fragmented on layer2 "
327                         "which could impact the performance. Setting the MTU "
328                         "to %zi would solve the problem.\n",
329                         batman_if->net_dev->name, batman_if->net_dev->mtu,
330                         ETH_DATA_LEN + BAT_HEADER_LEN);
331
332         if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
333                 ETH_DATA_LEN + BAT_HEADER_LEN)
334                 bat_info(batman_if->soft_iface,
335                         "The MTU of interface %s is too small (%i) to handle "
336                         "the transport of batman-adv packets. If you experience"
337                         " problems getting traffic through try increasing the "
338                         "MTU to %zi.\n",
339                         batman_if->net_dev->name, batman_if->net_dev->mtu,
340                         ETH_DATA_LEN + BAT_HEADER_LEN);
341
342         if (hardif_is_iface_up(batman_if))
343                 hardif_activate_interface(batman_if);
344         else
345                 bat_err(batman_if->soft_iface, "Not using interface %s "
346                         "(retrying later): interface not active\n",
347                         batman_if->net_dev->name);
348
349         /* begin scheduling originator messages on that interface */
350         schedule_own_packet(batman_if);
351
352 out:
353         return 0;
354
355 err:
356         return -ENOMEM;
357 }
358
359 void hardif_disable_interface(struct batman_if *batman_if)
360 {
361         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
362
363         if (batman_if->if_status == IF_ACTIVE)
364                 hardif_deactivate_interface(batman_if);
365
366         if (batman_if->if_status != IF_INACTIVE)
367                 return;
368
369         bat_info(batman_if->soft_iface, "Removing interface: %s\n",
370                  batman_if->net_dev->name);
371         dev_remove_pack(&batman_if->batman_adv_ptype);
372         hardif_put(batman_if);
373
374         bat_priv->num_ifaces--;
375         orig_hash_del_if(batman_if, bat_priv->num_ifaces);
376
377         if (batman_if == bat_priv->primary_if) {
378                 struct batman_if *new_if;
379
380                 new_if = get_active_batman_if(batman_if->soft_iface);
381                 set_primary_if(bat_priv, new_if);
382
383                 if (new_if)
384                         hardif_put(new_if);
385         }
386
387         kfree(batman_if->packet_buff);
388         batman_if->packet_buff = NULL;
389         batman_if->if_status = IF_NOT_IN_USE;
390
391         /* delete all references to this batman_if */
392         purge_orig_ref(bat_priv);
393         purge_outstanding_packets(bat_priv, batman_if);
394         dev_put(batman_if->soft_iface);
395
396         /* nobody uses this interface anymore */
397         if (!bat_priv->num_ifaces)
398                 softif_destroy(batman_if->soft_iface);
399
400         batman_if->soft_iface = NULL;
401 }
402
403 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
404 {
405         struct batman_if *batman_if;
406         int ret;
407
408         ret = is_valid_iface(net_dev);
409         if (ret != 1)
410                 goto out;
411
412         dev_hold(net_dev);
413
414         batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
415         if (!batman_if) {
416                 pr_err("Can't add interface (%s): out of memory\n",
417                        net_dev->name);
418                 goto release_dev;
419         }
420
421         ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
422         if (ret)
423                 goto free_if;
424
425         batman_if->if_num = -1;
426         batman_if->net_dev = net_dev;
427         batman_if->soft_iface = NULL;
428         batman_if->if_status = IF_NOT_IN_USE;
429         INIT_LIST_HEAD(&batman_if->list);
430         atomic_set(&batman_if->refcnt, 0);
431         hardif_hold(batman_if);
432
433         check_known_mac_addr(batman_if->net_dev->dev_addr);
434
435         spin_lock(&if_list_lock);
436         list_add_tail_rcu(&batman_if->list, &if_list);
437         spin_unlock(&if_list_lock);
438
439         /* extra reference for return */
440         hardif_hold(batman_if);
441         return batman_if;
442
443 free_if:
444         kfree(batman_if);
445 release_dev:
446         dev_put(net_dev);
447 out:
448         return NULL;
449 }
450
451 static void hardif_remove_interface(struct batman_if *batman_if)
452 {
453         /* first deactivate interface */
454         if (batman_if->if_status != IF_NOT_IN_USE)
455                 hardif_disable_interface(batman_if);
456
457         if (batman_if->if_status != IF_NOT_IN_USE)
458                 return;
459
460         batman_if->if_status = IF_TO_BE_REMOVED;
461
462         /* caller must take if_list_lock */
463         list_del_rcu(&batman_if->list);
464         synchronize_rcu();
465         sysfs_del_hardif(&batman_if->hardif_obj);
466         hardif_put(batman_if);
467 }
468
469 void hardif_remove_interfaces(void)
470 {
471         struct batman_if *batman_if, *batman_if_tmp;
472
473         rtnl_lock();
474         spin_lock(&if_list_lock);
475         list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
476                 hardif_remove_interface(batman_if);
477         }
478         spin_unlock(&if_list_lock);
479         rtnl_unlock();
480 }
481
482 static int hard_if_event(struct notifier_block *this,
483                          unsigned long event, void *ptr)
484 {
485         struct net_device *net_dev = (struct net_device *)ptr;
486         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
487         struct bat_priv *bat_priv;
488
489         if (!batman_if && event == NETDEV_REGISTER)
490                 batman_if = hardif_add_interface(net_dev);
491
492         if (!batman_if)
493                 goto out;
494
495         switch (event) {
496         case NETDEV_UP:
497                 hardif_activate_interface(batman_if);
498                 break;
499         case NETDEV_GOING_DOWN:
500         case NETDEV_DOWN:
501                 hardif_deactivate_interface(batman_if);
502                 break;
503         case NETDEV_UNREGISTER:
504                 spin_lock(&if_list_lock);
505                 hardif_remove_interface(batman_if);
506                 spin_unlock(&if_list_lock);
507                 break;
508         case NETDEV_CHANGEMTU:
509                 if (batman_if->soft_iface)
510                         update_min_mtu(batman_if->soft_iface);
511                 break;
512         case NETDEV_CHANGEADDR:
513                 if (batman_if->if_status == IF_NOT_IN_USE) {
514                         hardif_put(batman_if);
515                         goto out;
516                 }
517
518                 check_known_mac_addr(batman_if->net_dev->dev_addr);
519                 update_mac_addresses(batman_if);
520
521                 bat_priv = netdev_priv(batman_if->soft_iface);
522                 if (batman_if == bat_priv->primary_if)
523                         update_primary_addr(bat_priv);
524                 break;
525         default:
526                 break;
527         };
528         hardif_put(batman_if);
529
530 out:
531         return NOTIFY_DONE;
532 }
533
534 /* receive a packet with the batman ethertype coming on a hard
535  * interface */
536 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
537         struct packet_type *ptype, struct net_device *orig_dev)
538 {
539         struct bat_priv *bat_priv;
540         struct batman_packet *batman_packet;
541         struct batman_if *batman_if;
542         int ret;
543
544         batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
545         skb = skb_share_check(skb, GFP_ATOMIC);
546
547         /* skb was released by skb_share_check() */
548         if (!skb)
549                 goto err_out;
550
551         /* packet should hold at least type and version */
552         if (unlikely(!pskb_may_pull(skb, 2)))
553                 goto err_free;
554
555         /* expect a valid ethernet header here. */
556         if (unlikely(skb->mac_len != sizeof(struct ethhdr)
557                                 || !skb_mac_header(skb)))
558                 goto err_free;
559
560         if (!batman_if->soft_iface)
561                 goto err_free;
562
563         bat_priv = netdev_priv(batman_if->soft_iface);
564
565         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
566                 goto err_free;
567
568         /* discard frames on not active interfaces */
569         if (batman_if->if_status != IF_ACTIVE)
570                 goto err_free;
571
572         batman_packet = (struct batman_packet *)skb->data;
573
574         if (batman_packet->version != COMPAT_VERSION) {
575                 bat_dbg(DBG_BATMAN, bat_priv,
576                         "Drop packet: incompatible batman version (%i)\n",
577                         batman_packet->version);
578                 goto err_free;
579         }
580
581         /* all receive handlers return whether they received or reused
582          * the supplied skb. if not, we have to free the skb. */
583
584         switch (batman_packet->packet_type) {
585                 /* batman originator packet */
586         case BAT_PACKET:
587                 ret = recv_bat_packet(skb, batman_if);
588                 break;
589
590                 /* batman icmp packet */
591         case BAT_ICMP:
592                 ret = recv_icmp_packet(skb, batman_if);
593                 break;
594
595                 /* unicast packet */
596         case BAT_UNICAST:
597                 ret = recv_unicast_packet(skb, batman_if);
598                 break;
599
600                 /* fragmented unicast packet */
601         case BAT_UNICAST_FRAG:
602                 ret = recv_ucast_frag_packet(skb, batman_if);
603                 break;
604
605                 /* broadcast packet */
606         case BAT_BCAST:
607                 ret = recv_bcast_packet(skb, batman_if);
608                 break;
609
610                 /* vis packet */
611         case BAT_VIS:
612                 ret = recv_vis_packet(skb, batman_if);
613                 break;
614         default:
615                 ret = NET_RX_DROP;
616         }
617
618         if (ret == NET_RX_DROP)
619                 kfree_skb(skb);
620
621         /* return NET_RX_SUCCESS in any case as we
622          * most probably dropped the packet for
623          * routing-logical reasons. */
624
625         return NET_RX_SUCCESS;
626
627 err_free:
628         kfree_skb(skb);
629 err_out:
630         return NET_RX_DROP;
631 }
632
633 struct notifier_block hard_if_notifier = {
634         .notifier_call = hard_if_event,
635 };