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