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.16 1994/12/12 22:54:59 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 <net/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 */
64 u_int32_t idle_timer_running = 0;
65 extern int idle_time_limit;
69 * Callbacks for fsm code. (CI = Configuration Information)
71 static void lcp_resetci __P((fsm *)); /* Reset our CI */
72 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
73 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
74 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
75 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
76 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
77 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
78 static void lcp_up __P((fsm *)); /* We're UP */
79 static void lcp_down __P((fsm *)); /* We're DOWN */
80 static void lcp_starting __P((fsm *)); /* We need lower layer up */
81 static void lcp_finished __P((fsm *)); /* We need lower layer down */
82 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
83 static void lcp_rprotrej __P((fsm *, u_char *, int));
86 * routines to send LCP echos to peer
89 static void lcp_echo_lowerup __P((int));
90 static void lcp_echo_lowerdown __P((int));
91 static void LcpEchoTimeout __P((caddr_t));
92 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
93 static void LcpSendEchoRequest __P((fsm *));
94 static void LcpLinkFailure __P((fsm *));
96 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
97 lcp_resetci, /* Reset our Configuration Information */
98 lcp_cilen, /* Length of our Configuration Information */
99 lcp_addci, /* Add our Configuration Information */
100 lcp_ackci, /* ACK our Configuration Information */
101 lcp_nakci, /* NAK our Configuration Information */
102 lcp_rejci, /* Reject our Configuration Information */
103 lcp_reqci, /* Request peer's Configuration Information */
104 lcp_up, /* Called when fsm reaches OPENED state */
105 lcp_down, /* Called when fsm leaves OPENED state */
106 lcp_starting, /* Called when we want the lower layer up */
107 lcp_finished, /* Called when we want the lower layer down */
108 NULL, /* Called when Protocol-Reject received */
109 NULL, /* Retransmission is necessary */
110 lcp_extcode, /* Called to handle LCP-specific codes */
111 "LCP" /* String name of protocol */
114 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
117 * Length of each type of configuration option (in octets)
120 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
121 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
122 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
123 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
125 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
126 (x) == CONFNAK ? "NAK" : "REJ")
130 * lcp_init - Initialize LCP.
136 fsm *f = &lcp_fsm[unit];
137 lcp_options *wo = &lcp_wantoptions[unit];
138 lcp_options *ao = &lcp_allowoptions[unit];
141 f->protocol = PPP_LCP;
142 f->callbacks = &lcp_callbacks;
148 wo->restart = 0; /* Set to 1 in kernels or multi-line
152 wo->neg_asyncmap = 0;
154 wo->neg_chap = 0; /* Set to 1 on server */
155 wo->neg_upap = 0; /* Set to 1 on server */
156 wo->chap_mdtype = CHAP_DIGEST_MD5;
157 wo->neg_magicnumber = 1;
158 wo->neg_pcompression = 1;
159 wo->neg_accompression = 1;
160 wo->neg_lqr = 0; /* no LQR implementation yet */
164 ao->neg_asyncmap = 1;
167 ao->chap_mdtype = CHAP_DIGEST_MD5;
169 ao->neg_magicnumber = 1;
170 ao->neg_pcompression = 1;
171 ao->neg_accompression = 1;
172 ao->neg_lqr = 0; /* no LQR implementation yet */
174 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
175 xmit_accm[unit][3] = 0x60000000;
180 * lcp_open - LCP is allowed to come up.
186 fsm *f = &lcp_fsm[unit];
187 lcp_options *wo = &lcp_wantoptions[unit];
191 f->flags |= OPT_PASSIVE;
193 f->flags |= OPT_SILENT;
199 * lcp_close - Take LCP down.
205 fsm *f = &lcp_fsm[unit];
207 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
209 * This action is not strictly according to the FSM in RFC1548,
210 * but it does mean that the program terminates if you do a
211 * lcp_close(0) in passive/silent mode when a connection hasn't
218 fsm_close(&lcp_fsm[unit]);
222 static void IdleTimeCheck __P((caddr_t));
225 * Timer expired for the LCP echo requests from this process.
233 struct ppp_ddinfo ddinfo;
236 * Read the time since the last packet was received.
238 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
239 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
243 * Choose the most recient IP activity. It may be a read or write frame
245 latest = ddinfo.ip_sjiffies < ddinfo.ip_rjiffies ? ddinfo.ip_sjiffies
246 : ddinfo.ip_rjiffies;
248 * Compute the time since the last packet was received. If the timer
249 * has expired then send the echo request and reset the timer to maximum.
251 delta = (idle_time_limit * HZ) - latest;
252 if (((int) delta < HZ || (int) latest < 0L) && f->state == OPENED) {
253 syslog (LOG_NOTICE, "No IP frames exchanged within idle time limit");
254 lcp_close(f->unit); /* Reset connection */
255 phase = PHASE_TERMINATE; /* Mark it down */
257 delta = (delta + HZ - 1) / HZ;
259 delta = (u_long) idle_time_limit;
260 assert (idle_timer_running==0);
261 TIMEOUT (IdleTimeCheck, (caddr_t) f, delta);
262 idle_timer_running = 1;
267 * IdleTimeCheck - Timer expired on the IDLE detection for IP frames
274 if (idle_timer_running != 0) {
275 idle_timer_running = 0;
276 RestartIdleTimer ((fsm *) arg);
282 * lcp_lowerup - The lower layer is up.
289 ppp_set_xaccm(unit, xmit_accm[unit]);
290 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
291 ppp_recv_config(unit, PPP_MRU, 0x00000000, 0, 0);
292 peer_mru[unit] = PPP_MRU;
293 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
295 fsm_lowerup(&lcp_fsm[unit]);
300 * lcp_lowerdown - The lower layer is down.
306 fsm_lowerdown(&lcp_fsm[unit]);
311 * lcp_input - Input LCP packet.
314 lcp_input(unit, p, len)
320 fsm *f = &lcp_fsm[unit];
321 lcp_options *go = &lcp_gotoptions[f->unit];
324 fsm_input(f, p, len);
325 if (oldstate == REQSENT && f->state == ACKSENT) {
327 * The peer will probably send us an ack soon and then
328 * immediately start sending packets with the negotiated
329 * options. So as to be ready when that happens, we set
330 * our receive side to accept packets as negotiated now.
332 ppp_recv_config(f->unit, PPP_MRU,
333 go->neg_asyncmap? go->asyncmap: 0x00000000,
334 go->neg_pcompression, go->neg_accompression);
340 * lcp_extcode - Handle a LCP-specific code.
343 lcp_extcode(f, code, id, inp, len)
353 lcp_rprotrej(f, inp, len);
357 if (f->state != OPENED)
359 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
361 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
362 fsm_sdata(f, ECHOREP, id, inp, len);
366 lcp_received_echo_reply(f, id, inp, len);
380 * lcp_rprotrej - Receive an Protocol-Reject.
382 * Figure out which protocol is rejected and inform it.
385 lcp_rprotrej(f, inp, len)
392 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
394 if (len < sizeof (u_short)) {
396 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
403 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
407 * Protocol-Reject packets received in any state other than the LCP
408 * OPENED state SHOULD be silently discarded.
410 if( f->state != OPENED ){
411 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
416 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
421 * lcp_protrej - A Protocol-Reject was received.
431 LCPDEBUG((LOG_WARNING,
432 "lcp_protrej: Received Protocol-Reject for LCP!"));
433 fsm_protreject(&lcp_fsm[unit]);
438 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
441 lcp_sprotrej(unit, p, len)
447 * Send back the protocol and the information field of the
448 * rejected packet. We only get here if LCP is in the OPENED state.
453 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
459 * lcp_resetci - Reset our CI.
465 lcp_wantoptions[f->unit].magicnumber = magic();
466 lcp_wantoptions[f->unit].numloops = 0;
467 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
468 peer_mru[f->unit] = PPP_MRU;
473 * lcp_cilen - Return length of our CI.
479 lcp_options *go = &lcp_gotoptions[f->unit];
481 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
482 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
483 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
484 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
485 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
487 * NB: we only ask for one of CHAP and UPAP, even if we will
490 return (LENCISHORT(go->neg_mru) +
491 LENCILONG(go->neg_asyncmap) +
492 LENCICHAP(go->neg_chap) +
493 LENCISHORT(!go->neg_chap && go->neg_upap) +
494 LENCILQR(go->neg_lqr) +
495 LENCILONG(go->neg_magicnumber) +
496 LENCIVOID(go->neg_pcompression) +
497 LENCIVOID(go->neg_accompression));
502 * lcp_addci - Add our desired CIs to a packet.
505 lcp_addci(f, ucp, lenp)
510 lcp_options *go = &lcp_gotoptions[f->unit];
511 u_char *start_ucp = ucp;
513 #define ADDCIVOID(opt, neg) \
516 PUTCHAR(CILEN_VOID, ucp); \
518 #define ADDCISHORT(opt, neg, val) \
521 PUTCHAR(CILEN_SHORT, ucp); \
522 PUTSHORT(val, ucp); \
524 #define ADDCICHAP(opt, neg, val, digest) \
527 PUTCHAR(CILEN_CHAP, ucp); \
528 PUTSHORT(val, ucp); \
529 PUTCHAR(digest, ucp); \
531 #define ADDCILONG(opt, neg, val) \
534 PUTCHAR(CILEN_LONG, ucp); \
537 #define ADDCILQR(opt, neg, val) \
540 PUTCHAR(CILEN_LQR, ucp); \
541 PUTSHORT(PPP_LQR, ucp); \
545 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
546 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
547 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
548 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
549 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
550 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
551 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
552 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
554 if (ucp - start_ucp != *lenp) {
555 /* this should never happen, because peer_mtu should be 1500 */
556 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
562 * lcp_ackci - Ack our CIs.
563 * This should not modify any state if the Ack is bad.
575 lcp_options *go = &lcp_gotoptions[f->unit];
576 u_char cilen, citype, cichar;
581 * CIs must be in exactly the same order that we sent.
582 * Check packet length and CI length at each step.
583 * If we find any deviations, then this packet is bad.
585 #define ACKCIVOID(opt, neg) \
587 if ((len -= CILEN_VOID) < 0) \
589 GETCHAR(citype, p); \
591 if (cilen != CILEN_VOID || \
595 #define ACKCISHORT(opt, neg, val) \
597 if ((len -= CILEN_SHORT) < 0) \
599 GETCHAR(citype, p); \
601 if (cilen != CILEN_SHORT || \
604 GETSHORT(cishort, p); \
605 if (cishort != val) \
608 #define ACKCICHAP(opt, neg, val, digest) \
610 if ((len -= CILEN_CHAP) < 0) \
612 GETCHAR(citype, p); \
614 if (cilen != CILEN_CHAP || \
617 GETSHORT(cishort, p); \
618 if (cishort != val) \
620 GETCHAR(cichar, p); \
621 if (cichar != digest) \
624 #define ACKCILONG(opt, neg, val) \
626 if ((len -= CILEN_LONG) < 0) \
628 GETCHAR(citype, p); \
630 if (cilen != CILEN_LONG || \
633 GETLONG(cilong, p); \
637 #define ACKCILQR(opt, neg, val) \
639 if ((len -= CILEN_LQR) < 0) \
641 GETCHAR(citype, p); \
643 if (cilen != CILEN_LQR || \
646 GETSHORT(cishort, p); \
647 if (cishort != PPP_LQR) \
649 GETLONG(cilong, p); \
654 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
655 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
656 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
657 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
658 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
659 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
660 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
661 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
664 * If there are any remaining CIs, then this packet is bad.
670 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
676 * lcp_nakci - Peer has sent a NAK for some of our CIs.
677 * This should not modify any state if the Nak is bad
678 * or if LCP is in the OPENED state.
690 lcp_options *go = &lcp_gotoptions[f->unit];
691 lcp_options *wo = &lcp_wantoptions[f->unit];
692 u_char cilen, citype, cichar, *next;
695 lcp_options no; /* options we've seen Naks for */
696 lcp_options try; /* options to request next time */
699 BZERO(&no, sizeof(no));
703 * Any Nak'd CIs must be in exactly the same order that we sent.
704 * Check packet length and CI length at each step.
705 * If we find any deviations, then this packet is bad.
707 #define NAKCIVOID(opt, neg, code) \
709 len >= CILEN_VOID && \
710 p[1] == CILEN_VOID && \
713 INCPTR(CILEN_VOID, p); \
717 #define NAKCICHAP(opt, neg, code) \
719 len >= CILEN_CHAP && \
720 p[1] == CILEN_CHAP && \
724 GETSHORT(cishort, p); \
725 GETCHAR(cichar, p); \
729 #define NAKCISHORT(opt, neg, code) \
731 len >= CILEN_SHORT && \
732 p[1] == CILEN_SHORT && \
734 len -= CILEN_SHORT; \
736 GETSHORT(cishort, p); \
740 #define NAKCILONG(opt, neg, code) \
742 len >= CILEN_LONG && \
743 p[1] == CILEN_LONG && \
747 GETLONG(cilong, p); \
751 #define NAKCILQR(opt, neg, code) \
753 len >= CILEN_LQR && \
754 p[1] == CILEN_LQR && \
758 GETSHORT(cishort, p); \
759 GETLONG(cilong, p); \
765 * We don't care if they want to send us smaller packets than
766 * we want. Therefore, accept any MRU less than what we asked for,
767 * but then ignore the new value when setting the MRU in the kernel.
768 * If they send us a bigger MRU than what we asked, accept it, up to
769 * the limit of the default MRU we'd get if we didn't negotiate.
771 NAKCISHORT(CI_MRU, neg_mru,
772 if (cishort <= wo->mru || cishort < DEFMRU)
776 * Add any characters they want to our (receive-side) asyncmap.
778 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
779 try.asyncmap = go->asyncmap | cilong;
782 * If they can't cope with our CHAP hash algorithm, we'll have
783 * to stop asking for CHAP. We haven't got any other algorithm.
785 NAKCICHAP(CI_AUTHTYPE, neg_chap,
789 * Peer shouldn't send Nak for UPAP, protocol compression or
790 * address/control compression requests; they should send
791 * a Reject instead. If they send a Nak, treat it as a Reject.
794 NAKCISHORT(CI_AUTHTYPE, neg_upap,
799 * If they can't cope with our link quality protocol, we'll have
800 * to stop asking for LQR. We haven't got any other protocol.
801 * If they Nak the reporting period, take their value XXX ?
803 NAKCILQR(CI_QUALITY, neg_lqr,
804 if (cishort != PPP_LQR)
807 try.lqr_period = cilong;
810 * Check for a looped-back line.
812 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
813 try.magicnumber = magic();
818 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
819 try.neg_pcompression = 0;
821 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
822 try.neg_accompression = 0;
826 * There may be remaining CIs, if the peer is requesting negotiation
827 * on an option that we didn't include in our request packet.
828 * If we see an option that we requested, or one we've already seen
829 * in this packet, then this packet is bad.
830 * If we wanted to respond by starting to negotiate on the requested
831 * option(s), we could, but we don't, because except for the
832 * authentication type and quality protocol, if we are not negotiating
833 * an option, it is because we were told not to.
834 * For the authentication type, the Nak from the peer means
835 * `let me authenticate myself with you' which is a bit pointless.
836 * For the quality protocol, the Nak means `ask me to send you quality
837 * reports', but if we didn't ask for them, we don't want them.
839 while (len > CILEN_VOID) {
842 if( (len -= cilen) < 0 )
844 next = p + cilen - 2;
848 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
852 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
856 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
860 if (go->neg_magicnumber || no.neg_magicnumber ||
864 case CI_PCOMPRESSION:
865 if (go->neg_pcompression || no.neg_pcompression
866 || cilen != CILEN_VOID)
869 case CI_ACCOMPRESSION:
870 if (go->neg_accompression || no.neg_accompression
871 || cilen != CILEN_VOID)
875 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
884 /* If there is still anything left, this packet is bad. */
889 * OK, the Nak is good. Now we can update state.
891 if (f->state != OPENED) {
893 if (looped_back && try.numloops % lcp_warnloops == 0)
894 syslog(LOG_WARNING, "Serial line appears to be looped back.");
900 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
906 * lcp_rejci - Peer has Rejected some of our CIs.
907 * This should not modify any state if the Reject is bad
908 * or if LCP is in the OPENED state.
911 * 0 - Reject was bad.
912 * 1 - Reject was good.
920 lcp_options *go = &lcp_gotoptions[f->unit];
926 lcp_options try; /* options to request next time */
931 * Any Rejected CIs must be in exactly the same order that we sent.
932 * Check packet length and CI length at each step.
933 * If we find any deviations, then this packet is bad.
935 #define REJCIVOID(opt, neg) \
937 len >= CILEN_VOID && \
938 p[1] == CILEN_VOID && \
941 INCPTR(CILEN_VOID, p); \
943 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
945 #define REJCISHORT(opt, neg, val) \
947 len >= CILEN_SHORT && \
948 p[1] == CILEN_SHORT && \
950 len -= CILEN_SHORT; \
952 GETSHORT(cishort, p); \
953 /* Check rejected value. */ \
954 if (cishort != val) \
957 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
959 #define REJCICHAP(opt, neg, val, digest) \
961 len >= CILEN_CHAP && \
962 p[1] == CILEN_CHAP && \
966 GETSHORT(cishort, p); \
967 GETCHAR(cichar, p); \
968 /* Check rejected value. */ \
969 if (cishort != val || cichar != digest) \
972 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
974 #define REJCILONG(opt, neg, val) \
976 len >= CILEN_LONG && \
977 p[1] == CILEN_LONG && \
981 GETLONG(cilong, p); \
982 /* Check rejected value. */ \
986 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
988 #define REJCILQR(opt, neg, val) \
990 len >= CILEN_LQR && \
991 p[1] == CILEN_LQR && \
995 GETSHORT(cishort, p); \
996 GETLONG(cilong, p); \
997 /* Check rejected value. */ \
998 if (cishort != PPP_LQR || cilong != val) \
1001 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1004 REJCISHORT(CI_MRU, neg_mru, go->mru);
1005 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1006 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1007 if (!go->neg_chap) {
1008 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1010 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1011 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1012 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1013 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1016 * If there are any remaining CIs, then this packet is bad.
1021 * Now we can update state.
1023 if (f->state != OPENED)
1028 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1029 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1030 plen, len, p - start));
1036 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1038 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1039 * appropriately. If reject_if_disagree is non-zero, doesn't return
1040 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1043 lcp_reqci(f, inp, lenp, reject_if_disagree)
1045 u_char *inp; /* Requested CIs */
1046 int *lenp; /* Length of requested CIs */
1047 int reject_if_disagree;
1049 lcp_options *go = &lcp_gotoptions[f->unit];
1050 lcp_options *ho = &lcp_hisoptions[f->unit];
1051 lcp_options *ao = &lcp_allowoptions[f->unit];
1052 u_char *cip, *next; /* Pointer to current and next CIs */
1053 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1054 u_short cishort; /* Parsed short value */
1055 u_int32_t cilong; /* Parse long value */
1056 int rc = CONFACK; /* Final packet return code */
1057 int orc; /* Individual option return code */
1058 u_char *p; /* Pointer to next char to parse */
1059 u_char *ucp = inp; /* Pointer to current output char */
1060 int l = *lenp; /* Length left */
1063 * Reset all his options.
1065 BZERO(ho, sizeof(*ho));
1068 * Process all his options.
1072 orc = CONFACK; /* Assume success */
1073 cip = p = next; /* Remember begining of CI */
1074 if (l < 2 || /* Not enough data for CI header or */
1075 p[1] < 2 || /* CI length too small or */
1076 p[1] > l) { /* CI length too big? */
1077 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1078 orc = CONFREJ; /* Reject bad CI */
1079 cilen = l; /* Reject till end of packet */
1080 l = 0; /* Don't loop again */
1083 GETCHAR(citype, p); /* Parse CI type */
1084 GETCHAR(cilen, p); /* Parse CI length */
1085 l -= cilen; /* Adjust remaining length */
1086 next += cilen; /* Step to next CI */
1088 switch (citype) { /* Check CI type */
1090 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1091 if (!ao->neg_mru || /* Allow option? */
1092 cilen != CILEN_SHORT) { /* Check CI length */
1093 orc = CONFREJ; /* Reject CI */
1096 GETSHORT(cishort, p); /* Parse MRU */
1097 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1100 * He must be able to receive at least our minimum.
1101 * No need to check a maximum. If he sends a large number,
1102 * we'll just ignore it.
1104 if (cishort < MINMRU) {
1105 orc = CONFNAK; /* Nak CI */
1106 if( !reject_if_disagree ){
1107 DECPTR(sizeof (short), p); /* Backup */
1108 PUTSHORT(MINMRU, p); /* Give him a hint */
1112 ho->neg_mru = 1; /* Remember he sent MRU */
1113 ho->mru = cishort; /* And remember value */
1117 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1118 if (!ao->neg_asyncmap ||
1119 cilen != CILEN_LONG) {
1124 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1127 * Asyncmap must have set at least the bits
1128 * which are set in lcp_allowoptions[unit].asyncmap.
1130 if ((ao->asyncmap & ~cilong) != 0) {
1132 if( !reject_if_disagree ){
1133 DECPTR(sizeof(u_int32_t), p);
1134 PUTLONG(ao->asyncmap | cilong, p);
1138 ho->neg_asyncmap = 1;
1139 ho->asyncmap = cilong;
1143 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1144 if (cilen < CILEN_SHORT ||
1145 !(ao->neg_upap || ao->neg_chap)) {
1149 GETSHORT(cishort, p);
1150 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1153 * Authtype must be UPAP or CHAP.
1155 * Note: if both ao->neg_upap and ao->neg_chap are set,
1156 * and the peer sends a Configure-Request with two
1157 * authenticate-protocol requests, one for CHAP and one
1158 * for UPAP, then we will reject the second request.
1159 * Whether we end up doing CHAP or UPAP depends then on
1160 * the ordering of the CIs in the peer's Configure-Request.
1163 if (cishort == PPP_PAP) {
1164 if (!ao->neg_upap || /* we don't want to do PAP */
1165 ho->neg_chap || /* or we've already accepted CHAP */
1166 cilen != CILEN_SHORT) {
1167 LCPDEBUG((LOG_WARNING,
1168 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1175 if (cishort == PPP_CHAP) {
1176 if (!ao->neg_chap || /* we don't want to do CHAP */
1177 ho->neg_upap || /* or we've already accepted UPAP */
1178 cilen != CILEN_CHAP) {
1180 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1184 GETCHAR(cichar, p); /* get digest type*/
1185 if (cichar != ao->chap_mdtype) {
1187 if( !reject_if_disagree ){
1188 DECPTR(sizeof (u_char), p);
1189 PUTCHAR(ao->chap_mdtype, p);
1193 ho->chap_mdtype = cichar; /* save md type */
1199 * We don't recognize the protocol they're asking for.
1206 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1208 cilen != CILEN_LQR) {
1213 GETSHORT(cishort, p);
1215 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1216 if (cishort != PPP_LQR) {
1222 * Check the reporting period.
1223 * XXX When should we Nak this, and what with?
1227 case CI_MAGICNUMBER:
1228 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1229 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1230 cilen != CILEN_LONG) {
1235 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1238 * He must have a different magic number.
1240 if (go->neg_magicnumber &&
1241 cilong == go->magicnumber) {
1243 DECPTR(sizeof(u_int32_t), p);
1244 cilong = magic(); /* Don't put magic() inside macro! */
1248 ho->neg_magicnumber = 1;
1249 ho->magicnumber = cilong;
1253 case CI_PCOMPRESSION:
1254 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1255 if (!ao->neg_pcompression ||
1256 cilen != CILEN_VOID) {
1260 ho->neg_pcompression = 1;
1263 case CI_ACCOMPRESSION:
1264 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1265 if (!ao->neg_accompression ||
1266 cilen != CILEN_VOID) {
1270 ho->neg_accompression = 1;
1274 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1281 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1282 if (orc == CONFACK && /* Good CI */
1283 rc != CONFACK) /* but prior CI wasnt? */
1284 continue; /* Don't send this one */
1286 if (orc == CONFNAK) { /* Nak this CI? */
1287 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1288 orc = CONFREJ; /* Get tough if so */
1290 if (rc == CONFREJ) /* Rejecting prior CI? */
1291 continue; /* Don't send this one */
1292 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1293 rc = CONFNAK; /* Not anymore... */
1294 ucp = inp; /* Backup */
1298 if (orc == CONFREJ && /* Reject this CI */
1299 rc != CONFREJ) { /* but no prior ones? */
1301 ucp = inp; /* Backup */
1303 if (ucp != cip) /* Need to move CI? */
1304 BCOPY(cip, ucp, cilen); /* Move it */
1305 INCPTR(cilen, ucp); /* Update output pointer */
1309 * If we wanted to send additional NAKs (for unsent CIs), the
1310 * code would go here. This must be done with care since it might
1311 * require a longer packet than we received. At present there
1312 * are no cases where we want to ask the peer to negotiate an option.
1315 *lenp = ucp - inp; /* Compute output length */
1316 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1317 return (rc); /* Return final code */
1322 * lcp_up - LCP has come UP.
1324 * Start UPAP, IPCP, etc.
1330 lcp_options *wo = &lcp_wantoptions[f->unit];
1331 lcp_options *ho = &lcp_hisoptions[f->unit];
1332 lcp_options *go = &lcp_gotoptions[f->unit];
1333 lcp_options *ao = &lcp_allowoptions[f->unit];
1335 if (!go->neg_magicnumber)
1336 go->magicnumber = 0;
1337 if (!ho->neg_magicnumber)
1338 ho->magicnumber = 0;
1341 * Set our MTU to the smaller of the MTU we wanted and
1342 * the MRU our peer wanted. If we negotiated an MRU,
1343 * set our MRU to the larger of value we wanted and
1344 * the value we got in the negotiation.
1346 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1347 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1348 ho->neg_pcompression, ho->neg_accompression);
1350 * If the asyncmap hasn't been negotiated, we really should
1351 * set the receive asyncmap to ffffffff, but we set it to 0
1352 * for backwards contemptibility.
1354 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1355 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1356 go->neg_pcompression, go->neg_accompression);
1359 peer_mru[f->unit] = ho->mru;
1361 ChapLowerUp(f->unit); /* Enable CHAP */
1362 upap_lowerup(f->unit); /* Enable UPAP */
1363 ipcp_lowerup(f->unit); /* Enable IPCP */
1364 ccp_lowerup(f->unit); /* Enable CCP */
1365 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1367 link_established(f->unit);
1372 * lcp_down - LCP has gone DOWN.
1374 * Alert other protocols.
1380 lcp_echo_lowerdown(f->unit);
1381 ccp_lowerdown(f->unit);
1382 ipcp_lowerdown(f->unit);
1383 ChapLowerDown(f->unit);
1384 upap_lowerdown(f->unit);
1387 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1388 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1389 peer_mru[f->unit] = PPP_MRU;
1396 * lcp_starting - LCP needs the lower layer up.
1402 link_required(f->unit);
1407 * lcp_finished - LCP has finished with the lower layer.
1413 link_terminated(f->unit);
1418 * lcp_printpkt - print the contents of an LCP packet.
1420 char *lcp_codenames[] = {
1421 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1422 "TermReq", "TermAck", "CodeRej", "ProtRej",
1423 "EchoReq", "EchoRep", "DiscReq"
1427 lcp_printpkt(p, plen, printer, arg)
1430 void (*printer) __P((void *, char *, ...));
1433 int code, id, len, olen;
1434 u_char *pstart, *optend;
1438 if (plen < HEADERLEN)
1444 if (len < HEADERLEN || len > plen)
1447 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1448 printer(arg, " %s", lcp_codenames[code-1]);
1450 printer(arg, " code=0x%x", code);
1451 printer(arg, " id=0x%x", id);
1458 /* print option list */
1463 if (olen < 2 || olen > len) {
1471 if (olen == CILEN_SHORT) {
1473 GETSHORT(cishort, p);
1474 printer(arg, "mru %d", cishort);
1478 if (olen == CILEN_LONG) {
1481 printer(arg, "asyncmap 0x%x", cilong);
1485 if (olen >= CILEN_SHORT) {
1487 printer(arg, "auth ");
1488 GETSHORT(cishort, p);
1491 printer(arg, "upap");
1494 printer(arg, "chap");
1497 printer(arg, "0x%x", cishort);
1502 if (olen >= CILEN_SHORT) {
1504 printer(arg, "quality ");
1505 GETSHORT(cishort, p);
1508 printer(arg, "lqr");
1511 printer(arg, "0x%x", cishort);
1515 case CI_MAGICNUMBER:
1516 if (olen == CILEN_LONG) {
1519 printer(arg, "magic 0x%x", cilong);
1522 case CI_PCOMPRESSION:
1523 if (olen == CILEN_VOID) {
1525 printer(arg, "pcomp");
1528 case CI_ACCOMPRESSION:
1529 if (olen == CILEN_VOID) {
1531 printer(arg, "accomp");
1535 while (p < optend) {
1537 printer(arg, " %.2x", code);
1544 /* print the rest of the bytes in the packet */
1545 for (; len > 0; --len) {
1547 printer(arg, " %.2x", code);
1554 * Time to shut down the link because there is nothing out there.
1558 void LcpLinkFailure (f)
1561 if (f->state == OPENED) {
1562 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1563 lcp_close(f->unit); /* Reset connection */
1568 * Timer expired for the LCP echo requests from this process.
1577 struct ppp_ddinfo ddinfo;
1580 * Read the time since the last packet was received.
1582 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1583 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1587 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1589 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1590 : ddinfo.ip_rjiffies;
1592 * Compute the time since the last packet was received. If the timer
1593 * has expired then send the echo request and reset the timer to maximum.
1595 delta = (lcp_echo_interval * HZ) - latest;
1596 if (delta < HZ || latest < 0L) {
1597 LcpSendEchoRequest (f);
1598 delta = lcp_echo_interval * HZ;
1602 #else /* Other implementations do not have ability to find delta */
1603 LcpSendEchoRequest (f);
1604 delta = lcp_echo_interval;
1608 * Start the timer for the next interval.
1610 assert (lcp_echo_timer_running==0);
1611 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1612 lcp_echo_timer_running = 1;
1616 * LcpEchoTimeout - Timer expired on the LCP echo
1620 LcpEchoTimeout (arg)
1623 if (lcp_echo_timer_running != 0) {
1624 lcp_echo_timer_running = 0;
1625 LcpEchoCheck ((fsm *) arg);
1630 * LcpEchoReply - LCP has received a reply to the echo
1634 lcp_received_echo_reply (f, id, inp, len)
1636 int id; u_char *inp; int len;
1640 /* Check the magic number - don't count replies from ourselves. */
1642 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1645 GETLONG(magic, inp);
1646 if (lcp_gotoptions[f->unit].neg_magicnumber
1647 && magic == lcp_gotoptions[f->unit].magicnumber) {
1648 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1652 /* Reset the number of outstanding echo frames */
1653 lcp_echos_pending = 0;
1657 * LcpSendEchoRequest - Send an echo request frame to the peer
1661 LcpSendEchoRequest (f)
1664 u_int32_t lcp_magic;
1665 u_char pkt[4], *pktp;
1668 * Detect the failure of the peer at this point.
1670 if (lcp_echo_fails != 0) {
1671 if (lcp_echos_pending++ >= lcp_echo_fails) {
1673 lcp_echos_pending = 0;
1677 * Make and send the echo request frame.
1679 if (f->state == OPENED) {
1680 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1681 ? lcp_gotoptions[f->unit].magicnumber
1684 PUTLONG(lcp_magic, pktp);
1686 fsm_sdata(f, ECHOREQ,
1687 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1692 * lcp_echo_lowerup - Start the timer for the LCP frame
1696 lcp_echo_lowerup (unit)
1699 fsm *f = &lcp_fsm[unit];
1701 /* Clear the parameters for generating echo frames */
1702 lcp_echos_pending = 0;
1703 lcp_echo_number = 0;
1704 lcp_echo_timer_running = 0;
1706 /* If a timeout interval is specified then start the timer */
1707 if (lcp_echo_interval != 0)
1710 /* If a idle time limit is given then start it */
1711 if (idle_time_limit != 0)
1712 RestartIdleTimer (f);
1717 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1721 lcp_echo_lowerdown (unit)
1724 fsm *f = &lcp_fsm[unit];
1726 if (lcp_echo_timer_running != 0) {
1727 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1728 lcp_echo_timer_running = 0;
1731 /* If a idle time limit is running then stop it */
1732 if (idle_timer_running != 0) {
1733 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1734 idle_timer_running = 0;