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