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