batman-adv: Fix duplicated OGMs on NETDEV_UP
[linux/fpc-iii.git] / net / batman-adv / hard-interface.c
blob5346c6595a097ddfe4bebcd2df5dd3fa8e3e0bba
1 /* Copyright (C) 2007-2016 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"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/fs.h>
25 #include <linux/if.h>
26 #include <linux/if_arp.h>
27 #include <linux/if_ether.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/list.h>
31 #include <linux/netdevice.h>
32 #include <linux/printk.h>
33 #include <linux/rculist.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <net/net_namespace.h>
38 #include <net/rtnetlink.h>
40 #include "bat_v.h"
41 #include "bridge_loop_avoidance.h"
42 #include "debugfs.h"
43 #include "distributed-arp-table.h"
44 #include "gateway_client.h"
45 #include "log.h"
46 #include "originator.h"
47 #include "packet.h"
48 #include "send.h"
49 #include "soft-interface.h"
50 #include "sysfs.h"
51 #include "translation-table.h"
53 /**
54 * batadv_hardif_release - release hard interface from lists and queue for
55 * free after rcu grace period
56 * @ref: kref pointer of the hard interface
58 void batadv_hardif_release(struct kref *ref)
60 struct batadv_hard_iface *hard_iface;
62 hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
63 dev_put(hard_iface->net_dev);
65 kfree_rcu(hard_iface, rcu);
68 struct batadv_hard_iface *
69 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
71 struct batadv_hard_iface *hard_iface;
73 rcu_read_lock();
74 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
75 if (hard_iface->net_dev == net_dev &&
76 kref_get_unless_zero(&hard_iface->refcount))
77 goto out;
80 hard_iface = NULL;
82 out:
83 rcu_read_unlock();
84 return hard_iface;
87 /**
88 * batadv_getlink_net - return link net namespace (of use fallback)
89 * @netdev: net_device to check
90 * @fallback_net: return in case get_link_net is not available for @netdev
92 * Return: result of rtnl_link_ops->get_link_net or @fallback_net
94 static const struct net *batadv_getlink_net(const struct net_device *netdev,
95 const struct net *fallback_net)
97 if (!netdev->rtnl_link_ops)
98 return fallback_net;
100 if (!netdev->rtnl_link_ops->get_link_net)
101 return fallback_net;
103 return netdev->rtnl_link_ops->get_link_net(netdev);
107 * batadv_mutual_parents - check if two devices are each others parent
108 * @dev1: 1st net dev
109 * @net1: 1st devices netns
110 * @dev2: 2nd net dev
111 * @net2: 2nd devices netns
113 * veth devices come in pairs and each is the parent of the other!
115 * Return: true if the devices are each others parent, otherwise false
117 static bool batadv_mutual_parents(const struct net_device *dev1,
118 const struct net *net1,
119 const struct net_device *dev2,
120 const struct net *net2)
122 int dev1_parent_iflink = dev_get_iflink(dev1);
123 int dev2_parent_iflink = dev_get_iflink(dev2);
124 const struct net *dev1_parent_net;
125 const struct net *dev2_parent_net;
127 dev1_parent_net = batadv_getlink_net(dev1, net1);
128 dev2_parent_net = batadv_getlink_net(dev2, net2);
130 if (!dev1_parent_iflink || !dev2_parent_iflink)
131 return false;
133 return (dev1_parent_iflink == dev2->ifindex) &&
134 (dev2_parent_iflink == dev1->ifindex) &&
135 net_eq(dev1_parent_net, net2) &&
136 net_eq(dev2_parent_net, net1);
140 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
141 * @net_dev: the device to check
143 * If the user creates any virtual device on top of a batman-adv interface, it
144 * is important to prevent this new interface to be used to create a new mesh
145 * network (this behaviour would lead to a batman-over-batman configuration).
146 * This function recursively checks all the fathers of the device passed as
147 * argument looking for a batman-adv soft interface.
149 * Return: true if the device is descendant of a batman-adv mesh interface (or
150 * if it is a batman-adv interface itself), false otherwise
152 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
154 struct net *net = dev_net(net_dev);
155 struct net_device *parent_dev;
156 const struct net *parent_net;
157 bool ret;
159 /* check if this is a batman-adv mesh interface */
160 if (batadv_softif_is_valid(net_dev))
161 return true;
163 /* no more parents..stop recursion */
164 if (dev_get_iflink(net_dev) == 0 ||
165 dev_get_iflink(net_dev) == net_dev->ifindex)
166 return false;
168 parent_net = batadv_getlink_net(net_dev, net);
170 /* recurse over the parent device */
171 parent_dev = __dev_get_by_index((struct net *)parent_net,
172 dev_get_iflink(net_dev));
173 /* if we got a NULL parent_dev there is something broken.. */
174 if (!parent_dev) {
175 pr_err("Cannot find parent device\n");
176 return false;
179 if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
180 return false;
182 ret = batadv_is_on_batman_iface(parent_dev);
184 return ret;
187 static bool batadv_is_valid_iface(const struct net_device *net_dev)
189 if (net_dev->flags & IFF_LOOPBACK)
190 return false;
192 if (net_dev->type != ARPHRD_ETHER)
193 return false;
195 if (net_dev->addr_len != ETH_ALEN)
196 return false;
198 /* no batman over batman */
199 if (batadv_is_on_batman_iface(net_dev))
200 return false;
202 return true;
206 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
207 * interface
208 * @net_device: the device to check
210 * Return: true if the net device is a 802.11 wireless device, false otherwise.
212 bool batadv_is_wifi_netdev(struct net_device *net_device)
214 if (!net_device)
215 return false;
217 #ifdef CONFIG_WIRELESS_EXT
218 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
219 * check for wireless_handlers != NULL
221 if (net_device->wireless_handlers)
222 return true;
223 #endif
225 /* cfg80211 drivers have to set ieee80211_ptr */
226 if (net_device->ieee80211_ptr)
227 return true;
229 return false;
232 static struct batadv_hard_iface *
233 batadv_hardif_get_active(const struct net_device *soft_iface)
235 struct batadv_hard_iface *hard_iface;
237 rcu_read_lock();
238 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
239 if (hard_iface->soft_iface != soft_iface)
240 continue;
242 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
243 kref_get_unless_zero(&hard_iface->refcount))
244 goto out;
247 hard_iface = NULL;
249 out:
250 rcu_read_unlock();
251 return hard_iface;
254 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
255 struct batadv_hard_iface *oldif)
257 struct batadv_hard_iface *primary_if;
259 primary_if = batadv_primary_if_get_selected(bat_priv);
260 if (!primary_if)
261 goto out;
263 batadv_dat_init_own_addr(bat_priv, primary_if);
264 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
265 out:
266 if (primary_if)
267 batadv_hardif_put(primary_if);
270 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
271 struct batadv_hard_iface *new_hard_iface)
273 struct batadv_hard_iface *curr_hard_iface;
275 ASSERT_RTNL();
277 if (new_hard_iface)
278 kref_get(&new_hard_iface->refcount);
280 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
281 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
283 if (!new_hard_iface)
284 goto out;
286 bat_priv->algo_ops->iface.primary_set(new_hard_iface);
287 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
289 out:
290 if (curr_hard_iface)
291 batadv_hardif_put(curr_hard_iface);
294 static bool
295 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
297 if (hard_iface->net_dev->flags & IFF_UP)
298 return true;
300 return false;
303 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
305 const struct batadv_hard_iface *hard_iface;
307 rcu_read_lock();
308 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
309 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
310 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
311 continue;
313 if (hard_iface->net_dev == net_dev)
314 continue;
316 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
317 net_dev->dev_addr))
318 continue;
320 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
321 net_dev->dev_addr, hard_iface->net_dev->name);
322 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
324 rcu_read_unlock();
328 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
329 * @soft_iface: netdev struct of the mesh interface
331 static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
333 const struct batadv_hard_iface *hard_iface;
334 unsigned short lower_header_len = ETH_HLEN;
335 unsigned short lower_headroom = 0;
336 unsigned short lower_tailroom = 0;
337 unsigned short needed_headroom;
339 rcu_read_lock();
340 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
341 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
342 continue;
344 if (hard_iface->soft_iface != soft_iface)
345 continue;
347 lower_header_len = max_t(unsigned short, lower_header_len,
348 hard_iface->net_dev->hard_header_len);
350 lower_headroom = max_t(unsigned short, lower_headroom,
351 hard_iface->net_dev->needed_headroom);
353 lower_tailroom = max_t(unsigned short, lower_tailroom,
354 hard_iface->net_dev->needed_tailroom);
356 rcu_read_unlock();
358 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
359 needed_headroom += batadv_max_header_len();
361 soft_iface->needed_headroom = needed_headroom;
362 soft_iface->needed_tailroom = lower_tailroom;
365 int batadv_hardif_min_mtu(struct net_device *soft_iface)
367 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
368 const struct batadv_hard_iface *hard_iface;
369 int min_mtu = INT_MAX;
371 rcu_read_lock();
372 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
373 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
374 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
375 continue;
377 if (hard_iface->soft_iface != soft_iface)
378 continue;
380 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
382 rcu_read_unlock();
384 if (atomic_read(&bat_priv->fragmentation) == 0)
385 goto out;
387 /* with fragmentation enabled the maximum size of internally generated
388 * packets such as translation table exchanges or tvlv containers, etc
389 * has to be calculated
391 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
392 min_mtu -= sizeof(struct batadv_frag_packet);
393 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
395 out:
396 /* report to the other components the maximum amount of bytes that
397 * batman-adv can send over the wire (without considering the payload
398 * overhead). For example, this value is used by TT to compute the
399 * maximum local table table size
401 atomic_set(&bat_priv->packet_size_max, min_mtu);
403 /* the real soft-interface MTU is computed by removing the payload
404 * overhead from the maximum amount of bytes that was just computed.
406 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
408 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
411 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
412 void batadv_update_min_mtu(struct net_device *soft_iface)
414 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
416 /* Check if the local translate table should be cleaned up to match a
417 * new (and smaller) MTU.
419 batadv_tt_local_resize_to_mtu(soft_iface);
422 static void
423 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
425 struct batadv_priv *bat_priv;
426 struct batadv_hard_iface *primary_if = NULL;
428 if (hard_iface->if_status != BATADV_IF_INACTIVE)
429 goto out;
431 bat_priv = netdev_priv(hard_iface->soft_iface);
433 bat_priv->algo_ops->iface.update_mac(hard_iface);
434 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
436 /* the first active interface becomes our primary interface or
437 * the next active interface after the old primary interface was removed
439 primary_if = batadv_primary_if_get_selected(bat_priv);
440 if (!primary_if)
441 batadv_primary_if_select(bat_priv, hard_iface);
443 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
444 hard_iface->net_dev->name);
446 batadv_update_min_mtu(hard_iface->soft_iface);
448 if (bat_priv->algo_ops->iface.activate)
449 bat_priv->algo_ops->iface.activate(hard_iface);
451 out:
452 if (primary_if)
453 batadv_hardif_put(primary_if);
456 static void
457 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
459 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
460 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
461 return;
463 hard_iface->if_status = BATADV_IF_INACTIVE;
465 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
466 hard_iface->net_dev->name);
468 batadv_update_min_mtu(hard_iface->soft_iface);
472 * batadv_master_del_slave - remove hard_iface from the current master interface
473 * @slave: the interface enslaved in another master
474 * @master: the master from which slave has to be removed
476 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
477 * is free'd and master can correctly change its internal state.
479 * Return: 0 on success, a negative value representing the error otherwise
481 static int batadv_master_del_slave(struct batadv_hard_iface *slave,
482 struct net_device *master)
484 int ret;
486 if (!master)
487 return 0;
489 ret = -EBUSY;
490 if (master->netdev_ops->ndo_del_slave)
491 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
493 return ret;
496 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
497 struct net *net, const char *iface_name)
499 struct batadv_priv *bat_priv;
500 struct net_device *soft_iface, *master;
501 __be16 ethertype = htons(ETH_P_BATMAN);
502 int max_header_len = batadv_max_header_len();
503 int ret;
505 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
506 goto out;
508 kref_get(&hard_iface->refcount);
510 soft_iface = dev_get_by_name(net, iface_name);
512 if (!soft_iface) {
513 soft_iface = batadv_softif_create(net, iface_name);
515 if (!soft_iface) {
516 ret = -ENOMEM;
517 goto err;
520 /* dev_get_by_name() increases the reference counter for us */
521 dev_hold(soft_iface);
524 if (!batadv_softif_is_valid(soft_iface)) {
525 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
526 soft_iface->name);
527 ret = -EINVAL;
528 goto err_dev;
531 /* check if the interface is enslaved in another virtual one and
532 * in that case unlink it first
534 master = netdev_master_upper_dev_get(hard_iface->net_dev);
535 ret = batadv_master_del_slave(hard_iface, master);
536 if (ret)
537 goto err_dev;
539 hard_iface->soft_iface = soft_iface;
540 bat_priv = netdev_priv(hard_iface->soft_iface);
542 if (bat_priv->num_ifaces >= UINT_MAX) {
543 ret = -ENOSPC;
544 goto err_dev;
547 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
548 soft_iface, NULL, NULL);
549 if (ret)
550 goto err_dev;
552 ret = bat_priv->algo_ops->iface.enable(hard_iface);
553 if (ret < 0)
554 goto err_upper;
556 hard_iface->if_num = bat_priv->num_ifaces;
557 bat_priv->num_ifaces++;
558 hard_iface->if_status = BATADV_IF_INACTIVE;
559 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
560 if (ret < 0) {
561 bat_priv->algo_ops->iface.disable(hard_iface);
562 bat_priv->num_ifaces--;
563 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
564 goto err_upper;
567 kref_get(&hard_iface->refcount);
568 hard_iface->batman_adv_ptype.type = ethertype;
569 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
570 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
571 dev_add_pack(&hard_iface->batman_adv_ptype);
573 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
574 hard_iface->net_dev->name);
576 if (atomic_read(&bat_priv->fragmentation) &&
577 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
578 batadv_info(hard_iface->soft_iface,
579 "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",
580 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
581 ETH_DATA_LEN + max_header_len);
583 if (!atomic_read(&bat_priv->fragmentation) &&
584 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
585 batadv_info(hard_iface->soft_iface,
586 "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",
587 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
588 ETH_DATA_LEN + max_header_len);
590 if (batadv_hardif_is_iface_up(hard_iface))
591 batadv_hardif_activate_interface(hard_iface);
592 else
593 batadv_err(hard_iface->soft_iface,
594 "Not using interface %s (retrying later): interface not active\n",
595 hard_iface->net_dev->name);
597 batadv_hardif_recalc_extra_skbroom(soft_iface);
599 if (bat_priv->algo_ops->iface.enabled)
600 bat_priv->algo_ops->iface.enabled(hard_iface);
602 out:
603 return 0;
605 err_upper:
606 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
607 err_dev:
608 hard_iface->soft_iface = NULL;
609 dev_put(soft_iface);
610 err:
611 batadv_hardif_put(hard_iface);
612 return ret;
615 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
616 enum batadv_hard_if_cleanup autodel)
618 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
619 struct batadv_hard_iface *primary_if = NULL;
621 batadv_hardif_deactivate_interface(hard_iface);
623 if (hard_iface->if_status != BATADV_IF_INACTIVE)
624 goto out;
626 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
627 hard_iface->net_dev->name);
628 dev_remove_pack(&hard_iface->batman_adv_ptype);
629 batadv_hardif_put(hard_iface);
631 bat_priv->num_ifaces--;
632 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
634 primary_if = batadv_primary_if_get_selected(bat_priv);
635 if (hard_iface == primary_if) {
636 struct batadv_hard_iface *new_if;
638 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
639 batadv_primary_if_select(bat_priv, new_if);
641 if (new_if)
642 batadv_hardif_put(new_if);
645 bat_priv->algo_ops->iface.disable(hard_iface);
646 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
648 /* delete all references to this hard_iface */
649 batadv_purge_orig_ref(bat_priv);
650 batadv_purge_outstanding_packets(bat_priv, hard_iface);
651 dev_put(hard_iface->soft_iface);
653 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
654 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
656 /* nobody uses this interface anymore */
657 if (bat_priv->num_ifaces == 0) {
658 batadv_gw_check_client_stop(bat_priv);
660 if (autodel == BATADV_IF_CLEANUP_AUTO)
661 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
664 hard_iface->soft_iface = NULL;
665 batadv_hardif_put(hard_iface);
667 out:
668 if (primary_if)
669 batadv_hardif_put(primary_if);
672 static struct batadv_hard_iface *
673 batadv_hardif_add_interface(struct net_device *net_dev)
675 struct batadv_hard_iface *hard_iface;
676 int ret;
678 ASSERT_RTNL();
680 if (!batadv_is_valid_iface(net_dev))
681 goto out;
683 dev_hold(net_dev);
685 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
686 if (!hard_iface)
687 goto release_dev;
689 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
690 if (ret)
691 goto free_if;
693 hard_iface->if_num = 0;
694 hard_iface->net_dev = net_dev;
695 hard_iface->soft_iface = NULL;
696 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
698 ret = batadv_debugfs_add_hardif(hard_iface);
699 if (ret)
700 goto free_sysfs;
702 INIT_LIST_HEAD(&hard_iface->list);
703 INIT_HLIST_HEAD(&hard_iface->neigh_list);
705 spin_lock_init(&hard_iface->neigh_list_lock);
706 kref_init(&hard_iface->refcount);
708 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
709 if (batadv_is_wifi_netdev(net_dev))
710 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
712 batadv_v_hardif_init(hard_iface);
714 batadv_check_known_mac_addr(hard_iface->net_dev);
715 kref_get(&hard_iface->refcount);
716 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
718 return hard_iface;
720 free_sysfs:
721 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
722 free_if:
723 kfree(hard_iface);
724 release_dev:
725 dev_put(net_dev);
726 out:
727 return NULL;
730 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
732 ASSERT_RTNL();
734 /* first deactivate interface */
735 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
736 batadv_hardif_disable_interface(hard_iface,
737 BATADV_IF_CLEANUP_KEEP);
739 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
740 return;
742 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
743 batadv_debugfs_del_hardif(hard_iface);
744 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
745 batadv_hardif_put(hard_iface);
748 void batadv_hardif_remove_interfaces(void)
750 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
752 rtnl_lock();
753 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
754 &batadv_hardif_list, list) {
755 list_del_rcu(&hard_iface->list);
756 batadv_hardif_remove_interface(hard_iface);
758 rtnl_unlock();
762 * batadv_hard_if_event_softif() - Handle events for soft interfaces
763 * @event: NETDEV_* event to handle
764 * @net_dev: net_device which generated an event
766 * Return: NOTIFY_* result
768 static int batadv_hard_if_event_softif(unsigned long event,
769 struct net_device *net_dev)
771 struct batadv_priv *bat_priv;
773 switch (event) {
774 case NETDEV_REGISTER:
775 batadv_sysfs_add_meshif(net_dev);
776 bat_priv = netdev_priv(net_dev);
777 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
778 break;
779 case NETDEV_CHANGENAME:
780 batadv_debugfs_rename_meshif(net_dev);
781 break;
784 return NOTIFY_DONE;
787 static int batadv_hard_if_event(struct notifier_block *this,
788 unsigned long event, void *ptr)
790 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
791 struct batadv_hard_iface *hard_iface;
792 struct batadv_hard_iface *primary_if = NULL;
793 struct batadv_priv *bat_priv;
795 if (batadv_softif_is_valid(net_dev))
796 return batadv_hard_if_event_softif(event, net_dev);
798 hard_iface = batadv_hardif_get_by_netdev(net_dev);
799 if (!hard_iface && (event == NETDEV_REGISTER ||
800 event == NETDEV_POST_TYPE_CHANGE))
801 hard_iface = batadv_hardif_add_interface(net_dev);
803 if (!hard_iface)
804 goto out;
806 switch (event) {
807 case NETDEV_UP:
808 batadv_hardif_activate_interface(hard_iface);
809 break;
810 case NETDEV_GOING_DOWN:
811 case NETDEV_DOWN:
812 batadv_hardif_deactivate_interface(hard_iface);
813 break;
814 case NETDEV_UNREGISTER:
815 case NETDEV_PRE_TYPE_CHANGE:
816 list_del_rcu(&hard_iface->list);
818 batadv_hardif_remove_interface(hard_iface);
819 break;
820 case NETDEV_CHANGEMTU:
821 if (hard_iface->soft_iface)
822 batadv_update_min_mtu(hard_iface->soft_iface);
823 break;
824 case NETDEV_CHANGEADDR:
825 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
826 goto hardif_put;
828 batadv_check_known_mac_addr(hard_iface->net_dev);
830 bat_priv = netdev_priv(hard_iface->soft_iface);
831 bat_priv->algo_ops->iface.update_mac(hard_iface);
833 primary_if = batadv_primary_if_get_selected(bat_priv);
834 if (!primary_if)
835 goto hardif_put;
837 if (hard_iface == primary_if)
838 batadv_primary_if_update_addr(bat_priv, NULL);
839 break;
840 case NETDEV_CHANGENAME:
841 batadv_debugfs_rename_hardif(hard_iface);
842 break;
843 default:
844 break;
847 hardif_put:
848 batadv_hardif_put(hard_iface);
849 out:
850 if (primary_if)
851 batadv_hardif_put(primary_if);
852 return NOTIFY_DONE;
855 struct notifier_block batadv_hard_if_notifier = {
856 .notifier_call = batadv_hard_if_event,