Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / net / batman-adv / bat_iv_ogm.c
bloba2b480a908723a37d47d14bd76e63628518e6561
1 /* Copyright (C) 2007-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
20 #include "main.h"
21 #include "translation-table.h"
22 #include "originator.h"
23 #include "routing.h"
24 #include "gateway_common.h"
25 #include "gateway_client.h"
26 #include "hard-interface.h"
27 #include "send.h"
28 #include "bat_algo.h"
29 #include "network-coding.h"
32 /**
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36 * neighbor)
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
40 enum batadv_dup_status {
41 BATADV_NO_DUP = 0,
42 BATADV_ORIG_DUP,
43 BATADV_NEIGH_DUP,
44 BATADV_PROTECTED,
47 /**
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
53 static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54 uint8_t value)
56 lq_recv[*lq_index] = value;
57 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
60 /**
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
65 * Returns computed average value.
67 static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
69 const uint8_t *ptr;
70 uint16_t count = 0, i = 0, sum = 0;
72 ptr = lq_recv;
74 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75 if (*ptr != 0) {
76 count++;
77 sum += *ptr;
80 i++;
81 ptr++;
84 if (count == 0)
85 return 0;
87 return (uint8_t)(sum / count);
90 /**
91 * batadv_iv_ogm_orig_free - free the private resources allocated for this
92 * orig_node
93 * @orig_node: the orig_node for which the resources have to be free'd
95 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
97 kfree(orig_node->bat_iv.bcast_own);
98 kfree(orig_node->bat_iv.bcast_own_sum);
102 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
103 * include the new hard-interface
104 * @orig_node: the orig_node that has to be changed
105 * @max_if_num: the current amount of interfaces
107 * Returns 0 on success, a negative error code otherwise.
109 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
110 int max_if_num)
112 void *data_ptr;
113 size_t data_size, old_size;
114 int ret = -ENOMEM;
116 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
118 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
119 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
120 data_ptr = kmalloc(data_size, GFP_ATOMIC);
121 if (!data_ptr)
122 goto unlock;
124 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
125 kfree(orig_node->bat_iv.bcast_own);
126 orig_node->bat_iv.bcast_own = data_ptr;
128 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
129 if (!data_ptr) {
130 kfree(orig_node->bat_iv.bcast_own);
131 goto unlock;
134 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
135 (max_if_num - 1) * sizeof(uint8_t));
136 kfree(orig_node->bat_iv.bcast_own_sum);
137 orig_node->bat_iv.bcast_own_sum = data_ptr;
139 ret = 0;
141 unlock:
142 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
144 return ret;
148 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
149 * exclude the removed interface
150 * @orig_node: the orig_node that has to be changed
151 * @max_if_num: the current amount of interfaces
152 * @del_if_num: the index of the interface being removed
154 * Returns 0 on success, a negative error code otherwise.
156 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
157 int max_if_num, int del_if_num)
159 int chunk_size, ret = -ENOMEM, if_offset;
160 void *data_ptr = NULL;
162 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
164 /* last interface was removed */
165 if (max_if_num == 0)
166 goto free_bcast_own;
168 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
169 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
170 if (!data_ptr)
171 goto unlock;
173 /* copy first part */
174 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
176 /* copy second part */
177 memcpy((char *)data_ptr + del_if_num * chunk_size,
178 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
179 (max_if_num - del_if_num) * chunk_size);
181 free_bcast_own:
182 kfree(orig_node->bat_iv.bcast_own);
183 orig_node->bat_iv.bcast_own = data_ptr;
185 if (max_if_num == 0)
186 goto free_own_sum;
188 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
189 if (!data_ptr) {
190 kfree(orig_node->bat_iv.bcast_own);
191 goto unlock;
194 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
195 del_if_num * sizeof(uint8_t));
197 if_offset = (del_if_num + 1) * sizeof(uint8_t);
198 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
199 orig_node->bat_iv.bcast_own_sum + if_offset,
200 (max_if_num - del_if_num) * sizeof(uint8_t));
202 free_own_sum:
203 kfree(orig_node->bat_iv.bcast_own_sum);
204 orig_node->bat_iv.bcast_own_sum = data_ptr;
206 ret = 0;
207 unlock:
208 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
210 return ret;
214 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
215 * @bat_priv: the bat priv with all the soft interface information
216 * @addr: mac address of the originator
218 * Returns the originator object corresponding to the passed mac address or NULL
219 * on failure.
220 * If the object does not exists it is created an initialised.
222 static struct batadv_orig_node *
223 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
225 struct batadv_orig_node *orig_node;
226 int size, hash_added;
228 orig_node = batadv_orig_hash_find(bat_priv, addr);
229 if (orig_node)
230 return orig_node;
232 orig_node = batadv_orig_node_new(bat_priv, addr);
233 if (!orig_node)
234 return NULL;
236 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
238 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
239 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
240 if (!orig_node->bat_iv.bcast_own)
241 goto free_orig_node;
243 size = bat_priv->num_ifaces * sizeof(uint8_t);
244 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
245 if (!orig_node->bat_iv.bcast_own_sum)
246 goto free_bcast_own;
248 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
249 batadv_choose_orig, orig_node,
250 &orig_node->hash_entry);
251 if (hash_added != 0)
252 goto free_bcast_own;
254 return orig_node;
256 free_bcast_own:
257 kfree(orig_node->bat_iv.bcast_own);
258 free_orig_node:
259 batadv_orig_node_free_ref(orig_node);
261 return NULL;
264 static struct batadv_neigh_node *
265 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
266 const uint8_t *neigh_addr,
267 struct batadv_orig_node *orig_node,
268 struct batadv_orig_node *orig_neigh)
270 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
271 struct batadv_neigh_node *neigh_node;
273 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
274 if (!neigh_node)
275 goto out;
277 spin_lock_init(&neigh_node->bat_iv.lq_update_lock);
279 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
280 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
281 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
283 spin_lock_bh(&orig_node->neigh_list_lock);
284 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
285 spin_unlock_bh(&orig_node->neigh_list_lock);
287 out:
288 return neigh_node;
291 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
293 struct batadv_ogm_packet *batadv_ogm_packet;
294 unsigned char *ogm_buff;
295 uint32_t random_seqno;
296 int res = -ENOMEM;
298 /* randomize initial seqno to avoid collision */
299 get_random_bytes(&random_seqno, sizeof(random_seqno));
300 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
302 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
303 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
304 if (!ogm_buff)
305 goto out;
307 hard_iface->bat_iv.ogm_buff = ogm_buff;
309 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
310 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
311 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
312 batadv_ogm_packet->header.ttl = 2;
313 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
314 batadv_ogm_packet->reserved = 0;
315 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
317 res = 0;
319 out:
320 return res;
323 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
325 kfree(hard_iface->bat_iv.ogm_buff);
326 hard_iface->bat_iv.ogm_buff = NULL;
329 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
331 struct batadv_ogm_packet *batadv_ogm_packet;
332 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
334 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
335 memcpy(batadv_ogm_packet->orig,
336 hard_iface->net_dev->dev_addr, ETH_ALEN);
337 memcpy(batadv_ogm_packet->prev_sender,
338 hard_iface->net_dev->dev_addr, ETH_ALEN);
341 static void
342 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
344 struct batadv_ogm_packet *batadv_ogm_packet;
345 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
347 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
348 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
349 batadv_ogm_packet->header.ttl = BATADV_TTL;
352 /* when do we schedule our own ogm to be sent */
353 static unsigned long
354 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
356 unsigned int msecs;
358 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
359 msecs += prandom_u32() % (2 * BATADV_JITTER);
361 return jiffies + msecs_to_jiffies(msecs);
364 /* when do we schedule a ogm packet to be sent */
365 static unsigned long batadv_iv_ogm_fwd_send_time(void)
367 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
370 /* apply hop penalty for a normal link */
371 static uint8_t batadv_hop_penalty(uint8_t tq,
372 const struct batadv_priv *bat_priv)
374 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
375 int new_tq;
377 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
378 new_tq /= BATADV_TQ_MAX_VALUE;
380 return new_tq;
383 /* is there another aggregated packet here? */
384 static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
385 __be16 tvlv_len)
387 int next_buff_pos = 0;
389 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
390 next_buff_pos += ntohs(tvlv_len);
392 return (next_buff_pos <= packet_len) &&
393 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
396 /* send a batman ogm to a given interface */
397 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
398 struct batadv_hard_iface *hard_iface)
400 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
401 char *fwd_str;
402 uint8_t packet_num;
403 int16_t buff_pos;
404 struct batadv_ogm_packet *batadv_ogm_packet;
405 struct sk_buff *skb;
406 uint8_t *packet_pos;
408 if (hard_iface->if_status != BATADV_IF_ACTIVE)
409 return;
411 packet_num = 0;
412 buff_pos = 0;
413 packet_pos = forw_packet->skb->data;
414 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
416 /* adjust all flags and log packets */
417 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
418 batadv_ogm_packet->tvlv_len)) {
419 /* we might have aggregated direct link packets with an
420 * ordinary base packet
422 if (forw_packet->direct_link_flags & BIT(packet_num) &&
423 forw_packet->if_incoming == hard_iface)
424 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
425 else
426 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
428 if (packet_num > 0 || !forw_packet->own)
429 fwd_str = "Forwarding";
430 else
431 fwd_str = "Sending own";
433 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
434 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
435 fwd_str, (packet_num > 0 ? "aggregated " : ""),
436 batadv_ogm_packet->orig,
437 ntohl(batadv_ogm_packet->seqno),
438 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
439 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
440 "on" : "off"),
441 hard_iface->net_dev->name,
442 hard_iface->net_dev->dev_addr);
444 buff_pos += BATADV_OGM_HLEN;
445 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
446 packet_num++;
447 packet_pos = forw_packet->skb->data + buff_pos;
448 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
451 /* create clone because function is called more than once */
452 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
453 if (skb) {
454 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
455 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
456 skb->len + ETH_HLEN);
457 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
461 /* send a batman ogm packet */
462 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
464 struct batadv_hard_iface *hard_iface;
465 struct net_device *soft_iface;
466 struct batadv_priv *bat_priv;
467 struct batadv_hard_iface *primary_if = NULL;
468 struct batadv_ogm_packet *batadv_ogm_packet;
469 unsigned char directlink;
470 uint8_t *packet_pos;
472 packet_pos = forw_packet->skb->data;
473 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
474 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
476 if (!forw_packet->if_incoming) {
477 pr_err("Error - can't forward packet: incoming iface not specified\n");
478 goto out;
481 soft_iface = forw_packet->if_incoming->soft_iface;
482 bat_priv = netdev_priv(soft_iface);
484 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
485 goto out;
487 primary_if = batadv_primary_if_get_selected(bat_priv);
488 if (!primary_if)
489 goto out;
491 /* multihomed peer assumed
492 * non-primary OGMs are only broadcasted on their interface
494 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
495 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
496 /* FIXME: what about aggregated packets ? */
497 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
498 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
499 (forw_packet->own ? "Sending own" : "Forwarding"),
500 batadv_ogm_packet->orig,
501 ntohl(batadv_ogm_packet->seqno),
502 batadv_ogm_packet->header.ttl,
503 forw_packet->if_incoming->net_dev->name,
504 forw_packet->if_incoming->net_dev->dev_addr);
506 /* skb is only used once and than forw_packet is free'd */
507 batadv_send_skb_packet(forw_packet->skb,
508 forw_packet->if_incoming,
509 batadv_broadcast_addr);
510 forw_packet->skb = NULL;
512 goto out;
515 /* broadcast on every interface */
516 rcu_read_lock();
517 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
518 if (hard_iface->soft_iface != soft_iface)
519 continue;
521 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
523 rcu_read_unlock();
525 out:
526 if (primary_if)
527 batadv_hardif_free_ref(primary_if);
530 /* return true if new_packet can be aggregated with forw_packet */
531 static bool
532 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
533 struct batadv_priv *bat_priv,
534 int packet_len, unsigned long send_time,
535 bool directlink,
536 const struct batadv_hard_iface *if_incoming,
537 const struct batadv_forw_packet *forw_packet)
539 struct batadv_ogm_packet *batadv_ogm_packet;
540 int aggregated_bytes = forw_packet->packet_len + packet_len;
541 struct batadv_hard_iface *primary_if = NULL;
542 bool res = false;
543 unsigned long aggregation_end_time;
545 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
546 aggregation_end_time = send_time;
547 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
549 /* we can aggregate the current packet to this aggregated packet
550 * if:
552 * - the send time is within our MAX_AGGREGATION_MS time
553 * - the resulting packet wont be bigger than
554 * MAX_AGGREGATION_BYTES
556 if (time_before(send_time, forw_packet->send_time) &&
557 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
558 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
559 /* check aggregation compatibility
560 * -> direct link packets are broadcasted on
561 * their interface only
562 * -> aggregate packet if the current packet is
563 * a "global" packet as well as the base
564 * packet
566 primary_if = batadv_primary_if_get_selected(bat_priv);
567 if (!primary_if)
568 goto out;
570 /* packets without direct link flag and high TTL
571 * are flooded through the net
573 if ((!directlink) &&
574 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
575 (batadv_ogm_packet->header.ttl != 1) &&
577 /* own packets originating non-primary
578 * interfaces leave only that interface
580 ((!forw_packet->own) ||
581 (forw_packet->if_incoming == primary_if))) {
582 res = true;
583 goto out;
586 /* if the incoming packet is sent via this one
587 * interface only - we still can aggregate
589 if ((directlink) &&
590 (new_bat_ogm_packet->header.ttl == 1) &&
591 (forw_packet->if_incoming == if_incoming) &&
593 /* packets from direct neighbors or
594 * own secondary interface packets
595 * (= secondary interface packets in general)
597 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
598 (forw_packet->own &&
599 forw_packet->if_incoming != primary_if))) {
600 res = true;
601 goto out;
605 out:
606 if (primary_if)
607 batadv_hardif_free_ref(primary_if);
608 return res;
611 /* create a new aggregated packet and add this packet to it */
612 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
613 int packet_len, unsigned long send_time,
614 bool direct_link,
615 struct batadv_hard_iface *if_incoming,
616 int own_packet)
618 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
619 struct batadv_forw_packet *forw_packet_aggr;
620 unsigned char *skb_buff;
621 unsigned int skb_size;
623 if (!atomic_inc_not_zero(&if_incoming->refcount))
624 return;
626 /* own packet should always be scheduled */
627 if (!own_packet) {
628 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
629 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
630 "batman packet queue full\n");
631 goto out;
635 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
636 if (!forw_packet_aggr) {
637 if (!own_packet)
638 atomic_inc(&bat_priv->batman_queue_left);
639 goto out;
642 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
643 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
644 skb_size = BATADV_MAX_AGGREGATION_BYTES;
645 else
646 skb_size = packet_len;
648 skb_size += ETH_HLEN;
650 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
651 if (!forw_packet_aggr->skb) {
652 if (!own_packet)
653 atomic_inc(&bat_priv->batman_queue_left);
654 kfree(forw_packet_aggr);
655 goto out;
657 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
658 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
660 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
661 forw_packet_aggr->packet_len = packet_len;
662 memcpy(skb_buff, packet_buff, packet_len);
664 forw_packet_aggr->own = own_packet;
665 forw_packet_aggr->if_incoming = if_incoming;
666 forw_packet_aggr->num_packets = 0;
667 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
668 forw_packet_aggr->send_time = send_time;
670 /* save packet direct link flag status */
671 if (direct_link)
672 forw_packet_aggr->direct_link_flags |= 1;
674 /* add new packet to packet list */
675 spin_lock_bh(&bat_priv->forw_bat_list_lock);
676 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
677 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
679 /* start timer for this packet */
680 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
681 batadv_send_outstanding_bat_ogm_packet);
682 queue_delayed_work(batadv_event_workqueue,
683 &forw_packet_aggr->delayed_work,
684 send_time - jiffies);
686 return;
687 out:
688 batadv_hardif_free_ref(if_incoming);
691 /* aggregate a new packet into the existing ogm packet */
692 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
693 const unsigned char *packet_buff,
694 int packet_len, bool direct_link)
696 unsigned char *skb_buff;
697 unsigned long new_direct_link_flag;
699 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
700 memcpy(skb_buff, packet_buff, packet_len);
701 forw_packet_aggr->packet_len += packet_len;
702 forw_packet_aggr->num_packets++;
704 /* save packet direct link flag status */
705 if (direct_link) {
706 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
707 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
711 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
712 unsigned char *packet_buff,
713 int packet_len,
714 struct batadv_hard_iface *if_incoming,
715 int own_packet, unsigned long send_time)
717 /* _aggr -> pointer to the packet we want to aggregate with
718 * _pos -> pointer to the position in the queue
720 struct batadv_forw_packet *forw_packet_aggr = NULL;
721 struct batadv_forw_packet *forw_packet_pos = NULL;
722 struct batadv_ogm_packet *batadv_ogm_packet;
723 bool direct_link;
724 unsigned long max_aggregation_jiffies;
726 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
727 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
728 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
730 /* find position for the packet in the forward queue */
731 spin_lock_bh(&bat_priv->forw_bat_list_lock);
732 /* own packets are not to be aggregated */
733 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
734 hlist_for_each_entry(forw_packet_pos,
735 &bat_priv->forw_bat_list, list) {
736 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
737 bat_priv, packet_len,
738 send_time, direct_link,
739 if_incoming,
740 forw_packet_pos)) {
741 forw_packet_aggr = forw_packet_pos;
742 break;
747 /* nothing to aggregate with - either aggregation disabled or no
748 * suitable aggregation packet found
750 if (!forw_packet_aggr) {
751 /* the following section can run without the lock */
752 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
754 /* if we could not aggregate this packet with one of the others
755 * we hold it back for a while, so that it might be aggregated
756 * later on
758 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
759 send_time += max_aggregation_jiffies;
761 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
762 send_time, direct_link,
763 if_incoming, own_packet);
764 } else {
765 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
766 packet_len, direct_link);
767 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
771 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
772 const struct ethhdr *ethhdr,
773 struct batadv_ogm_packet *batadv_ogm_packet,
774 bool is_single_hop_neigh,
775 bool is_from_best_next_hop,
776 struct batadv_hard_iface *if_incoming)
778 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
779 uint16_t tvlv_len;
781 if (batadv_ogm_packet->header.ttl <= 1) {
782 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
783 return;
786 if (!is_from_best_next_hop) {
787 /* Mark the forwarded packet when it is not coming from our
788 * best next hop. We still need to forward the packet for our
789 * neighbor link quality detection to work in case the packet
790 * originated from a single hop neighbor. Otherwise we can
791 * simply drop the ogm.
793 if (is_single_hop_neigh)
794 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
795 else
796 return;
799 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
801 batadv_ogm_packet->header.ttl--;
802 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
804 /* apply hop penalty */
805 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
806 bat_priv);
808 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
809 "Forwarding packet: tq: %i, ttl: %i\n",
810 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
812 /* switch of primaries first hop flag when forwarding */
813 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
814 if (is_single_hop_neigh)
815 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
816 else
817 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
819 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
820 BATADV_OGM_HLEN + tvlv_len,
821 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
825 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
826 * the given interface
827 * @hard_iface: the interface for which the windows have to be shifted
829 static void
830 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
832 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
833 struct batadv_hashtable *hash = bat_priv->orig_hash;
834 struct hlist_head *head;
835 struct batadv_orig_node *orig_node;
836 unsigned long *word;
837 uint32_t i;
838 size_t word_index;
839 uint8_t *w;
840 int if_num;
842 for (i = 0; i < hash->size; i++) {
843 head = &hash->table[i];
845 rcu_read_lock();
846 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
847 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
848 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
849 word = &(orig_node->bat_iv.bcast_own[word_index]);
851 batadv_bit_get_packet(bat_priv, word, 1, 0);
852 if_num = hard_iface->if_num;
853 w = &orig_node->bat_iv.bcast_own_sum[if_num];
854 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
855 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
857 rcu_read_unlock();
861 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
863 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
864 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
865 struct batadv_ogm_packet *batadv_ogm_packet;
866 struct batadv_hard_iface *primary_if;
867 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
868 uint32_t seqno;
869 uint16_t tvlv_len = 0;
871 primary_if = batadv_primary_if_get_selected(bat_priv);
873 if (hard_iface == primary_if) {
874 /* tt changes have to be committed before the tvlv data is
875 * appended as it may alter the tt tvlv container
877 batadv_tt_local_commit_changes(bat_priv);
878 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
879 ogm_buff_len,
880 BATADV_OGM_HLEN);
883 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
884 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
886 /* change sequence number to network order */
887 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
888 batadv_ogm_packet->seqno = htonl(seqno);
889 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
891 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
892 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
893 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
894 batadv_iv_ogm_emit_send_time(bat_priv));
896 if (primary_if)
897 batadv_hardif_free_ref(primary_if);
900 static void
901 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
902 struct batadv_orig_node *orig_node,
903 const struct ethhdr *ethhdr,
904 const struct batadv_ogm_packet *batadv_ogm_packet,
905 struct batadv_hard_iface *if_incoming,
906 const unsigned char *tt_buff,
907 enum batadv_dup_status dup_status)
909 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
910 struct batadv_neigh_node *router = NULL;
911 struct batadv_orig_node *orig_node_tmp;
912 int if_num;
913 uint8_t sum_orig, sum_neigh;
914 uint8_t *neigh_addr;
915 uint8_t tq_avg;
917 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
918 "update_originator(): Searching and updating originator entry of received packet\n");
920 rcu_read_lock();
921 hlist_for_each_entry_rcu(tmp_neigh_node,
922 &orig_node->neigh_list, list) {
923 neigh_addr = tmp_neigh_node->addr;
924 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
925 tmp_neigh_node->if_incoming == if_incoming &&
926 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
927 if (WARN(neigh_node, "too many matching neigh_nodes"))
928 batadv_neigh_node_free_ref(neigh_node);
929 neigh_node = tmp_neigh_node;
930 continue;
933 if (dup_status != BATADV_NO_DUP)
934 continue;
936 spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
937 batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv,
938 &tmp_neigh_node->bat_iv.tq_index, 0);
939 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv);
940 tmp_neigh_node->bat_iv.tq_avg = tq_avg;
941 spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
944 if (!neigh_node) {
945 struct batadv_orig_node *orig_tmp;
947 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
948 if (!orig_tmp)
949 goto unlock;
951 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
952 ethhdr->h_source,
953 orig_node, orig_tmp);
955 batadv_orig_node_free_ref(orig_tmp);
956 if (!neigh_node)
957 goto unlock;
958 } else
959 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
960 "Updating existing last-hop neighbor of originator\n");
962 rcu_read_unlock();
964 neigh_node->last_seen = jiffies;
966 spin_lock_bh(&neigh_node->bat_iv.lq_update_lock);
967 batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv,
968 &neigh_node->bat_iv.tq_index,
969 batadv_ogm_packet->tq);
970 tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv);
971 neigh_node->bat_iv.tq_avg = tq_avg;
972 spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock);
974 if (dup_status == BATADV_NO_DUP) {
975 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
976 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
979 batadv_bonding_candidate_add(bat_priv, orig_node, neigh_node);
981 /* if this neighbor already is our next hop there is nothing
982 * to change
984 router = batadv_orig_node_get_router(orig_node);
985 if (router == neigh_node)
986 goto out;
988 /* if this neighbor does not offer a better TQ we won't consider it */
989 if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg))
990 goto out;
992 /* if the TQ is the same and the link not more symmetric we
993 * won't consider it either
995 if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) {
996 orig_node_tmp = router->orig_node;
997 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
998 if_num = router->if_incoming->if_num;
999 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1000 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1002 orig_node_tmp = neigh_node->orig_node;
1003 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1004 if_num = neigh_node->if_incoming->if_num;
1005 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1006 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1008 if (sum_orig >= sum_neigh)
1009 goto out;
1012 batadv_update_route(bat_priv, orig_node, neigh_node);
1013 goto out;
1015 unlock:
1016 rcu_read_unlock();
1017 out:
1018 if (neigh_node)
1019 batadv_neigh_node_free_ref(neigh_node);
1020 if (router)
1021 batadv_neigh_node_free_ref(router);
1024 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1025 struct batadv_orig_node *orig_neigh_node,
1026 struct batadv_ogm_packet *batadv_ogm_packet,
1027 struct batadv_hard_iface *if_incoming)
1029 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1030 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1031 uint8_t total_count;
1032 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1033 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1034 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
1035 unsigned int combined_tq;
1037 /* find corresponding one hop neighbor */
1038 rcu_read_lock();
1039 hlist_for_each_entry_rcu(tmp_neigh_node,
1040 &orig_neigh_node->neigh_list, list) {
1041 if (!batadv_compare_eth(tmp_neigh_node->addr,
1042 orig_neigh_node->orig))
1043 continue;
1045 if (tmp_neigh_node->if_incoming != if_incoming)
1046 continue;
1048 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1049 continue;
1051 neigh_node = tmp_neigh_node;
1052 break;
1054 rcu_read_unlock();
1056 if (!neigh_node)
1057 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1058 orig_neigh_node->orig,
1059 orig_neigh_node,
1060 orig_neigh_node);
1062 if (!neigh_node)
1063 goto out;
1065 /* if orig_node is direct neighbor update neigh_node last_seen */
1066 if (orig_node == orig_neigh_node)
1067 neigh_node->last_seen = jiffies;
1069 orig_node->last_seen = jiffies;
1071 /* find packet count of corresponding one hop neighbor */
1072 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1073 if_num = if_incoming->if_num;
1074 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1075 neigh_rq_count = neigh_node->bat_iv.real_packet_count;
1076 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1078 /* pay attention to not get a value bigger than 100 % */
1079 if (orig_eq_count > neigh_rq_count)
1080 total_count = neigh_rq_count;
1081 else
1082 total_count = orig_eq_count;
1084 /* if we have too few packets (too less data) we set tq_own to zero
1085 * if we receive too few packets it is not considered bidirectional
1087 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1088 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1089 tq_own = 0;
1090 else
1091 /* neigh_node->real_packet_count is never zero as we
1092 * only purge old information when getting new
1093 * information
1095 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1097 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1098 * affect the nearly-symmetric links only a little, but
1099 * punishes asymmetric links more. This will give a value
1100 * between 0 and TQ_MAX_VALUE
1102 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1103 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1104 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1105 BATADV_TQ_LOCAL_WINDOW_SIZE *
1106 BATADV_TQ_LOCAL_WINDOW_SIZE;
1107 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1108 inv_asym_penalty /= neigh_rq_max_cube;
1109 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1111 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
1112 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
1113 batadv_ogm_packet->tq = combined_tq;
1115 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1116 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
1117 orig_node->orig, orig_neigh_node->orig, total_count,
1118 neigh_rq_count, tq_own,
1119 tq_asym_penalty, batadv_ogm_packet->tq);
1121 /* if link has the minimum required transmission quality
1122 * consider it bidirectional
1124 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1125 ret = 1;
1127 out:
1128 if (neigh_node)
1129 batadv_neigh_node_free_ref(neigh_node);
1130 return ret;
1134 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1135 * adjust the sequence number and find out whether it is a duplicate
1136 * @ethhdr: ethernet header of the packet
1137 * @batadv_ogm_packet: OGM packet to be considered
1138 * @if_incoming: interface on which the OGM packet was received
1140 * Returns duplicate status as enum batadv_dup_status
1142 static enum batadv_dup_status
1143 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1144 const struct batadv_ogm_packet *batadv_ogm_packet,
1145 const struct batadv_hard_iface *if_incoming)
1147 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1148 struct batadv_orig_node *orig_node;
1149 struct batadv_neigh_node *tmp_neigh_node;
1150 int is_dup;
1151 int32_t seq_diff;
1152 int need_update = 0;
1153 int set_mark;
1154 enum batadv_dup_status ret = BATADV_NO_DUP;
1155 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
1156 uint8_t *neigh_addr;
1157 uint8_t packet_count;
1158 unsigned long *bitmap;
1160 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1161 if (!orig_node)
1162 return BATADV_NO_DUP;
1164 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1165 seq_diff = seqno - orig_node->last_real_seqno;
1167 /* signalize caller that the packet is to be dropped. */
1168 if (!hlist_empty(&orig_node->neigh_list) &&
1169 batadv_window_protected(bat_priv, seq_diff,
1170 &orig_node->batman_seqno_reset)) {
1171 ret = BATADV_PROTECTED;
1172 goto out;
1175 rcu_read_lock();
1176 hlist_for_each_entry_rcu(tmp_neigh_node,
1177 &orig_node->neigh_list, list) {
1178 neigh_addr = tmp_neigh_node->addr;
1179 is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits,
1180 orig_node->last_real_seqno,
1181 seqno);
1183 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1184 tmp_neigh_node->if_incoming == if_incoming) {
1185 set_mark = 1;
1186 if (is_dup)
1187 ret = BATADV_NEIGH_DUP;
1188 } else {
1189 set_mark = 0;
1190 if (is_dup && (ret != BATADV_NEIGH_DUP))
1191 ret = BATADV_ORIG_DUP;
1194 /* if the window moved, set the update flag. */
1195 bitmap = tmp_neigh_node->bat_iv.real_bits;
1196 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1197 seq_diff, set_mark);
1199 packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits,
1200 BATADV_TQ_LOCAL_WINDOW_SIZE);
1201 tmp_neigh_node->bat_iv.real_packet_count = packet_count;
1203 rcu_read_unlock();
1205 if (need_update) {
1206 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1207 "updating last_seqno: old %u, new %u\n",
1208 orig_node->last_real_seqno, seqno);
1209 orig_node->last_real_seqno = seqno;
1212 out:
1213 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1214 batadv_orig_node_free_ref(orig_node);
1215 return ret;
1218 static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1219 struct batadv_ogm_packet *batadv_ogm_packet,
1220 const unsigned char *tt_buff,
1221 struct batadv_hard_iface *if_incoming)
1223 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1224 struct batadv_hard_iface *hard_iface;
1225 struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp;
1226 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1227 struct batadv_neigh_node *orig_neigh_router = NULL;
1228 int has_directlink_flag;
1229 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
1230 int is_bidirect;
1231 bool is_single_hop_neigh = false;
1232 bool is_from_best_next_hop = false;
1233 int sameseq, similar_ttl;
1234 enum batadv_dup_status dup_status;
1235 uint32_t if_incoming_seqno;
1236 uint8_t *prev_sender;
1238 /* Silently drop when the batman packet is actually not a
1239 * correct packet.
1241 * This might happen if a packet is padded (e.g. Ethernet has a
1242 * minimum frame length of 64 byte) and the aggregation interprets
1243 * it as an additional length.
1245 * TODO: A more sane solution would be to have a bit in the
1246 * batadv_ogm_packet to detect whether the packet is the last
1247 * packet in an aggregation. Here we expect that the padding
1248 * is always zero (or not 0x01)
1250 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
1251 return;
1253 /* could be changed by schedule_own_packet() */
1254 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1256 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
1257 has_directlink_flag = 1;
1258 else
1259 has_directlink_flag = 0;
1261 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
1262 is_single_hop_neigh = true;
1264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1265 "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",
1266 ethhdr->h_source, if_incoming->net_dev->name,
1267 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1268 batadv_ogm_packet->prev_sender,
1269 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
1270 batadv_ogm_packet->header.ttl,
1271 batadv_ogm_packet->header.version, has_directlink_flag);
1273 rcu_read_lock();
1274 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1275 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1276 continue;
1278 if (hard_iface->soft_iface != if_incoming->soft_iface)
1279 continue;
1281 if (batadv_compare_eth(ethhdr->h_source,
1282 hard_iface->net_dev->dev_addr))
1283 is_my_addr = 1;
1285 if (batadv_compare_eth(batadv_ogm_packet->orig,
1286 hard_iface->net_dev->dev_addr))
1287 is_my_orig = 1;
1289 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
1290 hard_iface->net_dev->dev_addr))
1291 is_my_oldorig = 1;
1293 rcu_read_unlock();
1295 if (is_my_addr) {
1296 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1297 "Drop packet: received my own broadcast (sender: %pM)\n",
1298 ethhdr->h_source);
1299 return;
1302 if (is_my_orig) {
1303 unsigned long *word;
1304 int offset;
1305 int32_t bit_pos;
1306 int16_t if_num;
1307 uint8_t *weight;
1309 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1310 ethhdr->h_source);
1311 if (!orig_neigh_node)
1312 return;
1314 /* neighbor has to indicate direct link and it has to
1315 * come via the corresponding interface
1316 * save packet seqno for bidirectional check
1318 if (has_directlink_flag &&
1319 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1320 batadv_ogm_packet->orig)) {
1321 if_num = if_incoming->if_num;
1322 offset = if_num * BATADV_NUM_WORDS;
1324 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1325 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
1326 bit_pos = if_incoming_seqno - 2;
1327 bit_pos -= ntohl(batadv_ogm_packet->seqno);
1328 batadv_set_bit(word, bit_pos);
1329 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1330 *weight = bitmap_weight(word,
1331 BATADV_TQ_LOCAL_WINDOW_SIZE);
1332 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1335 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1336 "Drop packet: originator packet from myself (via neighbor)\n");
1337 batadv_orig_node_free_ref(orig_neigh_node);
1338 return;
1341 if (is_my_oldorig) {
1342 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1343 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1344 ethhdr->h_source);
1345 return;
1348 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1349 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1350 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1351 ethhdr->h_source);
1352 return;
1355 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1356 if (!orig_node)
1357 return;
1359 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1360 if_incoming);
1362 if (dup_status == BATADV_PROTECTED) {
1363 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1364 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1365 ethhdr->h_source);
1366 goto out;
1369 if (batadv_ogm_packet->tq == 0) {
1370 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1371 "Drop packet: originator packet with tq equal 0\n");
1372 goto out;
1375 router = batadv_orig_node_get_router(orig_node);
1376 if (router) {
1377 orig_node_tmp = router->orig_node;
1378 router_router = batadv_orig_node_get_router(orig_node_tmp);
1381 if ((router && router->bat_iv.tq_avg != 0) &&
1382 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1383 is_from_best_next_hop = true;
1385 prev_sender = batadv_ogm_packet->prev_sender;
1386 /* avoid temporary routing loops */
1387 if (router && router_router &&
1388 (batadv_compare_eth(router->addr, prev_sender)) &&
1389 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
1390 (batadv_compare_eth(router->addr, router_router->addr))) {
1391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1392 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1393 ethhdr->h_source);
1394 goto out;
1397 batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1399 /* if sender is a direct neighbor the sender mac equals
1400 * originator mac
1402 if (is_single_hop_neigh)
1403 orig_neigh_node = orig_node;
1404 else
1405 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1406 ethhdr->h_source);
1408 if (!orig_neigh_node)
1409 goto out;
1411 /* Update nc_nodes of the originator */
1412 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1413 batadv_ogm_packet, is_single_hop_neigh);
1415 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
1417 /* drop packet if sender is not a direct neighbor and if we
1418 * don't route towards it
1420 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1421 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1422 "Drop packet: OGM via unknown neighbor!\n");
1423 goto out_neigh;
1426 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1427 batadv_ogm_packet, if_incoming);
1429 batadv_bonding_save_primary(orig_node, orig_neigh_node,
1430 batadv_ogm_packet);
1432 /* update ranking if it is not a duplicate or has the same
1433 * seqno and similar ttl as the non-duplicate
1435 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
1436 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1437 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1438 (sameseq && similar_ttl)))
1439 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1440 batadv_ogm_packet, if_incoming,
1441 tt_buff, dup_status);
1443 /* is single hop (direct) neighbor */
1444 if (is_single_hop_neigh) {
1445 /* mark direct link on incoming interface */
1446 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1447 is_single_hop_neigh,
1448 is_from_best_next_hop, if_incoming);
1450 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1451 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1452 goto out_neigh;
1455 /* multihop originator */
1456 if (!is_bidirect) {
1457 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1458 "Drop packet: not received via bidirectional link\n");
1459 goto out_neigh;
1462 if (dup_status == BATADV_NEIGH_DUP) {
1463 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1464 "Drop packet: duplicate packet received\n");
1465 goto out_neigh;
1468 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1469 "Forwarding packet: rebroadcast originator packet\n");
1470 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1471 is_single_hop_neigh, is_from_best_next_hop,
1472 if_incoming);
1474 out_neigh:
1475 if ((orig_neigh_node) && (!is_single_hop_neigh))
1476 batadv_orig_node_free_ref(orig_neigh_node);
1477 out:
1478 if (router)
1479 batadv_neigh_node_free_ref(router);
1480 if (router_router)
1481 batadv_neigh_node_free_ref(router_router);
1482 if (orig_neigh_router)
1483 batadv_neigh_node_free_ref(orig_neigh_router);
1485 batadv_orig_node_free_ref(orig_node);
1488 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1489 struct batadv_hard_iface *if_incoming)
1491 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1492 struct batadv_ogm_packet *batadv_ogm_packet;
1493 struct ethhdr *ethhdr;
1494 int buff_pos = 0, packet_len;
1495 unsigned char *tvlv_buff, *packet_buff;
1496 uint8_t *packet_pos;
1497 bool ret;
1499 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1500 if (!ret)
1501 return NET_RX_DROP;
1503 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1504 * that does not have B.A.T.M.A.N. IV enabled ?
1506 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1507 return NET_RX_DROP;
1509 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1510 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1511 skb->len + ETH_HLEN);
1513 packet_len = skb_headlen(skb);
1514 ethhdr = eth_hdr(skb);
1515 packet_buff = skb->data;
1516 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
1518 /* unpack the aggregated packets and process them one by one */
1519 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1520 batadv_ogm_packet->tvlv_len)) {
1521 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
1523 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1524 tvlv_buff, if_incoming);
1526 buff_pos += BATADV_OGM_HLEN;
1527 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
1529 packet_pos = packet_buff + buff_pos;
1530 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1533 kfree_skb(skb);
1534 return NET_RX_SUCCESS;
1538 * batadv_iv_ogm_orig_print - print the originator table
1539 * @bat_priv: the bat priv with all the soft interface information
1540 * @seq: debugfs table seq_file struct
1542 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1543 struct seq_file *seq)
1545 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
1546 struct batadv_hashtable *hash = bat_priv->orig_hash;
1547 int last_seen_msecs, last_seen_secs;
1548 struct batadv_orig_node *orig_node;
1549 unsigned long last_seen_jiffies;
1550 struct hlist_head *head;
1551 int batman_count = 0;
1552 uint32_t i;
1554 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1555 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1556 "Nexthop", "outgoingIF", "Potential nexthops");
1558 for (i = 0; i < hash->size; i++) {
1559 head = &hash->table[i];
1561 rcu_read_lock();
1562 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1563 neigh_node = batadv_orig_node_get_router(orig_node);
1564 if (!neigh_node)
1565 continue;
1567 if (neigh_node->bat_iv.tq_avg == 0)
1568 goto next;
1570 last_seen_jiffies = jiffies - orig_node->last_seen;
1571 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1572 last_seen_secs = last_seen_msecs / 1000;
1573 last_seen_msecs = last_seen_msecs % 1000;
1575 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1576 orig_node->orig, last_seen_secs,
1577 last_seen_msecs, neigh_node->bat_iv.tq_avg,
1578 neigh_node->addr,
1579 neigh_node->if_incoming->net_dev->name);
1581 hlist_for_each_entry_rcu(neigh_node_tmp,
1582 &orig_node->neigh_list, list) {
1583 seq_printf(seq, " %pM (%3i)",
1584 neigh_node_tmp->addr,
1585 neigh_node_tmp->bat_iv.tq_avg);
1588 seq_puts(seq, "\n");
1589 batman_count++;
1591 next:
1592 batadv_neigh_node_free_ref(neigh_node);
1594 rcu_read_unlock();
1597 if (batman_count == 0)
1598 seq_puts(seq, "No batman nodes in range ...\n");
1602 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1603 * @neigh1: the first neighbor object of the comparison
1604 * @neigh2: the second neighbor object of the comparison
1606 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1607 * lower, the same as or higher than the metric via neigh2
1609 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
1610 struct batadv_neigh_node *neigh2)
1612 uint8_t tq1, tq2;
1614 tq1 = neigh1->bat_iv.tq_avg;
1615 tq2 = neigh2->bat_iv.tq_avg;
1617 return tq1 - tq2;
1621 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
1622 * neigh2 from the metric prospective
1623 * @neigh1: the first neighbor object of the comparison
1624 * @neigh2: the second neighbor object of the comparison
1626 * Returns true if the metric via neigh1 is equally good or better than the
1627 * metric via neigh2, false otherwise.
1629 static bool batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
1630 struct batadv_neigh_node *neigh2)
1632 int diff = batadv_iv_ogm_neigh_cmp(neigh1, neigh2);
1634 return diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
1637 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1638 .name = "BATMAN_IV",
1639 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1640 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1641 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1642 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1643 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1644 .bat_ogm_emit = batadv_iv_ogm_emit,
1645 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
1646 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
1647 .bat_orig_print = batadv_iv_ogm_orig_print,
1648 .bat_orig_free = batadv_iv_ogm_orig_free,
1649 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
1650 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
1653 int __init batadv_iv_init(void)
1655 int ret;
1657 /* batman originator packet */
1658 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1659 batadv_iv_ogm_receive);
1660 if (ret < 0)
1661 goto out;
1663 ret = batadv_algo_register(&batadv_batman_iv);
1664 if (ret < 0)
1665 goto handler_unregister;
1667 goto out;
1669 handler_unregister:
1670 batadv_recv_handler_unregister(BATADV_IV_OGM);
1671 out:
1672 return ret;