Linux 4.8-rc8
[linux/fpc-iii.git] / net / batman-adv / bat_iv_ogm.c
blob19b0abd6c640e26f11b26b7fd6f3eea82134e2ee
1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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/>.
18 #include "bat_iv_ogm.h"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/bitmap.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
29 #include <linux/fs.h>
30 #include <linux/if_ether.h>
31 #include <linux/init.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/pkt_sched.h>
39 #include <linux/printk.h>
40 #include <linux/random.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/seq_file.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/types.h>
50 #include <linux/workqueue.h>
52 #include "bat_algo.h"
53 #include "bitarray.h"
54 #include "hard-interface.h"
55 #include "hash.h"
56 #include "log.h"
57 #include "network-coding.h"
58 #include "originator.h"
59 #include "packet.h"
60 #include "routing.h"
61 #include "send.h"
62 #include "translation-table.h"
63 #include "tvlv.h"
65 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
67 /**
68 * enum batadv_dup_status - duplicate status
69 * @BATADV_NO_DUP: the packet is no duplicate
70 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
71 * neighbor)
72 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
73 * @BATADV_PROTECTED: originator is currently protected (after reboot)
75 enum batadv_dup_status {
76 BATADV_NO_DUP = 0,
77 BATADV_ORIG_DUP,
78 BATADV_NEIGH_DUP,
79 BATADV_PROTECTED,
82 /**
83 * batadv_ring_buffer_set - update the ring buffer with the given value
84 * @lq_recv: pointer to the ring buffer
85 * @lq_index: index to store the value at
86 * @value: value to store in the ring buffer
88 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
90 lq_recv[*lq_index] = value;
91 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
94 /**
95 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
96 * in the given ring buffer
97 * @lq_recv: pointer to the ring buffer
99 * Return: computed average value.
101 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
103 const u8 *ptr;
104 u16 count = 0;
105 u16 i = 0;
106 u16 sum = 0;
108 ptr = lq_recv;
110 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
111 if (*ptr != 0) {
112 count++;
113 sum += *ptr;
116 i++;
117 ptr++;
120 if (count == 0)
121 return 0;
123 return (u8)(sum / count);
127 * batadv_iv_ogm_orig_free - free the private resources allocated for this
128 * orig_node
129 * @orig_node: the orig_node for which the resources have to be free'd
131 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
133 kfree(orig_node->bat_iv.bcast_own);
134 kfree(orig_node->bat_iv.bcast_own_sum);
138 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
139 * include the new hard-interface
140 * @orig_node: the orig_node that has to be changed
141 * @max_if_num: the current amount of interfaces
143 * Return: 0 on success, a negative error code otherwise.
145 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
146 int max_if_num)
148 void *data_ptr;
149 size_t old_size;
150 int ret = -ENOMEM;
152 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
154 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
155 data_ptr = kmalloc_array(max_if_num,
156 BATADV_NUM_WORDS * sizeof(unsigned long),
157 GFP_ATOMIC);
158 if (!data_ptr)
159 goto unlock;
161 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
162 kfree(orig_node->bat_iv.bcast_own);
163 orig_node->bat_iv.bcast_own = data_ptr;
165 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
166 if (!data_ptr)
167 goto unlock;
169 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
170 (max_if_num - 1) * sizeof(u8));
171 kfree(orig_node->bat_iv.bcast_own_sum);
172 orig_node->bat_iv.bcast_own_sum = data_ptr;
174 ret = 0;
176 unlock:
177 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
179 return ret;
183 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
184 * @orig_node: the orig_node that has to be changed
185 * @max_if_num: the current amount of interfaces
186 * @del_if_num: the index of the interface being removed
188 static void
189 batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
190 int max_if_num, int del_if_num)
192 size_t chunk_size;
193 size_t if_offset;
194 void *data_ptr;
196 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
198 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
199 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
200 if (!data_ptr)
201 /* use old buffer when new one could not be allocated */
202 data_ptr = orig_node->bat_iv.bcast_own;
204 /* copy first part */
205 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
207 /* copy second part */
208 if_offset = (del_if_num + 1) * chunk_size;
209 memmove((char *)data_ptr + del_if_num * chunk_size,
210 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
211 (max_if_num - del_if_num) * chunk_size);
213 /* bcast_own was shrunk down in new buffer; free old one */
214 if (orig_node->bat_iv.bcast_own != data_ptr) {
215 kfree(orig_node->bat_iv.bcast_own);
216 orig_node->bat_iv.bcast_own = data_ptr;
221 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
222 * @orig_node: the orig_node that has to be changed
223 * @max_if_num: the current amount of interfaces
224 * @del_if_num: the index of the interface being removed
226 static void
227 batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
228 int max_if_num, int del_if_num)
230 size_t if_offset;
231 void *data_ptr;
233 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
235 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
236 if (!data_ptr)
237 /* use old buffer when new one could not be allocated */
238 data_ptr = orig_node->bat_iv.bcast_own_sum;
240 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
241 del_if_num * sizeof(u8));
243 if_offset = (del_if_num + 1) * sizeof(u8);
244 memmove((char *)data_ptr + del_if_num * sizeof(u8),
245 orig_node->bat_iv.bcast_own_sum + if_offset,
246 (max_if_num - del_if_num) * sizeof(u8));
248 /* bcast_own_sum was shrunk down in new buffer; free old one */
249 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
250 kfree(orig_node->bat_iv.bcast_own_sum);
251 orig_node->bat_iv.bcast_own_sum = data_ptr;
256 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
257 * exclude the removed interface
258 * @orig_node: the orig_node that has to be changed
259 * @max_if_num: the current amount of interfaces
260 * @del_if_num: the index of the interface being removed
262 * Return: 0 on success, a negative error code otherwise.
264 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
265 int max_if_num, int del_if_num)
267 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
269 if (max_if_num == 0) {
270 kfree(orig_node->bat_iv.bcast_own);
271 kfree(orig_node->bat_iv.bcast_own_sum);
272 orig_node->bat_iv.bcast_own = NULL;
273 orig_node->bat_iv.bcast_own_sum = NULL;
274 } else {
275 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
276 del_if_num);
277 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
278 del_if_num);
281 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
283 return 0;
287 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
288 * @bat_priv: the bat priv with all the soft interface information
289 * @addr: mac address of the originator
291 * Return: the originator object corresponding to the passed mac address or NULL
292 * on failure.
293 * If the object does not exists it is created an initialised.
295 static struct batadv_orig_node *
296 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
298 struct batadv_orig_node *orig_node;
299 int size, hash_added;
301 orig_node = batadv_orig_hash_find(bat_priv, addr);
302 if (orig_node)
303 return orig_node;
305 orig_node = batadv_orig_node_new(bat_priv, addr);
306 if (!orig_node)
307 return NULL;
309 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
311 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
312 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
313 if (!orig_node->bat_iv.bcast_own)
314 goto free_orig_node;
316 size = bat_priv->num_ifaces * sizeof(u8);
317 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
318 if (!orig_node->bat_iv.bcast_own_sum)
319 goto free_orig_node;
321 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
322 batadv_choose_orig, orig_node,
323 &orig_node->hash_entry);
324 if (hash_added != 0)
325 goto free_orig_node;
327 return orig_node;
329 free_orig_node:
330 /* free twice, as batadv_orig_node_new sets refcount to 2 */
331 batadv_orig_node_put(orig_node);
332 batadv_orig_node_put(orig_node);
334 return NULL;
337 static struct batadv_neigh_node *
338 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
339 const u8 *neigh_addr,
340 struct batadv_orig_node *orig_node,
341 struct batadv_orig_node *orig_neigh)
343 struct batadv_neigh_node *neigh_node;
345 neigh_node = batadv_neigh_node_get_or_create(orig_node,
346 hard_iface, neigh_addr);
347 if (!neigh_node)
348 goto out;
350 neigh_node->orig_node = orig_neigh;
352 out:
353 return neigh_node;
356 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
358 struct batadv_ogm_packet *batadv_ogm_packet;
359 unsigned char *ogm_buff;
360 u32 random_seqno;
362 /* randomize initial seqno to avoid collision */
363 get_random_bytes(&random_seqno, sizeof(random_seqno));
364 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
366 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
367 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
368 if (!ogm_buff)
369 return -ENOMEM;
371 hard_iface->bat_iv.ogm_buff = ogm_buff;
373 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
374 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
375 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
376 batadv_ogm_packet->ttl = 2;
377 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
378 batadv_ogm_packet->reserved = 0;
379 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
381 return 0;
384 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
386 kfree(hard_iface->bat_iv.ogm_buff);
387 hard_iface->bat_iv.ogm_buff = NULL;
390 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
392 struct batadv_ogm_packet *batadv_ogm_packet;
393 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
395 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
396 ether_addr_copy(batadv_ogm_packet->orig,
397 hard_iface->net_dev->dev_addr);
398 ether_addr_copy(batadv_ogm_packet->prev_sender,
399 hard_iface->net_dev->dev_addr);
402 static void
403 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
405 struct batadv_ogm_packet *batadv_ogm_packet;
406 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
408 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
409 batadv_ogm_packet->ttl = BATADV_TTL;
412 /* when do we schedule our own ogm to be sent */
413 static unsigned long
414 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
416 unsigned int msecs;
418 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
419 msecs += prandom_u32() % (2 * BATADV_JITTER);
421 return jiffies + msecs_to_jiffies(msecs);
424 /* when do we schedule a ogm packet to be sent */
425 static unsigned long batadv_iv_ogm_fwd_send_time(void)
427 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
430 /* apply hop penalty for a normal link */
431 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
433 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
434 int new_tq;
436 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
437 new_tq /= BATADV_TQ_MAX_VALUE;
439 return new_tq;
443 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
444 * @buff_pos: current position in the skb
445 * @packet_len: total length of the skb
446 * @tvlv_len: tvlv length of the previously considered OGM
448 * Return: true if there is enough space for another OGM, false otherwise.
450 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
451 __be16 tvlv_len)
453 int next_buff_pos = 0;
455 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
456 next_buff_pos += ntohs(tvlv_len);
458 return (next_buff_pos <= packet_len) &&
459 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
462 /* send a batman ogm to a given interface */
463 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
464 struct batadv_hard_iface *hard_iface)
466 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
467 const char *fwd_str;
468 u8 packet_num;
469 s16 buff_pos;
470 struct batadv_ogm_packet *batadv_ogm_packet;
471 struct sk_buff *skb;
472 u8 *packet_pos;
474 if (hard_iface->if_status != BATADV_IF_ACTIVE)
475 return;
477 packet_num = 0;
478 buff_pos = 0;
479 packet_pos = forw_packet->skb->data;
480 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
482 /* adjust all flags and log packets */
483 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
484 batadv_ogm_packet->tvlv_len)) {
485 /* we might have aggregated direct link packets with an
486 * ordinary base packet
488 if (forw_packet->direct_link_flags & BIT(packet_num) &&
489 forw_packet->if_incoming == hard_iface)
490 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
491 else
492 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
494 if (packet_num > 0 || !forw_packet->own)
495 fwd_str = "Forwarding";
496 else
497 fwd_str = "Sending own";
499 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
500 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
501 fwd_str, (packet_num > 0 ? "aggregated " : ""),
502 batadv_ogm_packet->orig,
503 ntohl(batadv_ogm_packet->seqno),
504 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
505 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
506 "on" : "off"),
507 hard_iface->net_dev->name,
508 hard_iface->net_dev->dev_addr);
510 buff_pos += BATADV_OGM_HLEN;
511 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
512 packet_num++;
513 packet_pos = forw_packet->skb->data + buff_pos;
514 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
517 /* create clone because function is called more than once */
518 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
519 if (skb) {
520 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
521 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
522 skb->len + ETH_HLEN);
523 batadv_send_broadcast_skb(skb, hard_iface);
527 /* send a batman ogm packet */
528 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
530 struct net_device *soft_iface;
531 struct batadv_priv *bat_priv;
532 struct batadv_hard_iface *primary_if = NULL;
534 if (!forw_packet->if_incoming) {
535 pr_err("Error - can't forward packet: incoming iface not specified\n");
536 goto out;
539 soft_iface = forw_packet->if_incoming->soft_iface;
540 bat_priv = netdev_priv(soft_iface);
542 if (WARN_ON(!forw_packet->if_outgoing))
543 goto out;
545 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
546 goto out;
548 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
549 goto out;
551 primary_if = batadv_primary_if_get_selected(bat_priv);
552 if (!primary_if)
553 goto out;
555 /* only for one specific outgoing interface */
556 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
558 out:
559 if (primary_if)
560 batadv_hardif_put(primary_if);
564 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
565 * existing forward packet
566 * @new_bat_ogm_packet: OGM packet to be aggregated
567 * @bat_priv: the bat priv with all the soft interface information
568 * @packet_len: (total) length of the OGM
569 * @send_time: timestamp (jiffies) when the packet is to be sent
570 * @directlink: true if this is a direct link packet
571 * @if_incoming: interface where the packet was received
572 * @if_outgoing: interface for which the retransmission should be considered
573 * @forw_packet: the forwarded packet which should be checked
575 * Return: true if new_packet can be aggregated with forw_packet
577 static bool
578 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
579 struct batadv_priv *bat_priv,
580 int packet_len, unsigned long send_time,
581 bool directlink,
582 const struct batadv_hard_iface *if_incoming,
583 const struct batadv_hard_iface *if_outgoing,
584 const struct batadv_forw_packet *forw_packet)
586 struct batadv_ogm_packet *batadv_ogm_packet;
587 int aggregated_bytes = forw_packet->packet_len + packet_len;
588 struct batadv_hard_iface *primary_if = NULL;
589 bool res = false;
590 unsigned long aggregation_end_time;
592 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
593 aggregation_end_time = send_time;
594 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
596 /* we can aggregate the current packet to this aggregated packet
597 * if:
599 * - the send time is within our MAX_AGGREGATION_MS time
600 * - the resulting packet wont be bigger than
601 * MAX_AGGREGATION_BYTES
602 * otherwise aggregation is not possible
604 if (!time_before(send_time, forw_packet->send_time) ||
605 !time_after_eq(aggregation_end_time, forw_packet->send_time))
606 return false;
608 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
609 return false;
611 /* packet is not leaving on the same interface. */
612 if (forw_packet->if_outgoing != if_outgoing)
613 return false;
615 /* check aggregation compatibility
616 * -> direct link packets are broadcasted on
617 * their interface only
618 * -> aggregate packet if the current packet is
619 * a "global" packet as well as the base
620 * packet
622 primary_if = batadv_primary_if_get_selected(bat_priv);
623 if (!primary_if)
624 return false;
626 /* packets without direct link flag and high TTL
627 * are flooded through the net
629 if (!directlink &&
630 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
631 batadv_ogm_packet->ttl != 1 &&
633 /* own packets originating non-primary
634 * interfaces leave only that interface
636 (!forw_packet->own ||
637 forw_packet->if_incoming == primary_if)) {
638 res = true;
639 goto out;
642 /* if the incoming packet is sent via this one
643 * interface only - we still can aggregate
645 if (directlink &&
646 new_bat_ogm_packet->ttl == 1 &&
647 forw_packet->if_incoming == if_incoming &&
649 /* packets from direct neighbors or
650 * own secondary interface packets
651 * (= secondary interface packets in general)
653 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
654 (forw_packet->own &&
655 forw_packet->if_incoming != primary_if))) {
656 res = true;
657 goto out;
660 out:
661 if (primary_if)
662 batadv_hardif_put(primary_if);
663 return res;
667 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
668 * packet to it.
669 * @packet_buff: pointer to the OGM
670 * @packet_len: (total) length of the OGM
671 * @send_time: timestamp (jiffies) when the packet is to be sent
672 * @direct_link: whether this OGM has direct link status
673 * @if_incoming: interface where the packet was received
674 * @if_outgoing: interface for which the retransmission should be considered
675 * @own_packet: true if it is a self-generated ogm
677 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
678 int packet_len, unsigned long send_time,
679 bool direct_link,
680 struct batadv_hard_iface *if_incoming,
681 struct batadv_hard_iface *if_outgoing,
682 int own_packet)
684 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
685 struct batadv_forw_packet *forw_packet_aggr;
686 unsigned char *skb_buff;
687 unsigned int skb_size;
689 /* own packet should always be scheduled */
690 if (!own_packet) {
691 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
692 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
693 "batman packet queue full\n");
694 return;
698 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
699 if (!forw_packet_aggr)
700 goto out_nomem;
702 if (atomic_read(&bat_priv->aggregated_ogms) &&
703 packet_len < BATADV_MAX_AGGREGATION_BYTES)
704 skb_size = BATADV_MAX_AGGREGATION_BYTES;
705 else
706 skb_size = packet_len;
708 skb_size += ETH_HLEN;
710 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
711 if (!forw_packet_aggr->skb)
712 goto out_free_forw_packet;
713 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
714 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
716 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
717 forw_packet_aggr->packet_len = packet_len;
718 memcpy(skb_buff, packet_buff, packet_len);
720 kref_get(&if_incoming->refcount);
721 kref_get(&if_outgoing->refcount);
722 forw_packet_aggr->own = own_packet;
723 forw_packet_aggr->if_incoming = if_incoming;
724 forw_packet_aggr->if_outgoing = if_outgoing;
725 forw_packet_aggr->num_packets = 0;
726 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
727 forw_packet_aggr->send_time = send_time;
729 /* save packet direct link flag status */
730 if (direct_link)
731 forw_packet_aggr->direct_link_flags |= 1;
733 /* add new packet to packet list */
734 spin_lock_bh(&bat_priv->forw_bat_list_lock);
735 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
736 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
738 /* start timer for this packet */
739 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
740 batadv_iv_send_outstanding_bat_ogm_packet);
741 queue_delayed_work(batadv_event_workqueue,
742 &forw_packet_aggr->delayed_work,
743 send_time - jiffies);
745 return;
746 out_free_forw_packet:
747 kfree(forw_packet_aggr);
748 out_nomem:
749 if (!own_packet)
750 atomic_inc(&bat_priv->batman_queue_left);
753 /* aggregate a new packet into the existing ogm packet */
754 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
755 const unsigned char *packet_buff,
756 int packet_len, bool direct_link)
758 unsigned char *skb_buff;
759 unsigned long new_direct_link_flag;
761 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
762 memcpy(skb_buff, packet_buff, packet_len);
763 forw_packet_aggr->packet_len += packet_len;
764 forw_packet_aggr->num_packets++;
766 /* save packet direct link flag status */
767 if (direct_link) {
768 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
769 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
774 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
775 * @bat_priv: the bat priv with all the soft interface information
776 * @packet_buff: pointer to the OGM
777 * @packet_len: (total) length of the OGM
778 * @if_incoming: interface where the packet was received
779 * @if_outgoing: interface for which the retransmission should be considered
780 * @own_packet: true if it is a self-generated ogm
781 * @send_time: timestamp (jiffies) when the packet is to be sent
783 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
784 unsigned char *packet_buff,
785 int packet_len,
786 struct batadv_hard_iface *if_incoming,
787 struct batadv_hard_iface *if_outgoing,
788 int own_packet, unsigned long send_time)
790 /* _aggr -> pointer to the packet we want to aggregate with
791 * _pos -> pointer to the position in the queue
793 struct batadv_forw_packet *forw_packet_aggr = NULL;
794 struct batadv_forw_packet *forw_packet_pos = NULL;
795 struct batadv_ogm_packet *batadv_ogm_packet;
796 bool direct_link;
797 unsigned long max_aggregation_jiffies;
799 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
800 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
801 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
803 /* find position for the packet in the forward queue */
804 spin_lock_bh(&bat_priv->forw_bat_list_lock);
805 /* own packets are not to be aggregated */
806 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
807 hlist_for_each_entry(forw_packet_pos,
808 &bat_priv->forw_bat_list, list) {
809 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
810 bat_priv, packet_len,
811 send_time, direct_link,
812 if_incoming,
813 if_outgoing,
814 forw_packet_pos)) {
815 forw_packet_aggr = forw_packet_pos;
816 break;
821 /* nothing to aggregate with - either aggregation disabled or no
822 * suitable aggregation packet found
824 if (!forw_packet_aggr) {
825 /* the following section can run without the lock */
826 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
828 /* if we could not aggregate this packet with one of the others
829 * we hold it back for a while, so that it might be aggregated
830 * later on
832 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
833 send_time += max_aggregation_jiffies;
835 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
836 send_time, direct_link,
837 if_incoming, if_outgoing,
838 own_packet);
839 } else {
840 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
841 packet_len, direct_link);
842 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
846 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
847 const struct ethhdr *ethhdr,
848 struct batadv_ogm_packet *batadv_ogm_packet,
849 bool is_single_hop_neigh,
850 bool is_from_best_next_hop,
851 struct batadv_hard_iface *if_incoming,
852 struct batadv_hard_iface *if_outgoing)
854 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
855 u16 tvlv_len;
857 if (batadv_ogm_packet->ttl <= 1) {
858 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
859 return;
862 if (!is_from_best_next_hop) {
863 /* Mark the forwarded packet when it is not coming from our
864 * best next hop. We still need to forward the packet for our
865 * neighbor link quality detection to work in case the packet
866 * originated from a single hop neighbor. Otherwise we can
867 * simply drop the ogm.
869 if (is_single_hop_neigh)
870 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
871 else
872 return;
875 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
877 batadv_ogm_packet->ttl--;
878 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
880 /* apply hop penalty */
881 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
882 bat_priv);
884 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
885 "Forwarding packet: tq: %i, ttl: %i\n",
886 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
888 if (is_single_hop_neigh)
889 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
890 else
891 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
893 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
894 BATADV_OGM_HLEN + tvlv_len,
895 if_incoming, if_outgoing, 0,
896 batadv_iv_ogm_fwd_send_time());
900 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
901 * the given interface
902 * @hard_iface: the interface for which the windows have to be shifted
904 static void
905 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
907 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
908 struct batadv_hashtable *hash = bat_priv->orig_hash;
909 struct hlist_head *head;
910 struct batadv_orig_node *orig_node;
911 unsigned long *word;
912 u32 i;
913 size_t word_index;
914 u8 *w;
915 int if_num;
917 for (i = 0; i < hash->size; i++) {
918 head = &hash->table[i];
920 rcu_read_lock();
921 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
922 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
923 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
924 word = &orig_node->bat_iv.bcast_own[word_index];
926 batadv_bit_get_packet(bat_priv, word, 1, 0);
927 if_num = hard_iface->if_num;
928 w = &orig_node->bat_iv.bcast_own_sum[if_num];
929 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
930 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
932 rcu_read_unlock();
936 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
938 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
939 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
940 struct batadv_ogm_packet *batadv_ogm_packet;
941 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
942 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
943 u32 seqno;
944 u16 tvlv_len = 0;
945 unsigned long send_time;
947 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
948 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
949 return;
951 /* the interface gets activated here to avoid race conditions between
952 * the moment of activating the interface in
953 * hardif_activate_interface() where the originator mac is set and
954 * outdated packets (especially uninitialized mac addresses) in the
955 * packet queue
957 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
958 hard_iface->if_status = BATADV_IF_ACTIVE;
960 primary_if = batadv_primary_if_get_selected(bat_priv);
962 if (hard_iface == primary_if) {
963 /* tt changes have to be committed before the tvlv data is
964 * appended as it may alter the tt tvlv container
966 batadv_tt_local_commit_changes(bat_priv);
967 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
968 ogm_buff_len,
969 BATADV_OGM_HLEN);
972 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
973 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
975 /* change sequence number to network order */
976 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
977 batadv_ogm_packet->seqno = htonl(seqno);
978 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
980 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
982 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
984 if (hard_iface != primary_if) {
985 /* OGMs from secondary interfaces are only scheduled on their
986 * respective interfaces.
988 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
989 hard_iface, hard_iface, 1, send_time);
990 goto out;
993 /* OGMs from primary interfaces are scheduled on all
994 * interfaces.
996 rcu_read_lock();
997 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
998 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
999 continue;
1001 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
1002 continue;
1004 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
1005 *ogm_buff_len, hard_iface,
1006 tmp_hard_iface, 1, send_time);
1008 batadv_hardif_put(tmp_hard_iface);
1010 rcu_read_unlock();
1012 out:
1013 if (primary_if)
1014 batadv_hardif_put(primary_if);
1018 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1019 * originator
1020 * @bat_priv: the bat priv with all the soft interface information
1021 * @orig_node: the orig node who originally emitted the ogm packet
1022 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
1023 * @ethhdr: Ethernet header of the OGM
1024 * @batadv_ogm_packet: the ogm packet
1025 * @if_incoming: interface where the packet was received
1026 * @if_outgoing: interface for which the retransmission should be considered
1027 * @dup_status: the duplicate status of this ogm packet.
1029 static void
1030 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1031 struct batadv_orig_node *orig_node,
1032 struct batadv_orig_ifinfo *orig_ifinfo,
1033 const struct ethhdr *ethhdr,
1034 const struct batadv_ogm_packet *batadv_ogm_packet,
1035 struct batadv_hard_iface *if_incoming,
1036 struct batadv_hard_iface *if_outgoing,
1037 enum batadv_dup_status dup_status)
1039 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1040 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1041 struct batadv_neigh_node *neigh_node = NULL;
1042 struct batadv_neigh_node *tmp_neigh_node = NULL;
1043 struct batadv_neigh_node *router = NULL;
1044 struct batadv_orig_node *orig_node_tmp;
1045 int if_num;
1046 u8 sum_orig, sum_neigh;
1047 u8 *neigh_addr;
1048 u8 tq_avg;
1050 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1051 "update_originator(): Searching and updating originator entry of received packet\n");
1053 rcu_read_lock();
1054 hlist_for_each_entry_rcu(tmp_neigh_node,
1055 &orig_node->neigh_list, list) {
1056 neigh_addr = tmp_neigh_node->addr;
1057 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1058 tmp_neigh_node->if_incoming == if_incoming &&
1059 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1060 if (WARN(neigh_node, "too many matching neigh_nodes"))
1061 batadv_neigh_node_put(neigh_node);
1062 neigh_node = tmp_neigh_node;
1063 continue;
1066 if (dup_status != BATADV_NO_DUP)
1067 continue;
1069 /* only update the entry for this outgoing interface */
1070 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1071 if_outgoing);
1072 if (!neigh_ifinfo)
1073 continue;
1075 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1076 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1077 &neigh_ifinfo->bat_iv.tq_index, 0);
1078 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1079 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1080 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1082 batadv_neigh_ifinfo_put(neigh_ifinfo);
1083 neigh_ifinfo = NULL;
1086 if (!neigh_node) {
1087 struct batadv_orig_node *orig_tmp;
1089 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1090 if (!orig_tmp)
1091 goto unlock;
1093 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1094 ethhdr->h_source,
1095 orig_node, orig_tmp);
1097 batadv_orig_node_put(orig_tmp);
1098 if (!neigh_node)
1099 goto unlock;
1100 } else {
1101 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1102 "Updating existing last-hop neighbor of originator\n");
1105 rcu_read_unlock();
1106 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1107 if (!neigh_ifinfo)
1108 goto out;
1110 neigh_node->last_seen = jiffies;
1112 spin_lock_bh(&neigh_node->ifinfo_lock);
1113 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1114 &neigh_ifinfo->bat_iv.tq_index,
1115 batadv_ogm_packet->tq);
1116 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1117 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1118 spin_unlock_bh(&neigh_node->ifinfo_lock);
1120 if (dup_status == BATADV_NO_DUP) {
1121 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1122 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1125 /* if this neighbor already is our next hop there is nothing
1126 * to change
1128 router = batadv_orig_router_get(orig_node, if_outgoing);
1129 if (router == neigh_node)
1130 goto out;
1132 if (router) {
1133 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1134 if (!router_ifinfo)
1135 goto out;
1137 /* if this neighbor does not offer a better TQ we won't
1138 * consider it
1140 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1141 goto out;
1144 /* if the TQ is the same and the link not more symmetric we
1145 * won't consider it either
1147 if (router_ifinfo &&
1148 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1149 orig_node_tmp = router->orig_node;
1150 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1151 if_num = router->if_incoming->if_num;
1152 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1153 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1155 orig_node_tmp = neigh_node->orig_node;
1156 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1157 if_num = neigh_node->if_incoming->if_num;
1158 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1159 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1161 if (sum_orig >= sum_neigh)
1162 goto out;
1165 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1166 goto out;
1168 unlock:
1169 rcu_read_unlock();
1170 out:
1171 if (neigh_node)
1172 batadv_neigh_node_put(neigh_node);
1173 if (router)
1174 batadv_neigh_node_put(router);
1175 if (neigh_ifinfo)
1176 batadv_neigh_ifinfo_put(neigh_ifinfo);
1177 if (router_ifinfo)
1178 batadv_neigh_ifinfo_put(router_ifinfo);
1182 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1183 * @orig_node: the orig node who originally emitted the ogm packet
1184 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1185 * @batadv_ogm_packet: the ogm packet
1186 * @if_incoming: interface where the packet was received
1187 * @if_outgoing: interface for which the retransmission should be considered
1189 * Return: true if the link can be considered bidirectional, false otherwise
1191 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1192 struct batadv_orig_node *orig_neigh_node,
1193 struct batadv_ogm_packet *batadv_ogm_packet,
1194 struct batadv_hard_iface *if_incoming,
1195 struct batadv_hard_iface *if_outgoing)
1197 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1198 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1199 struct batadv_neigh_ifinfo *neigh_ifinfo;
1200 u8 total_count;
1201 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1202 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1203 int if_num;
1204 unsigned int tq_asym_penalty, inv_asym_penalty;
1205 unsigned int combined_tq;
1206 unsigned int tq_iface_penalty;
1207 bool ret = false;
1209 /* find corresponding one hop neighbor */
1210 rcu_read_lock();
1211 hlist_for_each_entry_rcu(tmp_neigh_node,
1212 &orig_neigh_node->neigh_list, list) {
1213 if (!batadv_compare_eth(tmp_neigh_node->addr,
1214 orig_neigh_node->orig))
1215 continue;
1217 if (tmp_neigh_node->if_incoming != if_incoming)
1218 continue;
1220 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1221 continue;
1223 neigh_node = tmp_neigh_node;
1224 break;
1226 rcu_read_unlock();
1228 if (!neigh_node)
1229 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1230 orig_neigh_node->orig,
1231 orig_neigh_node,
1232 orig_neigh_node);
1234 if (!neigh_node)
1235 goto out;
1237 /* if orig_node is direct neighbor update neigh_node last_seen */
1238 if (orig_node == orig_neigh_node)
1239 neigh_node->last_seen = jiffies;
1241 orig_node->last_seen = jiffies;
1243 /* find packet count of corresponding one hop neighbor */
1244 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1245 if_num = if_incoming->if_num;
1246 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1247 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1248 if (neigh_ifinfo) {
1249 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1250 batadv_neigh_ifinfo_put(neigh_ifinfo);
1251 } else {
1252 neigh_rq_count = 0;
1254 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1256 /* pay attention to not get a value bigger than 100 % */
1257 if (orig_eq_count > neigh_rq_count)
1258 total_count = neigh_rq_count;
1259 else
1260 total_count = orig_eq_count;
1262 /* if we have too few packets (too less data) we set tq_own to zero
1263 * if we receive too few packets it is not considered bidirectional
1265 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1266 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1267 tq_own = 0;
1268 else
1269 /* neigh_node->real_packet_count is never zero as we
1270 * only purge old information when getting new
1271 * information
1273 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1275 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1276 * affect the nearly-symmetric links only a little, but
1277 * punishes asymmetric links more. This will give a value
1278 * between 0 and TQ_MAX_VALUE
1280 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1281 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1282 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1283 BATADV_TQ_LOCAL_WINDOW_SIZE *
1284 BATADV_TQ_LOCAL_WINDOW_SIZE;
1285 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1286 inv_asym_penalty /= neigh_rq_max_cube;
1287 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1289 /* penalize if the OGM is forwarded on the same interface. WiFi
1290 * interfaces and other half duplex devices suffer from throughput
1291 * drops as they can't send and receive at the same time.
1293 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1294 if (if_outgoing && (if_incoming == if_outgoing) &&
1295 batadv_is_wifi_netdev(if_outgoing->net_dev))
1296 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1297 bat_priv);
1299 combined_tq = batadv_ogm_packet->tq *
1300 tq_own *
1301 tq_asym_penalty *
1302 tq_iface_penalty;
1303 combined_tq /= BATADV_TQ_MAX_VALUE *
1304 BATADV_TQ_MAX_VALUE *
1305 BATADV_TQ_MAX_VALUE;
1306 batadv_ogm_packet->tq = combined_tq;
1308 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1309 "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",
1310 orig_node->orig, orig_neigh_node->orig, total_count,
1311 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1312 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1313 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1315 /* if link has the minimum required transmission quality
1316 * consider it bidirectional
1318 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1319 ret = true;
1321 out:
1322 if (neigh_node)
1323 batadv_neigh_node_put(neigh_node);
1324 return ret;
1328 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1329 * adjust the sequence number and find out whether it is a duplicate
1330 * @ethhdr: ethernet header of the packet
1331 * @batadv_ogm_packet: OGM packet to be considered
1332 * @if_incoming: interface on which the OGM packet was received
1333 * @if_outgoing: interface for which the retransmission should be considered
1335 * Return: duplicate status as enum batadv_dup_status
1337 static enum batadv_dup_status
1338 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1339 const struct batadv_ogm_packet *batadv_ogm_packet,
1340 const struct batadv_hard_iface *if_incoming,
1341 struct batadv_hard_iface *if_outgoing)
1343 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1344 struct batadv_orig_node *orig_node;
1345 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1346 struct batadv_neigh_node *neigh_node;
1347 struct batadv_neigh_ifinfo *neigh_ifinfo;
1348 bool is_dup;
1349 s32 seq_diff;
1350 bool need_update = false;
1351 int set_mark;
1352 enum batadv_dup_status ret = BATADV_NO_DUP;
1353 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1354 u8 *neigh_addr;
1355 u8 packet_count;
1356 unsigned long *bitmap;
1358 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1359 if (!orig_node)
1360 return BATADV_NO_DUP;
1362 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1363 if (WARN_ON(!orig_ifinfo)) {
1364 batadv_orig_node_put(orig_node);
1365 return 0;
1368 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1369 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1371 /* signalize caller that the packet is to be dropped. */
1372 if (!hlist_empty(&orig_node->neigh_list) &&
1373 batadv_window_protected(bat_priv, seq_diff,
1374 BATADV_TQ_LOCAL_WINDOW_SIZE,
1375 &orig_ifinfo->batman_seqno_reset, NULL)) {
1376 ret = BATADV_PROTECTED;
1377 goto out;
1380 rcu_read_lock();
1381 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1382 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1383 if_outgoing);
1384 if (!neigh_ifinfo)
1385 continue;
1387 neigh_addr = neigh_node->addr;
1388 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1389 orig_ifinfo->last_real_seqno,
1390 seqno);
1392 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1393 neigh_node->if_incoming == if_incoming) {
1394 set_mark = 1;
1395 if (is_dup)
1396 ret = BATADV_NEIGH_DUP;
1397 } else {
1398 set_mark = 0;
1399 if (is_dup && (ret != BATADV_NEIGH_DUP))
1400 ret = BATADV_ORIG_DUP;
1403 /* if the window moved, set the update flag. */
1404 bitmap = neigh_ifinfo->bat_iv.real_bits;
1405 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1406 seq_diff, set_mark);
1408 packet_count = bitmap_weight(bitmap,
1409 BATADV_TQ_LOCAL_WINDOW_SIZE);
1410 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1411 batadv_neigh_ifinfo_put(neigh_ifinfo);
1413 rcu_read_unlock();
1415 if (need_update) {
1416 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1417 "%s updating last_seqno: old %u, new %u\n",
1418 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1419 orig_ifinfo->last_real_seqno, seqno);
1420 orig_ifinfo->last_real_seqno = seqno;
1423 out:
1424 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1425 batadv_orig_node_put(orig_node);
1426 batadv_orig_ifinfo_put(orig_ifinfo);
1427 return ret;
1431 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1432 * @skb: the skb containing the OGM
1433 * @ogm_offset: offset from skb->data to start of ogm header
1434 * @orig_node: the (cached) orig node for the originator of this OGM
1435 * @if_incoming: the interface where this packet was received
1436 * @if_outgoing: the interface for which the packet should be considered
1438 static void
1439 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1440 struct batadv_orig_node *orig_node,
1441 struct batadv_hard_iface *if_incoming,
1442 struct batadv_hard_iface *if_outgoing)
1444 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1445 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1446 struct batadv_neigh_node *router = NULL;
1447 struct batadv_neigh_node *router_router = NULL;
1448 struct batadv_orig_node *orig_neigh_node;
1449 struct batadv_orig_ifinfo *orig_ifinfo;
1450 struct batadv_neigh_node *orig_neigh_router = NULL;
1451 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1452 struct batadv_ogm_packet *ogm_packet;
1453 enum batadv_dup_status dup_status;
1454 bool is_from_best_next_hop = false;
1455 bool is_single_hop_neigh = false;
1456 bool sameseq, similar_ttl;
1457 struct sk_buff *skb_priv;
1458 struct ethhdr *ethhdr;
1459 u8 *prev_sender;
1460 bool is_bidirect;
1462 /* create a private copy of the skb, as some functions change tq value
1463 * and/or flags.
1465 skb_priv = skb_copy(skb, GFP_ATOMIC);
1466 if (!skb_priv)
1467 return;
1469 ethhdr = eth_hdr(skb_priv);
1470 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1472 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1473 if_incoming, if_outgoing);
1474 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1475 is_single_hop_neigh = true;
1477 if (dup_status == BATADV_PROTECTED) {
1478 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1479 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1480 ethhdr->h_source);
1481 goto out;
1484 if (ogm_packet->tq == 0) {
1485 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1486 "Drop packet: originator packet with tq equal 0\n");
1487 goto out;
1490 if (is_single_hop_neigh) {
1491 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1492 ethhdr->h_source);
1493 if (hardif_neigh)
1494 hardif_neigh->last_seen = jiffies;
1497 router = batadv_orig_router_get(orig_node, if_outgoing);
1498 if (router) {
1499 router_router = batadv_orig_router_get(router->orig_node,
1500 if_outgoing);
1501 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1504 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1505 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1506 is_from_best_next_hop = true;
1508 prev_sender = ogm_packet->prev_sender;
1509 /* avoid temporary routing loops */
1510 if (router && router_router &&
1511 (batadv_compare_eth(router->addr, prev_sender)) &&
1512 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1513 (batadv_compare_eth(router->addr, router_router->addr))) {
1514 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1515 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1516 ethhdr->h_source);
1517 goto out;
1520 if (if_outgoing == BATADV_IF_DEFAULT)
1521 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1523 /* if sender is a direct neighbor the sender mac equals
1524 * originator mac
1526 if (is_single_hop_neigh)
1527 orig_neigh_node = orig_node;
1528 else
1529 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1530 ethhdr->h_source);
1532 if (!orig_neigh_node)
1533 goto out;
1535 /* Update nc_nodes of the originator */
1536 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1537 ogm_packet, is_single_hop_neigh);
1539 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1540 if_outgoing);
1542 /* drop packet if sender is not a direct neighbor and if we
1543 * don't route towards it
1545 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1546 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1547 "Drop packet: OGM via unknown neighbor!\n");
1548 goto out_neigh;
1551 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1552 ogm_packet, if_incoming,
1553 if_outgoing);
1555 /* update ranking if it is not a duplicate or has the same
1556 * seqno and similar ttl as the non-duplicate
1558 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1559 if (!orig_ifinfo)
1560 goto out_neigh;
1562 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1563 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1565 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1566 (sameseq && similar_ttl))) {
1567 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1568 orig_ifinfo, ethhdr,
1569 ogm_packet, if_incoming,
1570 if_outgoing, dup_status);
1572 batadv_orig_ifinfo_put(orig_ifinfo);
1574 /* only forward for specific interface, not for the default one. */
1575 if (if_outgoing == BATADV_IF_DEFAULT)
1576 goto out_neigh;
1578 /* is single hop (direct) neighbor */
1579 if (is_single_hop_neigh) {
1580 /* OGMs from secondary interfaces should only scheduled once
1581 * per interface where it has been received, not multiple times
1583 if ((ogm_packet->ttl <= 2) &&
1584 (if_incoming != if_outgoing)) {
1585 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1586 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1587 goto out_neigh;
1589 /* mark direct link on incoming interface */
1590 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1591 is_single_hop_neigh,
1592 is_from_best_next_hop, if_incoming,
1593 if_outgoing);
1595 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1596 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1597 goto out_neigh;
1600 /* multihop originator */
1601 if (!is_bidirect) {
1602 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1603 "Drop packet: not received via bidirectional link\n");
1604 goto out_neigh;
1607 if (dup_status == BATADV_NEIGH_DUP) {
1608 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1609 "Drop packet: duplicate packet received\n");
1610 goto out_neigh;
1613 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1614 "Forwarding packet: rebroadcast originator packet\n");
1615 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1616 is_single_hop_neigh, is_from_best_next_hop,
1617 if_incoming, if_outgoing);
1619 out_neigh:
1620 if ((orig_neigh_node) && (!is_single_hop_neigh))
1621 batadv_orig_node_put(orig_neigh_node);
1622 out:
1623 if (router_ifinfo)
1624 batadv_neigh_ifinfo_put(router_ifinfo);
1625 if (router)
1626 batadv_neigh_node_put(router);
1627 if (router_router)
1628 batadv_neigh_node_put(router_router);
1629 if (orig_neigh_router)
1630 batadv_neigh_node_put(orig_neigh_router);
1631 if (hardif_neigh)
1632 batadv_hardif_neigh_put(hardif_neigh);
1634 kfree_skb(skb_priv);
1638 * batadv_iv_ogm_process - process an incoming batman iv OGM
1639 * @skb: the skb containing the OGM
1640 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1641 * @if_incoming: the interface where this packet was receved
1643 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1644 struct batadv_hard_iface *if_incoming)
1646 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1647 struct batadv_orig_node *orig_neigh_node, *orig_node;
1648 struct batadv_hard_iface *hard_iface;
1649 struct batadv_ogm_packet *ogm_packet;
1650 u32 if_incoming_seqno;
1651 bool has_directlink_flag;
1652 struct ethhdr *ethhdr;
1653 bool is_my_oldorig = false;
1654 bool is_my_addr = false;
1655 bool is_my_orig = false;
1657 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1658 ethhdr = eth_hdr(skb);
1660 /* Silently drop when the batman packet is actually not a
1661 * correct packet.
1663 * This might happen if a packet is padded (e.g. Ethernet has a
1664 * minimum frame length of 64 byte) and the aggregation interprets
1665 * it as an additional length.
1667 * TODO: A more sane solution would be to have a bit in the
1668 * batadv_ogm_packet to detect whether the packet is the last
1669 * packet in an aggregation. Here we expect that the padding
1670 * is always zero (or not 0x01)
1672 if (ogm_packet->packet_type != BATADV_IV_OGM)
1673 return;
1675 /* could be changed by schedule_own_packet() */
1676 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1678 if (ogm_packet->flags & BATADV_DIRECTLINK)
1679 has_directlink_flag = true;
1680 else
1681 has_directlink_flag = false;
1683 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1684 "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",
1685 ethhdr->h_source, if_incoming->net_dev->name,
1686 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1687 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1688 ogm_packet->tq, ogm_packet->ttl,
1689 ogm_packet->version, has_directlink_flag);
1691 rcu_read_lock();
1692 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1693 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1694 continue;
1696 if (hard_iface->soft_iface != if_incoming->soft_iface)
1697 continue;
1699 if (batadv_compare_eth(ethhdr->h_source,
1700 hard_iface->net_dev->dev_addr))
1701 is_my_addr = true;
1703 if (batadv_compare_eth(ogm_packet->orig,
1704 hard_iface->net_dev->dev_addr))
1705 is_my_orig = true;
1707 if (batadv_compare_eth(ogm_packet->prev_sender,
1708 hard_iface->net_dev->dev_addr))
1709 is_my_oldorig = true;
1711 rcu_read_unlock();
1713 if (is_my_addr) {
1714 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1715 "Drop packet: received my own broadcast (sender: %pM)\n",
1716 ethhdr->h_source);
1717 return;
1720 if (is_my_orig) {
1721 unsigned long *word;
1722 int offset;
1723 s32 bit_pos;
1724 s16 if_num;
1725 u8 *weight;
1727 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1728 ethhdr->h_source);
1729 if (!orig_neigh_node)
1730 return;
1732 /* neighbor has to indicate direct link and it has to
1733 * come via the corresponding interface
1734 * save packet seqno for bidirectional check
1736 if (has_directlink_flag &&
1737 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1738 ogm_packet->orig)) {
1739 if_num = if_incoming->if_num;
1740 offset = if_num * BATADV_NUM_WORDS;
1742 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1743 word = &orig_neigh_node->bat_iv.bcast_own[offset];
1744 bit_pos = if_incoming_seqno - 2;
1745 bit_pos -= ntohl(ogm_packet->seqno);
1746 batadv_set_bit(word, bit_pos);
1747 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1748 *weight = bitmap_weight(word,
1749 BATADV_TQ_LOCAL_WINDOW_SIZE);
1750 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1753 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1754 "Drop packet: originator packet from myself (via neighbor)\n");
1755 batadv_orig_node_put(orig_neigh_node);
1756 return;
1759 if (is_my_oldorig) {
1760 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1761 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1762 ethhdr->h_source);
1763 return;
1766 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1767 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1768 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1769 ethhdr->h_source);
1770 return;
1773 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1774 if (!orig_node)
1775 return;
1777 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1778 if_incoming, BATADV_IF_DEFAULT);
1780 rcu_read_lock();
1781 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1782 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1783 continue;
1785 if (hard_iface->soft_iface != bat_priv->soft_iface)
1786 continue;
1788 if (!kref_get_unless_zero(&hard_iface->refcount))
1789 continue;
1791 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1792 if_incoming, hard_iface);
1794 batadv_hardif_put(hard_iface);
1796 rcu_read_unlock();
1798 batadv_orig_node_put(orig_node);
1801 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1803 struct delayed_work *delayed_work;
1804 struct batadv_forw_packet *forw_packet;
1805 struct batadv_priv *bat_priv;
1807 delayed_work = to_delayed_work(work);
1808 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1809 delayed_work);
1810 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1811 spin_lock_bh(&bat_priv->forw_bat_list_lock);
1812 hlist_del(&forw_packet->list);
1813 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
1815 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1816 goto out;
1818 batadv_iv_ogm_emit(forw_packet);
1820 /* we have to have at least one packet in the queue to determine the
1821 * queues wake up time unless we are shutting down.
1823 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1824 * primary interface should only be rescheduled once per period, but
1825 * this function will be called for the forw_packet instances of the
1826 * other secondary interfaces as well.
1828 if (forw_packet->own &&
1829 forw_packet->if_incoming == forw_packet->if_outgoing)
1830 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1832 out:
1833 /* don't count own packet */
1834 if (!forw_packet->own)
1835 atomic_inc(&bat_priv->batman_queue_left);
1837 batadv_forw_packet_free(forw_packet);
1840 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1841 struct batadv_hard_iface *if_incoming)
1843 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1844 struct batadv_ogm_packet *ogm_packet;
1845 u8 *packet_pos;
1846 int ogm_offset;
1847 bool ret;
1849 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1850 if (!ret)
1851 return NET_RX_DROP;
1853 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1854 * that does not have B.A.T.M.A.N. IV enabled ?
1856 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1857 return NET_RX_DROP;
1859 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1860 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1861 skb->len + ETH_HLEN);
1863 ogm_offset = 0;
1864 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1866 /* unpack the aggregated packets and process them one by one */
1867 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1868 ogm_packet->tvlv_len)) {
1869 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1871 ogm_offset += BATADV_OGM_HLEN;
1872 ogm_offset += ntohs(ogm_packet->tvlv_len);
1874 packet_pos = skb->data + ogm_offset;
1875 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1878 kfree_skb(skb);
1879 return NET_RX_SUCCESS;
1883 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1884 * @orig_node: the orig_node for which the neighbors are printed
1885 * @if_outgoing: outgoing interface for these entries
1886 * @seq: debugfs table seq_file struct
1888 * Must be called while holding an rcu lock.
1890 static void
1891 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1892 struct batadv_hard_iface *if_outgoing,
1893 struct seq_file *seq)
1895 struct batadv_neigh_node *neigh_node;
1896 struct batadv_neigh_ifinfo *n_ifinfo;
1898 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1899 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1900 if (!n_ifinfo)
1901 continue;
1903 seq_printf(seq, " %pM (%3i)",
1904 neigh_node->addr,
1905 n_ifinfo->bat_iv.tq_avg);
1907 batadv_neigh_ifinfo_put(n_ifinfo);
1912 * batadv_iv_ogm_orig_print - print the originator table
1913 * @bat_priv: the bat priv with all the soft interface information
1914 * @seq: debugfs table seq_file struct
1915 * @if_outgoing: the outgoing interface for which this should be printed
1917 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1918 struct seq_file *seq,
1919 struct batadv_hard_iface *if_outgoing)
1921 struct batadv_neigh_node *neigh_node;
1922 struct batadv_hashtable *hash = bat_priv->orig_hash;
1923 int last_seen_msecs, last_seen_secs;
1924 struct batadv_orig_node *orig_node;
1925 struct batadv_neigh_ifinfo *n_ifinfo;
1926 unsigned long last_seen_jiffies;
1927 struct hlist_head *head;
1928 int batman_count = 0;
1929 u32 i;
1931 seq_puts(seq,
1932 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1934 for (i = 0; i < hash->size; i++) {
1935 head = &hash->table[i];
1937 rcu_read_lock();
1938 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1939 neigh_node = batadv_orig_router_get(orig_node,
1940 if_outgoing);
1941 if (!neigh_node)
1942 continue;
1944 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1945 if_outgoing);
1946 if (!n_ifinfo)
1947 goto next;
1949 if (n_ifinfo->bat_iv.tq_avg == 0)
1950 goto next;
1952 last_seen_jiffies = jiffies - orig_node->last_seen;
1953 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1954 last_seen_secs = last_seen_msecs / 1000;
1955 last_seen_msecs = last_seen_msecs % 1000;
1957 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1958 orig_node->orig, last_seen_secs,
1959 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1960 neigh_node->addr,
1961 neigh_node->if_incoming->net_dev->name);
1963 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1964 seq);
1965 seq_puts(seq, "\n");
1966 batman_count++;
1968 next:
1969 batadv_neigh_node_put(neigh_node);
1970 if (n_ifinfo)
1971 batadv_neigh_ifinfo_put(n_ifinfo);
1973 rcu_read_unlock();
1976 if (batman_count == 0)
1977 seq_puts(seq, "No batman nodes in range ...\n");
1981 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1982 * @seq: neighbour table seq_file struct
1983 * @hardif_neigh: hardif neighbour information
1985 static void
1986 batadv_iv_hardif_neigh_print(struct seq_file *seq,
1987 struct batadv_hardif_neigh_node *hardif_neigh)
1989 int last_secs, last_msecs;
1991 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
1992 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
1994 seq_printf(seq, " %10s %pM %4i.%03is\n",
1995 hardif_neigh->if_incoming->net_dev->name,
1996 hardif_neigh->addr, last_secs, last_msecs);
2000 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
2001 * @bat_priv: the bat priv with all the soft interface information
2002 * @seq: neighbour table seq_file struct
2004 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2005 struct seq_file *seq)
2007 struct net_device *net_dev = (struct net_device *)seq->private;
2008 struct batadv_hardif_neigh_node *hardif_neigh;
2009 struct batadv_hard_iface *hard_iface;
2010 int batman_count = 0;
2012 seq_puts(seq, " IF Neighbor last-seen\n");
2014 rcu_read_lock();
2015 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2016 if (hard_iface->soft_iface != net_dev)
2017 continue;
2019 hlist_for_each_entry_rcu(hardif_neigh,
2020 &hard_iface->neigh_list, list) {
2021 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2022 batman_count++;
2025 rcu_read_unlock();
2027 if (batman_count == 0)
2028 seq_puts(seq, "No batman nodes in range ...\n");
2032 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2033 * @neigh1: the first neighbor object of the comparison
2034 * @if_outgoing1: outgoing interface for the first neighbor
2035 * @neigh2: the second neighbor object of the comparison
2036 * @if_outgoing2: outgoing interface for the second neighbor
2038 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2039 * lower, the same as or higher than the metric via neigh2
2041 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2042 struct batadv_hard_iface *if_outgoing1,
2043 struct batadv_neigh_node *neigh2,
2044 struct batadv_hard_iface *if_outgoing2)
2046 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2047 u8 tq1, tq2;
2048 int diff;
2050 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2051 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2053 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2054 diff = 0;
2055 goto out;
2058 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2059 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2060 diff = tq1 - tq2;
2062 out:
2063 if (neigh1_ifinfo)
2064 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2065 if (neigh2_ifinfo)
2066 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2068 return diff;
2072 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2073 * than neigh2 from the metric prospective
2074 * @neigh1: the first neighbor object of the comparison
2075 * @if_outgoing1: outgoing interface for the first neighbor
2076 * @neigh2: the second neighbor object of the comparison
2077 * @if_outgoing2: outgoing interface for the second neighbor
2079 * Return: true if the metric via neigh1 is equally good or better than
2080 * the metric via neigh2, false otherwise.
2082 static bool
2083 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2084 struct batadv_hard_iface *if_outgoing1,
2085 struct batadv_neigh_node *neigh2,
2086 struct batadv_hard_iface *if_outgoing2)
2088 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2089 u8 tq1, tq2;
2090 bool ret;
2092 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2093 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2095 /* we can't say that the metric is better */
2096 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2097 ret = false;
2098 goto out;
2101 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2102 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2103 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
2105 out:
2106 if (neigh1_ifinfo)
2107 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2108 if (neigh2_ifinfo)
2109 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2111 return ret;
2114 static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2116 /* begin scheduling originator messages on that interface */
2117 batadv_iv_ogm_schedule(hard_iface);
2120 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2121 .name = "BATMAN_IV",
2122 .iface = {
2123 .activate = batadv_iv_iface_activate,
2124 .enable = batadv_iv_ogm_iface_enable,
2125 .disable = batadv_iv_ogm_iface_disable,
2126 .update_mac = batadv_iv_ogm_iface_update_mac,
2127 .primary_set = batadv_iv_ogm_primary_iface_set,
2129 .neigh = {
2130 .cmp = batadv_iv_ogm_neigh_cmp,
2131 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2132 .print = batadv_iv_neigh_print,
2134 .orig = {
2135 .print = batadv_iv_ogm_orig_print,
2136 .free = batadv_iv_ogm_orig_free,
2137 .add_if = batadv_iv_ogm_orig_add_if,
2138 .del_if = batadv_iv_ogm_orig_del_if,
2142 int __init batadv_iv_init(void)
2144 int ret;
2146 /* batman originator packet */
2147 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2148 batadv_iv_ogm_receive);
2149 if (ret < 0)
2150 goto out;
2152 ret = batadv_algo_register(&batadv_batman_iv);
2153 if (ret < 0)
2154 goto handler_unregister;
2156 goto out;
2158 handler_unregister:
2159 batadv_recv_handler_unregister(BATADV_IV_OGM);
2160 out:
2161 return ret;