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