xref: /linux/net/batman-adv/hard-interface.c (revision c36461825469a9ceee2346a2e89286c522525da7)
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/bug.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/compiler.h>
13 #include <linux/container_of.h>
14 #include <linux/errno.h>
15 #include <linux/gfp.h>
16 #include <linux/if.h>
17 #include <linux/if_arp.h>
18 #include <linux/if_ether.h>
19 #include <linux/kref.h>
20 #include <linux/limits.h>
21 #include <linux/list.h>
22 #include <linux/minmax.h>
23 #include <linux/mutex.h>
24 #include <linux/netdevice.h>
25 #include <linux/notifier.h>
26 #include <linux/printk.h>
27 #include <linux/rculist.h>
28 #include <linux/rhashtable-types.h>
29 #include <linux/rhashtable.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <net/net_namespace.h>
34 #include <net/rtnetlink.h>
35 #include <uapi/linux/batadv_packet.h>
36 
37 #include "bat_v.h"
38 #include "bridge_loop_avoidance.h"
39 #include "distributed-arp-table.h"
40 #include "gateway_client.h"
41 #include "log.h"
42 #include "mesh-interface.h"
43 #include "originator.h"
44 #include "send.h"
45 #include "translation-table.h"
46 
47 static const struct rhashtable_params batadv_wifi_net_devices_params = {
48 	.key_len		= sizeof(struct net_device *),
49 	.key_offset		= offsetof(struct batadv_wifi_net_device_state, netdev),
50 	.head_offset		= offsetof(struct batadv_wifi_net_device_state, l),
51 	.automatic_shrinking	= true,
52 };
53 
54 static struct rhashtable batadv_wifi_net_devices;
55 
56 /**
57  * batadv_hardif_release() - release hard interface from lists and queue for
58  *  free after rcu grace period
59  * @ref: kref pointer of the hard interface
60  */
61 void batadv_hardif_release(struct kref *ref)
62 {
63 	struct batadv_hard_iface *hard_iface;
64 
65 	hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
66 	netdev_put(hard_iface->mesh_iface, &hard_iface->meshif_dev_tracker);
67 	netdev_put(hard_iface->net_dev, &hard_iface->dev_tracker);
68 
69 	kfree_rcu(hard_iface, rcu);
70 }
71 
72 /**
73  * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device
74  * @net_dev: net_device to search for
75  *
76  * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors
77  */
78 struct batadv_hard_iface *
79 batadv_hardif_get_by_netdev(struct net_device *net_dev)
80 {
81 	struct batadv_hard_iface *hard_iface;
82 	struct net_device *mesh_iface;
83 
84 	ASSERT_RTNL();
85 
86 	mesh_iface = netdev_master_upper_dev_get(net_dev);
87 	if (!mesh_iface || !batadv_meshif_is_valid(mesh_iface))
88 		return NULL;
89 
90 	hard_iface = netdev_lower_dev_get_private(mesh_iface, net_dev);
91 	if (!kref_get_unless_zero(&hard_iface->refcount))
92 		return NULL;
93 
94 	return hard_iface;
95 }
96 
97 /**
98  * batadv_getlink_net() - return link net namespace (of use fallback)
99  * @netdev: net_device to check
100  * @fallback_net: return in case get_link_net is not available for @netdev
101  *
102  * Return: result of rtnl_link_ops->get_link_net or @fallback_net
103  */
104 static struct net *batadv_getlink_net(const struct net_device *netdev,
105 				      struct net *fallback_net)
106 {
107 	if (!netdev->rtnl_link_ops)
108 		return fallback_net;
109 
110 	if (!netdev->rtnl_link_ops->get_link_net)
111 		return fallback_net;
112 
113 	return netdev->rtnl_link_ops->get_link_net(netdev);
114 }
115 
116 /**
117  * batadv_mutual_parents() - check if two devices are each others parent
118  * @dev1: 1st net dev
119  * @net1: 1st devices netns
120  * @dev2: 2nd net dev
121  * @net2: 2nd devices netns
122  *
123  * veth devices come in pairs and each is the parent of the other!
124  *
125  * Return: true if the devices are each others parent, otherwise false
126  */
127 static bool batadv_mutual_parents(const struct net_device *dev1,
128 				  struct net *net1,
129 				  const struct net_device *dev2,
130 				  struct net *net2)
131 {
132 	int dev1_parent_iflink = dev_get_iflink(dev1);
133 	int dev2_parent_iflink = dev_get_iflink(dev2);
134 	const struct net *dev1_parent_net;
135 	const struct net *dev2_parent_net;
136 
137 	dev1_parent_net = batadv_getlink_net(dev1, net1);
138 	dev2_parent_net = batadv_getlink_net(dev2, net2);
139 
140 	if (!dev1_parent_iflink || !dev2_parent_iflink)
141 		return false;
142 
143 	return (dev1_parent_iflink == dev2->ifindex) &&
144 	       (dev2_parent_iflink == dev1->ifindex) &&
145 	       net_eq(dev1_parent_net, net2) &&
146 	       net_eq(dev2_parent_net, net1);
147 }
148 
149 /**
150  * batadv_is_on_batman_iface() - check if a device is a batman iface descendant
151  * @net_dev: the device to check
152  *
153  * If the user creates any virtual device on top of a batman-adv interface, it
154  * is important to prevent this new interface from being used to create a new
155  * mesh network (this behaviour would lead to a batman-over-batman
156  * configuration). This function recursively checks all the fathers of the
157  * device passed as argument looking for a batman-adv mesh interface.
158  *
159  * Return: true if the device is descendant of a batman-adv mesh interface (or
160  * if it is a batman-adv interface itself), false otherwise
161  */
162 static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
163 {
164 	struct net *net = dev_net(net_dev);
165 	struct net_device *parent_dev;
166 	struct net *parent_net;
167 	int iflink;
168 	bool ret;
169 
170 	/* check if this is a batman-adv mesh interface */
171 	if (batadv_meshif_is_valid(net_dev))
172 		return true;
173 
174 	iflink = dev_get_iflink(net_dev);
175 	if (iflink == 0)
176 		return false;
177 
178 	parent_net = batadv_getlink_net(net_dev, net);
179 
180 	/* iflink to itself, most likely physical device */
181 	if (net == parent_net && iflink == net_dev->ifindex)
182 		return false;
183 
184 	/* recurse over the parent device */
185 	parent_dev = __dev_get_by_index((struct net *)parent_net, iflink);
186 	if (!parent_dev) {
187 		pr_warn("Cannot find parent device. Skipping batadv-on-batadv check for %s\n",
188 			net_dev->name);
189 		return false;
190 	}
191 
192 	if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
193 		return false;
194 
195 	ret = batadv_is_on_batman_iface(parent_dev);
196 
197 	return ret;
198 }
199 
200 /**
201  * batadv_is_valid_iface() - check whether a net_device can be used as a hard
202  *  interface
203  * @net_dev: the net_device to check
204  *
205  * Refuse loopback devices, non-Ethernet devices, devices with a non-Ethernet
206  * address length and any device that is already part of a batman-adv mesh
207  * interface stack.
208  *
209  * Return: true if @net_dev can be used as a hard interface, false otherwise
210  */
211 static bool batadv_is_valid_iface(const struct net_device *net_dev)
212 {
213 	if (net_dev->flags & IFF_LOOPBACK)
214 		return false;
215 
216 	if (net_dev->type != ARPHRD_ETHER)
217 		return false;
218 
219 	if (net_dev->addr_len != ETH_ALEN)
220 		return false;
221 
222 	/* no batman over batman */
223 	if (batadv_is_on_batman_iface(net_dev))
224 		return false;
225 
226 	return true;
227 }
228 
229 /**
230  * __batadv_get_real_netdev() - check if the given netdev struct is a virtual
231  *  interface on top of another 'real' interface
232  * @netdev: the device to check
233  *
234  * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev()
235  * instead of this.
236  *
237  * Return: the 'real' net device or the original net device and NULL in case
238  *  of an error.
239  */
240 struct net_device *__batadv_get_real_netdev(struct net_device *netdev)
241 {
242 	struct batadv_hard_iface *hard_iface = NULL;
243 	struct net_device *real_netdev = NULL;
244 	struct net *real_net;
245 	struct net *net;
246 	int iflink;
247 
248 	ASSERT_RTNL();
249 
250 	if (!netdev)
251 		return NULL;
252 
253 	iflink = dev_get_iflink(netdev);
254 	if (iflink == 0) {
255 		dev_hold(netdev);
256 		return netdev;
257 	}
258 
259 	hard_iface = batadv_hardif_get_by_netdev(netdev);
260 	if (!hard_iface)
261 		goto out;
262 
263 	net = dev_net(hard_iface->mesh_iface);
264 	real_net = batadv_getlink_net(netdev, net);
265 
266 	/* iflink to itself, most likely physical device */
267 	if (net == real_net && netdev->ifindex == iflink) {
268 		real_netdev = netdev;
269 		dev_hold(real_netdev);
270 		goto out;
271 	}
272 
273 	real_netdev = dev_get_by_index(real_net, iflink);
274 
275 out:
276 	batadv_hardif_put(hard_iface);
277 	return real_netdev;
278 }
279 
280 /**
281  * batadv_get_real_netdev() - check if the given net_device struct is a virtual
282  *  interface on top of another 'real' interface
283  * @net_device: the device to check
284  *
285  * Return: the 'real' net device or the original net device and NULL in case
286  *  of an error.
287  */
288 struct net_device *batadv_get_real_netdev(struct net_device *net_device)
289 {
290 	struct net_device *real_netdev;
291 
292 	rtnl_lock();
293 	real_netdev = __batadv_get_real_netdev(net_device);
294 	rtnl_unlock();
295 
296 	return real_netdev;
297 }
298 
299 /**
300  * batadv_is_wext_netdev() - check if the given net_device struct is a
301  *  wext wifi interface
302  * @net_device: the device to check
303  *
304  * Return: true if the net device is a wext wireless device, false
305  *  otherwise.
306  */
307 static bool batadv_is_wext_netdev(struct net_device *net_device)
308 {
309 	if (!net_device)
310 		return false;
311 
312 #ifdef CONFIG_WIRELESS_EXT
313 	/* pre-cfg80211 drivers have to implement WEXT, so it is possible to
314 	 * check for wireless_handlers != NULL
315 	 */
316 	if (net_device->wireless_handlers)
317 		return true;
318 #endif
319 
320 	return false;
321 }
322 
323 /**
324  * batadv_is_cfg80211_netdev() - check if the given net_device struct is a
325  *  cfg80211 wifi interface
326  * @net_device: the device to check
327  *
328  * Return: true if the net device is a cfg80211 wireless device, false
329  *  otherwise.
330  */
331 static bool batadv_is_cfg80211_netdev(struct net_device *net_device)
332 {
333 	if (!net_device)
334 		return false;
335 
336 #if IS_ENABLED(CONFIG_CFG80211)
337 	/* cfg80211 drivers have to set ieee80211_ptr */
338 	if (net_device->ieee80211_ptr)
339 		return true;
340 #endif
341 
342 	return false;
343 }
344 
345 /**
346  * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device
347  * @net_device: the device to check
348  *
349  * Return: batadv_hard_iface_wifi_flags flags of the device
350  */
351 static u32 batadv_wifi_flags_evaluate(struct net_device *net_device)
352 {
353 	struct net_device *real_netdev;
354 	u32 wifi_flags = 0;
355 
356 	if (batadv_is_wext_netdev(net_device))
357 		wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT;
358 
359 	if (batadv_is_cfg80211_netdev(net_device))
360 		wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT;
361 
362 	real_netdev = __batadv_get_real_netdev(net_device);
363 	if (!real_netdev)
364 		return wifi_flags;
365 
366 	if (real_netdev == net_device)
367 		goto out;
368 
369 	if (batadv_is_wext_netdev(real_netdev))
370 		wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT;
371 
372 	if (batadv_is_cfg80211_netdev(real_netdev))
373 		wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT;
374 
375 out:
376 	dev_put(real_netdev);
377 	return wifi_flags;
378 }
379 
380 /**
381  * batadv_netdev_get_wifi_flags() - retrieve wifi flags for net_device
382  * @net_dev: the device to check
383  *
384  * Return: batadv_hard_iface_wifi_flags flags of the device
385  */
386 u32 batadv_netdev_get_wifi_flags(struct net_device *net_dev)
387 {
388 	struct batadv_wifi_net_device_state *device_state;
389 	u32 wifi_flags = 0;
390 
391 	rcu_read_lock();
392 	device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices,
393 					      &net_dev,
394 					      batadv_wifi_net_devices_params);
395 	if (device_state)
396 		wifi_flags = READ_ONCE(device_state->wifi_flags);
397 	rcu_read_unlock();
398 
399 	return wifi_flags;
400 }
401 
402 /**
403  * batadv_hardif_get_wifi_flags() - retrieve wifi flags for hard_iface
404  * @hard_iface: the device to check
405  *
406  * Return: batadv_hard_iface_wifi_flags flags of the device
407  */
408 u32 batadv_hardif_get_wifi_flags(struct batadv_hard_iface *hard_iface)
409 {
410 	if (!hard_iface)
411 		return 0;
412 
413 	return batadv_netdev_get_wifi_flags(hard_iface->net_dev);
414 }
415 
416 /**
417  * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface
418  * @hard_iface: the device to check
419  *
420  * Return: true if the net device is a 802.11 wireless device, false otherwise.
421  */
422 bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface)
423 {
424 	u32 wifi_flags = batadv_hardif_get_wifi_flags(hard_iface);
425 
426 	return batadv_is_wifi(wifi_flags);
427 }
428 
429 /**
430  * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary
431  * @if_outgoing: the outgoing interface checked and considered for (re)broadcast
432  * @orig_addr: the originator of this packet
433  * @orig_neigh: originator address of the forwarder we just got the packet from
434  *  (NULL if we originated)
435  *
436  * Checks whether a packet needs to be (re)broadcasted on the given interface.
437  *
438  * Return:
439  *	BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface
440  *	BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder
441  *	BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator
442  *	BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast
443  */
444 int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing,
445 			       u8 *orig_addr, u8 *orig_neigh)
446 {
447 	struct batadv_hardif_neigh_node *hardif_neigh;
448 	int ret = BATADV_HARDIF_BCAST_OK;
449 	struct hlist_node *first;
450 
451 	rcu_read_lock();
452 
453 	/* 0 neighbors -> no (re)broadcast */
454 	first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list));
455 	if (!first) {
456 		ret = BATADV_HARDIF_BCAST_NORECIPIENT;
457 		goto out;
458 	}
459 
460 	/* >1 neighbors -> (re)broadcast */
461 	if (rcu_dereference(hlist_next_rcu(first)))
462 		goto out;
463 
464 	hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node,
465 				   list);
466 
467 	/* 1 neighbor, is the originator -> no rebroadcast */
468 	if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) {
469 		ret = BATADV_HARDIF_BCAST_DUPORIG;
470 	/* 1 neighbor, is the one we received from -> no rebroadcast */
471 	} else if (orig_neigh &&
472 		   batadv_compare_eth(hardif_neigh->orig, orig_neigh)) {
473 		ret = BATADV_HARDIF_BCAST_DUPFWD;
474 	}
475 
476 out:
477 	rcu_read_unlock();
478 	return ret;
479 }
480 
481 /**
482  * batadv_hardif_get_active() - retrieve an active hard interface for a mesh
483  *  interface
484  * @mesh_iface: mesh interface to search
485  *
486  * Return: first hard interface in BATADV_IF_ACTIVE state attached to
487  *  @mesh_iface, or NULL if none is active.
488  */
489 static struct batadv_hard_iface *
490 batadv_hardif_get_active(struct net_device *mesh_iface)
491 {
492 	struct batadv_hard_iface *hard_iface;
493 	struct list_head *iter;
494 
495 	rcu_read_lock();
496 	netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
497 		if (hard_iface->if_status == BATADV_IF_ACTIVE &&
498 		    kref_get_unless_zero(&hard_iface->refcount))
499 			goto out;
500 	}
501 
502 	hard_iface = NULL;
503 
504 out:
505 	rcu_read_unlock();
506 	return hard_iface;
507 }
508 
509 /**
510  * batadv_primary_if_update_addr() - propagate the new primary interface MAC
511  *  address to all dependent components
512  * @bat_priv: the bat priv with all the mesh interface information
513  * @oldif: previously used primary interface, or NULL if none
514  *
515  * Inform DAT and BLA that the originator address has changed so they can
516  * adjust their state accordingly.
517  */
518 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
519 					  struct batadv_hard_iface *oldif)
520 {
521 	struct batadv_hard_iface *primary_if;
522 
523 	primary_if = batadv_primary_if_get_selected(bat_priv);
524 	if (!primary_if)
525 		goto out;
526 
527 	batadv_dat_init_own_addr(bat_priv, primary_if);
528 	batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
529 out:
530 	batadv_hardif_put(primary_if);
531 }
532 
533 /**
534  * batadv_primary_if_select() - select the new primary interface
535  * @bat_priv: the bat priv with all the mesh interface information
536  * @new_hard_iface: new primary interface, may be NULL
537  *
538  * Replace the currently selected primary interface with @new_hard_iface,
539  * invoke the algorithm-specific primary_set hook and update the originator
540  * MAC address.
541  */
542 static void batadv_primary_if_select(struct batadv_priv *bat_priv,
543 				     struct batadv_hard_iface *new_hard_iface)
544 {
545 	struct batadv_hard_iface *curr_hard_iface;
546 
547 	ASSERT_RTNL();
548 
549 	if (new_hard_iface)
550 		kref_get(&new_hard_iface->refcount);
551 
552 	curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if,
553 					      new_hard_iface, 1);
554 
555 	if (!new_hard_iface)
556 		goto out;
557 
558 	bat_priv->algo_ops->iface.primary_set(new_hard_iface);
559 	batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
560 
561 out:
562 	batadv_hardif_put(curr_hard_iface);
563 }
564 
565 /**
566  * batadv_hardif_is_iface_up() - check whether the underlying net_device of a
567  *  hard interface is up
568  * @hard_iface: the hard interface to check
569  *
570  * Return: true if the underlying net_device has the IFF_UP flag set
571  */
572 static bool
573 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
574 {
575 	if (hard_iface->net_dev->flags & IFF_UP)
576 		return true;
577 
578 	return false;
579 }
580 
581 /**
582  * batadv_check_known_mac_addr() - warn about duplicate hard interface MAC
583  *  addresses
584  * @hard_iface: hard interface that was just added or had its MAC changed
585  *
586  * Iterate over all hard interfaces of the same mesh interface and emit a
587  * warning if another in-use interface shares the same MAC address as
588  * @hard_iface.
589  */
590 static void batadv_check_known_mac_addr(const struct batadv_hard_iface *hard_iface)
591 {
592 	struct net_device *mesh_iface = hard_iface->mesh_iface;
593 	const struct batadv_hard_iface *tmp_hard_iface;
594 	struct list_head *iter;
595 
596 	netdev_for_each_lower_private(mesh_iface, tmp_hard_iface, iter) {
597 		if (tmp_hard_iface == hard_iface)
598 			continue;
599 
600 		if (!batadv_compare_eth(tmp_hard_iface->net_dev->dev_addr,
601 					hard_iface->net_dev->dev_addr))
602 			continue;
603 
604 		pr_warn("The newly added mac address (%pM) already exists on: %s\n",
605 			hard_iface->net_dev->dev_addr, tmp_hard_iface->net_dev->name);
606 		pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
607 	}
608 }
609 
610 /**
611  * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
612  * @mesh_iface: netdev struct of the mesh interface
613  */
614 static void batadv_hardif_recalc_extra_skbroom(struct net_device *mesh_iface)
615 {
616 	const struct batadv_hard_iface *hard_iface;
617 	unsigned short lower_header_len = ETH_HLEN;
618 	unsigned short lower_headroom = 0;
619 	unsigned short lower_tailroom = 0;
620 	unsigned short needed_headroom;
621 	struct list_head *iter;
622 
623 	rcu_read_lock();
624 	netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
625 		lower_header_len = max_t(unsigned short, lower_header_len,
626 					 hard_iface->net_dev->hard_header_len);
627 
628 		lower_headroom = max_t(unsigned short, lower_headroom,
629 				       hard_iface->net_dev->needed_headroom);
630 
631 		lower_tailroom = max_t(unsigned short, lower_tailroom,
632 				       hard_iface->net_dev->needed_tailroom);
633 	}
634 	rcu_read_unlock();
635 
636 	needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
637 	needed_headroom += batadv_max_header_len();
638 
639 	/* fragmentation headers don't strip the unicast/... header */
640 	needed_headroom += sizeof(struct batadv_frag_packet);
641 
642 	mesh_iface->needed_headroom = needed_headroom;
643 	mesh_iface->needed_tailroom = lower_tailroom;
644 }
645 
646 /**
647  * batadv_hardif_min_mtu() - Calculate maximum MTU for mesh interface
648  * @mesh_iface: netdev struct of the mesh interface
649  *
650  * Return: MTU for the mesh-interface (limited by the minimal MTU of all active
651  *  slave interfaces)
652  */
653 int batadv_hardif_min_mtu(struct net_device *mesh_iface)
654 {
655 	struct batadv_priv *bat_priv = netdev_priv(mesh_iface);
656 	const struct batadv_hard_iface *hard_iface;
657 	struct list_head *iter;
658 	int min_mtu = INT_MAX;
659 
660 	rcu_read_lock();
661 	netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) {
662 		if (hard_iface->if_status != BATADV_IF_ACTIVE &&
663 		    hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
664 			continue;
665 
666 		min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
667 	}
668 	rcu_read_unlock();
669 
670 	if (READ_ONCE(bat_priv->fragmentation) == 0)
671 		goto out;
672 
673 	/* with fragmentation enabled the maximum size of internally generated
674 	 * packets such as translation table exchanges or tvlv containers, etc
675 	 * has to be calculated
676 	 */
677 	min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
678 	min_mtu -= sizeof(struct batadv_frag_packet);
679 	min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
680 
681 out:
682 	/* report to the other components the maximum amount of bytes that
683 	 * batman-adv can send over the wire (without considering the payload
684 	 * overhead). For example, this value is used by TT to compute the
685 	 * maximum local table size
686 	 */
687 	WRITE_ONCE(bat_priv->packet_size_max, min_mtu);
688 
689 	/* the real mesh-interface MTU is computed by removing the payload
690 	 * overhead from the maximum amount of bytes that was just computed.
691 	 */
692 	return min_t(int, min_mtu - batadv_max_header_len(), BATADV_MAX_MTU);
693 }
694 
695 /**
696  * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller
697  *  MTU appeared
698  * @mesh_iface: netdev struct of the mesh interface
699  */
700 void batadv_update_min_mtu(struct net_device *mesh_iface)
701 {
702 	struct batadv_priv *bat_priv = netdev_priv(mesh_iface);
703 	int limit_mtu;
704 	int mtu;
705 
706 	mtu = batadv_hardif_min_mtu(mesh_iface);
707 
708 	if (bat_priv->mtu_set_by_user)
709 		limit_mtu = bat_priv->mtu_set_by_user;
710 	else
711 		limit_mtu = ETH_DATA_LEN;
712 
713 	mtu = min(mtu, limit_mtu);
714 	dev_set_mtu(mesh_iface, mtu);
715 
716 	/* Check if the local translate table should be cleaned up to match a
717 	 * new (and smaller) MTU.
718 	 */
719 	batadv_tt_local_resize_to_mtu(mesh_iface);
720 }
721 
722 /**
723  * batadv_hardif_activate_interface() - move a hard interface to the active
724  *  state
725  * @hard_iface: the interface that has come up
726  *
727  * Activate an interface, select it as the primary interface if no
728  * primary is currently active and invoke the algorithm-specific
729  * activate hook.
730  */
731 static void
732 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
733 {
734 	struct batadv_hard_iface *primary_if = NULL;
735 	struct batadv_priv *bat_priv;
736 
737 	if (hard_iface->if_status != BATADV_IF_INACTIVE)
738 		goto out;
739 
740 	bat_priv = netdev_priv(hard_iface->mesh_iface);
741 
742 	bat_priv->algo_ops->iface.update_mac(hard_iface);
743 	hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
744 
745 	/* the first active interface becomes our primary interface or
746 	 * the next active interface after the old primary interface was removed
747 	 */
748 	primary_if = batadv_primary_if_get_selected(bat_priv);
749 	if (!primary_if)
750 		batadv_primary_if_select(bat_priv, hard_iface);
751 
752 	batadv_info(hard_iface->mesh_iface, "Interface activated: %s\n",
753 		    hard_iface->net_dev->name);
754 
755 	batadv_update_min_mtu(hard_iface->mesh_iface);
756 
757 	if (bat_priv->algo_ops->iface.activate)
758 		bat_priv->algo_ops->iface.activate(hard_iface);
759 
760 out:
761 	batadv_hardif_put(primary_if);
762 }
763 
764 /**
765  * batadv_hardif_deactivate_interface() - move a hard interface to the
766  *  inactive state
767  * @hard_iface: the interface that went down
768  *
769  * Demote @hard_iface to BATADV_IF_INACTIVE and recalculate the mesh minimum
770  * MTU based on the remaining active interfaces.
771  */
772 static void
773 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
774 {
775 	if (hard_iface->if_status != BATADV_IF_ACTIVE &&
776 	    hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)
777 		return;
778 
779 	hard_iface->if_status = BATADV_IF_INACTIVE;
780 
781 	batadv_info(hard_iface->mesh_iface, "Interface deactivated: %s\n",
782 		    hard_iface->net_dev->name);
783 
784 	batadv_update_min_mtu(hard_iface->mesh_iface);
785 }
786 
787 /**
788  * batadv_hardif_enable_interface() - Enslave interface to mesh interface
789  * @net_dev: netdev struct of the interface to add to mesh interface
790  * @mesh_iface: netdev struct of the mesh interface
791  *
792  * Return: 0 on success or negative error number in case of failure
793  */
794 int batadv_hardif_enable_interface(struct net_device *net_dev,
795 				   struct net_device *mesh_iface)
796 {
797 	int max_header_len = batadv_max_header_len();
798 	__be16 ethertype = htons(ETH_P_BATMAN);
799 	struct batadv_hard_iface *hard_iface;
800 	struct batadv_priv *bat_priv;
801 	unsigned int required_mtu;
802 	unsigned int hardif_mtu;
803 	bool fragmentation;
804 	int ret;
805 
806 	ASSERT_RTNL();
807 
808 	if (!batadv_is_valid_iface(net_dev))
809 		return -EINVAL;
810 
811 	hardif_mtu = READ_ONCE(net_dev->mtu);
812 	required_mtu = READ_ONCE(mesh_iface->mtu) + max_header_len;
813 
814 	if (hardif_mtu < ETH_MIN_MTU + max_header_len)
815 		return -EINVAL;
816 
817 	hard_iface = kzalloc_obj(*hard_iface, GFP_ATOMIC);
818 	if (!hard_iface)
819 		return -ENOMEM;
820 
821 	netdev_hold(net_dev, &hard_iface->dev_tracker, GFP_ATOMIC);
822 	hard_iface->net_dev = net_dev;
823 
824 	hard_iface->if_status = BATADV_IF_INACTIVE;
825 
826 	INIT_HLIST_HEAD(&hard_iface->neigh_list);
827 
828 	mutex_init(&hard_iface->bat_iv.ogm_buff_mutex);
829 	spin_lock_init(&hard_iface->neigh_list_lock);
830 	kref_init(&hard_iface->refcount);
831 
832 	hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
833 	if (batadv_is_wifi_hardif(hard_iface))
834 		hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
835 
836 	WRITE_ONCE(hard_iface->hop_penalty, 0);
837 
838 	batadv_v_hardif_init(hard_iface);
839 
840 	netdev_hold(mesh_iface, &hard_iface->meshif_dev_tracker, GFP_ATOMIC);
841 	hard_iface->mesh_iface = mesh_iface;
842 	bat_priv = netdev_priv(hard_iface->mesh_iface);
843 
844 	bat_priv->hardif_generation++;
845 	ret = netdev_master_upper_dev_link(hard_iface->net_dev,
846 					   mesh_iface, hard_iface, NULL, NULL);
847 	if (ret)
848 		goto err_dev;
849 
850 	ret = bat_priv->algo_ops->iface.enable(hard_iface);
851 	if (ret < 0)
852 		goto err_upper;
853 
854 	hard_iface->batman_adv_ptype.type = ethertype;
855 	hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
856 	hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
857 	dev_add_pack(&hard_iface->batman_adv_ptype);
858 
859 	batadv_info(hard_iface->mesh_iface, "Adding interface: %s\n",
860 		    hard_iface->net_dev->name);
861 
862 	fragmentation = READ_ONCE(bat_priv->fragmentation);
863 	if (fragmentation && hardif_mtu < required_mtu)
864 		batadv_info(hard_iface->mesh_iface,
865 			    "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",
866 			    hard_iface->net_dev->name, hardif_mtu,
867 			    required_mtu);
868 
869 	if (!fragmentation && hardif_mtu < required_mtu)
870 		batadv_info(hard_iface->mesh_iface,
871 			    "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",
872 			    hard_iface->net_dev->name, hardif_mtu,
873 			    required_mtu);
874 
875 	batadv_check_known_mac_addr(hard_iface);
876 
877 	if (batadv_hardif_is_iface_up(hard_iface))
878 		batadv_hardif_activate_interface(hard_iface);
879 	else
880 		batadv_err(hard_iface->mesh_iface,
881 			   "Not using interface %s (retrying later): interface not active\n",
882 			   hard_iface->net_dev->name);
883 
884 	batadv_hardif_recalc_extra_skbroom(mesh_iface);
885 
886 	if (bat_priv->algo_ops->iface.enabled)
887 		bat_priv->algo_ops->iface.enabled(hard_iface);
888 
889 	return 0;
890 
891 err_upper:
892 	netdev_upper_dev_unlink(hard_iface->net_dev, mesh_iface);
893 err_dev:
894 	batadv_hardif_put(hard_iface);
895 	return ret;
896 }
897 
898 /**
899  * batadv_hardif_disable_interface() - Remove hard interface from mesh interface
900  * @hard_iface: hard interface to be removed
901  */
902 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
903 {
904 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
905 	struct batadv_hard_iface *primary_if;
906 
907 	ASSERT_RTNL();
908 
909 	batadv_hardif_deactivate_interface(hard_iface);
910 
911 	if (hard_iface->if_status != BATADV_IF_INACTIVE)
912 		return;
913 
914 	batadv_info(hard_iface->mesh_iface, "Removing interface: %s\n",
915 		    hard_iface->net_dev->name);
916 	dev_remove_pack(&hard_iface->batman_adv_ptype);
917 
918 	primary_if = batadv_primary_if_get_selected(bat_priv);
919 	if (hard_iface == primary_if) {
920 		struct batadv_hard_iface *new_if;
921 
922 		new_if = batadv_hardif_get_active(hard_iface->mesh_iface);
923 		batadv_primary_if_select(bat_priv, new_if);
924 
925 		batadv_hardif_put(new_if);
926 	}
927 	batadv_hardif_put(primary_if);
928 
929 	bat_priv->algo_ops->iface.disable(hard_iface);
930 	hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
931 
932 	/* delete all references to this hard_iface */
933 	batadv_purge_orig_ref(bat_priv);
934 	batadv_purge_outstanding_packets(bat_priv, hard_iface);
935 
936 	bat_priv->hardif_generation++;
937 	netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->mesh_iface);
938 	batadv_hardif_recalc_extra_skbroom(hard_iface->mesh_iface);
939 
940 	/* nobody uses this mesh interface anymore */
941 	if (list_empty(&hard_iface->mesh_iface->adj_list.lower))
942 		batadv_gw_check_client_stop(bat_priv);
943 
944 	batadv_hardif_put(hard_iface);
945 }
946 
947 /**
948  * batadv_hard_if_event_meshif() - Handle events for mesh interfaces
949  * @event: NETDEV_* event to handle
950  * @net_dev: net_device which generated an event
951  *
952  * Return: NOTIFY_* result
953  */
954 static int batadv_hard_if_event_meshif(unsigned long event,
955 				       struct net_device *net_dev)
956 {
957 	struct batadv_priv *bat_priv;
958 
959 	switch (event) {
960 	case NETDEV_REGISTER:
961 		bat_priv = netdev_priv(net_dev);
962 		batadv_meshif_create_vlan(bat_priv, BATADV_NO_FLAGS);
963 		break;
964 	}
965 
966 	return NOTIFY_DONE;
967 }
968 
969 /**
970  * batadv_wifi_net_device_insert() - save information about wifi net_device
971  * @net_dev: net_device to add to batadv_wifi_net_devices
972  * @wifi_flags: extracted batadv_hard_iface_wifi_flags of a net_device
973  *
974  * Return: 0 on success, negative value on error
975  */
976 static int
977 batadv_wifi_net_device_insert(struct net_device *net_dev, u32 wifi_flags)
978 {
979 	struct batadv_wifi_net_device_state *device_state;
980 	int ret;
981 
982 	ASSERT_RTNL();
983 
984 	device_state = kzalloc_obj(*device_state, GFP_KERNEL);
985 	if (!device_state)
986 		return -ENOMEM;
987 
988 	netdev_hold(net_dev, &device_state->dev_tracker, GFP_KERNEL);
989 	device_state->netdev = net_dev;
990 	WRITE_ONCE(device_state->wifi_flags, wifi_flags);
991 
992 	ret = rhashtable_insert_fast(&batadv_wifi_net_devices, &device_state->l,
993 				     batadv_wifi_net_devices_params);
994 	if (ret < 0)
995 		goto err_free;
996 
997 	return 0;
998 
999 err_free:
1000 	netdev_put(device_state->netdev, &device_state->dev_tracker);
1001 	kfree(device_state);
1002 	return ret;
1003 }
1004 
1005 /**
1006  * batadv_wifi_net_device_remove() - remove information about wifi net_device
1007  * @device_state: wifi net_device state to remove from batadv_wifi_net_devices
1008  */
1009 static void
1010 batadv_wifi_net_device_remove(struct batadv_wifi_net_device_state *device_state)
1011 {
1012 	ASSERT_RTNL();
1013 
1014 	rhashtable_remove_fast(&batadv_wifi_net_devices, &device_state->l,
1015 			       batadv_wifi_net_devices_params);
1016 	netdev_put(device_state->netdev, &device_state->dev_tracker);
1017 	kfree_rcu(device_state, rcu);
1018 }
1019 
1020 /**
1021  * batadv_wifi_net_device_update() - update wifi state of net_device
1022  * @net_dev: net_device to update in batadv_wifi_net_devices
1023  *
1024  * The device will only be stored in batadv_wifi_net_devices when
1025  * it could be identified as wifi device. If the net_device is no
1026  * longer a wifi device, it is automatically removed from
1027  * batadv_wifi_net_devices.
1028  */
1029 static void
1030 batadv_wifi_net_device_update(struct net_device *net_dev)
1031 {
1032 	struct batadv_wifi_net_device_state *device_state;
1033 	u32 wifi_flags;
1034 
1035 	ASSERT_RTNL();
1036 
1037 	wifi_flags = batadv_wifi_flags_evaluate(net_dev);
1038 	device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices,
1039 					      &net_dev,
1040 					      batadv_wifi_net_devices_params);
1041 
1042 	if (device_state) {
1043 		if (batadv_is_wifi(wifi_flags))
1044 			WRITE_ONCE(device_state->wifi_flags, wifi_flags);
1045 		else
1046 			batadv_wifi_net_device_remove(device_state);
1047 	} else if (batadv_is_wifi(wifi_flags)) {
1048 		batadv_wifi_net_device_insert(net_dev, wifi_flags);
1049 	}
1050 }
1051 
1052 /**
1053  * batadv_wifi_net_device_unregister() - remove wifi state of net_device
1054  * @net_dev: net_device to remove from batadv_wifi_net_devices
1055  */
1056 static void
1057 batadv_wifi_net_device_unregister(struct net_device *net_dev)
1058 {
1059 	struct batadv_wifi_net_device_state *device_state;
1060 
1061 	ASSERT_RTNL();
1062 
1063 	device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices,
1064 					      &net_dev,
1065 					      batadv_wifi_net_devices_params);
1066 	if (!device_state)
1067 		return;
1068 
1069 	batadv_wifi_net_device_remove(device_state);
1070 }
1071 
1072 /**
1073  * batadv_wifi_net_device_event() - handle network events for batadv_wifi_net_devices
1074  * @event: enum netdev_cmd event to handle
1075  * @net_dev: net_device to update in batadv_wifi_net_devices
1076  */
1077 static void batadv_wifi_net_device_event(unsigned long event,
1078 					 struct net_device *net_dev)
1079 {
1080 	switch (event) {
1081 	case NETDEV_REGISTER:
1082 	case NETDEV_POST_TYPE_CHANGE:
1083 	case NETDEV_CHANGEUPPER:
1084 		batadv_wifi_net_device_update(net_dev);
1085 		break;
1086 	case NETDEV_UNREGISTER:
1087 	case NETDEV_PRE_TYPE_CHANGE:
1088 		batadv_wifi_net_device_unregister(net_dev);
1089 		break;
1090 	}
1091 }
1092 
1093 /**
1094  * batadv_hard_if_event() - netdevice notifier callback for hard interfaces
1095  * @this: notifier block (unused)
1096  * @event: the NETDEV_* event to handle
1097  * @ptr: notifier info pointing to the affected net_device
1098  *
1099  * Dispatch netdevice events that affect potential or existing hard
1100  * interfaces. Mesh interfaces are handled separately by
1101  * batadv_hard_if_event_meshif().
1102  *
1103  * Return: NOTIFY_DONE
1104  */
1105 static int batadv_hard_if_event(struct notifier_block *this,
1106 				unsigned long event, void *ptr)
1107 {
1108 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
1109 	struct batadv_hard_iface *primary_if = NULL;
1110 	struct batadv_hard_iface *hard_iface;
1111 	struct batadv_priv *bat_priv;
1112 
1113 	if (batadv_meshif_is_valid(net_dev))
1114 		return batadv_hard_if_event_meshif(event, net_dev);
1115 
1116 	batadv_wifi_net_device_event(event, net_dev);
1117 
1118 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
1119 	if (!hard_iface)
1120 		goto out;
1121 
1122 	switch (event) {
1123 	case NETDEV_UP:
1124 		batadv_hardif_activate_interface(hard_iface);
1125 		break;
1126 	case NETDEV_GOING_DOWN:
1127 	case NETDEV_DOWN:
1128 		batadv_hardif_deactivate_interface(hard_iface);
1129 		break;
1130 	case NETDEV_UNREGISTER:
1131 	case NETDEV_PRE_TYPE_CHANGE:
1132 		batadv_hardif_disable_interface(hard_iface);
1133 		break;
1134 	case NETDEV_CHANGEMTU:
1135 		batadv_update_min_mtu(hard_iface->mesh_iface);
1136 		break;
1137 	case NETDEV_CHANGEADDR:
1138 		batadv_check_known_mac_addr(hard_iface);
1139 
1140 		bat_priv = netdev_priv(hard_iface->mesh_iface);
1141 		bat_priv->algo_ops->iface.update_mac(hard_iface);
1142 
1143 		primary_if = batadv_primary_if_get_selected(bat_priv);
1144 		if (!primary_if)
1145 			goto hardif_put;
1146 
1147 		if (hard_iface == primary_if)
1148 			batadv_primary_if_update_addr(bat_priv, NULL);
1149 		break;
1150 	case NETDEV_REGISTER:
1151 	case NETDEV_POST_TYPE_CHANGE:
1152 	case NETDEV_CHANGEUPPER:
1153 		if (batadv_is_wifi_hardif(hard_iface))
1154 			hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
1155 		break;
1156 	default:
1157 		break;
1158 	}
1159 
1160 hardif_put:
1161 	batadv_hardif_put(hard_iface);
1162 out:
1163 	batadv_hardif_put(primary_if);
1164 	return NOTIFY_DONE;
1165 }
1166 
1167 struct notifier_block batadv_hard_if_notifier = {
1168 	.notifier_call = batadv_hard_if_event,
1169 };
1170 
1171 /**
1172  * batadv_wifi_net_devices_init() - Initialize wifi devices cache
1173  *
1174  * Return: 0 on success, negative error code on failure
1175  */
1176 int __init batadv_wifi_net_devices_init(void)
1177 {
1178 	return rhashtable_init(&batadv_wifi_net_devices,
1179 			       &batadv_wifi_net_devices_params);
1180 }
1181 
1182 /**
1183  * batadv_wifi_net_devices_deinit() - Deinitialize wifi devices cache
1184  */
1185 void batadv_wifi_net_devices_deinit(void)
1186 {
1187 	/* just destroy table. entries should have been removed by
1188 	 * unregister_netdevice_notifier() and the corresponding
1189 	 * NETDEV_UNREGISTER events
1190 	 */
1191 	rhashtable_destroy(&batadv_wifi_net_devices);
1192 }
1193