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.12 1994/09/01 00:24:14 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 uint32 xmit_accm[NPPP][8]; /* extended transmit ACCM */
59 static uint32 lcp_echos_pending = 0; /* Number of outstanding echo msgs */
60 static uint32 lcp_echo_number = 0; /* ID number of next echo frame */
61 static uint32 lcp_echo_timer_running = 0; /* TRUE if a timer is running */
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 fsm_sdata(f, ECHOREP, id, inp, len);
286 lcp_received_echo_reply(f, id, inp, len);
300 * lcp_rprotrej - Receive an Protocol-Reject.
302 * Figure out which protocol is rejected and inform it.
305 lcp_rprotrej(f, inp, len)
312 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
314 if (len < sizeof (u_short)) {
316 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
323 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
327 * Protocol-Reject packets received in any state other than the LCP
328 * OPENED state SHOULD be silently discarded.
330 if( f->state != OPENED ){
331 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
336 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
341 * lcp_protrej - A Protocol-Reject was received.
351 LCPDEBUG((LOG_WARNING,
352 "lcp_protrej: Received Protocol-Reject for LCP!"));
353 fsm_protreject(&lcp_fsm[unit]);
358 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
361 lcp_sprotrej(unit, p, len)
367 * Send back the protocol and the information field of the
368 * rejected packet. We only get here if LCP is in the OPENED state.
373 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
379 * lcp_resetci - Reset our CI.
385 lcp_wantoptions[f->unit].magicnumber = magic();
386 lcp_wantoptions[f->unit].numloops = 0;
387 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
388 peer_mru[f->unit] = MTU;
393 * lcp_cilen - Return length of our CI.
399 lcp_options *go = &lcp_gotoptions[f->unit];
401 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
402 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
403 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
404 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
405 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
407 * NB: we only ask for one of CHAP and UPAP, even if we will
410 return (LENCISHORT(go->neg_mru) +
411 LENCILONG(go->neg_asyncmap) +
412 LENCICHAP(go->neg_chap) +
413 LENCISHORT(!go->neg_chap && go->neg_upap) +
414 LENCILQR(go->neg_lqr) +
415 LENCILONG(go->neg_magicnumber) +
416 LENCIVOID(go->neg_pcompression) +
417 LENCIVOID(go->neg_accompression));
422 * lcp_addci - Add our desired CIs to a packet.
425 lcp_addci(f, ucp, lenp)
430 lcp_options *go = &lcp_gotoptions[f->unit];
431 u_char *start_ucp = ucp;
433 #define ADDCIVOID(opt, neg) \
436 PUTCHAR(CILEN_VOID, ucp); \
438 #define ADDCISHORT(opt, neg, val) \
441 PUTCHAR(CILEN_SHORT, ucp); \
442 PUTSHORT(val, ucp); \
444 #define ADDCICHAP(opt, neg, val, digest) \
447 PUTCHAR(CILEN_CHAP, ucp); \
448 PUTSHORT(val, ucp); \
449 PUTCHAR(digest, ucp); \
451 #define ADDCILONG(opt, neg, val) \
454 PUTCHAR(CILEN_LONG, ucp); \
457 #define ADDCILQR(opt, neg, val) \
460 PUTCHAR(CILEN_LQR, ucp); \
461 PUTSHORT(LQR, ucp); \
465 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
466 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
467 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
468 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
469 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
470 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
471 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
472 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
474 if (ucp - start_ucp != *lenp) {
475 /* this should never happen, because peer_mtu should be 1500 */
476 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
482 * lcp_ackci - Ack our CIs.
483 * This should not modify any state if the Ack is bad.
495 lcp_options *go = &lcp_gotoptions[f->unit];
496 u_char cilen, citype, cichar;
501 * CIs must be in exactly the same order that we sent.
502 * Check packet length and CI length at each step.
503 * If we find any deviations, then this packet is bad.
505 #define ACKCIVOID(opt, neg) \
507 if ((len -= CILEN_VOID) < 0) \
509 GETCHAR(citype, p); \
511 if (cilen != CILEN_VOID || \
515 #define ACKCISHORT(opt, neg, val) \
517 if ((len -= CILEN_SHORT) < 0) \
519 GETCHAR(citype, p); \
521 if (cilen != CILEN_SHORT || \
524 GETSHORT(cishort, p); \
525 if (cishort != val) \
528 #define ACKCICHAP(opt, neg, val, digest) \
530 if ((len -= CILEN_CHAP) < 0) \
532 GETCHAR(citype, p); \
534 if (cilen != CILEN_CHAP || \
537 GETSHORT(cishort, p); \
538 if (cishort != val) \
540 GETCHAR(cichar, p); \
541 if (cichar != digest) \
544 #define ACKCILONG(opt, neg, val) \
546 if ((len -= CILEN_LONG) < 0) \
548 GETCHAR(citype, p); \
550 if (cilen != CILEN_LONG || \
553 GETLONG(cilong, p); \
557 #define ACKCILQR(opt, neg, val) \
559 if ((len -= CILEN_LQR) < 0) \
561 GETCHAR(citype, p); \
563 if (cilen != CILEN_LQR || \
566 GETSHORT(cishort, p); \
567 if (cishort != LQR) \
569 GETLONG(cilong, p); \
574 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
575 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
576 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
577 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
578 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
579 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
580 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
581 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
584 * If there are any remaining CIs, then this packet is bad.
590 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
596 * lcp_nakci - Peer has sent a NAK for some of our CIs.
597 * This should not modify any state if the Nak is bad
598 * or if LCP is in the OPENED state.
610 lcp_options *go = &lcp_gotoptions[f->unit];
611 lcp_options *wo = &lcp_wantoptions[f->unit];
612 u_char cilen, citype, cichar, *next;
615 lcp_options no; /* options we've seen Naks for */
616 lcp_options try; /* options to request next time */
619 BZERO(&no, sizeof(no));
623 * Any Nak'd CIs must be in exactly the same order that we sent.
624 * Check packet length and CI length at each step.
625 * If we find any deviations, then this packet is bad.
627 #define NAKCIVOID(opt, neg, code) \
629 len >= CILEN_VOID && \
630 p[1] == CILEN_VOID && \
633 INCPTR(CILEN_VOID, p); \
637 #define NAKCICHAP(opt, neg, code) \
639 len >= CILEN_CHAP && \
640 p[1] == CILEN_CHAP && \
644 GETSHORT(cishort, p); \
645 GETCHAR(cichar, p); \
649 #define NAKCISHORT(opt, neg, code) \
651 len >= CILEN_SHORT && \
652 p[1] == CILEN_SHORT && \
654 len -= CILEN_SHORT; \
656 GETSHORT(cishort, p); \
660 #define NAKCILONG(opt, neg, code) \
662 len >= CILEN_LONG && \
663 p[1] == CILEN_LONG && \
667 GETLONG(cilong, p); \
671 #define NAKCILQR(opt, neg, code) \
673 len >= CILEN_LQR && \
674 p[1] == CILEN_LQR && \
678 GETSHORT(cishort, p); \
679 GETLONG(cilong, p); \
685 * We don't care if they want to send us smaller packets than
686 * we want. Therefore, accept any MRU less than what we asked for,
687 * but then ignore the new value when setting the MRU in the kernel.
688 * If they send us a bigger MRU than what we asked, accept it, up to
689 * the limit of the default MRU we'd get if we didn't negotiate.
691 NAKCISHORT(CI_MRU, neg_mru,
692 if (cishort <= wo->mru || cishort < DEFMRU)
696 * Add any characters they want to our (receive-side) asyncmap.
698 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
699 try.asyncmap = go->asyncmap | cilong;
702 * If they can't cope with our CHAP hash algorithm, we'll have
703 * to stop asking for CHAP. We haven't got any other algorithm.
705 NAKCICHAP(CI_AUTHTYPE, neg_chap,
709 * Peer shouldn't send Nak for UPAP, protocol compression or
710 * address/control compression requests; they should send
711 * a Reject instead. If they send a Nak, treat it as a Reject.
714 NAKCISHORT(CI_AUTHTYPE, neg_upap,
719 * If they can't cope with our link quality protocol, we'll have
720 * to stop asking for LQR. We haven't got any other protocol.
721 * If they Nak the reporting period, take their value XXX ?
723 NAKCILQR(CI_QUALITY, neg_lqr,
727 try.lqr_period = cilong;
730 * Check for a looped-back line.
732 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
733 try.magicnumber = magic();
738 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
739 try.neg_pcompression = 0;
741 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
742 try.neg_accompression = 0;
746 * There may be remaining CIs, if the peer is requesting negotiation
747 * on an option that we didn't include in our request packet.
748 * If we see an option that we requested, or one we've already seen
749 * in this packet, then this packet is bad.
750 * If we wanted to respond by starting to negotiate on the requested
751 * option(s), we could, but we don't, because except for the
752 * authentication type and quality protocol, if we are not negotiating
753 * an option, it is because we were told not to.
754 * For the authentication type, the Nak from the peer means
755 * `let me authenticate myself with you' which is a bit pointless.
756 * For the quality protocol, the Nak means `ask me to send you quality
757 * reports', but if we didn't ask for them, we don't want them.
759 while (len > CILEN_VOID) {
762 if( (len -= cilen) < 0 )
764 next = p + cilen - 2;
768 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
772 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
776 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
780 if (go->neg_magicnumber || no.neg_magicnumber ||
784 case CI_PCOMPRESSION:
785 if (go->neg_pcompression || no.neg_pcompression
786 || cilen != CILEN_VOID)
789 case CI_ACCOMPRESSION:
790 if (go->neg_accompression || no.neg_accompression
791 || cilen != CILEN_VOID)
795 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
804 /* If there is still anything left, this packet is bad. */
809 * OK, the Nak is good. Now we can update state.
811 if (f->state != OPENED) {
813 if (looped_back && try.numloops % lcp_warnloops == 0)
814 syslog(LOG_WARNING, "Serial line appears to be looped back.");
820 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
826 * lcp_rejci - Peer has Rejected some of our CIs.
827 * This should not modify any state if the Reject is bad
828 * or if LCP is in the OPENED state.
831 * 0 - Reject was bad.
832 * 1 - Reject was good.
840 lcp_options *go = &lcp_gotoptions[f->unit];
846 lcp_options try; /* options to request next time */
851 * Any Rejected CIs must be in exactly the same order that we sent.
852 * Check packet length and CI length at each step.
853 * If we find any deviations, then this packet is bad.
855 #define REJCIVOID(opt, neg) \
857 len >= CILEN_VOID && \
858 p[1] == CILEN_VOID && \
861 INCPTR(CILEN_VOID, p); \
863 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
865 #define REJCISHORT(opt, neg, val) \
867 len >= CILEN_SHORT && \
868 p[1] == CILEN_SHORT && \
870 len -= CILEN_SHORT; \
872 GETSHORT(cishort, p); \
873 /* Check rejected value. */ \
874 if (cishort != val) \
877 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
879 #define REJCICHAP(opt, neg, val, digest) \
881 len >= CILEN_CHAP && \
882 p[1] == CILEN_CHAP && \
886 GETSHORT(cishort, p); \
887 GETCHAR(cichar, p); \
888 /* Check rejected value. */ \
889 if (cishort != val || cichar != digest) \
892 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
894 #define REJCILONG(opt, neg, val) \
896 len >= CILEN_LONG && \
897 p[1] == CILEN_LONG && \
901 GETLONG(cilong, p); \
902 /* Check rejected value. */ \
906 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
908 #define REJCILQR(opt, neg, val) \
910 len >= CILEN_LQR && \
911 p[1] == CILEN_LQR && \
915 GETSHORT(cishort, p); \
916 GETLONG(cilong, p); \
917 /* Check rejected value. */ \
918 if (cishort != LQR || cilong != val) \
921 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
924 REJCISHORT(CI_MRU, neg_mru, go->mru);
925 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
926 REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
928 REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
930 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
931 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
932 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
933 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
936 * If there are any remaining CIs, then this packet is bad.
941 * Now we can update state.
943 if (f->state != OPENED)
948 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
949 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
950 plen, len, p - start));
956 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
958 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
959 * appropriately. If reject_if_disagree is non-zero, doesn't return
960 * CONFNAK; returns CONFREJ if it can't return CONFACK.
963 lcp_reqci(f, inp, lenp, reject_if_disagree)
965 u_char *inp; /* Requested CIs */
966 int *lenp; /* Length of requested CIs */
967 int reject_if_disagree;
969 lcp_options *go = &lcp_gotoptions[f->unit];
970 lcp_options *ho = &lcp_hisoptions[f->unit];
971 lcp_options *ao = &lcp_allowoptions[f->unit];
972 u_char *cip, *next; /* Pointer to current and next CIs */
973 u_char cilen, citype, cichar;/* Parsed len, type, char value */
974 u_short cishort; /* Parsed short value */
975 uint32 cilong; /* Parse long value */
976 int rc = CONFACK; /* Final packet return code */
977 int orc; /* Individual option return code */
978 u_char *p; /* Pointer to next char to parse */
979 u_char *ucp = inp; /* Pointer to current output char */
980 int l = *lenp; /* Length left */
983 * Reset all his options.
985 BZERO(ho, sizeof(*ho));
988 * Process all his options.
992 orc = CONFACK; /* Assume success */
993 cip = p = next; /* Remember begining of CI */
994 if (l < 2 || /* Not enough data for CI header or */
995 p[1] < 2 || /* CI length too small or */
996 p[1] > l) { /* CI length too big? */
997 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
998 orc = CONFREJ; /* Reject bad CI */
999 cilen = l; /* Reject till end of packet */
1000 l = 0; /* Don't loop again */
1003 GETCHAR(citype, p); /* Parse CI type */
1004 GETCHAR(cilen, p); /* Parse CI length */
1005 l -= cilen; /* Adjust remaining length */
1006 next += cilen; /* Step to next CI */
1008 switch (citype) { /* Check CI type */
1010 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1011 if (!ao->neg_mru || /* Allow option? */
1012 cilen != CILEN_SHORT) { /* Check CI length */
1013 orc = CONFREJ; /* Reject CI */
1016 GETSHORT(cishort, p); /* Parse MRU */
1017 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1020 * He must be able to receive at least our minimum.
1021 * No need to check a maximum. If he sends a large number,
1022 * we'll just ignore it.
1024 if (cishort < MINMRU) {
1025 orc = CONFNAK; /* Nak CI */
1026 if( !reject_if_disagree ){
1027 DECPTR(sizeof (short), p); /* Backup */
1028 PUTSHORT(MINMRU, p); /* Give him a hint */
1032 ho->neg_mru = 1; /* Remember he sent MRU */
1033 ho->mru = cishort; /* And remember value */
1037 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1038 if (!ao->neg_asyncmap ||
1039 cilen != CILEN_LONG) {
1044 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1047 * Asyncmap must have set at least the bits
1048 * which are set in lcp_allowoptions[unit].asyncmap.
1050 if ((ao->asyncmap & ~cilong) != 0) {
1052 if( !reject_if_disagree ){
1053 DECPTR(sizeof (long), p);
1054 PUTLONG(ao->asyncmap | cilong, p);
1058 ho->neg_asyncmap = 1;
1059 ho->asyncmap = cilong;
1063 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1064 if (cilen < CILEN_SHORT ||
1065 !(ao->neg_upap || ao->neg_chap)) {
1069 GETSHORT(cishort, p);
1070 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1073 * Authtype must be UPAP or CHAP.
1075 * Note: if both ao->neg_upap and ao->neg_chap are set,
1076 * and the peer sends a Configure-Request with two
1077 * authenticate-protocol requests, one for CHAP and one
1078 * for UPAP, then we will reject the second request.
1079 * Whether we end up doing CHAP or UPAP depends then on
1080 * the ordering of the CIs in the peer's Configure-Request.
1083 if (cishort == UPAP) {
1084 if (!ao->neg_upap || /* we don't want to do PAP */
1085 ho->neg_chap || /* or we've already accepted CHAP */
1086 cilen != CILEN_SHORT) {
1087 LCPDEBUG((LOG_WARNING,
1088 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1095 if (cishort == CHAP) {
1096 if (!ao->neg_chap || /* we don't want to do CHAP */
1097 ho->neg_upap || /* or we've already accepted UPAP */
1098 cilen != CILEN_CHAP) {
1100 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1104 GETCHAR(cichar, p); /* get digest type*/
1105 if (cichar != ao->chap_mdtype) {
1107 if( !reject_if_disagree ){
1108 DECPTR(sizeof (u_char), p);
1109 PUTCHAR(ao->chap_mdtype, p);
1113 ho->chap_mdtype = cichar; /* save md type */
1119 * We don't recognize the protocol they're asking for.
1126 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1128 cilen != CILEN_LQR) {
1133 GETSHORT(cishort, p);
1135 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1136 if (cishort != LQR) {
1142 * Check the reporting period.
1143 * XXX When should we Nak this, and what with?
1147 case CI_MAGICNUMBER:
1148 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1149 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1150 cilen != CILEN_LONG) {
1155 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1158 * He must have a different magic number.
1160 if (go->neg_magicnumber &&
1161 cilong == go->magicnumber) {
1163 DECPTR(sizeof (long), p);
1164 cilong = magic(); /* Don't put magic() inside macro! */
1168 ho->neg_magicnumber = 1;
1169 ho->magicnumber = cilong;
1173 case CI_PCOMPRESSION:
1174 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1175 if (!ao->neg_pcompression ||
1176 cilen != CILEN_VOID) {
1180 ho->neg_pcompression = 1;
1183 case CI_ACCOMPRESSION:
1184 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1185 if (!ao->neg_accompression ||
1186 cilen != CILEN_VOID) {
1190 ho->neg_accompression = 1;
1194 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1201 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1202 if (orc == CONFACK && /* Good CI */
1203 rc != CONFACK) /* but prior CI wasnt? */
1204 continue; /* Don't send this one */
1206 if (orc == CONFNAK) { /* Nak this CI? */
1207 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1208 orc = CONFREJ; /* Get tough if so */
1210 if (rc == CONFREJ) /* Rejecting prior CI? */
1211 continue; /* Don't send this one */
1212 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1213 rc = CONFNAK; /* Not anymore... */
1214 ucp = inp; /* Backup */
1218 if (orc == CONFREJ && /* Reject this CI */
1219 rc != CONFREJ) { /* but no prior ones? */
1221 ucp = inp; /* Backup */
1223 if (ucp != cip) /* Need to move CI? */
1224 BCOPY(cip, ucp, cilen); /* Move it */
1225 INCPTR(cilen, ucp); /* Update output pointer */
1229 * If we wanted to send additional NAKs (for unsent CIs), the
1230 * code would go here. This must be done with care since it might
1231 * require a longer packet than we received. At present there
1232 * are no cases where we want to ask the peer to negotiate an option.
1235 *lenp = ucp - inp; /* Compute output length */
1236 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1237 return (rc); /* Return final code */
1242 * lcp_up - LCP has come UP.
1244 * Start UPAP, IPCP, etc.
1250 lcp_options *wo = &lcp_wantoptions[f->unit];
1251 lcp_options *ho = &lcp_hisoptions[f->unit];
1252 lcp_options *go = &lcp_gotoptions[f->unit];
1253 lcp_options *ao = &lcp_allowoptions[f->unit];
1255 if (!go->neg_magicnumber)
1256 go->magicnumber = 0;
1257 if (!ho->neg_magicnumber)
1258 ho->magicnumber = 0;
1261 * Set our MTU to the smaller of the MTU we wanted and
1262 * the MRU our peer wanted. If we negotiated an MRU,
1263 * set our MRU to the larger of value we wanted and
1264 * the value we got in the negotiation.
1266 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1267 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1268 ho->neg_pcompression, ho->neg_accompression);
1270 * If the asyncmap hasn't been negotiated, we really should
1271 * set the receive asyncmap to ffffffff, but we set it to 0
1272 * for backwards contemptibility.
1274 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1275 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1276 go->neg_pcompression, go->neg_accompression);
1279 peer_mru[f->unit] = ho->mru;
1281 ChapLowerUp(f->unit); /* Enable CHAP */
1282 upap_lowerup(f->unit); /* Enable UPAP */
1283 ipcp_lowerup(f->unit); /* Enable IPCP */
1284 ccp_lowerup(f->unit); /* Enable CCP */
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 ccp_lowerdown(f->unit);
1302 ipcp_lowerdown(f->unit);
1303 ChapLowerDown(f->unit);
1304 upap_lowerdown(f->unit);
1307 ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1308 ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1309 peer_mru[f->unit] = MTU;
1316 * lcp_starting - LCP needs the lower layer up.
1322 link_required(f->unit);
1327 * lcp_finished - LCP has finished with the lower layer.
1333 link_terminated(f->unit);
1338 * lcp_printpkt - print the contents of an LCP packet.
1340 char *lcp_codenames[] = {
1341 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1342 "TermReq", "TermAck", "CodeRej", "ProtRej",
1343 "EchoReq", "EchoRep", "DiscReq"
1347 lcp_printpkt(p, plen, printer, arg)
1350 void (*printer) __ARGS((void *, char *, ...));
1353 int code, id, len, olen;
1354 u_char *pstart, *optend;
1358 if (plen < HEADERLEN)
1364 if (len < HEADERLEN || len > plen)
1367 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1368 printer(arg, " %s", lcp_codenames[code-1]);
1370 printer(arg, " code=0x%x", code);
1371 printer(arg, " id=0x%x", id);
1378 /* print option list */
1383 if (olen < 2 || olen > len) {
1391 if (olen == CILEN_SHORT) {
1393 GETSHORT(cishort, p);
1394 printer(arg, "mru %d", cishort);
1398 if (olen == CILEN_LONG) {
1401 printer(arg, "asyncmap 0x%x", cilong);
1405 if (olen >= CILEN_SHORT) {
1407 printer(arg, "auth ");
1408 GETSHORT(cishort, p);
1411 printer(arg, "upap");
1414 printer(arg, "chap");
1417 printer(arg, "0x%x", cishort);
1422 if (olen >= CILEN_SHORT) {
1424 printer(arg, "quality ");
1425 GETSHORT(cishort, p);
1428 printer(arg, "lqr");
1431 printer(arg, "0x%x", cishort);
1435 case CI_MAGICNUMBER:
1436 if (olen == CILEN_LONG) {
1439 printer(arg, "magic 0x%x", cilong);
1442 case CI_PCOMPRESSION:
1443 if (olen == CILEN_VOID) {
1445 printer(arg, "pcomp");
1448 case CI_ACCOMPRESSION:
1449 if (olen == CILEN_VOID) {
1451 printer(arg, "accomp");
1455 while (p < optend) {
1457 printer(arg, " %.2x", code);
1464 /* print the rest of the bytes in the packet */
1465 for (; len > 0; --len) {
1467 printer(arg, " %.2x", code);
1474 * Time to shut down the link because there is nothing out there.
1478 void LcpLinkFailure (f)
1481 if (f->state == OPENED) {
1482 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1483 lcp_close(f->unit); /* Reset connection */
1488 * Timer expired for the LCP echo requests from this process.
1497 struct ppp_ddinfo ddinfo;
1500 * Read the time since the last packet was received.
1502 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1503 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1507 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1509 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1510 : ddinfo.ip_rjiffies;
1512 * Compute the time since the last packet was received. If the timer
1513 * has expired then send the echo request and reset the timer to maximum.
1515 delta = (lcp_echo_interval * HZ) - latest;
1516 if (delta < HZ || latest < 0L) {
1517 LcpSendEchoRequest (f);
1518 delta = lcp_echo_interval * HZ;
1522 #else /* Other implementations do not have ability to find delta */
1523 LcpSendEchoRequest (f);
1524 delta = lcp_echo_interval;
1528 * Start the timer for the next interval.
1530 assert (lcp_echo_timer_running==0);
1531 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1532 lcp_echo_timer_running = 1;
1536 * LcpEchoTimeout - Timer expired on the LCP echo
1540 LcpEchoTimeout (arg)
1543 if (lcp_echo_timer_running != 0) {
1544 lcp_echo_timer_running = 0;
1545 LcpEchoCheck ((fsm *) arg);
1550 * LcpEchoReply - LCP has received a reply to the echo
1554 lcp_received_echo_reply (f, id, inp, len)
1556 int id; u_char *inp; int len;
1560 /* Check the magic number - don't count replies from ourselves. */
1562 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1565 GETLONG(magic, inp);
1566 if (lcp_gotoptions[f->unit].neg_magicnumber
1567 && magic == lcp_gotoptions[f->unit].magicnumber) {
1568 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1572 /* Reset the number of outstanding echo frames */
1573 lcp_echos_pending = 0;
1577 * LcpSendEchoRequest - Send an echo request frame to the peer
1581 LcpSendEchoRequest (f)
1585 u_char pkt[4], *pktp;
1588 * Detect the failure of the peer at this point.
1590 if (lcp_echo_fails != 0) {
1591 if (lcp_echos_pending++ >= lcp_echo_fails) {
1593 lcp_echos_pending = 0;
1597 * Make and send the echo request frame.
1599 if (f->state == OPENED) {
1600 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1601 ? lcp_gotoptions[f->unit].magicnumber
1604 PUTLONG(lcp_magic, pktp);
1606 fsm_sdata(f, ECHOREQ,
1607 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1612 * lcp_echo_lowerup - Start the timer for the LCP frame
1616 lcp_echo_lowerup (unit)
1619 fsm *f = &lcp_fsm[unit];
1621 /* Clear the parameters for generating echo frames */
1622 lcp_echos_pending = 0;
1623 lcp_echo_number = 0;
1624 lcp_echo_timer_running = 0;
1626 /* If a timeout interval is specified then start the timer */
1627 if (lcp_echo_interval != 0)
1632 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1636 lcp_echo_lowerdown (unit)
1639 fsm *f = &lcp_fsm[unit];
1641 if (lcp_echo_timer_running != 0) {
1642 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1643 lcp_echo_timer_running = 0;