xref: /freebsd/usr.sbin/ppp/mp.c (revision 91cbd2eec0d10f7e8be60a45cb28464a8dd47f06)
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 
19291cbd2eeSBrian Somers   percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
19391cbd2eeSBrian Somers                 mp->link.stats.total.out.OctetsPerSecond) * 800 /
19491cbd2eeSBrian Somers             mp->bundle->bandwidth;
195ab2de065SBrian Somers   if (percent >= mp->cfg.autoload.max) {
196ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
197ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
198ab2de065SBrian Somers   } else if (percent <= mp->cfg.autoload.min) {
199ab2de065SBrian Somers     log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
200ab2de065SBrian Somers     bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
201ab2de065SBrian Somers   }
202ab2de065SBrian Somers }
203ab2de065SBrian Somers 
204ab2de065SBrian Somers void
205ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp)
206ab2de065SBrian Somers {
20711572abfSBrian Somers   throughput_stop(&mp->link.stats.total);
208ab2de065SBrian Somers }
209ab2de065SBrian Somers 
210ab2de065SBrian Somers void
211ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp)
212ab2de065SBrian Somers {
21311572abfSBrian Somers   if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
21411572abfSBrian Somers     throughput_destroy(&mp->link.stats.total);
21511572abfSBrian Somers     throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
21611572abfSBrian Somers     throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
217ab2de065SBrian Somers   }
218ab2de065SBrian Somers 
219ab2de065SBrian Somers   if (bundle_WantAutoloadTimer(mp->bundle))
22011572abfSBrian Somers     throughput_start(&mp->link.stats.total, "MP throughput", 1);
221ab2de065SBrian Somers   else
222ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
223ab2de065SBrian Somers }
224ab2de065SBrian Somers 
225ab2de065SBrian Somers void
226ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp)
227ab2de065SBrian Somers {
22811572abfSBrian Somers   if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
229ab2de065SBrian Somers     mp_CheckAutoloadTimer(mp);
230ab2de065SBrian Somers   else
23111572abfSBrian Somers     throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
232ab2de065SBrian Somers }
233ab2de065SBrian Somers 
2343b0f8d2eSBrian Somers void
2353b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
2363b0f8d2eSBrian Somers {
23749052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
23849052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
239643f4904SBrian Somers 
240643f4904SBrian Somers   peerid_Init(&mp->peer);
241643f4904SBrian Somers 
2429c53a7b1SBrian Somers   mp->out.seq = 0;
2439c53a7b1SBrian Somers   mp->out.link = 0;
2443b0f8d2eSBrian Somers   mp->seq.min_in = 0;
2453b0f8d2eSBrian Somers   mp->seq.next_in = 0;
2463b0f8d2eSBrian Somers   mp->inbufs = NULL;
2473b0f8d2eSBrian Somers   mp->bundle = bundle;
2483b0f8d2eSBrian Somers 
2495d9e6103SBrian Somers   mp->link.type = LOGICAL_LINK;
2503b0f8d2eSBrian Somers   mp->link.name = "mp";
2513b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
252643f4904SBrian Somers 
253ab2de065SBrian Somers   mp->cfg.autoload.period = SAMPLE_PERIOD;
254ab2de065SBrian Somers   mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
25511572abfSBrian Somers   throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
25611572abfSBrian Somers   throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
25711572abfSBrian Somers   mp->link.stats.parent = NULL;
25811572abfSBrian Somers   mp->link.stats.gather = 0;	/* Let the physical links gather stats */
2593b0f8d2eSBrian Somers   memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
2603b0f8d2eSBrian Somers   memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
2613b0f8d2eSBrian Somers   memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
2623b0f8d2eSBrian Somers 
2633b0f8d2eSBrian Somers   mp->fsmp.LayerStart = mp_LayerStart;
2643b0f8d2eSBrian Somers   mp->fsmp.LayerUp = mp_LayerUp;
2653b0f8d2eSBrian Somers   mp->fsmp.LayerDown = mp_LayerDown;
2663b0f8d2eSBrian Somers   mp->fsmp.LayerFinish = mp_LayerFinish;
2673b0f8d2eSBrian Somers   mp->fsmp.object = mp;
2683b0f8d2eSBrian Somers 
2691fa665f5SBrian Somers   mpserver_Init(&mp->server);
2701fa665f5SBrian Somers 
27149052c95SBrian Somers   mp->cfg.mrru = 0;
27249052c95SBrian Somers   mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
2737063995cSBrian Somers   mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
27449052c95SBrian Somers   mp->cfg.enddisc.class = 0;
27549052c95SBrian Somers   *mp->cfg.enddisc.address = '\0';
27649052c95SBrian Somers   mp->cfg.enddisc.len = 0;
27749052c95SBrian Somers 
2783b0f8d2eSBrian Somers   lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2793b0f8d2eSBrian Somers   ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
2805d9e6103SBrian Somers 
2815d9e6103SBrian Somers   link_EmptyStack(&mp->link);
2825d9e6103SBrian Somers   link_Stack(&mp->link, &protolayer);
2835d9e6103SBrian Somers   link_Stack(&mp->link, &ccplayer);
2845d9e6103SBrian Somers   link_Stack(&mp->link, &vjlayer);
28567b072f7SBrian Somers #ifndef NONAT
28667b072f7SBrian Somers   link_Stack(&mp->link, &natlayer);
2875d9e6103SBrian Somers #endif
28849052c95SBrian Somers }
28949052c95SBrian Somers 
29049052c95SBrian Somers int
2911fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
29249052c95SBrian Somers {
2931fa665f5SBrian Somers   struct lcp *lcp = &dl->physical->link.lcp;
2941fa665f5SBrian Somers 
29549052c95SBrian Somers   if (mp->active) {
29649052c95SBrian Somers     /* We're adding a link - do a last validation on our parameters */
2971fa665f5SBrian Somers     if (!peerid_Equal(&dl->peer, &mp->peer)) {
298dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
2997ebff009SBrian Somers       log_Printf(LogPHASE, "  Attached to peer %s/%s\n", mp->peer.authname,
3007ebff009SBrian Somers                  mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
3017ebff009SBrian Somers                             mp->peer.enddisc.len));
3027ebff009SBrian Somers       log_Printf(LogPHASE, "  New link is peer %s/%s\n", dl->peer.authname,
3037ebff009SBrian Somers                  mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
3047ebff009SBrian Somers                             dl->peer.enddisc.len));
3051fa665f5SBrian Somers       return MP_FAILED;
306643f4904SBrian Somers     }
3071fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
3081fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
3091fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
3101fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
311dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
3121fa665f5SBrian Somers                 dl->name);
3131fa665f5SBrian Somers       return MP_FAILED;
31449052c95SBrian Somers     }
3151fa665f5SBrian Somers     return MP_ADDED;
31649052c95SBrian Somers   } else {
31749052c95SBrian Somers     /* First link in multilink mode */
31849052c95SBrian Somers 
3191fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
3201fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
3211fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
3221fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
3231fa665f5SBrian Somers     mp->peer = dl->peer;
32449052c95SBrian Somers 
32511572abfSBrian Somers     throughput_destroy(&mp->link.stats.total);
32611572abfSBrian Somers     throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
32711572abfSBrian Somers     throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
328673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
329673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
330673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
331673903ecSBrian Somers 
33211572abfSBrian Somers     /* Tell the link who it belongs to */
33311572abfSBrian Somers     dl->physical->link.stats.parent = &mp->link.stats.total;
33411572abfSBrian Somers 
3359c53a7b1SBrian Somers     mp->out.seq = 0;
3369c53a7b1SBrian Somers     mp->out.link = 0;
337673903ecSBrian Somers     mp->seq.min_in = 0;
338673903ecSBrian Somers     mp->seq.next_in = 0;
339673903ecSBrian Somers 
3401fa665f5SBrian Somers     /*
3411fa665f5SBrian Somers      * Now we create our server socket.
3421fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
3431fa665f5SBrian Somers      */
34496c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
34596c9bb21SBrian Somers     case MPSERVER_CONNECTED:
346dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
3476f384573SBrian Somers                 mp->server.socket.sun_path);
34896c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
3491fa665f5SBrian Somers       return MP_LINKSENT;
35096c9bb21SBrian Somers     case MPSERVER_FAILED:
3511fa665f5SBrian Somers       return MP_FAILED;
35296c9bb21SBrian Somers     case MPSERVER_LISTENING:
353dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
354dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
3551fa665f5SBrian Somers 
35649052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
357ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
3583b0f8d2eSBrian Somers 
3593b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
36006337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
361dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
362dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
36306337856SBrian Somers       }
3643b0f8d2eSBrian Somers 
3653b0f8d2eSBrian Somers       mp->active = 1;
36696c9bb21SBrian Somers       break;
36749052c95SBrian Somers     }
3681fa665f5SBrian Somers   }
3693b0f8d2eSBrian Somers 
3701fa665f5SBrian Somers   return MP_UP;
3713b0f8d2eSBrian Somers }
3723b0f8d2eSBrian Somers 
3733b0f8d2eSBrian Somers void
374673903ecSBrian Somers mp_Down(struct mp *mp)
375673903ecSBrian Somers {
376673903ecSBrian Somers   if (mp->active) {
377673903ecSBrian Somers     struct mbuf *next;
378673903ecSBrian Somers 
379ab2de065SBrian Somers     /* Stop that ! */
380ab2de065SBrian Somers     mp_StopAutoloadTimer(mp);
381ab2de065SBrian Somers 
3821fa665f5SBrian Somers     /* Don't want any more of these */
3831fa665f5SBrian Somers     mpserver_Close(&mp->server);
3841fa665f5SBrian Somers 
3851fa665f5SBrian Somers     /* CCP goes down with a bang */
38609206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
387673903ecSBrian Somers 
388673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
389673903ecSBrian Somers     while (mp->inbufs) {
39026af0ae9SBrian Somers       next = mp->inbufs->m_nextpkt;
39126af0ae9SBrian Somers       m_freem(mp->inbufs);
392673903ecSBrian Somers       mp->inbufs = next;
393673903ecSBrian Somers     }
394673903ecSBrian Somers 
395643f4904SBrian Somers     peerid_Init(&mp->peer);
396673903ecSBrian Somers     mp->active = 0;
397673903ecSBrian Somers   }
398673903ecSBrian Somers }
399673903ecSBrian Somers 
400673903ecSBrian Somers void
4013b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
4023b0f8d2eSBrian Somers {
4033b0f8d2eSBrian Somers   mplink->seq = 0;
404ab2de065SBrian Somers   mplink->bandwidth = 0;
4053b0f8d2eSBrian Somers }
4063b0f8d2eSBrian Somers 
4075d9e6103SBrian Somers static void
4085d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
4093b0f8d2eSBrian Somers {
4103b0f8d2eSBrian Somers   struct mp_header mh, h;
4113b0f8d2eSBrian Somers   struct mbuf *q, *last;
4123b0f8d2eSBrian Somers   int32_t seq;
4133b0f8d2eSBrian Somers 
41486b1f0d7SBrian Somers   /*
41586b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
41686b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
41786b1f0d7SBrian Somers    * as normal
41886b1f0d7SBrian Somers    */
41986b1f0d7SBrian Somers 
42086b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
42126af0ae9SBrian Somers     m_freem(m);
4223b0f8d2eSBrian Somers     return;
4233b0f8d2eSBrian Somers   }
4243b0f8d2eSBrian Somers 
42586b1f0d7SBrian Somers   if (p) {
4263b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
4273b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
42886b1f0d7SBrian Somers   } else
42986b1f0d7SBrian Somers     seq = mp->seq.min_in;
43086b1f0d7SBrian Somers 
4313b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
4323b0f8d2eSBrian Somers     /*
4333b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
4343b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
4353b0f8d2eSBrian Somers      */
4363b0f8d2eSBrian Somers     struct datalink *dl;
4373b0f8d2eSBrian Somers 
43886b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
4393b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
44086b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
44186b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
44286b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
4433b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
4443b0f8d2eSBrian Somers   }
4453b0f8d2eSBrian Somers 
4463b0f8d2eSBrian Somers   /*
4473b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
4483b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
4493b0f8d2eSBrian Somers    * the queue.
4503b0f8d2eSBrian Somers    */
4513b0f8d2eSBrian Somers 
4523b0f8d2eSBrian Somers   last = NULL;
4533b0f8d2eSBrian Somers   seq = mp->seq.next_in;
4543b0f8d2eSBrian Somers   q = mp->inbufs;
455cb8bd8ddSBrian Somers   while (q || m) {
456cb8bd8ddSBrian Somers     if (!q) {
457cb8bd8ddSBrian Somers       if (last)
458cb8bd8ddSBrian Somers         last->m_nextpkt = m;
459cb8bd8ddSBrian Somers       else
460cb8bd8ddSBrian Somers         mp->inbufs = m;
461cb8bd8ddSBrian Somers       q = m;
462cb8bd8ddSBrian Somers       m = NULL;
463cb8bd8ddSBrian Somers       h = mh;
464cb8bd8ddSBrian Somers     } else {
4653b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
466cb8bd8ddSBrian Somers 
4671bcced10SBrian Somers       if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
4683b0f8d2eSBrian Somers         /* Our received fragment fits in before this one, so link it in */
4693b0f8d2eSBrian Somers         if (last)
47026af0ae9SBrian Somers           last->m_nextpkt = m;
4713b0f8d2eSBrian Somers         else
4723b0f8d2eSBrian Somers           mp->inbufs = m;
47326af0ae9SBrian Somers         m->m_nextpkt = q;
4743b0f8d2eSBrian Somers         q = m;
4753b0f8d2eSBrian Somers         h = mh;
4763b0f8d2eSBrian Somers         m = NULL;
4773b0f8d2eSBrian Somers       }
478cb8bd8ddSBrian Somers     }
4793b0f8d2eSBrian Somers 
4803b0f8d2eSBrian Somers     if (h.seq != seq) {
4813b0f8d2eSBrian Somers       /* we're missing something :-( */
48264e0f466SBrian Somers       if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
4833b0f8d2eSBrian Somers         /* we're never gonna get it */
4843b0f8d2eSBrian Somers         struct mbuf *next;
4853b0f8d2eSBrian Somers 
4863b0f8d2eSBrian Somers         /* Zap all older fragments */
4873b0f8d2eSBrian Somers         while (mp->inbufs != q) {
488dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
48926af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
49026af0ae9SBrian Somers           m_freem(mp->inbufs);
4913b0f8d2eSBrian Somers           mp->inbufs = next;
4923b0f8d2eSBrian Somers         }
4933b0f8d2eSBrian Somers 
4943b0f8d2eSBrian Somers         /*
4953b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4963b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4973b0f8d2eSBrian Somers          * comes first.
4983b0f8d2eSBrian Somers          */
4993b0f8d2eSBrian Somers         do {
5003b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
5013b0f8d2eSBrian Somers           if (h.begin) {
502ce828a6eSBrian Somers             /* We might be able to process this ! */
5033b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
5043b0f8d2eSBrian Somers             break;
5053b0f8d2eSBrian Somers           }
50626af0ae9SBrian Somers           next = mp->inbufs->m_nextpkt;
507dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
50826af0ae9SBrian Somers           m_freem(mp->inbufs);
5093b0f8d2eSBrian Somers           mp->inbufs = next;
51064e0f466SBrian Somers         } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
51164e0f466SBrian Somers                                          h.seq) || h.end));
5123b0f8d2eSBrian Somers 
5133b0f8d2eSBrian Somers         /*
5143b0f8d2eSBrian Somers          * Continue processing things from here.
5153b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
5163b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
5173b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
5183b0f8d2eSBrian Somers          * packets that have already been received.
5193b0f8d2eSBrian Somers          */
5203b0f8d2eSBrian Somers 
521147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5223b0f8d2eSBrian Somers         last = NULL;
5233b0f8d2eSBrian Somers         q = mp->inbufs;
5243b0f8d2eSBrian Somers       } else
5253b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
5263b0f8d2eSBrian Somers         break;
5273b0f8d2eSBrian Somers     } else if (h.end) {
5283b0f8d2eSBrian Somers       /* We've got something, reassemble */
5293b0f8d2eSBrian Somers       struct mbuf **frag = &q;
5303b0f8d2eSBrian Somers       int len;
531ce828a6eSBrian Somers       u_long first = -1;
5323b0f8d2eSBrian Somers 
5333b0f8d2eSBrian Somers       do {
5343b0f8d2eSBrian Somers         *frag = mp->inbufs;
53526af0ae9SBrian Somers         mp->inbufs = mp->inbufs->m_nextpkt;
5363b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
537ce828a6eSBrian Somers         if (first == -1)
538ce828a6eSBrian Somers           first = h.seq;
53926af0ae9SBrian Somers         (*frag)->m_offset += len;
54026af0ae9SBrian Somers         (*frag)->m_len -= len;
54126af0ae9SBrian Somers         (*frag)->m_nextpkt = NULL;
5423b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
543dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
5443b0f8d2eSBrian Somers                     (u_long)h.seq);
54526af0ae9SBrian Somers           m_freem(q);
5463b0f8d2eSBrian Somers           q = NULL;
5473b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
548dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
5493b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
5503b0f8d2eSBrian Somers           /*
5513b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
5523b0f8d2eSBrian Somers            * our half-assembed packet.
5533b0f8d2eSBrian Somers            */
55426af0ae9SBrian Somers           (*frag)->m_nextpkt = mp->inbufs;
5553b0f8d2eSBrian Somers           mp->inbufs = *frag;
5563b0f8d2eSBrian Somers           *frag = NULL;
55726af0ae9SBrian Somers           m_freem(q);
5583b0f8d2eSBrian Somers           q = NULL;
5593b0f8d2eSBrian Somers           frag = &q;
5603b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
5613b0f8d2eSBrian Somers         } else
5623b0f8d2eSBrian Somers           do
56326af0ae9SBrian Somers             frag = &(*frag)->m_next;
564ce828a6eSBrian Somers           while (*frag != NULL);
5653b0f8d2eSBrian Somers       } while (!h.end);
5663b0f8d2eSBrian Somers 
5673b0f8d2eSBrian Somers       if (q) {
56826af0ae9SBrian Somers         q = m_pullup(q);
569dd7e2610SBrian Somers         log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
57026af0ae9SBrian Somers                    first, (u_long)h.seq, m_length(q));
57126af0ae9SBrian Somers         link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
57226af0ae9SBrian Somers         m_freem(q);
5733b0f8d2eSBrian Somers       }
5743b0f8d2eSBrian Somers 
575147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5763b0f8d2eSBrian Somers       last = NULL;
5773b0f8d2eSBrian Somers       q = mp->inbufs;
5783b0f8d2eSBrian Somers     } else {
5793b0f8d2eSBrian Somers       /* Look for the next fragment */
580147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5813b0f8d2eSBrian Somers       last = q;
58226af0ae9SBrian Somers       q = q->m_nextpkt;
5833b0f8d2eSBrian Somers     }
5843b0f8d2eSBrian Somers   }
5853b0f8d2eSBrian Somers 
5863b0f8d2eSBrian Somers   if (m) {
5873b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5883b0f8d2eSBrian Somers     last = NULL;
58926af0ae9SBrian Somers     for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
5903b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5911bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
592147613eaSBrian Somers         break;
593147613eaSBrian Somers     }
594147613eaSBrian Somers     /* Our received fragment fits in here */
5953b0f8d2eSBrian Somers     if (last)
59626af0ae9SBrian Somers       last->m_nextpkt = m;
5973b0f8d2eSBrian Somers     else
5983b0f8d2eSBrian Somers       mp->inbufs = m;
59926af0ae9SBrian Somers     m->m_nextpkt = q;
6003b0f8d2eSBrian Somers   }
6013b0f8d2eSBrian Somers }
6023b0f8d2eSBrian Somers 
6035d9e6103SBrian Somers struct mbuf *
6045d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
6055d9e6103SBrian Somers {
6065d9e6103SBrian Somers   struct physical *p = link2physical(l);
6075d9e6103SBrian Somers 
6085d9e6103SBrian Somers   if (!bundle->ncp.mp.active)
6095d9e6103SBrian Somers     /* Let someone else deal with it ! */
6105d9e6103SBrian Somers     return bp;
6115d9e6103SBrian Somers 
6125d9e6103SBrian Somers   if (p == NULL) {
6135d9e6103SBrian Somers     log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
61426af0ae9SBrian Somers     m_freem(bp);
615411675baSBrian Somers   } else {
61626af0ae9SBrian Somers     m_settype(bp, MB_MPIN);
6175d9e6103SBrian Somers     mp_Assemble(&bundle->ncp.mp, bp, p);
618411675baSBrian Somers   }
6195d9e6103SBrian Somers 
6205d9e6103SBrian Somers   return NULL;
6215d9e6103SBrian Somers }
6225d9e6103SBrian Somers 
6233b0f8d2eSBrian Somers static void
6245d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
6255d9e6103SBrian Somers           struct mbuf *m, u_int32_t begin, u_int32_t end)
6263b0f8d2eSBrian Somers {
62726af0ae9SBrian Somers   char prepend[4];
6283b0f8d2eSBrian Somers 
629ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
63026af0ae9SBrian Somers 
63149052c95SBrian Somers   if (mp->peer_is12bit) {
6329e8ec64bSBrian Somers     u_int16_t val;
633ce828a6eSBrian Somers 
6349e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
63526af0ae9SBrian Somers     ua_htons(&val, prepend);
63626af0ae9SBrian Somers     m = m_prepend(m, prepend, 2, 0);
6373b0f8d2eSBrian Somers   } else {
6389e8ec64bSBrian Somers     u_int32_t val;
639ce828a6eSBrian Somers 
6409e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
64126af0ae9SBrian Somers     ua_htonl(&val, prepend);
64226af0ae9SBrian Somers     m = m_prepend(m, prepend, 4, 0);
6433b0f8d2eSBrian Somers   }
644dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
6459c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
64626af0ae9SBrian Somers                mp->out.seq, m_length(m), l->name);
647147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
6483b0f8d2eSBrian Somers 
64926af0ae9SBrian Somers   link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
6503b0f8d2eSBrian Somers }
6513b0f8d2eSBrian Somers 
6523b0f8d2eSBrian Somers int
6533b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
6543b0f8d2eSBrian Somers {
6553b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
6569c53a7b1SBrian Somers   struct datalink *dl, *fdl;
65726af0ae9SBrian Somers   size_t total, add, len;
65826af0ae9SBrian Somers   int thislink, nlinks;
659b4b280abSBrian Somers   u_int32_t begin, end;
6603b0f8d2eSBrian Somers   struct mbuf *m, *mo;
6613b0f8d2eSBrian Somers 
6629c53a7b1SBrian Somers   thislink = nlinks = 0;
6639c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
66486b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
6659c53a7b1SBrian Somers     if (!fdl) {
6669c53a7b1SBrian Somers       if (thislink == mp->out.link)
6679c53a7b1SBrian Somers         fdl = dl;
6689c53a7b1SBrian Somers       else
6699c53a7b1SBrian Somers         thislink++;
6709c53a7b1SBrian Somers     }
6719c53a7b1SBrian Somers     nlinks++;
6729c53a7b1SBrian Somers   }
6739c53a7b1SBrian Somers 
6749c53a7b1SBrian Somers   if (!fdl) {
6759c53a7b1SBrian Somers     fdl = bundle->links;
6761bc9b5baSBrian Somers     if (!fdl)
6771bc9b5baSBrian Somers       return 0;
6789c53a7b1SBrian Somers     thislink = 0;
6799c53a7b1SBrian Somers   }
6803b0f8d2eSBrian Somers 
6813b0f8d2eSBrian Somers   total = 0;
6829c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6839c53a7b1SBrian Somers     if (!dl) {
6849c53a7b1SBrian Somers       dl = bundle->links;
6859c53a7b1SBrian Somers       thislink = 0;
6869c53a7b1SBrian Somers     }
6879c53a7b1SBrian Somers 
6886f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
6896f384573SBrian Somers       continue;
6909c53a7b1SBrian Somers 
6913b0f8d2eSBrian Somers     if (dl->physical->out)
6923b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
6933b0f8d2eSBrian Somers       continue;
6949c53a7b1SBrian Somers 
6953b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
6963b0f8d2eSBrian Somers     total += add;
6973b0f8d2eSBrian Somers     if (add)
6983b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6993b0f8d2eSBrian Somers       continue;
7009c53a7b1SBrian Somers 
7015d9e6103SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
7023b0f8d2eSBrian Somers       /* Nothing else to send */
7033b0f8d2eSBrian Somers       break;
7043b0f8d2eSBrian Somers 
7053b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
70626af0ae9SBrian Somers     len = m_length(m);
7073b0f8d2eSBrian Somers     begin = 1;
7083b0f8d2eSBrian Somers     end = 0;
7093b0f8d2eSBrian Somers 
7109c53a7b1SBrian Somers     while (!end) {
7119c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
712ab2de065SBrian Somers         /* Write at most his_mru bytes to the physical link */
713ab2de065SBrian Somers         if (len <= dl->physical->link.lcp.his_mru) {
7143b0f8d2eSBrian Somers           mo = m;
7153b0f8d2eSBrian Somers           end = 1;
71626af0ae9SBrian Somers           m_settype(mo, MB_MPOUT);
7173b0f8d2eSBrian Somers         } else {
718ab2de065SBrian Somers           /* It's > his_mru, chop the packet (`m') into bits */
71926af0ae9SBrian Somers           mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
72026af0ae9SBrian Somers           len -= mo->m_len;
72126af0ae9SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
7223b0f8d2eSBrian Somers         }
7235d9e6103SBrian Somers         mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
7243b0f8d2eSBrian Somers         begin = 0;
7253b0f8d2eSBrian Somers       }
7269c53a7b1SBrian Somers 
7279c53a7b1SBrian Somers       if (!end) {
7289c53a7b1SBrian Somers         nlinks--;
7299c53a7b1SBrian Somers         dl = dl->next;
7309c53a7b1SBrian Somers         if (!dl) {
7319c53a7b1SBrian Somers           dl = bundle->links;
7329c53a7b1SBrian Somers           thislink = 0;
7339c53a7b1SBrian Somers         } else
7349c53a7b1SBrian Somers           thislink++;
7353b0f8d2eSBrian Somers       }
7369c53a7b1SBrian Somers     }
7379c53a7b1SBrian Somers   }
7389c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
7393b0f8d2eSBrian Somers 
7403b0f8d2eSBrian Somers   return total;
7413b0f8d2eSBrian Somers }
7423b0f8d2eSBrian Somers 
7433b0f8d2eSBrian Somers int
744ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg)
7453b0f8d2eSBrian Somers {
7463b0f8d2eSBrian Somers   int val;
7473b0f8d2eSBrian Somers 
74825092092SBrian Somers   if (arg->argc != arg->argn+1)
7493b0f8d2eSBrian Somers     return -1;
7503b0f8d2eSBrian Somers 
75125092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
752ab2de065SBrian Somers   if (val <= 0) {
753ab2de065SBrian Somers     log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
7543b0f8d2eSBrian Somers     return 1;
7553b0f8d2eSBrian Somers   }
756ab2de065SBrian Somers   arg->cx->mp.bandwidth = val;
757ab2de065SBrian Somers 
758ab2de065SBrian Somers   if (arg->cx->state == DATALINK_OPEN)
759ab2de065SBrian Somers     bundle_CalculateBandwidth(arg->bundle);
760ab2de065SBrian Somers 
7613b0f8d2eSBrian Somers   return 0;
7623b0f8d2eSBrian Somers }
76349052c95SBrian Somers 
76449052c95SBrian Somers int
76549052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
76649052c95SBrian Somers {
76749052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
76849052c95SBrian Somers 
76949052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
7709c53a7b1SBrian Somers   if (mp->active) {
771411675baSBrian Somers     struct mbuf *m, *lm;
7729c53a7b1SBrian Somers     int bufs = 0;
7739c53a7b1SBrian Somers 
774fa0d5216SBrian Somers     lm = NULL;
7751fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
7766f384573SBrian Somers                   mp->server.socket.sun_path);
77726af0ae9SBrian Somers     for (m = mp->inbufs; m; m = m->m_nextpkt) {
7789c53a7b1SBrian Somers       bufs++;
779411675baSBrian Somers       lm = m;
780411675baSBrian Somers     }
781411675baSBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d", bufs);
782411675baSBrian Somers     if (bufs) {
783411675baSBrian Somers       struct mp_header mh;
784411675baSBrian Somers       unsigned long first, last;
785411675baSBrian Somers 
786411675baSBrian Somers       first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
787411675baSBrian Somers       last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
78864e0f466SBrian Somers       prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
789411675baSBrian Somers                     first, last, (unsigned long)mp->seq.next_in,
790411675baSBrian Somers                     (unsigned long)mp->seq.min_in);
79164e0f466SBrian Somers       prompt_Printf(arg->prompt, "                First has %sbegin bit and "
79264e0f466SBrian Somers                     "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
793411675baSBrian Somers     }
794411675baSBrian Somers     prompt_Printf(arg->prompt, "\n");
7959c53a7b1SBrian Somers   }
79649052c95SBrian Somers 
79749052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
79849052c95SBrian Somers   if (mp->active) {
799ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Output SEQ:    %u\n", mp->out.seq);
80049052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
80149052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
80249052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
80349052c95SBrian Somers   }
804643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
80549052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
80649052c95SBrian Somers                            mp->cfg.enddisc.len));
80749052c95SBrian Somers 
80849052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
80949052c95SBrian Somers   if (mp->active) {
810643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
811ab2de065SBrian Somers     prompt_Printf(arg->prompt, " Input SEQ:     %u\n", mp->seq.next_in);
81249052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
81349052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
81449052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
81549052c95SBrian Somers   }
816643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
817643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
818643f4904SBrian Somers                            mp->peer.enddisc.len));
81949052c95SBrian Somers 
82049052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
82149052c95SBrian Somers 
82249052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
82349052c95SBrian Somers   if (mp->cfg.mrru)
82449052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
82549052c95SBrian Somers   else
82649052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
82749052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
82849052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
8297063995cSBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
8307063995cSBrian Somers                   command_ShowNegval(mp->cfg.negenddisc));
831ab2de065SBrian Somers   prompt_Printf(arg->prompt, " AutoLoad:      min %d%%, max %d%%,"
832ab2de065SBrian Somers                 " period %d secs\n", mp->cfg.autoload.min,
833ab2de065SBrian Somers                 mp->cfg.autoload.max, mp->cfg.autoload.period);
83449052c95SBrian Somers 
83549052c95SBrian Somers   return 0;
83649052c95SBrian Somers }
83749052c95SBrian Somers 
83849052c95SBrian Somers const char *
83949052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
84049052c95SBrian Somers {
841d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
84249052c95SBrian Somers   int f, header;
84349052c95SBrian Somers 
84449052c95SBrian Somers   switch (c) {
84508676022SBrian Somers     case ENDDISC_NULL:
84649052c95SBrian Somers       sprintf(result, "Null Class");
84749052c95SBrian Somers       break;
84849052c95SBrian Somers 
84908676022SBrian Somers     case ENDDISC_LOCAL:
85049052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
85149052c95SBrian Somers       break;
85249052c95SBrian Somers 
85308676022SBrian Somers     case ENDDISC_IP:
85449052c95SBrian Somers       if (len == 4)
85549052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
85649052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
85749052c95SBrian Somers       else
85849052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
85949052c95SBrian Somers       break;
86049052c95SBrian Somers 
86108676022SBrian Somers     case ENDDISC_MAC:
86249052c95SBrian Somers       if (len == 6) {
86349052c95SBrian Somers         const u_char *m = (const u_char *)address;
86449052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
86549052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
86649052c95SBrian Somers       } else
86749052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
86849052c95SBrian Somers       break;
86949052c95SBrian Somers 
87008676022SBrian Somers     case ENDDISC_MAGIC:
87149052c95SBrian Somers       sprintf(result, "Magic: 0x");
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 
87908676022SBrian Somers     case ENDDISC_PSN:
88049052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
88149052c95SBrian Somers       break;
88249052c95SBrian Somers 
88349052c95SBrian Somers      default:
88449052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
88549052c95SBrian Somers       header = strlen(result);
88649052c95SBrian Somers       if (len > sizeof result - header - 1)
88749052c95SBrian Somers         len = sizeof result - header - 1;
88849052c95SBrian Somers       for (f = 0; f < len; f++)
88949052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
89049052c95SBrian Somers       break;
89149052c95SBrian Somers   }
89249052c95SBrian Somers   return result;
89349052c95SBrian Somers }
89449052c95SBrian Somers 
89549052c95SBrian Somers int
89649052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
89749052c95SBrian Somers {
89849052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
899d47dceb8SBrian Somers   struct in_addr addr;
90049052c95SBrian Somers 
9019b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
9029b5f8ffdSBrian Somers     case PHASE_DEAD:
9039b5f8ffdSBrian Somers       break;
9049b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
9059b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
9069b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
9079b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
9089b5f8ffdSBrian Somers                    " LCP negotiations\n");
9099b5f8ffdSBrian Somers         return 1;
9109b5f8ffdSBrian Somers       }
9119b5f8ffdSBrian Somers       break;
9129b5f8ffdSBrian Somers     default:
9139b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
91449052c95SBrian Somers       return 1;
91549052c95SBrian Somers   }
91649052c95SBrian Somers 
91749052c95SBrian Somers   if (arg->argc == arg->argn) {
91849052c95SBrian Somers     mp->cfg.enddisc.class = 0;
91949052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
92049052c95SBrian Somers     mp->cfg.enddisc.len = 0;
921e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
92208676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
92308676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
92408676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
92508676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
92608676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
927259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
928259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
929d47dceb8SBrian Somers       else
930259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
931d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
93208676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
93349052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
93408676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
93508676022SBrian Somers       struct sockaddr_dl hwaddr;
93608676022SBrian Somers       int s;
93708676022SBrian Somers 
938259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
939259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
940d47dceb8SBrian Somers       else
941259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
942d47dceb8SBrian Somers 
94308676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
94408676022SBrian Somers       if (s < 0) {
945dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
94608676022SBrian Somers         return 2;
94708676022SBrian Somers       }
948d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
94908676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
95008676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
95108676022SBrian Somers                hwaddr.sdl_alen);
95208676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
95308676022SBrian Somers       } else {
954dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
955259a6191SBrian Somers                   inet_ntoa(addr));
95608676022SBrian Somers         close(s);
95708676022SBrian Somers         return 4;
95808676022SBrian Somers       }
95908676022SBrian Somers       close(s);
96049052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
96149052c95SBrian Somers       int f;
96249052c95SBrian Somers 
96349052c95SBrian Somers       randinit();
96449052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
96549052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
96608676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
96749052c95SBrian Somers       mp->cfg.enddisc.len = 20;
96849052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
96949052c95SBrian Somers       if (arg->argc > arg->argn+1) {
97008676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
97149052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
97249052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
97349052c95SBrian Somers       } else {
974dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
97508676022SBrian Somers         return 5;
97649052c95SBrian Somers       }
97749052c95SBrian Somers     } else {
978dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
97949052c95SBrian Somers                 arg->argv[arg->argn]);
98008676022SBrian Somers       return 6;
98149052c95SBrian Somers     }
982e43ebac1SBrian Somers   }
98349052c95SBrian Somers 
98449052c95SBrian Somers   return 0;
98549052c95SBrian Somers }
9861fa665f5SBrian Somers 
9871fa665f5SBrian Somers static int
988f013f33eSBrian Somers mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
9891fa665f5SBrian Somers                    int *n)
9901fa665f5SBrian Somers {
9911fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9920f2f3eb3SBrian Somers   int result;
9931fa665f5SBrian Somers 
9940f2f3eb3SBrian Somers   result = 0;
9956f384573SBrian Somers   if (s->send.dl != NULL) {
99696c9bb21SBrian Somers     /* We've connect()ed */
997b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
998b7c5748eSBrian Somers         !s->send.dl->physical->out) {
999b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
10000f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
100196c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
10026f384573SBrian Somers       s->send.dl = NULL;
100396c9bb21SBrian Somers       s->fd = -1;
1004ea722969SBrian Somers     } else
1005ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
10060f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
100796c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
10081fa665f5SBrian Somers     if (*n < s->fd + 1)
10091fa665f5SBrian Somers       *n = s->fd + 1;
10101fa665f5SBrian Somers     FD_SET(s->fd, r);
101124989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
10120f2f3eb3SBrian Somers     result++;
10131fa665f5SBrian Somers   }
10140f2f3eb3SBrian Somers   return result;
10151fa665f5SBrian Somers }
10161fa665f5SBrian Somers 
10171fa665f5SBrian Somers static int
1018f013f33eSBrian Somers mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
10191fa665f5SBrian Somers {
10201fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10211fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
10221fa665f5SBrian Somers }
10231fa665f5SBrian Somers 
10241fa665f5SBrian Somers static void
1025f013f33eSBrian Somers mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
10261fa665f5SBrian Somers {
10271fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
10281fa665f5SBrian Somers 
10292cb305afSBrian Somers   bundle_ReceiveDatalink(bundle, s->fd);
10301fa665f5SBrian Somers }
10311fa665f5SBrian Somers 
10321af29a6eSBrian Somers static int
1033f013f33eSBrian Somers mpserver_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
10341fa665f5SBrian Somers {
10351fa665f5SBrian Somers   /* We never want to write here ! */
1036a33b2ef7SBrian Somers   log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
10371af29a6eSBrian Somers   return 0;
10381fa665f5SBrian Somers }
10391fa665f5SBrian Somers 
10401fa665f5SBrian Somers void
10411fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
10421fa665f5SBrian Somers {
10431fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
10441fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
10451fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
10461fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
10471fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
10486f384573SBrian Somers   s->send.dl = NULL;
10491fa665f5SBrian Somers   s->fd = -1;
10506f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
10511fa665f5SBrian Somers }
10521fa665f5SBrian Somers 
10531fa665f5SBrian Somers int
10541fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
10551fa665f5SBrian Somers {
1056cbee9754SBrian Somers   int f, l;
10571fa665f5SBrian Somers   mode_t mask;
10581fa665f5SBrian Somers 
10591fa665f5SBrian Somers   if (s->fd != -1) {
1060a33b2ef7SBrian Somers     log_Printf(LogALERT, "Internal error !  mpserver already open\n");
10616f384573SBrian Somers     mpserver_Close(s);
10621fa665f5SBrian Somers   }
10631fa665f5SBrian Somers 
10646f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
10656f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
10661fa665f5SBrian Somers 
10676f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
10686f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
10691fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
10706f384573SBrian Somers     l += 2;
10711fa665f5SBrian Somers   }
10721fa665f5SBrian Somers 
10736f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
10746f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
10752cb305afSBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
10761fa665f5SBrian Somers   if (s->fd < 0) {
10772cb305afSBrian Somers     log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
107896c9bb21SBrian Somers     return MPSERVER_FAILED;
10791fa665f5SBrian Somers   }
10801fa665f5SBrian Somers 
10816f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
10826f384573SBrian Somers              sizeof s->socket);
10831fa665f5SBrian Somers   mask = umask(0177);
10842cb305afSBrian Somers 
10852cb305afSBrian Somers   /*
1086cbee9754SBrian Somers    * Try to bind the socket.  If we succeed we play server, if we fail
1087cbee9754SBrian Somers    * we connect() and hand the link off.
10882cb305afSBrian Somers    */
10892cb305afSBrian Somers 
10906f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
10916f384573SBrian Somers     if (errno != EADDRINUSE) {
1092dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
10936f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10941fa665f5SBrian Somers       umask(mask);
10951fa665f5SBrian Somers       close(s->fd);
10961fa665f5SBrian Somers       s->fd = -1;
109796c9bb21SBrian Somers       return MPSERVER_FAILED;
10986f384573SBrian Somers     }
10992cb305afSBrian Somers 
1100cbee9754SBrian Somers     /* So we're the sender */
11016f384573SBrian Somers     umask(mask);
11026f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
1103dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
11046f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
11056f384573SBrian Somers       if (errno == ECONNREFUSED)
11062cb305afSBrian Somers         log_Printf(LogPHASE, "          The previous server died badly !\n");
11076f384573SBrian Somers       close(s->fd);
11086f384573SBrian Somers       s->fd = -1;
110996c9bb21SBrian Somers       return MPSERVER_FAILED;
11106f384573SBrian Somers     }
11116f384573SBrian Somers 
11126f384573SBrian Somers     /* Donate our link to the other guy */
111396c9bb21SBrian Somers     return MPSERVER_CONNECTED;
11141fa665f5SBrian Somers   }
11156f384573SBrian Somers 
111696c9bb21SBrian Somers   return MPSERVER_LISTENING;
11171fa665f5SBrian Somers }
11181fa665f5SBrian Somers 
11191fa665f5SBrian Somers void
11201fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
11211fa665f5SBrian Somers {
11226f384573SBrian Somers   if (s->send.dl != NULL) {
112396c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
11246f384573SBrian Somers     s->send.dl = NULL;
112596c9bb21SBrian Somers     s->fd = -1;
112696c9bb21SBrian Somers   } else if (s->fd >= 0) {
11271fa665f5SBrian Somers     close(s->fd);
11286f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1129dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
11306f384573SBrian Somers                 strerror(errno));
11316f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
11321fa665f5SBrian Somers     s->fd = -1;
11331fa665f5SBrian Somers   }
11341fa665f5SBrian Somers }
113586b1f0d7SBrian Somers 
113686b1f0d7SBrian Somers void
113786b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
113886b1f0d7SBrian Somers {
113986b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
114086b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
11415d9e6103SBrian Somers     mp_Assemble(mp, NULL, NULL);
114286b1f0d7SBrian Somers }
11436f8e9f0aSBrian Somers 
11446f8e9f0aSBrian Somers void
11456f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
11466f8e9f0aSBrian Somers {
11476f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
11486f8e9f0aSBrian Somers }
1149