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/] [lcp.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
* lcp.c - Network Link Control Protocol 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.
32
*****************************************************************************/
33
 
34
/*
35
 * lcp.c - PPP Link Control Protocol.
36
 *
37
 * Copyright (c) 1989 Carnegie Mellon University.
38
 * All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms are permitted
41
 * provided that the above copyright notice and this paragraph are
42
 * duplicated in all such forms and that any documentation,
43
 * advertising materials, and other materials related to such
44
 * distribution and use acknowledge that the software was developed
45
 * by Carnegie Mellon University.  The name of the
46
 * University may not be used to endorse or promote products derived
47
 * from this software without specific prior written permission.
48
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
49
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
50
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
51
 */
52
 
53
 
54
#include "lwip/opt.h"
55
 
56
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
57
 
58
#include "netif/ppp/ppp.h"
59
#include "pppdebug.h"
60
#include "timesys.h"
61
 
62
#include "fsm.h"
63
#include "chap.h"
64
#include "magic.h"
65
#include "auth.h"
66
#include "lcp.h"
67
 
68
#include <string.h>
69
 
70
#if PPPOE_SUPPORT
71
#include "netif/ppp_oe.h"
72
#else
73
#define PPPOE_MAXMTU PPP_MAXMRU
74
#endif
75
 
76
 
77
/*************************/
78
/*** LOCAL DEFINITIONS ***/
79
/*************************/
80
/*
81
 * Length of each type of configuration option (in octets)
82
 */
83
#define CILEN_VOID  2
84
#define CILEN_CHAR  3
85
#define CILEN_SHORT 4 /* CILEN_VOID + sizeof(short) */
86
#define CILEN_CHAP  5 /* CILEN_VOID + sizeof(short) + 1 */
87
#define CILEN_LONG  6 /* CILEN_VOID + sizeof(long) */
88
#define CILEN_LQR   8 /* CILEN_VOID + sizeof(short) + sizeof(long) */
89
#define CILEN_CBCP  3
90
 
91
 
92
/***********************************/
93
/*** LOCAL FUNCTION DECLARATIONS ***/
94
/***********************************/
95
/*
96
 * Callbacks for fsm code.  (CI = Configuration Information)
97
 */
98
static void lcp_resetci (fsm*);                 /* Reset our CI */
99
static int  lcp_cilen (fsm*);                   /* Return length of our CI */
100
static void lcp_addci (fsm*, u8_t*, int*);      /* Add our CI to pkt */
101
static int  lcp_ackci (fsm*, u8_t*, int);       /* Peer ack'd our CI */
102
static int  lcp_nakci (fsm*, u8_t*, int);       /* Peer nak'd our CI */
103
static int  lcp_rejci (fsm*, u8_t*, int);       /* Peer rej'd our CI */
104
static int  lcp_reqci (fsm*, u8_t*, int*, int); /* Rcv peer CI */
105
static void lcp_up (fsm*);                      /* We're UP */
106
static void lcp_down (fsm*);                    /* We're DOWN */
107
static void lcp_starting (fsm*);                /* We need lower layer up */
108
static void lcp_finished (fsm*);                /* We need lower layer down */
109
static int  lcp_extcode (fsm*, int, u8_t, u8_t*, int);
110
 
111
static void lcp_rprotrej (fsm*, u8_t*, int);
112
 
113
/*
114
 * routines to send LCP echos to peer
115
 */
116
static void lcp_echo_lowerup (int);
117
static void lcp_echo_lowerdown (int);
118
static void lcp_echo_timeout (void*);
119
static void lcp_received_echo_reply (fsm*, int, u8_t*, int);
120
static void lcp_send_echo_request (fsm*);
121
static void lcp_link_failure (fsm*);
122
static void lcp_echo_check (fsm*);
123
 
124
/*
125
 * Protocol entry points.
126
 * Some of these are called directly.
127
 */
128
static void lcp_input (int, u8_t *, int);
129
static void lcp_protrej (int);
130
 
131
#define CODENAME(x) ((x) == CONFACK ? "ACK" : (x) == CONFNAK ? "NAK" : "REJ")
132
 
133
 
134
/******************************/
135
/*** PUBLIC DATA STRUCTURES ***/
136
/******************************/
137
/* global vars */
138
LinkPhase lcp_phase[NUM_PPP];          /* Phase of link session (RFC 1661) */
139
lcp_options lcp_wantoptions[NUM_PPP];  /* Options that we want to request */
140
lcp_options lcp_gotoptions[NUM_PPP];   /* Options that peer ack'd */
141
lcp_options lcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
142
lcp_options lcp_hisoptions[NUM_PPP];   /* Options that we ack'd */
143
ext_accm xmit_accm[NUM_PPP];           /* extended transmit ACCM */
144
 
145
 
146
 
147
/*****************************/
148
/*** LOCAL DATA STRUCTURES ***/
149
/*****************************/
150
static fsm lcp_fsm[NUM_PPP];                            /* LCP fsm structure (global)*/
151
static u32_t lcp_echo_interval      = LCP_ECHOINTERVAL; /* Interval between LCP echo-requests */
152
static u32_t lcp_echo_fails         = LCP_MAXECHOFAILS; /* Tolerance to unanswered echo-requests */
153
static u32_t lcp_echos_pending      = 0;                /* Number of outstanding echo msgs */
154
static u32_t lcp_echo_number        = 0;                /* ID number of next echo frame */
155
static u32_t lcp_echo_timer_running = 0;                /* TRUE if a timer is running */
156
 
157
static u8_t nak_buffer[PPP_MRU]; /* where we construct a nak packet */
158
 
159
static fsm_callbacks lcp_callbacks = { /* LCP callback routines */
160
  lcp_resetci,  /* Reset our Configuration Information */
161
  lcp_cilen,    /* Length of our Configuration Information */
162
  lcp_addci,    /* Add our Configuration Information */
163
  lcp_ackci,    /* ACK our Configuration Information */
164
  lcp_nakci,    /* NAK our Configuration Information */
165
  lcp_rejci,    /* Reject our Configuration Information */
166
  lcp_reqci,    /* Request peer's Configuration Information */
167
  lcp_up,       /* Called when fsm reaches LS_OPENED state */
168
  lcp_down,     /* Called when fsm leaves LS_OPENED state */
169
  lcp_starting, /* Called when we want the lower layer up */
170
  lcp_finished, /* Called when we want the lower layer down */
171
  NULL,         /* Called when Protocol-Reject received */
172
  NULL,         /* Retransmission is necessary */
173
  lcp_extcode,  /* Called to handle LCP-specific codes */
174
  "LCP"         /* String name of protocol */
175
};
176
 
177
struct protent lcp_protent = {
178
    PPP_LCP,
179
    lcp_init,
180
    lcp_input,
181
    lcp_protrej,
182
    lcp_lowerup,
183
    lcp_lowerdown,
184
    lcp_open,
185
    lcp_close,
186
#if 0
187
    lcp_printpkt,
188
    NULL,
189
#endif
190
    1,
191
    "LCP",
192
#if 0
193
    NULL,
194
    NULL,
195
    NULL
196
#endif
197
};
198
 
199
int lcp_loopbackfail = DEFLOOPBACKFAIL;
200
 
201
 
202
 
203
/***********************************/
204
/*** PUBLIC FUNCTION DEFINITIONS ***/
205
/***********************************/
206
/*
207
 * lcp_init - Initialize LCP.
208
 */
209
void
210
lcp_init(int unit)
211
{
212
  fsm         *f  = &lcp_fsm[unit];
213
  lcp_options *wo = &lcp_wantoptions[unit];
214
  lcp_options *ao = &lcp_allowoptions[unit];
215
 
216
  f->unit      = unit;
217
  f->protocol  = PPP_LCP;
218
  f->callbacks = &lcp_callbacks;
219
 
220
  fsm_init(f);
221
 
222
  wo->passive           = 0;
223
  wo->silent            = 0;
224
  wo->restart           = 0;               /* Set to 1 in kernels or multi-line implementations */
225
  wo->neg_mru           = 1;
226
  wo->mru               = PPP_DEFMRU;
227
  wo->neg_asyncmap      = 1;
228
  wo->asyncmap          = 0x00000000l;     /* Assume don't need to escape any ctl chars. */
229
  wo->neg_chap          = 0;               /* Set to 1 on server */
230
  wo->neg_upap          = 0;               /* Set to 1 on server */
231
  wo->chap_mdtype       = CHAP_DIGEST_MD5;
232
  wo->neg_magicnumber   = 1;
233
  wo->neg_pcompression  = 1;
234
  wo->neg_accompression = 1;
235
  wo->neg_lqr           = 0;               /* no LQR implementation yet */
236
  wo->neg_cbcp          = 0;
237
 
238
  ao->neg_mru           = 1;
239
  ao->mru               = PPP_MAXMRU;
240
  ao->neg_asyncmap      = 1;
241
  ao->asyncmap          = 0x00000000l;     /* Assume don't need to escape any ctl chars. */
242
  ao->neg_chap          = (CHAP_SUPPORT != 0);
243
  ao->chap_mdtype       = CHAP_DIGEST_MD5;
244
  ao->neg_upap          = (PAP_SUPPORT != 0);
245
  ao->neg_magicnumber   = 1;
246
  ao->neg_pcompression  = 1;
247
  ao->neg_accompression = 1;
248
  ao->neg_lqr           = 0;               /* no LQR implementation yet */
249
  ao->neg_cbcp          = (CBCP_SUPPORT != 0);
250
 
251
  /*
252
   * Set transmit escape for the flag and escape characters plus anything
253
   * set for the allowable options.
254
   */
255
  memset(xmit_accm[unit], 0, sizeof(xmit_accm[0]));
256
  xmit_accm[unit][15] = 0x60;
257
  xmit_accm[unit][0]  = (u8_t)((ao->asyncmap        & 0xFF));
258
  xmit_accm[unit][1]  = (u8_t)((ao->asyncmap >> 8)  & 0xFF);
259
  xmit_accm[unit][2]  = (u8_t)((ao->asyncmap >> 16) & 0xFF);
260
  xmit_accm[unit][3]  = (u8_t)((ao->asyncmap >> 24) & 0xFF);
261
  LCPDEBUG((LOG_INFO, "lcp_init: xmit_accm=%X %X %X %X\n",
262
        xmit_accm[unit][0],
263
        xmit_accm[unit][1],
264
        xmit_accm[unit][2],
265
        xmit_accm[unit][3]));
266
 
267
  lcp_phase[unit] = PHASE_INITIALIZE;
268
}
269
 
270
 
271
/*
272
 * lcp_open - LCP is allowed to come up.
273
 */
274
void
275
lcp_open(int unit)
276
{
277
  fsm         *f  = &lcp_fsm[unit];
278
  lcp_options *wo = &lcp_wantoptions[unit];
279
 
280
  f->flags = 0;
281
  if (wo->passive) {
282
    f->flags |= OPT_PASSIVE;
283
  }
284
  if (wo->silent) {
285
    f->flags |= OPT_SILENT;
286
  }
287
  fsm_open(f);
288
 
289
  lcp_phase[unit] = PHASE_ESTABLISH;
290
}
291
 
292
 
293
/*
294
 * lcp_close - Take LCP down.
295
 */
296
void
297
lcp_close(int unit, char *reason)
298
{
299
  fsm *f = &lcp_fsm[unit];
300
 
301
  if (lcp_phase[unit] != PHASE_DEAD) {
302
    lcp_phase[unit] = PHASE_TERMINATE;
303
  }
304
  if (f->state == LS_STOPPED && f->flags & (OPT_PASSIVE|OPT_SILENT)) {
305
    /*
306
     * This action is not strictly according to the FSM in RFC1548,
307
     * but it does mean that the program terminates if you do an
308
     * lcp_close() in passive/silent mode when a connection hasn't
309
     * been established.
310
     */
311
    f->state = LS_CLOSED;
312
    lcp_finished(f);
313
  } else {
314
    fsm_close(&lcp_fsm[unit], reason);
315
  }
316
}
317
 
318
 
319
/*
320
 * lcp_lowerup - The lower layer is up.
321
 */
322
void
323
lcp_lowerup(int unit)
324
{
325
  lcp_options *wo = &lcp_wantoptions[unit];
326
 
327
  /*
328
   * Don't use A/C or protocol compression on transmission,
329
   * but accept A/C and protocol compressed packets
330
   * if we are going to ask for A/C and protocol compression.
331
   */
332
  ppp_set_xaccm(unit, &xmit_accm[unit]);
333
  ppp_send_config(unit, PPP_MRU, 0xffffffffl, 0, 0);
334
  ppp_recv_config(unit, PPP_MRU, 0x00000000l,
335
  wo->neg_pcompression, wo->neg_accompression);
336
  peer_mru[unit] = PPP_MRU;
337
  lcp_allowoptions[unit].asyncmap = (u32_t)xmit_accm[unit][0]
338
                                 | ((u32_t)xmit_accm[unit][1] << 8)
339
                                 | ((u32_t)xmit_accm[unit][2] << 16)
340
                                 | ((u32_t)xmit_accm[unit][3] << 24);
341
  LCPDEBUG((LOG_INFO, "lcp_lowerup: asyncmap=%X %X %X %X\n",
342
            xmit_accm[unit][3],
343
            xmit_accm[unit][2],
344
            xmit_accm[unit][1],
345
            xmit_accm[unit][0]));
346
 
347
  fsm_lowerup(&lcp_fsm[unit]);
348
}
349
 
350
 
351
/*
352
 * lcp_lowerdown - The lower layer is down.
353
 */
354
void
355
lcp_lowerdown(int unit)
356
{
357
  fsm_lowerdown(&lcp_fsm[unit]);
358
}
359
 
360
/*
361
 * lcp_sprotrej - Send a Protocol-Reject for some protocol.
362
 */
363
void
364
lcp_sprotrej(int unit, u8_t *p, int len)
365
{
366
  /*
367
   * Send back the protocol and the information field of the
368
   * rejected packet.  We only get here if LCP is in the LS_OPENED state.
369
   */
370
 
371
  fsm_sdata(&lcp_fsm[unit], PROTREJ, ++lcp_fsm[unit].id, p, len);
372
}
373
 
374
 
375
 
376
/**********************************/
377
/*** LOCAL FUNCTION DEFINITIONS ***/
378
/**********************************/
379
/*
380
 * lcp_input - Input LCP packet.
381
 */
382
static void
383
lcp_input(int unit, u8_t *p, int len)
384
{
385
  fsm *f = &lcp_fsm[unit];
386
 
387
  fsm_input(f, p, len);
388
}
389
 
390
 
391
/*
392
 * lcp_extcode - Handle a LCP-specific code.
393
 */
394
static int
395
lcp_extcode(fsm *f, int code, u8_t id, u8_t *inp, int len)
396
{
397
  u8_t *magp;
398
 
399
  switch( code ){
400
    case PROTREJ:
401
      lcp_rprotrej(f, inp, len);
402
      break;
403
 
404
    case ECHOREQ:
405
      if (f->state != LS_OPENED) {
406
        break;
407
      }
408
      LCPDEBUG((LOG_INFO, "lcp: Echo-Request, Rcvd id %d\n", id));
409
      magp = inp;
410
      PUTLONG(lcp_gotoptions[f->unit].magicnumber, magp);
411
      fsm_sdata(f, ECHOREP, id, inp, len);
412
      break;
413
 
414
    case ECHOREP:
415
      lcp_received_echo_reply(f, id, inp, len);
416
      break;
417
 
418
    case DISCREQ:
419
      break;
420
 
421
    default:
422
      return 0;
423
  }
424
  return 1;
425
}
426
 
427
 
428
/*
429
 * lcp_rprotrej - Receive an Protocol-Reject.
430
 *
431
 * Figure out which protocol is rejected and inform it.
432
 */
433
static void
434
lcp_rprotrej(fsm *f, u8_t *inp, int len)
435
{
436
  int i;
437
  struct protent *protp;
438
  u16_t prot;
439
 
440
  if (len < sizeof (u16_t)) {
441
    LCPDEBUG((LOG_INFO, "lcp_rprotrej: Rcvd short Protocol-Reject packet!\n"));
442
    return;
443
  }
444
 
445
  GETSHORT(prot, inp);
446
 
447
  LCPDEBUG((LOG_INFO, "lcp_rprotrej: Rcvd Protocol-Reject packet for %x!\n", prot));
448
 
449
  /*
450
   * Protocol-Reject packets received in any state other than the LCP
451
   * LS_OPENED state SHOULD be silently discarded.
452
   */
453
  if( f->state != LS_OPENED ) {
454
    LCPDEBUG((LOG_INFO, "Protocol-Reject discarded: LCP in state %d\n", f->state));
455
    return;
456
  }
457
 
458
  /*
459
   * Upcall the proper Protocol-Reject routine.
460
   */
461
  for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
462
    if (protp->protocol == prot && protp->enabled_flag) {
463
      (*protp->protrej)(f->unit);
464
      return;
465
    }
466
  }
467
 
468
  LCPDEBUG((LOG_WARNING, "Protocol-Reject for unsupported protocol 0x%x\n", prot));
469
}
470
 
