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.
60 * When the link comes up we want to be able to wait for a short while,
61 * or until seeing some input from the peer, before starting to send
62 * configure-requests. We do this by delaying the fsm_lowerup call.
64 /* steal a bit in fsm flags word */
65 #define DELAYED_UP 0x100
67 static void lcp_delayed_up(void *);
70 * LCP-related command-line options.
72 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
73 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
74 bool lcp_echo_adaptive = 0; /* request echo only if the link was idle */
75 bool lax_recv = 0; /* accept control chars in asyncmap */
76 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
78 static int noopt(char **);
80 #ifdef PPP_WITH_MULTILINK
81 static int setendpoint(char **);
82 static void printendpoint(option_t *, void (*)(void *, char *, ...), void *);
83 #endif /* PPP_WITH_MULTILINK */
85 static option_t lcp_option_list[] = {
87 { "-all", o_special_noarg, (void *)noopt,
88 "Don't request/allow any LCP options" },
90 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
91 "Disable address/control compression",
92 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
93 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
94 "Disable address/control compression",
95 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
97 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
98 "Set asyncmap (for received packets)",
99 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
100 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
101 "Set asyncmap (for received packets)",
102 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
103 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
104 "Disable asyncmap negotiation",
105 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
106 &lcp_allowoptions[0].neg_asyncmap },
107 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
108 "Disable asyncmap negotiation",
109 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
110 &lcp_allowoptions[0].neg_asyncmap },
112 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
113 "Disable magic number negotiation (looped-back line detection)",
114 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
115 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
116 "Disable magic number negotiation (looped-back line detection)",
117 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
119 { "mru", o_int, &lcp_wantoptions[0].mru,
120 "Set MRU (maximum received packet size) for negotiation",
121 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
122 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
123 "Disable MRU negotiation (use default 1500)",
124 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
125 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
126 "Disable MRU negotiation (use default 1500)",
127 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
129 { "mtu", o_int, &lcp_allowoptions[0].mru,
130 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
132 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
133 "Disable protocol field compression",
134 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
135 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
136 "Disable protocol field compression",
137 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
139 { "passive", o_bool, &lcp_wantoptions[0].passive,
140 "Set passive mode", 1 },
141 { "-p", o_bool, &lcp_wantoptions[0].passive,
142 "Set passive mode", OPT_ALIAS | 1 },
144 { "silent", o_bool, &lcp_wantoptions[0].silent,
145 "Set silent mode", 1 },
147 { "lcp-echo-failure", o_int, &lcp_echo_fails,
148 "Set number of consecutive echo failures to indicate link failure",
150 { "lcp-echo-interval", o_int, &lcp_echo_interval,
151 "Set time in seconds between LCP echo requests", OPT_PRIO },
152 { "lcp-echo-adaptive", o_bool, &lcp_echo_adaptive,
153 "Suppress LCP echo requests if traffic was received", 1 },
154 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
155 "Set time in seconds between LCP retransmissions", OPT_PRIO },
156 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
157 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
158 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
159 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
160 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
161 "Set limit on number of LCP configure-naks", OPT_PRIO },
163 { "receive-all", o_bool, &lax_recv,
164 "Accept all received control characters", 1 },
166 #ifdef PPP_WITH_MULTILINK
167 { "mrru", o_int, &lcp_wantoptions[0].mrru,
168 "Maximum received packet size for multilink bundle",
169 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
171 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
172 "Use short sequence numbers in multilink headers",
173 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
174 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
175 "Don't use short sequence numbers in multilink headers",
176 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
178 { "endpoint", o_special, (void *) setendpoint,
179 "Endpoint discriminator for multilink",
180 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
181 #endif /* PPP_WITH_MULTILINK */
183 { "noendpoint", o_bool, &noendpoint,
184 "Don't send or accept multilink endpoint discriminator", 1 },
190 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
191 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
192 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
193 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
194 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
196 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
197 static int lcp_echo_number = 0; /* ID number of next echo frame */
198 static int lcp_echo_timer_running = 0; /* set if a timer is running */
200 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
203 * Callbacks for fsm code. (CI = Configuration Information)
205 static void lcp_resetci(fsm *); /* Reset our CI */
206 static int lcp_cilen(fsm *); /* Return length of our CI */
207 static void lcp_addci(fsm *, u_char *, int *); /* Add our CI to pkt */
208 static int lcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
209 static int lcp_nakci(fsm *, u_char *, int, int); /* Peer nak'd our CI */
210 static int lcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
211 static int lcp_reqci(fsm *, u_char *, int *, int); /* Rcv peer CI */
212 static void lcp_up(fsm *); /* We're UP */
213 static void lcp_down(fsm *); /* We're DOWN */
214 static void lcp_starting(fsm *); /* We need lower layer up */
215 static void lcp_finished(fsm *); /* We need lower layer down */
216 static int lcp_extcode(fsm *, int, int, u_char *, int);
217 static void lcp_rprotrej(fsm *, u_char *, int);
220 * routines to send LCP echos to peer
223 static void lcp_echo_lowerup(int);
224 static void lcp_echo_lowerdown(int);
225 static void LcpEchoTimeout(void *);
226 static void lcp_received_echo_reply(fsm *, int, u_char *, int);
227 static void LcpSendEchoRequest(fsm *);
228 static void LcpLinkFailure(fsm *);
229 static void LcpEchoCheck(fsm *);
231 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
232 lcp_resetci, /* Reset our Configuration Information */
233 lcp_cilen, /* Length of our Configuration Information */
234 lcp_addci, /* Add our Configuration Information */
235 lcp_ackci, /* ACK our Configuration Information */
236 lcp_nakci, /* NAK our Configuration Information */
237 lcp_rejci, /* Reject our Configuration Information */
238 lcp_reqci, /* Request peer's Configuration Information */
239 lcp_up, /* Called when fsm reaches OPENED state */
240 lcp_down, /* Called when fsm leaves OPENED state */
241 lcp_starting, /* Called when we want the lower layer up */
242 lcp_finished, /* Called when we want the lower layer down */
243 NULL, /* Called when Protocol-Reject received */
244 NULL, /* Retransmission is necessary */
245 lcp_extcode, /* Called to handle LCP-specific codes */
246 "LCP" /* String name of protocol */
250 * Protocol entry points.
251 * Some of these are called directly.
254 static void lcp_init(int);
255 static void lcp_input(int, u_char *, int);
256 static void lcp_protrej(int);
257 static int lcp_printpkt(u_char *, int, void (*)(void *, char *, ...), void *);
259 struct protent lcp_protent = {
279 int lcp_loopbackfail = DEFLOOPBACKFAIL;
282 * Length of each type of configuration option (in octets)
286 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
287 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
288 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
289 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
292 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
293 (x) == CONFNAK ? "NAK" : "REJ")
296 * noopt - Disable all options (why?).
301 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
302 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
307 #ifdef PPP_WITH_MULTILINK
309 setendpoint(char **argv)
311 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
312 lcp_wantoptions[0].neg_endpoint = 1;
315 option_error("Can't parse '%s' as an endpoint discriminator", *argv);
320 printendpoint(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
322 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
324 #endif /* PPP_WITH_MULTILINK */
327 * lcp_init - Initialize LCP.
332 fsm *f = &lcp_fsm[unit];
333 lcp_options *wo = &lcp_wantoptions[unit];
334 lcp_options *ao = &lcp_allowoptions[unit];
337 f->protocol = PPP_LCP;
338 f->callbacks = &lcp_callbacks;
342 BZERO(wo, sizeof(*wo));
345 wo->neg_asyncmap = 1;
346 wo->neg_magicnumber = 1;
347 wo->neg_pcompression = 1;
348 wo->neg_accompression = 1;
350 BZERO(ao, sizeof(*ao));
353 ao->neg_asyncmap = 1;
355 ao->chap_mdtype = chap_mdtype_all;
358 ao->neg_magicnumber = 1;
359 ao->neg_pcompression = 1;
360 ao->neg_accompression = 1;
361 ao->neg_endpoint = 1;
366 * lcp_open - LCP is allowed to come up.
371 fsm *f = &lcp_fsm[unit];
372 lcp_options *wo = &lcp_wantoptions[unit];
374 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
376 f->flags |= OPT_PASSIVE;
378 f->flags |= OPT_SILENT;
384 * lcp_close - Take LCP down.
387 lcp_close(int unit, char *reason)
389 fsm *f = &lcp_fsm[unit];
392 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
393 new_phase(PHASE_TERMINATE);
395 if (f->flags & DELAYED_UP) {
396 untimeout(lcp_delayed_up, f);
401 fsm_close(f, reason);
402 if (oldstate == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT|DELAYED_UP)) {
404 * This action is not strictly according to the FSM in RFC1548,
405 * but it does mean that the program terminates if you do a
406 * lcp_close() when a connection hasn't been established
407 * because we are in passive/silent mode or because we have
408 * delayed the fsm_lowerup() call and it hasn't happened yet.
410 f->flags &= ~DELAYED_UP;
417 * lcp_lowerup - The lower layer is up.
420 lcp_lowerup(int unit)
422 lcp_options *wo = &lcp_wantoptions[unit];
423 fsm *f = &lcp_fsm[unit];
426 * Don't use A/C or protocol compression on transmission,
427 * but accept A/C and protocol compressed packets
428 * if we are going to ask for A/C and protocol compression.
430 if (ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0) < 0
431 || ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
432 wo->neg_pcompression, wo->neg_accompression) < 0)
434 peer_mru[unit] = PPP_MRU;
436 if (listen_time != 0) {
437 f->flags |= DELAYED_UP;
438 timeout(lcp_delayed_up, f, 0, listen_time * 1000);
445 * lcp_lowerdown - The lower layer is down.
448 lcp_lowerdown(int unit)
450 fsm *f = &lcp_fsm[unit];
452 if (f->flags & DELAYED_UP) {
453 f->flags &= ~DELAYED_UP;
454 untimeout(lcp_delayed_up, f);
456 fsm_lowerdown(&lcp_fsm[unit]);
461 * lcp_delayed_up - Bring the lower layer up now.
464 lcp_delayed_up(void *arg)
468 if (f->flags & DELAYED_UP) {
469 f->flags &= ~DELAYED_UP;
476 * lcp_input - Input LCP packet.
479 lcp_input(int unit, u_char *p, int len)
481 fsm *f = &lcp_fsm[unit];
483 if (f->flags & DELAYED_UP) {
484 f->flags &= ~DELAYED_UP;
485 untimeout(lcp_delayed_up, f);
488 fsm_input(f, p, len);
492 * lcp_extcode - Handle a LCP-specific code.
495 lcp_extcode(fsm *f, int code, int id, u_char *inp, int len)
501 lcp_rprotrej(f, inp, len);
505 if (f->state != OPENED)
508 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
509 fsm_sdata(f, ECHOREP, id, inp, len);
513 lcp_received_echo_reply(f, id, inp, len);
529 * lcp_rprotrej - Receive an Protocol-Reject.
531 * Figure out which protocol is rejected and inform it.
534 lcp_rprotrej(fsm *f, u_char *inp, int len)
537 struct protent *protp;
542 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
549 * Protocol-Reject packets received in any state other than the LCP
550 * OPENED state SHOULD be silently discarded.
552 if( f->state != OPENED ){
553 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
557 pname = protocol_name(prot);
560 * Upcall the proper Protocol-Reject routine.
562 for (i = 0; (protp = protocols[i]) != NULL; ++i)
563 if (protp->protocol == prot && protp->enabled_flag) {
565 dbglog("Protocol-Reject for 0x%x received", prot);
567 dbglog("Protocol-Reject for '%s' (0x%x) received", pname,
569 (*protp->protrej)(f->unit);
574 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
576 warn("Protocol-Reject for unsupported protocol '%s' (0x%x)", pname,
582 * lcp_protrej - A Protocol-Reject was received.
586 lcp_protrej(int unit)
591 error("Received Protocol-Reject for LCP!");
592 fsm_protreject(&lcp_fsm[unit]);
597 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
600 lcp_sprotrej(int unit, u_char *p, int len)
603 * Send back the protocol and the information field of the
604 * rejected packet. We only get here if LCP is in the OPENED state.
609 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
615 * lcp_resetci - Reset our CI.
620 lcp_options *wo = &lcp_wantoptions[f->unit];
621 lcp_options *go = &lcp_gotoptions[f->unit];
622 lcp_options *ao = &lcp_allowoptions[f->unit];
624 wo->magicnumber = magic();
630 go->neg_endpoint = 0;
633 ao->neg_endpoint = 0;
634 peer_mru[f->unit] = PPP_MRU;
640 * lcp_cilen - Return length of our CI.
645 lcp_options *go = &lcp_gotoptions[f->unit];
647 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
648 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
649 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
650 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
651 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
652 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
654 * NB: we only ask for one of CHAP, UPAP, or EAP, even if we will
655 * accept more than one. We prefer EAP first, then CHAP, then
658 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
659 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
660 LENCISHORT(go->neg_eap) +
661 LENCICHAP(!go->neg_eap && go->neg_chap) +
662 LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
663 LENCILQR(go->neg_lqr) +
664 LENCICBCP(go->neg_cbcp) +
665 LENCILONG(go->neg_magicnumber) +
666 LENCIVOID(go->neg_pcompression) +
667 LENCIVOID(go->neg_accompression) +
668 LENCISHORT(go->neg_mrru) +
669 LENCIVOID(go->neg_ssnhf) +
670 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
675 * lcp_addci - Add our desired CIs to a packet.
678 lcp_addci(fsm *f, u_char *ucp, int *lenp)
680 lcp_options *go = &lcp_gotoptions[f->unit];
681 u_char *start_ucp = ucp;
683 #define ADDCIVOID(opt, neg) \
686 PUTCHAR(CILEN_VOID, ucp); \
688 #define ADDCISHORT(opt, neg, val) \
691 PUTCHAR(CILEN_SHORT, ucp); \
692 PUTSHORT(val, ucp); \
694 #define ADDCICHAP(opt, neg, val) \
696 PUTCHAR((opt), ucp); \
697 PUTCHAR(CILEN_CHAP, ucp); \
698 PUTSHORT(PPP_CHAP, ucp); \
699 PUTCHAR((CHAP_DIGEST(val)), ucp); \
701 #define ADDCILONG(opt, neg, val) \
704 PUTCHAR(CILEN_LONG, ucp); \
707 #define ADDCILQR(opt, neg, val) \
710 PUTCHAR(CILEN_LQR, ucp); \
711 PUTSHORT(PPP_LQR, ucp); \
714 #define ADDCICHAR(opt, neg, val) \
717 PUTCHAR(CILEN_CHAR, ucp); \
720 #define ADDCIENDP(opt, neg, class, val, len) \
724 PUTCHAR(CILEN_CHAR + len, ucp); \
725 PUTCHAR(class, ucp); \
726 for (i = 0; i < len; ++i) \
727 PUTCHAR(val[i], ucp); \
730 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
731 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
733 ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
734 ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
735 ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
737 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
738 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
739 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
740 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
741 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
742 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
743 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
744 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
745 go->endpoint.value, go->endpoint.length);
747 if (ucp - start_ucp != *lenp) {
748 /* this should never happen, because peer_mtu should be 1500 */
749 error("Bug in lcp_addci: wrong length");
755 * lcp_ackci - Ack our CIs.
756 * This should not modify any state if the Ack is bad.
763 lcp_ackci(fsm *f, u_char *p, int len)
765 lcp_options *go = &lcp_gotoptions[f->unit];
766 u_char cilen, citype, cichar;
771 * CIs must be in exactly the same order that we sent.
772 * Check packet length and CI length at each step.
773 * If we find any deviations, then this packet is bad.
775 #define ACKCIVOID(opt, neg) \
777 if ((len -= CILEN_VOID) < 0) \
779 GETCHAR(citype, p); \
781 if (cilen != CILEN_VOID || \
785 #define ACKCISHORT(opt, neg, val) \
787 if ((len -= CILEN_SHORT) < 0) \
789 GETCHAR(citype, p); \
791 if (cilen != CILEN_SHORT || \
794 GETSHORT(cishort, p); \
795 if (cishort != val) \
798 #define ACKCICHAR(opt, neg, val) \
800 if ((len -= CILEN_CHAR) < 0) \
802 GETCHAR(citype, p); \
804 if (cilen != CILEN_CHAR || \
807 GETCHAR(cichar, p); \
811 #define ACKCICHAP(opt, neg, val) \
813 if ((len -= CILEN_CHAP) < 0) \
815 GETCHAR(citype, p); \
817 if (cilen != CILEN_CHAP || \
820 GETSHORT(cishort, p); \
821 if (cishort != PPP_CHAP) \
823 GETCHAR(cichar, p); \
824 if (cichar != (CHAP_DIGEST(val))) \
827 #define ACKCILONG(opt, neg, val) \
829 if ((len -= CILEN_LONG) < 0) \
831 GETCHAR(citype, p); \
833 if (cilen != CILEN_LONG || \
836 GETLONG(cilong, p); \
840 #define ACKCILQR(opt, neg, val) \
842 if ((len -= CILEN_LQR) < 0) \
844 GETCHAR(citype, p); \
846 if (cilen != CILEN_LQR || \
849 GETSHORT(cishort, p); \
850 if (cishort != PPP_LQR) \
852 GETLONG(cilong, p); \
856 #define ACKCIENDP(opt, neg, class, val, vlen) \
859 if ((len -= CILEN_CHAR + vlen) < 0) \
861 GETCHAR(citype, p); \
863 if (cilen != CILEN_CHAR + vlen || \
866 GETCHAR(cichar, p); \
867 if (cichar != class) \
869 for (i = 0; i < vlen; ++i) { \
870 GETCHAR(cichar, p); \
871 if (cichar != val[i]) \
876 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
877 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
879 ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
880 ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
881 ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
883 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
884 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
885 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
886 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
887 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
888 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
889 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
890 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
891 go->endpoint.value, go->endpoint.length);
894 * If there are any remaining CIs, then this packet is bad.
900 LCPDEBUG(("lcp_acki: received bad Ack!"));
906 * lcp_nakci - Peer has sent a NAK for some of our CIs.
907 * This should not modify any state if the Nak is bad
908 * or if LCP is in the OPENED state.
915 lcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
917 lcp_options *go = &lcp_gotoptions[f->unit];
918 lcp_options *wo = &lcp_wantoptions[f->unit];
919 u_char citype, cichar, *next;
922 lcp_options no; /* options we've seen Naks for */
923 lcp_options try; /* options to request next time */
927 BZERO(&no, sizeof(no));
931 * Any Nak'd CIs must be in exactly the same order that we sent.
932 * Check packet length and CI length at each step.
933 * If we find any deviations, then this packet is bad.
935 #define NAKCIVOID(opt, neg) \
937 len >= CILEN_VOID && \
938 p[1] == CILEN_VOID && \
941 INCPTR(CILEN_VOID, p); \
945 #define NAKCICHAP(opt, neg, code) \
947 len >= CILEN_CHAP && \
948 p[1] == CILEN_CHAP && \
952 GETSHORT(cishort, p); \
953 GETCHAR(cichar, p); \
957 #define NAKCICHAR(opt, neg, code) \
959 len >= CILEN_CHAR && \
960 p[1] == CILEN_CHAR && \
964 GETCHAR(cichar, p); \
968 #define NAKCISHORT(opt, neg, code) \
970 len >= CILEN_SHORT && \
971 p[1] == CILEN_SHORT && \
973 len -= CILEN_SHORT; \
975 GETSHORT(cishort, p); \
979 #define NAKCILONG(opt, neg, code) \
981 len >= CILEN_LONG && \
982 p[1] == CILEN_LONG && \
986 GETLONG(cilong, p); \
990 #define NAKCILQR(opt, neg, code) \
992 len >= CILEN_LQR && \
993 p[1] == CILEN_LQR && \
997 GETSHORT(cishort, p); \
998 GETLONG(cilong, p); \
1002 #define NAKCIENDP(opt, neg) \
1004 len >= CILEN_CHAR && \
1006 p[1] >= CILEN_CHAR && \
1015 * NOTE! There must be no assignments to individual fields of *go in
1016 * the code below. Any such assignment is a BUG!
1019 * We don't care if they want to send us smaller packets than
1020 * we want. Therefore, accept any MRU less than what we asked for,
1021 * but then ignore the new value when setting the MRU in the kernel.
1022 * If they send us a bigger MRU than what we asked, accept it, up to
1023 * the limit of the default MRU we'd get if we didn't negotiate.
1025 if (go->neg_mru && go->mru != DEFMRU) {
1026 NAKCISHORT(CI_MRU, neg_mru,
1027 if (cishort <= wo->mru || cishort <= DEFMRU)
1033 * Add any characters they want to our (receive-side) asyncmap.
1035 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1036 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1037 try.asyncmap = go->asyncmap | cilong;
1042 * If they've nak'd our authentication-protocol, check whether
1043 * they are proposing a different protocol, or a different
1044 * hash algorithm for CHAP.
1046 if ((go->neg_chap || go->neg_upap || go->neg_eap)
1047 && len >= CILEN_SHORT
1048 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1051 no.neg_chap = go->neg_chap;
1052 no.neg_upap = go->neg_upap;
1053 no.neg_eap = go->neg_eap;
1055 GETSHORT(cishort, p);
1056 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1057 /* If we were asking for EAP, then we need to stop that. */
1061 /* If we were asking for CHAP, then we need to stop that. */
1062 else if (go->neg_chap)
1065 * If we weren't asking for CHAP or EAP, then we were asking for
1066 * PAP, in which case this Nak is bad.
1071 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1073 /* Stop asking for EAP, if we were. */
1076 /* Try to set up to use their suggestion, if possible */
1077 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1078 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1079 } else if (go->neg_chap) {
1081 * We were asking for our preferred algorithm, they must
1082 * want something different.
1084 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1085 if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1086 /* Use their suggestion if we support it ... */
1087 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1089 /* ... otherwise, try our next-preferred algorithm. */
1090 try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1091 if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1096 * Whoops, they Nak'd our algorithm of choice
1097 * but then suggested it back to us.
1103 * Stop asking for PAP if we were asking for it.
1111 * If we were asking for EAP, and they're Conf-Naking EAP,
1112 * well, that's just strange. Nobody should do that.
1114 if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1115 dbglog("Unexpected Conf-Nak for EAP");
1118 * We don't recognize what they're suggesting.
1119 * Stop asking for what we were asking for.
1123 else if (go->neg_chap)
1127 p += cilen - CILEN_SHORT;
1132 * If they can't cope with our link quality protocol, we'll have
1133 * to stop asking for LQR. We haven't got any other protocol.
1134 * If they Nak the reporting period, take their value XXX ?
1136 NAKCILQR(CI_QUALITY, neg_lqr,
1137 if (cishort != PPP_LQR)
1140 try.lqr_period = cilong;
1144 * Only implementing CBCP...not the rest of the callback options
1146 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1151 * Check for a looped-back line.
1153 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1154 try.magicnumber = magic();
1159 * Peer shouldn't send Nak for protocol compression or
1160 * address/control compression requests; they should send
1161 * a Reject instead. If they send a Nak, treat it as a Reject.
1163 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1164 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1167 * Nak for MRRU option - accept their value if it is smaller
1168 * than the one we want.
1171 NAKCISHORT(CI_MRRU, neg_mrru,
1172 if (treat_as_reject)
1174 else if (cishort <= wo->mrru)
1180 * Nak for short sequence numbers shouldn't be sent, treat it
1183 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1186 * Nak of the endpoint discriminator option is not permitted,
1187 * treat it like a reject.
1189 NAKCIENDP(CI_EPDISC, neg_endpoint);
1192 * There may be remaining CIs, if the peer is requesting negotiation
1193 * on an option that we didn't include in our request packet.
1194 * If we see an option that we requested, or one we've already seen
1195 * in this packet, then this packet is bad.
1196 * If we wanted to respond by starting to negotiate on the requested
1197 * option(s), we could, but we don't, because except for the
1198 * authentication type and quality protocol, if we are not negotiating
1199 * an option, it is because we were told not to.
1200 * For the authentication type, the Nak from the peer means
1201 * `let me authenticate myself with you' which is a bit pointless.
1202 * For the quality protocol, the Nak means `ask me to send you quality
1203 * reports', but if we didn't ask for them, we don't want them.
1204 * An option we don't recognize represents the peer asking to
1205 * negotiate some option we don't support, so ignore it.
1207 while (len >= CILEN_VOID) {
1210 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1212 next = p + cilen - 2;
1216 if ((go->neg_mru && go->mru != DEFMRU)
1217 || no.neg_mru || cilen != CILEN_SHORT)
1219 GETSHORT(cishort, p);
1220 if (cishort < DEFMRU) {
1226 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1227 || no.neg_asyncmap || cilen != CILEN_LONG)
1231 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1232 go->neg_eap || no.neg_eap)
1235 case CI_MAGICNUMBER:
1236 if (go->neg_magicnumber || no.neg_magicnumber ||
1237 cilen != CILEN_LONG)
1240 case CI_PCOMPRESSION:
1241 if (go->neg_pcompression || no.neg_pcompression
1242 || cilen != CILEN_VOID)
1245 case CI_ACCOMPRESSION:
1246 if (go->neg_accompression || no.neg_accompression
1247 || cilen != CILEN_VOID)
1251 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1255 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1259 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1264 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1272 * OK, the Nak is good. Now we can update state.
1273 * If there are any options left we ignore them.
1275 if (f->state != OPENED) {
1277 if (++try.numloops >= lcp_loopbackfail) {
1278 notice("Serial line is looped back.");
1279 status = EXIT_LOOPBACK;
1280 lcp_close(f->unit, "Loopback detected");
1290 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1296 * lcp_rejci - Peer has Rejected some of our CIs.
1297 * This should not modify any state if the Reject is bad
1298 * or if LCP is in the OPENED state.
1301 * 0 - Reject was bad.
1302 * 1 - Reject was good.
1305 lcp_rejci(fsm *f, u_char *p, int len)
1307 lcp_options *go = &lcp_gotoptions[f->unit];
1311 lcp_options try; /* options to request next time */
1316 * Any Rejected CIs must be in exactly the same order that we sent.
1317 * Check packet length and CI length at each step.
1318 * If we find any deviations, then this packet is bad.
1320 #define REJCIVOID(opt, neg) \
1322 len >= CILEN_VOID && \
1323 p[1] == CILEN_VOID && \
1325 len -= CILEN_VOID; \
1326 INCPTR(CILEN_VOID, p); \
1329 #define REJCISHORT(opt, neg, val) \
1331 len >= CILEN_SHORT && \
1332 p[1] == CILEN_SHORT && \
1334 len -= CILEN_SHORT; \
1336 GETSHORT(cishort, p); \
1337 /* Check rejected value. */ \
1338 if (cishort != val) \
1342 #define REJCICHAP(opt, neg, val) \
1344 len >= CILEN_CHAP && \
1345 p[1] == CILEN_CHAP && \
1347 len -= CILEN_CHAP; \
1349 GETSHORT(cishort, p); \
1350 GETCHAR(cichar, p); \
1351 /* Check rejected value. */ \
1352 if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1355 try.neg_eap = try.neg_upap = 0; \
1357 #define REJCILONG(opt, neg, val) \
1359 len >= CILEN_LONG && \
1360 p[1] == CILEN_LONG && \
1362 len -= CILEN_LONG; \
1364 GETLONG(cilong, p); \
1365 /* Check rejected value. */ \
1366 if (cilong != val) \
1370 #define REJCILQR(opt, neg, val) \
1372 len >= CILEN_LQR && \
1373 p[1] == CILEN_LQR && \
1377 GETSHORT(cishort, p); \
1378 GETLONG(cilong, p); \
1379 /* Check rejected value. */ \
1380 if (cishort != PPP_LQR || cilong != val) \
1384 #define REJCICBCP(opt, neg, val) \
1386 len >= CILEN_CBCP && \
1387 p[1] == CILEN_CBCP && \
1389 len -= CILEN_CBCP; \
1391 GETCHAR(cichar, p); \
1392 /* Check rejected value. */ \
1393 if (cichar != val) \
1397 #define REJCIENDP(opt, neg, class, val, vlen) \
1399 len >= CILEN_CHAR + vlen && \
1401 p[1] == CILEN_CHAR + vlen) { \
1403 len -= CILEN_CHAR + vlen; \
1405 GETCHAR(cichar, p); \
1406 if (cichar != class) \
1408 for (i = 0; i < vlen; ++i) { \
1409 GETCHAR(cichar, p); \
1410 if (cichar != val[i]) \
1416 REJCISHORT(CI_MRU, neg_mru, go->mru);
1417 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1418 REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1420 REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1421 if (!go->neg_chap) {
1422 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1425 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1426 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1427 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1428 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1429 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1430 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1431 REJCIVOID(CI_SSNHF, neg_ssnhf);
1432 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1433 go->endpoint.value, go->endpoint.length);
1436 * If there are any remaining CIs, then this packet is bad.
1441 * Now we can update state.
1443 if (f->state != OPENED)
1448 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1454 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1456 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1457 * appropriately. If reject_if_disagree is non-zero, doesn't return
1458 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1461 lcp_reqci(fsm *f, u_char *inp, int *lenp, int reject_if_disagree)
1463 lcp_options *go = &lcp_gotoptions[f->unit];
1464 lcp_options *ho = &lcp_hisoptions[f->unit];
1465 lcp_options *ao = &lcp_allowoptions[f->unit];
1466 u_char *cip, *next; /* Pointer to current and next CIs */
1467 int cilen, citype, cichar; /* Parsed len, type, char value */
1468 u_short cishort; /* Parsed short value */
1469 u_int32_t cilong; /* Parse long value */
1470 int rc = CONFACK; /* Final packet return code */
1471 int orc; /* Individual option return code */
1472 u_char *p; /* Pointer to next char to parse */
1473 u_char *rejp; /* Pointer to next char in reject frame */
1474 u_char *nakp; /* Pointer to next char in Nak frame */
1475 int l = *lenp; /* Length left */
1478 * Reset all his options.
1480 BZERO(ho, sizeof(*ho));
1483 * Process all his options.
1489 orc = CONFACK; /* Assume success */
1490 cip = p = next; /* Remember begining of CI */
1491 if (l < 2 || /* Not enough data for CI header or */
1492 p[1] < 2 || /* CI length too small or */
1493 p[1] > l) { /* CI length too big? */
1494 LCPDEBUG(("lcp_reqci: bad CI length!"));
1495 orc = CONFREJ; /* Reject bad CI */
1496 cilen = l; /* Reject till end of packet */
1497 l = 0; /* Don't loop again */
1501 GETCHAR(citype, p); /* Parse CI type */
1502 GETCHAR(cilen, p); /* Parse CI length */
1503 l -= cilen; /* Adjust remaining length */
1504 next += cilen; /* Step to next CI */
1506 switch (citype) { /* Check CI type */
1508 if (!ao->neg_mru || /* Allow option? */
1509 cilen != CILEN_SHORT) { /* Check CI length */
1510 orc = CONFREJ; /* Reject CI */
1513 GETSHORT(cishort, p); /* Parse MRU */
1516 * He must be able to receive at least our minimum.
1517 * No need to check a maximum. If he sends a large number,
1518 * we'll just ignore it.
1520 if (cishort < MINMRU) {
1521 orc = CONFNAK; /* Nak CI */
1522 PUTCHAR(CI_MRU, nakp);
1523 PUTCHAR(CILEN_SHORT, nakp);
1524 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1527 ho->neg_mru = 1; /* Remember he sent MRU */
1528 ho->mru = cishort; /* And remember value */
1532 if (!ao->neg_asyncmap ||
1533 cilen != CILEN_LONG) {
1540 * Asyncmap must have set at least the bits
1541 * which are set in lcp_allowoptions[unit].asyncmap.
1543 if ((ao->asyncmap & ~cilong) != 0) {
1545 PUTCHAR(CI_ASYNCMAP, nakp);
1546 PUTCHAR(CILEN_LONG, nakp);
1547 PUTLONG(ao->asyncmap | cilong, nakp);
1550 ho->neg_asyncmap = 1;
1551 ho->asyncmap = cilong;
1555 if (cilen < CILEN_SHORT ||
1556 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1558 * Reject the option if we're not willing to authenticate.
1560 dbglog("No auth is possible");
1564 GETSHORT(cishort, p);
1567 * Authtype must be PAP, CHAP, or EAP.
1569 * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1570 * ao->neg_eap are set, and the peer sends a Configure-Request
1571 * with two or more authenticate-protocol requests, then we will
1572 * reject the second request.
1573 * Whether we end up doing CHAP, UPAP, or EAP depends then on
1574 * the ordering of the CIs in the peer's Configure-Request.
1577 if (cishort == PPP_PAP) {
1578 /* we've already accepted CHAP or EAP */
1579 if (ho->neg_chap || ho->neg_eap ||
1580 cilen != CILEN_SHORT) {
1581 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1585 if (!ao->neg_upap) { /* we don't want to do PAP */
1586 orc = CONFNAK; /* NAK it and suggest CHAP or EAP */
1587 PUTCHAR(CI_AUTHTYPE, nakp);
1589 PUTCHAR(CILEN_SHORT, nakp);
1590 PUTSHORT(PPP_EAP, nakp);
1592 PUTCHAR(CILEN_CHAP, nakp);
1593 PUTSHORT(PPP_CHAP, nakp);
1594 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1601 if (cishort == PPP_CHAP) {
1602 /* we've already accepted PAP or EAP */
1603 if (ho->neg_upap || ho->neg_eap ||
1604 cilen != CILEN_CHAP) {
1605 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1609 if (!ao->neg_chap) { /* we don't want to do CHAP */
1610 orc = CONFNAK; /* NAK it and suggest EAP or PAP */
1611 PUTCHAR(CI_AUTHTYPE, nakp);
1612 PUTCHAR(CILEN_SHORT, nakp);
1614 PUTSHORT(PPP_EAP, nakp);
1616 PUTSHORT(PPP_PAP, nakp);
1620 GETCHAR(cichar, p); /* get digest type */
1621 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1623 * We can't/won't do the requested type,
1624 * suggest something else.
1627 PUTCHAR(CI_AUTHTYPE, nakp);
1628 PUTCHAR(CILEN_CHAP, nakp);
1629 PUTSHORT(PPP_CHAP, nakp);
1630 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1633 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1637 if (cishort == PPP_EAP) {
1638 /* we've already accepted CHAP or PAP */
1639 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1640 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1644 if (!ao->neg_eap) { /* we don't want to do EAP */
1645 orc = CONFNAK; /* NAK it and suggest CHAP or PAP */
1646 PUTCHAR(CI_AUTHTYPE, nakp);
1648 PUTCHAR(CILEN_CHAP, nakp);
1649 PUTSHORT(PPP_CHAP, nakp);
1650 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1652 PUTCHAR(CILEN_SHORT, nakp);
1653 PUTSHORT(PPP_PAP, nakp);
1662 * We don't recognize the protocol they're asking for.
1663 * Nak it with something we're willing to do.
1664 * (At this point we know ao->neg_upap || ao->neg_chap ||
1668 PUTCHAR(CI_AUTHTYPE, nakp);
1670 PUTCHAR(CILEN_SHORT, nakp);
1671 PUTSHORT(PPP_EAP, nakp);
1672 } else if (ao->neg_chap) {
1673 PUTCHAR(CILEN_CHAP, nakp);
1674 PUTSHORT(PPP_CHAP, nakp);
1675 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1677 PUTCHAR(CILEN_SHORT, nakp);
1678 PUTSHORT(PPP_PAP, nakp);
1684 cilen != CILEN_LQR) {
1689 GETSHORT(cishort, p);
1693 * Check the protocol and the reporting period.
1694 * XXX When should we Nak this, and what with?
1696 if (cishort != PPP_LQR) {
1698 PUTCHAR(CI_QUALITY, nakp);
1699 PUTCHAR(CILEN_LQR, nakp);
1700 PUTSHORT(PPP_LQR, nakp);
1701 PUTLONG(ao->lqr_period, nakp);
1706 case CI_MAGICNUMBER:
1707 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1708 cilen != CILEN_LONG) {
1715 * He must have a different magic number.
1717 if (go->neg_magicnumber &&
1718 cilong == go->magicnumber) {
1719 cilong = magic(); /* Don't put magic() inside macro! */
1721 PUTCHAR(CI_MAGICNUMBER, nakp);
1722 PUTCHAR(CILEN_LONG, nakp);
1723 PUTLONG(cilong, nakp);
1726 ho->neg_magicnumber = 1;
1727 ho->magicnumber = cilong;
1731 case CI_PCOMPRESSION:
1732 if (!ao->neg_pcompression ||
1733 cilen != CILEN_VOID) {
1737 ho->neg_pcompression = 1;
1740 case CI_ACCOMPRESSION:
1741 if (!ao->neg_accompression ||
1742 cilen != CILEN_VOID) {
1746 ho->neg_accompression = 1;
1750 if (!ao->neg_mrru || !multilink ||
1751 cilen != CILEN_SHORT) {
1756 GETSHORT(cishort, p);
1757 /* possibly should insist on a minimum/maximum MRRU here */
1763 if (!ao->neg_ssnhf || !multilink ||
1764 cilen != CILEN_VOID) {
1772 if (!ao->neg_endpoint ||
1773 cilen < CILEN_CHAR ||
1774 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1779 cilen -= CILEN_CHAR;
1780 ho->neg_endpoint = 1;
1781 ho->endpoint.class = cichar;
1782 ho->endpoint.length = cilen;
1783 BCOPY(p, ho->endpoint.value, cilen);
1788 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1794 if (orc == CONFACK && /* Good CI */
1795 rc != CONFACK) /* but prior CI wasnt? */
1796 continue; /* Don't send this one */
1798 if (orc == CONFNAK) { /* Nak this CI? */
1799 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1800 && citype != CI_MAGICNUMBER) {
1801 orc = CONFREJ; /* Get tough if so */
1803 if (rc == CONFREJ) /* Rejecting prior CI? */
1804 continue; /* Don't send this one */
1808 if (orc == CONFREJ) { /* Reject this CI */
1810 if (cip != rejp) /* Need to move rejected CI? */
1811 BCOPY(cip, rejp, cilen); /* Move it */
1812 INCPTR(cilen, rejp); /* Update output pointer */
1817 * If we wanted to send additional NAKs (for unsent CIs), the
1818 * code would go here. The extra NAKs would go at *nakp.
1819 * At present there are no cases where we want to ask the
1820 * peer to negotiate an option.
1829 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1831 *lenp = nakp - nak_buffer;
1832 BCOPY(nak_buffer, inp, *lenp);
1839 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1840 return (rc); /* Return final code */
1845 * lcp_up - LCP has come UP.
1850 lcp_options *wo = &lcp_wantoptions[f->unit];
1851 lcp_options *ho = &lcp_hisoptions[f->unit];
1852 lcp_options *go = &lcp_gotoptions[f->unit];
1853 lcp_options *ao = &lcp_allowoptions[f->unit];
1856 if (!go->neg_magicnumber)
1857 go->magicnumber = 0;
1858 if (!ho->neg_magicnumber)
1859 ho->magicnumber = 0;
1862 * Set our MTU to the smaller of the MTU we wanted and
1863 * the MRU our peer wanted. If we negotiated an MRU,
1864 * set our MRU to the larger of value we wanted and
1865 * the value we got in the negotiation.
1866 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1867 * the interface MTU is set to the lowest of that, the
1868 * MTU we want to use, and our link MRU.
1870 mtu = ho->neg_mru? ho->mru: PPP_MRU;
1871 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1872 #ifdef PPP_WITH_MULTILINK
1873 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1874 #endif /* PPP_WITH_MULTILINK */
1875 netif_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1876 ppp_send_config(f->unit, mtu,
1877 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1878 ho->neg_pcompression, ho->neg_accompression);
1879 ppp_recv_config(f->unit, mru,
1880 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1881 go->neg_pcompression, go->neg_accompression);
1884 peer_mru[f->unit] = ho->mru;
1886 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1888 link_established(f->unit);
1893 * lcp_down - LCP has gone DOWN.
1895 * Alert other protocols.
1900 lcp_options *go = &lcp_gotoptions[f->unit];
1902 lcp_echo_lowerdown(f->unit);
1906 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1907 ppp_recv_config(f->unit, PPP_MRU,
1908 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1909 go->neg_pcompression, go->neg_accompression);
1910 peer_mru[f->unit] = PPP_MRU;
1915 * lcp_starting - LCP needs the lower layer up.
1918 lcp_starting(fsm *f)
1920 link_required(f->unit);
1925 * lcp_finished - LCP has finished with the lower layer.
1928 lcp_finished(fsm *f)
1930 link_terminated(f->unit);
1935 * lcp_printpkt - print the contents of an LCP packet.
1937 static char *lcp_codenames[] = {
1938 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1939 "TermReq", "TermAck", "CodeRej", "ProtRej",
1940 "EchoReq", "EchoRep", "DiscReq", "Ident",
1945 lcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
1947 int code, id, len, olen, i;
1948 u_char *pstart, *optend;
1952 if (plen < HEADERLEN)
1958 if (len < HEADERLEN || len > plen)
1961 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1962 printer(arg, " %s", lcp_codenames[code-1]);
1964 printer(arg, " code=0x%x", code);
1965 printer(arg, " id=0x%x", id);
1972 /* print option list */
1977 if (olen < 2 || olen > len) {
1985 if (olen == CILEN_SHORT) {
1987 GETSHORT(cishort, p);
1988 printer(arg, "mru %d", cishort);
1992 if (olen == CILEN_LONG) {
1995 printer(arg, "asyncmap 0x%x", cilong);
1999 if (olen >= CILEN_SHORT) {
2001 printer(arg, "auth ");
2002 GETSHORT(cishort, p);
2005 printer(arg, "pap");
2008 printer(arg, "chap");
2012 printer(arg, " MD5");
2015 case CHAP_MICROSOFT:
2016 printer(arg, " MS");
2020 case CHAP_MICROSOFT_V2:
2021 printer(arg, " MS-v2");
2028 printer(arg, "eap");
2031 printer(arg, "0x%x", cishort);
2036 if (olen >= CILEN_SHORT) {
2038 printer(arg, "quality ");
2039 GETSHORT(cishort, p);
2042 printer(arg, "lqr");
2045 printer(arg, "0x%x", cishort);
2050 if (olen >= CILEN_CHAR) {
2052 printer(arg, "callback ");
2053 GETCHAR(cishort, p);
2056 printer(arg, "CBCP");
2059 printer(arg, "0x%x", cishort);
2063 case CI_MAGICNUMBER:
2064 if (olen == CILEN_LONG) {
2067 printer(arg, "magic 0x%x", cilong);
2070 case CI_PCOMPRESSION:
2071 if (olen == CILEN_VOID) {
2073 printer(arg, "pcomp");
2076 case CI_ACCOMPRESSION:
2077 if (olen == CILEN_VOID) {
2079 printer(arg, "accomp");
2083 if (olen == CILEN_SHORT) {
2085 GETSHORT(cishort, p);
2086 printer(arg, "mrru %d", cishort);
2090 if (olen == CILEN_VOID) {
2092 printer(arg, "ssnhf");
2096 #ifdef PPP_WITH_MULTILINK
2097 if (olen >= CILEN_CHAR) {
2100 GETCHAR(epd.class, p);
2101 epd.length = olen - CILEN_CHAR;
2102 if (epd.length > MAX_ENDP_LEN)
2103 epd.length = MAX_ENDP_LEN;
2104 if (epd.length > 0) {
2105 BCOPY(p, epd.value, epd.length);
2108 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2111 printer(arg, "endpoint");
2115 while (p < optend) {
2117 printer(arg, " %.2x", code);
2125 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2127 print_string((char *)p, len, printer, arg);
2138 printer(arg, " magic=0x%x", cilong);
2147 printer(arg, " magic=0x%x", cilong);
2150 if (code == TIMEREM) {
2154 printer(arg, " seconds=%u", cilong);
2159 print_string((char *)p, len, printer, arg);
2166 /* print the rest of the bytes in the packet */
2167 for (i = 0; i < len && i < 32; ++i) {
2169 printer(arg, " %.2x", code);
2172 printer(arg, " ...");
2180 * Time to shut down the link because there is nothing out there.
2184 void LcpLinkFailure (fsm *f)
2186 if (f->state == OPENED) {
2187 info("No response to %d echo-requests", lcp_echos_pending);
2188 notice("Serial link appears to be disconnected.");
2189 status = EXIT_PEER_DEAD;
2190 lcp_close(f->unit, "Peer not responding");
2195 * Timer expired for the LCP echo requests from this process.
2199 LcpEchoCheck (fsm *f)
2201 LcpSendEchoRequest (f);
2202 if (f->state != OPENED)
2206 * Start the timer for the next interval.
2208 if (lcp_echo_timer_running)
2209 warn("assertion lcp_echo_timer_running==0 failed");
2210 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2211 lcp_echo_timer_running = 1;
2215 * LcpEchoTimeout - Timer expired on the LCP echo
2219 LcpEchoTimeout (void *arg)
2221 if (lcp_echo_timer_running != 0) {
2222 lcp_echo_timer_running = 0;
2223 LcpEchoCheck ((fsm *) arg);
2228 * LcpEchoReply - LCP has received a reply to the echo
2232 lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
2236 /* Check the magic number - don't count replies from ourselves. */
2238 dbglog("lcp: received short Echo-Reply, length %d", len);
2241 GETLONG(magic, inp);
2242 if (lcp_gotoptions[f->unit].neg_magicnumber
2243 && magic == lcp_gotoptions[f->unit].magicnumber) {
2244 warn("appear to have received our own echo-reply!");
2248 /* Reset the number of outstanding echo frames */
2249 lcp_echos_pending = 0;
2253 * LcpSendEchoRequest - Send an echo request frame to the peer
2257 LcpSendEchoRequest (fsm *f)
2259 u_int32_t lcp_magic;
2260 u_char pkt[4], *pktp;
2263 * Detect the failure of the peer at this point.
2265 if (lcp_echo_fails != 0) {
2266 if (lcp_echos_pending >= lcp_echo_fails) {
2268 lcp_echos_pending = 0;
2273 * If adaptive echos have been enabled, only send the echo request if
2274 * no traffic was received since the last one.
2276 if (lcp_echo_adaptive) {
2277 static unsigned int last_pkts_in = 0;
2278 struct pppd_stats cur_stats;
2280 if (get_ppp_stats(f->unit, &cur_stats) && cur_stats.pkts_in != last_pkts_in) {
2281 last_pkts_in = cur_stats.pkts_in;
2287 * Make and send the echo request frame.
2289 if (f->state == OPENED) {
2290 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2292 PUTLONG(lcp_magic, pktp);
2293 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2294 ++lcp_echos_pending;
2299 * lcp_echo_lowerup - Start the timer for the LCP frame
2303 lcp_echo_lowerup (int unit)
2305 fsm *f = &lcp_fsm[unit];
2307 /* Clear the parameters for generating echo frames */
2308 lcp_echos_pending = 0;
2309 lcp_echo_number = 0;
2310 lcp_echo_timer_running = 0;
2312 /* If a timeout interval is specified then start the timer */
2313 if (lcp_echo_interval != 0)
2318 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2322 lcp_echo_lowerdown (int unit)
2324 fsm *f = &lcp_fsm[unit];
2326 if (lcp_echo_timer_running != 0) {
2327 UNTIMEOUT (LcpEchoTimeout, f);
2328 lcp_echo_timer_running = 0;