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