xref: /nrf52832-nimble/rt-thread/components/net/lwip-1.4.1/src/netif/ppp/fsm.c (revision 104654410c56c573564690304ae786df310c91fc)
1*10465441SEvalZero /*****************************************************************************
2*10465441SEvalZero * fsm.c - Network Control Protocol Finite State Machine program file.
3*10465441SEvalZero *
4*10465441SEvalZero * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5*10465441SEvalZero * portions Copyright (c) 1997 by Global Election Systems Inc.
6*10465441SEvalZero *
7*10465441SEvalZero * The authors hereby grant permission to use, copy, modify, distribute,
8*10465441SEvalZero * and license this software and its documentation for any purpose, provided
9*10465441SEvalZero * that existing copyright notices are retained in all copies and that this
10*10465441SEvalZero * notice and the following disclaimer are included verbatim in any
11*10465441SEvalZero * distributions. No written agreement, license, or royalty fee is required
12*10465441SEvalZero * for any of the authorized uses.
13*10465441SEvalZero *
14*10465441SEvalZero * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15*10465441SEvalZero * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*10465441SEvalZero * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*10465441SEvalZero * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18*10465441SEvalZero * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*10465441SEvalZero * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*10465441SEvalZero * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*10465441SEvalZero * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*10465441SEvalZero * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*10465441SEvalZero * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*10465441SEvalZero *
25*10465441SEvalZero ******************************************************************************
26*10465441SEvalZero * REVISION HISTORY
27*10465441SEvalZero *
28*10465441SEvalZero * 03-01-01 Marc Boucher <[email protected]>
29*10465441SEvalZero *   Ported to lwIP.
30*10465441SEvalZero * 97-12-01 Guy Lancaster <[email protected]>, Global Election Systems Inc.
31*10465441SEvalZero *   Original based on BSD fsm.c.
32*10465441SEvalZero *****************************************************************************/
33*10465441SEvalZero /*
34*10465441SEvalZero  * fsm.c - {Link, IP} Control Protocol Finite State Machine.
35*10465441SEvalZero  *
36*10465441SEvalZero  * Copyright (c) 1989 Carnegie Mellon University.
37*10465441SEvalZero  * All rights reserved.
38*10465441SEvalZero  *
39*10465441SEvalZero  * Redistribution and use in source and binary forms are permitted
40*10465441SEvalZero  * provided that the above copyright notice and this paragraph are
41*10465441SEvalZero  * duplicated in all such forms and that any documentation,
42*10465441SEvalZero  * advertising materials, and other materials related to such
43*10465441SEvalZero  * distribution and use acknowledge that the software was developed
44*10465441SEvalZero  * by Carnegie Mellon University.  The name of the
45*10465441SEvalZero  * University may not be used to endorse or promote products derived
46*10465441SEvalZero  * from this software without specific prior written permission.
47*10465441SEvalZero  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48*10465441SEvalZero  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49*10465441SEvalZero  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50*10465441SEvalZero  */
51*10465441SEvalZero 
52*10465441SEvalZero /*
53*10465441SEvalZero  * TODO:
54*10465441SEvalZero  * Randomize fsm id on link/init.
55*10465441SEvalZero  * Deal with variable outgoing MTU.
56*10465441SEvalZero  */
57*10465441SEvalZero 
58*10465441SEvalZero #include "lwip/opt.h"
59*10465441SEvalZero 
60*10465441SEvalZero #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
61*10465441SEvalZero 
62*10465441SEvalZero #include "ppp_impl.h"
63*10465441SEvalZero #include "pppdebug.h"
64*10465441SEvalZero 
65*10465441SEvalZero #include "fsm.h"
66*10465441SEvalZero 
67*10465441SEvalZero #include <string.h>
68*10465441SEvalZero 
69*10465441SEvalZero #if PPP_DEBUG
70*10465441SEvalZero static const char *ppperr_strerr[] = {
71*10465441SEvalZero            "LS_INITIAL",  /* LS_INITIAL  0 */
72*10465441SEvalZero            "LS_STARTING", /* LS_STARTING 1 */
73*10465441SEvalZero            "LS_CLOSED",   /* LS_CLOSED   2 */
74*10465441SEvalZero            "LS_STOPPED",  /* LS_STOPPED  3 */
75*10465441SEvalZero            "LS_CLOSING",  /* LS_CLOSING  4 */
76*10465441SEvalZero            "LS_STOPPING", /* LS_STOPPING 5 */
77*10465441SEvalZero            "LS_REQSENT",  /* LS_REQSENT  6 */
78*10465441SEvalZero            "LS_ACKRCVD",  /* LS_ACKRCVD  7 */
79*10465441SEvalZero            "LS_ACKSENT",  /* LS_ACKSENT  8 */
80*10465441SEvalZero            "LS_OPENED"    /* LS_OPENED   9 */
81*10465441SEvalZero };
82*10465441SEvalZero #endif /* PPP_DEBUG */
83*10465441SEvalZero 
84*10465441SEvalZero static void fsm_timeout (void *);
85*10465441SEvalZero static void fsm_rconfreq (fsm *, u_char, u_char *, int);
86*10465441SEvalZero static void fsm_rconfack (fsm *, int, u_char *, int);
87*10465441SEvalZero static void fsm_rconfnakrej (fsm *, int, int, u_char *, int);
88*10465441SEvalZero static void fsm_rtermreq (fsm *, int, u_char *, int);
89*10465441SEvalZero static void fsm_rtermack (fsm *);
90*10465441SEvalZero static void fsm_rcoderej (fsm *, u_char *, int);
91*10465441SEvalZero static void fsm_sconfreq (fsm *, int);
92*10465441SEvalZero 
93*10465441SEvalZero #define PROTO_NAME(f) ((f)->callbacks->proto_name)
94*10465441SEvalZero 
95*10465441SEvalZero int peer_mru[NUM_PPP];
96*10465441SEvalZero 
97*10465441SEvalZero 
98*10465441SEvalZero /*
99*10465441SEvalZero  * fsm_init - Initialize fsm.
100*10465441SEvalZero  *
101*10465441SEvalZero  * Initialize fsm state.
102*10465441SEvalZero  */
103*10465441SEvalZero void
fsm_init(fsm * f)104*10465441SEvalZero fsm_init(fsm *f)
105*10465441SEvalZero {
106*10465441SEvalZero   f->state = LS_INITIAL;
107*10465441SEvalZero   f->flags = 0;
108*10465441SEvalZero   f->id = 0;        /* XXX Start with random id? */
109*10465441SEvalZero   f->timeouttime = FSM_DEFTIMEOUT;
110*10465441SEvalZero   f->maxconfreqtransmits = FSM_DEFMAXCONFREQS;
111*10465441SEvalZero   f->maxtermtransmits = FSM_DEFMAXTERMREQS;
112*10465441SEvalZero   f->maxnakloops = FSM_DEFMAXNAKLOOPS;
113*10465441SEvalZero   f->term_reason_len = 0;
114*10465441SEvalZero }
115*10465441SEvalZero 
116*10465441SEvalZero 
117*10465441SEvalZero /*
118*10465441SEvalZero  * fsm_lowerup - The lower layer is up.
119*10465441SEvalZero  */
120*10465441SEvalZero void
fsm_lowerup(fsm * f)121*10465441SEvalZero fsm_lowerup(fsm *f)
122*10465441SEvalZero {
123*10465441SEvalZero   int oldState = f->state;
124*10465441SEvalZero 
125*10465441SEvalZero   LWIP_UNUSED_ARG(oldState);
126*10465441SEvalZero 
127*10465441SEvalZero   switch( f->state ) {
128*10465441SEvalZero     case LS_INITIAL:
129*10465441SEvalZero       f->state = LS_CLOSED;
130*10465441SEvalZero       break;
131*10465441SEvalZero 
132*10465441SEvalZero     case LS_STARTING:
133*10465441SEvalZero       if( f->flags & OPT_SILENT ) {
134*10465441SEvalZero         f->state = LS_STOPPED;
135*10465441SEvalZero       } else {
136*10465441SEvalZero         /* Send an initial configure-request */
137*10465441SEvalZero         fsm_sconfreq(f, 0);
138*10465441SEvalZero         f->state = LS_REQSENT;
139*10465441SEvalZero       }
140*10465441SEvalZero     break;
141*10465441SEvalZero 
142*10465441SEvalZero     default:
143*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("%s: Up event in state %d (%s)!\n",
144*10465441SEvalZero           PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
145*10465441SEvalZero   }
146*10465441SEvalZero 
147*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("%s: lowerup state %d (%s) -> %d (%s)\n",
148*10465441SEvalZero       PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
149*10465441SEvalZero }
150*10465441SEvalZero 
151*10465441SEvalZero 
152*10465441SEvalZero /*
153*10465441SEvalZero  * fsm_lowerdown - The lower layer is down.
154*10465441SEvalZero  *
155*10465441SEvalZero  * Cancel all timeouts and inform upper layers.
156*10465441SEvalZero  */
157*10465441SEvalZero void
fsm_lowerdown(fsm * f)158*10465441SEvalZero fsm_lowerdown(fsm *f)
159*10465441SEvalZero {
160*10465441SEvalZero   int oldState = f->state;
161*10465441SEvalZero 
162*10465441SEvalZero   LWIP_UNUSED_ARG(oldState);
163*10465441SEvalZero 
164*10465441SEvalZero   switch( f->state ) {
165*10465441SEvalZero     case LS_CLOSED:
166*10465441SEvalZero       f->state = LS_INITIAL;
167*10465441SEvalZero       break;
168*10465441SEvalZero 
169*10465441SEvalZero     case LS_STOPPED:
170*10465441SEvalZero       f->state = LS_STARTING;
171*10465441SEvalZero       if( f->callbacks->starting ) {
172*10465441SEvalZero         (*f->callbacks->starting)(f);
173*10465441SEvalZero       }
174*10465441SEvalZero       break;
175*10465441SEvalZero 
176*10465441SEvalZero     case LS_CLOSING:
177*10465441SEvalZero       f->state = LS_INITIAL;
178*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
179*10465441SEvalZero       break;
180*10465441SEvalZero 
181*10465441SEvalZero     case LS_STOPPING:
182*10465441SEvalZero     case LS_REQSENT:
183*10465441SEvalZero     case LS_ACKRCVD:
184*10465441SEvalZero     case LS_ACKSENT:
185*10465441SEvalZero       f->state = LS_STARTING;
186*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
187*10465441SEvalZero       break;
188*10465441SEvalZero 
189*10465441SEvalZero     case LS_OPENED:
190*10465441SEvalZero       if( f->callbacks->down ) {
191*10465441SEvalZero         (*f->callbacks->down)(f);
192*10465441SEvalZero       }
193*10465441SEvalZero       f->state = LS_STARTING;
194*10465441SEvalZero       break;
195*10465441SEvalZero 
196*10465441SEvalZero     default:
197*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("%s: Down event in state %d (%s)!\n",
198*10465441SEvalZero           PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
199*10465441SEvalZero   }
200*10465441SEvalZero 
201*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("%s: lowerdown state %d (%s) -> %d (%s)\n",
202*10465441SEvalZero       PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
203*10465441SEvalZero }
204*10465441SEvalZero 
205*10465441SEvalZero 
206*10465441SEvalZero /*
207*10465441SEvalZero  * fsm_open - Link is allowed to come up.
208*10465441SEvalZero  */
209*10465441SEvalZero void
fsm_open(fsm * f)210*10465441SEvalZero fsm_open(fsm *f)
211*10465441SEvalZero {
212*10465441SEvalZero   int oldState = f->state;
213*10465441SEvalZero 
214*10465441SEvalZero   LWIP_UNUSED_ARG(oldState);
215*10465441SEvalZero 
216*10465441SEvalZero   switch( f->state ) {
217*10465441SEvalZero     case LS_INITIAL:
218*10465441SEvalZero       f->state = LS_STARTING;
219*10465441SEvalZero       if( f->callbacks->starting ) {
220*10465441SEvalZero         (*f->callbacks->starting)(f);
221*10465441SEvalZero       }
222*10465441SEvalZero       break;
223*10465441SEvalZero 
224*10465441SEvalZero     case LS_CLOSED:
225*10465441SEvalZero       if( f->flags & OPT_SILENT ) {
226*10465441SEvalZero         f->state = LS_STOPPED;
227*10465441SEvalZero       } else {
228*10465441SEvalZero         /* Send an initial configure-request */
229*10465441SEvalZero         fsm_sconfreq(f, 0);
230*10465441SEvalZero         f->state = LS_REQSENT;
231*10465441SEvalZero       }
232*10465441SEvalZero       break;
233*10465441SEvalZero 
234*10465441SEvalZero     case LS_CLOSING:
235*10465441SEvalZero       f->state = LS_STOPPING;
236*10465441SEvalZero       /* fall through */
237*10465441SEvalZero     case LS_STOPPED:
238*10465441SEvalZero     case LS_OPENED:
239*10465441SEvalZero       if( f->flags & OPT_RESTART ) {
240*10465441SEvalZero         fsm_lowerdown(f);
241*10465441SEvalZero         fsm_lowerup(f);
242*10465441SEvalZero       }
243*10465441SEvalZero       break;
244*10465441SEvalZero   }
245*10465441SEvalZero 
246*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("%s: open state %d (%s) -> %d (%s)\n",
247*10465441SEvalZero       PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
248*10465441SEvalZero }
249*10465441SEvalZero 
250*10465441SEvalZero #if 0 /* backport pppd 2.4.4b1; */
251*10465441SEvalZero /*
252*10465441SEvalZero  * terminate_layer - Start process of shutting down the FSM
253*10465441SEvalZero  *
254*10465441SEvalZero  * Cancel any timeout running, notify upper layers we're done, and
255*10465441SEvalZero  * send a terminate-request message as configured.
256*10465441SEvalZero  */
257*10465441SEvalZero static void
258*10465441SEvalZero terminate_layer(fsm *f, int nextstate)
259*10465441SEvalZero {
260*10465441SEvalZero   /* @todo */
261*10465441SEvalZero }
262*10465441SEvalZero #endif
263*10465441SEvalZero 
264*10465441SEvalZero /*
265*10465441SEvalZero  * fsm_close - Start closing connection.
266*10465441SEvalZero  *
267*10465441SEvalZero  * Cancel timeouts and either initiate close or possibly go directly to
268*10465441SEvalZero  * the LS_CLOSED state.
269*10465441SEvalZero  */
270*10465441SEvalZero void
fsm_close(fsm * f,char * reason)271*10465441SEvalZero fsm_close(fsm *f, char *reason)
272*10465441SEvalZero {
273*10465441SEvalZero   int oldState = f->state;
274*10465441SEvalZero 
275*10465441SEvalZero   LWIP_UNUSED_ARG(oldState);
276*10465441SEvalZero 
277*10465441SEvalZero   f->term_reason = reason;
278*10465441SEvalZero   f->term_reason_len = (reason == NULL ? 0 : (int)strlen(reason));
279*10465441SEvalZero   switch( f->state ) {
280*10465441SEvalZero     case LS_STARTING:
281*10465441SEvalZero       f->state = LS_INITIAL;
282*10465441SEvalZero       break;
283*10465441SEvalZero     case LS_STOPPED:
284*10465441SEvalZero       f->state = LS_CLOSED;
285*10465441SEvalZero       break;
286*10465441SEvalZero     case LS_STOPPING:
287*10465441SEvalZero       f->state = LS_CLOSING;
288*10465441SEvalZero       break;
289*10465441SEvalZero 
290*10465441SEvalZero     case LS_REQSENT:
291*10465441SEvalZero     case LS_ACKRCVD:
292*10465441SEvalZero     case LS_ACKSENT:
293*10465441SEvalZero     case LS_OPENED:
294*10465441SEvalZero       if( f->state != LS_OPENED ) {
295*10465441SEvalZero         UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
296*10465441SEvalZero       } else if( f->callbacks->down ) {
297*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers we're down */
298*10465441SEvalZero       }
299*10465441SEvalZero       /* Init restart counter, send Terminate-Request */
300*10465441SEvalZero       f->retransmits = f->maxtermtransmits;
301*10465441SEvalZero       fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
302*10465441SEvalZero             (u_char *) f->term_reason, f->term_reason_len);
303*10465441SEvalZero       TIMEOUT(fsm_timeout, f, f->timeouttime);
304*10465441SEvalZero       --f->retransmits;
305*10465441SEvalZero 
306*10465441SEvalZero       f->state = LS_CLOSING;
307*10465441SEvalZero       break;
308*10465441SEvalZero   }
309*10465441SEvalZero 
310*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("%s: close reason=%s state %d (%s) -> %d (%s)\n",
311*10465441SEvalZero       PROTO_NAME(f), reason, oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
312*10465441SEvalZero }
313*10465441SEvalZero 
314*10465441SEvalZero 
315*10465441SEvalZero /*
316*10465441SEvalZero  * fsm_timeout - Timeout expired.
317*10465441SEvalZero  */
318*10465441SEvalZero static void
fsm_timeout(void * arg)319*10465441SEvalZero fsm_timeout(void *arg)
320*10465441SEvalZero {
321*10465441SEvalZero   fsm *f = (fsm *) arg;
322*10465441SEvalZero 
323*10465441SEvalZero   switch (f->state) {
324*10465441SEvalZero     case LS_CLOSING:
325*10465441SEvalZero     case LS_STOPPING:
326*10465441SEvalZero       if( f->retransmits <= 0 ) {
327*10465441SEvalZero         FSMDEBUG(LOG_WARNING, ("%s: timeout sending Terminate-Request state=%d (%s)\n",
328*10465441SEvalZero              PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
329*10465441SEvalZero         /*
330*10465441SEvalZero          * We've waited for an ack long enough.  Peer probably heard us.
331*10465441SEvalZero          */
332*10465441SEvalZero         f->state = (f->state == LS_CLOSING)? LS_CLOSED: LS_STOPPED;
333*10465441SEvalZero         if( f->callbacks->finished ) {
334*10465441SEvalZero           (*f->callbacks->finished)(f);
335*10465441SEvalZero         }
336*10465441SEvalZero       } else {
337*10465441SEvalZero         FSMDEBUG(LOG_WARNING, ("%s: timeout resending Terminate-Requests state=%d (%s)\n",
338*10465441SEvalZero              PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
339*10465441SEvalZero         /* Send Terminate-Request */
340*10465441SEvalZero         fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
341*10465441SEvalZero             (u_char *) f->term_reason, f->term_reason_len);
342*10465441SEvalZero         TIMEOUT(fsm_timeout, f, f->timeouttime);
343*10465441SEvalZero         --f->retransmits;
344*10465441SEvalZero       }
345*10465441SEvalZero       break;
346*10465441SEvalZero 
347*10465441SEvalZero     case LS_REQSENT:
348*10465441SEvalZero     case LS_ACKRCVD:
349*10465441SEvalZero     case LS_ACKSENT:
350*10465441SEvalZero       if (f->retransmits <= 0) {
351*10465441SEvalZero         FSMDEBUG(LOG_WARNING, ("%s: timeout sending Config-Requests state=%d (%s)\n",
352*10465441SEvalZero          PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
353*10465441SEvalZero         f->state = LS_STOPPED;
354*10465441SEvalZero         if( (f->flags & OPT_PASSIVE) == 0 && f->callbacks->finished ) {
355*10465441SEvalZero           (*f->callbacks->finished)(f);
356*10465441SEvalZero         }
357*10465441SEvalZero       } else {
358*10465441SEvalZero         FSMDEBUG(LOG_WARNING, ("%s: timeout resending Config-Request state=%d (%s)\n",
359*10465441SEvalZero          PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
360*10465441SEvalZero         /* Retransmit the configure-request */
361*10465441SEvalZero         if (f->callbacks->retransmit) {
362*10465441SEvalZero           (*f->callbacks->retransmit)(f);
363*10465441SEvalZero         }
364*10465441SEvalZero         fsm_sconfreq(f, 1);    /* Re-send Configure-Request */
365*10465441SEvalZero         if( f->state == LS_ACKRCVD ) {
366*10465441SEvalZero           f->state = LS_REQSENT;
367*10465441SEvalZero         }
368*10465441SEvalZero       }
369*10465441SEvalZero       break;
370*10465441SEvalZero 
371*10465441SEvalZero     default:
372*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("%s: UNHANDLED timeout event in state %d (%s)!\n",
373*10465441SEvalZero           PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
374*10465441SEvalZero   }
375*10465441SEvalZero }
376*10465441SEvalZero 
377*10465441SEvalZero 
378*10465441SEvalZero /*
379*10465441SEvalZero  * fsm_input - Input packet.
380*10465441SEvalZero  */
381*10465441SEvalZero void
fsm_input(fsm * f,u_char * inpacket,int l)382*10465441SEvalZero fsm_input(fsm *f, u_char *inpacket, int l)
383*10465441SEvalZero {
384*10465441SEvalZero   u_char *inp = inpacket;
385*10465441SEvalZero   u_char code, id;
386*10465441SEvalZero   int len;
387*10465441SEvalZero 
388*10465441SEvalZero   /*
389*10465441SEvalZero   * Parse header (code, id and length).
390*10465441SEvalZero   * If packet too short, drop it.
391*10465441SEvalZero   */
392*10465441SEvalZero   if (l < HEADERLEN) {
393*10465441SEvalZero     FSMDEBUG(LOG_WARNING, ("fsm_input(%x): Rcvd short header.\n",
394*10465441SEvalZero           f->protocol));
395*10465441SEvalZero     return;
396*10465441SEvalZero   }
397*10465441SEvalZero   GETCHAR(code, inp);
398*10465441SEvalZero   GETCHAR(id, inp);
399*10465441SEvalZero   GETSHORT(len, inp);
400*10465441SEvalZero   if (len < HEADERLEN) {
401*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd illegal length.\n",
402*10465441SEvalZero         f->protocol));
403*10465441SEvalZero     return;
404*10465441SEvalZero   }
405*10465441SEvalZero   if (len > l) {
406*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd short packet.\n",
407*10465441SEvalZero         f->protocol));
408*10465441SEvalZero     return;
409*10465441SEvalZero   }
410*10465441SEvalZero   len -= HEADERLEN;    /* subtract header length */
411*10465441SEvalZero 
412*10465441SEvalZero   if( f->state == LS_INITIAL || f->state == LS_STARTING ) {
413*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd packet in state %d (%s).\n",
414*10465441SEvalZero         f->protocol, f->state, ppperr_strerr[f->state]));
415*10465441SEvalZero     return;
416*10465441SEvalZero   }
417*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_input(%s):%d,%d,%d\n", PROTO_NAME(f), code, id, l));
418*10465441SEvalZero   /*
419*10465441SEvalZero    * Action depends on code.
420*10465441SEvalZero    */
421*10465441SEvalZero   switch (code) {
422*10465441SEvalZero     case CONFREQ:
423*10465441SEvalZero       fsm_rconfreq(f, id, inp, len);
424*10465441SEvalZero       break;
425*10465441SEvalZero 
426*10465441SEvalZero     case CONFACK:
427*10465441SEvalZero       fsm_rconfack(f, id, inp, len);
428*10465441SEvalZero       break;
429*10465441SEvalZero 
430*10465441SEvalZero     case CONFNAK:
431*10465441SEvalZero     case CONFREJ:
432*10465441SEvalZero       fsm_rconfnakrej(f, code, id, inp, len);
433*10465441SEvalZero       break;
434*10465441SEvalZero 
435*10465441SEvalZero     case TERMREQ:
436*10465441SEvalZero       fsm_rtermreq(f, id, inp, len);
437*10465441SEvalZero       break;
438*10465441SEvalZero 
439*10465441SEvalZero     case TERMACK:
440*10465441SEvalZero       fsm_rtermack(f);
441*10465441SEvalZero       break;
442*10465441SEvalZero 
443*10465441SEvalZero     case CODEREJ:
444*10465441SEvalZero       fsm_rcoderej(f, inp, len);
445*10465441SEvalZero       break;
446*10465441SEvalZero 
447*10465441SEvalZero     default:
448*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("fsm_input(%s): default: \n", PROTO_NAME(f)));
449*10465441SEvalZero       if( !f->callbacks->extcode ||
450*10465441SEvalZero           !(*f->callbacks->extcode)(f, code, id, inp, len) ) {
451*10465441SEvalZero         fsm_sdata(f, CODEREJ, ++f->id, inpacket, len + HEADERLEN);
452*10465441SEvalZero       }
453*10465441SEvalZero       break;
454*10465441SEvalZero   }
455*10465441SEvalZero }
456*10465441SEvalZero 
457*10465441SEvalZero 
458*10465441SEvalZero /*
459*10465441SEvalZero  * fsm_rconfreq - Receive Configure-Request.
460*10465441SEvalZero  */
461*10465441SEvalZero static void
fsm_rconfreq(fsm * f,u_char id,u_char * inp,int len)462*10465441SEvalZero fsm_rconfreq(fsm *f, u_char id, u_char *inp, int len)
463*10465441SEvalZero {
464*10465441SEvalZero   int code, reject_if_disagree;
465*10465441SEvalZero 
466*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rconfreq(%s): Rcvd id %d state=%d (%s)\n",
467*10465441SEvalZero         PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
468*10465441SEvalZero   switch( f->state ) {
469*10465441SEvalZero     case LS_CLOSED:
470*10465441SEvalZero       /* Go away, we're closed */
471*10465441SEvalZero       fsm_sdata(f, TERMACK, id, NULL, 0);
472*10465441SEvalZero       return;
473*10465441SEvalZero     case LS_CLOSING:
474*10465441SEvalZero     case LS_STOPPING:
475*10465441SEvalZero       return;
476*10465441SEvalZero 
477*10465441SEvalZero     case LS_OPENED:
478*10465441SEvalZero       /* Go down and restart negotiation */
479*10465441SEvalZero       if( f->callbacks->down ) {
480*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers */
481*10465441SEvalZero       }
482*10465441SEvalZero       fsm_sconfreq(f, 0);    /* Send initial Configure-Request */
483*10465441SEvalZero       break;
484*10465441SEvalZero 
485*10465441SEvalZero     case LS_STOPPED:
486*10465441SEvalZero       /* Negotiation started by our peer */
487*10465441SEvalZero       fsm_sconfreq(f, 0);    /* Send initial Configure-Request */
488*10465441SEvalZero       f->state = LS_REQSENT;
489*10465441SEvalZero       break;
490*10465441SEvalZero   }
491*10465441SEvalZero 
492*10465441SEvalZero   /*
493*10465441SEvalZero   * Pass the requested configuration options
494*10465441SEvalZero   * to protocol-specific code for checking.
495*10465441SEvalZero   */
496*10465441SEvalZero   if (f->callbacks->reqci) {    /* Check CI */
497*10465441SEvalZero     reject_if_disagree = (f->nakloops >= f->maxnakloops);
498*10465441SEvalZero     code = (*f->callbacks->reqci)(f, inp, &len, reject_if_disagree);
499*10465441SEvalZero   } else if (len) {
500*10465441SEvalZero     code = CONFREJ;      /* Reject all CI */
501*10465441SEvalZero   } else {
502*10465441SEvalZero     code = CONFACK;
503*10465441SEvalZero   }
504*10465441SEvalZero 
505*10465441SEvalZero   /* send the Ack, Nak or Rej to the peer */
506*10465441SEvalZero   fsm_sdata(f, (u_char)code, id, inp, len);
507*10465441SEvalZero 
508*10465441SEvalZero   if (code == CONFACK) {
509*10465441SEvalZero     if (f->state == LS_ACKRCVD) {
510*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
511*10465441SEvalZero       f->state = LS_OPENED;
512*10465441SEvalZero       if (f->callbacks->up) {
513*10465441SEvalZero         (*f->callbacks->up)(f);  /* Inform upper layers */
514*10465441SEvalZero       }
515*10465441SEvalZero     } else {
516*10465441SEvalZero       f->state = LS_ACKSENT;
517*10465441SEvalZero     }
518*10465441SEvalZero     f->nakloops = 0;
519*10465441SEvalZero   } else {
520*10465441SEvalZero     /* we sent CONFACK or CONFREJ */
521*10465441SEvalZero     if (f->state != LS_ACKRCVD) {
522*10465441SEvalZero       f->state = LS_REQSENT;
523*10465441SEvalZero     }
524*10465441SEvalZero     if( code == CONFNAK ) {
525*10465441SEvalZero       ++f->nakloops;
526*10465441SEvalZero     }
527*10465441SEvalZero   }
528*10465441SEvalZero }
529*10465441SEvalZero 
530*10465441SEvalZero 
531*10465441SEvalZero /*
532*10465441SEvalZero  * fsm_rconfack - Receive Configure-Ack.
533*10465441SEvalZero  */
534*10465441SEvalZero static void
fsm_rconfack(fsm * f,int id,u_char * inp,int len)535*10465441SEvalZero fsm_rconfack(fsm *f, int id, u_char *inp, int len)
536*10465441SEvalZero {
537*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rconfack(%s): Rcvd id %d state=%d (%s)\n",
538*10465441SEvalZero         PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
539*10465441SEvalZero 
540*10465441SEvalZero   if (id != f->reqid || f->seen_ack) {   /* Expected id? */
541*10465441SEvalZero     return; /* Nope, toss... */
542*10465441SEvalZero   }
543*10465441SEvalZero   if( !(f->callbacks->ackci? (*f->callbacks->ackci)(f, inp, len): (len == 0)) ) {
544*10465441SEvalZero     /* Ack is bad - ignore it */
545*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("%s: received bad Ack (length %d)\n",
546*10465441SEvalZero           PROTO_NAME(f), len));
547*10465441SEvalZero     return;
548*10465441SEvalZero   }
549*10465441SEvalZero   f->seen_ack = 1;
550*10465441SEvalZero 
551*10465441SEvalZero   switch (f->state) {
552*10465441SEvalZero     case LS_CLOSED:
553*10465441SEvalZero     case LS_STOPPED:
554*10465441SEvalZero       fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
555*10465441SEvalZero       break;
556*10465441SEvalZero 
557*10465441SEvalZero     case LS_REQSENT:
558*10465441SEvalZero       f->state = LS_ACKRCVD;
559*10465441SEvalZero       f->retransmits = f->maxconfreqtransmits;
560*10465441SEvalZero       break;
561*10465441SEvalZero 
562*10465441SEvalZero     case LS_ACKRCVD:
563*10465441SEvalZero       /* Huh? an extra valid Ack? oh well... */
564*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
565*10465441SEvalZero       fsm_sconfreq(f, 0);
566*10465441SEvalZero       f->state = LS_REQSENT;
567*10465441SEvalZero       break;
568*10465441SEvalZero 
569*10465441SEvalZero     case LS_ACKSENT:
570*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
571*10465441SEvalZero       f->state = LS_OPENED;
572*10465441SEvalZero       f->retransmits = f->maxconfreqtransmits;
573*10465441SEvalZero       if (f->callbacks->up) {
574*10465441SEvalZero         (*f->callbacks->up)(f);  /* Inform upper layers */
575*10465441SEvalZero       }
576*10465441SEvalZero       break;
577*10465441SEvalZero 
578*10465441SEvalZero     case LS_OPENED:
579*10465441SEvalZero       /* Go down and restart negotiation */
580*10465441SEvalZero       if (f->callbacks->down) {
581*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers */
582*10465441SEvalZero       }
583*10465441SEvalZero       fsm_sconfreq(f, 0);    /* Send initial Configure-Request */
584*10465441SEvalZero       f->state = LS_REQSENT;
585*10465441SEvalZero       break;
586*10465441SEvalZero   }
587*10465441SEvalZero }
588*10465441SEvalZero 
589*10465441SEvalZero 
590*10465441SEvalZero /*
591*10465441SEvalZero  * fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
592*10465441SEvalZero  */
593*10465441SEvalZero static void
fsm_rconfnakrej(fsm * f,int code,int id,u_char * inp,int len)594*10465441SEvalZero fsm_rconfnakrej(fsm *f, int code, int id, u_char *inp, int len)
595*10465441SEvalZero {
596*10465441SEvalZero   int (*proc) (fsm *, u_char *, int);
597*10465441SEvalZero   int ret;
598*10465441SEvalZero 
599*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rconfnakrej(%s): Rcvd id %d state=%d (%s)\n",
600*10465441SEvalZero         PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
601*10465441SEvalZero 
602*10465441SEvalZero   if (id != f->reqid || f->seen_ack) { /* Expected id? */
603*10465441SEvalZero     return;        /* Nope, toss... */
604*10465441SEvalZero   }
605*10465441SEvalZero   proc = (code == CONFNAK)? f->callbacks->nakci: f->callbacks->rejci;
606*10465441SEvalZero   if (!proc || !((ret = proc(f, inp, len)))) {
607*10465441SEvalZero     /* Nak/reject is bad - ignore it */
608*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("%s: received bad %s (length %d)\n",
609*10465441SEvalZero           PROTO_NAME(f), (code==CONFNAK? "Nak": "reject"), len));
610*10465441SEvalZero     return;
611*10465441SEvalZero   }
612*10465441SEvalZero   f->seen_ack = 1;
613*10465441SEvalZero 
614*10465441SEvalZero   switch (f->state) {
615*10465441SEvalZero     case LS_CLOSED:
616*10465441SEvalZero     case LS_STOPPED:
617*10465441SEvalZero       fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
618*10465441SEvalZero       break;
619*10465441SEvalZero 
620*10465441SEvalZero     case LS_REQSENT:
621*10465441SEvalZero     case LS_ACKSENT:
622*10465441SEvalZero       /* They didn't agree to what we wanted - try another request */
623*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
624*10465441SEvalZero       if (ret < 0) {
625*10465441SEvalZero         f->state = LS_STOPPED;    /* kludge for stopping CCP */
626*10465441SEvalZero       } else {
627*10465441SEvalZero         fsm_sconfreq(f, 0);    /* Send Configure-Request */
628*10465441SEvalZero       }
629*10465441SEvalZero       break;
630*10465441SEvalZero 
631*10465441SEvalZero     case LS_ACKRCVD:
632*10465441SEvalZero       /* Got a Nak/reject when we had already had an Ack?? oh well... */
633*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
634*10465441SEvalZero       fsm_sconfreq(f, 0);
635*10465441SEvalZero       f->state = LS_REQSENT;
636*10465441SEvalZero       break;
637*10465441SEvalZero 
638*10465441SEvalZero     case LS_OPENED:
639*10465441SEvalZero       /* Go down and restart negotiation */
640*10465441SEvalZero       if (f->callbacks->down) {
641*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers */
642*10465441SEvalZero       }
643*10465441SEvalZero       fsm_sconfreq(f, 0);    /* Send initial Configure-Request */
644*10465441SEvalZero       f->state = LS_REQSENT;
645*10465441SEvalZero       break;
646*10465441SEvalZero   }
647*10465441SEvalZero }
648*10465441SEvalZero 
649*10465441SEvalZero 
650*10465441SEvalZero /*
651*10465441SEvalZero  * fsm_rtermreq - Receive Terminate-Req.
652*10465441SEvalZero  */
653*10465441SEvalZero static void
fsm_rtermreq(fsm * f,int id,u_char * p,int len)654*10465441SEvalZero fsm_rtermreq(fsm *f, int id, u_char *p, int len)
655*10465441SEvalZero {
656*10465441SEvalZero   LWIP_UNUSED_ARG(p);
657*10465441SEvalZero 
658*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rtermreq(%s): Rcvd id %d state=%d (%s)\n",
659*10465441SEvalZero         PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
660*10465441SEvalZero 
661*10465441SEvalZero   switch (f->state) {
662*10465441SEvalZero     case LS_ACKRCVD:
663*10465441SEvalZero     case LS_ACKSENT:
664*10465441SEvalZero       f->state = LS_REQSENT;    /* Start over but keep trying */
665*10465441SEvalZero       break;
666*10465441SEvalZero 
667*10465441SEvalZero     case LS_OPENED:
668*10465441SEvalZero       if (len > 0) {
669*10465441SEvalZero         FSMDEBUG(LOG_INFO, ("%s terminated by peer (%p)\n", PROTO_NAME(f), p));
670*10465441SEvalZero       } else {
671*10465441SEvalZero         FSMDEBUG(LOG_INFO, ("%s terminated by peer\n", PROTO_NAME(f)));
672*10465441SEvalZero       }
673*10465441SEvalZero       if (f->callbacks->down) {
674*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers */
675*10465441SEvalZero       }
676*10465441SEvalZero       f->retransmits = 0;
677*10465441SEvalZero       f->state = LS_STOPPING;
678*10465441SEvalZero       TIMEOUT(fsm_timeout, f, f->timeouttime);
679*10465441SEvalZero       break;
680*10465441SEvalZero   }
681*10465441SEvalZero 
682*10465441SEvalZero   fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
683*10465441SEvalZero }
684*10465441SEvalZero 
685*10465441SEvalZero 
686*10465441SEvalZero /*
687*10465441SEvalZero  * fsm_rtermack - Receive Terminate-Ack.
688*10465441SEvalZero  */
689*10465441SEvalZero static void
fsm_rtermack(fsm * f)690*10465441SEvalZero fsm_rtermack(fsm *f)
691*10465441SEvalZero {
692*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rtermack(%s): state=%d (%s)\n",
693*10465441SEvalZero         PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
694*10465441SEvalZero 
695*10465441SEvalZero   switch (f->state) {
696*10465441SEvalZero     case LS_CLOSING:
697*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);
698*10465441SEvalZero       f->state = LS_CLOSED;
699*10465441SEvalZero       if( f->callbacks->finished ) {
700*10465441SEvalZero         (*f->callbacks->finished)(f);
701*10465441SEvalZero       }
702*10465441SEvalZero       break;
703*10465441SEvalZero 
704*10465441SEvalZero     case LS_STOPPING:
705*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);
706*10465441SEvalZero       f->state = LS_STOPPED;
707*10465441SEvalZero       if( f->callbacks->finished ) {
708*10465441SEvalZero         (*f->callbacks->finished)(f);
709*10465441SEvalZero       }
710*10465441SEvalZero       break;
711*10465441SEvalZero 
712*10465441SEvalZero     case LS_ACKRCVD:
713*10465441SEvalZero       f->state = LS_REQSENT;
714*10465441SEvalZero       break;
715*10465441SEvalZero 
716*10465441SEvalZero     case LS_OPENED:
717*10465441SEvalZero       if (f->callbacks->down) {
718*10465441SEvalZero         (*f->callbacks->down)(f);  /* Inform upper layers */
719*10465441SEvalZero       }
720*10465441SEvalZero       fsm_sconfreq(f, 0);
721*10465441SEvalZero       break;
722*10465441SEvalZero     default:
723*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("fsm_rtermack(%s): UNHANDLED state=%d (%s)!!!\n",
724*10465441SEvalZero                 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
725*10465441SEvalZero   }
726*10465441SEvalZero }
727*10465441SEvalZero 
728*10465441SEvalZero 
729*10465441SEvalZero /*
730*10465441SEvalZero  * fsm_rcoderej - Receive an Code-Reject.
731*10465441SEvalZero  */
732*10465441SEvalZero static void
fsm_rcoderej(fsm * f,u_char * inp,int len)733*10465441SEvalZero fsm_rcoderej(fsm *f, u_char *inp, int len)
734*10465441SEvalZero {
735*10465441SEvalZero   u_char code, id;
736*10465441SEvalZero 
737*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_rcoderej(%s): state=%d (%s)\n",
738*10465441SEvalZero         PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
739*10465441SEvalZero 
740*10465441SEvalZero   if (len < HEADERLEN) {
741*10465441SEvalZero     FSMDEBUG(LOG_INFO, ("fsm_rcoderej: Rcvd short Code-Reject packet!\n"));
742*10465441SEvalZero     return;
743*10465441SEvalZero   }
744*10465441SEvalZero   GETCHAR(code, inp);
745*10465441SEvalZero   GETCHAR(id, inp);
746*10465441SEvalZero   FSMDEBUG(LOG_WARNING, ("%s: Rcvd Code-Reject for code %d, id %d\n",
747*10465441SEvalZero         PROTO_NAME(f), code, id));
748*10465441SEvalZero 
749*10465441SEvalZero   if( f->state == LS_ACKRCVD ) {
750*10465441SEvalZero     f->state = LS_REQSENT;
751*10465441SEvalZero   }
752*10465441SEvalZero }
753*10465441SEvalZero 
754*10465441SEvalZero 
755*10465441SEvalZero /*
756*10465441SEvalZero  * fsm_protreject - Peer doesn't speak this protocol.
757*10465441SEvalZero  *
758*10465441SEvalZero  * Treat this as a catastrophic error (RXJ-).
759*10465441SEvalZero  */
760*10465441SEvalZero void
fsm_protreject(fsm * f)761*10465441SEvalZero fsm_protreject(fsm *f)
762*10465441SEvalZero {
763*10465441SEvalZero   switch( f->state ) {
764*10465441SEvalZero     case LS_CLOSING:
765*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
766*10465441SEvalZero       /* fall through */
767*10465441SEvalZero     case LS_CLOSED:
768*10465441SEvalZero       f->state = LS_CLOSED;
769*10465441SEvalZero       if( f->callbacks->finished ) {
770*10465441SEvalZero         (*f->callbacks->finished)(f);
771*10465441SEvalZero       }
772*10465441SEvalZero       break;
773*10465441SEvalZero 
774*10465441SEvalZero     case LS_STOPPING:
775*10465441SEvalZero     case LS_REQSENT:
776*10465441SEvalZero     case LS_ACKRCVD:
777*10465441SEvalZero     case LS_ACKSENT:
778*10465441SEvalZero       UNTIMEOUT(fsm_timeout, f);  /* Cancel timeout */
779*10465441SEvalZero       /* fall through */
780*10465441SEvalZero     case LS_STOPPED:
781*10465441SEvalZero       f->state = LS_STOPPED;
782*10465441SEvalZero       if( f->callbacks->finished ) {
783*10465441SEvalZero         (*f->callbacks->finished)(f);
784*10465441SEvalZero       }
785*10465441SEvalZero       break;
786*10465441SEvalZero 
787*10465441SEvalZero     case LS_OPENED:
788*10465441SEvalZero       if( f->callbacks->down ) {
789*10465441SEvalZero         (*f->callbacks->down)(f);
790*10465441SEvalZero       }
791*10465441SEvalZero       /* Init restart counter, send Terminate-Request */
792*10465441SEvalZero       f->retransmits = f->maxtermtransmits;
793*10465441SEvalZero       fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
794*10465441SEvalZero             (u_char *) f->term_reason, f->term_reason_len);
795*10465441SEvalZero       TIMEOUT(fsm_timeout, f, f->timeouttime);
796*10465441SEvalZero       --f->retransmits;
797*10465441SEvalZero 
798*10465441SEvalZero       f->state = LS_STOPPING;
799*10465441SEvalZero       break;
800*10465441SEvalZero 
801*10465441SEvalZero     default:
802*10465441SEvalZero       FSMDEBUG(LOG_INFO, ("%s: Protocol-reject event in state %d (%s)!\n",
803*10465441SEvalZero             PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
804*10465441SEvalZero     }
805*10465441SEvalZero }
806*10465441SEvalZero 
807*10465441SEvalZero 
808*10465441SEvalZero /*
809*10465441SEvalZero  * fsm_sconfreq - Send a Configure-Request.
810*10465441SEvalZero  */
811*10465441SEvalZero static void
fsm_sconfreq(fsm * f,int retransmit)812*10465441SEvalZero fsm_sconfreq(fsm *f, int retransmit)
813*10465441SEvalZero {
814*10465441SEvalZero   u_char *outp;
815*10465441SEvalZero   int cilen;
816*10465441SEvalZero 
817*10465441SEvalZero   if( f->state != LS_REQSENT && f->state != LS_ACKRCVD && f->state != LS_ACKSENT ) {
818*10465441SEvalZero     /* Not currently negotiating - reset options */
819*10465441SEvalZero     if( f->callbacks->resetci ) {
820*10465441SEvalZero       (*f->callbacks->resetci)(f);
821*10465441SEvalZero     }
822*10465441SEvalZero     f->nakloops = 0;
823*10465441SEvalZero   }
824*10465441SEvalZero 
825*10465441SEvalZero   if( !retransmit ) {
826*10465441SEvalZero     /* New request - reset retransmission counter, use new ID */
827*10465441SEvalZero     f->retransmits = f->maxconfreqtransmits;
828*10465441SEvalZero     f->reqid = ++f->id;
829*10465441SEvalZero   }
830*10465441SEvalZero 
831*10465441SEvalZero   f->seen_ack = 0;
832*10465441SEvalZero 
833*10465441SEvalZero   /*
834*10465441SEvalZero    * Make up the request packet
835*10465441SEvalZero    */
836*10465441SEvalZero   outp = outpacket_buf[f->unit] + PPP_HDRLEN + HEADERLEN;
837*10465441SEvalZero   if( f->callbacks->cilen && f->callbacks->addci ) {
838*10465441SEvalZero     cilen = (*f->callbacks->cilen)(f);
839*10465441SEvalZero     if( cilen > peer_mru[f->unit] - (int)HEADERLEN ) {
840*10465441SEvalZero       cilen = peer_mru[f->unit] - HEADERLEN;
841*10465441SEvalZero     }
842*10465441SEvalZero     if (f->callbacks->addci) {
843*10465441SEvalZero       (*f->callbacks->addci)(f, outp, &cilen);
844*10465441SEvalZero     }
845*10465441SEvalZero   } else {
846*10465441SEvalZero     cilen = 0;
847*10465441SEvalZero   }
848*10465441SEvalZero 
849*10465441SEvalZero   /* send the request to our peer */
850*10465441SEvalZero   fsm_sdata(f, CONFREQ, f->reqid, outp, cilen);
851*10465441SEvalZero 
852*10465441SEvalZero   /* start the retransmit timer */
853*10465441SEvalZero   --f->retransmits;
854*10465441SEvalZero   TIMEOUT(fsm_timeout, f, f->timeouttime);
855*10465441SEvalZero 
856*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("%s: sending Configure-Request, id %d\n",
857*10465441SEvalZero         PROTO_NAME(f), f->reqid));
858*10465441SEvalZero }
859*10465441SEvalZero 
860*10465441SEvalZero 
861*10465441SEvalZero /*
862*10465441SEvalZero  * fsm_sdata - Send some data.
863*10465441SEvalZero  *
864*10465441SEvalZero  * Used for all packets sent to our peer by this module.
865*10465441SEvalZero  */
866*10465441SEvalZero void
fsm_sdata(fsm * f,u_char code,u_char id,u_char * data,int datalen)867*10465441SEvalZero fsm_sdata( fsm *f, u_char code, u_char id, u_char *data, int datalen)
868*10465441SEvalZero {
869*10465441SEvalZero   u_char *outp;
870*10465441SEvalZero   int outlen;
871*10465441SEvalZero 
872*10465441SEvalZero   /* Adjust length to be smaller than MTU */
873*10465441SEvalZero   outp = outpacket_buf[f->unit];
874*10465441SEvalZero   if (datalen > peer_mru[f->unit] - (int)HEADERLEN) {
875*10465441SEvalZero     datalen = peer_mru[f->unit] - HEADERLEN;
876*10465441SEvalZero   }
877*10465441SEvalZero   if (datalen && data != outp + PPP_HDRLEN + HEADERLEN) {
878*10465441SEvalZero     BCOPY(data, outp + PPP_HDRLEN + HEADERLEN, datalen);
879*10465441SEvalZero   }
880*10465441SEvalZero   outlen = datalen + HEADERLEN;
881*10465441SEvalZero   MAKEHEADER(outp, f->protocol);
882*10465441SEvalZero   PUTCHAR(code, outp);
883*10465441SEvalZero   PUTCHAR(id, outp);
884*10465441SEvalZero   PUTSHORT(outlen, outp);
885*10465441SEvalZero   pppWrite(f->unit, outpacket_buf[f->unit], outlen + PPP_HDRLEN);
886*10465441SEvalZero   FSMDEBUG(LOG_INFO, ("fsm_sdata(%s): Sent code %d,%d,%d.\n",
887*10465441SEvalZero         PROTO_NAME(f), code, id, outlen));
888*10465441SEvalZero }
889*10465441SEvalZero 
890*10465441SEvalZero #endif /* PPP_SUPPORT */
891