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.7 1994/05/27 01:01:49 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #ifdef _linux_ /* Needs ppp ioctls */
40 #include <linux/ppp.h>
53 fsm lcp_fsm[NPPP]; /* LCP fsm structure (global)*/
54 lcp_options lcp_wantoptions[NPPP]; /* Options that we want to request */
55 lcp_options lcp_gotoptions[NPPP]; /* Options that peer ack'd */
56 lcp_options lcp_allowoptions[NPPP]; /* Options we allow peer to request */
57 lcp_options lcp_hisoptions[NPPP]; /* Options that we ack'd */
58 u_long xmit_accm[NPPP][8]; /* extended transmit ACCM */
60 static u_long lcp_echos_pending = 0; /* Number of outstanding echo msgs */
61 static u_long lcp_echo_number = 0; /* ID number of next echo frame */
62 static u_long lcp_echo_timer_running = 0; /* TRUE if a timer is running */
64 u_long lcp_echo_interval = 0;
65 u_long lcp_echo_fails = 0;
68 * Callbacks for fsm code. (CI = Configuration Information)
70 static void lcp_resetci __ARGS((fsm *)); /* Reset our CI */
71 static int lcp_cilen __ARGS((fsm *)); /* Return length of our CI */
72 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
73 static int lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
74 static int lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
75 static int lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
76 static int lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
77 static void lcp_up __ARGS((fsm *)); /* We're UP */
78 static void lcp_down __ARGS((fsm *)); /* We're DOWN */
79 static void lcp_starting __ARGS((fsm *)); /* We need lower layer up */
80 static void lcp_finished __ARGS((fsm *)); /* We need lower layer down */
81 static int lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
82 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
85 * routines to send LCP echos to peer
88 static void lcp_echo_lowerup __ARGS((int));
89 static void lcp_echo_lowerdown __ARGS((int));
90 static void LcpEchoTimeout __ARGS((caddr_t));
91 static void lcp_received_echo_reply __ARGS((fsm *, int, u_char *, int));
92 static void LcpSendEchoRequest __ARGS((fsm *));
93 static void LcpLinkFailure __ARGS((fsm *));
95 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
96 lcp_resetci, /* Reset our Configuration Information */
97 lcp_cilen, /* Length of our Configuration Information */
98 lcp_addci, /* Add our Configuration Information */
99 lcp_ackci, /* ACK our Configuration Information */
100 lcp_nakci, /* NAK our Configuration Information */
101 lcp_rejci, /* Reject our Configuration Information */
102 lcp_reqci, /* Request peer's Configuration Information */
103 lcp_up, /* Called when fsm reaches OPENED state */
104 lcp_down, /* Called when fsm leaves OPENED state */
105 lcp_starting, /* Called when we want the lower layer up */
106 lcp_finished, /* Called when we want the lower layer down */
107 NULL, /* Called when Protocol-Reject received */
108 NULL, /* Retransmission is necessary */
109 lcp_extcode, /* Called to handle LCP-specific codes */
110 "LCP" /* String name of protocol */
113 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
116 * Length of each type of configuration option (in octets)
119 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
120 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
121 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
122 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
124 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
125 (x) == CONFNAK ? "NAK" : "REJ")
129 * lcp_init - Initialize LCP.
135 fsm *f = &lcp_fsm[unit];
136 lcp_options *wo = &lcp_wantoptions[unit];
137 lcp_options *ao = &lcp_allowoptions[unit];
141 f->callbacks = &lcp_callbacks;
147 wo->restart = 0; /* Set to 1 in kernels or multi-line
151 wo->neg_asyncmap = 0;
153 wo->neg_chap = 0; /* Set to 1 on server */
154 wo->neg_upap = 0; /* Set to 1 on server */
155 wo->chap_mdtype = CHAP_DIGEST_MD5;
156 wo->neg_magicnumber = 1;
157 wo->neg_pcompression = 1;
158 wo->neg_accompression = 1;
159 wo->neg_lqr = 0; /* no LQR implementation yet */
163 ao->neg_asyncmap = 1;
166 ao->chap_mdtype = CHAP_DIGEST_MD5;
168 ao->neg_magicnumber = 1;
169 ao->neg_pcompression = 1;
170 ao->neg_accompression = 1;
171 ao->neg_lqr = 0; /* no LQR implementation yet */
173 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
174 xmit_accm[unit][3] = 0x60000000;
179 * lcp_open - LCP is allowed to come up.
185 fsm *f = &lcp_fsm[unit];
186 lcp_options *wo = &lcp_wantoptions[unit];
190 f->flags |= OPT_PASSIVE;
192 f->flags |= OPT_SILENT;
198 * lcp_close - Take LCP down.
204 fsm *f = &lcp_fsm[unit];
206 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
208 * This action is not strictly according to the FSM in RFC1548,
209 * but it does mean that the program terminates if you do a
210 * lcp_close(0) in passive/silent mode when a connection hasn't
217 fsm_close(&lcp_fsm[unit]);
222 * lcp_lowerup - The lower layer is up.
229 ppp_set_xaccm(unit, xmit_accm[unit]);
230 ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
231 ppp_recv_config(unit, MTU, 0x00000000, 0, 0);
232 peer_mru[unit] = MTU;
233 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
235 fsm_lowerup(&lcp_fsm[unit]);
240 * lcp_lowerdown - The lower layer is down.
246 fsm_lowerdown(&lcp_fsm[unit]);
251 * lcp_input - Input LCP packet.
254 lcp_input(unit, p, len)
259 fsm_input(&lcp_fsm[unit], p, len);
264 * lcp_extcode - Handle a LCP-specific code.
267 lcp_extcode(f, code, id, inp, len)
277 lcp_rprotrej(f, inp, len);
281 if (f->state != OPENED)
283 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
285 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
286 if (len < CILEN_LONG)
288 fsm_sdata(f, ECHOREP, id, inp, len);
292 lcp_received_echo_reply(f, id, inp, len);
306 * lcp_rprotrej - Receive an Protocol-Reject.
308 * Figure out which protocol is rejected and inform it.
311 lcp_rprotrej(f, inp, len)
318 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
320 if (len < sizeof (u_short)) {
322 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
329 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
333 * Protocol-Reject packets received in any state other than the LCP
334 * OPENED state SHOULD be silently discarded.
336 if( f->state != OPENED ){
337 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
342 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
347 * lcp_protrej - A Protocol-Reject was received.
357 LCPDEBUG((LOG_WARNING,
358 "lcp_protrej: Received Protocol-Reject for LCP!"));
359 fsm_protreject(&lcp_fsm[unit]);
364 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
367 lcp_sprotrej(unit, p, len)
373 * Send back the protocol and the information field of the
374 * rejected packet. We only get here if LCP is in the OPENED state.
379 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
385 * lcp_resetci - Reset our CI.
391 lcp_wantoptions[f->unit].magicnumber = magic();
392 lcp_wantoptions[f->unit].numloops = 0;
393 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
394 peer_mru[f->unit] = MTU;
399 * lcp_cilen - Return length of our CI.
405 lcp_options *go = &lcp_gotoptions[f->unit];
407 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
408 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
409 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
410 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
411 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
413 * NB: we only ask for one of CHAP and UPAP, even if we will
416 return (LENCISHORT(go->neg_mru) +
417 LENCILONG(go->neg_asyncmap) +
418 LENCICHAP(go->neg_chap) +
419 LENCISHORT(!go->neg_chap && go->neg_upap) +
420 LENCILQR(go->neg_lqr) +
421 LENCILONG(go->neg_magicnumber) +
422 LENCIVOID(go->neg_pcompression) +
423 LENCIVOID(go->neg_accompression));
428 * lcp_addci - Add our desired CIs to a packet.
431 lcp_addci(f, ucp, lenp)
436 lcp_options *go = &lcp_gotoptions[f->unit];
437 u_char *start_ucp = ucp;
439 #define ADDCIVOID(opt, neg) \
442 PUTCHAR(CILEN_VOID, ucp); \
444 #define ADDCISHORT(opt, neg, val) \
447 PUTCHAR(CILEN_SHORT, ucp); \
448 PUTSHORT(val, ucp); \
450 #define ADDCICHAP(opt, neg, val, digest) \
453 PUTCHAR(CILEN_CHAP, ucp); \
454 PUTSHORT(val, ucp); \
455 PUTCHAR(digest, ucp); \
457 #define ADDCILONG(opt, neg, val) \
460 PUTCHAR(CILEN_LONG, ucp); \
463 #define ADDCILQR(opt, neg, val) \
466 PUTCHAR(CILEN_LQR, ucp); \
467 PUTSHORT(LQR, ucp); \
471 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
472 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
473 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
474 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
475 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
476 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
477 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
478 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
480 if (ucp - start_ucp != *lenp) {
481 /* this should never happen, because peer_mtu should be 1500 */
482 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
488 * lcp_ackci - Ack our CIs.
489 * This should not modify any state if the Ack is bad.
501 lcp_options *go = &lcp_gotoptions[f->unit];
502 u_char cilen, citype, cichar;
507 * CIs must be in exactly the same order that we sent.
508 * Check packet length and CI length at each step.
509 * If we find any deviations, then this packet is bad.
511 #define ACKCIVOID(opt, neg) \
513 if ((len -= CILEN_VOID) < 0) \
515 GETCHAR(citype, p); \
517 if (cilen != CILEN_VOID || \
521 #define ACKCISHORT(opt, neg, val) \
523 if ((len -= CILEN_SHORT) < 0) \
525 GETCHAR(citype, p); \
527 if (cilen != CILEN_SHORT || \
530 GETSHORT(cishort, p); \
531 if (cishort != val) \
534 #define ACKCICHAP(opt, neg, val, digest) \
536 if ((len -= CILEN_CHAP) < 0) \
538 GETCHAR(citype, p); \
540 if (cilen != CILEN_CHAP || \
543 GETSHORT(cishort, p); \
544 if (cishort != val) \
546 GETCHAR(cichar, p); \
547 if (cichar != digest) \
550 #define ACKCILONG(opt, neg, val) \
552 if ((len -= CILEN_LONG) < 0) \
554 GETCHAR(citype, p); \
556 if (cilen != CILEN_LONG || \
559 GETLONG(cilong, p); \
563 #define ACKCILQR(opt, neg, val) \
565 if ((len -= CILEN_LQR) < 0) \
567 GETCHAR(citype, p); \
569 if (cilen != CILEN_LQR || \
572 GETSHORT(cishort, p); \
573 if (cishort != LQR) \
575 GETLONG(cilong, p); \
580 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
581 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
582 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
583 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
584 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
585 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
586 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
587 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
590 * If there are any remaining CIs, then this packet is bad.
596 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
602 * lcp_nakci - Peer has sent a NAK for some of our CIs.
603 * This should not modify any state if the Nak is bad
604 * or if LCP is in the OPENED state.
616 lcp_options *go = &lcp_gotoptions[f->unit];
617 lcp_options *wo = &lcp_wantoptions[f->unit];
618 u_char cilen, citype, cichar, *next;
621 lcp_options no; /* options we've seen Naks for */
622 lcp_options try; /* options to request next time */
625 BZERO(&no, sizeof(no));
629 * Any Nak'd CIs must be in exactly the same order that we sent.
630 * Check packet length and CI length at each step.
631 * If we find any deviations, then this packet is bad.
633 #define NAKCIVOID(opt, neg, code) \
635 len >= CILEN_VOID && \
636 p[1] == CILEN_VOID && \
639 INCPTR(CILEN_VOID, p); \
643 #define NAKCICHAP(opt, neg, code) \
645 len >= CILEN_CHAP && \
646 p[1] == CILEN_CHAP && \
650 GETSHORT(cishort, p); \
651 GETCHAR(cichar, p); \
655 #define NAKCISHORT(opt, neg, code) \
657 len >= CILEN_SHORT && \
658 p[1] == CILEN_SHORT && \
660 len -= CILEN_SHORT; \
662 GETSHORT(cishort, p); \
666 #define NAKCILONG(opt, neg, code) \
668 len >= CILEN_LONG && \
669 p[1] == CILEN_LONG && \
673 GETLONG(cilong, p); \
677 #define NAKCILQR(opt, neg, code) \
679 len >= CILEN_LQR && \
680 p[1] == CILEN_LQR && \
684 GETSHORT(cishort, p); \
685 GETLONG(cilong, p); \
691 * We don't care if they want to send us smaller packets than
692 * we want. Therefore, accept any MRU less than what we asked for,
693 * but then ignore the new value when setting the MRU in the kernel.
694 * If they send us a bigger MRU than what we asked, accept it, up to
695 * the limit of the default MRU we'd get if we didn't negotiate.
697 NAKCISHORT(CI_MRU, neg_mru,
698 if (cishort <= wo->mru || cishort < DEFMRU)
702 * Add any characters they want to our (receive-side) asyncmap.
704 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
705 try.asyncmap = go->asyncmap | cilong;
708 * If they can't cope with our CHAP hash algorithm, we'll have
709 * to stop asking for CHAP. We haven't got any other algorithm.
711 NAKCICHAP(CI_AUTHTYPE, neg_chap,
715 * Peer shouldn't send Nak for UPAP, protocol compression or
716 * address/control compression requests; they should send
717 * a Reject instead. If they send a Nak, treat it as a Reject.
720 NAKCISHORT(CI_AUTHTYPE, neg_upap,
725 * If they can't cope with our link quality protocol, we'll have
726 * to stop asking for LQR. We haven't got any other protocol.
727 * If they Nak the reporting period, take their value XXX ?
729 NAKCILQR(CI_QUALITY, neg_lqr,
733 try.lqr_period = cilong;
736 * Check for a looped-back line.
738 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
739 try.magicnumber = magic();
744 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
745 try.neg_pcompression = 0;
747 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
748 try.neg_accompression = 0;
752 * There may be remaining CIs, if the peer is requesting negotiation
753 * on an option that we didn't include in our request packet.
754 * If we see an option that we requested, or one we've already seen
755 * in this packet, then this packet is bad.
756 * If we wanted to respond by starting to negotiate on the requested
757 * option(s), we could, but we don't, because except for the
758 * authentication type and quality protocol, if we are not negotiating
759 * an option, it is because we were told not to.
760 * For the authentication type, the Nak from the peer means
761 * `let me authenticate myself with you' which is a bit pointless.
762 * For the quality protocol, the Nak means `ask me to send you quality
763 * reports', but if we didn't ask for them, we don't want them.
765 while (len > CILEN_VOID) {
768 if( (len -= cilen) < 0 )
770 next = p + cilen - 2;
774 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
778 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
782 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
786 if (go->neg_magicnumber || no.neg_magicnumber ||
790 case CI_PCOMPRESSION:
791 if (go->neg_pcompression || no.neg_pcompression
792 || cilen != CILEN_VOID)
795 case CI_ACCOMPRESSION:
796 if (go->neg_accompression || no.neg_accompression
797 || cilen != CILEN_VOID)
801 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
810 /* If there is still anything left, this packet is bad. */
815 * OK, the Nak is good. Now we can update state.
817 if (f->state != OPENED) {
819 if (looped_back && try.numloops % lcp_warnloops == 0)
820 LCPDEBUG((LOG_INFO, "The line appears to be looped back."));
826 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
832 * lcp_rejci - Peer has Rejected some of our CIs.
833 * This should not modify any state if the Reject is bad
834 * or if LCP is in the OPENED state.
837 * 0 - Reject was bad.
838 * 1 - Reject was good.
846 lcp_options *go = &lcp_gotoptions[f->unit];
852 lcp_options try; /* options to request next time */
857 * Any Rejected CIs must be in exactly the same order that we sent.
858 * Check packet length and CI length at each step.
859 * If we find any deviations, then this packet is bad.
861 #define REJCIVOID(opt, neg) \
863 len >= CILEN_VOID && \
864 p[1] == CILEN_VOID && \
867 INCPTR(CILEN_VOID, p); \
869 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
871 #define REJCISHORT(opt, neg, val) \
873 len >= CILEN_SHORT && \
874 p[1] == CILEN_SHORT && \
876 len -= CILEN_SHORT; \
878 GETSHORT(cishort, p); \
879 /* Check rejected value. */ \
880 if (cishort != val) \
883 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
885 #define REJCICHAP(opt, neg, val, digest) \
887 len >= CILEN_CHAP && \
888 p[1] == CILEN_CHAP && \
892 GETSHORT(cishort, p); \
893 GETCHAR(cichar, p); \
894 /* Check rejected value. */ \
895 if (cishort != val || cichar != digest) \
898 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
900 #define REJCILONG(opt, neg, val) \
902 len >= CILEN_LONG && \
903 p[1] == CILEN_LONG && \
907 GETLONG(cilong, p); \
908 /* Check rejected value. */ \
912 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
914 #define REJCILQR(opt, neg, val) \
916 len >= CILEN_LQR && \
917 p[1] == CILEN_LQR && \
921 GETSHORT(cishort, p); \
922 GETLONG(cilong, p); \
923 /* Check rejected value. */ \
924 if (cishort != LQR || cilong != val) \
927 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
930 REJCISHORT(CI_MRU, neg_mru, go->mru);
931 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
932 REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
934 REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
936 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
937 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
938 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
939 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
942 * If there are any remaining CIs, then this packet is bad.
947 * Now we can update state.
949 if (f->state != OPENED)
954 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
955 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
956 plen, len, p - start));
962 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
964 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
965 * appropriately. If reject_if_disagree is non-zero, doesn't return
966 * CONFNAK; returns CONFREJ if it can't return CONFACK.
969 lcp_reqci(f, inp, lenp, reject_if_disagree)
971 u_char *inp; /* Requested CIs */
972 int *lenp; /* Length of requested CIs */
973 int reject_if_disagree;
975 lcp_options *go = &lcp_gotoptions[f->unit];
976 lcp_options *ho = &lcp_hisoptions[f->unit];
977 lcp_options *ao = &lcp_allowoptions[f->unit];
978 u_char *cip, *next; /* Pointer to current and next CIs */
979 u_char cilen, citype, cichar;/* Parsed len, type, char value */
980 u_short cishort; /* Parsed short value */
981 u_long cilong; /* Parse long value */
982 int rc = CONFACK; /* Final packet return code */
983 int orc; /* Individual option return code */
984 u_char *p; /* Pointer to next char to parse */
985 u_char *ucp = inp; /* Pointer to current output char */
986 int l = *lenp; /* Length left */
989 * Reset all his options.
991 BZERO(ho, sizeof(*ho));
994 * Process all his options.
998 orc = CONFACK; /* Assume success */
999 cip = p = next; /* Remember begining of CI */
1000 if (l < 2 || /* Not enough data for CI header or */
1001 p[1] < 2 || /* CI length too small or */
1002 p[1] > l) { /* CI length too big? */
1003 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1004 orc = CONFREJ; /* Reject bad CI */
1005 cilen = l; /* Reject till end of packet */
1006 l = 0; /* Don't loop again */
1009 GETCHAR(citype, p); /* Parse CI type */
1010 GETCHAR(cilen, p); /* Parse CI length */
1011 l -= cilen; /* Adjust remaining length */
1012 next += cilen; /* Step to next CI */
1014 switch (citype) { /* Check CI type */
1016 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1017 if (!ao->neg_mru || /* Allow option? */
1018 cilen != CILEN_SHORT) { /* Check CI length */
1019 orc = CONFREJ; /* Reject CI */
1022 GETSHORT(cishort, p); /* Parse MRU */
1023 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1026 * He must be able to receive at least our minimum.
1027 * No need to check a maximum. If he sends a large number,
1028 * we'll just ignore it.
1030 if (cishort < MINMRU) {
1031 orc = CONFNAK; /* Nak CI */
1032 if( !reject_if_disagree ){
1033 DECPTR(sizeof (short), p); /* Backup */
1034 PUTSHORT(MINMRU, p); /* Give him a hint */
1038 ho->neg_mru = 1; /* Remember he sent MRU */
1039 ho->mru = cishort; /* And remember value */
1043 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1044 if (!ao->neg_asyncmap ||
1045 cilen != CILEN_LONG) {
1050 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1053 * Asyncmap must have set at least the bits
1054 * which are set in lcp_allowoptions[unit].asyncmap.
1056 if ((ao->asyncmap & ~cilong) != 0) {
1058 if( !reject_if_disagree ){
1059 DECPTR(sizeof (long), p);
1060 PUTLONG(ao->asyncmap | cilong, p);
1064 ho->neg_asyncmap = 1;
1065 ho->asyncmap = cilong;
1069 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1070 if (cilen < CILEN_SHORT ||
1071 !(ao->neg_upap || ao->neg_chap)) {
1075 GETSHORT(cishort, p);
1076 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1079 * Authtype must be UPAP or CHAP.
1081 * Note: if both ao->neg_upap and ao->neg_chap are set,
1082 * and the peer sends a Configure-Request with two
1083 * authenticate-protocol requests, one for CHAP and one
1084 * for UPAP, then we will reject the second request.
1085 * Whether we end up doing CHAP or UPAP depends then on
1086 * the ordering of the CIs in the peer's Configure-Request.
1089 if (cishort == UPAP) {
1090 if (!ao->neg_upap || /* we don't want to do PAP */
1091 ho->neg_chap || /* or we've already accepted CHAP */
1092 cilen != CILEN_SHORT) {
1093 LCPDEBUG((LOG_WARNING,
1094 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1101 if (cishort == CHAP) {
1102 if (!ao->neg_chap || /* we don't want to do CHAP */
1103 ho->neg_upap || /* or we've already accepted UPAP */
1104 cilen != CILEN_CHAP) {
1106 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1110 GETCHAR(cichar, p); /* get digest type*/
1111 if (cichar != ao->chap_mdtype) {
1113 if( !reject_if_disagree ){
1114 DECPTR(sizeof (u_char), p);
1115 PUTCHAR(ao->chap_mdtype, p);
1119 ho->chap_mdtype = cichar; /* save md type */
1125 * We don't recognize the protocol they're asking for.
1132 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1134 cilen != CILEN_LQR) {
1139 GETSHORT(cishort, p);
1141 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1142 if (cishort != LQR) {
1148 * Check the reporting period.
1149 * XXX When should we Nak this, and what with?
1153 case CI_MAGICNUMBER:
1154 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1155 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1156 cilen != CILEN_LONG) {
1161 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1164 * He must have a different magic number.
1166 if (go->neg_magicnumber &&
1167 cilong == go->magicnumber) {
1169 DECPTR(sizeof (long), p);
1170 cilong = magic(); /* Don't put magic() inside macro! */
1174 ho->neg_magicnumber = 1;
1175 ho->magicnumber = cilong;
1179 case CI_PCOMPRESSION:
1180 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1181 if (!ao->neg_pcompression ||
1182 cilen != CILEN_VOID) {
1186 ho->neg_pcompression = 1;
1189 case CI_ACCOMPRESSION:
1190 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1191 if (!ao->neg_accompression ||
1192 cilen != CILEN_VOID) {
1196 ho->neg_accompression = 1;
1200 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1207 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1208 if (orc == CONFACK && /* Good CI */
1209 rc != CONFACK) /* but prior CI wasnt? */
1210 continue; /* Don't send this one */
1212 if (orc == CONFNAK) { /* Nak this CI? */
1213 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1214 orc = CONFREJ; /* Get tough if so */
1216 if (rc == CONFREJ) /* Rejecting prior CI? */
1217 continue; /* Don't send this one */
1218 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1219 rc = CONFNAK; /* Not anymore... */
1220 ucp = inp; /* Backup */
1224 if (orc == CONFREJ && /* Reject this CI */
1225 rc != CONFREJ) { /* but no prior ones? */
1227 ucp = inp; /* Backup */
1229 if (ucp != cip) /* Need to move CI? */
1230 BCOPY(cip, ucp, cilen); /* Move it */
1231 INCPTR(cilen, ucp); /* Update output pointer */
1235 * If we wanted to send additional NAKs (for unsent CIs), the
1236 * code would go here. This must be done with care since it might
1237 * require a longer packet than we received. At present there
1238 * are no cases where we want to ask the peer to negotiate an option.
1241 *lenp = ucp - inp; /* Compute output length */
1242 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1243 return (rc); /* Return final code */
1248 * lcp_up - LCP has come UP.
1250 * Start UPAP, IPCP, etc.
1256 lcp_options *wo = &lcp_wantoptions[f->unit];
1257 lcp_options *ho = &lcp_hisoptions[f->unit];
1258 lcp_options *go = &lcp_gotoptions[f->unit];
1259 lcp_options *ao = &lcp_allowoptions[f->unit];
1261 if (!go->neg_magicnumber)
1262 go->magicnumber = 0;
1263 if (!ho->neg_magicnumber)
1264 ho->magicnumber = 0;
1267 * Set our MTU to the smaller of the MTU we wanted and
1268 * the MRU our peer wanted. If we negotiated an MRU,
1269 * set our MRU to the larger of value we wanted and
1270 * the value we got in the negotiation.
1272 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1273 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1274 ho->neg_pcompression, ho->neg_accompression);
1275 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1276 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1277 go->neg_pcompression, go->neg_accompression);
1280 peer_mru[f->unit] = ho->mru;
1282 ChapLowerUp(f->unit); /* Enable CHAP */
1283 upap_lowerup(f->unit); /* Enable UPAP */
1284 ipcp_lowerup(f->unit); /* Enable IPCP */
1285 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1287 link_established(f->unit);
1292 * lcp_down - LCP has gone DOWN.
1294 * Alert other protocols.
1300 lcp_echo_lowerdown(f->unit);
1301 ipcp_lowerdown(f->unit);
1302 ChapLowerDown(f->unit);
1303 upap_lowerdown(f->unit);
1306 ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1307 ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1308 peer_mru[f->unit] = MTU;
1315 * lcp_starting - LCP needs the lower layer up.
1321 link_required(f->unit);
1326 * lcp_finished - LCP has finished with the lower layer.
1332 link_terminated(f->unit);
1337 * lcp_printpkt - print the contents of an LCP packet.
1339 char *lcp_codenames[] = {
1340 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1341 "TermReq", "TermAck", "CodeRej", "ProtRej",
1342 "EchoReq", "EchoRep", "DiscReq"
1346 lcp_printpkt(p, plen, printer, arg)
1349 void (*printer) __ARGS((void *, char *, ...));
1352 int code, id, len, olen;
1353 u_char *pstart, *optend;
1357 if (plen < HEADERLEN)
1363 if (len < HEADERLEN || len > plen)
1366 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1367 printer(arg, " %s", lcp_codenames[code-1]);
1369 printer(arg, " code=0x%x", code);
1370 printer(arg, " id=0x%x", id);
1377 /* print option list */
1382 if (olen < 2 || olen > len) {
1390 if (olen == CILEN_SHORT) {
1392 GETSHORT(cishort, p);
1393 printer(arg, "mru %d", cishort);
1397 if (olen == CILEN_LONG) {
1400 printer(arg, "asyncmap 0x%x", cilong);
1404 if (olen >= CILEN_SHORT) {
1406 printer(arg, "auth ");
1407 GETSHORT(cishort, p);
1410 printer(arg, "upap");
1413 printer(arg, "chap");
1416 printer(arg, "0x%x", cishort);
1421 if (olen >= CILEN_SHORT) {
1423 printer(arg, "quality ");
1424 GETSHORT(cishort, p);
1427 printer(arg, "lqr");
1430 printer(arg, "0x%x", cishort);
1434 case CI_MAGICNUMBER:
1435 if (olen == CILEN_LONG) {
1438 printer(arg, "magic 0x%x", cilong);
1441 case CI_PCOMPRESSION:
1442 if (olen == CILEN_VOID) {
1444 printer(arg, "pcomp");
1447 case CI_ACCOMPRESSION:
1448 if (olen == CILEN_VOID) {
1450 printer(arg, "accomp");
1454 while (p < optend) {
1456 printer(arg, " %.2x", code);
1463 /* print the rest of the bytes in the packet */
1464 for (; len > 0; --len) {
1466 printer(arg, " %.2x", code);
1473 * Time to shut down the link because there is nothing out there.
1477 void LcpLinkFailure (f)
1480 if (f->state == OPENED) {
1481 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1482 lcp_lowerdown(f->unit); /* Reset connection */
1487 * Timer expired for the LCP echo requests from this process.
1496 struct ppp_ddinfo ddinfo;
1499 * Read the time since the last packet was received.
1501 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1502 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1506 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1508 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1509 : ddinfo.ip_rjiffies;
1511 * Compute the time since the last packet was received. If the timer
1512 * has expired then send the echo request and reset the timer to maximum.
1514 delta = (lcp_echo_interval * HZ) - latest;
1515 if (delta < HZ || latest < 0L) {
1516 LcpSendEchoRequest (f);
1517 delta = lcp_echo_interval * HZ;
1521 #else /* Other implementations do not have ability to find delta */
1522 LcpSendEchoRequest (f);
1523 delta = lcp_echo_interval;
1527 * Start the timer for the next interval.
1529 assert (lcp_echo_timer_running==0);
1530 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1531 lcp_echo_timer_running = 1;
1535 * LcpEchoTimeout - Timer expired on the LCP echo
1539 LcpEchoTimeout (arg)
1542 if (lcp_echo_timer_running != 0) {
1543 lcp_echo_timer_running = 0;
1544 LcpEchoCheck ((fsm *) arg);
1549 * LcpEchoReply - LCP has received a reply to the echo
1553 lcp_received_echo_reply (f, id, inp, len)
1555 int id; u_char *inp; int len;
1559 /* Check the magic number - don't count replies from ourselves. */
1560 if (len < CILEN_LONG)
1562 GETLONG(magic, inp);
1563 if (lcp_gotoptions[f->unit].neg_magicnumber
1564 && magic == lcp_gotoptions[f->unit].magicnumber)
1567 /* Reset the number of outstanding echo frames */
1568 lcp_echos_pending = 0;
1572 * LcpSendEchoRequest - Send an echo request frame to the peer
1576 LcpSendEchoRequest (f)
1580 u_char pkt[4], *pktp;
1583 * Detect the failure of the peer at this point.
1585 if (lcp_echo_fails != 0) {
1586 if (lcp_echos_pending++ >= lcp_echo_fails) {
1588 lcp_echos_pending = 0;
1592 * Make and send the echo request frame.
1594 if (f->state == OPENED) {
1595 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1596 ? lcp_gotoptions[f->unit].magicnumber
1599 PUTLONG(lcp_magic, pktp);
1601 fsm_sdata(f, ECHOREQ,
1602 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1607 * lcp_echo_lowerup - Start the timer for the LCP frame
1611 lcp_echo_lowerup (unit)
1614 fsm *f = &lcp_fsm[unit];
1616 /* Clear the parameters for generating echo frames */
1617 lcp_echos_pending = 0;
1618 lcp_echo_number = 0;
1619 lcp_echo_timer_running = 0;
1621 /* If a timeout interval is specified then start the timer */
1622 if (lcp_echo_interval != 0)
1627 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1631 lcp_echo_lowerdown (unit)
1634 fsm *f = &lcp_fsm[unit];
1636 if (lcp_echo_timer_running != 0) {
1637 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1638 lcp_echo_timer_running = 0;