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_hardif_get_wifi_flags() - retrieve wifi flags for hard_iface 370 * @hard_iface: the device to check 371 * 372 * Return: batadv_hard_iface_wifi_flags flags of the device 373 */ 374 u32 batadv_hardif_get_wifi_flags(struct batadv_hard_iface *hard_iface) 375 { 376 struct batadv_wifi_net_device_state *device_state; 377 u32 wifi_flags = 0; 378 379 if (!hard_iface) 380 return 0; 381 382 rcu_read_lock(); 383 device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices, 384 &hard_iface->net_dev, 385 batadv_wifi_net_devices_params); 386 if (device_state) 387 wifi_flags = READ_ONCE(device_state->wifi_flags); 388 rcu_read_unlock(); 389 390 return wifi_flags; 391 } 392 393 /** 394 * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface 395 * @hard_iface: the device to check 396 * 397 * Return: true if the net device is a 802.11 wireless device, false otherwise. 398 */ 399 bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface) 400 { 401 u32 wifi_flags = batadv_hardif_get_wifi_flags(hard_iface); 402 403 return batadv_is_wifi(wifi_flags); 404 } 405 406 /** 407 * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary 408 * @if_outgoing: the outgoing interface checked and considered for (re)broadcast 409 * @orig_addr: the originator of this packet 410 * @orig_neigh: originator address of the forwarder we just got the packet from 411 * (NULL if we originated) 412 * 413 * Checks whether a packet needs to be (re)broadcasted on the given interface. 414 * 415 * Return: 416 * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface 417 * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder 418 * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator 419 * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast 420 */ 421 int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing, 422 u8 *orig_addr, u8 *orig_neigh) 423 { 424 struct batadv_hardif_neigh_node *hardif_neigh; 425 struct hlist_node *first; 426 int ret = BATADV_HARDIF_BCAST_OK; 427 428 rcu_read_lock(); 429 430 /* 0 neighbors -> no (re)broadcast */ 431 first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list)); 432 if (!first) { 433 ret = BATADV_HARDIF_BCAST_NORECIPIENT; 434 goto out; 435 } 436 437 /* >1 neighbors -> (re)broadcast */ 438 if (rcu_dereference(hlist_next_rcu(first))) 439 goto out; 440 441 hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node, 442 list); 443 444 /* 1 neighbor, is the originator -> no rebroadcast */ 445 if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) { 446 ret = BATADV_HARDIF_BCAST_DUPORIG; 447 /* 1 neighbor, is the one we received from -> no rebroadcast */ 448 } else if (orig_neigh && 449 batadv_compare_eth(hardif_neigh->orig, orig_neigh)) { 450 ret = BATADV_HARDIF_BCAST_DUPFWD; 451 } 452 453 out: 454 rcu_read_unlock(); 455 return ret; 456 } 457 458 static struct batadv_hard_iface * 459 batadv_hardif_get_active(struct net_device *mesh_iface) 460 { 461 struct batadv_hard_iface *hard_iface; 462 struct list_head *iter; 463 464 rcu_read_lock(); 465 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 466 if (hard_iface->if_status == BATADV_IF_ACTIVE && 467 kref_get_unless_zero(&hard_iface->refcount)) 468 goto out; 469 } 470 471 hard_iface = NULL; 472 473 out: 474 rcu_read_unlock(); 475 return hard_iface; 476 } 477 478 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, 479 struct batadv_hard_iface *oldif) 480 { 481 struct batadv_hard_iface *primary_if; 482 483 primary_if = batadv_primary_if_get_selected(bat_priv); 484 if (!primary_if) 485 goto out; 486 487 batadv_dat_init_own_addr(bat_priv, primary_if); 488 batadv_bla_update_orig_address(bat_priv, primary_if, oldif); 489 out: 490 batadv_hardif_put(primary_if); 491 } 492 493 static void batadv_primary_if_select(struct batadv_priv *bat_priv, 494 struct batadv_hard_iface *new_hard_iface) 495 { 496 struct batadv_hard_iface *curr_hard_iface; 497 498 ASSERT_RTNL(); 499 500 if (new_hard_iface) 501 kref_get(&new_hard_iface->refcount); 502 503 curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if, 504 new_hard_iface, 1); 505 506 if (!new_hard_iface) 507 goto out; 508 509 bat_priv->algo_ops->iface.primary_set(new_hard_iface); 510 batadv_primary_if_update_addr(bat_priv, curr_hard_iface); 511 512 out: 513 batadv_hardif_put(curr_hard_iface); 514 } 515 516 static bool 517 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) 518 { 519 if (hard_iface->net_dev->flags & IFF_UP) 520 return true; 521 522 return false; 523 } 524 525 static void batadv_check_known_mac_addr(const struct batadv_hard_iface *hard_iface) 526 { 527 struct net_device *mesh_iface = hard_iface->mesh_iface; 528 const struct batadv_hard_iface *tmp_hard_iface; 529 struct list_head *iter; 530 531 if (!mesh_iface) 532 return; 533 534 netdev_for_each_lower_private(mesh_iface, tmp_hard_iface, iter) { 535 if (tmp_hard_iface == hard_iface) 536 continue; 537 538 if (tmp_hard_iface->if_status == BATADV_IF_NOT_IN_USE) 539 continue; 540 541 if (!batadv_compare_eth(tmp_hard_iface->net_dev->dev_addr, 542 hard_iface->net_dev->dev_addr)) 543 continue; 544 545 pr_warn("The newly added mac address (%pM) already exists on: %s\n", 546 hard_iface->net_dev->dev_addr, tmp_hard_iface->net_dev->name); 547 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); 548 } 549 } 550 551 /** 552 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom 553 * @mesh_iface: netdev struct of the mesh interface 554 */ 555 static void batadv_hardif_recalc_extra_skbroom(struct net_device *mesh_iface) 556 { 557 const struct batadv_hard_iface *hard_iface; 558 unsigned short lower_header_len = ETH_HLEN; 559 unsigned short lower_headroom = 0; 560 unsigned short lower_tailroom = 0; 561 unsigned short needed_headroom; 562 struct list_head *iter; 563 564 rcu_read_lock(); 565 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 566 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 567 continue; 568 569 lower_header_len = max_t(unsigned short, lower_header_len, 570 hard_iface->net_dev->hard_header_len); 571 572 lower_headroom = max_t(unsigned short, lower_headroom, 573 hard_iface->net_dev->needed_headroom); 574 575 lower_tailroom = max_t(unsigned short, lower_tailroom, 576 hard_iface->net_dev->needed_tailroom); 577 } 578 rcu_read_unlock(); 579 580 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN); 581 needed_headroom += batadv_max_header_len(); 582 583 /* fragmentation headers don't strip the unicast/... header */ 584 needed_headroom += sizeof(struct batadv_frag_packet); 585 586 mesh_iface->needed_headroom = needed_headroom; 587 mesh_iface->needed_tailroom = lower_tailroom; 588 } 589 590 /** 591 * batadv_hardif_min_mtu() - Calculate maximum MTU for mesh interface 592 * @mesh_iface: netdev struct of the mesh interface 593 * 594 * Return: MTU for the mesh-interface (limited by the minimal MTU of all active 595 * slave interfaces) 596 */ 597 int batadv_hardif_min_mtu(struct net_device *mesh_iface) 598 { 599 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 600 const struct batadv_hard_iface *hard_iface; 601 struct list_head *iter; 602 int min_mtu = INT_MAX; 603 604 rcu_read_lock(); 605 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 606 if (hard_iface->if_status != BATADV_IF_ACTIVE && 607 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED) 608 continue; 609 610 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu); 611 } 612 rcu_read_unlock(); 613 614 if (READ_ONCE(bat_priv->fragmentation) == 0) 615 goto out; 616 617 /* with fragmentation enabled the maximum size of internally generated 618 * packets such as translation table exchanges or tvlv containers, etc 619 * has to be calculated 620 */ 621 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE); 622 min_mtu -= sizeof(struct batadv_frag_packet); 623 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS; 624 625 out: 626 /* report to the other components the maximum amount of bytes that 627 * batman-adv can send over the wire (without considering the payload 628 * overhead). For example, this value is used by TT to compute the 629 * maximum local table size 630 */ 631 WRITE_ONCE(bat_priv->packet_size_max, min_mtu); 632 633 /* the real mesh-interface MTU is computed by removing the payload 634 * overhead from the maximum amount of bytes that was just computed. 635 */ 636 return min_t(int, min_mtu - batadv_max_header_len(), BATADV_MAX_MTU); 637 } 638 639 /** 640 * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller 641 * MTU appeared 642 * @mesh_iface: netdev struct of the mesh interface 643 */ 644 void batadv_update_min_mtu(struct net_device *mesh_iface) 645 { 646 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 647 int limit_mtu; 648 int mtu; 649 650 mtu = batadv_hardif_min_mtu(mesh_iface); 651 652 if (bat_priv->mtu_set_by_user) 653 limit_mtu = bat_priv->mtu_set_by_user; 654 else 655 limit_mtu = ETH_DATA_LEN; 656 657 mtu = min(mtu, limit_mtu); 658 dev_set_mtu(mesh_iface, mtu); 659 660 /* Check if the local translate table should be cleaned up to match a 661 * new (and smaller) MTU. 662 */ 663 batadv_tt_local_resize_to_mtu(mesh_iface); 664 } 665 666 static void 667 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) 668 { 669 struct batadv_priv *bat_priv; 670 struct batadv_hard_iface *primary_if = NULL; 671 672 if (hard_iface->if_status != BATADV_IF_INACTIVE) 673 goto out; 674 675 bat_priv = netdev_priv(hard_iface->mesh_iface); 676 677 bat_priv->algo_ops->iface.update_mac(hard_iface); 678 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; 679 680 /* the first active interface becomes our primary interface or 681 * the next active interface after the old primary interface was removed 682 */ 683 primary_if = batadv_primary_if_get_selected(bat_priv); 684 if (!primary_if) 685 batadv_primary_if_select(bat_priv, hard_iface); 686 687 batadv_info(hard_iface->mesh_iface, "Interface activated: %s\n", 688 hard_iface->net_dev->name); 689 690 batadv_update_min_mtu(hard_iface->mesh_iface); 691 692 if (bat_priv->algo_ops->iface.activate) 693 bat_priv->algo_ops->iface.activate(hard_iface); 694 695 out: 696 batadv_hardif_put(primary_if); 697 } 698 699 static void 700 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) 701 { 702 if (hard_iface->if_status != BATADV_IF_ACTIVE && 703 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED) 704 return; 705 706 hard_iface->if_status = BATADV_IF_INACTIVE; 707 708 batadv_info(hard_iface->mesh_iface, "Interface deactivated: %s\n", 709 hard_iface->net_dev->name); 710 711 batadv_update_min_mtu(hard_iface->mesh_iface); 712 } 713 714 /** 715 * batadv_hardif_enable_interface() - Enslave hard interface to mesh interface 716 * @hard_iface: hard interface to add to mesh interface 717 * @mesh_iface: netdev struct of the mesh interface 718 * 719 * Return: 0 on success or negative error number in case of failure 720 */ 721 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, 722 struct net_device *mesh_iface) 723 { 724 struct batadv_priv *bat_priv; 725 __be16 ethertype = htons(ETH_P_BATMAN); 726 int max_header_len = batadv_max_header_len(); 727 unsigned int required_mtu; 728 unsigned int hardif_mtu; 729 bool fragmentation; 730 int ret; 731 732 hardif_mtu = READ_ONCE(hard_iface->net_dev->mtu); 733 required_mtu = READ_ONCE(mesh_iface->mtu) + max_header_len; 734 735 if (hardif_mtu < ETH_MIN_MTU + max_header_len) 736 return -EINVAL; 737 738 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 739 goto out; 740 741 kref_get(&hard_iface->refcount); 742 743 netdev_hold(mesh_iface, &hard_iface->meshif_dev_tracker, GFP_ATOMIC); 744 hard_iface->mesh_iface = mesh_iface; 745 bat_priv = netdev_priv(hard_iface->mesh_iface); 746 747 ret = netdev_master_upper_dev_link(hard_iface->net_dev, 748 mesh_iface, hard_iface, NULL, NULL); 749 if (ret) 750 goto err_dev; 751 752 ret = bat_priv->algo_ops->iface.enable(hard_iface); 753 if (ret < 0) 754 goto err_upper; 755 756 hard_iface->if_status = BATADV_IF_INACTIVE; 757 758 kref_get(&hard_iface->refcount); 759 hard_iface->batman_adv_ptype.type = ethertype; 760 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; 761 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; 762 dev_add_pack(&hard_iface->batman_adv_ptype); 763 764 batadv_info(hard_iface->mesh_iface, "Adding interface: %s\n", 765 hard_iface->net_dev->name); 766 767 fragmentation = READ_ONCE(bat_priv->fragmentation); 768 if (fragmentation && hardif_mtu < required_mtu) 769 batadv_info(hard_iface->mesh_iface, 770 "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", 771 hard_iface->net_dev->name, hardif_mtu, 772 required_mtu); 773 774 if (!fragmentation && hardif_mtu < required_mtu) 775 batadv_info(hard_iface->mesh_iface, 776 "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", 777 hard_iface->net_dev->name, hardif_mtu, 778 required_mtu); 779 780 batadv_check_known_mac_addr(hard_iface); 781 782 if (batadv_hardif_is_iface_up(hard_iface)) 783 batadv_hardif_activate_interface(hard_iface); 784 else 785 batadv_err(hard_iface->mesh_iface, 786 "Not using interface %s (retrying later): interface not active\n", 787 hard_iface->net_dev->name); 788 789 batadv_hardif_recalc_extra_skbroom(mesh_iface); 790 791 if (bat_priv->algo_ops->iface.enabled) 792 bat_priv->algo_ops->iface.enabled(hard_iface); 793 794 out: 795 return 0; 796 797 err_upper: 798 netdev_upper_dev_unlink(hard_iface->net_dev, mesh_iface); 799 err_dev: 800 hard_iface->mesh_iface = NULL; 801 netdev_put(mesh_iface, &hard_iface->meshif_dev_tracker); 802 batadv_hardif_put(hard_iface); 803 return ret; 804 } 805 806 /** 807 * batadv_hardif_cnt() - get number of interfaces enslaved to mesh interface 808 * @mesh_iface: mesh interface to check 809 * 810 * This function is only using RCU for locking - the result can therefore be 811 * off when another function is modifying the list at the same time. The 812 * caller can use the rtnl_lock to make sure that the count is accurate. 813 * 814 * Return: number of connected/enslaved hard interfaces 815 */ 816 static size_t batadv_hardif_cnt(struct net_device *mesh_iface) 817 { 818 struct batadv_hard_iface *hard_iface; 819 struct list_head *iter; 820 size_t count = 0; 821 822 rcu_read_lock(); 823 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) 824 count++; 825 rcu_read_unlock(); 826 827 return count; 828 } 829 830 /** 831 * batadv_hardif_disable_interface() - Remove hard interface from mesh interface 832 * @hard_iface: hard interface to be removed 833 */ 834 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface) 835 { 836 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface); 837 struct batadv_hard_iface *primary_if = NULL; 838 839 batadv_hardif_deactivate_interface(hard_iface); 840 841 if (hard_iface->if_status != BATADV_IF_INACTIVE) 842 goto out; 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 batadv_hardif_put(hard_iface); 848 849 primary_if = batadv_primary_if_get_selected(bat_priv); 850 if (hard_iface == primary_if) { 851 struct batadv_hard_iface *new_if; 852 853 new_if = batadv_hardif_get_active(hard_iface->mesh_iface); 854 batadv_primary_if_select(bat_priv, new_if); 855 856 batadv_hardif_put(new_if); 857 } 858 859 bat_priv->algo_ops->iface.disable(hard_iface); 860 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 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 netdev_put(hard_iface->mesh_iface, &hard_iface->meshif_dev_tracker); 866 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 interface anymore */ 871 if (batadv_hardif_cnt(hard_iface->mesh_iface) <= 1) 872 batadv_gw_check_client_stop(bat_priv); 873 874 hard_iface->mesh_iface = NULL; 875 batadv_hardif_put(hard_iface); 876 877 out: 878 batadv_hardif_put(primary_if); 879 } 880 881 static struct batadv_hard_iface * 882 batadv_hardif_add_interface(struct net_device *net_dev) 883 { 884 struct batadv_hard_iface *hard_iface; 885 886 ASSERT_RTNL(); 887 888 if (!batadv_is_valid_iface(net_dev)) 889 return NULL; 890 891 hard_iface = kzalloc_obj(*hard_iface, GFP_ATOMIC); 892 if (!hard_iface) 893 return NULL; 894 895 netdev_hold(net_dev, &hard_iface->dev_tracker, GFP_ATOMIC); 896 hard_iface->net_dev = net_dev; 897 898 hard_iface->mesh_iface = NULL; 899 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 900 901 INIT_LIST_HEAD(&hard_iface->list); 902 INIT_HLIST_HEAD(&hard_iface->neigh_list); 903 904 mutex_init(&hard_iface->bat_iv.ogm_buff_mutex); 905 spin_lock_init(&hard_iface->neigh_list_lock); 906 kref_init(&hard_iface->refcount); 907 908 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT; 909 if (batadv_is_wifi_hardif(hard_iface)) 910 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; 911 912 WRITE_ONCE(hard_iface->hop_penalty, 0); 913 914 batadv_v_hardif_init(hard_iface); 915 916 kref_get(&hard_iface->refcount); 917 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); 918 batadv_hardif_generation++; 919 920 return hard_iface; 921 } 922 923 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) 924 { 925 ASSERT_RTNL(); 926 927 /* first deactivate interface */ 928 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 929 batadv_hardif_disable_interface(hard_iface); 930 931 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 932 return; 933 934 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; 935 batadv_hardif_put(hard_iface); 936 } 937 938 /** 939 * batadv_hard_if_event_meshif() - Handle events for mesh interfaces 940 * @event: NETDEV_* event to handle 941 * @net_dev: net_device which generated an event 942 * 943 * Return: NOTIFY_* result 944 */ 945 static int batadv_hard_if_event_meshif(unsigned long event, 946 struct net_device *net_dev) 947 { 948 struct batadv_priv *bat_priv; 949 950 switch (event) { 951 case NETDEV_REGISTER: 952 bat_priv = netdev_priv(net_dev); 953 batadv_meshif_create_vlan(bat_priv, BATADV_NO_FLAGS); 954 break; 955 } 956 957 return NOTIFY_DONE; 958 } 959 960 /** 961 * batadv_wifi_net_device_insert() - save information about wifi net_device 962 * @net_dev: net_device to add to batadv_wifi_net_devices 963 * @wifi_flags: net_device which generated an event 964 * 965 * Return: 0 on result, negative value on error 966 */ 967 static int 968 batadv_wifi_net_device_insert(struct net_device *net_dev, u32 wifi_flags) 969 { 970 struct batadv_wifi_net_device_state *device_state; 971 int ret; 972 973 ASSERT_RTNL(); 974 975 device_state = kzalloc_obj(*device_state, GFP_ATOMIC); 976 if (!device_state) 977 return -ENOMEM; 978 979 device_state->wifi_flags = wifi_flags; 980 netdev_hold(net_dev, &device_state->dev_tracker, GFP_ATOMIC); 981 device_state->netdev = net_dev; 982 WRITE_ONCE(device_state->wifi_flags, wifi_flags); 983 984 ret = rhashtable_insert_fast(&batadv_wifi_net_devices, &device_state->l, 985 batadv_wifi_net_devices_params); 986 if (ret < 0) 987 goto err_free; 988 989 return 0; 990 991 err_free: 992 netdev_put(device_state->netdev, &device_state->dev_tracker); 993 kfree(device_state); 994 return ret; 995 } 996 997 /** 998 * batadv_wifi_net_device_remove() - remove information about wifi net_device 999 * @device_state: wifi net_device state to remove from batadv_wifi_net_device_state 1000 */ 1001 static void 1002 batadv_wifi_net_device_remove(struct batadv_wifi_net_device_state *device_state) 1003 { 1004 ASSERT_RTNL(); 1005 1006 rhashtable_remove_fast(&batadv_wifi_net_devices, &device_state->l, 1007 batadv_wifi_net_devices_params); 1008 netdev_put(device_state->netdev, &device_state->dev_tracker); 1009 kfree_rcu(device_state, rcu); 1010 } 1011 1012 /** 1013 * batadv_wifi_net_device_update() - update wifi state of net_device 1014 * @net_dev: net_device to update in batadv_wifi_net_devices 1015 * 1016 * The device will only be stored in batadv_wifi_net_devices when 1017 * it could be identified as wifi device. If the net_device is no 1018 * longer a wifi device, it is automatically removed from 1019 * batadv_wifi_net_devices. 1020 */ 1021 static void 1022 batadv_wifi_net_device_update(struct net_device *net_dev) 1023 { 1024 struct batadv_wifi_net_device_state *device_state; 1025 u32 wifi_flags; 1026 1027 ASSERT_RTNL(); 1028 1029 wifi_flags = batadv_wifi_flags_evaluate(net_dev); 1030 device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices, 1031 &net_dev, 1032 batadv_wifi_net_devices_params); 1033 1034 if (device_state) { 1035 if (batadv_is_wifi(wifi_flags)) 1036 WRITE_ONCE(device_state->wifi_flags, wifi_flags); 1037 else 1038 batadv_wifi_net_device_remove(device_state); 1039 } else if (batadv_is_wifi(wifi_flags)) { 1040 batadv_wifi_net_device_insert(net_dev, wifi_flags); 1041 } 1042 } 1043 1044 /** 1045 * batadv_wifi_net_device_unregister() - remove wifi state of net_device 1046 * @net_dev: net_device to remove from batadv_wifi_net_devices 1047 */ 1048 static void 1049 batadv_wifi_net_device_unregister(struct net_device *net_dev) 1050 { 1051 struct batadv_wifi_net_device_state *device_state; 1052 1053 ASSERT_RTNL(); 1054 1055 device_state = rhashtable_lookup_fast(&batadv_wifi_net_devices, 1056 &net_dev, 1057 batadv_wifi_net_devices_params); 1058 if (!device_state) 1059 return; 1060 1061 batadv_wifi_net_device_remove(device_state); 1062 } 1063 1064 /** 1065 * batadv_wifi_net_device_event() - handle network events for batadv_wifi_net_devices 1066 * @event: enum netdev_cmd event to handle 1067 * @net_dev: net_device to update in batadv_wifi_net_devices 1068 */ 1069 static void batadv_wifi_net_device_event(unsigned long event, 1070 struct net_device *net_dev) 1071 { 1072 switch (event) { 1073 case NETDEV_REGISTER: 1074 case NETDEV_POST_TYPE_CHANGE: 1075 case NETDEV_CHANGEUPPER: 1076 batadv_wifi_net_device_update(net_dev); 1077 break; 1078 case NETDEV_UNREGISTER: 1079 case NETDEV_PRE_TYPE_CHANGE: 1080 batadv_wifi_net_device_unregister(net_dev); 1081 break; 1082 } 1083 } 1084 1085 static int batadv_hard_if_event(struct notifier_block *this, 1086 unsigned long event, void *ptr) 1087 { 1088 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); 1089 struct batadv_hard_iface *hard_iface; 1090 struct batadv_hard_iface *primary_if = NULL; 1091 struct batadv_priv *bat_priv; 1092 1093 if (batadv_meshif_is_valid(net_dev)) 1094 return batadv_hard_if_event_meshif(event, net_dev); 1095 1096 batadv_wifi_net_device_event(event, net_dev); 1097 1098 hard_iface = batadv_hardif_get_by_netdev(net_dev); 1099 if (!hard_iface && (event == NETDEV_REGISTER || 1100 event == NETDEV_POST_TYPE_CHANGE)) 1101 hard_iface = batadv_hardif_add_interface(net_dev); 1102 1103 if (!hard_iface) 1104 goto out; 1105 1106 switch (event) { 1107 case NETDEV_UP: 1108 batadv_hardif_activate_interface(hard_iface); 1109 break; 1110 case NETDEV_GOING_DOWN: 1111 case NETDEV_DOWN: 1112 batadv_hardif_deactivate_interface(hard_iface); 1113 break; 1114 case NETDEV_UNREGISTER: 1115 case NETDEV_PRE_TYPE_CHANGE: 1116 list_del_rcu(&hard_iface->list); 1117 batadv_hardif_generation++; 1118 1119 batadv_hardif_remove_interface(hard_iface); 1120 break; 1121 case NETDEV_CHANGEMTU: 1122 if (hard_iface->mesh_iface) 1123 batadv_update_min_mtu(hard_iface->mesh_iface); 1124 break; 1125 case NETDEV_CHANGEADDR: 1126 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 1127 goto hardif_put; 1128 1129 batadv_check_known_mac_addr(hard_iface); 1130 1131 bat_priv = netdev_priv(hard_iface->mesh_iface); 1132 bat_priv->algo_ops->iface.update_mac(hard_iface); 1133 1134 primary_if = batadv_primary_if_get_selected(bat_priv); 1135 if (!primary_if) 1136 goto hardif_put; 1137 1138 if (hard_iface == primary_if) 1139 batadv_primary_if_update_addr(bat_priv, NULL); 1140 break; 1141 case NETDEV_REGISTER: 1142 case NETDEV_POST_TYPE_CHANGE: 1143 case NETDEV_CHANGEUPPER: 1144 if (batadv_is_wifi_hardif(hard_iface)) 1145 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; 1146 break; 1147 default: 1148 break; 1149 } 1150 1151 hardif_put: 1152 batadv_hardif_put(hard_iface); 1153 out: 1154 batadv_hardif_put(primary_if); 1155 return NOTIFY_DONE; 1156 } 1157 1158 struct notifier_block batadv_hard_if_notifier = { 1159 .notifier_call = batadv_hard_if_event, 1160 }; 1161 1162 /** 1163 * batadv_wifi_net_devices_init() - Initialize wifi devices cache 1164 * 1165 * Return: 0 on success, negative error code on failure 1166 */ 1167 int __init batadv_wifi_net_devices_init(void) 1168 { 1169 return rhashtable_init(&batadv_wifi_net_devices, 1170 &batadv_wifi_net_devices_params); 1171 } 1172 1173 /** 1174 * batadv_wifi_net_devices_deinit() - Deinitialize wifi devices cache 1175 */ 1176 void batadv_wifi_net_devices_deinit(void) 1177 { 1178 rhashtable_destroy(&batadv_wifi_net_devices); 1179 } 1180