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);
47 neigh_node->orig_node = orig_neigh;
48 neigh_node->if_incoming = hard_iface;
50 spin_lock_bh(&orig_node->neigh_list_lock);
51 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
52 spin_unlock_bh(&orig_node->neigh_list_lock);
58 static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
60 struct batman_ogm_packet *batman_ogm_packet;
61 uint32_t random_seqno;
64 /* randomize initial seqno to avoid collision */
65 get_random_bytes(&random_seqno, sizeof(random_seqno));
66 atomic_set(&hard_iface->seqno, random_seqno);
68 hard_iface->packet_len = BATMAN_OGM_HLEN;
69 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
71 if (!hard_iface->packet_buff)
74 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
75 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
76 batman_ogm_packet->header.version = COMPAT_VERSION;
77 batman_ogm_packet->header.ttl = 2;
78 batman_ogm_packet->flags = NO_FLAGS;
79 batman_ogm_packet->tq = TQ_MAX_VALUE;
80 batman_ogm_packet->tt_num_changes = 0;
81 batman_ogm_packet->ttvn = 0;
89 static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
91 kfree(hard_iface->packet_buff);
92 hard_iface->packet_buff = NULL;
95 static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
97 struct batman_ogm_packet *batman_ogm_packet;
99 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
100 memcpy(batman_ogm_packet->orig,
101 hard_iface->net_dev->dev_addr, ETH_ALEN);
102 memcpy(batman_ogm_packet->prev_sender,
103 hard_iface->net_dev->dev_addr, ETH_ALEN);
106 static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
108 struct batman_ogm_packet *batman_ogm_packet;
110 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
111 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
112 batman_ogm_packet->header.ttl = TTL;
115 /* when do we schedule our own ogm to be sent */
116 static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
118 return jiffies + msecs_to_jiffies(
119 atomic_read(&bat_priv->orig_interval) -
120 JITTER + (random32() % 2*JITTER));
123 /* when do we schedule a ogm packet to be sent */
124 static unsigned long bat_iv_ogm_fwd_send_time(void)
126 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
129 /* apply hop penalty for a normal link */
130 static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
132 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
133 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
136 /* is there another aggregated packet here? */
137 static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
140 int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes);
142 return (next_buff_pos <= packet_len) &&
143 (next_buff_pos <= MAX_AGGREGATION_BYTES);
146 /* send a batman ogm to a given interface */
147 static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
148 struct hard_iface *hard_iface)
150 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
154 struct batman_ogm_packet *batman_ogm_packet;
157 if (hard_iface->if_status != IF_ACTIVE)
162 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
164 /* adjust all flags and log packets */
165 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
166 batman_ogm_packet->tt_num_changes)) {
168 /* we might have aggregated direct link packets with an
169 * ordinary base packet */
170 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
171 (forw_packet->if_incoming == hard_iface))
172 batman_ogm_packet->flags |= DIRECTLINK;
174 batman_ogm_packet->flags &= ~DIRECTLINK;
176 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
179 bat_dbg(DBG_BATMAN, bat_priv,
180 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
181 fwd_str, (packet_num > 0 ? "aggregated " : ""),
182 batman_ogm_packet->orig,
183 ntohl(batman_ogm_packet->seqno),
184 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
185 (batman_ogm_packet->flags & DIRECTLINK ?
187 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
188 hard_iface->net_dev->dev_addr);
190 buff_pos += BATMAN_OGM_HLEN +
191 tt_len(batman_ogm_packet->tt_num_changes);
193 batman_ogm_packet = (struct batman_ogm_packet *)
194 (forw_packet->skb->data + buff_pos);
197 /* create clone because function is called more than once */
198 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
200 send_skb_packet(skb, hard_iface, broadcast_addr);
203 /* send a batman ogm packet */
204 static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
206 struct hard_iface *hard_iface;
207 struct net_device *soft_iface;
208 struct bat_priv *bat_priv;
209 struct hard_iface *primary_if = NULL;
210 struct batman_ogm_packet *batman_ogm_packet;
211 unsigned char directlink;
213 batman_ogm_packet = (struct batman_ogm_packet *)
214 (forw_packet->skb->data);
215 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
217 if (!forw_packet->if_incoming) {
218 pr_err("Error - can't forward packet: incoming iface not specified\n");
222 soft_iface = forw_packet->if_incoming->soft_iface;
223 bat_priv = netdev_priv(soft_iface);
225 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
228 primary_if = primary_if_get_selected(bat_priv);
232 /* multihomed peer assumed */
233 /* non-primary OGMs are only broadcasted on their interface */
234 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
235 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
237 /* FIXME: what about aggregated packets ? */
238 bat_dbg(DBG_BATMAN, bat_priv,
239 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
240 (forw_packet->own ? "Sending own" : "Forwarding"),
241 batman_ogm_packet->orig,
242 ntohl(batman_ogm_packet->seqno),
243 batman_ogm_packet->header.ttl,
244 forw_packet->if_incoming->net_dev->name,
245 forw_packet->if_incoming->net_dev->dev_addr);
247 /* skb is only used once and than forw_packet is free'd */
248 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
250 forw_packet->skb = NULL;
255 /* broadcast on every interface */
257 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
258 if (hard_iface->soft_iface != soft_iface)
261 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
267 hardif_free_ref(primary_if);
270 /* return true if new_packet can be aggregated with forw_packet */
271 static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
272 *new_batman_ogm_packet,
273 struct bat_priv *bat_priv,
274 int packet_len, unsigned long send_time,
276 const struct hard_iface *if_incoming,
277 const struct forw_packet *forw_packet)
279 struct batman_ogm_packet *batman_ogm_packet;
280 int aggregated_bytes = forw_packet->packet_len + packet_len;
281 struct hard_iface *primary_if = NULL;
284 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
287 * we can aggregate the current packet to this aggregated packet
290 * - the send time is within our MAX_AGGREGATION_MS time
291 * - the resulting packet wont be bigger than
292 * MAX_AGGREGATION_BYTES
295 if (time_before(send_time, forw_packet->send_time) &&
296 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
297 forw_packet->send_time) &&
298 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
301 * check aggregation compatibility
302 * -> direct link packets are broadcasted on
303 * their interface only
304 * -> aggregate packet if the current packet is
305 * a "global" packet as well as the base
309 primary_if = primary_if_get_selected(bat_priv);
313 /* packets without direct link flag and high TTL
314 * are flooded through the net */
316 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
317 (batman_ogm_packet->header.ttl != 1) &&
319 /* own packets originating non-primary
320 * interfaces leave only that interface */
321 ((!forw_packet->own) ||
322 (forw_packet->if_incoming == primary_if))) {
327 /* if the incoming packet is sent via this one
328 * interface only - we still can aggregate */
330 (new_batman_ogm_packet->header.ttl == 1) &&
331 (forw_packet->if_incoming == if_incoming) &&
333 /* packets from direct neighbors or
334 * own secondary interface packets
335 * (= secondary interface packets in general) */
336 (batman_ogm_packet->flags & DIRECTLINK ||
338 forw_packet->if_incoming != primary_if))) {
346 hardif_free_ref(primary_if);
350 /* create a new aggregated packet and add this packet to it */
351 static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
352 int packet_len, unsigned long send_time,
354 struct hard_iface *if_incoming,
357 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
358 struct forw_packet *forw_packet_aggr;
359 unsigned char *skb_buff;
361 if (!atomic_inc_not_zero(&if_incoming->refcount))
364 /* own packet should always be scheduled */
366 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
367 bat_dbg(DBG_BATMAN, bat_priv,
368 "batman packet queue full\n");
373 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
374 if (!forw_packet_aggr) {
376 atomic_inc(&bat_priv->batman_queue_left);
380 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
381 (packet_len < MAX_AGGREGATION_BYTES))
382 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
385 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
387 if (!forw_packet_aggr->skb) {
389 atomic_inc(&bat_priv->batman_queue_left);
390 kfree(forw_packet_aggr);
393 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
395 INIT_HLIST_NODE(&forw_packet_aggr->list);
397 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
398 forw_packet_aggr->packet_len = packet_len;
399 memcpy(skb_buff, packet_buff, packet_len);
401 forw_packet_aggr->own = own_packet;
402 forw_packet_aggr->if_incoming = if_incoming;
403 forw_packet_aggr->num_packets = 0;
404 forw_packet_aggr->direct_link_flags = NO_FLAGS;
405 forw_packet_aggr->send_time = send_time;
407 /* save packet direct link flag status */
409 forw_packet_aggr->direct_link_flags |= 1;
411 /* add new packet to packet list */
412 spin_lock_bh(&bat_priv->forw_bat_list_lock);
413 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
414 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
416 /* start timer for this packet */
417 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
418 send_outstanding_bat_ogm_packet);
419 queue_delayed_work(bat_event_workqueue,
420 &forw_packet_aggr->delayed_work,
421 send_time - jiffies);
425 hardif_free_ref(if_incoming);
428 /* aggregate a new packet into the existing ogm packet */
429 static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
430 const unsigned char *packet_buff,
431 int packet_len, bool direct_link)
433 unsigned char *skb_buff;
435 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
436 memcpy(skb_buff, packet_buff, packet_len);
437 forw_packet_aggr->packet_len += packet_len;
438 forw_packet_aggr->num_packets++;
440 /* save packet direct link flag status */
442 forw_packet_aggr->direct_link_flags |=
443 (1 << forw_packet_aggr->num_packets);
446 static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
447 unsigned char *packet_buff,
448 int packet_len, struct hard_iface *if_incoming,
449 int own_packet, unsigned long send_time)
452 * _aggr -> pointer to the packet we want to aggregate with
453 * _pos -> pointer to the position in the queue
455 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
456 struct hlist_node *tmp_node;
457 struct batman_ogm_packet *batman_ogm_packet;
460 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
461 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
463 /* find position for the packet in the forward queue */
464 spin_lock_bh(&bat_priv->forw_bat_list_lock);
465 /* own packets are not to be aggregated */
466 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
467 hlist_for_each_entry(forw_packet_pos, tmp_node,
468 &bat_priv->forw_bat_list, list) {
469 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
470 bat_priv, packet_len,
471 send_time, direct_link,
474 forw_packet_aggr = forw_packet_pos;
480 /* nothing to aggregate with - either aggregation disabled or no
481 * suitable aggregation packet found */
482 if (!forw_packet_aggr) {
483 /* the following section can run without the lock */
484 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
487 * if we could not aggregate this packet with one of the others
488 * we hold it back for a while, so that it might be aggregated
492 (atomic_read(&bat_priv->aggregated_ogms)))
493 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
495 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
496 send_time, direct_link,
497 if_incoming, own_packet);
499 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
500 packet_len, direct_link);
501 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
505 static void bat_iv_ogm_forward(struct orig_node *orig_node,
506 const struct ethhdr *ethhdr,
507 struct batman_ogm_packet *batman_ogm_packet,
508 bool is_single_hop_neigh,
509 bool is_from_best_next_hop,
510 struct hard_iface *if_incoming)
512 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
513 uint8_t tt_num_changes;
515 if (batman_ogm_packet->header.ttl <= 1) {
516 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
520 if (!is_from_best_next_hop) {
521 /* Mark the forwarded packet when it is not coming from our
522 * best next hop. We still need to forward the packet for our
523 * neighbor link quality detection to work in case the packet
524 * originated from a single hop neighbor. Otherwise we can
525 * simply drop the ogm.
527 if (is_single_hop_neigh)
528 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
533 tt_num_changes = batman_ogm_packet->tt_num_changes;
535 batman_ogm_packet->header.ttl--;
536 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
538 /* apply hop penalty */
539 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
541 bat_dbg(DBG_BATMAN, bat_priv,
542 "Forwarding packet: tq: %i, ttl: %i\n",
543 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
545 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
546 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
548 /* switch of primaries first hop flag when forwarding */
549 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
550 if (is_single_hop_neigh)
551 batman_ogm_packet->flags |= DIRECTLINK;
553 batman_ogm_packet->flags &= ~DIRECTLINK;
555 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
556 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
557 if_incoming, 0, bat_iv_ogm_fwd_send_time());
560 static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
563 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
564 struct batman_ogm_packet *batman_ogm_packet;
565 struct hard_iface *primary_if;
568 vis_server = atomic_read(&bat_priv->vis_mode);
569 primary_if = primary_if_get_selected(bat_priv);
571 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
573 /* change sequence number to network order */
574 batman_ogm_packet->seqno =
575 htonl((uint32_t)atomic_read(&hard_iface->seqno));
577 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
578 batman_ogm_packet->tt_crc = htons((uint16_t)
579 atomic_read(&bat_priv->tt_crc));
580 if (tt_num_changes >= 0)
581 batman_ogm_packet->tt_num_changes = tt_num_changes;
583 if (vis_server == VIS_TYPE_SERVER_SYNC)
584 batman_ogm_packet->flags |= VIS_SERVER;
586 batman_ogm_packet->flags &= ~VIS_SERVER;
588 if ((hard_iface == primary_if) &&
589 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
590 batman_ogm_packet->gw_flags =
591 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
593 batman_ogm_packet->gw_flags = NO_FLAGS;
595 atomic_inc(&hard_iface->seqno);
597 slide_own_bcast_window(hard_iface);
598 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
599 hard_iface->packet_len, hard_iface, 1,
600 bat_iv_ogm_emit_send_time(bat_priv));
603 hardif_free_ref(primary_if);
606 static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
607 struct orig_node *orig_node,
608 const struct ethhdr *ethhdr,
609 const struct batman_ogm_packet
611 struct hard_iface *if_incoming,
612 const unsigned char *tt_buff,
615 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
616 struct neigh_node *router = NULL;
617 struct orig_node *orig_node_tmp;
618 struct hlist_node *node;
619 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
621 bat_dbg(DBG_BATMAN, bat_priv,
622 "update_originator(): Searching and updating originator entry of received packet\n");
625 hlist_for_each_entry_rcu(tmp_neigh_node, node,
626 &orig_node->neigh_list, list) {
627 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
628 (tmp_neigh_node->if_incoming == if_incoming) &&
629 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
631 neigh_node_free_ref(neigh_node);
632 neigh_node = tmp_neigh_node;
639 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
640 ring_buffer_set(tmp_neigh_node->tq_recv,
641 &tmp_neigh_node->tq_index, 0);
642 tmp_neigh_node->tq_avg =
643 ring_buffer_avg(tmp_neigh_node->tq_recv);
644 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
648 struct orig_node *orig_tmp;
650 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
654 neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source,
656 batman_ogm_packet->seqno);
658 orig_node_free_ref(orig_tmp);
662 bat_dbg(DBG_BATMAN, bat_priv,
663 "Updating existing last-hop neighbor of originator\n");
667 orig_node->flags = batman_ogm_packet->flags;
668 neigh_node->last_seen = jiffies;
670 spin_lock_bh(&neigh_node->lq_update_lock);
671 ring_buffer_set(neigh_node->tq_recv,
672 &neigh_node->tq_index,
673 batman_ogm_packet->tq);
674 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
675 spin_unlock_bh(&neigh_node->lq_update_lock);
678 orig_node->last_ttl = batman_ogm_packet->header.ttl;
679 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
682 bonding_candidate_add(orig_node, neigh_node);
684 /* if this neighbor already is our next hop there is nothing
686 router = orig_node_get_router(orig_node);
687 if (router == neigh_node)
690 /* if this neighbor does not offer a better TQ we won't consider it */
691 if (router && (router->tq_avg > neigh_node->tq_avg))
694 /* if the TQ is the same and the link not more symmetric we
695 * won't consider it either */
696 if (router && (neigh_node->tq_avg == router->tq_avg)) {
697 orig_node_tmp = router->orig_node;
698 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
700 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
701 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
703 orig_node_tmp = neigh_node->orig_node;
704 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
705 bcast_own_sum_neigh =
706 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
707 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
709 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
713 update_route(bat_priv, orig_node, neigh_node);
716 /* I have to check for transtable changes only if the OGM has been
717 * sent through a primary interface */
718 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
719 (batman_ogm_packet->header.ttl > 2)) ||
720 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
721 tt_update_orig(bat_priv, orig_node, tt_buff,
722 batman_ogm_packet->tt_num_changes,
723 batman_ogm_packet->ttvn,
724 batman_ogm_packet->tt_crc);
726 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
727 gw_node_update(bat_priv, orig_node,
728 batman_ogm_packet->gw_flags);
730 orig_node->gw_flags = batman_ogm_packet->gw_flags;
732 /* restart gateway selection if fast or late switching was enabled */
733 if ((orig_node->gw_flags) &&
734 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
735 (atomic_read(&bat_priv->gw_sel_class) > 2))
736 gw_check_election(bat_priv, orig_node);
744 neigh_node_free_ref(neigh_node);
746 neigh_node_free_ref(router);
749 static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
750 struct orig_node *orig_neigh_node,
751 struct batman_ogm_packet *batman_ogm_packet,
752 struct hard_iface *if_incoming)
754 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
755 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
756 struct hlist_node *node;
758 uint8_t orig_eq_count, neigh_rq_count, tq_own;
759 int tq_asym_penalty, ret = 0;
761 /* find corresponding one hop neighbor */
763 hlist_for_each_entry_rcu(tmp_neigh_node, node,
764 &orig_neigh_node->neigh_list, list) {
766 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
769 if (tmp_neigh_node->if_incoming != if_incoming)
772 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
775 neigh_node = tmp_neigh_node;
781 neigh_node = bat_iv_ogm_neigh_new(if_incoming,
782 orig_neigh_node->orig,
785 batman_ogm_packet->seqno);
790 /* if orig_node is direct neighbor update neigh_node last_seen */
791 if (orig_node == orig_neigh_node)
792 neigh_node->last_seen = jiffies;
794 orig_node->last_seen = jiffies;
796 /* find packet count of corresponding one hop neighbor */
797 spin_lock_bh(&orig_node->ogm_cnt_lock);
798 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
799 neigh_rq_count = neigh_node->real_packet_count;
800 spin_unlock_bh(&orig_node->ogm_cnt_lock);
802 /* pay attention to not get a value bigger than 100 % */
803 total_count = (orig_eq_count > neigh_rq_count ?
804 neigh_rq_count : orig_eq_count);
806 /* if we have too few packets (too less data) we set tq_own to zero */
807 /* if we receive too few packets it is not considered bidirectional */
808 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
809 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
812 /* neigh_node->real_packet_count is never zero as we
813 * only purge old information when getting new
815 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
817 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
818 * affect the nearly-symmetric links only a little, but
819 * punishes asymmetric links more. This will give a value
820 * between 0 and TQ_MAX_VALUE
822 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
823 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
824 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
825 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
826 (TQ_LOCAL_WINDOW_SIZE *
827 TQ_LOCAL_WINDOW_SIZE *
828 TQ_LOCAL_WINDOW_SIZE);
830 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
832 (TQ_MAX_VALUE * TQ_MAX_VALUE));
834 bat_dbg(DBG_BATMAN, bat_priv,
835 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
836 orig_node->orig, orig_neigh_node->orig, total_count,
837 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
839 /* if link has the minimum required transmission quality
840 * consider it bidirectional */
841 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
846 neigh_node_free_ref(neigh_node);
850 /* processes a batman packet for all interfaces, adjusts the sequence number and
851 * finds out whether it is a duplicate.
853 * 1 the packet is a duplicate
854 * 0 the packet has not yet been received
855 * -1 the packet is old and has been received while the seqno window
856 * was protected. Caller should drop it.
858 static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
859 const struct batman_ogm_packet
861 const struct hard_iface *if_incoming)
863 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
864 struct orig_node *orig_node;
865 struct neigh_node *tmp_neigh_node;
866 struct hlist_node *node;
867 int is_duplicate = 0;
870 int set_mark, ret = -1;
872 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
876 spin_lock_bh(&orig_node->ogm_cnt_lock);
877 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
879 /* signalize caller that the packet is to be dropped. */
880 if (!hlist_empty(&orig_node->neigh_list) &&
881 window_protected(bat_priv, seq_diff,
882 &orig_node->batman_seqno_reset))
886 hlist_for_each_entry_rcu(tmp_neigh_node, node,
887 &orig_node->neigh_list, list) {
889 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
890 orig_node->last_real_seqno,
891 batman_ogm_packet->seqno);
893 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
894 (tmp_neigh_node->if_incoming == if_incoming))
899 /* if the window moved, set the update flag. */
900 need_update |= bit_get_packet(bat_priv,
901 tmp_neigh_node->real_bits,
904 tmp_neigh_node->real_packet_count =
905 bitmap_weight(tmp_neigh_node->real_bits,
906 TQ_LOCAL_WINDOW_SIZE);
911 bat_dbg(DBG_BATMAN, bat_priv,
912 "updating last_seqno: old %u, new %u\n",
913 orig_node->last_real_seqno, batman_ogm_packet->seqno);
914 orig_node->last_real_seqno = batman_ogm_packet->seqno;
920 spin_unlock_bh(&orig_node->ogm_cnt_lock);
921 orig_node_free_ref(orig_node);
925 static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
926 struct batman_ogm_packet *batman_ogm_packet,
927 const unsigned char *tt_buff,
928 struct hard_iface *if_incoming)
930 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
931 struct hard_iface *hard_iface;
932 struct orig_node *orig_neigh_node, *orig_node;
933 struct neigh_node *router = NULL, *router_router = NULL;
934 struct neigh_node *orig_neigh_router = NULL;
935 int has_directlink_flag;
936 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
937 int is_broadcast = 0, is_bidirectional;
938 bool is_single_hop_neigh = false;
939 bool is_from_best_next_hop = false;
941 uint32_t if_incoming_seqno;
943 /* Silently drop when the batman packet is actually not a
946 * This might happen if a packet is padded (e.g. Ethernet has a
947 * minimum frame length of 64 byte) and the aggregation interprets
948 * it as an additional length.
950 * TODO: A more sane solution would be to have a bit in the
951 * batman_ogm_packet to detect whether the packet is the last
952 * packet in an aggregation. Here we expect that the padding
953 * is always zero (or not 0x01)
955 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
958 /* could be changed by schedule_own_packet() */
959 if_incoming_seqno = atomic_read(&if_incoming->seqno);
961 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
963 if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
964 is_single_hop_neigh = true;
966 bat_dbg(DBG_BATMAN, bat_priv,
967 "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",
968 ethhdr->h_source, if_incoming->net_dev->name,
969 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
970 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
971 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
972 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
973 batman_ogm_packet->header.ttl,
974 batman_ogm_packet->header.version, has_directlink_flag);
977 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
978 if (hard_iface->if_status != IF_ACTIVE)
981 if (hard_iface->soft_iface != if_incoming->soft_iface)
984 if (compare_eth(ethhdr->h_source,
985 hard_iface->net_dev->dev_addr))
988 if (compare_eth(batman_ogm_packet->orig,
989 hard_iface->net_dev->dev_addr))
992 if (compare_eth(batman_ogm_packet->prev_sender,
993 hard_iface->net_dev->dev_addr))
996 if (is_broadcast_ether_addr(ethhdr->h_source))
1001 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
1002 bat_dbg(DBG_BATMAN, bat_priv,
1003 "Drop packet: incompatible batman version (%i)\n",
1004 batman_ogm_packet->header.version);
1009 bat_dbg(DBG_BATMAN, bat_priv,
1010 "Drop packet: received my own broadcast (sender: %pM)\n",
1016 bat_dbg(DBG_BATMAN, bat_priv,
1017 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1023 unsigned long *word;
1026 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1027 if (!orig_neigh_node)
1030 /* neighbor has to indicate direct link and it has to
1031 * come via the corresponding interface */
1032 /* save packet seqno for bidirectional check */
1033 if (has_directlink_flag &&
1034 compare_eth(if_incoming->net_dev->dev_addr,
1035 batman_ogm_packet->orig)) {
1036 offset = if_incoming->if_num * NUM_WORDS;
1038 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1039 word = &(orig_neigh_node->bcast_own[offset]);
1042 batman_ogm_packet->seqno - 2);
1043 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
1044 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
1045 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1048 bat_dbg(DBG_BATMAN, bat_priv,
1049 "Drop packet: originator packet from myself (via neighbor)\n");
1050 orig_node_free_ref(orig_neigh_node);
1054 if (is_my_oldorig) {
1055 bat_dbg(DBG_BATMAN, bat_priv,
1056 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1061 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1062 bat_dbg(DBG_BATMAN, bat_priv,
1063 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1068 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1072 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1075 if (is_duplicate == -1) {
1076 bat_dbg(DBG_BATMAN, bat_priv,
1077 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1082 if (batman_ogm_packet->tq == 0) {
1083 bat_dbg(DBG_BATMAN, bat_priv,
1084 "Drop packet: originator packet with tq equal 0\n");
1088 router = orig_node_get_router(orig_node);
1090 router_router = orig_node_get_router(router->orig_node);
1092 if ((router && router->tq_avg != 0) &&
1093 (compare_eth(router->addr, ethhdr->h_source)))
1094 is_from_best_next_hop = true;
1096 /* avoid temporary routing loops */
1097 if (router && router_router &&
1098 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1099 !(compare_eth(batman_ogm_packet->orig,
1100 batman_ogm_packet->prev_sender)) &&
1101 (compare_eth(router->addr, router_router->addr))) {
1102 bat_dbg(DBG_BATMAN, bat_priv,
1103 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1108 /* if sender is a direct neighbor the sender mac equals
1110 orig_neigh_node = (is_single_hop_neigh ?
1112 get_orig_node(bat_priv, ethhdr->h_source));
1113 if (!orig_neigh_node)
1116 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1118 /* drop packet if sender is not a direct neighbor and if we
1119 * don't route towards it */
1120 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1121 bat_dbg(DBG_BATMAN, bat_priv,
1122 "Drop packet: OGM via unknown neighbor!\n");
1126 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1127 batman_ogm_packet, if_incoming);
1129 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1131 /* update ranking if it is not a duplicate or has the same
1132 * seqno and similar ttl as the non-duplicate */
1133 if (is_bidirectional &&
1135 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
1136 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
1137 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1138 batman_ogm_packet, if_incoming,
1139 tt_buff, is_duplicate);
1141 /* is single hop (direct) neighbor */
1142 if (is_single_hop_neigh) {
1144 /* mark direct link on incoming interface */
1145 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1146 is_single_hop_neigh, is_from_best_next_hop,
1149 bat_dbg(DBG_BATMAN, bat_priv,
1150 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1154 /* multihop originator */
1155 if (!is_bidirectional) {
1156 bat_dbg(DBG_BATMAN, bat_priv,
1157 "Drop packet: not received via bidirectional link\n");
1162 bat_dbg(DBG_BATMAN, bat_priv,
1163 "Drop packet: duplicate packet received\n");
1167 bat_dbg(DBG_BATMAN, bat_priv,
1168 "Forwarding packet: rebroadcast originator packet\n");
1169 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1170 is_single_hop_neigh, is_from_best_next_hop,
1174 if ((orig_neigh_node) && (!is_single_hop_neigh))
1175 orig_node_free_ref(orig_neigh_node);
1178 neigh_node_free_ref(router);
1180 neigh_node_free_ref(router_router);
1181 if (orig_neigh_router)
1182 neigh_node_free_ref(orig_neigh_router);
1184 orig_node_free_ref(orig_node);
1187 static int bat_iv_ogm_receive(struct sk_buff *skb,
1188 struct hard_iface *if_incoming)
1190 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1191 struct batman_ogm_packet *batman_ogm_packet;
1192 struct ethhdr *ethhdr;
1193 int buff_pos = 0, packet_len;
1194 unsigned char *tt_buff, *packet_buff;
1197 ret = check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
1201 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1202 * that does not have B.A.T.M.A.N. IV enabled ?
1204 if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit)
1207 packet_len = skb_headlen(skb);
1208 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1209 packet_buff = skb->data;
1210 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1212 /* unpack the aggregated packets and process them one by one */
1214 /* network to host order for our 32bit seqno and the
1216 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1217 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1219 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
1221 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1222 tt_buff, if_incoming);
1224 buff_pos += BATMAN_OGM_HLEN +
1225 tt_len(batman_ogm_packet->tt_num_changes);
1227 batman_ogm_packet = (struct batman_ogm_packet *)
1228 (packet_buff + buff_pos);
1229 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1230 batman_ogm_packet->tt_num_changes));
1233 return NET_RX_SUCCESS;
1236 static struct bat_algo_ops batman_iv __read_mostly = {
1237 .name = "BATMAN IV",
1238 .bat_iface_enable = bat_iv_ogm_iface_enable,
1239 .bat_iface_disable = bat_iv_ogm_iface_disable,
1240 .bat_iface_update_mac = bat_iv_ogm_iface_update_mac,
1241 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
1242 .bat_ogm_schedule = bat_iv_ogm_schedule,
1243 .bat_ogm_emit = bat_iv_ogm_emit,
1246 int __init bat_iv_init(void)
1250 /* batman originator packet */
1251 ret = recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive);
1255 ret = bat_algo_register(&batman_iv);
1257 goto handler_unregister;
1262 recv_handler_unregister(BAT_IV_OGM);