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