message.c 42.7 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

int parasitic = 0;
int split_horizon = 1;

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

51 52 53 54 55 56
#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};

57 58 59 60 61 62 63 64 65
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
66
{
67 68 69 70 71 72 73 74 75 76
    unsigned pb;
    unsigned char prefix[16];

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

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

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

    switch(ae) {
83
    case 0: break;
84
    case 1:
85 86 87 88 89 90 91
        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);
92 93 94
        break;
    case 2:
        if(pb > omitted && len < pb - omitted) return -1;
95 96 97 98
        if(omitted) {
            if (dp == NULL || v4mapped(dp)) return -1;
            memcpy(prefix, dp, omitted);
        }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        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
120 121
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
122 123
void
parse_packet(const unsigned char *from, struct network *net,
124
             const unsigned char *packet, int packetlen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
125
{
126
    int i;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
127
    const unsigned char *message;
128 129
    unsigned char type, len;
    int bodylen;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
130
    struct neighbour *neigh;
131 132 133 134 135
    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
136

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

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

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

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

162 163 164 165 166 167 168 169 170 171 172
    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;
173
        type = message[0];
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        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;
195
            if(len < 6) goto fail;
196 197 198 199 200 201 202 203 204 205 206
            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;
207
            int changed;
208 209 210 211 212 213
            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);
214
            net->activity_time = now.tv_sec;
215
            update_hello_interval(net);
216
            changed = update_neighbour(neigh, seqno, interval);
217 218
            if(changed)
                update_neighbour_metric(neigh);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            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);
241
            }
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
        } 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;
309
                } else {
310 311
                    memcpy(v6_prefix, prefix, 16);
                    have_v6_prefix = 1;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
312
                }
313 314 315 316 317 318 319
            }
            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);
320
                }
321 322
                have_router_id = 1;
            }
323
            if(!have_router_id && message[2] != 0) {
324 325 326 327 328 329 330 331 332
                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);

333 334 335 336 337 338 339 340 341
            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) {
342 343 344 345 346
                if(!have_v4_nh)
                    goto fail;
                nh = v4_nh;
            } else if(have_v6_nh) {
                nh = v6_nh;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
347
            } else {
348
                nh = neigh->address;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
349
            }
350

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

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

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

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

static int
check_bucket(struct network *net)
{
417 418 419 420 421 422 423 424
    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;
425 426
    }

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

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

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

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

    if(net->buffered > 0) {
        debugf("  (flushing %d buffered bytes on %s)\n",
               net->buffered, net->ifname);
448 449 450 451 452 453
        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;
454
            DO_HTONS(packet_header + 2, net->buffered);
455 456 457 458 459 460 461 462 463 464
            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
465 466 467
    }
    VALGRIND_MAKE_MEM_UNDEFINED(net->sendbuf, net->bufsize);
    net->buffered = 0;
468 469 470 471
    net->have_buffered_hello = 0;
    net->have_buffered_id = 0;
    net->have_buffered_nh = 0;
    net->have_buffered_prefix = 0;
472 473
    net->flush_timeout.tv_sec = 0;
    net->flush_timeout.tv_usec = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
474 475 476 477 478
}

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

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

497
static void
498
schedule_unicast_flush(unsigned msecs)
499 500 501 502 503 504 505 506 507 508 509
{
    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
510
static void
511
ensure_space(struct network *net, int space)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
512
{
513
    if(net->bufsize - net->buffered < space)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
514 515 516
        flushbuf(net);
}

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

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

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

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

548 549 550
static void
accumulate_bytes(struct network *net,
                 const unsigned char *value, unsigned len)
551
{
552 553 554 555 556 557 558 559 560 561 562 563
    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);
564
    }
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    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
580 581
end_unicast_message(struct neighbour *neigh, int type, int bytes)
{
582 583 584
    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
585
    schedule_unicast_flush(jitter(neigh->network, 0));
586 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
}

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);
618 619
    /* Roughly yields a value no larger than 3/2, so this meets the deadline */
    schedule_unicast_flush(roughly(interval * 6));
