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