message.c 42.5 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1
/*
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <arpa/inet.h>

30
#include "babel.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
31 32
#include "util.h"
#include "net.h"
33
#include "network.h"
34
#include "source.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
35 36 37
#include "neighbour.h"
#include "route.h"
#include "xroute.h"
38
#include "resend.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
39
#include "message.h"
40
#include "config.h"
41
#include "kernel.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
42

43
unsigned char packet_header[4] = {42, 2};
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
44 45 46 47 48

int parasitic = 0;
int silent_time = 30;
int split_horizon = 1;

49
unsigned short myseqno = 0;
50
struct timeval seqno_time = {0, 0};
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
51

52 53 54 55 56 57
#define UNICAST_BUFSIZE 1024
int unicast_buffered = 0;
unsigned char *unicast_buffer = NULL;
struct neighbour *unicast_neighbour = NULL;
struct timeval unicast_flush_timeout = {0, 0};

58 59 60 61 62 63 64 65 66
static const unsigned char v4prefix[16] =
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
static const unsigned char ll_prefix[16] =
    {0xFE, 0x80};

static int
network_prefix(int ae, int plen, unsigned int omitted,
               const unsigned char *p, const unsigned char *dp,
               unsigned int len, unsigned char *p_r)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
67
{
68 69 70 71 72 73 74 75 76 77
    unsigned pb;
    unsigned char prefix[16];

    if(plen >= 0)
        pb = (plen + 7) / 8;
    else if(ae == 1)
        pb = 4;
    else
        pb = 16;

78 79 80
    if(pb > 16)
        return -1;

81 82 83
    memset(prefix, 0, 16);

    switch(ae) {
84
    case 0: break;
85
    case 1:
86 87 88 89 90 91 92
        if(pb > 4 || (pb > omitted && len < pb - omitted)) return -1;
        memcpy(prefix, v4prefix, 12);
        if(omitted) {
            if (dp == NULL || !v4mapped(dp)) return -1;
            memcpy(prefix, dp, 12 + omitted);
        }
        if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb);
93 94 95
        break;
    case 2:
        if(pb > omitted && len < pb - omitted) return -1;
96 97 98 99
        if(omitted) {
            if (dp == NULL || v4mapped(dp)) return -1;
            memcpy(prefix, dp, omitted);
        }
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted);
        break;
    case 3:
        if(pb > 8 && len < pb - 8) return -1;
        prefix[0] = 0xfe;
        prefix[1] = 0x80;
        if(pb > 8) memcpy(prefix + 8, p, pb - 8);
        break;
    default:
        return -1;
    }

    mask_prefix(p_r, prefix, plen < 0 ? 128 : ae == 1 ? plen + 96 : plen);
    return 1;
}

static int
network_address(int ae, const unsigned char *a, unsigned int len,
                unsigned char *a_r)
{
    return network_prefix(ae, -1, 0, a, NULL, len, a_r);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
121 122
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
123 124
void
parse_packet(const unsigned char *from, struct network *net,
125
             const unsigned char *packet, int packetlen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
126
{
127
    int i;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
128
    const unsigned char *message;
129 130
    unsigned char type, len;
    int bodylen;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
131
    struct neighbour *neigh;
132 133 134 135 136
    int have_router_id = 0, have_v4_prefix = 0, have_v6_prefix = 0,
        have_v4_nh = 0, have_v6_nh = 0;
    unsigned char router_id[8], v4_prefix[16], v6_prefix[16],
        v4_nh[16], v6_nh[16];

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
137

138 139 140 141 142 143
    if(from[0] != 0xFE || (from[1] & 0xC0) != 0x80) {
        fprintf(stderr, "Received packet from non-local address %s.\n",
                format_address(from));
        return;
    }

144
    if(packet[0] != 42) {
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
145 146 147 148 149
        fprintf(stderr, "Received malformed packet on %s from %s.\n",
                net->ifname, format_address(from));
        return;
    }

150
    if(packet[1] != 2) {
151 152 153 154 155 156
        fprintf(stderr,
                "Received packet with unknown version %d on %s from %s.\n",
                packet[1], net->ifname, format_address(from));
        return;
    }

157 158 159
    neigh = find_neighbour(from, net);
    if(neigh == NULL) {
        fprintf(stderr, "Couldn't allocate neighbour.\n");
160 161 162
        return;
    }

163 164 165 166 167 168 169 170 171 172 173
    DO_NTOHS(bodylen, packet + 2);

    if(bodylen + 4 > packetlen) {
        fprintf(stderr, "Received truncated packet (%d + 4 > %d).\n",
                bodylen, packetlen);
        bodylen = packetlen - 4;
    }

    i = 0;
    while(i < bodylen) {
        message = packet + 4 + i;
174
        type = message[0];
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        if(type == MESSAGE_PAD1) {
            debugf("Received pad1 from %s on %s.\n",
                   format_address(from), net->ifname);
            i++;
            continue;
        }
        if(i + 1 > bodylen) {
            fprintf(stderr, "Received truncated message.\n");
            break;
        }
        len = message[1];
        if(i + len > bodylen) {
            fprintf(stderr, "Received truncated message.\n");
            break;
        }

        if(type == MESSAGE_PADN) {
            debugf("Received pad%d from %s on %s.\n",
                   len, format_address(from), net->ifname);
        } else if(type == MESSAGE_ACK_REQ) {
            unsigned short nonce, interval;
196
            if(len < 6) goto fail;
197 198 199 200 201 202 203 204 205 206 207
            DO_NTOHS(nonce, message + 4);
            DO_NTOHS(interval, message + 6);
            debugf("Received ack-req (%04X %d) from %s on %s.\n",
                   nonce, interval, format_address(from), net->ifname);
            send_ack(neigh, nonce, interval);
        } else if(type == MESSAGE_ACK) {
            debugf("Received ack from %s on %s.\n",
                   format_address(from), net->ifname);
            /* Nothing right now */
        } else if(type == MESSAGE_HELLO) {
            unsigned short seqno, interval;
208
            int changed;
209 210 211 212 213 214
            if(len < 6) goto fail;
            DO_NTOHS(seqno, message + 4);
            DO_NTOHS(interval, message + 6);
            debugf("Received hello %d (%d) from %s on %s.\n",
                   seqno, interval,
                   format_address(from), net->ifname);
215
            net->activity_time = now.tv_sec;
216
            update_hello_interval(net);
217
            changed = update_neighbour(neigh, seqno, interval);
218 219
            if(changed)
                update_neighbour_metric(neigh);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            if(interval > 0)
                schedule_neighbours_check(interval * 10, 0);
        } else if(type == MESSAGE_IHU) {
            unsigned short txcost, interval;
            unsigned char address[16];
            int rc;
            if(len < 6) goto fail;
            DO_NTOHS(txcost, message + 4);
            DO_NTOHS(interval, message + 6);
            rc = network_address(message[2], message + 8, len - 6, address);
            if(rc < 0) goto fail;
            debugf("Received ihu %d (%d) from %s on %s for %s.\n",
                   txcost, interval,
                   format_address(from), net->ifname,
                   format_address(address));
            if(message[3] == 0 || network_ll_address(net, address)) {
                neigh->txcost = txcost;
                neigh->ihu_time = now;
                neigh->ihu_interval = interval;
                update_neighbour_metric(neigh);
                if(interval > 0)
                    schedule_neighbours_check(interval * 10 * 3, 0);
242
            }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        } else if(type == MESSAGE_ROUTER_ID) {
            if(len < 10) {
                have_router_id = 0;
                goto fail;
            }
            memcpy(router_id, message + 4, 8);
            have_router_id = 1;
            debugf("Received router-id %s from %s on %s.\n",
                   format_eui64(router_id), format_address(from), net->ifname);
        } else if(type == MESSAGE_NH) {
            unsigned char nh[16];
            int rc;
            if(len < 2) {
                have_v4_nh = 0;
                have_v6_nh = 0;
                goto fail;
            }
            rc = network_address(message[2], message + 4, len - 2,
                                 nh);
            if(rc < 0) {
                have_v4_nh = 0;
                have_v6_nh = 0;
                goto fail;
            }
            debugf("Received nh %s (%d) from %s on %s.\n",
                   format_address(nh), message[2],
                   format_address(from), net->ifname);
            if(message[2] == 1) {
                memcpy(v4_nh, nh, 16);
                have_v4_nh = 1;
            } else {
                memcpy(v6_nh, nh, 16);
                have_v6_nh = 1;
            }
        } else if(type == MESSAGE_UPDATE) {
            unsigned char prefix[16], *nh;
            unsigned char plen;
            unsigned short interval, seqno, metric;
            int rc;
            if(len < 10) {
                if(len < 2 || message[3] & 0x80)
                    have_v4_prefix = have_v6_prefix = 0;
                goto fail;
            }
            DO_NTOHS(interval, message + 6);
            DO_NTOHS(seqno, message + 8);
            DO_NTOHS(metric, message + 10);
            if(message[5] == 0 ||
               (message[3] == 1 ? have_v4_prefix : have_v6_prefix))
                rc = network_prefix(message[2], message[4], message[5],
                                    message + 12,
                                    message[3] == 1 ? v4_prefix : v6_prefix,
                                    len - 10, prefix);
            else
                rc = -1;
            if(rc < 0) {
                if(message[3] & 0x80)
                    have_v4_prefix = have_v6_prefix = 0;
                goto fail;
            }

            plen = message[4] + (message[2] == 1 ? 96 : 0);

            if(message[3] & 0x80) {
                if(message[2] == 1) {
                    memcpy(v4_prefix, prefix, 16);
                    have_v4_prefix = 1;
310
                } else {
311 312
                    memcpy(v6_prefix, prefix, 16);
                    have_v6_prefix = 1;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
313
                }
314 315 316 317 318 319 320
            }
            if(message[3] & 0x40) {
                if(message[2] == 1) {
                    memset(router_id, 0, 4);
                    memcpy(router_id + 4, prefix + 12, 4);
                } else {
                    memcpy(router_id, prefix + 8, 8);
321
                }
322 323
                have_router_id = 1;
            }
324
            if(!have_router_id && message[2] != 0) {
325 326 327 328 329 330 331 332 333
                fprintf(stderr, "Received prefix with no router id.\n");
                goto fail;
            }
            debugf("Received update%s%s for %s from %s on %s.\n",
                   (message[3] & 0x80) ? "/prefix" : "",
                   (message[3] & 0x40) ? "/id" : "",
                   format_prefix(prefix, plen),
                   format_address(from), net->ifname);

334 335 336 337 338 339 340 341 342
            if(message[2] == 0) {
                if(metric < 0xFFFF) {
                    fprintf(stderr,
                            "Received wildcard update with finite metric.\n");
                    goto done;
                }
                retract_neighbour_routes(neigh);
                goto done;
            } else if(message[2] == 1) {
343 344 345 346 347
                if(!have_v4_nh)
                    goto fail;
                nh = v4_nh;
            } else if(have_v6_nh) {
                nh = v6_nh;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
348
            } else {
349
                nh = neigh->address;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
350
            }
351

352 353 354 355 356
            if(message[2] == 1) {
                if(!net->ipv4)
                    goto done;
            }

357 358
            update_route(router_id, prefix, plen, seqno, metric, interval,
                         neigh, nh);
359
        } else if(type == MESSAGE_REQUEST) {
360
            unsigned char prefix[16], plen;
361
            int rc;
362
            if(len < 2) goto fail;
363 364 365
            rc = network_prefix(message[2], message[3], 0,
                                message + 4, NULL, len - 2, prefix);
            if(rc < 0) goto fail;
366
            plen = message[3] + (message[2] == 1 ? 96 : 0);
367
            debugf("Received request for %s from %s on %s.\n",
368
                   message[2] == 0 ? "any" : format_prefix(prefix, plen),
369 370 371 372 373 374 375
                   format_address(from), net->ifname);
            if(message[2] == 0) {
                /* If a neighbour is requesting a full route dump from us,
                   we might as well send it an IHU. */
                send_ihu(neigh, NULL);
                send_update(neigh->network, 0, NULL, 0);
            } else {
376
                send_update(neigh->network, 0, prefix, plen);
377
            }
378
        } else if(type == MESSAGE_MH_REQUEST) {
379
            unsigned char prefix[16], plen;
380 381
            unsigned short seqno;
            int rc;
382
            if(len < 14) goto fail;
383 384 385 386
            DO_NTOHS(seqno, message + 4);
            rc = network_prefix(message[2], message[3], 0,
                                message + 16, NULL, len - 14, prefix);
            if(rc < 0) goto fail;
387
            plen = message[3] + (message[2] == 1 ? 96 : 0);
388 389
            debugf("Received request (%d) for %s from %s on %s (%s, %d).\n",
                   message[6],
390
                   format_prefix(prefix, plen),
391 392
                   format_address(from), net->ifname,
                   format_eui64(message + 8), seqno);
393
            handle_request(neigh, prefix, plen, message[6],
394 395 396 397
                           seqno, message + 8);
        } else {
            debugf("Received unknown packet type %d from %s on %s.\n",
                   type, format_address(from), net->ifname);
398
        }
