1
2
3
4
5
6
7#include "bat_iv_ogm.h"
8#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/bitmap.h>
12#include <linux/bitops.h>
13#include <linux/bug.h>
14#include <linux/byteorder/generic.h>
15#include <linux/cache.h>
16#include <linux/errno.h>
17#include <linux/etherdevice.h>
18#include <linux/gfp.h>
19#include <linux/if_ether.h>
20#include <linux/init.h>
21#include <linux/jiffies.h>
22#include <linux/kernel.h>
23#include <linux/kref.h>
24#include <linux/list.h>
25#include <linux/netdevice.h>
26#include <linux/netlink.h>
27#include <linux/pkt_sched.h>
28#include <linux/printk.h>
29#include <linux/random.h>
30#include <linux/rculist.h>
31#include <linux/rcupdate.h>
32#include <linux/seq_file.h>
33#include <linux/skbuff.h>
34#include <linux/slab.h>
35#include <linux/spinlock.h>
36#include <linux/stddef.h>
37#include <linux/string.h>
38#include <linux/types.h>
39#include <linux/workqueue.h>
40#include <net/genetlink.h>
41#include <net/netlink.h>
42#include <uapi/linux/batadv_packet.h>
43#include <uapi/linux/batman_adv.h>
44
45#include "bat_algo.h"
46#include "bitarray.h"
47#include "gateway_client.h"
48#include "hard-interface.h"
49#include "hash.h"
50#include "log.h"
51#include "netlink.h"
52#include "network-coding.h"
53#include "originator.h"
54#include "routing.h"
55#include "send.h"
56#include "translation-table.h"
57#include "tvlv.h"
58
59static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
60
61
62
63
64enum batadv_dup_status {
65
66 BATADV_NO_DUP = 0,
67
68
69
70
71
72 BATADV_ORIG_DUP,
73
74
75 BATADV_NEIGH_DUP,
76
77
78
79
80 BATADV_PROTECTED,
81};
82
83
84
85
86
87
88
89static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
90{
91 lq_recv[*lq_index] = value;
92 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
93}
94
95
96
97
98
99
100
101
102static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
103{
104 const u8 *ptr;
105 u16 count = 0;
106 u16 i = 0;
107 u16 sum = 0;
108
109 ptr = lq_recv;
110
111 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
112 if (*ptr != 0) {
113 count++;
114 sum += *ptr;
115 }
116
117 i++;
118 ptr++;
119 }
120
121 if (count == 0)
122 return 0;
123
124 return (u8)(sum / count);
125}
126
127
128
129
130
131
132
133
134
135
136
137static struct batadv_orig_node *
138batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
139{
140 struct batadv_orig_node *orig_node;
141 int hash_added;
142
143 orig_node = batadv_orig_hash_find(bat_priv, addr);
144 if (orig_node)
145 return orig_node;
146
147 orig_node = batadv_orig_node_new(bat_priv, addr);
148 if (!orig_node)
149 return NULL;
150
151 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
152
153 kref_get(&orig_node->refcount);
154 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
155 batadv_choose_orig, orig_node,
156 &orig_node->hash_entry);
157 if (hash_added != 0)
158 goto free_orig_node_hash;
159
160 return orig_node;
161
162free_orig_node_hash:
163
164 batadv_orig_node_put(orig_node);
165
166 batadv_orig_node_put(orig_node);
167
168 return NULL;
169}
170
171static struct batadv_neigh_node *
172batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
173 const u8 *neigh_addr,
174 struct batadv_orig_node *orig_node,
175 struct batadv_orig_node *orig_neigh)
176{
177 struct batadv_neigh_node *neigh_node;
178
179 neigh_node = batadv_neigh_node_get_or_create(orig_node,
180 hard_iface, neigh_addr);
181 if (!neigh_node)
182 goto out;
183
184 neigh_node->orig_node = orig_neigh;
185
186out:
187 return neigh_node;
188}
189
190static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
191{
192 struct batadv_ogm_packet *batadv_ogm_packet;
193 unsigned char *ogm_buff;
194 u32 random_seqno;
195
196
197 get_random_bytes(&random_seqno, sizeof(random_seqno));
198 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
199
200 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
201 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
202 if (!ogm_buff)
203 return -ENOMEM;
204
205 hard_iface->bat_iv.ogm_buff = ogm_buff;
206
207 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
208 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
209 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
210 batadv_ogm_packet->ttl = 2;
211 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
212 batadv_ogm_packet->reserved = 0;
213 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
214
215 return 0;
216}
217
218static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
219{
220 kfree(hard_iface->bat_iv.ogm_buff);
221 hard_iface->bat_iv.ogm_buff = NULL;
222}
223
224static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
225{
226 struct batadv_ogm_packet *batadv_ogm_packet;
227 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
228
229 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
230 ether_addr_copy(batadv_ogm_packet->orig,
231 hard_iface->net_dev->dev_addr);
232 ether_addr_copy(batadv_ogm_packet->prev_sender,
233 hard_iface->net_dev->dev_addr);
234}
235
236static void
237batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
238{
239 struct batadv_ogm_packet *batadv_ogm_packet;
240 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
241
242 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
243 batadv_ogm_packet->ttl = BATADV_TTL;
244}
245
246
247static unsigned long
248batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
249{
250 unsigned int msecs;
251
252 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
253 msecs += prandom_u32() % (2 * BATADV_JITTER);
254
255 return jiffies + msecs_to_jiffies(msecs);
256}
257
258
259static unsigned long batadv_iv_ogm_fwd_send_time(void)
260{
261 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
262}
263
264
265static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
266{
267 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
268 int new_tq;
269
270 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
271 new_tq /= BATADV_TQ_MAX_VALUE;
272
273 return new_tq;
274}
275
276
277
278
279
280
281
282
283
284static bool
285batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
286 const struct batadv_ogm_packet *ogm_packet)
287{
288 int next_buff_pos = 0;
289
290
291 next_buff_pos += buff_pos + sizeof(*ogm_packet);
292 if (next_buff_pos > packet_len)
293 return false;
294
295
296 next_buff_pos += ntohs(ogm_packet->tvlv_len);
297
298 return (next_buff_pos <= packet_len) &&
299 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
300}
301
302
303static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
304 struct batadv_hard_iface *hard_iface)
305{
306 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
307 const char *fwd_str;
308 u8 packet_num;
309 s16 buff_pos;
310 struct batadv_ogm_packet *batadv_ogm_packet;
311 struct sk_buff *skb;
312 u8 *packet_pos;
313
314 if (hard_iface->if_status != BATADV_IF_ACTIVE)
315 return;
316
317 packet_num = 0;
318 buff_pos = 0;
319 packet_pos = forw_packet->skb->data;
320 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
321
322
323 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
324 batadv_ogm_packet)) {
325
326
327
328 if (forw_packet->direct_link_flags & BIT(packet_num) &&
329 forw_packet->if_incoming == hard_iface)
330 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
331 else
332 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
333
334 if (packet_num > 0 || !forw_packet->own)
335 fwd_str = "Forwarding";
336 else
337 fwd_str = "Sending own";
338
339 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
340 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
341 fwd_str, (packet_num > 0 ? "aggregated " : ""),
342 batadv_ogm_packet->orig,
343 ntohl(batadv_ogm_packet->seqno),
344 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
345 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
346 "on" : "off"),
347 hard_iface->net_dev->name,
348 hard_iface->net_dev->dev_addr);
349
350 buff_pos += BATADV_OGM_HLEN;
351 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
352 packet_num++;
353 packet_pos = forw_packet->skb->data + buff_pos;
354 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
355 }
356
357
358 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
359 if (skb) {
360 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
361 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
362 skb->len + ETH_HLEN);
363 batadv_send_broadcast_skb(skb, hard_iface);
364 }
365}
366
367
368static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
369{
370 struct net_device *soft_iface;
371
372 if (!forw_packet->if_incoming) {
373 pr_err("Error - can't forward packet: incoming iface not specified\n");
374 return;
375 }
376
377 soft_iface = forw_packet->if_incoming->soft_iface;
378
379 if (WARN_ON(!forw_packet->if_outgoing))
380 return;
381
382 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
383 return;
384
385 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
386 return;
387
388
389 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
390}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406static bool
407batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
408 struct batadv_priv *bat_priv,
409 int packet_len, unsigned long send_time,
410 bool directlink,
411 const struct batadv_hard_iface *if_incoming,
412 const struct batadv_hard_iface *if_outgoing,
413 const struct batadv_forw_packet *forw_packet)
414{
415 struct batadv_ogm_packet *batadv_ogm_packet;
416 int aggregated_bytes = forw_packet->packet_len + packet_len;
417 struct batadv_hard_iface *primary_if = NULL;
418 bool res = false;
419 unsigned long aggregation_end_time;
420
421 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
422 aggregation_end_time = send_time;
423 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
424
425
426
427
428
429
430
431
432
433 if (!time_before(send_time, forw_packet->send_time) ||
434 !time_after_eq(aggregation_end_time, forw_packet->send_time))
435 return false;
436
437 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
438 return false;
439
440
441 if (forw_packet->if_outgoing != if_outgoing)
442 return false;
443
444
445
446
447
448
449
450
451 primary_if = batadv_primary_if_get_selected(bat_priv);
452 if (!primary_if)
453 return false;
454
455
456
457
458 if (!directlink &&
459 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
460 batadv_ogm_packet->ttl != 1 &&
461
462
463
464
465 (!forw_packet->own ||
466 forw_packet->if_incoming == primary_if)) {
467 res = true;
468 goto out;
469 }
470
471
472
473
474 if (directlink &&
475 new_bat_ogm_packet->ttl == 1 &&
476 forw_packet->if_incoming == if_incoming &&
477
478
479
480
481
482 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
483 (forw_packet->own &&
484 forw_packet->if_incoming != primary_if))) {
485 res = true;
486 goto out;
487 }
488
489out:
490 if (primary_if)
491 batadv_hardif_put(primary_if);
492 return res;
493}
494
495
496
497
498
499
500
501
502
503
504
505
506static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
507 int packet_len, unsigned long send_time,
508 bool direct_link,
509 struct batadv_hard_iface *if_incoming,
510 struct batadv_hard_iface *if_outgoing,
511 int own_packet)
512{
513 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
514 struct batadv_forw_packet *forw_packet_aggr;
515 struct sk_buff *skb;
516 unsigned char *skb_buff;
517 unsigned int skb_size;
518 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
519
520 if (atomic_read(&bat_priv->aggregated_ogms) &&
521 packet_len < BATADV_MAX_AGGREGATION_BYTES)
522 skb_size = BATADV_MAX_AGGREGATION_BYTES;
523 else
524 skb_size = packet_len;
525
526 skb_size += ETH_HLEN;
527
528 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
529 if (!skb)
530 return;
531
532 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
533 queue_left, bat_priv, skb);
534 if (!forw_packet_aggr) {
535 kfree_skb(skb);
536 return;
537 }
538
539 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
540 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
541
542 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
543 forw_packet_aggr->packet_len = packet_len;
544 memcpy(skb_buff, packet_buff, packet_len);
545
546 forw_packet_aggr->own = own_packet;
547 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
548 forw_packet_aggr->send_time = send_time;
549
550
551 if (direct_link)
552 forw_packet_aggr->direct_link_flags |= 1;
553
554 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
555 batadv_iv_send_outstanding_bat_ogm_packet);
556
557 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
558}
559
560
561static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
562 const unsigned char *packet_buff,
563 int packet_len, bool direct_link)
564{
565 unsigned long new_direct_link_flag;
566
567 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
568 forw_packet_aggr->packet_len += packet_len;
569 forw_packet_aggr->num_packets++;
570
571
572 if (direct_link) {
573 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
574 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
575 }
576}
577
578
579
580
581
582
583
584
585
586
587
588static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
589 unsigned char *packet_buff,
590 int packet_len,
591 struct batadv_hard_iface *if_incoming,
592 struct batadv_hard_iface *if_outgoing,
593 int own_packet, unsigned long send_time)
594{
595
596
597
598 struct batadv_forw_packet *forw_packet_aggr = NULL;
599 struct batadv_forw_packet *forw_packet_pos = NULL;
600 struct batadv_ogm_packet *batadv_ogm_packet;
601 bool direct_link;
602 unsigned long max_aggregation_jiffies;
603
604 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
605 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
606 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
607
608
609 spin_lock_bh(&bat_priv->forw_bat_list_lock);
610
611 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
612 hlist_for_each_entry(forw_packet_pos,
613 &bat_priv->forw_bat_list, list) {
614 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
615 bat_priv, packet_len,
616 send_time, direct_link,
617 if_incoming,
618 if_outgoing,
619 forw_packet_pos)) {
620 forw_packet_aggr = forw_packet_pos;
621 break;
622 }
623 }
624 }
625
626
627
628
629 if (!forw_packet_aggr) {
630
631 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
632
633
634
635
636
637 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
638 send_time += max_aggregation_jiffies;
639
640 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
641 send_time, direct_link,
642 if_incoming, if_outgoing,
643 own_packet);
644 } else {
645 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
646 packet_len, direct_link);
647 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
648 }
649}
650
651static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
652 const struct ethhdr *ethhdr,
653 struct batadv_ogm_packet *batadv_ogm_packet,
654 bool is_single_hop_neigh,
655 bool is_from_best_next_hop,
656 struct batadv_hard_iface *if_incoming,
657 struct batadv_hard_iface *if_outgoing)
658{
659 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
660 u16 tvlv_len;
661
662 if (batadv_ogm_packet->ttl <= 1) {
663 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
664 return;
665 }
666
667 if (!is_from_best_next_hop) {
668
669
670
671
672
673
674 if (is_single_hop_neigh)
675 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
676 else
677 return;
678 }
679
680 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
681
682 batadv_ogm_packet->ttl--;
683 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
684
685
686 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
687 bat_priv);
688
689 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
690 "Forwarding packet: tq: %i, ttl: %i\n",
691 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
692
693 if (is_single_hop_neigh)
694 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
695 else
696 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
697
698 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
699 BATADV_OGM_HLEN + tvlv_len,
700 if_incoming, if_outgoing, 0,
701 batadv_iv_ogm_fwd_send_time());
702}
703
704
705
706
707
708
709static void
710batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
711{
712 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
713 struct batadv_hashtable *hash = bat_priv->orig_hash;
714 struct hlist_head *head;
715 struct batadv_orig_node *orig_node;
716 struct batadv_orig_ifinfo *orig_ifinfo;
717 unsigned long *word;
718 u32 i;
719 u8 *w;
720
721 for (i = 0; i < hash->size; i++) {
722 head = &hash->table[i];
723
724 rcu_read_lock();
725 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
726 hlist_for_each_entry_rcu(orig_ifinfo,
727 &orig_node->ifinfo_list,
728 list) {
729 if (orig_ifinfo->if_outgoing != hard_iface)
730 continue;
731
732 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
733 word = orig_ifinfo->bat_iv.bcast_own;
734 batadv_bit_get_packet(bat_priv, word, 1, 0);
735 w = &orig_ifinfo->bat_iv.bcast_own_sum;
736 *w = bitmap_weight(word,
737 BATADV_TQ_LOCAL_WINDOW_SIZE);
738 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
739 }
740 }
741 rcu_read_unlock();
742 }
743}
744
745static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
746{
747 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
748 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
749 struct batadv_ogm_packet *batadv_ogm_packet;
750 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
751 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
752 u32 seqno;
753 u16 tvlv_len = 0;
754 unsigned long send_time;
755
756 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
757 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
758 return;
759
760
761
762
763
764
765
766 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
767 hard_iface->if_status = BATADV_IF_ACTIVE;
768
769 primary_if = batadv_primary_if_get_selected(bat_priv);
770
771 if (hard_iface == primary_if) {
772
773
774
775 batadv_tt_local_commit_changes(bat_priv);
776 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
777 ogm_buff_len,
778 BATADV_OGM_HLEN);
779 }
780
781 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
782 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
783
784
785 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
786 batadv_ogm_packet->seqno = htonl(seqno);
787 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
788
789 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
790
791 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
792
793 if (hard_iface != primary_if) {
794
795
796
797 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
798 hard_iface, hard_iface, 1, send_time);
799 goto out;
800 }
801
802
803
804
805 rcu_read_lock();
806 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
807 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
808 continue;
809
810 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
811 continue;
812
813 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
814 *ogm_buff_len, hard_iface,
815 tmp_hard_iface, 1, send_time);
816
817 batadv_hardif_put(tmp_hard_iface);
818 }
819 rcu_read_unlock();
820
821out:
822 if (primary_if)
823 batadv_hardif_put(primary_if);
824}
825
826
827
828
829
830
831
832
833
834
835
836static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
837 struct batadv_hard_iface *if_outgoing)
838{
839 struct batadv_orig_ifinfo *orig_ifinfo;
840 u8 sum;
841
842 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
843 if (!orig_ifinfo)
844 return 0;
845
846 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
847 sum = orig_ifinfo->bat_iv.bcast_own_sum;
848 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
849
850 batadv_orig_ifinfo_put(orig_ifinfo);
851
852 return sum;
853}
854
855
856
857
858
859
860
861
862
863
864
865
866
867static void
868batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
869 struct batadv_orig_node *orig_node,
870 struct batadv_orig_ifinfo *orig_ifinfo,
871 const struct ethhdr *ethhdr,
872 const struct batadv_ogm_packet *batadv_ogm_packet,
873 struct batadv_hard_iface *if_incoming,
874 struct batadv_hard_iface *if_outgoing,
875 enum batadv_dup_status dup_status)
876{
877 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
878 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
879 struct batadv_neigh_node *neigh_node = NULL;
880 struct batadv_neigh_node *tmp_neigh_node = NULL;
881 struct batadv_neigh_node *router = NULL;
882 u8 sum_orig, sum_neigh;
883 u8 *neigh_addr;
884 u8 tq_avg;
885
886 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
887 "%s(): Searching and updating originator entry of received packet\n",
888 __func__);
889
890 rcu_read_lock();
891 hlist_for_each_entry_rcu(tmp_neigh_node,
892 &orig_node->neigh_list, list) {
893 neigh_addr = tmp_neigh_node->addr;
894 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
895 tmp_neigh_node->if_incoming == if_incoming &&
896 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
897 if (WARN(neigh_node, "too many matching neigh_nodes"))
898 batadv_neigh_node_put(neigh_node);
899 neigh_node = tmp_neigh_node;
900 continue;
901 }
902
903 if (dup_status != BATADV_NO_DUP)
904 continue;
905
906
907 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
908 if_outgoing);
909 if (!neigh_ifinfo)
910 continue;
911
912 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
913 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
914 &neigh_ifinfo->bat_iv.tq_index, 0);
915 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
916 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
917 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
918
919 batadv_neigh_ifinfo_put(neigh_ifinfo);
920 neigh_ifinfo = NULL;
921 }
922
923 if (!neigh_node) {
924 struct batadv_orig_node *orig_tmp;
925
926 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
927 if (!orig_tmp)
928 goto unlock;
929
930 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
931 ethhdr->h_source,
932 orig_node, orig_tmp);
933
934 batadv_orig_node_put(orig_tmp);
935 if (!neigh_node)
936 goto unlock;
937 } else {
938 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
939 "Updating existing last-hop neighbor of originator\n");
940 }
941
942 rcu_read_unlock();
943 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
944 if (!neigh_ifinfo)
945 goto out;
946
947 neigh_node->last_seen = jiffies;
948
949 spin_lock_bh(&neigh_node->ifinfo_lock);
950 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
951 &neigh_ifinfo->bat_iv.tq_index,
952 batadv_ogm_packet->tq);
953 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
954 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
955 spin_unlock_bh(&neigh_node->ifinfo_lock);
956
957 if (dup_status == BATADV_NO_DUP) {
958 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
959 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
960 }
961
962
963
964
965 router = batadv_orig_router_get(orig_node, if_outgoing);
966 if (router == neigh_node)
967 goto out;
968
969 if (router) {
970 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
971 if (!router_ifinfo)
972 goto out;
973
974
975
976
977 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
978 goto out;
979 }
980
981
982
983
984 if (router_ifinfo &&
985 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
986 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
987 router->if_incoming);
988 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
989 neigh_node->if_incoming);
990 if (sum_orig >= sum_neigh)
991 goto out;
992 }
993
994 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
995 goto out;
996
997unlock:
998 rcu_read_unlock();
999out:
1000 if (neigh_node)
1001 batadv_neigh_node_put(neigh_node);
1002 if (router)
1003 batadv_neigh_node_put(router);
1004 if (neigh_ifinfo)
1005 batadv_neigh_ifinfo_put(neigh_ifinfo);
1006 if (router_ifinfo)
1007 batadv_neigh_ifinfo_put(router_ifinfo);
1008}
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1021 struct batadv_orig_node *orig_neigh_node,
1022 struct batadv_ogm_packet *batadv_ogm_packet,
1023 struct batadv_hard_iface *if_incoming,
1024 struct batadv_hard_iface *if_outgoing)
1025{
1026 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1027 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1028 struct batadv_neigh_ifinfo *neigh_ifinfo;
1029 u8 total_count;
1030 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1031 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1032 unsigned int tq_asym_penalty, inv_asym_penalty;
1033 unsigned int combined_tq;
1034 unsigned int tq_iface_penalty;
1035 bool ret = false;
1036
1037
1038 rcu_read_lock();
1039 hlist_for_each_entry_rcu(tmp_neigh_node,
1040 &orig_neigh_node->neigh_list, list) {
1041 if (!batadv_compare_eth(tmp_neigh_node->addr,
1042 orig_neigh_node->orig))
1043 continue;
1044
1045 if (tmp_neigh_node->if_incoming != if_incoming)
1046 continue;
1047
1048 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1049 continue;
1050
1051 neigh_node = tmp_neigh_node;
1052 break;
1053 }
1054 rcu_read_unlock();
1055
1056 if (!neigh_node)
1057 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1058 orig_neigh_node->orig,
1059 orig_neigh_node,
1060 orig_neigh_node);
1061
1062 if (!neigh_node)
1063 goto out;
1064
1065
1066 if (orig_node == orig_neigh_node)
1067 neigh_node->last_seen = jiffies;
1068
1069 orig_node->last_seen = jiffies;
1070
1071
1072 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1073 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1074 if (neigh_ifinfo) {
1075 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1076 batadv_neigh_ifinfo_put(neigh_ifinfo);
1077 } else {
1078 neigh_rq_count = 0;
1079 }
1080
1081
1082 if (orig_eq_count > neigh_rq_count)
1083 total_count = neigh_rq_count;
1084 else
1085 total_count = orig_eq_count;
1086
1087
1088
1089
1090 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1091 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1092 tq_own = 0;
1093 else
1094
1095
1096
1097
1098 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1099
1100
1101
1102
1103
1104
1105 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1106 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1107 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1108 BATADV_TQ_LOCAL_WINDOW_SIZE *
1109 BATADV_TQ_LOCAL_WINDOW_SIZE;
1110 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1111 inv_asym_penalty /= neigh_rq_max_cube;
1112 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1113
1114
1115
1116
1117
1118 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1119 if (if_outgoing && if_incoming == if_outgoing &&
1120 batadv_is_wifi_hardif(if_outgoing))
1121 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1122 bat_priv);
1123
1124 combined_tq = batadv_ogm_packet->tq *
1125 tq_own *
1126 tq_asym_penalty *
1127 tq_iface_penalty;
1128 combined_tq /= BATADV_TQ_MAX_VALUE *
1129 BATADV_TQ_MAX_VALUE *
1130 BATADV_TQ_MAX_VALUE;
1131 batadv_ogm_packet->tq = combined_tq;
1132
1133 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1134 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1135 orig_node->orig, orig_neigh_node->orig, total_count,
1136 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1137 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1138 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1139
1140
1141
1142
1143 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1144 ret = true;
1145
1146out:
1147 if (neigh_node)
1148 batadv_neigh_node_put(neigh_node);
1149 return ret;
1150}
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162static enum batadv_dup_status
1163batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1164 const struct batadv_ogm_packet *batadv_ogm_packet,
1165 const struct batadv_hard_iface *if_incoming,
1166 struct batadv_hard_iface *if_outgoing)
1167{
1168 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1169 struct batadv_orig_node *orig_node;
1170 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1171 struct batadv_neigh_node *neigh_node;
1172 struct batadv_neigh_ifinfo *neigh_ifinfo;
1173 bool is_dup;
1174 s32 seq_diff;
1175 bool need_update = false;
1176 int set_mark;
1177 enum batadv_dup_status ret = BATADV_NO_DUP;
1178 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1179 u8 *neigh_addr;
1180 u8 packet_count;
1181 unsigned long *bitmap;
1182
1183 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1184 if (!orig_node)
1185 return BATADV_NO_DUP;
1186
1187 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1188 if (WARN_ON(!orig_ifinfo)) {
1189 batadv_orig_node_put(orig_node);
1190 return 0;
1191 }
1192
1193 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1194 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1195
1196
1197 if (!hlist_empty(&orig_node->neigh_list) &&
1198 batadv_window_protected(bat_priv, seq_diff,
1199 BATADV_TQ_LOCAL_WINDOW_SIZE,
1200 &orig_ifinfo->batman_seqno_reset, NULL)) {
1201 ret = BATADV_PROTECTED;
1202 goto out;
1203 }
1204
1205 rcu_read_lock();
1206 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1207 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1208 if_outgoing);
1209 if (!neigh_ifinfo)
1210 continue;
1211
1212 neigh_addr = neigh_node->addr;
1213 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1214 orig_ifinfo->last_real_seqno,
1215 seqno);
1216
1217 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1218 neigh_node->if_incoming == if_incoming) {
1219 set_mark = 1;
1220 if (is_dup)
1221 ret = BATADV_NEIGH_DUP;
1222 } else {
1223 set_mark = 0;
1224 if (is_dup && ret != BATADV_NEIGH_DUP)
1225 ret = BATADV_ORIG_DUP;
1226 }
1227
1228
1229 bitmap = neigh_ifinfo->bat_iv.real_bits;
1230 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1231 seq_diff, set_mark);
1232
1233 packet_count = bitmap_weight(bitmap,
1234 BATADV_TQ_LOCAL_WINDOW_SIZE);
1235 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1236 batadv_neigh_ifinfo_put(neigh_ifinfo);
1237 }
1238 rcu_read_unlock();
1239
1240 if (need_update) {
1241 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1242 "%s updating last_seqno: old %u, new %u\n",
1243 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1244 orig_ifinfo->last_real_seqno, seqno);
1245 orig_ifinfo->last_real_seqno = seqno;
1246 }
1247
1248out:
1249 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1250 batadv_orig_node_put(orig_node);
1251 batadv_orig_ifinfo_put(orig_ifinfo);
1252 return ret;
1253}
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264static void
1265batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1266 struct batadv_orig_node *orig_node,
1267 struct batadv_hard_iface *if_incoming,
1268 struct batadv_hard_iface *if_outgoing)
1269{
1270 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1271 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1272 struct batadv_neigh_node *router = NULL;
1273 struct batadv_neigh_node *router_router = NULL;
1274 struct batadv_orig_node *orig_neigh_node;
1275 struct batadv_orig_ifinfo *orig_ifinfo;
1276 struct batadv_neigh_node *orig_neigh_router = NULL;
1277 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1278 struct batadv_ogm_packet *ogm_packet;
1279 enum batadv_dup_status dup_status;
1280 bool is_from_best_next_hop = false;
1281 bool is_single_hop_neigh = false;
1282 bool sameseq, similar_ttl;
1283 struct sk_buff *skb_priv;
1284 struct ethhdr *ethhdr;
1285 u8 *prev_sender;
1286 bool is_bidirect;
1287
1288
1289
1290
1291 skb_priv = skb_copy(skb, GFP_ATOMIC);
1292 if (!skb_priv)
1293 return;
1294
1295 ethhdr = eth_hdr(skb_priv);
1296 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1297
1298 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1299 if_incoming, if_outgoing);
1300 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1301 is_single_hop_neigh = true;
1302
1303 if (dup_status == BATADV_PROTECTED) {
1304 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1305 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1306 ethhdr->h_source);
1307 goto out;
1308 }
1309
1310 if (ogm_packet->tq == 0) {
1311 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1312 "Drop packet: originator packet with tq equal 0\n");
1313 goto out;
1314 }
1315
1316 if (is_single_hop_neigh) {
1317 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1318 ethhdr->h_source);
1319 if (hardif_neigh)
1320 hardif_neigh->last_seen = jiffies;
1321 }
1322
1323 router = batadv_orig_router_get(orig_node, if_outgoing);
1324 if (router) {
1325 router_router = batadv_orig_router_get(router->orig_node,
1326 if_outgoing);
1327 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1328 }
1329
1330 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1331 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1332 is_from_best_next_hop = true;
1333
1334 prev_sender = ogm_packet->prev_sender;
1335
1336 if (router && router_router &&
1337 (batadv_compare_eth(router->addr, prev_sender)) &&
1338 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1339 (batadv_compare_eth(router->addr, router_router->addr))) {
1340 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1341 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1342 ethhdr->h_source);
1343 goto out;
1344 }
1345
1346 if (if_outgoing == BATADV_IF_DEFAULT)
1347 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1348
1349
1350
1351
1352 if (is_single_hop_neigh)
1353 orig_neigh_node = orig_node;
1354 else
1355 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1356 ethhdr->h_source);
1357
1358 if (!orig_neigh_node)
1359 goto out;
1360
1361
1362 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1363 ogm_packet, is_single_hop_neigh);
1364
1365 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1366 if_outgoing);
1367
1368
1369
1370
1371 if (!is_single_hop_neigh && !orig_neigh_router) {
1372 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1373 "Drop packet: OGM via unknown neighbor!\n");
1374 goto out_neigh;
1375 }
1376
1377 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1378 ogm_packet, if_incoming,
1379 if_outgoing);
1380
1381
1382
1383
1384 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1385 if (!orig_ifinfo)
1386 goto out_neigh;
1387
1388 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1389 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1390
1391 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1392 (sameseq && similar_ttl))) {
1393 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1394 orig_ifinfo, ethhdr,
1395 ogm_packet, if_incoming,
1396 if_outgoing, dup_status);
1397 }
1398 batadv_orig_ifinfo_put(orig_ifinfo);
1399
1400
1401 if (if_outgoing == BATADV_IF_DEFAULT)
1402 goto out_neigh;
1403
1404
1405 if (is_single_hop_neigh) {
1406
1407
1408
1409 if (ogm_packet->ttl <= 2 &&
1410 if_incoming != if_outgoing) {
1411 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1412 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1413 goto out_neigh;
1414 }
1415
1416 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1417 is_single_hop_neigh,
1418 is_from_best_next_hop, if_incoming,
1419 if_outgoing);
1420
1421 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1422 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1423 goto out_neigh;
1424 }
1425
1426
1427 if (!is_bidirect) {
1428 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1429 "Drop packet: not received via bidirectional link\n");
1430 goto out_neigh;
1431 }
1432
1433 if (dup_status == BATADV_NEIGH_DUP) {
1434 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1435 "Drop packet: duplicate packet received\n");
1436 goto out_neigh;
1437 }
1438
1439 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1440 "Forwarding packet: rebroadcast originator packet\n");
1441 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1442 is_single_hop_neigh, is_from_best_next_hop,
1443 if_incoming, if_outgoing);
1444
1445out_neigh:
1446 if (orig_neigh_node && !is_single_hop_neigh)
1447 batadv_orig_node_put(orig_neigh_node);
1448out:
1449 if (router_ifinfo)
1450 batadv_neigh_ifinfo_put(router_ifinfo);
1451 if (router)
1452 batadv_neigh_node_put(router);
1453 if (router_router)
1454 batadv_neigh_node_put(router_router);
1455 if (orig_neigh_router)
1456 batadv_neigh_node_put(orig_neigh_router);
1457 if (hardif_neigh)
1458 batadv_hardif_neigh_put(hardif_neigh);
1459
1460 consume_skb(skb_priv);
1461}
1462
1463
1464
1465
1466
1467
1468
1469
1470static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1471 struct batadv_hard_iface *if_incoming,
1472 struct batadv_orig_node *orig_node,
1473 u32 if_incoming_seqno)
1474{
1475 struct batadv_orig_ifinfo *orig_ifinfo;
1476 s32 bit_pos;
1477 u8 *weight;
1478
1479
1480
1481
1482 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1483 return;
1484
1485 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1486 ogm_packet->orig))
1487 return;
1488
1489 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1490 if (!orig_ifinfo)
1491 return;
1492
1493
1494 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1495 bit_pos = if_incoming_seqno - 2;
1496 bit_pos -= ntohl(ogm_packet->seqno);
1497 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1498 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1499 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1500 BATADV_TQ_LOCAL_WINDOW_SIZE);
1501 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1502
1503 batadv_orig_ifinfo_put(orig_ifinfo);
1504}
1505
1506
1507
1508
1509
1510
1511
1512static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1513 struct batadv_hard_iface *if_incoming)
1514{
1515 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1516 struct batadv_orig_node *orig_neigh_node, *orig_node;
1517 struct batadv_hard_iface *hard_iface;
1518 struct batadv_ogm_packet *ogm_packet;
1519 u32 if_incoming_seqno;
1520 bool has_directlink_flag;
1521 struct ethhdr *ethhdr;
1522 bool is_my_oldorig = false;
1523 bool is_my_addr = false;
1524 bool is_my_orig = false;
1525
1526 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1527 ethhdr = eth_hdr(skb);
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541 if (ogm_packet->packet_type != BATADV_IV_OGM)
1542 return;
1543
1544
1545 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1546
1547 if (ogm_packet->flags & BATADV_DIRECTLINK)
1548 has_directlink_flag = true;
1549 else
1550 has_directlink_flag = false;
1551
1552 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1553 "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",
1554 ethhdr->h_source, if_incoming->net_dev->name,
1555 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1556 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1557 ogm_packet->tq, ogm_packet->ttl,
1558 ogm_packet->version, has_directlink_flag);
1559
1560 rcu_read_lock();
1561 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1562 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1563 continue;
1564
1565 if (hard_iface->soft_iface != if_incoming->soft_iface)
1566 continue;
1567
1568 if (batadv_compare_eth(ethhdr->h_source,
1569 hard_iface->net_dev->dev_addr))
1570 is_my_addr = true;
1571
1572 if (batadv_compare_eth(ogm_packet->orig,
1573 hard_iface->net_dev->dev_addr))
1574 is_my_orig = true;
1575
1576 if (batadv_compare_eth(ogm_packet->prev_sender,
1577 hard_iface->net_dev->dev_addr))
1578 is_my_oldorig = true;
1579 }
1580 rcu_read_unlock();
1581
1582 if (is_my_addr) {
1583 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1584 "Drop packet: received my own broadcast (sender: %pM)\n",
1585 ethhdr->h_source);
1586 return;
1587 }
1588
1589 if (is_my_orig) {
1590 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1591 ethhdr->h_source);
1592 if (!orig_neigh_node)
1593 return;
1594
1595 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1596 orig_neigh_node, if_incoming_seqno);
1597
1598 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1599 "Drop packet: originator packet from myself (via neighbor)\n");
1600 batadv_orig_node_put(orig_neigh_node);
1601 return;
1602 }
1603
1604 if (is_my_oldorig) {
1605 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1606 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1607 ethhdr->h_source);
1608 return;
1609 }
1610
1611 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1612 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1613 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1614 ethhdr->h_source);
1615 return;
1616 }
1617
1618 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1619 if (!orig_node)
1620 return;
1621
1622 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1623 if_incoming, BATADV_IF_DEFAULT);
1624
1625 rcu_read_lock();
1626 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1627 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1628 continue;
1629
1630 if (hard_iface->soft_iface != bat_priv->soft_iface)
1631 continue;
1632
1633 if (!kref_get_unless_zero(&hard_iface->refcount))
1634 continue;
1635
1636 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1637 if_incoming, hard_iface);
1638
1639 batadv_hardif_put(hard_iface);
1640 }
1641 rcu_read_unlock();
1642
1643 batadv_orig_node_put(orig_node);
1644}
1645
1646static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1647{
1648 struct delayed_work *delayed_work;
1649 struct batadv_forw_packet *forw_packet;
1650 struct batadv_priv *bat_priv;
1651 bool dropped = false;
1652
1653 delayed_work = to_delayed_work(work);
1654 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1655 delayed_work);
1656 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1657
1658 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1659 dropped = true;
1660 goto out;
1661 }
1662
1663 batadv_iv_ogm_emit(forw_packet);
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673 if (forw_packet->own &&
1674 forw_packet->if_incoming == forw_packet->if_outgoing)
1675 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1676
1677out:
1678
1679 if (batadv_forw_packet_steal(forw_packet,
1680 &bat_priv->forw_bat_list_lock))
1681 batadv_forw_packet_free(forw_packet, dropped);
1682}
1683
1684static int batadv_iv_ogm_receive(struct sk_buff *skb,
1685 struct batadv_hard_iface *if_incoming)
1686{
1687 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1688 struct batadv_ogm_packet *ogm_packet;
1689 u8 *packet_pos;
1690 int ogm_offset;
1691 bool res;
1692 int ret = NET_RX_DROP;
1693
1694 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1695 if (!res)
1696 goto free_skb;
1697
1698
1699
1700
1701 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1702 goto free_skb;
1703
1704 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1705 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1706 skb->len + ETH_HLEN);
1707
1708 ogm_offset = 0;
1709 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1710
1711
1712 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1713 ogm_packet)) {
1714 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1715
1716 ogm_offset += BATADV_OGM_HLEN;
1717 ogm_offset += ntohs(ogm_packet->tvlv_len);
1718
1719 packet_pos = skb->data + ogm_offset;
1720 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1721 }
1722
1723 ret = NET_RX_SUCCESS;
1724
1725free_skb:
1726 if (ret == NET_RX_SUCCESS)
1727 consume_skb(skb);
1728 else
1729 kfree_skb(skb);
1730
1731 return ret;
1732}
1733
1734#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1735
1736
1737
1738
1739
1740
1741
1742
1743static void
1744batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1745 struct batadv_hard_iface *if_outgoing,
1746 struct seq_file *seq)
1747{
1748 struct batadv_neigh_node *neigh_node;
1749 struct batadv_neigh_ifinfo *n_ifinfo;
1750
1751 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1752 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1753 if (!n_ifinfo)
1754 continue;
1755
1756 seq_printf(seq, " %pM (%3i)",
1757 neigh_node->addr,
1758 n_ifinfo->bat_iv.tq_avg);
1759
1760 batadv_neigh_ifinfo_put(n_ifinfo);
1761 }
1762}
1763
1764
1765
1766
1767
1768
1769
1770static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1771 struct seq_file *seq,
1772 struct batadv_hard_iface *if_outgoing)
1773{
1774 struct batadv_neigh_node *neigh_node;
1775 struct batadv_hashtable *hash = bat_priv->orig_hash;
1776 int last_seen_msecs, last_seen_secs;
1777 struct batadv_orig_node *orig_node;
1778 struct batadv_neigh_ifinfo *n_ifinfo;
1779 unsigned long last_seen_jiffies;
1780 struct hlist_head *head;
1781 int batman_count = 0;
1782 u32 i;
1783
1784 seq_puts(seq,
1785 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1786
1787 for (i = 0; i < hash->size; i++) {
1788 head = &hash->table[i];
1789
1790 rcu_read_lock();
1791 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1792 neigh_node = batadv_orig_router_get(orig_node,
1793 if_outgoing);
1794 if (!neigh_node)
1795 continue;
1796
1797 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1798 if_outgoing);
1799 if (!n_ifinfo)
1800 goto next;
1801
1802 if (n_ifinfo->bat_iv.tq_avg == 0)
1803 goto next;
1804
1805 last_seen_jiffies = jiffies - orig_node->last_seen;
1806 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1807 last_seen_secs = last_seen_msecs / 1000;
1808 last_seen_msecs = last_seen_msecs % 1000;
1809
1810 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1811 orig_node->orig, last_seen_secs,
1812 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1813 neigh_node->addr,
1814 neigh_node->if_incoming->net_dev->name);
1815
1816 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1817 seq);
1818 seq_putc(seq, '\n');
1819 batman_count++;
1820
1821next:
1822 batadv_neigh_node_put(neigh_node);
1823 if (n_ifinfo)
1824 batadv_neigh_ifinfo_put(n_ifinfo);
1825 }
1826 rcu_read_unlock();
1827 }
1828
1829 if (batman_count == 0)
1830 seq_puts(seq, "No batman nodes in range ...\n");
1831}
1832#endif
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843static bool
1844batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1845 struct batadv_hard_iface *if_outgoing,
1846 u8 *tq_avg)
1847{
1848 struct batadv_neigh_ifinfo *n_ifinfo;
1849
1850 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1851 if (!n_ifinfo)
1852 return false;
1853
1854 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1855 batadv_neigh_ifinfo_put(n_ifinfo);
1856
1857 return true;
1858}
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874static int
1875batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1876 struct batadv_priv *bat_priv,
1877 struct batadv_hard_iface *if_outgoing,
1878 struct batadv_orig_node *orig_node,
1879 struct batadv_neigh_node *neigh_node,
1880 bool best)
1881{
1882 void *hdr;
1883 u8 tq_avg;
1884 unsigned int last_seen_msecs;
1885
1886 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1887
1888 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1889 return 0;
1890
1891 if (if_outgoing != BATADV_IF_DEFAULT &&
1892 if_outgoing != neigh_node->if_incoming)
1893 return 0;
1894
1895 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1896 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1897 if (!hdr)
1898 return -ENOBUFS;
1899
1900 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1901 orig_node->orig) ||
1902 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1903 neigh_node->addr) ||
1904 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1905 neigh_node->if_incoming->net_dev->ifindex) ||
1906 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1907 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1908 last_seen_msecs))
1909 goto nla_put_failure;
1910
1911 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1912 goto nla_put_failure;
1913
1914 genlmsg_end(msg, hdr);
1915 return 0;
1916
1917 nla_put_failure:
1918 genlmsg_cancel(msg, hdr);
1919 return -EMSGSIZE;
1920}
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936static int
1937batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1938 struct batadv_priv *bat_priv,
1939 struct batadv_hard_iface *if_outgoing,
1940 struct batadv_orig_node *orig_node, int *sub_s)
1941{
1942 struct batadv_neigh_node *neigh_node_best;
1943 struct batadv_neigh_node *neigh_node;
1944 int sub = 0;
1945 bool best;
1946 u8 tq_avg_best;
1947
1948 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1949 if (!neigh_node_best)
1950 goto out;
1951
1952 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1953 &tq_avg_best))
1954 goto out;
1955
1956 if (tq_avg_best == 0)
1957 goto out;
1958
1959 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1960 if (sub++ < *sub_s)
1961 continue;
1962
1963 best = (neigh_node == neigh_node_best);
1964
1965 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1966 bat_priv, if_outgoing,
1967 orig_node, neigh_node,
1968 best)) {
1969 batadv_neigh_node_put(neigh_node_best);
1970
1971 *sub_s = sub - 1;
1972 return -EMSGSIZE;
1973 }
1974 }
1975
1976 out:
1977 if (neigh_node_best)
1978 batadv_neigh_node_put(neigh_node_best);
1979
1980 *sub_s = 0;
1981 return 0;
1982}
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998static int
1999batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2000 struct batadv_priv *bat_priv,
2001 struct batadv_hard_iface *if_outgoing,
2002 struct hlist_head *head, int *idx_s, int *sub)
2003{
2004 struct batadv_orig_node *orig_node;
2005 int idx = 0;
2006
2007 rcu_read_lock();
2008 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2009 if (idx++ < *idx_s)
2010 continue;
2011
2012 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2013 if_outgoing, orig_node,
2014 sub)) {
2015 rcu_read_unlock();
2016 *idx_s = idx - 1;
2017 return -EMSGSIZE;
2018 }
2019 }
2020 rcu_read_unlock();
2021
2022 *idx_s = 0;
2023 *sub = 0;
2024 return 0;
2025}
2026
2027
2028
2029
2030
2031
2032
2033
2034static void
2035batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2036 struct batadv_priv *bat_priv,
2037 struct batadv_hard_iface *if_outgoing)
2038{
2039 struct batadv_hashtable *hash = bat_priv->orig_hash;
2040 struct hlist_head *head;
2041 int bucket = cb->args[0];
2042 int idx = cb->args[1];
2043 int sub = cb->args[2];
2044 int portid = NETLINK_CB(cb->skb).portid;
2045
2046 while (bucket < hash->size) {
2047 head = &hash->table[bucket];
2048
2049 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2050 cb->nlh->nlmsg_seq,
2051 bat_priv, if_outgoing, head,
2052 &idx, &sub))
2053 break;
2054
2055 bucket++;
2056 }
2057
2058 cb->args[0] = bucket;
2059 cb->args[1] = idx;
2060 cb->args[2] = sub;
2061}
2062
2063#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2064
2065
2066
2067
2068
2069static void
2070batadv_iv_hardif_neigh_print(struct seq_file *seq,
2071 struct batadv_hardif_neigh_node *hardif_neigh)
2072{
2073 int last_secs, last_msecs;
2074
2075 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2076 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2077
2078 seq_printf(seq, " %10s %pM %4i.%03is\n",
2079 hardif_neigh->if_incoming->net_dev->name,
2080 hardif_neigh->addr, last_secs, last_msecs);
2081}
2082
2083
2084
2085
2086
2087
2088static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2089 struct seq_file *seq)
2090{
2091 struct net_device *net_dev = (struct net_device *)seq->private;
2092 struct batadv_hardif_neigh_node *hardif_neigh;
2093 struct batadv_hard_iface *hard_iface;
2094 int batman_count = 0;
2095
2096 seq_puts(seq, " IF Neighbor last-seen\n");
2097
2098 rcu_read_lock();
2099 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2100 if (hard_iface->soft_iface != net_dev)
2101 continue;
2102
2103 hlist_for_each_entry_rcu(hardif_neigh,
2104 &hard_iface->neigh_list, list) {
2105 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2106 batman_count++;
2107 }
2108 }
2109 rcu_read_unlock();
2110
2111 if (batman_count == 0)
2112 seq_puts(seq, "No batman nodes in range ...\n");
2113}
2114#endif
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2131 struct batadv_hard_iface *if_outgoing1,
2132 struct batadv_neigh_node *neigh2,
2133 struct batadv_hard_iface *if_outgoing2,
2134 int *diff)
2135{
2136 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2137 u8 tq1, tq2;
2138 bool ret = true;
2139
2140 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2141 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2142
2143 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2144 ret = false;
2145 goto out;
2146 }
2147
2148 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2149 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2150 *diff = (int)tq1 - (int)tq2;
2151
2152out:
2153 if (neigh1_ifinfo)
2154 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2155 if (neigh2_ifinfo)
2156 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2157
2158 return ret;
2159}
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170static int
2171batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2172 struct batadv_hardif_neigh_node *hardif_neigh)
2173{
2174 void *hdr;
2175 unsigned int last_seen_msecs;
2176
2177 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2178
2179 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2180 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2181 if (!hdr)
2182 return -ENOBUFS;
2183
2184 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2185 hardif_neigh->addr) ||
2186 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2187 hardif_neigh->if_incoming->net_dev->ifindex) ||
2188 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2189 last_seen_msecs))
2190 goto nla_put_failure;
2191
2192 genlmsg_end(msg, hdr);
2193 return 0;
2194
2195 nla_put_failure:
2196 genlmsg_cancel(msg, hdr);
2197 return -EMSGSIZE;
2198}
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214static int
2215batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2216 struct batadv_priv *bat_priv,
2217 struct batadv_hard_iface *hard_iface,
2218 int *idx_s)
2219{
2220 struct batadv_hardif_neigh_node *hardif_neigh;
2221 int idx = 0;
2222
2223 hlist_for_each_entry_rcu(hardif_neigh,
2224 &hard_iface->neigh_list, list) {
2225 if (idx++ < *idx_s)
2226 continue;
2227
2228 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2229 hardif_neigh)) {
2230 *idx_s = idx - 1;
2231 return -EMSGSIZE;
2232 }
2233 }
2234
2235 *idx_s = 0;
2236 return 0;
2237}
2238
2239
2240
2241
2242
2243
2244
2245
2246static void
2247batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2248 struct batadv_priv *bat_priv,
2249 struct batadv_hard_iface *single_hardif)
2250{
2251 struct batadv_hard_iface *hard_iface;
2252 int i_hardif = 0;
2253 int i_hardif_s = cb->args[0];
2254 int idx = cb->args[1];
2255 int portid = NETLINK_CB(cb->skb).portid;
2256
2257 rcu_read_lock();
2258 if (single_hardif) {
2259 if (i_hardif_s == 0) {
2260 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2261 cb->nlh->nlmsg_seq,
2262 bat_priv,
2263 single_hardif,
2264 &idx) == 0)
2265 i_hardif++;
2266 }
2267 } else {
2268 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2269 list) {
2270 if (hard_iface->soft_iface != bat_priv->soft_iface)
2271 continue;
2272
2273 if (i_hardif++ < i_hardif_s)
2274 continue;
2275
2276 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2277 cb->nlh->nlmsg_seq,
2278 bat_priv,
2279 hard_iface, &idx)) {
2280 i_hardif--;
2281 break;
2282 }
2283 }
2284 }
2285 rcu_read_unlock();
2286
2287 cb->args[0] = i_hardif;
2288 cb->args[1] = idx;
2289}
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2302 struct batadv_hard_iface *if_outgoing1,
2303 struct batadv_neigh_node *neigh2,
2304 struct batadv_hard_iface *if_outgoing2)
2305{
2306 bool ret;
2307 int diff;
2308
2309 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2310 if_outgoing2, &diff);
2311 if (!ret)
2312 return 0;
2313
2314 return diff;
2315}
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328static bool
2329batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2330 struct batadv_hard_iface *if_outgoing1,
2331 struct batadv_neigh_node *neigh2,
2332 struct batadv_hard_iface *if_outgoing2)
2333{
2334 bool ret;
2335 int diff;
2336
2337 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2338 if_outgoing2, &diff);
2339 if (!ret)
2340 return false;
2341
2342 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2343 return ret;
2344}
2345
2346static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2347{
2348
2349 batadv_iv_ogm_schedule(hard_iface);
2350}
2351
2352
2353
2354
2355
2356static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2357{
2358
2359 atomic_set(&bat_priv->gw.sel_class, 20);
2360}
2361
2362static struct batadv_gw_node *
2363batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2364{
2365 struct batadv_neigh_node *router;
2366 struct batadv_neigh_ifinfo *router_ifinfo;
2367 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2368 u64 max_gw_factor = 0;
2369 u64 tmp_gw_factor = 0;
2370 u8 max_tq = 0;
2371 u8 tq_avg;
2372 struct batadv_orig_node *orig_node;
2373
2374 rcu_read_lock();
2375 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2376 orig_node = gw_node->orig_node;
2377 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2378 if (!router)
2379 continue;
2380
2381 router_ifinfo = batadv_neigh_ifinfo_get(router,
2382 BATADV_IF_DEFAULT);
2383 if (!router_ifinfo)
2384 goto next;
2385
2386 if (!kref_get_unless_zero(&gw_node->refcount))
2387 goto next;
2388
2389 tq_avg = router_ifinfo->bat_iv.tq_avg;
2390
2391 switch (atomic_read(&bat_priv->gw.sel_class)) {
2392 case 1:
2393 tmp_gw_factor = tq_avg * tq_avg;
2394 tmp_gw_factor *= gw_node->bandwidth_down;
2395 tmp_gw_factor *= 100 * 100;
2396 tmp_gw_factor >>= 18;
2397
2398 if (tmp_gw_factor > max_gw_factor ||
2399 (tmp_gw_factor == max_gw_factor &&
2400 tq_avg > max_tq)) {
2401 if (curr_gw)
2402 batadv_gw_node_put(curr_gw);
2403 curr_gw = gw_node;
2404 kref_get(&curr_gw->refcount);
2405 }
2406 break;
2407
2408 default:
2409
2410
2411
2412
2413
2414
2415 if (tq_avg > max_tq) {
2416 if (curr_gw)
2417 batadv_gw_node_put(curr_gw);
2418 curr_gw = gw_node;
2419 kref_get(&curr_gw->refcount);
2420 }
2421 break;
2422 }
2423
2424 if (tq_avg > max_tq)
2425 max_tq = tq_avg;
2426
2427 if (tmp_gw_factor > max_gw_factor)
2428 max_gw_factor = tmp_gw_factor;
2429
2430 batadv_gw_node_put(gw_node);
2431
2432next:
2433 batadv_neigh_node_put(router);
2434 if (router_ifinfo)
2435 batadv_neigh_ifinfo_put(router_ifinfo);
2436 }
2437 rcu_read_unlock();
2438
2439 return curr_gw;
2440}
2441
2442static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2443 struct batadv_orig_node *curr_gw_orig,
2444 struct batadv_orig_node *orig_node)
2445{
2446 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2447 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2448 struct batadv_neigh_node *router_gw = NULL;
2449 struct batadv_neigh_node *router_orig = NULL;
2450 u8 gw_tq_avg, orig_tq_avg;
2451 bool ret = false;
2452
2453
2454 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2455 return false;
2456
2457 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2458 if (!router_gw) {
2459 ret = true;
2460 goto out;
2461 }
2462
2463 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2464 BATADV_IF_DEFAULT);
2465 if (!router_gw_ifinfo) {
2466 ret = true;
2467 goto out;
2468 }
2469
2470 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2471 if (!router_orig)
2472 goto out;
2473
2474 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2475 BATADV_IF_DEFAULT);
2476 if (!router_orig_ifinfo)
2477 goto out;
2478
2479 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2480 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2481
2482
2483 if (orig_tq_avg < gw_tq_avg)
2484 goto out;
2485
2486
2487
2488
2489 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2490 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2491 goto out;
2492
2493 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2494 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2495 gw_tq_avg, orig_tq_avg);
2496
2497 ret = true;
2498out:
2499 if (router_gw_ifinfo)
2500 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2501 if (router_orig_ifinfo)
2502 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2503 if (router_gw)
2504 batadv_neigh_node_put(router_gw);
2505 if (router_orig)
2506 batadv_neigh_node_put(router_orig);
2507
2508 return ret;
2509}
2510
2511#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2512
2513static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2514 struct seq_file *seq,
2515 const struct batadv_gw_node *gw_node)
2516{
2517 struct batadv_gw_node *curr_gw;
2518 struct batadv_neigh_node *router;
2519 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2520 int ret = -1;
2521
2522 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2523 if (!router)
2524 goto out;
2525
2526 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2527 if (!router_ifinfo)
2528 goto out;
2529
2530 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2531
2532 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2533 (curr_gw == gw_node ? "=>" : " "),
2534 gw_node->orig_node->orig,
2535 router_ifinfo->bat_iv.tq_avg, router->addr,
2536 router->if_incoming->net_dev->name,
2537 gw_node->bandwidth_down / 10,
2538 gw_node->bandwidth_down % 10,
2539 gw_node->bandwidth_up / 10,
2540 gw_node->bandwidth_up % 10);
2541 ret = seq_has_overflowed(seq) ? -1 : 0;
2542
2543 if (curr_gw)
2544 batadv_gw_node_put(curr_gw);
2545out:
2546 if (router_ifinfo)
2547 batadv_neigh_ifinfo_put(router_ifinfo);
2548 if (router)
2549 batadv_neigh_node_put(router);
2550 return ret;
2551}
2552
2553static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2554 struct seq_file *seq)
2555{
2556 struct batadv_gw_node *gw_node;
2557 int gw_count = 0;
2558
2559 seq_puts(seq,
2560 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2561
2562 rcu_read_lock();
2563 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2564
2565 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2566 continue;
2567
2568 gw_count++;
2569 }
2570 rcu_read_unlock();
2571
2572 if (gw_count == 0)
2573 seq_puts(seq, "No gateways in range ...\n");
2574}
2575#endif
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2588 struct netlink_callback *cb,
2589 struct batadv_priv *bat_priv,
2590 struct batadv_gw_node *gw_node)
2591{
2592 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2593 struct batadv_neigh_node *router;
2594 struct batadv_gw_node *curr_gw = NULL;
2595 int ret = 0;
2596 void *hdr;
2597
2598 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2599 if (!router)
2600 goto out;
2601
2602 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2603 if (!router_ifinfo)
2604 goto out;
2605
2606 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2607
2608 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2609 &batadv_netlink_family, NLM_F_MULTI,
2610 BATADV_CMD_GET_GATEWAYS);
2611 if (!hdr) {
2612 ret = -ENOBUFS;
2613 goto out;
2614 }
2615
2616 genl_dump_check_consistent(cb, hdr);
2617
2618 ret = -EMSGSIZE;
2619
2620 if (curr_gw == gw_node)
2621 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2622 genlmsg_cancel(msg, hdr);
2623 goto out;
2624 }
2625
2626 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2627 gw_node->orig_node->orig) ||
2628 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2629 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2630 router->addr) ||
2631 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2632 router->if_incoming->net_dev->name) ||
2633 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2634 gw_node->bandwidth_down) ||
2635 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2636 gw_node->bandwidth_up)) {
2637 genlmsg_cancel(msg, hdr);
2638 goto out;
2639 }
2640
2641 genlmsg_end(msg, hdr);
2642 ret = 0;
2643
2644out:
2645 if (curr_gw)
2646 batadv_gw_node_put(curr_gw);
2647 if (router_ifinfo)
2648 batadv_neigh_ifinfo_put(router_ifinfo);
2649 if (router)
2650 batadv_neigh_node_put(router);
2651 return ret;
2652}
2653
2654
2655
2656
2657
2658
2659
2660static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2661 struct batadv_priv *bat_priv)
2662{
2663 int portid = NETLINK_CB(cb->skb).portid;
2664 struct batadv_gw_node *gw_node;
2665 int idx_skip = cb->args[0];
2666 int idx = 0;
2667
2668 spin_lock_bh(&bat_priv->gw.list_lock);
2669 cb->seq = bat_priv->gw.generation << 1 | 1;
2670
2671 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2672 if (idx++ < idx_skip)
2673 continue;
2674
2675 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2676 gw_node)) {
2677 idx_skip = idx - 1;
2678 goto unlock;
2679 }
2680 }
2681
2682 idx_skip = idx;
2683unlock:
2684 spin_unlock_bh(&bat_priv->gw.list_lock);
2685
2686 cb->args[0] = idx_skip;
2687}
2688
2689static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2690 .name = "BATMAN_IV",
2691 .iface = {
2692 .enable = batadv_iv_ogm_iface_enable,
2693 .enabled = batadv_iv_iface_enabled,
2694 .disable = batadv_iv_ogm_iface_disable,
2695 .update_mac = batadv_iv_ogm_iface_update_mac,
2696 .primary_set = batadv_iv_ogm_primary_iface_set,
2697 },
2698 .neigh = {
2699 .cmp = batadv_iv_ogm_neigh_cmp,
2700 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2701#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2702 .print = batadv_iv_neigh_print,
2703#endif
2704 .dump = batadv_iv_ogm_neigh_dump,
2705 },
2706 .orig = {
2707#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2708 .print = batadv_iv_ogm_orig_print,
2709#endif
2710 .dump = batadv_iv_ogm_orig_dump,
2711 },
2712 .gw = {
2713 .init_sel_class = batadv_iv_init_sel_class,
2714 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2715 .is_eligible = batadv_iv_gw_is_eligible,
2716#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2717 .print = batadv_iv_gw_print,
2718#endif
2719 .dump = batadv_iv_gw_dump,
2720 },
2721};
2722
2723
2724
2725
2726
2727
2728int __init batadv_iv_init(void)
2729{
2730 int ret;
2731
2732
2733 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2734 batadv_iv_ogm_receive);
2735 if (ret < 0)
2736 goto out;
2737
2738 ret = batadv_algo_register(&batadv_batman_iv);
2739 if (ret < 0)
2740 goto handler_unregister;
2741
2742 goto out;
2743
2744handler_unregister:
2745 batadv_recv_handler_unregister(BATADV_IV_OGM);
2746out:
2747 return ret;
2748}
2749