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 LCP ECHO packets lost **\n", 188 lcp->fsm.link->name); 189 log_Printf(LogLQM, "%s: Too many LCP ECHO 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 static void 267 lqr_Setup(struct lcp *lcp) 268 { 269 struct physical *physical = link2physical(lcp->fsm.link); 270 int period; 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 = lcp->cfg.echo ? LQM_ECHO : 0; 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 period = lcp->want_lqrperiod ? 290 lcp->want_lqrperiod : lcp->cfg.lqrperiod * 100; 291 physical->hdlc.lqm.timer.func = SendLqrReport; 292 physical->hdlc.lqm.timer.name = "lqm"; 293 physical->hdlc.lqm.timer.arg = lcp; 294 295 if (lcp->want_lqrperiod || physical->hdlc.lqm.method & LQM_ECHO) { 296 log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n", 297 physical->link.name, lcp->want_lqrperiod ? "LQR" : "LCP ECHO", 298 period / 100, period % 100); 299 physical->hdlc.lqm.timer.load = period * SECTICKS / 100; 300 } else { 301 physical->hdlc.lqm.timer.load = 0; 302 if (!lcp->his_lqrperiod) 303 log_Printf(LogLQM, "%s: LQR/LCP ECHO not negotiated\n", 304 physical->link.name); 305 } 306 } 307 308 void 309 lqr_Start(struct lcp *lcp) 310 { 311 struct physical *p = link2physical(lcp->fsm.link); 312 313 lqr_Setup(lcp); 314 if (p->hdlc.lqm.timer.load) 315 SendLqrReport(lcp); 316 } 317 318 void 319 lqr_reStart(struct lcp *lcp) 320 { 321 struct physical *p = link2physical(lcp->fsm.link); 322 323 lqr_Setup(lcp); 324 if (p->hdlc.lqm.timer.load) 325 timer_Start(&p->hdlc.lqm.timer); 326 } 327 328 void 329 lqr_StopTimer(struct physical *physical) 330 { 331 timer_Stop(&physical->hdlc.lqm.timer); 332 } 333 334 void 335 lqr_Stop(struct physical *physical, int method) 336 { 337 if (method == LQM_LQR) 338 log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n", 339 physical->link.name); 340 if (method == LQM_ECHO) 341 log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n", 342 physical->link.name); 343 physical->hdlc.lqm.method &= ~method; 344 if (physical->hdlc.lqm.method) 345 SendLqrReport(physical->hdlc.lqm.owner); 346 else 347 timer_Stop(&physical->hdlc.lqm.timer); 348 } 349 350 void 351 lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr) 352 { 353 if (log_IsKept(LogLQM)) { 354 log_Printf(LogLQM, "%s: %s:\n", link, message); 355 log_Printf(LogLQM, " Magic: %08x LastOutLQRs: %08x\n", 356 lqr->MagicNumber, lqr->LastOutLQRs); 357 log_Printf(LogLQM, " LastOutPackets: %08x LastOutOctets: %08x\n", 358 lqr->LastOutPackets, lqr->LastOutOctets); 359 log_Printf(LogLQM, " PeerInLQRs: %08x PeerInPackets: %08x\n", 360 lqr->PeerInLQRs, lqr->PeerInPackets); 361 log_Printf(LogLQM, " PeerInDiscards: %08x PeerInErrors: %08x\n", 362 lqr->PeerInDiscards, lqr->PeerInErrors); 363 log_Printf(LogLQM, " PeerInOctets: %08x PeerOutLQRs: %08x\n", 364 lqr->PeerInOctets, lqr->PeerOutLQRs); 365 log_Printf(LogLQM, " PeerOutPackets: %08x PeerOutOctets: %08x\n", 366 lqr->PeerOutPackets, lqr->PeerOutOctets); 367 } 368 } 369 370 void 371 lqr_Analyse(const struct hdlc *hdlc, const struct lqrdata *oldlqr, 372 const struct lqrdata *newlqr) 373 { 374 u_int32_t LQRs, transitLQRs, pkts, octets, disc, err; 375 376 if (!newlqr->PeerInLQRs) /* No analysis possible yet! */ 377 return; 378 379 log_Printf(LogLQM, "Analysis:\n"); 380 381 LQRs = (newlqr->LastOutLQRs - oldlqr->LastOutLQRs) - 382 (newlqr->PeerInLQRs - oldlqr->PeerInLQRs); 383 transitLQRs = hdlc->lqm.lqr.OutLQRs - newlqr->LastOutLQRs; 384 pkts = (newlqr->LastOutPackets - oldlqr->LastOutPackets) - 385 (newlqr->PeerInPackets - oldlqr->PeerInPackets); 386 octets = (newlqr->LastOutOctets - oldlqr->LastOutOctets) - 387 (newlqr->PeerInOctets - oldlqr->PeerInOctets); 388 log_Printf(LogLQM, " Outbound lossage: %d LQR%s (%d en route), %d packet%s," 389 " %d octet%s\n", (int)LQRs, LQRs == 1 ? "" : "s", (int)transitLQRs, 390 (int)pkts, pkts == 1 ? "" : "s", 391 (int)octets, octets == 1 ? "" : "s"); 392 393 pkts = (newlqr->PeerOutPackets - oldlqr->PeerOutPackets) - 394 (hdlc->lqm.lqr.Save.InPackets - hdlc->lqm.lqr.prevSave.InPackets); 395 octets = (newlqr->PeerOutOctets - oldlqr->PeerOutOctets) - 396 (hdlc->lqm.lqr.Save.InOctets - hdlc->lqm.lqr.prevSave.InOctets); 397 log_Printf(LogLQM, " Inbound lossage: %d packet%s, %d octet%s\n", 398 (int)pkts, pkts == 1 ? "" : "s", 399 (int)octets, octets == 1 ? "" : "s"); 400 401 disc = newlqr->PeerInDiscards - oldlqr->PeerInDiscards; 402 err = newlqr->PeerInErrors - oldlqr->PeerInErrors; 403 if (disc && err) 404 log_Printf(LogLQM, " Likely due to both peer congestion" 405 " and physical errors\n"); 406 else if (disc) 407 log_Printf(LogLQM, " Likely due to peer congestion\n"); 408 else if (err) 409 log_Printf(LogLQM, " Likely due to physical errors\n"); 410 else if (pkts) 411 log_Printf(LogLQM, " Likely due to transport " 412 "congestion\n"); 413 } 414 415 static struct mbuf * 416 lqr_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp, 417 int pri __unused, u_short *proto) 418 { 419 struct physical *p = link2physical(l); 420 int len, layer; 421 422 if (!p) { 423 /* Oops - can't happen :-] */ 424 m_freem(bp); 425 return NULL; 426 } 427 428 bp = m_pullup(bp); 429 len = m_length(bp); 430 431 /*- 432 * From rfc1989: 433 * 434 * All octets which are included in the FCS calculation MUST be counted, 435 * including the packet header, the information field, and any padding. 436 * The FCS octets MUST also be counted, and one flag octet per frame 437 * MUST be counted. All other octets (such as additional flag 438 * sequences, and escape bits or octets) MUST NOT be counted. 439 * 440 * As we're stacked higher than the HDLC layer (otherwise HDLC wouldn't be 441 * able to calculate the FCS), we must not forget about these additional 442 * bytes when we're asynchronous. 443 * 444 * We're also expecting to be stacked *before* the likes of the proto and 445 * acf layers (to avoid alignment issues), so deal with this too. 446 */ 447 448 p->hdlc.lqm.ifOutUniPackets++; 449 p->hdlc.lqm.ifOutOctets += len + 1; /* plus 1 flag octet! */ 450 for (layer = 0; layer < l->nlayers; layer++) 451 switch (l->layer[layer]->type) { 452 case LAYER_ACF: 453 p->hdlc.lqm.ifOutOctets += acf_WrapperOctets(&l->lcp, *proto); 454 break; 455 case LAYER_ASYNC: 456 /* Not included - see rfc1989 */ 457 break; 458 case LAYER_HDLC: 459 p->hdlc.lqm.ifOutOctets += hdlc_WrapperOctets(); 460 break; 461 case LAYER_LQR: 462 layer = l->nlayers; 463 break; 464 case LAYER_PROTO: 465 p->hdlc.lqm.ifOutOctets += proto_WrapperOctets(&l->lcp, *proto); 466 break; 467 case LAYER_SYNC: 468 /* Nothing to add on */ 469 break; 470 default: 471 log_Printf(LogWARN, "Oops, don't know how to do octets for %s layer\n", 472 l->layer[layer]->name); 473 break; 474 } 475 476 if (*proto == PROTO_LQR) { 477 /* Overwrite the entire packet (created in SendLqrData()) */ 478 struct lqrdata lqr; 479 size_t pending_pkts, pending_octets; 480 481 p->hdlc.lqm.lqr.OutLQRs++; 482 483 /* 484 * We need to compensate for the fact that we're pushing our data 485 * onto the highest priority queue by factoring out packet & octet 486 * values from other queues! 487 */ 488 link_PendingLowPriorityData(l, &pending_pkts, &pending_octets); 489 490 memset(&lqr, '\0', sizeof lqr); 491 lqr.MagicNumber = p->link.lcp.want_magic; 492 lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs; 493 lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets; 494 lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets; 495 lqr.PeerInLQRs = p->hdlc.lqm.lqr.Save.InLQRs; 496 lqr.PeerInPackets = p->hdlc.lqm.lqr.Save.InPackets; 497 lqr.PeerInDiscards = p->hdlc.lqm.lqr.Save.InDiscards; 498 lqr.PeerInErrors = p->hdlc.lqm.lqr.Save.InErrors; 499 lqr.PeerInOctets = p->hdlc.lqm.lqr.Save.InOctets; 500 lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs; 501 lqr.PeerOutPackets = p->hdlc.lqm.ifOutUniPackets - pending_pkts; 502 /* Don't forget our ``flag'' octets.... */ 503 lqr.PeerOutOctets = p->hdlc.lqm.ifOutOctets - pending_octets - pending_pkts; 504 lqr_Dump(l->name, "Output", &lqr); 505 lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp)); 506 } 507 508 return bp; 509 } 510 511 static struct mbuf * 512 lqr_LayerPull(struct bundle *b __unused, struct link *l __unused, 513 struct mbuf *bp, u_short *proto) 514 { 515 /* 516 * This is the ``Rx'' process from rfc1989, although a part of it is 517 * actually performed by sync_LayerPull() & hdlc_LayerPull() so that 518 * our octet counts are correct. 519 */ 520 521 if (*proto == PROTO_LQR) 522 m_settype(bp, MB_LQRIN); 523 return bp; 524 } 525 526 /* 527 * Statistics for pulled packets are recorded either in hdlc_PullPacket() 528 * or sync_PullPacket() 529 */ 530 531 struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull }; 532