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