2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "translation-table.h"
24 #include "ring_buffer.h"
25 #include "originator.h"
27 #include "gateway_common.h"
28 #include "gateway_client.h"
29 #include "hard-interface.h"
33 static struct neigh_node *bat_iv_ogm_neigh_new(struct hard_iface *hard_iface,
34 const uint8_t *neigh_addr,
35 struct orig_node *orig_node,
36 struct orig_node *orig_neigh,
39 struct neigh_node *neigh_node;
41 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, seqno);
45 INIT_LIST_HEAD(&neigh_node->bonding_list);
46 spin_lock_init(&neigh_node->tq_lock);
48 neigh_node->orig_node = orig_neigh;
49 neigh_node->if_incoming = hard_iface;
51 spin_lock_bh(&orig_node->neigh_list_lock);
52 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
53 spin_unlock_bh(&orig_node->neigh_list_lock);
59 static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
61 struct batman_ogm_packet *batman_ogm_packet;
62 uint32_t random_seqno;
65 /* randomize initial seqno to avoid collision */
66 get_random_bytes(&random_seqno, sizeof(random_seqno));
67 atomic_set(&hard_iface->seqno, random_seqno);
69 hard_iface->packet_len = BATMAN_OGM_HLEN;
70 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
72 if (!hard_iface->packet_buff)
75 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
76 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
77 batman_ogm_packet->header.version = COMPAT_VERSION;
78 batman_ogm_packet->header.ttl = 2;
79 batman_ogm_packet->flags = NO_FLAGS;
80 batman_ogm_packet->tq = TQ_MAX_VALUE;
81 batman_ogm_packet->tt_num_changes = 0;
82 batman_ogm_packet->ttvn = 0;
90 static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
92 kfree(hard_iface->packet_buff);
93 hard_iface->packet_buff = NULL;
96 static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
98 struct batman_ogm_packet *batman_ogm_packet;
100 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
101 memcpy(batman_ogm_packet->orig,
102 hard_iface->net_dev->dev_addr, ETH_ALEN);
103 memcpy(batman_ogm_packet->prev_sender,
104 hard_iface->net_dev->dev_addr, ETH_ALEN);
107 static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
109 struct batman_ogm_packet *batman_ogm_packet;
111 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
112 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
113 batman_ogm_packet->header.ttl = TTL;
116 /* when do we schedule our own ogm to be sent */
117 static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
119 return jiffies + msecs_to_jiffies(
120 atomic_read(&bat_priv->orig_interval) -
121 JITTER + (random32() % 2*JITTER));
124 /* when do we schedule a ogm packet to be sent */
125 static unsigned long bat_iv_ogm_fwd_send_time(void)
127 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
130 /* apply hop penalty for a normal link */
131 static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
133 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
134 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
137 /* is there another aggregated packet here? */
138 static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
141 int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes);
143 return (next_buff_pos <= packet_len) &&
144 (next_buff_pos <= MAX_AGGREGATION_BYTES);
147 /* send a batman ogm to a given interface */
148 static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
149 struct hard_iface *hard_iface)
151 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
155 struct batman_ogm_packet *batman_ogm_packet;
158 if (hard_iface->if_status != IF_ACTIVE)
163 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
165 /* adjust all flags and log packets */
166 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
167 batman_ogm_packet->tt_num_changes)) {
169 /* we might have aggregated direct link packets with an
170 * ordinary base packet */
171 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
172 (forw_packet->if_incoming == hard_iface))
173 batman_ogm_packet->flags |= DIRECTLINK;
175 batman_ogm_packet->flags &= ~DIRECTLINK;
177 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
180 bat_dbg(DBG_BATMAN, bat_priv,
181 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
182 fwd_str, (packet_num > 0 ? "aggregated " : ""),
183 batman_ogm_packet->orig,
184 ntohl(batman_ogm_packet->seqno),
185 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
186 (batman_ogm_packet->flags & DIRECTLINK ?
188 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
189 hard_iface->net_dev->dev_addr);
191 buff_pos += BATMAN_OGM_HLEN +
192 tt_len(batman_ogm_packet->tt_num_changes);
194 batman_ogm_packet = (struct batman_ogm_packet *)
195 (forw_packet->skb->data + buff_pos);
198 /* create clone because function is called more than once */
199 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
201 send_skb_packet(skb, hard_iface, broadcast_addr);
204 /* send a batman ogm packet */
205 static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
207 struct hard_iface *hard_iface;
208 struct net_device *soft_iface;
209 struct bat_priv *bat_priv;
210 struct hard_iface *primary_if = NULL;
211 struct batman_ogm_packet *batman_ogm_packet;
212 unsigned char directlink;
214 batman_ogm_packet = (struct batman_ogm_packet *)
215 (forw_packet->skb->data);
216 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
218 if (!forw_packet->if_incoming) {
219 pr_err("Error - can't forward packet: incoming iface not specified\n");
223 soft_iface = forw_packet->if_incoming->soft_iface;
224 bat_priv = netdev_priv(soft_iface);
226 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
229 primary_if = primary_if_get_selected(bat_priv);
233 /* multihomed peer assumed */
234 /* non-primary OGMs are only broadcasted on their interface */
235 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
236 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
238 /* FIXME: what about aggregated packets ? */
239 bat_dbg(DBG_BATMAN, bat_priv,
240 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
241 (forw_packet->own ? "Sending own" : "Forwarding"),
242 batman_ogm_packet->orig,
243 ntohl(batman_ogm_packet->seqno),
244 batman_ogm_packet->header.ttl,
245 forw_packet->if_incoming->net_dev->name,
246 forw_packet->if_incoming->net_dev->dev_addr);
248 /* skb is only used once and than forw_packet is free'd */
249 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
251 forw_packet->skb = NULL;
256 /* broadcast on every interface */
258 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
259 if (hard_iface->soft_iface != soft_iface)
262 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
268 hardif_free_ref(primary_if);
271 /* return true if new_packet can be aggregated with forw_packet */
272 static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
273 *new_batman_ogm_packet,
274 struct bat_priv *bat_priv,
275 int packet_len, unsigned long send_time,
277 const struct hard_iface *if_incoming,
278 const struct forw_packet *forw_packet)
280 struct batman_ogm_packet *batman_ogm_packet;
281 int aggregated_bytes = forw_packet->packet_len + packet_len;
282 struct hard_iface *primary_if = NULL;
285 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
288 * we can aggregate the current packet to this aggregated packet
291 * - the send time is within our MAX_AGGREGATION_MS time
292 * - the resulting packet wont be bigger than
293 * MAX_AGGREGATION_BYTES
296 if (time_before(send_time, forw_packet->send_time) &&
297 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
298 forw_packet->send_time) &&
299 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
302 * check aggregation compatibility
303 * -> direct link packets are broadcasted on
304 * their interface only
305 * -> aggregate packet if the current packet is
306 * a "global" packet as well as the base
310 primary_if = primary_if_get_selected(bat_priv);
314 /* packets without direct link flag and high TTL
315 * are flooded through the net */
317 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
318 (batman_ogm_packet->header.ttl != 1) &&
320 /* own packets originating non-primary
321 * interfaces leave only that interface */
322 ((!forw_packet->own) ||
323 (forw_packet->if_incoming == primary_if))) {
328 /* if the incoming packet is sent via this one
329 * interface only - we still can aggregate */
331 (new_batman_ogm_packet->header.ttl == 1) &&
332 (forw_packet->if_incoming == if_incoming) &&
334 /* packets from direct neighbors or
335 * own secondary interface packets
336 * (= secondary interface packets in general) */
337 (batman_ogm_packet->flags & DIRECTLINK ||
339 forw_packet->if_incoming != primary_if))) {
347 hardif_free_ref(primary_if);
351 /* create a new aggregated packet and add this packet to it */
352 static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
353 int packet_len, unsigned long send_time,
355 struct hard_iface *if_incoming,
358 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
359 struct forw_packet *forw_packet_aggr;
360 unsigned char *skb_buff;
362 if (!atomic_inc_not_zero(&if_incoming->refcount))
365 /* own packet should always be scheduled */
367 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
368 bat_dbg(DBG_BATMAN, bat_priv,
369 "batman packet queue full\n");
374 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
375 if (!forw_packet_aggr) {
377 atomic_inc(&bat_priv->batman_queue_left);
381 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
382 (packet_len < MAX_AGGREGATION_BYTES))
383 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
386 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
388 if (!forw_packet_aggr->skb) {
390 atomic_inc(&bat_priv->batman_queue_left);
391 kfree(forw_packet_aggr);
394 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
396 INIT_HLIST_NODE(&forw_packet_aggr->list);
398 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
399 forw_packet_aggr->packet_len = packet_len;
400 memcpy(skb_buff, packet_buff, packet_len);
402 forw_packet_aggr->own = own_packet;
403 forw_packet_aggr->if_incoming = if_incoming;
404 forw_packet_aggr->num_packets = 0;
405 forw_packet_aggr->direct_link_flags = NO_FLAGS;
406 forw_packet_aggr->send_time = send_time;
408 /* save packet direct link flag status */
410 forw_packet_aggr->direct_link_flags |= 1;
412 /* add new packet to packet list */
413 spin_lock_bh(&bat_priv->forw_bat_list_lock);
414 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
415 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
417 /* start timer for this packet */
418 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
419 send_outstanding_bat_ogm_packet);
420 queue_delayed_work(bat_event_workqueue,
421 &forw_packet_aggr->delayed_work,
422 send_time - jiffies);
426 hardif_free_ref(if_incoming);
429 /* aggregate a new packet into the existing ogm packet */
430 static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
431 const unsigned char *packet_buff,
432 int packet_len, bool direct_link)
434 unsigned char *skb_buff;
436 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
437 memcpy(skb_buff, packet_buff, packet_len);
438 forw_packet_aggr->packet_len += packet_len;
439 forw_packet_aggr->num_packets++;
441 /* save packet direct link flag status */
443 forw_packet_aggr->direct_link_flags |=
444 (1 << forw_packet_aggr->num_packets);
447 static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
448 unsigned char *packet_buff,
449 int packet_len, struct hard_iface *if_incoming,
450 int own_packet, unsigned long send_time)
453 * _aggr -> pointer to the packet we want to aggregate with
454 * _pos -> pointer to the position in the queue
456 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
457 struct hlist_node *tmp_node;
458 struct batman_ogm_packet *batman_ogm_packet;
461 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
462 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
464 /* find position for the packet in the forward queue */
465 spin_lock_bh(&bat_priv->forw_bat_list_lock);
466 /* own packets are not to be aggregated */
467 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
468 hlist_for_each_entry(forw_packet_pos, tmp_node,
469 &bat_priv->forw_bat_list, list) {
470 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
471 bat_priv, packet_len,
472 send_time, direct_link,
475 forw_packet_aggr = forw_packet_pos;
481 /* nothing to aggregate with - either aggregation disabled or no
482 * suitable aggregation packet found */
483 if (!forw_packet_aggr) {
484 /* the following section can run without the lock */
485 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
488 * if we could not aggregate this packet with one of the others
489 * we hold it back for a while, so that it might be aggregated
493 (atomic_read(&bat_priv->aggregated_ogms)))
494 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
496 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
497 send_time, direct_link,
498 if_incoming, own_packet);
500 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
501 packet_len, direct_link);
502 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
506 static void bat_iv_ogm_forward(struct orig_node *orig_node,
507 const struct ethhdr *ethhdr,
508 struct batman_ogm_packet *batman_ogm_packet,
509 bool is_single_hop_neigh,
510 bool is_from_best_next_hop,
511 struct hard_iface *if_incoming)
513 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
514 uint8_t tt_num_changes;
516 if (batman_ogm_packet->header.ttl <= 1) {
517 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
521 if (!is_from_best_next_hop) {
522 /* Mark the forwarded packet when it is not coming from our
523 * best next hop. We still need to forward the packet for our
524 * neighbor link quality detection to work in case the packet
525 * originated from a single hop neighbor. Otherwise we can
526 * simply drop the ogm.
528 if (is_single_hop_neigh)
529 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
534 tt_num_changes = batman_ogm_packet->tt_num_changes;
536 batman_ogm_packet->header.ttl--;
537 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
539 /* apply hop penalty */
540 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
542 bat_dbg(DBG_BATMAN, bat_priv,
543 "Forwarding packet: tq: %i, ttl: %i\n",
544 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
546 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
547 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
549 /* switch of primaries first hop flag when forwarding */
550 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
551 if (is_single_hop_neigh)
552 batman_ogm_packet->flags |= DIRECTLINK;
554 batman_ogm_packet->flags &= ~DIRECTLINK;
556 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
557 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
558 if_incoming, 0, bat_iv_ogm_fwd_send_time());
561 static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
564 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
565 struct batman_ogm_packet *batman_ogm_packet;
566 struct hard_iface *primary_if;
569 vis_server = atomic_read(&bat_priv->vis_mode);
570 primary_if = primary_if_get_selected(bat_priv);
572 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
574 /* change sequence number to network order */
575 batman_ogm_packet->seqno =
576 htonl((uint32_t)atomic_read(&hard_iface->seqno));
578 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
579 batman_ogm_packet->tt_crc = htons((uint16_t)
580 atomic_read(&bat_priv->tt_crc));
581 if (tt_num_changes >= 0)
582 batman_ogm_packet->tt_num_changes = tt_num_changes;
584 if (vis_server == VIS_TYPE_SERVER_SYNC)
585 batman_ogm_packet->flags |= VIS_SERVER;
587 batman_ogm_packet->flags &= ~VIS_SERVER;
589 if ((hard_iface == primary_if) &&
590 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
591 batman_ogm_packet->gw_flags =
592 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
594 batman_ogm_packet->gw_flags = NO_FLAGS;
596 atomic_inc(&hard_iface->seqno);
598 slide_own_bcast_window(hard_iface);
599 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
600 hard_iface->packet_len, hard_iface, 1,
601 bat_iv_ogm_emit_send_time(bat_priv));
604 hardif_free_ref(primary_if);
607 static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
608 struct orig_node *orig_node,
609 const struct ethhdr *ethhdr,
610 const struct batman_ogm_packet
612 struct hard_iface *if_incoming,
613 const unsigned char *tt_buff,
616 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
617 struct neigh_node *router = NULL;
618 struct orig_node *orig_node_tmp;
619 struct hlist_node *node;
620 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
622 bat_dbg(DBG_BATMAN, bat_priv,
623 "update_originator(): Searching and updating originator entry of received packet\n");
626 hlist_for_each_entry_rcu(tmp_neigh_node, node,
627 &orig_node->neigh_list, list) {
628 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
629 (tmp_neigh_node->if_incoming == if_incoming) &&
630 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
632 neigh_node_free_ref(neigh_node);
633 neigh_node = tmp_neigh_node;
640 spin_lock_bh(&tmp_neigh_node->tq_lock);
641 ring_buffer_set(tmp_neigh_node->tq_recv,
642 &tmp_neigh_node->tq_index, 0);
643 tmp_neigh_node->tq_avg =
644 ring_buffer_avg(tmp_neigh_node->tq_recv);
645 spin_unlock_bh(&tmp_neigh_node->tq_lock);
649 struct orig_node *orig_tmp;
651 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
655 neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source,
657 batman_ogm_packet->seqno);
659 orig_node_free_ref(orig_tmp);
663 bat_dbg(DBG_BATMAN, bat_priv,
664 "Updating existing last-hop neighbor of originator\n");
668 orig_node->flags = batman_ogm_packet->flags;
669 neigh_node->last_seen = jiffies;
671 spin_lock_bh(&neigh_node->tq_lock);
672 ring_buffer_set(neigh_node->tq_recv,
673 &neigh_node->tq_index,
674 batman_ogm_packet->tq);
675 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
676 spin_unlock_bh(&neigh_node->tq_lock);
679 orig_node->last_ttl = batman_ogm_packet->header.ttl;
680 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
683 bonding_candidate_add(orig_node, neigh_node);
685 /* if this neighbor already is our next hop there is nothing
687 router = orig_node_get_router(orig_node);
688 if (router == neigh_node)
691 /* if this neighbor does not offer a better TQ we won't consider it */
692 if (router && (router->tq_avg > neigh_node->tq_avg))
695 /* if the TQ is the same and the link not more symmetric we
696 * won't consider it either */
697 if (router && (neigh_node->tq_avg == router->tq_avg)) {
698 orig_node_tmp = router->orig_node;
699 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
701 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
702 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
704 orig_node_tmp = neigh_node->orig_node;
705 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
706 bcast_own_sum_neigh =
707 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
708 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
710 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
714 update_route(bat_priv, orig_node, neigh_node);
717 /* I have to check for transtable changes only if the OGM has been
718 * sent through a primary interface */
719 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
720 (batman_ogm_packet->header.ttl > 2)) ||
721 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
722 tt_update_orig(bat_priv, orig_node, tt_buff,
723 batman_ogm_packet->tt_num_changes,
724 batman_ogm_packet->ttvn,
725 batman_ogm_packet->tt_crc);
727 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
728 gw_node_update(bat_priv, orig_node,
729 batman_ogm_packet->gw_flags);
731 orig_node->gw_flags = batman_ogm_packet->gw_flags;
733 /* restart gateway selection if fast or late switching was enabled */
734 if ((orig_node->gw_flags) &&
735 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
736 (atomic_read(&bat_priv->gw_sel_class) > 2))
737 gw_check_election(bat_priv, orig_node);
745 neigh_node_free_ref(neigh_node);
747 neigh_node_free_ref(router);
750 static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
751 struct orig_node *orig_neigh_node,
752 struct batman_ogm_packet *batman_ogm_packet,
753 struct hard_iface *if_incoming)
755 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
756 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
757 struct hlist_node *node;
759 uint8_t orig_eq_count, neigh_rq_count, tq_own;
760 int tq_asym_penalty, ret = 0;
762 /* find corresponding one hop neighbor */
764 hlist_for_each_entry_rcu(tmp_neigh_node, node,
765 &orig_neigh_node->neigh_list, list) {
767 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
770 if (tmp_neigh_node->if_incoming != if_incoming)
773 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
776 neigh_node = tmp_neigh_node;
782 neigh_node = bat_iv_ogm_neigh_new(if_incoming,
783 orig_neigh_node->orig,
786 batman_ogm_packet->seqno);
791 /* if orig_node is direct neighbor update neigh_node last_seen */
792 if (orig_node == orig_neigh_node)
793 neigh_node->last_seen = jiffies;
795 orig_node->last_seen = jiffies;
797 /* find packet count of corresponding one hop neighbor */
798 spin_lock_bh(&orig_node->ogm_cnt_lock);
799 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
800 neigh_rq_count = neigh_node->real_packet_count;
801 spin_unlock_bh(&orig_node->ogm_cnt_lock);
803 /* pay attention to not get a value bigger than 100 % */
804 total_count = (orig_eq_count > neigh_rq_count ?
805 neigh_rq_count : orig_eq_count);
807 /* if we have too few packets (too less data) we set tq_own to zero */
808 /* if we receive too few packets it is not considered bidirectional */
809 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
810 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
813 /* neigh_node->real_packet_count is never zero as we
814 * only purge old information when getting new
816 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
818 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
819 * affect the nearly-symmetric links only a little, but
820 * punishes asymmetric links more. This will give a value
821 * between 0 and TQ_MAX_VALUE
823 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
824 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
825 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
826 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
827 (TQ_LOCAL_WINDOW_SIZE *
828 TQ_LOCAL_WINDOW_SIZE *
829 TQ_LOCAL_WINDOW_SIZE);
831 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
833 (TQ_MAX_VALUE * TQ_MAX_VALUE));
835 bat_dbg(DBG_BATMAN, bat_priv,
836 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
837 orig_node->orig, orig_neigh_node->orig, total_count,
838 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
840 /* if link has the minimum required transmission quality
841 * consider it bidirectional */
842 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
847 neigh_node_free_ref(neigh_node);
851 /* processes a batman packet for all interfaces, adjusts the sequence number and
852 * finds out whether it is a duplicate.
854 * 1 the packet is a duplicate
855 * 0 the packet has not yet been received
856 * -1 the packet is old and has been received while the seqno window
857 * was protected. Caller should drop it.
859 static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
860 const struct batman_ogm_packet
862 const struct hard_iface *if_incoming)
864 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
865 struct orig_node *orig_node;
866 struct neigh_node *tmp_neigh_node;
867 struct hlist_node *node;
868 int is_duplicate = 0;
871 int set_mark, ret = -1;
873 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
877 spin_lock_bh(&orig_node->ogm_cnt_lock);
878 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
880 /* signalize caller that the packet is to be dropped. */
881 if (!hlist_empty(&orig_node->neigh_list) &&
882 window_protected(bat_priv, seq_diff,
883 &orig_node->batman_seqno_reset))
887 hlist_for_each_entry_rcu(tmp_neigh_node, node,
888 &orig_node->neigh_list, list) {
890 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
891 orig_node->last_real_seqno,
892 batman_ogm_packet->seqno);
894 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
895 (tmp_neigh_node->if_incoming == if_incoming))
900 /* if the window moved, set the update flag. */
901 need_update |= bit_get_packet(bat_priv,
902 tmp_neigh_node->real_bits,
905 tmp_neigh_node->real_packet_count =
906 bitmap_weight(tmp_neigh_node->real_bits,
907 TQ_LOCAL_WINDOW_SIZE);
912 bat_dbg(DBG_BATMAN, bat_priv,
913 "updating last_seqno: old %u, new %u\n",
914 orig_node->last_real_seqno, batman_ogm_packet->seqno);
915 orig_node->last_real_seqno = batman_ogm_packet->seqno;
921 spin_unlock_bh(&orig_node->ogm_cnt_lock);
922 orig_node_free_ref(orig_node);
926 static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
927 struct batman_ogm_packet *batman_ogm_packet,
928 const unsigned char *tt_buff,
929 struct hard_iface *if_incoming)
931 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
932 struct hard_iface *hard_iface;
933 struct orig_node *orig_neigh_node, *orig_node;
934 struct neigh_node *router = NULL, *router_router = NULL;
935 struct neigh_node *orig_neigh_router = NULL;
936 int has_directlink_flag;
937 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
938 int is_broadcast = 0, is_bidirectional;
939 bool is_single_hop_neigh = false;
940 bool is_from_best_next_hop = false;
942 uint32_t if_incoming_seqno;
944 /* Silently drop when the batman packet is actually not a
947 * This might happen if a packet is padded (e.g. Ethernet has a
948 * minimum frame length of 64 byte) and the aggregation interprets
949 * it as an additional length.
951 * TODO: A more sane solution would be to have a bit in the
952 * batman_ogm_packet to detect whether the packet is the last
953 * packet in an aggregation. Here we expect that the padding
954 * is always zero (or not 0x01)
956 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
959 /* could be changed by schedule_own_packet() */
960 if_incoming_seqno = atomic_read(&if_incoming->seqno);
962 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
964 if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
965 is_single_hop_neigh = true;
967 bat_dbg(DBG_BATMAN, bat_priv,
968 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
969 ethhdr->h_source, if_incoming->net_dev->name,
970 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
971 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
972 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
973 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
974 batman_ogm_packet->header.ttl,
975 batman_ogm_packet->header.version, has_directlink_flag);
978 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
979 if (hard_iface->if_status != IF_ACTIVE)
982 if (hard_iface->soft_iface != if_incoming->soft_iface)
985 if (compare_eth(ethhdr->h_source,
986 hard_iface->net_dev->dev_addr))
989 if (compare_eth(batman_ogm_packet->orig,
990 hard_iface->net_dev->dev_addr))
993 if (compare_eth(batman_ogm_packet->prev_sender,
994 hard_iface->net_dev->dev_addr))
997 if (is_broadcast_ether_addr(ethhdr->h_source))
1002 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
1003 bat_dbg(DBG_BATMAN, bat_priv,
1004 "Drop packet: incompatible batman version (%i)\n",
1005 batman_ogm_packet->header.version);
1010 bat_dbg(DBG_BATMAN, bat_priv,
1011 "Drop packet: received my own broadcast (sender: %pM)\n",
1017 bat_dbg(DBG_BATMAN, bat_priv,
1018 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1024 unsigned long *word;
1027 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1028 if (!orig_neigh_node)
1031 /* neighbor has to indicate direct link and it has to
1032 * come via the corresponding interface */
1033 /* save packet seqno for bidirectional check */
1034 if (has_directlink_flag &&
1035 compare_eth(if_incoming->net_dev->dev_addr,
1036 batman_ogm_packet->orig)) {
1037 offset = if_incoming->if_num * NUM_WORDS;
1039 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1040 word = &(orig_neigh_node->bcast_own[offset]);
1043 batman_ogm_packet->seqno - 2);
1044 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
1045 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
1046 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1049 bat_dbg(DBG_BATMAN, bat_priv,
1050 "Drop packet: originator packet from myself (via neighbor)\n");
1051 orig_node_free_ref(orig_neigh_node);
1055 if (is_my_oldorig) {
1056 bat_dbg(DBG_BATMAN, bat_priv,
1057 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1062 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1063 bat_dbg(DBG_BATMAN, bat_priv,
1064 "Drop packet: ignoring all packets not forwarded from "
1065 "the best next hop (sender: %pM)\n", ethhdr->h_source);
1069 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1073 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1076 if (is_duplicate == -1) {
1077 bat_dbg(DBG_BATMAN, bat_priv,
1078 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1083 if (batman_ogm_packet->tq == 0) {
1084 bat_dbg(DBG_BATMAN, bat_priv,
1085 "Drop packet: originator packet with tq equal 0\n");
1089 router = orig_node_get_router(orig_node);
1091 router_router = orig_node_get_router(router->orig_node);
1093 if ((router && router->tq_avg != 0) &&
1094 (compare_eth(router->addr, ethhdr->h_source)))
1095 is_from_best_next_hop = true;
1097 /* avoid temporary routing loops */
1098 if (router && router_router &&
1099 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1100 !(compare_eth(batman_ogm_packet->orig,
1101 batman_ogm_packet->prev_sender)) &&
1102 (compare_eth(router->addr, router_router->addr))) {
1103 bat_dbg(DBG_BATMAN, bat_priv,
1104 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1109 /* if sender is a direct neighbor the sender mac equals
1111 orig_neigh_node = (is_single_hop_neigh ?
1113 get_orig_node(bat_priv, ethhdr->h_source));
1114 if (!orig_neigh_node)
1117 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1119 /* drop packet if sender is not a direct neighbor and if we
1120 * don't route towards it */
1121 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1122 bat_dbg(DBG_BATMAN, bat_priv,
1123 "Drop packet: OGM via unknown neighbor!\n");
1127 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1128 batman_ogm_packet, if_incoming);
1130 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1132 /* update ranking if it is not a duplicate or has the same
1133 * seqno and similar ttl as the non-duplicate */
1134 if (is_bidirectional &&
1136 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
1137 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
1138 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1139 batman_ogm_packet, if_incoming,
1140 tt_buff, is_duplicate);
1142 /* is single hop (direct) neighbor */
1143 if (is_single_hop_neigh) {
1145 /* mark direct link on incoming interface */
1146 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1147 is_single_hop_neigh, is_from_best_next_hop,
1150 bat_dbg(DBG_BATMAN, bat_priv,
1151 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1155 /* multihop originator */
1156 if (!is_bidirectional) {
1157 bat_dbg(DBG_BATMAN, bat_priv,
1158 "Drop packet: not received via bidirectional link\n");
1163 bat_dbg(DBG_BATMAN, bat_priv,
1164 "Drop packet: duplicate packet received\n");
1168 bat_dbg(DBG_BATMAN, bat_priv,
1169 "Forwarding packet: rebroadcast originator packet\n");
1170 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1171 is_single_hop_neigh, is_from_best_next_hop,
1175 if ((orig_neigh_node) && (!is_single_hop_neigh))
1176 orig_node_free_ref(orig_neigh_node);
1179 neigh_node_free_ref(router);
1181 neigh_node_free_ref(router_router);
1182 if (orig_neigh_router)
1183 neigh_node_free_ref(orig_neigh_router);
1185 orig_node_free_ref(orig_node);
1188 static int bat_iv_ogm_receive(struct sk_buff *skb,
1189 struct hard_iface *if_incoming)
1191 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1192 struct batman_ogm_packet *batman_ogm_packet;
1193 struct ethhdr *ethhdr;
1194 int buff_pos = 0, packet_len;
1195 unsigned char *tt_buff, *packet_buff;
1198 ret = check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
1202 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1203 * that does not have B.A.T.M.A.N. IV enabled ?
1205 if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit)
1208 packet_len = skb_headlen(skb);
1209 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1210 packet_buff = skb->data;
1211 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1213 /* unpack the aggregated packets and process them one by one */
1215 /* network to host order for our 32bit seqno and the
1217 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1218 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1220 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
1222 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1223 tt_buff, if_incoming);
1225 buff_pos += BATMAN_OGM_HLEN +
1226 tt_len(batman_ogm_packet->tt_num_changes);
1228 batman_ogm_packet = (struct batman_ogm_packet *)
1229 (packet_buff + buff_pos);
1230 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1231 batman_ogm_packet->tt_num_changes));
1234 return NET_RX_SUCCESS;
1237 static struct bat_algo_ops batman_iv __read_mostly = {
1238 .name = "BATMAN IV",
1239 .bat_iface_enable = bat_iv_ogm_iface_enable,
1240 .bat_iface_disable = bat_iv_ogm_iface_disable,
1241 .bat_iface_update_mac = bat_iv_ogm_iface_update_mac,
1242 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
1243 .bat_ogm_schedule = bat_iv_ogm_schedule,
1244 .bat_ogm_emit = bat_iv_ogm_emit,
1247 int __init bat_iv_init(void)
1251 /* batman originator packet */
1252 ret = recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive);
1256 ret = bat_algo_register(&batman_iv);
1258 goto handler_unregister;
1263 recv_handler_unregister(BAT_IV_OGM);