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