471
 
472
/*
473
 * lcp_protrej - A Protocol-Reject was received.
474
 */
475
static void
476
lcp_protrej(int unit)
477
{
478
  LWIP_UNUSED_ARG(unit);
479
  /*
480
   * Can't reject LCP!
481
   */
482
  LCPDEBUG((LOG_WARNING, "lcp_protrej: Received Protocol-Reject for LCP!\n"));
483
  fsm_protreject(&lcp_fsm[unit]);
484
}
485
 
486
 
487
/*
488
 * lcp_resetci - Reset our CI.
489
 */
490
static void
491
lcp_resetci(fsm *f)
492
{
493
  lcp_wantoptions[f->unit].magicnumber = magic_next();
494
  lcp_wantoptions[f->unit].numloops = 0;
495
  lcp_gotoptions[f->unit] = lcp_wantoptions[f->unit];
496
  peer_mru[f->unit] = PPP_MRU;
497
  auth_reset(f->unit);
498
}
499
 
500
 
501
/*
502
 * lcp_cilen - Return length of our CI.
503
 */
504
static int lcp_cilen(fsm *f)
505
{
506
  lcp_options *go = &lcp_gotoptions[f->unit];
507
 
508
#define LENCIVOID(neg)  ((neg) ? CILEN_VOID : 0)
509
#define LENCICHAP(neg)  ((neg) ? CILEN_CHAP : 0)
510
#define LENCISHORT(neg) ((neg) ? CILEN_SHORT : 0)
511
#define LENCILONG(neg)  ((neg) ? CILEN_LONG : 0)
512
#define LENCILQR(neg)   ((neg) ? CILEN_LQR: 0)
513
#define LENCICBCP(neg)  ((neg) ? CILEN_CBCP: 0)
514
  /*
515
   * NB: we only ask for one of CHAP and UPAP, even if we will
516
   * accept either.
517
   */
518
  return (LENCISHORT(go->neg_mru && go->mru != PPP_DEFMRU) +
519
          LENCILONG(go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) +
520
          LENCICHAP(go->neg_chap) +
521
          LENCISHORT(!go->neg_chap && go->neg_upap) +
522
          LENCILQR(go->neg_lqr) +
523
          LENCICBCP(go->neg_cbcp) +
524
          LENCILONG(go->neg_magicnumber) +
525
          LENCIVOID(go->neg_pcompression) +
526
          LENCIVOID(go->neg_accompression));
527
}
528
 
