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