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.24 1996/01/01 22:57:47 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
45 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
46 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
47 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
48 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
49 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
50 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
52 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
53 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
54 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
56 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
59 * Callbacks for fsm code. (CI = Configuration Information)
61 static void lcp_resetci __P((fsm *)); /* Reset our CI */
62 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
63 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
64 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
65 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
66 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
67 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
68 static void lcp_up __P((fsm *)); /* We're UP */
69 static void lcp_down __P((fsm *)); /* We're DOWN */
70 static void lcp_starting __P((fsm *)); /* We need lower layer up */
71 static void lcp_finished __P((fsm *)); /* We need lower layer down */
72 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
73 static void lcp_rprotrej __P((fsm *, u_char *, int));
76 * routines to send LCP echos to peer
79 static void lcp_echo_lowerup __P((int));
80 static void lcp_echo_lowerdown __P((int));
81 static void LcpEchoTimeout __P((caddr_t));
82 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
83 static void LcpSendEchoRequest __P((fsm *));
84 static void LcpLinkFailure __P((fsm *));
86 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
87 lcp_resetci, /* Reset our Configuration Information */
88 lcp_cilen, /* Length of our Configuration Information */
89 lcp_addci, /* Add our Configuration Information */
90 lcp_ackci, /* ACK our Configuration Information */
91 lcp_nakci, /* NAK our Configuration Information */
92 lcp_rejci, /* Reject our Configuration Information */
93 lcp_reqci, /* Request peer's Configuration Information */
94 lcp_up, /* Called when fsm reaches OPENED state */
95 lcp_down, /* Called when fsm leaves OPENED state */
96 lcp_starting, /* Called when we want the lower layer up */
97 lcp_finished, /* Called when we want the lower layer down */
98 NULL, /* Called when Protocol-Reject received */
99 NULL, /* Retransmission is necessary */
100 lcp_extcode, /* Called to handle LCP-specific codes */
101 "LCP" /* String name of protocol */
104 struct protent lcp_protent = {
105 PPP_LCP, lcp_init, lcp_input, lcp_protrej,
106 lcp_lowerup, lcp_lowerdown, lcp_open, lcp_close,
107 lcp_printpkt, NULL, 1, "LCP", NULL, NULL
110 int lcp_loopbackfail = DEFLOOPBACKFAIL;
113 * Length of each type of configuration option (in octets)
116 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
117 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
118 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
119 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
121 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
122 (x) == CONFNAK ? "NAK" : "REJ")
126 * lcp_init - Initialize LCP.
132 fsm *f = &lcp_fsm[unit];
133 lcp_options *wo = &lcp_wantoptions[unit];
134 lcp_options *ao = &lcp_allowoptions[unit];
137 f->protocol = PPP_LCP;
138 f->callbacks = &lcp_callbacks;
144 wo->restart = 0; /* Set to 1 in kernels or multi-line
148 wo->neg_asyncmap = 0;
150 wo->neg_chap = 0; /* Set to 1 on server */
151 wo->neg_upap = 0; /* Set to 1 on server */
152 wo->chap_mdtype = CHAP_DIGEST_MD5;
153 wo->neg_magicnumber = 1;
154 wo->neg_pcompression = 1;
155 wo->neg_accompression = 1;
156 wo->neg_lqr = 0; /* no LQR implementation yet */
160 ao->neg_asyncmap = 1;
163 ao->chap_mdtype = CHAP_DIGEST_MD5;
165 ao->neg_magicnumber = 1;
166 ao->neg_pcompression = 1;
167 ao->neg_accompression = 1;
168 ao->neg_lqr = 0; /* no LQR implementation yet */
170 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
171 xmit_accm[unit][3] = 0x60000000;
176 * lcp_open - LCP is allowed to come up.
182 fsm *f = &lcp_fsm[unit];
183 lcp_options *wo = &lcp_wantoptions[unit];
187 f->flags |= OPT_PASSIVE;
189 f->flags |= OPT_SILENT;
195 * lcp_close - Take LCP down.
198 lcp_close(unit, reason)
202 fsm *f = &lcp_fsm[unit];
204 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
206 * This action is not strictly according to the FSM in RFC1548,
207 * but it does mean that the program terminates if you do a
208 * lcp_close() in passive/silent mode when a connection hasn't
215 fsm_close(&lcp_fsm[unit], reason);
220 * lcp_lowerup - The lower layer is up.
226 lcp_options *wo = &lcp_wantoptions[unit];
229 * Don't use A/C or protocol compression on transmission,
230 * but accept A/C and protocol compressed packets
231 * if we are going to ask for A/C and protocol compression.
233 ppp_set_xaccm(unit, xmit_accm[unit]);
234 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
235 ppp_recv_config(unit, PPP_MRU, 0x00000000,
236 wo->neg_pcompression, wo->neg_accompression);
237 peer_mru[unit] = PPP_MRU;
238 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
240 fsm_lowerup(&lcp_fsm[unit]);
245 * lcp_lowerdown - The lower layer is down.
251 fsm_lowerdown(&lcp_fsm[unit]);
256 * lcp_input - Input LCP packet.
259 lcp_input(unit, p, len)
264 fsm *f = &lcp_fsm[unit];
266 fsm_input(f, p, len);
271 * lcp_extcode - Handle a LCP-specific code.
274 lcp_extcode(f, code, id, inp, len)
284 lcp_rprotrej(f, inp, len);
288 if (f->state != OPENED)
290 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
292 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
293 fsm_sdata(f, ECHOREP, id, inp, len);
297 lcp_received_echo_reply(f, id, inp, len);
311 * lcp_rprotrej - Receive an Protocol-Reject.
313 * Figure out which protocol is rejected and inform it.
316 lcp_rprotrej(f, inp, len)
322 struct protent *protp;
325 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
327 if (len < sizeof (u_short)) {
329 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
336 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
340 * Protocol-Reject packets received in any state other than the LCP
341 * OPENED state SHOULD be silently discarded.
343 if( f->state != OPENED ){
344 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
350 * Upcall the proper Protocol-Reject routine.
352 for (i = 0; (protp = protocols[i]) != NULL; ++i)
353 if (protp->protocol == prot && protp->enabled_flag) {
354 (*protp->protrej)(f->unit);
358 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
364 * lcp_protrej - A Protocol-Reject was received.
374 LCPDEBUG((LOG_WARNING,
375 "lcp_protrej: Received Protocol-Reject for LCP!"));
376 fsm_protreject(&lcp_fsm[unit]);
381 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
384 lcp_sprotrej(unit, p, len)
390 * Send back the protocol and the information field of the
391 * rejected packet. We only get here if LCP is in the OPENED state.
396 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
402 * lcp_resetci - Reset our CI.
408 lcp_wantoptions[f->unit].magicnumber = magic();
409 lcp_wantoptions[f->unit].numloops = 0;
410 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
411 peer_mru[f->unit] = PPP_MRU;
416 * lcp_cilen - Return length of our CI.
422 lcp_options *go = &lcp_gotoptions[f->unit];
424 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
425 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
426 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
427 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
428 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
430 * NB: we only ask for one of CHAP and UPAP, even if we will
433 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
434 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
435 LENCICHAP(go->neg_chap) +
436 LENCISHORT(!go->neg_chap && go->neg_upap) +
437 LENCILQR(go->neg_lqr) +
438 LENCILONG(go->neg_magicnumber) +
439 LENCIVOID(go->neg_pcompression) +
440 LENCIVOID(go->neg_accompression));
445 * lcp_addci - Add our desired CIs to a packet.
448 lcp_addci(f, ucp, lenp)
453 lcp_options *go = &lcp_gotoptions[f->unit];
454 u_char *start_ucp = ucp;
456 #define ADDCIVOID(opt, neg) \
459 PUTCHAR(CILEN_VOID, ucp); \
461 #define ADDCISHORT(opt, neg, val) \
464 PUTCHAR(CILEN_SHORT, ucp); \
465 PUTSHORT(val, ucp); \
467 #define ADDCICHAP(opt, neg, val, digest) \
470 PUTCHAR(CILEN_CHAP, ucp); \
471 PUTSHORT(val, ucp); \
472 PUTCHAR(digest, ucp); \
474 #define ADDCILONG(opt, neg, val) \
477 PUTCHAR(CILEN_LONG, ucp); \
480 #define ADDCILQR(opt, neg, val) \
483 PUTCHAR(CILEN_LQR, ucp); \
484 PUTSHORT(PPP_LQR, ucp); \
488 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
489 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
491 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
492 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
493 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
494 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
495 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
496 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
498 if (ucp - start_ucp != *lenp) {
499 /* this should never happen, because peer_mtu should be 1500 */
500 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
506 * lcp_ackci - Ack our CIs.
507 * This should not modify any state if the Ack is bad.
519 lcp_options *go = &lcp_gotoptions[f->unit];
520 u_char cilen, citype, cichar;
525 * CIs must be in exactly the same order that we sent.
526 * Check packet length and CI length at each step.
527 * If we find any deviations, then this packet is bad.
529 #define ACKCIVOID(opt, neg) \
531 if ((len -= CILEN_VOID) < 0) \
533 GETCHAR(citype, p); \
535 if (cilen != CILEN_VOID || \
539 #define ACKCISHORT(opt, neg, val) \
541 if ((len -= CILEN_SHORT) < 0) \
543 GETCHAR(citype, p); \
545 if (cilen != CILEN_SHORT || \
548 GETSHORT(cishort, p); \
549 if (cishort != val) \
552 #define ACKCICHAP(opt, neg, val, digest) \
554 if ((len -= CILEN_CHAP) < 0) \
556 GETCHAR(citype, p); \
558 if (cilen != CILEN_CHAP || \
561 GETSHORT(cishort, p); \
562 if (cishort != val) \
564 GETCHAR(cichar, p); \
565 if (cichar != digest) \
568 #define ACKCILONG(opt, neg, val) \
570 if ((len -= CILEN_LONG) < 0) \
572 GETCHAR(citype, p); \
574 if (cilen != CILEN_LONG || \
577 GETLONG(cilong, p); \
581 #define ACKCILQR(opt, neg, val) \
583 if ((len -= CILEN_LQR) < 0) \
585 GETCHAR(citype, p); \
587 if (cilen != CILEN_LQR || \
590 GETSHORT(cishort, p); \
591 if (cishort != PPP_LQR) \
593 GETLONG(cilong, p); \
598 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
599 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
601 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
602 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
603 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
604 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
605 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
606 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
609 * If there are any remaining CIs, then this packet is bad.
615 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
621 * lcp_nakci - Peer has sent a NAK for some of our CIs.
622 * This should not modify any state if the Nak is bad
623 * or if LCP is in the OPENED state.
635 lcp_options *go = &lcp_gotoptions[f->unit];
636 lcp_options *wo = &lcp_wantoptions[f->unit];
637 u_char citype, cichar, *next;
640 lcp_options no; /* options we've seen Naks for */
641 lcp_options try; /* options to request next time */
645 BZERO(&no, sizeof(no));
649 * Any Nak'd CIs must be in exactly the same order that we sent.
650 * Check packet length and CI length at each step.
651 * If we find any deviations, then this packet is bad.
653 #define NAKCIVOID(opt, neg, code) \
655 len >= CILEN_VOID && \
656 p[1] == CILEN_VOID && \
659 INCPTR(CILEN_VOID, p); \
663 #define NAKCICHAP(opt, neg, code) \
665 len >= CILEN_CHAP && \
666 p[1] == CILEN_CHAP && \
670 GETSHORT(cishort, p); \
671 GETCHAR(cichar, p); \
675 #define NAKCISHORT(opt, neg, code) \
677 len >= CILEN_SHORT && \
678 p[1] == CILEN_SHORT && \
680 len -= CILEN_SHORT; \
682 GETSHORT(cishort, p); \
686 #define NAKCILONG(opt, neg, code) \
688 len >= CILEN_LONG && \
689 p[1] == CILEN_LONG && \
693 GETLONG(cilong, p); \
697 #define NAKCILQR(opt, neg, code) \
699 len >= CILEN_LQR && \
700 p[1] == CILEN_LQR && \
704 GETSHORT(cishort, p); \
705 GETLONG(cilong, p); \
711 * We don't care if they want to send us smaller packets than
712 * we want. Therefore, accept any MRU less than what we asked for,
713 * but then ignore the new value when setting the MRU in the kernel.
714 * If they send us a bigger MRU than what we asked, accept it, up to
715 * the limit of the default MRU we'd get if we didn't negotiate.
717 if (go->neg_mru && go->mru != DEFMRU) {
718 NAKCISHORT(CI_MRU, neg_mru,
719 if (cishort <= wo->mru || cishort < DEFMRU)
725 * Add any characters they want to our (receive-side) asyncmap.
727 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
728 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
729 try.asyncmap = go->asyncmap | cilong;
734 * If they've nak'd our authentication-protocol, check whether
735 * they are proposing a different protocol, or a different
736 * hash algorithm for CHAP.
738 if ((go->neg_chap || go->neg_upap)
739 && len >= CILEN_SHORT
740 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT) {
743 GETSHORT(cishort, p);
744 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
746 * If they are asking for PAP, then they don't want to do CHAP.
747 * If we weren't asking for CHAP, then we were asking for PAP,
748 * in which case this Nak is bad.
754 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
758 * We were asking for CHAP/MD5; they must want a different
759 * algorithm. If they can't do MD5, we'll have to stop
762 if (cichar != go->chap_mdtype)
766 * Stop asking for PAP if we were asking for it.
773 * We don't recognize what they're suggesting.
774 * Stop asking for what we were asking for.
780 p += cilen - CILEN_SHORT;
785 * Peer shouldn't send Nak for protocol compression or
786 * address/control compression requests; they should send
787 * a Reject instead. If they send a Nak, treat it as a Reject.
790 NAKCISHORT(CI_AUTHTYPE, neg_upap,
796 * If they can't cope with our link quality protocol, we'll have
797 * to stop asking for LQR. We haven't got any other protocol.
798 * If they Nak the reporting period, take their value XXX ?
800 NAKCILQR(CI_QUALITY, neg_lqr,
801 if (cishort != PPP_LQR)
804 try.lqr_period = cilong;
808 * Check for a looped-back line.
810 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
811 try.magicnumber = magic();
815 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
816 try.neg_pcompression = 0;
818 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
819 try.neg_accompression = 0;
823 * There may be remaining CIs, if the peer is requesting negotiation
824 * on an option that we didn't include in our request packet.
825 * If we see an option that we requested, or one we've already seen
826 * in this packet, then this packet is bad.
827 * If we wanted to respond by starting to negotiate on the requested
828 * option(s), we could, but we don't, because except for the
829 * authentication type and quality protocol, if we are not negotiating
830 * an option, it is because we were told not to.
831 * For the authentication type, the Nak from the peer means
832 * `let me authenticate myself with you' which is a bit pointless.
833 * For the quality protocol, the Nak means `ask me to send you quality
834 * reports', but if we didn't ask for them, we don't want them.
835 * An option we don't recognize represents the peer asking to
836 * negotiate some option we don't support, so ignore it.
838 while (len > CILEN_VOID) {
841 if ((len -= cilen) < 0)
843 next = p + cilen - 2;
847 if (go->neg_mru && go->mru != DEFMRU
848 || no.neg_mru || cilen != CILEN_SHORT)
850 GETSHORT(cishort, p);
851 if (cishort < DEFMRU)
855 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF
856 || no.neg_asyncmap || cilen != CILEN_LONG)
860 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
864 if (go->neg_magicnumber || no.neg_magicnumber ||
868 case CI_PCOMPRESSION:
869 if (go->neg_pcompression || no.neg_pcompression
870 || cilen != CILEN_VOID)
873 case CI_ACCOMPRESSION:
874 if (go->neg_accompression || no.neg_accompression
875 || cilen != CILEN_VOID)
879 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
886 /* If there is still anything left, this packet is bad. */
891 * OK, the Nak is good. Now we can update state.
893 if (f->state != OPENED) {
895 if (++try.numloops >= lcp_loopbackfail) {
896 syslog(LOG_NOTICE, "Serial line is looped back.");
897 lcp_close(f->unit, "Loopback detected");
907 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
913 * lcp_rejci - Peer has Rejected some of our CIs.
914 * This should not modify any state if the Reject is bad
915 * or if LCP is in the OPENED state.
918 * 0 - Reject was bad.
919 * 1 - Reject was good.
927 lcp_options *go = &lcp_gotoptions[f->unit];
933 lcp_options try; /* options to request next time */
938 * Any Rejected CIs must be in exactly the same order that we sent.
939 * Check packet length and CI length at each step.
940 * If we find any deviations, then this packet is bad.
942 #define REJCIVOID(opt, neg) \
944 len >= CILEN_VOID && \
945 p[1] == CILEN_VOID && \
948 INCPTR(CILEN_VOID, p); \
950 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
952 #define REJCISHORT(opt, neg, val) \
954 len >= CILEN_SHORT && \
955 p[1] == CILEN_SHORT && \
957 len -= CILEN_SHORT; \
959 GETSHORT(cishort, p); \
960 /* Check rejected value. */ \
961 if (cishort != val) \
964 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
966 #define REJCICHAP(opt, neg, val, digest) \
968 len >= CILEN_CHAP && \
969 p[1] == CILEN_CHAP && \
973 GETSHORT(cishort, p); \
974 GETCHAR(cichar, p); \
975 /* Check rejected value. */ \
976 if (cishort != val || cichar != digest) \
980 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
982 #define REJCILONG(opt, neg, val) \
984 len >= CILEN_LONG && \
985 p[1] == CILEN_LONG && \
989 GETLONG(cilong, p); \
990 /* Check rejected value. */ \
994 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
996 #define REJCILQR(opt, neg, val) \
998 len >= CILEN_LQR && \
999 p[1] == CILEN_LQR && \
1003 GETSHORT(cishort, p); \
1004 GETLONG(cilong, p); \
1005 /* Check rejected value. */ \
1006 if (cishort != PPP_LQR || cilong != val) \
1009 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1012 REJCISHORT(CI_MRU, neg_mru, go->mru);
1013 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1014 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1015 if (!go->neg_chap) {
1016 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1018 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1019 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1020 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1021 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1024 * If there are any remaining CIs, then this packet is bad.
1029 * Now we can update state.
1031 if (f->state != OPENED)
1036 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1037 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1038 plen, len, p - start));
1044 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1046 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1047 * appropriately. If reject_if_disagree is non-zero, doesn't return
1048 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1051 lcp_reqci(f, inp, lenp, reject_if_disagree)
1053 u_char *inp; /* Requested CIs */
1054 int *lenp; /* Length of requested CIs */
1055 int reject_if_disagree;
1057 lcp_options *go = &lcp_gotoptions[f->unit];
1058 lcp_options *ho = &lcp_hisoptions[f->unit];
1059 lcp_options *ao = &lcp_allowoptions[f->unit];
1060 u_char *cip, *next; /* Pointer to current and next CIs */
1061 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1062 u_short cishort; /* Parsed short value */
1063 u_int32_t cilong; /* Parse long value */
1064 int rc = CONFACK; /* Final packet return code */
1065 int orc; /* Individual option return code */
1066 u_char *p; /* Pointer to next char to parse */
1067 u_char *rejp; /* Pointer to next char in reject frame */
1068 u_char *nakp; /* Pointer to next char in Nak frame */
1069 int l = *lenp; /* Length left */
1072 * Reset all his options.
1074 BZERO(ho, sizeof(*ho));
1077 * Process all his options.
1083 orc = CONFACK; /* Assume success */
1084 cip = p = next; /* Remember begining of CI */
1085 if (l < 2 || /* Not enough data for CI header or */
1086 p[1] < 2 || /* CI length too small or */
1087 p[1] > l) { /* CI length too big? */
1088 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1089 orc = CONFREJ; /* Reject bad CI */
1090 cilen = l; /* Reject till end of packet */
1091 l = 0; /* Don't loop again */
1094 GETCHAR(citype, p); /* Parse CI type */
1095 GETCHAR(cilen, p); /* Parse CI length */
1096 l -= cilen; /* Adjust remaining length */
1097 next += cilen; /* Step to next CI */
1099 switch (citype) { /* Check CI type */
1101 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1102 if (!ao->neg_mru || /* Allow option? */
1103 cilen != CILEN_SHORT) { /* Check CI length */
1104 orc = CONFREJ; /* Reject CI */
1107 GETSHORT(cishort, p); /* Parse MRU */
1108 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1111 * He must be able to receive at least our minimum.
1112 * No need to check a maximum. If he sends a large number,
1113 * we'll just ignore it.
1115 if (cishort < MINMRU) {
1116 orc = CONFNAK; /* Nak CI */
1117 PUTCHAR(CI_MRU, nakp);
1118 PUTCHAR(CILEN_SHORT, nakp);
1119 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1122 ho->neg_mru = 1; /* Remember he sent MRU */
1123 ho->mru = cishort; /* And remember value */
1127 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1128 if (!ao->neg_asyncmap ||
1129 cilen != CILEN_LONG) {
1134 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1137 * Asyncmap must have set at least the bits
1138 * which are set in lcp_allowoptions[unit].asyncmap.
1140 if ((ao->asyncmap & ~cilong) != 0) {
1142 PUTCHAR(CI_ASYNCMAP, nakp);
1143 PUTCHAR(CILEN_LONG, nakp);
1144 PUTLONG(ao->asyncmap | cilong, nakp);
1147 ho->neg_asyncmap = 1;
1148 ho->asyncmap = cilong;
1152 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1153 if (cilen < CILEN_SHORT ||
1154 !(ao->neg_upap || ao->neg_chap)) {
1156 * Reject the option if we're not willing to authenticate.
1161 GETSHORT(cishort, p);
1162 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1165 * Authtype must be UPAP or CHAP.
1167 * Note: if both ao->neg_upap and ao->neg_chap are set,
1168 * and the peer sends a Configure-Request with two
1169 * authenticate-protocol requests, one for CHAP and one
1170 * for UPAP, then we will reject the second request.
1171 * Whether we end up doing CHAP or UPAP depends then on
1172 * the ordering of the CIs in the peer's Configure-Request.
1175 if (cishort == PPP_PAP) {
1176 if (ho->neg_chap || /* we've already accepted CHAP */
1177 cilen != CILEN_SHORT) {
1178 LCPDEBUG((LOG_WARNING,
1179 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1183 if (!ao->neg_upap) { /* we don't want to do PAP */
1184 orc = CONFNAK; /* NAK it and suggest CHAP */
1185 PUTCHAR(CI_AUTHTYPE, nakp);
1186 PUTCHAR(CILEN_CHAP, nakp);
1187 PUTSHORT(PPP_CHAP, nakp);
1188 PUTCHAR(ao->chap_mdtype, nakp);
1194 if (cishort == PPP_CHAP) {
1195 if (ho->neg_upap || /* we've already accepted PAP */
1196 cilen != CILEN_CHAP) {
1198 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1202 if (!ao->neg_chap) { /* we don't want to do CHAP */
1203 orc = CONFNAK; /* NAK it and suggest PAP */
1204 PUTCHAR(CI_AUTHTYPE, nakp);
1205 PUTCHAR(CILEN_SHORT, nakp);
1206 PUTSHORT(PPP_PAP, nakp);
1209 GETCHAR(cichar, p); /* get digest type*/
1210 if (cichar != ao->chap_mdtype) {
1212 PUTCHAR(CI_AUTHTYPE, nakp);
1213 PUTCHAR(CILEN_CHAP, nakp);
1214 PUTSHORT(PPP_CHAP, nakp);
1215 PUTCHAR(ao->chap_mdtype, nakp);
1218 ho->chap_mdtype = cichar; /* save md type */
1224 * We don't recognize the protocol they're asking for.
1225 * Nak it with something we're willing to do.
1226 * (At this point we know ao->neg_upap || ao->neg_chap.)
1229 PUTCHAR(CI_AUTHTYPE, nakp);
1231 PUTCHAR(CILEN_CHAP, nakp);
1232 PUTSHORT(PPP_CHAP, nakp);
1233 PUTCHAR(ao->chap_mdtype, nakp);
1235 PUTCHAR(CILEN_SHORT, nakp);
1236 PUTSHORT(PPP_PAP, nakp);
1241 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1243 cilen != CILEN_LQR) {
1248 GETSHORT(cishort, p);
1250 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1253 * Check the protocol and the reporting period.
1254 * XXX When should we Nak this, and what with?
1256 if (cishort != PPP_LQR) {
1258 PUTCHAR(CI_QUALITY, nakp);
1259 PUTCHAR(CILEN_LQR, nakp);
1260 PUTSHORT(PPP_LQR, nakp);
1261 PUTLONG(ao->lqr_period, nakp);
1266 case CI_MAGICNUMBER:
1267 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1268 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1269 cilen != CILEN_LONG) {
1274 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1277 * He must have a different magic number.
1279 if (go->neg_magicnumber &&
1280 cilong == go->magicnumber) {
1281 cilong = magic(); /* Don't put magic() inside macro! */
1283 PUTCHAR(CI_MAGICNUMBER, nakp);
1284 PUTCHAR(CILEN_LONG, nakp);
1285 PUTLONG(cilong, nakp);
1288 ho->neg_magicnumber = 1;
1289 ho->magicnumber = cilong;
1293 case CI_PCOMPRESSION:
1294 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1295 if (!ao->neg_pcompression ||
1296 cilen != CILEN_VOID) {
1300 ho->neg_pcompression = 1;
1303 case CI_ACCOMPRESSION:
1304 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1305 if (!ao->neg_accompression ||
1306 cilen != CILEN_VOID) {
1310 ho->neg_accompression = 1;
1314 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1321 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1322 if (orc == CONFACK && /* Good CI */
1323 rc != CONFACK) /* but prior CI wasnt? */
1324 continue; /* Don't send this one */
1326 if (orc == CONFNAK) { /* Nak this CI? */
1327 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1328 && citype != CI_MAGICNUMBER) {
1329 orc = CONFREJ; /* Get tough if so */
1331 if (rc == CONFREJ) /* Rejecting prior CI? */
1332 continue; /* Don't send this one */
1336 if (orc == CONFREJ) { /* Reject this CI */
1338 if (cip != rejp) /* Need to move rejected CI? */
1339 BCOPY(cip, rejp, cilen); /* Move it */
1340 INCPTR(cilen, rejp); /* Update output pointer */
1345 * If we wanted to send additional NAKs (for unsent CIs), the
1346 * code would go here. The extra NAKs would go at *nakp.
1347 * At present there are no cases where we want to ask the
1348 * peer to negotiate an option.
1357 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1359 *lenp = nakp - nak_buffer;
1360 BCOPY(nak_buffer, inp, *lenp);
1367 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1368 return (rc); /* Return final code */
1373 * lcp_up - LCP has come UP.
1379 lcp_options *wo = &lcp_wantoptions[f->unit];
1380 lcp_options *ho = &lcp_hisoptions[f->unit];
1381 lcp_options *go = &lcp_gotoptions[f->unit];
1382 lcp_options *ao = &lcp_allowoptions[f->unit];
1384 if (!go->neg_magicnumber)
1385 go->magicnumber = 0;
1386 if (!ho->neg_magicnumber)
1387 ho->magicnumber = 0;
1390 * Set our MTU to the smaller of the MTU we wanted and
1391 * the MRU our peer wanted. If we negotiated an MRU,
1392 * set our MRU to the larger of value we wanted and
1393 * the value we got in the negotiation.
1395 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1396 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1397 ho->neg_pcompression, ho->neg_accompression);
1399 * If the asyncmap hasn't been negotiated, we really should
1400 * set the receive asyncmap to ffffffff, but we set it to 0
1401 * for backwards contemptibility.
1403 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1404 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1405 go->neg_pcompression, go->neg_accompression);
1408 peer_mru[f->unit] = ho->mru;
1410 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1412 link_established(f->unit);
1417 * lcp_down - LCP has gone DOWN.
1419 * Alert other protocols.
1425 lcp_options *go = &lcp_gotoptions[f->unit];
1427 lcp_echo_lowerdown(f->unit);
1431 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1432 ppp_recv_config(f->unit, PPP_MRU,
1433 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1434 go->neg_pcompression, go->neg_accompression);
1435 peer_mru[f->unit] = PPP_MRU;
1440 * lcp_starting - LCP needs the lower layer up.
1446 link_required(f->unit);
1451 * lcp_finished - LCP has finished with the lower layer.
1457 link_terminated(f->unit);
1462 * lcp_printpkt - print the contents of an LCP packet.
1464 char *lcp_codenames[] = {
1465 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1466 "TermReq", "TermAck", "CodeRej", "ProtRej",
1467 "EchoReq", "EchoRep", "DiscReq"
1471 lcp_printpkt(p, plen, printer, arg)
1474 void (*printer) __P((void *, char *, ...));
1477 int code, id, len, olen;
1478 u_char *pstart, *optend;
1482 if (plen < HEADERLEN)
1488 if (len < HEADERLEN || len > plen)
1491 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1492 printer(arg, " %s", lcp_codenames[code-1]);
1494 printer(arg, " code=0x%x", code);
1495 printer(arg, " id=0x%x", id);
1502 /* print option list */
1507 if (olen < 2 || olen > len) {
1515 if (olen == CILEN_SHORT) {
1517 GETSHORT(cishort, p);
1518 printer(arg, "mru %d", cishort);
1522 if (olen == CILEN_LONG) {
1525 printer(arg, "asyncmap 0x%x", cilong);
1529 if (olen >= CILEN_SHORT) {
1531 printer(arg, "auth ");
1532 GETSHORT(cishort, p);
1535 printer(arg, "pap");
1538 printer(arg, "chap");
1541 printer(arg, "0x%x", cishort);
1546 if (olen >= CILEN_SHORT) {
1548 printer(arg, "quality ");
1549 GETSHORT(cishort, p);
1552 printer(arg, "lqr");
1555 printer(arg, "0x%x", cishort);
1559 case CI_MAGICNUMBER:
1560 if (olen == CILEN_LONG) {
1563 printer(arg, "magic 0x%x", cilong);
1566 case CI_PCOMPRESSION:
1567 if (olen == CILEN_VOID) {
1569 printer(arg, "pcomp");
1572 case CI_ACCOMPRESSION:
1573 if (olen == CILEN_VOID) {
1575 printer(arg, "accomp");
1579 while (p < optend) {
1581 printer(arg, " %.2x", code);
1588 /* print the rest of the bytes in the packet */
1589 for (; len > 0; --len) {
1591 printer(arg, " %.2x", code);
1598 * Time to shut down the link because there is nothing out there.
1602 void LcpLinkFailure (f)
1605 if (f->state == OPENED) {
1606 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1607 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1608 lcp_close(f->unit, "Peer not responding");
1609 phase = PHASE_TERMINATE;
1614 * Timer expired for the LCP echo requests from this process.
1621 LcpSendEchoRequest (f);
1624 * Start the timer for the next interval.
1626 assert (lcp_echo_timer_running==0);
1627 TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1628 lcp_echo_timer_running = 1;
1632 * LcpEchoTimeout - Timer expired on the LCP echo
1636 LcpEchoTimeout (arg)
1639 if (lcp_echo_timer_running != 0) {
1640 lcp_echo_timer_running = 0;
1641 LcpEchoCheck ((fsm *) arg);
1646 * LcpEchoReply - LCP has received a reply to the echo
1650 lcp_received_echo_reply (f, id, inp, len)
1652 int id; u_char *inp; int len;
1656 /* Check the magic number - don't count replies from ourselves. */
1658 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1661 GETLONG(magic, inp);
1662 if (lcp_gotoptions[f->unit].neg_magicnumber
1663 && magic == lcp_gotoptions[f->unit].magicnumber) {
1664 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1668 /* Reset the number of outstanding echo frames */
1669 lcp_echos_pending = 0;
1673 * LcpSendEchoRequest - Send an echo request frame to the peer
1677 LcpSendEchoRequest (f)
1680 u_int32_t lcp_magic;
1681 u_char pkt[4], *pktp;
1684 * Detect the failure of the peer at this point.
1686 if (lcp_echo_fails != 0) {
1687 if (lcp_echos_pending++ >= lcp_echo_fails) {
1689 lcp_echos_pending = 0;
1694 * Make and send the echo request frame.
1696 if (f->state == OPENED) {
1697 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1699 PUTLONG(lcp_magic, pktp);
1700 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1705 * lcp_echo_lowerup - Start the timer for the LCP frame
1709 lcp_echo_lowerup (unit)
1712 fsm *f = &lcp_fsm[unit];
1714 /* Clear the parameters for generating echo frames */
1715 lcp_echos_pending = 0;
1716 lcp_echo_number = 0;
1717 lcp_echo_timer_running = 0;
1719 /* If a timeout interval is specified then start the timer */
1720 if (lcp_echo_interval != 0)
1725 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1729 lcp_echo_lowerdown (unit)
1732 fsm *f = &lcp_fsm[unit];
1734 if (lcp_echo_timer_running != 0) {
1735 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1736 lcp_echo_timer_running = 0;