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