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 * 2697d92980SPeter Wemm * $FreeBSD$ 273b0f8d2eSBrian Somers */ 283b0f8d2eSBrian Somers 29972a1bcfSBrian Somers #include <sys/param.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 475d9e6103SBrian Somers #include "layer.h" 4867b072f7SBrian Somers #ifndef NONAT 49f02c2029SBrian Somers #include "nat_cmd.h" 505d9e6103SBrian Somers #endif 515d9e6103SBrian Somers #include "vjcomp.h" 529e8ec64bSBrian Somers #include "ua.h" 53c9e11a11SBrian Somers #include "defs.h" 543b0f8d2eSBrian Somers #include "command.h" 553b0f8d2eSBrian Somers #include "mbuf.h" 563b0f8d2eSBrian Somers #include "log.h" 573b0f8d2eSBrian Somers #include "timer.h" 583b0f8d2eSBrian Somers #include "fsm.h" 593b0f8d2eSBrian Somers #include "iplist.h" 603b0f8d2eSBrian Somers #include "throughput.h" 613b0f8d2eSBrian Somers #include "slcompress.h" 625a72b6edSBrian Somers #include "lqr.h" 635a72b6edSBrian Somers #include "hdlc.h" 6430949fd4SBrian Somers #include "ncpaddr.h" 6530949fd4SBrian Somers #include "ip.h" 663b0f8d2eSBrian Somers #include "ipcp.h" 673b0f8d2eSBrian Somers #include "auth.h" 683b0f8d2eSBrian Somers #include "lcp.h" 693b0f8d2eSBrian Somers #include "async.h" 703b0f8d2eSBrian Somers #include "ccp.h" 713b0f8d2eSBrian Somers #include "link.h" 723b0f8d2eSBrian Somers #include "descriptor.h" 733b0f8d2eSBrian Somers #include "physical.h" 743b0f8d2eSBrian Somers #include "chat.h" 755d9e6103SBrian Somers #include "proto.h" 763b0f8d2eSBrian Somers #include "filter.h" 773b0f8d2eSBrian Somers #include "mp.h" 783b0f8d2eSBrian Somers #include "chap.h" 7992b09558SBrian Somers #include "cbcp.h" 803b0f8d2eSBrian Somers #include "datalink.h" 81972a1bcfSBrian Somers #ifndef NORADIUS 82972a1bcfSBrian Somers #include "radius.h" 83972a1bcfSBrian Somers #endif 8430949fd4SBrian Somers #include "ipv6cp.h" 8530949fd4SBrian Somers #include "ncp.h" 863b0f8d2eSBrian Somers #include "bundle.h" 8749052c95SBrian Somers #include "prompt.h" 8808676022SBrian Somers #include "id.h" 8908676022SBrian Somers #include "arp.h" 903b0f8d2eSBrian Somers 91643f4904SBrian Somers void 92643f4904SBrian Somers peerid_Init(struct peerid *peer) 93643f4904SBrian Somers { 94643f4904SBrian Somers peer->enddisc.class = 0; 95643f4904SBrian Somers *peer->enddisc.address = '\0'; 96643f4904SBrian Somers peer->enddisc.len = 0; 97643f4904SBrian Somers *peer->authname = '\0'; 98643f4904SBrian Somers } 99643f4904SBrian Somers 100643f4904SBrian Somers int 101643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2) 102643f4904SBrian Somers { 103643f4904SBrian Somers return !strcmp(p1->authname, p2->authname) && 104643f4904SBrian Somers p1->enddisc.class == p2->enddisc.class && 105643f4904SBrian Somers p1->enddisc.len == p2->enddisc.len && 106643f4904SBrian Somers !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 107643f4904SBrian Somers } 108643f4904SBrian Somers 1093b0f8d2eSBrian Somers static u_int32_t 110147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq) 1113b0f8d2eSBrian Somers { 1123b0f8d2eSBrian Somers seq++; 113147613eaSBrian Somers if (is12bit) { 1143b0f8d2eSBrian Somers if (seq & 0xfffff000) 1153b0f8d2eSBrian Somers seq = 0; 1163b0f8d2eSBrian Somers } else if (seq & 0xff000000) 1173b0f8d2eSBrian Somers seq = 0; 1183b0f8d2eSBrian Somers return seq; 1193b0f8d2eSBrian Somers } 1203b0f8d2eSBrian Somers 1213b0f8d2eSBrian Somers static int 1221bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2) 1231bcced10SBrian Somers { 124b4b280abSBrian Somers u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200; 125b4b280abSBrian Somers 126b4b280abSBrian Somers if (seq1 > max) { 127b4b280abSBrian Somers if (seq2 < 0x200 || seq2 > seq1) 128b4b280abSBrian Somers return 1; 129b4b280abSBrian Somers } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2) 130b4b280abSBrian Somers return 1; 131b4b280abSBrian Somers 132b4b280abSBrian Somers return 0; 1331bcced10SBrian Somers } 1341bcced10SBrian Somers 1351bcced10SBrian Somers static int 1363b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 1373b0f8d2eSBrian Somers { 13849052c95SBrian Somers if (mp->local_is12bit) { 1399e8ec64bSBrian Somers u_int16_t val; 1409e8ec64bSBrian Somers 1419e8ec64bSBrian Somers ua_ntohs(MBUF_CTOP(m), &val); 1429e8ec64bSBrian Somers if (val & 0x3000) { 143dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1443b0f8d2eSBrian Somers return 0; 1453b0f8d2eSBrian Somers } 1469e8ec64bSBrian Somers header->begin = val & 0x8000 ? 1 : 0; 1479e8ec64bSBrian Somers header->end = val & 0x4000 ? 1 : 0; 1489e8ec64bSBrian Somers header->seq = val & 0x0fff; 1493b0f8d2eSBrian Somers return 2; 1503b0f8d2eSBrian Somers } else { 1519e8ec64bSBrian Somers ua_ntohl(MBUF_CTOP(m), &header->seq); 1523b0f8d2eSBrian Somers if (header->seq & 0x3f000000) { 153dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1543b0f8d2eSBrian Somers return 0; 1553b0f8d2eSBrian Somers } 156ce828a6eSBrian Somers header->begin = header->seq & 0x80000000 ? 1 : 0; 157ce828a6eSBrian Somers header->end = header->seq & 0x40000000 ? 1 : 0; 1583b0f8d2eSBrian Somers header->seq &= 0x00ffffff; 1593b0f8d2eSBrian Somers return 4; 1603b0f8d2eSBrian Somers } 1613b0f8d2eSBrian Somers } 1623b0f8d2eSBrian Somers 1633b0f8d2eSBrian Somers static void 1643b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp) 1653b0f8d2eSBrian Somers { 16649052c95SBrian Somers /* The given FSM (ccp) is about to start up ! */ 1673b0f8d2eSBrian Somers } 1683b0f8d2eSBrian Somers 1693b0f8d2eSBrian Somers static void 1703b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp) 1713b0f8d2eSBrian Somers { 17249052c95SBrian Somers /* The given fsm (ccp) is now up */ 1736301d506SBrian Somers 1746301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 1753b0f8d2eSBrian Somers } 1763b0f8d2eSBrian Somers 1773b0f8d2eSBrian Somers static void 1783b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp) 1793b0f8d2eSBrian Somers { 18049052c95SBrian Somers /* The given FSM (ccp) has been told to come down */ 1813b0f8d2eSBrian Somers } 1823b0f8d2eSBrian Somers 1833b0f8d2eSBrian Somers static void 1843b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp) 1853b0f8d2eSBrian Somers { 18649052c95SBrian Somers /* The given fsm (ccp) is now down */ 1870a1b5c9dSBrian Somers if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 1880a1b5c9dSBrian Somers fsm_Open(fp); /* CCP goes to ST_STOPPED */ 1893b0f8d2eSBrian Somers } 1903b0f8d2eSBrian Somers 191ab2de065SBrian Somers static void 192ab2de065SBrian Somers mp_UpDown(void *v) 193ab2de065SBrian Somers { 194ab2de065SBrian Somers struct mp *mp = (struct mp *)v; 195ab2de065SBrian Somers int percent; 196ab2de065SBrian Somers 19791cbd2eeSBrian Somers percent = MAX(mp->link.stats.total.in.OctetsPerSecond, 19891cbd2eeSBrian Somers mp->link.stats.total.out.OctetsPerSecond) * 800 / 19991cbd2eeSBrian Somers mp->bundle->bandwidth; 200ab2de065SBrian Somers if (percent >= mp->cfg.autoload.max) { 201ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent); 202ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_UP); 203ab2de065SBrian Somers } else if (percent <= mp->cfg.autoload.min) { 204ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent); 205ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN); 206ab2de065SBrian Somers } 207ab2de065SBrian Somers } 208ab2de065SBrian Somers 209ab2de065SBrian Somers void 210ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp) 211ab2de065SBrian Somers { 21211572abfSBrian Somers throughput_stop(&mp->link.stats.total); 213ab2de065SBrian Somers } 214ab2de065SBrian Somers 215ab2de065SBrian Somers void 216ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp) 217ab2de065SBrian Somers { 21811572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) { 21911572abfSBrian Somers throughput_destroy(&mp->link.stats.total); 22011572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 22111572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 222ab2de065SBrian Somers } 223ab2de065SBrian Somers 224ab2de065SBrian Somers if (bundle_WantAutoloadTimer(mp->bundle)) 22511572abfSBrian Somers throughput_start(&mp->link.stats.total, "MP throughput", 1); 226ab2de065SBrian Somers else 227ab2de065SBrian Somers mp_StopAutoloadTimer(mp); 228ab2de065SBrian Somers } 229ab2de065SBrian Somers 230ab2de065SBrian Somers void 231ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp) 232ab2de065SBrian Somers { 23311572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) 234ab2de065SBrian Somers mp_CheckAutoloadTimer(mp); 235ab2de065SBrian Somers else 23611572abfSBrian Somers throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL); 237ab2de065SBrian Somers } 238ab2de065SBrian Somers 2393b0f8d2eSBrian Somers void 2403b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle) 2413b0f8d2eSBrian Somers { 24249052c95SBrian Somers mp->peer_is12bit = mp->local_is12bit = 0; 24349052c95SBrian Somers mp->peer_mrru = mp->local_mrru = 0; 244643f4904SBrian Somers 245643f4904SBrian Somers peerid_Init(&mp->peer); 246643f4904SBrian Somers 2479c53a7b1SBrian Somers mp->out.seq = 0; 2489c53a7b1SBrian Somers mp->out.link = 0; 24930949fd4SBrian Somers mp->out.af = AF_INET; 2503b0f8d2eSBrian Somers mp->seq.min_in = 0; 2513b0f8d2eSBrian Somers mp->seq.next_in = 0; 2523b0f8d2eSBrian Somers mp->inbufs = NULL; 2533b0f8d2eSBrian Somers mp->bundle = bundle; 2543b0f8d2eSBrian Somers 2555d9e6103SBrian Somers mp->link.type = LOGICAL_LINK; 2563b0f8d2eSBrian Somers mp->link.name = "mp"; 2573b0f8d2eSBrian Somers mp->link.len = sizeof *mp; 258643f4904SBrian Somers 259ab2de065SBrian Somers mp->cfg.autoload.period = SAMPLE_PERIOD; 260ab2de065SBrian Somers mp->cfg.autoload.min = mp->cfg.autoload.max = 0; 26111572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 26211572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 26311572abfSBrian Somers mp->link.stats.parent = NULL; 26411572abfSBrian Somers mp->link.stats.gather = 0; /* Let the physical links gather stats */ 2653b0f8d2eSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 2663b0f8d2eSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 2673b0f8d2eSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 2683b0f8d2eSBrian Somers 2693b0f8d2eSBrian Somers mp->fsmp.LayerStart = mp_LayerStart; 2703b0f8d2eSBrian Somers mp->fsmp.LayerUp = mp_LayerUp; 2713b0f8d2eSBrian Somers mp->fsmp.LayerDown = mp_LayerDown; 2723b0f8d2eSBrian Somers mp->fsmp.LayerFinish = mp_LayerFinish; 2733b0f8d2eSBrian Somers mp->fsmp.object = mp; 2743b0f8d2eSBrian Somers 2751fa665f5SBrian Somers mpserver_Init(&mp->server); 2761fa665f5SBrian Somers 27749052c95SBrian Somers mp->cfg.mrru = 0; 27849052c95SBrian Somers mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 2797063995cSBrian Somers mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED; 28049052c95SBrian Somers mp->cfg.enddisc.class = 0; 28149052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 28249052c95SBrian Somers mp->cfg.enddisc.len = 0; 28349052c95SBrian Somers 2843b0f8d2eSBrian Somers lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 2853b0f8d2eSBrian Somers ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 2865d9e6103SBrian Somers 2875d9e6103SBrian Somers link_EmptyStack(&mp->link); 2885d9e6103SBrian Somers link_Stack(&mp->link, &protolayer); 2895d9e6103SBrian Somers link_Stack(&mp->link, &ccplayer); 2905d9e6103SBrian Somers link_Stack(&mp->link, &vjlayer); 29167b072f7SBrian Somers #ifndef NONAT 29267b072f7SBrian Somers link_Stack(&mp->link, &natlayer); 2935d9e6103SBrian Somers #endif 29449052c95SBrian Somers } 29549052c95SBrian Somers 29649052c95SBrian Somers int 2971fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl) 29849052c95SBrian Somers { 2991fa665f5SBrian Somers struct lcp *lcp = &dl->physical->link.lcp; 3001fa665f5SBrian Somers 30149052c95SBrian Somers if (mp->active) { 30249052c95SBrian Somers /* We're adding a link - do a last validation on our parameters */ 3031fa665f5SBrian Somers if (!peerid_Equal(&dl->peer, &mp->peer)) { 304dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name); 3057ebff009SBrian Somers log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname, 3067ebff009SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 3077ebff009SBrian Somers mp->peer.enddisc.len)); 3087ebff009SBrian Somers log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname, 3097ebff009SBrian Somers mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address, 3107ebff009SBrian Somers dl->peer.enddisc.len)); 3111fa665f5SBrian Somers return MP_FAILED; 312643f4904SBrian Somers } 3131fa665f5SBrian Somers if (mp->local_mrru != lcp->want_mrru || 3141fa665f5SBrian Somers mp->peer_mrru != lcp->his_mrru || 3151fa665f5SBrian Somers mp->local_is12bit != lcp->want_shortseq || 3161fa665f5SBrian Somers mp->peer_is12bit != lcp->his_shortseq) { 317dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n", 3181fa665f5SBrian Somers dl->name); 3191fa665f5SBrian Somers return MP_FAILED; 32049052c95SBrian Somers } 3211fa665f5SBrian Somers return MP_ADDED; 32249052c95SBrian Somers } else { 32349052c95SBrian Somers /* First link in multilink mode */ 32449052c95SBrian Somers 3251fa665f5SBrian Somers mp->local_mrru = lcp->want_mrru; 3261fa665f5SBrian Somers mp->peer_mrru = lcp->his_mrru; 3271fa665f5SBrian Somers mp->local_is12bit = lcp->want_shortseq; 3281fa665f5SBrian Somers mp->peer_is12bit = lcp->his_shortseq; 3291fa665f5SBrian Somers mp->peer = dl->peer; 33049052c95SBrian Somers 33111572abfSBrian Somers throughput_destroy(&mp->link.stats.total); 33211572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 33311572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 334673903ecSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 335673903ecSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 336673903ecSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 337673903ecSBrian Somers 33811572abfSBrian Somers /* Tell the link who it belongs to */ 33911572abfSBrian Somers dl->physical->link.stats.parent = &mp->link.stats.total; 34011572abfSBrian Somers 3419c53a7b1SBrian Somers mp->out.seq = 0; 3429c53a7b1SBrian Somers mp->out.link = 0; 34330949fd4SBrian Somers mp->out.af = AF_INET; 344673903ecSBrian Somers mp->seq.min_in = 0; 345673903ecSBrian Somers mp->seq.next_in = 0; 346673903ecSBrian Somers 3471fa665f5SBrian Somers /* 3481fa665f5SBrian Somers * Now we create our server socket. 3491fa665f5SBrian Somers * If it already exists, join it. Otherwise, create and own it 3501fa665f5SBrian Somers */ 35196c9bb21SBrian Somers switch (mpserver_Open(&mp->server, &mp->peer)) { 35296c9bb21SBrian Somers case MPSERVER_CONNECTED: 353dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Transfer link on %s\n", 3546f384573SBrian Somers mp->server.socket.sun_path); 35596c9bb21SBrian Somers mp->server.send.dl = dl; /* Defer 'till it's safe to send */ 3561fa665f5SBrian Somers return MP_LINKSENT; 35796c9bb21SBrian Somers case MPSERVER_FAILED: 3581fa665f5SBrian Somers return MP_FAILED; 35996c9bb21SBrian Somers case MPSERVER_LISTENING: 360dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path); 361dd7e2610SBrian Somers log_Printf(LogPHASE, " First link: %s\n", dl->name); 3621fa665f5SBrian Somers 36330949fd4SBrian Somers /* Re-point our NCP layers at our MP link */ 36430949fd4SBrian Somers ncp_SetLink(&mp->bundle->ncp, &mp->link); 3653b0f8d2eSBrian Somers 3663b0f8d2eSBrian Somers /* Our lcp's already up 'cos of the NULL parent */ 36706337856SBrian Somers if (ccp_SetOpenMode(&mp->link.ccp)) { 368dd7e2610SBrian Somers fsm_Up(&mp->link.ccp.fsm); 369dd7e2610SBrian Somers fsm_Open(&mp->link.ccp.fsm); 37006337856SBrian Somers } 3713b0f8d2eSBrian Somers 3723b0f8d2eSBrian Somers mp->active = 1; 37396c9bb21SBrian Somers break; 37449052c95SBrian Somers } 3751fa665f5SBrian Somers } 3763b0f8d2eSBrian Somers 3771fa665f5SBrian Somers return MP_UP; 3783b0f8d2eSBrian Somers } 3793b0f8d2eSBrian Somers 3803b0f8d2eSBrian Somers void 381673903ecSBrian Somers mp_Down(struct mp *mp) 382673903ecSBrian Somers { 383673903ecSBrian Somers if (mp->active) { 384673903ecSBrian Somers struct mbuf *next; 385673903ecSBrian Somers 386ab2de065SBrian Somers /* Stop that ! */ 387ab2de065SBrian Somers mp_StopAutoloadTimer(mp); 388ab2de065SBrian Somers 3891fa665f5SBrian Somers /* Don't want any more of these */ 3901fa665f5SBrian Somers mpserver_Close(&mp->server); 3911fa665f5SBrian Somers 3921fa665f5SBrian Somers /* CCP goes down with a bang */ 39309206a6fSBrian Somers fsm2initial(&mp->link.ccp.fsm); 394673903ecSBrian Somers 395673903ecSBrian Somers /* Received fragments go in the bit-bucket */ 396673903ecSBrian Somers while (mp->inbufs) { 39726af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 39826af0ae9SBrian Somers m_freem(mp->inbufs); 399673903ecSBrian Somers mp->inbufs = next; 400673903ecSBrian Somers } 401673903ecSBrian Somers 402643f4904SBrian Somers peerid_Init(&mp->peer); 403673903ecSBrian Somers mp->active = 0; 404673903ecSBrian Somers } 405673903ecSBrian Somers } 406673903ecSBrian Somers 407673903ecSBrian Somers void 4083b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink) 4093b0f8d2eSBrian Somers { 4103b0f8d2eSBrian Somers mplink->seq = 0; 411ab2de065SBrian Somers mplink->bandwidth = 0; 4123b0f8d2eSBrian Somers } 4133b0f8d2eSBrian Somers 4145d9e6103SBrian Somers static void 4155d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p) 4163b0f8d2eSBrian Somers { 4173b0f8d2eSBrian Somers struct mp_header mh, h; 4183b0f8d2eSBrian Somers struct mbuf *q, *last; 4193b0f8d2eSBrian Somers int32_t seq; 4203b0f8d2eSBrian Somers 42186b1f0d7SBrian Somers /* 42286b1f0d7SBrian Somers * When `m' and `p' are NULL, it means our oldest link has gone down. 42386b1f0d7SBrian Somers * We want to determine a new min, and process any intermediate stuff 42486b1f0d7SBrian Somers * as normal 42586b1f0d7SBrian Somers */ 42686b1f0d7SBrian Somers 42786b1f0d7SBrian Somers if (m && mp_ReadHeader(mp, m, &mh) == 0) { 42826af0ae9SBrian Somers m_freem(m); 4293b0f8d2eSBrian Somers return; 4303b0f8d2eSBrian Somers } 4313b0f8d2eSBrian Somers 43286b1f0d7SBrian Somers if (p) { 4333b0f8d2eSBrian Somers seq = p->dl->mp.seq; 4343b0f8d2eSBrian Somers p->dl->mp.seq = mh.seq; 43586b1f0d7SBrian Somers } else 43686b1f0d7SBrian Somers seq = mp->seq.min_in; 43786b1f0d7SBrian Somers 4383b0f8d2eSBrian Somers if (mp->seq.min_in == seq) { 4393b0f8d2eSBrian Somers /* 4403b0f8d2eSBrian Somers * We've received new data on the link that has our min (oldest) seq. 4413b0f8d2eSBrian Somers * Figure out which link now has the smallest (oldest) seq. 4423b0f8d2eSBrian Somers */ 4433b0f8d2eSBrian Somers struct datalink *dl; 4443b0f8d2eSBrian Somers 44586b1f0d7SBrian Somers mp->seq.min_in = (u_int32_t)-1; 4463b0f8d2eSBrian Somers for (dl = mp->bundle->links; dl; dl = dl->next) 44786b1f0d7SBrian Somers if (dl->state == DATALINK_OPEN && 44886b1f0d7SBrian Somers (mp->seq.min_in == -1 || 44986b1f0d7SBrian Somers isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in))) 4503b0f8d2eSBrian Somers mp->seq.min_in = dl->mp.seq; 4513b0f8d2eSBrian Somers } 4523b0f8d2eSBrian Somers 4533b0f8d2eSBrian Somers /* 4543b0f8d2eSBrian Somers * Now process as many of our fragments as we can, adding our new 4553b0f8d2eSBrian Somers * fragment in as we go, and ordering with the oldest at the top of 4563b0f8d2eSBrian Somers * the queue. 4573b0f8d2eSBrian Somers */ 4583b0f8d2eSBrian Somers 4593b0f8d2eSBrian Somers last = NULL; 4603b0f8d2eSBrian Somers seq = mp->seq.next_in; 4613b0f8d2eSBrian Somers q = mp->inbufs; 462cb8bd8ddSBrian Somers while (q || m) { 463cb8bd8ddSBrian Somers if (!q) { 464cb8bd8ddSBrian Somers if (last) 465cb8bd8ddSBrian Somers last->m_nextpkt = m; 466cb8bd8ddSBrian Somers else 467cb8bd8ddSBrian Somers mp->inbufs = m; 468cb8bd8ddSBrian Somers q = m; 469cb8bd8ddSBrian Somers m = NULL; 470cb8bd8ddSBrian Somers h = mh; 471cb8bd8ddSBrian Somers } else { 4723b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 473cb8bd8ddSBrian Somers 4741bcced10SBrian Somers if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) { 4753b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 4763b0f8d2eSBrian Somers if (last) 47726af0ae9SBrian Somers last->m_nextpkt = m; 4783b0f8d2eSBrian Somers else 4793b0f8d2eSBrian Somers mp->inbufs = m; 48026af0ae9SBrian Somers m->m_nextpkt = q; 4813b0f8d2eSBrian Somers q = m; 4823b0f8d2eSBrian Somers h = mh; 4833b0f8d2eSBrian Somers m = NULL; 4843b0f8d2eSBrian Somers } 485cb8bd8ddSBrian Somers } 4863b0f8d2eSBrian Somers 4873b0f8d2eSBrian Somers if (h.seq != seq) { 4883b0f8d2eSBrian Somers /* we're missing something :-( */ 48964e0f466SBrian Somers if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) { 4903b0f8d2eSBrian Somers /* we're never gonna get it */ 4913b0f8d2eSBrian Somers struct mbuf *next; 4923b0f8d2eSBrian Somers 4933b0f8d2eSBrian Somers /* Zap all older fragments */ 4943b0f8d2eSBrian Somers while (mp->inbufs != q) { 495dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag\n"); 49626af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 49726af0ae9SBrian Somers m_freem(mp->inbufs); 4983b0f8d2eSBrian Somers mp->inbufs = next; 4993b0f8d2eSBrian Somers } 5003b0f8d2eSBrian Somers 5013b0f8d2eSBrian Somers /* 5023b0f8d2eSBrian Somers * Zap everything until the next `end' fragment OR just before 5033b0f8d2eSBrian Somers * the next `begin' fragment OR 'till seq.min_in - whichever 5043b0f8d2eSBrian Somers * comes first. 5053b0f8d2eSBrian Somers */ 5063b0f8d2eSBrian Somers do { 5073b0f8d2eSBrian Somers mp_ReadHeader(mp, mp->inbufs, &h); 5083b0f8d2eSBrian Somers if (h.begin) { 509ce828a6eSBrian Somers /* We might be able to process this ! */ 5103b0f8d2eSBrian Somers h.seq--; /* We're gonna look for fragment with h.seq+1 */ 5113b0f8d2eSBrian Somers break; 5123b0f8d2eSBrian Somers } 51326af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 514dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag %u\n", h.seq); 51526af0ae9SBrian Somers m_freem(mp->inbufs); 5163b0f8d2eSBrian Somers mp->inbufs = next; 51764e0f466SBrian Somers } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in, 51864e0f466SBrian Somers h.seq) || h.end)); 5193b0f8d2eSBrian Somers 5203b0f8d2eSBrian Somers /* 5213b0f8d2eSBrian Somers * Continue processing things from here. 5223b0f8d2eSBrian Somers * This deals with the possibility that we received a fragment 5233b0f8d2eSBrian Somers * on the slowest link that invalidates some of our data (because 5243b0f8d2eSBrian Somers * of the hole at `q'), but where there are subsequent `whole' 5253b0f8d2eSBrian Somers * packets that have already been received. 5263b0f8d2eSBrian Somers */ 5273b0f8d2eSBrian Somers 528147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 5293b0f8d2eSBrian Somers last = NULL; 5303b0f8d2eSBrian Somers q = mp->inbufs; 5313b0f8d2eSBrian Somers } else 5323b0f8d2eSBrian Somers /* we may still receive the missing fragment */ 5333b0f8d2eSBrian Somers break; 5343b0f8d2eSBrian Somers } else if (h.end) { 5353b0f8d2eSBrian Somers /* We've got something, reassemble */ 5363b0f8d2eSBrian Somers struct mbuf **frag = &q; 5373b0f8d2eSBrian Somers int len; 538ce828a6eSBrian Somers u_long first = -1; 5393b0f8d2eSBrian Somers 5403b0f8d2eSBrian Somers do { 5413b0f8d2eSBrian Somers *frag = mp->inbufs; 54226af0ae9SBrian Somers mp->inbufs = mp->inbufs->m_nextpkt; 5433b0f8d2eSBrian Somers len = mp_ReadHeader(mp, *frag, &h); 544ce828a6eSBrian Somers if (first == -1) 545ce828a6eSBrian Somers first = h.seq; 54626af0ae9SBrian Somers (*frag)->m_offset += len; 54726af0ae9SBrian Somers (*frag)->m_len -= len; 54826af0ae9SBrian Somers (*frag)->m_nextpkt = NULL; 5493b0f8d2eSBrian Somers if (frag == &q && !h.begin) { 550dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 5513b0f8d2eSBrian Somers (u_long)h.seq); 55226af0ae9SBrian Somers m_freem(q); 5533b0f8d2eSBrian Somers q = NULL; 5543b0f8d2eSBrian Somers } else if (frag != &q && h.begin) { 555dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 5563b0f8d2eSBrian Somers (u_long)h.seq - 1); 5573b0f8d2eSBrian Somers /* 5583b0f8d2eSBrian Somers * Stuff our fragment back at the front of the queue and zap 5593b0f8d2eSBrian Somers * our half-assembed packet. 5603b0f8d2eSBrian Somers */ 56126af0ae9SBrian Somers (*frag)->m_nextpkt = mp->inbufs; 5623b0f8d2eSBrian Somers mp->inbufs = *frag; 5633b0f8d2eSBrian Somers *frag = NULL; 56426af0ae9SBrian Somers m_freem(q); 5653b0f8d2eSBrian Somers q = NULL; 5663b0f8d2eSBrian Somers frag = &q; 5673b0f8d2eSBrian Somers h.end = 0; /* just in case it's a whole packet */ 5683b0f8d2eSBrian Somers } else 5693b0f8d2eSBrian Somers do 57026af0ae9SBrian Somers frag = &(*frag)->m_next; 571ce828a6eSBrian Somers while (*frag != NULL); 5723b0f8d2eSBrian Somers } while (!h.end); 5733b0f8d2eSBrian Somers 5743b0f8d2eSBrian Somers if (q) { 57526af0ae9SBrian Somers q = m_pullup(q); 576dd7e2610SBrian Somers log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n", 57726af0ae9SBrian Somers first, (u_long)h.seq, m_length(q)); 57826af0ae9SBrian Somers link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle); 57926af0ae9SBrian Somers m_freem(q); 5803b0f8d2eSBrian Somers } 5813b0f8d2eSBrian Somers 582147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 5833b0f8d2eSBrian Somers last = NULL; 5843b0f8d2eSBrian Somers q = mp->inbufs; 5853b0f8d2eSBrian Somers } else { 5863b0f8d2eSBrian Somers /* Look for the next fragment */ 587147613eaSBrian Somers seq = inc_seq(mp->local_is12bit, seq); 5883b0f8d2eSBrian Somers last = q; 58926af0ae9SBrian Somers q = q->m_nextpkt; 5903b0f8d2eSBrian Somers } 5913b0f8d2eSBrian Somers } 5923b0f8d2eSBrian Somers 5933b0f8d2eSBrian Somers if (m) { 5943b0f8d2eSBrian Somers /* We still have to find a home for our new fragment */ 5953b0f8d2eSBrian Somers last = NULL; 59626af0ae9SBrian Somers for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) { 5973b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 5981bcced10SBrian Somers if (isbefore(mp->local_is12bit, mh.seq, h.seq)) 599147613eaSBrian Somers break; 600147613eaSBrian Somers } 601147613eaSBrian Somers /* Our received fragment fits in here */ 6023b0f8d2eSBrian Somers if (last) 60326af0ae9SBrian Somers last->m_nextpkt = m; 6043b0f8d2eSBrian Somers else 6053b0f8d2eSBrian Somers mp->inbufs = m; 60626af0ae9SBrian Somers m->m_nextpkt = q; 6073b0f8d2eSBrian Somers } 6083b0f8d2eSBrian Somers } 6093b0f8d2eSBrian Somers 6105d9e6103SBrian Somers struct mbuf * 6115d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 6125d9e6103SBrian Somers { 6135d9e6103SBrian Somers struct physical *p = link2physical(l); 6145d9e6103SBrian Somers 6155d9e6103SBrian Somers if (!bundle->ncp.mp.active) 6165d9e6103SBrian Somers /* Let someone else deal with it ! */ 6175d9e6103SBrian Somers return bp; 6185d9e6103SBrian Somers 6195d9e6103SBrian Somers if (p == NULL) { 6205d9e6103SBrian Somers log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n"); 62126af0ae9SBrian Somers m_freem(bp); 622411675baSBrian Somers } else { 62326af0ae9SBrian Somers m_settype(bp, MB_MPIN); 6245d9e6103SBrian Somers mp_Assemble(&bundle->ncp.mp, bp, p); 625411675baSBrian Somers } 6265d9e6103SBrian Somers 6275d9e6103SBrian Somers return NULL; 6285d9e6103SBrian Somers } 6295d9e6103SBrian Somers 6303b0f8d2eSBrian Somers static void 6315d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l, 6325d9e6103SBrian Somers struct mbuf *m, u_int32_t begin, u_int32_t end) 6333b0f8d2eSBrian Somers { 63426af0ae9SBrian Somers char prepend[4]; 6353b0f8d2eSBrian Somers 636ce828a6eSBrian Somers /* Stuff an MP header on the front of our packet and send it */ 63726af0ae9SBrian Somers 63849052c95SBrian Somers if (mp->peer_is12bit) { 6399e8ec64bSBrian Somers u_int16_t val; 640ce828a6eSBrian Somers 6419e8ec64bSBrian Somers val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq; 64226af0ae9SBrian Somers ua_htons(&val, prepend); 64326af0ae9SBrian Somers m = m_prepend(m, prepend, 2, 0); 6443b0f8d2eSBrian Somers } else { 6459e8ec64bSBrian Somers u_int32_t val; 646ce828a6eSBrian Somers 6479e8ec64bSBrian Somers val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq; 64826af0ae9SBrian Somers ua_htonl(&val, prepend); 64926af0ae9SBrian Somers m = m_prepend(m, prepend, 4, 0); 6503b0f8d2eSBrian Somers } 651dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 6529c53a7b1SBrian Somers log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n", 65326af0ae9SBrian Somers mp->out.seq, m_length(m), l->name); 654147613eaSBrian Somers mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq); 6553b0f8d2eSBrian Somers 6566301d506SBrian Somers if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) { 6576301d506SBrian Somers log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name); 6586301d506SBrian Somers return; 6596301d506SBrian Somers } 6606301d506SBrian Somers 66126af0ae9SBrian Somers link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP); 6623b0f8d2eSBrian Somers } 6633b0f8d2eSBrian Somers 6643b0f8d2eSBrian Somers int 66530949fd4SBrian Somers mp_FillPhysicalQueues(struct bundle *bundle) 6663b0f8d2eSBrian Somers { 6673b0f8d2eSBrian Somers struct mp *mp = &bundle->ncp.mp; 6689c53a7b1SBrian Somers struct datalink *dl, *fdl; 66926af0ae9SBrian Somers size_t total, add, len; 67026af0ae9SBrian Somers int thislink, nlinks; 671b4b280abSBrian Somers u_int32_t begin, end; 6723b0f8d2eSBrian Somers struct mbuf *m, *mo; 67330949fd4SBrian Somers struct link *bestlink; 6743b0f8d2eSBrian Somers 6759c53a7b1SBrian Somers thislink = nlinks = 0; 6769c53a7b1SBrian Somers for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) { 67786b1f0d7SBrian Somers /* Include non-open links here as mp->out.link will stay more correct */ 6789c53a7b1SBrian Somers if (!fdl) { 6799c53a7b1SBrian Somers if (thislink == mp->out.link) 6809c53a7b1SBrian Somers fdl = dl; 6819c53a7b1SBrian Somers else 6829c53a7b1SBrian Somers thislink++; 6839c53a7b1SBrian Somers } 6849c53a7b1SBrian Somers nlinks++; 6859c53a7b1SBrian Somers } 6869c53a7b1SBrian Somers 6879c53a7b1SBrian Somers if (!fdl) { 6889c53a7b1SBrian Somers fdl = bundle->links; 6891bc9b5baSBrian Somers if (!fdl) 6901bc9b5baSBrian Somers return 0; 6919c53a7b1SBrian Somers thislink = 0; 6929c53a7b1SBrian Somers } 6933b0f8d2eSBrian Somers 6943b0f8d2eSBrian Somers total = 0; 6959c53a7b1SBrian Somers for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) { 6969c53a7b1SBrian Somers if (!dl) { 6979c53a7b1SBrian Somers dl = bundle->links; 6989c53a7b1SBrian Somers thislink = 0; 6999c53a7b1SBrian Somers } 7009c53a7b1SBrian Somers 7016f384573SBrian Somers if (dl->state != DATALINK_OPEN) 7026f384573SBrian Somers continue; 7039c53a7b1SBrian Somers 7043b0f8d2eSBrian Somers if (dl->physical->out) 7053b0f8d2eSBrian Somers /* this link has suffered a short write. Let it continue */ 7063b0f8d2eSBrian Somers continue; 7079c53a7b1SBrian Somers 7083b0f8d2eSBrian Somers add = link_QueueLen(&dl->physical->link); 7093fd1e17cSBrian Somers if (add) { 7103b0f8d2eSBrian Somers /* this link has got stuff already queued. Let it continue */ 7113fd1e17cSBrian Somers total += add; 7123b0f8d2eSBrian Somers continue; 7133fd1e17cSBrian Somers } 7149c53a7b1SBrian Somers 71530949fd4SBrian Somers if (!mp_QueueLen(mp)) { 7163fd1e17cSBrian Somers struct datalink *other; 7173fd1e17cSBrian Somers int mrutoosmall; 7183fd1e17cSBrian Somers 7193fd1e17cSBrian Somers /* 7203fd1e17cSBrian Somers * If there's only a single open link in our bundle and we haven't got 7213fd1e17cSBrian Somers * MP level link compression, queue outbound traffic directly via that 7223fd1e17cSBrian Somers * link's protocol stack rather than using the MP link. This results 72330949fd4SBrian Somers * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather 72430949fd4SBrian Somers * than PROTO_MP. 7253fd1e17cSBrian Somers */ 7263fd1e17cSBrian Somers for (other = dl->next; other; other = other->next) 7273fd1e17cSBrian Somers if (other->state == DATALINK_OPEN) 7283fd1e17cSBrian Somers break; 7293fd1e17cSBrian Somers 7303fd1e17cSBrian Somers mrutoosmall = 0; 7313fd1e17cSBrian Somers if (!other) { 7323fd1e17cSBrian Somers if (dl->physical->link.lcp.his_mru < mp->peer_mrru) { 7333fd1e17cSBrian Somers /* 7343fd1e17cSBrian Somers * Actually, forget it. This test is done against the MRRU rather 7353fd1e17cSBrian Somers * than the packet size so that we don't end up sending some data 7363fd1e17cSBrian Somers * in MP fragments and some data in PROTO_IP packets. That's just 7373fd1e17cSBrian Somers * too likely to upset some ppp implementations. 7383fd1e17cSBrian Somers */ 7393fd1e17cSBrian Somers mrutoosmall = 1; 7403fd1e17cSBrian Somers other = dl; 7413fd1e17cSBrian Somers } 7423fd1e17cSBrian Somers } 7433fd1e17cSBrian Somers 74430949fd4SBrian Somers bestlink = other ? &mp->link : &dl->physical->link; 74530949fd4SBrian Somers if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink)) 74630949fd4SBrian Somers break; /* Nothing else to send */ 7473b0f8d2eSBrian Somers 7483fd1e17cSBrian Somers if (mrutoosmall) 7493fd1e17cSBrian Somers log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n"); 7503fd1e17cSBrian Somers else if (!other) 7513fd1e17cSBrian Somers log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n"); 7523fd1e17cSBrian Somers 7533fd1e17cSBrian Somers if (!other) { 7543fd1e17cSBrian Somers add = link_QueueLen(&dl->physical->link); 7553fd1e17cSBrian Somers if (add) { 7563fd1e17cSBrian Somers /* this link has got stuff already queued. Let it continue */ 7573fd1e17cSBrian Somers total += add; 7583fd1e17cSBrian Somers continue; 7593fd1e17cSBrian Somers } 7603fd1e17cSBrian Somers } 7613fd1e17cSBrian Somers } 7623fd1e17cSBrian Somers 7633b0f8d2eSBrian Somers m = link_Dequeue(&mp->link); 7643fd1e17cSBrian Somers if (m) { 76526af0ae9SBrian Somers len = m_length(m); 7663b0f8d2eSBrian Somers begin = 1; 7673b0f8d2eSBrian Somers end = 0; 7683b0f8d2eSBrian Somers 7699c53a7b1SBrian Somers while (!end) { 7709c53a7b1SBrian Somers if (dl->state == DATALINK_OPEN) { 771ab2de065SBrian Somers /* Write at most his_mru bytes to the physical link */ 772ab2de065SBrian Somers if (len <= dl->physical->link.lcp.his_mru) { 7733b0f8d2eSBrian Somers mo = m; 7743b0f8d2eSBrian Somers end = 1; 77526af0ae9SBrian Somers m_settype(mo, MB_MPOUT); 7763b0f8d2eSBrian Somers } else { 777ab2de065SBrian Somers /* It's > his_mru, chop the packet (`m') into bits */ 77826af0ae9SBrian Somers mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT); 77926af0ae9SBrian Somers len -= mo->m_len; 78026af0ae9SBrian Somers m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len); 7813b0f8d2eSBrian Somers } 7825d9e6103SBrian Somers mp_Output(mp, bundle, &dl->physical->link, mo, begin, end); 7833b0f8d2eSBrian Somers begin = 0; 7843b0f8d2eSBrian Somers } 7859c53a7b1SBrian Somers 7869c53a7b1SBrian Somers if (!end) { 7879c53a7b1SBrian Somers nlinks--; 7889c53a7b1SBrian Somers dl = dl->next; 7899c53a7b1SBrian Somers if (!dl) { 7909c53a7b1SBrian Somers dl = bundle->links; 7919c53a7b1SBrian Somers thislink = 0; 7929c53a7b1SBrian Somers } else 7939c53a7b1SBrian Somers thislink++; 7943b0f8d2eSBrian Somers } 7959c53a7b1SBrian Somers } 7969c53a7b1SBrian Somers } 7973fd1e17cSBrian Somers } 7989c53a7b1SBrian Somers mp->out.link = thislink; /* Start here next time */ 7993b0f8d2eSBrian Somers 8003b0f8d2eSBrian Somers return total; 8013b0f8d2eSBrian Somers } 8023b0f8d2eSBrian Somers 8033b0f8d2eSBrian Somers int 804ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg) 8053b0f8d2eSBrian Somers { 8063b0f8d2eSBrian Somers int val; 8073b0f8d2eSBrian Somers 80825092092SBrian Somers if (arg->argc != arg->argn+1) 8093b0f8d2eSBrian Somers return -1; 8103b0f8d2eSBrian Somers 81125092092SBrian Somers val = atoi(arg->argv[arg->argn]); 812ab2de065SBrian Somers if (val <= 0) { 813ab2de065SBrian Somers log_Printf(LogWARN, "The link bandwidth must be greater than zero\n"); 8143b0f8d2eSBrian Somers return 1; 8153b0f8d2eSBrian Somers } 816ab2de065SBrian Somers arg->cx->mp.bandwidth = val; 817ab2de065SBrian Somers 818ab2de065SBrian Somers if (arg->cx->state == DATALINK_OPEN) 819ab2de065SBrian Somers bundle_CalculateBandwidth(arg->bundle); 820ab2de065SBrian Somers 8213b0f8d2eSBrian Somers return 0; 8223b0f8d2eSBrian Somers } 82349052c95SBrian Somers 82449052c95SBrian Somers int 82549052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg) 82649052c95SBrian Somers { 82749052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 82849052c95SBrian Somers 82949052c95SBrian Somers prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 8309c53a7b1SBrian Somers if (mp->active) { 831411675baSBrian Somers struct mbuf *m, *lm; 8329c53a7b1SBrian Somers int bufs = 0; 8339c53a7b1SBrian Somers 834fa0d5216SBrian Somers lm = NULL; 8351fa665f5SBrian Somers prompt_Printf(arg->prompt, "Socket: %s\n", 8366f384573SBrian Somers mp->server.socket.sun_path); 83726af0ae9SBrian Somers for (m = mp->inbufs; m; m = m->m_nextpkt) { 8389c53a7b1SBrian Somers bufs++; 839411675baSBrian Somers lm = m; 840411675baSBrian Somers } 841411675baSBrian Somers prompt_Printf(arg->prompt, "Pending frags: %d", bufs); 842411675baSBrian Somers if (bufs) { 843411675baSBrian Somers struct mp_header mh; 844411675baSBrian Somers unsigned long first, last; 845411675baSBrian Somers 846411675baSBrian Somers first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0; 847411675baSBrian Somers last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0; 84864e0f466SBrian Somers prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n", 849411675baSBrian Somers first, last, (unsigned long)mp->seq.next_in, 850411675baSBrian Somers (unsigned long)mp->seq.min_in); 85164e0f466SBrian Somers prompt_Printf(arg->prompt, " First has %sbegin bit and " 85264e0f466SBrian Somers "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no "); 853411675baSBrian Somers } 854411675baSBrian Somers prompt_Printf(arg->prompt, "\n"); 8559c53a7b1SBrian Somers } 85649052c95SBrian Somers 85749052c95SBrian Somers prompt_Printf(arg->prompt, "\nMy Side:\n"); 85849052c95SBrian Somers if (mp->active) { 859ab2de065SBrian Somers prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq); 86049052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 86149052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 86249052c95SBrian Somers mp->local_is12bit ? "on" : "off"); 86349052c95SBrian Somers } 864643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 86549052c95SBrian Somers mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 86649052c95SBrian Somers mp->cfg.enddisc.len)); 86749052c95SBrian Somers 86849052c95SBrian Somers prompt_Printf(arg->prompt, "\nHis Side:\n"); 86949052c95SBrian Somers if (mp->active) { 870643f4904SBrian Somers prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 871ab2de065SBrian Somers prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in); 87249052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 87349052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 87449052c95SBrian Somers mp->peer_is12bit ? "on" : "off"); 87549052c95SBrian Somers } 876643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 877643f4904SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 878643f4904SBrian Somers mp->peer.enddisc.len)); 87949052c95SBrian Somers 88049052c95SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 88149052c95SBrian Somers 88249052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: "); 88349052c95SBrian Somers if (mp->cfg.mrru) 88449052c95SBrian Somers prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 88549052c95SBrian Somers else 88649052c95SBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 88749052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 88849052c95SBrian Somers command_ShowNegval(mp->cfg.shortseq)); 8897063995cSBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 8907063995cSBrian Somers command_ShowNegval(mp->cfg.negenddisc)); 891ab2de065SBrian Somers prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%," 892ab2de065SBrian Somers " period %d secs\n", mp->cfg.autoload.min, 893ab2de065SBrian Somers mp->cfg.autoload.max, mp->cfg.autoload.period); 89449052c95SBrian Somers 89549052c95SBrian Somers return 0; 89649052c95SBrian Somers } 89749052c95SBrian Somers 89849052c95SBrian Somers const char * 89949052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len) 90049052c95SBrian Somers { 901d93d3a9cSBrian Somers static char result[100]; /* Used immediately after it's returned */ 90249052c95SBrian Somers int f, header; 90349052c95SBrian Somers 90449052c95SBrian Somers switch (c) { 90508676022SBrian Somers case ENDDISC_NULL: 90649052c95SBrian Somers sprintf(result, "Null Class"); 90749052c95SBrian Somers break; 90849052c95SBrian Somers 90908676022SBrian Somers case ENDDISC_LOCAL: 91049052c95SBrian Somers snprintf(result, sizeof result, "Local Addr: %.*s", len, address); 91149052c95SBrian Somers break; 91249052c95SBrian Somers 91308676022SBrian Somers case ENDDISC_IP: 91449052c95SBrian Somers if (len == 4) 91549052c95SBrian Somers snprintf(result, sizeof result, "IP %s", 91649052c95SBrian Somers inet_ntoa(*(const struct in_addr *)address)); 91749052c95SBrian Somers else 91849052c95SBrian Somers sprintf(result, "IP[%d] ???", len); 91949052c95SBrian Somers break; 92049052c95SBrian Somers 92108676022SBrian Somers case ENDDISC_MAC: 92249052c95SBrian Somers if (len == 6) { 92349052c95SBrian Somers const u_char *m = (const u_char *)address; 92449052c95SBrian Somers snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 92549052c95SBrian Somers m[0], m[1], m[2], m[3], m[4], m[5]); 92649052c95SBrian Somers } else 92749052c95SBrian Somers sprintf(result, "MAC[%d] ???", len); 92849052c95SBrian Somers break; 92949052c95SBrian Somers 93008676022SBrian Somers case ENDDISC_MAGIC: 93149052c95SBrian Somers sprintf(result, "Magic: 0x"); 93249052c95SBrian Somers header = strlen(result); 93349052c95SBrian Somers if (len > sizeof result - header - 1) 93449052c95SBrian Somers len = sizeof result - header - 1; 93549052c95SBrian Somers for (f = 0; f < len; f++) 93649052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 93749052c95SBrian Somers break; 93849052c95SBrian Somers 93908676022SBrian Somers case ENDDISC_PSN: 94049052c95SBrian Somers snprintf(result, sizeof result, "PSN: %.*s", len, address); 94149052c95SBrian Somers break; 94249052c95SBrian Somers 94349052c95SBrian Somers default: 94449052c95SBrian Somers sprintf(result, "%d: ", (int)c); 94549052c95SBrian Somers header = strlen(result); 94649052c95SBrian Somers if (len > sizeof result - header - 1) 94749052c95SBrian Somers len = sizeof result - header - 1; 94849052c95SBrian Somers for (f = 0; f < len; f++) 94949052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 95049052c95SBrian Somers break; 95149052c95SBrian Somers } 95249052c95SBrian Somers return result; 95349052c95SBrian Somers } 95449052c95SBrian Somers 95549052c95SBrian Somers int 95649052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg) 95749052c95SBrian Somers { 95849052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 959d47dceb8SBrian Somers struct in_addr addr; 96049052c95SBrian Somers 9619b5f8ffdSBrian Somers switch (bundle_Phase(arg->bundle)) { 9629b5f8ffdSBrian Somers case PHASE_DEAD: 9639b5f8ffdSBrian Somers break; 9649b5f8ffdSBrian Somers case PHASE_ESTABLISH: 9659b5f8ffdSBrian Somers /* Make sure none of our links are DATALINK_LCP or greater */ 9669b5f8ffdSBrian Somers if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) { 9679b5f8ffdSBrian Somers log_Printf(LogWARN, "enddisc: Only changable before" 9689b5f8ffdSBrian Somers " LCP negotiations\n"); 9699b5f8ffdSBrian Somers return 1; 9709b5f8ffdSBrian Somers } 9719b5f8ffdSBrian Somers break; 9729b5f8ffdSBrian Somers default: 9739b5f8ffdSBrian Somers log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n"); 97449052c95SBrian Somers return 1; 97549052c95SBrian Somers } 97649052c95SBrian Somers 97749052c95SBrian Somers if (arg->argc == arg->argn) { 97849052c95SBrian Somers mp->cfg.enddisc.class = 0; 97949052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 98049052c95SBrian Somers mp->cfg.enddisc.len = 0; 981e43ebac1SBrian Somers } else if (arg->argc > arg->argn) { 98208676022SBrian Somers if (!strcasecmp(arg->argv[arg->argn], "label")) { 98308676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_LOCAL; 98408676022SBrian Somers strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 98508676022SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 98608676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 987259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 98830949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr); 989d47dceb8SBrian Somers else 990259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 991d47dceb8SBrian Somers memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 99208676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_IP; 99349052c95SBrian Somers mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 99408676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 99508676022SBrian Somers struct sockaddr_dl hwaddr; 99608676022SBrian Somers int s; 99708676022SBrian Somers 998259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 99930949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr); 1000d47dceb8SBrian Somers else 1001259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 1002d47dceb8SBrian Somers 100308676022SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 100408676022SBrian Somers if (s < 0) { 1005dd7e2610SBrian Somers log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno)); 100608676022SBrian Somers return 2; 100708676022SBrian Somers } 100865cacad4SBrian Somers if (arp_EtherAddr(s, addr, &hwaddr, 1)) { 100908676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAC; 101008676022SBrian Somers memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 101108676022SBrian Somers hwaddr.sdl_alen); 101208676022SBrian Somers mp->cfg.enddisc.len = hwaddr.sdl_alen; 101308676022SBrian Somers } else { 1014dd7e2610SBrian Somers log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 1015259a6191SBrian Somers inet_ntoa(addr)); 101608676022SBrian Somers close(s); 101708676022SBrian Somers return 4; 101808676022SBrian Somers } 101908676022SBrian Somers close(s); 102049052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 102149052c95SBrian Somers int f; 102249052c95SBrian Somers 102349052c95SBrian Somers randinit(); 102449052c95SBrian Somers for (f = 0; f < 20; f += sizeof(long)) 102549052c95SBrian Somers *(long *)(mp->cfg.enddisc.address + f) = random(); 102608676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAGIC; 102749052c95SBrian Somers mp->cfg.enddisc.len = 20; 102849052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 102949052c95SBrian Somers if (arg->argc > arg->argn+1) { 103008676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_PSN; 103149052c95SBrian Somers strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 103249052c95SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 103349052c95SBrian Somers } else { 1034dd7e2610SBrian Somers log_Printf(LogWARN, "PSN endpoint requires additional data\n"); 103508676022SBrian Somers return 5; 103649052c95SBrian Somers } 103749052c95SBrian Somers } else { 1038dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Unrecognised endpoint type\n", 103949052c95SBrian Somers arg->argv[arg->argn]); 104008676022SBrian Somers return 6; 104149052c95SBrian Somers } 1042e43ebac1SBrian Somers } 104349052c95SBrian Somers 104449052c95SBrian Somers return 0; 104549052c95SBrian Somers } 10461fa665f5SBrian Somers 10471fa665f5SBrian Somers static int 1048f013f33eSBrian Somers mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 10491fa665f5SBrian Somers int *n) 10501fa665f5SBrian Somers { 10511fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10520f2f3eb3SBrian Somers int result; 10531fa665f5SBrian Somers 10540f2f3eb3SBrian Somers result = 0; 10556f384573SBrian Somers if (s->send.dl != NULL) { 105696c9bb21SBrian Somers /* We've connect()ed */ 1057b7c5748eSBrian Somers if (!link_QueueLen(&s->send.dl->physical->link) && 1058b7c5748eSBrian Somers !s->send.dl->physical->out) { 1059b7c5748eSBrian Somers /* Only send if we've transmitted all our data (i.e. the ConfigAck) */ 10600f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, w, e); 106196c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 10626f384573SBrian Somers s->send.dl = NULL; 106396c9bb21SBrian Somers s->fd = -1; 1064ea722969SBrian Somers } else 1065ea722969SBrian Somers /* Never read from a datalink that's on death row ! */ 10660f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL); 106796c9bb21SBrian Somers } else if (r && s->fd >= 0) { 10681fa665f5SBrian Somers if (*n < s->fd + 1) 10691fa665f5SBrian Somers *n = s->fd + 1; 10701fa665f5SBrian Somers FD_SET(s->fd, r); 107124989c68SBrian Somers log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd); 10720f2f3eb3SBrian Somers result++; 10731fa665f5SBrian Somers } 10740f2f3eb3SBrian Somers return result; 10751fa665f5SBrian Somers } 10761fa665f5SBrian Somers 10771fa665f5SBrian Somers static int 1078f013f33eSBrian Somers mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset) 10791fa665f5SBrian Somers { 10801fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10811fa665f5SBrian Somers return s->fd >= 0 && FD_ISSET(s->fd, fdset); 10821fa665f5SBrian Somers } 10831fa665f5SBrian Somers 10841fa665f5SBrian Somers static void 1085f013f33eSBrian Somers mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 10861fa665f5SBrian Somers { 10871fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10881fa665f5SBrian Somers 10892cb305afSBrian Somers bundle_ReceiveDatalink(bundle, s->fd); 10901fa665f5SBrian Somers } 10911fa665f5SBrian Somers 10921af29a6eSBrian Somers static int 10939b996792SBrian Somers mpserver_Write(struct fdescriptor *d, struct bundle *bundle, 10949b996792SBrian Somers const fd_set *fdset) 10951fa665f5SBrian Somers { 10961fa665f5SBrian Somers /* We never want to write here ! */ 1097a33b2ef7SBrian Somers log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n"); 10981af29a6eSBrian Somers return 0; 10991fa665f5SBrian Somers } 11001fa665f5SBrian Somers 11011fa665f5SBrian Somers void 11021fa665f5SBrian Somers mpserver_Init(struct mpserver *s) 11031fa665f5SBrian Somers { 11041fa665f5SBrian Somers s->desc.type = MPSERVER_DESCRIPTOR; 11051fa665f5SBrian Somers s->desc.UpdateSet = mpserver_UpdateSet; 11061fa665f5SBrian Somers s->desc.IsSet = mpserver_IsSet; 11071fa665f5SBrian Somers s->desc.Read = mpserver_Read; 11081fa665f5SBrian Somers s->desc.Write = mpserver_Write; 11096f384573SBrian Somers s->send.dl = NULL; 11101fa665f5SBrian Somers s->fd = -1; 11116f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 11121fa665f5SBrian Somers } 11131fa665f5SBrian Somers 11141fa665f5SBrian Somers int 11151fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer) 11161fa665f5SBrian Somers { 1117cbee9754SBrian Somers int f, l; 11181fa665f5SBrian Somers mode_t mask; 11191fa665f5SBrian Somers 11201fa665f5SBrian Somers if (s->fd != -1) { 1121a33b2ef7SBrian Somers log_Printf(LogALERT, "Internal error ! mpserver already open\n"); 11226f384573SBrian Somers mpserver_Close(s); 11231fa665f5SBrian Somers } 11241fa665f5SBrian Somers 11256f384573SBrian Somers l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-", 11266f384573SBrian Somers _PATH_VARRUN, peer->authname, peer->enddisc.class); 11271433aa5dSBrian Somers if (l < 0) { 11281433aa5dSBrian Somers log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno)); 11291433aa5dSBrian Somers return MPSERVER_FAILED; 11301433aa5dSBrian Somers } 11311fa665f5SBrian Somers 11326f384573SBrian Somers for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) { 11336f384573SBrian Somers snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l, 11341fa665f5SBrian Somers "%02x", *(u_char *)(peer->enddisc.address+f)); 11356f384573SBrian Somers l += 2; 11361fa665f5SBrian Somers } 11371fa665f5SBrian Somers 11386f384573SBrian Somers s->socket.sun_family = AF_LOCAL; 11396f384573SBrian Somers s->socket.sun_len = sizeof s->socket; 11402cb305afSBrian Somers s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0); 11411fa665f5SBrian Somers if (s->fd < 0) { 11422cb305afSBrian Somers log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno)); 114396c9bb21SBrian Somers return MPSERVER_FAILED; 11441fa665f5SBrian Somers } 11451fa665f5SBrian Somers 11466f384573SBrian Somers setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket, 11476f384573SBrian Somers sizeof s->socket); 11481fa665f5SBrian Somers mask = umask(0177); 11492cb305afSBrian Somers 11502cb305afSBrian Somers /* 1151cbee9754SBrian Somers * Try to bind the socket. If we succeed we play server, if we fail 1152cbee9754SBrian Somers * we connect() and hand the link off. 11532cb305afSBrian Somers */ 11542cb305afSBrian Somers 11556f384573SBrian Somers if (ID0bind_un(s->fd, &s->socket) < 0) { 11566f384573SBrian Somers if (errno != EADDRINUSE) { 1157dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n", 11586f384573SBrian Somers s->socket.sun_path, strerror(errno)); 11591fa665f5SBrian Somers umask(mask); 11601fa665f5SBrian Somers close(s->fd); 11611fa665f5SBrian Somers s->fd = -1; 116296c9bb21SBrian Somers return MPSERVER_FAILED; 11636f384573SBrian Somers } 11642cb305afSBrian Somers 1165cbee9754SBrian Somers /* So we're the sender */ 11666f384573SBrian Somers umask(mask); 11676f384573SBrian Somers if (ID0connect_un(s->fd, &s->socket) < 0) { 1168dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n", 11696f384573SBrian Somers s->socket.sun_path, strerror(errno)); 11706f384573SBrian Somers if (errno == ECONNREFUSED) 11712cb305afSBrian Somers log_Printf(LogPHASE, " The previous server died badly !\n"); 11726f384573SBrian Somers close(s->fd); 11736f384573SBrian Somers s->fd = -1; 117496c9bb21SBrian Somers return MPSERVER_FAILED; 11756f384573SBrian Somers } 11766f384573SBrian Somers 11776f384573SBrian Somers /* Donate our link to the other guy */ 117896c9bb21SBrian Somers return MPSERVER_CONNECTED; 11791fa665f5SBrian Somers } 11806f384573SBrian Somers 118196c9bb21SBrian Somers return MPSERVER_LISTENING; 11821fa665f5SBrian Somers } 11831fa665f5SBrian Somers 11841fa665f5SBrian Somers void 11851fa665f5SBrian Somers mpserver_Close(struct mpserver *s) 11861fa665f5SBrian Somers { 11876f384573SBrian Somers if (s->send.dl != NULL) { 118896c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 11896f384573SBrian Somers s->send.dl = NULL; 119096c9bb21SBrian Somers s->fd = -1; 119196c9bb21SBrian Somers } else if (s->fd >= 0) { 11921fa665f5SBrian Somers close(s->fd); 11936f384573SBrian Somers if (ID0unlink(s->socket.sun_path) == -1) 1194dd7e2610SBrian Somers log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path, 11956f384573SBrian Somers strerror(errno)); 11966f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 11971fa665f5SBrian Somers s->fd = -1; 11981fa665f5SBrian Somers } 11991fa665f5SBrian Somers } 120086b1f0d7SBrian Somers 120186b1f0d7SBrian Somers void 120286b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl) 120386b1f0d7SBrian Somers { 120486b1f0d7SBrian Somers if (mp->seq.min_in == dl->mp.seq) 120586b1f0d7SBrian Somers /* We've lost the link that's holding everything up ! */ 12065d9e6103SBrian Somers mp_Assemble(mp, NULL, NULL); 120786b1f0d7SBrian Somers } 12086f8e9f0aSBrian Somers 120930949fd4SBrian Somers size_t 121030949fd4SBrian Somers mp_QueueLen(struct mp *mp) 12116f8e9f0aSBrian Somers { 121230949fd4SBrian Somers return link_QueueLen(&mp->link); 12136f8e9f0aSBrian Somers } 1214