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