1 /*****************************************************************************
2 * lcp.c - Network Link Control Protocol program file.
4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5 * portions Copyright (c) 1997 by Global Election Systems Inc.
7 * The authors hereby grant permission to use, copy, modify, distribute,
8 * and license this software and its documentation for any purpose, provided
9 * that existing copyright notices are retained in all copies and that this
10 * notice and the following disclaimer are included verbatim in any
11 * distributions. No written agreement, license, or royalty fee is required
12 * for any of the authorized uses.
14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ******************************************************************************
28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
30 * 97-12-01 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
32 *****************************************************************************/
35 * lcp.c - PPP Link Control Protocol.
37 * Copyright (c) 1989 Carnegie Mellon University.
38 * All rights reserved.
40 * Redistribution and use in source and binary forms are permitted
41 * provided that the above copyright notice and this paragraph are
42 * duplicated in all such forms and that any documentation,
43 * advertising materials, and other materials related to such
44 * distribution and use acknowledge that the software was developed
45 * by Carnegie Mellon University. The name of the
46 * University may not be used to endorse or promote products derived
47 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
50 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
62 /*************************/
63 /*** LOCAL DEFINITIONS ***/
64 /*************************/
66 * Length of each type of configuration option (in octets)
70 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
71 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
72 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
73 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
77 /***********************************/
78 /*** LOCAL FUNCTION DECLARATIONS ***/
79 /***********************************/
81 * Callbacks for fsm code. (CI = Configuration Information)
83 static void lcp_resetci (fsm*); /* Reset our CI */
84 static int lcp_cilen (fsm*); /* Return length of our CI */
85 static void lcp_addci (fsm*, u_char*, int*); /* Add our CI to pkt */
86 static int lcp_ackci (fsm*, u_char*, int);/* Peer ack'd our CI */
87 static int lcp_nakci (fsm*, u_char*, int);/* Peer nak'd our CI */
88 static int lcp_rejci (fsm*, u_char*, int);/* Peer rej'd our CI */
89 static int lcp_reqci (fsm*, u_char*, int*, int); /* Rcv peer CI */
90 static void lcp_up (fsm*); /* We're UP */
91 static void lcp_down (fsm*); /* We're DOWN */
92 static void lcp_starting (fsm*); /* We need lower layer up */
93 static void lcp_finished (fsm*); /* We need lower layer down */
94 static int lcp_extcode (fsm*, int, u_char, u_char*, int);
96 static void lcp_rprotrej (fsm*, u_char*, int);
99 * routines to send LCP echos to peer
101 static void lcp_echo_lowerup (int);
102 static void lcp_echo_lowerdown (int);
103 static void LcpEchoTimeout (void*);
104 static void lcp_received_echo_reply (fsm*, int, u_char*, int);
105 static void LcpSendEchoRequest (fsm*);
106 static void LcpLinkFailure (fsm*);
107 static void LcpEchoCheck (fsm*);
110 * Protocol entry points.
111 * Some of these are called directly.
113 static void lcp_input (int, u_char *, int);
114 static void lcp_protrej (int);
116 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
117 (x) == CONFNAK ? "NAK" : "REJ")
120 /******************************/
121 /*** PUBLIC DATA STRUCTURES ***/
122 /******************************/
124 LinkPhase lcp_phase[NUM_PPP]; /* Phase of link session (RFC 1661) */
125 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
126 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
127 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
128 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
129 ext_accm xmit_accm[NUM_PPP]; /* extended transmit ACCM */
133 /*****************************/
134 /*** LOCAL DATA STRUCTURES ***/
135 /*****************************/
136 static fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
137 static u_int lcp_echo_interval = LCP_ECHOINTERVAL; /* Interval between LCP echo-requests */
138 static u_int lcp_echo_fails = LCP_MAXECHOFAILS; /* Tolerance to unanswered echo-requests */
139 static u32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
140 static u32_t lcp_echo_number = 0; /* ID number of next echo frame */
141 static u32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
143 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
145 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
146 lcp_resetci, /* Reset our Configuration Information */
147 lcp_cilen, /* Length of our Configuration Information */
148 lcp_addci, /* Add our Configuration Information */
149 lcp_ackci, /* ACK our Configuration Information */
150 lcp_nakci, /* NAK our Configuration Information */
151 lcp_rejci, /* Reject our Configuration Information */
152 lcp_reqci, /* Request peer's Configuration Information */
153 lcp_up, /* Called when fsm reaches OPENED state */
154 lcp_down, /* Called when fsm leaves OPENED state */
155 lcp_starting, /* Called when we want the lower layer up */
156 lcp_finished, /* Called when we want the lower layer down */
157 NULL, /* Called when Protocol-Reject received */
158 NULL, /* Retransmission is necessary */
159 lcp_extcode, /* Called to handle LCP-specific codes */
160 "LCP" /* String name of protocol */
163 struct protent lcp_protent = {
185 int lcp_loopbackfail = DEFLOOPBACKFAIL;
189 /***********************************/
190 /*** PUBLIC FUNCTION DEFINITIONS ***/
191 /***********************************/
193 * lcp_init - Initialize LCP.
195 void lcp_init(int unit)
197 fsm *f = &lcp_fsm[unit];
198 lcp_options *wo = &lcp_wantoptions[unit];
199 lcp_options *ao = &lcp_allowoptions[unit];
202 f->protocol = PPP_LCP;
203 f->callbacks = &lcp_callbacks;
209 wo->restart = 0; /* Set to 1 in kernels or multi-line
212 wo->mru = PPP_DEFMRU;
213 wo->neg_asyncmap = 1;
214 wo->asyncmap = 0x00000000l; /* Assume don't need to escape any ctl chars. */
215 wo->neg_chap = 0; /* Set to 1 on server */
216 wo->neg_upap = 0; /* Set to 1 on server */
217 wo->chap_mdtype = CHAP_DIGEST_MD5;
218 wo->neg_magicnumber = 1;
219 wo->neg_pcompression = 1;
220 wo->neg_accompression = 1;
221 wo->neg_lqr = 0; /* no LQR implementation yet */
225 ao->mru = PPP_MAXMRU;
226 ao->neg_asyncmap = 1;
227 ao->asyncmap = 0x00000000l; /* Assume don't need to escape any ctl chars. */
228 ao->neg_chap = (CHAP_SUPPORT != 0);
229 ao->chap_mdtype = CHAP_DIGEST_MD5;
230 ao->neg_upap = (PAP_SUPPORT != 0);
231 ao->neg_magicnumber = 1;
232 ao->neg_pcompression = 1;
233 ao->neg_accompression = 1;
234 ao->neg_lqr = 0; /* no LQR implementation yet */
235 ao->neg_cbcp = (CBCP_SUPPORT != 0);
238 * Set transmit escape for the flag and escape characters plus anything
239 * set for the allowable options.
241 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
242 xmit_accm[unit][15] = 0x60;
243 xmit_accm[unit][0] = (u_char)(ao->asyncmap & 0xFF);
244 xmit_accm[unit][1] = (u_char)((ao->asyncmap >> 8) & 0xFF);
245 xmit_accm[unit][2] = (u_char)((ao->asyncmap >> 16) & 0xFF);
246 xmit_accm[unit][3] = (u_char)((ao->asyncmap >> 24) & 0xFF);
247 LCPDEBUG((LOG_INFO, "lcp_init: xmit_accm=%X %X %X %X\n",
251 xmit_accm[unit][3]));
253 lcp_phase[unit] = PHASE_INITIALIZE;
258 * lcp_open - LCP is allowed to come up.
260 void lcp_open(int unit)
262 fsm *f = &lcp_fsm[unit];
263 lcp_options *wo = &lcp_wantoptions[unit];
267 f->flags |= OPT_PASSIVE;
269 f->flags |= OPT_SILENT;
272 lcp_phase[unit] = PHASE_ESTABLISH;
277 * lcp_close - Take LCP down.
279 void lcp_close(int unit, char *reason)
281 fsm *f = &lcp_fsm[unit];
283 if (lcp_phase[unit] != PHASE_DEAD)
284 lcp_phase[unit] = PHASE_TERMINATE;
285 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
287 * This action is not strictly according to the FSM in RFC1548,
288 * but it does mean that the program terminates if you do an
289 * lcp_close() in passive/silent mode when a connection hasn't
296 fsm_close(&lcp_fsm[unit], reason);
301 * lcp_lowerup - The lower layer is up.
303 void lcp_lowerup(int unit)
305 lcp_options *wo = &lcp_wantoptions[unit];
308 * Don't use A/C or protocol compression on transmission,
309 * but accept A/C and protocol compressed packets
310 * if we are going to ask for A/C and protocol compression.
312 ppp_set_xaccm(unit, &xmit_accm[unit]);
313 ppp_send_config(unit, PPP_MRU, 0xffffffffl, 0, 0);
314 ppp_recv_config(unit, PPP_MRU, 0x00000000l,
315 wo->neg_pcompression, wo->neg_accompression);
316 peer_mru[unit] = PPP_MRU;
317 lcp_allowoptions[unit].asyncmap
318 = (u_long)xmit_accm[unit][0]
319 | ((u_long)xmit_accm[unit][1] << 8)
320 | ((u_long)xmit_accm[unit][2] << 16)
321 | ((u_long)xmit_accm[unit][3] << 24);
322 LCPDEBUG((LOG_INFO, "lcp_lowerup: asyncmap=%X %X %X %X\n",
326 xmit_accm[unit][0]));
328 fsm_lowerup(&lcp_fsm[unit]);
333 * lcp_lowerdown - The lower layer is down.
335 void lcp_lowerdown(int unit)
337 fsm_lowerdown(&lcp_fsm[unit]);
341 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
343 void lcp_sprotrej(int unit, u_char *p, int len)
346 * Send back the protocol and the information field of the
347 * rejected packet. We only get here if LCP is in the OPENED state.
350 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
356 /**********************************/
357 /*** LOCAL FUNCTION DEFINITIONS ***/
358 /**********************************/
360 * lcp_input - Input LCP packet.
362 static void lcp_input(int unit, u_char *p, int len)
364 fsm *f = &lcp_fsm[unit];
366 fsm_input(f, p, len);
371 * lcp_extcode - Handle a LCP-specific code.
373 static int lcp_extcode(fsm *f, int code, u_char id, u_char *inp, int len)
379 lcp_rprotrej(f, inp, len);
383 if (f->state != OPENED)
385 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d\n", id));
387 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
388 fsm_sdata(f, ECHOREP, id, inp, len);
392 lcp_received_echo_reply(f, id, inp, len);
406 * lcp_rprotrej - Receive an Protocol-Reject.
408 * Figure out which protocol is rejected and inform it.
410 static void lcp_rprotrej(fsm *f, u_char *inp, int len)
413 struct protent *protp;
416 if (len < sizeof (u_short)) {
418 "lcp_rprotrej: Rcvd short Protocol-Reject packet!\n"));
425 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!\n",
429 * Protocol-Reject packets received in any state other than the LCP
430 * OPENED state SHOULD be silently discarded.
432 if( f->state != OPENED ){
433 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d\n",
439 * Upcall the proper Protocol-Reject routine.
441 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i)
442 if (protp->protocol == prot && protp->enabled_flag) {
443 (*protp->protrej)(f->unit);
447 LCPDEBUG((LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x\n",
453 * lcp_protrej - A Protocol-Reject was received.
455 static void lcp_protrej(int unit)
461 LCPDEBUG((LOG_WARNING,
462 "lcp_protrej: Received Protocol-Reject for LCP!\n"));
463 fsm_protreject(&lcp_fsm[unit]);
468 * lcp_resetci - Reset our CI.
470 static void lcp_resetci(fsm *f)
472 lcp_wantoptions[f->unit].magicnumber = magic();
473 lcp_wantoptions[f->unit].numloops = 0;
474 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
475 peer_mru[f->unit] = PPP_MRU;
481 * lcp_cilen - Return length of our CI.
483 static int lcp_cilen(fsm *f)
485 lcp_options *go = &lcp_gotoptions[f->unit];
487 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
488 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
489 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
490 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
491 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
492 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
494 * NB: we only ask for one of CHAP and UPAP, even if we will
497 return (LENCISHORT(go->neg_mru && go->mru != PPP_DEFMRU) +
498 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) +
499 LENCICHAP(go->neg_chap) +
500 LENCISHORT(!go->neg_chap && go->neg_upap) +
501 LENCILQR(go->neg_lqr) +
502 LENCICBCP(go->neg_cbcp) +
503 LENCILONG(go->neg_magicnumber) +
504 LENCIVOID(go->neg_pcompression) +
505 LENCIVOID(go->neg_accompression));
510 * lcp_addci - Add our desired CIs to a packet.
512 static void lcp_addci(fsm *f, u_char *ucp, int *lenp)
514 lcp_options *go = &lcp_gotoptions[f->unit];
515 u_char *start_ucp = ucp;
517 #define ADDCIVOID(opt, neg) \
519 LCPDEBUG((LOG_INFO, "lcp_addci: opt=%d\n", opt)); \
521 PUTCHAR(CILEN_VOID, ucp); \
523 #define ADDCISHORT(opt, neg, val) \
525 LCPDEBUG((LOG_INFO, "lcp_addci: INT opt=%d %X\n", opt, val)); \
527 PUTCHAR(CILEN_SHORT, ucp); \
528 PUTSHORT(val, ucp); \
530 #define ADDCICHAP(opt, neg, val, digest) \
532 LCPDEBUG((LOG_INFO, "lcp_addci: CHAP opt=%d %X\n", opt, val)); \
534 PUTCHAR(CILEN_CHAP, ucp); \
535 PUTSHORT(val, ucp); \
536 PUTCHAR(digest, ucp); \
538 #define ADDCILONG(opt, neg, val) \
540 LCPDEBUG((LOG_INFO, "lcp_addci: L opt=%d %lX\n", opt, val)); \
542 PUTCHAR(CILEN_LONG, ucp); \
545 #define ADDCILQR(opt, neg, val) \
547 LCPDEBUG((LOG_INFO, "lcp_addci: LQR opt=%d %lX\n", opt, val)); \
549 PUTCHAR(CILEN_LQR, ucp); \
550 PUTSHORT(PPP_LQR, ucp); \
553 #define ADDCICHAR(opt, neg, val) \
555 LCPDEBUG((LOG_INFO, "lcp_addci: CHAR opt=%d %X '%z'\n", opt, val, val)); \
557 PUTCHAR(CILEN_CHAR, ucp); \
561 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
562 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl,
564 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
565 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
566 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
567 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
568 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
569 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
570 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
572 if (ucp - start_ucp != *lenp) {
573 /* this should never happen, because peer_mtu should be 1500 */
574 LCPDEBUG((LOG_ERR, "Bug in lcp_addci: wrong length\n"));
580 * lcp_ackci - Ack our CIs.
581 * This should not modify any state if the Ack is bad.
587 static int lcp_ackci(fsm *f, u_char *p, int len)
589 lcp_options *go = &lcp_gotoptions[f->unit];
590 u_char cilen, citype, cichar;
595 * CIs must be in exactly the same order that we sent.
596 * Check packet length and CI length at each step.
597 * If we find any deviations, then this packet is bad.
599 #define ACKCIVOID(opt, neg) \
601 if ((len -= CILEN_VOID) < 0) \
603 GETCHAR(citype, p); \
605 if (cilen != CILEN_VOID || \
609 #define ACKCISHORT(opt, neg, val) \
611 if ((len -= CILEN_SHORT) < 0) \
613 GETCHAR(citype, p); \
615 if (cilen != CILEN_SHORT || \
618 GETSHORT(cishort, p); \
619 if (cishort != val) \
622 #define ACKCICHAR(opt, neg, val) \
624 if ((len -= CILEN_CHAR) < 0) \
626 GETCHAR(citype, p); \
628 if (cilen != CILEN_CHAR || \
631 GETCHAR(cichar, p); \
635 #define ACKCICHAP(opt, neg, val, digest) \
637 if ((len -= CILEN_CHAP) < 0) \
639 GETCHAR(citype, p); \
641 if (cilen != CILEN_CHAP || \
644 GETSHORT(cishort, p); \
645 if (cishort != val) \
647 GETCHAR(cichar, p); \
648 if (cichar != digest) \
651 #define ACKCILONG(opt, neg, val) \
653 if ((len -= CILEN_LONG) < 0) \
655 GETCHAR(citype, p); \
657 if (cilen != CILEN_LONG || \
660 GETLONG(cilong, p); \
664 #define ACKCILQR(opt, neg, val) \
666 if ((len -= CILEN_LQR) < 0) \
668 GETCHAR(citype, p); \
670 if (cilen != CILEN_LQR || \
673 GETSHORT(cishort, p); \
674 if (cishort != PPP_LQR) \
676 GETLONG(cilong, p); \
681 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
682 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl,
684 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
685 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
686 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
687 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
688 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
689 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
690 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
693 * If there are any remaining CIs, then this packet is bad.
697 LCPDEBUG((LOG_INFO, "lcp_acki: Ack\n"));
700 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!\n"));
706 * lcp_nakci - Peer has sent a NAK for some of our CIs.
707 * This should not modify any state if the Nak is bad
708 * or if LCP is in the OPENED state.
714 static int lcp_nakci(fsm *f, u_char *p, int len)
716 lcp_options *go = &lcp_gotoptions[f->unit];
717 lcp_options *wo = &lcp_wantoptions[f->unit];
718 u_char citype, cichar, *next;
721 lcp_options no; /* options we've seen Naks for */
722 lcp_options try; /* options to request next time */
726 BZERO(&no, sizeof(no));
730 * Any Nak'd CIs must be in exactly the same order that we sent.
731 * Check packet length and CI length at each step.
732 * If we find any deviations, then this packet is bad.
734 #define NAKCIVOID(opt, neg, code) \
736 len >= CILEN_VOID && \
737 p[1] == CILEN_VOID && \
740 INCPTR(CILEN_VOID, p); \
744 #define NAKCICHAP(opt, neg, code) \
746 len >= CILEN_CHAP && \
747 p[1] == CILEN_CHAP && \
751 GETSHORT(cishort, p); \
752 GETCHAR(cichar, p); \
756 #define NAKCICHAR(opt, neg, code) \
758 len >= CILEN_CHAR && \
759 p[1] == CILEN_CHAR && \
763 GETCHAR(cichar, p); \
767 #define NAKCISHORT(opt, neg, code) \
769 len >= CILEN_SHORT && \
770 p[1] == CILEN_SHORT && \
772 len -= CILEN_SHORT; \
774 GETSHORT(cishort, p); \
778 #define NAKCILONG(opt, neg, code) \
780 len >= CILEN_LONG && \
781 p[1] == CILEN_LONG && \
785 GETLONG(cilong, p); \
789 #define NAKCILQR(opt, neg, code) \
791 len >= CILEN_LQR && \
792 p[1] == CILEN_LQR && \
796 GETSHORT(cishort, p); \
797 GETLONG(cilong, p); \
803 * We don't care if they want to send us smaller packets than
804 * we want. Therefore, accept any MRU less than what we asked for,
805 * but then ignore the new value when setting the MRU in the kernel.
806 * If they send us a bigger MRU than what we asked, accept it, up to
807 * the limit of the default MRU we'd get if we didn't negotiate.
809 if (go->neg_mru && go->mru != PPP_DEFMRU) {
810 NAKCISHORT(CI_MRU, neg_mru,
811 if (cishort <= wo->mru || cishort < PPP_DEFMRU)
817 * Add any characters they want to our (receive-side) asyncmap.
819 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) {
820 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
821 try.asyncmap = go->asyncmap | cilong;
826 * If they've nak'd our authentication-protocol, check whether
827 * they are proposing a different protocol, or a different
828 * hash algorithm for CHAP.
830 if ((go->neg_chap || go->neg_upap)
831 && len >= CILEN_SHORT
832 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
835 no.neg_chap = go->neg_chap;
836 no.neg_upap = go->neg_upap;
838 GETSHORT(cishort, p);
839 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
841 * If we were asking for CHAP, they obviously don't want to do it.
842 * If we weren't asking for CHAP, then we were asking for PAP,
843 * in which case this Nak is bad.
849 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
853 * We were asking for CHAP/MD5; they must want a different
854 * algorithm. If they can't do MD5, we'll have to stop
857 if (cichar != go->chap_mdtype)
861 * Stop asking for PAP if we were asking for it.
868 * We don't recognize what they're suggesting.
869 * Stop asking for what we were asking for.
875 p += cilen - CILEN_SHORT;
880 * If they can't cope with our link quality protocol, we'll have
881 * to stop asking for LQR. We haven't got any other protocol.
882 * If they Nak the reporting period, take their value XXX ?
884 NAKCILQR(CI_QUALITY, neg_lqr,
885 if (cishort != PPP_LQR)
888 try.lqr_period = cilong;
892 * Only implementing CBCP...not the rest of the callback options
894 NAKCICHAR(CI_CALLBACK, neg_cbcp,
899 * Check for a looped-back line.
901 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
902 try.magicnumber = magic();
907 * Peer shouldn't send Nak for protocol compression or
908 * address/control compression requests; they should send
909 * a Reject instead. If they send a Nak, treat it as a Reject.
911 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
912 try.neg_pcompression = 0;
914 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
915 try.neg_accompression = 0;
919 * There may be remaining CIs, if the peer is requesting negotiation
920 * on an option that we didn't include in our request packet.
921 * If we see an option that we requested, or one we've already seen
922 * in this packet, then this packet is bad.
923 * If we wanted to respond by starting to negotiate on the requested
924 * option(s), we could, but we don't, because except for the
925 * authentication type and quality protocol, if we are not negotiating
926 * an option, it is because we were told not to.
927 * For the authentication type, the Nak from the peer means
928 * `let me authenticate myself with you' which is a bit pointless.
929 * For the quality protocol, the Nak means `ask me to send you quality
930 * reports', but if we didn't ask for them, we don't want them.
931 * An option we don't recognize represents the peer asking to
932 * negotiate some option we don't support, so ignore it.
934 while (len > CILEN_VOID) {
937 if (cilen < CILEN_VOID || (len -= cilen) < 0)
939 next = p + cilen - 2;
943 if ((go->neg_mru && go->mru != PPP_DEFMRU)
944 || no.neg_mru || cilen != CILEN_SHORT)
946 GETSHORT(cishort, p);
947 if (cishort < PPP_DEFMRU)
951 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl)
952 || no.neg_asyncmap || cilen != CILEN_LONG)
956 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
960 if (go->neg_magicnumber || no.neg_magicnumber ||
964 case CI_PCOMPRESSION:
965 if (go->neg_pcompression || no.neg_pcompression
966 || cilen != CILEN_VOID)
969 case CI_ACCOMPRESSION:
970 if (go->neg_accompression || no.neg_accompression
971 || cilen != CILEN_VOID)
975 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
982 /* If there is still anything left, this packet is bad. */
987 * OK, the Nak is good. Now we can update state.
989 if (f->state != OPENED) {
991 if (++try.numloops >= lcp_loopbackfail) {
992 LCPDEBUG((LOG_NOTICE, "Serial line is looped back.\n"));
993 lcp_close(f->unit, "Loopback detected");
1004 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!\n"));
1010 * lcp_rejci - Peer has Rejected some of our CIs.
1011 * This should not modify any state if the Reject is bad
1012 * or if LCP is in the OPENED state.
1015 * 0 - Reject was bad.
1016 * 1 - Reject was good.
1018 static int lcp_rejci(fsm *f, u_char *p, int len)
1020 lcp_options *go = &lcp_gotoptions[f->unit];
1024 lcp_options try; /* options to request next time */
1029 * Any Rejected CIs must be in exactly the same order that we sent.
1030 * Check packet length and CI length at each step.
1031 * If we find any deviations, then this packet is bad.
1033 #define REJCIVOID(opt, neg) \
1035 len >= CILEN_VOID && \
1036 p[1] == CILEN_VOID && \
1038 len -= CILEN_VOID; \
1039 INCPTR(CILEN_VOID, p); \
1041 LCPDEBUG((LOG_INFO, "lcp_rejci: void opt %d rejected\n", opt)); \
1043 #define REJCISHORT(opt, neg, val) \
1045 len >= CILEN_SHORT && \
1046 p[1] == CILEN_SHORT && \
1048 len -= CILEN_SHORT; \
1050 GETSHORT(cishort, p); \
1051 /* Check rejected value. */ \
1052 if (cishort != val) \
1055 LCPDEBUG((LOG_INFO,"lcp_rejci: short opt %d rejected\n", opt)); \
1057 #define REJCICHAP(opt, neg, val, digest) \
1059 len >= CILEN_CHAP && \
1060 p[1] == CILEN_CHAP && \
1062 len -= CILEN_CHAP; \
1064 GETSHORT(cishort, p); \
1065 GETCHAR(cichar, p); \
1066 /* Check rejected value. */ \
1067 if (cishort != val || cichar != digest) \
1071 LCPDEBUG((LOG_INFO,"lcp_rejci: chap opt %d rejected\n", opt)); \
1073 #define REJCILONG(opt, neg, val) \
1075 len >= CILEN_LONG && \
1076 p[1] == CILEN_LONG && \
1078 len -= CILEN_LONG; \
1080 GETLONG(cilong, p); \
1081 /* Check rejected value. */ \
1082 if (cilong != val) \
1085 LCPDEBUG((LOG_INFO,"lcp_rejci: long opt %d rejected\n", opt)); \
1087 #define REJCILQR(opt, neg, val) \
1089 len >= CILEN_LQR && \
1090 p[1] == CILEN_LQR && \
1094 GETSHORT(cishort, p); \
1095 GETLONG(cilong, p); \
1096 /* Check rejected value. */ \
1097 if (cishort != PPP_LQR || cilong != val) \
1100 LCPDEBUG((LOG_INFO,"lcp_rejci: LQR opt %d rejected\n", opt)); \
1102 #define REJCICBCP(opt, neg, val) \
1104 len >= CILEN_CBCP && \
1105 p[1] == CILEN_CBCP && \
1107 len -= CILEN_CBCP; \
1109 GETCHAR(cichar, p); \
1110 /* Check rejected value. */ \
1111 if (cichar != val) \
1114 LCPDEBUG((LOG_INFO,"lcp_rejci: Callback opt %d rejected\n", opt)); \
1117 REJCISHORT(CI_MRU, neg_mru, go->mru);
1118 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1119 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1120 if (!go->neg_chap) {
1121 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1123 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1124 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1125 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1126 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1127 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1130 * If there are any remaining CIs, then this packet is bad.
1135 * Now we can update state.
1137 if (f->state != OPENED)
1142 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!\n"));
1148 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1150 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1151 * appropriately. If reject_if_disagree is non-zero, doesn't return
1152 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1154 static int lcp_reqci(fsm *f,
1155 u_char *inp, /* Requested CIs */
1156 int *lenp, /* Length of requested CIs */
1157 int reject_if_disagree)
1159 lcp_options *go = &lcp_gotoptions[f->unit];
1160 lcp_options *ho = &lcp_hisoptions[f->unit];
1161 lcp_options *ao = &lcp_allowoptions[f->unit];
1162 u_char *cip, *next; /* Pointer to current and next CIs */
1163 int cilen, citype, cichar; /* Parsed len, type, char value */
1164 u_short cishort; /* Parsed short value */
1165 u32_t cilong; /* Parse long value */
1166 int rc = CONFACK; /* Final packet return code */
1167 int orc; /* Individual option return code */
1168 u_char *p; /* Pointer to next char to parse */
1169 u_char *rejp; /* Pointer to next char in reject frame */
1170 u_char *nakp; /* Pointer to next char in Nak frame */
1171 int l = *lenp; /* Length left */
1178 * Reset all his options.
1180 BZERO(ho, sizeof(*ho));
1183 * Process all his options.
1189 orc = CONFACK; /* Assume success */
1190 cip = p = next; /* Remember begining of CI */
1191 if (l < 2 || /* Not enough data for CI header or */
1192 p[1] < 2 || /* CI length too small or */
1193 p[1] > l) { /* CI length too big? */
1194 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!\n"));
1195 orc = CONFREJ; /* Reject bad CI */
1196 cilen = l; /* Reject till end of packet */
1197 l = 0; /* Don't loop again */
1201 GETCHAR(citype, p); /* Parse CI type */
1202 GETCHAR(cilen, p); /* Parse CI length */
1203 l -= cilen; /* Adjust remaining length */
1204 next += cilen; /* Step to next CI */
1206 switch (citype) { /* Check CI type */
1208 if (!ao->neg_mru) { /* Allow option? */
1209 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - not allowed\n"));
1210 orc = CONFREJ; /* Reject CI */
1212 } else if (cilen != CILEN_SHORT) { /* Check CI length */
1213 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - bad length\n"));
1214 orc = CONFREJ; /* Reject CI */
1217 GETSHORT(cishort, p); /* Parse MRU */
1220 * He must be able to receive at least our minimum.
1221 * No need to check a maximum. If he sends a large number,
1222 * we'll just ignore it.
1224 if (cishort < PPP_MINMRU) {
1225 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak - MRU too small\n"));
1226 orc = CONFNAK; /* Nak CI */
1227 PUTCHAR(CI_MRU, nakp);
1228 PUTCHAR(CILEN_SHORT, nakp);
1229 PUTSHORT(PPP_MINMRU, nakp); /* Give him a hint */
1232 ho->neg_mru = 1; /* Remember he sent MRU */
1233 ho->mru = cishort; /* And remember value */
1235 sprintf(&traceBuf[traceNdx], " MRU %d", cishort);
1236 traceNdx = strlen(traceBuf);
1241 if (!ao->neg_asyncmap) {
1242 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP not allowed\n"));
1245 } else if (cilen != CILEN_LONG) {
1246 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP bad length\n"));
1253 * Asyncmap must have set at least the bits
1254 * which are set in lcp_allowoptions[unit].asyncmap.
1256 if ((ao->asyncmap & ~cilong) != 0) {
1257 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak ASYNCMAP %lX missing %lX\n",
1258 cilong, ao->asyncmap));
1260 PUTCHAR(CI_ASYNCMAP, nakp);
1261 PUTCHAR(CILEN_LONG, nakp);
1262 PUTLONG(ao->asyncmap | cilong, nakp);
1265 ho->neg_asyncmap = 1;
1266 ho->asyncmap = cilong;
1268 sprintf(&traceBuf[traceNdx], " ASYNCMAP=%lX", cilong);
1269 traceNdx = strlen(traceBuf);
1274 if (cilen < CILEN_SHORT) {
1275 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE missing arg\n"));
1278 } else if (!(ao->neg_upap || ao->neg_chap)) {
1280 * Reject the option if we're not willing to authenticate.
1282 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE not allowed\n"));
1286 GETSHORT(cishort, p);
1289 * Authtype must be UPAP or CHAP.
1291 * Note: if both ao->neg_upap and ao->neg_chap are set,
1292 * and the peer sends a Configure-Request with two
1293 * authenticate-protocol requests, one for CHAP and one
1294 * for UPAP, then we will reject the second request.
1295 * Whether we end up doing CHAP or UPAP depends then on
1296 * the ordering of the CIs in the peer's Configure-Request.
1299 if (cishort == PPP_PAP) {
1300 if (ho->neg_chap) { /* we've already accepted CHAP */
1301 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP already accepted\n"));
1304 } else if (cilen != CILEN_SHORT) {
1305 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP bad len\n"));
1309 if (!ao->neg_upap) { /* we don't want to do PAP */
1310 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE PAP not allowed\n"));
1311 orc = CONFNAK; /* NAK it and suggest CHAP */
1312 PUTCHAR(CI_AUTHTYPE, nakp);
1313 PUTCHAR(CILEN_CHAP, nakp);
1314 PUTSHORT(PPP_CHAP, nakp);
1315 PUTCHAR(ao->chap_mdtype, nakp);
1320 sprintf(&traceBuf[traceNdx], " PAP (%X)", cishort);
1321 traceNdx = strlen(traceBuf);
1325 if (cishort == PPP_CHAP) {
1326 if (ho->neg_upap) { /* we've already accepted PAP */
1327 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP accepted PAP\n"));
1330 } else if (cilen != CILEN_CHAP) {
1331 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP bad len\n"));
1335 if (!ao->neg_chap) { /* we don't want to do CHAP */
1336 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP not allowed\n"));
1337 orc = CONFNAK; /* NAK it and suggest PAP */
1338 PUTCHAR(CI_AUTHTYPE, nakp);
1339 PUTCHAR(CILEN_SHORT, nakp);
1340 PUTSHORT(PPP_PAP, nakp);
1343 GETCHAR(cichar, p); /* get digest type*/
1344 if (cichar != CHAP_DIGEST_MD5
1346 && cichar != CHAP_MICROSOFT
1349 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP digest=%d\n", cichar));
1351 PUTCHAR(CI_AUTHTYPE, nakp);
1352 PUTCHAR(CILEN_CHAP, nakp);
1353 PUTSHORT(PPP_CHAP, nakp);
1354 PUTCHAR(ao->chap_mdtype, nakp);
1358 sprintf(&traceBuf[traceNdx], " CHAP %X,%d", cishort, cichar);
1359 traceNdx = strlen(traceBuf);
1361 ho->chap_mdtype = cichar; /* save md type */
1367 * We don't recognize the protocol they're asking for.
1368 * Nak it with something we're willing to do.
1369 * (At this point we know ao->neg_upap || ao->neg_chap.)
1372 PUTCHAR(CI_AUTHTYPE, nakp);
1374 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req CHAP\n", cishort));
1375 PUTCHAR(CILEN_CHAP, nakp);
1376 PUTSHORT(PPP_CHAP, nakp);
1377 PUTCHAR(ao->chap_mdtype, nakp);
1380 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req PAP\n", cishort));
1381 PUTCHAR(CILEN_SHORT, nakp);
1382 PUTSHORT(PPP_PAP, nakp);
1387 GETSHORT(cishort, p);
1390 sprintf(&traceBuf[traceNdx], " QUALITY (%x %x)", cishort, (unsigned int) cilong);
1391 traceNdx = strlen(traceBuf);
1395 cilen != CILEN_LQR) {
1401 * Check the protocol and the reporting period.
1402 * XXX When should we Nak this, and what with?
1404 if (cishort != PPP_LQR) {
1406 PUTCHAR(CI_QUALITY, nakp);
1407 PUTCHAR(CILEN_LQR, nakp);
1408 PUTSHORT(PPP_LQR, nakp);
1409 PUTLONG(ao->lqr_period, nakp);
1414 case CI_MAGICNUMBER:
1415 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1416 cilen != CILEN_LONG) {
1422 sprintf(&traceBuf[traceNdx], " MAGICNUMBER (%lX)", cilong);
1423 traceNdx = strlen(traceBuf);
1427 * He must have a different magic number.
1429 if (go->neg_magicnumber &&
1430 cilong == go->magicnumber) {
1431 cilong = magic(); /* Don't put magic() inside macro! */
1433 PUTCHAR(CI_MAGICNUMBER, nakp);
1434 PUTCHAR(CILEN_LONG, nakp);
1435 PUTLONG(cilong, nakp);
1438 ho->neg_magicnumber = 1;
1439 ho->magicnumber = cilong;
1443 case CI_PCOMPRESSION:
1445 sprintf(&traceBuf[traceNdx], " PCOMPRESSION");
1446 traceNdx = strlen(traceBuf);
1448 if (!ao->neg_pcompression ||
1449 cilen != CILEN_VOID) {
1453 ho->neg_pcompression = 1;
1456 case CI_ACCOMPRESSION:
1458 sprintf(&traceBuf[traceNdx], " ACCOMPRESSION");
1459 traceNdx = strlen(traceBuf);
1461 if (!ao->neg_accompression ||
1462 cilen != CILEN_VOID) {
1466 ho->neg_accompression = 1;
1471 sprintf(&traceBuf[traceNdx], " CI_MRRU");
1472 traceNdx = strlen(traceBuf);
1479 sprintf(&traceBuf[traceNdx], " CI_SSNHF");
1480 traceNdx = strlen(traceBuf);
1487 sprintf(&traceBuf[traceNdx], " CI_EPDISC");
1488 traceNdx = strlen(traceBuf);
1495 sprintf(&traceBuf[traceNdx], " unknown %d", citype);
1496 traceNdx = strlen(traceBuf);
1504 if (traceNdx >= 80 - 32) {
1505 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd%s\n", traceBuf));
1509 if (orc == CONFACK && /* Good CI */
1510 rc != CONFACK) /* but prior CI wasnt? */
1511 continue; /* Don't send this one */
1513 if (orc == CONFNAK) { /* Nak this CI? */
1514 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1515 && citype != CI_MAGICNUMBER) {
1516 orc = CONFREJ; /* Get tough if so */
1519 if (rc == CONFREJ) /* Rejecting prior CI? */
1520 continue; /* Don't send this one */
1524 if (orc == CONFREJ) { /* Reject this CI */
1526 if (cip != rejp) /* Need to move rejected CI? */
1527 BCOPY(cip, rejp, cilen); /* Move it */
1528 INCPTR(cilen, rejp); /* Update output pointer */
1533 * If we wanted to send additional NAKs (for unsent CIs), the
1534 * code would go here. The extra NAKs would go at *nakp.
1535 * At present there are no cases where we want to ask the
1536 * peer to negotiate an option.
1541 *lenp = (int)(next - inp);
1545 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1547 *lenp = (int)(nakp - nak_buffer);
1548 BCOPY(nak_buffer, inp, *lenp);
1551 *lenp = (int)(rejp - inp);
1557 LCPDEBUG((LOG_INFO, "lcp_reqci: %s\n", traceBuf));
1560 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.\n", CODENAME(rc)));
1561 return (rc); /* Return final code */
1566 * lcp_up - LCP has come UP.
1568 static void lcp_up(fsm *f)
1570 lcp_options *wo = &lcp_wantoptions[f->unit];
1571 lcp_options *ho = &lcp_hisoptions[f->unit];
1572 lcp_options *go = &lcp_gotoptions[f->unit];
1573 lcp_options *ao = &lcp_allowoptions[f->unit];
1575 if (!go->neg_magicnumber)
1576 go->magicnumber = 0;
1577 if (!ho->neg_magicnumber)
1578 ho->magicnumber = 0;
1581 * Set our MTU to the smaller of the MTU we wanted and
1582 * the MRU our peer wanted. If we negotiated an MRU,
1583 * set our MRU to the larger of value we wanted and
1584 * the value we got in the negotiation.
1586 ppp_send_config(f->unit, LWIP_MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1587 (ho->neg_asyncmap? ho->asyncmap: 0xffffffffl),
1588 ho->neg_pcompression, ho->neg_accompression);
1590 * If the asyncmap hasn't been negotiated, we really should
1591 * set the receive asyncmap to ffffffff, but we set it to 0
1592 * for backwards contemptibility.
1594 ppp_recv_config(f->unit, (go->neg_mru? LWIP_MAX(wo->mru, go->mru): PPP_MRU),
1595 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1596 go->neg_pcompression, go->neg_accompression);
1599 peer_mru[f->unit] = ho->mru;
1601 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1603 link_established(f->unit);
1608 * lcp_down - LCP has gone DOWN.
1610 * Alert other protocols.
1612 static void lcp_down(fsm *f)
1614 lcp_options *go = &lcp_gotoptions[f->unit];
1616 lcp_echo_lowerdown(f->unit);
1620 ppp_send_config(f->unit, PPP_MRU, 0xffffffffl, 0, 0);
1621 ppp_recv_config(f->unit, PPP_MRU,
1622 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1623 go->neg_pcompression, go->neg_accompression);
1624 peer_mru[f->unit] = PPP_MRU;
1629 * lcp_starting - LCP needs the lower layer up.
1631 static void lcp_starting(fsm *f)
1633 link_required(f->unit);
1638 * lcp_finished - LCP has finished with the lower layer.
1640 static void lcp_finished(fsm *f)
1642 link_terminated(f->unit);
1648 * print_string - print a readable representation of a string using
1651 static void print_string(
1654 void (*printer) (void *, char *, ...),
1661 for (; len > 0; --len) {
1663 if (' ' <= c && c <= '~') {
1664 if (c == '\\' || c == '"')
1666 printer(arg, "%c", c);
1670 printer(arg, "\\n");
1673 printer(arg, "\\r");
1676 printer(arg, "\\t");
1679 printer(arg, "\\%.3o", c);
1688 * lcp_printpkt - print the contents of an LCP packet.
1690 static char *lcp_codenames[] = {
1691 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1692 "TermReq", "TermAck", "CodeRej", "ProtRej",
1693 "EchoReq", "EchoRep", "DiscReq"
1696 static int lcp_printpkt(
1699 void (*printer) (void *, char *, ...),
1703 int code, id, len, olen;
1704 u_char *pstart, *optend;
1708 if (plen < HEADERLEN)
1714 if (len < HEADERLEN || len > plen)
1717 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1718 printer(arg, " %s", lcp_codenames[code-1]);
1720 printer(arg, " code=0x%x", code);
1721 printer(arg, " id=0x%x", id);
1728 /* print option list */
1733 if (olen < 2 || olen > len) {
1741 if (olen == CILEN_SHORT) {
1743 GETSHORT(cishort, p);
1744 printer(arg, "mru %d", cishort);
1748 if (olen == CILEN_LONG) {
1751 printer(arg, "asyncmap 0x%lx", cilong);
1755 if (olen >= CILEN_SHORT) {
1757 printer(arg, "auth ");
1758 GETSHORT(cishort, p);
1761 printer(arg, "pap");
1764 printer(arg, "chap");
1767 printer(arg, "0x%x", cishort);
1772 if (olen >= CILEN_SHORT) {
1774 printer(arg, "quality ");
1775 GETSHORT(cishort, p);
1778 printer(arg, "lqr");
1781 printer(arg, "0x%x", cishort);
1786 if (olen >= CILEN_CHAR) {
1788 printer(arg, "callback ");
1789 GETSHORT(cishort, p);
1792 printer(arg, "CBCP");
1795 printer(arg, "0x%x", cishort);
1799 case CI_MAGICNUMBER:
1800 if (olen == CILEN_LONG) {
1803 printer(arg, "magic 0x%x", cilong);
1806 case CI_PCOMPRESSION:
1807 if (olen == CILEN_VOID) {
1809 printer(arg, "pcomp");
1812 case CI_ACCOMPRESSION:
1813 if (olen == CILEN_VOID) {
1815 printer(arg, "accomp");
1819 while (p < optend) {
1821 printer(arg, " %.2x", code);
1829 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1831 print_string((char*)p, len, printer, arg);
1842 printer(arg, " magic=0x%x", cilong);
1849 /* print the rest of the bytes in the packet */
1850 for (; len > 0; --len) {
1852 printer(arg, " %.2x", code);
1855 return (int)(p - pstart);
1860 * Time to shut down the link because there is nothing out there.
1863 static void LcpLinkFailure (fsm *f)
1865 if (f->state == OPENED) {
1866 LCPDEBUG((LOG_INFO, "No response to %d echo-requests\n", lcp_echos_pending));
1867 LCPDEBUG((LOG_NOTICE, "Serial link appears to be disconnected.\n"));
1868 lcp_close(f->unit, "Peer not responding");
1873 * Timer expired for the LCP echo requests from this process.
1876 static void LcpEchoCheck (fsm *f)
1878 LcpSendEchoRequest (f);
1881 * Start the timer for the next interval.
1883 LWIP_ASSERT("lcp_echo_timer_running == 0", lcp_echo_timer_running == 0);
1885 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
1886 lcp_echo_timer_running = 1;
1890 * LcpEchoTimeout - Timer expired on the LCP echo
1893 static void LcpEchoTimeout (void *arg)
1895 if (lcp_echo_timer_running != 0) {
1896 lcp_echo_timer_running = 0;
1897 LcpEchoCheck ((fsm *) arg);
1902 * LcpEchoReply - LCP has received a reply to the echo
1904 static void lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
1910 /* Check the magic number - don't count replies from ourselves. */
1912 LCPDEBUG((LOG_WARNING, "lcp: received short Echo-Reply, length %d\n", len));
1915 GETLONG(magic, inp);
1916 if (lcp_gotoptions[f->unit].neg_magicnumber
1917 && magic == lcp_gotoptions[f->unit].magicnumber) {
1918 LCPDEBUG((LOG_WARNING, "appear to have received our own echo-reply!\n"));
1922 /* Reset the number of outstanding echo frames */
1923 lcp_echos_pending = 0;
1927 * LcpSendEchoRequest - Send an echo request frame to the peer
1930 static void LcpSendEchoRequest (fsm *f)
1933 u_char pkt[4], *pktp;
1936 * Detect the failure of the peer at this point.
1938 if (lcp_echo_fails != 0) {
1939 if (lcp_echos_pending++ >= lcp_echo_fails) {
1941 lcp_echos_pending = 0;
1946 * Make and send the echo request frame.
1948 if (f->state == OPENED) {
1949 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1951 PUTLONG(lcp_magic, pktp);
1952 fsm_sdata(f, ECHOREQ, (u_char)(lcp_echo_number++ & 0xFF), pkt, (int)(pktp - pkt));
1957 * lcp_echo_lowerup - Start the timer for the LCP frame
1960 static void lcp_echo_lowerup (int unit)
1962 fsm *f = &lcp_fsm[unit];
1964 /* Clear the parameters for generating echo frames */
1965 lcp_echos_pending = 0;
1966 lcp_echo_number = 0;
1967 lcp_echo_timer_running = 0;
1969 /* If a timeout interval is specified then start the timer */
1970 if (lcp_echo_interval != 0)
1975 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1978 static void lcp_echo_lowerdown (int unit)
1980 fsm *f = &lcp_fsm[unit];
1982 if (lcp_echo_timer_running != 0) {
1983 UNTIMEOUT (LcpEchoTimeout, f);
1984 lcp_echo_timer_running = 0;
1988 #endif /* PPP_SUPPORT */