2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
26 * $FreeBSD: src/usr.sbin/ppp/link.c,v 1.16.2.2 2002/09/01 02:12:28 brian Exp $
30 #include <sys/types.h>
31 #include <netinet/in_systm.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
49 #include "throughput.h"
52 #include "descriptor.h"
61 #include "slcompress.h"
72 static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short);
75 link_AddInOctets(struct link *l, int n)
77 if (l->stats.gather) {
78 throughput_addin(&l->stats.total, n);
80 throughput_addin(l->stats.parent, n);
85 link_AddOutOctets(struct link *l, int n)
87 if (l->stats.gather) {
88 throughput_addout(&l->stats.total, n);
90 throughput_addout(l->stats.parent, n);
95 link_SequenceQueue(struct link *l)
97 struct mqueue *queue, *highest;
99 log_Printf(LogDEBUG, "link_SequenceQueue\n");
101 highest = LINK_HIGHQ(l);
102 for (queue = l->Queue; queue < highest; queue++)
104 m_enqueue(highest, m_dequeue(queue));
108 link_DeleteQueue(struct link *l)
110 struct mqueue *queue, *highest;
112 highest = LINK_HIGHQ(l);
113 for (queue = l->Queue; queue <= highest; queue++)
115 m_freem(m_dequeue(queue));
119 link_QueueLen(struct link *l)
124 for (i = 0, len = 0; i < LINK_QUEUES(l); i++)
125 len += l->Queue[i].len;
131 link_QueueBytes(struct link *l)
138 for (i = 0, len = 0; i < LINK_QUEUES(l); i++) {
139 len = l->Queue[i].len;
142 bytes += m_length(m);
151 link_Dequeue(struct link *l)
156 for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--)
157 if (l->Queue[pri].len) {
158 bp = m_dequeue(l->Queue + pri);
159 log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d,"
160 " containing %lu more packets\n", pri,
161 (u_long)l->Queue[pri].len);
168 static struct protostatheader {
171 } ProtocolStat[NPROTOSTAT] = {
173 { PROTO_VJUNCOMP, "VJ_UNCOMP" },
174 { PROTO_VJCOMP, "VJ_COMP" },
175 { PROTO_COMPD, "COMPD" },
176 { PROTO_ICOMPD, "ICOMPD" },
177 { PROTO_LCP, "LCP" },
178 { PROTO_IPCP, "IPCP" },
179 { PROTO_CCP, "CCP" },
180 { PROTO_PAP, "PAP" },
181 { PROTO_LQR, "LQR" },
182 { PROTO_CHAP, "CHAP" },
183 { PROTO_MP, "MULTILINK" },
188 link_ProtocolRecord(struct link *l, u_short proto, int type)
192 for (i = 0; i < NPROTOSTAT; i++)
193 if (ProtocolStat[i].number == proto)
196 if (type == PROTO_IN)
203 link_ReportProtocolStatus(struct link *l, struct prompt *prompt)
207 prompt_Printf(prompt, " Protocol in out "
208 "Protocol in out\n");
209 for (i = 0; i < NPROTOSTAT; i++) {
210 prompt_Printf(prompt, " %-9s: %8lu, %8lu",
211 ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]);
213 prompt_Printf(prompt, "\n");
216 prompt_Printf(prompt, "\n");
220 link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri,
226 * When we ``push'' a packet into the link, it gets processed by the
227 * ``push'' function in each layer starting at the top.
228 * We never expect the result of a ``push'' to be more than one
229 * packet (as we do with ``pull''s).
232 if(pri < 0 || pri >= LINK_QUEUES(l))
235 for (layer = l->nlayers; layer && bp; layer--)
236 if (l->layer[layer - 1]->push != NULL)
237 bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto);
240 link_AddOutOctets(l, m_length(bp));
241 log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto);
242 m_enqueue(l->Queue + pri, m_pullup(bp));
247 link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b)
249 struct mbuf *bp, *lbp[LAYER_MAX], *next;
250 u_short lproto[LAYER_MAX], proto;
254 * When we ``pull'' a packet from the link, it gets processed by the
255 * ``pull'' function in each layer starting at the bottom.
256 * Each ``pull'' may produce multiple packets, chained together using
258 * Each packet that results from each pull has to be pulled through
259 * all of the higher layers before the next resulting packet is pulled
260 * through anything; this ensures that packets that depend on the
261 * fsm state resulting from the receipt of the previous packet aren't
265 link_AddInOctets(l, len);
267 memset(lbp, '\0', sizeof lbp);
268 lbp[0] = m_get(len, MB_UNKNOWN);
269 memcpy(MBUF_CTOP(lbp[0]), buf, len);
273 while (layer || lbp[layer]) {
274 if (lbp[layer] == NULL) {
279 lbp[layer] = bp->m_nextpkt;
280 bp->m_nextpkt = NULL;
281 proto = lproto[layer];
283 if (l->layer[layer]->pull != NULL)
284 bp = (*l->layer[layer]->pull)(b, l, bp, &proto);
286 if (layer == l->nlayers - 1) {
287 /* We've just done the top layer, despatch the packet(s) */
289 next = bp->m_nextpkt;
290 bp->m_nextpkt = NULL;
291 log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto);
292 Despatch(b, l, bp, proto);
297 lproto[layer] = proto;
303 link_Stack(struct link *l, struct layer *layer)
305 if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) {
306 log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n",
307 l->name, layer->name);
310 l->layer[l->nlayers++] = layer;
315 link_EmptyStack(struct link *l)
320 static const struct {
322 struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *);
324 { PROTO_IP, ipv4_Input },
326 { PROTO_IPV6, ipv6_Input },
328 { PROTO_MP, mp_Input },
329 { PROTO_LCP, lcp_Input },
330 { PROTO_IPCP, ipcp_Input },
332 { PROTO_IPV6CP, ipv6cp_Input },
334 { PROTO_PAP, pap_Input },
335 { PROTO_CHAP, chap_Input },
336 { PROTO_CCP, ccp_Input },
337 { PROTO_LQR, lqr_Input },
338 { PROTO_CBCP, cbcp_Input }
341 #define DSIZE (sizeof despatcher / sizeof despatcher[0])
344 Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto)
348 for (f = 0; f < DSIZE; f++)
349 if (despatcher[f].proto == proto) {
350 bp = (*despatcher[f].fn)(bundle, l, bp);
355 struct physical *p = link2physical(l);
357 log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n",
358 f == DSIZE ? "Unknown" : "Unexpected", proto,
359 hdlc_Protocol2Nam(proto));
360 bp = m_pullup(proto_Prepend(bp, proto, 0, 0));
361 lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->m_len);
363 p->hdlc.lqm.SaveInDiscards++;
364 p->hdlc.stats.unknownproto++;
371 link_ShowLayers(struct cmdargs const *arg)
373 struct link *l = command_ChooseLink(arg);
376 for (layer = l->nlayers; layer; layer--)
377 prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ",
378 l->layer[layer - 1]->name);
380 prompt_Printf(arg->prompt, "\n");