1 /*- 2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <netinet/in.h> 31 #include <netinet/in_systm.h> 32 #include <netinet/ip.h> 33 #include <arpa/inet.h> 34 #include <net/if_dl.h> 35 #include <sys/socket.h> 36 #include <sys/un.h> 37 38 #include <errno.h> 39 #include <paths.h> 40 #include <stdlib.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <sys/stat.h> 44 #include <termios.h> 45 #include <unistd.h> 46 47 #include "layer.h" 48 #ifndef NONAT 49 #include "nat_cmd.h" 50 #endif 51 #include "vjcomp.h" 52 #include "ua.h" 53 #include "defs.h" 54 #include "command.h" 55 #include "mbuf.h" 56 #include "log.h" 57 #include "timer.h" 58 #include "fsm.h" 59 #include "iplist.h" 60 #include "throughput.h" 61 #include "slcompress.h" 62 #include "lqr.h" 63 #include "hdlc.h" 64 #include "ipcp.h" 65 #include "auth.h" 66 #include "lcp.h" 67 #include "async.h" 68 #include "ccp.h" 69 #include "link.h" 70 #include "descriptor.h" 71 #include "physical.h" 72 #include "chat.h" 73 #include "proto.h" 74 #include "filter.h" 75 #include "mp.h" 76 #include "chap.h" 77 #include "cbcp.h" 78 #include "datalink.h" 79 #ifndef NORADIUS 80 #include "radius.h" 81 #endif 82 #include "bundle.h" 83 #include "ip.h" 84 #include "prompt.h" 85 #include "id.h" 86 #include "arp.h" 87 88 void 89 peerid_Init(struct peerid *peer) 90 { 91 peer->enddisc.class = 0; 92 *peer->enddisc.address = '\0'; 93 peer->enddisc.len = 0; 94 *peer->authname = '\0'; 95 } 96 97 int 98 peerid_Equal(const struct peerid *p1, const struct peerid *p2) 99 { 100 return !strcmp(p1->authname, p2->authname) && 101 p1->enddisc.class == p2->enddisc.class && 102 p1->enddisc.len == p2->enddisc.len && 103 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 104 } 105 106 static u_int32_t 107 inc_seq(unsigned is12bit, u_int32_t seq) 108 { 109 seq++; 110 if (is12bit) { 111 if (seq & 0xfffff000) 112 seq = 0; 113 } else if (seq & 0xff000000) 114 seq = 0; 115 return seq; 116 } 117 118 static int 119 isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2) 120 { 121 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200; 122 123 if (seq1 > max) { 124 if (seq2 < 0x200 || seq2 > seq1) 125 return 1; 126 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2) 127 return 1; 128 129 return 0; 130 } 131 132 static int 133 mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 134 { 135 if (mp->local_is12bit) { 136 u_int16_t val; 137 138 ua_ntohs(MBUF_CTOP(m), &val); 139 if (val & 0x3000) { 140 log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 141 return 0; 142 } 143 header->begin = val & 0x8000 ? 1 : 0; 144 header->end = val & 0x4000 ? 1 : 0; 145 header->seq = val & 0x0fff; 146 return 2; 147 } else { 148 ua_ntohl(MBUF_CTOP(m), &header->seq); 149 if (header->seq & 0x3f000000) { 150 log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 151 return 0; 152 } 153 header->begin = header->seq & 0x80000000 ? 1 : 0; 154 header->end = header->seq & 0x40000000 ? 1 : 0; 155 header->seq &= 0x00ffffff; 156 return 4; 157 } 158 } 159 160 static void 161 mp_LayerStart(void *v, struct fsm *fp) 162 { 163 /* The given FSM (ccp) is about to start up ! */ 164 } 165 166 static void 167 mp_LayerUp(void *v, struct fsm *fp) 168 { 169 /* The given fsm (ccp) is now up */ 170 } 171 172 static void 173 mp_LayerDown(void *v, struct fsm *fp) 174 { 175 /* The given FSM (ccp) has been told to come down */ 176 } 177 178 static void 179 mp_LayerFinish(void *v, struct fsm *fp) 180 { 181 /* The given fsm (ccp) is now down */ 182 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 183 fsm_Open(fp); /* CCP goes to ST_STOPPED */ 184 } 185 186 static void 187 mp_UpDown(void *v) 188 { 189 struct mp *mp = (struct mp *)v; 190 int percent; 191 192 percent = MAX(mp->link.stats.total.in.OctetsPerSecond, 193 mp->link.stats.total.out.OctetsPerSecond) * 800 / 194 mp->bundle->bandwidth; 195 if (percent >= mp->cfg.autoload.max) { 196 log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent); 197 bundle_AutoAdjust(mp->bundle, percent, AUTO_UP); 198 } else if (percent <= mp->cfg.autoload.min) { 199 log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent); 200 bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN); 201 } 202 } 203 204 void 205 mp_StopAutoloadTimer(struct mp *mp) 206 { 207 throughput_stop(&mp->link.stats.total); 208 } 209 210 void 211 mp_CheckAutoloadTimer(struct mp *mp) 212 { 213 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) { 214 throughput_destroy(&mp->link.stats.total); 215 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 216 throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 217 } 218 219 if (bundle_WantAutoloadTimer(mp->bundle)) 220 throughput_start(&mp->link.stats.total, "MP throughput", 1); 221 else 222 mp_StopAutoloadTimer(mp); 223 } 224 225 void 226 mp_RestartAutoloadTimer(struct mp *mp) 227 { 228 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) 229 mp_CheckAutoloadTimer(mp); 230 else 231 throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL); 232 } 233 234 void 235 mp_Init(struct mp *mp, struct bundle *bundle) 236 { 237 mp->peer_is12bit = mp->local_is12bit = 0; 238 mp->peer_mrru = mp->local_mrru = 0; 239 240 peerid_Init(&mp->peer); 241 242 mp->out.seq = 0; 243 mp->out.link = 0; 244 mp->seq.min_in = 0; 245 mp->seq.next_in = 0; 246 mp->inbufs = NULL; 247 mp->bundle = bundle; 248 249 mp->link.type = LOGICAL_LINK; 250 mp->link.name = "mp"; 251 mp->link.len = sizeof *mp; 252 253 mp->cfg.autoload.period = SAMPLE_PERIOD; 254 mp->cfg.autoload.min = mp->cfg.autoload.max = 0; 255 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 256 throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 257 mp->link.stats.parent = NULL; 258 mp->link.stats.gather = 0; /* Let the physical links gather stats */ 259 memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 260 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 261 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 262 263 mp->fsmp.LayerStart = mp_LayerStart; 264 mp->fsmp.LayerUp = mp_LayerUp; 265 mp->fsmp.LayerDown = mp_LayerDown; 266 mp->fsmp.LayerFinish = mp_LayerFinish; 267 mp->fsmp.object = mp; 268 269 mpserver_Init(&mp->server); 270 271 mp->cfg.mrru = 0; 272 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 273 mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED; 274 mp->cfg.enddisc.class = 0; 275 *mp->cfg.enddisc.address = '\0'; 276 mp->cfg.enddisc.len = 0; 277 278 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 279 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 280 281 link_EmptyStack(&mp->link); 282 link_Stack(&mp->link, &protolayer); 283 link_Stack(&mp->link, &ccplayer); 284 link_Stack(&mp->link, &vjlayer); 285 #ifndef NONAT 286 link_Stack(&mp->link, &natlayer); 287 #endif 288 } 289 290 int 291 mp_Up(struct mp *mp, struct datalink *dl) 292 { 293 struct lcp *lcp = &dl->physical->link.lcp; 294 295 if (mp->active) { 296 /* We're adding a link - do a last validation on our parameters */ 297 if (!peerid_Equal(&dl->peer, &mp->peer)) { 298 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name); 299 log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname, 300 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 301 mp->peer.enddisc.len)); 302 log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname, 303 mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address, 304 dl->peer.enddisc.len)); 305 return MP_FAILED; 306 } 307 if (mp->local_mrru != lcp->want_mrru || 308 mp->peer_mrru != lcp->his_mrru || 309 mp->local_is12bit != lcp->want_shortseq || 310 mp->peer_is12bit != lcp->his_shortseq) { 311 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n", 312 dl->name); 313 return MP_FAILED; 314 } 315 return MP_ADDED; 316 } else { 317 /* First link in multilink mode */ 318 319 mp->local_mrru = lcp->want_mrru; 320 mp->peer_mrru = lcp->his_mrru; 321 mp->local_is12bit = lcp->want_shortseq; 322 mp->peer_is12bit = lcp->his_shortseq; 323 mp->peer = dl->peer; 324 325 throughput_destroy(&mp->link.stats.total); 326 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 327 throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 328 memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 329 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 330 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 331 332 /* Tell the link who it belongs to */ 333 dl->physical->link.stats.parent = &mp->link.stats.total; 334 335 mp->out.seq = 0; 336 mp->out.link = 0; 337 mp->seq.min_in = 0; 338 mp->seq.next_in = 0; 339 340 /* 341 * Now we create our server socket. 342 * If it already exists, join it. Otherwise, create and own it 343 */ 344 switch (mpserver_Open(&mp->server, &mp->peer)) { 345 case MPSERVER_CONNECTED: 346 log_Printf(LogPHASE, "mp: Transfer link on %s\n", 347 mp->server.socket.sun_path); 348 mp->server.send.dl = dl; /* Defer 'till it's safe to send */ 349 return MP_LINKSENT; 350 case MPSERVER_FAILED: 351 return MP_FAILED; 352 case MPSERVER_LISTENING: 353 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path); 354 log_Printf(LogPHASE, " First link: %s\n", dl->name); 355 356 /* Re-point our IPCP layer at our MP link */ 357 ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link); 358 359 /* Our lcp's already up 'cos of the NULL parent */ 360 if (ccp_SetOpenMode(&mp->link.ccp)) { 361 fsm_Up(&mp->link.ccp.fsm); 362 fsm_Open(&mp->link.ccp.fsm); 363 } 364 365 mp->active = 1; 366 break; 367 } 368 } 369 370 return MP_UP; 371 } 372 373 void 374 mp_Down(struct mp *mp) 375 { 376 if (mp->active) { 377 struct mbuf *next; 378 379 /* Stop that ! */ 380 mp_StopAutoloadTimer(mp); 381 382 /* Don't want any more of these */ 383 mpserver_Close(&mp->server); 384 385 /* CCP goes down with a bang */ 386 fsm2initial(&mp->link.ccp.fsm); 387 388 /* Received fragments go in the bit-bucket */ 389 while (mp->inbufs) { 390 next = mp->inbufs->m_nextpkt; 391 m_freem(mp->inbufs); 392 mp->inbufs = next; 393 } 394 395 peerid_Init(&mp->peer); 396 mp->active = 0; 397 } 398 } 399 400 void 401 mp_linkInit(struct mp_link *mplink) 402 { 403 mplink->seq = 0; 404 mplink->bandwidth = 0; 405 } 406 407 static void 408 mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p) 409 { 410 struct mp_header mh, h; 411 struct mbuf *q, *last; 412 int32_t seq; 413 414 /* 415 * When `m' and `p' are NULL, it means our oldest link has gone down. 416 * We want to determine a new min, and process any intermediate stuff 417 * as normal 418 */ 419 420 if (m && mp_ReadHeader(mp, m, &mh) == 0) { 421 m_freem(m); 422 return; 423 } 424 425 if (p) { 426 seq = p->dl->mp.seq; 427 p->dl->mp.seq = mh.seq; 428 } else 429 seq = mp->seq.min_in; 430 431 if (mp->seq.min_in == seq) { 432 /* 433 * We've received new data on the link that has our min (oldest) seq. 434 * Figure out which link now has the smallest (oldest) seq. 435 */ 436 struct datalink *dl; 437 438 mp->seq.min_in = (u_int32_t)-1; 439 for (dl = mp->bundle->links; dl; dl = dl->next) 440 if (dl->state == DATALINK_OPEN && 441 (mp->seq.min_in == -1 || 442 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in))) 443 mp->seq.min_in = dl->mp.seq; 444 } 445 446 /* 447 * Now process as many of our fragments as we can, adding our new 448 * fragment in as we go, and ordering with the oldest at the top of 449 * the queue. 450 */ 451 452 last = NULL; 453 seq = mp->seq.next_in; 454 q = mp->inbufs; 455 while (q || m) { 456 if (!q) { 457 if (last) 458 last->m_nextpkt = m; 459 else 460 mp->inbufs = m; 461 q = m; 462 m = NULL; 463 h = mh; 464 } else { 465 mp_ReadHeader(mp, q, &h); 466 467 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) { 468 /* Our received fragment fits in before this one, so link it in */ 469 if (last) 470 last->m_nextpkt = m; 471 else 472 mp->inbufs = m; 473 m->m_nextpkt = q; 474 q = m; 475 h = mh; 476 m = NULL; 477 } 478 } 479 480 if (h.seq != seq) { 481 /* we're missing something :-( */ 482 if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) { 483 /* we're never gonna get it */ 484 struct mbuf *next; 485 486 /* Zap all older fragments */ 487 while (mp->inbufs != q) { 488 log_Printf(LogDEBUG, "Drop frag\n"); 489 next = mp->inbufs->m_nextpkt; 490 m_freem(mp->inbufs); 491 mp->inbufs = next; 492 } 493 494 /* 495 * Zap everything until the next `end' fragment OR just before 496 * the next `begin' fragment OR 'till seq.min_in - whichever 497 * comes first. 498 */ 499 do { 500 mp_ReadHeader(mp, mp->inbufs, &h); 501 if (h.begin) { 502 /* We might be able to process this ! */ 503 h.seq--; /* We're gonna look for fragment with h.seq+1 */ 504 break; 505 } 506 next = mp->inbufs->m_nextpkt; 507 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq); 508 m_freem(mp->inbufs); 509 mp->inbufs = next; 510 } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in, 511 h.seq) || h.end)); 512 513 /* 514 * Continue processing things from here. 515 * This deals with the possibility that we received a fragment 516 * on the slowest link that invalidates some of our data (because 517 * of the hole at `q'), but where there are subsequent `whole' 518 * packets that have already been received. 519 */ 520 521 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 522 last = NULL; 523 q = mp->inbufs; 524 } else 525 /* we may still receive the missing fragment */ 526 break; 527 } else if (h.end) { 528 /* We've got something, reassemble */ 529 struct mbuf **frag = &q; 530 int len; 531 u_long first = -1; 532 533 do { 534 *frag = mp->inbufs; 535 mp->inbufs = mp->inbufs->m_nextpkt; 536 len = mp_ReadHeader(mp, *frag, &h); 537 if (first == -1) 538 first = h.seq; 539 (*frag)->m_offset += len; 540 (*frag)->m_len -= len; 541 (*frag)->m_nextpkt = NULL; 542 if (frag == &q && !h.begin) { 543 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 544 (u_long)h.seq); 545 m_freem(q); 546 q = NULL; 547 } else if (frag != &q && h.begin) { 548 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 549 (u_long)h.seq - 1); 550 /* 551 * Stuff our fragment back at the front of the queue and zap 552 * our half-assembed packet. 553 */ 554 (*frag)->m_nextpkt = mp->inbufs; 555 mp->inbufs = *frag; 556 *frag = NULL; 557 m_freem(q); 558 q = NULL; 559 frag = &q; 560 h.end = 0; /* just in case it's a whole packet */ 561 } else 562 do 563 frag = &(*frag)->m_next; 564 while (*frag != NULL); 565 } while (!h.end); 566 567 if (q) { 568 q = m_pullup(q); 569 log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n", 570 first, (u_long)h.seq, m_length(q)); 571 link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle); 572 m_freem(q); 573 } 574 575 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 576 last = NULL; 577 q = mp->inbufs; 578 } else { 579 /* Look for the next fragment */ 580 seq = inc_seq(mp->local_is12bit, seq); 581 last = q; 582 q = q->m_nextpkt; 583 } 584 } 585 586 if (m) { 587 /* We still have to find a home for our new fragment */ 588 last = NULL; 589 for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) { 590 mp_ReadHeader(mp, q, &h); 591 if (isbefore(mp->local_is12bit, mh.seq, h.seq)) 592 break; 593 } 594 /* Our received fragment fits in here */ 595 if (last) 596 last->m_nextpkt = m; 597 else 598 mp->inbufs = m; 599 m->m_nextpkt = q; 600 } 601 } 602 603 struct mbuf * 604 mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 605 { 606 struct physical *p = link2physical(l); 607 608 if (!bundle->ncp.mp.active) 609 /* Let someone else deal with it ! */ 610 return bp; 611 612 if (p == NULL) { 613 log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n"); 614 m_freem(bp); 615 } else { 616 m_settype(bp, MB_MPIN); 617 mp_Assemble(&bundle->ncp.mp, bp, p); 618 } 619 620 return NULL; 621 } 622 623 static void 624 mp_Output(struct mp *mp, struct bundle *bundle, struct link *l, 625 struct mbuf *m, u_int32_t begin, u_int32_t end) 626 { 627 char prepend[4]; 628 629 /* Stuff an MP header on the front of our packet and send it */ 630 631 if (mp->peer_is12bit) { 632 u_int16_t val; 633 634 val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq; 635 ua_htons(&val, prepend); 636 m = m_prepend(m, prepend, 2, 0); 637 } else { 638 u_int32_t val; 639 640 val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq; 641 ua_htonl(&val, prepend); 642 m = m_prepend(m, prepend, 4, 0); 643 } 644 if (log_IsKept(LogDEBUG)) 645 log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n", 646 mp->out.seq, m_length(m), l->name); 647 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq); 648 649 link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP); 650 } 651 652 int 653 mp_FillQueues(struct bundle *bundle) 654 { 655 struct mp *mp = &bundle->ncp.mp; 656 struct datalink *dl, *fdl; 657 size_t total, add, len; 658 int thislink, nlinks; 659 u_int32_t begin, end; 660 struct mbuf *m, *mo; 661 662 thislink = nlinks = 0; 663 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) { 664 /* Include non-open links here as mp->out.link will stay more correct */ 665 if (!fdl) { 666 if (thislink == mp->out.link) 667 fdl = dl; 668 else 669 thislink++; 670 } 671 nlinks++; 672 } 673 674 if (!fdl) { 675 fdl = bundle->links; 676 if (!fdl) 677 return 0; 678 thislink = 0; 679 } 680 681 total = 0; 682 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) { 683 if (!dl) { 684 dl = bundle->links; 685 thislink = 0; 686 } 687 688 if (dl->state != DATALINK_OPEN) 689 continue; 690 691 if (dl->physical->out) 692 /* this link has suffered a short write. Let it continue */ 693 continue; 694 695 add = link_QueueLen(&dl->physical->link); 696 if (add) { 697 /* this link has got stuff already queued. Let it continue */ 698 total += add; 699 continue; 700 } 701 702 if (!link_QueueLen(&mp->link)) { 703 struct datalink *other; 704 int mrutoosmall; 705 706 /* 707 * If there's only a single open link in our bundle and we haven't got 708 * MP level link compression, queue outbound traffic directly via that 709 * link's protocol stack rather than using the MP link. This results 710 * in the outbound traffic going out as PROTO_IP rather than PROTO_MP. 711 */ 712 for (other = dl->next; other; other = other->next) 713 if (other->state == DATALINK_OPEN) 714 break; 715 716 mrutoosmall = 0; 717 if (!other) { 718 if (dl->physical->link.lcp.his_mru < mp->peer_mrru) { 719 /* 720 * Actually, forget it. This test is done against the MRRU rather 721 * than the packet size so that we don't end up sending some data 722 * in MP fragments and some data in PROTO_IP packets. That's just 723 * too likely to upset some ppp implementations. 724 */ 725 mrutoosmall = 1; 726 other = dl; 727 } 728 } 729 730 if (!ip_PushPacket(other ? &mp->link : &dl->physical->link, bundle)) 731 /* Nothing else to send */ 732 break; 733 734 if (mrutoosmall) 735 log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n"); 736 else if (!other) 737 log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n"); 738 739 if (!other) { 740 add = link_QueueLen(&dl->physical->link); 741 if (add) { 742 /* this link has got stuff already queued. Let it continue */ 743 total += add; 744 continue; 745 } 746 } 747 } 748 749 m = link_Dequeue(&mp->link); 750 if (m) { 751 len = m_length(m); 752 begin = 1; 753 end = 0; 754 755 while (!end) { 756 if (dl->state == DATALINK_OPEN) { 757 /* Write at most his_mru bytes to the physical link */ 758 if (len <= dl->physical->link.lcp.his_mru) { 759 mo = m; 760 end = 1; 761 m_settype(mo, MB_MPOUT); 762 } else { 763 /* It's > his_mru, chop the packet (`m') into bits */ 764 mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT); 765 len -= mo->m_len; 766 m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len); 767 } 768 mp_Output(mp, bundle, &dl->physical->link, mo, begin, end); 769 begin = 0; 770 } 771 772 if (!end) { 773 nlinks--; 774 dl = dl->next; 775 if (!dl) { 776 dl = bundle->links; 777 thislink = 0; 778 } else 779 thislink++; 780 } 781 } 782 } 783 } 784 mp->out.link = thislink; /* Start here next time */ 785 786 return total; 787 } 788 789 int 790 mp_SetDatalinkBandwidth(struct cmdargs const *arg) 791 { 792 int val; 793 794 if (arg->argc != arg->argn+1) 795 return -1; 796 797 val = atoi(arg->argv[arg->argn]); 798 if (val <= 0) { 799 log_Printf(LogWARN, "The link bandwidth must be greater than zero\n"); 800 return 1; 801 } 802 arg->cx->mp.bandwidth = val; 803 804 if (arg->cx->state == DATALINK_OPEN) 805 bundle_CalculateBandwidth(arg->bundle); 806 807 return 0; 808 } 809 810 int 811 mp_ShowStatus(struct cmdargs const *arg) 812 { 813 struct mp *mp = &arg->bundle->ncp.mp; 814 815 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 816 if (mp->active) { 817 struct mbuf *m, *lm; 818 int bufs = 0; 819 820 lm = NULL; 821 prompt_Printf(arg->prompt, "Socket: %s\n", 822 mp->server.socket.sun_path); 823 for (m = mp->inbufs; m; m = m->m_nextpkt) { 824 bufs++; 825 lm = m; 826 } 827 prompt_Printf(arg->prompt, "Pending frags: %d", bufs); 828 if (bufs) { 829 struct mp_header mh; 830 unsigned long first, last; 831 832 first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0; 833 last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0; 834 prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n", 835 first, last, (unsigned long)mp->seq.next_in, 836 (unsigned long)mp->seq.min_in); 837 prompt_Printf(arg->prompt, " First has %sbegin bit and " 838 "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no "); 839 } 840 prompt_Printf(arg->prompt, "\n"); 841 } 842 843 prompt_Printf(arg->prompt, "\nMy Side:\n"); 844 if (mp->active) { 845 prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq); 846 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 847 prompt_Printf(arg->prompt, " Short Seq: %s\n", 848 mp->local_is12bit ? "on" : "off"); 849 } 850 prompt_Printf(arg->prompt, " Discriminator: %s\n", 851 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 852 mp->cfg.enddisc.len)); 853 854 prompt_Printf(arg->prompt, "\nHis Side:\n"); 855 if (mp->active) { 856 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 857 prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in); 858 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 859 prompt_Printf(arg->prompt, " Short Seq: %s\n", 860 mp->peer_is12bit ? "on" : "off"); 861 } 862 prompt_Printf(arg->prompt, " Discriminator: %s\n", 863 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 864 mp->peer.enddisc.len)); 865 866 prompt_Printf(arg->prompt, "\nDefaults:\n"); 867 868 prompt_Printf(arg->prompt, " MRRU: "); 869 if (mp->cfg.mrru) 870 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 871 else 872 prompt_Printf(arg->prompt, "disabled\n"); 873 prompt_Printf(arg->prompt, " Short Seq: %s\n", 874 command_ShowNegval(mp->cfg.shortseq)); 875 prompt_Printf(arg->prompt, " Discriminator: %s\n", 876 command_ShowNegval(mp->cfg.negenddisc)); 877 prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%," 878 " period %d secs\n", mp->cfg.autoload.min, 879 mp->cfg.autoload.max, mp->cfg.autoload.period); 880 881 return 0; 882 } 883 884 const char * 885 mp_Enddisc(u_char c, const char *address, int len) 886 { 887 static char result[100]; /* Used immediately after it's returned */ 888 int f, header; 889 890 switch (c) { 891 case ENDDISC_NULL: 892 sprintf(result, "Null Class"); 893 break; 894 895 case ENDDISC_LOCAL: 896 snprintf(result, sizeof result, "Local Addr: %.*s", len, address); 897 break; 898 899 case ENDDISC_IP: 900 if (len == 4) 901 snprintf(result, sizeof result, "IP %s", 902 inet_ntoa(*(const struct in_addr *)address)); 903 else 904 sprintf(result, "IP[%d] ???", len); 905 break; 906 907 case ENDDISC_MAC: 908 if (len == 6) { 909 const u_char *m = (const u_char *)address; 910 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 911 m[0], m[1], m[2], m[3], m[4], m[5]); 912 } else 913 sprintf(result, "MAC[%d] ???", len); 914 break; 915 916 case ENDDISC_MAGIC: 917 sprintf(result, "Magic: 0x"); 918 header = strlen(result); 919 if (len > sizeof result - header - 1) 920 len = sizeof result - header - 1; 921 for (f = 0; f < len; f++) 922 sprintf(result + header + 2 * f, "%02x", address[f]); 923 break; 924 925 case ENDDISC_PSN: 926 snprintf(result, sizeof result, "PSN: %.*s", len, address); 927 break; 928 929 default: 930 sprintf(result, "%d: ", (int)c); 931 header = strlen(result); 932 if (len > sizeof result - header - 1) 933 len = sizeof result - header - 1; 934 for (f = 0; f < len; f++) 935 sprintf(result + header + 2 * f, "%02x", address[f]); 936 break; 937 } 938 return result; 939 } 940 941 int 942 mp_SetEnddisc(struct cmdargs const *arg) 943 { 944 struct mp *mp = &arg->bundle->ncp.mp; 945 struct in_addr addr; 946 947 switch (bundle_Phase(arg->bundle)) { 948 case PHASE_DEAD: 949 break; 950 case PHASE_ESTABLISH: 951 /* Make sure none of our links are DATALINK_LCP or greater */ 952 if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) { 953 log_Printf(LogWARN, "enddisc: Only changable before" 954 " LCP negotiations\n"); 955 return 1; 956 } 957 break; 958 default: 959 log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n"); 960 return 1; 961 } 962 963 if (arg->argc == arg->argn) { 964 mp->cfg.enddisc.class = 0; 965 *mp->cfg.enddisc.address = '\0'; 966 mp->cfg.enddisc.len = 0; 967 } else if (arg->argc > arg->argn) { 968 if (!strcasecmp(arg->argv[arg->argn], "label")) { 969 mp->cfg.enddisc.class = ENDDISC_LOCAL; 970 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 971 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 972 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 973 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 974 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 975 else 976 addr = arg->bundle->ncp.ipcp.my_ip; 977 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 978 mp->cfg.enddisc.class = ENDDISC_IP; 979 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 980 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 981 struct sockaddr_dl hwaddr; 982 int s; 983 984 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 985 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 986 else 987 addr = arg->bundle->ncp.ipcp.my_ip; 988 989 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 990 if (s < 0) { 991 log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno)); 992 return 2; 993 } 994 if (get_ether_addr(s, addr, &hwaddr)) { 995 mp->cfg.enddisc.class = ENDDISC_MAC; 996 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 997 hwaddr.sdl_alen); 998 mp->cfg.enddisc.len = hwaddr.sdl_alen; 999 } else { 1000 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 1001 inet_ntoa(addr)); 1002 close(s); 1003 return 4; 1004 } 1005 close(s); 1006 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 1007 int f; 1008 1009 randinit(); 1010 for (f = 0; f < 20; f += sizeof(long)) 1011 *(long *)(mp->cfg.enddisc.address + f) = random(); 1012 mp->cfg.enddisc.class = ENDDISC_MAGIC; 1013 mp->cfg.enddisc.len = 20; 1014 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 1015 if (arg->argc > arg->argn+1) { 1016 mp->cfg.enddisc.class = ENDDISC_PSN; 1017 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 1018 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 1019 } else { 1020 log_Printf(LogWARN, "PSN endpoint requires additional data\n"); 1021 return 5; 1022 } 1023 } else { 1024 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n", 1025 arg->argv[arg->argn]); 1026 return 6; 1027 } 1028 } 1029 1030 return 0; 1031 } 1032 1033 static int 1034 mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 1035 int *n) 1036 { 1037 struct mpserver *s = descriptor2mpserver(d); 1038 int result; 1039 1040 result = 0; 1041 if (s->send.dl != NULL) { 1042 /* We've connect()ed */ 1043 if (!link_QueueLen(&s->send.dl->physical->link) && 1044 !s->send.dl->physical->out) { 1045 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */ 1046 result -= datalink_RemoveFromSet(s->send.dl, r, w, e); 1047 bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 1048 s->send.dl = NULL; 1049 s->fd = -1; 1050 } else 1051 /* Never read from a datalink that's on death row ! */ 1052 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL); 1053 } else if (r && s->fd >= 0) { 1054 if (*n < s->fd + 1) 1055 *n = s->fd + 1; 1056 FD_SET(s->fd, r); 1057 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd); 1058 result++; 1059 } 1060 return result; 1061 } 1062 1063 static int 1064 mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset) 1065 { 1066 struct mpserver *s = descriptor2mpserver(d); 1067 return s->fd >= 0 && FD_ISSET(s->fd, fdset); 1068 } 1069 1070 static void 1071 mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 1072 { 1073 struct mpserver *s = descriptor2mpserver(d); 1074 1075 bundle_ReceiveDatalink(bundle, s->fd); 1076 } 1077 1078 static int 1079 mpserver_Write(struct fdescriptor *d, struct bundle *bundle, 1080 const fd_set *fdset) 1081 { 1082 /* We never want to write here ! */ 1083 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n"); 1084 return 0; 1085 } 1086 1087 void 1088 mpserver_Init(struct mpserver *s) 1089 { 1090 s->desc.type = MPSERVER_DESCRIPTOR; 1091 s->desc.UpdateSet = mpserver_UpdateSet; 1092 s->desc.IsSet = mpserver_IsSet; 1093 s->desc.Read = mpserver_Read; 1094 s->desc.Write = mpserver_Write; 1095 s->send.dl = NULL; 1096 s->fd = -1; 1097 memset(&s->socket, '\0', sizeof s->socket); 1098 } 1099 1100 int 1101 mpserver_Open(struct mpserver *s, struct peerid *peer) 1102 { 1103 int f, l; 1104 mode_t mask; 1105 1106 if (s->fd != -1) { 1107 log_Printf(LogALERT, "Internal error ! mpserver already open\n"); 1108 mpserver_Close(s); 1109 } 1110 1111 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-", 1112 _PATH_VARRUN, peer->authname, peer->enddisc.class); 1113 1114 for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) { 1115 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l, 1116 "%02x", *(u_char *)(peer->enddisc.address+f)); 1117 l += 2; 1118 } 1119 1120 s->socket.sun_family = AF_LOCAL; 1121 s->socket.sun_len = sizeof s->socket; 1122 s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0); 1123 if (s->fd < 0) { 1124 log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno)); 1125 return MPSERVER_FAILED; 1126 } 1127 1128 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket, 1129 sizeof s->socket); 1130 mask = umask(0177); 1131 1132 /* 1133 * Try to bind the socket. If we succeed we play server, if we fail 1134 * we connect() and hand the link off. 1135 */ 1136 1137 if (ID0bind_un(s->fd, &s->socket) < 0) { 1138 if (errno != EADDRINUSE) { 1139 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n", 1140 s->socket.sun_path, strerror(errno)); 1141 umask(mask); 1142 close(s->fd); 1143 s->fd = -1; 1144 return MPSERVER_FAILED; 1145 } 1146 1147 /* So we're the sender */ 1148 umask(mask); 1149 if (ID0connect_un(s->fd, &s->socket) < 0) { 1150 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n", 1151 s->socket.sun_path, strerror(errno)); 1152 if (errno == ECONNREFUSED) 1153 log_Printf(LogPHASE, " The previous server died badly !\n"); 1154 close(s->fd); 1155 s->fd = -1; 1156 return MPSERVER_FAILED; 1157 } 1158 1159 /* Donate our link to the other guy */ 1160 return MPSERVER_CONNECTED; 1161 } 1162 1163 return MPSERVER_LISTENING; 1164 } 1165 1166 void 1167 mpserver_Close(struct mpserver *s) 1168 { 1169 if (s->send.dl != NULL) { 1170 bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 1171 s->send.dl = NULL; 1172 s->fd = -1; 1173 } else if (s->fd >= 0) { 1174 close(s->fd); 1175 if (ID0unlink(s->socket.sun_path) == -1) 1176 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path, 1177 strerror(errno)); 1178 memset(&s->socket, '\0', sizeof s->socket); 1179 s->fd = -1; 1180 } 1181 } 1182 1183 void 1184 mp_LinkLost(struct mp *mp, struct datalink *dl) 1185 { 1186 if (mp->seq.min_in == dl->mp.seq) 1187 /* We've lost the link that's holding everything up ! */ 1188 mp_Assemble(mp, NULL, NULL); 1189 } 1190 1191 void 1192 mp_DeleteQueue(struct mp *mp) 1193 { 1194 link_DeleteQueue(&mp->link); 1195 } 1196