1 /* Copyright (C) 2007-2013 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 "distributed-arp-table.h"
22 #include "hard-interface.h"
23 #include "soft-interface.h"
25 #include "translation-table.h"
28 #include "originator.h"
30 #include "bridge_loop_avoidance.h"
31 #include "gateway_client.h"
33 #include <linux/if_arp.h>
34 #include <linux/if_ether.h>
36 void batadv_hardif_free_rcu(struct rcu_head *rcu)
38 struct batadv_hard_iface *hard_iface;
40 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
41 dev_put(hard_iface->net_dev);
45 struct batadv_hard_iface *
46 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
48 struct batadv_hard_iface *hard_iface;
51 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
52 if (hard_iface->net_dev == net_dev &&
53 atomic_inc_not_zero(&hard_iface->refcount))
65 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
66 * @net_dev: the device to check
68 * If the user creates any virtual device on top of a batman-adv interface, it
69 * is important to prevent this new interface to be used to create a new mesh
70 * network (this behaviour would lead to a batman-over-batman configuration).
71 * This function recursively checks all the fathers of the device passed as
72 * argument looking for a batman-adv soft interface.
74 * Returns true if the device is descendant of a batman-adv mesh interface (or
75 * if it is a batman-adv interface itself), false otherwise
77 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
79 struct net_device *parent_dev;
82 /* check if this is a batman-adv mesh interface */
83 if (batadv_softif_is_valid(net_dev))
86 /* no more parents..stop recursion */
87 if (net_dev->iflink == net_dev->ifindex)
90 /* recurse over the parent device */
91 parent_dev = dev_get_by_index(&init_net, net_dev->iflink);
92 /* if we got a NULL parent_dev there is something broken.. */
93 if (WARN(!parent_dev, "Cannot find parent device"))
96 ret = batadv_is_on_batman_iface(parent_dev);
103 static int batadv_is_valid_iface(const struct net_device *net_dev)
105 if (net_dev->flags & IFF_LOOPBACK)
108 if (net_dev->type != ARPHRD_ETHER)
111 if (net_dev->addr_len != ETH_ALEN)
114 /* no batman over batman */
115 if (batadv_is_on_batman_iface(net_dev))
122 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
124 * @net_device: the device to check
126 * Returns true if the net device is a 802.11 wireless device, false otherwise.
128 static bool batadv_is_wifi_netdev(struct net_device *net_device)
130 #ifdef CONFIG_WIRELESS_EXT
131 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
132 * check for wireless_handlers != NULL
134 if (net_device->wireless_handlers)
138 /* cfg80211 drivers have to set ieee80211_ptr */
139 if (net_device->ieee80211_ptr)
146 * batadv_is_wifi_iface - check if the given interface represented by ifindex
147 * is a wifi interface
148 * @ifindex: interface index to check
150 * Returns true if the interface represented by ifindex is a 802.11 wireless
151 * device, false otherwise.
153 bool batadv_is_wifi_iface(int ifindex)
155 struct net_device *net_device = NULL;
158 if (ifindex == BATADV_NULL_IFINDEX)
161 net_device = dev_get_by_index(&init_net, ifindex);
165 ret = batadv_is_wifi_netdev(net_device);
173 static struct batadv_hard_iface *
174 batadv_hardif_get_active(const struct net_device *soft_iface)
176 struct batadv_hard_iface *hard_iface;
179 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
180 if (hard_iface->soft_iface != soft_iface)
183 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
184 atomic_inc_not_zero(&hard_iface->refcount))
195 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
196 struct batadv_hard_iface *oldif)
198 struct batadv_hard_iface *primary_if;
200 primary_if = batadv_primary_if_get_selected(bat_priv);
204 batadv_dat_init_own_addr(bat_priv, primary_if);
205 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
208 batadv_hardif_free_ref(primary_if);
211 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
212 struct batadv_hard_iface *new_hard_iface)
214 struct batadv_hard_iface *curr_hard_iface;
218 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
219 new_hard_iface = NULL;
221 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
222 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
227 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
228 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
232 batadv_hardif_free_ref(curr_hard_iface);
236 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
238 if (hard_iface->net_dev->flags & IFF_UP)
244 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
246 const struct batadv_hard_iface *hard_iface;
249 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
250 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
251 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
254 if (hard_iface->net_dev == net_dev)
257 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
261 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
262 net_dev->dev_addr, hard_iface->net_dev->name);
263 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
268 int batadv_hardif_min_mtu(struct net_device *soft_iface)
270 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
271 const struct batadv_hard_iface *hard_iface;
272 int min_mtu = ETH_DATA_LEN;
275 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
276 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
277 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
280 if (hard_iface->soft_iface != soft_iface)
283 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
287 atomic_set(&bat_priv->packet_size_max, min_mtu);
289 if (atomic_read(&bat_priv->fragmentation) == 0)
292 /* with fragmentation enabled the maximum size of internally generated
293 * packets such as translation table exchanges or tvlv containers, etc
294 * has to be calculated
296 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
297 min_mtu -= sizeof(struct batadv_frag_packet);
298 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
299 atomic_set(&bat_priv->packet_size_max, min_mtu);
301 /* with fragmentation enabled we can fragment external packets easily */
302 min_mtu = min_t(int, min_mtu, ETH_DATA_LEN);
305 return min_mtu - batadv_max_header_len();
308 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
309 void batadv_update_min_mtu(struct net_device *soft_iface)
311 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
313 /* Check if the local translate table should be cleaned up to match a
314 * new (and smaller) MTU.
316 batadv_tt_local_resize_to_mtu(soft_iface);
320 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
322 struct batadv_priv *bat_priv;
323 struct batadv_hard_iface *primary_if = NULL;
325 if (hard_iface->if_status != BATADV_IF_INACTIVE)
328 bat_priv = netdev_priv(hard_iface->soft_iface);
330 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
331 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
333 /* the first active interface becomes our primary interface or
334 * the next active interface after the old primary interface was removed
336 primary_if = batadv_primary_if_get_selected(bat_priv);
338 batadv_primary_if_select(bat_priv, hard_iface);
340 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
341 hard_iface->net_dev->name);
343 batadv_update_min_mtu(hard_iface->soft_iface);
347 batadv_hardif_free_ref(primary_if);
351 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
353 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
354 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
357 hard_iface->if_status = BATADV_IF_INACTIVE;
359 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
360 hard_iface->net_dev->name);
362 batadv_update_min_mtu(hard_iface->soft_iface);
366 * batadv_master_del_slave - remove hard_iface from the current master interface
367 * @slave: the interface enslaved in another master
368 * @master: the master from which slave has to be removed
370 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
371 * is free'd and master can correctly change its internal state.
372 * Return 0 on success, a negative value representing the error otherwise
374 static int batadv_master_del_slave(struct batadv_hard_iface *slave,
375 struct net_device *master)
383 if (master->netdev_ops->ndo_del_slave)
384 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
389 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
390 const char *iface_name)
392 struct batadv_priv *bat_priv;
393 struct net_device *soft_iface, *master;
394 __be16 ethertype = htons(ETH_P_BATMAN);
395 int max_header_len = batadv_max_header_len();
398 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
401 if (!atomic_inc_not_zero(&hard_iface->refcount))
404 soft_iface = dev_get_by_name(&init_net, iface_name);
407 soft_iface = batadv_softif_create(iface_name);
414 /* dev_get_by_name() increases the reference counter for us */
415 dev_hold(soft_iface);
418 if (!batadv_softif_is_valid(soft_iface)) {
419 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
425 /* check if the interface is enslaved in another virtual one and
426 * in that case unlink it first
428 master = netdev_master_upper_dev_get(hard_iface->net_dev);
429 ret = batadv_master_del_slave(hard_iface, master);
433 hard_iface->soft_iface = soft_iface;
434 bat_priv = netdev_priv(hard_iface->soft_iface);
436 ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
440 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
444 hard_iface->if_num = bat_priv->num_ifaces;
445 bat_priv->num_ifaces++;
446 hard_iface->if_status = BATADV_IF_INACTIVE;
447 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
449 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
450 bat_priv->num_ifaces--;
451 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
455 hard_iface->batman_adv_ptype.type = ethertype;
456 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
457 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
458 dev_add_pack(&hard_iface->batman_adv_ptype);
460 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
461 hard_iface->net_dev->name);
463 if (atomic_read(&bat_priv->fragmentation) &&
464 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
465 batadv_info(hard_iface->soft_iface,
466 "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 %i would solve the problem.\n",
467 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
468 ETH_DATA_LEN + max_header_len);
470 if (!atomic_read(&bat_priv->fragmentation) &&
471 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
472 batadv_info(hard_iface->soft_iface,
473 "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 %i.\n",
474 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
475 ETH_DATA_LEN + max_header_len);
477 if (batadv_hardif_is_iface_up(hard_iface))
478 batadv_hardif_activate_interface(hard_iface);
480 batadv_err(hard_iface->soft_iface,
481 "Not using interface %s (retrying later): interface not active\n",
482 hard_iface->net_dev->name);
484 /* begin scheduling originator messages on that interface */
485 batadv_schedule_bat_ogm(hard_iface);
491 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
493 hard_iface->soft_iface = NULL;
496 batadv_hardif_free_ref(hard_iface);
500 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
501 enum batadv_hard_if_cleanup autodel)
503 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
504 struct batadv_hard_iface *primary_if = NULL;
506 if (hard_iface->if_status == BATADV_IF_ACTIVE)
507 batadv_hardif_deactivate_interface(hard_iface);
509 if (hard_iface->if_status != BATADV_IF_INACTIVE)
512 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
513 hard_iface->net_dev->name);
514 dev_remove_pack(&hard_iface->batman_adv_ptype);
516 bat_priv->num_ifaces--;
517 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
519 primary_if = batadv_primary_if_get_selected(bat_priv);
520 if (hard_iface == primary_if) {
521 struct batadv_hard_iface *new_if;
523 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
524 batadv_primary_if_select(bat_priv, new_if);
527 batadv_hardif_free_ref(new_if);
530 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
531 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
533 /* delete all references to this hard_iface */
534 batadv_purge_orig_ref(bat_priv);
535 batadv_purge_outstanding_packets(bat_priv, hard_iface);
536 dev_put(hard_iface->soft_iface);
538 /* nobody uses this interface anymore */
539 if (!bat_priv->num_ifaces) {
540 batadv_gw_check_client_stop(bat_priv);
542 if (autodel == BATADV_IF_CLEANUP_AUTO)
543 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
546 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
547 hard_iface->soft_iface = NULL;
548 batadv_hardif_free_ref(hard_iface);
552 batadv_hardif_free_ref(primary_if);
556 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
557 * @work: work queue item
559 * Free the parts of the hard interface which can not be removed under
560 * rtnl lock (to prevent deadlock situations).
562 static void batadv_hardif_remove_interface_finish(struct work_struct *work)
564 struct batadv_hard_iface *hard_iface;
566 hard_iface = container_of(work, struct batadv_hard_iface,
569 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
570 batadv_hardif_free_ref(hard_iface);
573 static struct batadv_hard_iface *
574 batadv_hardif_add_interface(struct net_device *net_dev)
576 struct batadv_hard_iface *hard_iface;
581 ret = batadv_is_valid_iface(net_dev);
587 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
591 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
595 hard_iface->if_num = -1;
596 hard_iface->net_dev = net_dev;
597 hard_iface->soft_iface = NULL;
598 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
599 INIT_LIST_HEAD(&hard_iface->list);
600 INIT_WORK(&hard_iface->cleanup_work,
601 batadv_hardif_remove_interface_finish);
603 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
604 if (batadv_is_wifi_netdev(net_dev))
605 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
607 /* extra reference for return */
608 atomic_set(&hard_iface->refcount, 2);
610 batadv_check_known_mac_addr(hard_iface->net_dev);
611 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
623 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
627 /* first deactivate interface */
628 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
629 batadv_hardif_disable_interface(hard_iface,
630 BATADV_IF_CLEANUP_AUTO);
632 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
635 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
636 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
639 void batadv_hardif_remove_interfaces(void)
641 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
644 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
645 &batadv_hardif_list, list) {
646 list_del_rcu(&hard_iface->list);
647 batadv_hardif_remove_interface(hard_iface);
652 static int batadv_hard_if_event(struct notifier_block *this,
653 unsigned long event, void *ptr)
655 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
656 struct batadv_hard_iface *hard_iface;
657 struct batadv_hard_iface *primary_if = NULL;
658 struct batadv_priv *bat_priv;
660 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
661 batadv_sysfs_add_meshif(net_dev);
662 bat_priv = netdev_priv(net_dev);
663 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
667 hard_iface = batadv_hardif_get_by_netdev(net_dev);
668 if (!hard_iface && event == NETDEV_REGISTER)
669 hard_iface = batadv_hardif_add_interface(net_dev);
676 batadv_hardif_activate_interface(hard_iface);
678 case NETDEV_GOING_DOWN:
680 batadv_hardif_deactivate_interface(hard_iface);
682 case NETDEV_UNREGISTER:
683 list_del_rcu(&hard_iface->list);
685 batadv_hardif_remove_interface(hard_iface);
687 case NETDEV_CHANGEMTU:
688 if (hard_iface->soft_iface)
689 batadv_update_min_mtu(hard_iface->soft_iface);
691 case NETDEV_CHANGEADDR:
692 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
695 batadv_check_known_mac_addr(hard_iface->net_dev);
697 bat_priv = netdev_priv(hard_iface->soft_iface);
698 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
700 primary_if = batadv_primary_if_get_selected(bat_priv);
704 if (hard_iface == primary_if)
705 batadv_primary_if_update_addr(bat_priv, NULL);
712 batadv_hardif_free_ref(hard_iface);
715 batadv_hardif_free_ref(primary_if);
719 struct notifier_block batadv_hard_if_notifier = {
720 .notifier_call = batadv_hard_if_event,