xref: /freebsd/usr.sbin/ppp/mp.c (revision 972a1bcf5db5ee4c5520a1d29d3c81e81bdec84f)
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  *
26972a1bcfSBrian Somers  *	$Id: mp.c,v 1.17 1998/10/24 01:08:45 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 
479e8ec64bSBrian Somers #include "ua.h"
48c9e11a11SBrian Somers #include "defs.h"
493b0f8d2eSBrian Somers #include "command.h"
503b0f8d2eSBrian Somers #include "mbuf.h"
513b0f8d2eSBrian Somers #include "log.h"
523b0f8d2eSBrian Somers #include "timer.h"
533b0f8d2eSBrian Somers #include "fsm.h"
543b0f8d2eSBrian Somers #include "iplist.h"
553b0f8d2eSBrian Somers #include "throughput.h"
563b0f8d2eSBrian Somers #include "slcompress.h"
575a72b6edSBrian Somers #include "lqr.h"
585a72b6edSBrian Somers #include "hdlc.h"
593b0f8d2eSBrian Somers #include "ipcp.h"
603b0f8d2eSBrian Somers #include "auth.h"
613b0f8d2eSBrian Somers #include "lcp.h"
623b0f8d2eSBrian Somers #include "async.h"
633b0f8d2eSBrian Somers #include "ccp.h"
643b0f8d2eSBrian Somers #include "link.h"
653b0f8d2eSBrian Somers #include "descriptor.h"
663b0f8d2eSBrian Somers #include "physical.h"
673b0f8d2eSBrian Somers #include "chat.h"
683b0f8d2eSBrian Somers #include "lcpproto.h"
693b0f8d2eSBrian Somers #include "filter.h"
703b0f8d2eSBrian Somers #include "mp.h"
713b0f8d2eSBrian Somers #include "chap.h"
7292b09558SBrian Somers #include "cbcp.h"
733b0f8d2eSBrian Somers #include "datalink.h"
74972a1bcfSBrian Somers #ifndef NORADIUS
75972a1bcfSBrian Somers #include "radius.h"
76972a1bcfSBrian Somers #endif
773b0f8d2eSBrian Somers #include "bundle.h"
783b0f8d2eSBrian Somers #include "ip.h"
7949052c95SBrian Somers #include "prompt.h"
8008676022SBrian Somers #include "id.h"
8108676022SBrian Somers #include "arp.h"
823b0f8d2eSBrian Somers 
83643f4904SBrian Somers void
84643f4904SBrian Somers peerid_Init(struct peerid *peer)
85643f4904SBrian Somers {
86643f4904SBrian Somers   peer->enddisc.class = 0;
87643f4904SBrian Somers   *peer->enddisc.address = '\0';
88643f4904SBrian Somers   peer->enddisc.len = 0;
89643f4904SBrian Somers   *peer->authname = '\0';
90643f4904SBrian Somers }
91643f4904SBrian Somers 
92643f4904SBrian Somers int
93643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
94643f4904SBrian Somers {
95643f4904SBrian Somers   return !strcmp(p1->authname, p2->authname) &&
96643f4904SBrian Somers          p1->enddisc.class == p2->enddisc.class &&
97643f4904SBrian Somers          p1->enddisc.len == p2->enddisc.len &&
98643f4904SBrian Somers          !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
99643f4904SBrian Somers }
100643f4904SBrian Somers 
1013b0f8d2eSBrian Somers static u_int32_t
102147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
1033b0f8d2eSBrian Somers {
1043b0f8d2eSBrian Somers   seq++;
105147613eaSBrian Somers   if (is12bit) {
1063b0f8d2eSBrian Somers     if (seq & 0xfffff000)
1073b0f8d2eSBrian Somers       seq = 0;
1083b0f8d2eSBrian Somers   } else if (seq & 0xff000000)
1093b0f8d2eSBrian Somers     seq = 0;
1103b0f8d2eSBrian Somers   return seq;
1113b0f8d2eSBrian Somers }
1123b0f8d2eSBrian Somers 
1133b0f8d2eSBrian Somers static int
1141bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1151bcced10SBrian Somers {
116b4b280abSBrian Somers   u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
117b4b280abSBrian Somers 
118b4b280abSBrian Somers   if (seq1 > max) {
119b4b280abSBrian Somers     if (seq2 < 0x200 || seq2 > seq1)
120b4b280abSBrian Somers       return 1;
121b4b280abSBrian Somers   } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
122b4b280abSBrian Somers     return 1;
123b4b280abSBrian Somers 
124b4b280abSBrian Somers   return 0;
1251bcced10SBrian Somers }
1261bcced10SBrian Somers 
1271bcced10SBrian Somers static int
1283b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1293b0f8d2eSBrian Somers {
13049052c95SBrian Somers   if (mp->local_is12bit) {
1319e8ec64bSBrian Somers     u_int16_t val;
1329e8ec64bSBrian Somers 
1339e8ec64bSBrian Somers     ua_ntohs(MBUF_CTOP(m), &val);
1349e8ec64bSBrian Somers     if (val & 0x3000) {
135dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1363b0f8d2eSBrian Somers       return 0;
1373b0f8d2eSBrian Somers     }
1389e8ec64bSBrian Somers     header->begin = val & 0x8000 ? 1 : 0;
1399e8ec64bSBrian Somers     header->end = val & 0x4000 ? 1 : 0;
1409e8ec64bSBrian Somers     header->seq = val & 0x0fff;
1413b0f8d2eSBrian Somers     return 2;
1423b0f8d2eSBrian Somers   } else {
1439e8ec64bSBrian Somers     ua_ntohl(MBUF_CTOP(m), &header->seq);
1443b0f8d2eSBrian Somers     if (header->seq & 0x3f000000) {
145dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1463b0f8d2eSBrian Somers       return 0;
1473b0f8d2eSBrian Somers     }
148ce828a6eSBrian Somers     header->begin = header->seq & 0x80000000 ? 1 : 0;
149ce828a6eSBrian Somers     header->end = header->seq & 0x40000000 ? 1 : 0;
1503b0f8d2eSBrian Somers     header->seq &= 0x00ffffff;
1513b0f8d2eSBrian Somers     return 4;
1523b0f8d2eSBrian Somers   }
1533b0f8d2eSBrian Somers }
1543b0f8d2eSBrian Somers 
1553b0f8d2eSBrian Somers static void
1563b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp)
1573b0f8d2eSBrian Somers {
15849052c95SBrian Somers   /* The given FSM (ccp) is about to start up ! */
1593b0f8d2eSBrian Somers }
1603b0f8d2eSBrian Somers 
1613b0f8d2eSBrian Somers static void
1623b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp)
1633b0f8d2eSBrian Somers {
16449052c95SBrian Somers   /* The given fsm (ccp) is now up */
1653b0f8d2eSBrian Somers }
1663b0f8d2eSBrian Somers 
1673b0f8d2eSBrian Somers static void
1683b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp)
1693b0f8d2eSBrian Somers {
17049052c95SBrian Somers   /* The given FSM (ccp) has been told to come down */
1713b0f8d2eSBrian Somers }
1723b0f8d2eSBrian Somers 
1733b0f8d2eSBrian Somers static void
1743b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp)
1753b0f8d2eSBrian Somers {
17649052c95SBrian Somers   /* The given fsm (ccp) is now down */
1770a1b5c9dSBrian Somers   if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1780a1b5c9dSBrian Somers     fsm_Open(fp);		/* CCP goes to ST_STOPPED */
1793b0f8d2eSBrian Somers }
1803b0f8d2eSBrian Somers 
1813b0f8d2eSBrian Somers void
1823b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
1833b0f8d2eSBrian Somers {
18449052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
18549052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
186643f4904SBrian Somers 
187643f4904SBrian Somers   peerid_Init(&mp->peer);
188643f4904SBrian Somers 
1899c53a7b1SBrian Somers   mp->out.seq = 0;
1909c53a7b1SBrian Somers   mp->out.link = 0;
1913b0f8d2eSBrian Somers   mp->seq.min_in = 0;
1923b0f8d2eSBrian Somers   mp->seq.next_in = 0;
1933b0f8d2eSBrian Somers   mp->inbufs = NULL;
1943b0f8d2eSBrian Somers   mp->bundle = bundle;
1953b0f8d2eSBrian Somers 
1963b0f8d2eSBrian Somers   mp->link.type = MP_LINK;
1973b0f8d2eSBrian Somers   mp->link.name = "mp";
1983b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
199643f4904SBrian Somers 
2003b0f8d2eSBrian Somers   throughput_init(&mp->link.throughput);
2013b0f8d2eSBrian Somers   memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
2023b0f8d2eSBrian Somers   memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
2033b0f8d2eSBrian Somers   memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
2043b0f8d2eSBrian Somers 
2053b0f8d2eSBrian Somers   mp->fsmp.LayerStart = mp_LayerStart;
2063b0f8d2eSBrian Somers   mp->fsmp.LayerUp = mp_LayerUp;
2073b0f8d2eSBrian Somers   mp->fsmp.LayerDown = mp_LayerDown;
2083b0f8d2eSBrian Somers   mp->fsmp.LayerFinish = mp_LayerFinish;
2093b0f8d2eSBrian Somers   mp->fsmp.object = mp;
2103b0f8d2eSBrian Somers 
2111fa665f5SBrian Somers   mpserver_Init(&mp->server);
2121fa665f5SBrian Somers 
21349052c95SBrian Somers   mp->cfg.mrru = 0;
21449052c95SBrian Somers   mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
21549052c95SBrian Somers   mp->cfg.enddisc.class = 0;
21649052c95SBrian Somers   *mp->cfg.enddisc.address = '\0';
21749052c95SBrian Somers   mp->cfg.enddisc.len = 0;
21849052c95SBrian Somers 
2193b0f8d2eSBrian Somers   lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2203b0f8d2eSBrian Somers   ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
22149052c95SBrian Somers }
22249052c95SBrian Somers 
22349052c95SBrian Somers int
2241fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
22549052c95SBrian Somers {
2261fa665f5SBrian Somers   struct lcp *lcp = &dl->physical->link.lcp;
2271fa665f5SBrian Somers 
22849052c95SBrian Somers   if (mp->active) {
22949052c95SBrian Somers     /* We're adding a link - do a last validation on our parameters */
2301fa665f5SBrian Somers     if (!peerid_Equal(&dl->peer, &mp->peer)) {
231dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
2321fa665f5SBrian Somers       return MP_FAILED;
233643f4904SBrian Somers     }
2341fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
2351fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
2361fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
2371fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
238dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
2391fa665f5SBrian Somers                 dl->name);
2401fa665f5SBrian Somers       return MP_FAILED;
24149052c95SBrian Somers     }
2421fa665f5SBrian Somers     return MP_ADDED;
24349052c95SBrian Somers   } else {
24449052c95SBrian Somers     /* First link in multilink mode */
24549052c95SBrian Somers 
2461fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
2471fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
2481fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
2491fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
2501fa665f5SBrian Somers     mp->peer = dl->peer;
25149052c95SBrian Somers 
252673903ecSBrian Somers     throughput_init(&mp->link.throughput);
253673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
254673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
255673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
256673903ecSBrian Somers 
2579c53a7b1SBrian Somers     mp->out.seq = 0;
2589c53a7b1SBrian Somers     mp->out.link = 0;
259673903ecSBrian Somers     mp->seq.min_in = 0;
260673903ecSBrian Somers     mp->seq.next_in = 0;
261673903ecSBrian Somers 
2621fa665f5SBrian Somers     /*
2631fa665f5SBrian Somers      * Now we create our server socket.
2641fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
2651fa665f5SBrian Somers      */
26696c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
26796c9bb21SBrian Somers     case MPSERVER_CONNECTED:
268dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
2696f384573SBrian Somers                 mp->server.socket.sun_path);
27096c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
2711fa665f5SBrian Somers       return MP_LINKSENT;
27296c9bb21SBrian Somers     case MPSERVER_FAILED:
2731fa665f5SBrian Somers       return MP_FAILED;
27496c9bb21SBrian Somers     case MPSERVER_LISTENING:
275dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
276dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
2771fa665f5SBrian Somers 
27849052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
279ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
2803b0f8d2eSBrian Somers 
2813b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
28206337856SBrian Somers       if (ccp_SetOpenMode(&mp->link.ccp)) {
283dd7e2610SBrian Somers         fsm_Up(&mp->link.ccp.fsm);
284dd7e2610SBrian Somers         fsm_Open(&mp->link.ccp.fsm);
28506337856SBrian Somers       }
2863b0f8d2eSBrian Somers 
2873b0f8d2eSBrian Somers       mp->active = 1;
28896c9bb21SBrian Somers       break;
28949052c95SBrian Somers     }
2901fa665f5SBrian Somers   }
2913b0f8d2eSBrian Somers 
2921fa665f5SBrian Somers   return MP_UP;
2933b0f8d2eSBrian Somers }
2943b0f8d2eSBrian Somers 
2953b0f8d2eSBrian Somers void
296673903ecSBrian Somers mp_Down(struct mp *mp)
297673903ecSBrian Somers {
298673903ecSBrian Somers   if (mp->active) {
299673903ecSBrian Somers     struct mbuf *next;
300673903ecSBrian Somers 
3011fa665f5SBrian Somers     /* Don't want any more of these */
3021fa665f5SBrian Somers     mpserver_Close(&mp->server);
3031fa665f5SBrian Somers 
3041fa665f5SBrian Somers     /* CCP goes down with a bang */
30509206a6fSBrian Somers     fsm2initial(&mp->link.ccp.fsm);
306673903ecSBrian Somers 
307673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
308673903ecSBrian Somers     while (mp->inbufs) {
309673903ecSBrian Somers       next = mp->inbufs->pnext;
310dd7e2610SBrian Somers       mbuf_Free(mp->inbufs);
311673903ecSBrian Somers       mp->inbufs = next;
312673903ecSBrian Somers     }
313673903ecSBrian Somers 
314643f4904SBrian Somers     peerid_Init(&mp->peer);
315673903ecSBrian Somers     mp->active = 0;
316673903ecSBrian Somers   }
317673903ecSBrian Somers }
318673903ecSBrian Somers 
319673903ecSBrian Somers void
3203b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
3213b0f8d2eSBrian Somers {
3223b0f8d2eSBrian Somers   mplink->seq = 0;
3233b0f8d2eSBrian Somers   mplink->weight = 1500;
3243b0f8d2eSBrian Somers }
3253b0f8d2eSBrian Somers 
3263b0f8d2eSBrian Somers void
3273b0f8d2eSBrian Somers mp_Input(struct mp *mp, struct mbuf *m, struct physical *p)
3283b0f8d2eSBrian Somers {
3293b0f8d2eSBrian Somers   struct mp_header mh, h;
3303b0f8d2eSBrian Somers   struct mbuf *q, *last;
3313b0f8d2eSBrian Somers   int32_t seq;
3323b0f8d2eSBrian Somers 
33386b1f0d7SBrian Somers   /*
33486b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
33586b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
33686b1f0d7SBrian Somers    * as normal
33786b1f0d7SBrian Somers    */
33886b1f0d7SBrian Somers 
33986b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
340dd7e2610SBrian Somers     mbuf_Free(m);
3413b0f8d2eSBrian Somers     return;
3423b0f8d2eSBrian Somers   }
3433b0f8d2eSBrian Somers 
34486b1f0d7SBrian Somers   if (p) {
3453b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
3463b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
34786b1f0d7SBrian Somers   } else
34886b1f0d7SBrian Somers     seq = mp->seq.min_in;
34986b1f0d7SBrian Somers 
3503b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
3513b0f8d2eSBrian Somers     /*
3523b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
3533b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
3543b0f8d2eSBrian Somers      */
3553b0f8d2eSBrian Somers     struct datalink *dl;
3563b0f8d2eSBrian Somers 
35786b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
3583b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
35986b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
36086b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
36186b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
3623b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
3633b0f8d2eSBrian Somers   }
3643b0f8d2eSBrian Somers 
3653b0f8d2eSBrian Somers   /*
3663b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
3673b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
3683b0f8d2eSBrian Somers    * the queue.
3693b0f8d2eSBrian Somers    */
3703b0f8d2eSBrian Somers 
3713b0f8d2eSBrian Somers   if (!mp->inbufs) {
3723b0f8d2eSBrian Somers     mp->inbufs = m;
3733b0f8d2eSBrian Somers     m = NULL;
3743b0f8d2eSBrian Somers   }
3753b0f8d2eSBrian Somers 
3763b0f8d2eSBrian Somers   last = NULL;
3773b0f8d2eSBrian Somers   seq = mp->seq.next_in;
3783b0f8d2eSBrian Somers   q = mp->inbufs;
3793b0f8d2eSBrian Somers   while (q) {
3803b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
3811bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
3823b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
3833b0f8d2eSBrian Somers       if (last)
3843b0f8d2eSBrian Somers         last->pnext = m;
3853b0f8d2eSBrian Somers       else
3863b0f8d2eSBrian Somers         mp->inbufs = m;
3873b0f8d2eSBrian Somers       m->pnext = q;
3883b0f8d2eSBrian Somers       q = m;
3893b0f8d2eSBrian Somers       h = mh;
3903b0f8d2eSBrian Somers       m = NULL;
3913b0f8d2eSBrian Somers     }
3923b0f8d2eSBrian Somers 
3933b0f8d2eSBrian Somers     if (h.seq != seq) {
3943b0f8d2eSBrian Somers       /* we're missing something :-( */
3953b0f8d2eSBrian Somers       if (mp->seq.min_in > seq) {
3963b0f8d2eSBrian Somers         /* we're never gonna get it */
3973b0f8d2eSBrian Somers         struct mbuf *next;
3983b0f8d2eSBrian Somers 
3993b0f8d2eSBrian Somers         /* Zap all older fragments */
4003b0f8d2eSBrian Somers         while (mp->inbufs != q) {
401dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
4023b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
403dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4043b0f8d2eSBrian Somers           mp->inbufs = next;
4053b0f8d2eSBrian Somers         }
4063b0f8d2eSBrian Somers 
4073b0f8d2eSBrian Somers         /*
4083b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4093b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4103b0f8d2eSBrian Somers          * comes first.
4113b0f8d2eSBrian Somers          */
4123b0f8d2eSBrian Somers         do {
4133b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4143b0f8d2eSBrian Somers           if (h.begin) {
415ce828a6eSBrian Somers             /* We might be able to process this ! */
4163b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4173b0f8d2eSBrian Somers             break;
4183b0f8d2eSBrian Somers           }
4193b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
420dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
421dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4223b0f8d2eSBrian Somers           mp->inbufs = next;
423ce828a6eSBrian Somers         } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end));
4243b0f8d2eSBrian Somers 
4253b0f8d2eSBrian Somers         /*
4263b0f8d2eSBrian Somers          * Continue processing things from here.
4273b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
4283b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
4293b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
4303b0f8d2eSBrian Somers          * packets that have already been received.
4313b0f8d2eSBrian Somers          */
4323b0f8d2eSBrian Somers 
433147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
4343b0f8d2eSBrian Somers         last = NULL;
4353b0f8d2eSBrian Somers         q = mp->inbufs;
4363b0f8d2eSBrian Somers       } else
4373b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
4383b0f8d2eSBrian Somers         break;
4393b0f8d2eSBrian Somers     } else if (h.end) {
4403b0f8d2eSBrian Somers       /* We've got something, reassemble */
4413b0f8d2eSBrian Somers       struct mbuf **frag = &q;
4423b0f8d2eSBrian Somers       int len;
443ce828a6eSBrian Somers       u_long first = -1;
4443b0f8d2eSBrian Somers 
4453b0f8d2eSBrian Somers       do {
4463b0f8d2eSBrian Somers         *frag = mp->inbufs;
4473b0f8d2eSBrian Somers         mp->inbufs = mp->inbufs->pnext;
4483b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
449ce828a6eSBrian Somers         if (first == -1)
450ce828a6eSBrian Somers           first = h.seq;
4513b0f8d2eSBrian Somers         (*frag)->offset += len;
4523b0f8d2eSBrian Somers         (*frag)->cnt -= len;
4533b0f8d2eSBrian Somers         (*frag)->pnext = NULL;
4543b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
455dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
4563b0f8d2eSBrian Somers                     (u_long)h.seq);
457dd7e2610SBrian Somers           mbuf_Free(q);
4583b0f8d2eSBrian Somers           q = NULL;
4593b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
460dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
4613b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
4623b0f8d2eSBrian Somers           /*
4633b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
4643b0f8d2eSBrian Somers            * our half-assembed packet.
4653b0f8d2eSBrian Somers            */
4663b0f8d2eSBrian Somers           (*frag)->pnext = mp->inbufs;
4673b0f8d2eSBrian Somers           mp->inbufs = *frag;
4683b0f8d2eSBrian Somers           *frag = NULL;
469dd7e2610SBrian Somers           mbuf_Free(q);
4703b0f8d2eSBrian Somers           q = NULL;
4713b0f8d2eSBrian Somers           frag = &q;
4723b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
4733b0f8d2eSBrian Somers         } else
4743b0f8d2eSBrian Somers           do
4753b0f8d2eSBrian Somers             frag = &(*frag)->next;
476ce828a6eSBrian Somers           while (*frag != NULL);
4773b0f8d2eSBrian Somers       } while (!h.end);
4783b0f8d2eSBrian Somers 
4793b0f8d2eSBrian Somers       if (q) {
480673903ecSBrian Somers         u_short proto;
481673903ecSBrian Somers         u_char ch;
482673903ecSBrian Somers 
483dd7e2610SBrian Somers         q = mbuf_Read(q, &ch, 1);
484673903ecSBrian Somers         proto = ch;
485673903ecSBrian Somers         if (!(proto & 1)) {
486dd7e2610SBrian Somers           q = mbuf_Read(q, &ch, 1);
487673903ecSBrian Somers           proto <<= 8;
4883b0f8d2eSBrian Somers           proto += ch;
489673903ecSBrian Somers         }
490dd7e2610SBrian Somers         if (log_IsKept(LogDEBUG))
491dd7e2610SBrian Somers           log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
492dd7e2610SBrian Somers                     first, (u_long)h.seq, mbuf_Length(q));
4933b0f8d2eSBrian Somers         hdlc_DecodePacket(mp->bundle, proto, q, &mp->link);
4943b0f8d2eSBrian Somers       }
4953b0f8d2eSBrian Somers 
496147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
4973b0f8d2eSBrian Somers       last = NULL;
4983b0f8d2eSBrian Somers       q = mp->inbufs;
4993b0f8d2eSBrian Somers     } else {
5003b0f8d2eSBrian Somers       /* Look for the next fragment */
501147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
5023b0f8d2eSBrian Somers       last = q;
5033b0f8d2eSBrian Somers       q = q->pnext;
5043b0f8d2eSBrian Somers     }
5053b0f8d2eSBrian Somers   }
5063b0f8d2eSBrian Somers 
5073b0f8d2eSBrian Somers   if (m) {
5083b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5093b0f8d2eSBrian Somers     last = NULL;
5103b0f8d2eSBrian Somers     for (q = mp->inbufs; q; last = q, q = q->pnext) {
5113b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5121bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
513147613eaSBrian Somers         break;
514147613eaSBrian Somers     }
515147613eaSBrian Somers     /* Our received fragment fits in here */
5163b0f8d2eSBrian Somers     if (last)
5173b0f8d2eSBrian Somers       last->pnext = m;
5183b0f8d2eSBrian Somers     else
5193b0f8d2eSBrian Somers       mp->inbufs = m;
5203b0f8d2eSBrian Somers     m->pnext = q;
5213b0f8d2eSBrian Somers   }
5223b0f8d2eSBrian Somers }
5233b0f8d2eSBrian Somers 
5243b0f8d2eSBrian Somers static void
525b4b280abSBrian Somers mp_Output(struct mp *mp, struct link *l, struct mbuf *m, u_int32_t begin,
526b4b280abSBrian Somers           u_int32_t end)
5273b0f8d2eSBrian Somers {
5283b0f8d2eSBrian Somers   struct mbuf *mo;
5293b0f8d2eSBrian Somers 
530ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
531dd7e2610SBrian Somers   mo = mbuf_Alloc(4, MB_MP);
5323b0f8d2eSBrian Somers   mo->next = m;
53349052c95SBrian Somers   if (mp->peer_is12bit) {
5349e8ec64bSBrian Somers     u_int16_t val;
535ce828a6eSBrian Somers 
5369e8ec64bSBrian Somers     val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
5379e8ec64bSBrian Somers     ua_htons(&val, MBUF_CTOP(mo));
5383b0f8d2eSBrian Somers     mo->cnt = 2;
5393b0f8d2eSBrian Somers   } else {
5409e8ec64bSBrian Somers     u_int32_t val;
541ce828a6eSBrian Somers 
5429e8ec64bSBrian Somers     val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
5439e8ec64bSBrian Somers     ua_htonl(&val, MBUF_CTOP(mo));
5443b0f8d2eSBrian Somers     mo->cnt = 4;
5453b0f8d2eSBrian Somers   }
546dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
5479c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
5489c53a7b1SBrian Somers               mp->out.seq, mbuf_Length(mo), l->name);
549147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
5503b0f8d2eSBrian Somers 
5510a1b5c9dSBrian Somers   if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, PROTO_MP, mo))
552dd7e2610SBrian Somers     hdlc_Output(l, PRI_NORMAL, PROTO_MP, mo);
5533b0f8d2eSBrian Somers }
5543b0f8d2eSBrian Somers 
5553b0f8d2eSBrian Somers int
5563b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
5573b0f8d2eSBrian Somers {
5583b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
5599c53a7b1SBrian Somers   struct datalink *dl, *fdl;
560b4b280abSBrian Somers   int total, add, len, thislink, nlinks;
561b4b280abSBrian Somers   u_int32_t begin, end;
5623b0f8d2eSBrian Somers   struct mbuf *m, *mo;
5633b0f8d2eSBrian Somers 
5649c53a7b1SBrian Somers   thislink = nlinks = 0;
5659c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
56686b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
5679c53a7b1SBrian Somers     if (!fdl) {
5689c53a7b1SBrian Somers       if (thislink == mp->out.link)
5699c53a7b1SBrian Somers         fdl = dl;
5709c53a7b1SBrian Somers       else
5719c53a7b1SBrian Somers         thislink++;
5729c53a7b1SBrian Somers     }
5739c53a7b1SBrian Somers     nlinks++;
5749c53a7b1SBrian Somers   }
5759c53a7b1SBrian Somers 
5769c53a7b1SBrian Somers   if (!fdl) {
5779c53a7b1SBrian Somers     fdl = bundle->links;
5781bc9b5baSBrian Somers     if (!fdl)
5791bc9b5baSBrian Somers       return 0;
5809c53a7b1SBrian Somers     thislink = 0;
5819c53a7b1SBrian Somers   }
5823b0f8d2eSBrian Somers 
5833b0f8d2eSBrian Somers   total = 0;
5849c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
5859c53a7b1SBrian Somers     if (!dl) {
5869c53a7b1SBrian Somers       dl = bundle->links;
5879c53a7b1SBrian Somers       thislink = 0;
5889c53a7b1SBrian Somers     }
5899c53a7b1SBrian Somers 
5906f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
5916f384573SBrian Somers       continue;
5929c53a7b1SBrian Somers 
5933b0f8d2eSBrian Somers     if (dl->physical->out)
5943b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
5953b0f8d2eSBrian Somers       continue;
5969c53a7b1SBrian Somers 
5973b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
5983b0f8d2eSBrian Somers     total += add;
5993b0f8d2eSBrian Somers     if (add)
6003b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
6013b0f8d2eSBrian Somers       continue;
6029c53a7b1SBrian Somers 
603dd7e2610SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_FlushPacket(&mp->link, bundle))
6043b0f8d2eSBrian Somers       /* Nothing else to send */
6053b0f8d2eSBrian Somers       break;
6063b0f8d2eSBrian Somers 
6073b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
608dd7e2610SBrian Somers     len = mbuf_Length(m);
6093b0f8d2eSBrian Somers     begin = 1;
6103b0f8d2eSBrian Somers     end = 0;
6113b0f8d2eSBrian Somers 
6129c53a7b1SBrian Somers     while (!end) {
6139c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
6143b0f8d2eSBrian Somers         if (len <= dl->mp.weight + LINK_MINWEIGHT) {
6159c53a7b1SBrian Somers           /*
6169c53a7b1SBrian Somers            * XXX: Should we remember how much of our `weight' wasn't sent
6179c53a7b1SBrian Somers            *      so that we can compensate next time ?
6189c53a7b1SBrian Somers            */
6193b0f8d2eSBrian Somers           mo = m;
6203b0f8d2eSBrian Somers           end = 1;
6213b0f8d2eSBrian Somers         } else {
622dd7e2610SBrian Somers           mo = mbuf_Alloc(dl->mp.weight, MB_MP);
6233b0f8d2eSBrian Somers           mo->cnt = dl->mp.weight;
6243b0f8d2eSBrian Somers           len -= mo->cnt;
625dd7e2610SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt);
6263b0f8d2eSBrian Somers         }
6273b0f8d2eSBrian Somers         mp_Output(mp, &dl->physical->link, mo, begin, end);
6283b0f8d2eSBrian Somers         begin = 0;
6293b0f8d2eSBrian Somers       }
6309c53a7b1SBrian Somers 
6319c53a7b1SBrian Somers       if (!end) {
6329c53a7b1SBrian Somers         nlinks--;
6339c53a7b1SBrian Somers         dl = dl->next;
6349c53a7b1SBrian Somers         if (!dl) {
6359c53a7b1SBrian Somers           dl = bundle->links;
6369c53a7b1SBrian Somers           thislink = 0;
6379c53a7b1SBrian Somers         } else
6389c53a7b1SBrian Somers           thislink++;
6393b0f8d2eSBrian Somers       }
6409c53a7b1SBrian Somers     }
6419c53a7b1SBrian Somers   }
6429c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
6433b0f8d2eSBrian Somers 
6443b0f8d2eSBrian Somers   return total;
6453b0f8d2eSBrian Somers }
6463b0f8d2eSBrian Somers 
6473b0f8d2eSBrian Somers int
6483b0f8d2eSBrian Somers mp_SetDatalinkWeight(struct cmdargs const *arg)
6493b0f8d2eSBrian Somers {
6503b0f8d2eSBrian Somers   int val;
6513b0f8d2eSBrian Somers 
65225092092SBrian Somers   if (arg->argc != arg->argn+1)
6533b0f8d2eSBrian Somers     return -1;
6543b0f8d2eSBrian Somers 
65525092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
6563b0f8d2eSBrian Somers   if (val < LINK_MINWEIGHT) {
657dd7e2610SBrian Somers     log_Printf(LogWARN, "Link weights must not be less than %d\n",
6583b0f8d2eSBrian Somers               LINK_MINWEIGHT);
6593b0f8d2eSBrian Somers     return 1;
6603b0f8d2eSBrian Somers   }
6613b0f8d2eSBrian Somers   arg->cx->mp.weight = val;
6623b0f8d2eSBrian Somers   return 0;
6633b0f8d2eSBrian Somers }
66449052c95SBrian Somers 
66549052c95SBrian Somers int
66649052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
66749052c95SBrian Somers {
66849052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
66949052c95SBrian Somers 
67049052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
6719c53a7b1SBrian Somers   if (mp->active) {
6729c53a7b1SBrian Somers     struct mbuf *m;
6739c53a7b1SBrian Somers     int bufs = 0;
6749c53a7b1SBrian Somers 
6751fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
6766f384573SBrian Somers                   mp->server.socket.sun_path);
6779c53a7b1SBrian Somers     for (m = mp->inbufs; m; m = m->pnext)
6789c53a7b1SBrian Somers       bufs++;
6799c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d\n", bufs);
6809c53a7b1SBrian Somers   }
68149052c95SBrian Somers 
68249052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
68349052c95SBrian Somers   if (mp->active) {
68449052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
68549052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
68649052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
68749052c95SBrian Somers   }
688643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
68949052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
69049052c95SBrian Somers                            mp->cfg.enddisc.len));
69149052c95SBrian Somers 
69249052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
69349052c95SBrian Somers   if (mp->active) {
694643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
6959c53a7b1SBrian Somers     prompt_Printf(arg->prompt, " Next SEQ:      %u\n", mp->out.seq);
69649052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
69749052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
69849052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
69949052c95SBrian Somers   }
700643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
701643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
702643f4904SBrian Somers                            mp->peer.enddisc.len));
70349052c95SBrian Somers 
70449052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
70549052c95SBrian Somers 
70649052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
70749052c95SBrian Somers   if (mp->cfg.mrru)
70849052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
70949052c95SBrian Somers   else
71049052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
71149052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
71249052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
71349052c95SBrian Somers 
71449052c95SBrian Somers   return 0;
71549052c95SBrian Somers }
71649052c95SBrian Somers 
71749052c95SBrian Somers const char *
71849052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
71949052c95SBrian Somers {
720d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
72149052c95SBrian Somers   int f, header;
72249052c95SBrian Somers 
72349052c95SBrian Somers   switch (c) {
72408676022SBrian Somers     case ENDDISC_NULL:
72549052c95SBrian Somers       sprintf(result, "Null Class");
72649052c95SBrian Somers       break;
72749052c95SBrian Somers 
72808676022SBrian Somers     case ENDDISC_LOCAL:
72949052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
73049052c95SBrian Somers       break;
73149052c95SBrian Somers 
73208676022SBrian Somers     case ENDDISC_IP:
73349052c95SBrian Somers       if (len == 4)
73449052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
73549052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
73649052c95SBrian Somers       else
73749052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
73849052c95SBrian Somers       break;
73949052c95SBrian Somers 
74008676022SBrian Somers     case ENDDISC_MAC:
74149052c95SBrian Somers       if (len == 6) {
74249052c95SBrian Somers         const u_char *m = (const u_char *)address;
74349052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
74449052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
74549052c95SBrian Somers       } else
74649052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
74749052c95SBrian Somers       break;
74849052c95SBrian Somers 
74908676022SBrian Somers     case ENDDISC_MAGIC:
75049052c95SBrian Somers       sprintf(result, "Magic: 0x");
75149052c95SBrian Somers       header = strlen(result);
75249052c95SBrian Somers       if (len > sizeof result - header - 1)
75349052c95SBrian Somers         len = sizeof result - header - 1;
75449052c95SBrian Somers       for (f = 0; f < len; f++)
75549052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
75649052c95SBrian Somers       break;
75749052c95SBrian Somers 
75808676022SBrian Somers     case ENDDISC_PSN:
75949052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
76049052c95SBrian Somers       break;
76149052c95SBrian Somers 
76249052c95SBrian Somers      default:
76349052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
76449052c95SBrian Somers       header = strlen(result);
76549052c95SBrian Somers       if (len > sizeof result - header - 1)
76649052c95SBrian Somers         len = sizeof result - header - 1;
76749052c95SBrian Somers       for (f = 0; f < len; f++)
76849052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
76949052c95SBrian Somers       break;
77049052c95SBrian Somers   }
77149052c95SBrian Somers   return result;
77249052c95SBrian Somers }
77349052c95SBrian Somers 
77449052c95SBrian Somers int
77549052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
77649052c95SBrian Somers {
77749052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
778d47dceb8SBrian Somers   struct in_addr addr;
77949052c95SBrian Somers 
7809b5f8ffdSBrian Somers   switch (bundle_Phase(arg->bundle)) {
7819b5f8ffdSBrian Somers     case PHASE_DEAD:
7829b5f8ffdSBrian Somers       break;
7839b5f8ffdSBrian Somers     case PHASE_ESTABLISH:
7849b5f8ffdSBrian Somers       /* Make sure none of our links are DATALINK_LCP or greater */
7859b5f8ffdSBrian Somers       if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
7869b5f8ffdSBrian Somers         log_Printf(LogWARN, "enddisc: Only changable before"
7879b5f8ffdSBrian Somers                    " LCP negotiations\n");
7889b5f8ffdSBrian Somers         return 1;
7899b5f8ffdSBrian Somers       }
7909b5f8ffdSBrian Somers       break;
7919b5f8ffdSBrian Somers     default:
7929b5f8ffdSBrian Somers       log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
79349052c95SBrian Somers       return 1;
79449052c95SBrian Somers   }
79549052c95SBrian Somers 
79649052c95SBrian Somers   if (arg->argc == arg->argn) {
79749052c95SBrian Somers     mp->cfg.enddisc.class = 0;
79849052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
79949052c95SBrian Somers     mp->cfg.enddisc.len = 0;
800e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
80108676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
80208676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
80308676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
80408676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
80508676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
806259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
807259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
808d47dceb8SBrian Somers       else
809259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
810d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
81108676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
81249052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
81308676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
81408676022SBrian Somers       struct sockaddr_dl hwaddr;
81508676022SBrian Somers       int s;
81608676022SBrian Somers 
817259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
818259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
819d47dceb8SBrian Somers       else
820259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
821d47dceb8SBrian Somers 
82208676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
82308676022SBrian Somers       if (s < 0) {
824dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
82508676022SBrian Somers         return 2;
82608676022SBrian Somers       }
827d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
82808676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
82908676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
83008676022SBrian Somers                hwaddr.sdl_alen);
83108676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
83208676022SBrian Somers       } else {
833dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
834259a6191SBrian Somers                   inet_ntoa(addr));
83508676022SBrian Somers         close(s);
83608676022SBrian Somers         return 4;
83708676022SBrian Somers       }
83808676022SBrian Somers       close(s);
83949052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
84049052c95SBrian Somers       int f;
84149052c95SBrian Somers 
84249052c95SBrian Somers       randinit();
84349052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
84449052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
84508676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
84649052c95SBrian Somers       mp->cfg.enddisc.len = 20;
84749052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
84849052c95SBrian Somers       if (arg->argc > arg->argn+1) {
84908676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
85049052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
85149052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
85249052c95SBrian Somers       } else {
853dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
85408676022SBrian Somers         return 5;
85549052c95SBrian Somers       }
85649052c95SBrian Somers     } else {
857dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
85849052c95SBrian Somers                 arg->argv[arg->argn]);
85908676022SBrian Somers       return 6;
86049052c95SBrian Somers     }
861e43ebac1SBrian Somers   }
86249052c95SBrian Somers 
86349052c95SBrian Somers   return 0;
86449052c95SBrian Somers }
8651fa665f5SBrian Somers 
8661fa665f5SBrian Somers static int
8671fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
8681fa665f5SBrian Somers                    int *n)
8691fa665f5SBrian Somers {
8701fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
8710f2f3eb3SBrian Somers   int result;
8721fa665f5SBrian Somers 
8730f2f3eb3SBrian Somers   result = 0;
8746f384573SBrian Somers   if (s->send.dl != NULL) {
87596c9bb21SBrian Somers     /* We've connect()ed */
876b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
877b7c5748eSBrian Somers         !s->send.dl->physical->out) {
878b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
8790f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
88096c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
8816f384573SBrian Somers       s->send.dl = NULL;
88296c9bb21SBrian Somers       s->fd = -1;
883ea722969SBrian Somers     } else
884ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
8850f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
88696c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
8871fa665f5SBrian Somers     if (*n < s->fd + 1)
8881fa665f5SBrian Somers       *n = s->fd + 1;
8891fa665f5SBrian Somers     FD_SET(s->fd, r);
89024989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
8910f2f3eb3SBrian Somers     result++;
8921fa665f5SBrian Somers   }
8930f2f3eb3SBrian Somers   return result;
8941fa665f5SBrian Somers }
8951fa665f5SBrian Somers 
8961fa665f5SBrian Somers static int
8971fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset)
8981fa665f5SBrian Somers {
8991fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9001fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
9011fa665f5SBrian Somers }
9021fa665f5SBrian Somers 
9031fa665f5SBrian Somers static void
9041fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
9051fa665f5SBrian Somers {
9061fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
9076f384573SBrian Somers   struct sockaddr in;
9081fa665f5SBrian Somers   int fd, size;
9091fa665f5SBrian Somers 
9106f384573SBrian Somers   size = sizeof in;
9116f384573SBrian Somers   fd = accept(s->fd, &in, &size);
9121fa665f5SBrian Somers   if (fd < 0) {
913dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno));
9141fa665f5SBrian Somers     return;
9151fa665f5SBrian Somers   }
9161fa665f5SBrian Somers 
9171bc9b5baSBrian Somers   if (in.sa_family == AF_LOCAL)
91896c9bb21SBrian Somers     bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in);
91954cd8e13SBrian Somers   else
9201fa665f5SBrian Somers     close(fd);
9211fa665f5SBrian Somers }
9221fa665f5SBrian Somers 
9231af29a6eSBrian Somers static int
9241fa665f5SBrian Somers mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
9251fa665f5SBrian Somers {
9261fa665f5SBrian Somers   /* We never want to write here ! */
927a33b2ef7SBrian Somers   log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
9281af29a6eSBrian Somers   return 0;
9291fa665f5SBrian Somers }
9301fa665f5SBrian Somers 
9311fa665f5SBrian Somers void
9321fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
9331fa665f5SBrian Somers {
9341fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
9351fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
9361fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
9371fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
9381fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
9396f384573SBrian Somers   s->send.dl = NULL;
9401fa665f5SBrian Somers   s->fd = -1;
9416f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
9421fa665f5SBrian Somers }
9431fa665f5SBrian Somers 
9441fa665f5SBrian Somers int
9451fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
9461fa665f5SBrian Somers {
94796c9bb21SBrian Somers   int f, l;
9481fa665f5SBrian Somers   mode_t mask;
9491fa665f5SBrian Somers 
9501fa665f5SBrian Somers   if (s->fd != -1) {
951a33b2ef7SBrian Somers     log_Printf(LogALERT, "Internal error !  mpserver already open\n");
9526f384573SBrian Somers     mpserver_Close(s);
9531fa665f5SBrian Somers   }
9541fa665f5SBrian Somers 
9556f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
9566f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
9571fa665f5SBrian Somers 
9586f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
9596f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
9601fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
9616f384573SBrian Somers     l += 2;
9621fa665f5SBrian Somers   }
9631fa665f5SBrian Somers 
9646f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
9656f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
9661fa665f5SBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
9671fa665f5SBrian Somers   if (s->fd < 0) {
968dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno));
96996c9bb21SBrian Somers     return MPSERVER_FAILED;
9701fa665f5SBrian Somers   }
9711fa665f5SBrian Somers 
9726f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
9736f384573SBrian Somers              sizeof s->socket);
9741fa665f5SBrian Somers   mask = umask(0177);
9756f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
9766f384573SBrian Somers     if (errno != EADDRINUSE) {
977dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
9786f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
9791fa665f5SBrian Somers       umask(mask);
9801fa665f5SBrian Somers       close(s->fd);
9811fa665f5SBrian Somers       s->fd = -1;
98296c9bb21SBrian Somers       return MPSERVER_FAILED;
9836f384573SBrian Somers     }
9846f384573SBrian Somers     umask(mask);
9856f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
986dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
9876f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
9886f384573SBrian Somers       if (errno == ECONNREFUSED)
989dd7e2610SBrian Somers         log_Printf(LogPHASE, "          Has the previous server died badly ?\n");
9906f384573SBrian Somers       close(s->fd);
9916f384573SBrian Somers       s->fd = -1;
99296c9bb21SBrian Somers       return MPSERVER_FAILED;
9936f384573SBrian Somers     }
9946f384573SBrian Somers 
9956f384573SBrian Somers     /* Donate our link to the other guy */
99696c9bb21SBrian Somers     return MPSERVER_CONNECTED;
9971fa665f5SBrian Somers   }
9986f384573SBrian Somers 
9996f384573SBrian Somers   /* Listen for other ppp invocations that want to donate links */
10001fa665f5SBrian Somers   if (listen(s->fd, 5) != 0) {
1001dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: Unable to listen to socket"
10021fa665f5SBrian Somers               " - BUNDLE overload?\n");
10031fa665f5SBrian Somers     mpserver_Close(s);
10041fa665f5SBrian Somers   }
10051fa665f5SBrian Somers 
100696c9bb21SBrian Somers   return MPSERVER_LISTENING;
10071fa665f5SBrian Somers }
10081fa665f5SBrian Somers 
10091fa665f5SBrian Somers void
10101fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
10111fa665f5SBrian Somers {
10126f384573SBrian Somers   if (s->send.dl != NULL) {
101396c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
10146f384573SBrian Somers     s->send.dl = NULL;
101596c9bb21SBrian Somers     s->fd = -1;
101696c9bb21SBrian Somers   } else if (s->fd >= 0) {
10171fa665f5SBrian Somers     close(s->fd);
10186f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1019dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
10206f384573SBrian Somers                 strerror(errno));
10216f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
10221fa665f5SBrian Somers     s->fd = -1;
10231fa665f5SBrian Somers   }
10241fa665f5SBrian Somers }
102586b1f0d7SBrian Somers 
102686b1f0d7SBrian Somers void
102786b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
102886b1f0d7SBrian Somers {
102986b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
103086b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
103186b1f0d7SBrian Somers     mp_Input(mp, NULL, NULL);
103286b1f0d7SBrian Somers }
10336f8e9f0aSBrian Somers 
10346f8e9f0aSBrian Somers void
10356f8e9f0aSBrian Somers mp_DeleteQueue(struct mp *mp)
10366f8e9f0aSBrian Somers {
10376f8e9f0aSBrian Somers   link_DeleteQueue(&mp->link);
10386f8e9f0aSBrian Somers }
1039