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