Obfuscate RCS ID matching so that CVS doesn't expand it.
[netbsd-mini2440.git] / dist / pppd / pppd / lcp.c
blobc6858330b0c066a8ba0b90af4061cfcc2afbf2f5
1 /* $NetBSD: lcp.c,v 1.2 2005/02/20 10:47:17 cube Exp $ */
3 /*
4 * lcp.c - PPP Link Control Protocol.
6 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
20 * 3. The name "Carnegie Mellon University" must not be used to
21 * endorse or promote products derived from this software without
22 * prior written permission. For permission or any legal
23 * details, please contact
24 * Office of Technology Transfer
25 * Carnegie Mellon University
26 * 5000 Forbes Avenue
27 * Pittsburgh, PA 15213-3890
28 * (412) 268-4387, fax: (412) 268-7395
29 * tech-transfer@andrew.cmu.edu
31 * 4. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by Computing Services
34 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 #include <sys/cdefs.h>
46 #ifndef lint
47 #if 0
48 #define RCSID "Id: lcp.c,v 1.76 2006/05/22 00:04:07 paulus Exp"
49 #else
50 __RCSID("$NetBSD: lcp.c,v 1.2 2005/02/20 10:47:17 cube Exp $");
51 #endif
52 #endif
55 * TODO:
58 #include <stdio.h>
59 #include <string.h>
60 #include <stdlib.h>
62 #include "pppd.h"
63 #include "fsm.h"
64 #include "lcp.h"
65 #include "chap-new.h"
66 #include "magic.h"
68 #ifdef RCSID
69 static const char rcsid[] = RCSID;
70 #endif
73 * When the link comes up we want to be able to wait for a short while,
74 * or until seeing some input from the peer, before starting to send
75 * configure-requests. We do this by delaying the fsm_lowerup call.
77 /* steal a bit in fsm flags word */
78 #define DELAYED_UP 0x100
80 static void lcp_delayed_up __P((void *));
83 * LCP-related command-line options.
85 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
86 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
87 bool lax_recv = 0; /* accept control chars in asyncmap */
88 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
90 static int noopt __P((char **));
92 #ifdef HAVE_MULTILINK
93 static int setendpoint __P((char **));
94 static void printendpoint __P((option_t *, void (*)(void *, char *, ...),
95 void *));
96 #endif /* HAVE_MULTILINK */
98 static option_t lcp_option_list[] = {
99 /* LCP options */
100 { "-all", o_special_noarg, (void *)noopt,
101 "Don't request/allow any LCP options" },
103 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
104 "Disable address/control compression",
105 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
106 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
107 "Disable address/control compression",
108 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
110 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
111 "Set asyncmap (for received packets)",
112 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
113 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
114 "Set asyncmap (for received packets)",
115 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
116 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
117 "Disable asyncmap negotiation",
118 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
119 &lcp_allowoptions[0].neg_asyncmap },
120 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
121 "Disable asyncmap negotiation",
122 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
123 &lcp_allowoptions[0].neg_asyncmap },
125 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
126 "Disable magic number negotiation (looped-back line detection)",
127 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
128 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
129 "Disable magic number negotiation (looped-back line detection)",
130 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
132 { "mru", o_int, &lcp_wantoptions[0].mru,
133 "Set MRU (maximum received packet size) for negotiation",
134 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
135 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
136 "Disable MRU negotiation (use default 1500)",
137 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
138 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
139 "Disable MRU negotiation (use default 1500)",
140 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
142 { "mtu", o_int, &lcp_allowoptions[0].mru,
143 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
145 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
146 "Disable protocol field compression",
147 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
148 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
149 "Disable protocol field compression",
150 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
152 { "passive", o_bool, &lcp_wantoptions[0].passive,
153 "Set passive mode", 1 },
154 { "-p", o_bool, &lcp_wantoptions[0].passive,
155 "Set passive mode", OPT_ALIAS | 1 },
157 { "silent", o_bool, &lcp_wantoptions[0].silent,
158 "Set silent mode", 1 },
160 { "lcp-echo-failure", o_int, &lcp_echo_fails,
161 "Set number of consecutive echo failures to indicate link failure",
162 OPT_PRIO },
163 { "lcp-echo-interval", o_int, &lcp_echo_interval,
164 "Set time in seconds between LCP echo requests", OPT_PRIO },
165 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
166 "Set time in seconds between LCP retransmissions", OPT_PRIO },
167 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
168 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
169 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
170 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
171 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
172 "Set limit on number of LCP configure-naks", OPT_PRIO },
174 { "receive-all", o_bool, &lax_recv,
175 "Accept all received control characters", 1 },
177 #ifdef HAVE_MULTILINK
178 { "mrru", o_int, &lcp_wantoptions[0].mrru,
179 "Maximum received packet size for multilink bundle",
180 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
182 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
183 "Use short sequence numbers in multilink headers",
184 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
185 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
186 "Don't use short sequence numbers in multilink headers",
187 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
189 { "endpoint", o_special, (void *) setendpoint,
190 "Endpoint discriminator for multilink",
191 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
192 #endif /* HAVE_MULTILINK */
194 { "noendpoint", o_bool, &noendpoint,
195 "Don't send or accept multilink endpoint discriminator", 1 },
197 {NULL}
200 /* global vars */
201 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
202 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
203 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
204 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
205 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
207 /* Hook for LCP up */
208 void (*lcp_up_hook) __P((void)) = NULL;
210 /* Hook for LCP down */
211 void (*lcp_down_hook) __P((void)) = NULL;
213 /* Hook for sending an LCP echo request, argument is pending count */
214 void (*lcp_echo_hook) __P((int)) = NULL;
216 /* Hook for receiving an LCP echo reply, argument is whether it's ours */
217 void (*lcp_echoreply_hook) __P((int)) = NULL;
219 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
220 static int lcp_echo_number = 0; /* ID number of next echo frame */
221 static int lcp_echo_timer_running = 0; /* set if a timer is running */
223 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
226 * Callbacks for fsm code. (CI = Configuration Information)
228 static void lcp_resetci __P((fsm *)); /* Reset our CI */
229 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
230 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
231 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
232 static int lcp_nakci __P((fsm *, u_char *, int, int)); /* Peer nak'd our CI */
233 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
234 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
235 static void lcp_up __P((fsm *)); /* We're UP */
236 static void lcp_down __P((fsm *)); /* We're DOWN */
237 static void lcp_starting __P((fsm *)); /* We need lower layer up */
238 static void lcp_finished __P((fsm *)); /* We need lower layer down */
239 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
240 static void lcp_rprotrej __P((fsm *, u_char *, int));
243 * routines to send LCP echos to peer
246 static void lcp_echo_lowerup __P((int));
247 static void lcp_echo_lowerdown __P((int));
248 static void LcpEchoTimeout __P((void *));
249 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
250 static void LcpSendEchoRequest __P((fsm *));
251 static void LcpLinkFailure __P((fsm *));
252 static void LcpEchoCheck __P((fsm *));
254 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
255 lcp_resetci, /* Reset our Configuration Information */
256 lcp_cilen, /* Length of our Configuration Information */
257 lcp_addci, /* Add our Configuration Information */
258 lcp_ackci, /* ACK our Configuration Information */
259 lcp_nakci, /* NAK our Configuration Information */
260 lcp_rejci, /* Reject our Configuration Information */
261 lcp_reqci, /* Request peer's Configuration Information */
262 lcp_up, /* Called when fsm reaches OPENED state */
263 lcp_down, /* Called when fsm leaves OPENED state */
264 lcp_starting, /* Called when we want the lower layer up */
265 lcp_finished, /* Called when we want the lower layer down */
266 NULL, /* Called when Protocol-Reject received */
267 NULL, /* Retransmission is necessary */
268 lcp_extcode, /* Called to handle LCP-specific codes */
269 "LCP" /* String name of protocol */
273 * Protocol entry points.
274 * Some of these are called directly.
277 static void lcp_init __P((int));
278 static void lcp_input __P((int, u_char *, int));
279 static void lcp_protrej __P((int));
280 static int lcp_printpkt __P((u_char *, int,
281 void (*) __P((void *, char *, ...)), void *));
283 struct protent lcp_protent = {
284 PPP_LCP,
285 lcp_init,
286 lcp_input,
287 lcp_protrej,
288 lcp_lowerup,
289 lcp_lowerdown,
290 lcp_open,
291 lcp_close,
292 lcp_printpkt,
293 NULL,
295 "LCP",
296 NULL,
297 lcp_option_list,
298 NULL,
299 NULL,
300 NULL
303 int lcp_loopbackfail = DEFLOOPBACKFAIL;
306 * Length of each type of configuration option (in octets)
308 #define CILEN_VOID 2
309 #define CILEN_CHAR 3
310 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
311 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
312 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
313 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
314 #define CILEN_CBCP 3
316 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
317 (x) == CONFNAK ? "NAK" : "REJ")
320 * noopt - Disable all options (why?).
322 static int
323 noopt(argv)
324 char **argv;
326 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
327 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
329 return (1);
332 #ifdef HAVE_MULTILINK
333 static int
334 setendpoint(argv)
335 char **argv;
337 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
338 lcp_wantoptions[0].neg_endpoint = 1;
339 return 1;
341 option_error("Can't parse '%s' as an endpoint discriminator", *argv);
342 return 0;
345 static void
346 printendpoint(opt, printer, arg)
347 option_t *opt;
348 void (*printer) __P((void *, char *, ...));
349 void *arg;
351 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
353 #endif /* HAVE_MULTILINK */
356 * lcp_init - Initialize LCP.
358 static void
359 lcp_init(unit)
360 int unit;
362 fsm *f = &lcp_fsm[unit];
363 lcp_options *wo = &lcp_wantoptions[unit];
364 lcp_options *ao = &lcp_allowoptions[unit];
366 f->unit = unit;
367 f->protocol = PPP_LCP;
368 f->callbacks = &lcp_callbacks;
370 fsm_init(f);
372 BZERO(wo, sizeof(*wo));
373 wo->neg_mru = 1;
374 wo->mru = DEFMRU;
375 wo->neg_asyncmap = 1;
376 wo->neg_magicnumber = 1;
377 wo->neg_pcompression = 1;
378 wo->neg_accompression = 1;
380 BZERO(ao, sizeof(*ao));
381 ao->neg_mru = 1;
382 ao->mru = MAXMRU;
383 ao->neg_asyncmap = 1;
384 ao->neg_chap = 1;
385 ao->chap_mdtype = chap_mdtype_all;
386 ao->neg_upap = 1;
387 ao->neg_eap = 1;
388 ao->neg_magicnumber = 1;
389 ao->neg_pcompression = 1;
390 ao->neg_accompression = 1;
391 ao->neg_endpoint = 1;
396 * lcp_open - LCP is allowed to come up.
398 void
399 lcp_open(unit)
400 int unit;
402 fsm *f = &lcp_fsm[unit];
403 lcp_options *wo = &lcp_wantoptions[unit];
405 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
406 if (wo->passive)
407 f->flags |= OPT_PASSIVE;
408 if (wo->silent)
409 f->flags |= OPT_SILENT;
410 fsm_open(f);
415 * lcp_close - Take LCP down.
417 void
418 lcp_close(unit, reason)
419 int unit;
420 char *reason;
422 fsm *f = &lcp_fsm[unit];
424 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
425 new_phase(PHASE_TERMINATE);
426 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
428 * This action is not strictly according to the FSM in RFC1548,
429 * but it does mean that the program terminates if you do a
430 * lcp_close() in passive/silent mode when a connection hasn't
431 * been established.
433 f->state = CLOSED;
434 lcp_finished(f);
436 } else
437 fsm_close(f, reason);
442 * lcp_lowerup - The lower layer is up.
444 void
445 lcp_lowerup(unit)
446 int unit;
448 lcp_options *wo = &lcp_wantoptions[unit];
449 fsm *f = &lcp_fsm[unit];
452 * Don't use A/C or protocol compression on transmission,
453 * but accept A/C and protocol compressed packets
454 * if we are going to ask for A/C and protocol compression.
456 if (ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0) < 0
457 || ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
458 wo->neg_pcompression, wo->neg_accompression) < 0)
459 return;
460 peer_mru[unit] = PPP_MRU;
462 if (listen_time != 0) {
463 f->flags |= DELAYED_UP;
464 timeout(lcp_delayed_up, f, 0, listen_time * 1000);
465 } else
466 fsm_lowerup(f);
471 * lcp_lowerdown - The lower layer is down.
473 void
474 lcp_lowerdown(unit)
475 int unit;
477 fsm *f = &lcp_fsm[unit];
479 if (f->flags & DELAYED_UP)
480 f->flags &= ~DELAYED_UP;
481 else
482 fsm_lowerdown(&lcp_fsm[unit]);
487 * lcp_delayed_up - Bring the lower layer up now.
489 static void
490 lcp_delayed_up(arg)
491 void *arg;
493 fsm *f = arg;
495 if (f->flags & DELAYED_UP) {
496 f->flags &= ~DELAYED_UP;
497 fsm_lowerup(f);
503 * lcp_input - Input LCP packet.
505 static void
506 lcp_input(unit, p, len)
507 int unit;
508 u_char *p;
509 int len;
511 fsm *f = &lcp_fsm[unit];
513 if (f->flags & DELAYED_UP) {
514 f->flags &= ~DELAYED_UP;
515 fsm_lowerup(f);
517 fsm_input(f, p, len);
521 * lcp_extcode - Handle a LCP-specific code.
523 static int
524 lcp_extcode(f, code, id, inp, len)
525 fsm *f;
526 int code, id;
527 u_char *inp;
528 int len;
530 u_char *magp;
532 switch( code ){
533 case PROTREJ:
534 lcp_rprotrej(f, inp, len);
535 break;
537 case ECHOREQ:
538 if (f->state != OPENED)
539 break;
540 magp = inp;
541 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
542 fsm_sdata(f, ECHOREP, id, inp, len);
543 break;
545 case ECHOREP:
546 lcp_received_echo_reply(f, id, inp, len);
547 break;
549 case DISCREQ:
550 case IDENTIF:
551 case TIMEREM:
552 break;
554 default:
555 return 0;
557 return 1;
562 * lcp_rprotrej - Receive an Protocol-Reject.
564 * Figure out which protocol is rejected and inform it.
566 static void
567 lcp_rprotrej(f, inp, len)
568 fsm *f;
569 u_char *inp;
570 int len;
572 int i;
573 struct protent *protp;
574 u_short prot;
575 const char *pname;
577 if (len < 2) {
578 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
579 return;
582 GETSHORT(prot, inp);
585 * Protocol-Reject packets received in any state other than the LCP
586 * OPENED state SHOULD be silently discarded.
588 if( f->state != OPENED ){
589 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
590 return;
593 pname = protocol_name(prot);
596 * Upcall the proper Protocol-Reject routine.
598 for (i = 0; (protp = protocols[i]) != NULL; ++i)
599 if (protp->protocol == prot && protp->enabled_flag) {
600 if (pname == NULL)
601 dbglog("Protocol-Reject for 0x%x received", prot);
602 else
603 dbglog("Protocol-Reject for '%s' (0x%x) received", pname,
604 prot);
605 (*protp->protrej)(f->unit);
606 return;
609 if (pname == NULL)
610 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
611 else
612 warn("Protocol-Reject for unsupported protocol '%s' (0x%x)", pname,
613 prot);
618 * lcp_protrej - A Protocol-Reject was received.
620 /*ARGSUSED*/
621 static void
622 lcp_protrej(unit)
623 int unit;
626 * Can't reject LCP!
628 error("Received Protocol-Reject for LCP!");
629 fsm_protreject(&lcp_fsm[unit]);
634 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
636 void
637 lcp_sprotrej(unit, p, len)
638 int unit;
639 u_char *p;
640 int len;
643 * Send back the protocol and the information field of the
644 * rejected packet. We only get here if LCP is in the OPENED state.
646 p += 2;
647 len -= 2;
649 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
650 p, len);
655 * lcp_resetci - Reset our CI.
657 static void
658 lcp_resetci(f)
659 fsm *f;
661 lcp_options *wo = &lcp_wantoptions[f->unit];
662 lcp_options *go = &lcp_gotoptions[f->unit];
663 lcp_options *ao = &lcp_allowoptions[f->unit];
665 wo->magicnumber = magic();
666 wo->numloops = 0;
667 *go = *wo;
668 if (!multilink) {
669 go->neg_mrru = 0;
670 go->neg_ssnhf = 0;
671 go->neg_endpoint = 0;
673 if (noendpoint)
674 ao->neg_endpoint = 0;
675 peer_mru[f->unit] = PPP_MRU;
676 auth_reset(f->unit);
681 * lcp_cilen - Return length of our CI.
683 static int
684 lcp_cilen(f)
685 fsm *f;
687 lcp_options *go = &lcp_gotoptions[f->unit];
689 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
690 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
691 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
692 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
693 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
694 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
696 * NB: we only ask for one of CHAP, UPAP, or EAP, even if we will
697 * accept more than one. We prefer EAP first, then CHAP, then
698 * PAP.
700 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
701 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
702 LENCISHORT(go->neg_eap) +
703 LENCICHAP(!go->neg_eap && go->neg_chap) +
704 LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
705 LENCILQR(go->neg_lqr) +
706 LENCICBCP(go->neg_cbcp) +
707 LENCILONG(go->neg_magicnumber) +
708 LENCIVOID(go->neg_pcompression) +
709 LENCIVOID(go->neg_accompression) +
710 LENCISHORT(go->neg_mrru) +
711 LENCIVOID(go->neg_ssnhf) +
712 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
717 * lcp_addci - Add our desired CIs to a packet.
719 static void
720 lcp_addci(f, ucp, lenp)
721 fsm *f;
722 u_char *ucp;
723 int *lenp;
725 lcp_options *go = &lcp_gotoptions[f->unit];
726 u_char *start_ucp = ucp;
728 #define ADDCIVOID(opt, neg) \
729 if (neg) { \
730 PUTCHAR(opt, ucp); \
731 PUTCHAR(CILEN_VOID, ucp); \
733 #define ADDCISHORT(opt, neg, val) \
734 if (neg) { \
735 PUTCHAR(opt, ucp); \
736 PUTCHAR(CILEN_SHORT, ucp); \
737 PUTSHORT(val, ucp); \
739 #define ADDCICHAP(opt, neg, val) \
740 if (neg) { \
741 PUTCHAR((opt), ucp); \
742 PUTCHAR(CILEN_CHAP, ucp); \
743 PUTSHORT(PPP_CHAP, ucp); \
744 PUTCHAR((CHAP_DIGEST(val)), ucp); \
746 #define ADDCILONG(opt, neg, val) \
747 if (neg) { \
748 PUTCHAR(opt, ucp); \
749 PUTCHAR(CILEN_LONG, ucp); \
750 PUTLONG(val, ucp); \
752 #define ADDCILQR(opt, neg, val) \
753 if (neg) { \
754 PUTCHAR(opt, ucp); \
755 PUTCHAR(CILEN_LQR, ucp); \
756 PUTSHORT(PPP_LQR, ucp); \
757 PUTLONG(val, ucp); \
759 #define ADDCICHAR(opt, neg, val) \
760 if (neg) { \
761 PUTCHAR(opt, ucp); \
762 PUTCHAR(CILEN_CHAR, ucp); \
763 PUTCHAR(val, ucp); \
765 #define ADDCIENDP(opt, neg, class, val, len) \
766 if (neg) { \
767 int i; \
768 PUTCHAR(opt, ucp); \
769 PUTCHAR(CILEN_CHAR + len, ucp); \
770 PUTCHAR(class, ucp); \
771 for (i = 0; i < len; ++i) \
772 PUTCHAR(val[i], ucp); \
775 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
776 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
777 go->asyncmap);
778 ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
779 ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
780 ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
781 PPP_PAP);
782 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
783 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
784 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
785 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
786 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
787 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
788 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
789 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
790 go->endpoint.value, go->endpoint.length);
792 if (ucp - start_ucp != *lenp) {
793 /* this should never happen, because peer_mtu should be 1500 */
794 error("Bug in lcp_addci: wrong length");
800 * lcp_ackci - Ack our CIs.
801 * This should not modify any state if the Ack is bad.
803 * Returns:
804 * 0 - Ack was bad.
805 * 1 - Ack was good.
807 static int
808 lcp_ackci(f, p, len)
809 fsm *f;
810 u_char *p;
811 int len;
813 lcp_options *go = &lcp_gotoptions[f->unit];
814 u_char cilen, citype, cichar;
815 u_short cishort;
816 u_int32_t cilong;
819 * CIs must be in exactly the same order that we sent.
820 * Check packet length and CI length at each step.
821 * If we find any deviations, then this packet is bad.
823 #define ACKCIVOID(opt, neg) \
824 if (neg) { \
825 if ((len -= CILEN_VOID) < 0) \
826 goto bad; \
827 GETCHAR(citype, p); \
828 GETCHAR(cilen, p); \
829 if (cilen != CILEN_VOID || \
830 citype != opt) \
831 goto bad; \
833 #define ACKCISHORT(opt, neg, val) \
834 if (neg) { \
835 if ((len -= CILEN_SHORT) < 0) \
836 goto bad; \
837 GETCHAR(citype, p); \
838 GETCHAR(cilen, p); \
839 if (cilen != CILEN_SHORT || \
840 citype != opt) \
841 goto bad; \
842 GETSHORT(cishort, p); \
843 if (cishort != val) \
844 goto bad; \
846 #define ACKCICHAR(opt, neg, val) \
847 if (neg) { \
848 if ((len -= CILEN_CHAR) < 0) \
849 goto bad; \
850 GETCHAR(citype, p); \
851 GETCHAR(cilen, p); \
852 if (cilen != CILEN_CHAR || \
853 citype != opt) \
854 goto bad; \
855 GETCHAR(cichar, p); \
856 if (cichar != val) \
857 goto bad; \
859 #define ACKCICHAP(opt, neg, val) \
860 if (neg) { \
861 if ((len -= CILEN_CHAP) < 0) \
862 goto bad; \
863 GETCHAR(citype, p); \
864 GETCHAR(cilen, p); \
865 if (cilen != CILEN_CHAP || \
866 citype != (opt)) \
867 goto bad; \
868 GETSHORT(cishort, p); \
869 if (cishort != PPP_CHAP) \
870 goto bad; \
871 GETCHAR(cichar, p); \
872 if (cichar != (CHAP_DIGEST(val))) \
873 goto bad; \
875 #define ACKCILONG(opt, neg, val) \
876 if (neg) { \
877 if ((len -= CILEN_LONG) < 0) \
878 goto bad; \
879 GETCHAR(citype, p); \
880 GETCHAR(cilen, p); \
881 if (cilen != CILEN_LONG || \
882 citype != opt) \
883 goto bad; \
884 GETLONG(cilong, p); \
885 if (cilong != val) \
886 goto bad; \
888 #define ACKCILQR(opt, neg, val) \
889 if (neg) { \
890 if ((len -= CILEN_LQR) < 0) \
891 goto bad; \
892 GETCHAR(citype, p); \
893 GETCHAR(cilen, p); \
894 if (cilen != CILEN_LQR || \
895 citype != opt) \
896 goto bad; \
897 GETSHORT(cishort, p); \
898 if (cishort != PPP_LQR) \
899 goto bad; \
900 GETLONG(cilong, p); \
901 if (cilong != val) \
902 goto bad; \
904 #define ACKCIENDP(opt, neg, class, val, vlen) \
905 if (neg) { \
906 int i; \
907 if ((len -= CILEN_CHAR + vlen) < 0) \
908 goto bad; \
909 GETCHAR(citype, p); \
910 GETCHAR(cilen, p); \
911 if (cilen != CILEN_CHAR + vlen || \
912 citype != opt) \
913 goto bad; \
914 GETCHAR(cichar, p); \
915 if (cichar != class) \
916 goto bad; \
917 for (i = 0; i < vlen; ++i) { \
918 GETCHAR(cichar, p); \
919 if (cichar != val[i]) \
920 goto bad; \
924 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
925 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
926 go->asyncmap);
927 ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
928 ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
929 ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
930 PPP_PAP);
931 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
932 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
933 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
934 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
935 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
936 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
937 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
938 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
939 go->endpoint.value, go->endpoint.length);
942 * If there are any remaining CIs, then this packet is bad.
944 if (len != 0)
945 goto bad;
946 return (1);
947 bad:
948 LCPDEBUG(("lcp_acki: received bad Ack!"));
949 return (0);
954 * lcp_nakci - Peer has sent a NAK for some of our CIs.
955 * This should not modify any state if the Nak is bad
956 * or if LCP is in the OPENED state.
958 * Returns:
959 * 0 - Nak was bad.
960 * 1 - Nak was good.
962 static int
963 lcp_nakci(f, p, len, treat_as_reject)
964 fsm *f;
965 u_char *p;
966 int len;
967 int treat_as_reject;
969 lcp_options *go = &lcp_gotoptions[f->unit];
970 lcp_options *wo = &lcp_wantoptions[f->unit];
971 u_char citype, cichar, *next;
972 u_short cishort;
973 u_int32_t cilong;
974 lcp_options no; /* options we've seen Naks for */
975 lcp_options try; /* options to request next time */
976 int looped_back = 0;
977 int cilen;
979 BZERO(&no, sizeof(no));
980 try = *go;
983 * Any Nak'd CIs must be in exactly the same order that we sent.
984 * Check packet length and CI length at each step.
985 * If we find any deviations, then this packet is bad.
987 #define NAKCIVOID(opt, neg) \
988 if (go->neg && \
989 len >= CILEN_VOID && \
990 p[1] == CILEN_VOID && \
991 p[0] == opt) { \
992 len -= CILEN_VOID; \
993 INCPTR(CILEN_VOID, p); \
994 no.neg = 1; \
995 try.neg = 0; \
997 #define NAKCICHAP(opt, neg, code) \
998 if (go->neg && \
999 len >= CILEN_CHAP && \
1000 p[1] == CILEN_CHAP && \
1001 p[0] == opt) { \
1002 len -= CILEN_CHAP; \
1003 INCPTR(2, p); \
1004 GETSHORT(cishort, p); \
1005 GETCHAR(cichar, p); \
1006 no.neg = 1; \
1007 code \
1009 #define NAKCICHAR(opt, neg, code) \
1010 if (go->neg && \
1011 len >= CILEN_CHAR && \
1012 p[1] == CILEN_CHAR && \
1013 p[0] == opt) { \
1014 len -= CILEN_CHAR; \
1015 INCPTR(2, p); \
1016 GETCHAR(cichar, p); \
1017 no.neg = 1; \
1018 code \
1020 #define NAKCISHORT(opt, neg, code) \
1021 if (go->neg && \
1022 len >= CILEN_SHORT && \
1023 p[1] == CILEN_SHORT && \
1024 p[0] == opt) { \
1025 len -= CILEN_SHORT; \
1026 INCPTR(2, p); \
1027 GETSHORT(cishort, p); \
1028 no.neg = 1; \
1029 code \
1031 #define NAKCILONG(opt, neg, code) \
1032 if (go->neg && \
1033 len >= CILEN_LONG && \
1034 p[1] == CILEN_LONG && \
1035 p[0] == opt) { \
1036 len -= CILEN_LONG; \
1037 INCPTR(2, p); \
1038 GETLONG(cilong, p); \
1039 no.neg = 1; \
1040 code \
1042 #define NAKCILQR(opt, neg, code) \
1043 if (go->neg && \
1044 len >= CILEN_LQR && \
1045 p[1] == CILEN_LQR && \
1046 p[0] == opt) { \
1047 len -= CILEN_LQR; \
1048 INCPTR(2, p); \
1049 GETSHORT(cishort, p); \
1050 GETLONG(cilong, p); \
1051 no.neg = 1; \
1052 code \
1054 #define NAKCIENDP(opt, neg) \
1055 if (go->neg && \
1056 len >= CILEN_CHAR && \
1057 p[0] == opt && \
1058 p[1] >= CILEN_CHAR && \
1059 p[1] <= len) { \
1060 len -= p[1]; \
1061 INCPTR(p[1], p); \
1062 no.neg = 1; \
1063 try.neg = 0; \
1067 * NOTE! There must be no assignments to individual fields of *go in
1068 * the code below. Any such assignment is a BUG!
1071 * We don't care if they want to send us smaller packets than
1072 * we want. Therefore, accept any MRU less than what we asked for,
1073 * but then ignore the new value when setting the MRU in the kernel.
1074 * If they send us a bigger MRU than what we asked, accept it, up to
1075 * the limit of the default MRU we'd get if we didn't negotiate.
1077 if (go->neg_mru && go->mru != DEFMRU) {
1078 NAKCISHORT(CI_MRU, neg_mru,
1079 if (cishort <= wo->mru || cishort <= DEFMRU)
1080 try.mru = cishort;
1085 * Add any characters they want to our (receive-side) asyncmap.
1087 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1088 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1089 try.asyncmap = go->asyncmap | cilong;
1094 * If they've nak'd our authentication-protocol, check whether
1095 * they are proposing a different protocol, or a different
1096 * hash algorithm for CHAP.
1098 if ((go->neg_chap || go->neg_upap || go->neg_eap)
1099 && len >= CILEN_SHORT
1100 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1101 cilen = p[1];
1102 len -= cilen;
1103 no.neg_chap = go->neg_chap;
1104 no.neg_upap = go->neg_upap;
1105 no.neg_eap = go->neg_eap;
1106 INCPTR(2, p);
1107 GETSHORT(cishort, p);
1108 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1109 /* If we were asking for EAP, then we need to stop that. */
1110 if (go->neg_eap)
1111 try.neg_eap = 0;
1113 /* If we were asking for CHAP, then we need to stop that. */
1114 else if (go->neg_chap)
1115 try.neg_chap = 0;
1117 * If we weren't asking for CHAP or EAP, then we were asking for
1118 * PAP, in which case this Nak is bad.
1120 else
1121 goto bad;
1123 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1124 GETCHAR(cichar, p);
1125 /* Stop asking for EAP, if we were. */
1126 if (go->neg_eap) {
1127 try.neg_eap = 0;
1128 /* Try to set up to use their suggestion, if possible */
1129 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1130 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1131 } else if (go->neg_chap) {
1133 * We were asking for our preferred algorithm, they must
1134 * want something different.
1136 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1137 if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1138 /* Use their suggestion if we support it ... */
1139 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1140 } else {
1141 /* ... otherwise, try our next-preferred algorithm. */
1142 try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1143 if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1144 try.neg_chap = 0;
1146 } else {
1148 * Whoops, they Nak'd our algorithm of choice
1149 * but then suggested it back to us.
1151 goto bad;
1153 } else {
1155 * Stop asking for PAP if we were asking for it.
1157 try.neg_upap = 0;
1160 } else {
1163 * If we were asking for EAP, and they're Conf-Naking EAP,
1164 * well, that's just strange. Nobody should do that.
1166 if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1167 dbglog("Unexpected Conf-Nak for EAP");
1170 * We don't recognize what they're suggesting.
1171 * Stop asking for what we were asking for.
1173 if (go->neg_eap)
1174 try.neg_eap = 0;
1175 else if (go->neg_chap)
1176 try.neg_chap = 0;
1177 else
1178 try.neg_upap = 0;
1179 p += cilen - CILEN_SHORT;
1184 * If they can't cope with our link quality protocol, we'll have
1185 * to stop asking for LQR. We haven't got any other protocol.
1186 * If they Nak the reporting period, take their value XXX ?
1188 NAKCILQR(CI_QUALITY, neg_lqr,
1189 if (cishort != PPP_LQR)
1190 try.neg_lqr = 0;
1191 else
1192 try.lqr_period = cilong;
1196 * Only implementing CBCP...not the rest of the callback options
1198 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1199 try.neg_cbcp = 0;
1203 * Check for a looped-back line.
1205 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1206 try.magicnumber = magic();
1207 looped_back = 1;
1211 * Peer shouldn't send Nak for protocol compression or
1212 * address/control compression requests; they should send
1213 * a Reject instead. If they send a Nak, treat it as a Reject.
1215 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1216 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1219 * Nak for MRRU option - accept their value if it is smaller
1220 * than the one we want.
1222 if (go->neg_mrru) {
1223 NAKCISHORT(CI_MRRU, neg_mrru,
1224 if (treat_as_reject)
1225 try.neg_mrru = 0;
1226 else if (cishort <= wo->mrru)
1227 try.mrru = cishort;
1232 * Nak for short sequence numbers shouldn't be sent, treat it
1233 * like a reject.
1235 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1238 * Nak of the endpoint discriminator option is not permitted,
1239 * treat it like a reject.
1241 NAKCIENDP(CI_EPDISC, neg_endpoint);
1244 * There may be remaining CIs, if the peer is requesting negotiation
1245 * on an option that we didn't include in our request packet.
1246 * If we see an option that we requested, or one we've already seen
1247 * in this packet, then this packet is bad.
1248 * If we wanted to respond by starting to negotiate on the requested
1249 * option(s), we could, but we don't, because except for the
1250 * authentication type and quality protocol, if we are not negotiating
1251 * an option, it is because we were told not to.
1252 * For the authentication type, the Nak from the peer means
1253 * `let me authenticate myself with you' which is a bit pointless.
1254 * For the quality protocol, the Nak means `ask me to send you quality
1255 * reports', but if we didn't ask for them, we don't want them.
1256 * An option we don't recognize represents the peer asking to
1257 * negotiate some option we don't support, so ignore it.
1259 while (len >= CILEN_VOID) {
1260 GETCHAR(citype, p);
1261 GETCHAR(cilen, p);
1262 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1263 goto bad;
1264 next = p + cilen - 2;
1266 switch (citype) {
1267 case CI_MRU:
1268 if ((go->neg_mru && go->mru != DEFMRU)
1269 || no.neg_mru || cilen != CILEN_SHORT)
1270 goto bad;
1271 GETSHORT(cishort, p);
1272 if (cishort < DEFMRU) {
1273 try.neg_mru = 1;
1274 try.mru = cishort;
1276 break;
1277 case CI_ASYNCMAP:
1278 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1279 || no.neg_asyncmap || cilen != CILEN_LONG)
1280 goto bad;
1281 break;
1282 case CI_AUTHTYPE:
1283 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1284 go->neg_eap || no.neg_eap)
1285 goto bad;
1286 break;
1287 case CI_MAGICNUMBER:
1288 if (go->neg_magicnumber || no.neg_magicnumber ||
1289 cilen != CILEN_LONG)
1290 goto bad;
1291 break;
1292 case CI_PCOMPRESSION:
1293 if (go->neg_pcompression || no.neg_pcompression
1294 || cilen != CILEN_VOID)
1295 goto bad;
1296 break;
1297 case CI_ACCOMPRESSION:
1298 if (go->neg_accompression || no.neg_accompression
1299 || cilen != CILEN_VOID)
1300 goto bad;
1301 break;
1302 case CI_QUALITY:
1303 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1304 goto bad;
1305 break;
1306 case CI_MRRU:
1307 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1308 goto bad;
1309 break;
1310 case CI_SSNHF:
1311 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1312 goto bad;
1313 try.neg_ssnhf = 1;
1314 break;
1315 case CI_EPDISC:
1316 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1317 goto bad;
1318 break;
1320 p = next;
1324 * OK, the Nak is good. Now we can update state.
1325 * If there are any options left we ignore them.
1327 if (f->state != OPENED) {
1328 if (looped_back) {
1329 if (++try.numloops >= lcp_loopbackfail) {
1330 notice("Serial line is looped back.");
1331 status = EXIT_LOOPBACK;
1332 lcp_close(f->unit, "Loopback detected");
1334 } else
1335 try.numloops = 0;
1336 *go = try;
1339 return 1;
1341 bad:
1342 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1343 return 0;
1348 * lcp_rejci - Peer has Rejected some of our CIs.
1349 * This should not modify any state if the Reject is bad
1350 * or if LCP is in the OPENED state.
1352 * Returns:
1353 * 0 - Reject was bad.
1354 * 1 - Reject was good.
1356 static int
1357 lcp_rejci(f, p, len)
1358 fsm *f;
1359 u_char *p;
1360 int len;
1362 lcp_options *go = &lcp_gotoptions[f->unit];
1363 u_char cichar;
1364 u_short cishort;
1365 u_int32_t cilong;
1366 lcp_options try; /* options to request next time */
1368 try = *go;
1371 * Any Rejected CIs must be in exactly the same order that we sent.
1372 * Check packet length and CI length at each step.
1373 * If we find any deviations, then this packet is bad.
1375 #define REJCIVOID(opt, neg) \
1376 if (go->neg && \
1377 len >= CILEN_VOID && \
1378 p[1] == CILEN_VOID && \
1379 p[0] == opt) { \
1380 len -= CILEN_VOID; \
1381 INCPTR(CILEN_VOID, p); \
1382 try.neg = 0; \
1384 #define REJCISHORT(opt, neg, val) \
1385 if (go->neg && \
1386 len >= CILEN_SHORT && \
1387 p[1] == CILEN_SHORT && \
1388 p[0] == opt) { \
1389 len -= CILEN_SHORT; \
1390 INCPTR(2, p); \
1391 GETSHORT(cishort, p); \
1392 /* Check rejected value. */ \
1393 if (cishort != val) \
1394 goto bad; \
1395 try.neg = 0; \
1397 #define REJCICHAP(opt, neg, val) \
1398 if (go->neg && \
1399 len >= CILEN_CHAP && \
1400 p[1] == CILEN_CHAP && \
1401 p[0] == opt) { \
1402 len -= CILEN_CHAP; \
1403 INCPTR(2, p); \
1404 GETSHORT(cishort, p); \
1405 GETCHAR(cichar, p); \
1406 /* Check rejected value. */ \
1407 if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1408 goto bad; \
1409 try.neg = 0; \
1410 try.neg_eap = try.neg_upap = 0; \
1412 #define REJCILONG(opt, neg, val) \
1413 if (go->neg && \
1414 len >= CILEN_LONG && \
1415 p[1] == CILEN_LONG && \
1416 p[0] == opt) { \
1417 len -= CILEN_LONG; \
1418 INCPTR(2, p); \
1419 GETLONG(cilong, p); \
1420 /* Check rejected value. */ \
1421 if (cilong != val) \
1422 goto bad; \
1423 try.neg = 0; \
1425 #define REJCILQR(opt, neg, val) \
1426 if (go->neg && \
1427 len >= CILEN_LQR && \
1428 p[1] == CILEN_LQR && \
1429 p[0] == opt) { \
1430 len -= CILEN_LQR; \
1431 INCPTR(2, p); \
1432 GETSHORT(cishort, p); \
1433 GETLONG(cilong, p); \
1434 /* Check rejected value. */ \
1435 if (cishort != PPP_LQR || cilong != val) \
1436 goto bad; \
1437 try.neg = 0; \
1439 #define REJCICBCP(opt, neg, val) \
1440 if (go->neg && \
1441 len >= CILEN_CBCP && \
1442 p[1] == CILEN_CBCP && \
1443 p[0] == opt) { \
1444 len -= CILEN_CBCP; \
1445 INCPTR(2, p); \
1446 GETCHAR(cichar, p); \
1447 /* Check rejected value. */ \
1448 if (cichar != val) \
1449 goto bad; \
1450 try.neg = 0; \
1452 #define REJCIENDP(opt, neg, class, val, vlen) \
1453 if (go->neg && \
1454 len >= CILEN_CHAR + vlen && \
1455 p[0] == opt && \
1456 p[1] == CILEN_CHAR + vlen) { \
1457 int i; \
1458 len -= CILEN_CHAR + vlen; \
1459 INCPTR(2, p); \
1460 GETCHAR(cichar, p); \
1461 if (cichar != class) \
1462 goto bad; \
1463 for (i = 0; i < vlen; ++i) { \
1464 GETCHAR(cichar, p); \
1465 if (cichar != val[i]) \
1466 goto bad; \
1468 try.neg = 0; \
1471 REJCISHORT(CI_MRU, neg_mru, go->mru);
1472 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1473 REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1474 if (!go->neg_eap) {
1475 REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1476 if (!go->neg_chap) {
1477 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1480 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1481 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1482 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1483 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1484 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1485 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1486 REJCIVOID(CI_SSNHF, neg_ssnhf);
1487 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1488 go->endpoint.value, go->endpoint.length);
1491 * If there are any remaining CIs, then this packet is bad.
1493 if (len != 0)
1494 goto bad;
1496 * Now we can update state.
1498 if (f->state != OPENED)
1499 *go = try;
1500 return 1;
1502 bad:
1503 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1504 return 0;
1509 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1511 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1512 * appropriately. If reject_if_disagree is non-zero, doesn't return
1513 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1515 static int
1516 lcp_reqci(f, inp, lenp, reject_if_disagree)
1517 fsm *f;
1518 u_char *inp; /* Requested CIs */
1519 int *lenp; /* Length of requested CIs */
1520 int reject_if_disagree;
1522 lcp_options *go = &lcp_gotoptions[f->unit];
1523 lcp_options *ho = &lcp_hisoptions[f->unit];
1524 lcp_options *ao = &lcp_allowoptions[f->unit];
1525 u_char *cip, *next; /* Pointer to current and next CIs */
1526 int cilen, citype, cichar; /* Parsed len, type, char value */
1527 u_short cishort; /* Parsed short value */
1528 u_int32_t cilong; /* Parse long value */
1529 int rc = CONFACK; /* Final packet return code */
1530 int orc; /* Individual option return code */
1531 u_char *p; /* Pointer to next char to parse */
1532 u_char *rejp; /* Pointer to next char in reject frame */
1533 u_char *nakp; /* Pointer to next char in Nak frame */
1534 int l = *lenp; /* Length left */
1537 * Reset all his options.
1539 BZERO(ho, sizeof(*ho));
1542 * Process all his options.
1544 next = inp;
1545 nakp = nak_buffer;
1546 rejp = inp;
1547 while (l) {
1548 orc = CONFACK; /* Assume success */
1549 cip = p = next; /* Remember begining of CI */
1550 if (l < 2 || /* Not enough data for CI header or */
1551 p[1] < 2 || /* CI length too small or */
1552 p[1] > l) { /* CI length too big? */
1553 LCPDEBUG(("lcp_reqci: bad CI length!"));
1554 orc = CONFREJ; /* Reject bad CI */
1555 cilen = l; /* Reject till end of packet */
1556 l = 0; /* Don't loop again */
1557 citype = 0;
1558 goto endswitch;
1560 GETCHAR(citype, p); /* Parse CI type */
1561 GETCHAR(cilen, p); /* Parse CI length */
1562 l -= cilen; /* Adjust remaining length */
1563 next += cilen; /* Step to next CI */
1565 switch (citype) { /* Check CI type */
1566 case CI_MRU:
1567 if (!ao->neg_mru || /* Allow option? */
1568 cilen != CILEN_SHORT) { /* Check CI length */
1569 orc = CONFREJ; /* Reject CI */
1570 break;
1572 GETSHORT(cishort, p); /* Parse MRU */
1575 * He must be able to receive at least our minimum.
1576 * No need to check a maximum. If he sends a large number,
1577 * we'll just ignore it.
1579 if (cishort < MINMRU) {
1580 orc = CONFNAK; /* Nak CI */
1581 PUTCHAR(CI_MRU, nakp);
1582 PUTCHAR(CILEN_SHORT, nakp);
1583 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1584 break;
1586 ho->neg_mru = 1; /* Remember he sent MRU */
1587 ho->mru = cishort; /* And remember value */
1588 break;
1590 case CI_ASYNCMAP:
1591 if (!ao->neg_asyncmap ||
1592 cilen != CILEN_LONG) {
1593 orc = CONFREJ;
1594 break;
1596 GETLONG(cilong, p);
1599 * Asyncmap must have set at least the bits
1600 * which are set in lcp_allowoptions[unit].asyncmap.
1602 if ((ao->asyncmap & ~cilong) != 0) {
1603 orc = CONFNAK;
1604 PUTCHAR(CI_ASYNCMAP, nakp);
1605 PUTCHAR(CILEN_LONG, nakp);
1606 PUTLONG(ao->asyncmap | cilong, nakp);
1607 break;
1609 ho->neg_asyncmap = 1;
1610 ho->asyncmap = cilong;
1611 break;
1613 case CI_AUTHTYPE:
1614 if (cilen < CILEN_SHORT ||
1615 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1617 * Reject the option if we're not willing to authenticate.
1619 dbglog("No auth is possible");
1620 orc = CONFREJ;
1621 break;
1623 GETSHORT(cishort, p);
1626 * Authtype must be PAP, CHAP, or EAP.
1628 * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1629 * ao->neg_eap are set, and the peer sends a Configure-Request
1630 * with two or more authenticate-protocol requests, then we will
1631 * reject the second request.
1632 * Whether we end up doing CHAP, UPAP, or EAP depends then on
1633 * the ordering of the CIs in the peer's Configure-Request.
1636 if (cishort == PPP_PAP) {
1637 /* we've already accepted CHAP or EAP */
1638 if (ho->neg_chap || ho->neg_eap ||
1639 cilen != CILEN_SHORT) {
1640 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1641 orc = CONFREJ;
1642 break;
1644 if (!ao->neg_upap) { /* we don't want to do PAP */
1645 orc = CONFNAK; /* NAK it and suggest CHAP or EAP */
1646 PUTCHAR(CI_AUTHTYPE, nakp);
1647 if (ao->neg_eap) {
1648 PUTCHAR(CILEN_SHORT, nakp);
1649 PUTSHORT(PPP_EAP, nakp);
1650 } else {
1651 PUTCHAR(CILEN_CHAP, nakp);
1652 PUTSHORT(PPP_CHAP, nakp);
1653 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1655 break;
1657 ho->neg_upap = 1;
1658 break;
1660 if (cishort == PPP_CHAP) {
1661 /* we've already accepted PAP or EAP */
1662 if (ho->neg_upap || ho->neg_eap ||
1663 cilen != CILEN_CHAP) {
1664 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1665 orc = CONFREJ;
1666 break;
1668 if (!ao->neg_chap) { /* we don't want to do CHAP */
1669 orc = CONFNAK; /* NAK it and suggest EAP or PAP */
1670 PUTCHAR(CI_AUTHTYPE, nakp);
1671 PUTCHAR(CILEN_SHORT, nakp);
1672 if (ao->neg_eap) {
1673 PUTSHORT(PPP_EAP, nakp);
1674 } else {
1675 PUTSHORT(PPP_PAP, nakp);
1677 break;
1679 GETCHAR(cichar, p); /* get digest type */
1680 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1682 * We can't/won't do the requested type,
1683 * suggest something else.
1685 orc = CONFNAK;
1686 PUTCHAR(CI_AUTHTYPE, nakp);
1687 PUTCHAR(CILEN_CHAP, nakp);
1688 PUTSHORT(PPP_CHAP, nakp);
1689 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1690 break;
1692 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1693 ho->neg_chap = 1;
1694 break;
1696 if (cishort == PPP_EAP) {
1697 /* we've already accepted CHAP or PAP */
1698 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1699 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1700 orc = CONFREJ;
1701 break;
1703 if (!ao->neg_eap) { /* we don't want to do EAP */
1704 orc = CONFNAK; /* NAK it and suggest CHAP or PAP */
1705 PUTCHAR(CI_AUTHTYPE, nakp);
1706 if (ao->neg_chap) {
1707 PUTCHAR(CILEN_CHAP, nakp);
1708 PUTSHORT(PPP_CHAP, nakp);
1709 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1710 } else {
1711 PUTCHAR(CILEN_SHORT, nakp);
1712 PUTSHORT(PPP_PAP, nakp);
1714 break;
1716 ho->neg_eap = 1;
1717 break;
1721 * We don't recognize the protocol they're asking for.
1722 * Nak it with something we're willing to do.
1723 * (At this point we know ao->neg_upap || ao->neg_chap ||
1724 * ao->neg_eap.)
1726 orc = CONFNAK;
1727 PUTCHAR(CI_AUTHTYPE, nakp);
1728 if (ao->neg_eap) {
1729 PUTCHAR(CILEN_SHORT, nakp);
1730 PUTSHORT(PPP_EAP, nakp);
1731 } else if (ao->neg_chap) {
1732 PUTCHAR(CILEN_CHAP, nakp);
1733 PUTSHORT(PPP_CHAP, nakp);
1734 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1735 } else {
1736 PUTCHAR(CILEN_SHORT, nakp);
1737 PUTSHORT(PPP_PAP, nakp);
1739 break;
1741 case CI_QUALITY:
1742 if (!ao->neg_lqr ||
1743 cilen != CILEN_LQR) {
1744 orc = CONFREJ;
1745 break;
1748 GETSHORT(cishort, p);
1749 GETLONG(cilong, p);
1752 * Check the protocol and the reporting period.
1753 * XXX When should we Nak this, and what with?
1755 if (cishort != PPP_LQR) {
1756 orc = CONFNAK;
1757 PUTCHAR(CI_QUALITY, nakp);
1758 PUTCHAR(CILEN_LQR, nakp);
1759 PUTSHORT(PPP_LQR, nakp);
1760 PUTLONG(ao->lqr_period, nakp);
1761 break;
1763 break;
1765 case CI_MAGICNUMBER:
1766 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1767 cilen != CILEN_LONG) {
1768 orc = CONFREJ;
1769 break;
1771 GETLONG(cilong, p);
1774 * He must have a different magic number.
1776 if (go->neg_magicnumber &&
1777 cilong == go->magicnumber) {
1778 cilong = magic(); /* Don't put magic() inside macro! */
1779 orc = CONFNAK;
1780 PUTCHAR(CI_MAGICNUMBER, nakp);
1781 PUTCHAR(CILEN_LONG, nakp);
1782 PUTLONG(cilong, nakp);
1783 break;
1785 ho->neg_magicnumber = 1;
1786 ho->magicnumber = cilong;
1787 break;
1790 case CI_PCOMPRESSION:
1791 if (!ao->neg_pcompression ||
1792 cilen != CILEN_VOID) {
1793 orc = CONFREJ;
1794 break;
1796 ho->neg_pcompression = 1;
1797 break;
1799 case CI_ACCOMPRESSION:
1800 if (!ao->neg_accompression ||
1801 cilen != CILEN_VOID) {
1802 orc = CONFREJ;
1803 break;
1805 ho->neg_accompression = 1;
1806 break;
1808 case CI_MRRU:
1809 if (!ao->neg_mrru || !multilink ||
1810 cilen != CILEN_SHORT) {
1811 orc = CONFREJ;
1812 break;
1815 GETSHORT(cishort, p);
1816 /* possibly should insist on a minimum/maximum MRRU here */
1817 ho->neg_mrru = 1;
1818 ho->mrru = cishort;
1819 break;
1821 case CI_SSNHF:
1822 if (!ao->neg_ssnhf || !multilink ||
1823 cilen != CILEN_VOID) {
1824 orc = CONFREJ;
1825 break;
1827 ho->neg_ssnhf = 1;
1828 break;
1830 case CI_EPDISC:
1831 if (!ao->neg_endpoint ||
1832 cilen < CILEN_CHAR ||
1833 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1834 orc = CONFREJ;
1835 break;
1837 GETCHAR(cichar, p);
1838 cilen -= CILEN_CHAR;
1839 ho->neg_endpoint = 1;
1840 ho->endpoint.class = cichar;
1841 ho->endpoint.length = cilen;
1842 BCOPY(p, ho->endpoint.value, cilen);
1843 INCPTR(cilen, p);
1844 break;
1846 default:
1847 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1848 orc = CONFREJ;
1849 break;
1852 endswitch:
1853 if (orc == CONFACK && /* Good CI */
1854 rc != CONFACK) /* but prior CI wasnt? */
1855 continue; /* Don't send this one */
1857 if (orc == CONFNAK) { /* Nak this CI? */
1858 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1859 && citype != CI_MAGICNUMBER) {
1860 orc = CONFREJ; /* Get tough if so */
1861 } else {
1862 if (rc == CONFREJ) /* Rejecting prior CI? */
1863 continue; /* Don't send this one */
1864 rc = CONFNAK;
1867 if (orc == CONFREJ) { /* Reject this CI */
1868 rc = CONFREJ;
1869 if (cip != rejp) /* Need to move rejected CI? */
1870 BCOPY(cip, rejp, cilen); /* Move it */
1871 INCPTR(cilen, rejp); /* Update output pointer */
1876 * If we wanted to send additional NAKs (for unsent CIs), the
1877 * code would go here. The extra NAKs would go at *nakp.
1878 * At present there are no cases where we want to ask the
1879 * peer to negotiate an option.
1882 switch (rc) {
1883 case CONFACK:
1884 *lenp = next - inp;
1885 break;
1886 case CONFNAK:
1888 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1890 *lenp = nakp - nak_buffer;
1891 BCOPY(nak_buffer, inp, *lenp);
1892 break;
1893 case CONFREJ:
1894 *lenp = rejp - inp;
1895 break;
1898 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1899 return (rc); /* Return final code */
1904 * lcp_up - LCP has come UP.
1906 static void
1907 lcp_up(f)
1908 fsm *f;
1910 lcp_options *wo = &lcp_wantoptions[f->unit];
1911 lcp_options *ho = &lcp_hisoptions[f->unit];
1912 lcp_options *go = &lcp_gotoptions[f->unit];
1913 lcp_options *ao = &lcp_allowoptions[f->unit];
1914 int mtu, mru;
1916 if (!go->neg_magicnumber)
1917 go->magicnumber = 0;
1918 if (!ho->neg_magicnumber)
1919 ho->magicnumber = 0;
1922 * Set our MTU to the smaller of the MTU we wanted and
1923 * the MRU our peer wanted. If we negotiated an MRU,
1924 * set our MRU to the larger of value we wanted and
1925 * the value we got in the negotiation.
1926 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1927 * the interface MTU is set to the lowest of that, the
1928 * MTU we want to use, and our link MRU.
1930 mtu = ho->neg_mru? ho->mru: PPP_MRU;
1931 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1932 #ifdef HAVE_MULTILINK
1933 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1934 #endif /* HAVE_MULTILINK */
1935 netif_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1936 ppp_send_config(f->unit, mtu,
1937 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1938 ho->neg_pcompression, ho->neg_accompression);
1939 ppp_recv_config(f->unit, mru,
1940 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1941 go->neg_pcompression, go->neg_accompression);
1943 if (ho->neg_mru)
1944 peer_mru[f->unit] = ho->mru;
1946 if (lcp_up_hook) (*lcp_up_hook)();
1948 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1950 link_established(f->unit);
1955 * lcp_down - LCP has gone DOWN.
1957 * Alert other protocols.
1959 static void
1960 lcp_down(f)
1961 fsm *f;
1963 lcp_options *go = &lcp_gotoptions[f->unit];
1965 lcp_echo_lowerdown(f->unit);
1967 link_down(f->unit);
1969 if (lcp_down_hook) (*lcp_down_hook)();
1971 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1972 ppp_recv_config(f->unit, PPP_MRU,
1973 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1974 go->neg_pcompression, go->neg_accompression);
1975 peer_mru[f->unit] = PPP_MRU;
1980 * lcp_starting - LCP needs the lower layer up.
1982 static void
1983 lcp_starting(f)
1984 fsm *f;
1986 link_required(f->unit);
1991 * lcp_finished - LCP has finished with the lower layer.
1993 static void
1994 lcp_finished(f)
1995 fsm *f;
1997 link_terminated(f->unit);
2002 * lcp_printpkt - print the contents of an LCP packet.
2004 static char *lcp_codenames[] = {
2005 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2006 "TermReq", "TermAck", "CodeRej", "ProtRej",
2007 "EchoReq", "EchoRep", "DiscReq", "Ident",
2008 "TimeRem"
2011 static int
2012 lcp_printpkt(p, plen, printer, arg)
2013 u_char *p;
2014 int plen;
2015 void (*printer) __P((void *, char *, ...));
2016 void *arg;
2018 int code, id, len, olen, i;
2019 u_char *pstart, *optend;
2020 u_short cishort;
2021 u_int32_t cilong;
2023 if (plen < HEADERLEN)
2024 return 0;
2025 pstart = p;
2026 GETCHAR(code, p);
2027 GETCHAR(id, p);
2028 GETSHORT(len, p);
2029 if (len < HEADERLEN || len > plen)
2030 return 0;
2032 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
2033 printer(arg, " %s", lcp_codenames[code-1]);
2034 else
2035 printer(arg, " code=0x%x", code);
2036 printer(arg, " id=0x%x", id);
2037 len -= HEADERLEN;
2038 switch (code) {
2039 case CONFREQ:
2040 case CONFACK:
2041 case CONFNAK:
2042 case CONFREJ:
2043 /* print option list */
2044 while (len >= 2) {
2045 GETCHAR(code, p);
2046 GETCHAR(olen, p);
2047 p -= 2;
2048 if (olen < 2 || olen > len) {
2049 break;
2051 printer(arg, " <");
2052 len -= olen;
2053 optend = p + olen;
2054 switch (code) {
2055 case CI_MRU:
2056 if (olen == CILEN_SHORT) {
2057 p += 2;
2058 GETSHORT(cishort, p);
2059 printer(arg, "mru %d", cishort);
2061 break;
2062 case CI_ASYNCMAP:
2063 if (olen == CILEN_LONG) {
2064 p += 2;
2065 GETLONG(cilong, p);
2066 printer(arg, "asyncmap 0x%x", cilong);
2068 break;
2069 case CI_AUTHTYPE:
2070 if (olen >= CILEN_SHORT) {
2071 p += 2;
2072 printer(arg, "auth ");
2073 GETSHORT(cishort, p);
2074 switch (cishort) {
2075 case PPP_PAP:
2076 printer(arg, "pap");
2077 break;
2078 case PPP_CHAP:
2079 printer(arg, "chap");
2080 if (p < optend) {
2081 switch (*p) {
2082 case CHAP_MD5:
2083 printer(arg, " MD5");
2084 ++p;
2085 break;
2086 #ifdef CHAPMS
2087 case CHAP_MICROSOFT:
2088 printer(arg, " MS");
2089 ++p;
2090 break;
2092 case CHAP_MICROSOFT_V2:
2093 printer(arg, " MS-v2");
2094 ++p;
2095 break;
2096 #endif
2099 break;
2100 case PPP_EAP:
2101 printer(arg, "eap");
2102 break;
2103 default:
2104 printer(arg, "0x%x", cishort);
2107 break;
2108 case CI_QUALITY:
2109 if (olen >= CILEN_SHORT) {
2110 p += 2;
2111 printer(arg, "quality ");
2112 GETSHORT(cishort, p);
2113 switch (cishort) {
2114 case PPP_LQR:
2115 printer(arg, "lqr");
2116 break;
2117 default:
2118 printer(arg, "0x%x", cishort);
2121 break;
2122 case CI_CALLBACK:
2123 if (olen >= CILEN_CHAR) {
2124 p += 2;
2125 printer(arg, "callback ");
2126 GETCHAR(cishort, p);
2127 switch (cishort) {
2128 case CBCP_OPT:
2129 printer(arg, "CBCP");
2130 break;
2131 default:
2132 printer(arg, "0x%x", cishort);
2135 break;
2136 case CI_MAGICNUMBER:
2137 if (olen == CILEN_LONG) {
2138 p += 2;
2139 GETLONG(cilong, p);
2140 printer(arg, "magic 0x%x", cilong);
2142 break;
2143 case CI_PCOMPRESSION:
2144 if (olen == CILEN_VOID) {
2145 p += 2;
2146 printer(arg, "pcomp");
2148 break;
2149 case CI_ACCOMPRESSION:
2150 if (olen == CILEN_VOID) {
2151 p += 2;
2152 printer(arg, "accomp");
2154 break;
2155 case CI_MRRU:
2156 if (olen == CILEN_SHORT) {
2157 p += 2;
2158 GETSHORT(cishort, p);
2159 printer(arg, "mrru %d", cishort);
2161 break;
2162 case CI_SSNHF:
2163 if (olen == CILEN_VOID) {
2164 p += 2;
2165 printer(arg, "ssnhf");
2167 break;
2168 case CI_EPDISC:
2169 #ifdef HAVE_MULTILINK
2170 if (olen >= CILEN_CHAR) {
2171 struct epdisc epd;
2172 p += 2;
2173 GETCHAR(epd.class, p);
2174 epd.length = olen - CILEN_CHAR;
2175 if (epd.length > MAX_ENDP_LEN)
2176 epd.length = MAX_ENDP_LEN;
2177 if (epd.length > 0) {
2178 BCOPY(p, epd.value, epd.length);
2179 p += epd.length;
2181 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2183 #else
2184 printer(arg, "endpoint");
2185 #endif
2186 break;
2188 while (p < optend) {
2189 GETCHAR(code, p);
2190 printer(arg, " %.2x", code);
2192 printer(arg, ">");
2194 break;
2196 case TERMACK:
2197 case TERMREQ:
2198 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2199 printer(arg, " ");
2200 print_string((char *)p, len, printer, arg);
2201 p += len;
2202 len = 0;
2204 break;
2206 case ECHOREQ:
2207 case ECHOREP:
2208 case DISCREQ:
2209 if (len >= 4) {
2210 GETLONG(cilong, p);
2211 printer(arg, " magic=0x%x", cilong);
2212 len -= 4;
2214 break;
2216 case IDENTIF:
2217 case TIMEREM:
2218 if (len >= 4) {
2219 GETLONG(cilong, p);
2220 printer(arg, " magic=0x%x", cilong);
2221 len -= 4;
2223 if (code == TIMEREM) {
2224 if (len < 4)
2225 break;
2226 GETLONG(cilong, p);
2227 printer(arg, " seconds=%u", cilong);
2228 len -= 4;
2230 if (len > 0) {
2231 printer(arg, " ");
2232 print_string((char *)p, len, printer, arg);
2233 p += len;
2234 len = 0;
2236 break;
2239 /* print the rest of the bytes in the packet */
2240 for (i = 0; i < len && i < 32; ++i) {
2241 GETCHAR(code, p);
2242 printer(arg, " %.2x", code);
2244 if (i < len) {
2245 printer(arg, " ...");
2246 p += len - i;
2249 return p - pstart;
2253 * Time to shut down the link because there is nothing out there.
2256 static
2257 void LcpLinkFailure (f)
2258 fsm *f;
2260 if (f->state == OPENED) {
2261 info("No response to %d echo-requests", lcp_echos_pending);
2262 notice("Serial link appears to be disconnected.");
2263 status = EXIT_PEER_DEAD;
2264 lcp_close(f->unit, "Peer not responding");
2269 * Timer expired for the LCP echo requests from this process.
2272 static void
2273 LcpEchoCheck (f)
2274 fsm *f;
2276 LcpSendEchoRequest (f);
2277 if (f->state != OPENED)
2278 return;
2281 * Start the timer for the next interval.
2283 if (lcp_echo_timer_running)
2284 warn("assertion lcp_echo_timer_running==0 failed");
2285 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2286 lcp_echo_timer_running = 1;
2290 * LcpEchoTimeout - Timer expired on the LCP echo
2293 static void
2294 LcpEchoTimeout (arg)
2295 void *arg;
2297 if (lcp_echo_timer_running != 0) {
2298 lcp_echo_timer_running = 0;
2299 LcpEchoCheck ((fsm *) arg);
2304 * LcpEchoReply - LCP has received a reply to the echo
2307 static void
2308 lcp_received_echo_reply (f, id, inp, len)
2309 fsm *f;
2310 int id;
2311 u_char *inp;
2312 int len;
2314 u_int32_t magic;
2316 /* Check the magic number - don't count replies from ourselves. */
2317 if (len < 4) {
2318 dbglog("lcp: received short Echo-Reply, length %d", len);
2319 return;
2321 GETLONG(magic, inp);
2322 if (lcp_gotoptions[f->unit].neg_magicnumber
2323 && magic == lcp_gotoptions[f->unit].magicnumber) {
2324 warn("appear to have received our own echo-reply!");
2325 if (lcp_echoreply_hook) (*lcp_echoreply_hook)(1);
2326 return;
2328 if (lcp_echoreply_hook) (*lcp_echoreply_hook)(0);
2330 /* Reset the number of outstanding echo frames */
2331 lcp_echos_pending = 0;
2335 * LcpSendEchoRequest - Send an echo request frame to the peer
2338 static void
2339 LcpSendEchoRequest (f)
2340 fsm *f;
2342 u_int32_t lcp_magic;
2343 u_char pkt[4], *pktp;
2346 * Detect the failure of the peer at this point.
2348 if (lcp_echo_fails != 0) {
2349 if (lcp_echos_pending >= lcp_echo_fails) {
2350 LcpLinkFailure(f);
2351 lcp_echos_pending = 0;
2356 * Make and send the echo request frame.
2358 if (f->state == OPENED) {
2359 if (lcp_echo_hook) (*lcp_echo_hook)(lcp_echos_pending);
2360 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2361 pktp = pkt;
2362 PUTLONG(lcp_magic, pktp);
2363 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2364 ++lcp_echos_pending;
2369 * lcp_echo_lowerup - Start the timer for the LCP frame
2372 static void
2373 lcp_echo_lowerup (unit)
2374 int unit;
2376 fsm *f = &lcp_fsm[unit];
2378 /* Clear the parameters for generating echo frames */
2379 lcp_echos_pending = 0;
2380 lcp_echo_number = 0;
2381 lcp_echo_timer_running = 0;
2383 /* If a timeout interval is specified then start the timer */
2384 if (lcp_echo_interval != 0)
2385 LcpEchoCheck (f);
2389 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2392 static void
2393 lcp_echo_lowerdown (unit)
2394 int unit;
2396 fsm *f = &lcp_fsm[unit];
2398 if (lcp_echo_timer_running != 0) {
2399 UNTIMEOUT (LcpEchoTimeout, f);
2400 lcp_echo_timer_running = 0;