1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  */
6 
7 #include "hard-interface.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/container_of.h>
13 #include <linux/errno.h>
14 #include <linux/gfp.h>
15 #include <linux/if.h>
16 #include <linux/if_arp.h>
17 #include <linux/if_ether.h>
18 #include <linux/kref.h>
19 #include <linux/limits.h>
20 #include <linux/list.h>
21 #include <linux/minmax.h>
22 #include <linux/mutex.h>
23 #include <linux/netdevice.h>
24 #include <linux/printk.h>
25 #include <linux/rculist.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <net/net_namespace.h>
30 #include <net/rtnetlink.h>
31 #include <uapi/linux/batadv_packet.h>
32 
33 #include "bat_v.h"
34 #include "bridge_loop_avoidance.h"
35 #include "distributed-arp-table.h"
36 #include "gateway_client.h"
37 #include "log.h"
38 #include "originator.h"
39 #include "send.h"
40 #include "soft-interface.h"
41 #include "translation-table.h"
42 
43 /**
44  * batadv_hardif_release() - release hard interface from lists and queue for
45  *  free after rcu grace period
46  * @ref: kref pointer of the hard interface
47  */
48 void batadv_hardif_release(struct kref *ref)
49 {
50 	struct batadv_hard_iface *hard_iface;
51 
52 	hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
53 	dev_put(hard_iface->net_dev);
54 
55 	kfree_rcu(hard_iface, rcu);
56 }
57 
58 /**
59  * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
60  * @net_dev: net_device to search for
61  *
62  * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
63  */
64 struct batadv_hard_iface *
65 batadv_hardif_get_by_netdev(const struct net_device *net_dev)
66 {
67 	struct batadv_hard_iface *hard_iface;
68 
69 	rcu_read_lock();
70 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
71 		if (hard_iface->net_dev == net_dev &&
72 		    kref_get_unless_zero(&hard_iface->refcount))
73 			goto out;
74 	}
75 
76 	hard_iface = NULL;
77 
78 out:
79 	rcu_read_unlock();
80 	return hard_iface;
81 }
82 
83 /**
84  * batadv_getlink_net() - return link net namespace (of use fallback)
85  * @netdev: net_device to check
86  * @fallback_net: return in case get_link_net is not available for @netdev
87  *
88  * Return: result of rtnl_link_ops->get_link_net or @fallback_net
89  */
90 static struct net *batadv_getlink_net(const struct net_device *netdev,
91 				      struct net *fallback_net)
92 {
93 	if (!netdev->rtnl_link_ops)
94 		return fallback_net;
95 
96 	if (!netdev->rtnl_link_ops->get_link_net)
97 		return fallback_net;
98 
99 	return netdev->rtnl_link_ops->get_link_net(netdev);
100 }
101 
102 /**
103  * batadv_mutual_parents() - check if two devices are each others parent
104  * @dev1: 1st net dev
105  * @net1: 1st devices netns
106  * @dev2: 2nd net dev
107  * @net2: 2nd devices netns
108  *
109  * veth devices come in pairs and each is the parent of the other!
110  *
111  * Return: true if the devices are each others parent, otherwise false
112  */
113 static bool batadv_mutual_parents(const struct net_device *dev1,
114 				  struct net *net1,
115 				  const struct net_device *dev2,
116 				  struct net *net2)
117 {
118 	int dev1_parent_iflink = dev_get_iflink(dev1);
119 	int dev2_parent_iflink = dev_get_iflink(dev2);
120 	const struct net *dev1_parent_net;
121 	const struct net *dev2_parent_net;
122 
123 	dev1_parent_net = batadv_getlink_net(dev1, net1);
124 	dev2_parent_net = batadv_getlink_net(dev2, net2);
125 
126 	if (!dev1_parent_iflink || !dev2_parent_iflink)
127 		return false;
128 
129 	return (dev1_parent_iflink == dev2->ifindex) &&
130 	       (dev2_parent_iflink == dev1->ifindex) &&
131 	       net_eq(dev1_parent_net, net2) &&
132 	       net_eq(dev2_parent_net, net1);
133 }
134 
135 /**
136  * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
137  * @net_dev: the device to check
138  *
139  * If the user creates any virtual device on top of a batman-adv interface, it
140  * is important to prevent this new interface from being used to create a new
141  * mesh network (this behaviour would lead to a batman-over-batman
142  * configuration). This function recursively checks all the fathers of the
143  * device passed as argument looking for a batman-adv soft interface.
144  *
145  * Return: true if the device is descendant of a batman-adv mesh interface (or
146  * if it is a batman-adv interface itself), false otherwise
147  */
148 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
149 {
150 	struct net *net = dev_net(net_dev);
151 	struct net_device *parent_dev;
152 	struct net *parent_net;
153 	int iflink;
154 	bool ret;
155 
156 	/* check if this is a batman-adv mesh interface */
157 	if (batadv_softif_is_valid(net_dev))
158 		return true;
159 
160 	iflink = dev_get_iflink(net_dev);
161 	if (iflink == 0)
162 		return false;
163 
164 	parent_net = batadv_getlink_net(net_dev, net);
165 
166 	/* iflink to itself, most likely physical device */
167 	if (net == parent_net && iflink == net_dev->ifindex)
168 		return false;
169 
170 	/* recurse over the parent device */
171 	parent_dev = __dev_get_by_index((struct net *)parent_net, iflink);
172 	if (!parent_dev) {
173 		pr_warn("Cannot find parent device. Skipping batadv-on-batadv check for %s\n",
174 			net_dev->name);
175 		return false;
176 	}
177 
178 	if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
179 		return false;
180 
181 	ret = batadv_is_on_batman_iface(parent_dev);
182 
183 	return ret;
184 }
185 
186 static bool batadv_is_valid_iface(const struct net_device *net_dev)
187 {
188 	if (net_dev->flags & IFF_LOOPBACK)
189 		return false;
190 
191 	if (net_dev->type != ARPHRD_ETHER)
192 		return false;
193 
194 	if (net_dev->addr_len != ETH_ALEN)
195 		return false;
196 
197 	/* no batman over batman */
198 	if (batadv_is_on_batman_iface(net_dev))
199 		return false;
200 
201 	return true;
202 }
203 
204 /**
205  * batadv_get_real_netdevice() - check if the given netdev struct is a virtual
206  *  interface on top of another 'real' interface
207  * @netdev: the device to check
208  *
209  * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
210  * instead of this.
211  *
212  * Return: the 'real' net device or the original net device and NULL in case
213  *  of an error.
214  */
215 static struct net_device *batadv_get_real_netdevice(struct net_device *netdev)
216 {
217 	struct batadv_hard_iface *hard_iface = NULL;
218 	struct net_device *real_netdev = NULL;
219 	struct net *real_net;
220 	struct net *net;
221 	int iflink;
222 
223 	ASSERT_RTNL();
224 
225 	if (!netdev)
226 		return NULL;
227 
228 	iflink = dev_get_iflink(netdev);
229 	if (iflink == 0) {
230 		dev_hold(netdev);
231 		return netdev;
232 	}
233 
234 	hard_iface = batadv_hardif_get_by_netdev(netdev);
235 	if (!hard_iface || !hard_iface->soft_iface)
236 		goto out;
237 
238 	net = dev_net(hard_iface->soft_iface);
239 	real_net = batadv_getlink_net(netdev, net);
240 
241 	/* iflink to itself, most likely physical device */
242 	if (net == real_net && netdev->ifindex == iflink) {
243 		real_netdev = netdev;
244 		dev_hold(real_netdev);
245 		goto out;
246 	}
247 
248 	real_netdev = dev_get_by_index(real_net, iflink);
249 
250 out:
251 	batadv_hardif_put(hard_iface);
252 	return real_netdev;
253 }
254 
255 /**
256  * batadv_get_real_netdev() - check if the given net_device struct is a virtual
257  *  interface on top of another 'real' interface
258  * @net_device: the device to check
259  *
260  * Return: the 'real' net device or the original net device and NULL in case
261  *  of an error.
262  */
263 struct net_device *batadv_get_real_netdev(struct net_device *net_device)
264 {
265 	struct net_device *real_netdev;
266 
267 	rtnl_lock();
268 	real_netdev = batadv_get_real_netdevice(net_device);
269 	rtnl_unlock();
270 
271 	return real_netdev;
272 }
273 
274 /**
275  * batadv_is_wext_netdev() - check if the given net_device struct is a
276  *  wext wifi interface
277  * @net_device: the device to check
278  *
279  * Return: true if the net device is a wext wireless device, false
280  *  otherwise.
281  */
282 static bool batadv_is_wext_netdev(struct net_device *net_device)
283 {
284 	if (!net_device)
285 		return false;
286 
287 #ifdef CONFIG_WIRELESS_EXT
288 	/* pre-cfg80211 drivers have to implement WEXT, so it is possible to
289 	 * check for wireless_handlers != NULL
290 	 */
291 	if (net_device->wireless_handlers)
292 		return true;
293 #endif
294 
295 	return false;
296 }
297 
298 /**
299  * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
300  *  cfg80211 wifi interface
301  * @net_device: the device to check
302  *
303  * Return: true if the net device is a cfg80211 wireless device, false
304  *  otherwise.
305  */
306 static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
307 {
308 	if (!net_device)
309 		return false;
310 
311 #if IS_ENABLED(CONFIG_CFG80211)
312 	/* cfg80211 drivers have to set ieee80211_ptr */
313 	if (net_device->ieee80211_ptr)
314 		return true;
315 #endif
316 
317 	return false;
318 }
319 
320 /**
321  * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
322  * @net_device: the device to check
323  *
324  * Return: batadv_hard_iface_wifi_flags flags of the device
325  */
326 static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
327 {
328 	u32 wifi_flags = 0;
329 	struct net_device *real_netdev;
330 
331 	if (batadv_is_wext_netdev(net_device))
332 		wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
333 
334 	if (batadv_is_cfg80211_netdev(net_device))
335 		wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
336 
337 	real_netdev = batadv_get_real_netdevice(net_device);
338 	if (!real_netdev)
339 		return wifi_flags;
340 
341 	if (real_netdev == net_device)
342 		goto out;
343 
344 	if (batadv_is_wext_netdev(real_netdev))
345 		wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
346 
347 	if (batadv_is_cfg80211_netdev(real_netdev))
348 		wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
349 
350 out:
351 	dev_put(real_netdev);
352 	return wifi_flags;
353 }
354 
355 /**
356  * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi
357  *  interface
358  * @hard_iface: the device to check
359  *
360  * Return: true if the net device is a cfg80211 wireless device, false
361  *  otherwise.
362  */
363 bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface)
364 {
365 	u32 allowed_flags = 0;
366 
367 	allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
368 	allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
369 
370 	return !!(hard_iface->wifi_flags & allowed_flags);
371 }
372 
373 /**
374  * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
375  * @hard_iface: the device to check
376  *
377  * Return: true if the net device is a 802.11 wireless device, false otherwise.
378  */
379 bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
380 {
381 	if (!hard_iface)
382 		return false;
383 
384 	return hard_iface->wifi_flags != 0;
385 }
386 
387 /**
388  * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
389  * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
390  * @orig_addr: the originator of this packet
391  * @orig_neigh: originator address of the forwarder we just got the packet from
392  *  (NULL if we originated)
393  *
394  * Checks whether a packet needs to be (re)broadcasted on the given interface.
395  *
396  * Return:
397  *	BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
398  *	BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
399  *	BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
400  *	BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
401  */
402 int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
403 			       u8 *orig_addr, u8 *orig_neigh)
404 {
405 	struct batadv_hardif_neigh_node *hardif_neigh;
406 	struct hlist_node *first;
407 	int ret = BATADV_HARDIF_BCAST_OK;
408 
409 	rcu_read_lock();
410 
411 	/* 0 neighbors -> no (re)broadcast */
412 	first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
413 	if (!first) {
414 		ret = BATADV_HARDIF_BCAST_NORECIPIENT;
415 		goto out;
416 	}
417 
418 	/* >1 neighbors -> (re)broadcast */
419 	if (rcu_dereference(hlist_next_rcu(first)))
420 		goto out;
421 
422 	hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
423 				   list);
424 
425 	/* 1 neighbor, is the originator -> no rebroadcast */
426 	if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
427 		ret = BATADV_HARDIF_BCAST_DUPORIG;
428 	/* 1 neighbor, is the one we received from -> no rebroadcast */
429 	} else if (orig_neigh &&
430 		   batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
431 		ret = BATADV_HARDIF_BCAST_DUPFWD;
432 	}
433 
434 out:
435 	rcu_read_unlock();
436 	return ret;
437 }
438 
439 static struct batadv_hard_iface *
440 batadv_hardif_get_active(const struct net_device *soft_iface)
441 {
442 	struct batadv_hard_iface *hard_iface;
443 
444 	rcu_read_lock();
445 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
446 		if (hard_iface->soft_iface != soft_iface)
447 			continue;
448 
449 		if (hard_iface->if_status == BATADV_IF_ACTIVE &&
450 		    kref_get_unless_zero(&hard_iface->refcount))
451 			goto out;
452 	}
453 
454 	hard_iface = NULL;
455 
456 out:
457 	rcu_read_unlock();
458 	return hard_iface;
459 }
460 
461 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
462 					  struct batadv_hard_iface *oldif)
463 {
464 	struct batadv_hard_iface *primary_if;
465 
466 	primary_if = batadv_primary_if_get_selected(bat_priv);
467 	if (!primary_if)
468 		goto out;
469 
470 	batadv_dat_init_own_addr(bat_priv, primary_if);
471 	batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
472 out:
473 	batadv_hardif_put(primary_if);
474 }
475 
476 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
477 				     struct batadv_hard_iface *new_hard_iface)
478 {
479 	struct batadv_hard_iface *curr_hard_iface;
480 
481 	ASSERT_RTNL();
482 
483 	if (new_hard_iface)
484 		kref_get(&new_hard_iface->refcount);
485 
486 	curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if,
487 					      new_hard_iface, 1);
488 
489 	if (!new_hard_iface)
490 		goto out;
491 
492 	bat_priv->algo_ops->iface.primary_set(new_hard_iface);
493 	batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
494 
495 out:
496 	batadv_hardif_put(curr_hard_iface);
497 }
498 
499 static bool
500 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
501 {
502 	if (hard_iface->net_dev->flags & IFF_UP)
503 		return true;
504 
505 	return false;
506 }
507 
508 static void batadv_check_known_mac_addr(const struct net_device *net_dev)
509 {
510 	const struct batadv_hard_iface *hard_iface;
511 
512 	rcu_read_lock();
513 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
514 		if (hard_iface->if_status != BATADV_IF_ACTIVE &&
515 		    hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
516 			continue;
517 
518 		if (hard_iface->net_dev == net_dev)
519 			continue;
520 
521 		if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
522 					net_dev->dev_addr))
523 			continue;
524 
525 		pr_warn("The newly added mac address (%pM) already exists on: %s\n",
526 			net_dev->dev_addr, hard_iface->net_dev->name);
527 		pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
528 	}
529 	rcu_read_unlock();
530 }
531 
532 /**
533  * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
534  * @soft_iface: netdev struct of the mesh interface
535  */
536 static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
537 {
538 	const struct batadv_hard_iface *hard_iface;
539 	unsigned short lower_header_len = ETH_HLEN;
540 	unsigned short lower_headroom = 0;
541 	unsigned short lower_tailroom = 0;
542 	unsigned short needed_headroom;
543 
544 	rcu_read_lock();
545 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
546 		if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
547 			continue;
548 
549 		if (hard_iface->soft_iface != soft_iface)
550 			continue;
551 
552 		lower_header_len = max_t(unsigned short, lower_header_len,
553 					 hard_iface->net_dev->hard_header_len);
554 
555 		lower_headroom = max_t(unsigned short, lower_headroom,
556 				       hard_iface->net_dev->needed_headroom);
557 
558 		lower_tailroom = max_t(unsigned short, lower_tailroom,
559 				       hard_iface->net_dev->needed_tailroom);
560 	}
561 	rcu_read_unlock();
562 
563 	needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
564 	needed_headroom += batadv_max_header_len();
565 
566 	/* fragmentation headers don't strip the unicast/... header */
567 	needed_headroom += sizeof(struct batadv_frag_packet);
568 
569 	soft_iface->needed_headroom = needed_headroom;
570 	soft_iface->needed_tailroom = lower_tailroom;
571 }
572 
573 /**
574  * batadv_hardif_min_mtu() - Calculate maximum MTU for soft interface
575  * @soft_iface: netdev struct of the soft interface
576  *
577  * Return: MTU for the soft-interface (limited by the minimal MTU of all active
578  *  slave interfaces)
579  */
580 int batadv_hardif_min_mtu(struct net_device *soft_iface)
581 {
582 	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
583 	const struct batadv_hard_iface *hard_iface;
584 	int min_mtu = INT_MAX;
585 
586 	rcu_read_lock();
587 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
588 		if (hard_iface->if_status != BATADV_IF_ACTIVE &&
589 		    hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
590 			continue;
591 
592 		if (hard_iface->soft_iface != soft_iface)
593 			continue;
594 
595 		min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
596 	}
597 	rcu_read_unlock();
598 
599 	if (atomic_read(&bat_priv->fragmentation) == 0)
600 		goto out;
601 
602 	/* with fragmentation enabled the maximum size of internally generated
603 	 * packets such as translation table exchanges or tvlv containers, etc
604 	 * has to be calculated
605 	 */
606 	min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
607 	min_mtu -= sizeof(struct batadv_frag_packet);
608 	min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
609 
610 out:
611 	/* report to the other components the maximum amount of bytes that
612 	 * batman-adv can send over the wire (without considering the payload
613 	 * overhead). For example, this value is used by TT to compute the
614 	 * maximum local table size
615 	 */
616 	atomic_set(&bat_priv->packet_size_max, min_mtu);
617 
618 	/* the real soft-interface MTU is computed by removing the payload
619 	 * overhead from the maximum amount of bytes that was just computed.
620 	 *
621 	 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
622 	 */
623 	return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
624 }
625 
626 /**
627  * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
628  *  MTU appeared
629  * @soft_iface: netdev struct of the soft interface
630  */
631 void batadv_update_min_mtu(struct net_device *soft_iface)
632 {
633 	soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
634 
635 	/* Check if the local translate table should be cleaned up to match a
636 	 * new (and smaller) MTU.
637 	 */
638 	batadv_tt_local_resize_to_mtu(soft_iface);
639 }
640 
641 static void
642 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
643 {
644 	struct batadv_priv *bat_priv;
645 	struct batadv_hard_iface *primary_if = NULL;
646 
647 	if (hard_iface->if_status != BATADV_IF_INACTIVE)
648 		goto out;
649 
650 	bat_priv = netdev_priv(hard_iface->soft_iface);
651 
652 	bat_priv->algo_ops->iface.update_mac(hard_iface);
653 	hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
654 
655 	/* the first active interface becomes our primary interface or
656 	 * the next active interface after the old primary interface was removed
657 	 */
658 	primary_if = batadv_primary_if_get_selected(bat_priv);
659 	if (!primary_if)
660 		batadv_primary_if_select(bat_priv, hard_iface);
661 
662 	batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
663 		    hard_iface->net_dev->name);
664 
665 	batadv_update_min_mtu(hard_iface->soft_iface);
666 
667 	if (bat_priv->algo_ops->iface.activate)
668 		bat_priv->algo_ops->iface.activate(hard_iface);
669 
670 out:
671 	batadv_hardif_put(primary_if);
672 }
673 
674 static void
675 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
676 {
677 	if (hard_iface->if_status != BATADV_IF_ACTIVE &&
678 	    hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
679 		return;
680 
681 	hard_iface->if_status = BATADV_IF_INACTIVE;
682 
683 	batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
684 		    hard_iface->net_dev->name);
685 
686 	batadv_update_min_mtu(hard_iface->soft_iface);
687 }
688 
689 /**
690  * batadv_hardif_enable_interface() - Enslave hard interface to soft interface
691  * @hard_iface: hard interface to add to soft interface
692  * @soft_iface: netdev struct of the mesh interface
693  *
694  * Return: 0 on success or negative error number in case of failure
695  */
696 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
697 				   struct net_device *soft_iface)
698 {
699 	struct batadv_priv *bat_priv;
700 	__be16 ethertype = htons(ETH_P_BATMAN);
701 	int max_header_len = batadv_max_header_len();
702 	int ret;
703 
704 	if (hard_iface->net_dev->mtu < ETH_MIN_MTU + max_header_len)
705 		return -EINVAL;
706 
707 	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
708 		goto out;
709 
710 	kref_get(&hard_iface->refcount);
711 
712 	dev_hold(soft_iface);
713 	hard_iface->soft_iface = soft_iface;
714 	bat_priv = netdev_priv(hard_iface->soft_iface);
715 
716 	ret = netdev_master_upper_dev_link(hard_iface->net_dev,
717 					   soft_iface, NULL, NULL, NULL);
718 	if (ret)
719 		goto err_dev;
720 
721 	ret = bat_priv->algo_ops->iface.enable(hard_iface);
722 	if (ret < 0)
723 		goto err_upper;
724 
725 	hard_iface->if_status = BATADV_IF_INACTIVE;
726 
727 	kref_get(&hard_iface->refcount);
728 	hard_iface->batman_adv_ptype.type = ethertype;
729 	hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
730 	hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
731 	dev_add_pack(&hard_iface->batman_adv_ptype);
732 
733 	batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
734 		    hard_iface->net_dev->name);
735 
736 	if (atomic_read(&bat_priv->fragmentation) &&
737 	    hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
738 		batadv_info(hard_iface->soft_iface,
739 			    "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",
740 			    hard_iface->net_dev->name, hard_iface->net_dev->mtu,
741 			    ETH_DATA_LEN + max_header_len);
742 
743 	if (!atomic_read(&bat_priv->fragmentation) &&
744 	    hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
745 		batadv_info(hard_iface->soft_iface,
746 			    "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",
747 			    hard_iface->net_dev->name, hard_iface->net_dev->mtu,
748 			    ETH_DATA_LEN + max_header_len);
749 
750 	if (batadv_hardif_is_iface_up(hard_iface))
751 		batadv_hardif_activate_interface(hard_iface);
752 	else
753 		batadv_err(hard_iface->soft_iface,
754 			   "Not using interface %s (retrying later): interface not active\n",
755 			   hard_iface->net_dev->name);
756 
757 	batadv_hardif_recalc_extra_skbroom(soft_iface);
758 
759 	if (bat_priv->algo_ops->iface.enabled)
760 		bat_priv->algo_ops->iface.enabled(hard_iface);
761 
762 out:
763 	return 0;
764 
765 err_upper:
766 	netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
767 err_dev:
768 	hard_iface->soft_iface = NULL;
769 	dev_put(soft_iface);
770 	batadv_hardif_put(hard_iface);
771 	return ret;
772 }
773 
774 /**
775  * batadv_hardif_cnt() - get number of interfaces enslaved to soft interface
776  * @soft_iface: soft interface to check
777  *
778  * This function is only using RCU for locking - the result can therefore be
779  * off when another function is modifying the list at the same time. The
780  * caller can use the rtnl_lock to make sure that the count is accurate.
781  *
782  * Return: number of connected/enslaved hard interfaces
783  */
784 static size_t batadv_hardif_cnt(const struct net_device *soft_iface)
785 {
786 	struct batadv_hard_iface *hard_iface;
787 	size_t count = 0;
788 
789 	rcu_read_lock();
790 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
791 		if (hard_iface->soft_iface != soft_iface)
792 			continue;
793 
794 		count++;
795 	}
796 	rcu_read_unlock();
797 
798 	return count;
799 }
800 
801 /**
802  * batadv_hardif_disable_interface() - Remove hard interface from soft interface
803  * @hard_iface: hard interface to be removed
804  */
805 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
806 {
807 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
808 	struct batadv_hard_iface *primary_if = NULL;
809 
810 	batadv_hardif_deactivate_interface(hard_iface);
811 
812 	if (hard_iface->if_status != BATADV_IF_INACTIVE)
813 		goto out;
814 
815 	batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
816 		    hard_iface->net_dev->name);
817 	dev_remove_pack(&hard_iface->batman_adv_ptype);
818 	batadv_hardif_put(hard_iface);
819 
820 	primary_if = batadv_primary_if_get_selected(bat_priv);
821 	if (hard_iface == primary_if) {
822 		struct batadv_hard_iface *new_if;
823 
824 		new_if = batadv_hardif_get_active(hard_iface->soft_iface);
825 		batadv_primary_if_select(bat_priv, new_if);
826 
827 		batadv_hardif_put(new_if);
828 	}
829 
830 	bat_priv->algo_ops->iface.disable(hard_iface);
831 	hard_iface->if_status = BATADV_IF_NOT_IN_USE;
832 
833 	/* delete all references to this hard_iface */
834 	batadv_purge_orig_ref(bat_priv);
835 	batadv_purge_outstanding_packets(bat_priv, hard_iface);
836 	dev_put(hard_iface->soft_iface);
837 
838 	netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
839 	batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
840 
841 	/* nobody uses this interface anymore */
842 	if (batadv_hardif_cnt(hard_iface->soft_iface) <= 1)
843 		batadv_gw_check_client_stop(bat_priv);
844 
845 	hard_iface->soft_iface = NULL;
846 	batadv_hardif_put(hard_iface);
847 
848 out:
849 	batadv_hardif_put(primary_if);
850 }
851 
852 static struct batadv_hard_iface *
853 batadv_hardif_add_interface(struct net_device *net_dev)
854 {
855 	struct batadv_hard_iface *hard_iface;
856 
857 	ASSERT_RTNL();
858 
859 	if (!batadv_is_valid_iface(net_dev))
860 		goto out;
861 
862 	dev_hold(net_dev);
863 
864 	hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
865 	if (!hard_iface)
866 		goto release_dev;
867 
868 	hard_iface->net_dev = net_dev;
869 	hard_iface->soft_iface = NULL;
870 	hard_iface->if_status = BATADV_IF_NOT_IN_USE;
871 
872 	INIT_LIST_HEAD(&hard_iface->list);
873 	INIT_HLIST_HEAD(&hard_iface->neigh_list);
874 
875 	mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
876 	spin_lock_init(&hard_iface->neigh_list_lock);
877 	kref_init(&hard_iface->refcount);
878 
879 	hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
880 	hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
881 	if (batadv_is_wifi_hardif(hard_iface))
882 		hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
883 
884 	atomic_set(&hard_iface->hop_penalty, 0);
885 
886 	batadv_v_hardif_init(hard_iface);
887 
888 	batadv_check_known_mac_addr(hard_iface->net_dev);
889 	kref_get(&hard_iface->refcount);
890 	list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
891 	batadv_hardif_generation++;
892 
893 	return hard_iface;
894 
895 release_dev:
896 	dev_put(net_dev);
897 out:
898 	return NULL;
899 }
900 
901 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
902 {
903 	ASSERT_RTNL();
904 
905 	/* first deactivate interface */
906 	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
907 		batadv_hardif_disable_interface(hard_iface);
908 
909 	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
910 		return;
911 
912 	hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
913 	batadv_hardif_put(hard_iface);
914 }
915 
916 /**
917  * batadv_hard_if_event_softif() - Handle events for soft interfaces
918  * @event: NETDEV_* event to handle
919  * @net_dev: net_device which generated an event
920  *
921  * Return: NOTIFY_* result
922  */
923 static int batadv_hard_if_event_softif(unsigned long event,
924 				       struct net_device *net_dev)
925 {
926 	struct batadv_priv *bat_priv;
927 
928 	switch (event) {
929 	case NETDEV_REGISTER:
930 		bat_priv = netdev_priv(net_dev);
931 		batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
932 		break;
933 	}
934 
935 	return NOTIFY_DONE;
936 }
937 
938 static int batadv_hard_if_event(struct notifier_block *this,
939 				unsigned long event, void *ptr)
940 {
941 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
942 	struct batadv_hard_iface *hard_iface;
943 	struct batadv_hard_iface *primary_if = NULL;
944 	struct batadv_priv *bat_priv;
945 
946 	if (batadv_softif_is_valid(net_dev))
947 		return batadv_hard_if_event_softif(event, net_dev);
948 
949 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
950 	if (!hard_iface && (event == NETDEV_REGISTER ||
951 			    event == NETDEV_POST_TYPE_CHANGE))
952 		hard_iface = batadv_hardif_add_interface(net_dev);
953 
954 	if (!hard_iface)
955 		goto out;
956 
957 	switch (event) {
958 	case NETDEV_UP:
959 		batadv_hardif_activate_interface(hard_iface);
960 		break;
961 	case NETDEV_GOING_DOWN:
962 	case NETDEV_DOWN:
963 		batadv_hardif_deactivate_interface(hard_iface);
964 		break;
965 	case NETDEV_UNREGISTER:
966 	case NETDEV_PRE_TYPE_CHANGE:
967 		list_del_rcu(&hard_iface->list);
968 		batadv_hardif_generation++;
969 
970 		batadv_hardif_remove_interface(hard_iface);
971 		break;
972 	case NETDEV_CHANGEMTU:
973 		if (hard_iface->soft_iface)
974 			batadv_update_min_mtu(hard_iface->soft_iface);
975 		break;
976 	case NETDEV_CHANGEADDR:
977 		if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
978 			goto hardif_put;
979 
980 		batadv_check_known_mac_addr(hard_iface->net_dev);
981 
982 		bat_priv = netdev_priv(hard_iface->soft_iface);
983 		bat_priv->algo_ops->iface.update_mac(hard_iface);
984 
985 		primary_if = batadv_primary_if_get_selected(bat_priv);
986 		if (!primary_if)
987 			goto hardif_put;
988 
989 		if (hard_iface == primary_if)
990 			batadv_primary_if_update_addr(bat_priv, NULL);
991 		break;
992 	case NETDEV_CHANGEUPPER:
993 		hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev);
994 		if (batadv_is_wifi_hardif(hard_iface))
995 			hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
996 		break;
997 	default:
998 		break;
999 	}
1000 
1001 hardif_put:
1002 	batadv_hardif_put(hard_iface);
1003 out:
1004 	batadv_hardif_put(primary_if);
1005 	return NOTIFY_DONE;
1006 }
1007 
1008 struct notifier_block batadv_hard_if_notifier = {
1009 	.notifier_call = batadv_hard_if_event,
1010 };
1011