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