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