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.18 1995/04/28 06:25:23 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
46 #ifdef _linux_ /* Needs ppp ioctls */
48 #include <linux/if_ppp.h>
52 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
53 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
54 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
55 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
56 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
57 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
59 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
60 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
61 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
63 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
66 u_int32_t idle_timer_running = 0;
67 extern int idle_time_limit;
71 * Callbacks for fsm code. (CI = Configuration Information)
73 static void lcp_resetci __P((fsm *)); /* Reset our CI */
74 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
75 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
76 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
77 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
78 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
79 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
80 static void lcp_up __P((fsm *)); /* We're UP */
81 static void lcp_down __P((fsm *)); /* We're DOWN */
82 static void lcp_starting __P((fsm *)); /* We need lower layer up */
83 static void lcp_finished __P((fsm *)); /* We need lower layer down */
84 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
85 static void lcp_rprotrej __P((fsm *, u_char *, int));
88 * routines to send LCP echos to peer
91 static void lcp_echo_lowerup __P((int));
92 static void lcp_echo_lowerdown __P((int));
93 static void LcpEchoTimeout __P((caddr_t));
94 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
95 static void LcpSendEchoRequest __P((fsm *));
96 static void LcpLinkFailure __P((fsm *));
98 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
99 lcp_resetci, /* Reset our Configuration Information */
100 lcp_cilen, /* Length of our Configuration Information */
101 lcp_addci, /* Add our Configuration Information */
102 lcp_ackci, /* ACK our Configuration Information */
103 lcp_nakci, /* NAK our Configuration Information */
104 lcp_rejci, /* Reject our Configuration Information */
105 lcp_reqci, /* Request peer's Configuration Information */
106 lcp_up, /* Called when fsm reaches OPENED state */
107 lcp_down, /* Called when fsm leaves OPENED state */
108 lcp_starting, /* Called when we want the lower layer up */
109 lcp_finished, /* Called when we want the lower layer down */
110 NULL, /* Called when Protocol-Reject received */
111 NULL, /* Retransmission is necessary */
112 lcp_extcode, /* Called to handle LCP-specific codes */
113 "LCP" /* String name of protocol */
116 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
119 * Length of each type of configuration option (in octets)
122 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
123 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
124 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
125 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
127 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
128 (x) == CONFNAK ? "NAK" : "REJ")
132 * lcp_init - Initialize LCP.
138 fsm *f = &lcp_fsm[unit];
139 lcp_options *wo = &lcp_wantoptions[unit];
140 lcp_options *ao = &lcp_allowoptions[unit];
143 f->protocol = PPP_LCP;
144 f->callbacks = &lcp_callbacks;
150 wo->restart = 0; /* Set to 1 in kernels or multi-line
154 wo->neg_asyncmap = 0;
156 wo->neg_chap = 0; /* Set to 1 on server */
157 wo->neg_upap = 0; /* Set to 1 on server */
158 wo->chap_mdtype = CHAP_DIGEST_MD5;
159 wo->neg_magicnumber = 1;
160 wo->neg_pcompression = 1;
161 wo->neg_accompression = 1;
162 wo->neg_lqr = 0; /* no LQR implementation yet */
166 ao->neg_asyncmap = 1;
169 ao->chap_mdtype = CHAP_DIGEST_MD5;
171 ao->neg_magicnumber = 1;
172 ao->neg_pcompression = 1;
173 ao->neg_accompression = 1;
174 ao->neg_lqr = 0; /* no LQR implementation yet */
176 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
177 xmit_accm[unit][3] = 0x60000000;
182 * lcp_open - LCP is allowed to come up.
188 fsm *f = &lcp_fsm[unit];
189 lcp_options *wo = &lcp_wantoptions[unit];
193 f->flags |= OPT_PASSIVE;
195 f->flags |= OPT_SILENT;
201 * lcp_close - Take LCP down.
207 fsm *f = &lcp_fsm[unit];
209 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
211 * This action is not strictly according to the FSM in RFC1548,
212 * but it does mean that the program terminates if you do a
213 * lcp_close(0) in passive/silent mode when a connection hasn't
220 fsm_close(&lcp_fsm[unit]);
224 static void IdleTimeCheck __P((caddr_t));
227 * Timer expired for the LCP echo requests from this process.
235 struct ppp_ddinfo ddinfo;
238 * Read the time since the last packet was received.
240 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
241 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
245 * Choose the most recient IP activity. It may be a read or write frame
247 latest = ddinfo.ip_sjiffies < ddinfo.ip_rjiffies ? ddinfo.ip_sjiffies
248 : ddinfo.ip_rjiffies;
250 * Compute the time since the last packet was received. If the timer
251 * has expired then send the echo request and reset the timer to maximum.
253 delta = (idle_time_limit * HZ) - latest;
254 if (((int) delta < HZ || (int) latest < 0L) && f->state == OPENED) {
255 syslog (LOG_NOTICE, "No IP frames exchanged within idle time limit");
256 lcp_close(f->unit); /* Reset connection */
257 phase = PHASE_TERMINATE; /* Mark it down */
259 delta = (delta + HZ - 1) / HZ;
261 delta = (u_long) idle_time_limit;
262 assert (idle_timer_running==0);
263 TIMEOUT (IdleTimeCheck, (caddr_t) f, delta);
264 idle_timer_running = 1;
269 * IdleTimeCheck - Timer expired on the IDLE detection for IP frames
276 if (idle_timer_running != 0) {
277 idle_timer_running = 0;
278 RestartIdleTimer ((fsm *) arg);
284 * lcp_lowerup - The lower layer is up.
291 ppp_set_xaccm(unit, xmit_accm[unit]);
292 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
293 ppp_recv_config(unit, PPP_MRU, 0x00000000, 0, 0);
294 peer_mru[unit] = PPP_MRU;
295 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
297 fsm_lowerup(&lcp_fsm[unit]);
302 * lcp_lowerdown - The lower layer is down.
308 fsm_lowerdown(&lcp_fsm[unit]);
313 * lcp_input - Input LCP packet.
316 lcp_input(unit, p, len)
322 fsm *f = &lcp_fsm[unit];
323 lcp_options *go = &lcp_gotoptions[f->unit];
326 fsm_input(f, p, len);
327 if (oldstate == REQSENT && f->state == ACKSENT) {
329 * The peer will probably send us an ack soon and then
330 * immediately start sending packets with the negotiated
331 * options. So as to be ready when that happens, we set
332 * our receive side to accept packets as negotiated now.
334 ppp_recv_config(f->unit, PPP_MRU,
335 go->neg_asyncmap? go->asyncmap: 0x00000000,
336 go->neg_pcompression, go->neg_accompression);
342 * lcp_extcode - Handle a LCP-specific code.
345 lcp_extcode(f, code, id, inp, len)
355 lcp_rprotrej(f, inp, len);
359 if (f->state != OPENED)
361 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
363 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
364 fsm_sdata(f, ECHOREP, id, inp, len);
368 lcp_received_echo_reply(f, id, inp, len);
382 * lcp_rprotrej - Receive an Protocol-Reject.
384 * Figure out which protocol is rejected and inform it.
387 lcp_rprotrej(f, inp, len)
394 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
396 if (len < sizeof (u_short)) {
398 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
405 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
409 * Protocol-Reject packets received in any state other than the LCP
410 * OPENED state SHOULD be silently discarded.
412 if( f->state != OPENED ){
413 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
418 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
423 * lcp_protrej - A Protocol-Reject was received.
433 LCPDEBUG((LOG_WARNING,
434 "lcp_protrej: Received Protocol-Reject for LCP!"));
435 fsm_protreject(&lcp_fsm[unit]);
440 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
443 lcp_sprotrej(unit, p, len)
449 * Send back the protocol and the information field of the
450 * rejected packet. We only get here if LCP is in the OPENED state.
455 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
461 * lcp_resetci - Reset our CI.
467 lcp_wantoptions[f->unit].magicnumber = magic();
468 lcp_wantoptions[f->unit].numloops = 0;
469 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
470 peer_mru[f->unit] = PPP_MRU;
475 * lcp_cilen - Return length of our CI.
481 lcp_options *go = &lcp_gotoptions[f->unit];
483 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
484 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
485 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
486 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
487 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
489 * NB: we only ask for one of CHAP and UPAP, even if we will
492 return (LENCISHORT(go->neg_mru) +
493 LENCILONG(go->neg_asyncmap) +
494 LENCICHAP(go->neg_chap) +
495 LENCISHORT(!go->neg_chap && go->neg_upap) +
496 LENCILQR(go->neg_lqr) +
497 LENCILONG(go->neg_magicnumber) +
498 LENCIVOID(go->neg_pcompression) +
499 LENCIVOID(go->neg_accompression));
504 * lcp_addci - Add our desired CIs to a packet.
507 lcp_addci(f, ucp, lenp)
512 lcp_options *go = &lcp_gotoptions[f->unit];
513 u_char *start_ucp = ucp;
515 #define ADDCIVOID(opt, neg) \
518 PUTCHAR(CILEN_VOID, ucp); \
520 #define ADDCISHORT(opt, neg, val) \
523 PUTCHAR(CILEN_SHORT, ucp); \
524 PUTSHORT(val, ucp); \
526 #define ADDCICHAP(opt, neg, val, digest) \
529 PUTCHAR(CILEN_CHAP, ucp); \
530 PUTSHORT(val, ucp); \
531 PUTCHAR(digest, ucp); \
533 #define ADDCILONG(opt, neg, val) \
536 PUTCHAR(CILEN_LONG, ucp); \
539 #define ADDCILQR(opt, neg, val) \
542 PUTCHAR(CILEN_LQR, ucp); \
543 PUTSHORT(PPP_LQR, ucp); \
547 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
548 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
549 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
550 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
551 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
552 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
553 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
554 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
556 if (ucp - start_ucp != *lenp) {
557 /* this should never happen, because peer_mtu should be 1500 */
558 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
564 * lcp_ackci - Ack our CIs.
565 * This should not modify any state if the Ack is bad.
577 lcp_options *go = &lcp_gotoptions[f->unit];
578 u_char cilen, citype, cichar;
583 * CIs must be in exactly the same order that we sent.
584 * Check packet length and CI length at each step.
585 * If we find any deviations, then this packet is bad.
587 #define ACKCIVOID(opt, neg) \
589 if ((len -= CILEN_VOID) < 0) \
591 GETCHAR(citype, p); \
593 if (cilen != CILEN_VOID || \
597 #define ACKCISHORT(opt, neg, val) \
599 if ((len -= CILEN_SHORT) < 0) \
601 GETCHAR(citype, p); \
603 if (cilen != CILEN_SHORT || \
606 GETSHORT(cishort, p); \
607 if (cishort != val) \
610 #define ACKCICHAP(opt, neg, val, digest) \
612 if ((len -= CILEN_CHAP) < 0) \
614 GETCHAR(citype, p); \
616 if (cilen != CILEN_CHAP || \
619 GETSHORT(cishort, p); \
620 if (cishort != val) \
622 GETCHAR(cichar, p); \
623 if (cichar != digest) \
626 #define ACKCILONG(opt, neg, val) \
628 if ((len -= CILEN_LONG) < 0) \
630 GETCHAR(citype, p); \
632 if (cilen != CILEN_LONG || \
635 GETLONG(cilong, p); \
639 #define ACKCILQR(opt, neg, val) \
641 if ((len -= CILEN_LQR) < 0) \
643 GETCHAR(citype, p); \
645 if (cilen != CILEN_LQR || \
648 GETSHORT(cishort, p); \
649 if (cishort != PPP_LQR) \
651 GETLONG(cilong, p); \
656 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
657 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
658 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
659 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
660 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
661 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
662 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
663 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
666 * If there are any remaining CIs, then this packet is bad.
672 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
678 * lcp_nakci - Peer has sent a NAK for some of our CIs.
679 * This should not modify any state if the Nak is bad
680 * or if LCP is in the OPENED state.
692 lcp_options *go = &lcp_gotoptions[f->unit];
693 lcp_options *wo = &lcp_wantoptions[f->unit];
694 u_char citype, cichar, *next;
697 lcp_options no; /* options we've seen Naks for */
698 lcp_options try; /* options to request next time */
702 BZERO(&no, sizeof(no));
706 * Any Nak'd CIs must be in exactly the same order that we sent.
707 * Check packet length and CI length at each step.
708 * If we find any deviations, then this packet is bad.
710 #define NAKCIVOID(opt, neg, code) \
712 len >= CILEN_VOID && \
713 p[1] == CILEN_VOID && \
716 INCPTR(CILEN_VOID, p); \
720 #define NAKCICHAP(opt, neg, code) \
722 len >= CILEN_CHAP && \
723 p[1] == CILEN_CHAP && \
727 GETSHORT(cishort, p); \
728 GETCHAR(cichar, p); \
732 #define NAKCISHORT(opt, neg, code) \
734 len >= CILEN_SHORT && \
735 p[1] == CILEN_SHORT && \
737 len -= CILEN_SHORT; \
739 GETSHORT(cishort, p); \
743 #define NAKCILONG(opt, neg, code) \
745 len >= CILEN_LONG && \
746 p[1] == CILEN_LONG && \
750 GETLONG(cilong, p); \
754 #define NAKCILQR(opt, neg, code) \
756 len >= CILEN_LQR && \
757 p[1] == CILEN_LQR && \
761 GETSHORT(cishort, p); \
762 GETLONG(cilong, p); \
768 * We don't care if they want to send us smaller packets than
769 * we want. Therefore, accept any MRU less than what we asked for,
770 * but then ignore the new value when setting the MRU in the kernel.
771 * If they send us a bigger MRU than what we asked, accept it, up to
772 * the limit of the default MRU we'd get if we didn't negotiate.
774 NAKCISHORT(CI_MRU, neg_mru,
775 if (cishort <= wo->mru || cishort < DEFMRU)
780 * Add any characters they want to our (receive-side) asyncmap.
782 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
783 try.asyncmap = go->asyncmap | cilong;
787 * If they've nak'd our authentication-protocol, check whether
788 * they are proposing a different protocol, or a different
789 * hash algorithm for CHAP.
791 if ((go->neg_chap || go->neg_upap)
792 && len >= CILEN_SHORT
793 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT) {
796 GETSHORT(cishort, p);
797 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
799 * If they are asking for PAP, then they don't want to do CHAP.
800 * If we weren't asking for CHAP, then we were asking for PAP,
801 * in which case this Nak is bad.
807 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
811 * We were asking for CHAP/MD5; they must want a different
812 * algorithm. If they can't do MD5, we'll have to stop
815 if (cichar != go->chap_mdtype)
819 * Stop asking for PAP if we were asking for it.
826 * We don't recognize what they're suggesting.
827 * Stop asking for what we were asking for.
833 p += cilen - CILEN_SHORT;
838 * Peer shouldn't send Nak for protocol compression or
839 * address/control compression requests; they should send
840 * a Reject instead. If they send a Nak, treat it as a Reject.
843 NAKCISHORT(CI_AUTHTYPE, neg_upap,
849 * If they can't cope with our link quality protocol, we'll have
850 * to stop asking for LQR. We haven't got any other protocol.
851 * If they Nak the reporting period, take their value XXX ?
853 NAKCILQR(CI_QUALITY, neg_lqr,
854 if (cishort != PPP_LQR)
857 try.lqr_period = cilong;
861 * Check for a looped-back line.
863 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
864 try.magicnumber = magic();
869 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
870 try.neg_pcompression = 0;
872 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
873 try.neg_accompression = 0;
877 * There may be remaining CIs, if the peer is requesting negotiation
878 * on an option that we didn't include in our request packet.
879 * If we see an option that we requested, or one we've already seen
880 * in this packet, then this packet is bad.
881 * If we wanted to respond by starting to negotiate on the requested
882 * option(s), we could, but we don't, because except for the
883 * authentication type and quality protocol, if we are not negotiating
884 * an option, it is because we were told not to.
885 * For the authentication type, the Nak from the peer means
886 * `let me authenticate myself with you' which is a bit pointless.
887 * For the quality protocol, the Nak means `ask me to send you quality
888 * reports', but if we didn't ask for them, we don't want them.
890 while (len > CILEN_VOID) {
893 if( (len -= cilen) < 0 )
895 next = p + cilen - 2;
899 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
903 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
907 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
911 if (go->neg_magicnumber || no.neg_magicnumber ||
915 case CI_PCOMPRESSION:
916 if (go->neg_pcompression || no.neg_pcompression
917 || cilen != CILEN_VOID)
920 case CI_ACCOMPRESSION:
921 if (go->neg_accompression || no.neg_accompression
922 || cilen != CILEN_VOID)
926 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
935 /* If there is still anything left, this packet is bad. */
940 * OK, the Nak is good. Now we can update state.
942 if (f->state != OPENED) {
944 if (looped_back && try.numloops % lcp_warnloops == 0)
945 syslog(LOG_WARNING, "Serial line appears to be looped back.");
951 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
957 * lcp_rejci - Peer has Rejected some of our CIs.
958 * This should not modify any state if the Reject is bad
959 * or if LCP is in the OPENED state.
962 * 0 - Reject was bad.
963 * 1 - Reject was good.
971 lcp_options *go = &lcp_gotoptions[f->unit];
977 lcp_options try; /* options to request next time */
982 * Any Rejected CIs must be in exactly the same order that we sent.
983 * Check packet length and CI length at each step.
984 * If we find any deviations, then this packet is bad.
986 #define REJCIVOID(opt, neg) \
988 len >= CILEN_VOID && \
989 p[1] == CILEN_VOID && \
992 INCPTR(CILEN_VOID, p); \
994 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
996 #define REJCISHORT(opt, neg, val) \
998 len >= CILEN_SHORT && \
999 p[1] == CILEN_SHORT && \
1001 len -= CILEN_SHORT; \
1003 GETSHORT(cishort, p); \
1004 /* Check rejected value. */ \
1005 if (cishort != val) \
1008 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1010 #define REJCICHAP(opt, neg, val, digest) \
1012 len >= CILEN_CHAP && \
1013 p[1] == CILEN_CHAP && \
1015 len -= CILEN_CHAP; \
1017 GETSHORT(cishort, p); \
1018 GETCHAR(cichar, p); \
1019 /* Check rejected value. */ \
1020 if (cishort != val || cichar != digest) \
1024 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1026 #define REJCILONG(opt, neg, val) \
1028 len >= CILEN_LONG && \
1029 p[1] == CILEN_LONG && \
1031 len -= CILEN_LONG; \
1033 GETLONG(cilong, p); \
1034 /* Check rejected value. */ \
1035 if (cilong != val) \
1038 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1040 #define REJCILQR(opt, neg, val) \
1042 len >= CILEN_LQR && \
1043 p[1] == CILEN_LQR && \
1047 GETSHORT(cishort, p); \
1048 GETLONG(cilong, p); \
1049 /* Check rejected value. */ \
1050 if (cishort != PPP_LQR || cilong != val) \
1053 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1056 REJCISHORT(CI_MRU, neg_mru, go->mru);
1057 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1058 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1059 if (!go->neg_chap) {
1060 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1062 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1063 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1064 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1065 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1068 * If there are any remaining CIs, then this packet is bad.
1073 * Now we can update state.
1075 if (f->state != OPENED)
1080 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1081 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1082 plen, len, p - start));
1088 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1090 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1091 * appropriately. If reject_if_disagree is non-zero, doesn't return
1092 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1095 lcp_reqci(f, inp, lenp, reject_if_disagree)
1097 u_char *inp; /* Requested CIs */
1098 int *lenp; /* Length of requested CIs */
1099 int reject_if_disagree;
1101 lcp_options *go = &lcp_gotoptions[f->unit];
1102 lcp_options *ho = &lcp_hisoptions[f->unit];
1103 lcp_options *ao = &lcp_allowoptions[f->unit];
1104 u_char *cip, *next; /* Pointer to current and next CIs */
1105 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1106 u_short cishort; /* Parsed short value */
1107 u_int32_t cilong; /* Parse long value */
1108 int rc = CONFACK; /* Final packet return code */
1109 int orc; /* Individual option return code */
1110 u_char *p; /* Pointer to next char to parse */
1111 u_char *rejp; /* Pointer to next char in reject frame */
1112 u_char *nakp; /* Pointer to next char in Nak frame */
1113 int l = *lenp; /* Length left */
1116 * Reset all his options.
1118 BZERO(ho, sizeof(*ho));
1121 * Process all his options.
1127 orc = CONFACK; /* Assume success */
1128 cip = p = next; /* Remember begining of CI */
1129 if (l < 2 || /* Not enough data for CI header or */
1130 p[1] < 2 || /* CI length too small or */
1131 p[1] > l) { /* CI length too big? */
1132 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1133 orc = CONFREJ; /* Reject bad CI */
1134 cilen = l; /* Reject till end of packet */
1135 l = 0; /* Don't loop again */
1138 GETCHAR(citype, p); /* Parse CI type */
1139 GETCHAR(cilen, p); /* Parse CI length */
1140 l -= cilen; /* Adjust remaining length */
1141 next += cilen; /* Step to next CI */
1143 switch (citype) { /* Check CI type */
1145 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1146 if (!ao->neg_mru || /* Allow option? */
1147 cilen != CILEN_SHORT) { /* Check CI length */
1148 orc = CONFREJ; /* Reject CI */
1151 GETSHORT(cishort, p); /* Parse MRU */
1152 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1155 * He must be able to receive at least our minimum.
1156 * No need to check a maximum. If he sends a large number,
1157 * we'll just ignore it.
1159 if (cishort < MINMRU) {
1160 orc = CONFNAK; /* Nak CI */
1161 PUTCHAR(CI_MRU, nakp);
1162 PUTCHAR(CILEN_SHORT, nakp);
1163 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1166 ho->neg_mru = 1; /* Remember he sent MRU */
1167 ho->mru = cishort; /* And remember value */
1171 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1172 if (!ao->neg_asyncmap ||
1173 cilen != CILEN_LONG) {
1178 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1181 * Asyncmap must have set at least the bits
1182 * which are set in lcp_allowoptions[unit].asyncmap.
1184 if ((ao->asyncmap & ~cilong) != 0) {
1186 PUTCHAR(CI_ASYNCMAP, nakp);
1187 PUTCHAR(CILEN_LONG, nakp);
1188 PUTLONG(ao->asyncmap | cilong, nakp);
1191 ho->neg_asyncmap = 1;
1192 ho->asyncmap = cilong;
1196 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1197 if (cilen < CILEN_SHORT ||
1198 !(ao->neg_upap || ao->neg_chap)) {
1200 * Reject the option if we're not willing to authenticate.
1205 GETSHORT(cishort, p);
1206 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1209 * Authtype must be UPAP or CHAP.
1211 * Note: if both ao->neg_upap and ao->neg_chap are set,
1212 * and the peer sends a Configure-Request with two
1213 * authenticate-protocol requests, one for CHAP and one
1214 * for UPAP, then we will reject the second request.
1215 * Whether we end up doing CHAP or UPAP depends then on
1216 * the ordering of the CIs in the peer's Configure-Request.
1219 if (cishort == PPP_PAP) {
1220 if (ho->neg_chap || /* we've already accepted CHAP */
1221 cilen != CILEN_SHORT) {
1222 LCPDEBUG((LOG_WARNING,
1223 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1227 if (!ao->neg_upap) { /* we don't want to do PAP */
1228 orc = CONFNAK; /* NAK it and suggest CHAP */
1229 PUTCHAR(CI_AUTHTYPE, nakp);
1230 PUTCHAR(CILEN_CHAP, nakp);
1231 PUTSHORT(PPP_CHAP, nakp);
1232 PUTCHAR(ao->chap_mdtype, nakp);
1238 if (cishort == PPP_CHAP) {
1239 if (ho->neg_upap || /* we've already accepted PAP */
1240 cilen != CILEN_CHAP) {
1242 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1246 if (!ao->neg_chap) { /* we don't want to do CHAP */
1247 orc = CONFNAK; /* NAK it and suggest PAP */
1248 PUTCHAR(CI_AUTHTYPE, nakp);
1249 PUTCHAR(CILEN_SHORT, nakp);
1250 PUTSHORT(PPP_PAP, nakp);
1253 GETCHAR(cichar, p); /* get digest type*/
1254 if (cichar != ao->chap_mdtype) {
1256 PUTCHAR(CI_AUTHTYPE, nakp);
1257 PUTCHAR(CILEN_CHAP, nakp);
1258 PUTSHORT(PPP_CHAP, nakp);
1259 PUTCHAR(ao->chap_mdtype, nakp);
1262 ho->chap_mdtype = cichar; /* save md type */
1268 * We don't recognize the protocol they're asking for.
1269 * Nak it with something we're willing to do.
1270 * (At this point we know ao->neg_upap || ao->neg_chap.)
1273 PUTCHAR(CI_AUTHTYPE, nakp);
1275 PUTCHAR(CILEN_CHAP, nakp);
1276 PUTSHORT(PPP_CHAP, nakp);
1277 PUTCHAR(ao->chap_mdtype, nakp);
1279 PUTCHAR(CILEN_SHORT, nakp);
1280 PUTSHORT(PPP_PAP, nakp);
1285 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1287 cilen != CILEN_LQR) {
1292 GETSHORT(cishort, p);
1294 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1297 * Check the protocol and the reporting period.
1298 * XXX When should we Nak this, and what with?
1300 if (cishort != PPP_LQR) {
1302 PUTCHAR(CI_QUALITY, nakp);
1303 PUTCHAR(CILEN_LQR, nakp);
1304 PUTSHORT(PPP_LQR, nakp);
1305 PUTLONG(ao->lqr_period, nakp);
1310 case CI_MAGICNUMBER:
1311 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1312 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1313 cilen != CILEN_LONG) {
1318 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1321 * He must have a different magic number.
1323 if (go->neg_magicnumber &&
1324 cilong == go->magicnumber) {
1325 cilong = magic(); /* Don't put magic() inside macro! */
1327 PUTCHAR(CI_MAGICNUMBER, nakp);
1328 PUTCHAR(CILEN_LONG, nakp);
1329 PUTLONG(cilong, nakp);
1332 ho->neg_magicnumber = 1;
1333 ho->magicnumber = cilong;
1337 case CI_PCOMPRESSION:
1338 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1339 if (!ao->neg_pcompression ||
1340 cilen != CILEN_VOID) {
1344 ho->neg_pcompression = 1;
1347 case CI_ACCOMPRESSION:
1348 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1349 if (!ao->neg_accompression ||
1350 cilen != CILEN_VOID) {
1354 ho->neg_accompression = 1;
1358 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1365 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1366 if (orc == CONFACK && /* Good CI */
1367 rc != CONFACK) /* but prior CI wasnt? */
1368 continue; /* Don't send this one */
1370 if (orc == CONFNAK) { /* Nak this CI? */
1371 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1372 orc = CONFREJ; /* Get tough if so */
1374 if (rc == CONFREJ) /* Rejecting prior CI? */
1375 continue; /* Don't send this one */
1379 if (orc == CONFREJ) { /* Reject this CI */
1381 if (cip != rejp) /* Need to move rejected CI? */
1382 BCOPY(cip, rejp, cilen); /* Move it */
1383 INCPTR(cilen, rejp); /* Update output pointer */
1388 * If we wanted to send additional NAKs (for unsent CIs), the
1389 * code would go here. The extra NAKs would go at *nakp.
1390 * At present there are no cases where we want to ask the
1391 * peer to negotiate an option.
1400 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1402 *lenp = nakp - nak_buffer;
1403 BCOPY(nak_buffer, inp, *lenp);
1410 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1411 return (rc); /* Return final code */
1416 * lcp_up - LCP has come UP.
1418 * Start UPAP, IPCP, etc.
1424 lcp_options *wo = &lcp_wantoptions[f->unit];
1425 lcp_options *ho = &lcp_hisoptions[f->unit];
1426 lcp_options *go = &lcp_gotoptions[f->unit];
1427 lcp_options *ao = &lcp_allowoptions[f->unit];
1429 if (!go->neg_magicnumber)
1430 go->magicnumber = 0;
1431 if (!ho->neg_magicnumber)
1432 ho->magicnumber = 0;
1435 * Set our MTU to the smaller of the MTU we wanted and
1436 * the MRU our peer wanted. If we negotiated an MRU,
1437 * set our MRU to the larger of value we wanted and
1438 * the value we got in the negotiation.
1440 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1441 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1442 ho->neg_pcompression, ho->neg_accompression);
1444 * If the asyncmap hasn't been negotiated, we really should
1445 * set the receive asyncmap to ffffffff, but we set it to 0
1446 * for backwards contemptibility.
1448 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1449 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1450 go->neg_pcompression, go->neg_accompression);
1453 peer_mru[f->unit] = ho->mru;
1455 ChapLowerUp(f->unit); /* Enable CHAP */
1456 upap_lowerup(f->unit); /* Enable UPAP */
1457 ipcp_lowerup(f->unit); /* Enable IPCP */
1458 ccp_lowerup(f->unit); /* Enable CCP */
1459 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1461 link_established(f->unit);
1466 * lcp_down - LCP has gone DOWN.
1468 * Alert other protocols.
1474 lcp_echo_lowerdown(f->unit);
1475 ccp_lowerdown(f->unit);
1476 ipcp_lowerdown(f->unit);
1477 ChapLowerDown(f->unit);
1478 upap_lowerdown(f->unit);
1481 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1482 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1483 peer_mru[f->unit] = PPP_MRU;
1490 * lcp_starting - LCP needs the lower layer up.
1496 link_required(f->unit);
1501 * lcp_finished - LCP has finished with the lower layer.
1507 link_terminated(f->unit);
1512 * lcp_printpkt - print the contents of an LCP packet.
1514 char *lcp_codenames[] = {
1515 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1516 "TermReq", "TermAck", "CodeRej", "ProtRej",
1517 "EchoReq", "EchoRep", "DiscReq"
1521 lcp_printpkt(p, plen, printer, arg)
1524 void (*printer) __P((void *, char *, ...));
1527 int code, id, len, olen;
1528 u_char *pstart, *optend;
1532 if (plen < HEADERLEN)
1538 if (len < HEADERLEN || len > plen)
1541 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1542 printer(arg, " %s", lcp_codenames[code-1]);
1544 printer(arg, " code=0x%x", code);
1545 printer(arg, " id=0x%x", id);
1552 /* print option list */
1557 if (olen < 2 || olen > len) {
1565 if (olen == CILEN_SHORT) {
1567 GETSHORT(cishort, p);
1568 printer(arg, "mru %d", cishort);
1572 if (olen == CILEN_LONG) {
1575 printer(arg, "asyncmap 0x%x", cilong);
1579 if (olen >= CILEN_SHORT) {
1581 printer(arg, "auth ");
1582 GETSHORT(cishort, p);
1585 printer(arg, "upap");
1588 printer(arg, "chap");
1591 printer(arg, "0x%x", cishort);
1596 if (olen >= CILEN_SHORT) {
1598 printer(arg, "quality ");
1599 GETSHORT(cishort, p);
1602 printer(arg, "lqr");
1605 printer(arg, "0x%x", cishort);
1609 case CI_MAGICNUMBER:
1610 if (olen == CILEN_LONG) {
1613 printer(arg, "magic 0x%x", cilong);
1616 case CI_PCOMPRESSION:
1617 if (olen == CILEN_VOID) {
1619 printer(arg, "pcomp");
1622 case CI_ACCOMPRESSION:
1623 if (olen == CILEN_VOID) {
1625 printer(arg, "accomp");
1629 while (p < optend) {
1631 printer(arg, " %.2x", code);
1638 /* print the rest of the bytes in the packet */
1639 for (; len > 0; --len) {
1641 printer(arg, " %.2x", code);
1648 * Time to shut down the link because there is nothing out there.
1652 void LcpLinkFailure (f)
1655 if (f->state == OPENED) {
1656 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1657 lcp_close(f->unit); /* Reset connection */
1662 * Timer expired for the LCP echo requests from this process.
1671 struct ppp_ddinfo ddinfo;
1674 * Read the time since the last packet was received.
1676 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1677 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1681 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1683 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1684 : ddinfo.ip_rjiffies;
1686 * Compute the time since the last packet was received. If the timer
1687 * has expired then send the echo request and reset the timer to maximum.
1689 delta = (lcp_echo_interval * HZ) - latest;
1690 if (delta < HZ || latest < 0L) {
1691 LcpSendEchoRequest (f);
1692 delta = lcp_echo_interval * HZ;
1696 #else /* Other implementations do not have ability to find delta */
1697 LcpSendEchoRequest (f);
1698 delta = lcp_echo_interval;
1702 * Start the timer for the next interval.
1704 assert (lcp_echo_timer_running==0);
1705 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1706 lcp_echo_timer_running = 1;
1710 * LcpEchoTimeout - Timer expired on the LCP echo
1714 LcpEchoTimeout (arg)
1717 if (lcp_echo_timer_running != 0) {
1718 lcp_echo_timer_running = 0;
1719 LcpEchoCheck ((fsm *) arg);
1724 * LcpEchoReply - LCP has received a reply to the echo
1728 lcp_received_echo_reply (f, id, inp, len)
1730 int id; u_char *inp; int len;
1734 /* Check the magic number - don't count replies from ourselves. */
1736 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1739 GETLONG(magic, inp);
1740 if (lcp_gotoptions[f->unit].neg_magicnumber
1741 && magic == lcp_gotoptions[f->unit].magicnumber) {
1742 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1746 /* Reset the number of outstanding echo frames */
1747 lcp_echos_pending = 0;
1751 * LcpSendEchoRequest - Send an echo request frame to the peer
1755 LcpSendEchoRequest (f)
1758 u_int32_t lcp_magic;
1759 u_char pkt[4], *pktp;
1762 * Detect the failure of the peer at this point.
1764 if (lcp_echo_fails != 0) {
1765 if (lcp_echos_pending++ >= lcp_echo_fails) {
1767 lcp_echos_pending = 0;
1771 * Make and send the echo request frame.
1773 if (f->state == OPENED) {
1774 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1775 ? lcp_gotoptions[f->unit].magicnumber
1778 PUTLONG(lcp_magic, pktp);
1780 fsm_sdata(f, ECHOREQ,
1781 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1786 * lcp_echo_lowerup - Start the timer for the LCP frame
1790 lcp_echo_lowerup (unit)
1793 fsm *f = &lcp_fsm[unit];
1795 /* Clear the parameters for generating echo frames */
1796 lcp_echos_pending = 0;
1797 lcp_echo_number = 0;
1798 lcp_echo_timer_running = 0;
1800 /* If a timeout interval is specified then start the timer */
1801 if (lcp_echo_interval != 0)
1804 /* If a idle time limit is given then start it */
1805 if (idle_time_limit != 0)
1806 RestartIdleTimer (f);
1811 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1815 lcp_echo_lowerdown (unit)
1818 fsm *f = &lcp_fsm[unit];
1820 if (lcp_echo_timer_running != 0) {
1821 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1822 lcp_echo_timer_running = 0;
1825 /* If a idle time limit is running then stop it */
1826 if (idle_timer_running != 0) {
1827 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1828 idle_timer_running = 0;