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