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