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