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.13 1994/09/16 02:15:37 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
47 #ifdef _linux_ /* Needs ppp ioctls */
48 #include <linux/ppp.h>
52 fsm lcp_fsm[NPPP]; /* LCP fsm structure (global)*/
53 lcp_options lcp_wantoptions[NPPP]; /* Options that we want to request */
54 lcp_options lcp_gotoptions[NPPP]; /* Options that peer ack'd */
55 lcp_options lcp_allowoptions[NPPP]; /* Options we allow peer to request */
56 lcp_options lcp_hisoptions[NPPP]; /* Options that we ack'd */
57 uint32 xmit_accm[NPPP][8]; /* extended transmit ACCM */
59 static uint32 lcp_echos_pending = 0; /* Number of outstanding echo msgs */
60 static uint32 lcp_echo_number = 0; /* ID number of next echo frame */
61 static uint32 lcp_echo_timer_running = 0; /* TRUE if a timer is running */
64 * Callbacks for fsm code. (CI = Configuration Information)
66 static void lcp_resetci __ARGS((fsm *)); /* Reset our CI */
67 static int lcp_cilen __ARGS((fsm *)); /* Return length of our CI */
68 static void lcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI to pkt */
69 static int lcp_ackci __ARGS((fsm *, u_char *, int)); /* Peer ack'd our CI */
70 static int lcp_nakci __ARGS((fsm *, u_char *, int)); /* Peer nak'd our CI */
71 static int lcp_rejci __ARGS((fsm *, u_char *, int)); /* Peer rej'd our CI */
72 static int lcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv peer CI */
73 static void lcp_up __ARGS((fsm *)); /* We're UP */
74 static void lcp_down __ARGS((fsm *)); /* We're DOWN */
75 static void lcp_starting __ARGS((fsm *)); /* We need lower layer up */
76 static void lcp_finished __ARGS((fsm *)); /* We need lower layer down */
77 static int lcp_extcode __ARGS((fsm *, int, int, u_char *, int));
78 static void lcp_rprotrej __ARGS((fsm *, u_char *, int));
81 * routines to send LCP echos to peer
84 static void lcp_echo_lowerup __ARGS((int));
85 static void lcp_echo_lowerdown __ARGS((int));
86 static void LcpEchoTimeout __ARGS((caddr_t));
87 static void lcp_received_echo_reply __ARGS((fsm *, int, u_char *, int));
88 static void LcpSendEchoRequest __ARGS((fsm *));
89 static void LcpLinkFailure __ARGS((fsm *));
91 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
92 lcp_resetci, /* Reset our Configuration Information */
93 lcp_cilen, /* Length of our Configuration Information */
94 lcp_addci, /* Add our Configuration Information */
95 lcp_ackci, /* ACK our Configuration Information */
96 lcp_nakci, /* NAK our Configuration Information */
97 lcp_rejci, /* Reject our Configuration Information */
98 lcp_reqci, /* Request peer's Configuration Information */
99 lcp_up, /* Called when fsm reaches OPENED state */
100 lcp_down, /* Called when fsm leaves OPENED state */
101 lcp_starting, /* Called when we want the lower layer up */
102 lcp_finished, /* Called when we want the lower layer down */
103 NULL, /* Called when Protocol-Reject received */
104 NULL, /* Retransmission is necessary */
105 lcp_extcode, /* Called to handle LCP-specific codes */
106 "LCP" /* String name of protocol */
109 int lcp_warnloops = DEFWARNLOOPS; /* Warn about a loopback this often */
112 * Length of each type of configuration option (in octets)
115 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
116 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
117 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
118 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
120 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
121 (x) == CONFNAK ? "NAK" : "REJ")
125 * lcp_init - Initialize LCP.
131 fsm *f = &lcp_fsm[unit];
132 lcp_options *wo = &lcp_wantoptions[unit];
133 lcp_options *ao = &lcp_allowoptions[unit];
137 f->callbacks = &lcp_callbacks;
143 wo->restart = 0; /* Set to 1 in kernels or multi-line
147 wo->neg_asyncmap = 0;
149 wo->neg_chap = 0; /* Set to 1 on server */
150 wo->neg_upap = 0; /* Set to 1 on server */
151 wo->chap_mdtype = CHAP_DIGEST_MD5;
152 wo->neg_magicnumber = 1;
153 wo->neg_pcompression = 1;
154 wo->neg_accompression = 1;
155 wo->neg_lqr = 0; /* no LQR implementation yet */
159 ao->neg_asyncmap = 1;
162 ao->chap_mdtype = CHAP_DIGEST_MD5;
164 ao->neg_magicnumber = 1;
165 ao->neg_pcompression = 1;
166 ao->neg_accompression = 1;
167 ao->neg_lqr = 0; /* no LQR implementation yet */
169 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
170 xmit_accm[unit][3] = 0x60000000;
175 * lcp_open - LCP is allowed to come up.
181 fsm *f = &lcp_fsm[unit];
182 lcp_options *wo = &lcp_wantoptions[unit];
186 f->flags |= OPT_PASSIVE;
188 f->flags |= OPT_SILENT;
194 * lcp_close - Take LCP down.
200 fsm *f = &lcp_fsm[unit];
202 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
204 * This action is not strictly according to the FSM in RFC1548,
205 * but it does mean that the program terminates if you do a
206 * lcp_close(0) in passive/silent mode when a connection hasn't
213 fsm_close(&lcp_fsm[unit]);
218 * lcp_lowerup - The lower layer is up.
225 ppp_set_xaccm(unit, xmit_accm[unit]);
226 ppp_send_config(unit, MTU, 0xffffffff, 0, 0);
227 ppp_recv_config(unit, MTU, 0x00000000, 0, 0);
228 peer_mru[unit] = MTU;
229 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
231 fsm_lowerup(&lcp_fsm[unit]);
236 * lcp_lowerdown - The lower layer is down.
242 fsm_lowerdown(&lcp_fsm[unit]);
247 * lcp_input - Input LCP packet.
250 lcp_input(unit, p, len)
256 fsm *f = &lcp_fsm[unit];
257 lcp_options *go = &lcp_gotoptions[f->unit];
260 fsm_input(f, p, len);
261 if (oldstate == REQSENT && f->state == ACKSENT) {
263 * The peer will probably send us an ack soon and then
264 * immediately start sending packets with the negotiated
265 * options. So as to be ready when that happens, we set
266 * our receive side to accept packets as negotiated now.
268 ppp_recv_config(f->unit, MTU,
269 go->neg_asyncmap? go->asyncmap: 0x00000000,
270 go->neg_pcompression, go->neg_accompression);
276 * lcp_extcode - Handle a LCP-specific code.
279 lcp_extcode(f, code, id, inp, len)
289 lcp_rprotrej(f, inp, len);
293 if (f->state != OPENED)
295 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
297 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
298 fsm_sdata(f, ECHOREP, id, inp, len);
302 lcp_received_echo_reply(f, id, inp, len);
316 * lcp_rprotrej - Receive an Protocol-Reject.
318 * Figure out which protocol is rejected and inform it.
321 lcp_rprotrej(f, inp, len)
328 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
330 if (len < sizeof (u_short)) {
332 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
339 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
343 * Protocol-Reject packets received in any state other than the LCP
344 * OPENED state SHOULD be silently discarded.
346 if( f->state != OPENED ){
347 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
352 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
357 * lcp_protrej - A Protocol-Reject was received.
367 LCPDEBUG((LOG_WARNING,
368 "lcp_protrej: Received Protocol-Reject for LCP!"));
369 fsm_protreject(&lcp_fsm[unit]);
374 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
377 lcp_sprotrej(unit, p, len)
383 * Send back the protocol and the information field of the
384 * rejected packet. We only get here if LCP is in the OPENED state.
389 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
395 * lcp_resetci - Reset our CI.
401 lcp_wantoptions[f->unit].magicnumber = magic();
402 lcp_wantoptions[f->unit].numloops = 0;
403 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
404 peer_mru[f->unit] = MTU;
409 * lcp_cilen - Return length of our CI.
415 lcp_options *go = &lcp_gotoptions[f->unit];
417 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
418 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
419 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
420 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
421 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
423 * NB: we only ask for one of CHAP and UPAP, even if we will
426 return (LENCISHORT(go->neg_mru) +
427 LENCILONG(go->neg_asyncmap) +
428 LENCICHAP(go->neg_chap) +
429 LENCISHORT(!go->neg_chap && go->neg_upap) +
430 LENCILQR(go->neg_lqr) +
431 LENCILONG(go->neg_magicnumber) +
432 LENCIVOID(go->neg_pcompression) +
433 LENCIVOID(go->neg_accompression));
438 * lcp_addci - Add our desired CIs to a packet.
441 lcp_addci(f, ucp, lenp)
446 lcp_options *go = &lcp_gotoptions[f->unit];
447 u_char *start_ucp = ucp;
449 #define ADDCIVOID(opt, neg) \
452 PUTCHAR(CILEN_VOID, ucp); \
454 #define ADDCISHORT(opt, neg, val) \
457 PUTCHAR(CILEN_SHORT, ucp); \
458 PUTSHORT(val, ucp); \
460 #define ADDCICHAP(opt, neg, val, digest) \
463 PUTCHAR(CILEN_CHAP, ucp); \
464 PUTSHORT(val, ucp); \
465 PUTCHAR(digest, ucp); \
467 #define ADDCILONG(opt, neg, val) \
470 PUTCHAR(CILEN_LONG, ucp); \
473 #define ADDCILQR(opt, neg, val) \
476 PUTCHAR(CILEN_LQR, ucp); \
477 PUTSHORT(LQR, ucp); \
481 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
482 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
483 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
484 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
485 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
486 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
487 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
488 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
490 if (ucp - start_ucp != *lenp) {
491 /* this should never happen, because peer_mtu should be 1500 */
492 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
498 * lcp_ackci - Ack our CIs.
499 * This should not modify any state if the Ack is bad.
511 lcp_options *go = &lcp_gotoptions[f->unit];
512 u_char cilen, citype, cichar;
517 * CIs must be in exactly the same order that we sent.
518 * Check packet length and CI length at each step.
519 * If we find any deviations, then this packet is bad.
521 #define ACKCIVOID(opt, neg) \
523 if ((len -= CILEN_VOID) < 0) \
525 GETCHAR(citype, p); \
527 if (cilen != CILEN_VOID || \
531 #define ACKCISHORT(opt, neg, val) \
533 if ((len -= CILEN_SHORT) < 0) \
535 GETCHAR(citype, p); \
537 if (cilen != CILEN_SHORT || \
540 GETSHORT(cishort, p); \
541 if (cishort != val) \
544 #define ACKCICHAP(opt, neg, val, digest) \
546 if ((len -= CILEN_CHAP) < 0) \
548 GETCHAR(citype, p); \
550 if (cilen != CILEN_CHAP || \
553 GETSHORT(cishort, p); \
554 if (cishort != val) \
556 GETCHAR(cichar, p); \
557 if (cichar != digest) \
560 #define ACKCILONG(opt, neg, val) \
562 if ((len -= CILEN_LONG) < 0) \
564 GETCHAR(citype, p); \
566 if (cilen != CILEN_LONG || \
569 GETLONG(cilong, p); \
573 #define ACKCILQR(opt, neg, val) \
575 if ((len -= CILEN_LQR) < 0) \
577 GETCHAR(citype, p); \
579 if (cilen != CILEN_LQR || \
582 GETSHORT(cishort, p); \
583 if (cishort != LQR) \
585 GETLONG(cilong, p); \
590 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
591 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
592 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, CHAP, go->chap_mdtype);
593 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, UPAP);
594 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
595 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
596 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
597 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
600 * If there are any remaining CIs, then this packet is bad.
606 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
612 * lcp_nakci - Peer has sent a NAK for some of our CIs.
613 * This should not modify any state if the Nak is bad
614 * or if LCP is in the OPENED state.
626 lcp_options *go = &lcp_gotoptions[f->unit];
627 lcp_options *wo = &lcp_wantoptions[f->unit];
628 u_char cilen, citype, cichar, *next;
631 lcp_options no; /* options we've seen Naks for */
632 lcp_options try; /* options to request next time */
635 BZERO(&no, sizeof(no));
639 * Any Nak'd CIs must be in exactly the same order that we sent.
640 * Check packet length and CI length at each step.
641 * If we find any deviations, then this packet is bad.
643 #define NAKCIVOID(opt, neg, code) \
645 len >= CILEN_VOID && \
646 p[1] == CILEN_VOID && \
649 INCPTR(CILEN_VOID, p); \
653 #define NAKCICHAP(opt, neg, code) \
655 len >= CILEN_CHAP && \
656 p[1] == CILEN_CHAP && \
660 GETSHORT(cishort, p); \
661 GETCHAR(cichar, p); \
665 #define NAKCISHORT(opt, neg, code) \
667 len >= CILEN_SHORT && \
668 p[1] == CILEN_SHORT && \
670 len -= CILEN_SHORT; \
672 GETSHORT(cishort, p); \
676 #define NAKCILONG(opt, neg, code) \
678 len >= CILEN_LONG && \
679 p[1] == CILEN_LONG && \
683 GETLONG(cilong, p); \
687 #define NAKCILQR(opt, neg, code) \
689 len >= CILEN_LQR && \
690 p[1] == CILEN_LQR && \
694 GETSHORT(cishort, p); \
695 GETLONG(cilong, p); \
701 * We don't care if they want to send us smaller packets than
702 * we want. Therefore, accept any MRU less than what we asked for,
703 * but then ignore the new value when setting the MRU in the kernel.
704 * If they send us a bigger MRU than what we asked, accept it, up to
705 * the limit of the default MRU we'd get if we didn't negotiate.
707 NAKCISHORT(CI_MRU, neg_mru,
708 if (cishort <= wo->mru || cishort < DEFMRU)
712 * Add any characters they want to our (receive-side) asyncmap.
714 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
715 try.asyncmap = go->asyncmap | cilong;
718 * If they can't cope with our CHAP hash algorithm, we'll have
719 * to stop asking for CHAP. We haven't got any other algorithm.
721 NAKCICHAP(CI_AUTHTYPE, neg_chap,
725 * Peer shouldn't send Nak for UPAP, protocol compression or
726 * address/control compression requests; they should send
727 * a Reject instead. If they send a Nak, treat it as a Reject.
730 NAKCISHORT(CI_AUTHTYPE, neg_upap,
735 * If they can't cope with our link quality protocol, we'll have
736 * to stop asking for LQR. We haven't got any other protocol.
737 * If they Nak the reporting period, take their value XXX ?
739 NAKCILQR(CI_QUALITY, neg_lqr,
743 try.lqr_period = cilong;
746 * Check for a looped-back line.
748 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
749 try.magicnumber = magic();
754 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
755 try.neg_pcompression = 0;
757 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
758 try.neg_accompression = 0;
762 * There may be remaining CIs, if the peer is requesting negotiation
763 * on an option that we didn't include in our request packet.
764 * If we see an option that we requested, or one we've already seen
765 * in this packet, then this packet is bad.
766 * If we wanted to respond by starting to negotiate on the requested
767 * option(s), we could, but we don't, because except for the
768 * authentication type and quality protocol, if we are not negotiating
769 * an option, it is because we were told not to.
770 * For the authentication type, the Nak from the peer means
771 * `let me authenticate myself with you' which is a bit pointless.
772 * For the quality protocol, the Nak means `ask me to send you quality
773 * reports', but if we didn't ask for them, we don't want them.
775 while (len > CILEN_VOID) {
778 if( (len -= cilen) < 0 )
780 next = p + cilen - 2;
784 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
788 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
792 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
796 if (go->neg_magicnumber || no.neg_magicnumber ||
800 case CI_PCOMPRESSION:
801 if (go->neg_pcompression || no.neg_pcompression
802 || cilen != CILEN_VOID)
805 case CI_ACCOMPRESSION:
806 if (go->neg_accompression || no.neg_accompression
807 || cilen != CILEN_VOID)
811 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
820 /* If there is still anything left, this packet is bad. */
825 * OK, the Nak is good. Now we can update state.
827 if (f->state != OPENED) {
829 if (looped_back && try.numloops % lcp_warnloops == 0)
830 syslog(LOG_WARNING, "Serial line appears to be looped back.");
836 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
842 * lcp_rejci - Peer has Rejected some of our CIs.
843 * This should not modify any state if the Reject is bad
844 * or if LCP is in the OPENED state.
847 * 0 - Reject was bad.
848 * 1 - Reject was good.
856 lcp_options *go = &lcp_gotoptions[f->unit];
862 lcp_options try; /* options to request next time */
867 * Any Rejected CIs must be in exactly the same order that we sent.
868 * Check packet length and CI length at each step.
869 * If we find any deviations, then this packet is bad.
871 #define REJCIVOID(opt, neg) \
873 len >= CILEN_VOID && \
874 p[1] == CILEN_VOID && \
877 INCPTR(CILEN_VOID, p); \
879 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
881 #define REJCISHORT(opt, neg, val) \
883 len >= CILEN_SHORT && \
884 p[1] == CILEN_SHORT && \
886 len -= CILEN_SHORT; \
888 GETSHORT(cishort, p); \
889 /* Check rejected value. */ \
890 if (cishort != val) \
893 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
895 #define REJCICHAP(opt, neg, val, digest) \
897 len >= CILEN_CHAP && \
898 p[1] == CILEN_CHAP && \
902 GETSHORT(cishort, p); \
903 GETCHAR(cichar, p); \
904 /* Check rejected value. */ \
905 if (cishort != val || cichar != digest) \
908 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
910 #define REJCILONG(opt, neg, val) \
912 len >= CILEN_LONG && \
913 p[1] == CILEN_LONG && \
917 GETLONG(cilong, p); \
918 /* Check rejected value. */ \
922 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
924 #define REJCILQR(opt, neg, val) \
926 len >= CILEN_LQR && \
927 p[1] == CILEN_LQR && \
931 GETSHORT(cishort, p); \
932 GETLONG(cilong, p); \
933 /* Check rejected value. */ \
934 if (cishort != LQR || cilong != val) \
937 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
940 REJCISHORT(CI_MRU, neg_mru, go->mru);
941 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
942 REJCICHAP(CI_AUTHTYPE, neg_chap, CHAP, go->chap_mdtype);
944 REJCISHORT(CI_AUTHTYPE, neg_upap, UPAP);
946 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
947 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
948 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
949 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
952 * If there are any remaining CIs, then this packet is bad.
957 * Now we can update state.
959 if (f->state != OPENED)
964 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
965 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
966 plen, len, p - start));
972 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
974 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
975 * appropriately. If reject_if_disagree is non-zero, doesn't return
976 * CONFNAK; returns CONFREJ if it can't return CONFACK.
979 lcp_reqci(f, inp, lenp, reject_if_disagree)
981 u_char *inp; /* Requested CIs */
982 int *lenp; /* Length of requested CIs */
983 int reject_if_disagree;
985 lcp_options *go = &lcp_gotoptions[f->unit];
986 lcp_options *ho = &lcp_hisoptions[f->unit];
987 lcp_options *ao = &lcp_allowoptions[f->unit];
988 u_char *cip, *next; /* Pointer to current and next CIs */
989 u_char cilen, citype, cichar;/* Parsed len, type, char value */
990 u_short cishort; /* Parsed short value */
991 uint32 cilong; /* Parse long value */
992 int rc = CONFACK; /* Final packet return code */
993 int orc; /* Individual option return code */
994 u_char *p; /* Pointer to next char to parse */
995 u_char *ucp = inp; /* Pointer to current output char */
996 int l = *lenp; /* Length left */
999 * Reset all his options.
1001 BZERO(ho, sizeof(*ho));
1004 * Process all his options.
1008 orc = CONFACK; /* Assume success */
1009 cip = p = next; /* Remember begining of CI */
1010 if (l < 2 || /* Not enough data for CI header or */
1011 p[1] < 2 || /* CI length too small or */
1012 p[1] > l) { /* CI length too big? */
1013 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1014 orc = CONFREJ; /* Reject bad CI */
1015 cilen = l; /* Reject till end of packet */
1016 l = 0; /* Don't loop again */
1019 GETCHAR(citype, p); /* Parse CI type */
1020 GETCHAR(cilen, p); /* Parse CI length */
1021 l -= cilen; /* Adjust remaining length */
1022 next += cilen; /* Step to next CI */
1024 switch (citype) { /* Check CI type */
1026 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1027 if (!ao->neg_mru || /* Allow option? */
1028 cilen != CILEN_SHORT) { /* Check CI length */
1029 orc = CONFREJ; /* Reject CI */
1032 GETSHORT(cishort, p); /* Parse MRU */
1033 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1036 * He must be able to receive at least our minimum.
1037 * No need to check a maximum. If he sends a large number,
1038 * we'll just ignore it.
1040 if (cishort < MINMRU) {
1041 orc = CONFNAK; /* Nak CI */
1042 if( !reject_if_disagree ){
1043 DECPTR(sizeof (short), p); /* Backup */
1044 PUTSHORT(MINMRU, p); /* Give him a hint */
1048 ho->neg_mru = 1; /* Remember he sent MRU */
1049 ho->mru = cishort; /* And remember value */
1053 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1054 if (!ao->neg_asyncmap ||
1055 cilen != CILEN_LONG) {
1060 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1063 * Asyncmap must have set at least the bits
1064 * which are set in lcp_allowoptions[unit].asyncmap.
1066 if ((ao->asyncmap & ~cilong) != 0) {
1068 if( !reject_if_disagree ){
1069 DECPTR(sizeof (long), p);
1070 PUTLONG(ao->asyncmap | cilong, p);
1074 ho->neg_asyncmap = 1;
1075 ho->asyncmap = cilong;
1079 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1080 if (cilen < CILEN_SHORT ||
1081 !(ao->neg_upap || ao->neg_chap)) {
1085 GETSHORT(cishort, p);
1086 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1089 * Authtype must be UPAP or CHAP.
1091 * Note: if both ao->neg_upap and ao->neg_chap are set,
1092 * and the peer sends a Configure-Request with two
1093 * authenticate-protocol requests, one for CHAP and one
1094 * for UPAP, then we will reject the second request.
1095 * Whether we end up doing CHAP or UPAP depends then on
1096 * the ordering of the CIs in the peer's Configure-Request.
1099 if (cishort == UPAP) {
1100 if (!ao->neg_upap || /* we don't want to do PAP */
1101 ho->neg_chap || /* or we've already accepted CHAP */
1102 cilen != CILEN_SHORT) {
1103 LCPDEBUG((LOG_WARNING,
1104 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1111 if (cishort == CHAP) {
1112 if (!ao->neg_chap || /* we don't want to do CHAP */
1113 ho->neg_upap || /* or we've already accepted UPAP */
1114 cilen != CILEN_CHAP) {
1116 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1120 GETCHAR(cichar, p); /* get digest type*/
1121 if (cichar != ao->chap_mdtype) {
1123 if( !reject_if_disagree ){
1124 DECPTR(sizeof (u_char), p);
1125 PUTCHAR(ao->chap_mdtype, p);
1129 ho->chap_mdtype = cichar; /* save md type */
1135 * We don't recognize the protocol they're asking for.
1142 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1144 cilen != CILEN_LQR) {
1149 GETSHORT(cishort, p);
1151 LCPDEBUG((LOG_INFO, "(%x %lx)", cishort, cilong));
1152 if (cishort != LQR) {
1158 * Check the reporting period.
1159 * XXX When should we Nak this, and what with?
1163 case CI_MAGICNUMBER:
1164 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1165 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1166 cilen != CILEN_LONG) {
1171 LCPDEBUG((LOG_INFO, "(%lx)", cilong));
1174 * He must have a different magic number.
1176 if (go->neg_magicnumber &&
1177 cilong == go->magicnumber) {
1179 DECPTR(sizeof (long), p);
1180 cilong = magic(); /* Don't put magic() inside macro! */
1184 ho->neg_magicnumber = 1;
1185 ho->magicnumber = cilong;
1189 case CI_PCOMPRESSION:
1190 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1191 if (!ao->neg_pcompression ||
1192 cilen != CILEN_VOID) {
1196 ho->neg_pcompression = 1;
1199 case CI_ACCOMPRESSION:
1200 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1201 if (!ao->neg_accompression ||
1202 cilen != CILEN_VOID) {
1206 ho->neg_accompression = 1;
1210 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1217 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1218 if (orc == CONFACK && /* Good CI */
1219 rc != CONFACK) /* but prior CI wasnt? */
1220 continue; /* Don't send this one */
1222 if (orc == CONFNAK) { /* Nak this CI? */
1223 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1224 orc = CONFREJ; /* Get tough if so */
1226 if (rc == CONFREJ) /* Rejecting prior CI? */
1227 continue; /* Don't send this one */
1228 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1229 rc = CONFNAK; /* Not anymore... */
1230 ucp = inp; /* Backup */
1234 if (orc == CONFREJ && /* Reject this CI */
1235 rc != CONFREJ) { /* but no prior ones? */
1237 ucp = inp; /* Backup */
1239 if (ucp != cip) /* Need to move CI? */
1240 BCOPY(cip, ucp, cilen); /* Move it */
1241 INCPTR(cilen, ucp); /* Update output pointer */
1245 * If we wanted to send additional NAKs (for unsent CIs), the
1246 * code would go here. This must be done with care since it might
1247 * require a longer packet than we received. At present there
1248 * are no cases where we want to ask the peer to negotiate an option.
1251 *lenp = ucp - inp; /* Compute output length */
1252 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1253 return (rc); /* Return final code */
1258 * lcp_up - LCP has come UP.
1260 * Start UPAP, IPCP, etc.
1266 lcp_options *wo = &lcp_wantoptions[f->unit];
1267 lcp_options *ho = &lcp_hisoptions[f->unit];
1268 lcp_options *go = &lcp_gotoptions[f->unit];
1269 lcp_options *ao = &lcp_allowoptions[f->unit];
1271 if (!go->neg_magicnumber)
1272 go->magicnumber = 0;
1273 if (!ho->neg_magicnumber)
1274 ho->magicnumber = 0;
1277 * Set our MTU to the smaller of the MTU we wanted and
1278 * the MRU our peer wanted. If we negotiated an MRU,
1279 * set our MRU to the larger of value we wanted and
1280 * the value we got in the negotiation.
1282 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: MTU)),
1283 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1284 ho->neg_pcompression, ho->neg_accompression);
1286 * If the asyncmap hasn't been negotiated, we really should
1287 * set the receive asyncmap to ffffffff, but we set it to 0
1288 * for backwards contemptibility.
1290 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): MTU),
1291 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1292 go->neg_pcompression, go->neg_accompression);
1295 peer_mru[f->unit] = ho->mru;
1297 ChapLowerUp(f->unit); /* Enable CHAP */
1298 upap_lowerup(f->unit); /* Enable UPAP */
1299 ipcp_lowerup(f->unit); /* Enable IPCP */
1300 ccp_lowerup(f->unit); /* Enable CCP */
1301 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1303 link_established(f->unit);
1308 * lcp_down - LCP has gone DOWN.
1310 * Alert other protocols.
1316 lcp_echo_lowerdown(f->unit);
1317 ccp_lowerdown(f->unit);
1318 ipcp_lowerdown(f->unit);
1319 ChapLowerDown(f->unit);
1320 upap_lowerdown(f->unit);
1323 ppp_send_config(f->unit, MTU, 0xffffffff, 0, 0);
1324 ppp_recv_config(f->unit, MTU, 0x00000000, 0, 0);
1325 peer_mru[f->unit] = MTU;
1332 * lcp_starting - LCP needs the lower layer up.
1338 link_required(f->unit);
1343 * lcp_finished - LCP has finished with the lower layer.
1349 link_terminated(f->unit);
1354 * lcp_printpkt - print the contents of an LCP packet.
1356 char *lcp_codenames[] = {
1357 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1358 "TermReq", "TermAck", "CodeRej", "ProtRej",
1359 "EchoReq", "EchoRep", "DiscReq"
1363 lcp_printpkt(p, plen, printer, arg)
1366 void (*printer) __ARGS((void *, char *, ...));
1369 int code, id, len, olen;
1370 u_char *pstart, *optend;
1374 if (plen < HEADERLEN)
1380 if (len < HEADERLEN || len > plen)
1383 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1384 printer(arg, " %s", lcp_codenames[code-1]);
1386 printer(arg, " code=0x%x", code);
1387 printer(arg, " id=0x%x", id);
1394 /* print option list */
1399 if (olen < 2 || olen > len) {
1407 if (olen == CILEN_SHORT) {
1409 GETSHORT(cishort, p);
1410 printer(arg, "mru %d", cishort);
1414 if (olen == CILEN_LONG) {
1417 printer(arg, "asyncmap 0x%x", cilong);
1421 if (olen >= CILEN_SHORT) {
1423 printer(arg, "auth ");
1424 GETSHORT(cishort, p);
1427 printer(arg, "upap");
1430 printer(arg, "chap");
1433 printer(arg, "0x%x", cishort);
1438 if (olen >= CILEN_SHORT) {
1440 printer(arg, "quality ");
1441 GETSHORT(cishort, p);
1444 printer(arg, "lqr");
1447 printer(arg, "0x%x", cishort);
1451 case CI_MAGICNUMBER:
1452 if (olen == CILEN_LONG) {
1455 printer(arg, "magic 0x%x", cilong);
1458 case CI_PCOMPRESSION:
1459 if (olen == CILEN_VOID) {
1461 printer(arg, "pcomp");
1464 case CI_ACCOMPRESSION:
1465 if (olen == CILEN_VOID) {
1467 printer(arg, "accomp");
1471 while (p < optend) {
1473 printer(arg, " %.2x", code);
1480 /* print the rest of the bytes in the packet */
1481 for (; len > 0; --len) {
1483 printer(arg, " %.2x", code);
1490 * Time to shut down the link because there is nothing out there.
1494 void LcpLinkFailure (f)
1497 if (f->state == OPENED) {
1498 syslog (LOG_NOTICE, "Excessive lack of response to LCP echo frames.");
1499 lcp_close(f->unit); /* Reset connection */
1504 * Timer expired for the LCP echo requests from this process.
1513 struct ppp_ddinfo ddinfo;
1516 * Read the time since the last packet was received.
1518 if (ioctl (fd, PPPIOCGTIME, &ddinfo) < 0) {
1519 syslog (LOG_ERR, "ioctl(PPPIOCGTIME): %m");
1523 * Choose the most recient frame received. It may be an IP or NON-IP frame.
1525 latest = ddinfo.nip_rjiffies < ddinfo.ip_rjiffies ? ddinfo.nip_rjiffies
1526 : ddinfo.ip_rjiffies;
1528 * Compute the time since the last packet was received. If the timer
1529 * has expired then send the echo request and reset the timer to maximum.
1531 delta = (lcp_echo_interval * HZ) - latest;
1532 if (delta < HZ || latest < 0L) {
1533 LcpSendEchoRequest (f);
1534 delta = lcp_echo_interval * HZ;
1538 #else /* Other implementations do not have ability to find delta */
1539 LcpSendEchoRequest (f);
1540 delta = lcp_echo_interval;
1544 * Start the timer for the next interval.
1546 assert (lcp_echo_timer_running==0);
1547 TIMEOUT (LcpEchoTimeout, (caddr_t) f, delta);
1548 lcp_echo_timer_running = 1;
1552 * LcpEchoTimeout - Timer expired on the LCP echo
1556 LcpEchoTimeout (arg)
1559 if (lcp_echo_timer_running != 0) {
1560 lcp_echo_timer_running = 0;
1561 LcpEchoCheck ((fsm *) arg);
1566 * LcpEchoReply - LCP has received a reply to the echo
1570 lcp_received_echo_reply (f, id, inp, len)
1572 int id; u_char *inp; int len;
1576 /* Check the magic number - don't count replies from ourselves. */
1578 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1581 GETLONG(magic, inp);
1582 if (lcp_gotoptions[f->unit].neg_magicnumber
1583 && magic == lcp_gotoptions[f->unit].magicnumber) {
1584 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1588 /* Reset the number of outstanding echo frames */
1589 lcp_echos_pending = 0;
1593 * LcpSendEchoRequest - Send an echo request frame to the peer
1597 LcpSendEchoRequest (f)
1601 u_char pkt[4], *pktp;
1604 * Detect the failure of the peer at this point.
1606 if (lcp_echo_fails != 0) {
1607 if (lcp_echos_pending++ >= lcp_echo_fails) {
1609 lcp_echos_pending = 0;
1613 * Make and send the echo request frame.
1615 if (f->state == OPENED) {
1616 lcp_magic = lcp_gotoptions[f->unit].neg_magicnumber
1617 ? lcp_gotoptions[f->unit].magicnumber
1620 PUTLONG(lcp_magic, pktp);
1622 fsm_sdata(f, ECHOREQ,
1623 lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1628 * lcp_echo_lowerup - Start the timer for the LCP frame
1632 lcp_echo_lowerup (unit)
1635 fsm *f = &lcp_fsm[unit];
1637 /* Clear the parameters for generating echo frames */
1638 lcp_echos_pending = 0;
1639 lcp_echo_number = 0;
1640 lcp_echo_timer_running = 0;
1642 /* If a timeout interval is specified then start the timer */
1643 if (lcp_echo_interval != 0)
1648 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1652 lcp_echo_lowerdown (unit)
1655 fsm *f = &lcp_fsm[unit];
1657 if (lcp_echo_timer_running != 0) {
1658 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1659 lcp_echo_timer_running = 0;