xref: /freebsd/usr.sbin/ppp/mp.c (revision 5d9e6103663c28d3986c674a772142f29e6584be)
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  *
265d9e6103SBrian Somers  *	$Id: mp.c,v 1.18 1999/01/28 01:56:33 brian Exp $
273b0f8d2eSBrian Somers  */
283b0f8d2eSBrian Somers 
29972a1bcfSBrian Somers #include <sys/param.h>
303b0f8d2eSBrian Somers #include <netinet/in.h>
313b0f8d2eSBrian Somers #include <netinet/in_systm.h>
323b0f8d2eSBrian Somers #include <netinet/ip.h>
3349052c95SBrian Somers #include <arpa/inet.h>
3408676022SBrian Somers #include <net/if_dl.h>
3508676022SBrian Somers #include <sys/socket.h>
361fa665f5SBrian Somers #include <sys/un.h>
373b0f8d2eSBrian Somers 
3808676022SBrian Somers #include <errno.h>
391fa665f5SBrian Somers #include <paths.h>
403b0f8d2eSBrian Somers #include <stdlib.h>
4149052c95SBrian Somers #include <stdio.h>
423b0f8d2eSBrian Somers #include <string.h>
431fa665f5SBrian Somers #include <sys/stat.h>
443b0f8d2eSBrian Somers #include <termios.h>
4508676022SBrian Somers #include <unistd.h>
463b0f8d2eSBrian Somers 
475d9e6103SBrian Somers #include "layer.h"
485d9e6103SBrian Somers #ifndef NOALIAS
495d9e6103SBrian Somers #include "alias_cmd.h"
505d9e6103SBrian Somers #endif
515d9e6103SBrian Somers #include "vjcomp.h"
529e8ec64bSBrian Somers #include "ua.h"
53c9e11a11SBrian Somers #include "defs.h"
543b0f8d2eSBrian Somers #include "command.h"
553b0f8d2eSBrian Somers #include "mbuf.h"
563b0f8d2eSBrian Somers #include "log.h"
573b0f8d2eSBrian Somers #include "timer.h"
583b0f8d2eSBrian Somers #include "fsm.h"
593b0f8d2eSBrian Somers #include "iplist.h"
603b0f8d2eSBrian Somers #include "throughput.h"
613b0f8d2eSBrian Somers #include "slcompress.h"
625a72b6edSBrian Somers #include "lqr.h"
635a72b6edSBrian Somers #include "hdlc.h"
643b0f8d2eSBrian Somers #include "ipcp.h"
653b0f8d2eSBrian Somers #include "auth.h"
663b0f8d2eSBrian Somers #include "lcp.h"
673b0f8d2eSBrian Somers #include "async.h"
683b0f8d2eSBrian Somers #include "ccp.h"
693b0f8d2eSBrian Somers #include "link.h"
703b0f8d2eSBrian Somers #include "descriptor.h"
713b0f8d2eSBrian Somers #include "physical.h"
723b0f8d2eSBrian Somers #include "chat.h"
735d9e6103SBrian Somers #include "proto.h"
743b0f8d2eSBrian Somers #include "filter.h"
753b0f8d2eSBrian Somers #include "mp.h"
763b0f8d2eSBrian Somers #include "chap.h"
7792b09558SBrian Somers #include "cbcp.h"
783b0f8d2eSBrian Somers #include "datalink.h"
79972a1bcfSBrian Somers #ifndef NORADIUS
80972a1bcfSBrian Somers #include "radius.h"
81972a1bcfSBrian Somers #endif
823b0f8d2eSBrian Somers #include "bundle.h"
833b0f8d2eSBrian Somers #include "ip.h"
8449052c95SBrian Somers #include "prompt.h"
8508676022SBrian Somers #include "id.h"
8608676022SBrian Somers #include "arp.h"
873b0f8d2eSBrian Somers 
88643f4904SBrian Somers void
89643f4904SBrian Somers peerid_Init(struct peerid *peer)
90643f4904SBrian Somers {
91643f4904SBrian Somers   peer->enddisc.class = 0;
92643f4904SBrian Somers   *peer->enddisc.address = '\0';
93643f4904SBrian Somers   peer->enddisc.len = 0;
94643f4904SBrian Somers   *peer->authname = '\0';
95643f4904SBrian Somers }
96643f4904SBrian Somers 
97643f4904SBrian Somers int
98643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
99643f4904SBrian Somers {
100643f4904SBrian Somers   return !strcmp(p1->authname, p2->authname) &&
101643f4904SBrian Somers          p1->enddisc.class == p2->enddisc.class &&
102643f4904SBrian Somers          p1->enddisc.len == p2->enddisc.len &&
103643f4904SBrian Somers          !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
104643f4904SBrian Somers }
105643f4904SBrian Somers 
1063b0f8d2eSBrian Somers static u_int32_t
107147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
1083b0f8d2eSBrian Somers {
1093b0f8d2eSBrian Somers   seq++;
110147613eaSBrian Somers   if (is12bit) {
1113b0f8d2eSBrian Somers     if (seq & 0xfffff000)
1123b0f8d2eSBrian Somers       seq = 0;
1133b0f8d2eSBrian Somers   } else if (seq & 0xff000000)
1143b0f8d2eSBrian Somers     seq = 0;
1153b0f8d2eSBrian Somers   return seq;
1163b0f8d2eSBrian Somers }
1173b0f8d2eSBrian Somers 
1183b0f8d2eSBrian Somers static int
1191bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1201bcced10SBrian Somers {
121b4b280abSBrian Somers   u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
122b4b280abSBrian Somers 
123b4b280abSBrian Somers   if (seq1 > max) {
124b4b280abSBrian Somers     if (seq2 < 0x200 || seq2 > seq1)
125b4b280abSBrian Somers       return 1;
126b4b280abSBrian Somers   } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
127b4b280abSBrian Somers     return 1;
128b4b280abSBrian Somers 
129b4b280abSBrian Somers   return 0;
1301bcced10SBrian Somers }
1311bcced10SBrian Somers 
1321bcced10SBrian Somers static int
1333b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1343b0f8d2eSBrian Somers {
13549052c95SBrian Somers   if (mp->local_is12bit) {
1369e8ec64bSBrian Somers     u_int16_t val;
1379e8ec64bSBrian Somers 
1389e8ec64bSBrian Somers     ua_ntohs(MBUF_CTOP(m), &val);
1399e8ec64bSBrian Somers     if (val & 0x3000) {
140dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1413b0f8d2eSBrian Somers       return 0;
1423b0f8d2eSBrian Somers     }
1439e8ec64bSBrian Somers     header->begin = val & 0x8000 ? 1 : 0;
1449e8ec64bSBrian Somers     header->end = val & 0x4000 ? 1 : 0;
1459e8ec64bSBrian Somers     header->seq = val & 0x0fff;
1463b0f8d2eSBrian Somers     return 2;
1473b0f8d2eSBrian Somers   } else {
1489e8ec64bSBrian Somers     ua_ntohl(MBUF_CTOP(m), &header->seq);
1493b0f8d2eSBrian Somers     if (header->seq & 0x3f000000) {
150dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1513b0f8d2eSBrian Somers       return 0;
1523b0f8d2eSBrian Somers     }
153ce828a6eSBrian Somers     header->begin = header->seq & 0x80000000 ? 1 : 0;
154ce828a6eSBrian Somers     header->end = header->seq & 0x40000000 ? 1 : 0;
1553b0f8d2eSBrian Somers     header->seq &= 0x00ffffff;
1563b0f8d2eSBrian Somers     return 4;
1573b0f8d2eSBrian Somers   }
1583b0f8d2eSBrian Somers }
1593b0f8d2eSBrian Somers 
1603b0f8d2eSBrian Somers static void
1613b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp)
1623b0f8d2eSBrian Somers {
16349052c95SBrian Somers   /* The given FSM (ccp) is about to start up ! */
1643b0f8d2eSBrian Somers }
1653b0f8d2eSBrian Somers 
1663b0f8d2eSBrian Somers static void
1673b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp)
1683b0f8d2eSBrian Somers {
16949052c95SBrian Somers   /* The given fsm (ccp) is now up */
1703b0f8d2eSBrian Somers }
1713b0f8d2eSBrian Somers 
1723b0f8d2eSBrian Somers static void
1733b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp)
1743b0f8d2eSBrian Somers {
17549052c95SBrian Somers   /* The given FSM (ccp) has been told to come down */
1763b0f8d2eSBrian Somers }
1773b0f8d2eSBrian Somers 
1783b0f8d2eSBrian Somers static void
1793b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp)
1803b0f8d2eSBrian Somers {
18149052c95SBrian Somers   /* The given fsm (ccp) is now down */
1820a1b5c9dSBrian Somers   if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1830a1b5c9dSBrian Somers     fsm_Open(fp);		/* CCP goes to ST_STOPPED */
1843b0f8d2eSBrian Somers }
1853b0f8d2eSBrian Somers 
1863b0f8d2eSBrian Somers void
1873b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
1883b0f8d2eSBrian Somers {
18949052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
19049052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
191643f4904SBrian Somers 
192643f4904SBrian Somers   peerid_Init(&mp->peer);
193643f4904SBrian Somers 
1949c53a7b1SBrian Somers   mp->out.seq = 0;
1959c53a7b1SBrian Somers   mp->out.link = 0;
1963b0f8d2eSBrian Somers   mp->seq.min_in = 0;
1973b0f8d2eSBrian Somers   mp->seq.next_in = 0;
1983b0f8d2eSBrian Somers   mp->inbufs = NULL;
1993b0f8d2eSBrian Somers   mp->bundle = bundle;
2003b0f8d2eSBrian Somers 
2015d9e6103SBrian Somers   mp->link.type = LOGICAL_LINK;
2023b0f8d2eSBrian Somers   mp->link.name = "mp";
2033b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
204643f4904SBrian Somers 
2053b0f8d2eSBrian Somers   throughput_init(&mp->link.throughput);
2063b0f8d2eSBrian Somers   memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
2073b0f8d2eSBrian Somers   memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
2083b0f8d2eSBrian Somers   memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
2093b0f8d2eSBrian Somers 
2103b0f8d2eSBrian Somers   mp->fsmp.LayerStart = mp_LayerStart;
2113b0f8d2eSBrian Somers   mp->fsmp.LayerUp = mp_LayerUp;
2123b0f8d2eSBrian Somers   mp->fsmp.LayerDown = mp_LayerDown;
2133b0f8d2eSBrian Somers   mp->fsmp.LayerFinish = mp_LayerFinish;
2143b0f8d2eSBrian Somers   mp->fsmp.object = mp;
2153b0f8d2eSBrian Somers 
2161fa665f5SBrian Somers   mpserver_Init(&mp->server);
2171fa665f5SBrian Somers 
21849052c95SBrian Somers   mp->cfg.mrru = 0;
21949052c95SBrian Somers   mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
22049052c95SBrian Somers   mp->cfg.enddisc.class = 0;
22149052c95SBrian Somers   *mp->cfg.enddisc.address = '\0';
22249052c95SBrian Somers   mp->cfg.enddisc.len = 0;
22349052c95SBrian Somers 
2243b0f8d2eSBrian Somers   lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2253b0f8d2eSBrian Somers   ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
2265d9e6103SBrian Somers 
2275d9e6103SBrian Somers   link_EmptyStack(&mp->link);
2285d9e6103SBrian Somers   link_Stack(&mp->link, &protolayer);
2295d9e6103SBrian Somers   link_Stack(&mp->link, &ccplayer);
2305d9e6103SBrian Somers   link_Stack(&mp->link, &vjlayer);
2315d9e6103SBrian Somers #ifndef NOALIAS
2325d9e6103SBrian Somers   link_Stack(&mp->link, &aliaslayer);
2335d9e6103SBrian Somers #endif
23449052c95SBrian Somers }
23549052c95SBrian Somers 
23649052c95SBrian Somers int
2371fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
23849052c95SBrian Somers {
2391fa665f5SBrian Somers   struct lcp *lcp = &dl->physical->link.lcp;
2401fa665f5SBrian Somers 
24149052c95SBrian Somers   if (mp->active) {
24249052c95SBrian Somers     /* We're adding a link - do a last validation on our parameters */
2431fa665f5SBrian Somers     if (!peerid_Equal(&dl->peer, &mp->peer)) {
244dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
2451fa665f5SBrian Somers       return MP_FAILED;
246643f4904SBrian Somers     }
2471fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
2481fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
2491fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
2501fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
251dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
2521fa665f5SBrian Somers                 dl->name);
2531fa665f5SBrian Somers       return MP_FAILED;
25449052c95SBrian Somers     }
2551fa665f5SBrian Somers     return MP_ADDED;
25649052c95SBrian Somers   } else {
25749052c95SBrian Somers     /* First link in multilink mode */
25849052c95SBrian Somers 
2591fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
2601fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
2611fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
2621fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
2631fa665f5SBrian Somers     mp->peer = dl->peer;
26449052c95SBrian Somers 
265673903ecSBrian Somers     throughput_init(&mp->link.throughput);
266673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
267673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
268673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
269673903ecSBrian Somers 
2709c53a7b1SBrian Somers     mp->out.seq = 0;
2719c53a7b1SBrian Somers     mp->out.link = 0;
272673903ecSBrian Somers     mp->seq.min_in = 0;
273673903ecSBrian Somers     mp->seq.next_in = 0;
274673903ecSBrian Somers 
2751fa665f5SBrian Somers     /*
2761fa665f5SBrian Somers      * Now we create our server socket.
2771fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
2781fa665f5SBrian Somers      */
27996c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
28096c9bb21SBrian Somers     case MPSERVER_CONNECTED:
281dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
2826f384573SBrian Somers                 mp->server.socket.sun_path);
28396c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
2841fa665f5SBrian Somers       return MP_LINKSENT;
28596c9bb21SBrian Somers     case MPSERVER_FAILED:
2861fa665f5SBrian Somers       return MP_FAILED;
28796c9bb21SBrian Somers     case MPSERVER_LISTENING:
288dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
289dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
2901fa665f5SBrian Somers 
29149052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
292ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
2933b0f8d2eSBrian Somers 
2943b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
29506337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
296dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
297dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
29806337856SBrian Somers       }
2993b0f8d2eSBrian Somers 
3003b0f8d2eSBrian Somers       mp->active = 1;
30196c9bb21SBrian Somers       break;
30249052c95SBrian Somers     }
3031fa665f5SBrian Somers   }
3043b0f8d2eSBrian Somers 
3051fa665f5SBrian Somers   return MP_UP;
3063b0f8d2eSBrian Somers }
3073b0f8d2eSBrian Somers 
3083b0f8d2eSBrian Somers void
309673903ecSBrian Somers mp_Down(struct mp *mp)
310673903ecSBrian Somers {
311673903ecSBrian Somers   if (mp->active) {
312673903ecSBrian Somers     struct mbuf *next;
313673903ecSBrian Somers 
3141fa665f5SBrian Somers     /* Don't want any more of these */
3151fa665f5SBrian Somers     mpserver_Close(&mp->server);
3161fa665f5SBrian Somers 
3171fa665f5SBrian Somers     /* CCP goes down with a bang */
31809206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
319673903ecSBrian Somers 
320673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
321673903ecSBrian Somers     while (mp->inbufs) {
322673903ecSBrian Somers       next = mp->inbufs->pnext;
323dd7e2610SBrian Somers       mbuf_Free(mp->inbufs);
324673903ecSBrian Somers       mp->inbufs = next;
325673903ecSBrian Somers     }
326673903ecSBrian Somers 
327643f4904SBrian Somers     peerid_Init(&mp->peer);
328673903ecSBrian Somers     mp->active = 0;
329673903ecSBrian Somers   }
330673903ecSBrian Somers }
331673903ecSBrian Somers 
332673903ecSBrian Somers void
3333b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
3343b0f8d2eSBrian Somers {
3353b0f8d2eSBrian Somers   mplink->seq = 0;
3363b0f8d2eSBrian Somers   mplink->weight = 1500;
3373b0f8d2eSBrian Somers }
3383b0f8d2eSBrian Somers 
3395d9e6103SBrian Somers static void
3405d9e6103SBrian Somers mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
3413b0f8d2eSBrian Somers {
3423b0f8d2eSBrian Somers   struct mp_header mh, h;
3433b0f8d2eSBrian Somers   struct mbuf *q, *last;
3443b0f8d2eSBrian Somers   int32_t seq;
3453b0f8d2eSBrian Somers 
34686b1f0d7SBrian Somers   /*
34786b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
34886b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
34986b1f0d7SBrian Somers    * as normal
35086b1f0d7SBrian Somers    */
35186b1f0d7SBrian Somers 
35286b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
353dd7e2610SBrian Somers     mbuf_Free(m);
3543b0f8d2eSBrian Somers     return;
3553b0f8d2eSBrian Somers   }
3563b0f8d2eSBrian Somers 
35786b1f0d7SBrian Somers   if (p) {
3583b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
3593b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
36086b1f0d7SBrian Somers   } else
36186b1f0d7SBrian Somers     seq = mp->seq.min_in;
36286b1f0d7SBrian Somers 
3633b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
3643b0f8d2eSBrian Somers     /*
3653b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
3663b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
3673b0f8d2eSBrian Somers      */
3683b0f8d2eSBrian Somers     struct datalink *dl;
3693b0f8d2eSBrian Somers 
37086b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
3713b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
37286b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
37386b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
37486b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
3753b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
3763b0f8d2eSBrian Somers   }
3773b0f8d2eSBrian Somers 
3783b0f8d2eSBrian Somers   /*
3793b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
3803b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
3813b0f8d2eSBrian Somers    * the queue.
3823b0f8d2eSBrian Somers    */
3833b0f8d2eSBrian Somers 
3843b0f8d2eSBrian Somers   if (!mp->inbufs) {
3853b0f8d2eSBrian Somers     mp->inbufs = m;
3863b0f8d2eSBrian Somers     m = NULL;
3873b0f8d2eSBrian Somers   }
3883b0f8d2eSBrian Somers 
3893b0f8d2eSBrian Somers   last = NULL;
3903b0f8d2eSBrian Somers   seq = mp->seq.next_in;
3913b0f8d2eSBrian Somers   q = mp->inbufs;
3923b0f8d2eSBrian Somers   while (q) {
3933b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
3941bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
3953b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
3963b0f8d2eSBrian Somers       if (last)
3973b0f8d2eSBrian Somers         last->pnext = m;
3983b0f8d2eSBrian Somers       else
3993b0f8d2eSBrian Somers         mp->inbufs = m;
4003b0f8d2eSBrian Somers       m->pnext = q;
4013b0f8d2eSBrian Somers       q = m;
4023b0f8d2eSBrian Somers       h = mh;
4033b0f8d2eSBrian Somers       m = NULL;
4043b0f8d2eSBrian Somers     }
4053b0f8d2eSBrian Somers 
4063b0f8d2eSBrian Somers     if (h.seq != seq) {
4073b0f8d2eSBrian Somers       /* we're missing something :-( */
4083b0f8d2eSBrian Somers       if (mp->seq.min_in > seq) {
4093b0f8d2eSBrian Somers         /* we're never gonna get it */
4103b0f8d2eSBrian Somers         struct mbuf *next;
4113b0f8d2eSBrian Somers 
4123b0f8d2eSBrian Somers         /* Zap all older fragments */
4133b0f8d2eSBrian Somers         while (mp->inbufs != q) {
414dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
4153b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
416dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4173b0f8d2eSBrian Somers           mp->inbufs = next;
4183b0f8d2eSBrian Somers         }
4193b0f8d2eSBrian Somers 
4203b0f8d2eSBrian Somers         /*
4213b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4223b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4233b0f8d2eSBrian Somers          * comes first.
4243b0f8d2eSBrian Somers          */
4253b0f8d2eSBrian Somers         do {
4263b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4273b0f8d2eSBrian Somers           if (h.begin) {
428ce828a6eSBrian Somers             /* We might be able to process this ! */
4293b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4303b0f8d2eSBrian Somers             break;
4313b0f8d2eSBrian Somers           }
4323b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
433dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
434dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4353b0f8d2eSBrian Somers           mp->inbufs = next;
436ce828a6eSBrian Somers         } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end));
4373b0f8d2eSBrian Somers 
4383b0f8d2eSBrian Somers         /*
4393b0f8d2eSBrian Somers          * Continue processing things from here.
4403b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
4413b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
4423b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
4433b0f8d2eSBrian Somers          * packets that have already been received.
4443b0f8d2eSBrian Somers          */
4453b0f8d2eSBrian Somers 
446147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
4473b0f8d2eSBrian Somers         last = NULL;
4483b0f8d2eSBrian Somers         q = mp->inbufs;
4493b0f8d2eSBrian Somers       } else
4503b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
4513b0f8d2eSBrian Somers         break;
4523b0f8d2eSBrian Somers     } else if (h.end) {
4533b0f8d2eSBrian Somers       /* We've got something, reassemble */
4543b0f8d2eSBrian Somers       struct mbuf **frag = &q;
4553b0f8d2eSBrian Somers       int len;
456ce828a6eSBrian Somers       u_long first = -1;
4573b0f8d2eSBrian Somers 
4583b0f8d2eSBrian Somers       do {
4593b0f8d2eSBrian Somers         *frag = mp->inbufs;
4603b0f8d2eSBrian Somers         mp->inbufs = mp->inbufs->pnext;
4613b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
462ce828a6eSBrian Somers         if (first == -1)
463ce828a6eSBrian Somers           first = h.seq;
4643b0f8d2eSBrian Somers         (*frag)->offset += len;
4653b0f8d2eSBrian Somers         (*frag)->cnt -= len;
4663b0f8d2eSBrian Somers         (*frag)->pnext = NULL;
4673b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
468dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
4693b0f8d2eSBrian Somers                     (u_long)h.seq);
470dd7e2610SBrian Somers           mbuf_Free(q);
4713b0f8d2eSBrian Somers           q = NULL;
4723b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
473dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
4743b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
4753b0f8d2eSBrian Somers           /*
4763b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
4773b0f8d2eSBrian Somers            * our half-assembed packet.
4783b0f8d2eSBrian Somers            */
4793b0f8d2eSBrian Somers           (*frag)->pnext = mp->inbufs;
4803b0f8d2eSBrian Somers           mp->inbufs = *frag;
4813b0f8d2eSBrian Somers           *frag = NULL;
482dd7e2610SBrian Somers           mbuf_Free(q);
4833b0f8d2eSBrian Somers           q = NULL;
4843b0f8d2eSBrian Somers           frag = &q;
4853b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
4863b0f8d2eSBrian Somers         } else
4873b0f8d2eSBrian Somers           do
4883b0f8d2eSBrian Somers             frag = &(*frag)->next;
489ce828a6eSBrian Somers           while (*frag != NULL);
4903b0f8d2eSBrian Somers       } while (!h.end);
4913b0f8d2eSBrian Somers 
4923b0f8d2eSBrian Somers       if (q) {
493673903ecSBrian Somers         u_short proto;
494673903ecSBrian Somers         u_char ch;
495673903ecSBrian Somers 
496dd7e2610SBrian Somers         q = mbuf_Read(q, &ch, 1);
497673903ecSBrian Somers         proto = ch;
498673903ecSBrian Somers         if (!(proto & 1)) {
499dd7e2610SBrian Somers           q = mbuf_Read(q, &ch, 1);
500673903ecSBrian Somers           proto <<= 8;
5013b0f8d2eSBrian Somers           proto += ch;
502673903ecSBrian Somers         }
503dd7e2610SBrian Somers         if (log_IsKept(LogDEBUG))
504dd7e2610SBrian Somers           log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
505dd7e2610SBrian Somers                     first, (u_long)h.seq, mbuf_Length(q));
5065d9e6103SBrian Somers         q = mbuf_Contiguous(q);
5075d9e6103SBrian Somers         link_PullPacket(&mp->link, MBUF_CTOP(q), q->cnt, mp->bundle);
5083b0f8d2eSBrian Somers       }
5093b0f8d2eSBrian Somers 
510147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
5113b0f8d2eSBrian Somers       last = NULL;
5123b0f8d2eSBrian Somers       q = mp->inbufs;
5133b0f8d2eSBrian Somers     } else {
5143b0f8d2eSBrian Somers       /* Look for the next fragment */
515147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5163b0f8d2eSBrian Somers       last = q;
5173b0f8d2eSBrian Somers       q = q->pnext;
5183b0f8d2eSBrian Somers     }
5193b0f8d2eSBrian Somers   }
5203b0f8d2eSBrian Somers 
5213b0f8d2eSBrian Somers   if (m) {
5223b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5233b0f8d2eSBrian Somers     last = NULL;
5243b0f8d2eSBrian Somers     for (q = mp->inbufs; q; last = q, q = q->pnext) {
5253b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5261bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
527147613eaSBrian Somers         break;
528147613eaSBrian Somers     }
529147613eaSBrian Somers     /* Our received fragment fits in here */
5303b0f8d2eSBrian Somers     if (last)
5313b0f8d2eSBrian Somers       last->pnext = m;
5323b0f8d2eSBrian Somers     else
5333b0f8d2eSBrian Somers       mp->inbufs = m;
5343b0f8d2eSBrian Somers     m->pnext = q;
5353b0f8d2eSBrian Somers   }
5363b0f8d2eSBrian Somers }
5373b0f8d2eSBrian Somers 
5385d9e6103SBrian Somers struct mbuf *
5395d9e6103SBrian Somers mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
5405d9e6103SBrian Somers {
5415d9e6103SBrian Somers   struct physical *p = link2physical(l);
5425d9e6103SBrian Somers 
5435d9e6103SBrian Somers   if (!bundle->ncp.mp.active)
5445d9e6103SBrian Somers     /* Let someone else deal with it ! */
5455d9e6103SBrian Somers     return bp;
5465d9e6103SBrian Somers 
5475d9e6103SBrian Somers   if (p == NULL) {
5485d9e6103SBrian Somers     log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
5495d9e6103SBrian Somers     mbuf_Free(bp);
5505d9e6103SBrian Somers   } else
5515d9e6103SBrian Somers     mp_Assemble(&bundle->ncp.mp, bp, p);
5525d9e6103SBrian Somers 
5535d9e6103SBrian Somers   return NULL;
5545d9e6103SBrian Somers }
5555d9e6103SBrian Somers 
5563b0f8d2eSBrian Somers static void
5575d9e6103SBrian Somers mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
5585d9e6103SBrian Somers           struct mbuf *m, u_int32_t begin, u_int32_t end)
5593b0f8d2eSBrian Somers {
5603b0f8d2eSBrian Somers   struct mbuf *mo;
5613b0f8d2eSBrian Somers 
562ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
563dd7e2610SBrian Somers   mo = mbuf_Alloc(4, MB_MP);
5643b0f8d2eSBrian Somers   mo->next = m;
56549052c95SBrian Somers   if (mp->peer_is12bit) {
5669e8ec64bSBrian Somers     u_int16_t val;
567ce828a6eSBrian Somers 
5689e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
5699e8ec64bSBrian Somers     ua_htons(&val, MBUF_CTOP(mo));
5703b0f8d2eSBrian Somers     mo->cnt = 2;
5713b0f8d2eSBrian Somers   } else {
5729e8ec64bSBrian Somers     u_int32_t val;
573ce828a6eSBrian Somers 
5749e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
5759e8ec64bSBrian Somers     ua_htonl(&val, MBUF_CTOP(mo));
5763b0f8d2eSBrian Somers     mo->cnt = 4;
5773b0f8d2eSBrian Somers   }
578dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
5799c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
5809c53a7b1SBrian Somers               mp->out.seq, mbuf_Length(mo), l->name);
581147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
5823b0f8d2eSBrian Somers 
5835d9e6103SBrian Somers   link_PushPacket(l, mo, bundle, PRI_NORMAL, PROTO_MP);
5843b0f8d2eSBrian Somers }
5853b0f8d2eSBrian Somers 
5863b0f8d2eSBrian Somers int
5873b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
5883b0f8d2eSBrian Somers {
5893b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
5909c53a7b1SBrian Somers   struct datalink *dl, *fdl;
591b4b280abSBrian Somers   int total, add, len, thislink, nlinks;
592b4b280abSBrian Somers   u_int32_t begin, end;
5933b0f8d2eSBrian Somers   struct mbuf *m, *mo;
5943b0f8d2eSBrian Somers 
5959c53a7b1SBrian Somers   thislink = nlinks = 0;
5969c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
59786b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
5989c53a7b1SBrian Somers     if (!fdl) {
5999c53a7b1SBrian Somers       if (thislink == mp->out.link)
6009c53a7b1SBrian Somers         fdl = dl;
6019c53a7b1SBrian Somers       else
6029c53a7b1SBrian Somers         thislink++;
6039c53a7b1SBrian Somers     }
6049c53a7b1SBrian Somers     nlinks++;
6059c53a7b1SBrian Somers   }
6069c53a7b1SBrian Somers 
6079c53a7b1SBrian Somers   if (!fdl) {
6089c53a7b1SBrian Somers     fdl = bundle->links;
6091bc9b5baSBrian Somers     if (!fdl)
6101bc9b5baSBrian Somers       return 0;
6119c53a7b1SBrian Somers     thislink = 0;
6129c53a7b1SBrian Somers   }
6133b0f8d2eSBrian Somers 
6143b0f8d2eSBrian Somers   total = 0;
6159c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
6169c53a7b1SBrian Somers     if (!dl) {
6179c53a7b1SBrian Somers       dl = bundle->links;
6189c53a7b1SBrian Somers       thislink = 0;
6199c53a7b1SBrian Somers     }
6209c53a7b1SBrian Somers 
6216f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
6226f384573SBrian Somers       continue;
6239c53a7b1SBrian Somers 
6243b0f8d2eSBrian Somers     if (dl->physical->out)
6253b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
6263b0f8d2eSBrian Somers       continue;
6279c53a7b1SBrian Somers 
6283b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
6293b0f8d2eSBrian Somers     total += add;
6303b0f8d2eSBrian Somers     if (add)
6313b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6323b0f8d2eSBrian Somers       continue;
6339c53a7b1SBrian Somers 
6345d9e6103SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
6353b0f8d2eSBrian Somers       /* Nothing else to send */
6363b0f8d2eSBrian Somers       break;
6373b0f8d2eSBrian Somers 
6383b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
639dd7e2610SBrian Somers     len = mbuf_Length(m);
6403b0f8d2eSBrian Somers     begin = 1;
6413b0f8d2eSBrian Somers     end = 0;
6423b0f8d2eSBrian Somers 
6439c53a7b1SBrian Somers     while (!end) {
6449c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
6453b0f8d2eSBrian Somers         if (len <= dl->mp.weight + LINK_MINWEIGHT) {
6469c53a7b1SBrian Somers           /*
6479c53a7b1SBrian Somers            * XXX: Should we remember how much of our `weight' wasn't sent
6489c53a7b1SBrian Somers            *      so that we can compensate next time ?
6499c53a7b1SBrian Somers            */
6503b0f8d2eSBrian Somers           mo = m;
6513b0f8d2eSBrian Somers           end = 1;
6523b0f8d2eSBrian Somers         } else {
653dd7e2610SBrian Somers           mo = mbuf_Alloc(dl->mp.weight, MB_MP);
6543b0f8d2eSBrian Somers           mo->cnt = dl->mp.weight;
6553b0f8d2eSBrian Somers           len -= mo->cnt;
656dd7e2610SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt);
6573b0f8d2eSBrian Somers         }
6585d9e6103SBrian Somers         mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
6593b0f8d2eSBrian Somers         begin = 0;
6603b0f8d2eSBrian Somers       }
6619c53a7b1SBrian Somers 
6629c53a7b1SBrian Somers       if (!end) {
6639c53a7b1SBrian Somers         nlinks--;
6649c53a7b1SBrian Somers         dl = dl->next;
6659c53a7b1SBrian Somers         if (!dl) {
6669c53a7b1SBrian Somers           dl = bundle->links;
6679c53a7b1SBrian Somers           thislink = 0;
6689c53a7b1SBrian Somers         } else
6699c53a7b1SBrian Somers           thislink++;
6703b0f8d2eSBrian Somers       }
6719c53a7b1SBrian Somers     }
6729c53a7b1SBrian Somers   }
6739c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
6743b0f8d2eSBrian Somers 
6753b0f8d2eSBrian Somers   return total;
6763b0f8d2eSBrian Somers }
6773b0f8d2eSBrian Somers 
6783b0f8d2eSBrian Somers int
6793b0f8d2eSBrian Somers mp_SetDatalinkWeight(struct cmdargs const *arg)
6803b0f8d2eSBrian Somers {
6813b0f8d2eSBrian Somers   int val;
6823b0f8d2eSBrian Somers 
68325092092SBrian Somers   if (arg->argc != arg->argn+1)
6843b0f8d2eSBrian Somers     return -1;
6853b0f8d2eSBrian Somers 
68625092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
6873b0f8d2eSBrian Somers   if (val < LINK_MINWEIGHT) {
688dd7e2610SBrian Somers     log_Printf(LogWARN, "Link weights must not be less than %d\n",
6893b0f8d2eSBrian Somers               LINK_MINWEIGHT);
6903b0f8d2eSBrian Somers     return 1;
6913b0f8d2eSBrian Somers   }
6923b0f8d2eSBrian Somers   arg->cx->mp.weight = val;
6933b0f8d2eSBrian Somers   return 0;
6943b0f8d2eSBrian Somers }
69549052c95SBrian Somers 
69649052c95SBrian Somers int
69749052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
69849052c95SBrian Somers {
69949052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
70049052c95SBrian Somers 
70149052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
7029c53a7b1SBrian Somers   if (mp->active) {
7039c53a7b1SBrian Somers     struct mbuf *m;
7049c53a7b1SBrian Somers     int bufs = 0;
7059c53a7b1SBrian Somers 
7061fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
7076f384573SBrian Somers                   mp->server.socket.sun_path);
7089c53a7b1SBrian Somers     for (m = mp->inbufs; m; m = m->pnext)
7099c53a7b1SBrian Somers       bufs++;
7109c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d\n", bufs);
7119c53a7b1SBrian Somers   }
71249052c95SBrian Somers 
71349052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
71449052c95SBrian Somers   if (mp->active) {
71549052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
71649052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
71749052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
71849052c95SBrian Somers   }
719643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
72049052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
72149052c95SBrian Somers                            mp->cfg.enddisc.len));
72249052c95SBrian Somers 
72349052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
72449052c95SBrian Somers   if (mp->active) {
725643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
7269c53a7b1SBrian Somers     prompt_Printf(arg->prompt, " Next SEQ:      %u\n", mp->out.seq);
72749052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
72849052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
72949052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
73049052c95SBrian Somers   }
731643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
732643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
733643f4904SBrian Somers                            mp->peer.enddisc.len));
73449052c95SBrian Somers 
73549052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
73649052c95SBrian Somers 
73749052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
73849052c95SBrian Somers   if (mp->cfg.mrru)
73949052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
74049052c95SBrian Somers   else
74149052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
74249052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
74349052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
74449052c95SBrian Somers 
74549052c95SBrian Somers   return 0;
74649052c95SBrian Somers }
74749052c95SBrian Somers 
74849052c95SBrian Somers const char *
74949052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
75049052c95SBrian Somers {
751d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
75249052c95SBrian Somers   int f, header;
75349052c95SBrian Somers 
75449052c95SBrian Somers   switch (c) {
75508676022SBrian Somers     case ENDDISC_NULL:
75649052c95SBrian Somers       sprintf(result, "Null Class");
75749052c95SBrian Somers       break;
75849052c95SBrian Somers 
75908676022SBrian Somers     case ENDDISC_LOCAL:
76049052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
76149052c95SBrian Somers       break;
76249052c95SBrian Somers 
76308676022SBrian Somers     case ENDDISC_IP:
76449052c95SBrian Somers       if (len == 4)
76549052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
76649052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
76749052c95SBrian Somers       else
76849052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
76949052c95SBrian Somers       break;
77049052c95SBrian Somers 
77108676022SBrian Somers     case ENDDISC_MAC:
77249052c95SBrian Somers       if (len == 6) {
77349052c95SBrian Somers         const u_char *m = (const u_char *)address;
77449052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
77549052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
77649052c95SBrian Somers       } else
77749052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
77849052c95SBrian Somers       break;
77949052c95SBrian Somers 
78008676022SBrian Somers     case ENDDISC_MAGIC:
78149052c95SBrian Somers       sprintf(result, "Magic: 0x");
78249052c95SBrian Somers       header = strlen(result);
78349052c95SBrian Somers       if (len > sizeof result - header - 1)
78449052c95SBrian Somers         len = sizeof result - header - 1;
78549052c95SBrian Somers       for (f = 0; f < len; f++)
78649052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
78749052c95SBrian Somers       break;
78849052c95SBrian Somers 
78908676022SBrian Somers     case ENDDISC_PSN:
79049052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
79149052c95SBrian Somers       break;
79249052c95SBrian Somers 
79349052c95SBrian Somers      default:
79449052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
79549052c95SBrian Somers       header = strlen(result);
79649052c95SBrian Somers       if (len > sizeof result - header - 1)
79749052c95SBrian Somers         len = sizeof result - header - 1;
79849052c95SBrian Somers       for (f = 0; f < len; f++)
79949052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
80049052c95SBrian Somers       break;
80149052c95SBrian Somers   }
80249052c95SBrian Somers   return result;
80349052c95SBrian Somers }
80449052c95SBrian Somers 
80549052c95SBrian Somers int
80649052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
80749052c95SBrian Somers {
80849052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
809d47dceb8SBrian Somers   struct in_addr addr;
81049052c95SBrian Somers 
8119b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
8129b5f8ffdSBrian Somers     case PHASE_DEAD:
8139b5f8ffdSBrian Somers       break;
8149b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
8159b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
8169b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
8179b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
8189b5f8ffdSBrian Somers                    " LCP negotiations\n");
8199b5f8ffdSBrian Somers         return 1;
8209b5f8ffdSBrian Somers       }
8219b5f8ffdSBrian Somers       break;
8229b5f8ffdSBrian Somers     default:
8239b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
82449052c95SBrian Somers       return 1;
82549052c95SBrian Somers   }
82649052c95SBrian Somers 
82749052c95SBrian Somers   if (arg->argc == arg->argn) {
82849052c95SBrian Somers     mp->cfg.enddisc.class = 0;
82949052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
83049052c95SBrian Somers     mp->cfg.enddisc.len = 0;
831e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
83208676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
83308676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
83408676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
83508676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
83608676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
837259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
838259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
839d47dceb8SBrian Somers       else
840259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
841d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
84208676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
84349052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
84408676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
84508676022SBrian Somers       struct sockaddr_dl hwaddr;
84608676022SBrian Somers       int s;
84708676022SBrian Somers 
848259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
849259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
850d47dceb8SBrian Somers       else
851259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
852d47dceb8SBrian Somers 
85308676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
85408676022SBrian Somers       if (s < 0) {
855dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
85608676022SBrian Somers         return 2;
85708676022SBrian Somers       }
858d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
85908676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
86008676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
86108676022SBrian Somers                hwaddr.sdl_alen);
86208676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
86308676022SBrian Somers       } else {
864dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
865259a6191SBrian Somers                   inet_ntoa(addr));
86608676022SBrian Somers         close(s);
86708676022SBrian Somers         return 4;
86808676022SBrian Somers       }
86908676022SBrian Somers       close(s);
87049052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
87149052c95SBrian Somers       int f;
87249052c95SBrian Somers 
87349052c95SBrian Somers       randinit();
87449052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
87549052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
87608676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
87749052c95SBrian Somers       mp->cfg.enddisc.len = 20;
87849052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
87949052c95SBrian Somers       if (arg->argc > arg->argn+1) {
88008676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
88149052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
88249052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
88349052c95SBrian Somers       } else {
884dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
88508676022SBrian Somers         return 5;
88649052c95SBrian Somers       }
88749052c95SBrian Somers     } else {
888dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
88949052c95SBrian Somers                 arg->argv[arg->argn]);
89008676022SBrian Somers       return 6;
89149052c95SBrian Somers     }
892e43ebac1SBrian Somers   }
89349052c95SBrian Somers 
89449052c95SBrian Somers   return 0;
89549052c95SBrian Somers }
8961fa665f5SBrian Somers 
8971fa665f5SBrian Somers static int
8981fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
8991fa665f5SBrian Somers                    int *n)
9001fa665f5SBrian Somers {
9011fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9020f2f3eb3SBrian Somers   int result;
9031fa665f5SBrian Somers 
9040f2f3eb3SBrian Somers   result = 0;
9056f384573SBrian Somers   if (s->send.dl != NULL) {
90696c9bb21SBrian Somers     /* We've connect()ed */
907b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
908b7c5748eSBrian Somers         !s->send.dl->physical->out) {
909b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
9100f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
91196c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
9126f384573SBrian Somers       s->send.dl = NULL;
91396c9bb21SBrian Somers       s->fd = -1;
914ea722969SBrian Somers     } else
915ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
9160f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
91796c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
9181fa665f5SBrian Somers     if (*n < s->fd + 1)
9191fa665f5SBrian Somers       *n = s->fd + 1;
9201fa665f5SBrian Somers     FD_SET(s->fd, r);
92124989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
9220f2f3eb3SBrian Somers     result++;
9231fa665f5SBrian Somers   }
9240f2f3eb3SBrian Somers   return result;
9251fa665f5SBrian Somers }
9261fa665f5SBrian Somers 
9271fa665f5SBrian Somers static int
9281fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset)
9291fa665f5SBrian Somers {
9301fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9311fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
9321fa665f5SBrian Somers }
9331fa665f5SBrian Somers 
9341fa665f5SBrian Somers static void
9351fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
9361fa665f5SBrian Somers {
9371fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9386f384573SBrian Somers   struct sockaddr in;
9391fa665f5SBrian Somers   int fd, size;
9401fa665f5SBrian Somers 
9416f384573SBrian Somers   size = sizeof in;
9426f384573SBrian Somers   fd = accept(s->fd, &in, &size);
9431fa665f5SBrian Somers   if (fd < 0) {
944dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno));
9451fa665f5SBrian Somers     return;
9461fa665f5SBrian Somers   }
9471fa665f5SBrian Somers 
9481bc9b5baSBrian Somers   if (in.sa_family == AF_LOCAL)
94996c9bb21SBrian Somers     bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in);
95054cd8e13SBrian Somers   else
9511fa665f5SBrian Somers     close(fd);
9521fa665f5SBrian Somers }
9531fa665f5SBrian Somers 
9541af29a6eSBrian Somers static int
9551fa665f5SBrian Somers mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
9561fa665f5SBrian Somers {
9571fa665f5SBrian Somers   /* We never want to write here ! */
958a33b2ef7SBrian Somers   log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
9591af29a6eSBrian Somers   return 0;
9601fa665f5SBrian Somers }
9611fa665f5SBrian Somers 
9621fa665f5SBrian Somers void
9631fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
9641fa665f5SBrian Somers {
9651fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
9661fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
9671fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
9681fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
9691fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
9706f384573SBrian Somers   s->send.dl = NULL;
9711fa665f5SBrian Somers   s->fd = -1;
9726f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
9731fa665f5SBrian Somers }
9741fa665f5SBrian Somers 
9751fa665f5SBrian Somers int
9761fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
9771fa665f5SBrian Somers {
97896c9bb21SBrian Somers   int f, l;
9791fa665f5SBrian Somers   mode_t mask;
9801fa665f5SBrian Somers 
9811fa665f5SBrian Somers   if (s->fd != -1) {
982a33b2ef7SBrian Somers     log_Printf(LogALERT, "Internal error !  mpserver already open\n");
9836f384573SBrian Somers     mpserver_Close(s);
9841fa665f5SBrian Somers   }
9851fa665f5SBrian Somers 
9866f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
9876f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
9881fa665f5SBrian Somers 
9896f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
9906f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
9911fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
9926f384573SBrian Somers     l += 2;
9931fa665f5SBrian Somers   }
9941fa665f5SBrian Somers 
9956f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
9966f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
9971fa665f5SBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
9981fa665f5SBrian Somers   if (s->fd < 0) {
999dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno));
100096c9bb21SBrian Somers     return MPSERVER_FAILED;
10011fa665f5SBrian Somers   }
10021fa665f5SBrian Somers 
10036f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
10046f384573SBrian Somers              sizeof s->socket);
10051fa665f5SBrian Somers   mask = umask(0177);
10066f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
10076f384573SBrian Somers     if (errno != EADDRINUSE) {
1008dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
10096f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10101fa665f5SBrian Somers       umask(mask);
10111fa665f5SBrian Somers       close(s->fd);
10121fa665f5SBrian Somers       s->fd = -1;
101396c9bb21SBrian Somers       return MPSERVER_FAILED;
10146f384573SBrian Somers     }
10156f384573SBrian Somers     umask(mask);
10166f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
1017dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
10186f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
10196f384573SBrian Somers       if (errno == ECONNREFUSED)
1020dd7e2610SBrian Somers         log_Printf(LogPHASE, "          Has the previous server died badly ?\n");
10216f384573SBrian Somers       close(s->fd);
10226f384573SBrian Somers       s->fd = -1;
102396c9bb21SBrian Somers       return MPSERVER_FAILED;
10246f384573SBrian Somers     }
10256f384573SBrian Somers 
10266f384573SBrian Somers     /* Donate our link to the other guy */
102796c9bb21SBrian Somers     return MPSERVER_CONNECTED;
10281fa665f5SBrian Somers   }
10296f384573SBrian Somers 
10306f384573SBrian Somers   /* Listen for other ppp invocations that want to donate links */
10311fa665f5SBrian Somers   if (listen(s->fd, 5) != 0) {
1032dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: Unable to listen to socket"
10331fa665f5SBrian Somers               " - BUNDLE overload?\n");
10341fa665f5SBrian Somers     mpserver_Close(s);
10351fa665f5SBrian Somers   }
10361fa665f5SBrian Somers 
103796c9bb21SBrian Somers   return MPSERVER_LISTENING;
10381fa665f5SBrian Somers }
10391fa665f5SBrian Somers 
10401fa665f5SBrian Somers void
10411fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
10421fa665f5SBrian Somers {
10436f384573SBrian Somers   if (s->send.dl != NULL) {
104496c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
10456f384573SBrian Somers     s->send.dl = NULL;
104696c9bb21SBrian Somers     s->fd = -1;
104796c9bb21SBrian Somers   } else if (s->fd >= 0) {
10481fa665f5SBrian Somers     close(s->fd);
10496f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1050dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
10516f384573SBrian Somers                 strerror(errno));
10526f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
10531fa665f5SBrian Somers     s->fd = -1;
10541fa665f5SBrian Somers   }
10551fa665f5SBrian Somers }
105686b1f0d7SBrian Somers 
105786b1f0d7SBrian Somers void
105886b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
105986b1f0d7SBrian Somers {
106086b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
106186b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
10625d9e6103SBrian Somers     mp_Assemble(mp, NULL, NULL);
106386b1f0d7SBrian Somers }
10646f8e9f0aSBrian Somers 
10656f8e9f0aSBrian Somers void
10666f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
10676f8e9f0aSBrian Somers {
10686f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
10696f8e9f0aSBrian Somers }
1070