0001
0002
0003
0004
0005
0006
0007 #include "soft-interface.h"
0008 #include "main.h"
0009
0010 #include <linux/atomic.h>
0011 #include <linux/byteorder/generic.h>
0012 #include <linux/cache.h>
0013 #include <linux/compiler.h>
0014 #include <linux/container_of.h>
0015 #include <linux/cpumask.h>
0016 #include <linux/errno.h>
0017 #include <linux/etherdevice.h>
0018 #include <linux/ethtool.h>
0019 #include <linux/gfp.h>
0020 #include <linux/if_ether.h>
0021 #include <linux/if_vlan.h>
0022 #include <linux/jiffies.h>
0023 #include <linux/kref.h>
0024 #include <linux/list.h>
0025 #include <linux/lockdep.h>
0026 #include <linux/netdevice.h>
0027 #include <linux/netlink.h>
0028 #include <linux/percpu.h>
0029 #include <linux/random.h>
0030 #include <linux/rculist.h>
0031 #include <linux/rcupdate.h>
0032 #include <linux/skbuff.h>
0033 #include <linux/slab.h>
0034 #include <linux/socket.h>
0035 #include <linux/spinlock.h>
0036 #include <linux/stddef.h>
0037 #include <linux/string.h>
0038 #include <linux/types.h>
0039 #include <net/net_namespace.h>
0040 #include <net/netlink.h>
0041 #include <uapi/linux/batadv_packet.h>
0042 #include <uapi/linux/batman_adv.h>
0043
0044 #include "bat_algo.h"
0045 #include "bridge_loop_avoidance.h"
0046 #include "distributed-arp-table.h"
0047 #include "gateway_client.h"
0048 #include "hard-interface.h"
0049 #include "multicast.h"
0050 #include "network-coding.h"
0051 #include "originator.h"
0052 #include "send.h"
0053 #include "translation-table.h"
0054
0055
0056
0057
0058
0059
0060
0061
0062 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
0063 {
0064 int result;
0065
0066
0067
0068
0069
0070
0071
0072
0073 result = skb_cow_head(skb, len);
0074 if (result < 0)
0075 return result;
0076
0077 skb_push(skb, len);
0078 return 0;
0079 }
0080
0081 static int batadv_interface_open(struct net_device *dev)
0082 {
0083 netif_start_queue(dev);
0084 return 0;
0085 }
0086
0087 static int batadv_interface_release(struct net_device *dev)
0088 {
0089 netif_stop_queue(dev);
0090 return 0;
0091 }
0092
0093
0094
0095
0096
0097
0098
0099
0100 static u64 batadv_sum_counter(struct batadv_priv *bat_priv, size_t idx)
0101 {
0102 u64 *counters, sum = 0;
0103 int cpu;
0104
0105 for_each_possible_cpu(cpu) {
0106 counters = per_cpu_ptr(bat_priv->bat_counters, cpu);
0107 sum += counters[idx];
0108 }
0109
0110 return sum;
0111 }
0112
0113 static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
0114 {
0115 struct batadv_priv *bat_priv = netdev_priv(dev);
0116 struct net_device_stats *stats = &dev->stats;
0117
0118 stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX);
0119 stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES);
0120 stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED);
0121 stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX);
0122 stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES);
0123 return stats;
0124 }
0125
0126 static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
0127 {
0128 struct batadv_priv *bat_priv = netdev_priv(dev);
0129 struct batadv_softif_vlan *vlan;
0130 struct sockaddr *addr = p;
0131 u8 old_addr[ETH_ALEN];
0132
0133 if (!is_valid_ether_addr(addr->sa_data))
0134 return -EADDRNOTAVAIL;
0135
0136 ether_addr_copy(old_addr, dev->dev_addr);
0137 eth_hw_addr_set(dev, addr->sa_data);
0138
0139
0140 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
0141 return 0;
0142
0143 rcu_read_lock();
0144 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
0145 batadv_tt_local_remove(bat_priv, old_addr, vlan->vid,
0146 "mac address changed", false);
0147 batadv_tt_local_add(dev, addr->sa_data, vlan->vid,
0148 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
0149 }
0150 rcu_read_unlock();
0151
0152 return 0;
0153 }
0154
0155 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
0156 {
0157
0158 if (new_mtu < 68 || new_mtu > batadv_hardif_min_mtu(dev))
0159 return -EINVAL;
0160
0161 dev->mtu = new_mtu;
0162
0163 return 0;
0164 }
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 static void batadv_interface_set_rx_mode(struct net_device *dev)
0175 {
0176 }
0177
0178 static netdev_tx_t batadv_interface_tx(struct sk_buff *skb,
0179 struct net_device *soft_iface)
0180 {
0181 struct ethhdr *ethhdr;
0182 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
0183 struct batadv_hard_iface *primary_if = NULL;
0184 struct batadv_bcast_packet *bcast_packet;
0185 static const u8 stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
0186 0x00, 0x00};
0187 static const u8 ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
0188 0x00, 0x00};
0189 enum batadv_dhcp_recipient dhcp_rcp = BATADV_DHCP_NO;
0190 u8 *dst_hint = NULL, chaddr[ETH_ALEN];
0191 struct vlan_ethhdr *vhdr;
0192 unsigned int header_len = 0;
0193 int data_len = skb->len, ret;
0194 unsigned long brd_delay = 0;
0195 bool do_bcast = false, client_added;
0196 unsigned short vid;
0197 u32 seqno;
0198 int gw_mode;
0199 enum batadv_forw_mode forw_mode = BATADV_FORW_SINGLE;
0200 struct batadv_orig_node *mcast_single_orig = NULL;
0201 int mcast_is_routable = 0;
0202 int network_offset = ETH_HLEN;
0203 __be16 proto;
0204
0205 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
0206 goto dropped;
0207
0208
0209 memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
0210
0211 netif_trans_update(soft_iface);
0212 vid = batadv_get_vid(skb, 0);
0213
0214 skb_reset_mac_header(skb);
0215 ethhdr = eth_hdr(skb);
0216
0217 proto = ethhdr->h_proto;
0218
0219 switch (ntohs(proto)) {
0220 case ETH_P_8021Q:
0221 if (!pskb_may_pull(skb, sizeof(*vhdr)))
0222 goto dropped;
0223 vhdr = vlan_eth_hdr(skb);
0224 proto = vhdr->h_vlan_encapsulated_proto;
0225
0226
0227 if (proto != htons(ETH_P_BATMAN)) {
0228 network_offset += VLAN_HLEN;
0229 break;
0230 }
0231
0232 fallthrough;
0233 case ETH_P_BATMAN:
0234 goto dropped;
0235 }
0236
0237 skb_set_network_header(skb, network_offset);
0238
0239 if (batadv_bla_tx(bat_priv, skb, vid))
0240 goto dropped;
0241
0242
0243 ethhdr = eth_hdr(skb);
0244
0245
0246 if (!is_multicast_ether_addr(ethhdr->h_source) &&
0247 !batadv_bla_is_loopdetect_mac(ethhdr->h_source)) {
0248 client_added = batadv_tt_local_add(soft_iface, ethhdr->h_source,
0249 vid, skb->skb_iif,
0250 skb->mark);
0251 if (!client_added)
0252 goto dropped;
0253 }
0254
0255
0256 batadv_dat_snoop_outgoing_dhcp_ack(bat_priv, skb, proto, vid);
0257
0258
0259
0260
0261
0262
0263
0264 if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
0265 goto dropped;
0266
0267 if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
0268 goto dropped;
0269
0270 gw_mode = atomic_read(&bat_priv->gw.mode);
0271 if (is_multicast_ether_addr(ethhdr->h_dest)) {
0272
0273 if (gw_mode == BATADV_GW_MODE_OFF) {
0274 do_bcast = true;
0275 goto send;
0276 }
0277
0278 dhcp_rcp = batadv_gw_dhcp_recipient_get(skb, &header_len,
0279 chaddr);
0280
0281
0282
0283 ethhdr = eth_hdr(skb);
0284
0285
0286
0287 if (dhcp_rcp == BATADV_DHCP_NO) {
0288 do_bcast = true;
0289 goto send;
0290 }
0291
0292 if (dhcp_rcp == BATADV_DHCP_TO_CLIENT)
0293 dst_hint = chaddr;
0294 else if ((gw_mode == BATADV_GW_MODE_SERVER) &&
0295 (dhcp_rcp == BATADV_DHCP_TO_SERVER))
0296
0297
0298
0299 goto dropped;
0300
0301 send:
0302 if (do_bcast && !is_broadcast_ether_addr(ethhdr->h_dest)) {
0303 forw_mode = batadv_mcast_forw_mode(bat_priv, skb,
0304 &mcast_single_orig,
0305 &mcast_is_routable);
0306 if (forw_mode == BATADV_FORW_NONE)
0307 goto dropped;
0308
0309 if (forw_mode == BATADV_FORW_SINGLE ||
0310 forw_mode == BATADV_FORW_SOME)
0311 do_bcast = false;
0312 }
0313 }
0314
0315 batadv_skb_set_priority(skb, 0);
0316
0317
0318 if (do_bcast) {
0319 primary_if = batadv_primary_if_get_selected(bat_priv);
0320 if (!primary_if)
0321 goto dropped;
0322
0323
0324
0325
0326
0327 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
0328 brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
0329
0330 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
0331 goto dropped;
0332
0333 bcast_packet = (struct batadv_bcast_packet *)skb->data;
0334 bcast_packet->version = BATADV_COMPAT_VERSION;
0335 bcast_packet->ttl = BATADV_TTL - 1;
0336
0337
0338 bcast_packet->packet_type = BATADV_BCAST;
0339 bcast_packet->reserved = 0;
0340
0341
0342
0343
0344 ether_addr_copy(bcast_packet->orig,
0345 primary_if->net_dev->dev_addr);
0346
0347
0348 seqno = atomic_inc_return(&bat_priv->bcast_seqno);
0349 bcast_packet->seqno = htonl(seqno);
0350
0351 batadv_send_bcast_packet(bat_priv, skb, brd_delay, true);
0352
0353 } else {
0354
0355 if (dhcp_rcp == BATADV_DHCP_TO_SERVER) {
0356 ret = batadv_gw_out_of_range(bat_priv, skb);
0357 if (ret)
0358 goto dropped;
0359 ret = batadv_send_skb_via_gw(bat_priv, skb, vid);
0360 } else if (mcast_single_orig) {
0361 ret = batadv_mcast_forw_send_orig(bat_priv, skb, vid,
0362 mcast_single_orig);
0363 } else if (forw_mode == BATADV_FORW_SOME) {
0364 ret = batadv_mcast_forw_send(bat_priv, skb, vid,
0365 mcast_is_routable);
0366 } else {
0367 if (batadv_dat_snoop_outgoing_arp_request(bat_priv,
0368 skb))
0369 goto dropped;
0370
0371 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
0372
0373 ret = batadv_send_skb_via_tt(bat_priv, skb, dst_hint,
0374 vid);
0375 }
0376 if (ret != NET_XMIT_SUCCESS)
0377 goto dropped_freed;
0378 }
0379
0380 batadv_inc_counter(bat_priv, BATADV_CNT_TX);
0381 batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
0382 goto end;
0383
0384 dropped:
0385 kfree_skb(skb);
0386 dropped_freed:
0387 batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
0388 end:
0389 batadv_orig_node_put(mcast_single_orig);
0390 batadv_hardif_put(primary_if);
0391 return NETDEV_TX_OK;
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 void batadv_interface_rx(struct net_device *soft_iface,
0412 struct sk_buff *skb, int hdr_size,
0413 struct batadv_orig_node *orig_node)
0414 {
0415 struct batadv_bcast_packet *batadv_bcast_packet;
0416 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
0417 struct vlan_ethhdr *vhdr;
0418 struct ethhdr *ethhdr;
0419 unsigned short vid;
0420 int packet_type;
0421
0422 batadv_bcast_packet = (struct batadv_bcast_packet *)skb->data;
0423 packet_type = batadv_bcast_packet->packet_type;
0424
0425 skb_pull_rcsum(skb, hdr_size);
0426 skb_reset_mac_header(skb);
0427
0428
0429
0430
0431 nf_reset_ct(skb);
0432
0433 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
0434 goto dropped;
0435
0436 vid = batadv_get_vid(skb, 0);
0437 ethhdr = eth_hdr(skb);
0438
0439 switch (ntohs(ethhdr->h_proto)) {
0440 case ETH_P_8021Q:
0441 if (!pskb_may_pull(skb, VLAN_ETH_HLEN))
0442 goto dropped;
0443
0444 vhdr = (struct vlan_ethhdr *)skb->data;
0445
0446
0447 if (vhdr->h_vlan_encapsulated_proto != htons(ETH_P_BATMAN))
0448 break;
0449
0450 fallthrough;
0451 case ETH_P_BATMAN:
0452 goto dropped;
0453 }
0454
0455
0456 skb->protocol = eth_type_trans(skb, soft_iface);
0457 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
0458
0459 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
0460 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
0461 skb->len + ETH_HLEN);
0462
0463
0464
0465
0466 if (batadv_bla_rx(bat_priv, skb, vid, packet_type))
0467 goto out;
0468
0469 if (orig_node)
0470 batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
0471 ethhdr->h_source, vid);
0472
0473 if (is_multicast_ether_addr(ethhdr->h_dest)) {
0474
0475
0476
0477 if (batadv_vlan_ap_isola_get(bat_priv, vid) &&
0478 batadv_tt_global_is_isolated(bat_priv, ethhdr->h_source,
0479 vid)) {
0480
0481
0482
0483 skb->mark &= ~bat_priv->isolation_mark_mask;
0484 skb->mark |= bat_priv->isolation_mark;
0485 }
0486 } else if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source,
0487 ethhdr->h_dest, vid)) {
0488 goto dropped;
0489 }
0490
0491 netif_rx(skb);
0492 goto out;
0493
0494 dropped:
0495 kfree_skb(skb);
0496 out:
0497 return;
0498 }
0499
0500
0501
0502
0503
0504
0505 void batadv_softif_vlan_release(struct kref *ref)
0506 {
0507 struct batadv_softif_vlan *vlan;
0508
0509 vlan = container_of(ref, struct batadv_softif_vlan, refcount);
0510
0511 spin_lock_bh(&vlan->bat_priv->softif_vlan_list_lock);
0512 hlist_del_rcu(&vlan->list);
0513 spin_unlock_bh(&vlan->bat_priv->softif_vlan_list_lock);
0514
0515 kfree_rcu(vlan, rcu);
0516 }
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526 struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
0527 unsigned short vid)
0528 {
0529 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
0530
0531 rcu_read_lock();
0532 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
0533 if (vlan_tmp->vid != vid)
0534 continue;
0535
0536 if (!kref_get_unless_zero(&vlan_tmp->refcount))
0537 continue;
0538
0539 vlan = vlan_tmp;
0540 break;
0541 }
0542 rcu_read_unlock();
0543
0544 return vlan;
0545 }
0546
0547
0548
0549
0550
0551
0552
0553
0554 int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
0555 {
0556 struct batadv_softif_vlan *vlan;
0557
0558 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
0559
0560 vlan = batadv_softif_vlan_get(bat_priv, vid);
0561 if (vlan) {
0562 batadv_softif_vlan_put(vlan);
0563 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
0564 return -EEXIST;
0565 }
0566
0567 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
0568 if (!vlan) {
0569 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
0570 return -ENOMEM;
0571 }
0572
0573 vlan->bat_priv = bat_priv;
0574 vlan->vid = vid;
0575 kref_init(&vlan->refcount);
0576
0577 atomic_set(&vlan->ap_isolation, 0);
0578
0579 kref_get(&vlan->refcount);
0580 hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
0581 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
0582
0583
0584
0585
0586 batadv_tt_local_add(bat_priv->soft_iface,
0587 bat_priv->soft_iface->dev_addr, vid,
0588 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
0589
0590
0591 batadv_softif_vlan_put(vlan);
0592
0593 return 0;
0594 }
0595
0596
0597
0598
0599
0600
0601 static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
0602 struct batadv_softif_vlan *vlan)
0603 {
0604
0605
0606
0607 batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
0608 vlan->vid, "vlan interface destroyed", false);
0609
0610 batadv_softif_vlan_put(vlan);
0611 }
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624 static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
0625 unsigned short vid)
0626 {
0627 struct batadv_priv *bat_priv = netdev_priv(dev);
0628 struct batadv_softif_vlan *vlan;
0629
0630
0631
0632
0633 if (proto != htons(ETH_P_8021Q))
0634 return -EINVAL;
0635
0636 vid |= BATADV_VLAN_HAS_TAG;
0637
0638
0639
0640
0641
0642
0643
0644 vlan = batadv_softif_vlan_get(bat_priv, vid);
0645 if (!vlan)
0646 return batadv_softif_create_vlan(bat_priv, vid);
0647
0648
0649
0650
0651
0652 batadv_tt_local_add(bat_priv->soft_iface,
0653 bat_priv->soft_iface->dev_addr, vid,
0654 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
0655
0656 return 0;
0657 }
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671 static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
0672 unsigned short vid)
0673 {
0674 struct batadv_priv *bat_priv = netdev_priv(dev);
0675 struct batadv_softif_vlan *vlan;
0676
0677
0678
0679
0680 if (proto != htons(ETH_P_8021Q))
0681 return -EINVAL;
0682
0683 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
0684 if (!vlan)
0685 return -ENOENT;
0686
0687 batadv_softif_destroy_vlan(bat_priv, vlan);
0688
0689
0690 batadv_softif_vlan_put(vlan);
0691
0692 return 0;
0693 }
0694
0695
0696
0697
0698
0699 static struct lock_class_key batadv_netdev_xmit_lock_key;
0700 static struct lock_class_key batadv_netdev_addr_lock_key;
0701
0702
0703
0704
0705
0706
0707
0708 static void batadv_set_lockdep_class_one(struct net_device *dev,
0709 struct netdev_queue *txq,
0710 void *_unused)
0711 {
0712 lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
0713 }
0714
0715
0716
0717
0718
0719 static void batadv_set_lockdep_class(struct net_device *dev)
0720 {
0721 lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
0722 netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
0723 }
0724
0725
0726
0727
0728
0729
0730
0731 static int batadv_softif_init_late(struct net_device *dev)
0732 {
0733 struct batadv_priv *bat_priv;
0734 u32 random_seqno;
0735 int ret;
0736 size_t cnt_len = sizeof(u64) * BATADV_CNT_NUM;
0737
0738 batadv_set_lockdep_class(dev);
0739
0740 bat_priv = netdev_priv(dev);
0741 bat_priv->soft_iface = dev;
0742
0743
0744
0745
0746 bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(u64));
0747 if (!bat_priv->bat_counters)
0748 return -ENOMEM;
0749
0750 atomic_set(&bat_priv->aggregated_ogms, 1);
0751 atomic_set(&bat_priv->bonding, 0);
0752 #ifdef CONFIG_BATMAN_ADV_BLA
0753 atomic_set(&bat_priv->bridge_loop_avoidance, 1);
0754 #endif
0755 #ifdef CONFIG_BATMAN_ADV_DAT
0756 atomic_set(&bat_priv->distributed_arp_table, 1);
0757 #endif
0758 #ifdef CONFIG_BATMAN_ADV_MCAST
0759 atomic_set(&bat_priv->multicast_mode, 1);
0760 atomic_set(&bat_priv->multicast_fanout, 16);
0761 atomic_set(&bat_priv->mcast.num_want_all_unsnoopables, 0);
0762 atomic_set(&bat_priv->mcast.num_want_all_ipv4, 0);
0763 atomic_set(&bat_priv->mcast.num_want_all_ipv6, 0);
0764 #endif
0765 atomic_set(&bat_priv->gw.mode, BATADV_GW_MODE_OFF);
0766 atomic_set(&bat_priv->gw.bandwidth_down, 100);
0767 atomic_set(&bat_priv->gw.bandwidth_up, 20);
0768 atomic_set(&bat_priv->orig_interval, 1000);
0769 atomic_set(&bat_priv->hop_penalty, 30);
0770 #ifdef CONFIG_BATMAN_ADV_DEBUG
0771 atomic_set(&bat_priv->log_level, 0);
0772 #endif
0773 atomic_set(&bat_priv->fragmentation, 1);
0774 atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
0775 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
0776 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
0777
0778 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
0779 atomic_set(&bat_priv->bcast_seqno, 1);
0780 atomic_set(&bat_priv->tt.vn, 0);
0781 atomic_set(&bat_priv->tt.local_changes, 0);
0782 atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
0783 #ifdef CONFIG_BATMAN_ADV_BLA
0784 atomic_set(&bat_priv->bla.num_requests, 0);
0785 #endif
0786 atomic_set(&bat_priv->tp_num, 0);
0787
0788 bat_priv->tt.last_changeset = NULL;
0789 bat_priv->tt.last_changeset_len = 0;
0790 bat_priv->isolation_mark = 0;
0791 bat_priv->isolation_mark_mask = 0;
0792
0793
0794 get_random_bytes(&random_seqno, sizeof(random_seqno));
0795 atomic_set(&bat_priv->frag_seqno, random_seqno);
0796
0797 bat_priv->primary_if = NULL;
0798
0799 batadv_nc_init_bat_priv(bat_priv);
0800
0801 if (!bat_priv->algo_ops) {
0802 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
0803 if (ret < 0)
0804 goto free_bat_counters;
0805 }
0806
0807 ret = batadv_mesh_init(dev);
0808 if (ret < 0)
0809 goto free_bat_counters;
0810
0811 return 0;
0812
0813 free_bat_counters:
0814 free_percpu(bat_priv->bat_counters);
0815 bat_priv->bat_counters = NULL;
0816
0817 return ret;
0818 }
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828 static int batadv_softif_slave_add(struct net_device *dev,
0829 struct net_device *slave_dev,
0830 struct netlink_ext_ack *extack)
0831 {
0832 struct batadv_hard_iface *hard_iface;
0833 int ret = -EINVAL;
0834
0835 hard_iface = batadv_hardif_get_by_netdev(slave_dev);
0836 if (!hard_iface || hard_iface->soft_iface)
0837 goto out;
0838
0839 ret = batadv_hardif_enable_interface(hard_iface, dev);
0840
0841 out:
0842 batadv_hardif_put(hard_iface);
0843 return ret;
0844 }
0845
0846
0847
0848
0849
0850
0851
0852
0853 static int batadv_softif_slave_del(struct net_device *dev,
0854 struct net_device *slave_dev)
0855 {
0856 struct batadv_hard_iface *hard_iface;
0857 int ret = -EINVAL;
0858
0859 hard_iface = batadv_hardif_get_by_netdev(slave_dev);
0860
0861 if (!hard_iface || hard_iface->soft_iface != dev)
0862 goto out;
0863
0864 batadv_hardif_disable_interface(hard_iface);
0865 ret = 0;
0866
0867 out:
0868 batadv_hardif_put(hard_iface);
0869 return ret;
0870 }
0871
0872 static const struct net_device_ops batadv_netdev_ops = {
0873 .ndo_init = batadv_softif_init_late,
0874 .ndo_open = batadv_interface_open,
0875 .ndo_stop = batadv_interface_release,
0876 .ndo_get_stats = batadv_interface_stats,
0877 .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
0878 .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
0879 .ndo_set_mac_address = batadv_interface_set_mac_addr,
0880 .ndo_change_mtu = batadv_interface_change_mtu,
0881 .ndo_set_rx_mode = batadv_interface_set_rx_mode,
0882 .ndo_start_xmit = batadv_interface_tx,
0883 .ndo_validate_addr = eth_validate_addr,
0884 .ndo_add_slave = batadv_softif_slave_add,
0885 .ndo_del_slave = batadv_softif_slave_del,
0886 };
0887
0888 static void batadv_get_drvinfo(struct net_device *dev,
0889 struct ethtool_drvinfo *info)
0890 {
0891 strscpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
0892 strscpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
0893 strscpy(info->fw_version, "N/A", sizeof(info->fw_version));
0894 strscpy(info->bus_info, "batman", sizeof(info->bus_info));
0895 }
0896
0897
0898
0899
0900
0901 static const struct {
0902 const char name[ETH_GSTRING_LEN];
0903 } batadv_counters_strings[] = {
0904 { "tx" },
0905 { "tx_bytes" },
0906 { "tx_dropped" },
0907 { "rx" },
0908 { "rx_bytes" },
0909 { "forward" },
0910 { "forward_bytes" },
0911 { "mgmt_tx" },
0912 { "mgmt_tx_bytes" },
0913 { "mgmt_rx" },
0914 { "mgmt_rx_bytes" },
0915 { "frag_tx" },
0916 { "frag_tx_bytes" },
0917 { "frag_rx" },
0918 { "frag_rx_bytes" },
0919 { "frag_fwd" },
0920 { "frag_fwd_bytes" },
0921 { "tt_request_tx" },
0922 { "tt_request_rx" },
0923 { "tt_response_tx" },
0924 { "tt_response_rx" },
0925 { "tt_roam_adv_tx" },
0926 { "tt_roam_adv_rx" },
0927 #ifdef CONFIG_BATMAN_ADV_DAT
0928 { "dat_get_tx" },
0929 { "dat_get_rx" },
0930 { "dat_put_tx" },
0931 { "dat_put_rx" },
0932 { "dat_cached_reply_tx" },
0933 #endif
0934 #ifdef CONFIG_BATMAN_ADV_NC
0935 { "nc_code" },
0936 { "nc_code_bytes" },
0937 { "nc_recode" },
0938 { "nc_recode_bytes" },
0939 { "nc_buffer" },
0940 { "nc_decode" },
0941 { "nc_decode_bytes" },
0942 { "nc_decode_failed" },
0943 { "nc_sniffed" },
0944 #endif
0945 };
0946
0947 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data)
0948 {
0949 if (stringset == ETH_SS_STATS)
0950 memcpy(data, batadv_counters_strings,
0951 sizeof(batadv_counters_strings));
0952 }
0953
0954 static void batadv_get_ethtool_stats(struct net_device *dev,
0955 struct ethtool_stats *stats, u64 *data)
0956 {
0957 struct batadv_priv *bat_priv = netdev_priv(dev);
0958 int i;
0959
0960 for (i = 0; i < BATADV_CNT_NUM; i++)
0961 data[i] = batadv_sum_counter(bat_priv, i);
0962 }
0963
0964 static int batadv_get_sset_count(struct net_device *dev, int stringset)
0965 {
0966 if (stringset == ETH_SS_STATS)
0967 return BATADV_CNT_NUM;
0968
0969 return -EOPNOTSUPP;
0970 }
0971
0972 static const struct ethtool_ops batadv_ethtool_ops = {
0973 .get_drvinfo = batadv_get_drvinfo,
0974 .get_link = ethtool_op_get_link,
0975 .get_strings = batadv_get_strings,
0976 .get_ethtool_stats = batadv_get_ethtool_stats,
0977 .get_sset_count = batadv_get_sset_count,
0978 };
0979
0980
0981
0982
0983
0984 static void batadv_softif_free(struct net_device *dev)
0985 {
0986 batadv_mesh_free(dev);
0987
0988
0989
0990
0991
0992 rcu_barrier();
0993 }
0994
0995
0996
0997
0998
0999 static void batadv_softif_init_early(struct net_device *dev)
1000 {
1001 ether_setup(dev);
1002
1003 dev->netdev_ops = &batadv_netdev_ops;
1004 dev->needs_free_netdev = true;
1005 dev->priv_destructor = batadv_softif_free;
1006 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_NETNS_LOCAL;
1007 dev->features |= NETIF_F_LLTX;
1008 dev->priv_flags |= IFF_NO_QUEUE;
1009
1010
1011
1012
1013 dev->mtu = ETH_DATA_LEN;
1014
1015
1016 eth_hw_addr_random(dev);
1017
1018 dev->ethtool_ops = &batadv_ethtool_ops;
1019 }
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[],
1030 struct netlink_ext_ack *extack)
1031 {
1032 struct batadv_algo_ops *algo_ops;
1033
1034 if (!data)
1035 return 0;
1036
1037 if (data[IFLA_BATADV_ALGO_NAME]) {
1038 algo_ops = batadv_algo_get(nla_data(data[IFLA_BATADV_ALGO_NAME]));
1039 if (!algo_ops)
1040 return -EINVAL;
1041 }
1042
1043 return 0;
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 static int batadv_softif_newlink(struct net *src_net, struct net_device *dev,
1057 struct nlattr *tb[], struct nlattr *data[],
1058 struct netlink_ext_ack *extack)
1059 {
1060 struct batadv_priv *bat_priv = netdev_priv(dev);
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
1076
1077
1078
1079
1080 static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
1081 struct list_head *head)
1082 {
1083 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
1084 struct batadv_hard_iface *hard_iface;
1085 struct batadv_softif_vlan *vlan;
1086
1087 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
1088 if (hard_iface->soft_iface == soft_iface)
1089 batadv_hardif_disable_interface(hard_iface);
1090 }
1091
1092
1093 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
1094 if (vlan) {
1095 batadv_softif_destroy_vlan(bat_priv, vlan);
1096 batadv_softif_vlan_put(vlan);
1097 }
1098
1099 unregister_netdevice_queue(soft_iface, head);
1100 }
1101
1102
1103
1104
1105
1106
1107
1108 bool batadv_softif_is_valid(const struct net_device *net_dev)
1109 {
1110 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
1111 return true;
1112
1113 return false;
1114 }
1115
1116 static const struct nla_policy batadv_ifla_policy[IFLA_BATADV_MAX + 1] = {
1117 [IFLA_BATADV_ALGO_NAME] = { .type = NLA_NUL_STRING },
1118 };
1119
1120 struct rtnl_link_ops batadv_link_ops __read_mostly = {
1121 .kind = "batadv",
1122 .priv_size = sizeof(struct batadv_priv),
1123 .setup = batadv_softif_init_early,
1124 .maxtype = IFLA_BATADV_MAX,
1125 .policy = batadv_ifla_policy,
1126 .validate = batadv_softif_validate,
1127 .newlink = batadv_softif_newlink,
1128 .dellink = batadv_softif_destroy_netlink,
1129 };