529
 
530
/*
531
 * lcp_addci - Add our desired CIs to a packet.
532
 */
533
static void
534
lcp_addci(fsm *f, u8_t *ucp, int *lenp)
535
{
536
  lcp_options *go = &lcp_gotoptions[f->unit];
537
  u8_t *start_ucp = ucp;
538
 
539
#define ADDCIVOID(opt, neg) \
540
  if (neg) { \
541
    LCPDEBUG((LOG_INFO, "lcp_addci: opt=%d\n", opt)); \
542
    PUTCHAR(opt, ucp); \
543
    PUTCHAR(CILEN_VOID, ucp); \
544
  }
545
#define ADDCISHORT(opt, neg, val) \
546
  if (neg) { \
547
    LCPDEBUG((LOG_INFO, "lcp_addci: INT opt=%d %X\n", opt, val)); \
548
    PUTCHAR(opt, ucp); \
549
    PUTCHAR(CILEN_SHORT, ucp); \
550
    PUTSHORT(val, ucp); \
551
  }
552
#define ADDCICHAP(opt, neg, val, digest) \
553
  if (neg) { \
554
    LCPDEBUG((LOG_INFO, "lcp_addci: CHAP opt=%d %X\n", opt, val)); \
555
    PUTCHAR(opt, ucp); \
556
    PUTCHAR(CILEN_CHAP, ucp); \
557
    PUTSHORT(val, ucp); \
558
    PUTCHAR(digest, ucp); \
559
  }
560
#define ADDCILONG(opt, neg, val) \
561
  if (neg) { \
562
    LCPDEBUG((LOG_INFO, "lcp_addci: L opt=%d %lX\n", opt, val)); \
563
    PUTCHAR(opt, ucp); \
564
    PUTCHAR(CILEN_LONG, ucp); \
565
    PUTLONG(val, ucp); \
566
  }
567
#define ADDCILQR(opt, neg, val) \
568
  if (neg) { \
569
    LCPDEBUG((LOG_INFO, "lcp_addci: LQR opt=%d %lX\n", opt, val)); \
570
    PUTCHAR(opt, ucp); \
571
    PUTCHAR(CILEN_LQR, ucp); \
572
    PUTSHORT(PPP_LQR, ucp); \
573
    PUTLONG(val, ucp); \
574
  }
575
#define ADDCICHAR(opt, neg, val) \
576
  if (neg) { \
577
    LCPDEBUG((LOG_INFO, "lcp_addci: CHAR opt=%d %X '%z'\n", opt, val, val)); \
578
    PUTCHAR(opt, ucp); \
579
    PUTCHAR(CILEN_CHAR, ucp); \
580
    PUTCHAR(val, ucp); \
581
  }
582
 
583
  ADDCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
584
  ADDCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl, go->asyncmap);
585
  ADDCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
586
  ADDCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
587
  ADDCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
588
  ADDCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
589
  ADDCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
590
  ADDCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
591
  ADDCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
592
 
593
  if (ucp - start_ucp != *lenp) {
594
    /* this should never happen, because peer_mtu should be 1500 */
595
    LCPDEBUG((LOG_ERR, "Bug in lcp_addci: wrong length\n"));
596
  }
597
}
598
 
599
 
600
/*
601
 * lcp_ackci - Ack our CIs.
602
 * This should not modify any state if the Ack is bad.
603
 *
604
 * Returns:
605
 *  0 - Ack was bad.
606
 *  1 - Ack was good.
607
 */
608
static int
609
lcp_ackci(fsm *f, u8_t *p, int len)
610
{
611
  lcp_options *go = &lcp_gotoptions[f->unit];
612
  u8_t cilen, citype, cichar;
613
  u16_t cishort;
614
  u32_t cilong;
615
 
616
  /*
617
   * CIs must be in exactly the same order that we sent.
618
   * Check packet length and CI length at each step.
619
   * If we find any deviations, then this packet is bad.
620
   */
621
#define ACKCIVOID(opt, neg) \
622
  if (neg) { \
623
    if ((len -= CILEN_VOID) < 0) \
624
      goto bad; \
625
    GETCHAR(citype, p); \
626
    GETCHAR(cilen, p); \
627
    if (cilen != CILEN_VOID || citype != opt) \
628
      goto bad; \
629
  }
630
#define ACKCISHORT(opt, neg, val) \
631
  if (neg) { \
632
    if ((len -= CILEN_SHORT) < 0) \
633
      goto bad; \
634
    GETCHAR(citype, p); \
635
    GETCHAR(cilen, p); \
636
    if (cilen != CILEN_SHORT || citype != opt) \
637
      goto bad; \
638
    GETSHORT(cishort, p); \
639
    if (cishort != val) \
640
      goto bad; \
641
  }
642
#define ACKCICHAR(opt, neg, val) \
643
  if (neg) { \
644
    if ((len -= CILEN_CHAR) < 0) \
645
      goto bad; \
646
    GETCHAR(citype, p); \
647
    GETCHAR(cilen, p); \
648
    if (cilen != CILEN_CHAR || citype != opt) \
649
      goto bad; \
650
    GETCHAR(cichar, p); \
651
    if (cichar != val) \
652
      goto bad; \
653
  }
654
#define ACKCICHAP(opt, neg, val, digest) \
655
  if (neg) { \
656
    if ((len -= CILEN_CHAP) < 0) \
657
      goto bad; \
658
    GETCHAR(citype, p); \
659
    GETCHAR(cilen, p); \
660
    if (cilen != CILEN_CHAP || citype != opt) \
661
      goto bad; \
662
    GETSHORT(cishort, p); \
663
    if (cishort != val) \
664
      goto bad; \
665
    GETCHAR(cichar, p); \
666
    if (cichar != digest) \
667
      goto bad; \
668
  }
669
#define ACKCILONG(opt, neg, val) \
670
  if (neg) { \
671
    if ((len -= CILEN_LONG) < 0) \
672
      goto bad; \
673
    GETCHAR(citype, p); \
674
    GETCHAR(cilen, p); \
675
    if (cilen != CILEN_LONG ||  citype != opt) \
676
      goto bad; \
677
    GETLONG(cilong, p); \
678
    if (cilong != val) \
679
      goto bad; \
680
  }
681
#define ACKCILQR(opt, neg, val) \
682
  if (neg) { \
683
    if ((len -= CILEN_LQR) < 0) \
684
      goto bad; \
685
    GETCHAR(citype, p); \
686
    GETCHAR(cilen, p); \
687
    if (cilen != CILEN_LQR || citype != opt) \
688
      goto bad; \
689
    GETSHORT(cishort, p); \
690
    if (cishort != PPP_LQR) \
691
      goto bad; \
692
    GETLONG(cilong, p); \
693
    if (cilong != val) \
694
      goto bad; \
695
  }
696
 
697
  ACKCISHORT(CI_MRU, go->neg_mru && go->mru != PPP_DEFMRU, go->mru);
698
  ACKCILONG(CI_ASYNCMAP, go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl, go->asyncmap);
699
  ACKCICHAP(CI_AUTHTYPE, go->neg_chap, PPP_CHAP, go->chap_mdtype);
700
  ACKCISHORT(CI_AUTHTYPE, !go->neg_chap && go->neg_upap, PPP_PAP);
701
  ACKCILQR(CI_QUALITY, go->neg_lqr, go->lqr_period);
702
  ACKCICHAR(CI_CALLBACK, go->neg_cbcp, CBCP_OPT);
703
  ACKCILONG(CI_MAGICNUMBER, go->neg_magicnumber, go->magicnumber);
704
  ACKCIVOID(CI_PCOMPRESSION, go->neg_pcompression);
705
  ACKCIVOID(CI_ACCOMPRESSION, go->neg_accompression);
706
 
707
  /*
708
   * If there are any remaining CIs, then this packet is bad.
709
   */
710
  if (len != 0) {
711
    goto bad;
712
  }
713
  LCPDEBUG((LOG_INFO, "lcp_acki: Ack\n"));
714
  return (1);
715
bad:
716
  LCPDEBUG((LOG_WARNING, "lcp_acki: received bad Ack!\n"));
717
  return (0);
718
}
719
 
720
 
721
/*
722
 * lcp_nakci - Peer has sent a NAK for some of our CIs.
723
 * This should not modify any state if the Nak is bad
724
 * or if LCP is in the LS_OPENED state.
725
 *
726
 * Returns:
727
 *  0 - Nak was bad.
728
 *  1 - Nak was good.
729
 */
