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.
20 #define RCSID "$Id: lcp.c,v 1.4 2004/08/12 02:56:55 tallest Exp $"
29 #include <sys/sysinfo.h>
40 static const char rcsid
[] = RCSID
;
43 * When the link comes up we want to be able to wait for a short while,
44 * or until seeing some input from the peer, before starting to send
45 * configure-requests. We do this by delaying the fsm_lowerup call.
47 /* steal a bit in fsm flags word */
48 #define DELAYED_UP 0x100
50 static void lcp_delayed_up
__P((void *));
53 * LCP-related command-line options.
55 int lcp_echo_interval
= 0; /* Interval between LCP echo-requests */
56 int lcp_echo_fails
= 0; /* Tolerance to unanswered echo-requests */
57 bool lax_recv
= 0; /* accept control chars in asyncmap */
58 bool noendpoint
= 0; /* don't send/accept endpoint discriminator */
60 static int noopt
__P((char **));
62 static time_t echo_time
= 0;
65 static int setendpoint
__P((char **));
66 static void printendpoint
__P((option_t
*, void (*)(void *, char *, ...),
68 #endif /* HAVE_MULTILINK */
70 static option_t lcp_option_list
[] = {
72 { "-all", o_special_noarg
, (void *)noopt
,
73 "Don't request/allow any LCP options" },
75 { "noaccomp", o_bool
, &lcp_wantoptions
[0].neg_accompression
,
76 "Disable address/control compression",
77 OPT_A2CLR
, &lcp_allowoptions
[0].neg_accompression
},
78 { "-ac", o_bool
, &lcp_wantoptions
[0].neg_accompression
,
79 "Disable address/control compression",
80 OPT_ALIAS
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_accompression
},
82 { "asyncmap", o_uint32
, &lcp_wantoptions
[0].asyncmap
,
83 "Set asyncmap (for received packets)",
84 OPT_OR
, &lcp_wantoptions
[0].neg_asyncmap
},
85 { "-as", o_uint32
, &lcp_wantoptions
[0].asyncmap
,
86 "Set asyncmap (for received packets)",
87 OPT_ALIAS
| OPT_OR
, &lcp_wantoptions
[0].neg_asyncmap
},
88 { "default-asyncmap", o_uint32
, &lcp_wantoptions
[0].asyncmap
,
89 "Disable asyncmap negotiation",
90 OPT_OR
| OPT_NOARG
| OPT_VAL(~0U) | OPT_A2CLR
,
91 &lcp_allowoptions
[0].neg_asyncmap
},
92 { "-am", o_uint32
, &lcp_wantoptions
[0].asyncmap
,
93 "Disable asyncmap negotiation",
94 OPT_ALIAS
| OPT_OR
| OPT_NOARG
| OPT_VAL(~0U) | OPT_A2CLR
,
95 &lcp_allowoptions
[0].neg_asyncmap
},
97 { "nomagic", o_bool
, &lcp_wantoptions
[0].neg_magicnumber
,
98 "Disable magic number negotiation (looped-back line detection)",
99 OPT_A2CLR
, &lcp_allowoptions
[0].neg_magicnumber
},
100 { "-mn", o_bool
, &lcp_wantoptions
[0].neg_magicnumber
,
101 "Disable magic number negotiation (looped-back line detection)",
102 OPT_ALIAS
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_magicnumber
},
104 { "mru", o_int
, &lcp_wantoptions
[0].mru
,
105 "Set MRU (maximum received packet size) for negotiation",
106 OPT_PRIO
, &lcp_wantoptions
[0].neg_mru
},
107 { "default-mru", o_bool
, &lcp_wantoptions
[0].neg_mru
,
108 "Disable MRU negotiation (use default 1500)",
109 OPT_PRIOSUB
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_mru
},
110 { "-mru", o_bool
, &lcp_wantoptions
[0].neg_mru
,
111 "Disable MRU negotiation (use default 1500)",
112 OPT_ALIAS
| OPT_PRIOSUB
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_mru
},
114 { "mtu", o_int
, &lcp_allowoptions
[0].mru
,
115 "Set our MTU", OPT_LIMITS
, NULL
, MAXMRU
, MINMRU
},
117 { "nopcomp", o_bool
, &lcp_wantoptions
[0].neg_pcompression
,
118 "Disable protocol field compression",
119 OPT_A2CLR
, &lcp_allowoptions
[0].neg_pcompression
},
120 { "-pc", o_bool
, &lcp_wantoptions
[0].neg_pcompression
,
121 "Disable protocol field compression",
122 OPT_ALIAS
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_pcompression
},
124 { "passive", o_bool
, &lcp_wantoptions
[0].passive
,
125 "Set passive mode", 1 },
126 { "-p", o_bool
, &lcp_wantoptions
[0].passive
,
127 "Set passive mode", OPT_ALIAS
| 1 },
129 { "silent", o_bool
, &lcp_wantoptions
[0].silent
,
130 "Set silent mode", 1 },
132 { "lcp-echo-failure", o_int
, &lcp_echo_fails
,
133 "Set number of consecutive echo failures to indicate link failure",
135 { "lcp-echo-interval", o_int
, &lcp_echo_interval
,
136 "Set time in seconds between LCP echo requests", OPT_PRIO
},
137 { "lcp-restart", o_int
, &lcp_fsm
[0].timeouttime
,
138 "Set time in seconds between LCP retransmissions", OPT_PRIO
},
139 { "lcp-max-terminate", o_int
, &lcp_fsm
[0].maxtermtransmits
,
140 "Set maximum number of LCP terminate-request transmissions", OPT_PRIO
},
141 { "lcp-max-configure", o_int
, &lcp_fsm
[0].maxconfreqtransmits
,
142 "Set maximum number of LCP configure-request transmissions", OPT_PRIO
},
143 { "lcp-max-failure", o_int
, &lcp_fsm
[0].maxnakloops
,
144 "Set limit on number of LCP configure-naks", OPT_PRIO
},
146 { "receive-all", o_bool
, &lax_recv
,
147 "Accept all received control characters", 1 },
149 #ifdef HAVE_MULTILINK
150 { "mrru", o_int
, &lcp_wantoptions
[0].mrru
,
151 "Maximum received packet size for multilink bundle",
152 OPT_PRIO
, &lcp_wantoptions
[0].neg_mrru
},
154 { "mpshortseq", o_bool
, &lcp_wantoptions
[0].neg_ssnhf
,
155 "Use short sequence numbers in multilink headers",
156 OPT_PRIO
| 1, &lcp_allowoptions
[0].neg_ssnhf
},
157 { "nompshortseq", o_bool
, &lcp_wantoptions
[0].neg_ssnhf
,
158 "Don't use short sequence numbers in multilink headers",
159 OPT_PRIOSUB
| OPT_A2CLR
, &lcp_allowoptions
[0].neg_ssnhf
},
161 { "endpoint", o_special
, (void *) setendpoint
,
162 "Endpoint discriminator for multilink",
163 OPT_PRIO
| OPT_A2PRINTER
, (void *) printendpoint
},
164 #endif /* HAVE_MULTILINK */
166 { "noendpoint", o_bool
, &noendpoint
,
167 "Don't send or accept multilink endpoint discriminator", 1 },
173 fsm lcp_fsm
[NUM_PPP
]; /* LCP fsm structure (global)*/
174 lcp_options lcp_wantoptions
[NUM_PPP
]; /* Options that we want to request */
175 lcp_options lcp_gotoptions
[NUM_PPP
]; /* Options that peer ack'd */
176 lcp_options lcp_allowoptions
[NUM_PPP
]; /* Options we allow peer to request */
177 lcp_options lcp_hisoptions
[NUM_PPP
]; /* Options that we ack'd */
179 static int lcp_echos_pending
= 0; /* Number of outstanding echo msgs */
180 static int lcp_echo_number
= 0; /* ID number of next echo frame */
181 static int lcp_echo_timer_running
= 0; /* set if a timer is running */
183 static u_char nak_buffer
[PPP_MRU
]; /* where we construct a nak packet */
184 // static int now_unit;
187 * Callbacks for fsm code. (CI = Configuration Information)
189 static void lcp_resetci
__P((fsm
*)); /* Reset our CI */
190 static int lcp_cilen
__P((fsm
*)); /* Return length of our CI */
191 static void lcp_addci
__P((fsm
*, u_char
*, int *)); /* Add our CI to pkt */
192 static int lcp_ackci
__P((fsm
*, u_char
*, int)); /* Peer ack'd our CI */
193 static int lcp_nakci
__P((fsm
*, u_char
*, int)); /* Peer nak'd our CI */
194 static int lcp_rejci
__P((fsm
*, u_char
*, int)); /* Peer rej'd our CI */
195 static int lcp_reqci
__P((fsm
*, u_char
*, int *, int)); /* Rcv peer CI */
196 static void lcp_up
__P((fsm
*)); /* We're UP */
197 static void lcp_down
__P((fsm
*)); /* We're DOWN */
198 static void lcp_starting
__P((fsm
*)); /* We need lower layer up */
199 static void lcp_finished
__P((fsm
*)); /* We need lower layer down */
200 static int lcp_extcode
__P((fsm
*, int, int, u_char
*, int));
201 static void lcp_rprotrej
__P((fsm
*, u_char
*, int));
204 * routines to send LCP echos to peer
207 static void lcp_echo_lowerup
__P((int));
208 static void lcp_echo_lowerdown
__P((int));
209 static void LcpEchoTimeout
__P((void *));
210 static void lcp_received_echo_reply
__P((fsm
*, int, u_char
*, int));
211 static void LcpSendEchoRequest
__P((fsm
*));
212 static void LcpLinkFailure
__P((fsm
*));
213 static void LcpEchoCheck
__P((fsm
*));
215 static fsm_callbacks lcp_callbacks
= { /* LCP callback routines */
216 lcp_resetci
, /* Reset our Configuration Information */
217 lcp_cilen
, /* Length of our Configuration Information */
218 lcp_addci
, /* Add our Configuration Information */
219 lcp_ackci
, /* ACK our Configuration Information */
220 lcp_nakci
, /* NAK our Configuration Information */
221 lcp_rejci
, /* Reject our Configuration Information */
222 lcp_reqci
, /* Request peer's Configuration Information */
223 lcp_up
, /* Called when fsm reaches OPENED state */
224 lcp_down
, /* Called when fsm leaves OPENED state */
225 lcp_starting
, /* Called when we want the lower layer up */
226 lcp_finished
, /* Called when we want the lower layer down */
227 NULL
, /* Called when Protocol-Reject received */
228 NULL
, /* Retransmission is necessary */
229 lcp_extcode
, /* Called to handle LCP-specific codes */
230 "LCP" /* String name of protocol */
234 * Protocol entry points.
235 * Some of these are called directly.
238 static void lcp_init
__P((int));
239 static void lcp_input
__P((int, u_char
*, int));
240 static void lcp_protrej
__P((int));
241 static int lcp_printpkt
__P((u_char
*, int,
242 void (*) __P((void *, char *, ...)), void *));
244 struct protent lcp_protent
= {
264 int lcp_loopbackfail
= DEFLOOPBACKFAIL
;
267 * Length of each type of configuration option (in octets)
271 #define CILEN_SHORT 4 /* CILEN_VOID + 2 */
272 #define CILEN_CHAP 5 /* CILEN_VOID + 2 + 1 */
273 #define CILEN_LONG 6 /* CILEN_VOID + 4 */
274 #define CILEN_LQR 8 /* CILEN_VOID + 2 + 4 */
277 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
278 (x) == CONFNAK ? "NAK" : "REJ")
280 time_t get_time(void)
290 * noopt - Disable all options (why?).
296 BZERO((char *) &lcp_wantoptions
[0], sizeof (struct lcp_options
));
297 BZERO((char *) &lcp_allowoptions
[0], sizeof (struct lcp_options
));
302 #ifdef HAVE_MULTILINK
307 if (str_to_epdisc(&lcp_wantoptions
[0].endpoint
, *argv
)) {
308 lcp_wantoptions
[0].neg_endpoint
= 1;
311 option_error("Can't parse '%s' as an endpoint discriminator", *argv
);
316 printendpoint(opt
, printer
, arg
)
318 void (*printer
) __P((void *, char *, ...));
321 printer(arg
, "%s", epdisc_to_str(&lcp_wantoptions
[0].endpoint
));
323 #endif /* HAVE_MULTILINK */
326 * lcp_init - Initialize LCP.
332 fsm
*f
= &lcp_fsm
[unit
];
333 lcp_options
*wo
= &lcp_wantoptions
[unit
];
334 lcp_options
*ao
= &lcp_allowoptions
[unit
];
337 f
->protocol
= PPP_LCP
;
338 f
->callbacks
= &lcp_callbacks
;
342 BZERO(wo
, sizeof(*wo
));
345 wo
->neg_asyncmap
= 1;
346 wo
->chap_mdtype
= CHAP_DIGEST_MD5
;
347 wo
->neg_magicnumber
= 1;
348 wo
->neg_pcompression
= 1;
349 wo
->neg_accompression
= 1;
351 BZERO(ao
, sizeof(*ao
));
354 ao
->neg_asyncmap
= 1;
356 ao
->chap_mdtype
= CHAP_DIGEST_MD5
;
358 ao
->neg_magicnumber
= 1;
359 ao
->neg_pcompression
= 1;
360 ao
->neg_accompression
= 1;
364 ao
->neg_endpoint
= 1;
369 * lcp_open - LCP is allowed to come up.
375 fsm
*f
= &lcp_fsm
[unit
];
376 lcp_options
*wo
= &lcp_wantoptions
[unit
];
378 f
->flags
&= ~(OPT_PASSIVE
| OPT_SILENT
);
380 f
->flags
|= OPT_PASSIVE
;
382 f
->flags
|= OPT_SILENT
;
388 * lcp_close - Take LCP down.
391 lcp_close(unit
, reason
)
395 fsm
*f
= &lcp_fsm
[unit
];
397 if (phase
!= PHASE_DEAD
)
398 new_phase(PHASE_TERMINATE
);
399 if (f
->state
== STOPPED
&& f
->flags
& (OPT_PASSIVE
|OPT_SILENT
)) {
401 * This action is not strictly according to the FSM in RFC1548,
402 * but it does mean that the program terminates if you do a
403 * lcp_close() in passive/silent mode when a connection hasn't
410 fsm_close(&lcp_fsm
[unit
], reason
);
415 * lcp_lowerup - The lower layer is up.
421 lcp_options
*wo
= &lcp_wantoptions
[unit
];
422 fsm
*f
= &lcp_fsm
[unit
];
425 * Don't use A/C or protocol compression on transmission,
426 * but accept A/C and protocol compressed packets
427 * if we are going to ask for A/C and protocol compression.
429 ppp_send_config(unit
, PPP_MRU
, 0xffffffff, 0, 0);
430 ppp_recv_config(unit
, PPP_MRU
, (lax_recv
? 0: 0xffffffff),
431 wo
->neg_pcompression
, wo
->neg_accompression
);
432 peer_mru
[unit
] = PPP_MRU
;
434 if (listen_time
!= 0) {
435 f
->flags
|= DELAYED_UP
;
436 timeout(lcp_delayed_up
, f
, 0, listen_time
* 1000);
443 * lcp_lowerdown - The lower layer is down.
449 fsm
*f
= &lcp_fsm
[unit
];
451 if (f
->flags
& DELAYED_UP
)
452 f
->flags
&= ~DELAYED_UP
;
454 fsm_lowerdown(&lcp_fsm
[unit
]);
459 * lcp_delayed_up - Bring the lower layer up now.
467 if (f
->flags
& DELAYED_UP
) {
468 f
->flags
&= ~DELAYED_UP
;
475 * lcp_input - Input LCP packet.
478 lcp_input(unit
, p
, len
)
483 fsm
*f
= &lcp_fsm
[unit
];
485 if (f
->flags
& DELAYED_UP
) {
486 f
->flags
&= ~DELAYED_UP
;
489 fsm_input(f
, p
, len
);
494 * lcp_extcode - Handle a LCP-specific code.
497 lcp_extcode(f
, code
, id
, inp
, len
)
505 if ((code
> 0) && (code
< 16) && (code_log_details
[code
][0] != '#')) {
506 LOGX_INFO("Received %s %s", PNAME(f
), code_log_details
[code
]);
511 lcp_rprotrej(f
, inp
, len
);
515 if (f
->state
!= OPENED
)
518 PUTLONG(lcp_gotoptions
[f
->unit
].magicnumber
, magp
);
519 fsm_sdata(f
, ECHOREP
, id
, inp
, len
);
521 /* Reset the number of outstanding echo frames */
522 lcp_echos_pending
= 0;
523 echo_time
= get_time();
527 lcp_received_echo_reply(f
, id
, inp
, len
);
541 * lcp_rprotrej - Receive an Protocol-Reject.
543 * Figure out which protocol is rejected and inform it.
546 lcp_rprotrej(f
, inp
, len
)
552 struct protent
*protp
;
556 LCPDEBUG(("lcp_rprotrej: Rcvd short Protocol-Reject packet!"));
563 * Protocol-Reject packets received in any state other than the LCP
564 * OPENED state SHOULD be silently discarded.
566 if( f
->state
!= OPENED
){
567 LCPDEBUG(("Protocol-Reject discarded: LCP in state %d", f
->state
));
572 * Upcall the proper Protocol-Reject routine.
574 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
575 if (protp
->protocol
== prot
&& protp
->enabled_flag
) {
576 (*protp
->protrej
)(f
->unit
);
580 warn("Protocol-Reject for unsupported protocol 0x%x", prot
);
585 * lcp_protrej - A Protocol-Reject was received.
596 LOGX_ERROR("Received Protocol-Reject for LCP.");
597 error("Received Protocol-Reject for LCP!");
598 fsm_protreject(&lcp_fsm
[unit
]);
603 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
606 lcp_sprotrej(unit
, p
, len
)
612 * Send back the protocol and the information field of the
613 * rejected packet. We only get here if LCP is in the OPENED state.
618 fsm_sdata(&lcp_fsm
[unit
], PROTREJ
, ++lcp_fsm
[unit
].id
,
624 * lcp_resetci - Reset our CI.
630 lcp_options
*wo
= &lcp_wantoptions
[f
->unit
];
631 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
632 lcp_options
*ao
= &lcp_allowoptions
[f
->unit
];
634 wo
->magicnumber
= magic();
640 go
->neg_endpoint
= 0;
643 ao
->neg_endpoint
= 0;
644 peer_mru
[f
->unit
] = PPP_MRU
;
650 * lcp_cilen - Return length of our CI.
656 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
658 #define LENCIVOID(neg) ((neg) ? CILEN_VOID : 0)
659 #define LENCICHAP(neg) ((neg) ? CILEN_CHAP : 0)
660 #define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
661 #define LENCILONG(neg) ((neg) ? CILEN_LONG : 0)
662 #define LENCILQR(neg) ((neg) ? CILEN_LQR: 0)
663 #define LENCICBCP(neg) ((neg) ? CILEN_CBCP: 0)
665 * NB: we only ask for one of CHAP and UPAP, even if we will
668 return (LENCISHORT(go
->neg_mru
&& go
->mru
!= DEFMRU
) +
669 LENCILONG(go
->neg_asyncmap
&& go
->asyncmap
!= 0xFFFFFFFF) +
670 LENCICHAP(go
->neg_chap
) +
671 LENCISHORT(!go
->neg_chap
&& go
->neg_upap
) +
672 LENCILQR(go
->neg_lqr
) +
673 LENCICBCP(go
->neg_cbcp
) +
674 LENCILONG(go
->neg_magicnumber
) +
675 LENCIVOID(go
->neg_pcompression
) +
676 LENCIVOID(go
->neg_accompression
) +
677 LENCISHORT(go
->neg_mrru
) +
678 LENCIVOID(go
->neg_ssnhf
) +
679 (go
->neg_endpoint
? CILEN_CHAR
+ go
->endpoint
.length
: 0));
684 * lcp_addci - Add our desired CIs to a packet.
687 lcp_addci(f
, ucp
, lenp
)
692 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
693 u_char
*start_ucp
= ucp
;
695 #define ADDCIVOID(opt, neg) \
698 PUTCHAR(CILEN_VOID, ucp); \
700 #define ADDCISHORT(opt, neg, val) \
703 PUTCHAR(CILEN_SHORT, ucp); \
704 PUTSHORT(val, ucp); \
706 #define ADDCICHAP(opt, neg, val, digest) \
709 PUTCHAR(CILEN_CHAP, ucp); \
710 PUTSHORT(val, ucp); \
711 PUTCHAR(digest, ucp); \
713 #define ADDCILONG(opt, neg, val) \
716 PUTCHAR(CILEN_LONG, ucp); \
719 #define ADDCILQR(opt, neg, val) \
722 PUTCHAR(CILEN_LQR, ucp); \
723 PUTSHORT(PPP_LQR, ucp); \
726 #define ADDCICHAR(opt, neg, val) \
729 PUTCHAR(CILEN_CHAR, ucp); \
732 #define ADDCIENDP(opt, neg, class, val, len) \
736 PUTCHAR(CILEN_CHAR + len, ucp); \
737 PUTCHAR(class, ucp); \
738 for (i = 0; i < len; ++i) \
739 PUTCHAR(val[i], ucp); \
742 ADDCISHORT(CI_MRU
, go
->neg_mru
&& go
->mru
!= DEFMRU
, go
->mru
);
743 ADDCILONG(CI_ASYNCMAP
, go
->neg_asyncmap
&& go
->asyncmap
!= 0xFFFFFFFF,
745 ADDCICHAP(CI_AUTHTYPE
, go
->neg_chap
, PPP_CHAP
, go
->chap_mdtype
);
746 ADDCISHORT(CI_AUTHTYPE
, !go
->neg_chap
&& go
->neg_upap
, PPP_PAP
);
747 ADDCILQR(CI_QUALITY
, go
->neg_lqr
, go
->lqr_period
);
748 ADDCICHAR(CI_CALLBACK
, go
->neg_cbcp
, CBCP_OPT
);
749 ADDCILONG(CI_MAGICNUMBER
, go
->neg_magicnumber
, go
->magicnumber
);
750 ADDCIVOID(CI_PCOMPRESSION
, go
->neg_pcompression
);
751 ADDCIVOID(CI_ACCOMPRESSION
, go
->neg_accompression
);
752 ADDCISHORT(CI_MRRU
, go
->neg_mrru
, go
->mrru
);
753 ADDCIVOID(CI_SSNHF
, go
->neg_ssnhf
);
754 ADDCIENDP(CI_EPDISC
, go
->neg_endpoint
, go
->endpoint
.class,
755 go
->endpoint
.value
, go
->endpoint
.length
);
757 if (ucp
- start_ucp
!= *lenp
) {
758 /* this should never happen, because peer_mtu should be 1500 */
759 error("Bug in lcp_addci: wrong length");
765 * lcp_ackci - Ack our CIs.
766 * This should not modify any state if the Ack is bad.
778 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
779 u_char cilen
, citype
, cichar
;
784 * CIs must be in exactly the same order that we sent.
785 * Check packet length and CI length at each step.
786 * If we find any deviations, then this packet is bad.
788 #define ACKCIVOID(opt, neg) \
790 if ((len -= CILEN_VOID) < 0) \
792 GETCHAR(citype, p); \
794 if (cilen != CILEN_VOID || \
798 #define ACKCISHORT(opt, neg, val) \
800 if ((len -= CILEN_SHORT) < 0) \
802 GETCHAR(citype, p); \
804 if (cilen != CILEN_SHORT || \
807 GETSHORT(cishort, p); \
808 if (cishort != val) \
811 #define ACKCICHAR(opt, neg, val) \
813 if ((len -= CILEN_CHAR) < 0) \
815 GETCHAR(citype, p); \
817 if (cilen != CILEN_CHAR || \
820 GETCHAR(cichar, p); \
824 #define ACKCICHAP(opt, neg, val, digest) \
826 if ((len -= CILEN_CHAP) < 0) \
828 GETCHAR(citype, p); \
830 if (cilen != CILEN_CHAP || \
833 GETSHORT(cishort, p); \
834 if (cishort != val) \
836 GETCHAR(cichar, p); \
837 if (cichar != digest) \
840 #define ACKCILONG(opt, neg, val) \
842 if ((len -= CILEN_LONG) < 0) \
844 GETCHAR(citype, p); \
846 if (cilen != CILEN_LONG || \
849 GETLONG(cilong, p); \
853 #define ACKCILQR(opt, neg, val) \
855 if ((len -= CILEN_LQR) < 0) \
857 GETCHAR(citype, p); \
859 if (cilen != CILEN_LQR || \
862 GETSHORT(cishort, p); \
863 if (cishort != PPP_LQR) \
865 GETLONG(cilong, p); \
869 #define ACKCIENDP(opt, neg, class, val, vlen) \
872 if ((len -= CILEN_CHAR + vlen) < 0) \
874 GETCHAR(citype, p); \
876 if (cilen != CILEN_CHAR + vlen || \
879 GETCHAR(cichar, p); \
880 if (cichar != class) \
882 for (i = 0; i < vlen; ++i) { \
883 GETCHAR(cichar, p); \
884 if (cichar != val[i]) \
889 ACKCISHORT(CI_MRU
, go
->neg_mru
&& go
->mru
!= DEFMRU
, go
->mru
);
890 ACKCILONG(CI_ASYNCMAP
, go
->neg_asyncmap
&& go
->asyncmap
!= 0xFFFFFFFF,
892 ACKCICHAP(CI_AUTHTYPE
, go
->neg_chap
, PPP_CHAP
, go
->chap_mdtype
);
893 ACKCISHORT(CI_AUTHTYPE
, !go
->neg_chap
&& go
->neg_upap
, PPP_PAP
);
894 ACKCILQR(CI_QUALITY
, go
->neg_lqr
, go
->lqr_period
);
895 ACKCICHAR(CI_CALLBACK
, go
->neg_cbcp
, CBCP_OPT
);
896 ACKCILONG(CI_MAGICNUMBER
, go
->neg_magicnumber
, go
->magicnumber
);
897 ACKCIVOID(CI_PCOMPRESSION
, go
->neg_pcompression
);
898 ACKCIVOID(CI_ACCOMPRESSION
, go
->neg_accompression
);
899 ACKCISHORT(CI_MRRU
, go
->neg_mrru
, go
->mrru
);
900 ACKCIVOID(CI_SSNHF
, go
->neg_ssnhf
);
901 ACKCIENDP(CI_EPDISC
, go
->neg_endpoint
, go
->endpoint
.class,
902 go
->endpoint
.value
, go
->endpoint
.length
);
905 * If there are any remaining CIs, then this packet is bad.
911 LCPDEBUG(("lcp_acki: received bad Ack!"));
917 * lcp_nakci - Peer has sent a NAK for some of our CIs.
918 * This should not modify any state if the Nak is bad
919 * or if LCP is in the OPENED state.
931 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
932 lcp_options
*wo
= &lcp_wantoptions
[f
->unit
];
933 u_char citype
, cichar
, *next
;
936 lcp_options no
; /* options we've seen Naks for */
937 lcp_options
try; /* options to request next time */
941 BZERO(&no
, sizeof(no
));
945 * Any Nak'd CIs must be in exactly the same order that we sent.
946 * Check packet length and CI length at each step.
947 * If we find any deviations, then this packet is bad.
949 #define NAKCIVOID(opt, neg) \
951 len >= CILEN_VOID && \
952 p[1] == CILEN_VOID && \
955 INCPTR(CILEN_VOID, p); \
959 #define NAKCICHAP(opt, neg, code) \
961 len >= CILEN_CHAP && \
962 p[1] == CILEN_CHAP && \
966 GETSHORT(cishort, p); \
967 GETCHAR(cichar, p); \
971 #define NAKCICHAR(opt, neg, code) \
973 len >= CILEN_CHAR && \
974 p[1] == CILEN_CHAR && \
978 GETCHAR(cichar, p); \
982 #define NAKCISHORT(opt, neg, code) \
984 len >= CILEN_SHORT && \
985 p[1] == CILEN_SHORT && \
987 len -= CILEN_SHORT; \
989 GETSHORT(cishort, p); \
993 #define NAKCILONG(opt, neg, code) \
995 len >= CILEN_LONG && \
996 p[1] == CILEN_LONG && \
1000 GETLONG(cilong, p); \
1004 #define NAKCILQR(opt, neg, code) \
1006 len >= CILEN_LQR && \
1007 p[1] == CILEN_LQR && \
1011 GETSHORT(cishort, p); \
1012 GETLONG(cilong, p); \
1016 #define NAKCIENDP(opt, neg) \
1018 len >= CILEN_CHAR && \
1020 p[1] >= CILEN_CHAR && \
1029 * We don't care if they want to send us smaller packets than
1030 * we want. Therefore, accept any MRU less than what we asked for,
1031 * but then ignore the new value when setting the MRU in the kernel.
1032 * If they send us a bigger MRU than what we asked, accept it, up to
1033 * the limit of the default MRU we'd get if we didn't negotiate.
1035 if (go
->neg_mru
&& go
->mru
!= DEFMRU
) {
1036 NAKCISHORT(CI_MRU
, neg_mru
,
1037 if (cishort
<= wo
->mru
|| cishort
<= DEFMRU
)
1043 * Add any characters they want to our (receive-side) asyncmap.
1045 if (go
->neg_asyncmap
&& go
->asyncmap
!= 0xFFFFFFFF) {
1046 NAKCILONG(CI_ASYNCMAP
, neg_asyncmap
,
1047 try.asyncmap
= go
->asyncmap
| cilong
;
1052 * If they've nak'd our authentication-protocol, check whether
1053 * they are proposing a different protocol, or a different
1054 * hash algorithm for CHAP.
1056 if ((go
->neg_chap
|| go
->neg_upap
)
1057 && len
>= CILEN_SHORT
1058 && p
[0] == CI_AUTHTYPE
&& p
[1] >= CILEN_SHORT
&& p
[1] <= len
) {
1061 no
.neg_chap
= go
->neg_chap
;
1062 no
.neg_upap
= go
->neg_upap
;
1064 GETSHORT(cishort
, p
);
1065 if (cishort
== PPP_PAP
&& cilen
== CILEN_SHORT
) {
1067 * If we were asking for CHAP, they obviously don't want to do it.
1068 * If we weren't asking for CHAP, then we were asking for PAP,
1069 * in which case this Nak is bad.
1075 } else if (cishort
== PPP_CHAP
&& cilen
== CILEN_CHAP
) {
1079 * We were asking for CHAP/MD5; they must want a different
1080 * algorithm. If they can't do MD5, we can ask for M$-CHAP
1081 * if we support it, otherwise we'll have to stop
1084 if (cichar
!= go
->chap_mdtype
) {
1086 if (cichar
== CHAP_MICROSOFT
)
1087 go
->chap_mdtype
= CHAP_MICROSOFT
;
1094 * Stop asking for PAP if we were asking for it.
1101 * We don't recognize what they're suggesting.
1102 * Stop asking for what we were asking for.
1108 p
+= cilen
- CILEN_SHORT
;
1112 NAKCILQR(CI_QUALITY
, neg_lqr
,
1113 if (cishort
!= PPP_LQR
)
1116 try.lqr_period
= cilong
;
1120 * Only implementing CBCP...not the rest of the callback options
1122 NAKCICHAR(CI_CALLBACK
, neg_cbcp
,
1127 * Check for a looped-back line.
1129 NAKCILONG(CI_MAGICNUMBER
, neg_magicnumber
,
1130 try.magicnumber
= magic();
1135 * Peer shouldn't send Nak for protocol compression or
1136 * address/control compression requests; they should send
1137 * a Reject instead. If they send a Nak, treat it as a Reject.
1139 NAKCIVOID(CI_PCOMPRESSION
, neg_pcompression
);
1140 NAKCIVOID(CI_ACCOMPRESSION
, neg_accompression
);
1143 * Nak for MRRU option - accept their value if it is smaller
1144 * than the one we want.
1147 NAKCISHORT(CI_MRRU
, neg_mrru
,
1148 if (cishort
<= wo
->mrru
)
1154 * Nak for short sequence numbers shouldn't be sent, treat it
1157 NAKCIVOID(CI_SSNHF
, neg_ssnhf
);
1160 * Nak of the endpoint discriminator option is not permitted,
1161 * treat it like a reject.
1163 NAKCIENDP(CI_EPDISC
, neg_endpoint
);
1166 * There may be remaining CIs, if the peer is requesting negotiation
1167 * on an option that we didn't include in our request packet.
1168 * If we see an option that we requested, or one we've already seen
1169 * in this packet, then this packet is bad.
1170 * If we wanted to respond by starting to negotiate on the requested
1171 * option(s), we could, but we don't, because except for the
1172 * authentication type and quality protocol, if we are not negotiating
1173 * an option, it is because we were told not to.
1174 * For the authentication type, the Nak from the peer means
1175 * `let me authenticate myself with you' which is a bit pointless.
1176 * For the quality protocol, the Nak means `ask me to send you quality
1177 * reports', but if we didn't ask for them, we don't want them.
1178 * An option we don't recognize represents the peer asking to
1179 * negotiate some option we don't support, so ignore it.
1181 while (len
> CILEN_VOID
) {
1184 if (cilen
< CILEN_VOID
|| (len
-= cilen
) < 0)
1186 next
= p
+ cilen
- 2;
1190 if ((go
->neg_mru
&& go
->mru
!= DEFMRU
)
1191 || no
.neg_mru
|| cilen
!= CILEN_SHORT
)
1193 GETSHORT(cishort
, p
);
1194 if (cishort
< DEFMRU
) {
1200 if ((go
->neg_asyncmap
&& go
->asyncmap
!= 0xFFFFFFFF)
1201 || no
.neg_asyncmap
|| cilen
!= CILEN_LONG
)
1205 if (go
->neg_chap
|| no
.neg_chap
|| go
->neg_upap
|| no
.neg_upap
)
1208 case CI_MAGICNUMBER
:
1209 if (go
->neg_magicnumber
|| no
.neg_magicnumber
||
1210 cilen
!= CILEN_LONG
)
1213 case CI_PCOMPRESSION
:
1214 if (go
->neg_pcompression
|| no
.neg_pcompression
1215 || cilen
!= CILEN_VOID
)
1218 case CI_ACCOMPRESSION
:
1219 if (go
->neg_accompression
|| no
.neg_accompression
1220 || cilen
!= CILEN_VOID
)
1224 if (go
->neg_lqr
|| no
.neg_lqr
|| cilen
!= CILEN_LQR
)
1228 if (go
->neg_mrru
|| no
.neg_mrru
|| cilen
!= CILEN_SHORT
)
1232 if (go
->neg_ssnhf
|| no
.neg_ssnhf
|| cilen
!= CILEN_VOID
)
1237 if (go
->neg_endpoint
|| no
.neg_endpoint
|| cilen
< CILEN_CHAR
)
1245 * OK, the Nak is good. Now we can update state.
1246 * If there are any options left we ignore them.
1248 if (f
->state
!= OPENED
) {
1250 if (++try.numloops
>= lcp_loopbackfail
) {
1251 // LOGX_INFO("Serial line is looped back.");
1252 notice("Serial line is looped back.");
1253 lcp_close(f
->unit
, "Loopback detected");
1254 status
= EXIT_LOOPBACK
;
1264 LCPDEBUG(("lcp_nakci: received bad Nak!"));
1270 * lcp_rejci - Peer has Rejected some of our CIs.
1271 * This should not modify any state if the Reject is bad
1272 * or if LCP is in the OPENED state.
1275 * 0 - Reject was bad.
1276 * 1 - Reject was good.
1279 lcp_rejci(f
, p
, len
)
1284 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
1288 lcp_options
try; /* options to request next time */
1293 * Any Rejected CIs must be in exactly the same order that we sent.
1294 * Check packet length and CI length at each step.
1295 * If we find any deviations, then this packet is bad.
1297 #define REJCIVOID(opt, neg) \
1299 len >= CILEN_VOID && \
1300 p[1] == CILEN_VOID && \
1302 len -= CILEN_VOID; \
1303 INCPTR(CILEN_VOID, p); \
1306 #define REJCISHORT(opt, neg, val) \
1308 len >= CILEN_SHORT && \
1309 p[1] == CILEN_SHORT && \
1311 len -= CILEN_SHORT; \
1313 GETSHORT(cishort, p); \
1314 /* Check rejected value. */ \
1315 if (cishort != val) \
1319 #define REJCICHAP(opt, neg, val, digest) \
1321 len >= CILEN_CHAP && \
1322 p[1] == CILEN_CHAP && \
1324 len -= CILEN_CHAP; \
1326 GETSHORT(cishort, p); \
1327 GETCHAR(cichar, p); \
1328 /* Check rejected value. */ \
1329 if (cishort != val || cichar != digest) \
1334 #define REJCILONG(opt, neg, val) \
1336 len >= CILEN_LONG && \
1337 p[1] == CILEN_LONG && \
1339 len -= CILEN_LONG; \
1341 GETLONG(cilong, p); \
1342 /* Check rejected value. */ \
1343 if (cilong != val) \
1347 #define REJCILQR(opt, neg, val) \
1349 len >= CILEN_LQR && \
1350 p[1] == CILEN_LQR && \
1354 GETSHORT(cishort, p); \
1355 GETLONG(cilong, p); \
1356 /* Check rejected value. */ \
1357 if (cishort != PPP_LQR || cilong != val) \
1361 #define REJCICBCP(opt, neg, val) \
1363 len >= CILEN_CBCP && \
1364 p[1] == CILEN_CBCP && \
1366 len -= CILEN_CBCP; \
1368 GETCHAR(cichar, p); \
1369 /* Check rejected value. */ \
1370 if (cichar != val) \
1374 #define REJCIENDP(opt, neg, class, val, vlen) \
1376 len >= CILEN_CHAR + vlen && \
1378 p[1] == CILEN_CHAR + vlen) { \
1380 len -= CILEN_CHAR + vlen; \
1382 GETCHAR(cichar, p); \
1383 if (cichar != class) \
1385 for (i = 0; i < vlen; ++i) { \
1386 GETCHAR(cichar, p); \
1387 if (cichar != val[i]) \
1393 REJCISHORT(CI_MRU
, neg_mru
, go
->mru
);
1394 REJCILONG(CI_ASYNCMAP
, neg_asyncmap
, go
->asyncmap
);
1395 REJCICHAP(CI_AUTHTYPE
, neg_chap
, PPP_CHAP
, go
->chap_mdtype
);
1396 if (!go
->neg_chap
) {
1397 REJCISHORT(CI_AUTHTYPE
, neg_upap
, PPP_PAP
);
1399 REJCILQR(CI_QUALITY
, neg_lqr
, go
->lqr_period
);
1400 REJCICBCP(CI_CALLBACK
, neg_cbcp
, CBCP_OPT
);
1401 REJCILONG(CI_MAGICNUMBER
, neg_magicnumber
, go
->magicnumber
);
1402 REJCIVOID(CI_PCOMPRESSION
, neg_pcompression
);
1403 REJCIVOID(CI_ACCOMPRESSION
, neg_accompression
);
1404 REJCISHORT(CI_MRRU
, neg_mrru
, go
->mrru
);
1405 REJCIVOID(CI_SSNHF
, neg_ssnhf
);
1406 REJCIENDP(CI_EPDISC
, neg_endpoint
, go
->endpoint
.class,
1407 go
->endpoint
.value
, go
->endpoint
.length
);
1410 * If there are any remaining CIs, then this packet is bad.
1415 * Now we can update state.
1417 if (f
->state
!= OPENED
)
1422 LCPDEBUG(("lcp_rejci: received bad Reject!"));
1428 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1430 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1431 * appropriately. If reject_if_disagree is non-zero, doesn't return
1432 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1435 lcp_reqci(f
, inp
, lenp
, reject_if_disagree
)
1437 u_char
*inp
; /* Requested CIs */
1438 int *lenp
; /* Length of requested CIs */
1439 int reject_if_disagree
;
1441 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
1442 lcp_options
*ho
= &lcp_hisoptions
[f
->unit
];
1443 lcp_options
*ao
= &lcp_allowoptions
[f
->unit
];
1444 u_char
*cip
, *next
; /* Pointer to current and next CIs */
1445 int cilen
, citype
, cichar
; /* Parsed len, type, char value */
1446 u_short cishort
; /* Parsed short value */
1447 u_int32_t cilong
; /* Parse long value */
1448 int rc
= CONFACK
; /* Final packet return code */
1449 int orc
; /* Individual option return code */
1450 u_char
*p
; /* Pointer to next char to parse */
1451 u_char
*rejp
; /* Pointer to next char in reject frame */
1452 u_char
*nakp
; /* Pointer to next char in Nak frame */
1453 int l
= *lenp
; /* Length left */
1456 * Reset all his options.
1458 BZERO(ho
, sizeof(*ho
));
1461 * Process all his options.
1467 orc
= CONFACK
; /* Assume success */
1468 cip
= p
= next
; /* Remember begining of CI */
1469 if (l
< 2 || /* Not enough data for CI header or */
1470 p
[1] < 2 || /* CI length too small or */
1471 p
[1] > l
) { /* CI length too big? */
1472 LCPDEBUG(("lcp_reqci: bad CI length!"));
1473 orc
= CONFREJ
; /* Reject bad CI */
1474 cilen
= l
; /* Reject till end of packet */
1475 l
= 0; /* Don't loop again */
1479 GETCHAR(citype
, p
); /* Parse CI type */
1480 GETCHAR(cilen
, p
); /* Parse CI length */
1481 l
-= cilen
; /* Adjust remaining length */
1482 next
+= cilen
; /* Step to next CI */
1484 switch (citype
) { /* Check CI type */
1485 #if 1 //fixed by crazy 20070613: bug id unknown(20070613)
1486 //FIXME: Maybe we can define 0 and 9 in the head file...
1487 // Please see line 74 on sheet 'WAN_Broadband' in document 'WG414-Q-LS_V1.0 Product Verification Plan(IN).xls'.
1488 case 0: /* Reserved */
1489 /* Same as case 9, send a Configure-Reject */
1490 // Please see line 109 on sheet 'WAN_Broadband' in document 'WG414-Q-LS_V1.0 Product Verification Plan(IN).xls'.
1491 case 9: /* Field Check Sequence (FCS) Alternatives */
1493 LOGX_DEBUG("%s: citype=%d", __FUNCTION__
, citype
);
1497 if (!ao
->neg_mru
|| /* Allow option? */
1498 cilen
!= CILEN_SHORT
) { /* Check CI length */
1499 orc
= CONFREJ
; /* Reject CI */
1502 GETSHORT(cishort
, p
); /* Parse MRU */
1505 * He must be able to receive at least our minimum.
1506 * No need to check a maximum. If he sends a large number,
1507 * we'll just ignore it.
1509 if (cishort
< MINMRU
) {
1510 orc
= CONFNAK
; /* Nak CI */
1511 PUTCHAR(CI_MRU
, nakp
);
1512 PUTCHAR(CILEN_SHORT
, nakp
);
1513 PUTSHORT(MINMRU
, nakp
); /* Give him a hint */
1516 #if 1 //Fixed by crazy 20070424: bug id 6439(20070423)
1517 if (cishort
> REJECT_MAXMRU
) {
1519 orc
= CONFREJ
; /* Reject CI */
1521 //Fix by crazy 20080121: The Test Plan is updated. Please refer to the item 1.3.13 in latest Test Plan.
1522 orc
= CONFNAK
; /* Nak CI */
1523 PUTCHAR(CI_MRU
, nakp
);
1524 PUTCHAR(CILEN_SHORT
, nakp
);
1525 PUTSHORT(REJECT_MAXMRU
, nakp
); /* Give him a hint */
1527 LOGX_DEBUG("%s: cishort(%d) > REJECT_MAXMRU", __FUNCTION__
, cishort
);
1531 ho
->neg_mru
= 1; /* Remember he sent MRU */
1532 ho
->mru
= cishort
; /* And remember value */
1536 if (!ao
->neg_asyncmap
||
1537 cilen
!= CILEN_LONG
) {
1544 * Asyncmap must have set at least the bits
1545 * which are set in lcp_allowoptions[unit].asyncmap.
1547 if ((ao
->asyncmap
& ~cilong
) != 0) {
1549 PUTCHAR(CI_ASYNCMAP
, nakp
);
1550 PUTCHAR(CILEN_LONG
, nakp
);
1551 PUTLONG(ao
->asyncmap
| cilong
, nakp
);
1554 ho
->neg_asyncmap
= 1;
1555 ho
->asyncmap
= cilong
;
1559 if (cilen
< CILEN_SHORT
||
1560 !(ao
->neg_upap
|| ao
->neg_chap
)) {
1562 * Reject the option if we're not willing to authenticate.
1567 GETSHORT(cishort
, p
);
1570 * Authtype must be PAP or CHAP.
1572 * Note: if both ao->neg_upap and ao->neg_chap are set,
1573 * and the peer sends a Configure-Request with two
1574 * authenticate-protocol requests, one for CHAP and one
1575 * for UPAP, then we will reject the second request.
1576 * Whether we end up doing CHAP or UPAP depends then on
1577 * the ordering of the CIs in the peer's Configure-Request.
1580 if (cishort
== PPP_PAP
) {
1581 if (ho
->neg_chap
|| /* we've already accepted CHAP */
1582 cilen
!= CILEN_SHORT
) {
1583 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE PAP, rejecting..."));
1587 if (!ao
->neg_upap
) { /* we don't want to do PAP */
1588 orc
= CONFNAK
; /* NAK it and suggest CHAP */
1589 PUTCHAR(CI_AUTHTYPE
, nakp
);
1590 PUTCHAR(CILEN_CHAP
, nakp
);
1591 PUTSHORT(PPP_CHAP
, nakp
);
1592 PUTCHAR(ao
->chap_mdtype
, nakp
);
1598 if (cishort
== PPP_CHAP
) {
1599 if (ho
->neg_upap
|| /* we've already accepted PAP */
1600 cilen
!= CILEN_CHAP
) {
1601 LCPDEBUG(("lcp_reqci: rcvd AUTHTYPE CHAP, rejecting..."));
1605 if (!ao
->neg_chap
) { /* we don't want to do CHAP */
1606 orc
= CONFNAK
; /* NAK it and suggest PAP */
1607 PUTCHAR(CI_AUTHTYPE
, nakp
);
1608 PUTCHAR(CILEN_SHORT
, nakp
);
1609 PUTSHORT(PPP_PAP
, nakp
);
1612 GETCHAR(cichar
, p
); /* get digest type*/
1613 if (cichar
!= CHAP_DIGEST_MD5
1615 && cichar
!= CHAP_MICROSOFT
1619 PUTCHAR(CI_AUTHTYPE
, nakp
);
1620 PUTCHAR(CILEN_CHAP
, nakp
);
1621 PUTSHORT(PPP_CHAP
, nakp
);
1622 PUTCHAR(ao
->chap_mdtype
, nakp
);
1625 ho
->chap_mdtype
= cichar
; /* save md type */
1631 * We don't recognize the protocol they're asking for.
1632 * Nak it with something we're willing to do.
1633 * (At this point we know ao->neg_upap || ao->neg_chap.)
1636 PUTCHAR(CI_AUTHTYPE
, nakp
);
1638 PUTCHAR(CILEN_CHAP
, nakp
);
1639 PUTSHORT(PPP_CHAP
, nakp
);
1640 PUTCHAR(ao
->chap_mdtype
, nakp
);
1642 PUTCHAR(CILEN_SHORT
, nakp
);
1643 PUTSHORT(PPP_PAP
, nakp
);
1649 cilen
!= CILEN_LQR
) {
1654 GETSHORT(cishort
, p
);
1657 if (cishort
!= PPP_LQR
) {
1659 PUTCHAR(CI_QUALITY
, nakp
);
1660 PUTCHAR(CILEN_LQR
, nakp
);
1661 PUTSHORT(PPP_LQR
, nakp
);
1662 PUTLONG(ao
->lqr_period
, nakp
);
1667 case CI_MAGICNUMBER
:
1668 if (!(ao
->neg_magicnumber
|| go
->neg_magicnumber
) ||
1669 cilen
!= CILEN_LONG
) {
1676 * He must have a different magic number.
1678 if (go
->neg_magicnumber
&&
1679 cilong
== go
->magicnumber
) {
1680 cilong
= magic(); /* Don't put magic() inside macro! */
1682 PUTCHAR(CI_MAGICNUMBER
, nakp
);
1683 PUTCHAR(CILEN_LONG
, nakp
);
1684 PUTLONG(cilong
, nakp
);
1687 ho
->neg_magicnumber
= 1;
1688 ho
->magicnumber
= cilong
;
1692 case CI_PCOMPRESSION
:
1693 if (!ao
->neg_pcompression
||
1694 cilen
!= CILEN_VOID
) {
1698 ho
->neg_pcompression
= 1;
1701 case CI_ACCOMPRESSION
:
1702 if (!ao
->neg_accompression
||
1703 cilen
!= CILEN_VOID
) {
1707 ho
->neg_accompression
= 1;
1711 if (!ao
->neg_mrru
|| !multilink
||
1712 cilen
!= CILEN_SHORT
) {
1717 GETSHORT(cishort
, p
);
1718 /* possibly should insist on a minimum/maximum MRRU here */
1724 if (!ao
->neg_ssnhf
|| !multilink
||
1725 cilen
!= CILEN_VOID
) {
1733 if (!ao
->neg_endpoint
||
1734 cilen
< CILEN_CHAR
||
1735 cilen
> CILEN_CHAR
+ MAX_ENDP_LEN
) {
1740 cilen
-= CILEN_CHAR
;
1741 ho
->neg_endpoint
= 1;
1742 ho
->endpoint
.class = cichar
;
1743 ho
->endpoint
.length
= cilen
;
1744 BCOPY(p
, ho
->endpoint
.value
, cilen
);
1749 LCPDEBUG(("lcp_reqci: rcvd unknown option %d", citype
));
1750 #if 1 //fixed by crazy 20070424: bug id 6438(20070423)
1751 //Fix by crazy 20071102 (Bug id IR-B0003328): This issue is reported by CRDC. Please refer to the email.
1754 //FIXME: maybe it is a wrong packet...
1755 orc
= CONFNAK
; //Received an unknown option type(like 0xFF), send a LCP Configure-Nak message.
1756 LOGX_DEBUG("%s: unknown option %d", __FUNCTION__
, citype
);
1762 if (orc
== CONFACK
&& /* Good CI */
1763 rc
!= CONFACK
) /* but prior CI wasnt? */
1764 continue; /* Don't send this one */
1766 if (orc
== CONFNAK
) { /* Nak this CI? */
1767 if (reject_if_disagree
/* Getting fed up with sending NAKs? */
1768 && citype
!= CI_MAGICNUMBER
) {
1769 orc
= CONFREJ
; /* Get tough if so */
1771 if (rc
== CONFREJ
) /* Rejecting prior CI? */
1772 continue; /* Don't send this one */
1776 if (orc
== CONFREJ
) { /* Reject this CI */
1778 if (cip
!= rejp
) /* Need to move rejected CI? */
1779 BCOPY(cip
, rejp
, cilen
); /* Move it */
1780 INCPTR(cilen
, rejp
); /* Update output pointer */
1785 * If we wanted to send additional NAKs (for unsent CIs), the
1786 * code would go here. The extra NAKs would go at *nakp.
1787 * At present there are no cases where we want to ask the
1788 * peer to negotiate an option.
1797 * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1799 *lenp
= nakp
- nak_buffer
;
1800 BCOPY(nak_buffer
, inp
, *lenp
);
1807 LCPDEBUG(("lcp_reqci: returning CONF%s.", CODENAME(rc
)));
1808 return (rc
); /* Return final code */
1813 * lcp_up - LCP has come UP.
1819 lcp_options
*wo
= &lcp_wantoptions
[f
->unit
];
1820 lcp_options
*ho
= &lcp_hisoptions
[f
->unit
];
1821 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
1822 lcp_options
*ao
= &lcp_allowoptions
[f
->unit
];
1826 if (!go
->neg_magicnumber
)
1827 go
->magicnumber
= 0;
1828 if (!ho
->neg_magicnumber
)
1829 ho
->magicnumber
= 0;
1832 * Set our MTU to the smaller of the MTU we wanted and
1833 * the MRU our peer wanted. If we negotiated an MRU,
1834 * set our MRU to the larger of value we wanted and
1835 * the value we got in the negotiation.
1836 * Note on the MTU: the link MTU can be the MRU the peer wanted,
1837 * the interface MTU is set to the lower of that and the
1838 * MTU we want to use.
1840 mtu
= MIN(ho
->neg_mru
? ho
->mru
: PPP_MRU
, ao
->mru
);
1841 sprintf(runmtu
, "%d", mtu
); // by honor 20040618
1842 script_setenv("MTU", runmtu
, 1);
1843 #ifdef HAVE_MULTILINK
1844 if (!(multilink
&& go
->neg_mrru
&& ho
->neg_mrru
))
1845 #endif /* HAVE_MULTILINK */
1846 netif_set_mtu(f
->unit
, mtu
);
1847 ppp_send_config(f
->unit
, mtu
,
1848 (ho
->neg_asyncmap
? ho
->asyncmap
: 0xffffffff),
1849 ho
->neg_pcompression
, ho
->neg_accompression
);
1850 ppp_recv_config(f
->unit
, (go
->neg_mru
? MAX(wo
->mru
, go
->mru
): PPP_MRU
),
1851 (lax_recv
? 0: go
->neg_asyncmap
? go
->asyncmap
: 0xffffffff),
1852 go
->neg_pcompression
, go
->neg_accompression
);
1855 peer_mru
[f
->unit
] = ho
->mru
;
1857 lcp_echo_lowerup(f
->unit
); /* Enable echo messages */
1859 link_established(f
->unit
);
1864 * lcp_down - LCP has gone DOWN.
1866 * Alert other protocols.
1872 lcp_options
*go
= &lcp_gotoptions
[f
->unit
];
1874 lcp_echo_lowerdown(f
->unit
);
1878 ppp_send_config(f
->unit
, PPP_MRU
, 0xffffffff, 0, 0);
1879 ppp_recv_config(f
->unit
, PPP_MRU
,
1880 (go
->neg_asyncmap
? go
->asyncmap
: 0xffffffff),
1881 go
->neg_pcompression
, go
->neg_accompression
);
1882 peer_mru
[f
->unit
] = PPP_MRU
;
1887 * lcp_starting - LCP needs the lower layer up.
1893 link_required(f
->unit
);
1898 * lcp_finished - LCP has finished with the lower layer.
1904 link_terminated(f
->unit
);
1909 * lcp_printpkt - print the contents of an LCP packet.
1911 static char *lcp_codenames
[] = {
1912 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1913 "TermReq", "TermAck", "CodeRej", "ProtRej",
1914 "EchoReq", "EchoRep", "DiscReq"
1918 lcp_printpkt(p
, plen
, printer
, arg
)
1921 void (*printer
) __P((void *, char *, ...));
1924 int code
, id
, len
, olen
, i
;
1925 u_char
*pstart
, *optend
;
1929 if (plen
< HEADERLEN
)
1935 if (len
< HEADERLEN
|| len
> plen
)
1938 if (code
>= 1 && code
<= sizeof(lcp_codenames
) / sizeof(char *))
1939 printer(arg
, " pid=(%d) %s", getpid(), lcp_codenames
[code
-1]);
1941 printer(arg
, " code=0x%x", code
);
1942 printer(arg
, " id=0x%x", id
);
1949 /* print option list */
1954 if (olen
< 2 || olen
> len
) {
1962 if (olen
== CILEN_SHORT
) {
1964 GETSHORT(cishort
, p
);
1965 printer(arg
, "mru %d", cishort
);
1969 if (olen
== CILEN_LONG
) {
1972 printer(arg
, "asyncmap 0x%x", cilong
);
1976 if (olen
>= CILEN_SHORT
) {
1978 printer(arg
, "auth ");
1979 GETSHORT(cishort
, p
);
1982 printer(arg
, "pap");
1985 printer(arg
, "chap");
1988 case CHAP_DIGEST_MD5
:
1989 printer(arg
, " MD5");
1993 case CHAP_MICROSOFT
:
1994 printer(arg
, " m$oft");
2002 printer(arg
, "0x%x", cishort
);
2007 if (olen
>= CILEN_SHORT
) {
2009 printer(arg
, "quality ");
2010 GETSHORT(cishort
, p
);
2013 printer(arg
, "lqr");
2016 printer(arg
, "0x%x", cishort
);
2021 if (olen
>= CILEN_CHAR
) {
2023 printer(arg
, "callback ");
2024 GETCHAR(cishort
, p
);
2027 printer(arg
, "CBCP");
2030 printer(arg
, "0x%x", cishort
);
2034 case CI_MAGICNUMBER
:
2035 if (olen
== CILEN_LONG
) {
2038 printer(arg
, "magic 0x%x", cilong
);
2041 case CI_PCOMPRESSION
:
2042 if (olen
== CILEN_VOID
) {
2044 printer(arg
, "pcomp");
2047 case CI_ACCOMPRESSION
:
2048 if (olen
== CILEN_VOID
) {
2050 printer(arg
, "accomp");
2054 if (olen
== CILEN_SHORT
) {
2056 GETSHORT(cishort
, p
);
2057 printer(arg
, "mrru %d", cishort
);
2061 if (olen
== CILEN_VOID
) {
2063 printer(arg
, "ssnhf");
2067 #ifdef HAVE_MULTILINK
2068 if (olen
>= CILEN_CHAR
) {
2071 GETCHAR(epd
.class, p
);
2072 epd
.length
= olen
- CILEN_CHAR
;
2073 if (epd
.length
> MAX_ENDP_LEN
)
2074 epd
.length
= MAX_ENDP_LEN
;
2075 if (epd
.length
> 0) {
2076 BCOPY(p
, epd
.value
, epd
.length
);
2079 printer(arg
, "endpoint [%s]", epdisc_to_str(&epd
));
2082 printer(arg
, "endpoint");
2086 while (p
< optend
) {
2088 printer(arg
, " %.2x", code
);
2096 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
2098 print_string((char *)p
, len
, printer
, arg
);
2109 printer(arg
, " magic=0x%x", cilong
);
2116 /* print the rest of the bytes in the packet */
2117 for (i
= 0; i
< len
&& i
< 32; ++i
) {
2119 printer(arg
, " %.2x", code
);
2122 printer(arg
, " ...");
2130 * Time to shut down the link because there is nothing out there.
2134 void LcpLinkFailure (f
)
2137 if (f
->state
== OPENED
) {
2138 LOGX_WARN("LCP appears to be disconnected (pending: %d).", lcp_echos_pending
);
2140 // info("No response to %d echo-requests", lcp_echos_pending);
2141 // notice("Serial link appears to be disconnected.");
2142 lcp_close(f
->unit
, "Peer not responding");
2143 status
= EXIT_PEER_DEAD
;
2147 #if 0 // timer -> timer ??? -- zzz
2149 * Check if we have been received "echo reply" packet
2156 fsm
*f1
= &lcp_fsm
[ifunit
];
2159 if(lcp_echos_pending
>= 1)
2160 TIMEOUT (LcpEchoTimeout
, f1
, 1);
2163 TIMEOUT (LcpEchoTimeout
, f1
, lcp_echo_interval
-1);
2168 * Timer expired for the LCP echo requests from this process.
2175 LcpSendEchoRequest (f
);
2176 if (f
->state
!= OPENED
)
2180 * Start the timer for the next interval.
2183 TIMEOUT (LcpEchoTimeout
, f
, lcp_echo_interval
);
2185 if (lcp_echo_timer_running
)
2186 warn("assertion lcp_echo_timer_running==0 failed");
2188 TIMEOUT (LcpCheckReply
, f
, 1);
2191 lcp_echo_timer_running
= 1;
2195 * LcpEchoTimeout - Timer expired on the LCP echo
2199 LcpEchoTimeout (arg
)
2202 if (lcp_echo_timer_running
!= 0) {
2203 lcp_echo_timer_running
= 0;
2204 LcpEchoCheck ((fsm
*) arg
);
2209 * LcpEchoReply - LCP has received a reply to the echo
2213 lcp_received_echo_reply (f
, id
, inp
, len
)
2221 /* Check the magic number - don't count replies from ourselves. */
2223 dbglog("lcp: received short Echo-Reply, length %d", len
);
2226 GETLONG(magic
, inp
);
2227 if (lcp_gotoptions
[f
->unit
].neg_magicnumber
2228 && magic
== lcp_gotoptions
[f
->unit
].magicnumber
) {
2229 warn("appear to have received our own echo-reply!");
2233 /* Reset the number of outstanding echo frames */
2234 lcp_echos_pending
= 0;
2238 * LcpSendEchoRequest - Send an echo request frame to the peer
2242 LcpSendEchoRequest (f
)
2245 u_int32_t lcp_magic
;
2246 u_char pkt
[4], *pktp
;
2247 struct ppp_idle idle
;
2248 unsigned long echo_interval
;
2250 echo_interval
= get_time() - echo_time
;
2251 if (echo_interval
<= lcp_echo_interval
) {
2252 // LOGX_DEBUG("(echo_interval %lu <= lcp_echo_interval %d)", echo_interval, lcp_echo_interval);
2257 LOGX_DEBUG("echo_interval=%lu lcp_echo_interval=%d lcp_echos_pending=%d lcp_echo_fails=%d",
2258 echo_interval, lcp_echo_interval,
2259 lcp_echos_pending, lcp_echo_fails);
2263 * Detect the failure of the peer at this point.
2265 if (lcp_echo_fails
!= 0) {
2266 if (lcp_echos_pending
>= lcp_echo_fails
) {
2267 // LOGX_DEBUG("lcp_echos_pending=%d lcp_echo_fails=%d", lcp_echos_pending, lcp_echo_fails);
2268 if (get_idle_time(0, &idle
)) {
2269 // LOGX_DEBUG("idle.recv_idle=%ld", idle.recv_idle);
2270 if (idle
.recv_idle
< lcp_echo_fails
* lcp_echo_interval
) {
2271 lcp_echos_pending
--;
2272 goto SendEchoRequest
;
2275 // LOGX_DEBUG("check echo reply failed");
2277 lcp_echos_pending
= 0;
2283 * Make and send the echo request frame.
2285 if (f
->state
== OPENED
) {
2286 lcp_magic
= lcp_gotoptions
[f
->unit
].magicnumber
;
2288 PUTLONG(lcp_magic
, pktp
);
2289 fsm_sdata(f
, ECHOREQ
, lcp_echo_number
++ & 0xFF, pkt
, pktp
- pkt
);
2290 ++lcp_echos_pending
;
2295 * lcp_echo_lowerup - Start the timer for the LCP frame
2299 lcp_echo_lowerup (unit
)
2302 fsm
*f
= &lcp_fsm
[unit
];
2304 /* Clear the parameters for generating echo frames */
2305 lcp_echos_pending
= 0;
2306 lcp_echo_number
= 0;
2307 lcp_echo_timer_running
= 0;
2309 /* If a timeout interval is specified then start the timer */
2310 if (lcp_echo_interval
!= 0)
2315 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2319 lcp_echo_lowerdown (unit
)
2322 fsm
*f
= &lcp_fsm
[unit
];
2324 if (lcp_echo_timer_running
!= 0) {
2325 UNTIMEOUT (LcpEchoTimeout
, f
);
2326 lcp_echo_timer_running
= 0;