620 621
}

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

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

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

636 637 638 639 640 641 642 643 644
    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;
645 646 647 648 649 650 651
}

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

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

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

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

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

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

 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;
}

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

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

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

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

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
    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;
    }

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

    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);
777
    accumulate_short(net, (net->update_interval + 5) / 10);
778 779 780 781 782 783 784 785 786
    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
787 788
}

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

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

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

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

807 808
    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);
809

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

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

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

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

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

    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;
846

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

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

853 854 855 856
        /* 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++) {
857
            route = find_installed_route(b[i].prefix, b[i].plen);
858
            if(route)
859
                memcpy(b[i].id, route->src->id, 8);
860
            else
861
                memcpy(b[i].id, myid, 8);
862 863
        }

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

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

            /* 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) {
875 876
                if(b[i].plen == last_plen &&
                   memcmp(b[i].prefix, last_prefix, 16) == 0)
877 878 879
                    continue;
            }

880
            xroute = find_xroute(b[i].prefix, b[i].plen);
881 882 883
            if(xroute) {
                really_send_update(net, myid,
                                   xroute->prefix, xroute->plen,
884
                                   myseqno, xroute->metric);
885 886 887
                last_prefix = xroute->prefix;
                last_plen = xroute->plen;
                continue;
888
            }
889
            route = find_installed_route(b[i].prefix, b[i].plen);
890 891
            if(route) {
                seqno = route->seqno;
892
                metric = route->metric;
893 894
                if(metric < INFINITY)
                    satisfy_request(route->src->prefix, route->src->plen,
895
                                    seqno, route->src->id, net);
896
                if((net->flags & NET_SPLIT_HORIZON) &&
897
                   route->neigh->network == net)
898
                    continue;
899
                really_send_update(net, route->src->id,
900 901
                                   route->src->prefix,
                                   route->src->plen,
902
                                   seqno, metric);
903
                update_source(route->src, seqno, metric);
904 905 906
                last_prefix = route->src->prefix;
                last_plen = route->src->plen;
                continue;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
907
            }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
908 909 910 911 912
            /* If we reach this point, there's no route for this prefix.
               This can happen after an xroute has been retracted, so send
               a retraction. */
            really_send_update(net, myid, b[i].prefix, b[i].plen,
                               myseqno, INFINITY);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
913 914
        }
        schedule_flush_now(net);
915 916
    done:
        free(b);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
917
    }
918 919
    net->update_flush_timeout.tv_sec = 0;
    net->update_flush_timeout.tv_usec = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
920 921 922
}

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

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

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    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
958

959 960 961 962
    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
963 964 965
}

void
966
send_update(struct network *net, int urgent,
967
            const unsigned char *prefix, unsigned char plen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
968
{
969
    int i;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
970 971

    if(net == NULL) {
972
        struct network *n;
973
        struct route *route;
974 975
        FOR_ALL_NETS(n)
            send_update(n, urgent, prefix, plen);
976 977 978 979 980 981 982 983
        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
984 985 986
        return;
    }

987
    if(!net_up(net))
988 989
        return;

990
    if(prefix) {
991
        if(!parasitic || find_xroute(prefix, plen)) {
992 993 994 995
            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
996
    } else {
997 998 999 1000 1001 1002 1003 1004 1005 1006
        if(!network_idle(net)) {
            send_self_update(net);
            if(!parasitic) {
                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
        }
1008
        delay_jitter(&net->update_timeout, net->update_interval);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1009
    }
1010
    schedule_update_flush(net, urgent);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1011 1012
}

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

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

1021 1022 1023 1024 1025 1026
    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
1027
    record_resend(RESEND_UPDATE, prefix, plen, 0, 0, NULL, delay);
1028 1029
}

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

1040
    if(!net_up(net))
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
        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;
}
1055

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

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

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

1078 1079 1080 1081 1082
    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);
    }
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);
}