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