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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ppp/] [current/] [src/] [lcp.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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