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