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 "mesh-interface.h" 8 #include "main.h" 9 10 #include <linux/atomic.h> 11 #include <linux/bug.h> 12 #include <linux/byteorder/generic.h> 13 #include <linux/cache.h> 14 #include <linux/compiler.h> 15 #include <linux/container_of.h> 16 #include <linux/cpumask.h> 17 #include <linux/errno.h> 18 #include <linux/etherdevice.h> 19 #include <linux/ethtool.h> 20 #include <linux/gfp.h> 21 #include <linux/if_ether.h> 22 #include <linux/if_vlan.h> 23 #include <linux/jiffies.h> 24 #include <linux/kref.h> 25 #include <linux/list.h> 26 #include <linux/lockdep.h> 27 #include <linux/netdevice.h> 28 #include <linux/netlink.h> 29 #include <linux/percpu.h> 30 #include <linux/random.h> 31 #include <linux/rculist.h> 32 #include <linux/rcupdate.h> 33 #include <linux/skbuff.h> 34 #include <linux/slab.h> 35 #include <linux/socket.h> 36 #include <linux/spinlock.h> 37 #include <linux/stddef.h> 38 #include <linux/string.h> 39 #include <linux/types.h> 40 #include <linux/utsname.h> 41 #include <net/netlink.h> 42 #include <net/rtnetlink.h> 43 #include <uapi/linux/batadv_packet.h> 44 #include <uapi/linux/batman_adv.h> 45 46 #include "bat_algo.h" 47 #include "bridge_loop_avoidance.h" 48 #include "distributed-arp-table.h" 49 #include "gateway_client.h" 50 #include "hard-interface.h" 51 #include "multicast.h" 52 #include "send.h" 53 #include "translation-table.h" 54 55 /** 56 * batadv_skb_head_push() - Increase header size and move (push) head pointer 57 * @skb: packet buffer which should be modified 58 * @len: number of bytes to add 59 * 60 * Return: 0 on success or negative error number in case of failure 61 */ 62 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len) 63 { 64 int result; 65 66 /* TODO: We must check if we can release all references to non-payload 67 * data using __skb_header_release in our skbs to allow skb_cow_header 68 * to work optimally. This means that those skbs are not allowed to read 69 * or write any data which is before the current position of skb->data 70 * after that call and thus allow other skbs with the same data buffer 71 * to write freely in that area. 72 */ 73 result = skb_cow_head(skb, len); 74 if (result < 0) 75 return result; 76 77 skb_push(skb, len); 78 return 0; 79 } 80 81 /** 82 * batadv_sum_counter() - Sum the cpu-local counters for index 'idx' 83 * @bat_priv: the bat priv with all the mesh interface information 84 * @idx: index of counter to sum up 85 * 86 * Return: sum of all cpu-local counters 87 */ 88 static u64 batadv_sum_counter(struct batadv_priv *bat_priv, size_t idx) 89 { 90 u64 *counters, sum = 0; 91 int cpu; 92 93 for_each_possible_cpu(cpu) { 94 counters = per_cpu_ptr(bat_priv->bat_counters, cpu); 95 sum += counters[idx]; 96 } 97 98 return sum; 99 } 100 101 static struct net_device_stats *batadv_interface_stats(struct net_device *dev) 102 { 103 struct batadv_priv *bat_priv = netdev_priv(dev); 104 struct net_device_stats *stats = &dev->stats; 105 106 stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX); 107 stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES); 108 stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED); 109 stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX); 110 stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES); 111 return stats; 112 } 113 114 static int batadv_interface_set_mac_addr(struct net_device *dev, void *p) 115 { 116 struct batadv_priv *bat_priv = netdev_priv(dev); 117 struct batadv_meshif_vlan *vlan; 118 struct sockaddr *addr = p; 119 u8 old_addr[ETH_ALEN]; 120 121 if (!is_valid_ether_addr(addr->sa_data)) 122 return -EADDRNOTAVAIL; 123 124 ether_addr_copy(old_addr, dev->dev_addr); 125 eth_hw_addr_set(dev, addr->sa_data); 126 127 /* only modify transtable if it has been initialized before */ 128 if (READ_ONCE(bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 129 return 0; 130 131 rcu_read_lock(); 132 hlist_for_each_entry_rcu(vlan, &bat_priv->meshif_vlan_list, list) { 133 batadv_tt_local_remove(bat_priv, old_addr, vlan->vid, 134 "mac address changed", false); 135 batadv_tt_local_add(dev, addr->sa_data, vlan->vid, 136 BATADV_NULL_IFINDEX, BATADV_NO_MARK); 137 } 138 rcu_read_unlock(); 139 140 return 0; 141 } 142 143 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu) 144 { 145 struct batadv_priv *bat_priv = netdev_priv(dev); 146 147 /* check ranges */ 148 if (new_mtu < ETH_MIN_MTU || new_mtu > batadv_hardif_min_mtu(dev)) 149 return -EINVAL; 150 151 WRITE_ONCE(dev->mtu, new_mtu); 152 bat_priv->mtu_set_by_user = new_mtu; 153 154 return 0; 155 } 156 157 /** 158 * batadv_interface_set_rx_mode() - set the rx mode of a device 159 * @dev: registered network device to modify 160 * 161 * We do not actually need to set any rx filters for the virtual batman 162 * mesh interface. However a dummy handler enables a user to set static 163 * multicast listeners for instance. 164 */ 165 static void batadv_interface_set_rx_mode(struct net_device *dev) 166 { 167 } 168 169 static netdev_tx_t batadv_interface_tx(struct sk_buff *skb, 170 struct net_device *mesh_iface) 171 { 172 struct ethhdr *ethhdr; 173 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 174 struct batadv_hard_iface *primary_if = NULL; 175 struct batadv_bcast_packet *bcast_packet; 176 static const u8 stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 177 0x00, 0x00}; 178 static const u8 ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00, 179 0x00, 0x00}; 180 enum batadv_dhcp_recipient dhcp_rcp = BATADV_DHCP_NO; 181 u8 *dst_hint = NULL, chaddr[ETH_ALEN]; 182 struct vlan_ethhdr *vhdr; 183 unsigned int header_len = 0; 184 int data_len = skb->len, ret; 185 unsigned long brd_delay = 0; 186 bool do_bcast = false, client_added; 187 unsigned short vid; 188 u32 seqno; 189 int gw_mode; 190 enum batadv_forw_mode forw_mode = BATADV_FORW_BCAST; 191 int mcast_is_routable = 0; 192 int network_offset = ETH_HLEN; 193 __be16 proto; 194 195 if (READ_ONCE(bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 196 goto dropped; 197 198 /* reset control block to avoid left overs from previous users */ 199 memset(skb->cb, 0, sizeof(struct batadv_skb_cb)); 200 201 netif_trans_update(mesh_iface); 202 vid = batadv_get_vid(skb, 0); 203 204 skb_reset_mac_header(skb); 205 ethhdr = eth_hdr(skb); 206 207 proto = ethhdr->h_proto; 208 209 switch (ntohs(proto)) { 210 case ETH_P_8021Q: 211 if (!pskb_may_pull(skb, sizeof(*vhdr))) 212 goto dropped; 213 vhdr = vlan_eth_hdr(skb); 214 proto = vhdr->h_vlan_encapsulated_proto; 215 216 /* drop batman-in-batman packets to prevent loops */ 217 if (proto != htons(ETH_P_BATMAN)) { 218 network_offset += VLAN_HLEN; 219 break; 220 } 221 222 fallthrough; 223 case ETH_P_BATMAN: 224 goto dropped; 225 } 226 227 skb_set_network_header(skb, network_offset); 228 229 if (batadv_bla_tx(bat_priv, skb, vid)) 230 goto dropped; 231 232 /* skb->data might have been reallocated by batadv_bla_tx() */ 233 ethhdr = eth_hdr(skb); 234 235 /* Register the client MAC in the transtable */ 236 if (!is_multicast_ether_addr(ethhdr->h_source) && 237 !batadv_bla_is_loopdetect_mac(ethhdr->h_source)) { 238 client_added = batadv_tt_local_add(mesh_iface, ethhdr->h_source, 239 vid, skb->skb_iif, 240 skb->mark); 241 if (!client_added) 242 goto dropped; 243 } 244 245 /* Snoop address candidates from DHCPACKs for early DAT filling */ 246 batadv_dat_snoop_outgoing_dhcp_ack(bat_priv, skb, proto, vid); 247 248 /* don't accept stp packets. STP does not help in meshes. 249 * better use the bridge loop avoidance ... 250 * 251 * The same goes for ECTP sent at least by some Cisco Switches, 252 * it might confuse the mesh when used with bridge loop avoidance. 253 */ 254 if (batadv_compare_eth(ethhdr->h_dest, stp_addr)) 255 goto dropped; 256 257 if (batadv_compare_eth(ethhdr->h_dest, ectp_addr)) 258 goto dropped; 259 260 gw_mode = READ_ONCE(bat_priv->gw.mode); 261 if (is_multicast_ether_addr(ethhdr->h_dest)) { 262 /* if gw mode is off, broadcast every packet */ 263 if (gw_mode == BATADV_GW_MODE_OFF) { 264 do_bcast = true; 265 goto send; 266 } 267 268 dhcp_rcp = batadv_gw_dhcp_recipient_get(skb, &header_len, 269 chaddr); 270 /* skb->data may have been modified by 271 * batadv_gw_dhcp_recipient_get() 272 */ 273 ethhdr = eth_hdr(skb); 274 /* if gw_mode is on, broadcast any non-DHCP message. 275 * All the DHCP packets are going to be sent as unicast 276 */ 277 if (dhcp_rcp == BATADV_DHCP_NO) { 278 do_bcast = true; 279 goto send; 280 } 281 282 if (dhcp_rcp == BATADV_DHCP_TO_CLIENT) 283 dst_hint = chaddr; 284 else if ((gw_mode == BATADV_GW_MODE_SERVER) && 285 (dhcp_rcp == BATADV_DHCP_TO_SERVER)) 286 /* gateways should not forward any DHCP message if 287 * directed to a DHCP server 288 */ 289 goto dropped; 290 291 send: 292 if (do_bcast && !is_broadcast_ether_addr(ethhdr->h_dest)) { 293 forw_mode = batadv_mcast_forw_mode(bat_priv, skb, vid, 294 &mcast_is_routable); 295 switch (forw_mode) { 296 case BATADV_FORW_BCAST: 297 break; 298 case BATADV_FORW_UCASTS: 299 case BATADV_FORW_MCAST: 300 do_bcast = false; 301 break; 302 case BATADV_FORW_NONE: 303 fallthrough; 304 default: 305 goto dropped; 306 } 307 } 308 } 309 310 batadv_skb_set_priority(skb, 0); 311 312 /* ethernet packet should be broadcasted */ 313 if (do_bcast) { 314 primary_if = batadv_primary_if_get_selected(bat_priv); 315 if (!primary_if) 316 goto dropped; 317 318 /* in case of ARP request, we do not immediately broadcasti the 319 * packet, instead we first wait for DAT to try to retrieve the 320 * correct ARP entry 321 */ 322 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb)) 323 brd_delay = msecs_to_jiffies(ARP_REQ_DELAY); 324 325 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0) 326 goto dropped; 327 328 bcast_packet = (struct batadv_bcast_packet *)skb->data; 329 bcast_packet->version = BATADV_COMPAT_VERSION; 330 bcast_packet->ttl = BATADV_TTL - 1; 331 332 /* batman packet type: broadcast */ 333 bcast_packet->packet_type = BATADV_BCAST; 334 bcast_packet->reserved = 0; 335 336 /* hw address of first interface is the orig mac because only 337 * this mac is known throughout the mesh 338 */ 339 ether_addr_copy(bcast_packet->orig, 340 primary_if->net_dev->dev_addr); 341 342 /* set broadcast sequence number */ 343 seqno = atomic_inc_return(&bat_priv->bcast_seqno); 344 bcast_packet->seqno = htonl(seqno); 345 346 batadv_send_bcast_packet(bat_priv, skb, brd_delay, true); 347 /* unicast packet */ 348 } else { 349 /* DHCP packets going to a server will use the GW feature */ 350 if (dhcp_rcp == BATADV_DHCP_TO_SERVER) { 351 ret = batadv_gw_out_of_range(bat_priv, skb); 352 if (ret) 353 goto dropped; 354 ret = batadv_send_skb_via_gw(bat_priv, skb, vid); 355 } else if (forw_mode == BATADV_FORW_UCASTS) { 356 ret = batadv_mcast_forw_send(bat_priv, skb, vid, 357 mcast_is_routable); 358 } else if (forw_mode == BATADV_FORW_MCAST) { 359 ret = batadv_mcast_forw_mcsend(bat_priv, skb); 360 } else { 361 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, 362 skb)) 363 goto dropped; 364 365 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb); 366 367 ret = batadv_send_skb_via_tt(bat_priv, skb, dst_hint, 368 vid); 369 } 370 if (ret != NET_XMIT_SUCCESS) 371 goto dropped_freed; 372 } 373 374 batadv_inc_counter(bat_priv, BATADV_CNT_TX); 375 batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len); 376 goto end; 377 378 dropped: 379 kfree_skb(skb); 380 dropped_freed: 381 batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED); 382 end: 383 batadv_hardif_put(primary_if); 384 return NETDEV_TX_OK; 385 } 386 387 /** 388 * batadv_interface_rx() - receive ethernet frame on local batman-adv interface 389 * @mesh_iface: local interface which will receive the ethernet frame 390 * @skb: ethernet frame for @mesh_iface 391 * @hdr_size: size of already parsed batman-adv header 392 * @orig_node: originator from which the batman-adv packet was sent 393 * 394 * Sends an ethernet frame to the receive path of the local @mesh_iface. 395 * skb->data must still point to the batman-adv header with the size @hdr_size. 396 * The caller has to have parsed this header already and made sure that at least 397 * @hdr_size bytes are still available for pull in @skb. 398 * 399 * The packet may still get dropped. This can happen when the encapsulated 400 * ethernet frame is invalid or contains again a batman-adv packet. Also 401 * unicast packets will be dropped directly when they were sent between two 402 * isolated clients. 403 */ 404 void batadv_interface_rx(struct net_device *mesh_iface, 405 struct sk_buff *skb, int hdr_size, 406 struct batadv_orig_node *orig_node) 407 { 408 struct batadv_bcast_packet *batadv_bcast_packet; 409 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 410 struct vlan_ethhdr *vhdr; 411 struct ethhdr *ethhdr; 412 unsigned short vid; 413 int packet_type; 414 415 batadv_bcast_packet = (struct batadv_bcast_packet *)skb->data; 416 packet_type = batadv_bcast_packet->packet_type; 417 418 skb_pull_rcsum(skb, hdr_size); 419 skb_reset_mac_header(skb); 420 421 /* clean the netfilter state now that the batman-adv header has been 422 * removed 423 */ 424 nf_reset_ct(skb); 425 426 if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 427 goto dropped; 428 429 vid = batadv_get_vid(skb, 0); 430 ethhdr = eth_hdr(skb); 431 432 switch (ntohs(ethhdr->h_proto)) { 433 case ETH_P_8021Q: 434 if (!pskb_may_pull(skb, VLAN_ETH_HLEN)) 435 goto dropped; 436 437 vhdr = skb_vlan_eth_hdr(skb); 438 439 /* drop batman-in-batman packets to prevent loops */ 440 if (vhdr->h_vlan_encapsulated_proto != htons(ETH_P_BATMAN)) 441 break; 442 443 fallthrough; 444 case ETH_P_BATMAN: 445 goto dropped; 446 } 447 448 /* skb->dev & skb->pkt_type are set here */ 449 skb->protocol = eth_type_trans(skb, mesh_iface); 450 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 451 452 batadv_inc_counter(bat_priv, BATADV_CNT_RX); 453 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES, 454 skb->len + ETH_HLEN); 455 456 /* Let the bridge loop avoidance check the packet. If will 457 * not handle it, we can safely push it up. 458 */ 459 if (batadv_bla_rx(bat_priv, skb, vid, packet_type)) 460 goto out; 461 462 if (orig_node) 463 batadv_tt_add_temporary_global_entry(bat_priv, orig_node, 464 ethhdr->h_source, vid); 465 466 if (is_multicast_ether_addr(ethhdr->h_dest)) { 467 /* set the mark on broadcast packets if AP isolation is ON and 468 * the packet is coming from an "isolated" client 469 */ 470 if (batadv_vlan_ap_isola_get(bat_priv, vid) && 471 batadv_tt_global_is_isolated(bat_priv, ethhdr->h_source, 472 vid)) { 473 /* save bits in skb->mark not covered by the mask and 474 * apply the mark on the rest 475 */ 476 skb->mark &= ~bat_priv->isolation_mark_mask; 477 skb->mark |= bat_priv->isolation_mark; 478 } 479 } else if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, 480 ethhdr->h_dest, vid)) { 481 goto dropped; 482 } 483 484 netif_rx(skb); 485 goto out; 486 487 dropped: 488 kfree_skb(skb); 489 out: 490 return; 491 } 492 493 /** 494 * batadv_meshif_vlan_release() - release vlan from lists and queue for free 495 * after rcu grace period 496 * @ref: kref pointer of the vlan object 497 */ 498 void batadv_meshif_vlan_release(struct kref *ref) 499 { 500 struct batadv_meshif_vlan *vlan; 501 502 vlan = container_of(ref, struct batadv_meshif_vlan, refcount); 503 504 spin_lock_bh(&vlan->bat_priv->meshif_vlan_list_lock); 505 hlist_del_rcu(&vlan->list); 506 spin_unlock_bh(&vlan->bat_priv->meshif_vlan_list_lock); 507 508 kfree_rcu(vlan, rcu); 509 } 510 511 /** 512 * batadv_meshif_vlan_get() - get the vlan object for a specific vid 513 * @bat_priv: the bat priv with all the mesh interface information 514 * @vid: the identifier of the vlan object to retrieve 515 * 516 * Return: the private data of the vlan matching the vid passed as argument or 517 * NULL otherwise. The refcounter of the returned object is incremented by 1. 518 */ 519 struct batadv_meshif_vlan *batadv_meshif_vlan_get(struct batadv_priv *bat_priv, 520 unsigned short vid) 521 { 522 struct batadv_meshif_vlan *vlan_tmp, *vlan = NULL; 523 524 rcu_read_lock(); 525 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->meshif_vlan_list, list) { 526 if (vlan_tmp->vid != vid) 527 continue; 528 529 if (!kref_get_unless_zero(&vlan_tmp->refcount)) 530 continue; 531 532 vlan = vlan_tmp; 533 break; 534 } 535 rcu_read_unlock(); 536 537 return vlan; 538 } 539 540 /** 541 * batadv_meshif_create_vlan() - allocate the needed resources for a new vlan 542 * @bat_priv: the bat priv with all the mesh interface information 543 * @vid: the VLAN identifier 544 * 545 * Return: 0 on success, a negative error otherwise. 546 */ 547 int batadv_meshif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid) 548 { 549 struct batadv_meshif_vlan *vlan; 550 551 spin_lock_bh(&bat_priv->meshif_vlan_list_lock); 552 553 vlan = batadv_meshif_vlan_get(bat_priv, vid); 554 if (vlan) { 555 batadv_meshif_vlan_put(vlan); 556 spin_unlock_bh(&bat_priv->meshif_vlan_list_lock); 557 return -EEXIST; 558 } 559 560 vlan = kzalloc_obj(*vlan, GFP_ATOMIC); 561 if (!vlan) { 562 spin_unlock_bh(&bat_priv->meshif_vlan_list_lock); 563 return -ENOMEM; 564 } 565 566 vlan->bat_priv = bat_priv; 567 vlan->vid = vid; 568 kref_init(&vlan->refcount); 569 570 WRITE_ONCE(vlan->ap_isolation, 0); 571 572 kref_get(&vlan->refcount); 573 hlist_add_head_rcu(&vlan->list, &bat_priv->meshif_vlan_list); 574 spin_unlock_bh(&bat_priv->meshif_vlan_list_lock); 575 576 /* add a new TT local entry. This one will be marked with the NOPURGE 577 * flag 578 */ 579 batadv_tt_local_add(bat_priv->mesh_iface, 580 bat_priv->mesh_iface->dev_addr, vid, 581 BATADV_NULL_IFINDEX, BATADV_NO_MARK); 582 583 /* don't return reference to new meshif_vlan */ 584 batadv_meshif_vlan_put(vlan); 585 586 return 0; 587 } 588 589 /** 590 * batadv_meshif_destroy_vlan() - remove and destroy a meshif_vlan object 591 * @bat_priv: the bat priv with all the mesh interface information 592 * @vlan: the object to remove 593 */ 594 static void batadv_meshif_destroy_vlan(struct batadv_priv *bat_priv, 595 struct batadv_meshif_vlan *vlan) 596 { 597 /* explicitly remove the associated TT local entry because it is marked 598 * with the NOPURGE flag 599 */ 600 batadv_tt_local_remove(bat_priv, bat_priv->mesh_iface->dev_addr, 601 vlan->vid, "vlan interface destroyed", false); 602 603 batadv_meshif_vlan_put(vlan); 604 } 605 606 /** 607 * batadv_interface_add_vid() - ndo_add_vid API implementation 608 * @dev: the netdev of the mesh interface 609 * @proto: protocol of the vlan id 610 * @vid: identifier of the new vlan 611 * 612 * Set up all the internal structures for handling the new vlan on top of the 613 * mesh interface 614 * 615 * Return: 0 on success or a negative error code in case of failure. 616 */ 617 static int batadv_interface_add_vid(struct net_device *dev, __be16 proto, 618 unsigned short vid) 619 { 620 struct batadv_priv *bat_priv = netdev_priv(dev); 621 struct batadv_meshif_vlan *vlan; 622 623 /* only 802.1Q vlans are supported. 624 * batman-adv does not know how to handle other types 625 */ 626 if (proto != htons(ETH_P_8021Q)) 627 return -EINVAL; 628 629 /* VID 0 is only used to indicate "priority tag" frames which only 630 * contain priority information and no VID. No management structures 631 * should be created for this VID and it should be handled like an 632 * untagged frame. 633 */ 634 if (vid == 0) 635 return 0; 636 637 vid |= BATADV_VLAN_HAS_TAG; 638 639 /* if a new vlan is getting created and it already exists, it means that 640 * it was not deleted yet. batadv_meshif_vlan_get() increases the 641 * refcount in order to revive the object. 642 * 643 * if it does not exist then create it. 644 */ 645 vlan = batadv_meshif_vlan_get(bat_priv, vid); 646 if (!vlan) 647 return batadv_meshif_create_vlan(bat_priv, vid); 648 649 /* add a new TT local entry. This one will be marked with the NOPURGE 650 * flag. This must be added again, even if the vlan object already 651 * exists, because the entry was deleted by kill_vid() 652 */ 653 batadv_tt_local_add(bat_priv->mesh_iface, 654 bat_priv->mesh_iface->dev_addr, vid, 655 BATADV_NULL_IFINDEX, BATADV_NO_MARK); 656 657 return 0; 658 } 659 660 /** 661 * batadv_interface_kill_vid() - ndo_kill_vid API implementation 662 * @dev: the netdev of the mesh interface 663 * @proto: protocol of the vlan id 664 * @vid: identifier of the deleted vlan 665 * 666 * Destroy all the internal structures used to handle the vlan identified by vid 667 * on top of the mesh interface 668 * 669 * Return: 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q 670 * or -ENOENT if the specified vlan id wasn't registered. 671 */ 672 static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto, 673 unsigned short vid) 674 { 675 struct batadv_priv *bat_priv = netdev_priv(dev); 676 struct batadv_meshif_vlan *vlan; 677 678 /* only 802.1Q vlans are supported. batman-adv does not know how to 679 * handle other types 680 */ 681 if (proto != htons(ETH_P_8021Q)) 682 return -EINVAL; 683 684 /* "priority tag" frames are handled like "untagged" frames 685 * and no meshif_vlan needs to be destroyed 686 */ 687 if (vid == 0) 688 return 0; 689 690 vlan = batadv_meshif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG); 691 if (!vlan) 692 return -ENOENT; 693 694 batadv_meshif_destroy_vlan(bat_priv, vlan); 695 696 /* finally free the vlan object */ 697 batadv_meshif_vlan_put(vlan); 698 699 return 0; 700 } 701 702 /* batman-adv network devices have devices nesting below it and are a special 703 * "super class" of normal network devices; split their locks off into a 704 * separate class since they always nest. 705 */ 706 static struct lock_class_key batadv_netdev_xmit_lock_key; 707 static struct lock_class_key batadv_netdev_addr_lock_key; 708 709 /** 710 * batadv_set_lockdep_class_one() - Set lockdep class for a single tx queue 711 * @dev: device which owns the tx queue 712 * @txq: tx queue to modify 713 * @_unused: always NULL 714 */ 715 static void batadv_set_lockdep_class_one(struct net_device *dev, 716 struct netdev_queue *txq, 717 void *_unused) 718 { 719 lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key); 720 } 721 722 /** 723 * batadv_set_lockdep_class() - Set txq and addr_list lockdep class 724 * @dev: network device to modify 725 */ 726 static void batadv_set_lockdep_class(struct net_device *dev) 727 { 728 lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key); 729 netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL); 730 } 731 732 /** 733 * batadv_meshif_init_late() - late stage initialization of mesh interface 734 * @dev: registered network device to modify 735 * 736 * Return: error code on failures 737 */ 738 static int batadv_meshif_init_late(struct net_device *dev) 739 { 740 struct batadv_priv *bat_priv; 741 u32 random_seqno; 742 int ret; 743 size_t cnt_len = sizeof(u64) * BATADV_CNT_NUM; 744 745 batadv_set_lockdep_class(dev); 746 747 bat_priv = netdev_priv(dev); 748 bat_priv->mesh_iface = dev; 749 750 /* batadv_interface_stats() needs to be available as soon as 751 * register_netdevice() has been called 752 */ 753 bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(u64)); 754 if (!bat_priv->bat_counters) 755 return -ENOMEM; 756 757 WRITE_ONCE(bat_priv->aggregated_ogms, 1); 758 WRITE_ONCE(bat_priv->bonding, 0); 759 #ifdef CONFIG_BATMAN_ADV_BLA 760 WRITE_ONCE(bat_priv->bridge_loop_avoidance, 1); 761 #endif 762 #ifdef CONFIG_BATMAN_ADV_DAT 763 WRITE_ONCE(bat_priv->distributed_arp_table, 1); 764 #endif 765 #ifdef CONFIG_BATMAN_ADV_MCAST 766 WRITE_ONCE(bat_priv->multicast_mode, 1); 767 WRITE_ONCE(bat_priv->multicast_fanout, 16); 768 atomic_set(&bat_priv->mcast.num_want_all_unsnoopables, 0); 769 atomic_set(&bat_priv->mcast.num_want_all_ipv4, 0); 770 atomic_set(&bat_priv->mcast.num_want_all_ipv6, 0); 771 atomic_set(&bat_priv->mcast.num_no_mc_ptype_capa, 0); 772 #endif 773 WRITE_ONCE(bat_priv->gw.mode, BATADV_GW_MODE_OFF); 774 WRITE_ONCE(bat_priv->gw.bandwidth_down, 100); 775 WRITE_ONCE(bat_priv->gw.bandwidth_up, 20); 776 WRITE_ONCE(bat_priv->orig_interval, 1000); 777 WRITE_ONCE(bat_priv->hop_penalty, 30); 778 #ifdef CONFIG_BATMAN_ADV_DEBUG 779 WRITE_ONCE(bat_priv->log_level, 0); 780 #endif 781 WRITE_ONCE(bat_priv->fragmentation, 1); 782 WRITE_ONCE(bat_priv->packet_size_max, BATADV_MAX_MTU); 783 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN); 784 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN); 785 786 WRITE_ONCE(bat_priv->mesh_state, BATADV_MESH_INACTIVE); 787 atomic_set(&bat_priv->bcast_seqno, 1); 788 atomic_set(&bat_priv->tt.vn, 0); 789 atomic_set(&bat_priv->tt.ogm_append_cnt, 0); 790 #ifdef CONFIG_BATMAN_ADV_BLA 791 atomic_set(&bat_priv->bla.num_requests, 0); 792 spin_lock_init(&bat_priv->bla.num_requests_lock); 793 #endif 794 atomic_set(&bat_priv->tp_num, 0); 795 796 WRITE_ONCE(bat_priv->tt.local_changes, 0); 797 bat_priv->tt.last_changeset = NULL; 798 bat_priv->tt.last_changeset_len = 0; 799 bat_priv->isolation_mark = 0; 800 bat_priv->isolation_mark_mask = 0; 801 802 /* randomize initial seqno to avoid collision */ 803 get_random_bytes(&random_seqno, sizeof(random_seqno)); 804 atomic_set(&bat_priv->frag_seqno, random_seqno); 805 806 bat_priv->primary_if = NULL; 807 808 if (!bat_priv->algo_ops) { 809 ret = batadv_algo_select(bat_priv, batadv_routing_algo); 810 if (ret < 0) 811 goto free_bat_counters; 812 } 813 814 ret = batadv_mesh_init(dev); 815 if (ret < 0) 816 goto free_bat_counters; 817 818 return 0; 819 820 free_bat_counters: 821 free_percpu(bat_priv->bat_counters); 822 bat_priv->bat_counters = NULL; 823 824 return ret; 825 } 826 827 /** 828 * batadv_meshif_slave_add() - Add a slave interface to a batadv_mesh_interface 829 * @dev: batadv_mesh_interface used as master interface 830 * @slave_dev: net_device which should become the slave interface 831 * @extack: extended ACK report struct 832 * 833 * Return: 0 if successful or error otherwise. 834 */ 835 static int batadv_meshif_slave_add(struct net_device *dev, 836 struct net_device *slave_dev, 837 struct netlink_ext_ack *extack) 838 { 839 struct batadv_hard_iface *hard_iface; 840 int ret = -EINVAL; 841 842 hard_iface = batadv_hardif_get_by_netdev(slave_dev); 843 if (!hard_iface || hard_iface->mesh_iface) 844 goto out; 845 846 ret = batadv_hardif_enable_interface(hard_iface, dev); 847 848 out: 849 batadv_hardif_put(hard_iface); 850 return ret; 851 } 852 853 /** 854 * batadv_meshif_slave_del() - Delete a slave iface from a batadv_mesh_interface 855 * @dev: batadv_mesh_interface used as master interface 856 * @slave_dev: net_device which should be removed from the master interface 857 * 858 * Return: 0 if successful or error otherwise. 859 */ 860 static int batadv_meshif_slave_del(struct net_device *dev, 861 struct net_device *slave_dev) 862 { 863 struct batadv_hard_iface *hard_iface; 864 int ret = -EINVAL; 865 866 hard_iface = batadv_hardif_get_by_netdev(slave_dev); 867 868 if (!hard_iface || hard_iface->mesh_iface != dev) 869 goto out; 870 871 batadv_hardif_disable_interface(hard_iface); 872 ret = 0; 873 874 out: 875 batadv_hardif_put(hard_iface); 876 return ret; 877 } 878 879 static const struct net_device_ops batadv_netdev_ops = { 880 .ndo_init = batadv_meshif_init_late, 881 .ndo_get_stats = batadv_interface_stats, 882 .ndo_vlan_rx_add_vid = batadv_interface_add_vid, 883 .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid, 884 .ndo_set_mac_address = batadv_interface_set_mac_addr, 885 .ndo_change_mtu = batadv_interface_change_mtu, 886 .ndo_set_rx_mode = batadv_interface_set_rx_mode, 887 .ndo_start_xmit = batadv_interface_tx, 888 .ndo_validate_addr = eth_validate_addr, 889 .ndo_add_slave = batadv_meshif_slave_add, 890 .ndo_del_slave = batadv_meshif_slave_del, 891 }; 892 893 static void batadv_get_drvinfo(struct net_device *dev, 894 struct ethtool_drvinfo *info) 895 { 896 strscpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver)); 897 strscpy(info->version, init_utsname()->release, sizeof(info->version)); 898 strscpy(info->fw_version, "N/A", sizeof(info->fw_version)); 899 strscpy(info->bus_info, "batman", sizeof(info->bus_info)); 900 } 901 902 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702 903 * Declare each description string in struct.name[] to get fixed sized buffer 904 * and compile time checking for strings longer than ETH_GSTRING_LEN. 905 */ 906 static const struct { 907 const char name[ETH_GSTRING_LEN]; 908 } batadv_counters_strings[] = { 909 { "tx" }, 910 { "tx_bytes" }, 911 { "tx_dropped" }, 912 { "rx" }, 913 { "rx_bytes" }, 914 { "forward" }, 915 { "forward_bytes" }, 916 { "mgmt_tx" }, 917 { "mgmt_tx_bytes" }, 918 { "mgmt_rx" }, 919 { "mgmt_rx_bytes" }, 920 { "frag_tx" }, 921 { "frag_tx_bytes" }, 922 { "frag_rx" }, 923 { "frag_rx_bytes" }, 924 { "frag_fwd" }, 925 { "frag_fwd_bytes" }, 926 { "tt_request_tx" }, 927 { "tt_request_rx" }, 928 { "tt_response_tx" }, 929 { "tt_response_rx" }, 930 { "tt_roam_adv_tx" }, 931 { "tt_roam_adv_rx" }, 932 #ifdef CONFIG_BATMAN_ADV_MCAST 933 { "mcast_tx" }, 934 { "mcast_tx_bytes" }, 935 { "mcast_tx_local" }, 936 { "mcast_tx_local_bytes" }, 937 { "mcast_rx" }, 938 { "mcast_rx_bytes" }, 939 { "mcast_rx_local" }, 940 { "mcast_rx_local_bytes" }, 941 { "mcast_fwd" }, 942 { "mcast_fwd_bytes" }, 943 #endif 944 #ifdef CONFIG_BATMAN_ADV_DAT 945 { "dat_get_tx" }, 946 { "dat_get_rx" }, 947 { "dat_put_tx" }, 948 { "dat_put_rx" }, 949 { "dat_cached_reply_tx" }, 950 #endif 951 }; 952 953 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data) 954 { 955 if (stringset == ETH_SS_STATS) 956 memcpy(data, batadv_counters_strings, 957 sizeof(batadv_counters_strings)); 958 } 959 960 static void batadv_get_ethtool_stats(struct net_device *dev, 961 struct ethtool_stats *stats, u64 *data) 962 { 963 struct batadv_priv *bat_priv = netdev_priv(dev); 964 int i; 965 966 for (i = 0; i < BATADV_CNT_NUM; i++) 967 data[i] = batadv_sum_counter(bat_priv, i); 968 } 969 970 static int batadv_get_sset_count(struct net_device *dev, int stringset) 971 { 972 if (stringset == ETH_SS_STATS) 973 return BATADV_CNT_NUM; 974 975 return -EOPNOTSUPP; 976 } 977 978 static const struct ethtool_ops batadv_ethtool_ops = { 979 .get_drvinfo = batadv_get_drvinfo, 980 .get_link = ethtool_op_get_link, 981 .get_strings = batadv_get_strings, 982 .get_ethtool_stats = batadv_get_ethtool_stats, 983 .get_sset_count = batadv_get_sset_count, 984 }; 985 986 /** 987 * batadv_meshif_free() - Deconstructor of batadv_mesh_interface 988 * @dev: Device to cleanup and remove 989 */ 990 static void batadv_meshif_free(struct net_device *dev) 991 { 992 batadv_mesh_free(dev); 993 994 /* some scheduled RCU callbacks need the bat_priv struct to accomplish 995 * their tasks. Wait for them all to be finished before freeing the 996 * netdev and its private data (bat_priv) 997 */ 998 rcu_barrier(); 999 } 1000 1001 /** 1002 * batadv_meshif_init_early() - early stage initialization of mesh interface 1003 * @dev: registered network device to modify 1004 */ 1005 static void batadv_meshif_init_early(struct net_device *dev) 1006 { 1007 ether_setup(dev); 1008 1009 dev->netdev_ops = &batadv_netdev_ops; 1010 dev->needs_free_netdev = true; 1011 dev->priv_destructor = batadv_meshif_free; 1012 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1013 dev->priv_flags |= IFF_NO_QUEUE; 1014 dev->lltx = true; 1015 dev->netns_immutable = true; 1016 1017 /* can't call min_mtu, because the needed variables 1018 * have not been initialized yet 1019 */ 1020 dev->mtu = ETH_DATA_LEN; 1021 dev->max_mtu = BATADV_MAX_MTU; 1022 1023 /* generate random address */ 1024 eth_hw_addr_random(dev); 1025 1026 dev->ethtool_ops = &batadv_ethtool_ops; 1027 } 1028 1029 /** 1030 * batadv_meshif_validate() - validate configuration of new batadv link 1031 * @tb: IFLA_INFO_DATA netlink attributes 1032 * @data: enum batadv_ifla_attrs attributes 1033 * @extack: extended ACK report struct 1034 * 1035 * Return: 0 if successful or error otherwise. 1036 */ 1037 static int batadv_meshif_validate(struct nlattr *tb[], struct nlattr *data[], 1038 struct netlink_ext_ack *extack) 1039 { 1040 struct batadv_algo_ops *algo_ops; 1041 1042 if (!data) 1043 return 0; 1044 1045 if (data[IFLA_BATADV_ALGO_NAME]) { 1046 algo_ops = batadv_algo_get(nla_data(data[IFLA_BATADV_ALGO_NAME])); 1047 if (!algo_ops) 1048 return -EINVAL; 1049 } 1050 1051 return 0; 1052 } 1053 1054 /** 1055 * batadv_meshif_newlink() - pre-initialize and register new batadv link 1056 * @dev: network device to register 1057 * @params: rtnl newlink parameters 1058 * @extack: extended ACK report struct 1059 * 1060 * Return: 0 if successful or error otherwise. 1061 */ 1062 static int batadv_meshif_newlink(struct net_device *dev, 1063 struct rtnl_newlink_params *params, 1064 struct netlink_ext_ack *extack) 1065 { 1066 struct batadv_priv *bat_priv = netdev_priv(dev); 1067 struct nlattr **data = params->data; 1068 const char *algo_name; 1069 int err; 1070 1071 if (data && data[IFLA_BATADV_ALGO_NAME]) { 1072 algo_name = nla_data(data[IFLA_BATADV_ALGO_NAME]); 1073 err = batadv_algo_select(bat_priv, algo_name); 1074 if (err) 1075 return -EINVAL; 1076 } 1077 1078 return register_netdevice(dev); 1079 } 1080 1081 /** 1082 * batadv_meshif_destroy_netlink() - deletion of batadv_mesh_interface via 1083 * netlink 1084 * @mesh_iface: the to-be-removed batman-adv interface 1085 * @head: list pointer 1086 */ 1087 static void batadv_meshif_destroy_netlink(struct net_device *mesh_iface, 1088 struct list_head *head) 1089 { 1090 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 1091 struct batadv_hard_iface *hard_iface; 1092 struct batadv_meshif_vlan *vlan; 1093 1094 while (!list_empty(&mesh_iface->adj_list.lower)) { 1095 hard_iface = netdev_adjacent_get_private(mesh_iface->adj_list.lower.next); 1096 batadv_hardif_disable_interface(hard_iface); 1097 } 1098 1099 /* destroy the "untagged" VLAN */ 1100 vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS); 1101 if (vlan) { 1102 batadv_meshif_destroy_vlan(bat_priv, vlan); 1103 batadv_meshif_vlan_put(vlan); 1104 } 1105 1106 unregister_netdevice_queue(mesh_iface, head); 1107 } 1108 1109 /** 1110 * batadv_meshif_is_valid() - Check whether device is a batadv mesh interface 1111 * @net_dev: device which should be checked 1112 * 1113 * Return: true when net_dev is a batman-adv interface, false otherwise 1114 */ 1115 bool batadv_meshif_is_valid(const struct net_device *net_dev) 1116 { 1117 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx) 1118 return true; 1119 1120 return false; 1121 } 1122 1123 static const struct nla_policy batadv_ifla_policy[IFLA_BATADV_MAX + 1] = { 1124 [IFLA_BATADV_ALGO_NAME] = { .type = NLA_NUL_STRING }, 1125 }; 1126 1127 struct rtnl_link_ops batadv_link_ops __read_mostly = { 1128 .kind = "batadv", 1129 .priv_size = sizeof(struct batadv_priv), 1130 .setup = batadv_meshif_init_early, 1131 .maxtype = IFLA_BATADV_MAX, 1132 .policy = batadv_ifla_policy, 1133 .validate = batadv_meshif_validate, 1134 .newlink = batadv_meshif_newlink, 1135 .dellink = batadv_meshif_destroy_netlink, 1136 }; 1137