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