730
static int
731
lcp_nakci(fsm *f, u8_t *p, int len)
732
{
733
  lcp_options *go = &lcp_gotoptions[f->unit];
734
  lcp_options *wo = &lcp_wantoptions[f->unit];
735
  u8_t citype, cichar, *next;
736
  u16_t cishort;
737
  u32_t cilong;
738
  lcp_options no;     /* options we've seen Naks for */
739
  lcp_options try;    /* options to request next time */
740
  int looped_back = 0;
741
  int cilen;
742
 
743
  BZERO(&no, sizeof(no));
744
  try = *go;
745
 
746
  /*
747
   * Any Nak'd CIs must be in exactly the same order that we sent.
748
   * Check packet length and CI length at each step.
749
   * If we find any deviations, then this packet is bad.
750
   */
751
#define NAKCIVOID(opt, neg, code) \
752
  if (go->neg && \
753
      len >= CILEN_VOID && \
754
      p[1] == CILEN_VOID && \
755
      p[0] == opt) { \
756
    len -= CILEN_VOID; \
757
    INCPTR(CILEN_VOID, p); \
758
    no.neg = 1; \
759
    code \
760
  }
761
#define NAKCICHAP(opt, neg, code) \
762
  if (go->neg && \
763
      len >= CILEN_CHAP && \
764
      p[1] == CILEN_CHAP && \
765
      p[0] == opt) { \
766
    len -= CILEN_CHAP; \
767
    INCPTR(2, p); \
768
    GETSHORT(cishort, p); \
769
    GETCHAR(cichar, p); \
770
    no.neg = 1; \
771
    code \
772
  }
773
#define NAKCICHAR(opt, neg, code) \
774
  if (go->neg && \
775
      len >= CILEN_CHAR && \
776
      p[1] == CILEN_CHAR && \
777
      p[0] == opt) { \
778
    len -= CILEN_CHAR; \
779
    INCPTR(2, p); \
780
    GETCHAR(cichar, p); \
781
    no.neg = 1; \
782
    code \
783
  }
784
#define NAKCISHORT(opt, neg, code) \
785
  if (go->neg && \
786
      len >= CILEN_SHORT && \
787
      p[1] == CILEN_SHORT && \
788
      p[0] == opt) { \
789
    len -= CILEN_SHORT; \
790
    INCPTR(2, p); \
791
    GETSHORT(cishort, p); \
792
    no.neg = 1; \
793
    code \
794
  }
795
#define NAKCILONG(opt, neg, code) \
796
  if (go->neg && \
797
      len >= CILEN_LONG && \
798
      p[1] == CILEN_LONG && \
799
      p[0] == opt) { \
800
    len -= CILEN_LONG; \
801
    INCPTR(2, p); \
802
    GETLONG(cilong, p); \
803
    no.neg = 1; \
804
    code \
805
  }
806
#define NAKCILQR(opt, neg, code) \
807
  if (go->neg && \
808
      len >= CILEN_LQR && \
809
      p[1] == CILEN_LQR && \
810
      p[0] == opt) { \
811
    len -= CILEN_LQR; \
812
    INCPTR(2, p); \
813
    GETSHORT(cishort, p); \
814
    GETLONG(cilong, p); \
815
    no.neg = 1; \
816
    code \
817
  }
818
 
819
  /*
820
   * We don't care if they want to send us smaller packets than
821
   * we want.  Therefore, accept any MRU less than what we asked for,
822
   * but then ignore the new value when setting the MRU in the kernel.
823
   * If they send us a bigger MRU than what we asked, accept it, up to
824
   * the limit of the default MRU we'd get if we didn't negotiate.
825
   */
826
  if (go->neg_mru && go->mru != PPP_DEFMRU) {
827
    NAKCISHORT(CI_MRU, neg_mru,
828
      if (cishort <= wo->mru || cishort < PPP_DEFMRU) {
829
        try.mru = cishort;
830
      }
831
    );
832
  }
833
 
834
  /*
835
   * Add any characters they want to our (receive-side) asyncmap.
836
   */
837
  if (go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl) {
838
    NAKCILONG(CI_ASYNCMAP, neg_asyncmap,
839
      try.asyncmap = go->asyncmap | cilong;
840
    );
841
  }
842
 
843
  /*
844
   * If they've nak'd our authentication-protocol, check whether
845
   * they are proposing a different protocol, or a different
846
   * hash algorithm for CHAP.
847
   */
848
  if ((go->neg_chap || go->neg_upap)
849
      && len >= CILEN_SHORT
850
      && p[0] == CI_AUTHTYPE && p[1] >= CILEN_SHORT && p[1] <= len) {
851
    cilen = p[1];
852
    len -= cilen;
853
    no.neg_chap = go->neg_chap;
854
    no.neg_upap = go->neg_upap;
855
    INCPTR(2, p);
856
    GETSHORT(cishort, p);
857
    if (cishort == PPP_PAP && cilen == CILEN_SHORT) {
858
      /*
859
       * If we were asking for CHAP, they obviously don't want to do it.
860
       * If we weren't asking for CHAP, then we were asking for PAP,
861
       * in which case this Nak is bad.
862
       */
863
      if (!go->neg_chap) {
864
        goto bad;
865
      }
866
      try.neg_chap = 0;
867
 
868
    } else if (cishort == PPP_CHAP && cilen == CILEN_CHAP) {
869
      GETCHAR(cichar, p);
870
      if (go->neg_chap) {
871
        /*
872
         * We were asking for CHAP/MD5; they must want a different
873
         * algorithm.  If they can't do MD5, we'll have to stop
874
         * asking for CHAP.
875
         */
876
        if (cichar != go->chap_mdtype) {
877
          try.neg_chap = 0;
878
        }
879
      } else {
880
        /*
881
         * Stop asking for PAP if we were asking for it.
882
         */
883
        try.neg_upap = 0;
884
      }
885
 
886
    } else {
887
      /*
888
       * We don't recognize what they're suggesting.
889
       * Stop asking for what we were asking for.
890
       */
891
      if (go->neg_chap) {
892
        try.neg_chap = 0;
893
      } else {
894
        try.neg_upap = 0;
895
      }
896
      p += cilen - CILEN_SHORT;
897
    }
898
  }
899
 
900
  /*
901
   * If they can't cope with our link quality protocol, we'll have
902
   * to stop asking for LQR.  We haven't got any other protocol.
903
   * If they Nak the reporting period, take their value XXX ?
904
   */
905
  NAKCILQR(CI_QUALITY, neg_lqr,
906
    if (cishort != PPP_LQR) {
907
      try.neg_lqr = 0;
908
    } else {
909
      try.lqr_period = cilong;
910
    }
911
  );
912
 
913
  /*
914
   * Only implementing CBCP...not the rest of the callback options
915
   */
916
  NAKCICHAR(CI_CALLBACK, neg_cbcp,
917
    try.neg_cbcp = 0;
918
  );
919
 
920
  /*
921
   * Check for a looped-back line.
922
   */
923
  NAKCILONG(CI_MAGICNUMBER, neg_magicnumber,
924
    try.magicnumber = magic_next();
925
    looped_back = 1;
926
  );
927
 
928
  /*
929
   * Peer shouldn't send Nak for protocol compression or
930
   * address/control compression requests; they should send
931
   * a Reject instead.  If they send a Nak, treat it as a Reject.
932
   */
933
  NAKCIVOID(CI_PCOMPRESSION, neg_pcompression,
934
    try.neg_pcompression = 0;
935
  );
936
  NAKCIVOID(CI_ACCOMPRESSION, neg_accompression,
937
    try.neg_accompression = 0;
938
  );
939
 
940
  /*
941
   * There may be remaining CIs, if the peer is requesting negotiation
942
   * on an option that we didn't include in our request packet.
943
   * If we see an option that we requested, or one we've already seen
944
   * in this packet, then this packet is bad.
945
   * If we wanted to respond by starting to negotiate on the requested
946
   * option(s), we could, but we don't, because except for the
947
   * authentication type and quality protocol, if we are not negotiating
948
   * an option, it is because we were told not to.
949
   * For the authentication type, the Nak from the peer means
950
   * `let me authenticate myself with you' which is a bit pointless.
951
   * For the quality protocol, the Nak means `ask me to send you quality
952
   * reports', but if we didn't ask for them, we don't want them.
953
   * An option we don't recognize represents the peer asking to
954
   * negotiate some option we don't support, so ignore it.
955
   */
956
  while (len > CILEN_VOID) {
957
    GETCHAR(citype, p);
958
    GETCHAR(cilen, p);
959
    if (cilen < CILEN_VOID || (len -= cilen) < 0) {
960
      goto bad;
961
    }
962
    next = p + cilen - 2;
963
 
964
    switch (citype) {
965
      case CI_MRU:
966
        if ((go->neg_mru && go->mru != PPP_DEFMRU)
967
            || no.neg_mru || cilen != CILEN_SHORT) {
968
          goto bad;
969
        }
970
        GETSHORT(cishort, p);
971
        if (cishort < PPP_DEFMRU) {
972
          try.mru = cishort;
973
        }
974
        break;
975
      case CI_ASYNCMAP:
976
        if ((go->neg_asyncmap && go->asyncmap != 0xFFFFFFFFl)
977
            || no.neg_asyncmap || cilen != CILEN_LONG) {
978
          goto bad;
979
        }
980
        break;
981
      case CI_AUTHTYPE:
982
        if (go->neg_chap || no.neg_chap || go->neg_upap || no.neg_upap) {
983
          goto bad;
984
        }
985
        break;
986
      case CI_MAGICNUMBER:
987
        if (go->neg_magicnumber || no.neg_magicnumber ||
988
            cilen != CILEN_LONG) {
989
          goto bad;
990
        }
991
        break;
992
      case CI_PCOMPRESSION:
993
        if (go->neg_pcompression || no.neg_pcompression
994
            || cilen != CILEN_VOID) {
995
          goto bad;
996
        }
997
        break;
998
      case CI_ACCOMPRESSION:
999
        if (go->neg_accompression || no.neg_accompression
1000
            || cilen != CILEN_VOID) {
1001
          goto bad;
1002
        }
1003
        break;
1004
      case CI_QUALITY:
1005
        if (go->neg_lqr || no.neg_lqr || cilen != CILEN_LQR) {
1006
          goto bad;
1007
        }
1008
        break;
1009
    }
1010
    p = next;
1011
  }
1012
 
1013
  /* If there is still anything left, this packet is bad. */
1014
  if (len != 0) {
1015
    goto bad;
1016
  }
1017
 
1018
  /*
1019
  * OK, the Nak is good.  Now we can update state.
1020
  */
1021
  if (f->state != LS_OPENED) {
1022
    if (looped_back) {
1023
      if (++try.numloops >= lcp_loopbackfail) {
1024
        LCPDEBUG((LOG_NOTICE, "Serial line is looped back.\n"));
1025
        lcp_close(f->unit, "Loopback detected");
1026
      }
1027
    } else {
1028
      try.numloops = 0;
1029
    }
1030
    *go = try;
1031
  }
1032
 
1033
  return 1;
1034
 
1035
bad:
1036
  LCPDEBUG((LOG_WARNING, "lcp_nakci: received bad Nak!\n"));
1037
  return 0;
1038
}
1039
 
