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