2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 * This file incorporates work covered by the following copyright and
19 Copyright (c) 2007, 2008 by Juliusz Chroboczek
21 Permission is hereby granted, free of charge, to any person obtaining a copy
22 of this software and associated documentation files (the "Software"), to deal
23 in the Software without restriction, including without limitation the rights
24 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 copies of the Software, and to permit persons to whom the Software is
26 furnished to do so, subject to the following conditions:
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 #include "babel_interface.h"
48 #include "neighbour.h"
55 unsigned char packet_header
[4] = {42, 2};
58 int split_horizon
= 1;
60 unsigned short myseqno
= 0;
61 struct timeval seqno_time
= {0, 0};
63 #define UNICAST_BUFSIZE 1024
64 int unicast_buffered
= 0;
65 unsigned char *unicast_buffer
= NULL
;
66 struct neighbour
*unicast_neighbour
= NULL
;
67 struct timeval unicast_flush_timeout
= {0, 0};
69 static const unsigned char v4prefix
[16] =
70 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
72 /* Parse a network prefix, encoded in the somewhat baroque compressed
73 representation used by Babel. Return the number of bytes parsed. */
75 network_prefix(int ae
, int plen
, unsigned int omitted
,
76 const unsigned char *p
, const unsigned char *dp
,
77 unsigned int len
, unsigned char *p_r
)
80 unsigned char prefix
[16];
93 memset(prefix
, 0, 16);
100 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
102 memcpy(prefix
, v4prefix
, 12);
104 if (dp
== NULL
|| !v4mapped(dp
)) return -1;
105 memcpy(prefix
, dp
, 12 + omitted
);
107 if(pb
> omitted
) memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
111 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
)) return -1;
113 if (dp
== NULL
|| v4mapped(dp
)) return -1;
114 memcpy(prefix
, dp
, omitted
);
116 if(pb
> omitted
) memcpy(prefix
+ omitted
, p
, pb
- omitted
);
120 if(pb
> 8 && len
< pb
- 8) return -1;
123 if(pb
> 8) memcpy(prefix
+ 8, p
, pb
- 8);
130 mask_prefix(p_r
, prefix
, plen
< 0 ? 128 : ae
== 1 ? plen
+ 96 : plen
);
135 parse_route_attributes(const unsigned char *a
, int alen
,
136 unsigned char *channels
)
138 int type
, len
, i
= 0;
148 fprintf(stderr
, "Received truncated attributes.\n");
153 fprintf(stderr
, "Received truncated attributes.\n");
159 } else if(type
== 2) {
160 if(len
> DIVERSITY_HOPS
) {
162 "Received overlong channel information (%d > %d).\n",
163 len
, DIVERSITY_HOPS
);
164 len
= DIVERSITY_HOPS
;
166 if(memchr(a
+ i
+ 2, 0, len
) != NULL
) {
168 fprintf(stderr
, "Channel information contains 0!");
171 memset(channels
, 0, DIVERSITY_HOPS
);
172 memcpy(channels
, a
+ i
+ 2, len
);
174 fprintf(stderr
, "Received unknown route attribute %d.\n", type
);
182 network_address(int ae
, const unsigned char *a
, unsigned int len
,
185 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
189 channels_len(unsigned char *channels
)
191 unsigned char *p
= memchr(channels
, 0, DIVERSITY_HOPS
);
192 return p
? (p
- channels
) : DIVERSITY_HOPS
;
196 parse_packet(const unsigned char *from
, struct interface
*ifp
,
197 const unsigned char *packet
, int packetlen
)
200 const unsigned char *message
;
201 unsigned char type
, len
;
203 struct neighbour
*neigh
;
204 int have_router_id
= 0, have_v4_prefix
= 0, have_v6_prefix
= 0,
205 have_v4_nh
= 0, have_v6_nh
= 0;
206 unsigned char router_id
[8], v4_prefix
[16], v6_prefix
[16],
207 v4_nh
[16], v6_nh
[16];
209 if(!linklocal(from
)) {
210 zlog_err("Received packet from non-local address %s.",
211 format_address(from
));
215 if(packet
[0] != 42) {
216 zlog_err("Received malformed packet on %s from %s.",
217 ifp
->name
, format_address(from
));
222 zlog_err("Received packet with unknown version %d on %s from %s.",
223 packet
[1], ifp
->name
, format_address(from
));
227 neigh
= find_neighbour(from
, ifp
);
229 zlog_err("Couldn't allocate neighbour.");
233 DO_NTOHS(bodylen
, packet
+ 2);
235 if(bodylen
+ 4 > packetlen
) {
236 zlog_err("Received truncated packet (%d + 4 > %d).",
238 bodylen
= packetlen
- 4;
243 message
= packet
+ 4 + i
;
245 if(type
== MESSAGE_PAD1
) {
246 debugf(BABEL_DEBUG_COMMON
,"Received pad1 from %s on %s.",
247 format_address(from
), ifp
->name
);
251 if(i
+ 1 > bodylen
) {
252 zlog_err("Received truncated message.");
256 if(i
+ len
> bodylen
) {
257 zlog_err("Received truncated message.");
261 if(type
== MESSAGE_PADN
) {
262 debugf(BABEL_DEBUG_COMMON
,"Received pad%d from %s on %s.",
263 len
, format_address(from
), ifp
->name
);
264 } else if(type
== MESSAGE_ACK_REQ
) {
265 unsigned short nonce
, interval
;
266 if(len
< 6) goto fail
;
267 DO_NTOHS(nonce
, message
+ 4);
268 DO_NTOHS(interval
, message
+ 6);
269 debugf(BABEL_DEBUG_COMMON
,"Received ack-req (%04X %d) from %s on %s.",
270 nonce
, interval
, format_address(from
), ifp
->name
);
271 send_ack(neigh
, nonce
, interval
);
272 } else if(type
== MESSAGE_ACK
) {
273 debugf(BABEL_DEBUG_COMMON
,"Received ack from %s on %s.",
274 format_address(from
), ifp
->name
);
275 /* Nothing right now */
276 } else if(type
== MESSAGE_HELLO
) {
277 unsigned short seqno
, interval
;
279 if(len
< 6) goto fail
;
280 DO_NTOHS(seqno
, message
+ 4);
281 DO_NTOHS(interval
, message
+ 6);
282 debugf(BABEL_DEBUG_COMMON
,"Received hello %d (%d) from %s on %s.",
284 format_address(from
), ifp
->name
);
285 babel_get_if_nfo(ifp
)->activity_time
= babel_now
.tv_sec
;
286 changed
= update_neighbour(neigh
, seqno
, interval
);
287 update_neighbour_metric(neigh
, changed
);
289 schedule_neighbours_check(interval
* 10, 0);
290 } else if(type
== MESSAGE_IHU
) {
291 unsigned short txcost
, interval
;
292 unsigned char address
[16];
294 if(len
< 6) goto fail
;
295 DO_NTOHS(txcost
, message
+ 4);
296 DO_NTOHS(interval
, message
+ 6);
297 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
298 if(rc
< 0) goto fail
;
299 debugf(BABEL_DEBUG_COMMON
,"Received ihu %d (%d) from %s on %s for %s.",
301 format_address(from
), ifp
->name
,
302 format_address(address
));
303 if(message
[2] == 0 || is_interface_ll_address(ifp
, address
)) {
304 int changed
= txcost
!= neigh
->txcost
;
305 neigh
->txcost
= txcost
;
306 neigh
->ihu_time
= babel_now
;
307 neigh
->ihu_interval
= interval
;
308 update_neighbour_metric(neigh
, changed
);
310 schedule_neighbours_check(interval
* 10 * 3, 0);
312 } else if(type
== MESSAGE_ROUTER_ID
) {
317 memcpy(router_id
, message
+ 4, 8);
319 debugf(BABEL_DEBUG_COMMON
,"Received router-id %s from %s on %s.",
320 format_eui64(router_id
), format_address(from
), ifp
->name
);
321 } else if(type
== MESSAGE_NH
) {
322 unsigned char nh
[16];
329 rc
= network_address(message
[2], message
+ 4, len
- 2,
336 debugf(BABEL_DEBUG_COMMON
,"Received nh %s (%d) from %s on %s.",
337 format_address(nh
), message
[2],
338 format_address(from
), ifp
->name
);
339 if(message
[2] == 1) {
340 memcpy(v4_nh
, nh
, 16);
343 memcpy(v6_nh
, nh
, 16);
346 } else if(type
== MESSAGE_UPDATE
) {
347 unsigned char prefix
[16], *nh
;
349 unsigned char channels
[DIVERSITY_HOPS
];
350 unsigned short interval
, seqno
, metric
;
353 if(len
< 2 || message
[3] & 0x80)
354 have_v4_prefix
= have_v6_prefix
= 0;
357 DO_NTOHS(interval
, message
+ 6);
358 DO_NTOHS(seqno
, message
+ 8);
359 DO_NTOHS(metric
, message
+ 10);
360 if(message
[5] == 0 ||
361 (message
[3] == 1 ? have_v4_prefix
: have_v6_prefix
))
362 rc
= network_prefix(message
[2], message
[4], message
[5],
364 message
[2] == 1 ? v4_prefix
: v6_prefix
,
369 if(message
[3] & 0x80)
370 have_v4_prefix
= have_v6_prefix
= 0;
373 parsed_len
= 10 + rc
;
375 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
377 if(message
[3] & 0x80) {
378 if(message
[2] == 1) {
379 memcpy(v4_prefix
, prefix
, 16);
382 memcpy(v6_prefix
, prefix
, 16);
386 if(message
[3] & 0x40) {
387 if(message
[2] == 1) {
388 memset(router_id
, 0, 4);
389 memcpy(router_id
+ 4, prefix
+ 12, 4);
391 memcpy(router_id
, prefix
+ 8, 8);
395 if(!have_router_id
&& message
[2] != 0) {
396 zlog_err("Received prefix with no router id.");
399 debugf(BABEL_DEBUG_COMMON
,"Received update%s%s for %s from %s on %s.",
400 (message
[3] & 0x80) ? "/prefix" : "",
401 (message
[3] & 0x40) ? "/id" : "",
402 format_prefix(prefix
, plen
),
403 format_address(from
), ifp
->name
);
405 if(message
[2] == 0) {
406 if(metric
< 0xFFFF) {
407 zlog_err("Received wildcard update with finite metric.");
410 retract_neighbour_routes(neigh
);
412 } else if(message
[2] == 1) {
416 } else if(have_v6_nh
) {
422 if(message
[2] == 1) {
423 if(!babel_get_if_nfo(ifp
)->ipv4
)
427 if((ifp
->flags
& BABEL_IF_FARAWAY
)) {
430 /* This will be overwritten by parse_route_attributes below. */
432 /* Assume non-interfering (wired) link. */
435 /* Assume interfering. */
436 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
441 parse_route_attributes(message
+ 2 + parsed_len
,
442 len
- parsed_len
, channels
);
445 update_route(router_id
, prefix
, plen
, seqno
, metric
, interval
,
447 channels
, channels_len(channels
));
448 } else if(type
== MESSAGE_REQUEST
) {
449 unsigned char prefix
[16], plen
;
451 if(len
< 2) goto fail
;
452 rc
= network_prefix(message
[2], message
[3], 0,
453 message
+ 4, NULL
, len
- 2, prefix
);
454 if(rc
< 0) goto fail
;
455 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
456 debugf(BABEL_DEBUG_COMMON
,"Received request for %s from %s on %s.",
457 message
[2] == 0 ? "any" : format_prefix(prefix
, plen
),
458 format_address(from
), ifp
->name
);
459 if(message
[2] == 0) {
460 struct babel_interface
*babel_ifp
=babel_get_if_nfo(neigh
->ifp
);
461 /* If a neighbour is requesting a full route dump from us,
462 we might as well send it an IHU. */
463 send_ihu(neigh
, NULL
);
464 /* Since nodes send wildcard requests on boot, booting
465 a large number of nodes at the same time may cause an
466 update storm. Ignore a wildcard request that happens
467 shortly after we sent a full update. */
468 if(babel_ifp
->last_update_time
<
469 babel_now
.tv_sec
- MAX(babel_ifp
->hello_interval
/ 100, 1))
470 send_update(neigh
->ifp
, 0, NULL
, 0);
472 send_update(neigh
->ifp
, 0, prefix
, plen
);
474 } else if(type
== MESSAGE_MH_REQUEST
) {
475 unsigned char prefix
[16], plen
;
476 unsigned short seqno
;
478 if(len
< 14) goto fail
;
479 DO_NTOHS(seqno
, message
+ 4);
480 rc
= network_prefix(message
[2], message
[3], 0,
481 message
+ 16, NULL
, len
- 14, prefix
);
482 if(rc
< 0) goto fail
;
483 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
484 debugf(BABEL_DEBUG_COMMON
,"Received request (%d) for %s from %s on %s (%s, %d).",
486 format_prefix(prefix
, plen
),
487 format_address(from
), ifp
->name
,
488 format_eui64(message
+ 8), seqno
);
489 handle_request(neigh
, prefix
, plen
, message
[6],
492 debugf(BABEL_DEBUG_COMMON
,"Received unknown packet type %d from %s on %s.",
493 type
, format_address(from
), ifp
->name
);
500 zlog_err("Couldn't parse packet (%d, %d) from %s on %s.",
501 message
[0], message
[1], format_address(from
), ifp
->name
);
507 /* Under normal circumstances, there are enough moderation mechanisms
508 elsewhere in the protocol to make sure that this last-ditch check
509 should never trigger. But I'm superstitious. */
512 check_bucket(struct interface
*ifp
)
514 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
515 if(babel_ifp
->bucket
<= 0) {
516 int seconds
= babel_now
.tv_sec
- babel_ifp
->bucket_time
;
518 babel_ifp
->bucket
= MIN(BUCKET_TOKENS_MAX
,
519 seconds
* BUCKET_TOKENS_PER_SEC
);
521 /* Reset bucket time unconditionally, in case clock is stepped. */
522 babel_ifp
->bucket_time
= babel_now
.tv_sec
;
525 if(babel_ifp
->bucket
> 0) {
534 flushbuf(struct interface
*ifp
)
537 struct sockaddr_in6 sin6
;
538 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
540 assert(babel_ifp
->buffered
<= babel_ifp
->bufsize
);
544 if(babel_ifp
->buffered
> 0) {
545 debugf(BABEL_DEBUG_COMMON
," (flushing %d buffered bytes on %s)",
546 babel_ifp
->buffered
, ifp
->name
);
547 if(check_bucket(ifp
)) {
548 memset(&sin6
, 0, sizeof(sin6
));
549 sin6
.sin6_family
= AF_INET6
;
550 memcpy(&sin6
.sin6_addr
, protocol_group
, 16);
551 sin6
.sin6_port
= htons(protocol_port
);
552 sin6
.sin6_scope_id
= ifp
->ifindex
;
553 DO_HTONS(packet_header
+ 2, babel_ifp
->buffered
);
554 rc
= babel_send(protocol_socket
,
555 packet_header
, sizeof(packet_header
),
556 babel_ifp
->sendbuf
, babel_ifp
->buffered
,
557 (struct sockaddr
*)&sin6
, sizeof(sin6
));
559 zlog_err("send: %s", safe_strerror(errno
));
561 zlog_err("Warning: bucket full, dropping packet to %s.",
565 VALGRIND_MAKE_MEM_UNDEFINED(babel_ifp
->sendbuf
, babel_ifp
->bufsize
);
566 babel_ifp
->buffered
= 0;
567 babel_ifp
->have_buffered_hello
= 0;
568 babel_ifp
->have_buffered_id
= 0;
569 babel_ifp
->have_buffered_nh
= 0;
570 babel_ifp
->have_buffered_prefix
= 0;
571 babel_ifp
->flush_timeout
.tv_sec
= 0;
572 babel_ifp
->flush_timeout
.tv_usec
= 0;
576 schedule_flush(struct interface
*ifp
)
578 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
579 unsigned msecs
= jitter(babel_ifp
, 0);
580 if(babel_ifp
->flush_timeout
.tv_sec
!= 0 &&
581 timeval_minus_msec(&babel_ifp
->flush_timeout
, &babel_now
) < msecs
)
583 set_timeout(&babel_ifp
->flush_timeout
, msecs
);
587 schedule_flush_now(struct interface
*ifp
)
589 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
591 unsigned msecs
= roughly(10);
592 if(babel_ifp
->flush_timeout
.tv_sec
!= 0 &&
593 timeval_minus_msec(&babel_ifp
->flush_timeout
, &babel_now
) < msecs
)
595 set_timeout(&babel_ifp
->flush_timeout
, msecs
);
599 schedule_unicast_flush(unsigned msecs
)
601 if(!unicast_neighbour
)
603 if(unicast_flush_timeout
.tv_sec
!= 0 &&
604 timeval_minus_msec(&unicast_flush_timeout
, &babel_now
) < msecs
)
606 unicast_flush_timeout
.tv_usec
= (babel_now
.tv_usec
+ msecs
* 1000) %1000000;
607 unicast_flush_timeout
.tv_sec
=
608 babel_now
.tv_sec
+ (babel_now
.tv_usec
/ 1000 + msecs
) / 1000;
612 ensure_space(struct interface
*ifp
, int space
)
614 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
615 if(babel_ifp
->bufsize
- babel_ifp
->buffered
< space
)
620 start_message(struct interface
*ifp
, int type
, int len
)
622 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
623 if(babel_ifp
->bufsize
- babel_ifp
->buffered
< len
+ 2)
625 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = type
;
626 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = len
;
630 end_message(struct interface
*ifp
, int type
, int bytes
)
632 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
633 assert(babel_ifp
->buffered
>= bytes
+ 2 &&
634 babel_ifp
->sendbuf
[babel_ifp
->buffered
- bytes
- 2] == type
&&
635 babel_ifp
->sendbuf
[babel_ifp
->buffered
- bytes
- 1] == bytes
);
640 accumulate_byte(struct interface
*ifp
, unsigned char value
)
642 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
643 babel_ifp
->sendbuf
[babel_ifp
->buffered
++] = value
;
647 accumulate_short(struct interface
*ifp
, unsigned short value
)
649 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
650 DO_HTONS(babel_ifp
->sendbuf
+ babel_ifp
->buffered
, value
);
651 babel_ifp
->buffered
+= 2;
655 accumulate_bytes(struct interface
*ifp
,
656 const unsigned char *value
, unsigned len
)
658 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
659 memcpy(babel_ifp
->sendbuf
+ babel_ifp
->buffered
, value
, len
);
660 babel_ifp
->buffered
+= len
;
664 start_unicast_message(struct neighbour
*neigh
, int type
, int len
)
666 if(unicast_neighbour
) {
667 if(neigh
!= unicast_neighbour
||
668 unicast_buffered
+ len
+ 2 >=
669 MIN(UNICAST_BUFSIZE
, babel_get_if_nfo(neigh
->ifp
)->bufsize
))
673 unicast_buffer
= malloc(UNICAST_BUFSIZE
);
674 if(!unicast_buffer
) {
675 zlog_err("malloc(unicast_buffer): %s", safe_strerror(errno
));
679 unicast_neighbour
= neigh
;
681 unicast_buffer
[unicast_buffered
++] = type
;
682 unicast_buffer
[unicast_buffered
++] = len
;
687 end_unicast_message(struct neighbour
*neigh
, int type
, int bytes
)
689 assert(unicast_neighbour
== neigh
&& unicast_buffered
>= bytes
+ 2 &&
690 unicast_buffer
[unicast_buffered
- bytes
- 2] == type
&&
691 unicast_buffer
[unicast_buffered
- bytes
- 1] == bytes
);
692 schedule_unicast_flush(jitter(babel_get_if_nfo(neigh
->ifp
), 0));
696 accumulate_unicast_byte(struct neighbour
*neigh
, unsigned char value
)
698 unicast_buffer
[unicast_buffered
++] = value
;
702 accumulate_unicast_short(struct neighbour
*neigh
, unsigned short value
)
704 DO_HTONS(unicast_buffer
+ unicast_buffered
, value
);
705 unicast_buffered
+= 2;
709 accumulate_unicast_bytes(struct neighbour
*neigh
,
710 const unsigned char *value
, unsigned len
)
712 memcpy(unicast_buffer
+ unicast_buffered
, value
, len
);
713 unicast_buffered
+= len
;
717 send_ack(struct neighbour
*neigh
, unsigned short nonce
, unsigned short interval
)
720 debugf(BABEL_DEBUG_COMMON
,"Sending ack (%04x) to %s on %s.",
721 nonce
, format_address(neigh
->address
), neigh
->ifp
->name
);
722 rc
= start_unicast_message(neigh
, MESSAGE_ACK
, 2); if(rc
< 0) return;
723 accumulate_unicast_short(neigh
, nonce
);
724 end_unicast_message(neigh
, MESSAGE_ACK
, 2);
725 /* Roughly yields a value no larger than 3/2, so this meets the deadline */
726 schedule_unicast_flush(roughly(interval
* 6));
730 send_hello_noupdate(struct interface
*ifp
, unsigned interval
)
732 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
733 /* This avoids sending multiple hellos in a single packet, which breaks
734 link quality estimation. */
735 if(babel_ifp
->have_buffered_hello
)
738 babel_ifp
->hello_seqno
= seqno_plus(babel_ifp
->hello_seqno
, 1);
739 set_timeout(&babel_ifp
->hello_timeout
, babel_ifp
->hello_interval
);
744 debugf(BABEL_DEBUG_COMMON
,"Sending hello %d (%d) to %s.",
745 babel_ifp
->hello_seqno
, interval
, ifp
->name
);
747 start_message(ifp
, MESSAGE_HELLO
, 6);
748 accumulate_short(ifp
, 0);
749 accumulate_short(ifp
, babel_ifp
->hello_seqno
);
750 accumulate_short(ifp
, interval
> 0xFFFF ? 0xFFFF : interval
);
751 end_message(ifp
, MESSAGE_HELLO
, 6);
752 babel_ifp
->have_buffered_hello
= 1;
756 send_hello(struct interface
*ifp
)
759 changed
= update_hello_interval(ifp
);
760 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
761 send_hello_noupdate(ifp
, (babel_ifp
->hello_interval
+ 9) / 10);
762 /* Send full IHU every 3 hellos, and marginal IHU each time */
763 if(changed
|| babel_ifp
->hello_seqno
% 3 == 0)
766 send_marginal_ihu(ifp
);
770 flush_unicast(int dofree
)
772 struct sockaddr_in6 sin6
;
775 if(unicast_buffered
== 0)
778 if(!if_up(unicast_neighbour
->ifp
))
781 /* Preserve ordering of messages */
782 flushbuf(unicast_neighbour
->ifp
);
784 if(check_bucket(unicast_neighbour
->ifp
)) {
785 memset(&sin6
, 0, sizeof(sin6
));
786 sin6
.sin6_family
= AF_INET6
;
787 memcpy(&sin6
.sin6_addr
, unicast_neighbour
->address
, 16);
788 sin6
.sin6_port
= htons(protocol_port
);
789 sin6
.sin6_scope_id
= unicast_neighbour
->ifp
->ifindex
;
790 DO_HTONS(packet_header
+ 2, unicast_buffered
);
791 rc
= babel_send(protocol_socket
,
792 packet_header
, sizeof(packet_header
),
793 unicast_buffer
, unicast_buffered
,
794 (struct sockaddr
*)&sin6
, sizeof(sin6
));
796 zlog_err("send(unicast): %s", safe_strerror(errno
));
798 zlog_err("Warning: bucket full, dropping unicast packet to %s if %s.",
799 format_address(unicast_neighbour
->address
),
800 unicast_neighbour
->ifp
->name
);
804 VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer
, UNICAST_BUFSIZE
);
805 unicast_buffered
= 0;
806 if(dofree
&& unicast_buffer
) {
807 free(unicast_buffer
);
808 unicast_buffer
= NULL
;
810 unicast_neighbour
= NULL
;
811 unicast_flush_timeout
.tv_sec
= 0;
812 unicast_flush_timeout
.tv_usec
= 0;
816 really_send_update(struct interface
*ifp
,
817 const unsigned char *id
,
818 const unsigned char *prefix
, unsigned char plen
,
819 unsigned short seqno
, unsigned short metric
,
820 unsigned char *channels
, int channels_len
)
822 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
823 int add_metric
, v4
, real_plen
, omit
= 0;
824 const unsigned char *real_prefix
;
825 unsigned short flags
= 0;
828 if(diversity_kind
!= DIVERSITY_CHANNEL
)
831 channels_size
= channels_len
>= 0 ? channels_len
+ 2 : 0;
836 add_metric
= output_filter(id
, prefix
, plen
, ifp
->ifindex
);
837 if(add_metric
>= INFINITY
)
840 metric
= MIN(metric
+ add_metric
, INFINITY
);
842 ensure_space(ifp
, 20 + 12 + 28);
844 v4
= plen
>= 96 && v4mapped(prefix
);
849 if(!babel_ifp
->have_buffered_nh
||
850 memcmp(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4) != 0) {
851 start_message(ifp
, MESSAGE_NH
, 6);
852 accumulate_byte(ifp
, 1);
853 accumulate_byte(ifp
, 0);
854 accumulate_bytes(ifp
, babel_ifp
->ipv4
, 4);
855 end_message(ifp
, MESSAGE_NH
, 6);
856 memcpy(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4);
857 babel_ifp
->have_buffered_nh
= 1;
860 real_prefix
= prefix
+ 12;
861 real_plen
= plen
- 96;
863 if(babel_ifp
->have_buffered_prefix
) {
864 while(omit
< plen
/ 8 &&
865 babel_ifp
->buffered_prefix
[omit
] == prefix
[omit
])
868 if(!babel_ifp
->have_buffered_prefix
|| plen
>= 48)
870 real_prefix
= prefix
;
874 if(!babel_ifp
->have_buffered_id
875 || memcmp(id
, babel_ifp
->buffered_id
, 8) != 0) {
876 if(real_plen
== 128 && memcmp(real_prefix
+ 8, id
, 8) == 0) {
879 start_message(ifp
, MESSAGE_ROUTER_ID
, 10);
880 accumulate_short(ifp
, 0);
881 accumulate_bytes(ifp
, id
, 8);
882 end_message(ifp
, MESSAGE_ROUTER_ID
, 10);
884 memcpy(babel_ifp
->buffered_id
, id
, 16);
885 babel_ifp
->have_buffered_id
= 1;
888 start_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
890 accumulate_byte(ifp
, v4
? 1 : 2);
891 accumulate_byte(ifp
, flags
);
892 accumulate_byte(ifp
, real_plen
);
893 accumulate_byte(ifp
, omit
);
894 accumulate_short(ifp
, (babel_ifp
->update_interval
+ 5) / 10);
895 accumulate_short(ifp
, seqno
);
896 accumulate_short(ifp
, metric
);
897 accumulate_bytes(ifp
, real_prefix
+ omit
, (real_plen
+ 7) / 8 - omit
);
898 /* Note that an empty channels TLV is different from no such TLV. */
899 if(channels_len
>= 0) {
900 accumulate_byte(ifp
, 2);
901 accumulate_byte(ifp
, channels_len
);
902 accumulate_bytes(ifp
, channels
, channels_len
);
904 end_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
908 memcpy(babel_ifp
->buffered_prefix
, prefix
, 16);
909 babel_ifp
->have_buffered_prefix
= 1;
914 compare_buffered_updates(const void *av
, const void *bv
)
916 const struct buffered_update
*a
= av
, *b
= bv
;
917 int rc
, v4a
, v4b
, ma
, mb
;
919 rc
= memcmp(a
->id
, b
->id
, 8);
923 v4a
= (a
->plen
>= 96 && v4mapped(a
->prefix
));
924 v4b
= (b
->plen
>= 96 && v4mapped(b
->prefix
));
931 ma
= (!v4a
&& a
->plen
== 128 && memcmp(a
->prefix
+ 8, a
->id
, 8) == 0);
932 mb
= (!v4b
&& b
->plen
== 128 && memcmp(b
->prefix
+ 8, b
->id
, 8) == 0);
939 if(a
->plen
< b
->plen
)
941 else if(a
->plen
> b
->plen
)
944 return memcmp(a
->prefix
, b
->prefix
, 16);
948 flushupdates(struct interface
*ifp
)
950 babel_interface_nfo
*babel_ifp
= NULL
;
951 struct xroute
*xroute
;
952 struct babel_route
*route
;
953 const unsigned char *last_prefix
= NULL
;
954 unsigned char last_plen
= 0xFF;
958 struct interface
*ifp_aux
;
959 struct listnode
*linklist_node
= NULL
;
960 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
961 flushupdates(ifp_aux
);
965 babel_ifp
= babel_get_if_nfo(ifp
);
966 if(babel_ifp
->num_buffered_updates
> 0) {
967 struct buffered_update
*b
= babel_ifp
->buffered_updates
;
968 int n
= babel_ifp
->num_buffered_updates
;
970 babel_ifp
->buffered_updates
= NULL
;
971 babel_ifp
->update_bufsize
= 0;
972 babel_ifp
->num_buffered_updates
= 0;
977 debugf(BABEL_DEBUG_COMMON
," (flushing %d buffered updates on %s (%d))",
978 n
, ifp
->name
, ifp
->ifindex
);
980 /* In order to send fewer update messages, we want to send updates
981 with the same router-id together, with IPv6 going out before IPv4. */
983 for(i
= 0; i
< n
; i
++) {
984 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
986 memcpy(b
[i
].id
, route
->src
->id
, 8);
988 memcpy(b
[i
].id
, myid
, 8);
991 qsort(b
, n
, sizeof(struct buffered_update
), compare_buffered_updates
);
993 for(i
= 0; i
< n
; i
++) {
994 /* The same update may be scheduled multiple times before it is
995 sent out. Since our buffer is now sorted, it is enough to
996 compare with the previous update. */
999 if(b
[i
].plen
== last_plen
&&
1000 memcmp(b
[i
].prefix
, last_prefix
, 16) == 0)
1004 xroute
= find_xroute(b
[i
].prefix
, b
[i
].plen
);
1005 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
1007 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1008 really_send_update(ifp
, myid
,
1009 xroute
->prefix
, xroute
->plen
,
1010 myseqno
, xroute
->metric
,
1012 last_prefix
= xroute
->prefix
;
1013 last_plen
= xroute
->plen
;
1015 unsigned char channels
[DIVERSITY_HOPS
];
1017 struct interface
*route_ifp
= route
->neigh
->ifp
;
1018 struct babel_interface
*babel_route_ifp
= NULL
;
1019 unsigned short metric
;
1020 unsigned short seqno
;
1022 seqno
= route
->seqno
;
1024 route_interferes(route
, ifp
) ?
1025 route_metric(route
) :
1026 route_metric_noninterfering(route
);
1028 if(metric
< INFINITY
)
1029 satisfy_request(route
->src
->prefix
, route
->src
->plen
,
1030 seqno
, route
->src
->id
, ifp
);
1031 if((babel_ifp
->flags
& BABEL_IF_SPLIT_HORIZON
) &&
1032 route
->neigh
->ifp
== ifp
)
1035 babel_route_ifp
= babel_get_if_nfo(route_ifp
);
1036 if(babel_route_ifp
->channel
==BABEL_IF_CHANNEL_NONINTERFERING
) {
1037 memcpy(channels
, route
->channels
, DIVERSITY_HOPS
);
1039 if(babel_route_ifp
->channel
== BABEL_IF_CHANNEL_UNKNOWN
)
1040 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
1042 assert(babel_route_ifp
->channel
> 0 &&
1043 babel_route_ifp
->channel
<= 255);
1044 channels
[0] = babel_route_ifp
->channel
;
1046 memcpy(channels
+ 1, route
->channels
, DIVERSITY_HOPS
- 1);
1049 chlen
= channels_len(channels
);
1050 really_send_update(ifp
, route
->src
->id
,
1055 update_source(route
->src
, seqno
, metric
);
1056 last_prefix
= route
->src
->prefix
;
1057 last_plen
= route
->src
->plen
;
1059 /* There's no route for this prefix. This can happen shortly
1060 after an xroute has been retracted, so send a retraction. */
1061 really_send_update(ifp
, myid
, b
[i
].prefix
, b
[i
].plen
,
1062 myseqno
, INFINITY
, NULL
, -1);
1065 schedule_flush_now(ifp
);
1069 babel_ifp
->update_flush_timeout
.tv_sec
= 0;
1070 babel_ifp
->update_flush_timeout
.tv_usec
= 0;
1074 schedule_update_flush(struct interface
*ifp
, int urgent
)
1076 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1078 msecs
= update_jitter(babel_ifp
, urgent
);
1079 if(babel_ifp
->update_flush_timeout
.tv_sec
!= 0 &&
1080 timeval_minus_msec(&babel_ifp
->update_flush_timeout
, &babel_now
) < msecs
)
1082 set_timeout(&babel_ifp
->update_flush_timeout
, msecs
);
1086 buffer_update(struct interface
*ifp
,
1087 const unsigned char *prefix
, unsigned char plen
)
1089 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1090 if(babel_ifp
->num_buffered_updates
> 0 &&
1091 babel_ifp
->num_buffered_updates
>= babel_ifp
->update_bufsize
)
1094 if(babel_ifp
->update_bufsize
== 0) {
1096 assert(babel_ifp
->buffered_updates
== NULL
);
1097 /* Allocate enough space to hold a full update. Since the
1098 number of installed routes will grow over time, make sure we
1099 have enough space to send a full-ish frame. */
1100 n
= installed_routes_estimate() + xroutes_estimate() + 4;
1101 n
= MAX(n
, babel_ifp
->bufsize
/ 16);
1103 babel_ifp
->buffered_updates
= malloc(n
*sizeof(struct buffered_update
));
1104 if(babel_ifp
->buffered_updates
== NULL
) {
1105 zlog_err("malloc(buffered_updates): %s", safe_strerror(errno
));
1107 /* Try again with a tiny buffer. */
1113 babel_ifp
->update_bufsize
= n
;
1114 babel_ifp
->num_buffered_updates
= 0;
1117 memcpy(babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].prefix
,
1119 babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].plen
= plen
;
1120 babel_ifp
->num_buffered_updates
++;
1124 buffer_update_callback(struct babel_route
*route
, void *closure
)
1126 buffer_update((struct interface
*)closure
,
1127 route
->src
->prefix
, route
->src
->plen
);
1131 send_update(struct interface
*ifp
, int urgent
,
1132 const unsigned char *prefix
, unsigned char plen
)
1134 babel_interface_nfo
*babel_ifp
= NULL
;
1137 struct interface
*ifp_aux
;
1138 struct listnode
*linklist_node
= NULL
;
1139 struct babel_route
*route
;
1140 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
1141 send_update(ifp_aux
, urgent
, prefix
, plen
);
1143 /* Since flushupdates only deals with non-wildcard interfaces, we
1144 need to do this now. */
1145 route
= find_installed_route(prefix
, plen
);
1146 if(route
&& route_metric(route
) < INFINITY
)
1147 satisfy_request(prefix
, plen
, route
->src
->seqno
, route
->src
->id
,
1156 babel_ifp
= babel_get_if_nfo(ifp
);
1158 if(!parasitic
|| find_xroute(prefix
, plen
)) {
1159 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for %s.",
1160 ifp
->name
, format_prefix(prefix
, plen
));
1161 buffer_update(ifp
, prefix
, plen
);
1164 if(!interface_idle(babel_ifp
)) {
1165 send_self_update(ifp
);
1167 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for any.",
1169 for_all_installed_routes(buffer_update_callback
, ifp
);
1172 set_timeout(&babel_ifp
->update_timeout
, babel_ifp
->update_interval
);
1173 babel_ifp
->last_update_time
= babel_now
.tv_sec
;
1175 schedule_update_flush(ifp
, urgent
);
1179 send_update_resend(struct interface
*ifp
,
1180 const unsigned char *prefix
, unsigned char plen
)
1184 assert(prefix
!= NULL
);
1186 send_update(ifp
, 1, prefix
, plen
);
1189 delay
= MIN(delay
, wireless_hello_interval
/ 2);
1190 delay
= MIN(delay
, wired_hello_interval
/ 2);
1191 delay
= MAX(delay
, 10);
1192 record_resend(RESEND_UPDATE
, prefix
, plen
, 0, 0, NULL
, delay
);
1196 send_wildcard_retraction(struct interface
*ifp
)
1198 babel_interface_nfo
*babel_ifp
= NULL
;
1200 struct interface
*ifp_aux
;
1201 struct listnode
*linklist_node
= NULL
;
1202 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
1203 send_wildcard_retraction(ifp_aux
);
1210 babel_ifp
= babel_get_if_nfo(ifp
);
1211 start_message(ifp
, MESSAGE_UPDATE
, 10);
1212 accumulate_byte(ifp
, 0);
1213 accumulate_byte(ifp
, 0x40);
1214 accumulate_byte(ifp
, 0);
1215 accumulate_byte(ifp
, 0);
1216 accumulate_short(ifp
, 0xFFFF);
1217 accumulate_short(ifp
, myseqno
);
1218 accumulate_short(ifp
, 0xFFFF);
1219 end_message(ifp
, MESSAGE_UPDATE
, 10);
1221 babel_ifp
->have_buffered_id
= 0;
1227 myseqno
= seqno_plus(myseqno
, 1);
1228 seqno_time
= babel_now
;
1232 send_xroute_update_callback(struct xroute
*xroute
, void *closure
)
1234 struct interface
*ifp
= (struct interface
*)closure
;
1235 send_update(ifp
, 0, xroute
->prefix
, xroute
->plen
);
1239 send_self_update(struct interface
*ifp
)
1242 struct interface
*ifp_aux
;
1243 struct listnode
*linklist_node
= NULL
;
1244 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1247 send_self_update(ifp_aux
);
1252 if(!interface_idle(babel_get_if_nfo(ifp
))) {
1253 debugf(BABEL_DEBUG_COMMON
,"Sending self update to %s.", ifp
->name
);
1254 for_all_xroutes(send_xroute_update_callback
, ifp
);
1259 send_ihu(struct neighbour
*neigh
, struct interface
*ifp
)
1261 babel_interface_nfo
*babel_ifp
= NULL
;
1262 int rxcost
, interval
;
1265 if(neigh
== NULL
&& ifp
== NULL
) {
1266 struct interface
*ifp_aux
;
1267 struct listnode
*linklist_node
= NULL
;
1268 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1271 send_ihu(NULL
, ifp_aux
);
1277 struct neighbour
*ngh
;
1278 FOR_ALL_NEIGHBOURS(ngh
) {
1286 if(ifp
&& neigh
->ifp
!= ifp
)
1290 babel_ifp
= babel_get_if_nfo(ifp
);
1294 rxcost
= neighbour_rxcost(neigh
);
1295 interval
= (babel_ifp
->hello_interval
* 3 + 9) / 10;
1297 /* Conceptually, an IHU is a unicast message. We usually send them as
1298 multicast, since this allows aggregation into a single packet and
1299 avoids an ARP exchange. If we already have a unicast message queued
1300 for this neighbour, however, we might as well piggyback the IHU. */
1301 debugf(BABEL_DEBUG_COMMON
,"Sending %sihu %d on %s to %s.",
1302 unicast_neighbour
== neigh
? "unicast " : "",
1305 format_address(neigh
->address
));
1307 ll
= linklocal(neigh
->address
);
1309 if(unicast_neighbour
!= neigh
) {
1310 start_message(ifp
, MESSAGE_IHU
, ll
? 14 : 22);
1311 accumulate_byte(ifp
, ll
? 3 : 2);
1312 accumulate_byte(ifp
, 0);
1313 accumulate_short(ifp
, rxcost
);
1314 accumulate_short(ifp
, interval
);
1316 accumulate_bytes(ifp
, neigh
->address
+ 8, 8);
1318 accumulate_bytes(ifp
, neigh
->address
, 16);
1319 end_message(ifp
, MESSAGE_IHU
, ll
? 14 : 22);
1322 rc
= start_unicast_message(neigh
, MESSAGE_IHU
, ll
? 14 : 22);
1324 accumulate_unicast_byte(neigh
, ll
? 3 : 2);
1325 accumulate_unicast_byte(neigh
, 0);
1326 accumulate_unicast_short(neigh
, rxcost
);
1327 accumulate_unicast_short(neigh
, interval
);
1329 accumulate_unicast_bytes(neigh
, neigh
->address
+ 8, 8);
1331 accumulate_unicast_bytes(neigh
, neigh
->address
, 16);
1332 end_unicast_message(neigh
, MESSAGE_IHU
, ll
? 14 : 22);
1336 /* Send IHUs to all marginal neighbours */
1338 send_marginal_ihu(struct interface
*ifp
)
1340 struct neighbour
*neigh
;
1341 FOR_ALL_NEIGHBOURS(neigh
) {
1342 if(ifp
&& neigh
->ifp
!= ifp
)
1344 if(neigh
->txcost
>= 384 || (neigh
->reach
& 0xF000) != 0xF000)
1345 send_ihu(neigh
, ifp
);
1350 send_request(struct interface
*ifp
,
1351 const unsigned char *prefix
, unsigned char plen
)
1356 struct interface
*ifp_aux
;
1357 struct listnode
*linklist_node
= NULL
;
1358 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1361 send_request(ifp_aux
, prefix
, plen
);
1366 /* make sure any buffered updates go out before this request. */
1372 debugf(BABEL_DEBUG_COMMON
,"sending request to %s for %s.",
1373 ifp
->name
, prefix
? format_prefix(prefix
, plen
) : "any");
1374 v4
= plen
>= 96 && v4mapped(prefix
);
1375 len
= !prefix
? 2 : v4
? 6 : 18;
1377 start_message(ifp
, MESSAGE_REQUEST
, len
);
1378 accumulate_byte(ifp
, !prefix
? 0 : v4
? 1 : 2);
1379 accumulate_byte(ifp
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1382 accumulate_bytes(ifp
, prefix
+ 12, 4);
1384 accumulate_bytes(ifp
, prefix
, 16);
1386 end_message(ifp
, MESSAGE_REQUEST
, len
);
1390 send_unicast_request(struct neighbour
*neigh
,
1391 const unsigned char *prefix
, unsigned char plen
)
1395 /* make sure any buffered updates go out before this request. */
1396 flushupdates(neigh
->ifp
);
1398 debugf(BABEL_DEBUG_COMMON
,"sending unicast request to %s for %s.",
1399 format_address(neigh
->address
),
1400 prefix
? format_prefix(prefix
, plen
) : "any");
1401 v4
= plen
>= 96 && v4mapped(prefix
);
1402 len
= !prefix
? 2 : v4
? 6 : 18;
1404 rc
= start_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1406 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? 1 : 2);
1407 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1410 accumulate_unicast_bytes(neigh
, prefix
+ 12, 4);
1412 accumulate_unicast_bytes(neigh
, prefix
, 16);
1414 end_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1418 send_multihop_request(struct interface
*ifp
,
1419 const unsigned char *prefix
, unsigned char plen
,
1420 unsigned short seqno
, const unsigned char *id
,
1421 unsigned short hop_count
)
1425 /* Make sure any buffered updates go out before this request. */
1429 struct interface
*ifp_aux
;
1430 struct listnode
*linklist_node
= NULL
;
1431 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1434 send_multihop_request(ifp_aux
, prefix
, plen
, seqno
, id
, hop_count
);
1442 debugf(BABEL_DEBUG_COMMON
,"Sending request (%d) on %s for %s.",
1443 hop_count
, ifp
->name
, format_prefix(prefix
, plen
));
1444 v4
= plen
>= 96 && v4mapped(prefix
);
1445 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1448 start_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1449 accumulate_byte(ifp
, v4
? 1 : 2);
1450 accumulate_byte(ifp
, v4
? plen
- 96 : plen
);
1451 accumulate_short(ifp
, seqno
);
1452 accumulate_byte(ifp
, hop_count
);
1453 accumulate_byte(ifp
, 0);
1454 accumulate_bytes(ifp
, id
, 8);
1457 accumulate_bytes(ifp
, prefix
+ 12, pb
);
1459 accumulate_bytes(ifp
, prefix
, pb
);
1461 end_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1465 send_unicast_multihop_request(struct neighbour
*neigh
,
1466 const unsigned char *prefix
, unsigned char plen
,
1467 unsigned short seqno
, const unsigned char *id
,
1468 unsigned short hop_count
)
1470 int rc
, v4
, pb
, len
;
1472 /* Make sure any buffered updates go out before this request. */
1473 flushupdates(neigh
->ifp
);
1475 debugf(BABEL_DEBUG_COMMON
,"Sending multi-hop request to %s for %s (%d hops).",
1476 format_address(neigh
->address
),
1477 format_prefix(prefix
, plen
), hop_count
);
1478 v4
= plen
>= 96 && v4mapped(prefix
);
1479 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1482 rc
= start_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1484 accumulate_unicast_byte(neigh
, v4
? 1 : 2);
1485 accumulate_unicast_byte(neigh
, v4
? plen
- 96 : plen
);
1486 accumulate_unicast_short(neigh
, seqno
);
1487 accumulate_unicast_byte(neigh
, hop_count
);
1488 accumulate_unicast_byte(neigh
, 0);
1489 accumulate_unicast_bytes(neigh
, id
, 8);
1492 accumulate_unicast_bytes(neigh
, prefix
+ 12, pb
);
1494 accumulate_unicast_bytes(neigh
, prefix
, pb
);
1496 end_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1500 send_request_resend(struct neighbour
*neigh
,
1501 const unsigned char *prefix
, unsigned char plen
,
1502 unsigned short seqno
, unsigned char *id
)
1507 send_unicast_multihop_request(neigh
, prefix
, plen
, seqno
, id
, 127);
1509 send_multihop_request(NULL
, prefix
, plen
, seqno
, id
, 127);
1512 delay
= MIN(delay
, wireless_hello_interval
/ 2);
1513 delay
= MIN(delay
, wired_hello_interval
/ 2);
1514 delay
= MAX(delay
, 10);
1515 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,
1516 neigh
? neigh
->ifp
: NULL
, delay
);
1520 handle_request(struct neighbour
*neigh
, const unsigned char *prefix
,
1521 unsigned char plen
, unsigned char hop_count
,
1522 unsigned short seqno
, const unsigned char *id
)
1524 struct xroute
*xroute
;
1525 struct babel_route
*route
;
1526 struct neighbour
*successor
= NULL
;
1528 xroute
= find_xroute(prefix
, plen
);
1529 route
= find_installed_route(prefix
, plen
);
1531 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1532 if(hop_count
> 0 && memcmp(id
, myid
, 8) == 0) {
1533 if(seqno_compare(seqno
, myseqno
) > 0) {
1534 if(seqno_minus(seqno
, myseqno
) > 100) {
1535 /* Hopelessly out-of-date request */
1541 send_update(neigh
->ifp
, 1, prefix
, plen
);
1546 (memcmp(id
, route
->src
->id
, 8) != 0 ||
1547 seqno_compare(seqno
, route
->seqno
) <= 0)) {
1548 send_update(neigh
->ifp
, 1, prefix
, plen
);
1555 if(route
&& memcmp(id
, route
->src
->id
, 8) == 0 &&
1556 seqno_minus(seqno
, route
->seqno
) > 100) {
1557 /* Hopelessly out-of-date */
1561 if(request_redundant(neigh
->ifp
, prefix
, plen
, seqno
, id
))
1564 /* Let's try to forward this request. */
1565 if(route
&& route_metric(route
) < INFINITY
)
1566 successor
= route
->neigh
;
1568 if(!successor
|| successor
== neigh
) {
1569 /* We were about to forward a request to its requestor. Try to
1570 find a different neighbour to forward the request to. */
1571 struct babel_route
*other_route
;
1573 other_route
= find_best_route(prefix
, plen
, 0, neigh
);
1574 if(other_route
&& route_metric(other_route
) < INFINITY
)
1575 successor
= other_route
->neigh
;
1578 if(!successor
|| successor
== neigh
)
1582 send_unicast_multihop_request(successor
, prefix
, plen
, seqno
, id
,
1584 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,