1 /* Copyright (C) 2007-2012 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
21 #include "translation-table.h"
22 #include "ring_buffer.h"
23 #include "originator.h"
25 #include "gateway_common.h"
26 #include "gateway_client.h"
27 #include "hard-interface.h"
31 static struct neigh_node *batadv_iv_ogm_neigh_new(struct hard_iface *hard_iface,
32 const uint8_t *neigh_addr,
33 struct orig_node *orig_node,
34 struct orig_node *orig_neigh,
37 struct neigh_node *neigh_node;
39 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr,
44 INIT_LIST_HEAD(&neigh_node->bonding_list);
46 neigh_node->orig_node = orig_neigh;
47 neigh_node->if_incoming = hard_iface;
49 spin_lock_bh(&orig_node->neigh_list_lock);
50 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
51 spin_unlock_bh(&orig_node->neigh_list_lock);
57 static int batadv_iv_ogm_iface_enable(struct hard_iface *hard_iface)
59 struct batman_ogm_packet *batman_ogm_packet;
60 uint32_t random_seqno;
63 /* randomize initial seqno to avoid collision */
64 get_random_bytes(&random_seqno, sizeof(random_seqno));
65 atomic_set(&hard_iface->seqno, random_seqno);
67 hard_iface->packet_len = BATMAN_OGM_HLEN;
68 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
70 if (!hard_iface->packet_buff)
73 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
74 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
75 batman_ogm_packet->header.version = COMPAT_VERSION;
76 batman_ogm_packet->header.ttl = 2;
77 batman_ogm_packet->flags = NO_FLAGS;
78 batman_ogm_packet->tq = TQ_MAX_VALUE;
79 batman_ogm_packet->tt_num_changes = 0;
80 batman_ogm_packet->ttvn = 0;
88 static void batadv_iv_ogm_iface_disable(struct hard_iface *hard_iface)
90 kfree(hard_iface->packet_buff);
91 hard_iface->packet_buff = NULL;
94 static void batadv_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
96 struct batman_ogm_packet *batman_ogm_packet;
98 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
99 memcpy(batman_ogm_packet->orig,
100 hard_iface->net_dev->dev_addr, ETH_ALEN);
101 memcpy(batman_ogm_packet->prev_sender,
102 hard_iface->net_dev->dev_addr, ETH_ALEN);
105 static void batadv_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
107 struct batman_ogm_packet *batman_ogm_packet;
109 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
110 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
111 batman_ogm_packet->header.ttl = TTL;
114 /* when do we schedule our own ogm to be sent */
116 batadv_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 batadv_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 batadv_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 batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
140 int next_buff_pos = 0;
142 next_buff_pos += buff_pos + BATMAN_OGM_HLEN;
143 next_buff_pos += batadv_tt_len(tt_num_changes);
145 return (next_buff_pos <= packet_len) &&
146 (next_buff_pos <= MAX_AGGREGATION_BYTES);
149 /* send a batman ogm to a given interface */
150 static void batadv_iv_ogm_send_to_if(struct forw_packet *forw_packet,
151 struct hard_iface *hard_iface)
153 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
157 struct batman_ogm_packet *batman_ogm_packet;
160 if (hard_iface->if_status != IF_ACTIVE)
165 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
167 /* adjust all flags and log packets */
168 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
169 batman_ogm_packet->tt_num_changes)) {
171 /* we might have aggregated direct link packets with an
172 * ordinary base packet
174 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
175 (forw_packet->if_incoming == hard_iface))
176 batman_ogm_packet->flags |= DIRECTLINK;
178 batman_ogm_packet->flags &= ~DIRECTLINK;
180 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
183 batadv_dbg(DBG_BATMAN, bat_priv,
184 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
185 fwd_str, (packet_num > 0 ? "aggregated " : ""),
186 batman_ogm_packet->orig,
187 ntohl(batman_ogm_packet->seqno),
188 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
189 (batman_ogm_packet->flags & DIRECTLINK ?
191 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
192 hard_iface->net_dev->dev_addr);
194 buff_pos += BATMAN_OGM_HLEN;
195 buff_pos += batadv_tt_len(batman_ogm_packet->tt_num_changes);
197 batman_ogm_packet = (struct batman_ogm_packet *)
198 (forw_packet->skb->data + buff_pos);
201 /* create clone because function is called more than once */
202 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
204 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_TX);
205 batadv_add_counter(bat_priv, BAT_CNT_MGMT_TX_BYTES,
206 skb->len + ETH_HLEN);
207 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
211 /* send a batman ogm packet */
212 static void batadv_iv_ogm_emit(struct forw_packet *forw_packet)
214 struct hard_iface *hard_iface;
215 struct net_device *soft_iface;
216 struct bat_priv *bat_priv;
217 struct hard_iface *primary_if = NULL;
218 struct batman_ogm_packet *batman_ogm_packet;
219 unsigned char directlink;
221 batman_ogm_packet = (struct batman_ogm_packet *)
222 (forw_packet->skb->data);
223 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
225 if (!forw_packet->if_incoming) {
226 pr_err("Error - can't forward packet: incoming iface not specified\n");
230 soft_iface = forw_packet->if_incoming->soft_iface;
231 bat_priv = netdev_priv(soft_iface);
233 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
236 primary_if = batadv_primary_if_get_selected(bat_priv);
240 /* multihomed peer assumed
241 * non-primary OGMs are only broadcasted on their interface
243 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
244 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
246 /* FIXME: what about aggregated packets ? */
247 batadv_dbg(DBG_BATMAN, bat_priv,
248 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
249 (forw_packet->own ? "Sending own" : "Forwarding"),
250 batman_ogm_packet->orig,
251 ntohl(batman_ogm_packet->seqno),
252 batman_ogm_packet->header.ttl,
253 forw_packet->if_incoming->net_dev->name,
254 forw_packet->if_incoming->net_dev->dev_addr);
256 /* skb is only used once and than forw_packet is free'd */
257 batadv_send_skb_packet(forw_packet->skb,
258 forw_packet->if_incoming,
259 batadv_broadcast_addr);
260 forw_packet->skb = NULL;
265 /* broadcast on every interface */
267 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
268 if (hard_iface->soft_iface != soft_iface)
271 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
277 batadv_hardif_free_ref(primary_if);
280 /* return true if new_packet can be aggregated with forw_packet */
282 batadv_iv_ogm_can_aggregate(const struct batman_ogm_packet *new_bat_ogm_packet,
283 struct bat_priv *bat_priv,
284 int packet_len, unsigned long send_time,
286 const struct hard_iface *if_incoming,
287 const struct forw_packet *forw_packet)
289 struct batman_ogm_packet *batman_ogm_packet;
290 int aggregated_bytes = forw_packet->packet_len + packet_len;
291 struct hard_iface *primary_if = NULL;
294 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
296 /* we can aggregate the current packet to this aggregated packet
299 * - the send time is within our MAX_AGGREGATION_MS time
300 * - the resulting packet wont be bigger than
301 * MAX_AGGREGATION_BYTES
303 if (time_before(send_time, forw_packet->send_time) &&
304 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
305 forw_packet->send_time) &&
306 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
308 /* check aggregation compatibility
309 * -> direct link packets are broadcasted on
310 * their interface only
311 * -> aggregate packet if the current packet is
312 * a "global" packet as well as the base
315 primary_if = batadv_primary_if_get_selected(bat_priv);
319 /* packets without direct link flag and high TTL
320 * are flooded through the net
323 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
324 (batman_ogm_packet->header.ttl != 1) &&
326 /* own packets originating non-primary
327 * interfaces leave only that interface
329 ((!forw_packet->own) ||
330 (forw_packet->if_incoming == primary_if))) {
335 /* if the incoming packet is sent via this one
336 * interface only - we still can aggregate
339 (new_bat_ogm_packet->header.ttl == 1) &&
340 (forw_packet->if_incoming == if_incoming) &&
342 /* packets from direct neighbors or
343 * own secondary interface packets
344 * (= secondary interface packets in general)
346 (batman_ogm_packet->flags & DIRECTLINK ||
348 forw_packet->if_incoming != primary_if))) {
356 batadv_hardif_free_ref(primary_if);
360 /* create a new aggregated packet and add this packet to it */
361 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
362 int packet_len, unsigned long send_time,
364 struct hard_iface *if_incoming,
367 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
368 struct forw_packet *forw_packet_aggr;
369 unsigned char *skb_buff;
371 if (!atomic_inc_not_zero(&if_incoming->refcount))
374 /* own packet should always be scheduled */
376 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
377 batadv_dbg(DBG_BATMAN, bat_priv,
378 "batman packet queue full\n");
383 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
384 if (!forw_packet_aggr) {
386 atomic_inc(&bat_priv->batman_queue_left);
390 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
391 (packet_len < MAX_AGGREGATION_BYTES))
392 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
395 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
397 if (!forw_packet_aggr->skb) {
399 atomic_inc(&bat_priv->batman_queue_left);
400 kfree(forw_packet_aggr);
403 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
405 INIT_HLIST_NODE(&forw_packet_aggr->list);
407 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
408 forw_packet_aggr->packet_len = packet_len;
409 memcpy(skb_buff, packet_buff, packet_len);
411 forw_packet_aggr->own = own_packet;
412 forw_packet_aggr->if_incoming = if_incoming;
413 forw_packet_aggr->num_packets = 0;
414 forw_packet_aggr->direct_link_flags = NO_FLAGS;
415 forw_packet_aggr->send_time = send_time;
417 /* save packet direct link flag status */
419 forw_packet_aggr->direct_link_flags |= 1;
421 /* add new packet to packet list */
422 spin_lock_bh(&bat_priv->forw_bat_list_lock);
423 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
424 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
426 /* start timer for this packet */
427 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
428 batadv_send_outstanding_bat_ogm_packet);
429 queue_delayed_work(batadv_event_workqueue,
430 &forw_packet_aggr->delayed_work,
431 send_time - jiffies);
435 batadv_hardif_free_ref(if_incoming);
438 /* aggregate a new packet into the existing ogm packet */
439 static void batadv_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
440 const unsigned char *packet_buff,
441 int packet_len, bool direct_link)
443 unsigned char *skb_buff;
445 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
446 memcpy(skb_buff, packet_buff, packet_len);
447 forw_packet_aggr->packet_len += packet_len;
448 forw_packet_aggr->num_packets++;
450 /* save packet direct link flag status */
452 forw_packet_aggr->direct_link_flags |=
453 (1 << forw_packet_aggr->num_packets);
456 static void batadv_iv_ogm_queue_add(struct bat_priv *bat_priv,
457 unsigned char *packet_buff,
459 struct hard_iface *if_incoming,
460 int own_packet, unsigned long send_time)
462 /* _aggr -> pointer to the packet we want to aggregate with
463 * _pos -> pointer to the position in the queue
465 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
466 struct hlist_node *tmp_node;
467 struct batman_ogm_packet *batman_ogm_packet;
470 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
471 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
473 /* find position for the packet in the forward queue */
474 spin_lock_bh(&bat_priv->forw_bat_list_lock);
475 /* own packets are not to be aggregated */
476 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
477 hlist_for_each_entry(forw_packet_pos, tmp_node,
478 &bat_priv->forw_bat_list, list) {
479 if (batadv_iv_ogm_can_aggregate(batman_ogm_packet,
480 bat_priv, packet_len,
481 send_time, direct_link,
484 forw_packet_aggr = forw_packet_pos;
490 /* nothing to aggregate with - either aggregation disabled or no
491 * suitable aggregation packet found
493 if (!forw_packet_aggr) {
494 /* the following section can run without the lock */
495 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
497 /* if we could not aggregate this packet with one of the others
498 * we hold it back for a while, so that it might be aggregated
502 (atomic_read(&bat_priv->aggregated_ogms)))
503 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
505 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
506 send_time, direct_link,
507 if_incoming, own_packet);
509 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
510 packet_len, direct_link);
511 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
515 static void batadv_iv_ogm_forward(struct orig_node *orig_node,
516 const struct ethhdr *ethhdr,
517 struct batman_ogm_packet *batman_ogm_packet,
518 bool is_single_hop_neigh,
519 bool is_from_best_next_hop,
520 struct hard_iface *if_incoming)
522 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
523 uint8_t tt_num_changes;
525 if (batman_ogm_packet->header.ttl <= 1) {
526 batadv_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
530 if (!is_from_best_next_hop) {
531 /* Mark the forwarded packet when it is not coming from our
532 * best next hop. We still need to forward the packet for our
533 * neighbor link quality detection to work in case the packet
534 * originated from a single hop neighbor. Otherwise we can
535 * simply drop the ogm.
537 if (is_single_hop_neigh)
538 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
543 tt_num_changes = batman_ogm_packet->tt_num_changes;
545 batman_ogm_packet->header.ttl--;
546 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
548 /* apply hop penalty */
549 batman_ogm_packet->tq = batadv_hop_penalty(batman_ogm_packet->tq,
552 batadv_dbg(DBG_BATMAN, bat_priv,
553 "Forwarding packet: tq: %i, ttl: %i\n",
554 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
556 /* switch of primaries first hop flag when forwarding */
557 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
558 if (is_single_hop_neigh)
559 batman_ogm_packet->flags |= DIRECTLINK;
561 batman_ogm_packet->flags &= ~DIRECTLINK;
563 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
564 BATMAN_OGM_HLEN + batadv_tt_len(tt_num_changes),
565 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
568 static void batadv_iv_ogm_schedule(struct hard_iface *hard_iface)
570 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
571 struct batman_ogm_packet *batman_ogm_packet;
572 struct hard_iface *primary_if;
573 int vis_server, tt_num_changes = 0;
575 vis_server = atomic_read(&bat_priv->vis_mode);
576 primary_if = batadv_primary_if_get_selected(bat_priv);
578 if (hard_iface == primary_if)
579 tt_num_changes = batadv_tt_append_diff(bat_priv,
580 &hard_iface->packet_buff,
581 &hard_iface->packet_len,
584 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
586 /* change sequence number to network order */
587 batman_ogm_packet->seqno =
588 htonl((uint32_t)atomic_read(&hard_iface->seqno));
589 atomic_inc(&hard_iface->seqno);
591 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
592 batman_ogm_packet->tt_crc = htons(bat_priv->tt_crc);
593 if (tt_num_changes >= 0)
594 batman_ogm_packet->tt_num_changes = tt_num_changes;
596 if (vis_server == VIS_TYPE_SERVER_SYNC)
597 batman_ogm_packet->flags |= VIS_SERVER;
599 batman_ogm_packet->flags &= ~VIS_SERVER;
601 if ((hard_iface == primary_if) &&
602 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
603 batman_ogm_packet->gw_flags =
604 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
606 batman_ogm_packet->gw_flags = NO_FLAGS;
608 batadv_slide_own_bcast_window(hard_iface);
609 batadv_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
610 hard_iface->packet_len, hard_iface, 1,
611 batadv_iv_ogm_emit_send_time(bat_priv));
614 batadv_hardif_free_ref(primary_if);
618 batadv_iv_ogm_orig_update(struct bat_priv *bat_priv,
619 struct orig_node *orig_node,
620 const struct ethhdr *ethhdr,
621 const struct batman_ogm_packet *batman_ogm_packet,
622 struct hard_iface *if_incoming,
623 const unsigned char *tt_buff,
626 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
627 struct neigh_node *router = NULL;
628 struct orig_node *orig_node_tmp;
629 struct hlist_node *node;
630 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
633 batadv_dbg(DBG_BATMAN, bat_priv,
634 "update_originator(): Searching and updating originator entry of received packet\n");
637 hlist_for_each_entry_rcu(tmp_neigh_node, node,
638 &orig_node->neigh_list, list) {
639 neigh_addr = tmp_neigh_node->addr;
640 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
641 tmp_neigh_node->if_incoming == if_incoming &&
642 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
644 batadv_neigh_node_free_ref(neigh_node);
645 neigh_node = tmp_neigh_node;
652 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
653 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
654 &tmp_neigh_node->tq_index, 0);
655 tmp_neigh_node->tq_avg =
656 batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
657 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
661 struct orig_node *orig_tmp;
663 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
667 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
670 batman_ogm_packet->seqno);
672 batadv_orig_node_free_ref(orig_tmp);
676 batadv_dbg(DBG_BATMAN, bat_priv,
677 "Updating existing last-hop neighbor of originator\n");
681 orig_node->flags = batman_ogm_packet->flags;
682 neigh_node->last_seen = jiffies;
684 spin_lock_bh(&neigh_node->lq_update_lock);
685 batadv_ring_buffer_set(neigh_node->tq_recv,
686 &neigh_node->tq_index,
687 batman_ogm_packet->tq);
688 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
689 spin_unlock_bh(&neigh_node->lq_update_lock);
692 orig_node->last_ttl = batman_ogm_packet->header.ttl;
693 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
696 batadv_bonding_candidate_add(orig_node, neigh_node);
698 /* if this neighbor already is our next hop there is nothing
701 router = batadv_orig_node_get_router(orig_node);
702 if (router == neigh_node)
705 /* if this neighbor does not offer a better TQ we won't consider it */
706 if (router && (router->tq_avg > neigh_node->tq_avg))
709 /* if the TQ is the same and the link not more symmetric we
710 * won't consider it either
712 if (router && (neigh_node->tq_avg == router->tq_avg)) {
713 orig_node_tmp = router->orig_node;
714 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
716 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
717 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
719 orig_node_tmp = neigh_node->orig_node;
720 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
721 bcast_own_sum_neigh =
722 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
723 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
725 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
729 batadv_update_route(bat_priv, orig_node, neigh_node);
732 /* I have to check for transtable changes only if the OGM has been
733 * sent through a primary interface
735 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
736 (batman_ogm_packet->header.ttl > 2)) ||
737 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
738 batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
739 batman_ogm_packet->tt_num_changes,
740 batman_ogm_packet->ttvn,
741 ntohs(batman_ogm_packet->tt_crc));
743 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
744 batadv_gw_node_update(bat_priv, orig_node,
745 batman_ogm_packet->gw_flags);
747 orig_node->gw_flags = batman_ogm_packet->gw_flags;
749 /* restart gateway selection if fast or late switching was enabled */
750 if ((orig_node->gw_flags) &&
751 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
752 (atomic_read(&bat_priv->gw_sel_class) > 2))
753 batadv_gw_check_election(bat_priv, orig_node);
761 batadv_neigh_node_free_ref(neigh_node);
763 batadv_neigh_node_free_ref(router);
766 static int batadv_iv_ogm_calc_tq(struct orig_node *orig_node,
767 struct orig_node *orig_neigh_node,
768 struct batman_ogm_packet *batman_ogm_packet,
769 struct hard_iface *if_incoming)
771 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
772 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
773 struct hlist_node *node;
775 uint8_t orig_eq_count, neigh_rq_count, tq_own;
776 int tq_asym_penalty, ret = 0;
778 /* find corresponding one hop neighbor */
780 hlist_for_each_entry_rcu(tmp_neigh_node, node,
781 &orig_neigh_node->neigh_list, list) {
783 if (!batadv_compare_eth(tmp_neigh_node->addr,
784 orig_neigh_node->orig))
787 if (tmp_neigh_node->if_incoming != if_incoming)
790 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
793 neigh_node = tmp_neigh_node;
799 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
800 orig_neigh_node->orig,
803 batman_ogm_packet->seqno);
808 /* if orig_node is direct neighbor update neigh_node last_seen */
809 if (orig_node == orig_neigh_node)
810 neigh_node->last_seen = jiffies;
812 orig_node->last_seen = jiffies;
814 /* find packet count of corresponding one hop neighbor */
815 spin_lock_bh(&orig_node->ogm_cnt_lock);
816 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
817 neigh_rq_count = neigh_node->real_packet_count;
818 spin_unlock_bh(&orig_node->ogm_cnt_lock);
820 /* pay attention to not get a value bigger than 100 % */
821 total_count = (orig_eq_count > neigh_rq_count ?
822 neigh_rq_count : orig_eq_count);
824 /* if we have too few packets (too less data) we set tq_own to zero
825 * if we receive too few packets it is not considered bidirectional
827 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
828 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
831 /* neigh_node->real_packet_count is never zero as we
832 * only purge old information when getting new
835 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
837 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
838 * affect the nearly-symmetric links only a little, but
839 * punishes asymmetric links more. This will give a value
840 * between 0 and TQ_MAX_VALUE
842 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
843 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
844 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
845 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
846 (TQ_LOCAL_WINDOW_SIZE *
847 TQ_LOCAL_WINDOW_SIZE *
848 TQ_LOCAL_WINDOW_SIZE);
850 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
852 (TQ_MAX_VALUE * TQ_MAX_VALUE));
854 batadv_dbg(DBG_BATMAN, bat_priv,
855 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
856 orig_node->orig, orig_neigh_node->orig, total_count,
857 neigh_rq_count, tq_own,
858 tq_asym_penalty, batman_ogm_packet->tq);
860 /* if link has the minimum required transmission quality
861 * consider it bidirectional
863 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
868 batadv_neigh_node_free_ref(neigh_node);
872 /* processes a batman packet for all interfaces, adjusts the sequence number and
873 * finds out whether it is a duplicate.
875 * 1 the packet is a duplicate
876 * 0 the packet has not yet been received
877 * -1 the packet is old and has been received while the seqno window
878 * was protected. Caller should drop it.
881 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
882 const struct batman_ogm_packet *batman_ogm_packet,
883 const struct hard_iface *if_incoming)
885 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
886 struct orig_node *orig_node;
887 struct neigh_node *tmp_neigh_node;
888 struct hlist_node *node;
889 int is_duplicate = 0;
892 int set_mark, ret = -1;
893 uint32_t seqno = ntohl(batman_ogm_packet->seqno);
896 orig_node = batadv_get_orig_node(bat_priv, batman_ogm_packet->orig);
900 spin_lock_bh(&orig_node->ogm_cnt_lock);
901 seq_diff = seqno - orig_node->last_real_seqno;
903 /* signalize caller that the packet is to be dropped. */
904 if (!hlist_empty(&orig_node->neigh_list) &&
905 batadv_window_protected(bat_priv, seq_diff,
906 &orig_node->batman_seqno_reset))
910 hlist_for_each_entry_rcu(tmp_neigh_node, node,
911 &orig_node->neigh_list, list) {
913 is_duplicate |= batadv_test_bit(tmp_neigh_node->real_bits,
914 orig_node->last_real_seqno,
917 neigh_addr = tmp_neigh_node->addr;
918 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
919 tmp_neigh_node->if_incoming == if_incoming)
924 /* if the window moved, set the update flag. */
925 need_update |= batadv_bit_get_packet(bat_priv,
926 tmp_neigh_node->real_bits,
929 tmp_neigh_node->real_packet_count =
930 bitmap_weight(tmp_neigh_node->real_bits,
931 TQ_LOCAL_WINDOW_SIZE);
936 batadv_dbg(DBG_BATMAN, bat_priv,
937 "updating last_seqno: old %u, new %u\n",
938 orig_node->last_real_seqno, seqno);
939 orig_node->last_real_seqno = seqno;
945 spin_unlock_bh(&orig_node->ogm_cnt_lock);
946 batadv_orig_node_free_ref(orig_node);
950 static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
951 struct batman_ogm_packet *batman_ogm_packet,
952 const unsigned char *tt_buff,
953 struct hard_iface *if_incoming)
955 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
956 struct hard_iface *hard_iface;
957 struct orig_node *orig_neigh_node, *orig_node;
958 struct neigh_node *router = NULL, *router_router = NULL;
959 struct neigh_node *orig_neigh_router = NULL;
960 int has_directlink_flag;
961 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
962 int is_broadcast = 0, is_bidirect;
963 bool is_single_hop_neigh = false;
964 bool is_from_best_next_hop = false;
965 int is_duplicate, sameseq, simlar_ttl;
966 uint32_t if_incoming_seqno;
967 uint8_t *prev_sender;
969 /* Silently drop when the batman packet is actually not a
972 * This might happen if a packet is padded (e.g. Ethernet has a
973 * minimum frame length of 64 byte) and the aggregation interprets
974 * it as an additional length.
976 * TODO: A more sane solution would be to have a bit in the
977 * batman_ogm_packet to detect whether the packet is the last
978 * packet in an aggregation. Here we expect that the padding
979 * is always zero (or not 0x01)
981 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
984 /* could be changed by schedule_own_packet() */
985 if_incoming_seqno = atomic_read(&if_incoming->seqno);
987 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
989 if (batadv_compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
990 is_single_hop_neigh = true;
992 batadv_dbg(DBG_BATMAN, bat_priv,
993 "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",
994 ethhdr->h_source, if_incoming->net_dev->name,
995 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
996 batman_ogm_packet->prev_sender,
997 ntohl(batman_ogm_packet->seqno), batman_ogm_packet->ttvn,
998 ntohs(batman_ogm_packet->tt_crc),
999 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
1000 batman_ogm_packet->header.ttl,
1001 batman_ogm_packet->header.version, has_directlink_flag);
1004 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1005 if (hard_iface->if_status != IF_ACTIVE)
1008 if (hard_iface->soft_iface != if_incoming->soft_iface)
1011 if (batadv_compare_eth(ethhdr->h_source,
1012 hard_iface->net_dev->dev_addr))
1015 if (batadv_compare_eth(batman_ogm_packet->orig,
1016 hard_iface->net_dev->dev_addr))
1019 if (batadv_compare_eth(batman_ogm_packet->prev_sender,
1020 hard_iface->net_dev->dev_addr))
1023 if (is_broadcast_ether_addr(ethhdr->h_source))
1028 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
1029 batadv_dbg(DBG_BATMAN, bat_priv,
1030 "Drop packet: incompatible batman version (%i)\n",
1031 batman_ogm_packet->header.version);
1036 batadv_dbg(DBG_BATMAN, bat_priv,
1037 "Drop packet: received my own broadcast (sender: %pM)\n",
1043 batadv_dbg(DBG_BATMAN, bat_priv,
1044 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1050 unsigned long *word;
1054 orig_neigh_node = batadv_get_orig_node(bat_priv,
1056 if (!orig_neigh_node)
1059 /* neighbor has to indicate direct link and it has to
1060 * come via the corresponding interface
1061 * save packet seqno for bidirectional check
1063 if (has_directlink_flag &&
1064 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1065 batman_ogm_packet->orig)) {
1066 offset = if_incoming->if_num * NUM_WORDS;
1068 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1069 word = &(orig_neigh_node->bcast_own[offset]);
1070 bit_pos = if_incoming_seqno - 2;
1071 bit_pos -= ntohl(batman_ogm_packet->seqno);
1072 batadv_set_bit(word, bit_pos);
1073 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
1074 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
1075 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1078 batadv_dbg(DBG_BATMAN, bat_priv,
1079 "Drop packet: originator packet from myself (via neighbor)\n");
1080 batadv_orig_node_free_ref(orig_neigh_node);
1084 if (is_my_oldorig) {
1085 batadv_dbg(DBG_BATMAN, bat_priv,
1086 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1091 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1092 batadv_dbg(DBG_BATMAN, bat_priv,
1093 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1098 orig_node = batadv_get_orig_node(bat_priv, batman_ogm_packet->orig);
1102 is_duplicate = batadv_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1105 if (is_duplicate == -1) {
1106 batadv_dbg(DBG_BATMAN, bat_priv,
1107 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1112 if (batman_ogm_packet->tq == 0) {
1113 batadv_dbg(DBG_BATMAN, bat_priv,
1114 "Drop packet: originator packet with tq equal 0\n");
1118 router = batadv_orig_node_get_router(orig_node);
1120 router_router = batadv_orig_node_get_router(router->orig_node);
1122 if ((router && router->tq_avg != 0) &&
1123 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1124 is_from_best_next_hop = true;
1126 prev_sender = batman_ogm_packet->prev_sender;
1127 /* avoid temporary routing loops */
1128 if (router && router_router &&
1129 (batadv_compare_eth(router->addr, prev_sender)) &&
1130 !(batadv_compare_eth(batman_ogm_packet->orig, prev_sender)) &&
1131 (batadv_compare_eth(router->addr, router_router->addr))) {
1132 batadv_dbg(DBG_BATMAN, bat_priv,
1133 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1138 /* if sender is a direct neighbor the sender mac equals
1141 orig_neigh_node = (is_single_hop_neigh ?
1143 batadv_get_orig_node(bat_priv, ethhdr->h_source));
1144 if (!orig_neigh_node)
1147 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
1149 /* drop packet if sender is not a direct neighbor and if we
1150 * don't route towards it
1152 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1153 batadv_dbg(DBG_BATMAN, bat_priv,
1154 "Drop packet: OGM via unknown neighbor!\n");
1158 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1159 batman_ogm_packet, if_incoming);
1161 batadv_bonding_save_primary(orig_node, orig_neigh_node,
1164 /* update ranking if it is not a duplicate or has the same
1165 * seqno and similar ttl as the non-duplicate
1167 sameseq = orig_node->last_real_seqno == ntohl(batman_ogm_packet->seqno);
1168 simlar_ttl = orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl;
1169 if (is_bidirect && (!is_duplicate || (sameseq && simlar_ttl)))
1170 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1171 batman_ogm_packet, if_incoming,
1172 tt_buff, is_duplicate);
1174 /* is single hop (direct) neighbor */
1175 if (is_single_hop_neigh) {
1177 /* mark direct link on incoming interface */
1178 batadv_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1179 is_single_hop_neigh,
1180 is_from_best_next_hop, if_incoming);
1182 batadv_dbg(DBG_BATMAN, bat_priv,
1183 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1187 /* multihop originator */
1189 batadv_dbg(DBG_BATMAN, bat_priv,
1190 "Drop packet: not received via bidirectional link\n");
1195 batadv_dbg(DBG_BATMAN, bat_priv,
1196 "Drop packet: duplicate packet received\n");
1200 batadv_dbg(DBG_BATMAN, bat_priv,
1201 "Forwarding packet: rebroadcast originator packet\n");
1202 batadv_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1203 is_single_hop_neigh, is_from_best_next_hop,
1207 if ((orig_neigh_node) && (!is_single_hop_neigh))
1208 batadv_orig_node_free_ref(orig_neigh_node);
1211 batadv_neigh_node_free_ref(router);
1213 batadv_neigh_node_free_ref(router_router);
1214 if (orig_neigh_router)
1215 batadv_neigh_node_free_ref(orig_neigh_router);
1217 batadv_orig_node_free_ref(orig_node);
1220 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1221 struct hard_iface *if_incoming)
1223 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1224 struct batman_ogm_packet *batman_ogm_packet;
1225 struct ethhdr *ethhdr;
1226 int buff_pos = 0, packet_len;
1227 unsigned char *tt_buff, *packet_buff;
1230 ret = batadv_check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
1234 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1235 * that does not have B.A.T.M.A.N. IV enabled ?
1237 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1240 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_RX);
1241 batadv_add_counter(bat_priv, BAT_CNT_MGMT_RX_BYTES,
1242 skb->len + ETH_HLEN);
1244 packet_len = skb_headlen(skb);
1245 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1246 packet_buff = skb->data;
1247 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1249 /* unpack the aggregated packets and process them one by one */
1251 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
1253 batadv_iv_ogm_process(ethhdr, batman_ogm_packet, tt_buff,
1256 buff_pos += BATMAN_OGM_HLEN;
1257 buff_pos += batadv_tt_len(batman_ogm_packet->tt_num_changes);
1259 batman_ogm_packet = (struct batman_ogm_packet *)
1260 (packet_buff + buff_pos);
1261 } while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1262 batman_ogm_packet->tt_num_changes));
1265 return NET_RX_SUCCESS;
1268 static struct bat_algo_ops batadv_batman_iv __read_mostly = {
1269 .name = "BATMAN_IV",
1270 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1271 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1272 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1273 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1274 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1275 .bat_ogm_emit = batadv_iv_ogm_emit,
1278 int __init batadv_iv_init(void)
1282 /* batman originator packet */
1283 ret = batadv_recv_handler_register(BAT_IV_OGM, batadv_iv_ogm_receive);
1287 ret = batadv_algo_register(&batadv_batman_iv);
1289 goto handler_unregister;
1294 batadv_recv_handler_unregister(BAT_IV_OGM);