2 * lcp.c - PPP Link Control Protocol.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #include "pppd-private.h"
58 #include "multilink.h"
61 * When the link comes up we want to be able to wait for a short while,
62 * or until seeing some input from the peer, before starting to send
63 * configure-requests. We do this by delaying the fsm_lowerup call.
65 /* steal a bit in fsm flags word */
66 #define DELAYED_UP 0x100
68 static void lcp_delayed_up(void *);
71 * LCP-related command-line options.
73 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
74 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
75 bool lcp_echo_adaptive = 0; /* request echo only if the link was idle */
76 bool lax_recv = 0; /* accept control chars in asyncmap */
77 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
79 static int noopt(char **);
81 #ifdef PPP_WITH_MULTILINK
82 static int setendpoint(char **);
83 static void printendpoint(option_t *, void (*)(void *, char *, ...), void *);
84 #endif /* PPP_WITH_MULTILINK */
86 static struct option lcp_option_list[] = {
88 { "-all", o_special_noarg, (void *)noopt,
89 "Don't request/allow any LCP options" },
91 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
92 "Disable address/control compression",
93 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
94 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
95 "Disable address/control compression",
96 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
98 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
99 "Set asyncmap (for received packets)",
100 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
101 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
102 "Set asyncmap (for received packets)",
103 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
104 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
105 "Disable asyncmap negotiation",
106 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
107 &lcp_allowoptions[0].neg_asyncmap },
108 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
109 "Disable asyncmap negotiation",
110 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
111 &lcp_allowoptions[0].neg_asyncmap },
113 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
114 "Disable magic number negotiation (looped-back line detection)",
115 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
116 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
117 "Disable magic number negotiation (looped-back line detection)",
118 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
120 { "mru", o_int, &lcp_wantoptions[0].mru,
121 "Set MRU (maximum received packet size) for negotiation",
122 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
123 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
124 "Disable MRU negotiation (use default 1500)",
125 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
126 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
127 "Disable MRU negotiation (use default 1500)",
128 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
130 { "mtu", o_int, &lcp_allowoptions[0].mru,
131 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
133 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
134 "Disable protocol field compression",
135 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
136 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
137 "Disable protocol field compression",
138 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
140 { "passive", o_bool, &lcp_wantoptions[0].passive,
141 "Set passive mode", 1 },
142 { "-p", o_bool, &lcp_wantoptions[0].passive,
143 "Set passive mode", OPT_ALIAS | 1 },
145 { "silent", o_bool, &lcp_wantoptions[0].silent,
146 "Set silent mode", 1 },
148 { "lcp-echo-failure", o_int, &lcp_echo_fails,
149 "Set number of consecutive echo failures to indicate link failure",
151 { "lcp-echo-interval", o_int, &lcp_echo_interval,
152 "Set time in seconds between LCP echo requests", OPT_PRIO },
153 { "lcp-echo-adaptive", o_bool, &lcp_echo_adaptive,
154 "Suppress LCP echo requests if traffic was received", 1 },
155 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
156 "Set time in seconds between LCP retransmissions", OPT_PRIO },
157 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
158 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
159 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
160 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
161 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
162 "Set limit on number of LCP configure-naks", OPT_PRIO },
164 { "receive-all", o_bool, &lax_recv,
165 "Accept all received control characters", 1 },
167 #ifdef PPP_WITH_MULTILINK
168 { "mrru", o_int, &lcp_wantoptions[0].mrru,
169 "Maximum received packet size for multilink bundle",
170 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
172 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
173 "Use short sequence numbers in multilink headers",
174 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
175 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
176 "Don't use short sequence numbers in multilink headers",
177 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
179 { "endpoint", o_special, (void *) setendpoint,
180 "Endpoint discriminator for multilink",
181 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
182 #endif /* PPP_WITH_MULTILINK */
184 { "noendpoint", o_bool, &noendpoint,
185 "Don't send or accept multilink endpoint discriminator", 1 },
191 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
192 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
193 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
194 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
195 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
197 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
198 static int lcp_echo_number = 0; /* ID number of next echo frame */
199 static int lcp_echo_timer_running = 0; /* set if a timer is running */
201 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
204 * Callbacks for fsm code. (CI = Configuration Information)
206 static void lcp_resetci(fsm *); /* Reset our CI */
207 static int lcp_cilen(fsm *); /* Return length of our CI */
208 static void lcp_addci(fsm *, u_char *, int *); /* Add our CI to pkt */
209 static int lcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
210 static int lcp_nakci(fsm *, u_char *, int, int); /* Peer nak'd our CI */
211 static int lcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
212 static int lcp_reqci(fsm *, u_char *, int *, int); /* Rcv peer CI */
213 static void lcp_up(fsm *); /* We're UP */
214 static void lcp_down(fsm *); /* We're DOWN */
215 static void lcp_starting(fsm *); /* We need lower layer up */
216 static void lcp_finished(fsm *); /* We need lower layer down */
217 static int lcp_extcode(fsm *, int, int, u_char *, int);
218 static void lcp_rprotrej(fsm *, u_char *, int);
221 * routines to send LCP echos to peer
224 static void lcp_echo_lowerup(int);
225 static void lcp_echo_lowerdown(int);
226 static void LcpEchoTimeout(void *);
227 static void lcp_received_echo_reply(fsm *, int, u_char *, int);
228 static void LcpSendEchoRequest(fsm *);
229 static void LcpLinkFailure(fsm *);
230 static void LcpEchoCheck(fsm *);
232 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
233 lcp_resetci, /* Reset our Configuration Information */
234 lcp_cilen, /* Length of our Configuration Information */
235 lcp_addci, /* Add our Configuration Information */
236 lcp_ackci, /* ACK our Configuration Information */
237 lcp_nakci, /* NAK our Configuration Information */
238 lcp_rejci, /* Reject our Configuration Information */
239 lcp_reqci, /* Request peer's Configuration Information */
240 lcp_up, /* Called when fsm reaches OPENED state */
241 lcp_down, /* Called when fsm leaves OPENED state */
242 lcp_starting, /* Called when we want the lower layer up */
243 lcp_finished, /* Called when we want the lower layer down */
244 NULL, /* Called when Protocol-Reject received */
245 NULL, /* Retransmission is necessary */
246 lcp_extcode, /* Called to handle LCP-specific codes */
247 "LCP" /* String name of protocol */
251 * Protocol entry points.
252 * Some of these are called directly.
255 static void lcp_init(int);
256 static void lcp_input(int, u_char *, int);
257 static void lcp_protrej(int);
258 static int lcp_printpkt(u_char *, int, void (*)(void *, char *, ...), void *);
260 struct protent lcp_protent = {
280 int lcp_loopbackfail = DEFLOOPBACKFAIL;
283 * Length of each type of configuration option (in octets)
287 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
288 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
289 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
290 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
293 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
294 (x) == CONFNAK ? "NAK" : "REJ")
297 * noopt - Disable all options (why?).
302 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
303 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
308 #ifdef PPP_WITH_MULTILINK
310 setendpoint(char **argv)
312 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
313 lcp_wantoptions[0].neg_endpoint = 1;
316 ppp_option_error("Can't parse '%s' as an endpoint discriminator", *argv);
321 printendpoint(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
323 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
325 #endif /* PPP_WITH_MULTILINK */
328 * lcp_init - Initialize LCP.
333 fsm *f = &lcp_fsm[unit];
334 lcp_options *wo = &lcp_wantoptions[unit];
335 lcp_options *ao = &lcp_allowoptions[unit];
338 f->protocol = PPP_LCP;
339 f->callbacks = &lcp_callbacks;
343 BZERO(wo, sizeof(*wo));
346 wo->neg_asyncmap = 1;
347 wo->neg_magicnumber = 1;
348 wo->neg_pcompression = 1;
349 wo->neg_accompression = 1;
351 BZERO(ao, sizeof(*ao));
354 ao->neg_asyncmap = 1;
356 ao->chap_mdtype = chap_mdtype_all;
359 ao->neg_magicnumber = 1;
360 ao->neg_pcompression = 1;
361 ao->neg_accompression = 1;
362 ao->neg_endpoint = 1;
367 * lcp_open - LCP is allowed to come up.
372 fsm *f = &lcp_fsm[unit];
373 lcp_options *wo = &lcp_wantoptions[unit];
375 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
377 f->flags |= OPT_PASSIVE;
379 f->flags |= OPT_SILENT;
385 * lcp_close - Take LCP down.
388 lcp_close(int unit, char *reason)
390 fsm *f = &lcp_fsm[unit];
393 if (!in_phase(PHASE_DEAD) && !in_phase(PHASE_MASTER))
394 new_phase(PHASE_TERMINATE);
396 if (f->flags & DELAYED_UP) {
397 UNTIMEOUT(lcp_delayed_up, f);
402 fsm_close(f, reason);
403 if (oldstate == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT|DELAYED_UP)) {
405 * This action is not strictly according to the FSM in RFC1548,
406 * but it does mean that the program terminates if you do a
407 * lcp_close() when a connection hasn't been established
408 * because we are in passive/silent mode or because we have
409 * delayed the fsm_lowerup() call and it hasn't happened yet.
411 f->flags &= ~DELAYED_UP;
418 * lcp_lowerup - The lower layer is up.
421 lcp_lowerup(int unit)
423 lcp_options *wo = &lcp_wantoptions[unit];
424 fsm *f = &lcp_fsm[unit];
427 * Don't use A/C or protocol compression on transmission,
428 * but accept A/C and protocol compressed packets
429 * if we are going to ask for A/C and protocol compression.
431 if (ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0) < 0
432 || ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
433 wo->neg_pcompression, wo->neg_accompression) < 0)
435 peer_mru[unit] = PPP_MRU;
437 if (listen_time != 0) {
438 f->flags |= DELAYED_UP;
439 ppp_timeout(lcp_delayed_up, f, 0, listen_time * 1000);
446 * lcp_lowerdown - The lower layer is down.
449 lcp_lowerdown(int unit)
451 fsm *f = &lcp_fsm[unit];
453 if (f->flags & DELAYED_UP) {
454 f->flags &= ~DELAYED_UP;
455 UNTIMEOUT(lcp_delayed_up, f);
457 fsm_lowerdown(&lcp_fsm[unit]);
462 * lcp_delayed_up - Bring the lower layer up now.
465 lcp_delayed_up(void *arg)
469 if (f->flags & DELAYED_UP) {
470 f->flags &= ~DELAYED_UP;
477 * lcp_input - Input LCP packet.
480 lcp_input(int unit, u_char *p, int len)
482 fsm *f = &lcp_fsm[unit];
484 if (f->flags & DELAYED_UP) {
485 f->flags &= ~DELAYED_UP;
486 UNTIMEOUT(lcp_delayed_up, f);
489 fsm_input(f, p, len);
493 * lcp_extcode - Handle a LCP-specific code.
496 lcp_extcode(fsm *f, int code, int id, u_char *inp, int len)
502 lcp_rprotrej(f, inp, len);
506 if (f->state != OPENED)
509 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
510 fsm_sdata(f, ECHOREP, id, inp, len);
514 lcp_received_echo_reply(f, id, inp, len);
530 * lcp_rprotrej - Receive an Protocol-Reject.
532 * Figure out which protocol is rejected and inform it.
535 lcp_rprotrej(fsm *f, u_char *inp, int len)
538 struct protent *protp;
543 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
550 * Protocol-Reject packets received in any state other than the LCP
551 * OPENED state SHOULD be silently discarded.
553 if( f->state != OPENED ){
554 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
558 pname = protocol_name(prot);
561 * Upcall the proper Protocol-Reject routine.
563 for (i = 0; (protp = protocols[i]) != NULL; ++i)
564 if (protp->protocol == prot && protp->enabled_flag) {
566 dbglog("Protocol-Reject for 0x%x received", prot);
568 dbglog("Protocol-Reject for '%s' (0x%x) received", pname,
570 (*protp->protrej)(f->unit);
575 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
577 warn("Protocol-Reject for unsupported protocol '%s' (0x%x)", pname,
583 * lcp_protrej - A Protocol-Reject was received.
587 lcp_protrej(int unit)
592 error("Received Protocol-Reject for LCP!");
593 fsm_protreject(&lcp_fsm[unit]);
598 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
601 lcp_sprotrej(int unit, u_char *p, int len)
604 * Send back the protocol and the information field of the
605 * rejected packet. We only get here if LCP is in the OPENED state.
610 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
616 * lcp_resetci - Reset our CI.
621 lcp_options *wo = &lcp_wantoptions[f->unit];
622 lcp_options *go = &lcp_gotoptions[f->unit];
623 lcp_options *ao = &lcp_allowoptions[f->unit];
625 wo->magicnumber = magic();
631 go->neg_endpoint = 0;
634 ao->neg_endpoint = 0;
635 peer_mru[f->unit] = PPP_MRU;
641 * lcp_cilen - Return length of our CI.
646 lcp_options *go = &lcp_gotoptions[f->unit];
648 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
649 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
650 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
651 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
652 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
653 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
655 * NB: we only ask for one of CHAP, UPAP, or EAP, even if we will
656 * accept more than one. We prefer EAP first, then CHAP, then
659 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
660 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
661 LENCISHORT(go->neg_eap) +
662 LENCICHAP(!go->neg_eap && go->neg_chap) +
663 LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
664 LENCILQR(go->neg_lqr) +
665 LENCICBCP(go->neg_cbcp) +
666 LENCILONG(go->neg_magicnumber) +
667 LENCIVOID(go->neg_pcompression) +
668 LENCIVOID(go->neg_accompression) +
669 LENCISHORT(go->neg_mrru) +
670 LENCIVOID(go->neg_ssnhf) +
671 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
676 * lcp_addci - Add our desired CIs to a packet.
679 lcp_addci(fsm *f, u_char *ucp, int *lenp)
681 lcp_options *go = &lcp_gotoptions[f->unit];
682 u_char *start_ucp = ucp;
684 #define ADDCIVOID(opt, neg) \
687 PUTCHAR(CILEN_VOID, ucp); \
689 #define ADDCISHORT(opt, neg, val) \
692 PUTCHAR(CILEN_SHORT, ucp); \
693 PUTSHORT(val, ucp); \
695 #define ADDCICHAP(opt, neg, val) \
697 PUTCHAR((opt), ucp); \
698 PUTCHAR(CILEN_CHAP, ucp); \
699 PUTSHORT(PPP_CHAP, ucp); \
700 PUTCHAR((CHAP_DIGEST(val)), ucp); \
702 #define ADDCILONG(opt, neg, val) \
705 PUTCHAR(CILEN_LONG, ucp); \
708 #define ADDCILQR(opt, neg, val) \
711 PUTCHAR(CILEN_LQR, ucp); \
712 PUTSHORT(PPP_LQR, ucp); \
715 #define ADDCICHAR(opt, neg, val) \
718 PUTCHAR(CILEN_CHAR, ucp); \
721 #define ADDCIENDP(opt, neg, class, val, len) \
725 PUTCHAR(CILEN_CHAR + len, ucp); \
726 PUTCHAR(class, ucp); \
727 for (i = 0; i < len; ++i) \
728 PUTCHAR(val[i], ucp); \
731 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
732 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
734 ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
735 ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
736 ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
738 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
739 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
740 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
741 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
742 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
743 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
744 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
745 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
746 go->endpoint.value, go->endpoint.length);
748 if (ucp - start_ucp != *lenp) {
749 /* this should never happen, because peer_mtu should be 1500 */
750 error("Bug in lcp_addci: wrong length");
756 * lcp_ackci - Ack our CIs.
757 * This should not modify any state if the Ack is bad.
764 lcp_ackci(fsm *f, u_char *p, int len)
766 lcp_options *go = &lcp_gotoptions[f->unit];
767 u_char cilen, citype, cichar;
772 * CIs must be in exactly the same order that we sent.
773 * Check packet length and CI length at each step.
774 * If we find any deviations, then this packet is bad.
776 #define ACKCIVOID(opt, neg) \
778 if ((len -= CILEN_VOID) < 0) \
780 GETCHAR(citype, p); \
782 if (cilen != CILEN_VOID || \
786 #define ACKCISHORT(opt, neg, val) \
788 if ((len -= CILEN_SHORT) < 0) \
790 GETCHAR(citype, p); \
792 if (cilen != CILEN_SHORT || \
795 GETSHORT(cishort, p); \
796 if (cishort != val) \
799 #define ACKCICHAR(opt, neg, val) \
801 if ((len -= CILEN_CHAR) < 0) \
803 GETCHAR(citype, p); \
805 if (cilen != CILEN_CHAR || \
808 GETCHAR(cichar, p); \
812 #define ACKCICHAP(opt, neg, val) \
814 if ((len -= CILEN_CHAP) < 0) \
816 GETCHAR(citype, p); \
818 if (cilen != CILEN_CHAP || \
821 GETSHORT(cishort, p); \
822 if (cishort != PPP_CHAP) \
824 GETCHAR(cichar, p); \
825 if (cichar != (CHAP_DIGEST(val))) \
828 #define ACKCILONG(opt, neg, val) \
830 if ((len -= CILEN_LONG) < 0) \
832 GETCHAR(citype, p); \
834 if (cilen != CILEN_LONG || \
837 GETLONG(cilong, p); \
841 #define ACKCILQR(opt, neg, val) \
843 if ((len -= CILEN_LQR) < 0) \
845 GETCHAR(citype, p); \
847 if (cilen != CILEN_LQR || \
850 GETSHORT(cishort, p); \
851 if (cishort != PPP_LQR) \
853 GETLONG(cilong, p); \
857 #define ACKCIENDP(opt, neg, class, val, vlen) \
860 if ((len -= CILEN_CHAR + vlen) < 0) \
862 GETCHAR(citype, p); \
864 if (cilen != CILEN_CHAR + vlen || \
867 GETCHAR(cichar, p); \
868 if (cichar != class) \
870 for (i = 0; i < vlen; ++i) { \
871 GETCHAR(cichar, p); \
872 if (cichar != val[i]) \
877 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
878 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
880 ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
881 ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
882 ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
884 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
885 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
886 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
887 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
888 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
889 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
890 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
891 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
892 go->endpoint.value, go->endpoint.length);
895 * If there are any remaining CIs, then this packet is bad.
901 LCPDEBUG(("lcp_acki: received bad Ack!"));
907 * lcp_nakci - Peer has sent a NAK for some of our CIs.
908 * This should not modify any state if the Nak is bad
909 * or if LCP is in the OPENED state.
916 lcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
918 lcp_options *go = &lcp_gotoptions[f->unit];
919 lcp_options *wo = &lcp_wantoptions[f->unit];
920 u_char citype, cichar, *next;
923 lcp_options no; /* options we've seen Naks for */
924 lcp_options try; /* options to request next time */
928 BZERO(&no, sizeof(no));
932 * Any Nak'd CIs must be in exactly the same order that we sent.
933 * Check packet length and CI length at each step.
934 * If we find any deviations, then this packet is bad.
936 #define NAKCIVOID(opt, neg) \
938 len >= CILEN_VOID && \
939 p[1] == CILEN_VOID && \
942 INCPTR(CILEN_VOID, p); \
946 #define NAKCICHAP(opt, neg, code) \
948 len >= CILEN_CHAP && \
949 p[1] == CILEN_CHAP && \
953 GETSHORT(cishort, p); \
954 GETCHAR(cichar, p); \
958 #define NAKCICHAR(opt, neg, code) \
960 len >= CILEN_CHAR && \
961 p[1] == CILEN_CHAR && \
965 GETCHAR(cichar, p); \
969 #define NAKCISHORT(opt, neg, code) \
971 len >= CILEN_SHORT && \
972 p[1] == CILEN_SHORT && \
974 len -= CILEN_SHORT; \
976 GETSHORT(cishort, p); \
980 #define NAKCILONG(opt, neg, code) \
982 len >= CILEN_LONG && \
983 p[1] == CILEN_LONG && \
987 GETLONG(cilong, p); \
991 #define NAKCILQR(opt, neg, code) \
993 len >= CILEN_LQR && \
994 p[1] == CILEN_LQR && \
998 GETSHORT(cishort, p); \
999 GETLONG(cilong, p); \
1003 #define NAKCIENDP(opt, neg) \
1005 len >= CILEN_CHAR && \
1007 p[1] >= CILEN_CHAR && \
1016 * NOTE! There must be no assignments to individual fields of *go in
1017 * the code below. Any such assignment is a BUG!
1020 * We don't care if they want to send us smaller packets than
1021 * we want. Therefore, accept any MRU less than what we asked for,
1022 * but then ignore the new value when setting the MRU in the kernel.
1023 * If they send us a bigger MRU than what we asked, accept it, up to
1024 * the limit of the default MRU we'd get if we didn't negotiate.
1026 if (go->neg_mru && go->mru != DEFMRU) {
1027 NAKCISHORT(CI_MRU, neg_mru,
1028 if (cishort <= wo->mru || cishort <= DEFMRU)
1034 * Add any characters they want to our (receive-side) asyncmap.
1036 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1037 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1038 try.asyncmap = go->asyncmap | cilong;
1043 * If they've nak'd our authentication-protocol, check whether
1044 * they are proposing a different protocol, or a different
1045 * hash algorithm for CHAP.
1047 if ((go->neg_chap || go->neg_upap || go->neg_eap)
1048 && len >= CILEN_SHORT
1049 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1052 no.neg_chap = go->neg_chap;
1053 no.neg_upap = go->neg_upap;
1054 no.neg_eap = go->neg_eap;
1056 GETSHORT(cishort, p);
1057 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1058 /* If we were asking for EAP, then we need to stop that. */
1062 /* If we were asking for CHAP, then we need to stop that. */
1063 else if (go->neg_chap)
1066 * If we weren't asking for CHAP or EAP, then we were asking for
1067 * PAP, in which case this Nak is bad.
1072 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1074 /* Stop asking for EAP, if we were. */
1077 /* Try to set up to use their suggestion, if possible */
1078 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1079 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1080 } else if (go->neg_chap) {
1082 * We were asking for our preferred algorithm, they must
1083 * want something different.
1085 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1086 if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1087 /* Use their suggestion if we support it ... */
1088 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1090 /* ... otherwise, try our next-preferred algorithm. */
1091 try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1092 if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1097 * Whoops, they Nak'd our algorithm of choice
1098 * but then suggested it back to us.
1104 * Stop asking for PAP if we were asking for it.
1112 * If we were asking for EAP, and they're Conf-Naking EAP,
1113 * well, that's just strange. Nobody should do that.
1115 if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1116 dbglog("Unexpected Conf-Nak for EAP");
1119 * We don't recognize what they're suggesting.
1120 * Stop asking for what we were asking for.
1124 else if (go->neg_chap)
1128 p += cilen - CILEN_SHORT;
1133 * If they can't cope with our link quality protocol, we'll have
1134 * to stop asking for LQR. We haven't got any other protocol.
1135 * If they Nak the reporting period, take their value XXX ?
1137 NAKCILQR(CI_QUALITY, neg_lqr,
1138 if (cishort != PPP_LQR)
1141 try.lqr_period = cilong;
1145 * Only implementing CBCP...not the rest of the callback options
1147 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1152 * Check for a looped-back line.
1154 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1155 try.magicnumber = magic();
1160 * Peer shouldn't send Nak for protocol compression or
1161 * address/control compression requests; they should send
1162 * a Reject instead. If they send a Nak, treat it as a Reject.
1164 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1165 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1168 * Nak for MRRU option - accept their value if it is smaller
1169 * than the one we want.
1172 NAKCISHORT(CI_MRRU, neg_mrru,
1173 if (treat_as_reject)
1175 else if (cishort <= wo->mrru)
1181 * Nak for short sequence numbers shouldn't be sent, treat it
1184 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1187 * Nak of the endpoint discriminator option is not permitted,
1188 * treat it like a reject.
1190 NAKCIENDP(CI_EPDISC, neg_endpoint);
1193 * There may be remaining CIs, if the peer is requesting negotiation
1194 * on an option that we didn't include in our request packet.
1195 * If we see an option that we requested, or one we've already seen
1196 * in this packet, then this packet is bad.
1197 * If we wanted to respond by starting to negotiate on the requested
1198 * option(s), we could, but we don't, because except for the
1199 * authentication type and quality protocol, if we are not negotiating
1200 * an option, it is because we were told not to.
1201 * For the authentication type, the Nak from the peer means
1202 * `let me authenticate myself with you' which is a bit pointless.
1203 * For the quality protocol, the Nak means `ask me to send you quality
1204 * reports', but if we didn't ask for them, we don't want them.
1205 * An option we don't recognize represents the peer asking to
1206 * negotiate some option we don't support, so ignore it.
1208 while (len >= CILEN_VOID) {
1211 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1213 next = p + cilen - 2;
1217 if ((go->neg_mru && go->mru != DEFMRU)
1218 || no.neg_mru || cilen != CILEN_SHORT)
1220 GETSHORT(cishort, p);
1221 if (cishort < DEFMRU) {
1227 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1228 || no.neg_asyncmap || cilen != CILEN_LONG)
1232 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1233 go->neg_eap || no.neg_eap)
1236 case CI_MAGICNUMBER:
1237 if (go->neg_magicnumber || no.neg_magicnumber ||
1238 cilen != CILEN_LONG)
1241 case CI_PCOMPRESSION:
1242 if (go->neg_pcompression || no.neg_pcompression
1243 || cilen != CILEN_VOID)
1246 case CI_ACCOMPRESSION:
1247 if (go->neg_accompression || no.neg_accompression
1248 || cilen != CILEN_VOID)
1252 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1256 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1260 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1265 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1273 * OK, the Nak is good. Now we can update state.
1274 * If there are any options left we ignore them.
1276 if (f->state != OPENED) {
1278 if (++try.numloops >= lcp_loopbackfail) {
1279 notice("Serial line is looped back.");
1280 ppp_set_status(EXIT_LOOPBACK);
1281 lcp_close(f->unit, "Loopback detected");
1291 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1297 * lcp_rejci - Peer has Rejected some of our CIs.
1298 * This should not modify any state if the Reject is bad
1299 * or if LCP is in the OPENED state.
1302 * 0 - Reject was bad.
1303 * 1 - Reject was good.
1306 lcp_rejci(fsm *f, u_char *p, int len)
1308 lcp_options *go = &lcp_gotoptions[f->unit];
1312 lcp_options try; /* options to request next time */
1317 * Any Rejected CIs must be in exactly the same order that we sent.
1318 * Check packet length and CI length at each step.
1319 * If we find any deviations, then this packet is bad.
1321 #define REJCIVOID(opt, neg) \
1323 len >= CILEN_VOID && \
1324 p[1] == CILEN_VOID && \
1326 len -= CILEN_VOID; \
1327 INCPTR(CILEN_VOID, p); \
1330 #define REJCISHORT(opt, neg, val) \
1332 len >= CILEN_SHORT && \
1333 p[1] == CILEN_SHORT && \
1335 len -= CILEN_SHORT; \
1337 GETSHORT(cishort, p); \
1338 /* Check rejected value. */ \
1339 if (cishort != val) \
1343 #define REJCICHAP(opt, neg, val) \
1345 len >= CILEN_CHAP && \
1346 p[1] == CILEN_CHAP && \
1348 len -= CILEN_CHAP; \
1350 GETSHORT(cishort, p); \
1351 GETCHAR(cichar, p); \
1352 /* Check rejected value. */ \
1353 if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1356 try.neg_eap = try.neg_upap = 0; \
1358 #define REJCILONG(opt, neg, val) \
1360 len >= CILEN_LONG && \
1361 p[1] == CILEN_LONG && \
1363 len -= CILEN_LONG; \
1365 GETLONG(cilong, p); \
1366 /* Check rejected value. */ \
1367 if (cilong != val) \
1371 #define REJCILQR(opt, neg, val) \
1373 len >= CILEN_LQR && \
1374 p[1] == CILEN_LQR && \
1378 GETSHORT(cishort, p); \
1379 GETLONG(cilong, p); \
1380 /* Check rejected value. */ \
1381 if (cishort != PPP_LQR || cilong != val) \
1385 #define REJCICBCP(opt, neg, val) \
1387 len >= CILEN_CBCP && \
1388 p[1] == CILEN_CBCP && \
1390 len -= CILEN_CBCP; \
1392 GETCHAR(cichar, p); \
1393 /* Check rejected value. */ \
1394 if (cichar != val) \
1398 #define REJCIENDP(opt, neg, class, val, vlen) \
1400 len >= CILEN_CHAR + vlen && \
1402 p[1] == CILEN_CHAR + vlen) { \
1404 len -= CILEN_CHAR + vlen; \
1406 GETCHAR(cichar, p); \
1407 if (cichar != class) \
1409 for (i = 0; i < vlen; ++i) { \
1410 GETCHAR(cichar, p); \
1411 if (cichar != val[i]) \
1417 REJCISHORT(CI_MRU, neg_mru, go->mru);
1418 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1419 REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1421 REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1422 if (!go->neg_chap) {
1423 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1426 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1427 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1428 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1429 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1430 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1431 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1432 REJCIVOID(CI_SSNHF, neg_ssnhf);
1433 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1434 go->endpoint.value, go->endpoint.length);
1437 * If there are any remaining CIs, then this packet is bad.
1442 * Now we can update state.
1444 if (f->state != OPENED)
1449 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1455 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1457 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1458 * appropriately. If reject_if_disagree is non-zero, doesn't return
1459 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1462 lcp_reqci(fsm *f, u_char *inp, int *lenp, int reject_if_disagree)
1464 lcp_options *go = &lcp_gotoptions[f->unit];
1465 lcp_options *ho = &lcp_hisoptions[f->unit];
1466 lcp_options *ao = &lcp_allowoptions[f->unit];
1467 u_char *cip, *next; /* Pointer to current and next CIs */
1468 int cilen, citype, cichar; /* Parsed len, type, char value */
1469 u_short cishort; /* Parsed short value */
1470 u_int32_t cilong; /* Parse long value */
1471 int rc = CONFACK; /* Final packet return code */
1472 int orc; /* Individual option return code */
1473 u_char *p; /* Pointer to next char to parse */
1474 u_char *rejp; /* Pointer to next char in reject frame */
1475 u_char *nakp; /* Pointer to next char in Nak frame */
1476 int l = *lenp; /* Length left */
1479 * Reset all his options.
1481 BZERO(ho, sizeof(*ho));
1484 * Process all his options.
1490 orc = CONFACK; /* Assume success */
1491 cip = p = next; /* Remember begining of CI */
1492 if (l < 2 || /* Not enough data for CI header or */
1493 p[1] < 2 || /* CI length too small or */
1494 p[1] > l) { /* CI length too big? */
1495 LCPDEBUG(("lcp_reqci: bad CI length!"));
1496 orc = CONFREJ; /* Reject bad CI */
1497 cilen = l; /* Reject till end of packet */
1498 l = 0; /* Don't loop again */
1502 GETCHAR(citype, p); /* Parse CI type */
1503 GETCHAR(cilen, p); /* Parse CI length */
1504 l -= cilen; /* Adjust remaining length */
1505 next += cilen; /* Step to next CI */
1507 switch (citype) { /* Check CI type */
1509 if (!ao->neg_mru || /* Allow option? */
1510 cilen != CILEN_SHORT) { /* Check CI length */
1511 orc = CONFREJ; /* Reject CI */
1514 GETSHORT(cishort, p); /* Parse MRU */
1517 * He must be able to receive at least our minimum.
1518 * No need to check a maximum. If he sends a large number,
1519 * we'll just ignore it.
1521 if (cishort < MINMRU) {
1522 orc = CONFNAK; /* Nak CI */
1523 PUTCHAR(CI_MRU, nakp);
1524 PUTCHAR(CILEN_SHORT, nakp);
1525 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1528 ho->neg_mru = 1; /* Remember he sent MRU */
1529 ho->mru = cishort; /* And remember value */
1533 if (!ao->neg_asyncmap ||
1534 cilen != CILEN_LONG) {
1541 * Asyncmap must have set at least the bits
1542 * which are set in lcp_allowoptions[unit].asyncmap.
1544 if ((ao->asyncmap & ~cilong) != 0) {
1546 PUTCHAR(CI_ASYNCMAP, nakp);
1547 PUTCHAR(CILEN_LONG, nakp);
1548 PUTLONG(ao->asyncmap | cilong, nakp);
1551 ho->neg_asyncmap = 1;
1552 ho->asyncmap = cilong;
1556 if (cilen < CILEN_SHORT ||
1557 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1559 * Reject the option if we're not willing to authenticate.
1561 dbglog("No auth is possible");
1565 GETSHORT(cishort, p);
1568 * Authtype must be PAP, CHAP, or EAP.
1570 * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1571 * ao->neg_eap are set, and the peer sends a Configure-Request
1572 * with two or more authenticate-protocol requests, then we will
1573 * reject the second request.
1574 * Whether we end up doing CHAP, UPAP, or EAP depends then on
1575 * the ordering of the CIs in the peer's Configure-Request.
1578 if (cishort == PPP_PAP) {
1579 /* we've already accepted CHAP or EAP */
1580 if (ho->neg_chap || ho->neg_eap ||
1581 cilen != CILEN_SHORT) {
1582 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1586 if (!ao->neg_upap) { /* we don't want to do PAP */
1587 orc = CONFNAK; /* NAK it and suggest CHAP or EAP */
1588 PUTCHAR(CI_AUTHTYPE, nakp);
1590 PUTCHAR(CILEN_SHORT, nakp);
1591 PUTSHORT(PPP_EAP, nakp);
1593 PUTCHAR(CILEN_CHAP, nakp);
1594 PUTSHORT(PPP_CHAP, nakp);
1595 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1602 if (cishort == PPP_CHAP) {
1603 /* we've already accepted PAP or EAP */
1604 if (ho->neg_upap || ho->neg_eap ||
1605 cilen != CILEN_CHAP) {
1606 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1610 if (!ao->neg_chap) { /* we don't want to do CHAP */
1611 orc = CONFNAK; /* NAK it and suggest EAP or PAP */
1612 PUTCHAR(CI_AUTHTYPE, nakp);
1613 PUTCHAR(CILEN_SHORT, nakp);
1615 PUTSHORT(PPP_EAP, nakp);
1617 PUTSHORT(PPP_PAP, nakp);
1621 GETCHAR(cichar, p); /* get digest type */
1622 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1624 * We can't/won't do the requested type,
1625 * suggest something else.
1628 PUTCHAR(CI_AUTHTYPE, nakp);
1629 PUTCHAR(CILEN_CHAP, nakp);
1630 PUTSHORT(PPP_CHAP, nakp);
1631 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1634 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1638 if (cishort == PPP_EAP) {
1639 /* we've already accepted CHAP or PAP */
1640 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1641 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1645 if (!ao->neg_eap) { /* we don't want to do EAP */
1646 orc = CONFNAK; /* NAK it and suggest CHAP or PAP */
1647 PUTCHAR(CI_AUTHTYPE, nakp);
1649 PUTCHAR(CILEN_CHAP, nakp);
1650 PUTSHORT(PPP_CHAP, nakp);
1651 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1653 PUTCHAR(CILEN_SHORT, nakp);
1654 PUTSHORT(PPP_PAP, nakp);
1663 * We don't recognize the protocol they're asking for.
1664 * Nak it with something we're willing to do.
1665 * (At this point we know ao->neg_upap || ao->neg_chap ||
1669 PUTCHAR(CI_AUTHTYPE, nakp);
1671 PUTCHAR(CILEN_SHORT, nakp);
1672 PUTSHORT(PPP_EAP, nakp);
1673 } else if (ao->neg_chap) {
1674 PUTCHAR(CILEN_CHAP, nakp);
1675 PUTSHORT(PPP_CHAP, nakp);
1676 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1678 PUTCHAR(CILEN_SHORT, nakp);
1679 PUTSHORT(PPP_PAP, nakp);
1685 cilen != CILEN_LQR) {
1690 GETSHORT(cishort, p);
1694 * Check the protocol and the reporting period.
1695 * XXX When should we Nak this, and what with?
1697 if (cishort != PPP_LQR) {
1699 PUTCHAR(CI_QUALITY, nakp);
1700 PUTCHAR(CILEN_LQR, nakp);
1701 PUTSHORT(PPP_LQR, nakp);
1702 PUTLONG(ao->lqr_period, nakp);
1707 case CI_MAGICNUMBER:
1708 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1709 cilen != CILEN_LONG) {
1716 * He must have a different magic number.
1718 if (go->neg_magicnumber &&
1719 cilong == go->magicnumber) {
1720 cilong = magic(); /* Don't put magic() inside macro! */
1722 PUTCHAR(CI_MAGICNUMBER, nakp);
1723 PUTCHAR(CILEN_LONG, nakp);
1724 PUTLONG(cilong, nakp);
1727 ho->neg_magicnumber = 1;
1728 ho->magicnumber = cilong;
1732 case CI_PCOMPRESSION:
1733 if (!ao->neg_pcompression ||
1734 cilen != CILEN_VOID) {
1738 ho->neg_pcompression = 1;
1741 case CI_ACCOMPRESSION:
1742 if (!ao->neg_accompression ||
1743 cilen != CILEN_VOID) {
1747 ho->neg_accompression = 1;
1751 if (!ao->neg_mrru || !multilink ||
1752 cilen != CILEN_SHORT) {
1757 GETSHORT(cishort, p);
1758 /* possibly should insist on a minimum/maximum MRRU here */
1764 if (!ao->neg_ssnhf || !multilink ||
1765 cilen != CILEN_VOID) {
1773 if (!ao->neg_endpoint ||
1774 cilen < CILEN_CHAR ||
1775 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1780 cilen -= CILEN_CHAR;
1781 ho->neg_endpoint = 1;
1782 ho->endpoint.class = cichar;
1783 ho->endpoint.length = cilen;
1784 BCOPY(p, ho->endpoint.value, cilen);
1789 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1795 if (orc == CONFACK && /* Good CI */
1796 rc != CONFACK) /* but prior CI wasnt? */
1797 continue; /* Don't send this one */
1799 if (orc == CONFNAK) { /* Nak this CI? */
1800 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1801 && citype != CI_MAGICNUMBER) {
1802 orc = CONFREJ; /* Get tough if so */
1804 if (rc == CONFREJ) /* Rejecting prior CI? */
1805 continue; /* Don't send this one */
1809 if (orc == CONFREJ) { /* Reject this CI */
1811 if (cip != rejp) /* Need to move rejected CI? */
1812 BCOPY(cip, rejp, cilen); /* Move it */
1813 INCPTR(cilen, rejp); /* Update output pointer */
1818 * If we wanted to send additional NAKs (for unsent CIs), the
1819 * code would go here. The extra NAKs would go at *nakp.
1820 * At present there are no cases where we want to ask the
1821 * peer to negotiate an option.
1830 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1832 *lenp = nakp - nak_buffer;
1833 BCOPY(nak_buffer, inp, *lenp);
1840 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1841 return (rc); /* Return final code */
1846 * lcp_up - LCP has come UP.
1851 lcp_options *wo = &lcp_wantoptions[f->unit];
1852 lcp_options *ho = &lcp_hisoptions[f->unit];
1853 lcp_options *go = &lcp_gotoptions[f->unit];
1854 lcp_options *ao = &lcp_allowoptions[f->unit];
1857 if (!go->neg_magicnumber)
1858 go->magicnumber = 0;
1859 if (!ho->neg_magicnumber)
1860 ho->magicnumber = 0;
1863 * Set our MTU to the smaller of the MTU we wanted and
1864 * the MRU our peer wanted. If we negotiated an MRU,
1865 * set our MRU to the larger of value we wanted and
1866 * the value we got in the negotiation.
1867 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1868 * the interface MTU is set to the lowest of that, the
1869 * MTU we want to use, and our link MRU.
1871 mtu = ho->neg_mru? ho->mru: PPP_MRU;
1872 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1873 #ifdef PPP_WITH_MULTILINK
1874 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1875 #endif /* PPP_WITH_MULTILINK */
1876 ppp_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1877 ppp_send_config(f->unit, mtu,
1878 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1879 ho->neg_pcompression, ho->neg_accompression);
1880 ppp_recv_config(f->unit, mru,
1881 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1882 go->neg_pcompression, go->neg_accompression);
1885 peer_mru[f->unit] = ho->mru;
1887 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1889 link_established(f->unit);
1894 * lcp_down - LCP has gone DOWN.
1896 * Alert other protocols.
1901 lcp_options *go = &lcp_gotoptions[f->unit];
1903 lcp_echo_lowerdown(f->unit);
1907 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1908 ppp_recv_config(f->unit, PPP_MRU,
1909 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1910 go->neg_pcompression, go->neg_accompression);
1911 peer_mru[f->unit] = PPP_MRU;
1916 * lcp_starting - LCP needs the lower layer up.
1919 lcp_starting(fsm *f)
1921 link_required(f->unit);
1926 * lcp_finished - LCP has finished with the lower layer.
1929 lcp_finished(fsm *f)
1931 link_terminated(f->unit);
1936 * lcp_printpkt - print the contents of an LCP packet.
1938 static char *lcp_codenames[] = {
1939 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1940 "TermReq", "TermAck", "CodeRej", "ProtRej",
1941 "EchoReq", "EchoRep", "DiscReq", "Ident",
1946 lcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
1948 int code, id, len, olen, i;
1949 u_char *pstart, *optend;
1953 if (plen < HEADERLEN)
1959 if (len < HEADERLEN || len > plen)
1962 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1963 printer(arg, " %s", lcp_codenames[code-1]);
1965 printer(arg, " code=0x%x", code);
1966 printer(arg, " id=0x%x", id);
1973 /* print option list */
1978 if (olen < 2 || olen > len) {
1986 if (olen == CILEN_SHORT) {
1988 GETSHORT(cishort, p);
1989 printer(arg, "mru %d", cishort);
1993 if (olen == CILEN_LONG) {
1996 printer(arg, "asyncmap 0x%x", cilong);
2000 if (olen >= CILEN_SHORT) {
2002 printer(arg, "auth ");
2003 GETSHORT(cishort, p);
2006 printer(arg, "pap");
2009 printer(arg, "chap");
2013 printer(arg, " MD5");
2016 case CHAP_MICROSOFT:
2017 printer(arg, " MS");
2021 case CHAP_MICROSOFT_V2:
2022 printer(arg, " MS-v2");
2029 printer(arg, "eap");
2032 printer(arg, "0x%x", cishort);
2037 if (olen >= CILEN_SHORT) {
2039 printer(arg, "quality ");
2040 GETSHORT(cishort, p);
2043 printer(arg, "lqr");
2046 printer(arg, "0x%x", cishort);
2051 if (olen >= CILEN_CHAR) {
2053 printer(arg, "callback ");
2054 GETCHAR(cishort, p);
2057 printer(arg, "CBCP");
2060 printer(arg, "0x%x", cishort);
2064 case CI_MAGICNUMBER:
2065 if (olen == CILEN_LONG) {
2068 printer(arg, "magic 0x%x", cilong);
2071 case CI_PCOMPRESSION:
2072 if (olen == CILEN_VOID) {
2074 printer(arg, "pcomp");
2077 case CI_ACCOMPRESSION:
2078 if (olen == CILEN_VOID) {
2080 printer(arg, "accomp");
2084 if (olen == CILEN_SHORT) {
2086 GETSHORT(cishort, p);
2087 printer(arg, "mrru %d", cishort);
2091 if (olen == CILEN_VOID) {
2093 printer(arg, "ssnhf");
2097 #ifdef PPP_WITH_MULTILINK
2098 if (olen >= CILEN_CHAR) {
2101 GETCHAR(epd.class, p);
2102 epd.length = olen - CILEN_CHAR;
2103 if (epd.length > MAX_ENDP_LEN)
2104 epd.length = MAX_ENDP_LEN;
2105 if (epd.length > 0) {
2106 BCOPY(p, epd.value, epd.length);
2109 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2112 printer(arg, "endpoint");
2116 while (p < optend) {
2118 printer(arg, " %.2x", code);
2126 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2128 print_string((char *)p, len, printer, arg);
2139 printer(arg, " magic=0x%x", cilong);
2148 printer(arg, " magic=0x%x", cilong);
2151 if (code == TIMEREM) {
2155 printer(arg, " seconds=%u", cilong);
2160 print_string((char *)p, len, printer, arg);
2167 /* print the rest of the bytes in the packet */
2168 for (i = 0; i < len && i < 32; ++i) {
2170 printer(arg, " %.2x", code);
2173 printer(arg, " ...");
2181 * Time to shut down the link because there is nothing out there.
2185 void LcpLinkFailure (fsm *f)
2187 if (f->state == OPENED) {
2188 info("No response to %d echo-requests", lcp_echos_pending);
2189 notice("Serial link appears to be disconnected.");
2190 ppp_set_status(EXIT_PEER_DEAD);
2191 lcp_close(f->unit, "Peer not responding");
2196 * Timer expired for the LCP echo requests from this process.
2200 LcpEchoCheck (fsm *f)
2202 LcpSendEchoRequest (f);
2203 if (f->state != OPENED)
2207 * Start the timer for the next interval.
2209 if (lcp_echo_timer_running)
2210 warn("assertion lcp_echo_timer_running==0 failed");
2211 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2212 lcp_echo_timer_running = 1;
2216 * LcpEchoTimeout - Timer expired on the LCP echo
2220 LcpEchoTimeout (void *arg)
2222 if (lcp_echo_timer_running != 0) {
2223 lcp_echo_timer_running = 0;
2224 LcpEchoCheck ((fsm *) arg);
2229 * LcpEchoReply - LCP has received a reply to the echo
2233 lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
2237 /* Check the magic number - don't count replies from ourselves. */
2239 dbglog("lcp: received short Echo-Reply, length %d", len);
2242 GETLONG(magic, inp);
2243 if (lcp_gotoptions[f->unit].neg_magicnumber
2244 && magic == lcp_gotoptions[f->unit].magicnumber) {
2245 warn("appear to have received our own echo-reply!");
2249 /* Reset the number of outstanding echo frames */
2250 lcp_echos_pending = 0;
2254 * LcpSendEchoRequest - Send an echo request frame to the peer
2258 LcpSendEchoRequest (fsm *f)
2260 u_int32_t lcp_magic;
2261 u_char pkt[4], *pktp;
2264 * Detect the failure of the peer at this point.
2266 if (lcp_echo_fails != 0) {
2267 if (lcp_echos_pending >= lcp_echo_fails) {
2269 lcp_echos_pending = 0;
2274 * If adaptive echos have been enabled, only send the echo request if
2275 * no traffic was received since the last one.
2277 if (lcp_echo_adaptive) {
2278 static unsigned int last_pkts_in = 0;
2279 struct pppd_stats cur_stats;
2281 if (get_ppp_stats(f->unit, &cur_stats) && cur_stats.pkts_in != last_pkts_in) {
2282 last_pkts_in = cur_stats.pkts_in;
2283 /* receipt of traffic indicates the link is working... */
2284 lcp_echos_pending = 0;
2290 * Make and send the echo request frame.
2292 if (f->state == OPENED) {
2293 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2295 PUTLONG(lcp_magic, pktp);
2296 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2297 ++lcp_echos_pending;
2302 * lcp_echo_lowerup - Start the timer for the LCP frame
2306 lcp_echo_lowerup (int unit)
2308 fsm *f = &lcp_fsm[unit];
2310 /* Clear the parameters for generating echo frames */
2311 lcp_echos_pending = 0;
2312 lcp_echo_number = 0;
2313 lcp_echo_timer_running = 0;
2315 /* If a timeout interval is specified then start the timer */
2316 if (lcp_echo_interval != 0)
2321 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2325 lcp_echo_lowerdown (int unit)
2327 fsm *f = &lcp_fsm[unit];
2329 if (lcp_echo_timer_running != 0) {
2330 UNTIMEOUT (LcpEchoTimeout, f);
2331 lcp_echo_timer_running = 0;