xref: /freebsd/usr.sbin/ppp/mp.c (revision d93d3a9c32d3e1ba843e8e7033f6570c714f86df)
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  *
26d93d3a9cSBrian Somers  *	$Id: mp.c,v 1.7 1998/06/15 19:06:22 brian Exp $
273b0f8d2eSBrian Somers  */
283b0f8d2eSBrian Somers 
293b0f8d2eSBrian Somers #include <sys/types.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 
47c9e11a11SBrian Somers #include "defs.h"
483b0f8d2eSBrian Somers #include "command.h"
493b0f8d2eSBrian Somers #include "mbuf.h"
503b0f8d2eSBrian Somers #include "log.h"
513b0f8d2eSBrian Somers #include "timer.h"
523b0f8d2eSBrian Somers #include "fsm.h"
533b0f8d2eSBrian Somers #include "iplist.h"
543b0f8d2eSBrian Somers #include "throughput.h"
553b0f8d2eSBrian Somers #include "slcompress.h"
563b0f8d2eSBrian Somers #include "ipcp.h"
573b0f8d2eSBrian Somers #include "auth.h"
583b0f8d2eSBrian Somers #include "lcp.h"
593b0f8d2eSBrian Somers #include "lqr.h"
603b0f8d2eSBrian Somers #include "hdlc.h"
613b0f8d2eSBrian Somers #include "async.h"
623b0f8d2eSBrian Somers #include "ccp.h"
633b0f8d2eSBrian Somers #include "link.h"
643b0f8d2eSBrian Somers #include "descriptor.h"
653b0f8d2eSBrian Somers #include "physical.h"
663b0f8d2eSBrian Somers #include "chat.h"
673b0f8d2eSBrian Somers #include "lcpproto.h"
683b0f8d2eSBrian Somers #include "filter.h"
693b0f8d2eSBrian Somers #include "mp.h"
703b0f8d2eSBrian Somers #include "chap.h"
713b0f8d2eSBrian Somers #include "datalink.h"
723b0f8d2eSBrian Somers #include "bundle.h"
733b0f8d2eSBrian Somers #include "ip.h"
7449052c95SBrian Somers #include "prompt.h"
7508676022SBrian Somers #include "id.h"
7608676022SBrian Somers #include "arp.h"
773b0f8d2eSBrian Somers 
78643f4904SBrian Somers void
79643f4904SBrian Somers peerid_Init(struct peerid *peer)
80643f4904SBrian Somers {
81643f4904SBrian Somers   peer->enddisc.class = 0;
82643f4904SBrian Somers   *peer->enddisc.address = '\0';
83643f4904SBrian Somers   peer->enddisc.len = 0;
84643f4904SBrian Somers   *peer->authname = '\0';
85643f4904SBrian Somers }
86643f4904SBrian Somers 
87643f4904SBrian Somers int
88643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2)
89643f4904SBrian Somers {
90643f4904SBrian Somers   return !strcmp(p1->authname, p2->authname) &&
91643f4904SBrian Somers          p1->enddisc.class == p2->enddisc.class &&
92643f4904SBrian Somers          p1->enddisc.len == p2->enddisc.len &&
93643f4904SBrian Somers          !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
94643f4904SBrian Somers }
95643f4904SBrian Somers 
963b0f8d2eSBrian Somers static u_int32_t
97147613eaSBrian Somers inc_seq(unsigned is12bit, u_int32_t seq)
983b0f8d2eSBrian Somers {
993b0f8d2eSBrian Somers   seq++;
100147613eaSBrian Somers   if (is12bit) {
1013b0f8d2eSBrian Somers     if (seq & 0xfffff000)
1023b0f8d2eSBrian Somers       seq = 0;
1033b0f8d2eSBrian Somers   } else if (seq & 0xff000000)
1043b0f8d2eSBrian Somers     seq = 0;
1053b0f8d2eSBrian Somers   return seq;
1063b0f8d2eSBrian Somers }
1073b0f8d2eSBrian Somers 
1083b0f8d2eSBrian Somers static int
1091bcced10SBrian Somers isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
1101bcced10SBrian Somers {
111b4b280abSBrian Somers   u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
112b4b280abSBrian Somers 
113b4b280abSBrian Somers   if (seq1 > max) {
114b4b280abSBrian Somers     if (seq2 < 0x200 || seq2 > seq1)
115b4b280abSBrian Somers       return 1;
116b4b280abSBrian Somers   } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
117b4b280abSBrian Somers     return 1;
118b4b280abSBrian Somers 
119b4b280abSBrian Somers   return 0;
1201bcced10SBrian Somers }
1211bcced10SBrian Somers 
1221bcced10SBrian Somers static int
1233b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
1243b0f8d2eSBrian Somers {
12549052c95SBrian Somers   if (mp->local_is12bit) {
126ce828a6eSBrian Somers     header->seq = ntohs(*(u_int16_t *)MBUF_CTOP(m));
1273b0f8d2eSBrian Somers     if (header->seq & 0x3000) {
128dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1293b0f8d2eSBrian Somers       return 0;
1303b0f8d2eSBrian Somers     }
131ce828a6eSBrian Somers     header->begin = header->seq & 0x8000 ? 1 : 0;
132ce828a6eSBrian Somers     header->end = header->seq & 0x4000 ? 1 : 0;
1333b0f8d2eSBrian Somers     header->seq &= 0x0fff;
1343b0f8d2eSBrian Somers     return 2;
1353b0f8d2eSBrian Somers   } else {
136ce828a6eSBrian Somers     header->seq = ntohl(*(u_int32_t *)MBUF_CTOP(m));
1373b0f8d2eSBrian Somers     if (header->seq & 0x3f000000) {
138dd7e2610SBrian Somers       log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
1393b0f8d2eSBrian Somers       return 0;
1403b0f8d2eSBrian Somers     }
141ce828a6eSBrian Somers     header->begin = header->seq & 0x80000000 ? 1 : 0;
142ce828a6eSBrian Somers     header->end = header->seq & 0x40000000 ? 1 : 0;
1433b0f8d2eSBrian Somers     header->seq &= 0x00ffffff;
1443b0f8d2eSBrian Somers     return 4;
1453b0f8d2eSBrian Somers   }
1463b0f8d2eSBrian Somers }
1473b0f8d2eSBrian Somers 
1483b0f8d2eSBrian Somers static void
1493b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp)
1503b0f8d2eSBrian Somers {
15149052c95SBrian Somers   /* The given FSM (ccp) is about to start up ! */
1523b0f8d2eSBrian Somers }
1533b0f8d2eSBrian Somers 
1543b0f8d2eSBrian Somers static void
1553b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp)
1563b0f8d2eSBrian Somers {
15749052c95SBrian Somers   /* The given fsm (ccp) is now up */
1583b0f8d2eSBrian Somers }
1593b0f8d2eSBrian Somers 
1603b0f8d2eSBrian Somers static void
1613b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp)
1623b0f8d2eSBrian Somers {
16349052c95SBrian Somers   /* The given FSM (ccp) has been told to come down */
1643b0f8d2eSBrian Somers }
1653b0f8d2eSBrian Somers 
1663b0f8d2eSBrian Somers static void
1673b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp)
1683b0f8d2eSBrian Somers {
16949052c95SBrian Somers   /* The given fsm (ccp) is now down */
1700a1b5c9dSBrian Somers   if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
1710a1b5c9dSBrian Somers     fsm_Open(fp);		/* CCP goes to ST_STOPPED */
1723b0f8d2eSBrian Somers }
1733b0f8d2eSBrian Somers 
1743b0f8d2eSBrian Somers void
1753b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle)
1763b0f8d2eSBrian Somers {
17749052c95SBrian Somers   mp->peer_is12bit = mp->local_is12bit = 0;
17849052c95SBrian Somers   mp->peer_mrru = mp->local_mrru = 0;
179643f4904SBrian Somers 
180643f4904SBrian Somers   peerid_Init(&mp->peer);
181643f4904SBrian Somers 
1829c53a7b1SBrian Somers   mp->out.seq = 0;
1839c53a7b1SBrian Somers   mp->out.link = 0;
1843b0f8d2eSBrian Somers   mp->seq.min_in = 0;
1853b0f8d2eSBrian Somers   mp->seq.next_in = 0;
1863b0f8d2eSBrian Somers   mp->inbufs = NULL;
1873b0f8d2eSBrian Somers   mp->bundle = bundle;
1883b0f8d2eSBrian Somers 
1893b0f8d2eSBrian Somers   mp->link.type = MP_LINK;
1903b0f8d2eSBrian Somers   mp->link.name = "mp";
1913b0f8d2eSBrian Somers   mp->link.len = sizeof *mp;
192643f4904SBrian Somers 
1933b0f8d2eSBrian Somers   throughput_init(&mp->link.throughput);
1943b0f8d2eSBrian Somers   memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
1953b0f8d2eSBrian Somers   memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
1963b0f8d2eSBrian Somers   memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
1973b0f8d2eSBrian Somers 
1983b0f8d2eSBrian Somers   mp->fsmp.LayerStart = mp_LayerStart;
1993b0f8d2eSBrian Somers   mp->fsmp.LayerUp = mp_LayerUp;
2003b0f8d2eSBrian Somers   mp->fsmp.LayerDown = mp_LayerDown;
2013b0f8d2eSBrian Somers   mp->fsmp.LayerFinish = mp_LayerFinish;
2023b0f8d2eSBrian Somers   mp->fsmp.object = mp;
2033b0f8d2eSBrian Somers 
2041fa665f5SBrian Somers   mpserver_Init(&mp->server);
2051fa665f5SBrian Somers 
20649052c95SBrian Somers   mp->cfg.mrru = 0;
20749052c95SBrian Somers   mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
20849052c95SBrian Somers   mp->cfg.enddisc.class = 0;
20949052c95SBrian Somers   *mp->cfg.enddisc.address = '\0';
21049052c95SBrian Somers   mp->cfg.enddisc.len = 0;
21149052c95SBrian Somers 
2123b0f8d2eSBrian Somers   lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
2133b0f8d2eSBrian Somers   ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
21449052c95SBrian Somers }
21549052c95SBrian Somers 
21649052c95SBrian Somers int
2171fa665f5SBrian Somers mp_Up(struct mp *mp, struct datalink *dl)
21849052c95SBrian Somers {
2191fa665f5SBrian Somers   struct lcp *lcp = &dl->physical->link.lcp;
2201fa665f5SBrian Somers 
22149052c95SBrian Somers   if (mp->active) {
22249052c95SBrian Somers     /* We're adding a link - do a last validation on our parameters */
2231fa665f5SBrian Somers     if (!peerid_Equal(&dl->peer, &mp->peer)) {
224dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
2251fa665f5SBrian Somers       return MP_FAILED;
226643f4904SBrian Somers     }
2271fa665f5SBrian Somers     if (mp->local_mrru != lcp->want_mrru ||
2281fa665f5SBrian Somers         mp->peer_mrru != lcp->his_mrru ||
2291fa665f5SBrian Somers         mp->local_is12bit != lcp->want_shortseq ||
2301fa665f5SBrian Somers         mp->peer_is12bit != lcp->his_shortseq) {
231dd7e2610SBrian Somers       log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
2321fa665f5SBrian Somers                 dl->name);
2331fa665f5SBrian Somers       return MP_FAILED;
23449052c95SBrian Somers     }
2351fa665f5SBrian Somers     return MP_ADDED;
23649052c95SBrian Somers   } else {
23749052c95SBrian Somers     /* First link in multilink mode */
23849052c95SBrian Somers 
2391fa665f5SBrian Somers     mp->local_mrru = lcp->want_mrru;
2401fa665f5SBrian Somers     mp->peer_mrru = lcp->his_mrru;
2411fa665f5SBrian Somers     mp->local_is12bit = lcp->want_shortseq;
2421fa665f5SBrian Somers     mp->peer_is12bit = lcp->his_shortseq;
2431fa665f5SBrian Somers     mp->peer = dl->peer;
24449052c95SBrian Somers 
245673903ecSBrian Somers     throughput_init(&mp->link.throughput);
246673903ecSBrian Somers     memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
247673903ecSBrian Somers     memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
248673903ecSBrian Somers     memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
249673903ecSBrian Somers 
2509c53a7b1SBrian Somers     mp->out.seq = 0;
2519c53a7b1SBrian Somers     mp->out.link = 0;
252673903ecSBrian Somers     mp->seq.min_in = 0;
253673903ecSBrian Somers     mp->seq.next_in = 0;
254673903ecSBrian Somers 
2551fa665f5SBrian Somers     /*
2561fa665f5SBrian Somers      * Now we create our server socket.
2571fa665f5SBrian Somers      * If it already exists, join it.  Otherwise, create and own it
2581fa665f5SBrian Somers      */
25996c9bb21SBrian Somers     switch (mpserver_Open(&mp->server, &mp->peer)) {
26096c9bb21SBrian Somers     case MPSERVER_CONNECTED:
261dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Transfer link on %s\n",
2626f384573SBrian Somers                 mp->server.socket.sun_path);
26396c9bb21SBrian Somers       mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
2641fa665f5SBrian Somers       return MP_LINKSENT;
26596c9bb21SBrian Somers     case MPSERVER_FAILED:
2661fa665f5SBrian Somers       return MP_FAILED;
26796c9bb21SBrian Somers     case MPSERVER_LISTENING:
268dd7e2610SBrian Somers       log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
269dd7e2610SBrian Somers       log_Printf(LogPHASE, "    First link: %s\n", dl->name);
2701fa665f5SBrian Somers 
27149052c95SBrian Somers       /* Re-point our IPCP layer at our MP link */
272ce828a6eSBrian Somers       ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
2733b0f8d2eSBrian Somers 
2743b0f8d2eSBrian Somers       /* Our lcp's already up 'cos of the NULL parent */
2751df0a3b9SBrian Somers       ccp_SetOpenMode(&mp->link.ccp);
276dd7e2610SBrian Somers       fsm_Up(&mp->link.ccp.fsm);
277dd7e2610SBrian Somers       fsm_Open(&mp->link.ccp.fsm);
2783b0f8d2eSBrian Somers 
2793b0f8d2eSBrian Somers       mp->active = 1;
28096c9bb21SBrian Somers       break;
28149052c95SBrian Somers     }
2821fa665f5SBrian Somers   }
2833b0f8d2eSBrian Somers 
2841fa665f5SBrian Somers   return MP_UP;
2853b0f8d2eSBrian Somers }
2863b0f8d2eSBrian Somers 
2873b0f8d2eSBrian Somers void
288673903ecSBrian Somers mp_Down(struct mp *mp)
289673903ecSBrian Somers {
290673903ecSBrian Somers   if (mp->active) {
291673903ecSBrian Somers     struct mbuf *next;
292673903ecSBrian Somers 
2931fa665f5SBrian Somers     /* Don't want any more of these */
2941fa665f5SBrian Somers     mpserver_Close(&mp->server);
2951fa665f5SBrian Somers 
2961fa665f5SBrian Somers     /* CCP goes down with a bang */
297dd7e2610SBrian Somers     fsm_Down(&mp->link.ccp.fsm);
298dd7e2610SBrian Somers     fsm_Close(&mp->link.ccp.fsm);
299673903ecSBrian Somers 
300673903ecSBrian Somers     /* Received fragments go in the bit-bucket */
301673903ecSBrian Somers     while (mp->inbufs) {
302673903ecSBrian Somers       next = mp->inbufs->pnext;
303dd7e2610SBrian Somers       mbuf_Free(mp->inbufs);
304673903ecSBrian Somers       mp->inbufs = next;
305673903ecSBrian Somers     }
306673903ecSBrian Somers 
307643f4904SBrian Somers     peerid_Init(&mp->peer);
308673903ecSBrian Somers     mp->active = 0;
309673903ecSBrian Somers   }
310673903ecSBrian Somers }
311673903ecSBrian Somers 
312673903ecSBrian Somers void
3133b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink)
3143b0f8d2eSBrian Somers {
3153b0f8d2eSBrian Somers   mplink->seq = 0;
3163b0f8d2eSBrian Somers   mplink->weight = 1500;
3173b0f8d2eSBrian Somers }
3183b0f8d2eSBrian Somers 
3193b0f8d2eSBrian Somers void
3203b0f8d2eSBrian Somers mp_Input(struct mp *mp, struct mbuf *m, struct physical *p)
3213b0f8d2eSBrian Somers {
3223b0f8d2eSBrian Somers   struct mp_header mh, h;
3233b0f8d2eSBrian Somers   struct mbuf *q, *last;
3243b0f8d2eSBrian Somers   int32_t seq;
3253b0f8d2eSBrian Somers 
32686b1f0d7SBrian Somers   /*
32786b1f0d7SBrian Somers    * When `m' and `p' are NULL, it means our oldest link has gone down.
32886b1f0d7SBrian Somers    * We want to determine a new min, and process any intermediate stuff
32986b1f0d7SBrian Somers    * as normal
33086b1f0d7SBrian Somers    */
33186b1f0d7SBrian Somers 
33286b1f0d7SBrian Somers   if (m && mp_ReadHeader(mp, m, &mh) == 0) {
333dd7e2610SBrian Somers     mbuf_Free(m);
3343b0f8d2eSBrian Somers     return;
3353b0f8d2eSBrian Somers   }
3363b0f8d2eSBrian Somers 
33786b1f0d7SBrian Somers   if (p) {
3383b0f8d2eSBrian Somers     seq = p->dl->mp.seq;
3393b0f8d2eSBrian Somers     p->dl->mp.seq = mh.seq;
34086b1f0d7SBrian Somers   } else
34186b1f0d7SBrian Somers     seq = mp->seq.min_in;
34286b1f0d7SBrian Somers 
3433b0f8d2eSBrian Somers   if (mp->seq.min_in == seq) {
3443b0f8d2eSBrian Somers     /*
3453b0f8d2eSBrian Somers      * We've received new data on the link that has our min (oldest) seq.
3463b0f8d2eSBrian Somers      * Figure out which link now has the smallest (oldest) seq.
3473b0f8d2eSBrian Somers      */
3483b0f8d2eSBrian Somers     struct datalink *dl;
3493b0f8d2eSBrian Somers 
35086b1f0d7SBrian Somers     mp->seq.min_in = (u_int32_t)-1;
3513b0f8d2eSBrian Somers     for (dl = mp->bundle->links; dl; dl = dl->next)
35286b1f0d7SBrian Somers       if (dl->state == DATALINK_OPEN &&
35386b1f0d7SBrian Somers           (mp->seq.min_in == -1 ||
35486b1f0d7SBrian Somers            isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
3553b0f8d2eSBrian Somers         mp->seq.min_in = dl->mp.seq;
3563b0f8d2eSBrian Somers   }
3573b0f8d2eSBrian Somers 
3583b0f8d2eSBrian Somers   /*
3593b0f8d2eSBrian Somers    * Now process as many of our fragments as we can, adding our new
3603b0f8d2eSBrian Somers    * fragment in as we go, and ordering with the oldest at the top of
3613b0f8d2eSBrian Somers    * the queue.
3623b0f8d2eSBrian Somers    */
3633b0f8d2eSBrian Somers 
3643b0f8d2eSBrian Somers   if (!mp->inbufs) {
3653b0f8d2eSBrian Somers     mp->inbufs = m;
3663b0f8d2eSBrian Somers     m = NULL;
3673b0f8d2eSBrian Somers   }
3683b0f8d2eSBrian Somers 
3693b0f8d2eSBrian Somers   last = NULL;
3703b0f8d2eSBrian Somers   seq = mp->seq.next_in;
3713b0f8d2eSBrian Somers   q = mp->inbufs;
3723b0f8d2eSBrian Somers   while (q) {
3733b0f8d2eSBrian Somers     mp_ReadHeader(mp, q, &h);
3741bcced10SBrian Somers     if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
3753b0f8d2eSBrian Somers       /* Our received fragment fits in before this one, so link it in */
3763b0f8d2eSBrian Somers       if (last)
3773b0f8d2eSBrian Somers         last->pnext = m;
3783b0f8d2eSBrian Somers       else
3793b0f8d2eSBrian Somers         mp->inbufs = m;
3803b0f8d2eSBrian Somers       m->pnext = q;
3813b0f8d2eSBrian Somers       q = m;
3823b0f8d2eSBrian Somers       h = mh;
3833b0f8d2eSBrian Somers       m = NULL;
3843b0f8d2eSBrian Somers     }
3853b0f8d2eSBrian Somers 
3863b0f8d2eSBrian Somers     if (h.seq != seq) {
3873b0f8d2eSBrian Somers       /* we're missing something :-( */
3883b0f8d2eSBrian Somers       if (mp->seq.min_in > seq) {
3893b0f8d2eSBrian Somers         /* we're never gonna get it */
3903b0f8d2eSBrian Somers         struct mbuf *next;
3913b0f8d2eSBrian Somers 
3923b0f8d2eSBrian Somers         /* Zap all older fragments */
3933b0f8d2eSBrian Somers         while (mp->inbufs != q) {
394dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag\n");
3953b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
396dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
3973b0f8d2eSBrian Somers           mp->inbufs = next;
3983b0f8d2eSBrian Somers         }
3993b0f8d2eSBrian Somers 
4003b0f8d2eSBrian Somers         /*
4013b0f8d2eSBrian Somers          * Zap everything until the next `end' fragment OR just before
4023b0f8d2eSBrian Somers          * the next `begin' fragment OR 'till seq.min_in - whichever
4033b0f8d2eSBrian Somers          * comes first.
4043b0f8d2eSBrian Somers          */
4053b0f8d2eSBrian Somers         do {
4063b0f8d2eSBrian Somers           mp_ReadHeader(mp, mp->inbufs, &h);
4073b0f8d2eSBrian Somers           if (h.begin) {
408ce828a6eSBrian Somers             /* We might be able to process this ! */
4093b0f8d2eSBrian Somers             h.seq--;  /* We're gonna look for fragment with h.seq+1 */
4103b0f8d2eSBrian Somers             break;
4113b0f8d2eSBrian Somers           }
4123b0f8d2eSBrian Somers           next = mp->inbufs->pnext;
413dd7e2610SBrian Somers           log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
414dd7e2610SBrian Somers           mbuf_Free(mp->inbufs);
4153b0f8d2eSBrian Somers           mp->inbufs = next;
416ce828a6eSBrian Somers         } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end));
4173b0f8d2eSBrian Somers 
4183b0f8d2eSBrian Somers         /*
4193b0f8d2eSBrian Somers          * Continue processing things from here.
4203b0f8d2eSBrian Somers          * This deals with the possibility that we received a fragment
4213b0f8d2eSBrian Somers          * on the slowest link that invalidates some of our data (because
4223b0f8d2eSBrian Somers          * of the hole at `q'), but where there are subsequent `whole'
4233b0f8d2eSBrian Somers          * packets that have already been received.
4243b0f8d2eSBrian Somers          */
4253b0f8d2eSBrian Somers 
426147613eaSBrian Somers         mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
4273b0f8d2eSBrian Somers         last = NULL;
4283b0f8d2eSBrian Somers         q = mp->inbufs;
4293b0f8d2eSBrian Somers       } else
4303b0f8d2eSBrian Somers         /* we may still receive the missing fragment */
4313b0f8d2eSBrian Somers         break;
4323b0f8d2eSBrian Somers     } else if (h.end) {
4333b0f8d2eSBrian Somers       /* We've got something, reassemble */
4343b0f8d2eSBrian Somers       struct mbuf **frag = &q;
4353b0f8d2eSBrian Somers       int len;
436ce828a6eSBrian Somers       u_long first = -1;
4373b0f8d2eSBrian Somers 
4383b0f8d2eSBrian Somers       do {
4393b0f8d2eSBrian Somers         *frag = mp->inbufs;
4403b0f8d2eSBrian Somers         mp->inbufs = mp->inbufs->pnext;
4413b0f8d2eSBrian Somers         len = mp_ReadHeader(mp, *frag, &h);
442ce828a6eSBrian Somers         if (first == -1)
443ce828a6eSBrian Somers           first = h.seq;
4443b0f8d2eSBrian Somers         (*frag)->offset += len;
4453b0f8d2eSBrian Somers         (*frag)->cnt -= len;
4463b0f8d2eSBrian Somers         (*frag)->pnext = NULL;
4473b0f8d2eSBrian Somers         if (frag == &q && !h.begin) {
448dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
4493b0f8d2eSBrian Somers                     (u_long)h.seq);
450dd7e2610SBrian Somers           mbuf_Free(q);
4513b0f8d2eSBrian Somers           q = NULL;
4523b0f8d2eSBrian Somers         } else if (frag != &q && h.begin) {
453dd7e2610SBrian Somers           log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
4543b0f8d2eSBrian Somers                     (u_long)h.seq - 1);
4553b0f8d2eSBrian Somers           /*
4563b0f8d2eSBrian Somers            * Stuff our fragment back at the front of the queue and zap
4573b0f8d2eSBrian Somers            * our half-assembed packet.
4583b0f8d2eSBrian Somers            */
4593b0f8d2eSBrian Somers           (*frag)->pnext = mp->inbufs;
4603b0f8d2eSBrian Somers           mp->inbufs = *frag;
4613b0f8d2eSBrian Somers           *frag = NULL;
462dd7e2610SBrian Somers           mbuf_Free(q);
4633b0f8d2eSBrian Somers           q = NULL;
4643b0f8d2eSBrian Somers           frag = &q;
4653b0f8d2eSBrian Somers           h.end = 0;	/* just in case it's a whole packet */
4663b0f8d2eSBrian Somers         } else
4673b0f8d2eSBrian Somers           do
4683b0f8d2eSBrian Somers             frag = &(*frag)->next;
469ce828a6eSBrian Somers           while (*frag != NULL);
4703b0f8d2eSBrian Somers       } while (!h.end);
4713b0f8d2eSBrian Somers 
4723b0f8d2eSBrian Somers       if (q) {
473673903ecSBrian Somers         u_short proto;
474673903ecSBrian Somers         u_char ch;
475673903ecSBrian Somers 
476dd7e2610SBrian Somers         q = mbuf_Read(q, &ch, 1);
477673903ecSBrian Somers         proto = ch;
478673903ecSBrian Somers         if (!(proto & 1)) {
479dd7e2610SBrian Somers           q = mbuf_Read(q, &ch, 1);
480673903ecSBrian Somers           proto <<= 8;
4813b0f8d2eSBrian Somers           proto += ch;
482673903ecSBrian Somers         }
483dd7e2610SBrian Somers         if (log_IsKept(LogDEBUG))
484dd7e2610SBrian Somers           log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
485dd7e2610SBrian Somers                     first, (u_long)h.seq, mbuf_Length(q));
4863b0f8d2eSBrian Somers         hdlc_DecodePacket(mp->bundle, proto, q, &mp->link);
4873b0f8d2eSBrian Somers       }
4883b0f8d2eSBrian Somers 
489147613eaSBrian Somers       mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
4903b0f8d2eSBrian Somers       last = NULL;
4913b0f8d2eSBrian Somers       q = mp->inbufs;
4923b0f8d2eSBrian Somers     } else {
4933b0f8d2eSBrian Somers       /* Look for the next fragment */
494147613eaSBrian Somers       seq = inc_seq(mp->local_is12bit, seq);
4953b0f8d2eSBrian Somers       last = q;
4963b0f8d2eSBrian Somers       q = q->pnext;
4973b0f8d2eSBrian Somers     }
4983b0f8d2eSBrian Somers   }
4993b0f8d2eSBrian Somers 
5003b0f8d2eSBrian Somers   if (m) {
5013b0f8d2eSBrian Somers     /* We still have to find a home for our new fragment */
5023b0f8d2eSBrian Somers     last = NULL;
5033b0f8d2eSBrian Somers     for (q = mp->inbufs; q; last = q, q = q->pnext) {
5043b0f8d2eSBrian Somers       mp_ReadHeader(mp, q, &h);
5051bcced10SBrian Somers       if (isbefore(mp->local_is12bit, mh.seq, h.seq))
506147613eaSBrian Somers         break;
507147613eaSBrian Somers     }
508147613eaSBrian Somers     /* Our received fragment fits in here */
5093b0f8d2eSBrian Somers     if (last)
5103b0f8d2eSBrian Somers       last->pnext = m;
5113b0f8d2eSBrian Somers     else
5123b0f8d2eSBrian Somers       mp->inbufs = m;
5133b0f8d2eSBrian Somers     m->pnext = q;
5143b0f8d2eSBrian Somers   }
5153b0f8d2eSBrian Somers }
5163b0f8d2eSBrian Somers 
5173b0f8d2eSBrian Somers static void
518b4b280abSBrian Somers mp_Output(struct mp *mp, struct link *l, struct mbuf *m, u_int32_t begin,
519b4b280abSBrian Somers           u_int32_t end)
5203b0f8d2eSBrian Somers {
5213b0f8d2eSBrian Somers   struct mbuf *mo;
5223b0f8d2eSBrian Somers 
523ce828a6eSBrian Somers   /* Stuff an MP header on the front of our packet and send it */
524dd7e2610SBrian Somers   mo = mbuf_Alloc(4, MB_MP);
5253b0f8d2eSBrian Somers   mo->next = m;
52649052c95SBrian Somers   if (mp->peer_is12bit) {
527ce828a6eSBrian Somers     u_int16_t *seq16;
528ce828a6eSBrian Somers 
529ce828a6eSBrian Somers     seq16 = (u_int16_t *)MBUF_CTOP(mo);
5309c53a7b1SBrian Somers     *seq16 = htons((begin << 15) | (end << 14) | (u_int16_t)mp->out.seq);
5313b0f8d2eSBrian Somers     mo->cnt = 2;
5323b0f8d2eSBrian Somers   } else {
533ce828a6eSBrian Somers     u_int32_t *seq32;
534ce828a6eSBrian Somers 
535ce828a6eSBrian Somers     seq32 = (u_int32_t *)MBUF_CTOP(mo);
5369c53a7b1SBrian Somers     *seq32 = htonl((begin << 31) | (end << 30) | (u_int32_t)mp->out.seq);
5373b0f8d2eSBrian Somers     mo->cnt = 4;
5383b0f8d2eSBrian Somers   }
539dd7e2610SBrian Somers   if (log_IsKept(LogDEBUG))
5409c53a7b1SBrian Somers     log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
5419c53a7b1SBrian Somers               mp->out.seq, mbuf_Length(mo), l->name);
542147613eaSBrian Somers   mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
5433b0f8d2eSBrian Somers 
5440a1b5c9dSBrian Somers   if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, PROTO_MP, mo))
545dd7e2610SBrian Somers     hdlc_Output(l, PRI_NORMAL, PROTO_MP, mo);
5463b0f8d2eSBrian Somers }
5473b0f8d2eSBrian Somers 
5483b0f8d2eSBrian Somers int
5493b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle)
5503b0f8d2eSBrian Somers {
5513b0f8d2eSBrian Somers   struct mp *mp = &bundle->ncp.mp;
5529c53a7b1SBrian Somers   struct datalink *dl, *fdl;
553b4b280abSBrian Somers   int total, add, len, thislink, nlinks;
554b4b280abSBrian Somers   u_int32_t begin, end;
5553b0f8d2eSBrian Somers   struct mbuf *m, *mo;
5563b0f8d2eSBrian Somers 
5579c53a7b1SBrian Somers   thislink = nlinks = 0;
5589c53a7b1SBrian Somers   for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
55986b1f0d7SBrian Somers     /* Include non-open links here as mp->out.link will stay more correct */
5609c53a7b1SBrian Somers     if (!fdl) {
5619c53a7b1SBrian Somers       if (thislink == mp->out.link)
5629c53a7b1SBrian Somers         fdl = dl;
5639c53a7b1SBrian Somers       else
5649c53a7b1SBrian Somers         thislink++;
5659c53a7b1SBrian Somers     }
5669c53a7b1SBrian Somers     nlinks++;
5679c53a7b1SBrian Somers   }
5689c53a7b1SBrian Somers 
5699c53a7b1SBrian Somers   if (!fdl) {
5709c53a7b1SBrian Somers     fdl = bundle->links;
5711bc9b5baSBrian Somers     if (!fdl)
5721bc9b5baSBrian Somers       return 0;
5739c53a7b1SBrian Somers     thislink = 0;
5749c53a7b1SBrian Somers   }
5753b0f8d2eSBrian Somers 
5763b0f8d2eSBrian Somers   total = 0;
5779c53a7b1SBrian Somers   for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
5789c53a7b1SBrian Somers     if (!dl) {
5799c53a7b1SBrian Somers       dl = bundle->links;
5809c53a7b1SBrian Somers       thislink = 0;
5819c53a7b1SBrian Somers     }
5829c53a7b1SBrian Somers 
5836f384573SBrian Somers     if (dl->state != DATALINK_OPEN)
5846f384573SBrian Somers       continue;
5859c53a7b1SBrian Somers 
5863b0f8d2eSBrian Somers     if (dl->physical->out)
5873b0f8d2eSBrian Somers       /* this link has suffered a short write.  Let it continue */
5883b0f8d2eSBrian Somers       continue;
5899c53a7b1SBrian Somers 
5903b0f8d2eSBrian Somers     add = link_QueueLen(&dl->physical->link);
5913b0f8d2eSBrian Somers     total += add;
5923b0f8d2eSBrian Somers     if (add)
5933b0f8d2eSBrian Somers       /* this link has got stuff already queued.  Let it continue */
5943b0f8d2eSBrian Somers       continue;
5959c53a7b1SBrian Somers 
596dd7e2610SBrian Somers     if (!link_QueueLen(&mp->link) && !ip_FlushPacket(&mp->link, bundle))
5973b0f8d2eSBrian Somers       /* Nothing else to send */
5983b0f8d2eSBrian Somers       break;
5993b0f8d2eSBrian Somers 
6003b0f8d2eSBrian Somers     m = link_Dequeue(&mp->link);
601dd7e2610SBrian Somers     len = mbuf_Length(m);
6023b0f8d2eSBrian Somers     begin = 1;
6033b0f8d2eSBrian Somers     end = 0;
6043b0f8d2eSBrian Somers 
6059c53a7b1SBrian Somers     while (!end) {
6069c53a7b1SBrian Somers       if (dl->state == DATALINK_OPEN) {
6073b0f8d2eSBrian Somers         if (len <= dl->mp.weight + LINK_MINWEIGHT) {
6089c53a7b1SBrian Somers           /*
6099c53a7b1SBrian Somers            * XXX: Should we remember how much of our `weight' wasn't sent
6109c53a7b1SBrian Somers            *      so that we can compensate next time ?
6119c53a7b1SBrian Somers            */
6123b0f8d2eSBrian Somers           mo = m;
6133b0f8d2eSBrian Somers           end = 1;
6143b0f8d2eSBrian Somers         } else {
615dd7e2610SBrian Somers           mo = mbuf_Alloc(dl->mp.weight, MB_MP);
6163b0f8d2eSBrian Somers           mo->cnt = dl->mp.weight;
6173b0f8d2eSBrian Somers           len -= mo->cnt;
618dd7e2610SBrian Somers           m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt);
6193b0f8d2eSBrian Somers         }
6203b0f8d2eSBrian Somers         mp_Output(mp, &dl->physical->link, mo, begin, end);
6213b0f8d2eSBrian Somers         begin = 0;
6223b0f8d2eSBrian Somers       }
6239c53a7b1SBrian Somers 
6249c53a7b1SBrian Somers       if (!end) {
6259c53a7b1SBrian Somers         nlinks--;
6269c53a7b1SBrian Somers         dl = dl->next;
6279c53a7b1SBrian Somers         if (!dl) {
6289c53a7b1SBrian Somers           dl = bundle->links;
6299c53a7b1SBrian Somers           thislink = 0;
6309c53a7b1SBrian Somers         } else
6319c53a7b1SBrian Somers           thislink++;
6323b0f8d2eSBrian Somers       }
6339c53a7b1SBrian Somers     }
6349c53a7b1SBrian Somers   }
6359c53a7b1SBrian Somers   mp->out.link = thislink;		/* Start here next time */
6363b0f8d2eSBrian Somers 
6373b0f8d2eSBrian Somers   return total;
6383b0f8d2eSBrian Somers }
6393b0f8d2eSBrian Somers 
6403b0f8d2eSBrian Somers int
6413b0f8d2eSBrian Somers mp_SetDatalinkWeight(struct cmdargs const *arg)
6423b0f8d2eSBrian Somers {
6433b0f8d2eSBrian Somers   int val;
6443b0f8d2eSBrian Somers 
64525092092SBrian Somers   if (arg->argc != arg->argn+1)
6463b0f8d2eSBrian Somers     return -1;
6473b0f8d2eSBrian Somers 
64825092092SBrian Somers   val = atoi(arg->argv[arg->argn]);
6493b0f8d2eSBrian Somers   if (val < LINK_MINWEIGHT) {
650dd7e2610SBrian Somers     log_Printf(LogWARN, "Link weights must not be less than %d\n",
6513b0f8d2eSBrian Somers               LINK_MINWEIGHT);
6523b0f8d2eSBrian Somers     return 1;
6533b0f8d2eSBrian Somers   }
6543b0f8d2eSBrian Somers   arg->cx->mp.weight = val;
6553b0f8d2eSBrian Somers   return 0;
6563b0f8d2eSBrian Somers }
65749052c95SBrian Somers 
65849052c95SBrian Somers int
65949052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg)
66049052c95SBrian Somers {
66149052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
66249052c95SBrian Somers 
66349052c95SBrian Somers   prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
6649c53a7b1SBrian Somers   if (mp->active) {
6659c53a7b1SBrian Somers     struct mbuf *m;
6669c53a7b1SBrian Somers     int bufs = 0;
6679c53a7b1SBrian Somers 
6681fa665f5SBrian Somers     prompt_Printf(arg->prompt, "Socket:         %s\n",
6696f384573SBrian Somers                   mp->server.socket.sun_path);
6709c53a7b1SBrian Somers     for (m = mp->inbufs; m; m = m->pnext)
6719c53a7b1SBrian Somers       bufs++;
6729c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "Pending frags:  %d\n", bufs);
6739c53a7b1SBrian Somers   }
67449052c95SBrian Somers 
67549052c95SBrian Somers   prompt_Printf(arg->prompt, "\nMy Side:\n");
67649052c95SBrian Somers   if (mp->active) {
67749052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
67849052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
67949052c95SBrian Somers                   mp->local_is12bit ? "on" : "off");
68049052c95SBrian Somers   }
681643f4904SBrian Somers   prompt_Printf(arg->prompt, " Discriminator: %s\n",
68249052c95SBrian Somers                 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
68349052c95SBrian Somers                            mp->cfg.enddisc.len));
68449052c95SBrian Somers 
68549052c95SBrian Somers   prompt_Printf(arg->prompt, "\nHis Side:\n");
68649052c95SBrian Somers   if (mp->active) {
687643f4904SBrian Somers     prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
6889c53a7b1SBrian Somers     prompt_Printf(arg->prompt, " Next SEQ:      %u\n", mp->out.seq);
68949052c95SBrian Somers     prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
69049052c95SBrian Somers     prompt_Printf(arg->prompt, " Short Seq:     %s\n",
69149052c95SBrian Somers                   mp->peer_is12bit ? "on" : "off");
69249052c95SBrian Somers   }
693643f4904SBrian Somers   prompt_Printf(arg->prompt,   " Discriminator: %s\n",
694643f4904SBrian Somers                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
695643f4904SBrian Somers                            mp->peer.enddisc.len));
69649052c95SBrian Somers 
69749052c95SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
69849052c95SBrian Somers 
69949052c95SBrian Somers   prompt_Printf(arg->prompt, " MRRU:          ");
70049052c95SBrian Somers   if (mp->cfg.mrru)
70149052c95SBrian Somers     prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
70249052c95SBrian Somers   else
70349052c95SBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
70449052c95SBrian Somers   prompt_Printf(arg->prompt, " Short Seq:     %s\n",
70549052c95SBrian Somers                   command_ShowNegval(mp->cfg.shortseq));
70649052c95SBrian Somers 
70749052c95SBrian Somers   return 0;
70849052c95SBrian Somers }
70949052c95SBrian Somers 
71049052c95SBrian Somers const char *
71149052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len)
71249052c95SBrian Somers {
713d93d3a9cSBrian Somers   static char result[100];	/* Used immediately after it's returned */
71449052c95SBrian Somers   int f, header;
71549052c95SBrian Somers 
71649052c95SBrian Somers   switch (c) {
71708676022SBrian Somers     case ENDDISC_NULL:
71849052c95SBrian Somers       sprintf(result, "Null Class");
71949052c95SBrian Somers       break;
72049052c95SBrian Somers 
72108676022SBrian Somers     case ENDDISC_LOCAL:
72249052c95SBrian Somers       snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
72349052c95SBrian Somers       break;
72449052c95SBrian Somers 
72508676022SBrian Somers     case ENDDISC_IP:
72649052c95SBrian Somers       if (len == 4)
72749052c95SBrian Somers         snprintf(result, sizeof result, "IP %s",
72849052c95SBrian Somers                  inet_ntoa(*(const struct in_addr *)address));
72949052c95SBrian Somers       else
73049052c95SBrian Somers         sprintf(result, "IP[%d] ???", len);
73149052c95SBrian Somers       break;
73249052c95SBrian Somers 
73308676022SBrian Somers     case ENDDISC_MAC:
73449052c95SBrian Somers       if (len == 6) {
73549052c95SBrian Somers         const u_char *m = (const u_char *)address;
73649052c95SBrian Somers         snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
73749052c95SBrian Somers                  m[0], m[1], m[2], m[3], m[4], m[5]);
73849052c95SBrian Somers       } else
73949052c95SBrian Somers         sprintf(result, "MAC[%d] ???", len);
74049052c95SBrian Somers       break;
74149052c95SBrian Somers 
74208676022SBrian Somers     case ENDDISC_MAGIC:
74349052c95SBrian Somers       sprintf(result, "Magic: 0x");
74449052c95SBrian Somers       header = strlen(result);
74549052c95SBrian Somers       if (len > sizeof result - header - 1)
74649052c95SBrian Somers         len = sizeof result - header - 1;
74749052c95SBrian Somers       for (f = 0; f < len; f++)
74849052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
74949052c95SBrian Somers       break;
75049052c95SBrian Somers 
75108676022SBrian Somers     case ENDDISC_PSN:
75249052c95SBrian Somers       snprintf(result, sizeof result, "PSN: %.*s", len, address);
75349052c95SBrian Somers       break;
75449052c95SBrian Somers 
75549052c95SBrian Somers      default:
75649052c95SBrian Somers       sprintf(result, "%d: ", (int)c);
75749052c95SBrian Somers       header = strlen(result);
75849052c95SBrian Somers       if (len > sizeof result - header - 1)
75949052c95SBrian Somers         len = sizeof result - header - 1;
76049052c95SBrian Somers       for (f = 0; f < len; f++)
76149052c95SBrian Somers         sprintf(result + header + 2 * f, "%02x", address[f]);
76249052c95SBrian Somers       break;
76349052c95SBrian Somers   }
76449052c95SBrian Somers   return result;
76549052c95SBrian Somers }
76649052c95SBrian Somers 
76749052c95SBrian Somers int
76849052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg)
76949052c95SBrian Somers {
77049052c95SBrian Somers   struct mp *mp = &arg->bundle->ncp.mp;
771d47dceb8SBrian Somers   struct in_addr addr;
77249052c95SBrian Somers 
77349052c95SBrian Somers   if (bundle_Phase(arg->bundle) != PHASE_DEAD) {
774dd7e2610SBrian Somers     log_Printf(LogWARN, "set enddisc: Only available at phase DEAD\n");
77549052c95SBrian Somers     return 1;
77649052c95SBrian Somers   }
77749052c95SBrian Somers 
77849052c95SBrian Somers   if (arg->argc == arg->argn) {
77949052c95SBrian Somers     mp->cfg.enddisc.class = 0;
78049052c95SBrian Somers     *mp->cfg.enddisc.address = '\0';
78149052c95SBrian Somers     mp->cfg.enddisc.len = 0;
782e43ebac1SBrian Somers   } else if (arg->argc > arg->argn) {
78308676022SBrian Somers     if (!strcasecmp(arg->argv[arg->argn], "label")) {
78408676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_LOCAL;
78508676022SBrian Somers       strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
78608676022SBrian Somers       mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
78708676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
788259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
789259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
790d47dceb8SBrian Somers       else
791259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
792d47dceb8SBrian Somers       memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
79308676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_IP;
79449052c95SBrian Somers       mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
79508676022SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
79608676022SBrian Somers       struct sockaddr_dl hwaddr;
79708676022SBrian Somers       int s;
79808676022SBrian Somers 
799259a6191SBrian Somers       if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
800259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
801d47dceb8SBrian Somers       else
802259a6191SBrian Somers         addr = arg->bundle->ncp.ipcp.my_ip;
803d47dceb8SBrian Somers 
80408676022SBrian Somers       s = ID0socket(AF_INET, SOCK_DGRAM, 0);
80508676022SBrian Somers       if (s < 0) {
806dd7e2610SBrian Somers         log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
80708676022SBrian Somers         return 2;
80808676022SBrian Somers       }
809d47dceb8SBrian Somers       if (get_ether_addr(s, addr, &hwaddr)) {
81008676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_MAC;
81108676022SBrian Somers         memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
81208676022SBrian Somers                hwaddr.sdl_alen);
81308676022SBrian Somers         mp->cfg.enddisc.len = hwaddr.sdl_alen;
81408676022SBrian Somers       } else {
815dd7e2610SBrian Somers         log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
816259a6191SBrian Somers                   inet_ntoa(addr));
81708676022SBrian Somers         close(s);
81808676022SBrian Somers         return 4;
81908676022SBrian Somers       }
82008676022SBrian Somers       close(s);
82149052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
82249052c95SBrian Somers       int f;
82349052c95SBrian Somers 
82449052c95SBrian Somers       randinit();
82549052c95SBrian Somers       for (f = 0; f < 20; f += sizeof(long))
82649052c95SBrian Somers         *(long *)(mp->cfg.enddisc.address + f) = random();
82708676022SBrian Somers       mp->cfg.enddisc.class = ENDDISC_MAGIC;
82849052c95SBrian Somers       mp->cfg.enddisc.len = 20;
82949052c95SBrian Somers     } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
83049052c95SBrian Somers       if (arg->argc > arg->argn+1) {
83108676022SBrian Somers         mp->cfg.enddisc.class = ENDDISC_PSN;
83249052c95SBrian Somers         strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
83349052c95SBrian Somers         mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
83449052c95SBrian Somers       } else {
835dd7e2610SBrian Somers         log_Printf(LogWARN, "PSN endpoint requires additional data\n");
83608676022SBrian Somers         return 5;
83749052c95SBrian Somers       }
83849052c95SBrian Somers     } else {
839dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
84049052c95SBrian Somers                 arg->argv[arg->argn]);
84108676022SBrian Somers       return 6;
84249052c95SBrian Somers     }
843e43ebac1SBrian Somers   }
84449052c95SBrian Somers 
84549052c95SBrian Somers   return 0;
84649052c95SBrian Somers }
8471fa665f5SBrian Somers 
8481fa665f5SBrian Somers static int
8491fa665f5SBrian Somers mpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
8501fa665f5SBrian Somers                    int *n)
8511fa665f5SBrian Somers {
8521fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
8530f2f3eb3SBrian Somers   int result;
8541fa665f5SBrian Somers 
8550f2f3eb3SBrian Somers   result = 0;
8566f384573SBrian Somers   if (s->send.dl != NULL) {
85796c9bb21SBrian Somers     /* We've connect()ed */
858b7c5748eSBrian Somers     if (!link_QueueLen(&s->send.dl->physical->link) &&
859b7c5748eSBrian Somers         !s->send.dl->physical->out) {
860b7c5748eSBrian Somers       /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
8610f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
86296c9bb21SBrian Somers       bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
8636f384573SBrian Somers       s->send.dl = NULL;
86496c9bb21SBrian Somers       s->fd = -1;
865ea722969SBrian Somers     } else
866ea722969SBrian Somers       /* Never read from a datalink that's on death row ! */
8670f2f3eb3SBrian Somers       result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
86896c9bb21SBrian Somers   } else if (r && s->fd >= 0) {
8691fa665f5SBrian Somers     if (*n < s->fd + 1)
8701fa665f5SBrian Somers       *n = s->fd + 1;
8711fa665f5SBrian Somers     FD_SET(s->fd, r);
87224989c68SBrian Somers     log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
8730f2f3eb3SBrian Somers     result++;
8741fa665f5SBrian Somers   }
8750f2f3eb3SBrian Somers   return result;
8761fa665f5SBrian Somers }
8771fa665f5SBrian Somers 
8781fa665f5SBrian Somers static int
8791fa665f5SBrian Somers mpserver_IsSet(struct descriptor *d, const fd_set *fdset)
8801fa665f5SBrian Somers {
8811fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
8821fa665f5SBrian Somers   return s->fd >= 0 && FD_ISSET(s->fd, fdset);
8831fa665f5SBrian Somers }
8841fa665f5SBrian Somers 
8851fa665f5SBrian Somers static void
8861fa665f5SBrian Somers mpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
8871fa665f5SBrian Somers {
8881fa665f5SBrian Somers   struct mpserver *s = descriptor2mpserver(d);
8896f384573SBrian Somers   struct sockaddr in;
8901fa665f5SBrian Somers   int fd, size;
8911fa665f5SBrian Somers 
8926f384573SBrian Somers   size = sizeof in;
8936f384573SBrian Somers   fd = accept(s->fd, &in, &size);
8941fa665f5SBrian Somers   if (fd < 0) {
895dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno));
8961fa665f5SBrian Somers     return;
8971fa665f5SBrian Somers   }
8981fa665f5SBrian Somers 
8991bc9b5baSBrian Somers   if (in.sa_family == AF_LOCAL)
90096c9bb21SBrian Somers     bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in);
90154cd8e13SBrian Somers   else
9021fa665f5SBrian Somers     close(fd);
9031fa665f5SBrian Somers }
9041fa665f5SBrian Somers 
9051fa665f5SBrian Somers static void
9061fa665f5SBrian Somers mpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
9071fa665f5SBrian Somers {
9081fa665f5SBrian Somers   /* We never want to write here ! */
909dd7e2610SBrian Somers   log_Printf(LogERROR, "mpserver_Write: Internal error: Bad call !\n");
9101fa665f5SBrian Somers }
9111fa665f5SBrian Somers 
9121fa665f5SBrian Somers void
9131fa665f5SBrian Somers mpserver_Init(struct mpserver *s)
9141fa665f5SBrian Somers {
9151fa665f5SBrian Somers   s->desc.type = MPSERVER_DESCRIPTOR;
9161fa665f5SBrian Somers   s->desc.UpdateSet = mpserver_UpdateSet;
9171fa665f5SBrian Somers   s->desc.IsSet = mpserver_IsSet;
9181fa665f5SBrian Somers   s->desc.Read = mpserver_Read;
9191fa665f5SBrian Somers   s->desc.Write = mpserver_Write;
9206f384573SBrian Somers   s->send.dl = NULL;
9211fa665f5SBrian Somers   s->fd = -1;
9226f384573SBrian Somers   memset(&s->socket, '\0', sizeof s->socket);
9231fa665f5SBrian Somers }
9241fa665f5SBrian Somers 
9251fa665f5SBrian Somers int
9261fa665f5SBrian Somers mpserver_Open(struct mpserver *s, struct peerid *peer)
9271fa665f5SBrian Somers {
92896c9bb21SBrian Somers   int f, l;
9291fa665f5SBrian Somers   mode_t mask;
9301fa665f5SBrian Somers 
9311fa665f5SBrian Somers   if (s->fd != -1) {
932dd7e2610SBrian Somers     log_Printf(LogERROR, "Internal error !  mpserver already open\n");
9336f384573SBrian Somers     mpserver_Close(s);
9341fa665f5SBrian Somers   }
9351fa665f5SBrian Somers 
9366f384573SBrian Somers   l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
9376f384573SBrian Somers                _PATH_VARRUN, peer->authname, peer->enddisc.class);
9381fa665f5SBrian Somers 
9396f384573SBrian Somers   for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
9406f384573SBrian Somers     snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
9411fa665f5SBrian Somers              "%02x", *(u_char *)(peer->enddisc.address+f));
9426f384573SBrian Somers     l += 2;
9431fa665f5SBrian Somers   }
9441fa665f5SBrian Somers 
9456f384573SBrian Somers   s->socket.sun_family = AF_LOCAL;
9466f384573SBrian Somers   s->socket.sun_len = sizeof s->socket;
9471fa665f5SBrian Somers   s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
9481fa665f5SBrian Somers   if (s->fd < 0) {
949dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno));
95096c9bb21SBrian Somers     return MPSERVER_FAILED;
9511fa665f5SBrian Somers   }
9521fa665f5SBrian Somers 
9536f384573SBrian Somers   setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
9546f384573SBrian Somers              sizeof s->socket);
9551fa665f5SBrian Somers   mask = umask(0177);
9566f384573SBrian Somers   if (ID0bind_un(s->fd, &s->socket) < 0) {
9576f384573SBrian Somers     if (errno != EADDRINUSE) {
958dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
9596f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
9601fa665f5SBrian Somers       umask(mask);
9611fa665f5SBrian Somers       close(s->fd);
9621fa665f5SBrian Somers       s->fd = -1;
96396c9bb21SBrian Somers       return MPSERVER_FAILED;
9646f384573SBrian Somers     }
9656f384573SBrian Somers     umask(mask);
9666f384573SBrian Somers     if (ID0connect_un(s->fd, &s->socket) < 0) {
967dd7e2610SBrian Somers       log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
9686f384573SBrian Somers                 s->socket.sun_path, strerror(errno));
9696f384573SBrian Somers       if (errno == ECONNREFUSED)
970dd7e2610SBrian Somers         log_Printf(LogPHASE, "          Has the previous server died badly ?\n");
9716f384573SBrian Somers       close(s->fd);
9726f384573SBrian Somers       s->fd = -1;
97396c9bb21SBrian Somers       return MPSERVER_FAILED;
9746f384573SBrian Somers     }
9756f384573SBrian Somers 
9766f384573SBrian Somers     /* Donate our link to the other guy */
97796c9bb21SBrian Somers     return MPSERVER_CONNECTED;
9781fa665f5SBrian Somers   }
9796f384573SBrian Somers 
9806f384573SBrian Somers   /* Listen for other ppp invocations that want to donate links */
9811fa665f5SBrian Somers   if (listen(s->fd, 5) != 0) {
982dd7e2610SBrian Somers     log_Printf(LogERROR, "mpserver: Unable to listen to socket"
9831fa665f5SBrian Somers               " - BUNDLE overload?\n");
9841fa665f5SBrian Somers     mpserver_Close(s);
9851fa665f5SBrian Somers   }
9861fa665f5SBrian Somers 
98796c9bb21SBrian Somers   return MPSERVER_LISTENING;
9881fa665f5SBrian Somers }
9891fa665f5SBrian Somers 
9901fa665f5SBrian Somers void
9911fa665f5SBrian Somers mpserver_Close(struct mpserver *s)
9921fa665f5SBrian Somers {
9936f384573SBrian Somers   if (s->send.dl != NULL) {
99496c9bb21SBrian Somers     bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
9956f384573SBrian Somers     s->send.dl = NULL;
99696c9bb21SBrian Somers     s->fd = -1;
99796c9bb21SBrian Somers   } else if (s->fd >= 0) {
9981fa665f5SBrian Somers     close(s->fd);
9996f384573SBrian Somers     if (ID0unlink(s->socket.sun_path) == -1)
1000dd7e2610SBrian Somers       log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
10016f384573SBrian Somers                 strerror(errno));
10026f384573SBrian Somers     memset(&s->socket, '\0', sizeof s->socket);
10031fa665f5SBrian Somers     s->fd = -1;
10041fa665f5SBrian Somers   }
10051fa665f5SBrian Somers }
100686b1f0d7SBrian Somers 
100786b1f0d7SBrian Somers void
100886b1f0d7SBrian Somers mp_LinkLost(struct mp *mp, struct datalink *dl)
100986b1f0d7SBrian Somers {
101086b1f0d7SBrian Somers   if (mp->seq.min_in == dl->mp.seq)
101186b1f0d7SBrian Somers     /* We've lost the link that's holding everything up ! */
101286b1f0d7SBrian Somers     mp_Input(mp, NULL, NULL);
101386b1f0d7SBrian Somers }
1014