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