link.c revision 46686
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *  $Id: link.c,v 1.8 1999/03/31 14:21:45 brian Exp $
27 *
28 */
29
30#include <sys/types.h>
31#include <netinet/in_systm.h>
32#include <netdb.h>
33#include <sys/un.h>
34#include <netinet/in.h>
35#include <netinet/ip.h>
36
37#include <stdio.h>
38#include <string.h>
39#include <termios.h>
40
41#include "defs.h"
42#include "layer.h"
43#include "mbuf.h"
44#include "log.h"
45#include "timer.h"
46#include "lqr.h"
47#include "hdlc.h"
48#include "throughput.h"
49#include "proto.h"
50#include "fsm.h"
51#include "descriptor.h"
52#include "lcp.h"
53#include "ccp.h"
54#include "link.h"
55#include "prompt.h"
56#include "async.h"
57#include "physical.h"
58#include "mp.h"
59#include "iplist.h"
60#include "slcompress.h"
61#include "ipcp.h"
62#include "ip.h"
63#include "auth.h"
64#include "pap.h"
65#include "chap.h"
66#include "cbcp.h"
67
68static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short);
69
70void
71link_AddInOctets(struct link *l, int n)
72{
73  throughput_addin(&l->throughput, n);
74}
75
76void
77link_AddOutOctets(struct link *l, int n)
78{
79  throughput_addout(&l->throughput, n);
80}
81
82void
83link_SequenceQueue(struct link *l)
84{
85  log_Printf(LogDEBUG, "link_SequenceQueue\n");
86  while (l->Queue[PRI_NORMAL].qlen)
87    mbuf_Enqueue(l->Queue + PRI_LINK, mbuf_Dequeue(l->Queue + PRI_NORMAL));
88}
89
90void
91link_DeleteQueue(struct link *l)
92{
93  struct mqueue *queue;
94
95  for (queue = l->Queue; queue < l->Queue + LINK_QUEUES; queue++)
96    while (queue->top)
97      mbuf_Free(mbuf_Dequeue(queue));
98}
99
100int
101link_QueueLen(struct link *l)
102{
103  int i, len;
104
105  for (i = 0, len = 0; i < LINK_QUEUES; i++)
106    len += l->Queue[i].qlen;
107
108  return len;
109}
110
111int
112link_QueueBytes(struct link *l)
113{
114  int i, len, bytes;
115  struct mbuf *m;
116
117  bytes = 0;
118  for (i = 0, len = 0; i < LINK_QUEUES; i++) {
119    len = l->Queue[i].qlen;
120    m = l->Queue[i].top;
121    while (len--) {
122      bytes += mbuf_Length(m);
123      m = m->pnext;
124    }
125  }
126
127  return bytes;
128}
129
130struct mbuf *
131link_Dequeue(struct link *l)
132{
133  int pri;
134  struct mbuf *bp;
135
136  for (bp = (struct mbuf *)0, pri = LINK_QUEUES - 1; pri >= 0; pri--)
137    if (l->Queue[pri].qlen) {
138      bp = mbuf_Dequeue(l->Queue + pri);
139      log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
140                " containing %d more packets\n", pri, l->Queue[pri].qlen);
141      break;
142    }
143
144  return bp;
145}
146
147static struct protostatheader {
148  u_short number;
149  const char *name;
150} ProtocolStat[NPROTOSTAT] = {
151  { PROTO_IP, "IP" },
152  { PROTO_VJUNCOMP, "VJ_UNCOMP" },
153  { PROTO_VJCOMP, "VJ_COMP" },
154  { PROTO_COMPD, "COMPD" },
155  { PROTO_ICOMPD, "ICOMPD" },
156  { PROTO_LCP, "LCP" },
157  { PROTO_IPCP, "IPCP" },
158  { PROTO_CCP, "CCP" },
159  { PROTO_PAP, "PAP" },
160  { PROTO_LQR, "LQR" },
161  { PROTO_CHAP, "CHAP" },
162  { PROTO_MP, "MULTILINK" },
163  { 0, "Others" }
164};
165
166void
167link_ProtocolRecord(struct link *l, u_short proto, int type)
168{
169  int i;
170
171  for (i = 0; i < NPROTOSTAT; i++)
172    if (ProtocolStat[i].number == proto)
173      break;
174
175  if (type == PROTO_IN)
176    l->proto_in[i]++;
177  else
178    l->proto_out[i]++;
179}
180
181void
182link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
183{
184  int i;
185
186  prompt_Printf(prompt, "    Protocol     in        out      "
187                "Protocol      in       out\n");
188  for (i = 0; i < NPROTOSTAT; i++) {
189    prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
190	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
191    if ((i % 2) == 0)
192      prompt_Printf(prompt, "\n");
193  }
194  if (!(i % 2))
195    prompt_Printf(prompt, "\n");
196}
197
198void
199link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri,
200                u_short proto)
201{
202  int layer;
203
204  /*
205   * When we ``push'' a packet into the link, it gets processed by the
206   * ``push'' function in each layer starting at the top.
207   * We never expect the result of a ``push'' to be more than one
208   * packet (as we do with ``pull''s).
209   */
210
211  if(pri < 0 || pri >= LINK_QUEUES)
212    pri = 0;
213
214  for (layer = l->nlayers; layer && bp; layer--)
215    if (l->layer[layer - 1]->push != NULL)
216      bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto);
217
218  if (bp) {
219    log_Printf(LogDEBUG, "link_PushPacket: proto = 0x%04x\n", proto);
220    link_AddOutOctets(l, mbuf_Length(bp));
221    mbuf_Enqueue(l->Queue + pri, mbuf_Contiguous(bp));
222  }
223}
224
225void
226link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b)
227{
228  struct mbuf *bp, *lbp[LAYER_MAX], *next;
229  u_short lproto[LAYER_MAX], proto;
230  int layer;
231
232  /*
233   * When we ``pull'' a packet from the link, it gets processed by the
234   * ``pull'' function in each layer starting at the bottom.
235   * Each ``pull'' may produce multiple packets, chained together using
236   * bp->pnext.
237   * Each packet that results from each pull has to be pulled through
238   * all of the higher layers before the next resulting packet is pulled
239   * through anything; this ensures that packets that depend on the
240   * fsm state resulting from the receipt of the previous packet aren't
241   * surprised.
242   */
243
244  link_AddInOctets(l, len);
245
246  memset(lbp, '\0', sizeof lbp);
247  lbp[0] = mbuf_Alloc(len, MB_ASYNC);
248  memcpy(MBUF_CTOP(lbp[0]), buf, len);
249  lproto[0] = 0;
250  layer = 0;
251
252  while (layer || lbp[layer]) {
253    if (lbp[layer] == NULL) {
254      layer--;
255      continue;
256    }
257    bp = lbp[layer];
258    lbp[layer] = bp->pnext;
259    bp->pnext = NULL;
260    proto = lproto[layer];
261
262    if (l->layer[layer]->pull != NULL)
263      bp = (*l->layer[layer]->pull)(b, l, bp, &proto);
264
265    if (layer == l->nlayers - 1) {
266      /* We've just done the top layer, despatch the packet(s) */
267      while (bp) {
268        next = bp->pnext;
269        bp->pnext = NULL;
270        Despatch(b, l, bp, proto);
271        bp = next;
272      }
273    } else {
274      lbp[++layer] = bp;
275      lproto[layer] = proto;
276    }
277  }
278}
279
280int
281link_Stack(struct link *l, struct layer *layer)
282{
283  if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) {
284    log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n",
285               l->name, layer->name);
286    return 0;
287  }
288  l->layer[l->nlayers++] = layer;
289  return 1;
290}
291
292void
293link_EmptyStack(struct link *l)
294{
295  l->nlayers = 0;
296}
297
298static const struct {
299  u_short proto;
300  struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *);
301} despatcher[] = {
302  { PROTO_IP, ip_Input },
303  { PROTO_MP, mp_Input },
304  { PROTO_LCP, lcp_Input },
305  { PROTO_IPCP, ipcp_Input },
306  { PROTO_PAP, pap_Input },
307  { PROTO_CHAP, chap_Input },
308  { PROTO_CCP, ccp_Input },
309  { PROTO_LQR, lqr_Input },
310  { PROTO_CBCP, cbcp_Input }
311};
312
313#define DSIZE (sizeof despatcher / sizeof despatcher[0])
314
315static void
316Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto)
317{
318  int f;
319
320  for (f = 0; f < DSIZE; f++)
321    if (despatcher[f].proto == proto) {
322      bp = (*despatcher[f].fn)(bundle, l, bp);
323      break;
324    }
325
326  if (bp) {
327    struct physical *p = link2physical(l);
328
329    log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n",
330               f == DSIZE ? "Unknown" : "Unexpected", proto,
331               hdlc_Protocol2Nam(proto));
332    bp = mbuf_Contiguous(proto_Prepend(bp, proto, 0, 0));
333    lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->cnt);
334    if (p) {
335      p->hdlc.lqm.SaveInDiscards++;
336      p->hdlc.stats.unknownproto++;
337    }
338    mbuf_Free(bp);
339  }
340}
341