]> git.ozlabs.org Git - ppp.git/blob - pppd/lcp.c
minor cleanup; add print for ECHORE[PQ] and DISCREQ
[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.25 1996/04/04 03:58:24 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 != ao->chap_mdtype) {
1208                     orc = CONFNAK;
1209                     PUTCHAR(CI_AUTHTYPE, nakp);
1210                     PUTCHAR(CILEN_CHAP, nakp);
1211                     PUTSHORT(PPP_CHAP, nakp);
1212                     PUTCHAR(ao->chap_mdtype, nakp);
1213                     break;
1214                 }
1215                 ho->chap_mdtype = cichar; /* save md type */
1216                 ho->neg_chap = 1;
1217                 break;
1218             }
1219
1220             /*
1221              * We don't recognize the protocol they're asking for.
1222              * Nak it with something we're willing to do.
1223              * (At this point we know ao->neg_upap || ao->neg_chap.)
1224              */
1225             orc = CONFNAK;
1226             PUTCHAR(CI_AUTHTYPE, nakp);
1227             if (ao->neg_chap) {
1228                 PUTCHAR(CILEN_CHAP, nakp);
1229                 PUTSHORT(PPP_CHAP, nakp);
1230                 PUTCHAR(ao->chap_mdtype, nakp);
1231             } else {
1232                 PUTCHAR(CILEN_SHORT, nakp);
1233                 PUTSHORT(PPP_PAP, nakp);
1234             }
1235             break;
1236
1237         case CI_QUALITY:
1238             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1239             if (!ao->neg_lqr ||
1240                 cilen != CILEN_LQR) {
1241                 orc = CONFREJ;
1242                 break;
1243             }
1244
1245             GETSHORT(cishort, p);
1246             GETLONG(cilong, p);
1247             LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1248
1249             /*
1250              * Check the protocol and the reporting period.
1251              * XXX When should we Nak this, and what with?
1252              */
1253             if (cishort != PPP_LQR) {
1254                 orc = CONFNAK;
1255                 PUTCHAR(CI_QUALITY, nakp);
1256                 PUTCHAR(CILEN_LQR, nakp);
1257                 PUTSHORT(PPP_LQR, nakp);
1258                 PUTLONG(ao->lqr_period, nakp);
1259                 break;
1260             }
1261             break;
1262
1263         case CI_MAGICNUMBER:
1264             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1265             if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1266                 cilen != CILEN_LONG) {
1267                 orc = CONFREJ;
1268                 break;
1269             }
1270             GETLONG(cilong, p);
1271             LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1272
1273             /*
1274              * He must have a different magic number.
1275              */
1276             if (go->neg_magicnumber &&
1277                 cilong == go->magicnumber) {
1278                 cilong = magic();       /* Don't put magic() inside macro! */
1279                 orc = CONFNAK;
1280                 PUTCHAR(CI_MAGICNUMBER, nakp);
1281                 PUTCHAR(CILEN_LONG, nakp);
1282                 PUTLONG(cilong, nakp);
1283                 break;
1284             }
1285             ho->neg_magicnumber = 1;
1286             ho->magicnumber = cilong;
1287             break;
1288
1289
1290         case CI_PCOMPRESSION:
1291             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1292             if (!ao->neg_pcompression ||
1293                 cilen != CILEN_VOID) {
1294                 orc = CONFREJ;
1295                 break;
1296             }
1297             ho->neg_pcompression = 1;
1298             break;
1299
1300         case CI_ACCOMPRESSION:
1301             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1302             if (!ao->neg_accompression ||
1303                 cilen != CILEN_VOID) {
1304                 orc = CONFREJ;
1305                 break;
1306             }
1307             ho->neg_accompression = 1;
1308             break;
1309
1310         default:
1311             LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1312                       citype));
1313             orc = CONFREJ;
1314             break;
1315         }
1316
1317 endswitch:
1318         LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1319         if (orc == CONFACK &&           /* Good CI */
1320             rc != CONFACK)              /*  but prior CI wasnt? */
1321             continue;                   /* Don't send this one */
1322
1323         if (orc == CONFNAK) {           /* Nak this CI? */
1324             if (reject_if_disagree      /* Getting fed up with sending NAKs? */
1325                 && citype != CI_MAGICNUMBER) {
1326                 orc = CONFREJ;          /* Get tough if so */
1327             } else {
1328                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1329                     continue;           /* Don't send this one */
1330                 rc = CONFNAK;
1331             }
1332         }
1333         if (orc == CONFREJ) {           /* Reject this CI */
1334             rc = CONFREJ;
1335             if (cip != rejp)            /* Need to move rejected CI? */
1336                 BCOPY(cip, rejp, cilen); /* Move it */
1337             INCPTR(cilen, rejp);        /* Update output pointer */
1338         }
1339     }
1340
1341     /*
1342      * If we wanted to send additional NAKs (for unsent CIs), the
1343      * code would go here.  The extra NAKs would go at *nakp.
1344      * At present there are no cases where we want to ask the
1345      * peer to negotiate an option.
1346      */
1347
1348     switch (rc) {
1349     case CONFACK:
1350         *lenp = next - inp;
1351         break;
1352     case CONFNAK:
1353         /*
1354          * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1355          */
1356         *lenp = nakp - nak_buffer;
1357         BCOPY(nak_buffer, inp, *lenp);
1358         break;
1359     case CONFREJ:
1360         *lenp = rejp - inp;
1361         break;
1362     }
1363
1364     LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1365     return (rc);                        /* Return final code */
1366 }
1367
1368
1369 /*
1370  * lcp_up - LCP has come UP.
1371  */
1372 static void
1373 lcp_up(f)
1374     fsm *f;
1375 {
1376     lcp_options *wo = &lcp_wantoptions[f->unit];
1377     lcp_options *ho = &lcp_hisoptions[f->unit];
1378     lcp_options *go = &lcp_gotoptions[f->unit];
1379     lcp_options *ao = &lcp_allowoptions[f->unit];
1380
1381     if (!go->neg_magicnumber)
1382         go->magicnumber = 0;
1383     if (!ho->neg_magicnumber)
1384         ho->magicnumber = 0;
1385
1386     /*
1387      * Set our MTU to the smaller of the MTU we wanted and
1388      * the MRU our peer wanted.  If we negotiated an MRU,
1389      * set our MRU to the larger of value we wanted and
1390      * the value we got in the negotiation.
1391      */
1392     ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1393                     (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1394                     ho->neg_pcompression, ho->neg_accompression);
1395     /*
1396      * If the asyncmap hasn't been negotiated, we really should
1397      * set the receive asyncmap to ffffffff, but we set it to 0
1398      * for backwards contemptibility.
1399      */
1400     ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1401                     (go->neg_asyncmap? go->asyncmap: 0x00000000),
1402                     go->neg_pcompression, go->neg_accompression);
1403
1404     if (ho->neg_mru)
1405         peer_mru[f->unit] = ho->mru;
1406
1407     lcp_echo_lowerup(f->unit);  /* Enable echo messages */
1408
1409     link_established(f->unit);
1410 }
1411
1412
1413 /*
1414  * lcp_down - LCP has gone DOWN.
1415  *
1416  * Alert other protocols.
1417  */
1418 static void
1419 lcp_down(f)
1420     fsm *f;
1421 {
1422     lcp_options *go = &lcp_gotoptions[f->unit];
1423
1424     lcp_echo_lowerdown(f->unit);
1425
1426     link_down(f->unit);
1427
1428     ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1429     ppp_recv_config(f->unit, PPP_MRU,
1430                     (go->neg_asyncmap? go->asyncmap: 0x00000000),
1431                     go->neg_pcompression, go->neg_accompression);
1432     peer_mru[f->unit] = PPP_MRU;
1433 }
1434
1435
1436 /*
1437  * lcp_starting - LCP needs the lower layer up.
1438  */
1439 static void
1440 lcp_starting(f)
1441     fsm *f;
1442 {
1443     link_required(f->unit);
1444 }
1445
1446
1447 /*
1448  * lcp_finished - LCP has finished with the lower layer.
1449  */
1450 static void
1451 lcp_finished(f)
1452     fsm *f;
1453 {
1454     link_terminated(f->unit);
1455 }
1456
1457
1458 /*
1459  * lcp_printpkt - print the contents of an LCP packet.
1460  */
1461 char *lcp_codenames[] = {
1462     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1463     "TermReq", "TermAck", "CodeRej", "ProtRej",
1464     "EchoReq", "EchoRep", "DiscReq"
1465 };
1466
1467 int
1468 lcp_printpkt(p, plen, printer, arg)
1469     u_char *p;
1470     int plen;
1471     void (*printer) __P((void *, char *, ...));
1472     void *arg;
1473 {
1474     int code, id, len, olen;
1475     u_char *pstart, *optend;
1476     u_short cishort;
1477     u_int32_t cilong;
1478
1479     if (plen < HEADERLEN)
1480         return 0;
1481     pstart = p;
1482     GETCHAR(code, p);
1483     GETCHAR(id, p);
1484     GETSHORT(len, p);
1485     if (len < HEADERLEN || len > plen)
1486         return 0;
1487
1488     if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1489         printer(arg, " %s", lcp_codenames[code-1]);
1490     else
1491         printer(arg, " code=0x%x", code);
1492     printer(arg, " id=0x%x", id);
1493     len -= HEADERLEN;
1494     switch (code) {
1495     case CONFREQ:
1496     case CONFACK:
1497     case CONFNAK:
1498     case CONFREJ:
1499         /* print option list */
1500         while (len >= 2) {
1501             GETCHAR(code, p);
1502             GETCHAR(olen, p);
1503             p -= 2;
1504             if (olen < 2 || olen > len) {
1505                 break;
1506             }
1507             printer(arg, " <");
1508             len -= olen;
1509             optend = p + olen;
1510             switch (code) {
1511             case CI_MRU:
1512                 if (olen == CILEN_SHORT) {
1513                     p += 2;
1514                     GETSHORT(cishort, p);
1515                     printer(arg, "mru %d", cishort);
1516                 }
1517                 break;
1518             case CI_ASYNCMAP:
1519                 if (olen == CILEN_LONG) {
1520                     p += 2;
1521                     GETLONG(cilong, p);
1522                     printer(arg, "asyncmap 0x%x", cilong);
1523                 }
1524                 break;
1525             case CI_AUTHTYPE:
1526                 if (olen >= CILEN_SHORT) {
1527                     p += 2;
1528                     printer(arg, "auth ");
1529                     GETSHORT(cishort, p);
1530                     switch (cishort) {
1531                     case PPP_PAP:
1532                         printer(arg, "pap");
1533                         break;
1534                     case PPP_CHAP:
1535                         printer(arg, "chap");
1536                         break;
1537                     default:
1538                         printer(arg, "0x%x", cishort);
1539                     }
1540                 }
1541                 break;
1542             case CI_QUALITY:
1543                 if (olen >= CILEN_SHORT) {
1544                     p += 2;
1545                     printer(arg, "quality ");
1546                     GETSHORT(cishort, p);
1547                     switch (cishort) {
1548                     case PPP_LQR:
1549                         printer(arg, "lqr");
1550                         break;
1551                     default:
1552                         printer(arg, "0x%x", cishort);
1553                     }
1554                 }
1555                 break;
1556             case CI_MAGICNUMBER:
1557                 if (olen == CILEN_LONG) {
1558                     p += 2;
1559                     GETLONG(cilong, p);
1560                     printer(arg, "magic 0x%x", cilong);
1561                 }
1562                 break;
1563             case CI_PCOMPRESSION:
1564                 if (olen == CILEN_VOID) {
1565                     p += 2;
1566                     printer(arg, "pcomp");
1567                 }
1568                 break;
1569             case CI_ACCOMPRESSION:
1570                 if (olen == CILEN_VOID) {
1571                     p += 2;
1572                     printer(arg, "accomp");
1573                 }
1574                 break;
1575             }
1576             while (p < optend) {
1577                 GETCHAR(code, p);
1578                 printer(arg, " %.2x", code);
1579             }
1580             printer(arg, ">");
1581         }
1582         break;
1583     case ECHOREQ:
1584     case ECHOREP:
1585     case DISCREQ:
1586         if (len >= 4) {
1587             GETLONG(cilong, p);
1588             printer(arg, " magic=0x%x", cilong);
1589             p += 4;
1590             len -= 4;
1591         }
1592         break;
1593     }
1594
1595     /* print the rest of the bytes in the packet */
1596     for (; len > 0; --len) {
1597         GETCHAR(code, p);
1598         printer(arg, " %.2x", code);
1599     }
1600
1601     return p - pstart;
1602 }
1603
1604 /*
1605  * Time to shut down the link because there is nothing out there.
1606  */
1607
1608 static
1609 void LcpLinkFailure (f)
1610     fsm *f;
1611 {
1612     if (f->state == OPENED) {
1613         syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1614         syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1615         lcp_close(f->unit, "Peer not responding");
1616         phase = PHASE_TERMINATE;
1617     }
1618 }
1619
1620 /*
1621  * Timer expired for the LCP echo requests from this process.
1622  */
1623
1624 static void
1625 LcpEchoCheck (f)
1626     fsm *f;
1627 {
1628     LcpSendEchoRequest (f);
1629
1630     /*
1631      * Start the timer for the next interval.
1632      */
1633     assert (lcp_echo_timer_running==0);
1634     TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1635     lcp_echo_timer_running = 1;
1636 }
1637
1638 /*
1639  * LcpEchoTimeout - Timer expired on the LCP echo
1640  */
1641
1642 static void
1643 LcpEchoTimeout (arg)
1644     caddr_t arg;
1645 {
1646     if (lcp_echo_timer_running != 0) {
1647         lcp_echo_timer_running = 0;
1648         LcpEchoCheck ((fsm *) arg);
1649     }
1650 }
1651
1652 /*
1653  * LcpEchoReply - LCP has received a reply to the echo
1654  */
1655
1656 static void
1657 lcp_received_echo_reply (f, id, inp, len)
1658     fsm *f;
1659     int id; u_char *inp; int len;
1660 {
1661     u_int32_t magic;
1662
1663     /* Check the magic number - don't count replies from ourselves. */
1664     if (len < 4) {
1665         syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1666         return;
1667     }
1668     GETLONG(magic, inp);
1669     if (lcp_gotoptions[f->unit].neg_magicnumber
1670         && magic == lcp_gotoptions[f->unit].magicnumber) {
1671         syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1672         return;
1673     }
1674
1675     /* Reset the number of outstanding echo frames */
1676     lcp_echos_pending = 0;
1677 }
1678
1679 /*
1680  * LcpSendEchoRequest - Send an echo request frame to the peer
1681  */
1682
1683 static void
1684 LcpSendEchoRequest (f)
1685     fsm *f;
1686 {
1687     u_int32_t lcp_magic;
1688     u_char pkt[4], *pktp;
1689
1690     /*
1691      * Detect the failure of the peer at this point.
1692      */
1693     if (lcp_echo_fails != 0) {
1694         if (lcp_echos_pending++ >= lcp_echo_fails) {
1695             LcpLinkFailure(f);
1696             lcp_echos_pending = 0;
1697         }
1698     }
1699
1700     /*
1701      * Make and send the echo request frame.
1702      */
1703     if (f->state == OPENED) {
1704         lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1705         pktp = pkt;
1706         PUTLONG(lcp_magic, pktp);
1707         fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1708     }
1709 }
1710
1711 /*
1712  * lcp_echo_lowerup - Start the timer for the LCP frame
1713  */
1714
1715 static void
1716 lcp_echo_lowerup (unit)
1717     int unit;
1718 {
1719     fsm *f = &lcp_fsm[unit];
1720
1721     /* Clear the parameters for generating echo frames */
1722     lcp_echos_pending      = 0;
1723     lcp_echo_number        = 0;
1724     lcp_echo_timer_running = 0;
1725   
1726     /* If a timeout interval is specified then start the timer */
1727     if (lcp_echo_interval != 0)
1728         LcpEchoCheck (f);
1729 }
1730
1731 /*
1732  * lcp_echo_lowerdown - Stop the timer for the LCP frame
1733  */
1734
1735 static void
1736 lcp_echo_lowerdown (unit)
1737     int unit;
1738 {
1739     fsm *f = &lcp_fsm[unit];
1740
1741     if (lcp_echo_timer_running != 0) {
1742         UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1743         lcp_echo_timer_running = 0;
1744     }
1745 }