0001
0002
0003
0004
0005
0006
0007 #include "bat_iv_ogm.h"
0008 #include "main.h"
0009
0010 #include <linux/atomic.h>
0011 #include <linux/bitmap.h>
0012 #include <linux/bitops.h>
0013 #include <linux/bug.h>
0014 #include <linux/byteorder/generic.h>
0015 #include <linux/cache.h>
0016 #include <linux/container_of.h>
0017 #include <linux/errno.h>
0018 #include <linux/etherdevice.h>
0019 #include <linux/gfp.h>
0020 #include <linux/if_ether.h>
0021 #include <linux/init.h>
0022 #include <linux/jiffies.h>
0023 #include <linux/kref.h>
0024 #include <linux/list.h>
0025 #include <linux/lockdep.h>
0026 #include <linux/mutex.h>
0027 #include <linux/netdevice.h>
0028 #include <linux/netlink.h>
0029 #include <linux/pkt_sched.h>
0030 #include <linux/prandom.h>
0031 #include <linux/printk.h>
0032 #include <linux/random.h>
0033 #include <linux/rculist.h>
0034 #include <linux/rcupdate.h>
0035 #include <linux/skbuff.h>
0036 #include <linux/slab.h>
0037 #include <linux/spinlock.h>
0038 #include <linux/stddef.h>
0039 #include <linux/string.h>
0040 #include <linux/types.h>
0041 #include <linux/workqueue.h>
0042 #include <net/genetlink.h>
0043 #include <net/netlink.h>
0044 #include <uapi/linux/batadv_packet.h>
0045 #include <uapi/linux/batman_adv.h>
0046
0047 #include "bat_algo.h"
0048 #include "bitarray.h"
0049 #include "gateway_client.h"
0050 #include "hard-interface.h"
0051 #include "hash.h"
0052 #include "log.h"
0053 #include "netlink.h"
0054 #include "network-coding.h"
0055 #include "originator.h"
0056 #include "routing.h"
0057 #include "send.h"
0058 #include "translation-table.h"
0059 #include "tvlv.h"
0060
0061 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
0062
0063
0064
0065
0066 enum batadv_dup_status {
0067
0068 BATADV_NO_DUP = 0,
0069
0070
0071
0072
0073
0074 BATADV_ORIG_DUP,
0075
0076
0077 BATADV_NEIGH_DUP,
0078
0079
0080
0081
0082 BATADV_PROTECTED,
0083 };
0084
0085
0086
0087
0088
0089
0090
0091 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
0092 {
0093 lq_recv[*lq_index] = value;
0094 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
0105 {
0106 const u8 *ptr;
0107 u16 count = 0;
0108 u16 i = 0;
0109 u16 sum = 0;
0110
0111 ptr = lq_recv;
0112
0113 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
0114 if (*ptr != 0) {
0115 count++;
0116 sum += *ptr;
0117 }
0118
0119 i++;
0120 ptr++;
0121 }
0122
0123 if (count == 0)
0124 return 0;
0125
0126 return (u8)(sum / count);
0127 }
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 static struct batadv_orig_node *
0140 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
0141 {
0142 struct batadv_orig_node *orig_node;
0143 int hash_added;
0144
0145 orig_node = batadv_orig_hash_find(bat_priv, addr);
0146 if (orig_node)
0147 return orig_node;
0148
0149 orig_node = batadv_orig_node_new(bat_priv, addr);
0150 if (!orig_node)
0151 return NULL;
0152
0153 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
0154
0155 kref_get(&orig_node->refcount);
0156 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
0157 batadv_choose_orig, orig_node,
0158 &orig_node->hash_entry);
0159 if (hash_added != 0)
0160 goto free_orig_node_hash;
0161
0162 return orig_node;
0163
0164 free_orig_node_hash:
0165
0166 batadv_orig_node_put(orig_node);
0167
0168 batadv_orig_node_put(orig_node);
0169
0170 return NULL;
0171 }
0172
0173 static struct batadv_neigh_node *
0174 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
0175 const u8 *neigh_addr,
0176 struct batadv_orig_node *orig_node,
0177 struct batadv_orig_node *orig_neigh)
0178 {
0179 struct batadv_neigh_node *neigh_node;
0180
0181 neigh_node = batadv_neigh_node_get_or_create(orig_node,
0182 hard_iface, neigh_addr);
0183 if (!neigh_node)
0184 goto out;
0185
0186 neigh_node->orig_node = orig_neigh;
0187
0188 out:
0189 return neigh_node;
0190 }
0191
0192 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
0193 {
0194 struct batadv_ogm_packet *batadv_ogm_packet;
0195 unsigned char *ogm_buff;
0196 u32 random_seqno;
0197
0198 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
0199
0200
0201 get_random_bytes(&random_seqno, sizeof(random_seqno));
0202 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
0203
0204 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
0205 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
0206 if (!ogm_buff) {
0207 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0208 return -ENOMEM;
0209 }
0210
0211 hard_iface->bat_iv.ogm_buff = ogm_buff;
0212
0213 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
0214 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
0215 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
0216 batadv_ogm_packet->ttl = 2;
0217 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
0218 batadv_ogm_packet->reserved = 0;
0219 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
0220
0221 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0222
0223 return 0;
0224 }
0225
0226 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
0227 {
0228 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
0229
0230 kfree(hard_iface->bat_iv.ogm_buff);
0231 hard_iface->bat_iv.ogm_buff = NULL;
0232
0233 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0234 }
0235
0236 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
0237 {
0238 struct batadv_ogm_packet *batadv_ogm_packet;
0239 void *ogm_buff;
0240
0241 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
0242
0243 ogm_buff = hard_iface->bat_iv.ogm_buff;
0244 if (!ogm_buff)
0245 goto unlock;
0246
0247 batadv_ogm_packet = ogm_buff;
0248 ether_addr_copy(batadv_ogm_packet->orig,
0249 hard_iface->net_dev->dev_addr);
0250 ether_addr_copy(batadv_ogm_packet->prev_sender,
0251 hard_iface->net_dev->dev_addr);
0252
0253 unlock:
0254 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0255 }
0256
0257 static void
0258 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
0259 {
0260 struct batadv_ogm_packet *batadv_ogm_packet;
0261 void *ogm_buff;
0262
0263 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
0264
0265 ogm_buff = hard_iface->bat_iv.ogm_buff;
0266 if (!ogm_buff)
0267 goto unlock;
0268
0269 batadv_ogm_packet = ogm_buff;
0270 batadv_ogm_packet->ttl = BATADV_TTL;
0271
0272 unlock:
0273 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0274 }
0275
0276
0277 static unsigned long
0278 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
0279 {
0280 unsigned int msecs;
0281
0282 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
0283 msecs += prandom_u32_max(2 * BATADV_JITTER);
0284
0285 return jiffies + msecs_to_jiffies(msecs);
0286 }
0287
0288
0289 static unsigned long batadv_iv_ogm_fwd_send_time(void)
0290 {
0291 return jiffies + msecs_to_jiffies(prandom_u32_max(BATADV_JITTER / 2));
0292 }
0293
0294
0295 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
0296 {
0297 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
0298 int new_tq;
0299
0300 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
0301 new_tq /= BATADV_TQ_MAX_VALUE;
0302
0303 return new_tq;
0304 }
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314 static bool
0315 batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
0316 const struct batadv_ogm_packet *ogm_packet)
0317 {
0318 int next_buff_pos = 0;
0319
0320
0321 next_buff_pos += buff_pos + sizeof(*ogm_packet);
0322 if (next_buff_pos > packet_len)
0323 return false;
0324
0325
0326 next_buff_pos += ntohs(ogm_packet->tvlv_len);
0327
0328 return (next_buff_pos <= packet_len) &&
0329 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
0330 }
0331
0332
0333 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
0334 struct batadv_hard_iface *hard_iface)
0335 {
0336 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
0337 const char *fwd_str;
0338 u8 packet_num;
0339 s16 buff_pos;
0340 struct batadv_ogm_packet *batadv_ogm_packet;
0341 struct sk_buff *skb;
0342 u8 *packet_pos;
0343
0344 if (hard_iface->if_status != BATADV_IF_ACTIVE)
0345 return;
0346
0347 packet_num = 0;
0348 buff_pos = 0;
0349 packet_pos = forw_packet->skb->data;
0350 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
0351
0352
0353 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
0354 batadv_ogm_packet)) {
0355
0356
0357
0358 if (forw_packet->direct_link_flags & BIT(packet_num) &&
0359 forw_packet->if_incoming == hard_iface)
0360 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
0361 else
0362 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
0363
0364 if (packet_num > 0 || !forw_packet->own)
0365 fwd_str = "Forwarding";
0366 else
0367 fwd_str = "Sending own";
0368
0369 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0370 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
0371 fwd_str, (packet_num > 0 ? "aggregated " : ""),
0372 batadv_ogm_packet->orig,
0373 ntohl(batadv_ogm_packet->seqno),
0374 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
0375 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
0376 "on" : "off"),
0377 hard_iface->net_dev->name,
0378 hard_iface->net_dev->dev_addr);
0379
0380 buff_pos += BATADV_OGM_HLEN;
0381 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
0382 packet_num++;
0383 packet_pos = forw_packet->skb->data + buff_pos;
0384 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
0385 }
0386
0387
0388 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
0389 if (skb) {
0390 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
0391 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
0392 skb->len + ETH_HLEN);
0393 batadv_send_broadcast_skb(skb, hard_iface);
0394 }
0395 }
0396
0397
0398 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
0399 {
0400 struct net_device *soft_iface;
0401
0402 if (!forw_packet->if_incoming) {
0403 pr_err("Error - can't forward packet: incoming iface not specified\n");
0404 return;
0405 }
0406
0407 soft_iface = forw_packet->if_incoming->soft_iface;
0408
0409 if (WARN_ON(!forw_packet->if_outgoing))
0410 return;
0411
0412 if (forw_packet->if_outgoing->soft_iface != soft_iface) {
0413 pr_warn("%s: soft interface switch for queued OGM\n", __func__);
0414 return;
0415 }
0416
0417 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
0418 return;
0419
0420
0421 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
0422 }
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438 static bool
0439 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
0440 struct batadv_priv *bat_priv,
0441 int packet_len, unsigned long send_time,
0442 bool directlink,
0443 const struct batadv_hard_iface *if_incoming,
0444 const struct batadv_hard_iface *if_outgoing,
0445 const struct batadv_forw_packet *forw_packet)
0446 {
0447 struct batadv_ogm_packet *batadv_ogm_packet;
0448 int aggregated_bytes = forw_packet->packet_len + packet_len;
0449 struct batadv_hard_iface *primary_if = NULL;
0450 bool res = false;
0451 unsigned long aggregation_end_time;
0452
0453 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
0454 aggregation_end_time = send_time;
0455 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465 if (!time_before(send_time, forw_packet->send_time) ||
0466 !time_after_eq(aggregation_end_time, forw_packet->send_time))
0467 return false;
0468
0469 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
0470 return false;
0471
0472
0473 if (forw_packet->if_outgoing != if_outgoing)
0474 return false;
0475
0476
0477
0478
0479
0480
0481
0482
0483 primary_if = batadv_primary_if_get_selected(bat_priv);
0484 if (!primary_if)
0485 return false;
0486
0487
0488
0489
0490 if (!directlink &&
0491 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
0492 batadv_ogm_packet->ttl != 1 &&
0493
0494
0495
0496
0497 (!forw_packet->own ||
0498 forw_packet->if_incoming == primary_if)) {
0499 res = true;
0500 goto out;
0501 }
0502
0503
0504
0505
0506 if (directlink &&
0507 new_bat_ogm_packet->ttl == 1 &&
0508 forw_packet->if_incoming == if_incoming &&
0509
0510
0511
0512
0513
0514 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
0515 (forw_packet->own &&
0516 forw_packet->if_incoming != primary_if))) {
0517 res = true;
0518 goto out;
0519 }
0520
0521 out:
0522 batadv_hardif_put(primary_if);
0523 return res;
0524 }
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
0538 int packet_len, unsigned long send_time,
0539 bool direct_link,
0540 struct batadv_hard_iface *if_incoming,
0541 struct batadv_hard_iface *if_outgoing,
0542 int own_packet)
0543 {
0544 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
0545 struct batadv_forw_packet *forw_packet_aggr;
0546 struct sk_buff *skb;
0547 unsigned char *skb_buff;
0548 unsigned int skb_size;
0549 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
0550
0551 if (atomic_read(&bat_priv->aggregated_ogms) &&
0552 packet_len < BATADV_MAX_AGGREGATION_BYTES)
0553 skb_size = BATADV_MAX_AGGREGATION_BYTES;
0554 else
0555 skb_size = packet_len;
0556
0557 skb_size += ETH_HLEN;
0558
0559 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
0560 if (!skb)
0561 return;
0562
0563 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
0564 queue_left, bat_priv, skb);
0565 if (!forw_packet_aggr) {
0566 kfree_skb(skb);
0567 return;
0568 }
0569
0570 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
0571 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
0572
0573 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
0574 forw_packet_aggr->packet_len = packet_len;
0575 memcpy(skb_buff, packet_buff, packet_len);
0576
0577 forw_packet_aggr->own = own_packet;
0578 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
0579 forw_packet_aggr->send_time = send_time;
0580
0581
0582 if (direct_link)
0583 forw_packet_aggr->direct_link_flags |= 1;
0584
0585 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
0586 batadv_iv_send_outstanding_bat_ogm_packet);
0587
0588 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
0589 }
0590
0591
0592 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
0593 const unsigned char *packet_buff,
0594 int packet_len, bool direct_link)
0595 {
0596 unsigned long new_direct_link_flag;
0597
0598 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
0599 forw_packet_aggr->packet_len += packet_len;
0600 forw_packet_aggr->num_packets++;
0601
0602
0603 if (direct_link) {
0604 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
0605 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
0606 }
0607 }
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
0620 unsigned char *packet_buff,
0621 int packet_len,
0622 struct batadv_hard_iface *if_incoming,
0623 struct batadv_hard_iface *if_outgoing,
0624 int own_packet, unsigned long send_time)
0625 {
0626
0627
0628
0629 struct batadv_forw_packet *forw_packet_aggr = NULL;
0630 struct batadv_forw_packet *forw_packet_pos = NULL;
0631 struct batadv_ogm_packet *batadv_ogm_packet;
0632 bool direct_link;
0633 unsigned long max_aggregation_jiffies;
0634
0635 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
0636 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
0637 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
0638
0639
0640 spin_lock_bh(&bat_priv->forw_bat_list_lock);
0641
0642 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
0643 hlist_for_each_entry(forw_packet_pos,
0644 &bat_priv->forw_bat_list, list) {
0645 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
0646 bat_priv, packet_len,
0647 send_time, direct_link,
0648 if_incoming,
0649 if_outgoing,
0650 forw_packet_pos)) {
0651 forw_packet_aggr = forw_packet_pos;
0652 break;
0653 }
0654 }
0655 }
0656
0657
0658
0659
0660 if (!forw_packet_aggr) {
0661
0662 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
0663
0664
0665
0666
0667
0668 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
0669 send_time += max_aggregation_jiffies;
0670
0671 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
0672 send_time, direct_link,
0673 if_incoming, if_outgoing,
0674 own_packet);
0675 } else {
0676 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
0677 packet_len, direct_link);
0678 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
0679 }
0680 }
0681
0682 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
0683 const struct ethhdr *ethhdr,
0684 struct batadv_ogm_packet *batadv_ogm_packet,
0685 bool is_single_hop_neigh,
0686 bool is_from_best_next_hop,
0687 struct batadv_hard_iface *if_incoming,
0688 struct batadv_hard_iface *if_outgoing)
0689 {
0690 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
0691 u16 tvlv_len;
0692
0693 if (batadv_ogm_packet->ttl <= 1) {
0694 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
0695 return;
0696 }
0697
0698 if (!is_from_best_next_hop) {
0699
0700
0701
0702
0703
0704
0705 if (is_single_hop_neigh)
0706 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
0707 else
0708 return;
0709 }
0710
0711 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
0712
0713 batadv_ogm_packet->ttl--;
0714 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
0715
0716
0717 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
0718 bat_priv);
0719
0720 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0721 "Forwarding packet: tq: %i, ttl: %i\n",
0722 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
0723
0724 if (is_single_hop_neigh)
0725 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
0726 else
0727 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
0728
0729 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
0730 BATADV_OGM_HLEN + tvlv_len,
0731 if_incoming, if_outgoing, 0,
0732 batadv_iv_ogm_fwd_send_time());
0733 }
0734
0735
0736
0737
0738
0739
0740 static void
0741 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
0742 {
0743 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
0744 struct batadv_hashtable *hash = bat_priv->orig_hash;
0745 struct hlist_head *head;
0746 struct batadv_orig_node *orig_node;
0747 struct batadv_orig_ifinfo *orig_ifinfo;
0748 unsigned long *word;
0749 u32 i;
0750 u8 *w;
0751
0752 for (i = 0; i < hash->size; i++) {
0753 head = &hash->table[i];
0754
0755 rcu_read_lock();
0756 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
0757 hlist_for_each_entry_rcu(orig_ifinfo,
0758 &orig_node->ifinfo_list,
0759 list) {
0760 if (orig_ifinfo->if_outgoing != hard_iface)
0761 continue;
0762
0763 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
0764 word = orig_ifinfo->bat_iv.bcast_own;
0765 batadv_bit_get_packet(bat_priv, word, 1, 0);
0766 w = &orig_ifinfo->bat_iv.bcast_own_sum;
0767 *w = bitmap_weight(word,
0768 BATADV_TQ_LOCAL_WINDOW_SIZE);
0769 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
0770 }
0771 }
0772 rcu_read_unlock();
0773 }
0774 }
0775
0776
0777
0778
0779
0780 static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
0781 {
0782 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
0783 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
0784 struct batadv_ogm_packet *batadv_ogm_packet;
0785 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
0786 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
0787 u32 seqno;
0788 u16 tvlv_len = 0;
0789 unsigned long send_time;
0790
0791 lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
0792
0793
0794 if (!*ogm_buff)
0795 return;
0796
0797
0798
0799
0800
0801
0802
0803 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
0804 hard_iface->if_status = BATADV_IF_ACTIVE;
0805
0806 primary_if = batadv_primary_if_get_selected(bat_priv);
0807
0808 if (hard_iface == primary_if) {
0809
0810
0811
0812 batadv_tt_local_commit_changes(bat_priv);
0813 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
0814 ogm_buff_len,
0815 BATADV_OGM_HLEN);
0816 }
0817
0818 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
0819 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
0820
0821
0822 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
0823 batadv_ogm_packet->seqno = htonl(seqno);
0824 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
0825
0826 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
0827
0828 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
0829
0830 if (hard_iface != primary_if) {
0831
0832
0833
0834 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
0835 hard_iface, hard_iface, 1, send_time);
0836 goto out;
0837 }
0838
0839
0840
0841
0842 rcu_read_lock();
0843 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
0844 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
0845 continue;
0846
0847 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
0848 continue;
0849
0850 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
0851 *ogm_buff_len, hard_iface,
0852 tmp_hard_iface, 1, send_time);
0853
0854 batadv_hardif_put(tmp_hard_iface);
0855 }
0856 rcu_read_unlock();
0857
0858 out:
0859 batadv_hardif_put(primary_if);
0860 }
0861
0862 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
0863 {
0864 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
0865 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
0866 return;
0867
0868 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
0869 batadv_iv_ogm_schedule_buff(hard_iface);
0870 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
0871 }
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883 static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
0884 struct batadv_hard_iface *if_outgoing)
0885 {
0886 struct batadv_orig_ifinfo *orig_ifinfo;
0887 u8 sum;
0888
0889 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
0890 if (!orig_ifinfo)
0891 return 0;
0892
0893 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
0894 sum = orig_ifinfo->bat_iv.bcast_own_sum;
0895 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
0896
0897 batadv_orig_ifinfo_put(orig_ifinfo);
0898
0899 return sum;
0900 }
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914 static void
0915 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
0916 struct batadv_orig_node *orig_node,
0917 struct batadv_orig_ifinfo *orig_ifinfo,
0918 const struct ethhdr *ethhdr,
0919 const struct batadv_ogm_packet *batadv_ogm_packet,
0920 struct batadv_hard_iface *if_incoming,
0921 struct batadv_hard_iface *if_outgoing,
0922 enum batadv_dup_status dup_status)
0923 {
0924 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
0925 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
0926 struct batadv_neigh_node *neigh_node = NULL;
0927 struct batadv_neigh_node *tmp_neigh_node = NULL;
0928 struct batadv_neigh_node *router = NULL;
0929 u8 sum_orig, sum_neigh;
0930 u8 *neigh_addr;
0931 u8 tq_avg;
0932
0933 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0934 "%s(): Searching and updating originator entry of received packet\n",
0935 __func__);
0936
0937 rcu_read_lock();
0938 hlist_for_each_entry_rcu(tmp_neigh_node,
0939 &orig_node->neigh_list, list) {
0940 neigh_addr = tmp_neigh_node->addr;
0941 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
0942 tmp_neigh_node->if_incoming == if_incoming &&
0943 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
0944 if (WARN(neigh_node, "too many matching neigh_nodes"))
0945 batadv_neigh_node_put(neigh_node);
0946 neigh_node = tmp_neigh_node;
0947 continue;
0948 }
0949
0950 if (dup_status != BATADV_NO_DUP)
0951 continue;
0952
0953
0954 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
0955 if_outgoing);
0956 if (!neigh_ifinfo)
0957 continue;
0958
0959 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
0960 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
0961 &neigh_ifinfo->bat_iv.tq_index, 0);
0962 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
0963 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
0964 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
0965
0966 batadv_neigh_ifinfo_put(neigh_ifinfo);
0967 neigh_ifinfo = NULL;
0968 }
0969
0970 if (!neigh_node) {
0971 struct batadv_orig_node *orig_tmp;
0972
0973 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
0974 if (!orig_tmp)
0975 goto unlock;
0976
0977 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
0978 ethhdr->h_source,
0979 orig_node, orig_tmp);
0980
0981 batadv_orig_node_put(orig_tmp);
0982 if (!neigh_node)
0983 goto unlock;
0984 } else {
0985 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0986 "Updating existing last-hop neighbor of originator\n");
0987 }
0988
0989 rcu_read_unlock();
0990 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
0991 if (!neigh_ifinfo)
0992 goto out;
0993
0994 neigh_node->last_seen = jiffies;
0995
0996 spin_lock_bh(&neigh_node->ifinfo_lock);
0997 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
0998 &neigh_ifinfo->bat_iv.tq_index,
0999 batadv_ogm_packet->tq);
1000 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1001 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1002 spin_unlock_bh(&neigh_node->ifinfo_lock);
1003
1004 if (dup_status == BATADV_NO_DUP) {
1005 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1006 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1007 }
1008
1009
1010
1011
1012 router = batadv_orig_router_get(orig_node, if_outgoing);
1013 if (router == neigh_node)
1014 goto out;
1015
1016 if (router) {
1017 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1018 if (!router_ifinfo)
1019 goto out;
1020
1021
1022
1023
1024 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1025 goto out;
1026 }
1027
1028
1029
1030
1031 if (router_ifinfo &&
1032 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1033 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
1034 router->if_incoming);
1035 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
1036 neigh_node->if_incoming);
1037 if (sum_orig >= sum_neigh)
1038 goto out;
1039 }
1040
1041 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1042 goto out;
1043
1044 unlock:
1045 rcu_read_unlock();
1046 out:
1047 batadv_neigh_node_put(neigh_node);
1048 batadv_neigh_node_put(router);
1049 batadv_neigh_ifinfo_put(neigh_ifinfo);
1050 batadv_neigh_ifinfo_put(router_ifinfo);
1051 }
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1064 struct batadv_orig_node *orig_neigh_node,
1065 struct batadv_ogm_packet *batadv_ogm_packet,
1066 struct batadv_hard_iface *if_incoming,
1067 struct batadv_hard_iface *if_outgoing)
1068 {
1069 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1070 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1071 struct batadv_neigh_ifinfo *neigh_ifinfo;
1072 u8 total_count;
1073 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1074 unsigned int tq_iface_hop_penalty = BATADV_TQ_MAX_VALUE;
1075 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1076 unsigned int tq_asym_penalty, inv_asym_penalty;
1077 unsigned int combined_tq;
1078 bool ret = false;
1079
1080
1081 rcu_read_lock();
1082 hlist_for_each_entry_rcu(tmp_neigh_node,
1083 &orig_neigh_node->neigh_list, list) {
1084 if (!batadv_compare_eth(tmp_neigh_node->addr,
1085 orig_neigh_node->orig))
1086 continue;
1087
1088 if (tmp_neigh_node->if_incoming != if_incoming)
1089 continue;
1090
1091 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1092 continue;
1093
1094 neigh_node = tmp_neigh_node;
1095 break;
1096 }
1097 rcu_read_unlock();
1098
1099 if (!neigh_node)
1100 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1101 orig_neigh_node->orig,
1102 orig_neigh_node,
1103 orig_neigh_node);
1104
1105 if (!neigh_node)
1106 goto out;
1107
1108
1109 if (orig_node == orig_neigh_node)
1110 neigh_node->last_seen = jiffies;
1111
1112 orig_node->last_seen = jiffies;
1113
1114
1115 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1116 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1117 if (neigh_ifinfo) {
1118 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1119 batadv_neigh_ifinfo_put(neigh_ifinfo);
1120 } else {
1121 neigh_rq_count = 0;
1122 }
1123
1124
1125 if (orig_eq_count > neigh_rq_count)
1126 total_count = neigh_rq_count;
1127 else
1128 total_count = orig_eq_count;
1129
1130
1131
1132
1133 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1134 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1135 tq_own = 0;
1136 else
1137
1138
1139
1140
1141 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1142
1143
1144
1145
1146
1147
1148 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1149 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1150 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1151 BATADV_TQ_LOCAL_WINDOW_SIZE *
1152 BATADV_TQ_LOCAL_WINDOW_SIZE;
1153 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1154 inv_asym_penalty /= neigh_rq_max_cube;
1155 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1156 tq_iface_hop_penalty -= atomic_read(&if_incoming->hop_penalty);
1157
1158
1159
1160
1161
1162 if (if_outgoing && if_incoming == if_outgoing &&
1163 batadv_is_wifi_hardif(if_outgoing))
1164 tq_iface_hop_penalty = batadv_hop_penalty(tq_iface_hop_penalty,
1165 bat_priv);
1166
1167 combined_tq = batadv_ogm_packet->tq *
1168 tq_own *
1169 tq_asym_penalty *
1170 tq_iface_hop_penalty;
1171 combined_tq /= BATADV_TQ_MAX_VALUE *
1172 BATADV_TQ_MAX_VALUE *
1173 BATADV_TQ_MAX_VALUE;
1174 batadv_ogm_packet->tq = combined_tq;
1175
1176 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1177 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_hop_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1178 orig_node->orig, orig_neigh_node->orig, total_count,
1179 neigh_rq_count, tq_own, tq_asym_penalty,
1180 tq_iface_hop_penalty, batadv_ogm_packet->tq,
1181 if_incoming->net_dev->name,
1182 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1183
1184
1185
1186
1187 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1188 ret = true;
1189
1190 out:
1191 batadv_neigh_node_put(neigh_node);
1192 return ret;
1193 }
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205 static enum batadv_dup_status
1206 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1207 const struct batadv_ogm_packet *batadv_ogm_packet,
1208 const struct batadv_hard_iface *if_incoming,
1209 struct batadv_hard_iface *if_outgoing)
1210 {
1211 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1212 struct batadv_orig_node *orig_node;
1213 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1214 struct batadv_neigh_node *neigh_node;
1215 struct batadv_neigh_ifinfo *neigh_ifinfo;
1216 bool is_dup;
1217 s32 seq_diff;
1218 bool need_update = false;
1219 int set_mark;
1220 enum batadv_dup_status ret = BATADV_NO_DUP;
1221 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1222 u8 *neigh_addr;
1223 u8 packet_count;
1224 unsigned long *bitmap;
1225
1226 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1227 if (!orig_node)
1228 return BATADV_NO_DUP;
1229
1230 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1231 if (WARN_ON(!orig_ifinfo)) {
1232 batadv_orig_node_put(orig_node);
1233 return 0;
1234 }
1235
1236 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1237 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1238
1239
1240 if (!hlist_empty(&orig_node->neigh_list) &&
1241 batadv_window_protected(bat_priv, seq_diff,
1242 BATADV_TQ_LOCAL_WINDOW_SIZE,
1243 &orig_ifinfo->batman_seqno_reset, NULL)) {
1244 ret = BATADV_PROTECTED;
1245 goto out;
1246 }
1247
1248 rcu_read_lock();
1249 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1250 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1251 if_outgoing);
1252 if (!neigh_ifinfo)
1253 continue;
1254
1255 neigh_addr = neigh_node->addr;
1256 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1257 orig_ifinfo->last_real_seqno,
1258 seqno);
1259
1260 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1261 neigh_node->if_incoming == if_incoming) {
1262 set_mark = 1;
1263 if (is_dup)
1264 ret = BATADV_NEIGH_DUP;
1265 } else {
1266 set_mark = 0;
1267 if (is_dup && ret != BATADV_NEIGH_DUP)
1268 ret = BATADV_ORIG_DUP;
1269 }
1270
1271
1272 bitmap = neigh_ifinfo->bat_iv.real_bits;
1273 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1274 seq_diff, set_mark);
1275
1276 packet_count = bitmap_weight(bitmap,
1277 BATADV_TQ_LOCAL_WINDOW_SIZE);
1278 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1279 batadv_neigh_ifinfo_put(neigh_ifinfo);
1280 }
1281 rcu_read_unlock();
1282
1283 if (need_update) {
1284 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1285 "%s updating last_seqno: old %u, new %u\n",
1286 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1287 orig_ifinfo->last_real_seqno, seqno);
1288 orig_ifinfo->last_real_seqno = seqno;
1289 }
1290
1291 out:
1292 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1293 batadv_orig_node_put(orig_node);
1294 batadv_orig_ifinfo_put(orig_ifinfo);
1295 return ret;
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307 static void
1308 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1309 struct batadv_orig_node *orig_node,
1310 struct batadv_hard_iface *if_incoming,
1311 struct batadv_hard_iface *if_outgoing)
1312 {
1313 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1314 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1315 struct batadv_neigh_node *router = NULL;
1316 struct batadv_neigh_node *router_router = NULL;
1317 struct batadv_orig_node *orig_neigh_node;
1318 struct batadv_orig_ifinfo *orig_ifinfo;
1319 struct batadv_neigh_node *orig_neigh_router = NULL;
1320 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1321 struct batadv_ogm_packet *ogm_packet;
1322 enum batadv_dup_status dup_status;
1323 bool is_from_best_next_hop = false;
1324 bool is_single_hop_neigh = false;
1325 bool sameseq, similar_ttl;
1326 struct sk_buff *skb_priv;
1327 struct ethhdr *ethhdr;
1328 u8 *prev_sender;
1329 bool is_bidirect;
1330
1331
1332
1333
1334 skb_priv = skb_copy(skb, GFP_ATOMIC);
1335 if (!skb_priv)
1336 return;
1337
1338 ethhdr = eth_hdr(skb_priv);
1339 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1340
1341 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1342 if_incoming, if_outgoing);
1343 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1344 is_single_hop_neigh = true;
1345
1346 if (dup_status == BATADV_PROTECTED) {
1347 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1348 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1349 ethhdr->h_source);
1350 goto out;
1351 }
1352
1353 if (ogm_packet->tq == 0) {
1354 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1355 "Drop packet: originator packet with tq equal 0\n");
1356 goto out;
1357 }
1358
1359 if (is_single_hop_neigh) {
1360 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1361 ethhdr->h_source);
1362 if (hardif_neigh)
1363 hardif_neigh->last_seen = jiffies;
1364 }
1365
1366 router = batadv_orig_router_get(orig_node, if_outgoing);
1367 if (router) {
1368 router_router = batadv_orig_router_get(router->orig_node,
1369 if_outgoing);
1370 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1371 }
1372
1373 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1374 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1375 is_from_best_next_hop = true;
1376
1377 prev_sender = ogm_packet->prev_sender;
1378
1379 if (router && router_router &&
1380 (batadv_compare_eth(router->addr, prev_sender)) &&
1381 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1382 (batadv_compare_eth(router->addr, router_router->addr))) {
1383 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1384 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1385 ethhdr->h_source);
1386 goto out;
1387 }
1388
1389 if (if_outgoing == BATADV_IF_DEFAULT)
1390 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1391
1392
1393
1394
1395 if (is_single_hop_neigh)
1396 orig_neigh_node = orig_node;
1397 else
1398 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1399 ethhdr->h_source);
1400
1401 if (!orig_neigh_node)
1402 goto out;
1403
1404
1405 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1406 ogm_packet, is_single_hop_neigh);
1407
1408 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1409 if_outgoing);
1410
1411
1412
1413
1414 if (!is_single_hop_neigh && !orig_neigh_router) {
1415 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1416 "Drop packet: OGM via unknown neighbor!\n");
1417 goto out_neigh;
1418 }
1419
1420 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1421 ogm_packet, if_incoming,
1422 if_outgoing);
1423
1424
1425
1426
1427 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1428 if (!orig_ifinfo)
1429 goto out_neigh;
1430
1431 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1432 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1433
1434 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1435 (sameseq && similar_ttl))) {
1436 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1437 orig_ifinfo, ethhdr,
1438 ogm_packet, if_incoming,
1439 if_outgoing, dup_status);
1440 }
1441 batadv_orig_ifinfo_put(orig_ifinfo);
1442
1443
1444 if (if_outgoing == BATADV_IF_DEFAULT)
1445 goto out_neigh;
1446
1447
1448 if (is_single_hop_neigh) {
1449
1450
1451
1452 if (ogm_packet->ttl <= 2 &&
1453 if_incoming != if_outgoing) {
1454 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1455 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1456 goto out_neigh;
1457 }
1458
1459 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1460 is_single_hop_neigh,
1461 is_from_best_next_hop, if_incoming,
1462 if_outgoing);
1463
1464 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1465 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1466 goto out_neigh;
1467 }
1468
1469
1470 if (!is_bidirect) {
1471 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1472 "Drop packet: not received via bidirectional link\n");
1473 goto out_neigh;
1474 }
1475
1476 if (dup_status == BATADV_NEIGH_DUP) {
1477 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1478 "Drop packet: duplicate packet received\n");
1479 goto out_neigh;
1480 }
1481
1482 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1483 "Forwarding packet: rebroadcast originator packet\n");
1484 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1485 is_single_hop_neigh, is_from_best_next_hop,
1486 if_incoming, if_outgoing);
1487
1488 out_neigh:
1489 if (orig_neigh_node && !is_single_hop_neigh)
1490 batadv_orig_node_put(orig_neigh_node);
1491 out:
1492 batadv_neigh_ifinfo_put(router_ifinfo);
1493 batadv_neigh_node_put(router);
1494 batadv_neigh_node_put(router_router);
1495 batadv_neigh_node_put(orig_neigh_router);
1496 batadv_hardif_neigh_put(hardif_neigh);
1497
1498 consume_skb(skb_priv);
1499 }
1500
1501
1502
1503
1504
1505
1506
1507
1508 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1509 struct batadv_hard_iface *if_incoming,
1510 struct batadv_orig_node *orig_node,
1511 u32 if_incoming_seqno)
1512 {
1513 struct batadv_orig_ifinfo *orig_ifinfo;
1514 s32 bit_pos;
1515 u8 *weight;
1516
1517
1518
1519
1520 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1521 return;
1522
1523 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1524 ogm_packet->orig))
1525 return;
1526
1527 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1528 if (!orig_ifinfo)
1529 return;
1530
1531
1532 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1533 bit_pos = if_incoming_seqno - 2;
1534 bit_pos -= ntohl(ogm_packet->seqno);
1535 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1536 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1537 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1538 BATADV_TQ_LOCAL_WINDOW_SIZE);
1539 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1540
1541 batadv_orig_ifinfo_put(orig_ifinfo);
1542 }
1543
1544
1545
1546
1547
1548
1549
1550 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1551 struct batadv_hard_iface *if_incoming)
1552 {
1553 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1554 struct batadv_orig_node *orig_neigh_node, *orig_node;
1555 struct batadv_hard_iface *hard_iface;
1556 struct batadv_ogm_packet *ogm_packet;
1557 u32 if_incoming_seqno;
1558 bool has_directlink_flag;
1559 struct ethhdr *ethhdr;
1560 bool is_my_oldorig = false;
1561 bool is_my_addr = false;
1562 bool is_my_orig = false;
1563
1564 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1565 ethhdr = eth_hdr(skb);
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579 if (ogm_packet->packet_type != BATADV_IV_OGM)
1580 return;
1581
1582
1583 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1584
1585 if (ogm_packet->flags & BATADV_DIRECTLINK)
1586 has_directlink_flag = true;
1587 else
1588 has_directlink_flag = false;
1589
1590 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1591 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1592 ethhdr->h_source, if_incoming->net_dev->name,
1593 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1594 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1595 ogm_packet->tq, ogm_packet->ttl,
1596 ogm_packet->version, has_directlink_flag);
1597
1598 rcu_read_lock();
1599 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1600 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1601 continue;
1602
1603 if (hard_iface->soft_iface != if_incoming->soft_iface)
1604 continue;
1605
1606 if (batadv_compare_eth(ethhdr->h_source,
1607 hard_iface->net_dev->dev_addr))
1608 is_my_addr = true;
1609
1610 if (batadv_compare_eth(ogm_packet->orig,
1611 hard_iface->net_dev->dev_addr))
1612 is_my_orig = true;
1613
1614 if (batadv_compare_eth(ogm_packet->prev_sender,
1615 hard_iface->net_dev->dev_addr))
1616 is_my_oldorig = true;
1617 }
1618 rcu_read_unlock();
1619
1620 if (is_my_addr) {
1621 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1622 "Drop packet: received my own broadcast (sender: %pM)\n",
1623 ethhdr->h_source);
1624 return;
1625 }
1626
1627 if (is_my_orig) {
1628 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1629 ethhdr->h_source);
1630 if (!orig_neigh_node)
1631 return;
1632
1633 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1634 orig_neigh_node, if_incoming_seqno);
1635
1636 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1637 "Drop packet: originator packet from myself (via neighbor)\n");
1638 batadv_orig_node_put(orig_neigh_node);
1639 return;
1640 }
1641
1642 if (is_my_oldorig) {
1643 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1644 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1645 ethhdr->h_source);
1646 return;
1647 }
1648
1649 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1650 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1651 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1652 ethhdr->h_source);
1653 return;
1654 }
1655
1656 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1657 if (!orig_node)
1658 return;
1659
1660 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1661 if_incoming, BATADV_IF_DEFAULT);
1662
1663 rcu_read_lock();
1664 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1665 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1666 continue;
1667
1668 if (hard_iface->soft_iface != bat_priv->soft_iface)
1669 continue;
1670
1671 if (!kref_get_unless_zero(&hard_iface->refcount))
1672 continue;
1673
1674 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1675 if_incoming, hard_iface);
1676
1677 batadv_hardif_put(hard_iface);
1678 }
1679 rcu_read_unlock();
1680
1681 batadv_orig_node_put(orig_node);
1682 }
1683
1684 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1685 {
1686 struct delayed_work *delayed_work;
1687 struct batadv_forw_packet *forw_packet;
1688 struct batadv_priv *bat_priv;
1689 bool dropped = false;
1690
1691 delayed_work = to_delayed_work(work);
1692 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1693 delayed_work);
1694 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1695
1696 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1697 dropped = true;
1698 goto out;
1699 }
1700
1701 batadv_iv_ogm_emit(forw_packet);
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711 if (forw_packet->own &&
1712 forw_packet->if_incoming == forw_packet->if_outgoing)
1713 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1714
1715 out:
1716
1717 if (batadv_forw_packet_steal(forw_packet,
1718 &bat_priv->forw_bat_list_lock))
1719 batadv_forw_packet_free(forw_packet, dropped);
1720 }
1721
1722 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1723 struct batadv_hard_iface *if_incoming)
1724 {
1725 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1726 struct batadv_ogm_packet *ogm_packet;
1727 u8 *packet_pos;
1728 int ogm_offset;
1729 bool res;
1730 int ret = NET_RX_DROP;
1731
1732 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1733 if (!res)
1734 goto free_skb;
1735
1736
1737
1738
1739 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1740 goto free_skb;
1741
1742 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1743 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1744 skb->len + ETH_HLEN);
1745
1746 ogm_offset = 0;
1747 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1748
1749
1750 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1751 ogm_packet)) {
1752 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1753
1754 ogm_offset += BATADV_OGM_HLEN;
1755 ogm_offset += ntohs(ogm_packet->tvlv_len);
1756
1757 packet_pos = skb->data + ogm_offset;
1758 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1759 }
1760
1761 ret = NET_RX_SUCCESS;
1762
1763 free_skb:
1764 if (ret == NET_RX_SUCCESS)
1765 consume_skb(skb);
1766 else
1767 kfree_skb(skb);
1768
1769 return ret;
1770 }
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781 static bool
1782 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1783 struct batadv_hard_iface *if_outgoing,
1784 u8 *tq_avg)
1785 {
1786 struct batadv_neigh_ifinfo *n_ifinfo;
1787
1788 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1789 if (!n_ifinfo)
1790 return false;
1791
1792 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1793 batadv_neigh_ifinfo_put(n_ifinfo);
1794
1795 return true;
1796 }
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 static int
1813 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1814 struct batadv_priv *bat_priv,
1815 struct batadv_hard_iface *if_outgoing,
1816 struct batadv_orig_node *orig_node,
1817 struct batadv_neigh_node *neigh_node,
1818 bool best)
1819 {
1820 void *hdr;
1821 u8 tq_avg;
1822 unsigned int last_seen_msecs;
1823
1824 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1825
1826 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1827 return 0;
1828
1829 if (if_outgoing != BATADV_IF_DEFAULT &&
1830 if_outgoing != neigh_node->if_incoming)
1831 return 0;
1832
1833 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1834 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1835 if (!hdr)
1836 return -ENOBUFS;
1837
1838 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1839 orig_node->orig) ||
1840 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1841 neigh_node->addr) ||
1842 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
1843 neigh_node->if_incoming->net_dev->name) ||
1844 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1845 neigh_node->if_incoming->net_dev->ifindex) ||
1846 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1847 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1848 last_seen_msecs))
1849 goto nla_put_failure;
1850
1851 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1852 goto nla_put_failure;
1853
1854 genlmsg_end(msg, hdr);
1855 return 0;
1856
1857 nla_put_failure:
1858 genlmsg_cancel(msg, hdr);
1859 return -EMSGSIZE;
1860 }
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876 static int
1877 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1878 struct batadv_priv *bat_priv,
1879 struct batadv_hard_iface *if_outgoing,
1880 struct batadv_orig_node *orig_node, int *sub_s)
1881 {
1882 struct batadv_neigh_node *neigh_node_best;
1883 struct batadv_neigh_node *neigh_node;
1884 int sub = 0;
1885 bool best;
1886 u8 tq_avg_best;
1887
1888 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1889 if (!neigh_node_best)
1890 goto out;
1891
1892 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1893 &tq_avg_best))
1894 goto out;
1895
1896 if (tq_avg_best == 0)
1897 goto out;
1898
1899 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1900 if (sub++ < *sub_s)
1901 continue;
1902
1903 best = (neigh_node == neigh_node_best);
1904
1905 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1906 bat_priv, if_outgoing,
1907 orig_node, neigh_node,
1908 best)) {
1909 batadv_neigh_node_put(neigh_node_best);
1910
1911 *sub_s = sub - 1;
1912 return -EMSGSIZE;
1913 }
1914 }
1915
1916 out:
1917 batadv_neigh_node_put(neigh_node_best);
1918
1919 *sub_s = 0;
1920 return 0;
1921 }
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937 static int
1938 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1939 struct batadv_priv *bat_priv,
1940 struct batadv_hard_iface *if_outgoing,
1941 struct hlist_head *head, int *idx_s, int *sub)
1942 {
1943 struct batadv_orig_node *orig_node;
1944 int idx = 0;
1945
1946 rcu_read_lock();
1947 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1948 if (idx++ < *idx_s)
1949 continue;
1950
1951 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
1952 if_outgoing, orig_node,
1953 sub)) {
1954 rcu_read_unlock();
1955 *idx_s = idx - 1;
1956 return -EMSGSIZE;
1957 }
1958 }
1959 rcu_read_unlock();
1960
1961 *idx_s = 0;
1962 *sub = 0;
1963 return 0;
1964 }
1965
1966
1967
1968
1969
1970
1971
1972
1973 static void
1974 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
1975 struct batadv_priv *bat_priv,
1976 struct batadv_hard_iface *if_outgoing)
1977 {
1978 struct batadv_hashtable *hash = bat_priv->orig_hash;
1979 struct hlist_head *head;
1980 int bucket = cb->args[0];
1981 int idx = cb->args[1];
1982 int sub = cb->args[2];
1983 int portid = NETLINK_CB(cb->skb).portid;
1984
1985 while (bucket < hash->size) {
1986 head = &hash->table[bucket];
1987
1988 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
1989 cb->nlh->nlmsg_seq,
1990 bat_priv, if_outgoing, head,
1991 &idx, &sub))
1992 break;
1993
1994 bucket++;
1995 }
1996
1997 cb->args[0] = bucket;
1998 cb->args[1] = idx;
1999 cb->args[2] = sub;
2000 }
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2017 struct batadv_hard_iface *if_outgoing1,
2018 struct batadv_neigh_node *neigh2,
2019 struct batadv_hard_iface *if_outgoing2,
2020 int *diff)
2021 {
2022 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2023 u8 tq1, tq2;
2024 bool ret = true;
2025
2026 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2027 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2028
2029 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2030 ret = false;
2031 goto out;
2032 }
2033
2034 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2035 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2036 *diff = (int)tq1 - (int)tq2;
2037
2038 out:
2039 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2040 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2041
2042 return ret;
2043 }
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054 static int
2055 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2056 struct batadv_hardif_neigh_node *hardif_neigh)
2057 {
2058 void *hdr;
2059 unsigned int last_seen_msecs;
2060
2061 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2062
2063 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2064 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2065 if (!hdr)
2066 return -ENOBUFS;
2067
2068 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2069 hardif_neigh->addr) ||
2070 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2071 hardif_neigh->if_incoming->net_dev->name) ||
2072 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2073 hardif_neigh->if_incoming->net_dev->ifindex) ||
2074 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2075 last_seen_msecs))
2076 goto nla_put_failure;
2077
2078 genlmsg_end(msg, hdr);
2079 return 0;
2080
2081 nla_put_failure:
2082 genlmsg_cancel(msg, hdr);
2083 return -EMSGSIZE;
2084 }
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100 static int
2101 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2102 struct batadv_priv *bat_priv,
2103 struct batadv_hard_iface *hard_iface,
2104 int *idx_s)
2105 {
2106 struct batadv_hardif_neigh_node *hardif_neigh;
2107 int idx = 0;
2108
2109 hlist_for_each_entry_rcu(hardif_neigh,
2110 &hard_iface->neigh_list, list) {
2111 if (idx++ < *idx_s)
2112 continue;
2113
2114 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2115 hardif_neigh)) {
2116 *idx_s = idx - 1;
2117 return -EMSGSIZE;
2118 }
2119 }
2120
2121 *idx_s = 0;
2122 return 0;
2123 }
2124
2125
2126
2127
2128
2129
2130
2131
2132 static void
2133 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2134 struct batadv_priv *bat_priv,
2135 struct batadv_hard_iface *single_hardif)
2136 {
2137 struct batadv_hard_iface *hard_iface;
2138 int i_hardif = 0;
2139 int i_hardif_s = cb->args[0];
2140 int idx = cb->args[1];
2141 int portid = NETLINK_CB(cb->skb).portid;
2142
2143 rcu_read_lock();
2144 if (single_hardif) {
2145 if (i_hardif_s == 0) {
2146 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2147 cb->nlh->nlmsg_seq,
2148 bat_priv,
2149 single_hardif,
2150 &idx) == 0)
2151 i_hardif++;
2152 }
2153 } else {
2154 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2155 list) {
2156 if (hard_iface->soft_iface != bat_priv->soft_iface)
2157 continue;
2158
2159 if (i_hardif++ < i_hardif_s)
2160 continue;
2161
2162 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2163 cb->nlh->nlmsg_seq,
2164 bat_priv,
2165 hard_iface, &idx)) {
2166 i_hardif--;
2167 break;
2168 }
2169 }
2170 }
2171 rcu_read_unlock();
2172
2173 cb->args[0] = i_hardif;
2174 cb->args[1] = idx;
2175 }
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2188 struct batadv_hard_iface *if_outgoing1,
2189 struct batadv_neigh_node *neigh2,
2190 struct batadv_hard_iface *if_outgoing2)
2191 {
2192 bool ret;
2193 int diff;
2194
2195 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2196 if_outgoing2, &diff);
2197 if (!ret)
2198 return 0;
2199
2200 return diff;
2201 }
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214 static bool
2215 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2216 struct batadv_hard_iface *if_outgoing1,
2217 struct batadv_neigh_node *neigh2,
2218 struct batadv_hard_iface *if_outgoing2)
2219 {
2220 bool ret;
2221 int diff;
2222
2223 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2224 if_outgoing2, &diff);
2225 if (!ret)
2226 return false;
2227
2228 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2229 return ret;
2230 }
2231
2232 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2233 {
2234
2235 batadv_iv_ogm_schedule(hard_iface);
2236 }
2237
2238
2239
2240
2241
2242 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2243 {
2244
2245 atomic_set(&bat_priv->gw.sel_class, 20);
2246 }
2247
2248 static struct batadv_gw_node *
2249 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2250 {
2251 struct batadv_neigh_node *router;
2252 struct batadv_neigh_ifinfo *router_ifinfo;
2253 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2254 u64 max_gw_factor = 0;
2255 u64 tmp_gw_factor = 0;
2256 u8 max_tq = 0;
2257 u8 tq_avg;
2258 struct batadv_orig_node *orig_node;
2259
2260 rcu_read_lock();
2261 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2262 orig_node = gw_node->orig_node;
2263 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2264 if (!router)
2265 continue;
2266
2267 router_ifinfo = batadv_neigh_ifinfo_get(router,
2268 BATADV_IF_DEFAULT);
2269 if (!router_ifinfo)
2270 goto next;
2271
2272 if (!kref_get_unless_zero(&gw_node->refcount))
2273 goto next;
2274
2275 tq_avg = router_ifinfo->bat_iv.tq_avg;
2276
2277 switch (atomic_read(&bat_priv->gw.sel_class)) {
2278 case 1:
2279 tmp_gw_factor = tq_avg * tq_avg;
2280 tmp_gw_factor *= gw_node->bandwidth_down;
2281 tmp_gw_factor *= 100 * 100;
2282 tmp_gw_factor >>= 18;
2283
2284 if (tmp_gw_factor > max_gw_factor ||
2285 (tmp_gw_factor == max_gw_factor &&
2286 tq_avg > max_tq)) {
2287 batadv_gw_node_put(curr_gw);
2288 curr_gw = gw_node;
2289 kref_get(&curr_gw->refcount);
2290 }
2291 break;
2292
2293 default:
2294
2295
2296
2297
2298
2299
2300 if (tq_avg > max_tq) {
2301 batadv_gw_node_put(curr_gw);
2302 curr_gw = gw_node;
2303 kref_get(&curr_gw->refcount);
2304 }
2305 break;
2306 }
2307
2308 if (tq_avg > max_tq)
2309 max_tq = tq_avg;
2310
2311 if (tmp_gw_factor > max_gw_factor)
2312 max_gw_factor = tmp_gw_factor;
2313
2314 batadv_gw_node_put(gw_node);
2315
2316 next:
2317 batadv_neigh_node_put(router);
2318 batadv_neigh_ifinfo_put(router_ifinfo);
2319 }
2320 rcu_read_unlock();
2321
2322 return curr_gw;
2323 }
2324
2325 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2326 struct batadv_orig_node *curr_gw_orig,
2327 struct batadv_orig_node *orig_node)
2328 {
2329 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2330 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2331 struct batadv_neigh_node *router_gw = NULL;
2332 struct batadv_neigh_node *router_orig = NULL;
2333 u8 gw_tq_avg, orig_tq_avg;
2334 bool ret = false;
2335
2336
2337 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2338 return false;
2339
2340 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2341 if (!router_gw) {
2342 ret = true;
2343 goto out;
2344 }
2345
2346 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2347 BATADV_IF_DEFAULT);
2348 if (!router_gw_ifinfo) {
2349 ret = true;
2350 goto out;
2351 }
2352
2353 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2354 if (!router_orig)
2355 goto out;
2356
2357 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2358 BATADV_IF_DEFAULT);
2359 if (!router_orig_ifinfo)
2360 goto out;
2361
2362 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2363 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2364
2365
2366 if (orig_tq_avg < gw_tq_avg)
2367 goto out;
2368
2369
2370
2371
2372 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2373 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2374 goto out;
2375
2376 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2377 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2378 gw_tq_avg, orig_tq_avg);
2379
2380 ret = true;
2381 out:
2382 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2383 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2384 batadv_neigh_node_put(router_gw);
2385 batadv_neigh_node_put(router_orig);
2386
2387 return ret;
2388 }
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2401 struct netlink_callback *cb,
2402 struct batadv_priv *bat_priv,
2403 struct batadv_gw_node *gw_node)
2404 {
2405 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2406 struct batadv_neigh_node *router;
2407 struct batadv_gw_node *curr_gw = NULL;
2408 int ret = 0;
2409 void *hdr;
2410
2411 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2412 if (!router)
2413 goto out;
2414
2415 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2416 if (!router_ifinfo)
2417 goto out;
2418
2419 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2420
2421 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2422 &batadv_netlink_family, NLM_F_MULTI,
2423 BATADV_CMD_GET_GATEWAYS);
2424 if (!hdr) {
2425 ret = -ENOBUFS;
2426 goto out;
2427 }
2428
2429 genl_dump_check_consistent(cb, hdr);
2430
2431 ret = -EMSGSIZE;
2432
2433 if (curr_gw == gw_node)
2434 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2435 genlmsg_cancel(msg, hdr);
2436 goto out;
2437 }
2438
2439 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2440 gw_node->orig_node->orig) ||
2441 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2442 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2443 router->addr) ||
2444 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2445 router->if_incoming->net_dev->name) ||
2446 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2447 router->if_incoming->net_dev->ifindex) ||
2448 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2449 gw_node->bandwidth_down) ||
2450 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2451 gw_node->bandwidth_up)) {
2452 genlmsg_cancel(msg, hdr);
2453 goto out;
2454 }
2455
2456 genlmsg_end(msg, hdr);
2457 ret = 0;
2458
2459 out:
2460 batadv_gw_node_put(curr_gw);
2461 batadv_neigh_ifinfo_put(router_ifinfo);
2462 batadv_neigh_node_put(router);
2463 return ret;
2464 }
2465
2466
2467
2468
2469
2470
2471
2472 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2473 struct batadv_priv *bat_priv)
2474 {
2475 int portid = NETLINK_CB(cb->skb).portid;
2476 struct batadv_gw_node *gw_node;
2477 int idx_skip = cb->args[0];
2478 int idx = 0;
2479
2480 spin_lock_bh(&bat_priv->gw.list_lock);
2481 cb->seq = bat_priv->gw.generation << 1 | 1;
2482
2483 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2484 if (idx++ < idx_skip)
2485 continue;
2486
2487 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2488 gw_node)) {
2489 idx_skip = idx - 1;
2490 goto unlock;
2491 }
2492 }
2493
2494 idx_skip = idx;
2495 unlock:
2496 spin_unlock_bh(&bat_priv->gw.list_lock);
2497
2498 cb->args[0] = idx_skip;
2499 }
2500
2501 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2502 .name = "BATMAN_IV",
2503 .iface = {
2504 .enable = batadv_iv_ogm_iface_enable,
2505 .enabled = batadv_iv_iface_enabled,
2506 .disable = batadv_iv_ogm_iface_disable,
2507 .update_mac = batadv_iv_ogm_iface_update_mac,
2508 .primary_set = batadv_iv_ogm_primary_iface_set,
2509 },
2510 .neigh = {
2511 .cmp = batadv_iv_ogm_neigh_cmp,
2512 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2513 .dump = batadv_iv_ogm_neigh_dump,
2514 },
2515 .orig = {
2516 .dump = batadv_iv_ogm_orig_dump,
2517 },
2518 .gw = {
2519 .init_sel_class = batadv_iv_init_sel_class,
2520 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2521 .is_eligible = batadv_iv_gw_is_eligible,
2522 .dump = batadv_iv_gw_dump,
2523 },
2524 };
2525
2526
2527
2528
2529
2530
2531 int __init batadv_iv_init(void)
2532 {
2533 int ret;
2534
2535
2536 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2537 batadv_iv_ogm_receive);
2538 if (ret < 0)
2539 goto out;
2540
2541 ret = batadv_algo_register(&batadv_batman_iv);
2542 if (ret < 0)
2543 goto handler_unregister;
2544
2545 goto out;
2546
2547 handler_unregister:
2548 batadv_recv_handler_unregister(BATADV_IV_OGM);
2549 out:
2550 return ret;
2551 }