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.6 1994/05/26 06:37:19 paulus Exp $";
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
49 fsm lcp_fsm[NPPP]; /* LCP fsm structure (global)*/
50 lcp_options lcp_wantoptions[NPPP]; /* Options that we want to request */
51 lcp_options lcp_gotoptions[NPPP]; /* Options that peer ack'd */
52 lcp_options lcp_allowoptions[NPPP]; /* Options we allow peer to request */
53 lcp_options lcp_hisoptions[NPPP]; /* Options that we ack'd */
54 u_long xmit_accm[NPPP][8]; /* extended transmit ACCM */
56 static u_long lcp_echos_pending = 0; /* Number of outstanding echo msgs */
57 static u_long lcp_echo_number = 0; /* ID number of next echo frame */
58 static u_long lcp_echo_timer_running = 0; /* TRUE if a timer is running */
60 u_long lcp_echo_interval = 0;
61 u_long lcp_echo_fails = 0;
64 * Callbacks for fsm code. (CI = Configuration Information)
66 static void lcp_resetci __ARGS((fsm *)); /* Reset our CI */
67 static int lcp_cilen __ARGS((fsm *)); /* Return length of our CI */
68 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
69 static int lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
70 static int lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
71 static int lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
72 static int lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
73 static void lcp_up __ARGS((fsm *)); /* We're UP */
74 static void lcp_down __ARGS((fsm *)); /* We're DOWN */
75 static void lcp_starting __ARGS((fsm *)); /* We need lower layer up */
76 static void lcp_finished __ARGS((fsm *)); /* We need lower layer down */
77 static int lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
78 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
81 * routines to send LCP echos to peer
84 static void lcp_echo_lowerup __ARGS((int));
85 static void lcp_echo_lowerdown __ARGS((int));
86 static void LcpEchoTimeout __ARGS((caddr_t));
87 static void lcp_received_echo_reply __ARGS((fsm *, int, u_char *, int));
88 static void LcpSendEchoRequest __ARGS((fsm *));
89 static void LcpLinkFailure __ARGS((fsm *));
91 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
92 lcp_resetci, /* Reset our Configuration Information */
93 lcp_cilen, /* Length of our Configuration Information */
94 lcp_addci, /* Add our Configuration Information */
95 lcp_ackci, /* ACK our Configuration Information */
96 lcp_nakci, /* NAK our Configuration Information */
97 lcp_rejci, /* Reject our Configuration Information */
98 lcp_reqci, /* Request peer's Configuration Information */
99 lcp_up, /* Called when fsm reaches OPENED state */
100 lcp_down, /* Called when fsm leaves OPENED state */
101 lcp_starting, /* Called when we want the lower layer up */
102 lcp_finished, /* Called when we want the lower layer down */
103 NULL, /* Called when Protocol-Reject received */
104 NULL, /* Retransmission is necessary */
105 lcp_extcode, /* Called to handle LCP-specific codes */
106 "LCP" /* String name of protocol */
109 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
112 * Length of each type of configuration option (in octets)
115 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
116 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
117 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
118 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
120 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
121 (x) == CONFNAK ? "NAK" : "REJ")
125 * lcp_init - Initialize LCP.
131 fsm *f = &lcp_fsm[unit];
132 lcp_options *wo = &lcp_wantoptions[unit];
133 lcp_options *ao = &lcp_allowoptions[unit];
137 f->callbacks = &lcp_callbacks;
143 wo->restart = 0; /* Set to 1 in kernels or multi-line
147 wo->neg_asyncmap = 0;
149 wo->neg_chap = 0; /* Set to 1 on server */
150 wo->neg_upap = 0; /* Set to 1 on server */
151 wo->chap_mdtype = CHAP_DIGEST_MD5;
152 wo->neg_magicnumber = 1;
153 wo->neg_pcompression = 1;
154 wo->neg_accompression = 1;
155 wo->neg_lqr = 0; /* no LQR implementation yet */
159 ao->neg_asyncmap = 1;
162 ao->chap_mdtype = CHAP_DIGEST_MD5;
164 ao->neg_magicnumber = 1;
165 ao->neg_pcompression = 1;
166 ao->neg_accompression = 1;
167 ao->neg_lqr = 0; /* no LQR implementation yet */
169 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
170 xmit_accm[unit][3] = 0x60000000;
175 * lcp_open - LCP is allowed to come up.
181 fsm *f = &lcp_fsm[unit];
182 lcp_options *wo = &lcp_wantoptions[unit];
186 f->flags |= OPT_PASSIVE;
188 f->flags |= OPT_SILENT;
194 * lcp_close - Take LCP down.
200 fsm *f = &lcp_fsm[unit];
202 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
204 * This action is not strictly according to the FSM in RFC1548,
205 * but it does mean that the program terminates if you do a
206 * lcp_close(0) in passive/silent mode when a connection hasn't
213 fsm_close(&lcp_fsm[unit]);
218 * lcp_lowerup - The lower layer is up.
225 ppp_set_xaccm(unit, xmit_accm[unit]);
226 ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
227 ppp_recv_config(unit, MTU, 0x00000000, 0, 0);
228 peer_mru[unit] = MTU;
229 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
231 fsm_lowerup(&lcp_fsm[unit]);
236 * lcp_lowerdown - The lower layer is down.
242 fsm_lowerdown(&lcp_fsm[unit]);
247 * lcp_input - Input LCP packet.
250 lcp_input(unit, p, len)
255 fsm_input(&lcp_fsm[unit], p, len);
260 * lcp_extcode - Handle a LCP-specific code.
263 lcp_extcode(f, code, id, inp, len)
273 lcp_rprotrej(f, inp, len);
277 if (f->state != OPENED)
279 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
281 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
282 if (len < CILEN_LONG)
284 fsm_sdata(f, ECHOREP, id, inp, len);
288 lcp_received_echo_reply(f, id, inp, len);
302 * lcp_rprotrej - Receive an Protocol-Reject.
304 * Figure out which protocol is rejected and inform it.
307 lcp_rprotrej(f, inp, len)
314 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
316 if (len < sizeof (u_short)) {
318 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
325 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
329 * Protocol-Reject packets received in any state other than the LCP
330 * OPENED state SHOULD be silently discarded.
332 if( f->state != OPENED ){
333 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
338 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
343 * lcp_protrej - A Protocol-Reject was received.
353 LCPDEBUG((LOG_WARNING,
354 "lcp_protrej: Received Protocol-Reject for LCP!"));
355 fsm_protreject(&lcp_fsm[unit]);
360 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
363 lcp_sprotrej(unit, p, len)
369 * Send back the protocol and the information field of the
370 * rejected packet. We only get here if LCP is in the OPENED state.
375 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
381 * lcp_resetci - Reset our CI.
387 lcp_wantoptions[f->unit].magicnumber = magic();
388 lcp_wantoptions[f->unit].numloops = 0;
389 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
390 peer_mru[f->unit] = MTU;
395 * lcp_cilen - Return length of our CI.
401 lcp_options *go = &lcp_gotoptions[f->unit];
403 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
404 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
405 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
406 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
407 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
409 * NB: we only ask for one of CHAP and UPAP, even if we will
412 return (LENCISHORT(go->neg_mru) +
413 LENCILONG(go->neg_asyncmap) +
414 LENCICHAP(go->neg_chap) +
415 LENCISHORT(!go->neg_chap && go->neg_upap) +
416 LENCILQR(go->neg_lqr) +
417 LENCILONG(go->neg_magicnumber) +
418 LENCIVOID(go->neg_pcompression) +
419 LENCIVOID(go->neg_accompression));
424 * lcp_addci - Add our desired CIs to a packet.
427 lcp_addci(f, ucp, lenp)
432 lcp_options *go = &lcp_gotoptions[f->unit];
433 u_char *start_ucp = ucp;
435 #define ADDCIVOID(opt, neg) \
438 PUTCHAR(CILEN_VOID, ucp); \
440 #define ADDCISHORT(opt, neg, val) \
443 PUTCHAR(CILEN_SHORT, ucp); \
444 PUTSHORT(val, ucp); \
446 #define ADDCICHAP(opt, neg, val, digest) \
449 PUTCHAR(CILEN_CHAP, ucp); \
450 PUTSHORT(val, ucp); \
451 PUTCHAR(digest, ucp); \
453 #define ADDCILONG(opt, neg, val) \
456 PUTCHAR(CILEN_LONG, ucp); \
459 #define ADDCILQR(opt, neg, val) \
462 PUTCHAR(CILEN_LQR, ucp); \
463 PUTSHORT(LQR, ucp); \
467 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
468 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
469 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
470 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
471 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
472 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
473 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
474 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
476 if (ucp - start_ucp != *lenp) {
477 /* this should never happen, because peer_mtu should be 1500 */
478 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
484 * lcp_ackci - Ack our CIs.
485 * This should not modify any state if the Ack is bad.
497 lcp_options *go = &lcp_gotoptions[f->unit];
498 u_char cilen, citype, cichar;
503 * CIs must be in exactly the same order that we sent.
504 * Check packet length and CI length at each step.
505 * If we find any deviations, then this packet is bad.
507 #define ACKCIVOID(opt, neg) \
509 if ((len -= CILEN_VOID) < 0) \
511 GETCHAR(citype, p); \
513 if (cilen != CILEN_VOID || \
517 #define ACKCISHORT(opt, neg, val) \
519 if ((len -= CILEN_SHORT) < 0) \
521 GETCHAR(citype, p); \
523 if (cilen != CILEN_SHORT || \
526 GETSHORT(cishort, p); \
527 if (cishort != val) \
530 #define ACKCICHAP(opt, neg, val, digest) \
532 if ((len -= CILEN_CHAP) < 0) \
534 GETCHAR(citype, p); \
536 if (cilen != CILEN_CHAP || \
539 GETSHORT(cishort, p); \
540 if (cishort != val) \
542 GETCHAR(cichar, p); \
543 if (cichar != digest) \
546 #define ACKCILONG(opt, neg, val) \
548 if ((len -= CILEN_LONG) < 0) \
550 GETCHAR(citype, p); \
552 if (cilen != CILEN_LONG || \
555 GETLONG(cilong, p); \
559 #define ACKCILQR(opt, neg, val) \
561 if ((len -= CILEN_LQR) < 0) \
563 GETCHAR(citype, p); \
565 if (cilen != CILEN_LQR || \
568 GETSHORT(cishort, p); \
569 if (cishort != LQR) \
571 GETLONG(cilong, p); \
576 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
577 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
578 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
579 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
580 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
581 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
582 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
583 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
586 * If there are any remaining CIs, then this packet is bad.
592 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
598 * lcp_nakci - Peer has sent a NAK for some of our CIs.
599 * This should not modify any state if the Nak is bad
600 * or if LCP is in the OPENED state.
612 lcp_options *go = &lcp_gotoptions[f->unit];
613 lcp_options *wo = &lcp_wantoptions[f->unit];
614 u_char cilen, citype, cichar, *next;
617 lcp_options no; /* options we've seen Naks for */
618 lcp_options try; /* options to request next time */
621 BZERO(&no, sizeof(no));
625 * Any Nak'd CIs must be in exactly the same order that we sent.
626 * Check packet length and CI length at each step.
627 * If we find any deviations, then this packet is bad.
629 #define NAKCIVOID(opt, neg, code) \
631 len >= CILEN_VOID && \
632 p[1] == CILEN_VOID && \
635 INCPTR(CILEN_VOID, p); \
639 #define NAKCICHAP(opt, neg, code) \
641 len >= CILEN_CHAP && \
642 p[1] == CILEN_CHAP && \
646 GETSHORT(cishort, p); \
647 GETCHAR(cichar, p); \
651 #define NAKCISHORT(opt, neg, code) \
653 len >= CILEN_SHORT && \
654 p[1] == CILEN_SHORT && \
656 len -= CILEN_SHORT; \
658 GETSHORT(cishort, p); \
662 #define NAKCILONG(opt, neg, code) \
664 len >= CILEN_LONG && \
665 p[1] == CILEN_LONG && \
669 GETLONG(cilong, p); \
673 #define NAKCILQR(opt, neg, code) \
675 len >= CILEN_LQR && \
676 p[1] == CILEN_LQR && \
680 GETSHORT(cishort, p); \
681 GETLONG(cilong, p); \
687 * We don't care if they want to send us smaller packets than
688 * we want. Therefore, accept any MRU less than what we asked for,
689 * but then ignore the new value when setting the MRU in the kernel.
690 * If they send us a bigger MRU than what we asked, accept it, up to
691 * the limit of the default MRU we'd get if we didn't negotiate.
693 NAKCISHORT(CI_MRU, neg_mru,
694 if (cishort <= wo->mru || cishort < DEFMRU)
698 * Add any characters they want to our (receive-side) asyncmap.
700 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
701 try.asyncmap = go->asyncmap | cilong;
704 * If they can't cope with our CHAP hash algorithm, we'll have
705 * to stop asking for CHAP. We haven't got any other algorithm.
707 NAKCICHAP(CI_AUTHTYPE, neg_chap,
711 * Peer shouldn't send Nak for UPAP, protocol compression or
712 * address/control compression requests; they should send
713 * a Reject instead. If they send a Nak, treat it as a Reject.
716 NAKCISHORT(CI_AUTHTYPE, neg_upap,
721 * If they can't cope with our link quality protocol, we'll have
722 * to stop asking for LQR. We haven't got any other protocol.
723 * If they Nak the reporting period, take their value XXX ?
725 NAKCILQR(CI_QUALITY, neg_lqr,
729 try.lqr_period = cilong;
732 * Check for a looped-back line.
734 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
735 try.magicnumber = magic();
740 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
741 try.neg_pcompression = 0;
743 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
744 try.neg_accompression = 0;
748 * There may be remaining CIs, if the peer is requesting negotiation
749 * on an option that we didn't include in our request packet.
750 * If we see an option that we requested, or one we've already seen
751 * in this packet, then this packet is bad.
752 * If we wanted to respond by starting to negotiate on the requested
753 * option(s), we could, but we don't, because except for the
754 * authentication type and quality protocol, if we are not negotiating
755 * an option, it is because we were told not to.
756 * For the authentication type, the Nak from the peer means
757 * `let me authenticate myself with you' which is a bit pointless.
758 * For the quality protocol, the Nak means `ask me to send you quality
759 * reports', but if we didn't ask for them, we don't want them.
761 while (len > CILEN_VOID) {
764 if( (len -= cilen) < 0 )
766 next = p + cilen - 2;
770 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
774 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
778 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
782 if (go->neg_magicnumber || no.neg_magicnumber ||
786 case CI_PCOMPRESSION:
787 if (go->neg_pcompression || no.neg_pcompression
788 || cilen != CILEN_VOID)
791 case CI_ACCOMPRESSION:
792 if (go->neg_accompression || no.neg_accompression
793 || cilen != CILEN_VOID)
797 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
806 /* If there is still anything left, this packet is bad. */
811 * OK, the Nak is good. Now we can update state.
813 if (f->state != OPENED) {
815 if (looped_back && try.numloops % lcp_warnloops == 0)
816 LCPDEBUG((LOG_INFO, "The line appears to be looped back."));
822 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
828 * lcp_rejci - Peer has Rejected some of our CIs.
829 * This should not modify any state if the Reject is bad
830 * or if LCP is in the OPENED state.
833 * 0 - Reject was bad.
834 * 1 - Reject was good.
842 lcp_options *go = &lcp_gotoptions[f->unit];
848 lcp_options try; /* options to request next time */
853 * Any Rejected CIs must be in exactly the same order that we sent.
854 * Check packet length and CI length at each step.
855 * If we find any deviations, then this packet is bad.
857 #define REJCIVOID(opt, neg) \
859 len >= CILEN_VOID && \
860 p[1] == CILEN_VOID && \
863 INCPTR(CILEN_VOID, p); \
865 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
867 #define REJCISHORT(opt, neg, val) \
869 len >= CILEN_SHORT && \
870 p[1] == CILEN_SHORT && \
872 len -= CILEN_SHORT; \
874 GETSHORT(cishort, p); \
875 /* Check rejected value. */ \
876 if (cishort != val) \
879 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
881 #define REJCICHAP(opt, neg, val, digest) \
883 len >= CILEN_CHAP && \
884 p[1] == CILEN_CHAP && \
888 GETSHORT(cishort, p); \
889 GETCHAR(cichar, p); \
890 /* Check rejected value. */ \
891 if (cishort != val || cichar != digest) \
894 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
896 #define REJCILONG(opt, neg, val) \
898 len >= CILEN_LONG && \
899 p[1] == CILEN_LONG && \
903 GETLONG(cilong, p); \
904 /* Check rejected value. */ \
908 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
910 #define REJCILQR(opt, neg, val) \
912 len >= CILEN_LQR && \
913 p[1] == CILEN_LQR && \
917 GETSHORT(cishort, p); \
918 GETLONG(cilong, p); \
919 /* Check rejected value. */ \
920 if (cishort != LQR || cilong != val) \
923 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
926 REJCISHORT(CI_MRU, neg_mru, go->mru);
927 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
928 REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
930 REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
932 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
933 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
934 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
935 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
938 * If there are any remaining CIs, then this packet is bad.
943 * Now we can update state.
945 if (f->state != OPENED)
950 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
951 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
952 plen, len, p - start));
958 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
960 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
961 * appropriately. If reject_if_disagree is non-zero, doesn't return
962 * CONFNAK; returns CONFREJ if it can't return CONFACK.
965 lcp_reqci(f, inp, lenp, reject_if_disagree)
967 u_char *inp; /* Requested CIs */
968 int *lenp; /* Length of requested CIs */
969 int reject_if_disagree;
971 lcp_options *go = &lcp_gotoptions[f->unit];
972 lcp_options *ho = &lcp_hisoptions[f->unit];
973 lcp_options *ao = &lcp_allowoptions[f->unit];
974 u_char *cip, *next; /* Pointer to current and next CIs */
975 u_char cilen, citype, cichar;/* Parsed len, type, char value */
976 u_short cishort; /* Parsed short value */
977 u_long cilong; /* Parse long value */
978 int rc = CONFACK; /* Final packet return code */
979 int orc; /* Individual option return code */
980 u_char *p; /* Pointer to next char to parse */
981 u_char *ucp = inp; /* Pointer to current output char */
982 int l = *lenp; /* Length left */
985 * Reset all his options.
987 BZERO(ho, sizeof(*ho));
990 * Process all his options.
994 orc = CONFACK; /* Assume success */
995 cip = p = next; /* Remember begining of CI */
996 if (l < 2 || /* Not enough data for CI header or */
997 p[1] < 2 || /* CI length too small or */
998 p[1] > l) { /* CI length too big? */
999 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1000 orc = CONFREJ; /* Reject bad CI */
1001 cilen = l; /* Reject till end of packet */
1002 l = 0; /* Don't loop again */
1005 GETCHAR(citype, p); /* Parse CI type */
1006 GETCHAR(cilen, p); /* Parse CI length */
1007 l -= cilen; /* Adjust remaining length */
1008 next += cilen; /* Step to next CI */
1010 switch (citype) { /* Check CI type */
1012 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1013 if (!ao->neg_mru || /* Allow option? */
1014 cilen != CILEN_SHORT) { /* Check CI length */
1015 orc = CONFREJ; /* Reject CI */
1018 GETSHORT(cishort, p); /* Parse MRU */
1019 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1022 * He must be able to receive at least our minimum.
1023 * No need to check a maximum. If he sends a large number,
1024 * we'll just ignore it.
1026 if (cishort < MINMRU) {
1027 orc = CONFNAK; /* Nak CI */
1028 if( !reject_if_disagree ){
1029 DECPTR(sizeof (short), p); /* Backup */
1030 PUTSHORT(MINMRU, p); /* Give him a hint */
1034 ho->neg_mru = 1; /* Remember he sent MRU */
1035 ho->mru = cishort; /* And remember value */
1039 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1040 if (!ao->neg_asyncmap ||
1041 cilen != CILEN_LONG) {
1046 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1049 * Asyncmap must have set at least the bits
1050 * which are set in lcp_allowoptions[unit].asyncmap.
1052 if ((ao->asyncmap & ~cilong) != 0) {
1054 if( !reject_if_disagree ){
1055 DECPTR(sizeof (long), p);
1056 PUTLONG(ao->asyncmap | cilong, p);
1060 ho->neg_asyncmap = 1;
1061 ho->asyncmap = cilong;
1065 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1066 if (cilen < CILEN_SHORT ||
1067 !(ao->neg_upap || ao->neg_chap)) {
1071 GETSHORT(cishort, p);
1072 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1075 * Authtype must be UPAP or CHAP.
1077 * Note: if both ao->neg_upap and ao->neg_chap are set,
1078 * and the peer sends a Configure-Request with two
1079 * authenticate-protocol requests, one for CHAP and one
1080 * for UPAP, then we will reject the second request.
1081 * Whether we end up doing CHAP or UPAP depends then on
1082 * the ordering of the CIs in the peer's Configure-Request.
1085 if (cishort == UPAP) {
1086 if (!ao->neg_upap || /* we don't want to do PAP */
1087 ho->neg_chap || /* or we've already accepted CHAP */
1088 cilen != CILEN_SHORT) {
1089 LCPDEBUG((LOG_WARNING,
1090 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1097 if (cishort == CHAP) {
1098 if (!ao->neg_chap || /* we don't want to do CHAP */
1099 ho->neg_upap || /* or we've already accepted UPAP */
1100 cilen != CILEN_CHAP) {
1102 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1106 GETCHAR(cichar, p); /* get digest type*/
1107 if (cichar != ao->chap_mdtype) {
1109 if( !reject_if_disagree ){
1110 DECPTR(sizeof (u_char), p);
1111 PUTCHAR(ao->chap_mdtype, p);
1115 ho->chap_mdtype = cichar; /* save md type */
1121 * We don't recognize the protocol they're asking for.
1128 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1130 cilen != CILEN_LQR) {
1135 GETSHORT(cishort, p);
1137 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1138 if (cishort != LQR) {
1144 * Check the reporting period.
1145 * XXX When should we Nak this, and what with?
1149 case CI_MAGICNUMBER:
1150 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1151 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1152 cilen != CILEN_LONG) {
1157 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1160 * He must have a different magic number.
1162 if (go->neg_magicnumber &&
1163 cilong == go->magicnumber) {
1165 DECPTR(sizeof (long), p);
1166 cilong = magic(); /* Don't put magic() inside macro! */
1170 ho->neg_magicnumber = 1;
1171 ho->magicnumber = cilong;
1175 case CI_PCOMPRESSION:
1176 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1177 if (!ao->neg_pcompression ||
1178 cilen != CILEN_VOID) {
1182 ho->neg_pcompression = 1;
1185 case CI_ACCOMPRESSION:
1186 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1187 if (!ao->neg_accompression ||
1188 cilen != CILEN_VOID) {
1192 ho->neg_accompression = 1;
1196 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1203 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1204 if (orc == CONFACK && /* Good CI */
1205 rc != CONFACK) /* but prior CI wasnt? */
1206 continue; /* Don't send this one */
1208 if (orc == CONFNAK) { /* Nak this CI? */
1209 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1210 orc = CONFREJ; /* Get tough if so */
1212 if (rc == CONFREJ) /* Rejecting prior CI? */
1213 continue; /* Don't send this one */
1214 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1215 rc = CONFNAK; /* Not anymore... */
1216 ucp = inp; /* Backup */
1220 if (orc == CONFREJ && /* Reject this CI */
1221 rc != CONFREJ) { /* but no prior ones? */
1223 ucp = inp; /* Backup */
1225 if (ucp != cip) /* Need to move CI? */
1226 BCOPY(cip, ucp, cilen); /* Move it */
1227 INCPTR(cilen, ucp); /* Update output pointer */
1231 * If we wanted to send additional NAKs (for unsent CIs), the
1232 * code would go here. This must be done with care since it might
1233 * require a longer packet than we received. At present there
1234 * are no cases where we want to ask the peer to negotiate an option.
1237 *lenp = ucp - inp; /* Compute output length */
1238 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1239 return (rc); /* Return final code */
1244 * lcp_up - LCP has come UP.
1246 * Start UPAP, IPCP, etc.
1252 lcp_options *wo = &lcp_wantoptions[f->unit];
1253 lcp_options *ho = &lcp_hisoptions[f->unit];
1254 lcp_options *go = &lcp_gotoptions[f->unit];
1255 lcp_options *ao = &lcp_allowoptions[f->unit];
1257 if (!go->neg_magicnumber)
1258 go->magicnumber = 0;
1259 if (!ho->neg_magicnumber)
1260 ho->magicnumber = 0;
1263 * Set our MTU to the smaller of the MTU we wanted and
1264 * the MRU our peer wanted. If we negotiated an MRU,
1265 * set our MRU to the larger of value we wanted and
1266 * the value we got in the negotiation.
1268 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1269 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1270 ho->neg_pcompression, ho->neg_accompression);
1271 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1272 (go->neg_asyncmap? go->asyncmap: 0xffffffff),
1273 go->neg_pcompression, go->neg_accompression);
1276 peer_mru[f->unit] = ho->mru;
1278 ChapLowerUp(f->unit); /* Enable CHAP */
1279 upap_lowerup(f->unit); /* Enable UPAP */
1280 ipcp_lowerup(f->unit); /* Enable IPCP */
1281 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1283 link_established(f->unit);
1288 * lcp_down - LCP has gone DOWN.
1290 * Alert other protocols.
1296 lcp_echo_lowerdown(f->unit);
1297 ipcp_lowerdown(f->unit);
1298 ChapLowerDown(f->unit);
1299 upap_lowerdown(f->unit);
1302 ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1303 ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1304 peer_mru[f->unit] = MTU;
1311 * lcp_starting - LCP needs the lower layer up.
1317 link_required(f->unit);
1322 * lcp_finished - LCP has finished with the lower layer.
1328 link_terminated(f->unit);
1333 * lcp_printpkt - print the contents of an LCP packet.
1335 char *lcp_codenames[] = {
1336 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1337 "TermReq", "TermAck", "CodeRej", "ProtRej",
1338 "EchoReq", "EchoRep", "DiscReq"
1342 lcp_printpkt(p, plen, printer, arg)
1345 void (*printer) __ARGS((void *, char *, ...));
1348 int code, id, len, olen;
1349 u_char *pstart, *optend;
1353 if (plen < HEADERLEN)
1359 if (len < HEADERLEN || len > plen)
1362 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1363 printer(arg, " %s", lcp_codenames[code-1]);
1365 printer(arg, " code=0x%x", code);
1366 printer(arg, " id=0x%x", id);
1373 /* print option list */
1378 if (olen < 2 || olen > len) {
1386 if (olen == CILEN_SHORT) {
1388 GETSHORT(cishort, p);
1389 printer(arg, "mru %d", cishort);
1393 if (olen == CILEN_LONG) {
1396 printer(arg, "asyncmap 0x%x", cilong);
1400 if (olen >= CILEN_SHORT) {
1402 printer(arg, "auth ");
1403 GETSHORT(cishort, p);
1406 printer(arg, "upap");
1409 printer(arg, "chap");
1412 printer(arg, "0x%x", cishort);
1417 if (olen >= CILEN_SHORT) {
1419 printer(arg, "quality ");
1420 GETSHORT(cishort, p);
1423 printer(arg, "lqr");
1426 printer(arg, "0x%x", cishort);
1430 case CI_MAGICNUMBER:
1431 if (olen == CILEN_LONG) {
1434 printer(arg, "magic 0x%x", cilong);
1437 case CI_PCOMPRESSION:
1438 if (olen == CILEN_VOID) {
1440 printer(arg, "pcomp");
1443 case CI_ACCOMPRESSION:
1444 if (olen == CILEN_VOID) {
1446 printer(arg, "accomp");
1450 while (p < optend) {
1452 printer(arg, " %.2x", code);
1459 /* print the rest of the bytes in the packet */
1460 for (; len > 0; --len) {
1462 printer(arg, " %.2x", code);
1469 * Time to shut down the link because there is nothing out there.
1473 void LcpLinkFailure (f)
1476 if (f->state == OPENED) {
1477 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1478 lcp_lowerdown(f->unit); /* Reset connection */
1483 * Timer expired for the LCP echo requests from this process.
1492 struct ppp_ddinfo ddinfo;
1495 * Read the time since the last packet was received.
1497 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1498 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1502 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1504 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1505 : ddinfo.ip_rjiffies;
1507 * Compute the time since the last packet was received. If the timer
1508 * has expired then send the echo request and reset the timer to maximum.
1510 delta = (lcp_echo_interval * HZ) - latest;
1511 if (delta < HZ || latest < 0L) {
1512 LcpSendEchoRequest (f);
1513 delta = lcp_echo_interval * HZ;
1517 #else /* Other implementations do not have ability to find delta */
1518 LcpSendEchoRequest (f);
1519 delta = lcp_echo_interval;
1523 * Start the timer for the next interval.
1525 assert (lcp_echo_timer_running==0);
1526 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1527 lcp_echo_timer_running = 1;
1531 * LcpEchoTimeout - Timer expired on the LCP echo
1535 LcpEchoTimeout (arg)
1538 if (lcp_echo_timer_running != 0) {
1539 lcp_echo_timer_running = 0;
1540 LcpEchoCheck ((fsm *) arg);
1545 * LcpEchoReply - LCP has received a reply to the echo
1549 lcp_received_echo_reply (f, id, inp, len)
1551 int id; u_char *inp; int len;
1555 /* Check the magic number - don't count replies from ourselves. */
1556 if (len < CILEN_LONG)
1558 GETLONG(magic, inp);
1559 if (lcp_gotoptions[f->unit].neg_magicnumber
1560 && magic == lcp_gotoptions[f->unit].magicnumber)
1563 /* Reset the number of outstanding echo frames */
1564 lcp_echos_pending = 0;
1568 * LcpSendEchoRequest - Send an echo request frame to the peer
1572 LcpSendEchoRequest (f)
1576 u_char pkt[4], *pktp;
1579 * Detect the failure of the peer at this point.
1581 if (lcp_echo_fails != 0) {
1582 if (lcp_echos_pending++ >= lcp_echo_fails) {
1584 lcp_echos_pending = 0;
1588 * Make and send the echo request frame.
1590 if (f->state == OPENED) {
1591 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1592 ? lcp_gotoptions[f->unit].magicnumber
1595 PUTLONG(lcp_magic, pktp);
1597 fsm_sdata(f, ECHOREQ,
1598 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1603 * lcp_echo_lowerup - Start the timer for the LCP frame
1607 lcp_echo_lowerup (unit)
1610 fsm *f = &lcp_fsm[unit];
1612 /* Clear the parameters for generating echo frames */
1613 lcp_echos_pending = 0;
1614 lcp_echo_number = 0;
1615 lcp_echo_timer_running = 0;
1617 /* If a timeout interval is specified then start the timer */
1618 if (lcp_echo_interval != 0)
1623 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1627 lcp_echo_lowerdown (unit)
1630 fsm *f = &lcp_fsm[unit];
1632 if (lcp_echo_timer_running != 0) {
1633 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1634 lcp_echo_timer_running = 0;