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