File Index
Files and directories
Sign in
FILE

mesh-interface.c

File preview with syntax highlighting.

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