]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
changes to linux interface for new kernel driver,
[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 #define RCSID   "$Id: lcp.c,v 1.48 2000/03/27 06:02:59 paulus Exp $";
21
22 /*
23  * TODO:
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "pppd.h"
31 #include "fsm.h"
32 #include "lcp.h"
33 #include "chap.h"
34 #include "magic.h"
35
36 static const char rcsid[] = RCSID;
37
38 /*
39  * LCP-related command-line options.
40  */
41 int     lcp_echo_interval = 0;  /* Interval between LCP echo-requests */
42 int     lcp_echo_fails = 0;     /* Tolerance to unanswered echo-requests */
43 bool    lax_recv = 0;           /* accept control chars in asyncmap */
44
45 static int setescape __P((char **));
46
47 static option_t lcp_option_list[] = {
48     /* LCP options */
49     { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
50       "Disable address/control compression",
51       OPT_A2COPY, &lcp_allowoptions[0].neg_accompression },
52     { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
53       "Disable address/control compression",
54       OPT_A2COPY, &lcp_allowoptions[0].neg_accompression },
55     { "default-asyncmap", o_bool, &lcp_wantoptions[0].neg_asyncmap,
56       "Disable asyncmap negotiation",
57       OPT_A2COPY, &lcp_allowoptions[0].neg_asyncmap },
58     { "-am", o_bool, &lcp_wantoptions[0].neg_asyncmap,
59       "Disable asyncmap negotiation",
60       OPT_A2COPY, &lcp_allowoptions[0].neg_asyncmap },
61     { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
62       "Set asyncmap (for received packets)",
63       OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
64     { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
65       "Set asyncmap (for received packets)",
66       OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
67     { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
68       "Disable magic number negotiation (looped-back line detection)",
69       OPT_A2COPY, &lcp_allowoptions[0].neg_magicnumber },
70     { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
71       "Disable magic number negotiation (looped-back line detection)",
72       OPT_A2COPY, &lcp_allowoptions[0].neg_magicnumber },
73     { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
74       "Disable MRU negotiation (use default 1500)",
75       OPT_A2COPY, &lcp_allowoptions[0].neg_mru },
76     { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
77       "Disable MRU negotiation (use default 1500)",
78       OPT_A2COPY, &lcp_allowoptions[0].neg_mru },
79     { "mru", o_int, &lcp_wantoptions[0].mru,
80       "Set MRU (maximum received packet size) for negotiation",
81       0, &lcp_wantoptions[0].neg_mru },
82     { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
83       "Disable protocol field compression",
84       OPT_A2COPY, &lcp_allowoptions[0].neg_pcompression },
85     { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
86       "Disable protocol field compression",
87       OPT_A2COPY, &lcp_allowoptions[0].neg_pcompression },
88     { "-p", o_bool, &lcp_wantoptions[0].passive,
89       "Set passive mode", 1 },
90     { "passive", o_bool, &lcp_wantoptions[0].passive,
91       "Set passive mode", 1 },
92     { "silent", o_bool, &lcp_wantoptions[0].silent,
93       "Set silent mode", 1 },
94     { "escape", o_special, setescape,
95       "List of character codes to escape on transmission" },
96     { "lcp-echo-failure", o_int, &lcp_echo_fails,
97       "Set number of consecutive echo failures to indicate link failure" },
98     { "lcp-echo-interval", o_int, &lcp_echo_interval,
99       "Set time in seconds between LCP echo requests" },
100     { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
101       "Set time in seconds between LCP retransmissions" },
102     { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
103       "Set maximum number of LCP terminate-request transmissions" },
104     { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
105       "Set maximum number of LCP configure-request transmissions" },
106     { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
107       "Set limit on number of LCP configure-naks" },
108     { "receive-all", o_bool, &lax_recv,
109       "Accept all received control characters", 1 },
110 #ifdef HAVE_MULTILINK
111     { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
112       "Use short sequence numbers in multilink headers",
113       OPT_A2COPY, &lcp_allowoptions[0].neg_ssnhf },
114     { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
115       "Don't use short sequence numbers in multilink headers",
116       OPT_A2COPY, &lcp_allowoptions[0].neg_ssnhf },
117 #endif /* HAVE_MULTILINK */
118     {NULL}
119 };
120
121 /* global vars */
122 fsm lcp_fsm[NUM_PPP];                   /* LCP fsm structure (global)*/
123 lcp_options lcp_wantoptions[NUM_PPP];   /* Options that we want to request */
124 lcp_options lcp_gotoptions[NUM_PPP];    /* Options that peer ack'd */
125 lcp_options lcp_allowoptions[NUM_PPP];  /* Options we allow peer to request */
126 lcp_options lcp_hisoptions[NUM_PPP];    /* Options that we ack'd */
127 u_int32_t xmit_accm[NUM_PPP][8];                /* extended transmit ACCM */
128
129 static int lcp_echos_pending = 0;       /* Number of outstanding echo msgs */
130 static int lcp_echo_number   = 0;       /* ID number of next echo frame */
131 static int lcp_echo_timer_running = 0;  /* set if a timer is running */
132
133 static u_char nak_buffer[PPP_MRU];      /* where we construct a nak packet */
134
135 /*
136  * Callbacks for fsm code.  (CI = Configuration Information)
137  */
138 static void lcp_resetci __P((fsm *));   /* Reset our CI */
139 static int  lcp_cilen __P((fsm *));             /* Return length of our CI */
140 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
141 static int  lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
142 static int  lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
143 static int  lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
144 static int  lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
145 static void lcp_up __P((fsm *));                /* We're UP */
146 static void lcp_down __P((fsm *));              /* We're DOWN */
147 static void lcp_starting __P((fsm *));  /* We need lower layer up */
148 static void lcp_finished __P((fsm *));  /* We need lower layer down */
149 static int  lcp_extcode __P((fsm *, int, int, u_char *, int));
150 static void lcp_rprotrej __P((fsm *, u_char *, int));
151
152 /*
153  * routines to send LCP echos to peer
154  */
155
156 static void lcp_echo_lowerup __P((int));
157 static void lcp_echo_lowerdown __P((int));
158 static void LcpEchoTimeout __P((void *));
159 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
160 static void LcpSendEchoRequest __P((fsm *));
161 static void LcpLinkFailure __P((fsm *));
162 static void LcpEchoCheck __P((fsm *));
163
164 static fsm_callbacks lcp_callbacks = {  /* LCP callback routines */
165     lcp_resetci,                /* Reset our Configuration Information */
166     lcp_cilen,                  /* Length of our Configuration Information */
167     lcp_addci,                  /* Add our Configuration Information */
168     lcp_ackci,                  /* ACK our Configuration Information */
169     lcp_nakci,                  /* NAK our Configuration Information */
170     lcp_rejci,                  /* Reject our Configuration Information */
171     lcp_reqci,                  /* Request peer's Configuration Information */
172     lcp_up,                     /* Called when fsm reaches OPENED state */
173     lcp_down,                   /* Called when fsm leaves OPENED state */
174     lcp_starting,               /* Called when we want the lower layer up */
175     lcp_finished,               /* Called when we want the lower layer down */
176     NULL,                       /* Called when Protocol-Reject received */
177     NULL,                       /* Retransmission is necessary */
178     lcp_extcode,                /* Called to handle LCP-specific codes */
179     "LCP"                       /* String name of protocol */
180 };
181
182 /*
183  * Protocol entry points.
184  * Some of these are called directly.
185  */
186
187 static void lcp_init __P((int));
188 static void lcp_input __P((int, u_char *, int));
189 static void lcp_protrej __P((int));
190 static int  lcp_printpkt __P((u_char *, int,
191                               void (*) __P((void *, char *, ...)), void *));
192
193 struct protent lcp_protent = {
194     PPP_LCP,
195     lcp_init,
196     lcp_input,
197     lcp_protrej,
198     lcp_lowerup,
199     lcp_lowerdown,
200     lcp_open,
201     lcp_close,
202     lcp_printpkt,
203     NULL,
204     1,
205     "LCP",
206     NULL,
207     lcp_option_list,
208     NULL,
209     NULL,
210     NULL
211 };
212
213 int lcp_loopbackfail = DEFLOOPBACKFAIL;
214
215 /*
216  * Length of each type of configuration option (in octets)
217  */
218 #define CILEN_VOID      2
219 #define CILEN_CHAR      3
220 #define CILEN_SHORT     4       /* CILEN_VOID + 2 */
221 #define CILEN_CHAP      5       /* CILEN_VOID + 2 + 1 */
222 #define CILEN_LONG      6       /* CILEN_VOID + 4 */
223 #define CILEN_LQR       8       /* CILEN_VOID + 2 + 4 */
224 #define CILEN_CBCP      3
225
226 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
227                          (x) == CONFNAK ? "NAK" : "REJ")
228
229
230 /*
231  * setescape - add chars to the set we escape on transmission.
232  */
233 static int
234 setescape(argv)
235     char **argv;
236 {
237     int n, ret;
238     char *p, *endp;
239
240     p = *argv;
241     ret = 1;
242     while (*p) {
243         n = strtol(p, &endp, 16);
244         if (p == endp) {
245             option_error("escape parameter contains invalid hex number '%s'",
246                          p);
247             return 0;
248         }
249         p = endp;
250         if (n < 0 || n == 0x5E || n > 0xFF) {
251             option_error("can't escape character 0x%x", n);
252             ret = 0;
253         } else
254             xmit_accm[0][n >> 5] |= 1 << (n & 0x1F);
255         while (*p == ',' || *p == ' ')
256             ++p;
257     }
258     return ret;
259 }
260
261 /*
262  * lcp_init - Initialize LCP.
263  */
264 static void
265 lcp_init(unit)
266     int unit;
267 {
268     fsm *f = &lcp_fsm[unit];
269     lcp_options *wo = &lcp_wantoptions[unit];
270     lcp_options *ao = &lcp_allowoptions[unit];
271
272     f->unit = unit;
273     f->protocol = PPP_LCP;
274     f->callbacks = &lcp_callbacks;
275
276     fsm_init(f);
277
278     wo->passive = 0;
279     wo->silent = 0;
280     wo->restart = 0;                    /* Set to 1 in kernels or multi-line
281                                            implementations */
282     wo->neg_mru = 1;
283     wo->mru = DEFMRU;
284     wo->neg_asyncmap = 1;
285     wo->asyncmap = 0;
286     wo->neg_chap = 0;                   /* Set to 1 on server */
287     wo->neg_upap = 0;                   /* Set to 1 on server */
288     wo->chap_mdtype = CHAP_DIGEST_MD5;
289     wo->neg_magicnumber = 1;
290     wo->neg_pcompression = 1;
291     wo->neg_accompression = 1;
292     wo->neg_lqr = 0;                    /* no LQR implementation yet */
293     wo->neg_cbcp = 0;
294
295     ao->neg_mru = 1;
296     ao->mru = MAXMRU;
297     ao->neg_asyncmap = 1;
298     ao->asyncmap = 0;
299     ao->neg_chap = 1;
300     ao->chap_mdtype = CHAP_DIGEST_MD5;
301     ao->neg_upap = 1;
302     ao->neg_magicnumber = 1;
303     ao->neg_pcompression = 1;
304     ao->neg_accompression = 1;
305     ao->neg_lqr = 0;                    /* no LQR implementation yet */
306 #ifdef CBCP_SUPPORT
307     ao->neg_cbcp = 1;
308 #else
309     ao->neg_cbcp = 0;
310 #endif
311
312     memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
313     xmit_accm[unit][3] = 0x60000000;
314 }
315
316
317 /*
318  * lcp_open - LCP is allowed to come up.
319  */
320 void
321 lcp_open(unit)
322     int unit;
323 {
324     fsm *f = &lcp_fsm[unit];
325     lcp_options *wo = &lcp_wantoptions[unit];
326
327     f->flags = 0;
328     if (wo->passive)
329         f->flags |= OPT_PASSIVE;
330     if (wo->silent)
331         f->flags |= OPT_SILENT;
332     fsm_open(f);
333 }
334
335
336 /*
337  * lcp_close - Take LCP down.
338  */
339 void
340 lcp_close(unit, reason)
341     int unit;
342     char *reason;
343 {
344     fsm *f = &lcp_fsm[unit];
345
346     if (phase != PHASE_DEAD)
347         new_phase(PHASE_TERMINATE);
348     if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
349         /*
350          * This action is not strictly according to the FSM in RFC1548,
351          * but it does mean that the program terminates if you do a
352          * lcp_close() in passive/silent mode when a connection hasn't
353          * been established.
354          */
355         f->state = CLOSED;
356         lcp_finished(f);
357
358     } else
359         fsm_close(&lcp_fsm[unit], reason);
360 }
361
362
363 /*
364  * lcp_lowerup - The lower layer is up.
365  */
366 void
367 lcp_lowerup(unit)
368     int unit;
369 {
370     lcp_options *wo = &lcp_wantoptions[unit];
371
372     /*
373      * Don't use A/C or protocol compression on transmission,
374      * but accept A/C and protocol compressed packets
375      * if we are going to ask for A/C and protocol compression.
376      */
377     ppp_set_xaccm(unit, xmit_accm[unit]);
378     ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
379     ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
380                     wo->neg_pcompression, wo->neg_accompression);
381     peer_mru[unit] = PPP_MRU;
382     lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
383
384     fsm_lowerup(&lcp_fsm[unit]);
385 }
386
387
388 /*
389  * lcp_lowerdown - The lower layer is down.
390  */
391 void
392 lcp_lowerdown(unit)
393     int unit;
394 {
395     fsm_lowerdown(&lcp_fsm[unit]);
396 }
397
398
399 /*
400  * lcp_input - Input LCP packet.
401  */
402 static void
403 lcp_input(unit, p, len)
404     int unit;
405     u_char *p;
406     int len;
407 {
408     fsm *f = &lcp_fsm[unit];
409
410     fsm_input(f, p, len);
411 }
412
413
414 /*
415  * lcp_extcode - Handle a LCP-specific code.
416  */
417 static int
418 lcp_extcode(f, code, id, inp, len)
419     fsm *f;
420     int code, id;
421     u_char *inp;
422     int len;
423 {
424     u_char *magp;
425
426     switch( code ){
427     case PROTREJ:
428         lcp_rprotrej(f, inp, len);
429         break;
430     
431     case ECHOREQ:
432         if (f->state != OPENED)
433             break;
434         magp = inp;
435         PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
436         fsm_sdata(f, ECHOREP, id, inp, len);
437         break;
438     
439     case ECHOREP:
440         lcp_received_echo_reply(f, id, inp, len);
441         break;
442
443     case DISCREQ:
444         break;
445
446     default:
447         return 0;
448     }
449     return 1;
450 }
451
452     
453 /*
454  * lcp_rprotrej - Receive an Protocol-Reject.
455  *
456  * Figure out which protocol is rejected and inform it.
457  */
458 static void
459 lcp_rprotrej(f, inp, len)
460     fsm *f;
461     u_char *inp;
462     int len;
463 {
464     int i;
465     struct protent *protp;
466     u_short prot;
467
468     if (len < 2) {
469         LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
470         return;
471     }
472
473     GETSHORT(prot, inp);
474
475     /*
476      * Protocol-Reject packets received in any state other than the LCP
477      * OPENED state SHOULD be silently discarded.
478      */
479     if( f->state != OPENED ){
480         LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
481         return;
482     }
483
484     /*
485      * Upcall the proper Protocol-Reject routine.
486      */
487     for (i = 0; (protp = protocols[i]) != NULL; ++i)
488         if (protp->protocol == prot && protp->enabled_flag) {
489             (*protp->protrej)(f->unit);
490             return;
491         }
492
493     warn("Protocol-Reject for unsupported protocol 0x%x", prot);
494 }
495
496
497 /*
498  * lcp_protrej - A Protocol-Reject was received.
499  */
500 /*ARGSUSED*/
501 static void
502 lcp_protrej(unit)
503     int unit;
504 {
505     /*
506      * Can't reject LCP!
507      */
508     error("Received Protocol-Reject for LCP!");
509     fsm_protreject(&lcp_fsm[unit]);
510 }
511
512
513 /*
514  * lcp_sprotrej - Send a Protocol-Reject for some protocol.
515  */
516 void
517 lcp_sprotrej(unit, p, len)
518     int unit;
519     u_char *p;
520     int len;
521 {
522     /*
523      * Send back the protocol and the information field of the
524      * rejected packet.  We only get here if LCP is in the OPENED state.
525      */
526     p += 2;
527     len -= 2;
528
529     fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
530               p, len);
531 }
532
533
534 /*
535  * lcp_resetci - Reset our CI.
536  */
537 static void
538 lcp_resetci(f)
539     fsm *f;
540 {
541     lcp_options *wo = &lcp_wantoptions[f->unit];
542
543     wo->magicnumber = magic();
544     wo->numloops = 0;
545     if (!wo->neg_multilink)
546         wo->neg_ssnhf = 0;
547     lcp_gotoptions[f->unit] = *wo;
548     peer_mru[f->unit] = PPP_MRU;
549     auth_reset(f->unit);
550 }
551
552
553 /*
554  * lcp_cilen - Return length of our CI.
555  */
556 static int
557 lcp_cilen(f)
558     fsm *f;
559 {
560     lcp_options *go = &lcp_gotoptions[f->unit];
561
562 #define LENCIVOID(neg)  ((neg) ? CILEN_VOID : 0)
563 #define LENCICHAP(neg)  ((neg) ? CILEN_CHAP : 0)
564 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
565 #define LENCILONG(neg)  ((neg) ? CILEN_LONG : 0)
566 #define LENCILQR(neg)   ((neg) ? CILEN_LQR: 0)
567 #define LENCICBCP(neg)  ((neg) ? CILEN_CBCP: 0)
568     /*
569      * NB: we only ask for one of CHAP and UPAP, even if we will
570      * accept either.
571      */
572     return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
573             LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
574             LENCICHAP(go->neg_chap) +
575             LENCISHORT(!go->neg_chap && go->neg_upap) +
576             LENCILQR(go->neg_lqr) +
577             LENCICBCP(go->neg_cbcp) +
578             LENCILONG(go->neg_magicnumber) +
579             LENCIVOID(go->neg_pcompression) +
580             LENCIVOID(go->neg_accompression) +
581             LENCISHORT(go->neg_multilink) +
582             LENCIVOID(go->neg_ssnhf) +
583             (go->neg_endpoint? CILEN_CHAR + go->endp_len: 0));
584 }
585
586
587 /*
588  * lcp_addci - Add our desired CIs to a packet.
589  */
590 static void
591 lcp_addci(f, ucp, lenp)
592     fsm *f;
593     u_char *ucp;
594     int *lenp;
595 {
596     lcp_options *go = &lcp_gotoptions[f->unit];
597     u_char *start_ucp = ucp;
598
599 #define ADDCIVOID(opt, neg) \
600     if (neg) { \
601         PUTCHAR(opt, ucp); \
602         PUTCHAR(CILEN_VOID, ucp); \
603     }
604 #define ADDCISHORT(opt, neg, val) \
605     if (neg) { \
606         PUTCHAR(opt, ucp); \
607         PUTCHAR(CILEN_SHORT, ucp); \
608         PUTSHORT(val, ucp); \
609     }
610 #define ADDCICHAP(opt, neg, val, digest) \
611     if (neg) { \
612         PUTCHAR(opt, ucp); \
613         PUTCHAR(CILEN_CHAP, ucp); \
614         PUTSHORT(val, ucp); \
615         PUTCHAR(digest, ucp); \
616     }
617 #define ADDCILONG(opt, neg, val) \
618     if (neg) { \
619         PUTCHAR(opt, ucp); \
620         PUTCHAR(CILEN_LONG, ucp); \
621         PUTLONG(val, ucp); \
622     }
623 #define ADDCILQR(opt, neg, val) \
624     if (neg) { \
625         PUTCHAR(opt, ucp); \
626         PUTCHAR(CILEN_LQR, ucp); \
627         PUTSHORT(PPP_LQR, ucp); \
628         PUTLONG(val, ucp); \
629     }
630 #define ADDCICHAR(opt, neg, val) \
631     if (neg) { \
632         PUTCHAR(opt, ucp); \
633         PUTCHAR(CILEN_CHAR, ucp); \
634         PUTCHAR(val, ucp); \
635     }
636 #define ADDCIENDP(opt, neg, class, val, len) \
637     if (neg) { \
638         int i; \
639         PUTCHAR(opt, ucp); \
640         PUTCHAR(CILEN_CHAR + len, ucp); \
641         PUTCHAR(class, ucp); \
642         for (i = 0; i < len; ++i) \
643             PUTCHAR(val[i], ucp); \
644     }
645
646     ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
647     ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
648               go->asyncmap);
649     ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
650     ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
651     ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
652     ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
653     ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
654     ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
655     ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
656     ADDCISHORT(CI_MRRU, go->neg_multilink, go->mrru);
657     ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
658     ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endp_class, go->endpoint,
659               go->endp_len);
660
661     if (ucp - start_ucp != *lenp) {
662         /* this should never happen, because peer_mtu should be 1500 */
663         error("Bug in lcp_addci: wrong length");
664     }
665 }
666
667
668 /*
669  * lcp_ackci - Ack our CIs.
670  * This should not modify any state if the Ack is bad.
671  *
672  * Returns:
673  *      0 - Ack was bad.
674  *      1 - Ack was good.
675  */
676 static int
677 lcp_ackci(f, p, len)
678     fsm *f;
679     u_char *p;
680     int len;
681 {
682     lcp_options *go = &lcp_gotoptions[f->unit];
683     u_char cilen, citype, cichar;
684     u_short cishort;
685     u_int32_t cilong;
686
687     /*
688      * CIs must be in exactly the same order that we sent.
689      * Check packet length and CI length at each step.
690      * If we find any deviations, then this packet is bad.
691      */
692 #define ACKCIVOID(opt, neg) \
693     if (neg) { \
694         if ((len -= CILEN_VOID) < 0) \
695             goto bad; \
696         GETCHAR(citype, p); \
697         GETCHAR(cilen, p); \
698         if (cilen != CILEN_VOID || \
699             citype != opt) \
700             goto bad; \
701     }
702 #define ACKCISHORT(opt, neg, val) \
703     if (neg) { \
704         if ((len -= CILEN_SHORT) < 0) \
705             goto bad; \
706         GETCHAR(citype, p); \
707         GETCHAR(cilen, p); \
708         if (cilen != CILEN_SHORT || \
709             citype != opt) \
710             goto bad; \
711         GETSHORT(cishort, p); \
712         if (cishort != val) \
713             goto bad; \
714     }
715 #define ACKCICHAR(opt, neg, val) \
716     if (neg) { \
717         if ((len -= CILEN_CHAR) < 0) \
718             goto bad; \
719         GETCHAR(citype, p); \
720         GETCHAR(cilen, p); \
721         if (cilen != CILEN_CHAR || \
722             citype != opt) \
723             goto bad; \
724         GETCHAR(cichar, p); \
725         if (cichar != val) \
726             goto bad; \
727     }
728 #define ACKCICHAP(opt, neg, val, digest) \
729     if (neg) { \
730         if ((len -= CILEN_CHAP) < 0) \
731             goto bad; \
732         GETCHAR(citype, p); \
733         GETCHAR(cilen, p); \
734         if (cilen != CILEN_CHAP || \
735             citype != opt) \
736             goto bad; \
737         GETSHORT(cishort, p); \
738         if (cishort != val) \
739             goto bad; \
740         GETCHAR(cichar, p); \
741         if (cichar != digest) \
742           goto bad; \
743     }
744 #define ACKCILONG(opt, neg, val) \
745     if (neg) { \
746         if ((len -= CILEN_LONG) < 0) \
747             goto bad; \
748         GETCHAR(citype, p); \
749         GETCHAR(cilen, p); \
750         if (cilen != CILEN_LONG || \
751             citype != opt) \
752             goto bad; \
753         GETLONG(cilong, p); \
754         if (cilong != val) \
755             goto bad; \
756     }
757 #define ACKCILQR(opt, neg, val) \
758     if (neg) { \
759         if ((len -= CILEN_LQR) < 0) \
760             goto bad; \
761         GETCHAR(citype, p); \
762         GETCHAR(cilen, p); \
763         if (cilen != CILEN_LQR || \
764             citype != opt) \
765             goto bad; \
766         GETSHORT(cishort, p); \
767         if (cishort != PPP_LQR) \
768             goto bad; \
769         GETLONG(cilong, p); \
770         if (cilong != val) \
771           goto bad; \
772     }
773 #define ACKCIENDP(opt, neg, class, val, vlen) \
774     if (neg) { \
775         int i; \
776         if ((len -= CILEN_CHAR + vlen) < 0) \
777             goto bad; \
778         GETCHAR(citype, p); \
779         GETCHAR(cilen, p); \
780         if (cilen != CILEN_CHAR + vlen || \
781             citype != opt) \
782             goto bad; \
783         GETCHAR(cichar, p); \
784         if (cichar != class) \
785             goto bad; \
786         for (i = 0; i < vlen; ++i) { \
787             GETCHAR(cichar, p); \
788             if (cichar != val[i]) \
789                 goto bad; \
790         } \
791     }
792
793     ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
794     ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
795               go->asyncmap);
796     ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
797     ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
798     ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
799     ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
800     ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
801     ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
802     ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
803     ACKCISHORT(CI_MRRU, go->neg_multilink, go->mrru);
804     ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
805     ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endp_class, go->endpoint,
806               go->endp_len);
807
808     /*
809      * If there are any remaining CIs, then this packet is bad.
810      */
811     if (len != 0)
812         goto bad;
813     return (1);
814 bad:
815     LCPDEBUG(("lcp_acki: received bad Ack!"));
816     return (0);
817 }
818
819
820 /*
821  * lcp_nakci - Peer has sent a NAK for some of our CIs.
822  * This should not modify any state if the Nak is bad
823  * or if LCP is in the OPENED state.
824  *
825  * Returns:
826  *      0 - Nak was bad.
827  *      1 - Nak was good.
828  */
829 static int
830 lcp_nakci(f, p, len)
831     fsm *f;
832     u_char *p;
833     int len;
834 {
835     lcp_options *go = &lcp_gotoptions[f->unit];
836     lcp_options *wo = &lcp_wantoptions[f->unit];
837     u_char citype, cichar, *next;
838     u_short cishort;
839     u_int32_t cilong;
840     lcp_options no;             /* options we've seen Naks for */
841     lcp_options try;            /* options to request next time */
842     int looped_back = 0;
843     int cilen;
844
845     BZERO(&no, sizeof(no));
846     try = *go;
847
848     /*
849      * Any Nak'd CIs must be in exactly the same order that we sent.
850      * Check packet length and CI length at each step.
851      * If we find any deviations, then this packet is bad.
852      */
853 #define NAKCIVOID(opt, neg) \
854     if (go->neg && \
855         len >= CILEN_VOID && \
856         p[1] == CILEN_VOID && \
857         p[0] == opt) { \
858         len -= CILEN_VOID; \
859         INCPTR(CILEN_VOID, p); \
860         no.neg = 1; \
861         try.neg = 0; \
862     }
863 #define NAKCICHAP(opt, neg, code) \
864     if (go->neg && \
865         len >= CILEN_CHAP && \
866         p[1] == CILEN_CHAP && \
867         p[0] == opt) { \
868         len -= CILEN_CHAP; \
869         INCPTR(2, p); \
870         GETSHORT(cishort, p); \
871         GETCHAR(cichar, p); \
872         no.neg = 1; \
873         code \
874     }
875 #define NAKCICHAR(opt, neg, code) \
876     if (go->neg && \
877         len >= CILEN_CHAR && \
878         p[1] == CILEN_CHAR && \
879         p[0] == opt) { \
880         len -= CILEN_CHAR; \
881         INCPTR(2, p); \
882         GETCHAR(cichar, p); \
883         no.neg = 1; \
884         code \
885     }
886 #define NAKCISHORT(opt, neg, code) \
887     if (go->neg && \
888         len >= CILEN_SHORT && \
889         p[1] == CILEN_SHORT && \
890         p[0] == opt) { \
891         len -= CILEN_SHORT; \
892         INCPTR(2, p); \
893         GETSHORT(cishort, p); \
894         no.neg = 1; \
895         code \
896     }
897 #define NAKCILONG(opt, neg, code) \
898     if (go->neg && \
899         len >= CILEN_LONG && \
900         p[1] == CILEN_LONG && \
901         p[0] == opt) { \
902         len -= CILEN_LONG; \
903         INCPTR(2, p); \
904         GETLONG(cilong, p); \
905         no.neg = 1; \
906         code \
907     }
908 #define NAKCILQR(opt, neg, code) \
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         no.neg = 1; \
918         code \
919     }
920 #define NAKCIENDP(opt, neg) \
921     if (go->neg && \
922         len >= CILEN_CHAR && \
923         p[0] == opt && \
924         p[1] >= CILEN_CHAR && \
925         p[1] <= len) { \
926         len -= p[1]; \
927         INCPTR(p[1], p); \
928         no.neg = 1; \
929         try.neg = 0; \
930     }
931
932     /*
933      * We don't care if they want to send us smaller packets than
934      * we want.  Therefore, accept any MRU less than what we asked for,
935      * but then ignore the new value when setting the MRU in the kernel.
936      * If they send us a bigger MRU than what we asked, accept it, up to
937      * the limit of the default MRU we'd get if we didn't negotiate.
938      */
939     if (go->neg_mru && go->mru != DEFMRU) {
940         NAKCISHORT(CI_MRU, neg_mru,
941                    if (cishort <= wo->mru || cishort <= DEFMRU)
942                        try.mru = cishort;
943                    );
944     }
945
946     /*
947      * Add any characters they want to our (receive-side) asyncmap.
948      */
949     if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
950         NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
951                   try.asyncmap = go->asyncmap | cilong;
952                   );
953     }
954
955     /*
956      * If they've nak'd our authentication-protocol, check whether
957      * they are proposing a different protocol, or a different
958      * hash algorithm for CHAP.
959      */
960     if ((go->neg_chap || go->neg_upap)
961         && len >= CILEN_SHORT
962         && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
963         cilen = p[1];
964         len -= cilen;
965         no.neg_chap = go->neg_chap;
966         no.neg_upap = go->neg_upap;
967         INCPTR(2, p);
968         GETSHORT(cishort, p);
969         if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
970             /*
971              * If we were asking for CHAP, they obviously don't want to do it.
972              * If we weren't asking for CHAP, then we were asking for PAP,
973              * in which case this Nak is bad.
974              */
975             if (!go->neg_chap)
976                 goto bad;
977             try.neg_chap = 0;
978
979         } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
980             GETCHAR(cichar, p);
981             if (go->neg_chap) {
982                 /*
983                  * We were asking for CHAP/MD5; they must want a different
984                  * algorithm.  If they can't do MD5, we can ask for M$-CHAP
985                  * if we support it, otherwise we'll have to stop
986                  * asking for CHAP.
987                  */
988                 if (cichar != go->chap_mdtype) {
989 #ifdef CHAPMS
990                     if (cichar == CHAP_MICROSOFT)
991                         go->chap_mdtype = CHAP_MICROSOFT;
992                     else
993 #endif /* CHAPMS */
994                         try.neg_chap = 0;
995                 }
996             } else {
997                 /*
998                  * Stop asking for PAP if we were asking for it.
999                  */
1000                 try.neg_upap = 0;
1001             }
1002
1003         } else {
1004             /*
1005              * We don't recognize what they're suggesting.
1006              * Stop asking for what we were asking for.
1007              */
1008             if (go->neg_chap)
1009                 try.neg_chap = 0;
1010             else
1011                 try.neg_upap = 0;
1012             p += cilen - CILEN_SHORT;
1013         }
1014     }
1015
1016     /*
1017      * If they can't cope with our link quality protocol, we'll have
1018      * to stop asking for LQR.  We haven't got any other protocol.
1019      * If they Nak the reporting period, take their value XXX ?
1020      */
1021     NAKCILQR(CI_QUALITY, neg_lqr,
1022              if (cishort != PPP_LQR)
1023                  try.neg_lqr = 0;
1024              else
1025                  try.lqr_period = cilong;
1026              );
1027
1028     /*
1029      * Only implementing CBCP...not the rest of the callback options
1030      */
1031     NAKCICHAR(CI_CALLBACK, neg_cbcp,
1032               try.neg_cbcp = 0;
1033               );
1034
1035     /*
1036      * Check for a looped-back line.
1037      */
1038     NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1039               try.magicnumber = magic();
1040               looped_back = 1;
1041               );
1042
1043     /*
1044      * Peer shouldn't send Nak for protocol compression or
1045      * address/control compression requests; they should send
1046      * a Reject instead.  If they send a Nak, treat it as a Reject.
1047      */
1048     NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1049     NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1050
1051     /*
1052      * Nak for MRRU option - accept their value if it is smaller
1053      * than the one we want.
1054      */
1055     if (go->neg_multilink) {
1056         NAKCISHORT(CI_MRRU, neg_multilink,
1057                    if (cishort <= wo->mrru)
1058                        try.mrru = cishort;
1059                    );
1060     }
1061
1062     /*
1063      * Nak for short sequence numbers shouldn't be sent, treat it
1064      * like a reject.
1065      */
1066     NAKCIVOID(CI_SSNHF, neg_ssnhf);
1067
1068     /*
1069      * Nak of the endpoint discriminator option is not permitted,
1070      * treat it like a reject.
1071      */
1072     NAKCIENDP(CI_EPDISC, neg_endpoint);
1073
1074     /*
1075      * There may be remaining CIs, if the peer is requesting negotiation
1076      * on an option that we didn't include in our request packet.
1077      * If we see an option that we requested, or one we've already seen
1078      * in this packet, then this packet is bad.
1079      * If we wanted to respond by starting to negotiate on the requested
1080      * option(s), we could, but we don't, because except for the
1081      * authentication type and quality protocol, if we are not negotiating
1082      * an option, it is because we were told not to.
1083      * For the authentication type, the Nak from the peer means
1084      * `let me authenticate myself with you' which is a bit pointless.
1085      * For the quality protocol, the Nak means `ask me to send you quality
1086      * reports', but if we didn't ask for them, we don't want them.
1087      * An option we don't recognize represents the peer asking to
1088      * negotiate some option we don't support, so ignore it.
1089      */
1090     while (len > CILEN_VOID) {
1091         GETCHAR(citype, p);
1092         GETCHAR(cilen, p);
1093         if (cilen < CILEN_VOID || (len -= cilen) < 0)
1094             goto bad;
1095         next = p + cilen - 2;
1096
1097         switch (citype) {
1098         case CI_MRU:
1099             if ((go->neg_mru && go->mru != DEFMRU)
1100                 || no.neg_mru || cilen != CILEN_SHORT)
1101                 goto bad;
1102             GETSHORT(cishort, p);
1103             if (cishort < DEFMRU) {
1104                 try.neg_mru = 1;
1105                 try.mru = cishort;
1106             }
1107             break;
1108         case CI_ASYNCMAP:
1109             if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1110                 || no.neg_asyncmap || cilen != CILEN_LONG)
1111                 goto bad;
1112             break;
1113         case CI_AUTHTYPE:
1114             if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
1115                 goto bad;
1116             break;
1117         case CI_MAGICNUMBER:
1118             if (go->neg_magicnumber || no.neg_magicnumber ||
1119                 cilen != CILEN_LONG)
1120                 goto bad;
1121             break;
1122         case CI_PCOMPRESSION:
1123             if (go->neg_pcompression || no.neg_pcompression
1124                 || cilen != CILEN_VOID)
1125                 goto bad;
1126             break;
1127         case CI_ACCOMPRESSION:
1128             if (go->neg_accompression || no.neg_accompression
1129                 || cilen != CILEN_VOID)
1130                 goto bad;
1131             break;
1132         case CI_QUALITY:
1133             if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1134                 goto bad;
1135             break;
1136         case CI_MRRU:
1137             if (go->neg_multilink || no.neg_multilink || cilen != CILEN_SHORT)
1138                 goto bad;
1139             break;
1140         case CI_SSNHF:
1141             if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1142                 goto bad;
1143             try.neg_ssnhf = 1;
1144             break;
1145         case CI_EPDISC:
1146             if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1147                 goto bad;
1148             break;
1149         }
1150         p = next;
1151     }
1152
1153     /*
1154      * OK, the Nak is good.  Now we can update state.
1155      * If there are any options left we ignore them.
1156      */
1157     if (f->state != OPENED) {
1158         if (looped_back) {
1159             if (++try.numloops >= lcp_loopbackfail) {
1160                 notice("Serial line is looped back.");
1161                 lcp_close(f->unit, "Loopback detected");
1162                 status = EXIT_LOOPBACK;
1163             }
1164         } else
1165             try.numloops = 0;
1166         *go = try;
1167     }
1168
1169     return 1;
1170
1171 bad:
1172     LCPDEBUG(("lcp_nakci: received bad Nak!"));
1173     return 0;
1174 }
1175
1176
1177 /*
1178  * lcp_rejci - Peer has Rejected some of our CIs.
1179  * This should not modify any state if the Reject is bad
1180  * or if LCP is in the OPENED state.
1181  *
1182  * Returns:
1183  *      0 - Reject was bad.
1184  *      1 - Reject was good.
1185  */
1186 static int
1187 lcp_rejci(f, p, len)
1188     fsm *f;
1189     u_char *p;
1190     int len;
1191 {
1192     lcp_options *go = &lcp_gotoptions[f->unit];
1193     u_char cichar;
1194     u_short cishort;
1195     u_int32_t cilong;
1196     lcp_options try;            /* options to request next time */
1197
1198     try = *go;
1199
1200     /*
1201      * Any Rejected CIs must be in exactly the same order that we sent.
1202      * Check packet length and CI length at each step.
1203      * If we find any deviations, then this packet is bad.
1204      */
1205 #define REJCIVOID(opt, neg) \
1206     if (go->neg && \
1207         len >= CILEN_VOID && \
1208         p[1] == CILEN_VOID && \
1209         p[0] == opt) { \
1210         len -= CILEN_VOID; \
1211         INCPTR(CILEN_VOID, p); \
1212         try.neg = 0; \
1213     }
1214 #define REJCISHORT(opt, neg, val) \
1215     if (go->neg && \
1216         len >= CILEN_SHORT && \
1217         p[1] == CILEN_SHORT && \
1218         p[0] == opt) { \
1219         len -= CILEN_SHORT; \
1220         INCPTR(2, p); \
1221         GETSHORT(cishort, p); \
1222         /* Check rejected value. */ \
1223         if (cishort != val) \
1224             goto bad; \
1225         try.neg = 0; \
1226     }
1227 #define REJCICHAP(opt, neg, val, digest) \
1228     if (go->neg && \
1229         len >= CILEN_CHAP && \
1230         p[1] == CILEN_CHAP && \
1231         p[0] == opt) { \
1232         len -= CILEN_CHAP; \
1233         INCPTR(2, p); \
1234         GETSHORT(cishort, p); \
1235         GETCHAR(cichar, p); \
1236         /* Check rejected value. */ \
1237         if (cishort != val || cichar != digest) \
1238             goto bad; \
1239         try.neg = 0; \
1240         try.neg_upap = 0; \
1241     }
1242 #define REJCILONG(opt, neg, val) \
1243     if (go->neg && \
1244         len >= CILEN_LONG && \
1245         p[1] == CILEN_LONG && \
1246         p[0] == opt) { \
1247         len -= CILEN_LONG; \
1248         INCPTR(2, p); \
1249         GETLONG(cilong, p); \
1250         /* Check rejected value. */ \
1251         if (cilong != val) \
1252             goto bad; \
1253         try.neg = 0; \
1254     }
1255 #define REJCILQR(opt, neg, val) \
1256     if (go->neg && \
1257         len >= CILEN_LQR && \
1258         p[1] == CILEN_LQR && \
1259         p[0] == opt) { \
1260         len -= CILEN_LQR; \
1261         INCPTR(2, p); \
1262         GETSHORT(cishort, p); \
1263         GETLONG(cilong, p); \
1264         /* Check rejected value. */ \
1265         if (cishort != PPP_LQR || cilong != val) \
1266             goto bad; \
1267         try.neg = 0; \
1268     }
1269 #define REJCICBCP(opt, neg, val) \
1270     if (go->neg && \
1271         len >= CILEN_CBCP && \
1272         p[1] == CILEN_CBCP && \
1273         p[0] == opt) { \
1274         len -= CILEN_CBCP; \
1275         INCPTR(2, p); \
1276         GETCHAR(cichar, p); \
1277         /* Check rejected value. */ \
1278         if (cichar != val) \
1279             goto bad; \
1280         try.neg = 0; \
1281     }
1282 #define REJCIENDP(opt, neg, class, val, vlen) \
1283     if (go->neg && \
1284         len >= CILEN_CHAR + vlen && \
1285         p[0] == opt && \
1286         p[1] == CILEN_CHAR + vlen) { \
1287         int i; \
1288         len -= CILEN_CHAR + vlen; \
1289         INCPTR(p[1], p); \
1290         GETCHAR(cichar, p); \
1291         if (cichar != class) \
1292             goto bad; \
1293         for (i = 0; i < vlen; ++i) { \
1294             GETCHAR(cichar, p); \
1295             if (cichar != val[i]) \
1296                 goto bad; \
1297         } \
1298         try.neg = 0; \
1299     }
1300
1301     REJCISHORT(CI_MRU, neg_mru, go->mru);
1302     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1303     REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1304     if (!go->neg_chap) {
1305         REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1306     }
1307     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1308     REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1309     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1310     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1311     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1312     REJCISHORT(CI_MRRU, neg_multilink, go->mrru);
1313     REJCIVOID(CI_SSNHF, neg_ssnhf);
1314     REJCIENDP(CI_EPDISC, neg_endpoint, go->endp_class, go->endpoint,
1315               go->endp_len);
1316
1317     /*
1318      * If there are any remaining CIs, then this packet is bad.
1319      */
1320     if (len != 0)
1321         goto bad;
1322     /*
1323      * Now we can update state.
1324      */
1325     if (f->state != OPENED)
1326         *go = try;
1327     return 1;
1328
1329 bad:
1330     LCPDEBUG(("lcp_rejci: received bad Reject!"));
1331     return 0;
1332 }
1333
1334
1335 /*
1336  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1337  *
1338  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1339  * appropriately.  If reject_if_disagree is non-zero, doesn't return
1340  * CONFNAK; returns CONFREJ if it can't return CONFACK.
1341  */
1342 static int
1343 lcp_reqci(f, inp, lenp, reject_if_disagree)
1344     fsm *f;
1345     u_char *inp;                /* Requested CIs */
1346     int *lenp;                  /* Length of requested CIs */
1347     int reject_if_disagree;
1348 {
1349     lcp_options *go = &lcp_gotoptions[f->unit];
1350     lcp_options *ho = &lcp_hisoptions[f->unit];
1351     lcp_options *ao = &lcp_allowoptions[f->unit];
1352     u_char *cip, *next;         /* Pointer to current and next CIs */
1353     int cilen, citype, cichar;  /* Parsed len, type, char value */
1354     u_short cishort;            /* Parsed short value */
1355     u_int32_t cilong;           /* Parse long value */
1356     int rc = CONFACK;           /* Final packet return code */
1357     int orc;                    /* Individual option return code */
1358     u_char *p;                  /* Pointer to next char to parse */
1359     u_char *rejp;               /* Pointer to next char in reject frame */
1360     u_char *nakp;               /* Pointer to next char in Nak frame */
1361     int l = *lenp;              /* Length left */
1362
1363     /*
1364      * Reset all his options.
1365      */
1366     BZERO(ho, sizeof(*ho));
1367
1368     /*
1369      * Process all his options.
1370      */
1371     next = inp;
1372     nakp = nak_buffer;
1373     rejp = inp;
1374     while (l) {
1375         orc = CONFACK;                  /* Assume success */
1376         cip = p = next;                 /* Remember begining of CI */
1377         if (l < 2 ||                    /* Not enough data for CI header or */
1378             p[1] < 2 ||                 /*  CI length too small or */
1379             p[1] > l) {                 /*  CI length too big? */
1380             LCPDEBUG(("lcp_reqci: bad CI length!"));
1381             orc = CONFREJ;              /* Reject bad CI */
1382             cilen = l;                  /* Reject till end of packet */
1383             l = 0;                      /* Don't loop again */
1384             citype = 0;
1385             goto endswitch;
1386         }
1387         GETCHAR(citype, p);             /* Parse CI type */
1388         GETCHAR(cilen, p);              /* Parse CI length */
1389         l -= cilen;                     /* Adjust remaining length */
1390         next += cilen;                  /* Step to next CI */
1391
1392         switch (citype) {               /* Check CI type */
1393         case CI_MRU:
1394             if (!ao->neg_mru ||         /* Allow option? */
1395                 cilen != CILEN_SHORT) { /* Check CI length */
1396                 orc = CONFREJ;          /* Reject CI */
1397                 break;
1398             }
1399             GETSHORT(cishort, p);       /* Parse MRU */
1400
1401             /*
1402              * He must be able to receive at least our minimum.
1403              * No need to check a maximum.  If he sends a large number,
1404              * we'll just ignore it.
1405              */
1406             if (cishort < MINMRU) {
1407                 orc = CONFNAK;          /* Nak CI */
1408                 PUTCHAR(CI_MRU, nakp);
1409                 PUTCHAR(CILEN_SHORT, nakp);
1410                 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1411                 break;
1412             }
1413             ho->neg_mru = 1;            /* Remember he sent MRU */
1414             ho->mru = cishort;          /* And remember value */
1415             break;
1416
1417         case CI_ASYNCMAP:
1418             if (!ao->neg_asyncmap ||
1419                 cilen != CILEN_LONG) {
1420                 orc = CONFREJ;
1421                 break;
1422             }
1423             GETLONG(cilong, p);
1424
1425             /*
1426              * Asyncmap must have set at least the bits
1427              * which are set in lcp_allowoptions[unit].asyncmap.
1428              */
1429             if ((ao->asyncmap & ~cilong) != 0) {
1430                 orc = CONFNAK;
1431                 PUTCHAR(CI_ASYNCMAP, nakp);
1432                 PUTCHAR(CILEN_LONG, nakp);
1433                 PUTLONG(ao->asyncmap | cilong, nakp);
1434                 break;
1435             }
1436             ho->neg_asyncmap = 1;
1437             ho->asyncmap = cilong;
1438             break;
1439
1440         case CI_AUTHTYPE:
1441             if (cilen < CILEN_SHORT ||
1442                 !(ao->neg_upap || ao->neg_chap)) {
1443                 /*
1444                  * Reject the option if we're not willing to authenticate.
1445                  */
1446                 orc = CONFREJ;
1447                 break;
1448             }
1449             GETSHORT(cishort, p);
1450
1451             /*
1452              * Authtype must be PAP or CHAP.
1453              *
1454              * Note: if both ao->neg_upap and ao->neg_chap are set,
1455              * and the peer sends a Configure-Request with two
1456              * authenticate-protocol requests, one for CHAP and one
1457              * for UPAP, then we will reject the second request.
1458              * Whether we end up doing CHAP or UPAP depends then on
1459              * the ordering of the CIs in the peer's Configure-Request.
1460              */
1461
1462             if (cishort == PPP_PAP) {
1463                 if (ho->neg_chap ||     /* we've already accepted CHAP */
1464                     cilen != CILEN_SHORT) {
1465                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1466                     orc = CONFREJ;
1467                     break;
1468                 }
1469                 if (!ao->neg_upap) {    /* we don't want to do PAP */
1470                     orc = CONFNAK;      /* NAK it and suggest CHAP */
1471                     PUTCHAR(CI_AUTHTYPE, nakp);
1472                     PUTCHAR(CILEN_CHAP, nakp);
1473                     PUTSHORT(PPP_CHAP, nakp);
1474                     PUTCHAR(ao->chap_mdtype, nakp);
1475                     /* XXX if we can do CHAP_MICROSOFT as well, we should
1476                        probably put in another option saying so */
1477                     break;
1478                 }
1479                 ho->neg_upap = 1;
1480                 break;
1481             }
1482             if (cishort == PPP_CHAP) {
1483                 if (ho->neg_upap ||     /* we've already accepted PAP */
1484                     cilen != CILEN_CHAP) {
1485                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1486                     orc = CONFREJ;
1487                     break;
1488                 }
1489                 if (!ao->neg_chap) {    /* we don't want to do CHAP */
1490                     orc = CONFNAK;      /* NAK it and suggest PAP */
1491                     PUTCHAR(CI_AUTHTYPE, nakp);
1492                     PUTCHAR(CILEN_SHORT, nakp);
1493                     PUTSHORT(PPP_PAP, nakp);
1494                     break;
1495                 }
1496                 GETCHAR(cichar, p);     /* get digest type*/
1497                 if (cichar != CHAP_DIGEST_MD5
1498 #ifdef CHAPMS
1499                     && cichar != CHAP_MICROSOFT
1500 #endif
1501                     ) {
1502                     orc = CONFNAK;
1503                     PUTCHAR(CI_AUTHTYPE, nakp);
1504                     PUTCHAR(CILEN_CHAP, nakp);
1505                     PUTSHORT(PPP_CHAP, nakp);
1506                     PUTCHAR(ao->chap_mdtype, nakp);
1507                     break;
1508                 }
1509                 ho->chap_mdtype = cichar; /* save md type */
1510                 ho->neg_chap = 1;
1511                 break;
1512             }
1513
1514             /*
1515              * We don't recognize the protocol they're asking for.
1516              * Nak it with something we're willing to do.
1517              * (At this point we know ao->neg_upap || ao->neg_chap.)
1518              */
1519             orc = CONFNAK;
1520             PUTCHAR(CI_AUTHTYPE, nakp);
1521             if (ao->neg_chap) {
1522                 PUTCHAR(CILEN_CHAP, nakp);
1523                 PUTSHORT(PPP_CHAP, nakp);
1524                 PUTCHAR(ao->chap_mdtype, nakp);
1525             } else {
1526                 PUTCHAR(CILEN_SHORT, nakp);
1527                 PUTSHORT(PPP_PAP, nakp);
1528             }
1529             break;
1530
1531         case CI_QUALITY:
1532             if (!ao->neg_lqr ||
1533                 cilen != CILEN_LQR) {
1534                 orc = CONFREJ;
1535                 break;
1536             }
1537
1538             GETSHORT(cishort, p);
1539             GETLONG(cilong, p);
1540
1541             /*
1542              * Check the protocol and the reporting period.
1543              * XXX When should we Nak this, and what with?
1544              */
1545             if (cishort != PPP_LQR) {
1546                 orc = CONFNAK;
1547                 PUTCHAR(CI_QUALITY, nakp);
1548                 PUTCHAR(CILEN_LQR, nakp);
1549                 PUTSHORT(PPP_LQR, nakp);
1550                 PUTLONG(ao->lqr_period, nakp);
1551                 break;
1552             }
1553             break;
1554
1555         case CI_MAGICNUMBER:
1556             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1557                 cilen != CILEN_LONG) {
1558                 orc = CONFREJ;
1559                 break;
1560             }
1561             GETLONG(cilong, p);
1562
1563             /*
1564              * He must have a different magic number.
1565              */
1566             if (go->neg_magicnumber &&
1567                 cilong == go->magicnumber) {
1568                 cilong = magic();       /* Don't put magic() inside macro! */
1569                 orc = CONFNAK;
1570                 PUTCHAR(CI_MAGICNUMBER, nakp);
1571                 PUTCHAR(CILEN_LONG, nakp);
1572                 PUTLONG(cilong, nakp);
1573                 break;
1574             }
1575             ho->neg_magicnumber = 1;
1576             ho->magicnumber = cilong;
1577             break;
1578
1579
1580         case CI_PCOMPRESSION:
1581             if (!ao->neg_pcompression ||
1582                 cilen != CILEN_VOID) {
1583                 orc = CONFREJ;
1584                 break;
1585             }
1586             ho->neg_pcompression = 1;
1587             break;
1588
1589         case CI_ACCOMPRESSION:
1590             if (!ao->neg_accompression ||
1591                 cilen != CILEN_VOID) {
1592                 orc = CONFREJ;
1593                 break;
1594             }
1595             ho->neg_accompression = 1;
1596             break;
1597
1598         case CI_MRRU:
1599             if (!ao->neg_multilink ||
1600                 cilen != CILEN_SHORT) {
1601                 orc = CONFREJ;
1602                 break;
1603             }
1604
1605             GETSHORT(cishort, p);
1606             /* possibly should insist on a minimum/maximum MRRU here */
1607             ho->neg_multilink = 1;
1608             ho->mrru = cishort;
1609             break;
1610
1611         case CI_SSNHF:
1612             if (!ao->neg_ssnhf ||
1613                 cilen != CILEN_VOID) {
1614                 orc = CONFREJ;
1615                 break;
1616             }
1617             ho->neg_ssnhf = 1;
1618             break;
1619
1620         case CI_EPDISC:
1621             if (!ao->neg_endpoint ||
1622                 cilen < CILEN_CHAR ||
1623                 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1624                 orc = CONFREJ;
1625                 break;
1626             }
1627             GETCHAR(cichar, p);
1628             cilen -= CILEN_CHAR;
1629             ho->neg_endpoint = 1;
1630             ho->endp_class = cichar;
1631             ho->endp_len = cilen;
1632             BCOPY(p, ho->endpoint, cilen);
1633             INCPTR(cilen, p);
1634             break;
1635
1636         default:
1637             LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1638             orc = CONFREJ;
1639             break;
1640         }
1641
1642 endswitch:
1643         if (orc == CONFACK &&           /* Good CI */
1644             rc != CONFACK)              /*  but prior CI wasnt? */
1645             continue;                   /* Don't send this one */
1646
1647         if (orc == CONFNAK) {           /* Nak this CI? */
1648             if (reject_if_disagree      /* Getting fed up with sending NAKs? */
1649                 && citype != CI_MAGICNUMBER) {
1650                 orc = CONFREJ;          /* Get tough if so */
1651             } else {
1652                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1653                     continue;           /* Don't send this one */
1654                 rc = CONFNAK;
1655             }
1656         }
1657         if (orc == CONFREJ) {           /* Reject this CI */
1658             rc = CONFREJ;
1659             if (cip != rejp)            /* Need to move rejected CI? */
1660                 BCOPY(cip, rejp, cilen); /* Move it */
1661             INCPTR(cilen, rejp);        /* Update output pointer */
1662         }
1663     }
1664
1665     /*
1666      * If we wanted to send additional NAKs (for unsent CIs), the
1667      * code would go here.  The extra NAKs would go at *nakp.
1668      * At present there are no cases where we want to ask the
1669      * peer to negotiate an option.
1670      */
1671
1672     switch (rc) {
1673     case CONFACK:
1674         *lenp = next - inp;
1675         break;
1676     case CONFNAK:
1677         /*
1678          * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1679          */
1680         *lenp = nakp - nak_buffer;
1681         BCOPY(nak_buffer, inp, *lenp);
1682         break;
1683     case CONFREJ:
1684         *lenp = rejp - inp;
1685         break;
1686     }
1687
1688     LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1689     return (rc);                        /* Return final code */
1690 }
1691
1692
1693 /*
1694  * lcp_up - LCP has come UP.
1695  */
1696 static void
1697 lcp_up(f)
1698     fsm *f;
1699 {
1700     lcp_options *wo = &lcp_wantoptions[f->unit];
1701     lcp_options *ho = &lcp_hisoptions[f->unit];
1702     lcp_options *go = &lcp_gotoptions[f->unit];
1703     lcp_options *ao = &lcp_allowoptions[f->unit];
1704
1705     if (!go->neg_magicnumber)
1706         go->magicnumber = 0;
1707     if (!ho->neg_magicnumber)
1708         ho->magicnumber = 0;
1709
1710     /*
1711      * Set our MTU to the smaller of the MTU we wanted and
1712      * the MRU our peer wanted.  If we negotiated an MRU,
1713      * set our MRU to the larger of value we wanted and
1714      * the value we got in the negotiation.
1715      */
1716     ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1717                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1718                     ho->neg_pcompression, ho->neg_accompression);
1719     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1720                     (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1721                     go->neg_pcompression, go->neg_accompression);
1722
1723     if (ho->neg_mru)
1724         peer_mru[f->unit] = ho->mru;
1725
1726     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
1727
1728     link_established(f->unit);
1729 }
1730
1731
1732 /*
1733  * lcp_down - LCP has gone DOWN.
1734  *
1735  * Alert other protocols.
1736  */
1737 static void
1738 lcp_down(f)
1739     fsm *f;
1740 {
1741     lcp_options *go = &lcp_gotoptions[f->unit];
1742
1743     lcp_echo_lowerdown(f->unit);
1744
1745     link_down(f->unit);
1746
1747     ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1748     ppp_recv_config(f->unit, PPP_MRU,
1749                     (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1750                     go->neg_pcompression, go->neg_accompression);
1751     peer_mru[f->unit] = PPP_MRU;
1752 }
1753
1754
1755 /*
1756  * lcp_starting - LCP needs the lower layer up.
1757  */
1758 static void
1759 lcp_starting(f)
1760     fsm *f;
1761 {
1762     link_required(f->unit);
1763 }
1764
1765
1766 /*
1767  * lcp_finished - LCP has finished with the lower layer.
1768  */
1769 static void
1770 lcp_finished(f)
1771     fsm *f;
1772 {
1773     link_terminated(f->unit);
1774 }
1775
1776
1777 /*
1778  * lcp_printpkt - print the contents of an LCP packet.
1779  */
1780 static char *lcp_codenames[] = {
1781     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1782     "TermReq", "TermAck", "CodeRej", "ProtRej",
1783     "EchoReq", "EchoRep", "DiscReq"
1784 };
1785
1786 static char *endp_class_names[] = {
1787     "null", "local", "IP", "MAC", "magic", "phone"
1788 };
1789
1790 static int
1791 lcp_printpkt(p, plen, printer, arg)
1792     u_char *p;
1793     int plen;
1794     void (*printer) __P((void *, char *, ...));
1795     void *arg;
1796 {
1797     int code, id, len, olen, i;
1798     u_char *pstart, *optend;
1799     u_char cichar;
1800     u_short cishort;
1801     u_int32_t cilong;
1802
1803     if (plen < HEADERLEN)
1804         return 0;
1805     pstart = p;
1806     GETCHAR(code, p);
1807     GETCHAR(id, p);
1808     GETSHORT(len, p);
1809     if (len < HEADERLEN || len > plen)
1810         return 0;
1811
1812     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1813         printer(arg, " %s", lcp_codenames[code-1]);
1814     else
1815         printer(arg, " code=0x%x", code);
1816     printer(arg, " id=0x%x", id);
1817     len -= HEADERLEN;
1818     switch (code) {
1819     case CONFREQ:
1820     case CONFACK:
1821     case CONFNAK:
1822     case CONFREJ:
1823         /* print option list */
1824         while (len >= 2) {
1825             GETCHAR(code, p);
1826             GETCHAR(olen, p);
1827             p -= 2;
1828             if (olen < 2 || olen > len) {
1829                 break;
1830             }
1831             printer(arg, " <");
1832             len -= olen;
1833             optend = p + olen;
1834             switch (code) {
1835             case CI_MRU:
1836                 if (olen == CILEN_SHORT) {
1837                     p += 2;
1838                     GETSHORT(cishort, p);
1839                     printer(arg, "mru %d", cishort);
1840                 }
1841                 break;
1842             case CI_ASYNCMAP:
1843                 if (olen == CILEN_LONG) {
1844                     p += 2;
1845                     GETLONG(cilong, p);
1846                     printer(arg, "asyncmap 0x%x", cilong);
1847                 }
1848                 break;
1849             case CI_AUTHTYPE:
1850                 if (olen >= CILEN_SHORT) {
1851                     p += 2;
1852                     printer(arg, "auth ");
1853                     GETSHORT(cishort, p);
1854                     switch (cishort) {
1855                     case PPP_PAP:
1856                         printer(arg, "pap");
1857                         break;
1858                     case PPP_CHAP:
1859                         printer(arg, "chap");
1860                         if (p < optend) {
1861                             switch (*p) {
1862                             case CHAP_DIGEST_MD5:
1863                                 printer(arg, " MD5");
1864                                 ++p;
1865                                 break;
1866 #ifdef CHAPMS
1867                             case CHAP_MICROSOFT:
1868                                 printer(arg, " m$oft");
1869                                 ++p;
1870                                 break;
1871 #endif
1872                             }
1873                         }
1874                         break;
1875                     default:
1876                         printer(arg, "0x%x", cishort);
1877                     }
1878                 }
1879                 break;
1880             case CI_QUALITY:
1881                 if (olen >= CILEN_SHORT) {
1882                     p += 2;
1883                     printer(arg, "quality ");
1884                     GETSHORT(cishort, p);
1885                     switch (cishort) {
1886                     case PPP_LQR:
1887                         printer(arg, "lqr");
1888                         break;
1889                     default:
1890                         printer(arg, "0x%x", cishort);
1891                     }
1892                 }
1893                 break;
1894             case CI_CALLBACK:
1895                 if (olen >= CILEN_CHAR) {
1896                     p += 2;
1897                     printer(arg, "callback ");
1898                     GETCHAR(cishort, p);
1899                     switch (cishort) {
1900                     case CBCP_OPT:
1901                         printer(arg, "CBCP");
1902                         break;
1903                     default:
1904                         printer(arg, "0x%x", cishort);
1905                     }
1906                 }
1907                 break;
1908             case CI_MAGICNUMBER:
1909                 if (olen == CILEN_LONG) {
1910                     p += 2;
1911                     GETLONG(cilong, p);
1912                     printer(arg, "magic 0x%x", cilong);
1913                 }
1914                 break;
1915             case CI_PCOMPRESSION:
1916                 if (olen == CILEN_VOID) {
1917                     p += 2;
1918                     printer(arg, "pcomp");
1919                 }
1920                 break;
1921             case CI_ACCOMPRESSION:
1922                 if (olen == CILEN_VOID) {
1923                     p += 2;
1924                     printer(arg, "accomp");
1925                 }
1926                 break;
1927             case CI_MRRU:
1928                 if (olen == CILEN_SHORT) {
1929                     p += 2;
1930                     GETSHORT(cishort, p);
1931                     printer(arg, "mrru %d", cishort);
1932                 }
1933                 break;
1934             case CI_SSNHF:
1935                 if (olen == CILEN_VOID) {
1936                     p += 2;
1937                     printer(arg, "ssnhf");
1938                 }
1939                 break;
1940             case CI_EPDISC:
1941                 if (olen >= CILEN_CHAR) {
1942                     p += 2;
1943                     GETCHAR(cichar, p);
1944                     if (cichar <= 5)
1945                         printer(arg, "endpoint [%s]:",
1946                                 endp_class_names[cichar]);
1947                     else
1948                         printer(arg, "endpoint [%d]:");
1949                 }
1950                 break;
1951             }
1952             while (p < optend) {
1953                 GETCHAR(code, p);
1954                 printer(arg, " %.2x", code);
1955             }
1956             printer(arg, ">");
1957         }
1958         break;
1959
1960     case TERMACK:
1961     case TERMREQ:
1962         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1963             printer(arg, " ");
1964             print_string(p, len, printer, arg);
1965             p += len;
1966             len = 0;
1967         }
1968         break;
1969
1970     case ECHOREQ:
1971     case ECHOREP:
1972     case DISCREQ:
1973         if (len >= 4) {
1974             GETLONG(cilong, p);
1975             printer(arg, " magic=0x%x", cilong);
1976             p += 4;
1977             len -= 4;
1978         }
1979         break;
1980     }
1981
1982     /* print the rest of the bytes in the packet */
1983     for (i = 0; i < len && i < 32; ++i) {
1984         GETCHAR(code, p);
1985         printer(arg, " %.2x", code);
1986     }
1987     if (i < len) {
1988         printer(arg, " ...");
1989         p += len - i;
1990     }
1991
1992     return p - pstart;
1993 }
1994
1995 /*
1996  * Time to shut down the link because there is nothing out there.
1997  */
1998
1999 static
2000 void LcpLinkFailure (f)
2001     fsm *f;
2002 {
2003     if (f->state == OPENED) {
2004         info("No response to %d echo-requests", lcp_echos_pending);
2005         notice("Serial link appears to be disconnected.");
2006         lcp_close(f->unit, "Peer not responding");
2007         status = EXIT_PEER_DEAD;
2008     }
2009 }
2010
2011 /*
2012  * Timer expired for the LCP echo requests from this process.
2013  */
2014
2015 static void
2016 LcpEchoCheck (f)
2017     fsm *f;
2018 {
2019     LcpSendEchoRequest (f);
2020     if (f->state != OPENED)
2021         return;
2022
2023     /*
2024      * Start the timer for the next interval.
2025      */
2026     if (lcp_echo_timer_running)
2027         warn("assertion lcp_echo_timer_running==0 failed");
2028     TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2029     lcp_echo_timer_running = 1;
2030 }
2031
2032 /*
2033  * LcpEchoTimeout - Timer expired on the LCP echo
2034  */
2035
2036 static void
2037 LcpEchoTimeout (arg)
2038     void *arg;
2039 {
2040     if (lcp_echo_timer_running != 0) {
2041         lcp_echo_timer_running = 0;
2042         LcpEchoCheck ((fsm *) arg);
2043     }
2044 }
2045
2046 /*
2047  * LcpEchoReply - LCP has received a reply to the echo
2048  */
2049
2050 static void
2051 lcp_received_echo_reply (f, id, inp, len)
2052     fsm *f;
2053     int id;
2054     u_char *inp;
2055     int len;
2056 {
2057     u_int32_t magic;
2058
2059     /* Check the magic number - don't count replies from ourselves. */
2060     if (len < 4) {
2061         dbglog("lcp: received short Echo-Reply, length %d", len);
2062         return;
2063     }
2064     GETLONG(magic, inp);
2065     if (lcp_gotoptions[f->unit].neg_magicnumber
2066         && magic == lcp_gotoptions[f->unit].magicnumber) {
2067         warn("appear to have received our own echo-reply!");
2068         return;
2069     }
2070
2071     /* Reset the number of outstanding echo frames */
2072     lcp_echos_pending = 0;
2073 }
2074
2075 /*
2076  * LcpSendEchoRequest - Send an echo request frame to the peer
2077  */
2078
2079 static void
2080 LcpSendEchoRequest (f)
2081     fsm *f;
2082 {
2083     u_int32_t lcp_magic;
2084     u_char pkt[4], *pktp;
2085
2086     /*
2087      * Detect the failure of the peer at this point.
2088      */
2089     if (lcp_echo_fails != 0) {
2090         if (lcp_echos_pending >= lcp_echo_fails) {
2091             LcpLinkFailure(f);
2092             lcp_echos_pending = 0;
2093         }
2094     }
2095
2096     /*
2097      * Make and send the echo request frame.
2098      */
2099     if (f->state == OPENED) {
2100         lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2101         pktp = pkt;
2102         PUTLONG(lcp_magic, pktp);
2103         fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2104         ++lcp_echos_pending;
2105     }
2106 }
2107
2108 /*
2109  * lcp_echo_lowerup - Start the timer for the LCP frame
2110  */
2111
2112 static void
2113 lcp_echo_lowerup (unit)
2114     int unit;
2115 {
2116     fsm *f = &lcp_fsm[unit];
2117
2118     /* Clear the parameters for generating echo frames */
2119     lcp_echos_pending      = 0;
2120     lcp_echo_number        = 0;
2121     lcp_echo_timer_running = 0;
2122   
2123     /* If a timeout interval is specified then start the timer */
2124     if (lcp_echo_interval != 0)
2125         LcpEchoCheck (f);
2126 }
2127
2128 /*
2129  * lcp_echo_lowerdown - Stop the timer for the LCP frame
2130  */
2131
2132 static void
2133 lcp_echo_lowerdown (unit)
2134     int unit;
2135 {
2136     fsm *f = &lcp_fsm[unit];
2137
2138     if (lcp_echo_timer_running != 0) {
2139         UNTIMEOUT (LcpEchoTimeout, f);
2140         lcp_echo_timer_running = 0;
2141     }
2142 }