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