13b0f8d2eSBrian Somers /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 31de7b4b8SPedro F. Giffuni * 43b0f8d2eSBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 53b0f8d2eSBrian Somers * All rights reserved. 63b0f8d2eSBrian Somers * 73b0f8d2eSBrian Somers * Redistribution and use in source and binary forms, with or without 83b0f8d2eSBrian Somers * modification, are permitted provided that the following conditions 93b0f8d2eSBrian Somers * are met: 103b0f8d2eSBrian Somers * 1. Redistributions of source code must retain the above copyright 113b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer. 123b0f8d2eSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 133b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer in the 143b0f8d2eSBrian Somers * documentation and/or other materials provided with the distribution. 153b0f8d2eSBrian Somers * 163b0f8d2eSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 173b0f8d2eSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 183b0f8d2eSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 193b0f8d2eSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 203b0f8d2eSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 213b0f8d2eSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 223b0f8d2eSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 233b0f8d2eSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 243b0f8d2eSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 253b0f8d2eSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 263b0f8d2eSBrian Somers * SUCH DAMAGE. 273b0f8d2eSBrian Somers * 2897d92980SPeter Wemm * $FreeBSD$ 293b0f8d2eSBrian Somers */ 303b0f8d2eSBrian Somers 31972a1bcfSBrian Somers #include <sys/param.h> 323b0f8d2eSBrian Somers #include <netinet/in.h> 333b0f8d2eSBrian Somers #include <netinet/in_systm.h> 343b0f8d2eSBrian Somers #include <netinet/ip.h> 3549052c95SBrian Somers #include <arpa/inet.h> 3608676022SBrian Somers #include <net/if_dl.h> 3708676022SBrian Somers #include <sys/socket.h> 381fa665f5SBrian Somers #include <sys/un.h> 393b0f8d2eSBrian Somers 4008676022SBrian Somers #include <errno.h> 411fa665f5SBrian Somers #include <paths.h> 426eafd353SBrian Somers #include <stdarg.h> 4349052c95SBrian Somers #include <stdio.h> 446eafd353SBrian Somers #include <stdlib.h> 453b0f8d2eSBrian Somers #include <string.h> 461fa665f5SBrian Somers #include <sys/stat.h> 473b0f8d2eSBrian Somers #include <termios.h> 4808676022SBrian Somers #include <unistd.h> 493b0f8d2eSBrian Somers 505d9e6103SBrian Somers #include "layer.h" 5167b072f7SBrian Somers #ifndef NONAT 52f02c2029SBrian Somers #include "nat_cmd.h" 535d9e6103SBrian Somers #endif 545d9e6103SBrian Somers #include "vjcomp.h" 559e8ec64bSBrian Somers #include "ua.h" 56c9e11a11SBrian Somers #include "defs.h" 573b0f8d2eSBrian Somers #include "command.h" 583b0f8d2eSBrian Somers #include "mbuf.h" 593b0f8d2eSBrian Somers #include "log.h" 603b0f8d2eSBrian Somers #include "timer.h" 613b0f8d2eSBrian Somers #include "fsm.h" 623b0f8d2eSBrian Somers #include "iplist.h" 633b0f8d2eSBrian Somers #include "throughput.h" 643b0f8d2eSBrian Somers #include "slcompress.h" 655a72b6edSBrian Somers #include "lqr.h" 665a72b6edSBrian Somers #include "hdlc.h" 6730949fd4SBrian Somers #include "ncpaddr.h" 683b0f8d2eSBrian Somers #include "ipcp.h" 693b0f8d2eSBrian Somers #include "auth.h" 703b0f8d2eSBrian Somers #include "lcp.h" 713b0f8d2eSBrian Somers #include "async.h" 723b0f8d2eSBrian Somers #include "ccp.h" 733b0f8d2eSBrian Somers #include "link.h" 743b0f8d2eSBrian Somers #include "descriptor.h" 753b0f8d2eSBrian Somers #include "physical.h" 763b0f8d2eSBrian Somers #include "chat.h" 775d9e6103SBrian Somers #include "proto.h" 783b0f8d2eSBrian Somers #include "filter.h" 793b0f8d2eSBrian Somers #include "mp.h" 803b0f8d2eSBrian Somers #include "chap.h" 8192b09558SBrian Somers #include "cbcp.h" 823b0f8d2eSBrian Somers #include "datalink.h" 83972a1bcfSBrian Somers #ifndef NORADIUS 84972a1bcfSBrian Somers #include "radius.h" 85972a1bcfSBrian Somers #endif 8630949fd4SBrian Somers #include "ipv6cp.h" 8730949fd4SBrian Somers #include "ncp.h" 883b0f8d2eSBrian Somers #include "bundle.h" 8949052c95SBrian Somers #include "prompt.h" 9008676022SBrian Somers #include "id.h" 9108676022SBrian Somers #include "arp.h" 923b0f8d2eSBrian Somers 93643f4904SBrian Somers void 94643f4904SBrian Somers peerid_Init(struct peerid *peer) 95643f4904SBrian Somers { 96643f4904SBrian Somers peer->enddisc.class = 0; 97643f4904SBrian Somers *peer->enddisc.address = '\0'; 98643f4904SBrian Somers peer->enddisc.len = 0; 99643f4904SBrian Somers *peer->authname = '\0'; 100643f4904SBrian Somers } 101643f4904SBrian Somers 102643f4904SBrian Somers int 103643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2) 104643f4904SBrian Somers { 105643f4904SBrian Somers return !strcmp(p1->authname, p2->authname) && 106643f4904SBrian Somers p1->enddisc.class == p2->enddisc.class && 107643f4904SBrian Somers p1->enddisc.len == p2->enddisc.len && 108643f4904SBrian Somers !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 109643f4904SBrian Somers } 110643f4904SBrian Somers 1113b0f8d2eSBrian Somers static u_int32_t 112147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq) 1133b0f8d2eSBrian Somers { 1143b0f8d2eSBrian Somers seq++; 115147613eaSBrian Somers if (is12bit) { 1163b0f8d2eSBrian Somers if (seq & 0xfffff000) 1173b0f8d2eSBrian Somers seq = 0; 1183b0f8d2eSBrian Somers } else if (seq & 0xff000000) 1193b0f8d2eSBrian Somers seq = 0; 1203b0f8d2eSBrian Somers return seq; 1213b0f8d2eSBrian Somers } 1223b0f8d2eSBrian Somers 1233b0f8d2eSBrian Somers static int 1241bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2) 1251bcced10SBrian Somers { 126b4b280abSBrian Somers u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200; 127b4b280abSBrian Somers 128b4b280abSBrian Somers if (seq1 > max) { 129b4b280abSBrian Somers if (seq2 < 0x200 || seq2 > seq1) 130b4b280abSBrian Somers return 1; 131b4b280abSBrian Somers } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2) 132b4b280abSBrian Somers return 1; 133b4b280abSBrian Somers 134b4b280abSBrian Somers return 0; 1351bcced10SBrian Somers } 1361bcced10SBrian Somers 1371bcced10SBrian Somers static int 1383b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 1393b0f8d2eSBrian Somers { 14049052c95SBrian Somers if (mp->local_is12bit) { 1419e8ec64bSBrian Somers u_int16_t val; 1429e8ec64bSBrian Somers 1439e8ec64bSBrian Somers ua_ntohs(MBUF_CTOP(m), &val); 1449e8ec64bSBrian Somers if (val & 0x3000) { 145dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1463b0f8d2eSBrian Somers return 0; 1473b0f8d2eSBrian Somers } 1489e8ec64bSBrian Somers header->begin = val & 0x8000 ? 1 : 0; 1499e8ec64bSBrian Somers header->end = val & 0x4000 ? 1 : 0; 1509e8ec64bSBrian Somers header->seq = val & 0x0fff; 1513b0f8d2eSBrian Somers return 2; 1523b0f8d2eSBrian Somers } else { 1539e8ec64bSBrian Somers ua_ntohl(MBUF_CTOP(m), &header->seq); 1543b0f8d2eSBrian Somers if (header->seq & 0x3f000000) { 155dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n"); 1563b0f8d2eSBrian Somers return 0; 1573b0f8d2eSBrian Somers } 158ce828a6eSBrian Somers header->begin = header->seq & 0x80000000 ? 1 : 0; 159ce828a6eSBrian Somers header->end = header->seq & 0x40000000 ? 1 : 0; 1603b0f8d2eSBrian Somers header->seq &= 0x00ffffff; 1613b0f8d2eSBrian Somers return 4; 1623b0f8d2eSBrian Somers } 1633b0f8d2eSBrian Somers } 1643b0f8d2eSBrian Somers 1653b0f8d2eSBrian Somers static void 166057f1760SBrian Somers mp_LayerStart(void *v __unused, struct fsm *fp __unused) 1673b0f8d2eSBrian Somers { 16849052c95SBrian Somers /* The given FSM (ccp) is about to start up ! */ 1693b0f8d2eSBrian Somers } 1703b0f8d2eSBrian Somers 1713b0f8d2eSBrian Somers static void 172057f1760SBrian Somers mp_LayerUp(void *v __unused, struct fsm *fp) 1733b0f8d2eSBrian Somers { 17449052c95SBrian Somers /* The given fsm (ccp) is now up */ 1756301d506SBrian Somers 1766301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 1773b0f8d2eSBrian Somers } 1783b0f8d2eSBrian Somers 1793b0f8d2eSBrian Somers static void 180057f1760SBrian Somers mp_LayerDown(void *v __unused, struct fsm *fp __unused) 1813b0f8d2eSBrian Somers { 18249052c95SBrian Somers /* The given FSM (ccp) has been told to come down */ 1833b0f8d2eSBrian Somers } 1843b0f8d2eSBrian Somers 1853b0f8d2eSBrian Somers static void 186057f1760SBrian Somers mp_LayerFinish(void *v __unused, struct fsm *fp) 1873b0f8d2eSBrian Somers { 18849052c95SBrian Somers /* The given fsm (ccp) is now down */ 1890a1b5c9dSBrian Somers if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE) 1900a1b5c9dSBrian Somers fsm_Open(fp); /* CCP goes to ST_STOPPED */ 1913b0f8d2eSBrian Somers } 1923b0f8d2eSBrian Somers 193ab2de065SBrian Somers static void 194ab2de065SBrian Somers mp_UpDown(void *v) 195ab2de065SBrian Somers { 196ab2de065SBrian Somers struct mp *mp = (struct mp *)v; 197ab2de065SBrian Somers int percent; 198ab2de065SBrian Somers 19991cbd2eeSBrian Somers percent = MAX(mp->link.stats.total.in.OctetsPerSecond, 20091cbd2eeSBrian Somers mp->link.stats.total.out.OctetsPerSecond) * 800 / 20191cbd2eeSBrian Somers mp->bundle->bandwidth; 202ab2de065SBrian Somers if (percent >= mp->cfg.autoload.max) { 203ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent); 204ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_UP); 205ab2de065SBrian Somers } else if (percent <= mp->cfg.autoload.min) { 206ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent); 207ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN); 208ab2de065SBrian Somers } 209ab2de065SBrian Somers } 210ab2de065SBrian Somers 211ab2de065SBrian Somers void 212ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp) 213ab2de065SBrian Somers { 21411572abfSBrian Somers throughput_stop(&mp->link.stats.total); 215ab2de065SBrian Somers } 216ab2de065SBrian Somers 217ab2de065SBrian Somers void 218ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp) 219ab2de065SBrian Somers { 22011572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) { 22111572abfSBrian Somers throughput_destroy(&mp->link.stats.total); 22211572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 22311572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 224ab2de065SBrian Somers } 225ab2de065SBrian Somers 226ab2de065SBrian Somers if (bundle_WantAutoloadTimer(mp->bundle)) 22711572abfSBrian Somers throughput_start(&mp->link.stats.total, "MP throughput", 1); 228ab2de065SBrian Somers else 229ab2de065SBrian Somers mp_StopAutoloadTimer(mp); 230ab2de065SBrian Somers } 231ab2de065SBrian Somers 232ab2de065SBrian Somers void 233ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp) 234ab2de065SBrian Somers { 23511572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) 236ab2de065SBrian Somers mp_CheckAutoloadTimer(mp); 237ab2de065SBrian Somers else 23811572abfSBrian Somers throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL); 239ab2de065SBrian Somers } 240ab2de065SBrian Somers 2413b0f8d2eSBrian Somers void 2423b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle) 2433b0f8d2eSBrian Somers { 24449052c95SBrian Somers mp->peer_is12bit = mp->local_is12bit = 0; 24549052c95SBrian Somers mp->peer_mrru = mp->local_mrru = 0; 246643f4904SBrian Somers 247643f4904SBrian Somers peerid_Init(&mp->peer); 248643f4904SBrian Somers 2499c53a7b1SBrian Somers mp->out.seq = 0; 2509c53a7b1SBrian Somers mp->out.link = 0; 25130949fd4SBrian Somers mp->out.af = AF_INET; 2523b0f8d2eSBrian Somers mp->seq.min_in = 0; 2533b0f8d2eSBrian Somers mp->seq.next_in = 0; 2543b0f8d2eSBrian Somers mp->inbufs = NULL; 2553b0f8d2eSBrian Somers mp->bundle = bundle; 2563b0f8d2eSBrian Somers 2575d9e6103SBrian Somers mp->link.type = LOGICAL_LINK; 2583b0f8d2eSBrian Somers mp->link.name = "mp"; 2593b0f8d2eSBrian Somers mp->link.len = sizeof *mp; 260643f4904SBrian Somers 261ab2de065SBrian Somers mp->cfg.autoload.period = SAMPLE_PERIOD; 262ab2de065SBrian Somers mp->cfg.autoload.min = mp->cfg.autoload.max = 0; 26311572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 26411572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 26511572abfSBrian Somers mp->link.stats.parent = NULL; 26611572abfSBrian Somers mp->link.stats.gather = 0; /* Let the physical links gather stats */ 2673b0f8d2eSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 2683b0f8d2eSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 2693b0f8d2eSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 2703b0f8d2eSBrian Somers 2713b0f8d2eSBrian Somers mp->fsmp.LayerStart = mp_LayerStart; 2723b0f8d2eSBrian Somers mp->fsmp.LayerUp = mp_LayerUp; 2733b0f8d2eSBrian Somers mp->fsmp.LayerDown = mp_LayerDown; 2743b0f8d2eSBrian Somers mp->fsmp.LayerFinish = mp_LayerFinish; 2753b0f8d2eSBrian Somers mp->fsmp.object = mp; 2763b0f8d2eSBrian Somers 2771fa665f5SBrian Somers mpserver_Init(&mp->server); 2781fa665f5SBrian Somers 27949052c95SBrian Somers mp->cfg.mrru = 0; 28049052c95SBrian Somers mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 2817063995cSBrian Somers mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED; 28249052c95SBrian Somers mp->cfg.enddisc.class = 0; 28349052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 28449052c95SBrian Somers mp->cfg.enddisc.len = 0; 28549052c95SBrian Somers 2863b0f8d2eSBrian Somers lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 2873b0f8d2eSBrian Somers ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 2885d9e6103SBrian Somers 2895d9e6103SBrian Somers link_EmptyStack(&mp->link); 2905d9e6103SBrian Somers link_Stack(&mp->link, &protolayer); 2915d9e6103SBrian Somers link_Stack(&mp->link, &ccplayer); 2925d9e6103SBrian Somers link_Stack(&mp->link, &vjlayer); 29367b072f7SBrian Somers #ifndef NONAT 29467b072f7SBrian Somers link_Stack(&mp->link, &natlayer); 2955d9e6103SBrian Somers #endif 29649052c95SBrian Somers } 29749052c95SBrian Somers 29849052c95SBrian Somers int 2991fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl) 30049052c95SBrian Somers { 3011fa665f5SBrian Somers struct lcp *lcp = &dl->physical->link.lcp; 3021fa665f5SBrian Somers 30349052c95SBrian Somers if (mp->active) { 30449052c95SBrian Somers /* We're adding a link - do a last validation on our parameters */ 3051fa665f5SBrian Somers if (!peerid_Equal(&dl->peer, &mp->peer)) { 306dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name); 3077ebff009SBrian Somers log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname, 3087ebff009SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 3097ebff009SBrian Somers mp->peer.enddisc.len)); 3107ebff009SBrian Somers log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname, 3117ebff009SBrian Somers mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address, 3127ebff009SBrian Somers dl->peer.enddisc.len)); 3131fa665f5SBrian Somers return MP_FAILED; 314643f4904SBrian Somers } 3151fa665f5SBrian Somers if (mp->local_mrru != lcp->want_mrru || 3161fa665f5SBrian Somers mp->peer_mrru != lcp->his_mrru || 3171fa665f5SBrian Somers mp->local_is12bit != lcp->want_shortseq || 3181fa665f5SBrian Somers mp->peer_is12bit != lcp->his_shortseq) { 319dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n", 3201fa665f5SBrian Somers dl->name); 3211fa665f5SBrian Somers return MP_FAILED; 32249052c95SBrian Somers } 3231fa665f5SBrian Somers return MP_ADDED; 32449052c95SBrian Somers } else { 32549052c95SBrian Somers /* First link in multilink mode */ 32649052c95SBrian Somers 3271fa665f5SBrian Somers mp->local_mrru = lcp->want_mrru; 3281fa665f5SBrian Somers mp->peer_mrru = lcp->his_mrru; 3291fa665f5SBrian Somers mp->local_is12bit = lcp->want_shortseq; 3301fa665f5SBrian Somers mp->peer_is12bit = lcp->his_shortseq; 3311fa665f5SBrian Somers mp->peer = dl->peer; 33249052c95SBrian Somers 33311572abfSBrian Somers throughput_destroy(&mp->link.stats.total); 33411572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period); 33511572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp); 336673903ecSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 337673903ecSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 338673903ecSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 339673903ecSBrian Somers 34011572abfSBrian Somers /* Tell the link who it belongs to */ 34111572abfSBrian Somers dl->physical->link.stats.parent = &mp->link.stats.total; 34211572abfSBrian Somers 3439c53a7b1SBrian Somers mp->out.seq = 0; 3449c53a7b1SBrian Somers mp->out.link = 0; 34530949fd4SBrian Somers mp->out.af = AF_INET; 346673903ecSBrian Somers mp->seq.min_in = 0; 347673903ecSBrian Somers mp->seq.next_in = 0; 348673903ecSBrian Somers 3491fa665f5SBrian Somers /* 3501fa665f5SBrian Somers * Now we create our server socket. 3511fa665f5SBrian Somers * If it already exists, join it. Otherwise, create and own it 3521fa665f5SBrian Somers */ 35396c9bb21SBrian Somers switch (mpserver_Open(&mp->server, &mp->peer)) { 35496c9bb21SBrian Somers case MPSERVER_CONNECTED: 355dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Transfer link on %s\n", 3566f384573SBrian Somers mp->server.socket.sun_path); 35796c9bb21SBrian Somers mp->server.send.dl = dl; /* Defer 'till it's safe to send */ 3581fa665f5SBrian Somers return MP_LINKSENT; 35996c9bb21SBrian Somers case MPSERVER_FAILED: 3601fa665f5SBrian Somers return MP_FAILED; 36196c9bb21SBrian Somers case MPSERVER_LISTENING: 362dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path); 363dd7e2610SBrian Somers log_Printf(LogPHASE, " First link: %s\n", dl->name); 3641fa665f5SBrian Somers 36530949fd4SBrian Somers /* Re-point our NCP layers at our MP link */ 36630949fd4SBrian Somers ncp_SetLink(&mp->bundle->ncp, &mp->link); 3673b0f8d2eSBrian Somers 3683b0f8d2eSBrian Somers /* Our lcp's already up 'cos of the NULL parent */ 36906337856SBrian Somers if (ccp_SetOpenMode(&mp->link.ccp)) { 370dd7e2610SBrian Somers fsm_Up(&mp->link.ccp.fsm); 371dd7e2610SBrian Somers fsm_Open(&mp->link.ccp.fsm); 37206337856SBrian Somers } 3733b0f8d2eSBrian Somers 3743b0f8d2eSBrian Somers mp->active = 1; 37596c9bb21SBrian Somers break; 37649052c95SBrian Somers } 3771fa665f5SBrian Somers } 3783b0f8d2eSBrian Somers 3791fa665f5SBrian Somers return MP_UP; 3803b0f8d2eSBrian Somers } 3813b0f8d2eSBrian Somers 3823b0f8d2eSBrian Somers void 383673903ecSBrian Somers mp_Down(struct mp *mp) 384673903ecSBrian Somers { 385673903ecSBrian Somers if (mp->active) { 386673903ecSBrian Somers struct mbuf *next; 387673903ecSBrian Somers 388ab2de065SBrian Somers /* Stop that ! */ 389ab2de065SBrian Somers mp_StopAutoloadTimer(mp); 390ab2de065SBrian Somers 3911fa665f5SBrian Somers /* Don't want any more of these */ 3921fa665f5SBrian Somers mpserver_Close(&mp->server); 3931fa665f5SBrian Somers 3941fa665f5SBrian Somers /* CCP goes down with a bang */ 39509206a6fSBrian Somers fsm2initial(&mp->link.ccp.fsm); 396673903ecSBrian Somers 397673903ecSBrian Somers /* Received fragments go in the bit-bucket */ 398673903ecSBrian Somers while (mp->inbufs) { 39926af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 40026af0ae9SBrian Somers m_freem(mp->inbufs); 401673903ecSBrian Somers mp->inbufs = next; 402673903ecSBrian Somers } 403673903ecSBrian Somers 404643f4904SBrian Somers peerid_Init(&mp->peer); 405673903ecSBrian Somers mp->active = 0; 406673903ecSBrian Somers } 407673903ecSBrian Somers } 408673903ecSBrian Somers 409673903ecSBrian Somers void 4103b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink) 4113b0f8d2eSBrian Somers { 4123b0f8d2eSBrian Somers mplink->seq = 0; 413ab2de065SBrian Somers mplink->bandwidth = 0; 4143b0f8d2eSBrian Somers } 4153b0f8d2eSBrian Somers 4165d9e6103SBrian Somers static void 4175d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p) 4183b0f8d2eSBrian Somers { 4193b0f8d2eSBrian Somers struct mp_header mh, h; 4203b0f8d2eSBrian Somers struct mbuf *q, *last; 421057f1760SBrian Somers u_int32_t seq; 4223b0f8d2eSBrian Somers 42386b1f0d7SBrian Somers /* 42486b1f0d7SBrian Somers * When `m' and `p' are NULL, it means our oldest link has gone down. 42586b1f0d7SBrian Somers * We want to determine a new min, and process any intermediate stuff 42686b1f0d7SBrian Somers * as normal 42786b1f0d7SBrian Somers */ 42886b1f0d7SBrian Somers 42986b1f0d7SBrian Somers if (m && mp_ReadHeader(mp, m, &mh) == 0) { 43026af0ae9SBrian Somers m_freem(m); 4313b0f8d2eSBrian Somers return; 4323b0f8d2eSBrian Somers } 4333b0f8d2eSBrian Somers 43486b1f0d7SBrian Somers if (p) { 4353b0f8d2eSBrian Somers seq = p->dl->mp.seq; 4363b0f8d2eSBrian Somers p->dl->mp.seq = mh.seq; 43786b1f0d7SBrian Somers } else 43886b1f0d7SBrian Somers seq = mp->seq.min_in; 43986b1f0d7SBrian Somers 4403b0f8d2eSBrian Somers if (mp->seq.min_in == seq) { 4413b0f8d2eSBrian Somers /* 4423b0f8d2eSBrian Somers * We've received new data on the link that has our min (oldest) seq. 4433b0f8d2eSBrian Somers * Figure out which link now has the smallest (oldest) seq. 4443b0f8d2eSBrian Somers */ 4453b0f8d2eSBrian Somers struct datalink *dl; 4463b0f8d2eSBrian Somers 44786b1f0d7SBrian Somers mp->seq.min_in = (u_int32_t)-1; 4483b0f8d2eSBrian Somers for (dl = mp->bundle->links; dl; dl = dl->next) 44986b1f0d7SBrian Somers if (dl->state == DATALINK_OPEN && 450057f1760SBrian Somers (mp->seq.min_in == (u_int32_t)-1 || 45186b1f0d7SBrian Somers isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in))) 4523b0f8d2eSBrian Somers mp->seq.min_in = dl->mp.seq; 4533b0f8d2eSBrian Somers } 4543b0f8d2eSBrian Somers 4553b0f8d2eSBrian Somers /* 4563b0f8d2eSBrian Somers * Now process as many of our fragments as we can, adding our new 4573b0f8d2eSBrian Somers * fragment in as we go, and ordering with the oldest at the top of 4583b0f8d2eSBrian Somers * the queue. 4593b0f8d2eSBrian Somers */ 4603b0f8d2eSBrian Somers 4613b0f8d2eSBrian Somers last = NULL; 4623b0f8d2eSBrian Somers seq = mp->seq.next_in; 4633b0f8d2eSBrian Somers q = mp->inbufs; 464cb8bd8ddSBrian Somers while (q || m) { 465cb8bd8ddSBrian Somers if (!q) { 466cb8bd8ddSBrian Somers if (last) 467cb8bd8ddSBrian Somers last->m_nextpkt = m; 468cb8bd8ddSBrian Somers else 469cb8bd8ddSBrian Somers mp->inbufs = m; 470cb8bd8ddSBrian Somers q = m; 471cb8bd8ddSBrian Somers m = NULL; 472cb8bd8ddSBrian Somers h = mh; 473cb8bd8ddSBrian Somers } else { 4743b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 475cb8bd8ddSBrian Somers 4761bcced10SBrian Somers if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) { 4773b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 4783b0f8d2eSBrian Somers if (last) 47926af0ae9SBrian Somers last->m_nextpkt = m; 4803b0f8d2eSBrian Somers else 4813b0f8d2eSBrian Somers mp->inbufs = m; 48226af0ae9SBrian Somers m->m_nextpkt = q; 4833b0f8d2eSBrian Somers q = m; 4843b0f8d2eSBrian Somers h = mh; 4853b0f8d2eSBrian Somers m = NULL; 4863b0f8d2eSBrian Somers } 487cb8bd8ddSBrian Somers } 4883b0f8d2eSBrian Somers 4893b0f8d2eSBrian Somers if (h.seq != seq) { 4903b0f8d2eSBrian Somers /* we're missing something :-( */ 49164e0f466SBrian Somers if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) { 4923b0f8d2eSBrian Somers /* we're never gonna get it */ 4933b0f8d2eSBrian Somers struct mbuf *next; 4943b0f8d2eSBrian Somers 4953b0f8d2eSBrian Somers /* Zap all older fragments */ 4963b0f8d2eSBrian Somers while (mp->inbufs != q) { 497dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag\n"); 49826af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 49926af0ae9SBrian Somers m_freem(mp->inbufs); 5003b0f8d2eSBrian Somers mp->inbufs = next; 5013b0f8d2eSBrian Somers } 5023b0f8d2eSBrian Somers 5033b0f8d2eSBrian Somers /* 5043b0f8d2eSBrian Somers * Zap everything until the next `end' fragment OR just before 5053b0f8d2eSBrian Somers * the next `begin' fragment OR 'till seq.min_in - whichever 5063b0f8d2eSBrian Somers * comes first. 5073b0f8d2eSBrian Somers */ 5083b0f8d2eSBrian Somers do { 5093b0f8d2eSBrian Somers mp_ReadHeader(mp, mp->inbufs, &h); 5103b0f8d2eSBrian Somers if (h.begin) { 511ce828a6eSBrian Somers /* We might be able to process this ! */ 5123b0f8d2eSBrian Somers h.seq--; /* We're gonna look for fragment with h.seq+1 */ 5133b0f8d2eSBrian Somers break; 5143b0f8d2eSBrian Somers } 51526af0ae9SBrian Somers next = mp->inbufs->m_nextpkt; 516dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag %u\n", h.seq); 51726af0ae9SBrian Somers m_freem(mp->inbufs); 5183b0f8d2eSBrian Somers mp->inbufs = next; 51964e0f466SBrian Somers } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in, 52064e0f466SBrian Somers h.seq) || h.end)); 5213b0f8d2eSBrian Somers 5223b0f8d2eSBrian Somers /* 5233b0f8d2eSBrian Somers * Continue processing things from here. 5243b0f8d2eSBrian Somers * This deals with the possibility that we received a fragment 5253b0f8d2eSBrian Somers * on the slowest link that invalidates some of our data (because 5263b0f8d2eSBrian Somers * of the hole at `q'), but where there are subsequent `whole' 5273b0f8d2eSBrian Somers * packets that have already been received. 5283b0f8d2eSBrian Somers */ 5293b0f8d2eSBrian Somers 530147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 5313b0f8d2eSBrian Somers last = NULL; 5323b0f8d2eSBrian Somers q = mp->inbufs; 5333b0f8d2eSBrian Somers } else 5343b0f8d2eSBrian Somers /* we may still receive the missing fragment */ 5353b0f8d2eSBrian Somers break; 5363b0f8d2eSBrian Somers } else if (h.end) { 5373b0f8d2eSBrian Somers /* We've got something, reassemble */ 5383b0f8d2eSBrian Somers struct mbuf **frag = &q; 5393b0f8d2eSBrian Somers int len; 540057f1760SBrian Somers long long first = -1; 5413b0f8d2eSBrian Somers 5423b0f8d2eSBrian Somers do { 5433b0f8d2eSBrian Somers *frag = mp->inbufs; 54426af0ae9SBrian Somers mp->inbufs = mp->inbufs->m_nextpkt; 5453b0f8d2eSBrian Somers len = mp_ReadHeader(mp, *frag, &h); 546ce828a6eSBrian Somers if (first == -1) 547ce828a6eSBrian Somers first = h.seq; 5483b0f8d2eSBrian Somers if (frag == &q && !h.begin) { 549dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 5503b0f8d2eSBrian Somers (u_long)h.seq); 55126af0ae9SBrian Somers m_freem(q); 5523b0f8d2eSBrian Somers q = NULL; 5533b0f8d2eSBrian Somers } else if (frag != &q && h.begin) { 554dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 5553b0f8d2eSBrian Somers (u_long)h.seq - 1); 5563b0f8d2eSBrian Somers /* 5573b0f8d2eSBrian Somers * Stuff our fragment back at the front of the queue and zap 558299920e5SBrian Somers * our half-assembled packet. 5593b0f8d2eSBrian Somers */ 56026af0ae9SBrian Somers (*frag)->m_nextpkt = mp->inbufs; 5613b0f8d2eSBrian Somers mp->inbufs = *frag; 5623b0f8d2eSBrian Somers *frag = NULL; 56326af0ae9SBrian Somers m_freem(q); 5643b0f8d2eSBrian Somers q = NULL; 5653b0f8d2eSBrian Somers frag = &q; 5663b0f8d2eSBrian Somers h.end = 0; /* just in case it's a whole packet */ 567299920e5SBrian Somers } else { 568299920e5SBrian Somers (*frag)->m_offset += len; 569299920e5SBrian Somers (*frag)->m_len -= len; 570299920e5SBrian Somers (*frag)->m_nextpkt = NULL; 5713b0f8d2eSBrian Somers do 57226af0ae9SBrian Somers frag = &(*frag)->m_next; 573ce828a6eSBrian Somers while (*frag != NULL); 574299920e5SBrian Somers } 5753b0f8d2eSBrian Somers } while (!h.end); 5763b0f8d2eSBrian Somers 5773b0f8d2eSBrian Somers if (q) { 57826af0ae9SBrian Somers q = m_pullup(q); 5791814213eSMarcel Moolenaar log_Printf(LogDEBUG, "MP: Reassembled frags %lu-%lu, length %zd\n", 580057f1760SBrian Somers (u_long)first, (u_long)h.seq, m_length(q)); 58126af0ae9SBrian Somers link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle); 58226af0ae9SBrian Somers m_freem(q); 5833b0f8d2eSBrian Somers } 5843b0f8d2eSBrian Somers 585147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq); 5863b0f8d2eSBrian Somers last = NULL; 5873b0f8d2eSBrian Somers q = mp->inbufs; 5883b0f8d2eSBrian Somers } else { 5893b0f8d2eSBrian Somers /* Look for the next fragment */ 590147613eaSBrian Somers seq = inc_seq(mp->local_is12bit, seq); 5913b0f8d2eSBrian Somers last = q; 59226af0ae9SBrian Somers q = q->m_nextpkt; 5933b0f8d2eSBrian Somers } 5943b0f8d2eSBrian Somers } 5953b0f8d2eSBrian Somers 5963b0f8d2eSBrian Somers if (m) { 5973b0f8d2eSBrian Somers /* We still have to find a home for our new fragment */ 5983b0f8d2eSBrian Somers last = NULL; 59926af0ae9SBrian Somers for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) { 6003b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 6011bcced10SBrian Somers if (isbefore(mp->local_is12bit, mh.seq, h.seq)) 602147613eaSBrian Somers break; 603147613eaSBrian Somers } 604147613eaSBrian Somers /* Our received fragment fits in here */ 6053b0f8d2eSBrian Somers if (last) 60626af0ae9SBrian Somers last->m_nextpkt = m; 6073b0f8d2eSBrian Somers else 6083b0f8d2eSBrian Somers mp->inbufs = m; 60926af0ae9SBrian Somers m->m_nextpkt = q; 6103b0f8d2eSBrian Somers } 6113b0f8d2eSBrian Somers } 6123b0f8d2eSBrian Somers 6135d9e6103SBrian Somers struct mbuf * 6145d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 6155d9e6103SBrian Somers { 6165d9e6103SBrian Somers struct physical *p = link2physical(l); 6175d9e6103SBrian Somers 6185d9e6103SBrian Somers if (!bundle->ncp.mp.active) 6195d9e6103SBrian Somers /* Let someone else deal with it ! */ 6205d9e6103SBrian Somers return bp; 6215d9e6103SBrian Somers 6225d9e6103SBrian Somers if (p == NULL) { 6235d9e6103SBrian Somers log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n"); 62426af0ae9SBrian Somers m_freem(bp); 625411675baSBrian Somers } else { 62626af0ae9SBrian Somers m_settype(bp, MB_MPIN); 6275d9e6103SBrian Somers mp_Assemble(&bundle->ncp.mp, bp, p); 628411675baSBrian Somers } 6295d9e6103SBrian Somers 6305d9e6103SBrian Somers return NULL; 6315d9e6103SBrian Somers } 6325d9e6103SBrian Somers 6333b0f8d2eSBrian Somers static void 6345d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l, 6355d9e6103SBrian Somers struct mbuf *m, u_int32_t begin, u_int32_t end) 6363b0f8d2eSBrian Somers { 63726af0ae9SBrian Somers char prepend[4]; 6383b0f8d2eSBrian Somers 639ce828a6eSBrian Somers /* Stuff an MP header on the front of our packet and send it */ 64026af0ae9SBrian Somers 64149052c95SBrian Somers if (mp->peer_is12bit) { 6429e8ec64bSBrian Somers u_int16_t val; 643ce828a6eSBrian Somers 6449e8ec64bSBrian Somers val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq; 64526af0ae9SBrian Somers ua_htons(&val, prepend); 64626af0ae9SBrian Somers m = m_prepend(m, prepend, 2, 0); 6473b0f8d2eSBrian Somers } else { 6489e8ec64bSBrian Somers u_int32_t val; 649ce828a6eSBrian Somers 6509e8ec64bSBrian Somers val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq; 65126af0ae9SBrian Somers ua_htonl(&val, prepend); 65226af0ae9SBrian Somers m = m_prepend(m, prepend, 4, 0); 6533b0f8d2eSBrian Somers } 654dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 6551814213eSMarcel Moolenaar log_Printf(LogDEBUG, "MP[frag %d]: Send %zd bytes on link `%s'\n", 65626af0ae9SBrian Somers mp->out.seq, m_length(m), l->name); 657147613eaSBrian Somers mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq); 6583b0f8d2eSBrian Somers 6596301d506SBrian Somers if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) { 6606301d506SBrian Somers log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name); 6616301d506SBrian Somers return; 6626301d506SBrian Somers } 6636301d506SBrian Somers 66426af0ae9SBrian Somers link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP); 6653b0f8d2eSBrian Somers } 6663b0f8d2eSBrian Somers 6673b0f8d2eSBrian Somers int 66830949fd4SBrian Somers mp_FillPhysicalQueues(struct bundle *bundle) 6693b0f8d2eSBrian Somers { 6703b0f8d2eSBrian Somers struct mp *mp = &bundle->ncp.mp; 6719c53a7b1SBrian Somers struct datalink *dl, *fdl; 67226af0ae9SBrian Somers size_t total, add, len; 673a43e859dSBrian Somers int thislink, nlinks, nopenlinks, sendasip; 674b4b280abSBrian Somers u_int32_t begin, end; 6753b0f8d2eSBrian Somers struct mbuf *m, *mo; 67630949fd4SBrian Somers struct link *bestlink; 6773b0f8d2eSBrian Somers 678a43e859dSBrian Somers thislink = nlinks = nopenlinks = 0; 6799c53a7b1SBrian Somers for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) { 68086b1f0d7SBrian Somers /* Include non-open links here as mp->out.link will stay more correct */ 6819c53a7b1SBrian Somers if (!fdl) { 6829c53a7b1SBrian Somers if (thislink == mp->out.link) 6839c53a7b1SBrian Somers fdl = dl; 6849c53a7b1SBrian Somers else 6859c53a7b1SBrian Somers thislink++; 6869c53a7b1SBrian Somers } 6879c53a7b1SBrian Somers nlinks++; 688a43e859dSBrian Somers if (dl->state == DATALINK_OPEN) 689a43e859dSBrian Somers nopenlinks++; 6909c53a7b1SBrian Somers } 6919c53a7b1SBrian Somers 6929c53a7b1SBrian Somers if (!fdl) { 6939c53a7b1SBrian Somers fdl = bundle->links; 6941bc9b5baSBrian Somers if (!fdl) 6951bc9b5baSBrian Somers return 0; 6969c53a7b1SBrian Somers thislink = 0; 6979c53a7b1SBrian Somers } 6983b0f8d2eSBrian Somers 6993b0f8d2eSBrian Somers total = 0; 7009c53a7b1SBrian Somers for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) { 7019c53a7b1SBrian Somers if (!dl) { 7029c53a7b1SBrian Somers dl = bundle->links; 7039c53a7b1SBrian Somers thislink = 0; 7049c53a7b1SBrian Somers } 7059c53a7b1SBrian Somers 7066f384573SBrian Somers if (dl->state != DATALINK_OPEN) 7076f384573SBrian Somers continue; 7089c53a7b1SBrian Somers 7093b0f8d2eSBrian Somers if (dl->physical->out) 7103b0f8d2eSBrian Somers /* this link has suffered a short write. Let it continue */ 7113b0f8d2eSBrian Somers continue; 7129c53a7b1SBrian Somers 7133b0f8d2eSBrian Somers add = link_QueueLen(&dl->physical->link); 7143fd1e17cSBrian Somers if (add) { 7153b0f8d2eSBrian Somers /* this link has got stuff already queued. Let it continue */ 7163fd1e17cSBrian Somers total += add; 7173b0f8d2eSBrian Somers continue; 7183fd1e17cSBrian Somers } 7199c53a7b1SBrian Somers 72030949fd4SBrian Somers if (!mp_QueueLen(mp)) { 7213fd1e17cSBrian Somers int mrutoosmall; 7223fd1e17cSBrian Somers 7233fd1e17cSBrian Somers /* 7243fd1e17cSBrian Somers * If there's only a single open link in our bundle and we haven't got 7253fd1e17cSBrian Somers * MP level link compression, queue outbound traffic directly via that 7263fd1e17cSBrian Somers * link's protocol stack rather than using the MP link. This results 72730949fd4SBrian Somers * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather 72830949fd4SBrian Somers * than PROTO_MP. 7293fd1e17cSBrian Somers */ 7303fd1e17cSBrian Somers 7313fd1e17cSBrian Somers mrutoosmall = 0; 732a43e859dSBrian Somers sendasip = nopenlinks < 2; 733a43e859dSBrian Somers if (sendasip) { 7343fd1e17cSBrian Somers if (dl->physical->link.lcp.his_mru < mp->peer_mrru) { 7353fd1e17cSBrian Somers /* 7363fd1e17cSBrian Somers * Actually, forget it. This test is done against the MRRU rather 7373fd1e17cSBrian Somers * than the packet size so that we don't end up sending some data 7383fd1e17cSBrian Somers * in MP fragments and some data in PROTO_IP packets. That's just 7393fd1e17cSBrian Somers * too likely to upset some ppp implementations. 7403fd1e17cSBrian Somers */ 7413fd1e17cSBrian Somers mrutoosmall = 1; 742a43e859dSBrian Somers sendasip = 0; 7433fd1e17cSBrian Somers } 7443fd1e17cSBrian Somers } 7453fd1e17cSBrian Somers 746a43e859dSBrian Somers bestlink = sendasip ? &dl->physical->link : &mp->link; 74730949fd4SBrian Somers if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink)) 74830949fd4SBrian Somers break; /* Nothing else to send */ 7493b0f8d2eSBrian Somers 7503fd1e17cSBrian Somers if (mrutoosmall) 7513fd1e17cSBrian Somers log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n"); 752a43e859dSBrian Somers else if (sendasip) 7533fd1e17cSBrian Somers log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n"); 7543fd1e17cSBrian Somers 755a43e859dSBrian Somers if (sendasip) { 7563fd1e17cSBrian Somers add = link_QueueLen(&dl->physical->link); 7573fd1e17cSBrian Somers if (add) { 7583fd1e17cSBrian Somers /* this link has got stuff already queued. Let it continue */ 7593fd1e17cSBrian Somers total += add; 7603fd1e17cSBrian Somers continue; 7613fd1e17cSBrian Somers } 7623fd1e17cSBrian Somers } 7633fd1e17cSBrian Somers } 7643fd1e17cSBrian Somers 7653b0f8d2eSBrian Somers m = link_Dequeue(&mp->link); 7663fd1e17cSBrian Somers if (m) { 76726af0ae9SBrian Somers len = m_length(m); 7683b0f8d2eSBrian Somers begin = 1; 7693b0f8d2eSBrian Somers end = 0; 7703b0f8d2eSBrian Somers 7719c53a7b1SBrian Somers while (!end) { 7729c53a7b1SBrian Somers if (dl->state == DATALINK_OPEN) { 773ab2de065SBrian Somers /* Write at most his_mru bytes to the physical link */ 774ab2de065SBrian Somers if (len <= dl->physical->link.lcp.his_mru) { 7753b0f8d2eSBrian Somers mo = m; 7763b0f8d2eSBrian Somers end = 1; 77726af0ae9SBrian Somers m_settype(mo, MB_MPOUT); 7783b0f8d2eSBrian Somers } else { 779ab2de065SBrian Somers /* It's > his_mru, chop the packet (`m') into bits */ 78026af0ae9SBrian Somers mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT); 78126af0ae9SBrian Somers len -= mo->m_len; 78226af0ae9SBrian Somers m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len); 7833b0f8d2eSBrian Somers } 7845d9e6103SBrian Somers mp_Output(mp, bundle, &dl->physical->link, mo, begin, end); 7853b0f8d2eSBrian Somers begin = 0; 7863b0f8d2eSBrian Somers } 7879c53a7b1SBrian Somers 7889c53a7b1SBrian Somers if (!end) { 7899c53a7b1SBrian Somers nlinks--; 7909c53a7b1SBrian Somers dl = dl->next; 7919c53a7b1SBrian Somers if (!dl) { 7929c53a7b1SBrian Somers dl = bundle->links; 7939c53a7b1SBrian Somers thislink = 0; 7949c53a7b1SBrian Somers } else 7959c53a7b1SBrian Somers thislink++; 7963b0f8d2eSBrian Somers } 7979c53a7b1SBrian Somers } 7989c53a7b1SBrian Somers } 7993fd1e17cSBrian Somers } 8009c53a7b1SBrian Somers mp->out.link = thislink; /* Start here next time */ 8013b0f8d2eSBrian Somers 8023b0f8d2eSBrian Somers return total; 8033b0f8d2eSBrian Somers } 8043b0f8d2eSBrian Somers 8053b0f8d2eSBrian Somers int 806ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg) 8073b0f8d2eSBrian Somers { 8083b0f8d2eSBrian Somers int val; 8093b0f8d2eSBrian Somers 81025092092SBrian Somers if (arg->argc != arg->argn+1) 8113b0f8d2eSBrian Somers return -1; 8123b0f8d2eSBrian Somers 81325092092SBrian Somers val = atoi(arg->argv[arg->argn]); 814ab2de065SBrian Somers if (val <= 0) { 815ab2de065SBrian Somers log_Printf(LogWARN, "The link bandwidth must be greater than zero\n"); 8163b0f8d2eSBrian Somers return 1; 8173b0f8d2eSBrian Somers } 818ab2de065SBrian Somers arg->cx->mp.bandwidth = val; 819ab2de065SBrian Somers 820ab2de065SBrian Somers if (arg->cx->state == DATALINK_OPEN) 821ab2de065SBrian Somers bundle_CalculateBandwidth(arg->bundle); 822ab2de065SBrian Somers 8233b0f8d2eSBrian Somers return 0; 8243b0f8d2eSBrian Somers } 82549052c95SBrian Somers 82649052c95SBrian Somers int 82749052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg) 82849052c95SBrian Somers { 82949052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 83049052c95SBrian Somers 83149052c95SBrian Somers prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 8329c53a7b1SBrian Somers if (mp->active) { 833411675baSBrian Somers struct mbuf *m, *lm; 8349c53a7b1SBrian Somers int bufs = 0; 8359c53a7b1SBrian Somers 836fa0d5216SBrian Somers lm = NULL; 8371fa665f5SBrian Somers prompt_Printf(arg->prompt, "Socket: %s\n", 8386f384573SBrian Somers mp->server.socket.sun_path); 83926af0ae9SBrian Somers for (m = mp->inbufs; m; m = m->m_nextpkt) { 8409c53a7b1SBrian Somers bufs++; 841411675baSBrian Somers lm = m; 842411675baSBrian Somers } 843411675baSBrian Somers prompt_Printf(arg->prompt, "Pending frags: %d", bufs); 844411675baSBrian Somers if (bufs) { 845411675baSBrian Somers struct mp_header mh; 846411675baSBrian Somers unsigned long first, last; 847411675baSBrian Somers 848411675baSBrian Somers first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0; 849411675baSBrian Somers last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0; 85064e0f466SBrian Somers prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n", 851411675baSBrian Somers first, last, (unsigned long)mp->seq.next_in, 852411675baSBrian Somers (unsigned long)mp->seq.min_in); 85364e0f466SBrian Somers prompt_Printf(arg->prompt, " First has %sbegin bit and " 85464e0f466SBrian Somers "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no "); 855411675baSBrian Somers } 856411675baSBrian Somers prompt_Printf(arg->prompt, "\n"); 8579c53a7b1SBrian Somers } 85849052c95SBrian Somers 85949052c95SBrian Somers prompt_Printf(arg->prompt, "\nMy Side:\n"); 86049052c95SBrian Somers if (mp->active) { 861ab2de065SBrian Somers prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq); 86249052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 86349052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 86449052c95SBrian Somers mp->local_is12bit ? "on" : "off"); 86549052c95SBrian Somers } 866643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 86749052c95SBrian Somers mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 86849052c95SBrian Somers mp->cfg.enddisc.len)); 86949052c95SBrian Somers 87049052c95SBrian Somers prompt_Printf(arg->prompt, "\nHis Side:\n"); 87149052c95SBrian Somers if (mp->active) { 872643f4904SBrian Somers prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 873ab2de065SBrian Somers prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in); 87449052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 87549052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 87649052c95SBrian Somers mp->peer_is12bit ? "on" : "off"); 87749052c95SBrian Somers } 878643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 879643f4904SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 880643f4904SBrian Somers mp->peer.enddisc.len)); 88149052c95SBrian Somers 88249052c95SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 88349052c95SBrian Somers 88449052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: "); 88549052c95SBrian Somers if (mp->cfg.mrru) 88649052c95SBrian Somers prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 88749052c95SBrian Somers else 88849052c95SBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 88949052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 89049052c95SBrian Somers command_ShowNegval(mp->cfg.shortseq)); 8917063995cSBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 8927063995cSBrian Somers command_ShowNegval(mp->cfg.negenddisc)); 893ab2de065SBrian Somers prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%," 894ab2de065SBrian Somers " period %d secs\n", mp->cfg.autoload.min, 895ab2de065SBrian Somers mp->cfg.autoload.max, mp->cfg.autoload.period); 89649052c95SBrian Somers 89749052c95SBrian Somers return 0; 89849052c95SBrian Somers } 89949052c95SBrian Somers 90049052c95SBrian Somers const char * 901057f1760SBrian Somers mp_Enddisc(u_char c, const char *address, size_t len) 90249052c95SBrian Somers { 903d93d3a9cSBrian Somers static char result[100]; /* Used immediately after it's returned */ 904057f1760SBrian Somers unsigned f, header; 90549052c95SBrian Somers 90649052c95SBrian Somers switch (c) { 90708676022SBrian Somers case ENDDISC_NULL: 90849052c95SBrian Somers sprintf(result, "Null Class"); 90949052c95SBrian Somers break; 91049052c95SBrian Somers 91108676022SBrian Somers case ENDDISC_LOCAL: 9121814213eSMarcel Moolenaar snprintf(result, sizeof result, "Local Addr: %.*s", (int)len, 9131814213eSMarcel Moolenaar address); 91449052c95SBrian Somers break; 91549052c95SBrian Somers 91608676022SBrian Somers case ENDDISC_IP: 91749052c95SBrian Somers if (len == 4) 91849052c95SBrian Somers snprintf(result, sizeof result, "IP %s", 91949052c95SBrian Somers inet_ntoa(*(const struct in_addr *)address)); 92049052c95SBrian Somers else 9211814213eSMarcel Moolenaar sprintf(result, "IP[%zd] ???", len); 92249052c95SBrian Somers break; 92349052c95SBrian Somers 92408676022SBrian Somers case ENDDISC_MAC: 92549052c95SBrian Somers if (len == 6) { 92649052c95SBrian Somers const u_char *m = (const u_char *)address; 92749052c95SBrian Somers snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 92849052c95SBrian Somers m[0], m[1], m[2], m[3], m[4], m[5]); 92949052c95SBrian Somers } else 9301814213eSMarcel Moolenaar sprintf(result, "MAC[%zd] ???", len); 93149052c95SBrian Somers break; 93249052c95SBrian Somers 93308676022SBrian Somers case ENDDISC_MAGIC: 93449052c95SBrian Somers sprintf(result, "Magic: 0x"); 93549052c95SBrian Somers header = strlen(result); 936057f1760SBrian Somers if (len + header + 1 > sizeof result) 93749052c95SBrian Somers len = sizeof result - header - 1; 93849052c95SBrian Somers for (f = 0; f < len; f++) 93949052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 94049052c95SBrian Somers break; 94149052c95SBrian Somers 94208676022SBrian Somers case ENDDISC_PSN: 9431814213eSMarcel Moolenaar snprintf(result, sizeof result, "PSN: %.*s", (int)len, address); 94449052c95SBrian Somers break; 94549052c95SBrian Somers 94649052c95SBrian Somers default: 94749052c95SBrian Somers sprintf(result, "%d: ", (int)c); 94849052c95SBrian Somers header = strlen(result); 949057f1760SBrian Somers if (len + header + 1 > sizeof result) 95049052c95SBrian Somers len = sizeof result - header - 1; 95149052c95SBrian Somers for (f = 0; f < len; f++) 95249052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 95349052c95SBrian Somers break; 95449052c95SBrian Somers } 95549052c95SBrian Somers return result; 95649052c95SBrian Somers } 95749052c95SBrian Somers 95849052c95SBrian Somers int 95949052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg) 96049052c95SBrian Somers { 96149052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 962d47dceb8SBrian Somers struct in_addr addr; 96349052c95SBrian Somers 9649b5f8ffdSBrian Somers switch (bundle_Phase(arg->bundle)) { 9659b5f8ffdSBrian Somers case PHASE_DEAD: 9669b5f8ffdSBrian Somers break; 9679b5f8ffdSBrian Somers case PHASE_ESTABLISH: 9689b5f8ffdSBrian Somers /* Make sure none of our links are DATALINK_LCP or greater */ 9699b5f8ffdSBrian Somers if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) { 9703df5ecacSUlrich Spörlein log_Printf(LogWARN, "enddisc: Only changeable before" 9719b5f8ffdSBrian Somers " LCP negotiations\n"); 9729b5f8ffdSBrian Somers return 1; 9739b5f8ffdSBrian Somers } 9749b5f8ffdSBrian Somers break; 9759b5f8ffdSBrian Somers default: 9763df5ecacSUlrich Spörlein log_Printf(LogWARN, "enddisc: Only changeable at phase DEAD/ESTABLISH\n"); 97749052c95SBrian Somers return 1; 97849052c95SBrian Somers } 97949052c95SBrian Somers 98049052c95SBrian Somers if (arg->argc == arg->argn) { 98149052c95SBrian Somers mp->cfg.enddisc.class = 0; 98249052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 98349052c95SBrian Somers mp->cfg.enddisc.len = 0; 984e43ebac1SBrian Somers } else if (arg->argc > arg->argn) { 98508676022SBrian Somers if (!strcasecmp(arg->argv[arg->argn], "label")) { 98608676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_LOCAL; 98708676022SBrian Somers strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 98808676022SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 98908676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 990259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 99130949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr); 992d47dceb8SBrian Somers else 993259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 994d47dceb8SBrian Somers memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 99508676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_IP; 99649052c95SBrian Somers mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 99708676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 99808676022SBrian Somers struct sockaddr_dl hwaddr; 99908676022SBrian Somers 1000259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY) 100130949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr); 1002d47dceb8SBrian Somers else 1003259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 1004d47dceb8SBrian Somers 1005057f1760SBrian Somers if (arp_EtherAddr(addr, &hwaddr, 1)) { 100608676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAC; 100708676022SBrian Somers memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 100808676022SBrian Somers hwaddr.sdl_alen); 100908676022SBrian Somers mp->cfg.enddisc.len = hwaddr.sdl_alen; 101008676022SBrian Somers } else { 1011dd7e2610SBrian Somers log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 1012259a6191SBrian Somers inet_ntoa(addr)); 101308676022SBrian Somers return 4; 101408676022SBrian Somers } 101549052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 101649052c95SBrian Somers int f; 101749052c95SBrian Somers 101849052c95SBrian Somers randinit(); 101949052c95SBrian Somers for (f = 0; f < 20; f += sizeof(long)) 102049052c95SBrian Somers *(long *)(mp->cfg.enddisc.address + f) = random(); 102108676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAGIC; 102249052c95SBrian Somers mp->cfg.enddisc.len = 20; 102349052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 102449052c95SBrian Somers if (arg->argc > arg->argn+1) { 102508676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_PSN; 102649052c95SBrian Somers strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 102749052c95SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 102849052c95SBrian Somers } else { 1029dd7e2610SBrian Somers log_Printf(LogWARN, "PSN endpoint requires additional data\n"); 103008676022SBrian Somers return 5; 103149052c95SBrian Somers } 103249052c95SBrian Somers } else { 1033dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Unrecognised endpoint type\n", 103449052c95SBrian Somers arg->argv[arg->argn]); 103508676022SBrian Somers return 6; 103649052c95SBrian Somers } 1037e43ebac1SBrian Somers } 103849052c95SBrian Somers 103949052c95SBrian Somers return 0; 104049052c95SBrian Somers } 10411fa665f5SBrian Somers 10421fa665f5SBrian Somers static int 1043f013f33eSBrian Somers mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, 10441fa665f5SBrian Somers int *n) 10451fa665f5SBrian Somers { 10461fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10470f2f3eb3SBrian Somers int result; 10481fa665f5SBrian Somers 10490f2f3eb3SBrian Somers result = 0; 10506f384573SBrian Somers if (s->send.dl != NULL) { 105196c9bb21SBrian Somers /* We've connect()ed */ 1052b7c5748eSBrian Somers if (!link_QueueLen(&s->send.dl->physical->link) && 1053b7c5748eSBrian Somers !s->send.dl->physical->out) { 1054b7c5748eSBrian Somers /* Only send if we've transmitted all our data (i.e. the ConfigAck) */ 10550f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, w, e); 105696c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 10576f384573SBrian Somers s->send.dl = NULL; 105896c9bb21SBrian Somers s->fd = -1; 1059ea722969SBrian Somers } else 1060ea722969SBrian Somers /* Never read from a datalink that's on death row ! */ 10610f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL); 106296c9bb21SBrian Somers } else if (r && s->fd >= 0) { 10631fa665f5SBrian Somers if (*n < s->fd + 1) 10641fa665f5SBrian Somers *n = s->fd + 1; 10651fa665f5SBrian Somers FD_SET(s->fd, r); 106624989c68SBrian Somers log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd); 10670f2f3eb3SBrian Somers result++; 10681fa665f5SBrian Somers } 10690f2f3eb3SBrian Somers return result; 10701fa665f5SBrian Somers } 10711fa665f5SBrian Somers 10721fa665f5SBrian Somers static int 1073f013f33eSBrian Somers mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset) 10741fa665f5SBrian Somers { 10751fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10761fa665f5SBrian Somers return s->fd >= 0 && FD_ISSET(s->fd, fdset); 10771fa665f5SBrian Somers } 10781fa665f5SBrian Somers 10791fa665f5SBrian Somers static void 1080057f1760SBrian Somers mpserver_Read(struct fdescriptor *d, struct bundle *bundle, 1081057f1760SBrian Somers const fd_set *fdset __unused) 10821fa665f5SBrian Somers { 10831fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d); 10841fa665f5SBrian Somers 10852cb305afSBrian Somers bundle_ReceiveDatalink(bundle, s->fd); 10861fa665f5SBrian Somers } 10871fa665f5SBrian Somers 10881af29a6eSBrian Somers static int 1089057f1760SBrian Somers mpserver_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused, 1090057f1760SBrian Somers const fd_set *fdset __unused) 10911fa665f5SBrian Somers { 10921fa665f5SBrian Somers /* We never want to write here ! */ 1093a33b2ef7SBrian Somers log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n"); 10941af29a6eSBrian Somers return 0; 10951fa665f5SBrian Somers } 10961fa665f5SBrian Somers 10971fa665f5SBrian Somers void 10981fa665f5SBrian Somers mpserver_Init(struct mpserver *s) 10991fa665f5SBrian Somers { 11001fa665f5SBrian Somers s->desc.type = MPSERVER_DESCRIPTOR; 11011fa665f5SBrian Somers s->desc.UpdateSet = mpserver_UpdateSet; 11021fa665f5SBrian Somers s->desc.IsSet = mpserver_IsSet; 11031fa665f5SBrian Somers s->desc.Read = mpserver_Read; 11041fa665f5SBrian Somers s->desc.Write = mpserver_Write; 11056f384573SBrian Somers s->send.dl = NULL; 11061fa665f5SBrian Somers s->fd = -1; 11076f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 11081fa665f5SBrian Somers } 11091fa665f5SBrian Somers 11101fa665f5SBrian Somers int 11111fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer) 11121fa665f5SBrian Somers { 1113cbee9754SBrian Somers int f, l; 11141fa665f5SBrian Somers mode_t mask; 11151fa665f5SBrian Somers 11161fa665f5SBrian Somers if (s->fd != -1) { 1117a33b2ef7SBrian Somers log_Printf(LogALERT, "Internal error ! mpserver already open\n"); 11186f384573SBrian Somers mpserver_Close(s); 11191fa665f5SBrian Somers } 11201fa665f5SBrian Somers 11216f384573SBrian Somers l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-", 11226f384573SBrian Somers _PATH_VARRUN, peer->authname, peer->enddisc.class); 11231433aa5dSBrian Somers if (l < 0) { 11241433aa5dSBrian Somers log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno)); 11251433aa5dSBrian Somers return MPSERVER_FAILED; 11261433aa5dSBrian Somers } 11271fa665f5SBrian Somers 1128057f1760SBrian Somers for (f = 0; 1129057f1760SBrian Somers f < peer->enddisc.len && (size_t)l < sizeof s->socket.sun_path - 2; 1130057f1760SBrian Somers f++) { 11316f384573SBrian Somers snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l, 11321fa665f5SBrian Somers "%02x", *(u_char *)(peer->enddisc.address+f)); 11336f384573SBrian Somers l += 2; 11341fa665f5SBrian Somers } 11351fa665f5SBrian Somers 11366f384573SBrian Somers s->socket.sun_family = AF_LOCAL; 11376f384573SBrian Somers s->socket.sun_len = sizeof s->socket; 113863c6cac9SBrian Somers s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0); 11391fa665f5SBrian Somers if (s->fd < 0) { 11402cb305afSBrian Somers log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno)); 114196c9bb21SBrian Somers return MPSERVER_FAILED; 11421fa665f5SBrian Somers } 11431fa665f5SBrian Somers 11446f384573SBrian Somers setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket, 11456f384573SBrian Somers sizeof s->socket); 11461fa665f5SBrian Somers mask = umask(0177); 11472cb305afSBrian Somers 11482cb305afSBrian Somers /* 1149cbee9754SBrian Somers * Try to bind the socket. If we succeed we play server, if we fail 1150cbee9754SBrian Somers * we connect() and hand the link off. 11512cb305afSBrian Somers */ 11522cb305afSBrian Somers 11536f384573SBrian Somers if (ID0bind_un(s->fd, &s->socket) < 0) { 11546f384573SBrian Somers if (errno != EADDRINUSE) { 1155dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n", 11566f384573SBrian Somers s->socket.sun_path, strerror(errno)); 11571fa665f5SBrian Somers umask(mask); 11581fa665f5SBrian Somers close(s->fd); 11591fa665f5SBrian Somers s->fd = -1; 116096c9bb21SBrian Somers return MPSERVER_FAILED; 11616f384573SBrian Somers } 11622cb305afSBrian Somers 1163cbee9754SBrian Somers /* So we're the sender */ 11646f384573SBrian Somers umask(mask); 11656f384573SBrian Somers if (ID0connect_un(s->fd, &s->socket) < 0) { 1166dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n", 11676f384573SBrian Somers s->socket.sun_path, strerror(errno)); 11686f384573SBrian Somers if (errno == ECONNREFUSED) 11692cb305afSBrian Somers log_Printf(LogPHASE, " The previous server died badly !\n"); 11706f384573SBrian Somers close(s->fd); 11716f384573SBrian Somers s->fd = -1; 117296c9bb21SBrian Somers return MPSERVER_FAILED; 11736f384573SBrian Somers } 11746f384573SBrian Somers 11756f384573SBrian Somers /* Donate our link to the other guy */ 117696c9bb21SBrian Somers return MPSERVER_CONNECTED; 11771fa665f5SBrian Somers } 11786f384573SBrian Somers 117996c9bb21SBrian Somers return MPSERVER_LISTENING; 11801fa665f5SBrian Somers } 11811fa665f5SBrian Somers 11821fa665f5SBrian Somers void 11831fa665f5SBrian Somers mpserver_Close(struct mpserver *s) 11841fa665f5SBrian Somers { 11856f384573SBrian Somers if (s->send.dl != NULL) { 118696c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket); 11876f384573SBrian Somers s->send.dl = NULL; 118896c9bb21SBrian Somers s->fd = -1; 118996c9bb21SBrian Somers } else if (s->fd >= 0) { 11901fa665f5SBrian Somers close(s->fd); 11916f384573SBrian Somers if (ID0unlink(s->socket.sun_path) == -1) 1192dd7e2610SBrian Somers log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path, 11936f384573SBrian Somers strerror(errno)); 11946f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket); 11951fa665f5SBrian Somers s->fd = -1; 11961fa665f5SBrian Somers } 11971fa665f5SBrian Somers } 119886b1f0d7SBrian Somers 119986b1f0d7SBrian Somers void 120086b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl) 120186b1f0d7SBrian Somers { 120286b1f0d7SBrian Somers if (mp->seq.min_in == dl->mp.seq) 120386b1f0d7SBrian Somers /* We've lost the link that's holding everything up ! */ 12045d9e6103SBrian Somers mp_Assemble(mp, NULL, NULL); 120586b1f0d7SBrian Somers } 12066f8e9f0aSBrian Somers 120730949fd4SBrian Somers size_t 120830949fd4SBrian Somers mp_QueueLen(struct mp *mp) 12096f8e9f0aSBrian Somers { 121030949fd4SBrian Somers return link_QueueLen(&mp->link); 12116f8e9f0aSBrian Somers } 1212