2 * lcp.c - PPP Link Control Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * $FreeBSD: src/usr.sbin/pppd/lcp.c,v 1.9.2.1 2003/04/22 10:27:44 fjoe Exp $
20 * $DragonFly: src/usr.sbin/pppd/lcp.c,v 1.4 2005/11/24 23:42:54 swildner Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
44 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
45 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
46 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
47 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
48 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
49 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
51 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
52 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
53 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
55 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
58 * Callbacks for fsm code. (CI = Configuration Information)
60 static void lcp_resetci(fsm *); /* Reset our CI */
61 static int lcp_cilen(fsm *); /* Return length of our CI */
62 static void lcp_addci(fsm *, u_char *, int *); /* Add our CI to pkt */
63 static int lcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
64 static int lcp_nakci(fsm *, u_char *, int); /* Peer nak'd our CI */
65 static int lcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
66 static int lcp_reqci(fsm *, u_char *, int *, int); /* Rcv peer CI */
67 static void lcp_up(fsm *); /* We're UP */
68 static void lcp_down(fsm *); /* We're DOWN */
69 static void lcp_starting(fsm *); /* We need lower layer up */
70 static void lcp_finished(fsm *); /* We need lower layer down */
71 static int lcp_extcode(fsm *, int, int, u_char *, int);
72 static void lcp_rprotrej(fsm *, u_char *, int);
75 * routines to send LCP echos to peer
78 static void lcp_echo_lowerup(int);
79 static void lcp_echo_lowerdown(int);
80 static void LcpEchoTimeout(void *);
81 static void lcp_received_echo_reply(fsm *, int, u_char *, int);
82 static void LcpSendEchoRequest(fsm *);
83 static void LcpLinkFailure(fsm *);
84 static void LcpEchoCheck(fsm *);
86 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
87 lcp_resetci, /* Reset our Configuration Information */
88 lcp_cilen, /* Length of our Configuration Information */
89 lcp_addci, /* Add our Configuration Information */
90 lcp_ackci, /* ACK our Configuration Information */
91 lcp_nakci, /* NAK our Configuration Information */
92 lcp_rejci, /* Reject our Configuration Information */
93 lcp_reqci, /* Request peer's Configuration Information */
94 lcp_up, /* Called when fsm reaches OPENED state */
95 lcp_down, /* Called when fsm leaves OPENED state */
96 lcp_starting, /* Called when we want the lower layer up */
97 lcp_finished, /* Called when we want the lower layer down */
98 NULL, /* Called when Protocol-Reject received */
99 NULL, /* Retransmission is necessary */
100 lcp_extcode, /* Called to handle LCP-specific codes */
101 "LCP" /* String name of protocol */
105 * Protocol entry points.
106 * Some of these are called directly.
109 static void lcp_init(int);
110 static void lcp_input(int, u_char *, int);
111 static void lcp_protrej(int);
112 static int lcp_printpkt(u_char *, int,
113 void (*)(void *, char *, ...), void *);
115 struct protent lcp_protent = {
133 int lcp_loopbackfail = DEFLOOPBACKFAIL;
136 * Length of each type of configuration option (in octets)
140 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
141 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
142 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
143 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
146 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
147 (x) == CONFNAK ? "NAK" : "REJ")
151 * lcp_init - Initialize LCP.
156 fsm *f = &lcp_fsm[unit];
157 lcp_options *wo = &lcp_wantoptions[unit];
158 lcp_options *ao = &lcp_allowoptions[unit];
161 f->protocol = PPP_LCP;
162 f->callbacks = &lcp_callbacks;
168 wo->restart = 0; /* Set to 1 in kernels or multi-line
172 wo->neg_asyncmap = 0;
174 wo->neg_chap = 0; /* Set to 1 on server */
175 wo->neg_upap = 0; /* Set to 1 on server */
176 wo->chap_mdtype = CHAP_DIGEST_MD5;
177 wo->neg_magicnumber = 1;
178 wo->neg_pcompression = 1;
179 wo->neg_accompression = 1;
180 wo->neg_lqr = 0; /* no LQR implementation yet */
185 ao->neg_asyncmap = 1;
188 ao->chap_mdtype = CHAP_DIGEST_MD5;
190 ao->neg_magicnumber = 1;
191 ao->neg_pcompression = 1;
192 ao->neg_accompression = 1;
193 ao->neg_lqr = 0; /* no LQR implementation yet */
200 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
201 xmit_accm[unit][3] = 0x60000000;
206 * lcp_open - LCP is allowed to come up.
211 fsm *f = &lcp_fsm[unit];
212 lcp_options *wo = &lcp_wantoptions[unit];
216 f->flags |= OPT_PASSIVE;
218 f->flags |= OPT_SILENT;
224 * lcp_close - Take LCP down.
227 lcp_close(int unit, char *reason)
229 fsm *f = &lcp_fsm[unit];
231 if (phase != PHASE_DEAD)
232 phase = PHASE_TERMINATE;
233 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
235 * This action is not strictly according to the FSM in RFC1548,
236 * but it does mean that the program terminates if you do a
237 * lcp_close() in passive/silent mode when a connection hasn't
244 fsm_close(&lcp_fsm[unit], reason);
249 * lcp_lowerup - The lower layer is up.
252 lcp_lowerup(int unit)
254 lcp_options *wo = &lcp_wantoptions[unit];
257 * Don't use A/C or protocol compression on transmission,
258 * but accept A/C and protocol compressed packets
259 * if we are going to ask for A/C and protocol compression.
261 ppp_set_xaccm(unit, xmit_accm[unit]);
262 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
263 ppp_recv_config(unit, PPP_MRU, 0xffffffff,
264 wo->neg_pcompression, wo->neg_accompression);
265 peer_mru[unit] = PPP_MRU;
266 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
268 fsm_lowerup(&lcp_fsm[unit]);
273 * lcp_lowerdown - The lower layer is down.
276 lcp_lowerdown(int unit)
278 fsm_lowerdown(&lcp_fsm[unit]);
283 * lcp_input - Input LCP packet.
286 lcp_input(int unit, u_char *p, int len)
288 fsm *f = &lcp_fsm[unit];
290 fsm_input(f, p, len);
295 * lcp_extcode - Handle a LCP-specific code.
298 lcp_extcode(fsm *f, int code, int id, u_char *inp, int len)
304 lcp_rprotrej(f, inp, len);
308 if (f->state != OPENED)
310 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
312 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
313 fsm_sdata(f, ECHOREP, id, inp, len);
317 lcp_received_echo_reply(f, id, inp, len);
331 * lcp_rprotrej - Receive an Protocol-Reject.
333 * Figure out which protocol is rejected and inform it.
336 lcp_rprotrej(fsm *f, u_char *inp, int len)
339 struct protent *protp;
342 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
344 if (len < sizeof (u_short)) {
346 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
353 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
357 * Protocol-Reject packets received in any state other than the LCP
358 * OPENED state SHOULD be silently discarded.
360 if( f->state != OPENED ){
361 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
367 * Upcall the proper Protocol-Reject routine.
369 for (i = 0; (protp = protocols[i]) != NULL; ++i)
370 if (protp->protocol == prot && protp->enabled_flag) {
371 (*protp->protrej)(f->unit);
375 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
381 * lcp_protrej - A Protocol-Reject was received.
385 lcp_protrej(int unit)
390 LCPDEBUG((LOG_WARNING,
391 "lcp_protrej: Received Protocol-Reject for LCP!"));
392 fsm_protreject(&lcp_fsm[unit]);
397 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
400 lcp_sprotrej(int unit, u_char *p, int len)
403 * Send back the protocol and the information field of the
404 * rejected packet. We only get here if LCP is in the OPENED state.
409 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
415 * lcp_resetci - Reset our CI.
420 lcp_wantoptions[f->unit].magicnumber = magic();
421 lcp_wantoptions[f->unit].numloops = 0;
422 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
423 peer_mru[f->unit] = PPP_MRU;
429 * lcp_cilen - Return length of our CI.
434 lcp_options *go = &lcp_gotoptions[f->unit];
436 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
437 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
438 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
439 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
440 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
441 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
443 * NB: we only ask for one of CHAP and UPAP, even if we will
446 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
447 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
448 LENCICHAP(go->neg_chap) +
449 LENCISHORT(!go->neg_chap && go->neg_upap) +
450 LENCILQR(go->neg_lqr) +
451 LENCICBCP(go->neg_cbcp) +
452 LENCILONG(go->neg_magicnumber) +
453 LENCIVOID(go->neg_pcompression) +
454 LENCIVOID(go->neg_accompression));
459 * lcp_addci - Add our desired CIs to a packet.
462 lcp_addci(fsm *f, u_char *ucp, int *lenp)
464 lcp_options *go = &lcp_gotoptions[f->unit];
465 u_char *start_ucp = ucp;
467 #define ADDCIVOID(opt, neg) \
470 PUTCHAR(CILEN_VOID, ucp); \
472 #define ADDCISHORT(opt, neg, val) \
475 PUTCHAR(CILEN_SHORT, ucp); \
476 PUTSHORT(val, ucp); \
478 #define ADDCICHAP(opt, neg, val, digest) \
481 PUTCHAR(CILEN_CHAP, ucp); \
482 PUTSHORT(val, ucp); \
483 PUTCHAR(digest, ucp); \
485 #define ADDCILONG(opt, neg, val) \
488 PUTCHAR(CILEN_LONG, ucp); \
491 #define ADDCILQR(opt, neg, val) \
494 PUTCHAR(CILEN_LQR, ucp); \
495 PUTSHORT(PPP_LQR, ucp); \
498 #define ADDCICHAR(opt, neg, val) \
501 PUTCHAR(CILEN_CHAR, ucp); \
505 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
506 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
508 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
509 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
510 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
511 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
512 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
513 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
514 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
516 if (ucp - start_ucp != *lenp) {
517 /* this should never happen, because peer_mtu should be 1500 */
518 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
524 * lcp_ackci - Ack our CIs.
525 * This should not modify any state if the Ack is bad.
532 lcp_ackci(fsm *f, u_char *p, int len)
534 lcp_options *go = &lcp_gotoptions[f->unit];
535 u_char cilen, citype, cichar;
540 * CIs must be in exactly the same order that we sent.
541 * Check packet length and CI length at each step.
542 * If we find any deviations, then this packet is bad.
544 #define ACKCIVOID(opt, neg) \
546 if ((len -= CILEN_VOID) < 0) \
548 GETCHAR(citype, p); \
550 if (cilen != CILEN_VOID || \
554 #define ACKCISHORT(opt, neg, val) \
556 if ((len -= CILEN_SHORT) < 0) \
558 GETCHAR(citype, p); \
560 if (cilen != CILEN_SHORT || \
563 GETSHORT(cishort, p); \
564 if (cishort != val) \
567 #define ACKCICHAR(opt, neg, val) \
569 if ((len -= CILEN_CHAR) < 0) \
571 GETCHAR(citype, p); \
573 if (cilen != CILEN_CHAR || \
576 GETCHAR(cichar, p); \
580 #define ACKCICHAP(opt, neg, val, digest) \
582 if ((len -= CILEN_CHAP) < 0) \
584 GETCHAR(citype, p); \
586 if (cilen != CILEN_CHAP || \
589 GETSHORT(cishort, p); \
590 if (cishort != val) \
592 GETCHAR(cichar, p); \
593 if (cichar != digest) \
596 #define ACKCILONG(opt, neg, val) \
598 if ((len -= CILEN_LONG) < 0) \
600 GETCHAR(citype, p); \
602 if (cilen != CILEN_LONG || \
605 GETLONG(cilong, p); \
609 #define ACKCILQR(opt, neg, val) \
611 if ((len -= CILEN_LQR) < 0) \
613 GETCHAR(citype, p); \
615 if (cilen != CILEN_LQR || \
618 GETSHORT(cishort, p); \
619 if (cishort != PPP_LQR) \
621 GETLONG(cilong, p); \
626 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
627 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
629 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
630 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
631 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
632 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
633 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
634 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
635 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
638 * If there are any remaining CIs, then this packet is bad.
644 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
650 * lcp_nakci - Peer has sent a NAK for some of our CIs.
651 * This should not modify any state if the Nak is bad
652 * or if LCP is in the OPENED state.
659 lcp_nakci(fsm *f, u_char *p, int len)
661 lcp_options *go = &lcp_gotoptions[f->unit];
662 lcp_options *wo = &lcp_wantoptions[f->unit];
663 u_char citype, cichar, *next;
666 lcp_options no; /* options we've seen Naks for */
667 lcp_options try; /* options to request next time */
671 BZERO(&no, sizeof(no));
675 * Any Nak'd CIs must be in exactly the same order that we sent.
676 * Check packet length and CI length at each step.
677 * If we find any deviations, then this packet is bad.
679 #define NAKCIVOID(opt, neg, code) \
681 len >= CILEN_VOID && \
682 p[1] == CILEN_VOID && \
685 INCPTR(CILEN_VOID, p); \
689 #define NAKCICHAP(opt, neg, code) \
691 len >= CILEN_CHAP && \
692 p[1] == CILEN_CHAP && \
696 GETSHORT(cishort, p); \
697 GETCHAR(cichar, p); \
701 #define NAKCICHAR(opt, neg, code) \
703 len >= CILEN_CHAR && \
704 p[1] == CILEN_CHAR && \
708 GETCHAR(cichar, p); \
712 #define NAKCISHORT(opt, neg, code) \
714 len >= CILEN_SHORT && \
715 p[1] == CILEN_SHORT && \
717 len -= CILEN_SHORT; \
719 GETSHORT(cishort, p); \
723 #define NAKCILONG(opt, neg, code) \
725 len >= CILEN_LONG && \
726 p[1] == CILEN_LONG && \
730 GETLONG(cilong, p); \
734 #define NAKCILQR(opt, neg, code) \
736 len >= CILEN_LQR && \
737 p[1] == CILEN_LQR && \
741 GETSHORT(cishort, p); \
742 GETLONG(cilong, p); \
748 * We don't care if they want to send us smaller packets than
749 * we want. Therefore, accept any MRU less than what we asked for,
750 * but then ignore the new value when setting the MRU in the kernel.
751 * If they send us a bigger MRU than what we asked, accept it, up to
752 * the limit of the default MRU we'd get if we didn't negotiate.
754 if (go->neg_mru && go->mru != DEFMRU) {
755 NAKCISHORT(CI_MRU, neg_mru,
756 if (cishort <= wo->mru || cishort <= DEFMRU)
762 * Add any characters they want to our (receive-side) asyncmap.
764 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
765 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
766 try.asyncmap = go->asyncmap | cilong;
771 * If they've nak'd our authentication-protocol, check whether
772 * they are proposing a different protocol, or a different
773 * hash algorithm for CHAP.
775 if ((go->neg_chap || go->neg_upap)
776 && len >= CILEN_SHORT
777 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
780 no.neg_chap = go->neg_chap;
781 no.neg_upap = go->neg_upap;
783 GETSHORT(cishort, p);
784 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
786 * If we were asking for CHAP, they obviously don't want to do it.
787 * If we weren't asking for CHAP, then we were asking for PAP,
788 * in which case this Nak is bad.
794 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
798 * We were asking for CHAP/MD5; they must want a different
799 * algorithm. If they can't do MD5, we'll have to stop
802 if (cichar != go->chap_mdtype)
806 * Stop asking for PAP if we were asking for it.
813 * We don't recognize what they're suggesting.
814 * Stop asking for what we were asking for.
820 p += cilen - CILEN_SHORT;
825 * If they can't cope with our link quality protocol, we'll have
826 * to stop asking for LQR. We haven't got any other protocol.
827 * If they Nak the reporting period, take their value XXX ?
829 NAKCILQR(CI_QUALITY, neg_lqr,
830 if (cishort != PPP_LQR)
833 try.lqr_period = cilong;
837 * Only implementing CBCP...not the rest of the callback options
839 NAKCICHAR(CI_CALLBACK, neg_cbcp,
844 * Check for a looped-back line.
846 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
847 try.magicnumber = magic();
852 * Peer shouldn't send Nak for protocol compression or
853 * address/control compression requests; they should send
854 * a Reject instead. If they send a Nak, treat it as a Reject.
856 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
857 try.neg_pcompression = 0;
859 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
860 try.neg_accompression = 0;
864 * There may be remaining CIs, if the peer is requesting negotiation
865 * on an option that we didn't include in our request packet.
866 * If we see an option that we requested, or one we've already seen
867 * in this packet, then this packet is bad.
868 * If we wanted to respond by starting to negotiate on the requested
869 * option(s), we could, but we don't, because except for the
870 * authentication type and quality protocol, if we are not negotiating
871 * an option, it is because we were told not to.
872 * For the authentication type, the Nak from the peer means
873 * `let me authenticate myself with you' which is a bit pointless.
874 * For the quality protocol, the Nak means `ask me to send you quality
875 * reports', but if we didn't ask for them, we don't want them.
876 * An option we don't recognize represents the peer asking to
877 * negotiate some option we don't support, so ignore it.
879 while (len > CILEN_VOID) {
882 if (cilen < CILEN_VOID || (len -= cilen) < 0)
884 next = p + cilen - 2;
888 if ((go->neg_mru && go->mru != DEFMRU)
889 || no.neg_mru || cilen != CILEN_SHORT)
891 GETSHORT(cishort, p);
892 if (cishort < DEFMRU)
896 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
897 || no.neg_asyncmap || cilen != CILEN_LONG)
901 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
905 if (go->neg_magicnumber || no.neg_magicnumber ||
909 case CI_PCOMPRESSION:
910 if (go->neg_pcompression || no.neg_pcompression
911 || cilen != CILEN_VOID)
914 case CI_ACCOMPRESSION:
915 if (go->neg_accompression || no.neg_accompression
916 || cilen != CILEN_VOID)
920 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
927 /* If there is still anything left, this packet is bad. */
932 * OK, the Nak is good. Now we can update state.
934 if (f->state != OPENED) {
936 if (++try.numloops >= lcp_loopbackfail) {
937 syslog(LOG_NOTICE, "Serial line is looped back.");
938 lcp_close(f->unit, "Loopback detected");
948 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
954 * lcp_rejci - Peer has Rejected some of our CIs.
955 * This should not modify any state if the Reject is bad
956 * or if LCP is in the OPENED state.
959 * 0 - Reject was bad.
960 * 1 - Reject was good.
963 lcp_rejci(fsm *f, u_char *p, int len)
965 lcp_options *go = &lcp_gotoptions[f->unit];
969 lcp_options try; /* options to request next time */
974 * Any Rejected CIs must be in exactly the same order that we sent.
975 * Check packet length and CI length at each step.
976 * If we find any deviations, then this packet is bad.
978 #define REJCIVOID(opt, neg) \
980 len >= CILEN_VOID && \
981 p[1] == CILEN_VOID && \
984 INCPTR(CILEN_VOID, p); \
986 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
988 #define REJCISHORT(opt, neg, val) \
990 len >= CILEN_SHORT && \
991 p[1] == CILEN_SHORT && \
993 len -= CILEN_SHORT; \
995 GETSHORT(cishort, p); \
996 /* Check rejected value. */ \
997 if (cishort != val) \
1000 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1002 #define REJCICHAP(opt, neg, val, digest) \
1004 len >= CILEN_CHAP && \
1005 p[1] == CILEN_CHAP && \
1007 len -= CILEN_CHAP; \
1009 GETSHORT(cishort, p); \
1010 GETCHAR(cichar, p); \
1011 /* Check rejected value. */ \
1012 if (cishort != val || cichar != digest) \
1016 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1018 #define REJCILONG(opt, neg, val) \
1020 len >= CILEN_LONG && \
1021 p[1] == CILEN_LONG && \
1023 len -= CILEN_LONG; \
1025 GETLONG(cilong, p); \
1026 /* Check rejected value. */ \
1027 if (cilong != val) \
1030 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1032 #define REJCILQR(opt, neg, val) \
1034 len >= CILEN_LQR && \
1035 p[1] == CILEN_LQR && \
1039 GETSHORT(cishort, p); \
1040 GETLONG(cilong, p); \
1041 /* Check rejected value. */ \
1042 if (cishort != PPP_LQR || cilong != val) \
1045 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1047 #define REJCICBCP(opt, neg, val) \
1049 len >= CILEN_CBCP && \
1050 p[1] == CILEN_CBCP && \
1052 len -= CILEN_CBCP; \
1054 GETCHAR(cichar, p); \
1055 /* Check rejected value. */ \
1056 if (cichar != val) \
1059 LCPDEBUG((LOG_INFO,"lcp_rejci rejected Callback opt %d", opt)); \
1062 REJCISHORT(CI_MRU, neg_mru, go->mru);
1063 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1064 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1065 if (!go->neg_chap) {
1066 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1068 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1069 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1070 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1071 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1072 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1075 * If there are any remaining CIs, then this packet is bad.
1080 * Now we can update state.
1082 if (f->state != OPENED)
1087 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1093 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1095 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1096 * appropriately. If reject_if_disagree is non-zero, doesn't return
1097 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1100 * inp: Requested CIs
1101 * len: Length of requested CIs
1104 lcp_reqci(fsm *f, u_char *inp, int *lenp, int reject_if_disagree)
1106 lcp_options *go = &lcp_gotoptions[f->unit];
1107 lcp_options *ho = &lcp_hisoptions[f->unit];
1108 lcp_options *ao = &lcp_allowoptions[f->unit];
1109 u_char *cip, *next; /* Pointer to current and next CIs */
1110 int cilen, citype, cichar; /* Parsed len, type, char value */
1111 u_short cishort; /* Parsed short value */
1112 u_int32_t cilong; /* Parse long value */
1113 int rc = CONFACK; /* Final packet return code */
1114 int orc; /* Individual option return code */
1115 u_char *p; /* Pointer to next char to parse */
1116 u_char *rejp; /* Pointer to next char in reject frame */
1117 u_char *nakp; /* Pointer to next char in Nak frame */
1118 int l = *lenp; /* Length left */
1121 * Reset all his options.
1123 BZERO(ho, sizeof(*ho));
1126 * Process all his options.
1132 orc = CONFACK; /* Assume success */
1133 cip = p = next; /* Remember begining of CI */
1134 if (l < 2 || /* Not enough data for CI header or */
1135 p[1] < 2 || /* CI length too small or */
1136 p[1] > l) { /* CI length too big? */
1137 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1138 orc = CONFREJ; /* Reject bad CI */
1139 cilen = l; /* Reject till end of packet */
1140 l = 0; /* Don't loop again */
1144 GETCHAR(citype, p); /* Parse CI type */
1145 GETCHAR(cilen, p); /* Parse CI length */
1146 l -= cilen; /* Adjust remaining length */
1147 next += cilen; /* Step to next CI */
1149 switch (citype) { /* Check CI type */
1151 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1152 if (!ao->neg_mru || /* Allow option? */
1153 cilen != CILEN_SHORT) { /* Check CI length */
1154 orc = CONFREJ; /* Reject CI */
1157 GETSHORT(cishort, p); /* Parse MRU */
1158 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1161 * He must be able to receive at least our minimum.
1162 * No need to check a maximum. If he sends a large number,
1163 * we'll just ignore it.
1165 if (cishort < MINMRU) {
1166 orc = CONFNAK; /* Nak CI */
1167 PUTCHAR(CI_MRU, nakp);
1168 PUTCHAR(CILEN_SHORT, nakp);
1169 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1172 ho->neg_mru = 1; /* Remember he sent MRU */
1173 ho->mru = cishort; /* And remember value */
1177 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1178 if (!ao->neg_asyncmap ||
1179 cilen != CILEN_LONG) {
1184 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1187 * Asyncmap must have set at least the bits
1188 * which are set in lcp_allowoptions[unit].asyncmap.
1190 if ((ao->asyncmap & ~cilong) != 0) {
1192 PUTCHAR(CI_ASYNCMAP, nakp);
1193 PUTCHAR(CILEN_LONG, nakp);
1194 PUTLONG(ao->asyncmap | cilong, nakp);
1197 ho->neg_asyncmap = 1;
1198 ho->asyncmap = cilong;
1202 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1203 if (cilen < CILEN_SHORT ||
1204 !(ao->neg_upap || ao->neg_chap)) {
1206 * Reject the option if we're not willing to authenticate.
1211 GETSHORT(cishort, p);
1212 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1215 * Authtype must be UPAP or CHAP.
1217 * Note: if both ao->neg_upap and ao->neg_chap are set,
1218 * and the peer sends a Configure-Request with two
1219 * authenticate-protocol requests, one for CHAP and one
1220 * for UPAP, then we will reject the second request.
1221 * Whether we end up doing CHAP or UPAP depends then on
1222 * the ordering of the CIs in the peer's Configure-Request.
1225 if (cishort == PPP_PAP) {
1226 if (ho->neg_chap || /* we've already accepted CHAP */
1227 cilen != CILEN_SHORT) {
1228 LCPDEBUG((LOG_WARNING,
1229 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1233 if (!ao->neg_upap) { /* we don't want to do PAP */
1234 orc = CONFNAK; /* NAK it and suggest CHAP */
1235 PUTCHAR(CI_AUTHTYPE, nakp);
1236 PUTCHAR(CILEN_CHAP, nakp);
1237 PUTSHORT(PPP_CHAP, nakp);
1238 PUTCHAR(ao->chap_mdtype, nakp);
1244 if (cishort == PPP_CHAP) {
1245 if (ho->neg_upap || /* we've already accepted PAP */
1246 cilen != CILEN_CHAP) {
1248 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1252 if (!ao->neg_chap) { /* we don't want to do CHAP */
1253 orc = CONFNAK; /* NAK it and suggest PAP */
1254 PUTCHAR(CI_AUTHTYPE, nakp);
1255 PUTCHAR(CILEN_SHORT, nakp);
1256 PUTSHORT(PPP_PAP, nakp);
1259 GETCHAR(cichar, p); /* get digest type*/
1260 if (cichar != CHAP_DIGEST_MD5
1262 && cichar != CHAP_MICROSOFT
1266 PUTCHAR(CI_AUTHTYPE, nakp);
1267 PUTCHAR(CILEN_CHAP, nakp);
1268 PUTSHORT(PPP_CHAP, nakp);
1269 PUTCHAR(ao->chap_mdtype, nakp);
1272 ho->chap_mdtype = cichar; /* save md type */
1278 * We don't recognize the protocol they're asking for.
1279 * Nak it with something we're willing to do.
1280 * (At this point we know ao->neg_upap || ao->neg_chap.)
1283 PUTCHAR(CI_AUTHTYPE, nakp);
1285 PUTCHAR(CILEN_CHAP, nakp);
1286 PUTSHORT(PPP_CHAP, nakp);
1287 PUTCHAR(ao->chap_mdtype, nakp);
1289 PUTCHAR(CILEN_SHORT, nakp);
1290 PUTSHORT(PPP_PAP, nakp);
1295 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1297 cilen != CILEN_LQR) {
1302 GETSHORT(cishort, p);
1304 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1307 * Check the protocol and the reporting period.
1308 * XXX When should we Nak this, and what with?
1310 if (cishort != PPP_LQR) {
1312 PUTCHAR(CI_QUALITY, nakp);
1313 PUTCHAR(CILEN_LQR, nakp);
1314 PUTSHORT(PPP_LQR, nakp);
1315 PUTLONG(ao->lqr_period, nakp);
1320 case CI_MAGICNUMBER:
1321 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1322 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1323 cilen != CILEN_LONG) {
1328 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1331 * He must have a different magic number.
1333 if (go->neg_magicnumber &&
1334 cilong == go->magicnumber) {
1335 cilong = magic(); /* Don't put magic() inside macro! */
1337 PUTCHAR(CI_MAGICNUMBER, nakp);
1338 PUTCHAR(CILEN_LONG, nakp);
1339 PUTLONG(cilong, nakp);
1342 ho->neg_magicnumber = 1;
1343 ho->magicnumber = cilong;
1347 case CI_PCOMPRESSION:
1348 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1349 if (!ao->neg_pcompression ||
1350 cilen != CILEN_VOID) {
1354 ho->neg_pcompression = 1;
1357 case CI_ACCOMPRESSION:
1358 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1359 if (!ao->neg_accompression ||
1360 cilen != CILEN_VOID) {
1364 ho->neg_accompression = 1;
1368 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1375 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1376 if (orc == CONFACK && /* Good CI */
1377 rc != CONFACK) /* but prior CI wasnt? */
1378 continue; /* Don't send this one */
1380 if (orc == CONFNAK) { /* Nak this CI? */
1381 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1382 && citype != CI_MAGICNUMBER) {
1383 orc = CONFREJ; /* Get tough if so */
1385 if (rc == CONFREJ) /* Rejecting prior CI? */
1386 continue; /* Don't send this one */
1390 if (orc == CONFREJ) { /* Reject this CI */
1392 if (cip != rejp) /* Need to move rejected CI? */
1393 BCOPY(cip, rejp, cilen); /* Move it */
1394 INCPTR(cilen, rejp); /* Update output pointer */
1399 * If we wanted to send additional NAKs (for unsent CIs), the
1400 * code would go here. The extra NAKs would go at *nakp.
1401 * At present there are no cases where we want to ask the
1402 * peer to negotiate an option.
1411 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1413 *lenp = nakp - nak_buffer;
1414 BCOPY(nak_buffer, inp, *lenp);
1421 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1422 return (rc); /* Return final code */
1427 * lcp_up - LCP has come UP.
1432 lcp_options *wo = &lcp_wantoptions[f->unit];
1433 lcp_options *ho = &lcp_hisoptions[f->unit];
1434 lcp_options *go = &lcp_gotoptions[f->unit];
1435 lcp_options *ao = &lcp_allowoptions[f->unit];
1437 if (!go->neg_magicnumber)
1438 go->magicnumber = 0;
1439 if (!ho->neg_magicnumber)
1440 ho->magicnumber = 0;
1443 * Set our MTU to the smaller of the MTU we wanted and
1444 * the MRU our peer wanted. If we negotiated an MRU,
1445 * set our MRU to the larger of value we wanted and
1446 * the value we got in the negotiation.
1448 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1449 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1450 ho->neg_pcompression, ho->neg_accompression);
1451 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1452 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1453 go->neg_pcompression, go->neg_accompression);
1456 peer_mru[f->unit] = ho->mru;
1458 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1460 link_established(f->unit);
1465 * lcp_down - LCP has gone DOWN.
1467 * Alert other protocols.
1472 lcp_options *go = &lcp_gotoptions[f->unit];
1474 lcp_echo_lowerdown(f->unit);
1478 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1479 ppp_recv_config(f->unit, PPP_MRU,
1480 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1481 go->neg_pcompression, go->neg_accompression);
1482 peer_mru[f->unit] = PPP_MRU;
1487 * lcp_starting - LCP needs the lower layer up.
1490 lcp_starting(fsm *f)
1492 link_required(f->unit);
1497 * lcp_finished - LCP has finished with the lower layer.
1500 lcp_finished(fsm *f)
1502 link_terminated(f->unit);
1507 * lcp_printpkt - print the contents of an LCP packet.
1509 static char *lcp_codenames[] = {
1510 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1511 "TermReq", "TermAck", "CodeRej", "ProtRej",
1512 "EchoReq", "EchoRep", "DiscReq"
1516 lcp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
1519 int code, id, len, olen;
1520 u_char *pstart, *optend;
1524 if (plen < HEADERLEN)
1530 if (len < HEADERLEN || len > plen)
1533 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1534 printer(arg, " %s", lcp_codenames[code-1]);
1536 printer(arg, " code=0x%x", code);
1537 printer(arg, " id=0x%x", id);
1544 /* print option list */
1549 if (olen < 2 || olen > len) {
1557 if (olen == CILEN_SHORT) {
1559 GETSHORT(cishort, p);
1560 printer(arg, "mru %d", cishort);
1564 if (olen == CILEN_LONG) {
1567 printer(arg, "asyncmap 0x%x", cilong);
1571 if (olen >= CILEN_SHORT) {
1573 printer(arg, "auth ");
1574 GETSHORT(cishort, p);
1577 printer(arg, "pap");
1580 printer(arg, "chap");
1583 printer(arg, "0x%x", cishort);
1588 if (olen >= CILEN_SHORT) {
1590 printer(arg, "quality ");
1591 GETSHORT(cishort, p);
1594 printer(arg, "lqr");
1597 printer(arg, "0x%x", cishort);
1602 if (olen >= CILEN_CHAR) {
1604 printer(arg, "callback ");
1605 GETSHORT(cishort, p);
1608 printer(arg, "CBCP");
1611 printer(arg, "0x%x", cishort);
1615 case CI_MAGICNUMBER:
1616 if (olen == CILEN_LONG) {
1619 printer(arg, "magic 0x%x", cilong);
1622 case CI_PCOMPRESSION:
1623 if (olen == CILEN_VOID) {
1625 printer(arg, "pcomp");
1628 case CI_ACCOMPRESSION:
1629 if (olen == CILEN_VOID) {
1631 printer(arg, "accomp");
1635 while (p < optend) {
1637 printer(arg, " %.2x", code);
1645 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1647 print_string(p, len, printer, arg);
1658 printer(arg, " magic=0x%x", cilong);
1665 /* print the rest of the bytes in the packet */
1666 for (; len > 0; --len) {
1668 printer(arg, " %.2x", code);
1675 * Time to shut down the link because there is nothing out there.
1679 void LcpLinkFailure(fsm *f)
1681 if (f->state == OPENED) {
1682 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1683 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1684 lcp_close(f->unit, "Peer not responding");
1689 * Timer expired for the LCP echo requests from this process.
1693 LcpEchoCheck(fsm *f)
1695 LcpSendEchoRequest (f);
1696 if (f->state != OPENED)
1700 * Start the timer for the next interval.
1702 assert (lcp_echo_timer_running==0);
1703 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
1704 lcp_echo_timer_running = 1;
1708 * LcpEchoTimeout - Timer expired on the LCP echo
1712 LcpEchoTimeout(void *arg)
1714 if (lcp_echo_timer_running != 0) {
1715 lcp_echo_timer_running = 0;
1716 LcpEchoCheck ((fsm *) arg);
1721 * LcpEchoReply - LCP has received a reply to the echo
1725 lcp_received_echo_reply(fsm *f, int id, u_char *inp, int len)
1729 /* Check the magic number - don't count replies from ourselves. */
1731 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1734 GETLONG(magic, inp);
1735 if (lcp_gotoptions[f->unit].neg_magicnumber
1736 && magic == lcp_gotoptions[f->unit].magicnumber) {
1737 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1741 /* Reset the number of outstanding echo frames */
1742 lcp_echos_pending = 0;
1746 * LcpSendEchoRequest - Send an echo request frame to the peer
1750 LcpSendEchoRequest(fsm *f)
1752 u_int32_t lcp_magic;
1753 u_char pkt[4], *pktp;
1756 * Detect the failure of the peer at this point.
1758 if (lcp_echo_fails != 0) {
1759 if (lcp_echos_pending >= lcp_echo_fails) {
1761 lcp_echos_pending = 0;
1766 * Make and send the echo request frame.
1768 if (f->state == OPENED) {
1769 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1771 PUTLONG(lcp_magic, pktp);
1772 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1773 ++lcp_echos_pending;
1778 * lcp_echo_lowerup - Start the timer for the LCP frame
1782 lcp_echo_lowerup(int unit)
1784 fsm *f = &lcp_fsm[unit];
1786 /* Clear the parameters for generating echo frames */
1787 lcp_echos_pending = 0;
1788 lcp_echo_number = 0;
1789 lcp_echo_timer_running = 0;
1791 /* If a timeout interval is specified then start the timer */
1792 if (lcp_echo_interval != 0)
1797 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1801 lcp_echo_lowerdown(int unit)
1803 fsm *f = &lcp_fsm[unit];
1805 if (lcp_echo_timer_running != 0) {
1806 UNTIMEOUT (LcpEchoTimeout, f);
1807 lcp_echo_timer_running = 0;