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