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.22 1995/10/27 03:42:09 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.
283 lcp_options *wo = &lcp_wantoptions[unit];
287 * Don't use A/C or protocol compression on transmission,
288 * but accept A/C and protocol compressed packets
289 * if we are going to ask for A/C and protocol compression.
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,
294 wo->neg_pcompression, wo->neg_accompression);
295 peer_mru[unit] = PPP_MRU;
296 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
298 fsm_lowerup(&lcp_fsm[unit]);
303 * lcp_lowerdown - The lower layer is down.
309 fsm_lowerdown(&lcp_fsm[unit]);
314 * lcp_input - Input LCP packet.
317 lcp_input(unit, p, len)
322 fsm *f = &lcp_fsm[unit];
324 fsm_input(f, p, len);
329 * lcp_extcode - Handle a LCP-specific code.
332 lcp_extcode(f, code, id, inp, len)
342 lcp_rprotrej(f, inp, len);
346 if (f->state != OPENED)
348 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
350 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
351 fsm_sdata(f, ECHOREP, id, inp, len);
355 lcp_received_echo_reply(f, id, inp, len);
369 * lcp_rprotrej - Receive an Protocol-Reject.
371 * Figure out which protocol is rejected and inform it.
374 lcp_rprotrej(f, inp, len)
381 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
383 if (len < sizeof (u_short)) {
385 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
392 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
396 * Protocol-Reject packets received in any state other than the LCP
397 * OPENED state SHOULD be silently discarded.
399 if( f->state != OPENED ){
400 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
405 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
410 * lcp_protrej - A Protocol-Reject was received.
420 LCPDEBUG((LOG_WARNING,
421 "lcp_protrej: Received Protocol-Reject for LCP!"));
422 fsm_protreject(&lcp_fsm[unit]);
427 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
430 lcp_sprotrej(unit, p, len)
436 * Send back the protocol and the information field of the
437 * rejected packet. We only get here if LCP is in the OPENED state.
442 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
448 * lcp_resetci - Reset our CI.
454 lcp_wantoptions[f->unit].magicnumber = magic();
455 lcp_wantoptions[f->unit].numloops = 0;
456 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
457 peer_mru[f->unit] = PPP_MRU;
462 * lcp_cilen - Return length of our CI.
468 lcp_options *go = &lcp_gotoptions[f->unit];
470 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
471 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
472 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
473 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
474 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
476 * NB: we only ask for one of CHAP and UPAP, even if we will
479 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
480 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
481 LENCICHAP(go->neg_chap) +
482 LENCISHORT(!go->neg_chap && go->neg_upap) +
483 LENCILQR(go->neg_lqr) +
484 LENCILONG(go->neg_magicnumber) +
485 LENCIVOID(go->neg_pcompression) +
486 LENCIVOID(go->neg_accompression));
491 * lcp_addci - Add our desired CIs to a packet.
494 lcp_addci(f, ucp, lenp)
499 lcp_options *go = &lcp_gotoptions[f->unit];
500 u_char *start_ucp = ucp;
502 #define ADDCIVOID(opt, neg) \
505 PUTCHAR(CILEN_VOID, ucp); \
507 #define ADDCISHORT(opt, neg, val) \
510 PUTCHAR(CILEN_SHORT, ucp); \
511 PUTSHORT(val, ucp); \
513 #define ADDCICHAP(opt, neg, val, digest) \
516 PUTCHAR(CILEN_CHAP, ucp); \
517 PUTSHORT(val, ucp); \
518 PUTCHAR(digest, ucp); \
520 #define ADDCILONG(opt, neg, val) \
523 PUTCHAR(CILEN_LONG, ucp); \
526 #define ADDCILQR(opt, neg, val) \
529 PUTCHAR(CILEN_LQR, ucp); \
530 PUTSHORT(PPP_LQR, ucp); \
534 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
535 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
537 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
538 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
539 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
540 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
541 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
542 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
544 if (ucp - start_ucp != *lenp) {
545 /* this should never happen, because peer_mtu should be 1500 */
546 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
552 * lcp_ackci - Ack our CIs.
553 * This should not modify any state if the Ack is bad.
565 lcp_options *go = &lcp_gotoptions[f->unit];
566 u_char cilen, citype, cichar;
571 * CIs must be in exactly the same order that we sent.
572 * Check packet length and CI length at each step.
573 * If we find any deviations, then this packet is bad.
575 #define ACKCIVOID(opt, neg) \
577 if ((len -= CILEN_VOID) < 0) \
579 GETCHAR(citype, p); \
581 if (cilen != CILEN_VOID || \
585 #define ACKCISHORT(opt, neg, val) \
587 if ((len -= CILEN_SHORT) < 0) \
589 GETCHAR(citype, p); \
591 if (cilen != CILEN_SHORT || \
594 GETSHORT(cishort, p); \
595 if (cishort != val) \
598 #define ACKCICHAP(opt, neg, val, digest) \
600 if ((len -= CILEN_CHAP) < 0) \
602 GETCHAR(citype, p); \
604 if (cilen != CILEN_CHAP || \
607 GETSHORT(cishort, p); \
608 if (cishort != val) \
610 GETCHAR(cichar, p); \
611 if (cichar != digest) \
614 #define ACKCILONG(opt, neg, val) \
616 if ((len -= CILEN_LONG) < 0) \
618 GETCHAR(citype, p); \
620 if (cilen != CILEN_LONG || \
623 GETLONG(cilong, p); \
627 #define ACKCILQR(opt, neg, val) \
629 if ((len -= CILEN_LQR) < 0) \
631 GETCHAR(citype, p); \
633 if (cilen != CILEN_LQR || \
636 GETSHORT(cishort, p); \
637 if (cishort != PPP_LQR) \
639 GETLONG(cilong, p); \
644 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
645 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
647 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
648 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
649 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
650 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
651 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
652 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
655 * If there are any remaining CIs, then this packet is bad.
661 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
667 * lcp_nakci - Peer has sent a NAK for some of our CIs.
668 * This should not modify any state if the Nak is bad
669 * or if LCP is in the OPENED state.
681 lcp_options *go = &lcp_gotoptions[f->unit];
682 lcp_options *wo = &lcp_wantoptions[f->unit];
683 u_char citype, cichar, *next;
686 lcp_options no; /* options we've seen Naks for */
687 lcp_options try; /* options to request next time */
691 BZERO(&no, sizeof(no));
695 * Any Nak'd CIs must be in exactly the same order that we sent.
696 * Check packet length and CI length at each step.
697 * If we find any deviations, then this packet is bad.
699 #define NAKCIVOID(opt, neg, code) \
701 len >= CILEN_VOID && \
702 p[1] == CILEN_VOID && \
705 INCPTR(CILEN_VOID, p); \
709 #define NAKCICHAP(opt, neg, code) \
711 len >= CILEN_CHAP && \
712 p[1] == CILEN_CHAP && \
716 GETSHORT(cishort, p); \
717 GETCHAR(cichar, p); \
721 #define NAKCISHORT(opt, neg, code) \
723 len >= CILEN_SHORT && \
724 p[1] == CILEN_SHORT && \
726 len -= CILEN_SHORT; \
728 GETSHORT(cishort, p); \
732 #define NAKCILONG(opt, neg, code) \
734 len >= CILEN_LONG && \
735 p[1] == CILEN_LONG && \
739 GETLONG(cilong, p); \
743 #define NAKCILQR(opt, neg, code) \
745 len >= CILEN_LQR && \
746 p[1] == CILEN_LQR && \
750 GETSHORT(cishort, p); \
751 GETLONG(cilong, p); \
757 * We don't care if they want to send us smaller packets than
758 * we want. Therefore, accept any MRU less than what we asked for,
759 * but then ignore the new value when setting the MRU in the kernel.
760 * If they send us a bigger MRU than what we asked, accept it, up to
761 * the limit of the default MRU we'd get if we didn't negotiate.
763 if (go->neg_mru && go->mru != DEFMRU) {
764 NAKCISHORT(CI_MRU, neg_mru,
765 if (cishort <= wo->mru || cishort < DEFMRU)
771 * Add any characters they want to our (receive-side) asyncmap.
773 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
774 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
775 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 && go->mru != DEFMRU
894 || no.neg_mru || cilen != CILEN_SHORT)
896 GETSHORT(cishort, p);
897 if (cishort < DEFMRU)
901 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF
902 || 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)
932 /* If there is still anything left, this packet is bad. */
937 * OK, the Nak is good. Now we can update state.
939 if (f->state != OPENED) {
941 if (++try.numloops >= lcp_loopbackfail) {
942 syslog(LOG_NOTICE, "Serial line is looped back.");
953 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
959 * lcp_rejci - Peer has Rejected some of our CIs.
960 * This should not modify any state if the Reject is bad
961 * or if LCP is in the OPENED state.
964 * 0 - Reject was bad.
965 * 1 - Reject was good.
973 lcp_options *go = &lcp_gotoptions[f->unit];
979 lcp_options try; /* options to request next time */
984 * Any Rejected CIs must be in exactly the same order that we sent.
985 * Check packet length and CI length at each step.
986 * If we find any deviations, then this packet is bad.
988 #define REJCIVOID(opt, neg) \
990 len >= CILEN_VOID && \
991 p[1] == CILEN_VOID && \
994 INCPTR(CILEN_VOID, p); \
996 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
998 #define REJCISHORT(opt, neg, val) \
1000 len >= CILEN_SHORT && \
1001 p[1] == CILEN_SHORT && \
1003 len -= CILEN_SHORT; \
1005 GETSHORT(cishort, p); \
1006 /* Check rejected value. */ \
1007 if (cishort != val) \
1010 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1012 #define REJCICHAP(opt, neg, val, digest) \
1014 len >= CILEN_CHAP && \
1015 p[1] == CILEN_CHAP && \
1017 len -= CILEN_CHAP; \
1019 GETSHORT(cishort, p); \
1020 GETCHAR(cichar, p); \
1021 /* Check rejected value. */ \
1022 if (cishort != val || cichar != digest) \
1026 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1028 #define REJCILONG(opt, neg, val) \
1030 len >= CILEN_LONG && \
1031 p[1] == CILEN_LONG && \
1033 len -= CILEN_LONG; \
1035 GETLONG(cilong, p); \
1036 /* Check rejected value. */ \
1037 if (cilong != val) \
1040 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1042 #define REJCILQR(opt, neg, val) \
1044 len >= CILEN_LQR && \
1045 p[1] == CILEN_LQR && \
1049 GETSHORT(cishort, p); \
1050 GETLONG(cilong, p); \
1051 /* Check rejected value. */ \
1052 if (cishort != PPP_LQR || cilong != val) \
1055 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1058 REJCISHORT(CI_MRU, neg_mru, go->mru);
1059 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1060 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1061 if (!go->neg_chap) {
1062 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1064 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1065 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1066 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1067 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1070 * If there are any remaining CIs, then this packet is bad.
1075 * Now we can update state.
1077 if (f->state != OPENED)
1082 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1083 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1084 plen, len, p - start));
1090 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1092 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1093 * appropriately. If reject_if_disagree is non-zero, doesn't return
1094 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1097 lcp_reqci(f, inp, lenp, reject_if_disagree)
1099 u_char *inp; /* Requested CIs */
1100 int *lenp; /* Length of requested CIs */
1101 int reject_if_disagree;
1103 lcp_options *go = &lcp_gotoptions[f->unit];
1104 lcp_options *ho = &lcp_hisoptions[f->unit];
1105 lcp_options *ao = &lcp_allowoptions[f->unit];
1106 u_char *cip, *next; /* Pointer to current and next CIs */
1107 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1108 u_short cishort; /* Parsed short value */
1109 u_int32_t cilong; /* Parse long value */
1110 int rc = CONFACK; /* Final packet return code */
1111 int orc; /* Individual option return code */
1112 u_char *p; /* Pointer to next char to parse */
1113 u_char *rejp; /* Pointer to next char in reject frame */
1114 u_char *nakp; /* Pointer to next char in Nak frame */
1115 int l = *lenp; /* Length left */
1118 * Reset all his options.
1120 BZERO(ho, sizeof(*ho));
1123 * Process all his options.
1129 orc = CONFACK; /* Assume success */
1130 cip = p = next; /* Remember begining of CI */
1131 if (l < 2 || /* Not enough data for CI header or */
1132 p[1] < 2 || /* CI length too small or */
1133 p[1] > l) { /* CI length too big? */
1134 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1135 orc = CONFREJ; /* Reject bad CI */
1136 cilen = l; /* Reject till end of packet */
1137 l = 0; /* Don't loop again */
1140 GETCHAR(citype, p); /* Parse CI type */
1141 GETCHAR(cilen, p); /* Parse CI length */
1142 l -= cilen; /* Adjust remaining length */
1143 next += cilen; /* Step to next CI */
1145 switch (citype) { /* Check CI type */
1147 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1148 if (!ao->neg_mru || /* Allow option? */
1149 cilen != CILEN_SHORT) { /* Check CI length */
1150 orc = CONFREJ; /* Reject CI */
1153 GETSHORT(cishort, p); /* Parse MRU */
1154 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1157 * He must be able to receive at least our minimum.
1158 * No need to check a maximum. If he sends a large number,
1159 * we'll just ignore it.
1161 if (cishort < MINMRU) {
1162 orc = CONFNAK; /* Nak CI */
1163 PUTCHAR(CI_MRU, nakp);
1164 PUTCHAR(CILEN_SHORT, nakp);
1165 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1168 ho->neg_mru = 1; /* Remember he sent MRU */
1169 ho->mru = cishort; /* And remember value */
1173 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1174 if (!ao->neg_asyncmap ||
1175 cilen != CILEN_LONG) {
1180 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1183 * Asyncmap must have set at least the bits
1184 * which are set in lcp_allowoptions[unit].asyncmap.
1186 if ((ao->asyncmap & ~cilong) != 0) {
1188 PUTCHAR(CI_ASYNCMAP, nakp);
1189 PUTCHAR(CILEN_LONG, nakp);
1190 PUTLONG(ao->asyncmap | cilong, nakp);
1193 ho->neg_asyncmap = 1;
1194 ho->asyncmap = cilong;
1198 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1199 if (cilen < CILEN_SHORT ||
1200 !(ao->neg_upap || ao->neg_chap)) {
1202 * Reject the option if we're not willing to authenticate.
1207 GETSHORT(cishort, p);
1208 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1211 * Authtype must be UPAP or CHAP.
1213 * Note: if both ao->neg_upap and ao->neg_chap are set,
1214 * and the peer sends a Configure-Request with two
1215 * authenticate-protocol requests, one for CHAP and one
1216 * for UPAP, then we will reject the second request.
1217 * Whether we end up doing CHAP or UPAP depends then on
1218 * the ordering of the CIs in the peer's Configure-Request.
1221 if (cishort == PPP_PAP) {
1222 if (ho->neg_chap || /* we've already accepted CHAP */
1223 cilen != CILEN_SHORT) {
1224 LCPDEBUG((LOG_WARNING,
1225 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1229 if (!ao->neg_upap) { /* we don't want to do PAP */
1230 orc = CONFNAK; /* NAK it and suggest CHAP */
1231 PUTCHAR(CI_AUTHTYPE, nakp);
1232 PUTCHAR(CILEN_CHAP, nakp);
1233 PUTSHORT(PPP_CHAP, nakp);
1234 PUTCHAR(ao->chap_mdtype, nakp);
1240 if (cishort == PPP_CHAP) {
1241 if (ho->neg_upap || /* we've already accepted PAP */
1242 cilen != CILEN_CHAP) {
1244 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1248 if (!ao->neg_chap) { /* we don't want to do CHAP */
1249 orc = CONFNAK; /* NAK it and suggest PAP */
1250 PUTCHAR(CI_AUTHTYPE, nakp);
1251 PUTCHAR(CILEN_SHORT, nakp);
1252 PUTSHORT(PPP_PAP, nakp);
1255 GETCHAR(cichar, p); /* get digest type*/
1256 if (cichar != ao->chap_mdtype) {
1258 PUTCHAR(CI_AUTHTYPE, nakp);
1259 PUTCHAR(CILEN_CHAP, nakp);
1260 PUTSHORT(PPP_CHAP, nakp);
1261 PUTCHAR(ao->chap_mdtype, nakp);
1264 ho->chap_mdtype = cichar; /* save md type */
1270 * We don't recognize the protocol they're asking for.
1271 * Nak it with something we're willing to do.
1272 * (At this point we know ao->neg_upap || ao->neg_chap.)
1275 PUTCHAR(CI_AUTHTYPE, nakp);
1277 PUTCHAR(CILEN_CHAP, nakp);
1278 PUTSHORT(PPP_CHAP, nakp);
1279 PUTCHAR(ao->chap_mdtype, nakp);
1281 PUTCHAR(CILEN_SHORT, nakp);
1282 PUTSHORT(PPP_PAP, nakp);
1287 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1289 cilen != CILEN_LQR) {
1294 GETSHORT(cishort, p);
1296 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1299 * Check the protocol and the reporting period.
1300 * XXX When should we Nak this, and what with?
1302 if (cishort != PPP_LQR) {
1304 PUTCHAR(CI_QUALITY, nakp);
1305 PUTCHAR(CILEN_LQR, nakp);
1306 PUTSHORT(PPP_LQR, nakp);
1307 PUTLONG(ao->lqr_period, nakp);
1312 case CI_MAGICNUMBER:
1313 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1314 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1315 cilen != CILEN_LONG) {
1320 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1323 * He must have a different magic number.
1325 if (go->neg_magicnumber &&
1326 cilong == go->magicnumber) {
1327 cilong = magic(); /* Don't put magic() inside macro! */
1329 PUTCHAR(CI_MAGICNUMBER, nakp);
1330 PUTCHAR(CILEN_LONG, nakp);
1331 PUTLONG(cilong, nakp);
1334 ho->neg_magicnumber = 1;
1335 ho->magicnumber = cilong;
1339 case CI_PCOMPRESSION:
1340 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1341 if (!ao->neg_pcompression ||
1342 cilen != CILEN_VOID) {
1346 ho->neg_pcompression = 1;
1349 case CI_ACCOMPRESSION:
1350 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1351 if (!ao->neg_accompression ||
1352 cilen != CILEN_VOID) {
1356 ho->neg_accompression = 1;
1360 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1367 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1368 if (orc == CONFACK && /* Good CI */
1369 rc != CONFACK) /* but prior CI wasnt? */
1370 continue; /* Don't send this one */
1372 if (orc == CONFNAK) { /* Nak this CI? */
1373 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1374 && citype != CI_MAGICNUMBER) {
1375 orc = CONFREJ; /* Get tough if so */
1377 if (rc == CONFREJ) /* Rejecting prior CI? */
1378 continue; /* Don't send this one */
1382 if (orc == CONFREJ) { /* Reject this CI */
1384 if (cip != rejp) /* Need to move rejected CI? */
1385 BCOPY(cip, rejp, cilen); /* Move it */
1386 INCPTR(cilen, rejp); /* Update output pointer */
1391 * If we wanted to send additional NAKs (for unsent CIs), the
1392 * code would go here. The extra NAKs would go at *nakp.
1393 * At present there are no cases where we want to ask the
1394 * peer to negotiate an option.
1403 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1405 *lenp = nakp - nak_buffer;
1406 BCOPY(nak_buffer, inp, *lenp);
1413 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1414 return (rc); /* Return final code */
1419 * lcp_up - LCP has come UP.
1421 * Start UPAP, IPCP, etc.
1427 lcp_options *wo = &lcp_wantoptions[f->unit];
1428 lcp_options *ho = &lcp_hisoptions[f->unit];
1429 lcp_options *go = &lcp_gotoptions[f->unit];
1430 lcp_options *ao = &lcp_allowoptions[f->unit];
1432 if (!go->neg_magicnumber)
1433 go->magicnumber = 0;
1434 if (!ho->neg_magicnumber)
1435 ho->magicnumber = 0;
1438 * Set our MTU to the smaller of the MTU we wanted and
1439 * the MRU our peer wanted. If we negotiated an MRU,
1440 * set our MRU to the larger of value we wanted and
1441 * the value we got in the negotiation.
1443 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1444 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1445 ho->neg_pcompression, ho->neg_accompression);
1447 * If the asyncmap hasn't been negotiated, we really should
1448 * set the receive asyncmap to ffffffff, but we set it to 0
1449 * for backwards contemptibility.
1451 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1452 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1453 go->neg_pcompression, go->neg_accompression);
1456 peer_mru[f->unit] = ho->mru;
1458 ChapLowerUp(f->unit); /* Enable CHAP */
1459 upap_lowerup(f->unit); /* Enable UPAP */
1460 ipcp_lowerup(f->unit); /* Enable IPCP */
1461 ccp_lowerup(f->unit); /* Enable CCP */
1462 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1464 link_established(f->unit);
1469 * lcp_down - LCP has gone DOWN.
1471 * Alert other protocols.
1477 lcp_options *go = &lcp_gotoptions[f->unit];
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,
1488 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1489 go->neg_pcompression, go->neg_accompression);
1490 peer_mru[f->unit] = PPP_MRU;
1497 * lcp_starting - LCP needs the lower layer up.
1503 link_required(f->unit);
1508 * lcp_finished - LCP has finished with the lower layer.
1514 link_terminated(f->unit);
1519 * lcp_printpkt - print the contents of an LCP packet.
1521 char *lcp_codenames[] = {
1522 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1523 "TermReq", "TermAck", "CodeRej", "ProtRej",
1524 "EchoReq", "EchoRep", "DiscReq"
1528 lcp_printpkt(p, plen, printer, arg)
1531 void (*printer) __P((void *, char *, ...));
1534 int code, id, len, olen;
1535 u_char *pstart, *optend;
1539 if (plen < HEADERLEN)
1545 if (len < HEADERLEN || len > plen)
1548 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1549 printer(arg, " %s", lcp_codenames[code-1]);
1551 printer(arg, " code=0x%x", code);
1552 printer(arg, " id=0x%x", id);
1559 /* print option list */
1564 if (olen < 2 || olen > len) {
1572 if (olen == CILEN_SHORT) {
1574 GETSHORT(cishort, p);
1575 printer(arg, "mru %d", cishort);
1579 if (olen == CILEN_LONG) {
1582 printer(arg, "asyncmap 0x%x", cilong);
1586 if (olen >= CILEN_SHORT) {
1588 printer(arg, "auth ");
1589 GETSHORT(cishort, p);
1592 printer(arg, "upap");
1595 printer(arg, "chap");
1598 printer(arg, "0x%x", cishort);
1603 if (olen >= CILEN_SHORT) {
1605 printer(arg, "quality ");
1606 GETSHORT(cishort, p);
1609 printer(arg, "lqr");
1612 printer(arg, "0x%x", cishort);
1616 case CI_MAGICNUMBER:
1617 if (olen == CILEN_LONG) {
1620 printer(arg, "magic 0x%x", cilong);
1623 case CI_PCOMPRESSION:
1624 if (olen == CILEN_VOID) {
1626 printer(arg, "pcomp");
1629 case CI_ACCOMPRESSION:
1630 if (olen == CILEN_VOID) {
1632 printer(arg, "accomp");
1636 while (p < optend) {
1638 printer(arg, " %.2x", code);
1645 /* print the rest of the bytes in the packet */
1646 for (; len > 0; --len) {
1648 printer(arg, " %.2x", code);
1655 * Time to shut down the link because there is nothing out there.
1659 void LcpLinkFailure (f)
1662 if (f->state == OPENED) {
1663 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1664 lcp_close(f->unit); /* Reset connection */
1669 * Timer expired for the LCP echo requests from this process.
1678 struct ppp_idle ddinfo;
1680 * Read the time since the last packet was received.
1682 if (ioctl (fd, PPPIOCGIDLE, &ddinfo) < 0) {
1683 syslog (LOG_ERR, "ioctl(PPPIOCGIDLE): %m");
1687 * Compute the time since the last packet was received. If the timer
1688 * has expired then send the echo request and reset the timer to maximum.
1690 delta = (long int) lcp_echo_interval - (long int) ddinfo.recv_idle;
1692 LcpSendEchoRequest (f);
1693 delta = (int) lcp_echo_interval;
1696 #else /* Other implementations do not have ability to find delta */
1697 LcpSendEchoRequest (f);
1698 delta = (int) lcp_echo_interval;
1702 * Start the timer for the next interval.
1704 assert (lcp_echo_timer_running==0);
1705 TIMEOUT (LcpEchoTimeout, (caddr_t) f, (u_int32_t) delta);
1706 lcp_echo_timer_running = 1;
1710 * LcpEchoTimeout - Timer expired on the LCP echo
1714 LcpEchoTimeout (arg)
1717 if (lcp_echo_timer_running != 0) {
1718 lcp_echo_timer_running = 0;
1719 LcpEchoCheck ((fsm *) arg);
1724 * LcpEchoReply - LCP has received a reply to the echo
1728 lcp_received_echo_reply (f, id, inp, len)
1730 int id; u_char *inp; int len;
1734 /* Check the magic number - don't count replies from ourselves. */
1736 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1739 GETLONG(magic, inp);
1740 if (lcp_gotoptions[f->unit].neg_magicnumber
1741 && magic == lcp_gotoptions[f->unit].magicnumber) {
1742 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1746 /* Reset the number of outstanding echo frames */
1747 lcp_echos_pending = 0;
1751 * LcpSendEchoRequest - Send an echo request frame to the peer
1755 LcpSendEchoRequest (f)
1758 u_int32_t lcp_magic;
1759 u_char pkt[4], *pktp;
1762 * Detect the failure of the peer at this point.
1764 if (lcp_echo_fails != 0) {
1765 if (lcp_echos_pending++ >= lcp_echo_fails) {
1767 lcp_echos_pending = 0;
1771 * Make and send the echo request frame.
1773 if (f->state == OPENED) {
1774 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1776 PUTLONG(lcp_magic, pktp);
1778 fsm_sdata(f, ECHOREQ,
1779 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1784 * lcp_echo_lowerup - Start the timer for the LCP frame
1788 lcp_echo_lowerup (unit)
1791 fsm *f = &lcp_fsm[unit];
1793 /* Clear the parameters for generating echo frames */
1794 lcp_echos_pending = 0;
1795 lcp_echo_number = 0;
1796 lcp_echo_timer_running = 0;
1798 /* If a timeout interval is specified then start the timer */
1799 if (lcp_echo_interval != 0)
1802 /* If a idle time limit is given then start it */
1803 if (idle_time_limit != 0)
1804 RestartIdleTimer (f);
1809 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1813 lcp_echo_lowerdown (unit)
1816 fsm *f = &lcp_fsm[unit];
1818 if (lcp_echo_timer_running != 0) {
1819 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1820 lcp_echo_timer_running = 0;
1823 /* If a idle time limit is running then stop it */
1824 if (idle_timer_running != 0) {
1825 UNTIMEOUT (IdleTimeCheck, (caddr_t) f);
1826 idle_timer_running = 0;