gifconfig(8): Staticize and remove some unused variables.
[dragonfly.git] / usr.sbin / ppp / link.c
bloba9efc93f88a3edefa16c498c8e48cae63ade6292
1 /*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/usr.sbin/ppp/link.c,v 1.16.2.2 2002/09/01 02:12:28 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/link.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
31 #include <sys/types.h>
32 #include <sys/select.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>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <termios.h>
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"
74 static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short);
76 static inline void
77 link_AddInOctets(struct link *l, int n)
79 if (l->stats.gather) {
80 throughput_addin(&l->stats.total, n);
81 if (l->stats.parent)
82 throughput_addin(l->stats.parent, n);
86 static inline void
87 link_AddOutOctets(struct link *l, int n)
89 if (l->stats.gather) {
90 throughput_addout(&l->stats.total, n);
91 if (l->stats.parent)
92 throughput_addout(l->stats.parent, n);
96 void
97 link_SequenceQueue(struct link *l)
99 struct mqueue *queue, *highest;
101 log_Printf(LogDEBUG, "link_SequenceQueue\n");
103 highest = LINK_HIGHQ(l);
104 for (queue = l->Queue; queue < highest; queue++)
105 while (queue->len)
106 m_enqueue(highest, m_dequeue(queue));
109 void
110 link_DeleteQueue(struct link *l)
112 struct mqueue *queue, *highest;
114 highest = LINK_HIGHQ(l);
115 for (queue = l->Queue; queue <= highest; queue++)
116 while (queue->top)
117 m_freem(m_dequeue(queue));
120 size_t
121 link_QueueLen(struct link *l)
123 unsigned i;
124 size_t len;
126 for (i = 0, len = 0; i < LINK_QUEUES(l); i++)
127 len += l->Queue[i].len;
129 return len;
132 size_t
133 link_QueueBytes(struct link *l)
135 unsigned i;
136 size_t len, bytes;
137 struct mbuf *m;
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;
149 return bytes;
152 struct mbuf *
153 link_Dequeue(struct link *l)
155 int pri;
156 struct mbuf *bp;
158 for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--)
159 if (l->Queue[pri].len) {
160 bp = m_dequeue(l->Queue + pri);
161 log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
162 " containing %lu more packets\n", pri,
163 (u_long)l->Queue[pri].len);
164 break;
167 return bp;
170 static struct protostatheader {
171 u_short number;
172 const char *name;
173 } ProtocolStat[NPROTOSTAT] = {
174 { PROTO_IP, "IP" },
175 { PROTO_VJUNCOMP, "VJ_UNCOMP" },
176 { PROTO_VJCOMP, "VJ_COMP" },
177 { PROTO_COMPD, "COMPD" },
178 { PROTO_ICOMPD, "ICOMPD" },
179 { PROTO_LCP, "LCP" },
180 { PROTO_IPCP, "IPCP" },
181 { PROTO_CCP, "CCP" },
182 { PROTO_PAP, "PAP" },
183 { PROTO_LQR, "LQR" },
184 { PROTO_CHAP, "CHAP" },
185 { PROTO_MP, "MULTILINK" },
186 { 0, "Others" }
189 void
190 link_ProtocolRecord(struct link *l, u_short proto, int type)
192 int i;
194 for (i = 0; i < NPROTOSTAT; i++) {
195 if (ProtocolStat[i].number == proto)
196 break;
198 if (i != NPROTOSTAT) {
199 if (type == PROTO_IN)
200 l->proto_in[i]++;
201 else
202 l->proto_out[i]++;
206 void
207 link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
209 int i;
211 prompt_Printf(prompt, " Protocol in out "
212 "Protocol in out\n");
213 for (i = 0; i < NPROTOSTAT; i++) {
214 prompt_Printf(prompt, " %-9s: %8lu, %8lu",
215 ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
216 if ((i % 2) == 0)
217 prompt_Printf(prompt, "\n");
219 if (!(i % 2))
220 prompt_Printf(prompt, "\n");
223 void
224 link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri,
225 u_short proto)
227 int layer;
230 * When we ``push'' a packet into the link, it gets processed by the
231 * ``push'' function in each layer starting at the top.
232 * We never expect the result of a ``push'' to be more than one
233 * packet (as we do with ``pull''s).
236 if(pri < 0 || (unsigned)pri >= LINK_QUEUES(l))
237 pri = 0;
239 for (layer = l->nlayers; layer && bp; layer--)
240 if (l->layer[layer - 1]->push != NULL)
241 bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto);
243 if (bp) {
244 link_AddOutOctets(l, m_length(bp));
245 log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto);
246 m_enqueue(l->Queue + pri, m_pullup(bp));
250 void
251 link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b)
253 struct mbuf *bp, *lbp[LAYER_MAX], *next;
254 u_short lproto[LAYER_MAX], proto;
255 int layer;
258 * When we ``pull'' a packet from the link, it gets processed by the
259 * ``pull'' function in each layer starting at the bottom.
260 * Each ``pull'' may produce multiple packets, chained together using
261 * bp->m_nextpkt.
262 * Each packet that results from each pull has to be pulled through
263 * all of the higher layers before the next resulting packet is pulled
264 * through anything; this ensures that packets that depend on the
265 * fsm state resulting from the receipt of the previous packet aren't
266 * surprised.
269 link_AddInOctets(l, len);
271 memset(lbp, '\0', sizeof lbp);
272 lbp[0] = m_get(len, MB_UNKNOWN);
273 memcpy(MBUF_CTOP(lbp[0]), buf, len);
274 lproto[0] = 0;
275 layer = 0;
277 while (layer || lbp[layer]) {
278 if (lbp[layer] == NULL) {
279 layer--;
280 continue;
282 bp = lbp[layer];
283 lbp[layer] = bp->m_nextpkt;
284 bp->m_nextpkt = NULL;
285 proto = lproto[layer];
287 if (l->layer[layer]->pull != NULL)
288 bp = (*l->layer[layer]->pull)(b, l, bp, &proto);
290 if (layer == l->nlayers - 1) {
291 /* We've just done the top layer, despatch the packet(s) */
292 while (bp) {
293 next = bp->m_nextpkt;
294 bp->m_nextpkt = NULL;
295 log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto);
296 Despatch(b, l, bp, proto);
297 bp = next;
299 } else {
300 lbp[++layer] = bp;
301 lproto[layer] = proto;
307 link_Stack(struct link *l, struct layer *layer)
309 if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) {
310 log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n",
311 l->name, layer->name);
312 return 0;
314 l->layer[l->nlayers++] = layer;
315 return 1;
318 void
319 link_EmptyStack(struct link *l)
321 l->nlayers = 0;
324 static const struct {
325 u_short proto;
326 struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *);
327 } despatcher[] = {
328 { PROTO_IP, ipv4_Input },
329 #ifndef NOINET6
330 { PROTO_IPV6, ipv6_Input },
331 #endif
332 { PROTO_MP, mp_Input },
333 { PROTO_LCP, lcp_Input },
334 { PROTO_IPCP, ipcp_Input },
335 #ifndef NOINET6
336 { PROTO_IPV6CP, ipv6cp_Input },
337 #endif
338 { PROTO_PAP, pap_Input },
339 { PROTO_CHAP, chap_Input },
340 { PROTO_CCP, ccp_Input },
341 { PROTO_LQR, lqr_Input },
342 { PROTO_CBCP, cbcp_Input }
345 #define DSIZE (sizeof despatcher / sizeof despatcher[0])
347 static void
348 Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto)
350 unsigned f;
352 for (f = 0; f < DSIZE; f++)
353 if (despatcher[f].proto == proto) {
354 bp = (*despatcher[f].fn)(bundle, l, bp);
355 break;
358 if (bp) {
359 struct physical *p = link2physical(l);
361 log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n",
362 f == DSIZE ? "Unknown" : "Unexpected", proto,
363 hdlc_Protocol2Nam(proto));
364 bp = m_pullup(proto_Prepend(bp, proto, 0, 0));
365 lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->m_len);
366 if (p) {
367 p->hdlc.lqm.SaveInDiscards++;
368 p->hdlc.stats.unknownproto++;
370 m_freem(bp);
375 link_ShowLayers(struct cmdargs const *arg)
377 struct link *l = command_ChooseLink(arg);
378 int layer;
380 for (layer = l->nlayers; layer; layer--)
381 prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ",
382 l->layer[layer - 1]->name);
383 if (l->nlayers)
384 prompt_Printf(arg->prompt, "\n");
386 return 0;