CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / pppd / lcp.c
blobfb75af5f7c6334d0465c7f4ede0b120c09840701
1 /*
2 * lcp.c - PPP Link Control Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
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.
20 #define RCSID "$Id: lcp.c,v 1.4 2004/08/12 02:56:55 tallest Exp $"
23 * TODO:
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/sysinfo.h>
30 #include <time.h>
31 #include <unistd.h>
33 #include <pppd.h>
34 #include "fsm.h"
35 #include "lcp.h"
36 #include "chap.h"
37 #include "magic.h"
40 static const char rcsid[] = RCSID;
43 * When the link comes up we want to be able to wait for a short while,
44 * or until seeing some input from the peer, before starting to send
45 * configure-requests. We do this by delaying the fsm_lowerup call.
47 /* steal a bit in fsm flags word */
48 #define DELAYED_UP 0x100
50 static void lcp_delayed_up __P((void *));
53 * LCP-related command-line options.
55 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
56 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
57 bool lax_recv = 0; /* accept control chars in asyncmap */
58 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
60 static int noopt __P((char **));
62 static time_t echo_time = 0;
64 #ifdef HAVE_MULTILINK
65 static int setendpoint __P((char **));
66 static void printendpoint __P((option_t *, void (*)(void *, char *, ...),
67 void *));
68 #endif /* HAVE_MULTILINK */
70 static option_t lcp_option_list[] = {
71 /* LCP options */
72 { "-all", o_special_noarg, (void *)noopt,
73 "Don't request/allow any LCP options" },
75 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
76 "Disable address/control compression",
77 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
78 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
79 "Disable address/control compression",
80 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
82 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
83 "Set asyncmap (for received packets)",
84 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
85 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
86 "Set asyncmap (for received packets)",
87 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
88 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
89 "Disable asyncmap negotiation",
90 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
91 &lcp_allowoptions[0].neg_asyncmap },
92 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
93 "Disable asyncmap negotiation",
94 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
95 &lcp_allowoptions[0].neg_asyncmap },
97 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
98 "Disable magic number negotiation (looped-back line detection)",
99 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
100 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
101 "Disable magic number negotiation (looped-back line detection)",
102 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
104 { "mru", o_int, &lcp_wantoptions[0].mru,
105 "Set MRU (maximum received packet size) for negotiation",
106 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
107 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
108 "Disable MRU negotiation (use default 1500)",
109 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
110 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
111 "Disable MRU negotiation (use default 1500)",
112 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
114 { "mtu", o_int, &lcp_allowoptions[0].mru,
115 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
117 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
118 "Disable protocol field compression",
119 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
120 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
121 "Disable protocol field compression",
122 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
124 { "passive", o_bool, &lcp_wantoptions[0].passive,
125 "Set passive mode", 1 },
126 { "-p", o_bool, &lcp_wantoptions[0].passive,
127 "Set passive mode", OPT_ALIAS | 1 },
129 { "silent", o_bool, &lcp_wantoptions[0].silent,
130 "Set silent mode", 1 },
132 { "lcp-echo-failure", o_int, &lcp_echo_fails,
133 "Set number of consecutive echo failures to indicate link failure",
134 OPT_PRIO },
135 { "lcp-echo-interval", o_int, &lcp_echo_interval,
136 "Set time in seconds between LCP echo requests", OPT_PRIO },
137 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
138 "Set time in seconds between LCP retransmissions", OPT_PRIO },
139 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
140 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
141 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
142 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
143 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
144 "Set limit on number of LCP configure-naks", OPT_PRIO },
146 { "receive-all", o_bool, &lax_recv,
147 "Accept all received control characters", 1 },
149 #ifdef HAVE_MULTILINK
150 { "mrru", o_int, &lcp_wantoptions[0].mrru,
151 "Maximum received packet size for multilink bundle",
152 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
154 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
155 "Use short sequence numbers in multilink headers",
156 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
157 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
158 "Don't use short sequence numbers in multilink headers",
159 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
161 { "endpoint", o_special, (void *) setendpoint,
162 "Endpoint discriminator for multilink",
163 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
164 #endif /* HAVE_MULTILINK */
166 { "noendpoint", o_bool, &noendpoint,
167 "Don't send or accept multilink endpoint discriminator", 1 },
169 {NULL}
172 /* global vars */
173 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
174 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
175 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
176 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
177 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
179 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
180 static int lcp_echo_number = 0; /* ID number of next echo frame */
181 static int lcp_echo_timer_running = 0; /* set if a timer is running */
183 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
184 // static int now_unit;
187 * Callbacks for fsm code. (CI = Configuration Information)
189 static void lcp_resetci __P((fsm *)); /* Reset our CI */
190 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
191 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
192 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
193 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
194 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
195 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
196 static void lcp_up __P((fsm *)); /* We're UP */
197 static void lcp_down __P((fsm *)); /* We're DOWN */
198 static void lcp_starting __P((fsm *)); /* We need lower layer up */
199 static void lcp_finished __P((fsm *)); /* We need lower layer down */
200 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
201 static void lcp_rprotrej __P((fsm *, u_char *, int));
204 * routines to send LCP echos to peer
207 static void lcp_echo_lowerup __P((int));
208 static void lcp_echo_lowerdown __P((int));
209 static void LcpEchoTimeout __P((void *));
210 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
211 static void LcpSendEchoRequest __P((fsm *));
212 static void LcpLinkFailure __P((fsm *));
213 static void LcpEchoCheck __P((fsm *));
215 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
216 lcp_resetci, /* Reset our Configuration Information */
217 lcp_cilen, /* Length of our Configuration Information */
218 lcp_addci, /* Add our Configuration Information */
219 lcp_ackci, /* ACK our Configuration Information */
220 lcp_nakci, /* NAK our Configuration Information */
221 lcp_rejci, /* Reject our Configuration Information */
222 lcp_reqci, /* Request peer's Configuration Information */
223 lcp_up, /* Called when fsm reaches OPENED state */
224 lcp_down, /* Called when fsm leaves OPENED state */
225 lcp_starting, /* Called when we want the lower layer up */
226 lcp_finished, /* Called when we want the lower layer down */
227 NULL, /* Called when Protocol-Reject received */
228 NULL, /* Retransmission is necessary */
229 lcp_extcode, /* Called to handle LCP-specific codes */
230 "LCP" /* String name of protocol */
234 * Protocol entry points.
235 * Some of these are called directly.
238 static void lcp_init __P((int));
239 static void lcp_input __P((int, u_char *, int));
240 static void lcp_protrej __P((int));
241 static int lcp_printpkt __P((u_char *, int,
242 void (*) __P((void *, char *, ...)), void *));
244 struct protent lcp_protent = {
245 PPP_LCP,
246 lcp_init,
247 lcp_input,
248 lcp_protrej,
249 lcp_lowerup,
250 lcp_lowerdown,
251 lcp_open,
252 lcp_close,
253 lcp_printpkt,
254 NULL,
256 "LCP",
257 NULL,
258 lcp_option_list,
259 NULL,
260 NULL,
261 NULL
264 int lcp_loopbackfail = DEFLOOPBACKFAIL;
267 * Length of each type of configuration option (in octets)
269 #define CILEN_VOID 2
270 #define CILEN_CHAR 3
271 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
272 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
273 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
274 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
275 #define CILEN_CBCP 3
277 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
278 (x) == CONFNAK ? "NAK" : "REJ")
280 time_t get_time(void)
282 struct sysinfo info;
284 sysinfo(&info);
286 return info.uptime;
290 * noopt - Disable all options (why?).
292 static int
293 noopt(argv)
294 char **argv;
296 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
297 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
299 return (1);
302 #ifdef HAVE_MULTILINK
303 static int
304 setendpoint(argv)
305 char **argv;
307 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
308 lcp_wantoptions[0].neg_endpoint = 1;
309 return 1;
311 option_error("Can't parse '%s' as an endpoint discriminator", *argv);
312 return 0;
315 static void
316 printendpoint(opt, printer, arg)
317 option_t *opt;
318 void (*printer) __P((void *, char *, ...));
319 void *arg;
321 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
323 #endif /* HAVE_MULTILINK */
326 * lcp_init - Initialize LCP.
328 static void
329 lcp_init(unit)
330 int unit;
332 fsm *f = &lcp_fsm[unit];
333 lcp_options *wo = &lcp_wantoptions[unit];
334 lcp_options *ao = &lcp_allowoptions[unit];
336 f->unit = unit;
337 f->protocol = PPP_LCP;
338 f->callbacks = &lcp_callbacks;
340 fsm_init(f);
342 BZERO(wo, sizeof(*wo));
343 wo->neg_mru = 1;
344 wo->mru = DEFMRU;
345 wo->neg_asyncmap = 1;
346 wo->chap_mdtype = CHAP_DIGEST_MD5;
347 wo->neg_magicnumber = 1;
348 wo->neg_pcompression = 1;
349 wo->neg_accompression = 1;
351 BZERO(ao, sizeof(*ao));
352 ao->neg_mru = 1;
353 ao->mru = MAXMRU;
354 ao->neg_asyncmap = 1;
355 ao->neg_chap = 1;
356 ao->chap_mdtype = CHAP_DIGEST_MD5;
357 ao->neg_upap = 1;
358 ao->neg_magicnumber = 1;
359 ao->neg_pcompression = 1;
360 ao->neg_accompression = 1;
361 #ifdef CBCP_SUPPORT
362 ao->neg_cbcp = 1;
363 #endif
364 ao->neg_endpoint = 1;
369 * lcp_open - LCP is allowed to come up.
371 void
372 lcp_open(unit)
373 int unit;
375 fsm *f = &lcp_fsm[unit];
376 lcp_options *wo = &lcp_wantoptions[unit];
378 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
379 if (wo->passive)
380 f->flags |= OPT_PASSIVE;
381 if (wo->silent)
382 f->flags |= OPT_SILENT;
383 fsm_open(f);
388 * lcp_close - Take LCP down.
390 void
391 lcp_close(unit, reason)
392 int unit;
393 char *reason;
395 fsm *f = &lcp_fsm[unit];
397 if (phase != PHASE_DEAD)
398 new_phase(PHASE_TERMINATE);
399 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
401 * This action is not strictly according to the FSM in RFC1548,
402 * but it does mean that the program terminates if you do a
403 * lcp_close() in passive/silent mode when a connection hasn't
404 * been established.
406 f->state = CLOSED;
407 lcp_finished(f);
409 } else
410 fsm_close(&lcp_fsm[unit], reason);
415 * lcp_lowerup - The lower layer is up.
417 void
418 lcp_lowerup(unit)
419 int unit;
421 lcp_options *wo = &lcp_wantoptions[unit];
422 fsm *f = &lcp_fsm[unit];
425 * Don't use A/C or protocol compression on transmission,
426 * but accept A/C and protocol compressed packets
427 * if we are going to ask for A/C and protocol compression.
429 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
430 ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
431 wo->neg_pcompression, wo->neg_accompression);
432 peer_mru[unit] = PPP_MRU;
434 if (listen_time != 0) {
435 f->flags |= DELAYED_UP;
436 timeout(lcp_delayed_up, f, 0, listen_time * 1000);
437 } else
438 fsm_lowerup(f);
443 * lcp_lowerdown - The lower layer is down.
445 void
446 lcp_lowerdown(unit)
447 int unit;
449 fsm *f = &lcp_fsm[unit];
451 if (f->flags & DELAYED_UP)
452 f->flags &= ~DELAYED_UP;
453 else
454 fsm_lowerdown(&lcp_fsm[unit]);
459 * lcp_delayed_up - Bring the lower layer up now.
461 static void
462 lcp_delayed_up(arg)
463 void *arg;
465 fsm *f = arg;
467 if (f->flags & DELAYED_UP) {
468 f->flags &= ~DELAYED_UP;
469 fsm_lowerup(f);
475 * lcp_input - Input LCP packet.
477 static void
478 lcp_input(unit, p, len)
479 int unit;
480 u_char *p;
481 int len;
483 fsm *f = &lcp_fsm[unit];
485 if (f->flags & DELAYED_UP) {
486 f->flags &= ~DELAYED_UP;
487 fsm_lowerup(f);
489 fsm_input(f, p, len);
494 * lcp_extcode - Handle a LCP-specific code.
496 static int
497 lcp_extcode(f, code, id, inp, len)
498 fsm *f;
499 int code, id;
500 u_char *inp;
501 int len;
503 u_char *magp;
505 if ((code > 0) && (code < 16) && (code_log_details[code][0] != '#')) {
506 LOGX_INFO("Received %s %s", PNAME(f), code_log_details[code]);
509 switch( code ){
510 case PROTREJ:
511 lcp_rprotrej(f, inp, len);
512 break;
514 case ECHOREQ:
515 if (f->state != OPENED)
516 break;
517 magp = inp;
518 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
519 fsm_sdata(f, ECHOREP, id, inp, len);
521 /* Reset the number of outstanding echo frames */
522 lcp_echos_pending = 0;
523 echo_time = get_time();
524 break;
526 case ECHOREP:
527 lcp_received_echo_reply(f, id, inp, len);
528 break;
530 case DISCREQ:
531 break;
533 default:
534 return 0;
536 return 1;
541 * lcp_rprotrej - Receive an Protocol-Reject.
543 * Figure out which protocol is rejected and inform it.
545 static void
546 lcp_rprotrej(f, inp, len)
547 fsm *f;
548 u_char *inp;
549 int len;
551 int i;
552 struct protent *protp;
553 u_short prot;
555 if (len < 2) {
556 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
557 return;
560 GETSHORT(prot, inp);
563 * Protocol-Reject packets received in any state other than the LCP
564 * OPENED state SHOULD be silently discarded.
566 if( f->state != OPENED ){
567 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
568 return;
572 * Upcall the proper Protocol-Reject routine.
574 for (i = 0; (protp = protocols[i]) != NULL; ++i)
575 if (protp->protocol == prot && protp->enabled_flag) {
576 (*protp->protrej)(f->unit);
577 return;
580 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
585 * lcp_protrej - A Protocol-Reject was received.
587 /*ARGSUSED*/
588 static void
589 lcp_protrej(unit)
590 int unit;
593 * Can't reject LCP!
596 LOGX_ERROR("Received Protocol-Reject for LCP.");
597 error("Received Protocol-Reject for LCP!");
598 fsm_protreject(&lcp_fsm[unit]);
603 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
605 void
606 lcp_sprotrej(unit, p, len)
607 int unit;
608 u_char *p;
609 int len;
612 * Send back the protocol and the information field of the
613 * rejected packet. We only get here if LCP is in the OPENED state.
615 p += 2;
616 len -= 2;
618 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
619 p, len);
624 * lcp_resetci - Reset our CI.
626 static void
627 lcp_resetci(f)
628 fsm *f;
630 lcp_options *wo = &lcp_wantoptions[f->unit];
631 lcp_options *go = &lcp_gotoptions[f->unit];
632 lcp_options *ao = &lcp_allowoptions[f->unit];
634 wo->magicnumber = magic();
635 wo->numloops = 0;
636 *go = *wo;
637 if (!multilink) {
638 go->neg_mrru = 0;
639 go->neg_ssnhf = 0;
640 go->neg_endpoint = 0;
642 if (noendpoint)
643 ao->neg_endpoint = 0;
644 peer_mru[f->unit] = PPP_MRU;
645 auth_reset(f->unit);
650 * lcp_cilen - Return length of our CI.
652 static int
653 lcp_cilen(f)
654 fsm *f;
656 lcp_options *go = &lcp_gotoptions[f->unit];
658 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
659 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
660 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
661 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
662 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
663 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
665 * NB: we only ask for one of CHAP and UPAP, even if we will
666 * accept either.
668 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
669 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
670 LENCICHAP(go->neg_chap) +
671 LENCISHORT(!go->neg_chap && go->neg_upap) +
672 LENCILQR(go->neg_lqr) +
673 LENCICBCP(go->neg_cbcp) +
674 LENCILONG(go->neg_magicnumber) +
675 LENCIVOID(go->neg_pcompression) +
676 LENCIVOID(go->neg_accompression) +
677 LENCISHORT(go->neg_mrru) +
678 LENCIVOID(go->neg_ssnhf) +
679 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
684 * lcp_addci - Add our desired CIs to a packet.
686 static void
687 lcp_addci(f, ucp, lenp)
688 fsm *f;
689 u_char *ucp;
690 int *lenp;
692 lcp_options *go = &lcp_gotoptions[f->unit];
693 u_char *start_ucp = ucp;
695 #define ADDCIVOID(opt, neg) \
696 if (neg) { \
697 PUTCHAR(opt, ucp); \
698 PUTCHAR(CILEN_VOID, ucp); \
700 #define ADDCISHORT(opt, neg, val) \
701 if (neg) { \
702 PUTCHAR(opt, ucp); \
703 PUTCHAR(CILEN_SHORT, ucp); \
704 PUTSHORT(val, ucp); \
706 #define ADDCICHAP(opt, neg, val, digest) \
707 if (neg) { \
708 PUTCHAR(opt, ucp); \
709 PUTCHAR(CILEN_CHAP, ucp); \
710 PUTSHORT(val, ucp); \
711 PUTCHAR(digest, ucp); \
713 #define ADDCILONG(opt, neg, val) \
714 if (neg) { \
715 PUTCHAR(opt, ucp); \
716 PUTCHAR(CILEN_LONG, ucp); \
717 PUTLONG(val, ucp); \
719 #define ADDCILQR(opt, neg, val) \
720 if (neg) { \
721 PUTCHAR(opt, ucp); \
722 PUTCHAR(CILEN_LQR, ucp); \
723 PUTSHORT(PPP_LQR, ucp); \
724 PUTLONG(val, ucp); \
726 #define ADDCICHAR(opt, neg, val) \
727 if (neg) { \
728 PUTCHAR(opt, ucp); \
729 PUTCHAR(CILEN_CHAR, ucp); \
730 PUTCHAR(val, ucp); \
732 #define ADDCIENDP(opt, neg, class, val, len) \
733 if (neg) { \
734 int i; \
735 PUTCHAR(opt, ucp); \
736 PUTCHAR(CILEN_CHAR + len, ucp); \
737 PUTCHAR(class, ucp); \
738 for (i = 0; i < len; ++i) \
739 PUTCHAR(val[i], ucp); \
742 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
743 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
744 go->asyncmap);
745 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
746 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
747 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
748 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
749 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
750 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
751 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
752 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
753 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
754 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
755 go->endpoint.value, go->endpoint.length);
757 if (ucp - start_ucp != *lenp) {
758 /* this should never happen, because peer_mtu should be 1500 */
759 error("Bug in lcp_addci: wrong length");
765 * lcp_ackci - Ack our CIs.
766 * This should not modify any state if the Ack is bad.
768 * Returns:
769 * 0 - Ack was bad.
770 * 1 - Ack was good.
772 static int
773 lcp_ackci(f, p, len)
774 fsm *f;
775 u_char *p;
776 int len;
778 lcp_options *go = &lcp_gotoptions[f->unit];
779 u_char cilen, citype, cichar;
780 u_short cishort;
781 u_int32_t cilong;
784 * CIs must be in exactly the same order that we sent.
785 * Check packet length and CI length at each step.
786 * If we find any deviations, then this packet is bad.
788 #define ACKCIVOID(opt, neg) \
789 if (neg) { \
790 if ((len -= CILEN_VOID) < 0) \
791 goto bad; \
792 GETCHAR(citype, p); \
793 GETCHAR(cilen, p); \
794 if (cilen != CILEN_VOID || \
795 citype != opt) \
796 goto bad; \
798 #define ACKCISHORT(opt, neg, val) \
799 if (neg) { \
800 if ((len -= CILEN_SHORT) < 0) \
801 goto bad; \
802 GETCHAR(citype, p); \
803 GETCHAR(cilen, p); \
804 if (cilen != CILEN_SHORT || \
805 citype != opt) \
806 goto bad; \
807 GETSHORT(cishort, p); \
808 if (cishort != val) \
809 goto bad; \
811 #define ACKCICHAR(opt, neg, val) \
812 if (neg) { \
813 if ((len -= CILEN_CHAR) < 0) \
814 goto bad; \
815 GETCHAR(citype, p); \
816 GETCHAR(cilen, p); \
817 if (cilen != CILEN_CHAR || \
818 citype != opt) \
819 goto bad; \
820 GETCHAR(cichar, p); \
821 if (cichar != val) \
822 goto bad; \
824 #define ACKCICHAP(opt, neg, val, digest) \
825 if (neg) { \
826 if ((len -= CILEN_CHAP) < 0) \
827 goto bad; \
828 GETCHAR(citype, p); \
829 GETCHAR(cilen, p); \
830 if (cilen != CILEN_CHAP || \
831 citype != opt) \
832 goto bad; \
833 GETSHORT(cishort, p); \
834 if (cishort != val) \
835 goto bad; \
836 GETCHAR(cichar, p); \
837 if (cichar != digest) \
838 goto bad; \
840 #define ACKCILONG(opt, neg, val) \
841 if (neg) { \
842 if ((len -= CILEN_LONG) < 0) \
843 goto bad; \
844 GETCHAR(citype, p); \
845 GETCHAR(cilen, p); \
846 if (cilen != CILEN_LONG || \
847 citype != opt) \
848 goto bad; \
849 GETLONG(cilong, p); \
850 if (cilong != val) \
851 goto bad; \
853 #define ACKCILQR(opt, neg, val) \
854 if (neg) { \
855 if ((len -= CILEN_LQR) < 0) \
856 goto bad; \
857 GETCHAR(citype, p); \
858 GETCHAR(cilen, p); \
859 if (cilen != CILEN_LQR || \
860 citype != opt) \
861 goto bad; \
862 GETSHORT(cishort, p); \
863 if (cishort != PPP_LQR) \
864 goto bad; \
865 GETLONG(cilong, p); \
866 if (cilong != val) \
867 goto bad; \
869 #define ACKCIENDP(opt, neg, class, val, vlen) \
870 if (neg) { \
871 int i; \
872 if ((len -= CILEN_CHAR + vlen) < 0) \
873 goto bad; \
874 GETCHAR(citype, p); \
875 GETCHAR(cilen, p); \
876 if (cilen != CILEN_CHAR + vlen || \
877 citype != opt) \
878 goto bad; \
879 GETCHAR(cichar, p); \
880 if (cichar != class) \
881 goto bad; \
882 for (i = 0; i < vlen; ++i) { \
883 GETCHAR(cichar, p); \
884 if (cichar != val[i]) \
885 goto bad; \
889 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
890 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
891 go->asyncmap);
892 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
893 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
894 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
895 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
896 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
897 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
898 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
899 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
900 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
901 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
902 go->endpoint.value, go->endpoint.length);
905 * If there are any remaining CIs, then this packet is bad.
907 if (len != 0)
908 goto bad;
909 return (1);
910 bad:
911 LCPDEBUG(("lcp_acki: received bad Ack!"));
912 return (0);
917 * lcp_nakci - Peer has sent a NAK for some of our CIs.
918 * This should not modify any state if the Nak is bad
919 * or if LCP is in the OPENED state.
921 * Returns:
922 * 0 - Nak was bad.
923 * 1 - Nak was good.
925 static int
926 lcp_nakci(f, p, len)
927 fsm *f;
928 u_char *p;
929 int len;
931 lcp_options *go = &lcp_gotoptions[f->unit];
932 lcp_options *wo = &lcp_wantoptions[f->unit];
933 u_char citype, cichar, *next;
934 u_short cishort;
935 u_int32_t cilong;
936 lcp_options no; /* options we've seen Naks for */
937 lcp_options try; /* options to request next time */
938 int looped_back = 0;
939 int cilen;
941 BZERO(&no, sizeof(no));
942 try = *go;
945 * Any Nak'd CIs must be in exactly the same order that we sent.
946 * Check packet length and CI length at each step.
947 * If we find any deviations, then this packet is bad.
949 #define NAKCIVOID(opt, neg) \
950 if (go->neg && \
951 len >= CILEN_VOID && \
952 p[1] == CILEN_VOID && \
953 p[0] == opt) { \
954 len -= CILEN_VOID; \
955 INCPTR(CILEN_VOID, p); \
956 no.neg = 1; \
957 try.neg = 0; \
959 #define NAKCICHAP(opt, neg, code) \
960 if (go->neg && \
961 len >= CILEN_CHAP && \
962 p[1] == CILEN_CHAP && \
963 p[0] == opt) { \
964 len -= CILEN_CHAP; \
965 INCPTR(2, p); \
966 GETSHORT(cishort, p); \
967 GETCHAR(cichar, p); \
968 no.neg = 1; \
969 code \
971 #define NAKCICHAR(opt, neg, code) \
972 if (go->neg && \
973 len >= CILEN_CHAR && \
974 p[1] == CILEN_CHAR && \
975 p[0] == opt) { \
976 len -= CILEN_CHAR; \
977 INCPTR(2, p); \
978 GETCHAR(cichar, p); \
979 no.neg = 1; \
980 code \
982 #define NAKCISHORT(opt, neg, code) \
983 if (go->neg && \
984 len >= CILEN_SHORT && \
985 p[1] == CILEN_SHORT && \
986 p[0] == opt) { \
987 len -= CILEN_SHORT; \
988 INCPTR(2, p); \
989 GETSHORT(cishort, p); \
990 no.neg = 1; \
991 code \
993 #define NAKCILONG(opt, neg, code) \
994 if (go->neg && \
995 len >= CILEN_LONG && \
996 p[1] == CILEN_LONG && \
997 p[0] == opt) { \
998 len -= CILEN_LONG; \
999 INCPTR(2, p); \
1000 GETLONG(cilong, p); \
1001 no.neg = 1; \
1002 code \
1004 #define NAKCILQR(opt, neg, code) \
1005 if (go->neg && \
1006 len >= CILEN_LQR && \
1007 p[1] == CILEN_LQR && \
1008 p[0] == opt) { \
1009 len -= CILEN_LQR; \
1010 INCPTR(2, p); \
1011 GETSHORT(cishort, p); \
1012 GETLONG(cilong, p); \
1013 no.neg = 1; \
1014 code \
1016 #define NAKCIENDP(opt, neg) \
1017 if (go->neg && \
1018 len >= CILEN_CHAR && \
1019 p[0] == opt && \
1020 p[1] >= CILEN_CHAR && \
1021 p[1] <= len) { \
1022 len -= p[1]; \
1023 INCPTR(p[1], p); \
1024 no.neg = 1; \
1025 try.neg = 0; \
1029 * We don't care if they want to send us smaller packets than
1030 * we want. Therefore, accept any MRU less than what we asked for,
1031 * but then ignore the new value when setting the MRU in the kernel.
1032 * If they send us a bigger MRU than what we asked, accept it, up to
1033 * the limit of the default MRU we'd get if we didn't negotiate.
1035 if (go->neg_mru && go->mru != DEFMRU) {
1036 NAKCISHORT(CI_MRU, neg_mru,
1037 if (cishort <= wo->mru || cishort <= DEFMRU)
1038 try.mru = cishort;
1043 * Add any characters they want to our (receive-side) asyncmap.
1045 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1046 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1047 try.asyncmap = go->asyncmap | cilong;
1052 * If they've nak'd our authentication-protocol, check whether
1053 * they are proposing a different protocol, or a different
1054 * hash algorithm for CHAP.
1056 if ((go->neg_chap || go->neg_upap)
1057 && len >= CILEN_SHORT
1058 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1059 cilen = p[1];
1060 len -= cilen;
1061 no.neg_chap = go->neg_chap;
1062 no.neg_upap = go->neg_upap;
1063 INCPTR(2, p);
1064 GETSHORT(cishort, p);
1065 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1067 * If we were asking for CHAP, they obviously don't want to do it.
1068 * If we weren't asking for CHAP, then we were asking for PAP,
1069 * in which case this Nak is bad.
1071 if (!go->neg_chap)
1072 goto bad;
1073 try.neg_chap = 0;
1075 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1076 GETCHAR(cichar, p);
1077 if (go->neg_chap) {
1079 * We were asking for CHAP/MD5; they must want a different
1080 * algorithm. If they can't do MD5, we can ask for M$-CHAP
1081 * if we support it, otherwise we'll have to stop
1082 * asking for CHAP.
1084 if (cichar != go->chap_mdtype) {
1085 #ifdef CHAPMS
1086 if (cichar == CHAP_MICROSOFT)
1087 go->chap_mdtype = CHAP_MICROSOFT;
1088 else
1089 #endif /* CHAPMS */
1090 try.neg_chap = 0;
1092 } else {
1094 * Stop asking for PAP if we were asking for it.
1096 try.neg_upap = 0;
1099 } else {
1101 * We don't recognize what they're suggesting.
1102 * Stop asking for what we were asking for.
1104 if (go->neg_chap)
1105 try.neg_chap = 0;
1106 else
1107 try.neg_upap = 0;
1108 p += cilen - CILEN_SHORT;
1112 NAKCILQR(CI_QUALITY, neg_lqr,
1113 if (cishort != PPP_LQR)
1114 try.neg_lqr = 0;
1115 else
1116 try.lqr_period = cilong;
1120 * Only implementing CBCP...not the rest of the callback options
1122 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1123 try.neg_cbcp = 0;
1127 * Check for a looped-back line.
1129 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1130 try.magicnumber = magic();
1131 looped_back = 1;
1135 * Peer shouldn't send Nak for protocol compression or
1136 * address/control compression requests; they should send
1137 * a Reject instead. If they send a Nak, treat it as a Reject.
1139 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1140 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1143 * Nak for MRRU option - accept their value if it is smaller
1144 * than the one we want.
1146 if (go->neg_mrru) {
1147 NAKCISHORT(CI_MRRU, neg_mrru,
1148 if (cishort <= wo->mrru)
1149 try.mrru = cishort;
1154 * Nak for short sequence numbers shouldn't be sent, treat it
1155 * like a reject.
1157 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1160 * Nak of the endpoint discriminator option is not permitted,
1161 * treat it like a reject.
1163 NAKCIENDP(CI_EPDISC, neg_endpoint);
1166 * There may be remaining CIs, if the peer is requesting negotiation
1167 * on an option that we didn't include in our request packet.
1168 * If we see an option that we requested, or one we've already seen
1169 * in this packet, then this packet is bad.
1170 * If we wanted to respond by starting to negotiate on the requested
1171 * option(s), we could, but we don't, because except for the
1172 * authentication type and quality protocol, if we are not negotiating
1173 * an option, it is because we were told not to.
1174 * For the authentication type, the Nak from the peer means
1175 * `let me authenticate myself with you' which is a bit pointless.
1176 * For the quality protocol, the Nak means `ask me to send you quality
1177 * reports', but if we didn't ask for them, we don't want them.
1178 * An option we don't recognize represents the peer asking to
1179 * negotiate some option we don't support, so ignore it.
1181 while (len > CILEN_VOID) {
1182 GETCHAR(citype, p);
1183 GETCHAR(cilen, p);
1184 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1185 goto bad;
1186 next = p + cilen - 2;
1188 switch (citype) {
1189 case CI_MRU:
1190 if ((go->neg_mru && go->mru != DEFMRU)
1191 || no.neg_mru || cilen != CILEN_SHORT)
1192 goto bad;
1193 GETSHORT(cishort, p);
1194 if (cishort < DEFMRU) {
1195 try.neg_mru = 1;
1196 try.mru = cishort;
1198 break;
1199 case CI_ASYNCMAP:
1200 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1201 || no.neg_asyncmap || cilen != CILEN_LONG)
1202 goto bad;
1203 break;
1204 case CI_AUTHTYPE:
1205 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
1206 goto bad;
1207 break;
1208 case CI_MAGICNUMBER:
1209 if (go->neg_magicnumber || no.neg_magicnumber ||
1210 cilen != CILEN_LONG)
1211 goto bad;
1212 break;
1213 case CI_PCOMPRESSION:
1214 if (go->neg_pcompression || no.neg_pcompression
1215 || cilen != CILEN_VOID)
1216 goto bad;
1217 break;
1218 case CI_ACCOMPRESSION:
1219 if (go->neg_accompression || no.neg_accompression
1220 || cilen != CILEN_VOID)
1221 goto bad;
1222 break;
1223 case CI_QUALITY:
1224 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1225 goto bad;
1226 break;
1227 case CI_MRRU:
1228 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1229 goto bad;
1230 break;
1231 case CI_SSNHF:
1232 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1233 goto bad;
1234 try.neg_ssnhf = 1;
1235 break;
1236 case CI_EPDISC:
1237 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1238 goto bad;
1239 break;
1241 p = next;
1245 * OK, the Nak is good. Now we can update state.
1246 * If there are any options left we ignore them.
1248 if (f->state != OPENED) {
1249 if (looped_back) {
1250 if (++try.numloops >= lcp_loopbackfail) {
1251 // LOGX_INFO("Serial line is looped back.");
1252 notice("Serial line is looped back.");
1253 lcp_close(f->unit, "Loopback detected");
1254 status = EXIT_LOOPBACK;
1256 } else
1257 try.numloops = 0;
1258 *go = try;
1261 return 1;
1263 bad:
1264 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1265 return 0;
1270 * lcp_rejci - Peer has Rejected some of our CIs.
1271 * This should not modify any state if the Reject is bad
1272 * or if LCP is in the OPENED state.
1274 * Returns:
1275 * 0 - Reject was bad.
1276 * 1 - Reject was good.
1278 static int
1279 lcp_rejci(f, p, len)
1280 fsm *f;
1281 u_char *p;
1282 int len;
1284 lcp_options *go = &lcp_gotoptions[f->unit];
1285 u_char cichar;
1286 u_short cishort;
1287 u_int32_t cilong;
1288 lcp_options try; /* options to request next time */
1290 try = *go;
1293 * Any Rejected CIs must be in exactly the same order that we sent.
1294 * Check packet length and CI length at each step.
1295 * If we find any deviations, then this packet is bad.
1297 #define REJCIVOID(opt, neg) \
1298 if (go->neg && \
1299 len >= CILEN_VOID && \
1300 p[1] == CILEN_VOID && \
1301 p[0] == opt) { \
1302 len -= CILEN_VOID; \
1303 INCPTR(CILEN_VOID, p); \
1304 try.neg = 0; \
1306 #define REJCISHORT(opt, neg, val) \
1307 if (go->neg && \
1308 len >= CILEN_SHORT && \
1309 p[1] == CILEN_SHORT && \
1310 p[0] == opt) { \
1311 len -= CILEN_SHORT; \
1312 INCPTR(2, p); \
1313 GETSHORT(cishort, p); \
1314 /* Check rejected value. */ \
1315 if (cishort != val) \
1316 goto bad; \
1317 try.neg = 0; \
1319 #define REJCICHAP(opt, neg, val, digest) \
1320 if (go->neg && \
1321 len >= CILEN_CHAP && \
1322 p[1] == CILEN_CHAP && \
1323 p[0] == opt) { \
1324 len -= CILEN_CHAP; \
1325 INCPTR(2, p); \
1326 GETSHORT(cishort, p); \
1327 GETCHAR(cichar, p); \
1328 /* Check rejected value. */ \
1329 if (cishort != val || cichar != digest) \
1330 goto bad; \
1331 try.neg = 0; \
1332 try.neg_upap = 0; \
1334 #define REJCILONG(opt, neg, val) \
1335 if (go->neg && \
1336 len >= CILEN_LONG && \
1337 p[1] == CILEN_LONG && \
1338 p[0] == opt) { \
1339 len -= CILEN_LONG; \
1340 INCPTR(2, p); \
1341 GETLONG(cilong, p); \
1342 /* Check rejected value. */ \
1343 if (cilong != val) \
1344 goto bad; \
1345 try.neg = 0; \
1347 #define REJCILQR(opt, neg, val) \
1348 if (go->neg && \
1349 len >= CILEN_LQR && \
1350 p[1] == CILEN_LQR && \
1351 p[0] == opt) { \
1352 len -= CILEN_LQR; \
1353 INCPTR(2, p); \
1354 GETSHORT(cishort, p); \
1355 GETLONG(cilong, p); \
1356 /* Check rejected value. */ \
1357 if (cishort != PPP_LQR || cilong != val) \
1358 goto bad; \
1359 try.neg = 0; \
1361 #define REJCICBCP(opt, neg, val) \
1362 if (go->neg && \
1363 len >= CILEN_CBCP && \
1364 p[1] == CILEN_CBCP && \
1365 p[0] == opt) { \
1366 len -= CILEN_CBCP; \
1367 INCPTR(2, p); \
1368 GETCHAR(cichar, p); \
1369 /* Check rejected value. */ \
1370 if (cichar != val) \
1371 goto bad; \
1372 try.neg = 0; \
1374 #define REJCIENDP(opt, neg, class, val, vlen) \
1375 if (go->neg && \
1376 len >= CILEN_CHAR + vlen && \
1377 p[0] == opt && \
1378 p[1] == CILEN_CHAR + vlen) { \
1379 int i; \
1380 len -= CILEN_CHAR + vlen; \
1381 INCPTR(2, p); \
1382 GETCHAR(cichar, p); \
1383 if (cichar != class) \
1384 goto bad; \
1385 for (i = 0; i < vlen; ++i) { \
1386 GETCHAR(cichar, p); \
1387 if (cichar != val[i]) \
1388 goto bad; \
1390 try.neg = 0; \
1393 REJCISHORT(CI_MRU, neg_mru, go->mru);
1394 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1395 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1396 if (!go->neg_chap) {
1397 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1399 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1400 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1401 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1402 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1403 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1404 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1405 REJCIVOID(CI_SSNHF, neg_ssnhf);
1406 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1407 go->endpoint.value, go->endpoint.length);
1410 * If there are any remaining CIs, then this packet is bad.
1412 if (len != 0)
1413 goto bad;
1415 * Now we can update state.
1417 if (f->state != OPENED)
1418 *go = try;
1419 return 1;
1421 bad:
1422 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1423 return 0;
1428 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1430 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1431 * appropriately. If reject_if_disagree is non-zero, doesn't return
1432 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1434 static int
1435 lcp_reqci(f, inp, lenp, reject_if_disagree)
1436 fsm *f;
1437 u_char *inp; /* Requested CIs */
1438 int *lenp; /* Length of requested CIs */
1439 int reject_if_disagree;
1441 lcp_options *go = &lcp_gotoptions[f->unit];
1442 lcp_options *ho = &lcp_hisoptions[f->unit];
1443 lcp_options *ao = &lcp_allowoptions[f->unit];
1444 u_char *cip, *next; /* Pointer to current and next CIs */
1445 int cilen, citype, cichar; /* Parsed len, type, char value */
1446 u_short cishort; /* Parsed short value */
1447 u_int32_t cilong; /* Parse long value */
1448 int rc = CONFACK; /* Final packet return code */
1449 int orc; /* Individual option return code */
1450 u_char *p; /* Pointer to next char to parse */
1451 u_char *rejp; /* Pointer to next char in reject frame */
1452 u_char *nakp; /* Pointer to next char in Nak frame */
1453 int l = *lenp; /* Length left */
1456 * Reset all his options.
1458 BZERO(ho, sizeof(*ho));
1461 * Process all his options.
1463 next = inp;
1464 nakp = nak_buffer;
1465 rejp = inp;
1466 while (l) {
1467 orc = CONFACK; /* Assume success */
1468 cip = p = next; /* Remember begining of CI */
1469 if (l < 2 || /* Not enough data for CI header or */
1470 p[1] < 2 || /* CI length too small or */
1471 p[1] > l) { /* CI length too big? */
1472 LCPDEBUG(("lcp_reqci: bad CI length!"));
1473 orc = CONFREJ; /* Reject bad CI */
1474 cilen = l; /* Reject till end of packet */
1475 l = 0; /* Don't loop again */
1476 citype = 0;
1477 goto endswitch;
1479 GETCHAR(citype, p); /* Parse CI type */
1480 GETCHAR(cilen, p); /* Parse CI length */
1481 l -= cilen; /* Adjust remaining length */
1482 next += cilen; /* Step to next CI */
1484 switch (citype) { /* Check CI type */
1485 #if 1 //fixed by crazy 20070613: bug id unknown(20070613)
1486 //FIXME: Maybe we can define 0 and 9 in the head file...
1487 // Please see line 74 on sheet 'WAN_Broadband' in document 'WG414-Q-LS_V1.0 Product Verification Plan(IN).xls'.
1488 case 0: /* Reserved */
1489 /* Same as case 9, send a Configure-Reject */
1490 // Please see line 109 on sheet 'WAN_Broadband' in document 'WG414-Q-LS_V1.0 Product Verification Plan(IN).xls'.
1491 case 9: /* Field Check Sequence (FCS) Alternatives */
1492 orc = CONFREJ;
1493 LOGX_DEBUG("%s: citype=%d", __FUNCTION__, citype);
1494 break;
1495 #endif
1496 case CI_MRU:
1497 if (!ao->neg_mru || /* Allow option? */
1498 cilen != CILEN_SHORT) { /* Check CI length */
1499 orc = CONFREJ; /* Reject CI */
1500 break;
1502 GETSHORT(cishort, p); /* Parse MRU */
1505 * He must be able to receive at least our minimum.
1506 * No need to check a maximum. If he sends a large number,
1507 * we'll just ignore it.
1509 if (cishort < MINMRU) {
1510 orc = CONFNAK; /* Nak CI */
1511 PUTCHAR(CI_MRU, nakp);
1512 PUTCHAR(CILEN_SHORT, nakp);
1513 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1514 break;
1516 #if 1 //Fixed by crazy 20070424: bug id 6439(20070423)
1517 if (cishort > REJECT_MAXMRU) {
1518 #if 0
1519 orc = CONFREJ; /* Reject CI */
1520 #else
1521 //Fix by crazy 20080121: The Test Plan is updated. Please refer to the item 1.3.13 in latest Test Plan.
1522 orc = CONFNAK; /* Nak CI */
1523 PUTCHAR(CI_MRU, nakp);
1524 PUTCHAR(CILEN_SHORT, nakp);
1525 PUTSHORT(REJECT_MAXMRU, nakp); /* Give him a hint */
1526 #endif
1527 LOGX_DEBUG("%s: cishort(%d) > REJECT_MAXMRU", __FUNCTION__, cishort);
1528 break;
1530 #endif
1531 ho->neg_mru = 1; /* Remember he sent MRU */
1532 ho->mru = cishort; /* And remember value */
1533 break;
1535 case CI_ASYNCMAP:
1536 if (!ao->neg_asyncmap ||
1537 cilen != CILEN_LONG) {
1538 orc = CONFREJ;
1539 break;
1541 GETLONG(cilong, p);
1544 * Asyncmap must have set at least the bits
1545 * which are set in lcp_allowoptions[unit].asyncmap.
1547 if ((ao->asyncmap & ~cilong) != 0) {
1548 orc = CONFNAK;
1549 PUTCHAR(CI_ASYNCMAP, nakp);
1550 PUTCHAR(CILEN_LONG, nakp);
1551 PUTLONG(ao->asyncmap | cilong, nakp);
1552 break;
1554 ho->neg_asyncmap = 1;
1555 ho->asyncmap = cilong;
1556 break;
1558 case CI_AUTHTYPE:
1559 if (cilen < CILEN_SHORT ||
1560 !(ao->neg_upap || ao->neg_chap)) {
1562 * Reject the option if we're not willing to authenticate.
1564 orc = CONFREJ;
1565 break;
1567 GETSHORT(cishort, p);
1570 * Authtype must be PAP or CHAP.
1572 * Note: if both ao->neg_upap and ao->neg_chap are set,
1573 * and the peer sends a Configure-Request with two
1574 * authenticate-protocol requests, one for CHAP and one
1575 * for UPAP, then we will reject the second request.
1576 * Whether we end up doing CHAP or UPAP depends then on
1577 * the ordering of the CIs in the peer's Configure-Request.
1580 if (cishort == PPP_PAP) {
1581 if (ho->neg_chap || /* we've already accepted CHAP */
1582 cilen != CILEN_SHORT) {
1583 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1584 orc = CONFREJ;
1585 break;
1587 if (!ao->neg_upap) { /* we don't want to do PAP */
1588 orc = CONFNAK; /* NAK it and suggest CHAP */
1589 PUTCHAR(CI_AUTHTYPE, nakp);
1590 PUTCHAR(CILEN_CHAP, nakp);
1591 PUTSHORT(PPP_CHAP, nakp);
1592 PUTCHAR(ao->chap_mdtype, nakp);
1593 break;
1595 ho->neg_upap = 1;
1596 break;
1598 if (cishort == PPP_CHAP) {
1599 if (ho->neg_upap || /* we've already accepted PAP */
1600 cilen != CILEN_CHAP) {
1601 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1602 orc = CONFREJ;
1603 break;
1605 if (!ao->neg_chap) { /* we don't want to do CHAP */
1606 orc = CONFNAK; /* NAK it and suggest PAP */
1607 PUTCHAR(CI_AUTHTYPE, nakp);
1608 PUTCHAR(CILEN_SHORT, nakp);
1609 PUTSHORT(PPP_PAP, nakp);
1610 break;
1612 GETCHAR(cichar, p); /* get digest type*/
1613 if (cichar != CHAP_DIGEST_MD5
1614 #ifdef CHAPMS
1615 && cichar != CHAP_MICROSOFT
1616 #endif
1618 orc = CONFNAK;
1619 PUTCHAR(CI_AUTHTYPE, nakp);
1620 PUTCHAR(CILEN_CHAP, nakp);
1621 PUTSHORT(PPP_CHAP, nakp);
1622 PUTCHAR(ao->chap_mdtype, nakp);
1623 break;
1625 ho->chap_mdtype = cichar; /* save md type */
1626 ho->neg_chap = 1;
1627 break;
1631 * We don't recognize the protocol they're asking for.
1632 * Nak it with something we're willing to do.
1633 * (At this point we know ao->neg_upap || ao->neg_chap.)
1635 orc = CONFNAK;
1636 PUTCHAR(CI_AUTHTYPE, nakp);
1637 if (ao->neg_chap) {
1638 PUTCHAR(CILEN_CHAP, nakp);
1639 PUTSHORT(PPP_CHAP, nakp);
1640 PUTCHAR(ao->chap_mdtype, nakp);
1641 } else {
1642 PUTCHAR(CILEN_SHORT, nakp);
1643 PUTSHORT(PPP_PAP, nakp);
1645 break;
1647 case CI_QUALITY:
1648 if (!ao->neg_lqr ||
1649 cilen != CILEN_LQR) {
1650 orc = CONFREJ;
1651 break;
1654 GETSHORT(cishort, p);
1655 GETLONG(cilong, p);
1657 if (cishort != PPP_LQR) {
1658 orc = CONFNAK;
1659 PUTCHAR(CI_QUALITY, nakp);
1660 PUTCHAR(CILEN_LQR, nakp);
1661 PUTSHORT(PPP_LQR, nakp);
1662 PUTLONG(ao->lqr_period, nakp);
1663 break;
1665 break;
1667 case CI_MAGICNUMBER:
1668 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1669 cilen != CILEN_LONG) {
1670 orc = CONFREJ;
1671 break;
1673 GETLONG(cilong, p);
1676 * He must have a different magic number.
1678 if (go->neg_magicnumber &&
1679 cilong == go->magicnumber) {
1680 cilong = magic(); /* Don't put magic() inside macro! */
1681 orc = CONFNAK;
1682 PUTCHAR(CI_MAGICNUMBER, nakp);
1683 PUTCHAR(CILEN_LONG, nakp);
1684 PUTLONG(cilong, nakp);
1685 break;
1687 ho->neg_magicnumber = 1;
1688 ho->magicnumber = cilong;
1689 break;
1692 case CI_PCOMPRESSION:
1693 if (!ao->neg_pcompression ||
1694 cilen != CILEN_VOID) {
1695 orc = CONFREJ;
1696 break;
1698 ho->neg_pcompression = 1;
1699 break;
1701 case CI_ACCOMPRESSION:
1702 if (!ao->neg_accompression ||
1703 cilen != CILEN_VOID) {
1704 orc = CONFREJ;
1705 break;
1707 ho->neg_accompression = 1;
1708 break;
1710 case CI_MRRU:
1711 if (!ao->neg_mrru || !multilink ||
1712 cilen != CILEN_SHORT) {
1713 orc = CONFREJ;
1714 break;
1717 GETSHORT(cishort, p);
1718 /* possibly should insist on a minimum/maximum MRRU here */
1719 ho->neg_mrru = 1;
1720 ho->mrru = cishort;
1721 break;
1723 case CI_SSNHF:
1724 if (!ao->neg_ssnhf || !multilink ||
1725 cilen != CILEN_VOID) {
1726 orc = CONFREJ;
1727 break;
1729 ho->neg_ssnhf = 1;
1730 break;
1732 case CI_EPDISC:
1733 if (!ao->neg_endpoint ||
1734 cilen < CILEN_CHAR ||
1735 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1736 orc = CONFREJ;
1737 break;
1739 GETCHAR(cichar, p);
1740 cilen -= CILEN_CHAR;
1741 ho->neg_endpoint = 1;
1742 ho->endpoint.class = cichar;
1743 ho->endpoint.length = cilen;
1744 BCOPY(p, ho->endpoint.value, cilen);
1745 INCPTR(cilen, p);
1746 break;
1748 default:
1749 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1750 #if 1 //fixed by crazy 20070424: bug id 6438(20070423)
1751 //Fix by crazy 20071102 (Bug id IR-B0003328): This issue is reported by CRDC. Please refer to the email.
1752 orc = CONFREJ;
1753 #else
1754 //FIXME: maybe it is a wrong packet...
1755 orc = CONFNAK; //Received an unknown option type(like 0xFF), send a LCP Configure-Nak message.
1756 LOGX_DEBUG("%s: unknown option %d", __FUNCTION__, citype);
1757 #endif
1758 break;
1761 endswitch:
1762 if (orc == CONFACK && /* Good CI */
1763 rc != CONFACK) /* but prior CI wasnt? */
1764 continue; /* Don't send this one */
1766 if (orc == CONFNAK) { /* Nak this CI? */
1767 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1768 && citype != CI_MAGICNUMBER) {
1769 orc = CONFREJ; /* Get tough if so */
1770 } else {
1771 if (rc == CONFREJ) /* Rejecting prior CI? */
1772 continue; /* Don't send this one */
1773 rc = CONFNAK;
1776 if (orc == CONFREJ) { /* Reject this CI */
1777 rc = CONFREJ;
1778 if (cip != rejp) /* Need to move rejected CI? */
1779 BCOPY(cip, rejp, cilen); /* Move it */
1780 INCPTR(cilen, rejp); /* Update output pointer */
1785 * If we wanted to send additional NAKs (for unsent CIs), the
1786 * code would go here. The extra NAKs would go at *nakp.
1787 * At present there are no cases where we want to ask the
1788 * peer to negotiate an option.
1791 switch (rc) {
1792 case CONFACK:
1793 *lenp = next - inp;
1794 break;
1795 case CONFNAK:
1797 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1799 *lenp = nakp - nak_buffer;
1800 BCOPY(nak_buffer, inp, *lenp);
1801 break;
1802 case CONFREJ:
1803 *lenp = rejp - inp;
1804 break;
1807 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1808 return (rc); /* Return final code */
1813 * lcp_up - LCP has come UP.
1815 static void
1816 lcp_up(f)
1817 fsm *f;
1819 lcp_options *wo = &lcp_wantoptions[f->unit];
1820 lcp_options *ho = &lcp_hisoptions[f->unit];
1821 lcp_options *go = &lcp_gotoptions[f->unit];
1822 lcp_options *ao = &lcp_allowoptions[f->unit];
1823 int mtu;
1824 char runmtu[10];
1826 if (!go->neg_magicnumber)
1827 go->magicnumber = 0;
1828 if (!ho->neg_magicnumber)
1829 ho->magicnumber = 0;
1832 * Set our MTU to the smaller of the MTU we wanted and
1833 * the MRU our peer wanted. If we negotiated an MRU,
1834 * set our MRU to the larger of value we wanted and
1835 * the value we got in the negotiation.
1836 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1837 * the interface MTU is set to the lower of that and the
1838 * MTU we want to use.
1840 mtu = MIN(ho->neg_mru? ho->mru: PPP_MRU, ao->mru);
1841 sprintf(runmtu, "%d", mtu); // by honor 20040618
1842 script_setenv("MTU", runmtu, 1);
1843 #ifdef HAVE_MULTILINK
1844 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1845 #endif /* HAVE_MULTILINK */
1846 netif_set_mtu(f->unit, mtu);
1847 ppp_send_config(f->unit, mtu,
1848 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1849 ho->neg_pcompression, ho->neg_accompression);
1850 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1851 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1852 go->neg_pcompression, go->neg_accompression);
1854 if (ho->neg_mru)
1855 peer_mru[f->unit] = ho->mru;
1857 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1859 link_established(f->unit);
1864 * lcp_down - LCP has gone DOWN.
1866 * Alert other protocols.
1868 static void
1869 lcp_down(f)
1870 fsm *f;
1872 lcp_options *go = &lcp_gotoptions[f->unit];
1874 lcp_echo_lowerdown(f->unit);
1876 link_down(f->unit);
1878 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1879 ppp_recv_config(f->unit, PPP_MRU,
1880 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1881 go->neg_pcompression, go->neg_accompression);
1882 peer_mru[f->unit] = PPP_MRU;
1887 * lcp_starting - LCP needs the lower layer up.
1889 static void
1890 lcp_starting(f)
1891 fsm *f;
1893 link_required(f->unit);
1898 * lcp_finished - LCP has finished with the lower layer.
1900 static void
1901 lcp_finished(f)
1902 fsm *f;
1904 link_terminated(f->unit);
1909 * lcp_printpkt - print the contents of an LCP packet.
1911 static char *lcp_codenames[] = {
1912 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1913 "TermReq", "TermAck", "CodeRej", "ProtRej",
1914 "EchoReq", "EchoRep", "DiscReq"
1917 static int
1918 lcp_printpkt(p, plen, printer, arg)
1919 u_char *p;
1920 int plen;
1921 void (*printer) __P((void *, char *, ...));
1922 void *arg;
1924 int code, id, len, olen, i;
1925 u_char *pstart, *optend;
1926 u_short cishort;
1927 u_int32_t cilong;
1929 if (plen < HEADERLEN)
1930 return 0;
1931 pstart = p;
1932 GETCHAR(code, p);
1933 GETCHAR(id, p);
1934 GETSHORT(len, p);
1935 if (len < HEADERLEN || len > plen)
1936 return 0;
1938 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1939 printer(arg, " pid=(%d) %s", getpid(), lcp_codenames[code-1]);
1940 else
1941 printer(arg, " code=0x%x", code);
1942 printer(arg, " id=0x%x", id);
1943 len -= HEADERLEN;
1944 switch (code) {
1945 case CONFREQ:
1946 case CONFACK:
1947 case CONFNAK:
1948 case CONFREJ:
1949 /* print option list */
1950 while (len >= 2) {
1951 GETCHAR(code, p);
1952 GETCHAR(olen, p);
1953 p -= 2;
1954 if (olen < 2 || olen > len) {
1955 break;
1957 printer(arg, " <");
1958 len -= olen;
1959 optend = p + olen;
1960 switch (code) {
1961 case CI_MRU:
1962 if (olen == CILEN_SHORT) {
1963 p += 2;
1964 GETSHORT(cishort, p);
1965 printer(arg, "mru %d", cishort);
1967 break;
1968 case CI_ASYNCMAP:
1969 if (olen == CILEN_LONG) {
1970 p += 2;
1971 GETLONG(cilong, p);
1972 printer(arg, "asyncmap 0x%x", cilong);
1974 break;
1975 case CI_AUTHTYPE:
1976 if (olen >= CILEN_SHORT) {
1977 p += 2;
1978 printer(arg, "auth ");
1979 GETSHORT(cishort, p);
1980 switch (cishort) {
1981 case PPP_PAP:
1982 printer(arg, "pap");
1983 break;
1984 case PPP_CHAP:
1985 printer(arg, "chap");
1986 if (p < optend) {
1987 switch (*p) {
1988 case CHAP_DIGEST_MD5:
1989 printer(arg, " MD5");
1990 ++p;
1991 break;
1992 #ifdef CHAPMS
1993 case CHAP_MICROSOFT:
1994 printer(arg, " m$oft");
1995 ++p;
1996 break;
1997 #endif
2000 break;
2001 default:
2002 printer(arg, "0x%x", cishort);
2005 break;
2006 case CI_QUALITY:
2007 if (olen >= CILEN_SHORT) {
2008 p += 2;
2009 printer(arg, "quality ");
2010 GETSHORT(cishort, p);
2011 switch (cishort) {
2012 case PPP_LQR:
2013 printer(arg, "lqr");
2014 break;
2015 default:
2016 printer(arg, "0x%x", cishort);
2019 break;
2020 case CI_CALLBACK:
2021 if (olen >= CILEN_CHAR) {
2022 p += 2;
2023 printer(arg, "callback ");
2024 GETCHAR(cishort, p);
2025 switch (cishort) {
2026 case CBCP_OPT:
2027 printer(arg, "CBCP");
2028 break;
2029 default:
2030 printer(arg, "0x%x", cishort);
2033 break;
2034 case CI_MAGICNUMBER:
2035 if (olen == CILEN_LONG) {
2036 p += 2;
2037 GETLONG(cilong, p);
2038 printer(arg, "magic 0x%x", cilong);
2040 break;
2041 case CI_PCOMPRESSION:
2042 if (olen == CILEN_VOID) {
2043 p += 2;
2044 printer(arg, "pcomp");
2046 break;
2047 case CI_ACCOMPRESSION:
2048 if (olen == CILEN_VOID) {
2049 p += 2;
2050 printer(arg, "accomp");
2052 break;
2053 case CI_MRRU:
2054 if (olen == CILEN_SHORT) {
2055 p += 2;
2056 GETSHORT(cishort, p);
2057 printer(arg, "mrru %d", cishort);
2059 break;
2060 case CI_SSNHF:
2061 if (olen == CILEN_VOID) {
2062 p += 2;
2063 printer(arg, "ssnhf");
2065 break;
2066 case CI_EPDISC:
2067 #ifdef HAVE_MULTILINK
2068 if (olen >= CILEN_CHAR) {
2069 struct epdisc epd;
2070 p += 2;
2071 GETCHAR(epd.class, p);
2072 epd.length = olen - CILEN_CHAR;
2073 if (epd.length > MAX_ENDP_LEN)
2074 epd.length = MAX_ENDP_LEN;
2075 if (epd.length > 0) {
2076 BCOPY(p, epd.value, epd.length);
2077 p += epd.length;
2079 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2081 #else
2082 printer(arg, "endpoint");
2083 #endif
2084 break;
2086 while (p < optend) {
2087 GETCHAR(code, p);
2088 printer(arg, " %.2x", code);
2090 printer(arg, ">");
2092 break;
2094 case TERMACK:
2095 case TERMREQ:
2096 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2097 printer(arg, " ");
2098 print_string((char *)p, len, printer, arg);
2099 p += len;
2100 len = 0;
2102 break;
2104 case ECHOREQ:
2105 case ECHOREP:
2106 case DISCREQ:
2107 if (len >= 4) {
2108 GETLONG(cilong, p);
2109 printer(arg, " magic=0x%x", cilong);
2110 p += 4;
2111 len -= 4;
2113 break;
2116 /* print the rest of the bytes in the packet */
2117 for (i = 0; i < len && i < 32; ++i) {
2118 GETCHAR(code, p);
2119 printer(arg, " %.2x", code);
2121 if (i < len) {
2122 printer(arg, " ...");
2123 p += len - i;
2126 return p - pstart;
2130 * Time to shut down the link because there is nothing out there.
2133 static
2134 void LcpLinkFailure (f)
2135 fsm *f;
2137 if (f->state == OPENED) {
2138 LOGX_WARN("LCP appears to be disconnected (pending: %d).", lcp_echos_pending);
2140 // info("No response to %d echo-requests", lcp_echos_pending);
2141 // notice("Serial link appears to be disconnected.");
2142 lcp_close(f->unit, "Peer not responding");
2143 status = EXIT_PEER_DEAD;
2147 #if 0 // timer -> timer ??? -- zzz
2149 * Check if we have been received "echo reply" packet
2152 static void
2153 LcpCheckReply (arg)
2154 void *arg;
2156 fsm *f1 = &lcp_fsm[ifunit];
2158 #if 0
2159 if(lcp_echos_pending >= 1)
2160 TIMEOUT (LcpEchoTimeout, f1, 1);
2161 else
2162 #endif
2163 TIMEOUT (LcpEchoTimeout, f1, lcp_echo_interval-1);
2165 #endif // 0
2168 * Timer expired for the LCP echo requests from this process.
2171 static void
2172 LcpEchoCheck (f)
2173 fsm *f;
2175 LcpSendEchoRequest (f);
2176 if (f->state != OPENED)
2177 return;
2180 * Start the timer for the next interval.
2182 #if 1 // zzz
2183 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2184 #else
2185 if (lcp_echo_timer_running)
2186 warn("assertion lcp_echo_timer_running==0 failed");
2188 TIMEOUT (LcpCheckReply, f, 1);
2189 #endif
2191 lcp_echo_timer_running = 1;
2195 * LcpEchoTimeout - Timer expired on the LCP echo
2198 static void
2199 LcpEchoTimeout (arg)
2200 void *arg;
2202 if (lcp_echo_timer_running != 0) {
2203 lcp_echo_timer_running = 0;
2204 LcpEchoCheck ((fsm *) arg);
2209 * LcpEchoReply - LCP has received a reply to the echo
2212 static void
2213 lcp_received_echo_reply (f, id, inp, len)
2214 fsm *f;
2215 int id;
2216 u_char *inp;
2217 int len;
2219 u_int32_t magic;
2221 /* Check the magic number - don't count replies from ourselves. */
2222 if (len < 4) {
2223 dbglog("lcp: received short Echo-Reply, length %d", len);
2224 return;
2226 GETLONG(magic, inp);
2227 if (lcp_gotoptions[f->unit].neg_magicnumber
2228 && magic == lcp_gotoptions[f->unit].magicnumber) {
2229 warn("appear to have received our own echo-reply!");
2230 return;
2233 /* Reset the number of outstanding echo frames */
2234 lcp_echos_pending = 0;
2238 * LcpSendEchoRequest - Send an echo request frame to the peer
2241 static void
2242 LcpSendEchoRequest (f)
2243 fsm *f;
2245 u_int32_t lcp_magic;
2246 u_char pkt[4], *pktp;
2247 struct ppp_idle idle;
2248 unsigned long echo_interval;
2250 echo_interval = get_time() - echo_time;
2251 if (echo_interval <= lcp_echo_interval) {
2252 // LOGX_DEBUG("(echo_interval %lu <= lcp_echo_interval %d)", echo_interval, lcp_echo_interval);
2253 return;
2257 LOGX_DEBUG("echo_interval=%lu lcp_echo_interval=%d lcp_echos_pending=%d lcp_echo_fails=%d",
2258 echo_interval, lcp_echo_interval,
2259 lcp_echos_pending, lcp_echo_fails);
2263 * Detect the failure of the peer at this point.
2265 if (lcp_echo_fails != 0) {
2266 if (lcp_echos_pending >= lcp_echo_fails) {
2267 // LOGX_DEBUG("lcp_echos_pending=%d lcp_echo_fails=%d", lcp_echos_pending, lcp_echo_fails);
2268 if (get_idle_time(0, &idle)) {
2269 // LOGX_DEBUG("idle.recv_idle=%ld", idle.recv_idle);
2270 if (idle.recv_idle < lcp_echo_fails * lcp_echo_interval) {
2271 lcp_echos_pending--;
2272 goto SendEchoRequest;
2275 // LOGX_DEBUG("check echo reply failed");
2276 LcpLinkFailure(f);
2277 lcp_echos_pending = 0;
2281 SendEchoRequest:
2283 * Make and send the echo request frame.
2285 if (f->state == OPENED) {
2286 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2287 pktp = pkt;
2288 PUTLONG(lcp_magic, pktp);
2289 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2290 ++lcp_echos_pending;
2295 * lcp_echo_lowerup - Start the timer for the LCP frame
2298 static void
2299 lcp_echo_lowerup (unit)
2300 int unit;
2302 fsm *f = &lcp_fsm[unit];
2304 /* Clear the parameters for generating echo frames */
2305 lcp_echos_pending = 0;
2306 lcp_echo_number = 0;
2307 lcp_echo_timer_running = 0;
2309 /* If a timeout interval is specified then start the timer */
2310 if (lcp_echo_interval != 0)
2311 LcpEchoCheck (f);
2315 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2318 static void
2319 lcp_echo_lowerdown (unit)
2320 int unit;
2322 fsm *f = &lcp_fsm[unit];
2324 if (lcp_echo_timer_running != 0) {
2325 UNTIMEOUT (LcpEchoTimeout, f);
2326 lcp_echo_timer_running = 0;