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 void bat_iv_ogm_init(struct hard_iface
*hard_iface
)
35 struct batman_ogm_packet
*batman_ogm_packet
;
37 hard_iface
->packet_len
= BATMAN_OGM_LEN
;
38 hard_iface
->packet_buff
= kmalloc(hard_iface
->packet_len
, GFP_ATOMIC
);
40 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
41 batman_ogm_packet
->header
.packet_type
= BAT_OGM
;
42 batman_ogm_packet
->header
.version
= COMPAT_VERSION
;
43 batman_ogm_packet
->header
.ttl
= 2;
44 batman_ogm_packet
->flags
= NO_FLAGS
;
45 batman_ogm_packet
->tq
= TQ_MAX_VALUE
;
46 batman_ogm_packet
->tt_num_changes
= 0;
47 batman_ogm_packet
->ttvn
= 0;
50 static void bat_iv_ogm_init_primary(struct hard_iface
*hard_iface
)
52 struct batman_ogm_packet
*batman_ogm_packet
;
54 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
55 batman_ogm_packet
->flags
= PRIMARIES_FIRST_HOP
;
56 batman_ogm_packet
->header
.ttl
= TTL
;
59 static void bat_iv_ogm_update_mac(struct hard_iface
*hard_iface
)
61 struct batman_ogm_packet
*batman_ogm_packet
;
63 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
64 memcpy(batman_ogm_packet
->orig
,
65 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
66 memcpy(batman_ogm_packet
->prev_sender
,
67 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
70 /* when do we schedule our own ogm to be sent */
71 static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv
*bat_priv
)
73 return jiffies
+ msecs_to_jiffies(
74 atomic_read(&bat_priv
->orig_interval
) -
75 JITTER
+ (random32() % (2*JITTER
)));
78 /* when do we schedule a ogm packet to be sent */
79 static unsigned long bat_iv_ogm_fwd_send_time(void)
81 return jiffies
+ msecs_to_jiffies(random32() % (JITTER
/2));
84 /* apply hop penalty for a normal link */
85 static uint8_t hop_penalty(uint8_t tq
, const struct bat_priv
*bat_priv
)
87 int hop_penalty
= atomic_read(&bat_priv
->hop_penalty
);
88 return (tq
* (TQ_MAX_VALUE
- hop_penalty
)) / (TQ_MAX_VALUE
);
91 /* is there another aggregated packet here? */
92 static int bat_iv_ogm_aggr_packet(int buff_pos
, int packet_len
,
95 int next_buff_pos
= buff_pos
+ BATMAN_OGM_LEN
+ tt_len(tt_num_changes
);
97 return (next_buff_pos
<= packet_len
) &&
98 (next_buff_pos
<= MAX_AGGREGATION_BYTES
);
101 /* send a batman ogm to a given interface */
102 static void bat_iv_ogm_send_to_if(struct forw_packet
*forw_packet
,
103 struct hard_iface
*hard_iface
)
105 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
109 struct batman_ogm_packet
*batman_ogm_packet
;
112 if (hard_iface
->if_status
!= IF_ACTIVE
)
117 batman_ogm_packet
= (struct batman_ogm_packet
*)forw_packet
->skb
->data
;
119 /* adjust all flags and log packets */
120 while (bat_iv_ogm_aggr_packet(buff_pos
, forw_packet
->packet_len
,
121 batman_ogm_packet
->tt_num_changes
)) {
123 /* we might have aggregated direct link packets with an
124 * ordinary base packet */
125 if ((forw_packet
->direct_link_flags
& (1 << packet_num
)) &&
126 (forw_packet
->if_incoming
== hard_iface
))
127 batman_ogm_packet
->flags
|= DIRECTLINK
;
129 batman_ogm_packet
->flags
&= ~DIRECTLINK
;
131 fwd_str
= (packet_num
> 0 ? "Forwarding" : (forw_packet
->own
?
134 bat_dbg(DBG_BATMAN
, bat_priv
,
135 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
136 fwd_str
, (packet_num
> 0 ? "aggregated " : ""),
137 batman_ogm_packet
->orig
,
138 ntohl(batman_ogm_packet
->seqno
),
139 batman_ogm_packet
->tq
, batman_ogm_packet
->header
.ttl
,
140 (batman_ogm_packet
->flags
& DIRECTLINK
?
142 batman_ogm_packet
->ttvn
, hard_iface
->net_dev
->name
,
143 hard_iface
->net_dev
->dev_addr
);
145 buff_pos
+= BATMAN_OGM_LEN
+
146 tt_len(batman_ogm_packet
->tt_num_changes
);
148 batman_ogm_packet
= (struct batman_ogm_packet
*)
149 (forw_packet
->skb
->data
+ buff_pos
);
152 /* create clone because function is called more than once */
153 skb
= skb_clone(forw_packet
->skb
, GFP_ATOMIC
);
155 send_skb_packet(skb
, hard_iface
, broadcast_addr
);
158 /* send a batman ogm packet */
159 static void bat_iv_ogm_emit(struct forw_packet
*forw_packet
)
161 struct hard_iface
*hard_iface
;
162 struct net_device
*soft_iface
;
163 struct bat_priv
*bat_priv
;
164 struct hard_iface
*primary_if
= NULL
;
165 struct batman_ogm_packet
*batman_ogm_packet
;
166 unsigned char directlink
;
168 batman_ogm_packet
= (struct batman_ogm_packet
*)
169 (forw_packet
->skb
->data
);
170 directlink
= (batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0);
172 if (!forw_packet
->if_incoming
) {
173 pr_err("Error - can't forward packet: incoming iface not specified\n");
177 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
178 bat_priv
= netdev_priv(soft_iface
);
180 if (forw_packet
->if_incoming
->if_status
!= IF_ACTIVE
)
183 primary_if
= primary_if_get_selected(bat_priv
);
187 /* multihomed peer assumed */
188 /* non-primary OGMs are only broadcasted on their interface */
189 if ((directlink
&& (batman_ogm_packet
->header
.ttl
== 1)) ||
190 (forw_packet
->own
&& (forw_packet
->if_incoming
!= primary_if
))) {
192 /* FIXME: what about aggregated packets ? */
193 bat_dbg(DBG_BATMAN
, bat_priv
,
194 "%s packet (originator %pM, seqno %d, TTL %d) on interface %s [%pM]\n",
195 (forw_packet
->own
? "Sending own" : "Forwarding"),
196 batman_ogm_packet
->orig
,
197 ntohl(batman_ogm_packet
->seqno
),
198 batman_ogm_packet
->header
.ttl
,
199 forw_packet
->if_incoming
->net_dev
->name
,
200 forw_packet
->if_incoming
->net_dev
->dev_addr
);
202 /* skb is only used once and than forw_packet is free'd */
203 send_skb_packet(forw_packet
->skb
, forw_packet
->if_incoming
,
205 forw_packet
->skb
= NULL
;
210 /* broadcast on every interface */
212 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
213 if (hard_iface
->soft_iface
!= soft_iface
)
216 bat_iv_ogm_send_to_if(forw_packet
, hard_iface
);
222 hardif_free_ref(primary_if
);
225 /* return true if new_packet can be aggregated with forw_packet */
226 static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
227 *new_batman_ogm_packet
,
228 struct bat_priv
*bat_priv
,
229 int packet_len
, unsigned long send_time
,
231 const struct hard_iface
*if_incoming
,
232 const struct forw_packet
*forw_packet
)
234 struct batman_ogm_packet
*batman_ogm_packet
;
235 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
236 struct hard_iface
*primary_if
= NULL
;
239 batman_ogm_packet
= (struct batman_ogm_packet
*)forw_packet
->skb
->data
;
242 * we can aggregate the current packet to this aggregated packet
245 * - the send time is within our MAX_AGGREGATION_MS time
246 * - the resulting packet wont be bigger than
247 * MAX_AGGREGATION_BYTES
250 if (time_before(send_time
, forw_packet
->send_time
) &&
251 time_after_eq(send_time
+ msecs_to_jiffies(MAX_AGGREGATION_MS
),
252 forw_packet
->send_time
) &&
253 (aggregated_bytes
<= MAX_AGGREGATION_BYTES
)) {
256 * check aggregation compatibility
257 * -> direct link packets are broadcasted on
258 * their interface only
259 * -> aggregate packet if the current packet is
260 * a "global" packet as well as the base
264 primary_if
= primary_if_get_selected(bat_priv
);
268 /* packets without direct link flag and high TTL
269 * are flooded through the net */
271 (!(batman_ogm_packet
->flags
& DIRECTLINK
)) &&
272 (batman_ogm_packet
->header
.ttl
!= 1) &&
274 /* own packets originating non-primary
275 * interfaces leave only that interface */
276 ((!forw_packet
->own
) ||
277 (forw_packet
->if_incoming
== primary_if
))) {
282 /* if the incoming packet is sent via this one
283 * interface only - we still can aggregate */
285 (new_batman_ogm_packet
->header
.ttl
== 1) &&
286 (forw_packet
->if_incoming
== if_incoming
) &&
288 /* packets from direct neighbors or
289 * own secondary interface packets
290 * (= secondary interface packets in general) */
291 (batman_ogm_packet
->flags
& DIRECTLINK
||
293 forw_packet
->if_incoming
!= primary_if
))) {
301 hardif_free_ref(primary_if
);
305 /* create a new aggregated packet and add this packet to it */
306 static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff
,
307 int packet_len
, unsigned long send_time
,
309 struct hard_iface
*if_incoming
,
312 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
313 struct forw_packet
*forw_packet_aggr
;
314 unsigned char *skb_buff
;
316 if (!atomic_inc_not_zero(&if_incoming
->refcount
))
319 /* own packet should always be scheduled */
321 if (!atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
322 bat_dbg(DBG_BATMAN
, bat_priv
,
323 "batman packet queue full\n");
328 forw_packet_aggr
= kmalloc(sizeof(*forw_packet_aggr
), GFP_ATOMIC
);
329 if (!forw_packet_aggr
) {
331 atomic_inc(&bat_priv
->batman_queue_left
);
335 if ((atomic_read(&bat_priv
->aggregated_ogms
)) &&
336 (packet_len
< MAX_AGGREGATION_BYTES
))
337 forw_packet_aggr
->skb
= dev_alloc_skb(MAX_AGGREGATION_BYTES
+
338 sizeof(struct ethhdr
));
340 forw_packet_aggr
->skb
= dev_alloc_skb(packet_len
+
341 sizeof(struct ethhdr
));
343 if (!forw_packet_aggr
->skb
) {
345 atomic_inc(&bat_priv
->batman_queue_left
);
346 kfree(forw_packet_aggr
);
349 skb_reserve(forw_packet_aggr
->skb
, sizeof(struct ethhdr
));
351 INIT_HLIST_NODE(&forw_packet_aggr
->list
);
353 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
354 forw_packet_aggr
->packet_len
= packet_len
;
355 memcpy(skb_buff
, packet_buff
, packet_len
);
357 forw_packet_aggr
->own
= own_packet
;
358 forw_packet_aggr
->if_incoming
= if_incoming
;
359 forw_packet_aggr
->num_packets
= 0;
360 forw_packet_aggr
->direct_link_flags
= NO_FLAGS
;
361 forw_packet_aggr
->send_time
= send_time
;
363 /* save packet direct link flag status */
365 forw_packet_aggr
->direct_link_flags
|= 1;
367 /* add new packet to packet list */
368 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
369 hlist_add_head(&forw_packet_aggr
->list
, &bat_priv
->forw_bat_list
);
370 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
372 /* start timer for this packet */
373 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
374 send_outstanding_bat_ogm_packet
);
375 queue_delayed_work(bat_event_workqueue
,
376 &forw_packet_aggr
->delayed_work
,
377 send_time
- jiffies
);
381 hardif_free_ref(if_incoming
);
384 /* aggregate a new packet into the existing ogm packet */
385 static void bat_iv_ogm_aggregate(struct forw_packet
*forw_packet_aggr
,
386 const unsigned char *packet_buff
,
387 int packet_len
, bool direct_link
)
389 unsigned char *skb_buff
;
391 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
392 memcpy(skb_buff
, packet_buff
, packet_len
);
393 forw_packet_aggr
->packet_len
+= packet_len
;
394 forw_packet_aggr
->num_packets
++;
396 /* save packet direct link flag status */
398 forw_packet_aggr
->direct_link_flags
|=
399 (1 << forw_packet_aggr
->num_packets
);
402 static void bat_iv_ogm_queue_add(struct bat_priv
*bat_priv
,
403 unsigned char *packet_buff
,
404 int packet_len
, struct hard_iface
*if_incoming
,
405 int own_packet
, unsigned long send_time
)
408 * _aggr -> pointer to the packet we want to aggregate with
409 * _pos -> pointer to the position in the queue
411 struct forw_packet
*forw_packet_aggr
= NULL
, *forw_packet_pos
= NULL
;
412 struct hlist_node
*tmp_node
;
413 struct batman_ogm_packet
*batman_ogm_packet
;
416 batman_ogm_packet
= (struct batman_ogm_packet
*)packet_buff
;
417 direct_link
= batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0;
419 /* find position for the packet in the forward queue */
420 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
421 /* own packets are not to be aggregated */
422 if ((atomic_read(&bat_priv
->aggregated_ogms
)) && (!own_packet
)) {
423 hlist_for_each_entry(forw_packet_pos
, tmp_node
,
424 &bat_priv
->forw_bat_list
, list
) {
425 if (bat_iv_ogm_can_aggregate(batman_ogm_packet
,
426 bat_priv
, packet_len
,
427 send_time
, direct_link
,
430 forw_packet_aggr
= forw_packet_pos
;
436 /* nothing to aggregate with - either aggregation disabled or no
437 * suitable aggregation packet found */
438 if (!forw_packet_aggr
) {
439 /* the following section can run without the lock */
440 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
443 * if we could not aggregate this packet with one of the others
444 * we hold it back for a while, so that it might be aggregated
448 (atomic_read(&bat_priv
->aggregated_ogms
)))
449 send_time
+= msecs_to_jiffies(MAX_AGGREGATION_MS
);
451 bat_iv_ogm_aggregate_new(packet_buff
, packet_len
,
452 send_time
, direct_link
,
453 if_incoming
, own_packet
);
455 bat_iv_ogm_aggregate(forw_packet_aggr
, packet_buff
,
456 packet_len
, direct_link
);
457 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
461 static void bat_iv_ogm_forward(struct orig_node
*orig_node
,
462 const struct ethhdr
*ethhdr
,
463 struct batman_ogm_packet
*batman_ogm_packet
,
464 int directlink
, struct hard_iface
*if_incoming
)
466 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
467 struct neigh_node
*router
;
468 uint8_t in_tq
, in_ttl
, tq_avg
= 0;
469 uint8_t tt_num_changes
;
471 if (batman_ogm_packet
->header
.ttl
<= 1) {
472 bat_dbg(DBG_BATMAN
, bat_priv
, "ttl exceeded\n");
476 router
= orig_node_get_router(orig_node
);
478 in_tq
= batman_ogm_packet
->tq
;
479 in_ttl
= batman_ogm_packet
->header
.ttl
;
480 tt_num_changes
= batman_ogm_packet
->tt_num_changes
;
482 batman_ogm_packet
->header
.ttl
--;
483 memcpy(batman_ogm_packet
->prev_sender
, ethhdr
->h_source
, ETH_ALEN
);
485 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
486 * of our best tq value */
487 if (router
&& router
->tq_avg
!= 0) {
489 /* rebroadcast ogm of best ranking neighbor as is */
490 if (!compare_eth(router
->addr
, ethhdr
->h_source
)) {
491 batman_ogm_packet
->tq
= router
->tq_avg
;
493 if (router
->last_ttl
)
494 batman_ogm_packet
->header
.ttl
=
495 router
->last_ttl
- 1;
498 tq_avg
= router
->tq_avg
;
502 neigh_node_free_ref(router
);
504 /* apply hop penalty */
505 batman_ogm_packet
->tq
= hop_penalty(batman_ogm_packet
->tq
, bat_priv
);
507 bat_dbg(DBG_BATMAN
, bat_priv
,
508 "Forwarding packet: tq_orig: %i, tq_avg: %i, tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
509 in_tq
, tq_avg
, batman_ogm_packet
->tq
, in_ttl
- 1,
510 batman_ogm_packet
->header
.ttl
);
512 batman_ogm_packet
->seqno
= htonl(batman_ogm_packet
->seqno
);
513 batman_ogm_packet
->tt_crc
= htons(batman_ogm_packet
->tt_crc
);
515 /* switch of primaries first hop flag when forwarding */
516 batman_ogm_packet
->flags
&= ~PRIMARIES_FIRST_HOP
;
518 batman_ogm_packet
->flags
|= DIRECTLINK
;
520 batman_ogm_packet
->flags
&= ~DIRECTLINK
;
522 bat_iv_ogm_queue_add(bat_priv
, (unsigned char *)batman_ogm_packet
,
523 BATMAN_OGM_LEN
+ tt_len(tt_num_changes
),
524 if_incoming
, 0, bat_iv_ogm_fwd_send_time());
527 static void bat_iv_ogm_schedule(struct hard_iface
*hard_iface
,
530 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
531 struct batman_ogm_packet
*batman_ogm_packet
;
532 struct hard_iface
*primary_if
;
535 vis_server
= atomic_read(&bat_priv
->vis_mode
);
536 primary_if
= primary_if_get_selected(bat_priv
);
538 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
540 /* change sequence number to network order */
541 batman_ogm_packet
->seqno
=
542 htonl((uint32_t)atomic_read(&hard_iface
->seqno
));
544 batman_ogm_packet
->ttvn
= atomic_read(&bat_priv
->ttvn
);
545 batman_ogm_packet
->tt_crc
= htons((uint16_t)
546 atomic_read(&bat_priv
->tt_crc
));
547 if (tt_num_changes
>= 0)
548 batman_ogm_packet
->tt_num_changes
= tt_num_changes
;
550 if (vis_server
== VIS_TYPE_SERVER_SYNC
)
551 batman_ogm_packet
->flags
|= VIS_SERVER
;
553 batman_ogm_packet
->flags
&= ~VIS_SERVER
;
555 if ((hard_iface
== primary_if
) &&
556 (atomic_read(&bat_priv
->gw_mode
) == GW_MODE_SERVER
))
557 batman_ogm_packet
->gw_flags
=
558 (uint8_t)atomic_read(&bat_priv
->gw_bandwidth
);
560 batman_ogm_packet
->gw_flags
= NO_FLAGS
;
562 atomic_inc(&hard_iface
->seqno
);
564 slide_own_bcast_window(hard_iface
);
565 bat_iv_ogm_queue_add(bat_priv
, hard_iface
->packet_buff
,
566 hard_iface
->packet_len
, hard_iface
, 1,
567 bat_iv_ogm_emit_send_time(bat_priv
));
570 hardif_free_ref(primary_if
);
573 static void bat_iv_ogm_orig_update(struct bat_priv
*bat_priv
,
574 struct orig_node
*orig_node
,
575 const struct ethhdr
*ethhdr
,
576 const struct batman_ogm_packet
578 struct hard_iface
*if_incoming
,
579 const unsigned char *tt_buff
,
582 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
= NULL
;
583 struct neigh_node
*router
= NULL
;
584 struct orig_node
*orig_node_tmp
;
585 struct hlist_node
*node
;
586 uint8_t bcast_own_sum_orig
, bcast_own_sum_neigh
;
588 bat_dbg(DBG_BATMAN
, bat_priv
,
589 "update_originator(): Searching and updating originator entry of received packet\n");
592 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
593 &orig_node
->neigh_list
, list
) {
594 if (compare_eth(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
595 (tmp_neigh_node
->if_incoming
== if_incoming
) &&
596 atomic_inc_not_zero(&tmp_neigh_node
->refcount
)) {
598 neigh_node_free_ref(neigh_node
);
599 neigh_node
= tmp_neigh_node
;
606 spin_lock_bh(&tmp_neigh_node
->tq_lock
);
607 ring_buffer_set(tmp_neigh_node
->tq_recv
,
608 &tmp_neigh_node
->tq_index
, 0);
609 tmp_neigh_node
->tq_avg
=
610 ring_buffer_avg(tmp_neigh_node
->tq_recv
);
611 spin_unlock_bh(&tmp_neigh_node
->tq_lock
);
615 struct orig_node
*orig_tmp
;
617 orig_tmp
= get_orig_node(bat_priv
, ethhdr
->h_source
);
621 neigh_node
= create_neighbor(orig_node
, orig_tmp
,
622 ethhdr
->h_source
, if_incoming
);
624 orig_node_free_ref(orig_tmp
);
628 bat_dbg(DBG_BATMAN
, bat_priv
,
629 "Updating existing last-hop neighbor of originator\n");
633 orig_node
->flags
= batman_ogm_packet
->flags
;
634 neigh_node
->last_valid
= jiffies
;
636 spin_lock_bh(&neigh_node
->tq_lock
);
637 ring_buffer_set(neigh_node
->tq_recv
,
638 &neigh_node
->tq_index
,
639 batman_ogm_packet
->tq
);
640 neigh_node
->tq_avg
= ring_buffer_avg(neigh_node
->tq_recv
);
641 spin_unlock_bh(&neigh_node
->tq_lock
);
644 orig_node
->last_ttl
= batman_ogm_packet
->header
.ttl
;
645 neigh_node
->last_ttl
= batman_ogm_packet
->header
.ttl
;
648 bonding_candidate_add(orig_node
, neigh_node
);
650 /* if this neighbor already is our next hop there is nothing
652 router
= orig_node_get_router(orig_node
);
653 if (router
== neigh_node
)
656 /* if this neighbor does not offer a better TQ we won't consider it */
657 if (router
&& (router
->tq_avg
> neigh_node
->tq_avg
))
660 /* if the TQ is the same and the link not more symmetric we
661 * won't consider it either */
662 if (router
&& (neigh_node
->tq_avg
== router
->tq_avg
)) {
663 orig_node_tmp
= router
->orig_node
;
664 spin_lock_bh(&orig_node_tmp
->ogm_cnt_lock
);
666 orig_node_tmp
->bcast_own_sum
[if_incoming
->if_num
];
667 spin_unlock_bh(&orig_node_tmp
->ogm_cnt_lock
);
669 orig_node_tmp
= neigh_node
->orig_node
;
670 spin_lock_bh(&orig_node_tmp
->ogm_cnt_lock
);
671 bcast_own_sum_neigh
=
672 orig_node_tmp
->bcast_own_sum
[if_incoming
->if_num
];
673 spin_unlock_bh(&orig_node_tmp
->ogm_cnt_lock
);
675 if (bcast_own_sum_orig
>= bcast_own_sum_neigh
)
679 update_route(bat_priv
, orig_node
, neigh_node
);
682 /* I have to check for transtable changes only if the OGM has been
683 * sent through a primary interface */
684 if (((batman_ogm_packet
->orig
!= ethhdr
->h_source
) &&
685 (batman_ogm_packet
->header
.ttl
> 2)) ||
686 (batman_ogm_packet
->flags
& PRIMARIES_FIRST_HOP
))
687 tt_update_orig(bat_priv
, orig_node
, tt_buff
,
688 batman_ogm_packet
->tt_num_changes
,
689 batman_ogm_packet
->ttvn
,
690 batman_ogm_packet
->tt_crc
);
692 if (orig_node
->gw_flags
!= batman_ogm_packet
->gw_flags
)
693 gw_node_update(bat_priv
, orig_node
,
694 batman_ogm_packet
->gw_flags
);
696 orig_node
->gw_flags
= batman_ogm_packet
->gw_flags
;
698 /* restart gateway selection if fast or late switching was enabled */
699 if ((orig_node
->gw_flags
) &&
700 (atomic_read(&bat_priv
->gw_mode
) == GW_MODE_CLIENT
) &&
701 (atomic_read(&bat_priv
->gw_sel_class
) > 2))
702 gw_check_election(bat_priv
, orig_node
);
710 neigh_node_free_ref(neigh_node
);
712 neigh_node_free_ref(router
);
715 static int bat_iv_ogm_calc_tq(struct orig_node
*orig_node
,
716 struct orig_node
*orig_neigh_node
,
717 struct batman_ogm_packet
*batman_ogm_packet
,
718 struct hard_iface
*if_incoming
)
720 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
721 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
;
722 struct hlist_node
*node
;
724 uint8_t orig_eq_count
, neigh_rq_count
, tq_own
;
725 int tq_asym_penalty
, ret
= 0;
727 /* find corresponding one hop neighbor */
729 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
730 &orig_neigh_node
->neigh_list
, list
) {
732 if (!compare_eth(tmp_neigh_node
->addr
, orig_neigh_node
->orig
))
735 if (tmp_neigh_node
->if_incoming
!= if_incoming
)
738 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
741 neigh_node
= tmp_neigh_node
;
747 neigh_node
= create_neighbor(orig_neigh_node
,
749 orig_neigh_node
->orig
,
755 /* if orig_node is direct neighbor update neigh_node last_valid */
756 if (orig_node
== orig_neigh_node
)
757 neigh_node
->last_valid
= jiffies
;
759 orig_node
->last_valid
= jiffies
;
761 /* find packet count of corresponding one hop neighbor */
762 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
763 orig_eq_count
= orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
];
764 neigh_rq_count
= neigh_node
->real_packet_count
;
765 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
767 /* pay attention to not get a value bigger than 100 % */
768 total_count
= (orig_eq_count
> neigh_rq_count
?
769 neigh_rq_count
: orig_eq_count
);
771 /* if we have too few packets (too less data) we set tq_own to zero */
772 /* if we receive too few packets it is not considered bidirectional */
773 if ((total_count
< TQ_LOCAL_BIDRECT_SEND_MINIMUM
) ||
774 (neigh_rq_count
< TQ_LOCAL_BIDRECT_RECV_MINIMUM
))
777 /* neigh_node->real_packet_count is never zero as we
778 * only purge old information when getting new
780 tq_own
= (TQ_MAX_VALUE
* total_count
) / neigh_rq_count
;
782 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
783 * affect the nearly-symmetric links only a little, but
784 * punishes asymmetric links more. This will give a value
785 * between 0 and TQ_MAX_VALUE
787 tq_asym_penalty
= TQ_MAX_VALUE
- (TQ_MAX_VALUE
*
788 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
) *
789 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
) *
790 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
)) /
791 (TQ_LOCAL_WINDOW_SIZE
*
792 TQ_LOCAL_WINDOW_SIZE
*
793 TQ_LOCAL_WINDOW_SIZE
);
795 batman_ogm_packet
->tq
= ((batman_ogm_packet
->tq
* tq_own
797 (TQ_MAX_VALUE
* TQ_MAX_VALUE
));
799 bat_dbg(DBG_BATMAN
, bat_priv
,
800 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
801 orig_node
->orig
, orig_neigh_node
->orig
, total_count
,
802 neigh_rq_count
, tq_own
, tq_asym_penalty
, batman_ogm_packet
->tq
);
804 /* if link has the minimum required transmission quality
805 * consider it bidirectional */
806 if (batman_ogm_packet
->tq
>= TQ_TOTAL_BIDRECT_LIMIT
)
811 neigh_node_free_ref(neigh_node
);
815 /* processes a batman packet for all interfaces, adjusts the sequence number and
816 * finds out whether it is a duplicate.
818 * 1 the packet is a duplicate
819 * 0 the packet has not yet been received
820 * -1 the packet is old and has been received while the seqno window
821 * was protected. Caller should drop it.
823 static int bat_iv_ogm_update_seqnos(const struct ethhdr
*ethhdr
,
824 const struct batman_ogm_packet
826 const struct hard_iface
*if_incoming
)
828 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
829 struct orig_node
*orig_node
;
830 struct neigh_node
*tmp_neigh_node
;
831 struct hlist_node
*node
;
832 int is_duplicate
= 0;
835 int set_mark
, ret
= -1;
837 orig_node
= get_orig_node(bat_priv
, batman_ogm_packet
->orig
);
841 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
842 seq_diff
= batman_ogm_packet
->seqno
- orig_node
->last_real_seqno
;
844 /* signalize caller that the packet is to be dropped. */
845 if (window_protected(bat_priv
, seq_diff
,
846 &orig_node
->batman_seqno_reset
))
850 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
851 &orig_node
->neigh_list
, list
) {
853 is_duplicate
|= get_bit_status(tmp_neigh_node
->real_bits
,
854 orig_node
->last_real_seqno
,
855 batman_ogm_packet
->seqno
);
857 if (compare_eth(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
858 (tmp_neigh_node
->if_incoming
== if_incoming
))
863 /* if the window moved, set the update flag. */
864 need_update
|= bit_get_packet(bat_priv
,
865 tmp_neigh_node
->real_bits
,
868 tmp_neigh_node
->real_packet_count
=
869 bit_packet_count(tmp_neigh_node
->real_bits
);
874 bat_dbg(DBG_BATMAN
, bat_priv
,
875 "updating last_seqno: old %d, new %d\n",
876 orig_node
->last_real_seqno
, batman_ogm_packet
->seqno
);
877 orig_node
->last_real_seqno
= batman_ogm_packet
->seqno
;
883 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
884 orig_node_free_ref(orig_node
);
888 static void bat_iv_ogm_process(const struct ethhdr
*ethhdr
,
889 struct batman_ogm_packet
*batman_ogm_packet
,
890 const unsigned char *tt_buff
,
891 struct hard_iface
*if_incoming
)
893 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
894 struct hard_iface
*hard_iface
;
895 struct orig_node
*orig_neigh_node
, *orig_node
;
896 struct neigh_node
*router
= NULL
, *router_router
= NULL
;
897 struct neigh_node
*orig_neigh_router
= NULL
;
898 int has_directlink_flag
;
899 int is_my_addr
= 0, is_my_orig
= 0, is_my_oldorig
= 0;
900 int is_broadcast
= 0, is_bidirectional
, is_single_hop_neigh
;
902 uint32_t if_incoming_seqno
;
904 /* Silently drop when the batman packet is actually not a
907 * This might happen if a packet is padded (e.g. Ethernet has a
908 * minimum frame length of 64 byte) and the aggregation interprets
909 * it as an additional length.
911 * TODO: A more sane solution would be to have a bit in the
912 * batman_ogm_packet to detect whether the packet is the last
913 * packet in an aggregation. Here we expect that the padding
914 * is always zero (or not 0x01)
916 if (batman_ogm_packet
->header
.packet_type
!= BAT_OGM
)
919 /* could be changed by schedule_own_packet() */
920 if_incoming_seqno
= atomic_read(&if_incoming
->seqno
);
922 has_directlink_flag
= (batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0);
924 is_single_hop_neigh
= (compare_eth(ethhdr
->h_source
,
925 batman_ogm_packet
->orig
) ? 1 : 0);
927 bat_dbg(DBG_BATMAN
, bat_priv
,
928 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
929 ethhdr
->h_source
, if_incoming
->net_dev
->name
,
930 if_incoming
->net_dev
->dev_addr
, batman_ogm_packet
->orig
,
931 batman_ogm_packet
->prev_sender
, batman_ogm_packet
->seqno
,
932 batman_ogm_packet
->ttvn
, batman_ogm_packet
->tt_crc
,
933 batman_ogm_packet
->tt_num_changes
, batman_ogm_packet
->tq
,
934 batman_ogm_packet
->header
.ttl
,
935 batman_ogm_packet
->header
.version
, has_directlink_flag
);
938 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
939 if (hard_iface
->if_status
!= IF_ACTIVE
)
942 if (hard_iface
->soft_iface
!= if_incoming
->soft_iface
)
945 if (compare_eth(ethhdr
->h_source
,
946 hard_iface
->net_dev
->dev_addr
))
949 if (compare_eth(batman_ogm_packet
->orig
,
950 hard_iface
->net_dev
->dev_addr
))
953 if (compare_eth(batman_ogm_packet
->prev_sender
,
954 hard_iface
->net_dev
->dev_addr
))
957 if (is_broadcast_ether_addr(ethhdr
->h_source
))
962 if (batman_ogm_packet
->header
.version
!= COMPAT_VERSION
) {
963 bat_dbg(DBG_BATMAN
, bat_priv
,
964 "Drop packet: incompatible batman version (%i)\n",
965 batman_ogm_packet
->header
.version
);
970 bat_dbg(DBG_BATMAN
, bat_priv
,
971 "Drop packet: received my own broadcast (sender: %pM)\n",
977 bat_dbg(DBG_BATMAN
, bat_priv
,
978 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
987 orig_neigh_node
= get_orig_node(bat_priv
, ethhdr
->h_source
);
988 if (!orig_neigh_node
)
991 /* neighbor has to indicate direct link and it has to
992 * come via the corresponding interface */
993 /* save packet seqno for bidirectional check */
994 if (has_directlink_flag
&&
995 compare_eth(if_incoming
->net_dev
->dev_addr
,
996 batman_ogm_packet
->orig
)) {
997 offset
= if_incoming
->if_num
* NUM_WORDS
;
999 spin_lock_bh(&orig_neigh_node
->ogm_cnt_lock
);
1000 word
= &(orig_neigh_node
->bcast_own
[offset
]);
1003 batman_ogm_packet
->seqno
- 2);
1004 orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
] =
1005 bit_packet_count(word
);
1006 spin_unlock_bh(&orig_neigh_node
->ogm_cnt_lock
);
1009 bat_dbg(DBG_BATMAN
, bat_priv
,
1010 "Drop packet: originator packet from myself (via neighbor)\n");
1011 orig_node_free_ref(orig_neigh_node
);
1015 if (is_my_oldorig
) {
1016 bat_dbg(DBG_BATMAN
, bat_priv
,
1017 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1022 orig_node
= get_orig_node(bat_priv
, batman_ogm_packet
->orig
);
1026 is_duplicate
= bat_iv_ogm_update_seqnos(ethhdr
, batman_ogm_packet
,
1029 if (is_duplicate
== -1) {
1030 bat_dbg(DBG_BATMAN
, bat_priv
,
1031 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1036 if (batman_ogm_packet
->tq
== 0) {
1037 bat_dbg(DBG_BATMAN
, bat_priv
,
1038 "Drop packet: originator packet with tq equal 0\n");
1042 router
= orig_node_get_router(orig_node
);
1044 router_router
= orig_node_get_router(router
->orig_node
);
1046 /* avoid temporary routing loops */
1047 if (router
&& router_router
&&
1048 (compare_eth(router
->addr
, batman_ogm_packet
->prev_sender
)) &&
1049 !(compare_eth(batman_ogm_packet
->orig
,
1050 batman_ogm_packet
->prev_sender
)) &&
1051 (compare_eth(router
->addr
, router_router
->addr
))) {
1052 bat_dbg(DBG_BATMAN
, bat_priv
,
1053 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1058 /* if sender is a direct neighbor the sender mac equals
1060 orig_neigh_node
= (is_single_hop_neigh
?
1062 get_orig_node(bat_priv
, ethhdr
->h_source
));
1063 if (!orig_neigh_node
)
1066 orig_neigh_router
= orig_node_get_router(orig_neigh_node
);
1068 /* drop packet if sender is not a direct neighbor and if we
1069 * don't route towards it */
1070 if (!is_single_hop_neigh
&& (!orig_neigh_router
)) {
1071 bat_dbg(DBG_BATMAN
, bat_priv
,
1072 "Drop packet: OGM via unknown neighbor!\n");
1076 is_bidirectional
= bat_iv_ogm_calc_tq(orig_node
, orig_neigh_node
,
1077 batman_ogm_packet
, if_incoming
);
1079 bonding_save_primary(orig_node
, orig_neigh_node
, batman_ogm_packet
);
1081 /* update ranking if it is not a duplicate or has the same
1082 * seqno and similar ttl as the non-duplicate */
1083 if (is_bidirectional
&&
1085 ((orig_node
->last_real_seqno
== batman_ogm_packet
->seqno
) &&
1086 (orig_node
->last_ttl
- 3 <= batman_ogm_packet
->header
.ttl
))))
1087 bat_iv_ogm_orig_update(bat_priv
, orig_node
, ethhdr
,
1088 batman_ogm_packet
, if_incoming
,
1089 tt_buff
, is_duplicate
);
1091 /* is single hop (direct) neighbor */
1092 if (is_single_hop_neigh
) {
1094 /* mark direct link on incoming interface */
1095 bat_iv_ogm_forward(orig_node
, ethhdr
, batman_ogm_packet
,
1098 bat_dbg(DBG_BATMAN
, bat_priv
,
1099 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1103 /* multihop originator */
1104 if (!is_bidirectional
) {
1105 bat_dbg(DBG_BATMAN
, bat_priv
,
1106 "Drop packet: not received via bidirectional link\n");
1111 bat_dbg(DBG_BATMAN
, bat_priv
,
1112 "Drop packet: duplicate packet received\n");
1116 bat_dbg(DBG_BATMAN
, bat_priv
,
1117 "Forwarding packet: rebroadcast originator packet\n");
1118 bat_iv_ogm_forward(orig_node
, ethhdr
, batman_ogm_packet
,
1122 if ((orig_neigh_node
) && (!is_single_hop_neigh
))
1123 orig_node_free_ref(orig_neigh_node
);
1126 neigh_node_free_ref(router
);
1128 neigh_node_free_ref(router_router
);
1129 if (orig_neigh_router
)
1130 neigh_node_free_ref(orig_neigh_router
);
1132 orig_node_free_ref(orig_node
);
1135 static void bat_iv_ogm_receive(struct hard_iface
*if_incoming
,
1136 struct sk_buff
*skb
)
1138 struct batman_ogm_packet
*batman_ogm_packet
;
1139 struct ethhdr
*ethhdr
;
1140 int buff_pos
= 0, packet_len
;
1141 unsigned char *tt_buff
, *packet_buff
;
1143 packet_len
= skb_headlen(skb
);
1144 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1145 packet_buff
= skb
->data
;
1146 batman_ogm_packet
= (struct batman_ogm_packet
*)packet_buff
;
1148 /* unpack the aggregated packets and process them one by one */
1150 /* network to host order for our 32bit seqno and the
1152 batman_ogm_packet
->seqno
= ntohl(batman_ogm_packet
->seqno
);
1153 batman_ogm_packet
->tt_crc
= ntohs(batman_ogm_packet
->tt_crc
);
1155 tt_buff
= packet_buff
+ buff_pos
+ BATMAN_OGM_LEN
;
1157 bat_iv_ogm_process(ethhdr
, batman_ogm_packet
,
1158 tt_buff
, if_incoming
);
1160 buff_pos
+= BATMAN_OGM_LEN
+
1161 tt_len(batman_ogm_packet
->tt_num_changes
);
1163 batman_ogm_packet
= (struct batman_ogm_packet
*)
1164 (packet_buff
+ buff_pos
);
1165 } while (bat_iv_ogm_aggr_packet(buff_pos
, packet_len
,
1166 batman_ogm_packet
->tt_num_changes
));
1169 static struct bat_algo_ops batman_iv __read_mostly
= {
1170 .name
= "BATMAN IV",
1171 .bat_ogm_init
= bat_iv_ogm_init
,
1172 .bat_ogm_init_primary
= bat_iv_ogm_init_primary
,
1173 .bat_ogm_update_mac
= bat_iv_ogm_update_mac
,
1174 .bat_ogm_schedule
= bat_iv_ogm_schedule
,
1175 .bat_ogm_emit
= bat_iv_ogm_emit
,
1176 .bat_ogm_receive
= bat_iv_ogm_receive
,
1179 int __init
bat_iv_init(void)
1181 return bat_algo_register(&batman_iv
);