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