Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mjg59/platf...
[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(struct net_device *net_dev)
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 (batman_if->net_dev == net_dev)
179                         continue;
180
181                 if (!compare_orig(batman_if->net_dev->dev_addr,
182                                   net_dev->dev_addr))
183                         continue;
184
185                 pr_warning("The newly added mac address (%pM) already exists "
186                            "on: %s\n", net_dev->dev_addr,
187                            batman_if->net_dev->name);
188                 pr_warning("It is strongly recommended to keep mac addresses "
189                            "unique to avoid problems!\n");
190         }
191         rcu_read_unlock();
192 }
193
194 int hardif_min_mtu(struct net_device *soft_iface)
195 {
196         struct bat_priv *bat_priv = netdev_priv(soft_iface);
197         struct batman_if *batman_if;
198         /* allow big frames if all devices are capable to do so
199          * (have MTU > 1500 + BAT_HEADER_LEN) */
200         int min_mtu = ETH_DATA_LEN;
201
202         if (atomic_read(&bat_priv->frag_enabled))
203                 goto out;
204
205         rcu_read_lock();
206         list_for_each_entry_rcu(batman_if, &if_list, list) {
207                 if ((batman_if->if_status != IF_ACTIVE) &&
208                     (batman_if->if_status != IF_TO_BE_ACTIVATED))
209                         continue;
210
211                 if (batman_if->soft_iface != soft_iface)
212                         continue;
213
214                 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
215                               min_mtu);
216         }
217         rcu_read_unlock();
218 out:
219         return min_mtu;
220 }
221
222 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
223 void update_min_mtu(struct net_device *soft_iface)
224 {
225         int min_mtu;
226
227         min_mtu = hardif_min_mtu(soft_iface);
228         if (soft_iface->mtu != min_mtu)
229                 soft_iface->mtu = min_mtu;
230 }
231
232 static void hardif_activate_interface(struct batman_if *batman_if)
233 {
234         struct bat_priv *bat_priv;
235
236         if (batman_if->if_status != IF_INACTIVE)
237                 return;
238
239         bat_priv = netdev_priv(batman_if->soft_iface);
240
241         update_mac_addresses(batman_if);
242         batman_if->if_status = IF_TO_BE_ACTIVATED;
243
244         /**
245          * the first active interface becomes our primary interface or
246          * the next active interface after the old primay interface was removed
247          */
248         if (!bat_priv->primary_if)
249                 set_primary_if(bat_priv, batman_if);
250
251         bat_info(batman_if->soft_iface, "Interface activated: %s\n",
252                  batman_if->net_dev->name);
253
254         update_min_mtu(batman_if->soft_iface);
255         return;
256 }
257
258 static void hardif_deactivate_interface(struct batman_if *batman_if)
259 {
260         if ((batman_if->if_status != IF_ACTIVE) &&
261            (batman_if->if_status != IF_TO_BE_ACTIVATED))
262                 return;
263
264         batman_if->if_status = IF_INACTIVE;
265
266         bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
267                  batman_if->net_dev->name);
268
269         update_min_mtu(batman_if->soft_iface);
270 }
271
272 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
273 {
274         struct bat_priv *bat_priv;
275         struct batman_packet *batman_packet;
276
277         if (batman_if->if_status != IF_NOT_IN_USE)
278                 goto out;
279
280         batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
281
282         if (!batman_if->soft_iface) {
283                 batman_if->soft_iface = softif_create(iface_name);
284
285                 if (!batman_if->soft_iface)
286                         goto err;
287
288                 /* dev_get_by_name() increases the reference counter for us */
289                 dev_hold(batman_if->soft_iface);
290         }
291
292         bat_priv = netdev_priv(batman_if->soft_iface);
293         batman_if->packet_len = BAT_PACKET_LEN;
294         batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
295
296         if (!batman_if->packet_buff) {
297                 bat_err(batman_if->soft_iface, "Can't add interface packet "
298                         "(%s): out of memory\n", batman_if->net_dev->name);
299                 goto err;
300         }
301
302         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
303         batman_packet->packet_type = BAT_PACKET;
304         batman_packet->version = COMPAT_VERSION;
305         batman_packet->flags = 0;
306         batman_packet->ttl = 2;
307         batman_packet->tq = TQ_MAX_VALUE;
308         batman_packet->num_hna = 0;
309
310         batman_if->if_num = bat_priv->num_ifaces;
311         bat_priv->num_ifaces++;
312         batman_if->if_status = IF_INACTIVE;
313         orig_hash_add_if(batman_if, bat_priv->num_ifaces);
314
315         batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
316         batman_if->batman_adv_ptype.func = batman_skb_recv;
317         batman_if->batman_adv_ptype.dev = batman_if->net_dev;
318         hardif_hold(batman_if);
319         dev_add_pack(&batman_if->batman_adv_ptype);
320
321         atomic_set(&batman_if->seqno, 1);
322         atomic_set(&batman_if->frag_seqno, 1);
323         bat_info(batman_if->soft_iface, "Adding interface: %s\n",
324                  batman_if->net_dev->name);
325
326         if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
327                 ETH_DATA_LEN + BAT_HEADER_LEN)
328                 bat_info(batman_if->soft_iface,
329                         "The MTU of interface %s is too small (%i) to handle "
330                         "the transport of batman-adv packets. Packets going "
331                         "over this interface will be fragmented on layer2 "
332                         "which could impact the performance. Setting the MTU "
333                         "to %zi would solve the problem.\n",
334                         batman_if->net_dev->name, batman_if->net_dev->mtu,
335                         ETH_DATA_LEN + BAT_HEADER_LEN);
336
337         if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
338                 ETH_DATA_LEN + BAT_HEADER_LEN)
339                 bat_info(batman_if->soft_iface,
340                         "The MTU of interface %s is too small (%i) to handle "
341                         "the transport of batman-adv packets. If you experience"
342                         " problems getting traffic through try increasing the "
343                         "MTU to %zi.\n",
344                         batman_if->net_dev->name, batman_if->net_dev->mtu,
345                         ETH_DATA_LEN + BAT_HEADER_LEN);
346
347         if (hardif_is_iface_up(batman_if))
348                 hardif_activate_interface(batman_if);
349         else
350                 bat_err(batman_if->soft_iface, "Not using interface %s "
351                         "(retrying later): interface not active\n",
352                         batman_if->net_dev->name);
353
354         /* begin scheduling originator messages on that interface */
355         schedule_own_packet(batman_if);
356
357 out:
358         return 0;
359
360 err:
361         return -ENOMEM;
362 }
363
364 void hardif_disable_interface(struct batman_if *batman_if)
365 {
366         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
367
368         if (batman_if->if_status == IF_ACTIVE)
369                 hardif_deactivate_interface(batman_if);
370
371         if (batman_if->if_status != IF_INACTIVE)
372                 return;
373
374         bat_info(batman_if->soft_iface, "Removing interface: %s\n",
375                  batman_if->net_dev->name);
376         dev_remove_pack(&batman_if->batman_adv_ptype);
377         hardif_put(batman_if);
378
379         bat_priv->num_ifaces--;
380         orig_hash_del_if(batman_if, bat_priv->num_ifaces);
381
382         if (batman_if == bat_priv->primary_if) {
383                 struct batman_if *new_if;
384
385                 new_if = get_active_batman_if(batman_if->soft_iface);
386                 set_primary_if(bat_priv, new_if);
387
388                 if (new_if)
389                         hardif_put(new_if);
390         }
391
392         kfree(batman_if->packet_buff);
393         batman_if->packet_buff = NULL;
394         batman_if->if_status = IF_NOT_IN_USE;
395
396         /* delete all references to this batman_if */
397         purge_orig_ref(bat_priv);
398         purge_outstanding_packets(bat_priv, batman_if);
399         dev_put(batman_if->soft_iface);
400
401         /* nobody uses this interface anymore */
402         if (!bat_priv->num_ifaces)
403                 softif_destroy(batman_if->soft_iface);
404
405         batman_if->soft_iface = NULL;
406 }
407
408 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
409 {
410         struct batman_if *batman_if;
411         int ret;
412
413         ret = is_valid_iface(net_dev);
414         if (ret != 1)
415                 goto out;
416
417         dev_hold(net_dev);
418
419         batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
420         if (!batman_if) {
421                 pr_err("Can't add interface (%s): out of memory\n",
422                        net_dev->name);
423                 goto release_dev;
424         }
425
426         ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
427         if (ret)
428                 goto free_if;
429
430         batman_if->if_num = -1;
431         batman_if->net_dev = net_dev;
432         batman_if->soft_iface = NULL;
433         batman_if->if_status = IF_NOT_IN_USE;
434         INIT_LIST_HEAD(&batman_if->list);
435         atomic_set(&batman_if->refcnt, 0);
436         hardif_hold(batman_if);
437
438         check_known_mac_addr(batman_if->net_dev);
439
440         spin_lock(&if_list_lock);
441         list_add_tail_rcu(&batman_if->list, &if_list);
442         spin_unlock(&if_list_lock);
443
444         /* extra reference for return */
445         hardif_hold(batman_if);
446         return batman_if;
447
448 free_if:
449         kfree(batman_if);
450 release_dev:
451         dev_put(net_dev);
452 out:
453         return NULL;
454 }
455
456 static void hardif_remove_interface(struct batman_if *batman_if)
457 {
458         /* first deactivate interface */
459         if (batman_if->if_status != IF_NOT_IN_USE)
460                 hardif_disable_interface(batman_if);
461
462         if (batman_if->if_status != IF_NOT_IN_USE)
463                 return;
464
465         batman_if->if_status = IF_TO_BE_REMOVED;
466         synchronize_rcu();
467         sysfs_del_hardif(&batman_if->hardif_obj);
468         hardif_put(batman_if);
469 }
470
471 void hardif_remove_interfaces(void)
472 {
473         struct batman_if *batman_if, *batman_if_tmp;
474         struct list_head if_queue;
475
476         INIT_LIST_HEAD(&if_queue);
477
478         spin_lock(&if_list_lock);
479         list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
480                 list_del_rcu(&batman_if->list);
481                 list_add_tail(&batman_if->list, &if_queue);
482         }
483         spin_unlock(&if_list_lock);
484
485         rtnl_lock();
486         list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
487                 hardif_remove_interface(batman_if);
488         }
489         rtnl_unlock();
490 }
491
492 static int hard_if_event(struct notifier_block *this,
493                          unsigned long event, void *ptr)
494 {
495         struct net_device *net_dev = (struct net_device *)ptr;
496         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
497         struct bat_priv *bat_priv;
498
499         if (!batman_if && event == NETDEV_REGISTER)
500                 batman_if = hardif_add_interface(net_dev);
501
502         if (!batman_if)
503                 goto out;
504
505         switch (event) {
506         case NETDEV_UP:
507                 hardif_activate_interface(batman_if);
508                 break;
509         case NETDEV_GOING_DOWN:
510         case NETDEV_DOWN:
511                 hardif_deactivate_interface(batman_if);
512                 break;
513         case NETDEV_UNREGISTER:
514                 spin_lock(&if_list_lock);
515                 list_del_rcu(&batman_if->list);
516                 spin_unlock(&if_list_lock);
517
518                 hardif_remove_interface(batman_if);
519                 break;
520         case NETDEV_CHANGEMTU:
521                 if (batman_if->soft_iface)
522                         update_min_mtu(batman_if->soft_iface);
523                 break;
524         case NETDEV_CHANGEADDR:
525                 if (batman_if->if_status == IF_NOT_IN_USE) {
526                         hardif_put(batman_if);
527                         goto out;
528                 }
529
530                 check_known_mac_addr(batman_if->net_dev);
531                 update_mac_addresses(batman_if);
532
533                 bat_priv = netdev_priv(batman_if->soft_iface);
534                 if (batman_if == bat_priv->primary_if)
535                         update_primary_addr(bat_priv);
536                 break;
537         default:
538                 break;
539         };
540         hardif_put(batman_if);
541
542 out:
543         return NOTIFY_DONE;
544 }
545
546 /* receive a packet with the batman ethertype coming on a hard
547  * interface */
548 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
549         struct packet_type *ptype, struct net_device *orig_dev)
550 {
551         struct bat_priv *bat_priv;
552         struct batman_packet *batman_packet;
553         struct batman_if *batman_if;
554         int ret;
555
556         batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
557         skb = skb_share_check(skb, GFP_ATOMIC);
558
559         /* skb was released by skb_share_check() */
560         if (!skb)
561                 goto err_out;
562
563         /* packet should hold at least type and version */
564         if (unlikely(!pskb_may_pull(skb, 2)))
565                 goto err_free;
566
567         /* expect a valid ethernet header here. */
568         if (unlikely(skb->mac_len != sizeof(struct ethhdr)
569                                 || !skb_mac_header(skb)))
570                 goto err_free;
571
572         if (!batman_if->soft_iface)
573                 goto err_free;
574
575         bat_priv = netdev_priv(batman_if->soft_iface);
576
577         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
578                 goto err_free;
579
580         /* discard frames on not active interfaces */
581         if (batman_if->if_status != IF_ACTIVE)
582                 goto err_free;
583
584         batman_packet = (struct batman_packet *)skb->data;
585
586         if (batman_packet->version != COMPAT_VERSION) {
587                 bat_dbg(DBG_BATMAN, bat_priv,
588                         "Drop packet: incompatible batman version (%i)\n",
589                         batman_packet->version);
590                 goto err_free;
591         }
592
593         /* all receive handlers return whether they received or reused
594          * the supplied skb. if not, we have to free the skb. */
595
596         switch (batman_packet->packet_type) {
597                 /* batman originator packet */
598         case BAT_PACKET:
599                 ret = recv_bat_packet(skb, batman_if);
600                 break;
601
602                 /* batman icmp packet */
603         case BAT_ICMP:
604                 ret = recv_icmp_packet(skb, batman_if);
605                 break;
606
607                 /* unicast packet */
608         case BAT_UNICAST:
609                 ret = recv_unicast_packet(skb, batman_if);
610                 break;
611
612                 /* fragmented unicast packet */
613         case BAT_UNICAST_FRAG:
614                 ret = recv_ucast_frag_packet(skb, batman_if);
615                 break;
616
617                 /* broadcast packet */
618         case BAT_BCAST:
619                 ret = recv_bcast_packet(skb, batman_if);
620                 break;
621
622                 /* vis packet */
623         case BAT_VIS:
624                 ret = recv_vis_packet(skb, batman_if);
625                 break;
626         default:
627                 ret = NET_RX_DROP;
628         }
629
630         if (ret == NET_RX_DROP)
631                 kfree_skb(skb);
632
633         /* return NET_RX_SUCCESS in any case as we
634          * most probably dropped the packet for
635          * routing-logical reasons. */
636
637         return NET_RX_SUCCESS;
638
639 err_free:
640         kfree_skb(skb);
641 err_out:
642         return NET_RX_DROP;
643 }
644
645 struct notifier_block hard_if_notifier = {
646         .notifier_call = hard_if_event,
647 };