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