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.29 1997/03/04 03:39:56 paulus Exp $";
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
45 fsm lcp_fsm[NUM_PPP]; /* LCP fsm structure (global)*/
46 lcp_options lcp_wantoptions[NUM_PPP]; /* Options that we want to request */
47 lcp_options lcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
48 lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
49 lcp_options lcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
50 u_int32_t xmit_accm[NUM_PPP][8]; /* extended transmit ACCM */
52 static u_int32_t lcp_echos_pending = 0; /* Number of outstanding echo msgs */
53 static u_int32_t lcp_echo_number = 0; /* ID number of next echo frame */
54 static u_int32_t lcp_echo_timer_running = 0; /* TRUE if a timer is running */
56 static u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */
59 * Callbacks for fsm code. (CI = Configuration Information)
61 static void lcp_resetci __P((fsm *)); /* Reset our CI */
62 static int lcp_cilen __P((fsm *)); /* Return length of our CI */
63 static void lcp_addci __P((fsm *, u_char *, int *)); /* Add our CI to pkt */
64 static int lcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
65 static int lcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
66 static int lcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
67 static int lcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv peer CI */
68 static void lcp_up __P((fsm *)); /* We're UP */
69 static void lcp_down __P((fsm *)); /* We're DOWN */
70 static void lcp_starting __P((fsm *)); /* We need lower layer up */
71 static void lcp_finished __P((fsm *)); /* We need lower layer down */
72 static int lcp_extcode __P((fsm *, int, int, u_char *, int));
73 static void lcp_rprotrej __P((fsm *, u_char *, int));
76 * routines to send LCP echos to peer
79 static void lcp_echo_lowerup __P((int));
80 static void lcp_echo_lowerdown __P((int));
81 static void LcpEchoTimeout __P((caddr_t));
82 static void lcp_received_echo_reply __P((fsm *, int, u_char *, int));
83 static void LcpSendEchoRequest __P((fsm *));
84 static void LcpLinkFailure __P((fsm *));
86 static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
87 lcp_resetci, /* Reset our Configuration Information */
88 lcp_cilen, /* Length of our Configuration Information */
89 lcp_addci, /* Add our Configuration Information */
90 lcp_ackci, /* ACK our Configuration Information */
91 lcp_nakci, /* NAK our Configuration Information */
92 lcp_rejci, /* Reject our Configuration Information */
93 lcp_reqci, /* Request peer's Configuration Information */
94 lcp_up, /* Called when fsm reaches OPENED state */
95 lcp_down, /* Called when fsm leaves OPENED state */
96 lcp_starting, /* Called when we want the lower layer up */
97 lcp_finished, /* Called when we want the lower layer down */
98 NULL, /* Called when Protocol-Reject received */
99 NULL, /* Retransmission is necessary */
100 lcp_extcode, /* Called to handle LCP-specific codes */
101 "LCP" /* String name of protocol */
105 * Protocol entry points.
106 * Some of these are called directly.
109 static void lcp_init __P((int));
110 static void lcp_input __P((int, u_char *, int));
111 static void lcp_protrej __P((int));
112 static int lcp_printpkt __P((u_char *, int,
113 void (*) __P((void *, char *, ...)), void *));
115 struct protent lcp_protent = {
133 int lcp_loopbackfail = DEFLOOPBACKFAIL;
136 * Length of each type of configuration option (in octets)
140 #define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
141 #define CILEN_CHAP 5 /* CILEN_VOID + sizeof(short) + 1 */
142 #define CILEN_LONG 6 /* CILEN_VOID + sizeof(long) */
143 #define CILEN_LQR 8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
146 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
147 (x) == CONFNAK ? "NAK" : "REJ")
151 * lcp_init - Initialize LCP.
157 fsm *f = &lcp_fsm[unit];
158 lcp_options *wo = &lcp_wantoptions[unit];
159 lcp_options *ao = &lcp_allowoptions[unit];
162 f->protocol = PPP_LCP;
163 f->callbacks = &lcp_callbacks;
169 wo->restart = 0; /* Set to 1 in kernels or multi-line
173 wo->neg_asyncmap = 0;
175 wo->neg_chap = 0; /* Set to 1 on server */
176 wo->neg_upap = 0; /* Set to 1 on server */
177 wo->chap_mdtype = CHAP_DIGEST_MD5;
178 wo->neg_magicnumber = 1;
179 wo->neg_pcompression = 1;
180 wo->neg_accompression = 1;
181 wo->neg_lqr = 0; /* no LQR implementation yet */
186 ao->neg_asyncmap = 1;
189 ao->chap_mdtype = CHAP_DIGEST_MD5;
191 ao->neg_magicnumber = 1;
192 ao->neg_pcompression = 1;
193 ao->neg_accompression = 1;
194 ao->neg_lqr = 0; /* no LQR implementation yet */
201 memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
202 xmit_accm[unit][3] = 0x60000000;
207 * lcp_open - LCP is allowed to come up.
213 fsm *f = &lcp_fsm[unit];
214 lcp_options *wo = &lcp_wantoptions[unit];
218 f->flags |= OPT_PASSIVE;
220 f->flags |= OPT_SILENT;
226 * lcp_close - Take LCP down.
229 lcp_close(unit, reason)
233 fsm *f = &lcp_fsm[unit];
235 if (phase != PHASE_DEAD)
236 phase = PHASE_TERMINATE;
237 if (f->state == STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
239 * This action is not strictly according to the FSM in RFC1548,
240 * but it does mean that the program terminates if you do a
241 * lcp_close() in passive/silent mode when a connection hasn't
248 fsm_close(&lcp_fsm[unit], reason);
253 * lcp_lowerup - The lower layer is up.
259 lcp_options *wo = &lcp_wantoptions[unit];
262 * Don't use A/C or protocol compression on transmission,
263 * but accept A/C and protocol compressed packets
264 * if we are going to ask for A/C and protocol compression.
266 ppp_set_xaccm(unit, xmit_accm[unit]);
267 ppp_send_config(unit, PPP_MRU, 0xffffffff, 0, 0);
268 ppp_recv_config(unit, PPP_MRU, 0x00000000,
269 wo->neg_pcompression, wo->neg_accompression);
270 peer_mru[unit] = PPP_MRU;
271 lcp_allowoptions[unit].asyncmap = xmit_accm[unit][0];
273 fsm_lowerup(&lcp_fsm[unit]);
278 * lcp_lowerdown - The lower layer is down.
284 fsm_lowerdown(&lcp_fsm[unit]);
289 * lcp_input - Input LCP packet.
292 lcp_input(unit, p, len)
297 fsm *f = &lcp_fsm[unit];
299 fsm_input(f, p, len);
304 * lcp_extcode - Handle a LCP-specific code.
307 lcp_extcode(f, code, id, inp, len)
317 lcp_rprotrej(f, inp, len);
321 if (f->state != OPENED)
323 LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d", id));
325 PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
326 fsm_sdata(f, ECHOREP, id, inp, len);
330 lcp_received_echo_reply(f, id, inp, len);
344 * lcp_rprotrej - Receive an Protocol-Reject.
346 * Figure out which protocol is rejected and inform it.
349 lcp_rprotrej(f, inp, len)
355 struct protent *protp;
358 LCPDEBUG((LOG_INFO, "lcp_rprotrej."));
360 if (len < sizeof (u_short)) {
362 "lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
369 "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!",
373 * Protocol-Reject packets received in any state other than the LCP
374 * OPENED state SHOULD be silently discarded.
376 if( f->state != OPENED ){
377 LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d",
383 * Upcall the proper Protocol-Reject routine.
385 for (i = 0; (protp = protocols[i]) != NULL; ++i)
386 if (protp->protocol == prot && protp->enabled_flag) {
387 (*protp->protrej)(f->unit);
391 syslog(LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x",
397 * lcp_protrej - A Protocol-Reject was received.
407 LCPDEBUG((LOG_WARNING,
408 "lcp_protrej: Received Protocol-Reject for LCP!"));
409 fsm_protreject(&lcp_fsm[unit]);
414 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
417 lcp_sprotrej(unit, p, len)
423 * Send back the protocol and the information field of the
424 * rejected packet. We only get here if LCP is in the OPENED state.
429 fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id,
435 * lcp_resetci - Reset our CI.
441 lcp_wantoptions[f->unit].magicnumber = magic();
442 lcp_wantoptions[f->unit].numloops = 0;
443 lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
444 peer_mru[f->unit] = PPP_MRU;
450 * lcp_cilen - Return length of our CI.
456 lcp_options *go = &lcp_gotoptions[f->unit];
458 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
459 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
460 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
461 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
462 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
463 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
465 * NB: we only ask for one of CHAP and UPAP, even if we will
468 return (LENCISHORT(go->neg_mru && go->mru != DEFMRU) +
469 LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) +
470 LENCICHAP(go->neg_chap) +
471 LENCISHORT(!go->neg_chap && go->neg_upap) +
472 LENCILQR(go->neg_lqr) +
473 LENCICBCP(go->neg_cbcp) +
474 LENCILONG(go->neg_magicnumber) +
475 LENCIVOID(go->neg_pcompression) +
476 LENCIVOID(go->neg_accompression));
481 * lcp_addci - Add our desired CIs to a packet.
484 lcp_addci(f, ucp, lenp)
489 lcp_options *go = &lcp_gotoptions[f->unit];
490 u_char *start_ucp = ucp;
492 #define ADDCIVOID(opt, neg) \
495 PUTCHAR(CILEN_VOID, ucp); \
497 #define ADDCISHORT(opt, neg, val) \
500 PUTCHAR(CILEN_SHORT, ucp); \
501 PUTSHORT(val, ucp); \
503 #define ADDCICHAP(opt, neg, val, digest) \
506 PUTCHAR(CILEN_CHAP, ucp); \
507 PUTSHORT(val, ucp); \
508 PUTCHAR(digest, ucp); \
510 #define ADDCILONG(opt, neg, val) \
513 PUTCHAR(CILEN_LONG, ucp); \
516 #define ADDCILQR(opt, neg, val) \
519 PUTCHAR(CILEN_LQR, ucp); \
520 PUTSHORT(PPP_LQR, ucp); \
523 #define ADDCICHAR(opt, neg, val) \
526 PUTCHAR(CILEN_CHAR, ucp); \
530 ADDCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
531 ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
533 ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
534 ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
535 ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
536 ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
537 ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
538 ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
539 ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
541 if (ucp - start_ucp != *lenp) {
542 /* this should never happen, because peer_mtu should be 1500 */
543 syslog(LOG_ERR, "Bug in lcp_addci: wrong length");
549 * lcp_ackci - Ack our CIs.
550 * This should not modify any state if the Ack is bad.
562 lcp_options *go = &lcp_gotoptions[f->unit];
563 u_char cilen, citype, cichar;
568 * CIs must be in exactly the same order that we sent.
569 * Check packet length and CI length at each step.
570 * If we find any deviations, then this packet is bad.
572 #define ACKCIVOID(opt, neg) \
574 if ((len -= CILEN_VOID) < 0) \
576 GETCHAR(citype, p); \
578 if (cilen != CILEN_VOID || \
582 #define ACKCISHORT(opt, neg, val) \
584 if ((len -= CILEN_SHORT) < 0) \
586 GETCHAR(citype, p); \
588 if (cilen != CILEN_SHORT || \
591 GETSHORT(cishort, p); \
592 if (cishort != val) \
595 #define ACKCICHAR(opt, neg, val) \
597 if ((len -= CILEN_CHAR) < 0) \
599 GETCHAR(citype, p); \
601 if (cilen != CILEN_CHAR || \
604 GETCHAR(cichar, p); \
608 #define ACKCICHAP(opt, neg, val, digest) \
610 if ((len -= CILEN_CHAP) < 0) \
612 GETCHAR(citype, p); \
614 if (cilen != CILEN_CHAP || \
617 GETSHORT(cishort, p); \
618 if (cishort != val) \
620 GETCHAR(cichar, p); \
621 if (cichar != digest) \
624 #define ACKCILONG(opt, neg, val) \
626 if ((len -= CILEN_LONG) < 0) \
628 GETCHAR(citype, p); \
630 if (cilen != CILEN_LONG || \
633 GETLONG(cilong, p); \
637 #define ACKCILQR(opt, neg, val) \
639 if ((len -= CILEN_LQR) < 0) \
641 GETCHAR(citype, p); \
643 if (cilen != CILEN_LQR || \
646 GETSHORT(cishort, p); \
647 if (cishort != PPP_LQR) \
649 GETLONG(cilong, p); \
654 ACKCISHORT(CI_MRU, go->neg_mru && go->mru != DEFMRU, go->mru);
655 ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF,
657 ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
658 ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
659 ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
660 ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
661 ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
662 ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
663 ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
666 * If there are any remaining CIs, then this packet is bad.
672 LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!"));
678 * lcp_nakci - Peer has sent a NAK for some of our CIs.
679 * This should not modify any state if the Nak is bad
680 * or if LCP is in the OPENED state.
692 lcp_options *go = &lcp_gotoptions[f->unit];
693 lcp_options *wo = &lcp_wantoptions[f->unit];
694 u_char citype, cichar, *next;
697 lcp_options no; /* options we've seen Naks for */
698 lcp_options try; /* options to request next time */
702 BZERO(&no, sizeof(no));
706 * Any Nak'd CIs must be in exactly the same order that we sent.
707 * Check packet length and CI length at each step.
708 * If we find any deviations, then this packet is bad.
710 #define NAKCIVOID(opt, neg, code) \
712 len >= CILEN_VOID && \
713 p[1] == CILEN_VOID && \
716 INCPTR(CILEN_VOID, p); \
720 #define NAKCICHAP(opt, neg, code) \
722 len >= CILEN_CHAP && \
723 p[1] == CILEN_CHAP && \
727 GETSHORT(cishort, p); \
728 GETCHAR(cichar, p); \
732 #define NAKCICHAR(opt, neg, code) \
734 len >= CILEN_CHAR && \
735 p[1] == CILEN_CHAR && \
739 GETCHAR(cichar, p); \
743 #define NAKCISHORT(opt, neg, code) \
745 len >= CILEN_SHORT && \
746 p[1] == CILEN_SHORT && \
748 len -= CILEN_SHORT; \
750 GETSHORT(cishort, p); \
754 #define NAKCILONG(opt, neg, code) \
756 len >= CILEN_LONG && \
757 p[1] == CILEN_LONG && \
761 GETLONG(cilong, p); \
765 #define NAKCILQR(opt, neg, code) \
767 len >= CILEN_LQR && \
768 p[1] == CILEN_LQR && \
772 GETSHORT(cishort, p); \
773 GETLONG(cilong, p); \
779 * We don't care if they want to send us smaller packets than
780 * we want. Therefore, accept any MRU less than what we asked for,
781 * but then ignore the new value when setting the MRU in the kernel.
782 * If they send us a bigger MRU than what we asked, accept it, up to
783 * the limit of the default MRU we'd get if we didn't negotiate.
785 if (go->neg_mru && go->mru != DEFMRU) {
786 NAKCISHORT(CI_MRU, neg_mru,
787 if (cishort <= wo->mru || cishort < DEFMRU)
793 * Add any characters they want to our (receive-side) asyncmap.
795 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF) {
796 NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
797 try.asyncmap = go->asyncmap | cilong;
802 * If they've nak'd our authentication-protocol, check whether
803 * they are proposing a different protocol, or a different
804 * hash algorithm for CHAP.
806 if ((go->neg_chap || go->neg_upap)
807 && len >= CILEN_SHORT
808 && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
811 no.neg_chap = go->neg_chap;
812 no.neg_upap = go->neg_upap;
814 GETSHORT(cishort, p);
815 if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
817 * If we were asking for CHAP, they obviously don't want to do it.
818 * If we weren't asking for CHAP, then we were asking for PAP,
819 * in which case this Nak is bad.
825 } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
829 * We were asking for CHAP/MD5; they must want a different
830 * algorithm. If they can't do MD5, we'll have to stop
833 if (cichar != go->chap_mdtype)
837 * Stop asking for PAP if we were asking for it.
844 * We don't recognize what they're suggesting.
845 * Stop asking for what we were asking for.
851 p += cilen - CILEN_SHORT;
856 * If they can't cope with our link quality protocol, we'll have
857 * to stop asking for LQR. We haven't got any other protocol.
858 * If they Nak the reporting period, take their value XXX ?
860 NAKCILQR(CI_QUALITY, neg_lqr,
861 if (cishort != PPP_LQR)
864 try.lqr_period = cilong;
868 * Only implementing CBCP...not the rest of the callback options
870 NAKCICHAR(CI_CALLBACK, neg_cbcp,
875 * Check for a looped-back line.
877 NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
878 try.magicnumber = magic();
883 * Peer shouldn't send Nak for protocol compression or
884 * address/control compression requests; they should send
885 * a Reject instead. If they send a Nak, treat it as a Reject.
887 NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
888 try.neg_pcompression = 0;
890 NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
891 try.neg_accompression = 0;
895 * There may be remaining CIs, if the peer is requesting negotiation
896 * on an option that we didn't include in our request packet.
897 * If we see an option that we requested, or one we've already seen
898 * in this packet, then this packet is bad.
899 * If we wanted to respond by starting to negotiate on the requested
900 * option(s), we could, but we don't, because except for the
901 * authentication type and quality protocol, if we are not negotiating
902 * an option, it is because we were told not to.
903 * For the authentication type, the Nak from the peer means
904 * `let me authenticate myself with you' which is a bit pointless.
905 * For the quality protocol, the Nak means `ask me to send you quality
906 * reports', but if we didn't ask for them, we don't want them.
907 * An option we don't recognize represents the peer asking to
908 * negotiate some option we don't support, so ignore it.
910 while (len > CILEN_VOID) {
913 if (cilen < CILEN_VOID || (len -= cilen) < 0)
915 next = p + cilen - 2;
919 if (go->neg_mru && go->mru != DEFMRU
920 || no.neg_mru || cilen != CILEN_SHORT)
922 GETSHORT(cishort, p);
923 if (cishort < DEFMRU)
927 if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFF
928 || no.neg_asyncmap || cilen != CILEN_LONG)
932 if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap)
936 if (go->neg_magicnumber || no.neg_magicnumber ||
940 case CI_PCOMPRESSION:
941 if (go->neg_pcompression || no.neg_pcompression
942 || cilen != CILEN_VOID)
945 case CI_ACCOMPRESSION:
946 if (go->neg_accompression || no.neg_accompression
947 || cilen != CILEN_VOID)
951 if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR)
958 /* If there is still anything left, this packet is bad. */
963 * OK, the Nak is good. Now we can update state.
965 if (f->state != OPENED) {
967 if (++try.numloops >= lcp_loopbackfail) {
968 syslog(LOG_NOTICE, "Serial line is looped back.");
969 lcp_close(f->unit, "Loopback detected");
979 LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!"));
985 * lcp_rejci - Peer has Rejected some of our CIs.
986 * This should not modify any state if the Reject is bad
987 * or if LCP is in the OPENED state.
990 * 0 - Reject was bad.
991 * 1 - Reject was good.
999 lcp_options *go = &lcp_gotoptions[f->unit];
1003 lcp_options try; /* options to request next time */
1008 * Any Rejected CIs must be in exactly the same order that we sent.
1009 * Check packet length and CI length at each step.
1010 * If we find any deviations, then this packet is bad.
1012 #define REJCIVOID(opt, neg) \
1014 len >= CILEN_VOID && \
1015 p[1] == CILEN_VOID && \
1017 len -= CILEN_VOID; \
1018 INCPTR(CILEN_VOID, p); \
1020 LCPDEBUG((LOG_INFO, "lcp_rejci rejected void opt %d", opt)); \
1022 #define REJCISHORT(opt, neg, val) \
1024 len >= CILEN_SHORT && \
1025 p[1] == CILEN_SHORT && \
1027 len -= CILEN_SHORT; \
1029 GETSHORT(cishort, p); \
1030 /* Check rejected value. */ \
1031 if (cishort != val) \
1034 LCPDEBUG((LOG_INFO,"lcp_rejci rejected short opt %d", opt)); \
1036 #define REJCICHAP(opt, neg, val, digest) \
1038 len >= CILEN_CHAP && \
1039 p[1] == CILEN_CHAP && \
1041 len -= CILEN_CHAP; \
1043 GETSHORT(cishort, p); \
1044 GETCHAR(cichar, p); \
1045 /* Check rejected value. */ \
1046 if (cishort != val || cichar != digest) \
1050 LCPDEBUG((LOG_INFO,"lcp_rejci rejected chap opt %d", opt)); \
1052 #define REJCILONG(opt, neg, val) \
1054 len >= CILEN_LONG && \
1055 p[1] == CILEN_LONG && \
1057 len -= CILEN_LONG; \
1059 GETLONG(cilong, p); \
1060 /* Check rejected value. */ \
1061 if (cilong != val) \
1064 LCPDEBUG((LOG_INFO,"lcp_rejci rejected long opt %d", opt)); \
1066 #define REJCILQR(opt, neg, val) \
1068 len >= CILEN_LQR && \
1069 p[1] == CILEN_LQR && \
1073 GETSHORT(cishort, p); \
1074 GETLONG(cilong, p); \
1075 /* Check rejected value. */ \
1076 if (cishort != PPP_LQR || cilong != val) \
1079 LCPDEBUG((LOG_INFO,"lcp_rejci rejected LQR opt %d", opt)); \
1081 #define REJCICBCP(opt, neg, val) \
1083 len >= CILEN_CBCP && \
1084 p[1] == CILEN_CBCP && \
1086 len -= CILEN_CBCP; \
1088 GETCHAR(cichar, p); \
1089 /* Check rejected value. */ \
1090 if (cichar != val) \
1093 LCPDEBUG((LOG_INFO,"lcp_rejci rejected Callback opt %d", opt)); \
1096 REJCISHORT(CI_MRU, neg_mru, go->mru);
1097 REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1098 REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1099 if (!go->neg_chap) {
1100 REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1102 REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1103 REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1104 REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1105 REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1106 REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1109 * If there are any remaining CIs, then this packet is bad.
1114 * Now we can update state.
1116 if (f->state != OPENED)
1121 LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!"));
1127 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1129 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1130 * appropriately. If reject_if_disagree is non-zero, doesn't return
1131 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1134 lcp_reqci(f, inp, lenp, reject_if_disagree)
1136 u_char *inp; /* Requested CIs */
1137 int *lenp; /* Length of requested CIs */
1138 int reject_if_disagree;
1140 lcp_options *go = &lcp_gotoptions[f->unit];
1141 lcp_options *ho = &lcp_hisoptions[f->unit];
1142 lcp_options *ao = &lcp_allowoptions[f->unit];
1143 u_char *cip, *next; /* Pointer to current and next CIs */
1144 int cilen, citype, cichar; /* Parsed len, type, char value */
1145 u_short cishort; /* Parsed short value */
1146 u_int32_t cilong; /* Parse long value */
1147 int rc = CONFACK; /* Final packet return code */
1148 int orc; /* Individual option return code */
1149 u_char *p; /* Pointer to next char to parse */
1150 u_char *rejp; /* Pointer to next char in reject frame */
1151 u_char *nakp; /* Pointer to next char in Nak frame */
1152 int l = *lenp; /* Length left */
1155 * Reset all his options.
1157 BZERO(ho, sizeof(*ho));
1160 * Process all his options.
1166 orc = CONFACK; /* Assume success */
1167 cip = p = next; /* Remember begining of CI */
1168 if (l < 2 || /* Not enough data for CI header or */
1169 p[1] < 2 || /* CI length too small or */
1170 p[1] > l) { /* CI length too big? */
1171 LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!"));
1172 orc = CONFREJ; /* Reject bad CI */
1173 cilen = l; /* Reject till end of packet */
1174 l = 0; /* Don't loop again */
1178 GETCHAR(citype, p); /* Parse CI type */
1179 GETCHAR(cilen, p); /* Parse CI length */
1180 l -= cilen; /* Adjust remaining length */
1181 next += cilen; /* Step to next CI */
1183 switch (citype) { /* Check CI type */
1185 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MRU"));
1186 if (!ao->neg_mru || /* Allow option? */
1187 cilen != CILEN_SHORT) { /* Check CI length */
1188 orc = CONFREJ; /* Reject CI */
1191 GETSHORT(cishort, p); /* Parse MRU */
1192 LCPDEBUG((LOG_INFO, "(%d)", cishort));
1195 * He must be able to receive at least our minimum.
1196 * No need to check a maximum. If he sends a large number,
1197 * we'll just ignore it.
1199 if (cishort < MINMRU) {
1200 orc = CONFNAK; /* Nak CI */
1201 PUTCHAR(CI_MRU, nakp);
1202 PUTCHAR(CILEN_SHORT, nakp);
1203 PUTSHORT(MINMRU, nakp); /* Give him a hint */
1206 ho->neg_mru = 1; /* Remember he sent MRU */
1207 ho->mru = cishort; /* And remember value */
1211 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ASYNCMAP"));
1212 if (!ao->neg_asyncmap ||
1213 cilen != CILEN_LONG) {
1218 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1221 * Asyncmap must have set at least the bits
1222 * which are set in lcp_allowoptions[unit].asyncmap.
1224 if ((ao->asyncmap & ~cilong) != 0) {
1226 PUTCHAR(CI_ASYNCMAP, nakp);
1227 PUTCHAR(CILEN_LONG, nakp);
1228 PUTLONG(ao->asyncmap | cilong, nakp);
1231 ho->neg_asyncmap = 1;
1232 ho->asyncmap = cilong;
1236 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd AUTHTYPE"));
1237 if (cilen < CILEN_SHORT ||
1238 !(ao->neg_upap || ao->neg_chap)) {
1240 * Reject the option if we're not willing to authenticate.
1245 GETSHORT(cishort, p);
1246 LCPDEBUG((LOG_INFO, "(%x)", cishort));
1249 * Authtype must be UPAP or CHAP.
1251 * Note: if both ao->neg_upap and ao->neg_chap are set,
1252 * and the peer sends a Configure-Request with two
1253 * authenticate-protocol requests, one for CHAP and one
1254 * for UPAP, then we will reject the second request.
1255 * Whether we end up doing CHAP or UPAP depends then on
1256 * the ordering of the CIs in the peer's Configure-Request.
1259 if (cishort == PPP_PAP) {
1260 if (ho->neg_chap || /* we've already accepted CHAP */
1261 cilen != CILEN_SHORT) {
1262 LCPDEBUG((LOG_WARNING,
1263 "lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1267 if (!ao->neg_upap) { /* we don't want to do PAP */
1268 orc = CONFNAK; /* NAK it and suggest CHAP */
1269 PUTCHAR(CI_AUTHTYPE, nakp);
1270 PUTCHAR(CILEN_CHAP, nakp);
1271 PUTSHORT(PPP_CHAP, nakp);
1272 PUTCHAR(ao->chap_mdtype, nakp);
1278 if (cishort == PPP_CHAP) {
1279 if (ho->neg_upap || /* we've already accepted PAP */
1280 cilen != CILEN_CHAP) {
1282 "lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1286 if (!ao->neg_chap) { /* we don't want to do CHAP */
1287 orc = CONFNAK; /* NAK it and suggest PAP */
1288 PUTCHAR(CI_AUTHTYPE, nakp);
1289 PUTCHAR(CILEN_SHORT, nakp);
1290 PUTSHORT(PPP_PAP, nakp);
1293 GETCHAR(cichar, p); /* get digest type*/
1294 if (cichar != CHAP_DIGEST_MD5
1296 && cichar != CHAP_MICROSOFT
1300 PUTCHAR(CI_AUTHTYPE, nakp);
1301 PUTCHAR(CILEN_CHAP, nakp);
1302 PUTSHORT(PPP_CHAP, nakp);
1303 PUTCHAR(ao->chap_mdtype, nakp);
1306 ho->chap_mdtype = cichar; /* save md type */
1312 * We don't recognize the protocol they're asking for.
1313 * Nak it with something we're willing to do.
1314 * (At this point we know ao->neg_upap || ao->neg_chap.)
1317 PUTCHAR(CI_AUTHTYPE, nakp);
1319 PUTCHAR(CILEN_CHAP, nakp);
1320 PUTSHORT(PPP_CHAP, nakp);
1321 PUTCHAR(ao->chap_mdtype, nakp);
1323 PUTCHAR(CILEN_SHORT, nakp);
1324 PUTSHORT(PPP_PAP, nakp);
1329 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd QUALITY"));
1331 cilen != CILEN_LQR) {
1336 GETSHORT(cishort, p);
1338 LCPDEBUG((LOG_INFO, "(%x %x)", cishort, (unsigned int) cilong));
1341 * Check the protocol and the reporting period.
1342 * XXX When should we Nak this, and what with?
1344 if (cishort != PPP_LQR) {
1346 PUTCHAR(CI_QUALITY, nakp);
1347 PUTCHAR(CILEN_LQR, nakp);
1348 PUTSHORT(PPP_LQR, nakp);
1349 PUTLONG(ao->lqr_period, nakp);
1354 case CI_MAGICNUMBER:
1355 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd MAGICNUMBER"));
1356 if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1357 cilen != CILEN_LONG) {
1362 LCPDEBUG((LOG_INFO, "(%x)", (unsigned int) cilong));
1365 * He must have a different magic number.
1367 if (go->neg_magicnumber &&
1368 cilong == go->magicnumber) {
1369 cilong = magic(); /* Don't put magic() inside macro! */
1371 PUTCHAR(CI_MAGICNUMBER, nakp);
1372 PUTCHAR(CILEN_LONG, nakp);
1373 PUTLONG(cilong, nakp);
1376 ho->neg_magicnumber = 1;
1377 ho->magicnumber = cilong;
1381 case CI_PCOMPRESSION:
1382 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd PCOMPRESSION"));
1383 if (!ao->neg_pcompression ||
1384 cilen != CILEN_VOID) {
1388 ho->neg_pcompression = 1;
1391 case CI_ACCOMPRESSION:
1392 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd ACCOMPRESSION"));
1393 if (!ao->neg_accompression ||
1394 cilen != CILEN_VOID) {
1398 ho->neg_accompression = 1;
1402 LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd unknown option %d",
1409 LCPDEBUG((LOG_INFO, " (%s)", CODENAME(orc)));
1410 if (orc == CONFACK && /* Good CI */
1411 rc != CONFACK) /* but prior CI wasnt? */
1412 continue; /* Don't send this one */
1414 if (orc == CONFNAK) { /* Nak this CI? */
1415 if (reject_if_disagree /* Getting fed up with sending NAKs? */
1416 && citype != CI_MAGICNUMBER) {
1417 orc = CONFREJ; /* Get tough if so */
1419 if (rc == CONFREJ) /* Rejecting prior CI? */
1420 continue; /* Don't send this one */
1424 if (orc == CONFREJ) { /* Reject this CI */
1426 if (cip != rejp) /* Need to move rejected CI? */
1427 BCOPY(cip, rejp, cilen); /* Move it */
1428 INCPTR(cilen, rejp); /* Update output pointer */
1433 * If we wanted to send additional NAKs (for unsent CIs), the
1434 * code would go here. The extra NAKs would go at *nakp.
1435 * At present there are no cases where we want to ask the
1436 * peer to negotiate an option.
1445 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1447 *lenp = nakp - nak_buffer;
1448 BCOPY(nak_buffer, inp, *lenp);
1455 LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.", CODENAME(rc)));
1456 return (rc); /* Return final code */
1461 * lcp_up - LCP has come UP.
1467 lcp_options *wo = &lcp_wantoptions[f->unit];
1468 lcp_options *ho = &lcp_hisoptions[f->unit];
1469 lcp_options *go = &lcp_gotoptions[f->unit];
1470 lcp_options *ao = &lcp_allowoptions[f->unit];
1472 if (!go->neg_magicnumber)
1473 go->magicnumber = 0;
1474 if (!ho->neg_magicnumber)
1475 ho->magicnumber = 0;
1478 * Set our MTU to the smaller of the MTU we wanted and
1479 * the MRU our peer wanted. If we negotiated an MRU,
1480 * set our MRU to the larger of value we wanted and
1481 * the value we got in the negotiation.
1483 ppp_send_config(f->unit, MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1484 (ho->neg_asyncmap? ho->asyncmap: 0xffffffff),
1485 ho->neg_pcompression, ho->neg_accompression);
1487 * If the asyncmap hasn't been negotiated, we really should
1488 * set the receive asyncmap to ffffffff, but we set it to 0
1489 * for backwards contemptibility.
1491 ppp_recv_config(f->unit, (go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU),
1492 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1493 go->neg_pcompression, go->neg_accompression);
1496 peer_mru[f->unit] = ho->mru;
1498 lcp_echo_lowerup(f->unit); /* Enable echo messages */
1500 link_established(f->unit);
1505 * lcp_down - LCP has gone DOWN.
1507 * Alert other protocols.
1513 lcp_options *go = &lcp_gotoptions[f->unit];
1515 lcp_echo_lowerdown(f->unit);
1519 ppp_send_config(f->unit, PPP_MRU, 0xffffffff, 0, 0);
1520 ppp_recv_config(f->unit, PPP_MRU,
1521 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1522 go->neg_pcompression, go->neg_accompression);
1523 peer_mru[f->unit] = PPP_MRU;
1528 * lcp_starting - LCP needs the lower layer up.
1534 link_required(f->unit);
1539 * lcp_finished - LCP has finished with the lower layer.
1545 link_terminated(f->unit);
1550 * lcp_printpkt - print the contents of an LCP packet.
1552 static char *lcp_codenames[] = {
1553 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1554 "TermReq", "TermAck", "CodeRej", "ProtRej",
1555 "EchoReq", "EchoRep", "DiscReq"
1559 lcp_printpkt(p, plen, printer, arg)
1562 void (*printer) __P((void *, char *, ...));
1565 int code, id, len, olen;
1566 u_char *pstart, *optend;
1571 if (plen < HEADERLEN)
1577 if (len < HEADERLEN || len > plen)
1580 if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *))
1581 printer(arg, " %s", lcp_codenames[code-1]);
1583 printer(arg, " code=0x%x", code);
1584 printer(arg, " id=0x%x", id);
1591 /* print option list */
1596 if (olen < 2 || olen > len) {
1604 if (olen == CILEN_SHORT) {
1606 GETSHORT(cishort, p);
1607 printer(arg, "mru %d", cishort);
1611 if (olen == CILEN_LONG) {
1614 printer(arg, "asyncmap 0x%x", cilong);
1618 if (olen >= CILEN_SHORT) {
1620 printer(arg, "auth ");
1621 GETSHORT(cishort, p);
1624 printer(arg, "pap");
1627 printer(arg, "chap");
1630 printer(arg, "0x%x", cishort);
1635 if (olen >= CILEN_SHORT) {
1637 printer(arg, "quality ");
1638 GETSHORT(cishort, p);
1641 printer(arg, "lqr");
1644 printer(arg, "0x%x", cishort);
1649 if (olen >= CILEN_CHAR) {
1651 printer(arg, "callback ");
1652 GETSHORT(cishort, p);
1655 printer(arg, "CBCP");
1658 printer(arg, "0x%x", cishort);
1662 case CI_MAGICNUMBER:
1663 if (olen == CILEN_LONG) {
1666 printer(arg, "magic 0x%x", cilong);
1669 case CI_PCOMPRESSION:
1670 if (olen == CILEN_VOID) {
1672 printer(arg, "pcomp");
1675 case CI_ACCOMPRESSION:
1676 if (olen == CILEN_VOID) {
1678 printer(arg, "accomp");
1682 while (p < optend) {
1684 printer(arg, " %.2x", code);
1692 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1694 print_string(p, len, printer, arg);
1705 printer(arg, " magic=0x%x", cilong);
1712 /* print the rest of the bytes in the packet */
1713 for (; len > 0; --len) {
1715 printer(arg, " %.2x", code);
1722 * Time to shut down the link because there is nothing out there.
1726 void LcpLinkFailure (f)
1729 if (f->state == OPENED) {
1730 syslog(LOG_INFO, "No response to %d echo-requests", lcp_echos_pending);
1731 syslog(LOG_NOTICE, "Serial link appears to be disconnected.");
1732 lcp_close(f->unit, "Peer not responding");
1737 * Timer expired for the LCP echo requests from this process.
1744 LcpSendEchoRequest (f);
1747 * Start the timer for the next interval.
1749 assert (lcp_echo_timer_running==0);
1750 TIMEOUT (LcpEchoTimeout, (caddr_t) f, lcp_echo_interval);
1751 lcp_echo_timer_running = 1;
1755 * LcpEchoTimeout - Timer expired on the LCP echo
1759 LcpEchoTimeout (arg)
1762 if (lcp_echo_timer_running != 0) {
1763 lcp_echo_timer_running = 0;
1764 LcpEchoCheck ((fsm *) arg);
1769 * LcpEchoReply - LCP has received a reply to the echo
1773 lcp_received_echo_reply (f, id, inp, len)
1775 int id; u_char *inp; int len;
1779 /* Check the magic number - don't count replies from ourselves. */
1781 syslog(LOG_DEBUG, "lcp: received short Echo-Reply, length %d", len);
1784 GETLONG(magic, inp);
1785 if (lcp_gotoptions[f->unit].neg_magicnumber
1786 && magic == lcp_gotoptions[f->unit].magicnumber) {
1787 syslog(LOG_WARNING, "appear to have received our own echo-reply!");
1791 /* Reset the number of outstanding echo frames */
1792 lcp_echos_pending = 0;
1796 * LcpSendEchoRequest - Send an echo request frame to the peer
1800 LcpSendEchoRequest (f)
1803 u_int32_t lcp_magic;
1804 u_char pkt[4], *pktp;
1807 * Detect the failure of the peer at this point.
1809 if (lcp_echo_fails != 0) {
1810 if (lcp_echos_pending++ >= lcp_echo_fails) {
1812 lcp_echos_pending = 0;
1817 * Make and send the echo request frame.
1819 if (f->state == OPENED) {
1820 lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1822 PUTLONG(lcp_magic, pktp);
1823 fsm_sdata(f, ECHOREQ, lcp_echo_number++ & 0xFF, pkt, pktp - pkt);
1828 * lcp_echo_lowerup - Start the timer for the LCP frame
1832 lcp_echo_lowerup (unit)
1835 fsm *f = &lcp_fsm[unit];
1837 /* Clear the parameters for generating echo frames */
1838 lcp_echos_pending = 0;
1839 lcp_echo_number = 0;
1840 lcp_echo_timer_running = 0;
1842 /* If a timeout interval is specified then start the timer */
1843 if (lcp_echo_interval != 0)
1848 * lcp_echo_lowerdown - Stop the timer for the LCP frame
1852 lcp_echo_lowerdown (unit)
1855 fsm *f = &lcp_fsm[unit];
1857 if (lcp_echo_timer_running != 0) {
1858 UNTIMEOUT (LcpEchoTimeout, (caddr_t) f);
1859 lcp_echo_timer_running = 0;