xref: /freebsd/usr.sbin/ppp/lqr.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 #include <sys/un.h>
33 
34 #include <string.h>
35 #include <termios.h>
36 
37 #include "layer.h"
38 #include "mbuf.h"
39 #include "log.h"
40 #include "defs.h"
41 #include "timer.h"
42 #include "fsm.h"
43 #include "acf.h"
44 #include "proto.h"
45 #include "lqr.h"
46 #include "hdlc.h"
47 #include "lcp.h"
48 #include "async.h"
49 #include "throughput.h"
50 #include "ccp.h"
51 #include "link.h"
52 #include "descriptor.h"
53 #include "physical.h"
54 #include "mp.h"
55 #include "chat.h"
56 #include "auth.h"
57 #include "chap.h"
58 #include "command.h"
59 #include "cbcp.h"
60 #include "datalink.h"
61 
62 struct echolqr {
63   u_int32_t magic;
64   u_int32_t signature;
65   u_int32_t sequence;
66 };
67 
68 #define	SIGNATURE  0x594e4f54
69 
70 static void
71 SendEchoReq(struct lcp *lcp)
72 {
73   struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc;
74   struct echolqr echo;
75 
76   echo.magic = htonl(lcp->want_magic);
77   echo.signature = htonl(SIGNATURE);
78   echo.sequence = htonl(hdlc->lqm.echo.seq_sent);
79   fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++,
80             (u_char *)&echo, sizeof echo, MB_ECHOOUT);
81 }
82 
83 struct mbuf *
84 lqr_RecvEcho(struct fsm *fp, struct mbuf *bp)
85 {
86   struct hdlc *hdlc = &link2physical(fp->link)->hdlc;
87   struct lcp *lcp = fsm2lcp(fp);
88   struct echolqr lqr;
89 
90   if (m_length(bp) >= sizeof lqr) {
91     m_freem(mbuf_Read(bp, &lqr, sizeof lqr));
92     bp = NULL;
93     lqr.magic = ntohl(lqr.magic);
94     lqr.signature = ntohl(lqr.signature);
95     lqr.sequence = ntohl(lqr.sequence);
96 
97     /* Tolerate echo replies with either magic number */
98     if (lqr.magic != 0 && lqr.magic != lcp->his_magic &&
99         lqr.magic != lcp->want_magic) {
100       log_Printf(LogWARN, "%s: lqr_RecvEcho: Bad magic: expected 0x%08x,"
101                  " got 0x%08x\n", fp->link->name, lcp->his_magic, lqr.magic);
102       /*
103        * XXX: We should send a terminate request. But poor implementations may
104        *      die as a result.
105        */
106     }
107     if (lqr.signature == SIGNATURE) {
108       /* careful not to update lqm.echo.seq_recv with older values */
109       if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && lqr.sequence < 5) ||
110           (hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 &&
111            lqr.sequence > hdlc->lqm.echo.seq_recv))
112         hdlc->lqm.echo.seq_recv = lqr.sequence;
113     } else
114       log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n",
115                 (u_long)lqr.signature, (u_long)SIGNATURE);
116   } else
117     log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %d, expecting %ld !\n",
118               m_length(bp), (long)sizeof(struct echolqr));
119   return bp;
120 }
121 
122 void
123 lqr_ChangeOrder(struct lqrdata *src, struct lqrdata *dst)
124 {
125   u_int32_t *sp, *dp;
126   int n;
127 
128   sp = (u_int32_t *) src;
129   dp = (u_int32_t *) dst;
130   for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++, sp++, dp++)
131     *dp = ntohl(*sp);
132 }
133 
134 static void
135 SendLqrData(struct lcp *lcp)
136 {
137   struct mbuf *bp;
138   int extra;
139 
140   extra = proto_WrapperOctets(lcp, PROTO_LQR) +
141           acf_WrapperOctets(lcp, PROTO_LQR);
142   bp = m_get(sizeof(struct lqrdata) + extra, MB_LQROUT);
143   bp->m_len -= extra;
144   bp->m_offset += extra;
145   link_PushPacket(lcp->fsm.link, bp, lcp->fsm.bundle,
146                   LINK_QUEUES(lcp->fsm.link) - 1, PROTO_LQR);
147 }
148 
149 static void
150 SendLqrReport(void *v)
151 {
152   struct lcp *lcp = (struct lcp *)v;
153   struct physical *p = link2physical(lcp->fsm.link);
154 
155   timer_Stop(&p->hdlc.lqm.timer);
156 
157   if (p->hdlc.lqm.method & LQM_LQR) {
158     if (p->hdlc.lqm.lqr.resent > 5) {
159       /* XXX: Should implement LQM strategy */
160       log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n",
161                 lcp->fsm.link->name);
162       log_Printf(LogLQM, "%s: Too many LQR packets lost\n",
163                 lcp->fsm.link->name);
164       p->hdlc.lqm.method = 0;
165       datalink_Down(p->dl, CLOSE_NORMAL);
166     } else {
167       SendLqrData(lcp);
168       p->hdlc.lqm.lqr.resent++;
169     }
170   } else if (p->hdlc.lqm.method & LQM_ECHO) {
171     if ((p->hdlc.lqm.echo.seq_sent > 5 &&
172          p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) ||
173         (p->hdlc.lqm.echo.seq_sent <= 5 &&
174          p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) {
175       log_Printf(LogPHASE, "%s: ** Too many ECHO LQR packets lost **\n",
176                 lcp->fsm.link->name);
177       log_Printf(LogLQM, "%s: Too many ECHO LQR packets lost\n",
178                 lcp->fsm.link->name);
179       p->hdlc.lqm.method = 0;
180       datalink_Down(p->dl, CLOSE_NORMAL);
181     } else
182       SendEchoReq(lcp);
183   }
184   if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
185     timer_Start(&p->hdlc.lqm.timer);
186 }
187 
188 struct mbuf *
189 lqr_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
190 {
191   struct physical *p = link2physical(l);
192   struct lcp *lcp = p->hdlc.lqm.owner;
193   int len;
194 
195   if (p == NULL) {
196     log_Printf(LogERROR, "lqr_Input: Not a physical link - dropped\n");
197     m_freem(bp);
198     return NULL;
199   }
200 
201   p->hdlc.lqm.lqr.SaveInLQRs++;
202 
203   len = m_length(bp);
204   if (len != sizeof(struct lqrdata))
205     log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n",
206               len, (long)sizeof(struct lqrdata));
207   else if (!IsAccepted(l->lcp.cfg.lqr) && !(p->hdlc.lqm.method & LQM_LQR)) {
208     bp = m_pullup(proto_Prepend(bp, PROTO_LQR, 0, 0));
209     lcp_SendProtoRej(lcp, MBUF_CTOP(bp), bp->m_len);
210   } else {
211     struct lqrdata *lqr;
212     u_int32_t lastLQR;
213 
214     bp = m_pullup(bp);
215     lqr = (struct lqrdata *)MBUF_CTOP(bp);
216     if (ntohl(lqr->MagicNumber) != lcp->his_magic)
217       log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong,"
218                  " expecting 0x%08lx\n",
219 		 (u_long)ntohl(lqr->MagicNumber), (u_long)lcp->his_magic);
220     else {
221       /*
222        * Remember our PeerInLQRs, then convert byte order and save
223        */
224       lastLQR = p->hdlc.lqm.lqr.peer.PeerInLQRs;
225 
226       lqr_ChangeOrder(lqr, &p->hdlc.lqm.lqr.peer);
227       lqr_Dump(l->name, "Input", &p->hdlc.lqm.lqr.peer);
228       /* we have received an LQR from peer */
229       p->hdlc.lqm.lqr.resent = 0;
230 
231       /*
232        * Generate an LQR response if we're not running an LQR timer OR
233        * two successive LQR's PeerInLQRs are the same OR we're not going to
234        * send our next one before the peers max timeout.
235        */
236       if (p->hdlc.lqm.timer.load == 0 ||
237           !(p->hdlc.lqm.method & LQM_LQR) ||
238           (lastLQR && lastLQR == p->hdlc.lqm.lqr.peer.PeerInLQRs) ||
239           (p->hdlc.lqm.lqr.peer_timeout &&
240            p->hdlc.lqm.timer.rest * 100 / SECTICKS >
241            p->hdlc.lqm.lqr.peer_timeout))
242         SendLqrData(lcp);
243     }
244   }
245   m_freem(bp);
246   return NULL;
247 }
248 
249 /*
250  *  When LCP is reached to opened state, We'll start LQM activity.
251  */
252 
253 static void
254 lqr_Setup(struct lcp *lcp)
255 {
256   struct physical *physical = link2physical(lcp->fsm.link);
257 
258   physical->hdlc.lqm.lqr.resent = 0;
259   physical->hdlc.lqm.echo.seq_sent = 0;
260   physical->hdlc.lqm.echo.seq_recv = 0;
261   memset(&physical->hdlc.lqm.lqr.peer, '\0',
262          sizeof physical->hdlc.lqm.lqr.peer);
263 
264   physical->hdlc.lqm.method = LQM_ECHO;
265   if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO))
266     physical->hdlc.lqm.method |= LQM_LQR;
267   timer_Stop(&physical->hdlc.lqm.timer);
268 
269   physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod;
270   if (lcp->his_lqrperiod)
271     log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n",
272               physical->link.name, lcp->his_lqrperiod / 100,
273               lcp->his_lqrperiod % 100);
274 
275   if (lcp->want_lqrperiod) {
276     log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n",
277               physical->link.name,
278               physical->hdlc.lqm.method & LQM_LQR ? "LQR" : "ECHO LQR",
279               lcp->want_lqrperiod / 100, lcp->want_lqrperiod % 100);
280     physical->hdlc.lqm.timer.load = lcp->want_lqrperiod * SECTICKS / 100;
281     physical->hdlc.lqm.timer.func = SendLqrReport;
282     physical->hdlc.lqm.timer.name = "lqm";
283     physical->hdlc.lqm.timer.arg = lcp;
284   } else {
285     physical->hdlc.lqm.timer.load = 0;
286     if (!lcp->his_lqrperiod)
287       log_Printf(LogLQM, "%s: LQR/ECHO LQR not negotiated\n",
288                  physical->link.name);
289   }
290 }
291 
292 void
293 lqr_Start(struct lcp *lcp)
294 {
295   struct physical *p = link2physical(lcp->fsm.link);
296 
297   lqr_Setup(lcp);
298   if (p->hdlc.lqm.timer.load)
299     SendLqrReport(lcp);
300 }
301 
302 void
303 lqr_reStart(struct lcp *lcp)
304 {
305   struct physical *p = link2physical(lcp->fsm.link);
306 
307   lqr_Setup(lcp);
308   if (p->hdlc.lqm.timer.load)
309     timer_Start(&p->hdlc.lqm.timer);
310 }
311 
312 void
313 lqr_StopTimer(struct physical *physical)
314 {
315   timer_Stop(&physical->hdlc.lqm.timer);
316 }
317 
318 void
319 lqr_Stop(struct physical *physical, int method)
320 {
321   if (method == LQM_LQR)
322     log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n",
323                physical->link.name);
324   if (method == LQM_ECHO)
325     log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n",
326                physical->link.name);
327   physical->hdlc.lqm.method &= ~method;
328   if (physical->hdlc.lqm.method)
329     SendLqrReport(physical->hdlc.lqm.owner);
330   else
331     timer_Stop(&physical->hdlc.lqm.timer);
332 }
333 
334 void
335 lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr)
336 {
337   if (log_IsKept(LogLQM)) {
338     log_Printf(LogLQM, "%s: %s:\n", link, message);
339     log_Printf(LogLQM, "  Magic:          %08x   LastOutLQRs:    %08x\n",
340 	      lqr->MagicNumber, lqr->LastOutLQRs);
341     log_Printf(LogLQM, "  LastOutPackets: %08x   LastOutOctets:  %08x\n",
342 	      lqr->LastOutPackets, lqr->LastOutOctets);
343     log_Printf(LogLQM, "  PeerInLQRs:     %08x   PeerInPackets:  %08x\n",
344 	      lqr->PeerInLQRs, lqr->PeerInPackets);
345     log_Printf(LogLQM, "  PeerInDiscards: %08x   PeerInErrors:   %08x\n",
346 	      lqr->PeerInDiscards, lqr->PeerInErrors);
347     log_Printf(LogLQM, "  PeerInOctets:   %08x   PeerOutLQRs:    %08x\n",
348 	      lqr->PeerInOctets, lqr->PeerOutLQRs);
349     log_Printf(LogLQM, "  PeerOutPackets: %08x   PeerOutOctets:  %08x\n",
350 	      lqr->PeerOutPackets, lqr->PeerOutOctets);
351   }
352 }
353 
354 static struct mbuf *
355 lqr_LayerPush(struct bundle *b, struct link *l, struct mbuf *bp,
356               int pri, u_short *proto)
357 {
358   struct physical *p = link2physical(l);
359   int len;
360 
361   if (!p) {
362     /* Oops - can't happen :-] */
363     m_freem(bp);
364     return NULL;
365   }
366 
367   /*
368    * From rfc1989:
369    *
370    *  All octets which are included in the FCS calculation MUST be counted,
371    *  including the packet header, the information field, and any padding.
372    *  The FCS octets MUST also be counted, and one flag octet per frame
373    *  MUST be counted.  All other octets (such as additional flag
374    *  sequences, and escape bits or octets) MUST NOT be counted.
375    *
376    * As we're stacked before the HDLC layer (otherwise HDLC wouldn't be
377    * able to calculate the FCS), we must not forget about these additional
378    * bytes when we're asynchronous.
379    *
380    * We're also expecting to be stacked *before* the proto and acf layers.
381    * If we were after these, it makes alignment more of a pain, and we
382    * don't do LQR without these layers.
383    */
384 
385   bp = m_pullup(bp);
386   len = m_length(bp);
387 
388   if (!physical_IsSync(p))
389     p->hdlc.lqm.OutOctets += hdlc_WrapperOctets(&l->lcp, *proto);
390   p->hdlc.lqm.OutOctets += acf_WrapperOctets(&l->lcp, *proto) +
391                            proto_WrapperOctets(&l->lcp, *proto) + len + 1;
392   p->hdlc.lqm.OutPackets++;
393 
394   if (*proto == PROTO_LQR) {
395     /* Overwrite the entire packet (created in SendLqrData()) */
396     struct lqrdata lqr;
397 
398     lqr.MagicNumber = p->link.lcp.want_magic;
399     lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
400     lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
401     lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
402     lqr.PeerInLQRs = p->hdlc.lqm.lqr.SaveInLQRs;
403     lqr.PeerInPackets = p->hdlc.lqm.SaveInPackets;
404     lqr.PeerInDiscards = p->hdlc.lqm.SaveInDiscards;
405     lqr.PeerInErrors = p->hdlc.lqm.SaveInErrors;
406     lqr.PeerInOctets = p->hdlc.lqm.SaveInOctets;
407     lqr.PeerOutPackets = p->hdlc.lqm.OutPackets;
408     lqr.PeerOutOctets = p->hdlc.lqm.OutOctets;
409     if (p->hdlc.lqm.lqr.peer.LastOutLQRs == p->hdlc.lqm.lqr.OutLQRs) {
410       /*
411        * only increment if it's the first time or we've got a reply
412        * from the last one
413        */
414       lqr.PeerOutLQRs = ++p->hdlc.lqm.lqr.OutLQRs;
415       lqr_Dump(l->name, "Output", &lqr);
416     } else {
417       lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
418       lqr_Dump(l->name, "Output (again)", &lqr);
419     }
420     lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp));
421   }
422 
423   return bp;
424 }
425 
426 static struct mbuf *
427 lqr_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp, u_short *proto)
428 {
429   /*
430    * We mark the packet as ours but don't do anything 'till it's dispatched
431    * to lqr_Input()
432    */
433   if (*proto == PROTO_LQR)
434     m_settype(bp, MB_LQRIN);
435   return bp;
436 }
437 
438 /*
439  * Statistics for pulled packets are recorded either in hdlc_PullPacket()
440  * or sync_PullPacket()
441  */
442 
443 struct layer lqrlayer = { LAYER_LQR, "lqr", lqr_LayerPush, lqr_LayerPull };
444