1/*-
2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 *                           Internet Initiative Japan, Inc (IIJ)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31#include <sys/param.h>
32
33#ifdef __FreeBSD__
34#include <netinet/in.h>
35#endif
36#include <sys/un.h>
37
38#include <string.h>
39#include <termios.h>
40
41#include "layer.h"
42#include "mbuf.h"
43#include "log.h"
44#include "defs.h"
45#include "timer.h"
46#include "fsm.h"
47#include "acf.h"
48#include "proto.h"
49#include "lqr.h"
50#include "hdlc.h"
51#include "lcp.h"
52#include "async.h"
53#include "throughput.h"
54#include "ccp.h"
55#include "link.h"
56#include "descriptor.h"
57#include "physical.h"
58#include "mp.h"
59#include "chat.h"
60#include "auth.h"
61#include "chap.h"
62#include "command.h"
63#include "cbcp.h"
64#include "datalink.h"
65
66struct echolqr {
67  u_int32_t magic;
68  u_int32_t signature;
69  u_int32_t sequence;
70};
71
72#define	SIGNATURE  0x594e4f54
73
74static void
75SendEchoReq(struct lcp *lcp)
76{
77  struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc;
78  struct echolqr echo;
79
80  echo.magic = htonl(lcp->want_magic);
81  echo.signature = htonl(SIGNATURE);
82  echo.sequence = htonl(hdlc->lqm.echo.seq_sent);
83  fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++,
84            (u_char *)&echo, sizeof echo, MB_ECHOOUT);
85}
86
87struct mbuf *
88lqr_RecvEcho(struct fsm *fp, struct mbuf *bp)
89{
90  struct hdlc *hdlc = &link2physical(fp->link)->hdlc;
91  struct lcp *lcp = fsm2lcp(fp);
92  struct echolqr lqr;
93
94  if (m_length(bp) >= sizeof lqr) {
95    m_freem(mbuf_Read(bp, &lqr, sizeof lqr));
96    bp = NULL;
97    lqr.magic = ntohl(lqr.magic);
98    lqr.signature = ntohl(lqr.signature);
99    lqr.sequence = ntohl(lqr.sequence);
100
101    /* Tolerate echo replies with either magic number */
102    if (lqr.magic != 0 && lqr.magic != lcp->his_magic &&
103        lqr.magic != lcp->want_magic) {
104      log_Printf(LogWARN, "%s: lqr_RecvEcho: Bad magic: expected 0x%08x,"
105                 " got 0x%08x\n", fp->link->name, lcp->his_magic, lqr.magic);
106      /*
107       * XXX: We should send a terminate request. But poor implementations may
108       *      die as a result.
109       */
110    }
111    if (lqr.signature == SIGNATURE
112	|| lqr.signature == lcp->want_magic) {			/* some implementations return the wrong magic */
113      /* careful not to update lqm.echo.seq_recv with older values */
114      if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && lqr.sequence < 5) ||
115          (hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 &&
116           lqr.sequence > hdlc->lqm.echo.seq_recv))
117        hdlc->lqm.echo.seq_recv = lqr.sequence;
118    } else
119      log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n",
120                (u_long)lqr.signature, (u_long)SIGNATURE);
121  } else
122    log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %zd, expecting %ld !\n",
123              m_length(bp), (long)sizeof(struct echolqr));
124  return bp;
125}
126
127void
128lqr_ChangeOrder(struct lqrdata *src, struct lqrdata *dst)
129{
130  u_int32_t *sp, *dp;
131  unsigned n;
132
133  sp = (u_int32_t *) src;
134  dp = (u_int32_t *) dst;
135  for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++, sp++, dp++)
136    *dp = ntohl(*sp);
137}
138
139static void
140SendLqrData(struct lcp *lcp)
141{
142  struct mbuf *bp;
143  int extra;
144
145  extra = proto_WrapperOctets(lcp, PROTO_LQR) +
146          acf_WrapperOctets(lcp, PROTO_LQR);
147  bp = m_get(sizeof(struct lqrdata) + extra, MB_LQROUT);
148  bp->m_len -= extra;
149  bp->m_offset += extra;
150
151  /*
152   * Send on the highest priority queue.  We send garbage - the real data
153   * is written by lqr_LayerPush() where we know how to fill in all the
154   * fields.  Note, lqr_LayerPush() ``knows'' that we're pushing onto the
155   * highest priority queue, and factors out packet & octet values from
156   * other queues!
157   */
158  link_PushPacket(lcp->fsm.link, bp, lcp->fsm.bundle,
159                  LINK_QUEUES(lcp->fsm.link) - 1, PROTO_LQR);
160}
161
162static void
163SendLqrReport(void *v)
164{
165  struct lcp *lcp = (struct lcp *)v;
166  struct physical *p = link2physical(lcp->fsm.link);
167
168  timer_Stop(&p->hdlc.lqm.timer);
169
170  if (p->hdlc.lqm.method & LQM_LQR) {
171    if (p->hdlc.lqm.lqr.resent > 5) {
172      /* XXX: Should implement LQM strategy */
173      log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n",
174                lcp->fsm.link->name);
175      log_Printf(LogLQM, "%s: Too many LQR packets lost\n",
176                lcp->fsm.link->name);
177      p->hdlc.lqm.method = 0;
178      datalink_Down(p->dl, CLOSE_NORMAL);
179    } else {
180      SendLqrData(lcp);
181      p->hdlc.lqm.lqr.resent++;
182    }
183  } else if (p->hdlc.lqm.method & LQM_ECHO) {
184    if ((p->hdlc.lqm.echo.seq_sent > 5 &&
185         p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) ||
186        (p->hdlc.lqm.echo.seq_sent <= 5 &&
187         p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) {
188      log_Printf(LogPHASE, "%s: ** Too many LCP ECHO packets lost **\n",
189                lcp->fsm.link->name);
190      log_Printf(LogLQM, "%s: Too many LCP ECHO packets lost\n",
191                lcp->fsm.link->name);
192      p->hdlc.lqm.method = 0;
193      datalink_Down(p->dl, CLOSE_NORMAL);
194    } else
195      SendEchoReq(lcp);
196  }
197  if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
198    timer_Start(&p->hdlc.lqm.timer);
199}
200
201struct mbuf *
202lqr_Input(struct bundle *bundle __unused, struct link *l, struct mbuf *bp)
203{
204  struct physical *p = link2physical(l);
205  struct lcp *lcp = p->hdlc.lqm.owner;
206  int len;
207
208  if (p == NULL) {
209    log_Printf(LogERROR, "lqr_Input: Not a physical link - dropped\n");
210    m_freem(bp);
211    return NULL;
212  }
213
214  len = m_length(bp);
215  if (len != sizeof(struct lqrdata))
216    log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n",
217              len, (long)sizeof(struct lqrdata));
218  else if (!IsAccepted(l->lcp.cfg.lqr) && !(p->hdlc.lqm.method & LQM_LQR)) {
219    bp = m_pullup(proto_Prepend(bp, PROTO_LQR, 0, 0));
220    lcp_SendProtoRej(lcp, MBUF_CTOP(bp), bp->m_len);
221  } else {
222    struct lqrdata *lqr;
223
224    bp = m_pullup(bp);
225    lqr = (struct lqrdata *)MBUF_CTOP(bp);
226    if (ntohl(lqr->MagicNumber) != lcp->his_magic)
227      log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong,"
228                 " expecting 0x%08lx\n",
229		 (u_long)ntohl(lqr->MagicNumber), (u_long)lcp->his_magic);
230    else {
231      struct lqrdata lastlqr;
232
233      memcpy(&lastlqr, &p->hdlc.lqm.lqr.peer, sizeof lastlqr);
234      lqr_ChangeOrder(lqr, &p->hdlc.lqm.lqr.peer);
235      lqr_Dump(l->name, "Input", &p->hdlc.lqm.lqr.peer);
236      /* we have received an LQR from our peer */
237      p->hdlc.lqm.lqr.resent = 0;
238
239      /* Snapshot our state when the LQR packet was received */
240      memcpy(&p->hdlc.lqm.lqr.prevSave, &p->hdlc.lqm.lqr.Save,
241             sizeof p->hdlc.lqm.lqr.prevSave);
242      p->hdlc.lqm.lqr.Save.InLQRs = ++p->hdlc.lqm.lqr.InLQRs;
243      p->hdlc.lqm.lqr.Save.InPackets = p->hdlc.lqm.ifInUniPackets;
244      p->hdlc.lqm.lqr.Save.InDiscards = p->hdlc.lqm.ifInDiscards;
245      p->hdlc.lqm.lqr.Save.InErrors = p->hdlc.lqm.ifInErrors;
246      p->hdlc.lqm.lqr.Save.InOctets = p->hdlc.lqm.lqr.InGoodOctets;
247
248      lqr_Analyse(&p->hdlc, &lastlqr, &p->hdlc.lqm.lqr.peer);
249
250      /*
251       * Generate an LQR response if we're not running an LQR timer OR
252       * two successive LQR's PeerInLQRs are the same.
253       */
254      if (p->hdlc.lqm.timer.load == 0 || !(p->hdlc.lqm.method & LQM_LQR) ||
255          (lastlqr.PeerInLQRs &&
256           lastlqr.PeerInLQRs == p->hdlc.lqm.lqr.peer.PeerInLQRs))
257        SendLqrData(lcp);
258    }
259  }
260  m_freem(bp);
261  return NULL;
262}
263
264/*
265 *  When LCP is reached to opened state, We'll start LQM activity.
266 */
267static void
268lqr_Setup(struct lcp *lcp)
269{
270  struct physical *physical = link2physical(lcp->fsm.link);
271  int period;
272
273  physical->hdlc.lqm.lqr.resent = 0;
274  physical->hdlc.lqm.echo.seq_sent = 0;
275  physical->hdlc.lqm.echo.seq_recv = 0;
276  memset(&physical->hdlc.lqm.lqr.peer, '\0',
277         sizeof physical->hdlc.lqm.lqr.peer);
278
279  physical->hdlc.lqm.method = lcp->cfg.echo ? LQM_ECHO : 0;
280  if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO))
281    physical->hdlc.lqm.method |= LQM_LQR;
282  timer_Stop(&physical->hdlc.lqm.timer);
283
284  physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod;
285  if (lcp->his_lqrperiod)
286    log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n",
287              physical->link.name, lcp->his_lqrperiod / 100,
288              lcp->his_lqrperiod % 100);
289
290  period = lcp->want_lqrperiod ?
291    lcp->want_lqrperiod : lcp->cfg.lqrperiod * 100;
292  physical->hdlc.lqm.timer.func = SendLqrReport;
293  physical->hdlc.lqm.timer.name = "lqm";
294  physical->hdlc.lqm.timer.arg = lcp;
295
296  if (lcp->want_lqrperiod || physical->hdlc.lqm.method & LQM_ECHO) {
297    log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n",
298              physical->link.name, lcp->want_lqrperiod ? "LQR" : "LCP ECHO",
299              period / 100, period % 100);
300    physical->hdlc.lqm.timer.load = period * SECTICKS / 100;
301  } else {
302    physical->hdlc.lqm.timer.load = 0;
303    if (!lcp->his_lqrperiod)
304      log_Printf(LogLQM, "%s: LQR/LCP ECHO not negotiated\n",
305                 physical->link.name);
306  }
307}
308
309void
310lqr_Start(struct lcp *lcp)
311{
312  struct physical *p = link2physical(lcp->fsm.link);
313
314  lqr_Setup(lcp);
315  if (p->hdlc.lqm.timer.load)
316    SendLqrReport(lcp);
317}
318
319void
320lqr_reStart(struct lcp *lcp)
321{
322  struct physical *p = link2physical(lcp->fsm.link);
323
324  lqr_Setup(lcp);
325  if (p->hdlc.lqm.timer.load)
326    timer_Start(&p->hdlc.lqm.timer);
327}
328
329void
330lqr_StopTimer(struct physical *physical)
331{
332  timer_Stop(&physical->hdlc.lqm.timer);
333}
334
335void
336lqr_Stop(struct physical *physical, int method)
337{
338  if (method == LQM_LQR)
339    log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n",
340               physical->link.name);
341  if (method == LQM_ECHO)
342    log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n",
343               physical->link.name);
344  physical->hdlc.lqm.method &= ~method;
345  if (physical->hdlc.lqm.method)
346    SendLqrReport(physical->hdlc.lqm.owner);
347  else
348    timer_Stop(&physical->hdlc.lqm.timer);
349}
350
351void
352lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr)
353{
354  if (log_IsKept(LogLQM)) {
355    log_Printf(LogLQM, "%s: %s:\n", link, message);
356    log_Printf(LogLQM, "  Magic:          %08x   LastOutLQRs:    %08x\n",
357	      lqr->MagicNumber, lqr->LastOutLQRs);
358    log_Printf(LogLQM, "  LastOutPackets: %08x   LastOutOctets:  %08x\n",
359	      lqr->LastOutPackets, lqr->LastOutOctets);
360    log_Printf(LogLQM, "  PeerInLQRs:     %08x   PeerInPackets:  %08x\n",
361	      lqr->PeerInLQRs, lqr->PeerInPackets);
362    log_Printf(LogLQM, "  PeerInDiscards: %08x   PeerInErrors:   %08x\n",
363	      lqr->PeerInDiscards, lqr->PeerInErrors);
364    log_Printf(LogLQM, "  PeerInOctets:   %08x   PeerOutLQRs:    %08x\n",
365	      lqr->PeerInOctets, lqr->PeerOutLQRs);
366    log_Printf(LogLQM, "  PeerOutPackets: %08x   PeerOutOctets:  %08x\n",
367	      lqr->PeerOutPackets, lqr->PeerOutOctets);
368  }
369}
370
371void
372lqr_Analyse(const struct hdlc *hdlc, const struct lqrdata *oldlqr,
373            const struct lqrdata *newlqr)
374{
375  u_int32_t LQRs, transitLQRs, pkts, octets, disc, err;
376
377  if (!newlqr->PeerInLQRs)	/* No analysis possible yet! */
378    return;
379
380  log_Printf(LogLQM, "Analysis:\n");
381
382  LQRs = (newlqr->LastOutLQRs - oldlqr->LastOutLQRs) -
383         (newlqr->PeerInLQRs - oldlqr->PeerInLQRs);
384  transitLQRs = hdlc->lqm.lqr.OutLQRs - newlqr->LastOutLQRs;
385  pkts = (newlqr->LastOutPackets - oldlqr->LastOutPackets) -
386         (newlqr->PeerInPackets - oldlqr->PeerInPackets);
387  octets = (newlqr->LastOutOctets - oldlqr->LastOutOctets) -
388           (newlqr->PeerInOctets - oldlqr->PeerInOctets);
389  log_Printf(LogLQM, "  Outbound lossage: %d LQR%s (%d en route), %d packet%s,"
390             " %d octet%s\n", (int)LQRs, LQRs == 1 ? "" : "s", (int)transitLQRs,
391	     (int)pkts, pkts == 1 ? "" : "s",
392	     (int)octets, octets == 1 ? "" : "s");
393
394  pkts = (newlqr->PeerOutPackets - oldlqr->PeerOutPackets) -
395    (hdlc->lqm.lqr.Save.InPackets - hdlc->lqm.lqr.prevSave.InPackets);
396  octets = (newlqr->PeerOutOctets - oldlqr->PeerOutOctets) -
397    (hdlc->lqm.lqr.Save.InOctets - hdlc->lqm.lqr.prevSave.InOctets);
398  log_Printf(LogLQM, "  Inbound lossage: %d packet%s, %d octet%s\n",
399	     (int)pkts, pkts == 1 ? "" : "s",
400	     (int)octets, octets == 1 ? "" : "s");
401
402  disc = newlqr->PeerInDiscards - oldlqr->PeerInDiscards;
403  err = newlqr->PeerInErrors - oldlqr->PeerInErrors;
404  if (disc && err)
405    log_Printf(LogLQM, "                   Likely due to both peer congestion"
406               " and physical errors\n");
407  else if (disc)
408    log_Printf(LogLQM, "                   Likely due to peer congestion\n");
409  else if (err)
410    log_Printf(LogLQM, "                   Likely due to physical errors\n");
411  else if (pkts)
412    log_Printf(LogLQM, "                   Likely due to transport "
413	       "congestion\n");
414}
415
416static struct mbuf *
417lqr_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
418              int pri __unused, u_short *proto)
419{
420  struct physical *p = link2physical(l);
421  int len, layer;
422
423  if (!p) {
424    /* Oops - can't happen :-] */
425    m_freem(bp);
426    return NULL;
427  }
428
429  bp = m_pullup(bp);
430  len = m_length(bp);
431
432  /*-
433   * From rfc1989:
434   *
435   *  All octets which are included in the FCS calculation MUST be counted,
436   *  including the packet header, the information field, and any padding.
437   *  The FCS octets MUST also be counted, and one flag octet per frame
438   *  MUST be counted.  All other octets (such as additional flag
439   *  sequences, and escape bits or octets) MUST NOT be counted.
440   *
441   * As we're stacked higher than the HDLC layer (otherwise HDLC wouldn't be
442   * able to calculate the FCS), we must not forget about these additional
443   * bytes when we're asynchronous.
444   *
445   * We're also expecting to be stacked *before* the likes of the proto and
446   * acf layers (to avoid alignment issues), so deal with this too.
447   */
448
449  p->hdlc.lqm.ifOutUniPackets++;
450  p->hdlc.lqm.ifOutOctets += len + 1;		/* plus 1 flag octet! */
451  for (layer = 0; layer < l->nlayers; layer++)
452    switch (l->layer[layer]->type) {
453      case LAYER_ACF:
454        p->hdlc.lqm.ifOutOctets += acf_WrapperOctets(&l->lcp, *proto);
455        break;
456      case LAYER_ASYNC:
457        /* Not included - see rfc1989 */
458        break;
459      case LAYER_HDLC:
460        p->hdlc.lqm.ifOutOctets += hdlc_WrapperOctets();
461        break;
462      case LAYER_LQR:
463        layer = l->nlayers;
464        break;
465      case LAYER_PROTO:
466        p->hdlc.lqm.ifOutOctets += proto_WrapperOctets(&l->lcp, *proto);
467        break;
468      case LAYER_SYNC:
469        /* Nothing to add on */
470        break;
471      default:
472        log_Printf(LogWARN, "Oops, don't know how to do octets for %s layer\n",
473                   l->layer[layer]->name);
474        break;
475    }
476
477  if (*proto == PROTO_LQR) {
478    /* Overwrite the entire packet (created in SendLqrData()) */
479    struct lqrdata lqr;
480    size_t pending_pkts, pending_octets;
481
482    p->hdlc.lqm.lqr.OutLQRs++;
483
484    /*
485     * We need to compensate for the fact that we're pushing our data
486     * onto the highest priority queue by factoring out packet & octet
487     * values from other queues!
488     */
489    link_PendingLowPriorityData(l, &pending_pkts, &pending_octets);
490
491    memset(&lqr, '\0', sizeof lqr);
492    lqr.MagicNumber = p->link.lcp.want_magic;
493    lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
494    lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
495    lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
496    lqr.PeerInLQRs = p->hdlc.lqm.lqr.Save.InLQRs;
497    lqr.PeerInPackets = p->hdlc.lqm.lqr.Save.InPackets;
498    lqr.PeerInDiscards = p->hdlc.lqm.lqr.Save.InDiscards;
499    lqr.PeerInErrors = p->hdlc.lqm.lqr.Save.InErrors;
500    lqr.PeerInOctets = p->hdlc.lqm.lqr.Save.InOctets;
501    lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
502    lqr.PeerOutPackets = p->hdlc.lqm.ifOutUniPackets - pending_pkts;
503    /* Don't forget our ``flag'' octets.... */
504    lqr.PeerOutOctets = p->hdlc.lqm.ifOutOctets - pending_octets - pending_pkts;
505    lqr_Dump(l->name, "Output", &lqr);
506    lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp));
507  }
508
509  return bp;
510}
511
512static struct mbuf *
513lqr_LayerPull(struct bundle *b __unused, struct link *l __unused,
514	      struct mbuf *bp, u_short *proto)
515{
516  /*
517   * This is the ``Rx'' process from rfc1989, although a part of it is
518   * actually performed by sync_LayerPull() & hdlc_LayerPull() so that
519   * our octet counts are correct.
520   */
521
522  if (*proto == PROTO_LQR)
523    m_settype(bp, MB_LQRIN);
524  return bp;
525}
526
527/*
528 * Statistics for pulled packets are recorded either in hdlc_PullPacket()
529 * or sync_PullPacket()
530 */
531
532struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull };
533