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++) 9126af0ae9SBrian Somers while (queue->len) 9226af0ae9SBrian Somers m_enqueue(highest, m_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) 10326af0ae9SBrian Somers m_freem(m_dequeue(queue)); 1046f8e9f0aSBrian Somers } 1056f8e9f0aSBrian Somers 10626af0ae9SBrian Somers size_t 1078c07a7b2SBrian Somers link_QueueLen(struct link *l) 1088c07a7b2SBrian Somers { 10926af0ae9SBrian Somers int i; 11026af0ae9SBrian Somers size_t len; 1118c07a7b2SBrian Somers 112442f8495SBrian Somers for (i = 0, len = 0; i < LINK_QUEUES(l); i++) 11326af0ae9SBrian Somers len += l->Queue[i].len; 1148c07a7b2SBrian Somers 1158c07a7b2SBrian Somers return len; 1168c07a7b2SBrian Somers } 1178c07a7b2SBrian Somers 11826af0ae9SBrian Somers size_t 1193b0f8d2eSBrian Somers link_QueueBytes(struct link *l) 1203b0f8d2eSBrian Somers { 12126af0ae9SBrian Somers int i; 12226af0ae9SBrian Somers size_t len, bytes; 1233b0f8d2eSBrian Somers struct mbuf *m; 1243b0f8d2eSBrian Somers 1253b0f8d2eSBrian Somers bytes = 0; 126442f8495SBrian Somers for (i = 0, len = 0; i < LINK_QUEUES(l); i++) { 12726af0ae9SBrian Somers len = l->Queue[i].len; 1283b0f8d2eSBrian Somers m = l->Queue[i].top; 1293b0f8d2eSBrian Somers while (len--) { 13026af0ae9SBrian Somers bytes += m_length(m); 13126af0ae9SBrian Somers m = m->m_nextpkt; 1323b0f8d2eSBrian Somers } 1333b0f8d2eSBrian Somers } 1343b0f8d2eSBrian Somers 1353b0f8d2eSBrian Somers return bytes; 1363b0f8d2eSBrian Somers } 1373b0f8d2eSBrian Somers 1388c07a7b2SBrian Somers struct mbuf * 1398c07a7b2SBrian Somers link_Dequeue(struct link *l) 1408c07a7b2SBrian Somers { 1418c07a7b2SBrian Somers int pri; 1428c07a7b2SBrian Somers struct mbuf *bp; 1438c07a7b2SBrian Somers 144442f8495SBrian Somers for (bp = NULL, pri = LINK_QUEUES(l) - 1; pri >= 0; pri--) 14526af0ae9SBrian Somers if (l->Queue[pri].len) { 14626af0ae9SBrian Somers bp = m_dequeue(l->Queue + pri); 147dd7e2610SBrian Somers log_Printf(LogDEBUG, "link_Dequeue: Dequeued from queue %d," 14826af0ae9SBrian Somers " containing %lu more packets\n", pri, 14926af0ae9SBrian Somers (u_long)l->Queue[pri].len); 1508c07a7b2SBrian Somers break; 1518c07a7b2SBrian Somers } 1528c07a7b2SBrian Somers 1538c07a7b2SBrian Somers return bp; 1548c07a7b2SBrian Somers } 1558c07a7b2SBrian Somers 1568c07a7b2SBrian Somers static struct protostatheader { 1578c07a7b2SBrian Somers u_short number; 1588c07a7b2SBrian Somers const char *name; 1598c07a7b2SBrian Somers } ProtocolStat[NPROTOSTAT] = { 1608c07a7b2SBrian Somers { PROTO_IP, "IP" }, 1618c07a7b2SBrian Somers { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 1628c07a7b2SBrian Somers { PROTO_VJCOMP, "VJ_COMP" }, 1638c07a7b2SBrian Somers { PROTO_COMPD, "COMPD" }, 164ed32233cSBrian Somers { PROTO_ICOMPD, "ICOMPD" }, 1658c07a7b2SBrian Somers { PROTO_LCP, "LCP" }, 1668c07a7b2SBrian Somers { PROTO_IPCP, "IPCP" }, 1678c07a7b2SBrian Somers { PROTO_CCP, "CCP" }, 1688c07a7b2SBrian Somers { PROTO_PAP, "PAP" }, 1698c07a7b2SBrian Somers { PROTO_LQR, "LQR" }, 1708c07a7b2SBrian Somers { PROTO_CHAP, "CHAP" }, 1713b0f8d2eSBrian Somers { PROTO_MP, "MULTILINK" }, 1728c07a7b2SBrian Somers { 0, "Others" } 1738c07a7b2SBrian Somers }; 1748c07a7b2SBrian Somers 1758c07a7b2SBrian Somers void 1768c07a7b2SBrian Somers link_ProtocolRecord(struct link *l, u_short proto, int type) 1778c07a7b2SBrian Somers { 1788c07a7b2SBrian Somers int i; 1798c07a7b2SBrian Somers 1808c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) 1818c07a7b2SBrian Somers if (ProtocolStat[i].number == proto) 1828c07a7b2SBrian Somers break; 1838c07a7b2SBrian Somers 1848c07a7b2SBrian Somers if (type == PROTO_IN) 1858c07a7b2SBrian Somers l->proto_in[i]++; 1868c07a7b2SBrian Somers else 1878c07a7b2SBrian Somers l->proto_out[i]++; 1888c07a7b2SBrian Somers } 1898c07a7b2SBrian Somers 1908c07a7b2SBrian Somers void 191b6217683SBrian Somers link_ReportProtocolStatus(struct link *l, struct prompt *prompt) 1928c07a7b2SBrian Somers { 1938c07a7b2SBrian Somers int i; 1948c07a7b2SBrian Somers 195b6217683SBrian Somers prompt_Printf(prompt, " Protocol in out " 19685b542cfSBrian Somers "Protocol in out\n"); 1978c07a7b2SBrian Somers for (i = 0; i < NPROTOSTAT; i++) { 198b6217683SBrian Somers prompt_Printf(prompt, " %-9s: %8lu, %8lu", 1998c07a7b2SBrian Somers ProtocolStat[i].name, l->proto_in[i], l->proto_out[i]); 2008c07a7b2SBrian Somers if ((i % 2) == 0) 201b6217683SBrian Somers prompt_Printf(prompt, "\n"); 2028c07a7b2SBrian Somers } 2033b0f8d2eSBrian Somers if (!(i % 2)) 204b6217683SBrian Somers prompt_Printf(prompt, "\n"); 2058c07a7b2SBrian Somers } 2065d9e6103SBrian Somers 2075d9e6103SBrian Somers void 2085d9e6103SBrian Somers link_PushPacket(struct link *l, struct mbuf *bp, struct bundle *b, int pri, 2095d9e6103SBrian Somers u_short proto) 2105d9e6103SBrian Somers { 2115d9e6103SBrian Somers int layer; 2125d9e6103SBrian Somers 2135d9e6103SBrian Somers /* 2145d9e6103SBrian Somers * When we ``push'' a packet into the link, it gets processed by the 2155d9e6103SBrian Somers * ``push'' function in each layer starting at the top. 2165d9e6103SBrian Somers * We never expect the result of a ``push'' to be more than one 2175d9e6103SBrian Somers * packet (as we do with ``pull''s). 2185d9e6103SBrian Somers */ 2195d9e6103SBrian Somers 220442f8495SBrian Somers if(pri < 0 || pri >= LINK_QUEUES(l)) 2215d9e6103SBrian Somers pri = 0; 2225d9e6103SBrian Somers 2235d9e6103SBrian Somers for (layer = l->nlayers; layer && bp; layer--) 2245d9e6103SBrian Somers if (l->layer[layer - 1]->push != NULL) 2255d9e6103SBrian Somers bp = (*l->layer[layer - 1]->push)(b, l, bp, pri, &proto); 2265d9e6103SBrian Somers 2275d9e6103SBrian Somers if (bp) { 22826af0ae9SBrian Somers link_AddOutOctets(l, m_length(bp)); 2296815097bSBrian Somers log_Printf(LogDEBUG, "link_PushPacket: Transmit proto 0x%04x\n", proto); 23026af0ae9SBrian Somers m_enqueue(l->Queue + pri, m_pullup(bp)); 2315d9e6103SBrian Somers } 2325d9e6103SBrian Somers } 2335d9e6103SBrian Somers 2345d9e6103SBrian Somers void 2355d9e6103SBrian Somers link_PullPacket(struct link *l, char *buf, size_t len, struct bundle *b) 2365d9e6103SBrian Somers { 2375d9e6103SBrian Somers struct mbuf *bp, *lbp[LAYER_MAX], *next; 2385d9e6103SBrian Somers u_short lproto[LAYER_MAX], proto; 2395d9e6103SBrian Somers int layer; 2405d9e6103SBrian Somers 2415d9e6103SBrian Somers /* 2425d9e6103SBrian Somers * When we ``pull'' a packet from the link, it gets processed by the 2435d9e6103SBrian Somers * ``pull'' function in each layer starting at the bottom. 2445d9e6103SBrian Somers * Each ``pull'' may produce multiple packets, chained together using 24526af0ae9SBrian Somers * bp->m_nextpkt. 2465d9e6103SBrian Somers * Each packet that results from each pull has to be pulled through 2475d9e6103SBrian Somers * all of the higher layers before the next resulting packet is pulled 2485d9e6103SBrian Somers * through anything; this ensures that packets that depend on the 2495d9e6103SBrian Somers * fsm state resulting from the receipt of the previous packet aren't 2505d9e6103SBrian Somers * surprised. 2515d9e6103SBrian Somers */ 2525d9e6103SBrian Somers 2535d9e6103SBrian Somers link_AddInOctets(l, len); 2545d9e6103SBrian Somers 2555d9e6103SBrian Somers memset(lbp, '\0', sizeof lbp); 25626af0ae9SBrian Somers lbp[0] = m_get(len, MB_UNKNOWN); 2575d9e6103SBrian Somers memcpy(MBUF_CTOP(lbp[0]), buf, len); 2585d9e6103SBrian Somers lproto[0] = 0; 2595d9e6103SBrian Somers layer = 0; 2605d9e6103SBrian Somers 2615d9e6103SBrian Somers while (layer || lbp[layer]) { 2625d9e6103SBrian Somers if (lbp[layer] == NULL) { 2635d9e6103SBrian Somers layer--; 2645d9e6103SBrian Somers continue; 2655d9e6103SBrian Somers } 2665d9e6103SBrian Somers bp = lbp[layer]; 26726af0ae9SBrian Somers lbp[layer] = bp->m_nextpkt; 26826af0ae9SBrian Somers bp->m_nextpkt = NULL; 2695d9e6103SBrian Somers proto = lproto[layer]; 2705d9e6103SBrian Somers 2715d9e6103SBrian Somers if (l->layer[layer]->pull != NULL) 2725d9e6103SBrian Somers bp = (*l->layer[layer]->pull)(b, l, bp, &proto); 2735d9e6103SBrian Somers 2745d9e6103SBrian Somers if (layer == l->nlayers - 1) { 2755d9e6103SBrian Somers /* We've just done the top layer, despatch the packet(s) */ 2765d9e6103SBrian Somers while (bp) { 27726af0ae9SBrian Somers next = bp->m_nextpkt; 27826af0ae9SBrian Somers bp->m_nextpkt = NULL; 2796815097bSBrian Somers log_Printf(LogDEBUG, "link_PullPacket: Despatch proto 0x%04x\n", proto); 2805d9e6103SBrian Somers Despatch(b, l, bp, proto); 2815d9e6103SBrian Somers bp = next; 2825d9e6103SBrian Somers } 2835d9e6103SBrian Somers } else { 2845d9e6103SBrian Somers lbp[++layer] = bp; 2855d9e6103SBrian Somers lproto[layer] = proto; 2865d9e6103SBrian Somers } 2875d9e6103SBrian Somers } 2885d9e6103SBrian Somers } 2895d9e6103SBrian Somers 2905d9e6103SBrian Somers int 2915d9e6103SBrian Somers link_Stack(struct link *l, struct layer *layer) 2925d9e6103SBrian Somers { 2935d9e6103SBrian Somers if (l->nlayers == sizeof l->layer / sizeof l->layer[0]) { 2945d9e6103SBrian Somers log_Printf(LogERROR, "%s: Oops, cannot stack a %s layer...\n", 2955d9e6103SBrian Somers l->name, layer->name); 2965d9e6103SBrian Somers return 0; 2975d9e6103SBrian Somers } 2985d9e6103SBrian Somers l->layer[l->nlayers++] = layer; 2995d9e6103SBrian Somers return 1; 3005d9e6103SBrian Somers } 3015d9e6103SBrian Somers 3025d9e6103SBrian Somers void 3035d9e6103SBrian Somers link_EmptyStack(struct link *l) 3045d9e6103SBrian Somers { 3055d9e6103SBrian Somers l->nlayers = 0; 3065d9e6103SBrian Somers } 3075d9e6103SBrian Somers 3085d9e6103SBrian Somers static const struct { 3095d9e6103SBrian Somers u_short proto; 3105d9e6103SBrian Somers struct mbuf *(*fn)(struct bundle *, struct link *, struct mbuf *); 3115d9e6103SBrian Somers } despatcher[] = { 3125d9e6103SBrian Somers { PROTO_IP, ip_Input }, 3135d9e6103SBrian Somers { PROTO_MP, mp_Input }, 3145d9e6103SBrian Somers { PROTO_LCP, lcp_Input }, 3155d9e6103SBrian Somers { PROTO_IPCP, ipcp_Input }, 3165d9e6103SBrian Somers { PROTO_PAP, pap_Input }, 3175d9e6103SBrian Somers { PROTO_CHAP, chap_Input }, 3185d9e6103SBrian Somers { PROTO_CCP, ccp_Input }, 3195d9e6103SBrian Somers { PROTO_LQR, lqr_Input }, 3205d9e6103SBrian Somers { PROTO_CBCP, cbcp_Input } 3215d9e6103SBrian Somers }; 3225d9e6103SBrian Somers 3235d9e6103SBrian Somers #define DSIZE (sizeof despatcher / sizeof despatcher[0]) 3245d9e6103SBrian Somers 3255d9e6103SBrian Somers static void 3265d9e6103SBrian Somers Despatch(struct bundle *bundle, struct link *l, struct mbuf *bp, u_short proto) 3275d9e6103SBrian Somers { 3285d9e6103SBrian Somers int f; 3295d9e6103SBrian Somers 3305d9e6103SBrian Somers for (f = 0; f < DSIZE; f++) 3315d9e6103SBrian Somers if (despatcher[f].proto == proto) { 3325d9e6103SBrian Somers bp = (*despatcher[f].fn)(bundle, l, bp); 3335d9e6103SBrian Somers break; 3345d9e6103SBrian Somers } 3355d9e6103SBrian Somers 3365d9e6103SBrian Somers if (bp) { 3375d9e6103SBrian Somers struct physical *p = link2physical(l); 3385d9e6103SBrian Somers 3395d9e6103SBrian Somers log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n", 3405d9e6103SBrian Somers f == DSIZE ? "Unknown" : "Unexpected", proto, 3415d9e6103SBrian Somers hdlc_Protocol2Nam(proto)); 34226af0ae9SBrian Somers bp = m_pullup(proto_Prepend(bp, proto, 0, 0)); 34326af0ae9SBrian Somers lcp_SendProtoRej(&l->lcp, MBUF_CTOP(bp), bp->m_len); 3445d9e6103SBrian Somers if (p) { 3455d9e6103SBrian Somers p->hdlc.lqm.SaveInDiscards++; 3465d9e6103SBrian Somers p->hdlc.stats.unknownproto++; 3475d9e6103SBrian Somers } 34826af0ae9SBrian Somers m_freem(bp); 3495d9e6103SBrian Somers } 3505d9e6103SBrian Somers } 3514faec430SBrian Somers 3524faec430SBrian Somers int 3534faec430SBrian Somers link_ShowLayers(struct cmdargs const *arg) 3544faec430SBrian Somers { 3554faec430SBrian Somers struct link *l = command_ChooseLink(arg); 3564faec430SBrian Somers int layer; 3574faec430SBrian Somers 3584faec430SBrian Somers for (layer = l->nlayers; layer; layer--) 3594faec430SBrian Somers prompt_Printf(arg->prompt, "%s%s", layer == l->nlayers ? "" : ", ", 3604faec430SBrian Somers l->layer[layer - 1]->name); 3614faec430SBrian Somers if (l->nlayers) 3624faec430SBrian Somers prompt_Printf(arg->prompt, "\n"); 3634faec430SBrian Somers 3644faec430SBrian Somers return 0; 3654faec430SBrian Somers } 366