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.30 1997/04/30 05:52:59 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((void *));
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 *));
85 static void LcpEchoCheck __P((fsm *));
87 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
88 lcp_resetci, /* Reset our Configuration Information */
89 lcp_cilen, /* Length of our Configuration Information */
90 lcp_addci, /* Add our Configuration Information */
91 lcp_ackci, /* ACK our Configuration Information */
92 lcp_nakci, /* NAK our Configuration Information */
93 lcp_rejci, /* Reject our Configuration Information */
94 lcp_reqci, /* Request peer's Configuration Information */
95 lcp_up, /* Called when fsm reaches OPENED state */
96 lcp_down, /* Called when fsm leaves OPENED state */
97 lcp_starting, /* Called when we want the lower layer up */
98 lcp_finished, /* Called when we want the lower layer down */
99 NULL, /* Called when Protocol-Reject received */
100 NULL, /* Retransmission is necessary */
101 lcp_extcode, /* Called to handle LCP-specific codes */
102 "LCP" /* String name of protocol */
106 * Protocol entry points.
107 * Some of these are called directly.
110 static void lcp_init __P((int));
111 static void lcp_input __P((int, u_char *, int));
112 static void lcp_protrej __P((int));
113 static int lcp_printpkt __P((u_char *, int,
114 void (*) __P((void *, char *, ...)), void *));
116 struct protent lcp_protent = {
134 int lcp_loopbackfail = DEFLOOPBACKFAIL;
137 * Length of each type of configuration option (in octets)
141 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
142 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
143 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
144 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
147 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
148 (x) == CONFNAK ? "NAK" : "REJ")
152 * lcp_init - Initialize LCP.
158 fsm *f = &lcp_fsm[unit];
159 lcp_options *wo = &lcp_wantoptions[unit];
160 lcp_options *ao = &lcp_allowoptions[unit];
163 f->protocol = PPP_LCP;
164 f->callbacks = &lcp_callbacks;
170 wo->restart = 0; /* Set to 1 in kernels or multi-line
174 wo->neg_asyncmap = 0;
176 wo->neg_chap = 0; /* Set to 1 on server */
177 wo->neg_upap = 0; /* Set to 1 on server */
178 wo->chap_mdtype = CHAP_DIGEST_MD5;
179 wo->neg_magicnumber = 1;
180 wo->neg_pcompression = 1;
181 wo->neg_accompression = 1;
182 wo->neg_lqr = 0; /* no LQR implementation yet */
187 ao->neg_asyncmap = 1;
190 ao->chap_mdtype = CHAP_DIGEST_MD5;
192 ao->neg_magicnumber = 1;
193 ao->neg_pcompression = 1;
194 ao->neg_accompression = 1;
195 ao->neg_lqr = 0; /* no LQR implementation yet */
202 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
203 xmit_accm[unit][3] = 0x60000000;
208 * lcp_open - LCP is allowed to come up.
214 fsm *f = &lcp_fsm[unit];
215 lcp_options *wo = &lcp_wantoptions[unit];
219 f->flags |= OPT_PASSIVE;
221 f->flags |= OPT_SILENT;
227 * lcp_close - Take LCP down.
230 lcp_close(unit, reason)
234 fsm *f = &lcp_fsm[unit];
236 if (phase != PHASE_DEAD)
237 phase = PHASE_TERMINATE;
238 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
240 * This action is not strictly according to the FSM in RFC1548,
241 * but it does mean that the program terminates if you do a
242 * lcp_close() in passive/silent mode when a connection hasn't
249 fsm_close(&lcp_fsm[unit], reason);
254 * lcp_lowerup - The lower layer is up.
260 lcp_options *wo = &lcp_wantoptions[unit];
263 * Don't use A/C or protocol compression on transmission,
264 * but accept A/C and protocol compressed packets
265 * if we are going to ask for A/C and protocol compression.
267 ppp_set_xaccm(unit, xmit_accm[unit]);
268 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
269 ppp_recv_config(unit, PPP_MRU, 0x00000000,
270 wo->neg_pcompression, wo->neg_accompression);
271 peer_mru[unit] = PPP_MRU;
272 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
274 fsm_lowerup(&lcp_fsm[unit]);
279 * lcp_lowerdown - The lower layer is down.
285 fsm_lowerdown(&lcp_fsm[unit]);
290 * lcp_input - Input LCP packet.
293 lcp_input(unit, p, len)
298 fsm *f = &lcp_fsm[unit];
300 fsm_input(f, p, len);
305 * lcp_extcode - Handle a LCP-specific code.
308 lcp_extcode(f, code, id, inp, len)
318 lcp_rprotrej(f, inp, len);
322 if (f->state != OPENED)
324 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
326 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
327 fsm_sdata(f, ECHOREP, id, inp, len);
331 lcp_received_echo_reply(f, id, inp, len);
345 * lcp_rprotrej - Receive an Protocol-Reject.
347 * Figure out which protocol is rejected and inform it.
350 lcp_rprotrej(f, inp, len)
356 struct protent *protp;
359 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
361 if (len < sizeof (u_short)) {
363 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
370 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
374 * Protocol-Reject packets received in any state other than the LCP
375 * OPENED state SHOULD be silently discarded.
377 if( f->state != OPENED ){
378 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
384 * Upcall the proper Protocol-Reject routine.
386 for (i = 0; (protp = protocols[i]) != NULL; ++i)
387 if (protp->protocol == prot && protp->enabled_flag) {
388 (*protp->protrej)(f->unit);
392 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
398 * lcp_protrej - A Protocol-Reject was received.
408 LCPDEBUG((LOG_WARNING,
409 "lcp_protrej: Received Protocol-Reject for LCP!"));
410 fsm_protreject(&lcp_fsm[unit]);
415 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
418 lcp_sprotrej(unit, p, len)
424 * Send back the protocol and the information field of the
425 * rejected packet. We only get here if LCP is in the OPENED state.
430 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
436 * lcp_resetci - Reset our CI.
442 lcp_wantoptions[f->unit].magicnumber = magic();
443 lcp_wantoptions[f->unit].numloops = 0;
444 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
445 peer_mru[f->unit] = PPP_MRU;
451 * lcp_cilen - Return length of our CI.
457 lcp_options *go = &lcp_gotoptions[f->unit];
459 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
460 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
461 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
462 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
463 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
464 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
466 * NB: we only ask for one of CHAP and UPAP, even if we will
469 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
470 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
471 LENCICHAP(go->neg_chap) +
472 LENCISHORT(!go->neg_chap && go->neg_upap) +
473 LENCILQR(go->neg_lqr) +
474 LENCICBCP(go->neg_cbcp) +
475 LENCILONG(go->neg_magicnumber) +
476 LENCIVOID(go->neg_pcompression) +
477 LENCIVOID(go->neg_accompression));
482 * lcp_addci - Add our desired CIs to a packet.
485 lcp_addci(f, ucp, lenp)
490 lcp_options *go = &lcp_gotoptions[f->unit];
491 u_char *start_ucp = ucp;
493 #define ADDCIVOID(opt, neg) \
496 PUTCHAR(CILEN_VOID, ucp); \
498 #define ADDCISHORT(opt, neg, val) \
501 PUTCHAR(CILEN_SHORT, ucp); \
502 PUTSHORT(val, ucp); \
504 #define ADDCICHAP(opt, neg, val, digest) \
507 PUTCHAR(CILEN_CHAP, ucp); \
508 PUTSHORT(val, ucp); \
509 PUTCHAR(digest, ucp); \
511 #define ADDCILONG(opt, neg, val) \
514 PUTCHAR(CILEN_LONG, ucp); \
517 #define ADDCILQR(opt, neg, val) \
520 PUTCHAR(CILEN_LQR, ucp); \
521 PUTSHORT(PPP_LQR, ucp); \
524 #define ADDCICHAR(opt, neg, val) \
527 PUTCHAR(CILEN_CHAR, ucp); \
531 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
532 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
534 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
535 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
536 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
537 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
538 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
539 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
540 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
542 if (ucp - start_ucp != *lenp) {
543 /* this should never happen, because peer_mtu should be 1500 */
544 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
550 * lcp_ackci - Ack our CIs.
551 * This should not modify any state if the Ack is bad.
563 lcp_options *go = &lcp_gotoptions[f->unit];
564 u_char cilen, citype, cichar;
569 * CIs must be in exactly the same order that we sent.
570 * Check packet length and CI length at each step.
571 * If we find any deviations, then this packet is bad.
573 #define ACKCIVOID(opt, neg) \
575 if ((len -= CILEN_VOID) < 0) \
577 GETCHAR(citype, p); \
579 if (cilen != CILEN_VOID || \
583 #define ACKCISHORT(opt, neg, val) \
585 if ((len -= CILEN_SHORT) < 0) \
587 GETCHAR(citype, p); \
589 if (cilen != CILEN_SHORT || \
592 GETSHORT(cishort, p); \
593 if (cishort != val) \
596 #define ACKCICHAR(opt, neg, val) \
598 if ((len -= CILEN_CHAR) < 0) \
600 GETCHAR(citype, p); \
602 if (cilen != CILEN_CHAR || \
605 GETCHAR(cichar, p); \
609 #define ACKCICHAP(opt, neg, val, digest) \
611 if ((len -= CILEN_CHAP) < 0) \
613 GETCHAR(citype, p); \
615 if (cilen != CILEN_CHAP || \
618 GETSHORT(cishort, p); \
619 if (cishort != val) \
621 GETCHAR(cichar, p); \
622 if (cichar != digest) \
625 #define ACKCILONG(opt, neg, val) \
627 if ((len -= CILEN_LONG) < 0) \
629 GETCHAR(citype, p); \
631 if (cilen != CILEN_LONG || \
634 GETLONG(cilong, p); \
638 #define ACKCILQR(opt, neg, val) \
640 if ((len -= CILEN_LQR) < 0) \
642 GETCHAR(citype, p); \
644 if (cilen != CILEN_LQR || \
647 GETSHORT(cishort, p); \
648 if (cishort != PPP_LQR) \
650 GETLONG(cilong, p); \
655 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
656 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
658 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
659 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
660 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
661 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
662 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
663 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
664 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
667 * If there are any remaining CIs, then this packet is bad.
673 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
679 * lcp_nakci - Peer has sent a NAK for some of our CIs.
680 * This should not modify any state if the Nak is bad
681 * or if LCP is in the OPENED state.
693 lcp_options *go = &lcp_gotoptions[f->unit];
694 lcp_options *wo = &lcp_wantoptions[f->unit];
695 u_char citype, cichar, *next;
698 lcp_options no; /* options we've seen Naks for */
699 lcp_options try; /* options to request next time */
703 BZERO(&no, sizeof(no));
707 * Any Nak'd CIs must be in exactly the same order that we sent.
708 * Check packet length and CI length at each step.
709 * If we find any deviations, then this packet is bad.
711 #define NAKCIVOID(opt, neg, code) \
713 len >= CILEN_VOID && \
714 p[1] == CILEN_VOID && \
717 INCPTR(CILEN_VOID, p); \
721 #define NAKCICHAP(opt, neg, code) \
723 len >= CILEN_CHAP && \
724 p[1] == CILEN_CHAP && \
728 GETSHORT(cishort, p); \
729 GETCHAR(cichar, p); \
733 #define NAKCICHAR(opt, neg, code) \
735 len >= CILEN_CHAR && \
736 p[1] == CILEN_CHAR && \
740 GETCHAR(cichar, p); \
744 #define NAKCISHORT(opt, neg, code) \
746 len >= CILEN_SHORT && \
747 p[1] == CILEN_SHORT && \
749 len -= CILEN_SHORT; \
751 GETSHORT(cishort, p); \
755 #define NAKCILONG(opt, neg, code) \
757 len >= CILEN_LONG && \
758 p[1] == CILEN_LONG && \
762 GETLONG(cilong, p); \
766 #define NAKCILQR(opt, neg, code) \
768 len >= CILEN_LQR && \
769 p[1] == CILEN_LQR && \
773 GETSHORT(cishort, p); \
774 GETLONG(cilong, p); \
780 * We don't care if they want to send us smaller packets than
781 * we want. Therefore, accept any MRU less than what we asked for,
782 * but then ignore the new value when setting the MRU in the kernel.
783 * If they send us a bigger MRU than what we asked, accept it, up to
784 * the limit of the default MRU we'd get if we didn't negotiate.
786 if (go->neg_mru && go->mru != DEFMRU) {
787 NAKCISHORT(CI_MRU, neg_mru,
788 if (cishort <= wo->mru || cishort < DEFMRU)
794 * Add any characters they want to our (receive-side) asyncmap.
796 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
797 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
798 try.asyncmap = go->asyncmap | cilong;
803 * If they've nak'd our authentication-protocol, check whether
804 * they are proposing a different protocol, or a different
805 * hash algorithm for CHAP.
807 if ((go->neg_chap || go->neg_upap)
808 && len >= CILEN_SHORT
809 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
812 no.neg_chap = go->neg_chap;
813 no.neg_upap = go->neg_upap;
815 GETSHORT(cishort, p);
816 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
818 * If we were asking for CHAP, they obviously don't want to do it.
819 * If we weren't asking for CHAP, then we were asking for PAP,
820 * in which case this Nak is bad.
826 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
830 * We were asking for CHAP/MD5; they must want a different
831 * algorithm. If they can't do MD5, we'll have to stop
834 if (cichar != go->chap_mdtype)
838 * Stop asking for PAP if we were asking for it.
845 * We don't recognize what they're suggesting.
846 * Stop asking for what we were asking for.
852 p += cilen - CILEN_SHORT;
857 * If they can't cope with our link quality protocol, we'll have
858 * to stop asking for LQR. We haven't got any other protocol.
859 * If they Nak the reporting period, take their value XXX ?
861 NAKCILQR(CI_QUALITY, neg_lqr,
862 if (cishort != PPP_LQR)
865 try.lqr_period = cilong;
869 * Only implementing CBCP...not the rest of the callback options
871 NAKCICHAR(CI_CALLBACK, neg_cbcp,
876 * Check for a looped-back line.
878 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
879 try.magicnumber = magic();
884 * Peer shouldn't send Nak for protocol compression or
885 * address/control compression requests; they should send
886 * a Reject instead. If they send a Nak, treat it as a Reject.
888 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
889 try.neg_pcompression = 0;
891 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
892 try.neg_accompression = 0;
896 * There may be remaining CIs, if the peer is requesting negotiation
897 * on an option that we didn't include in our request packet.
898 * If we see an option that we requested, or one we've already seen
899 * in this packet, then this packet is bad.
900 * If we wanted to respond by starting to negotiate on the requested
901 * option(s), we could, but we don't, because except for the
902 * authentication type and quality protocol, if we are not negotiating
903 * an option, it is because we were told not to.
904 * For the authentication type, the Nak from the peer means
905 * `let me authenticate myself with you' which is a bit pointless.
906 * For the quality protocol, the Nak means `ask me to send you quality
907 * reports', but if we didn't ask for them, we don't want them.
908 * An option we don't recognize represents the peer asking to
909 * negotiate some option we don't support, so ignore it.
911 while (len > CILEN_VOID) {
914 if (cilen < CILEN_VOID || (len -= cilen) < 0)
916 next = p + cilen - 2;
920 if ((go->neg_mru && go->mru != DEFMRU)
921 || no.neg_mru || cilen != CILEN_SHORT)
923 GETSHORT(cishort, p);
924 if (cishort < DEFMRU)
928 if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF)
929 || no.neg_asyncmap || cilen != CILEN_LONG)
933 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
937 if (go->neg_magicnumber || no.neg_magicnumber ||
941 case CI_PCOMPRESSION:
942 if (go->neg_pcompression || no.neg_pcompression
943 || cilen != CILEN_VOID)
946 case CI_ACCOMPRESSION:
947 if (go->neg_accompression || no.neg_accompression
948 || cilen != CILEN_VOID)
952 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
959 /* If there is still anything left, this packet is bad. */
964 * OK, the Nak is good. Now we can update state.
966 if (f->state != OPENED) {
968 if (++try.numloops >= lcp_loopbackfail) {
969 syslog(LOG_NOTICE, "Serial line is looped back.");
970 lcp_close(f->unit, "Loopback detected");
980 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
986 * lcp_rejci - Peer has Rejected some of our CIs.
987 * This should not modify any state if the Reject is bad
988 * or if LCP is in the OPENED state.
991 * 0 - Reject was bad.
992 * 1 - Reject was good.
1000 lcp_options *go = &lcp_gotoptions[f->unit];
1004 lcp_options try; /* options to request next time */
1009 * Any Rejected CIs must be in exactly the same order that we sent.
1010 * Check packet length and CI length at each step.
1011 * If we find any deviations, then this packet is bad.
1013 #define REJCIVOID(opt, neg) \
1015 len >= CILEN_VOID && \
1016 p[1] == CILEN_VOID && \
1018 len -= CILEN_VOID; \
1019 INCPTR(CILEN_VOID, p); \
1021 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
1023 #define REJCISHORT(opt, neg, val) \
1025 len >= CILEN_SHORT && \
1026 p[1] == CILEN_SHORT && \
1028 len -= CILEN_SHORT; \
1030 GETSHORT(cishort, p); \
1031 /* Check rejected value. */ \
1032 if (cishort != val) \
1035 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1037 #define REJCICHAP(opt, neg, val, digest) \
1039 len >= CILEN_CHAP && \
1040 p[1] == CILEN_CHAP && \
1042 len -= CILEN_CHAP; \
1044 GETSHORT(cishort, p); \
1045 GETCHAR(cichar, p); \
1046 /* Check rejected value. */ \
1047 if (cishort != val || cichar != digest) \
1051 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1053 #define REJCILONG(opt, neg, val) \
1055 len >= CILEN_LONG && \
1056 p[1] == CILEN_LONG && \
1058 len -= CILEN_LONG; \
1060 GETLONG(cilong, p); \
1061 /* Check rejected value. */ \
1062 if (cilong != val) \
1065 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1067 #define REJCILQR(opt, neg, val) \
1069 len >= CILEN_LQR && \
1070 p[1] == CILEN_LQR && \
1074 GETSHORT(cishort, p); \
1075 GETLONG(cilong, p); \
1076 /* Check rejected value. */ \
1077 if (cishort != PPP_LQR || cilong != val) \
1080 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1082 #define REJCICBCP(opt, neg, val) \
1084 len >= CILEN_CBCP && \
1085 p[1] == CILEN_CBCP && \
1087 len -= CILEN_CBCP; \
1089 GETCHAR(cichar, p); \
1090 /* Check rejected value. */ \
1091 if (cichar != val) \
1094 LCPDEBUG((LOG_INFO,"lcp_rejci rejected Callback opt %d", opt)); \
1097 REJCISHORT(CI_MRU, neg_mru, go->mru);
1098 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1099 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1100 if (!go->neg_chap) {
1101 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1103 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1104 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1105 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1106 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1107 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1110 * If there are any remaining CIs, then this packet is bad.
1115 * Now we can update state.
1117 if (f->state != OPENED)
1122 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1128 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1130 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1131 * appropriately. If reject_if_disagree is non-zero, doesn't return
1132 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1135 lcp_reqci(f, inp, lenp, reject_if_disagree)
1137 u_char *inp; /* Requested CIs */
1138 int *lenp; /* Length of requested CIs */
1139 int reject_if_disagree;
1141 lcp_options *go = &lcp_gotoptions[f->unit];
1142 lcp_options *ho = &lcp_hisoptions[f->unit];
1143 lcp_options *ao = &lcp_allowoptions[f->unit];
1144 u_char *cip, *next; /* Pointer to current and next CIs */
1145 int cilen, citype, cichar; /* Parsed len, type, char value */
1146 u_short cishort; /* Parsed short value */
1147 u_int32_t cilong; /* Parse long value */
1148 int rc = CONFACK; /* Final packet return code */
1149 int orc; /* Individual option return code */
1150 u_char *p; /* Pointer to next char to parse */
1151 u_char *rejp; /* Pointer to next char in reject frame */
1152 u_char *nakp; /* Pointer to next char in Nak frame */
1153 int l = *lenp; /* Length left */
1156 * Reset all his options.
1158 BZERO(ho, sizeof(*ho));
1161 * Process all his options.
1167 orc = CONFACK; /* Assume success */
1168 cip = p = next; /* Remember begining of CI */
1169 if (l < 2 || /* Not enough data for CI header or */
1170 p[1] < 2 || /* CI length too small or */
1171 p[1] > l) { /* CI length too big? */
1172 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1173 orc = CONFREJ; /* Reject bad CI */
1174 cilen = l; /* Reject till end of packet */
1175 l = 0; /* Don't loop again */
1179 GETCHAR(citype, p); /* Parse CI type */
1180 GETCHAR(cilen, p); /* Parse CI length */
1181 l -= cilen; /* Adjust remaining length */
1182 next += cilen; /* Step to next CI */
1184 switch (citype) { /* Check CI type */
1186 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1187 if (!ao->neg_mru || /* Allow option? */
1188 cilen != CILEN_SHORT) { /* Check CI length */
1189 orc = CONFREJ; /* Reject CI */
1192 GETSHORT(cishort, p); /* Parse MRU */
1193 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1196 * He must be able to receive at least our minimum.
1197 * No need to check a maximum. If he sends a large number,
1198 * we'll just ignore it.
1200 if (cishort < MINMRU) {
1201 orc = CONFNAK; /* Nak CI */
1202 PUTCHAR(CI_MRU, nakp);
1203 PUTCHAR(CILEN_SHORT, nakp);
1204 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1207 ho->neg_mru = 1; /* Remember he sent MRU */
1208 ho->mru = cishort; /* And remember value */
1212 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1213 if (!ao->neg_asyncmap ||
1214 cilen != CILEN_LONG) {
1219 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1222 * Asyncmap must have set at least the bits
1223 * which are set in lcp_allowoptions[unit].asyncmap.
1225 if ((ao->asyncmap & ~cilong) != 0) {
1227 PUTCHAR(CI_ASYNCMAP, nakp);
1228 PUTCHAR(CILEN_LONG, nakp);
1229 PUTLONG(ao->asyncmap | cilong, nakp);
1232 ho->neg_asyncmap = 1;
1233 ho->asyncmap = cilong;
1237 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1238 if (cilen < CILEN_SHORT ||
1239 !(ao->neg_upap || ao->neg_chap)) {
1241 * Reject the option if we're not willing to authenticate.
1246 GETSHORT(cishort, p);
1247 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1250 * Authtype must be UPAP or CHAP.
1252 * Note: if both ao->neg_upap and ao->neg_chap are set,
1253 * and the peer sends a Configure-Request with two
1254 * authenticate-protocol requests, one for CHAP and one
1255 * for UPAP, then we will reject the second request.
1256 * Whether we end up doing CHAP or UPAP depends then on
1257 * the ordering of the CIs in the peer's Configure-Request.
1260 if (cishort == PPP_PAP) {
1261 if (ho->neg_chap || /* we've already accepted CHAP */
1262 cilen != CILEN_SHORT) {
1263 LCPDEBUG((LOG_WARNING,
1264 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1268 if (!ao->neg_upap) { /* we don't want to do PAP */
1269 orc = CONFNAK; /* NAK it and suggest CHAP */
1270 PUTCHAR(CI_AUTHTYPE, nakp);
1271 PUTCHAR(CILEN_CHAP, nakp);
1272 PUTSHORT(PPP_CHAP, nakp);
1273 PUTCHAR(ao->chap_mdtype, nakp);
1279 if (cishort == PPP_CHAP) {
1280 if (ho->neg_upap || /* we've already accepted PAP */
1281 cilen != CILEN_CHAP) {
1283 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1287 if (!ao->neg_chap) { /* we don't want to do CHAP */
1288 orc = CONFNAK; /* NAK it and suggest PAP */
1289 PUTCHAR(CI_AUTHTYPE, nakp);
1290 PUTCHAR(CILEN_SHORT, nakp);
1291 PUTSHORT(PPP_PAP, nakp);
1294 GETCHAR(cichar, p); /* get digest type*/
1295 if (cichar != CHAP_DIGEST_MD5
1297 && cichar != CHAP_MICROSOFT
1301 PUTCHAR(CI_AUTHTYPE, nakp);
1302 PUTCHAR(CILEN_CHAP, nakp);
1303 PUTSHORT(PPP_CHAP, nakp);
1304 PUTCHAR(ao->chap_mdtype, nakp);
1307 ho->chap_mdtype = cichar; /* save md type */
1313 * We don't recognize the protocol they're asking for.
1314 * Nak it with something we're willing to do.
1315 * (At this point we know ao->neg_upap || ao->neg_chap.)
1318 PUTCHAR(CI_AUTHTYPE, nakp);
1320 PUTCHAR(CILEN_CHAP, nakp);
1321 PUTSHORT(PPP_CHAP, nakp);
1322 PUTCHAR(ao->chap_mdtype, nakp);
1324 PUTCHAR(CILEN_SHORT, nakp);
1325 PUTSHORT(PPP_PAP, nakp);
1330 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1332 cilen != CILEN_LQR) {
1337 GETSHORT(cishort, p);
1339 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1342 * Check the protocol and the reporting period.
1343 * XXX When should we Nak this, and what with?
1345 if (cishort != PPP_LQR) {
1347 PUTCHAR(CI_QUALITY, nakp);
1348 PUTCHAR(CILEN_LQR, nakp);
1349 PUTSHORT(PPP_LQR, nakp);
1350 PUTLONG(ao->lqr_period, nakp);
1355 case CI_MAGICNUMBER:
1356 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1357 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1358 cilen != CILEN_LONG) {
1363 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1366 * He must have a different magic number.
1368 if (go->neg_magicnumber &&
1369 cilong == go->magicnumber) {
1370 cilong = magic(); /* Don't put magic() inside macro! */
1372 PUTCHAR(CI_MAGICNUMBER, nakp);
1373 PUTCHAR(CILEN_LONG, nakp);
1374 PUTLONG(cilong, nakp);
1377 ho->neg_magicnumber = 1;
1378 ho->magicnumber = cilong;
1382 case CI_PCOMPRESSION:
1383 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1384 if (!ao->neg_pcompression ||
1385 cilen != CILEN_VOID) {
1389 ho->neg_pcompression = 1;
1392 case CI_ACCOMPRESSION:
1393 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1394 if (!ao->neg_accompression ||
1395 cilen != CILEN_VOID) {
1399 ho->neg_accompression = 1;
1403 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1410 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1411 if (orc == CONFACK && /* Good CI */
1412 rc != CONFACK) /* but prior CI wasnt? */
1413 continue; /* Don't send this one */
1415 if (orc == CONFNAK) { /* Nak this CI? */
1416 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1417 && citype != CI_MAGICNUMBER) {
1418 orc = CONFREJ; /* Get tough if so */
1420 if (rc == CONFREJ) /* Rejecting prior CI? */
1421 continue; /* Don't send this one */
1425 if (orc == CONFREJ) { /* Reject this CI */
1427 if (cip != rejp) /* Need to move rejected CI? */
1428 BCOPY(cip, rejp, cilen); /* Move it */
1429 INCPTR(cilen, rejp); /* Update output pointer */
1434 * If we wanted to send additional NAKs (for unsent CIs), the
1435 * code would go here. The extra NAKs would go at *nakp.
1436 * At present there are no cases where we want to ask the
1437 * peer to negotiate an option.
1446 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1448 *lenp = nakp - nak_buffer;
1449 BCOPY(nak_buffer, inp, *lenp);
1456 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1457 return (rc); /* Return final code */
1462 * lcp_up - LCP has come UP.
1468 lcp_options *wo = &lcp_wantoptions[f->unit];
1469 lcp_options *ho = &lcp_hisoptions[f->unit];
1470 lcp_options *go = &lcp_gotoptions[f->unit];
1471 lcp_options *ao = &lcp_allowoptions[f->unit];
1473 if (!go->neg_magicnumber)
1474 go->magicnumber = 0;
1475 if (!ho->neg_magicnumber)
1476 ho->magicnumber = 0;
1479 * Set our MTU to the smaller of the MTU we wanted and
1480 * the MRU our peer wanted. If we negotiated an MRU,
1481 * set our MRU to the larger of value we wanted and
1482 * the value we got in the negotiation.
1484 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1485 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1486 ho->neg_pcompression, ho->neg_accompression);
1488 * If the asyncmap hasn't been negotiated, we really should
1489 * set the receive asyncmap to ffffffff, but we set it to 0
1490 * for backwards contemptibility.
1492 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1493 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1494 go->neg_pcompression, go->neg_accompression);
1497 peer_mru[f->unit] = ho->mru;
1499 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1501 link_established(f->unit);
1506 * lcp_down - LCP has gone DOWN.
1508 * Alert other protocols.
1514 lcp_options *go = &lcp_gotoptions[f->unit];
1516 lcp_echo_lowerdown(f->unit);
1520 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1521 ppp_recv_config(f->unit, PPP_MRU,
1522 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1523 go->neg_pcompression, go->neg_accompression);
1524 peer_mru[f->unit] = PPP_MRU;
1529 * lcp_starting - LCP needs the lower layer up.
1535 link_required(f->unit);
1540 * lcp_finished - LCP has finished with the lower layer.
1546 link_terminated(f->unit);
1551 * lcp_printpkt - print the contents of an LCP packet.
1553 static char *lcp_codenames[] = {
1554 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1555 "TermReq", "TermAck", "CodeRej", "ProtRej",
1556 "EchoReq", "EchoRep", "DiscReq"
1560 lcp_printpkt(p, plen, printer, arg)
1563 void (*printer) __P((void *, char *, ...));
1566 int code, id, len, olen;
1567 u_char *pstart, *optend;
1571 if (plen < HEADERLEN)
1577 if (len < HEADERLEN || len > plen)
1580 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1581 printer(arg, " %s", lcp_codenames[code-1]);
1583 printer(arg, " code=0x%x", code);
1584 printer(arg, " id=0x%x", id);
1591 /* print option list */
1596 if (olen < 2 || olen > len) {
1604 if (olen == CILEN_SHORT) {
1606 GETSHORT(cishort, p);
1607 printer(arg, "mru %d", cishort);
1611 if (olen == CILEN_LONG) {
1614 printer(arg, "asyncmap 0x%x", cilong);
1618 if (olen >= CILEN_SHORT) {
1620 printer(arg, "auth ");
1621 GETSHORT(cishort, p);
1624 printer(arg, "pap");
1627 printer(arg, "chap");
1630 printer(arg, "0x%x", cishort);
1635 if (olen >= CILEN_SHORT) {
1637 printer(arg, "quality ");
1638 GETSHORT(cishort, p);
1641 printer(arg, "lqr");
1644 printer(arg, "0x%x", cishort);
1649 if (olen >= CILEN_CHAR) {
1651 printer(arg, "callback ");
1652 GETSHORT(cishort, p);
1655 printer(arg, "CBCP");
1658 printer(arg, "0x%x", cishort);
1662 case CI_MAGICNUMBER:
1663 if (olen == CILEN_LONG) {
1666 printer(arg, "magic 0x%x", cilong);
1669 case CI_PCOMPRESSION:
1670 if (olen == CILEN_VOID) {
1672 printer(arg, "pcomp");
1675 case CI_ACCOMPRESSION:
1676 if (olen == CILEN_VOID) {
1678 printer(arg, "accomp");
1682 while (p < optend) {
1684 printer(arg, " %.2x", code);
1692 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1694 print_string(p, len, printer, arg);
1705 printer(arg, " magic=0x%x", cilong);
1712 /* print the rest of the bytes in the packet */
1713 for (; len > 0; --len) {
1715 printer(arg, " %.2x", code);
1722 * Time to shut down the link because there is nothing out there.
1726 void LcpLinkFailure (f)
1729 if (f->state == OPENED) {
1730 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1731 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1732 lcp_close(f->unit, "Peer not responding");
1737 * Timer expired for the LCP echo requests from this process.
1744 LcpSendEchoRequest (f);
1747 * Start the timer for the next interval.
1749 assert (lcp_echo_timer_running==0);
1750 TIMEOUT (LcpEchoTimeout, f, lcp_echo_interval);
1751 lcp_echo_timer_running = 1;
1755 * LcpEchoTimeout - Timer expired on the LCP echo
1759 LcpEchoTimeout (arg)
1762 if (lcp_echo_timer_running != 0) {
1763 lcp_echo_timer_running = 0;
1764 LcpEchoCheck ((fsm *) arg);
1769 * LcpEchoReply - LCP has received a reply to the echo
1773 lcp_received_echo_reply (f, id, inp, len)
1775 int id; u_char *inp; int len;
1779 /* Check the magic number - don't count replies from ourselves. */
1781 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1784 GETLONG(magic, inp);
1785 if (lcp_gotoptions[f->unit].neg_magicnumber
1786 && magic == lcp_gotoptions[f->unit].magicnumber) {
1787 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1791 /* Reset the number of outstanding echo frames */
1792 lcp_echos_pending = 0;
1796 * LcpSendEchoRequest - Send an echo request frame to the peer
1800 LcpSendEchoRequest (f)
1803 u_int32_t lcp_magic;
1804 u_char pkt[4], *pktp;
1807 * Detect the failure of the peer at this point.
1809 if (lcp_echo_fails != 0) {
1810 if (lcp_echos_pending++ >= lcp_echo_fails) {
1812 lcp_echos_pending = 0;
1817 * Make and send the echo request frame.
1819 if (f->state == OPENED) {
1820 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1822 PUTLONG(lcp_magic, pktp);
1823 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1828 * lcp_echo_lowerup - Start the timer for the LCP frame
1832 lcp_echo_lowerup (unit)
1835 fsm *f = &lcp_fsm[unit];
1837 /* Clear the parameters for generating echo frames */
1838 lcp_echos_pending = 0;
1839 lcp_echo_number = 0;
1840 lcp_echo_timer_running = 0;
1842 /* If a timeout interval is specified then start the timer */
1843 if (lcp_echo_interval != 0)
1848 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1852 lcp_echo_lowerdown (unit)
1855 fsm *f = &lcp_fsm[unit];
1857 if (lcp_echo_timer_running != 0) {
1858 UNTIMEOUT (LcpEchoTimeout, f);
1859 lcp_echo_timer_running = 0;