FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / lwIP_130 / src / netif / ppp / lcp.c
blobf8af22899bc315f0d03c4b9e11492f88371242ab
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 ******************************************************************************
26 * REVISION HISTORY
28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
29 * Ported to lwIP.
30 * 97-12-01 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
31 * Original.
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.
54 #include "lwip/opt.h"
56 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
58 #include "ppp.h"
59 #include "pppdebug.h"
61 #include "fsm.h"
62 #include "chap.h"
63 #include "magic.h"
64 #include "auth.h"
65 #include "lcp.h"
67 #include <string.h>
69 #if PPPOE_SUPPORT
70 #include "netif/ppp_oe.h"
71 #else
72 #define PPPOE_MAXMTU PPP_MAXMRU
73 #endif
76 /*************************/
77 /*** LOCAL DEFINITIONS ***/
78 /*************************/
80 * Length of each type of configuration option (in octets)
82 #define CILEN_VOID 2
83 #define CILEN_CHAR 3
84 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
85 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
86 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
87 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
88 #define CILEN_CBCP 3
91 /***********************************/
92 /*** LOCAL FUNCTION DECLARATIONS ***/
93 /***********************************/
95 * Callbacks for fsm code. (CI = Configuration Information)
97 static void lcp_resetci (fsm*); /* Reset our CI */
98 static int lcp_cilen (fsm*); /* Return length of our CI */
99 static void lcp_addci (fsm*, u_char*, int*); /* Add our CI to pkt */
100 static int lcp_ackci (fsm*, u_char*, int); /* Peer ack'd our CI */
101 static int lcp_nakci (fsm*, u_char*, int); /* Peer nak'd our CI */
102 static int lcp_rejci (fsm*, u_char*, int); /* Peer rej'd our CI */
103 static int lcp_reqci (fsm*, u_char*, int*, int); /* Rcv peer CI */
104 static void lcp_up (fsm*); /* We're UP */
105 static void lcp_down (fsm*); /* We're DOWN */
106 static void lcp_starting (fsm*); /* We need lower layer up */
107 static void lcp_finished (fsm*); /* We need lower layer down */
108 static int lcp_extcode (fsm*, int, u_char, u_char*, int);
110 static void lcp_rprotrej (fsm*, u_char*, int);
113 * routines to send LCP echos to peer
115 static void lcp_echo_lowerup (int);
116 static void lcp_echo_lowerdown (int);
117 static void LcpEchoTimeout (void*);
118 static void lcp_received_echo_reply (fsm*, int, u_char*, int);
119 static void LcpSendEchoRequest (fsm*);
120 static void LcpLinkFailure (fsm*);
121 static void LcpEchoCheck (fsm*);
124 * Protocol entry points.
125 * Some of these are called directly.
127 static void lcp_input (int, u_char *, int);
128 static void lcp_protrej (int);
130 #define CODENAME(x) ((x) == CONFACK ? "ACK" : (x) == CONFNAK ? "NAK" : "REJ")
133 /******************************/
134 /*** PUBLIC DATA STRUCTURES ***/
135 /******************************/
136 /* global vars */
137 LinkPhase lcp_phase[NUM_PPP]; /* Phase of link session (RFC 1661) */
138 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
139 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
140 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
141 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
142 ext_accm xmit_accm[NUM_PPP]; /* extended transmit ACCM */
146 /*****************************/
147 /*** LOCAL DATA STRUCTURES ***/
148 /*****************************/
149 static fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
150 static u_int lcp_echo_interval = LCP_ECHOINTERVAL; /* Interval between LCP echo-requests */
151 static u_int lcp_echo_fails = LCP_MAXECHOFAILS; /* Tolerance to unanswered echo-requests */
152 static u32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
153 static u32_t lcp_echo_number = 0; /* ID number of next echo frame */
154 static u32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
156 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
158 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
159 lcp_resetci, /* Reset our Configuration Information */
160 lcp_cilen, /* Length of our Configuration Information */
161 lcp_addci, /* Add our Configuration Information */
162 lcp_ackci, /* ACK our Configuration Information */
163 lcp_nakci, /* NAK our Configuration Information */
164 lcp_rejci, /* Reject our Configuration Information */
165 lcp_reqci, /* Request peer's Configuration Information */
166 lcp_up, /* Called when fsm reaches LS_OPENED state */
167 lcp_down, /* Called when fsm leaves LS_OPENED state */
168 lcp_starting, /* Called when we want the lower layer up */
169 lcp_finished, /* Called when we want the lower layer down */
170 NULL, /* Called when Protocol-Reject received */
171 NULL, /* Retransmission is necessary */
172 lcp_extcode, /* Called to handle LCP-specific codes */
173 "LCP" /* String name of protocol */
176 struct protent lcp_protent = {
177 PPP_LCP,
178 lcp_init,
179 lcp_input,
180 lcp_protrej,
181 lcp_lowerup,
182 lcp_lowerdown,
183 lcp_open,
184 lcp_close,
185 #if 0
186 lcp_printpkt,
187 NULL,
188 #endif
190 "LCP",
191 #if 0
192 NULL,
193 NULL,
194 NULL
195 #endif
198 int lcp_loopbackfail = DEFLOOPBACKFAIL;
202 /***********************************/
203 /*** PUBLIC FUNCTION DEFINITIONS ***/
204 /***********************************/
206 * lcp_init - Initialize LCP.
208 void
209 lcp_init(int unit)
211 fsm *f = &lcp_fsm[unit];
212 lcp_options *wo = &lcp_wantoptions[unit];
213 lcp_options *ao = &lcp_allowoptions[unit];
215 f->unit = unit;
216 f->protocol = PPP_LCP;
217 f->callbacks = &lcp_callbacks;
219 fsm_init(f);
221 wo->passive = 0;
222 wo->silent = 0;
223 wo->restart = 0; /* Set to 1 in kernels or multi-line implementations */
224 wo->neg_mru = 1;
225 wo->mru = PPP_DEFMRU;
226 wo->neg_asyncmap = 1;
227 wo->asyncmap = 0x00000000l; /* Assume don't need to escape any ctl chars. */
228 wo->neg_chap = 0; /* Set to 1 on server */
229 wo->neg_upap = 0; /* Set to 1 on server */
230 wo->chap_mdtype = CHAP_DIGEST_MD5;
231 wo->neg_magicnumber = 1;
232 wo->neg_pcompression = 1;
233 wo->neg_accompression = 1;
234 wo->neg_lqr = 0; /* no LQR implementation yet */
235 wo->neg_cbcp = 0;
237 ao->neg_mru = 1;
238 ao->mru = PPP_MAXMRU;
239 ao->neg_asyncmap = 1;
240 ao->asyncmap = 0x00000000l; /* Assume don't need to escape any ctl chars. */
241 ao->neg_chap = (CHAP_SUPPORT != 0);
242 ao->chap_mdtype = CHAP_DIGEST_MD5;
243 ao->neg_upap = (PAP_SUPPORT != 0);
244 ao->neg_magicnumber = 1;
245 ao->neg_pcompression = 1;
246 ao->neg_accompression = 1;
247 ao->neg_lqr = 0; /* no LQR implementation yet */
248 ao->neg_cbcp = (CBCP_SUPPORT != 0);
251 * Set transmit escape for the flag and escape characters plus anything
252 * set for the allowable options.
254 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
255 xmit_accm[unit][15] = 0x60;
256 xmit_accm[unit][0] = (u_char)((ao->asyncmap & 0xFF));
257 xmit_accm[unit][1] = (u_char)((ao->asyncmap >> 8) & 0xFF);
258 xmit_accm[unit][2] = (u_char)((ao->asyncmap >> 16) & 0xFF);
259 xmit_accm[unit][3] = (u_char)((ao->asyncmap >> 24) & 0xFF);
260 LCPDEBUG((LOG_INFO, "lcp_init: xmit_accm=%X %X %X %X\n",
261 xmit_accm[unit][0],
262 xmit_accm[unit][1],
263 xmit_accm[unit][2],
264 xmit_accm[unit][3]));
266 lcp_phase[unit] = PHASE_INITIALIZE;
271 * lcp_open - LCP is allowed to come up.
273 void
274 lcp_open(int unit)
276 fsm *f = &lcp_fsm[unit];
277 lcp_options *wo = &lcp_wantoptions[unit];
279 f->flags = 0;
280 if (wo->passive) {
281 f->flags |= OPT_PASSIVE;
283 if (wo->silent) {
284 f->flags |= OPT_SILENT;
286 fsm_open(f);
288 lcp_phase[unit] = PHASE_ESTABLISH;
293 * lcp_close - Take LCP down.
295 void
296 lcp_close(int unit, char *reason)
298 fsm *f = &lcp_fsm[unit];
300 if (lcp_phase[unit] != PHASE_DEAD) {
301 lcp_phase[unit] = PHASE_TERMINATE;
303 if (f->state == LS_STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
305 * This action is not strictly according to the FSM in RFC1548,
306 * but it does mean that the program terminates if you do an
307 * lcp_close() in passive/silent mode when a connection hasn't
308 * been established.
310 f->state = LS_CLOSED;
311 lcp_finished(f);
312 } else {
313 fsm_close(&lcp_fsm[unit], reason);
319 * lcp_lowerup - The lower layer is up.
321 void
322 lcp_lowerup(int unit)
324 lcp_options *wo = &lcp_wantoptions[unit];
327 * Don't use A/C or protocol compression on transmission,
328 * but accept A/C and protocol compressed packets
329 * if we are going to ask for A/C and protocol compression.
331 ppp_set_xaccm(unit, &xmit_accm[unit]);
332 ppp_send_config(unit, PPP_MRU, 0xffffffffl, 0, 0);
333 ppp_recv_config(unit, PPP_MRU, 0x00000000l,
334 wo->neg_pcompression, wo->neg_accompression);
335 peer_mru[unit] = PPP_MRU;
336 lcp_allowoptions[unit].asyncmap = (u_long)xmit_accm[unit][0]
337 | ((u_long)xmit_accm[unit][1] << 8)
338 | ((u_long)xmit_accm[unit][2] << 16)
339 | ((u_long)xmit_accm[unit][3] << 24);
340 LCPDEBUG((LOG_INFO, "lcp_lowerup: asyncmap=%X %X %X %X\n",
341 xmit_accm[unit][3],
342 xmit_accm[unit][2],
343 xmit_accm[unit][1],
344 xmit_accm[unit][0]));
346 fsm_lowerup(&lcp_fsm[unit]);
351 * lcp_lowerdown - The lower layer is down.
353 void
354 lcp_lowerdown(int unit)
356 fsm_lowerdown(&lcp_fsm[unit]);
360 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
362 void
363 lcp_sprotrej(int unit, u_char *p, int len)
366 * Send back the protocol and the information field of the
367 * rejected packet. We only get here if LCP is in the LS_OPENED state.
370 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id, p, len);
375 /**********************************/
376 /*** LOCAL FUNCTION DEFINITIONS ***/
377 /**********************************/
379 * lcp_input - Input LCP packet.
381 static void
382 lcp_input(int unit, u_char *p, int len)
384 fsm *f = &lcp_fsm[unit];
386 fsm_input(f, p, len);
391 * lcp_extcode - Handle a LCP-specific code.
393 static int
394 lcp_extcode(fsm *f, int code, u_char id, u_char *inp, int len)
396 u_char *magp;
398 switch( code ){
399 case PROTREJ:
400 lcp_rprotrej(f, inp, len);
401 break;
403 case ECHOREQ:
404 if (f->state != LS_OPENED) {
405 break;
407 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d\n", id));
408 magp = inp;
409 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
410 fsm_sdata(f, ECHOREP, id, inp, len);
411 break;
413 case ECHOREP:
414 lcp_received_echo_reply(f, id, inp, len);
415 break;
417 case DISCREQ:
418 break;
420 default:
421 return 0;
423 return 1;
428 * lcp_rprotrej - Receive an Protocol-Reject.
430 * Figure out which protocol is rejected and inform it.
432 static void
433 lcp_rprotrej(fsm *f, u_char *inp, int len)
435 int i;
436 struct protent *protp;
437 u_short prot;
439 if (len < sizeof (u_short)) {
440 LCPDEBUG((LOG_INFO, "lcp_rprotrej: Rcvd short Protocol-Reject packet!\n"));
441 return;
444 GETSHORT(prot, inp);
446 LCPDEBUG((LOG_INFO, "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!\n", prot));
449 * Protocol-Reject packets received in any state other than the LCP
450 * LS_OPENED state SHOULD be silently discarded.
452 if( f->state != LS_OPENED ) {
453 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d\n", f->state));
454 return;
458 * Upcall the proper Protocol-Reject routine.
460 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
461 if (protp->protocol == prot && protp->enabled_flag) {
462 (*protp->protrej)(f->unit);
463 return;
467 LCPDEBUG((LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x\n", prot));
472 * lcp_protrej - A Protocol-Reject was received.
474 static void
475 lcp_protrej(int unit)
477 LWIP_UNUSED_ARG(unit);
479 * Can't reject LCP!
481 LCPDEBUG((LOG_WARNING, "lcp_protrej: Received Protocol-Reject for LCP!\n"));
482 fsm_protreject(&lcp_fsm[unit]);
487 * lcp_resetci - Reset our CI.
489 static void
490 lcp_resetci(fsm *f)
492 lcp_wantoptions[f->unit].magicnumber = magic();
493 lcp_wantoptions[f->unit].numloops = 0;
494 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
495 peer_mru[f->unit] = PPP_MRU;
496 auth_reset(f->unit);
501 * lcp_cilen - Return length of our CI.
503 static int lcp_cilen(fsm *f)
505 lcp_options *go = &lcp_gotoptions[f->unit];
507 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
508 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
509 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
510 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
511 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
512 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
514 * NB: we only ask for one of CHAP and UPAP, even if we will
515 * accept either.
517 return (LENCISHORT(go->neg_mru && go->mru != PPP_DEFMRU) +
518 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) +
519 LENCICHAP(go->neg_chap) +
520 LENCISHORT(!go->neg_chap && go->neg_upap) +
521 LENCILQR(go->neg_lqr) +
522 LENCICBCP(go->neg_cbcp) +
523 LENCILONG(go->neg_magicnumber) +
524 LENCIVOID(go->neg_pcompression) +
525 LENCIVOID(go->neg_accompression));
530 * lcp_addci - Add our desired CIs to a packet.
532 static void
533 lcp_addci(fsm *f, u_char *ucp, int *lenp)
535 lcp_options *go = &lcp_gotoptions[f->unit];
536 u_char *start_ucp = ucp;
538 #define ADDCIVOID(opt, neg) \
539 if (neg) { \
540 LCPDEBUG((LOG_INFO, "lcp_addci: opt=%d\n", opt)); \
541 PUTCHAR(opt, ucp); \
542 PUTCHAR(CILEN_VOID, ucp); \
544 #define ADDCISHORT(opt, neg, val) \
545 if (neg) { \
546 LCPDEBUG((LOG_INFO, "lcp_addci: INT opt=%d %X\n", opt, val)); \
547 PUTCHAR(opt, ucp); \
548 PUTCHAR(CILEN_SHORT, ucp); \
549 PUTSHORT(val, ucp); \
551 #define ADDCICHAP(opt, neg, val, digest) \
552 if (neg) { \
553 LCPDEBUG((LOG_INFO, "lcp_addci: CHAP opt=%d %X\n", opt, val)); \
554 PUTCHAR(opt, ucp); \
555 PUTCHAR(CILEN_CHAP, ucp); \
556 PUTSHORT(val, ucp); \
557 PUTCHAR(digest, ucp); \
559 #define ADDCILONG(opt, neg, val) \
560 if (neg) { \
561 LCPDEBUG((LOG_INFO, "lcp_addci: L opt=%d %lX\n", opt, val)); \
562 PUTCHAR(opt, ucp); \
563 PUTCHAR(CILEN_LONG, ucp); \
564 PUTLONG(val, ucp); \
566 #define ADDCILQR(opt, neg, val) \
567 if (neg) { \
568 LCPDEBUG((LOG_INFO, "lcp_addci: LQR opt=%d %lX\n", opt, val)); \
569 PUTCHAR(opt, ucp); \
570 PUTCHAR(CILEN_LQR, ucp); \
571 PUTSHORT(PPP_LQR, ucp); \
572 PUTLONG(val, ucp); \
574 #define ADDCICHAR(opt, neg, val) \
575 if (neg) { \
576 LCPDEBUG((LOG_INFO, "lcp_addci: CHAR opt=%d %X '%z'\n", opt, val, val)); \
577 PUTCHAR(opt, ucp); \
578 PUTCHAR(CILEN_CHAR, ucp); \
579 PUTCHAR(val, ucp); \
582 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
583 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl, go->asyncmap);
584 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
585 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
586 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
587 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
588 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
589 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
590 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
592 if (ucp - start_ucp != *lenp) {
593 /* this should never happen, because peer_mtu should be 1500 */
594 LCPDEBUG((LOG_ERR, "Bug in lcp_addci: wrong length\n"));
600 * lcp_ackci - Ack our CIs.
601 * This should not modify any state if the Ack is bad.
603 * Returns:
604 * 0 - Ack was bad.
605 * 1 - Ack was good.
607 static int
608 lcp_ackci(fsm *f, u_char *p, int len)
610 lcp_options *go = &lcp_gotoptions[f->unit];
611 u_char cilen, citype, cichar;
612 u_short cishort;
613 u32_t cilong;
616 * CIs must be in exactly the same order that we sent.
617 * Check packet length and CI length at each step.
618 * If we find any deviations, then this packet is bad.
620 #define ACKCIVOID(opt, neg) \
621 if (neg) { \
622 if ((len -= CILEN_VOID) < 0) \
623 goto bad; \
624 GETCHAR(citype, p); \
625 GETCHAR(cilen, p); \
626 if (cilen != CILEN_VOID || citype != opt) \
627 goto bad; \
629 #define ACKCISHORT(opt, neg, val) \
630 if (neg) { \
631 if ((len -= CILEN_SHORT) < 0) \
632 goto bad; \
633 GETCHAR(citype, p); \
634 GETCHAR(cilen, p); \
635 if (cilen != CILEN_SHORT || citype != opt) \
636 goto bad; \
637 GETSHORT(cishort, p); \
638 if (cishort != val) \
639 goto bad; \
641 #define ACKCICHAR(opt, neg, val) \
642 if (neg) { \
643 if ((len -= CILEN_CHAR) < 0) \
644 goto bad; \
645 GETCHAR(citype, p); \
646 GETCHAR(cilen, p); \
647 if (cilen != CILEN_CHAR || citype != opt) \
648 goto bad; \
649 GETCHAR(cichar, p); \
650 if (cichar != val) \
651 goto bad; \
653 #define ACKCICHAP(opt, neg, val, digest) \
654 if (neg) { \
655 if ((len -= CILEN_CHAP) < 0) \
656 goto bad; \
657 GETCHAR(citype, p); \
658 GETCHAR(cilen, p); \
659 if (cilen != CILEN_CHAP || citype != opt) \
660 goto bad; \
661 GETSHORT(cishort, p); \
662 if (cishort != val) \
663 goto bad; \
664 GETCHAR(cichar, p); \
665 if (cichar != digest) \
666 goto bad; \
668 #define ACKCILONG(opt, neg, val) \
669 if (neg) { \
670 if ((len -= CILEN_LONG) < 0) \
671 goto bad; \
672 GETCHAR(citype, p); \
673 GETCHAR(cilen, p); \
674 if (cilen != CILEN_LONG || citype != opt) \
675 goto bad; \
676 GETLONG(cilong, p); \
677 if (cilong != val) \
678 goto bad; \
680 #define ACKCILQR(opt, neg, val) \
681 if (neg) { \
682 if ((len -= CILEN_LQR) < 0) \
683 goto bad; \
684 GETCHAR(citype, p); \
685 GETCHAR(cilen, p); \
686 if (cilen != CILEN_LQR || citype != opt) \
687 goto bad; \
688 GETSHORT(cishort, p); \
689 if (cishort != PPP_LQR) \
690 goto bad; \
691 GETLONG(cilong, p); \
692 if (cilong != val) \
693 goto bad; \
696 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
697 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl, go->asyncmap);
698 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
699 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
700 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
701 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
702 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
703 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
704 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
707 * If there are any remaining CIs, then this packet is bad.
709 if (len != 0) {
710 goto bad;
712 LCPDEBUG((LOG_INFO, "lcp_acki: Ack\n"));
713 return (1);
714 bad:
715 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!\n"));
716 return (0);
721 * lcp_nakci - Peer has sent a NAK for some of our CIs.
722 * This should not modify any state if the Nak is bad
723 * or if LCP is in the LS_OPENED state.
725 * Returns:
726 * 0 - Nak was bad.
727 * 1 - Nak was good.
729 static int
730 lcp_nakci(fsm *f, u_char *p, int len)
732 lcp_options *go = &lcp_gotoptions[f->unit];
733 lcp_options *wo = &lcp_wantoptions[f->unit];
734 u_char citype, cichar, *next;
735 u_short cishort;
736 u32_t cilong;
737 lcp_options no; /* options we've seen Naks for */
738 lcp_options try; /* options to request next time */
739 int looped_back = 0;
740 int cilen;
742 BZERO(&no, sizeof(no));
743 try = *go;
746 * Any Nak'd CIs must be in exactly the same order that we sent.
747 * Check packet length and CI length at each step.
748 * If we find any deviations, then this packet is bad.
750 #define NAKCIVOID(opt, neg, code) \
751 if (go->neg && \
752 len >= CILEN_VOID && \
753 p[1] == CILEN_VOID && \
754 p[0] == opt) { \
755 len -= CILEN_VOID; \
756 INCPTR(CILEN_VOID, p); \
757 no.neg = 1; \
758 code \
760 #define NAKCICHAP(opt, neg, code) \
761 if (go->neg && \
762 len >= CILEN_CHAP && \
763 p[1] == CILEN_CHAP && \
764 p[0] == opt) { \
765 len -= CILEN_CHAP; \
766 INCPTR(2, p); \
767 GETSHORT(cishort, p); \
768 GETCHAR(cichar, p); \
769 no.neg = 1; \
770 code \
772 #define NAKCICHAR(opt, neg, code) \
773 if (go->neg && \
774 len >= CILEN_CHAR && \
775 p[1] == CILEN_CHAR && \
776 p[0] == opt) { \
777 len -= CILEN_CHAR; \
778 INCPTR(2, p); \
779 GETCHAR(cichar, p); \
780 no.neg = 1; \
781 code \
783 #define NAKCISHORT(opt, neg, code) \
784 if (go->neg && \
785 len >= CILEN_SHORT && \
786 p[1] == CILEN_SHORT && \
787 p[0] == opt) { \
788 len -= CILEN_SHORT; \
789 INCPTR(2, p); \
790 GETSHORT(cishort, p); \
791 no.neg = 1; \
792 code \
794 #define NAKCILONG(opt, neg, code) \
795 if (go->neg && \
796 len >= CILEN_LONG && \
797 p[1] == CILEN_LONG && \
798 p[0] == opt) { \
799 len -= CILEN_LONG; \
800 INCPTR(2, p); \
801 GETLONG(cilong, p); \
802 no.neg = 1; \
803 code \
805 #define NAKCILQR(opt, neg, code) \
806 if (go->neg && \
807 len >= CILEN_LQR && \
808 p[1] == CILEN_LQR && \
809 p[0] == opt) { \
810 len -= CILEN_LQR; \
811 INCPTR(2, p); \
812 GETSHORT(cishort, p); \
813 GETLONG(cilong, p); \
814 no.neg = 1; \
815 code \
819 * We don't care if they want to send us smaller packets than
820 * we want. Therefore, accept any MRU less than what we asked for,
821 * but then ignore the new value when setting the MRU in the kernel.
822 * If they send us a bigger MRU than what we asked, accept it, up to
823 * the limit of the default MRU we'd get if we didn't negotiate.
825 if (go->neg_mru && go->mru != PPP_DEFMRU) {
826 NAKCISHORT(CI_MRU, neg_mru,
827 if (cishort <= wo->mru || cishort < PPP_DEFMRU) {
828 try.mru = cishort;
834 * Add any characters they want to our (receive-side) asyncmap.
836 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) {
837 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
838 try.asyncmap = go->asyncmap | cilong;
843 * If they've nak'd our authentication-protocol, check whether
844 * they are proposing a different protocol, or a different
845 * hash algorithm for CHAP.
847 if ((go->neg_chap || go->neg_upap)
848 && len >= CILEN_SHORT
849 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
850 cilen = p[1];
851 len -= cilen;
852 no.neg_chap = go->neg_chap;
853 no.neg_upap = go->neg_upap;
854 INCPTR(2, p);
855 GETSHORT(cishort, p);
856 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
858 * If we were asking for CHAP, they obviously don't want to do it.
859 * If we weren't asking for CHAP, then we were asking for PAP,
860 * in which case this Nak is bad.
862 if (!go->neg_chap) {
863 goto bad;
865 try.neg_chap = 0;
867 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
868 GETCHAR(cichar, p);
869 if (go->neg_chap) {
871 * We were asking for CHAP/MD5; they must want a different
872 * algorithm. If they can't do MD5, we'll have to stop
873 * asking for CHAP.
875 if (cichar != go->chap_mdtype) {
876 try.neg_chap = 0;
878 } else {
880 * Stop asking for PAP if we were asking for it.
882 try.neg_upap = 0;
885 } else {
887 * We don't recognize what they're suggesting.
888 * Stop asking for what we were asking for.
890 if (go->neg_chap) {
891 try.neg_chap = 0;
892 } else {
893 try.neg_upap = 0;
895 p += cilen - CILEN_SHORT;
900 * If they can't cope with our link quality protocol, we'll have
901 * to stop asking for LQR. We haven't got any other protocol.
902 * If they Nak the reporting period, take their value XXX ?
904 NAKCILQR(CI_QUALITY, neg_lqr,
905 if (cishort != PPP_LQR) {
906 try.neg_lqr = 0;
907 } else {
908 try.lqr_period = cilong;
913 * Only implementing CBCP...not the rest of the callback options
915 NAKCICHAR(CI_CALLBACK, neg_cbcp,
916 try.neg_cbcp = 0;
920 * Check for a looped-back line.
922 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
923 try.magicnumber = magic();
924 looped_back = 1;
928 * Peer shouldn't send Nak for protocol compression or
929 * address/control compression requests; they should send
930 * a Reject instead. If they send a Nak, treat it as a Reject.
932 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
933 try.neg_pcompression = 0;
935 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
936 try.neg_accompression = 0;
940 * There may be remaining CIs, if the peer is requesting negotiation
941 * on an option that we didn't include in our request packet.
942 * If we see an option that we requested, or one we've already seen
943 * in this packet, then this packet is bad.
944 * If we wanted to respond by starting to negotiate on the requested
945 * option(s), we could, but we don't, because except for the
946 * authentication type and quality protocol, if we are not negotiating
947 * an option, it is because we were told not to.
948 * For the authentication type, the Nak from the peer means
949 * `let me authenticate myself with you' which is a bit pointless.
950 * For the quality protocol, the Nak means `ask me to send you quality
951 * reports', but if we didn't ask for them, we don't want them.
952 * An option we don't recognize represents the peer asking to
953 * negotiate some option we don't support, so ignore it.
955 while (len > CILEN_VOID) {
956 GETCHAR(citype, p);
957 GETCHAR(cilen, p);
958 if (cilen < CILEN_VOID || (len -= cilen) < 0) {
959 goto bad;
961 next = p + cilen - 2;
963 switch (citype) {
964 case CI_MRU:
965 if ((go->neg_mru && go->mru != PPP_DEFMRU)
966 || no.neg_mru || cilen != CILEN_SHORT) {
967 goto bad;
969 GETSHORT(cishort, p);
970 if (cishort < PPP_DEFMRU) {
971 try.mru = cishort;
973 break;
974 case CI_ASYNCMAP:
975 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl)
976 || no.neg_asyncmap || cilen != CILEN_LONG) {
977 goto bad;
979 break;
980 case CI_AUTHTYPE:
981 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap) {
982 goto bad;
984 break;
985 case CI_MAGICNUMBER:
986 if (go->neg_magicnumber || no.neg_magicnumber ||
987 cilen != CILEN_LONG) {
988 goto bad;
990 break;
991 case CI_PCOMPRESSION:
992 if (go->neg_pcompression || no.neg_pcompression
993 || cilen != CILEN_VOID) {
994 goto bad;
996 break;
997 case CI_ACCOMPRESSION:
998 if (go->neg_accompression || no.neg_accompression
999 || cilen != CILEN_VOID) {
1000 goto bad;
1002 break;
1003 case CI_QUALITY:
1004 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR) {
1005 goto bad;
1007 break;
1009 p = next;
1012 /* If there is still anything left, this packet is bad. */
1013 if (len != 0) {
1014 goto bad;
1018 * OK, the Nak is good. Now we can update state.
1020 if (f->state != LS_OPENED) {
1021 if (looped_back) {
1022 if (++try.numloops >= lcp_loopbackfail) {
1023 LCPDEBUG((LOG_NOTICE, "Serial line is looped back.\n"));
1024 lcp_close(f->unit, "Loopback detected");
1026 } else {
1027 try.numloops = 0;
1029 *go = try;
1032 return 1;
1034 bad:
1035 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!\n"));
1036 return 0;
1041 * lcp_rejci - Peer has Rejected some of our CIs.
1042 * This should not modify any state if the Reject is bad
1043 * or if LCP is in the LS_OPENED state.
1045 * Returns:
1046 * 0 - Reject was bad.
1047 * 1 - Reject was good.
1049 static int
1050 lcp_rejci(fsm *f, u_char *p, int len)
1052 lcp_options *go = &lcp_gotoptions[f->unit];
1053 u_char cichar;
1054 u_short cishort;
1055 u32_t cilong;
1056 lcp_options try; /* options to request next time */
1058 try = *go;
1061 * Any Rejected CIs must be in exactly the same order that we sent.
1062 * Check packet length and CI length at each step.
1063 * If we find any deviations, then this packet is bad.
1065 #define REJCIVOID(opt, neg) \
1066 if (go->neg && \
1067 len >= CILEN_VOID && \
1068 p[1] == CILEN_VOID && \
1069 p[0] == opt) { \
1070 len -= CILEN_VOID; \
1071 INCPTR(CILEN_VOID, p); \
1072 try.neg = 0; \
1073 LCPDEBUG((LOG_INFO, "lcp_rejci: void opt %d rejected\n", opt)); \
1075 #define REJCISHORT(opt, neg, val) \
1076 if (go->neg && \
1077 len >= CILEN_SHORT && \
1078 p[1] == CILEN_SHORT && \
1079 p[0] == opt) { \
1080 len -= CILEN_SHORT; \
1081 INCPTR(2, p); \
1082 GETSHORT(cishort, p); \
1083 /* Check rejected value. */ \
1084 if (cishort != val) { \
1085 goto bad; \
1087 try.neg = 0; \
1088 LCPDEBUG((LOG_INFO,"lcp_rejci: short opt %d rejected\n", opt)); \
1090 #define REJCICHAP(opt, neg, val, digest) \
1091 if (go->neg && \
1092 len >= CILEN_CHAP && \
1093 p[1] == CILEN_CHAP && \
1094 p[0] == opt) { \
1095 len -= CILEN_CHAP; \
1096 INCPTR(2, p); \
1097 GETSHORT(cishort, p); \
1098 GETCHAR(cichar, p); \
1099 /* Check rejected value. */ \
1100 if (cishort != val || cichar != digest) { \
1101 goto bad; \
1103 try.neg = 0; \
1104 try.neg_upap = 0; \
1105 LCPDEBUG((LOG_INFO,"lcp_rejci: chap opt %d rejected\n", opt)); \
1107 #define REJCILONG(opt, neg, val) \
1108 if (go->neg && \
1109 len >= CILEN_LONG && \
1110 p[1] == CILEN_LONG && \
1111 p[0] == opt) { \
1112 len -= CILEN_LONG; \
1113 INCPTR(2, p); \
1114 GETLONG(cilong, p); \
1115 /* Check rejected value. */ \
1116 if (cilong != val) { \
1117 goto bad; \
1119 try.neg = 0; \
1120 LCPDEBUG((LOG_INFO,"lcp_rejci: long opt %d rejected\n", opt)); \
1122 #define REJCILQR(opt, neg, val) \
1123 if (go->neg && \
1124 len >= CILEN_LQR && \
1125 p[1] == CILEN_LQR && \
1126 p[0] == opt) { \
1127 len -= CILEN_LQR; \
1128 INCPTR(2, p); \
1129 GETSHORT(cishort, p); \
1130 GETLONG(cilong, p); \
1131 /* Check rejected value. */ \
1132 if (cishort != PPP_LQR || cilong != val) { \
1133 goto bad; \
1135 try.neg = 0; \
1136 LCPDEBUG((LOG_INFO,"lcp_rejci: LQR opt %d rejected\n", opt)); \
1138 #define REJCICBCP(opt, neg, val) \
1139 if (go->neg && \
1140 len >= CILEN_CBCP && \
1141 p[1] == CILEN_CBCP && \
1142 p[0] == opt) { \
1143 len -= CILEN_CBCP; \
1144 INCPTR(2, p); \
1145 GETCHAR(cichar, p); \
1146 /* Check rejected value. */ \
1147 if (cichar != val) { \
1148 goto bad; \
1150 try.neg = 0; \
1151 LCPDEBUG((LOG_INFO,"lcp_rejci: Callback opt %d rejected\n", opt)); \
1154 REJCISHORT(CI_MRU, neg_mru, go->mru);
1155 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1156 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1157 if (!go->neg_chap) {
1158 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1160 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1161 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1162 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1163 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1164 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1167 * If there are any remaining CIs, then this packet is bad.
1169 if (len != 0) {
1170 goto bad;
1173 * Now we can update state.
1175 if (f->state != LS_OPENED) {
1176 *go = try;
1178 return 1;
1180 bad:
1181 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!\n"));
1182 return 0;
1187 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1189 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1190 * appropriately. If reject_if_disagree is non-zero, doesn't return
1191 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1193 static int
1194 lcp_reqci(fsm *f,
1195 u_char *inp, /* Requested CIs */
1196 int *lenp, /* Length of requested CIs */
1197 int reject_if_disagree)
1199 lcp_options *go = &lcp_gotoptions[f->unit];
1200 lcp_options *ho = &lcp_hisoptions[f->unit];
1201 lcp_options *ao = &lcp_allowoptions[f->unit];
1202 u_char *cip, *next; /* Pointer to current and next CIs */
1203 int cilen, citype, cichar; /* Parsed len, type, char value */
1204 u_short cishort; /* Parsed short value */
1205 u32_t cilong; /* Parse long value */
1206 int rc = CONFACK; /* Final packet return code */
1207 int orc; /* Individual option return code */
1208 u_char *p; /* Pointer to next char to parse */
1209 u_char *rejp; /* Pointer to next char in reject frame */
1210 u_char *nakp; /* Pointer to next char in Nak frame */
1211 int l = *lenp; /* Length left */
1212 #if TRACELCP > 0
1213 char traceBuf[80];
1214 int traceNdx = 0;
1215 #endif
1218 * Reset all his options.
1220 BZERO(ho, sizeof(*ho));
1223 * Process all his options.
1225 next = inp;
1226 nakp = nak_buffer;
1227 rejp = inp;
1228 while (l) {
1229 orc = CONFACK; /* Assume success */
1230 cip = p = next; /* Remember begining of CI */
1231 if (l < 2 || /* Not enough data for CI header or */
1232 p[1] < 2 || /* CI length too small or */
1233 p[1] > l) { /* CI length too big? */
1234 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!\n"));
1235 orc = CONFREJ; /* Reject bad CI */
1236 cilen = l; /* Reject till end of packet */
1237 l = 0; /* Don't loop again */
1238 citype = 0;
1239 goto endswitch;
1241 GETCHAR(citype, p); /* Parse CI type */
1242 GETCHAR(cilen, p); /* Parse CI length */
1243 l -= cilen; /* Adjust remaining length */
1244 next += cilen; /* Step to next CI */
1246 switch (citype) { /* Check CI type */
1247 case CI_MRU:
1248 if (!ao->neg_mru) { /* Allow option? */
1249 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - not allowed\n"));
1250 orc = CONFREJ; /* Reject CI */
1251 break;
1252 } else if (cilen != CILEN_SHORT) { /* Check CI length */
1253 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - bad length\n"));
1254 orc = CONFREJ; /* Reject CI */
1255 break;
1257 GETSHORT(cishort, p); /* Parse MRU */
1260 * He must be able to receive at least our minimum.
1261 * No need to check a maximum. If he sends a large number,
1262 * we'll just ignore it.
1264 if (cishort < PPP_MINMRU) {
1265 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak - MRU too small\n"));
1266 orc = CONFNAK; /* Nak CI */
1267 PUTCHAR(CI_MRU, nakp);
1268 PUTCHAR(CILEN_SHORT, nakp);
1269 PUTSHORT(PPP_MINMRU, nakp); /* Give him a hint */
1270 break;
1272 ho->neg_mru = 1; /* Remember he sent MRU */
1273 ho->mru = cishort; /* And remember value */
1274 #if TRACELCP > 0
1275 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " MRU %d", cishort);
1276 traceNdx = strlen(traceBuf);
1277 #endif
1278 break;
1280 case CI_ASYNCMAP:
1281 if (!ao->neg_asyncmap) {
1282 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP not allowed\n"));
1283 orc = CONFREJ;
1284 break;
1285 } else if (cilen != CILEN_LONG) {
1286 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP bad length\n"));
1287 orc = CONFREJ;
1288 break;
1290 GETLONG(cilong, p);
1293 * Asyncmap must have set at least the bits
1294 * which are set in lcp_allowoptions[unit].asyncmap.
1296 if ((ao->asyncmap & ~cilong) != 0) {
1297 LCPDEBUG((LOG_INFO, "lcp_reqci: Nak ASYNCMAP %lX missing %lX\n",
1298 cilong, ao->asyncmap));
1299 orc = CONFNAK;
1300 PUTCHAR(CI_ASYNCMAP, nakp);
1301 PUTCHAR(CILEN_LONG, nakp);
1302 PUTLONG(ao->asyncmap | cilong, nakp);
1303 break;
1305 ho->neg_asyncmap = 1;
1306 ho->asyncmap = cilong;
1307 #if TRACELCP > 0
1308 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " ASYNCMAP=%lX", cilong);
1309 traceNdx = strlen(traceBuf);
1310 #endif
1311 break;
1313 case CI_AUTHTYPE:
1314 if (cilen < CILEN_SHORT) {
1315 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE missing arg\n"));
1316 orc = CONFREJ;
1317 break;
1318 } else if (!(ao->neg_upap || ao->neg_chap)) {
1320 * Reject the option if we're not willing to authenticate.
1322 LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE not allowed\n"));
1323 orc = CONFREJ;
1324 break;
1326 GETSHORT(cishort, p);
1329 * Authtype must be UPAP or CHAP.
1331 * Note: if both ao->neg_upap and ao->neg_chap are set,
1332 * and the peer sends a Configure-Request with two
1333 * authenticate-protocol requests, one for CHAP and one
1334 * for UPAP, then we will reject the second request.
1335 * Whether we end up doing CHAP or UPAP depends then on
1336 * the ordering of the CIs in the peer's Configure-Request.
1339 if (cishort == PPP_PAP) {
1340 if (ho->neg_chap) { /* we've already accepted CHAP */
1341 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP already accepted\n"));
1342 orc = CONFREJ;
1343 break;
1344 } else if (cilen != CILEN_SHORT) {
1345 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP bad len\n"));
1346 orc = CONFREJ;
1347 break;
1349 if (!ao->neg_upap) { /* we don't want to do PAP */
1350 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE PAP not allowed\n"));
1351 orc = CONFNAK; /* NAK it and suggest CHAP */
1352 PUTCHAR(CI_AUTHTYPE, nakp);
1353 PUTCHAR(CILEN_CHAP, nakp);
1354 PUTSHORT(PPP_CHAP, nakp);
1355 PUTCHAR(ao->chap_mdtype, nakp);
1356 break;
1358 ho->neg_upap = 1;
1359 #if TRACELCP > 0
1360 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " PAP (%X)", cishort);
1361 traceNdx = strlen(traceBuf);
1362 #endif
1363 break;
1365 if (cishort == PPP_CHAP) {
1366 if (ho->neg_upap) { /* we've already accepted PAP */
1367 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP accepted PAP\n"));
1368 orc = CONFREJ;
1369 break;
1370 } else if (cilen != CILEN_CHAP) {
1371 LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP bad len\n"));
1372 orc = CONFREJ;
1373 break;
1375 if (!ao->neg_chap) { /* we don't want to do CHAP */
1376 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP not allowed\n"));
1377 orc = CONFNAK; /* NAK it and suggest PAP */
1378 PUTCHAR(CI_AUTHTYPE, nakp);
1379 PUTCHAR(CILEN_SHORT, nakp);
1380 PUTSHORT(PPP_PAP, nakp);
1381 break;
1383 GETCHAR(cichar, p); /* get digest type*/
1384 if (cichar != CHAP_DIGEST_MD5
1385 #ifdef CHAPMS
1386 && cichar != CHAP_MICROSOFT
1387 #endif
1389 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP digest=%d\n", cichar));
1390 orc = CONFNAK;
1391 PUTCHAR(CI_AUTHTYPE, nakp);
1392 PUTCHAR(CILEN_CHAP, nakp);
1393 PUTSHORT(PPP_CHAP, nakp);
1394 PUTCHAR(ao->chap_mdtype, nakp);
1395 break;
1397 #if TRACELCP > 0
1398 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CHAP %X,%d", cishort, cichar);
1399 traceNdx = strlen(traceBuf);
1400 #endif
1401 ho->chap_mdtype = cichar; /* save md type */
1402 ho->neg_chap = 1;
1403 break;
1407 * We don't recognize the protocol they're asking for.
1408 * Nak it with something we're willing to do.
1409 * (At this point we know ao->neg_upap || ao->neg_chap.)
1411 orc = CONFNAK;
1412 PUTCHAR(CI_AUTHTYPE, nakp);
1413 if (ao->neg_chap) {
1414 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req CHAP\n", cishort));
1415 PUTCHAR(CILEN_CHAP, nakp);
1416 PUTSHORT(PPP_CHAP, nakp);
1417 PUTCHAR(ao->chap_mdtype, nakp);
1418 } else {
1419 LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req PAP\n", cishort));
1420 PUTCHAR(CILEN_SHORT, nakp);
1421 PUTSHORT(PPP_PAP, nakp);
1423 break;
1425 case CI_QUALITY:
1426 GETSHORT(cishort, p);
1427 GETLONG(cilong, p);
1428 #if TRACELCP > 0
1429 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " QUALITY (%x %x)", cishort, (unsigned int) cilong);
1430 traceNdx = strlen(traceBuf);
1431 #endif
1433 if (!ao->neg_lqr ||
1434 cilen != CILEN_LQR) {
1435 orc = CONFREJ;
1436 break;
1440 * Check the protocol and the reporting period.
1441 * XXX When should we Nak this, and what with?
1443 if (cishort != PPP_LQR) {
1444 orc = CONFNAK;
1445 PUTCHAR(CI_QUALITY, nakp);
1446 PUTCHAR(CILEN_LQR, nakp);
1447 PUTSHORT(PPP_LQR, nakp);
1448 PUTLONG(ao->lqr_period, nakp);
1449 break;
1451 break;
1453 case CI_MAGICNUMBER:
1454 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1455 cilen != CILEN_LONG) {
1456 orc = CONFREJ;
1457 break;
1459 GETLONG(cilong, p);
1460 #if TRACELCP > 0
1461 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " MAGICNUMBER (%lX)", cilong);
1462 traceNdx = strlen(traceBuf);
1463 #endif
1466 * He must have a different magic number.
1468 if (go->neg_magicnumber &&
1469 cilong == go->magicnumber) {
1470 cilong = magic(); /* Don't put magic() inside macro! */
1471 orc = CONFNAK;
1472 PUTCHAR(CI_MAGICNUMBER, nakp);
1473 PUTCHAR(CILEN_LONG, nakp);
1474 PUTLONG(cilong, nakp);
1475 break;
1477 ho->neg_magicnumber = 1;
1478 ho->magicnumber = cilong;
1479 break;
1482 case CI_PCOMPRESSION:
1483 #if TRACELCP > 0
1484 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " PCOMPRESSION");
1485 traceNdx = strlen(traceBuf);
1486 #endif
1487 if (!ao->neg_pcompression ||
1488 cilen != CILEN_VOID) {
1489 orc = CONFREJ;
1490 break;
1492 ho->neg_pcompression = 1;
1493 break;
1495 case CI_ACCOMPRESSION:
1496 #if TRACELCP > 0
1497 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " ACCOMPRESSION");
1498 traceNdx = strlen(traceBuf);
1499 #endif
1500 if (!ao->neg_accompression ||
1501 cilen != CILEN_VOID) {
1502 orc = CONFREJ;
1503 break;
1505 ho->neg_accompression = 1;
1506 break;
1508 case CI_MRRU:
1509 #if TRACELCP > 0
1510 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_MRRU");
1511 traceNdx = strlen(traceBuf);
1512 #endif
1513 orc = CONFREJ;
1514 break;
1516 case CI_SSNHF:
1517 #if TRACELCP > 0
1518 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_SSNHF");
1519 traceNdx = strlen(traceBuf);
1520 #endif
1521 orc = CONFREJ;
1522 break;
1524 case CI_EPDISC:
1525 #if TRACELCP > 0
1526 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_EPDISC");
1527 traceNdx = strlen(traceBuf);
1528 #endif
1529 orc = CONFREJ;
1530 break;
1532 default:
1533 #if TRACELCP
1534 snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " unknown %d", citype);
1535 traceNdx = strlen(traceBuf);
1536 #endif
1537 orc = CONFREJ;
1538 break;
1541 endswitch:
1542 #if TRACELCP
1543 if (traceNdx >= 80 - 32) {
1544 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd%s\n", traceBuf));
1545 traceNdx = 0;
1547 #endif
1548 if (orc == CONFACK && /* Good CI */
1549 rc != CONFACK) { /* but prior CI wasnt? */
1550 continue; /* Don't send this one */
1553 if (orc == CONFNAK) { /* Nak this CI? */
1554 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1555 && citype != CI_MAGICNUMBER) {
1556 orc = CONFREJ; /* Get tough if so */
1557 } else {
1558 if (rc == CONFREJ) { /* Rejecting prior CI? */
1559 continue; /* Don't send this one */
1561 rc = CONFNAK;
1564 if (orc == CONFREJ) { /* Reject this CI */
1565 rc = CONFREJ;
1566 if (cip != rejp) { /* Need to move rejected CI? */
1567 BCOPY(cip, rejp, cilen); /* Move it */
1569 INCPTR(cilen, rejp); /* Update output pointer */
1574 * If we wanted to send additional NAKs (for unsent CIs), the
1575 * code would go here. The extra NAKs would go at *nakp.
1576 * At present there are no cases where we want to ask the
1577 * peer to negotiate an option.
1580 switch (rc) {
1581 case CONFACK:
1582 *lenp = (int)(next - inp);
1583 break;
1584 case CONFNAK:
1586 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1588 *lenp = (int)(nakp - nak_buffer);
1589 BCOPY(nak_buffer, inp, *lenp);
1590 break;
1591 case CONFREJ:
1592 *lenp = (int)(rejp - inp);
1593 break;
1596 #if TRACELCP > 0
1597 if (traceNdx > 0) {
1598 LCPDEBUG((LOG_INFO, "lcp_reqci: %s\n", traceBuf));
1600 #endif
1601 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.\n", CODENAME(rc)));
1602 return (rc); /* Return final code */
1607 * lcp_up - LCP has come UP.
1609 static void
1610 lcp_up(fsm *f)
1612 lcp_options *wo = &lcp_wantoptions[f->unit];
1613 lcp_options *ho = &lcp_hisoptions[f->unit];
1614 lcp_options *go = &lcp_gotoptions[f->unit];
1615 lcp_options *ao = &lcp_allowoptions[f->unit];
1617 if (!go->neg_magicnumber) {
1618 go->magicnumber = 0;
1620 if (!ho->neg_magicnumber) {
1621 ho->magicnumber = 0;
1625 * Set our MTU to the smaller of the MTU we wanted and
1626 * the MRU our peer wanted. If we negotiated an MRU,
1627 * set our MRU to the larger of value we wanted and
1628 * the value we got in the negotiation.
1630 ppp_send_config(f->unit, LWIP_MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1631 (ho->neg_asyncmap? ho->asyncmap: 0xffffffffl),
1632 ho->neg_pcompression, ho->neg_accompression);
1634 * If the asyncmap hasn't been negotiated, we really should
1635 * set the receive asyncmap to ffffffff, but we set it to 0
1636 * for backwards contemptibility.
1638 ppp_recv_config(f->unit, (go->neg_mru? LWIP_MAX(wo->mru, go->mru): PPP_MRU),
1639 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1640 go->neg_pcompression, go->neg_accompression);
1642 if (ho->neg_mru) {
1643 peer_mru[f->unit] = ho->mru;
1646 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1648 link_established(f->unit);
1653 * lcp_down - LCP has gone DOWN.
1655 * Alert other protocols.
1657 static void
1658 lcp_down(fsm *f)
1660 lcp_options *go = &lcp_gotoptions[f->unit];
1662 lcp_echo_lowerdown(f->unit);
1664 link_down(f->unit);
1666 ppp_send_config(f->unit, PPP_MRU, 0xffffffffl, 0, 0);
1667 ppp_recv_config(f->unit, PPP_MRU,
1668 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1669 go->neg_pcompression, go->neg_accompression);
1670 peer_mru[f->unit] = PPP_MRU;
1675 * lcp_starting - LCP needs the lower layer up.
1677 static void
1678 lcp_starting(fsm *f)
1680 link_required(f->unit);
1685 * lcp_finished - LCP has finished with the lower layer.
1687 static void
1688 lcp_finished(fsm *f)
1690 link_terminated(f->unit);
1694 #if 0
1696 * print_string - print a readable representation of a string using
1697 * printer.
1699 static void
1700 print_string( char *p, int len, void (*printer) (void *, char *, ...), void *arg)
1702 int c;
1704 printer(arg, "\"");
1705 for (; len > 0; --len) {
1706 c = *p++;
1707 if (' ' <= c && c <= '~') {
1708 if (c == '\\' || c == '"') {
1709 printer(arg, "\\");
1711 printer(arg, "%c", c);
1712 } else {
1713 switch (c) {
1714 case '\n':
1715 printer(arg, "\\n");
1716 break;
1717 case '\r':
1718 printer(arg, "\\r");
1719 break;
1720 case '\t':
1721 printer(arg, "\\t");
1722 break;
1723 default:
1724 printer(arg, "\\%.3o", c);
1728 printer(arg, "\"");
1733 * lcp_printpkt - print the contents of an LCP packet.
1735 static char *lcp_codenames[] = {
1736 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1737 "TermReq", "TermAck", "CodeRej", "ProtRej",
1738 "EchoReq", "EchoRep", "DiscReq"
1741 static int
1742 lcp_printpkt( u_char *p, int plen, void (*printer) (void *, char *, ...), void *arg)
1744 int code, id, len, olen;
1745 u_char *pstart, *optend;
1746 u_short cishort;
1747 u32_t cilong;
1749 if (plen < HEADERLEN) {
1750 return 0;
1752 pstart = p;
1753 GETCHAR(code, p);
1754 GETCHAR(id, p);
1755 GETSHORT(len, p);
1756 if (len < HEADERLEN || len > plen) {
1757 return 0;
1760 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *)) {
1761 printer(arg, " %s", lcp_codenames[code-1]);
1762 } else {
1763 printer(arg, " code=0x%x", code);
1765 printer(arg, " id=0x%x", id);
1766 len -= HEADERLEN;
1767 switch (code) {
1768 case CONFREQ:
1769 case CONFACK:
1770 case CONFNAK:
1771 case CONFREJ:
1772 /* print option list */
1773 while (len >= 2) {
1774 GETCHAR(code, p);
1775 GETCHAR(olen, p);
1776 p -= 2;
1777 if (olen < 2 || olen > len) {
1778 break;
1780 printer(arg, " <");
1781 len -= olen;
1782 optend = p + olen;
1783 switch (code) {
1784 case CI_MRU:
1785 if (olen == CILEN_SHORT) {
1786 p += 2;
1787 GETSHORT(cishort, p);
1788 printer(arg, "mru %d", cishort);
1790 break;
1791 case CI_ASYNCMAP:
1792 if (olen == CILEN_LONG) {
1793 p += 2;
1794 GETLONG(cilong, p);
1795 printer(arg, "asyncmap 0x%lx", cilong);
1797 break;
1798 case CI_AUTHTYPE:
1799 if (olen >= CILEN_SHORT) {
1800 p += 2;
1801 printer(arg, "auth ");
1802 GETSHORT(cishort, p);
1803 switch (cishort) {
1804 case PPP_PAP:
1805 printer(arg, "pap");
1806 break;
1807 case PPP_CHAP:
1808 printer(arg, "chap");
1809 break;
1810 default:
1811 printer(arg, "0x%x", cishort);
1814 break;
1815 case CI_QUALITY:
1816 if (olen >= CILEN_SHORT) {
1817 p += 2;
1818 printer(arg, "quality ");
1819 GETSHORT(cishort, p);
1820 switch (cishort) {
1821 case PPP_LQR:
1822 printer(arg, "lqr");
1823 break;
1824 default:
1825 printer(arg, "0x%x", cishort);
1828 break;
1829 case CI_CALLBACK:
1830 if (olen >= CILEN_CHAR) {
1831 p += 2;
1832 printer(arg, "callback ");
1833 GETSHORT(cishort, p);
1834 switch (cishort) {
1835 case CBCP_OPT:
1836 printer(arg, "CBCP");
1837 break;
1838 default:
1839 printer(arg, "0x%x", cishort);
1842 break;
1843 case CI_MAGICNUMBER:
1844 if (olen == CILEN_LONG) {
1845 p += 2;
1846 GETLONG(cilong, p);
1847 printer(arg, "magic 0x%x", cilong);
1849 break;
1850 case CI_PCOMPRESSION:
1851 if (olen == CILEN_VOID) {
1852 p += 2;
1853 printer(arg, "pcomp");
1855 break;
1856 case CI_ACCOMPRESSION:
1857 if (olen == CILEN_VOID) {
1858 p += 2;
1859 printer(arg, "accomp");
1861 break;
1863 while (p < optend) {
1864 GETCHAR(code, p);
1865 printer(arg, " %.2x", code);
1867 printer(arg, ">");
1869 break;
1871 case TERMACK:
1872 case TERMREQ:
1873 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1874 printer(arg, " ");
1875 print_string((char*)p, len, printer, arg);
1876 p += len;
1877 len = 0;
1879 break;
1881 case ECHOREQ:
1882 case ECHOREP:
1883 case DISCREQ:
1884 if (len >= 4) {
1885 GETLONG(cilong, p);
1886 printer(arg, " magic=0x%x", cilong);
1887 p += 4;
1888 len -= 4;
1890 break;
1893 /* print the rest of the bytes in the packet */
1894 for (; len > 0; --len) {
1895 GETCHAR(code, p);
1896 printer(arg, " %.2x", code);
1899 return (int)(p - pstart);
1901 #endif
1904 * Time to shut down the link because there is nothing out there.
1906 static void
1907 LcpLinkFailure (fsm *f)
1909 if (f->state == LS_OPENED) {
1910 LCPDEBUG((LOG_INFO, "No response to %d echo-requests\n", lcp_echos_pending));
1911 LCPDEBUG((LOG_NOTICE, "Serial link appears to be disconnected.\n"));
1912 lcp_close(f->unit, "Peer not responding");
1917 * Timer expired for the LCP echo requests from this process.
1919 static void
1920 LcpEchoCheck (fsm *f)
1922 LcpSendEchoRequest (f);
1925 * Start the timer for the next interval.
1927 LWIP_ASSERT("lcp_echo_timer_running == 0", lcp_echo_timer_running == 0);
1929 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
1930 lcp_echo_timer_running = 1;
1934 * LcpEchoTimeout - Timer expired on the LCP echo
1936 static void
1937 LcpEchoTimeout (void *arg)
1939 if (lcp_echo_timer_running != 0) {
1940 lcp_echo_timer_running = 0;
1941 LcpEchoCheck ((fsm *) arg);
1946 * LcpEchoReply - LCP has received a reply to the echo
1948 static void
1949 lcp_received_echo_reply (fsm *f, int id, u_char *inp, int len)
1951 u32_t magic;
1953 LWIP_UNUSED_ARG(id);
1955 /* Check the magic number - don't count replies from ourselves. */
1956 if (len < 4) {
1957 LCPDEBUG((LOG_WARNING, "lcp: received short Echo-Reply, length %d\n", len));
1958 return;
1960 GETLONG(magic, inp);
1961 if (lcp_gotoptions[f->unit].neg_magicnumber && magic == lcp_gotoptions[f->unit].magicnumber) {
1962 LCPDEBUG((LOG_WARNING, "appear to have received our own echo-reply!\n"));
1963 return;
1966 /* Reset the number of outstanding echo frames */
1967 lcp_echos_pending = 0;
1971 * LcpSendEchoRequest - Send an echo request frame to the peer
1973 static void
1974 LcpSendEchoRequest (fsm *f)
1976 u32_t lcp_magic;
1977 u_char pkt[4], *pktp;
1980 * Detect the failure of the peer at this point.
1982 if (lcp_echo_fails != 0) {
1983 if (lcp_echos_pending++ >= lcp_echo_fails) {
1984 LcpLinkFailure(f);
1985 lcp_echos_pending = 0;
1990 * Make and send the echo request frame.
1992 if (f->state == LS_OPENED) {
1993 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1994 pktp = pkt;
1995 PUTLONG(lcp_magic, pktp);
1996 fsm_sdata(f, ECHOREQ, (u_char)(lcp_echo_number++ & 0xFF), pkt, (int)(pktp - pkt));
2001 * lcp_echo_lowerup - Start the timer for the LCP frame
2004 static void
2005 lcp_echo_lowerup (int unit)
2007 fsm *f = &lcp_fsm[unit];
2009 /* Clear the parameters for generating echo frames */
2010 lcp_echos_pending = 0;
2011 lcp_echo_number = 0;
2012 lcp_echo_timer_running = 0;
2014 /* If a timeout interval is specified then start the timer */
2015 if (lcp_echo_interval != 0) {
2016 LcpEchoCheck (f);
2021 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2024 static void
2025 lcp_echo_lowerdown (int unit)
2027 fsm *f = &lcp_fsm[unit];
2029 if (lcp_echo_timer_running != 0) {
2030 UNTIMEOUT (LcpEchoTimeout, f);
2031 lcp_echo_timer_running = 0;
2035 #endif /* PPP_SUPPORT */