399
    done:
400 401
        i += len + 2;
        continue;
402

403
    fail:
404 405
        fprintf(stderr, "Couldn't parse packet (%d, %d) from %s on %s.\n",
                message[0], message[1], format_address(from), net->ifname);
406
        goto done;
407
    }
408
    return;
409 410
}

411 412
/* Under normal circumstances, there are enough moderation mechanisms
   elsewhere in the protocol to make sure that this last-ditch check
413
   should never trigger.  But I'm superstitious. */
414 415 416 417

static int
check_bucket(struct network *net)
{
418 419 420 421 422 423 424 425
    if(net->bucket <= 0) {
        int seconds = now.tv_sec - net->bucket_time;
        if(seconds > 0) {
            net->bucket = MIN(BUCKET_TOKENS_MAX,
                              seconds * BUCKET_TOKENS_PER_SEC);
        }
        /* Reset bucket time unconditionally, in case clock is stepped. */
        net->bucket_time = now.tv_sec;
426 427
    }

428 429
    if(net->bucket > 0) {
        net->bucket--;
430 431 432 433 434 435
        return 1;
    } else {
        return 0;
    }
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
436 437 438 439 440 441
void
flushbuf(struct network *net)
{
    int rc;
    struct sockaddr_in6 sin6;

442 443
    assert(net->buffered <= net->bufsize);

444
    flushupdates(net);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
445 446 447 448

    if(net->buffered > 0) {
        debugf("  (flushing %d buffered bytes on %s)\n",
               net->buffered, net->ifname);
449 450 451 452 453 454
        if(check_bucket(net)) {
            memset(&sin6, 0, sizeof(sin6));
            sin6.sin6_family = AF_INET6;
            memcpy(&sin6.sin6_addr, protocol_group, 16);
            sin6.sin6_port = htons(protocol_port);
            sin6.sin6_scope_id = net->ifindex;
455
            DO_HTONS(packet_header + 2, net->buffered);
456 457 458 459 460 461 462 463 464 465
            rc = babel_send(protocol_socket,
                            packet_header, sizeof(packet_header),
                            net->sendbuf, net->buffered,
                            (struct sockaddr*)&sin6, sizeof(sin6));
            if(rc < 0)
                perror("send");
        } else {
            fprintf(stderr, "Warning: bucket full, dropping packet to %s.\n",
                    net->ifname);
        }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
466 467 468
    }
    VALGRIND_MAKE_MEM_UNDEFINED(net->sendbuf, net->bufsize);
    net->buffered = 0;
469 470 471 472
    net->have_buffered_hello = 0;
    net->have_buffered_id = 0;
    net->have_buffered_nh = 0;
    net->have_buffered_prefix = 0;
473 474
    net->flush_timeout.tv_sec = 0;
    net->flush_timeout.tv_usec = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
475 476 477 478 479
}

static void
schedule_flush(struct network *net)
{
480
    unsigned msecs = jitter(net, 0);
481 482
    if(net->flush_timeout.tv_sec != 0 &&
       timeval_minus_msec(&net->flush_timeout, &now) < msecs)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
483
        return;
484
    delay_jitter(&net->flush_timeout, msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
485 486
}

487
static void
488 489
schedule_flush_now(struct network *net)
{
490
    /* Almost now */
491
    unsigned msecs = roughly(10);
492 493
    if(net->flush_timeout.tv_sec != 0 &&
       timeval_minus_msec(&net->flush_timeout, &now) < msecs)
494
        return;
495
    delay_jitter(&net->flush_timeout, msecs);
496 497
}

498
static void
499
schedule_unicast_flush(unsigned msecs)
500 501 502 503 504 505 506 507 508 509 510
{
    if(!unicast_neighbour)
        return;
    if(unicast_flush_timeout.tv_sec != 0 &&
       timeval_minus_msec(&unicast_flush_timeout, &now) < msecs)
        return;
    unicast_flush_timeout.tv_usec = (now.tv_usec + msecs * 1000) % 1000000;
    unicast_flush_timeout.tv_sec =
        now.tv_sec + (now.tv_usec / 1000 + msecs) / 1000;
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
511
static void
512
ensure_space(struct network *net, int space)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
513
{
514
    if(net->bufsize - net->buffered < space)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
515 516 517
        flushbuf(net);
}

518
static void
519
start_message(struct network *net, int type, int len)
520
{
521 522 523 524 525
    if(net->bufsize - net->buffered < len + 2)
        flushbuf(net);
    net->sendbuf[net->buffered++] = type;
    net->sendbuf[net->buffered++] = len;
}
526

527
static void
528 529
end_message(struct network *net, int type, int bytes)
{
530 531 532 533 534
    assert(net->buffered >= bytes + 2 &&
           net->sendbuf[net->buffered - bytes - 2] == type &&
           net->sendbuf[net->buffered - bytes - 1] == bytes);
    schedule_flush(net);
}
535

536 537 538 539 540
static void
accumulate_byte(struct network *net, unsigned char value)
{
    net->sendbuf[net->buffered++] = value;
}
541

542 543 544 545 546
static void
accumulate_short(struct network *net, unsigned short value)
{
    DO_HTONS(net->sendbuf + net->buffered, value);
    net->buffered += 2;
547 548
}

549 550 551
static void
accumulate_bytes(struct network *net,
                 const unsigned char *value, unsigned len)
552
{
553 554 555 556 557 558 559 560 561 562 563 564
    memcpy(net->sendbuf + net->buffered, value, len);
    net->buffered += len;
}

static int
start_unicast_message(struct neighbour *neigh, int type, int len)
{
    if(unicast_neighbour) {
        if(neigh != unicast_neighbour ||
           unicast_buffered + len + 2 >=
           MIN(UNICAST_BUFSIZE, neigh->network->bufsize))
            flush_unicast(0);
565
    }
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    if(!unicast_buffer)
        unicast_buffer = malloc(UNICAST_BUFSIZE);
    if(!unicast_buffer) {
        perror("malloc(unicast_buffer)");
        return -1;
    }

    unicast_neighbour = neigh;

    unicast_buffer[unicast_buffered++] = type;
    unicast_buffer[unicast_buffered++] = len;
    return 1;
}

static void
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
581 582
end_unicast_message(struct neighbour *neigh, int type, int bytes)
{
583 584 585
    assert(unicast_neighbour == neigh && unicast_buffered >= bytes + 2 &&
           unicast_buffer[unicast_buffered - bytes - 2] == type &&
           unicast_buffer[unicast_buffered - bytes - 1] == bytes);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
586
    schedule_unicast_flush(jitter(neigh->network, 0));
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
}

static void
accumulate_unicast_byte(struct neighbour *neigh, unsigned char value)
{
    unicast_buffer[unicast_buffered++] = value;
}

static void
accumulate_unicast_short(struct neighbour *neigh, unsigned short value)
{
    DO_HTONS(unicast_buffer + unicast_buffered, value);
    unicast_buffered += 2;
}

static void
accumulate_unicast_bytes(struct neighbour *neigh,
                         const unsigned char *value, unsigned len)
{
    memcpy(unicast_buffer + unicast_buffered, value, len);
    unicast_buffered += len;
}

void
send_ack(struct neighbour *neigh, unsigned short nonce, unsigned short interval)
{
    int rc;
    debugf("Sending ack (%04x) to %s on %s.\n",
           nonce, format_address(neigh->address), neigh->network->ifname);
    rc = start_unicast_message(neigh, MESSAGE_ACK, 2); if(rc < 0) return;
    accumulate_unicast_short(neigh, nonce);
    end_unicast_message(neigh, MESSAGE_ACK, 2);
619 620
    /* Roughly yields a value no larger than 3/2, so this meets the deadline */
    schedule_unicast_flush(roughly(interval * 6));
621 622
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
623
void
624
send_hello_noupdate(struct network *net, unsigned interval)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
625
{
626 627 628 629 630
    /* This avoids sending multiple hellos in a single packet, which breaks
       link quality estimation. */
    if(net->have_buffered_hello)
        flushbuf(net);

631
    net->hello_seqno = seqno_plus(net->hello_seqno, 1);
632
    delay_jitter(&net->hello_timeout, net->hello_interval);
633

634
    if(!net_up(net))
635 636
        return;

637 638 639 640 641 642 643 644 645
    debugf("Sending hello %d (%d) to %s.\n",
           net->hello_seqno, interval, net->ifname);

    start_message(net, MESSAGE_HELLO, 6);
    accumulate_short(net, 0);
    accumulate_short(net, net->hello_seqno);
    accumulate_short(net, interval > 0xFFFF ? 0xFFFF : interval);
    end_message(net, MESSAGE_HELLO, 6);
    net->have_buffered_hello = 1;
646 647 648 649 650 651 652
}

void
send_hello(struct network *net)
{
    int changed;
    changed = update_hello_interval(net);
653
    send_hello_noupdate(net, (net->hello_interval + 9) / 10);
654 655
    /* Send full IHU every 3 hellos, and marginal IHU each time */
    if(changed || net->hello_seqno % 3 == 0)
656
        send_ihu(NULL, net);
657 658
    else
        send_marginal_ihu(net);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
659 660
}

661 662
void
flush_unicast(int dofree)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
663 664 665 666
{
    struct sockaddr_in6 sin6;
    int rc;

667 668 669
    if(unicast_buffered == 0)
        goto done;

670
    if(!net_up(unicast_neighbour->network))
671
        goto done;
672

673 674 675 676
    /* Preserve ordering of messages */
    flushbuf(unicast_neighbour->network);

    if(check_bucket(unicast_neighbour->network)) {
677 678
        memset(&sin6, 0, sizeof(sin6));
        sin6.sin6_family = AF_INET6;
679
        memcpy(&sin6.sin6_addr, unicast_neighbour->address, 16);
680
        sin6.sin6_port = htons(protocol_port);
681
        sin6.sin6_scope_id = unicast_neighbour->network->ifindex;
682
        DO_HTONS(packet_header + 2, unicast_buffered);
683 684
        rc = babel_send(protocol_socket,
                        packet_header, sizeof(packet_header),
685
                        unicast_buffer, unicast_buffered,
686 687 688 689
                        (struct sockaddr*)&sin6, sizeof(sin6));
        if(rc < 0)
            perror("send(unicast)");
    } else {
690 691
        fprintf(stderr,
                "Warning: bucket full, dropping unicast packet"
692
                "to %s if %s.\n",
693 694
                format_address(unicast_neighbour->address),
                unicast_neighbour->network->ifname);
695
    }
696 697 698 699 700 701 702 703 704 705 706 707 708

 done:
    VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer, UNICAST_BUFSIZE);
    unicast_buffered = 0;
    if(dofree && unicast_buffer) {
        free(unicast_buffer);
        unicast_buffer = NULL;
    }
    unicast_neighbour = NULL;
    unicast_flush_timeout.tv_sec = 0;
    unicast_flush_timeout.tv_usec = 0;
}

709 710
static void
really_send_update(struct network *net,
711
                   const unsigned char *id,
712
                   const unsigned char *prefix, unsigned char plen,
713
                   unsigned short seqno, unsigned short metric)
714
{
715 716 717
    int add_metric, v4, real_plen, omit = 0;
    const unsigned char *real_prefix;
    unsigned short flags = 0;
718

719
    if(!net_up(net))
720 721
        return;

722
    add_metric = output_filter(id, prefix, plen, net->ifindex);
723 724 725 726 727
    if(add_metric >= INFINITY)
        return;

    metric = MIN(metric + add_metric, INFINITY);
    /* Worst case */
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
728
    ensure_space(net, 20 + 12 + 28);
729

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
    v4 = plen >= 96 && v4mapped(prefix);

    if(v4) {
        if(!net->ipv4)
            return;
        if(!net->have_buffered_nh ||
           memcmp(net->buffered_nh, net->ipv4, 4) != 0) {
            start_message(net, MESSAGE_NH, 6);
            accumulate_byte(net, 1);
            accumulate_byte(net, 0);
            accumulate_bytes(net, net->ipv4, 4);
            end_message(net, MESSAGE_NH, 6);
            memcpy(net->buffered_nh, net->ipv4, 4);
            net->have_buffered_nh = 1;
        }

        real_prefix = prefix + 12;
        real_plen = plen - 96;
    } else {
        if(net->have_buffered_prefix) {
            while(omit < plen / 8 &&
                  net->buffered_prefix[omit] == prefix[omit])
                omit++;
        }
        if(!net->have_buffered_prefix || plen >= 48)
            flags |= 0x80;
        real_prefix = prefix;
        real_plen = plen;
    }

760
    if(!net->have_buffered_id || memcmp(id, net->buffered_id, 8) != 0) {
761 762
        if(real_plen == 128 && memcmp(real_prefix + 8, id, 8) == 0) {
            flags |= 0x40;
763
        } else {
764 765 766 767
            start_message(net, MESSAGE_ROUTER_ID, 10);
            accumulate_short(net, 0);
            accumulate_bytes(net, id, 8);
            end_message(net, MESSAGE_ROUTER_ID, 10);
768
        }
769 770
        memcpy(net->buffered_id, id, 16);
        net->have_buffered_id = 1;
771
    }
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

    start_message(net, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit);
    accumulate_byte(net, v4 ? 1 : 2);
    accumulate_byte(net, flags);
    accumulate_byte(net, real_plen);
    accumulate_byte(net, omit);
    accumulate_short(net, (update_interval + 5) / 10);
    accumulate_short(net, seqno);
    accumulate_short(net, metric);
    accumulate_bytes(net, real_prefix + omit, (real_plen + 7) / 8 - omit);
    end_message(net, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit);

    if(flags & 0x80) {
        memcpy(net->buffered_prefix, prefix, 16);
        net->have_buffered_prefix = 1;
    }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
788 789
}

790 791 792 793
static int
compare_buffered_updates(const void *av, const void *bv)
{
    const struct buffered_update *a = av, *b = bv;
794
    int rc, v4a, v4b, ma, mb;
795

796
    rc = memcmp(a->id, b->id, 8);
797 798 799 800 801 802 803 804
    if(rc != 0)
        return rc;

    v4a = (a->plen >= 96 && v4mapped(a->prefix));
    v4b = (b->plen >= 96 && v4mapped(b->prefix));

    if(v4a > v4b)
        return 1;
805
    else if(v4a < v4b)
806 807
        return -1;

808 809
    ma = (!v4a && a->plen == 128 && memcmp(a->prefix + 8, a->id, 8) == 0);
    mb = (!v4b && b->plen == 128 && memcmp(b->prefix + 8, b->id, 8) == 0);
810

811
    if(ma > mb)
812
        return -1;
813
    else if(mb > ma)
814 815
        return 1;

816 817
    if(a->plen < b->plen)
        return 1;
818 819
    else if(a->plen > b->plen)
        return -1;
820 821

    return memcmp(a->prefix, b->prefix, 16);
822 823
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
824
void
825
flushupdates(struct network *net)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
826
{
827 828 829 830
    struct xroute *xroute;
    struct route *route;
    const unsigned char *last_prefix = NULL;
    unsigned char last_plen = 0xFF;
831
    int i;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
832

833 834 835 836
    if(net == NULL) {
        struct network *n;
        FOR_ALL_NETS(n)
            flushupdates(n);
837
        return;
838 839 840 841 842 843 844 845 846
    }

    if(net->num_buffered_updates > 0) {
        struct buffered_update *b = net->buffered_updates;
        int n = net->num_buffered_updates;

        net->buffered_updates = NULL;
        net->update_bufsize = 0;
        net->num_buffered_updates = 0;
847

848
        if(!net_up(net))
849
            goto done;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
850

851 852
        debugf("  (flushing %d buffered updates on %s (%d))\n",
               n, net->ifname, net->ifindex);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
853

854 855 856 857
        /* In order to send fewer update messages, we want to send updates
           with the same router-id together, with IPv6 going out before IPv4. */

        for(i = 0; i < n; i++) {
858
            route = find_installed_route(b[i].prefix, b[i].plen);
859
            if(route)
860
                memcpy(b[i].id, route->src->id, 8);
861
            else
862
                memcpy(b[i].id, myid, 8);
863 864
        }

865
        qsort(b, n, sizeof(struct buffered_update), compare_buffered_updates);
866

867 868 869
        for(i = 0; i < n; i++) {
            unsigned short seqno;
            unsigned short metric;
870 871 872 873 874 875

            /* The same update may be scheduled multiple times before it is
               sent out.  Since our buffer is now sorted, it is enough to
               compare with the previous update. */

            if(last_prefix) {
876 877
                if(b[i].plen == last_plen &&
                   memcmp(b[i].prefix, last_prefix, 16) == 0)
878 879 880
                    continue;
            }

881
            xroute = find_xroute(b[i].prefix, b[i].plen);
882 883 884
            if(xroute) {
                really_send_update(net, myid,
                                   xroute->prefix, xroute->plen,
885
                                   myseqno, xroute->metric);
886 887 888
                last_prefix = xroute->prefix;
                last_plen = xroute->plen;
                continue;
889
            }
890
            route = find_installed_route(b[i].prefix, b[i].plen);
891 892
            if(route) {
                seqno = route->seqno;
893
                metric = route->metric;
894 895
                if(metric < INFINITY)
                    satisfy_request(route->src->prefix, route->src->plen,
896
                                    seqno, route->src->id, net);
897
                if((net->flags & NET_SPLIT_HORIZON) &&
898
                   route->neigh->network == net)
899
                    continue;
900
                really_send_update(net, route->src->id,
901 902
                                   route->src->prefix,
                                   route->src->plen,
903
                                   seqno, metric);
904
                update_source(route->src, seqno, metric);
905 906 907
                last_prefix = route->src->prefix;
                last_plen = route->src->plen;
                continue;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
908 909 910
            }
        }
        schedule_flush_now(net);
911 912
    done:
        free(b);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
913
    }
914 915
    net->update_flush_timeout.tv_sec = 0;
    net->update_flush_timeout.tv_usec = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
916 917 918
}

static void
919
schedule_update_flush(struct network *net, int urgent)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
920
{
921
    unsigned msecs;
922
    msecs = update_jitter(net, urgent);
923 924
    if(net->update_flush_timeout.tv_sec != 0 &&
       timeval_minus_msec(&net->update_flush_timeout, &now) < msecs)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
925
        return;
926
    delay_jitter(&net->update_flush_timeout, msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
927 928 929
}

static void
930 931
buffer_update(struct network *net,
              const unsigned char *prefix, unsigned char plen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
932
{
933 934 935
    if(net->num_buffered_updates > 0 &&
       net->num_buffered_updates >= net->update_bufsize)
        flushupdates(net);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
936

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    if(net->update_bufsize == 0) {
        int n;
        assert(net->buffered_updates == NULL);
        n = MAX(net->bufsize / 16, 4);
    again:
        net->buffered_updates = malloc(n * sizeof(struct buffered_update));
        if(net->buffered_updates == NULL) {
            perror("malloc(buffered_updates)");
            if(n > 4) {
                n = 4;
                goto again;
            }
            return;
        }
        net->update_bufsize = n;
        net->num_buffered_updates = 0;
    }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
954

955 956 957 958
    memcpy(net->buffered_updates[net->num_buffered_updates].prefix,
           prefix, 16);
    net->buffered_updates[net->num_buffered_updates].plen = plen;
    net->num_buffered_updates++;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
959 960 961
}

void
962
send_update(struct network *net, int urgent,
963
            const unsigned char *prefix, unsigned char plen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
964
{
965
    int i, selfonly;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
966 967

    if(net == NULL) {
968
        struct network *n;
969
        struct route *route;
970 971
        FOR_ALL_NETS(n)
            send_update(n, urgent, prefix, plen);
972 973 974 975 976 977 978 979
        if(prefix) {
            /* Since flushupdates only deals with non-wildcard interfaces, we
               need to do this now. */
            route = find_installed_route(prefix, plen);
            if(route && route->metric < INFINITY)
                satisfy_request(prefix, plen, route->src->seqno, route->src->id,
                                NULL);
        }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
980 981 982
        return;
    }

983
    if(!net_up(net))
984 985
        return;

986 987
    selfonly =
        parasitic || (silent_time && now.tv_sec < reboot_time + silent_time);
988

989 990
    if(!selfonly)
        silent_time = 0;
991

992
    if(prefix) {
993 994 995 996 997
        if(!selfonly || find_xroute(prefix, plen)) {
            debugf("Sending update to %s for %s.\n",
                   net->ifname, format_prefix(prefix, plen));
            buffer_update(net, prefix, plen);
        }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
998
    } else {
999
        send_self_update(net);
1000
        if(!selfonly && !network_idle(net)) {
1001 1002 1003 1004 1005 1006
            debugf("Sending update to %s for any.\n", net->ifname);
            for(i = 0; i < numroutes; i++)
                if(routes[i].installed)
                    buffer_update(net,
                                  routes[i].src->prefix, routes[i].src->plen);
        }
1007
        delay_jitter(&net->update_timeout, update_interval);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1008
    }
1009
    schedule_update_flush(net, urgent);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1010 1011
}

1012 1013 1014 1015 1016 1017
void
send_update_resend(struct network *net,
                   const unsigned char *prefix, unsigned char plen)
{
    int delay;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1018 1019
    assert(prefix != NULL);

1020 1021 1022 1023 1024 1025
    send_update(net, 1, prefix, plen);

    delay = 2000;
    delay = MIN(delay, wireless_hello_interval / 2);
    delay = MIN(delay, wired_hello_interval / 2);
    delay = MAX(delay, 10);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1026
    record_resend(RESEND_UPDATE, prefix, plen, 0, 0, NULL, delay);
1027 1028
}

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
void
send_wildcard_retraction(struct network *net)
{
    if(net == NULL) {
        struct network *n;
        FOR_ALL_NETS(n)
            send_wildcard_retraction(n);
        return;
    }

1039
    if(!net_up(net))
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        return;

    start_message(net, MESSAGE_UPDATE, 10);
    accumulate_byte(net, 0);
    accumulate_byte(net, 0x40);
    accumulate_byte(net, 0);
    accumulate_byte(net, 0);
    accumulate_short(net, 0xFFFF);
    accumulate_short(net, myseqno);
    accumulate_short(net, 0xFFFF);
    end_message(net, MESSAGE_UPDATE, 10);

    net->have_buffered_id = 0;
}
1054

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1055
void
1056
update_myseqno()
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1057
{
1058 1059
    myseqno = seqno_plus(myseqno, 1);
    seqno_time = now;
1060 1061 1062
}

void
1063
send_self_update(struct network *net)
1064 1065 1066
{
    int i;

1067
    if(net == NULL) {
1068 1069
        struct network *n;
        FOR_ALL_NETS(n) {
1070
            if(!net_up(n))
1071
                continue;
1072
            send_self_update(n);
1073
        }
1074 1075 1076
        return;
    }

1077 1078 1079 1080 1081
    if(!network_idle(net)) {
        debugf("Sending self update to %s.\n", net->ifname);
        for(i = 0; i < numxroutes; i++)
            send_update(net, 0, xroutes[i].prefix, xroutes[i].plen);
    }
1082
    delay_jitter(&net->self_update_timeout, net->self_update_interval);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1083 1084 1085
}

void
1086
send_ihu(struct neighbour *neigh, struct network *net)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1087
{
1088 1089 1090
    int rxcost, interval;
    int ll;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1091
    if(neigh == NULL && net == NULL) {
1092 1093
        struct network *n;
        FOR_ALL_NETS(n) {
1094
            if(net_up(n))
1095
                continue;
1096
            send_ihu(NULL, n);
1097
        }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1098 1099 1100 1101
        return;
    }

    if(neigh == NULL) {
1102 1103 1104 1105
        struct neighbour *ngh;
        FOR_ALL_NEIGHBOURS(ngh) {
            if(ngh->network == net)
                send_ihu(ngh, net);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1106
        }
1107 1108
        return;
    }
1109

1110

1111 1112
    if(net && neigh->network != net)
        return;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1113

1114
    net = neigh->network;
1115
    if(!net_up(net))
1116 1117
        return;

1118 1119 1120
    rxcost = neighbour_rxcost(neigh);
    interval = (net->hello_interval * 3 + 9) / 10;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1121 1122 1123 1124
    /* Conceptually, an IHU is a unicast message.  We usually send them as
       multicast, since this allows aggregation into a single packet and
       avoids an ARP exchange.  If we already have a unicast message queued
       for this neighbour, however, we might as well piggyback the IHU. */
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    debugf("Sending %sihu %d on %s to %s.\n",
           unicast_neighbour == neigh ? "unicast " : "",
           rxcost,
           neigh->network->ifname,
           format_address(neigh->address));

    ll = in_prefix(neigh->address, ll_prefix, 64);

    if(unicast_neighbour != neigh) {
        start_message(net, MESSAGE_IHU, ll ? 14 : 22);
        accumulate_byte(net, ll ? 3 : 2);
        accumulate_byte(net, 0);
        accumulate_short(net, rxcost);
        accumulate_short(net, interval);
        if(ll)
            accumulate_bytes(net, neigh->address + 8, 8);
        else
            accumulate_bytes(net, neigh->address, 16);
        end_message(net, MESSAGE_IHU, ll ? 14 : 22);
    } else {
        int rc;
        rc = start_unicast_message(neigh, MESSAGE_IHU, ll ? 14 : 22);
        if(rc < 0) return;
        accumulate_unicast_byte(neigh, ll ? 3 : 2);
        accumulate_unicast_byte(neigh, 0);
        accumulate_unicast_short(neigh, rxcost);
        accumulate_unicast_short(neigh, interval);
        if(ll)
            accumulate_unicast_bytes(neigh, neigh->address + 8, 8);
        else
            accumulate_unicast_bytes(neigh, neigh->address, 16);
        end_unicast_message(neigh, MESSAGE_IHU, ll ? 14 : 22);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1157 1158
    }
}
1159 1160 1161 1162 1163

/* Send IHUs to all marginal neighbours */
void
send_marginal_ihu(struct network *net)
{
1164 1165
    struct neighbour *neigh;
    FOR_ALL_NEIGHBOURS(neigh) {
1166
        if(net && neigh->network != net)
1167
            continue;
1168 1169
        if(neigh->txcost >= 384 || (neigh->reach & 0xF000) != 0xF000)
            send_ihu(neigh, net);
1170 1171
    }
}
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

void
send_request(struct network *net,
             const unsigned char *prefix, unsigned char plen)
{
    int v4, len;

    if(net == NULL) {
        struct network *n;
        FOR_ALL_NETS(n) {
1182
            if(net_up(n))
1183 1184 1185 1186 1187 1188 1189
                continue;
            send_request(n, prefix, plen);
        }
        return;
    }

    /* make sure any buffered updates go out before this request. */
1190
    flushupdates(net);
1191

1192
    if(!net_up(net))
1193 1194
        return;

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    debugf("sending request to %s for %s.\n",
           net->ifname, prefix ? format_prefix(prefix, plen) : "any");
    v4 = plen >= 96 && v4mapped(prefix);
    len = !prefix ? 2 : v4 ? 6 : 18;

    start_message(net, MESSAGE_REQUEST, len);
    accumulate_byte(net, !prefix ? 0 : v4 ? 1 : 2);
    accumulate_byte(net, !prefix ? 0 : v4 ? plen - 96 : plen);
    if(prefix) {
        if(v4)
            accumulate_bytes(net, prefix + 12, 4);
        else
            accumulate_bytes(net, prefix, 16);
    }
    end_message(net, MESSAGE_REQUEST, len);
}

void
send_unicast_request(struct neighbour *neigh,
                     const unsigned char *prefix, unsigned char plen)
{
    int rc, v4, len;

    /* make sure any buffered updates go out before this request. */
1219
    flushupdates(neigh->network);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

    debugf("sending unicast request to %s for %s.\n",
           format_address(neigh->address),
           prefix ? format_prefix(prefix, plen) : "any");
    v4 = plen >= 96 && v4mapped(prefix);
    len = !prefix ? 2 : v4 ? 6 : 18;

    rc = start_unicast_message(neigh, MESSAGE_REQUEST, len);
    if(rc < 0) return;
    accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? 1 : 2);
    accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? plen - 96 : plen);
    if(prefix) {
        if(v4)
            accumulate_unicast_bytes(neigh, prefix + 12, 4);
        else
            accumulate_unicast_bytes(neigh, prefix, 16);
    }
    end_unicast_message(neigh, MESSAGE_REQUEST, len);
}

void
send_multihop_request(struct network *net,
                      const unsigned char *prefix, unsigned char plen,
                      unsigned short seqno, const unsigned char *id,
                      unsigned short hop_count)
{
    int v4, pb, len;

    /* Make sure any buffered updates go out before this request. */
1249
    flushupdates(net);
1250 1251 1252 1253

    if(net == NULL) {
        struct network *n;
        FOR_ALL_NETS(n) {
1254
            if(!net_up(n))
1255 1256 1257 1258 1259 1260
                continue;
            send_multihop_request(n, prefix, plen, seqno, id, hop_count);
        }
        return;
    }

1261
    if(!net_up(net))
1262 1263
        return;

1264 1265
    debugf("Sending request (%d) on %s for %s.\n",
           hop_count, net->ifname, format_prefix(prefix, plen));
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    v4 = plen >= 96 && v4mapped(prefix);
    pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
    len = 6 + 8 + pb;

    start_message(net, MESSAGE_MH_REQUEST, len);
    accumulate_byte(net, v4 ? 1 : 2);
    accumulate_byte(net, v4 ? plen - 96 : plen);
    accumulate_short(net, seqno);
    accumulate_byte(net, hop_count);
    accumulate_byte(net, 0);
    accumulate_bytes(net, id, 8);
    if(prefix) {
        if(v4)
            accumulate_bytes(net, prefix + 12, pb);
        else
            accumulate_bytes(net, prefix, pb);
    }
    end_message(net, MESSAGE_MH_REQUEST, len);
}

void
send_unicast_multihop_request(struct neighbour *neigh,
                              const unsigned char *prefix, unsigned char plen,
                              unsigned short seqno, const unsigned char *id,
                              unsigned short hop_count)
{
    int rc, v4, pb, len;

    /* Make sure any buffered updates go out before this request. */
1295
    flushupdates(neigh->network);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

    debugf("Sending multi-hop request to %s for %s (%d hops).\n",
           format_address(neigh->address),
           format_prefix(prefix, plen), hop_count);
    v4 = plen >= 96 && v4mapped(prefix);
    pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
    len = 6 + 8 + pb;

    rc = start_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
    if(rc < 0) return;
    accumulate_unicast_byte(neigh, v4 ? 1 : 2);
    accumulate_unicast_byte(neigh, v4 ? plen - 96 : plen);
    accumulate_unicast_short(neigh, seqno);
    accumulate_unicast_byte(neigh, hop_count);
    accumulate_unicast_byte(neigh, 0);
    accumulate_unicast_bytes(neigh, id, 8);
    if(prefix) {
        if(v4)
            accumulate_unicast_bytes(neigh, prefix + 12, pb);
        else
            accumulate_unicast_bytes(neigh, prefix, pb);
    }
    end_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
}

void
send_request_resend(struct neighbour *neigh,
                    const unsigned char *prefix, unsigned char plen,
                    unsigned short seqno, unsigned char *id)
{
    int delay;

    if(neigh)
        send_unicast_multihop_request(neigh, prefix, plen, seqno, id, 127);
    else
        send_multihop_request(NULL, prefix, plen, seqno, id, 127);

    delay = 2000;
    delay = MIN(delay, wireless_hello_interval / 2);
    delay = MIN(delay, wired_hello_interval / 2);
    delay = MAX(delay, 10);
    record_resend(RESEND_REQUEST, prefix, plen, seqno, id,
                  neigh ? neigh->network : NULL, delay);
}

void
handle_request(struct neighbour *neigh, const unsigned char *prefix,
               unsigned char plen, unsigned char hop_count,
               unsigned short seqno, const unsigned char *id)
{
    struct xroute *xroute;
    struct route *route;
    struct neighbour *successor = NULL;

    xroute = find_xroute(prefix, plen);
    if(xroute) {
        if(hop_count > 0 && memcmp(id, myid, 8) == 0) {
            if(seqno_compare(seqno, myseqno) > 0) {
                if(seqno_minus(seqno, myseqno) > 100) {
                    /* Hopelessly out-of-date request */
                    return;
                }
1358
                update_myseqno();
1359 1360 1361 1362 1363 1364 1365 1366
            }
        }
        send_update(neigh->network, 1, prefix, plen);
        return;
    }

    route = find_installed_route(prefix, plen);
    if(route &&
1367 1368
       (memcmp(id, route->src->id, 8) != 0 ||
        seqno_compare(seqno, route->seqno) <= 0)) {
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        send_update(neigh->network, 1, prefix, plen);
        return;
    }

    if(hop_count <= 1)
        return;

    if(route && memcmp(id, route->src->id, 8) == 0 &&
       seqno_minus(seqno, route->seqno) > 100) {
        /* Hopelessly out-of-date */
        return;
    }

    if(request_redundant(neigh->network, prefix, plen, seqno, id))
        return;

    /* Let's try to forward this request. */
    if(route && route->metric < INFINITY)
        successor = route->neigh;

    if(!successor || successor == neigh) {
        /* We were about to forward a request to its requestor.  Try to
           find a different neighbour to forward the request to. */
        struct route *other_route;

        other_route = find_best_route(prefix, plen, 0, neigh);
        if(other_route && other_route->metric < INFINITY)
            successor = other_route->neigh;
    }

    if(!successor || successor == neigh)
        /* Give up */
        return;

    send_unicast_multihop_request(successor, prefix, plen, seqno, id,
                                  hop_count - 1);
    record_resend(RESEND_REQUEST, prefix, plen, seqno, id,
                  neigh->network, 0);
}