OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [lwip_tcpip/] [current/] [src/] [netif/] [ppp/] [fsm.c] - Blame information for rev 865

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.