13b0f8d2eSBrian Somers /*- 23b0f8d2eSBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 33b0f8d2eSBrian Somers * All rights reserved. 43b0f8d2eSBrian Somers * 53b0f8d2eSBrian Somers * Redistribution and use in source and binary forms, with or without 63b0f8d2eSBrian Somers * modification, are permitted provided that the following conditions 73b0f8d2eSBrian Somers * are met: 83b0f8d2eSBrian Somers * 1. Redistributions of source code must retain the above copyright 93b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer. 103b0f8d2eSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 113b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer in the 123b0f8d2eSBrian Somers * documentation and/or other materials provided with the distribution. 133b0f8d2eSBrian Somers * 143b0f8d2eSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 153b0f8d2eSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 163b0f8d2eSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 173b0f8d2eSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 183b0f8d2eSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 193b0f8d2eSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 203b0f8d2eSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 213b0f8d2eSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 223b0f8d2eSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 233b0f8d2eSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 243b0f8d2eSBrian Somers * SUCH DAMAGE. 253b0f8d2eSBrian Somers * 2696c9bb21SBrian Somers * $Id: mp.c,v 1.1.2.19 1998/05/01 19:25:27 brian Exp $ 273b0f8d2eSBrian Somers */ 283b0f8d2eSBrian Somers 293b0f8d2eSBrian Somers #include <sys/types.h> 303b0f8d2eSBrian Somers #include <netinet/in.h> 313b0f8d2eSBrian Somers #include <netinet/in_systm.h> 323b0f8d2eSBrian Somers #include <netinet/ip.h> 3349052c95SBrian Somers #include <arpa/inet.h> 3408676022SBrian Somers #include <net/if_dl.h> 3508676022SBrian Somers #include <sys/socket.h> 361fa665f5SBrian Somers #include <sys/un.h> 373b0f8d2eSBrian Somers 3808676022SBrian Somers #include <errno.h> 391fa665f5SBrian Somers #include <paths.h> 403b0f8d2eSBrian Somers #include <stdlib.h> 4149052c95SBrian Somers #include <stdio.h> 423b0f8d2eSBrian Somers #include <string.h> 431fa665f5SBrian Somers #include <sys/stat.h> 443b0f8d2eSBrian Somers #include <termios.h> 4508676022SBrian Somers #include <unistd.h> 463b0f8d2eSBrian Somers 473b0f8d2eSBrian Somers #include "command.h" 483b0f8d2eSBrian Somers #include "mbuf.h" 493b0f8d2eSBrian Somers #include "log.h" 503b0f8d2eSBrian Somers #include "defs.h" 513b0f8d2eSBrian Somers #include "timer.h" 523b0f8d2eSBrian Somers #include "fsm.h" 533b0f8d2eSBrian Somers #include "iplist.h" 543b0f8d2eSBrian Somers #include "throughput.h" 553b0f8d2eSBrian Somers #include "slcompress.h" 563b0f8d2eSBrian Somers #include "ipcp.h" 573b0f8d2eSBrian Somers #include "auth.h" 583b0f8d2eSBrian Somers #include "lcp.h" 593b0f8d2eSBrian Somers #include "lqr.h" 603b0f8d2eSBrian Somers #include "hdlc.h" 613b0f8d2eSBrian Somers #include "async.h" 623b0f8d2eSBrian Somers #include "ccp.h" 633b0f8d2eSBrian Somers #include "link.h" 643b0f8d2eSBrian Somers #include "descriptor.h" 653b0f8d2eSBrian Somers #include "physical.h" 663b0f8d2eSBrian Somers #include "chat.h" 673b0f8d2eSBrian Somers #include "lcpproto.h" 683b0f8d2eSBrian Somers #include "filter.h" 693b0f8d2eSBrian Somers #include "mp.h" 703b0f8d2eSBrian Somers #include "chap.h" 713b0f8d2eSBrian Somers #include "datalink.h" 723b0f8d2eSBrian Somers #include "bundle.h" 733b0f8d2eSBrian Somers #include "ip.h" 7449052c95SBrian Somers #include "prompt.h" 7508676022SBrian Somers #include "id.h" 7608676022SBrian Somers #include "arp.h" 773b0f8d2eSBrian Somers 78643f4904SBrian Somers void 79643f4904SBrian Somers peerid_Init(struct peerid *peer) 80643f4904SBrian Somers { 81643f4904SBrian Somers peer->enddisc.class = 0; 82643f4904SBrian Somers *peer->enddisc.address = '\0'; 83643f4904SBrian Somers peer->enddisc.len = 0; 84643f4904SBrian Somers *peer->authname = '\0'; 85643f4904SBrian Somers } 86643f4904SBrian Somers 87643f4904SBrian Somers int 88643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2) 89643f4904SBrian Somers { 90643f4904SBrian Somers return !strcmp(p1->authname, p2->authname) && 91643f4904SBrian Somers p1->enddisc.class == p2->enddisc.class && 92643f4904SBrian Somers p1->enddisc.len == p2->enddisc.len && 93643f4904SBrian Somers !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 94643f4904SBrian Somers } 95643f4904SBrian Somers 963b0f8d2eSBrian Somers static u_int32_t 973b0f8d2eSBrian Somers inc_seq(struct mp *mp, u_int32_t seq) 983b0f8d2eSBrian Somers { 993b0f8d2eSBrian Somers seq++; 10049052c95SBrian Somers if (mp->peer_is12bit) { 1013b0f8d2eSBrian Somers if (seq & 0xfffff000) 1023b0f8d2eSBrian Somers seq = 0; 1033b0f8d2eSBrian Somers } else if (seq & 0xff000000) 1043b0f8d2eSBrian Somers seq = 0; 1053b0f8d2eSBrian Somers return seq; 1063b0f8d2eSBrian Somers } 1073b0f8d2eSBrian Somers 1083b0f8d2eSBrian Somers static int 1093b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 1103b0f8d2eSBrian Somers { 11149052c95SBrian Somers if (mp->local_is12bit) { 112ce828a6eSBrian Somers header->seq = ntohs(*(u_int16_t *)MBUF_CTOP(m)); 1133b0f8d2eSBrian Somers if (header->seq & 0x3000) { 114dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1153b0f8d2eSBrian Somers return 0; 1163b0f8d2eSBrian Somers } 117ce828a6eSBrian Somers header->begin = header->seq & 0x8000 ? 1 : 0; 118ce828a6eSBrian Somers header->end = header->seq & 0x4000 ? 1 : 0; 1193b0f8d2eSBrian Somers header->seq &= 0x0fff; 1203b0f8d2eSBrian Somers return 2; 1213b0f8d2eSBrian Somers } else { 122ce828a6eSBrian Somers header->seq = ntohl(*(u_int32_t *)MBUF_CTOP(m)); 1233b0f8d2eSBrian Somers if (header->seq & 0x3f000000) { 124dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1253b0f8d2eSBrian Somers return 0; 1263b0f8d2eSBrian Somers } 127ce828a6eSBrian Somers header->begin = header->seq & 0x80000000 ? 1 : 0; 128ce828a6eSBrian Somers header->end = header->seq & 0x40000000 ? 1 : 0; 1293b0f8d2eSBrian Somers header->seq &= 0x00ffffff; 1303b0f8d2eSBrian Somers return 4; 1313b0f8d2eSBrian Somers } 1323b0f8d2eSBrian Somers } 1333b0f8d2eSBrian Somers 1343b0f8d2eSBrian Somers static void 1353b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp) 1363b0f8d2eSBrian Somers { 13749052c95SBrian Somers /* The given FSM (ccp) is about to start up ! */ 1383b0f8d2eSBrian Somers } 1393b0f8d2eSBrian Somers 1403b0f8d2eSBrian Somers static void 1413b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp) 1423b0f8d2eSBrian Somers { 14349052c95SBrian Somers /* The given fsm (ccp) is now up */ 1443b0f8d2eSBrian Somers } 1453b0f8d2eSBrian Somers 1463b0f8d2eSBrian Somers static void 1473b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp) 1483b0f8d2eSBrian Somers { 14949052c95SBrian Somers /* The given FSM (ccp) has been told to come down */ 1503b0f8d2eSBrian Somers } 1513b0f8d2eSBrian Somers 1523b0f8d2eSBrian Somers static void 1533b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp) 1543b0f8d2eSBrian Somers { 15549052c95SBrian Somers /* The given fsm (ccp) is now down */ 1563b0f8d2eSBrian Somers } 1573b0f8d2eSBrian Somers 1583b0f8d2eSBrian Somers void 1593b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle) 1603b0f8d2eSBrian Somers { 16149052c95SBrian Somers mp->peer_is12bit = mp->local_is12bit = 0; 16249052c95SBrian Somers mp->peer_mrru = mp->local_mrru = 0; 163643f4904SBrian Somers 164643f4904SBrian Somers peerid_Init(&mp->peer); 165643f4904SBrian Somers 1663b0f8d2eSBrian Somers mp->seq.out = 0; 1673b0f8d2eSBrian Somers mp->seq.min_in = 0; 1683b0f8d2eSBrian Somers mp->seq.next_in = 0; 1693b0f8d2eSBrian Somers mp->inbufs = NULL; 1703b0f8d2eSBrian Somers mp->bundle = bundle; 1713b0f8d2eSBrian Somers 1723b0f8d2eSBrian Somers mp->link.type = MP_LINK; 1733b0f8d2eSBrian Somers mp->link.name = "mp"; 1743b0f8d2eSBrian Somers mp->link.len = sizeof *mp; 175643f4904SBrian Somers 1763b0f8d2eSBrian Somers throughput_init(&mp->link.throughput); 1773b0f8d2eSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 1783b0f8d2eSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 1793b0f8d2eSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 1803b0f8d2eSBrian Somers 1813b0f8d2eSBrian Somers mp->fsmp.LayerStart = mp_LayerStart; 1823b0f8d2eSBrian Somers mp->fsmp.LayerUp = mp_LayerUp; 1833b0f8d2eSBrian Somers mp->fsmp.LayerDown = mp_LayerDown; 1843b0f8d2eSBrian Somers mp->fsmp.LayerFinish = mp_LayerFinish; 1853b0f8d2eSBrian Somers mp->fsmp.object = mp; 1863b0f8d2eSBrian Somers 1871fa665f5SBrian Somers mpserver_Init(&mp->server); 1881fa665f5SBrian Somers 18949052c95SBrian Somers mp->cfg.mrru = 0; 19049052c95SBrian Somers mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 19149052c95SBrian Somers mp->cfg.enddisc.class = 0; 19249052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 19349052c95SBrian Somers mp->cfg.enddisc.len = 0; 19449052c95SBrian Somers 1953b0f8d2eSBrian Somers lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 1963b0f8d2eSBrian Somers ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 19749052c95SBrian Somers } 19849052c95SBrian Somers 19949052c95SBrian Somers int 2001fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl) 20149052c95SBrian Somers { 2021fa665f5SBrian Somers struct lcp *lcp = &dl->physical->link.lcp; 2031fa665f5SBrian Somers 20449052c95SBrian Somers if (mp->active) { 20549052c95SBrian Somers /* We're adding a link - do a last validation on our parameters */ 2061fa665f5SBrian Somers if (!peerid_Equal(&dl->peer, &mp->peer)) { 207dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name); 2081fa665f5SBrian Somers return MP_FAILED; 209643f4904SBrian Somers } 2101fa665f5SBrian Somers if (mp->local_mrru != lcp->want_mrru || 2111fa665f5SBrian Somers mp->peer_mrru != lcp->his_mrru || 2121fa665f5SBrian Somers mp->local_is12bit != lcp->want_shortseq || 2131fa665f5SBrian Somers mp->peer_is12bit != lcp->his_shortseq) { 214dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n", 2151fa665f5SBrian Somers dl->name); 2161fa665f5SBrian Somers return MP_FAILED; 21749052c95SBrian Somers } 2181fa665f5SBrian Somers return MP_ADDED; 21949052c95SBrian Somers } else { 22049052c95SBrian Somers /* First link in multilink mode */ 22149052c95SBrian Somers 2221fa665f5SBrian Somers mp->local_mrru = lcp->want_mrru; 2231fa665f5SBrian Somers mp->peer_mrru = lcp->his_mrru; 2241fa665f5SBrian Somers mp->local_is12bit = lcp->want_shortseq; 2251fa665f5SBrian Somers mp->peer_is12bit = lcp->his_shortseq; 2261fa665f5SBrian Somers mp->peer = dl->peer; 22749052c95SBrian Somers 228673903ecSBrian Somers throughput_init(&mp->link.throughput); 229673903ecSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 230673903ecSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 231673903ecSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 232673903ecSBrian Somers 233673903ecSBrian Somers mp->seq.out = 0; 234673903ecSBrian Somers mp->seq.min_in = 0; 235673903ecSBrian Somers mp->seq.next_in = 0; 236673903ecSBrian Somers 2371fa665f5SBrian Somers /* 2381fa665f5SBrian Somers * Now we create our server socket. 2391fa665f5SBrian Somers * If it already exists, join it. Otherwise, create and own it 2401fa665f5SBrian Somers */ 24196c9bb21SBrian Somers switch (mpserver_Open(&mp->server, &mp->peer)) { 24296c9bb21SBrian Somers case MPSERVER_CONNECTED: 243dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Transfer link on %s\n", 2446f384573SBrian Somers mp->server.socket.sun_path); 24596c9bb21SBrian Somers mp->server.send.dl = dl; /* Defer 'till it's safe to send */ 2461fa665f5SBrian Somers return MP_LINKSENT; 24796c9bb21SBrian Somers case MPSERVER_FAILED: 2481fa665f5SBrian Somers return MP_FAILED; 24996c9bb21SBrian Somers case MPSERVER_LISTENING: 250dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path); 251dd7e2610SBrian Somers log_Printf(LogPHASE, " First link: %s\n", dl->name); 2521fa665f5SBrian Somers 25349052c95SBrian Somers /* Re-point our IPCP layer at our MP link */ 254ce828a6eSBrian Somers ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link); 2553b0f8d2eSBrian Somers 2563b0f8d2eSBrian Somers /* Our lcp's already up 'cos of the NULL parent */ 257dd7e2610SBrian Somers fsm_Up(&mp->link.ccp.fsm); 258dd7e2610SBrian Somers fsm_Open(&mp->link.ccp.fsm); 2593b0f8d2eSBrian Somers 2603b0f8d2eSBrian Somers mp->active = 1; 26196c9bb21SBrian Somers break; 26249052c95SBrian Somers } 2631fa665f5SBrian Somers } 2643b0f8d2eSBrian Somers 2651fa665f5SBrian Somers return MP_UP; 2663b0f8d2eSBrian Somers } 2673b0f8d2eSBrian Somers 2683b0f8d2eSBrian Somers void 269673903ecSBrian Somers mp_Down(struct mp *mp) 270673903ecSBrian Somers { 271673903ecSBrian Somers if (mp->active) { 272673903ecSBrian Somers struct mbuf *next; 273673903ecSBrian Somers 2741fa665f5SBrian Somers /* Don't want any more of these */ 2751fa665f5SBrian Somers mpserver_Close(&mp->server); 2761fa665f5SBrian Somers 2771fa665f5SBrian Somers /* CCP goes down with a bang */ 278dd7e2610SBrian Somers fsm_Down(&mp->link.ccp.fsm); 279dd7e2610SBrian Somers fsm_Close(&mp->link.ccp.fsm); 280673903ecSBrian Somers 281673903ecSBrian Somers /* Received fragments go in the bit-bucket */ 282673903ecSBrian Somers while (mp->inbufs) { 283673903ecSBrian Somers next = mp->inbufs->pnext; 284dd7e2610SBrian Somers mbuf_Free(mp->inbufs); 285673903ecSBrian Somers mp->inbufs = next; 286673903ecSBrian Somers } 287673903ecSBrian Somers 288643f4904SBrian Somers peerid_Init(&mp->peer); 289673903ecSBrian Somers mp->active = 0; 290673903ecSBrian Somers } 291673903ecSBrian Somers } 292673903ecSBrian Somers 293673903ecSBrian Somers void 2943b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink) 2953b0f8d2eSBrian Somers { 2963b0f8d2eSBrian Somers mplink->seq = 0; 2973b0f8d2eSBrian Somers mplink->weight = 1500; 2983b0f8d2eSBrian Somers } 2993b0f8d2eSBrian Somers 3003b0f8d2eSBrian Somers void 3013b0f8d2eSBrian Somers mp_Input(struct mp *mp, struct mbuf *m, struct physical *p) 3023b0f8d2eSBrian Somers { 3033b0f8d2eSBrian Somers struct mp_header mh, h; 3043b0f8d2eSBrian Somers struct mbuf *q, *last; 3053b0f8d2eSBrian Somers int32_t seq; 3063b0f8d2eSBrian Somers 307cdbbb6b5SBrian Somers if (mp_ReadHeader(mp, m, &mh) == 0) { 308dd7e2610SBrian Somers mbuf_Free(m); 3093b0f8d2eSBrian Somers return; 3103b0f8d2eSBrian Somers } 3113b0f8d2eSBrian Somers 3123b0f8d2eSBrian Somers seq = p->dl->mp.seq; 3133b0f8d2eSBrian Somers p->dl->mp.seq = mh.seq; 3143b0f8d2eSBrian Somers if (mp->seq.min_in == seq) { 3153b0f8d2eSBrian Somers /* 3163b0f8d2eSBrian Somers * We've received new data on the link that has our min (oldest) seq. 3173b0f8d2eSBrian Somers * Figure out which link now has the smallest (oldest) seq. 3183b0f8d2eSBrian Somers */ 3193b0f8d2eSBrian Somers struct datalink *dl; 3203b0f8d2eSBrian Somers 3213b0f8d2eSBrian Somers mp->seq.min_in = p->dl->mp.seq; 3223b0f8d2eSBrian Somers for (dl = mp->bundle->links; dl; dl = dl->next) 3233b0f8d2eSBrian Somers if (mp->seq.min_in > dl->mp.seq) 3243b0f8d2eSBrian Somers mp->seq.min_in = dl->mp.seq; 3253b0f8d2eSBrian Somers } 3263b0f8d2eSBrian Somers 3273b0f8d2eSBrian Somers /* 3283b0f8d2eSBrian Somers * Now process as many of our fragments as we can, adding our new 3293b0f8d2eSBrian Somers * fragment in as we go, and ordering with the oldest at the top of 3303b0f8d2eSBrian Somers * the queue. 3313b0f8d2eSBrian Somers */ 3323b0f8d2eSBrian Somers 3333b0f8d2eSBrian Somers if (!mp->inbufs) { 3343b0f8d2eSBrian Somers mp->inbufs = m; 3353b0f8d2eSBrian Somers m = NULL; 3363b0f8d2eSBrian Somers } 3373b0f8d2eSBrian Somers 3383b0f8d2eSBrian Somers last = NULL; 3393b0f8d2eSBrian Somers seq = mp->seq.next_in; 3403b0f8d2eSBrian Somers q = mp->inbufs; 3413b0f8d2eSBrian Somers while (q) { 3423b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 3433b0f8d2eSBrian Somers if (m && h.seq > mh.seq) { 3443b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 3453b0f8d2eSBrian Somers if (last) 3463b0f8d2eSBrian Somers last->pnext = m; 3473b0f8d2eSBrian Somers else 3483b0f8d2eSBrian Somers mp->inbufs = m; 3493b0f8d2eSBrian Somers m->pnext = q; 3503b0f8d2eSBrian Somers q = m; 3513b0f8d2eSBrian Somers h = mh; 3523b0f8d2eSBrian Somers m = NULL; 3533b0f8d2eSBrian Somers } 3543b0f8d2eSBrian Somers 3553b0f8d2eSBrian Somers if (h.seq != seq) { 3563b0f8d2eSBrian Somers /* we're missing something :-( */ 3573b0f8d2eSBrian Somers if (mp->seq.min_in > seq) { 3583b0f8d2eSBrian Somers /* we're never gonna get it */ 3593b0f8d2eSBrian Somers struct mbuf *next; 3603b0f8d2eSBrian Somers 3613b0f8d2eSBrian Somers /* Zap all older fragments */ 3623b0f8d2eSBrian Somers while (mp->inbufs != q) { 363dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag\n"); 3643b0f8d2eSBrian Somers next = mp->inbufs->pnext; 365dd7e2610SBrian Somers mbuf_Free(mp->inbufs); 3663b0f8d2eSBrian Somers mp->inbufs = next; 3673b0f8d2eSBrian Somers } 3683b0f8d2eSBrian Somers 3693b0f8d2eSBrian Somers /* 3703b0f8d2eSBrian Somers * Zap everything until the next `end' fragment OR just before 3713b0f8d2eSBrian Somers * the next `begin' fragment OR 'till seq.min_in - whichever 3723b0f8d2eSBrian Somers * comes first. 3733b0f8d2eSBrian Somers */ 3743b0f8d2eSBrian Somers do { 3753b0f8d2eSBrian Somers mp_ReadHeader(mp, mp->inbufs, &h); 3763b0f8d2eSBrian Somers if (h.begin) { 377ce828a6eSBrian Somers /* We might be able to process this ! */ 3783b0f8d2eSBrian Somers h.seq--; /* We're gonna look for fragment with h.seq+1 */ 3793b0f8d2eSBrian Somers break; 3803b0f8d2eSBrian Somers } 3813b0f8d2eSBrian Somers next = mp->inbufs->pnext; 382dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag %u\n", h.seq); 383dd7e2610SBrian Somers mbuf_Free(mp->inbufs); 3843b0f8d2eSBrian Somers mp->inbufs = next; 385ce828a6eSBrian Somers } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end)); 3863b0f8d2eSBrian Somers 3873b0f8d2eSBrian Somers /* 3883b0f8d2eSBrian Somers * Continue processing things from here. 3893b0f8d2eSBrian Somers * This deals with the possibility that we received a fragment 3903b0f8d2eSBrian Somers * on the slowest link that invalidates some of our data (because 3913b0f8d2eSBrian Somers * of the hole at `q'), but where there are subsequent `whole' 3923b0f8d2eSBrian Somers * packets that have already been received. 3933b0f8d2eSBrian Somers */ 3943b0f8d2eSBrian Somers 3953b0f8d2eSBrian Somers mp->seq.next_in = seq = h.seq + 1; 3963b0f8d2eSBrian Somers last = NULL; 3973b0f8d2eSBrian Somers q = mp->inbufs; 3983b0f8d2eSBrian Somers } else 3993b0f8d2eSBrian Somers /* we may still receive the missing fragment */ 4003b0f8d2eSBrian Somers break; 4013b0f8d2eSBrian Somers } else if (h.end) { 4023b0f8d2eSBrian Somers /* We've got something, reassemble */ 4033b0f8d2eSBrian Somers struct mbuf **frag = &q; 4043b0f8d2eSBrian Somers int len; 405ce828a6eSBrian Somers u_long first = -1; 4063b0f8d2eSBrian Somers 4073b0f8d2eSBrian Somers do { 4083b0f8d2eSBrian Somers *frag = mp->inbufs; 4093b0f8d2eSBrian Somers mp->inbufs = mp->inbufs->pnext; 4103b0f8d2eSBrian Somers len = mp_ReadHeader(mp, *frag, &h); 411ce828a6eSBrian Somers if (first == -1) 412ce828a6eSBrian Somers first = h.seq; 4133b0f8d2eSBrian Somers (*frag)->offset += len; 4143b0f8d2eSBrian Somers (*frag)->cnt -= len; 4153b0f8d2eSBrian Somers (*frag)->pnext = NULL; 4163b0f8d2eSBrian Somers if (frag == &q && !h.begin) { 417dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 4183b0f8d2eSBrian Somers (u_long)h.seq); 419dd7e2610SBrian Somers mbuf_Free(q); 4203b0f8d2eSBrian Somers q = NULL; 4213b0f8d2eSBrian Somers } else if (frag != &q && h.begin) { 422dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 4233b0f8d2eSBrian Somers (u_long)h.seq - 1); 4243b0f8d2eSBrian Somers /* 4253b0f8d2eSBrian Somers * Stuff our fragment back at the front of the queue and zap 4263b0f8d2eSBrian Somers * our half-assembed packet. 4273b0f8d2eSBrian Somers */ 4283b0f8d2eSBrian Somers (*frag)->pnext = mp->inbufs; 4293b0f8d2eSBrian Somers mp->inbufs = *frag; 4303b0f8d2eSBrian Somers *frag = NULL; 431dd7e2610SBrian Somers mbuf_Free(q); 4323b0f8d2eSBrian Somers q = NULL; 4333b0f8d2eSBrian Somers frag = &q; 4343b0f8d2eSBrian Somers h.end = 0; /* just in case it's a whole packet */ 4353b0f8d2eSBrian Somers } else 4363b0f8d2eSBrian Somers do 4373b0f8d2eSBrian Somers frag = &(*frag)->next; 438ce828a6eSBrian Somers while (*frag != NULL); 4393b0f8d2eSBrian Somers } while (!h.end); 4403b0f8d2eSBrian Somers 4413b0f8d2eSBrian Somers if (q) { 442673903ecSBrian Somers u_short proto; 443673903ecSBrian Somers u_char ch; 444673903ecSBrian Somers 445dd7e2610SBrian Somers q = mbuf_Read(q, &ch, 1); 446673903ecSBrian Somers proto = ch; 447673903ecSBrian Somers if (!(proto & 1)) { 448dd7e2610SBrian Somers q = mbuf_Read(q, &ch, 1); 449673903ecSBrian Somers proto <<= 8; 4503b0f8d2eSBrian Somers proto += ch; 451673903ecSBrian Somers } 452dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 453dd7e2610SBrian Somers log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n", 454dd7e2610SBrian Somers first, (u_long)h.seq, mbuf_Length(q)); 4553b0f8d2eSBrian Somers hdlc_DecodePacket(mp->bundle, proto, q, &mp->link); 4563b0f8d2eSBrian Somers } 4573b0f8d2eSBrian Somers 4583b0f8d2eSBrian Somers mp->seq.next_in = seq = h.seq + 1; 4593b0f8d2eSBrian Somers last = NULL; 4603b0f8d2eSBrian Somers q = mp->inbufs; 4613b0f8d2eSBrian Somers } else { 4623b0f8d2eSBrian Somers /* Look for the next fragment */ 4633b0f8d2eSBrian Somers seq++; 4643b0f8d2eSBrian Somers last = q; 4653b0f8d2eSBrian Somers q = q->pnext; 4663b0f8d2eSBrian Somers } 4673b0f8d2eSBrian Somers } 4683b0f8d2eSBrian Somers 4693b0f8d2eSBrian Somers if (m) { 4703b0f8d2eSBrian Somers /* We still have to find a home for our new fragment */ 4713b0f8d2eSBrian Somers last = NULL; 4723b0f8d2eSBrian Somers for (q = mp->inbufs; q; last = q, q = q->pnext) { 4733b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 4743b0f8d2eSBrian Somers if (h.seq > mh.seq) { 4753b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 4763b0f8d2eSBrian Somers if (last) 4773b0f8d2eSBrian Somers last->pnext = m; 4783b0f8d2eSBrian Somers else 4793b0f8d2eSBrian Somers mp->inbufs = m; 4803b0f8d2eSBrian Somers m->pnext = q; 4813b0f8d2eSBrian Somers break; 4823b0f8d2eSBrian Somers } 4833b0f8d2eSBrian Somers } 4843b0f8d2eSBrian Somers } 4853b0f8d2eSBrian Somers } 4863b0f8d2eSBrian Somers 4873b0f8d2eSBrian Somers static void 4883b0f8d2eSBrian Somers mp_Output(struct mp *mp, struct link *l, struct mbuf *m, int begin, int end) 4893b0f8d2eSBrian Somers { 4903b0f8d2eSBrian Somers struct mbuf *mo; 4913b0f8d2eSBrian Somers 492ce828a6eSBrian Somers /* Stuff an MP header on the front of our packet and send it */ 493dd7e2610SBrian Somers mo = mbuf_Alloc(4, MB_MP); 4943b0f8d2eSBrian Somers mo->next = m; 49549052c95SBrian Somers if (mp->peer_is12bit) { 496ce828a6eSBrian Somers u_int16_t *seq16; 497ce828a6eSBrian Somers 498ce828a6eSBrian Somers seq16 = (u_int16_t *)MBUF_CTOP(mo); 499ce828a6eSBrian Somers *seq16 = htons((begin << 15) | (end << 14) | (u_int16_t)mp->seq.out); 5003b0f8d2eSBrian Somers mo->cnt = 2; 5013b0f8d2eSBrian Somers } else { 502ce828a6eSBrian Somers u_int32_t *seq32; 503ce828a6eSBrian Somers 504ce828a6eSBrian Somers seq32 = (u_int32_t *)MBUF_CTOP(mo); 505ce828a6eSBrian Somers *seq32 = htonl((begin << 31) | (end << 30) | (u_int32_t)mp->seq.out); 5063b0f8d2eSBrian Somers mo->cnt = 4; 5073b0f8d2eSBrian Somers } 508dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 509dd7e2610SBrian Somers log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on %s\n", 510dd7e2610SBrian Somers mp->seq.out, mbuf_Length(mo), l->name); 5113b0f8d2eSBrian Somers mp->seq.out = inc_seq(mp, mp->seq.out); 5123b0f8d2eSBrian Somers 513dd7e2610SBrian Somers hdlc_Output(l, PRI_NORMAL, PROTO_MP, mo); 5143b0f8d2eSBrian Somers } 5153b0f8d2eSBrian Somers 5163b0f8d2eSBrian Somers int 5173b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle) 5183b0f8d2eSBrian Somers { 5193b0f8d2eSBrian Somers struct mp *mp = &bundle->ncp.mp; 5203b0f8d2eSBrian Somers struct datalink *dl; 5213b0f8d2eSBrian Somers int total, add, len, begin, end, looped; 5223b0f8d2eSBrian Somers struct mbuf *m, *mo; 5233b0f8d2eSBrian Somers 5243b0f8d2eSBrian Somers /* 5253b0f8d2eSBrian Somers * XXX: This routine is fairly simplistic. It should re-order the 5263b0f8d2eSBrian Somers * links based on the amount of data less than the links weight 5273b0f8d2eSBrian Somers * that was queued. That way we'd ``prefer'' the least used 5283b0f8d2eSBrian Somers * links the next time 'round. 5293b0f8d2eSBrian Somers */ 5303b0f8d2eSBrian Somers 5313b0f8d2eSBrian Somers total = 0; 5323b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 5336f384573SBrian Somers if (dl->state != DATALINK_OPEN) 5346f384573SBrian Somers continue; 5353b0f8d2eSBrian Somers if (dl->physical->out) 5363b0f8d2eSBrian Somers /* this link has suffered a short write. Let it continue */ 5373b0f8d2eSBrian Somers continue; 5383b0f8d2eSBrian Somers add = link_QueueLen(&dl->physical->link); 5393b0f8d2eSBrian Somers total += add; 5403b0f8d2eSBrian Somers if (add) 5413b0f8d2eSBrian Somers /* this link has got stuff already queued. Let it continue */ 5423b0f8d2eSBrian Somers continue; 543dd7e2610SBrian Somers if (!link_QueueLen(&mp->link) && !ip_FlushPacket(&mp->link, bundle)) 5443b0f8d2eSBrian Somers /* Nothing else to send */ 5453b0f8d2eSBrian Somers break; 5463b0f8d2eSBrian Somers 5473b0f8d2eSBrian Somers m = link_Dequeue(&mp->link); 548dd7e2610SBrian Somers len = mbuf_Length(m); 5493b0f8d2eSBrian Somers add += len; 5503b0f8d2eSBrian Somers begin = 1; 5513b0f8d2eSBrian Somers end = 0; 5523b0f8d2eSBrian Somers looped = 0; 5533b0f8d2eSBrian Somers 5543b0f8d2eSBrian Somers for (; !end; dl = dl->next) { 5553b0f8d2eSBrian Somers if (dl == NULL) { 5563b0f8d2eSBrian Somers /* Keep going 'till we get rid of the whole of `m' */ 5573b0f8d2eSBrian Somers looped = 1; 5583b0f8d2eSBrian Somers dl = bundle->links; 5593b0f8d2eSBrian Somers } 5606f384573SBrian Somers if (dl->state != DATALINK_OPEN) 5616f384573SBrian Somers continue; 5623b0f8d2eSBrian Somers if (len <= dl->mp.weight + LINK_MINWEIGHT) { 5633b0f8d2eSBrian Somers mo = m; 5643b0f8d2eSBrian Somers end = 1; 5653b0f8d2eSBrian Somers } else { 566dd7e2610SBrian Somers mo = mbuf_Alloc(dl->mp.weight, MB_MP); 5673b0f8d2eSBrian Somers mo->cnt = dl->mp.weight; 5683b0f8d2eSBrian Somers len -= mo->cnt; 569dd7e2610SBrian Somers m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt); 5703b0f8d2eSBrian Somers } 5713b0f8d2eSBrian Somers mp_Output(mp, &dl->physical->link, mo, begin, end); 5723b0f8d2eSBrian Somers begin = 0; 5733b0f8d2eSBrian Somers } 574ce828a6eSBrian Somers if (!dl || looped) 5753b0f8d2eSBrian Somers break; 5763b0f8d2eSBrian Somers } 5773b0f8d2eSBrian Somers 5783b0f8d2eSBrian Somers return total; 5793b0f8d2eSBrian Somers } 5803b0f8d2eSBrian Somers 5813b0f8d2eSBrian Somers int 5823b0f8d2eSBrian Somers mp_SetDatalinkWeight(struct cmdargs const *arg) 5833b0f8d2eSBrian Somers { 5843b0f8d2eSBrian Somers int val; 5853b0f8d2eSBrian Somers 58625092092SBrian Somers if (arg->argc != arg->argn+1) 5873b0f8d2eSBrian Somers return -1; 5883b0f8d2eSBrian Somers 58925092092SBrian Somers val = atoi(arg->argv[arg->argn]); 5903b0f8d2eSBrian Somers if (val < LINK_MINWEIGHT) { 591dd7e2610SBrian Somers log_Printf(LogWARN, "Link weights must not be less than %d\n", 5923b0f8d2eSBrian Somers LINK_MINWEIGHT); 5933b0f8d2eSBrian Somers return 1; 5943b0f8d2eSBrian Somers } 5953b0f8d2eSBrian Somers arg->cx->mp.weight = val; 5963b0f8d2eSBrian Somers return 0; 5973b0f8d2eSBrian Somers } 59849052c95SBrian Somers 59949052c95SBrian Somers int 60049052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg) 60149052c95SBrian Somers { 60249052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 60349052c95SBrian Somers 60449052c95SBrian Somers prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 6051fa665f5SBrian Somers if (mp->active) 6061fa665f5SBrian Somers prompt_Printf(arg->prompt, "Socket: %s\n", 6076f384573SBrian Somers mp->server.socket.sun_path); 60849052c95SBrian Somers 60949052c95SBrian Somers prompt_Printf(arg->prompt, "\nMy Side:\n"); 61049052c95SBrian Somers if (mp->active) { 61149052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 61249052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 61349052c95SBrian Somers mp->local_is12bit ? "on" : "off"); 61449052c95SBrian Somers } 615643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 61649052c95SBrian Somers mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 61749052c95SBrian Somers mp->cfg.enddisc.len)); 61849052c95SBrian Somers 61949052c95SBrian Somers prompt_Printf(arg->prompt, "\nHis Side:\n"); 62049052c95SBrian Somers if (mp->active) { 621643f4904SBrian Somers prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 62249052c95SBrian Somers prompt_Printf(arg->prompt, " Next SEQ: %u\n", mp->seq.out); 62349052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 62449052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 62549052c95SBrian Somers mp->peer_is12bit ? "on" : "off"); 62649052c95SBrian Somers } 627643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 628643f4904SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 629643f4904SBrian Somers mp->peer.enddisc.len)); 63049052c95SBrian Somers 63149052c95SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 63249052c95SBrian Somers 63349052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: "); 63449052c95SBrian Somers if (mp->cfg.mrru) 63549052c95SBrian Somers prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 63649052c95SBrian Somers else 63749052c95SBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 63849052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 63949052c95SBrian Somers command_ShowNegval(mp->cfg.shortseq)); 64049052c95SBrian Somers 64149052c95SBrian Somers return 0; 64249052c95SBrian Somers } 64349052c95SBrian Somers 64449052c95SBrian Somers const char * 64549052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len) 64649052c95SBrian Somers { 64749052c95SBrian Somers static char result[100]; 64849052c95SBrian Somers int f, header; 64949052c95SBrian Somers 65049052c95SBrian Somers switch (c) { 65108676022SBrian Somers case ENDDISC_NULL: 65249052c95SBrian Somers sprintf(result, "Null Class"); 65349052c95SBrian Somers break; 65449052c95SBrian Somers 65508676022SBrian Somers case ENDDISC_LOCAL: 65649052c95SBrian Somers snprintf(result, sizeof result, "Local Addr: %.*s", len, address); 65749052c95SBrian Somers break; 65849052c95SBrian Somers 65908676022SBrian Somers case ENDDISC_IP: 66049052c95SBrian Somers if (len == 4) 66149052c95SBrian Somers snprintf(result, sizeof result, "IP %s", 66249052c95SBrian Somers inet_ntoa(*(const struct in_addr *)address)); 66349052c95SBrian Somers else 66449052c95SBrian Somers sprintf(result, "IP[%d] ???", len); 66549052c95SBrian Somers break; 66649052c95SBrian Somers 66708676022SBrian Somers case ENDDISC_MAC: 66849052c95SBrian Somers if (len == 6) { 66949052c95SBrian Somers const u_char *m = (const u_char *)address; 67049052c95SBrian Somers snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 67149052c95SBrian Somers m[0], m[1], m[2], m[3], m[4], m[5]); 67249052c95SBrian Somers } else 67349052c95SBrian Somers sprintf(result, "MAC[%d] ???", len); 67449052c95SBrian Somers break; 67549052c95SBrian Somers 67608676022SBrian Somers case ENDDISC_MAGIC: 67749052c95SBrian Somers sprintf(result, "Magic: 0x"); 67849052c95SBrian Somers header = strlen(result); 67949052c95SBrian Somers if (len > sizeof result - header - 1) 68049052c95SBrian Somers len = sizeof result - header - 1; 68149052c95SBrian Somers for (f = 0; f < len; f++) 68249052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 68349052c95SBrian Somers break; 68449052c95SBrian Somers 68508676022SBrian Somers case ENDDISC_PSN: 68649052c95SBrian Somers snprintf(result, sizeof result, "PSN: %.*s", len, address); 68749052c95SBrian Somers break; 68849052c95SBrian Somers 68949052c95SBrian Somers default: 69049052c95SBrian Somers sprintf(result, "%d: ", (int)c); 69149052c95SBrian Somers header = strlen(result); 69249052c95SBrian Somers if (len > sizeof result - header - 1) 69349052c95SBrian Somers len = sizeof result - header - 1; 69449052c95SBrian Somers for (f = 0; f < len; f++) 69549052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 69649052c95SBrian Somers break; 69749052c95SBrian Somers } 69849052c95SBrian Somers return result; 69949052c95SBrian Somers } 70049052c95SBrian Somers 70149052c95SBrian Somers int 70249052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg) 70349052c95SBrian Somers { 70449052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 705d47dceb8SBrian Somers struct in_addr addr; 70649052c95SBrian Somers 70749052c95SBrian Somers if (bundle_Phase(arg->bundle) != PHASE_DEAD) { 708dd7e2610SBrian Somers log_Printf(LogWARN, "set enddisc: Only available at phase DEAD\n"); 70949052c95SBrian Somers return 1; 71049052c95SBrian Somers } 71149052c95SBrian Somers 71249052c95SBrian Somers if (arg->argc == arg->argn) { 71349052c95SBrian Somers mp->cfg.enddisc.class = 0; 71449052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 71549052c95SBrian Somers mp->cfg.enddisc.len = 0; 716e43ebac1SBrian Somers } else if (arg->argc > arg->argn) { 71708676022SBrian Somers if (!strcasecmp(arg->argv[arg->argn], "label")) { 71808676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_LOCAL; 71908676022SBrian Somers strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 72008676022SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 72108676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 722259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 723259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 724d47dceb8SBrian Somers else 725259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 726d47dceb8SBrian Somers memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 72708676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_IP; 72849052c95SBrian Somers mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 72908676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 73008676022SBrian Somers struct sockaddr_dl hwaddr; 73108676022SBrian Somers int s; 73208676022SBrian Somers 733259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 734259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr; 735d47dceb8SBrian Somers else 736259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 737d47dceb8SBrian Somers 73808676022SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 73908676022SBrian Somers if (s < 0) { 740dd7e2610SBrian Somers log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno)); 74108676022SBrian Somers return 2; 74208676022SBrian Somers } 743d47dceb8SBrian Somers if (get_ether_addr(s, addr, &hwaddr)) { 74408676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAC; 74508676022SBrian Somers memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 74608676022SBrian Somers hwaddr.sdl_alen); 74708676022SBrian Somers mp->cfg.enddisc.len = hwaddr.sdl_alen; 74808676022SBrian Somers } else { 749dd7e2610SBrian Somers log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 750259a6191SBrian Somers inet_ntoa(addr)); 75108676022SBrian Somers close(s); 75208676022SBrian Somers return 4; 75308676022SBrian Somers } 75408676022SBrian Somers close(s); 75549052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 75649052c95SBrian Somers int f; 75749052c95SBrian Somers 75849052c95SBrian Somers randinit(); 75949052c95SBrian Somers for (f = 0; f < 20; f += sizeof(long)) 76049052c95SBrian Somers *(long *)(mp->cfg.enddisc.address + f) = random(); 76108676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAGIC; 76249052c95SBrian Somers mp->cfg.enddisc.len = 20; 76349052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 76449052c95SBrian Somers if (arg->argc > arg->argn+1) { 76508676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_PSN; 76649052c95SBrian Somers strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 76749052c95SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 76849052c95SBrian Somers } else { 769dd7e2610SBrian Somers log_Printf(LogWARN, "PSN endpoint requires additional data\n"); 77008676022SBrian Somers return 5; 77149052c95SBrian Somers } 77249052c95SBrian Somers } else { 773dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Unrecognised endpoint type\n", 77449052c95SBrian Somers arg->argv[arg->argn]); 77508676022SBrian Somers return 6; 77649052c95SBrian Somers } 777e43ebac1SBrian Somers } 77849052c95SBrian Somers 77949052c95SBrian Somers return 0; 78049052c95SBrian Somers } 7811fa665f5SBrian Somers 7821fa665f5SBrian Somers static int 7831fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, 7841fa665f5SBrian Somers int *n) 7851fa665f5SBrian Somers { 7861fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 7871fa665f5SBrian Somers 7886f384573SBrian Somers if (s->send.dl != NULL) { 78996c9bb21SBrian Somers /* We've connect()ed */ 79096c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 7916f384573SBrian Somers s->send.dl = NULL; 79296c9bb21SBrian Somers close(s->fd); 79396c9bb21SBrian Somers s->fd = -1; 79496c9bb21SBrian Somers } else if (r && s->fd >= 0) { 7951fa665f5SBrian Somers if (*n < s->fd + 1) 7961fa665f5SBrian Somers *n = s->fd + 1; 7971fa665f5SBrian Somers FD_SET(s->fd, r); 7981fa665f5SBrian Somers return 1; 7991fa665f5SBrian Somers } 8001fa665f5SBrian Somers return 0; 8011fa665f5SBrian Somers } 8021fa665f5SBrian Somers 8031fa665f5SBrian Somers static int 8041fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset) 8051fa665f5SBrian Somers { 8061fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 8071fa665f5SBrian Somers return s->fd >= 0 && FD_ISSET(s->fd, fdset); 8081fa665f5SBrian Somers } 8091fa665f5SBrian Somers 8101fa665f5SBrian Somers static void 8111fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 8121fa665f5SBrian Somers { 8131fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 8146f384573SBrian Somers struct sockaddr in; 8151fa665f5SBrian Somers int fd, size; 8161fa665f5SBrian Somers 8176f384573SBrian Somers size = sizeof in; 8186f384573SBrian Somers fd = accept(s->fd, &in, &size); 8191fa665f5SBrian Somers if (fd < 0) { 820dd7e2610SBrian Somers log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno)); 8211fa665f5SBrian Somers return; 8221fa665f5SBrian Somers } 8231fa665f5SBrian Somers 82496c9bb21SBrian Somers if (in.sa_family == AF_LOCAL) /* ??? */ 82596c9bb21SBrian Somers bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in); 82696c9bb21SBrian Somers 8271fa665f5SBrian Somers close(fd); 8281fa665f5SBrian Somers } 8291fa665f5SBrian Somers 8301fa665f5SBrian Somers static void 8311fa665f5SBrian Somers mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset) 8321fa665f5SBrian Somers { 8331fa665f5SBrian Somers /* We never want to write here ! */ 834dd7e2610SBrian Somers log_Printf(LogERROR, "mpserver_Write: Internal error: Bad call !\n"); 8351fa665f5SBrian Somers } 8361fa665f5SBrian Somers 8371fa665f5SBrian Somers void 8381fa665f5SBrian Somers mpserver_Init(struct mpserver *s) 8391fa665f5SBrian Somers { 8401fa665f5SBrian Somers s->desc.type = MPSERVER_DESCRIPTOR; 8411fa665f5SBrian Somers s->desc.next = NULL; 8421fa665f5SBrian Somers s->desc.UpdateSet = mpserver_UpdateSet; 8431fa665f5SBrian Somers s->desc.IsSet = mpserver_IsSet; 8441fa665f5SBrian Somers s->desc.Read = mpserver_Read; 8451fa665f5SBrian Somers s->desc.Write = mpserver_Write; 8466f384573SBrian Somers s->send.dl = NULL; 8471fa665f5SBrian Somers s->fd = -1; 8486f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 8491fa665f5SBrian Somers } 8501fa665f5SBrian Somers 8511fa665f5SBrian Somers int 8521fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer) 8531fa665f5SBrian Somers { 85496c9bb21SBrian Somers int f, l; 8551fa665f5SBrian Somers mode_t mask; 8561fa665f5SBrian Somers 8571fa665f5SBrian Somers if (s->fd != -1) { 858dd7e2610SBrian Somers log_Printf(LogERROR, "Internal error ! mpserver already open\n"); 8596f384573SBrian Somers mpserver_Close(s); 8601fa665f5SBrian Somers } 8611fa665f5SBrian Somers 8626f384573SBrian Somers l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-", 8636f384573SBrian Somers _PATH_VARRUN, peer->authname, peer->enddisc.class); 8641fa665f5SBrian Somers 8656f384573SBrian Somers for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) { 8666f384573SBrian Somers snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l, 8671fa665f5SBrian Somers "%02x", *(u_char *)(peer->enddisc.address+f)); 8686f384573SBrian Somers l += 2; 8691fa665f5SBrian Somers } 8701fa665f5SBrian Somers 8716f384573SBrian Somers s->socket.sun_family = AF_LOCAL; 8726f384573SBrian Somers s->socket.sun_len = sizeof s->socket; 8731fa665f5SBrian Somers s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0); 8741fa665f5SBrian Somers if (s->fd < 0) { 875dd7e2610SBrian Somers log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno)); 87696c9bb21SBrian Somers return MPSERVER_FAILED; 8771fa665f5SBrian Somers } 8781fa665f5SBrian Somers 8796f384573SBrian Somers setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket, 8806f384573SBrian Somers sizeof s->socket); 8811fa665f5SBrian Somers mask = umask(0177); 8826f384573SBrian Somers if (ID0bind_un(s->fd, &s->socket) < 0) { 8836f384573SBrian Somers if (errno != EADDRINUSE) { 884dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n", 8856f384573SBrian Somers s->socket.sun_path, strerror(errno)); 8861fa665f5SBrian Somers umask(mask); 8871fa665f5SBrian Somers close(s->fd); 8881fa665f5SBrian Somers s->fd = -1; 88996c9bb21SBrian Somers return MPSERVER_FAILED; 8906f384573SBrian Somers } 8916f384573SBrian Somers umask(mask); 8926f384573SBrian Somers if (ID0connect_un(s->fd, &s->socket) < 0) { 893dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n", 8946f384573SBrian Somers s->socket.sun_path, strerror(errno)); 8956f384573SBrian Somers if (errno == ECONNREFUSED) 896dd7e2610SBrian Somers log_Printf(LogPHASE, " Has the previous server died badly ?\n"); 8976f384573SBrian Somers close(s->fd); 8986f384573SBrian Somers s->fd = -1; 89996c9bb21SBrian Somers return MPSERVER_FAILED; 9006f384573SBrian Somers } 9016f384573SBrian Somers 9026f384573SBrian Somers /* Donate our link to the other guy */ 90396c9bb21SBrian Somers return MPSERVER_CONNECTED; 9041fa665f5SBrian Somers } 9056f384573SBrian Somers 9066f384573SBrian Somers /* Listen for other ppp invocations that want to donate links */ 9071fa665f5SBrian Somers if (listen(s->fd, 5) != 0) { 908dd7e2610SBrian Somers log_Printf(LogERROR, "mpserver: Unable to listen to socket" 9091fa665f5SBrian Somers " - BUNDLE overload?\n"); 9101fa665f5SBrian Somers mpserver_Close(s); 9111fa665f5SBrian Somers } 9121fa665f5SBrian Somers 91396c9bb21SBrian Somers return MPSERVER_LISTENING; 9141fa665f5SBrian Somers } 9151fa665f5SBrian Somers 9161fa665f5SBrian Somers void 9171fa665f5SBrian Somers mpserver_Close(struct mpserver *s) 9181fa665f5SBrian Somers { 9196f384573SBrian Somers if (s->send.dl != NULL) { 92096c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 9216f384573SBrian Somers s->send.dl = NULL; 92296c9bb21SBrian Somers close(s->fd); 92396c9bb21SBrian Somers s->fd = -1; 92496c9bb21SBrian Somers } else if (s->fd >= 0) { 9251fa665f5SBrian Somers close(s->fd); 9266f384573SBrian Somers if (ID0unlink(s->socket.sun_path) == -1) 927dd7e2610SBrian Somers log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path, 9286f384573SBrian Somers strerror(errno)); 9296f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 9301fa665f5SBrian Somers s->fd = -1; 9311fa665f5SBrian Somers } 9321fa665f5SBrian Somers } 933