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.19 1995/05/19 03:25:16 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_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();
868 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
869 try.neg_pcompression = 0;
871 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
872 try.neg_accompression = 0;
876 * There may be remaining CIs, if the peer is requesting negotiation
877 * on an option that we didn't include in our request packet.
878 * If we see an option that we requested, or one we've already seen
879 * in this packet, then this packet is bad.
880 * If we wanted to respond by starting to negotiate on the requested
881 * option(s), we could, but we don't, because except for the
882 * authentication type and quality protocol, if we are not negotiating
883 * an option, it is because we were told not to.
884 * For the authentication type, the Nak from the peer means
885 * `let me authenticate myself with you' which is a bit pointless.
886 * For the quality protocol, the Nak means `ask me to send you quality
887 * reports', but if we didn't ask for them, we don't want them.
889 while (len > CILEN_VOID) {
892 if( (len -= cilen) < 0 )
894 next = p + cilen - 2;
898 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
902 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
906 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
910 if (go->neg_magicnumber || no.neg_magicnumber ||
914 case CI_PCOMPRESSION:
915 if (go->neg_pcompression || no.neg_pcompression
916 || cilen != CILEN_VOID)
919 case CI_ACCOMPRESSION:
920 if (go->neg_accompression || no.neg_accompression
921 || cilen != CILEN_VOID)
925 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
934 /* If there is still anything left, this packet is bad. */
939 * OK, the Nak is good. Now we can update state.
941 if (f->state != OPENED) {
943 if (++try.numloops >= lcp_loopbackfail) {
944 syslog(LOG_NOTICE, "Serial line is looped back.");
955 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
961 * lcp_rejci - Peer has Rejected some of our CIs.
962 * This should not modify any state if the Reject is bad
963 * or if LCP is in the OPENED state.
966 * 0 - Reject was bad.
967 * 1 - Reject was good.
975 lcp_options *go = &lcp_gotoptions[f->unit];
981 lcp_options try; /* options to request next time */
986 * Any Rejected CIs must be in exactly the same order that we sent.
987 * Check packet length and CI length at each step.
988 * If we find any deviations, then this packet is bad.
990 #define REJCIVOID(opt, neg) \
992 len >= CILEN_VOID && \
993 p[1] == CILEN_VOID && \
996 INCPTR(CILEN_VOID, p); \
998 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
1000 #define REJCISHORT(opt, neg, val) \
1002 len >= CILEN_SHORT && \
1003 p[1] == CILEN_SHORT && \
1005 len -= CILEN_SHORT; \
1007 GETSHORT(cishort, p); \
1008 /* Check rejected value. */ \
1009 if (cishort != val) \
1012 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1014 #define REJCICHAP(opt, neg, val, digest) \
1016 len >= CILEN_CHAP && \
1017 p[1] == CILEN_CHAP && \
1019 len -= CILEN_CHAP; \
1021 GETSHORT(cishort, p); \
1022 GETCHAR(cichar, p); \
1023 /* Check rejected value. */ \
1024 if (cishort != val || cichar != digest) \
1028 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1030 #define REJCILONG(opt, neg, val) \
1032 len >= CILEN_LONG && \
1033 p[1] == CILEN_LONG && \
1035 len -= CILEN_LONG; \
1037 GETLONG(cilong, p); \
1038 /* Check rejected value. */ \
1039 if (cilong != val) \
1042 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1044 #define REJCILQR(opt, neg, val) \
1046 len >= CILEN_LQR && \
1047 p[1] == CILEN_LQR && \
1051 GETSHORT(cishort, p); \
1052 GETLONG(cilong, p); \
1053 /* Check rejected value. */ \
1054 if (cishort != PPP_LQR || cilong != val) \
1057 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1060 REJCISHORT(CI_MRU, neg_mru, go->mru);
1061 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1062 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1063 if (!go->neg_chap) {
1064 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1066 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1067 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1068 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1069 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1072 * If there are any remaining CIs, then this packet is bad.
1077 * Now we can update state.
1079 if (f->state != OPENED)
1084 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1085 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1086 plen, len, p - start));
1092 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1094 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1095 * appropriately. If reject_if_disagree is non-zero, doesn't return
1096 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1099 lcp_reqci(f, inp, lenp, reject_if_disagree)
1101 u_char *inp; /* Requested CIs */
1102 int *lenp; /* Length of requested CIs */
1103 int reject_if_disagree;
1105 lcp_options *go = &lcp_gotoptions[f->unit];
1106 lcp_options *ho = &lcp_hisoptions[f->unit];
1107 lcp_options *ao = &lcp_allowoptions[f->unit];
1108 u_char *cip, *next; /* Pointer to current and next CIs */
1109 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1110 u_short cishort; /* Parsed short value */
1111 u_int32_t cilong; /* Parse long value */
1112 int rc = CONFACK; /* Final packet return code */
1113 int orc; /* Individual option return code */
1114 u_char *p; /* Pointer to next char to parse */
1115 u_char *rejp; /* Pointer to next char in reject frame */
1116 u_char *nakp; /* Pointer to next char in Nak frame */
1117 int l = *lenp; /* Length left */
1120 * Reset all his options.
1122 BZERO(ho, sizeof(*ho));
1125 * Process all his options.
1131 orc = CONFACK; /* Assume success */
1132 cip = p = next; /* Remember begining of CI */
1133 if (l < 2 || /* Not enough data for CI header or */
1134 p[1] < 2 || /* CI length too small or */
1135 p[1] > l) { /* CI length too big? */
1136 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1137 orc = CONFREJ; /* Reject bad CI */
1138 cilen = l; /* Reject till end of packet */
1139 l = 0; /* Don't loop again */
1142 GETCHAR(citype, p); /* Parse CI type */
1143 GETCHAR(cilen, p); /* Parse CI length */
1144 l -= cilen; /* Adjust remaining length */
1145 next += cilen; /* Step to next CI */
1147 switch (citype) { /* Check CI type */
1149 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1150 if (!ao->neg_mru || /* Allow option? */
1151 cilen != CILEN_SHORT) { /* Check CI length */
1152 orc = CONFREJ; /* Reject CI */
1155 GETSHORT(cishort, p); /* Parse MRU */
1156 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1159 * He must be able to receive at least our minimum.
1160 * No need to check a maximum. If he sends a large number,
1161 * we'll just ignore it.
1163 if (cishort < MINMRU) {
1164 orc = CONFNAK; /* Nak CI */
1165 PUTCHAR(CI_MRU, nakp);
1166 PUTCHAR(CILEN_SHORT, nakp);
1167 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1170 ho->neg_mru = 1; /* Remember he sent MRU */
1171 ho->mru = cishort; /* And remember value */
1175 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1176 if (!ao->neg_asyncmap ||
1177 cilen != CILEN_LONG) {
1182 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1185 * Asyncmap must have set at least the bits
1186 * which are set in lcp_allowoptions[unit].asyncmap.
1188 if ((ao->asyncmap & ~cilong) != 0) {
1190 PUTCHAR(CI_ASYNCMAP, nakp);
1191 PUTCHAR(CILEN_LONG, nakp);
1192 PUTLONG(ao->asyncmap | cilong, nakp);
1195 ho->neg_asyncmap = 1;
1196 ho->asyncmap = cilong;
1200 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1201 if (cilen < CILEN_SHORT ||
1202 !(ao->neg_upap || ao->neg_chap)) {
1204 * Reject the option if we're not willing to authenticate.
1209 GETSHORT(cishort, p);
1210 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1213 * Authtype must be UPAP or CHAP.
1215 * Note: if both ao->neg_upap and ao->neg_chap are set,
1216 * and the peer sends a Configure-Request with two
1217 * authenticate-protocol requests, one for CHAP and one
1218 * for UPAP, then we will reject the second request.
1219 * Whether we end up doing CHAP or UPAP depends then on
1220 * the ordering of the CIs in the peer's Configure-Request.
1223 if (cishort == PPP_PAP) {
1224 if (ho->neg_chap || /* we've already accepted CHAP */
1225 cilen != CILEN_SHORT) {
1226 LCPDEBUG((LOG_WARNING,
1227 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1231 if (!ao->neg_upap) { /* we don't want to do PAP */
1232 orc = CONFNAK; /* NAK it and suggest CHAP */
1233 PUTCHAR(CI_AUTHTYPE, nakp);
1234 PUTCHAR(CILEN_CHAP, nakp);
1235 PUTSHORT(PPP_CHAP, nakp);
1236 PUTCHAR(ao->chap_mdtype, nakp);
1242 if (cishort == PPP_CHAP) {
1243 if (ho->neg_upap || /* we've already accepted PAP */
1244 cilen != CILEN_CHAP) {
1246 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1250 if (!ao->neg_chap) { /* we don't want to do CHAP */
1251 orc = CONFNAK; /* NAK it and suggest PAP */
1252 PUTCHAR(CI_AUTHTYPE, nakp);
1253 PUTCHAR(CILEN_SHORT, nakp);
1254 PUTSHORT(PPP_PAP, nakp);
1257 GETCHAR(cichar, p); /* get digest type*/
1258 if (cichar != ao->chap_mdtype) {
1260 PUTCHAR(CI_AUTHTYPE, nakp);
1261 PUTCHAR(CILEN_CHAP, nakp);
1262 PUTSHORT(PPP_CHAP, nakp);
1263 PUTCHAR(ao->chap_mdtype, nakp);
1266 ho->chap_mdtype = cichar; /* save md type */
1272 * We don't recognize the protocol they're asking for.
1273 * Nak it with something we're willing to do.
1274 * (At this point we know ao->neg_upap || ao->neg_chap.)
1277 PUTCHAR(CI_AUTHTYPE, nakp);
1279 PUTCHAR(CILEN_CHAP, nakp);
1280 PUTSHORT(PPP_CHAP, nakp);
1281 PUTCHAR(ao->chap_mdtype, nakp);
1283 PUTCHAR(CILEN_SHORT, nakp);
1284 PUTSHORT(PPP_PAP, nakp);
1289 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1291 cilen != CILEN_LQR) {
1296 GETSHORT(cishort, p);
1298 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1301 * Check the protocol and the reporting period.
1302 * XXX When should we Nak this, and what with?
1304 if (cishort != PPP_LQR) {
1306 PUTCHAR(CI_QUALITY, nakp);
1307 PUTCHAR(CILEN_LQR, nakp);
1308 PUTSHORT(PPP_LQR, nakp);
1309 PUTLONG(ao->lqr_period, nakp);
1314 case CI_MAGICNUMBER:
1315 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1316 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1317 cilen != CILEN_LONG) {
1322 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1325 * He must have a different magic number.
1327 if (go->neg_magicnumber &&
1328 cilong == go->magicnumber) {
1329 cilong = magic(); /* Don't put magic() inside macro! */
1331 PUTCHAR(CI_MAGICNUMBER, nakp);
1332 PUTCHAR(CILEN_LONG, nakp);
1333 PUTLONG(cilong, nakp);
1336 ho->neg_magicnumber = 1;
1337 ho->magicnumber = cilong;
1341 case CI_PCOMPRESSION:
1342 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1343 if (!ao->neg_pcompression ||
1344 cilen != CILEN_VOID) {
1348 ho->neg_pcompression = 1;
1351 case CI_ACCOMPRESSION:
1352 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1353 if (!ao->neg_accompression ||
1354 cilen != CILEN_VOID) {
1358 ho->neg_accompression = 1;
1362 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1369 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1370 if (orc == CONFACK && /* Good CI */
1371 rc != CONFACK) /* but prior CI wasnt? */
1372 continue; /* Don't send this one */
1374 if (orc == CONFNAK) { /* Nak this CI? */
1375 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1376 && citype != CI_MAGICNUMBER) {
1377 orc = CONFREJ; /* Get tough if so */
1379 if (rc == CONFREJ) /* Rejecting prior CI? */
1380 continue; /* Don't send this one */
1384 if (orc == CONFREJ) { /* Reject this CI */
1386 if (cip != rejp) /* Need to move rejected CI? */
1387 BCOPY(cip, rejp, cilen); /* Move it */
1388 INCPTR(cilen, rejp); /* Update output pointer */
1393 * If we wanted to send additional NAKs (for unsent CIs), the
1394 * code would go here. The extra NAKs would go at *nakp.
1395 * At present there are no cases where we want to ask the
1396 * peer to negotiate an option.
1405 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1407 *lenp = nakp - nak_buffer;
1408 BCOPY(nak_buffer, inp, *lenp);
1415 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1416 return (rc); /* Return final code */
1421 * lcp_up - LCP has come UP.
1423 * Start UPAP, IPCP, etc.
1429 lcp_options *wo = &lcp_wantoptions[f->unit];
1430 lcp_options *ho = &lcp_hisoptions[f->unit];
1431 lcp_options *go = &lcp_gotoptions[f->unit];
1432 lcp_options *ao = &lcp_allowoptions[f->unit];
1434 if (!go->neg_magicnumber)
1435 go->magicnumber = 0;
1436 if (!ho->neg_magicnumber)
1437 ho->magicnumber = 0;
1440 * Set our MTU to the smaller of the MTU we wanted and
1441 * the MRU our peer wanted. If we negotiated an MRU,
1442 * set our MRU to the larger of value we wanted and
1443 * the value we got in the negotiation.
1445 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1446 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1447 ho->neg_pcompression, ho->neg_accompression);
1449 * If the asyncmap hasn't been negotiated, we really should
1450 * set the receive asyncmap to ffffffff, but we set it to 0
1451 * for backwards contemptibility.
1453 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1454 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1455 go->neg_pcompression, go->neg_accompression);
1458 peer_mru[f->unit] = ho->mru;
1460 ChapLowerUp(f->unit); /* Enable CHAP */
1461 upap_lowerup(f->unit); /* Enable UPAP */
1462 ipcp_lowerup(f->unit); /* Enable IPCP */
1463 ccp_lowerup(f->unit); /* Enable CCP */
1464 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1466 link_established(f->unit);
1471 * lcp_down - LCP has gone DOWN.
1473 * Alert other protocols.
1479 lcp_echo_lowerdown(f->unit);
1480 ccp_lowerdown(f->unit);
1481 ipcp_lowerdown(f->unit);
1482 ChapLowerDown(f->unit);
1483 upap_lowerdown(f->unit);
1486 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1487 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1488 peer_mru[f->unit] = PPP_MRU;
1495 * lcp_starting - LCP needs the lower layer up.
1501 link_required(f->unit);
1506 * lcp_finished - LCP has finished with the lower layer.
1512 link_terminated(f->unit);
1517 * lcp_printpkt - print the contents of an LCP packet.
1519 char *lcp_codenames[] = {
1520 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1521 "TermReq", "TermAck", "CodeRej", "ProtRej",
1522 "EchoReq", "EchoRep", "DiscReq"
1526 lcp_printpkt(p, plen, printer, arg)
1529 void (*printer) __P((void *, char *, ...));
1532 int code, id, len, olen;
1533 u_char *pstart, *optend;
1537 if (plen < HEADERLEN)
1543 if (len < HEADERLEN || len > plen)
1546 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1547 printer(arg, " %s", lcp_codenames[code-1]);
1549 printer(arg, " code=0x%x", code);
1550 printer(arg, " id=0x%x", id);
1557 /* print option list */
1562 if (olen < 2 || olen > len) {
1570 if (olen == CILEN_SHORT) {
1572 GETSHORT(cishort, p);
1573 printer(arg, "mru %d", cishort);
1577 if (olen == CILEN_LONG) {
1580 printer(arg, "asyncmap 0x%x", cilong);
1584 if (olen >= CILEN_SHORT) {
1586 printer(arg, "auth ");
1587 GETSHORT(cishort, p);
1590 printer(arg, "upap");
1593 printer(arg, "chap");
1596 printer(arg, "0x%x", cishort);
1601 if (olen >= CILEN_SHORT) {
1603 printer(arg, "quality ");
1604 GETSHORT(cishort, p);
1607 printer(arg, "lqr");
1610 printer(arg, "0x%x", cishort);
1614 case CI_MAGICNUMBER:
1615 if (olen == CILEN_LONG) {
1618 printer(arg, "magic 0x%x", cilong);
1621 case CI_PCOMPRESSION:
1622 if (olen == CILEN_VOID) {
1624 printer(arg, "pcomp");
1627 case CI_ACCOMPRESSION:
1628 if (olen == CILEN_VOID) {
1630 printer(arg, "accomp");
1634 while (p < optend) {
1636 printer(arg, " %.2x", code);
1643 /* print the rest of the bytes in the packet */
1644 for (; len > 0; --len) {
1646 printer(arg, " %.2x", code);
1653 * Time to shut down the link because there is nothing out there.
1657 void LcpLinkFailure (f)
1660 if (f->state == OPENED) {
1661 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1662 lcp_close(f->unit); /* Reset connection */
1667 * Timer expired for the LCP echo requests from this process.
1676 struct ppp_ddinfo ddinfo;
1679 * Read the time since the last packet was received.
1681 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1682 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1686 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1688 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1689 : ddinfo.ip_rjiffies;
1691 * Compute the time since the last packet was received. If the timer
1692 * has expired then send the echo request and reset the timer to maximum.
1694 delta = (lcp_echo_interval * HZ) - latest;
1695 if (delta < HZ || latest < 0L) {
1696 LcpSendEchoRequest (f);
1697 delta = lcp_echo_interval * HZ;
1701 #else /* Other implementations do not have ability to find delta */
1702 LcpSendEchoRequest (f);
1703 delta = lcp_echo_interval;
1707 * Start the timer for the next interval.
1709 assert (lcp_echo_timer_running==0);
1710 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1711 lcp_echo_timer_running = 1;
1715 * LcpEchoTimeout - Timer expired on the LCP echo
1719 LcpEchoTimeout (arg)
1722 if (lcp_echo_timer_running != 0) {
1723 lcp_echo_timer_running = 0;
1724 LcpEchoCheck ((fsm *) arg);
1729 * LcpEchoReply - LCP has received a reply to the echo
1733 lcp_received_echo_reply (f, id, inp, len)
1735 int id; u_char *inp; int len;
1739 /* Check the magic number - don't count replies from ourselves. */
1741 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1744 GETLONG(magic, inp);
1745 if (lcp_gotoptions[f->unit].neg_magicnumber
1746 && magic == lcp_gotoptions[f->unit].magicnumber) {
1747 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1751 /* Reset the number of outstanding echo frames */
1752 lcp_echos_pending = 0;
1756 * LcpSendEchoRequest - Send an echo request frame to the peer
1760 LcpSendEchoRequest (f)
1763 u_int32_t lcp_magic;
1764 u_char pkt[4], *pktp;
1767 * Detect the failure of the peer at this point.
1769 if (lcp_echo_fails != 0) {
1770 if (lcp_echos_pending++ >= lcp_echo_fails) {
1772 lcp_echos_pending = 0;
1776 * Make and send the echo request frame.
1778 if (f->state == OPENED) {
1779 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1780 ? lcp_gotoptions[f->unit].magicnumber
1783 PUTLONG(lcp_magic, pktp);
1785 fsm_sdata(f, ECHOREQ,
1786 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1791 * lcp_echo_lowerup - Start the timer for the LCP frame
1795 lcp_echo_lowerup (unit)
1798 fsm *f = &lcp_fsm[unit];
1800 /* Clear the parameters for generating echo frames */
1801 lcp_echos_pending = 0;
1802 lcp_echo_number = 0;
1803 lcp_echo_timer_running = 0;
1805 /* If a timeout interval is specified then start the timer */
1806 if (lcp_echo_interval != 0)
1809 /* If a idle time limit is given then start it */
1810 if (idle_time_limit != 0)
1811 RestartIdleTimer (f);
1816 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1820 lcp_echo_lowerdown (unit)
1823 fsm *f = &lcp_fsm[unit];
1825 if (lcp_echo_timer_running != 0) {
1826 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1827 lcp_echo_timer_running = 0;
1830 /* If a idle time limit is running then stop it */
1831 if (idle_timer_running != 0) {
1832 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1833 idle_timer_running = 0;