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