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