1 // SPDX-License-Identifier: MIT
3 Copyright (c) 2007, 2008 by Juliusz Chroboczek
12 #include "babel_interface.h"
14 #include "neighbour.h"
20 #include "babel_main.h"
21 #include "babel_errors.h"
23 static unsigned char packet_header
[4] = {42, 2};
25 int split_horizon
= 1;
27 unsigned short myseqno
= 0;
29 #define UNICAST_BUFSIZE 1024
30 static int unicast_buffered
= 0;
31 static unsigned char *unicast_buffer
= NULL
;
32 struct neighbour
*unicast_neighbour
= NULL
;
33 struct timeval unicast_flush_timeout
= {0, 0};
35 /* Minimum TLV _body_ length for TLVs of particular types (0 = no limit). */
36 static const unsigned char tlv_min_length
[MESSAGE_MAX
+ 1] =
40 [ MESSAGE_ACK_REQ
] = 6,
42 [ MESSAGE_HELLO
] = 6,
44 [ MESSAGE_ROUTER_ID
] = 10,
46 [ MESSAGE_UPDATE
] = 10,
47 [ MESSAGE_REQUEST
] = 2,
48 [ MESSAGE_MH_REQUEST
] = 14,
51 /* Checks whether an AE exists or must be silently ignored */
58 /* Parse a network prefix, encoded in the somewhat baroque compressed
59 representation used by Babel. Return the number of bytes parsed. */
61 network_prefix(int ae
, int plen
, unsigned int omitted
,
62 const unsigned char *p
, const unsigned char *dp
,
63 unsigned int len
, unsigned char *p_r
)
66 unsigned char prefix
[16];
79 memset(prefix
, 0, 16);
86 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
88 memcpy(prefix
, v4prefix
, 12);
90 if (dp
== NULL
|| !v4mapped(dp
)) return -1;
91 memcpy(prefix
, dp
, 12 + omitted
);
93 if(pb
> omitted
) memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
97 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
)) return -1;
99 if (dp
== NULL
|| v4mapped(dp
)) return -1;
100 memcpy(prefix
, dp
, omitted
);
102 if(pb
> omitted
) memcpy(prefix
+ omitted
, p
, pb
- omitted
);
106 if(pb
> 8 && len
< pb
- 8) return -1;
109 if(pb
> 8) memcpy(prefix
+ 8, p
, pb
- 8);
116 mask_prefix(p_r
, prefix
, plen
< 0 ? 128 : ae
== 1 ? plen
+ 96 : plen
);
120 static bool parse_update_subtlv(const unsigned char *a
, int alen
,
121 unsigned char *channels
)
123 int type
, len
, i
= 0;
127 if(type
== SUBTLV_PAD1
) {
133 flog_err(EC_BABEL_PACKET
, "Received truncated attributes.");
137 if(i
+ len
+ 2 > alen
) {
138 flog_err(EC_BABEL_PACKET
, "Received truncated attributes.");
142 if (type
& SUBTLV_MANDATORY
) {
145 * If the mandatory bit is set, then the whole enclosing
146 * TLV MUST be silently ignored (except for updating the
147 * parser state by a Router-Id, Next Hop, or Update TLV,
148 * as described in the next section).
150 debugf(BABEL_DEBUG_COMMON
,
151 "Received Mandatory bit set but this FRR version is not prepared to handle it at this point");
153 } else if (type
== SUBTLV_PADN
) {
155 } else if (type
== SUBTLV_DIVERSITY
) {
156 if (len
> DIVERSITY_HOPS
) {
159 "Received overlong channel information (%d > %d).n",
160 len
, DIVERSITY_HOPS
);
161 len
= DIVERSITY_HOPS
;
163 if (memchr(a
+ i
+ 2, 0, len
) != NULL
) {
165 flog_err(EC_BABEL_PACKET
,
166 "Channel information contains 0!");
169 memset(channels
, 0, DIVERSITY_HOPS
);
170 memcpy(channels
, a
+ i
+ 2, len
);
172 debugf(BABEL_DEBUG_COMMON
,
173 "Received unknown route attribute %d.", type
);
182 parse_hello_subtlv(const unsigned char *a
, int alen
,
183 unsigned int *hello_send_us
)
185 int type
, len
, i
= 0, ret
= 0;
189 if(type
== SUBTLV_PAD1
) {
195 flog_err(EC_BABEL_PACKET
,
196 "Received truncated sub-TLV on Hello message.");
200 if(i
+ len
+ 2 > alen
) {
201 flog_err(EC_BABEL_PACKET
,
202 "Received truncated sub-TLV on Hello message.");
206 if (type
& SUBTLV_MANDATORY
) {
209 * If the mandatory bit is set, then the whole enclosing
210 * TLV MUST be silently ignored (except for updating the
211 * parser state by a Router-Id, Next Hop, or Update TLV, as
212 * described in the next section).
214 debugf(BABEL_DEBUG_COMMON
,
215 "Received subtlv with Mandatory bit, this version of FRR is not prepared to handle this currently");
217 } else if (type
== SUBTLV_PADN
) {
219 } else if (type
== SUBTLV_TIMESTAMP
) {
221 DO_NTOHL(*hello_send_us
, a
+ i
+ 2);
226 "Received incorrect RTT sub-TLV on Hello message.");
229 debugf(BABEL_DEBUG_COMMON
,
230 "Received unknown Hello sub-TLV type %d.", type
);
239 parse_ihu_subtlv(const unsigned char *a
, int alen
,
240 unsigned int *hello_send_us
,
241 unsigned int *hello_rtt_receive_time
)
243 int type
, len
, i
= 0, ret
= 0;
247 if(type
== SUBTLV_PAD1
) {
253 flog_err(EC_BABEL_PACKET
,
254 "Received truncated sub-TLV on IHU message.");
258 if(i
+ len
+ 2 > alen
) {
259 flog_err(EC_BABEL_PACKET
,
260 "Received truncated sub-TLV on IHU message.");
264 if(type
== SUBTLV_PADN
) {
266 } else if(type
== SUBTLV_TIMESTAMP
) {
268 DO_NTOHL(*hello_send_us
, a
+ i
+ 2);
269 DO_NTOHL(*hello_rtt_receive_time
, a
+ i
+ 6);
273 flog_err(EC_BABEL_PACKET
,
274 "Received incorrect RTT sub-TLV on IHU message.");
277 debugf(BABEL_DEBUG_COMMON
,
278 "Received unknown IHU sub-TLV type %d.", type
);
287 parse_request_subtlv(int ae
, const unsigned char *a
, int alen
,
288 unsigned char *src_prefix
, unsigned char *src_plen
)
290 int type
, len
, i
= 0;
291 int have_src_prefix
= 0;
295 if(type
== SUBTLV_PAD1
) {
304 if(i
+ 2 + len
> alen
)
307 if(type
== SUBTLV_PADN
) {
309 } else if(type
== SUBTLV_SOURCE_PREFIX
) {
315 if(have_src_prefix
!= 0)
317 rc
= network_prefix(ae
, a
[i
+ 2], 0, a
+ i
+ 3, NULL
,
318 len
- 1, src_prefix
);
322 *src_plen
= a
[i
+ 2] + 96;
324 *src_plen
= a
[i
+ 2];
327 debugf(BABEL_DEBUG_COMMON
,"Received unknown%s Route Request sub-TLV %d.",
328 ((type
& 0x80) != 0) ? " mandatory" : "", type
);
329 if((type
& 0x80) != 0)
338 flog_err(EC_BABEL_PACKET
, "Received truncated sub-TLV on Route Request.");
343 network_address(int ae
, const unsigned char *a
, unsigned int len
,
346 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
350 channels_len(unsigned char *channels
)
352 unsigned char *p
= memchr(channels
, 0, DIVERSITY_HOPS
);
353 return p
? (p
- channels
) : DIVERSITY_HOPS
;
356 /* Check, that the provided frame consists of a valid Babel packet header
357 followed by a sequence of TLVs. TLVs of known types are also checked to meet
358 minimum length constraints defined for each. Return 0 for no errors. */
360 babel_packet_examin(const unsigned char *packet
, int packetlen
, int *blength
)
363 const unsigned char *message
;
364 unsigned char type
, len
;
366 if(packetlen
< 4 || packet
[0] != 42 || packet
[1] != 2)
368 DO_NTOHS(bodylen
, packet
+ 2);
369 if(bodylen
+ 4 > packetlen
) {
370 debugf(BABEL_DEBUG_COMMON
, "Received truncated packet (%d + 4 > %d).",
375 message
= packet
+ 4 + i
;
377 if(type
== MESSAGE_PAD1
) {
381 if(i
+ 2 > bodylen
) {
382 debugf(BABEL_DEBUG_COMMON
,"Received truncated message.");
386 if(i
+ len
+ 2 > bodylen
) {
387 debugf(BABEL_DEBUG_COMMON
,"Received truncated message.");
391 if(type
<= MESSAGE_MAX
&& tlv_min_length
[type
] && len
< tlv_min_length
[type
]) {
392 debugf(BABEL_DEBUG_COMMON
,"Undersized %u TLV", type
);
403 parse_packet(const unsigned char *from
, struct interface
*ifp
,
404 const unsigned char *packet
, int packetlen
)
407 const unsigned char *message
;
408 unsigned char type
, len
;
410 struct neighbour
*neigh
;
411 int have_router_id
= 0, have_v4_prefix
= 0, have_v6_prefix
= 0,
412 have_v4_nh
= 0, have_v6_nh
= 0;
413 unsigned char router_id
[8], v4_prefix
[16], v6_prefix
[16],
414 v4_nh
[16], v6_nh
[16];
415 int have_hello_rtt
= 0;
416 /* Content of the RTT sub-TLV on IHU messages. */
417 unsigned int hello_send_us
= 0, hello_rtt_receive_time
= 0;
418 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
420 if(babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) {
421 /* We want to track exactly when we received this packet. */
425 if(!linklocal(from
)) {
426 flog_err(EC_BABEL_PACKET
,
427 "Received packet from non-local address %s.",
428 format_address(from
));
432 if (babel_packet_examin (packet
, packetlen
, &bodylen
)) {
433 flog_err(EC_BABEL_PACKET
,
434 "Received malformed packet on %s from %s.",
435 ifp
->name
, format_address(from
));
439 neigh
= find_neighbour(from
, ifp
);
441 flog_err(EC_BABEL_PACKET
, "Couldn't allocate neighbour.");
447 message
= packet
+ 4 + i
;
449 if(type
== MESSAGE_PAD1
) {
450 debugf(BABEL_DEBUG_COMMON
,"Received pad1 from %s on %s.",
451 format_address(from
), ifp
->name
);
457 if(type
== MESSAGE_PADN
) {
458 debugf(BABEL_DEBUG_COMMON
,"Received pad%d from %s on %s.",
459 len
, format_address(from
), ifp
->name
);
460 } else if(type
== MESSAGE_ACK_REQ
) {
461 unsigned short nonce
, interval
;
462 DO_NTOHS(nonce
, message
+ 4);
463 DO_NTOHS(interval
, message
+ 6);
464 debugf(BABEL_DEBUG_COMMON
,"Received ack-req (%04X %d) from %s on %s.",
465 nonce
, interval
, format_address(from
), ifp
->name
);
466 send_ack(neigh
, nonce
, interval
);
467 } else if(type
== MESSAGE_ACK
) {
468 debugf(BABEL_DEBUG_COMMON
,"Received ack from %s on %s.",
469 format_address(from
), ifp
->name
);
470 /* Nothing right now */
471 } else if(type
== MESSAGE_HELLO
) {
472 unsigned short seqno
, interval
, flags
;
474 unsigned int timestamp
= 0;
476 #define BABEL_UNICAST_HELLO 0x8000
477 DO_NTOHS(flags
, message
+ 2);
480 * RFC 8966 Appendix F
481 * TL;DR -> Please ignore Unicast hellos until FRR's
482 * BABEL is brought up to date
484 if (CHECK_FLAG(flags
, BABEL_UNICAST_HELLO
)) {
485 debugf(BABEL_DEBUG_COMMON
,
486 "Received Unicast Hello from %s on %s that FRR is not prepared to understand yet",
487 format_address(from
), ifp
->name
);
491 DO_NTOHS(seqno
, message
+ 4);
492 DO_NTOHS(interval
, message
+ 6);
493 debugf(BABEL_DEBUG_COMMON
,
494 "Received hello %d (%d) from %s on %s.", seqno
, interval
,
495 format_address(from
), ifp
->name
);
498 * RFC 8966 Appendix F
499 * TL;DR -> Please ignore any Hello packets with the interval
503 debugf(BABEL_DEBUG_COMMON
,
504 "Received hello from %s on %s should be ignored as that this version of FRR does not know how to properly handle interval == 0",
505 format_address(from
), ifp
->name
);
509 changed
= update_neighbour(neigh
, seqno
, interval
);
510 update_neighbour_metric(neigh
, changed
);
512 /* Multiply by 3/2 to allow hellos to expire. */
513 schedule_neighbours_check(interval
* 15, 0);
514 /* Sub-TLV handling. */
516 if (parse_hello_subtlv(message
+ 8, len
- 6,
518 neigh
->hello_send_us
= timestamp
;
519 neigh
->hello_rtt_receive_time
= babel_now
;
523 } else if(type
== MESSAGE_IHU
) {
524 unsigned short txcost
, interval
;
525 unsigned char address
[16];
527 DO_NTOHS(txcost
, message
+ 4);
528 DO_NTOHS(interval
, message
+ 6);
529 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
530 if(rc
< 0) goto fail
;
531 debugf(BABEL_DEBUG_COMMON
,"Received ihu %d (%d) from %s on %s for %s.",
533 format_address(from
), ifp
->name
,
534 format_address(address
));
535 if(message
[2] == 0 || is_interface_ll_address(ifp
, address
)) {
536 int changed
= txcost
!= neigh
->txcost
;
537 neigh
->txcost
= txcost
;
538 neigh
->ihu_time
= babel_now
;
539 neigh
->ihu_interval
= interval
;
540 update_neighbour_metric(neigh
, changed
);
542 /* Multiply by 3/2 to allow neighbours to expire. */
543 schedule_neighbours_check(interval
* 45, 0);
546 parse_ihu_subtlv(message
+ 8 + rc
, len
- 6 - rc
,
547 &hello_send_us
, &hello_rtt_receive_time
);
549 } else if(type
== MESSAGE_ROUTER_ID
) {
550 memcpy(router_id
, message
+ 4, 8);
552 debugf(BABEL_DEBUG_COMMON
,"Received router-id %s from %s on %s.",
553 format_eui64(router_id
), format_address(from
), ifp
->name
);
554 } else if(type
== MESSAGE_NH
) {
555 unsigned char nh
[16];
557 rc
= network_address(message
[2], message
+ 4, len
- 2,
564 debugf(BABEL_DEBUG_COMMON
,"Received nh %s (%d) from %s on %s.",
565 format_address(nh
), message
[2],
566 format_address(from
), ifp
->name
);
567 if(message
[2] == 1) {
568 memcpy(v4_nh
, nh
, 16);
571 memcpy(v6_nh
, nh
, 16);
574 } else if(type
== MESSAGE_UPDATE
) {
575 unsigned char prefix
[16], *nh
;
577 unsigned char channels
[DIVERSITY_HOPS
];
578 unsigned short interval
, seqno
, metric
;
580 bool ignore_update
= false;
582 DO_NTOHS(interval
, message
+ 6);
583 DO_NTOHS(seqno
, message
+ 8);
584 DO_NTOHS(metric
, message
+ 10);
585 if(message
[5] == 0 ||
586 (message
[2] == 1 ? have_v4_prefix
: have_v6_prefix
))
587 rc
= network_prefix(message
[2], message
[4], message
[5],
589 message
[2] == 1 ? v4_prefix
: v6_prefix
,
594 if(message
[3] & 0x80)
595 have_v4_prefix
= have_v6_prefix
= 0;
598 parsed_len
= 10 + rc
;
600 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
602 if(message
[3] & 0x80) {
603 if(message
[2] == 1) {
604 memcpy(v4_prefix
, prefix
, 16);
607 memcpy(v6_prefix
, prefix
, 16);
611 if(message
[3] & 0x40) {
612 if(message
[2] == 1) {
613 memset(router_id
, 0, 4);
614 memcpy(router_id
+ 4, prefix
+ 12, 4);
616 memcpy(router_id
, prefix
+ 8, 8);
620 if(!have_router_id
&& message
[2] != 0) {
621 flog_err(EC_BABEL_PACKET
,
622 "Received prefix with no router id.");
625 debugf(BABEL_DEBUG_COMMON
,"Received update%s%s for %s from %s on %s.",
626 (message
[3] & 0x80) ? "/prefix" : "",
627 (message
[3] & 0x40) ? "/id" : "",
628 format_prefix(prefix
, plen
),
629 format_address(from
), ifp
->name
);
631 if(message
[2] == 0) {
632 if(metric
< 0xFFFF) {
633 flog_err(EC_BABEL_PACKET
,
634 "Received wildcard update with finite metric.");
637 retract_neighbour_routes(neigh
);
639 } else if(message
[2] == 1) {
643 } else if(have_v6_nh
) {
649 if(message
[2] == 1) {
650 if(!babel_get_if_nfo(ifp
)->ipv4
)
654 if((babel_get_if_nfo(ifp
)->flags
& BABEL_IF_FARAWAY
)) {
657 /* This will be overwritten by parse_update_subtlv below. */
659 /* Assume non-interfering (wired) link. */
662 /* Assume interfering. */
663 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
669 parse_update_subtlv(message
+ 2 + parsed_len
,
670 len
- parsed_len
, channels
);
674 update_route(router_id
, prefix
, plen
, seqno
, metric
,
675 interval
, neigh
, nh
, channels
,
676 channels_len(channels
));
677 } else if(type
== MESSAGE_REQUEST
) {
678 unsigned char prefix
[16], src_prefix
[16], plen
, src_plen
;
680 if(len
< 2) goto fail
;
681 if(!known_ae(message
[2])) {
682 debugf(BABEL_DEBUG_COMMON
,"Received request with unknown AE %d. Ignoring.",
686 rc
= network_prefix(message
[2], message
[3], 0,
687 message
+ 4, NULL
, len
- 2, prefix
);
688 if(rc
< 0) goto fail
;
689 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
690 debugf(BABEL_DEBUG_COMMON
,"Received request for %s from %s on %s.",
691 message
[2] == 0 ? "any" : format_prefix(prefix
, plen
),
692 format_address(from
), ifp
->name
);
693 if(message
[2] == 1) {
694 v4tov6(src_prefix
, zeroes
);
697 memcpy(src_prefix
, zeroes
, 16);
700 rc
= parse_request_subtlv(message
[2], message
+ 4 + rc
,
701 len
- 2 - rc
, src_prefix
, &src_plen
);
704 is_ss
= !is_default(src_prefix
, src_plen
);
705 if(message
[2] == 0) {
706 struct babel_interface
*neigh_ifp
=babel_get_if_nfo(neigh
->ifp
);
708 /* Wildcard requests don't carry a source prefix. */
709 flog_err(EC_BABEL_PACKET
,
710 "Received source-specific wildcard request.");
713 /* If a neighbour is requesting a full route dump from us,
714 we might as well send it an IHU. */
715 send_ihu(neigh
, NULL
);
716 /* Since nodes send wildcard requests on boot, booting
717 a large number of nodes at the same time may cause an
718 update storm. Ignore a wildcard request that happens
719 shortly after we sent a full update. */
720 if(neigh_ifp
->last_update_time
<
721 (time_t)(babel_now
.tv_sec
-
722 MAX(neigh_ifp
->hello_interval
/ 100, 1)))
723 send_update(neigh
->ifp
, 0, NULL
, 0);
725 send_update(neigh
->ifp
, 0, prefix
, plen
);
727 } else if(type
== MESSAGE_MH_REQUEST
) {
728 unsigned char prefix
[16], plen
;
729 unsigned short seqno
;
731 DO_NTOHS(seqno
, message
+ 4);
732 rc
= network_prefix(message
[2], message
[3], 0,
733 message
+ 16, NULL
, len
- 14, prefix
);
734 if(rc
< 0) goto fail
;
735 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
736 debugf(BABEL_DEBUG_COMMON
,"Received request (%d) for %s from %s on %s (%s, %d).",
738 format_prefix(prefix
, plen
),
739 format_address(from
), ifp
->name
,
740 format_eui64(message
+ 8), seqno
);
741 handle_request(neigh
, prefix
, plen
, message
[6],
744 debugf(BABEL_DEBUG_COMMON
,"Received unknown packet type %d from %s on %s.",
745 type
, format_address(from
), ifp
->name
);
752 flog_err(EC_BABEL_PACKET
,
753 "Couldn't parse packet (%d, %d) from %s on %s.",
754 message
[0], message
[1], format_address(from
), ifp
->name
);
758 /* We can calculate the RTT to this neighbour. */
759 if(have_hello_rtt
&& hello_send_us
&& hello_rtt_receive_time
) {
760 int remote_waiting_us
, local_waiting_us
;
761 unsigned int rtt
, smoothed_rtt
;
762 unsigned int old_rttcost
;
764 remote_waiting_us
= neigh
->hello_send_us
- hello_rtt_receive_time
;
765 local_waiting_us
= time_us(neigh
->hello_rtt_receive_time
) -
768 /* Sanity checks (validity window of 10 minutes). */
769 if(remote_waiting_us
< 0 || local_waiting_us
< 0 ||
770 remote_waiting_us
> 600000000 || local_waiting_us
> 600000000)
773 rtt
= MAX(0, local_waiting_us
- remote_waiting_us
);
774 debugf(BABEL_DEBUG_COMMON
, "RTT to %s on %s sample result: %d us.",
775 format_address(from
), ifp
->name
, rtt
);
777 old_rttcost
= neighbour_rttcost(neigh
);
778 if (valid_rtt(neigh
)) {
779 /* Running exponential average. */
780 smoothed_rtt
= (babel_ifp
->rtt_decay
* rtt
+
781 (256 - babel_ifp
->rtt_decay
) * neigh
->rtt
);
782 /* Rounding (up or down) to get closer to the sample. */
783 neigh
->rtt
= (neigh
->rtt
>= rtt
) ? smoothed_rtt
/ 256 :
784 (smoothed_rtt
+ 255) / 256;
786 /* We prefer to be conservative with new neighbours
788 assert(rtt
<= 0x7FFFFFFF);
791 changed
= (neighbour_rttcost(neigh
) == old_rttcost
? 0 : 1);
792 update_neighbour_metric(neigh
, changed
);
793 neigh
->rtt_time
= babel_now
;
798 /* Under normal circumstances, there are enough moderation mechanisms
799 elsewhere in the protocol to make sure that this last-ditch check
800 should never trigger. But I'm superstitious. */
803 check_bucket(struct interface
*ifp
)
805 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
806 if(babel_ifp
->bucket
== 0) {
807 int seconds
= babel_now
.tv_sec
- babel_ifp
->bucket_time
;
809 babel_ifp
->bucket
= MIN(BUCKET_TOKENS_MAX
,
810 seconds
* BUCKET_TOKENS_PER_SEC
);
812 /* Reset bucket time unconditionally, in case clock is stepped. */
813 babel_ifp
->bucket_time
= babel_now
.tv_sec
;
816 if(babel_ifp
->bucket
> 0) {
825 fill_rtt_message(struct interface
*ifp
)
827 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
828 if((babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) &&
829 (babel_ifp
->buffered_hello
>= 0)) {
830 if(babel_ifp
->sendbuf
[babel_ifp
->buffered_hello
+ 8] == SUBTLV_PADN
&&
831 babel_ifp
->sendbuf
[babel_ifp
->buffered_hello
+ 9] == 4) {
833 /* Change the type of sub-TLV. */
834 babel_ifp
->sendbuf
[babel_ifp
->buffered_hello
+ 8] =
837 time
= time_us(babel_now
);
838 DO_HTONL(babel_ifp
->sendbuf
+ babel_ifp
->buffered_hello
+ 10, time
);
841 flog_err(EC_BABEL_PACKET
, "No space left for timestamp sub-TLV (this shouldn't happen)");
849 flushbuf(struct interface
*ifp
)
852 struct sockaddr_in6 sin6
;
853 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
855 assert(babel_ifp
->buffered
<= babel_ifp
->bufsize
);
859 if(babel_ifp
->buffered
> 0) {
860 debugf(BABEL_DEBUG_COMMON
," (flushing %d buffered bytes on %s)",
861 babel_ifp
->buffered
, ifp
->name
);
862 if(check_bucket(ifp
)) {
863 memset(&sin6
, 0, sizeof(sin6
));
864 sin6
.sin6_family
= AF_INET6
;
865 memcpy(&sin6
.sin6_addr
, protocol_group
, 16);
866 sin6
.sin6_port
= htons(protocol_port
);
867 sin6
.sin6_scope_id
= ifp
->ifindex
;
868 DO_HTONS(packet_header
+ 2, babel_ifp
->buffered
);
869 fill_rtt_message(ifp
);
870 rc
= babel_send(protocol_socket
,
871 packet_header
, sizeof(packet_header
),
872 babel_ifp
->sendbuf
, babel_ifp
->buffered
,
873 (struct sockaddr
*)&sin6
, sizeof(sin6
));
875 flog_err(EC_BABEL_PACKET
, "send: %s", safe_strerror(errno
));
877 flog_err(EC_BABEL_PACKET
, "Bucket full, dropping packet to %s.",
881 VALGRIND_MAKE_MEM_UNDEFINED(babel_ifp
->sendbuf
, babel_ifp
->bufsize
);
882 babel_ifp
->buffered
= 0;
883 babel_ifp
->buffered_hello
= -1;
884 babel_ifp
->have_buffered_id
= 0;
885 babel_ifp
->have_buffered_nh
= 0;
886 babel_ifp
->have_buffered_prefix
= 0;
887 babel_ifp
->flush_timeout
.tv_sec
= 0;
888 babel_ifp
->flush_timeout
.tv_usec
= 0;
892 schedule_flush(struct interface
*ifp
)
894 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
895 unsigned msecs
= jitter(babel_ifp
, 0);
896 if(babel_ifp
->flush_timeout
.tv_sec
!= 0 &&
897 timeval_minus_msec(&babel_ifp
->flush_timeout
, &babel_now
) < msecs
)
899 set_timeout(&babel_ifp
->flush_timeout
, msecs
);
903 schedule_flush_now(struct interface
*ifp
)
905 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
907 unsigned msecs
= roughly(10);
908 if(babel_ifp
->flush_timeout
.tv_sec
!= 0 &&
909 timeval_minus_msec(&babel_ifp
->flush_timeout
, &babel_now
) < msecs
)
911 set_timeout(&babel_ifp
->flush_timeout
, msecs
);
915 schedule_unicast_flush(unsigned msecs
)
917 if(!unicast_neighbour
)
919 if(unicast_flush_timeout
.tv_sec
!= 0 &&
920 timeval_minus_msec(&unicast_flush_timeout
, &babel_now
) < msecs
)
922 unicast_flush_timeout
.tv_usec
= (babel_now
.tv_usec
+ msecs
* 1000) %1000000;
923 unicast_flush_timeout
.tv_sec
=
924 babel_now
.tv_sec
+ (babel_now
.tv_usec
/ 1000 + msecs
) / 1000;
928 ensure_space(struct interface
*ifp
, int space
)
930 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
931 if(babel_ifp
->bufsize
- babel_ifp
->buffered
< space
)
936 start_message(struct interface
*ifp
, int type
, int len
)
938 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
939 if(babel_ifp
->bufsize
- babel_ifp
->buffered
< len
+ 2)
941 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = type
;
942 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = len
;
946 end_message(struct interface
*ifp
, int type
, int bytes
)
948 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
949 assert(babel_ifp
->buffered
>= bytes
+ 2 &&
950 babel_ifp
->sendbuf
[babel_ifp
->buffered
- bytes
- 2] == type
&&
951 babel_ifp
->sendbuf
[babel_ifp
->buffered
- bytes
- 1] == bytes
);
956 accumulate_byte(struct interface
*ifp
, unsigned char value
)
958 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
959 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = value
;
963 accumulate_short(struct interface
*ifp
, unsigned short value
)
965 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
966 DO_HTONS(babel_ifp
->sendbuf
+ babel_ifp
->buffered
, value
);
967 babel_ifp
->buffered
+= 2;
971 accumulate_int(struct interface
*ifp
, unsigned int value
)
973 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
974 DO_HTONL(babel_ifp
->sendbuf
+ babel_ifp
->buffered
, value
);
975 babel_ifp
->buffered
+= 4;
979 accumulate_bytes(struct interface
*ifp
,
980 const unsigned char *value
, unsigned len
)
982 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
983 memcpy(babel_ifp
->sendbuf
+ babel_ifp
->buffered
, value
, len
);
984 babel_ifp
->buffered
+= len
;
988 start_unicast_message(struct neighbour
*neigh
, int type
, int len
)
990 if(unicast_neighbour
) {
991 if(neigh
!= unicast_neighbour
||
992 unicast_buffered
+ len
+ 2 >=
993 MIN(UNICAST_BUFSIZE
, babel_get_if_nfo(neigh
->ifp
)->bufsize
))
997 unicast_buffer
= malloc(UNICAST_BUFSIZE
);
998 if(!unicast_buffer
) {
999 flog_err(EC_BABEL_MEMORY
, "malloc(unicast_buffer): %s",
1000 safe_strerror(errno
));
1004 unicast_neighbour
= neigh
;
1006 unicast_buffer
[unicast_buffered
++] = type
;
1007 unicast_buffer
[unicast_buffered
++] = len
;
1012 end_unicast_message(struct neighbour
*neigh
, int type
, int bytes
)
1014 assert(unicast_neighbour
== neigh
&& unicast_buffered
>= bytes
+ 2 &&
1015 unicast_buffer
[unicast_buffered
- bytes
- 2] == type
&&
1016 unicast_buffer
[unicast_buffered
- bytes
- 1] == bytes
);
1017 schedule_unicast_flush(jitter(babel_get_if_nfo(neigh
->ifp
), 0));
1021 accumulate_unicast_byte(struct neighbour
*neigh
, unsigned char value
)
1023 unicast_buffer
[unicast_buffered
++] = value
;
1027 accumulate_unicast_short(struct neighbour
*neigh
, unsigned short value
)
1029 DO_HTONS(unicast_buffer
+ unicast_buffered
, value
);
1030 unicast_buffered
+= 2;
1034 accumulate_unicast_int(struct neighbour
*neigh
, unsigned int value
)
1036 DO_HTONL(unicast_buffer
+ unicast_buffered
, value
);
1037 unicast_buffered
+= 4;
1041 accumulate_unicast_bytes(struct neighbour
*neigh
,
1042 const unsigned char *value
, unsigned len
)
1044 memcpy(unicast_buffer
+ unicast_buffered
, value
, len
);
1045 unicast_buffered
+= len
;
1049 send_ack(struct neighbour
*neigh
, unsigned short nonce
, unsigned short interval
)
1052 debugf(BABEL_DEBUG_COMMON
,"Sending ack (%04x) to %s on %s.",
1053 nonce
, format_address(neigh
->address
), neigh
->ifp
->name
);
1054 rc
= start_unicast_message(neigh
, MESSAGE_ACK
, 2); if(rc
< 0) return;
1055 accumulate_unicast_short(neigh
, nonce
);
1056 end_unicast_message(neigh
, MESSAGE_ACK
, 2);
1057 /* Roughly yields a value no larger than 3/2, so this meets the deadline */
1058 schedule_unicast_flush(roughly(interval
* 6));
1062 send_hello_noupdate(struct interface
*ifp
, unsigned interval
)
1064 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1065 /* This avoids sending multiple hellos in a single packet, which breaks
1066 link quality estimation. */
1067 if(babel_ifp
->buffered_hello
>= 0)
1070 babel_ifp
->hello_seqno
= seqno_plus(babel_ifp
->hello_seqno
, 1);
1071 set_timeout(&babel_ifp
->hello_timeout
, babel_ifp
->hello_interval
);
1076 debugf(BABEL_DEBUG_COMMON
,"Sending hello %d (%d) to %s.",
1077 babel_ifp
->hello_seqno
, interval
, ifp
->name
);
1079 start_message(ifp
, MESSAGE_HELLO
,
1080 (babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) ? 12 : 6);
1081 babel_ifp
->buffered_hello
= babel_ifp
->buffered
- 2;
1082 accumulate_short(ifp
, 0);
1083 accumulate_short(ifp
, babel_ifp
->hello_seqno
);
1084 accumulate_short(ifp
, interval
> 0xFFFF ? 0xFFFF : interval
);
1085 if(babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) {
1086 /* Sub-TLV containing the local time of emission. We use a
1087 Pad4 sub-TLV, which we'll fill just before sending. */
1088 accumulate_byte(ifp
, SUBTLV_PADN
);
1089 accumulate_byte(ifp
, 4);
1090 accumulate_int(ifp
, 0);
1092 end_message(ifp
, MESSAGE_HELLO
,
1093 (babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) ? 12 : 6);
1097 send_hello(struct interface
*ifp
)
1099 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1100 send_hello_noupdate(ifp
, (babel_ifp
->hello_interval
+ 9) / 10);
1101 /* Send full IHU every 3 hellos, and marginal IHU each time */
1102 if(babel_ifp
->hello_seqno
% 3 == 0)
1103 send_ihu(NULL
, ifp
);
1105 send_marginal_ihu(ifp
);
1109 flush_unicast(int dofree
)
1111 struct sockaddr_in6 sin6
;
1114 if(unicast_buffered
== 0)
1117 if(!if_up(unicast_neighbour
->ifp
))
1120 /* Preserve ordering of messages */
1121 flushbuf(unicast_neighbour
->ifp
);
1123 if(check_bucket(unicast_neighbour
->ifp
)) {
1124 memset(&sin6
, 0, sizeof(sin6
));
1125 sin6
.sin6_family
= AF_INET6
;
1126 memcpy(&sin6
.sin6_addr
, unicast_neighbour
->address
, 16);
1127 sin6
.sin6_port
= htons(protocol_port
);
1128 sin6
.sin6_scope_id
= unicast_neighbour
->ifp
->ifindex
;
1129 DO_HTONS(packet_header
+ 2, unicast_buffered
);
1130 fill_rtt_message(unicast_neighbour
->ifp
);
1131 rc
= babel_send(protocol_socket
,
1132 packet_header
, sizeof(packet_header
),
1133 unicast_buffer
, unicast_buffered
,
1134 (struct sockaddr
*)&sin6
, sizeof(sin6
));
1136 flog_err(EC_BABEL_PACKET
, "send(unicast): %s",
1137 safe_strerror(errno
));
1139 flog_err(EC_BABEL_PACKET
,
1140 "Bucket full, dropping unicast packet to %s if %s.",
1141 format_address(unicast_neighbour
->address
),
1142 unicast_neighbour
->ifp
->name
);
1146 VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer
, UNICAST_BUFSIZE
);
1147 unicast_buffered
= 0;
1148 if(dofree
&& unicast_buffer
) {
1149 free(unicast_buffer
);
1150 unicast_buffer
= NULL
;
1152 unicast_neighbour
= NULL
;
1153 unicast_flush_timeout
.tv_sec
= 0;
1154 unicast_flush_timeout
.tv_usec
= 0;
1158 really_send_update(struct interface
*ifp
,
1159 const unsigned char *id
,
1160 const unsigned char *prefix
, unsigned char plen
,
1161 unsigned short seqno
, unsigned short metric
,
1162 unsigned char *channels
, int channels_len
)
1164 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1165 int add_metric
, v4
, real_plen
, omit
= 0;
1166 const unsigned char *real_prefix
;
1167 unsigned short flags
= 0;
1170 if(diversity_kind
!= DIVERSITY_CHANNEL
)
1173 channels_size
= channels_len
>= 0 ? channels_len
+ 2 : 0;
1178 add_metric
= output_filter(id
, prefix
, plen
, ifp
->ifindex
);
1179 if(add_metric
>= INFINITY
)
1182 metric
= MIN(metric
+ add_metric
, INFINITY
);
1184 ensure_space(ifp
, 20 + 12 + 28);
1186 v4
= plen
>= 96 && v4mapped(prefix
);
1189 if(!babel_ifp
->ipv4
)
1191 if(!babel_ifp
->have_buffered_nh
||
1192 memcmp(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4) != 0) {
1193 start_message(ifp
, MESSAGE_NH
, 6);
1194 accumulate_byte(ifp
, 1);
1195 accumulate_byte(ifp
, 0);
1196 accumulate_bytes(ifp
, babel_ifp
->ipv4
, 4);
1197 end_message(ifp
, MESSAGE_NH
, 6);
1198 memcpy(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4);
1199 babel_ifp
->have_buffered_nh
= 1;
1202 real_prefix
= prefix
+ 12;
1203 real_plen
= plen
- 96;
1205 if(babel_ifp
->have_buffered_prefix
) {
1206 while(omit
< plen
/ 8 &&
1207 babel_ifp
->buffered_prefix
[omit
] == prefix
[omit
])
1210 if(!babel_ifp
->have_buffered_prefix
|| plen
>= 48)
1212 real_prefix
= prefix
;
1216 if(!babel_ifp
->have_buffered_id
1217 || memcmp(id
, babel_ifp
->buffered_id
, 8) != 0) {
1218 if(real_plen
== 128 && memcmp(real_prefix
+ 8, id
, 8) == 0) {
1221 start_message(ifp
, MESSAGE_ROUTER_ID
, 10);
1222 accumulate_short(ifp
, 0);
1223 accumulate_bytes(ifp
, id
, 8);
1224 end_message(ifp
, MESSAGE_ROUTER_ID
, 10);
1226 memcpy(babel_ifp
->buffered_id
, id
, sizeof(babel_ifp
->buffered_id
));
1227 babel_ifp
->have_buffered_id
= 1;
1230 start_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
1232 accumulate_byte(ifp
, v4
? 1 : 2);
1233 accumulate_byte(ifp
, flags
);
1234 accumulate_byte(ifp
, real_plen
);
1235 accumulate_byte(ifp
, omit
);
1236 accumulate_short(ifp
, (babel_ifp
->update_interval
+ 5) / 10);
1237 accumulate_short(ifp
, seqno
);
1238 accumulate_short(ifp
, metric
);
1239 accumulate_bytes(ifp
, real_prefix
+ omit
, (real_plen
+ 7) / 8 - omit
);
1240 /* Note that an empty channels TLV is different from no such TLV. */
1241 if(channels_len
>= 0) {
1242 accumulate_byte(ifp
, 2);
1243 accumulate_byte(ifp
, channels_len
);
1245 if (channels
&& channels_len
> 0)
1246 accumulate_bytes(ifp
, channels
, channels_len
);
1248 end_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
1252 memcpy(babel_ifp
->buffered_prefix
, prefix
, 16);
1253 babel_ifp
->have_buffered_prefix
= 1;
1258 compare_buffered_updates(const void *av
, const void *bv
)
1260 const struct buffered_update
*a
= av
, *b
= bv
;
1261 int rc
, v4a
, v4b
, ma
, mb
;
1263 rc
= memcmp(a
->id
, b
->id
, 8);
1267 v4a
= (a
->plen
>= 96 && v4mapped(a
->prefix
));
1268 v4b
= (b
->plen
>= 96 && v4mapped(b
->prefix
));
1275 ma
= (!v4a
&& a
->plen
== 128 && memcmp(a
->prefix
+ 8, a
->id
, 8) == 0);
1276 mb
= (!v4b
&& b
->plen
== 128 && memcmp(b
->prefix
+ 8, b
->id
, 8) == 0);
1283 if(a
->plen
< b
->plen
)
1285 else if(a
->plen
> b
->plen
)
1288 return memcmp(a
->prefix
, b
->prefix
, 16);
1292 flushupdates(struct interface
*ifp
)
1294 babel_interface_nfo
*babel_ifp
= NULL
;
1295 struct xroute
*xroute
;
1296 struct babel_route
*route
;
1297 const unsigned char *last_prefix
= NULL
;
1298 unsigned char last_plen
= 0xFF;
1302 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1303 struct interface
*ifp_aux
;
1304 FOR_ALL_INTERFACES(vrf
, ifp_aux
)
1305 flushupdates(ifp_aux
);
1309 babel_ifp
= babel_get_if_nfo(ifp
);
1310 if(babel_ifp
->num_buffered_updates
> 0) {
1311 struct buffered_update
*b
= babel_ifp
->buffered_updates
;
1312 int n
= babel_ifp
->num_buffered_updates
;
1314 babel_ifp
->buffered_updates
= NULL
;
1315 babel_ifp
->update_bufsize
= 0;
1316 babel_ifp
->num_buffered_updates
= 0;
1321 debugf(BABEL_DEBUG_COMMON
," (flushing %d buffered updates on %s (%d))",
1322 n
, ifp
->name
, ifp
->ifindex
);
1324 /* In order to send fewer update messages, we want to send updates
1325 with the same router-id together, with IPv6 going out before IPv4. */
1327 for(i
= 0; i
< n
; i
++) {
1328 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
1330 memcpy(b
[i
].id
, route
->src
->id
, 8);
1332 memcpy(b
[i
].id
, myid
, 8);
1335 qsort(b
, n
, sizeof(struct buffered_update
), compare_buffered_updates
);
1337 for(i
= 0; i
< n
; i
++) {
1338 /* The same update may be scheduled multiple times before it is
1339 sent out. Since our buffer is now sorted, it is enough to
1340 compare with the previous update. */
1343 if(b
[i
].plen
== last_plen
&&
1344 memcmp(b
[i
].prefix
, last_prefix
, 16) == 0)
1348 xroute
= find_xroute(b
[i
].prefix
, b
[i
].plen
);
1349 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
1351 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1352 really_send_update(ifp
, myid
,
1353 xroute
->prefix
, xroute
->plen
,
1354 myseqno
, xroute
->metric
,
1356 last_prefix
= xroute
->prefix
;
1357 last_plen
= xroute
->plen
;
1359 unsigned char channels
[DIVERSITY_HOPS
];
1361 struct interface
*route_ifp
= route
->neigh
->ifp
;
1362 struct babel_interface
*babel_route_ifp
= NULL
;
1363 unsigned short metric
;
1364 unsigned short seqno
;
1366 seqno
= route
->seqno
;
1368 route_interferes(route
, ifp
) ?
1369 route_metric(route
) :
1370 route_metric_noninterfering(route
);
1372 if(metric
< INFINITY
)
1373 satisfy_request(route
->src
->prefix
, route
->src
->plen
,
1374 seqno
, route
->src
->id
, ifp
);
1375 if((babel_ifp
->flags
& BABEL_IF_SPLIT_HORIZON
) &&
1376 route
->neigh
->ifp
== ifp
)
1379 babel_route_ifp
= babel_get_if_nfo(route_ifp
);
1380 if(babel_route_ifp
->channel
==BABEL_IF_CHANNEL_NONINTERFERING
) {
1381 memcpy(channels
, route
->channels
, DIVERSITY_HOPS
);
1383 if(babel_route_ifp
->channel
== BABEL_IF_CHANNEL_UNKNOWN
)
1384 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
1386 assert(babel_route_ifp
->channel
> 0 &&
1387 babel_route_ifp
->channel
<= 255);
1388 channels
[0] = babel_route_ifp
->channel
;
1390 memcpy(channels
+ 1, route
->channels
, DIVERSITY_HOPS
- 1);
1393 chlen
= channels_len(channels
);
1394 really_send_update(ifp
, route
->src
->id
,
1399 update_source(route
->src
, seqno
, metric
);
1400 last_prefix
= route
->src
->prefix
;
1401 last_plen
= route
->src
->plen
;
1403 /* There's no route for this prefix. This can happen shortly
1404 after an xroute has been retracted, so send a retraction. */
1405 really_send_update(ifp
, myid
, b
[i
].prefix
, b
[i
].plen
,
1406 myseqno
, INFINITY
, NULL
, -1);
1409 schedule_flush_now(ifp
);
1413 babel_ifp
->update_flush_timeout
.tv_sec
= 0;
1414 babel_ifp
->update_flush_timeout
.tv_usec
= 0;
1418 schedule_update_flush(struct interface
*ifp
, int urgent
)
1420 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1422 msecs
= update_jitter(babel_ifp
, urgent
);
1423 if(babel_ifp
->update_flush_timeout
.tv_sec
!= 0 &&
1424 timeval_minus_msec(&babel_ifp
->update_flush_timeout
, &babel_now
) < msecs
)
1426 set_timeout(&babel_ifp
->update_flush_timeout
, msecs
);
1430 buffer_update(struct interface
*ifp
,
1431 const unsigned char *prefix
, unsigned char plen
)
1433 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1434 if(babel_ifp
->num_buffered_updates
> 0 &&
1435 babel_ifp
->num_buffered_updates
>= babel_ifp
->update_bufsize
)
1438 if(babel_ifp
->update_bufsize
== 0) {
1440 assert(babel_ifp
->buffered_updates
== NULL
);
1441 /* Allocate enough space to hold a full update. Since the
1442 number of installed routes will grow over time, make sure we
1443 have enough space to send a full-ish frame. */
1444 n
= installed_routes_estimate() + xroutes_estimate() + 4;
1445 n
= MAX(n
, babel_ifp
->bufsize
/ 16);
1447 babel_ifp
->buffered_updates
= malloc(n
*sizeof(struct buffered_update
));
1448 if(babel_ifp
->buffered_updates
== NULL
) {
1449 flog_err(EC_BABEL_MEMORY
, "malloc(buffered_updates): %s",
1450 safe_strerror(errno
));
1452 /* Try again with a tiny buffer. */
1458 babel_ifp
->update_bufsize
= n
;
1459 babel_ifp
->num_buffered_updates
= 0;
1462 memcpy(babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].prefix
,
1464 babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].plen
= plen
;
1465 babel_ifp
->num_buffered_updates
++;
1469 send_update(struct interface
*ifp
, int urgent
,
1470 const unsigned char *prefix
, unsigned char plen
)
1472 babel_interface_nfo
*babel_ifp
= NULL
;
1475 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1476 struct interface
*ifp_aux
;
1477 struct babel_route
*route
;
1478 FOR_ALL_INTERFACES(vrf
, ifp_aux
)
1479 send_update(ifp_aux
, urgent
, prefix
, plen
);
1481 /* Since flushupdates only deals with non-wildcard interfaces, we
1482 need to do this now. */
1483 route
= find_installed_route(prefix
, plen
);
1484 if(route
&& route_metric(route
) < INFINITY
)
1485 satisfy_request(prefix
, plen
, route
->src
->seqno
, route
->src
->id
,
1494 babel_ifp
= babel_get_if_nfo(ifp
);
1496 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for %s.",
1497 ifp
->name
, format_prefix(prefix
, plen
));
1498 buffer_update(ifp
, prefix
, plen
);
1500 struct route_stream
*routes
= NULL
;
1501 send_self_update(ifp
);
1502 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for any.", ifp
->name
);
1503 routes
= route_stream(1);
1506 struct babel_route
*route
= route_stream_next(routes
);
1509 buffer_update(ifp
, route
->src
->prefix
, route
->src
->plen
);
1511 route_stream_done(routes
);
1513 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate route stream.");
1515 set_timeout(&babel_ifp
->update_timeout
, babel_ifp
->update_interval
);
1516 babel_ifp
->last_update_time
= babel_now
.tv_sec
;
1518 schedule_update_flush(ifp
, urgent
);
1522 send_update_resend(struct interface
*ifp
,
1523 const unsigned char *prefix
, unsigned char plen
)
1525 assert(prefix
!= NULL
);
1527 send_update(ifp
, 1, prefix
, plen
);
1528 record_resend(RESEND_UPDATE
, prefix
, plen
, 0, NULL
, NULL
, resend_delay
);
1532 send_wildcard_retraction(struct interface
*ifp
)
1534 babel_interface_nfo
*babel_ifp
= NULL
;
1536 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1537 struct interface
*ifp_aux
;
1538 FOR_ALL_INTERFACES(vrf
, ifp_aux
)
1539 send_wildcard_retraction(ifp_aux
);
1546 babel_ifp
= babel_get_if_nfo(ifp
);
1547 start_message(ifp
, MESSAGE_UPDATE
, 10);
1548 accumulate_byte(ifp
, 0);
1549 accumulate_byte(ifp
, 0x40);
1550 accumulate_byte(ifp
, 0);
1551 accumulate_byte(ifp
, 0);
1552 accumulate_short(ifp
, 0xFFFF);
1553 accumulate_short(ifp
, myseqno
);
1554 accumulate_short(ifp
, 0xFFFF);
1555 end_message(ifp
, MESSAGE_UPDATE
, 10);
1557 babel_ifp
->have_buffered_id
= 0;
1561 update_myseqno(void)
1563 myseqno
= seqno_plus(myseqno
, 1);
1567 send_self_update(struct interface
*ifp
)
1569 struct xroute_stream
*xroutes
;
1571 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1572 struct interface
*ifp_aux
;
1573 FOR_ALL_INTERFACES(vrf
, ifp_aux
) {
1576 send_self_update(ifp_aux
);
1581 debugf(BABEL_DEBUG_COMMON
,"Sending self update to %s.", ifp
->name
);
1582 xroutes
= xroute_stream();
1585 struct xroute
*xroute
= xroute_stream_next(xroutes
);
1586 if(xroute
== NULL
) break;
1587 send_update(ifp
, 0, xroute
->prefix
, xroute
->plen
);
1589 xroute_stream_done(xroutes
);
1591 flog_err(EC_BABEL_MEMORY
, "Couldn't allocate xroute stream.");
1596 send_ihu(struct neighbour
*neigh
, struct interface
*ifp
)
1598 babel_interface_nfo
*babel_ifp
= NULL
;
1599 int rxcost
, interval
;
1604 if(neigh
== NULL
&& ifp
== NULL
) {
1605 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1606 struct interface
*ifp_aux
;
1607 FOR_ALL_INTERFACES(vrf
, ifp_aux
) {
1610 send_ihu(NULL
, ifp_aux
);
1616 struct neighbour
*ngh
;
1617 FOR_ALL_NEIGHBOURS(ngh
) {
1625 if(ifp
&& neigh
->ifp
!= ifp
)
1629 babel_ifp
= babel_get_if_nfo(ifp
);
1633 rxcost
= neighbour_rxcost(neigh
);
1634 interval
= (babel_ifp
->hello_interval
* 3 + 9) / 10;
1636 /* Conceptually, an IHU is a unicast message. We usually send them as
1637 multicast, since this allows aggregation into a single packet and
1638 avoids an ARP exchange. If we already have a unicast message queued
1639 for this neighbour, however, we might as well piggyback the IHU. */
1640 debugf(BABEL_DEBUG_COMMON
,"Sending %sihu %d on %s to %s.",
1641 unicast_neighbour
== neigh
? "unicast " : "",
1644 format_address(neigh
->address
));
1646 ll
= linklocal(neigh
->address
);
1648 if((babel_ifp
->flags
& BABEL_IF_TIMESTAMPS
) && neigh
->hello_send_us
1649 /* Checks whether the RTT data is not too old to be sent. */
1650 && timeval_minus_msec(&babel_now
,
1651 &neigh
->hello_rtt_receive_time
) < 1000000) {
1654 neigh
->hello_send_us
= 0;
1658 /* The length depends on the format of the address, and then an
1659 optional 10-bytes sub-TLV for timestamps (used to compute a RTT). */
1660 msglen
= (ll
? 14 : 22) + (send_rtt_data
? 10 : 0);
1662 if(unicast_neighbour
!= neigh
) {
1663 start_message(ifp
, MESSAGE_IHU
, msglen
);
1664 accumulate_byte(ifp
, ll
? 3 : 2);
1665 accumulate_byte(ifp
, 0);
1666 accumulate_short(ifp
, rxcost
);
1667 accumulate_short(ifp
, interval
);
1669 accumulate_bytes(ifp
, neigh
->address
+ 8, 8);
1671 accumulate_bytes(ifp
, neigh
->address
, 16);
1672 if (send_rtt_data
) {
1673 accumulate_byte(ifp
, SUBTLV_TIMESTAMP
);
1674 accumulate_byte(ifp
, 8);
1675 accumulate_int(ifp
, neigh
->hello_send_us
);
1676 accumulate_int(ifp
, time_us(neigh
->hello_rtt_receive_time
));
1678 end_message(ifp
, MESSAGE_IHU
, msglen
);
1681 rc
= start_unicast_message(neigh
, MESSAGE_IHU
, msglen
);
1683 accumulate_unicast_byte(neigh
, ll
? 3 : 2);
1684 accumulate_unicast_byte(neigh
, 0);
1685 accumulate_unicast_short(neigh
, rxcost
);
1686 accumulate_unicast_short(neigh
, interval
);
1688 accumulate_unicast_bytes(neigh
, neigh
->address
+ 8, 8);
1690 accumulate_unicast_bytes(neigh
, neigh
->address
, 16);
1691 if (send_rtt_data
) {
1692 accumulate_unicast_byte(neigh
, SUBTLV_TIMESTAMP
);
1693 accumulate_unicast_byte(neigh
, 8);
1694 accumulate_unicast_int(neigh
, neigh
->hello_send_us
);
1695 accumulate_unicast_int(neigh
,
1696 time_us(neigh
->hello_rtt_receive_time
));
1698 end_unicast_message(neigh
, MESSAGE_IHU
, msglen
);
1702 /* Send IHUs to all marginal neighbours */
1704 send_marginal_ihu(struct interface
*ifp
)
1706 struct neighbour
*neigh
;
1707 FOR_ALL_NEIGHBOURS(neigh
) {
1708 if(ifp
&& neigh
->ifp
!= ifp
)
1710 if(neigh
->txcost
>= 384 || (neigh
->reach
& 0xF000) != 0xF000)
1711 send_ihu(neigh
, ifp
);
1716 send_request(struct interface
*ifp
,
1717 const unsigned char *prefix
, unsigned char plen
)
1722 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1723 struct interface
*ifp_aux
;
1724 FOR_ALL_INTERFACES(vrf
, ifp_aux
) {
1727 send_request(ifp_aux
, prefix
, plen
);
1732 /* make sure any buffered updates go out before this request. */
1738 debugf(BABEL_DEBUG_COMMON
,"sending request to %s for %s.",
1739 ifp
->name
, prefix
? format_prefix(prefix
, plen
) : "any");
1740 v4
= plen
>= 96 && v4mapped(prefix
);
1741 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1742 len
= !prefix
? 2 : 2 + pb
;
1744 start_message(ifp
, MESSAGE_REQUEST
, len
);
1745 accumulate_byte(ifp
, !prefix
? 0 : v4
? 1 : 2);
1746 accumulate_byte(ifp
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1749 accumulate_bytes(ifp
, prefix
+ 12, pb
);
1751 accumulate_bytes(ifp
, prefix
, pb
);
1753 end_message(ifp
, MESSAGE_REQUEST
, len
);
1757 send_unicast_request(struct neighbour
*neigh
,
1758 const unsigned char *prefix
, unsigned char plen
)
1760 int rc
, v4
, pb
, len
;
1762 /* make sure any buffered updates go out before this request. */
1763 flushupdates(neigh
->ifp
);
1765 debugf(BABEL_DEBUG_COMMON
,"sending unicast request to %s for %s.",
1766 format_address(neigh
->address
),
1767 prefix
? format_prefix(prefix
, plen
) : "any");
1768 v4
= plen
>= 96 && v4mapped(prefix
);
1769 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1770 len
= !prefix
? 2 : 2 + pb
;
1772 rc
= start_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1774 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? 1 : 2);
1775 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1778 accumulate_unicast_bytes(neigh
, prefix
+ 12, pb
);
1780 accumulate_unicast_bytes(neigh
, prefix
, pb
);
1782 end_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1786 send_multihop_request(struct interface
*ifp
,
1787 const unsigned char *prefix
, unsigned char plen
,
1788 unsigned short seqno
, const unsigned char *id
,
1789 unsigned short hop_count
)
1793 /* Make sure any buffered updates go out before this request. */
1797 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1798 struct interface
*ifp_aux
;
1799 FOR_ALL_INTERFACES(vrf
, ifp_aux
) {
1802 send_multihop_request(ifp_aux
, prefix
, plen
, seqno
, id
, hop_count
);
1810 debugf(BABEL_DEBUG_COMMON
,"Sending request (%d) on %s for %s.",
1811 hop_count
, ifp
->name
, format_prefix(prefix
, plen
));
1812 v4
= plen
>= 96 && v4mapped(prefix
);
1813 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1816 start_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1817 accumulate_byte(ifp
, v4
? 1 : 2);
1818 accumulate_byte(ifp
, v4
? plen
- 96 : plen
);
1819 accumulate_short(ifp
, seqno
);
1820 accumulate_byte(ifp
, hop_count
);
1821 accumulate_byte(ifp
, 0);
1822 accumulate_bytes(ifp
, id
, 8);
1825 accumulate_bytes(ifp
, prefix
+ 12, pb
);
1827 accumulate_bytes(ifp
, prefix
, pb
);
1829 end_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1833 send_unicast_multihop_request(struct neighbour
*neigh
,
1834 const unsigned char *prefix
, unsigned char plen
,
1835 unsigned short seqno
, const unsigned char *id
,
1836 unsigned short hop_count
)
1838 int rc
, v4
, pb
, len
;
1840 /* Make sure any buffered updates go out before this request. */
1841 flushupdates(neigh
->ifp
);
1843 debugf(BABEL_DEBUG_COMMON
,"Sending multi-hop request to %s for %s (%d hops).",
1844 format_address(neigh
->address
),
1845 format_prefix(prefix
, plen
), hop_count
);
1846 v4
= plen
>= 96 && v4mapped(prefix
);
1847 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1850 rc
= start_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1852 accumulate_unicast_byte(neigh
, v4
? 1 : 2);
1853 accumulate_unicast_byte(neigh
, v4
? plen
- 96 : plen
);
1854 accumulate_unicast_short(neigh
, seqno
);
1855 accumulate_unicast_byte(neigh
, hop_count
);
1856 accumulate_unicast_byte(neigh
, 0);
1857 accumulate_unicast_bytes(neigh
, id
, 8);
1860 accumulate_unicast_bytes(neigh
, prefix
+ 12, pb
);
1862 accumulate_unicast_bytes(neigh
, prefix
, pb
);
1864 end_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1868 send_request_resend(struct neighbour
*neigh
,
1869 const unsigned char *prefix
, unsigned char plen
,
1870 unsigned short seqno
, unsigned char *id
)
1873 send_unicast_multihop_request(neigh
, prefix
, plen
, seqno
, id
, 127);
1875 send_multihop_request(NULL
, prefix
, plen
, seqno
, id
, 127);
1877 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,
1878 neigh
? neigh
->ifp
: NULL
, resend_delay
);
1882 handle_request(struct neighbour
*neigh
, const unsigned char *prefix
,
1883 unsigned char plen
, unsigned char hop_count
,
1884 unsigned short seqno
, const unsigned char *id
)
1886 struct xroute
*xroute
;
1887 struct babel_route
*route
;
1888 struct neighbour
*successor
= NULL
;
1890 xroute
= find_xroute(prefix
, plen
);
1891 route
= find_installed_route(prefix
, plen
);
1893 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1894 if(hop_count
> 0 && memcmp(id
, myid
, 8) == 0) {
1895 if(seqno_compare(seqno
, myseqno
) > 0) {
1896 if(seqno_minus(seqno
, myseqno
) > 100) {
1897 /* Hopelessly out-of-date request */
1903 send_update(neigh
->ifp
, 1, prefix
, plen
);
1908 (memcmp(id
, route
->src
->id
, 8) != 0 ||
1909 seqno_compare(seqno
, route
->seqno
) <= 0)) {
1910 send_update(neigh
->ifp
, 1, prefix
, plen
);
1917 if(route
&& memcmp(id
, route
->src
->id
, 8) == 0 &&
1918 seqno_minus(seqno
, route
->seqno
) > 100) {
1919 /* Hopelessly out-of-date */
1923 if(request_redundant(neigh
->ifp
, prefix
, plen
, seqno
, id
))
1926 /* Let's try to forward this request. */
1927 if(route
&& route_metric(route
) < INFINITY
)
1928 successor
= route
->neigh
;
1930 if(!successor
|| successor
== neigh
) {
1931 /* We were about to forward a request to its requestor. Try to
1932 find a different neighbour to forward the request to. */
1933 struct babel_route
*other_route
;
1935 other_route
= find_best_route(prefix
, plen
, 0, neigh
);
1936 if(other_route
&& route_metric(other_route
) < INFINITY
)
1937 successor
= other_route
->neigh
;
1940 if(!successor
|| successor
== neigh
)
1944 send_unicast_multihop_request(successor
, prefix
, plen
, seqno
, id
,
1946 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,