mp.c revision 37320
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 * $Id: mp.c,v 1.11 1998/06/24 19:33:34 brian Exp $ 27 */ 28 29#include <sys/types.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 "defs.h" 48#include "command.h" 49#include "mbuf.h" 50#include "log.h" 51#include "timer.h" 52#include "fsm.h" 53#include "iplist.h" 54#include "throughput.h" 55#include "slcompress.h" 56#include "ipcp.h" 57#include "auth.h" 58#include "lcp.h" 59#include "lqr.h" 60#include "hdlc.h" 61#include "async.h" 62#include "ccp.h" 63#include "link.h" 64#include "descriptor.h" 65#include "physical.h" 66#include "chat.h" 67#include "lcpproto.h" 68#include "filter.h" 69#include "mp.h" 70#include "chap.h" 71#include "datalink.h" 72#include "bundle.h" 73#include "ip.h" 74#include "prompt.h" 75#include "id.h" 76#include "arp.h" 77 78void 79peerid_Init(struct peerid *peer) 80{ 81 peer->enddisc.class = 0; 82 *peer->enddisc.address = '\0'; 83 peer->enddisc.len = 0; 84 *peer->authname = '\0'; 85} 86 87int 88peerid_Equal(const struct peerid *p1, const struct peerid *p2) 89{ 90 return !strcmp(p1->authname, p2->authname) && 91 p1->enddisc.class == p2->enddisc.class && 92 p1->enddisc.len == p2->enddisc.len && 93 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 94} 95 96static u_int32_t 97inc_seq(unsigned is12bit, u_int32_t seq) 98{ 99 seq++; 100 if (is12bit) { 101 if (seq & 0xfffff000) 102 seq = 0; 103 } else if (seq & 0xff000000) 104 seq = 0; 105 return seq; 106} 107 108static int 109isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2) 110{ 111 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200; 112 113 if (seq1 > max) { 114 if (seq2 < 0x200 || seq2 > seq1) 115 return 1; 116 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2) 117 return 1; 118 119 return 0; 120} 121 122static int 123mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 124{ 125 if (mp->local_is12bit) { 126 header->seq = ntohs(*(u_int16_t *)MBUF_CTOP(m)); 127 if (header->seq & 0x3000) { 128 log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 129 return 0; 130 } 131 header->begin = header->seq & 0x8000 ? 1 : 0; 132 header->end = header->seq & 0x4000 ? 1 : 0; 133 header->seq &= 0x0fff; 134 return 2; 135 } else { 136 header->seq = ntohl(*(u_int32_t *)MBUF_CTOP(m)); 137 if (header->seq & 0x3f000000) { 138 log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 139 return 0; 140 } 141 header->begin = header->seq & 0x80000000 ? 1 : 0; 142 header->end = header->seq & 0x40000000 ? 1 : 0; 143 header->seq &= 0x00ffffff; 144 return 4; 145 } 146} 147 148static void 149mp_LayerStart(void *v, struct fsm *fp) 150{ 151 /* The given FSM (ccp) is about to start up ! */ 152} 153 154static void 155mp_LayerUp(void *v, struct fsm *fp) 156{ 157 /* The given fsm (ccp) is now up */ 158} 159 160static void 161mp_LayerDown(void *v, struct fsm *fp) 162{ 163 /* The given FSM (ccp) has been told to come down */ 164} 165 166static void 167mp_LayerFinish(void *v, struct fsm *fp) 168{ 169 /* The given fsm (ccp) is now down */ 170 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 171 fsm_Open(fp); /* CCP goes to ST_STOPPED */ 172} 173 174void 175mp_Init(struct mp *mp, struct bundle *bundle) 176{ 177 mp->peer_is12bit = mp->local_is12bit = 0; 178 mp->peer_mrru = mp->local_mrru = 0; 179 180 peerid_Init(&mp->peer); 181 182 mp->out.seq = 0; 183 mp->out.link = 0; 184 mp->seq.min_in = 0; 185 mp->seq.next_in = 0; 186 mp->inbufs = NULL; 187 mp->bundle = bundle; 188 189 mp->link.type = MP_LINK; 190 mp->link.name = "mp"; 191 mp->link.len = sizeof *mp; 192 193 throughput_init(&mp->link.throughput); 194 memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 195 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 196 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 197 198 mp->fsmp.LayerStart = mp_LayerStart; 199 mp->fsmp.LayerUp = mp_LayerUp; 200 mp->fsmp.LayerDown = mp_LayerDown; 201 mp->fsmp.LayerFinish = mp_LayerFinish; 202 mp->fsmp.object = mp; 203 204 mpserver_Init(&mp->server); 205 206 mp->cfg.mrru = 0; 207 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 208 mp->cfg.enddisc.class = 0; 209 *mp->cfg.enddisc.address = '\0'; 210 mp->cfg.enddisc.len = 0; 211 212 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 213 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 214} 215 216int 217mp_Up(struct mp *mp, struct datalink *dl) 218{ 219 struct lcp *lcp = &dl->physical->link.lcp; 220 221 if (mp->active) { 222 /* We're adding a link - do a last validation on our parameters */ 223 if (!peerid_Equal(&dl->peer, &mp->peer)) { 224 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name); 225 return MP_FAILED; 226 } 227 if (mp->local_mrru != lcp->want_mrru || 228 mp->peer_mrru != lcp->his_mrru || 229 mp->local_is12bit != lcp->want_shortseq || 230 mp->peer_is12bit != lcp->his_shortseq) { 231 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n", 232 dl->name); 233 return MP_FAILED; 234 } 235 return MP_ADDED; 236 } else { 237 /* First link in multilink mode */ 238 239 mp->local_mrru = lcp->want_mrru; 240 mp->peer_mrru = lcp->his_mrru; 241 mp->local_is12bit = lcp->want_shortseq; 242 mp->peer_is12bit = lcp->his_shortseq; 243 mp->peer = dl->peer; 244 245 throughput_init(&mp->link.throughput); 246 memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 247 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 248 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 249 250 mp->out.seq = 0; 251 mp->out.link = 0; 252 mp->seq.min_in = 0; 253 mp->seq.next_in = 0; 254 255 /* 256 * Now we create our server socket. 257 * If it already exists, join it. Otherwise, create and own it 258 */ 259 switch (mpserver_Open(&mp->server, &mp->peer)) { 260 case MPSERVER_CONNECTED: 261 log_Printf(LogPHASE, "mp: Transfer link on %s\n", 262 mp->server.socket.sun_path); 263 mp->server.send.dl = dl; /* Defer 'till it's safe to send */ 264 return MP_LINKSENT; 265 case MPSERVER_FAILED: 266 return MP_FAILED; 267 case MPSERVER_LISTENING: 268 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path); 269 log_Printf(LogPHASE, " First link: %s\n", dl->name); 270 271 /* Re-point our IPCP layer at our MP link */ 272 ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link); 273 274 /* Our lcp's already up 'cos of the NULL parent */ 275 if (ccp_SetOpenMode(&mp->link.ccp)) { 276 fsm_Up(&mp->link.ccp.fsm); 277 fsm_Open(&mp->link.ccp.fsm); 278 } 279 280 mp->active = 1; 281 break; 282 } 283 } 284 285 return MP_UP; 286} 287 288void 289mp_Down(struct mp *mp) 290{ 291 if (mp->active) { 292 struct mbuf *next; 293 294 /* Don't want any more of these */ 295 mpserver_Close(&mp->server); 296 297 /* CCP goes down with a bang */ 298 fsm2initial(&mp->link.ccp.fsm); 299 300 /* Received fragments go in the bit-bucket */ 301 while (mp->inbufs) { 302 next = mp->inbufs->pnext; 303 mbuf_Free(mp->inbufs); 304 mp->inbufs = next; 305 } 306 307 peerid_Init(&mp->peer); 308 mp->active = 0; 309 } 310} 311 312void 313mp_linkInit(struct mp_link *mplink) 314{ 315 mplink->seq = 0; 316 mplink->weight = 1500; 317} 318 319void 320mp_Input(struct mp *mp, struct mbuf *m, struct physical *p) 321{ 322 struct mp_header mh, h; 323 struct mbuf *q, *last; 324 int32_t seq; 325 326 /* 327 * When `m' and `p' are NULL, it means our oldest link has gone down. 328 * We want to determine a new min, and process any intermediate stuff 329 * as normal 330 */ 331 332 if (m && mp_ReadHeader(mp, m, &mh) == 0) { 333 mbuf_Free(m); 334 return; 335 } 336 337 if (p) { 338 seq = p->dl->mp.seq; 339 p->dl->mp.seq = mh.seq; 340 } else 341 seq = mp->seq.min_in; 342 343 if (mp->seq.min_in == seq) { 344 /* 345 * We've received new data on the link that has our min (oldest) seq. 346 * Figure out which link now has the smallest (oldest) seq. 347 */ 348 struct datalink *dl; 349 350 mp->seq.min_in = (u_int32_t)-1; 351 for (dl = mp->bundle->links; dl; dl = dl->next) 352 if (dl->state == DATALINK_OPEN && 353 (mp->seq.min_in == -1 || 354 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in))) 355 mp->seq.min_in = dl->mp.seq; 356 } 357 358 /* 359 * Now process as many of our fragments as we can, adding our new 360 * fragment in as we go, and ordering with the oldest at the top of 361 * the queue. 362 */ 363 364 if (!mp->inbufs) { 365 mp->inbufs = m; 366 m = NULL; 367 } 368 369 last = NULL; 370 seq = mp->seq.next_in; 371 q = mp->inbufs; 372 while (q) { 373 mp_ReadHeader(mp, q, &h); 374 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) { 375 /* Our received fragment fits in before this one, so link it in */ 376 if (last) 377 last->pnext = m; 378 else 379 mp->inbufs = m; 380 m->pnext = q; 381 q = m; 382 h = mh; 383 m = NULL; 384 } 385 386 if (h.seq != seq) { 387 /* we're missing something :-( */ 388 if (mp->seq.min_in > seq) { 389 /* we're never gonna get it */ 390 struct mbuf *next; 391 392 /* Zap all older fragments */ 393 while (mp->inbufs != q) { 394 log_Printf(LogDEBUG, "Drop frag\n"); 395 next = mp->inbufs->pnext; 396 mbuf_Free(mp->inbufs); 397 mp->inbufs = next; 398 } 399 400 /* 401 * Zap everything until the next `end' fragment OR just before 402 * the next `begin' fragment OR 'till seq.min_in - whichever 403 * comes first. 404 */ 405 do { 406 mp_ReadHeader(mp, mp->inbufs, &h); 407 if (h.begin) { 408 /* We might be able to process this ! */ 409 h.seq--; /* We're gonna look for fragment with h.seq+1 */ 410 break; 411 } 412 next = mp->inbufs->pnext; 413 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq); 414 mbuf_Free(mp->inbufs); 415 mp->inbufs = next; 416 } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end)); 417 418 /* 419 * Continue processing things from here. 420 * This deals with the possibility that we received a fragment 421 * on the slowest link that invalidates some of our data (because 422 * of the hole at `q'), but where there are subsequent `whole' 423 * packets that have already been received. 424 */ 425 426 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 427 last = NULL; 428 q = mp->inbufs; 429 } else 430 /* we may still receive the missing fragment */ 431 break; 432 } else if (h.end) { 433 /* We've got something, reassemble */ 434 struct mbuf **frag = &q; 435 int len; 436 u_long first = -1; 437 438 do { 439 *frag = mp->inbufs; 440 mp->inbufs = mp->inbufs->pnext; 441 len = mp_ReadHeader(mp, *frag, &h); 442 if (first == -1) 443 first = h.seq; 444 (*frag)->offset += len; 445 (*frag)->cnt -= len; 446 (*frag)->pnext = NULL; 447 if (frag == &q && !h.begin) { 448 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 449 (u_long)h.seq); 450 mbuf_Free(q); 451 q = NULL; 452 } else if (frag != &q && h.begin) { 453 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 454 (u_long)h.seq - 1); 455 /* 456 * Stuff our fragment back at the front of the queue and zap 457 * our half-assembed packet. 458 */ 459 (*frag)->pnext = mp->inbufs; 460 mp->inbufs = *frag; 461 *frag = NULL; 462 mbuf_Free(q); 463 q = NULL; 464 frag = &q; 465 h.end = 0; /* just in case it's a whole packet */ 466 } else 467 do 468 frag = &(*frag)->next; 469 while (*frag != NULL); 470 } while (!h.end); 471 472 if (q) { 473 u_short proto; 474 u_char ch; 475 476 q = mbuf_Read(q, &ch, 1); 477 proto = ch; 478 if (!(proto & 1)) { 479 q = mbuf_Read(q, &ch, 1); 480 proto <<= 8; 481 proto += ch; 482 } 483 if (log_IsKept(LogDEBUG)) 484 log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n", 485 first, (u_long)h.seq, mbuf_Length(q)); 486 hdlc_DecodePacket(mp->bundle, proto, q, &mp->link); 487 } 488 489 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 490 last = NULL; 491 q = mp->inbufs; 492 } else { 493 /* Look for the next fragment */ 494 seq = inc_seq(mp->local_is12bit, seq); 495 last = q; 496 q = q->pnext; 497 } 498 } 499 500 if (m) { 501 /* We still have to find a home for our new fragment */ 502 last = NULL; 503 for (q = mp->inbufs; q; last = q, q = q->pnext) { 504 mp_ReadHeader(mp, q, &h); 505 if (isbefore(mp->local_is12bit, mh.seq, h.seq)) 506 break; 507 } 508 /* Our received fragment fits in here */ 509 if (last) 510 last->pnext = m; 511 else 512 mp->inbufs = m; 513 m->pnext = q; 514 } 515} 516 517static void 518mp_Output(struct mp *mp, struct link *l, struct mbuf *m, u_int32_t begin, 519 u_int32_t end) 520{ 521 struct mbuf *mo; 522 523 /* Stuff an MP header on the front of our packet and send it */ 524 mo = mbuf_Alloc(4, MB_MP); 525 mo->next = m; 526 if (mp->peer_is12bit) { 527 u_int16_t *seq16; 528 529 seq16 = (u_int16_t *)MBUF_CTOP(mo); 530 *seq16 = htons((begin << 15) | (end << 14) | (u_int16_t)mp->out.seq); 531 mo->cnt = 2; 532 } else { 533 u_int32_t *seq32; 534 535 seq32 = (u_int32_t *)MBUF_CTOP(mo); 536 *seq32 = htonl((begin << 31) | (end << 30) | (u_int32_t)mp->out.seq); 537 mo->cnt = 4; 538 } 539 if (log_IsKept(LogDEBUG)) 540 log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n", 541 mp->out.seq, mbuf_Length(mo), l->name); 542 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq); 543 544 if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, PROTO_MP, mo)) 545 hdlc_Output(l, PRI_NORMAL, PROTO_MP, mo); 546} 547 548int 549mp_FillQueues(struct bundle *bundle) 550{ 551 struct mp *mp = &bundle->ncp.mp; 552 struct datalink *dl, *fdl; 553 int total, add, len, thislink, nlinks; 554 u_int32_t begin, end; 555 struct mbuf *m, *mo; 556 557 thislink = nlinks = 0; 558 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) { 559 /* Include non-open links here as mp->out.link will stay more correct */ 560 if (!fdl) { 561 if (thislink == mp->out.link) 562 fdl = dl; 563 else 564 thislink++; 565 } 566 nlinks++; 567 } 568 569 if (!fdl) { 570 fdl = bundle->links; 571 if (!fdl) 572 return 0; 573 thislink = 0; 574 } 575 576 total = 0; 577 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) { 578 if (!dl) { 579 dl = bundle->links; 580 thislink = 0; 581 } 582 583 if (dl->state != DATALINK_OPEN) 584 continue; 585 586 if (dl->physical->out) 587 /* this link has suffered a short write. Let it continue */ 588 continue; 589 590 add = link_QueueLen(&dl->physical->link); 591 total += add; 592 if (add) 593 /* this link has got stuff already queued. Let it continue */ 594 continue; 595 596 if (!link_QueueLen(&mp->link) && !ip_FlushPacket(&mp->link, bundle)) 597 /* Nothing else to send */ 598 break; 599 600 m = link_Dequeue(&mp->link); 601 len = mbuf_Length(m); 602 begin = 1; 603 end = 0; 604 605 while (!end) { 606 if (dl->state == DATALINK_OPEN) { 607 if (len <= dl->mp.weight + LINK_MINWEIGHT) { 608 /* 609 * XXX: Should we remember how much of our `weight' wasn't sent 610 * so that we can compensate next time ? 611 */ 612 mo = m; 613 end = 1; 614 } else { 615 mo = mbuf_Alloc(dl->mp.weight, MB_MP); 616 mo->cnt = dl->mp.weight; 617 len -= mo->cnt; 618 m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt); 619 } 620 mp_Output(mp, &dl->physical->link, mo, begin, end); 621 begin = 0; 622 } 623 624 if (!end) { 625 nlinks--; 626 dl = dl->next; 627 if (!dl) { 628 dl = bundle->links; 629 thislink = 0; 630 } else 631 thislink++; 632 } 633 } 634 } 635 mp->out.link = thislink; /* Start here next time */ 636 637 return total; 638} 639 640int 641mp_SetDatalinkWeight(struct cmdargs const *arg) 642{ 643 int val; 644 645 if (arg->argc != arg->argn+1) 646 return -1; 647 648 val = atoi(arg->argv[arg->argn]); 649 if (val < LINK_MINWEIGHT) { 650 log_Printf(LogWARN, "Link weights must not be less than %d\n", 651 LINK_MINWEIGHT); 652 return 1; 653 } 654 arg->cx->mp.weight = val; 655 return 0; 656} 657 658int 659mp_ShowStatus(struct cmdargs const *arg) 660{ 661 struct mp *mp = &arg->bundle->ncp.mp; 662 663 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 664 if (mp->active) { 665 struct mbuf *m; 666 int bufs = 0; 667 668 prompt_Printf(arg->prompt, "Socket: %s\n", 669 mp->server.socket.sun_path); 670 for (m = mp->inbufs; m; m = m->pnext) 671 bufs++; 672 prompt_Printf(arg->prompt, "Pending frags: %d\n", bufs); 673 } 674 675 prompt_Printf(arg->prompt, "\nMy Side:\n"); 676 if (mp->active) { 677 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 678 prompt_Printf(arg->prompt, " Short Seq: %s\n", 679 mp->local_is12bit ? "on" : "off"); 680 } 681 prompt_Printf(arg->prompt, " Discriminator: %s\n", 682 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 683 mp->cfg.enddisc.len)); 684 685 prompt_Printf(arg->prompt, "\nHis Side:\n"); 686 if (mp->active) { 687 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 688 prompt_Printf(arg->prompt, " Next SEQ: %u\n", mp->out.seq); 689 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 690 prompt_Printf(arg->prompt, " Short Seq: %s\n", 691 mp->peer_is12bit ? "on" : "off"); 692 } 693 prompt_Printf(arg->prompt, " Discriminator: %s\n", 694 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 695 mp->peer.enddisc.len)); 696 697 prompt_Printf(arg->prompt, "\nDefaults:\n"); 698 699 prompt_Printf(arg->prompt, " MRRU: "); 700 if (mp->cfg.mrru) 701 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 702 else 703 prompt_Printf(arg->prompt, "disabled\n"); 704 prompt_Printf(arg->prompt, " Short Seq: %s\n", 705 command_ShowNegval(mp->cfg.shortseq)); 706 707 return 0; 708} 709 710const char * 711mp_Enddisc(u_char c, const char *address, int len) 712{ 713 static char result[100]; /* Used immediately after it's returned */ 714 int f, header; 715 716 switch (c) { 717 case ENDDISC_NULL: 718 sprintf(result, "Null Class"); 719 break; 720 721 case ENDDISC_LOCAL: 722 snprintf(result, sizeof result, "Local Addr: %.*s", len, address); 723 break; 724 725 case ENDDISC_IP: 726 if (len == 4) 727 snprintf(result, sizeof result, "IP %s", 728 inet_ntoa(*(const struct in_addr *)address)); 729 else 730 sprintf(result, "IP[%d] ???", len); 731 break; 732 733 case ENDDISC_MAC: 734 if (len == 6) { 735 const u_char *m = (const u_char *)address; 736 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 737 m[0], m[1], m[2], m[3], m[4], m[5]); 738 } else 739 sprintf(result, "MAC[%d] ???", len); 740 break; 741 742 case ENDDISC_MAGIC: 743 sprintf(result, "Magic: 0x"); 744 header = strlen(result); 745 if (len > sizeof result - header - 1) 746 len = sizeof result - header - 1; 747 for (f = 0; f < len; f++) 748 sprintf(result + header + 2 * f, "%02x", address[f]); 749 break; 750 751 case ENDDISC_PSN: 752 snprintf(result, sizeof result, "PSN: %.*s", len, address); 753 break; 754 755 default: 756 sprintf(result, "%d: ", (int)c); 757 header = strlen(result); 758 if (len > sizeof result - header - 1) 759 len = sizeof result - header - 1; 760 for (f = 0; f < len; f++) 761 sprintf(result + header + 2 * f, "%02x", address[f]); 762 break; 763 } 764 return result; 765} 766 767int 768mp_SetEnddisc(struct cmdargs const *arg) 769{ 770 struct mp *mp = &arg->bundle->ncp.mp; 771 struct in_addr addr; 772 773 if (bundle_Phase(arg->bundle) != PHASE_DEAD) { 774 log_Printf(LogWARN, "set enddisc: Only available at phase DEAD\n"); 775 return 1; 776 } 777 778 if (arg->argc == arg->argn) { 779 mp->cfg.enddisc.class = 0; 780 *mp->cfg.enddisc.address = '\0'; 781 mp->cfg.enddisc.len = 0; 782 } else if (arg->argc > arg->argn) { 783 if (!strcasecmp(arg->argv[arg->argn], "label")) { 784 mp->cfg.enddisc.class = ENDDISC_LOCAL; 785 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 786 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 787 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 788 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 789 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 790 else 791 addr = arg->bundle->ncp.ipcp.my_ip; 792 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 793 mp->cfg.enddisc.class = ENDDISC_IP; 794 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 795 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 796 struct sockaddr_dl hwaddr; 797 int s; 798 799 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 800 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 801 else 802 addr = arg->bundle->ncp.ipcp.my_ip; 803 804 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 805 if (s < 0) { 806 log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno)); 807 return 2; 808 } 809 if (get_ether_addr(s, addr, &hwaddr)) { 810 mp->cfg.enddisc.class = ENDDISC_MAC; 811 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 812 hwaddr.sdl_alen); 813 mp->cfg.enddisc.len = hwaddr.sdl_alen; 814 } else { 815 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 816 inet_ntoa(addr)); 817 close(s); 818 return 4; 819 } 820 close(s); 821 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 822 int f; 823 824 randinit(); 825 for (f = 0; f < 20; f += sizeof(long)) 826 *(long *)(mp->cfg.enddisc.address + f) = random(); 827 mp->cfg.enddisc.class = ENDDISC_MAGIC; 828 mp->cfg.enddisc.len = 20; 829 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 830 if (arg->argc > arg->argn+1) { 831 mp->cfg.enddisc.class = ENDDISC_PSN; 832 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 833 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 834 } else { 835 log_Printf(LogWARN, "PSN endpoint requires additional data\n"); 836 return 5; 837 } 838 } else { 839 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n", 840 arg->argv[arg->argn]); 841 return 6; 842 } 843 } 844 845 return 0; 846} 847 848static int 849mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 850 int *n) 851{ 852 struct mpserver *s = descriptor2mpserver(d); 853 int result; 854 855 result = 0; 856 if (s->send.dl != NULL) { 857 /* We've connect()ed */ 858 if (!link_QueueLen(&s->send.dl->physical->link) && 859 !s->send.dl->physical->out) { 860 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */ 861 result -= datalink_RemoveFromSet(s->send.dl, r, w, e); 862 bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 863 s->send.dl = NULL; 864 s->fd = -1; 865 } else 866 /* Never read from a datalink that's on death row ! */ 867 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL); 868 } else if (r && s->fd >= 0) { 869 if (*n < s->fd + 1) 870 *n = s->fd + 1; 871 FD_SET(s->fd, r); 872 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd); 873 result++; 874 } 875 return result; 876} 877 878static int 879mpserver_IsSet(struct descriptor *d, const fd_set *fdset) 880{ 881 struct mpserver *s = descriptor2mpserver(d); 882 return s->fd >= 0 && FD_ISSET(s->fd, fdset); 883} 884 885static void 886mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 887{ 888 struct mpserver *s = descriptor2mpserver(d); 889 struct sockaddr in; 890 int fd, size; 891 892 size = sizeof in; 893 fd = accept(s->fd, &in, &size); 894 if (fd < 0) { 895 log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno)); 896 return; 897 } 898 899 if (in.sa_family == AF_LOCAL) 900 bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in); 901 else 902 close(fd); 903} 904 905static int 906mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 907{ 908 /* We never want to write here ! */ 909 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n"); 910 return 0; 911} 912 913void 914mpserver_Init(struct mpserver *s) 915{ 916 s->desc.type = MPSERVER_DESCRIPTOR; 917 s->desc.UpdateSet = mpserver_UpdateSet; 918 s->desc.IsSet = mpserver_IsSet; 919 s->desc.Read = mpserver_Read; 920 s->desc.Write = mpserver_Write; 921 s->send.dl = NULL; 922 s->fd = -1; 923 memset(&s->socket, '\0', sizeof s->socket); 924} 925 926int 927mpserver_Open(struct mpserver *s, struct peerid *peer) 928{ 929 int f, l; 930 mode_t mask; 931 932 if (s->fd != -1) { 933 log_Printf(LogALERT, "Internal error ! mpserver already open\n"); 934 mpserver_Close(s); 935 } 936 937 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-", 938 _PATH_VARRUN, peer->authname, peer->enddisc.class); 939 940 for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) { 941 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l, 942 "%02x", *(u_char *)(peer->enddisc.address+f)); 943 l += 2; 944 } 945 946 s->socket.sun_family = AF_LOCAL; 947 s->socket.sun_len = sizeof s->socket; 948 s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0); 949 if (s->fd < 0) { 950 log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno)); 951 return MPSERVER_FAILED; 952 } 953 954 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket, 955 sizeof s->socket); 956 mask = umask(0177); 957 if (ID0bind_un(s->fd, &s->socket) < 0) { 958 if (errno != EADDRINUSE) { 959 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n", 960 s->socket.sun_path, strerror(errno)); 961 umask(mask); 962 close(s->fd); 963 s->fd = -1; 964 return MPSERVER_FAILED; 965 } 966 umask(mask); 967 if (ID0connect_un(s->fd, &s->socket) < 0) { 968 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n", 969 s->socket.sun_path, strerror(errno)); 970 if (errno == ECONNREFUSED) 971 log_Printf(LogPHASE, " Has the previous server died badly ?\n"); 972 close(s->fd); 973 s->fd = -1; 974 return MPSERVER_FAILED; 975 } 976 977 /* Donate our link to the other guy */ 978 return MPSERVER_CONNECTED; 979 } 980 981 /* Listen for other ppp invocations that want to donate links */ 982 if (listen(s->fd, 5) != 0) { 983 log_Printf(LogERROR, "mpserver: Unable to listen to socket" 984 " - BUNDLE overload?\n"); 985 mpserver_Close(s); 986 } 987 988 return MPSERVER_LISTENING; 989} 990 991void 992mpserver_Close(struct mpserver *s) 993{ 994 if (s->send.dl != NULL) { 995 bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 996 s->send.dl = NULL; 997 s->fd = -1; 998 } else if (s->fd >= 0) { 999 close(s->fd); 1000 if (ID0unlink(s->socket.sun_path) == -1) 1001 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path, 1002 strerror(errno)); 1003 memset(&s->socket, '\0', sizeof s->socket); 1004 s->fd = -1; 1005 } 1006} 1007 1008void 1009mp_LinkLost(struct mp *mp, struct datalink *dl) 1010{ 1011 if (mp->seq.min_in == dl->mp.seq) 1012 /* We've lost the link that's holding everything up ! */ 1013 mp_Input(mp, NULL, NULL); 1014} 1015