13b0f8d2eSBrian Somers /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
43b0f8d2eSBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
53b0f8d2eSBrian Somers * All rights reserved.
63b0f8d2eSBrian Somers *
73b0f8d2eSBrian Somers * Redistribution and use in source and binary forms, with or without
83b0f8d2eSBrian Somers * modification, are permitted provided that the following conditions
93b0f8d2eSBrian Somers * are met:
103b0f8d2eSBrian Somers * 1. Redistributions of source code must retain the above copyright
113b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer.
123b0f8d2eSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright
133b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer in the
143b0f8d2eSBrian Somers * documentation and/or other materials provided with the distribution.
153b0f8d2eSBrian Somers *
163b0f8d2eSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173b0f8d2eSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183b0f8d2eSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193b0f8d2eSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203b0f8d2eSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213b0f8d2eSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223b0f8d2eSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233b0f8d2eSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243b0f8d2eSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253b0f8d2eSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263b0f8d2eSBrian Somers * SUCH DAMAGE.
273b0f8d2eSBrian Somers */
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>
406eafd353SBrian Somers #include <stdarg.h>
4149052c95SBrian Somers #include <stdio.h>
426eafd353SBrian Somers #include <stdlib.h>
433b0f8d2eSBrian Somers #include <string.h>
441fa665f5SBrian Somers #include <sys/stat.h>
453b0f8d2eSBrian Somers #include <termios.h>
4608676022SBrian Somers #include <unistd.h>
473b0f8d2eSBrian Somers
485d9e6103SBrian Somers #include "layer.h"
4967b072f7SBrian Somers #ifndef NONAT
50f02c2029SBrian Somers #include "nat_cmd.h"
515d9e6103SBrian Somers #endif
525d9e6103SBrian Somers #include "vjcomp.h"
539e8ec64bSBrian Somers #include "ua.h"
54c9e11a11SBrian Somers #include "defs.h"
553b0f8d2eSBrian Somers #include "command.h"
563b0f8d2eSBrian Somers #include "mbuf.h"
573b0f8d2eSBrian Somers #include "log.h"
583b0f8d2eSBrian Somers #include "timer.h"
593b0f8d2eSBrian Somers #include "fsm.h"
603b0f8d2eSBrian Somers #include "iplist.h"
613b0f8d2eSBrian Somers #include "throughput.h"
623b0f8d2eSBrian Somers #include "slcompress.h"
635a72b6edSBrian Somers #include "lqr.h"
645a72b6edSBrian Somers #include "hdlc.h"
6530949fd4SBrian Somers #include "ncpaddr.h"
663b0f8d2eSBrian Somers #include "ipcp.h"
673b0f8d2eSBrian Somers #include "auth.h"
683b0f8d2eSBrian Somers #include "lcp.h"
693b0f8d2eSBrian Somers #include "async.h"
703b0f8d2eSBrian Somers #include "ccp.h"
713b0f8d2eSBrian Somers #include "link.h"
723b0f8d2eSBrian Somers #include "descriptor.h"
733b0f8d2eSBrian Somers #include "physical.h"
743b0f8d2eSBrian Somers #include "chat.h"
755d9e6103SBrian Somers #include "proto.h"
763b0f8d2eSBrian Somers #include "filter.h"
773b0f8d2eSBrian Somers #include "mp.h"
783b0f8d2eSBrian Somers #include "chap.h"
7992b09558SBrian Somers #include "cbcp.h"
803b0f8d2eSBrian Somers #include "datalink.h"
81972a1bcfSBrian Somers #ifndef NORADIUS
82972a1bcfSBrian Somers #include "radius.h"
83972a1bcfSBrian Somers #endif
8430949fd4SBrian Somers #include "ipv6cp.h"
8530949fd4SBrian Somers #include "ncp.h"
863b0f8d2eSBrian Somers #include "bundle.h"
8749052c95SBrian Somers #include "prompt.h"
8808676022SBrian Somers #include "id.h"
8908676022SBrian Somers #include "arp.h"
903b0f8d2eSBrian Somers
91643f4904SBrian Somers void
peerid_Init(struct peerid * peer)92643f4904SBrian Somers peerid_Init(struct peerid *peer)
93643f4904SBrian Somers {
94643f4904SBrian Somers peer->enddisc.class = 0;
95643f4904SBrian Somers *peer->enddisc.address = '\0';
96643f4904SBrian Somers peer->enddisc.len = 0;
97643f4904SBrian Somers *peer->authname = '\0';
98643f4904SBrian Somers }
99643f4904SBrian Somers
100643f4904SBrian Somers int
peerid_Equal(const struct peerid * p1,const struct peerid * p2)101643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
102643f4904SBrian Somers {
103643f4904SBrian Somers return !strcmp(p1->authname, p2->authname) &&
104643f4904SBrian Somers p1->enddisc.class == p2->enddisc.class &&
105643f4904SBrian Somers p1->enddisc.len == p2->enddisc.len &&
106643f4904SBrian Somers !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
107643f4904SBrian Somers }
108643f4904SBrian Somers
1093b0f8d2eSBrian Somers static u_int32_t
inc_seq(unsigned is12bit,u_int32_t seq)110147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
1113b0f8d2eSBrian Somers {
1123b0f8d2eSBrian Somers seq++;
113147613eaSBrian Somers if (is12bit) {
1143b0f8d2eSBrian Somers if (seq & 0xfffff000)
1153b0f8d2eSBrian Somers seq = 0;
1163b0f8d2eSBrian Somers } else if (seq & 0xff000000)
1173b0f8d2eSBrian Somers seq = 0;
1183b0f8d2eSBrian Somers return seq;
1193b0f8d2eSBrian Somers }
1203b0f8d2eSBrian Somers
1213b0f8d2eSBrian Somers static int
isbefore(unsigned is12bit,u_int32_t seq1,u_int32_t seq2)1221bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1231bcced10SBrian Somers {
124b4b280abSBrian Somers u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
125b4b280abSBrian Somers
126b4b280abSBrian Somers if (seq1 > max) {
127b4b280abSBrian Somers if (seq2 < 0x200 || seq2 > seq1)
128b4b280abSBrian Somers return 1;
129b4b280abSBrian Somers } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
130b4b280abSBrian Somers return 1;
131b4b280abSBrian Somers
132b4b280abSBrian Somers return 0;
1331bcced10SBrian Somers }
1341bcced10SBrian Somers
1351bcced10SBrian Somers static int
mp_ReadHeader(struct mp * mp,struct mbuf * m,struct mp_header * header)1363b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1373b0f8d2eSBrian Somers {
13849052c95SBrian Somers if (mp->local_is12bit) {
1399e8ec64bSBrian Somers u_int16_t val;
1409e8ec64bSBrian Somers
1419e8ec64bSBrian Somers ua_ntohs(MBUF_CTOP(m), &val);
1429e8ec64bSBrian Somers if (val & 0x3000) {
143dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1443b0f8d2eSBrian Somers return 0;
1453b0f8d2eSBrian Somers }
1469e8ec64bSBrian Somers header->begin = val & 0x8000 ? 1 : 0;
1479e8ec64bSBrian Somers header->end = val & 0x4000 ? 1 : 0;
1489e8ec64bSBrian Somers header->seq = val & 0x0fff;
1493b0f8d2eSBrian Somers return 2;
1503b0f8d2eSBrian Somers } else {
1519e8ec64bSBrian Somers ua_ntohl(MBUF_CTOP(m), &header->seq);
1523b0f8d2eSBrian Somers if (header->seq & 0x3f000000) {
153dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1543b0f8d2eSBrian Somers return 0;
1553b0f8d2eSBrian Somers }
156ce828a6eSBrian Somers header->begin = header->seq & 0x80000000 ? 1 : 0;
157ce828a6eSBrian Somers header->end = header->seq & 0x40000000 ? 1 : 0;
1583b0f8d2eSBrian Somers header->seq &= 0x00ffffff;
1593b0f8d2eSBrian Somers return 4;
1603b0f8d2eSBrian Somers }
1613b0f8d2eSBrian Somers }
1623b0f8d2eSBrian Somers
1633b0f8d2eSBrian Somers static void
mp_LayerStart(void * v __unused,struct fsm * fp __unused)164057f1760SBrian Somers mp_LayerStart(void *v __unused, struct fsm *fp __unused)
1653b0f8d2eSBrian Somers {
16649052c95SBrian Somers /* The given FSM (ccp) is about to start up ! */
1673b0f8d2eSBrian Somers }
1683b0f8d2eSBrian Somers
1693b0f8d2eSBrian Somers static void
mp_LayerUp(void * v __unused,struct fsm * fp)170057f1760SBrian Somers mp_LayerUp(void *v __unused, struct fsm *fp)
1713b0f8d2eSBrian Somers {
17249052c95SBrian Somers /* The given fsm (ccp) is now up */
1736301d506SBrian Somers
1746301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */
1753b0f8d2eSBrian Somers }
1763b0f8d2eSBrian Somers
1773b0f8d2eSBrian Somers static void
mp_LayerDown(void * v __unused,struct fsm * fp __unused)178057f1760SBrian Somers mp_LayerDown(void *v __unused, struct fsm *fp __unused)
1793b0f8d2eSBrian Somers {
18049052c95SBrian Somers /* The given FSM (ccp) has been told to come down */
1813b0f8d2eSBrian Somers }
1823b0f8d2eSBrian Somers
1833b0f8d2eSBrian Somers static void
mp_LayerFinish(void * v __unused,struct fsm * fp)184057f1760SBrian Somers mp_LayerFinish(void *v __unused, struct fsm *fp)
1853b0f8d2eSBrian Somers {
18649052c95SBrian Somers /* The given fsm (ccp) is now down */
1870a1b5c9dSBrian Somers if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1880a1b5c9dSBrian Somers fsm_Open(fp); /* CCP goes to ST_STOPPED */
1893b0f8d2eSBrian Somers }
1903b0f8d2eSBrian Somers
191ab2de065SBrian Somers static void
mp_UpDown(void * v)192ab2de065SBrian Somers mp_UpDown(void *v)
193ab2de065SBrian Somers {
194ab2de065SBrian Somers struct mp *mp = (struct mp *)v;
195ab2de065SBrian Somers int percent;
196ab2de065SBrian Somers
19791cbd2eeSBrian Somers percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
19891cbd2eeSBrian Somers mp->link.stats.total.out.OctetsPerSecond) * 800 /
19991cbd2eeSBrian Somers mp->bundle->bandwidth;
200ab2de065SBrian Somers if (percent >= mp->cfg.autoload.max) {
201ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
202ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
203ab2de065SBrian Somers } else if (percent <= mp->cfg.autoload.min) {
204ab2de065SBrian Somers log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
205ab2de065SBrian Somers bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
206ab2de065SBrian Somers }
207ab2de065SBrian Somers }
208ab2de065SBrian Somers
209ab2de065SBrian Somers void
mp_StopAutoloadTimer(struct mp * mp)210ab2de065SBrian Somers mp_StopAutoloadTimer(struct mp *mp)
211ab2de065SBrian Somers {
21211572abfSBrian Somers throughput_stop(&mp->link.stats.total);
213ab2de065SBrian Somers }
214ab2de065SBrian Somers
215ab2de065SBrian Somers void
mp_CheckAutoloadTimer(struct mp * mp)216ab2de065SBrian Somers mp_CheckAutoloadTimer(struct mp *mp)
217ab2de065SBrian Somers {
21811572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
21911572abfSBrian Somers throughput_destroy(&mp->link.stats.total);
22011572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
22111572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
222ab2de065SBrian Somers }
223ab2de065SBrian Somers
224ab2de065SBrian Somers if (bundle_WantAutoloadTimer(mp->bundle))
22511572abfSBrian Somers throughput_start(&mp->link.stats.total, "MP throughput", 1);
226ab2de065SBrian Somers else
227ab2de065SBrian Somers mp_StopAutoloadTimer(mp);
228ab2de065SBrian Somers }
229ab2de065SBrian Somers
230ab2de065SBrian Somers void
mp_RestartAutoloadTimer(struct mp * mp)231ab2de065SBrian Somers mp_RestartAutoloadTimer(struct mp *mp)
232ab2de065SBrian Somers {
23311572abfSBrian Somers if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
234ab2de065SBrian Somers mp_CheckAutoloadTimer(mp);
235ab2de065SBrian Somers else
23611572abfSBrian Somers throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
237ab2de065SBrian Somers }
238ab2de065SBrian Somers
2393b0f8d2eSBrian Somers void
mp_Init(struct mp * mp,struct bundle * bundle)2403b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
2413b0f8d2eSBrian Somers {
24249052c95SBrian Somers mp->peer_is12bit = mp->local_is12bit = 0;
24349052c95SBrian Somers mp->peer_mrru = mp->local_mrru = 0;
244643f4904SBrian Somers
245643f4904SBrian Somers peerid_Init(&mp->peer);
246643f4904SBrian Somers
2479c53a7b1SBrian Somers mp->out.seq = 0;
2489c53a7b1SBrian Somers mp->out.link = 0;
24930949fd4SBrian Somers mp->out.af = AF_INET;
2503b0f8d2eSBrian Somers mp->seq.min_in = 0;
2513b0f8d2eSBrian Somers mp->seq.next_in = 0;
2523b0f8d2eSBrian Somers mp->inbufs = NULL;
2533b0f8d2eSBrian Somers mp->bundle = bundle;
2543b0f8d2eSBrian Somers
2555d9e6103SBrian Somers mp->link.type = LOGICAL_LINK;
2563b0f8d2eSBrian Somers mp->link.name = "mp";
2573b0f8d2eSBrian Somers mp->link.len = sizeof *mp;
258643f4904SBrian Somers
259ab2de065SBrian Somers mp->cfg.autoload.period = SAMPLE_PERIOD;
260ab2de065SBrian Somers mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
26111572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
26211572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
26311572abfSBrian Somers mp->link.stats.parent = NULL;
26411572abfSBrian Somers mp->link.stats.gather = 0; /* Let the physical links gather stats */
2653b0f8d2eSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
2663b0f8d2eSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
2673b0f8d2eSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
2683b0f8d2eSBrian Somers
2693b0f8d2eSBrian Somers mp->fsmp.LayerStart = mp_LayerStart;
2703b0f8d2eSBrian Somers mp->fsmp.LayerUp = mp_LayerUp;
2713b0f8d2eSBrian Somers mp->fsmp.LayerDown = mp_LayerDown;
2723b0f8d2eSBrian Somers mp->fsmp.LayerFinish = mp_LayerFinish;
2733b0f8d2eSBrian Somers mp->fsmp.object = mp;
2743b0f8d2eSBrian Somers
2751fa665f5SBrian Somers mpserver_Init(&mp->server);
2761fa665f5SBrian Somers
27749052c95SBrian Somers mp->cfg.mrru = 0;
27849052c95SBrian Somers mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
2797063995cSBrian Somers mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
28049052c95SBrian Somers mp->cfg.enddisc.class = 0;
28149052c95SBrian Somers *mp->cfg.enddisc.address = '\0';
28249052c95SBrian Somers mp->cfg.enddisc.len = 0;
28349052c95SBrian Somers
2843b0f8d2eSBrian Somers lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2853b0f8d2eSBrian Somers ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
2865d9e6103SBrian Somers
2875d9e6103SBrian Somers link_EmptyStack(&mp->link);
2885d9e6103SBrian Somers link_Stack(&mp->link, &protolayer);
2895d9e6103SBrian Somers link_Stack(&mp->link, &ccplayer);
2905d9e6103SBrian Somers link_Stack(&mp->link, &vjlayer);
29167b072f7SBrian Somers #ifndef NONAT
29267b072f7SBrian Somers link_Stack(&mp->link, &natlayer);
2935d9e6103SBrian Somers #endif
29449052c95SBrian Somers }
29549052c95SBrian Somers
29649052c95SBrian Somers int
mp_Up(struct mp * mp,struct datalink * dl)2971fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
29849052c95SBrian Somers {
2991fa665f5SBrian Somers struct lcp *lcp = &dl->physical->link.lcp;
3001fa665f5SBrian Somers
30149052c95SBrian Somers if (mp->active) {
30249052c95SBrian Somers /* We're adding a link - do a last validation on our parameters */
3031fa665f5SBrian Somers if (!peerid_Equal(&dl->peer, &mp->peer)) {
304dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
3057ebff009SBrian Somers log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname,
3067ebff009SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
3077ebff009SBrian Somers mp->peer.enddisc.len));
3087ebff009SBrian Somers log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname,
3097ebff009SBrian Somers mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
3107ebff009SBrian Somers dl->peer.enddisc.len));
3111fa665f5SBrian Somers return MP_FAILED;
312643f4904SBrian Somers }
3131fa665f5SBrian Somers if (mp->local_mrru != lcp->want_mrru ||
3141fa665f5SBrian Somers mp->peer_mrru != lcp->his_mrru ||
3151fa665f5SBrian Somers mp->local_is12bit != lcp->want_shortseq ||
3161fa665f5SBrian Somers mp->peer_is12bit != lcp->his_shortseq) {
317dd7e2610SBrian Somers log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
3181fa665f5SBrian Somers dl->name);
3191fa665f5SBrian Somers return MP_FAILED;
32049052c95SBrian Somers }
3211fa665f5SBrian Somers return MP_ADDED;
32249052c95SBrian Somers } else {
32349052c95SBrian Somers /* First link in multilink mode */
32449052c95SBrian Somers
3251fa665f5SBrian Somers mp->local_mrru = lcp->want_mrru;
3261fa665f5SBrian Somers mp->peer_mrru = lcp->his_mrru;
3271fa665f5SBrian Somers mp->local_is12bit = lcp->want_shortseq;
3281fa665f5SBrian Somers mp->peer_is12bit = lcp->his_shortseq;
3291fa665f5SBrian Somers mp->peer = dl->peer;
33049052c95SBrian Somers
33111572abfSBrian Somers throughput_destroy(&mp->link.stats.total);
33211572abfSBrian Somers throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
33311572abfSBrian Somers throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
334673903ecSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
335673903ecSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
336673903ecSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
337673903ecSBrian Somers
33811572abfSBrian Somers /* Tell the link who it belongs to */
33911572abfSBrian Somers dl->physical->link.stats.parent = &mp->link.stats.total;
34011572abfSBrian Somers
3419c53a7b1SBrian Somers mp->out.seq = 0;
3429c53a7b1SBrian Somers mp->out.link = 0;
34330949fd4SBrian Somers mp->out.af = AF_INET;
344673903ecSBrian Somers mp->seq.min_in = 0;
345673903ecSBrian Somers mp->seq.next_in = 0;
346673903ecSBrian Somers
3471fa665f5SBrian Somers /*
3481fa665f5SBrian Somers * Now we create our server socket.
3491fa665f5SBrian Somers * If it already exists, join it. Otherwise, create and own it
3501fa665f5SBrian Somers */
35196c9bb21SBrian Somers switch (mpserver_Open(&mp->server, &mp->peer)) {
35296c9bb21SBrian Somers case MPSERVER_CONNECTED:
353dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Transfer link on %s\n",
3546f384573SBrian Somers mp->server.socket.sun_path);
35596c9bb21SBrian Somers mp->server.send.dl = dl; /* Defer 'till it's safe to send */
3561fa665f5SBrian Somers return MP_LINKSENT;
35796c9bb21SBrian Somers case MPSERVER_FAILED:
3581fa665f5SBrian Somers return MP_FAILED;
35996c9bb21SBrian Somers case MPSERVER_LISTENING:
360dd7e2610SBrian Somers log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
361dd7e2610SBrian Somers log_Printf(LogPHASE, " First link: %s\n", dl->name);
3621fa665f5SBrian Somers
36330949fd4SBrian Somers /* Re-point our NCP layers at our MP link */
36430949fd4SBrian Somers ncp_SetLink(&mp->bundle->ncp, &mp->link);
3653b0f8d2eSBrian Somers
3663b0f8d2eSBrian Somers /* Our lcp's already up 'cos of the NULL parent */
36706337856SBrian Somers if (ccp_SetOpenMode(&mp->link.ccp)) {
368dd7e2610SBrian Somers fsm_Up(&mp->link.ccp.fsm);
369dd7e2610SBrian Somers fsm_Open(&mp->link.ccp.fsm);
37006337856SBrian Somers }
3713b0f8d2eSBrian Somers
3723b0f8d2eSBrian Somers mp->active = 1;
37396c9bb21SBrian Somers break;
37449052c95SBrian Somers }
3751fa665f5SBrian Somers }
3763b0f8d2eSBrian Somers
3771fa665f5SBrian Somers return MP_UP;
3783b0f8d2eSBrian Somers }
3793b0f8d2eSBrian Somers
3803b0f8d2eSBrian Somers void
mp_Down(struct mp * mp)381673903ecSBrian Somers mp_Down(struct mp *mp)
382673903ecSBrian Somers {
383673903ecSBrian Somers if (mp->active) {
384673903ecSBrian Somers struct mbuf *next;
385673903ecSBrian Somers
386ab2de065SBrian Somers /* Stop that ! */
387ab2de065SBrian Somers mp_StopAutoloadTimer(mp);
388ab2de065SBrian Somers
3891fa665f5SBrian Somers /* Don't want any more of these */
3901fa665f5SBrian Somers mpserver_Close(&mp->server);
3911fa665f5SBrian Somers
3921fa665f5SBrian Somers /* CCP goes down with a bang */
39309206a6fSBrian Somers fsm2initial(&mp->link.ccp.fsm);
394673903ecSBrian Somers
395673903ecSBrian Somers /* Received fragments go in the bit-bucket */
396673903ecSBrian Somers while (mp->inbufs) {
39726af0ae9SBrian Somers next = mp->inbufs->m_nextpkt;
39826af0ae9SBrian Somers m_freem(mp->inbufs);
399673903ecSBrian Somers mp->inbufs = next;
400673903ecSBrian Somers }
401673903ecSBrian Somers
402643f4904SBrian Somers peerid_Init(&mp->peer);
403673903ecSBrian Somers mp->active = 0;
404673903ecSBrian Somers }
405673903ecSBrian Somers }
406673903ecSBrian Somers
407673903ecSBrian Somers void
mp_linkInit(struct mp_link * mplink)4083b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
4093b0f8d2eSBrian Somers {
4103b0f8d2eSBrian Somers mplink->seq = 0;
411ab2de065SBrian Somers mplink->bandwidth = 0;
4123b0f8d2eSBrian Somers }
4133b0f8d2eSBrian Somers
4145d9e6103SBrian Somers static void
mp_Assemble(struct mp * mp,struct mbuf * m,struct physical * p)4155d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
4163b0f8d2eSBrian Somers {
4173b0f8d2eSBrian Somers struct mp_header mh, h;
4183b0f8d2eSBrian Somers struct mbuf *q, *last;
419057f1760SBrian Somers u_int32_t seq;
4203b0f8d2eSBrian Somers
42186b1f0d7SBrian Somers /*
42286b1f0d7SBrian Somers * When `m' and `p' are NULL, it means our oldest link has gone down.
42386b1f0d7SBrian Somers * We want to determine a new min, and process any intermediate stuff
42486b1f0d7SBrian Somers * as normal
42586b1f0d7SBrian Somers */
42686b1f0d7SBrian Somers
42786b1f0d7SBrian Somers if (m && mp_ReadHeader(mp, m, &mh) == 0) {
42826af0ae9SBrian Somers m_freem(m);
4293b0f8d2eSBrian Somers return;
4303b0f8d2eSBrian Somers }
4313b0f8d2eSBrian Somers
43286b1f0d7SBrian Somers if (p) {
4333b0f8d2eSBrian Somers seq = p->dl->mp.seq;
4343b0f8d2eSBrian Somers p->dl->mp.seq = mh.seq;
43586b1f0d7SBrian Somers } else
43686b1f0d7SBrian Somers seq = mp->seq.min_in;
43786b1f0d7SBrian Somers
4383b0f8d2eSBrian Somers if (mp->seq.min_in == seq) {
4393b0f8d2eSBrian Somers /*
4403b0f8d2eSBrian Somers * We've received new data on the link that has our min (oldest) seq.
4413b0f8d2eSBrian Somers * Figure out which link now has the smallest (oldest) seq.
4423b0f8d2eSBrian Somers */
4433b0f8d2eSBrian Somers struct datalink *dl;
4443b0f8d2eSBrian Somers
44586b1f0d7SBrian Somers mp->seq.min_in = (u_int32_t)-1;
4463b0f8d2eSBrian Somers for (dl = mp->bundle->links; dl; dl = dl->next)
44786b1f0d7SBrian Somers if (dl->state == DATALINK_OPEN &&
448057f1760SBrian Somers (mp->seq.min_in == (u_int32_t)-1 ||
44986b1f0d7SBrian Somers isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
4503b0f8d2eSBrian Somers mp->seq.min_in = dl->mp.seq;
4513b0f8d2eSBrian Somers }
4523b0f8d2eSBrian Somers
4533b0f8d2eSBrian Somers /*
4543b0f8d2eSBrian Somers * Now process as many of our fragments as we can, adding our new
4553b0f8d2eSBrian Somers * fragment in as we go, and ordering with the oldest at the top of
4563b0f8d2eSBrian Somers * the queue.
4573b0f8d2eSBrian Somers */
4583b0f8d2eSBrian Somers
4593b0f8d2eSBrian Somers last = NULL;
4603b0f8d2eSBrian Somers seq = mp->seq.next_in;
4613b0f8d2eSBrian Somers q = mp->inbufs;
462cb8bd8ddSBrian Somers while (q || m) {
463cb8bd8ddSBrian Somers if (!q) {
464cb8bd8ddSBrian Somers if (last)
465cb8bd8ddSBrian Somers last->m_nextpkt = m;
466cb8bd8ddSBrian Somers else
467cb8bd8ddSBrian Somers mp->inbufs = m;
468cb8bd8ddSBrian Somers q = m;
469cb8bd8ddSBrian Somers m = NULL;
470cb8bd8ddSBrian Somers h = mh;
471cb8bd8ddSBrian Somers } else {
4723b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h);
473cb8bd8ddSBrian Somers
4741bcced10SBrian Somers if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
4753b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */
4763b0f8d2eSBrian Somers if (last)
47726af0ae9SBrian Somers last->m_nextpkt = m;
4783b0f8d2eSBrian Somers else
4793b0f8d2eSBrian Somers mp->inbufs = m;
48026af0ae9SBrian Somers m->m_nextpkt = q;
4813b0f8d2eSBrian Somers q = m;
4823b0f8d2eSBrian Somers h = mh;
4833b0f8d2eSBrian Somers m = NULL;
4843b0f8d2eSBrian Somers }
485cb8bd8ddSBrian Somers }
4863b0f8d2eSBrian Somers
4873b0f8d2eSBrian Somers if (h.seq != seq) {
4883b0f8d2eSBrian Somers /* we're missing something :-( */
48964e0f466SBrian Somers if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
4903b0f8d2eSBrian Somers /* we're never gonna get it */
4913b0f8d2eSBrian Somers struct mbuf *next;
4923b0f8d2eSBrian Somers
4933b0f8d2eSBrian Somers /* Zap all older fragments */
4943b0f8d2eSBrian Somers while (mp->inbufs != q) {
495dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag\n");
49626af0ae9SBrian Somers next = mp->inbufs->m_nextpkt;
49726af0ae9SBrian Somers m_freem(mp->inbufs);
4983b0f8d2eSBrian Somers mp->inbufs = next;
4993b0f8d2eSBrian Somers }
5003b0f8d2eSBrian Somers
5013b0f8d2eSBrian Somers /*
5023b0f8d2eSBrian Somers * Zap everything until the next `end' fragment OR just before
5033b0f8d2eSBrian Somers * the next `begin' fragment OR 'till seq.min_in - whichever
5043b0f8d2eSBrian Somers * comes first.
5053b0f8d2eSBrian Somers */
5063b0f8d2eSBrian Somers do {
5073b0f8d2eSBrian Somers mp_ReadHeader(mp, mp->inbufs, &h);
5083b0f8d2eSBrian Somers if (h.begin) {
509ce828a6eSBrian Somers /* We might be able to process this ! */
5103b0f8d2eSBrian Somers h.seq--; /* We're gonna look for fragment with h.seq+1 */
5113b0f8d2eSBrian Somers break;
5123b0f8d2eSBrian Somers }
51326af0ae9SBrian Somers next = mp->inbufs->m_nextpkt;
514dd7e2610SBrian Somers log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
51526af0ae9SBrian Somers m_freem(mp->inbufs);
5163b0f8d2eSBrian Somers mp->inbufs = next;
51764e0f466SBrian Somers } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
51864e0f466SBrian Somers h.seq) || h.end));
5193b0f8d2eSBrian Somers
5203b0f8d2eSBrian Somers /*
5213b0f8d2eSBrian Somers * Continue processing things from here.
5223b0f8d2eSBrian Somers * This deals with the possibility that we received a fragment
5233b0f8d2eSBrian Somers * on the slowest link that invalidates some of our data (because
5243b0f8d2eSBrian Somers * of the hole at `q'), but where there are subsequent `whole'
5253b0f8d2eSBrian Somers * packets that have already been received.
5263b0f8d2eSBrian Somers */
5273b0f8d2eSBrian Somers
528147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5293b0f8d2eSBrian Somers last = NULL;
5303b0f8d2eSBrian Somers q = mp->inbufs;
5313b0f8d2eSBrian Somers } else
5323b0f8d2eSBrian Somers /* we may still receive the missing fragment */
5333b0f8d2eSBrian Somers break;
5343b0f8d2eSBrian Somers } else if (h.end) {
5353b0f8d2eSBrian Somers /* We've got something, reassemble */
5363b0f8d2eSBrian Somers struct mbuf **frag = &q;
5373b0f8d2eSBrian Somers int len;
538057f1760SBrian Somers long long first = -1;
5393b0f8d2eSBrian Somers
5403b0f8d2eSBrian Somers do {
5413b0f8d2eSBrian Somers *frag = mp->inbufs;
54226af0ae9SBrian Somers mp->inbufs = mp->inbufs->m_nextpkt;
5433b0f8d2eSBrian Somers len = mp_ReadHeader(mp, *frag, &h);
544ce828a6eSBrian Somers if (first == -1)
545ce828a6eSBrian Somers first = h.seq;
5463b0f8d2eSBrian Somers if (frag == &q && !h.begin) {
547dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
5483b0f8d2eSBrian Somers (u_long)h.seq);
54926af0ae9SBrian Somers m_freem(q);
5503b0f8d2eSBrian Somers q = NULL;
5513b0f8d2eSBrian Somers } else if (frag != &q && h.begin) {
552dd7e2610SBrian Somers log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
5533b0f8d2eSBrian Somers (u_long)h.seq - 1);
5543b0f8d2eSBrian Somers /*
5553b0f8d2eSBrian Somers * Stuff our fragment back at the front of the queue and zap
556299920e5SBrian Somers * our half-assembled packet.
5573b0f8d2eSBrian Somers */
55826af0ae9SBrian Somers (*frag)->m_nextpkt = mp->inbufs;
5593b0f8d2eSBrian Somers mp->inbufs = *frag;
5603b0f8d2eSBrian Somers *frag = NULL;
56126af0ae9SBrian Somers m_freem(q);
5623b0f8d2eSBrian Somers q = NULL;
5633b0f8d2eSBrian Somers frag = &q;
5643b0f8d2eSBrian Somers h.end = 0; /* just in case it's a whole packet */
565299920e5SBrian Somers } else {
566299920e5SBrian Somers (*frag)->m_offset += len;
567299920e5SBrian Somers (*frag)->m_len -= len;
568299920e5SBrian Somers (*frag)->m_nextpkt = NULL;
5693b0f8d2eSBrian Somers do
57026af0ae9SBrian Somers frag = &(*frag)->m_next;
571ce828a6eSBrian Somers while (*frag != NULL);
572299920e5SBrian Somers }
5733b0f8d2eSBrian Somers } while (!h.end);
5743b0f8d2eSBrian Somers
5753b0f8d2eSBrian Somers if (q) {
57626af0ae9SBrian Somers q = m_pullup(q);
5771814213eSMarcel Moolenaar log_Printf(LogDEBUG, "MP: Reassembled frags %lu-%lu, length %zd\n",
578057f1760SBrian Somers (u_long)first, (u_long)h.seq, m_length(q));
57926af0ae9SBrian Somers link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
58026af0ae9SBrian Somers m_freem(q);
5813b0f8d2eSBrian Somers }
5823b0f8d2eSBrian Somers
583147613eaSBrian Somers mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5843b0f8d2eSBrian Somers last = NULL;
5853b0f8d2eSBrian Somers q = mp->inbufs;
5863b0f8d2eSBrian Somers } else {
5873b0f8d2eSBrian Somers /* Look for the next fragment */
588147613eaSBrian Somers seq = inc_seq(mp->local_is12bit, seq);
5893b0f8d2eSBrian Somers last = q;
59026af0ae9SBrian Somers q = q->m_nextpkt;
5913b0f8d2eSBrian Somers }
5923b0f8d2eSBrian Somers }
5933b0f8d2eSBrian Somers
5943b0f8d2eSBrian Somers if (m) {
5953b0f8d2eSBrian Somers /* We still have to find a home for our new fragment */
5963b0f8d2eSBrian Somers last = NULL;
59726af0ae9SBrian Somers for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
5983b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h);
5991bcced10SBrian Somers if (isbefore(mp->local_is12bit, mh.seq, h.seq))
600147613eaSBrian Somers break;
601147613eaSBrian Somers }
602147613eaSBrian Somers /* Our received fragment fits in here */
6033b0f8d2eSBrian Somers if (last)
60426af0ae9SBrian Somers last->m_nextpkt = m;
6053b0f8d2eSBrian Somers else
6063b0f8d2eSBrian Somers mp->inbufs = m;
60726af0ae9SBrian Somers m->m_nextpkt = q;
6083b0f8d2eSBrian Somers }
6093b0f8d2eSBrian Somers }
6103b0f8d2eSBrian Somers
6115d9e6103SBrian Somers struct mbuf *
mp_Input(struct bundle * bundle,struct link * l,struct mbuf * bp)6125d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
6135d9e6103SBrian Somers {
6145d9e6103SBrian Somers struct physical *p = link2physical(l);
6155d9e6103SBrian Somers
6165d9e6103SBrian Somers if (!bundle->ncp.mp.active)
6175d9e6103SBrian Somers /* Let someone else deal with it ! */
6185d9e6103SBrian Somers return bp;
6195d9e6103SBrian Somers
6205d9e6103SBrian Somers if (p == NULL) {
6215d9e6103SBrian Somers log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
62226af0ae9SBrian Somers m_freem(bp);
623411675baSBrian Somers } else {
62426af0ae9SBrian Somers m_settype(bp, MB_MPIN);
6255d9e6103SBrian Somers mp_Assemble(&bundle->ncp.mp, bp, p);
626411675baSBrian Somers }
6275d9e6103SBrian Somers
6285d9e6103SBrian Somers return NULL;
6295d9e6103SBrian Somers }
6305d9e6103SBrian Somers
6313b0f8d2eSBrian Somers static void
mp_Output(struct mp * mp,struct bundle * bundle,struct link * l,struct mbuf * m,u_int32_t begin,u_int32_t end)6325d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
6335d9e6103SBrian Somers struct mbuf *m, u_int32_t begin, u_int32_t end)
6343b0f8d2eSBrian Somers {
63526af0ae9SBrian Somers char prepend[4];
6363b0f8d2eSBrian Somers
637ce828a6eSBrian Somers /* Stuff an MP header on the front of our packet and send it */
63826af0ae9SBrian Somers
63949052c95SBrian Somers if (mp->peer_is12bit) {
6409e8ec64bSBrian Somers u_int16_t val;
641ce828a6eSBrian Somers
6429e8ec64bSBrian Somers val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
64326af0ae9SBrian Somers ua_htons(&val, prepend);
64426af0ae9SBrian Somers m = m_prepend(m, prepend, 2, 0);
6453b0f8d2eSBrian Somers } else {
6469e8ec64bSBrian Somers u_int32_t val;
647ce828a6eSBrian Somers
6489e8ec64bSBrian Somers val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
64926af0ae9SBrian Somers ua_htonl(&val, prepend);
65026af0ae9SBrian Somers m = m_prepend(m, prepend, 4, 0);
6513b0f8d2eSBrian Somers }
652dd7e2610SBrian Somers if (log_IsKept(LogDEBUG))
6531814213eSMarcel Moolenaar log_Printf(LogDEBUG, "MP[frag %d]: Send %zd bytes on link `%s'\n",
65426af0ae9SBrian Somers mp->out.seq, m_length(m), l->name);
655147613eaSBrian Somers mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
6563b0f8d2eSBrian Somers
6576301d506SBrian Somers if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
6586301d506SBrian Somers log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
6596301d506SBrian Somers return;
6606301d506SBrian Somers }
6616301d506SBrian Somers
66226af0ae9SBrian Somers link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
6633b0f8d2eSBrian Somers }
6643b0f8d2eSBrian Somers
6653b0f8d2eSBrian Somers int
mp_FillPhysicalQueues(struct bundle * bundle)66630949fd4SBrian Somers mp_FillPhysicalQueues(struct bundle *bundle)
6673b0f8d2eSBrian Somers {
6683b0f8d2eSBrian Somers struct mp *mp = &bundle->ncp.mp;
6699c53a7b1SBrian Somers struct datalink *dl, *fdl;
67026af0ae9SBrian Somers size_t total, add, len;
671a43e859dSBrian Somers int thislink, nlinks, nopenlinks, sendasip;
672b4b280abSBrian Somers u_int32_t begin, end;
6733b0f8d2eSBrian Somers struct mbuf *m, *mo;
67430949fd4SBrian Somers struct link *bestlink;
6753b0f8d2eSBrian Somers
676a43e859dSBrian Somers thislink = nlinks = nopenlinks = 0;
6779c53a7b1SBrian Somers for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
67886b1f0d7SBrian Somers /* Include non-open links here as mp->out.link will stay more correct */
6799c53a7b1SBrian Somers if (!fdl) {
6809c53a7b1SBrian Somers if (thislink == mp->out.link)
6819c53a7b1SBrian Somers fdl = dl;
6829c53a7b1SBrian Somers else
6839c53a7b1SBrian Somers thislink++;
6849c53a7b1SBrian Somers }
6859c53a7b1SBrian Somers nlinks++;
686a43e859dSBrian Somers if (dl->state == DATALINK_OPEN)
687a43e859dSBrian Somers nopenlinks++;
6889c53a7b1SBrian Somers }
6899c53a7b1SBrian Somers
6909c53a7b1SBrian Somers if (!fdl) {
6919c53a7b1SBrian Somers fdl = bundle->links;
6921bc9b5baSBrian Somers if (!fdl)
6931bc9b5baSBrian Somers return 0;
6949c53a7b1SBrian Somers thislink = 0;
6959c53a7b1SBrian Somers }
6963b0f8d2eSBrian Somers
6973b0f8d2eSBrian Somers total = 0;
6989c53a7b1SBrian Somers for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6999c53a7b1SBrian Somers if (!dl) {
7009c53a7b1SBrian Somers dl = bundle->links;
7019c53a7b1SBrian Somers thislink = 0;
7029c53a7b1SBrian Somers }
7039c53a7b1SBrian Somers
7046f384573SBrian Somers if (dl->state != DATALINK_OPEN)
7056f384573SBrian Somers continue;
7069c53a7b1SBrian Somers
7073b0f8d2eSBrian Somers if (dl->physical->out)
7083b0f8d2eSBrian Somers /* this link has suffered a short write. Let it continue */
7093b0f8d2eSBrian Somers continue;
7109c53a7b1SBrian Somers
7113b0f8d2eSBrian Somers add = link_QueueLen(&dl->physical->link);
7123fd1e17cSBrian Somers if (add) {
7133b0f8d2eSBrian Somers /* this link has got stuff already queued. Let it continue */
7143fd1e17cSBrian Somers total += add;
7153b0f8d2eSBrian Somers continue;
7163fd1e17cSBrian Somers }
7179c53a7b1SBrian Somers
71830949fd4SBrian Somers if (!mp_QueueLen(mp)) {
7193fd1e17cSBrian Somers int mrutoosmall;
7203fd1e17cSBrian Somers
7213fd1e17cSBrian Somers /*
7223fd1e17cSBrian Somers * If there's only a single open link in our bundle and we haven't got
7233fd1e17cSBrian Somers * MP level link compression, queue outbound traffic directly via that
7243fd1e17cSBrian Somers * link's protocol stack rather than using the MP link. This results
72530949fd4SBrian Somers * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather
72630949fd4SBrian Somers * than PROTO_MP.
7273fd1e17cSBrian Somers */
7283fd1e17cSBrian Somers
7293fd1e17cSBrian Somers mrutoosmall = 0;
730a43e859dSBrian Somers sendasip = nopenlinks < 2;
731a43e859dSBrian Somers if (sendasip) {
7323fd1e17cSBrian Somers if (dl->physical->link.lcp.his_mru < mp->peer_mrru) {
7333fd1e17cSBrian Somers /*
7343fd1e17cSBrian Somers * Actually, forget it. This test is done against the MRRU rather
7353fd1e17cSBrian Somers * than the packet size so that we don't end up sending some data
7363fd1e17cSBrian Somers * in MP fragments and some data in PROTO_IP packets. That's just
7373fd1e17cSBrian Somers * too likely to upset some ppp implementations.
7383fd1e17cSBrian Somers */
7393fd1e17cSBrian Somers mrutoosmall = 1;
740a43e859dSBrian Somers sendasip = 0;
7413fd1e17cSBrian Somers }
7423fd1e17cSBrian Somers }
7433fd1e17cSBrian Somers
744a43e859dSBrian Somers bestlink = sendasip ? &dl->physical->link : &mp->link;
74530949fd4SBrian Somers if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink))
74630949fd4SBrian Somers break; /* Nothing else to send */
7473b0f8d2eSBrian Somers
7483fd1e17cSBrian Somers if (mrutoosmall)
7493fd1e17cSBrian Somers log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n");
750a43e859dSBrian Somers else if (sendasip)
7513fd1e17cSBrian Somers log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n");
7523fd1e17cSBrian Somers
753a43e859dSBrian Somers if (sendasip) {
7543fd1e17cSBrian Somers add = link_QueueLen(&dl->physical->link);
7553fd1e17cSBrian Somers if (add) {
7563fd1e17cSBrian Somers /* this link has got stuff already queued. Let it continue */
7573fd1e17cSBrian Somers total += add;
7583fd1e17cSBrian Somers continue;
7593fd1e17cSBrian Somers }
7603fd1e17cSBrian Somers }
7613fd1e17cSBrian Somers }
7623fd1e17cSBrian Somers
7633b0f8d2eSBrian Somers m = link_Dequeue(&mp->link);
7643fd1e17cSBrian Somers if (m) {
76526af0ae9SBrian Somers len = m_length(m);
7663b0f8d2eSBrian Somers begin = 1;
7673b0f8d2eSBrian Somers end = 0;
7683b0f8d2eSBrian Somers
7699c53a7b1SBrian Somers while (!end) {
7709c53a7b1SBrian Somers if (dl->state == DATALINK_OPEN) {
771ab2de065SBrian Somers /* Write at most his_mru bytes to the physical link */
772ab2de065SBrian Somers if (len <= dl->physical->link.lcp.his_mru) {
7733b0f8d2eSBrian Somers mo = m;
7743b0f8d2eSBrian Somers end = 1;
77526af0ae9SBrian Somers m_settype(mo, MB_MPOUT);
7763b0f8d2eSBrian Somers } else {
777ab2de065SBrian Somers /* It's > his_mru, chop the packet (`m') into bits */
77826af0ae9SBrian Somers mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
77926af0ae9SBrian Somers len -= mo->m_len;
78026af0ae9SBrian Somers m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
7813b0f8d2eSBrian Somers }
7825d9e6103SBrian Somers mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
7833b0f8d2eSBrian Somers begin = 0;
7843b0f8d2eSBrian Somers }
7859c53a7b1SBrian Somers
7869c53a7b1SBrian Somers if (!end) {
7879c53a7b1SBrian Somers nlinks--;
7889c53a7b1SBrian Somers dl = dl->next;
7899c53a7b1SBrian Somers if (!dl) {
7909c53a7b1SBrian Somers dl = bundle->links;
7919c53a7b1SBrian Somers thislink = 0;
7929c53a7b1SBrian Somers } else
7939c53a7b1SBrian Somers thislink++;
7943b0f8d2eSBrian Somers }
7959c53a7b1SBrian Somers }
7969c53a7b1SBrian Somers }
7973fd1e17cSBrian Somers }
7989c53a7b1SBrian Somers mp->out.link = thislink; /* Start here next time */
7993b0f8d2eSBrian Somers
8003b0f8d2eSBrian Somers return total;
8013b0f8d2eSBrian Somers }
8023b0f8d2eSBrian Somers
8033b0f8d2eSBrian Somers int
mp_SetDatalinkBandwidth(struct cmdargs const * arg)804ab2de065SBrian Somers mp_SetDatalinkBandwidth(struct cmdargs const *arg)
8053b0f8d2eSBrian Somers {
8063b0f8d2eSBrian Somers int val;
8073b0f8d2eSBrian Somers
80825092092SBrian Somers if (arg->argc != arg->argn+1)
8093b0f8d2eSBrian Somers return -1;
8103b0f8d2eSBrian Somers
81125092092SBrian Somers val = atoi(arg->argv[arg->argn]);
812ab2de065SBrian Somers if (val <= 0) {
813ab2de065SBrian Somers log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
8143b0f8d2eSBrian Somers return 1;
8153b0f8d2eSBrian Somers }
816ab2de065SBrian Somers arg->cx->mp.bandwidth = val;
817ab2de065SBrian Somers
818ab2de065SBrian Somers if (arg->cx->state == DATALINK_OPEN)
819ab2de065SBrian Somers bundle_CalculateBandwidth(arg->bundle);
820ab2de065SBrian Somers
8213b0f8d2eSBrian Somers return 0;
8223b0f8d2eSBrian Somers }
82349052c95SBrian Somers
82449052c95SBrian Somers int
mp_ShowStatus(struct cmdargs const * arg)82549052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
82649052c95SBrian Somers {
82749052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp;
82849052c95SBrian Somers
82949052c95SBrian Somers prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
8309c53a7b1SBrian Somers if (mp->active) {
831411675baSBrian Somers struct mbuf *m, *lm;
8329c53a7b1SBrian Somers int bufs = 0;
8339c53a7b1SBrian Somers
834fa0d5216SBrian Somers lm = NULL;
8351fa665f5SBrian Somers prompt_Printf(arg->prompt, "Socket: %s\n",
8366f384573SBrian Somers mp->server.socket.sun_path);
83726af0ae9SBrian Somers for (m = mp->inbufs; m; m = m->m_nextpkt) {
8389c53a7b1SBrian Somers bufs++;
839411675baSBrian Somers lm = m;
840411675baSBrian Somers }
841411675baSBrian Somers prompt_Printf(arg->prompt, "Pending frags: %d", bufs);
842411675baSBrian Somers if (bufs) {
843411675baSBrian Somers struct mp_header mh;
844411675baSBrian Somers unsigned long first, last;
845411675baSBrian Somers
846411675baSBrian Somers first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
847411675baSBrian Somers last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
84864e0f466SBrian Somers prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
849411675baSBrian Somers first, last, (unsigned long)mp->seq.next_in,
850411675baSBrian Somers (unsigned long)mp->seq.min_in);
85164e0f466SBrian Somers prompt_Printf(arg->prompt, " First has %sbegin bit and "
85264e0f466SBrian Somers "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
853411675baSBrian Somers }
854411675baSBrian Somers prompt_Printf(arg->prompt, "\n");
8559c53a7b1SBrian Somers }
85649052c95SBrian Somers
85749052c95SBrian Somers prompt_Printf(arg->prompt, "\nMy Side:\n");
85849052c95SBrian Somers if (mp->active) {
859ab2de065SBrian Somers prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq);
86049052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru);
86149052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n",
86249052c95SBrian Somers mp->local_is12bit ? "on" : "off");
86349052c95SBrian Somers }
864643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n",
86549052c95SBrian Somers mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
86649052c95SBrian Somers mp->cfg.enddisc.len));
86749052c95SBrian Somers
86849052c95SBrian Somers prompt_Printf(arg->prompt, "\nHis Side:\n");
86949052c95SBrian Somers if (mp->active) {
870643f4904SBrian Somers prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname);
871ab2de065SBrian Somers prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in);
87249052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru);
87349052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n",
87449052c95SBrian Somers mp->peer_is12bit ? "on" : "off");
87549052c95SBrian Somers }
876643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n",
877643f4904SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
878643f4904SBrian Somers mp->peer.enddisc.len));
87949052c95SBrian Somers
88049052c95SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n");
88149052c95SBrian Somers
88249052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: ");
88349052c95SBrian Somers if (mp->cfg.mrru)
88449052c95SBrian Somers prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
88549052c95SBrian Somers else
88649052c95SBrian Somers prompt_Printf(arg->prompt, "disabled\n");
88749052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n",
88849052c95SBrian Somers command_ShowNegval(mp->cfg.shortseq));
8897063995cSBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n",
8907063995cSBrian Somers command_ShowNegval(mp->cfg.negenddisc));
891ab2de065SBrian Somers prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%,"
892ab2de065SBrian Somers " period %d secs\n", mp->cfg.autoload.min,
893ab2de065SBrian Somers mp->cfg.autoload.max, mp->cfg.autoload.period);
89449052c95SBrian Somers
89549052c95SBrian Somers return 0;
89649052c95SBrian Somers }
89749052c95SBrian Somers
89849052c95SBrian Somers const char *
mp_Enddisc(u_char c,const char * address,size_t len)899057f1760SBrian Somers mp_Enddisc(u_char c, const char *address, size_t len)
90049052c95SBrian Somers {
901d93d3a9cSBrian Somers static char result[100]; /* Used immediately after it's returned */
902057f1760SBrian Somers unsigned f, header;
90349052c95SBrian Somers
90449052c95SBrian Somers switch (c) {
90508676022SBrian Somers case ENDDISC_NULL:
90649052c95SBrian Somers sprintf(result, "Null Class");
90749052c95SBrian Somers break;
90849052c95SBrian Somers
90908676022SBrian Somers case ENDDISC_LOCAL:
9101814213eSMarcel Moolenaar snprintf(result, sizeof result, "Local Addr: %.*s", (int)len,
9111814213eSMarcel Moolenaar address);
91249052c95SBrian Somers break;
91349052c95SBrian Somers
91408676022SBrian Somers case ENDDISC_IP:
91549052c95SBrian Somers if (len == 4)
91649052c95SBrian Somers snprintf(result, sizeof result, "IP %s",
91749052c95SBrian Somers inet_ntoa(*(const struct in_addr *)address));
91849052c95SBrian Somers else
9191814213eSMarcel Moolenaar sprintf(result, "IP[%zd] ???", len);
92049052c95SBrian Somers break;
92149052c95SBrian Somers
92208676022SBrian Somers case ENDDISC_MAC:
92349052c95SBrian Somers if (len == 6) {
92449052c95SBrian Somers const u_char *m = (const u_char *)address;
92549052c95SBrian Somers snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
92649052c95SBrian Somers m[0], m[1], m[2], m[3], m[4], m[5]);
92749052c95SBrian Somers } else
9281814213eSMarcel Moolenaar sprintf(result, "MAC[%zd] ???", len);
92949052c95SBrian Somers break;
93049052c95SBrian Somers
93108676022SBrian Somers case ENDDISC_MAGIC:
93249052c95SBrian Somers sprintf(result, "Magic: 0x");
93349052c95SBrian Somers header = strlen(result);
934057f1760SBrian Somers if (len + header + 1 > sizeof result)
93549052c95SBrian Somers len = sizeof result - header - 1;
93649052c95SBrian Somers for (f = 0; f < len; f++)
93749052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]);
93849052c95SBrian Somers break;
93949052c95SBrian Somers
94008676022SBrian Somers case ENDDISC_PSN:
9411814213eSMarcel Moolenaar snprintf(result, sizeof result, "PSN: %.*s", (int)len, address);
94249052c95SBrian Somers break;
94349052c95SBrian Somers
94449052c95SBrian Somers default:
94549052c95SBrian Somers sprintf(result, "%d: ", (int)c);
94649052c95SBrian Somers header = strlen(result);
947057f1760SBrian Somers if (len + header + 1 > sizeof result)
94849052c95SBrian Somers len = sizeof result - header - 1;
94949052c95SBrian Somers for (f = 0; f < len; f++)
95049052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]);
95149052c95SBrian Somers break;
95249052c95SBrian Somers }
95349052c95SBrian Somers return result;
95449052c95SBrian Somers }
95549052c95SBrian Somers
95649052c95SBrian Somers int
mp_SetEnddisc(struct cmdargs const * arg)95749052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
95849052c95SBrian Somers {
95949052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp;
960d47dceb8SBrian Somers struct in_addr addr;
96149052c95SBrian Somers
9629b5f8ffdSBrian Somers switch (bundle_Phase(arg->bundle)) {
9639b5f8ffdSBrian Somers case PHASE_DEAD:
9649b5f8ffdSBrian Somers break;
9659b5f8ffdSBrian Somers case PHASE_ESTABLISH:
9669b5f8ffdSBrian Somers /* Make sure none of our links are DATALINK_LCP or greater */
9679b5f8ffdSBrian Somers if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
9683df5ecacSUlrich Spörlein log_Printf(LogWARN, "enddisc: Only changeable before"
9699b5f8ffdSBrian Somers " LCP negotiations\n");
9709b5f8ffdSBrian Somers return 1;
9719b5f8ffdSBrian Somers }
9729b5f8ffdSBrian Somers break;
9739b5f8ffdSBrian Somers default:
9743df5ecacSUlrich Spörlein log_Printf(LogWARN, "enddisc: Only changeable at phase DEAD/ESTABLISH\n");
97549052c95SBrian Somers return 1;
97649052c95SBrian Somers }
97749052c95SBrian Somers
97849052c95SBrian Somers if (arg->argc == arg->argn) {
97949052c95SBrian Somers mp->cfg.enddisc.class = 0;
98049052c95SBrian Somers *mp->cfg.enddisc.address = '\0';
98149052c95SBrian Somers mp->cfg.enddisc.len = 0;
982e43ebac1SBrian Somers } else if (arg->argc > arg->argn) {
98308676022SBrian Somers if (!strcasecmp(arg->argv[arg->argn], "label")) {
98408676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_LOCAL;
98508676022SBrian Somers strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
98608676022SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
98708676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
988259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
98930949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
990d47dceb8SBrian Somers else
991259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip;
992d47dceb8SBrian Somers memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
99308676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_IP;
99449052c95SBrian Somers mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
99508676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
99608676022SBrian Somers struct sockaddr_dl hwaddr;
99708676022SBrian Somers
998259a6191SBrian Somers if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
99930949fd4SBrian Somers ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
1000d47dceb8SBrian Somers else
1001259a6191SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip;
1002d47dceb8SBrian Somers
1003057f1760SBrian Somers if (arp_EtherAddr(addr, &hwaddr, 1)) {
100408676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAC;
100508676022SBrian Somers memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
100608676022SBrian Somers hwaddr.sdl_alen);
100708676022SBrian Somers mp->cfg.enddisc.len = hwaddr.sdl_alen;
100808676022SBrian Somers } else {
1009dd7e2610SBrian Somers log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
1010259a6191SBrian Somers inet_ntoa(addr));
101108676022SBrian Somers return 4;
101208676022SBrian Somers }
101349052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
101449052c95SBrian Somers int f;
101549052c95SBrian Somers
101649052c95SBrian Somers randinit();
101749052c95SBrian Somers for (f = 0; f < 20; f += sizeof(long))
101849052c95SBrian Somers *(long *)(mp->cfg.enddisc.address + f) = random();
101908676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAGIC;
102049052c95SBrian Somers mp->cfg.enddisc.len = 20;
102149052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
102249052c95SBrian Somers if (arg->argc > arg->argn+1) {
102308676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_PSN;
102449052c95SBrian Somers strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
102549052c95SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
102649052c95SBrian Somers } else {
1027dd7e2610SBrian Somers log_Printf(LogWARN, "PSN endpoint requires additional data\n");
102808676022SBrian Somers return 5;
102949052c95SBrian Somers }
103049052c95SBrian Somers } else {
1031dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
103249052c95SBrian Somers arg->argv[arg->argn]);
103308676022SBrian Somers return 6;
103449052c95SBrian Somers }
1035e43ebac1SBrian Somers }
103649052c95SBrian Somers
103749052c95SBrian Somers return 0;
103849052c95SBrian Somers }
10391fa665f5SBrian Somers
10401fa665f5SBrian Somers static int
mpserver_UpdateSet(struct fdescriptor * d,fd_set * r,fd_set * w,fd_set * e,int * n)1041f013f33eSBrian Somers mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
10421fa665f5SBrian Somers int *n)
10431fa665f5SBrian Somers {
10441fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d);
10450f2f3eb3SBrian Somers int result;
10461fa665f5SBrian Somers
10470f2f3eb3SBrian Somers result = 0;
10486f384573SBrian Somers if (s->send.dl != NULL) {
104996c9bb21SBrian Somers /* We've connect()ed */
1050b7c5748eSBrian Somers if (!link_QueueLen(&s->send.dl->physical->link) &&
1051b7c5748eSBrian Somers !s->send.dl->physical->out) {
1052b7c5748eSBrian Somers /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
10530f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
105496c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
10556f384573SBrian Somers s->send.dl = NULL;
105696c9bb21SBrian Somers s->fd = -1;
1057ea722969SBrian Somers } else
1058ea722969SBrian Somers /* Never read from a datalink that's on death row ! */
10590f2f3eb3SBrian Somers result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
106096c9bb21SBrian Somers } else if (r && s->fd >= 0) {
10611fa665f5SBrian Somers if (*n < s->fd + 1)
10621fa665f5SBrian Somers *n = s->fd + 1;
10631fa665f5SBrian Somers FD_SET(s->fd, r);
106424989c68SBrian Somers log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
10650f2f3eb3SBrian Somers result++;
10661fa665f5SBrian Somers }
10670f2f3eb3SBrian Somers return result;
10681fa665f5SBrian Somers }
10691fa665f5SBrian Somers
10701fa665f5SBrian Somers static int
mpserver_IsSet(struct fdescriptor * d,const fd_set * fdset)1071f013f33eSBrian Somers mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
10721fa665f5SBrian Somers {
10731fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d);
10741fa665f5SBrian Somers return s->fd >= 0 && FD_ISSET(s->fd, fdset);
10751fa665f5SBrian Somers }
10761fa665f5SBrian Somers
10771fa665f5SBrian Somers static void
mpserver_Read(struct fdescriptor * d,struct bundle * bundle,const fd_set * fdset __unused)1078057f1760SBrian Somers mpserver_Read(struct fdescriptor *d, struct bundle *bundle,
1079057f1760SBrian Somers const fd_set *fdset __unused)
10801fa665f5SBrian Somers {
10811fa665f5SBrian Somers struct mpserver *s = descriptor2mpserver(d);
10821fa665f5SBrian Somers
10832cb305afSBrian Somers bundle_ReceiveDatalink(bundle, s->fd);
10841fa665f5SBrian Somers }
10851fa665f5SBrian Somers
10861af29a6eSBrian Somers static int
mpserver_Write(struct fdescriptor * d __unused,struct bundle * bundle __unused,const fd_set * fdset __unused)1087057f1760SBrian Somers mpserver_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
1088057f1760SBrian Somers const fd_set *fdset __unused)
10891fa665f5SBrian Somers {
10901fa665f5SBrian Somers /* We never want to write here ! */
1091a33b2ef7SBrian Somers log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
10921af29a6eSBrian Somers return 0;
10931fa665f5SBrian Somers }
10941fa665f5SBrian Somers
10951fa665f5SBrian Somers void
mpserver_Init(struct mpserver * s)10961fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
10971fa665f5SBrian Somers {
10981fa665f5SBrian Somers s->desc.type = MPSERVER_DESCRIPTOR;
10991fa665f5SBrian Somers s->desc.UpdateSet = mpserver_UpdateSet;
11001fa665f5SBrian Somers s->desc.IsSet = mpserver_IsSet;
11011fa665f5SBrian Somers s->desc.Read = mpserver_Read;
11021fa665f5SBrian Somers s->desc.Write = mpserver_Write;
11036f384573SBrian Somers s->send.dl = NULL;
11041fa665f5SBrian Somers s->fd = -1;
11056f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket);
11061fa665f5SBrian Somers }
11071fa665f5SBrian Somers
11081fa665f5SBrian Somers int
mpserver_Open(struct mpserver * s,struct peerid * peer)11091fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
11101fa665f5SBrian Somers {
1111cbee9754SBrian Somers int f, l;
11121fa665f5SBrian Somers mode_t mask;
11131fa665f5SBrian Somers
11141fa665f5SBrian Somers if (s->fd != -1) {
1115a33b2ef7SBrian Somers log_Printf(LogALERT, "Internal error ! mpserver already open\n");
11166f384573SBrian Somers mpserver_Close(s);
11171fa665f5SBrian Somers }
11181fa665f5SBrian Somers
11196f384573SBrian Somers l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
11206f384573SBrian Somers _PATH_VARRUN, peer->authname, peer->enddisc.class);
11211433aa5dSBrian Somers if (l < 0) {
11221433aa5dSBrian Somers log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno));
11231433aa5dSBrian Somers return MPSERVER_FAILED;
11241433aa5dSBrian Somers }
11251fa665f5SBrian Somers
1126057f1760SBrian Somers for (f = 0;
1127057f1760SBrian Somers f < peer->enddisc.len && (size_t)l < sizeof s->socket.sun_path - 2;
1128057f1760SBrian Somers f++) {
11296f384573SBrian Somers snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
11301fa665f5SBrian Somers "%02x", *(u_char *)(peer->enddisc.address+f));
11316f384573SBrian Somers l += 2;
11321fa665f5SBrian Somers }
11331fa665f5SBrian Somers
11346f384573SBrian Somers s->socket.sun_family = AF_LOCAL;
11356f384573SBrian Somers s->socket.sun_len = sizeof s->socket;
113663c6cac9SBrian Somers s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
11371fa665f5SBrian Somers if (s->fd < 0) {
11382cb305afSBrian Somers log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
113996c9bb21SBrian Somers return MPSERVER_FAILED;
11401fa665f5SBrian Somers }
11411fa665f5SBrian Somers
11426f384573SBrian Somers setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
11436f384573SBrian Somers sizeof s->socket);
11441fa665f5SBrian Somers mask = umask(0177);
11452cb305afSBrian Somers
11462cb305afSBrian Somers /*
1147cbee9754SBrian Somers * Try to bind the socket. If we succeed we play server, if we fail
1148cbee9754SBrian Somers * we connect() and hand the link off.
11492cb305afSBrian Somers */
11502cb305afSBrian Somers
11516f384573SBrian Somers if (ID0bind_un(s->fd, &s->socket) < 0) {
11526f384573SBrian Somers if (errno != EADDRINUSE) {
1153dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
11546f384573SBrian Somers s->socket.sun_path, strerror(errno));
11551fa665f5SBrian Somers umask(mask);
11561fa665f5SBrian Somers close(s->fd);
11571fa665f5SBrian Somers s->fd = -1;
115896c9bb21SBrian Somers return MPSERVER_FAILED;
11596f384573SBrian Somers }
11602cb305afSBrian Somers
1161cbee9754SBrian Somers /* So we're the sender */
11626f384573SBrian Somers umask(mask);
11636f384573SBrian Somers if (ID0connect_un(s->fd, &s->socket) < 0) {
1164dd7e2610SBrian Somers log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
11656f384573SBrian Somers s->socket.sun_path, strerror(errno));
11666f384573SBrian Somers if (errno == ECONNREFUSED)
11672cb305afSBrian Somers log_Printf(LogPHASE, " The previous server died badly !\n");
11686f384573SBrian Somers close(s->fd);
11696f384573SBrian Somers s->fd = -1;
117096c9bb21SBrian Somers return MPSERVER_FAILED;
11716f384573SBrian Somers }
11726f384573SBrian Somers
11736f384573SBrian Somers /* Donate our link to the other guy */
117496c9bb21SBrian Somers return MPSERVER_CONNECTED;
11751fa665f5SBrian Somers }
11766f384573SBrian Somers
117796c9bb21SBrian Somers return MPSERVER_LISTENING;
11781fa665f5SBrian Somers }
11791fa665f5SBrian Somers
11801fa665f5SBrian Somers void
mpserver_Close(struct mpserver * s)11811fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
11821fa665f5SBrian Somers {
11836f384573SBrian Somers if (s->send.dl != NULL) {
118496c9bb21SBrian Somers bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
11856f384573SBrian Somers s->send.dl = NULL;
118696c9bb21SBrian Somers s->fd = -1;
118796c9bb21SBrian Somers } else if (s->fd >= 0) {
11881fa665f5SBrian Somers close(s->fd);
11896f384573SBrian Somers if (ID0unlink(s->socket.sun_path) == -1)
1190dd7e2610SBrian Somers log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
11916f384573SBrian Somers strerror(errno));
11926f384573SBrian Somers memset(&s->socket, '\0', sizeof s->socket);
11931fa665f5SBrian Somers s->fd = -1;
11941fa665f5SBrian Somers }
11951fa665f5SBrian Somers }
119686b1f0d7SBrian Somers
119786b1f0d7SBrian Somers void
mp_LinkLost(struct mp * mp,struct datalink * dl)119886b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
119986b1f0d7SBrian Somers {
120086b1f0d7SBrian Somers if (mp->seq.min_in == dl->mp.seq)
120186b1f0d7SBrian Somers /* We've lost the link that's holding everything up ! */
12025d9e6103SBrian Somers mp_Assemble(mp, NULL, NULL);
120386b1f0d7SBrian Somers }
12046f8e9f0aSBrian Somers
120530949fd4SBrian Somers size_t
mp_QueueLen(struct mp * mp)120630949fd4SBrian Somers mp_QueueLen(struct mp *mp)
12076f8e9f0aSBrian Somers {
120830949fd4SBrian Somers return link_QueueLen(&mp->link);
12096f8e9f0aSBrian Somers }
1210