1040
 
1041
/*
1042
 * lcp_rejci - Peer has Rejected some of our CIs.
1043
 * This should not modify any state if the Reject is bad
1044
 * or if LCP is in the LS_OPENED state.
1045
 *
1046
 * Returns:
1047
 *  0 - Reject was bad.
1048
 *  1 - Reject was good.
1049
 */
1050
static int
1051
lcp_rejci(fsm *f, u8_t *p, int len)
1052
{
1053
  lcp_options *go = &lcp_gotoptions[f->unit];
1054
  u8_t cichar;
1055
  u16_t cishort;
1056
  u32_t cilong;
1057
  lcp_options try; /* options to request next time */
1058
 
1059
  try = *go;
1060
 
1061
  /*
1062
   * Any Rejected CIs must be in exactly the same order that we sent.
1063
   * Check packet length and CI length at each step.
1064
   * If we find any deviations, then this packet is bad.
1065
   */
1066
#define REJCIVOID(opt, neg) \
1067
  if (go->neg && \
1068
      len >= CILEN_VOID && \
1069
      p[1] == CILEN_VOID && \
1070
      p[0] == opt) { \
1071
    len -= CILEN_VOID; \
1072
    INCPTR(CILEN_VOID, p); \
1073
    try.neg = 0; \
1074
    LCPDEBUG((LOG_INFO, "lcp_rejci: void opt %d rejected\n", opt)); \
1075
  }
1076
#define REJCISHORT(opt, neg, val) \
1077
  if (go->neg && \
1078
      len >= CILEN_SHORT && \
1079
      p[1] == CILEN_SHORT && \
1080
      p[0] == opt) { \
1081
    len -= CILEN_SHORT; \
1082
    INCPTR(2, p); \
1083
    GETSHORT(cishort, p); \
1084
    /* Check rejected value. */ \
1085
    if (cishort != val) { \
1086
      goto bad; \
1087
    } \
1088
    try.neg = 0; \
1089
    LCPDEBUG((LOG_INFO,"lcp_rejci: short opt %d rejected\n", opt)); \
1090
  }
1091
#define REJCICHAP(opt, neg, val, digest) \
1092
  if (go->neg && \
1093
      len >= CILEN_CHAP && \
1094
      p[1] == CILEN_CHAP && \
1095
      p[0] == opt) { \
1096
    len -= CILEN_CHAP; \
1097
    INCPTR(2, p); \
1098
    GETSHORT(cishort, p); \
1099
    GETCHAR(cichar, p); \
1100
    /* Check rejected value. */ \
1101
    if (cishort != val || cichar != digest) { \
1102
      goto bad; \
1103
    } \
1104
    try.neg = 0; \
1105
    try.neg_upap = 0; \
1106
    LCPDEBUG((LOG_INFO,"lcp_rejci: chap opt %d rejected\n", opt)); \
1107
  }
1108
#define REJCILONG(opt, neg, val) \
1109
  if (go->neg && \
1110
      len >= CILEN_LONG && \
1111
      p[1] == CILEN_LONG && \
1112
      p[0] == opt) { \
1113
    len -= CILEN_LONG; \
1114
    INCPTR(2, p); \
1115
    GETLONG(cilong, p); \
1116
    /* Check rejected value. */ \
1117
    if (cilong != val) { \
1118
      goto bad; \
1119
    } \
1120
    try.neg = 0; \
1121
    LCPDEBUG((LOG_INFO,"lcp_rejci: long opt %d rejected\n", opt)); \
1122
  }
1123
#define REJCILQR(opt, neg, val) \
1124
  if (go->neg && \
1125
      len >= CILEN_LQR && \
1126
      p[1] == CILEN_LQR && \
1127
      p[0] == opt) { \
1128
    len -= CILEN_LQR; \
1129
    INCPTR(2, p); \
1130
    GETSHORT(cishort, p); \
1131
    GETLONG(cilong, p); \
1132
    /* Check rejected value. */ \
1133
    if (cishort != PPP_LQR || cilong != val) { \
1134
      goto bad; \
1135
    } \
1136
    try.neg = 0; \
1137
    LCPDEBUG((LOG_INFO,"lcp_rejci: LQR opt %d rejected\n", opt)); \
1138
  }
1139
#define REJCICBCP(opt, neg, val) \
1140
  if (go->neg && \
1141
      len >= CILEN_CBCP && \
1142
      p[1] == CILEN_CBCP && \
1143
      p[0] == opt) { \
1144
    len -= CILEN_CBCP; \
1145
    INCPTR(2, p); \
1146
    GETCHAR(cichar, p); \
1147
    /* Check rejected value. */ \
1148
    if (cichar != val) { \
1149
      goto bad; \
1150
    } \
1151
    try.neg = 0; \
1152
    LCPDEBUG((LOG_INFO,"lcp_rejci: Callback opt %d rejected\n", opt)); \
1153
  }
1154
 
1155
  REJCISHORT(CI_MRU, neg_mru, go->mru);
1156
  REJCILONG(CI_ASYNCMAP, neg_asyncmap, go->asyncmap);
1157
  REJCICHAP(CI_AUTHTYPE, neg_chap, PPP_CHAP, go->chap_mdtype);
1158
  if (!go->neg_chap) {
1159
    REJCISHORT(CI_AUTHTYPE, neg_upap, PPP_PAP);
1160
  }
1161
  REJCILQR(CI_QUALITY, neg_lqr, go->lqr_period);
1162
  REJCICBCP(CI_CALLBACK, neg_cbcp, CBCP_OPT);
1163
  REJCILONG(CI_MAGICNUMBER, neg_magicnumber, go->magicnumber);
1164
  REJCIVOID(CI_PCOMPRESSION, neg_pcompression);
1165
  REJCIVOID(CI_ACCOMPRESSION, neg_accompression);
1166
 
1167
  /*
1168
   * If there are any remaining CIs, then this packet is bad.
1169
   */
1170
  if (len != 0) {
1171
    goto bad;
1172
  }
1173
  /*
1174
   * Now we can update state.
1175
   */
1176
  if (f->state != LS_OPENED) {
1177
    *go = try;
1178
  }
1179
  return 1;
1180
 
1181
bad:
1182
  LCPDEBUG((LOG_WARNING, "lcp_rejci: received bad Reject!\n"));
1183
  return 0;
1184
}
1185
 
1186
 
1187
/*
1188
 * lcp_reqci - Check the peer's requested CIs and send appropriate response.
1189
 *
1190
 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1191
 * appropriately.  If reject_if_disagree is non-zero, doesn't return
1192
 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1193
 */
1194
static int
1195
lcp_reqci(fsm *f,
1196
          u8_t *inp,    /* Requested CIs */
1197
          int *lenp,      /* Length of requested CIs */
1198
          int reject_if_disagree)
