mp.c revision 64780
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 *
2650479Speter * $FreeBSD: head/usr.sbin/ppp/mp.c 64780 2000-08-17 14:14:54Z brian $
2736285Sbrian */
2836285Sbrian
2943313Sbrian#include <sys/param.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
4746686Sbrian#include "layer.h"
4850059Sbrian#ifndef NONAT
4951075Sbrian#include "nat_cmd.h"
5046686Sbrian#endif
5146686Sbrian#include "vjcomp.h"
5238814Sbrian#include "ua.h"
5337009Sbrian#include "defs.h"
5436285Sbrian#include "command.h"
5536285Sbrian#include "mbuf.h"
5636285Sbrian#include "log.h"
5736285Sbrian#include "timer.h"
5836285Sbrian#include "fsm.h"
5936285Sbrian#include "iplist.h"
6036285Sbrian#include "throughput.h"
6136285Sbrian#include "slcompress.h"
6238557Sbrian#include "lqr.h"
6338557Sbrian#include "hdlc.h"
6436285Sbrian#include "ipcp.h"
6536285Sbrian#include "auth.h"
6636285Sbrian#include "lcp.h"
6736285Sbrian#include "async.h"
6836285Sbrian#include "ccp.h"
6936285Sbrian#include "link.h"
7036285Sbrian#include "descriptor.h"
7136285Sbrian#include "physical.h"
7236285Sbrian#include "chat.h"
7346686Sbrian#include "proto.h"
7436285Sbrian#include "filter.h"
7536285Sbrian#include "mp.h"
7636285Sbrian#include "chap.h"
7738174Sbrian#include "cbcp.h"
7836285Sbrian#include "datalink.h"
7943313Sbrian#ifndef NORADIUS
8043313Sbrian#include "radius.h"
8143313Sbrian#endif
8236285Sbrian#include "bundle.h"
8336285Sbrian#include "ip.h"
8436285Sbrian#include "prompt.h"
8536285Sbrian#include "id.h"
8636285Sbrian#include "arp.h"
8736285Sbrian
8836285Sbrianvoid
8936285Sbrianpeerid_Init(struct peerid *peer)
9036285Sbrian{
9136285Sbrian  peer->enddisc.class = 0;
9236285Sbrian  *peer->enddisc.address = '\0';
9336285Sbrian  peer->enddisc.len = 0;
9436285Sbrian  *peer->authname = '\0';
9536285Sbrian}
9636285Sbrian
9736285Sbrianint
9836285Sbrianpeerid_Equal(const struct peerid *p1, const struct peerid *p2)
9936285Sbrian{
10036285Sbrian  return !strcmp(p1->authname, p2->authname) &&
10136285Sbrian         p1->enddisc.class == p2->enddisc.class &&
10236285Sbrian         p1->enddisc.len == p2->enddisc.len &&
10336285Sbrian         !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
10436285Sbrian}
10536285Sbrian
10636285Sbrianstatic u_int32_t
10736285Sbrianinc_seq(unsigned is12bit, u_int32_t seq)
10836285Sbrian{
10936285Sbrian  seq++;
11036285Sbrian  if (is12bit) {
11136285Sbrian    if (seq & 0xfffff000)
11236285Sbrian      seq = 0;
11336285Sbrian  } else if (seq & 0xff000000)
11436285Sbrian    seq = 0;
11536285Sbrian  return seq;
11636285Sbrian}
11736285Sbrian
11836285Sbrianstatic int
11936285Sbrianisbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
12036285Sbrian{
12136285Sbrian  u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
12236285Sbrian
12336285Sbrian  if (seq1 > max) {
12436285Sbrian    if (seq2 < 0x200 || seq2 > seq1)
12536285Sbrian      return 1;
12636285Sbrian  } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
12736285Sbrian    return 1;
12836285Sbrian
12936285Sbrian  return 0;
13036285Sbrian}
13136285Sbrian
13236285Sbrianstatic int
13336285Sbrianmp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
13436285Sbrian{
13536285Sbrian  if (mp->local_is12bit) {
13638814Sbrian    u_int16_t val;
13738814Sbrian
13838814Sbrian    ua_ntohs(MBUF_CTOP(m), &val);
13938814Sbrian    if (val & 0x3000) {
14036285Sbrian      log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
14136285Sbrian      return 0;
14236285Sbrian    }
14338814Sbrian    header->begin = val & 0x8000 ? 1 : 0;
14438814Sbrian    header->end = val & 0x4000 ? 1 : 0;
14538814Sbrian    header->seq = val & 0x0fff;
14636285Sbrian    return 2;
14736285Sbrian  } else {
14838814Sbrian    ua_ntohl(MBUF_CTOP(m), &header->seq);
14936285Sbrian    if (header->seq & 0x3f000000) {
15036285Sbrian      log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
15136285Sbrian      return 0;
15236285Sbrian    }
15336285Sbrian    header->begin = header->seq & 0x80000000 ? 1 : 0;
15436285Sbrian    header->end = header->seq & 0x40000000 ? 1 : 0;
15536285Sbrian    header->seq &= 0x00ffffff;
15636285Sbrian    return 4;
15736285Sbrian  }
15836285Sbrian}
15936285Sbrian
16036285Sbrianstatic void
16136285Sbrianmp_LayerStart(void *v, struct fsm *fp)
16236285Sbrian{
16336285Sbrian  /* The given FSM (ccp) is about to start up ! */
16436285Sbrian}
16536285Sbrian
16636285Sbrianstatic void
16736285Sbrianmp_LayerUp(void *v, struct fsm *fp)
16836285Sbrian{
16936285Sbrian  /* The given fsm (ccp) is now up */
17036285Sbrian}
17136285Sbrian
17236285Sbrianstatic void
17336285Sbrianmp_LayerDown(void *v, struct fsm *fp)
17436285Sbrian{
17536285Sbrian  /* The given FSM (ccp) has been told to come down */
17636285Sbrian}
17736285Sbrian
17836285Sbrianstatic void
17936285Sbrianmp_LayerFinish(void *v, struct fsm *fp)
18036285Sbrian{
18136285Sbrian  /* The given fsm (ccp) is now down */
18236285Sbrian  if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
18336285Sbrian    fsm_Open(fp);		/* CCP goes to ST_STOPPED */
18436285Sbrian}
18536285Sbrian
18649434Sbrianstatic void
18749434Sbrianmp_UpDown(void *v)
18849434Sbrian{
18949434Sbrian  struct mp *mp = (struct mp *)v;
19049434Sbrian  int percent;
19149434Sbrian
19264670Sbrian  percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
19364670Sbrian                mp->link.stats.total.out.OctetsPerSecond) * 800 /
19464670Sbrian            mp->bundle->bandwidth;
19549434Sbrian  if (percent >= mp->cfg.autoload.max) {
19649434Sbrian    log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
19749434Sbrian    bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
19849434Sbrian  } else if (percent <= mp->cfg.autoload.min) {
19949434Sbrian    log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
20049434Sbrian    bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
20149434Sbrian  }
20249434Sbrian}
20349434Sbrian
20436285Sbrianvoid
20549434Sbrianmp_StopAutoloadTimer(struct mp *mp)
20649434Sbrian{
20764652Sbrian  throughput_stop(&mp->link.stats.total);
20849434Sbrian}
20949434Sbrian
21049434Sbrianvoid
21149434Sbrianmp_CheckAutoloadTimer(struct mp *mp)
21249434Sbrian{
21364652Sbrian  if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
21464652Sbrian    throughput_destroy(&mp->link.stats.total);
21564652Sbrian    throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
21664652Sbrian    throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
21749434Sbrian  }
21849434Sbrian
21949434Sbrian  if (bundle_WantAutoloadTimer(mp->bundle))
22064652Sbrian    throughput_start(&mp->link.stats.total, "MP throughput", 1);
22149434Sbrian  else
22249434Sbrian    mp_StopAutoloadTimer(mp);
22349434Sbrian}
22449434Sbrian
22549434Sbrianvoid
22649434Sbrianmp_RestartAutoloadTimer(struct mp *mp)
22749434Sbrian{
22864652Sbrian  if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
22949434Sbrian    mp_CheckAutoloadTimer(mp);
23049434Sbrian  else
23164652Sbrian    throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
23249434Sbrian}
23349434Sbrian
23449434Sbrianvoid
23536285Sbrianmp_Init(struct mp *mp, struct bundle *bundle)
23636285Sbrian{
23736285Sbrian  mp->peer_is12bit = mp->local_is12bit = 0;
23836285Sbrian  mp->peer_mrru = mp->local_mrru = 0;
23936285Sbrian
24036285Sbrian  peerid_Init(&mp->peer);
24136285Sbrian
24236285Sbrian  mp->out.seq = 0;
24336285Sbrian  mp->out.link = 0;
24436285Sbrian  mp->seq.min_in = 0;
24536285Sbrian  mp->seq.next_in = 0;
24636285Sbrian  mp->inbufs = NULL;
24736285Sbrian  mp->bundle = bundle;
24836285Sbrian
24946686Sbrian  mp->link.type = LOGICAL_LINK;
25036285Sbrian  mp->link.name = "mp";
25136285Sbrian  mp->link.len = sizeof *mp;
25236285Sbrian
25349434Sbrian  mp->cfg.autoload.period = SAMPLE_PERIOD;
25449434Sbrian  mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
25564652Sbrian  throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
25664652Sbrian  throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
25764652Sbrian  mp->link.stats.parent = NULL;
25864652Sbrian  mp->link.stats.gather = 0;	/* Let the physical links gather stats */
25936285Sbrian  memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
26036285Sbrian  memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
26136285Sbrian  memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
26236285Sbrian
26336285Sbrian  mp->fsmp.LayerStart = mp_LayerStart;
26436285Sbrian  mp->fsmp.LayerUp = mp_LayerUp;
26536285Sbrian  mp->fsmp.LayerDown = mp_LayerDown;
26636285Sbrian  mp->fsmp.LayerFinish = mp_LayerFinish;
26736285Sbrian  mp->fsmp.object = mp;
26836285Sbrian
26936285Sbrian  mpserver_Init(&mp->server);
27036285Sbrian
27136285Sbrian  mp->cfg.mrru = 0;
27236285Sbrian  mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
27347858Sbrian  mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
27436285Sbrian  mp->cfg.enddisc.class = 0;
27536285Sbrian  *mp->cfg.enddisc.address = '\0';
27636285Sbrian  mp->cfg.enddisc.len = 0;
27736285Sbrian
27836285Sbrian  lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
27936285Sbrian  ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
28046686Sbrian
28146686Sbrian  link_EmptyStack(&mp->link);
28246686Sbrian  link_Stack(&mp->link, &protolayer);
28346686Sbrian  link_Stack(&mp->link, &ccplayer);
28446686Sbrian  link_Stack(&mp->link, &vjlayer);
28550059Sbrian#ifndef NONAT
28650059Sbrian  link_Stack(&mp->link, &natlayer);
28746686Sbrian#endif
28836285Sbrian}
28936285Sbrian
29036285Sbrianint
29136285Sbrianmp_Up(struct mp *mp, struct datalink *dl)
29236285Sbrian{
29336285Sbrian  struct lcp *lcp = &dl->physical->link.lcp;
29436285Sbrian
29536285Sbrian  if (mp->active) {
29636285Sbrian    /* We're adding a link - do a last validation on our parameters */
29736285Sbrian    if (!peerid_Equal(&dl->peer, &mp->peer)) {
29836285Sbrian      log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
29960778Sbrian      log_Printf(LogPHASE, "  Attached to peer %s/%s\n", mp->peer.authname,
30060778Sbrian                 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
30160778Sbrian                            mp->peer.enddisc.len));
30260778Sbrian      log_Printf(LogPHASE, "  New link is peer %s/%s\n", dl->peer.authname,
30360778Sbrian                 mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
30460778Sbrian                            dl->peer.enddisc.len));
30536285Sbrian      return MP_FAILED;
30636285Sbrian    }
30736285Sbrian    if (mp->local_mrru != lcp->want_mrru ||
30836285Sbrian        mp->peer_mrru != lcp->his_mrru ||
30936285Sbrian        mp->local_is12bit != lcp->want_shortseq ||
31036285Sbrian        mp->peer_is12bit != lcp->his_shortseq) {
31136285Sbrian      log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
31236285Sbrian                dl->name);
31336285Sbrian      return MP_FAILED;
31436285Sbrian    }
31536285Sbrian    return MP_ADDED;
31636285Sbrian  } else {
31736285Sbrian    /* First link in multilink mode */
31836285Sbrian
31936285Sbrian    mp->local_mrru = lcp->want_mrru;
32036285Sbrian    mp->peer_mrru = lcp->his_mrru;
32136285Sbrian    mp->local_is12bit = lcp->want_shortseq;
32236285Sbrian    mp->peer_is12bit = lcp->his_shortseq;
32336285Sbrian    mp->peer = dl->peer;
32436285Sbrian
32564652Sbrian    throughput_destroy(&mp->link.stats.total);
32664652Sbrian    throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
32764652Sbrian    throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
32836285Sbrian    memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
32936285Sbrian    memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
33036285Sbrian    memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
33136285Sbrian
33264652Sbrian    /* Tell the link who it belongs to */
33364652Sbrian    dl->physical->link.stats.parent = &mp->link.stats.total;
33464652Sbrian
33536285Sbrian    mp->out.seq = 0;
33636285Sbrian    mp->out.link = 0;
33736285Sbrian    mp->seq.min_in = 0;
33836285Sbrian    mp->seq.next_in = 0;
33936285Sbrian
34036285Sbrian    /*
34136285Sbrian     * Now we create our server socket.
34236285Sbrian     * If it already exists, join it.  Otherwise, create and own it
34336285Sbrian     */
34436285Sbrian    switch (mpserver_Open(&mp->server, &mp->peer)) {
34536285Sbrian    case MPSERVER_CONNECTED:
34636285Sbrian      log_Printf(LogPHASE, "mp: Transfer link on %s\n",
34736285Sbrian                mp->server.socket.sun_path);
34836285Sbrian      mp->server.send.dl = dl;		/* Defer 'till it's safe to send */
34936285Sbrian      return MP_LINKSENT;
35036285Sbrian    case MPSERVER_FAILED:
35136285Sbrian      return MP_FAILED;
35236285Sbrian    case MPSERVER_LISTENING:
35336285Sbrian      log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
35436285Sbrian      log_Printf(LogPHASE, "    First link: %s\n", dl->name);
35536285Sbrian
35636285Sbrian      /* Re-point our IPCP layer at our MP link */
35736285Sbrian      ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
35836285Sbrian
35936285Sbrian      /* Our lcp's already up 'cos of the NULL parent */
36037320Sbrian      if (ccp_SetOpenMode(&mp->link.ccp)) {
36137320Sbrian        fsm_Up(&mp->link.ccp.fsm);
36237320Sbrian        fsm_Open(&mp->link.ccp.fsm);
36337320Sbrian      }
36436285Sbrian
36536285Sbrian      mp->active = 1;
36636285Sbrian      break;
36736285Sbrian    }
36836285Sbrian  }
36936285Sbrian
37036285Sbrian  return MP_UP;
37136285Sbrian}
37236285Sbrian
37336285Sbrianvoid
37436285Sbrianmp_Down(struct mp *mp)
37536285Sbrian{
37636285Sbrian  if (mp->active) {
37736285Sbrian    struct mbuf *next;
37836285Sbrian
37949434Sbrian    /* Stop that ! */
38049434Sbrian    mp_StopAutoloadTimer(mp);
38149434Sbrian
38236285Sbrian    /* Don't want any more of these */
38336285Sbrian    mpserver_Close(&mp->server);
38436285Sbrian
38536285Sbrian    /* CCP goes down with a bang */
38637060Sbrian    fsm2initial(&mp->link.ccp.fsm);
38736285Sbrian
38836285Sbrian    /* Received fragments go in the bit-bucket */
38936285Sbrian    while (mp->inbufs) {
39054912Sbrian      next = mp->inbufs->m_nextpkt;
39154912Sbrian      m_freem(mp->inbufs);
39236285Sbrian      mp->inbufs = next;
39336285Sbrian    }
39436285Sbrian
39536285Sbrian    peerid_Init(&mp->peer);
39636285Sbrian    mp->active = 0;
39736285Sbrian  }
39836285Sbrian}
39936285Sbrian
40036285Sbrianvoid
40136285Sbrianmp_linkInit(struct mp_link *mplink)
40236285Sbrian{
40336285Sbrian  mplink->seq = 0;
40449434Sbrian  mplink->bandwidth = 0;
40536285Sbrian}
40636285Sbrian
40746686Sbrianstatic void
40846686Sbrianmp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
40936285Sbrian{
41036285Sbrian  struct mp_header mh, h;
41136285Sbrian  struct mbuf *q, *last;
41236285Sbrian  int32_t seq;
41336285Sbrian
41436312Sbrian  /*
41536312Sbrian   * When `m' and `p' are NULL, it means our oldest link has gone down.
41636312Sbrian   * We want to determine a new min, and process any intermediate stuff
41736312Sbrian   * as normal
41836312Sbrian   */
41936312Sbrian
42036312Sbrian  if (m && mp_ReadHeader(mp, m, &mh) == 0) {
42154912Sbrian    m_freem(m);
42236285Sbrian    return;
42336285Sbrian  }
42436285Sbrian
42536312Sbrian  if (p) {
42636312Sbrian    seq = p->dl->mp.seq;
42736312Sbrian    p->dl->mp.seq = mh.seq;
42836312Sbrian  } else
42936312Sbrian    seq = mp->seq.min_in;
43036312Sbrian
43136285Sbrian  if (mp->seq.min_in == seq) {
43236285Sbrian    /*
43336285Sbrian     * We've received new data on the link that has our min (oldest) seq.
43436285Sbrian     * Figure out which link now has the smallest (oldest) seq.
43536285Sbrian     */
43636285Sbrian    struct datalink *dl;
43736285Sbrian
43836312Sbrian    mp->seq.min_in = (u_int32_t)-1;
43936285Sbrian    for (dl = mp->bundle->links; dl; dl = dl->next)
44036312Sbrian      if (dl->state == DATALINK_OPEN &&
44136312Sbrian          (mp->seq.min_in == -1 ||
44236312Sbrian           isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
44336285Sbrian        mp->seq.min_in = dl->mp.seq;
44436285Sbrian  }
44536285Sbrian
44636285Sbrian  /*
44736285Sbrian   * Now process as many of our fragments as we can, adding our new
44836285Sbrian   * fragment in as we go, and ordering with the oldest at the top of
44936285Sbrian   * the queue.
45036285Sbrian   */
45136285Sbrian
45236285Sbrian  last = NULL;
45336285Sbrian  seq = mp->seq.next_in;
45436285Sbrian  q = mp->inbufs;
45563020Sbrian  while (q || m) {
45663020Sbrian    if (!q) {
45736285Sbrian      if (last)
45854912Sbrian        last->m_nextpkt = m;
45936285Sbrian      else
46036285Sbrian        mp->inbufs = m;
46136285Sbrian      q = m;
46263020Sbrian      m = NULL;
46336285Sbrian      h = mh;
46463020Sbrian    } else {
46563020Sbrian      mp_ReadHeader(mp, q, &h);
46663020Sbrian
46763020Sbrian      if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
46863020Sbrian        /* Our received fragment fits in before this one, so link it in */
46963020Sbrian        if (last)
47063020Sbrian          last->m_nextpkt = m;
47163020Sbrian        else
47263020Sbrian          mp->inbufs = m;
47363020Sbrian        m->m_nextpkt = q;
47463020Sbrian        q = m;
47563020Sbrian        h = mh;
47663020Sbrian        m = NULL;
47763020Sbrian      }
47836285Sbrian    }
47936285Sbrian
48036285Sbrian    if (h.seq != seq) {
48136285Sbrian      /* we're missing something :-( */
48247712Sbrian      if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
48336285Sbrian        /* we're never gonna get it */
48436285Sbrian        struct mbuf *next;
48536285Sbrian
48636285Sbrian        /* Zap all older fragments */
48736285Sbrian        while (mp->inbufs != q) {
48836285Sbrian          log_Printf(LogDEBUG, "Drop frag\n");
48954912Sbrian          next = mp->inbufs->m_nextpkt;
49054912Sbrian          m_freem(mp->inbufs);
49136285Sbrian          mp->inbufs = next;
49236285Sbrian        }
49336285Sbrian
49436285Sbrian        /*
49536285Sbrian         * Zap everything until the next `end' fragment OR just before
49636285Sbrian         * the next `begin' fragment OR 'till seq.min_in - whichever
49736285Sbrian         * comes first.
49836285Sbrian         */
49936285Sbrian        do {
50036285Sbrian          mp_ReadHeader(mp, mp->inbufs, &h);
50136285Sbrian          if (h.begin) {
50236285Sbrian            /* We might be able to process this ! */
50336285Sbrian            h.seq--;  /* We're gonna look for fragment with h.seq+1 */
50436285Sbrian            break;
50536285Sbrian          }
50654912Sbrian          next = mp->inbufs->m_nextpkt;
50736285Sbrian          log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
50854912Sbrian          m_freem(mp->inbufs);
50936285Sbrian          mp->inbufs = next;
51047712Sbrian        } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
51147712Sbrian                                         h.seq) || h.end));
51236285Sbrian
51336285Sbrian        /*
51436285Sbrian         * Continue processing things from here.
51536285Sbrian         * This deals with the possibility that we received a fragment
51636285Sbrian         * on the slowest link that invalidates some of our data (because
51736285Sbrian         * of the hole at `q'), but where there are subsequent `whole'
51836285Sbrian         * packets that have already been received.
51936285Sbrian         */
52036285Sbrian
52136285Sbrian        mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
52236285Sbrian        last = NULL;
52336285Sbrian        q = mp->inbufs;
52436285Sbrian      } else
52536285Sbrian        /* we may still receive the missing fragment */
52636285Sbrian        break;
52736285Sbrian    } else if (h.end) {
52836285Sbrian      /* We've got something, reassemble */
52936285Sbrian      struct mbuf **frag = &q;
53036285Sbrian      int len;
53136285Sbrian      u_long first = -1;
53236285Sbrian
53336285Sbrian      do {
53436285Sbrian        *frag = mp->inbufs;
53554912Sbrian        mp->inbufs = mp->inbufs->m_nextpkt;
53636285Sbrian        len = mp_ReadHeader(mp, *frag, &h);
53736285Sbrian        if (first == -1)
53836285Sbrian          first = h.seq;
53954912Sbrian        (*frag)->m_offset += len;
54054912Sbrian        (*frag)->m_len -= len;
54154912Sbrian        (*frag)->m_nextpkt = NULL;
54236285Sbrian        if (frag == &q && !h.begin) {
54336285Sbrian          log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
54436285Sbrian                    (u_long)h.seq);
54554912Sbrian          m_freem(q);
54636285Sbrian          q = NULL;
54736285Sbrian        } else if (frag != &q && h.begin) {
54836285Sbrian          log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
54936285Sbrian                    (u_long)h.seq - 1);
55036285Sbrian          /*
55136285Sbrian           * Stuff our fragment back at the front of the queue and zap
55236285Sbrian           * our half-assembed packet.
55336285Sbrian           */
55454912Sbrian          (*frag)->m_nextpkt = mp->inbufs;
55536285Sbrian          mp->inbufs = *frag;
55636285Sbrian          *frag = NULL;
55754912Sbrian          m_freem(q);
55836285Sbrian          q = NULL;
55936285Sbrian          frag = &q;
56036285Sbrian          h.end = 0;	/* just in case it's a whole packet */
56136285Sbrian        } else
56236285Sbrian          do
56354912Sbrian            frag = &(*frag)->m_next;
56436285Sbrian          while (*frag != NULL);
56536285Sbrian      } while (!h.end);
56636285Sbrian
56736285Sbrian      if (q) {
56854912Sbrian        q = m_pullup(q);
56947061Sbrian        log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
57054912Sbrian                   first, (u_long)h.seq, m_length(q));
57154912Sbrian        link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
57254912Sbrian        m_freem(q);
57336285Sbrian      }
57436285Sbrian
57536285Sbrian      mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
57636285Sbrian      last = NULL;
57736285Sbrian      q = mp->inbufs;
57836285Sbrian    } else {
57936285Sbrian      /* Look for the next fragment */
58036285Sbrian      seq = inc_seq(mp->local_is12bit, seq);
58136285Sbrian      last = q;
58254912Sbrian      q = q->m_nextpkt;
58336285Sbrian    }
58436285Sbrian  }
58536285Sbrian
58636285Sbrian  if (m) {
58736285Sbrian    /* We still have to find a home for our new fragment */
58836285Sbrian    last = NULL;
58954912Sbrian    for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
59036285Sbrian      mp_ReadHeader(mp, q, &h);
59136285Sbrian      if (isbefore(mp->local_is12bit, mh.seq, h.seq))
59236285Sbrian        break;
59336285Sbrian    }
59436285Sbrian    /* Our received fragment fits in here */
59536285Sbrian    if (last)
59654912Sbrian      last->m_nextpkt = m;
59736285Sbrian    else
59836285Sbrian      mp->inbufs = m;
59954912Sbrian    m->m_nextpkt = q;
60036285Sbrian  }
60136285Sbrian}
60236285Sbrian
60346686Sbrianstruct mbuf *
60446686Sbrianmp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
60546686Sbrian{
60646686Sbrian  struct physical *p = link2physical(l);
60746686Sbrian
60846686Sbrian  if (!bundle->ncp.mp.active)
60946686Sbrian    /* Let someone else deal with it ! */
61046686Sbrian    return bp;
61146686Sbrian
61246686Sbrian  if (p == NULL) {
61346686Sbrian    log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
61454912Sbrian    m_freem(bp);
61547695Sbrian  } else {
61654912Sbrian    m_settype(bp, MB_MPIN);
61746686Sbrian    mp_Assemble(&bundle->ncp.mp, bp, p);
61847695Sbrian  }
61946686Sbrian
62046686Sbrian  return NULL;
62146686Sbrian}
62246686Sbrian
62336285Sbrianstatic void
62446686Sbrianmp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
62546686Sbrian          struct mbuf *m, u_int32_t begin, u_int32_t end)
62636285Sbrian{
62754912Sbrian  char prepend[4];
62836285Sbrian
62936285Sbrian  /* Stuff an MP header on the front of our packet and send it */
63054912Sbrian
63136285Sbrian  if (mp->peer_is12bit) {
63238814Sbrian    u_int16_t val;
63336285Sbrian
63438814Sbrian    val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
63554912Sbrian    ua_htons(&val, prepend);
63654912Sbrian    m = m_prepend(m, prepend, 2, 0);
63736285Sbrian  } else {
63838814Sbrian    u_int32_t val;
63936285Sbrian
64038814Sbrian    val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
64154912Sbrian    ua_htonl(&val, prepend);
64254912Sbrian    m = m_prepend(m, prepend, 4, 0);
64336285Sbrian  }
64436285Sbrian  if (log_IsKept(LogDEBUG))
64536285Sbrian    log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
64654912Sbrian               mp->out.seq, m_length(m), l->name);
64736285Sbrian  mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
64836285Sbrian
64954912Sbrian  link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
65036285Sbrian}
65136285Sbrian
65236285Sbrianint
65336285Sbrianmp_FillQueues(struct bundle *bundle)
65436285Sbrian{
65536285Sbrian  struct mp *mp = &bundle->ncp.mp;
65636285Sbrian  struct datalink *dl, *fdl;
65754912Sbrian  size_t total, add, len;
65854912Sbrian  int thislink, nlinks;
65936285Sbrian  u_int32_t begin, end;
66036285Sbrian  struct mbuf *m, *mo;
66136285Sbrian
66236285Sbrian  thislink = nlinks = 0;
66336285Sbrian  for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
66436312Sbrian    /* Include non-open links here as mp->out.link will stay more correct */
66536285Sbrian    if (!fdl) {
66636285Sbrian      if (thislink == mp->out.link)
66736285Sbrian        fdl = dl;
66836285Sbrian      else
66936285Sbrian        thislink++;
67036285Sbrian    }
67136285Sbrian    nlinks++;
67236285Sbrian  }
67336285Sbrian
67436285Sbrian  if (!fdl) {
67536285Sbrian    fdl = bundle->links;
67636285Sbrian    if (!fdl)
67736285Sbrian      return 0;
67836285Sbrian    thislink = 0;
67936285Sbrian  }
68036285Sbrian
68136285Sbrian  total = 0;
68236285Sbrian  for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
68336285Sbrian    if (!dl) {
68436285Sbrian      dl = bundle->links;
68536285Sbrian      thislink = 0;
68636285Sbrian    }
68736285Sbrian
68836285Sbrian    if (dl->state != DATALINK_OPEN)
68936285Sbrian      continue;
69036285Sbrian
69136285Sbrian    if (dl->physical->out)
69236285Sbrian      /* this link has suffered a short write.  Let it continue */
69336285Sbrian      continue;
69436285Sbrian
69536285Sbrian    add = link_QueueLen(&dl->physical->link);
69664780Sbrian    if (add) {
69736285Sbrian      /* this link has got stuff already queued.  Let it continue */
69864780Sbrian      total += add;
69936285Sbrian      continue;
70064780Sbrian    }
70136285Sbrian
70264780Sbrian    if (!link_QueueLen(&mp->link)) {
70364780Sbrian      struct datalink *other;
70464780Sbrian      int mrutoosmall;
70536285Sbrian
70664780Sbrian      /*
70764780Sbrian       * If there's only a single open link in our bundle and we haven't got
70864780Sbrian       * MP level link compression, queue outbound traffic directly via that
70964780Sbrian       * link's protocol stack rather than using the MP link.  This results
71064780Sbrian       * in the outbound traffic going out as PROTO_IP rather than PROTO_MP.
71164780Sbrian       */
71264780Sbrian      for (other = dl->next; other; other = other->next)
71364780Sbrian        if (other->state == DATALINK_OPEN)
71464780Sbrian          break;
71536285Sbrian
71664780Sbrian      mrutoosmall = 0;
71764780Sbrian      if (!other) {
71864780Sbrian        if (dl->physical->link.lcp.his_mru < mp->peer_mrru) {
71964780Sbrian          /*
72064780Sbrian           * Actually, forget it.  This test is done against the MRRU rather
72164780Sbrian           * than the packet size so that we don't end up sending some data
72264780Sbrian           * in MP fragments and some data in PROTO_IP packets.  That's just
72364780Sbrian           * too likely to upset some ppp implementations.
72464780Sbrian           */
72564780Sbrian          mrutoosmall = 1;
72664780Sbrian          other = dl;
72736285Sbrian        }
72836285Sbrian      }
72936285Sbrian
73064780Sbrian      if (!ip_PushPacket(other ? &mp->link : &dl->physical->link, bundle))
73164780Sbrian        /* Nothing else to send */
73264780Sbrian        break;
73364780Sbrian
73464780Sbrian      if (mrutoosmall)
73564780Sbrian        log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n");
73664780Sbrian      else if (!other)
73764780Sbrian        log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n");
73864780Sbrian
73964780Sbrian      if (!other) {
74064780Sbrian        add = link_QueueLen(&dl->physical->link);
74164780Sbrian        if (add) {
74264780Sbrian          /* this link has got stuff already queued.  Let it continue */
74364780Sbrian          total += add;
74464780Sbrian          continue;
74564780Sbrian        }
74636285Sbrian      }
74736285Sbrian    }
74864780Sbrian
74964780Sbrian    m = link_Dequeue(&mp->link);
75064780Sbrian    if (m) {
75164780Sbrian      len = m_length(m);
75264780Sbrian      begin = 1;
75364780Sbrian      end = 0;
75464780Sbrian
75564780Sbrian      while (!end) {
75664780Sbrian        if (dl->state == DATALINK_OPEN) {
75764780Sbrian          /* Write at most his_mru bytes to the physical link */
75864780Sbrian          if (len <= dl->physical->link.lcp.his_mru) {
75964780Sbrian            mo = m;
76064780Sbrian            end = 1;
76164780Sbrian            m_settype(mo, MB_MPOUT);
76264780Sbrian          } else {
76364780Sbrian            /* It's > his_mru, chop the packet (`m') into bits */
76464780Sbrian            mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
76564780Sbrian            len -= mo->m_len;
76664780Sbrian            m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
76764780Sbrian          }
76864780Sbrian          mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
76964780Sbrian          begin = 0;
77064780Sbrian        }
77164780Sbrian
77264780Sbrian        if (!end) {
77364780Sbrian          nlinks--;
77464780Sbrian          dl = dl->next;
77564780Sbrian          if (!dl) {
77664780Sbrian            dl = bundle->links;
77764780Sbrian            thislink = 0;
77864780Sbrian          } else
77964780Sbrian            thislink++;
78064780Sbrian        }
78164780Sbrian      }
78264780Sbrian    }
78336285Sbrian  }
78436285Sbrian  mp->out.link = thislink;		/* Start here next time */
78536285Sbrian
78636285Sbrian  return total;
78736285Sbrian}
78836285Sbrian
78936285Sbrianint
79049434Sbrianmp_SetDatalinkBandwidth(struct cmdargs const *arg)
79136285Sbrian{
79236285Sbrian  int val;
79336285Sbrian
79436285Sbrian  if (arg->argc != arg->argn+1)
79536285Sbrian    return -1;
79664780Sbrian
79736285Sbrian  val = atoi(arg->argv[arg->argn]);
79849434Sbrian  if (val <= 0) {
79949434Sbrian    log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
80036285Sbrian    return 1;
80136285Sbrian  }
80249434Sbrian  arg->cx->mp.bandwidth = val;
80349434Sbrian
80449434Sbrian  if (arg->cx->state == DATALINK_OPEN)
80549434Sbrian    bundle_CalculateBandwidth(arg->bundle);
80649434Sbrian
80736285Sbrian  return 0;
80836285Sbrian}
80936285Sbrian
81036285Sbrianint
81136285Sbrianmp_ShowStatus(struct cmdargs const *arg)
81236285Sbrian{
81336285Sbrian  struct mp *mp = &arg->bundle->ncp.mp;
81436285Sbrian
81536285Sbrian  prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
81636285Sbrian  if (mp->active) {
81747695Sbrian    struct mbuf *m, *lm;
81836285Sbrian    int bufs = 0;
81936285Sbrian
82047700Sbrian    lm = NULL;
82136285Sbrian    prompt_Printf(arg->prompt, "Socket:         %s\n",
82236285Sbrian                  mp->server.socket.sun_path);
82354912Sbrian    for (m = mp->inbufs; m; m = m->m_nextpkt) {
82436285Sbrian      bufs++;
82547695Sbrian      lm = m;
82647695Sbrian    }
82747695Sbrian    prompt_Printf(arg->prompt, "Pending frags:  %d", bufs);
82847695Sbrian    if (bufs) {
82947695Sbrian      struct mp_header mh;
83047695Sbrian      unsigned long first, last;
83147695Sbrian
83247695Sbrian      first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
83347695Sbrian      last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
83447712Sbrian      prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
83547695Sbrian                    first, last, (unsigned long)mp->seq.next_in,
83647695Sbrian                    (unsigned long)mp->seq.min_in);
83747712Sbrian      prompt_Printf(arg->prompt, "                First has %sbegin bit and "
83847712Sbrian                    "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
83947695Sbrian    }
84047695Sbrian    prompt_Printf(arg->prompt, "\n");
84136285Sbrian  }
84236285Sbrian
84336285Sbrian  prompt_Printf(arg->prompt, "\nMy Side:\n");
84436285Sbrian  if (mp->active) {
84549434Sbrian    prompt_Printf(arg->prompt, " Output SEQ:    %u\n", mp->out.seq);
84636285Sbrian    prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->local_mrru);
84736285Sbrian    prompt_Printf(arg->prompt, " Short Seq:     %s\n",
84836285Sbrian                  mp->local_is12bit ? "on" : "off");
84936285Sbrian  }
85036285Sbrian  prompt_Printf(arg->prompt, " Discriminator: %s\n",
85136285Sbrian                mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
85236285Sbrian                           mp->cfg.enddisc.len));
85336285Sbrian
85436285Sbrian  prompt_Printf(arg->prompt, "\nHis Side:\n");
85536285Sbrian  if (mp->active) {
85636285Sbrian    prompt_Printf(arg->prompt, " Auth Name:     %s\n", mp->peer.authname);
85749434Sbrian    prompt_Printf(arg->prompt, " Input SEQ:     %u\n", mp->seq.next_in);
85836285Sbrian    prompt_Printf(arg->prompt, " MRRU:          %u\n", mp->peer_mrru);
85936285Sbrian    prompt_Printf(arg->prompt, " Short Seq:     %s\n",
86036285Sbrian                  mp->peer_is12bit ? "on" : "off");
86136285Sbrian  }
86236285Sbrian  prompt_Printf(arg->prompt,   " Discriminator: %s\n",
86336285Sbrian                mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
86436285Sbrian                           mp->peer.enddisc.len));
86536285Sbrian
86636285Sbrian  prompt_Printf(arg->prompt, "\nDefaults:\n");
86764780Sbrian
86836285Sbrian  prompt_Printf(arg->prompt, " MRRU:          ");
86936285Sbrian  if (mp->cfg.mrru)
87036285Sbrian    prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
87136285Sbrian  else
87236285Sbrian    prompt_Printf(arg->prompt, "disabled\n");
87336285Sbrian  prompt_Printf(arg->prompt, " Short Seq:     %s\n",
87436285Sbrian                  command_ShowNegval(mp->cfg.shortseq));
87547858Sbrian  prompt_Printf(arg->prompt, " Discriminator: %s\n",
87647858Sbrian                  command_ShowNegval(mp->cfg.negenddisc));
87749434Sbrian  prompt_Printf(arg->prompt, " AutoLoad:      min %d%%, max %d%%,"
87849434Sbrian                " period %d secs\n", mp->cfg.autoload.min,
87949434Sbrian                mp->cfg.autoload.max, mp->cfg.autoload.period);
88036285Sbrian
88136285Sbrian  return 0;
88236285Sbrian}
88336285Sbrian
88436285Sbrianconst char *
88536285Sbrianmp_Enddisc(u_char c, const char *address, int len)
88636285Sbrian{
88737010Sbrian  static char result[100];	/* Used immediately after it's returned */
88836285Sbrian  int f, header;
88936285Sbrian
89036285Sbrian  switch (c) {
89136285Sbrian    case ENDDISC_NULL:
89236285Sbrian      sprintf(result, "Null Class");
89336285Sbrian      break;
89436285Sbrian
89536285Sbrian    case ENDDISC_LOCAL:
89636285Sbrian      snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
89736285Sbrian      break;
89836285Sbrian
89936285Sbrian    case ENDDISC_IP:
90036285Sbrian      if (len == 4)
90136285Sbrian        snprintf(result, sizeof result, "IP %s",
90236285Sbrian                 inet_ntoa(*(const struct in_addr *)address));
90336285Sbrian      else
90436285Sbrian        sprintf(result, "IP[%d] ???", len);
90536285Sbrian      break;
90636285Sbrian
90736285Sbrian    case ENDDISC_MAC:
90836285Sbrian      if (len == 6) {
90936285Sbrian        const u_char *m = (const u_char *)address;
91036285Sbrian        snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
91136285Sbrian                 m[0], m[1], m[2], m[3], m[4], m[5]);
91236285Sbrian      } else
91336285Sbrian        sprintf(result, "MAC[%d] ???", len);
91436285Sbrian      break;
91536285Sbrian
91636285Sbrian    case ENDDISC_MAGIC:
91736285Sbrian      sprintf(result, "Magic: 0x");
91836285Sbrian      header = strlen(result);
91936285Sbrian      if (len > sizeof result - header - 1)
92036285Sbrian        len = sizeof result - header - 1;
92136285Sbrian      for (f = 0; f < len; f++)
92236285Sbrian        sprintf(result + header + 2 * f, "%02x", address[f]);
92336285Sbrian      break;
92436285Sbrian
92536285Sbrian    case ENDDISC_PSN:
92636285Sbrian      snprintf(result, sizeof result, "PSN: %.*s", len, address);
92736285Sbrian      break;
92836285Sbrian
92936285Sbrian     default:
93036285Sbrian      sprintf(result, "%d: ", (int)c);
93136285Sbrian      header = strlen(result);
93236285Sbrian      if (len > sizeof result - header - 1)
93336285Sbrian        len = sizeof result - header - 1;
93436285Sbrian      for (f = 0; f < len; f++)
93536285Sbrian        sprintf(result + header + 2 * f, "%02x", address[f]);
93636285Sbrian      break;
93736285Sbrian  }
93836285Sbrian  return result;
93936285Sbrian}
94036285Sbrian
94136285Sbrianint
94236285Sbrianmp_SetEnddisc(struct cmdargs const *arg)
94336285Sbrian{
94436285Sbrian  struct mp *mp = &arg->bundle->ncp.mp;
94536285Sbrian  struct in_addr addr;
94636285Sbrian
94740622Sbrian  switch (bundle_Phase(arg->bundle)) {
94840622Sbrian    case PHASE_DEAD:
94940622Sbrian      break;
95040622Sbrian    case PHASE_ESTABLISH:
95140622Sbrian      /* Make sure none of our links are DATALINK_LCP or greater */
95240622Sbrian      if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
95340622Sbrian        log_Printf(LogWARN, "enddisc: Only changable before"
95440622Sbrian                   " LCP negotiations\n");
95540622Sbrian        return 1;
95640622Sbrian      }
95740622Sbrian      break;
95840622Sbrian    default:
95940622Sbrian      log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
96040622Sbrian      return 1;
96136285Sbrian  }
96236285Sbrian
96336285Sbrian  if (arg->argc == arg->argn) {
96436285Sbrian    mp->cfg.enddisc.class = 0;
96536285Sbrian    *mp->cfg.enddisc.address = '\0';
96636285Sbrian    mp->cfg.enddisc.len = 0;
96736285Sbrian  } else if (arg->argc > arg->argn) {
96836285Sbrian    if (!strcasecmp(arg->argv[arg->argn], "label")) {
96936285Sbrian      mp->cfg.enddisc.class = ENDDISC_LOCAL;
97036285Sbrian      strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
97136285Sbrian      mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
97236285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
97336285Sbrian      if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
97436285Sbrian        addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
97536285Sbrian      else
97636285Sbrian        addr = arg->bundle->ncp.ipcp.my_ip;
97736285Sbrian      memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
97836285Sbrian      mp->cfg.enddisc.class = ENDDISC_IP;
97936285Sbrian      mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
98036285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
98136285Sbrian      struct sockaddr_dl hwaddr;
98236285Sbrian      int s;
98336285Sbrian
98436285Sbrian      if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
98536285Sbrian        addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
98636285Sbrian      else
98736285Sbrian        addr = arg->bundle->ncp.ipcp.my_ip;
98836285Sbrian
98936285Sbrian      s = ID0socket(AF_INET, SOCK_DGRAM, 0);
99036285Sbrian      if (s < 0) {
99136285Sbrian        log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
99236285Sbrian        return 2;
99336285Sbrian      }
99436285Sbrian      if (get_ether_addr(s, addr, &hwaddr)) {
99536285Sbrian        mp->cfg.enddisc.class = ENDDISC_MAC;
99636285Sbrian        memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
99736285Sbrian               hwaddr.sdl_alen);
99836285Sbrian        mp->cfg.enddisc.len = hwaddr.sdl_alen;
99936285Sbrian      } else {
100036285Sbrian        log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
100136285Sbrian                  inet_ntoa(addr));
100236285Sbrian        close(s);
100336285Sbrian        return 4;
100436285Sbrian      }
100536285Sbrian      close(s);
100636285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
100736285Sbrian      int f;
100836285Sbrian
100936285Sbrian      randinit();
101036285Sbrian      for (f = 0; f < 20; f += sizeof(long))
101136285Sbrian        *(long *)(mp->cfg.enddisc.address + f) = random();
101236285Sbrian      mp->cfg.enddisc.class = ENDDISC_MAGIC;
101336285Sbrian      mp->cfg.enddisc.len = 20;
101436285Sbrian    } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
101536285Sbrian      if (arg->argc > arg->argn+1) {
101636285Sbrian        mp->cfg.enddisc.class = ENDDISC_PSN;
101736285Sbrian        strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
101836285Sbrian        mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
101936285Sbrian      } else {
102036285Sbrian        log_Printf(LogWARN, "PSN endpoint requires additional data\n");
102136285Sbrian        return 5;
102236285Sbrian      }
102336285Sbrian    } else {
102436285Sbrian      log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
102536285Sbrian                arg->argv[arg->argn]);
102636285Sbrian      return 6;
102736285Sbrian    }
102836285Sbrian  }
102936285Sbrian
103036285Sbrian  return 0;
103136285Sbrian}
103236285Sbrian
103336285Sbrianstatic int
103458028Sbrianmpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
103536285Sbrian                   int *n)
103636285Sbrian{
103736285Sbrian  struct mpserver *s = descriptor2mpserver(d);
103836314Sbrian  int result;
103936285Sbrian
104036314Sbrian  result = 0;
104136285Sbrian  if (s->send.dl != NULL) {
104236285Sbrian    /* We've connect()ed */
104336285Sbrian    if (!link_QueueLen(&s->send.dl->physical->link) &&
104436285Sbrian        !s->send.dl->physical->out) {
104536285Sbrian      /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
104636314Sbrian      result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
104736285Sbrian      bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
104836285Sbrian      s->send.dl = NULL;
104936285Sbrian      s->fd = -1;
105036285Sbrian    } else
105136285Sbrian      /* Never read from a datalink that's on death row ! */
105236314Sbrian      result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
105336285Sbrian  } else if (r && s->fd >= 0) {
105436285Sbrian    if (*n < s->fd + 1)
105536285Sbrian      *n = s->fd + 1;
105636285Sbrian    FD_SET(s->fd, r);
105736285Sbrian    log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
105836314Sbrian    result++;
105936285Sbrian  }
106036314Sbrian  return result;
106136285Sbrian}
106236285Sbrian
106336285Sbrianstatic int
106458028Sbrianmpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
106536285Sbrian{
106636285Sbrian  struct mpserver *s = descriptor2mpserver(d);
106736285Sbrian  return s->fd >= 0 && FD_ISSET(s->fd, fdset);
106836285Sbrian}
106936285Sbrian
107036285Sbrianstatic void
107158028Sbrianmpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
107236285Sbrian{
107336285Sbrian  struct mpserver *s = descriptor2mpserver(d);
107436285Sbrian
107553684Sbrian  bundle_ReceiveDatalink(bundle, s->fd);
107636285Sbrian}
107736285Sbrian
107837141Sbrianstatic int
107958028Sbrianmpserver_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
108036285Sbrian{
108136285Sbrian  /* We never want to write here ! */
108237019Sbrian  log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
108337141Sbrian  return 0;
108436285Sbrian}
108536285Sbrian
108636285Sbrianvoid
108736285Sbrianmpserver_Init(struct mpserver *s)
108836285Sbrian{
108936285Sbrian  s->desc.type = MPSERVER_DESCRIPTOR;
109036285Sbrian  s->desc.UpdateSet = mpserver_UpdateSet;
109136285Sbrian  s->desc.IsSet = mpserver_IsSet;
109236285Sbrian  s->desc.Read = mpserver_Read;
109336285Sbrian  s->desc.Write = mpserver_Write;
109436285Sbrian  s->send.dl = NULL;
109536285Sbrian  s->fd = -1;
109636285Sbrian  memset(&s->socket, '\0', sizeof s->socket);
109736285Sbrian}
109836285Sbrian
109936285Sbrianint
110036285Sbrianmpserver_Open(struct mpserver *s, struct peerid *peer)
110136285Sbrian{
110253970Sbrian  int f, l;
110336285Sbrian  mode_t mask;
110436285Sbrian
110536285Sbrian  if (s->fd != -1) {
110637019Sbrian    log_Printf(LogALERT, "Internal error !  mpserver already open\n");
110736285Sbrian    mpserver_Close(s);
110836285Sbrian  }
110936285Sbrian
111036285Sbrian  l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
111136285Sbrian               _PATH_VARRUN, peer->authname, peer->enddisc.class);
111236285Sbrian
111336285Sbrian  for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
111436285Sbrian    snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
111536285Sbrian             "%02x", *(u_char *)(peer->enddisc.address+f));
111636285Sbrian    l += 2;
111736285Sbrian  }
111836285Sbrian
111936285Sbrian  s->socket.sun_family = AF_LOCAL;
112036285Sbrian  s->socket.sun_len = sizeof s->socket;
112153684Sbrian  s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
112236285Sbrian  if (s->fd < 0) {
112353684Sbrian    log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
112436285Sbrian    return MPSERVER_FAILED;
112536285Sbrian  }
112636285Sbrian
112736285Sbrian  setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
112836285Sbrian             sizeof s->socket);
112936285Sbrian  mask = umask(0177);
113053684Sbrian
113153684Sbrian  /*
113253970Sbrian   * Try to bind the socket.  If we succeed we play server, if we fail
113353970Sbrian   * we connect() and hand the link off.
113453684Sbrian   */
113553684Sbrian
113636285Sbrian  if (ID0bind_un(s->fd, &s->socket) < 0) {
113736285Sbrian    if (errno != EADDRINUSE) {
113836285Sbrian      log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
113936285Sbrian                s->socket.sun_path, strerror(errno));
114036285Sbrian      umask(mask);
114136285Sbrian      close(s->fd);
114236285Sbrian      s->fd = -1;
114336285Sbrian      return MPSERVER_FAILED;
114436285Sbrian    }
114553684Sbrian
114653970Sbrian    /* So we're the sender */
114736285Sbrian    umask(mask);
114836285Sbrian    if (ID0connect_un(s->fd, &s->socket) < 0) {
114936285Sbrian      log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
115036285Sbrian                s->socket.sun_path, strerror(errno));
115136285Sbrian      if (errno == ECONNREFUSED)
115253684Sbrian        log_Printf(LogPHASE, "          The previous server died badly !\n");
115336285Sbrian      close(s->fd);
115436285Sbrian      s->fd = -1;
115536285Sbrian      return MPSERVER_FAILED;
115636285Sbrian    }
115736285Sbrian
115836285Sbrian    /* Donate our link to the other guy */
115936285Sbrian    return MPSERVER_CONNECTED;
116036285Sbrian  }
116136285Sbrian
116236285Sbrian  return MPSERVER_LISTENING;
116336285Sbrian}
116436285Sbrian
116536285Sbrianvoid
116636285Sbrianmpserver_Close(struct mpserver *s)
116736285Sbrian{
116836285Sbrian  if (s->send.dl != NULL) {
116936285Sbrian    bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
117036285Sbrian    s->send.dl = NULL;
117136285Sbrian    s->fd = -1;
117236285Sbrian  } else if (s->fd >= 0) {
117336285Sbrian    close(s->fd);
117436285Sbrian    if (ID0unlink(s->socket.sun_path) == -1)
117536285Sbrian      log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
117636285Sbrian                strerror(errno));
117736285Sbrian    memset(&s->socket, '\0', sizeof s->socket);
117836285Sbrian    s->fd = -1;
117936285Sbrian  }
118036285Sbrian}
118136312Sbrian
118236312Sbrianvoid
118336312Sbrianmp_LinkLost(struct mp *mp, struct datalink *dl)
118436312Sbrian{
118536312Sbrian  if (mp->seq.min_in == dl->mp.seq)
118636312Sbrian    /* We've lost the link that's holding everything up ! */
118746686Sbrian    mp_Assemble(mp, NULL, NULL);
118836312Sbrian}
118938544Sbrian
119038544Sbrianvoid
119138544Sbrianmp_DeleteQueue(struct mp *mp)
119238544Sbrian{
119338544Sbrian  link_DeleteQueue(&mp->link);
119438544Sbrian}
1195