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