1199
{
1200
  lcp_options *go = &lcp_gotoptions[f->unit];
1201
  lcp_options *ho = &lcp_hisoptions[f->unit];
1202
  lcp_options *ao = &lcp_allowoptions[f->unit];
1203
  u8_t *cip, *next;         /* Pointer to current and next CIs */
1204
  int cilen, citype, cichar;  /* Parsed len, type, char value */
1205
  u16_t cishort;            /* Parsed short value */
1206
  u32_t cilong;               /* Parse long value */
1207
  int rc = CONFACK;           /* Final packet return code */
1208
  int orc;                    /* Individual option return code */
1209
  u8_t *p;                  /* Pointer to next char to parse */
1210
  u8_t *rejp;               /* Pointer to next char in reject frame */
1211
  u8_t *nakp;               /* Pointer to next char in Nak frame */
1212
  int l = *lenp;              /* Length left */
1213
#if TRACELCP > 0
1214
  char traceBuf[80];
1215
  int traceNdx = 0;
1216
#endif
1217
 
1218
  /*
1219
   * Reset all his options.
1220
   */
1221
  BZERO(ho, sizeof(*ho));
1222
 
1223
  /*
1224
   * Process all his options.
1225
   */
1226
  next = inp;
1227
  nakp = nak_buffer;
1228
  rejp = inp;
1229
  while (l) {
1230
    orc = CONFACK;      /* Assume success */
1231
    cip = p = next;     /* Remember begining of CI */
1232
    if (l < 2 ||        /* Not enough data for CI header or */
1233
        p[1] < 2 ||     /*  CI length too small or */
1234
        p[1] > l) {     /*  CI length too big? */
1235
      LCPDEBUG((LOG_WARNING, "lcp_reqci: bad CI length!\n"));
1236
      orc = CONFREJ;    /* Reject bad CI */
1237
      cilen = l;        /* Reject till end of packet */
1238
      l = 0;            /* Don't loop again */
1239
      citype = 0;
1240
      goto endswitch;
1241
    }
1242
    GETCHAR(citype, p); /* Parse CI type */
1243
    GETCHAR(cilen, p);  /* Parse CI length */
1244
    l -= cilen;         /* Adjust remaining length */
1245
    next += cilen;      /* Step to next CI */
1246
 
1247
    switch (citype) {   /* Check CI type */
1248
      case CI_MRU:
1249
        if (!ao->neg_mru) {    /* Allow option? */
1250
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - not allowed\n"));
1251
          orc = CONFREJ;    /* Reject CI */
1252
          break;
1253
        } else if (cilen != CILEN_SHORT) {  /* Check CI length */
1254
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject MRU - bad length\n"));
1255
          orc = CONFREJ;    /* Reject CI */
1256
          break;
1257
        }
1258
        GETSHORT(cishort, p);  /* Parse MRU */
1259
 
1260
        /*
1261
         * He must be able to receive at least our minimum.
1262
         * No need to check a maximum.  If he sends a large number,
1263
         * we'll just ignore it.
1264
         */
1265
        if (cishort < PPP_MINMRU) {
1266
          LCPDEBUG((LOG_INFO, "lcp_reqci: Nak - MRU too small\n"));
1267
          orc = CONFNAK;    /* Nak CI */
1268
          PUTCHAR(CI_MRU, nakp);
1269
          PUTCHAR(CILEN_SHORT, nakp);
1270
          PUTSHORT(PPP_MINMRU, nakp);  /* Give him a hint */
1271
          break;
1272
        }
1273
        ho->neg_mru = 1;    /* Remember he sent MRU */
1274
        ho->mru = cishort;    /* And remember value */
1275
#if TRACELCP > 0
1276
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " MRU %d", cishort);
1277
        traceNdx = strlen(traceBuf);
1278
#endif
1279
        break;
1280
 
1281
      case CI_ASYNCMAP:
1282
        if (!ao->neg_asyncmap) {
1283
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP not allowed\n"));
1284
          orc = CONFREJ;
1285
          break;
1286
        } else if (cilen != CILEN_LONG) {
1287
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject ASYNCMAP bad length\n"));
1288
          orc = CONFREJ;
1289
          break;
1290
        }
1291
        GETLONG(cilong, p);
1292
 
1293
        /*
1294
         * Asyncmap must have set at least the bits
1295
         * which are set in lcp_allowoptions[unit].asyncmap.
1296
         */
1297
        if ((ao->asyncmap & ~cilong) != 0) {
1298
          LCPDEBUG((LOG_INFO, "lcp_reqci: Nak ASYNCMAP %lX missing %lX\n",
1299
                    cilong, ao->asyncmap));
1300
          orc = CONFNAK;
1301
          PUTCHAR(CI_ASYNCMAP, nakp);
1302
          PUTCHAR(CILEN_LONG, nakp);
1303
          PUTLONG(ao->asyncmap | cilong, nakp);
1304
          break;
1305
        }
1306
        ho->neg_asyncmap = 1;
1307
        ho->asyncmap = cilong;
1308
#if TRACELCP > 0
1309
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " ASYNCMAP=%lX", cilong);
1310
        traceNdx = strlen(traceBuf);
1311
#endif
1312
        break;
1313
 
1314
      case CI_AUTHTYPE:
1315
        if (cilen < CILEN_SHORT) {
1316
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE missing arg\n"));
1317
          orc = CONFREJ;
1318
          break;
1319
        } else if (!(ao->neg_upap || ao->neg_chap)) {
1320
          /*
1321
           * Reject the option if we're not willing to authenticate.
1322
           */
1323
          LCPDEBUG((LOG_INFO, "lcp_reqci: Reject AUTHTYPE not allowed\n"));
1324
          orc = CONFREJ;
1325
          break;
1326
        }
1327
        GETSHORT(cishort, p);
1328
 
1329
        /*
1330
         * Authtype must be UPAP or CHAP.
1331
         *
1332
         * Note: if both ao->neg_upap and ao->neg_chap are set,
1333
         * and the peer sends a Configure-Request with two
1334
         * authenticate-protocol requests, one for CHAP and one
1335
         * for UPAP, then we will reject the second request.
1336
         * Whether we end up doing CHAP or UPAP depends then on
1337
         * the ordering of the CIs in the peer's Configure-Request.
1338
         */
1339
 
1340
        if (cishort == PPP_PAP) {
1341
          if (ho->neg_chap) {  /* we've already accepted CHAP */
1342
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP already accepted\n"));
1343
            orc = CONFREJ;
1344
            break;
1345
          } else if (cilen != CILEN_SHORT) {
1346
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE PAP bad len\n"));
1347
            orc = CONFREJ;
1348
            break;
1349
          }
1350
          if (!ao->neg_upap) {  /* we don't want to do PAP */
1351
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE PAP not allowed\n"));
1352
            orc = CONFNAK;  /* NAK it and suggest CHAP */
1353
            PUTCHAR(CI_AUTHTYPE, nakp);
1354
            PUTCHAR(CILEN_CHAP, nakp);
1355
            PUTSHORT(PPP_CHAP, nakp);
1356
            PUTCHAR(ao->chap_mdtype, nakp);
1357
            break;
1358
          }
1359
          ho->neg_upap = 1;
1360
#if TRACELCP > 0
1361
          snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " PAP (%X)", cishort);
1362
          traceNdx = strlen(traceBuf);
1363
#endif
1364
          break;
1365
        }
1366
        if (cishort == PPP_CHAP) {
1367
          if (ho->neg_upap) {  /* we've already accepted PAP */
1368
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP accepted PAP\n"));
1369
            orc = CONFREJ;
1370
            break;
1371
          } else if (cilen != CILEN_CHAP) {
1372
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Reject AUTHTYPE CHAP bad len\n"));
1373
            orc = CONFREJ;
1374
            break;
1375
          }
1376
          if (!ao->neg_chap) {  /* we don't want to do CHAP */
1377
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP not allowed\n"));
1378
            orc = CONFNAK;  /* NAK it and suggest PAP */
1379
            PUTCHAR(CI_AUTHTYPE, nakp);
1380
            PUTCHAR(CILEN_SHORT, nakp);
1381
            PUTSHORT(PPP_PAP, nakp);
1382
            break;
1383
          }
1384
          GETCHAR(cichar, p);  /* get digest type*/
1385
          if (cichar != CHAP_DIGEST_MD5
1386
#ifdef CHAPMS
1387
              && cichar != CHAP_MICROSOFT
1388
#endif
1389
          ) {
1390
            LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE CHAP digest=%d\n", cichar));
1391
            orc = CONFNAK;
1392
            PUTCHAR(CI_AUTHTYPE, nakp);
1393
            PUTCHAR(CILEN_CHAP, nakp);
1394
            PUTSHORT(PPP_CHAP, nakp);
1395
            PUTCHAR(ao->chap_mdtype, nakp);
1396
            break;
1397
          }
1398
#if TRACELCP > 0
1399
          snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CHAP %X,%d", cishort, cichar);
1400
          traceNdx = strlen(traceBuf);
1401
#endif
1402
          ho->chap_mdtype = cichar; /* save md type */
1403
          ho->neg_chap = 1;
1404
          break;
1405
        }
1406
 
1407
        /*
1408
         * We don't recognize the protocol they're asking for.
1409
         * Nak it with something we're willing to do.
1410
         * (At this point we know ao->neg_upap || ao->neg_chap.)
1411
         */
1412
        orc = CONFNAK;
1413
        PUTCHAR(CI_AUTHTYPE, nakp);
1414
        if (ao->neg_chap) {
1415
          LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req CHAP\n", cishort));
1416
          PUTCHAR(CILEN_CHAP, nakp);
1417
          PUTSHORT(PPP_CHAP, nakp);
1418
          PUTCHAR(ao->chap_mdtype, nakp);
1419
        } else {
1420
          LCPDEBUG((LOG_WARNING, "lcp_reqci: Nak AUTHTYPE %d req PAP\n", cishort));
1421
          PUTCHAR(CILEN_SHORT, nakp);
1422
          PUTSHORT(PPP_PAP, nakp);
1423
        }
1424
        break;
1425
 
1426
      case CI_QUALITY:
1427
        GETSHORT(cishort, p);
1428
        GETLONG(cilong, p);
1429
#if TRACELCP > 0
1430
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " QUALITY (%x %x)", cishort, (unsigned int) cilong);
1431
        traceNdx = strlen(traceBuf);
1432
#endif
1433
 
1434
        if (!ao->neg_lqr ||
1435
            cilen != CILEN_LQR) {
1436
          orc = CONFREJ;
1437
          break;
1438
        }
1439
 
1440
        /*
1441
         * Check the protocol and the reporting period.
1442
         * XXX When should we Nak this, and what with?
1443
         */
1444
        if (cishort != PPP_LQR) {
1445
          orc = CONFNAK;
1446
          PUTCHAR(CI_QUALITY, nakp);
1447
          PUTCHAR(CILEN_LQR, nakp);
1448
          PUTSHORT(PPP_LQR, nakp);
1449
          PUTLONG(ao->lqr_period, nakp);
1450
          break;
1451
        }
1452
        break;
1453
 
1454
      case CI_MAGICNUMBER:
1455
        if (!(ao->neg_magicnumber || go->neg_magicnumber) ||
1456
            cilen != CILEN_LONG) {
1457
          orc = CONFREJ;
1458
          break;
1459
        }
1460
        GETLONG(cilong, p);
1461
#if TRACELCP > 0
1462
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " MAGICNUMBER (%lX)", cilong);
1463
        traceNdx = strlen(traceBuf);
1464
#endif
1465
 
1466
        /*
1467
         * He must have a different magic number.
1468
         */
1469
        if (go->neg_magicnumber &&
1470
            cilong == go->magicnumber) {
1471
          cilong = magic_next();
1472
          orc = CONFNAK;
1473
          PUTCHAR(CI_MAGICNUMBER, nakp);
1474
          PUTCHAR(CILEN_LONG, nakp);
1475
          PUTLONG(cilong, nakp);
1476
          break;
1477
        }
1478
        ho->neg_magicnumber = 1;
