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