xref: /freebsd/usr.sbin/ppp/link.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
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 
32 #include <sys/types.h>
33 #include <netinet/in_systm.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <termios.h>
43 
44 #include "defs.h"
45 #include "layer.h"
46 #include "mbuf.h"
47 #include "log.h"
48 #include "timer.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "throughput.h"
52 #include "proto.h"
53 #include "fsm.h"
54 #include "descriptor.h"
55 #include "lcp.h"
56 #include "ccp.h"
57 #include "link.h"
58 #include "prompt.h"
59 #include "async.h"
60 #include "physical.h"
61 #include "mp.h"
62 #include "iplist.h"
63 #include "slcompress.h"
64 #include "ncpaddr.h"
65 #include "ip.h"
66 #include "ipcp.h"
67 #include "ipv6cp.h"
68 #include "auth.h"
69 #include "pap.h"
70 #include "chap.h"
71 #include "cbcp.h"
72 #include "command.h"
73 
74 static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short);
75 
76 static inline void
77 link_AddInOctets(struct link *l, int n)
78 {
79   if (l->stats.gather) {
80     throughput_addin(&l->stats.total, n);
81     if (l->stats.parent)
82       throughput_addin(l->stats.parent, n);
83   }
84 }
85 
86 static inline void
87 link_AddOutOctets(struct link *l, int n)
88 {
89   if (l->stats.gather) {
90     throughput_addout(&l->stats.total, n);
91     if (l->stats.parent)
92       throughput_addout(l->stats.parent, n);
93   }
94 }
95 
96 void
97 link_SequenceQueue(struct link *l)
98 {
99   struct mqueue *queue, *highest;
100 
101   log_Printf(LogDEBUG, "link_SequenceQueue\n");
102 
103   highest = LINK_HIGHQ(l);
104   for (queue = l->Queue; queue < highest; queue++)
105     while (queue->len)
106       m_enqueue(highest, m_dequeue(queue));
107 }
108 
109 void
110 link_DeleteQueue(struct link *l)
111 {
112   struct mqueue *queue, *highest;
113 
114   highest = LINK_HIGHQ(l);
115   for (queue = l->Queue; queue <= highest; queue++)
116     while (queue->top)
117       m_freem(m_dequeue(queue));
118 }
119 
120 size_t
121 link_QueueLen(struct link *l)
122 {
123   unsigned i;
124   size_t len;
125 
126   for (i = 0, len = 0; i < LINK_QUEUES(l); i++)
127     len += l->Queue[i].len;
128 
129   return len;
130 }
131 
132 size_t
133 link_QueueBytes(struct link *l)
134 {
135   unsigned i;
136   size_t len, bytes;
137   struct mbuf *m;
138 
139   bytes = 0;
140   for (i = 0, len = 0; i < LINK_QUEUES(l); i++) {
141     len = l->Queue[i].len;
142     m = l->Queue[i].top;
143     while (len--) {
144       bytes += m_length(m);
145       m = m->m_nextpkt;
146     }
147   }
148 
149   return bytes;
150 }
151 
152 void
153 link_PendingLowPriorityData(struct link *l, size_t *pkts, size_t *octets)
154 {
155   struct mqueue *queue, *highest;
156   struct mbuf *m;
157   size_t len;
158 
159   /*
160    * This is all rfc1989 stuff... because our LQR packet is going to bypass
161    * everything that's not in the highest priority queue, we must be able to
162    * subtract that data from our outgoing packet/octet counts.  However,
163    * we've already async-encoded our data at this point, but the async
164    * encodings MUSTn't be a part of the LQR-reported payload :(  So, we have
165    * the async layer record how much it's padded the packet in the mbuf's
166    * priv field, and when we calculate our outgoing LQR values we subtract
167    * this value for each packet from the octet count sent.
168    */
169 
170   highest = LINK_HIGHQ(l);
171   *pkts = *octets = 0;
172   for (queue = l->Queue; queue < highest; queue++) {
173     len = queue->len;
174     *pkts += len;
175     for (m = queue->top; len--; m = m->m_nextpkt)
176       *octets += m_length(m) - m->priv;
177   }
178 }
179 
180 struct mbuf *
181 link_Dequeue(struct link *l)
182 {
183   int pri;
184   struct mbuf *bp;
185 
186   for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--)
187     if (l->Queue[pri].len) {
188       bp = m_dequeue(l->Queue + pri);
189       log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
190                 " containing %lu more packets\n", pri,
191                 (u_long)l->Queue[pri].len);
192       break;
193     }
194 
195   return bp;
196 }
197 
198 static struct protostatheader {
199   u_short number;
200   const char *name;
201 } ProtocolStat[NPROTOSTAT] = {
202   { PROTO_IP, "IP" },
203   { PROTO_VJUNCOMP, "VJ_UNCOMP" },
204   { PROTO_VJCOMP, "VJ_COMP" },
205   { PROTO_COMPD, "COMPD" },
206   { PROTO_ICOMPD, "ICOMPD" },
207   { PROTO_LCP, "LCP" },
208   { PROTO_IPCP, "IPCP" },
209   { PROTO_CCP, "CCP" },
210   { PROTO_PAP, "PAP" },
211   { PROTO_LQR, "LQR" },
212   { PROTO_CHAP, "CHAP" },
213   { PROTO_MP, "MULTILINK" },
214   { 0, "Others" }	/* must be last */
215 };
216 
217 void
218 link_ProtocolRecord(struct link *l, u_short proto, int type)
219 {
220   int i;
221 
222   for (i = 0; i < NPROTOSTAT; i++)
223     if (ProtocolStat[i].number == proto || ProtocolStat[i].number == 0) {
224       if (type == PROTO_IN)
225         l->proto_in[i]++;
226       else
227         l->proto_out[i]++;
228       break;
229     }
230 }
231 
232 void
233 link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
234 {
235   int i;
236 
237   prompt_Printf(prompt, "    Protocol     in        out      "
238                 "Protocol      in       out\n");
239   for (i = 0; i < NPROTOSTAT; i++) {
240     prompt_Printf(prompt, "   %-9s: %8lu, %8lu",
241 	    ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
242     if ((i % 2) == 0)
243       prompt_Printf(prompt, "\n");
244   }
245   if (!(i % 2))
246     prompt_Printf(prompt, "\n");
247 }
248 
249 void
250 link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri,
251                 u_short proto)
252 {
253   int layer;
254 
255   /*
256    * When we ``push'' a packet into the link, it gets processed by the
257    * ``push'' function in each layer starting at the top.
258    * We never expect the result of a ``push'' to be more than one
259    * packet (as we do with ``pull''s).
260    */
261 
262   if(pri < 0 || (unsigned)pri >= LINK_QUEUES(l))
263     pri = 0;
264 
265   bp->priv = 0;		/* Adjusted by the async layer ! */
266   for (layer = l->nlayers; layer && bp; layer--)
267     if (l->layer[layer - 1]->push != NULL)
268       bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto);
269 
270   if (bp) {
271     link_AddOutOctets(l, m_length(bp));
272     log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto);
273     m_enqueue(l->Queue + pri, m_pullup(bp));
274   }
275 }
276 
277 void
278 link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b)
279 {
280   struct mbuf *bp, *lbp[LAYER_MAX], *next;
281   u_short lproto[LAYER_MAX], proto;
282   int layer;
283 
284   /*
285    * When we ``pull'' a packet from the link, it gets processed by the
286    * ``pull'' function in each layer starting at the bottom.
287    * Each ``pull'' may produce multiple packets, chained together using
288    * bp->m_nextpkt.
289    * Each packet that results from each pull has to be pulled through
290    * all of the higher layers before the next resulting packet is pulled
291    * through anything; this ensures that packets that depend on the
292    * fsm state resulting from the receipt of the previous packet aren't
293    * surprised.
294    */
295 
296   link_AddInOctets(l, len);
297 
298   memset(lbp, '\0', sizeof lbp);
299   lbp[0] = m_get(len, MB_UNKNOWN);
300   memcpy(MBUF_CTOP(lbp[0]), buf, len);
301   lproto[0] = 0;
302   layer = 0;
303 
304   while (layer || lbp[layer]) {
305     if (lbp[layer] == NULL) {
306       layer--;
307       continue;
308     }
309     bp = lbp[layer];
310     lbp[layer] = bp->m_nextpkt;
311     bp->m_nextpkt = NULL;
312     proto = lproto[layer];
313 
314     if (l->layer[layer]->pull != NULL)
315       bp = (*l->layer[layer]->pull)(b, l, bp, &proto);
316 
317     if (layer == l->nlayers - 1) {
318       /* We've just done the top layer, despatch the packet(s) */
319       while (bp) {
320         next = bp->m_nextpkt;
321         bp->m_nextpkt = NULL;
322         log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto);
323         Despatch(b, l, bp, proto);
324         bp = next;
325       }
326     } else {
327       lbp[++layer] = bp;
328       lproto[layer] = proto;
329     }
330   }
331 }
332 
333 int
334 link_Stack(struct link *l, struct layer *layer)
335 {
336   if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) {
337     log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n",
338                l->name, layer->name);
339     return 0;
340   }
341   l->layer[l->nlayers++] = layer;
342   return 1;
343 }
344 
345 void
346 link_EmptyStack(struct link *l)
347 {
348   l->nlayers = 0;
349 }
350 
351 static const struct {
352   u_short proto;
353   struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *);
354 } despatcher[] = {
355   { PROTO_IP, ipv4_Input },
356 #ifndef NOINET6
357   { PROTO_IPV6, ipv6_Input },
358 #endif
359   { PROTO_MP, mp_Input },
360   { PROTO_LCP, lcp_Input },
361   { PROTO_IPCP, ipcp_Input },
362 #ifndef NOINET6
363   { PROTO_IPV6CP, ipv6cp_Input },
364 #endif
365   { PROTO_PAP, pap_Input },
366   { PROTO_CHAP, chap_Input },
367   { PROTO_CCP, ccp_Input },
368   { PROTO_LQR, lqr_Input },
369   { PROTO_CBCP, cbcp_Input }
370 };
371 
372 #define DSIZE (sizeof despatcher / sizeof despatcher[0])
373 
374 static void
375 Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto)
376 {
377   unsigned f;
378 
379   for (f = 0; f < DSIZE; f++)
380     if (despatcher[f].proto == proto) {
381       bp = (*despatcher[f].fn)(bundle, l, bp);
382       break;
383     }
384 
385   if (bp) {
386     struct physical *p = link2physical(l);
387 
388     log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n",
389                f == DSIZE ? "Unknown" : "Unexpected", proto,
390                hdlc_Protocol2Nam(proto));
391     bp = m_pullup(proto_Prepend(bp, proto, 0, 0));
392     lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->m_len);
393     if (p) {
394       p->hdlc.lqm.ifInDiscards++;
395       p->hdlc.stats.unknownproto++;
396     }
397     m_freem(bp);
398   }
399 }
400 
401 int
402 link_ShowLayers(struct cmdargs const *arg)
403 {
404   struct link *l = command_ChooseLink(arg);
405   int layer;
406 
407   for (layer = l->nlayers; layer; layer--)
408     prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ",
409                   l->layer[layer - 1]->name);
410   if (l->nlayers)
411     prompt_Printf(arg->prompt, "\n");
412 
413   return 0;
414 }
415