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