xref: /freebsd/usr.sbin/ppp/link.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  * $FreeBSD$
27  *
28  */
29 
30 #include <sys/types.h>
31 #include <netinet/in_systm.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 
36 #include <stdio.h>
37 #include <string.h>
38 #include <termios.h>
39 
40 #include "defs.h"
41 #include "layer.h"
42 #include "mbuf.h"
43 #include "log.h"
44 #include "timer.h"
45 #include "lqr.h"
46 #include "hdlc.h"
47 #include "throughput.h"
48 #include "proto.h"
49 #include "fsm.h"
50 #include "descriptor.h"
51 #include "lcp.h"
52 #include "ccp.h"
53 #include "link.h"
54 #include "prompt.h"
55 #include "async.h"
56 #include "physical.h"
57 #include "mp.h"
58 #include "iplist.h"
59 #include "slcompress.h"
60 #include "ipcp.h"
61 #include "ip.h"
62 #include "auth.h"
63 #include "pap.h"
64 #include "chap.h"
65 #include "cbcp.h"
66 #include "command.h"
67 
68 static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short);
69 
70 void
71 link_AddInOctets(struct link *l, int n)
72 {
73   throughput_addin(&l->throughput, n);
74 }
75 
76 void
77 link_AddOutOctets(struct link *l, int n)
78 {
79   throughput_addout(&l->throughput, n);
80 }
81 
82 void
83 link_SequenceQueue(struct link *l)
84 {
85   struct mqueue *queue, *highest;
86 
87   log_Printf(LogDEBUG, "link_SequenceQueue\n");
88 
89   highest = LINK_HIGHQ(l);
90   for (queue = l->Queue; queue < highest; queue++)
91     while (queue->qlen)
92       mbuf_Enqueue(highest, mbuf_Dequeue(queue));
93 }
94 
95 void
96 link_DeleteQueue(struct link *l)
97 {
98   struct mqueue *queue, *highest;
99 
100   highest = LINK_HIGHQ(l);
101   for (queue = l->Queue; queue <= highest; queue++)
102     while (queue->top)
103       mbuf_Free(mbuf_Dequeue(queue));
104 }
105 
106 int
107 link_QueueLen(struct link *l)
108 {
109   int i, len;
110 
111   for (i = 0, len = 0; i < LINK_QUEUES(l); i++)
112     len += l->Queue[i].qlen;
113 
114   return len;
115 }
116 
117 int
118 link_QueueBytes(struct link *l)
119 {
120   int i, len, bytes;
121   struct mbuf *m;
122 
123   bytes = 0;
124   for (i = 0, len = 0; i < LINK_QUEUES(l); i++) {
125     len = l->Queue[i].qlen;
126     m = l->Queue[i].top;
127     while (len--) {
128       bytes += mbuf_Length(m);
129       m = m->pnext;
130     }
131   }
132 
133   return bytes;
134 }
135 
136 struct mbuf *
137 link_Dequeue(struct link *l)
138 {
139   int pri;
140   struct mbuf *bp;
141 
142   for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--)
143     if (l->Queue[pri].qlen) {
144       bp = mbuf_Dequeue(l->Queue + pri);
145       log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
146                 " containing %d more packets\n", pri, l->Queue[pri].qlen);
147       break;
148     }
149 
150   return bp;
151 }
152 
153 static struct protostatheader {
154   u_short number;
155   const char *name;
156 } ProtocolStat[NPROTOSTAT] = {
157   { PROTO_IP, "IP" },
158   { PROTO_VJUNCOMP, "VJ_UNCOMP" },
159   { PROTO_VJCOMP, "VJ_COMP" },
160   { PROTO_COMPD, "COMPD" },
161   { PROTO_ICOMPD, "ICOMPD" },
162   { PROTO_LCP, "LCP" },
163   { PROTO_IPCP, "IPCP" },
164   { PROTO_CCP, "CCP" },
165   { PROTO_PAP, "PAP" },
166   { PROTO_LQR, "LQR" },
167   { PROTO_CHAP, "CHAP" },
168   { PROTO_MP, "MULTILINK" },
169   { 0, "Others" }
170 };
171 
172 void
173 link_ProtocolRecord(struct link *l, u_short proto, int type)
174 {
175   int i;
176 
177   for (i = 0; i < NPROTOSTAT; i++)
178     if (ProtocolStat[i].number == proto)
179       break;
180 
181   if (type == PROTO_IN)
182     l->proto_in[i]++;
183   else
184     l->proto_out[i]++;
185 }
186 
187 void
188 link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
189 {
190   int i;
191 
192   prompt_Printf(prompt, "    Protocol     in        out      "
193                 "Protocol      in       out\n");
194   for (i = 0; i < NPROTOSTAT; i++) {
195     prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
196 	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
197     if ((i % 2) == 0)
198       prompt_Printf(prompt, "\n");
199   }
200   if (!(i % 2))
201     prompt_Printf(prompt, "\n");
202 }
203 
204 void
205 link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri,
206                 u_short proto)
207 {
208   int layer;
209 
210   /*
211    * When we ``push'' a packet into the link, it gets processed by the
212    * ``push'' function in each layer starting at the top.
213    * We never expect the result of a ``push'' to be more than one
214    * packet (as we do with ``pull''s).
215    */
216 
217   if(pri < 0 || pri >= LINK_QUEUES(l))
218     pri = 0;
219 
220   for (layer = l->nlayers; layer && bp; layer--)
221     if (l->layer[layer - 1]->push != NULL)
222       bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto);
223 
224   if (bp) {
225     link_AddOutOctets(l, mbuf_Length(bp));
226     log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto);
227     mbuf_Enqueue(l->Queue + pri, mbuf_Contiguous(bp));
228   }
229 }
230 
231 void
232 link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b)
233 {
234   struct mbuf *bp, *lbp[LAYER_MAX], *next;
235   u_short lproto[LAYER_MAX], proto;
236   int layer;
237 
238   /*
239    * When we ``pull'' a packet from the link, it gets processed by the
240    * ``pull'' function in each layer starting at the bottom.
241    * Each ``pull'' may produce multiple packets, chained together using
242    * bp->pnext.
243    * Each packet that results from each pull has to be pulled through
244    * all of the higher layers before the next resulting packet is pulled
245    * through anything; this ensures that packets that depend on the
246    * fsm state resulting from the receipt of the previous packet aren't
247    * surprised.
248    */
249 
250   link_AddInOctets(l, len);
251 
252   memset(lbp, '\0', sizeof lbp);
253   lbp[0] = mbuf_Alloc(len, MB_UNKNOWN);
254   memcpy(MBUF_CTOP(lbp[0]), buf, len);
255   lproto[0] = 0;
256   layer = 0;
257 
258   while (layer || lbp[layer]) {
259     if (lbp[layer] == NULL) {
260       layer--;
261       continue;
262     }
263     bp = lbp[layer];
264     lbp[layer] = bp->pnext;
265     bp->pnext = NULL;
266     proto = lproto[layer];
267 
268     if (l->layer[layer]->pull != NULL)
269       bp = (*l->layer[layer]->pull)(b, l, bp, &proto);
270 
271     if (layer == l->nlayers - 1) {
272       /* We've just done the top layer, despatch the packet(s) */
273       while (bp) {
274         next = bp->pnext;
275         bp->pnext = NULL;
276         log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto);
277         Despatch(b, l, bp, proto);
278         bp = next;
279       }
280     } else {
281       lbp[++layer] = bp;
282       lproto[layer] = proto;
283     }
284   }
285 }
286 
287 int
288 link_Stack(struct link *l, struct layer *layer)
289 {
290   if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) {
291     log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n",
292                l->name, layer->name);
293     return 0;
294   }
295   l->layer[l->nlayers++] = layer;
296   return 1;
297 }
298 
299 void
300 link_EmptyStack(struct link *l)
301 {
302   l->nlayers = 0;
303 }
304 
305 static const struct {
306   u_short proto;
307   struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *);
308 } despatcher[] = {
309   { PROTO_IP, ip_Input },
310   { PROTO_MP, mp_Input },
311   { PROTO_LCP, lcp_Input },
312   { PROTO_IPCP, ipcp_Input },
313   { PROTO_PAP, pap_Input },
314   { PROTO_CHAP, chap_Input },
315   { PROTO_CCP, ccp_Input },
316   { PROTO_LQR, lqr_Input },
317   { PROTO_CBCP, cbcp_Input }
318 };
319 
320 #define DSIZE (sizeof despatcher / sizeof despatcher[0])
321 
322 static void
323 Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto)
324 {
325   int f;
326 
327   for (f = 0; f < DSIZE; f++)
328     if (despatcher[f].proto == proto) {
329       bp = (*despatcher[f].fn)(bundle, l, bp);
330       break;
331     }
332 
333   if (bp) {
334     struct physical *p = link2physical(l);
335 
336     log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n",
337                f == DSIZE ? "Unknown" : "Unexpected", proto,
338                hdlc_Protocol2Nam(proto));
339     bp = mbuf_Contiguous(proto_Prepend(bp, proto, 0, 0));
340     lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->cnt);
341     if (p) {
342       p->hdlc.lqm.SaveInDiscards++;
343       p->hdlc.stats.unknownproto++;
344     }
345     mbuf_Free(bp);
346   }
347 }
348 
349 int
350 link_ShowLayers(struct cmdargs const *arg)
351 {
352   struct link *l = command_ChooseLink(arg);
353   int layer;
354 
355   for (layer = l->nlayers; layer; layer--)
356     prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ",
357                   l->layer[layer - 1]->name);
358   if (l->nlayers)
359     prompt_Printf(arg->prompt, "\n");
360 
361   return 0;
362 }
363