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