xref: /freebsd/usr.sbin/ppp/mp.c (revision ab2de065b2604b53f8426fe493586bc3368518ef)
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  *
26ab2de065SBrian Somers  *	$Id: mp.c,v 1.25 1999/06/09 16:54:03 brian Exp $
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"
485d9e6103SBrian Somers #ifndef NOALIAS
495d9e6103SBrian Somers #include "alias_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"
643b0f8d2eSBrian Somers #include "ipcp.h"
653b0f8d2eSBrian Somers #include "auth.h"
663b0f8d2eSBrian Somers #include "lcp.h"
673b0f8d2eSBrian Somers #include "async.h"
683b0f8d2eSBrian Somers #include "ccp.h"
693b0f8d2eSBrian Somers #include "link.h"
703b0f8d2eSBrian Somers #include "descriptor.h"
713b0f8d2eSBrian Somers #include "physical.h"
723b0f8d2eSBrian Somers #include "chat.h"
735d9e6103SBrian Somers #include "proto.h"
743b0f8d2eSBrian Somers #include "filter.h"
753b0f8d2eSBrian Somers #include "mp.h"
763b0f8d2eSBrian Somers #include "chap.h"
7792b09558SBrian Somers #include "cbcp.h"
783b0f8d2eSBrian Somers #include "datalink.h"
79972a1bcfSBrian Somers #ifndef NORADIUS
80972a1bcfSBrian Somers #include "radius.h"
81972a1bcfSBrian Somers #endif
823b0f8d2eSBrian Somers #include "bundle.h"
833b0f8d2eSBrian Somers #include "ip.h"
8449052c95SBrian Somers #include "prompt.h"
8508676022SBrian Somers #include "id.h"
8608676022SBrian Somers #include "arp.h"
873b0f8d2eSBrian Somers 
88643f4904SBrian Somers void
89643f4904SBrian Somers peerid_Init(struct peerid *peer)
90643f4904SBrian Somers {
91643f4904SBrian Somers   peer->enddisc.class = 0;
92643f4904SBrian Somers   *peer->enddisc.address = '\0';
93643f4904SBrian Somers   peer->enddisc.len = 0;
94643f4904SBrian Somers   *peer->authname = '\0';
95643f4904SBrian Somers }
96643f4904SBrian Somers 
97643f4904SBrian Somers int
98643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
99643f4904SBrian Somers {
100643f4904SBrian Somers   return !strcmp(p1->authname, p2->authname) &&
101643f4904SBrian Somers          p1->enddisc.class == p2->enddisc.class &&
102643f4904SBrian Somers          p1->enddisc.len == p2->enddisc.len &&
103643f4904SBrian Somers          !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
104643f4904SBrian Somers }
105643f4904SBrian Somers 
1063b0f8d2eSBrian Somers static u_int32_t
107147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
1083b0f8d2eSBrian Somers {
1093b0f8d2eSBrian Somers   seq++;
110147613eaSBrian Somers   if (is12bit) {
1113b0f8d2eSBrian Somers     if (seq & 0xfffff000)
1123b0f8d2eSBrian Somers       seq = 0;
1133b0f8d2eSBrian Somers   } else if (seq & 0xff000000)
1143b0f8d2eSBrian Somers     seq = 0;
1153b0f8d2eSBrian Somers   return seq;
1163b0f8d2eSBrian Somers }
1173b0f8d2eSBrian Somers 
1183b0f8d2eSBrian Somers static int
1191bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1201bcced10SBrian Somers {
121b4b280abSBrian Somers   u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
122b4b280abSBrian Somers 
123b4b280abSBrian Somers   if (seq1 > max) {
124b4b280abSBrian Somers     if (seq2 < 0x200 || seq2 > seq1)
125b4b280abSBrian Somers       return 1;
126b4b280abSBrian Somers   } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
127b4b280abSBrian Somers     return 1;
128b4b280abSBrian Somers 
129b4b280abSBrian Somers   return 0;
1301bcced10SBrian Somers }
1311bcced10SBrian Somers 
1321bcced10SBrian Somers static int
1333b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1343b0f8d2eSBrian Somers {
13549052c95SBrian Somers   if (mp->local_is12bit) {
1369e8ec64bSBrian Somers     u_int16_t val;
1379e8ec64bSBrian Somers 
1389e8ec64bSBrian Somers     ua_ntohs(MBUF_CTOP(m), &val);
1399e8ec64bSBrian Somers     if (val & 0x3000) {
140dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1413b0f8d2eSBrian Somers       return 0;
1423b0f8d2eSBrian Somers     }
1439e8ec64bSBrian Somers     header->begin = val & 0x8000 ? 1 : 0;
1449e8ec64bSBrian Somers     header->end = val & 0x4000 ? 1 : 0;
1459e8ec64bSBrian Somers     header->seq = val & 0x0fff;
1463b0f8d2eSBrian Somers     return 2;
1473b0f8d2eSBrian Somers   } else {
1489e8ec64bSBrian Somers     ua_ntohl(MBUF_CTOP(m), &header->seq);
1493b0f8d2eSBrian Somers     if (header->seq & 0x3f000000) {
150dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1513b0f8d2eSBrian Somers       return 0;
1523b0f8d2eSBrian Somers     }
153ce828a6eSBrian Somers     header->begin = header->seq & 0x80000000 ? 1 : 0;
154ce828a6eSBrian Somers     header->end = header->seq & 0x40000000 ? 1 : 0;
1553b0f8d2eSBrian Somers     header->seq &= 0x00ffffff;
1563b0f8d2eSBrian Somers     return 4;
1573b0f8d2eSBrian Somers   }
1583b0f8d2eSBrian Somers }
1593b0f8d2eSBrian Somers 
1603b0f8d2eSBrian Somers static void
1613b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp)
1623b0f8d2eSBrian Somers {
16349052c95SBrian Somers   /* The given FSM (ccp) is about to start up ! */
1643b0f8d2eSBrian Somers }
1653b0f8d2eSBrian Somers 
1663b0f8d2eSBrian Somers static void
1673b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp)
1683b0f8d2eSBrian Somers {
16949052c95SBrian Somers   /* The given fsm (ccp) is now up */
1703b0f8d2eSBrian Somers }
1713b0f8d2eSBrian Somers 
1723b0f8d2eSBrian Somers static void
1733b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp)
1743b0f8d2eSBrian Somers {
17549052c95SBrian Somers   /* The given FSM (ccp) has been told to come down */
1763b0f8d2eSBrian Somers }
1773b0f8d2eSBrian Somers 
1783b0f8d2eSBrian Somers static void
1793b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp)
1803b0f8d2eSBrian Somers {
18149052c95SBrian Somers   /* The given fsm (ccp) is now down */
1820a1b5c9dSBrian Somers   if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1830a1b5c9dSBrian Somers     fsm_Open(fp);		/* CCP goes to ST_STOPPED */
1843b0f8d2eSBrian Somers }
1853b0f8d2eSBrian Somers 
186ab2de065SBrian Somers static void
187ab2de065SBrian Somers mp_UpDown(void *v)
188ab2de065SBrian Somers {
189ab2de065SBrian Somers   struct mp *mp = (struct mp *)v;
190ab2de065SBrian Somers   int percent;
191ab2de065SBrian Somers 
192ab2de065SBrian Somers   percent = mp->link.throughput.OctetsPerSecond * 800 / mp->bundle->bandwidth;
193ab2de065SBrian Somers   if (percent >= mp->cfg.autoload.max) {
194ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
195ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
196ab2de065SBrian Somers   } else if (percent <= mp->cfg.autoload.min) {
197ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
198ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
199ab2de065SBrian Somers   }
200ab2de065SBrian Somers }
201ab2de065SBrian Somers 
202ab2de065SBrian Somers void
203ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp)
204ab2de065SBrian Somers {
205ab2de065SBrian Somers   throughput_stop(&mp->link.throughput);
206ab2de065SBrian Somers }
207ab2de065SBrian Somers 
208ab2de065SBrian Somers void
209ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp)
210ab2de065SBrian Somers {
211ab2de065SBrian Somers   if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period) {
212ab2de065SBrian Somers     throughput_destroy(&mp->link.throughput);
213ab2de065SBrian Somers     throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
214ab2de065SBrian Somers     throughput_callback(&mp->link.throughput, mp_UpDown, mp);
215ab2de065SBrian Somers   }
216ab2de065SBrian Somers 
217ab2de065SBrian Somers   if (bundle_WantAutoloadTimer(mp->bundle))
218ab2de065SBrian Somers     throughput_start(&mp->link.throughput, "MP throughput", 1);
219ab2de065SBrian Somers   else
220ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
221ab2de065SBrian Somers }
222ab2de065SBrian Somers 
223ab2de065SBrian Somers void
224ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp)
225ab2de065SBrian Somers {
226ab2de065SBrian Somers   if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period)
227ab2de065SBrian Somers     mp_CheckAutoloadTimer(mp);
228ab2de065SBrian Somers   else
229ab2de065SBrian Somers     throughput_clear(&mp->link.throughput, THROUGHPUT_OVERALL, NULL);
230ab2de065SBrian Somers }
231ab2de065SBrian Somers 
2323b0f8d2eSBrian Somers void
2333b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
2343b0f8d2eSBrian Somers {
23549052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
23649052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
237643f4904SBrian Somers 
238643f4904SBrian Somers   peerid_Init(&mp->peer);
239643f4904SBrian Somers 
2409c53a7b1SBrian Somers   mp->out.seq = 0;
2419c53a7b1SBrian Somers   mp->out.link = 0;
2423b0f8d2eSBrian Somers   mp->seq.min_in = 0;
2433b0f8d2eSBrian Somers   mp->seq.next_in = 0;
2443b0f8d2eSBrian Somers   mp->inbufs = NULL;
2453b0f8d2eSBrian Somers   mp->bundle = bundle;
2463b0f8d2eSBrian Somers 
2475d9e6103SBrian Somers   mp->link.type = LOGICAL_LINK;
2483b0f8d2eSBrian Somers   mp->link.name = "mp";
2493b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
250643f4904SBrian Somers 
251ab2de065SBrian Somers   mp->cfg.autoload.period = SAMPLE_PERIOD;
252ab2de065SBrian Somers   mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
253ab2de065SBrian Somers   throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
254ab2de065SBrian Somers   throughput_callback(&mp->link.throughput, mp_UpDown, mp);
2553b0f8d2eSBrian Somers   memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
2563b0f8d2eSBrian Somers   memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
2573b0f8d2eSBrian Somers   memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
2583b0f8d2eSBrian Somers 
2593b0f8d2eSBrian Somers   mp->fsmp.LayerStart = mp_LayerStart;
2603b0f8d2eSBrian Somers   mp->fsmp.LayerUp = mp_LayerUp;
2613b0f8d2eSBrian Somers   mp->fsmp.LayerDown = mp_LayerDown;
2623b0f8d2eSBrian Somers   mp->fsmp.LayerFinish = mp_LayerFinish;
2633b0f8d2eSBrian Somers   mp->fsmp.object = mp;
2643b0f8d2eSBrian Somers 
2651fa665f5SBrian Somers   mpserver_Init(&mp->server);
2661fa665f5SBrian Somers 
26749052c95SBrian Somers   mp->cfg.mrru = 0;
26849052c95SBrian Somers   mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
2697063995cSBrian Somers   mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
27049052c95SBrian Somers   mp->cfg.enddisc.class = 0;
27149052c95SBrian Somers   *mp->cfg.enddisc.address = '\0';
27249052c95SBrian Somers   mp->cfg.enddisc.len = 0;
27349052c95SBrian Somers 
2743b0f8d2eSBrian Somers   lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2753b0f8d2eSBrian Somers   ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
2765d9e6103SBrian Somers 
2775d9e6103SBrian Somers   link_EmptyStack(&mp->link);
2785d9e6103SBrian Somers   link_Stack(&mp->link, &protolayer);
2795d9e6103SBrian Somers   link_Stack(&mp->link, &ccplayer);
2805d9e6103SBrian Somers   link_Stack(&mp->link, &vjlayer);
2815d9e6103SBrian Somers #ifndef NOALIAS
2825d9e6103SBrian Somers   link_Stack(&mp->link, &aliaslayer);
2835d9e6103SBrian Somers #endif
28449052c95SBrian Somers }
28549052c95SBrian Somers 
28649052c95SBrian Somers int
2871fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
28849052c95SBrian Somers {
2891fa665f5SBrian Somers   struct lcp *lcp = &dl->physical->link.lcp;
2901fa665f5SBrian Somers 
29149052c95SBrian Somers   if (mp->active) {
29249052c95SBrian Somers     /* We're adding a link - do a last validation on our parameters */
2931fa665f5SBrian Somers     if (!peerid_Equal(&dl->peer, &mp->peer)) {
294dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
2951fa665f5SBrian Somers       return MP_FAILED;
296643f4904SBrian Somers     }
2971fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
2981fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
2991fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
3001fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
301dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
3021fa665f5SBrian Somers                 dl->name);
3031fa665f5SBrian Somers       return MP_FAILED;
30449052c95SBrian Somers     }
3051fa665f5SBrian Somers     return MP_ADDED;
30649052c95SBrian Somers   } else {
30749052c95SBrian Somers     /* First link in multilink mode */
30849052c95SBrian Somers 
3091fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
3101fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
3111fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
3121fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
3131fa665f5SBrian Somers     mp->peer = dl->peer;
31449052c95SBrian Somers 
315ab2de065SBrian Somers     throughput_destroy(&mp->link.throughput);
316ab2de065SBrian Somers     throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
317ab2de065SBrian Somers     throughput_callback(&mp->link.throughput, mp_UpDown, mp);
318673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
319673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
320673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
321673903ecSBrian Somers 
3229c53a7b1SBrian Somers     mp->out.seq = 0;
3239c53a7b1SBrian Somers     mp->out.link = 0;
324673903ecSBrian Somers     mp->seq.min_in = 0;
325673903ecSBrian Somers     mp->seq.next_in = 0;
326673903ecSBrian Somers 
3271fa665f5SBrian Somers     /*
3281fa665f5SBrian Somers      * Now we create our server socket.
3291fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
3301fa665f5SBrian Somers      */
33196c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
33296c9bb21SBrian Somers     case MPSERVER_CONNECTED:
333dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
3346f384573SBrian Somers                 mp->server.socket.sun_path);
33596c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
3361fa665f5SBrian Somers       return MP_LINKSENT;
33796c9bb21SBrian Somers     case MPSERVER_FAILED:
3381fa665f5SBrian Somers       return MP_FAILED;
33996c9bb21SBrian Somers     case MPSERVER_LISTENING:
340dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
341dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
3421fa665f5SBrian Somers 
34349052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
344ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
3453b0f8d2eSBrian Somers 
3463b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
34706337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
348dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
349dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
35006337856SBrian Somers       }
3513b0f8d2eSBrian Somers 
3523b0f8d2eSBrian Somers       mp->active = 1;
35396c9bb21SBrian Somers       break;
35449052c95SBrian Somers     }
3551fa665f5SBrian Somers   }
3563b0f8d2eSBrian Somers 
3571fa665f5SBrian Somers   return MP_UP;
3583b0f8d2eSBrian Somers }
3593b0f8d2eSBrian Somers 
3603b0f8d2eSBrian Somers void
361673903ecSBrian Somers mp_Down(struct mp *mp)
362673903ecSBrian Somers {
363673903ecSBrian Somers   if (mp->active) {
364673903ecSBrian Somers     struct mbuf *next;
365673903ecSBrian Somers 
366ab2de065SBrian Somers     /* Stop that ! */
367ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
368ab2de065SBrian Somers 
3691fa665f5SBrian Somers     /* Don't want any more of these */
3701fa665f5SBrian Somers     mpserver_Close(&mp->server);
3711fa665f5SBrian Somers 
3721fa665f5SBrian Somers     /* CCP goes down with a bang */
37309206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
374673903ecSBrian Somers 
375673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
376673903ecSBrian Somers     while (mp->inbufs) {
377673903ecSBrian Somers       next = mp->inbufs->pnext;
378dd7e2610SBrian Somers       mbuf_Free(mp->inbufs);
379673903ecSBrian Somers       mp->inbufs = next;
380673903ecSBrian Somers     }
381673903ecSBrian Somers 
382643f4904SBrian Somers     peerid_Init(&mp->peer);
383673903ecSBrian Somers     mp->active = 0;
384673903ecSBrian Somers   }
385673903ecSBrian Somers }
386673903ecSBrian Somers 
387673903ecSBrian Somers void
3883b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
3893b0f8d2eSBrian Somers {
3903b0f8d2eSBrian Somers   mplink->seq = 0;
391ab2de065SBrian Somers   mplink->bandwidth = 0;
3923b0f8d2eSBrian Somers }
3933b0f8d2eSBrian Somers 
3945d9e6103SBrian Somers static void
3955d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
3963b0f8d2eSBrian Somers {
3973b0f8d2eSBrian Somers   struct mp_header mh, h;
3983b0f8d2eSBrian Somers   struct mbuf *q, *last;
3993b0f8d2eSBrian Somers   int32_t seq;
4003b0f8d2eSBrian Somers 
40186b1f0d7SBrian Somers   /*
40286b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
40386b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
40486b1f0d7SBrian Somers    * as normal
40586b1f0d7SBrian Somers    */
40686b1f0d7SBrian Somers 
40786b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
408dd7e2610SBrian Somers     mbuf_Free(m);
4093b0f8d2eSBrian Somers     return;
4103b0f8d2eSBrian Somers   }
4113b0f8d2eSBrian Somers 
41286b1f0d7SBrian Somers   if (p) {
4133b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
4143b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
41586b1f0d7SBrian Somers   } else
41686b1f0d7SBrian Somers     seq = mp->seq.min_in;
41786b1f0d7SBrian Somers 
4183b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
4193b0f8d2eSBrian Somers     /*
4203b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
4213b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
4223b0f8d2eSBrian Somers      */
4233b0f8d2eSBrian Somers     struct datalink *dl;
4243b0f8d2eSBrian Somers 
42586b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
4263b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
42786b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
42886b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
42986b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
4303b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
4313b0f8d2eSBrian Somers   }
4323b0f8d2eSBrian Somers 
4333b0f8d2eSBrian Somers   /*
4343b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
4353b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
4363b0f8d2eSBrian Somers    * the queue.
4373b0f8d2eSBrian Somers    */
4383b0f8d2eSBrian Somers 
4393b0f8d2eSBrian Somers   if (!mp->inbufs) {
4403b0f8d2eSBrian Somers     mp->inbufs = m;
4413b0f8d2eSBrian Somers     m = NULL;
4423b0f8d2eSBrian Somers   }
4433b0f8d2eSBrian Somers 
4443b0f8d2eSBrian Somers   last = NULL;
4453b0f8d2eSBrian Somers   seq = mp->seq.next_in;
4463b0f8d2eSBrian Somers   q = mp->inbufs;
4473b0f8d2eSBrian Somers   while (q) {
4483b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
4491bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
4503b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
4513b0f8d2eSBrian Somers       if (last)
4523b0f8d2eSBrian Somers         last->pnext = m;
4533b0f8d2eSBrian Somers       else
4543b0f8d2eSBrian Somers         mp->inbufs = m;
4553b0f8d2eSBrian Somers       m->pnext = q;
4563b0f8d2eSBrian Somers       q = m;
4573b0f8d2eSBrian Somers       h = mh;
4583b0f8d2eSBrian Somers       m = NULL;
4593b0f8d2eSBrian Somers     }
4603b0f8d2eSBrian Somers 
4613b0f8d2eSBrian Somers     if (h.seq != seq) {
4623b0f8d2eSBrian Somers       /* we're missing something :-( */
46364e0f466SBrian Somers       if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
4643b0f8d2eSBrian Somers         /* we're never gonna get it */
4653b0f8d2eSBrian Somers         struct mbuf *next;
4663b0f8d2eSBrian Somers 
4673b0f8d2eSBrian Somers         /* Zap all older fragments */
4683b0f8d2eSBrian Somers         while (mp->inbufs != q) {
469dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
4703b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
471dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4723b0f8d2eSBrian Somers           mp->inbufs = next;
4733b0f8d2eSBrian Somers         }
4743b0f8d2eSBrian Somers 
4753b0f8d2eSBrian Somers         /*
4763b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4773b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4783b0f8d2eSBrian Somers          * comes first.
4793b0f8d2eSBrian Somers          */
4803b0f8d2eSBrian Somers         do {
4813b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4823b0f8d2eSBrian Somers           if (h.begin) {
483ce828a6eSBrian Somers             /* We might be able to process this ! */
4843b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4853b0f8d2eSBrian Somers             break;
4863b0f8d2eSBrian Somers           }
4873b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
488dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
489dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4903b0f8d2eSBrian Somers           mp->inbufs = next;
49164e0f466SBrian Somers         } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
49264e0f466SBrian Somers                                          h.seq) || h.end));
4933b0f8d2eSBrian Somers 
4943b0f8d2eSBrian Somers         /*
4953b0f8d2eSBrian Somers          * Continue processing things from here.
4963b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
4973b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
4983b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
4993b0f8d2eSBrian Somers          * packets that have already been received.
5003b0f8d2eSBrian Somers          */
5013b0f8d2eSBrian Somers 
502147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5033b0f8d2eSBrian Somers         last = NULL;
5043b0f8d2eSBrian Somers         q = mp->inbufs;
5053b0f8d2eSBrian Somers       } else
5063b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
5073b0f8d2eSBrian Somers         break;
5083b0f8d2eSBrian Somers     } else if (h.end) {
5093b0f8d2eSBrian Somers       /* We've got something, reassemble */
5103b0f8d2eSBrian Somers       struct mbuf **frag = &q;
5113b0f8d2eSBrian Somers       int len;
512ce828a6eSBrian Somers       u_long first = -1;
5133b0f8d2eSBrian Somers 
5143b0f8d2eSBrian Somers       do {
5153b0f8d2eSBrian Somers         *frag = mp->inbufs;
5163b0f8d2eSBrian Somers         mp->inbufs = mp->inbufs->pnext;
5173b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
518ce828a6eSBrian Somers         if (first == -1)
519ce828a6eSBrian Somers           first = h.seq;
5203b0f8d2eSBrian Somers         (*frag)->offset += len;
5213b0f8d2eSBrian Somers         (*frag)->cnt -= len;
5223b0f8d2eSBrian Somers         (*frag)->pnext = NULL;
5233b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
524dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
5253b0f8d2eSBrian Somers                     (u_long)h.seq);
526dd7e2610SBrian Somers           mbuf_Free(q);
5273b0f8d2eSBrian Somers           q = NULL;
5283b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
529dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
5303b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
5313b0f8d2eSBrian Somers           /*
5323b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
5333b0f8d2eSBrian Somers            * our half-assembed packet.
5343b0f8d2eSBrian Somers            */
5353b0f8d2eSBrian Somers           (*frag)->pnext = mp->inbufs;
5363b0f8d2eSBrian Somers           mp->inbufs = *frag;
5373b0f8d2eSBrian Somers           *frag = NULL;
538dd7e2610SBrian Somers           mbuf_Free(q);
5393b0f8d2eSBrian Somers           q = NULL;
5403b0f8d2eSBrian Somers           frag = &q;
5413b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
5423b0f8d2eSBrian Somers         } else
5433b0f8d2eSBrian Somers           do
5443b0f8d2eSBrian Somers             frag = &(*frag)->next;
545ce828a6eSBrian Somers           while (*frag != NULL);
5463b0f8d2eSBrian Somers       } while (!h.end);
5473b0f8d2eSBrian Somers 
5483b0f8d2eSBrian Somers       if (q) {
5496815097bSBrian Somers         q = mbuf_Contiguous(q);
550dd7e2610SBrian Somers         log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
551dd7e2610SBrian Somers                    first, (u_long)h.seq, mbuf_Length(q));
5525d9e6103SBrian Somers         link_PullPacket(&mp->link, MBUF_CTOP(q), q->cnt, mp->bundle);
55365cec6e7SBrian Somers         mbuf_Free(q);
5543b0f8d2eSBrian Somers       }
5553b0f8d2eSBrian Somers 
556147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5573b0f8d2eSBrian Somers       last = NULL;
5583b0f8d2eSBrian Somers       q = mp->inbufs;
5593b0f8d2eSBrian Somers     } else {
5603b0f8d2eSBrian Somers       /* Look for the next fragment */
561147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5623b0f8d2eSBrian Somers       last = q;
5633b0f8d2eSBrian Somers       q = q->pnext;
5643b0f8d2eSBrian Somers     }
5653b0f8d2eSBrian Somers   }
5663b0f8d2eSBrian Somers 
5673b0f8d2eSBrian Somers   if (m) {
5683b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5693b0f8d2eSBrian Somers     last = NULL;
5703b0f8d2eSBrian Somers     for (q = mp->inbufs; q; last = q, q = q->pnext) {
5713b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5721bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
573147613eaSBrian Somers         break;
574147613eaSBrian Somers     }
575147613eaSBrian Somers     /* Our received fragment fits in here */
5763b0f8d2eSBrian Somers     if (last)
5773b0f8d2eSBrian Somers       last->pnext = m;
5783b0f8d2eSBrian Somers     else
5793b0f8d2eSBrian Somers       mp->inbufs = m;
5803b0f8d2eSBrian Somers     m->pnext = q;
5813b0f8d2eSBrian Somers   }
5823b0f8d2eSBrian Somers }
5833b0f8d2eSBrian Somers 
5845d9e6103SBrian Somers struct mbuf *
5855d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
5865d9e6103SBrian Somers {
5875d9e6103SBrian Somers   struct physical *p = link2physical(l);
5885d9e6103SBrian Somers 
5895d9e6103SBrian Somers   if (!bundle->ncp.mp.active)
5905d9e6103SBrian Somers     /* Let someone else deal with it ! */
5915d9e6103SBrian Somers     return bp;
5925d9e6103SBrian Somers 
5935d9e6103SBrian Somers   if (p == NULL) {
5945d9e6103SBrian Somers     log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
5955d9e6103SBrian Somers     mbuf_Free(bp);
596411675baSBrian Somers   } else {
597411675baSBrian Somers     mbuf_SetType(bp, MB_MPIN);
5985d9e6103SBrian Somers     mp_Assemble(&bundle->ncp.mp, bp, p);
599411675baSBrian Somers   }
6005d9e6103SBrian Somers 
6015d9e6103SBrian Somers   return NULL;
6025d9e6103SBrian Somers }
6035d9e6103SBrian Somers 
6043b0f8d2eSBrian Somers static void
6055d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
6065d9e6103SBrian Somers           struct mbuf *m, u_int32_t begin, u_int32_t end)
6073b0f8d2eSBrian Somers {
6083b0f8d2eSBrian Somers   struct mbuf *mo;
6093b0f8d2eSBrian Somers 
610ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
611411675baSBrian Somers   mo = mbuf_Alloc(4, MB_MPOUT);
6123b0f8d2eSBrian Somers   mo->next = m;
61349052c95SBrian Somers   if (mp->peer_is12bit) {
6149e8ec64bSBrian Somers     u_int16_t val;
615ce828a6eSBrian Somers 
6169e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
6179e8ec64bSBrian Somers     ua_htons(&val, MBUF_CTOP(mo));
6183b0f8d2eSBrian Somers     mo->cnt = 2;
6193b0f8d2eSBrian Somers   } else {
6209e8ec64bSBrian Somers     u_int32_t val;
621ce828a6eSBrian Somers 
6229e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
6239e8ec64bSBrian Somers     ua_htonl(&val, MBUF_CTOP(mo));
6243b0f8d2eSBrian Somers     mo->cnt = 4;
6253b0f8d2eSBrian Somers   }
626dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
6279c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
6289c53a7b1SBrian Somers                mp->out.seq, mbuf_Length(mo), l->name);
629147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
6303b0f8d2eSBrian Somers 
6315d9e6103SBrian Somers   link_PushPacket(l, mo, bundle, PRI_NORMAL, PROTO_MP);
6323b0f8d2eSBrian Somers }
6333b0f8d2eSBrian Somers 
6343b0f8d2eSBrian Somers int
6353b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
6363b0f8d2eSBrian Somers {
6373b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
6389c53a7b1SBrian Somers   struct datalink *dl, *fdl;
639b4b280abSBrian Somers   int total, add, len, thislink, nlinks;
640b4b280abSBrian Somers   u_int32_t begin, end;
6413b0f8d2eSBrian Somers   struct mbuf *m, *mo;
6423b0f8d2eSBrian Somers 
6439c53a7b1SBrian Somers   thislink = nlinks = 0;
6449c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
64586b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
6469c53a7b1SBrian Somers     if (!fdl) {
6479c53a7b1SBrian Somers       if (thislink == mp->out.link)
6489c53a7b1SBrian Somers         fdl = dl;
6499c53a7b1SBrian Somers       else
6509c53a7b1SBrian Somers         thislink++;
6519c53a7b1SBrian Somers     }
6529c53a7b1SBrian Somers     nlinks++;
6539c53a7b1SBrian Somers   }
6549c53a7b1SBrian Somers 
6559c53a7b1SBrian Somers   if (!fdl) {
6569c53a7b1SBrian Somers     fdl = bundle->links;
6571bc9b5baSBrian Somers     if (!fdl)
6581bc9b5baSBrian Somers       return 0;
6599c53a7b1SBrian Somers     thislink = 0;
6609c53a7b1SBrian Somers   }
6613b0f8d2eSBrian Somers 
6623b0f8d2eSBrian Somers   total = 0;
6639c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6649c53a7b1SBrian Somers     if (!dl) {
6659c53a7b1SBrian Somers       dl = bundle->links;
6669c53a7b1SBrian Somers       thislink = 0;
6679c53a7b1SBrian Somers     }
6689c53a7b1SBrian Somers 
6696f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
6706f384573SBrian Somers       continue;
6719c53a7b1SBrian Somers 
6723b0f8d2eSBrian Somers     if (dl->physical->out)
6733b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
6743b0f8d2eSBrian Somers       continue;
6759c53a7b1SBrian Somers 
6763b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
6773b0f8d2eSBrian Somers     total += add;
6783b0f8d2eSBrian Somers     if (add)
6793b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6803b0f8d2eSBrian Somers       continue;
6819c53a7b1SBrian Somers 
6825d9e6103SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
6833b0f8d2eSBrian Somers       /* Nothing else to send */
6843b0f8d2eSBrian Somers       break;
6853b0f8d2eSBrian Somers 
6863b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
687dd7e2610SBrian Somers     len = mbuf_Length(m);
6883b0f8d2eSBrian Somers     begin = 1;
6893b0f8d2eSBrian Somers     end = 0;
6903b0f8d2eSBrian Somers 
6919c53a7b1SBrian Somers     while (!end) {
6929c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
693ab2de065SBrian Somers         /* Write at most his_mru bytes to the physical link */
694ab2de065SBrian Somers         if (len <= dl->physical->link.lcp.his_mru) {
6953b0f8d2eSBrian Somers           mo = m;
6963b0f8d2eSBrian Somers           end = 1;
697411675baSBrian Somers           mbuf_SetType(mo, MB_MPOUT);
6983b0f8d2eSBrian Somers         } else {
699ab2de065SBrian Somers           /* It's > his_mru, chop the packet (`m') into bits */
700ab2de065SBrian Somers           mo = mbuf_Alloc(dl->physical->link.lcp.his_mru, MB_MPOUT);
7013b0f8d2eSBrian Somers           len -= mo->cnt;
702dd7e2610SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt);
7033b0f8d2eSBrian Somers         }
7045d9e6103SBrian Somers         mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
7053b0f8d2eSBrian Somers         begin = 0;
7063b0f8d2eSBrian Somers       }
7079c53a7b1SBrian Somers 
7089c53a7b1SBrian Somers       if (!end) {
7099c53a7b1SBrian Somers         nlinks--;
7109c53a7b1SBrian Somers         dl = dl->next;
7119c53a7b1SBrian Somers         if (!dl) {
7129c53a7b1SBrian Somers           dl = bundle->links;
7139c53a7b1SBrian Somers           thislink = 0;
7149c53a7b1SBrian Somers         } else
7159c53a7b1SBrian Somers           thislink++;
7163b0f8d2eSBrian Somers       }
7179c53a7b1SBrian Somers     }
7189c53a7b1SBrian Somers   }
7199c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
7203b0f8d2eSBrian Somers 
7213b0f8d2eSBrian Somers   return total;
7223b0f8d2eSBrian Somers }
7233b0f8d2eSBrian Somers 
7243b0f8d2eSBrian Somers int
725ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg)
7263b0f8d2eSBrian Somers {
7273b0f8d2eSBrian Somers   int val;
7283b0f8d2eSBrian Somers 
72925092092SBrian Somers   if (arg->argc != arg->argn+1)
7303b0f8d2eSBrian Somers     return -1;
7313b0f8d2eSBrian Somers 
73225092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
733ab2de065SBrian Somers   if (val <= 0) {
734ab2de065SBrian Somers     log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
7353b0f8d2eSBrian Somers     return 1;
7363b0f8d2eSBrian Somers   }
737ab2de065SBrian Somers   arg->cx->mp.bandwidth = val;
738ab2de065SBrian Somers 
739ab2de065SBrian Somers   if (arg->cx->state == DATALINK_OPEN)
740ab2de065SBrian Somers     bundle_CalculateBandwidth(arg->bundle);
741ab2de065SBrian Somers 
7423b0f8d2eSBrian Somers   return 0;
7433b0f8d2eSBrian Somers }
74449052c95SBrian Somers 
74549052c95SBrian Somers int
74649052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
74749052c95SBrian Somers {
74849052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
74949052c95SBrian Somers 
75049052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
7519c53a7b1SBrian Somers   if (mp->active) {
752411675baSBrian Somers     struct mbuf *m, *lm;
7539c53a7b1SBrian Somers     int bufs = 0;
7549c53a7b1SBrian Somers 
755fa0d5216SBrian Somers     lm = NULL;
7561fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
7576f384573SBrian Somers                   mp->server.socket.sun_path);
758411675baSBrian Somers     for (m = mp->inbufs; m; m = m->pnext) {
7599c53a7b1SBrian Somers       bufs++;
760411675baSBrian Somers       lm = m;
761411675baSBrian Somers     }
762411675baSBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d", bufs);
763411675baSBrian Somers     if (bufs) {
764411675baSBrian Somers       struct mp_header mh;
765411675baSBrian Somers       unsigned long first, last;
766411675baSBrian Somers 
767411675baSBrian Somers       first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
768411675baSBrian Somers       last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
76964e0f466SBrian Somers       prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
770411675baSBrian Somers                     first, last, (unsigned long)mp->seq.next_in,
771411675baSBrian Somers                     (unsigned long)mp->seq.min_in);
77264e0f466SBrian Somers       prompt_Printf(arg->prompt, "                First has %sbegin bit and "
77364e0f466SBrian Somers                     "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
774411675baSBrian Somers     }
775411675baSBrian Somers     prompt_Printf(arg->prompt, "\n");
7769c53a7b1SBrian Somers   }
77749052c95SBrian Somers 
77849052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
77949052c95SBrian Somers   if (mp->active) {
780ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Output SEQ:    %u\n", mp->out.seq);
78149052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
78249052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
78349052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
78449052c95SBrian Somers   }
785643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
78649052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
78749052c95SBrian Somers                            mp->cfg.enddisc.len));
78849052c95SBrian Somers 
78949052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
79049052c95SBrian Somers   if (mp->active) {
791643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
792ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Input SEQ:     %u\n", mp->seq.next_in);
79349052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
79449052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
79549052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
79649052c95SBrian Somers   }
797643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
798643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
799643f4904SBrian Somers                            mp->peer.enddisc.len));
80049052c95SBrian Somers 
80149052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
80249052c95SBrian Somers 
80349052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
80449052c95SBrian Somers   if (mp->cfg.mrru)
80549052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
80649052c95SBrian Somers   else
80749052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
80849052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
80949052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
8107063995cSBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
8117063995cSBrian Somers                   command_ShowNegval(mp->cfg.negenddisc));
812ab2de065SBrian Somers   prompt_Printf(arg->prompt, " AutoLoad:      min %d%%, max %d%%,"
813ab2de065SBrian Somers                 " period %d secs\n", mp->cfg.autoload.min,
814ab2de065SBrian Somers                 mp->cfg.autoload.max, mp->cfg.autoload.period);
81549052c95SBrian Somers 
81649052c95SBrian Somers   return 0;
81749052c95SBrian Somers }
81849052c95SBrian Somers 
81949052c95SBrian Somers const char *
82049052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
82149052c95SBrian Somers {
822d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
82349052c95SBrian Somers   int f, header;
82449052c95SBrian Somers 
82549052c95SBrian Somers   switch (c) {
82608676022SBrian Somers     case ENDDISC_NULL:
82749052c95SBrian Somers       sprintf(result, "Null Class");
82849052c95SBrian Somers       break;
82949052c95SBrian Somers 
83008676022SBrian Somers     case ENDDISC_LOCAL:
83149052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
83249052c95SBrian Somers       break;
83349052c95SBrian Somers 
83408676022SBrian Somers     case ENDDISC_IP:
83549052c95SBrian Somers       if (len == 4)
83649052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
83749052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
83849052c95SBrian Somers       else
83949052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
84049052c95SBrian Somers       break;
84149052c95SBrian Somers 
84208676022SBrian Somers     case ENDDISC_MAC:
84349052c95SBrian Somers       if (len == 6) {
84449052c95SBrian Somers         const u_char *m = (const u_char *)address;
84549052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
84649052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
84749052c95SBrian Somers       } else
84849052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
84949052c95SBrian Somers       break;
85049052c95SBrian Somers 
85108676022SBrian Somers     case ENDDISC_MAGIC:
85249052c95SBrian Somers       sprintf(result, "Magic: 0x");
85349052c95SBrian Somers       header = strlen(result);
85449052c95SBrian Somers       if (len > sizeof result - header - 1)
85549052c95SBrian Somers         len = sizeof result - header - 1;
85649052c95SBrian Somers       for (f = 0; f < len; f++)
85749052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
85849052c95SBrian Somers       break;
85949052c95SBrian Somers 
86008676022SBrian Somers     case ENDDISC_PSN:
86149052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
86249052c95SBrian Somers       break;
86349052c95SBrian Somers 
86449052c95SBrian Somers      default:
86549052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
86649052c95SBrian Somers       header = strlen(result);
86749052c95SBrian Somers       if (len > sizeof result - header - 1)
86849052c95SBrian Somers         len = sizeof result - header - 1;
86949052c95SBrian Somers       for (f = 0; f < len; f++)
87049052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
87149052c95SBrian Somers       break;
87249052c95SBrian Somers   }
87349052c95SBrian Somers   return result;
87449052c95SBrian Somers }
87549052c95SBrian Somers 
87649052c95SBrian Somers int
87749052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
87849052c95SBrian Somers {
87949052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
880d47dceb8SBrian Somers   struct in_addr addr;
88149052c95SBrian Somers 
8829b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
8839b5f8ffdSBrian Somers     case PHASE_DEAD:
8849b5f8ffdSBrian Somers       break;
8859b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
8869b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
8879b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
8889b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
8899b5f8ffdSBrian Somers                    " LCP negotiations\n");
8909b5f8ffdSBrian Somers         return 1;
8919b5f8ffdSBrian Somers       }
8929b5f8ffdSBrian Somers       break;
8939b5f8ffdSBrian Somers     default:
8949b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
89549052c95SBrian Somers       return 1;
89649052c95SBrian Somers   }
89749052c95SBrian Somers 
89849052c95SBrian Somers   if (arg->argc == arg->argn) {
89949052c95SBrian Somers     mp->cfg.enddisc.class = 0;
90049052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
90149052c95SBrian Somers     mp->cfg.enddisc.len = 0;
902e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
90308676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
90408676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
90508676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
90608676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
90708676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
908259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
909259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
910d47dceb8SBrian Somers       else
911259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
912d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
91308676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
91449052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
91508676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
91608676022SBrian Somers       struct sockaddr_dl hwaddr;
91708676022SBrian Somers       int s;
91808676022SBrian Somers 
919259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
920259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
921d47dceb8SBrian Somers       else
922259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
923d47dceb8SBrian Somers 
92408676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
92508676022SBrian Somers       if (s < 0) {
926dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
92708676022SBrian Somers         return 2;
92808676022SBrian Somers       }
929d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
93008676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
93108676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
93208676022SBrian Somers                hwaddr.sdl_alen);
93308676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
93408676022SBrian Somers       } else {
935dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
936259a6191SBrian Somers                   inet_ntoa(addr));
93708676022SBrian Somers         close(s);
93808676022SBrian Somers         return 4;
93908676022SBrian Somers       }
94008676022SBrian Somers       close(s);
94149052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
94249052c95SBrian Somers       int f;
94349052c95SBrian Somers 
94449052c95SBrian Somers       randinit();
94549052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
94649052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
94708676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
94849052c95SBrian Somers       mp->cfg.enddisc.len = 20;
94949052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
95049052c95SBrian Somers       if (arg->argc > arg->argn+1) {
95108676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
95249052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
95349052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
95449052c95SBrian Somers       } else {
955dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
95608676022SBrian Somers         return 5;
95749052c95SBrian Somers       }
95849052c95SBrian Somers     } else {
959dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
96049052c95SBrian Somers                 arg->argv[arg->argn]);
96108676022SBrian Somers       return 6;
96249052c95SBrian Somers     }
963e43ebac1SBrian Somers   }
96449052c95SBrian Somers 
96549052c95SBrian Somers   return 0;
96649052c95SBrian Somers }
9671fa665f5SBrian Somers 
9681fa665f5SBrian Somers static int
9691fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
9701fa665f5SBrian Somers                    int *n)
9711fa665f5SBrian Somers {
9721fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9730f2f3eb3SBrian Somers   int result;
9741fa665f5SBrian Somers 
9750f2f3eb3SBrian Somers   result = 0;
9766f384573SBrian Somers   if (s->send.dl != NULL) {
97796c9bb21SBrian Somers     /* We've connect()ed */
978b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
979b7c5748eSBrian Somers         !s->send.dl->physical->out) {
980b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
9810f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
98296c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
9836f384573SBrian Somers       s->send.dl = NULL;
98496c9bb21SBrian Somers       s->fd = -1;
985ea722969SBrian Somers     } else
986ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
9870f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
98896c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
9891fa665f5SBrian Somers     if (*n < s->fd + 1)
9901fa665f5SBrian Somers       *n = s->fd + 1;
9911fa665f5SBrian Somers     FD_SET(s->fd, r);
99224989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
9930f2f3eb3SBrian Somers     result++;
9941fa665f5SBrian Somers   }
9950f2f3eb3SBrian Somers   return result;
9961fa665f5SBrian Somers }
9971fa665f5SBrian Somers 
9981fa665f5SBrian Somers static int
9991fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset)
10001fa665f5SBrian Somers {
10011fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10021fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
10031fa665f5SBrian Somers }
10041fa665f5SBrian Somers 
10051fa665f5SBrian Somers static void
10061fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
10071fa665f5SBrian Somers {
10081fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10096f384573SBrian Somers   struct sockaddr in;
10101fa665f5SBrian Somers   int fd, size;
10111fa665f5SBrian Somers 
10126f384573SBrian Somers   size = sizeof in;
10136f384573SBrian Somers   fd = accept(s->fd, &in, &size);
10141fa665f5SBrian Somers   if (fd < 0) {
1015dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno));
10161fa665f5SBrian Somers     return;
10171fa665f5SBrian Somers   }
10181fa665f5SBrian Somers 
10191bc9b5baSBrian Somers   if (in.sa_family == AF_LOCAL)
102096c9bb21SBrian Somers     bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in);
102154cd8e13SBrian Somers   else
10221fa665f5SBrian Somers     close(fd);
10231fa665f5SBrian Somers }
10241fa665f5SBrian Somers 
10251af29a6eSBrian Somers static int
10261fa665f5SBrian Somers mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
10271fa665f5SBrian Somers {
10281fa665f5SBrian Somers   /* We never want to write here ! */
1029a33b2ef7SBrian Somers   log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
10301af29a6eSBrian Somers   return 0;
10311fa665f5SBrian Somers }
10321fa665f5SBrian Somers 
10331fa665f5SBrian Somers void
10341fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
10351fa665f5SBrian Somers {
10361fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
10371fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
10381fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
10391fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
10401fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
10416f384573SBrian Somers   s->send.dl = NULL;
10421fa665f5SBrian Somers   s->fd = -1;
10436f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
10441fa665f5SBrian Somers }
10451fa665f5SBrian Somers 
10461fa665f5SBrian Somers int
10471fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
10481fa665f5SBrian Somers {
104996c9bb21SBrian Somers   int f, l;
10501fa665f5SBrian Somers   mode_t mask;
10511fa665f5SBrian Somers 
10521fa665f5SBrian Somers   if (s->fd != -1) {
1053a33b2ef7SBrian Somers     log_Printf(LogALERT, "Internal error !  mpserver already open\n");
10546f384573SBrian Somers     mpserver_Close(s);
10551fa665f5SBrian Somers   }
10561fa665f5SBrian Somers 
10576f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
10586f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
10591fa665f5SBrian Somers 
10606f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
10616f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
10621fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
10636f384573SBrian Somers     l += 2;
10641fa665f5SBrian Somers   }
10651fa665f5SBrian Somers 
10666f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
10676f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
10681fa665f5SBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
10691fa665f5SBrian Somers   if (s->fd < 0) {
1070dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno));
107196c9bb21SBrian Somers     return MPSERVER_FAILED;
10721fa665f5SBrian Somers   }
10731fa665f5SBrian Somers 
10746f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
10756f384573SBrian Somers              sizeof s->socket);
10761fa665f5SBrian Somers   mask = umask(0177);
10776f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
10786f384573SBrian Somers     if (errno != EADDRINUSE) {
1079dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
10806f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10811fa665f5SBrian Somers       umask(mask);
10821fa665f5SBrian Somers       close(s->fd);
10831fa665f5SBrian Somers       s->fd = -1;
108496c9bb21SBrian Somers       return MPSERVER_FAILED;
10856f384573SBrian Somers     }
10866f384573SBrian Somers     umask(mask);
10876f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
1088dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
10896f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10906f384573SBrian Somers       if (errno == ECONNREFUSED)
1091dd7e2610SBrian Somers         log_Printf(LogPHASE, "          Has the previous server died badly ?\n");
10926f384573SBrian Somers       close(s->fd);
10936f384573SBrian Somers       s->fd = -1;
109496c9bb21SBrian Somers       return MPSERVER_FAILED;
10956f384573SBrian Somers     }
10966f384573SBrian Somers 
10976f384573SBrian Somers     /* Donate our link to the other guy */
109896c9bb21SBrian Somers     return MPSERVER_CONNECTED;
10991fa665f5SBrian Somers   }
11006f384573SBrian Somers 
11016f384573SBrian Somers   /* Listen for other ppp invocations that want to donate links */
11021fa665f5SBrian Somers   if (listen(s->fd, 5) != 0) {
1103dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: Unable to listen to socket"
11041fa665f5SBrian Somers               " - BUNDLE overload?\n");
11051fa665f5SBrian Somers     mpserver_Close(s);
11061fa665f5SBrian Somers   }
11071fa665f5SBrian Somers 
110896c9bb21SBrian Somers   return MPSERVER_LISTENING;
11091fa665f5SBrian Somers }
11101fa665f5SBrian Somers 
11111fa665f5SBrian Somers void
11121fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
11131fa665f5SBrian Somers {
11146f384573SBrian Somers   if (s->send.dl != NULL) {
111596c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
11166f384573SBrian Somers     s->send.dl = NULL;
111796c9bb21SBrian Somers     s->fd = -1;
111896c9bb21SBrian Somers   } else if (s->fd >= 0) {
11191fa665f5SBrian Somers     close(s->fd);
11206f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1121dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
11226f384573SBrian Somers                 strerror(errno));
11236f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
11241fa665f5SBrian Somers     s->fd = -1;
11251fa665f5SBrian Somers   }
11261fa665f5SBrian Somers }
112786b1f0d7SBrian Somers 
112886b1f0d7SBrian Somers void
112986b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
113086b1f0d7SBrian Somers {
113186b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
113286b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
11335d9e6103SBrian Somers     mp_Assemble(mp, NULL, NULL);
113486b1f0d7SBrian Somers }
11356f8e9f0aSBrian Somers 
11366f8e9f0aSBrian Somers void
11376f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
11386f8e9f0aSBrian Somers {
11396f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
11406f8e9f0aSBrian Somers }
1141