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