1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "hard-interface.h"
22 #include <linux/atomic.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/errno.h>
25 #include <linux/gfp.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_ether.h>
29 #include <linux/kernel.h>
30 #include <linux/kref.h>
31 #include <linux/list.h>
32 #include <linux/netdevice.h>
33 #include <linux/printk.h>
34 #include <linux/rculist.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <net/net_namespace.h>
39 #include <net/rtnetlink.h>
40 #include <uapi/linux/batadv_packet.h>
43 #include "bridge_loop_avoidance.h"
45 #include "distributed-arp-table.h"
46 #include "gateway_client.h"
48 #include "originator.h"
50 #include "soft-interface.h"
52 #include "translation-table.h"
55 * batadv_hardif_release() - release hard interface from lists and queue for
56 * free after rcu grace period
57 * @ref: kref pointer of the hard interface
59 void batadv_hardif_release(struct kref *ref)
61 struct batadv_hard_iface *hard_iface;
63 hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
64 dev_put(hard_iface->net_dev);
66 kfree_rcu(hard_iface, rcu);
70 * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
71 * @net_dev: net_device to search for
73 * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
75 struct batadv_hard_iface *
76 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
78 struct batadv_hard_iface *hard_iface;
81 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
82 if (hard_iface->net_dev == net_dev &&
83 kref_get_unless_zero(&hard_iface->refcount))
95 * batadv_getlink_net() - return link net namespace (of use fallback)
96 * @netdev: net_device to check
97 * @fallback_net: return in case get_link_net is not available for @netdev
99 * Return: result of rtnl_link_ops->get_link_net or @fallback_net
101 static struct net *batadv_getlink_net(const struct net_device *netdev,
102 struct net *fallback_net)
104 if (!netdev->rtnl_link_ops)
107 if (!netdev->rtnl_link_ops->get_link_net)
110 return netdev->rtnl_link_ops->get_link_net(netdev);
114 * batadv_mutual_parents() - check if two devices are each others parent
116 * @net1: 1st devices netns
118 * @net2: 2nd devices netns
120 * veth devices come in pairs and each is the parent of the other!
122 * Return: true if the devices are each others parent, otherwise false
124 static bool batadv_mutual_parents(const struct net_device *dev1,
126 const struct net_device *dev2,
129 int dev1_parent_iflink = dev_get_iflink(dev1);
130 int dev2_parent_iflink = dev_get_iflink(dev2);
131 const struct net *dev1_parent_net;
132 const struct net *dev2_parent_net;
134 dev1_parent_net = batadv_getlink_net(dev1, net1);
135 dev2_parent_net = batadv_getlink_net(dev2, net2);
137 if (!dev1_parent_iflink || !dev2_parent_iflink)
140 return (dev1_parent_iflink == dev2->ifindex) &&
141 (dev2_parent_iflink == dev1->ifindex) &&
142 net_eq(dev1_parent_net, net2) &&
143 net_eq(dev2_parent_net, net1);
147 * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
148 * @net_dev: the device to check
150 * If the user creates any virtual device on top of a batman-adv interface, it
151 * is important to prevent this new interface to be used to create a new mesh
152 * network (this behaviour would lead to a batman-over-batman configuration).
153 * This function recursively checks all the fathers of the device passed as
154 * argument looking for a batman-adv soft interface.
156 * Return: true if the device is descendant of a batman-adv mesh interface (or
157 * if it is a batman-adv interface itself), false otherwise
159 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
161 struct net *net = dev_net(net_dev);
162 struct net_device *parent_dev;
163 struct net *parent_net;
166 /* check if this is a batman-adv mesh interface */
167 if (batadv_softif_is_valid(net_dev))
170 /* no more parents..stop recursion */
171 if (dev_get_iflink(net_dev) == 0 ||
172 dev_get_iflink(net_dev) == net_dev->ifindex)
175 parent_net = batadv_getlink_net(net_dev, net);
177 /* recurse over the parent device */
178 parent_dev = __dev_get_by_index((struct net *)parent_net,
179 dev_get_iflink(net_dev));
180 /* if we got a NULL parent_dev there is something broken.. */
182 pr_err("Cannot find parent device\n");
186 if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
189 ret = batadv_is_on_batman_iface(parent_dev);
194 static bool batadv_is_valid_iface(const struct net_device *net_dev)
196 if (net_dev->flags & IFF_LOOPBACK)
199 if (net_dev->type != ARPHRD_ETHER)
202 if (net_dev->addr_len != ETH_ALEN)
205 /* no batman over batman */
206 if (batadv_is_on_batman_iface(net_dev))
213 * batadv_get_real_netdevice() - check if the given netdev struct is a virtual
214 * interface on top of another 'real' interface
215 * @netdev: the device to check
217 * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
220 * Return: the 'real' net device or the original net device and NULL in case
223 static struct net_device *batadv_get_real_netdevice(struct net_device *netdev)
225 struct batadv_hard_iface *hard_iface = NULL;
226 struct net_device *real_netdev = NULL;
227 struct net *real_net;
236 if (netdev->ifindex == dev_get_iflink(netdev)) {
241 hard_iface = batadv_hardif_get_by_netdev(netdev);
242 if (!hard_iface || !hard_iface->soft_iface)
245 net = dev_net(hard_iface->soft_iface);
246 ifindex = dev_get_iflink(netdev);
247 real_net = batadv_getlink_net(netdev, net);
248 real_netdev = dev_get_by_index(real_net, ifindex);
252 batadv_hardif_put(hard_iface);
257 * batadv_get_real_netdev() - check if the given net_device struct is a virtual
258 * interface on top of another 'real' interface
259 * @net_device: the device to check
261 * Return: the 'real' net device or the original net device and NULL in case
264 struct net_device *batadv_get_real_netdev(struct net_device *net_device)
266 struct net_device *real_netdev;
269 real_netdev = batadv_get_real_netdevice(net_device);
276 * batadv_is_wext_netdev() - check if the given net_device struct is a
277 * wext wifi interface
278 * @net_device: the device to check
280 * Return: true if the net device is a wext wireless device, false
283 static bool batadv_is_wext_netdev(struct net_device *net_device)
288 #ifdef CONFIG_WIRELESS_EXT
289 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
290 * check for wireless_handlers != NULL
292 if (net_device->wireless_handlers)
300 * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
301 * cfg80211 wifi interface
302 * @net_device: the device to check
304 * Return: true if the net device is a cfg80211 wireless device, false
307 static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
312 /* cfg80211 drivers have to set ieee80211_ptr */
313 if (net_device->ieee80211_ptr)
320 * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
321 * @net_device: the device to check
323 * Return: batadv_hard_iface_wifi_flags flags of the device
325 static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
328 struct net_device *real_netdev;
330 if (batadv_is_wext_netdev(net_device))
331 wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
333 if (batadv_is_cfg80211_netdev(net_device))
334 wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
336 real_netdev = batadv_get_real_netdevice(net_device);
340 if (real_netdev == net_device)
343 if (batadv_is_wext_netdev(real_netdev))
344 wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
346 if (batadv_is_cfg80211_netdev(real_netdev))
347 wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
350 dev_put(real_netdev);
355 * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi
357 * @hard_iface: the device to check
359 * Return: true if the net device is a cfg80211 wireless device, false
362 bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface)
364 u32 allowed_flags = 0;
366 allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
367 allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
369 return !!(hard_iface->wifi_flags & allowed_flags);
373 * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
374 * @hard_iface: the device to check
376 * Return: true if the net device is a 802.11 wireless device, false otherwise.
378 bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
383 return hard_iface->wifi_flags != 0;
387 * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
388 * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
389 * @orig_addr: the originator of this packet
390 * @orig_neigh: originator address of the forwarder we just got the packet from
391 * (NULL if we originated)
393 * Checks whether a packet needs to be (re)broadcasted on the given interface.
396 * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
397 * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
398 * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
399 * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
401 int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
402 u8 *orig_addr, u8 *orig_neigh)
404 struct batadv_hardif_neigh_node *hardif_neigh;
405 struct hlist_node *first;
406 int ret = BATADV_HARDIF_BCAST_OK;
410 /* 0 neighbors -> no (re)broadcast */
411 first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
413 ret = BATADV_HARDIF_BCAST_NORECIPIENT;
417 /* >1 neighbors -> (re)brodcast */
418 if (rcu_dereference(hlist_next_rcu(first)))
421 hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
424 /* 1 neighbor, is the originator -> no rebroadcast */
425 if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
426 ret = BATADV_HARDIF_BCAST_DUPORIG;
427 /* 1 neighbor, is the one we received from -> no rebroadcast */
428 } else if (orig_neigh &&
429 batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
430 ret = BATADV_HARDIF_BCAST_DUPFWD;
438 static struct batadv_hard_iface *
439 batadv_hardif_get_active(const struct net_device *soft_iface)
441 struct batadv_hard_iface *hard_iface;
444 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
445 if (hard_iface->soft_iface != soft_iface)
448 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
449 kref_get_unless_zero(&hard_iface->refcount))
460 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
461 struct batadv_hard_iface *oldif)
463 struct batadv_hard_iface *primary_if;
465 primary_if = batadv_primary_if_get_selected(bat_priv);
469 batadv_dat_init_own_addr(bat_priv, primary_if);
470 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
473 batadv_hardif_put(primary_if);
476 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
477 struct batadv_hard_iface *new_hard_iface)
479 struct batadv_hard_iface *curr_hard_iface;
484 kref_get(&new_hard_iface->refcount);
486 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
487 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
492 bat_priv->algo_ops->iface.primary_set(new_hard_iface);
493 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
497 batadv_hardif_put(curr_hard_iface);
501 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
503 if (hard_iface->net_dev->flags & IFF_UP)
509 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
511 const struct batadv_hard_iface *hard_iface;
514 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
515 if (hard_iface->if_status != BATADV_IF_ACTIVE &&
516 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
519 if (hard_iface->net_dev == net_dev)
522 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
526 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
527 net_dev->dev_addr, hard_iface->net_dev->name);
528 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
534 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
535 * @soft_iface: netdev struct of the mesh interface
537 static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
539 const struct batadv_hard_iface *hard_iface;
540 unsigned short lower_header_len = ETH_HLEN;
541 unsigned short lower_headroom = 0;
542 unsigned short lower_tailroom = 0;
543 unsigned short needed_headroom;
546 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
547 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
550 if (hard_iface->soft_iface != soft_iface)
553 lower_header_len = max_t(unsigned short, lower_header_len,
554 hard_iface->net_dev->hard_header_len);
556 lower_headroom = max_t(unsigned short, lower_headroom,
557 hard_iface->net_dev->needed_headroom);
559 lower_tailroom = max_t(unsigned short, lower_tailroom,
560 hard_iface->net_dev->needed_tailroom);
564 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
565 needed_headroom += batadv_max_header_len();
567 soft_iface->needed_headroom = needed_headroom;
568 soft_iface->needed_tailroom = lower_tailroom;
572 * batadv_hardif_min_mtu() - Calculate maximum MTU for soft interface
573 * @soft_iface: netdev struct of the soft interface
575 * Return: MTU for the soft-interface (limited by the minimal MTU of all active
578 int batadv_hardif_min_mtu(struct net_device *soft_iface)
580 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
581 const struct batadv_hard_iface *hard_iface;
582 int min_mtu = INT_MAX;
585 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
586 if (hard_iface->if_status != BATADV_IF_ACTIVE &&
587 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
590 if (hard_iface->soft_iface != soft_iface)
593 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
597 if (atomic_read(&bat_priv->fragmentation) == 0)
600 /* with fragmentation enabled the maximum size of internally generated
601 * packets such as translation table exchanges or tvlv containers, etc
602 * has to be calculated
604 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
605 min_mtu -= sizeof(struct batadv_frag_packet);
606 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
609 /* report to the other components the maximum amount of bytes that
610 * batman-adv can send over the wire (without considering the payload
611 * overhead). For example, this value is used by TT to compute the
612 * maximum local table table size
614 atomic_set(&bat_priv->packet_size_max, min_mtu);
616 /* the real soft-interface MTU is computed by removing the payload
617 * overhead from the maximum amount of bytes that was just computed.
619 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
621 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
625 * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
627 * @soft_iface: netdev struct of the soft interface
629 void batadv_update_min_mtu(struct net_device *soft_iface)
631 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
633 /* Check if the local translate table should be cleaned up to match a
634 * new (and smaller) MTU.
636 batadv_tt_local_resize_to_mtu(soft_iface);
640 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
642 struct batadv_priv *bat_priv;
643 struct batadv_hard_iface *primary_if = NULL;
645 if (hard_iface->if_status != BATADV_IF_INACTIVE)
648 bat_priv = netdev_priv(hard_iface->soft_iface);
650 bat_priv->algo_ops->iface.update_mac(hard_iface);
651 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
653 /* the first active interface becomes our primary interface or
654 * the next active interface after the old primary interface was removed
656 primary_if = batadv_primary_if_get_selected(bat_priv);
658 batadv_primary_if_select(bat_priv, hard_iface);
660 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
661 hard_iface->net_dev->name);
663 batadv_update_min_mtu(hard_iface->soft_iface);
665 if (bat_priv->algo_ops->iface.activate)
666 bat_priv->algo_ops->iface.activate(hard_iface);
670 batadv_hardif_put(primary_if);
674 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
676 if (hard_iface->if_status != BATADV_IF_ACTIVE &&
677 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
680 hard_iface->if_status = BATADV_IF_INACTIVE;
682 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
683 hard_iface->net_dev->name);
685 batadv_update_min_mtu(hard_iface->soft_iface);
689 * batadv_master_del_slave() - remove hard_iface from the current master iface
690 * @slave: the interface enslaved in another master
691 * @master: the master from which slave has to be removed
693 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
694 * is free'd and master can correctly change its internal state.
696 * Return: 0 on success, a negative value representing the error otherwise
698 static int batadv_master_del_slave(struct batadv_hard_iface *slave,
699 struct net_device *master)
707 if (master->netdev_ops->ndo_del_slave)
708 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
714 * batadv_hardif_enable_interface() - Enslave hard interface to soft interface
715 * @hard_iface: hard interface to add to soft interface
716 * @net: the applicable net namespace
717 * @iface_name: name of the soft interface
719 * Return: 0 on success or negative error number in case of failure
721 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
722 struct net *net, const char *iface_name)
724 struct batadv_priv *bat_priv;
725 struct net_device *soft_iface, *master;
726 __be16 ethertype = htons(ETH_P_BATMAN);
727 int max_header_len = batadv_max_header_len();
730 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
733 kref_get(&hard_iface->refcount);
735 soft_iface = dev_get_by_name(net, iface_name);
738 soft_iface = batadv_softif_create(net, iface_name);
745 /* dev_get_by_name() increases the reference counter for us */
746 dev_hold(soft_iface);
749 if (!batadv_softif_is_valid(soft_iface)) {
750 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
756 /* check if the interface is enslaved in another virtual one and
757 * in that case unlink it first
759 master = netdev_master_upper_dev_get(hard_iface->net_dev);
760 ret = batadv_master_del_slave(hard_iface, master);
764 hard_iface->soft_iface = soft_iface;
765 bat_priv = netdev_priv(hard_iface->soft_iface);
767 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
768 soft_iface, NULL, NULL, NULL);
772 ret = bat_priv->algo_ops->iface.enable(hard_iface);
776 hard_iface->if_status = BATADV_IF_INACTIVE;
778 kref_get(&hard_iface->refcount);
779 hard_iface->batman_adv_ptype.type = ethertype;
780 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
781 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
782 dev_add_pack(&hard_iface->batman_adv_ptype);
784 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
785 hard_iface->net_dev->name);
787 if (atomic_read(&bat_priv->fragmentation) &&
788 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
789 batadv_info(hard_iface->soft_iface,
790 "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",
791 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
792 ETH_DATA_LEN + max_header_len);
794 if (!atomic_read(&bat_priv->fragmentation) &&
795 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
796 batadv_info(hard_iface->soft_iface,
797 "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",
798 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
799 ETH_DATA_LEN + max_header_len);
801 if (batadv_hardif_is_iface_up(hard_iface))
802 batadv_hardif_activate_interface(hard_iface);
804 batadv_err(hard_iface->soft_iface,
805 "Not using interface %s (retrying later): interface not active\n",
806 hard_iface->net_dev->name);
808 batadv_hardif_recalc_extra_skbroom(soft_iface);
814 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
816 hard_iface->soft_iface = NULL;
819 batadv_hardif_put(hard_iface);
824 * batadv_hardif_cnt() - get number of interfaces enslaved to soft interface
825 * @soft_iface: soft interface to check
827 * This function is only using RCU for locking - the result can therefore be
828 * off when another functions is modifying the list at the same time. The
829 * caller can use the rtnl_lock to make sure that the count is accurate.
831 * Return: number of connected/enslaved hard interfaces
833 static size_t batadv_hardif_cnt(const struct net_device *soft_iface)
835 struct batadv_hard_iface *hard_iface;
839 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
840 if (hard_iface->soft_iface != soft_iface)
851 * batadv_hardif_disable_interface() - Remove hard interface from soft interface
852 * @hard_iface: hard interface to be removed
853 * @autodel: whether to delete soft interface when it doesn't contain any other
856 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
857 enum batadv_hard_if_cleanup autodel)
859 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
860 struct batadv_hard_iface *primary_if = NULL;
862 batadv_hardif_deactivate_interface(hard_iface);
864 if (hard_iface->if_status != BATADV_IF_INACTIVE)
867 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
868 hard_iface->net_dev->name);
869 dev_remove_pack(&hard_iface->batman_adv_ptype);
870 batadv_hardif_put(hard_iface);
872 primary_if = batadv_primary_if_get_selected(bat_priv);
873 if (hard_iface == primary_if) {
874 struct batadv_hard_iface *new_if;
876 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
877 batadv_primary_if_select(bat_priv, new_if);
880 batadv_hardif_put(new_if);
883 bat_priv->algo_ops->iface.disable(hard_iface);
884 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
886 /* delete all references to this hard_iface */
887 batadv_purge_orig_ref(bat_priv);
888 batadv_purge_outstanding_packets(bat_priv, hard_iface);
889 dev_put(hard_iface->soft_iface);
891 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
892 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
894 /* nobody uses this interface anymore */
895 if (batadv_hardif_cnt(hard_iface->soft_iface) <= 1) {
896 batadv_gw_check_client_stop(bat_priv);
898 if (autodel == BATADV_IF_CLEANUP_AUTO)
899 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
902 hard_iface->soft_iface = NULL;
903 batadv_hardif_put(hard_iface);
907 batadv_hardif_put(primary_if);
910 static struct batadv_hard_iface *
911 batadv_hardif_add_interface(struct net_device *net_dev)
913 struct batadv_hard_iface *hard_iface;
918 if (!batadv_is_valid_iface(net_dev))
923 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
927 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
931 hard_iface->net_dev = net_dev;
932 hard_iface->soft_iface = NULL;
933 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
935 ret = batadv_debugfs_add_hardif(hard_iface);
939 INIT_LIST_HEAD(&hard_iface->list);
940 INIT_HLIST_HEAD(&hard_iface->neigh_list);
942 spin_lock_init(&hard_iface->neigh_list_lock);
943 kref_init(&hard_iface->refcount);
945 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
946 hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
947 if (batadv_is_wifi_hardif(hard_iface))
948 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
950 batadv_v_hardif_init(hard_iface);
952 batadv_check_known_mac_addr(hard_iface->net_dev);
953 kref_get(&hard_iface->refcount);
954 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
955 batadv_hardif_generation++;
960 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
969 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
973 /* first deactivate interface */
974 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
975 batadv_hardif_disable_interface(hard_iface,
976 BATADV_IF_CLEANUP_KEEP);
978 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
981 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
982 batadv_debugfs_del_hardif(hard_iface);
983 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
984 batadv_hardif_put(hard_iface);
988 * batadv_hardif_remove_interfaces() - Remove all hard interfaces
990 void batadv_hardif_remove_interfaces(void)
992 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
995 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
996 &batadv_hardif_list, list) {
997 list_del_rcu(&hard_iface->list);
998 batadv_hardif_generation++;
999 batadv_hardif_remove_interface(hard_iface);
1005 * batadv_hard_if_event_softif() - Handle events for soft interfaces
1006 * @event: NETDEV_* event to handle
1007 * @net_dev: net_device which generated an event
1009 * Return: NOTIFY_* result
1011 static int batadv_hard_if_event_softif(unsigned long event,
1012 struct net_device *net_dev)
1014 struct batadv_priv *bat_priv;
1017 case NETDEV_REGISTER:
1018 batadv_sysfs_add_meshif(net_dev);
1019 bat_priv = netdev_priv(net_dev);
1020 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
1022 case NETDEV_CHANGENAME:
1023 batadv_debugfs_rename_meshif(net_dev);
1030 static int batadv_hard_if_event(struct notifier_block *this,
1031 unsigned long event, void *ptr)
1033 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
1034 struct batadv_hard_iface *hard_iface;
1035 struct batadv_hard_iface *primary_if = NULL;
1036 struct batadv_priv *bat_priv;
1038 if (batadv_softif_is_valid(net_dev))
1039 return batadv_hard_if_event_softif(event, net_dev);
1041 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1042 if (!hard_iface && (event == NETDEV_REGISTER ||
1043 event == NETDEV_POST_TYPE_CHANGE))
1044 hard_iface = batadv_hardif_add_interface(net_dev);
1051 batadv_hardif_activate_interface(hard_iface);
1053 case NETDEV_GOING_DOWN:
1055 batadv_hardif_deactivate_interface(hard_iface);
1057 case NETDEV_UNREGISTER:
1058 case NETDEV_PRE_TYPE_CHANGE:
1059 list_del_rcu(&hard_iface->list);
1060 batadv_hardif_generation++;
1062 batadv_hardif_remove_interface(hard_iface);
1064 case NETDEV_CHANGEMTU:
1065 if (hard_iface->soft_iface)
1066 batadv_update_min_mtu(hard_iface->soft_iface);
1068 case NETDEV_CHANGEADDR:
1069 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
1072 batadv_check_known_mac_addr(hard_iface->net_dev);
1074 bat_priv = netdev_priv(hard_iface->soft_iface);
1075 bat_priv->algo_ops->iface.update_mac(hard_iface);
1077 primary_if = batadv_primary_if_get_selected(bat_priv);
1081 if (hard_iface == primary_if)
1082 batadv_primary_if_update_addr(bat_priv, NULL);
1084 case NETDEV_CHANGEUPPER:
1085 hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
1086 if (batadv_is_wifi_hardif(hard_iface))
1087 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
1089 case NETDEV_CHANGENAME:
1090 batadv_debugfs_rename_hardif(hard_iface);
1097 batadv_hardif_put(hard_iface);
1100 batadv_hardif_put(primary_if);
1104 struct notifier_block batadv_hard_if_notifier = {
1105 .notifier_call = batadv_hard_if_event,