xref: /freebsd/usr.sbin/ppp/mp.c (revision 7ebff0094ceba5db94a8ef8f3125348edefabd15)
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 
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);
28167b072f7SBrian Somers #ifndef NONAT
28267b072f7SBrian Somers   link_Stack(&mp->link, &natlayer);
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);
2957ebff009SBrian Somers       log_Printf(LogPHASE, "  Attached to peer %s/%s\n", mp->peer.authname,
2967ebff009SBrian Somers                  mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
2977ebff009SBrian Somers                             mp->peer.enddisc.len));
2987ebff009SBrian Somers       log_Printf(LogPHASE, "  New link is peer %s/%s\n", dl->peer.authname,
2997ebff009SBrian Somers                  mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
3007ebff009SBrian Somers                             dl->peer.enddisc.len));
3011fa665f5SBrian Somers       return MP_FAILED;
302643f4904SBrian Somers     }
3031fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
3041fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
3051fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
3061fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
307dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
3081fa665f5SBrian Somers                 dl->name);
3091fa665f5SBrian Somers       return MP_FAILED;
31049052c95SBrian Somers     }
3111fa665f5SBrian Somers     return MP_ADDED;
31249052c95SBrian Somers   } else {
31349052c95SBrian Somers     /* First link in multilink mode */
31449052c95SBrian Somers 
3151fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
3161fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
3171fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
3181fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
3191fa665f5SBrian Somers     mp->peer = dl->peer;
32049052c95SBrian Somers 
321ab2de065SBrian Somers     throughput_destroy(&mp->link.throughput);
322ab2de065SBrian Somers     throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
323ab2de065SBrian Somers     throughput_callback(&mp->link.throughput, mp_UpDown, mp);
324673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
325673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
326673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
327673903ecSBrian Somers 
3289c53a7b1SBrian Somers     mp->out.seq = 0;
3299c53a7b1SBrian Somers     mp->out.link = 0;
330673903ecSBrian Somers     mp->seq.min_in = 0;
331673903ecSBrian Somers     mp->seq.next_in = 0;
332673903ecSBrian Somers 
3331fa665f5SBrian Somers     /*
3341fa665f5SBrian Somers      * Now we create our server socket.
3351fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
3361fa665f5SBrian Somers      */
33796c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
33896c9bb21SBrian Somers     case MPSERVER_CONNECTED:
339dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
3406f384573SBrian Somers                 mp->server.socket.sun_path);
34196c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
3421fa665f5SBrian Somers       return MP_LINKSENT;
34396c9bb21SBrian Somers     case MPSERVER_FAILED:
3441fa665f5SBrian Somers       return MP_FAILED;
34596c9bb21SBrian Somers     case MPSERVER_LISTENING:
346dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
347dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
3481fa665f5SBrian Somers 
34949052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
350ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
3513b0f8d2eSBrian Somers 
3523b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
35306337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
354dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
355dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
35606337856SBrian Somers       }
3573b0f8d2eSBrian Somers 
3583b0f8d2eSBrian Somers       mp->active = 1;
35996c9bb21SBrian Somers       break;
36049052c95SBrian Somers     }
3611fa665f5SBrian Somers   }
3623b0f8d2eSBrian Somers 
3631fa665f5SBrian Somers   return MP_UP;
3643b0f8d2eSBrian Somers }
3653b0f8d2eSBrian Somers 
3663b0f8d2eSBrian Somers void
367673903ecSBrian Somers mp_Down(struct mp *mp)
368673903ecSBrian Somers {
369673903ecSBrian Somers   if (mp->active) {
370673903ecSBrian Somers     struct mbuf *next;
371673903ecSBrian Somers 
372ab2de065SBrian Somers     /* Stop that ! */
373ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
374ab2de065SBrian Somers 
3751fa665f5SBrian Somers     /* Don't want any more of these */
3761fa665f5SBrian Somers     mpserver_Close(&mp->server);
3771fa665f5SBrian Somers 
3781fa665f5SBrian Somers     /* CCP goes down with a bang */
37909206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
380673903ecSBrian Somers 
381673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
382673903ecSBrian Somers     while (mp->inbufs) {
38326af0ae9SBrian Somers       next = mp->inbufs->m_nextpkt;
38426af0ae9SBrian Somers       m_freem(mp->inbufs);
385673903ecSBrian Somers       mp->inbufs = next;
386673903ecSBrian Somers     }
387673903ecSBrian Somers 
388643f4904SBrian Somers     peerid_Init(&mp->peer);
389673903ecSBrian Somers     mp->active = 0;
390673903ecSBrian Somers   }
391673903ecSBrian Somers }
392673903ecSBrian Somers 
393673903ecSBrian Somers void
3943b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
3953b0f8d2eSBrian Somers {
3963b0f8d2eSBrian Somers   mplink->seq = 0;
397ab2de065SBrian Somers   mplink->bandwidth = 0;
3983b0f8d2eSBrian Somers }
3993b0f8d2eSBrian Somers 
4005d9e6103SBrian Somers static void
4015d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
4023b0f8d2eSBrian Somers {
4033b0f8d2eSBrian Somers   struct mp_header mh, h;
4043b0f8d2eSBrian Somers   struct mbuf *q, *last;
4053b0f8d2eSBrian Somers   int32_t seq;
4063b0f8d2eSBrian Somers 
40786b1f0d7SBrian Somers   /*
40886b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
40986b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
41086b1f0d7SBrian Somers    * as normal
41186b1f0d7SBrian Somers    */
41286b1f0d7SBrian Somers 
41386b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
41426af0ae9SBrian Somers     m_freem(m);
4153b0f8d2eSBrian Somers     return;
4163b0f8d2eSBrian Somers   }
4173b0f8d2eSBrian Somers 
41886b1f0d7SBrian Somers   if (p) {
4193b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
4203b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
42186b1f0d7SBrian Somers   } else
42286b1f0d7SBrian Somers     seq = mp->seq.min_in;
42386b1f0d7SBrian Somers 
4243b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
4253b0f8d2eSBrian Somers     /*
4263b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
4273b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
4283b0f8d2eSBrian Somers      */
4293b0f8d2eSBrian Somers     struct datalink *dl;
4303b0f8d2eSBrian Somers 
43186b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
4323b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
43386b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
43486b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
43586b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
4363b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
4373b0f8d2eSBrian Somers   }
4383b0f8d2eSBrian Somers 
4393b0f8d2eSBrian Somers   /*
4403b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
4413b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
4423b0f8d2eSBrian Somers    * the queue.
4433b0f8d2eSBrian Somers    */
4443b0f8d2eSBrian Somers 
4453b0f8d2eSBrian Somers   if (!mp->inbufs) {
4463b0f8d2eSBrian Somers     mp->inbufs = m;
4473b0f8d2eSBrian Somers     m = NULL;
4483b0f8d2eSBrian Somers   }
4493b0f8d2eSBrian Somers 
4503b0f8d2eSBrian Somers   last = NULL;
4513b0f8d2eSBrian Somers   seq = mp->seq.next_in;
4523b0f8d2eSBrian Somers   q = mp->inbufs;
4533b0f8d2eSBrian Somers   while (q) {
4543b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
4551bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
4563b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
4573b0f8d2eSBrian Somers       if (last)
45826af0ae9SBrian Somers         last->m_nextpkt = m;
4593b0f8d2eSBrian Somers       else
4603b0f8d2eSBrian Somers         mp->inbufs = m;
46126af0ae9SBrian Somers       m->m_nextpkt = q;
4623b0f8d2eSBrian Somers       q = m;
4633b0f8d2eSBrian Somers       h = mh;
4643b0f8d2eSBrian Somers       m = NULL;
4653b0f8d2eSBrian Somers     }
4663b0f8d2eSBrian Somers 
4673b0f8d2eSBrian Somers     if (h.seq != seq) {
4683b0f8d2eSBrian Somers       /* we're missing something :-( */
46964e0f466SBrian Somers       if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
4703b0f8d2eSBrian Somers         /* we're never gonna get it */
4713b0f8d2eSBrian Somers         struct mbuf *next;
4723b0f8d2eSBrian Somers 
4733b0f8d2eSBrian Somers         /* Zap all older fragments */
4743b0f8d2eSBrian Somers         while (mp->inbufs != q) {
475dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
47626af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
47726af0ae9SBrian Somers           m_freem(mp->inbufs);
4783b0f8d2eSBrian Somers           mp->inbufs = next;
4793b0f8d2eSBrian Somers         }
4803b0f8d2eSBrian Somers 
4813b0f8d2eSBrian Somers         /*
4823b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4833b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4843b0f8d2eSBrian Somers          * comes first.
4853b0f8d2eSBrian Somers          */
4863b0f8d2eSBrian Somers         do {
4873b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4883b0f8d2eSBrian Somers           if (h.begin) {
489ce828a6eSBrian Somers             /* We might be able to process this ! */
4903b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4913b0f8d2eSBrian Somers             break;
4923b0f8d2eSBrian Somers           }
49326af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
494dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
49526af0ae9SBrian Somers           m_freem(mp->inbufs);
4963b0f8d2eSBrian Somers           mp->inbufs = next;
49764e0f466SBrian Somers         } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
49864e0f466SBrian Somers                                          h.seq) || h.end));
4993b0f8d2eSBrian Somers 
5003b0f8d2eSBrian Somers         /*
5013b0f8d2eSBrian Somers          * Continue processing things from here.
5023b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
5033b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
5043b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
5053b0f8d2eSBrian Somers          * packets that have already been received.
5063b0f8d2eSBrian Somers          */
5073b0f8d2eSBrian Somers 
508147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5093b0f8d2eSBrian Somers         last = NULL;
5103b0f8d2eSBrian Somers         q = mp->inbufs;
5113b0f8d2eSBrian Somers       } else
5123b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
5133b0f8d2eSBrian Somers         break;
5143b0f8d2eSBrian Somers     } else if (h.end) {
5153b0f8d2eSBrian Somers       /* We've got something, reassemble */
5163b0f8d2eSBrian Somers       struct mbuf **frag = &q;
5173b0f8d2eSBrian Somers       int len;
518ce828a6eSBrian Somers       u_long first = -1;
5193b0f8d2eSBrian Somers 
5203b0f8d2eSBrian Somers       do {
5213b0f8d2eSBrian Somers         *frag = mp->inbufs;
52226af0ae9SBrian Somers         mp->inbufs = mp->inbufs->m_nextpkt;
5233b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
524ce828a6eSBrian Somers         if (first == -1)
525ce828a6eSBrian Somers           first = h.seq;
52626af0ae9SBrian Somers         (*frag)->m_offset += len;
52726af0ae9SBrian Somers         (*frag)->m_len -= len;
52826af0ae9SBrian Somers         (*frag)->m_nextpkt = NULL;
5293b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
530dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
5313b0f8d2eSBrian Somers                     (u_long)h.seq);
53226af0ae9SBrian Somers           m_freem(q);
5333b0f8d2eSBrian Somers           q = NULL;
5343b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
535dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
5363b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
5373b0f8d2eSBrian Somers           /*
5383b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
5393b0f8d2eSBrian Somers            * our half-assembed packet.
5403b0f8d2eSBrian Somers            */
54126af0ae9SBrian Somers           (*frag)->m_nextpkt = mp->inbufs;
5423b0f8d2eSBrian Somers           mp->inbufs = *frag;
5433b0f8d2eSBrian Somers           *frag = NULL;
54426af0ae9SBrian Somers           m_freem(q);
5453b0f8d2eSBrian Somers           q = NULL;
5463b0f8d2eSBrian Somers           frag = &q;
5473b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
5483b0f8d2eSBrian Somers         } else
5493b0f8d2eSBrian Somers           do
55026af0ae9SBrian Somers             frag = &(*frag)->m_next;
551ce828a6eSBrian Somers           while (*frag != NULL);
5523b0f8d2eSBrian Somers       } while (!h.end);
5533b0f8d2eSBrian Somers 
5543b0f8d2eSBrian Somers       if (q) {
55526af0ae9SBrian Somers         q = m_pullup(q);
556dd7e2610SBrian Somers         log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
55726af0ae9SBrian Somers                    first, (u_long)h.seq, m_length(q));
55826af0ae9SBrian Somers         link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
55926af0ae9SBrian Somers         m_freem(q);
5603b0f8d2eSBrian Somers       }
5613b0f8d2eSBrian Somers 
562147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5633b0f8d2eSBrian Somers       last = NULL;
5643b0f8d2eSBrian Somers       q = mp->inbufs;
5653b0f8d2eSBrian Somers     } else {
5663b0f8d2eSBrian Somers       /* Look for the next fragment */
567147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5683b0f8d2eSBrian Somers       last = q;
56926af0ae9SBrian Somers       q = q->m_nextpkt;
5703b0f8d2eSBrian Somers     }
5713b0f8d2eSBrian Somers   }
5723b0f8d2eSBrian Somers 
5733b0f8d2eSBrian Somers   if (m) {
5743b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5753b0f8d2eSBrian Somers     last = NULL;
57626af0ae9SBrian Somers     for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
5773b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5781bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
579147613eaSBrian Somers         break;
580147613eaSBrian Somers     }
581147613eaSBrian Somers     /* Our received fragment fits in here */
5823b0f8d2eSBrian Somers     if (last)
58326af0ae9SBrian Somers       last->m_nextpkt = m;
5843b0f8d2eSBrian Somers     else
5853b0f8d2eSBrian Somers       mp->inbufs = m;
58626af0ae9SBrian Somers     m->m_nextpkt = q;
5873b0f8d2eSBrian Somers   }
5883b0f8d2eSBrian Somers }
5893b0f8d2eSBrian Somers 
5905d9e6103SBrian Somers struct mbuf *
5915d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
5925d9e6103SBrian Somers {
5935d9e6103SBrian Somers   struct physical *p = link2physical(l);
5945d9e6103SBrian Somers 
5955d9e6103SBrian Somers   if (!bundle->ncp.mp.active)
5965d9e6103SBrian Somers     /* Let someone else deal with it ! */
5975d9e6103SBrian Somers     return bp;
5985d9e6103SBrian Somers 
5995d9e6103SBrian Somers   if (p == NULL) {
6005d9e6103SBrian Somers     log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
60126af0ae9SBrian Somers     m_freem(bp);
602411675baSBrian Somers   } else {
60326af0ae9SBrian Somers     m_settype(bp, MB_MPIN);
6045d9e6103SBrian Somers     mp_Assemble(&bundle->ncp.mp, bp, p);
605411675baSBrian Somers   }
6065d9e6103SBrian Somers 
6075d9e6103SBrian Somers   return NULL;
6085d9e6103SBrian Somers }
6095d9e6103SBrian Somers 
6103b0f8d2eSBrian Somers static void
6115d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
6125d9e6103SBrian Somers           struct mbuf *m, u_int32_t begin, u_int32_t end)
6133b0f8d2eSBrian Somers {
61426af0ae9SBrian Somers   char prepend[4];
6153b0f8d2eSBrian Somers 
616ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
61726af0ae9SBrian Somers 
61849052c95SBrian Somers   if (mp->peer_is12bit) {
6199e8ec64bSBrian Somers     u_int16_t val;
620ce828a6eSBrian Somers 
6219e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
62226af0ae9SBrian Somers     ua_htons(&val, prepend);
62326af0ae9SBrian Somers     m = m_prepend(m, prepend, 2, 0);
6243b0f8d2eSBrian Somers   } else {
6259e8ec64bSBrian Somers     u_int32_t val;
626ce828a6eSBrian Somers 
6279e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
62826af0ae9SBrian Somers     ua_htonl(&val, prepend);
62926af0ae9SBrian Somers     m = m_prepend(m, prepend, 4, 0);
6303b0f8d2eSBrian Somers   }
631dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
6329c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
63326af0ae9SBrian Somers                mp->out.seq, m_length(m), l->name);
634147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
6353b0f8d2eSBrian Somers 
63626af0ae9SBrian Somers   link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
6373b0f8d2eSBrian Somers }
6383b0f8d2eSBrian Somers 
6393b0f8d2eSBrian Somers int
6403b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
6413b0f8d2eSBrian Somers {
6423b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
6439c53a7b1SBrian Somers   struct datalink *dl, *fdl;
64426af0ae9SBrian Somers   size_t total, add, len;
64526af0ae9SBrian Somers   int thislink, nlinks;
646b4b280abSBrian Somers   u_int32_t begin, end;
6473b0f8d2eSBrian Somers   struct mbuf *m, *mo;
6483b0f8d2eSBrian Somers 
6499c53a7b1SBrian Somers   thislink = nlinks = 0;
6509c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
65186b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
6529c53a7b1SBrian Somers     if (!fdl) {
6539c53a7b1SBrian Somers       if (thislink == mp->out.link)
6549c53a7b1SBrian Somers         fdl = dl;
6559c53a7b1SBrian Somers       else
6569c53a7b1SBrian Somers         thislink++;
6579c53a7b1SBrian Somers     }
6589c53a7b1SBrian Somers     nlinks++;
6599c53a7b1SBrian Somers   }
6609c53a7b1SBrian Somers 
6619c53a7b1SBrian Somers   if (!fdl) {
6629c53a7b1SBrian Somers     fdl = bundle->links;
6631bc9b5baSBrian Somers     if (!fdl)
6641bc9b5baSBrian Somers       return 0;
6659c53a7b1SBrian Somers     thislink = 0;
6669c53a7b1SBrian Somers   }
6673b0f8d2eSBrian Somers 
6683b0f8d2eSBrian Somers   total = 0;
6699c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6709c53a7b1SBrian Somers     if (!dl) {
6719c53a7b1SBrian Somers       dl = bundle->links;
6729c53a7b1SBrian Somers       thislink = 0;
6739c53a7b1SBrian Somers     }
6749c53a7b1SBrian Somers 
6756f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
6766f384573SBrian Somers       continue;
6779c53a7b1SBrian Somers 
6783b0f8d2eSBrian Somers     if (dl->physical->out)
6793b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
6803b0f8d2eSBrian Somers       continue;
6819c53a7b1SBrian Somers 
6823b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
6833b0f8d2eSBrian Somers     total += add;
6843b0f8d2eSBrian Somers     if (add)
6853b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6863b0f8d2eSBrian Somers       continue;
6879c53a7b1SBrian Somers 
6885d9e6103SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
6893b0f8d2eSBrian Somers       /* Nothing else to send */
6903b0f8d2eSBrian Somers       break;
6913b0f8d2eSBrian Somers 
6923b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
69326af0ae9SBrian Somers     len = m_length(m);
6943b0f8d2eSBrian Somers     begin = 1;
6953b0f8d2eSBrian Somers     end = 0;
6963b0f8d2eSBrian Somers 
6979c53a7b1SBrian Somers     while (!end) {
6989c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
699ab2de065SBrian Somers         /* Write at most his_mru bytes to the physical link */
700ab2de065SBrian Somers         if (len <= dl->physical->link.lcp.his_mru) {
7013b0f8d2eSBrian Somers           mo = m;
7023b0f8d2eSBrian Somers           end = 1;
70326af0ae9SBrian Somers           m_settype(mo, MB_MPOUT);
7043b0f8d2eSBrian Somers         } else {
705ab2de065SBrian Somers           /* It's > his_mru, chop the packet (`m') into bits */
70626af0ae9SBrian Somers           mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
70726af0ae9SBrian Somers           len -= mo->m_len;
70826af0ae9SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
7093b0f8d2eSBrian Somers         }
7105d9e6103SBrian Somers         mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
7113b0f8d2eSBrian Somers         begin = 0;
7123b0f8d2eSBrian Somers       }
7139c53a7b1SBrian Somers 
7149c53a7b1SBrian Somers       if (!end) {
7159c53a7b1SBrian Somers         nlinks--;
7169c53a7b1SBrian Somers         dl = dl->next;
7179c53a7b1SBrian Somers         if (!dl) {
7189c53a7b1SBrian Somers           dl = bundle->links;
7199c53a7b1SBrian Somers           thislink = 0;
7209c53a7b1SBrian Somers         } else
7219c53a7b1SBrian Somers           thislink++;
7223b0f8d2eSBrian Somers       }
7239c53a7b1SBrian Somers     }
7249c53a7b1SBrian Somers   }
7259c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
7263b0f8d2eSBrian Somers 
7273b0f8d2eSBrian Somers   return total;
7283b0f8d2eSBrian Somers }
7293b0f8d2eSBrian Somers 
7303b0f8d2eSBrian Somers int
731ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg)
7323b0f8d2eSBrian Somers {
7333b0f8d2eSBrian Somers   int val;
7343b0f8d2eSBrian Somers 
73525092092SBrian Somers   if (arg->argc != arg->argn+1)
7363b0f8d2eSBrian Somers     return -1;
7373b0f8d2eSBrian Somers 
73825092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
739ab2de065SBrian Somers   if (val <= 0) {
740ab2de065SBrian Somers     log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
7413b0f8d2eSBrian Somers     return 1;
7423b0f8d2eSBrian Somers   }
743ab2de065SBrian Somers   arg->cx->mp.bandwidth = val;
744ab2de065SBrian Somers 
745ab2de065SBrian Somers   if (arg->cx->state == DATALINK_OPEN)
746ab2de065SBrian Somers     bundle_CalculateBandwidth(arg->bundle);
747ab2de065SBrian Somers 
7483b0f8d2eSBrian Somers   return 0;
7493b0f8d2eSBrian Somers }
75049052c95SBrian Somers 
75149052c95SBrian Somers int
75249052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
75349052c95SBrian Somers {
75449052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
75549052c95SBrian Somers 
75649052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
7579c53a7b1SBrian Somers   if (mp->active) {
758411675baSBrian Somers     struct mbuf *m, *lm;
7599c53a7b1SBrian Somers     int bufs = 0;
7609c53a7b1SBrian Somers 
761fa0d5216SBrian Somers     lm = NULL;
7621fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
7636f384573SBrian Somers                   mp->server.socket.sun_path);
76426af0ae9SBrian Somers     for (m = mp->inbufs; m; m = m->m_nextpkt) {
7659c53a7b1SBrian Somers       bufs++;
766411675baSBrian Somers       lm = m;
767411675baSBrian Somers     }
768411675baSBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d", bufs);
769411675baSBrian Somers     if (bufs) {
770411675baSBrian Somers       struct mp_header mh;
771411675baSBrian Somers       unsigned long first, last;
772411675baSBrian Somers 
773411675baSBrian Somers       first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
774411675baSBrian Somers       last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
77564e0f466SBrian Somers       prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
776411675baSBrian Somers                     first, last, (unsigned long)mp->seq.next_in,
777411675baSBrian Somers                     (unsigned long)mp->seq.min_in);
77864e0f466SBrian Somers       prompt_Printf(arg->prompt, "                First has %sbegin bit and "
77964e0f466SBrian Somers                     "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
780411675baSBrian Somers     }
781411675baSBrian Somers     prompt_Printf(arg->prompt, "\n");
7829c53a7b1SBrian Somers   }
78349052c95SBrian Somers 
78449052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
78549052c95SBrian Somers   if (mp->active) {
786ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Output SEQ:    %u\n", mp->out.seq);
78749052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
78849052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
78949052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
79049052c95SBrian Somers   }
791643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
79249052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
79349052c95SBrian Somers                            mp->cfg.enddisc.len));
79449052c95SBrian Somers 
79549052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
79649052c95SBrian Somers   if (mp->active) {
797643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
798ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Input SEQ:     %u\n", mp->seq.next_in);
79949052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
80049052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
80149052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
80249052c95SBrian Somers   }
803643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
804643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
805643f4904SBrian Somers                            mp->peer.enddisc.len));
80649052c95SBrian Somers 
80749052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
80849052c95SBrian Somers 
80949052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
81049052c95SBrian Somers   if (mp->cfg.mrru)
81149052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
81249052c95SBrian Somers   else
81349052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
81449052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
81549052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
8167063995cSBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
8177063995cSBrian Somers                   command_ShowNegval(mp->cfg.negenddisc));
818ab2de065SBrian Somers   prompt_Printf(arg->prompt, " AutoLoad:      min %d%%, max %d%%,"
819ab2de065SBrian Somers                 " period %d secs\n", mp->cfg.autoload.min,
820ab2de065SBrian Somers                 mp->cfg.autoload.max, mp->cfg.autoload.period);
82149052c95SBrian Somers 
82249052c95SBrian Somers   return 0;
82349052c95SBrian Somers }
82449052c95SBrian Somers 
82549052c95SBrian Somers const char *
82649052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
82749052c95SBrian Somers {
828d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
82949052c95SBrian Somers   int f, header;
83049052c95SBrian Somers 
83149052c95SBrian Somers   switch (c) {
83208676022SBrian Somers     case ENDDISC_NULL:
83349052c95SBrian Somers       sprintf(result, "Null Class");
83449052c95SBrian Somers       break;
83549052c95SBrian Somers 
83608676022SBrian Somers     case ENDDISC_LOCAL:
83749052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
83849052c95SBrian Somers       break;
83949052c95SBrian Somers 
84008676022SBrian Somers     case ENDDISC_IP:
84149052c95SBrian Somers       if (len == 4)
84249052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
84349052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
84449052c95SBrian Somers       else
84549052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
84649052c95SBrian Somers       break;
84749052c95SBrian Somers 
84808676022SBrian Somers     case ENDDISC_MAC:
84949052c95SBrian Somers       if (len == 6) {
85049052c95SBrian Somers         const u_char *m = (const u_char *)address;
85149052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
85249052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
85349052c95SBrian Somers       } else
85449052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
85549052c95SBrian Somers       break;
85649052c95SBrian Somers 
85708676022SBrian Somers     case ENDDISC_MAGIC:
85849052c95SBrian Somers       sprintf(result, "Magic: 0x");
85949052c95SBrian Somers       header = strlen(result);
86049052c95SBrian Somers       if (len > sizeof result - header - 1)
86149052c95SBrian Somers         len = sizeof result - header - 1;
86249052c95SBrian Somers       for (f = 0; f < len; f++)
86349052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
86449052c95SBrian Somers       break;
86549052c95SBrian Somers 
86608676022SBrian Somers     case ENDDISC_PSN:
86749052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
86849052c95SBrian Somers       break;
86949052c95SBrian Somers 
87049052c95SBrian Somers      default:
87149052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
87249052c95SBrian Somers       header = strlen(result);
87349052c95SBrian Somers       if (len > sizeof result - header - 1)
87449052c95SBrian Somers         len = sizeof result - header - 1;
87549052c95SBrian Somers       for (f = 0; f < len; f++)
87649052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
87749052c95SBrian Somers       break;
87849052c95SBrian Somers   }
87949052c95SBrian Somers   return result;
88049052c95SBrian Somers }
88149052c95SBrian Somers 
88249052c95SBrian Somers int
88349052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
88449052c95SBrian Somers {
88549052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
886d47dceb8SBrian Somers   struct in_addr addr;
88749052c95SBrian Somers 
8889b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
8899b5f8ffdSBrian Somers     case PHASE_DEAD:
8909b5f8ffdSBrian Somers       break;
8919b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
8929b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
8939b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
8949b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
8959b5f8ffdSBrian Somers                    " LCP negotiations\n");
8969b5f8ffdSBrian Somers         return 1;
8979b5f8ffdSBrian Somers       }
8989b5f8ffdSBrian Somers       break;
8999b5f8ffdSBrian Somers     default:
9009b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
90149052c95SBrian Somers       return 1;
90249052c95SBrian Somers   }
90349052c95SBrian Somers 
90449052c95SBrian Somers   if (arg->argc == arg->argn) {
90549052c95SBrian Somers     mp->cfg.enddisc.class = 0;
90649052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
90749052c95SBrian Somers     mp->cfg.enddisc.len = 0;
908e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
90908676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
91008676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
91108676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
91208676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
91308676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
914259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
915259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
916d47dceb8SBrian Somers       else
917259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
918d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
91908676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
92049052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
92108676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
92208676022SBrian Somers       struct sockaddr_dl hwaddr;
92308676022SBrian Somers       int s;
92408676022SBrian Somers 
925259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
926259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
927d47dceb8SBrian Somers       else
928259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
929d47dceb8SBrian Somers 
93008676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
93108676022SBrian Somers       if (s < 0) {
932dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
93308676022SBrian Somers         return 2;
93408676022SBrian Somers       }
935d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
93608676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
93708676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
93808676022SBrian Somers                hwaddr.sdl_alen);
93908676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
94008676022SBrian Somers       } else {
941dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
942259a6191SBrian Somers                   inet_ntoa(addr));
94308676022SBrian Somers         close(s);
94408676022SBrian Somers         return 4;
94508676022SBrian Somers       }
94608676022SBrian Somers       close(s);
94749052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
94849052c95SBrian Somers       int f;
94949052c95SBrian Somers 
95049052c95SBrian Somers       randinit();
95149052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
95249052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
95308676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
95449052c95SBrian Somers       mp->cfg.enddisc.len = 20;
95549052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
95649052c95SBrian Somers       if (arg->argc > arg->argn+1) {
95708676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
95849052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
95949052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
96049052c95SBrian Somers       } else {
961dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
96208676022SBrian Somers         return 5;
96349052c95SBrian Somers       }
96449052c95SBrian Somers     } else {
965dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
96649052c95SBrian Somers                 arg->argv[arg->argn]);
96708676022SBrian Somers       return 6;
96849052c95SBrian Somers     }
969e43ebac1SBrian Somers   }
97049052c95SBrian Somers 
97149052c95SBrian Somers   return 0;
97249052c95SBrian Somers }
9731fa665f5SBrian Somers 
9741fa665f5SBrian Somers static int
975f013f33eSBrian Somers mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
9761fa665f5SBrian Somers                    int *n)
9771fa665f5SBrian Somers {
9781fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9790f2f3eb3SBrian Somers   int result;
9801fa665f5SBrian Somers 
9810f2f3eb3SBrian Somers   result = 0;
9826f384573SBrian Somers   if (s->send.dl != NULL) {
98396c9bb21SBrian Somers     /* We've connect()ed */
984b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
985b7c5748eSBrian Somers         !s->send.dl->physical->out) {
986b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
9870f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
98896c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
9896f384573SBrian Somers       s->send.dl = NULL;
99096c9bb21SBrian Somers       s->fd = -1;
991ea722969SBrian Somers     } else
992ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
9930f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
99496c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
9951fa665f5SBrian Somers     if (*n < s->fd + 1)
9961fa665f5SBrian Somers       *n = s->fd + 1;
9971fa665f5SBrian Somers     FD_SET(s->fd, r);
99824989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
9990f2f3eb3SBrian Somers     result++;
10001fa665f5SBrian Somers   }
10010f2f3eb3SBrian Somers   return result;
10021fa665f5SBrian Somers }
10031fa665f5SBrian Somers 
10041fa665f5SBrian Somers static int
1005f013f33eSBrian Somers mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
10061fa665f5SBrian Somers {
10071fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10081fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
10091fa665f5SBrian Somers }
10101fa665f5SBrian Somers 
10111fa665f5SBrian Somers static void
1012f013f33eSBrian Somers mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
10131fa665f5SBrian Somers {
10141fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10151fa665f5SBrian Somers 
10162cb305afSBrian Somers   bundle_ReceiveDatalink(bundle, s->fd);
10171fa665f5SBrian Somers }
10181fa665f5SBrian Somers 
10191af29a6eSBrian Somers static int
1020f013f33eSBrian Somers mpserver_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
10211fa665f5SBrian Somers {
10221fa665f5SBrian Somers   /* We never want to write here ! */
1023a33b2ef7SBrian Somers   log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
10241af29a6eSBrian Somers   return 0;
10251fa665f5SBrian Somers }
10261fa665f5SBrian Somers 
10271fa665f5SBrian Somers void
10281fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
10291fa665f5SBrian Somers {
10301fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
10311fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
10321fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
10331fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
10341fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
10356f384573SBrian Somers   s->send.dl = NULL;
10361fa665f5SBrian Somers   s->fd = -1;
10376f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
10381fa665f5SBrian Somers }
10391fa665f5SBrian Somers 
10401fa665f5SBrian Somers int
10411fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
10421fa665f5SBrian Somers {
1043cbee9754SBrian Somers   int f, l;
10441fa665f5SBrian Somers   mode_t mask;
10451fa665f5SBrian Somers 
10461fa665f5SBrian Somers   if (s->fd != -1) {
1047a33b2ef7SBrian Somers     log_Printf(LogALERT, "Internal error !  mpserver already open\n");
10486f384573SBrian Somers     mpserver_Close(s);
10491fa665f5SBrian Somers   }
10501fa665f5SBrian Somers 
10516f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
10526f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
10531fa665f5SBrian Somers 
10546f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
10556f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
10561fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
10576f384573SBrian Somers     l += 2;
10581fa665f5SBrian Somers   }
10591fa665f5SBrian Somers 
10606f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
10616f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
10622cb305afSBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
10631fa665f5SBrian Somers   if (s->fd < 0) {
10642cb305afSBrian Somers     log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
106596c9bb21SBrian Somers     return MPSERVER_FAILED;
10661fa665f5SBrian Somers   }
10671fa665f5SBrian Somers 
10686f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
10696f384573SBrian Somers              sizeof s->socket);
10701fa665f5SBrian Somers   mask = umask(0177);
10712cb305afSBrian Somers 
10722cb305afSBrian Somers   /*
1073cbee9754SBrian Somers    * Try to bind the socket.  If we succeed we play server, if we fail
1074cbee9754SBrian Somers    * we connect() and hand the link off.
10752cb305afSBrian Somers    */
10762cb305afSBrian Somers 
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     }
10862cb305afSBrian Somers 
1087cbee9754SBrian Somers     /* So we're the sender */
10886f384573SBrian Somers     umask(mask);
10896f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
1090dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
10916f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10926f384573SBrian Somers       if (errno == ECONNREFUSED)
10932cb305afSBrian Somers         log_Printf(LogPHASE, "          The previous server died badly !\n");
10946f384573SBrian Somers       close(s->fd);
10956f384573SBrian Somers       s->fd = -1;
109696c9bb21SBrian Somers       return MPSERVER_FAILED;
10976f384573SBrian Somers     }
10986f384573SBrian Somers 
10996f384573SBrian Somers     /* Donate our link to the other guy */
110096c9bb21SBrian Somers     return MPSERVER_CONNECTED;
11011fa665f5SBrian Somers   }
11026f384573SBrian Somers 
110396c9bb21SBrian Somers   return MPSERVER_LISTENING;
11041fa665f5SBrian Somers }
11051fa665f5SBrian Somers 
11061fa665f5SBrian Somers void
11071fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
11081fa665f5SBrian Somers {
11096f384573SBrian Somers   if (s->send.dl != NULL) {
111096c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
11116f384573SBrian Somers     s->send.dl = NULL;
111296c9bb21SBrian Somers     s->fd = -1;
111396c9bb21SBrian Somers   } else if (s->fd >= 0) {
11141fa665f5SBrian Somers     close(s->fd);
11156f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1116dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
11176f384573SBrian Somers                 strerror(errno));
11186f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
11191fa665f5SBrian Somers     s->fd = -1;
11201fa665f5SBrian Somers   }
11211fa665f5SBrian Somers }
112286b1f0d7SBrian Somers 
112386b1f0d7SBrian Somers void
112486b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
112586b1f0d7SBrian Somers {
112686b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
112786b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
11285d9e6103SBrian Somers     mp_Assemble(mp, NULL, NULL);
112986b1f0d7SBrian Somers }
11306f8e9f0aSBrian Somers 
11316f8e9f0aSBrian Somers void
11326f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
11336f8e9f0aSBrian Somers {
11346f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
11356f8e9f0aSBrian Somers }
1136