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.14 1994/09/21 06:47:37 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
46 #ifdef _linux_ /* Needs ppp ioctls */
47 #include <linux/ppp.h>
51 fsm lcp_fsm[N_PPP]; /* LCP fsm structure (global)*/
52 lcp_options lcp_wantoptions[N_PPP]; /* Options that we want to request */
53 lcp_options lcp_gotoptions[N_PPP]; /* Options that peer ack'd */
54 lcp_options lcp_allowoptions[N_PPP]; /* Options we allow peer to request */
55 lcp_options lcp_hisoptions[N_PPP]; /* Options that we ack'd */
56 u_int32_t xmit_accm[N_PPP][8]; /* extended transmit ACCM */
58 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
59 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
60 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
63 * Callbacks for fsm code. (CI = Configuration Information)
65 static void lcp_resetci __P((fsm *)); /* Reset our CI */
66 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
67 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
68 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
69 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
70 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
71 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
72 static void lcp_up __P((fsm *)); /* We're UP */
73 static void lcp_down __P((fsm *)); /* We're DOWN */
74 static void lcp_starting __P((fsm *)); /* We need lower layer up */
75 static void lcp_finished __P((fsm *)); /* We need lower layer down */
76 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
77 static void lcp_rprotrej __P((fsm *, u_char *, int));
80 * routines to send LCP echos to peer
83 static void lcp_echo_lowerup __P((int));
84 static void lcp_echo_lowerdown __P((int));
85 static void LcpEchoTimeout __P((caddr_t));
86 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
87 static void LcpSendEchoRequest __P((fsm *));
88 static void LcpLinkFailure __P((fsm *));
90 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
91 lcp_resetci, /* Reset our Configuration Information */
92 lcp_cilen, /* Length of our Configuration Information */
93 lcp_addci, /* Add our Configuration Information */
94 lcp_ackci, /* ACK our Configuration Information */
95 lcp_nakci, /* NAK our Configuration Information */
96 lcp_rejci, /* Reject our Configuration Information */
97 lcp_reqci, /* Request peer's Configuration Information */
98 lcp_up, /* Called when fsm reaches OPENED state */
99 lcp_down, /* Called when fsm leaves OPENED state */
100 lcp_starting, /* Called when we want the lower layer up */
101 lcp_finished, /* Called when we want the lower layer down */
102 NULL, /* Called when Protocol-Reject received */
103 NULL, /* Retransmission is necessary */
104 lcp_extcode, /* Called to handle LCP-specific codes */
105 "LCP" /* String name of protocol */
108 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
111 * Length of each type of configuration option (in octets)
114 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
115 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
116 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
117 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
119 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
120 (x) == CONFNAK ? "NAK" : "REJ")
124 * lcp_init - Initialize LCP.
130 fsm *f = &lcp_fsm[unit];
131 lcp_options *wo = &lcp_wantoptions[unit];
132 lcp_options *ao = &lcp_allowoptions[unit];
135 f->protocol = PPP_LCP;
136 f->callbacks = &lcp_callbacks;
142 wo->restart = 0; /* Set to 1 in kernels or multi-line
146 wo->neg_asyncmap = 0;
148 wo->neg_chap = 0; /* Set to 1 on server */
149 wo->neg_upap = 0; /* Set to 1 on server */
150 wo->chap_mdtype = CHAP_DIGEST_MD5;
151 wo->neg_magicnumber = 1;
152 wo->neg_pcompression = 1;
153 wo->neg_accompression = 1;
154 wo->neg_lqr = 0; /* no LQR implementation yet */
158 ao->neg_asyncmap = 1;
161 ao->chap_mdtype = CHAP_DIGEST_MD5;
163 ao->neg_magicnumber = 1;
164 ao->neg_pcompression = 1;
165 ao->neg_accompression = 1;
166 ao->neg_lqr = 0; /* no LQR implementation yet */
168 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
169 xmit_accm[unit][3] = 0x60000000;
174 * lcp_open - LCP is allowed to come up.
180 fsm *f = &lcp_fsm[unit];
181 lcp_options *wo = &lcp_wantoptions[unit];
185 f->flags |= OPT_PASSIVE;
187 f->flags |= OPT_SILENT;
193 * lcp_close - Take LCP down.
199 fsm *f = &lcp_fsm[unit];
201 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
203 * This action is not strictly according to the FSM in RFC1548,
204 * but it does mean that the program terminates if you do a
205 * lcp_close(0) in passive/silent mode when a connection hasn't
212 fsm_close(&lcp_fsm[unit]);
217 * lcp_lowerup - The lower layer is up.
224 ppp_set_xaccm(unit, xmit_accm[unit]);
225 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
226 ppp_recv_config(unit, PPP_MRU, 0x00000000, 0, 0);
227 peer_mru[unit] = PPP_MRU;
228 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
230 fsm_lowerup(&lcp_fsm[unit]);
235 * lcp_lowerdown - The lower layer is down.
241 fsm_lowerdown(&lcp_fsm[unit]);
246 * lcp_input - Input LCP packet.
249 lcp_input(unit, p, len)
255 fsm *f = &lcp_fsm[unit];
256 lcp_options *go = &lcp_gotoptions[f->unit];
259 fsm_input(f, p, len);
260 if (oldstate == REQSENT && f->state == ACKSENT) {
262 * The peer will probably send us an ack soon and then
263 * immediately start sending packets with the negotiated
264 * options. So as to be ready when that happens, we set
265 * our receive side to accept packets as negotiated now.
267 ppp_recv_config(f->unit, PPP_MRU,
268 go->neg_asyncmap? go->asyncmap: 0x00000000,
269 go->neg_pcompression, go->neg_accompression);
275 * lcp_extcode - Handle a LCP-specific code.
278 lcp_extcode(f, code, id, inp, len)
288 lcp_rprotrej(f, inp, len);
292 if (f->state != OPENED)
294 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
296 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
297 fsm_sdata(f, ECHOREP, id, inp, len);
301 lcp_received_echo_reply(f, id, inp, len);
315 * lcp_rprotrej - Receive an Protocol-Reject.
317 * Figure out which protocol is rejected and inform it.
320 lcp_rprotrej(f, inp, len)
327 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
329 if (len < sizeof (u_short)) {
331 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
338 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
342 * Protocol-Reject packets received in any state other than the LCP
343 * OPENED state SHOULD be silently discarded.
345 if( f->state != OPENED ){
346 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
351 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
356 * lcp_protrej - A Protocol-Reject was received.
366 LCPDEBUG((LOG_WARNING,
367 "lcp_protrej: Received Protocol-Reject for LCP!"));
368 fsm_protreject(&lcp_fsm[unit]);
373 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
376 lcp_sprotrej(unit, p, len)
382 * Send back the protocol and the information field of the
383 * rejected packet. We only get here if LCP is in the OPENED state.
388 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
394 * lcp_resetci - Reset our CI.
400 lcp_wantoptions[f->unit].magicnumber = magic();
401 lcp_wantoptions[f->unit].numloops = 0;
402 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
403 peer_mru[f->unit] = PPP_MRU;
408 * lcp_cilen - Return length of our CI.
414 lcp_options *go = &lcp_gotoptions[f->unit];
416 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
417 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
418 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
419 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
420 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
422 * NB: we only ask for one of CHAP and UPAP, even if we will
425 return (LENCISHORT(go->neg_mru) +
426 LENCILONG(go->neg_asyncmap) +
427 LENCICHAP(go->neg_chap) +
428 LENCISHORT(!go->neg_chap && go->neg_upap) +
429 LENCILQR(go->neg_lqr) +
430 LENCILONG(go->neg_magicnumber) +
431 LENCIVOID(go->neg_pcompression) +
432 LENCIVOID(go->neg_accompression));
437 * lcp_addci - Add our desired CIs to a packet.
440 lcp_addci(f, ucp, lenp)
445 lcp_options *go = &lcp_gotoptions[f->unit];
446 u_char *start_ucp = ucp;
448 #define ADDCIVOID(opt, neg) \
451 PUTCHAR(CILEN_VOID, ucp); \
453 #define ADDCISHORT(opt, neg, val) \
456 PUTCHAR(CILEN_SHORT, ucp); \
457 PUTSHORT(val, ucp); \
459 #define ADDCICHAP(opt, neg, val, digest) \
462 PUTCHAR(CILEN_CHAP, ucp); \
463 PUTSHORT(val, ucp); \
464 PUTCHAR(digest, ucp); \
466 #define ADDCILONG(opt, neg, val) \
469 PUTCHAR(CILEN_LONG, ucp); \
472 #define ADDCILQR(opt, neg, val) \
475 PUTCHAR(CILEN_LQR, ucp); \
476 PUTSHORT(PPP_LQR, ucp); \
480 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
481 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
482 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
483 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
484 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
485 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
486 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
487 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
489 if (ucp - start_ucp != *lenp) {
490 /* this should never happen, because peer_mtu should be 1500 */
491 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
497 * lcp_ackci - Ack our CIs.
498 * This should not modify any state if the Ack is bad.
510 lcp_options *go = &lcp_gotoptions[f->unit];
511 u_char cilen, citype, cichar;
516 * CIs must be in exactly the same order that we sent.
517 * Check packet length and CI length at each step.
518 * If we find any deviations, then this packet is bad.
520 #define ACKCIVOID(opt, neg) \
522 if ((len -= CILEN_VOID) < 0) \
524 GETCHAR(citype, p); \
526 if (cilen != CILEN_VOID || \
530 #define ACKCISHORT(opt, neg, val) \
532 if ((len -= CILEN_SHORT) < 0) \
534 GETCHAR(citype, p); \
536 if (cilen != CILEN_SHORT || \
539 GETSHORT(cishort, p); \
540 if (cishort != val) \
543 #define ACKCICHAP(opt, neg, val, digest) \
545 if ((len -= CILEN_CHAP) < 0) \
547 GETCHAR(citype, p); \
549 if (cilen != CILEN_CHAP || \
552 GETSHORT(cishort, p); \
553 if (cishort != val) \
555 GETCHAR(cichar, p); \
556 if (cichar != digest) \
559 #define ACKCILONG(opt, neg, val) \
561 if ((len -= CILEN_LONG) < 0) \
563 GETCHAR(citype, p); \
565 if (cilen != CILEN_LONG || \
568 GETLONG(cilong, p); \
572 #define ACKCILQR(opt, neg, val) \
574 if ((len -= CILEN_LQR) < 0) \
576 GETCHAR(citype, p); \
578 if (cilen != CILEN_LQR || \
581 GETSHORT(cishort, p); \
582 if (cishort != PPP_LQR) \
584 GETLONG(cilong, p); \
589 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
590 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
591 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
592 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
593 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
594 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
595 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
596 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
599 * If there are any remaining CIs, then this packet is bad.
605 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
611 * lcp_nakci - Peer has sent a NAK for some of our CIs.
612 * This should not modify any state if the Nak is bad
613 * or if LCP is in the OPENED state.
625 lcp_options *go = &lcp_gotoptions[f->unit];
626 lcp_options *wo = &lcp_wantoptions[f->unit];
627 u_char cilen, citype, cichar, *next;
630 lcp_options no; /* options we've seen Naks for */
631 lcp_options try; /* options to request next time */
634 BZERO(&no, sizeof(no));
638 * Any Nak'd CIs must be in exactly the same order that we sent.
639 * Check packet length and CI length at each step.
640 * If we find any deviations, then this packet is bad.
642 #define NAKCIVOID(opt, neg, code) \
644 len >= CILEN_VOID && \
645 p[1] == CILEN_VOID && \
648 INCPTR(CILEN_VOID, p); \
652 #define NAKCICHAP(opt, neg, code) \
654 len >= CILEN_CHAP && \
655 p[1] == CILEN_CHAP && \
659 GETSHORT(cishort, p); \
660 GETCHAR(cichar, p); \
664 #define NAKCISHORT(opt, neg, code) \
666 len >= CILEN_SHORT && \
667 p[1] == CILEN_SHORT && \
669 len -= CILEN_SHORT; \
671 GETSHORT(cishort, p); \
675 #define NAKCILONG(opt, neg, code) \
677 len >= CILEN_LONG && \
678 p[1] == CILEN_LONG && \
682 GETLONG(cilong, p); \
686 #define NAKCILQR(opt, neg, code) \
688 len >= CILEN_LQR && \
689 p[1] == CILEN_LQR && \
693 GETSHORT(cishort, p); \
694 GETLONG(cilong, p); \
700 * We don't care if they want to send us smaller packets than
701 * we want. Therefore, accept any MRU less than what we asked for,
702 * but then ignore the new value when setting the MRU in the kernel.
703 * If they send us a bigger MRU than what we asked, accept it, up to
704 * the limit of the default MRU we'd get if we didn't negotiate.
706 NAKCISHORT(CI_MRU, neg_mru,
707 if (cishort <= wo->mru || cishort < DEFMRU)
711 * Add any characters they want to our (receive-side) asyncmap.
713 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
714 try.asyncmap = go->asyncmap | cilong;
717 * If they can't cope with our CHAP hash algorithm, we'll have
718 * to stop asking for CHAP. We haven't got any other algorithm.
720 NAKCICHAP(CI_AUTHTYPE, neg_chap,
724 * Peer shouldn't send Nak for UPAP, protocol compression or
725 * address/control compression requests; they should send
726 * a Reject instead. If they send a Nak, treat it as a Reject.
729 NAKCISHORT(CI_AUTHTYPE, neg_upap,
734 * If they can't cope with our link quality protocol, we'll have
735 * to stop asking for LQR. We haven't got any other protocol.
736 * If they Nak the reporting period, take their value XXX ?
738 NAKCILQR(CI_QUALITY, neg_lqr,
739 if (cishort != PPP_LQR)
742 try.lqr_period = cilong;
745 * Check for a looped-back line.
747 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
748 try.magicnumber = magic();
753 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
754 try.neg_pcompression = 0;
756 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
757 try.neg_accompression = 0;
761 * There may be remaining CIs, if the peer is requesting negotiation
762 * on an option that we didn't include in our request packet.
763 * If we see an option that we requested, or one we've already seen
764 * in this packet, then this packet is bad.
765 * If we wanted to respond by starting to negotiate on the requested
766 * option(s), we could, but we don't, because except for the
767 * authentication type and quality protocol, if we are not negotiating
768 * an option, it is because we were told not to.
769 * For the authentication type, the Nak from the peer means
770 * `let me authenticate myself with you' which is a bit pointless.
771 * For the quality protocol, the Nak means `ask me to send you quality
772 * reports', but if we didn't ask for them, we don't want them.
774 while (len > CILEN_VOID) {
777 if( (len -= cilen) < 0 )
779 next = p + cilen - 2;
783 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
787 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
791 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
795 if (go->neg_magicnumber || no.neg_magicnumber ||
799 case CI_PCOMPRESSION:
800 if (go->neg_pcompression || no.neg_pcompression
801 || cilen != CILEN_VOID)
804 case CI_ACCOMPRESSION:
805 if (go->neg_accompression || no.neg_accompression
806 || cilen != CILEN_VOID)
810 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
819 /* If there is still anything left, this packet is bad. */
824 * OK, the Nak is good. Now we can update state.
826 if (f->state != OPENED) {
828 if (looped_back && try.numloops % lcp_warnloops == 0)
829 syslog(LOG_WARNING, "Serial line appears to be looped back.");
835 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
841 * lcp_rejci - Peer has Rejected some of our CIs.
842 * This should not modify any state if the Reject is bad
843 * or if LCP is in the OPENED state.
846 * 0 - Reject was bad.
847 * 1 - Reject was good.
855 lcp_options *go = &lcp_gotoptions[f->unit];
861 lcp_options try; /* options to request next time */
866 * Any Rejected CIs must be in exactly the same order that we sent.
867 * Check packet length and CI length at each step.
868 * If we find any deviations, then this packet is bad.
870 #define REJCIVOID(opt, neg) \
872 len >= CILEN_VOID && \
873 p[1] == CILEN_VOID && \
876 INCPTR(CILEN_VOID, p); \
878 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
880 #define REJCISHORT(opt, neg, val) \
882 len >= CILEN_SHORT && \
883 p[1] == CILEN_SHORT && \
885 len -= CILEN_SHORT; \
887 GETSHORT(cishort, p); \
888 /* Check rejected value. */ \
889 if (cishort != val) \
892 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
894 #define REJCICHAP(opt, neg, val, digest) \
896 len >= CILEN_CHAP && \
897 p[1] == CILEN_CHAP && \
901 GETSHORT(cishort, p); \
902 GETCHAR(cichar, p); \
903 /* Check rejected value. */ \
904 if (cishort != val || cichar != digest) \
907 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
909 #define REJCILONG(opt, neg, val) \
911 len >= CILEN_LONG && \
912 p[1] == CILEN_LONG && \
916 GETLONG(cilong, p); \
917 /* Check rejected value. */ \
921 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
923 #define REJCILQR(opt, neg, val) \
925 len >= CILEN_LQR && \
926 p[1] == CILEN_LQR && \
930 GETSHORT(cishort, p); \
931 GETLONG(cilong, p); \
932 /* Check rejected value. */ \
933 if (cishort != PPP_LQR || cilong != val) \
936 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
939 REJCISHORT(CI_MRU, neg_mru, go->mru);
940 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
941 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
943 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
945 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
946 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
947 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
948 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
951 * If there are any remaining CIs, then this packet is bad.
956 * Now we can update state.
958 if (f->state != OPENED)
963 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
964 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
965 plen, len, p - start));
971 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
973 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
974 * appropriately. If reject_if_disagree is non-zero, doesn't return
975 * CONFNAK; returns CONFREJ if it can't return CONFACK.
978 lcp_reqci(f, inp, lenp, reject_if_disagree)
980 u_char *inp; /* Requested CIs */
981 int *lenp; /* Length of requested CIs */
982 int reject_if_disagree;
984 lcp_options *go = &lcp_gotoptions[f->unit];
985 lcp_options *ho = &lcp_hisoptions[f->unit];
986 lcp_options *ao = &lcp_allowoptions[f->unit];
987 u_char *cip, *next; /* Pointer to current and next CIs */
988 u_char cilen, citype, cichar;/* Parsed len, type, char value */
989 u_short cishort; /* Parsed short value */
990 u_int32_t cilong; /* Parse long value */
991 int rc = CONFACK; /* Final packet return code */
992 int orc; /* Individual option return code */
993 u_char *p; /* Pointer to next char to parse */
994 u_char *ucp = inp; /* Pointer to current output char */
995 int l = *lenp; /* Length left */
998 * Reset all his options.
1000 BZERO(ho, sizeof(*ho));
1003 * Process all his options.
1007 orc = CONFACK; /* Assume success */
1008 cip = p = next; /* Remember begining of CI */
1009 if (l < 2 || /* Not enough data for CI header or */
1010 p[1] < 2 || /* CI length too small or */
1011 p[1] > l) { /* CI length too big? */
1012 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1013 orc = CONFREJ; /* Reject bad CI */
1014 cilen = l; /* Reject till end of packet */
1015 l = 0; /* Don't loop again */
1018 GETCHAR(citype, p); /* Parse CI type */
1019 GETCHAR(cilen, p); /* Parse CI length */
1020 l -= cilen; /* Adjust remaining length */
1021 next += cilen; /* Step to next CI */
1023 switch (citype) { /* Check CI type */
1025 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1026 if (!ao->neg_mru || /* Allow option? */
1027 cilen != CILEN_SHORT) { /* Check CI length */
1028 orc = CONFREJ; /* Reject CI */
1031 GETSHORT(cishort, p); /* Parse MRU */
1032 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1035 * He must be able to receive at least our minimum.
1036 * No need to check a maximum. If he sends a large number,
1037 * we'll just ignore it.
1039 if (cishort < MINMRU) {
1040 orc = CONFNAK; /* Nak CI */
1041 if( !reject_if_disagree ){
1042 DECPTR(sizeof (short), p); /* Backup */
1043 PUTSHORT(MINMRU, p); /* Give him a hint */
1047 ho->neg_mru = 1; /* Remember he sent MRU */
1048 ho->mru = cishort; /* And remember value */
1052 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1053 if (!ao->neg_asyncmap ||
1054 cilen != CILEN_LONG) {
1059 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1062 * Asyncmap must have set at least the bits
1063 * which are set in lcp_allowoptions[unit].asyncmap.
1065 if ((ao->asyncmap & ~cilong) != 0) {
1067 if( !reject_if_disagree ){
1068 DECPTR(sizeof (long), p);
1069 PUTLONG(ao->asyncmap | cilong, p);
1073 ho->neg_asyncmap = 1;
1074 ho->asyncmap = cilong;
1078 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1079 if (cilen < CILEN_SHORT ||
1080 !(ao->neg_upap || ao->neg_chap)) {
1084 GETSHORT(cishort, p);
1085 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1088 * Authtype must be UPAP or CHAP.
1090 * Note: if both ao->neg_upap and ao->neg_chap are set,
1091 * and the peer sends a Configure-Request with two
1092 * authenticate-protocol requests, one for CHAP and one
1093 * for UPAP, then we will reject the second request.
1094 * Whether we end up doing CHAP or UPAP depends then on
1095 * the ordering of the CIs in the peer's Configure-Request.
1098 if (cishort == PPP_PAP) {
1099 if (!ao->neg_upap || /* we don't want to do PAP */
1100 ho->neg_chap || /* or we've already accepted CHAP */
1101 cilen != CILEN_SHORT) {
1102 LCPDEBUG((LOG_WARNING,
1103 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1110 if (cishort == PPP_CHAP) {
1111 if (!ao->neg_chap || /* we don't want to do CHAP */
1112 ho->neg_upap || /* or we've already accepted UPAP */
1113 cilen != CILEN_CHAP) {
1115 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1119 GETCHAR(cichar, p); /* get digest type*/
1120 if (cichar != ao->chap_mdtype) {
1122 if( !reject_if_disagree ){
1123 DECPTR(sizeof (u_char), p);
1124 PUTCHAR(ao->chap_mdtype, p);
1128 ho->chap_mdtype = cichar; /* save md type */
1134 * We don't recognize the protocol they're asking for.
1141 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1143 cilen != CILEN_LQR) {
1148 GETSHORT(cishort, p);
1150 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1151 if (cishort != PPP_LQR) {
1157 * Check the reporting period.
1158 * XXX When should we Nak this, and what with?
1162 case CI_MAGICNUMBER:
1163 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1164 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1165 cilen != CILEN_LONG) {
1170 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1173 * He must have a different magic number.
1175 if (go->neg_magicnumber &&
1176 cilong == go->magicnumber) {
1178 DECPTR(sizeof (long), p);
1179 cilong = magic(); /* Don't put magic() inside macro! */
1183 ho->neg_magicnumber = 1;
1184 ho->magicnumber = cilong;
1188 case CI_PCOMPRESSION:
1189 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1190 if (!ao->neg_pcompression ||
1191 cilen != CILEN_VOID) {
1195 ho->neg_pcompression = 1;
1198 case CI_ACCOMPRESSION:
1199 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1200 if (!ao->neg_accompression ||
1201 cilen != CILEN_VOID) {
1205 ho->neg_accompression = 1;
1209 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1216 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1217 if (orc == CONFACK && /* Good CI */
1218 rc != CONFACK) /* but prior CI wasnt? */
1219 continue; /* Don't send this one */
1221 if (orc == CONFNAK) { /* Nak this CI? */
1222 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1223 orc = CONFREJ; /* Get tough if so */
1225 if (rc == CONFREJ) /* Rejecting prior CI? */
1226 continue; /* Don't send this one */
1227 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1228 rc = CONFNAK; /* Not anymore... */
1229 ucp = inp; /* Backup */
1233 if (orc == CONFREJ && /* Reject this CI */
1234 rc != CONFREJ) { /* but no prior ones? */
1236 ucp = inp; /* Backup */
1238 if (ucp != cip) /* Need to move CI? */
1239 BCOPY(cip, ucp, cilen); /* Move it */
1240 INCPTR(cilen, ucp); /* Update output pointer */
1244 * If we wanted to send additional NAKs (for unsent CIs), the
1245 * code would go here. This must be done with care since it might
1246 * require a longer packet than we received. At present there
1247 * are no cases where we want to ask the peer to negotiate an option.
1250 *lenp = ucp - inp; /* Compute output length */
1251 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1252 return (rc); /* Return final code */
1257 * lcp_up - LCP has come UP.
1259 * Start UPAP, IPCP, etc.
1265 lcp_options *wo = &lcp_wantoptions[f->unit];
1266 lcp_options *ho = &lcp_hisoptions[f->unit];
1267 lcp_options *go = &lcp_gotoptions[f->unit];
1268 lcp_options *ao = &lcp_allowoptions[f->unit];
1270 if (!go->neg_magicnumber)
1271 go->magicnumber = 0;
1272 if (!ho->neg_magicnumber)
1273 ho->magicnumber = 0;
1276 * Set our MTU to the smaller of the MTU we wanted and
1277 * the MRU our peer wanted. If we negotiated an MRU,
1278 * set our MRU to the larger of value we wanted and
1279 * the value we got in the negotiation.
1281 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1282 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1283 ho->neg_pcompression, ho->neg_accompression);
1285 * If the asyncmap hasn't been negotiated, we really should
1286 * set the receive asyncmap to ffffffff, but we set it to 0
1287 * for backwards contemptibility.
1289 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1290 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1291 go->neg_pcompression, go->neg_accompression);
1294 peer_mru[f->unit] = ho->mru;
1296 ChapLowerUp(f->unit); /* Enable CHAP */
1297 upap_lowerup(f->unit); /* Enable UPAP */
1298 ipcp_lowerup(f->unit); /* Enable IPCP */
1299 ccp_lowerup(f->unit); /* Enable CCP */
1300 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1302 link_established(f->unit);
1307 * lcp_down - LCP has gone DOWN.
1309 * Alert other protocols.
1315 lcp_echo_lowerdown(f->unit);
1316 ccp_lowerdown(f->unit);
1317 ipcp_lowerdown(f->unit);
1318 ChapLowerDown(f->unit);
1319 upap_lowerdown(f->unit);
1322 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1323 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1324 peer_mru[f->unit] = PPP_MRU;
1331 * lcp_starting - LCP needs the lower layer up.
1337 link_required(f->unit);
1342 * lcp_finished - LCP has finished with the lower layer.
1348 link_terminated(f->unit);
1353 * lcp_printpkt - print the contents of an LCP packet.
1355 char *lcp_codenames[] = {
1356 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1357 "TermReq", "TermAck", "CodeRej", "ProtRej",
1358 "EchoReq", "EchoRep", "DiscReq"
1362 lcp_printpkt(p, plen, printer, arg)
1365 void (*printer) __P((void *, char *, ...));
1368 int code, id, len, olen;
1369 u_char *pstart, *optend;
1373 if (plen < HEADERLEN)
1379 if (len < HEADERLEN || len > plen)
1382 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1383 printer(arg, " %s", lcp_codenames[code-1]);
1385 printer(arg, " code=0x%x", code);
1386 printer(arg, " id=0x%x", id);
1393 /* print option list */
1398 if (olen < 2 || olen > len) {
1406 if (olen == CILEN_SHORT) {
1408 GETSHORT(cishort, p);
1409 printer(arg, "mru %d", cishort);
1413 if (olen == CILEN_LONG) {
1416 printer(arg, "asyncmap 0x%x", cilong);
1420 if (olen >= CILEN_SHORT) {
1422 printer(arg, "auth ");
1423 GETSHORT(cishort, p);
1426 printer(arg, "upap");
1429 printer(arg, "chap");
1432 printer(arg, "0x%x", cishort);
1437 if (olen >= CILEN_SHORT) {
1439 printer(arg, "quality ");
1440 GETSHORT(cishort, p);
1443 printer(arg, "lqr");
1446 printer(arg, "0x%x", cishort);
1450 case CI_MAGICNUMBER:
1451 if (olen == CILEN_LONG) {
1454 printer(arg, "magic 0x%x", cilong);
1457 case CI_PCOMPRESSION:
1458 if (olen == CILEN_VOID) {
1460 printer(arg, "pcomp");
1463 case CI_ACCOMPRESSION:
1464 if (olen == CILEN_VOID) {
1466 printer(arg, "accomp");
1470 while (p < optend) {
1472 printer(arg, " %.2x", code);
1479 /* print the rest of the bytes in the packet */
1480 for (; len > 0; --len) {
1482 printer(arg, " %.2x", code);
1489 * Time to shut down the link because there is nothing out there.
1493 void LcpLinkFailure (f)
1496 if (f->state == OPENED) {
1497 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1498 lcp_close(f->unit); /* Reset connection */
1503 * Timer expired for the LCP echo requests from this process.
1512 struct ppp_ddinfo ddinfo;
1515 * Read the time since the last packet was received.
1517 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1518 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1522 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1524 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1525 : ddinfo.ip_rjiffies;
1527 * Compute the time since the last packet was received. If the timer
1528 * has expired then send the echo request and reset the timer to maximum.
1530 delta = (lcp_echo_interval * HZ) - latest;
1531 if (delta < HZ || latest < 0L) {
1532 LcpSendEchoRequest (f);
1533 delta = lcp_echo_interval * HZ;
1537 #else /* Other implementations do not have ability to find delta */
1538 LcpSendEchoRequest (f);
1539 delta = lcp_echo_interval;
1543 * Start the timer for the next interval.
1545 assert (lcp_echo_timer_running==0);
1546 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1547 lcp_echo_timer_running = 1;
1551 * LcpEchoTimeout - Timer expired on the LCP echo
1555 LcpEchoTimeout (arg)
1558 if (lcp_echo_timer_running != 0) {
1559 lcp_echo_timer_running = 0;
1560 LcpEchoCheck ((fsm *) arg);
1565 * LcpEchoReply - LCP has received a reply to the echo
1569 lcp_received_echo_reply (f, id, inp, len)
1571 int id; u_char *inp; int len;
1575 /* Check the magic number - don't count replies from ourselves. */
1577 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1580 GETLONG(magic, inp);
1581 if (lcp_gotoptions[f->unit].neg_magicnumber
1582 && magic == lcp_gotoptions[f->unit].magicnumber) {
1583 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1587 /* Reset the number of outstanding echo frames */
1588 lcp_echos_pending = 0;
1592 * LcpSendEchoRequest - Send an echo request frame to the peer
1596 LcpSendEchoRequest (f)
1599 u_int32_t lcp_magic;
1600 u_char pkt[4], *pktp;
1603 * Detect the failure of the peer at this point.
1605 if (lcp_echo_fails != 0) {
1606 if (lcp_echos_pending++ >= lcp_echo_fails) {
1608 lcp_echos_pending = 0;
1612 * Make and send the echo request frame.
1614 if (f->state == OPENED) {
1615 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1616 ? lcp_gotoptions[f->unit].magicnumber
1619 PUTLONG(lcp_magic, pktp);
1621 fsm_sdata(f, ECHOREQ,
1622 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1627 * lcp_echo_lowerup - Start the timer for the LCP frame
1631 lcp_echo_lowerup (unit)
1634 fsm *f = &lcp_fsm[unit];
1636 /* Clear the parameters for generating echo frames */
1637 lcp_echos_pending = 0;
1638 lcp_echo_number = 0;
1639 lcp_echo_timer_running = 0;
1641 /* If a timeout interval is specified then start the timer */
1642 if (lcp_echo_interval != 0)
1647 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1651 lcp_echo_lowerdown (unit)
1654 fsm *f = &lcp_fsm[unit];
1656 if (lcp_echo_timer_running != 0) {
1657 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1658 lcp_echo_timer_running = 0;