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 changed
= update_neighbour(neigh
, seqno
, interval
);
286 update_neighbour_metric(neigh
, changed
);
288 schedule_neighbours_check(interval
* 10, 0);
289 } else if(type
== MESSAGE_IHU
) {
290 unsigned short txcost
, interval
;
291 unsigned char address
[16];
293 if(len
< 6) goto fail
;
294 DO_NTOHS(txcost
, message
+ 4);
295 DO_NTOHS(interval
, message
+ 6);
296 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
297 if(rc
< 0) goto fail
;
298 debugf(BABEL_DEBUG_COMMON
,"Received ihu %d (%d) from %s on %s for %s.",
300 format_address(from
), ifp
->name
,
301 format_address(address
));
302 if(message
[2] == 0 || is_interface_ll_address(ifp
, address
)) {
303 int changed
= txcost
!= neigh
->txcost
;
304 neigh
->txcost
= txcost
;
305 neigh
->ihu_time
= babel_now
;
306 neigh
->ihu_interval
= interval
;
307 update_neighbour_metric(neigh
, changed
);
309 schedule_neighbours_check(interval
* 10 * 3, 0);
311 } else if(type
== MESSAGE_ROUTER_ID
) {
316 memcpy(router_id
, message
+ 4, 8);
318 debugf(BABEL_DEBUG_COMMON
,"Received router-id %s from %s on %s.",
319 format_eui64(router_id
), format_address(from
), ifp
->name
);
320 } else if(type
== MESSAGE_NH
) {
321 unsigned char nh
[16];
328 rc
= network_address(message
[2], message
+ 4, len
- 2,
335 debugf(BABEL_DEBUG_COMMON
,"Received nh %s (%d) from %s on %s.",
336 format_address(nh
), message
[2],
337 format_address(from
), ifp
->name
);
338 if(message
[2] == 1) {
339 memcpy(v4_nh
, nh
, 16);
342 memcpy(v6_nh
, nh
, 16);
345 } else if(type
== MESSAGE_UPDATE
) {
346 unsigned char prefix
[16], *nh
;
348 unsigned char channels
[DIVERSITY_HOPS
];
349 unsigned short interval
, seqno
, metric
;
352 if(len
< 2 || message
[3] & 0x80)
353 have_v4_prefix
= have_v6_prefix
= 0;
356 DO_NTOHS(interval
, message
+ 6);
357 DO_NTOHS(seqno
, message
+ 8);
358 DO_NTOHS(metric
, message
+ 10);
359 if(message
[5] == 0 ||
360 (message
[3] == 1 ? have_v4_prefix
: have_v6_prefix
))
361 rc
= network_prefix(message
[2], message
[4], message
[5],
363 message
[2] == 1 ? v4_prefix
: v6_prefix
,
368 if(message
[3] & 0x80)
369 have_v4_prefix
= have_v6_prefix
= 0;
372 parsed_len
= 10 + rc
;
374 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
376 if(message
[3] & 0x80) {
377 if(message
[2] == 1) {
378 memcpy(v4_prefix
, prefix
, 16);
381 memcpy(v6_prefix
, prefix
, 16);
385 if(message
[3] & 0x40) {
386 if(message
[2] == 1) {
387 memset(router_id
, 0, 4);
388 memcpy(router_id
+ 4, prefix
+ 12, 4);
390 memcpy(router_id
, prefix
+ 8, 8);
394 if(!have_router_id
&& message
[2] != 0) {
395 zlog_err("Received prefix with no router id.");
398 debugf(BABEL_DEBUG_COMMON
,"Received update%s%s for %s from %s on %s.",
399 (message
[3] & 0x80) ? "/prefix" : "",
400 (message
[3] & 0x40) ? "/id" : "",
401 format_prefix(prefix
, plen
),
402 format_address(from
), ifp
->name
);
404 if(message
[2] == 0) {
405 if(metric
< 0xFFFF) {
406 zlog_err("Received wildcard update with finite metric.");
409 retract_neighbour_routes(neigh
);
411 } else if(message
[2] == 1) {
415 } else if(have_v6_nh
) {
421 if(message
[2] == 1) {
422 if(!babel_get_if_nfo(ifp
)->ipv4
)
426 if((ifp
->flags
& BABEL_IF_FARAWAY
)) {
429 /* This will be overwritten by parse_route_attributes below. */
431 /* Assume non-interfering (wired) link. */
434 /* Assume interfering. */
435 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
440 parse_route_attributes(message
+ 2 + parsed_len
,
441 len
- parsed_len
, channels
);
444 update_route(router_id
, prefix
, plen
, seqno
, metric
, interval
,
446 channels
, channels_len(channels
));
447 } else if(type
== MESSAGE_REQUEST
) {
448 unsigned char prefix
[16], plen
;
450 if(len
< 2) goto fail
;
451 rc
= network_prefix(message
[2], message
[3], 0,
452 message
+ 4, NULL
, len
- 2, prefix
);
453 if(rc
< 0) goto fail
;
454 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
455 debugf(BABEL_DEBUG_COMMON
,"Received request for %s from %s on %s.",
456 message
[2] == 0 ? "any" : format_prefix(prefix
, plen
),
457 format_address(from
), ifp
->name
);
458 if(message
[2] == 0) {
459 struct babel_interface
*babel_ifp
=babel_get_if_nfo(neigh
->ifp
);
460 /* If a neighbour is requesting a full route dump from us,
461 we might as well send it an IHU. */
462 send_ihu(neigh
, NULL
);
463 /* Since nodes send wildcard requests on boot, booting
464 a large number of nodes at the same time may cause an
465 update storm. Ignore a wildcard request that happens
466 shortly after we sent a full update. */
467 if(babel_ifp
->last_update_time
<
468 (time_t)(babel_now
.tv_sec
-
469 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
)
758 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
759 send_hello_noupdate(ifp
, (babel_ifp
->hello_interval
+ 9) / 10);
760 /* Send full IHU every 3 hellos, and marginal IHU each time */
761 if(babel_ifp
->hello_seqno
% 3 == 0)
764 send_marginal_ihu(ifp
);
768 flush_unicast(int dofree
)
770 struct sockaddr_in6 sin6
;
773 if(unicast_buffered
== 0)
776 if(!if_up(unicast_neighbour
->ifp
))
779 /* Preserve ordering of messages */
780 flushbuf(unicast_neighbour
->ifp
);
782 if(check_bucket(unicast_neighbour
->ifp
)) {
783 memset(&sin6
, 0, sizeof(sin6
));
784 sin6
.sin6_family
= AF_INET6
;
785 memcpy(&sin6
.sin6_addr
, unicast_neighbour
->address
, 16);
786 sin6
.sin6_port
= htons(protocol_port
);
787 sin6
.sin6_scope_id
= unicast_neighbour
->ifp
->ifindex
;
788 DO_HTONS(packet_header
+ 2, unicast_buffered
);
789 rc
= babel_send(protocol_socket
,
790 packet_header
, sizeof(packet_header
),
791 unicast_buffer
, unicast_buffered
,
792 (struct sockaddr
*)&sin6
, sizeof(sin6
));
794 zlog_err("send(unicast): %s", safe_strerror(errno
));
796 zlog_err("Warning: bucket full, dropping unicast packet to %s if %s.",
797 format_address(unicast_neighbour
->address
),
798 unicast_neighbour
->ifp
->name
);
802 VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer
, UNICAST_BUFSIZE
);
803 unicast_buffered
= 0;
804 if(dofree
&& unicast_buffer
) {
805 free(unicast_buffer
);
806 unicast_buffer
= NULL
;
808 unicast_neighbour
= NULL
;
809 unicast_flush_timeout
.tv_sec
= 0;
810 unicast_flush_timeout
.tv_usec
= 0;
814 really_send_update(struct interface
*ifp
,
815 const unsigned char *id
,
816 const unsigned char *prefix
, unsigned char plen
,
817 unsigned short seqno
, unsigned short metric
,
818 unsigned char *channels
, int channels_len
)
820 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
821 int add_metric
, v4
, real_plen
, omit
= 0;
822 const unsigned char *real_prefix
;
823 unsigned short flags
= 0;
826 if(diversity_kind
!= DIVERSITY_CHANNEL
)
829 channels_size
= channels_len
>= 0 ? channels_len
+ 2 : 0;
834 add_metric
= output_filter(id
, prefix
, plen
, ifp
->ifindex
);
835 if(add_metric
>= INFINITY
)
838 metric
= MIN(metric
+ add_metric
, INFINITY
);
840 ensure_space(ifp
, 20 + 12 + 28);
842 v4
= plen
>= 96 && v4mapped(prefix
);
847 if(!babel_ifp
->have_buffered_nh
||
848 memcmp(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4) != 0) {
849 start_message(ifp
, MESSAGE_NH
, 6);
850 accumulate_byte(ifp
, 1);
851 accumulate_byte(ifp
, 0);
852 accumulate_bytes(ifp
, babel_ifp
->ipv4
, 4);
853 end_message(ifp
, MESSAGE_NH
, 6);
854 memcpy(babel_ifp
->buffered_nh
, babel_ifp
->ipv4
, 4);
855 babel_ifp
->have_buffered_nh
= 1;
858 real_prefix
= prefix
+ 12;
859 real_plen
= plen
- 96;
861 if(babel_ifp
->have_buffered_prefix
) {
862 while(omit
< plen
/ 8 &&
863 babel_ifp
->buffered_prefix
[omit
] == prefix
[omit
])
866 if(!babel_ifp
->have_buffered_prefix
|| plen
>= 48)
868 real_prefix
= prefix
;
872 if(!babel_ifp
->have_buffered_id
873 || memcmp(id
, babel_ifp
->buffered_id
, 8) != 0) {
874 if(real_plen
== 128 && memcmp(real_prefix
+ 8, id
, 8) == 0) {
877 start_message(ifp
, MESSAGE_ROUTER_ID
, 10);
878 accumulate_short(ifp
, 0);
879 accumulate_bytes(ifp
, id
, 8);
880 end_message(ifp
, MESSAGE_ROUTER_ID
, 10);
882 memcpy(babel_ifp
->buffered_id
, id
, 16);
883 babel_ifp
->have_buffered_id
= 1;
886 start_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
888 accumulate_byte(ifp
, v4
? 1 : 2);
889 accumulate_byte(ifp
, flags
);
890 accumulate_byte(ifp
, real_plen
);
891 accumulate_byte(ifp
, omit
);
892 accumulate_short(ifp
, (babel_ifp
->update_interval
+ 5) / 10);
893 accumulate_short(ifp
, seqno
);
894 accumulate_short(ifp
, metric
);
895 accumulate_bytes(ifp
, real_prefix
+ omit
, (real_plen
+ 7) / 8 - omit
);
896 /* Note that an empty channels TLV is different from no such TLV. */
897 if(channels_len
>= 0) {
898 accumulate_byte(ifp
, 2);
899 accumulate_byte(ifp
, channels_len
);
900 accumulate_bytes(ifp
, channels
, channels_len
);
902 end_message(ifp
, MESSAGE_UPDATE
, 10 + (real_plen
+ 7) / 8 - omit
+
906 memcpy(babel_ifp
->buffered_prefix
, prefix
, 16);
907 babel_ifp
->have_buffered_prefix
= 1;
912 compare_buffered_updates(const void *av
, const void *bv
)
914 const struct buffered_update
*a
= av
, *b
= bv
;
915 int rc
, v4a
, v4b
, ma
, mb
;
917 rc
= memcmp(a
->id
, b
->id
, 8);
921 v4a
= (a
->plen
>= 96 && v4mapped(a
->prefix
));
922 v4b
= (b
->plen
>= 96 && v4mapped(b
->prefix
));
929 ma
= (!v4a
&& a
->plen
== 128 && memcmp(a
->prefix
+ 8, a
->id
, 8) == 0);
930 mb
= (!v4b
&& b
->plen
== 128 && memcmp(b
->prefix
+ 8, b
->id
, 8) == 0);
937 if(a
->plen
< b
->plen
)
939 else if(a
->plen
> b
->plen
)
942 return memcmp(a
->prefix
, b
->prefix
, 16);
946 flushupdates(struct interface
*ifp
)
948 babel_interface_nfo
*babel_ifp
= NULL
;
949 struct xroute
*xroute
;
950 struct babel_route
*route
;
951 const unsigned char *last_prefix
= NULL
;
952 unsigned char last_plen
= 0xFF;
956 struct interface
*ifp_aux
;
957 struct listnode
*linklist_node
= NULL
;
958 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
959 flushupdates(ifp_aux
);
963 babel_ifp
= babel_get_if_nfo(ifp
);
964 if(babel_ifp
->num_buffered_updates
> 0) {
965 struct buffered_update
*b
= babel_ifp
->buffered_updates
;
966 int n
= babel_ifp
->num_buffered_updates
;
968 babel_ifp
->buffered_updates
= NULL
;
969 babel_ifp
->update_bufsize
= 0;
970 babel_ifp
->num_buffered_updates
= 0;
975 debugf(BABEL_DEBUG_COMMON
," (flushing %d buffered updates on %s (%d))",
976 n
, ifp
->name
, ifp
->ifindex
);
978 /* In order to send fewer update messages, we want to send updates
979 with the same router-id together, with IPv6 going out before IPv4. */
981 for(i
= 0; i
< n
; i
++) {
982 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
984 memcpy(b
[i
].id
, route
->src
->id
, 8);
986 memcpy(b
[i
].id
, myid
, 8);
989 qsort(b
, n
, sizeof(struct buffered_update
), compare_buffered_updates
);
991 for(i
= 0; i
< n
; i
++) {
992 /* The same update may be scheduled multiple times before it is
993 sent out. Since our buffer is now sorted, it is enough to
994 compare with the previous update. */
997 if(b
[i
].plen
== last_plen
&&
998 memcmp(b
[i
].prefix
, last_prefix
, 16) == 0)
1002 xroute
= find_xroute(b
[i
].prefix
, b
[i
].plen
);
1003 route
= find_installed_route(b
[i
].prefix
, b
[i
].plen
);
1005 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1006 really_send_update(ifp
, myid
,
1007 xroute
->prefix
, xroute
->plen
,
1008 myseqno
, xroute
->metric
,
1010 last_prefix
= xroute
->prefix
;
1011 last_plen
= xroute
->plen
;
1013 unsigned char channels
[DIVERSITY_HOPS
];
1015 struct interface
*route_ifp
= route
->neigh
->ifp
;
1016 struct babel_interface
*babel_route_ifp
= NULL
;
1017 unsigned short metric
;
1018 unsigned short seqno
;
1020 seqno
= route
->seqno
;
1022 route_interferes(route
, ifp
) ?
1023 route_metric(route
) :
1024 route_metric_noninterfering(route
);
1026 if(metric
< INFINITY
)
1027 satisfy_request(route
->src
->prefix
, route
->src
->plen
,
1028 seqno
, route
->src
->id
, ifp
);
1029 if((babel_ifp
->flags
& BABEL_IF_SPLIT_HORIZON
) &&
1030 route
->neigh
->ifp
== ifp
)
1033 babel_route_ifp
= babel_get_if_nfo(route_ifp
);
1034 if(babel_route_ifp
->channel
==BABEL_IF_CHANNEL_NONINTERFERING
) {
1035 memcpy(channels
, route
->channels
, DIVERSITY_HOPS
);
1037 if(babel_route_ifp
->channel
== BABEL_IF_CHANNEL_UNKNOWN
)
1038 channels
[0] = BABEL_IF_CHANNEL_INTERFERING
;
1040 assert(babel_route_ifp
->channel
> 0 &&
1041 babel_route_ifp
->channel
<= 255);
1042 channels
[0] = babel_route_ifp
->channel
;
1044 memcpy(channels
+ 1, route
->channels
, DIVERSITY_HOPS
- 1);
1047 chlen
= channels_len(channels
);
1048 really_send_update(ifp
, route
->src
->id
,
1053 update_source(route
->src
, seqno
, metric
);
1054 last_prefix
= route
->src
->prefix
;
1055 last_plen
= route
->src
->plen
;
1057 /* There's no route for this prefix. This can happen shortly
1058 after an xroute has been retracted, so send a retraction. */
1059 really_send_update(ifp
, myid
, b
[i
].prefix
, b
[i
].plen
,
1060 myseqno
, INFINITY
, NULL
, -1);
1063 schedule_flush_now(ifp
);
1067 babel_ifp
->update_flush_timeout
.tv_sec
= 0;
1068 babel_ifp
->update_flush_timeout
.tv_usec
= 0;
1072 schedule_update_flush(struct interface
*ifp
, int urgent
)
1074 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1076 msecs
= update_jitter(babel_ifp
, urgent
);
1077 if(babel_ifp
->update_flush_timeout
.tv_sec
!= 0 &&
1078 timeval_minus_msec(&babel_ifp
->update_flush_timeout
, &babel_now
) < msecs
)
1080 set_timeout(&babel_ifp
->update_flush_timeout
, msecs
);
1084 buffer_update(struct interface
*ifp
,
1085 const unsigned char *prefix
, unsigned char plen
)
1087 babel_interface_nfo
*babel_ifp
= babel_get_if_nfo(ifp
);
1088 if(babel_ifp
->num_buffered_updates
> 0 &&
1089 babel_ifp
->num_buffered_updates
>= babel_ifp
->update_bufsize
)
1092 if(babel_ifp
->update_bufsize
== 0) {
1094 assert(babel_ifp
->buffered_updates
== NULL
);
1095 /* Allocate enough space to hold a full update. Since the
1096 number of installed routes will grow over time, make sure we
1097 have enough space to send a full-ish frame. */
1098 n
= installed_routes_estimate() + xroutes_estimate() + 4;
1099 n
= MAX(n
, babel_ifp
->bufsize
/ 16);
1101 babel_ifp
->buffered_updates
= malloc(n
*sizeof(struct buffered_update
));
1102 if(babel_ifp
->buffered_updates
== NULL
) {
1103 zlog_err("malloc(buffered_updates): %s", safe_strerror(errno
));
1105 /* Try again with a tiny buffer. */
1111 babel_ifp
->update_bufsize
= n
;
1112 babel_ifp
->num_buffered_updates
= 0;
1115 memcpy(babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].prefix
,
1117 babel_ifp
->buffered_updates
[babel_ifp
->num_buffered_updates
].plen
= plen
;
1118 babel_ifp
->num_buffered_updates
++;
1122 buffer_update_callback(struct babel_route
*route
, void *closure
)
1124 buffer_update((struct interface
*)closure
,
1125 route
->src
->prefix
, route
->src
->plen
);
1129 send_update(struct interface
*ifp
, int urgent
,
1130 const unsigned char *prefix
, unsigned char plen
)
1132 babel_interface_nfo
*babel_ifp
= NULL
;
1135 struct interface
*ifp_aux
;
1136 struct listnode
*linklist_node
= NULL
;
1137 struct babel_route
*route
;
1138 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
1139 send_update(ifp_aux
, urgent
, prefix
, plen
);
1141 /* Since flushupdates only deals with non-wildcard interfaces, we
1142 need to do this now. */
1143 route
= find_installed_route(prefix
, plen
);
1144 if(route
&& route_metric(route
) < INFINITY
)
1145 satisfy_request(prefix
, plen
, route
->src
->seqno
, route
->src
->id
,
1154 babel_ifp
= babel_get_if_nfo(ifp
);
1156 if(!parasitic
|| find_xroute(prefix
, plen
)) {
1157 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for %s.",
1158 ifp
->name
, format_prefix(prefix
, plen
));
1159 buffer_update(ifp
, prefix
, plen
);
1162 send_self_update(ifp
);
1164 debugf(BABEL_DEBUG_COMMON
,"Sending update to %s for any.",
1166 for_all_installed_routes(buffer_update_callback
, ifp
);
1168 set_timeout(&babel_ifp
->update_timeout
, babel_ifp
->update_interval
);
1169 babel_ifp
->last_update_time
= babel_now
.tv_sec
;
1171 schedule_update_flush(ifp
, urgent
);
1175 send_update_resend(struct interface
*ifp
,
1176 const unsigned char *prefix
, unsigned char plen
)
1178 assert(prefix
!= NULL
);
1180 send_update(ifp
, 1, prefix
, plen
);
1181 record_resend(RESEND_UPDATE
, prefix
, plen
, 0, 0, NULL
, resend_delay
);
1185 send_wildcard_retraction(struct interface
*ifp
)
1187 babel_interface_nfo
*babel_ifp
= NULL
;
1189 struct interface
*ifp_aux
;
1190 struct listnode
*linklist_node
= NULL
;
1191 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
)
1192 send_wildcard_retraction(ifp_aux
);
1199 babel_ifp
= babel_get_if_nfo(ifp
);
1200 start_message(ifp
, MESSAGE_UPDATE
, 10);
1201 accumulate_byte(ifp
, 0);
1202 accumulate_byte(ifp
, 0x40);
1203 accumulate_byte(ifp
, 0);
1204 accumulate_byte(ifp
, 0);
1205 accumulate_short(ifp
, 0xFFFF);
1206 accumulate_short(ifp
, myseqno
);
1207 accumulate_short(ifp
, 0xFFFF);
1208 end_message(ifp
, MESSAGE_UPDATE
, 10);
1210 babel_ifp
->have_buffered_id
= 0;
1216 myseqno
= seqno_plus(myseqno
, 1);
1217 seqno_time
= babel_now
;
1221 send_xroute_update_callback(struct xroute
*xroute
, void *closure
)
1223 struct interface
*ifp
= (struct interface
*)closure
;
1224 send_update(ifp
, 0, xroute
->prefix
, xroute
->plen
);
1228 send_self_update(struct interface
*ifp
)
1231 struct interface
*ifp_aux
;
1232 struct listnode
*linklist_node
= NULL
;
1233 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1236 send_self_update(ifp_aux
);
1241 debugf(BABEL_DEBUG_COMMON
,"Sending self update to %s.", ifp
->name
);
1242 for_all_xroutes(send_xroute_update_callback
, ifp
);
1246 send_ihu(struct neighbour
*neigh
, struct interface
*ifp
)
1248 babel_interface_nfo
*babel_ifp
= NULL
;
1249 int rxcost
, interval
;
1252 if(neigh
== NULL
&& ifp
== NULL
) {
1253 struct interface
*ifp_aux
;
1254 struct listnode
*linklist_node
= NULL
;
1255 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1258 send_ihu(NULL
, ifp_aux
);
1264 struct neighbour
*ngh
;
1265 FOR_ALL_NEIGHBOURS(ngh
) {
1273 if(ifp
&& neigh
->ifp
!= ifp
)
1277 babel_ifp
= babel_get_if_nfo(ifp
);
1281 rxcost
= neighbour_rxcost(neigh
);
1282 interval
= (babel_ifp
->hello_interval
* 3 + 9) / 10;
1284 /* Conceptually, an IHU is a unicast message. We usually send them as
1285 multicast, since this allows aggregation into a single packet and
1286 avoids an ARP exchange. If we already have a unicast message queued
1287 for this neighbour, however, we might as well piggyback the IHU. */
1288 debugf(BABEL_DEBUG_COMMON
,"Sending %sihu %d on %s to %s.",
1289 unicast_neighbour
== neigh
? "unicast " : "",
1292 format_address(neigh
->address
));
1294 ll
= linklocal(neigh
->address
);
1296 if(unicast_neighbour
!= neigh
) {
1297 start_message(ifp
, MESSAGE_IHU
, ll
? 14 : 22);
1298 accumulate_byte(ifp
, ll
? 3 : 2);
1299 accumulate_byte(ifp
, 0);
1300 accumulate_short(ifp
, rxcost
);
1301 accumulate_short(ifp
, interval
);
1303 accumulate_bytes(ifp
, neigh
->address
+ 8, 8);
1305 accumulate_bytes(ifp
, neigh
->address
, 16);
1306 end_message(ifp
, MESSAGE_IHU
, ll
? 14 : 22);
1309 rc
= start_unicast_message(neigh
, MESSAGE_IHU
, ll
? 14 : 22);
1311 accumulate_unicast_byte(neigh
, ll
? 3 : 2);
1312 accumulate_unicast_byte(neigh
, 0);
1313 accumulate_unicast_short(neigh
, rxcost
);
1314 accumulate_unicast_short(neigh
, interval
);
1316 accumulate_unicast_bytes(neigh
, neigh
->address
+ 8, 8);
1318 accumulate_unicast_bytes(neigh
, neigh
->address
, 16);
1319 end_unicast_message(neigh
, MESSAGE_IHU
, ll
? 14 : 22);
1323 /* Send IHUs to all marginal neighbours */
1325 send_marginal_ihu(struct interface
*ifp
)
1327 struct neighbour
*neigh
;
1328 FOR_ALL_NEIGHBOURS(neigh
) {
1329 if(ifp
&& neigh
->ifp
!= ifp
)
1331 if(neigh
->txcost
>= 384 || (neigh
->reach
& 0xF000) != 0xF000)
1332 send_ihu(neigh
, ifp
);
1337 send_request(struct interface
*ifp
,
1338 const unsigned char *prefix
, unsigned char plen
)
1343 struct interface
*ifp_aux
;
1344 struct listnode
*linklist_node
= NULL
;
1345 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1348 send_request(ifp_aux
, prefix
, plen
);
1353 /* make sure any buffered updates go out before this request. */
1359 debugf(BABEL_DEBUG_COMMON
,"sending request to %s for %s.",
1360 ifp
->name
, prefix
? format_prefix(prefix
, plen
) : "any");
1361 v4
= plen
>= 96 && v4mapped(prefix
);
1362 len
= !prefix
? 2 : v4
? 6 : 18;
1364 start_message(ifp
, MESSAGE_REQUEST
, len
);
1365 accumulate_byte(ifp
, !prefix
? 0 : v4
? 1 : 2);
1366 accumulate_byte(ifp
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1369 accumulate_bytes(ifp
, prefix
+ 12, 4);
1371 accumulate_bytes(ifp
, prefix
, 16);
1373 end_message(ifp
, MESSAGE_REQUEST
, len
);
1377 send_unicast_request(struct neighbour
*neigh
,
1378 const unsigned char *prefix
, unsigned char plen
)
1382 /* make sure any buffered updates go out before this request. */
1383 flushupdates(neigh
->ifp
);
1385 debugf(BABEL_DEBUG_COMMON
,"sending unicast request to %s for %s.",
1386 format_address(neigh
->address
),
1387 prefix
? format_prefix(prefix
, plen
) : "any");
1388 v4
= plen
>= 96 && v4mapped(prefix
);
1389 len
= !prefix
? 2 : v4
? 6 : 18;
1391 rc
= start_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1393 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? 1 : 2);
1394 accumulate_unicast_byte(neigh
, !prefix
? 0 : v4
? plen
- 96 : plen
);
1397 accumulate_unicast_bytes(neigh
, prefix
+ 12, 4);
1399 accumulate_unicast_bytes(neigh
, prefix
, 16);
1401 end_unicast_message(neigh
, MESSAGE_REQUEST
, len
);
1405 send_multihop_request(struct interface
*ifp
,
1406 const unsigned char *prefix
, unsigned char plen
,
1407 unsigned short seqno
, const unsigned char *id
,
1408 unsigned short hop_count
)
1412 /* Make sure any buffered updates go out before this request. */
1416 struct interface
*ifp_aux
;
1417 struct listnode
*linklist_node
= NULL
;
1418 FOR_ALL_INTERFACES(ifp_aux
, linklist_node
) {
1421 send_multihop_request(ifp_aux
, prefix
, plen
, seqno
, id
, hop_count
);
1429 debugf(BABEL_DEBUG_COMMON
,"Sending request (%d) on %s for %s.",
1430 hop_count
, ifp
->name
, format_prefix(prefix
, plen
));
1431 v4
= plen
>= 96 && v4mapped(prefix
);
1432 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1435 start_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1436 accumulate_byte(ifp
, v4
? 1 : 2);
1437 accumulate_byte(ifp
, v4
? plen
- 96 : plen
);
1438 accumulate_short(ifp
, seqno
);
1439 accumulate_byte(ifp
, hop_count
);
1440 accumulate_byte(ifp
, 0);
1441 accumulate_bytes(ifp
, id
, 8);
1444 accumulate_bytes(ifp
, prefix
+ 12, pb
);
1446 accumulate_bytes(ifp
, prefix
, pb
);
1448 end_message(ifp
, MESSAGE_MH_REQUEST
, len
);
1452 send_unicast_multihop_request(struct neighbour
*neigh
,
1453 const unsigned char *prefix
, unsigned char plen
,
1454 unsigned short seqno
, const unsigned char *id
,
1455 unsigned short hop_count
)
1457 int rc
, v4
, pb
, len
;
1459 /* Make sure any buffered updates go out before this request. */
1460 flushupdates(neigh
->ifp
);
1462 debugf(BABEL_DEBUG_COMMON
,"Sending multi-hop request to %s for %s (%d hops).",
1463 format_address(neigh
->address
),
1464 format_prefix(prefix
, plen
), hop_count
);
1465 v4
= plen
>= 96 && v4mapped(prefix
);
1466 pb
= v4
? ((plen
- 96) + 7) / 8 : (plen
+ 7) / 8;
1469 rc
= start_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1471 accumulate_unicast_byte(neigh
, v4
? 1 : 2);
1472 accumulate_unicast_byte(neigh
, v4
? plen
- 96 : plen
);
1473 accumulate_unicast_short(neigh
, seqno
);
1474 accumulate_unicast_byte(neigh
, hop_count
);
1475 accumulate_unicast_byte(neigh
, 0);
1476 accumulate_unicast_bytes(neigh
, id
, 8);
1479 accumulate_unicast_bytes(neigh
, prefix
+ 12, pb
);
1481 accumulate_unicast_bytes(neigh
, prefix
, pb
);
1483 end_unicast_message(neigh
, MESSAGE_MH_REQUEST
, len
);
1487 send_request_resend(struct neighbour
*neigh
,
1488 const unsigned char *prefix
, unsigned char plen
,
1489 unsigned short seqno
, unsigned char *id
)
1492 send_unicast_multihop_request(neigh
, prefix
, plen
, seqno
, id
, 127);
1494 send_multihop_request(NULL
, prefix
, plen
, seqno
, id
, 127);
1496 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,
1497 neigh
? neigh
->ifp
: NULL
, resend_delay
);
1501 handle_request(struct neighbour
*neigh
, const unsigned char *prefix
,
1502 unsigned char plen
, unsigned char hop_count
,
1503 unsigned short seqno
, const unsigned char *id
)
1505 struct xroute
*xroute
;
1506 struct babel_route
*route
;
1507 struct neighbour
*successor
= NULL
;
1509 xroute
= find_xroute(prefix
, plen
);
1510 route
= find_installed_route(prefix
, plen
);
1512 if(xroute
&& (!route
|| xroute
->metric
<= kernel_metric
)) {
1513 if(hop_count
> 0 && memcmp(id
, myid
, 8) == 0) {
1514 if(seqno_compare(seqno
, myseqno
) > 0) {
1515 if(seqno_minus(seqno
, myseqno
) > 100) {
1516 /* Hopelessly out-of-date request */
1522 send_update(neigh
->ifp
, 1, prefix
, plen
);
1527 (memcmp(id
, route
->src
->id
, 8) != 0 ||
1528 seqno_compare(seqno
, route
->seqno
) <= 0)) {
1529 send_update(neigh
->ifp
, 1, prefix
, plen
);
1536 if(route
&& memcmp(id
, route
->src
->id
, 8) == 0 &&
1537 seqno_minus(seqno
, route
->seqno
) > 100) {
1538 /* Hopelessly out-of-date */
1542 if(request_redundant(neigh
->ifp
, prefix
, plen
, seqno
, id
))
1545 /* Let's try to forward this request. */
1546 if(route
&& route_metric(route
) < INFINITY
)
1547 successor
= route
->neigh
;
1549 if(!successor
|| successor
== neigh
) {
1550 /* We were about to forward a request to its requestor. Try to
1551 find a different neighbour to forward the request to. */
1552 struct babel_route
*other_route
;
1554 other_route
= find_best_route(prefix
, plen
, 0, neigh
);
1555 if(other_route
&& route_metric(other_route
) < INFINITY
)
1556 successor
= other_route
->neigh
;
1559 if(!successor
|| successor
== neigh
)
1563 send_unicast_multihop_request(successor
, prefix
, plen
, seqno
, id
,
1565 record_resend(RESEND_REQUEST
, prefix
, plen
, seqno
, id
,