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