]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
moved protos here from ipcp.h; print reason on terminate-request;
[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.26 1996/05/28 00:41:13 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     lcp_options try;            /* options to request next time */
932
933     try = *go;
934
935     /*
936      * Any Rejected CIs must be in exactly the same order that we sent.
937      * Check packet length and CI length at each step.
938      * If we find any deviations, then this packet is bad.
939      */
940 #define REJCIVOID(opt, neg) \
941     if (go->neg && \
942         len >= CILEN_VOID && \
943         p[1] == CILEN_VOID && \
944         p[0] == opt) { \
945         len -= CILEN_VOID; \
946         INCPTR(CILEN_VOID, p); \
947         try.neg = 0; \
948         LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
949     }
950 #define REJCISHORT(opt, neg, val) \
951     if (go->neg && \
952         len >= CILEN_SHORT && \
953         p[1] == CILEN_SHORT && \
954         p[0] == opt) { \
955         len -= CILEN_SHORT; \
956         INCPTR(2, p); \
957         GETSHORT(cishort, p); \
958         /* Check rejected value. */ \
959         if (cishort != val) \
960             goto bad; \
961         try.neg = 0; \
962         LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
963     }
964 #define REJCICHAP(opt, neg, val, digest) \
965     if (go->neg && \
966         len >= CILEN_CHAP && \
967         p[1] == CILEN_CHAP && \
968         p[0] == opt) { \
969         len -= CILEN_CHAP; \
970         INCPTR(2, p); \
971         GETSHORT(cishort, p); \
972         GETCHAR(cichar, p); \
973         /* Check rejected value. */ \
974         if (cishort != val || cichar != digest) \
975             goto bad; \
976         try.neg = 0; \
977         try.neg_upap = 0; \
978         LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
979     }
980 #define REJCILONG(opt, neg, val) \
981     if (go->neg && \
982         len >= CILEN_LONG && \
983         p[1] == CILEN_LONG && \
984         p[0] == opt) { \
985         len -= CILEN_LONG; \
986         INCPTR(2, p); \
987         GETLONG(cilong, p); \
988         /* Check rejected value. */ \
989         if (cilong != val) \
990             goto bad; \
991         try.neg = 0; \
992         LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
993     }
994 #define REJCILQR(opt, neg, val) \
995     if (go->neg && \
996         len >= CILEN_LQR && \
997         p[1] == CILEN_LQR && \
998         p[0] == opt) { \
999         len -= CILEN_LQR; \
1000         INCPTR(2, p); \
1001         GETSHORT(cishort, p); \
1002         GETLONG(cilong, p); \
1003         /* Check rejected value. */ \
1004         if (cishort != PPP_LQR || cilong != val) \
1005             goto bad; \
1006         try.neg = 0; \
1007         LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1008     }
1009
1010     REJCISHORT(CI_MRU, neg_mru, go->mru);
1011     REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1012     REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1013     if (!go->neg_chap) {
1014         REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1015     }
1016     REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1017     REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1018     REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1019     REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1020
1021     /*
1022      * If there are any remaining CIs, then this packet is bad.
1023      */
1024     if (len != 0)
1025         goto bad;
1026     /*
1027      * Now we can update state.
1028      */
1029     if (f->state != OPENED)
1030         *go = try;
1031     return 1;
1032
1033 bad:
1034     LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1035     return 0;
1036 }
1037
1038
1039 /*
1040  * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1041  *
1042  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1043  * appropriately.  If reject_if_disagree is non-zero, doesn't return
1044  * CONFNAK; returns CONFREJ if it can't return CONFACK.
1045  */
1046 static int
1047 lcp_reqci(f, inp, lenp, reject_if_disagree)
1048     fsm *f;
1049     u_char *inp;                /* Requested CIs */
1050     int *lenp;                  /* Length of requested CIs */
1051     int reject_if_disagree;
1052 {
1053     lcp_options *go = &lcp_gotoptions[f->unit];
1054     lcp_options *ho = &lcp_hisoptions[f->unit];
1055     lcp_options *ao = &lcp_allowoptions[f->unit];
1056     u_char *cip, *next;         /* Pointer to current and next CIs */
1057     int cilen, citype, cichar;  /* Parsed len, type, char value */
1058     u_short cishort;            /* Parsed short value */
1059     u_int32_t cilong;           /* Parse long value */
1060     int rc = CONFACK;           /* Final packet return code */
1061     int orc;                    /* Individual option return code */
1062     u_char *p;                  /* Pointer to next char to parse */
1063     u_char *rejp;               /* Pointer to next char in reject frame */
1064     u_char *nakp;               /* Pointer to next char in Nak frame */
1065     int l = *lenp;              /* Length left */
1066
1067     /*
1068      * Reset all his options.
1069      */
1070     BZERO(ho, sizeof(*ho));
1071
1072     /*
1073      * Process all his options.
1074      */
1075     next = inp;
1076     nakp = nak_buffer;
1077     rejp = inp;
1078     while (l) {
1079         orc = CONFACK;                  /* Assume success */
1080         cip = p = next;                 /* Remember begining of CI */
1081         if (l < 2 ||                    /* Not enough data for CI header or */
1082             p[1] < 2 ||                 /*  CI length too small or */
1083             p[1] > l) {                 /*  CI length too big? */
1084             LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1085             orc = CONFREJ;              /* Reject bad CI */
1086             cilen = l;                  /* Reject till end of packet */
1087             l = 0;                      /* Don't loop again */
1088             citype = 0;
1089             goto endswitch;
1090         }
1091         GETCHAR(citype, p);             /* Parse CI type */
1092         GETCHAR(cilen, p);              /* Parse CI length */
1093         l -= cilen;                     /* Adjust remaining length */
1094         next += cilen;                  /* Step to next CI */
1095
1096         switch (citype) {               /* Check CI type */
1097         case CI_MRU:
1098             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1099             if (!ao->neg_mru ||         /* Allow option? */
1100                 cilen != CILEN_SHORT) { /* Check CI length */
1101                 orc = CONFREJ;          /* Reject CI */
1102                 break;
1103             }
1104             GETSHORT(cishort, p);       /* Parse MRU */
1105             LCPDEBUG((LOG_INFO, "(%d)", cishort));
1106
1107             /*
1108              * He must be able to receive at least our minimum.
1109              * No need to check a maximum.  If he sends a large number,
1110              * we'll just ignore it.
1111              */
1112             if (cishort < MINMRU) {
1113                 orc = CONFNAK;          /* Nak CI */
1114                 PUTCHAR(CI_MRU, nakp);
1115                 PUTCHAR(CILEN_SHORT, nakp);
1116                 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1117                 break;
1118             }
1119             ho->neg_mru = 1;            /* Remember he sent MRU */
1120             ho->mru = cishort;          /* And remember value */
1121             break;
1122
1123         case CI_ASYNCMAP:
1124             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1125             if (!ao->neg_asyncmap ||
1126                 cilen != CILEN_LONG) {
1127                 orc = CONFREJ;
1128                 break;
1129             }
1130             GETLONG(cilong, p);
1131             LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1132
1133             /*
1134              * Asyncmap must have set at least the bits
1135              * which are set in lcp_allowoptions[unit].asyncmap.
1136              */
1137             if ((ao->asyncmap & ~cilong) != 0) {
1138                 orc = CONFNAK;
1139                 PUTCHAR(CI_ASYNCMAP, nakp);
1140                 PUTCHAR(CILEN_LONG, nakp);
1141                 PUTLONG(ao->asyncmap | cilong, nakp);
1142                 break;
1143             }
1144             ho->neg_asyncmap = 1;
1145             ho->asyncmap = cilong;
1146             break;
1147
1148         case CI_AUTHTYPE:
1149             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1150             if (cilen < CILEN_SHORT ||
1151                 !(ao->neg_upap || ao->neg_chap)) {
1152                 /*
1153                  * Reject the option if we're not willing to authenticate.
1154                  */
1155                 orc = CONFREJ;
1156                 break;
1157             }
1158             GETSHORT(cishort, p);
1159             LCPDEBUG((LOG_INFO, "(%x)", cishort));
1160
1161             /*
1162              * Authtype must be UPAP or CHAP.
1163              *
1164              * Note: if both ao->neg_upap and ao->neg_chap are set,
1165              * and the peer sends a Configure-Request with two
1166              * authenticate-protocol requests, one for CHAP and one
1167              * for UPAP, then we will reject the second request.
1168              * Whether we end up doing CHAP or UPAP depends then on
1169              * the ordering of the CIs in the peer's Configure-Request.
1170              */
1171
1172             if (cishort == PPP_PAP) {
1173                 if (ho->neg_chap ||     /* we've already accepted CHAP */
1174                     cilen != CILEN_SHORT) {
1175                     LCPDEBUG((LOG_WARNING,
1176                               "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1177                     orc = CONFREJ;
1178                     break;
1179                 }
1180                 if (!ao->neg_upap) {    /* we don't want to do PAP */
1181                     orc = CONFNAK;      /* NAK it and suggest CHAP */
1182                     PUTCHAR(CI_AUTHTYPE, nakp);
1183                     PUTCHAR(CILEN_CHAP, nakp);
1184                     PUTSHORT(PPP_CHAP, nakp);
1185                     PUTCHAR(ao->chap_mdtype, nakp);
1186                     break;
1187                 }
1188                 ho->neg_upap = 1;
1189                 break;
1190             }
1191             if (cishort == PPP_CHAP) {
1192                 if (ho->neg_upap ||     /* we've already accepted PAP */
1193                     cilen != CILEN_CHAP) {
1194                     LCPDEBUG((LOG_INFO,
1195                               "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1196                     orc = CONFREJ;
1197                     break;
1198                 }
1199                 if (!ao->neg_chap) {    /* we don't want to do CHAP */
1200                     orc = CONFNAK;      /* NAK it and suggest PAP */
1201                     PUTCHAR(CI_AUTHTYPE, nakp);
1202                     PUTCHAR(CILEN_SHORT, nakp);
1203                     PUTSHORT(PPP_PAP, nakp);
1204                     break;
1205                 }
1206                 GETCHAR(cichar, p);     /* get digest type*/
1207                 if (cichar != CHAP_DIGEST_MD5
1208 #ifdef CHAPMS
1209                     && cichar != CHAP_MICROSOFT
1210 #endif
1211                     ) {
1212                     orc = CONFNAK;
1213                     PUTCHAR(CI_AUTHTYPE, nakp);
1214                     PUTCHAR(CILEN_CHAP, nakp);
1215                     PUTSHORT(PPP_CHAP, nakp);
1216                     PUTCHAR(ao->chap_mdtype, nakp);
1217                     break;
1218                 }
1219                 ho->chap_mdtype = cichar; /* save md type */
1220                 ho->neg_chap = 1;
1221                 break;
1222             }
1223
1224             /*
1225              * We don't recognize the protocol they're asking for.
1226              * Nak it with something we're willing to do.
1227              * (At this point we know ao->neg_upap || ao->neg_chap.)
1228              */
1229             orc = CONFNAK;
1230             PUTCHAR(CI_AUTHTYPE, nakp);
1231             if (ao->neg_chap) {
1232                 PUTCHAR(CILEN_CHAP, nakp);
1233                 PUTSHORT(PPP_CHAP, nakp);
1234                 PUTCHAR(ao->chap_mdtype, nakp);
1235             } else {
1236                 PUTCHAR(CILEN_SHORT, nakp);
1237                 PUTSHORT(PPP_PAP, nakp);
1238             }
1239             break;
1240
1241         case CI_QUALITY:
1242             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1243             if (!ao->neg_lqr ||
1244                 cilen != CILEN_LQR) {
1245                 orc = CONFREJ;
1246                 break;
1247             }
1248
1249             GETSHORT(cishort, p);
1250             GETLONG(cilong, p);
1251             LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1252
1253             /*
1254              * Check the protocol and the reporting period.
1255              * XXX When should we Nak this, and what with?
1256              */
1257             if (cishort != PPP_LQR) {
1258                 orc = CONFNAK;
1259                 PUTCHAR(CI_QUALITY, nakp);
1260                 PUTCHAR(CILEN_LQR, nakp);
1261                 PUTSHORT(PPP_LQR, nakp);
1262                 PUTLONG(ao->lqr_period, nakp);
1263                 break;
1264             }
1265             break;
1266
1267         case CI_MAGICNUMBER:
1268             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1269             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1270                 cilen != CILEN_LONG) {
1271                 orc = CONFREJ;
1272                 break;
1273             }
1274             GETLONG(cilong, p);
1275             LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1276
1277             /*
1278              * He must have a different magic number.
1279              */
1280             if (go->neg_magicnumber &&
1281                 cilong == go->magicnumber) {
1282                 cilong = magic();       /* Don't put magic() inside macro! */
1283                 orc = CONFNAK;
1284                 PUTCHAR(CI_MAGICNUMBER, nakp);
1285                 PUTCHAR(CILEN_LONG, nakp);
1286                 PUTLONG(cilong, nakp);
1287                 break;
1288             }
1289             ho->neg_magicnumber = 1;
1290             ho->magicnumber = cilong;
1291             break;
1292
1293
1294         case CI_PCOMPRESSION:
1295             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1296             if (!ao->neg_pcompression ||
1297                 cilen != CILEN_VOID) {
1298                 orc = CONFREJ;
1299                 break;
1300             }
1301             ho->neg_pcompression = 1;
1302             break;
1303
1304         case CI_ACCOMPRESSION:
1305             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1306             if (!ao->neg_accompression ||
1307                 cilen != CILEN_VOID) {
1308                 orc = CONFREJ;
1309                 break;
1310             }
1311             ho->neg_accompression = 1;
1312             break;
1313
1314         default:
1315             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1316                       citype));
1317             orc = CONFREJ;
1318             break;
1319         }
1320
1321 endswitch:
1322         LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1323         if (orc == CONFACK &&           /* Good CI */
1324             rc != CONFACK)              /*  but prior CI wasnt? */
1325             continue;                   /* Don't send this one */
1326
1327         if (orc == CONFNAK) {           /* Nak this CI? */
1328             if (reject_if_disagree      /* Getting fed up with sending NAKs? */
1329                 && citype != CI_MAGICNUMBER) {
1330                 orc = CONFREJ;          /* Get tough if so */
1331             } else {
1332                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1333                     continue;           /* Don't send this one */
1334                 rc = CONFNAK;
1335             }
1336         }
1337         if (orc == CONFREJ) {           /* Reject this CI */
1338             rc = CONFREJ;
1339             if (cip != rejp)            /* Need to move rejected CI? */
1340                 BCOPY(cip, rejp, cilen); /* Move it */
1341             INCPTR(cilen, rejp);        /* Update output pointer */
1342         }
1343     }
1344
1345     /*
1346      * If we wanted to send additional NAKs (for unsent CIs), the
1347      * code would go here.  The extra NAKs would go at *nakp.
1348      * At present there are no cases where we want to ask the
1349      * peer to negotiate an option.
1350      */
1351
1352     switch (rc) {
1353     case CONFACK:
1354         *lenp = next - inp;
1355         break;
1356     case CONFNAK:
1357         /*
1358          * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1359          */
1360         *lenp = nakp - nak_buffer;
1361         BCOPY(nak_buffer, inp, *lenp);
1362         break;
1363     case CONFREJ:
1364         *lenp = rejp - inp;
1365         break;
1366     }
1367
1368     LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1369     return (rc);                        /* Return final code */
1370 }
1371
1372
1373 /*
1374  * lcp_up - LCP has come UP.
1375  */
1376 static void
1377 lcp_up(f)
1378     fsm *f;
1379 {
1380     lcp_options *wo = &lcp_wantoptions[f->unit];
1381     lcp_options *ho = &lcp_hisoptions[f->unit];
1382     lcp_options *go = &lcp_gotoptions[f->unit];
1383     lcp_options *ao = &lcp_allowoptions[f->unit];
1384
1385     if (!go->neg_magicnumber)
1386         go->magicnumber = 0;
1387     if (!ho->neg_magicnumber)
1388         ho->magicnumber = 0;
1389
1390     /*
1391      * Set our MTU to the smaller of the MTU we wanted and
1392      * the MRU our peer wanted.  If we negotiated an MRU,
1393      * set our MRU to the larger of value we wanted and
1394      * the value we got in the negotiation.
1395      */
1396     ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1397                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1398                     ho->neg_pcompression, ho->neg_accompression);
1399     /*
1400      * If the asyncmap hasn't been negotiated, we really should
1401      * set the receive asyncmap to ffffffff, but we set it to 0
1402      * for backwards contemptibility.
1403      */
1404     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1405                     (go->neg_asyncmap? go->asyncmap: 0x00000000),
1406                     go->neg_pcompression, go->neg_accompression);
1407
1408     if (ho->neg_mru)
1409         peer_mru[f->unit] = ho->mru;
1410
1411     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
1412
1413     link_established(f->unit);
1414 }
1415
1416
1417 /*
1418  * lcp_down - LCP has gone DOWN.
1419  *
1420  * Alert other protocols.
1421  */
1422 static void
1423 lcp_down(f)
1424     fsm *f;
1425 {
1426     lcp_options *go = &lcp_gotoptions[f->unit];
1427
1428     lcp_echo_lowerdown(f->unit);
1429
1430     link_down(f->unit);
1431
1432     ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1433     ppp_recv_config(f->unit, PPP_MRU,
1434                     (go->neg_asyncmap? go->asyncmap: 0x00000000),
1435                     go->neg_pcompression, go->neg_accompression);
1436     peer_mru[f->unit] = PPP_MRU;
1437 }
1438
1439
1440 /*
1441  * lcp_starting - LCP needs the lower layer up.
1442  */
1443 static void
1444 lcp_starting(f)
1445     fsm *f;
1446 {
1447     link_required(f->unit);
1448 }
1449
1450
1451 /*
1452  * lcp_finished - LCP has finished with the lower layer.
1453  */
1454 static void
1455 lcp_finished(f)
1456     fsm *f;
1457 {
1458     link_terminated(f->unit);
1459 }
1460
1461
1462 /*
1463  * lcp_printpkt - print the contents of an LCP packet.
1464  */
1465 char *lcp_codenames[] = {
1466     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1467     "TermReq", "TermAck", "CodeRej", "ProtRej",
1468     "EchoReq", "EchoRep", "DiscReq"
1469 };
1470
1471 int
1472 lcp_printpkt(p, plen, printer, arg)
1473     u_char *p;
1474     int plen;
1475     void (*printer) __P((void *, char *, ...));
1476     void *arg;
1477 {
1478     int code, id, len, olen;
1479     u_char *pstart, *optend;
1480     u_short cishort;
1481     u_int32_t cilong;
1482
1483     if (plen < HEADERLEN)
1484         return 0;
1485     pstart = p;
1486     GETCHAR(code, p);
1487     GETCHAR(id, p);
1488     GETSHORT(len, p);
1489     if (len < HEADERLEN || len > plen)
1490         return 0;
1491
1492     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1493         printer(arg, " %s", lcp_codenames[code-1]);
1494     else
1495         printer(arg, " code=0x%x", code);
1496     printer(arg, " id=0x%x", id);
1497     len -= HEADERLEN;
1498     switch (code) {
1499     case CONFREQ:
1500     case CONFACK:
1501     case CONFNAK:
1502     case CONFREJ:
1503         /* print option list */
1504         while (len >= 2) {
1505             GETCHAR(code, p);
1506             GETCHAR(olen, p);
1507             p -= 2;
1508             if (olen < 2 || olen > len) {
1509                 break;
1510             }
1511             printer(arg, " <");
1512             len -= olen;
1513             optend = p + olen;
1514             switch (code) {
1515             case CI_MRU:
1516                 if (olen == CILEN_SHORT) {
1517                     p += 2;
1518                     GETSHORT(cishort, p);
1519                     printer(arg, "mru %d", cishort);
1520                 }
1521                 break;
1522             case CI_ASYNCMAP:
1523                 if (olen == CILEN_LONG) {
1524                     p += 2;
1525                     GETLONG(cilong, p);
1526                     printer(arg, "asyncmap 0x%x", cilong);
1527                 }
1528                 break;
1529             case CI_AUTHTYPE:
1530                 if (olen >= CILEN_SHORT) {
1531                     p += 2;
1532                     printer(arg, "auth ");
1533                     GETSHORT(cishort, p);
1534                     switch (cishort) {
1535                     case PPP_PAP:
1536                         printer(arg, "pap");
1537                         break;
1538                     case PPP_CHAP:
1539                         printer(arg, "chap");
1540                         break;
1541                     default:
1542                         printer(arg, "0x%x", cishort);
1543                     }
1544                 }
1545                 break;
1546             case CI_QUALITY:
1547                 if (olen >= CILEN_SHORT) {
1548                     p += 2;
1549                     printer(arg, "quality ");
1550                     GETSHORT(cishort, p);
1551                     switch (cishort) {
1552                     case PPP_LQR:
1553                         printer(arg, "lqr");
1554                         break;
1555                     default:
1556                         printer(arg, "0x%x", cishort);
1557                     }
1558                 }
1559                 break;
1560             case CI_MAGICNUMBER:
1561                 if (olen == CILEN_LONG) {
1562                     p += 2;
1563                     GETLONG(cilong, p);
1564                     printer(arg, "magic 0x%x", cilong);
1565                 }
1566                 break;
1567             case CI_PCOMPRESSION:
1568                 if (olen == CILEN_VOID) {
1569                     p += 2;
1570                     printer(arg, "pcomp");
1571                 }
1572                 break;
1573             case CI_ACCOMPRESSION:
1574                 if (olen == CILEN_VOID) {
1575                     p += 2;
1576                     printer(arg, "accomp");
1577                 }
1578                 break;
1579             }
1580             while (p < optend) {
1581                 GETCHAR(code, p);
1582                 printer(arg, " %.2x", code);
1583             }
1584             printer(arg, ">");
1585         }
1586         break;
1587     case ECHOREQ:
1588     case ECHOREP:
1589     case DISCREQ:
1590         if (len >= 4) {
1591             GETLONG(cilong, p);
1592             printer(arg, " magic=0x%x", cilong);
1593             p += 4;
1594             len -= 4;
1595         }
1596         break;
1597     }
1598
1599     /* print the rest of the bytes in the packet */
1600     for (; len > 0; --len) {
1601         GETCHAR(code, p);
1602         printer(arg, " %.2x", code);
1603     }
1604
1605     return p - pstart;
1606 }
1607
1608 /*
1609  * Time to shut down the link because there is nothing out there.
1610  */
1611
1612 static
1613 void LcpLinkFailure (f)
1614     fsm *f;
1615 {
1616     if (f->state == OPENED) {
1617         syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1618         syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1619         lcp_close(f->unit, "Peer not responding");
1620         phase = PHASE_TERMINATE;
1621     }
1622 }
1623
1624 /*
1625  * Timer expired for the LCP echo requests from this process.
1626  */
1627
1628 static void
1629 LcpEchoCheck (f)
1630     fsm *f;
1631 {
1632     LcpSendEchoRequest (f);
1633
1634     /*
1635      * Start the timer for the next interval.
1636      */
1637     assert (lcp_echo_timer_running==0);
1638     TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1639     lcp_echo_timer_running = 1;
1640 }
1641
1642 /*
1643  * LcpEchoTimeout - Timer expired on the LCP echo
1644  */
1645
1646 static void
1647 LcpEchoTimeout (arg)
1648     caddr_t arg;
1649 {
1650     if (lcp_echo_timer_running != 0) {
1651         lcp_echo_timer_running = 0;
1652         LcpEchoCheck ((fsm *) arg);
1653     }
1654 }
1655
1656 /*
1657  * LcpEchoReply - LCP has received a reply to the echo
1658  */
1659
1660 static void
1661 lcp_received_echo_reply (f, id, inp, len)
1662     fsm *f;
1663     int id; u_char *inp; int len;
1664 {
1665     u_int32_t magic;
1666
1667     /* Check the magic number - don't count replies from ourselves. */
1668     if (len < 4) {
1669         syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1670         return;
1671     }
1672     GETLONG(magic, inp);
1673     if (lcp_gotoptions[f->unit].neg_magicnumber
1674         && magic == lcp_gotoptions[f->unit].magicnumber) {
1675         syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1676         return;
1677     }
1678
1679     /* Reset the number of outstanding echo frames */
1680     lcp_echos_pending = 0;
1681 }
1682
1683 /*
1684  * LcpSendEchoRequest - Send an echo request frame to the peer
1685  */
1686
1687 static void
1688 LcpSendEchoRequest (f)
1689     fsm *f;
1690 {
1691     u_int32_t lcp_magic;
1692     u_char pkt[4], *pktp;
1693
1694     /*
1695      * Detect the failure of the peer at this point.
1696      */
1697     if (lcp_echo_fails != 0) {
1698         if (lcp_echos_pending++ >= lcp_echo_fails) {
1699             LcpLinkFailure(f);
1700             lcp_echos_pending = 0;
1701         }
1702     }
1703
1704     /*
1705      * Make and send the echo request frame.
1706      */
1707     if (f->state == OPENED) {
1708         lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1709         pktp = pkt;
1710         PUTLONG(lcp_magic, pktp);
1711         fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1712     }
1713 }
1714
1715 /*
1716  * lcp_echo_lowerup - Start the timer for the LCP frame
1717  */
1718
1719 static void
1720 lcp_echo_lowerup (unit)
1721     int unit;
1722 {
1723     fsm *f = &lcp_fsm[unit];
1724
1725     /* Clear the parameters for generating echo frames */
1726     lcp_echos_pending      = 0;
1727     lcp_echo_number        = 0;
1728     lcp_echo_timer_running = 0;
1729   
1730     /* If a timeout interval is specified then start the timer */
1731     if (lcp_echo_interval != 0)
1732         LcpEchoCheck (f);
1733 }
1734
1735 /*
1736  * lcp_echo_lowerdown - Stop the timer for the LCP frame
1737  */
1738
1739 static void
1740 lcp_echo_lowerdown (unit)
1741     int unit;
1742 {
1743     fsm *f = &lcp_fsm[unit];
1744
1745     if (lcp_echo_timer_running != 0) {
1746         UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1747         lcp_echo_timer_running = 0;
1748     }
1749 }