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.8 1994/05/30 02:38:49 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
47 #ifdef _linux_ /* Needs ppp ioctls */
48 #include <linux/ppp.h>
52 fsm lcp_fsm[NPPP]; /* LCP fsm structure (global)*/
53 lcp_options lcp_wantoptions[NPPP]; /* Options that we want to request */
54 lcp_options lcp_gotoptions[NPPP]; /* Options that peer ack'd */
55 lcp_options lcp_allowoptions[NPPP]; /* Options we allow peer to request */
56 lcp_options lcp_hisoptions[NPPP]; /* Options that we ack'd */
57 u_long xmit_accm[NPPP][8]; /* extended transmit ACCM */
59 static u_long lcp_echos_pending = 0; /* Number of outstanding echo msgs */
60 static u_long lcp_echo_number = 0; /* ID number of next echo frame */
61 static u_long lcp_echo_timer_running = 0; /* TRUE if a timer is running */
63 u_long lcp_echo_interval = 0;
64 u_long lcp_echo_fails = 0;
67 * Callbacks for fsm code. (CI = Configuration Information)
69 static void lcp_resetci __ARGS((fsm *)); /* Reset our CI */
70 static int lcp_cilen __ARGS((fsm *)); /* Return length of our CI */
71 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
72 static int lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
73 static int lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
74 static int lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
75 static int lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
76 static void lcp_up __ARGS((fsm *)); /* We're UP */
77 static void lcp_down __ARGS((fsm *)); /* We're DOWN */
78 static void lcp_starting __ARGS((fsm *)); /* We need lower layer up */
79 static void lcp_finished __ARGS((fsm *)); /* We need lower layer down */
80 static int lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
81 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
84 * routines to send LCP echos to peer
87 static void lcp_echo_lowerup __ARGS((int));
88 static void lcp_echo_lowerdown __ARGS((int));
89 static void LcpEchoTimeout __ARGS((caddr_t));
90 static void lcp_received_echo_reply __ARGS((fsm *, int, u_char *, int));
91 static void LcpSendEchoRequest __ARGS((fsm *));
92 static void LcpLinkFailure __ARGS((fsm *));
94 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
95 lcp_resetci, /* Reset our Configuration Information */
96 lcp_cilen, /* Length of our Configuration Information */
97 lcp_addci, /* Add our Configuration Information */
98 lcp_ackci, /* ACK our Configuration Information */
99 lcp_nakci, /* NAK our Configuration Information */
100 lcp_rejci, /* Reject our Configuration Information */
101 lcp_reqci, /* Request peer's Configuration Information */
102 lcp_up, /* Called when fsm reaches OPENED state */
103 lcp_down, /* Called when fsm leaves OPENED state */
104 lcp_starting, /* Called when we want the lower layer up */
105 lcp_finished, /* Called when we want the lower layer down */
106 NULL, /* Called when Protocol-Reject received */
107 NULL, /* Retransmission is necessary */
108 lcp_extcode, /* Called to handle LCP-specific codes */
109 "LCP" /* String name of protocol */
112 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
115 * Length of each type of configuration option (in octets)
118 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
119 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
120 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
121 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
123 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
124 (x) == CONFNAK ? "NAK" : "REJ")
128 * lcp_init - Initialize LCP.
134 fsm *f = &lcp_fsm[unit];
135 lcp_options *wo = &lcp_wantoptions[unit];
136 lcp_options *ao = &lcp_allowoptions[unit];
140 f->callbacks = &lcp_callbacks;
146 wo->restart = 0; /* Set to 1 in kernels or multi-line
150 wo->neg_asyncmap = 0;
152 wo->neg_chap = 0; /* Set to 1 on server */
153 wo->neg_upap = 0; /* Set to 1 on server */
154 wo->chap_mdtype = CHAP_DIGEST_MD5;
155 wo->neg_magicnumber = 1;
156 wo->neg_pcompression = 1;
157 wo->neg_accompression = 1;
158 wo->neg_lqr = 0; /* no LQR implementation yet */
162 ao->neg_asyncmap = 1;
165 ao->chap_mdtype = CHAP_DIGEST_MD5;
167 ao->neg_magicnumber = 1;
168 ao->neg_pcompression = 1;
169 ao->neg_accompression = 1;
170 ao->neg_lqr = 0; /* no LQR implementation yet */
172 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
173 xmit_accm[unit][3] = 0x60000000;
178 * lcp_open - LCP is allowed to come up.
184 fsm *f = &lcp_fsm[unit];
185 lcp_options *wo = &lcp_wantoptions[unit];
189 f->flags |= OPT_PASSIVE;
191 f->flags |= OPT_SILENT;
197 * lcp_close - Take LCP down.
203 fsm *f = &lcp_fsm[unit];
205 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
207 * This action is not strictly according to the FSM in RFC1548,
208 * but it does mean that the program terminates if you do a
209 * lcp_close(0) in passive/silent mode when a connection hasn't
216 fsm_close(&lcp_fsm[unit]);
221 * lcp_lowerup - The lower layer is up.
228 ppp_set_xaccm(unit, xmit_accm[unit]);
229 ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
230 ppp_recv_config(unit, MTU, 0x00000000, 0, 0);
231 peer_mru[unit] = MTU;
232 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
234 fsm_lowerup(&lcp_fsm[unit]);
239 * lcp_lowerdown - The lower layer is down.
245 fsm_lowerdown(&lcp_fsm[unit]);
250 * lcp_input - Input LCP packet.
253 lcp_input(unit, p, len)
258 fsm_input(&lcp_fsm[unit], p, len);
263 * lcp_extcode - Handle a LCP-specific code.
266 lcp_extcode(f, code, id, inp, len)
276 lcp_rprotrej(f, inp, len);
280 if (f->state != OPENED)
282 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
284 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
285 if (len < CILEN_LONG)
287 fsm_sdata(f, ECHOREP, id, inp, len);
291 lcp_received_echo_reply(f, id, inp, len);
305 * lcp_rprotrej - Receive an Protocol-Reject.
307 * Figure out which protocol is rejected and inform it.
310 lcp_rprotrej(f, inp, len)
317 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
319 if (len < sizeof (u_short)) {
321 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
328 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
332 * Protocol-Reject packets received in any state other than the LCP
333 * OPENED state SHOULD be silently discarded.
335 if( f->state != OPENED ){
336 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
341 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
346 * lcp_protrej - A Protocol-Reject was received.
356 LCPDEBUG((LOG_WARNING,
357 "lcp_protrej: Received Protocol-Reject for LCP!"));
358 fsm_protreject(&lcp_fsm[unit]);
363 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
366 lcp_sprotrej(unit, p, len)
372 * Send back the protocol and the information field of the
373 * rejected packet. We only get here if LCP is in the OPENED state.
378 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
384 * lcp_resetci - Reset our CI.
390 lcp_wantoptions[f->unit].magicnumber = magic();
391 lcp_wantoptions[f->unit].numloops = 0;
392 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
393 peer_mru[f->unit] = MTU;
398 * lcp_cilen - Return length of our CI.
404 lcp_options *go = &lcp_gotoptions[f->unit];
406 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
407 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
408 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
409 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
410 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
412 * NB: we only ask for one of CHAP and UPAP, even if we will
415 return (LENCISHORT(go->neg_mru) +
416 LENCILONG(go->neg_asyncmap) +
417 LENCICHAP(go->neg_chap) +
418 LENCISHORT(!go->neg_chap && go->neg_upap) +
419 LENCILQR(go->neg_lqr) +
420 LENCILONG(go->neg_magicnumber) +
421 LENCIVOID(go->neg_pcompression) +
422 LENCIVOID(go->neg_accompression));
427 * lcp_addci - Add our desired CIs to a packet.
430 lcp_addci(f, ucp, lenp)
435 lcp_options *go = &lcp_gotoptions[f->unit];
436 u_char *start_ucp = ucp;
438 #define ADDCIVOID(opt, neg) \
441 PUTCHAR(CILEN_VOID, ucp); \
443 #define ADDCISHORT(opt, neg, val) \
446 PUTCHAR(CILEN_SHORT, ucp); \
447 PUTSHORT(val, ucp); \
449 #define ADDCICHAP(opt, neg, val, digest) \
452 PUTCHAR(CILEN_CHAP, ucp); \
453 PUTSHORT(val, ucp); \
454 PUTCHAR(digest, ucp); \
456 #define ADDCILONG(opt, neg, val) \
459 PUTCHAR(CILEN_LONG, ucp); \
462 #define ADDCILQR(opt, neg, val) \
465 PUTCHAR(CILEN_LQR, ucp); \
466 PUTSHORT(LQR, ucp); \
470 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
471 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
472 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
473 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
474 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
475 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
476 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
477 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
479 if (ucp - start_ucp != *lenp) {
480 /* this should never happen, because peer_mtu should be 1500 */
481 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
487 * lcp_ackci - Ack our CIs.
488 * This should not modify any state if the Ack is bad.
500 lcp_options *go = &lcp_gotoptions[f->unit];
501 u_char cilen, citype, cichar;
506 * CIs must be in exactly the same order that we sent.
507 * Check packet length and CI length at each step.
508 * If we find any deviations, then this packet is bad.
510 #define ACKCIVOID(opt, neg) \
512 if ((len -= CILEN_VOID) < 0) \
514 GETCHAR(citype, p); \
516 if (cilen != CILEN_VOID || \
520 #define ACKCISHORT(opt, neg, val) \
522 if ((len -= CILEN_SHORT) < 0) \
524 GETCHAR(citype, p); \
526 if (cilen != CILEN_SHORT || \
529 GETSHORT(cishort, p); \
530 if (cishort != val) \
533 #define ACKCICHAP(opt, neg, val, digest) \
535 if ((len -= CILEN_CHAP) < 0) \
537 GETCHAR(citype, p); \
539 if (cilen != CILEN_CHAP || \
542 GETSHORT(cishort, p); \
543 if (cishort != val) \
545 GETCHAR(cichar, p); \
546 if (cichar != digest) \
549 #define ACKCILONG(opt, neg, val) \
551 if ((len -= CILEN_LONG) < 0) \
553 GETCHAR(citype, p); \
555 if (cilen != CILEN_LONG || \
558 GETLONG(cilong, p); \
562 #define ACKCILQR(opt, neg, val) \
564 if ((len -= CILEN_LQR) < 0) \
566 GETCHAR(citype, p); \
568 if (cilen != CILEN_LQR || \
571 GETSHORT(cishort, p); \
572 if (cishort != LQR) \
574 GETLONG(cilong, p); \
579 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
580 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
581 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
582 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
583 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
584 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
585 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
586 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
589 * If there are any remaining CIs, then this packet is bad.
595 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
601 * lcp_nakci - Peer has sent a NAK for some of our CIs.
602 * This should not modify any state if the Nak is bad
603 * or if LCP is in the OPENED state.
615 lcp_options *go = &lcp_gotoptions[f->unit];
616 lcp_options *wo = &lcp_wantoptions[f->unit];
617 u_char cilen, citype, cichar, *next;
620 lcp_options no; /* options we've seen Naks for */
621 lcp_options try; /* options to request next time */
624 BZERO(&no, sizeof(no));
628 * Any Nak'd CIs must be in exactly the same order that we sent.
629 * Check packet length and CI length at each step.
630 * If we find any deviations, then this packet is bad.
632 #define NAKCIVOID(opt, neg, code) \
634 len >= CILEN_VOID && \
635 p[1] == CILEN_VOID && \
638 INCPTR(CILEN_VOID, p); \
642 #define NAKCICHAP(opt, neg, code) \
644 len >= CILEN_CHAP && \
645 p[1] == CILEN_CHAP && \
649 GETSHORT(cishort, p); \
650 GETCHAR(cichar, p); \
654 #define NAKCISHORT(opt, neg, code) \
656 len >= CILEN_SHORT && \
657 p[1] == CILEN_SHORT && \
659 len -= CILEN_SHORT; \
661 GETSHORT(cishort, p); \
665 #define NAKCILONG(opt, neg, code) \
667 len >= CILEN_LONG && \
668 p[1] == CILEN_LONG && \
672 GETLONG(cilong, p); \
676 #define NAKCILQR(opt, neg, code) \
678 len >= CILEN_LQR && \
679 p[1] == CILEN_LQR && \
683 GETSHORT(cishort, p); \
684 GETLONG(cilong, p); \
690 * We don't care if they want to send us smaller packets than
691 * we want. Therefore, accept any MRU less than what we asked for,
692 * but then ignore the new value when setting the MRU in the kernel.
693 * If they send us a bigger MRU than what we asked, accept it, up to
694 * the limit of the default MRU we'd get if we didn't negotiate.
696 NAKCISHORT(CI_MRU, neg_mru,
697 if (cishort <= wo->mru || cishort < DEFMRU)
701 * Add any characters they want to our (receive-side) asyncmap.
703 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
704 try.asyncmap = go->asyncmap | cilong;
707 * If they can't cope with our CHAP hash algorithm, we'll have
708 * to stop asking for CHAP. We haven't got any other algorithm.
710 NAKCICHAP(CI_AUTHTYPE, neg_chap,
714 * Peer shouldn't send Nak for UPAP, protocol compression or
715 * address/control compression requests; they should send
716 * a Reject instead. If they send a Nak, treat it as a Reject.
719 NAKCISHORT(CI_AUTHTYPE, neg_upap,
724 * If they can't cope with our link quality protocol, we'll have
725 * to stop asking for LQR. We haven't got any other protocol.
726 * If they Nak the reporting period, take their value XXX ?
728 NAKCILQR(CI_QUALITY, neg_lqr,
732 try.lqr_period = cilong;
735 * Check for a looped-back line.
737 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
738 try.magicnumber = magic();
743 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
744 try.neg_pcompression = 0;
746 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
747 try.neg_accompression = 0;
751 * There may be remaining CIs, if the peer is requesting negotiation
752 * on an option that we didn't include in our request packet.
753 * If we see an option that we requested, or one we've already seen
754 * in this packet, then this packet is bad.
755 * If we wanted to respond by starting to negotiate on the requested
756 * option(s), we could, but we don't, because except for the
757 * authentication type and quality protocol, if we are not negotiating
758 * an option, it is because we were told not to.
759 * For the authentication type, the Nak from the peer means
760 * `let me authenticate myself with you' which is a bit pointless.
761 * For the quality protocol, the Nak means `ask me to send you quality
762 * reports', but if we didn't ask for them, we don't want them.
764 while (len > CILEN_VOID) {
767 if( (len -= cilen) < 0 )
769 next = p + cilen - 2;
773 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
777 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
781 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
785 if (go->neg_magicnumber || no.neg_magicnumber ||
789 case CI_PCOMPRESSION:
790 if (go->neg_pcompression || no.neg_pcompression
791 || cilen != CILEN_VOID)
794 case CI_ACCOMPRESSION:
795 if (go->neg_accompression || no.neg_accompression
796 || cilen != CILEN_VOID)
800 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
809 /* If there is still anything left, this packet is bad. */
814 * OK, the Nak is good. Now we can update state.
816 if (f->state != OPENED) {
818 if (looped_back && try.numloops % lcp_warnloops == 0)
819 LCPDEBUG((LOG_INFO, "The line appears to be looped back."));
825 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
831 * lcp_rejci - Peer has Rejected some of our CIs.
832 * This should not modify any state if the Reject is bad
833 * or if LCP is in the OPENED state.
836 * 0 - Reject was bad.
837 * 1 - Reject was good.
845 lcp_options *go = &lcp_gotoptions[f->unit];
851 lcp_options try; /* options to request next time */
856 * Any Rejected CIs must be in exactly the same order that we sent.
857 * Check packet length and CI length at each step.
858 * If we find any deviations, then this packet is bad.
860 #define REJCIVOID(opt, neg) \
862 len >= CILEN_VOID && \
863 p[1] == CILEN_VOID && \
866 INCPTR(CILEN_VOID, p); \
868 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
870 #define REJCISHORT(opt, neg, val) \
872 len >= CILEN_SHORT && \
873 p[1] == CILEN_SHORT && \
875 len -= CILEN_SHORT; \
877 GETSHORT(cishort, p); \
878 /* Check rejected value. */ \
879 if (cishort != val) \
882 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
884 #define REJCICHAP(opt, neg, val, digest) \
886 len >= CILEN_CHAP && \
887 p[1] == CILEN_CHAP && \
891 GETSHORT(cishort, p); \
892 GETCHAR(cichar, p); \
893 /* Check rejected value. */ \
894 if (cishort != val || cichar != digest) \
897 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
899 #define REJCILONG(opt, neg, val) \
901 len >= CILEN_LONG && \
902 p[1] == CILEN_LONG && \
906 GETLONG(cilong, p); \
907 /* Check rejected value. */ \
911 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
913 #define REJCILQR(opt, neg, val) \
915 len >= CILEN_LQR && \
916 p[1] == CILEN_LQR && \
920 GETSHORT(cishort, p); \
921 GETLONG(cilong, p); \
922 /* Check rejected value. */ \
923 if (cishort != LQR || cilong != val) \
926 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
929 REJCISHORT(CI_MRU, neg_mru, go->mru);
930 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
931 REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
933 REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
935 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
936 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
937 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
938 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
941 * If there are any remaining CIs, then this packet is bad.
946 * Now we can update state.
948 if (f->state != OPENED)
953 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
954 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
955 plen, len, p - start));
961 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
963 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
964 * appropriately. If reject_if_disagree is non-zero, doesn't return
965 * CONFNAK; returns CONFREJ if it can't return CONFACK.
968 lcp_reqci(f, inp, lenp, reject_if_disagree)
970 u_char *inp; /* Requested CIs */
971 int *lenp; /* Length of requested CIs */
972 int reject_if_disagree;
974 lcp_options *go = &lcp_gotoptions[f->unit];
975 lcp_options *ho = &lcp_hisoptions[f->unit];
976 lcp_options *ao = &lcp_allowoptions[f->unit];
977 u_char *cip, *next; /* Pointer to current and next CIs */
978 u_char cilen, citype, cichar;/* Parsed len, type, char value */
979 u_short cishort; /* Parsed short value */
980 u_long cilong; /* Parse long value */
981 int rc = CONFACK; /* Final packet return code */
982 int orc; /* Individual option return code */
983 u_char *p; /* Pointer to next char to parse */
984 u_char *ucp = inp; /* Pointer to current output char */
985 int l = *lenp; /* Length left */
988 * Reset all his options.
990 BZERO(ho, sizeof(*ho));
993 * Process all his options.
997 orc = CONFACK; /* Assume success */
998 cip = p = next; /* Remember begining of CI */
999 if (l < 2 || /* Not enough data for CI header or */
1000 p[1] < 2 || /* CI length too small or */
1001 p[1] > l) { /* CI length too big? */
1002 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1003 orc = CONFREJ; /* Reject bad CI */
1004 cilen = l; /* Reject till end of packet */
1005 l = 0; /* Don't loop again */
1008 GETCHAR(citype, p); /* Parse CI type */
1009 GETCHAR(cilen, p); /* Parse CI length */
1010 l -= cilen; /* Adjust remaining length */
1011 next += cilen; /* Step to next CI */
1013 switch (citype) { /* Check CI type */
1015 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1016 if (!ao->neg_mru || /* Allow option? */
1017 cilen != CILEN_SHORT) { /* Check CI length */
1018 orc = CONFREJ; /* Reject CI */
1021 GETSHORT(cishort, p); /* Parse MRU */
1022 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1025 * He must be able to receive at least our minimum.
1026 * No need to check a maximum. If he sends a large number,
1027 * we'll just ignore it.
1029 if (cishort < MINMRU) {
1030 orc = CONFNAK; /* Nak CI */
1031 if( !reject_if_disagree ){
1032 DECPTR(sizeof (short), p); /* Backup */
1033 PUTSHORT(MINMRU, p); /* Give him a hint */
1037 ho->neg_mru = 1; /* Remember he sent MRU */
1038 ho->mru = cishort; /* And remember value */
1042 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1043 if (!ao->neg_asyncmap ||
1044 cilen != CILEN_LONG) {
1049 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1052 * Asyncmap must have set at least the bits
1053 * which are set in lcp_allowoptions[unit].asyncmap.
1055 if ((ao->asyncmap & ~cilong) != 0) {
1057 if( !reject_if_disagree ){
1058 DECPTR(sizeof (long), p);
1059 PUTLONG(ao->asyncmap | cilong, p);
1063 ho->neg_asyncmap = 1;
1064 ho->asyncmap = cilong;
1068 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1069 if (cilen < CILEN_SHORT ||
1070 !(ao->neg_upap || ao->neg_chap)) {
1074 GETSHORT(cishort, p);
1075 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1078 * Authtype must be UPAP or CHAP.
1080 * Note: if both ao->neg_upap and ao->neg_chap are set,
1081 * and the peer sends a Configure-Request with two
1082 * authenticate-protocol requests, one for CHAP and one
1083 * for UPAP, then we will reject the second request.
1084 * Whether we end up doing CHAP or UPAP depends then on
1085 * the ordering of the CIs in the peer's Configure-Request.
1088 if (cishort == UPAP) {
1089 if (!ao->neg_upap || /* we don't want to do PAP */
1090 ho->neg_chap || /* or we've already accepted CHAP */
1091 cilen != CILEN_SHORT) {
1092 LCPDEBUG((LOG_WARNING,
1093 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1100 if (cishort == CHAP) {
1101 if (!ao->neg_chap || /* we don't want to do CHAP */
1102 ho->neg_upap || /* or we've already accepted UPAP */
1103 cilen != CILEN_CHAP) {
1105 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1109 GETCHAR(cichar, p); /* get digest type*/
1110 if (cichar != ao->chap_mdtype) {
1112 if( !reject_if_disagree ){
1113 DECPTR(sizeof (u_char), p);
1114 PUTCHAR(ao->chap_mdtype, p);
1118 ho->chap_mdtype = cichar; /* save md type */
1124 * We don't recognize the protocol they're asking for.
1131 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1133 cilen != CILEN_LQR) {
1138 GETSHORT(cishort, p);
1140 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1141 if (cishort != LQR) {
1147 * Check the reporting period.
1148 * XXX When should we Nak this, and what with?
1152 case CI_MAGICNUMBER:
1153 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1154 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1155 cilen != CILEN_LONG) {
1160 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1163 * He must have a different magic number.
1165 if (go->neg_magicnumber &&
1166 cilong == go->magicnumber) {
1168 DECPTR(sizeof (long), p);
1169 cilong = magic(); /* Don't put magic() inside macro! */
1173 ho->neg_magicnumber = 1;
1174 ho->magicnumber = cilong;
1178 case CI_PCOMPRESSION:
1179 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1180 if (!ao->neg_pcompression ||
1181 cilen != CILEN_VOID) {
1185 ho->neg_pcompression = 1;
1188 case CI_ACCOMPRESSION:
1189 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1190 if (!ao->neg_accompression ||
1191 cilen != CILEN_VOID) {
1195 ho->neg_accompression = 1;
1199 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1206 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1207 if (orc == CONFACK && /* Good CI */
1208 rc != CONFACK) /* but prior CI wasnt? */
1209 continue; /* Don't send this one */
1211 if (orc == CONFNAK) { /* Nak this CI? */
1212 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1213 orc = CONFREJ; /* Get tough if so */
1215 if (rc == CONFREJ) /* Rejecting prior CI? */
1216 continue; /* Don't send this one */
1217 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1218 rc = CONFNAK; /* Not anymore... */
1219 ucp = inp; /* Backup */
1223 if (orc == CONFREJ && /* Reject this CI */
1224 rc != CONFREJ) { /* but no prior ones? */
1226 ucp = inp; /* Backup */
1228 if (ucp != cip) /* Need to move CI? */
1229 BCOPY(cip, ucp, cilen); /* Move it */
1230 INCPTR(cilen, ucp); /* Update output pointer */
1234 * If we wanted to send additional NAKs (for unsent CIs), the
1235 * code would go here. This must be done with care since it might
1236 * require a longer packet than we received. At present there
1237 * are no cases where we want to ask the peer to negotiate an option.
1240 *lenp = ucp - inp; /* Compute output length */
1241 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1242 return (rc); /* Return final code */
1247 * lcp_up - LCP has come UP.
1249 * Start UPAP, IPCP, etc.
1255 lcp_options *wo = &lcp_wantoptions[f->unit];
1256 lcp_options *ho = &lcp_hisoptions[f->unit];
1257 lcp_options *go = &lcp_gotoptions[f->unit];
1258 lcp_options *ao = &lcp_allowoptions[f->unit];
1260 if (!go->neg_magicnumber)
1261 go->magicnumber = 0;
1262 if (!ho->neg_magicnumber)
1263 ho->magicnumber = 0;
1266 * Set our MTU to the smaller of the MTU we wanted and
1267 * the MRU our peer wanted. If we negotiated an MRU,
1268 * set our MRU to the larger of value we wanted and
1269 * the value we got in the negotiation.
1271 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1272 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1273 ho->neg_pcompression, ho->neg_accompression);
1275 * If the asyncmap hasn't been negotiated, we really should
1276 * set the receive asyncmap to ffffffff, but we set it to 0
1277 * for backwards contemptibility.
1279 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1280 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1281 go->neg_pcompression, go->neg_accompression);
1284 peer_mru[f->unit] = ho->mru;
1286 ChapLowerUp(f->unit); /* Enable CHAP */
1287 upap_lowerup(f->unit); /* Enable UPAP */
1288 ipcp_lowerup(f->unit); /* Enable IPCP */
1289 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1291 link_established(f->unit);
1296 * lcp_down - LCP has gone DOWN.
1298 * Alert other protocols.
1304 lcp_echo_lowerdown(f->unit);
1305 ipcp_lowerdown(f->unit);
1306 ChapLowerDown(f->unit);
1307 upap_lowerdown(f->unit);
1310 ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1311 ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1312 peer_mru[f->unit] = MTU;
1319 * lcp_starting - LCP needs the lower layer up.
1325 link_required(f->unit);
1330 * lcp_finished - LCP has finished with the lower layer.
1336 link_terminated(f->unit);
1341 * lcp_printpkt - print the contents of an LCP packet.
1343 char *lcp_codenames[] = {
1344 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1345 "TermReq", "TermAck", "CodeRej", "ProtRej",
1346 "EchoReq", "EchoRep", "DiscReq"
1350 lcp_printpkt(p, plen, printer, arg)
1353 void (*printer) __ARGS((void *, char *, ...));
1356 int code, id, len, olen;
1357 u_char *pstart, *optend;
1361 if (plen < HEADERLEN)
1367 if (len < HEADERLEN || len > plen)
1370 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1371 printer(arg, " %s", lcp_codenames[code-1]);
1373 printer(arg, " code=0x%x", code);
1374 printer(arg, " id=0x%x", id);
1381 /* print option list */
1386 if (olen < 2 || olen > len) {
1394 if (olen == CILEN_SHORT) {
1396 GETSHORT(cishort, p);
1397 printer(arg, "mru %d", cishort);
1401 if (olen == CILEN_LONG) {
1404 printer(arg, "asyncmap 0x%x", cilong);
1408 if (olen >= CILEN_SHORT) {
1410 printer(arg, "auth ");
1411 GETSHORT(cishort, p);
1414 printer(arg, "upap");
1417 printer(arg, "chap");
1420 printer(arg, "0x%x", cishort);
1425 if (olen >= CILEN_SHORT) {
1427 printer(arg, "quality ");
1428 GETSHORT(cishort, p);
1431 printer(arg, "lqr");
1434 printer(arg, "0x%x", cishort);
1438 case CI_MAGICNUMBER:
1439 if (olen == CILEN_LONG) {
1442 printer(arg, "magic 0x%x", cilong);
1445 case CI_PCOMPRESSION:
1446 if (olen == CILEN_VOID) {
1448 printer(arg, "pcomp");
1451 case CI_ACCOMPRESSION:
1452 if (olen == CILEN_VOID) {
1454 printer(arg, "accomp");
1458 while (p < optend) {
1460 printer(arg, " %.2x", code);
1467 /* print the rest of the bytes in the packet */
1468 for (; len > 0; --len) {
1470 printer(arg, " %.2x", code);
1477 * Time to shut down the link because there is nothing out there.
1481 void LcpLinkFailure (f)
1484 if (f->state == OPENED) {
1485 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1486 lcp_lowerdown(f->unit); /* Reset connection */
1491 * Timer expired for the LCP echo requests from this process.
1500 struct ppp_ddinfo ddinfo;
1503 * Read the time since the last packet was received.
1505 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1506 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1510 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1512 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1513 : ddinfo.ip_rjiffies;
1515 * Compute the time since the last packet was received. If the timer
1516 * has expired then send the echo request and reset the timer to maximum.
1518 delta = (lcp_echo_interval * HZ) - latest;
1519 if (delta < HZ || latest < 0L) {
1520 LcpSendEchoRequest (f);
1521 delta = lcp_echo_interval * HZ;
1525 #else /* Other implementations do not have ability to find delta */
1526 LcpSendEchoRequest (f);
1527 delta = lcp_echo_interval;
1531 * Start the timer for the next interval.
1533 assert (lcp_echo_timer_running==0);
1534 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1535 lcp_echo_timer_running = 1;
1539 * LcpEchoTimeout - Timer expired on the LCP echo
1543 LcpEchoTimeout (arg)
1546 if (lcp_echo_timer_running != 0) {
1547 lcp_echo_timer_running = 0;
1548 LcpEchoCheck ((fsm *) arg);
1553 * LcpEchoReply - LCP has received a reply to the echo
1557 lcp_received_echo_reply (f, id, inp, len)
1559 int id; u_char *inp; int len;
1563 /* Check the magic number - don't count replies from ourselves. */
1564 if (len < CILEN_LONG)
1566 GETLONG(magic, inp);
1567 if (lcp_gotoptions[f->unit].neg_magicnumber
1568 && magic == lcp_gotoptions[f->unit].magicnumber)
1571 /* Reset the number of outstanding echo frames */
1572 lcp_echos_pending = 0;
1576 * LcpSendEchoRequest - Send an echo request frame to the peer
1580 LcpSendEchoRequest (f)
1584 u_char pkt[4], *pktp;
1587 * Detect the failure of the peer at this point.
1589 if (lcp_echo_fails != 0) {
1590 if (lcp_echos_pending++ >= lcp_echo_fails) {
1592 lcp_echos_pending = 0;
1596 * Make and send the echo request frame.
1598 if (f->state == OPENED) {
1599 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1600 ? lcp_gotoptions[f->unit].magicnumber
1603 PUTLONG(lcp_magic, pktp);
1605 fsm_sdata(f, ECHOREQ,
1606 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1611 * lcp_echo_lowerup - Start the timer for the LCP frame
1615 lcp_echo_lowerup (unit)
1618 fsm *f = &lcp_fsm[unit];
1620 /* Clear the parameters for generating echo frames */
1621 lcp_echos_pending = 0;
1622 lcp_echo_number = 0;
1623 lcp_echo_timer_running = 0;
1625 /* If a timeout interval is specified then start the timer */
1626 if (lcp_echo_interval != 0)
1631 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1635 lcp_echo_lowerdown (unit)
1638 fsm *f = &lcp_fsm[unit];
1640 if (lcp_echo_timer_running != 0) {
1641 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1642 lcp_echo_timer_running = 0;