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.27 1996/07/01 01:15:19 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 */
105 * Protocol entry points.
106 * Some of these are called directly.
109 static void lcp_init __P((int));
110 static void lcp_input __P((int, u_char *, int));
111 static void lcp_protrej __P((int));
112 static int lcp_printpkt __P((u_char *, int,
113 void (*) __P((void *, char *, ...)), void *));
115 struct protent lcp_protent = {
133 int lcp_loopbackfail = DEFLOOPBACKFAIL;
136 * Length of each type of configuration option (in octets)
139 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
140 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
141 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
142 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
144 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
145 (x) == CONFNAK ? "NAK" : "REJ")
149 * lcp_init - Initialize LCP.
155 fsm *f = &lcp_fsm[unit];
156 lcp_options *wo = &lcp_wantoptions[unit];
157 lcp_options *ao = &lcp_allowoptions[unit];
160 f->protocol = PPP_LCP;
161 f->callbacks = &lcp_callbacks;
167 wo->restart = 0; /* Set to 1 in kernels or multi-line
171 wo->neg_asyncmap = 0;
173 wo->neg_chap = 0; /* Set to 1 on server */
174 wo->neg_upap = 0; /* Set to 1 on server */
175 wo->chap_mdtype = CHAP_DIGEST_MD5;
176 wo->neg_magicnumber = 1;
177 wo->neg_pcompression = 1;
178 wo->neg_accompression = 1;
179 wo->neg_lqr = 0; /* no LQR implementation yet */
183 ao->neg_asyncmap = 1;
186 ao->chap_mdtype = CHAP_DIGEST_MD5;
188 ao->neg_magicnumber = 1;
189 ao->neg_pcompression = 1;
190 ao->neg_accompression = 1;
191 ao->neg_lqr = 0; /* no LQR implementation yet */
193 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
194 xmit_accm[unit][3] = 0x60000000;
199 * lcp_open - LCP is allowed to come up.
205 fsm *f = &lcp_fsm[unit];
206 lcp_options *wo = &lcp_wantoptions[unit];
210 f->flags |= OPT_PASSIVE;
212 f->flags |= OPT_SILENT;
218 * lcp_close - Take LCP down.
221 lcp_close(unit, reason)
225 fsm *f = &lcp_fsm[unit];
227 phase = PHASE_TERMINATE;
228 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
230 * This action is not strictly according to the FSM in RFC1548,
231 * but it does mean that the program terminates if you do a
232 * lcp_close() in passive/silent mode when a connection hasn't
239 fsm_close(&lcp_fsm[unit], reason);
244 * lcp_lowerup - The lower layer is up.
250 lcp_options *wo = &lcp_wantoptions[unit];
253 * Don't use A/C or protocol compression on transmission,
254 * but accept A/C and protocol compressed packets
255 * if we are going to ask for A/C and protocol compression.
257 ppp_set_xaccm(unit, xmit_accm[unit]);
258 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
259 ppp_recv_config(unit, PPP_MRU, 0x00000000,
260 wo->neg_pcompression, wo->neg_accompression);
261 peer_mru[unit] = PPP_MRU;
262 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
264 fsm_lowerup(&lcp_fsm[unit]);
269 * lcp_lowerdown - The lower layer is down.
275 fsm_lowerdown(&lcp_fsm[unit]);
280 * lcp_input - Input LCP packet.
283 lcp_input(unit, p, len)
288 fsm *f = &lcp_fsm[unit];
290 fsm_input(f, p, len);
295 * lcp_extcode - Handle a LCP-specific code.
298 lcp_extcode(f, code, id, inp, len)
308 lcp_rprotrej(f, inp, len);
312 if (f->state != OPENED)
314 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
316 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
317 fsm_sdata(f, ECHOREP, id, inp, len);
321 lcp_received_echo_reply(f, id, inp, len);
335 * lcp_rprotrej - Receive an Protocol-Reject.
337 * Figure out which protocol is rejected and inform it.
340 lcp_rprotrej(f, inp, len)
346 struct protent *protp;
349 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
351 if (len < sizeof (u_short)) {
353 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
360 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
364 * Protocol-Reject packets received in any state other than the LCP
365 * OPENED state SHOULD be silently discarded.
367 if( f->state != OPENED ){
368 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
374 * Upcall the proper Protocol-Reject routine.
376 for (i = 0; (protp = protocols[i]) != NULL; ++i)
377 if (protp->protocol == prot && protp->enabled_flag) {
378 (*protp->protrej)(f->unit);
382 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
388 * lcp_protrej - A Protocol-Reject was received.
398 LCPDEBUG((LOG_WARNING,
399 "lcp_protrej: Received Protocol-Reject for LCP!"));
400 fsm_protreject(&lcp_fsm[unit]);
405 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
408 lcp_sprotrej(unit, p, len)
414 * Send back the protocol and the information field of the
415 * rejected packet. We only get here if LCP is in the OPENED state.
420 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
426 * lcp_resetci - Reset our CI.
432 lcp_wantoptions[f->unit].magicnumber = magic();
433 lcp_wantoptions[f->unit].numloops = 0;
434 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
435 peer_mru[f->unit] = PPP_MRU;
441 * lcp_cilen - Return length of our CI.
447 lcp_options *go = &lcp_gotoptions[f->unit];
449 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
450 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
451 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
452 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
453 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
455 * NB: we only ask for one of CHAP and UPAP, even if we will
458 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
459 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
460 LENCICHAP(go->neg_chap) +
461 LENCISHORT(!go->neg_chap && go->neg_upap) +
462 LENCILQR(go->neg_lqr) +
463 LENCILONG(go->neg_magicnumber) +
464 LENCIVOID(go->neg_pcompression) +
465 LENCIVOID(go->neg_accompression));
470 * lcp_addci - Add our desired CIs to a packet.
473 lcp_addci(f, ucp, lenp)
478 lcp_options *go = &lcp_gotoptions[f->unit];
479 u_char *start_ucp = ucp;
481 #define ADDCIVOID(opt, neg) \
484 PUTCHAR(CILEN_VOID, ucp); \
486 #define ADDCISHORT(opt, neg, val) \
489 PUTCHAR(CILEN_SHORT, ucp); \
490 PUTSHORT(val, ucp); \
492 #define ADDCICHAP(opt, neg, val, digest) \
495 PUTCHAR(CILEN_CHAP, ucp); \
496 PUTSHORT(val, ucp); \
497 PUTCHAR(digest, ucp); \
499 #define ADDCILONG(opt, neg, val) \
502 PUTCHAR(CILEN_LONG, ucp); \
505 #define ADDCILQR(opt, neg, val) \
508 PUTCHAR(CILEN_LQR, ucp); \
509 PUTSHORT(PPP_LQR, ucp); \
513 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
514 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
516 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
517 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
518 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
519 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
520 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
521 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
523 if (ucp - start_ucp != *lenp) {
524 /* this should never happen, because peer_mtu should be 1500 */
525 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
531 * lcp_ackci - Ack our CIs.
532 * This should not modify any state if the Ack is bad.
544 lcp_options *go = &lcp_gotoptions[f->unit];
545 u_char cilen, citype, cichar;
550 * CIs must be in exactly the same order that we sent.
551 * Check packet length and CI length at each step.
552 * If we find any deviations, then this packet is bad.
554 #define ACKCIVOID(opt, neg) \
556 if ((len -= CILEN_VOID) < 0) \
558 GETCHAR(citype, p); \
560 if (cilen != CILEN_VOID || \
564 #define ACKCISHORT(opt, neg, val) \
566 if ((len -= CILEN_SHORT) < 0) \
568 GETCHAR(citype, p); \
570 if (cilen != CILEN_SHORT || \
573 GETSHORT(cishort, p); \
574 if (cishort != val) \
577 #define ACKCICHAP(opt, neg, val, digest) \
579 if ((len -= CILEN_CHAP) < 0) \
581 GETCHAR(citype, p); \
583 if (cilen != CILEN_CHAP || \
586 GETSHORT(cishort, p); \
587 if (cishort != val) \
589 GETCHAR(cichar, p); \
590 if (cichar != digest) \
593 #define ACKCILONG(opt, neg, val) \
595 if ((len -= CILEN_LONG) < 0) \
597 GETCHAR(citype, p); \
599 if (cilen != CILEN_LONG || \
602 GETLONG(cilong, p); \
606 #define ACKCILQR(opt, neg, val) \
608 if ((len -= CILEN_LQR) < 0) \
610 GETCHAR(citype, p); \
612 if (cilen != CILEN_LQR || \
615 GETSHORT(cishort, p); \
616 if (cishort != PPP_LQR) \
618 GETLONG(cilong, p); \
623 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
624 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
626 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
627 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
628 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
629 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
630 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
631 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
634 * If there are any remaining CIs, then this packet is bad.
640 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
646 * lcp_nakci - Peer has sent a NAK for some of our CIs.
647 * This should not modify any state if the Nak is bad
648 * or if LCP is in the OPENED state.
660 lcp_options *go = &lcp_gotoptions[f->unit];
661 lcp_options *wo = &lcp_wantoptions[f->unit];
662 u_char citype, cichar, *next;
665 lcp_options no; /* options we've seen Naks for */
666 lcp_options try; /* options to request next time */
670 BZERO(&no, sizeof(no));
674 * Any Nak'd CIs must be in exactly the same order that we sent.
675 * Check packet length and CI length at each step.
676 * If we find any deviations, then this packet is bad.
678 #define NAKCIVOID(opt, neg, code) \
680 len >= CILEN_VOID && \
681 p[1] == CILEN_VOID && \
684 INCPTR(CILEN_VOID, p); \
688 #define NAKCICHAP(opt, neg, code) \
690 len >= CILEN_CHAP && \
691 p[1] == CILEN_CHAP && \
695 GETSHORT(cishort, p); \
696 GETCHAR(cichar, p); \
700 #define NAKCISHORT(opt, neg, code) \
702 len >= CILEN_SHORT && \
703 p[1] == CILEN_SHORT && \
705 len -= CILEN_SHORT; \
707 GETSHORT(cishort, p); \
711 #define NAKCILONG(opt, neg, code) \
713 len >= CILEN_LONG && \
714 p[1] == CILEN_LONG && \
718 GETLONG(cilong, p); \
722 #define NAKCILQR(opt, neg, code) \
724 len >= CILEN_LQR && \
725 p[1] == CILEN_LQR && \
729 GETSHORT(cishort, p); \
730 GETLONG(cilong, p); \
736 * We don't care if they want to send us smaller packets than
737 * we want. Therefore, accept any MRU less than what we asked for,
738 * but then ignore the new value when setting the MRU in the kernel.
739 * If they send us a bigger MRU than what we asked, accept it, up to
740 * the limit of the default MRU we'd get if we didn't negotiate.
742 if (go->neg_mru && go->mru != DEFMRU) {
743 NAKCISHORT(CI_MRU, neg_mru,
744 if (cishort <= wo->mru || cishort < DEFMRU)
750 * Add any characters they want to our (receive-side) asyncmap.
752 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
753 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
754 try.asyncmap = go->asyncmap | cilong;
759 * If they've nak'd our authentication-protocol, check whether
760 * they are proposing a different protocol, or a different
761 * hash algorithm for CHAP.
763 if ((go->neg_chap || go->neg_upap)
764 && len >= CILEN_SHORT
765 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
768 no.neg_chap = go->neg_chap;
769 no.neg_upap = go->neg_upap;
771 GETSHORT(cishort, p);
772 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
774 * If we were asking for CHAP, they obviously don't want to do it.
775 * If we weren't asking for CHAP, then we were asking for PAP,
776 * in which case this Nak is bad.
782 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
786 * We were asking for CHAP/MD5; they must want a different
787 * algorithm. If they can't do MD5, we'll have to stop
790 if (cichar != go->chap_mdtype)
794 * Stop asking for PAP if we were asking for it.
801 * We don't recognize what they're suggesting.
802 * Stop asking for what we were asking for.
808 p += cilen - CILEN_SHORT;
813 * If they can't cope with our link quality protocol, we'll have
814 * to stop asking for LQR. We haven't got any other protocol.
815 * If they Nak the reporting period, take their value XXX ?
817 NAKCILQR(CI_QUALITY, neg_lqr,
818 if (cishort != PPP_LQR)
821 try.lqr_period = cilong;
825 * Check for a looped-back line.
827 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
828 try.magicnumber = magic();
833 * Peer shouldn't send Nak for protocol compression or
834 * address/control compression requests; they should send
835 * a Reject instead. If they send a Nak, treat it as a Reject.
837 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
838 try.neg_pcompression = 0;
840 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
841 try.neg_accompression = 0;
845 * There may be remaining CIs, if the peer is requesting negotiation
846 * on an option that we didn't include in our request packet.
847 * If we see an option that we requested, or one we've already seen
848 * in this packet, then this packet is bad.
849 * If we wanted to respond by starting to negotiate on the requested
850 * option(s), we could, but we don't, because except for the
851 * authentication type and quality protocol, if we are not negotiating
852 * an option, it is because we were told not to.
853 * For the authentication type, the Nak from the peer means
854 * `let me authenticate myself with you' which is a bit pointless.
855 * For the quality protocol, the Nak means `ask me to send you quality
856 * reports', but if we didn't ask for them, we don't want them.
857 * An option we don't recognize represents the peer asking to
858 * negotiate some option we don't support, so ignore it.
860 while (len > CILEN_VOID) {
863 if (cilen < CILEN_VOID || (len -= cilen) < 0)
865 next = p + cilen - 2;
869 if (go->neg_mru && go->mru != DEFMRU
870 || no.neg_mru || cilen != CILEN_SHORT)
872 GETSHORT(cishort, p);
873 if (cishort < DEFMRU)
877 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF
878 || no.neg_asyncmap || cilen != CILEN_LONG)
882 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
886 if (go->neg_magicnumber || no.neg_magicnumber ||
890 case CI_PCOMPRESSION:
891 if (go->neg_pcompression || no.neg_pcompression
892 || cilen != CILEN_VOID)
895 case CI_ACCOMPRESSION:
896 if (go->neg_accompression || no.neg_accompression
897 || cilen != CILEN_VOID)
901 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
908 /* If there is still anything left, this packet is bad. */
913 * OK, the Nak is good. Now we can update state.
915 if (f->state != OPENED) {
917 if (++try.numloops >= lcp_loopbackfail) {
918 syslog(LOG_NOTICE, "Serial line is looped back.");
919 lcp_close(f->unit, "Loopback detected");
929 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
935 * lcp_rejci - Peer has Rejected some of our CIs.
936 * This should not modify any state if the Reject is bad
937 * or if LCP is in the OPENED state.
940 * 0 - Reject was bad.
941 * 1 - Reject was good.
949 lcp_options *go = &lcp_gotoptions[f->unit];
953 lcp_options try; /* options to request next time */
958 * Any Rejected CIs must be in exactly the same order that we sent.
959 * Check packet length and CI length at each step.
960 * If we find any deviations, then this packet is bad.
962 #define REJCIVOID(opt, neg) \
964 len >= CILEN_VOID && \
965 p[1] == CILEN_VOID && \
968 INCPTR(CILEN_VOID, p); \
970 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
972 #define REJCISHORT(opt, neg, val) \
974 len >= CILEN_SHORT && \
975 p[1] == CILEN_SHORT && \
977 len -= CILEN_SHORT; \
979 GETSHORT(cishort, p); \
980 /* Check rejected value. */ \
981 if (cishort != val) \
984 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
986 #define REJCICHAP(opt, neg, val, digest) \
988 len >= CILEN_CHAP && \
989 p[1] == CILEN_CHAP && \
993 GETSHORT(cishort, p); \
994 GETCHAR(cichar, p); \
995 /* Check rejected value. */ \
996 if (cishort != val || cichar != digest) \
1000 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1002 #define REJCILONG(opt, neg, val) \
1004 len >= CILEN_LONG && \
1005 p[1] == CILEN_LONG && \
1007 len -= CILEN_LONG; \
1009 GETLONG(cilong, p); \
1010 /* Check rejected value. */ \
1011 if (cilong != val) \
1014 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1016 #define REJCILQR(opt, neg, val) \
1018 len >= CILEN_LQR && \
1019 p[1] == CILEN_LQR && \
1023 GETSHORT(cishort, p); \
1024 GETLONG(cilong, p); \
1025 /* Check rejected value. */ \
1026 if (cishort != PPP_LQR || cilong != val) \
1029 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1032 REJCISHORT(CI_MRU, neg_mru, go->mru);
1033 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1034 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1035 if (!go->neg_chap) {
1036 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1038 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1039 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1040 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1041 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1044 * If there are any remaining CIs, then this packet is bad.
1049 * Now we can update state.
1051 if (f->state != OPENED)
1056 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1062 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1064 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1065 * appropriately. If reject_if_disagree is non-zero, doesn't return
1066 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1069 lcp_reqci(f, inp, lenp, reject_if_disagree)
1071 u_char *inp; /* Requested CIs */
1072 int *lenp; /* Length of requested CIs */
1073 int reject_if_disagree;
1075 lcp_options *go = &lcp_gotoptions[f->unit];
1076 lcp_options *ho = &lcp_hisoptions[f->unit];
1077 lcp_options *ao = &lcp_allowoptions[f->unit];
1078 u_char *cip, *next; /* Pointer to current and next CIs */
1079 int cilen, citype, cichar; /* Parsed len, type, char value */
1080 u_short cishort; /* Parsed short value */
1081 u_int32_t cilong; /* Parse long value */
1082 int rc = CONFACK; /* Final packet return code */
1083 int orc; /* Individual option return code */
1084 u_char *p; /* Pointer to next char to parse */
1085 u_char *rejp; /* Pointer to next char in reject frame */
1086 u_char *nakp; /* Pointer to next char in Nak frame */
1087 int l = *lenp; /* Length left */
1090 * Reset all his options.
1092 BZERO(ho, sizeof(*ho));
1095 * Process all his options.
1101 orc = CONFACK; /* Assume success */
1102 cip = p = next; /* Remember begining of CI */
1103 if (l < 2 || /* Not enough data for CI header or */
1104 p[1] < 2 || /* CI length too small or */
1105 p[1] > l) { /* CI length too big? */
1106 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1107 orc = CONFREJ; /* Reject bad CI */
1108 cilen = l; /* Reject till end of packet */
1109 l = 0; /* Don't loop again */
1113 GETCHAR(citype, p); /* Parse CI type */
1114 GETCHAR(cilen, p); /* Parse CI length */
1115 l -= cilen; /* Adjust remaining length */
1116 next += cilen; /* Step to next CI */
1118 switch (citype) { /* Check CI type */
1120 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1121 if (!ao->neg_mru || /* Allow option? */
1122 cilen != CILEN_SHORT) { /* Check CI length */
1123 orc = CONFREJ; /* Reject CI */
1126 GETSHORT(cishort, p); /* Parse MRU */
1127 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1130 * He must be able to receive at least our minimum.
1131 * No need to check a maximum. If he sends a large number,
1132 * we'll just ignore it.
1134 if (cishort < MINMRU) {
1135 orc = CONFNAK; /* Nak CI */
1136 PUTCHAR(CI_MRU, nakp);
1137 PUTCHAR(CILEN_SHORT, nakp);
1138 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1141 ho->neg_mru = 1; /* Remember he sent MRU */
1142 ho->mru = cishort; /* And remember value */
1146 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1147 if (!ao->neg_asyncmap ||
1148 cilen != CILEN_LONG) {
1153 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1156 * Asyncmap must have set at least the bits
1157 * which are set in lcp_allowoptions[unit].asyncmap.
1159 if ((ao->asyncmap & ~cilong) != 0) {
1161 PUTCHAR(CI_ASYNCMAP, nakp);
1162 PUTCHAR(CILEN_LONG, nakp);
1163 PUTLONG(ao->asyncmap | cilong, nakp);
1166 ho->neg_asyncmap = 1;
1167 ho->asyncmap = cilong;
1171 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1172 if (cilen < CILEN_SHORT ||
1173 !(ao->neg_upap || ao->neg_chap)) {
1175 * Reject the option if we're not willing to authenticate.
1180 GETSHORT(cishort, p);
1181 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1184 * Authtype must be UPAP or CHAP.
1186 * Note: if both ao->neg_upap and ao->neg_chap are set,
1187 * and the peer sends a Configure-Request with two
1188 * authenticate-protocol requests, one for CHAP and one
1189 * for UPAP, then we will reject the second request.
1190 * Whether we end up doing CHAP or UPAP depends then on
1191 * the ordering of the CIs in the peer's Configure-Request.
1194 if (cishort == PPP_PAP) {
1195 if (ho->neg_chap || /* we've already accepted CHAP */
1196 cilen != CILEN_SHORT) {
1197 LCPDEBUG((LOG_WARNING,
1198 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1202 if (!ao->neg_upap) { /* we don't want to do PAP */
1203 orc = CONFNAK; /* NAK it and suggest CHAP */
1204 PUTCHAR(CI_AUTHTYPE, nakp);
1205 PUTCHAR(CILEN_CHAP, nakp);
1206 PUTSHORT(PPP_CHAP, nakp);
1207 PUTCHAR(ao->chap_mdtype, nakp);
1213 if (cishort == PPP_CHAP) {
1214 if (ho->neg_upap || /* we've already accepted PAP */
1215 cilen != CILEN_CHAP) {
1217 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1221 if (!ao->neg_chap) { /* we don't want to do CHAP */
1222 orc = CONFNAK; /* NAK it and suggest PAP */
1223 PUTCHAR(CI_AUTHTYPE, nakp);
1224 PUTCHAR(CILEN_SHORT, nakp);
1225 PUTSHORT(PPP_PAP, nakp);
1228 GETCHAR(cichar, p); /* get digest type*/
1229 if (cichar != CHAP_DIGEST_MD5
1231 && cichar != CHAP_MICROSOFT
1235 PUTCHAR(CI_AUTHTYPE, nakp);
1236 PUTCHAR(CILEN_CHAP, nakp);
1237 PUTSHORT(PPP_CHAP, nakp);
1238 PUTCHAR(ao->chap_mdtype, nakp);
1241 ho->chap_mdtype = cichar; /* save md type */
1247 * We don't recognize the protocol they're asking for.
1248 * Nak it with something we're willing to do.
1249 * (At this point we know ao->neg_upap || ao->neg_chap.)
1252 PUTCHAR(CI_AUTHTYPE, nakp);
1254 PUTCHAR(CILEN_CHAP, nakp);
1255 PUTSHORT(PPP_CHAP, nakp);
1256 PUTCHAR(ao->chap_mdtype, nakp);
1258 PUTCHAR(CILEN_SHORT, nakp);
1259 PUTSHORT(PPP_PAP, nakp);
1264 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1266 cilen != CILEN_LQR) {
1271 GETSHORT(cishort, p);
1273 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1276 * Check the protocol and the reporting period.
1277 * XXX When should we Nak this, and what with?
1279 if (cishort != PPP_LQR) {
1281 PUTCHAR(CI_QUALITY, nakp);
1282 PUTCHAR(CILEN_LQR, nakp);
1283 PUTSHORT(PPP_LQR, nakp);
1284 PUTLONG(ao->lqr_period, nakp);
1289 case CI_MAGICNUMBER:
1290 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1291 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1292 cilen != CILEN_LONG) {
1297 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1300 * He must have a different magic number.
1302 if (go->neg_magicnumber &&
1303 cilong == go->magicnumber) {
1304 cilong = magic(); /* Don't put magic() inside macro! */
1306 PUTCHAR(CI_MAGICNUMBER, nakp);
1307 PUTCHAR(CILEN_LONG, nakp);
1308 PUTLONG(cilong, nakp);
1311 ho->neg_magicnumber = 1;
1312 ho->magicnumber = cilong;
1316 case CI_PCOMPRESSION:
1317 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1318 if (!ao->neg_pcompression ||
1319 cilen != CILEN_VOID) {
1323 ho->neg_pcompression = 1;
1326 case CI_ACCOMPRESSION:
1327 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1328 if (!ao->neg_accompression ||
1329 cilen != CILEN_VOID) {
1333 ho->neg_accompression = 1;
1337 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1344 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1345 if (orc == CONFACK && /* Good CI */
1346 rc != CONFACK) /* but prior CI wasnt? */
1347 continue; /* Don't send this one */
1349 if (orc == CONFNAK) { /* Nak this CI? */
1350 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1351 && citype != CI_MAGICNUMBER) {
1352 orc = CONFREJ; /* Get tough if so */
1354 if (rc == CONFREJ) /* Rejecting prior CI? */
1355 continue; /* Don't send this one */
1359 if (orc == CONFREJ) { /* Reject this CI */
1361 if (cip != rejp) /* Need to move rejected CI? */
1362 BCOPY(cip, rejp, cilen); /* Move it */
1363 INCPTR(cilen, rejp); /* Update output pointer */
1368 * If we wanted to send additional NAKs (for unsent CIs), the
1369 * code would go here. The extra NAKs would go at *nakp.
1370 * At present there are no cases where we want to ask the
1371 * peer to negotiate an option.
1380 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1382 *lenp = nakp - nak_buffer;
1383 BCOPY(nak_buffer, inp, *lenp);
1390 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1391 return (rc); /* Return final code */
1396 * lcp_up - LCP has come UP.
1402 lcp_options *wo = &lcp_wantoptions[f->unit];
1403 lcp_options *ho = &lcp_hisoptions[f->unit];
1404 lcp_options *go = &lcp_gotoptions[f->unit];
1405 lcp_options *ao = &lcp_allowoptions[f->unit];
1407 if (!go->neg_magicnumber)
1408 go->magicnumber = 0;
1409 if (!ho->neg_magicnumber)
1410 ho->magicnumber = 0;
1413 * Set our MTU to the smaller of the MTU we wanted and
1414 * the MRU our peer wanted. If we negotiated an MRU,
1415 * set our MRU to the larger of value we wanted and
1416 * the value we got in the negotiation.
1418 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1419 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1420 ho->neg_pcompression, ho->neg_accompression);
1422 * If the asyncmap hasn't been negotiated, we really should
1423 * set the receive asyncmap to ffffffff, but we set it to 0
1424 * for backwards contemptibility.
1426 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1427 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1428 go->neg_pcompression, go->neg_accompression);
1431 peer_mru[f->unit] = ho->mru;
1433 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1435 link_established(f->unit);
1440 * lcp_down - LCP has gone DOWN.
1442 * Alert other protocols.
1448 lcp_options *go = &lcp_gotoptions[f->unit];
1450 lcp_echo_lowerdown(f->unit);
1454 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1455 ppp_recv_config(f->unit, PPP_MRU,
1456 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1457 go->neg_pcompression, go->neg_accompression);
1458 peer_mru[f->unit] = PPP_MRU;
1463 * lcp_starting - LCP needs the lower layer up.
1469 link_required(f->unit);
1474 * lcp_finished - LCP has finished with the lower layer.
1480 link_terminated(f->unit);
1485 * lcp_printpkt - print the contents of an LCP packet.
1487 static char *lcp_codenames[] = {
1488 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1489 "TermReq", "TermAck", "CodeRej", "ProtRej",
1490 "EchoReq", "EchoRep", "DiscReq"
1494 lcp_printpkt(p, plen, printer, arg)
1497 void (*printer) __P((void *, char *, ...));
1500 int code, id, len, olen;
1501 u_char *pstart, *optend;
1506 if (plen < HEADERLEN)
1512 if (len < HEADERLEN || len > plen)
1515 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1516 printer(arg, " %s", lcp_codenames[code-1]);
1518 printer(arg, " code=0x%x", code);
1519 printer(arg, " id=0x%x", id);
1526 /* print option list */
1531 if (olen < 2 || olen > len) {
1539 if (olen == CILEN_SHORT) {
1541 GETSHORT(cishort, p);
1542 printer(arg, "mru %d", cishort);
1546 if (olen == CILEN_LONG) {
1549 printer(arg, "asyncmap 0x%x", cilong);
1553 if (olen >= CILEN_SHORT) {
1555 printer(arg, "auth ");
1556 GETSHORT(cishort, p);
1559 printer(arg, "pap");
1562 printer(arg, "chap");
1565 printer(arg, "0x%x", cishort);
1570 if (olen >= CILEN_SHORT) {
1572 printer(arg, "quality ");
1573 GETSHORT(cishort, p);
1576 printer(arg, "lqr");
1579 printer(arg, "0x%x", cishort);
1583 case CI_MAGICNUMBER:
1584 if (olen == CILEN_LONG) {
1587 printer(arg, "magic 0x%x", cilong);
1590 case CI_PCOMPRESSION:
1591 if (olen == CILEN_VOID) {
1593 printer(arg, "pcomp");
1596 case CI_ACCOMPRESSION:
1597 if (olen == CILEN_VOID) {
1599 printer(arg, "accomp");
1603 while (p < optend) {
1605 printer(arg, " %.2x", code);
1613 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1615 print_string(p, len, printer, arg);
1626 printer(arg, " magic=0x%x", cilong);
1633 /* print the rest of the bytes in the packet */
1634 for (; len > 0; --len) {
1636 printer(arg, " %.2x", code);
1643 * Time to shut down the link because there is nothing out there.
1647 void LcpLinkFailure (f)
1650 if (f->state == OPENED) {
1651 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1652 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1653 lcp_close(f->unit, "Peer not responding");
1658 * Timer expired for the LCP echo requests from this process.
1665 LcpSendEchoRequest (f);
1668 * Start the timer for the next interval.
1670 assert (lcp_echo_timer_running==0);
1671 TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1672 lcp_echo_timer_running = 1;
1676 * LcpEchoTimeout - Timer expired on the LCP echo
1680 LcpEchoTimeout (arg)
1683 if (lcp_echo_timer_running != 0) {
1684 lcp_echo_timer_running = 0;
1685 LcpEchoCheck ((fsm *) arg);
1690 * LcpEchoReply - LCP has received a reply to the echo
1694 lcp_received_echo_reply (f, id, inp, len)
1696 int id; u_char *inp; int len;
1700 /* Check the magic number - don't count replies from ourselves. */
1702 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1705 GETLONG(magic, inp);
1706 if (lcp_gotoptions[f->unit].neg_magicnumber
1707 && magic == lcp_gotoptions[f->unit].magicnumber) {
1708 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1712 /* Reset the number of outstanding echo frames */
1713 lcp_echos_pending = 0;
1717 * LcpSendEchoRequest - Send an echo request frame to the peer
1721 LcpSendEchoRequest (f)
1724 u_int32_t lcp_magic;
1725 u_char pkt[4], *pktp;
1728 * Detect the failure of the peer at this point.
1730 if (lcp_echo_fails != 0) {
1731 if (lcp_echos_pending++ >= lcp_echo_fails) {
1733 lcp_echos_pending = 0;
1738 * Make and send the echo request frame.
1740 if (f->state == OPENED) {
1741 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1743 PUTLONG(lcp_magic, pktp);
1744 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1749 * lcp_echo_lowerup - Start the timer for the LCP frame
1753 lcp_echo_lowerup (unit)
1756 fsm *f = &lcp_fsm[unit];
1758 /* Clear the parameters for generating echo frames */
1759 lcp_echos_pending = 0;
1760 lcp_echo_number = 0;
1761 lcp_echo_timer_running = 0;
1763 /* If a timeout interval is specified then start the timer */
1764 if (lcp_echo_interval != 0)
1769 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1773 lcp_echo_lowerdown (unit)
1776 fsm *f = &lcp_fsm[unit];
1778 if (lcp_echo_timer_running != 0) {
1779 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1780 lcp_echo_timer_running = 0;