18c07a7b2SBrian Somers /*- 28c07a7b2SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 38c07a7b2SBrian Somers * All rights reserved. 48c07a7b2SBrian Somers * 58c07a7b2SBrian Somers * Redistribution and use in source and binary forms, with or without 68c07a7b2SBrian Somers * modification, are permitted provided that the following conditions 78c07a7b2SBrian Somers * are met: 88c07a7b2SBrian Somers * 1. Redistributions of source code must retain the above copyright 98c07a7b2SBrian Somers * notice, this list of conditions and the following disclaimer. 108c07a7b2SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 118c07a7b2SBrian Somers * notice, this list of conditions and the following disclaimer in the 128c07a7b2SBrian Somers * documentation and/or other materials provided with the distribution. 138c07a7b2SBrian Somers * 148c07a7b2SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 158c07a7b2SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 168c07a7b2SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 178c07a7b2SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 188c07a7b2SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 198c07a7b2SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 208c07a7b2SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 218c07a7b2SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 228c07a7b2SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 238c07a7b2SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 248c07a7b2SBrian Somers * SUCH DAMAGE. 258c07a7b2SBrian Somers * 2697d92980SPeter Wemm * $FreeBSD$ 278c07a7b2SBrian Somers * 288c07a7b2SBrian Somers */ 298c07a7b2SBrian Somers 302764b86aSBrian Somers #include <sys/types.h> 315d9e6103SBrian Somers #include <netinet/in_systm.h> 325d9e6103SBrian Somers #include <sys/un.h> 335d9e6103SBrian Somers #include <netinet/in.h> 345d9e6103SBrian Somers #include <netinet/ip.h> 358c07a7b2SBrian Somers 368c07a7b2SBrian Somers #include <stdio.h> 37eb2d27cfSBrian Somers #include <string.h> 3885b542cfSBrian Somers #include <termios.h> 398c07a7b2SBrian Somers 4092b09558SBrian Somers #include "defs.h" 415d9e6103SBrian Somers #include "layer.h" 428c07a7b2SBrian Somers #include "mbuf.h" 438c07a7b2SBrian Somers #include "log.h" 448c07a7b2SBrian Somers #include "timer.h" 45879ed6faSBrian Somers #include "lqr.h" 4663258dccSBrian Somers #include "hdlc.h" 478c07a7b2SBrian Somers #include "throughput.h" 485d9e6103SBrian Somers #include "proto.h" 496d666775SBrian Somers #include "fsm.h" 5085b542cfSBrian Somers #include "descriptor.h" 513b0f8d2eSBrian Somers #include "lcp.h" 523b0f8d2eSBrian Somers #include "ccp.h" 533b0f8d2eSBrian Somers #include "link.h" 5485b542cfSBrian Somers #include "prompt.h" 555d9e6103SBrian Somers #include "async.h" 565d9e6103SBrian Somers #include "physical.h" 575d9e6103SBrian Somers #include "mp.h" 585d9e6103SBrian Somers #include "iplist.h" 595d9e6103SBrian Somers #include "slcompress.h" 605d9e6103SBrian Somers #include "ipcp.h" 615d9e6103SBrian Somers #include "ip.h" 625d9e6103SBrian Somers #include "auth.h" 635d9e6103SBrian Somers #include "pap.h" 645d9e6103SBrian Somers #include "chap.h" 655d9e6103SBrian Somers #include "cbcp.h" 664faec430SBrian Somers #include "command.h" 675d9e6103SBrian Somers 685d9e6103SBrian Somers static void Despatch(struct bundle *, struct link *, struct mbuf *, u_short); 698c07a7b2SBrian Somers 708c07a7b2SBrian Somers void 718c07a7b2SBrian Somers link_AddInOctets(struct link *l, int n) 728c07a7b2SBrian Somers { 738c07a7b2SBrian Somers throughput_addin(&l->throughput, n); 748c07a7b2SBrian Somers } 758c07a7b2SBrian Somers 768c07a7b2SBrian Somers void 778c07a7b2SBrian Somers link_AddOutOctets(struct link *l, int n) 788c07a7b2SBrian Somers { 798c07a7b2SBrian Somers throughput_addout(&l->throughput, n); 808c07a7b2SBrian Somers } 818c07a7b2SBrian Somers 828c07a7b2SBrian Somers void 838c07a7b2SBrian Somers link_SequenceQueue(struct link *l) 848c07a7b2SBrian Somers { 85442f8495SBrian Somers struct mqueue *queue, *highest; 86442f8495SBrian Somers 87dd7e2610SBrian Somers log_Printf(LogDEBUG, "link_SequenceQueue\n"); 88442f8495SBrian Somers 89442f8495SBrian Somers highest = LINK_HIGHQ(l); 90442f8495SBrian Somers for (queue = l->Queue; queue < highest; queue++) 91442f8495SBrian Somers while (queue->qlen) 92442f8495SBrian Somers mbuf_Enqueue(highest, mbuf_Dequeue(queue)); 938c07a7b2SBrian Somers } 948c07a7b2SBrian Somers 956f8e9f0aSBrian Somers void 966f8e9f0aSBrian Somers link_DeleteQueue(struct link *l) 976f8e9f0aSBrian Somers { 98442f8495SBrian Somers struct mqueue *queue, *highest; 996f8e9f0aSBrian Somers 100442f8495SBrian Somers highest = LINK_HIGHQ(l); 101442f8495SBrian Somers for (queue = l->Queue; queue <= highest; queue++) 1026f8e9f0aSBrian Somers while (queue->top) 1036f8e9f0aSBrian Somers mbuf_Free(mbuf_Dequeue(queue)); 1046f8e9f0aSBrian Somers } 1056f8e9f0aSBrian Somers 1068c07a7b2SBrian Somers int 1078c07a7b2SBrian Somers link_QueueLen(struct link *l) 1088c07a7b2SBrian Somers { 1098c07a7b2SBrian Somers int i, len; 1108c07a7b2SBrian Somers 111442f8495SBrian Somers for (i = 0, len = 0; i < LINK_QUEUES(l); i++) 1128c07a7b2SBrian Somers len += l->Queue[i].qlen; 1138c07a7b2SBrian Somers 1148c07a7b2SBrian Somers return len; 1158c07a7b2SBrian Somers } 1168c07a7b2SBrian Somers 1173b0f8d2eSBrian Somers int 1183b0f8d2eSBrian Somers link_QueueBytes(struct link *l) 1193b0f8d2eSBrian Somers { 1203b0f8d2eSBrian Somers int i, len, bytes; 1213b0f8d2eSBrian Somers struct mbuf *m; 1223b0f8d2eSBrian Somers 1233b0f8d2eSBrian Somers bytes = 0; 124442f8495SBrian Somers for (i = 0, len = 0; i < LINK_QUEUES(l); i++) { 1253b0f8d2eSBrian Somers len = l->Queue[i].qlen; 1263b0f8d2eSBrian Somers m = l->Queue[i].top; 1273b0f8d2eSBrian Somers while (len--) { 128dd7e2610SBrian Somers bytes += mbuf_Length(m); 1293b0f8d2eSBrian Somers m = m->pnext; 1303b0f8d2eSBrian Somers } 1313b0f8d2eSBrian Somers } 1323b0f8d2eSBrian Somers 1333b0f8d2eSBrian Somers return bytes; 1343b0f8d2eSBrian Somers } 1353b0f8d2eSBrian Somers 1368c07a7b2SBrian Somers struct mbuf * 1378c07a7b2SBrian Somers link_Dequeue(struct link *l) 1388c07a7b2SBrian Somers { 1398c07a7b2SBrian Somers int pri; 1408c07a7b2SBrian Somers struct mbuf *bp; 1418c07a7b2SBrian Somers 142442f8495SBrian Somers for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--) 1438c07a7b2SBrian Somers if (l->Queue[pri].qlen) { 144dd7e2610SBrian Somers bp = mbuf_Dequeue(l->Queue + pri); 145dd7e2610SBrian Somers log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d," 1462289f246SBrian Somers " containing %d more packets\n", pri, l->Queue[pri].qlen); 1478c07a7b2SBrian Somers break; 1488c07a7b2SBrian Somers } 1498c07a7b2SBrian Somers 1508c07a7b2SBrian Somers return bp; 1518c07a7b2SBrian Somers } 1528c07a7b2SBrian Somers 1538c07a7b2SBrian Somers static struct protostatheader { 1548c07a7b2SBrian Somers u_short number; 1558c07a7b2SBrian Somers const char *name; 1568c07a7b2SBrian Somers } ProtocolStat[NPROTOSTAT] = { 1578c07a7b2SBrian Somers { PROTO_IP, "IP" }, 1588c07a7b2SBrian Somers { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 1598c07a7b2SBrian Somers { PROTO_VJCOMP, "VJ_COMP" }, 1608c07a7b2SBrian Somers { PROTO_COMPD, "COMPD" }, 161ed32233cSBrian Somers { PROTO_ICOMPD, "ICOMPD" }, 1628c07a7b2SBrian Somers { PROTO_LCP, "LCP" }, 1638c07a7b2SBrian Somers { PROTO_IPCP, "IPCP" }, 1648c07a7b2SBrian Somers { PROTO_CCP, "CCP" }, 1658c07a7b2SBrian Somers { PROTO_PAP, "PAP" }, 1668c07a7b2SBrian Somers { PROTO_LQR, "LQR" }, 1678c07a7b2SBrian Somers { PROTO_CHAP, "CHAP" }, 1683b0f8d2eSBrian Somers { PROTO_MP, "MULTILINK" }, 1698c07a7b2SBrian Somers { 0, "Others" } 1708c07a7b2SBrian Somers }; 1718c07a7b2SBrian Somers 1728c07a7b2SBrian Somers void 1738c07a7b2SBrian Somers link_ProtocolRecord(struct link *l, u_short proto, int type) 1748c07a7b2SBrian Somers { 1758c07a7b2SBrian Somers int i; 1768c07a7b2SBrian Somers 1778c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) 1788c07a7b2SBrian Somers if (ProtocolStat[i].number == proto) 1798c07a7b2SBrian Somers break; 1808c07a7b2SBrian Somers 1818c07a7b2SBrian Somers if (type == PROTO_IN) 1828c07a7b2SBrian Somers l->proto_in[i]++; 1838c07a7b2SBrian Somers else 1848c07a7b2SBrian Somers l->proto_out[i]++; 1858c07a7b2SBrian Somers } 1868c07a7b2SBrian Somers 1878c07a7b2SBrian Somers void 188b6217683SBrian Somers link_ReportProtocolStatus(struct link *l, struct prompt *prompt) 1898c07a7b2SBrian Somers { 1908c07a7b2SBrian Somers int i; 1918c07a7b2SBrian Somers 192b6217683SBrian Somers prompt_Printf(prompt, " Protocol in out " 19385b542cfSBrian Somers "Protocol in out\n"); 1948c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) { 195b6217683SBrian Somers prompt_Printf(prompt, " %-9s: %8lu, %8lu", 1968c07a7b2SBrian Somers ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]); 1978c07a7b2SBrian Somers if ((i % 2) == 0) 198b6217683SBrian Somers prompt_Printf(prompt, "\n"); 1998c07a7b2SBrian Somers } 2003b0f8d2eSBrian Somers if (!(i % 2)) 201b6217683SBrian Somers prompt_Printf(prompt, "\n"); 2028c07a7b2SBrian Somers } 2035d9e6103SBrian Somers 2045d9e6103SBrian Somers void 2055d9e6103SBrian Somers link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri, 2065d9e6103SBrian Somers u_short proto) 2075d9e6103SBrian Somers { 2085d9e6103SBrian Somers int layer; 2095d9e6103SBrian Somers 2105d9e6103SBrian Somers /* 2115d9e6103SBrian Somers * When we ``push'' a packet into the link, it gets processed by the 2125d9e6103SBrian Somers * ``push'' function in each layer starting at the top. 2135d9e6103SBrian Somers * We never expect the result of a ``push'' to be more than one 2145d9e6103SBrian Somers * packet (as we do with ``pull''s). 2155d9e6103SBrian Somers */ 2165d9e6103SBrian Somers 217442f8495SBrian Somers if(pri < 0 || pri >= LINK_QUEUES(l)) 2185d9e6103SBrian Somers pri = 0; 2195d9e6103SBrian Somers 2205d9e6103SBrian Somers for (layer = l->nlayers; layer && bp; layer--) 2215d9e6103SBrian Somers if (l->layer[layer - 1]->push != NULL) 2225d9e6103SBrian Somers bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto); 2235d9e6103SBrian Somers 2245d9e6103SBrian Somers if (bp) { 2255d9e6103SBrian Somers link_AddOutOctets(l, mbuf_Length(bp)); 2266815097bSBrian Somers log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto); 2275d9e6103SBrian Somers mbuf_Enqueue(l->Queue + pri, mbuf_Contiguous(bp)); 2285d9e6103SBrian Somers } 2295d9e6103SBrian Somers } 2305d9e6103SBrian Somers 2315d9e6103SBrian Somers void 2325d9e6103SBrian Somers link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b) 2335d9e6103SBrian Somers { 2345d9e6103SBrian Somers struct mbuf *bp, *lbp[LAYER_MAX], *next; 2355d9e6103SBrian Somers u_short lproto[LAYER_MAX], proto; 2365d9e6103SBrian Somers int layer; 2375d9e6103SBrian Somers 2385d9e6103SBrian Somers /* 2395d9e6103SBrian Somers * When we ``pull'' a packet from the link, it gets processed by the 2405d9e6103SBrian Somers * ``pull'' function in each layer starting at the bottom. 2415d9e6103SBrian Somers * Each ``pull'' may produce multiple packets, chained together using 2425d9e6103SBrian Somers * bp->pnext. 2435d9e6103SBrian Somers * Each packet that results from each pull has to be pulled through 2445d9e6103SBrian Somers * all of the higher layers before the next resulting packet is pulled 2455d9e6103SBrian Somers * through anything; this ensures that packets that depend on the 2465d9e6103SBrian Somers * fsm state resulting from the receipt of the previous packet aren't 2475d9e6103SBrian Somers * surprised. 2485d9e6103SBrian Somers */ 2495d9e6103SBrian Somers 2505d9e6103SBrian Somers link_AddInOctets(l, len); 2515d9e6103SBrian Somers 2525d9e6103SBrian Somers memset(lbp, '\0', sizeof lbp); 253411675baSBrian Somers lbp[0] = mbuf_Alloc(len, MB_UNKNOWN); 2545d9e6103SBrian Somers memcpy(MBUF_CTOP(lbp[0]), buf, len); 2555d9e6103SBrian Somers lproto[0] = 0; 2565d9e6103SBrian Somers layer = 0; 2575d9e6103SBrian Somers 2585d9e6103SBrian Somers while (layer || lbp[layer]) { 2595d9e6103SBrian Somers if (lbp[layer] == NULL) { 2605d9e6103SBrian Somers layer--; 2615d9e6103SBrian Somers continue; 2625d9e6103SBrian Somers } 2635d9e6103SBrian Somers bp = lbp[layer]; 2645d9e6103SBrian Somers lbp[layer] = bp->pnext; 2655d9e6103SBrian Somers bp->pnext = NULL; 2665d9e6103SBrian Somers proto = lproto[layer]; 2675d9e6103SBrian Somers 2685d9e6103SBrian Somers if (l->layer[layer]->pull != NULL) 2695d9e6103SBrian Somers bp = (*l->layer[layer]->pull)(b, l, bp, &proto); 2705d9e6103SBrian Somers 2715d9e6103SBrian Somers if (layer == l->nlayers - 1) { 2725d9e6103SBrian Somers /* We've just done the top layer, despatch the packet(s) */ 2735d9e6103SBrian Somers while (bp) { 2745d9e6103SBrian Somers next = bp->pnext; 2755d9e6103SBrian Somers bp->pnext = NULL; 2766815097bSBrian Somers log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto); 2775d9e6103SBrian Somers Despatch(b, l, bp, proto); 2785d9e6103SBrian Somers bp = next; 2795d9e6103SBrian Somers } 2805d9e6103SBrian Somers } else { 2815d9e6103SBrian Somers lbp[++layer] = bp; 2825d9e6103SBrian Somers lproto[layer] = proto; 2835d9e6103SBrian Somers } 2845d9e6103SBrian Somers } 2855d9e6103SBrian Somers } 2865d9e6103SBrian Somers 2875d9e6103SBrian Somers int 2885d9e6103SBrian Somers link_Stack(struct link *l, struct layer *layer) 2895d9e6103SBrian Somers { 2905d9e6103SBrian Somers if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) { 2915d9e6103SBrian Somers log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n", 2925d9e6103SBrian Somers l->name, layer->name); 2935d9e6103SBrian Somers return 0; 2945d9e6103SBrian Somers } 2955d9e6103SBrian Somers l->layer[l->nlayers++] = layer; 2965d9e6103SBrian Somers return 1; 2975d9e6103SBrian Somers } 2985d9e6103SBrian Somers 2995d9e6103SBrian Somers void 3005d9e6103SBrian Somers link_EmptyStack(struct link *l) 3015d9e6103SBrian Somers { 3025d9e6103SBrian Somers l->nlayers = 0; 3035d9e6103SBrian Somers } 3045d9e6103SBrian Somers 3055d9e6103SBrian Somers static const struct { 3065d9e6103SBrian Somers u_short proto; 3075d9e6103SBrian Somers struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *); 3085d9e6103SBrian Somers } despatcher[] = { 3095d9e6103SBrian Somers { PROTO_IP, ip_Input }, 3105d9e6103SBrian Somers { PROTO_MP, mp_Input }, 3115d9e6103SBrian Somers { PROTO_LCP, lcp_Input }, 3125d9e6103SBrian Somers { PROTO_IPCP, ipcp_Input }, 3135d9e6103SBrian Somers { PROTO_PAP, pap_Input }, 3145d9e6103SBrian Somers { PROTO_CHAP, chap_Input }, 3155d9e6103SBrian Somers { PROTO_CCP, ccp_Input }, 3165d9e6103SBrian Somers { PROTO_LQR, lqr_Input }, 3175d9e6103SBrian Somers { PROTO_CBCP, cbcp_Input } 3185d9e6103SBrian Somers }; 3195d9e6103SBrian Somers 3205d9e6103SBrian Somers #define DSIZE (sizeof despatcher / sizeof despatcher[0]) 3215d9e6103SBrian Somers 3225d9e6103SBrian Somers static void 3235d9e6103SBrian Somers Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto) 3245d9e6103SBrian Somers { 3255d9e6103SBrian Somers int f; 3265d9e6103SBrian Somers 3275d9e6103SBrian Somers for (f = 0; f < DSIZE; f++) 3285d9e6103SBrian Somers if (despatcher[f].proto == proto) { 3295d9e6103SBrian Somers bp = (*despatcher[f].fn)(bundle, l, bp); 3305d9e6103SBrian Somers break; 3315d9e6103SBrian Somers } 3325d9e6103SBrian Somers 3335d9e6103SBrian Somers if (bp) { 3345d9e6103SBrian Somers struct physical *p = link2physical(l); 3355d9e6103SBrian Somers 3365d9e6103SBrian Somers log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n", 3375d9e6103SBrian Somers f == DSIZE ? "Unknown" : "Unexpected", proto, 3385d9e6103SBrian Somers hdlc_Protocol2Nam(proto)); 3395d9e6103SBrian Somers bp = mbuf_Contiguous(proto_Prepend(bp, proto, 0, 0)); 3405d9e6103SBrian Somers lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->cnt); 3415d9e6103SBrian Somers if (p) { 3425d9e6103SBrian Somers p->hdlc.lqm.SaveInDiscards++; 3435d9e6103SBrian Somers p->hdlc.stats.unknownproto++; 3445d9e6103SBrian Somers } 3455d9e6103SBrian Somers mbuf_Free(bp); 3465d9e6103SBrian Somers } 3475d9e6103SBrian Somers } 3484faec430SBrian Somers 3494faec430SBrian Somers int 3504faec430SBrian Somers link_ShowLayers(struct cmdargs const *arg) 3514faec430SBrian Somers { 3524faec430SBrian Somers struct link *l = command_ChooseLink(arg); 3534faec430SBrian Somers int layer; 3544faec430SBrian Somers 3554faec430SBrian Somers for (layer = l->nlayers; layer; layer--) 3564faec430SBrian Somers prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ", 3574faec430SBrian Somers l->layer[layer - 1]->name); 3584faec430SBrian Somers if (l->nlayers) 3594faec430SBrian Somers prompt_Printf(arg->prompt, "\n"); 3604faec430SBrian Somers 3614faec430SBrian Somers return 0; 3624faec430SBrian Somers } 363