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