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