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