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.21 1995/08/10 06:51:06 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_loopbackfail = DEFLOOPBACKFAIL;
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_idle ddinfo;
237 * Read the time since the last packet was received.
239 if (ioctl (fd, PPPIOCGIDLE, &ddinfo) < 0) {
240 syslog (LOG_ERR, "ioctl(PPPIOCGIDLE): %m");
244 * Compute the time since the last packet was received. If the timer
245 * has expired then disconnect the line.
247 delta = idle_time_limit - (u_long) ddinfo.recv_idle;
248 if (((int) delta <= 0L) && (f->state == OPENED)) {
249 syslog (LOG_NOTICE, "No IP frames received within idle time limit");
250 lcp_close(f->unit); /* Reset connection */
251 phase = PHASE_TERMINATE; /* Mark it down */
253 if ((int) delta <= 0L)
254 delta = (u_long) idle_time_limit;
255 assert (idle_timer_running==0);
256 TIMEOUT (IdleTimeCheck, (caddr_t) f, delta);
257 idle_timer_running = 1;
262 * IdleTimeCheck - Timer expired on the IDLE detection for IP frames
269 if (idle_timer_running != 0) {
270 idle_timer_running = 0;
271 RestartIdleTimer ((fsm *) arg);
277 * lcp_lowerup - The lower layer is up.
284 ppp_set_xaccm(unit, xmit_accm[unit]);
285 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
286 ppp_recv_config(unit, PPP_MRU, 0x00000000, 0, 0);
287 peer_mru[unit] = PPP_MRU;
288 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
290 fsm_lowerup(&lcp_fsm[unit]);
295 * lcp_lowerdown - The lower layer is down.
301 fsm_lowerdown(&lcp_fsm[unit]);
306 * lcp_input - Input LCP packet.
309 lcp_input(unit, p, len)
315 fsm *f = &lcp_fsm[unit];
316 lcp_options *go = &lcp_gotoptions[f->unit];
319 fsm_input(f, p, len);
320 if (oldstate == REQSENT && f->state == ACKSENT) {
322 * The peer will probably send us an ack soon and then
323 * immediately start sending packets with the negotiated
324 * options. So as to be ready when that happens, we set
325 * our receive side to accept packets as negotiated now.
327 ppp_recv_config(f->unit, PPP_MRU,
328 go->neg_asyncmap? go->asyncmap: 0x00000000,
329 go->neg_pcompression, go->neg_accompression);
335 * lcp_extcode - Handle a LCP-specific code.
338 lcp_extcode(f, code, id, inp, len)
348 lcp_rprotrej(f, inp, len);
352 if (f->state != OPENED)
354 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
356 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
357 fsm_sdata(f, ECHOREP, id, inp, len);
361 lcp_received_echo_reply(f, id, inp, len);
375 * lcp_rprotrej - Receive an Protocol-Reject.
377 * Figure out which protocol is rejected and inform it.
380 lcp_rprotrej(f, inp, len)
387 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
389 if (len < sizeof (u_short)) {
391 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
398 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
402 * Protocol-Reject packets received in any state other than the LCP
403 * OPENED state SHOULD be silently discarded.
405 if( f->state != OPENED ){
406 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
411 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
416 * lcp_protrej - A Protocol-Reject was received.
426 LCPDEBUG((LOG_WARNING,
427 "lcp_protrej: Received Protocol-Reject for LCP!"));
428 fsm_protreject(&lcp_fsm[unit]);
433 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
436 lcp_sprotrej(unit, p, len)
442 * Send back the protocol and the information field of the
443 * rejected packet. We only get here if LCP is in the OPENED state.
448 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
454 * lcp_resetci - Reset our CI.
460 lcp_wantoptions[f->unit].magicnumber = magic();
461 lcp_wantoptions[f->unit].numloops = 0;
462 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
463 peer_mru[f->unit] = PPP_MRU;
468 * lcp_cilen - Return length of our CI.
474 lcp_options *go = &lcp_gotoptions[f->unit];
476 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
477 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
478 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
479 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
480 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
482 * NB: we only ask for one of CHAP and UPAP, even if we will
485 return (LENCISHORT(go->neg_mru) +
486 LENCILONG(go->neg_asyncmap) +
487 LENCICHAP(go->neg_chap) +
488 LENCISHORT(!go->neg_chap && go->neg_upap) +
489 LENCILQR(go->neg_lqr) +
490 LENCILONG(go->neg_magicnumber) +
491 LENCIVOID(go->neg_pcompression) +
492 LENCIVOID(go->neg_accompression));
497 * lcp_addci - Add our desired CIs to a packet.
500 lcp_addci(f, ucp, lenp)
505 lcp_options *go = &lcp_gotoptions[f->unit];
506 u_char *start_ucp = ucp;
508 #define ADDCIVOID(opt, neg) \
511 PUTCHAR(CILEN_VOID, ucp); \
513 #define ADDCISHORT(opt, neg, val) \
516 PUTCHAR(CILEN_SHORT, ucp); \
517 PUTSHORT(val, ucp); \
519 #define ADDCICHAP(opt, neg, val, digest) \
522 PUTCHAR(CILEN_CHAP, ucp); \
523 PUTSHORT(val, ucp); \
524 PUTCHAR(digest, ucp); \
526 #define ADDCILONG(opt, neg, val) \
529 PUTCHAR(CILEN_LONG, ucp); \
532 #define ADDCILQR(opt, neg, val) \
535 PUTCHAR(CILEN_LQR, ucp); \
536 PUTSHORT(PPP_LQR, ucp); \
540 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
541 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
542 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
543 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
544 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
545 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
546 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
547 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
549 if (ucp - start_ucp != *lenp) {
550 /* this should never happen, because peer_mtu should be 1500 */
551 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
557 * lcp_ackci - Ack our CIs.
558 * This should not modify any state if the Ack is bad.
570 lcp_options *go = &lcp_gotoptions[f->unit];
571 u_char cilen, citype, cichar;
576 * CIs must be in exactly the same order that we sent.
577 * Check packet length and CI length at each step.
578 * If we find any deviations, then this packet is bad.
580 #define ACKCIVOID(opt, neg) \
582 if ((len -= CILEN_VOID) < 0) \
584 GETCHAR(citype, p); \
586 if (cilen != CILEN_VOID || \
590 #define ACKCISHORT(opt, neg, val) \
592 if ((len -= CILEN_SHORT) < 0) \
594 GETCHAR(citype, p); \
596 if (cilen != CILEN_SHORT || \
599 GETSHORT(cishort, p); \
600 if (cishort != val) \
603 #define ACKCICHAP(opt, neg, val, digest) \
605 if ((len -= CILEN_CHAP) < 0) \
607 GETCHAR(citype, p); \
609 if (cilen != CILEN_CHAP || \
612 GETSHORT(cishort, p); \
613 if (cishort != val) \
615 GETCHAR(cichar, p); \
616 if (cichar != digest) \
619 #define ACKCILONG(opt, neg, val) \
621 if ((len -= CILEN_LONG) < 0) \
623 GETCHAR(citype, p); \
625 if (cilen != CILEN_LONG || \
628 GETLONG(cilong, p); \
632 #define ACKCILQR(opt, neg, val) \
634 if ((len -= CILEN_LQR) < 0) \
636 GETCHAR(citype, p); \
638 if (cilen != CILEN_LQR || \
641 GETSHORT(cishort, p); \
642 if (cishort != PPP_LQR) \
644 GETLONG(cilong, p); \
649 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
650 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
651 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
652 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
653 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
654 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
655 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
656 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
659 * If there are any remaining CIs, then this packet is bad.
665 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
671 * lcp_nakci - Peer has sent a NAK for some of our CIs.
672 * This should not modify any state if the Nak is bad
673 * or if LCP is in the OPENED state.
685 lcp_options *go = &lcp_gotoptions[f->unit];
686 lcp_options *wo = &lcp_wantoptions[f->unit];
687 u_char citype, cichar, *next;
690 lcp_options no; /* options we've seen Naks for */
691 lcp_options try; /* options to request next time */
695 BZERO(&no, sizeof(no));
699 * Any Nak'd CIs must be in exactly the same order that we sent.
700 * Check packet length and CI length at each step.
701 * If we find any deviations, then this packet is bad.
703 #define NAKCIVOID(opt, neg, code) \
705 len >= CILEN_VOID && \
706 p[1] == CILEN_VOID && \
709 INCPTR(CILEN_VOID, p); \
713 #define NAKCICHAP(opt, neg, code) \
715 len >= CILEN_CHAP && \
716 p[1] == CILEN_CHAP && \
720 GETSHORT(cishort, p); \
721 GETCHAR(cichar, p); \
725 #define NAKCISHORT(opt, neg, code) \
727 len >= CILEN_SHORT && \
728 p[1] == CILEN_SHORT && \
730 len -= CILEN_SHORT; \
732 GETSHORT(cishort, p); \
736 #define NAKCILONG(opt, neg, code) \
738 len >= CILEN_LONG && \
739 p[1] == CILEN_LONG && \
743 GETLONG(cilong, p); \
747 #define NAKCILQR(opt, neg, code) \
749 len >= CILEN_LQR && \
750 p[1] == CILEN_LQR && \
754 GETSHORT(cishort, p); \
755 GETLONG(cilong, p); \
761 * We don't care if they want to send us smaller packets than
762 * we want. Therefore, accept any MRU less than what we asked for,
763 * but then ignore the new value when setting the MRU in the kernel.
764 * If they send us a bigger MRU than what we asked, accept it, up to
765 * the limit of the default MRU we'd get if we didn't negotiate.
767 NAKCISHORT(CI_MRU, neg_mru,
768 if (cishort <= wo->mru || cishort < DEFMRU)
773 * Add any characters they want to our (receive-side) asyncmap.
775 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
776 try.asyncmap = go->asyncmap | cilong;
780 * If they've nak'd our authentication-protocol, check whether
781 * they are proposing a different protocol, or a different
782 * hash algorithm for CHAP.
784 if ((go->neg_chap || go->neg_upap)
785 && len >= CILEN_SHORT
786 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT) {
789 GETSHORT(cishort, p);
790 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
792 * If they are asking for PAP, then they don't want to do CHAP.
793 * If we weren't asking for CHAP, then we were asking for PAP,
794 * in which case this Nak is bad.
800 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
804 * We were asking for CHAP/MD5; they must want a different
805 * algorithm. If they can't do MD5, we'll have to stop
808 if (cichar != go->chap_mdtype)
812 * Stop asking for PAP if we were asking for it.
819 * We don't recognize what they're suggesting.
820 * Stop asking for what we were asking for.
826 p += cilen - CILEN_SHORT;
831 * Peer shouldn't send Nak for protocol compression or
832 * address/control compression requests; they should send
833 * a Reject instead. If they send a Nak, treat it as a Reject.
836 NAKCISHORT(CI_AUTHTYPE, neg_upap,
842 * If they can't cope with our link quality protocol, we'll have
843 * to stop asking for LQR. We haven't got any other protocol.
844 * If they Nak the reporting period, take their value XXX ?
846 NAKCILQR(CI_QUALITY, neg_lqr,
847 if (cishort != PPP_LQR)
850 try.lqr_period = cilong;
854 * Check for a looped-back line.
856 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
857 try.magicnumber = magic();
861 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
862 try.neg_pcompression = 0;
864 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
865 try.neg_accompression = 0;
869 * There may be remaining CIs, if the peer is requesting negotiation
870 * on an option that we didn't include in our request packet.
871 * If we see an option that we requested, or one we've already seen
872 * in this packet, then this packet is bad.
873 * If we wanted to respond by starting to negotiate on the requested
874 * option(s), we could, but we don't, because except for the
875 * authentication type and quality protocol, if we are not negotiating
876 * an option, it is because we were told not to.
877 * For the authentication type, the Nak from the peer means
878 * `let me authenticate myself with you' which is a bit pointless.
879 * For the quality protocol, the Nak means `ask me to send you quality
880 * reports', but if we didn't ask for them, we don't want them.
881 * An option we don't recognize represents the peer asking to
882 * negotiate some option we don't support, so ignore it.
884 while (len > CILEN_VOID) {
887 if ((len -= cilen) < 0)
889 next = p + cilen - 2;
893 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
897 if (go->neg_asyncmap || 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.");
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.
968 lcp_options *go = &lcp_gotoptions[f->unit];
974 lcp_options try; /* options to request next time */
979 * Any Rejected CIs must be in exactly the same order that we sent.
980 * Check packet length and CI length at each step.
981 * If we find any deviations, then this packet is bad.
983 #define REJCIVOID(opt, neg) \
985 len >= CILEN_VOID && \
986 p[1] == CILEN_VOID && \
989 INCPTR(CILEN_VOID, p); \
991 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
993 #define REJCISHORT(opt, neg, val) \
995 len >= CILEN_SHORT && \
996 p[1] == CILEN_SHORT && \
998 len -= CILEN_SHORT; \
1000 GETSHORT(cishort, p); \
1001 /* Check rejected value. */ \
1002 if (cishort != val) \
1005 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1007 #define REJCICHAP(opt, neg, val, digest) \
1009 len >= CILEN_CHAP && \
1010 p[1] == CILEN_CHAP && \
1012 len -= CILEN_CHAP; \
1014 GETSHORT(cishort, p); \
1015 GETCHAR(cichar, p); \
1016 /* Check rejected value. */ \
1017 if (cishort != val || cichar != digest) \
1021 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1023 #define REJCILONG(opt, neg, val) \
1025 len >= CILEN_LONG && \
1026 p[1] == CILEN_LONG && \
1028 len -= CILEN_LONG; \
1030 GETLONG(cilong, p); \
1031 /* Check rejected value. */ \
1032 if (cilong != val) \
1035 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1037 #define REJCILQR(opt, neg, val) \
1039 len >= CILEN_LQR && \
1040 p[1] == CILEN_LQR && \
1044 GETSHORT(cishort, p); \
1045 GETLONG(cilong, p); \
1046 /* Check rejected value. */ \
1047 if (cishort != PPP_LQR || cilong != val) \
1050 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1053 REJCISHORT(CI_MRU, neg_mru, go->mru);
1054 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1055 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1056 if (!go->neg_chap) {
1057 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1059 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1060 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1061 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1062 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1065 * If there are any remaining CIs, then this packet is bad.
1070 * Now we can update state.
1072 if (f->state != OPENED)
1077 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1078 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1079 plen, len, p - start));
1085 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1087 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1088 * appropriately. If reject_if_disagree is non-zero, doesn't return
1089 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1092 lcp_reqci(f, inp, lenp, reject_if_disagree)
1094 u_char *inp; /* Requested CIs */
1095 int *lenp; /* Length of requested CIs */
1096 int reject_if_disagree;
1098 lcp_options *go = &lcp_gotoptions[f->unit];
1099 lcp_options *ho = &lcp_hisoptions[f->unit];
1100 lcp_options *ao = &lcp_allowoptions[f->unit];
1101 u_char *cip, *next; /* Pointer to current and next CIs */
1102 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1103 u_short cishort; /* Parsed short value */
1104 u_int32_t cilong; /* Parse long value */
1105 int rc = CONFACK; /* Final packet return code */
1106 int orc; /* Individual option return code */
1107 u_char *p; /* Pointer to next char to parse */
1108 u_char *rejp; /* Pointer to next char in reject frame */
1109 u_char *nakp; /* Pointer to next char in Nak frame */
1110 int l = *lenp; /* Length left */
1113 * Reset all his options.
1115 BZERO(ho, sizeof(*ho));
1118 * Process all his options.
1124 orc = CONFACK; /* Assume success */
1125 cip = p = next; /* Remember begining of CI */
1126 if (l < 2 || /* Not enough data for CI header or */
1127 p[1] < 2 || /* CI length too small or */
1128 p[1] > l) { /* CI length too big? */
1129 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1130 orc = CONFREJ; /* Reject bad CI */
1131 cilen = l; /* Reject till end of packet */
1132 l = 0; /* Don't loop again */
1135 GETCHAR(citype, p); /* Parse CI type */
1136 GETCHAR(cilen, p); /* Parse CI length */
1137 l -= cilen; /* Adjust remaining length */
1138 next += cilen; /* Step to next CI */
1140 switch (citype) { /* Check CI type */
1142 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1143 if (!ao->neg_mru || /* Allow option? */
1144 cilen != CILEN_SHORT) { /* Check CI length */
1145 orc = CONFREJ; /* Reject CI */
1148 GETSHORT(cishort, p); /* Parse MRU */
1149 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1152 * He must be able to receive at least our minimum.
1153 * No need to check a maximum. If he sends a large number,
1154 * we'll just ignore it.
1156 if (cishort < MINMRU) {
1157 orc = CONFNAK; /* Nak CI */
1158 PUTCHAR(CI_MRU, nakp);
1159 PUTCHAR(CILEN_SHORT, nakp);
1160 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1163 ho->neg_mru = 1; /* Remember he sent MRU */
1164 ho->mru = cishort; /* And remember value */
1168 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1169 if (!ao->neg_asyncmap ||
1170 cilen != CILEN_LONG) {
1175 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1178 * Asyncmap must have set at least the bits
1179 * which are set in lcp_allowoptions[unit].asyncmap.
1181 if ((ao->asyncmap & ~cilong) != 0) {
1183 PUTCHAR(CI_ASYNCMAP, nakp);
1184 PUTCHAR(CILEN_LONG, nakp);
1185 PUTLONG(ao->asyncmap | cilong, nakp);
1188 ho->neg_asyncmap = 1;
1189 ho->asyncmap = cilong;
1193 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1194 if (cilen < CILEN_SHORT ||
1195 !(ao->neg_upap || ao->neg_chap)) {
1197 * Reject the option if we're not willing to authenticate.
1202 GETSHORT(cishort, p);
1203 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1206 * Authtype must be UPAP or CHAP.
1208 * Note: if both ao->neg_upap and ao->neg_chap are set,
1209 * and the peer sends a Configure-Request with two
1210 * authenticate-protocol requests, one for CHAP and one
1211 * for UPAP, then we will reject the second request.
1212 * Whether we end up doing CHAP or UPAP depends then on
1213 * the ordering of the CIs in the peer's Configure-Request.
1216 if (cishort == PPP_PAP) {
1217 if (ho->neg_chap || /* we've already accepted CHAP */
1218 cilen != CILEN_SHORT) {
1219 LCPDEBUG((LOG_WARNING,
1220 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1224 if (!ao->neg_upap) { /* we don't want to do PAP */
1225 orc = CONFNAK; /* NAK it and suggest CHAP */
1226 PUTCHAR(CI_AUTHTYPE, nakp);
1227 PUTCHAR(CILEN_CHAP, nakp);
1228 PUTSHORT(PPP_CHAP, nakp);
1229 PUTCHAR(ao->chap_mdtype, nakp);
1235 if (cishort == PPP_CHAP) {
1236 if (ho->neg_upap || /* we've already accepted PAP */
1237 cilen != CILEN_CHAP) {
1239 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1243 if (!ao->neg_chap) { /* we don't want to do CHAP */
1244 orc = CONFNAK; /* NAK it and suggest PAP */
1245 PUTCHAR(CI_AUTHTYPE, nakp);
1246 PUTCHAR(CILEN_SHORT, nakp);
1247 PUTSHORT(PPP_PAP, nakp);
1250 GETCHAR(cichar, p); /* get digest type*/
1251 if (cichar != ao->chap_mdtype) {
1253 PUTCHAR(CI_AUTHTYPE, nakp);
1254 PUTCHAR(CILEN_CHAP, nakp);
1255 PUTSHORT(PPP_CHAP, nakp);
1256 PUTCHAR(ao->chap_mdtype, nakp);
1259 ho->chap_mdtype = cichar; /* save md type */
1265 * We don't recognize the protocol they're asking for.
1266 * Nak it with something we're willing to do.
1267 * (At this point we know ao->neg_upap || ao->neg_chap.)
1270 PUTCHAR(CI_AUTHTYPE, nakp);
1272 PUTCHAR(CILEN_CHAP, nakp);
1273 PUTSHORT(PPP_CHAP, nakp);
1274 PUTCHAR(ao->chap_mdtype, nakp);
1276 PUTCHAR(CILEN_SHORT, nakp);
1277 PUTSHORT(PPP_PAP, nakp);
1282 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1284 cilen != CILEN_LQR) {
1289 GETSHORT(cishort, p);
1291 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1294 * Check the protocol and the reporting period.
1295 * XXX When should we Nak this, and what with?
1297 if (cishort != PPP_LQR) {
1299 PUTCHAR(CI_QUALITY, nakp);
1300 PUTCHAR(CILEN_LQR, nakp);
1301 PUTSHORT(PPP_LQR, nakp);
1302 PUTLONG(ao->lqr_period, nakp);
1307 case CI_MAGICNUMBER:
1308 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1309 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1310 cilen != CILEN_LONG) {
1315 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1318 * He must have a different magic number.
1320 if (go->neg_magicnumber &&
1321 cilong == go->magicnumber) {
1322 cilong = magic(); /* Don't put magic() inside macro! */
1324 PUTCHAR(CI_MAGICNUMBER, nakp);
1325 PUTCHAR(CILEN_LONG, nakp);
1326 PUTLONG(cilong, nakp);
1329 ho->neg_magicnumber = 1;
1330 ho->magicnumber = cilong;
1334 case CI_PCOMPRESSION:
1335 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1336 if (!ao->neg_pcompression ||
1337 cilen != CILEN_VOID) {
1341 ho->neg_pcompression = 1;
1344 case CI_ACCOMPRESSION:
1345 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1346 if (!ao->neg_accompression ||
1347 cilen != CILEN_VOID) {
1351 ho->neg_accompression = 1;
1355 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1362 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1363 if (orc == CONFACK && /* Good CI */
1364 rc != CONFACK) /* but prior CI wasnt? */
1365 continue; /* Don't send this one */
1367 if (orc == CONFNAK) { /* Nak this CI? */
1368 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1369 && citype != CI_MAGICNUMBER) {
1370 orc = CONFREJ; /* Get tough if so */
1372 if (rc == CONFREJ) /* Rejecting prior CI? */
1373 continue; /* Don't send this one */
1377 if (orc == CONFREJ) { /* Reject this CI */
1379 if (cip != rejp) /* Need to move rejected CI? */
1380 BCOPY(cip, rejp, cilen); /* Move it */
1381 INCPTR(cilen, rejp); /* Update output pointer */
1386 * If we wanted to send additional NAKs (for unsent CIs), the
1387 * code would go here. The extra NAKs would go at *nakp.
1388 * At present there are no cases where we want to ask the
1389 * peer to negotiate an option.
1398 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1400 *lenp = nakp - nak_buffer;
1401 BCOPY(nak_buffer, inp, *lenp);
1408 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1409 return (rc); /* Return final code */
1414 * lcp_up - LCP has come UP.
1416 * Start UPAP, IPCP, etc.
1422 lcp_options *wo = &lcp_wantoptions[f->unit];
1423 lcp_options *ho = &lcp_hisoptions[f->unit];
1424 lcp_options *go = &lcp_gotoptions[f->unit];
1425 lcp_options *ao = &lcp_allowoptions[f->unit];
1427 if (!go->neg_magicnumber)
1428 go->magicnumber = 0;
1429 if (!ho->neg_magicnumber)
1430 ho->magicnumber = 0;
1433 * Set our MTU to the smaller of the MTU we wanted and
1434 * the MRU our peer wanted. If we negotiated an MRU,
1435 * set our MRU to the larger of value we wanted and
1436 * the value we got in the negotiation.
1438 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1439 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1440 ho->neg_pcompression, ho->neg_accompression);
1442 * If the asyncmap hasn't been negotiated, we really should
1443 * set the receive asyncmap to ffffffff, but we set it to 0
1444 * for backwards contemptibility.
1446 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1447 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1448 go->neg_pcompression, go->neg_accompression);
1451 peer_mru[f->unit] = ho->mru;
1453 ChapLowerUp(f->unit); /* Enable CHAP */
1454 upap_lowerup(f->unit); /* Enable UPAP */
1455 ipcp_lowerup(f->unit); /* Enable IPCP */
1456 ccp_lowerup(f->unit); /* Enable CCP */
1457 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1459 link_established(f->unit);
1464 * lcp_down - LCP has gone DOWN.
1466 * Alert other protocols.
1472 lcp_echo_lowerdown(f->unit);
1473 ccp_lowerdown(f->unit);
1474 ipcp_lowerdown(f->unit);
1475 ChapLowerDown(f->unit);
1476 upap_lowerdown(f->unit);
1479 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1480 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1481 peer_mru[f->unit] = PPP_MRU;
1488 * lcp_starting - LCP needs the lower layer up.
1494 link_required(f->unit);
1499 * lcp_finished - LCP has finished with the lower layer.
1505 link_terminated(f->unit);
1510 * lcp_printpkt - print the contents of an LCP packet.
1512 char *lcp_codenames[] = {
1513 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1514 "TermReq", "TermAck", "CodeRej", "ProtRej",
1515 "EchoReq", "EchoRep", "DiscReq"
1519 lcp_printpkt(p, plen, printer, arg)
1522 void (*printer) __P((void *, char *, ...));
1525 int code, id, len, olen;
1526 u_char *pstart, *optend;
1530 if (plen < HEADERLEN)
1536 if (len < HEADERLEN || len > plen)
1539 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1540 printer(arg, " %s", lcp_codenames[code-1]);
1542 printer(arg, " code=0x%x", code);
1543 printer(arg, " id=0x%x", id);
1550 /* print option list */
1555 if (olen < 2 || olen > len) {
1563 if (olen == CILEN_SHORT) {
1565 GETSHORT(cishort, p);
1566 printer(arg, "mru %d", cishort);
1570 if (olen == CILEN_LONG) {
1573 printer(arg, "asyncmap 0x%x", cilong);
1577 if (olen >= CILEN_SHORT) {
1579 printer(arg, "auth ");
1580 GETSHORT(cishort, p);
1583 printer(arg, "upap");
1586 printer(arg, "chap");
1589 printer(arg, "0x%x", cishort);
1594 if (olen >= CILEN_SHORT) {
1596 printer(arg, "quality ");
1597 GETSHORT(cishort, p);
1600 printer(arg, "lqr");
1603 printer(arg, "0x%x", cishort);
1607 case CI_MAGICNUMBER:
1608 if (olen == CILEN_LONG) {
1611 printer(arg, "magic 0x%x", cilong);
1614 case CI_PCOMPRESSION:
1615 if (olen == CILEN_VOID) {
1617 printer(arg, "pcomp");
1620 case CI_ACCOMPRESSION:
1621 if (olen == CILEN_VOID) {
1623 printer(arg, "accomp");
1627 while (p < optend) {
1629 printer(arg, " %.2x", code);
1636 /* print the rest of the bytes in the packet */
1637 for (; len > 0; --len) {
1639 printer(arg, " %.2x", code);
1646 * Time to shut down the link because there is nothing out there.
1650 void LcpLinkFailure (f)
1653 if (f->state == OPENED) {
1654 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1655 lcp_close(f->unit); /* Reset connection */
1660 * Timer expired for the LCP echo requests from this process.
1669 struct ppp_idle ddinfo;
1671 * Read the time since the last packet was received.
1673 if (ioctl (fd, PPPIOCGIDLE, &ddinfo) < 0) {
1674 syslog (LOG_ERR, "ioctl(PPPIOCGIDLE): %m");
1678 * Compute the time since the last packet was received. If the timer
1679 * has expired then send the echo request and reset the timer to maximum.
1681 delta = (long int) lcp_echo_interval - (long int) ddinfo.recv_idle;
1683 LcpSendEchoRequest (f);
1684 delta = (int) lcp_echo_interval;
1687 #else /* Other implementations do not have ability to find delta */
1688 LcpSendEchoRequest (f);
1689 delta = (int) lcp_echo_interval;
1693 * Start the timer for the next interval.
1695 assert (lcp_echo_timer_running==0);
1696 TIMEOUT (LcpEchoTimeout, (caddr_t) f, (u_int32_t) delta);
1697 lcp_echo_timer_running = 1;
1701 * LcpEchoTimeout - Timer expired on the LCP echo
1705 LcpEchoTimeout (arg)
1708 if (lcp_echo_timer_running != 0) {
1709 lcp_echo_timer_running = 0;
1710 LcpEchoCheck ((fsm *) arg);
1715 * LcpEchoReply - LCP has received a reply to the echo
1719 lcp_received_echo_reply (f, id, inp, len)
1721 int id; u_char *inp; int len;
1725 /* Check the magic number - don't count replies from ourselves. */
1727 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1730 GETLONG(magic, inp);
1731 if (lcp_gotoptions[f->unit].neg_magicnumber
1732 && magic == lcp_gotoptions[f->unit].magicnumber) {
1733 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1737 /* Reset the number of outstanding echo frames */
1738 lcp_echos_pending = 0;
1742 * LcpSendEchoRequest - Send an echo request frame to the peer
1746 LcpSendEchoRequest (f)
1749 u_int32_t lcp_magic;
1750 u_char pkt[4], *pktp;
1753 * Detect the failure of the peer at this point.
1755 if (lcp_echo_fails != 0) {
1756 if (lcp_echos_pending++ >= lcp_echo_fails) {
1758 lcp_echos_pending = 0;
1762 * Make and send the echo request frame.
1764 if (f->state == OPENED) {
1765 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1766 ? lcp_gotoptions[f->unit].magicnumber
1769 PUTLONG(lcp_magic, pktp);
1771 fsm_sdata(f, ECHOREQ,
1772 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1777 * lcp_echo_lowerup - Start the timer for the LCP frame
1781 lcp_echo_lowerup (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)
1795 /* If a idle time limit is given then start it */
1796 if (idle_time_limit != 0)
1797 RestartIdleTimer (f);
1802 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1806 lcp_echo_lowerdown (unit)
1809 fsm *f = &lcp_fsm[unit];
1811 if (lcp_echo_timer_running != 0) {
1812 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1813 lcp_echo_timer_running = 0;
1816 /* If a idle time limit is running then stop it */
1817 if (idle_timer_running != 0) {
1818 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1819 idle_timer_running = 0;