1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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
21 #include "hard-interface.h"
22 #include "soft-interface.h"
24 #include "translation-table.h"
27 #include "originator.h"
29 #include "bridge_loop_avoidance.h"
31 #include <linux/if_arp.h>
33 void batadv_hardif_free_rcu(struct rcu_head *rcu)
35 struct batadv_hard_iface *hard_iface;
37 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
38 dev_put(hard_iface->net_dev);
42 struct batadv_hard_iface *
43 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
45 struct batadv_hard_iface *hard_iface;
48 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
49 if (hard_iface->net_dev == net_dev &&
50 atomic_inc_not_zero(&hard_iface->refcount))
61 static int batadv_is_valid_iface(const struct net_device *net_dev)
63 if (net_dev->flags & IFF_LOOPBACK)
66 if (net_dev->type != ARPHRD_ETHER)
69 if (net_dev->addr_len != ETH_ALEN)
72 /* no batman over batman */
73 if (batadv_softif_is_valid(net_dev))
79 static struct batadv_hard_iface *
80 batadv_hardif_get_active(const struct net_device *soft_iface)
82 struct batadv_hard_iface *hard_iface;
85 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
86 if (hard_iface->soft_iface != soft_iface)
89 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
90 atomic_inc_not_zero(&hard_iface->refcount))
101 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
102 struct batadv_hard_iface *oldif)
104 struct batadv_vis_packet *vis_packet;
105 struct batadv_hard_iface *primary_if;
107 primary_if = batadv_primary_if_get_selected(bat_priv);
111 vis_packet = (struct batadv_vis_packet *)
112 bat_priv->my_vis_info->skb_packet->data;
113 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
114 memcpy(vis_packet->sender_orig,
115 primary_if->net_dev->dev_addr, ETH_ALEN);
117 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
120 batadv_hardif_free_ref(primary_if);
123 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
124 struct batadv_hard_iface *new_hard_iface)
126 struct batadv_hard_iface *curr_hard_iface;
130 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
131 new_hard_iface = NULL;
133 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
134 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
139 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
140 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
144 batadv_hardif_free_ref(curr_hard_iface);
148 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
150 if (hard_iface->net_dev->flags & IFF_UP)
156 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
158 const struct batadv_hard_iface *hard_iface;
161 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
162 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
163 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
166 if (hard_iface->net_dev == net_dev)
169 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
173 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
174 net_dev->dev_addr, hard_iface->net_dev->name);
175 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
180 int batadv_hardif_min_mtu(struct net_device *soft_iface)
182 const struct batadv_priv *bat_priv = netdev_priv(soft_iface);
183 const struct batadv_hard_iface *hard_iface;
184 /* allow big frames if all devices are capable to do so
185 * (have MTU > 1500 + BAT_HEADER_LEN)
187 int min_mtu = ETH_DATA_LEN;
189 if (atomic_read(&bat_priv->fragmentation))
193 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
194 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
195 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
198 if (hard_iface->soft_iface != soft_iface)
202 hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
210 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
211 void batadv_update_min_mtu(struct net_device *soft_iface)
215 min_mtu = batadv_hardif_min_mtu(soft_iface);
216 if (soft_iface->mtu != min_mtu)
217 soft_iface->mtu = min_mtu;
221 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
223 struct batadv_priv *bat_priv;
224 struct batadv_hard_iface *primary_if = NULL;
226 if (hard_iface->if_status != BATADV_IF_INACTIVE)
229 bat_priv = netdev_priv(hard_iface->soft_iface);
231 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
232 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
234 /* the first active interface becomes our primary interface or
235 * the next active interface after the old primary interface was removed
237 primary_if = batadv_primary_if_get_selected(bat_priv);
239 batadv_primary_if_select(bat_priv, hard_iface);
241 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
242 hard_iface->net_dev->name);
244 batadv_update_min_mtu(hard_iface->soft_iface);
248 batadv_hardif_free_ref(primary_if);
252 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
254 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
255 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
258 hard_iface->if_status = BATADV_IF_INACTIVE;
260 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
261 hard_iface->net_dev->name);
263 batadv_update_min_mtu(hard_iface->soft_iface);
266 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
267 const char *iface_name)
269 struct batadv_priv *bat_priv;
270 struct net_device *soft_iface;
271 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
274 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
277 if (!atomic_inc_not_zero(&hard_iface->refcount))
280 /* hard-interface is part of a bridge */
281 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
282 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",
283 hard_iface->net_dev->name);
285 soft_iface = dev_get_by_name(&init_net, iface_name);
288 soft_iface = batadv_softif_create(iface_name);
295 /* dev_get_by_name() increases the reference counter for us */
296 dev_hold(soft_iface);
299 if (!batadv_softif_is_valid(soft_iface)) {
300 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
306 hard_iface->soft_iface = soft_iface;
307 bat_priv = netdev_priv(hard_iface->soft_iface);
309 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
313 hard_iface->if_num = bat_priv->num_ifaces;
314 bat_priv->num_ifaces++;
315 hard_iface->if_status = BATADV_IF_INACTIVE;
316 batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
318 hard_iface->batman_adv_ptype.type = ethertype;
319 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
320 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
321 dev_add_pack(&hard_iface->batman_adv_ptype);
323 atomic_set(&hard_iface->frag_seqno, 1);
324 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
325 hard_iface->net_dev->name);
327 if (atomic_read(&bat_priv->fragmentation) &&
328 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
329 batadv_info(hard_iface->soft_iface,
330 "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",
331 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
332 ETH_DATA_LEN + BATADV_HEADER_LEN);
334 if (!atomic_read(&bat_priv->fragmentation) &&
335 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
336 batadv_info(hard_iface->soft_iface,
337 "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",
338 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
339 ETH_DATA_LEN + BATADV_HEADER_LEN);
341 if (batadv_hardif_is_iface_up(hard_iface))
342 batadv_hardif_activate_interface(hard_iface);
344 batadv_err(hard_iface->soft_iface,
345 "Not using interface %s (retrying later): interface not active\n",
346 hard_iface->net_dev->name);
348 /* begin scheduling originator messages on that interface */
349 batadv_schedule_bat_ogm(hard_iface);
357 batadv_hardif_free_ref(hard_iface);
361 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
363 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
364 struct batadv_hard_iface *primary_if = NULL;
366 if (hard_iface->if_status == BATADV_IF_ACTIVE)
367 batadv_hardif_deactivate_interface(hard_iface);
369 if (hard_iface->if_status != BATADV_IF_INACTIVE)
372 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
373 hard_iface->net_dev->name);
374 dev_remove_pack(&hard_iface->batman_adv_ptype);
376 bat_priv->num_ifaces--;
377 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
379 primary_if = batadv_primary_if_get_selected(bat_priv);
380 if (hard_iface == primary_if) {
381 struct batadv_hard_iface *new_if;
383 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
384 batadv_primary_if_select(bat_priv, new_if);
387 batadv_hardif_free_ref(new_if);
390 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
391 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
393 /* delete all references to this hard_iface */
394 batadv_purge_orig_ref(bat_priv);
395 batadv_purge_outstanding_packets(bat_priv, hard_iface);
396 dev_put(hard_iface->soft_iface);
398 /* nobody uses this interface anymore */
399 if (!bat_priv->num_ifaces)
400 batadv_softif_destroy(hard_iface->soft_iface);
402 hard_iface->soft_iface = NULL;
403 batadv_hardif_free_ref(hard_iface);
407 batadv_hardif_free_ref(primary_if);
410 static struct batadv_hard_iface *
411 batadv_hardif_add_interface(struct net_device *net_dev)
413 struct batadv_hard_iface *hard_iface;
418 ret = batadv_is_valid_iface(net_dev);
424 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
428 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
432 hard_iface->if_num = -1;
433 hard_iface->net_dev = net_dev;
434 hard_iface->soft_iface = NULL;
435 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
436 INIT_LIST_HEAD(&hard_iface->list);
437 /* extra reference for return */
438 atomic_set(&hard_iface->refcount, 2);
440 batadv_check_known_mac_addr(hard_iface->net_dev);
441 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
443 /* This can't be called via a bat_priv callback because
444 * we have no bat_priv yet.
446 atomic_set(&hard_iface->seqno, 1);
447 hard_iface->packet_buff = NULL;
459 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
463 /* first deactivate interface */
464 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
465 batadv_hardif_disable_interface(hard_iface);
467 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
470 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
471 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
472 batadv_hardif_free_ref(hard_iface);
475 void batadv_hardif_remove_interfaces(void)
477 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
480 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
481 &batadv_hardif_list, list) {
482 list_del_rcu(&hard_iface->list);
483 batadv_hardif_remove_interface(hard_iface);
488 static int batadv_hard_if_event(struct notifier_block *this,
489 unsigned long event, void *ptr)
491 struct net_device *net_dev = ptr;
492 struct batadv_hard_iface *hard_iface;
493 struct batadv_hard_iface *primary_if = NULL;
494 struct batadv_priv *bat_priv;
496 hard_iface = batadv_hardif_get_by_netdev(net_dev);
497 if (!hard_iface && event == NETDEV_REGISTER)
498 hard_iface = batadv_hardif_add_interface(net_dev);
505 batadv_hardif_activate_interface(hard_iface);
507 case NETDEV_GOING_DOWN:
509 batadv_hardif_deactivate_interface(hard_iface);
511 case NETDEV_UNREGISTER:
512 list_del_rcu(&hard_iface->list);
514 batadv_hardif_remove_interface(hard_iface);
516 case NETDEV_CHANGEMTU:
517 if (hard_iface->soft_iface)
518 batadv_update_min_mtu(hard_iface->soft_iface);
520 case NETDEV_CHANGEADDR:
521 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
524 batadv_check_known_mac_addr(hard_iface->net_dev);
526 bat_priv = netdev_priv(hard_iface->soft_iface);
527 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
529 primary_if = batadv_primary_if_get_selected(bat_priv);
533 if (hard_iface == primary_if)
534 batadv_primary_if_update_addr(bat_priv, NULL);
541 batadv_hardif_free_ref(hard_iface);
544 batadv_hardif_free_ref(primary_if);
548 /* This function returns true if the interface represented by ifindex is a
549 * 802.11 wireless device
551 bool batadv_is_wifi_iface(int ifindex)
553 struct net_device *net_device = NULL;
556 if (ifindex == BATADV_NULL_IFINDEX)
559 net_device = dev_get_by_index(&init_net, ifindex);
563 #ifdef CONFIG_WIRELESS_EXT
564 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
565 * check for wireless_handlers != NULL
567 if (net_device->wireless_handlers)
571 /* cfg80211 drivers have to set ieee80211_ptr */
572 if (net_device->ieee80211_ptr)
580 struct notifier_block batadv_hard_if_notifier = {
581 .notifier_call = batadv_hard_if_event,