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