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