Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / net / batman-adv / hard-interface.c
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner, Simon Wunderlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA
18  */
19
20 #include "main.h"
21 #include "distributed-arp-table.h"
22 #include "hard-interface.h"
23 #include "soft-interface.h"
24 #include "send.h"
25 #include "translation-table.h"
26 #include "routing.h"
27 #include "sysfs.h"
28 #include "originator.h"
29 #include "hash.h"
30 #include "bridge_loop_avoidance.h"
31
32 #include <linux/if_arp.h>
33
34 void batadv_hardif_free_rcu(struct rcu_head *rcu)
35 {
36         struct batadv_hard_iface *hard_iface;
37
38         hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
39         dev_put(hard_iface->net_dev);
40         kfree(hard_iface);
41 }
42
43 struct batadv_hard_iface *
44 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
45 {
46         struct batadv_hard_iface *hard_iface;
47
48         rcu_read_lock();
49         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
50                 if (hard_iface->net_dev == net_dev &&
51                     atomic_inc_not_zero(&hard_iface->refcount))
52                         goto out;
53         }
54
55         hard_iface = NULL;
56
57 out:
58         rcu_read_unlock();
59         return hard_iface;
60 }
61
62 /**
63  * batadv_is_on_batman_iface - check if a device is a batman iface descendant
64  * @net_dev: the device to check
65  *
66  * If the user creates any virtual device on top of a batman-adv interface, it
67  * is important to prevent this new interface to be used to create a new mesh
68  * network (this behaviour would lead to a batman-over-batman configuration).
69  * This function recursively checks all the fathers of the device passed as
70  * argument looking for a batman-adv soft interface.
71  *
72  * Returns true if the device is descendant of a batman-adv mesh interface (or
73  * if it is a batman-adv interface itself), false otherwise
74  */
75 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
76 {
77         struct net_device *parent_dev;
78         bool ret;
79
80         /* check if this is a batman-adv mesh interface */
81         if (batadv_softif_is_valid(net_dev))
82                 return true;
83
84         /* no more parents..stop recursion */
85         if (net_dev->iflink == net_dev->ifindex)
86                 return false;
87
88         /* recurse over the parent device */
89         parent_dev = dev_get_by_index(&init_net, net_dev->iflink);
90         /* if we got a NULL parent_dev there is something broken.. */
91         if (WARN(!parent_dev, "Cannot find parent device"))
92                 return false;
93
94         ret = batadv_is_on_batman_iface(parent_dev);
95
96         if (parent_dev)
97                 dev_put(parent_dev);
98         return ret;
99 }
100
101 static int batadv_is_valid_iface(const struct net_device *net_dev)
102 {
103         if (net_dev->flags & IFF_LOOPBACK)
104                 return 0;
105
106         if (net_dev->type != ARPHRD_ETHER)
107                 return 0;
108
109         if (net_dev->addr_len != ETH_ALEN)
110                 return 0;
111
112         /* no batman over batman */
113         if (batadv_is_on_batman_iface(net_dev))
114                 return 0;
115
116         return 1;
117 }
118
119 static struct batadv_hard_iface *
120 batadv_hardif_get_active(const struct net_device *soft_iface)
121 {
122         struct batadv_hard_iface *hard_iface;
123
124         rcu_read_lock();
125         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
126                 if (hard_iface->soft_iface != soft_iface)
127                         continue;
128
129                 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
130                     atomic_inc_not_zero(&hard_iface->refcount))
131                         goto out;
132         }
133
134         hard_iface = NULL;
135
136 out:
137         rcu_read_unlock();
138         return hard_iface;
139 }
140
141 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
142                                           struct batadv_hard_iface *oldif)
143 {
144         struct batadv_vis_packet *vis_packet;
145         struct batadv_hard_iface *primary_if;
146         struct sk_buff *skb;
147
148         primary_if = batadv_primary_if_get_selected(bat_priv);
149         if (!primary_if)
150                 goto out;
151
152         batadv_dat_init_own_addr(bat_priv, primary_if);
153
154         skb = bat_priv->vis.my_info->skb_packet;
155         vis_packet = (struct batadv_vis_packet *)skb->data;
156         memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
157         memcpy(vis_packet->sender_orig,
158                primary_if->net_dev->dev_addr, ETH_ALEN);
159
160         batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
161 out:
162         if (primary_if)
163                 batadv_hardif_free_ref(primary_if);
164 }
165
166 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
167                                      struct batadv_hard_iface *new_hard_iface)
168 {
169         struct batadv_hard_iface *curr_hard_iface;
170
171         ASSERT_RTNL();
172
173         if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
174                 new_hard_iface = NULL;
175
176         curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
177         rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
178
179         if (!new_hard_iface)
180                 goto out;
181
182         bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
183         batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
184
185 out:
186         if (curr_hard_iface)
187                 batadv_hardif_free_ref(curr_hard_iface);
188 }
189
190 static bool
191 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
192 {
193         if (hard_iface->net_dev->flags & IFF_UP)
194                 return true;
195
196         return false;
197 }
198
199 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
200 {
201         const struct batadv_hard_iface *hard_iface;
202
203         rcu_read_lock();
204         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
205                 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
206                     (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
207                         continue;
208
209                 if (hard_iface->net_dev == net_dev)
210                         continue;
211
212                 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
213                                         net_dev->dev_addr))
214                         continue;
215
216                 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
217                         net_dev->dev_addr, hard_iface->net_dev->name);
218                 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
219         }
220         rcu_read_unlock();
221 }
222
223 int batadv_hardif_min_mtu(struct net_device *soft_iface)
224 {
225         const struct batadv_priv *bat_priv = netdev_priv(soft_iface);
226         const struct batadv_hard_iface *hard_iface;
227         /* allow big frames if all devices are capable to do so
228          * (have MTU > 1500 + BAT_HEADER_LEN)
229          */
230         int min_mtu = ETH_DATA_LEN;
231
232         if (atomic_read(&bat_priv->fragmentation))
233                 goto out;
234
235         rcu_read_lock();
236         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
237                 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
238                     (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
239                         continue;
240
241                 if (hard_iface->soft_iface != soft_iface)
242                         continue;
243
244                 min_mtu = min_t(int,
245                                 hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
246                                 min_mtu);
247         }
248         rcu_read_unlock();
249 out:
250         return min_mtu;
251 }
252
253 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
254 void batadv_update_min_mtu(struct net_device *soft_iface)
255 {
256         int min_mtu;
257
258         min_mtu = batadv_hardif_min_mtu(soft_iface);
259         if (soft_iface->mtu != min_mtu)
260                 soft_iface->mtu = min_mtu;
261 }
262
263 static void
264 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
265 {
266         struct batadv_priv *bat_priv;
267         struct batadv_hard_iface *primary_if = NULL;
268
269         if (hard_iface->if_status != BATADV_IF_INACTIVE)
270                 goto out;
271
272         bat_priv = netdev_priv(hard_iface->soft_iface);
273
274         bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
275         hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
276
277         /* the first active interface becomes our primary interface or
278          * the next active interface after the old primary interface was removed
279          */
280         primary_if = batadv_primary_if_get_selected(bat_priv);
281         if (!primary_if)
282                 batadv_primary_if_select(bat_priv, hard_iface);
283
284         batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
285                     hard_iface->net_dev->name);
286
287         batadv_update_min_mtu(hard_iface->soft_iface);
288
289 out:
290         if (primary_if)
291                 batadv_hardif_free_ref(primary_if);
292 }
293
294 static void
295 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
296 {
297         if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
298             (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
299                 return;
300
301         hard_iface->if_status = BATADV_IF_INACTIVE;
302
303         batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
304                     hard_iface->net_dev->name);
305
306         batadv_update_min_mtu(hard_iface->soft_iface);
307 }
308
309 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
310                                    const char *iface_name)
311 {
312         struct batadv_priv *bat_priv;
313         struct net_device *soft_iface;
314         __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
315         int ret;
316
317         if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
318                 goto out;
319
320         if (!atomic_inc_not_zero(&hard_iface->refcount))
321                 goto out;
322
323         /* hard-interface is part of a bridge */
324         if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
325                 pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n",
326                        hard_iface->net_dev->name);
327
328         soft_iface = dev_get_by_name(&init_net, iface_name);
329
330         if (!soft_iface) {
331                 soft_iface = batadv_softif_create(iface_name);
332
333                 if (!soft_iface) {
334                         ret = -ENOMEM;
335                         goto err;
336                 }
337
338                 /* dev_get_by_name() increases the reference counter for us */
339                 dev_hold(soft_iface);
340         }
341
342         if (!batadv_softif_is_valid(soft_iface)) {
343                 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
344                        soft_iface->name);
345                 ret = -EINVAL;
346                 goto err_dev;
347         }
348
349         hard_iface->soft_iface = soft_iface;
350         bat_priv = netdev_priv(hard_iface->soft_iface);
351
352         ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
353         if (ret < 0)
354                 goto err_dev;
355
356         hard_iface->if_num = bat_priv->num_ifaces;
357         bat_priv->num_ifaces++;
358         hard_iface->if_status = BATADV_IF_INACTIVE;
359         ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
360         if (ret < 0) {
361                 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
362                 bat_priv->num_ifaces--;
363                 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
364                 goto err_dev;
365         }
366
367         hard_iface->batman_adv_ptype.type = ethertype;
368         hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
369         hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
370         dev_add_pack(&hard_iface->batman_adv_ptype);
371
372         atomic_set(&hard_iface->frag_seqno, 1);
373         batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
374                     hard_iface->net_dev->name);
375
376         if (atomic_read(&bat_priv->fragmentation) &&
377             hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
378                 batadv_info(hard_iface->soft_iface,
379                             "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %zi would solve the problem.\n",
380                             hard_iface->net_dev->name, hard_iface->net_dev->mtu,
381                             ETH_DATA_LEN + BATADV_HEADER_LEN);
382
383         if (!atomic_read(&bat_priv->fragmentation) &&
384             hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
385                 batadv_info(hard_iface->soft_iface,
386                             "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %zi.\n",
387                             hard_iface->net_dev->name, hard_iface->net_dev->mtu,
388                             ETH_DATA_LEN + BATADV_HEADER_LEN);
389
390         if (batadv_hardif_is_iface_up(hard_iface))
391                 batadv_hardif_activate_interface(hard_iface);
392         else
393                 batadv_err(hard_iface->soft_iface,
394                            "Not using interface %s (retrying later): interface not active\n",
395                            hard_iface->net_dev->name);
396
397         /* begin scheduling originator messages on that interface */
398         batadv_schedule_bat_ogm(hard_iface);
399
400 out:
401         return 0;
402
403 err_dev:
404         dev_put(soft_iface);
405 err:
406         batadv_hardif_free_ref(hard_iface);
407         return ret;
408 }
409
410 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
411 {
412         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
413         struct batadv_hard_iface *primary_if = NULL;
414
415         if (hard_iface->if_status == BATADV_IF_ACTIVE)
416                 batadv_hardif_deactivate_interface(hard_iface);
417
418         if (hard_iface->if_status != BATADV_IF_INACTIVE)
419                 goto out;
420
421         batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
422                     hard_iface->net_dev->name);
423         dev_remove_pack(&hard_iface->batman_adv_ptype);
424
425         bat_priv->num_ifaces--;
426         batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
427
428         primary_if = batadv_primary_if_get_selected(bat_priv);
429         if (hard_iface == primary_if) {
430                 struct batadv_hard_iface *new_if;
431
432                 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
433                 batadv_primary_if_select(bat_priv, new_if);
434
435                 if (new_if)
436                         batadv_hardif_free_ref(new_if);
437         }
438
439         bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
440         hard_iface->if_status = BATADV_IF_NOT_IN_USE;
441
442         /* delete all references to this hard_iface */
443         batadv_purge_orig_ref(bat_priv);
444         batadv_purge_outstanding_packets(bat_priv, hard_iface);
445         dev_put(hard_iface->soft_iface);
446
447         /* nobody uses this interface anymore */
448         if (!bat_priv->num_ifaces)
449                 batadv_softif_destroy(hard_iface->soft_iface);
450
451         hard_iface->soft_iface = NULL;
452         batadv_hardif_free_ref(hard_iface);
453
454 out:
455         if (primary_if)
456                 batadv_hardif_free_ref(primary_if);
457 }
458
459 static struct batadv_hard_iface *
460 batadv_hardif_add_interface(struct net_device *net_dev)
461 {
462         struct batadv_hard_iface *hard_iface;
463         int ret;
464
465         ASSERT_RTNL();
466
467         ret = batadv_is_valid_iface(net_dev);
468         if (ret != 1)
469                 goto out;
470
471         dev_hold(net_dev);
472
473         hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
474         if (!hard_iface)
475                 goto release_dev;
476
477         ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
478         if (ret)
479                 goto free_if;
480
481         hard_iface->if_num = -1;
482         hard_iface->net_dev = net_dev;
483         hard_iface->soft_iface = NULL;
484         hard_iface->if_status = BATADV_IF_NOT_IN_USE;
485         INIT_LIST_HEAD(&hard_iface->list);
486         /* extra reference for return */
487         atomic_set(&hard_iface->refcount, 2);
488
489         batadv_check_known_mac_addr(hard_iface->net_dev);
490         list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
491
492         /* This can't be called via a bat_priv callback because
493          * we have no bat_priv yet.
494          */
495         atomic_set(&hard_iface->bat_iv.ogm_seqno, 1);
496         hard_iface->bat_iv.ogm_buff = NULL;
497
498         return hard_iface;
499
500 free_if:
501         kfree(hard_iface);
502 release_dev:
503         dev_put(net_dev);
504 out:
505         return NULL;
506 }
507
508 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
509 {
510         ASSERT_RTNL();
511
512         /* first deactivate interface */
513         if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
514                 batadv_hardif_disable_interface(hard_iface);
515
516         if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
517                 return;
518
519         hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
520         batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
521         batadv_hardif_free_ref(hard_iface);
522 }
523
524 void batadv_hardif_remove_interfaces(void)
525 {
526         struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
527
528         rtnl_lock();
529         list_for_each_entry_safe(hard_iface, hard_iface_tmp,
530                                  &batadv_hardif_list, list) {
531                 list_del_rcu(&hard_iface->list);
532                 batadv_hardif_remove_interface(hard_iface);
533         }
534         rtnl_unlock();
535 }
536
537 static int batadv_hard_if_event(struct notifier_block *this,
538                                 unsigned long event, void *ptr)
539 {
540         struct net_device *net_dev = ptr;
541         struct batadv_hard_iface *hard_iface;
542         struct batadv_hard_iface *primary_if = NULL;
543         struct batadv_priv *bat_priv;
544
545         hard_iface = batadv_hardif_get_by_netdev(net_dev);
546         if (!hard_iface && event == NETDEV_REGISTER)
547                 hard_iface = batadv_hardif_add_interface(net_dev);
548
549         if (!hard_iface)
550                 goto out;
551
552         switch (event) {
553         case NETDEV_UP:
554                 batadv_hardif_activate_interface(hard_iface);
555                 break;
556         case NETDEV_GOING_DOWN:
557         case NETDEV_DOWN:
558                 batadv_hardif_deactivate_interface(hard_iface);
559                 break;
560         case NETDEV_UNREGISTER:
561                 list_del_rcu(&hard_iface->list);
562
563                 batadv_hardif_remove_interface(hard_iface);
564                 break;
565         case NETDEV_CHANGEMTU:
566                 if (hard_iface->soft_iface)
567                         batadv_update_min_mtu(hard_iface->soft_iface);
568                 break;
569         case NETDEV_CHANGEADDR:
570                 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
571                         goto hardif_put;
572
573                 batadv_check_known_mac_addr(hard_iface->net_dev);
574
575                 bat_priv = netdev_priv(hard_iface->soft_iface);
576                 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
577
578                 primary_if = batadv_primary_if_get_selected(bat_priv);
579                 if (!primary_if)
580                         goto hardif_put;
581
582                 if (hard_iface == primary_if)
583                         batadv_primary_if_update_addr(bat_priv, NULL);
584                 break;
585         default:
586                 break;
587         }
588
589 hardif_put:
590         batadv_hardif_free_ref(hard_iface);
591 out:
592         if (primary_if)
593                 batadv_hardif_free_ref(primary_if);
594         return NOTIFY_DONE;
595 }
596
597 /* This function returns true if the interface represented by ifindex is a
598  * 802.11 wireless device
599  */
600 bool batadv_is_wifi_iface(int ifindex)
601 {
602         struct net_device *net_device = NULL;
603         bool ret = false;
604
605         if (ifindex == BATADV_NULL_IFINDEX)
606                 goto out;
607
608         net_device = dev_get_by_index(&init_net, ifindex);
609         if (!net_device)
610                 goto out;
611
612 #ifdef CONFIG_WIRELESS_EXT
613         /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
614          * check for wireless_handlers != NULL
615          */
616         if (net_device->wireless_handlers)
617                 ret = true;
618         else
619 #endif
620                 /* cfg80211 drivers have to set ieee80211_ptr */
621                 if (net_device->ieee80211_ptr)
622                         ret = true;
623 out:
624         if (net_device)
625                 dev_put(net_device);
626         return ret;
627 }
628
629 struct notifier_block batadv_hard_if_notifier = {
630         .notifier_call = batadv_hard_if_event,
631 };