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