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