mp.c revision 38174
136285Sbrian/*-
236285Sbrian * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
336285Sbrian * All rights reserved.
436285Sbrian *
536285Sbrian * Redistribution and use in source and binary forms, with or without
636285Sbrian * modification, are permitted provided that the following conditions
736285Sbrian * are met:
836285Sbrian * 1. Redistributions of source code must retain the above copyright
936285Sbrian *    notice, this list of conditions and the following disclaimer.
1036285Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1136285Sbrian *    notice, this list of conditions and the following disclaimer in the
1236285Sbrian *    documentation and/or other materials provided with the distribution.
1336285Sbrian *
1436285Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1536285Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1636285Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1736285Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1836285Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1936285Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2036285Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2136285Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2236285Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2336285Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2436285Sbrian * SUCH DAMAGE.
2536285Sbrian *
2638174Sbrian *	$Id: mp.c,v 1.12 1998/06/30 23:04:17 brian Exp $
2736285Sbrian */
2836285Sbrian
2936285Sbrian#include <sys/types.h>
3036285Sbrian#include <netinet/in.h>
3136285Sbrian#include <netinet/in_systm.h>
3236285Sbrian#include <netinet/ip.h>
3336285Sbrian#include <arpa/inet.h>
3436285Sbrian#include <net/if_dl.h>
3536285Sbrian#include <sys/socket.h>
3636285Sbrian#include <sys/un.h>
3736285Sbrian
3836285Sbrian#include <errno.h>
3936285Sbrian#include <paths.h>
4036285Sbrian#include <stdlib.h>
4136285Sbrian#include <stdio.h>
4236285Sbrian#include <string.h>
4336285Sbrian#include <sys/stat.h>
4436285Sbrian#include <termios.h>
4536285Sbrian#include <unistd.h>
4636285Sbrian
4737009Sbrian#include "defs.h"
4836285Sbrian#include "command.h"
4936285Sbrian#include "mbuf.h"
5036285Sbrian#include "log.h"
5136285Sbrian#include "timer.h"
5236285Sbrian#include "fsm.h"
5336285Sbrian#include "iplist.h"
5436285Sbrian#include "throughput.h"
5536285Sbrian#include "slcompress.h"
5636285Sbrian#include "ipcp.h"
5736285Sbrian#include "auth.h"
5836285Sbrian#include "lcp.h"
5936285Sbrian#include "lqr.h"
6036285Sbrian#include "hdlc.h"
6136285Sbrian#include "async.h"
6236285Sbrian#include "ccp.h"
6336285Sbrian#include "link.h"
6436285Sbrian#include "descriptor.h"
6536285Sbrian#include "physical.h"
6636285Sbrian#include "chat.h"
6736285Sbrian#include "lcpproto.h"
6836285Sbrian#include "filter.h"
6936285Sbrian#include "mp.h"
7036285Sbrian#include "chap.h"
7138174Sbrian#include "cbcp.h"
7236285Sbrian#include "datalink.h"
7336285Sbrian#include "bundle.h"
7436285Sbrian#include "ip.h"
7536285Sbrian#include "prompt.h"
7636285Sbrian#include "id.h"
7736285Sbrian#include "arp.h"
7836285Sbrian
7936285Sbrianvoid
8036285Sbrianpeerid_Init(struct peerid *peer)
8136285Sbrian{
8236285Sbrian  peer->enddisc.class = 0;
8336285Sbrian  *peer->enddisc.address = '\0';
8436285Sbrian  peer->enddisc.len = 0;
8536285Sbrian  *peer->authname = '\0';
8636285Sbrian}
8736285Sbrian
8836285Sbrianint
8936285Sbrianpeerid_Equal(const struct peerid *p1, const struct peerid *p2)
9036285Sbrian{
9136285Sbrian  return !strcmp(p1->authname, p2->authname) &&
9236285Sbrian         p1->enddisc.class == p2->enddisc.class &&
9336285Sbrian         p1->enddisc.len == p2->enddisc.len &&
9436285Sbrian         !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
9536285Sbrian}
9636285Sbrian
9736285Sbrianstatic u_int32_t
9836285Sbrianinc_seq(unsigned is12bit, u_int32_t seq)
9936285Sbrian{
10036285Sbrian  seq++;
10136285Sbrian  if (is12bit) {
10236285Sbrian    if (seq & 0xfffff000)
10336285Sbrian      seq = 0;
10436285Sbrian  } else if (seq & 0xff000000)
10536285Sbrian    seq = 0;
10636285Sbrian  return seq;
10736285Sbrian}
10836285Sbrian
10936285Sbrianstatic int
11036285Sbrianisbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
11136285Sbrian{
11236285Sbrian  u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
11336285Sbrian
11436285Sbrian  if (seq1 > max) {
11536285Sbrian    if (seq2 < 0x200 || seq2 > seq1)
11636285Sbrian      return 1;
11736285Sbrian  } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
11836285Sbrian    return 1;
11936285Sbrian
12036285Sbrian  return 0;
12136285Sbrian}
12236285Sbrian
12336285Sbrianstatic int
12436285Sbrianmp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
12536285Sbrian{
12636285Sbrian  if (mp->local_is12bit) {
12736285Sbrian    header->seq = ntohs(*(u_int16_t *)MBUF_CTOP(m));
12836285Sbrian    if (header->seq & 0x3000) {
12936285Sbrian      log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
13036285Sbrian      return 0;
13136285Sbrian    }
13236285Sbrian    header->begin = header->seq & 0x8000 ? 1 : 0;
13336285Sbrian    header->end = header->seq & 0x4000 ? 1 : 0;
13436285Sbrian    header->seq &= 0x0fff;
13536285Sbrian    return 2;
13636285Sbrian  } else {
13736285Sbrian    header->seq = ntohl(*(u_int32_t *)MBUF_CTOP(m));
13836285Sbrian    if (header->seq & 0x3f000000) {
13936285Sbrian      log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
14036285Sbrian      return 0;
14136285Sbrian    }
14236285Sbrian    header->begin = header->seq & 0x80000000 ? 1 : 0;
14336285Sbrian    header->end = header->seq & 0x40000000 ? 1 : 0;
14436285Sbrian    header->seq &= 0x00ffffff;
14536285Sbrian    return 4;
14636285Sbrian  }
14736285Sbrian}
14836285Sbrian
14936285Sbrianstatic void
15036285Sbrianmp_LayerStart(void *v, struct fsm *fp)
15136285Sbrian{
15236285Sbrian  /* The given FSM (ccp) is about to start up ! */
15336285Sbrian}
15436285Sbrian
15536285Sbrianstatic void
15636285Sbrianmp_LayerUp(void *v, struct fsm *fp)
15736285Sbrian{
15836285Sbrian  /* The given fsm (ccp) is now up */
15936285Sbrian}
16036285Sbrian
16136285Sbrianstatic void
16236285Sbrianmp_LayerDown(void *v, struct fsm *fp)
16336285Sbrian{
16436285Sbrian  /* The given FSM (ccp) has been told to come down */
16536285Sbrian}
16636285Sbrian
16736285Sbrianstatic void
16836285Sbrianmp_LayerFinish(void *v, struct fsm *fp)
16936285Sbrian{
17036285Sbrian  /* The given fsm (ccp) is now down */
17136285Sbrian  if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
17236285Sbrian    fsm_Open(fp);		/* CCP goes to ST_STOPPED */
17336285Sbrian}
17436285Sbrian
17536285Sbrianvoid
17636285Sbrianmp_Init(struct mp *mp, struct bundle *bundle)
17736285Sbrian{
17836285Sbrian  mp->peer_is12bit = mp->local_is12bit = 0;
17936285Sbrian  mp->peer_mrru = mp->local_mrru = 0;
18036285Sbrian
18136285Sbrian  peerid_Init(&mp->peer);
18236285Sbrian
18336285Sbrian  mp->out.seq = 0;
18436285Sbrian  mp->out.link = 0;
18536285Sbrian  mp->seq.min_in = 0;
18636285Sbrian  mp->seq.next_in = 0;
18736285Sbrian  mp->inbufs = NULL;
18836285Sbrian  mp->bundle = bundle;
18936285Sbrian
19036285Sbrian  mp->link.type = MP_LINK;
19136285Sbrian  mp->link.name = "mp";
19236285Sbrian  mp->link.len = sizeof *mp;
19336285Sbrian
19436285Sbrian  throughput_init(&mp->link.throughput);
19536285Sbrian  memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
19636285Sbrian  memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
19736285Sbrian  memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
19836285Sbrian
19936285Sbrian  mp->fsmp.LayerStart = mp_LayerStart;
20036285Sbrian  mp->fsmp.LayerUp = mp_LayerUp;
20136285Sbrian  mp->fsmp.LayerDown = mp_LayerDown;
20236285Sbrian  mp->fsmp.LayerFinish = mp_LayerFinish;
20336285Sbrian  mp->fsmp.object = mp;
20436285Sbrian
20536285Sbrian  mpserver_Init(&mp->server);
20636285Sbrian
20736285Sbrian  mp->cfg.mrru = 0;
20836285Sbrian  mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
20936285Sbrian  mp->cfg.enddisc.class = 0;
21036285Sbrian  *mp->cfg.enddisc.address = '\0';
21136285Sbrian  mp->cfg.enddisc.len = 0;
21236285Sbrian
21336285Sbrian  lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
21436285Sbrian  ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
21536285Sbrian}
21636285Sbrian
21736285Sbrianint
21836285Sbrianmp_Up(struct mp *mp, struct datalink *dl)
21936285Sbrian{
22036285Sbrian  struct lcp *lcp = &dl->physical->link.lcp;
22136285Sbrian
22236285Sbrian  if (mp->active) {
22336285Sbrian    /* We're adding a link - do a last validation on our parameters */
22436285Sbrian    if (!peerid_Equal(&dl->peer, &mp->peer)) {
22536285Sbrian      log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
22636285Sbrian      return MP_FAILED;
22736285Sbrian    }
22836285Sbrian    if (mp->local_mrru != lcp->want_mrru ||
22936285Sbrian        mp->peer_mrru != lcp->his_mrru ||
23036285Sbrian        mp->local_is12bit != lcp->want_shortseq ||
23136285Sbrian        mp->peer_is12bit != lcp->his_shortseq) {
23236285Sbrian      log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
23336285Sbrian                dl->name);
23436285Sbrian      return MP_FAILED;
23536285Sbrian    }
23636285Sbrian    return MP_ADDED;
23736285Sbrian  } else {
23836285Sbrian    /* First link in multilink mode */
23936285Sbrian
24036285Sbrian    mp->local_mrru = lcp->want_mrru;
24136285Sbrian    mp->peer_mrru = lcp->his_mrru;
24236285Sbrian    mp->local_is12bit = lcp->want_shortseq;
24336285Sbrian    mp->peer_is12bit = lcp->his_shortseq;
24436285Sbrian    mp->peer = dl->peer;
24536285Sbrian
24636285Sbrian    throughput_init(&mp->link.throughput);
24736285Sbrian    memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
24836285Sbrian    memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
24936285Sbrian    memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
25036285Sbrian
25136285Sbrian    mp->out.seq = 0;
25236285Sbrian    mp->out.link = 0;
25336285Sbrian    mp->seq.min_in = 0;
25436285Sbrian    mp->seq.next_in = 0;
25536285Sbrian
25636285Sbrian    /*
25736285Sbrian     * Now we create our server socket.
25836285Sbrian     * If it already exists, join it.  Otherwise, create and own it
25936285Sbrian     */
26036285Sbrian    switch (mpserver_Open(&mp->server, &mp->peer)) {
26136285Sbrian    case MPSERVER_CONNECTED:
26236285Sbrian      log_Printf(LogPHASE, "mp: Transfer link on %s\n",
26336285Sbrian                mp->server.socket.sun_path);
26436285Sbrian      mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
26536285Sbrian      return MP_LINKSENT;
26636285Sbrian    case MPSERVER_FAILED:
26736285Sbrian      return MP_FAILED;
26836285Sbrian    case MPSERVER_LISTENING:
26936285Sbrian      log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
27036285Sbrian      log_Printf(LogPHASE, "    First link: %s\n", dl->name);
27136285Sbrian
27236285Sbrian      /* Re-point our IPCP layer at our MP link */
27336285Sbrian      ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
27436285Sbrian
27536285Sbrian      /* Our lcp's already up 'cos of the NULL parent */
27637320Sbrian      if (ccp_SetOpenMode(&mp->link.ccp)) {
27737320Sbrian        fsm_Up(&mp->link.ccp.fsm);
27837320Sbrian        fsm_Open(&mp->link.ccp.fsm);
27937320Sbrian      }
28036285Sbrian
28136285Sbrian      mp->active = 1;
28236285Sbrian      break;
28336285Sbrian    }
28436285Sbrian  }
28536285Sbrian
28636285Sbrian  return MP_UP;
28736285Sbrian}
28836285Sbrian
28936285Sbrianvoid
29036285Sbrianmp_Down(struct mp *mp)
29136285Sbrian{
29236285Sbrian  if (mp->active) {
29336285Sbrian    struct mbuf *next;
29436285Sbrian
29536285Sbrian    /* Don't want any more of these */
29636285Sbrian    mpserver_Close(&mp->server);
29736285Sbrian
29836285Sbrian    /* CCP goes down with a bang */
29937060Sbrian    fsm2initial(&mp->link.ccp.fsm);
30036285Sbrian
30136285Sbrian    /* Received fragments go in the bit-bucket */
30236285Sbrian    while (mp->inbufs) {
30336285Sbrian      next = mp->inbufs->pnext;
30436285Sbrian      mbuf_Free(mp->inbufs);
30536285Sbrian      mp->inbufs = next;
30636285Sbrian    }
30736285Sbrian
30836285Sbrian    peerid_Init(&mp->peer);
30936285Sbrian    mp->active = 0;
31036285Sbrian  }
31136285Sbrian}
31236285Sbrian
31336285Sbrianvoid
31436285Sbrianmp_linkInit(struct mp_link *mplink)
31536285Sbrian{
31636285Sbrian  mplink->seq = 0;
31736285Sbrian  mplink->weight = 1500;
31836285Sbrian}
31936285Sbrian
32036285Sbrianvoid
32136285Sbrianmp_Input(struct mp *mp, struct mbuf *m, struct physical *p)
32236285Sbrian{
32336285Sbrian  struct mp_header mh, h;
32436285Sbrian  struct mbuf *q, *last;
32536285Sbrian  int32_t seq;
32636285Sbrian
32736312Sbrian  /*
32836312Sbrian   * When `m' and `p' are NULL, it means our oldest link has gone down.
32936312Sbrian   * We want to determine a new min, and process any intermediate stuff
33036312Sbrian   * as normal
33136312Sbrian   */
33236312Sbrian
33336312Sbrian  if (m && mp_ReadHeader(mp, m, &mh) == 0) {
33436285Sbrian    mbuf_Free(m);
33536285Sbrian    return;
33636285Sbrian  }
33736285Sbrian
33836312Sbrian  if (p) {
33936312Sbrian    seq = p->dl->mp.seq;
34036312Sbrian    p->dl->mp.seq = mh.seq;
34136312Sbrian  } else
34236312Sbrian    seq = mp->seq.min_in;
34336312Sbrian
34436285Sbrian  if (mp->seq.min_in == seq) {
34536285Sbrian    /*
34636285Sbrian     * We've received new data on the link that has our min (oldest) seq.
34736285Sbrian     * Figure out which link now has the smallest (oldest) seq.
34836285Sbrian     */
34936285Sbrian    struct datalink *dl;
35036285Sbrian
35136312Sbrian    mp->seq.min_in = (u_int32_t)-1;
35236285Sbrian    for (dl = mp->bundle->links; dl; dl = dl->next)
35336312Sbrian      if (dl->state == DATALINK_OPEN &&
35436312Sbrian          (mp->seq.min_in == -1 ||
35536312Sbrian           isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
35636285Sbrian        mp->seq.min_in = dl->mp.seq;
35736285Sbrian  }
35836285Sbrian
35936285Sbrian  /*
36036285Sbrian   * Now process as many of our fragments as we can, adding our new
36136285Sbrian   * fragment in as we go, and ordering with the oldest at the top of
36236285Sbrian   * the queue.
36336285Sbrian   */
36436285Sbrian
36536285Sbrian  if (!mp->inbufs) {
36636285Sbrian    mp->inbufs = m;
36736285Sbrian    m = NULL;
36836285Sbrian  }
36936285Sbrian
37036285Sbrian  last = NULL;
37136285Sbrian  seq = mp->seq.next_in;
37236285Sbrian  q = mp->inbufs;
37336285Sbrian  while (q) {
37436285Sbrian    mp_ReadHeader(mp, q, &h);
37536285Sbrian    if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
37636285Sbrian      /* Our received fragment fits in before this one, so link it in */
37736285Sbrian      if (last)
37836285Sbrian        last->pnext = m;
37936285Sbrian      else
38036285Sbrian        mp->inbufs = m;
38136285Sbrian      m->pnext = q;
38236285Sbrian      q = m;
38336285Sbrian      h = mh;
38436285Sbrian      m = NULL;
38536285Sbrian    }
38636285Sbrian
38736285Sbrian    if (h.seq != seq) {
38836285Sbrian      /* we're missing something :-( */
38936285Sbrian      if (mp->seq.min_in > seq) {
39036285Sbrian        /* we're never gonna get it */
39136285Sbrian        struct mbuf *next;
39236285Sbrian
39336285Sbrian        /* Zap all older fragments */
39436285Sbrian        while (mp->inbufs != q) {
39536285Sbrian          log_Printf(LogDEBUG, "Drop frag\n");
39636285Sbrian          next = mp->inbufs->pnext;
39736285Sbrian          mbuf_Free(mp->inbufs);
39836285Sbrian          mp->inbufs = next;
39936285Sbrian        }
40036285Sbrian
40136285Sbrian        /*
40236285Sbrian         * Zap everything until the next `end' fragment OR just before
40336285Sbrian         * the next `begin' fragment OR 'till seq.min_in - whichever
40436285Sbrian         * comes first.
40536285Sbrian         */
40636285Sbrian        do {
40736285Sbrian          mp_ReadHeader(mp, mp->inbufs, &h);
40836285Sbrian          if (h.begin) {
40936285Sbrian            /* We might be able to process this ! */
41036285Sbrian            h.seq--;  /* We're gonna look for fragment with h.seq+1 */
41136285Sbrian            break;
41236285Sbrian          }
41336285Sbrian          next = mp->inbufs->pnext;
41436285Sbrian          log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
41536285Sbrian          mbuf_Free(mp->inbufs);
41636285Sbrian          mp->inbufs = next;
41736285Sbrian        } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end));
41836285Sbrian
41936285Sbrian        /*
42036285Sbrian         * Continue processing things from here.
42136285Sbrian         * This deals with the possibility that we received a fragment
42236285Sbrian         * on the slowest link that invalidates some of our data (because
42336285Sbrian         * of the hole at `q'), but where there are subsequent `whole'
42436285Sbrian         * packets that have already been received.
42536285Sbrian         */
42636285Sbrian
42736285Sbrian        mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
42836285Sbrian        last = NULL;
42936285Sbrian        q = mp->inbufs;
43036285Sbrian      } else
43136285Sbrian        /* we may still receive the missing fragment */
43236285Sbrian        break;
43336285Sbrian    } else if (h.end) {
43436285Sbrian      /* We've got something, reassemble */
43536285Sbrian      struct mbuf **frag = &q;
43636285Sbrian      int len;
43736285Sbrian      u_long first = -1;
43836285Sbrian
43936285Sbrian      do {
44036285Sbrian        *frag = mp->inbufs;
44136285Sbrian        mp->inbufs = mp->inbufs->pnext;
44236285Sbrian        len = mp_ReadHeader(mp, *frag, &h);
44336285Sbrian        if (first == -1)
44436285Sbrian          first = h.seq;
44536285Sbrian        (*frag)->offset += len;
44636285Sbrian        (*frag)->cnt -= len;
44736285Sbrian        (*frag)->pnext = NULL;
44836285Sbrian        if (frag == &q && !h.begin) {
44936285Sbrian          log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
45036285Sbrian                    (u_long)h.seq);
45136285Sbrian          mbuf_Free(q);
45236285Sbrian          q = NULL;
45336285Sbrian        } else if (frag != &q && h.begin) {
45436285Sbrian          log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
45536285Sbrian                    (u_long)h.seq - 1);
45636285Sbrian          /*
45736285Sbrian           * Stuff our fragment back at the front of the queue and zap
45836285Sbrian           * our half-assembed packet.
45936285Sbrian           */
46036285Sbrian          (*frag)->pnext = mp->inbufs;
46136285Sbrian          mp->inbufs = *frag;
46236285Sbrian          *frag = NULL;
46336285Sbrian          mbuf_Free(q);
46436285Sbrian          q = NULL;
46536285Sbrian          frag = &q;
46636285Sbrian          h.end = 0;	/* just in case it's a whole packet */
46736285Sbrian        } else
46836285Sbrian          do
46936285Sbrian            frag = &(*frag)->next;
47036285Sbrian          while (*frag != NULL);
47136285Sbrian      } while (!h.end);
47236285Sbrian
47336285Sbrian      if (q) {
47436285Sbrian        u_short proto;
47536285Sbrian        u_char ch;
47636285Sbrian
47736285Sbrian        q = mbuf_Read(q, &ch, 1);
47836285Sbrian        proto = ch;
47936285Sbrian        if (!(proto & 1)) {
48036285Sbrian          q = mbuf_Read(q, &ch, 1);
48136285Sbrian          proto <<= 8;
48236285Sbrian          proto += ch;
48336285Sbrian        }
48436285Sbrian        if (log_IsKept(LogDEBUG))
48536285Sbrian          log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
48636285Sbrian                    first, (u_long)h.seq, mbuf_Length(q));
48736285Sbrian        hdlc_DecodePacket(mp->bundle, proto, q, &mp->link);
48836285Sbrian      }
48936285Sbrian
49036285Sbrian      mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
49136285Sbrian      last = NULL;
49236285Sbrian      q = mp->inbufs;
49336285Sbrian    } else {
49436285Sbrian      /* Look for the next fragment */
49536285Sbrian      seq = inc_seq(mp->local_is12bit, seq);
49636285Sbrian      last = q;
49736285Sbrian      q = q->pnext;
49836285Sbrian    }
49936285Sbrian  }
50036285Sbrian
50136285Sbrian  if (m) {
50236285Sbrian    /* We still have to find a home for our new fragment */
50336285Sbrian    last = NULL;
50436285Sbrian    for (q = mp->inbufs; q; last = q, q = q->pnext) {
50536285Sbrian      mp_ReadHeader(mp, q, &h);
50636285Sbrian      if (isbefore(mp->local_is12bit, mh.seq, h.seq))
50736285Sbrian        break;
50836285Sbrian    }
50936285Sbrian    /* Our received fragment fits in here */
51036285Sbrian    if (last)
51136285Sbrian      last->pnext = m;
51236285Sbrian    else
51336285Sbrian      mp->inbufs = m;
51436285Sbrian    m->pnext = q;
51536285Sbrian  }
51636285Sbrian}
51736285Sbrian
51836285Sbrianstatic void
51936285Sbrianmp_Output(struct mp *mp, struct link *l, struct mbuf *m, u_int32_t begin,
52036285Sbrian          u_int32_t end)
52136285Sbrian{
52236285Sbrian  struct mbuf *mo;
52336285Sbrian
52436285Sbrian  /* Stuff an MP header on the front of our packet and send it */
52536285Sbrian  mo = mbuf_Alloc(4, MB_MP);
52636285Sbrian  mo->next = m;
52736285Sbrian  if (mp->peer_is12bit) {
52836285Sbrian    u_int16_t *seq16;
52936285Sbrian
53036285Sbrian    seq16 = (u_int16_t *)MBUF_CTOP(mo);
53136285Sbrian    *seq16 = htons((begin << 15) | (end << 14) | (u_int16_t)mp->out.seq);
53236285Sbrian    mo->cnt = 2;
53336285Sbrian  } else {
53436285Sbrian    u_int32_t *seq32;
53536285Sbrian
53636285Sbrian    seq32 = (u_int32_t *)MBUF_CTOP(mo);
53736285Sbrian    *seq32 = htonl((begin << 31) | (end << 30) | (u_int32_t)mp->out.seq);
53836285Sbrian    mo->cnt = 4;
53936285Sbrian  }
54036285Sbrian  if (log_IsKept(LogDEBUG))
54136285Sbrian    log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
54236285Sbrian              mp->out.seq, mbuf_Length(mo), l->name);
54336285Sbrian  mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
54436285Sbrian
54536285Sbrian  if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, PROTO_MP, mo))
54636285Sbrian    hdlc_Output(l, PRI_NORMAL, PROTO_MP, mo);
54736285Sbrian}
54836285Sbrian
54936285Sbrianint
55036285Sbrianmp_FillQueues(struct bundle *bundle)
55136285Sbrian{
55236285Sbrian  struct mp *mp = &bundle->ncp.mp;
55336285Sbrian  struct datalink *dl, *fdl;
55436285Sbrian  int total, add, len, thislink, nlinks;
55536285Sbrian  u_int32_t begin, end;
55636285Sbrian  struct mbuf *m, *mo;
55736285Sbrian
55836285Sbrian  thislink = nlinks = 0;
55936285Sbrian  for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
56036312Sbrian    /* Include non-open links here as mp->out.link will stay more correct */
56136285Sbrian    if (!fdl) {
56236285Sbrian      if (thislink == mp->out.link)
56336285Sbrian        fdl = dl;
56436285Sbrian      else
56536285Sbrian        thislink++;
56636285Sbrian    }
56736285Sbrian    nlinks++;
56836285Sbrian  }
56936285Sbrian
57036285Sbrian  if (!fdl) {
57136285Sbrian    fdl = bundle->links;
57236285Sbrian    if (!fdl)
57336285Sbrian      return 0;
57436285Sbrian    thislink = 0;
57536285Sbrian  }
57636285Sbrian
57736285Sbrian  total = 0;
57836285Sbrian  for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
57936285Sbrian    if (!dl) {
58036285Sbrian      dl = bundle->links;
58136285Sbrian      thislink = 0;
58236285Sbrian    }
58336285Sbrian
58436285Sbrian    if (dl->state != DATALINK_OPEN)
58536285Sbrian      continue;
58636285Sbrian
58736285Sbrian    if (dl->physical->out)
58836285Sbrian      /* this link has suffered a short write.  Let it continue */
58936285Sbrian      continue;
59036285Sbrian
59136285Sbrian    add = link_QueueLen(&dl->physical->link);
59236285Sbrian    total += add;
59336285Sbrian    if (add)
59436285Sbrian      /* this link has got stuff already queued.  Let it continue */
59536285Sbrian      continue;
59636285Sbrian
59736285Sbrian    if (!link_QueueLen(&mp->link) && !ip_FlushPacket(&mp->link, bundle))
59836285Sbrian      /* Nothing else to send */
59936285Sbrian      break;
60036285Sbrian
60136285Sbrian    m = link_Dequeue(&mp->link);
60236285Sbrian    len = mbuf_Length(m);
60336285Sbrian    begin = 1;
60436285Sbrian    end = 0;
60536285Sbrian
60636285Sbrian    while (!end) {
60736285Sbrian      if (dl->state == DATALINK_OPEN) {
60836285Sbrian        if (len <= dl->mp.weight + LINK_MINWEIGHT) {
60936285Sbrian          /*
61036285Sbrian           * XXX: Should we remember how much of our `weight' wasn't sent
61136285Sbrian           *      so that we can compensate next time ?
61236285Sbrian           */
61336285Sbrian          mo = m;
61436285Sbrian          end = 1;
61536285Sbrian        } else {
61636285Sbrian          mo = mbuf_Alloc(dl->mp.weight, MB_MP);
61736285Sbrian          mo->cnt = dl->mp.weight;
61836285Sbrian          len -= mo->cnt;
61936285Sbrian          m = mbuf_Read(m, MBUF_CTOP(mo), mo->cnt);
62036285Sbrian        }
62136285Sbrian        mp_Output(mp, &dl->physical->link, mo, begin, end);
62236285Sbrian        begin = 0;
62336285Sbrian      }
62436285Sbrian
62536285Sbrian      if (!end) {
62636285Sbrian        nlinks--;
62736285Sbrian        dl = dl->next;
62836285Sbrian        if (!dl) {
62936285Sbrian          dl = bundle->links;
63036285Sbrian          thislink = 0;
63136285Sbrian        } else
63236285Sbrian          thislink++;
63336285Sbrian      }
63436285Sbrian    }
63536285Sbrian  }
63636285Sbrian  mp->out.link = thislink;		/* Start here next time */
63736285Sbrian
63836285Sbrian  return total;
63936285Sbrian}
64036285Sbrian
64136285Sbrianint
64236285Sbrianmp_SetDatalinkWeight(struct cmdargs const *arg)
64336285Sbrian{
64436285Sbrian  int val;
64536285Sbrian
64636285Sbrian  if (arg->argc != arg->argn+1)
64736285Sbrian    return -1;
64836285Sbrian
64936285Sbrian  val = atoi(arg->argv[arg->argn]);
65036285Sbrian  if (val < LINK_MINWEIGHT) {
65136285Sbrian    log_Printf(LogWARN, "Link weights must not be less than %d\n",
65236285Sbrian              LINK_MINWEIGHT);
65336285Sbrian    return 1;
65436285Sbrian  }
65536285Sbrian  arg->cx->mp.weight = val;
65636285Sbrian  return 0;
65736285Sbrian}
65836285Sbrian
65936285Sbrianint
66036285Sbrianmp_ShowStatus(struct cmdargs const *arg)
66136285Sbrian{
66236285Sbrian  struct mp *mp = &arg->bundle->ncp.mp;
66336285Sbrian
66436285Sbrian  prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
66536285Sbrian  if (mp->active) {
66636285Sbrian    struct mbuf *m;
66736285Sbrian    int bufs = 0;
66836285Sbrian
66936285Sbrian    prompt_Printf(arg->prompt, "Socket:         %s\n",
67036285Sbrian                  mp->server.socket.sun_path);
67136285Sbrian    for (m = mp->inbufs; m; m = m->pnext)
67236285Sbrian      bufs++;
67336285Sbrian    prompt_Printf(arg->prompt, "Pending frags:  %d\n", bufs);
67436285Sbrian  }
67536285Sbrian
67636285Sbrian  prompt_Printf(arg->prompt, "\nMy Side:\n");
67736285Sbrian  if (mp->active) {
67836285Sbrian    prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
67936285Sbrian    prompt_Printf(arg->prompt, " Short Seq:     %s\n",
68036285Sbrian                  mp->local_is12bit ? "on" : "off");
68136285Sbrian  }
68236285Sbrian  prompt_Printf(arg->prompt, " Discriminator: %s\n",
68336285Sbrian                mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
68436285Sbrian                           mp->cfg.enddisc.len));
68536285Sbrian
68636285Sbrian  prompt_Printf(arg->prompt, "\nHis Side:\n");
68736285Sbrian  if (mp->active) {
68836285Sbrian    prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
68936285Sbrian    prompt_Printf(arg->prompt, " Next SEQ:      %u\n", mp->out.seq);
69036285Sbrian    prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
69136285Sbrian    prompt_Printf(arg->prompt, " Short Seq:     %s\n",
69236285Sbrian                  mp->peer_is12bit ? "on" : "off");
69336285Sbrian  }
69436285Sbrian  prompt_Printf(arg->prompt,   " Discriminator: %s\n",
69536285Sbrian                mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
69636285Sbrian                           mp->peer.enddisc.len));
69736285Sbrian
69836285Sbrian  prompt_Printf(arg->prompt, "\nDefaults:\n");
69936285Sbrian
70036285Sbrian  prompt_Printf(arg->prompt, " MRRU:          ");
70136285Sbrian  if (mp->cfg.mrru)
70236285Sbrian    prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
70336285Sbrian  else
70436285Sbrian    prompt_Printf(arg->prompt, "disabled\n");
70536285Sbrian  prompt_Printf(arg->prompt, " Short Seq:     %s\n",
70636285Sbrian                  command_ShowNegval(mp->cfg.shortseq));
70736285Sbrian
70836285Sbrian  return 0;
70936285Sbrian}
71036285Sbrian
71136285Sbrianconst char *
71236285Sbrianmp_Enddisc(u_char c, const char *address, int len)
71336285Sbrian{
71437010Sbrian  static char result[100];	/* Used immediately after it's returned */
71536285Sbrian  int f, header;
71636285Sbrian
71736285Sbrian  switch (c) {
71836285Sbrian    case ENDDISC_NULL:
71936285Sbrian      sprintf(result, "Null Class");
72036285Sbrian      break;
72136285Sbrian
72236285Sbrian    case ENDDISC_LOCAL:
72336285Sbrian      snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
72436285Sbrian      break;
72536285Sbrian
72636285Sbrian    case ENDDISC_IP:
72736285Sbrian      if (len == 4)
72836285Sbrian        snprintf(result, sizeof result, "IP %s",
72936285Sbrian                 inet_ntoa(*(const struct in_addr *)address));
73036285Sbrian      else
73136285Sbrian        sprintf(result, "IP[%d] ???", len);
73236285Sbrian      break;
73336285Sbrian
73436285Sbrian    case ENDDISC_MAC:
73536285Sbrian      if (len == 6) {
73636285Sbrian        const u_char *m = (const u_char *)address;
73736285Sbrian        snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
73836285Sbrian                 m[0], m[1], m[2], m[3], m[4], m[5]);
73936285Sbrian      } else
74036285Sbrian        sprintf(result, "MAC[%d] ???", len);
74136285Sbrian      break;
74236285Sbrian
74336285Sbrian    case ENDDISC_MAGIC:
74436285Sbrian      sprintf(result, "Magic: 0x");
74536285Sbrian      header = strlen(result);
74636285Sbrian      if (len > sizeof result - header - 1)
74736285Sbrian        len = sizeof result - header - 1;
74836285Sbrian      for (f = 0; f < len; f++)
74936285Sbrian        sprintf(result + header + 2 * f, "%02x", address[f]);
75036285Sbrian      break;
75136285Sbrian
75236285Sbrian    case ENDDISC_PSN:
75336285Sbrian      snprintf(result, sizeof result, "PSN: %.*s", len, address);
75436285Sbrian      break;
75536285Sbrian
75636285Sbrian     default:
75736285Sbrian      sprintf(result, "%d: ", (int)c);
75836285Sbrian      header = strlen(result);
75936285Sbrian      if (len > sizeof result - header - 1)
76036285Sbrian        len = sizeof result - header - 1;
76136285Sbrian      for (f = 0; f < len; f++)
76236285Sbrian        sprintf(result + header + 2 * f, "%02x", address[f]);
76336285Sbrian      break;
76436285Sbrian  }
76536285Sbrian  return result;
76636285Sbrian}
76736285Sbrian
76836285Sbrianint
76936285Sbrianmp_SetEnddisc(struct cmdargs const *arg)
77036285Sbrian{
77136285Sbrian  struct mp *mp = &arg->bundle->ncp.mp;
77236285Sbrian  struct in_addr addr;
77336285Sbrian
77436285Sbrian  if (bundle_Phase(arg->bundle) != PHASE_DEAD) {
77536285Sbrian    log_Printf(LogWARN, "set enddisc: Only available at phase DEAD\n");
77636285Sbrian    return 1;
77736285Sbrian  }
77836285Sbrian
77936285Sbrian  if (arg->argc == arg->argn) {
78036285Sbrian    mp->cfg.enddisc.class = 0;
78136285Sbrian    *mp->cfg.enddisc.address = '\0';
78236285Sbrian    mp->cfg.enddisc.len = 0;
78336285Sbrian  } else if (arg->argc > arg->argn) {
78436285Sbrian    if (!strcasecmp(arg->argv[arg->argn], "label")) {
78536285Sbrian      mp->cfg.enddisc.class = ENDDISC_LOCAL;
78636285Sbrian      strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
78736285Sbrian      mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
78836285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
78936285Sbrian      if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
79036285Sbrian        addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
79136285Sbrian      else
79236285Sbrian        addr = arg->bundle->ncp.ipcp.my_ip;
79336285Sbrian      memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
79436285Sbrian      mp->cfg.enddisc.class = ENDDISC_IP;
79536285Sbrian      mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
79636285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
79736285Sbrian      struct sockaddr_dl hwaddr;
79836285Sbrian      int s;
79936285Sbrian
80036285Sbrian      if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
80136285Sbrian        addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
80236285Sbrian      else
80336285Sbrian        addr = arg->bundle->ncp.ipcp.my_ip;
80436285Sbrian
80536285Sbrian      s = ID0socket(AF_INET, SOCK_DGRAM, 0);
80636285Sbrian      if (s < 0) {
80736285Sbrian        log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
80836285Sbrian        return 2;
80936285Sbrian      }
81036285Sbrian      if (get_ether_addr(s, addr, &hwaddr)) {
81136285Sbrian        mp->cfg.enddisc.class = ENDDISC_MAC;
81236285Sbrian        memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
81336285Sbrian               hwaddr.sdl_alen);
81436285Sbrian        mp->cfg.enddisc.len = hwaddr.sdl_alen;
81536285Sbrian      } else {
81636285Sbrian        log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
81736285Sbrian                  inet_ntoa(addr));
81836285Sbrian        close(s);
81936285Sbrian        return 4;
82036285Sbrian      }
82136285Sbrian      close(s);
82236285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
82336285Sbrian      int f;
82436285Sbrian
82536285Sbrian      randinit();
82636285Sbrian      for (f = 0; f < 20; f += sizeof(long))
82736285Sbrian        *(long *)(mp->cfg.enddisc.address + f) = random();
82836285Sbrian      mp->cfg.enddisc.class = ENDDISC_MAGIC;
82936285Sbrian      mp->cfg.enddisc.len = 20;
83036285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
83136285Sbrian      if (arg->argc > arg->argn+1) {
83236285Sbrian        mp->cfg.enddisc.class = ENDDISC_PSN;
83336285Sbrian        strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
83436285Sbrian        mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
83536285Sbrian      } else {
83636285Sbrian        log_Printf(LogWARN, "PSN endpoint requires additional data\n");
83736285Sbrian        return 5;
83836285Sbrian      }
83936285Sbrian    } else {
84036285Sbrian      log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
84136285Sbrian                arg->argv[arg->argn]);
84236285Sbrian      return 6;
84336285Sbrian    }
84436285Sbrian  }
84536285Sbrian
84636285Sbrian  return 0;
84736285Sbrian}
84836285Sbrian
84936285Sbrianstatic int
85036285Sbrianmpserver_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e,
85136285Sbrian                   int *n)
85236285Sbrian{
85336285Sbrian  struct mpserver *s = descriptor2mpserver(d);
85436314Sbrian  int result;
85536285Sbrian
85636314Sbrian  result = 0;
85736285Sbrian  if (s->send.dl != NULL) {
85836285Sbrian    /* We've connect()ed */
85936285Sbrian    if (!link_QueueLen(&s->send.dl->physical->link) &&
86036285Sbrian        !s->send.dl->physical->out) {
86136285Sbrian      /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
86236314Sbrian      result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
86336285Sbrian      bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
86436285Sbrian      s->send.dl = NULL;
86536285Sbrian      s->fd = -1;
86636285Sbrian    } else
86736285Sbrian      /* Never read from a datalink that's on death row ! */
86836314Sbrian      result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
86936285Sbrian  } else if (r && s->fd >= 0) {
87036285Sbrian    if (*n < s->fd + 1)
87136285Sbrian      *n = s->fd + 1;
87236285Sbrian    FD_SET(s->fd, r);
87336285Sbrian    log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
87436314Sbrian    result++;
87536285Sbrian  }
87636314Sbrian  return result;
87736285Sbrian}
87836285Sbrian
87936285Sbrianstatic int
88036285Sbrianmpserver_IsSet(struct descriptor *d, const fd_set *fdset)
88136285Sbrian{
88236285Sbrian  struct mpserver *s = descriptor2mpserver(d);
88336285Sbrian  return s->fd >= 0 && FD_ISSET(s->fd, fdset);
88436285Sbrian}
88536285Sbrian
88636285Sbrianstatic void
88736285Sbrianmpserver_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
88836285Sbrian{
88936285Sbrian  struct mpserver *s = descriptor2mpserver(d);
89036285Sbrian  struct sockaddr in;
89136285Sbrian  int fd, size;
89236285Sbrian
89336285Sbrian  size = sizeof in;
89436285Sbrian  fd = accept(s->fd, &in, &size);
89536285Sbrian  if (fd < 0) {
89636285Sbrian    log_Printf(LogERROR, "mpserver_Read: accept(): %s\n", strerror(errno));
89736285Sbrian    return;
89836285Sbrian  }
89936285Sbrian
90036285Sbrian  if (in.sa_family == AF_LOCAL)
90136285Sbrian    bundle_ReceiveDatalink(bundle, fd, (struct sockaddr_un *)&in);
90236345Sbrian  else
90336345Sbrian    close(fd);
90436285Sbrian}
90536285Sbrian
90637141Sbrianstatic int
90736285Sbrianmpserver_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
90836285Sbrian{
90936285Sbrian  /* We never want to write here ! */
91037019Sbrian  log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
91137141Sbrian  return 0;
91236285Sbrian}
91336285Sbrian
91436285Sbrianvoid
91536285Sbrianmpserver_Init(struct mpserver *s)
91636285Sbrian{
91736285Sbrian  s->desc.type = MPSERVER_DESCRIPTOR;
91836285Sbrian  s->desc.UpdateSet = mpserver_UpdateSet;
91936285Sbrian  s->desc.IsSet = mpserver_IsSet;
92036285Sbrian  s->desc.Read = mpserver_Read;
92136285Sbrian  s->desc.Write = mpserver_Write;
92236285Sbrian  s->send.dl = NULL;
92336285Sbrian  s->fd = -1;
92436285Sbrian  memset(&s->socket, '\0', sizeof s->socket);
92536285Sbrian}
92636285Sbrian
92736285Sbrianint
92836285Sbrianmpserver_Open(struct mpserver *s, struct peerid *peer)
92936285Sbrian{
93036285Sbrian  int f, l;
93136285Sbrian  mode_t mask;
93236285Sbrian
93336285Sbrian  if (s->fd != -1) {
93437019Sbrian    log_Printf(LogALERT, "Internal error !  mpserver already open\n");
93536285Sbrian    mpserver_Close(s);
93636285Sbrian  }
93736285Sbrian
93836285Sbrian  l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
93936285Sbrian               _PATH_VARRUN, peer->authname, peer->enddisc.class);
94036285Sbrian
94136285Sbrian  for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
94236285Sbrian    snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
94336285Sbrian             "%02x", *(u_char *)(peer->enddisc.address+f));
94436285Sbrian    l += 2;
94536285Sbrian  }
94636285Sbrian
94736285Sbrian  s->socket.sun_family = AF_LOCAL;
94836285Sbrian  s->socket.sun_len = sizeof s->socket;
94936285Sbrian  s->fd = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
95036285Sbrian  if (s->fd < 0) {
95136285Sbrian    log_Printf(LogERROR, "mpserver: socket: %s\n", strerror(errno));
95236285Sbrian    return MPSERVER_FAILED;
95336285Sbrian  }
95436285Sbrian
95536285Sbrian  setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
95636285Sbrian             sizeof s->socket);
95736285Sbrian  mask = umask(0177);
95836285Sbrian  if (ID0bind_un(s->fd, &s->socket) < 0) {
95936285Sbrian    if (errno != EADDRINUSE) {
96036285Sbrian      log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
96136285Sbrian                s->socket.sun_path, strerror(errno));
96236285Sbrian      umask(mask);
96336285Sbrian      close(s->fd);
96436285Sbrian      s->fd = -1;
96536285Sbrian      return MPSERVER_FAILED;
96636285Sbrian    }
96736285Sbrian    umask(mask);
96836285Sbrian    if (ID0connect_un(s->fd, &s->socket) < 0) {
96936285Sbrian      log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
97036285Sbrian                s->socket.sun_path, strerror(errno));
97136285Sbrian      if (errno == ECONNREFUSED)
97236285Sbrian        log_Printf(LogPHASE, "          Has the previous server died badly ?\n");
97336285Sbrian      close(s->fd);
97436285Sbrian      s->fd = -1;
97536285Sbrian      return MPSERVER_FAILED;
97636285Sbrian    }
97736285Sbrian
97836285Sbrian    /* Donate our link to the other guy */
97936285Sbrian    return MPSERVER_CONNECTED;
98036285Sbrian  }
98136285Sbrian
98236285Sbrian  /* Listen for other ppp invocations that want to donate links */
98336285Sbrian  if (listen(s->fd, 5) != 0) {
98436285Sbrian    log_Printf(LogERROR, "mpserver: Unable to listen to socket"
98536285Sbrian              " - BUNDLE overload?\n");
98636285Sbrian    mpserver_Close(s);
98736285Sbrian  }
98836285Sbrian
98936285Sbrian  return MPSERVER_LISTENING;
99036285Sbrian}
99136285Sbrian
99236285Sbrianvoid
99336285Sbrianmpserver_Close(struct mpserver *s)
99436285Sbrian{
99536285Sbrian  if (s->send.dl != NULL) {
99636285Sbrian    bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
99736285Sbrian    s->send.dl = NULL;
99836285Sbrian    s->fd = -1;
99936285Sbrian  } else if (s->fd >= 0) {
100036285Sbrian    close(s->fd);
100136285Sbrian    if (ID0unlink(s->socket.sun_path) == -1)
100236285Sbrian      log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
100336285Sbrian                strerror(errno));
100436285Sbrian    memset(&s->socket, '\0', sizeof s->socket);
100536285Sbrian    s->fd = -1;
100636285Sbrian  }
100736285Sbrian}
100836312Sbrian
100936312Sbrianvoid
101036312Sbrianmp_LinkLost(struct mp *mp, struct datalink *dl)
101136312Sbrian{
101236312Sbrian  if (mp->seq.min_in == dl->mp.seq)
101336312Sbrian    /* We've lost the link that's holding everything up ! */
101436312Sbrian    mp_Input(mp, NULL, NULL);
101536312Sbrian}
1016