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