1479
        ho->magicnumber = cilong;
1480
        break;
1481
 
1482
 
1483
      case CI_PCOMPRESSION:
1484
#if TRACELCP > 0
1485
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " PCOMPRESSION");
1486
        traceNdx = strlen(traceBuf);
1487
#endif
1488
        if (!ao->neg_pcompression ||
1489
            cilen != CILEN_VOID) {
1490
          orc = CONFREJ;
1491
          break;
1492
        }
1493
        ho->neg_pcompression = 1;
1494
        break;
1495
 
1496
      case CI_ACCOMPRESSION:
1497
#if TRACELCP > 0
1498
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " ACCOMPRESSION");
1499
        traceNdx = strlen(traceBuf);
1500
#endif
1501
        if (!ao->neg_accompression ||
1502
            cilen != CILEN_VOID) {
1503
          orc = CONFREJ;
1504
          break;
1505
        }
1506
        ho->neg_accompression = 1;
1507
        break;
1508
 
1509
      case CI_MRRU:
1510
#if TRACELCP > 0
1511
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_MRRU");
1512
        traceNdx = strlen(traceBuf);
1513
#endif
1514
        orc = CONFREJ;
1515
        break;
1516
 
1517
      case CI_SSNHF:
1518
#if TRACELCP > 0
1519
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_SSNHF");
1520
        traceNdx = strlen(traceBuf);
1521
#endif
1522
        orc = CONFREJ;
1523
        break;
1524
 
1525
      case CI_EPDISC:
1526
#if TRACELCP > 0
1527
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " CI_EPDISC");
1528
        traceNdx = strlen(traceBuf);
1529
#endif
1530
        orc = CONFREJ;
1531
        break;
1532
 
1533
      default:
1534
#if TRACELCP
1535
        snprintf(&traceBuf[traceNdx], sizeof(traceBuf), " unknown %d", citype);
1536
        traceNdx = strlen(traceBuf);
1537
#endif
1538
        orc = CONFREJ;
1539
        break;
1540
    }
1541
 
1542
  endswitch:
1543
#if TRACELCP
1544
    if (traceNdx >= 80 - 32) {
1545
      LCPDEBUG((LOG_INFO, "lcp_reqci: rcvd%s\n", traceBuf));
1546
      traceNdx = 0;
1547
    }
1548
#endif
1549
    if (orc == CONFACK && /* Good CI */
1550
        rc != CONFACK) {  /*  but prior CI wasnt? */
1551
      continue;           /* Don't send this one */
1552
    }
1553
 
1554
    if (orc == CONFNAK) {     /* Nak this CI? */
1555
      if (reject_if_disagree  /* Getting fed up with sending NAKs? */
1556
          && citype != CI_MAGICNUMBER) {
1557
        orc = CONFREJ;        /* Get tough if so */
1558
      } else {
1559
        if (rc == CONFREJ) {  /* Rejecting prior CI? */
1560
          continue;           /* Don't send this one */
1561
        }
1562
        rc = CONFNAK;
1563
      }
1564
    }
1565
    if (orc == CONFREJ) {        /* Reject this CI */
1566
      rc = CONFREJ;
1567
      if (cip != rejp) {         /* Need to move rejected CI? */
1568
        BCOPY(cip, rejp, cilen); /* Move it */
1569
      }
1570
      INCPTR(cilen, rejp);       /* Update output pointer */
1571
    }
1572
  }
1573
 
1574
  /*
1575
   * If we wanted to send additional NAKs (for unsent CIs), the
1576
   * code would go here.  The extra NAKs would go at *nakp.
1577
   * At present there are no cases where we want to ask the
1578
   * peer to negotiate an option.
1579
   */
1580
 
1581
  switch (rc) {
1582
    case CONFACK:
1583
      *lenp = (int)(next - inp);
1584
      break;
1585
    case CONFNAK:
1586
      /*
1587
       * Copy the Nak'd options from the nak_buffer to the caller's buffer.
1588
       */
1589
      *lenp = (int)(nakp - nak_buffer);
1590
      BCOPY(nak_buffer, inp, *lenp);
1591
      break;
1592
    case CONFREJ:
1593
      *lenp = (int)(rejp - inp);
1594
      break;
1595
  }
1596
 
1597
#if TRACELCP > 0
1598
  if (traceNdx > 0) {
1599
    LCPDEBUG((LOG_INFO, "lcp_reqci: %s\n", traceBuf));
1600
  }
1601
#endif
1602
  LCPDEBUG((LOG_INFO, "lcp_reqci: returning CONF%s.\n", CODENAME(rc)));
1603
  return (rc);      /* Return final code */
1604
}
1605
 
1606
 
1607
/*
1608
 * lcp_up - LCP has come UP.
1609
 */
1610
static void
1611
lcp_up(fsm *f)
1612
{
1613
  lcp_options *wo = &lcp_wantoptions[f->unit];
1614
  lcp_options *ho = &lcp_hisoptions[f->unit];
1615
  lcp_options *go = &lcp_gotoptions[f->unit];
1616
  lcp_options *ao = &lcp_allowoptions[f->unit];
1617
 
1618
  if (!go->neg_magicnumber) {
1619
    go->magicnumber = 0;
1620
  }
1621
  if (!ho->neg_magicnumber) {
1622
    ho->magicnumber = 0;
1623
  }
1624
 
1625
  /*
1626
   * Set our MTU to the smaller of the MTU we wanted and
1627
   * the MRU our peer wanted.  If we negotiated an MRU,
1628
   * set our MRU to the larger of value we wanted and
1629
   * the value we got in the negotiation.
1630
   */
1631
  ppp_send_config(f->unit, LWIP_MIN(ao->mru, (ho->neg_mru? ho->mru: PPP_MRU)),
1632
                 (ho->neg_asyncmap? ho->asyncmap: 0xffffffffl),
1633
                  ho->neg_pcompression, ho->neg_accompression);
1634
  /*
1635
   * If the asyncmap hasn't been negotiated, we really should
1636
   * set the receive asyncmap to ffffffff, but we set it to 0
1637
   * for backwards contemptibility.
1638
   */
1639
  ppp_recv_config(f->unit, (go->neg_mru? LWIP_MAX(wo->mru, go->mru): PPP_MRU),
1640
                 (go->neg_asyncmap? go->asyncmap: 0x00000000),
1641
                  go->neg_pcompression, go->neg_accompression);
1642
 
1643
  if (ho->neg_mru) {
1644
    peer_mru[f->unit] = ho->mru;
1645
  }
1646
 
1647
  lcp_echo_lowerup(f->unit); /* Enable echo messages */
1648
 
1649
  link_established(f->unit);
1650
}
1651
 
1652
 
1653
/*
1654
 * lcp_down - LCP has gone DOWN.
1655
 *
1656
 * Alert other protocols.
1657
 */
1658
static void
1659
lcp_down(fsm *f)
1660
{
1661
  lcp_options *go = &lcp_gotoptions[f->unit];
1662
 
1663
  lcp_echo_lowerdown(f->unit);
1664
 
1665
  link_down(f->unit);
1666
 
1667
  ppp_send_config(f->unit, PPP_MRU, 0xffffffffl, 0, 0);
1668
  ppp_recv_config(f->unit, PPP_MRU,
1669
                  (go->neg_asyncmap? go->asyncmap: 0x00000000),
1670
                   go->neg_pcompression, go->neg_accompression);
1671
  peer_mru[f->unit] = PPP_MRU;
1672
}
1673
 
1674
 
1675
/*
1676
 * lcp_starting - LCP needs the lower layer up.
1677
 */
1678
static void
1679
lcp_starting(fsm *f)
1680
{
1681
  link_required(f->unit);
1682
}
1683
 
1684
 
1685
/*
1686
 * lcp_finished - LCP has finished with the lower layer.
1687
 */
1688
static void
1689
lcp_finished(fsm *f)
1690
{
1691
  link_terminated(f->unit);
1692
}
1693
 
1694
 
1695
#if 0
1696
/*
1697
 * print_string - print a readable representation of a string using
1698
 * printer.
1699
 */
1700
static void
1701
print_string( char *p, int len, void (*printer) (void *, char *, ...), void *arg)
1702
{
1703
  int c;
1704
 
1705
  printer(arg, "\"");
1706
  for (; len > 0; --len) {
1707
    c = *p++;
1708
    if (' ' <= c && c <= '~') {
1709
        if (c == '\\' || c == '"') {
1710
          printer(arg, "\\");
1711
        }
1712
        printer(arg, "%c", c);
1713
    } else {
1714
      switch (c) {
1715
        case '\n':
1716
          printer(arg, "\\n");
1717
          break;
1718
        case '\r':
1719
          printer(arg, "\\r");
1720
          break;
1721
        case '\t':
1722
          printer(arg, "\\t");
1723
          break;
1724
        default:
1725
          printer(arg, "\\%.3o", c);
1726
        }
1727
    }
1728
  }
1729
  printer(arg, "\"");
1730
}
1731
 
1732
 
1733
/*
1734
 * lcp_printpkt - print the contents of an LCP packet.
1735
 */
1736
static char *lcp_codenames[] = {
1737
  "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1738
  "TermReq", "TermAck", "CodeRej", "ProtRej",
1739
  "EchoReq", "EchoRep", "DiscReq"
1740
};
1741
 
