xref: /freebsd/usr.sbin/ppp/mp.c (revision 26af0ae96638b0453bcc87cae6031eb0b3988171)
13b0f8d2eSBrian Somers /*-
23b0f8d2eSBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
33b0f8d2eSBrian Somers  * All rights reserved.
43b0f8d2eSBrian Somers  *
53b0f8d2eSBrian Somers  * Redistribution and use in source and binary forms, with or without
63b0f8d2eSBrian Somers  * modification, are permitted provided that the following conditions
73b0f8d2eSBrian Somers  * are met:
83b0f8d2eSBrian Somers  * 1. Redistributions of source code must retain the above copyright
93b0f8d2eSBrian Somers  *    notice, this list of conditions and the following disclaimer.
103b0f8d2eSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
113b0f8d2eSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
123b0f8d2eSBrian Somers  *    documentation and/or other materials provided with the distribution.
133b0f8d2eSBrian Somers  *
143b0f8d2eSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153b0f8d2eSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163b0f8d2eSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173b0f8d2eSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183b0f8d2eSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193b0f8d2eSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203b0f8d2eSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213b0f8d2eSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223b0f8d2eSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233b0f8d2eSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243b0f8d2eSBrian Somers  * SUCH DAMAGE.
253b0f8d2eSBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
273b0f8d2eSBrian Somers  */
283b0f8d2eSBrian Somers 
29972a1bcfSBrian Somers #include <sys/param.h>
303b0f8d2eSBrian Somers #include <netinet/in.h>
313b0f8d2eSBrian Somers #include <netinet/in_systm.h>
323b0f8d2eSBrian Somers #include <netinet/ip.h>
3349052c95SBrian Somers #include <arpa/inet.h>
3408676022SBrian Somers #include <net/if_dl.h>
3508676022SBrian Somers #include <sys/socket.h>
361fa665f5SBrian Somers #include <sys/un.h>
373b0f8d2eSBrian Somers 
3808676022SBrian Somers #include <errno.h>
391fa665f5SBrian Somers #include <paths.h>
403b0f8d2eSBrian Somers #include <stdlib.h>
4149052c95SBrian Somers #include <stdio.h>
423b0f8d2eSBrian Somers #include <string.h>
431fa665f5SBrian Somers #include <sys/stat.h>
443b0f8d2eSBrian Somers #include <termios.h>
4508676022SBrian Somers #include <unistd.h>
463b0f8d2eSBrian Somers 
475d9e6103SBrian Somers #include "layer.h"
4867b072f7SBrian Somers #ifndef NONAT
49f02c2029SBrian Somers #include "nat_cmd.h"
505d9e6103SBrian Somers #endif
515d9e6103SBrian Somers #include "vjcomp.h"
529e8ec64bSBrian Somers #include "ua.h"
53c9e11a11SBrian Somers #include "defs.h"
543b0f8d2eSBrian Somers #include "command.h"
553b0f8d2eSBrian Somers #include "mbuf.h"
563b0f8d2eSBrian Somers #include "log.h"
573b0f8d2eSBrian Somers #include "timer.h"
583b0f8d2eSBrian Somers #include "fsm.h"
593b0f8d2eSBrian Somers #include "iplist.h"
603b0f8d2eSBrian Somers #include "throughput.h"
613b0f8d2eSBrian Somers #include "slcompress.h"
625a72b6edSBrian Somers #include "lqr.h"
635a72b6edSBrian Somers #include "hdlc.h"
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 
882cb305afSBrian Somers /*
892cb305afSBrian Somers  * When we set our MP socket buffer size, we need some extra space
902cb305afSBrian Somers  * for the kernel to use to transfer the file descriptors and their
912cb305afSBrian Somers  * control structure.  In practice, this seems to be
922cb305afSBrian Somers  *
932cb305afSBrian Somers  *    sizeof(struct msghdr) + sizeof(int) * SEND_MAXFD
942cb305afSBrian Somers  *
952cb305afSBrian Somers  * (see bundle.c for SEND_MAXFD), but as this isn't actually documented,
962cb305afSBrian Somers  * we just add ``a bit extra''
972cb305afSBrian Somers  */
982cb305afSBrian Somers #define SOCKET_OVERHEAD	100
992cb305afSBrian Somers 
100643f4904SBrian Somers void
101643f4904SBrian Somers peerid_Init(struct peerid *peer)
102643f4904SBrian Somers {
103643f4904SBrian Somers   peer->enddisc.class = 0;
104643f4904SBrian Somers   *peer->enddisc.address = '\0';
105643f4904SBrian Somers   peer->enddisc.len = 0;
106643f4904SBrian Somers   *peer->authname = '\0';
107643f4904SBrian Somers }
108643f4904SBrian Somers 
109643f4904SBrian Somers int
110643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
111643f4904SBrian Somers {
112643f4904SBrian Somers   return !strcmp(p1->authname, p2->authname) &&
113643f4904SBrian Somers          p1->enddisc.class == p2->enddisc.class &&
114643f4904SBrian Somers          p1->enddisc.len == p2->enddisc.len &&
115643f4904SBrian Somers          !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
116643f4904SBrian Somers }
117643f4904SBrian Somers 
1183b0f8d2eSBrian Somers static u_int32_t
119147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
1203b0f8d2eSBrian Somers {
1213b0f8d2eSBrian Somers   seq++;
122147613eaSBrian Somers   if (is12bit) {
1233b0f8d2eSBrian Somers     if (seq & 0xfffff000)
1243b0f8d2eSBrian Somers       seq = 0;
1253b0f8d2eSBrian Somers   } else if (seq & 0xff000000)
1263b0f8d2eSBrian Somers     seq = 0;
1273b0f8d2eSBrian Somers   return seq;
1283b0f8d2eSBrian Somers }
1293b0f8d2eSBrian Somers 
1303b0f8d2eSBrian Somers static int
1311bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1321bcced10SBrian Somers {
133b4b280abSBrian Somers   u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
134b4b280abSBrian Somers 
135b4b280abSBrian Somers   if (seq1 > max) {
136b4b280abSBrian Somers     if (seq2 < 0x200 || seq2 > seq1)
137b4b280abSBrian Somers       return 1;
138b4b280abSBrian Somers   } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
139b4b280abSBrian Somers     return 1;
140b4b280abSBrian Somers 
141b4b280abSBrian Somers   return 0;
1421bcced10SBrian Somers }
1431bcced10SBrian Somers 
1441bcced10SBrian Somers static int
1453b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1463b0f8d2eSBrian Somers {
14749052c95SBrian Somers   if (mp->local_is12bit) {
1489e8ec64bSBrian Somers     u_int16_t val;
1499e8ec64bSBrian Somers 
1509e8ec64bSBrian Somers     ua_ntohs(MBUF_CTOP(m), &val);
1519e8ec64bSBrian Somers     if (val & 0x3000) {
152dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1533b0f8d2eSBrian Somers       return 0;
1543b0f8d2eSBrian Somers     }
1559e8ec64bSBrian Somers     header->begin = val & 0x8000 ? 1 : 0;
1569e8ec64bSBrian Somers     header->end = val & 0x4000 ? 1 : 0;
1579e8ec64bSBrian Somers     header->seq = val & 0x0fff;
1583b0f8d2eSBrian Somers     return 2;
1593b0f8d2eSBrian Somers   } else {
1609e8ec64bSBrian Somers     ua_ntohl(MBUF_CTOP(m), &header->seq);
1613b0f8d2eSBrian Somers     if (header->seq & 0x3f000000) {
162dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1633b0f8d2eSBrian Somers       return 0;
1643b0f8d2eSBrian Somers     }
165ce828a6eSBrian Somers     header->begin = header->seq & 0x80000000 ? 1 : 0;
166ce828a6eSBrian Somers     header->end = header->seq & 0x40000000 ? 1 : 0;
1673b0f8d2eSBrian Somers     header->seq &= 0x00ffffff;
1683b0f8d2eSBrian Somers     return 4;
1693b0f8d2eSBrian Somers   }
1703b0f8d2eSBrian Somers }
1713b0f8d2eSBrian Somers 
1723b0f8d2eSBrian Somers static void
1733b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp)
1743b0f8d2eSBrian Somers {
17549052c95SBrian Somers   /* The given FSM (ccp) is about to start up ! */
1763b0f8d2eSBrian Somers }
1773b0f8d2eSBrian Somers 
1783b0f8d2eSBrian Somers static void
1793b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp)
1803b0f8d2eSBrian Somers {
18149052c95SBrian Somers   /* The given fsm (ccp) is now up */
1823b0f8d2eSBrian Somers }
1833b0f8d2eSBrian Somers 
1843b0f8d2eSBrian Somers static void
1853b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp)
1863b0f8d2eSBrian Somers {
18749052c95SBrian Somers   /* The given FSM (ccp) has been told to come down */
1883b0f8d2eSBrian Somers }
1893b0f8d2eSBrian Somers 
1903b0f8d2eSBrian Somers static void
1913b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp)
1923b0f8d2eSBrian Somers {
19349052c95SBrian Somers   /* The given fsm (ccp) is now down */
1940a1b5c9dSBrian Somers   if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1950a1b5c9dSBrian Somers     fsm_Open(fp);		/* CCP goes to ST_STOPPED */
1963b0f8d2eSBrian Somers }
1973b0f8d2eSBrian Somers 
198ab2de065SBrian Somers static void
199ab2de065SBrian Somers mp_UpDown(void *v)
200ab2de065SBrian Somers {
201ab2de065SBrian Somers   struct mp *mp = (struct mp *)v;
202ab2de065SBrian Somers   int percent;
203ab2de065SBrian Somers 
204ab2de065SBrian Somers   percent = mp->link.throughput.OctetsPerSecond * 800 / mp->bundle->bandwidth;
205ab2de065SBrian Somers   if (percent >= mp->cfg.autoload.max) {
206ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
207ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
208ab2de065SBrian Somers   } else if (percent <= mp->cfg.autoload.min) {
209ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
210ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
211ab2de065SBrian Somers   }
212ab2de065SBrian Somers }
213ab2de065SBrian Somers 
214ab2de065SBrian Somers void
215ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp)
216ab2de065SBrian Somers {
217ab2de065SBrian Somers   throughput_stop(&mp->link.throughput);
218ab2de065SBrian Somers }
219ab2de065SBrian Somers 
220ab2de065SBrian Somers void
221ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp)
222ab2de065SBrian Somers {
223ab2de065SBrian Somers   if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period) {
224ab2de065SBrian Somers     throughput_destroy(&mp->link.throughput);
225ab2de065SBrian Somers     throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
226ab2de065SBrian Somers     throughput_callback(&mp->link.throughput, mp_UpDown, mp);
227ab2de065SBrian Somers   }
228ab2de065SBrian Somers 
229ab2de065SBrian Somers   if (bundle_WantAutoloadTimer(mp->bundle))
230ab2de065SBrian Somers     throughput_start(&mp->link.throughput, "MP throughput", 1);
231ab2de065SBrian Somers   else
232ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
233ab2de065SBrian Somers }
234ab2de065SBrian Somers 
235ab2de065SBrian Somers void
236ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp)
237ab2de065SBrian Somers {
238ab2de065SBrian Somers   if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period)
239ab2de065SBrian Somers     mp_CheckAutoloadTimer(mp);
240ab2de065SBrian Somers   else
241ab2de065SBrian Somers     throughput_clear(&mp->link.throughput, THROUGHPUT_OVERALL, NULL);
242ab2de065SBrian Somers }
243ab2de065SBrian Somers 
2443b0f8d2eSBrian Somers void
2453b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
2463b0f8d2eSBrian Somers {
24749052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
24849052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
249643f4904SBrian Somers 
250643f4904SBrian Somers   peerid_Init(&mp->peer);
251643f4904SBrian Somers 
2529c53a7b1SBrian Somers   mp->out.seq = 0;
2539c53a7b1SBrian Somers   mp->out.link = 0;
2543b0f8d2eSBrian Somers   mp->seq.min_in = 0;
2553b0f8d2eSBrian Somers   mp->seq.next_in = 0;
2563b0f8d2eSBrian Somers   mp->inbufs = NULL;
2573b0f8d2eSBrian Somers   mp->bundle = bundle;
2583b0f8d2eSBrian Somers 
2595d9e6103SBrian Somers   mp->link.type = LOGICAL_LINK;
2603b0f8d2eSBrian Somers   mp->link.name = "mp";
2613b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
262643f4904SBrian Somers 
263ab2de065SBrian Somers   mp->cfg.autoload.period = SAMPLE_PERIOD;
264ab2de065SBrian Somers   mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
265ab2de065SBrian Somers   throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
266ab2de065SBrian Somers   throughput_callback(&mp->link.throughput, mp_UpDown, mp);
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);
3071fa665f5SBrian Somers       return MP_FAILED;
308643f4904SBrian Somers     }
3091fa665f5SBrian Somers     if (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) {
313dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
3141fa665f5SBrian Somers                 dl->name);
3151fa665f5SBrian Somers       return MP_FAILED;
31649052c95SBrian Somers     }
3171fa665f5SBrian Somers     return MP_ADDED;
31849052c95SBrian Somers   } else {
31949052c95SBrian Somers     /* First link in multilink mode */
32049052c95SBrian Somers 
3211fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
3221fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
3231fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
3241fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
3251fa665f5SBrian Somers     mp->peer = dl->peer;
32649052c95SBrian Somers 
327ab2de065SBrian Somers     throughput_destroy(&mp->link.throughput);
328ab2de065SBrian Somers     throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
329ab2de065SBrian Somers     throughput_callback(&mp->link.throughput, mp_UpDown, mp);
330673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
331673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
332673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
333673903ecSBrian Somers 
3349c53a7b1SBrian Somers     mp->out.seq = 0;
3359c53a7b1SBrian Somers     mp->out.link = 0;
336673903ecSBrian Somers     mp->seq.min_in = 0;
337673903ecSBrian Somers     mp->seq.next_in = 0;
338673903ecSBrian Somers 
3391fa665f5SBrian Somers     /*
3401fa665f5SBrian Somers      * Now we create our server socket.
3411fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
3421fa665f5SBrian Somers      */
34396c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
34496c9bb21SBrian Somers     case MPSERVER_CONNECTED:
345dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
3466f384573SBrian Somers                 mp->server.socket.sun_path);
34796c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
3481fa665f5SBrian Somers       return MP_LINKSENT;
34996c9bb21SBrian Somers     case MPSERVER_FAILED:
3501fa665f5SBrian Somers       return MP_FAILED;
35196c9bb21SBrian Somers     case MPSERVER_LISTENING:
352dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
353dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
3541fa665f5SBrian Somers 
35549052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
356ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
3573b0f8d2eSBrian Somers 
3583b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
35906337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
360dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
361dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
36206337856SBrian Somers       }
3633b0f8d2eSBrian Somers 
3643b0f8d2eSBrian Somers       mp->active = 1;
36596c9bb21SBrian Somers       break;
36649052c95SBrian Somers     }
3671fa665f5SBrian Somers   }
3683b0f8d2eSBrian Somers 
3691fa665f5SBrian Somers   return MP_UP;
3703b0f8d2eSBrian Somers }
3713b0f8d2eSBrian Somers 
3723b0f8d2eSBrian Somers void
373673903ecSBrian Somers mp_Down(struct mp *mp)
374673903ecSBrian Somers {
375673903ecSBrian Somers   if (mp->active) {
376673903ecSBrian Somers     struct mbuf *next;
377673903ecSBrian Somers 
378ab2de065SBrian Somers     /* Stop that ! */
379ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
380ab2de065SBrian Somers 
3811fa665f5SBrian Somers     /* Don't want any more of these */
3821fa665f5SBrian Somers     mpserver_Close(&mp->server);
3831fa665f5SBrian Somers 
3841fa665f5SBrian Somers     /* CCP goes down with a bang */
38509206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
386673903ecSBrian Somers 
387673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
388673903ecSBrian Somers     while (mp->inbufs) {
38926af0ae9SBrian Somers       next = mp->inbufs->m_nextpkt;
39026af0ae9SBrian Somers       m_freem(mp->inbufs);
391673903ecSBrian Somers       mp->inbufs = next;
392673903ecSBrian Somers     }
393673903ecSBrian Somers 
394643f4904SBrian Somers     peerid_Init(&mp->peer);
395673903ecSBrian Somers     mp->active = 0;
396673903ecSBrian Somers   }
397673903ecSBrian Somers }
398673903ecSBrian Somers 
399673903ecSBrian Somers void
4003b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
4013b0f8d2eSBrian Somers {
4023b0f8d2eSBrian Somers   mplink->seq = 0;
403ab2de065SBrian Somers   mplink->bandwidth = 0;
4043b0f8d2eSBrian Somers }
4053b0f8d2eSBrian Somers 
4065d9e6103SBrian Somers static void
4075d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
4083b0f8d2eSBrian Somers {
4093b0f8d2eSBrian Somers   struct mp_header mh, h;
4103b0f8d2eSBrian Somers   struct mbuf *q, *last;
4113b0f8d2eSBrian Somers   int32_t seq;
4123b0f8d2eSBrian Somers 
41386b1f0d7SBrian Somers   /*
41486b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
41586b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
41686b1f0d7SBrian Somers    * as normal
41786b1f0d7SBrian Somers    */
41886b1f0d7SBrian Somers 
41986b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
42026af0ae9SBrian Somers     m_freem(m);
4213b0f8d2eSBrian Somers     return;
4223b0f8d2eSBrian Somers   }
4233b0f8d2eSBrian Somers 
42486b1f0d7SBrian Somers   if (p) {
4253b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
4263b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
42786b1f0d7SBrian Somers   } else
42886b1f0d7SBrian Somers     seq = mp->seq.min_in;
42986b1f0d7SBrian Somers 
4303b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
4313b0f8d2eSBrian Somers     /*
4323b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
4333b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
4343b0f8d2eSBrian Somers      */
4353b0f8d2eSBrian Somers     struct datalink *dl;
4363b0f8d2eSBrian Somers 
43786b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
4383b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
43986b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
44086b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
44186b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
4423b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
4433b0f8d2eSBrian Somers   }
4443b0f8d2eSBrian Somers 
4453b0f8d2eSBrian Somers   /*
4463b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
4473b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
4483b0f8d2eSBrian Somers    * the queue.
4493b0f8d2eSBrian Somers    */
4503b0f8d2eSBrian Somers 
4513b0f8d2eSBrian Somers   if (!mp->inbufs) {
4523b0f8d2eSBrian Somers     mp->inbufs = m;
4533b0f8d2eSBrian Somers     m = NULL;
4543b0f8d2eSBrian Somers   }
4553b0f8d2eSBrian Somers 
4563b0f8d2eSBrian Somers   last = NULL;
4573b0f8d2eSBrian Somers   seq = mp->seq.next_in;
4583b0f8d2eSBrian Somers   q = mp->inbufs;
4593b0f8d2eSBrian Somers   while (q) {
4603b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
4611bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
4623b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
4633b0f8d2eSBrian Somers       if (last)
46426af0ae9SBrian Somers         last->m_nextpkt = m;
4653b0f8d2eSBrian Somers       else
4663b0f8d2eSBrian Somers         mp->inbufs = m;
46726af0ae9SBrian Somers       m->m_nextpkt = q;
4683b0f8d2eSBrian Somers       q = m;
4693b0f8d2eSBrian Somers       h = mh;
4703b0f8d2eSBrian Somers       m = NULL;
4713b0f8d2eSBrian Somers     }
4723b0f8d2eSBrian Somers 
4733b0f8d2eSBrian Somers     if (h.seq != seq) {
4743b0f8d2eSBrian Somers       /* we're missing something :-( */
47564e0f466SBrian Somers       if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
4763b0f8d2eSBrian Somers         /* we're never gonna get it */
4773b0f8d2eSBrian Somers         struct mbuf *next;
4783b0f8d2eSBrian Somers 
4793b0f8d2eSBrian Somers         /* Zap all older fragments */
4803b0f8d2eSBrian Somers         while (mp->inbufs != q) {
481dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
48226af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
48326af0ae9SBrian Somers           m_freem(mp->inbufs);
4843b0f8d2eSBrian Somers           mp->inbufs = next;
4853b0f8d2eSBrian Somers         }
4863b0f8d2eSBrian Somers 
4873b0f8d2eSBrian Somers         /*
4883b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4893b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4903b0f8d2eSBrian Somers          * comes first.
4913b0f8d2eSBrian Somers          */
4923b0f8d2eSBrian Somers         do {
4933b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4943b0f8d2eSBrian Somers           if (h.begin) {
495ce828a6eSBrian Somers             /* We might be able to process this ! */
4963b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4973b0f8d2eSBrian Somers             break;
4983b0f8d2eSBrian Somers           }
49926af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
500dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
50126af0ae9SBrian Somers           m_freem(mp->inbufs);
5023b0f8d2eSBrian Somers           mp->inbufs = next;
50364e0f466SBrian Somers         } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
50464e0f466SBrian Somers                                          h.seq) || h.end));
5053b0f8d2eSBrian Somers 
5063b0f8d2eSBrian Somers         /*
5073b0f8d2eSBrian Somers          * Continue processing things from here.
5083b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
5093b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
5103b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
5113b0f8d2eSBrian Somers          * packets that have already been received.
5123b0f8d2eSBrian Somers          */
5133b0f8d2eSBrian Somers 
514147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5153b0f8d2eSBrian Somers         last = NULL;
5163b0f8d2eSBrian Somers         q = mp->inbufs;
5173b0f8d2eSBrian Somers       } else
5183b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
5193b0f8d2eSBrian Somers         break;
5203b0f8d2eSBrian Somers     } else if (h.end) {
5213b0f8d2eSBrian Somers       /* We've got something, reassemble */
5223b0f8d2eSBrian Somers       struct mbuf **frag = &q;
5233b0f8d2eSBrian Somers       int len;
524ce828a6eSBrian Somers       u_long first = -1;
5253b0f8d2eSBrian Somers 
5263b0f8d2eSBrian Somers       do {
5273b0f8d2eSBrian Somers         *frag = mp->inbufs;
52826af0ae9SBrian Somers         mp->inbufs = mp->inbufs->m_nextpkt;
5293b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
530ce828a6eSBrian Somers         if (first == -1)
531ce828a6eSBrian Somers           first = h.seq;
53226af0ae9SBrian Somers         (*frag)->m_offset += len;
53326af0ae9SBrian Somers         (*frag)->m_len -= len;
53426af0ae9SBrian Somers         (*frag)->m_nextpkt = NULL;
5353b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
536dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
5373b0f8d2eSBrian Somers                     (u_long)h.seq);
53826af0ae9SBrian Somers           m_freem(q);
5393b0f8d2eSBrian Somers           q = NULL;
5403b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
541dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
5423b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
5433b0f8d2eSBrian Somers           /*
5443b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
5453b0f8d2eSBrian Somers            * our half-assembed packet.
5463b0f8d2eSBrian Somers            */
54726af0ae9SBrian Somers           (*frag)->m_nextpkt = mp->inbufs;
5483b0f8d2eSBrian Somers           mp->inbufs = *frag;
5493b0f8d2eSBrian Somers           *frag = NULL;
55026af0ae9SBrian Somers           m_freem(q);
5513b0f8d2eSBrian Somers           q = NULL;
5523b0f8d2eSBrian Somers           frag = &q;
5533b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
5543b0f8d2eSBrian Somers         } else
5553b0f8d2eSBrian Somers           do
55626af0ae9SBrian Somers             frag = &(*frag)->m_next;
557ce828a6eSBrian Somers           while (*frag != NULL);
5583b0f8d2eSBrian Somers       } while (!h.end);
5593b0f8d2eSBrian Somers 
5603b0f8d2eSBrian Somers       if (q) {
56126af0ae9SBrian Somers         q = m_pullup(q);
562dd7e2610SBrian Somers         log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
56326af0ae9SBrian Somers                    first, (u_long)h.seq, m_length(q));
56426af0ae9SBrian Somers         link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
56526af0ae9SBrian Somers         m_freem(q);
5663b0f8d2eSBrian Somers       }
5673b0f8d2eSBrian Somers 
568147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5693b0f8d2eSBrian Somers       last = NULL;
5703b0f8d2eSBrian Somers       q = mp->inbufs;
5713b0f8d2eSBrian Somers     } else {
5723b0f8d2eSBrian Somers       /* Look for the next fragment */
573147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5743b0f8d2eSBrian Somers       last = q;
57526af0ae9SBrian Somers       q = q->m_nextpkt;
5763b0f8d2eSBrian Somers     }
5773b0f8d2eSBrian Somers   }
5783b0f8d2eSBrian Somers 
5793b0f8d2eSBrian Somers   if (m) {
5803b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5813b0f8d2eSBrian Somers     last = NULL;
58226af0ae9SBrian Somers     for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
5833b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5841bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
585147613eaSBrian Somers         break;
586147613eaSBrian Somers     }
587147613eaSBrian Somers     /* Our received fragment fits in here */
5883b0f8d2eSBrian Somers     if (last)
58926af0ae9SBrian Somers       last->m_nextpkt = m;
5903b0f8d2eSBrian Somers     else
5913b0f8d2eSBrian Somers       mp->inbufs = m;
59226af0ae9SBrian Somers     m->m_nextpkt = q;
5933b0f8d2eSBrian Somers   }
5943b0f8d2eSBrian Somers }
5953b0f8d2eSBrian Somers 
5965d9e6103SBrian Somers struct mbuf *
5975d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
5985d9e6103SBrian Somers {
5995d9e6103SBrian Somers   struct physical *p = link2physical(l);
6005d9e6103SBrian Somers 
6015d9e6103SBrian Somers   if (!bundle->ncp.mp.active)
6025d9e6103SBrian Somers     /* Let someone else deal with it ! */
6035d9e6103SBrian Somers     return bp;
6045d9e6103SBrian Somers 
6055d9e6103SBrian Somers   if (p == NULL) {
6065d9e6103SBrian Somers     log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
60726af0ae9SBrian Somers     m_freem(bp);
608411675baSBrian Somers   } else {
60926af0ae9SBrian Somers     m_settype(bp, MB_MPIN);
6105d9e6103SBrian Somers     mp_Assemble(&bundle->ncp.mp, bp, p);
611411675baSBrian Somers   }
6125d9e6103SBrian Somers 
6135d9e6103SBrian Somers   return NULL;
6145d9e6103SBrian Somers }
6155d9e6103SBrian Somers 
6163b0f8d2eSBrian Somers static void
6175d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
6185d9e6103SBrian Somers           struct mbuf *m, u_int32_t begin, u_int32_t end)
6193b0f8d2eSBrian Somers {
62026af0ae9SBrian Somers   char prepend[4];
6213b0f8d2eSBrian Somers 
622ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
62326af0ae9SBrian Somers 
62449052c95SBrian Somers   if (mp->peer_is12bit) {
6259e8ec64bSBrian Somers     u_int16_t val;
626ce828a6eSBrian Somers 
6279e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
62826af0ae9SBrian Somers     ua_htons(&val, prepend);
62926af0ae9SBrian Somers     m = m_prepend(m, prepend, 2, 0);
6303b0f8d2eSBrian Somers   } else {
6319e8ec64bSBrian Somers     u_int32_t val;
632ce828a6eSBrian Somers 
6339e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
63426af0ae9SBrian Somers     ua_htonl(&val, prepend);
63526af0ae9SBrian Somers     m = m_prepend(m, prepend, 4, 0);
6363b0f8d2eSBrian Somers   }
637dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
6389c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
63926af0ae9SBrian Somers                mp->out.seq, m_length(m), l->name);
640147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
6413b0f8d2eSBrian Somers 
64226af0ae9SBrian Somers   link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
6433b0f8d2eSBrian Somers }
6443b0f8d2eSBrian Somers 
6453b0f8d2eSBrian Somers int
6463b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
6473b0f8d2eSBrian Somers {
6483b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
6499c53a7b1SBrian Somers   struct datalink *dl, *fdl;
65026af0ae9SBrian Somers   size_t total, add, len;
65126af0ae9SBrian Somers   int thislink, nlinks;
652b4b280abSBrian Somers   u_int32_t begin, end;
6533b0f8d2eSBrian Somers   struct mbuf *m, *mo;
6543b0f8d2eSBrian Somers 
6559c53a7b1SBrian Somers   thislink = nlinks = 0;
6569c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
65786b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
6589c53a7b1SBrian Somers     if (!fdl) {
6599c53a7b1SBrian Somers       if (thislink == mp->out.link)
6609c53a7b1SBrian Somers         fdl = dl;
6619c53a7b1SBrian Somers       else
6629c53a7b1SBrian Somers         thislink++;
6639c53a7b1SBrian Somers     }
6649c53a7b1SBrian Somers     nlinks++;
6659c53a7b1SBrian Somers   }
6669c53a7b1SBrian Somers 
6679c53a7b1SBrian Somers   if (!fdl) {
6689c53a7b1SBrian Somers     fdl = bundle->links;
6691bc9b5baSBrian Somers     if (!fdl)
6701bc9b5baSBrian Somers       return 0;
6719c53a7b1SBrian Somers     thislink = 0;
6729c53a7b1SBrian Somers   }
6733b0f8d2eSBrian Somers 
6743b0f8d2eSBrian Somers   total = 0;
6759c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6769c53a7b1SBrian Somers     if (!dl) {
6779c53a7b1SBrian Somers       dl = bundle->links;
6789c53a7b1SBrian Somers       thislink = 0;
6799c53a7b1SBrian Somers     }
6809c53a7b1SBrian Somers 
6816f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
6826f384573SBrian Somers       continue;
6839c53a7b1SBrian Somers 
6843b0f8d2eSBrian Somers     if (dl->physical->out)
6853b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
6863b0f8d2eSBrian Somers       continue;
6879c53a7b1SBrian Somers 
6883b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
6893b0f8d2eSBrian Somers     total += add;
6903b0f8d2eSBrian Somers     if (add)
6913b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6923b0f8d2eSBrian Somers       continue;
6939c53a7b1SBrian Somers 
6945d9e6103SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
6953b0f8d2eSBrian Somers       /* Nothing else to send */
6963b0f8d2eSBrian Somers       break;
6973b0f8d2eSBrian Somers 
6983b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
69926af0ae9SBrian Somers     len = m_length(m);
7003b0f8d2eSBrian Somers     begin = 1;
7013b0f8d2eSBrian Somers     end = 0;
7023b0f8d2eSBrian Somers 
7039c53a7b1SBrian Somers     while (!end) {
7049c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
705ab2de065SBrian Somers         /* Write at most his_mru bytes to the physical link */
706ab2de065SBrian Somers         if (len <= dl->physical->link.lcp.his_mru) {
7073b0f8d2eSBrian Somers           mo = m;
7083b0f8d2eSBrian Somers           end = 1;
70926af0ae9SBrian Somers           m_settype(mo, MB_MPOUT);
7103b0f8d2eSBrian Somers         } else {
711ab2de065SBrian Somers           /* It's > his_mru, chop the packet (`m') into bits */
71226af0ae9SBrian Somers           mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
71326af0ae9SBrian Somers           len -= mo->m_len;
71426af0ae9SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
7153b0f8d2eSBrian Somers         }
7165d9e6103SBrian Somers         mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
7173b0f8d2eSBrian Somers         begin = 0;
7183b0f8d2eSBrian Somers       }
7199c53a7b1SBrian Somers 
7209c53a7b1SBrian Somers       if (!end) {
7219c53a7b1SBrian Somers         nlinks--;
7229c53a7b1SBrian Somers         dl = dl->next;
7239c53a7b1SBrian Somers         if (!dl) {
7249c53a7b1SBrian Somers           dl = bundle->links;
7259c53a7b1SBrian Somers           thislink = 0;
7269c53a7b1SBrian Somers         } else
7279c53a7b1SBrian Somers           thislink++;
7283b0f8d2eSBrian Somers       }
7299c53a7b1SBrian Somers     }
7309c53a7b1SBrian Somers   }
7319c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
7323b0f8d2eSBrian Somers 
7333b0f8d2eSBrian Somers   return total;
7343b0f8d2eSBrian Somers }
7353b0f8d2eSBrian Somers 
7363b0f8d2eSBrian Somers int
737ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg)
7383b0f8d2eSBrian Somers {
7393b0f8d2eSBrian Somers   int val;
7403b0f8d2eSBrian Somers 
74125092092SBrian Somers   if (arg->argc != arg->argn+1)
7423b0f8d2eSBrian Somers     return -1;
7433b0f8d2eSBrian Somers 
74425092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
745ab2de065SBrian Somers   if (val <= 0) {
746ab2de065SBrian Somers     log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
7473b0f8d2eSBrian Somers     return 1;
7483b0f8d2eSBrian Somers   }
749ab2de065SBrian Somers   arg->cx->mp.bandwidth = val;
750ab2de065SBrian Somers 
751ab2de065SBrian Somers   if (arg->cx->state == DATALINK_OPEN)
752ab2de065SBrian Somers     bundle_CalculateBandwidth(arg->bundle);
753ab2de065SBrian Somers 
7543b0f8d2eSBrian Somers   return 0;
7553b0f8d2eSBrian Somers }
75649052c95SBrian Somers 
75749052c95SBrian Somers int
75849052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
75949052c95SBrian Somers {
76049052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
76149052c95SBrian Somers 
76249052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
7639c53a7b1SBrian Somers   if (mp->active) {
764411675baSBrian Somers     struct mbuf *m, *lm;
7659c53a7b1SBrian Somers     int bufs = 0;
7669c53a7b1SBrian Somers 
767fa0d5216SBrian Somers     lm = NULL;
7681fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
7696f384573SBrian Somers                   mp->server.socket.sun_path);
77026af0ae9SBrian Somers     for (m = mp->inbufs; m; m = m->m_nextpkt) {
7719c53a7b1SBrian Somers       bufs++;
772411675baSBrian Somers       lm = m;
773411675baSBrian Somers     }
774411675baSBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d", bufs);
775411675baSBrian Somers     if (bufs) {
776411675baSBrian Somers       struct mp_header mh;
777411675baSBrian Somers       unsigned long first, last;
778411675baSBrian Somers 
779411675baSBrian Somers       first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
780411675baSBrian Somers       last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
78164e0f466SBrian Somers       prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
782411675baSBrian Somers                     first, last, (unsigned long)mp->seq.next_in,
783411675baSBrian Somers                     (unsigned long)mp->seq.min_in);
78464e0f466SBrian Somers       prompt_Printf(arg->prompt, "                First has %sbegin bit and "
78564e0f466SBrian Somers                     "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
786411675baSBrian Somers     }
787411675baSBrian Somers     prompt_Printf(arg->prompt, "\n");
7889c53a7b1SBrian Somers   }
78949052c95SBrian Somers 
79049052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
79149052c95SBrian Somers   if (mp->active) {
792ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Output SEQ:    %u\n", mp->out.seq);
79349052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
79449052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
79549052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
79649052c95SBrian Somers   }
797643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
79849052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
79949052c95SBrian Somers                            mp->cfg.enddisc.len));
80049052c95SBrian Somers 
80149052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
80249052c95SBrian Somers   if (mp->active) {
803643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
804ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Input SEQ:     %u\n", mp->seq.next_in);
80549052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
80649052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
80749052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
80849052c95SBrian Somers   }
809643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
810643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
811643f4904SBrian Somers                            mp->peer.enddisc.len));
81249052c95SBrian Somers 
81349052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
81449052c95SBrian Somers 
81549052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
81649052c95SBrian Somers   if (mp->cfg.mrru)
81749052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
81849052c95SBrian Somers   else
81949052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
82049052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
82149052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
8227063995cSBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
8237063995cSBrian Somers                   command_ShowNegval(mp->cfg.negenddisc));
824ab2de065SBrian Somers   prompt_Printf(arg->prompt, " AutoLoad:      min %d%%, max %d%%,"
825ab2de065SBrian Somers                 " period %d secs\n", mp->cfg.autoload.min,
826ab2de065SBrian Somers                 mp->cfg.autoload.max, mp->cfg.autoload.period);
82749052c95SBrian Somers 
82849052c95SBrian Somers   return 0;
82949052c95SBrian Somers }
83049052c95SBrian Somers 
83149052c95SBrian Somers const char *
83249052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
83349052c95SBrian Somers {
834d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
83549052c95SBrian Somers   int f, header;
83649052c95SBrian Somers 
83749052c95SBrian Somers   switch (c) {
83808676022SBrian Somers     case ENDDISC_NULL:
83949052c95SBrian Somers       sprintf(result, "Null Class");
84049052c95SBrian Somers       break;
84149052c95SBrian Somers 
84208676022SBrian Somers     case ENDDISC_LOCAL:
84349052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
84449052c95SBrian Somers       break;
84549052c95SBrian Somers 
84608676022SBrian Somers     case ENDDISC_IP:
84749052c95SBrian Somers       if (len == 4)
84849052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
84949052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
85049052c95SBrian Somers       else
85149052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
85249052c95SBrian Somers       break;
85349052c95SBrian Somers 
85408676022SBrian Somers     case ENDDISC_MAC:
85549052c95SBrian Somers       if (len == 6) {
85649052c95SBrian Somers         const u_char *m = (const u_char *)address;
85749052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
85849052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
85949052c95SBrian Somers       } else
86049052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
86149052c95SBrian Somers       break;
86249052c95SBrian Somers 
86308676022SBrian Somers     case ENDDISC_MAGIC:
86449052c95SBrian Somers       sprintf(result, "Magic: 0x");
86549052c95SBrian Somers       header = strlen(result);
86649052c95SBrian Somers       if (len > sizeof result - header - 1)
86749052c95SBrian Somers         len = sizeof result - header - 1;
86849052c95SBrian Somers       for (f = 0; f < len; f++)
86949052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
87049052c95SBrian Somers       break;
87149052c95SBrian Somers 
87208676022SBrian Somers     case ENDDISC_PSN:
87349052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
87449052c95SBrian Somers       break;
87549052c95SBrian Somers 
87649052c95SBrian Somers      default:
87749052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
87849052c95SBrian Somers       header = strlen(result);
87949052c95SBrian Somers       if (len > sizeof result - header - 1)
88049052c95SBrian Somers         len = sizeof result - header - 1;
88149052c95SBrian Somers       for (f = 0; f < len; f++)
88249052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
88349052c95SBrian Somers       break;
88449052c95SBrian Somers   }
88549052c95SBrian Somers   return result;
88649052c95SBrian Somers }
88749052c95SBrian Somers 
88849052c95SBrian Somers int
88949052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
89049052c95SBrian Somers {
89149052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
892d47dceb8SBrian Somers   struct in_addr addr;
89349052c95SBrian Somers 
8949b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
8959b5f8ffdSBrian Somers     case PHASE_DEAD:
8969b5f8ffdSBrian Somers       break;
8979b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
8989b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
8999b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
9009b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
9019b5f8ffdSBrian Somers                    " LCP negotiations\n");
9029b5f8ffdSBrian Somers         return 1;
9039b5f8ffdSBrian Somers       }
9049b5f8ffdSBrian Somers       break;
9059b5f8ffdSBrian Somers     default:
9069b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
90749052c95SBrian Somers       return 1;
90849052c95SBrian Somers   }
90949052c95SBrian Somers 
91049052c95SBrian Somers   if (arg->argc == arg->argn) {
91149052c95SBrian Somers     mp->cfg.enddisc.class = 0;
91249052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
91349052c95SBrian Somers     mp->cfg.enddisc.len = 0;
914e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
91508676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
91608676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
91708676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
91808676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
91908676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
920259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
921259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
922d47dceb8SBrian Somers       else
923259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
924d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
92508676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
92649052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
92708676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
92808676022SBrian Somers       struct sockaddr_dl hwaddr;
92908676022SBrian Somers       int s;
93008676022SBrian Somers 
931259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
932259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
933d47dceb8SBrian Somers       else
934259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
935d47dceb8SBrian Somers 
93608676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
93708676022SBrian Somers       if (s < 0) {
938dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
93908676022SBrian Somers         return 2;
94008676022SBrian Somers       }
941d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
94208676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
94308676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
94408676022SBrian Somers                hwaddr.sdl_alen);
94508676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
94608676022SBrian Somers       } else {
947dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
948259a6191SBrian Somers                   inet_ntoa(addr));
94908676022SBrian Somers         close(s);
95008676022SBrian Somers         return 4;
95108676022SBrian Somers       }
95208676022SBrian Somers       close(s);
95349052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
95449052c95SBrian Somers       int f;
95549052c95SBrian Somers 
95649052c95SBrian Somers       randinit();
95749052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
95849052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
95908676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
96049052c95SBrian Somers       mp->cfg.enddisc.len = 20;
96149052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
96249052c95SBrian Somers       if (arg->argc > arg->argn+1) {
96308676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
96449052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
96549052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
96649052c95SBrian Somers       } else {
967dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
96808676022SBrian Somers         return 5;
96949052c95SBrian Somers       }
97049052c95SBrian Somers     } else {
971dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
97249052c95SBrian Somers                 arg->argv[arg->argn]);
97308676022SBrian Somers       return 6;
97449052c95SBrian Somers     }
975e43ebac1SBrian Somers   }
97649052c95SBrian Somers 
97749052c95SBrian Somers   return 0;
97849052c95SBrian Somers }
9791fa665f5SBrian Somers 
9801fa665f5SBrian Somers static int
9811fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
9821fa665f5SBrian Somers                    int *n)
9831fa665f5SBrian Somers {
9841fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9850f2f3eb3SBrian Somers   int result;
9861fa665f5SBrian Somers 
9870f2f3eb3SBrian Somers   result = 0;
9886f384573SBrian Somers   if (s->send.dl != NULL) {
98996c9bb21SBrian Somers     /* We've connect()ed */
990b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
991b7c5748eSBrian Somers         !s->send.dl->physical->out) {
992b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
9930f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
99496c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
9956f384573SBrian Somers       s->send.dl = NULL;
99696c9bb21SBrian Somers       s->fd = -1;
997ea722969SBrian Somers     } else
998ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
9990f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
100096c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
10011fa665f5SBrian Somers     if (*n < s->fd + 1)
10021fa665f5SBrian Somers       *n = s->fd + 1;
10031fa665f5SBrian Somers     FD_SET(s->fd, r);
100424989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
10050f2f3eb3SBrian Somers     result++;
10061fa665f5SBrian Somers   }
10070f2f3eb3SBrian Somers   return result;
10081fa665f5SBrian Somers }
10091fa665f5SBrian Somers 
10101fa665f5SBrian Somers static int
10111fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset)
10121fa665f5SBrian Somers {
10131fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10141fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
10151fa665f5SBrian Somers }
10161fa665f5SBrian Somers 
10171fa665f5SBrian Somers static void
10181fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
10191fa665f5SBrian Somers {
10201fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10211fa665f5SBrian Somers 
10222cb305afSBrian Somers   bundle_ReceiveDatalink(bundle, s->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 {
1049cbee9754SBrian 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;
10682cb305afSBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
10691fa665f5SBrian Somers   if (s->fd < 0) {
10702cb305afSBrian 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);
10772cb305afSBrian Somers 
10782cb305afSBrian Somers   /*
1079cbee9754SBrian Somers    * Try to bind the socket.  If we succeed we play server, if we fail
1080cbee9754SBrian Somers    * we connect() and hand the link off.
10812cb305afSBrian Somers    */
10822cb305afSBrian Somers 
10836f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
10846f384573SBrian Somers     if (errno != EADDRINUSE) {
1085dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
10866f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10871fa665f5SBrian Somers       umask(mask);
10881fa665f5SBrian Somers       close(s->fd);
10891fa665f5SBrian Somers       s->fd = -1;
109096c9bb21SBrian Somers       return MPSERVER_FAILED;
10916f384573SBrian Somers     }
10922cb305afSBrian Somers 
1093cbee9754SBrian Somers     /* So we're the sender */
10946f384573SBrian Somers     umask(mask);
10956f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
1096dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
10976f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10986f384573SBrian Somers       if (errno == ECONNREFUSED)
10992cb305afSBrian Somers         log_Printf(LogPHASE, "          The previous server died badly !\n");
11006f384573SBrian Somers       close(s->fd);
11016f384573SBrian Somers       s->fd = -1;
110296c9bb21SBrian Somers       return MPSERVER_FAILED;
11036f384573SBrian Somers     }
11046f384573SBrian Somers 
11056f384573SBrian Somers     /* Donate our link to the other guy */
110696c9bb21SBrian Somers     return MPSERVER_CONNECTED;
11071fa665f5SBrian Somers   }
11086f384573SBrian Somers 
110996c9bb21SBrian Somers   return MPSERVER_LISTENING;
11101fa665f5SBrian Somers }
11111fa665f5SBrian Somers 
11121fa665f5SBrian Somers void
11131fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
11141fa665f5SBrian Somers {
11156f384573SBrian Somers   if (s->send.dl != NULL) {
111696c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
11176f384573SBrian Somers     s->send.dl = NULL;
111896c9bb21SBrian Somers     s->fd = -1;
111996c9bb21SBrian Somers   } else if (s->fd >= 0) {
11201fa665f5SBrian Somers     close(s->fd);
11216f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1122dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
11236f384573SBrian Somers                 strerror(errno));
11246f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
11251fa665f5SBrian Somers     s->fd = -1;
11261fa665f5SBrian Somers   }
11271fa665f5SBrian Somers }
112886b1f0d7SBrian Somers 
112986b1f0d7SBrian Somers void
113086b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
113186b1f0d7SBrian Somers {
113286b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
113386b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
11345d9e6103SBrian Somers     mp_Assemble(mp, NULL, NULL);
113586b1f0d7SBrian Somers }
11366f8e9f0aSBrian Somers 
11376f8e9f0aSBrian Somers void
11386f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
11396f8e9f0aSBrian Somers {
11406f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
11416f8e9f0aSBrian Somers }
1142