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.
21 static char rcsid[] = "$Id: lcp.c,v 1.23 1995/12/18 03:45:32 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
44 #ifdef _linux_ /* Needs ppp ioctls */
46 #include <linux/if_ppp.h>
50 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
51 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
52 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
53 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
54 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
55 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
57 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
58 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
59 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
61 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
64 u_int32_t idle_timer_running = 0;
65 extern int idle_time_limit;
69 * Callbacks for fsm code. (CI = Configuration Information)
71 static void lcp_resetci __P((fsm *)); /* Reset our CI */
72 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
73 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
74 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
75 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
76 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
77 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
78 static void lcp_up __P((fsm *)); /* We're UP */
79 static void lcp_down __P((fsm *)); /* We're DOWN */
80 static void lcp_starting __P((fsm *)); /* We need lower layer up */
81 static void lcp_finished __P((fsm *)); /* We need lower layer down */
82 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
83 static void lcp_rprotrej __P((fsm *, u_char *, int));
86 * routines to send LCP echos to peer
89 static void lcp_echo_lowerup __P((int));
90 static void lcp_echo_lowerdown __P((int));
91 static void LcpEchoTimeout __P((caddr_t));
92 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
93 static void LcpSendEchoRequest __P((fsm *));
94 static void LcpLinkFailure __P((fsm *));
96 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
97 lcp_resetci, /* Reset our Configuration Information */
98 lcp_cilen, /* Length of our Configuration Information */
99 lcp_addci, /* Add our Configuration Information */
100 lcp_ackci, /* ACK our Configuration Information */
101 lcp_nakci, /* NAK our Configuration Information */
102 lcp_rejci, /* Reject our Configuration Information */
103 lcp_reqci, /* Request peer's Configuration Information */
104 lcp_up, /* Called when fsm reaches OPENED state */
105 lcp_down, /* Called when fsm leaves OPENED state */
106 lcp_starting, /* Called when we want the lower layer up */
107 lcp_finished, /* Called when we want the lower layer down */
108 NULL, /* Called when Protocol-Reject received */
109 NULL, /* Retransmission is necessary */
110 lcp_extcode, /* Called to handle LCP-specific codes */
111 "LCP" /* String name of protocol */
114 struct protent lcp_protent = {
115 PPP_LCP, lcp_init, lcp_input, lcp_protrej,
116 lcp_lowerup, lcp_lowerdown, lcp_open, lcp_close,
117 lcp_printpkt, NULL, 1, "LCP"
120 int lcp_loopbackfail = DEFLOOPBACKFAIL;
123 * Length of each type of configuration option (in octets)
126 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
127 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
128 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
129 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
131 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
132 (x) == CONFNAK ? "NAK" : "REJ")
136 * lcp_init - Initialize LCP.
142 fsm *f = &lcp_fsm[unit];
143 lcp_options *wo = &lcp_wantoptions[unit];
144 lcp_options *ao = &lcp_allowoptions[unit];
147 f->protocol = PPP_LCP;
148 f->callbacks = &lcp_callbacks;
154 wo->restart = 0; /* Set to 1 in kernels or multi-line
158 wo->neg_asyncmap = 0;
160 wo->neg_chap = 0; /* Set to 1 on server */
161 wo->neg_upap = 0; /* Set to 1 on server */
162 wo->chap_mdtype = CHAP_DIGEST_MD5;
163 wo->neg_magicnumber = 1;
164 wo->neg_pcompression = 1;
165 wo->neg_accompression = 1;
166 wo->neg_lqr = 0; /* no LQR implementation yet */
170 ao->neg_asyncmap = 1;
173 ao->chap_mdtype = CHAP_DIGEST_MD5;
175 ao->neg_magicnumber = 1;
176 ao->neg_pcompression = 1;
177 ao->neg_accompression = 1;
178 ao->neg_lqr = 0; /* no LQR implementation yet */
180 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
181 xmit_accm[unit][3] = 0x60000000;
186 * lcp_open - LCP is allowed to come up.
192 fsm *f = &lcp_fsm[unit];
193 lcp_options *wo = &lcp_wantoptions[unit];
197 f->flags |= OPT_PASSIVE;
199 f->flags |= OPT_SILENT;
205 * lcp_close - Take LCP down.
208 lcp_close(unit, reason)
212 fsm *f = &lcp_fsm[unit];
214 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
216 * This action is not strictly according to the FSM in RFC1548,
217 * but it does mean that the program terminates if you do a
218 * lcp_close(0) in passive/silent mode when a connection hasn't
225 fsm_close(&lcp_fsm[unit], reason);
229 static void IdleTimeCheck __P((caddr_t));
232 * Timer expired for the LCP echo requests from this process.
240 struct ppp_idle ddinfo;
242 * Read the time since the last packet was received.
244 if (ioctl (fd, PPPIOCGIDLE, &ddinfo) < 0) {
245 syslog (LOG_ERR, "ioctl(PPPIOCGIDLE): %m");
249 * Compute the time since the last packet was received. If the timer
250 * has expired then disconnect the line.
252 delta = idle_time_limit - (u_long) ddinfo.recv_idle;
253 if (((int) delta <= 0L) && (f->state == OPENED)) {
254 syslog (LOG_NOTICE, "No IP frames received within idle time limit");
255 lcp_close(f->unit, "Idle time limit expired"); /* Reset connection */
256 phase = PHASE_TERMINATE; /* Mark it down */
258 if ((int) delta <= 0L)
259 delta = (u_long) idle_time_limit;
260 assert (idle_timer_running==0);
261 TIMEOUT (IdleTimeCheck, (caddr_t) f, delta);
262 idle_timer_running = 1;
267 * IdleTimeCheck - Timer expired on the IDLE detection for IP frames
274 if (idle_timer_running != 0) {
275 idle_timer_running = 0;
276 RestartIdleTimer ((fsm *) arg);
282 * lcp_lowerup - The lower layer is up.
288 lcp_options *wo = &lcp_wantoptions[unit];
292 * Don't use A/C or protocol compression on transmission,
293 * but accept A/C and protocol compressed packets
294 * if we are going to ask for A/C and protocol compression.
296 ppp_set_xaccm(unit, xmit_accm[unit]);
297 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
298 ppp_recv_config(unit, PPP_MRU, 0x00000000,
299 wo->neg_pcompression, wo->neg_accompression);
300 peer_mru[unit] = PPP_MRU;
301 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
303 fsm_lowerup(&lcp_fsm[unit]);
308 * lcp_lowerdown - The lower layer is down.
314 fsm_lowerdown(&lcp_fsm[unit]);
319 * lcp_input - Input LCP packet.
322 lcp_input(unit, p, len)
327 fsm *f = &lcp_fsm[unit];
329 fsm_input(f, p, len);
334 * lcp_extcode - Handle a LCP-specific code.
337 lcp_extcode(f, code, id, inp, len)
347 lcp_rprotrej(f, inp, len);
351 if (f->state != OPENED)
353 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
355 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
356 fsm_sdata(f, ECHOREP, id, inp, len);
360 lcp_received_echo_reply(f, id, inp, len);
374 * lcp_rprotrej - Receive an Protocol-Reject.
376 * Figure out which protocol is rejected and inform it.
379 lcp_rprotrej(f, inp, len)
385 struct protent *protp;
388 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
390 if (len < sizeof (u_short)) {
392 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
399 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
403 * Protocol-Reject packets received in any state other than the LCP
404 * OPENED state SHOULD be silently discarded.
406 if( f->state != OPENED ){
407 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
413 * Upcall the proper Protocol-Reject routine.
415 for (i = 0; (protp = protocols[i]) != NULL; ++i)
416 if (protp->protocol == prot && protp->enabled_flag) {
417 (*protp->protrej)(f->unit);
421 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
427 * lcp_protrej - A Protocol-Reject was received.
437 LCPDEBUG((LOG_WARNING,
438 "lcp_protrej: Received Protocol-Reject for LCP!"));
439 fsm_protreject(&lcp_fsm[unit]);
444 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
447 lcp_sprotrej(unit, p, len)
453 * Send back the protocol and the information field of the
454 * rejected packet. We only get here if LCP is in the OPENED state.
459 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
465 * lcp_resetci - Reset our CI.
471 lcp_wantoptions[f->unit].magicnumber = magic();
472 lcp_wantoptions[f->unit].numloops = 0;
473 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
474 peer_mru[f->unit] = PPP_MRU;
479 * lcp_cilen - Return length of our CI.
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)
493 * NB: we only ask for one of CHAP and UPAP, even if we will
496 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
497 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
498 LENCICHAP(go->neg_chap) +
499 LENCISHORT(!go->neg_chap && go->neg_upap) +
500 LENCILQR(go->neg_lqr) +
501 LENCILONG(go->neg_magicnumber) +
502 LENCIVOID(go->neg_pcompression) +
503 LENCIVOID(go->neg_accompression));
508 * lcp_addci - Add our desired CIs to a packet.
511 lcp_addci(f, ucp, lenp)
516 lcp_options *go = &lcp_gotoptions[f->unit];
517 u_char *start_ucp = ucp;
519 #define ADDCIVOID(opt, neg) \
522 PUTCHAR(CILEN_VOID, ucp); \
524 #define ADDCISHORT(opt, neg, val) \
527 PUTCHAR(CILEN_SHORT, ucp); \
528 PUTSHORT(val, ucp); \
530 #define ADDCICHAP(opt, neg, val, digest) \
533 PUTCHAR(CILEN_CHAP, ucp); \
534 PUTSHORT(val, ucp); \
535 PUTCHAR(digest, ucp); \
537 #define ADDCILONG(opt, neg, val) \
540 PUTCHAR(CILEN_LONG, ucp); \
543 #define ADDCILQR(opt, neg, val) \
546 PUTCHAR(CILEN_LQR, ucp); \
547 PUTSHORT(PPP_LQR, ucp); \
551 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
552 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
554 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
555 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
556 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
557 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
558 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
559 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
561 if (ucp - start_ucp != *lenp) {
562 /* this should never happen, because peer_mtu should be 1500 */
563 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
569 * lcp_ackci - Ack our CIs.
570 * This should not modify any state if the Ack is bad.
582 lcp_options *go = &lcp_gotoptions[f->unit];
583 u_char cilen, citype, cichar;
588 * CIs must be in exactly the same order that we sent.
589 * Check packet length and CI length at each step.
590 * If we find any deviations, then this packet is bad.
592 #define ACKCIVOID(opt, neg) \
594 if ((len -= CILEN_VOID) < 0) \
596 GETCHAR(citype, p); \
598 if (cilen != CILEN_VOID || \
602 #define ACKCISHORT(opt, neg, val) \
604 if ((len -= CILEN_SHORT) < 0) \
606 GETCHAR(citype, p); \
608 if (cilen != CILEN_SHORT || \
611 GETSHORT(cishort, p); \
612 if (cishort != val) \
615 #define ACKCICHAP(opt, neg, val, digest) \
617 if ((len -= CILEN_CHAP) < 0) \
619 GETCHAR(citype, p); \
621 if (cilen != CILEN_CHAP || \
624 GETSHORT(cishort, p); \
625 if (cishort != val) \
627 GETCHAR(cichar, p); \
628 if (cichar != digest) \
631 #define ACKCILONG(opt, neg, val) \
633 if ((len -= CILEN_LONG) < 0) \
635 GETCHAR(citype, p); \
637 if (cilen != CILEN_LONG || \
640 GETLONG(cilong, p); \
644 #define ACKCILQR(opt, neg, val) \
646 if ((len -= CILEN_LQR) < 0) \
648 GETCHAR(citype, p); \
650 if (cilen != CILEN_LQR || \
653 GETSHORT(cishort, p); \
654 if (cishort != PPP_LQR) \
656 GETLONG(cilong, p); \
661 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
662 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
664 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
665 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
666 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
667 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
668 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
669 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
672 * If there are any remaining CIs, then this packet is bad.
678 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
684 * lcp_nakci - Peer has sent a NAK for some of our CIs.
685 * This should not modify any state if the Nak is bad
686 * or if LCP is in the OPENED state.
698 lcp_options *go = &lcp_gotoptions[f->unit];
699 lcp_options *wo = &lcp_wantoptions[f->unit];
700 u_char citype, cichar, *next;
703 lcp_options no; /* options we've seen Naks for */
704 lcp_options try; /* options to request next time */
708 BZERO(&no, sizeof(no));
712 * Any Nak'd CIs must be in exactly the same order that we sent.
713 * Check packet length and CI length at each step.
714 * If we find any deviations, then this packet is bad.
716 #define NAKCIVOID(opt, neg, code) \
718 len >= CILEN_VOID && \
719 p[1] == CILEN_VOID && \
722 INCPTR(CILEN_VOID, p); \
726 #define NAKCICHAP(opt, neg, code) \
728 len >= CILEN_CHAP && \
729 p[1] == CILEN_CHAP && \
733 GETSHORT(cishort, p); \
734 GETCHAR(cichar, p); \
738 #define NAKCISHORT(opt, neg, code) \
740 len >= CILEN_SHORT && \
741 p[1] == CILEN_SHORT && \
743 len -= CILEN_SHORT; \
745 GETSHORT(cishort, p); \
749 #define NAKCILONG(opt, neg, code) \
751 len >= CILEN_LONG && \
752 p[1] == CILEN_LONG && \
756 GETLONG(cilong, p); \
760 #define NAKCILQR(opt, neg, code) \
762 len >= CILEN_LQR && \
763 p[1] == CILEN_LQR && \
767 GETSHORT(cishort, p); \
768 GETLONG(cilong, p); \
774 * We don't care if they want to send us smaller packets than
775 * we want. Therefore, accept any MRU less than what we asked for,
776 * but then ignore the new value when setting the MRU in the kernel.
777 * If they send us a bigger MRU than what we asked, accept it, up to
778 * the limit of the default MRU we'd get if we didn't negotiate.
780 if (go->neg_mru && go->mru != DEFMRU) {
781 NAKCISHORT(CI_MRU, neg_mru,
782 if (cishort <= wo->mru || cishort < DEFMRU)
788 * Add any characters they want to our (receive-side) asyncmap.
790 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
791 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
792 try.asyncmap = go->asyncmap | cilong;
797 * If they've nak'd our authentication-protocol, check whether
798 * they are proposing a different protocol, or a different
799 * hash algorithm for CHAP.
801 if ((go->neg_chap || go->neg_upap)
802 && len >= CILEN_SHORT
803 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT) {
806 GETSHORT(cishort, p);
807 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
809 * If they are asking for PAP, then they don't want to do CHAP.
810 * If we weren't asking for CHAP, then we were asking for PAP,
811 * in which case this Nak is bad.
817 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
821 * We were asking for CHAP/MD5; they must want a different
822 * algorithm. If they can't do MD5, we'll have to stop
825 if (cichar != go->chap_mdtype)
829 * Stop asking for PAP if we were asking for it.
836 * We don't recognize what they're suggesting.
837 * Stop asking for what we were asking for.
843 p += cilen - CILEN_SHORT;
848 * Peer shouldn't send Nak for protocol compression or
849 * address/control compression requests; they should send
850 * a Reject instead. If they send a Nak, treat it as a Reject.
853 NAKCISHORT(CI_AUTHTYPE, neg_upap,
859 * If they can't cope with our link quality protocol, we'll have
860 * to stop asking for LQR. We haven't got any other protocol.
861 * If they Nak the reporting period, take their value XXX ?
863 NAKCILQR(CI_QUALITY, neg_lqr,
864 if (cishort != PPP_LQR)
867 try.lqr_period = cilong;
871 * Check for a looped-back line.
873 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
874 try.magicnumber = magic();
878 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
879 try.neg_pcompression = 0;
881 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
882 try.neg_accompression = 0;
886 * There may be remaining CIs, if the peer is requesting negotiation
887 * on an option that we didn't include in our request packet.
888 * If we see an option that we requested, or one we've already seen
889 * in this packet, then this packet is bad.
890 * If we wanted to respond by starting to negotiate on the requested
891 * option(s), we could, but we don't, because except for the
892 * authentication type and quality protocol, if we are not negotiating
893 * an option, it is because we were told not to.
894 * For the authentication type, the Nak from the peer means
895 * `let me authenticate myself with you' which is a bit pointless.
896 * For the quality protocol, the Nak means `ask me to send you quality
897 * reports', but if we didn't ask for them, we don't want them.
898 * An option we don't recognize represents the peer asking to
899 * negotiate some option we don't support, so ignore it.
901 while (len > CILEN_VOID) {
904 if ((len -= cilen) < 0)
906 next = p + cilen - 2;
910 if (go->neg_mru && go->mru != DEFMRU
911 || no.neg_mru || cilen != CILEN_SHORT)
913 GETSHORT(cishort, p);
914 if (cishort < DEFMRU)
918 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF
919 || no.neg_asyncmap || cilen != CILEN_LONG)
923 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
927 if (go->neg_magicnumber || no.neg_magicnumber ||
931 case CI_PCOMPRESSION:
932 if (go->neg_pcompression || no.neg_pcompression
933 || cilen != CILEN_VOID)
936 case CI_ACCOMPRESSION:
937 if (go->neg_accompression || no.neg_accompression
938 || cilen != CILEN_VOID)
942 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
949 /* If there is still anything left, this packet is bad. */
954 * OK, the Nak is good. Now we can update state.
956 if (f->state != OPENED) {
958 if (++try.numloops >= lcp_loopbackfail) {
959 syslog(LOG_NOTICE, "Serial line is looped back.");
960 lcp_close(f->unit, "Loopback detected");
970 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
976 * lcp_rejci - Peer has Rejected some of our CIs.
977 * This should not modify any state if the Reject is bad
978 * or if LCP is in the OPENED state.
981 * 0 - Reject was bad.
982 * 1 - Reject was good.
990 lcp_options *go = &lcp_gotoptions[f->unit];
996 lcp_options try; /* options to request next time */
1001 * Any Rejected CIs must be in exactly the same order that we sent.
1002 * Check packet length and CI length at each step.
1003 * If we find any deviations, then this packet is bad.
1005 #define REJCIVOID(opt, neg) \
1007 len >= CILEN_VOID && \
1008 p[1] == CILEN_VOID && \
1010 len -= CILEN_VOID; \
1011 INCPTR(CILEN_VOID, p); \
1013 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
1015 #define REJCISHORT(opt, neg, val) \
1017 len >= CILEN_SHORT && \
1018 p[1] == CILEN_SHORT && \
1020 len -= CILEN_SHORT; \
1022 GETSHORT(cishort, p); \
1023 /* Check rejected value. */ \
1024 if (cishort != val) \
1027 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1029 #define REJCICHAP(opt, neg, val, digest) \
1031 len >= CILEN_CHAP && \
1032 p[1] == CILEN_CHAP && \
1034 len -= CILEN_CHAP; \
1036 GETSHORT(cishort, p); \
1037 GETCHAR(cichar, p); \
1038 /* Check rejected value. */ \
1039 if (cishort != val || cichar != digest) \
1043 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1045 #define REJCILONG(opt, neg, val) \
1047 len >= CILEN_LONG && \
1048 p[1] == CILEN_LONG && \
1050 len -= CILEN_LONG; \
1052 GETLONG(cilong, p); \
1053 /* Check rejected value. */ \
1054 if (cilong != val) \
1057 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1059 #define REJCILQR(opt, neg, val) \
1061 len >= CILEN_LQR && \
1062 p[1] == CILEN_LQR && \
1066 GETSHORT(cishort, p); \
1067 GETLONG(cilong, p); \
1068 /* Check rejected value. */ \
1069 if (cishort != PPP_LQR || cilong != val) \
1072 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1075 REJCISHORT(CI_MRU, neg_mru, go->mru);
1076 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1077 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1078 if (!go->neg_chap) {
1079 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1081 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1082 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1083 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1084 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1087 * If there are any remaining CIs, then this packet is bad.
1092 * Now we can update state.
1094 if (f->state != OPENED)
1099 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1100 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1101 plen, len, p - start));
1107 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1109 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1110 * appropriately. If reject_if_disagree is non-zero, doesn't return
1111 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1114 lcp_reqci(f, inp, lenp, reject_if_disagree)
1116 u_char *inp; /* Requested CIs */
1117 int *lenp; /* Length of requested CIs */
1118 int reject_if_disagree;
1120 lcp_options *go = &lcp_gotoptions[f->unit];
1121 lcp_options *ho = &lcp_hisoptions[f->unit];
1122 lcp_options *ao = &lcp_allowoptions[f->unit];
1123 u_char *cip, *next; /* Pointer to current and next CIs */
1124 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1125 u_short cishort; /* Parsed short value */
1126 u_int32_t cilong; /* Parse long value */
1127 int rc = CONFACK; /* Final packet return code */
1128 int orc; /* Individual option return code */
1129 u_char *p; /* Pointer to next char to parse */
1130 u_char *rejp; /* Pointer to next char in reject frame */
1131 u_char *nakp; /* Pointer to next char in Nak frame */
1132 int l = *lenp; /* Length left */
1135 * Reset all his options.
1137 BZERO(ho, sizeof(*ho));
1140 * Process all his options.
1146 orc = CONFACK; /* Assume success */
1147 cip = p = next; /* Remember begining of CI */
1148 if (l < 2 || /* Not enough data for CI header or */
1149 p[1] < 2 || /* CI length too small or */
1150 p[1] > l) { /* CI length too big? */
1151 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1152 orc = CONFREJ; /* Reject bad CI */
1153 cilen = l; /* Reject till end of packet */
1154 l = 0; /* Don't loop again */
1157 GETCHAR(citype, p); /* Parse CI type */
1158 GETCHAR(cilen, p); /* Parse CI length */
1159 l -= cilen; /* Adjust remaining length */
1160 next += cilen; /* Step to next CI */
1162 switch (citype) { /* Check CI type */
1164 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1165 if (!ao->neg_mru || /* Allow option? */
1166 cilen != CILEN_SHORT) { /* Check CI length */
1167 orc = CONFREJ; /* Reject CI */
1170 GETSHORT(cishort, p); /* Parse MRU */
1171 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1174 * He must be able to receive at least our minimum.
1175 * No need to check a maximum. If he sends a large number,
1176 * we'll just ignore it.
1178 if (cishort < MINMRU) {
1179 orc = CONFNAK; /* Nak CI */
1180 PUTCHAR(CI_MRU, nakp);
1181 PUTCHAR(CILEN_SHORT, nakp);
1182 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1185 ho->neg_mru = 1; /* Remember he sent MRU */
1186 ho->mru = cishort; /* And remember value */
1190 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1191 if (!ao->neg_asyncmap ||
1192 cilen != CILEN_LONG) {
1197 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1200 * Asyncmap must have set at least the bits
1201 * which are set in lcp_allowoptions[unit].asyncmap.
1203 if ((ao->asyncmap & ~cilong) != 0) {
1205 PUTCHAR(CI_ASYNCMAP, nakp);
1206 PUTCHAR(CILEN_LONG, nakp);
1207 PUTLONG(ao->asyncmap | cilong, nakp);
1210 ho->neg_asyncmap = 1;
1211 ho->asyncmap = cilong;
1215 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1216 if (cilen < CILEN_SHORT ||
1217 !(ao->neg_upap || ao->neg_chap)) {
1219 * Reject the option if we're not willing to authenticate.
1224 GETSHORT(cishort, p);
1225 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1228 * Authtype must be UPAP or CHAP.
1230 * Note: if both ao->neg_upap and ao->neg_chap are set,
1231 * and the peer sends a Configure-Request with two
1232 * authenticate-protocol requests, one for CHAP and one
1233 * for UPAP, then we will reject the second request.
1234 * Whether we end up doing CHAP or UPAP depends then on
1235 * the ordering of the CIs in the peer's Configure-Request.
1238 if (cishort == PPP_PAP) {
1239 if (ho->neg_chap || /* we've already accepted CHAP */
1240 cilen != CILEN_SHORT) {
1241 LCPDEBUG((LOG_WARNING,
1242 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1246 if (!ao->neg_upap) { /* we don't want to do PAP */
1247 orc = CONFNAK; /* NAK it and suggest CHAP */
1248 PUTCHAR(CI_AUTHTYPE, nakp);
1249 PUTCHAR(CILEN_CHAP, nakp);
1250 PUTSHORT(PPP_CHAP, nakp);
1251 PUTCHAR(ao->chap_mdtype, nakp);
1257 if (cishort == PPP_CHAP) {
1258 if (ho->neg_upap || /* we've already accepted PAP */
1259 cilen != CILEN_CHAP) {
1261 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1265 if (!ao->neg_chap) { /* we don't want to do CHAP */
1266 orc = CONFNAK; /* NAK it and suggest PAP */
1267 PUTCHAR(CI_AUTHTYPE, nakp);
1268 PUTCHAR(CILEN_SHORT, nakp);
1269 PUTSHORT(PPP_PAP, nakp);
1272 GETCHAR(cichar, p); /* get digest type*/
1273 if (cichar != ao->chap_mdtype) {
1275 PUTCHAR(CI_AUTHTYPE, nakp);
1276 PUTCHAR(CILEN_CHAP, nakp);
1277 PUTSHORT(PPP_CHAP, nakp);
1278 PUTCHAR(ao->chap_mdtype, nakp);
1281 ho->chap_mdtype = cichar; /* save md type */
1287 * We don't recognize the protocol they're asking for.
1288 * Nak it with something we're willing to do.
1289 * (At this point we know ao->neg_upap || ao->neg_chap.)
1292 PUTCHAR(CI_AUTHTYPE, nakp);
1294 PUTCHAR(CILEN_CHAP, nakp);
1295 PUTSHORT(PPP_CHAP, nakp);
1296 PUTCHAR(ao->chap_mdtype, nakp);
1298 PUTCHAR(CILEN_SHORT, nakp);
1299 PUTSHORT(PPP_PAP, nakp);
1304 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1306 cilen != CILEN_LQR) {
1311 GETSHORT(cishort, p);
1313 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1316 * Check the protocol and the reporting period.
1317 * XXX When should we Nak this, and what with?
1319 if (cishort != PPP_LQR) {
1321 PUTCHAR(CI_QUALITY, nakp);
1322 PUTCHAR(CILEN_LQR, nakp);
1323 PUTSHORT(PPP_LQR, nakp);
1324 PUTLONG(ao->lqr_period, nakp);
1329 case CI_MAGICNUMBER:
1330 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1331 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1332 cilen != CILEN_LONG) {
1337 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1340 * He must have a different magic number.
1342 if (go->neg_magicnumber &&
1343 cilong == go->magicnumber) {
1344 cilong = magic(); /* Don't put magic() inside macro! */
1346 PUTCHAR(CI_MAGICNUMBER, nakp);
1347 PUTCHAR(CILEN_LONG, nakp);
1348 PUTLONG(cilong, nakp);
1351 ho->neg_magicnumber = 1;
1352 ho->magicnumber = cilong;
1356 case CI_PCOMPRESSION:
1357 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1358 if (!ao->neg_pcompression ||
1359 cilen != CILEN_VOID) {
1363 ho->neg_pcompression = 1;
1366 case CI_ACCOMPRESSION:
1367 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1368 if (!ao->neg_accompression ||
1369 cilen != CILEN_VOID) {
1373 ho->neg_accompression = 1;
1377 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1384 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1385 if (orc == CONFACK && /* Good CI */
1386 rc != CONFACK) /* but prior CI wasnt? */
1387 continue; /* Don't send this one */
1389 if (orc == CONFNAK) { /* Nak this CI? */
1390 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1391 && citype != CI_MAGICNUMBER) {
1392 orc = CONFREJ; /* Get tough if so */
1394 if (rc == CONFREJ) /* Rejecting prior CI? */
1395 continue; /* Don't send this one */
1399 if (orc == CONFREJ) { /* Reject this CI */
1401 if (cip != rejp) /* Need to move rejected CI? */
1402 BCOPY(cip, rejp, cilen); /* Move it */
1403 INCPTR(cilen, rejp); /* Update output pointer */
1408 * If we wanted to send additional NAKs (for unsent CIs), the
1409 * code would go here. The extra NAKs would go at *nakp.
1410 * At present there are no cases where we want to ask the
1411 * peer to negotiate an option.
1420 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1422 *lenp = nakp - nak_buffer;
1423 BCOPY(nak_buffer, inp, *lenp);
1430 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1431 return (rc); /* Return final code */
1436 * lcp_up - LCP has come UP.
1442 lcp_options *wo = &lcp_wantoptions[f->unit];
1443 lcp_options *ho = &lcp_hisoptions[f->unit];
1444 lcp_options *go = &lcp_gotoptions[f->unit];
1445 lcp_options *ao = &lcp_allowoptions[f->unit];
1447 if (!go->neg_magicnumber)
1448 go->magicnumber = 0;
1449 if (!ho->neg_magicnumber)
1450 ho->magicnumber = 0;
1453 * Set our MTU to the smaller of the MTU we wanted and
1454 * the MRU our peer wanted. If we negotiated an MRU,
1455 * set our MRU to the larger of value we wanted and
1456 * the value we got in the negotiation.
1458 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1459 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1460 ho->neg_pcompression, ho->neg_accompression);
1462 * If the asyncmap hasn't been negotiated, we really should
1463 * set the receive asyncmap to ffffffff, but we set it to 0
1464 * for backwards contemptibility.
1466 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1467 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1468 go->neg_pcompression, go->neg_accompression);
1471 peer_mru[f->unit] = ho->mru;
1473 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1475 link_established(f->unit);
1480 * lcp_down - LCP has gone DOWN.
1482 * Alert other protocols.
1488 lcp_options *go = &lcp_gotoptions[f->unit];
1490 lcp_echo_lowerdown(f->unit);
1493 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1494 ppp_recv_config(f->unit, PPP_MRU,
1495 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1496 go->neg_pcompression, go->neg_accompression);
1497 peer_mru[f->unit] = PPP_MRU;
1504 * lcp_starting - LCP needs the lower layer up.
1510 link_required(f->unit);
1515 * lcp_finished - LCP has finished with the lower layer.
1521 link_terminated(f->unit);
1526 * lcp_printpkt - print the contents of an LCP packet.
1528 char *lcp_codenames[] = {
1529 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1530 "TermReq", "TermAck", "CodeRej", "ProtRej",
1531 "EchoReq", "EchoRep", "DiscReq"
1535 lcp_printpkt(p, plen, printer, arg)
1538 void (*printer) __P((void *, char *, ...));
1541 int code, id, len, olen;
1542 u_char *pstart, *optend;
1546 if (plen < HEADERLEN)
1552 if (len < HEADERLEN || len > plen)
1555 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1556 printer(arg, " %s", lcp_codenames[code-1]);
1558 printer(arg, " code=0x%x", code);
1559 printer(arg, " id=0x%x", id);
1566 /* print option list */
1571 if (olen < 2 || olen > len) {
1579 if (olen == CILEN_SHORT) {
1581 GETSHORT(cishort, p);
1582 printer(arg, "mru %d", cishort);
1586 if (olen == CILEN_LONG) {
1589 printer(arg, "asyncmap 0x%x", cilong);
1593 if (olen >= CILEN_SHORT) {
1595 printer(arg, "auth ");
1596 GETSHORT(cishort, p);
1599 printer(arg, "pap");
1602 printer(arg, "chap");
1605 printer(arg, "0x%x", cishort);
1610 if (olen >= CILEN_SHORT) {
1612 printer(arg, "quality ");
1613 GETSHORT(cishort, p);
1616 printer(arg, "lqr");
1619 printer(arg, "0x%x", cishort);
1623 case CI_MAGICNUMBER:
1624 if (olen == CILEN_LONG) {
1627 printer(arg, "magic 0x%x", cilong);
1630 case CI_PCOMPRESSION:
1631 if (olen == CILEN_VOID) {
1633 printer(arg, "pcomp");
1636 case CI_ACCOMPRESSION:
1637 if (olen == CILEN_VOID) {
1639 printer(arg, "accomp");
1643 while (p < optend) {
1645 printer(arg, " %.2x", code);
1652 /* print the rest of the bytes in the packet */
1653 for (; len > 0; --len) {
1655 printer(arg, " %.2x", code);
1662 * Time to shut down the link because there is nothing out there.
1666 void LcpLinkFailure (f)
1669 if (f->state == OPENED) {
1670 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1671 lcp_close(f->unit, "Peer not responding");
1672 phase = PHASE_TERMINATE;
1677 * Timer expired for the LCP echo requests from this process.
1686 struct ppp_idle ddinfo;
1688 * Read the time since the last packet was received.
1690 if (ioctl (fd, PPPIOCGIDLE, &ddinfo) < 0) {
1691 syslog (LOG_ERR, "ioctl(PPPIOCGIDLE): %m");
1695 * Compute the time since the last packet was received. If the timer
1696 * has expired then send the echo request and reset the timer to maximum.
1698 delta = (long int) lcp_echo_interval - (long int) ddinfo.recv_idle;
1700 LcpSendEchoRequest (f);
1701 delta = (int) lcp_echo_interval;
1704 #else /* Other implementations do not have ability to find delta */
1705 LcpSendEchoRequest (f);
1706 delta = (int) lcp_echo_interval;
1710 * Start the timer for the next interval.
1712 assert (lcp_echo_timer_running==0);
1713 TIMEOUT (LcpEchoTimeout, (caddr_t) f, (u_int32_t) delta);
1714 lcp_echo_timer_running = 1;
1718 * LcpEchoTimeout - Timer expired on the LCP echo
1722 LcpEchoTimeout (arg)
1725 if (lcp_echo_timer_running != 0) {
1726 lcp_echo_timer_running = 0;
1727 LcpEchoCheck ((fsm *) arg);
1732 * LcpEchoReply - LCP has received a reply to the echo
1736 lcp_received_echo_reply (f, id, inp, len)
1738 int id; u_char *inp; int len;
1742 /* Check the magic number - don't count replies from ourselves. */
1744 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1747 GETLONG(magic, inp);
1748 if (lcp_gotoptions[f->unit].neg_magicnumber
1749 && magic == lcp_gotoptions[f->unit].magicnumber) {
1750 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1754 /* Reset the number of outstanding echo frames */
1755 lcp_echos_pending = 0;
1759 * LcpSendEchoRequest - Send an echo request frame to the peer
1763 LcpSendEchoRequest (f)
1766 u_int32_t lcp_magic;
1767 u_char pkt[4], *pktp;
1770 * Detect the failure of the peer at this point.
1772 if (lcp_echo_fails != 0) {
1773 if (lcp_echos_pending++ >= lcp_echo_fails) {
1775 lcp_echos_pending = 0;
1779 * Make and send the echo request frame.
1781 if (f->state == OPENED) {
1782 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1784 PUTLONG(lcp_magic, pktp);
1786 fsm_sdata(f, ECHOREQ,
1787 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1792 * lcp_echo_lowerup - Start the timer for the LCP frame
1796 lcp_echo_lowerup (unit)
1799 fsm *f = &lcp_fsm[unit];
1801 /* Clear the parameters for generating echo frames */
1802 lcp_echos_pending = 0;
1803 lcp_echo_number = 0;
1804 lcp_echo_timer_running = 0;
1806 /* If a timeout interval is specified then start the timer */
1807 if (lcp_echo_interval != 0)
1810 /* If a idle time limit is given then start it */
1811 if (idle_time_limit != 0)
1812 RestartIdleTimer (f);
1817 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1821 lcp_echo_lowerdown (unit)
1824 fsm *f = &lcp_fsm[unit];
1826 if (lcp_echo_timer_running != 0) {
1827 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1828 lcp_echo_timer_running = 0;
1831 /* If a idle time limit is running then stop it */
1832 if (idle_timer_running != 0) {
1833 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1834 idle_timer_running = 0;