]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
Initial revision
[ppp.git] / pppd / lcp.c
1 /*
2  * lcp.c - PPP Link Control Protocol.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Carnegie Mellon University.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #ifndef lint
21 static char rcsid[] = "$Id: lcp.c,v 1.6 1994/05/26 06:37:19 paulus Exp $";
22 #endif
23
24 /*
25  * TODO:
26  */
27
28 #include <stdio.h>
29 #include <syslog.h>
30 #include <assert.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <netinet/in.h>
36
37 #include <string.h>
38
39 #include "pppd.h"
40 #include "ppp.h"
41 #include "fsm.h"
42 #include "lcp.h"
43 #include "magic.h"
44 #include "chap.h"
45 #include "upap.h"
46 #include "ipcp.h"
47
48 /* global vars */
49 fsm lcp_fsm[NPPP];                      /* LCP fsm structure (global)*/
50 lcp_options lcp_wantoptions[NPPP];      /* Options that we want to request */
51 lcp_options lcp_gotoptions[NPPP];       /* Options that peer ack'd */
52 lcp_options lcp_allowoptions[NPPP];     /* Options we allow peer to request */
53 lcp_options lcp_hisoptions[NPPP];       /* Options that we ack'd */
54 u_long xmit_accm[NPPP][8];              /* extended transmit ACCM */
55
56 static u_long lcp_echos_pending = 0;    /* Number of outstanding echo msgs */
57 static u_long lcp_echo_number   = 0;    /* ID number of next echo frame */
58 static u_long lcp_echo_timer_running = 0;  /* TRUE if a timer is running */
59
60 u_long lcp_echo_interval = 0;
61 u_long lcp_echo_fails = 0;
62
63 /*
64  * Callbacks for fsm code.  (CI = Configuration Information)
65  */
66 static void lcp_resetci __ARGS((fsm *));        /* Reset our CI */
67 static int  lcp_cilen __ARGS((fsm *));          /* Return length of our CI */
68 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
69 static int  lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
70 static int  lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
71 static int  lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
72 static int  lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
73 static void lcp_up __ARGS((fsm *));             /* We're UP */
74 static void lcp_down __ARGS((fsm *));           /* We're DOWN */
75 static void lcp_starting __ARGS((fsm *));       /* We need lower layer up */
76 static void lcp_finished __ARGS((fsm *));       /* We need lower layer down */
77 static int  lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
78 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
79
80 /*
81  * routines to send LCP echos to peer
82  */
83
84 static void lcp_echo_lowerup __ARGS((int));
85 static void lcp_echo_lowerdown __ARGS((int));
86 static void LcpEchoTimeout __ARGS((caddr_t));
87 static void lcp_received_echo_reply __ARGS((fsm *, int, u_char *, int));
88 static void LcpSendEchoRequest __ARGS((fsm *));
89 static void LcpLinkFailure __ARGS((fsm *));
90
91 static fsm_callbacks lcp_callbacks = {  /* LCP callback routines */
92     lcp_resetci,                /* Reset our Configuration Information */
93     lcp_cilen,                  /* Length of our Configuration Information */
94     lcp_addci,                  /* Add our Configuration Information */
95     lcp_ackci,                  /* ACK our Configuration Information */
96     lcp_nakci,                  /* NAK our Configuration Information */
97     lcp_rejci,                  /* Reject our Configuration Information */
98     lcp_reqci,                  /* Request peer's Configuration Information */
99     lcp_up,                     /* Called when fsm reaches OPENED state */
100     lcp_down,                   /* Called when fsm leaves OPENED state */
101     lcp_starting,               /* Called when we want the lower layer up */
102     lcp_finished,               /* Called when we want the lower layer down */
103     NULL,                       /* Called when Protocol-Reject received */
104     NULL,                       /* Retransmission is necessary */
105     lcp_extcode,                /* Called to handle LCP-specific codes */
106     "LCP"                       /* String name of protocol */
107 };
108
109 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
110
111 /*
112  * Length of each type of configuration option (in octets)
113  */
114 #define CILEN_VOID      2
115 #define CILEN_SHORT     4       /* CILEN_VOID + sizeof(short) */
116 #define CILEN_CHAP      5       /* CILEN_VOID + sizeof(short) + 1 */
117 #define CILEN_LONG      6       /* CILEN_VOID + sizeof(long) */
118 #define CILEN_LQR       8       /* CILEN_VOID + sizeof(short) + sizeof(long) */
119
120 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
121                          (x) == CONFNAK ? "NAK" : "REJ")
122
123
124 /*
125  * lcp_init - Initialize LCP.
126  */
127 void
128 lcp_init(unit)
129     int unit;
130 {
131     fsm *f = &lcp_fsm[unit];
132     lcp_options *wo = &lcp_wantoptions[unit];
133     lcp_options *ao = &lcp_allowoptions[unit];
134
135     f->unit = unit;
136     f->protocol = LCP;
137     f->callbacks = &lcp_callbacks;
138
139     fsm_init(f);
140
141     wo->passive = 0;
142     wo->silent = 0;
143     wo->restart = 0;                    /* Set to 1 in kernels or multi-line
144                                            implementations */
145     wo->neg_mru = 1;
146     wo->mru = DEFMRU;
147     wo->neg_asyncmap = 0;
148     wo->asyncmap = 0;
149     wo->neg_chap = 0;                   /* Set to 1 on server */
150     wo->neg_upap = 0;                   /* Set to 1 on server */
151     wo->chap_mdtype = CHAP_DIGEST_MD5;
152     wo->neg_magicnumber = 1;
153     wo->neg_pcompression = 1;
154     wo->neg_accompression = 1;
155     wo->neg_lqr = 0;                    /* no LQR implementation yet */
156
157     ao->neg_mru = 1;
158     ao->mru = MAXMRU;
159     ao->neg_asyncmap = 1;
160     ao->asyncmap = 0;
161     ao->neg_chap = 1;
162     ao->chap_mdtype = CHAP_DIGEST_MD5;
163     ao->neg_upap = 1;
164     ao->neg_magicnumber = 1;
165     ao->neg_pcompression = 1;
166     ao->neg_accompression = 1;
167     ao->neg_lqr = 0;                    /* no LQR implementation yet */
168
169     memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
170     xmit_accm[unit][3] = 0x60000000;
171 }
172
173
174 /*
175  * lcp_open - LCP is allowed to come up.
176  */
177 void
178 lcp_open(unit)
179     int unit;
180 {
181     fsm *f = &lcp_fsm[unit];
182     lcp_options *wo = &lcp_wantoptions[unit];
183
184     f->flags = 0;
185     if (wo->passive)
186         f->flags |= OPT_PASSIVE;
187     if (wo->silent)
188         f->flags |= OPT_SILENT;
189     fsm_open(f);
190 }
191
192
193 /*
194  * lcp_close - Take LCP down.
195  */
196 void
197 lcp_close(unit)
198     int unit;
199 {
200     fsm *f = &lcp_fsm[unit];
201
202     if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
203         /*
204          * This action is not strictly according to the FSM in RFC1548,
205          * but it does mean that the program terminates if you do a
206          * lcp_close(0) in passive/silent mode when a connection hasn't
207          * been established.
208          */
209         f->state = CLOSED;
210         lcp_finished(f);
211
212     } else
213         fsm_close(&lcp_fsm[unit]);
214 }
215
216
217 /*
218  * lcp_lowerup - The lower layer is up.
219  */
220 void
221 lcp_lowerup(unit)
222     int unit;
223 {
224     sifdown(unit);
225     ppp_set_xaccm(unit, xmit_accm[unit]);
226     ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
227     ppp_recv_config(unit, MTU, 0x00000000, 0, 0);
228     peer_mru[unit] = MTU;
229     lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
230
231     fsm_lowerup(&lcp_fsm[unit]);
232 }
233
234
235 /*
236  * lcp_lowerdown - The lower layer is down.
237  */
238 void
239 lcp_lowerdown(unit)
240     int unit;
241 {
242     fsm_lowerdown(&lcp_fsm[unit]);
243 }
244
245
246 /*
247  * lcp_input - Input LCP packet.
248  */
249 void
250 lcp_input(unit, p, len)
251     int unit;
252     u_char *p;
253     int len;
254 {
255     fsm_input(&lcp_fsm[unit], p, len);
256 }
257
258
259 /*
260  * lcp_extcode - Handle a LCP-specific code.
261  */
262 static int
263 lcp_extcode(f, code, id, inp, len)
264     fsm *f;
265     int code, id;
266     u_char *inp;
267     int len;
268 {
269     u_char *magp;
270
271     switch( code ){
272     case PROTREJ:
273         lcp_rprotrej(f, inp, len);
274         break;
275     
276     case ECHOREQ:
277         if (f->state != OPENED)
278             break;
279         LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
280         magp = inp;
281         PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
282         if (len < CILEN_LONG)
283             len = CILEN_LONG;
284         fsm_sdata(f, ECHOREP, id, inp, len);
285         break;
286     
287     case ECHOREP:
288         lcp_received_echo_reply(f, id, inp, len);
289         break;
290
291     case DISCREQ:
292         break;
293
294     default:
295         return 0;
296     }
297     return 1;
298 }
299
300     
301 /*
302  * lcp_rprotrej - Receive an Protocol-Reject.
303  *
304  * Figure out which protocol is rejected and inform it.
305  */
306 static void
307 lcp_rprotrej(f, inp, len)
308     fsm *f;
309     u_char *inp;
310     int len;
311 {
312     u_short prot;
313
314     LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
315
316     if (len < sizeof (u_short)) {
317         LCPDEBUG((LOG_INFO,
318                   "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
319         return;
320     }
321
322     GETSHORT(prot, inp);
323
324     LCPDEBUG((LOG_INFO,
325               "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
326               prot));
327
328     /*
329      * Protocol-Reject packets received in any state other than the LCP
330      * OPENED state SHOULD be silently discarded.
331      */
332     if( f->state != OPENED ){
333         LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
334                   f->state));
335         return;
336     }
337
338     DEMUXPROTREJ(f->unit, prot);        /* Inform protocol */
339 }
340
341
342 /*
343  * lcp_protrej - A Protocol-Reject was received.
344  */
345 /*ARGSUSED*/
346 void
347 lcp_protrej(unit)
348     int unit;
349 {
350     /*
351      * Can't reject LCP!
352      */
353     LCPDEBUG((LOG_WARNING,
354               "lcp_protrej: Received Protocol-Reject for LCP!"));
355     fsm_protreject(&lcp_fsm[unit]);
356 }
357
358
359 /*
360  * lcp_sprotrej - Send a Protocol-Reject for some protocol.
361  */
362 void
363 lcp_sprotrej(unit, p, len)
364     int unit;
365     u_char *p;
366     int len;
367 {
368     /*
369      * Send back the protocol and the information field of the
370      * rejected packet.  We only get here if LCP is in the OPENED state.
371      */
372     p += 2;
373     len -= 2;
374
375     fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
376               p, len);
377 }
378
379
380 /*
381  * lcp_resetci - Reset our CI.
382  */
383 static void
384   lcp_resetci(f)
385 fsm *f;
386 {
387     lcp_wantoptions[f->unit].magicnumber = magic();
388     lcp_wantoptions[f->unit].numloops = 0;
389     lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
390     peer_mru[f->unit] = MTU;
391 }
392
393
394 /*
395  * lcp_cilen - Return length of our CI.
396  */
397 static int
398 lcp_cilen(f)
399     fsm *f;
400 {
401     lcp_options *go = &lcp_gotoptions[f->unit];
402
403 #define LENCIVOID(neg)  (neg ? CILEN_VOID : 0)
404 #define LENCICHAP(neg)  (neg ? CILEN_CHAP : 0)
405 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
406 #define LENCILONG(neg)  (neg ? CILEN_LONG : 0)
407 #define LENCILQR(neg)   (neg ? CILEN_LQR: 0)
408     /*
409      * NB: we only ask for one of CHAP and UPAP, even if we will
410      * accept either.
411      */
412     return (LENCISHORT(go->neg_mru) +
413             LENCILONG(go->neg_asyncmap) +
414             LENCICHAP(go->neg_chap) +
415             LENCISHORT(!go->neg_chap && go->neg_upap) +
416             LENCILQR(go->neg_lqr) +
417             LENCILONG(go->neg_magicnumber) +
418             LENCIVOID(go->neg_pcompression) +
419             LENCIVOID(go->neg_accompression));
420 }
421
422
423 /*
424  * lcp_addci - Add our desired CIs to a packet.
425  */
426 static void
427 lcp_addci(f, ucp, lenp)
428     fsm *f;
429     u_char *ucp;
430     int *lenp;
431 {
432     lcp_options *go = &lcp_gotoptions[f->unit];
433     u_char *start_ucp = ucp;
434
435 #define ADDCIVOID(opt, neg) \
436     if (neg) { \
437         PUTCHAR(opt, ucp); \
438         PUTCHAR(CILEN_VOID, ucp); \
439     }
440 #define ADDCISHORT(opt, neg, val) \
441     if (neg) { \
442         PUTCHAR(opt, ucp); \
443         PUTCHAR(CILEN_SHORT, ucp); \
444         PUTSHORT(val, ucp); \
445     }
446 #define ADDCICHAP(opt, neg, val, digest) \
447     if (neg) { \
448         PUTCHAR(opt, ucp); \
449         PUTCHAR(CILEN_CHAP, ucp); \
450         PUTSHORT(val, ucp); \
451         PUTCHAR(digest, ucp); \
452     }
453 #define ADDCILONG(opt, neg, val) \
454     if (neg) { \
455         PUTCHAR(opt, ucp); \
456         PUTCHAR(CILEN_LONG, ucp); \
457         PUTLONG(val, ucp); \
458     }
459 #define ADDCILQR(opt, neg, val) \
460     if (neg) { \
461         PUTCHAR(opt, ucp); \
462         PUTCHAR(CILEN_LQR, ucp); \
463         PUTSHORT(LQR, ucp); \
464         PUTLONG(val, ucp); \
465     }
466
467     ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
468     ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
469     ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
470     ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
471     ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
472     ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
473     ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
474     ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
475
476     if (ucp - start_ucp != *lenp) {
477         /* this should never happen, because peer_mtu should be 1500 */
478         syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
479     }
480 }
481
482
483 /*
484  * lcp_ackci - Ack our CIs.
485  * This should not modify any state if the Ack is bad.
486  *
487  * Returns:
488  *      0 - Ack was bad.
489  *      1 - Ack was good.
490  */
491 static int
492 lcp_ackci(f, p, len)
493     fsm *f;
494     u_char *p;
495     int len;
496 {
497     lcp_options *go = &lcp_gotoptions[f->unit];
498     u_char cilen, citype, cichar;
499     u_short cishort;
500     u_long cilong;
501
502     /*
503      * CIs must be in exactly the same order that we sent.
504      * Check packet length and CI length at each step.
505      * If we find any deviations, then this packet is bad.
506      */
507 #define ACKCIVOID(opt, neg) \
508     if (neg) { \
509         if ((len -= CILEN_VOID) < 0) \
510             goto bad; \
511         GETCHAR(citype, p); \
512         GETCHAR(cilen, p); \
513         if (cilen != CILEN_VOID || \
514             citype != opt) \
515             goto bad; \
516     }
517 #define ACKCISHORT(opt, neg, val) \
518     if (neg) { \
519         if ((len -= CILEN_SHORT) < 0) \
520             goto bad; \
521         GETCHAR(citype, p); \
522         GETCHAR(cilen, p); \
523         if (cilen != CILEN_SHORT || \
524             citype != opt) \
525             goto bad; \
526         GETSHORT(cishort, p); \
527         if (cishort != val) \
528             goto bad; \
529     }
530 #define ACKCICHAP(opt, neg, val, digest) \
531     if (neg) { \
532         if ((len -= CILEN_CHAP) < 0) \
533             goto bad; \
534         GETCHAR(citype, p); \
535         GETCHAR(cilen, p); \
536         if (cilen != CILEN_CHAP || \
537             citype != opt) \
538             goto bad; \
539         GETSHORT(cishort, p); \
540         if (cishort != val) \
541             goto bad; \
542         GETCHAR(cichar, p); \
543         if (cichar != digest) \
544           goto bad; \
545     }
546 #define ACKCILONG(opt, neg, val) \
547     if (neg) { \
548         if ((len -= CILEN_LONG) < 0) \
549             goto bad; \
550         GETCHAR(citype, p); \
551         GETCHAR(cilen, p); \
552         if (cilen != CILEN_LONG || \
553             citype != opt) \
554             goto bad; \
555         GETLONG(cilong, p); \
556         if (cilong != val) \
557             goto bad; \
558     }
559 #define ACKCILQR(opt, neg, val) \
560     if (neg) { \
561         if ((len -= CILEN_LQR) < 0) \
562             goto bad; \
563         GETCHAR(citype, p); \
564         GETCHAR(cilen, p); \
565         if (cilen != CILEN_LQR || \
566             citype != opt) \
567             goto bad; \
568         GETSHORT(cishort, p); \
569         if (cishort != LQR) \
570             goto bad; \
571         GETLONG(cilong, p); \
572         if (cilong != val) \
573           goto bad; \
574     }
575
576     ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
577     ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
578     ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
579     ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
580     ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
581     ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
582     ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
583     ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
584
585     /*
586      * If there are any remaining CIs, then this packet is bad.
587      */
588     if (len != 0)
589         goto bad;
590     return (1);
591 bad:
592     LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
593     return (0);
594 }
595
596
597 /*
598  * lcp_nakci - Peer has sent a NAK for some of our CIs.
599  * This should not modify any state if the Nak is bad
600  * or if LCP is in the OPENED state.
601  *
602  * Returns:
603  *      0 - Nak was bad.
604  *      1 - Nak was good.
605  */
606 static int
607 lcp_nakci(f, p, len)
608     fsm *f;
609     u_char *p;
610     int len;
611 {
612     lcp_options *go = &lcp_gotoptions[f->unit];
613     lcp_options *wo = &lcp_wantoptions[f->unit];
614     u_char cilen, citype, cichar, *next;
615     u_short cishort;
616     u_long cilong;
617     lcp_options no;             /* options we've seen Naks for */
618     lcp_options try;            /* options to request next time */
619     int looped_back = 0;
620
621     BZERO(&no, sizeof(no));
622     try = *go;
623
624     /*
625      * Any Nak'd CIs must be in exactly the same order that we sent.
626      * Check packet length and CI length at each step.
627      * If we find any deviations, then this packet is bad.
628      */
629 #define NAKCIVOID(opt, neg, code) \
630     if (go->neg && \
631         len >= CILEN_VOID && \
632         p[1] == CILEN_VOID && \
633         p[0] == opt) { \
634         len -= CILEN_VOID; \
635         INCPTR(CILEN_VOID, p); \
636         no.neg = 1; \
637         code \
638     }
639 #define NAKCICHAP(opt, neg, code) \
640     if (go->neg && \
641         len >= CILEN_CHAP && \
642         p[1] == CILEN_CHAP && \
643         p[0] == opt) { \
644         len -= CILEN_CHAP; \
645         INCPTR(2, p); \
646         GETSHORT(cishort, p); \
647         GETCHAR(cichar, p); \
648         no.neg = 1; \
649         code \
650     }
651 #define NAKCISHORT(opt, neg, code) \
652     if (go->neg && \
653         len >= CILEN_SHORT && \
654         p[1] == CILEN_SHORT && \
655         p[0] == opt) { \
656         len -= CILEN_SHORT; \
657         INCPTR(2, p); \
658         GETSHORT(cishort, p); \
659         no.neg = 1; \
660         code \
661     }
662 #define NAKCILONG(opt, neg, code) \
663     if (go->neg && \
664         len >= CILEN_LONG && \
665         p[1] == CILEN_LONG && \
666         p[0] == opt) { \
667         len -= CILEN_LONG; \
668         INCPTR(2, p); \
669         GETLONG(cilong, p); \
670         no.neg = 1; \
671         code \
672     }
673 #define NAKCILQR(opt, neg, code) \
674     if (go->neg && \
675         len >= CILEN_LQR && \
676         p[1] == CILEN_LQR && \
677         p[0] == opt) { \
678         len -= CILEN_LQR; \
679         INCPTR(2, p); \
680         GETSHORT(cishort, p); \
681         GETLONG(cilong, p); \
682         no.neg = 1; \
683         code \
684     }
685
686     /*
687      * We don't care if they want to send us smaller packets than
688      * we want.  Therefore, accept any MRU less than what we asked for,
689      * but then ignore the new value when setting the MRU in the kernel.
690      * If they send us a bigger MRU than what we asked, accept it, up to
691      * the limit of the default MRU we'd get if we didn't negotiate.
692      */
693     NAKCISHORT(CI_MRU, neg_mru,
694                if (cishort <= wo->mru || cishort < DEFMRU)
695                    try.mru = cishort;
696                );
697     /*
698      * Add any characters they want to our (receive-side) asyncmap.
699      */
700     NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
701               try.asyncmap = go->asyncmap | cilong;
702               );
703     /*
704      * If they can't cope with our CHAP hash algorithm, we'll have
705      * to stop asking for CHAP.  We haven't got any other algorithm.
706      */
707     NAKCICHAP(CI_AUTHTYPE, neg_chap,
708               try.neg_chap = 0;
709               );
710     /*
711      * Peer shouldn't send Nak for UPAP, protocol compression or
712      * address/control compression requests; they should send
713      * a Reject instead.  If they send a Nak, treat it as a Reject.
714      */
715     if (!go->neg_chap ){
716         NAKCISHORT(CI_AUTHTYPE, neg_upap,
717                    try.neg_upap = 0;
718                    );
719     }
720     /*
721      * If they can't cope with our link quality protocol, we'll have
722      * to stop asking for LQR.  We haven't got any other protocol.
723      * If they Nak the reporting period, take their value XXX ?
724      */
725     NAKCILQR(CI_QUALITY, neg_lqr,
726              if (cishort != LQR)
727                  try.neg_lqr = 0;
728              else
729                  try.lqr_period = cilong;
730              );
731     /*
732      * Check for a looped-back line.
733      */
734     NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
735               try.magicnumber = magic();
736               ++try.numloops;
737               looped_back = 1;
738               );
739
740     NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
741               try.neg_pcompression = 0;
742               );
743     NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
744               try.neg_accompression = 0;
745               );
746
747     /*
748      * There may be remaining CIs, if the peer is requesting negotiation
749      * on an option that we didn't include in our request packet.
750      * If we see an option that we requested, or one we've already seen
751      * in this packet, then this packet is bad.
752      * If we wanted to respond by starting to negotiate on the requested
753      * option(s), we could, but we don't, because except for the
754      * authentication type and quality protocol, if we are not negotiating
755      * an option, it is because we were told not to.
756      * For the authentication type, the Nak from the peer means
757      * `let me authenticate myself with you' which is a bit pointless.
758      * For the quality protocol, the Nak means `ask me to send you quality
759      * reports', but if we didn't ask for them, we don't want them.
760      */
761     while (len > CILEN_VOID) {
762         GETCHAR(citype, p);
763         GETCHAR(cilen, p);
764         if( (len -= cilen) < 0 )
765             goto bad;
766         next = p + cilen - 2;
767
768         switch (citype) {
769         case CI_MRU:
770             if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
771                 goto bad;
772             break;
773         case CI_ASYNCMAP:
774             if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
775                 goto bad;
776             break;
777         case CI_AUTHTYPE:
778             if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
779                 goto bad;
780             break;
781         case CI_MAGICNUMBER:
782             if (go->neg_magicnumber || no.neg_magicnumber ||
783                 cilen != CILEN_LONG)
784                 goto bad;
785             break;
786         case CI_PCOMPRESSION:
787             if (go->neg_pcompression || no.neg_pcompression
788                 || cilen != CILEN_VOID)
789                 goto bad;
790             break;
791         case CI_ACCOMPRESSION:
792             if (go->neg_accompression || no.neg_accompression
793                 || cilen != CILEN_VOID)
794                 goto bad;
795             break;
796         case CI_QUALITY:
797             if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
798                 goto bad;
799             break;
800         default:
801             goto bad;
802         }
803         p = next;
804     }
805
806     /* If there is still anything left, this packet is bad. */
807     if (len != 0)
808         goto bad;
809
810     /*
811      * OK, the Nak is good.  Now we can update state.
812      */
813     if (f->state != OPENED) {
814         *go = try;
815         if (looped_back && try.numloops % lcp_warnloops == 0)
816             LCPDEBUG((LOG_INFO, "The line appears to be looped back."));
817     }
818
819     return 1;
820
821 bad:
822     LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
823     return 0;
824 }
825
826
827 /*
828  * lcp_rejci - Peer has Rejected some of our CIs.
829  * This should not modify any state if the Reject is bad
830  * or if LCP is in the OPENED state.
831  *
832  * Returns:
833  *      0 - Reject was bad.
834  *      1 - Reject was good.
835  */
836 static int
837 lcp_rejci(f, p, len)
838     fsm *f;
839     u_char *p;
840     int len;
841 {
842     lcp_options *go = &lcp_gotoptions[f->unit];
843     u_char cichar;
844     u_short cishort;
845     u_long cilong;
846     u_char *start = p;
847     int plen = len;
848     lcp_options try;            /* options to request next time */
849
850     try = *go;
851
852     /*
853      * Any Rejected CIs must be in exactly the same order that we sent.
854      * Check packet length and CI length at each step.
855      * If we find any deviations, then this packet is bad.
856      */
857 #define REJCIVOID(opt, neg) \
858     if (go->neg && \
859         len >= CILEN_VOID && \
860         p[1] == CILEN_VOID && \
861         p[0] == opt) { \
862         len -= CILEN_VOID; \
863         INCPTR(CILEN_VOID, p); \
864         try.neg = 0; \
865         LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
866     }
867 #define REJCISHORT(opt, neg, val) \
868     if (go->neg && \
869         len >= CILEN_SHORT && \
870         p[1] == CILEN_SHORT && \
871         p[0] == opt) { \
872         len -= CILEN_SHORT; \
873         INCPTR(2, p); \
874         GETSHORT(cishort, p); \
875         /* Check rejected value. */ \
876         if (cishort != val) \
877             goto bad; \
878         try.neg = 0; \
879         LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
880     }
881 #define REJCICHAP(opt, neg, val, digest) \
882     if (go->neg && \
883         len >= CILEN_CHAP && \
884         p[1] == CILEN_CHAP && \
885         p[0] == opt) { \
886         len -= CILEN_CHAP; \
887         INCPTR(2, p); \
888         GETSHORT(cishort, p); \
889         GETCHAR(cichar, p); \
890         /* Check rejected value. */ \
891         if (cishort != val || cichar != digest) \
892             goto bad; \
893         try.neg = 0; \
894         LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
895     }
896 #define REJCILONG(opt, neg, val) \
897     if (go->neg && \
898         len >= CILEN_LONG && \
899         p[1] == CILEN_LONG && \
900         p[0] == opt) { \
901         len -= CILEN_LONG; \
902         INCPTR(2, p); \
903         GETLONG(cilong, p); \
904         /* Check rejected value. */ \
905         if (cilong != val) \
906             goto bad; \
907         try.neg = 0; \
908         LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
909     }
910 #define REJCILQR(opt, neg, val) \
911     if (go->neg && \
912         len >= CILEN_LQR && \
913         p[1] == CILEN_LQR && \
914         p[0] == opt) { \
915         len -= CILEN_LQR; \
916         INCPTR(2, p); \
917         GETSHORT(cishort, p); \
918         GETLONG(cilong, p); \
919         /* Check rejected value. */ \
920         if (cishort != LQR || cilong != val) \
921             goto bad; \
922         try.neg = 0; \
923         LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
924     }
925
926     REJCISHORT(CI_MRU, neg_mru, go->mru);
927     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
928     REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
929     if (!go->neg_chap) {
930         REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
931     }
932     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
933     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
934     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
935     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
936
937     /*
938      * If there are any remaining CIs, then this packet is bad.
939      */
940     if (len != 0)
941         goto bad;
942     /*
943      * Now we can update state.
944      */
945     if (f->state != OPENED)
946         *go = try;
947     return 1;
948
949 bad:
950     LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
951     LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
952               plen, len, p - start));
953     return 0;
954 }
955
956
957 /*
958  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
959  *
960  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
961  * appropriately.  If reject_if_disagree is non-zero, doesn't return
962  * CONFNAK; returns CONFREJ if it can't return CONFACK.
963  */
964 static int
965 lcp_reqci(f, inp, lenp, reject_if_disagree)
966     fsm *f;
967     u_char *inp;                /* Requested CIs */
968     int *lenp;                  /* Length of requested CIs */
969     int reject_if_disagree;
970 {
971     lcp_options *go = &lcp_gotoptions[f->unit];
972     lcp_options *ho = &lcp_hisoptions[f->unit];
973     lcp_options *ao = &lcp_allowoptions[f->unit];
974     u_char *cip, *next;         /* Pointer to current and next CIs */
975     u_char cilen, citype, cichar;/* Parsed len, type, char value */
976     u_short cishort;            /* Parsed short value */
977     u_long cilong;              /* Parse long value */
978     int rc = CONFACK;           /* Final packet return code */
979     int orc;                    /* Individual option return code */
980     u_char *p;                  /* Pointer to next char to parse */
981     u_char *ucp = inp;          /* Pointer to current output char */
982     int l = *lenp;              /* Length left */
983
984     /*
985      * Reset all his options.
986      */
987     BZERO(ho, sizeof(*ho));
988
989     /*
990      * Process all his options.
991      */
992     next = inp;
993     while (l) {
994         orc = CONFACK;                  /* Assume success */
995         cip = p = next;                 /* Remember begining of CI */
996         if (l < 2 ||                    /* Not enough data for CI header or */
997             p[1] < 2 ||                 /*  CI length too small or */
998             p[1] > l) {                 /*  CI length too big? */
999             LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1000             orc = CONFREJ;              /* Reject bad CI */
1001             cilen = l;                  /* Reject till end of packet */
1002             l = 0;                      /* Don't loop again */
1003             goto endswitch;
1004         }
1005         GETCHAR(citype, p);             /* Parse CI type */
1006         GETCHAR(cilen, p);              /* Parse CI length */
1007         l -= cilen;                     /* Adjust remaining length */
1008         next += cilen;                  /* Step to next CI */
1009
1010         switch (citype) {               /* Check CI type */
1011         case CI_MRU:
1012             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1013             if (!ao->neg_mru ||         /* Allow option? */
1014                 cilen != CILEN_SHORT) { /* Check CI length */
1015                 orc = CONFREJ;          /* Reject CI */
1016                 break;
1017             }
1018             GETSHORT(cishort, p);       /* Parse MRU */
1019             LCPDEBUG((LOG_INFO, "(%d)", cishort));
1020
1021             /*
1022              * He must be able to receive at least our minimum.
1023              * No need to check a maximum.  If he sends a large number,
1024              * we'll just ignore it.
1025              */
1026             if (cishort < MINMRU) {
1027                 orc = CONFNAK;          /* Nak CI */
1028                 if( !reject_if_disagree ){
1029                     DECPTR(sizeof (short), p);  /* Backup */
1030                     PUTSHORT(MINMRU, p);        /* Give him a hint */
1031                 }
1032                 break;
1033             }
1034             ho->neg_mru = 1;            /* Remember he sent MRU */
1035             ho->mru = cishort;          /* And remember value */
1036             break;
1037
1038         case CI_ASYNCMAP:
1039             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1040             if (!ao->neg_asyncmap ||
1041                 cilen != CILEN_LONG) {
1042                 orc = CONFREJ;
1043                 break;
1044             }
1045             GETLONG(cilong, p);
1046             LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1047
1048             /*
1049              * Asyncmap must have set at least the bits
1050              * which are set in lcp_allowoptions[unit].asyncmap.
1051              */
1052             if ((ao->asyncmap & ~cilong) != 0) {
1053                 orc = CONFNAK;
1054                 if( !reject_if_disagree ){
1055                     DECPTR(sizeof (long), p);
1056                     PUTLONG(ao->asyncmap | cilong, p);
1057                 }
1058                 break;
1059             }
1060             ho->neg_asyncmap = 1;
1061             ho->asyncmap = cilong;
1062             break;
1063
1064         case CI_AUTHTYPE:
1065             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1066             if (cilen < CILEN_SHORT ||
1067                 !(ao->neg_upap || ao->neg_chap)) {
1068                 orc = CONFREJ;
1069                 break;
1070             }
1071             GETSHORT(cishort, p);
1072             LCPDEBUG((LOG_INFO, "(%x)", cishort));
1073
1074             /*
1075              * Authtype must be UPAP or CHAP.
1076              *
1077              * Note: if both ao->neg_upap and ao->neg_chap are set,
1078              * and the peer sends a Configure-Request with two
1079              * authenticate-protocol requests, one for CHAP and one
1080              * for UPAP, then we will reject the second request.
1081              * Whether we end up doing CHAP or UPAP depends then on
1082              * the ordering of the CIs in the peer's Configure-Request.
1083              */
1084
1085             if (cishort == UPAP) {
1086                 if (!ao->neg_upap ||    /* we don't want to do PAP */
1087                     ho->neg_chap ||     /* or we've already accepted CHAP */
1088                     cilen != CILEN_SHORT) {
1089                     LCPDEBUG((LOG_WARNING,
1090                               "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1091                     orc = CONFREJ;
1092                     break;
1093                 }
1094                 ho->neg_upap = 1;
1095                 break;
1096             }
1097             if (cishort == CHAP) {
1098                 if (!ao->neg_chap ||    /* we don't want to do CHAP */
1099                     ho->neg_upap ||     /* or we've already accepted UPAP */
1100                     cilen != CILEN_CHAP) {
1101                     LCPDEBUG((LOG_INFO,
1102                               "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1103                     orc = CONFREJ;
1104                     break;
1105                 }
1106                 GETCHAR(cichar, p);     /* get digest type*/
1107                 if (cichar != ao->chap_mdtype) {
1108                     orc = CONFNAK;
1109                     if( !reject_if_disagree ){
1110                         DECPTR(sizeof (u_char), p);
1111                         PUTCHAR(ao->chap_mdtype, p);
1112                     }
1113                     break;
1114                 }
1115                 ho->chap_mdtype = cichar; /* save md type */
1116                 ho->neg_chap = 1;
1117                 break;
1118             }
1119
1120             /*
1121              * We don't recognize the protocol they're asking for.
1122              * Reject it.
1123              */
1124             orc = CONFREJ;
1125             break;
1126
1127         case CI_QUALITY:
1128             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1129             if (!ao->neg_lqr ||
1130                 cilen != CILEN_LQR) {
1131                 orc = CONFREJ;
1132                 break;
1133             }
1134
1135             GETSHORT(cishort, p);
1136             GETLONG(cilong, p);
1137             LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1138             if (cishort != LQR) {
1139                 orc = CONFREJ;
1140                 break;
1141             }
1142
1143             /*
1144              * Check the reporting period.
1145              * XXX When should we Nak this, and what with?
1146              */
1147             break;
1148
1149         case CI_MAGICNUMBER:
1150             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1151             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1152                 cilen != CILEN_LONG) {
1153                 orc = CONFREJ;
1154                 break;
1155             }
1156             GETLONG(cilong, p);
1157             LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1158
1159             /*
1160              * He must have a different magic number.
1161              */
1162             if (go->neg_magicnumber &&
1163                 cilong == go->magicnumber) {
1164                 orc = CONFNAK;
1165                 DECPTR(sizeof (long), p);
1166                 cilong = magic();       /* Don't put magic() inside macro! */
1167                 PUTLONG(cilong, p);
1168                 break;
1169             }
1170             ho->neg_magicnumber = 1;
1171             ho->magicnumber = cilong;
1172             break;
1173
1174
1175         case CI_PCOMPRESSION:
1176             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1177             if (!ao->neg_pcompression ||
1178                 cilen != CILEN_VOID) {
1179                 orc = CONFREJ;
1180                 break;
1181             }
1182             ho->neg_pcompression = 1;
1183             break;
1184
1185         case CI_ACCOMPRESSION:
1186             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1187             if (!ao->neg_accompression ||
1188                 cilen != CILEN_VOID) {
1189                 orc = CONFREJ;
1190                 break;
1191             }
1192             ho->neg_accompression = 1;
1193             break;
1194
1195         default:
1196             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1197                       citype));
1198             orc = CONFREJ;
1199             break;
1200         }
1201
1202 endswitch:
1203         LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1204         if (orc == CONFACK &&           /* Good CI */
1205             rc != CONFACK)              /*  but prior CI wasnt? */
1206             continue;                   /* Don't send this one */
1207
1208         if (orc == CONFNAK) {           /* Nak this CI? */
1209             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1210                 orc = CONFREJ;          /* Get tough if so */
1211             else {
1212                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1213                     continue;           /* Don't send this one */
1214                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1215                     rc = CONFNAK;       /* Not anymore... */
1216                     ucp = inp;          /* Backup */
1217                 }
1218             }
1219         }
1220         if (orc == CONFREJ &&           /* Reject this CI */
1221             rc != CONFREJ) {            /*  but no prior ones? */
1222             rc = CONFREJ;
1223             ucp = inp;                  /* Backup */
1224         }
1225         if (ucp != cip)                 /* Need to move CI? */
1226             BCOPY(cip, ucp, cilen);     /* Move it */
1227         INCPTR(cilen, ucp);             /* Update output pointer */
1228     }
1229
1230     /*
1231      * If we wanted to send additional NAKs (for unsent CIs), the
1232      * code would go here.  This must be done with care since it might
1233      * require a longer packet than we received.  At present there
1234      * are no cases where we want to ask the peer to negotiate an option.
1235      */
1236
1237     *lenp = ucp - inp;                  /* Compute output length */
1238     LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1239     return (rc);                        /* Return final code */
1240 }
1241
1242
1243 /*
1244  * lcp_up - LCP has come UP.
1245  *
1246  * Start UPAP, IPCP, etc.
1247  */
1248 static void
1249 lcp_up(f)
1250     fsm *f;
1251 {
1252     lcp_options *wo = &lcp_wantoptions[f->unit];
1253     lcp_options *ho = &lcp_hisoptions[f->unit];
1254     lcp_options *go = &lcp_gotoptions[f->unit];
1255     lcp_options *ao = &lcp_allowoptions[f->unit];
1256
1257     if (!go->neg_magicnumber)
1258         go->magicnumber = 0;
1259     if (!ho->neg_magicnumber)
1260         ho->magicnumber = 0;
1261
1262     /*
1263      * Set our MTU to the smaller of the MTU we wanted and
1264      * the MRU our peer wanted.  If we negotiated an MRU,
1265      * set our MRU to the larger of value we wanted and
1266      * the value we got in the negotiation.
1267      */
1268     ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1269                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1270                     ho->neg_pcompression, ho->neg_accompression);
1271     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1272                     (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1273                     go->neg_pcompression, go->neg_accompression);
1274
1275     if (ho->neg_mru)
1276         peer_mru[f->unit] = ho->mru;
1277
1278     ChapLowerUp(f->unit);       /* Enable CHAP */
1279     upap_lowerup(f->unit);      /* Enable UPAP */
1280     ipcp_lowerup(f->unit);      /* Enable IPCP */
1281     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
1282
1283     link_established(f->unit);
1284 }
1285
1286
1287 /*
1288  * lcp_down - LCP has gone DOWN.
1289  *
1290  * Alert other protocols.
1291  */
1292 static void
1293 lcp_down(f)
1294     fsm *f;
1295 {
1296     lcp_echo_lowerdown(f->unit);
1297     ipcp_lowerdown(f->unit);
1298     ChapLowerDown(f->unit);
1299     upap_lowerdown(f->unit);
1300
1301     sifdown(f->unit);
1302     ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1303     ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1304     peer_mru[f->unit] = MTU;
1305
1306     link_down(f->unit);
1307 }
1308
1309
1310 /*
1311  * lcp_starting - LCP needs the lower layer up.
1312  */
1313 static void
1314 lcp_starting(f)
1315     fsm *f;
1316 {
1317     link_required(f->unit);
1318 }
1319
1320
1321 /*
1322  * lcp_finished - LCP has finished with the lower layer.
1323  */
1324 static void
1325 lcp_finished(f)
1326     fsm *f;
1327 {
1328     link_terminated(f->unit);
1329 }
1330
1331
1332 /*
1333  * lcp_printpkt - print the contents of an LCP packet.
1334  */
1335 char *lcp_codenames[] = {
1336     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1337     "TermReq", "TermAck", "CodeRej", "ProtRej",
1338     "EchoReq", "EchoRep", "DiscReq"
1339 };
1340
1341 int
1342 lcp_printpkt(p, plen, printer, arg)
1343     u_char *p;
1344     int plen;
1345     void (*printer) __ARGS((void *, char *, ...));
1346     void *arg;
1347 {
1348     int code, id, len, olen;
1349     u_char *pstart, *optend;
1350     u_short cishort;
1351     u_long cilong;
1352
1353     if (plen < HEADERLEN)
1354         return 0;
1355     pstart = p;
1356     GETCHAR(code, p);
1357     GETCHAR(id, p);
1358     GETSHORT(len, p);
1359     if (len < HEADERLEN || len > plen)
1360         return 0;
1361
1362     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1363         printer(arg, " %s", lcp_codenames[code-1]);
1364     else
1365         printer(arg, " code=0x%x", code);
1366     printer(arg, " id=0x%x", id);
1367     len -= HEADERLEN;
1368     switch (code) {
1369     case CONFREQ:
1370     case CONFACK:
1371     case CONFNAK:
1372     case CONFREJ:
1373         /* print option list */
1374         while (len >= 2) {
1375             GETCHAR(code, p);
1376             GETCHAR(olen, p);
1377             p -= 2;
1378             if (olen < 2 || olen > len) {
1379                 break;
1380             }
1381             printer(arg, " <");
1382             len -= olen;
1383             optend = p + olen;
1384             switch (code) {
1385             case CI_MRU:
1386                 if (olen == CILEN_SHORT) {
1387                     p += 2;
1388                     GETSHORT(cishort, p);
1389                     printer(arg, "mru %d", cishort);
1390                 }
1391                 break;
1392             case CI_ASYNCMAP:
1393                 if (olen == CILEN_LONG) {
1394                     p += 2;
1395                     GETLONG(cilong, p);
1396                     printer(arg, "asyncmap 0x%x", cilong);
1397                 }
1398                 break;
1399             case CI_AUTHTYPE:
1400                 if (olen >= CILEN_SHORT) {
1401                     p += 2;
1402                     printer(arg, "auth ");
1403                     GETSHORT(cishort, p);
1404                     switch (cishort) {
1405                     case UPAP:
1406                         printer(arg, "upap");
1407                         break;
1408                     case CHAP:
1409                         printer(arg, "chap");
1410                         break;
1411                     default:
1412                         printer(arg, "0x%x", cishort);
1413                     }
1414                 }
1415                 break;
1416             case CI_QUALITY:
1417                 if (olen >= CILEN_SHORT) {
1418                     p += 2;
1419                     printer(arg, "quality ");
1420                     GETSHORT(cishort, p);
1421                     switch (cishort) {
1422                     case LQR:
1423                         printer(arg, "lqr");
1424                         break;
1425                     default:
1426                         printer(arg, "0x%x", cishort);
1427                     }
1428                 }
1429                 break;
1430             case CI_MAGICNUMBER:
1431                 if (olen == CILEN_LONG) {
1432                     p += 2;
1433                     GETLONG(cilong, p);
1434                     printer(arg, "magic 0x%x", cilong);
1435                 }
1436                 break;
1437             case CI_PCOMPRESSION:
1438                 if (olen == CILEN_VOID) {
1439                     p += 2;
1440                     printer(arg, "pcomp");
1441                 }
1442                 break;
1443             case CI_ACCOMPRESSION:
1444                 if (olen == CILEN_VOID) {
1445                     p += 2;
1446                     printer(arg, "accomp");
1447                 }
1448                 break;
1449             }
1450             while (p < optend) {
1451                 GETCHAR(code, p);
1452                 printer(arg, " %.2x", code);
1453             }
1454             printer(arg, ">");
1455         }
1456         break;
1457     }
1458
1459     /* print the rest of the bytes in the packet */
1460     for (; len > 0; --len) {
1461         GETCHAR(code, p);
1462         printer(arg, " %.2x", code);
1463     }
1464
1465     return p - pstart;
1466 }
1467
1468 /*
1469  * Time to shut down the link because there is nothing out there.
1470  */
1471
1472 static
1473 void LcpLinkFailure (f)
1474     fsm *f;
1475 {
1476     if (f->state == OPENED) {
1477         syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1478         lcp_lowerdown(f->unit);         /* Reset connection */
1479     }
1480 }
1481
1482 /*
1483  * Timer expired for the LCP echo requests from this process.
1484  */
1485
1486 static void
1487 LcpEchoCheck (f)
1488     fsm *f;
1489 {
1490     u_long             delta;
1491 #ifdef __linux__
1492     struct ppp_ddinfo  ddinfo;
1493     u_long             latest;
1494 /*
1495  * Read the time since the last packet was received.
1496  */
1497     if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1498         syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1499         die (1);
1500     }
1501 /*
1502  * Choose the most recient frame received. It may be an IP or NON-IP frame.
1503  */
1504     latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1505                                                       : ddinfo.ip_rjiffies;
1506 /*
1507  * Compute the time since the last packet was received. If the timer
1508  *  has expired then send the echo request and reset the timer to maximum.
1509  */
1510     delta = (lcp_echo_interval * HZ) - latest;
1511     if (delta < HZ || latest < 0L) {
1512         LcpSendEchoRequest (f);
1513         delta = lcp_echo_interval * HZ;
1514     }
1515     delta /= HZ;
1516
1517 #else /* Other implementations do not have ability to find delta */
1518     LcpSendEchoRequest (f);
1519     delta = lcp_echo_interval;
1520 #endif
1521
1522 /*
1523  * Start the timer for the next interval.
1524  */
1525     assert (lcp_echo_timer_running==0);
1526     TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1527     lcp_echo_timer_running = 1;
1528 }
1529
1530 /*
1531  * LcpEchoTimeout - Timer expired on the LCP echo
1532  */
1533
1534 static void
1535 LcpEchoTimeout (arg)
1536     caddr_t arg;
1537 {
1538     if (lcp_echo_timer_running != 0) {
1539         lcp_echo_timer_running = 0;
1540         LcpEchoCheck ((fsm *) arg);
1541     }
1542 }
1543
1544 /*
1545  * LcpEchoReply - LCP has received a reply to the echo
1546  */
1547
1548 static void
1549 lcp_received_echo_reply (f, id, inp, len)
1550     fsm *f;
1551     int id; u_char *inp; int len;
1552 {
1553     u_long magic;
1554
1555     /* Check the magic number - don't count replies from ourselves. */
1556     if (len < CILEN_LONG)
1557         return;
1558     GETLONG(magic, inp);
1559     if (lcp_gotoptions[f->unit].neg_magicnumber
1560         && magic == lcp_gotoptions[f->unit].magicnumber)
1561         return;
1562
1563     /* Reset the number of outstanding echo frames */
1564     lcp_echos_pending = 0;
1565 }
1566
1567 /*
1568  * LcpSendEchoRequest - Send an echo request frame to the peer
1569  */
1570
1571 static void
1572 LcpSendEchoRequest (f)
1573     fsm *f;
1574 {
1575     u_long lcp_magic;
1576     u_char pkt[4], *pktp;
1577
1578 /*
1579  * Detect the failure of the peer at this point.
1580  */
1581     if (lcp_echo_fails != 0) {
1582         if (lcp_echos_pending++ >= lcp_echo_fails) {
1583             LcpLinkFailure(f);
1584             lcp_echos_pending = 0;
1585         }
1586     }
1587 /*
1588  * Make and send the echo request frame.
1589  */
1590     if (f->state == OPENED) {
1591         lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1592                     ? lcp_gotoptions[f->unit].magicnumber
1593                     : 0L;
1594         pktp = pkt;
1595         PUTLONG(lcp_magic, pktp);
1596       
1597         fsm_sdata(f, ECHOREQ,
1598                   lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1599     }
1600 }
1601
1602 /*
1603  * lcp_echo_lowerup - Start the timer for the LCP frame
1604  */
1605
1606 static void
1607 lcp_echo_lowerup (unit)
1608     int unit;
1609 {
1610     fsm *f = &lcp_fsm[unit];
1611
1612     /* Clear the parameters for generating echo frames */
1613     lcp_echos_pending      = 0;
1614     lcp_echo_number        = 0;
1615     lcp_echo_timer_running = 0;
1616   
1617     /* If a timeout interval is specified then start the timer */
1618     if (lcp_echo_interval != 0)
1619         LcpEchoCheck (f);
1620 }
1621
1622 /*
1623  * lcp_echo_lowerdown - Stop the timer for the LCP frame
1624  */
1625
1626 static void
1627 lcp_echo_lowerdown (unit)
1628     int unit;
1629 {
1630     fsm *f = &lcp_fsm[unit];
1631
1632     if (lcp_echo_timer_running != 0) {
1633         UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1634         lcp_echo_timer_running = 0;
1635     }
1636 }