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