]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
Added EAP support with MD5-Challenge and SRP-SHA1 methods. Tested
[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.63 2002/11/02 19:48:12 carlsonj 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->neg_magicnumber = 1;
331     wo->neg_pcompression = 1;
332     wo->neg_accompression = 1;
333
334     BZERO(ao, sizeof(*ao));
335     ao->neg_mru = 1;
336     ao->mru = MAXMRU;
337     ao->neg_asyncmap = 1;
338     ao->neg_chap = 1;
339     ao->chap_mdtype = MDTYPE_ALL;
340     ao->neg_upap = 1;
341     ao->neg_eap = 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, UPAP, or EAP, even if we will
640      * accept more than one.  We prefer EAP first, then CHAP, then
641      * PAP.
642      */
643     return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
644             LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
645             LENCISHORT(go->neg_eap) +
646             LENCICHAP(!go->neg_eap && go->neg_chap) +
647             LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
648             LENCILQR(go->neg_lqr) +
649             LENCICBCP(go->neg_cbcp) +
650             LENCILONG(go->neg_magicnumber) +
651             LENCIVOID(go->neg_pcompression) +
652             LENCIVOID(go->neg_accompression) +
653             LENCISHORT(go->neg_mrru) +
654             LENCIVOID(go->neg_ssnhf) +
655             (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
656 }
657
658
659 /*
660  * lcp_addci - Add our desired CIs to a packet.
661  */
662 static void
663 lcp_addci(f, ucp, lenp)
664     fsm *f;
665     u_char *ucp;
666     int *lenp;
667 {
668     lcp_options *go = &lcp_gotoptions[f->unit];
669     u_char *start_ucp = ucp;
670
671 #define ADDCIVOID(opt, neg) \
672     if (neg) { \
673         PUTCHAR(opt, ucp); \
674         PUTCHAR(CILEN_VOID, ucp); \
675     }
676 #define ADDCISHORT(opt, neg, val) \
677     if (neg) { \
678         PUTCHAR(opt, ucp); \
679         PUTCHAR(CILEN_SHORT, ucp); \
680         PUTSHORT(val, ucp); \
681     }
682 #define ADDCICHAP(opt, neg, val) \
683     if (neg) { \
684         PUTCHAR((opt), ucp); \
685         PUTCHAR(CILEN_CHAP, ucp); \
686         PUTSHORT(PPP_CHAP, ucp); \
687         PUTCHAR((CHAP_DIGEST(val)), ucp); \
688     }
689 #define ADDCILONG(opt, neg, val) \
690     if (neg) { \
691         PUTCHAR(opt, ucp); \
692         PUTCHAR(CILEN_LONG, ucp); \
693         PUTLONG(val, ucp); \
694     }
695 #define ADDCILQR(opt, neg, val) \
696     if (neg) { \
697         PUTCHAR(opt, ucp); \
698         PUTCHAR(CILEN_LQR, ucp); \
699         PUTSHORT(PPP_LQR, ucp); \
700         PUTLONG(val, ucp); \
701     }
702 #define ADDCICHAR(opt, neg, val) \
703     if (neg) { \
704         PUTCHAR(opt, ucp); \
705         PUTCHAR(CILEN_CHAR, ucp); \
706         PUTCHAR(val, ucp); \
707     }
708 #define ADDCIENDP(opt, neg, class, val, len) \
709     if (neg) { \
710         int i; \
711         PUTCHAR(opt, ucp); \
712         PUTCHAR(CILEN_CHAR + len, ucp); \
713         PUTCHAR(class, ucp); \
714         for (i = 0; i < len; ++i) \
715             PUTCHAR(val[i], ucp); \
716     }
717
718     ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
719     ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
720               go->asyncmap);
721     ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
722     ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
723     ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
724                PPP_PAP);
725     ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
726     ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
727     ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
728     ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
729     ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
730     ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
731     ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
732     ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
733               go->endpoint.value, go->endpoint.length);
734
735     if (ucp - start_ucp != *lenp) {
736         /* this should never happen, because peer_mtu should be 1500 */
737         error("Bug in lcp_addci: wrong length");
738     }
739 }
740
741
742 /*
743  * lcp_ackci - Ack our CIs.
744  * This should not modify any state if the Ack is bad.
745  *
746  * Returns:
747  *      0 - Ack was bad.
748  *      1 - Ack was good.
749  */
750 static int
751 lcp_ackci(f, p, len)
752     fsm *f;
753     u_char *p;
754     int len;
755 {
756     lcp_options *go = &lcp_gotoptions[f->unit];
757     u_char cilen, citype, cichar;
758     u_short cishort;
759     u_int32_t cilong;
760
761     /*
762      * CIs must be in exactly the same order that we sent.
763      * Check packet length and CI length at each step.
764      * If we find any deviations, then this packet is bad.
765      */
766 #define ACKCIVOID(opt, neg) \
767     if (neg) { \
768         if ((len -= CILEN_VOID) < 0) \
769             goto bad; \
770         GETCHAR(citype, p); \
771         GETCHAR(cilen, p); \
772         if (cilen != CILEN_VOID || \
773             citype != opt) \
774             goto bad; \
775     }
776 #define ACKCISHORT(opt, neg, val) \
777     if (neg) { \
778         if ((len -= CILEN_SHORT) < 0) \
779             goto bad; \
780         GETCHAR(citype, p); \
781         GETCHAR(cilen, p); \
782         if (cilen != CILEN_SHORT || \
783             citype != opt) \
784             goto bad; \
785         GETSHORT(cishort, p); \
786         if (cishort != val) \
787             goto bad; \
788     }
789 #define ACKCICHAR(opt, neg, val) \
790     if (neg) { \
791         if ((len -= CILEN_CHAR) < 0) \
792             goto bad; \
793         GETCHAR(citype, p); \
794         GETCHAR(cilen, p); \
795         if (cilen != CILEN_CHAR || \
796             citype != opt) \
797             goto bad; \
798         GETCHAR(cichar, p); \
799         if (cichar != val) \
800             goto bad; \
801     }
802 #define ACKCICHAP(opt, neg, val) \
803     if (neg) { \
804         if ((len -= CILEN_CHAP) < 0) \
805             goto bad; \
806         GETCHAR(citype, p); \
807         GETCHAR(cilen, p); \
808         if (cilen != CILEN_CHAP || \
809             citype != (opt)) \
810             goto bad; \
811         GETSHORT(cishort, p); \
812         if (cishort != PPP_CHAP) \
813             goto bad; \
814         GETCHAR(cichar, p); \
815         if (cichar != (CHAP_DIGEST(val))) \
816           goto bad; \
817     }
818 #define ACKCILONG(opt, neg, val) \
819     if (neg) { \
820         if ((len -= CILEN_LONG) < 0) \
821             goto bad; \
822         GETCHAR(citype, p); \
823         GETCHAR(cilen, p); \
824         if (cilen != CILEN_LONG || \
825             citype != opt) \
826             goto bad; \
827         GETLONG(cilong, p); \
828         if (cilong != val) \
829             goto bad; \
830     }
831 #define ACKCILQR(opt, neg, val) \
832     if (neg) { \
833         if ((len -= CILEN_LQR) < 0) \
834             goto bad; \
835         GETCHAR(citype, p); \
836         GETCHAR(cilen, p); \
837         if (cilen != CILEN_LQR || \
838             citype != opt) \
839             goto bad; \
840         GETSHORT(cishort, p); \
841         if (cishort != PPP_LQR) \
842             goto bad; \
843         GETLONG(cilong, p); \
844         if (cilong != val) \
845           goto bad; \
846     }
847 #define ACKCIENDP(opt, neg, class, val, vlen) \
848     if (neg) { \
849         int i; \
850         if ((len -= CILEN_CHAR + vlen) < 0) \
851             goto bad; \
852         GETCHAR(citype, p); \
853         GETCHAR(cilen, p); \
854         if (cilen != CILEN_CHAR + vlen || \
855             citype != opt) \
856             goto bad; \
857         GETCHAR(cichar, p); \
858         if (cichar != class) \
859             goto bad; \
860         for (i = 0; i < vlen; ++i) { \
861             GETCHAR(cichar, p); \
862             if (cichar != val[i]) \
863                 goto bad; \
864         } \
865     }
866
867     ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
868     ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
869               go->asyncmap);
870     ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
871     ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
872     ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
873                PPP_PAP);
874     ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
875     ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
876     ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
877     ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
878     ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
879     ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
880     ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
881     ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
882               go->endpoint.value, go->endpoint.length);
883
884     /*
885      * If there are any remaining CIs, then this packet is bad.
886      */
887     if (len != 0)
888         goto bad;
889     return (1);
890 bad:
891     LCPDEBUG(("lcp_acki: received bad Ack!"));
892     return (0);
893 }
894
895
896 /*
897  * lcp_nakci - Peer has sent a NAK for some of our CIs.
898  * This should not modify any state if the Nak is bad
899  * or if LCP is in the OPENED state.
900  *
901  * Returns:
902  *      0 - Nak was bad.
903  *      1 - Nak was good.
904  */
905 static int
906 lcp_nakci(f, p, len)
907     fsm *f;
908     u_char *p;
909     int len;
910 {
911     lcp_options *go = &lcp_gotoptions[f->unit];
912     lcp_options *wo = &lcp_wantoptions[f->unit];
913     u_char citype, cichar, *next;
914     u_short cishort;
915     u_int32_t cilong;
916     lcp_options no;             /* options we've seen Naks for */
917     lcp_options try;            /* options to request next time */
918     int looped_back = 0;
919     int cilen;
920
921     BZERO(&no, sizeof(no));
922     try = *go;
923
924     /*
925      * Any Nak'd CIs must be in exactly the same order that we sent.
926      * Check packet length and CI length at each step.
927      * If we find any deviations, then this packet is bad.
928      */
929 #define NAKCIVOID(opt, neg) \
930     if (go->neg && \
931         len >= CILEN_VOID && \
932         p[1] == CILEN_VOID && \
933         p[0] == opt) { \
934         len -= CILEN_VOID; \
935         INCPTR(CILEN_VOID, p); \
936         no.neg = 1; \
937         try.neg = 0; \
938     }
939 #define NAKCICHAP(opt, neg, code) \
940     if (go->neg && \
941         len >= CILEN_CHAP && \
942         p[1] == CILEN_CHAP && \
943         p[0] == opt) { \
944         len -= CILEN_CHAP; \
945         INCPTR(2, p); \
946         GETSHORT(cishort, p); \
947         GETCHAR(cichar, p); \
948         no.neg = 1; \
949         code \
950     }
951 #define NAKCICHAR(opt, neg, code) \
952     if (go->neg && \
953         len >= CILEN_CHAR && \
954         p[1] == CILEN_CHAR && \
955         p[0] == opt) { \
956         len -= CILEN_CHAR; \
957         INCPTR(2, p); \
958         GETCHAR(cichar, p); \
959         no.neg = 1; \
960         code \
961     }
962 #define NAKCISHORT(opt, neg, code) \
963     if (go->neg && \
964         len >= CILEN_SHORT && \
965         p[1] == CILEN_SHORT && \
966         p[0] == opt) { \
967         len -= CILEN_SHORT; \
968         INCPTR(2, p); \
969         GETSHORT(cishort, p); \
970         no.neg = 1; \
971         code \
972     }
973 #define NAKCILONG(opt, neg, code) \
974     if (go->neg && \
975         len >= CILEN_LONG && \
976         p[1] == CILEN_LONG && \
977         p[0] == opt) { \
978         len -= CILEN_LONG; \
979         INCPTR(2, p); \
980         GETLONG(cilong, p); \
981         no.neg = 1; \
982         code \
983     }
984 #define NAKCILQR(opt, neg, code) \
985     if (go->neg && \
986         len >= CILEN_LQR && \
987         p[1] == CILEN_LQR && \
988         p[0] == opt) { \
989         len -= CILEN_LQR; \
990         INCPTR(2, p); \
991         GETSHORT(cishort, p); \
992         GETLONG(cilong, p); \
993         no.neg = 1; \
994         code \
995     }
996 #define NAKCIENDP(opt, neg) \
997     if (go->neg && \
998         len >= CILEN_CHAR && \
999         p[0] == opt && \
1000         p[1] >= CILEN_CHAR && \
1001         p[1] <= len) { \
1002         len -= p[1]; \
1003         INCPTR(p[1], p); \
1004         no.neg = 1; \
1005         try.neg = 0; \
1006     }
1007
1008     /*
1009      * We don't care if they want to send us smaller packets than
1010      * we want.  Therefore, accept any MRU less than what we asked for,
1011      * but then ignore the new value when setting the MRU in the kernel.
1012      * If they send us a bigger MRU than what we asked, accept it, up to
1013      * the limit of the default MRU we'd get if we didn't negotiate.
1014      */
1015     if (go->neg_mru && go->mru != DEFMRU) {
1016         NAKCISHORT(CI_MRU, neg_mru,
1017                    if (cishort <= wo->mru || cishort <= DEFMRU)
1018                        try.mru = cishort;
1019                    );
1020     }
1021
1022     /*
1023      * Add any characters they want to our (receive-side) asyncmap.
1024      */
1025     if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1026         NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1027                   try.asyncmap = go->asyncmap | cilong;
1028                   );
1029     }
1030
1031     /*
1032      * If they've nak'd our authentication-protocol, check whether
1033      * they are proposing a different protocol, or a different
1034      * hash algorithm for CHAP.
1035      */
1036     if ((go->neg_chap || go->neg_upap || go->neg_eap)
1037         && len >= CILEN_SHORT
1038         && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1039         cilen = p[1];
1040         len -= cilen;
1041         no.neg_chap = go->neg_chap;
1042         no.neg_upap = go->neg_upap;
1043         no.neg_eap = go->neg_eap;
1044         INCPTR(2, p);
1045         GETSHORT(cishort, p);
1046         if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1047             /* If we were asking for EAP, then we need to stop that. */
1048             if (go->neg_eap)
1049                 try.neg_eap = 0;
1050
1051             /* If we were asking for CHAP, then we need to stop that. */
1052             else if (go->neg_chap)
1053                 try.neg_chap = 0;
1054             /*
1055              * If we weren't asking for CHAP or EAP, then we were asking for
1056              * PAP, in which case this Nak is bad.
1057              */
1058             else
1059                 goto bad;
1060
1061         } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1062             GETCHAR(cichar, p);
1063             /* Stop asking for EAP, if we were. */
1064             if (go->neg_eap) {
1065                 try.neg_eap = 0;
1066                 /* Try to set up to use their suggestion, if possible */
1067                 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1068                     go->chap_mdtype = CHAP_MDTYPE_D(cichar);
1069             } else if (go->neg_chap) {
1070                 /*
1071                  * We were asking for our preferred algorithm, they must
1072                  * want something different.
1073                  */
1074                 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1075                     if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1076                         /* Use their suggestion if we support it ... */
1077                         go->chap_mdtype = CHAP_MDTYPE_D(cichar);
1078                     } else {
1079                         /* ... otherwise, try our next-preferred algorithm. */
1080                         try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1081                         if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1082                             try.neg_chap = 0;
1083                     }
1084                 } else {
1085                     /*
1086                      * Whoops, they Nak'd our algorithm of choice
1087                      * but then suggested it back to us.
1088                      */
1089                     goto bad;
1090                 }
1091             } else {
1092                 /*
1093                  * Stop asking for PAP if we were asking for it.
1094                  */
1095                 try.neg_upap = 0;
1096             }
1097
1098         } else {
1099
1100             /*
1101              * If we were asking for EAP, and they're Conf-Naking EAP,
1102              * well, that's just strange.  Nobody should do that.
1103              */
1104             if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1105                 dbglog("Unexpected Conf-Nak for EAP");
1106
1107             /*
1108              * We don't recognize what they're suggesting.
1109              * Stop asking for what we were asking for.
1110              */
1111             if (go->neg_eap)
1112                 try.neg_eap = 0;
1113             else if (go->neg_chap)
1114                 try.neg_chap = 0;
1115             else
1116                 try.neg_upap = 0;
1117             p += cilen - CILEN_SHORT;
1118         }
1119     }
1120
1121     /*
1122      * If they can't cope with our link quality protocol, we'll have
1123      * to stop asking for LQR.  We haven't got any other protocol.
1124      * If they Nak the reporting period, take their value XXX ?
1125      */
1126     NAKCILQR(CI_QUALITY, neg_lqr,
1127              if (cishort != PPP_LQR)
1128                  try.neg_lqr = 0;
1129              else
1130                  try.lqr_period = cilong;
1131              );
1132
1133     /*
1134      * Only implementing CBCP...not the rest of the callback options
1135      */
1136     NAKCICHAR(CI_CALLBACK, neg_cbcp,
1137               try.neg_cbcp = 0;
1138               );
1139
1140     /*
1141      * Check for a looped-back line.
1142      */
1143     NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1144               try.magicnumber = magic();
1145               looped_back = 1;
1146               );
1147
1148     /*
1149      * Peer shouldn't send Nak for protocol compression or
1150      * address/control compression requests; they should send
1151      * a Reject instead.  If they send a Nak, treat it as a Reject.
1152      */
1153     NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1154     NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1155
1156     /*
1157      * Nak for MRRU option - accept their value if it is smaller
1158      * than the one we want.
1159      */
1160     if (go->neg_mrru) {
1161         NAKCISHORT(CI_MRRU, neg_mrru,
1162                    if (cishort <= wo->mrru)
1163                        try.mrru = cishort;
1164                    );
1165     }
1166
1167     /*
1168      * Nak for short sequence numbers shouldn't be sent, treat it
1169      * like a reject.
1170      */
1171     NAKCIVOID(CI_SSNHF, neg_ssnhf);
1172
1173     /*
1174      * Nak of the endpoint discriminator option is not permitted,
1175      * treat it like a reject.
1176      */
1177     NAKCIENDP(CI_EPDISC, neg_endpoint);
1178
1179     /*
1180      * There may be remaining CIs, if the peer is requesting negotiation
1181      * on an option that we didn't include in our request packet.
1182      * If we see an option that we requested, or one we've already seen
1183      * in this packet, then this packet is bad.
1184      * If we wanted to respond by starting to negotiate on the requested
1185      * option(s), we could, but we don't, because except for the
1186      * authentication type and quality protocol, if we are not negotiating
1187      * an option, it is because we were told not to.
1188      * For the authentication type, the Nak from the peer means
1189      * `let me authenticate myself with you' which is a bit pointless.
1190      * For the quality protocol, the Nak means `ask me to send you quality
1191      * reports', but if we didn't ask for them, we don't want them.
1192      * An option we don't recognize represents the peer asking to
1193      * negotiate some option we don't support, so ignore it.
1194      */
1195     while (len > CILEN_VOID) {
1196         GETCHAR(citype, p);
1197         GETCHAR(cilen, p);
1198         if (cilen < CILEN_VOID || (len -= cilen) < 0)
1199             goto bad;
1200         next = p + cilen - 2;
1201
1202         switch (citype) {
1203         case CI_MRU:
1204             if ((go->neg_mru && go->mru != DEFMRU)
1205                 || no.neg_mru || cilen != CILEN_SHORT)
1206                 goto bad;
1207             GETSHORT(cishort, p);
1208             if (cishort < DEFMRU) {
1209                 try.neg_mru = 1;
1210                 try.mru = cishort;
1211             }
1212             break;
1213         case CI_ASYNCMAP:
1214             if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1215                 || no.neg_asyncmap || cilen != CILEN_LONG)
1216                 goto bad;
1217             break;
1218         case CI_AUTHTYPE:
1219             if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1220                 go->neg_eap || no.neg_eap)
1221                 goto bad;
1222             break;
1223         case CI_MAGICNUMBER:
1224             if (go->neg_magicnumber || no.neg_magicnumber ||
1225                 cilen != CILEN_LONG)
1226                 goto bad;
1227             break;
1228         case CI_PCOMPRESSION:
1229             if (go->neg_pcompression || no.neg_pcompression
1230                 || cilen != CILEN_VOID)
1231                 goto bad;
1232             break;
1233         case CI_ACCOMPRESSION:
1234             if (go->neg_accompression || no.neg_accompression
1235                 || cilen != CILEN_VOID)
1236                 goto bad;
1237             break;
1238         case CI_QUALITY:
1239             if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1240                 goto bad;
1241             break;
1242         case CI_MRRU:
1243             if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1244                 goto bad;
1245             break;
1246         case CI_SSNHF:
1247             if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1248                 goto bad;
1249             try.neg_ssnhf = 1;
1250             break;
1251         case CI_EPDISC:
1252             if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1253                 goto bad;
1254             break;
1255         }
1256         p = next;
1257     }
1258
1259     /*
1260      * OK, the Nak is good.  Now we can update state.
1261      * If there are any options left we ignore them.
1262      */
1263     if (f->state != OPENED) {
1264         if (looped_back) {
1265             if (++try.numloops >= lcp_loopbackfail) {
1266                 notice("Serial line is looped back.");
1267                 lcp_close(f->unit, "Loopback detected");
1268                 status = EXIT_LOOPBACK;
1269             }
1270         } else
1271             try.numloops = 0;
1272         *go = try;
1273     }
1274
1275     return 1;
1276
1277 bad:
1278     LCPDEBUG(("lcp_nakci: received bad Nak!"));
1279     return 0;
1280 }
1281
1282
1283 /*
1284  * lcp_rejci - Peer has Rejected some of our CIs.
1285  * This should not modify any state if the Reject is bad
1286  * or if LCP is in the OPENED state.
1287  *
1288  * Returns:
1289  *      0 - Reject was bad.
1290  *      1 - Reject was good.
1291  */
1292 static int
1293 lcp_rejci(f, p, len)
1294     fsm *f;
1295     u_char *p;
1296     int len;
1297 {
1298     lcp_options *go = &lcp_gotoptions[f->unit];
1299     u_char cichar;
1300     u_short cishort;
1301     u_int32_t cilong;
1302     lcp_options try;            /* options to request next time */
1303
1304     try = *go;
1305
1306     /*
1307      * Any Rejected CIs must be in exactly the same order that we sent.
1308      * Check packet length and CI length at each step.
1309      * If we find any deviations, then this packet is bad.
1310      */
1311 #define REJCIVOID(opt, neg) \
1312     if (go->neg && \
1313         len >= CILEN_VOID && \
1314         p[1] == CILEN_VOID && \
1315         p[0] == opt) { \
1316         len -= CILEN_VOID; \
1317         INCPTR(CILEN_VOID, p); \
1318         try.neg = 0; \
1319     }
1320 #define REJCISHORT(opt, neg, val) \
1321     if (go->neg && \
1322         len >= CILEN_SHORT && \
1323         p[1] == CILEN_SHORT && \
1324         p[0] == opt) { \
1325         len -= CILEN_SHORT; \
1326         INCPTR(2, p); \
1327         GETSHORT(cishort, p); \
1328         /* Check rejected value. */ \
1329         if (cishort != val) \
1330             goto bad; \
1331         try.neg = 0; \
1332     }
1333 #define REJCICHAP(opt, neg, val) \
1334     if (go->neg && \
1335         len >= CILEN_CHAP && \
1336         p[1] == CILEN_CHAP && \
1337         p[0] == opt) { \
1338         len -= CILEN_CHAP; \
1339         INCPTR(2, p); \
1340         GETSHORT(cishort, p); \
1341         GETCHAR(cichar, p); \
1342         /* Check rejected value. */ \
1343         if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1344             goto bad; \
1345         try.neg = 0; \
1346         try.neg_eap = try.neg_upap = 0; \
1347     }
1348 #define REJCILONG(opt, neg, val) \
1349     if (go->neg && \
1350         len >= CILEN_LONG && \
1351         p[1] == CILEN_LONG && \
1352         p[0] == opt) { \
1353         len -= CILEN_LONG; \
1354         INCPTR(2, p); \
1355         GETLONG(cilong, p); \
1356         /* Check rejected value. */ \
1357         if (cilong != val) \
1358             goto bad; \
1359         try.neg = 0; \
1360     }
1361 #define REJCILQR(opt, neg, val) \
1362     if (go->neg && \
1363         len >= CILEN_LQR && \
1364         p[1] == CILEN_LQR && \
1365         p[0] == opt) { \
1366         len -= CILEN_LQR; \
1367         INCPTR(2, p); \
1368         GETSHORT(cishort, p); \
1369         GETLONG(cilong, p); \
1370         /* Check rejected value. */ \
1371         if (cishort != PPP_LQR || cilong != val) \
1372             goto bad; \
1373         try.neg = 0; \
1374     }
1375 #define REJCICBCP(opt, neg, val) \
1376     if (go->neg && \
1377         len >= CILEN_CBCP && \
1378         p[1] == CILEN_CBCP && \
1379         p[0] == opt) { \
1380         len -= CILEN_CBCP; \
1381         INCPTR(2, p); \
1382         GETCHAR(cichar, p); \
1383         /* Check rejected value. */ \
1384         if (cichar != val) \
1385             goto bad; \
1386         try.neg = 0; \
1387     }
1388 #define REJCIENDP(opt, neg, class, val, vlen) \
1389     if (go->neg && \
1390         len >= CILEN_CHAR + vlen && \
1391         p[0] == opt && \
1392         p[1] == CILEN_CHAR + vlen) { \
1393         int i; \
1394         len -= CILEN_CHAR + vlen; \
1395         INCPTR(2, p); \
1396         GETCHAR(cichar, p); \
1397         if (cichar != class) \
1398             goto bad; \
1399         for (i = 0; i < vlen; ++i) { \
1400             GETCHAR(cichar, p); \
1401             if (cichar != val[i]) \
1402                 goto bad; \
1403         } \
1404         try.neg = 0; \
1405     }
1406
1407     REJCISHORT(CI_MRU, neg_mru, go->mru);
1408     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1409     REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1410     if (!go->neg_eap) {
1411         REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1412         if (!go->neg_chap) {
1413             REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1414         }
1415     }
1416     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1417     REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1418     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1419     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1420     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1421     REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1422     REJCIVOID(CI_SSNHF, neg_ssnhf);
1423     REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1424               go->endpoint.value, go->endpoint.length);
1425
1426     /*
1427      * If there are any remaining CIs, then this packet is bad.
1428      */
1429     if (len != 0)
1430         goto bad;
1431     /*
1432      * Now we can update state.
1433      */
1434     if (f->state != OPENED)
1435         *go = try;
1436     return 1;
1437
1438 bad:
1439     LCPDEBUG(("lcp_rejci: received bad Reject!"));
1440     return 0;
1441 }
1442
1443
1444 /*
1445  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1446  *
1447  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1448  * appropriately.  If reject_if_disagree is non-zero, doesn't return
1449  * CONFNAK; returns CONFREJ if it can't return CONFACK.
1450  */
1451 static int
1452 lcp_reqci(f, inp, lenp, reject_if_disagree)
1453     fsm *f;
1454     u_char *inp;                /* Requested CIs */
1455     int *lenp;                  /* Length of requested CIs */
1456     int reject_if_disagree;
1457 {
1458     lcp_options *go = &lcp_gotoptions[f->unit];
1459     lcp_options *ho = &lcp_hisoptions[f->unit];
1460     lcp_options *ao = &lcp_allowoptions[f->unit];
1461     u_char *cip, *next;         /* Pointer to current and next CIs */
1462     int cilen, citype, cichar;  /* Parsed len, type, char value */
1463     u_short cishort;            /* Parsed short value */
1464     u_int32_t cilong;           /* Parse long value */
1465     int rc = CONFACK;           /* Final packet return code */
1466     int orc;                    /* Individual option return code */
1467     u_char *p;                  /* Pointer to next char to parse */
1468     u_char *rejp;               /* Pointer to next char in reject frame */
1469     u_char *nakp;               /* Pointer to next char in Nak frame */
1470     int l = *lenp;              /* Length left */
1471
1472     /*
1473      * Reset all his options.
1474      */
1475     BZERO(ho, sizeof(*ho));
1476
1477     /*
1478      * Process all his options.
1479      */
1480     next = inp;
1481     nakp = nak_buffer;
1482     rejp = inp;
1483     while (l) {
1484         orc = CONFACK;                  /* Assume success */
1485         cip = p = next;                 /* Remember begining of CI */
1486         if (l < 2 ||                    /* Not enough data for CI header or */
1487             p[1] < 2 ||                 /*  CI length too small or */
1488             p[1] > l) {                 /*  CI length too big? */
1489             LCPDEBUG(("lcp_reqci: bad CI length!"));
1490             orc = CONFREJ;              /* Reject bad CI */
1491             cilen = l;                  /* Reject till end of packet */
1492             l = 0;                      /* Don't loop again */
1493             citype = 0;
1494             goto endswitch;
1495         }
1496         GETCHAR(citype, p);             /* Parse CI type */
1497         GETCHAR(cilen, p);              /* Parse CI length */
1498         l -= cilen;                     /* Adjust remaining length */
1499         next += cilen;                  /* Step to next CI */
1500
1501         switch (citype) {               /* Check CI type */
1502         case CI_MRU:
1503             if (!ao->neg_mru ||         /* Allow option? */
1504                 cilen != CILEN_SHORT) { /* Check CI length */
1505                 orc = CONFREJ;          /* Reject CI */
1506                 break;
1507             }
1508             GETSHORT(cishort, p);       /* Parse MRU */
1509
1510             /*
1511              * He must be able to receive at least our minimum.
1512              * No need to check a maximum.  If he sends a large number,
1513              * we'll just ignore it.
1514              */
1515             if (cishort < MINMRU) {
1516                 orc = CONFNAK;          /* Nak CI */
1517                 PUTCHAR(CI_MRU, nakp);
1518                 PUTCHAR(CILEN_SHORT, nakp);
1519                 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1520                 break;
1521             }
1522             ho->neg_mru = 1;            /* Remember he sent MRU */
1523             ho->mru = cishort;          /* And remember value */
1524             break;
1525
1526         case CI_ASYNCMAP:
1527             if (!ao->neg_asyncmap ||
1528                 cilen != CILEN_LONG) {
1529                 orc = CONFREJ;
1530                 break;
1531             }
1532             GETLONG(cilong, p);
1533
1534             /*
1535              * Asyncmap must have set at least the bits
1536              * which are set in lcp_allowoptions[unit].asyncmap.
1537              */
1538             if ((ao->asyncmap & ~cilong) != 0) {
1539                 orc = CONFNAK;
1540                 PUTCHAR(CI_ASYNCMAP, nakp);
1541                 PUTCHAR(CILEN_LONG, nakp);
1542                 PUTLONG(ao->asyncmap | cilong, nakp);
1543                 break;
1544             }
1545             ho->neg_asyncmap = 1;
1546             ho->asyncmap = cilong;
1547             break;
1548
1549         case CI_AUTHTYPE:
1550             if (cilen < CILEN_SHORT ||
1551                 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1552                 /*
1553                  * Reject the option if we're not willing to authenticate.
1554                  */
1555                 dbglog("No auth is possible");
1556                 orc = CONFREJ;
1557                 break;
1558             }
1559             GETSHORT(cishort, p);
1560
1561             /*
1562              * Authtype must be PAP, CHAP, or EAP.
1563              *
1564              * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1565              * ao->neg_eap are set, and the peer sends a Configure-Request
1566              * with two or more authenticate-protocol requests, then we will
1567              * reject the second request.
1568              * Whether we end up doing CHAP, UPAP, or EAP depends then on
1569              * the ordering of the CIs in the peer's Configure-Request.
1570              */
1571
1572             if (cishort == PPP_PAP) {
1573                 /* we've already accepted CHAP or EAP */
1574                 if (ho->neg_chap || ho->neg_eap ||
1575                     cilen != CILEN_SHORT) {
1576                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1577                     orc = CONFREJ;
1578                     break;
1579                 }
1580                 if (!ao->neg_upap) {    /* we don't want to do PAP */
1581                     orc = CONFNAK;      /* NAK it and suggest CHAP or EAP */
1582                     if (ao->neg_eap) {
1583                         PUTCHAR(CILEN_SHORT, nakp);
1584                         PUTSHORT(PPP_EAP, nakp);
1585                     } else {
1586                         PUTCHAR(CI_AUTHTYPE, nakp);
1587                         PUTCHAR(CILEN_CHAP, nakp);
1588                         PUTSHORT(PPP_CHAP, nakp);
1589                         PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1590                     }
1591                     break;
1592                 }
1593                 ho->neg_upap = 1;
1594                 break;
1595             }
1596             if (cishort == PPP_CHAP) {
1597                 /* we've already accepted PAP or EAP */
1598                 if (ho->neg_upap || ho->neg_eap ||
1599                     cilen != CILEN_CHAP) {
1600                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1601                     orc = CONFREJ;
1602                     break;
1603                 }
1604                 if (!ao->neg_chap) {    /* we don't want to do CHAP */
1605                     orc = CONFNAK;      /* NAK it and suggest EAP or PAP */
1606                     PUTCHAR(CI_AUTHTYPE, nakp);
1607                     PUTCHAR(CILEN_SHORT, nakp);
1608                     if (ao->neg_eap) {
1609                         PUTSHORT(PPP_EAP, nakp);
1610                     } else {
1611                         PUTSHORT(PPP_PAP, nakp);
1612                     }
1613                     break;
1614                 }
1615                 GETCHAR(cichar, p);     /* get digest type */
1616                 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1617                     /*
1618                      * We can't/won't do the requested type,
1619                      * suggest something else.
1620                      */
1621                     orc = CONFNAK;
1622                     PUTCHAR(CI_AUTHTYPE, nakp);
1623                     PUTCHAR(CILEN_CHAP, nakp);
1624                     PUTSHORT(PPP_CHAP, nakp);
1625                     PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1626                     break;
1627                 }
1628                 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1629                 ho->neg_chap = 1;
1630                 break;
1631             }
1632             if (cishort == PPP_EAP) {
1633                 /* we've already accepted CHAP or PAP */
1634                 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1635                     LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1636                     orc = CONFREJ;
1637                     break;
1638                 }
1639                 if (!ao->neg_eap) {     /* we don't want to do EAP */
1640                     orc = CONFNAK;      /* NAK it and suggest CHAP or PAP */
1641                     PUTCHAR(CI_AUTHTYPE, nakp);
1642                     if (ao->neg_chap) {
1643                         PUTCHAR(CILEN_CHAP, nakp);
1644                         PUTSHORT(PPP_CHAP, nakp);
1645                         PUTCHAR(ao->chap_mdtype, nakp);
1646                     } else {
1647                         PUTCHAR(CILEN_SHORT, nakp);
1648                         PUTSHORT(PPP_PAP, nakp);
1649                     }
1650                     break;
1651                 }
1652                 ho->neg_eap = 1;
1653                 break;
1654             }
1655
1656             /*
1657              * We don't recognize the protocol they're asking for.
1658              * Nak it with something we're willing to do.
1659              * (At this point we know ao->neg_upap || ao->neg_chap ||
1660              * ao->neg_eap.)
1661              */
1662             orc = CONFNAK;
1663             PUTCHAR(CI_AUTHTYPE, nakp);
1664             if (ao->neg_eap) {
1665                 PUTCHAR(CILEN_SHORT, nakp);
1666                 PUTSHORT(PPP_EAP, nakp);
1667             } else if (ao->neg_chap) {
1668                 PUTCHAR(CILEN_CHAP, nakp);
1669                 PUTSHORT(PPP_CHAP, nakp);
1670                 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1671             } else {
1672                 PUTCHAR(CILEN_SHORT, nakp);
1673                 PUTSHORT(PPP_PAP, nakp);
1674             }
1675             break;
1676
1677         case CI_QUALITY:
1678             if (!ao->neg_lqr ||
1679                 cilen != CILEN_LQR) {
1680                 orc = CONFREJ;
1681                 break;
1682             }
1683
1684             GETSHORT(cishort, p);
1685             GETLONG(cilong, p);
1686
1687             /*
1688              * Check the protocol and the reporting period.
1689              * XXX When should we Nak this, and what with?
1690              */
1691             if (cishort != PPP_LQR) {
1692                 orc = CONFNAK;
1693                 PUTCHAR(CI_QUALITY, nakp);
1694                 PUTCHAR(CILEN_LQR, nakp);
1695                 PUTSHORT(PPP_LQR, nakp);
1696                 PUTLONG(ao->lqr_period, nakp);
1697                 break;
1698             }
1699             break;
1700
1701         case CI_MAGICNUMBER:
1702             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1703                 cilen != CILEN_LONG) {
1704                 orc = CONFREJ;
1705                 break;
1706             }
1707             GETLONG(cilong, p);
1708
1709             /*
1710              * He must have a different magic number.
1711              */
1712             if (go->neg_magicnumber &&
1713                 cilong == go->magicnumber) {
1714                 cilong = magic();       /* Don't put magic() inside macro! */
1715                 orc = CONFNAK;
1716                 PUTCHAR(CI_MAGICNUMBER, nakp);
1717                 PUTCHAR(CILEN_LONG, nakp);
1718                 PUTLONG(cilong, nakp);
1719                 break;
1720             }
1721             ho->neg_magicnumber = 1;
1722             ho->magicnumber = cilong;
1723             break;
1724
1725
1726         case CI_PCOMPRESSION:
1727             if (!ao->neg_pcompression ||
1728                 cilen != CILEN_VOID) {
1729                 orc = CONFREJ;
1730                 break;
1731             }
1732             ho->neg_pcompression = 1;
1733             break;
1734
1735         case CI_ACCOMPRESSION:
1736             if (!ao->neg_accompression ||
1737                 cilen != CILEN_VOID) {
1738                 orc = CONFREJ;
1739                 break;
1740             }
1741             ho->neg_accompression = 1;
1742             break;
1743
1744         case CI_MRRU:
1745             if (!ao->neg_mrru || !multilink ||
1746                 cilen != CILEN_SHORT) {
1747                 orc = CONFREJ;
1748                 break;
1749             }
1750
1751             GETSHORT(cishort, p);
1752             /* possibly should insist on a minimum/maximum MRRU here */
1753             ho->neg_mrru = 1;
1754             ho->mrru = cishort;
1755             break;
1756
1757         case CI_SSNHF:
1758             if (!ao->neg_ssnhf || !multilink ||
1759                 cilen != CILEN_VOID) {
1760                 orc = CONFREJ;
1761                 break;
1762             }
1763             ho->neg_ssnhf = 1;
1764             break;
1765
1766         case CI_EPDISC:
1767             if (!ao->neg_endpoint ||
1768                 cilen < CILEN_CHAR ||
1769                 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1770                 orc = CONFREJ;
1771                 break;
1772             }
1773             GETCHAR(cichar, p);
1774             cilen -= CILEN_CHAR;
1775             ho->neg_endpoint = 1;
1776             ho->endpoint.class = cichar;
1777             ho->endpoint.length = cilen;
1778             BCOPY(p, ho->endpoint.value, cilen);
1779             INCPTR(cilen, p);
1780             break;
1781
1782         default:
1783             LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1784             orc = CONFREJ;
1785             break;
1786         }
1787
1788 endswitch:
1789         if (orc == CONFACK &&           /* Good CI */
1790             rc != CONFACK)              /*  but prior CI wasnt? */
1791             continue;                   /* Don't send this one */
1792
1793         if (orc == CONFNAK) {           /* Nak this CI? */
1794             if (reject_if_disagree      /* Getting fed up with sending NAKs? */
1795                 && citype != CI_MAGICNUMBER) {
1796                 orc = CONFREJ;          /* Get tough if so */
1797             } else {
1798                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1799                     continue;           /* Don't send this one */
1800                 rc = CONFNAK;
1801             }
1802         }
1803         if (orc == CONFREJ) {           /* Reject this CI */
1804             rc = CONFREJ;
1805             if (cip != rejp)            /* Need to move rejected CI? */
1806                 BCOPY(cip, rejp, cilen); /* Move it */
1807             INCPTR(cilen, rejp);        /* Update output pointer */
1808         }
1809     }
1810
1811     /*
1812      * If we wanted to send additional NAKs (for unsent CIs), the
1813      * code would go here.  The extra NAKs would go at *nakp.
1814      * At present there are no cases where we want to ask the
1815      * peer to negotiate an option.
1816      */
1817
1818     switch (rc) {
1819     case CONFACK:
1820         *lenp = next - inp;
1821         break;
1822     case CONFNAK:
1823         /*
1824          * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1825          */
1826         *lenp = nakp - nak_buffer;
1827         BCOPY(nak_buffer, inp, *lenp);
1828         break;
1829     case CONFREJ:
1830         *lenp = rejp - inp;
1831         break;
1832     }
1833
1834     LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1835     return (rc);                        /* Return final code */
1836 }
1837
1838
1839 /*
1840  * lcp_up - LCP has come UP.
1841  */
1842 static void
1843 lcp_up(f)
1844     fsm *f;
1845 {
1846     lcp_options *wo = &lcp_wantoptions[f->unit];
1847     lcp_options *ho = &lcp_hisoptions[f->unit];
1848     lcp_options *go = &lcp_gotoptions[f->unit];
1849     lcp_options *ao = &lcp_allowoptions[f->unit];
1850     int mtu, mru;
1851
1852     if (!go->neg_magicnumber)
1853         go->magicnumber = 0;
1854     if (!ho->neg_magicnumber)
1855         ho->magicnumber = 0;
1856
1857     /*
1858      * Set our MTU to the smaller of the MTU we wanted and
1859      * the MRU our peer wanted.  If we negotiated an MRU,
1860      * set our MRU to the larger of value we wanted and
1861      * the value we got in the negotiation.
1862      * Note on the MTU: the link MTU can be the MRU the peer wanted,
1863      * the interface MTU is set to the lowest of that, the
1864      * MTU we want to use, and our link MRU.
1865      */
1866     mtu = ho->neg_mru? ho->mru: PPP_MRU;
1867     mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1868 #ifdef HAVE_MULTILINK
1869     if (!(multilink && go->neg_mrru && ho->neg_mrru))
1870 #endif /* HAVE_MULTILINK */
1871         netif_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1872     ppp_send_config(f->unit, mtu,
1873                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1874                     ho->neg_pcompression, ho->neg_accompression);
1875     ppp_recv_config(f->unit, mru,
1876                     (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1877                     go->neg_pcompression, go->neg_accompression);
1878
1879     if (ho->neg_mru)
1880         peer_mru[f->unit] = ho->mru;
1881
1882     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
1883
1884     link_established(f->unit);
1885 }
1886
1887
1888 /*
1889  * lcp_down - LCP has gone DOWN.
1890  *
1891  * Alert other protocols.
1892  */
1893 static void
1894 lcp_down(f)
1895     fsm *f;
1896 {
1897     lcp_options *go = &lcp_gotoptions[f->unit];
1898
1899     lcp_echo_lowerdown(f->unit);
1900
1901     link_down(f->unit);
1902
1903     ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1904     ppp_recv_config(f->unit, PPP_MRU,
1905                     (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1906                     go->neg_pcompression, go->neg_accompression);
1907     peer_mru[f->unit] = PPP_MRU;
1908 }
1909
1910
1911 /*
1912  * lcp_starting - LCP needs the lower layer up.
1913  */
1914 static void
1915 lcp_starting(f)
1916     fsm *f;
1917 {
1918     link_required(f->unit);
1919 }
1920
1921
1922 /*
1923  * lcp_finished - LCP has finished with the lower layer.
1924  */
1925 static void
1926 lcp_finished(f)
1927     fsm *f;
1928 {
1929     link_terminated(f->unit);
1930 }
1931
1932
1933 /*
1934  * lcp_printpkt - print the contents of an LCP packet.
1935  */
1936 static char *lcp_codenames[] = {
1937     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1938     "TermReq", "TermAck", "CodeRej", "ProtRej",
1939     "EchoReq", "EchoRep", "DiscReq"
1940 };
1941
1942 static int
1943 lcp_printpkt(p, plen, printer, arg)
1944     u_char *p;
1945     int plen;
1946     void (*printer) __P((void *, char *, ...));
1947     void *arg;
1948 {
1949     int code, id, len, olen, i;
1950     u_char *pstart, *optend;
1951     u_short cishort;
1952     u_int32_t cilong;
1953
1954     if (plen < HEADERLEN)
1955         return 0;
1956     pstart = p;
1957     GETCHAR(code, p);
1958     GETCHAR(id, p);
1959     GETSHORT(len, p);
1960     if (len < HEADERLEN || len > plen)
1961         return 0;
1962
1963     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1964         printer(arg, " %s", lcp_codenames[code-1]);
1965     else
1966         printer(arg, " code=0x%x", code);
1967     printer(arg, " id=0x%x", id);
1968     len -= HEADERLEN;
1969     switch (code) {
1970     case CONFREQ:
1971     case CONFACK:
1972     case CONFNAK:
1973     case CONFREJ:
1974         /* print option list */
1975         while (len >= 2) {
1976             GETCHAR(code, p);
1977             GETCHAR(olen, p);
1978             p -= 2;
1979             if (olen < 2 || olen > len) {
1980                 break;
1981             }
1982             printer(arg, " <");
1983             len -= olen;
1984             optend = p + olen;
1985             switch (code) {
1986             case CI_MRU:
1987                 if (olen == CILEN_SHORT) {
1988                     p += 2;
1989                     GETSHORT(cishort, p);
1990                     printer(arg, "mru %d", cishort);
1991                 }
1992                 break;
1993             case CI_ASYNCMAP:
1994                 if (olen == CILEN_LONG) {
1995                     p += 2;
1996                     GETLONG(cilong, p);
1997                     printer(arg, "asyncmap 0x%x", cilong);
1998                 }
1999                 break;
2000             case CI_AUTHTYPE:
2001                 if (olen >= CILEN_SHORT) {
2002                     p += 2;
2003                     printer(arg, "auth ");
2004                     GETSHORT(cishort, p);
2005                     switch (cishort) {
2006                     case PPP_PAP:
2007                         printer(arg, "pap");
2008                         break;
2009                     case PPP_CHAP:
2010                         printer(arg, "chap");
2011                         if (p < optend) {
2012                             switch (*p) {
2013                             case CHAP_DIGEST_MD5:
2014                                 printer(arg, " MD5");
2015                                 ++p;
2016                                 break;
2017 #ifdef CHAPMS
2018                             case CHAP_MICROSOFT:
2019                                 printer(arg, " MS");
2020                                 ++p;
2021                                 break;
2022
2023                             case CHAP_MICROSOFT_V2:
2024                                 printer(arg, " MS-v2");
2025                                 ++p;
2026                                 break;
2027 #endif
2028                             }
2029                         }
2030                         break;
2031                     case PPP_EAP:
2032                         printer(arg, "eap");
2033                         break;
2034                     default:
2035                         printer(arg, "0x%x", cishort);
2036                     }
2037                 }
2038                 break;
2039             case CI_QUALITY:
2040                 if (olen >= CILEN_SHORT) {
2041                     p += 2;
2042                     printer(arg, "quality ");
2043                     GETSHORT(cishort, p);
2044                     switch (cishort) {
2045                     case PPP_LQR:
2046                         printer(arg, "lqr");
2047                         break;
2048                     default:
2049                         printer(arg, "0x%x", cishort);
2050                     }
2051                 }
2052                 break;
2053             case CI_CALLBACK:
2054                 if (olen >= CILEN_CHAR) {
2055                     p += 2;
2056                     printer(arg, "callback ");
2057                     GETCHAR(cishort, p);
2058                     switch (cishort) {
2059                     case CBCP_OPT:
2060                         printer(arg, "CBCP");
2061                         break;
2062                     default:
2063                         printer(arg, "0x%x", cishort);
2064                     }
2065                 }
2066                 break;
2067             case CI_MAGICNUMBER:
2068                 if (olen == CILEN_LONG) {
2069                     p += 2;
2070                     GETLONG(cilong, p);
2071                     printer(arg, "magic 0x%x", cilong);
2072                 }
2073                 break;
2074             case CI_PCOMPRESSION:
2075                 if (olen == CILEN_VOID) {
2076                     p += 2;
2077                     printer(arg, "pcomp");
2078                 }
2079                 break;
2080             case CI_ACCOMPRESSION:
2081                 if (olen == CILEN_VOID) {
2082                     p += 2;
2083                     printer(arg, "accomp");
2084                 }
2085                 break;
2086             case CI_MRRU:
2087                 if (olen == CILEN_SHORT) {
2088                     p += 2;
2089                     GETSHORT(cishort, p);
2090                     printer(arg, "mrru %d", cishort);
2091                 }
2092                 break;
2093             case CI_SSNHF:
2094                 if (olen == CILEN_VOID) {
2095                     p += 2;
2096                     printer(arg, "ssnhf");
2097                 }
2098                 break;
2099             case CI_EPDISC:
2100 #ifdef HAVE_MULTILINK
2101                 if (olen >= CILEN_CHAR) {
2102                     struct epdisc epd;
2103                     p += 2;
2104                     GETCHAR(epd.class, p);
2105                     epd.length = olen - CILEN_CHAR;
2106                     if (epd.length > MAX_ENDP_LEN)
2107                         epd.length = MAX_ENDP_LEN;
2108                     if (epd.length > 0) {
2109                         BCOPY(p, epd.value, epd.length);
2110                         p += epd.length;
2111                     }
2112                     printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2113                 }
2114 #else
2115                 printer(arg, "endpoint");
2116 #endif
2117                 break;
2118             }
2119             while (p < optend) {
2120                 GETCHAR(code, p);
2121                 printer(arg, " %.2x", code);
2122             }
2123             printer(arg, ">");
2124         }
2125         break;
2126
2127     case TERMACK:
2128     case TERMREQ:
2129         if (len > 0 && *p >= ' ' && *p < 0x7f) {
2130             printer(arg, " ");
2131             print_string((char *)p, len, printer, arg);
2132             p += len;
2133             len = 0;
2134         }
2135         break;
2136
2137     case ECHOREQ:
2138     case ECHOREP:
2139     case DISCREQ:
2140         if (len >= 4) {
2141             GETLONG(cilong, p);
2142             printer(arg, " magic=0x%x", cilong);
2143             p += 4;
2144             len -= 4;
2145         }
2146         break;
2147     }
2148
2149     /* print the rest of the bytes in the packet */
2150     for (i = 0; i < len && i < 32; ++i) {
2151         GETCHAR(code, p);
2152         printer(arg, " %.2x", code);
2153     }
2154     if (i < len) {
2155         printer(arg, " ...");
2156         p += len - i;
2157     }
2158
2159     return p - pstart;
2160 }
2161
2162 /*
2163  * Time to shut down the link because there is nothing out there.
2164  */
2165
2166 static
2167 void LcpLinkFailure (f)
2168     fsm *f;
2169 {
2170     if (f->state == OPENED) {
2171         info("No response to %d echo-requests", lcp_echos_pending);
2172         notice("Serial link appears to be disconnected.");
2173         lcp_close(f->unit, "Peer not responding");
2174         status = EXIT_PEER_DEAD;
2175     }
2176 }
2177
2178 /*
2179  * Timer expired for the LCP echo requests from this process.
2180  */
2181
2182 static void
2183 LcpEchoCheck (f)
2184     fsm *f;
2185 {
2186     LcpSendEchoRequest (f);
2187     if (f->state != OPENED)
2188         return;
2189
2190     /*
2191      * Start the timer for the next interval.
2192      */
2193     if (lcp_echo_timer_running)
2194         warn("assertion lcp_echo_timer_running==0 failed");
2195     TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2196     lcp_echo_timer_running = 1;
2197 }
2198
2199 /*
2200  * LcpEchoTimeout - Timer expired on the LCP echo
2201  */
2202
2203 static void
2204 LcpEchoTimeout (arg)
2205     void *arg;
2206 {
2207     if (lcp_echo_timer_running != 0) {
2208         lcp_echo_timer_running = 0;
2209         LcpEchoCheck ((fsm *) arg);
2210     }
2211 }
2212
2213 /*
2214  * LcpEchoReply - LCP has received a reply to the echo
2215  */
2216
2217 static void
2218 lcp_received_echo_reply (f, id, inp, len)
2219     fsm *f;
2220     int id;
2221     u_char *inp;
2222     int len;
2223 {
2224     u_int32_t magic;
2225
2226     /* Check the magic number - don't count replies from ourselves. */
2227     if (len < 4) {
2228         dbglog("lcp: received short Echo-Reply, length %d", len);
2229         return;
2230     }
2231     GETLONG(magic, inp);
2232     if (lcp_gotoptions[f->unit].neg_magicnumber
2233         && magic == lcp_gotoptions[f->unit].magicnumber) {
2234         warn("appear to have received our own echo-reply!");
2235         return;
2236     }
2237
2238     /* Reset the number of outstanding echo frames */
2239     lcp_echos_pending = 0;
2240 }
2241
2242 /*
2243  * LcpSendEchoRequest - Send an echo request frame to the peer
2244  */
2245
2246 static void
2247 LcpSendEchoRequest (f)
2248     fsm *f;
2249 {
2250     u_int32_t lcp_magic;
2251     u_char pkt[4], *pktp;
2252
2253     /*
2254      * Detect the failure of the peer at this point.
2255      */
2256     if (lcp_echo_fails != 0) {
2257         if (lcp_echos_pending >= lcp_echo_fails) {
2258             LcpLinkFailure(f);
2259             lcp_echos_pending = 0;
2260         }
2261     }
2262
2263     /*
2264      * Make and send the echo request frame.
2265      */
2266     if (f->state == OPENED) {
2267         lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2268         pktp = pkt;
2269         PUTLONG(lcp_magic, pktp);
2270         fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2271         ++lcp_echos_pending;
2272     }
2273 }
2274
2275 /*
2276  * lcp_echo_lowerup - Start the timer for the LCP frame
2277  */
2278
2279 static void
2280 lcp_echo_lowerup (unit)
2281     int unit;
2282 {
2283     fsm *f = &lcp_fsm[unit];
2284
2285     /* Clear the parameters for generating echo frames */
2286     lcp_echos_pending      = 0;
2287     lcp_echo_number        = 0;
2288     lcp_echo_timer_running = 0;
2289   
2290     /* If a timeout interval is specified then start the timer */
2291     if (lcp_echo_interval != 0)
2292         LcpEchoCheck (f);
2293 }
2294
2295 /*
2296  * lcp_echo_lowerdown - Stop the timer for the LCP frame
2297  */
2298
2299 static void
2300 lcp_echo_lowerdown (unit)
2301     int unit;
2302 {
2303     fsm *f = &lcp_fsm[unit];
2304
2305     if (lcp_echo_timer_running != 0) {
2306         UNTIMEOUT (LcpEchoTimeout, f);
2307         lcp_echo_timer_running = 0;
2308     }
2309 }