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