1742
static int
1743
lcp_printpkt( u8_t *p, int plen, void (*printer) (void *, char *, ...), void *arg)
1744
{
1745
  int code, id, len, olen;
1746
  u8_t *pstart, *optend;
1747
  u16_t cishort;
1748
  u32_t cilong;
1749
 
1750
  if (plen < HEADERLEN) {
1751
    return 0;
1752
  }
1753
  pstart = p;
1754
  GETCHAR(code, p);
1755
  GETCHAR(id, p);
1756
  GETSHORT(len, p);
1757
  if (len < HEADERLEN || len > plen) {
1758
    return 0;
1759
  }
1760
 
1761
  if (code >= 1 && code <= sizeof(lcp_codenames) / sizeof(char *)) {
1762
    printer(arg, " %s", lcp_codenames[code-1]);
1763
  } else {
1764
    printer(arg, " code=0x%x", code);
1765
  }
1766
  printer(arg, " id=0x%x", id);
1767
  len -= HEADERLEN;
1768
  switch (code) {
1769
    case CONFREQ:
1770
    case CONFACK:
1771
    case CONFNAK:
1772
    case CONFREJ:
1773
      /* print option list */
1774
      while (len >= 2) {
1775
        GETCHAR(code, p);
1776
        GETCHAR(olen, p);
1777
        p -= 2;
1778
        if (olen < 2 || olen > len) {
1779
          break;
1780
        }
1781
        printer(arg, " <");
1782
        len -= olen;
1783
        optend = p + olen;
1784
        switch (code) {
1785
          case CI_MRU:
1786
            if (olen == CILEN_SHORT) {
1787
              p += 2;
1788
              GETSHORT(cishort, p);
1789
              printer(arg, "mru %d", cishort);
1790
            }
1791
            break;
1792
          case CI_ASYNCMAP:
1793
            if (olen == CILEN_LONG) {
1794
              p += 2;
1795
              GETLONG(cilong, p);
1796
              printer(arg, "asyncmap 0x%lx", cilong);
1797
            }
1798
            break;
1799
          case CI_AUTHTYPE:
1800
            if (olen >= CILEN_SHORT) {
1801
              p += 2;
1802
              printer(arg, "auth ");
1803
              GETSHORT(cishort, p);
1804
              switch (cishort) {
1805
                case PPP_PAP:
1806
                  printer(arg, "pap");
1807
                  break;
1808
                case PPP_CHAP:
1809
                  printer(arg, "chap");
1810
                  break;
1811
                default:
1812
                  printer(arg, "0x%x", cishort);
1813
              }
1814
            }
1815
            break;
1816
          case CI_QUALITY:
1817
            if (olen >= CILEN_SHORT) {
1818
              p += 2;
1819
              printer(arg, "quality ");
1820
              GETSHORT(cishort, p);
1821
              switch (cishort) {
1822
                case PPP_LQR:
1823
                  printer(arg, "lqr");
1824
                  break;
1825
                default:
1826
                  printer(arg, "0x%x", cishort);
1827
              }
1828
            }
1829
            break;
1830
          case CI_CALLBACK:
1831
            if (olen >= CILEN_CHAR) {
1832
              p += 2;
1833
              printer(arg, "callback ");
1834
              GETSHORT(cishort, p);
1835
              switch (cishort) {
1836
                case CBCP_OPT:
1837
                  printer(arg, "CBCP");
1838
                  break;
1839
                default:
1840
                  printer(arg, "0x%x", cishort);
1841
              }
1842
            }
1843
            break;
1844
          case CI_MAGICNUMBER:
1845
            if (olen == CILEN_LONG) {
1846
              p += 2;
1847
              GETLONG(cilong, p);
1848
              printer(arg, "magic 0x%x", cilong);
1849
            }
1850
            break;
1851
          case CI_PCOMPRESSION:
1852
            if (olen == CILEN_VOID) {
1853
              p += 2;
1854
              printer(arg, "pcomp");
1855
            }
1856
            break;
1857
          case CI_ACCOMPRESSION:
1858
            if (olen == CILEN_VOID) {
1859
              p += 2;
1860
              printer(arg, "accomp");
1861
            }
1862
            break;
1863
        }
1864
        while (p < optend) {
1865
          GETCHAR(code, p);
1866
          printer(arg, " %.2x", code);
1867
        }
1868
        printer(arg, ">");
1869
      }
1870
      break;
1871
 
1872
    case TERMACK:
1873
    case TERMREQ:
1874
      if (len > 0 && *p >= ' ' && *p < 0x7f) {
1875
        printer(arg, " ");
1876
        print_string((char*)p, len, printer, arg);
1877
        p += len;
1878
        len = 0;
1879
      }
1880
      break;
1881
 
1882
    case ECHOREQ:
1883
    case ECHOREP:
1884
    case DISCREQ:
1885
      if (len >= 4) {
1886
        GETLONG(cilong, p);
1887
        printer(arg, " magic=0x%x", cilong);
1888
        p += 4;
1889
        len -= 4;
1890
      }
1891
      break;
1892
  }
1893
 
1894
  /* print the rest of the bytes in the packet */
1895
  for (; len > 0; --len) {
1896
    GETCHAR(code, p);
1897
    printer(arg, " %.2x", code);
1898
  }
1899
 
1900
  return (int)(p - pstart);
1901
}
1902
#endif
1903
 
1904
/*
1905
 * Time to shut down the link because there is nothing out there.
1906
 */
1907
static void
1908
lcp_link_failure (fsm *f)
1909
{
1910
  if (f->state == LS_OPENED) {
1911
    LCPDEBUG((LOG_INFO, "No response to %d echo-requests\n", lcp_echos_pending));
1912
    LCPDEBUG((LOG_NOTICE, "Serial link appears to be disconnected.\n"));
1913
    lcp_close(f->unit, "Peer not responding");
1914
  }
1915
}
1916
 
1917
/*
1918
 * Timer expired for the LCP echo requests from this process.
1919
 */
1920
static void
1921
lcp_echo_check (fsm *f)
1922
{
1923
  lcp_send_echo_request (f);
1924
 
1925
  /*
1926
   * Start the timer for the next interval.
1927
   */
1928
  LWIP_ASSERT("lcp_echo_timer_running == 0", lcp_echo_timer_running == 0);
1929
 
1930
  TIMEOUT (lcp_echo_timeout, f, lcp_echo_interval);
1931
  lcp_echo_timer_running = 1;
1932
}
1933
 
1934
/*
1935
 * LcpEchoTimeout - Timer expired on the LCP echo
1936
 */
1937
static void
1938
lcp_echo_timeout (void *arg)
1939
{
1940
  if (lcp_echo_timer_running != 0) {
1941
    lcp_echo_timer_running = 0;
1942
    lcp_echo_check ((fsm *) arg);
1943
  }
1944
}
1945
 
1946
/*
1947
 * LcpEchoReply - LCP has received a reply to the echo
1948
 */
1949
static void
1950
lcp_received_echo_reply (fsm *f, int id, u8_t *inp, int len)
1951
{
1952
  u32_t magic;
1953
 
1954
  LWIP_UNUSED_ARG(id);
1955
 
1956
  /* Check the magic number - don't count replies from ourselves. */
1957
  if (len < 4) {
1958
    LCPDEBUG((LOG_WARNING, "lcp: received short Echo-Reply, length %d\n", len));
1959
    return;
1960
  }
1961
  GETLONG(magic, inp);
1962
  if (lcp_gotoptions[f->unit].neg_magicnumber && magic == lcp_gotoptions[f->unit].magicnumber) {
1963
    LCPDEBUG((LOG_WARNING, "appear to have received our own echo-reply!\n"));
1964
    return;
1965
  }
1966
 
1967
  /* Reset the number of outstanding echo frames */
1968
  lcp_echos_pending = 0;
1969
}
1970
 
1971
/*
1972
 * LcpSendEchoRequest - Send an echo request frame to the peer
1973
 */
1974
static void
1975
lcp_send_echo_request (fsm *f)
1976
{
1977
  u32_t lcp_magic;
1978
  u8_t pkt[4], *pktp;
1979
 
1980
  /*
1981
   * Detect the failure of the peer at this point.
1982
   */
1983
  if (lcp_echo_fails != 0) {
1984
    if (lcp_echos_pending++ >= lcp_echo_fails) {
1985
      lcp_link_failure(f);
1986
      lcp_echos_pending = 0;
1987
    }
1988
  }
1989
 
1990
  /*
1991
   * Make and send the echo request frame.
1992
   */
1993
  if (f->state == LS_OPENED) {
1994
    lcp_magic = lcp_gotoptions[f->unit].magicnumber;
1995
    pktp = pkt;
1996
    PUTLONG(lcp_magic, pktp);
1997
    fsm_sdata(f, ECHOREQ, (u8_t)(lcp_echo_number++ & 0xFF), pkt, (int)(pktp - pkt));
1998
  }
1999
}
2000
 
2001
/*
2002
 * lcp_echo_lowerup - Start the timer for the LCP frame
2003
 */
2004
 
2005
static void
2006
lcp_echo_lowerup (int unit)
2007
{
2008
  fsm *f = &lcp_fsm[unit];
2009
 
2010
  /* Clear the parameters for generating echo frames */
2011
  lcp_echos_pending      = 0;
2012
  lcp_echo_number        = 0;
2013
  lcp_echo_timer_running = 0;
2014
 
2015
  /* If a timeout interval is specified then start the timer */
2016
  if (lcp_echo_interval != 0) {
2017
    lcp_echo_check (f);
2018
  }
2019
}
2020
 
2021
/*
2022
 * lcp_echo_lowerdown - Stop the timer for the LCP frame
2023
 */
2024
 
2025
static void
2026
lcp_echo_lowerdown (int unit)
2027
{
2028
  fsm *f = &lcp_fsm[unit];
2029
 
2030
  if (lcp_echo_timer_running != 0) {
2031
    UNTIMEOUT (lcp_echo_timeout, f);
2032
    lcp_echo_timer_running = 0;
2033
  }
2034
}
2035
 
2036
#endif /* PPP_SUPPORT */

powered by: WebSVN 2.1.0

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