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.
55 * When the link comes up we want to be able to wait for a short while,
56 * or until seeing some input from the peer, before starting to send
57 * configure-requests. We do this by delaying the fsm_lowerup call.
59 /* steal a bit in fsm flags word */
60 #define DELAYED_UP 0x100
62 static void lcp_delayed_up(void *);
65 * LCP-related command-line options.
67 int lcp_echo_interval = 0; /* Interval between LCP echo-requests */
68 int lcp_echo_fails = 0; /* Tolerance to unanswered echo-requests */
69 bool lcp_echo_adaptive = 0; /* request echo only if the link was idle */
70 bool lax_recv = 0; /* accept control chars in asyncmap */
71 bool noendpoint = 0; /* don't send/accept endpoint discriminator */
73 static int noopt(char **);
76 static int setendpoint(char **);
77 static void printendpoint(option_t *, void (*)(void *, char *, ...), void *);
78 #endif /* HAVE_MULTILINK */
80 static option_t lcp_option_list[] = {
82 { "-all", o_special_noarg, (void *)noopt,
83 "Don't request/allow any LCP options" },
85 { "noaccomp", o_bool, &lcp_wantoptions[0].neg_accompression,
86 "Disable address/control compression",
87 OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
88 { "-ac", o_bool, &lcp_wantoptions[0].neg_accompression,
89 "Disable address/control compression",
90 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_accompression },
92 { "asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
93 "Set asyncmap (for received packets)",
94 OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
95 { "-as", o_uint32, &lcp_wantoptions[0].asyncmap,
96 "Set asyncmap (for received packets)",
97 OPT_ALIAS | OPT_OR, &lcp_wantoptions[0].neg_asyncmap },
98 { "default-asyncmap", o_uint32, &lcp_wantoptions[0].asyncmap,
99 "Disable asyncmap negotiation",
100 OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
101 &lcp_allowoptions[0].neg_asyncmap },
102 { "-am", o_uint32, &lcp_wantoptions[0].asyncmap,
103 "Disable asyncmap negotiation",
104 OPT_ALIAS | OPT_OR | OPT_NOARG | OPT_VAL(~0U) | OPT_A2CLR,
105 &lcp_allowoptions[0].neg_asyncmap },
107 { "nomagic", o_bool, &lcp_wantoptions[0].neg_magicnumber,
108 "Disable magic number negotiation (looped-back line detection)",
109 OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
110 { "-mn", o_bool, &lcp_wantoptions[0].neg_magicnumber,
111 "Disable magic number negotiation (looped-back line detection)",
112 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_magicnumber },
114 { "mru", o_int, &lcp_wantoptions[0].mru,
115 "Set MRU (maximum received packet size) for negotiation",
116 OPT_PRIO, &lcp_wantoptions[0].neg_mru },
117 { "default-mru", o_bool, &lcp_wantoptions[0].neg_mru,
118 "Disable MRU negotiation (use default 1500)",
119 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
120 { "-mru", o_bool, &lcp_wantoptions[0].neg_mru,
121 "Disable MRU negotiation (use default 1500)",
122 OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_mru },
124 { "mtu", o_int, &lcp_allowoptions[0].mru,
125 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
127 { "nopcomp", o_bool, &lcp_wantoptions[0].neg_pcompression,
128 "Disable protocol field compression",
129 OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
130 { "-pc", o_bool, &lcp_wantoptions[0].neg_pcompression,
131 "Disable protocol field compression",
132 OPT_ALIAS | OPT_A2CLR, &lcp_allowoptions[0].neg_pcompression },
134 { "passive", o_bool, &lcp_wantoptions[0].passive,
135 "Set passive mode", 1 },
136 { "-p", o_bool, &lcp_wantoptions[0].passive,
137 "Set passive mode", OPT_ALIAS | 1 },
139 { "silent", o_bool, &lcp_wantoptions[0].silent,
140 "Set silent mode", 1 },
142 { "lcp-echo-failure", o_int, &lcp_echo_fails,
143 "Set number of consecutive echo failures to indicate link failure",
145 { "lcp-echo-interval", o_int, &lcp_echo_interval,
146 "Set time in seconds between LCP echo requests", OPT_PRIO },
147 { "lcp-echo-adaptive", o_bool, &lcp_echo_adaptive,
148 "Suppress LCP echo requests if traffic was received", 1 },
149 { "lcp-restart", o_int, &lcp_fsm[0].timeouttime,
150 "Set time in seconds between LCP retransmissions", OPT_PRIO },
151 { "lcp-max-terminate", o_int, &lcp_fsm[0].maxtermtransmits,
152 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO },
153 { "lcp-max-configure", o_int, &lcp_fsm[0].maxconfreqtransmits,
154 "Set maximum number of LCP configure-request transmissions", OPT_PRIO },
155 { "lcp-max-failure", o_int, &lcp_fsm[0].maxnakloops,
156 "Set limit on number of LCP configure-naks", OPT_PRIO },
158 { "receive-all", o_bool, &lax_recv,
159 "Accept all received control characters", 1 },
161 #ifdef HAVE_MULTILINK
162 { "mrru", o_int, &lcp_wantoptions[0].mrru,
163 "Maximum received packet size for multilink bundle",
164 OPT_PRIO, &lcp_wantoptions[0].neg_mrru },
166 { "mpshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
167 "Use short sequence numbers in multilink headers",
168 OPT_PRIO | 1, &lcp_allowoptions[0].neg_ssnhf },
169 { "nompshortseq", o_bool, &lcp_wantoptions[0].neg_ssnhf,
170 "Don't use short sequence numbers in multilink headers",
171 OPT_PRIOSUB | OPT_A2CLR, &lcp_allowoptions[0].neg_ssnhf },
173 { "endpoint", o_special, (void *) setendpoint,
174 "Endpoint discriminator for multilink",
175 OPT_PRIO | OPT_A2PRINTER, (void *) printendpoint },
176 #endif /* HAVE_MULTILINK */
178 { "noendpoint", o_bool, &noendpoint,
179 "Don't send or accept multilink endpoint discriminator", 1 },
185 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
186 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
187 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
188 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
189 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
191 static int lcp_echos_pending = 0; /* Number of outstanding echo msgs */
192 static int lcp_echo_number = 0; /* ID number of next echo frame */
193 static int lcp_echo_timer_running = 0; /* set if a timer is running */
195 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
198 * Callbacks for fsm code. (CI = Configuration Information)
200 static void lcp_resetci(fsm *); /* Reset our CI */
201 static int lcp_cilen(fsm *); /* Return length of our CI */
202 static void lcp_addci(fsm *, u_char *, int *); /* Add our CI to pkt */
203 static int lcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
204 static int lcp_nakci(fsm *, u_char *, int, int); /* Peer nak'd our CI */
205 static int lcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
206 static int lcp_reqci(fsm *, u_char *, int *, int); /* Rcv peer CI */
207 static void lcp_up(fsm *); /* We're UP */
208 static void lcp_down(fsm *); /* We're DOWN */
209 static void lcp_starting(fsm *); /* We need lower layer up */
210 static void lcp_finished(fsm *); /* We need lower layer down */
211 static int lcp_extcode(fsm *, int, int, u_char *, int);
212 static void lcp_rprotrej(fsm *, u_char *, int);
215 * routines to send LCP echos to peer
218 static void lcp_echo_lowerup(int);
219 static void lcp_echo_lowerdown(int);
220 static void LcpEchoTimeout(void *);
221 static void lcp_received_echo_reply(fsm *, int, u_char *, int);
222 static void LcpSendEchoRequest(fsm *);
223 static void LcpLinkFailure(fsm *);
224 static void LcpEchoCheck(fsm *);
226 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
227 lcp_resetci, /* Reset our Configuration Information */
228 lcp_cilen, /* Length of our Configuration Information */
229 lcp_addci, /* Add our Configuration Information */
230 lcp_ackci, /* ACK our Configuration Information */
231 lcp_nakci, /* NAK our Configuration Information */
232 lcp_rejci, /* Reject our Configuration Information */
233 lcp_reqci, /* Request peer's Configuration Information */
234 lcp_up, /* Called when fsm reaches OPENED state */
235 lcp_down, /* Called when fsm leaves OPENED state */
236 lcp_starting, /* Called when we want the lower layer up */
237 lcp_finished, /* Called when we want the lower layer down */
238 NULL, /* Called when Protocol-Reject received */
239 NULL, /* Retransmission is necessary */
240 lcp_extcode, /* Called to handle LCP-specific codes */
241 "LCP" /* String name of protocol */
245 * Protocol entry points.
246 * Some of these are called directly.
249 static void lcp_init(int);
250 static void lcp_input(int, u_char *, int);
251 static void lcp_protrej(int);
252 static int lcp_printpkt(u_char *, int, void (*)(void *, char *, ...), void *);
254 struct protent lcp_protent = {
274 int lcp_loopbackfail = DEFLOOPBACKFAIL;
277 * Length of each type of configuration option (in octets)
281 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
282 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
283 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
284 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
287 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
288 (x) == CONFNAK ? "NAK" : "REJ")
291 * noopt - Disable all options (why?).
296 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
297 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
302 #ifdef HAVE_MULTILINK
304 setendpoint(char **argv)
306 if (str_to_epdisc(&lcp_wantoptions[0].endpoint, *argv)) {
307 lcp_wantoptions[0].neg_endpoint = 1;
310 option_error("Can't parse '%s' as an endpoint discriminator", *argv);
315 printendpoint(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
317 printer(arg, "%s", epdisc_to_str(&lcp_wantoptions[0].endpoint));
319 #endif /* HAVE_MULTILINK */
322 * lcp_init - Initialize LCP.
327 fsm *f = &lcp_fsm[unit];
328 lcp_options *wo = &lcp_wantoptions[unit];
329 lcp_options *ao = &lcp_allowoptions[unit];
332 f->protocol = PPP_LCP;
333 f->callbacks = &lcp_callbacks;
337 BZERO(wo, sizeof(*wo));
340 wo->neg_asyncmap = 1;
341 wo->neg_magicnumber = 1;
342 wo->neg_pcompression = 1;
343 wo->neg_accompression = 1;
345 BZERO(ao, sizeof(*ao));
348 ao->neg_asyncmap = 1;
350 ao->chap_mdtype = chap_mdtype_all;
353 ao->neg_magicnumber = 1;
354 ao->neg_pcompression = 1;
355 ao->neg_accompression = 1;
356 ao->neg_endpoint = 1;
361 * lcp_open - LCP is allowed to come up.
366 fsm *f = &lcp_fsm[unit];
367 lcp_options *wo = &lcp_wantoptions[unit];
369 f->flags &= ~(OPT_PASSIVE | OPT_SILENT);
371 f->flags |= OPT_PASSIVE;
373 f->flags |= OPT_SILENT;
379 * lcp_close - Take LCP down.
382 lcp_close(int unit, char *reason)
384 fsm *f = &lcp_fsm[unit];
387 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
388 new_phase(PHASE_TERMINATE);
390 if (f->flags & DELAYED_UP) {
391 untimeout(lcp_delayed_up, f);
396 fsm_close(f, reason);
397 if (oldstate == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT|DELAYED_UP)) {
399 * This action is not strictly according to the FSM in RFC1548,
400 * but it does mean that the program terminates if you do a
401 * lcp_close() when a connection hasn't been established
402 * because we are in passive/silent mode or because we have
403 * delayed the fsm_lowerup() call and it hasn't happened yet.
405 f->flags &= ~DELAYED_UP;
412 * lcp_lowerup - The lower layer is up.
415 lcp_lowerup(int unit)
417 lcp_options *wo = &lcp_wantoptions[unit];
418 fsm *f = &lcp_fsm[unit];
421 * Don't use A/C or protocol compression on transmission,
422 * but accept A/C and protocol compressed packets
423 * if we are going to ask for A/C and protocol compression.
425 if (ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0) < 0
426 || ppp_recv_config(unit, PPP_MRU, (lax_recv? 0: 0xffffffff),
427 wo->neg_pcompression, wo->neg_accompression) < 0)
429 peer_mru[unit] = PPP_MRU;
431 if (listen_time != 0) {
432 f->flags |= DELAYED_UP;
433 timeout(lcp_delayed_up, f, 0, listen_time * 1000);
440 * lcp_lowerdown - The lower layer is down.
443 lcp_lowerdown(int unit)
445 fsm *f = &lcp_fsm[unit];
447 if (f->flags & DELAYED_UP) {
448 f->flags &= ~DELAYED_UP;
449 untimeout(lcp_delayed_up, f);
451 fsm_lowerdown(&lcp_fsm[unit]);
456 * lcp_delayed_up - Bring the lower layer up now.
459 lcp_delayed_up(void *arg)
463 if (f->flags & DELAYED_UP) {
464 f->flags &= ~DELAYED_UP;
471 * lcp_input - Input LCP packet.
474 lcp_input(int unit, u_char *p, int len)
476 fsm *f = &lcp_fsm[unit];
478 if (f->flags & DELAYED_UP) {
479 f->flags &= ~DELAYED_UP;
480 untimeout(lcp_delayed_up, f);
483 fsm_input(f, p, len);
487 * lcp_extcode - Handle a LCP-specific code.
490 lcp_extcode(fsm *f, int code, int id, u_char *inp, int len)
496 lcp_rprotrej(f, inp, len);
500 if (f->state != OPENED)
503 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
504 fsm_sdata(f, ECHOREP, id, inp, len);
508 lcp_received_echo_reply(f, id, inp, len);
524 * lcp_rprotrej - Receive an Protocol-Reject.
526 * Figure out which protocol is rejected and inform it.
529 lcp_rprotrej(fsm *f, u_char *inp, int len)
532 struct protent *protp;
537 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
544 * Protocol-Reject packets received in any state other than the LCP
545 * OPENED state SHOULD be silently discarded.
547 if( f->state != OPENED ){
548 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f->state));
552 pname = protocol_name(prot);
555 * Upcall the proper Protocol-Reject routine.
557 for (i = 0; (protp = protocols[i]) != NULL; ++i)
558 if (protp->protocol == prot && protp->enabled_flag) {
560 dbglog("Protocol-Reject for 0x%x received", prot);
562 dbglog("Protocol-Reject for '%s' (0x%x) received", pname,
564 (*protp->protrej)(f->unit);
569 warn("Protocol-Reject for unsupported protocol 0x%x", prot);
571 warn("Protocol-Reject for unsupported protocol '%s' (0x%x)", pname,
577 * lcp_protrej - A Protocol-Reject was received.
581 lcp_protrej(int unit)
586 error("Received Protocol-Reject for LCP!");
587 fsm_protreject(&lcp_fsm[unit]);
592 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
595 lcp_sprotrej(int unit, u_char *p, int len)
598 * Send back the protocol and the information field of the
599 * rejected packet. We only get here if LCP is in the OPENED state.
604 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
610 * lcp_resetci - Reset our CI.
615 lcp_options *wo = &lcp_wantoptions[f->unit];
616 lcp_options *go = &lcp_gotoptions[f->unit];
617 lcp_options *ao = &lcp_allowoptions[f->unit];
619 wo->magicnumber = magic();
625 go->neg_endpoint = 0;
628 ao->neg_endpoint = 0;
629 peer_mru[f->unit] = PPP_MRU;
635 * lcp_cilen - Return length of our CI.
640 lcp_options *go = &lcp_gotoptions[f->unit];
642 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
643 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
644 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
645 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
646 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
647 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
649 * NB: we only ask for one of CHAP, UPAP, or EAP, even if we will
650 * accept more than one. We prefer EAP first, then CHAP, then
653 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
654 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
655 LENCISHORT(go->neg_eap) +
656 LENCICHAP(!go->neg_eap && go->neg_chap) +
657 LENCISHORT(!go->neg_eap && !go->neg_chap && go->neg_upap) +
658 LENCILQR(go->neg_lqr) +
659 LENCICBCP(go->neg_cbcp) +
660 LENCILONG(go->neg_magicnumber) +
661 LENCIVOID(go->neg_pcompression) +
662 LENCIVOID(go->neg_accompression) +
663 LENCISHORT(go->neg_mrru) +
664 LENCIVOID(go->neg_ssnhf) +
665 (go->neg_endpoint? CILEN_CHAR + go->endpoint.length: 0));
670 * lcp_addci - Add our desired CIs to a packet.
673 lcp_addci(fsm *f, u_char *ucp, int *lenp)
675 lcp_options *go = &lcp_gotoptions[f->unit];
676 u_char *start_ucp = ucp;
678 #define ADDCIVOID(opt, neg) \
681 PUTCHAR(CILEN_VOID, ucp); \
683 #define ADDCISHORT(opt, neg, val) \
686 PUTCHAR(CILEN_SHORT, ucp); \
687 PUTSHORT(val, ucp); \
689 #define ADDCICHAP(opt, neg, val) \
691 PUTCHAR((opt), ucp); \
692 PUTCHAR(CILEN_CHAP, ucp); \
693 PUTSHORT(PPP_CHAP, ucp); \
694 PUTCHAR((CHAP_DIGEST(val)), ucp); \
696 #define ADDCILONG(opt, neg, val) \
699 PUTCHAR(CILEN_LONG, ucp); \
702 #define ADDCILQR(opt, neg, val) \
705 PUTCHAR(CILEN_LQR, ucp); \
706 PUTSHORT(PPP_LQR, ucp); \
709 #define ADDCICHAR(opt, neg, val) \
712 PUTCHAR(CILEN_CHAR, ucp); \
715 #define ADDCIENDP(opt, neg, class, val, len) \
719 PUTCHAR(CILEN_CHAR + len, ucp); \
720 PUTCHAR(class, ucp); \
721 for (i = 0; i < len; ++i) \
722 PUTCHAR(val[i], ucp); \
725 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
726 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
728 ADDCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
729 ADDCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
730 ADDCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
732 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
733 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
734 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
735 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
736 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
737 ADDCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
738 ADDCIVOID(CI_SSNHF, go->neg_ssnhf);
739 ADDCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
740 go->endpoint.value, go->endpoint.length);
742 if (ucp - start_ucp != *lenp) {
743 /* this should never happen, because peer_mtu should be 1500 */
744 error("Bug in lcp_addci: wrong length");
750 * lcp_ackci - Ack our CIs.
751 * This should not modify any state if the Ack is bad.
758 lcp_ackci(fsm *f, u_char *p, int len)
760 lcp_options *go = &lcp_gotoptions[f->unit];
761 u_char cilen, citype, cichar;
766 * CIs must be in exactly the same order that we sent.
767 * Check packet length and CI length at each step.
768 * If we find any deviations, then this packet is bad.
770 #define ACKCIVOID(opt, neg) \
772 if ((len -= CILEN_VOID) < 0) \
774 GETCHAR(citype, p); \
776 if (cilen != CILEN_VOID || \
780 #define ACKCISHORT(opt, neg, val) \
782 if ((len -= CILEN_SHORT) < 0) \
784 GETCHAR(citype, p); \
786 if (cilen != CILEN_SHORT || \
789 GETSHORT(cishort, p); \
790 if (cishort != val) \
793 #define ACKCICHAR(opt, neg, val) \
795 if ((len -= CILEN_CHAR) < 0) \
797 GETCHAR(citype, p); \
799 if (cilen != CILEN_CHAR || \
802 GETCHAR(cichar, p); \
806 #define ACKCICHAP(opt, neg, val) \
808 if ((len -= CILEN_CHAP) < 0) \
810 GETCHAR(citype, p); \
812 if (cilen != CILEN_CHAP || \
815 GETSHORT(cishort, p); \
816 if (cishort != PPP_CHAP) \
818 GETCHAR(cichar, p); \
819 if (cichar != (CHAP_DIGEST(val))) \
822 #define ACKCILONG(opt, neg, val) \
824 if ((len -= CILEN_LONG) < 0) \
826 GETCHAR(citype, p); \
828 if (cilen != CILEN_LONG || \
831 GETLONG(cilong, p); \
835 #define ACKCILQR(opt, neg, val) \
837 if ((len -= CILEN_LQR) < 0) \
839 GETCHAR(citype, p); \
841 if (cilen != CILEN_LQR || \
844 GETSHORT(cishort, p); \
845 if (cishort != PPP_LQR) \
847 GETLONG(cilong, p); \
851 #define ACKCIENDP(opt, neg, class, val, vlen) \
854 if ((len -= CILEN_CHAR + vlen) < 0) \
856 GETCHAR(citype, p); \
858 if (cilen != CILEN_CHAR + vlen || \
861 GETCHAR(cichar, p); \
862 if (cichar != class) \
864 for (i = 0; i < vlen; ++i) { \
865 GETCHAR(cichar, p); \
866 if (cichar != val[i]) \
871 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
872 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
874 ACKCISHORT(CI_AUTHTYPE, go->neg_eap, PPP_EAP);
875 ACKCICHAP(CI_AUTHTYPE, !go->neg_eap && go->neg_chap, go->chap_mdtype);
876 ACKCISHORT(CI_AUTHTYPE, !go->neg_eap && !go->neg_chap && go->neg_upap,
878 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
879 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
880 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
881 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
882 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
883 ACKCISHORT(CI_MRRU, go->neg_mrru, go->mrru);
884 ACKCIVOID(CI_SSNHF, go->neg_ssnhf);
885 ACKCIENDP(CI_EPDISC, go->neg_endpoint, go->endpoint.class,
886 go->endpoint.value, go->endpoint.length);
889 * If there are any remaining CIs, then this packet is bad.
895 LCPDEBUG(("lcp_acki: received bad Ack!"));
901 * lcp_nakci - Peer has sent a NAK for some of our CIs.
902 * This should not modify any state if the Nak is bad
903 * or if LCP is in the OPENED state.
910 lcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
912 lcp_options *go = &lcp_gotoptions[f->unit];
913 lcp_options *wo = &lcp_wantoptions[f->unit];
914 u_char citype, cichar, *next;
917 lcp_options no; /* options we've seen Naks for */
918 lcp_options try; /* options to request next time */
922 BZERO(&no, sizeof(no));
926 * Any Nak'd CIs must be in exactly the same order that we sent.
927 * Check packet length and CI length at each step.
928 * If we find any deviations, then this packet is bad.
930 #define NAKCIVOID(opt, neg) \
932 len >= CILEN_VOID && \
933 p[1] == CILEN_VOID && \
936 INCPTR(CILEN_VOID, p); \
940 #define NAKCICHAP(opt, neg, code) \
942 len >= CILEN_CHAP && \
943 p[1] == CILEN_CHAP && \
947 GETSHORT(cishort, p); \
948 GETCHAR(cichar, p); \
952 #define NAKCICHAR(opt, neg, code) \
954 len >= CILEN_CHAR && \
955 p[1] == CILEN_CHAR && \
959 GETCHAR(cichar, p); \
963 #define NAKCISHORT(opt, neg, code) \
965 len >= CILEN_SHORT && \
966 p[1] == CILEN_SHORT && \
968 len -= CILEN_SHORT; \
970 GETSHORT(cishort, p); \
974 #define NAKCILONG(opt, neg, code) \
976 len >= CILEN_LONG && \
977 p[1] == CILEN_LONG && \
981 GETLONG(cilong, p); \
985 #define NAKCILQR(opt, neg, code) \
987 len >= CILEN_LQR && \
988 p[1] == CILEN_LQR && \
992 GETSHORT(cishort, p); \
993 GETLONG(cilong, p); \
997 #define NAKCIENDP(opt, neg) \
999 len >= CILEN_CHAR && \
1001 p[1] >= CILEN_CHAR && \
1010 * NOTE! There must be no assignments to individual fields of *go in
1011 * the code below. Any such assignment is a BUG!
1014 * We don't care if they want to send us smaller packets than
1015 * we want. Therefore, accept any MRU less than what we asked for,
1016 * but then ignore the new value when setting the MRU in the kernel.
1017 * If they send us a bigger MRU than what we asked, accept it, up to
1018 * the limit of the default MRU we'd get if we didn't negotiate.
1020 if (go->neg_mru && go->mru != DEFMRU) {
1021 NAKCISHORT(CI_MRU, neg_mru,
1022 if (cishort <= wo->mru || cishort <= DEFMRU)
1028 * Add any characters they want to our (receive-side) asyncmap.
1030 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
1031 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
1032 try.asyncmap = go->asyncmap | cilong;
1037 * If they've nak'd our authentication-protocol, check whether
1038 * they are proposing a different protocol, or a different
1039 * hash algorithm for CHAP.
1041 if ((go->neg_chap || go->neg_upap || go->neg_eap)
1042 && len >= CILEN_SHORT
1043 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
1046 no.neg_chap = go->neg_chap;
1047 no.neg_upap = go->neg_upap;
1048 no.neg_eap = go->neg_eap;
1050 GETSHORT(cishort, p);
1051 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
1052 /* If we were asking for EAP, then we need to stop that. */
1056 /* If we were asking for CHAP, then we need to stop that. */
1057 else if (go->neg_chap)
1060 * If we weren't asking for CHAP or EAP, then we were asking for
1061 * PAP, in which case this Nak is bad.
1066 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
1068 /* Stop asking for EAP, if we were. */
1071 /* Try to set up to use their suggestion, if possible */
1072 if (CHAP_CANDIGEST(go->chap_mdtype, cichar))
1073 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1074 } else if (go->neg_chap) {
1076 * We were asking for our preferred algorithm, they must
1077 * want something different.
1079 if (cichar != CHAP_DIGEST(go->chap_mdtype)) {
1080 if (CHAP_CANDIGEST(go->chap_mdtype, cichar)) {
1081 /* Use their suggestion if we support it ... */
1082 try.chap_mdtype = CHAP_MDTYPE_D(cichar);
1084 /* ... otherwise, try our next-preferred algorithm. */
1085 try.chap_mdtype &= ~(CHAP_MDTYPE(try.chap_mdtype));
1086 if (try.chap_mdtype == MDTYPE_NONE) /* out of algos */
1091 * Whoops, they Nak'd our algorithm of choice
1092 * but then suggested it back to us.
1098 * Stop asking for PAP if we were asking for it.
1106 * If we were asking for EAP, and they're Conf-Naking EAP,
1107 * well, that's just strange. Nobody should do that.
1109 if (cishort == PPP_EAP && cilen == CILEN_SHORT && go->neg_eap)
1110 dbglog("Unexpected Conf-Nak for EAP");
1113 * We don't recognize what they're suggesting.
1114 * Stop asking for what we were asking for.
1118 else if (go->neg_chap)
1122 p += cilen - CILEN_SHORT;
1127 * If they can't cope with our link quality protocol, we'll have
1128 * to stop asking for LQR. We haven't got any other protocol.
1129 * If they Nak the reporting period, take their value XXX ?
1131 NAKCILQR(CI_QUALITY, neg_lqr,
1132 if (cishort != PPP_LQR)
1135 try.lqr_period = cilong;
1139 * Only implementing CBCP...not the rest of the callback options
1141 NAKCICHAR(CI_CALLBACK, neg_cbcp,
1146 * Check for a looped-back line.
1148 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
1149 try.magicnumber = magic();
1154 * Peer shouldn't send Nak for protocol compression or
1155 * address/control compression requests; they should send
1156 * a Reject instead. If they send a Nak, treat it as a Reject.
1158 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression);
1159 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression);
1162 * Nak for MRRU option - accept their value if it is smaller
1163 * than the one we want.
1166 NAKCISHORT(CI_MRRU, neg_mrru,
1167 if (treat_as_reject)
1169 else if (cishort <= wo->mrru)
1175 * Nak for short sequence numbers shouldn't be sent, treat it
1178 NAKCIVOID(CI_SSNHF, neg_ssnhf);
1181 * Nak of the endpoint discriminator option is not permitted,
1182 * treat it like a reject.
1184 NAKCIENDP(CI_EPDISC, neg_endpoint);
1187 * There may be remaining CIs, if the peer is requesting negotiation
1188 * on an option that we didn't include in our request packet.
1189 * If we see an option that we requested, or one we've already seen
1190 * in this packet, then this packet is bad.
1191 * If we wanted to respond by starting to negotiate on the requested
1192 * option(s), we could, but we don't, because except for the
1193 * authentication type and quality protocol, if we are not negotiating
1194 * an option, it is because we were told not to.
1195 * For the authentication type, the Nak from the peer means
1196 * `let me authenticate myself with you' which is a bit pointless.
1197 * For the quality protocol, the Nak means `ask me to send you quality
1198 * reports', but if we didn't ask for them, we don't want them.
1199 * An option we don't recognize represents the peer asking to
1200 * negotiate some option we don't support, so ignore it.
1202 while (len >= CILEN_VOID) {
1205 if (cilen < CILEN_VOID || (len -= cilen) < 0)
1207 next = p + cilen - 2;
1211 if ((go->neg_mru && go->mru != DEFMRU)
1212 || no.neg_mru || cilen != CILEN_SHORT)
1214 GETSHORT(cishort, p);
1215 if (cishort < DEFMRU) {
1221 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
1222 || no.neg_asyncmap || cilen != CILEN_LONG)
1226 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap ||
1227 go->neg_eap || no.neg_eap)
1230 case CI_MAGICNUMBER:
1231 if (go->neg_magicnumber || no.neg_magicnumber ||
1232 cilen != CILEN_LONG)
1235 case CI_PCOMPRESSION:
1236 if (go->neg_pcompression || no.neg_pcompression
1237 || cilen != CILEN_VOID)
1240 case CI_ACCOMPRESSION:
1241 if (go->neg_accompression || no.neg_accompression
1242 || cilen != CILEN_VOID)
1246 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
1250 if (go->neg_mrru || no.neg_mrru || cilen != CILEN_SHORT)
1254 if (go->neg_ssnhf || no.neg_ssnhf || cilen != CILEN_VOID)
1259 if (go->neg_endpoint || no.neg_endpoint || cilen < CILEN_CHAR)
1267 * OK, the Nak is good. Now we can update state.
1268 * If there are any options left we ignore them.
1270 if (f->state != OPENED) {
1272 if (++try.numloops >= lcp_loopbackfail) {
1273 notice("Serial line is looped back.");
1274 status = EXIT_LOOPBACK;
1275 lcp_close(f->unit, "Loopback detected");
1285 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1291 * lcp_rejci - Peer has Rejected some of our CIs.
1292 * This should not modify any state if the Reject is bad
1293 * or if LCP is in the OPENED state.
1296 * 0 - Reject was bad.
1297 * 1 - Reject was good.
1300 lcp_rejci(fsm *f, u_char *p, int len)
1302 lcp_options *go = &lcp_gotoptions[f->unit];
1306 lcp_options try; /* options to request next time */
1311 * Any Rejected CIs must be in exactly the same order that we sent.
1312 * Check packet length and CI length at each step.
1313 * If we find any deviations, then this packet is bad.
1315 #define REJCIVOID(opt, neg) \
1317 len >= CILEN_VOID && \
1318 p[1] == CILEN_VOID && \
1320 len -= CILEN_VOID; \
1321 INCPTR(CILEN_VOID, p); \
1324 #define REJCISHORT(opt, neg, val) \
1326 len >= CILEN_SHORT && \
1327 p[1] == CILEN_SHORT && \
1329 len -= CILEN_SHORT; \
1331 GETSHORT(cishort, p); \
1332 /* Check rejected value. */ \
1333 if (cishort != val) \
1337 #define REJCICHAP(opt, neg, val) \
1339 len >= CILEN_CHAP && \
1340 p[1] == CILEN_CHAP && \
1342 len -= CILEN_CHAP; \
1344 GETSHORT(cishort, p); \
1345 GETCHAR(cichar, p); \
1346 /* Check rejected value. */ \
1347 if ((cishort != PPP_CHAP) || (cichar != (CHAP_DIGEST(val)))) \
1350 try.neg_eap = try.neg_upap = 0; \
1352 #define REJCILONG(opt, neg, val) \
1354 len >= CILEN_LONG && \
1355 p[1] == CILEN_LONG && \
1357 len -= CILEN_LONG; \
1359 GETLONG(cilong, p); \
1360 /* Check rejected value. */ \
1361 if (cilong != val) \
1365 #define REJCILQR(opt, neg, val) \
1367 len >= CILEN_LQR && \
1368 p[1] == CILEN_LQR && \
1372 GETSHORT(cishort, p); \
1373 GETLONG(cilong, p); \
1374 /* Check rejected value. */ \
1375 if (cishort != PPP_LQR || cilong != val) \
1379 #define REJCICBCP(opt, neg, val) \
1381 len >= CILEN_CBCP && \
1382 p[1] == CILEN_CBCP && \
1384 len -= CILEN_CBCP; \
1386 GETCHAR(cichar, p); \
1387 /* Check rejected value. */ \
1388 if (cichar != val) \
1392 #define REJCIENDP(opt, neg, class, val, vlen) \
1394 len >= CILEN_CHAR + vlen && \
1396 p[1] == CILEN_CHAR + vlen) { \
1398 len -= CILEN_CHAR + vlen; \
1400 GETCHAR(cichar, p); \
1401 if (cichar != class) \
1403 for (i = 0; i < vlen; ++i) { \
1404 GETCHAR(cichar, p); \
1405 if (cichar != val[i]) \
1411 REJCISHORT(CI_MRU, neg_mru, go->mru);
1412 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1413 REJCISHORT(CI_AUTHTYPE, neg_eap, PPP_EAP);
1415 REJCICHAP(CI_AUTHTYPE, neg_chap, go->chap_mdtype);
1416 if (!go->neg_chap) {
1417 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1420 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1421 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1422 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1423 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1424 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1425 REJCISHORT(CI_MRRU, neg_mrru, go->mrru);
1426 REJCIVOID(CI_SSNHF, neg_ssnhf);
1427 REJCIENDP(CI_EPDISC, neg_endpoint, go->endpoint.class,
1428 go->endpoint.value, go->endpoint.length);
1431 * If there are any remaining CIs, then this packet is bad.
1436 * Now we can update state.
1438 if (f->state != OPENED)
1443 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1449 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1451 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1452 * appropriately. If reject_if_disagree is non-zero, doesn't return
1453 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1456 lcp_reqci(fsm *f, u_char *inp, int *lenp, int reject_if_disagree)
1458 lcp_options *go = &lcp_gotoptions[f->unit];
1459 lcp_options *ho = &lcp_hisoptions[f->unit];
1460 lcp_options *ao = &lcp_allowoptions[f->unit];
1461 u_char *cip, *next; /* Pointer to current and next CIs */
1462 int cilen, citype, cichar; /* Parsed len, type, char value */
1463 u_short cishort; /* Parsed short value */
1464 u_int32_t cilong; /* Parse long value */
1465 int rc = CONFACK; /* Final packet return code */
1466 int orc; /* Individual option return code */
1467 u_char *p; /* Pointer to next char to parse */
1468 u_char *rejp; /* Pointer to next char in reject frame */
1469 u_char *nakp; /* Pointer to next char in Nak frame */
1470 int l = *lenp; /* Length left */
1473 * Reset all his options.
1475 BZERO(ho, sizeof(*ho));
1478 * Process all his options.
1484 orc = CONFACK; /* Assume success */
1485 cip = p = next; /* Remember begining of CI */
1486 if (l < 2 || /* Not enough data for CI header or */
1487 p[1] < 2 || /* CI length too small or */
1488 p[1] > l) { /* CI length too big? */
1489 LCPDEBUG(("lcp_reqci: bad CI length!"));
1490 orc = CONFREJ; /* Reject bad CI */
1491 cilen = l; /* Reject till end of packet */
1492 l = 0; /* Don't loop again */
1496 GETCHAR(citype, p); /* Parse CI type */
1497 GETCHAR(cilen, p); /* Parse CI length */
1498 l -= cilen; /* Adjust remaining length */
1499 next += cilen; /* Step to next CI */
1501 switch (citype) { /* Check CI type */
1503 if (!ao->neg_mru || /* Allow option? */
1504 cilen != CILEN_SHORT) { /* Check CI length */
1505 orc = CONFREJ; /* Reject CI */
1508 GETSHORT(cishort, p); /* Parse MRU */
1511 * He must be able to receive at least our minimum.
1512 * No need to check a maximum. If he sends a large number,
1513 * we'll just ignore it.
1515 if (cishort < MINMRU) {
1516 orc = CONFNAK; /* Nak CI */
1517 PUTCHAR(CI_MRU, nakp);
1518 PUTCHAR(CILEN_SHORT, nakp);
1519 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1522 ho->neg_mru = 1; /* Remember he sent MRU */
1523 ho->mru = cishort; /* And remember value */
1527 if (!ao->neg_asyncmap ||
1528 cilen != CILEN_LONG) {
1535 * Asyncmap must have set at least the bits
1536 * which are set in lcp_allowoptions[unit].asyncmap.
1538 if ((ao->asyncmap & ~cilong) != 0) {
1540 PUTCHAR(CI_ASYNCMAP, nakp);
1541 PUTCHAR(CILEN_LONG, nakp);
1542 PUTLONG(ao->asyncmap | cilong, nakp);
1545 ho->neg_asyncmap = 1;
1546 ho->asyncmap = cilong;
1550 if (cilen < CILEN_SHORT ||
1551 !(ao->neg_upap || ao->neg_chap || ao->neg_eap)) {
1553 * Reject the option if we're not willing to authenticate.
1555 dbglog("No auth is possible");
1559 GETSHORT(cishort, p);
1562 * Authtype must be PAP, CHAP, or EAP.
1564 * Note: if more than one of ao->neg_upap, ao->neg_chap, and
1565 * ao->neg_eap are set, and the peer sends a Configure-Request
1566 * with two or more authenticate-protocol requests, then we will
1567 * reject the second request.
1568 * Whether we end up doing CHAP, UPAP, or EAP depends then on
1569 * the ordering of the CIs in the peer's Configure-Request.
1572 if (cishort == PPP_PAP) {
1573 /* we've already accepted CHAP or EAP */
1574 if (ho->neg_chap || ho->neg_eap ||
1575 cilen != CILEN_SHORT) {
1576 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1580 if (!ao->neg_upap) { /* we don't want to do PAP */
1581 orc = CONFNAK; /* NAK it and suggest CHAP or EAP */
1582 PUTCHAR(CI_AUTHTYPE, nakp);
1584 PUTCHAR(CILEN_SHORT, nakp);
1585 PUTSHORT(PPP_EAP, nakp);
1587 PUTCHAR(CILEN_CHAP, nakp);
1588 PUTSHORT(PPP_CHAP, nakp);
1589 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1596 if (cishort == PPP_CHAP) {
1597 /* we've already accepted PAP or EAP */
1598 if (ho->neg_upap || ho->neg_eap ||
1599 cilen != CILEN_CHAP) {
1600 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1604 if (!ao->neg_chap) { /* we don't want to do CHAP */
1605 orc = CONFNAK; /* NAK it and suggest EAP or PAP */
1606 PUTCHAR(CI_AUTHTYPE, nakp);
1607 PUTCHAR(CILEN_SHORT, nakp);
1609 PUTSHORT(PPP_EAP, nakp);
1611 PUTSHORT(PPP_PAP, nakp);
1615 GETCHAR(cichar, p); /* get digest type */
1616 if (!(CHAP_CANDIGEST(ao->chap_mdtype, cichar))) {
1618 * We can't/won't do the requested type,
1619 * suggest something else.
1622 PUTCHAR(CI_AUTHTYPE, nakp);
1623 PUTCHAR(CILEN_CHAP, nakp);
1624 PUTSHORT(PPP_CHAP, nakp);
1625 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1628 ho->chap_mdtype = CHAP_MDTYPE_D(cichar); /* save md type */
1632 if (cishort == PPP_EAP) {
1633 /* we've already accepted CHAP or PAP */
1634 if (ho->neg_chap || ho->neg_upap || cilen != CILEN_SHORT) {
1635 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE EAP, rejecting..."));
1639 if (!ao->neg_eap) { /* we don't want to do EAP */
1640 orc = CONFNAK; /* NAK it and suggest CHAP or PAP */
1641 PUTCHAR(CI_AUTHTYPE, nakp);
1643 PUTCHAR(CILEN_CHAP, nakp);
1644 PUTSHORT(PPP_CHAP, nakp);
1645 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1647 PUTCHAR(CILEN_SHORT, nakp);
1648 PUTSHORT(PPP_PAP, nakp);
1657 * We don't recognize the protocol they're asking for.
1658 * Nak it with something we're willing to do.
1659 * (At this point we know ao->neg_upap || ao->neg_chap ||
1663 PUTCHAR(CI_AUTHTYPE, nakp);
1665 PUTCHAR(CILEN_SHORT, nakp);
1666 PUTSHORT(PPP_EAP, nakp);
1667 } else if (ao->neg_chap) {
1668 PUTCHAR(CILEN_CHAP, nakp);
1669 PUTSHORT(PPP_CHAP, nakp);
1670 PUTCHAR(CHAP_DIGEST(ao->chap_mdtype), nakp);
1672 PUTCHAR(CILEN_SHORT, nakp);
1673 PUTSHORT(PPP_PAP, nakp);
1679 cilen != CILEN_LQR) {
1684 GETSHORT(cishort, p);
1688 * Check the protocol and the reporting period.
1689 * XXX When should we Nak this, and what with?
1691 if (cishort != PPP_LQR) {
1693 PUTCHAR(CI_QUALITY, nakp);
1694 PUTCHAR(CILEN_LQR, nakp);
1695 PUTSHORT(PPP_LQR, nakp);
1696 PUTLONG(ao->lqr_period, nakp);
1701 case CI_MAGICNUMBER:
1702 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1703 cilen != CILEN_LONG) {
1710 * He must have a different magic number.
1712 if (go->neg_magicnumber &&
1713 cilong == go->magicnumber) {
1714 cilong = magic(); /* Don't put magic() inside macro! */
1716 PUTCHAR(CI_MAGICNUMBER, nakp);
1717 PUTCHAR(CILEN_LONG, nakp);
1718 PUTLONG(cilong, nakp);
1721 ho->neg_magicnumber = 1;
1722 ho->magicnumber = cilong;
1726 case CI_PCOMPRESSION:
1727 if (!ao->neg_pcompression ||
1728 cilen != CILEN_VOID) {
1732 ho->neg_pcompression = 1;
1735 case CI_ACCOMPRESSION:
1736 if (!ao->neg_accompression ||
1737 cilen != CILEN_VOID) {
1741 ho->neg_accompression = 1;
1745 if (!ao->neg_mrru || !multilink ||
1746 cilen != CILEN_SHORT) {
1751 GETSHORT(cishort, p);
1752 /* possibly should insist on a minimum/maximum MRRU here */
1758 if (!ao->neg_ssnhf || !multilink ||
1759 cilen != CILEN_VOID) {
1767 if (!ao->neg_endpoint ||
1768 cilen < CILEN_CHAR ||
1769 cilen > CILEN_CHAR + MAX_ENDP_LEN) {
1774 cilen -= CILEN_CHAR;
1775 ho->neg_endpoint = 1;
1776 ho->endpoint.class = cichar;
1777 ho->endpoint.length = cilen;
1778 BCOPY(p, ho->endpoint.value, cilen);
1783 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype));
1789 if (orc == CONFACK && /* Good CI */
1790 rc != CONFACK) /* but prior CI wasnt? */
1791 continue; /* Don't send this one */
1793 if (orc == CONFNAK) { /* Nak this CI? */
1794 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1795 && citype != CI_MAGICNUMBER) {
1796 orc = CONFREJ; /* Get tough if so */
1798 if (rc == CONFREJ) /* Rejecting prior CI? */
1799 continue; /* Don't send this one */
1803 if (orc == CONFREJ) { /* Reject this CI */
1805 if (cip != rejp) /* Need to move rejected CI? */
1806 BCOPY(cip, rejp, cilen); /* Move it */
1807 INCPTR(cilen, rejp); /* Update output pointer */
1812 * If we wanted to send additional NAKs (for unsent CIs), the
1813 * code would go here. The extra NAKs would go at *nakp.
1814 * At present there are no cases where we want to ask the
1815 * peer to negotiate an option.
1824 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1826 *lenp = nakp - nak_buffer;
1827 BCOPY(nak_buffer, inp, *lenp);
1834 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc)));
1835 return (rc); /* Return final code */
1840 * lcp_up - LCP has come UP.
1845 lcp_options *wo = &lcp_wantoptions[f->unit];
1846 lcp_options *ho = &lcp_hisoptions[f->unit];
1847 lcp_options *go = &lcp_gotoptions[f->unit];
1848 lcp_options *ao = &lcp_allowoptions[f->unit];
1851 if (!go->neg_magicnumber)
1852 go->magicnumber = 0;
1853 if (!ho->neg_magicnumber)
1854 ho->magicnumber = 0;
1857 * Set our MTU to the smaller of the MTU we wanted and
1858 * the MRU our peer wanted. If we negotiated an MRU,
1859 * set our MRU to the larger of value we wanted and
1860 * the value we got in the negotiation.
1861 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1862 * the interface MTU is set to the lowest of that, the
1863 * MTU we want to use, and our link MRU.
1865 mtu = ho->neg_mru? ho->mru: PPP_MRU;
1866 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
1867 #ifdef HAVE_MULTILINK
1868 if (!(multilink && go->neg_mrru && ho->neg_mrru))
1869 #endif /* HAVE_MULTILINK */
1870 netif_set_mtu(f->unit, MIN(MIN(mtu, mru), ao->mru));
1871 ppp_send_config(f->unit, mtu,
1872 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1873 ho->neg_pcompression, ho->neg_accompression);
1874 ppp_recv_config(f->unit, mru,
1875 (lax_recv? 0: go->neg_asyncmap? go->asyncmap: 0xffffffff),
1876 go->neg_pcompression, go->neg_accompression);
1879 peer_mru[f->unit] = ho->mru;
1881 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1883 link_established(f->unit);
1888 * lcp_down - LCP has gone DOWN.
1890 * Alert other protocols.
1895 lcp_options *go = &lcp_gotoptions[f->unit];
1897 lcp_echo_lowerdown(f->unit);
1901 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1902 ppp_recv_config(f->unit, PPP_MRU,
1903 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1904 go->neg_pcompression, go->neg_accompression);
1905 peer_mru[f->unit] = PPP_MRU;
1910 * lcp_starting - LCP needs the lower layer up.
1913 lcp_starting(fsm *f)
1915 link_required(f->unit);
1920 * lcp_finished - LCP has finished with the lower layer.
1923 lcp_finished(fsm *f)
1925 link_terminated(f->unit);
1930 * lcp_printpkt - print the contents of an LCP packet.
1932 static char *lcp_codenames[] = {
1933 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1934 "TermReq", "TermAck", "CodeRej", "ProtRej",
1935 "EchoReq", "EchoRep", "DiscReq", "Ident",
1940 lcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
1942 int code, id, len, olen, i;
1943 u_char *pstart, *optend;
1947 if (plen < HEADERLEN)
1953 if (len < HEADERLEN || len > plen)
1956 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1957 printer(arg, " %s", lcp_codenames[code-1]);
1959 printer(arg, " code=0x%x", code);
1960 printer(arg, " id=0x%x", id);
1967 /* print option list */
1972 if (olen < 2 || olen > len) {
1980 if (olen == CILEN_SHORT) {
1982 GETSHORT(cishort, p);
1983 printer(arg, "mru %d", cishort);
1987 if (olen == CILEN_LONG) {
1990 printer(arg, "asyncmap 0x%x", cilong);
1994 if (olen >= CILEN_SHORT) {
1996 printer(arg, "auth ");
1997 GETSHORT(cishort, p);
2000 printer(arg, "pap");
2003 printer(arg, "chap");
2007 printer(arg, " MD5");
2010 case CHAP_MICROSOFT:
2011 printer(arg, " MS");
2015 case CHAP_MICROSOFT_V2:
2016 printer(arg, " MS-v2");
2023 printer(arg, "eap");
2026 printer(arg, "0x%x", cishort);
2031 if (olen >= CILEN_SHORT) {
2033 printer(arg, "quality ");
2034 GETSHORT(cishort, p);
2037 printer(arg, "lqr");
2040 printer(arg, "0x%x", cishort);
2045 if (olen >= CILEN_CHAR) {
2047 printer(arg, "callback ");
2048 GETCHAR(cishort, p);
2051 printer(arg, "CBCP");
2054 printer(arg, "0x%x", cishort);
2058 case CI_MAGICNUMBER:
2059 if (olen == CILEN_LONG) {
2062 printer(arg, "magic 0x%x", cilong);
2065 case CI_PCOMPRESSION:
2066 if (olen == CILEN_VOID) {
2068 printer(arg, "pcomp");
2071 case CI_ACCOMPRESSION:
2072 if (olen == CILEN_VOID) {
2074 printer(arg, "accomp");
2078 if (olen == CILEN_SHORT) {
2080 GETSHORT(cishort, p);
2081 printer(arg, "mrru %d", cishort);
2085 if (olen == CILEN_VOID) {
2087 printer(arg, "ssnhf");
2091 #ifdef HAVE_MULTILINK
2092 if (olen >= CILEN_CHAR) {
2095 GETCHAR(epd.class, p);
2096 epd.length = olen - CILEN_CHAR;
2097 if (epd.length > MAX_ENDP_LEN)
2098 epd.length = MAX_ENDP_LEN;
2099 if (epd.length > 0) {
2100 BCOPY(p, epd.value, epd.length);
2103 printer(arg, "endpoint [%s]", epdisc_to_str(&epd));
2106 printer(arg, "endpoint");
2110 while (p < optend) {
2112 printer(arg, " %.2x", code);
2120 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2122 print_string((char *)p, len, printer, arg);
2133 printer(arg, " magic=0x%x", cilong);
2142 printer(arg, " magic=0x%x", cilong);
2145 if (code == TIMEREM) {
2149 printer(arg, " seconds=%u", cilong);
2154 print_string((char *)p, len, printer, arg);
2161 /* print the rest of the bytes in the packet */
2162 for (i = 0; i < len && i < 32; ++i) {
2164 printer(arg, " %.2x", code);
2167 printer(arg, " ...");
2175 * Time to shut down the link because there is nothing out there.
2179 void LcpLinkFailure (fsm *f)
2181 if (f->state == OPENED) {
2182 info("No response to %d echo-requests", lcp_echos_pending);
2183 notice("Serial link appears to be disconnected.");
2184 status = EXIT_PEER_DEAD;
2185 lcp_close(f->unit, "Peer not responding");
2190 * Timer expired for the LCP echo requests from this process.
2194 LcpEchoCheck (fsm *f)
2196 LcpSendEchoRequest (f);
2197 if (f->state != OPENED)
2201 * Start the timer for the next interval.
2203 if (lcp_echo_timer_running)
2204 warn("assertion lcp_echo_timer_running==0 failed");
2205 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
2206 lcp_echo_timer_running = 1;
2210 * LcpEchoTimeout - Timer expired on the LCP echo
2214 LcpEchoTimeout (void *arg)
2216 if (lcp_echo_timer_running != 0) {
2217 lcp_echo_timer_running = 0;
2218 LcpEchoCheck ((fsm *) arg);
2223 * LcpEchoReply - LCP has received a reply to the echo
2227 lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
2231 /* Check the magic number - don't count replies from ourselves. */
2233 dbglog("lcp: received short Echo-Reply, length %d", len);
2236 GETLONG(magic, inp);
2237 if (lcp_gotoptions[f->unit].neg_magicnumber
2238 && magic == lcp_gotoptions[f->unit].magicnumber) {
2239 warn("appear to have received our own echo-reply!");
2243 /* Reset the number of outstanding echo frames */
2244 lcp_echos_pending = 0;
2248 * LcpSendEchoRequest - Send an echo request frame to the peer
2252 LcpSendEchoRequest (fsm *f)
2254 u_int32_t lcp_magic;
2255 u_char pkt[4], *pktp;
2258 * Detect the failure of the peer at this point.
2260 if (lcp_echo_fails != 0) {
2261 if (lcp_echos_pending >= lcp_echo_fails) {
2263 lcp_echos_pending = 0;
2268 * If adaptive echos have been enabled, only send the echo request if
2269 * no traffic was received since the last one.
2271 if (lcp_echo_adaptive) {
2272 static unsigned int last_pkts_in = 0;
2273 struct pppd_stats cur_stats;
2275 if (get_ppp_stats(f->unit, &cur_stats) && cur_stats.pkts_in != last_pkts_in) {
2276 last_pkts_in = cur_stats.pkts_in;
2282 * Make and send the echo request frame.
2284 if (f->state == OPENED) {
2285 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
2287 PUTLONG(lcp_magic, pktp);
2288 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
2289 ++lcp_echos_pending;
2294 * lcp_echo_lowerup - Start the timer for the LCP frame
2298 lcp_echo_lowerup (int unit)
2300 fsm *f = &lcp_fsm[unit];
2302 /* Clear the parameters for generating echo frames */
2303 lcp_echos_pending = 0;
2304 lcp_echo_number = 0;
2305 lcp_echo_timer_running = 0;
2307 /* If a timeout interval is specified then start the timer */
2308 if (lcp_echo_interval != 0)
2313 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2317 lcp_echo_lowerdown (int unit)
2319 fsm *f = &lcp_fsm[unit];
2321 if (lcp_echo_timer_running != 0) {
2322 UNTIMEOUT (LcpEchoTimeout, f);
2323 lcp_echo_timer_running = 0;