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