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.
59 * When the link comes up we want to be able to wait for a short while,
60 * or until seeing some input from the peer, before starting to send
61 * configure-requests. We do this by delaying the fsm_lowerup call.
63 /* steal a bit in fsm flags word */
64 #define DELAYED_UP 0x100
66 static void lcp_delayed_up(void *);
69 * LCP-related command-line options.
71 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
72 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
73 bool lcp_echo_adaptive = 0; /* request echo only if the link was idle */
74 bool lax_recv = 0; /* accept control chars in asyncmap */
75 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
77 static int noopt(char **);
79 #ifdef PPP_WITH_MULTILINK
80 static int setendpoint(char **);
81 static void printendpoint(option_t *, void (*)(void *, char *, ...), void *);
82 #endif /* PPP_WITH_MULTILINK */
84 static option_t lcp_option_list[] = {
86 { "-all", o_special_noarg, (void *)noopt,
87 "Don't request/allow any LCP options" },
89 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
90 "Disable address/control compression",
91 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
92 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
93 "Disable address/control compression",
94 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
96 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
97 "Set asyncmap (for received packets)",
98 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
99 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
100 "Set asyncmap (for received packets)",
101 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
102 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
103 "Disable asyncmap negotiation",
104 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
105 &lcp_allowoptions[0].neg_asyncmap },
106 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
107 "Disable asyncmap negotiation",
108 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
109 &lcp_allowoptions[0].neg_asyncmap },
111 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
112 "Disable magic number negotiation (looped-back line detection)",
113 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
114 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
115 "Disable magic number negotiation (looped-back line detection)",
116 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
118 { "mru", o_int, &lcp_wantoptions[0].mru,
119 "Set MRU (maximum received packet size) for negotiation",
120 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
121 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
122 "Disable MRU negotiation (use default 1500)",
123 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
124 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
125 "Disable MRU negotiation (use default 1500)",
126 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
128 { "mtu", o_int, &lcp_allowoptions[0].mru,
129 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
131 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
132 "Disable protocol field compression",
133 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
134 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
135 "Disable protocol field compression",
136 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
138 { "passive", o_bool, &lcp_wantoptions[0].passive,
139 "Set passive mode", 1 },
140 { "-p", o_bool, &lcp_wantoptions[0].passive,
141 "Set passive mode", OPT_ALIAS | 1 },
143 { "silent", o_bool, &lcp_wantoptions[0].silent,
144 "Set silent mode", 1 },
146 { "lcp-echo-failure", o_int, &lcp_echo_fails,
147 "Set number of consecutive echo failures to indicate link failure",
149 { "lcp-echo-interval", o_int, &lcp_echo_interval,
150 "Set time in seconds between LCP echo requests", OPT_PRIO },
151 { "lcp-echo-adaptive", o_bool, &lcp_echo_adaptive,
152 "Suppress LCP echo requests if traffic was received", 1 },
153 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
154 "Set time in seconds between LCP retransmissions", OPT_PRIO },
155 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
156 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
157 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
158 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
159 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
160 "Set limit on number of LCP configure-naks", OPT_PRIO },
162 { "receive-all", o_bool, &lax_recv,
163 "Accept all received control characters", 1 },
165 #ifdef PPP_WITH_MULTILINK
166 { "mrru", o_int, &lcp_wantoptions[0].mrru,
167 "Maximum received packet size for multilink bundle",
168 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
170 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
171 "Use short sequence numbers in multilink headers",
172 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
173 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
174 "Don't use short sequence numbers in multilink headers",
175 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
177 { "endpoint", o_special, (void *) setendpoint,
178 "Endpoint discriminator for multilink",
179 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
180 #endif /* PPP_WITH_MULTILINK */
182 { "noendpoint", o_bool, &noendpoint,
183 "Don't send or accept multilink endpoint discriminator", 1 },
189 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
190 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
191 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
192 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
193 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
195 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
196 static int lcp_echo_number = 0; /* ID number of next echo frame */
197 static int lcp_echo_timer_running = 0; /* set if a timer is running */
199 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
202 * Callbacks for fsm code. (CI = Configuration Information)
204 static void lcp_resetci(fsm *); /* Reset our CI */
205 static int lcp_cilen(fsm *); /* Return length of our CI */
206 static void lcp_addci(fsm *, u_char *, int *); /* Add our CI to pkt */
207 static int lcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
208 static int lcp_nakci(fsm *, u_char *, int, int); /* Peer nak'd our CI */
209 static int lcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
210 static int lcp_reqci(fsm *, u_char *, int *, int); /* Rcv peer CI */
211 static void lcp_up(fsm *); /* We're UP */
212 static void lcp_down(fsm *); /* We're DOWN */
213 static void lcp_starting(fsm *); /* We need lower layer up */
214 static void lcp_finished(fsm *); /* We need lower layer down */
215 static int lcp_extcode(fsm *, int, int, u_char *, int);
216 static void lcp_rprotrej(fsm *, u_char *, int);
219 * routines to send LCP echos to peer
222 static void lcp_echo_lowerup(int);
223 static void lcp_echo_lowerdown(int);
224 static void LcpEchoTimeout(void *);
225 static void lcp_received_echo_reply(fsm *, int, u_char *, int);
226 static void LcpSendEchoRequest(fsm *);
227 static void LcpLinkFailure(fsm *);
228 static void LcpEchoCheck(fsm *);
230 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
231 lcp_resetci, /* Reset our Configuration Information */
232 lcp_cilen, /* Length of our Configuration Information */
233 lcp_addci, /* Add our Configuration Information */
234 lcp_ackci, /* ACK our Configuration Information */
235 lcp_nakci, /* NAK our Configuration Information */
236 lcp_rejci, /* Reject our Configuration Information */
237 lcp_reqci, /* Request peer's Configuration Information */
238 lcp_up, /* Called when fsm reaches OPENED state */
239 lcp_down, /* Called when fsm leaves OPENED state */
240 lcp_starting, /* Called when we want the lower layer up */
241 lcp_finished, /* Called when we want the lower layer down */
242 NULL, /* Called when Protocol-Reject received */
243 NULL, /* Retransmission is necessary */
244 lcp_extcode, /* Called to handle LCP-specific codes */
245 "LCP" /* String name of protocol */
249 * Protocol entry points.
250 * Some of these are called directly.
253 static void lcp_init(int);
254 static void lcp_input(int, u_char *, int);
255 static void lcp_protrej(int);
256 static int lcp_printpkt(u_char *, int, void (*)(void *, char *, ...), void *);
258 struct protent lcp_protent = {
278 int lcp_loopbackfail = DEFLOOPBACKFAIL;
281 * Length of each type of configuration option (in octets)
285 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
286 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
287 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
288 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
291 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
292 (x) == CONFNAK ? "NAK" : "REJ")
295 * noopt - Disable all options (why?).
300 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
301 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
306 #ifdef PPP_WITH_MULTILINK
308 setendpoint(char **argv)
310 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
311 lcp_wantoptions[0].neg_endpoint = 1;
314 option_error("Can't parse '%s' as an endpoint discriminator", *argv);
319 printendpoint(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
321 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
323 #endif /* PPP_WITH_MULTILINK */
326 * lcp_init - Initialize LCP.
331 fsm *f = &lcp_fsm[unit];
332 lcp_options *wo = &lcp_wantoptions[unit];
333 lcp_options *ao = &lcp_allowoptions[unit];
336 f->protocol = PPP_LCP;
337 f->callbacks = &lcp_callbacks;
341 BZERO(wo, sizeof(*wo));
344 wo->neg_asyncmap = 1;
345 wo->neg_magicnumber = 1;
346 wo->neg_pcompression = 1;
347 wo->neg_accompression = 1;
349 BZERO(ao, sizeof(*ao));
352 ao->neg_asyncmap = 1;
354 ao->chap_mdtype = chap_mdtype_all;
357 ao->neg_magicnumber = 1;
358 ao->neg_pcompression = 1;
359 ao->neg_accompression = 1;
360 ao->neg_endpoint = 1;
365 * lcp_open - LCP is allowed to come up.
370 fsm *f = &lcp_fsm[unit];
371 lcp_options *wo = &lcp_wantoptions[unit];
373 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
375 f->flags |= OPT_PASSIVE;
377 f->flags |= OPT_SILENT;
383 * lcp_close - Take LCP down.
386 lcp_close(int unit, char *reason)
388 fsm *f = &lcp_fsm[unit];
391 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
392 new_phase(PHASE_TERMINATE);
394 if (f->flags & DELAYED_UP) {
395 untimeout(lcp_delayed_up, f);
400 fsm_close(f, reason);
401 if (oldstate == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT|DELAYED_UP)) {
403 * This action is not strictly according to the FSM in RFC1548,
404 * but it does mean that the program terminates if you do a
405 * lcp_close() when a connection hasn't been established
406 * because we are in passive/silent mode or because we have
407 * delayed the fsm_lowerup() call and it hasn't happened yet.
409 f->flags &= ~DELAYED_UP;
416 * lcp_lowerup - The lower layer is up.
419 lcp_lowerup(int unit)
421 lcp_options *wo = &lcp_wantoptions[unit];
422 fsm *f = &lcp_fsm[unit];
425 * Don't use A/C or protocol compression on transmission,
426 * but accept A/C and protocol compressed packets
427 * if we are going to ask for A/C and protocol compression.
429 if (ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0) < 0
430 || ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
431 wo->neg_pcompression, wo->neg_accompression) < 0)
433 peer_mru[unit] = PPP_MRU;
435 if (listen_time != 0) {
436 f->flags |= DELAYED_UP;
437 timeout(lcp_delayed_up, f, 0, listen_time * 1000);
444 * lcp_lowerdown - The lower layer is down.
447 lcp_lowerdown(int unit)
449 fsm *f = &lcp_fsm[unit];
451 if (f->flags & DELAYED_UP) {
452 f->flags &= ~DELAYED_UP;
453 untimeout(lcp_delayed_up, f);
455 fsm_lowerdown(&lcp_fsm[unit]);
460 * lcp_delayed_up - Bring the lower layer up now.
463 lcp_delayed_up(void *arg)
467 if (f->flags & DELAYED_UP) {
468 f->flags &= ~DELAYED_UP;
475 * lcp_input - Input LCP packet.
478 lcp_input(int unit, u_char *p, int len)
480 fsm *f = &lcp_fsm[unit];
482 if (f->flags & DELAYED_UP) {
483 f->flags &= ~DELAYED_UP;
484 untimeout(lcp_delayed_up, f);
487 fsm_input(f, p, len);
491 * lcp_extcode - Handle a LCP-specific code.
494 lcp_extcode(fsm *f, int code, int id, u_char *inp, int len)
500 lcp_rprotrej(f, inp, len);
504 if (f->state != OPENED)
507 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
508 fsm_sdata(f, ECHOREP, id, inp, len);
512 lcp_received_echo_reply(f, id, inp, len);
528 * lcp_rprotrej - Receive an Protocol-Reject.
530 * Figure out which protocol is rejected and inform it.
533 lcp_rprotrej(fsm *f, u_char *inp, int len)
536 struct protent *protp;
541 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
548 * Protocol-Reject packets received in any state other than the LCP
549 * OPENED state SHOULD be silently discarded.
551 if( f->state != OPENED ){
552 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
556 pname = protocol_name(prot);
559 * Upcall the proper Protocol-Reject routine.
561 for (i = 0; (protp = protocols[i]) != NULL; ++i)
562 if (protp->protocol == prot && protp->enabled_flag) {
564 dbglog("Protocol-Reject for 0x%x received", prot);
566 dbglog("Protocol-Reject for '%s' (0x%x) received", pname,
568 (*protp->protrej)(f->unit);
573 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
575 warn("Protocol-Reject for unsupported protocol '%s' (0x%x)", pname,
581 * lcp_protrej - A Protocol-Reject was received.
585 lcp_protrej(int unit)
590 error("Received Protocol-Reject for LCP!");
591 fsm_protreject(&lcp_fsm[unit]);
596 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
599 lcp_sprotrej(int unit, u_char *p, int len)
602 * Send back the protocol and the information field of the
603 * rejected packet. We only get here if LCP is in the OPENED state.
608 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
614 * lcp_resetci - Reset our CI.
619 lcp_options *wo = &lcp_wantoptions[f->unit];
620 lcp_options *go = &lcp_gotoptions[f->unit];
621 lcp_options *ao = &lcp_allowoptions[f->unit];
623 wo->magicnumber = magic();
629 go->neg_endpoint = 0;
632 ao->neg_endpoint = 0;
633 peer_mru[f->unit] = PPP_MRU;
639 * lcp_cilen - Return length of our CI.
644 lcp_options *go = &lcp_gotoptions[f->unit];
646 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
647 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
648 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
649 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
650 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
651 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
653 * NB: we only ask for one of CHAP, UPAP, or EAP, even if we will
654 * accept more than one. We prefer EAP first, then CHAP, then
657 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
658 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
659 LENCISHORT(go->neg_eap) +
660 LENCICHAP(!go->neg_eap && go->neg_chap) +
661 LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
662 LENCILQR(go->neg_lqr) +
663 LENCICBCP(go->neg_cbcp) +
664 LENCILONG(go->neg_magicnumber) +
665 LENCIVOID(go->neg_pcompression) +
666 LENCIVOID(go->neg_accompression) +
667 LENCISHORT(go->neg_mrru) +
668 LENCIVOID(go->neg_ssnhf) +
669 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
674 * lcp_addci - Add our desired CIs to a packet.
677 lcp_addci(fsm *f, u_char *ucp, int *lenp)
679 lcp_options *go = &lcp_gotoptions[f->unit];
680 u_char *start_ucp = ucp;
682 #define ADDCIVOID(opt, neg) \
685 PUTCHAR(CILEN_VOID, ucp); \
687 #define ADDCISHORT(opt, neg, val) \
690 PUTCHAR(CILEN_SHORT, ucp); \
691 PUTSHORT(val, ucp); \
693 #define ADDCICHAP(opt, neg, val) \
695 PUTCHAR((opt), ucp); \
696 PUTCHAR(CILEN_CHAP, ucp); \
697 PUTSHORT(PPP_CHAP, ucp); \
698 PUTCHAR((CHAP_DIGEST(val)), ucp); \
700 #define ADDCILONG(opt, neg, val) \
703 PUTCHAR(CILEN_LONG, ucp); \
706 #define ADDCILQR(opt, neg, val) \
709 PUTCHAR(CILEN_LQR, ucp); \
710 PUTSHORT(PPP_LQR, ucp); \
713 #define ADDCICHAR(opt, neg, val) \
716 PUTCHAR(CILEN_CHAR, ucp); \
719 #define ADDCIENDP(opt, neg, class, val, len) \
723 PUTCHAR(CILEN_CHAR + len, ucp); \
724 PUTCHAR(class, ucp); \
725 for (i = 0; i < len; ++i) \
726 PUTCHAR(val[i], ucp); \
729 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
730 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
732 ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
733 ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
734 ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
736 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
737 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
738 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
739 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
740 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
741 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
742 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
743 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
744 go->endpoint.value, go->endpoint.length);
746 if (ucp - start_ucp != *lenp) {
747 /* this should never happen, because peer_mtu should be 1500 */
748 error("Bug in lcp_addci: wrong length");
754 * lcp_ackci - Ack our CIs.
755 * This should not modify any state if the Ack is bad.
762 lcp_ackci(fsm *f, u_char *p, int len)
764 lcp_options *go = &lcp_gotoptions[f->unit];
765 u_char cilen, citype, cichar;
770 * CIs must be in exactly the same order that we sent.
771 * Check packet length and CI length at each step.
772 * If we find any deviations, then this packet is bad.
774 #define ACKCIVOID(opt, neg) \
776 if ((len -= CILEN_VOID) < 0) \
778 GETCHAR(citype, p); \
780 if (cilen != CILEN_VOID || \
784 #define ACKCISHORT(opt, neg, val) \
786 if ((len -= CILEN_SHORT) < 0) \
788 GETCHAR(citype, p); \
790 if (cilen != CILEN_SHORT || \
793 GETSHORT(cishort, p); \
794 if (cishort != val) \
797 #define ACKCICHAR(opt, neg, val) \
799 if ((len -= CILEN_CHAR) < 0) \
801 GETCHAR(citype, p); \
803 if (cilen != CILEN_CHAR || \
806 GETCHAR(cichar, p); \
810 #define ACKCICHAP(opt, neg, val) \
812 if ((len -= CILEN_CHAP) < 0) \
814 GETCHAR(citype, p); \
816 if (cilen != CILEN_CHAP || \
819 GETSHORT(cishort, p); \
820 if (cishort != PPP_CHAP) \
822 GETCHAR(cichar, p); \
823 if (cichar != (CHAP_DIGEST(val))) \
826 #define ACKCILONG(opt, neg, val) \
828 if ((len -= CILEN_LONG) < 0) \
830 GETCHAR(citype, p); \
832 if (cilen != CILEN_LONG || \
835 GETLONG(cilong, p); \
839 #define ACKCILQR(opt, neg, val) \
841 if ((len -= CILEN_LQR) < 0) \
843 GETCHAR(citype, p); \
845 if (cilen != CILEN_LQR || \
848 GETSHORT(cishort, p); \
849 if (cishort != PPP_LQR) \
851 GETLONG(cilong, p); \
855 #define ACKCIENDP(opt, neg, class, val, vlen) \
858 if ((len -= CILEN_CHAR + vlen) < 0) \
860 GETCHAR(citype, p); \
862 if (cilen != CILEN_CHAR + vlen || \
865 GETCHAR(cichar, p); \
866 if (cichar != class) \
868 for (i = 0; i < vlen; ++i) { \
869 GETCHAR(cichar, p); \
870 if (cichar != val[i]) \
875 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
876 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
878 ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
879 ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
880 ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
882 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
883 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
884 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
885 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
886 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
887 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
888 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
889 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
890 go->endpoint.value, go->endpoint.length);
893 * If there are any remaining CIs, then this packet is bad.
899 LCPDEBUG(("lcp_acki: received bad Ack!"));
905 * lcp_nakci - Peer has sent a NAK for some of our CIs.
906 * This should not modify any state if the Nak is bad
907 * or if LCP is in the OPENED state.
914 lcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
916 lcp_options *go = &lcp_gotoptions[f->unit];
917 lcp_options *wo = &lcp_wantoptions[f->unit];
918 u_char citype, cichar, *next;
921 lcp_options no; /* options we've seen Naks for */
922 lcp_options try; /* options to request next time */
926 BZERO(&no, sizeof(no));
930 * Any Nak'd CIs must be in exactly the same order that we sent.
931 * Check packet length and CI length at each step.
932 * If we find any deviations, then this packet is bad.
934 #define NAKCIVOID(opt, neg) \
936 len >= CILEN_VOID && \
937 p[1] == CILEN_VOID && \
940 INCPTR(CILEN_VOID, p); \
944 #define NAKCICHAP(opt, neg, code) \
946 len >= CILEN_CHAP && \
947 p[1] == CILEN_CHAP && \
951 GETSHORT(cishort, p); \
952 GETCHAR(cichar, p); \
956 #define NAKCICHAR(opt, neg, code) \
958 len >= CILEN_CHAR && \
959 p[1] == CILEN_CHAR && \
963 GETCHAR(cichar, p); \
967 #define NAKCISHORT(opt, neg, code) \
969 len >= CILEN_SHORT && \
970 p[1] == CILEN_SHORT && \
972 len -= CILEN_SHORT; \
974 GETSHORT(cishort, p); \
978 #define NAKCILONG(opt, neg, code) \
980 len >= CILEN_LONG && \
981 p[1] == CILEN_LONG && \
985 GETLONG(cilong, p); \
989 #define NAKCILQR(opt, neg, code) \
991 len >= CILEN_LQR && \
992 p[1] == CILEN_LQR && \
996 GETSHORT(cishort, p); \
997 GETLONG(cilong, p); \
1001 #define NAKCIENDP(opt, neg) \
1003 len >= CILEN_CHAR && \
1005 p[1] >= CILEN_CHAR && \
1014 * NOTE! There must be no assignments to individual fields of *go in
1015 * the code below. Any such assignment is a BUG!
1018 * We don't care if they want to send us smaller packets than
1019 * we want. Therefore, accept any MRU less than what we asked for,
1020 * but then ignore the new value when setting the MRU in the kernel.
1021 * If they send us a bigger MRU than what we asked, accept it, up to
1022 * the limit of the default MRU we'd get if we didn't negotiate.
1024 if (go->neg_mru && go->mru != DEFMRU) {
1025 NAKCISHORT(CI_MRU, neg_mru,
1026 if (cishort <= wo->mru || cishort <= DEFMRU)
1032 * Add any characters they want to our (receive-side) asyncmap.
1034 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1035 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1036 try.asyncmap = go->asyncmap | cilong;
1041 * If they've nak'd our authentication-protocol, check whether
1042 * they are proposing a different protocol, or a different
1043 * hash algorithm for CHAP.
1045 if ((go->neg_chap || go->neg_upap || go->neg_eap)
1046 && len >= CILEN_SHORT
1047 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1050 no.neg_chap = go->neg_chap;
1051 no.neg_upap = go->neg_upap;
1052 no.neg_eap = go->neg_eap;
1054 GETSHORT(cishort, p);
1055 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1056 /* If we were asking for EAP, then we need to stop that. */
1060 /* If we were asking for CHAP, then we need to stop that. */
1061 else if (go->neg_chap)
1064 * If we weren't asking for CHAP or EAP, then we were asking for
1065 * PAP, in which case this Nak is bad.
1070 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1072 /* Stop asking for EAP, if we were. */
1075 /* Try to set up to use their suggestion, if possible */
1076 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1077 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1078 } else if (go->neg_chap) {
1080 * We were asking for our preferred algorithm, they must
1081 * want something different.
1083 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1084 if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1085 /* Use their suggestion if we support it ... */
1086 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1088 /* ... otherwise, try our next-preferred algorithm. */
1089 try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1090 if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1095 * Whoops, they Nak'd our algorithm of choice
1096 * but then suggested it back to us.
1102 * Stop asking for PAP if we were asking for it.
1110 * If we were asking for EAP, and they're Conf-Naking EAP,
1111 * well, that's just strange. Nobody should do that.
1113 if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1114 dbglog("Unexpected Conf-Nak for EAP");
1117 * We don't recognize what they're suggesting.
1118 * Stop asking for what we were asking for.
1122 else if (go->neg_chap)
1126 p += cilen - CILEN_SHORT;
1131 * If they can't cope with our link quality protocol, we'll have
1132 * to stop asking for LQR. We haven't got any other protocol.
1133 * If they Nak the reporting period, take their value XXX ?
1135 NAKCILQR(CI_QUALITY, neg_lqr,
1136 if (cishort != PPP_LQR)
1139 try.lqr_period = cilong;
1143 * Only implementing CBCP...not the rest of the callback options
1145 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1150 * Check for a looped-back line.
1152 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1153 try.magicnumber = magic();
1158 * Peer shouldn't send Nak for protocol compression or
1159 * address/control compression requests; they should send
1160 * a Reject instead. If they send a Nak, treat it as a Reject.
1162 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1163 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1166 * Nak for MRRU option - accept their value if it is smaller
1167 * than the one we want.
1170 NAKCISHORT(CI_MRRU, neg_mrru,
1171 if (treat_as_reject)
1173 else if (cishort <= wo->mrru)
1179 * Nak for short sequence numbers shouldn't be sent, treat it
1182 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1185 * Nak of the endpoint discriminator option is not permitted,
1186 * treat it like a reject.
1188 NAKCIENDP(CI_EPDISC, neg_endpoint);
1191 * There may be remaining CIs, if the peer is requesting negotiation
1192 * on an option that we didn't include in our request packet.
1193 * If we see an option that we requested, or one we've already seen
1194 * in this packet, then this packet is bad.
1195 * If we wanted to respond by starting to negotiate on the requested
1196 * option(s), we could, but we don't, because except for the
1197 * authentication type and quality protocol, if we are not negotiating
1198 * an option, it is because we were told not to.
1199 * For the authentication type, the Nak from the peer means
1200 * `let me authenticate myself with you' which is a bit pointless.
1201 * For the quality protocol, the Nak means `ask me to send you quality
1202 * reports', but if we didn't ask for them, we don't want them.
1203 * An option we don't recognize represents the peer asking to
1204 * negotiate some option we don't support, so ignore it.
1206 while (len >= CILEN_VOID) {
1209 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1211 next = p + cilen - 2;
1215 if ((go->neg_mru && go->mru != DEFMRU)
1216 || no.neg_mru || cilen != CILEN_SHORT)
1218 GETSHORT(cishort, p);
1219 if (cishort < DEFMRU) {
1225 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1226 || no.neg_asyncmap || cilen != CILEN_LONG)
1230 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1231 go->neg_eap || no.neg_eap)
1234 case CI_MAGICNUMBER:
1235 if (go->neg_magicnumber || no.neg_magicnumber ||
1236 cilen != CILEN_LONG)
1239 case CI_PCOMPRESSION:
1240 if (go->neg_pcompression || no.neg_pcompression
1241 || cilen != CILEN_VOID)
1244 case CI_ACCOMPRESSION:
1245 if (go->neg_accompression || no.neg_accompression
1246 || cilen != CILEN_VOID)
1250 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1254 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1258 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1263 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1271 * OK, the Nak is good. Now we can update state.
1272 * If there are any options left we ignore them.
1274 if (f->state != OPENED) {
1276 if (++try.numloops >= lcp_loopbackfail) {
1277 notice("Serial line is looped back.");
1278 status = EXIT_LOOPBACK;
1279 lcp_close(f->unit, "Loopback detected");
1289 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1295 * lcp_rejci - Peer has Rejected some of our CIs.
1296 * This should not modify any state if the Reject is bad
1297 * or if LCP is in the OPENED state.
1300 * 0 - Reject was bad.
1301 * 1 - Reject was good.
1304 lcp_rejci(fsm *f, u_char *p, int len)
1306 lcp_options *go = &lcp_gotoptions[f->unit];
1310 lcp_options try; /* options to request next time */
1315 * Any Rejected CIs must be in exactly the same order that we sent.
1316 * Check packet length and CI length at each step.
1317 * If we find any deviations, then this packet is bad.
1319 #define REJCIVOID(opt, neg) \
1321 len >= CILEN_VOID && \
1322 p[1] == CILEN_VOID && \
1324 len -= CILEN_VOID; \
1325 INCPTR(CILEN_VOID, p); \
1328 #define REJCISHORT(opt, neg, val) \
1330 len >= CILEN_SHORT && \
1331 p[1] == CILEN_SHORT && \
1333 len -= CILEN_SHORT; \
1335 GETSHORT(cishort, p); \
1336 /* Check rejected value. */ \
1337 if (cishort != val) \
1341 #define REJCICHAP(opt, neg, val) \
1343 len >= CILEN_CHAP && \
1344 p[1] == CILEN_CHAP && \
1346 len -= CILEN_CHAP; \
1348 GETSHORT(cishort, p); \
1349 GETCHAR(cichar, p); \
1350 /* Check rejected value. */ \
1351 if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1354 try.neg_eap = try.neg_upap = 0; \
1356 #define REJCILONG(opt, neg, val) \
1358 len >= CILEN_LONG && \
1359 p[1] == CILEN_LONG && \
1361 len -= CILEN_LONG; \
1363 GETLONG(cilong, p); \
1364 /* Check rejected value. */ \
1365 if (cilong != val) \
1369 #define REJCILQR(opt, neg, val) \
1371 len >= CILEN_LQR && \
1372 p[1] == CILEN_LQR && \
1376 GETSHORT(cishort, p); \
1377 GETLONG(cilong, p); \
1378 /* Check rejected value. */ \
1379 if (cishort != PPP_LQR || cilong != val) \
1383 #define REJCICBCP(opt, neg, val) \
1385 len >= CILEN_CBCP && \
1386 p[1] == CILEN_CBCP && \
1388 len -= CILEN_CBCP; \
1390 GETCHAR(cichar, p); \
1391 /* Check rejected value. */ \
1392 if (cichar != val) \
1396 #define REJCIENDP(opt, neg, class, val, vlen) \
1398 len >= CILEN_CHAR + vlen && \
1400 p[1] == CILEN_CHAR + vlen) { \
1402 len -= CILEN_CHAR + vlen; \
1404 GETCHAR(cichar, p); \
1405 if (cichar != class) \
1407 for (i = 0; i < vlen; ++i) { \
1408 GETCHAR(cichar, p); \
1409 if (cichar != val[i]) \
1415 REJCISHORT(CI_MRU, neg_mru, go->mru);
1416 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1417 REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1419 REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1420 if (!go->neg_chap) {
1421 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1424 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1425 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1426 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1427 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1428 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1429 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1430 REJCIVOID(CI_SSNHF, neg_ssnhf);
1431 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1432 go->endpoint.value, go->endpoint.length);
1435 * If there are any remaining CIs, then this packet is bad.
1440 * Now we can update state.
1442 if (f->state != OPENED)
1447 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1453 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1455 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1456 * appropriately. If reject_if_disagree is non-zero, doesn't return
1457 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1460 lcp_reqci(fsm *f, u_char *inp, int *lenp, int reject_if_disagree)
1462 lcp_options *go = &lcp_gotoptions[f->unit];
1463 lcp_options *ho = &lcp_hisoptions[f->unit];
1464 lcp_options *ao = &lcp_allowoptions[f->unit];
1465 u_char *cip, *next; /* Pointer to current and next CIs */
1466 int cilen, citype, cichar; /* Parsed len, type, char value */
1467 u_short cishort; /* Parsed short value */
1468 u_int32_t cilong; /* Parse long value */
1469 int rc = CONFACK; /* Final packet return code */
1470 int orc; /* Individual option return code */
1471 u_char *p; /* Pointer to next char to parse */
1472 u_char *rejp; /* Pointer to next char in reject frame */
1473 u_char *nakp; /* Pointer to next char in Nak frame */
1474 int l = *lenp; /* Length left */
1477 * Reset all his options.
1479 BZERO(ho, sizeof(*ho));
1482 * Process all his options.
1488 orc = CONFACK; /* Assume success */
1489 cip = p = next; /* Remember begining of CI */
1490 if (l < 2 || /* Not enough data for CI header or */
1491 p[1] < 2 || /* CI length too small or */
1492 p[1] > l) { /* CI length too big? */
1493 LCPDEBUG(("lcp_reqci: bad CI length!"));
1494 orc = CONFREJ; /* Reject bad CI */
1495 cilen = l; /* Reject till end of packet */
1496 l = 0; /* Don't loop again */
1500 GETCHAR(citype, p); /* Parse CI type */
1501 GETCHAR(cilen, p); /* Parse CI length */
1502 l -= cilen; /* Adjust remaining length */
1503 next += cilen; /* Step to next CI */
1505 switch (citype) { /* Check CI type */
1507 if (!ao->neg_mru || /* Allow option? */
1508 cilen != CILEN_SHORT) { /* Check CI length */
1509 orc = CONFREJ; /* Reject CI */
1512 GETSHORT(cishort, p); /* Parse MRU */
1515 * He must be able to receive at least our minimum.
1516 * No need to check a maximum. If he sends a large number,
1517 * we'll just ignore it.
1519 if (cishort < MINMRU) {
1520 orc = CONFNAK; /* Nak CI */
1521 PUTCHAR(CI_MRU, nakp);
1522 PUTCHAR(CILEN_SHORT, nakp);
1523 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1526 ho->neg_mru = 1; /* Remember he sent MRU */
1527 ho->mru = cishort; /* And remember value */
1531 if (!ao->neg_asyncmap ||
1532 cilen != CILEN_LONG) {
1539 * Asyncmap must have set at least the bits
1540 * which are set in lcp_allowoptions[unit].asyncmap.
1542 if ((ao->asyncmap & ~cilong) != 0) {
1544 PUTCHAR(CI_ASYNCMAP, nakp);
1545 PUTCHAR(CILEN_LONG, nakp);
1546 PUTLONG(ao->asyncmap | cilong, nakp);
1549 ho->neg_asyncmap = 1;
1550 ho->asyncmap = cilong;
1554 if (cilen < CILEN_SHORT ||
1555 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1557 * Reject the option if we're not willing to authenticate.
1559 dbglog("No auth is possible");
1563 GETSHORT(cishort, p);
1566 * Authtype must be PAP, CHAP, or EAP.
1568 * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1569 * ao->neg_eap are set, and the peer sends a Configure-Request
1570 * with two or more authenticate-protocol requests, then we will
1571 * reject the second request.
1572 * Whether we end up doing CHAP, UPAP, or EAP depends then on
1573 * the ordering of the CIs in the peer's Configure-Request.
1576 if (cishort == PPP_PAP) {
1577 /* we've already accepted CHAP or EAP */
1578 if (ho->neg_chap || ho->neg_eap ||
1579 cilen != CILEN_SHORT) {
1580 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1584 if (!ao->neg_upap) { /* we don't want to do PAP */
1585 orc = CONFNAK; /* NAK it and suggest CHAP or EAP */
1586 PUTCHAR(CI_AUTHTYPE, nakp);
1588 PUTCHAR(CILEN_SHORT, nakp);
1589 PUTSHORT(PPP_EAP, nakp);
1591 PUTCHAR(CILEN_CHAP, nakp);
1592 PUTSHORT(PPP_CHAP, nakp);
1593 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1600 if (cishort == PPP_CHAP) {
1601 /* we've already accepted PAP or EAP */
1602 if (ho->neg_upap || ho->neg_eap ||
1603 cilen != CILEN_CHAP) {
1604 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1608 if (!ao->neg_chap) { /* we don't want to do CHAP */
1609 orc = CONFNAK; /* NAK it and suggest EAP or PAP */
1610 PUTCHAR(CI_AUTHTYPE, nakp);
1611 PUTCHAR(CILEN_SHORT, nakp);
1613 PUTSHORT(PPP_EAP, nakp);
1615 PUTSHORT(PPP_PAP, nakp);
1619 GETCHAR(cichar, p); /* get digest type */
1620 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1622 * We can't/won't do the requested type,
1623 * suggest something else.
1626 PUTCHAR(CI_AUTHTYPE, nakp);
1627 PUTCHAR(CILEN_CHAP, nakp);
1628 PUTSHORT(PPP_CHAP, nakp);
1629 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1632 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1636 if (cishort == PPP_EAP) {
1637 /* we've already accepted CHAP or PAP */
1638 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1639 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1643 if (!ao->neg_eap) { /* we don't want to do EAP */
1644 orc = CONFNAK; /* NAK it and suggest CHAP or PAP */
1645 PUTCHAR(CI_AUTHTYPE, nakp);
1647 PUTCHAR(CILEN_CHAP, nakp);
1648 PUTSHORT(PPP_CHAP, nakp);
1649 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1651 PUTCHAR(CILEN_SHORT, nakp);
1652 PUTSHORT(PPP_PAP, nakp);
1661 * We don't recognize the protocol they're asking for.
1662 * Nak it with something we're willing to do.
1663 * (At this point we know ao->neg_upap || ao->neg_chap ||
1667 PUTCHAR(CI_AUTHTYPE, nakp);
1669 PUTCHAR(CILEN_SHORT, nakp);
1670 PUTSHORT(PPP_EAP, nakp);
1671 } else if (ao->neg_chap) {
1672 PUTCHAR(CILEN_CHAP, nakp);
1673 PUTSHORT(PPP_CHAP, nakp);
1674 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1676 PUTCHAR(CILEN_SHORT, nakp);
1677 PUTSHORT(PPP_PAP, nakp);
1683 cilen != CILEN_LQR) {
1688 GETSHORT(cishort, p);
1692 * Check the protocol and the reporting period.
1693 * XXX When should we Nak this, and what with?
1695 if (cishort != PPP_LQR) {
1697 PUTCHAR(CI_QUALITY, nakp);
1698 PUTCHAR(CILEN_LQR, nakp);
1699 PUTSHORT(PPP_LQR, nakp);
1700 PUTLONG(ao->lqr_period, nakp);
1705 case CI_MAGICNUMBER:
1706 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1707 cilen != CILEN_LONG) {
1714 * He must have a different magic number.
1716 if (go->neg_magicnumber &&
1717 cilong == go->magicnumber) {
1718 cilong = magic(); /* Don't put magic() inside macro! */
1720 PUTCHAR(CI_MAGICNUMBER, nakp);
1721 PUTCHAR(CILEN_LONG, nakp);
1722 PUTLONG(cilong, nakp);
1725 ho->neg_magicnumber = 1;
1726 ho->magicnumber = cilong;
1730 case CI_PCOMPRESSION:
1731 if (!ao->neg_pcompression ||
1732 cilen != CILEN_VOID) {
1736 ho->neg_pcompression = 1;
1739 case CI_ACCOMPRESSION:
1740 if (!ao->neg_accompression ||
1741 cilen != CILEN_VOID) {
1745 ho->neg_accompression = 1;
1749 if (!ao->neg_mrru || !multilink ||
1750 cilen != CILEN_SHORT) {
1755 GETSHORT(cishort, p);
1756 /* possibly should insist on a minimum/maximum MRRU here */
1762 if (!ao->neg_ssnhf || !multilink ||
1763 cilen != CILEN_VOID) {
1771 if (!ao->neg_endpoint ||
1772 cilen < CILEN_CHAR ||
1773 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1778 cilen -= CILEN_CHAR;
1779 ho->neg_endpoint = 1;
1780 ho->endpoint.class = cichar;
1781 ho->endpoint.length = cilen;
1782 BCOPY(p, ho->endpoint.value, cilen);
1787 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1793 if (orc == CONFACK && /* Good CI */
1794 rc != CONFACK) /* but prior CI wasnt? */
1795 continue; /* Don't send this one */
1797 if (orc == CONFNAK) { /* Nak this CI? */
1798 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1799 && citype != CI_MAGICNUMBER) {
1800 orc = CONFREJ; /* Get tough if so */
1802 if (rc == CONFREJ) /* Rejecting prior CI? */
1803 continue; /* Don't send this one */
1807 if (orc == CONFREJ) { /* Reject this CI */
1809 if (cip != rejp) /* Need to move rejected CI? */
1810 BCOPY(cip, rejp, cilen); /* Move it */
1811 INCPTR(cilen, rejp); /* Update output pointer */
1816 * If we wanted to send additional NAKs (for unsent CIs), the
1817 * code would go here. The extra NAKs would go at *nakp.
1818 * At present there are no cases where we want to ask the
1819 * peer to negotiate an option.
1828 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1830 *lenp = nakp - nak_buffer;
1831 BCOPY(nak_buffer, inp, *lenp);
1838 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1839 return (rc); /* Return final code */
1844 * lcp_up - LCP has come UP.
1849 lcp_options *wo = &lcp_wantoptions[f->unit];
1850 lcp_options *ho = &lcp_hisoptions[f->unit];
1851 lcp_options *go = &lcp_gotoptions[f->unit];
1852 lcp_options *ao = &lcp_allowoptions[f->unit];
1855 if (!go->neg_magicnumber)
1856 go->magicnumber = 0;
1857 if (!ho->neg_magicnumber)
1858 ho->magicnumber = 0;
1861 * Set our MTU to the smaller of the MTU we wanted and
1862 * the MRU our peer wanted. If we negotiated an MRU,
1863 * set our MRU to the larger of value we wanted and
1864 * the value we got in the negotiation.
1865 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1866 * the interface MTU is set to the lowest of that, the
1867 * MTU we want to use, and our link MRU.
1869 mtu = ho->neg_mru? ho->mru: PPP_MRU;
1870 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1871 #ifdef PPP_WITH_MULTILINK
1872 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1873 #endif /* PPP_WITH_MULTILINK */
1874 netif_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1875 ppp_send_config(f->unit, mtu,
1876 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1877 ho->neg_pcompression, ho->neg_accompression);
1878 ppp_recv_config(f->unit, mru,
1879 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1880 go->neg_pcompression, go->neg_accompression);
1883 peer_mru[f->unit] = ho->mru;
1885 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1887 link_established(f->unit);
1892 * lcp_down - LCP has gone DOWN.
1894 * Alert other protocols.
1899 lcp_options *go = &lcp_gotoptions[f->unit];
1901 lcp_echo_lowerdown(f->unit);
1905 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1906 ppp_recv_config(f->unit, PPP_MRU,
1907 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1908 go->neg_pcompression, go->neg_accompression);
1909 peer_mru[f->unit] = PPP_MRU;
1914 * lcp_starting - LCP needs the lower layer up.
1917 lcp_starting(fsm *f)
1919 link_required(f->unit);
1924 * lcp_finished - LCP has finished with the lower layer.
1927 lcp_finished(fsm *f)
1929 link_terminated(f->unit);
1934 * lcp_printpkt - print the contents of an LCP packet.
1936 static char *lcp_codenames[] = {
1937 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1938 "TermReq", "TermAck", "CodeRej", "ProtRej",
1939 "EchoReq", "EchoRep", "DiscReq", "Ident",
1944 lcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
1946 int code, id, len, olen, i;
1947 u_char *pstart, *optend;
1951 if (plen < HEADERLEN)
1957 if (len < HEADERLEN || len > plen)
1960 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1961 printer(arg, " %s", lcp_codenames[code-1]);
1963 printer(arg, " code=0x%x", code);
1964 printer(arg, " id=0x%x", id);
1971 /* print option list */
1976 if (olen < 2 || olen > len) {
1984 if (olen == CILEN_SHORT) {
1986 GETSHORT(cishort, p);
1987 printer(arg, "mru %d", cishort);
1991 if (olen == CILEN_LONG) {
1994 printer(arg, "asyncmap 0x%x", cilong);
1998 if (olen >= CILEN_SHORT) {
2000 printer(arg, "auth ");
2001 GETSHORT(cishort, p);
2004 printer(arg, "pap");
2007 printer(arg, "chap");
2011 printer(arg, " MD5");
2014 case CHAP_MICROSOFT:
2015 printer(arg, " MS");
2019 case CHAP_MICROSOFT_V2:
2020 printer(arg, " MS-v2");
2027 printer(arg, "eap");
2030 printer(arg, "0x%x", cishort);
2035 if (olen >= CILEN_SHORT) {
2037 printer(arg, "quality ");
2038 GETSHORT(cishort, p);
2041 printer(arg, "lqr");
2044 printer(arg, "0x%x", cishort);
2049 if (olen >= CILEN_CHAR) {
2051 printer(arg, "callback ");
2052 GETCHAR(cishort, p);
2055 printer(arg, "CBCP");
2058 printer(arg, "0x%x", cishort);
2062 case CI_MAGICNUMBER:
2063 if (olen == CILEN_LONG) {
2066 printer(arg, "magic 0x%x", cilong);
2069 case CI_PCOMPRESSION:
2070 if (olen == CILEN_VOID) {
2072 printer(arg, "pcomp");
2075 case CI_ACCOMPRESSION:
2076 if (olen == CILEN_VOID) {
2078 printer(arg, "accomp");
2082 if (olen == CILEN_SHORT) {
2084 GETSHORT(cishort, p);
2085 printer(arg, "mrru %d", cishort);
2089 if (olen == CILEN_VOID) {
2091 printer(arg, "ssnhf");
2095 #ifdef PPP_WITH_MULTILINK
2096 if (olen >= CILEN_CHAR) {
2099 GETCHAR(epd.class, p);
2100 epd.length = olen - CILEN_CHAR;
2101 if (epd.length > MAX_ENDP_LEN)
2102 epd.length = MAX_ENDP_LEN;
2103 if (epd.length > 0) {
2104 BCOPY(p, epd.value, epd.length);
2107 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2110 printer(arg, "endpoint");
2114 while (p < optend) {
2116 printer(arg, " %.2x", code);
2124 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2126 print_string((char *)p, len, printer, arg);
2137 printer(arg, " magic=0x%x", cilong);
2146 printer(arg, " magic=0x%x", cilong);
2149 if (code == TIMEREM) {
2153 printer(arg, " seconds=%u", cilong);
2158 print_string((char *)p, len, printer, arg);
2165 /* print the rest of the bytes in the packet */
2166 for (i = 0; i < len && i < 32; ++i) {
2168 printer(arg, " %.2x", code);
2171 printer(arg, " ...");
2179 * Time to shut down the link because there is nothing out there.
2183 void LcpLinkFailure (fsm *f)
2185 if (f->state == OPENED) {
2186 info("No response to %d echo-requests", lcp_echos_pending);
2187 notice("Serial link appears to be disconnected.");
2188 status = EXIT_PEER_DEAD;
2189 lcp_close(f->unit, "Peer not responding");
2194 * Timer expired for the LCP echo requests from this process.
2198 LcpEchoCheck (fsm *f)
2200 LcpSendEchoRequest (f);
2201 if (f->state != OPENED)
2205 * Start the timer for the next interval.
2207 if (lcp_echo_timer_running)
2208 warn("assertion lcp_echo_timer_running==0 failed");
2209 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2210 lcp_echo_timer_running = 1;
2214 * LcpEchoTimeout - Timer expired on the LCP echo
2218 LcpEchoTimeout (void *arg)
2220 if (lcp_echo_timer_running != 0) {
2221 lcp_echo_timer_running = 0;
2222 LcpEchoCheck ((fsm *) arg);
2227 * LcpEchoReply - LCP has received a reply to the echo
2231 lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
2235 /* Check the magic number - don't count replies from ourselves. */
2237 dbglog("lcp: received short Echo-Reply, length %d", len);
2240 GETLONG(magic, inp);
2241 if (lcp_gotoptions[f->unit].neg_magicnumber
2242 && magic == lcp_gotoptions[f->unit].magicnumber) {
2243 warn("appear to have received our own echo-reply!");
2247 /* Reset the number of outstanding echo frames */
2248 lcp_echos_pending = 0;
2252 * LcpSendEchoRequest - Send an echo request frame to the peer
2256 LcpSendEchoRequest (fsm *f)
2258 u_int32_t lcp_magic;
2259 u_char pkt[4], *pktp;
2262 * Detect the failure of the peer at this point.
2264 if (lcp_echo_fails != 0) {
2265 if (lcp_echos_pending >= lcp_echo_fails) {
2267 lcp_echos_pending = 0;
2272 * If adaptive echos have been enabled, only send the echo request if
2273 * no traffic was received since the last one.
2275 if (lcp_echo_adaptive) {
2276 static unsigned int last_pkts_in = 0;
2277 struct pppd_stats cur_stats;
2279 if (get_ppp_stats(f->unit, &cur_stats) && cur_stats.pkts_in != last_pkts_in) {
2280 last_pkts_in = cur_stats.pkts_in;
2286 * Make and send the echo request frame.
2288 if (f->state == OPENED) {
2289 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2291 PUTLONG(lcp_magic, pktp);
2292 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2293 ++lcp_echos_pending;
2298 * lcp_echo_lowerup - Start the timer for the LCP frame
2302 lcp_echo_lowerup (int unit)
2304 fsm *f = &lcp_fsm[unit];
2306 /* Clear the parameters for generating echo frames */
2307 lcp_echos_pending = 0;
2308 lcp_echo_number = 0;
2309 lcp_echo_timer_running = 0;
2311 /* If a timeout interval is specified then start the timer */
2312 if (lcp_echo_interval != 0)
2317 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2321 lcp_echo_lowerdown (int unit)
2323 fsm *f = &lcp_fsm[unit];
2325 if (lcp_echo_timer_running != 0) {
2326 UNTIMEOUT (LcpEchoTimeout, f);
2327 lcp_echo_timer_running = 0;