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