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