1 /*- 2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 4 * Internet Initiative Japan, Inc (IIJ) 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 33 #ifdef __FreeBSD__ 34 #include <netinet/in.h> 35 #endif 36 #include <sys/un.h> 37 38 #include <string.h> 39 #include <termios.h> 40 41 #include "layer.h" 42 #include "mbuf.h" 43 #include "log.h" 44 #include "defs.h" 45 #include "timer.h" 46 #include "fsm.h" 47 #include "acf.h" 48 #include "proto.h" 49 #include "lqr.h" 50 #include "hdlc.h" 51 #include "lcp.h" 52 #include "async.h" 53 #include "throughput.h" 54 #include "ccp.h" 55 #include "link.h" 56 #include "descriptor.h" 57 #include "physical.h" 58 #include "mp.h" 59 #include "chat.h" 60 #include "auth.h" 61 #include "chap.h" 62 #include "command.h" 63 #include "cbcp.h" 64 #include "datalink.h" 65 66 struct echolqr { 67 u_int32_t magic; 68 u_int32_t signature; 69 u_int32_t sequence; 70 }; 71 72 #define SIGNATURE 0x594e4f54 73 74 static void 75 SendEchoReq(struct lcp *lcp) 76 { 77 struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc; 78 struct echolqr echo; 79 80 echo.magic = htonl(lcp->want_magic); 81 echo.signature = htonl(SIGNATURE); 82 echo.sequence = htonl(hdlc->lqm.echo.seq_sent); 83 fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++, 84 (u_char *)&echo, sizeof echo, MB_ECHOOUT); 85 } 86 87 struct mbuf * 88 lqr_RecvEcho(struct fsm *fp, struct mbuf *bp) 89 { 90 struct hdlc *hdlc = &link2physical(fp->link)->hdlc; 91 struct lcp *lcp = fsm2lcp(fp); 92 struct echolqr lqr; 93 94 if (m_length(bp) >= sizeof lqr) { 95 m_freem(mbuf_Read(bp, &lqr, sizeof lqr)); 96 bp = NULL; 97 lqr.magic = ntohl(lqr.magic); 98 lqr.signature = ntohl(lqr.signature); 99 lqr.sequence = ntohl(lqr.sequence); 100 101 /* Tolerate echo replies with either magic number */ 102 if (lqr.magic != 0 && lqr.magic != lcp->his_magic && 103 lqr.magic != lcp->want_magic) { 104 log_Printf(LogWARN, "%s: lqr_RecvEcho: Bad magic: expected 0x%08x," 105 " got 0x%08x\n", fp->link->name, lcp->his_magic, lqr.magic); 106 /* 107 * XXX: We should send a terminate request. But poor implementations may 108 * die as a result. 109 */ 110 } 111 if (lqr.signature == SIGNATURE) { 112 /* careful not to update lqm.echo.seq_recv with older values */ 113 if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && lqr.sequence < 5) || 114 (hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 && 115 lqr.sequence > hdlc->lqm.echo.seq_recv)) 116 hdlc->lqm.echo.seq_recv = lqr.sequence; 117 } else 118 log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n", 119 (u_long)lqr.signature, (u_long)SIGNATURE); 120 } else 121 log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %zd, expecting %ld !\n", 122 m_length(bp), (long)sizeof(struct echolqr)); 123 return bp; 124 } 125 126 void 127 lqr_ChangeOrder(struct lqrdata *src, struct lqrdata *dst) 128 { 129 u_int32_t *sp, *dp; 130 unsigned n; 131 132 sp = (u_int32_t *) src; 133 dp = (u_int32_t *) dst; 134 for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++, sp++, dp++) 135 *dp = ntohl(*sp); 136 } 137 138 static void 139 SendLqrData(struct lcp *lcp) 140 { 141 struct mbuf *bp; 142 int extra; 143 144 extra = proto_WrapperOctets(lcp, PROTO_LQR) + 145 acf_WrapperOctets(lcp, PROTO_LQR); 146 bp = m_get(sizeof(struct lqrdata) + extra, MB_LQROUT); 147 bp->m_len -= extra; 148 bp->m_offset += extra; 149 150 /* 151 * Send on the highest priority queue. We send garbage - the real data 152 * is written by lqr_LayerPush() where we know how to fill in all the 153 * fields. Note, lqr_LayerPush() ``knows'' that we're pushing onto the 154 * highest priority queue, and factors out packet & octet values from 155 * other queues! 156 */ 157 link_PushPacket(lcp->fsm.link, bp, lcp->fsm.bundle, 158 LINK_QUEUES(lcp->fsm.link) - 1, PROTO_LQR); 159 } 160 161 static void 162 SendLqrReport(void *v) 163 { 164 struct lcp *lcp = (struct lcp *)v; 165 struct physical *p = link2physical(lcp->fsm.link); 166 167 timer_Stop(&p->hdlc.lqm.timer); 168 169 if (p->hdlc.lqm.method & LQM_LQR) { 170 if (p->hdlc.lqm.lqr.resent > 5) { 171 /* XXX: Should implement LQM strategy */ 172 log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n", 173 lcp->fsm.link->name); 174 log_Printf(LogLQM, "%s: Too many LQR packets lost\n", 175 lcp->fsm.link->name); 176 p->hdlc.lqm.method = 0; 177 datalink_Down(p->dl, CLOSE_NORMAL); 178 } else { 179 SendLqrData(lcp); 180 p->hdlc.lqm.lqr.resent++; 181 } 182 } else if (p->hdlc.lqm.method & LQM_ECHO) { 183 if ((p->hdlc.lqm.echo.seq_sent > 5 && 184 p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) || 185 (p->hdlc.lqm.echo.seq_sent <= 5 && 186 p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) { 187 log_Printf(LogPHASE, "%s: ** Too many ECHO LQR packets lost **\n", 188 lcp->fsm.link->name); 189 log_Printf(LogLQM, "%s: Too many ECHO LQR packets lost\n", 190 lcp->fsm.link->name); 191 p->hdlc.lqm.method = 0; 192 datalink_Down(p->dl, CLOSE_NORMAL); 193 } else 194 SendEchoReq(lcp); 195 } 196 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load) 197 timer_Start(&p->hdlc.lqm.timer); 198 } 199 200 struct mbuf * 201 lqr_Input(struct bundle *bundle __unused, struct link *l, struct mbuf *bp) 202 { 203 struct physical *p = link2physical(l); 204 struct lcp *lcp = p->hdlc.lqm.owner; 205 int len; 206 207 if (p == NULL) { 208 log_Printf(LogERROR, "lqr_Input: Not a physical link - dropped\n"); 209 m_freem(bp); 210 return NULL; 211 } 212 213 len = m_length(bp); 214 if (len != sizeof(struct lqrdata)) 215 log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n", 216 len, (long)sizeof(struct lqrdata)); 217 else if (!IsAccepted(l->lcp.cfg.lqr) && !(p->hdlc.lqm.method & LQM_LQR)) { 218 bp = m_pullup(proto_Prepend(bp, PROTO_LQR, 0, 0)); 219 lcp_SendProtoRej(lcp, MBUF_CTOP(bp), bp->m_len); 220 } else { 221 struct lqrdata *lqr; 222 223 bp = m_pullup(bp); 224 lqr = (struct lqrdata *)MBUF_CTOP(bp); 225 if (ntohl(lqr->MagicNumber) != lcp->his_magic) 226 log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong," 227 " expecting 0x%08lx\n", 228 (u_long)ntohl(lqr->MagicNumber), (u_long)lcp->his_magic); 229 else { 230 struct lqrdata lastlqr; 231 232 memcpy(&lastlqr, &p->hdlc.lqm.lqr.peer, sizeof lastlqr); 233 lqr_ChangeOrder(lqr, &p->hdlc.lqm.lqr.peer); 234 lqr_Dump(l->name, "Input", &p->hdlc.lqm.lqr.peer); 235 /* we have received an LQR from our peer */ 236 p->hdlc.lqm.lqr.resent = 0; 237 238 /* Snapshot our state when the LQR packet was received */ 239 memcpy(&p->hdlc.lqm.lqr.prevSave, &p->hdlc.lqm.lqr.Save, 240 sizeof p->hdlc.lqm.lqr.prevSave); 241 p->hdlc.lqm.lqr.Save.InLQRs = ++p->hdlc.lqm.lqr.InLQRs; 242 p->hdlc.lqm.lqr.Save.InPackets = p->hdlc.lqm.ifInUniPackets; 243 p->hdlc.lqm.lqr.Save.InDiscards = p->hdlc.lqm.ifInDiscards; 244 p->hdlc.lqm.lqr.Save.InErrors = p->hdlc.lqm.ifInErrors; 245 p->hdlc.lqm.lqr.Save.InOctets = p->hdlc.lqm.lqr.InGoodOctets; 246 247 lqr_Analyse(&p->hdlc, &lastlqr, &p->hdlc.lqm.lqr.peer); 248 249 /* 250 * Generate an LQR response if we're not running an LQR timer OR 251 * two successive LQR's PeerInLQRs are the same. 252 */ 253 if (p->hdlc.lqm.timer.load == 0 || !(p->hdlc.lqm.method & LQM_LQR) || 254 (lastlqr.PeerInLQRs && 255 lastlqr.PeerInLQRs == p->hdlc.lqm.lqr.peer.PeerInLQRs)) 256 SendLqrData(lcp); 257 } 258 } 259 m_freem(bp); 260 return NULL; 261 } 262 263 /* 264 * When LCP is reached to opened state, We'll start LQM activity. 265 */ 266 267 static void 268 lqr_Setup(struct lcp *lcp) 269 { 270 struct physical *physical = link2physical(lcp->fsm.link); 271 272 physical->hdlc.lqm.lqr.resent = 0; 273 physical->hdlc.lqm.echo.seq_sent = 0; 274 physical->hdlc.lqm.echo.seq_recv = 0; 275 memset(&physical->hdlc.lqm.lqr.peer, '\0', 276 sizeof physical->hdlc.lqm.lqr.peer); 277 278 physical->hdlc.lqm.method = LQM_ECHO; 279 if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO)) 280 physical->hdlc.lqm.method |= LQM_LQR; 281 timer_Stop(&physical->hdlc.lqm.timer); 282 283 physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod; 284 if (lcp->his_lqrperiod) 285 log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n", 286 physical->link.name, lcp->his_lqrperiod / 100, 287 lcp->his_lqrperiod % 100); 288 289 if (lcp->want_lqrperiod) { 290 log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n", 291 physical->link.name, 292 physical->hdlc.lqm.method & LQM_LQR ? "LQR" : "ECHO LQR", 293 lcp->want_lqrperiod / 100, lcp->want_lqrperiod % 100); 294 physical->hdlc.lqm.timer.load = lcp->want_lqrperiod * SECTICKS / 100; 295 physical->hdlc.lqm.timer.func = SendLqrReport; 296 physical->hdlc.lqm.timer.name = "lqm"; 297 physical->hdlc.lqm.timer.arg = lcp; 298 } else { 299 physical->hdlc.lqm.timer.load = 0; 300 if (!lcp->his_lqrperiod) 301 log_Printf(LogLQM, "%s: LQR/ECHO LQR not negotiated\n", 302 physical->link.name); 303 } 304 } 305 306 void 307 lqr_Start(struct lcp *lcp) 308 { 309 struct physical *p = link2physical(lcp->fsm.link); 310 311 lqr_Setup(lcp); 312 if (p->hdlc.lqm.timer.load) 313 SendLqrReport(lcp); 314 } 315 316 void 317 lqr_reStart(struct lcp *lcp) 318 { 319 struct physical *p = link2physical(lcp->fsm.link); 320 321 lqr_Setup(lcp); 322 if (p->hdlc.lqm.timer.load) 323 timer_Start(&p->hdlc.lqm.timer); 324 } 325 326 void 327 lqr_StopTimer(struct physical *physical) 328 { 329 timer_Stop(&physical->hdlc.lqm.timer); 330 } 331 332 void 333 lqr_Stop(struct physical *physical, int method) 334 { 335 if (method == LQM_LQR) 336 log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n", 337 physical->link.name); 338 if (method == LQM_ECHO) 339 log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n", 340 physical->link.name); 341 physical->hdlc.lqm.method &= ~method; 342 if (physical->hdlc.lqm.method) 343 SendLqrReport(physical->hdlc.lqm.owner); 344 else 345 timer_Stop(&physical->hdlc.lqm.timer); 346 } 347 348 void 349 lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr) 350 { 351 if (log_IsKept(LogLQM)) { 352 log_Printf(LogLQM, "%s: %s:\n", link, message); 353 log_Printf(LogLQM, " Magic: %08x LastOutLQRs: %08x\n", 354 lqr->MagicNumber, lqr->LastOutLQRs); 355 log_Printf(LogLQM, " LastOutPackets: %08x LastOutOctets: %08x\n", 356 lqr->LastOutPackets, lqr->LastOutOctets); 357 log_Printf(LogLQM, " PeerInLQRs: %08x PeerInPackets: %08x\n", 358 lqr->PeerInLQRs, lqr->PeerInPackets); 359 log_Printf(LogLQM, " PeerInDiscards: %08x PeerInErrors: %08x\n", 360 lqr->PeerInDiscards, lqr->PeerInErrors); 361 log_Printf(LogLQM, " PeerInOctets: %08x PeerOutLQRs: %08x\n", 362 lqr->PeerInOctets, lqr->PeerOutLQRs); 363 log_Printf(LogLQM, " PeerOutPackets: %08x PeerOutOctets: %08x\n", 364 lqr->PeerOutPackets, lqr->PeerOutOctets); 365 } 366 } 367 368 void 369 lqr_Analyse(const struct hdlc *hdlc, const struct lqrdata *oldlqr, 370 const struct lqrdata *newlqr) 371 { 372 u_int32_t LQRs, transitLQRs, pkts, octets, disc, err; 373 374 if (!newlqr->PeerInLQRs) /* No analysis possible yet! */ 375 return; 376 377 log_Printf(LogLQM, "Analysis:\n"); 378 379 LQRs = (newlqr->LastOutLQRs - oldlqr->LastOutLQRs) - 380 (newlqr->PeerInLQRs - oldlqr->PeerInLQRs); 381 transitLQRs = hdlc->lqm.lqr.OutLQRs - newlqr->LastOutLQRs; 382 pkts = (newlqr->LastOutPackets - oldlqr->LastOutPackets) - 383 (newlqr->PeerInPackets - oldlqr->PeerInPackets); 384 octets = (newlqr->LastOutOctets - oldlqr->LastOutOctets) - 385 (newlqr->PeerInOctets - oldlqr->PeerInOctets); 386 log_Printf(LogLQM, " Outbound lossage: %d LQR%s (%d en route), %d packet%s," 387 " %d octet%s\n", (int)LQRs, LQRs == 1 ? "" : "s", (int)transitLQRs, 388 (int)pkts, pkts == 1 ? "" : "s", 389 (int)octets, octets == 1 ? "" : "s"); 390 391 pkts = (newlqr->PeerOutPackets - oldlqr->PeerOutPackets) - 392 (hdlc->lqm.lqr.Save.InPackets - hdlc->lqm.lqr.prevSave.InPackets); 393 octets = (newlqr->PeerOutOctets - oldlqr->PeerOutOctets) - 394 (hdlc->lqm.lqr.Save.InOctets - hdlc->lqm.lqr.prevSave.InOctets); 395 log_Printf(LogLQM, " Inbound lossage: %d packet%s, %d octet%s\n", 396 (int)pkts, pkts == 1 ? "" : "s", 397 (int)octets, octets == 1 ? "" : "s"); 398 399 disc = newlqr->PeerInDiscards - oldlqr->PeerInDiscards; 400 err = newlqr->PeerInErrors - oldlqr->PeerInErrors; 401 if (disc && err) 402 log_Printf(LogLQM, " Likely due to both peer congestion" 403 " and physical errors\n"); 404 else if (disc) 405 log_Printf(LogLQM, " Likely due to peer congestion\n"); 406 else if (err) 407 log_Printf(LogLQM, " Likely due to physical errors\n"); 408 else if (pkts) 409 log_Printf(LogLQM, " Likely due to transport " 410 "congestion\n"); 411 } 412 413 static struct mbuf * 414 lqr_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp, 415 int pri __unused, u_short *proto) 416 { 417 struct physical *p = link2physical(l); 418 int len, layer, extra_async_bytes; 419 420 if (!p) { 421 /* Oops - can't happen :-] */ 422 m_freem(bp); 423 return NULL; 424 } 425 426 bp = m_pullup(bp); 427 len = m_length(bp); 428 429 /*- 430 * From rfc1989: 431 * 432 * All octets which are included in the FCS calculation MUST be counted, 433 * including the packet header, the information field, and any padding. 434 * The FCS octets MUST also be counted, and one flag octet per frame 435 * MUST be counted. All other octets (such as additional flag 436 * sequences, and escape bits or octets) MUST NOT be counted. 437 * 438 * As we're stacked higher than the HDLC layer (otherwise HDLC wouldn't be 439 * able to calculate the FCS), we must not forget about these additional 440 * bytes when we're asynchronous. 441 * 442 * We're also expecting to be stacked *before* the likes of the proto and 443 * acf layers (to avoid alignment issues), so deal with this too. 444 */ 445 446 extra_async_bytes = 0; 447 p->hdlc.lqm.ifOutUniPackets++; 448 p->hdlc.lqm.ifOutOctets += len + 1; /* plus 1 flag octet! */ 449 for (layer = 0; layer < l->nlayers; layer++) 450 switch (l->layer[layer]->type) { 451 case LAYER_ACF: 452 p->hdlc.lqm.ifOutOctets += acf_WrapperOctets(&l->lcp, *proto); 453 break; 454 case LAYER_ASYNC: 455 /* Not included - see rfc1989 */ 456 break; 457 case LAYER_HDLC: 458 p->hdlc.lqm.ifOutOctets += hdlc_WrapperOctets(); 459 break; 460 case LAYER_LQR: 461 layer = l->nlayers; 462 break; 463 case LAYER_PROTO: 464 p->hdlc.lqm.ifOutOctets += proto_WrapperOctets(&l->lcp, *proto); 465 break; 466 case LAYER_SYNC: 467 /* Nothing to add on */ 468 break; 469 default: 470 log_Printf(LogWARN, "Oops, don't know how to do octets for %s layer\n", 471 l->layer[layer]->name); 472 break; 473 } 474 475 if (*proto == PROTO_LQR) { 476 /* Overwrite the entire packet (created in SendLqrData()) */ 477 struct lqrdata lqr; 478 size_t pending_pkts, pending_octets; 479 480 p->hdlc.lqm.lqr.OutLQRs++; 481 482 /* 483 * We need to compensate for the fact that we're pushing our data 484 * onto the highest priority queue by factoring out packet & octet 485 * values from other queues! 486 */ 487 link_PendingLowPriorityData(l, &pending_pkts, &pending_octets); 488 489 memset(&lqr, '\0', sizeof lqr); 490 lqr.MagicNumber = p->link.lcp.want_magic; 491 lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs; 492 lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets; 493 lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets; 494 lqr.PeerInLQRs = p->hdlc.lqm.lqr.Save.InLQRs; 495 lqr.PeerInPackets = p->hdlc.lqm.lqr.Save.InPackets; 496 lqr.PeerInDiscards = p->hdlc.lqm.lqr.Save.InDiscards; 497 lqr.PeerInErrors = p->hdlc.lqm.lqr.Save.InErrors; 498 lqr.PeerInOctets = p->hdlc.lqm.lqr.Save.InOctets; 499 lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs; 500 lqr.PeerOutPackets = p->hdlc.lqm.ifOutUniPackets - pending_pkts; 501 /* Don't forget our ``flag'' octets.... */ 502 lqr.PeerOutOctets = p->hdlc.lqm.ifOutOctets - pending_octets - pending_pkts; 503 lqr_Dump(l->name, "Output", &lqr); 504 lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp)); 505 } 506 507 return bp; 508 } 509 510 static struct mbuf * 511 lqr_LayerPull(struct bundle *b __unused, struct link *l __unused, 512 struct mbuf *bp, u_short *proto) 513 { 514 /* 515 * This is the ``Rx'' process from rfc1989, although a part of it is 516 * actually performed by sync_LayerPull() & hdlc_LayerPull() so that 517 * our octet counts are correct. 518 */ 519 520 if (*proto == PROTO_LQR) 521 m_settype(bp, MB_LQRIN); 522 return bp; 523 } 524 525 /* 526 * Statistics for pulled packets are recorded either in hdlc_PullPacket() 527 * or sync_PullPacket() 528 */ 529 530 struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull }; 531