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