1 /* Copyright (C) 2007-2015 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, see <http://www.gnu.org/licenses/>.
18 #include "hard-interface.h"
21 #include <linux/byteorder/generic.h>
22 #include <linux/errno.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/mutex.h>
30 #include <linux/netdevice.h>
31 #include <linux/printk.h>
32 #include <linux/rculist.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
36 #include <net/net_namespace.h>
38 #include "bridge_loop_avoidance.h"
40 #include "distributed-arp-table.h"
41 #include "gateway_client.h"
42 #include "originator.h"
45 #include "soft-interface.h"
47 #include "translation-table.h"
50 * batadv_hardif_release - release hard interface from lists and queue for
51 * free after rcu grace period
52 * @hard_iface: the hard interface to free
54 void batadv_hardif_release(struct batadv_hard_iface *hard_iface)
56 dev_put(hard_iface->net_dev);
58 kfree_rcu(hard_iface, rcu);
61 struct batadv_hard_iface *
62 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
64 struct batadv_hard_iface *hard_iface;
67 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
68 if (hard_iface->net_dev == net_dev &&
69 atomic_inc_not_zero(&hard_iface->refcount))
81 * batadv_mutual_parents - check if two devices are each others parent
82 * @dev1: 1st net_device
83 * @dev2: 2nd net_device
85 * veth devices come in pairs and each is the parent of the other!
87 * Return: true if the devices are each others parent, otherwise false
89 static bool batadv_mutual_parents(const struct net_device *dev1,
90 const struct net_device *dev2)
92 int dev1_parent_iflink = dev_get_iflink(dev1);
93 int dev2_parent_iflink = dev_get_iflink(dev2);
95 if (!dev1_parent_iflink || !dev2_parent_iflink)
98 return (dev1_parent_iflink == dev2->ifindex) &&
99 (dev2_parent_iflink == dev1->ifindex);
103 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
104 * @net_dev: the device to check
106 * If the user creates any virtual device on top of a batman-adv interface, it
107 * is important to prevent this new interface to be used to create a new mesh
108 * network (this behaviour would lead to a batman-over-batman configuration).
109 * This function recursively checks all the fathers of the device passed as
110 * argument looking for a batman-adv soft interface.
112 * Returns true if the device is descendant of a batman-adv mesh interface (or
113 * if it is a batman-adv interface itself), false otherwise
115 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
117 struct net_device *parent_dev;
120 /* check if this is a batman-adv mesh interface */
121 if (batadv_softif_is_valid(net_dev))
124 /* no more parents..stop recursion */
125 if (dev_get_iflink(net_dev) == 0 ||
126 dev_get_iflink(net_dev) == net_dev->ifindex)
129 /* recurse over the parent device */
130 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
131 /* if we got a NULL parent_dev there is something broken.. */
133 pr_err("Cannot find parent device\n");
137 if (batadv_mutual_parents(net_dev, parent_dev))
140 ret = batadv_is_on_batman_iface(parent_dev);
145 static int batadv_is_valid_iface(const struct net_device *net_dev)
147 if (net_dev->flags & IFF_LOOPBACK)
150 if (net_dev->type != ARPHRD_ETHER)
153 if (net_dev->addr_len != ETH_ALEN)
156 /* no batman over batman */
157 if (batadv_is_on_batman_iface(net_dev))
164 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
166 * @net_device: the device to check
168 * Returns true if the net device is a 802.11 wireless device, false otherwise.
170 bool batadv_is_wifi_netdev(struct net_device *net_device)
175 #ifdef CONFIG_WIRELESS_EXT
176 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
177 * check for wireless_handlers != NULL
179 if (net_device->wireless_handlers)
183 /* cfg80211 drivers have to set ieee80211_ptr */
184 if (net_device->ieee80211_ptr)
190 static struct batadv_hard_iface *
191 batadv_hardif_get_active(const struct net_device *soft_iface)
193 struct batadv_hard_iface *hard_iface;
196 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
197 if (hard_iface->soft_iface != soft_iface)
200 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
201 atomic_inc_not_zero(&hard_iface->refcount))
212 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
213 struct batadv_hard_iface *oldif)
215 struct batadv_hard_iface *primary_if;
217 primary_if = batadv_primary_if_get_selected(bat_priv);
221 batadv_dat_init_own_addr(bat_priv, primary_if);
222 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
225 batadv_hardif_free_ref(primary_if);
228 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
229 struct batadv_hard_iface *new_hard_iface)
231 struct batadv_hard_iface *curr_hard_iface;
235 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
236 new_hard_iface = NULL;
238 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
239 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
244 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
245 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
249 batadv_hardif_free_ref(curr_hard_iface);
253 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
255 if (hard_iface->net_dev->flags & IFF_UP)
261 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
263 const struct batadv_hard_iface *hard_iface;
266 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
267 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
268 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
271 if (hard_iface->net_dev == net_dev)
274 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
278 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
279 net_dev->dev_addr, hard_iface->net_dev->name);
280 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
286 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
287 * @soft_iface: netdev struct of the mesh interface
289 static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
291 const struct batadv_hard_iface *hard_iface;
292 unsigned short lower_header_len = ETH_HLEN;
293 unsigned short lower_headroom = 0;
294 unsigned short lower_tailroom = 0;
295 unsigned short needed_headroom;
298 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
299 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
302 if (hard_iface->soft_iface != soft_iface)
305 lower_header_len = max_t(unsigned short, lower_header_len,
306 hard_iface->net_dev->hard_header_len);
308 lower_headroom = max_t(unsigned short, lower_headroom,
309 hard_iface->net_dev->needed_headroom);
311 lower_tailroom = max_t(unsigned short, lower_tailroom,
312 hard_iface->net_dev->needed_tailroom);
316 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
317 needed_headroom += batadv_max_header_len();
319 /* fragmentation headers don't strip the unicast/... header */
320 needed_headroom += sizeof(struct batadv_frag_packet);
322 soft_iface->needed_headroom = needed_headroom;
323 soft_iface->needed_tailroom = lower_tailroom;
326 int batadv_hardif_min_mtu(struct net_device *soft_iface)
328 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
329 const struct batadv_hard_iface *hard_iface;
330 int min_mtu = INT_MAX;
333 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
334 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
335 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
338 if (hard_iface->soft_iface != soft_iface)
341 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
345 if (atomic_read(&bat_priv->fragmentation) == 0)
348 /* with fragmentation enabled the maximum size of internally generated
349 * packets such as translation table exchanges or tvlv containers, etc
350 * has to be calculated
352 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
353 min_mtu -= sizeof(struct batadv_frag_packet);
354 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
357 /* report to the other components the maximum amount of bytes that
358 * batman-adv can send over the wire (without considering the payload
359 * overhead). For example, this value is used by TT to compute the
360 * maximum local table table size
362 atomic_set(&bat_priv->packet_size_max, min_mtu);
364 /* the real soft-interface MTU is computed by removing the payload
365 * overhead from the maximum amount of bytes that was just computed.
367 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
369 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
372 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
373 void batadv_update_min_mtu(struct net_device *soft_iface)
375 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
377 /* Check if the local translate table should be cleaned up to match a
378 * new (and smaller) MTU.
380 batadv_tt_local_resize_to_mtu(soft_iface);
384 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
386 struct batadv_priv *bat_priv;
387 struct batadv_hard_iface *primary_if = NULL;
389 if (hard_iface->if_status != BATADV_IF_INACTIVE)
392 bat_priv = netdev_priv(hard_iface->soft_iface);
394 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
395 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
397 /* the first active interface becomes our primary interface or
398 * the next active interface after the old primary interface was removed
400 primary_if = batadv_primary_if_get_selected(bat_priv);
402 batadv_primary_if_select(bat_priv, hard_iface);
404 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
405 hard_iface->net_dev->name);
407 batadv_update_min_mtu(hard_iface->soft_iface);
411 batadv_hardif_free_ref(primary_if);
415 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
417 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
418 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
421 hard_iface->if_status = BATADV_IF_INACTIVE;
423 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
424 hard_iface->net_dev->name);
426 batadv_update_min_mtu(hard_iface->soft_iface);
430 * batadv_master_del_slave - remove hard_iface from the current master interface
431 * @slave: the interface enslaved in another master
432 * @master: the master from which slave has to be removed
434 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
435 * is free'd and master can correctly change its internal state.
436 * Return 0 on success, a negative value representing the error otherwise
438 static int batadv_master_del_slave(struct batadv_hard_iface *slave,
439 struct net_device *master)
447 if (master->netdev_ops->ndo_del_slave)
448 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
453 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
454 const char *iface_name)
456 struct batadv_priv *bat_priv;
457 struct net_device *soft_iface, *master;
458 __be16 ethertype = htons(ETH_P_BATMAN);
459 int max_header_len = batadv_max_header_len();
462 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
465 if (!atomic_inc_not_zero(&hard_iface->refcount))
468 soft_iface = dev_get_by_name(&init_net, iface_name);
471 soft_iface = batadv_softif_create(iface_name);
478 /* dev_get_by_name() increases the reference counter for us */
479 dev_hold(soft_iface);
482 if (!batadv_softif_is_valid(soft_iface)) {
483 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
489 /* check if the interface is enslaved in another virtual one and
490 * in that case unlink it first
492 master = netdev_master_upper_dev_get(hard_iface->net_dev);
493 ret = batadv_master_del_slave(hard_iface, master);
497 hard_iface->soft_iface = soft_iface;
498 bat_priv = netdev_priv(hard_iface->soft_iface);
500 if (bat_priv->num_ifaces >= UINT_MAX) {
505 ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
509 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
513 hard_iface->if_num = bat_priv->num_ifaces;
514 bat_priv->num_ifaces++;
515 hard_iface->if_status = BATADV_IF_INACTIVE;
516 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
518 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
519 bat_priv->num_ifaces--;
520 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
524 hard_iface->batman_adv_ptype.type = ethertype;
525 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
526 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
527 dev_add_pack(&hard_iface->batman_adv_ptype);
529 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
530 hard_iface->net_dev->name);
532 if (atomic_read(&bat_priv->fragmentation) &&
533 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
534 batadv_info(hard_iface->soft_iface,
535 "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",
536 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
537 ETH_DATA_LEN + max_header_len);
539 if (!atomic_read(&bat_priv->fragmentation) &&
540 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
541 batadv_info(hard_iface->soft_iface,
542 "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",
543 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
544 ETH_DATA_LEN + max_header_len);
546 if (batadv_hardif_is_iface_up(hard_iface))
547 batadv_hardif_activate_interface(hard_iface);
549 batadv_err(hard_iface->soft_iface,
550 "Not using interface %s (retrying later): interface not active\n",
551 hard_iface->net_dev->name);
553 batadv_hardif_recalc_extra_skbroom(soft_iface);
555 /* begin scheduling originator messages on that interface */
556 batadv_schedule_bat_ogm(hard_iface);
562 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
564 hard_iface->soft_iface = NULL;
567 batadv_hardif_free_ref(hard_iface);
571 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
572 enum batadv_hard_if_cleanup autodel)
574 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
575 struct batadv_hard_iface *primary_if = NULL;
577 batadv_hardif_deactivate_interface(hard_iface);
579 if (hard_iface->if_status != BATADV_IF_INACTIVE)
582 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
583 hard_iface->net_dev->name);
584 dev_remove_pack(&hard_iface->batman_adv_ptype);
586 bat_priv->num_ifaces--;
587 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
589 primary_if = batadv_primary_if_get_selected(bat_priv);
590 if (hard_iface == primary_if) {
591 struct batadv_hard_iface *new_if;
593 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
594 batadv_primary_if_select(bat_priv, new_if);
597 batadv_hardif_free_ref(new_if);
600 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
601 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
603 /* delete all references to this hard_iface */
604 batadv_purge_orig_ref(bat_priv);
605 batadv_purge_outstanding_packets(bat_priv, hard_iface);
606 dev_put(hard_iface->soft_iface);
608 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
609 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
611 /* nobody uses this interface anymore */
612 if (bat_priv->num_ifaces == 0) {
613 batadv_gw_check_client_stop(bat_priv);
615 if (autodel == BATADV_IF_CLEANUP_AUTO)
616 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
619 hard_iface->soft_iface = NULL;
620 batadv_hardif_free_ref(hard_iface);
624 batadv_hardif_free_ref(primary_if);
628 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
629 * @work: work queue item
631 * Free the parts of the hard interface which can not be removed under
632 * rtnl lock (to prevent deadlock situations).
634 static void batadv_hardif_remove_interface_finish(struct work_struct *work)
636 struct batadv_hard_iface *hard_iface;
638 hard_iface = container_of(work, struct batadv_hard_iface,
641 batadv_debugfs_del_hardif(hard_iface);
642 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
643 batadv_hardif_free_ref(hard_iface);
646 static struct batadv_hard_iface *
647 batadv_hardif_add_interface(struct net_device *net_dev)
649 struct batadv_hard_iface *hard_iface;
654 ret = batadv_is_valid_iface(net_dev);
660 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
664 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
668 hard_iface->if_num = 0;
669 hard_iface->net_dev = net_dev;
670 hard_iface->soft_iface = NULL;
671 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
673 ret = batadv_debugfs_add_hardif(hard_iface);
677 INIT_LIST_HEAD(&hard_iface->list);
678 mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
679 INIT_WORK(&hard_iface->cleanup_work,
680 batadv_hardif_remove_interface_finish);
682 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
683 if (batadv_is_wifi_netdev(net_dev))
684 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
686 /* extra reference for return */
687 atomic_set(&hard_iface->refcount, 2);
689 batadv_check_known_mac_addr(hard_iface->net_dev);
690 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
695 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
704 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
708 /* first deactivate interface */
709 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
710 batadv_hardif_disable_interface(hard_iface,
711 BATADV_IF_CLEANUP_AUTO);
713 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
716 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
717 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
720 void batadv_hardif_remove_interfaces(void)
722 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
725 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
726 &batadv_hardif_list, list) {
727 list_del_rcu(&hard_iface->list);
728 batadv_hardif_remove_interface(hard_iface);
734 * batadv_hard_if_event_softif() - Handle events for soft interfaces
735 * @event: NETDEV_* event to handle
736 * @net_dev: net_device which generated an event
738 * Return: NOTIFY_* result
740 static int batadv_hard_if_event_softif(unsigned long event,
741 struct net_device *net_dev)
743 struct batadv_priv *bat_priv;
746 case NETDEV_REGISTER:
747 batadv_sysfs_add_meshif(net_dev);
748 bat_priv = netdev_priv(net_dev);
749 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
751 case NETDEV_CHANGENAME:
752 batadv_debugfs_rename_meshif(net_dev);
759 static int batadv_hard_if_event(struct notifier_block *this,
760 unsigned long event, void *ptr)
762 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
763 struct batadv_hard_iface *hard_iface;
764 struct batadv_hard_iface *primary_if = NULL;
765 struct batadv_priv *bat_priv;
767 if (batadv_softif_is_valid(net_dev))
768 return batadv_hard_if_event_softif(event, net_dev);
770 hard_iface = batadv_hardif_get_by_netdev(net_dev);
771 if (!hard_iface && event == NETDEV_REGISTER)
772 hard_iface = batadv_hardif_add_interface(net_dev);
779 batadv_hardif_activate_interface(hard_iface);
781 case NETDEV_GOING_DOWN:
783 batadv_hardif_deactivate_interface(hard_iface);
785 case NETDEV_UNREGISTER:
786 list_del_rcu(&hard_iface->list);
788 batadv_hardif_remove_interface(hard_iface);
790 case NETDEV_CHANGEMTU:
791 if (hard_iface->soft_iface)
792 batadv_update_min_mtu(hard_iface->soft_iface);
794 case NETDEV_CHANGEADDR:
795 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
798 batadv_check_known_mac_addr(hard_iface->net_dev);
800 bat_priv = netdev_priv(hard_iface->soft_iface);
801 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
803 primary_if = batadv_primary_if_get_selected(bat_priv);
807 if (hard_iface == primary_if)
808 batadv_primary_if_update_addr(bat_priv, NULL);
810 case NETDEV_CHANGENAME:
811 batadv_debugfs_rename_hardif(hard_iface);
818 batadv_hardif_free_ref(hard_iface);
821 batadv_hardif_free_ref(primary_if);
825 struct notifier_block batadv_hard_if_notifier = {
826 .notifier_call = batadv_hard_if_event,