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.19.2.1 1995/06/01 07:01:27 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
47 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
48 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
49 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
50 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
51 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
52 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
54 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
55 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
56 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
58 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
61 * Callbacks for fsm code. (CI = Configuration Information)
63 static void lcp_resetci __P((fsm *)); /* Reset our CI */
64 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
65 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
66 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
67 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
68 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
69 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
70 static void lcp_up __P((fsm *)); /* We're UP */
71 static void lcp_down __P((fsm *)); /* We're DOWN */
72 static void lcp_starting __P((fsm *)); /* We need lower layer up */
73 static void lcp_finished __P((fsm *)); /* We need lower layer down */
74 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
75 static void lcp_rprotrej __P((fsm *, u_char *, int));
78 * routines to send LCP echos to peer
81 static void lcp_echo_lowerup __P((int));
82 static void lcp_echo_lowerdown __P((int));
83 static void LcpEchoTimeout __P((caddr_t));
84 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
85 static void LcpSendEchoRequest __P((fsm *));
86 static void LcpLinkFailure __P((fsm *));
88 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
89 lcp_resetci, /* Reset our Configuration Information */
90 lcp_cilen, /* Length of our Configuration Information */
91 lcp_addci, /* Add our Configuration Information */
92 lcp_ackci, /* ACK our Configuration Information */
93 lcp_nakci, /* NAK our Configuration Information */
94 lcp_rejci, /* Reject our Configuration Information */
95 lcp_reqci, /* Request peer's Configuration Information */
96 lcp_up, /* Called when fsm reaches OPENED state */
97 lcp_down, /* Called when fsm leaves OPENED state */
98 lcp_starting, /* Called when we want the lower layer up */
99 lcp_finished, /* Called when we want the lower layer down */
100 NULL, /* Called when Protocol-Reject received */
101 NULL, /* Retransmission is necessary */
102 lcp_extcode, /* Called to handle LCP-specific codes */
103 "LCP" /* String name of protocol */
106 int lcp_loopbackfail = DEFLOOPBACKFAIL;
109 * Length of each type of configuration option (in octets)
112 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
113 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
114 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
115 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
117 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
118 (x) == CONFNAK ? "NAK" : "REJ")
122 * lcp_init - Initialize LCP.
128 fsm *f = &lcp_fsm[unit];
129 lcp_options *wo = &lcp_wantoptions[unit];
130 lcp_options *ao = &lcp_allowoptions[unit];
133 f->protocol = PPP_LCP;
134 f->callbacks = &lcp_callbacks;
140 wo->restart = 0; /* Set to 1 in kernels or multi-line
144 wo->neg_asyncmap = 0;
146 wo->neg_chap = 0; /* Set to 1 on server */
147 wo->neg_upap = 0; /* Set to 1 on server */
148 wo->chap_mdtype = CHAP_DIGEST_MD5;
149 wo->neg_magicnumber = 1;
150 wo->neg_pcompression = 1;
151 wo->neg_accompression = 1;
152 wo->neg_lqr = 0; /* no LQR implementation yet */
156 ao->neg_asyncmap = 1;
159 ao->chap_mdtype = CHAP_DIGEST_MD5;
161 ao->neg_magicnumber = 1;
162 ao->neg_pcompression = 1;
163 ao->neg_accompression = 1;
164 ao->neg_lqr = 0; /* no LQR implementation yet */
166 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
167 xmit_accm[unit][3] = 0x60000000;
172 * lcp_open - LCP is allowed to come up.
178 fsm *f = &lcp_fsm[unit];
179 lcp_options *wo = &lcp_wantoptions[unit];
183 f->flags |= OPT_PASSIVE;
185 f->flags |= OPT_SILENT;
191 * lcp_close - Take LCP down.
197 fsm *f = &lcp_fsm[unit];
199 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
201 * This action is not strictly according to the FSM in RFC1548,
202 * but it does mean that the program terminates if you do a
203 * lcp_close(0) in passive/silent mode when a connection hasn't
210 fsm_close(&lcp_fsm[unit]);
215 * lcp_lowerup - The lower layer is up.
221 ppp_set_xaccm(unit, xmit_accm[unit]);
222 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
223 ppp_recv_config(unit, PPP_MRU, 0x00000000, 0, 0);
224 peer_mru[unit] = PPP_MRU;
225 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
227 fsm_lowerup(&lcp_fsm[unit]);
232 * lcp_lowerdown - The lower layer is down.
238 fsm_lowerdown(&lcp_fsm[unit]);
243 * lcp_input - Input LCP packet.
246 lcp_input(unit, p, len)
252 fsm *f = &lcp_fsm[unit];
253 lcp_options *go = &lcp_gotoptions[f->unit];
256 fsm_input(f, p, len);
257 if (oldstate == REQSENT && f->state == ACKSENT) {
259 * The peer will probably send us an ack soon and then
260 * immediately start sending packets with the negotiated
261 * options. So as to be ready when that happens, we set
262 * our receive side to accept packets as negotiated now.
264 ppp_recv_config(f->unit, PPP_MRU,
265 go->neg_asyncmap? go->asyncmap: 0x00000000,
266 go->neg_pcompression, go->neg_accompression);
272 * lcp_extcode - Handle a LCP-specific code.
275 lcp_extcode(f, code, id, inp, len)
285 lcp_rprotrej(f, inp, len);
289 if (f->state != OPENED)
291 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
293 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
294 fsm_sdata(f, ECHOREP, id, inp, len);
298 lcp_received_echo_reply(f, id, inp, len);
312 * lcp_rprotrej - Receive an Protocol-Reject.
314 * Figure out which protocol is rejected and inform it.
317 lcp_rprotrej(f, inp, len)
324 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
326 if (len < sizeof (u_short)) {
328 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
335 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
339 * Protocol-Reject packets received in any state other than the LCP
340 * OPENED state SHOULD be silently discarded.
342 if( f->state != OPENED ){
343 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
348 DEMUXPROTREJ(f->unit, prot); /* Inform protocol */
353 * lcp_protrej - A Protocol-Reject was received.
363 LCPDEBUG((LOG_WARNING,
364 "lcp_protrej: Received Protocol-Reject for LCP!"));
365 fsm_protreject(&lcp_fsm[unit]);
370 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
373 lcp_sprotrej(unit, p, len)
379 * Send back the protocol and the information field of the
380 * rejected packet. We only get here if LCP is in the OPENED state.
385 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
391 * lcp_resetci - Reset our CI.
397 lcp_wantoptions[f->unit].magicnumber = magic();
398 lcp_wantoptions[f->unit].numloops = 0;
399 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
400 peer_mru[f->unit] = PPP_MRU;
405 * lcp_cilen - Return length of our CI.
411 lcp_options *go = &lcp_gotoptions[f->unit];
413 #define LENCIVOID(neg) (neg ? CILEN_VOID : 0)
414 #define LENCICHAP(neg) (neg ? CILEN_CHAP : 0)
415 #define LENCISHORT(neg) (neg ? CILEN_SHORT : 0)
416 #define LENCILONG(neg) (neg ? CILEN_LONG : 0)
417 #define LENCILQR(neg) (neg ? CILEN_LQR: 0)
419 * NB: we only ask for one of CHAP and UPAP, even if we will
422 return (LENCISHORT(go->neg_mru) +
423 LENCILONG(go->neg_asyncmap) +
424 LENCICHAP(go->neg_chap) +
425 LENCISHORT(!go->neg_chap && go->neg_upap) +
426 LENCILQR(go->neg_lqr) +
427 LENCILONG(go->neg_magicnumber) +
428 LENCIVOID(go->neg_pcompression) +
429 LENCIVOID(go->neg_accompression));
434 * lcp_addci - Add our desired CIs to a packet.
437 lcp_addci(f, ucp, lenp)
442 lcp_options *go = &lcp_gotoptions[f->unit];
443 u_char *start_ucp = ucp;
445 #define ADDCIVOID(opt, neg) \
448 PUTCHAR(CILEN_VOID, ucp); \
450 #define ADDCISHORT(opt, neg, val) \
453 PUTCHAR(CILEN_SHORT, ucp); \
454 PUTSHORT(val, ucp); \
456 #define ADDCICHAP(opt, neg, val, digest) \
459 PUTCHAR(CILEN_CHAP, ucp); \
460 PUTSHORT(val, ucp); \
461 PUTCHAR(digest, ucp); \
463 #define ADDCILONG(opt, neg, val) \
466 PUTCHAR(CILEN_LONG, ucp); \
469 #define ADDCILQR(opt, neg, val) \
472 PUTCHAR(CILEN_LQR, ucp); \
473 PUTSHORT(PPP_LQR, ucp); \
477 ADDCISHORT(CI_MRU, go->neg_mru, go->mru);
478 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
479 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
480 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
481 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
482 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
483 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
484 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
486 if (ucp - start_ucp != *lenp) {
487 /* this should never happen, because peer_mtu should be 1500 */
488 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
494 * lcp_ackci - Ack our CIs.
495 * This should not modify any state if the Ack is bad.
507 lcp_options *go = &lcp_gotoptions[f->unit];
508 u_char cilen, citype, cichar;
513 * CIs must be in exactly the same order that we sent.
514 * Check packet length and CI length at each step.
515 * If we find any deviations, then this packet is bad.
517 #define ACKCIVOID(opt, neg) \
519 if ((len -= CILEN_VOID) < 0) \
521 GETCHAR(citype, p); \
523 if (cilen != CILEN_VOID || \
527 #define ACKCISHORT(opt, neg, val) \
529 if ((len -= CILEN_SHORT) < 0) \
531 GETCHAR(citype, p); \
533 if (cilen != CILEN_SHORT || \
536 GETSHORT(cishort, p); \
537 if (cishort != val) \
540 #define ACKCICHAP(opt, neg, val, digest) \
542 if ((len -= CILEN_CHAP) < 0) \
544 GETCHAR(citype, p); \
546 if (cilen != CILEN_CHAP || \
549 GETSHORT(cishort, p); \
550 if (cishort != val) \
552 GETCHAR(cichar, p); \
553 if (cichar != digest) \
556 #define ACKCILONG(opt, neg, val) \
558 if ((len -= CILEN_LONG) < 0) \
560 GETCHAR(citype, p); \
562 if (cilen != CILEN_LONG || \
565 GETLONG(cilong, p); \
569 #define ACKCILQR(opt, neg, val) \
571 if ((len -= CILEN_LQR) < 0) \
573 GETCHAR(citype, p); \
575 if (cilen != CILEN_LQR || \
578 GETSHORT(cishort, p); \
579 if (cishort != PPP_LQR) \
581 GETLONG(cilong, p); \
586 ACKCISHORT(CI_MRU, go->neg_mru, go->mru);
587 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap, go->asyncmap);
588 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
589 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
590 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
591 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
592 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
593 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
596 * If there are any remaining CIs, then this packet is bad.
602 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
608 * lcp_nakci - Peer has sent a NAK for some of our CIs.
609 * This should not modify any state if the Nak is bad
610 * or if LCP is in the OPENED state.
622 lcp_options *go = &lcp_gotoptions[f->unit];
623 lcp_options *wo = &lcp_wantoptions[f->unit];
624 u_char citype, cichar, *next;
627 lcp_options no; /* options we've seen Naks for */
628 lcp_options try; /* options to request next time */
632 BZERO(&no, sizeof(no));
636 * Any Nak'd CIs must be in exactly the same order that we sent.
637 * Check packet length and CI length at each step.
638 * If we find any deviations, then this packet is bad.
640 #define NAKCIVOID(opt, neg, code) \
642 len >= CILEN_VOID && \
643 p[1] == CILEN_VOID && \
646 INCPTR(CILEN_VOID, p); \
650 #define NAKCICHAP(opt, neg, code) \
652 len >= CILEN_CHAP && \
653 p[1] == CILEN_CHAP && \
657 GETSHORT(cishort, p); \
658 GETCHAR(cichar, p); \
662 #define NAKCISHORT(opt, neg, code) \
664 len >= CILEN_SHORT && \
665 p[1] == CILEN_SHORT && \
667 len -= CILEN_SHORT; \
669 GETSHORT(cishort, p); \
673 #define NAKCILONG(opt, neg, code) \
675 len >= CILEN_LONG && \
676 p[1] == CILEN_LONG && \
680 GETLONG(cilong, p); \
684 #define NAKCILQR(opt, neg, code) \
686 len >= CILEN_LQR && \
687 p[1] == CILEN_LQR && \
691 GETSHORT(cishort, p); \
692 GETLONG(cilong, p); \
698 * We don't care if they want to send us smaller packets than
699 * we want. Therefore, accept any MRU less than what we asked for,
700 * but then ignore the new value when setting the MRU in the kernel.
701 * If they send us a bigger MRU than what we asked, accept it, up to
702 * the limit of the default MRU we'd get if we didn't negotiate.
704 NAKCISHORT(CI_MRU, neg_mru,
705 if (cishort <= wo->mru || cishort < DEFMRU)
710 * Add any characters they want to our (receive-side) asyncmap.
712 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
713 try.asyncmap = go->asyncmap | cilong;
717 * If they've nak'd our authentication-protocol, check whether
718 * they are proposing a different protocol, or a different
719 * hash algorithm for CHAP.
721 if ((go->neg_chap || go->neg_upap)
722 && len >= CILEN_SHORT
723 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT) {
726 GETSHORT(cishort, p);
727 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
729 * If they are asking for PAP, then they don't want to do CHAP.
730 * If we weren't asking for CHAP, then we were asking for PAP,
731 * in which case this Nak is bad.
737 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
741 * We were asking for CHAP/MD5; they must want a different
742 * algorithm. If they can't do MD5, we'll have to stop
745 if (cichar != go->chap_mdtype)
749 * Stop asking for PAP if we were asking for it.
756 * We don't recognize what they're suggesting.
757 * Stop asking for what we were asking for.
763 p += cilen - CILEN_SHORT;
768 * Peer shouldn't send Nak for protocol compression or
769 * address/control compression requests; they should send
770 * a Reject instead. If they send a Nak, treat it as a Reject.
773 NAKCISHORT(CI_AUTHTYPE, neg_upap,
779 * If they can't cope with our link quality protocol, we'll have
780 * to stop asking for LQR. We haven't got any other protocol.
781 * If they Nak the reporting period, take their value XXX ?
783 NAKCILQR(CI_QUALITY, neg_lqr,
784 if (cishort != PPP_LQR)
787 try.lqr_period = cilong;
791 * Check for a looped-back line.
793 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
795 try.magicnumber = magic();
799 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
800 try.neg_pcompression = 0;
802 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
803 try.neg_accompression = 0;
807 * There may be remaining CIs, if the peer is requesting negotiation
808 * on an option that we didn't include in our request packet.
809 * If we see an option that we requested, or one we've already seen
810 * in this packet, then this packet is bad.
811 * If we wanted to respond by starting to negotiate on the requested
812 * option(s), we could, but we don't, because except for the
813 * authentication type and quality protocol, if we are not negotiating
814 * an option, it is because we were told not to.
815 * For the authentication type, the Nak from the peer means
816 * `let me authenticate myself with you' which is a bit pointless.
817 * For the quality protocol, the Nak means `ask me to send you quality
818 * reports', but if we didn't ask for them, we don't want them.
820 while (len > CILEN_VOID) {
823 if( (len -= cilen) < 0 )
825 next = p + cilen - 2;
829 if (go->neg_mru || no.neg_mru || cilen != CILEN_SHORT)
833 if (go->neg_asyncmap || no.neg_asyncmap || cilen != CILEN_LONG)
837 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
841 if (go->neg_magicnumber || no.neg_magicnumber ||
845 case CI_PCOMPRESSION:
846 if (go->neg_pcompression || no.neg_pcompression
847 || cilen != CILEN_VOID)
850 case CI_ACCOMPRESSION:
851 if (go->neg_accompression || no.neg_accompression
852 || cilen != CILEN_VOID)
856 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
865 /* If there is still anything left, this packet is bad. */
870 * OK, the Nak is good. Now we can update state.
872 if (f->state != OPENED) {
874 if (++try.numloops >= lcp_loopbackfail) {
875 syslog(LOG_NOTICE, "Serial line is looped back.");
886 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
892 * lcp_rejci - Peer has Rejected some of our CIs.
893 * This should not modify any state if the Reject is bad
894 * or if LCP is in the OPENED state.
897 * 0 - Reject was bad.
898 * 1 - Reject was good.
906 lcp_options *go = &lcp_gotoptions[f->unit];
912 lcp_options try; /* options to request next time */
917 * Any Rejected CIs must be in exactly the same order that we sent.
918 * Check packet length and CI length at each step.
919 * If we find any deviations, then this packet is bad.
921 #define REJCIVOID(opt, neg) \
923 len >= CILEN_VOID && \
924 p[1] == CILEN_VOID && \
927 INCPTR(CILEN_VOID, p); \
929 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
931 #define REJCISHORT(opt, neg, val) \
933 len >= CILEN_SHORT && \
934 p[1] == CILEN_SHORT && \
936 len -= CILEN_SHORT; \
938 GETSHORT(cishort, p); \
939 /* Check rejected value. */ \
940 if (cishort != val) \
943 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
945 #define REJCICHAP(opt, neg, val, digest) \
947 len >= CILEN_CHAP && \
948 p[1] == CILEN_CHAP && \
952 GETSHORT(cishort, p); \
953 GETCHAR(cichar, p); \
954 /* Check rejected value. */ \
955 if (cishort != val || cichar != digest) \
959 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
961 #define REJCILONG(opt, neg, val) \
963 len >= CILEN_LONG && \
964 p[1] == CILEN_LONG && \
968 GETLONG(cilong, p); \
969 /* Check rejected value. */ \
973 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
975 #define REJCILQR(opt, neg, val) \
977 len >= CILEN_LQR && \
978 p[1] == CILEN_LQR && \
982 GETSHORT(cishort, p); \
983 GETLONG(cilong, p); \
984 /* Check rejected value. */ \
985 if (cishort != PPP_LQR || cilong != val) \
988 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
991 REJCISHORT(CI_MRU, neg_mru, go->mru);
992 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
993 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
995 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
997 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
998 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
999 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1000 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1003 * If there are any remaining CIs, then this packet is bad.
1008 * Now we can update state.
1010 if (f->state != OPENED)
1015 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1016 LCPDEBUG((LOG_WARNING, "lcp_rejci: plen %d len %d off %d",
1017 plen, len, p - start));
1023 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1025 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1026 * appropriately. If reject_if_disagree is non-zero, doesn't return
1027 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1030 lcp_reqci(f, inp, lenp, reject_if_disagree)
1032 u_char *inp; /* Requested CIs */
1033 int *lenp; /* Length of requested CIs */
1034 int reject_if_disagree;
1036 lcp_options *go = &lcp_gotoptions[f->unit];
1037 lcp_options *ho = &lcp_hisoptions[f->unit];
1038 lcp_options *ao = &lcp_allowoptions[f->unit];
1039 u_char *cip, *next; /* Pointer to current and next CIs */
1040 u_char cilen, citype, cichar;/* Parsed len, type, char value */
1041 u_short cishort; /* Parsed short value */
1042 u_int32_t cilong; /* Parse long value */
1043 int rc = CONFACK; /* Final packet return code */
1044 int orc; /* Individual option return code */
1045 u_char *p; /* Pointer to next char to parse */
1046 u_char *rejp; /* Pointer to next char in reject frame */
1047 u_char *nakp; /* Pointer to next char in Nak frame */
1048 int l = *lenp; /* Length left */
1051 * Reset all his options.
1053 BZERO(ho, sizeof(*ho));
1056 * Process all his options.
1062 orc = CONFACK; /* Assume success */
1063 cip = p = next; /* Remember begining of CI */
1064 if (l < 2 || /* Not enough data for CI header or */
1065 p[1] < 2 || /* CI length too small or */
1066 p[1] > l) { /* CI length too big? */
1067 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1068 orc = CONFREJ; /* Reject bad CI */
1069 cilen = l; /* Reject till end of packet */
1070 l = 0; /* Don't loop again */
1073 GETCHAR(citype, p); /* Parse CI type */
1074 GETCHAR(cilen, p); /* Parse CI length */
1075 l -= cilen; /* Adjust remaining length */
1076 next += cilen; /* Step to next CI */
1078 switch (citype) { /* Check CI type */
1080 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1081 if (!ao->neg_mru || /* Allow option? */
1082 cilen != CILEN_SHORT) { /* Check CI length */
1083 orc = CONFREJ; /* Reject CI */
1086 GETSHORT(cishort, p); /* Parse MRU */
1087 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1090 * He must be able to receive at least our minimum.
1091 * No need to check a maximum. If he sends a large number,
1092 * we'll just ignore it.
1094 if (cishort < MINMRU) {
1095 orc = CONFNAK; /* Nak CI */
1096 PUTCHAR(CI_MRU, nakp);
1097 PUTCHAR(CILEN_SHORT, nakp);
1098 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1101 ho->neg_mru = 1; /* Remember he sent MRU */
1102 ho->mru = cishort; /* And remember value */
1106 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1107 if (!ao->neg_asyncmap ||
1108 cilen != CILEN_LONG) {
1113 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1116 * Asyncmap must have set at least the bits
1117 * which are set in lcp_allowoptions[unit].asyncmap.
1119 if ((ao->asyncmap & ~cilong) != 0) {
1121 PUTCHAR(CI_ASYNCMAP, nakp);
1122 PUTCHAR(CILEN_LONG, nakp);
1123 PUTLONG(ao->asyncmap | cilong, nakp);
1126 ho->neg_asyncmap = 1;
1127 ho->asyncmap = cilong;
1131 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1132 if (cilen < CILEN_SHORT ||
1133 !(ao->neg_upap || ao->neg_chap)) {
1135 * Reject the option if we're not willing to authenticate.
1140 GETSHORT(cishort, p);
1141 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1144 * Authtype must be UPAP or CHAP.
1146 * Note: if both ao->neg_upap and ao->neg_chap are set,
1147 * and the peer sends a Configure-Request with two
1148 * authenticate-protocol requests, one for CHAP and one
1149 * for UPAP, then we will reject the second request.
1150 * Whether we end up doing CHAP or UPAP depends then on
1151 * the ordering of the CIs in the peer's Configure-Request.
1154 if (cishort == PPP_PAP) {
1155 if (ho->neg_chap || /* we've already accepted CHAP */
1156 cilen != CILEN_SHORT) {
1157 LCPDEBUG((LOG_WARNING,
1158 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1162 if (!ao->neg_upap) { /* we don't want to do PAP */
1163 orc = CONFNAK; /* NAK it and suggest CHAP */
1164 PUTCHAR(CI_AUTHTYPE, nakp);
1165 PUTCHAR(CILEN_CHAP, nakp);
1166 PUTSHORT(PPP_CHAP, nakp);
1167 PUTCHAR(ao->chap_mdtype, nakp);
1173 if (cishort == PPP_CHAP) {
1174 if (ho->neg_upap || /* we've already accepted PAP */
1175 cilen != CILEN_CHAP) {
1177 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1181 if (!ao->neg_chap) { /* we don't want to do CHAP */
1182 orc = CONFNAK; /* NAK it and suggest PAP */
1183 PUTCHAR(CI_AUTHTYPE, nakp);
1184 PUTCHAR(CILEN_SHORT, nakp);
1185 PUTSHORT(PPP_PAP, nakp);
1188 GETCHAR(cichar, p); /* get digest type*/
1189 if (cichar != ao->chap_mdtype) {
1191 PUTCHAR(CI_AUTHTYPE, nakp);
1192 PUTCHAR(CILEN_CHAP, nakp);
1193 PUTSHORT(PPP_CHAP, nakp);
1194 PUTCHAR(ao->chap_mdtype, nakp);
1197 ho->chap_mdtype = cichar; /* save md type */
1203 * We don't recognize the protocol they're asking for.
1204 * Nak it with something we're willing to do.
1205 * (At this point we know ao->neg_upap || ao->neg_chap.)
1208 PUTCHAR(CI_AUTHTYPE, nakp);
1210 PUTCHAR(CILEN_CHAP, nakp);
1211 PUTSHORT(PPP_CHAP, nakp);
1212 PUTCHAR(ao->chap_mdtype, nakp);
1214 PUTCHAR(CILEN_SHORT, nakp);
1215 PUTSHORT(PPP_PAP, nakp);
1220 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1222 cilen != CILEN_LQR) {
1227 GETSHORT(cishort, p);
1229 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1232 * Check the protocol and the reporting period.
1233 * XXX When should we Nak this, and what with?
1235 if (cishort != PPP_LQR) {
1237 PUTCHAR(CI_QUALITY, nakp);
1238 PUTCHAR(CILEN_LQR, nakp);
1239 PUTSHORT(PPP_LQR, nakp);
1240 PUTLONG(ao->lqr_period, nakp);
1245 case CI_MAGICNUMBER:
1246 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1247 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1248 cilen != CILEN_LONG) {
1253 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1256 * He must have a different magic number.
1258 if (go->neg_magicnumber &&
1259 cilong == go->magicnumber) {
1260 cilong = magic(); /* Don't put magic() inside macro! */
1262 PUTCHAR(CI_MAGICNUMBER, nakp);
1263 PUTCHAR(CILEN_LONG, nakp);
1264 PUTLONG(cilong, nakp);
1267 ho->neg_magicnumber = 1;
1268 ho->magicnumber = cilong;
1272 case CI_PCOMPRESSION:
1273 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1274 if (!ao->neg_pcompression ||
1275 cilen != CILEN_VOID) {
1279 ho->neg_pcompression = 1;
1282 case CI_ACCOMPRESSION:
1283 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1284 if (!ao->neg_accompression ||
1285 cilen != CILEN_VOID) {
1289 ho->neg_accompression = 1;
1293 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1300 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1301 if (orc == CONFACK && /* Good CI */
1302 rc != CONFACK) /* but prior CI wasnt? */
1303 continue; /* Don't send this one */
1305 if (orc == CONFNAK) { /* Nak this CI? */
1306 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1307 && citype != CI_MAGICNUMBER) {
1308 orc = CONFREJ; /* Get tough if so */
1310 if (rc == CONFREJ) /* Rejecting prior CI? */
1311 continue; /* Don't send this one */
1315 if (orc == CONFREJ) { /* Reject this CI */
1317 if (cip != rejp) /* Need to move rejected CI? */
1318 BCOPY(cip, rejp, cilen); /* Move it */
1319 INCPTR(cilen, rejp); /* Update output pointer */
1324 * If we wanted to send additional NAKs (for unsent CIs), the
1325 * code would go here. The extra NAKs would go at *nakp.
1326 * At present there are no cases where we want to ask the
1327 * peer to negotiate an option.
1336 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1338 *lenp = nakp - nak_buffer;
1339 BCOPY(nak_buffer, inp, *lenp);
1346 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1347 return (rc); /* Return final code */
1352 * lcp_up - LCP has come UP.
1354 * Start UPAP, IPCP, etc.
1360 lcp_options *wo = &lcp_wantoptions[f->unit];
1361 lcp_options *ho = &lcp_hisoptions[f->unit];
1362 lcp_options *go = &lcp_gotoptions[f->unit];
1363 lcp_options *ao = &lcp_allowoptions[f->unit];
1365 if (!go->neg_magicnumber)
1366 go->magicnumber = 0;
1367 if (!ho->neg_magicnumber)
1368 ho->magicnumber = 0;
1371 * Set our MTU to the smaller of the MTU we wanted and
1372 * the MRU our peer wanted. If we negotiated an MRU,
1373 * set our MRU to the larger of value we wanted and
1374 * the value we got in the negotiation.
1376 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1377 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1378 ho->neg_pcompression, ho->neg_accompression);
1380 * If the asyncmap hasn't been negotiated, we really should
1381 * set the receive asyncmap to ffffffff, but we set it to 0
1382 * for backwards contemptibility.
1384 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1385 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1386 go->neg_pcompression, go->neg_accompression);
1389 peer_mru[f->unit] = ho->mru;
1391 ChapLowerUp(f->unit); /* Enable CHAP */
1392 upap_lowerup(f->unit); /* Enable UPAP */
1393 ipcp_lowerup(f->unit); /* Enable IPCP */
1394 ccp_lowerup(f->unit); /* Enable CCP */
1395 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1397 link_established(f->unit);
1402 * lcp_down - LCP has gone DOWN.
1404 * Alert other protocols.
1410 lcp_echo_lowerdown(f->unit);
1411 ccp_lowerdown(f->unit);
1412 ipcp_lowerdown(f->unit);
1413 ChapLowerDown(f->unit);
1414 upap_lowerdown(f->unit);
1418 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1419 ppp_recv_config(f->unit, PPP_MRU, 0x00000000, 0, 0);
1420 peer_mru[f->unit] = PPP_MRU;
1425 * lcp_starting - LCP needs the lower layer up.
1431 link_required(f->unit);
1436 * lcp_finished - LCP has finished with the lower layer.
1442 link_terminated(f->unit);
1447 * lcp_printpkt - print the contents of an LCP packet.
1449 char *lcp_codenames[] = {
1450 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1451 "TermReq", "TermAck", "CodeRej", "ProtRej",
1452 "EchoReq", "EchoRep", "DiscReq"
1456 lcp_printpkt(p, plen, printer, arg)
1459 void (*printer) __P((void *, char *, ...));
1462 int code, id, len, olen;
1463 u_char *pstart, *optend;
1467 if (plen < HEADERLEN)
1473 if (len < HEADERLEN || len > plen)
1476 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1477 printer(arg, " %s", lcp_codenames[code-1]);
1479 printer(arg, " code=0x%x", code);
1480 printer(arg, " id=0x%x", id);
1487 /* print option list */
1492 if (olen < 2 || olen > len) {
1500 if (olen == CILEN_SHORT) {
1502 GETSHORT(cishort, p);
1503 printer(arg, "mru %d", cishort);
1507 if (olen == CILEN_LONG) {
1510 printer(arg, "asyncmap 0x%x", cilong);
1514 if (olen >= CILEN_SHORT) {
1516 printer(arg, "auth ");
1517 GETSHORT(cishort, p);
1520 printer(arg, "upap");
1523 printer(arg, "chap");
1526 printer(arg, "0x%x", cishort);
1531 if (olen >= CILEN_SHORT) {
1533 printer(arg, "quality ");
1534 GETSHORT(cishort, p);
1537 printer(arg, "lqr");
1540 printer(arg, "0x%x", cishort);
1544 case CI_MAGICNUMBER:
1545 if (olen == CILEN_LONG) {
1548 printer(arg, "magic 0x%x", cilong);
1551 case CI_PCOMPRESSION:
1552 if (olen == CILEN_VOID) {
1554 printer(arg, "pcomp");
1557 case CI_ACCOMPRESSION:
1558 if (olen == CILEN_VOID) {
1560 printer(arg, "accomp");
1564 while (p < optend) {
1566 printer(arg, " %.2x", code);
1573 /* print the rest of the bytes in the packet */
1574 for (; len > 0; --len) {
1576 printer(arg, " %.2x", code);
1583 * Time to shut down the link because there is nothing out there.
1587 void LcpLinkFailure (f)
1590 if (f->state == OPENED) {
1591 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1592 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1593 lcp_close(f->unit); /* Reset connection */
1598 * Timer expired for the LCP echo requests from this process.
1605 LcpSendEchoRequest (f);
1608 * Start the timer for the next interval.
1610 assert (lcp_echo_timer_running==0);
1611 TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1612 lcp_echo_timer_running = 1;
1616 * LcpEchoTimeout - Timer expired on the LCP echo
1620 LcpEchoTimeout (arg)
1623 if (lcp_echo_timer_running != 0) {
1624 lcp_echo_timer_running = 0;
1625 LcpEchoCheck ((fsm *) arg);
1630 * LcpEchoReply - LCP has received a reply to the echo
1634 lcp_received_echo_reply (f, id, inp, len)
1636 int id; u_char *inp; int len;
1640 /* Check the magic number - don't count replies from ourselves. */
1642 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1645 GETLONG(magic, inp);
1646 if (lcp_gotoptions[f->unit].neg_magicnumber
1647 && magic == lcp_gotoptions[f->unit].magicnumber) {
1648 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1652 /* Reset the number of outstanding echo frames */
1653 lcp_echos_pending = 0;
1657 * LcpSendEchoRequest - Send an echo request frame to the peer
1661 LcpSendEchoRequest (f)
1664 u_int32_t lcp_magic;
1665 u_char pkt[4], *pktp;
1668 * Detect the failure of the peer at this point.
1670 if (lcp_echo_fails != 0) {
1671 if (lcp_echos_pending++ >= lcp_echo_fails) {
1673 lcp_echos_pending = 0;
1678 * Make and send the echo request frame.
1680 if (f->state == OPENED) {
1681 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1683 PUTLONG(lcp_magic, pktp);
1684 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1689 * lcp_echo_lowerup - Start the timer for the LCP frame
1693 lcp_echo_lowerup (unit)
1696 fsm *f = &lcp_fsm[unit];
1698 /* Clear the parameters for generating echo frames */
1699 lcp_echos_pending = 0;
1700 lcp_echo_number = 0;
1701 lcp_echo_timer_running = 0;
1703 /* If a timeout interval is specified then start the timer */
1704 if (lcp_echo_interval != 0)
1709 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1713 lcp_echo_lowerdown (unit)
1716 fsm *f = &lcp_fsm[unit];
1718 if (lcp_echo_timer_running != 0) {
1719 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1720 lcp_echo_timer_running = 0;