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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/ipcp.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
 * ipcp.c - PPP IP 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/ipcp.c,v 1.12 1999/08/28 01:19:03 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 <netdb.h>
77
#include <sys/param.h>
78
#include <sys/types.h>
79
#include <sys/socket.h>
80
#include <netinet/in.h>
81
 
82
#include <pkgconf/system.h>
83
#include <pkgconf/ppp.h>
84
#include "cyg/ppp/pppd.h"
85
#include "cyg/ppp/fsm.h"
86
#include "cyg/ppp/ipcp.h"
87
#include "cyg/ppp/ppp_io.h"
88
 
89
#ifdef CYGPKG_NS_DNS
90
#include <cyg/ns/dns/dns.h>
91
#endif
92
 
93
#define option_error(msg) db_printf("Option error: %s\n", msg )
94
 
95
/* global vars */
96
ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
97
ipcp_options ipcp_gotoptions[NUM_PPP];  /* Options that peer ack'd */
98
ipcp_options ipcp_allowoptions[NUM_PPP];        /* Options we allow peer to request */
99
ipcp_options ipcp_hisoptions[NUM_PPP];  /* Options that we ack'd */
100
 
101
/* local vars */
102
static int cis_received[NUM_PPP];       /* # Conf-Reqs received */
103
static int default_route_set[NUM_PPP];  /* Have set up a default route */
104
static int proxy_arp_set[NUM_PPP];      /* Have created proxy arp entry */
105
 
106
/*
107
 * Callbacks for fsm code.  (CI = Configuration Information)
108
 */
109
static void ipcp_resetci __P((fsm *));  /* Reset our CI */
110
static int  ipcp_cilen __P((fsm *));            /* Return length of our CI */
111
static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
112
static int  ipcp_ackci __P((fsm *, u_char *, int));     /* Peer ack'd our CI */
113
static int  ipcp_nakci __P((fsm *, u_char *, int));     /* Peer nak'd our CI */
114
static int  ipcp_rejci __P((fsm *, u_char *, int));     /* Peer rej'd our CI */
115
static int  ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
116
static void ipcp_up __P((fsm *));               /* We're UP */
117
static void ipcp_down __P((fsm *));             /* We're DOWN */
118
static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
119
 
120
fsm ipcp_fsm[NUM_PPP];          /* IPCP fsm structure */
121
 
122
static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
123
    ipcp_resetci,               /* Reset our Configuration Information */
124
    ipcp_cilen,                 /* Length of our Configuration Information */
125
    ipcp_addci,                 /* Add our Configuration Information */
126
    ipcp_ackci,                 /* ACK our Configuration Information */
127
    ipcp_nakci,                 /* NAK our Configuration Information */
128
    ipcp_rejci,                 /* Reject our Configuration Information */
129
    ipcp_reqci,                 /* Request peer's Configuration Information */
130
    ipcp_up,                    /* Called when fsm reaches OPENED state */
131
    ipcp_down,                  /* Called when fsm leaves OPENED state */
132
    NULL,                       /* Called when we want the lower layer up */
133
    ipcp_finished,              /* Called when we want the lower layer down */
134
    NULL,                       /* Called when Protocol-Reject received */
135
    NULL,                       /* Retransmission is necessary */
136
    NULL,                       /* Called to handle protocol-specific codes */
137
    "IPCP"                      /* String name of protocol */
138
};
139
 
140
/*
141
 * Protocol entry points from main code.
142
 */
143
static void ipcp_init __P((int));
144
static void ipcp_open __P((int));
145
static void ipcp_close __P((int, char *));
146
static void ipcp_lowerup __P((int));
147
static void ipcp_lowerdown __P((int));
148
static void ipcp_input __P((int, u_char *, int));
149
static void ipcp_protrej __P((int));
150
static int  ipcp_printpkt __P((u_char *, int,
151
                               void (*) __P((void *, char *, ...)), void *));
152
static void ip_check_options __P((void));
153
static int  ip_demand_conf __P((int));
154
static int  ip_active_pkt __P((u_char *, int));
155
 
156
struct protent ipcp_protent = {
157
    PPP_IPCP,
158
    ipcp_init,
159
    ipcp_input,
160
    ipcp_protrej,
161
    ipcp_lowerup,
162
    ipcp_lowerdown,
163
    ipcp_open,
164
    ipcp_close,
165
    ipcp_printpkt,
166
    NULL,
167
    1,
168
    "IPCP",
169
    ip_check_options,
170
    ip_demand_conf,
171
    ip_active_pkt
172
};
173
 
174
static void ipcp_clear_addrs __P((int));
175
 
176
/*
177
 * Lengths of configuration options.
178
 */
179
#define CILEN_VOID      2
180
#define CILEN_COMPRESS  4       /* min length for compression protocol opt. */
181
#define CILEN_VJ        6       /* length for RFC1332 Van-Jacobson opt. */
182
#define CILEN_ADDR      6       /* new-style single address option */
183
#define CILEN_ADDRS     10      /* old-style dual address option */
184
 
185
 
186
#define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
187
                         (x) == CONFNAK ? "NAK" : "REJ")
188
 
189
 
190
/*
191
 * Make a string representation of a network IP address.
192
 */
193
char *
194
ip_ntoa(ipaddr)
195
u_int32_t ipaddr;
196
{
197
    static char b[64];
198
 
199
    ipaddr = ntohl(ipaddr);
200
 
201
    sprintf(b, "%d.%d.%d.%d",
202
            (u_char)(ipaddr >> 24),
203
            (u_char)(ipaddr >> 16),
204
            (u_char)(ipaddr >> 8),
205
            (u_char)(ipaddr));
206
    return b;
207
}
208
 
209
 
210
/*
211
 * ipcp_init - Initialize IPCP.
212
 */
213
static void
214
ipcp_init(unit)
215
    int unit;
216
{
217
    fsm *f = &ipcp_fsm[unit];
218
    ipcp_options *wo = &ipcp_wantoptions[unit];
219
    ipcp_options *ao = &ipcp_allowoptions[unit];
220
 
221
    f->unit = unit;
222
    f->protocol = PPP_IPCP;
223
    f->callbacks = &ipcp_callbacks;
224
    fsm_init(&ipcp_fsm[unit]);
225
 
226
    memset(wo, 0, sizeof(*wo));
227
    memset(ao, 0, sizeof(*ao));
228
 
229
    wo->neg_addr = 1;
230
    wo->neg_vj = 1;
231
    wo->vj_protocol = IPCP_VJ_COMP;
232
    wo->maxslotindex = MAX_STATES - 1; /* really max index */
233
    wo->cflag = 1;
234
    wo->ouraddr     = ppp_tty.options->our_address;
235
    wo->hisaddr     = ppp_tty.options->his_address;
236
    wo->default_route = ppp_tty.options->default_route;
237
 
238
#ifdef CYGOPT_PPP_NS_NEGOTIATE
239
    wo->neg_dns1 = 1;
240
    wo->neg_dns2 = 1;
241
#endif
242
 
243
    /* max slots and slot-id compression are currently hardwired in */
244
    /* ppp_if.c to 16 and 1, this needs to be changed (among other */
245
    /* things) gmc */
246
 
247
    ao->neg_addr = 1;
248
    ao->neg_vj = 1;
249
    ao->maxslotindex = MAX_STATES - 1;
250
    ao->cflag = 1;
251
 
252
    /*
253
     * XXX These control whether the user may use the proxyarp
254
     * and defaultroute options.
255
     */
256
    ao->proxy_arp = 1;
257
    ao->default_route = 1;
258
}
259
 
260
 
261
/*
262
 * ipcp_open - IPCP is allowed to come up.
263
 */
264
static void
265
ipcp_open(unit)
266
    int unit;
267
{
268
    fsm_open(&ipcp_fsm[unit]);
269
}
270
 
271
 
272
/*
273
 * ipcp_close - Take IPCP down.
274
 */
275
static void
276
ipcp_close(unit, reason)
277
    int unit;
278
    char *reason;
279
{
280
    fsm_close(&ipcp_fsm[unit], reason);
281
}
282
 
283
 
284
/*
285
 * ipcp_lowerup - The lower layer is up.
286
 */
287
static void
288
ipcp_lowerup(unit)
289
    int unit;
290
{
291
    fsm_lowerup(&ipcp_fsm[unit]);
292
}
293
 
294
 
295
/*
296
 * ipcp_lowerdown - The lower layer is down.
297
 */
298
static void
299
ipcp_lowerdown(unit)
300
    int unit;
301
{
302
    fsm_lowerdown(&ipcp_fsm[unit]);
303
}
304
 
305
 
306
/*
307
 * ipcp_input - Input IPCP packet.
308
 */
309
static void
310
ipcp_input(unit, p, len)
311
    int unit;
312
    u_char *p;
313
    int len;
314
{
315
    fsm_input(&ipcp_fsm[unit], p, len);
316
}
317
 
318
 
319
/*
320
 * ipcp_protrej - A Protocol-Reject was received for IPCP.
321
 *
322
 * Pretend the lower layer went down, so we shut up.
323
 */
324
static void
325
ipcp_protrej(unit)
326
    int unit;
327
{
328
    fsm_lowerdown(&ipcp_fsm[unit]);
329
}
330
 
331
 
332
/*
333
 * ipcp_resetci - Reset our CI.
334
 */
335
static void
336
ipcp_resetci(f)
337
    fsm *f;
338
{
339
    ipcp_options *wo = &ipcp_wantoptions[f->unit];
340
 
341
    wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
342
    if (wo->ouraddr == 0)
343
        wo->accept_local = 1;
344
    if (wo->hisaddr == 0)
345
        wo->accept_remote = 1;
346
    ipcp_gotoptions[f->unit] = *wo;
347
    cis_received[f->unit] = 0;
348
}
349
 
350
 
351
/*
352
 * ipcp_cilen - Return length of our CI.
353
 */
354
static int
355
ipcp_cilen(f)
356
    fsm *f;
357
{
358
    ipcp_options *go = &ipcp_gotoptions[f->unit];
359
    ipcp_options *wo = &ipcp_wantoptions[f->unit];
360
    ipcp_options *ho = &ipcp_hisoptions[f->unit];
361
    int len;
362
 
363
#define LENCIVJ(neg, old)       (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
364
#define LENCIADDR(neg, old)     (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
365
 
366
    /*
367
     * First see if we want to change our options to the old
368
     * forms because we have received old forms from the peer.
369
     */
370
    if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
371
        /* use the old style of address negotiation */
372
        go->neg_addr = 1;
373
        go->old_addrs = 1;
374
    }
375
    if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
376
        /* try an older style of VJ negotiation */
377
        if (cis_received[f->unit] == 0) {
378
#ifndef PPP_INHIBIT_OLD_VJ_COMPRESSION
379
            /* keep trying the new style until we see some CI from the peer */
380
            go->neg_vj = 1;
381
#endif
382
        } else {
383
            /* use the old style only if the peer did */
384
            if (ho->neg_vj && ho->old_vj) {
385
                go->neg_vj = 1;
386
                go->old_vj = 1;
387
                go->vj_protocol = ho->vj_protocol;
388
            }
389
        }
390
    }
391
 
392
    len = LENCIADDR(go->neg_addr, go->old_addrs) +
393
          LENCIVJ(go->neg_vj, go->old_vj);
394
 
395
#ifdef CYGOPT_PPP_NS_NEGOTIATE
396
    len += LENCIADDR(go->neg_dns1, 0) +
397
           LENCIADDR(go->neg_wins1, 0) +
398
           LENCIADDR(go->neg_dns2, 0) +
399
           LENCIADDR(go->neg_wins2, 0);
400
#endif
401
    return (len);
402
}
403
 
404
 
405
/*
406
 * ipcp_addci - Add our desired CIs to a packet.
407
 */
408
static void
409
ipcp_addci(f, ucp, lenp)
410
    fsm *f;
411
    u_char *ucp;
412
    int *lenp;
413
{
414
    ipcp_options *go = &ipcp_gotoptions[f->unit];
415
    int len = *lenp;
416
 
417
#define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
418
    if (neg) { \
419
        int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
420
        if (len >= vjlen) { \
421
            PUTCHAR(opt, ucp); \
422
            PUTCHAR(vjlen, ucp); \
423
            PUTSHORT(val, ucp); \
424
            if (!old) { \
425
                PUTCHAR(maxslotindex, ucp); \
426
                PUTCHAR(cflag, ucp); \
427
            } \
428
            len -= vjlen; \
429
        } else \
430
            neg = 0; \
431
    }
432
 
433
#define ADDCIADDR(opt, neg, old, val1, val2) \
434
    if (neg) { \
435
        int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
436
        if (len >= addrlen) { \
437
            u_int32_t l; \
438
            PUTCHAR(opt, ucp); \
439
            PUTCHAR(addrlen, ucp); \
440
            l = ntohl(val1); \
441
            PUTLONG(l, ucp); \
442
            if (old) { \
443
                l = ntohl(val2); \
444
                PUTLONG(l, ucp); \
445
            } \
446
            len -= addrlen; \
447
        } else \
448
            neg = 0; \
449
    }
450
 
451
    ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
452
              go->old_addrs, go->ouraddr, go->hisaddr);
453
 
454
    ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
455
            go->maxslotindex, go->cflag);
456
 
457
#ifdef CYGOPT_PPP_NS_NEGOTIATE
458
    ADDCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
459
    ADDCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
460
    ADDCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
461
    ADDCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
462
#endif
463
 
464
    *lenp -= len;
465
}
466
 
467
 
468
/*
469
 * ipcp_ackci - Ack our CIs.
470
 *
471
 * Returns:
472
 *      0 - Ack was bad.
473
 *      1 - Ack was good.
474
 */
475
static int
476
ipcp_ackci(f, p, len)
477
    fsm *f;
478
    u_char *p;
479
    int len;
480
{
481
    ipcp_options *go = &ipcp_gotoptions[f->unit];
482
    u_short cilen, citype, cishort;
483
    u_int32_t cilong;
484
    u_char cimaxslotindex, cicflag;
485
 
486
    /*
487
     * CIs must be in exactly the same order that we sent...
488
     * Check packet length and CI length at each step.
489
     * If we find any deviations, then this packet is bad.
490
     */
491
 
492
#define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
493
    if (neg) { \
494
        int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
495
        if ((len -= vjlen) < 0) \
496
            goto bad; \
497
        GETCHAR(citype, p); \
498
        GETCHAR(cilen, p); \
499
        if (cilen != vjlen || \
500
            citype != opt)  \
501
            goto bad; \
502
        GETSHORT(cishort, p); \
503
        if (cishort != val) \
504
            goto bad; \
505
        if (!old) { \
506
            GETCHAR(cimaxslotindex, p); \
507
            if (cimaxslotindex != maxslotindex) \
508
                goto bad; \
509
            GETCHAR(cicflag, p); \
510
            if (cicflag != cflag) \
511
                goto bad; \
512
        } \
513
    }
514
 
515
#define ACKCIADDR(opt, neg, old, val1, val2) \
516
    if (neg) { \
517
        int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
518
        u_int32_t l; \
519
        if ((len -= addrlen) < 0) \
520
            goto bad; \
521
        GETCHAR(citype, p); \
522
        GETCHAR(cilen, p); \
523
        if (cilen != addrlen || \
524
            citype != opt) \
525
            goto bad; \
526
        GETLONG(l, p); \
527
        cilong = htonl(l); \
528
        if (val1 != cilong) \
529
            goto bad; \
530
        if (old) { \
531
            GETLONG(l, p); \
532
            cilong = htonl(l); \
533
            if (val2 != cilong) \
534
                goto bad; \
535
        } \
536
    }
537
 
538
    ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
539
              go->old_addrs, go->ouraddr, go->hisaddr);
540
 
541
    ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
542
            go->maxslotindex, go->cflag);
543
 
544
#ifdef CYGOPT_PPP_NS_NEGOTIATE
545
    ACKCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
546
    ACKCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
547
    ACKCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
548
    ACKCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
549
#endif
550
 
551
    /*
552
     * If there are any remaining CIs, then this packet is bad.
553
     */
554
    if (len != 0)
555
        goto bad;
556
    return (1);
557
 
558
bad:
559
    IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
560
    return (0);
561
}
562
 
563
/*
564
 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
565
 * This should not modify any state if the Nak is bad
566
 * or if IPCP is in the OPENED state.
567
 *
568
 * Returns:
569
 *      0 - Nak was bad.
570
 *      1 - Nak was good.
571
 */
572
static int
573
ipcp_nakci(f, p, len)
574
    fsm *f;
575
    u_char *p;
576
    int len;
577
{
578
    ipcp_options *go = &ipcp_gotoptions[f->unit];
579
    u_char cimaxslotindex, cicflag;
580
    u_char citype, cilen, *next;
581
    u_short cishort;
582
    u_int32_t ciaddr1, ciaddr2, l;
583
    ipcp_options no;            /* options we've seen Naks for */
584
    ipcp_options try;           /* options to request next time */
585
 
586
    BZERO(&no, sizeof(no));
587
    try = *go;
588
 
589
    /*
590
     * Any Nak'd CIs must be in exactly the same order that we sent.
591
     * Check packet length and CI length at each step.
592
     * If we find any deviations, then this packet is bad.
593
     */
594
#define NAKCIADDR(opt, neg, old, code) \
595
    if (go->neg && \
596
        len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
597
        p[1] == cilen && \
598
        p[0] == opt) { \
599
        len -= cilen; \
600
        INCPTR(2, p); \
601
        GETLONG(l, p); \
602
        ciaddr1 = htonl(l); \
603
        if (old) { \
604
            GETLONG(l, p); \
605
            ciaddr2 = htonl(l); \
606
            no.old_addrs = 1; \
607
        } else \
608
            ciaddr2 = 0; \
609
        no.neg = 1; \
610
        code \
611
    }
612
 
613
#define NAKCIVJ(opt, neg, code) \
614
    if (go->neg && \
615
        ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
616
        len >= cilen && \
617
        p[0] == opt) { \
618
        len -= cilen; \
619
        INCPTR(2, p); \
620
        GETSHORT(cishort, p); \
621
        no.neg = 1; \
622
        code \
623
    }
624
 
625
    /*
626
     * Accept the peer's idea of {our,his} address, if different
627
     * from our idea, only if the accept_{local,remote} flag is set.
628
     */
629
    NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
630
              if (go->accept_local && ciaddr1) { /* Do we know our address? */
631
                  try.ouraddr = ciaddr1;
632
                  IPCPDEBUG((LOG_INFO, "local IP address %s",
633
                             ip_ntoa(ciaddr1)));
634
              }
635
              if (go->accept_remote && ciaddr2) { /* Does he know his? */
636
                  try.hisaddr = ciaddr2;
637
                  IPCPDEBUG((LOG_INFO, "remote IP address %s",
638
                             ip_ntoa(ciaddr2)));
639
              }
640
              );
641
 
642
    /*
643
     * Accept the peer's value of maxslotindex provided that it
644
     * is less than what we asked for.  Turn off slot-ID compression
645
     * if the peer wants.  Send old-style compress-type option if
646
     * the peer wants.
647
     */
648
    NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
649
            if (cilen == CILEN_VJ) {
650
                GETCHAR(cimaxslotindex, p);
651
                GETCHAR(cicflag, p);
652
                if (cishort == IPCP_VJ_COMP) {
653
                    try.old_vj = 0;
654
                    if (cimaxslotindex < go->maxslotindex)
655
                        try.maxslotindex = cimaxslotindex;
656
                    if (!cicflag)
657
                        try.cflag = 0;
658
                } else {
659
                    try.neg_vj = 0;
660
                }
661
            } else {
662
                if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
663
                    try.old_vj = 1;
664
                    try.vj_protocol = cishort;
665
                } else {
666
                    try.neg_vj = 0;
667
                }
668
            }
669
            );
670
 
671
#ifdef CYGOPT_PPP_NS_NEGOTIATE
672
    /*
673
     * Accept the peer's idea of the DNS and WINS addresses
674
     */
675
    NAKCIADDR(CI_MS_DNS1, neg_dns1, 0, try.dnsaddr[0] = ciaddr1;);
676
    NAKCIADDR(CI_MS_WINS1, neg_wins1, 0, try.winsaddr[0] = ciaddr1;);
677
    NAKCIADDR(CI_MS_DNS2, neg_dns2, 0, try.dnsaddr[1] = ciaddr1;);
678
    NAKCIADDR(CI_MS_WINS2, neg_wins2, 0, try.winsaddr[1] = ciaddr1;);
679
#ifdef CYGOPT_PPP_DNS_CONFIGURE
680
        if (try.dnsaddr[0] != 0 )
681
        {
682
        cyg_dns_res_init((struct in_addr *)&try.dnsaddr[0]);
683
    }
684
#endif
685
#endif
686
 
687
    /*
688
     * There may be remaining CIs, if the peer is requesting negotiation
689
     * on an option that we didn't include in our request packet.
690
     * If they want to negotiate about IP addresses, we comply.
691
     * If they want us to ask for compression, we refuse.
692
     */
693
    while (len > CILEN_VOID) {
694
        GETCHAR(citype, p);
695
        GETCHAR(cilen, p);
696
        if( (len -= cilen) < 0 )
697
            goto bad;
698
        next = p + cilen - 2;
699
 
700
        switch (citype) {
701
        case CI_COMPRESSTYPE:
702
            if (go->neg_vj || no.neg_vj ||
703
                (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
704
                goto bad;
705
            no.neg_vj = 1;
706
            break;
707
        case CI_ADDRS:
708
            if ((go->neg_addr && go->old_addrs) || no.old_addrs
709
                || cilen != CILEN_ADDRS)
710
                goto bad;
711
            try.neg_addr = 1;
712
            try.old_addrs = 1;
713
            GETLONG(l, p);
714
            ciaddr1 = htonl(l);
715
            if (ciaddr1 && go->accept_local)
716
                try.ouraddr = ciaddr1;
717
            GETLONG(l, p);
718
            ciaddr2 = htonl(l);
719
            if (ciaddr2 && go->accept_remote)
720
                try.hisaddr = ciaddr2;
721
            no.old_addrs = 1;
722
            break;
723
        case CI_ADDR:
724
            if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
725
                goto bad;
726
            try.old_addrs = 0;
727
            GETLONG(l, p);
728
            ciaddr1 = htonl(l);
729
            if (ciaddr1 && go->accept_local)
730
                try.ouraddr = ciaddr1;
731
            if (try.ouraddr != 0)
732
                try.neg_addr = 1;
733
            no.neg_addr = 1;
734
            break;
735
        }
736
        p = next;
737
    }
738
 
739
    /* If there is still anything left, this packet is bad. */
740
    if (len != 0)
741
        goto bad;
742
 
743
    /*
744
     * OK, the Nak is good.  Now we can update state.
745
     */
746
    if (f->state != OPENED)
747
        *go = try;
748
 
749
    return 1;
750
 
751
bad:
752
    IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
753
    return 0;
754
}
755
 
756
 
757
/*
758
 * ipcp_rejci - Reject some of our CIs.
759
 */
760
static int
761
ipcp_rejci(f, p, len)
762
    fsm *f;
763
    u_char *p;
764
    int len;
765
{
766
    ipcp_options *go = &ipcp_gotoptions[f->unit];
767
    u_char cimaxslotindex, ciflag, cilen;
768
    u_short cishort;
769
    u_int32_t cilong;
770
    ipcp_options try;           /* options to request next time */
771
#ifdef CYGOPT_PPP_NS_NEGOTIATE
772
    u_char citype, *next;
773
#endif
774
 
775
    try = *go;
776
    /*
777
     * Any Rejected CIs must be in exactly the same order that we sent.
778
     * Check packet length and CI length at each step.
779
     * If we find any deviations, then this packet is bad.
780
     */
781
#define REJCIADDR(opt, neg, old, val1, val2) \
782
    if (go->neg && \
783
        len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
784
        p[1] == cilen && \
785
        p[0] == opt) { \
786
        u_int32_t l; \
787
        len -= cilen; \
788
        INCPTR(2, p); \
789
        GETLONG(l, p); \
790
        cilong = htonl(l); \
791
        /* Check rejected value. */ \
792
        if (cilong != val1) \
793
            goto bad; \
794
        if (old) { \
795
            GETLONG(l, p); \
796
            cilong = htonl(l); \
797
            /* Check rejected value. */ \
798
            if (cilong != val2) \
799
                goto bad; \
800
        } \
801
        try.neg = 0; \
802
    }
803
 
804
#define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
805
    if (go->neg && \
806
        p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
807
        len >= p[1] && \
808
        p[0] == opt) { \
809
        len -= p[1]; \
810
        INCPTR(2, p); \
811
        GETSHORT(cishort, p); \
812
        /* Check rejected value. */  \
813
        if (cishort != val) \
814
            goto bad; \
815
        if (!old) { \
816
           GETCHAR(cimaxslotindex, p); \
817
           if (cimaxslotindex != maxslot) \
818
             goto bad; \
819
           GETCHAR(ciflag, p); \
820
           if (ciflag != cflag) \
821
             goto bad; \
822
        } \
823
        try.neg = 0; \
824
     }
825
 
826
    REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
827
              go->old_addrs, go->ouraddr, go->hisaddr);
828
 
829
    REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
830
            go->maxslotindex, go->cflag);
831
 
832
#ifdef CYGOPT_PPP_NS_NEGOTIATE
833
/*
834
     * There may be remaining CIs, if the peer is unable to support
835
     * DNS or WINS negotiation.  If so, just turn them off
836
     *
837
     */
838
 
839
    while (len > CILEN_VOID) {
840
        GETCHAR(citype, p);
841
        GETCHAR(cilen, p);
842
        if( (len -= cilen) < 0 )
843
            goto bad;
844
        next = p + cilen - 2;
845
 
846
        switch (citype) {
847
        case CI_MS_DNS1:
848
                try.neg_dns1 = 0;
849
                break;
850
        case CI_MS_WINS1:
851
                try.neg_wins1 = 0;
852
                break;
853
        case CI_MS_DNS2:
854
                try.neg_dns2 = 0;
855
                break;
856
        case CI_MS_WINS2:
857
                try.neg_wins2 = 0;
858
                break;
859
        }
860
        p = next;
861
    }
862
#endif
863
 
864
    /*
865
     * If there are any remaining CIs, then this packet is bad.
866
     */
867
    if (len != 0)
868
        goto bad;
869
    /*
870
     * Now we can update state.
871
     */
872
    if (f->state != OPENED)
873
        *go = try;
874
    return 1;
875
 
876
bad:
877
    IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
878
    return 0;
879
}
880
 
881
 
882
/*
883
 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
884
 *
885
 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
886
 * appropriately.  If reject_if_disagree is non-zero, doesn't return
887
 * CONFNAK; returns CONFREJ if it can't return CONFACK.
888
 */
889
static int
890
ipcp_reqci(f, inp, len, reject_if_disagree)
891
    fsm *f;
892
    u_char *inp;                /* Requested CIs */
893
    int *len;                   /* Length of requested CIs */
894
    int reject_if_disagree;
895
{
896
    ipcp_options *wo = &ipcp_wantoptions[f->unit];
897
    ipcp_options *ho = &ipcp_hisoptions[f->unit];
898
    ipcp_options *ao = &ipcp_allowoptions[f->unit];
899
    ipcp_options *go = &ipcp_gotoptions[f->unit];
900
    u_char *cip, *next;         /* Pointer to current and next CIs */
901
    u_short cilen, citype;      /* Parsed len, type */
902
    u_short cishort;            /* Parsed short value */
903
    u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
904
    int rc = CONFACK;           /* Final packet return code */
905
    int orc;                    /* Individual option return code */
906
    u_char *p;                  /* Pointer to next char to parse */
907
    u_char *ucp = inp;          /* Pointer to current output char */
908
    int l = *len;               /* Length left */
909
    u_char maxslotindex, cflag;
910
    int d;
911
 
912
    cis_received[f->unit] = 1;
913
 
914
    /*
915
     * Reset all his options.
916
     */
917
    BZERO(ho, sizeof(*ho));
918
 
919
    /*
920
     * Process all his options.
921
     */
922
    next = inp;
923
    while (l) {
924
        orc = CONFACK;                  /* Assume success */
925
        cip = p = next;                 /* Remember begining of CI */
926
        if (l < 2 ||                    /* Not enough data for CI header or */
927
            p[1] < 2 ||                 /*  CI length too small or */
928
            p[1] > l) {                 /*  CI length too big? */
929
            IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
930
            orc = CONFREJ;              /* Reject bad CI */
931
            cilen = l;                  /* Reject till end of packet */
932
            l = 0;                       /* Don't loop again */
933
            goto endswitch;
934
        }
935
        GETCHAR(citype, p);             /* Parse CI type */
936
        GETCHAR(cilen, p);              /* Parse CI length */
937
        l -= cilen;                     /* Adjust remaining length */
938
        next += cilen;                  /* Step to next CI */
939
 
940
        switch (citype) {               /* Check CI type */
941
        case CI_ADDRS:
942
            IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
943
            if (!ao->neg_addr ||
944
                cilen != CILEN_ADDRS) { /* Check CI length */
945
                orc = CONFREJ;          /* Reject CI */
946
                break;
947
            }
948
 
949
            /*
950
             * If he has no address, or if we both have his address but
951
             * disagree about it, then NAK it with our idea.
952
             * In particular, if we don't know his address, but he does,
953
             * then accept it.
954
             */
955
            GETLONG(tl, p);             /* Parse source address (his) */
956
            ciaddr1 = htonl(tl);
957
            IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
958
            if (ciaddr1 != wo->hisaddr
959
                && (ciaddr1 == 0 || !wo->accept_remote)) {
960
                orc = CONFNAK;
961
                if (!reject_if_disagree) {
962
                    DECPTR(sizeof(u_int32_t), p);
963
                    tl = ntohl(wo->hisaddr);
964
                    PUTLONG(tl, p);
965
                }
966
            } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
967
                /*
968
                 * If neither we nor he knows his address, reject the option.
969
                 */
970
                orc = CONFREJ;
971
                wo->req_addr = 0;        /* don't NAK with 0.0.0.0 later */
972
                break;
973
            }
974
 
975
            /*
976
             * If he doesn't know our address, or if we both have our address
977
             * but disagree about it, then NAK it with our idea.
978
             */
979
            GETLONG(tl, p);             /* Parse desination address (ours) */
980
            ciaddr2 = htonl(tl);
981
            IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
982
            if (ciaddr2 != wo->ouraddr) {
983
                if (ciaddr2 == 0 || !wo->accept_local) {
984
                    orc = CONFNAK;
985
                    if (!reject_if_disagree) {
986
                        DECPTR(sizeof(u_int32_t), p);
987
                        tl = ntohl(wo->ouraddr);
988
                        PUTLONG(tl, p);
989
                    }
990
                } else {
991
                    go->ouraddr = ciaddr2;      /* accept peer's idea */
992
                }
993
            }
994
 
995
            ho->neg_addr = 1;
996
            ho->old_addrs = 1;
997
            ho->hisaddr = ciaddr1;
998
            ho->ouraddr = ciaddr2;
999
            break;
1000
 
1001
        case CI_ADDR:
1002
            IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1003
 
1004
            if (!ao->neg_addr ||
1005
                cilen != CILEN_ADDR) {  /* Check CI length */
1006
                orc = CONFREJ;          /* Reject CI */
1007
                break;
1008
            }
1009
 
1010
            /*
1011
             * If he has no address, or if we both have his address but
1012
             * disagree about it, then NAK it with our idea.
1013
             * In particular, if we don't know his address, but he does,
1014
             * then accept it.
1015
             */
1016
            GETLONG(tl, p);     /* Parse source address (his) */
1017
            ciaddr1 = htonl(tl);
1018
            IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1019
            if (ciaddr1 != wo->hisaddr
1020
                && (ciaddr1 == 0 || !wo->accept_remote)) {
1021
                orc = CONFNAK;
1022
                if (!reject_if_disagree) {
1023
                    DECPTR(sizeof(u_int32_t), p);
1024
                    tl = ntohl(wo->hisaddr);
1025
                    PUTLONG(tl, p);
1026
                }
1027
            } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1028
                /*
1029
                 * Don't ACK an address of 0.0.0.0 - reject it instead.
1030
                 */
1031
                orc = CONFREJ;
1032
                wo->req_addr = 0;        /* don't NAK with 0.0.0.0 later */
1033
                break;
1034
            }
1035
 
1036
            ho->neg_addr = 1;
1037
            ho->hisaddr = ciaddr1;
1038
            break;
1039
 
1040
        case CI_MS_DNS1:
1041
        case CI_MS_DNS2:
1042
            /* Microsoft primary or secondary DNS request */
1043
            d = citype == CI_MS_DNS2;
1044
            IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1045
 
1046
            /* If we do not have a DNS address then we cannot send it */
1047
            if (ao->dnsaddr[d] == 0 ||
1048
                cilen != CILEN_ADDR) {  /* Check CI length */
1049
                orc = CONFREJ;          /* Reject CI */
1050
                break;
1051
            }
1052
            GETLONG(tl, p);
1053
            if (htonl(tl) != ao->dnsaddr[d]) {
1054
                DECPTR(sizeof(u_int32_t), p);
1055
                tl = ntohl(ao->dnsaddr[d]);
1056
                PUTLONG(tl, p);
1057
                orc = CONFNAK;
1058
            }
1059
            break;
1060
 
1061
        case CI_MS_WINS1:
1062
        case CI_MS_WINS2:
1063
            /* Microsoft primary or secondary WINS request */
1064
            d = citype == CI_MS_WINS2;
1065
            IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1066
 
1067
            /* If we do not have a DNS address then we cannot send it */
1068
            if (ao->winsaddr[d] == 0 ||
1069
                cilen != CILEN_ADDR) {  /* Check CI length */
1070
                orc = CONFREJ;          /* Reject CI */
1071
                break;
1072
            }
1073
            GETLONG(tl, p);
1074
            if (htonl(tl) != ao->winsaddr[d]) {
1075
                DECPTR(sizeof(u_int32_t), p);
1076
                tl = ntohl(ao->winsaddr[d]);
1077
                PUTLONG(tl, p);
1078
                orc = CONFNAK;
1079
            }
1080
            break;
1081
 
1082
        case CI_COMPRESSTYPE:
1083
            IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1084
            if (!ao->neg_vj ||
1085
                (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1086
                orc = CONFREJ;
1087
                break;
1088
            }
1089
            GETSHORT(cishort, p);
1090
            IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1091
 
1092
            if (!(cishort == IPCP_VJ_COMP ||
1093
                  (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1094
                orc = CONFREJ;
1095
                break;
1096
            }
1097
 
1098
            ho->neg_vj = 1;
1099
            ho->vj_protocol = cishort;
1100
            if (cilen == CILEN_VJ) {
1101
                GETCHAR(maxslotindex, p);
1102
                if (maxslotindex > ao->maxslotindex) {
1103
                    orc = CONFNAK;
1104
                    if (!reject_if_disagree){
1105
                        DECPTR(1, p);
1106
                        PUTCHAR(ao->maxslotindex, p);
1107
                    }
1108
                }
1109
                GETCHAR(cflag, p);
1110
                if (cflag && !ao->cflag) {
1111
                    orc = CONFNAK;
1112
                    if (!reject_if_disagree){
1113
                        DECPTR(1, p);
1114
                        PUTCHAR(wo->cflag, p);
1115
                    }
1116
                }
1117
                ho->maxslotindex = maxslotindex;
1118
                ho->cflag = cflag;
1119
            } else {
1120
                ho->old_vj = 1;
1121
                ho->maxslotindex = MAX_STATES - 1;
1122
                ho->cflag = 1;
1123
            }
1124
            break;
1125
 
1126
        default:
1127
            orc = CONFREJ;
1128
            break;
1129
        }
1130
 
1131
endswitch:
1132
        IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1133
 
1134
        if (orc == CONFACK &&           /* Good CI */
1135
            rc != CONFACK)              /*  but prior CI wasnt? */
1136
            continue;                   /* Don't send this one */
1137
 
1138
        if (orc == CONFNAK) {           /* Nak this CI? */
1139
            if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1140
                orc = CONFREJ;          /* Get tough if so */
1141
            else {
1142
                if (rc == CONFREJ)      /* Rejecting prior CI? */
1143
                    continue;           /* Don't send this one */
1144
                if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1145
                    rc = CONFNAK;       /* Not anymore... */
1146
                    ucp = inp;          /* Backup */
1147
                }
1148
            }
1149
        }
1150
 
1151
        if (orc == CONFREJ &&           /* Reject this CI */
1152
            rc != CONFREJ) {            /*  but no prior ones? */
1153
            rc = CONFREJ;
1154
            ucp = inp;                  /* Backup */
1155
        }
1156
 
1157
        /* Need to move CI? */
1158
        if (ucp != cip)
1159
            BCOPY(cip, ucp, cilen);     /* Move it */
1160
 
1161
        /* Update output pointer */
1162
        INCPTR(cilen, ucp);
1163
    }
1164
 
1165
    /*
1166
     * If we aren't rejecting this packet, and we want to negotiate
1167
     * their address, and they didn't send their address, then we
1168
     * send a NAK with a CI_ADDR option appended.  We assume the
1169
     * input buffer is long enough that we can append the extra
1170
     * option safely.
1171
     */
1172
    if (rc != CONFREJ && !ho->neg_addr &&
1173
        wo->req_addr && !reject_if_disagree) {
1174
        if (rc == CONFACK) {
1175
            rc = CONFNAK;
1176
            ucp = inp;                  /* reset pointer */
1177
            wo->req_addr = 0;            /* don't ask again */
1178
        }
1179
        PUTCHAR(CI_ADDR, ucp);
1180
        PUTCHAR(CILEN_ADDR, ucp);
1181
        tl = ntohl(wo->hisaddr);
1182
        PUTLONG(tl, ucp);
1183
    }
1184
 
1185
    *len = ucp - inp;                   /* Compute output length */
1186
    IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1187
    return (rc);                        /* Return final code */
1188
}
1189
 
1190
 
1191
/*
1192
 * ip_check_options - check that any IP-related options are OK,
1193
 * and assign appropriate defaults.
1194
 */
1195
static void
1196
ip_check_options()
1197
{
1198
    struct hostent *hp;
1199
    u_int32_t local;
1200
    ipcp_options *wo = &ipcp_wantoptions[0];
1201
 
1202
    /*
1203
     * Default our local IP address based on our hostname.
1204
     * If local IP address already given, don't bother.
1205
     */
1206
    if (wo->ouraddr == 0 && !disable_defaultip) {
1207
        /*
1208
         * Look up our hostname (possibly with domain name appended)
1209
         * and take the first IP address as our local IP address.
1210
         * If there isn't an IP address for our hostname, too bad.
1211
         */
1212
        wo->accept_local = 1;   /* don't insist on this default value */
1213
        if ((hp = gethostbyname(cyg_ppp_hostname)) != NULL) {
1214
            local = *(u_int32_t *)hp->h_addr;
1215
            if (local != 0 && !bad_ip_adrs(local))
1216
                wo->ouraddr = local;
1217
        }
1218
    }
1219
 
1220
    if (demand && wo->hisaddr == 0) {
1221
        option_error("remote IP address required for demand-dialling\n");
1222
    }
1223
}
1224
 
1225
 
1226
/*
1227
 * ip_demand_conf - configure the interface as though
1228
 * IPCP were up, for use with dial-on-demand.
1229
 */
1230
static int
1231
ip_demand_conf(u)
1232
    int u;
1233
{
1234
    ipcp_options *wo = &ipcp_wantoptions[u];
1235
 
1236
    if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1237
        return 0;
1238
    if (!sifup(u))
1239
        return 0;
1240
    if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1241
        return 0;
1242
    if (wo->default_route)
1243
        if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1244
            default_route_set[u] = 1;
1245
    if (wo->proxy_arp)
1246
        if (sifproxyarp(u, wo->hisaddr))
1247
            proxy_arp_set[u] = 1;
1248
 
1249
    syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(wo->ouraddr));
1250
    syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1251
 
1252
    return 1;
1253
}
1254
 
1255
 
1256
/*
1257
 * ipcp_up - IPCP has come UP.
1258
 *
1259
 * Configure the IP network interface appropriately and bring it up.
1260
 */
1261
static void
1262
ipcp_up(f)
1263
    fsm *f;
1264
{
1265
    u_int32_t mask;
1266
    ipcp_options *ho = &ipcp_hisoptions[f->unit];
1267
    ipcp_options *go = &ipcp_gotoptions[f->unit];
1268
    ipcp_options *wo = &ipcp_wantoptions[f->unit];
1269
 
1270
    np_up(f->unit, PPP_IP);
1271
    IPCPDEBUG((LOG_INFO, "ipcp: up"));
1272
 
1273
    /*
1274
     * We must have a non-zero IP address for both ends of the link.
1275
     */
1276
    if (!ho->neg_addr)
1277
        ho->hisaddr = wo->hisaddr;
1278
 
1279
    if (ho->hisaddr == 0) {
1280
        syslog(LOG_ERR, "Could not determine remote IP address");
1281
        ipcp_close(f->unit, "Could not determine remote IP address");
1282
        return;
1283
    }
1284
    if (go->ouraddr == 0) {
1285
        syslog(LOG_ERR, "Could not determine local IP address");
1286
        ipcp_close(f->unit, "Could not determine local IP address");
1287
        return;
1288
    }
1289
 
1290
    /*
1291
     * Check that the peer is allowed to use the IP address it wants.
1292
     */
1293
    if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1294
        syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1295
               ip_ntoa(ho->hisaddr));
1296
        ipcp_close(f->unit, "Unauthorized remote IP address");
1297
        return;
1298
    }
1299
 
1300
    /* set tcp compression */
1301
    sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1302
 
1303
    /*
1304
     * If we are doing dial-on-demand, the interface is already
1305
     * configured, so we put out any saved-up packets, then set the
1306
     * interface to pass IP packets.
1307
     */
1308
    if (demand) {
1309
        if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1310
            if (go->ouraddr != wo->ouraddr)
1311
                syslog(LOG_WARNING, "Local IP address changed to %s",
1312
                       ip_ntoa(go->ouraddr));
1313
            if (ho->hisaddr != wo->hisaddr)
1314
                syslog(LOG_WARNING, "Remote IP address changed to %s",
1315
                       ip_ntoa(ho->hisaddr));
1316
            ipcp_clear_addrs(f->unit);
1317
 
1318
            /* Set the interface to the new addresses */
1319
            mask = GetMask(go->ouraddr);
1320
            if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1321
                IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1322
                ipcp_close(f->unit, "Interface configuration failed");
1323
                return;
1324
            }
1325
 
1326
            /* assign a default route through the interface if required */
1327
            if (ipcp_wantoptions[f->unit].default_route)
1328
                if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1329
                    default_route_set[f->unit] = 1;
1330
 
1331
            /* Make a proxy ARP entry if requested. */
1332
            if (ipcp_wantoptions[f->unit].proxy_arp)
1333
                if (sifproxyarp(f->unit, ho->hisaddr))
1334
                    proxy_arp_set[f->unit] = 1;
1335
 
1336
        }
1337
        demand_rexmit(PPP_IP);
1338
        sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1339
 
1340
    } else {
1341
        /*
1342
         * Set IP addresses and (if specified) netmask.
1343
         */
1344
        mask = GetMask(go->ouraddr);
1345
 
1346
#if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1347
        if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1348
            IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1349
            ipcp_close(f->unit, "Interface configuration failed");
1350
            return;
1351
        }
1352
#endif
1353
 
1354
        /* bring the interface up for IP */
1355
        if (!sifup(f->unit)) {
1356
            IPCPDEBUG((LOG_WARNING, "sifup failed"));
1357
            ipcp_close(f->unit, "Interface configuration failed");
1358
            return;
1359
        }
1360
 
1361
#if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1362
        if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1363
            IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1364
            ipcp_close(f->unit, "Interface configuration failed");
1365
            return;
1366
        }
1367
#endif
1368
        sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1369
 
1370
        /* assign a default route through the interface if required */
1371
        if (ipcp_wantoptions[f->unit].default_route)
1372
            if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1373
                default_route_set[f->unit] = 1;
1374
 
1375
        /* Make a proxy ARP entry if requested. */
1376
        if (ipcp_wantoptions[f->unit].proxy_arp)
1377
            if (sifproxyarp(f->unit, ho->hisaddr))
1378
                proxy_arp_set[f->unit] = 1;
1379
 
1380
        syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1381
        syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1382
    }
1383
 
1384
}
1385
 
1386
 
1387
/*
1388
 * ipcp_down - IPCP has gone DOWN.
1389
 *
1390
 * Take the IP network interface down, clear its addresses
1391
 * and delete routes through it.
1392
 */
1393
static void
1394
ipcp_down(f)
1395
    fsm *f;
1396
{
1397
    IPCPDEBUG((LOG_INFO, "ipcp: down"));
1398
    np_down(f->unit, PPP_IP);
1399
    sifvjcomp(f->unit, 0, 0, 0);
1400
 
1401
    /*
1402
     * If we are doing dial-on-demand, set the interface
1403
     * to queue up outgoing packets (for now).
1404
     */
1405
    if (demand) {
1406
        sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1407
    } else {
1408
        sifdown(f->unit);
1409
        ipcp_clear_addrs(f->unit);
1410
    }
1411
 
1412
}
1413
 
1414
 
1415
/*
1416
 * ipcp_clear_addrs() - clear the interface addresses, routes,
1417
 * proxy arp entries, etc.
1418
 */
1419
static void
1420
ipcp_clear_addrs(unit)
1421
    int unit;
1422
{
1423
    u_int32_t ouraddr, hisaddr;
1424
 
1425
    ouraddr = ipcp_gotoptions[unit].ouraddr;
1426
    hisaddr = ipcp_hisoptions[unit].hisaddr;
1427
    if (proxy_arp_set[unit]) {
1428
        cifproxyarp(unit, hisaddr);
1429
        proxy_arp_set[unit] = 0;
1430
    }
1431
    if (default_route_set[unit]) {
1432
        cifdefaultroute(unit, ouraddr, hisaddr);
1433
        default_route_set[unit] = 0;
1434
    }
1435
    cifaddr(unit, ouraddr, hisaddr);
1436
}
1437
 
1438
 
1439
/*
1440
 * ipcp_finished - possibly shut down the lower layers.
1441
 */
1442
static void
1443
ipcp_finished(f)
1444
    fsm *f;
1445
{
1446
    np_finished(f->unit, PPP_IP);
1447
}
1448
 
1449
/*
1450
 * ipcp_printpkt - print the contents of an IPCP packet.
1451
 */
1452
static char *ipcp_codenames[] = {
1453
    "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1454
    "TermReq", "TermAck", "CodeRej"
1455
};
1456
 
1457
static int
1458
ipcp_printpkt(p, plen, printer, arg)
1459
    u_char *p;
1460
    int plen;
1461
    void (*printer) __P((void *, char *, ...));
1462
    void *arg;
1463
{
1464
    int code, id, len, olen;
1465
    u_char *pstart, *optend;
1466
    u_short cishort;
1467
    u_int32_t cilong;
1468
 
1469
    if (plen < HEADERLEN)
1470
        return 0;
1471
    pstart = p;
1472
    GETCHAR(code, p);
1473
    GETCHAR(id, p);
1474
    GETSHORT(len, p);
1475
    if (len < HEADERLEN || len > plen)
1476
        return 0;
1477
 
1478
    if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1479
        printer(arg, " %s", ipcp_codenames[code-1]);
1480
    else
1481
        printer(arg, " code=0x%x", code);
1482
    printer(arg, " id=0x%x", id);
1483
    len -= HEADERLEN;
1484
    switch (code) {
1485
    case CONFREQ:
1486
    case CONFACK:
1487
    case CONFNAK:
1488
    case CONFREJ:
1489
        /* print option list */
1490
        while (len >= 2) {
1491
            GETCHAR(code, p);
1492
            GETCHAR(olen, p);
1493
            p -= 2;
1494
            if (olen < 2 || olen > len) {
1495
                break;
1496
            }
1497
            printer(arg, " <");
1498
            len -= olen;
1499
            optend = p + olen;
1500
            switch (code) {
1501
            case CI_ADDRS:
1502
                if (olen == CILEN_ADDRS) {
1503
                    p += 2;
1504
                    GETLONG(cilong, p);
1505
                    printer(arg, "addrs %I", htonl(cilong));
1506
                    GETLONG(cilong, p);
1507
                    printer(arg, " %I", htonl(cilong));
1508
                }
1509
                break;
1510
            case CI_COMPRESSTYPE:
1511
                if (olen >= CILEN_COMPRESS) {
1512
                    p += 2;
1513
                    GETSHORT(cishort, p);
1514
                    printer(arg, "compress ");
1515
                    switch (cishort) {
1516
                    case IPCP_VJ_COMP:
1517
                        printer(arg, "VJ");
1518
                        break;
1519
                    case IPCP_VJ_COMP_OLD:
1520
                        printer(arg, "old-VJ");
1521
                        break;
1522
                    default:
1523
                        printer(arg, "0x%x", cishort);
1524
                    }
1525
                }
1526
                break;
1527
            case CI_ADDR:
1528
                if (olen == CILEN_ADDR) {
1529
                    p += 2;
1530
                    GETLONG(cilong, p);
1531
                    printer(arg, "addr %I", htonl(cilong));
1532
                }
1533
                break;
1534
            case CI_MS_DNS1:
1535
            case CI_MS_DNS2:
1536
                p += 2;
1537
                GETLONG(cilong, p);
1538
                printer(arg, "ms-dns %I", htonl(cilong));
1539
                break;
1540
            case CI_MS_WINS1:
1541
            case CI_MS_WINS2:
1542
                p += 2;
1543
                GETLONG(cilong, p);
1544
                printer(arg, "ms-wins %I", htonl(cilong));
1545
                break;
1546
            }
1547
            while (p < optend) {
1548
                GETCHAR(code, p);
1549
                printer(arg, " %.2x", code);
1550
            }
1551
            printer(arg, ">");
1552
        }
1553
        break;
1554
 
1555
    case TERMACK:
1556
    case TERMREQ:
1557
        if (len > 0 && *p >= ' ' && *p < 0x7f) {
1558
            printer(arg, " ");
1559
            print_string(p, len, printer, arg);
1560
            p += len;
1561
            len = 0;
1562
        }
1563
        break;
1564
    }
1565
 
1566
    /* print the rest of the bytes in the packet */
1567
    for (; len > 0; --len) {
1568
        GETCHAR(code, p);
1569
        printer(arg, " %.2x", code);
1570
    }
1571
 
1572
    return p - pstart;
1573
}
1574
 
1575
/*
1576
 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1577
 * We don't bring the link up for IP fragments or for TCP FIN packets
1578
 * with no data.
1579
 */
1580
#define IP_HDRLEN       20      /* bytes */
1581
#define IP_OFFMASK      0x1fff
1582
#define IPPROTO_TCP     6
1583
#define TCP_HDRLEN      20
1584
#define TH_FIN          0x01
1585
 
1586
/*
1587
 * We use these macros because the IP header may be at an odd address,
1588
 * and some compilers might use word loads to get th_off or ip_hl.
1589
 */
1590
 
1591
#define net_short(x)    (((x)[0] << 8) + (x)[1])
1592
#define get_iphl(x)     (((unsigned char *)(x))[0] & 0xF)
1593
#define get_ipoff(x)    net_short((unsigned char *)(x) + 6)
1594
#define get_ipproto(x)  (((unsigned char *)(x))[9])
1595
#define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
1596
#define get_tcpflags(x) (((unsigned char *)(x))[13])
1597
 
1598
static int
1599
ip_active_pkt(pkt, len)
1600
    u_char *pkt;
1601
    int len;
1602
{
1603
    u_char *tcp;
1604
    int hlen;
1605
 
1606
    len -= PPP_HDRLEN;
1607
    pkt += PPP_HDRLEN;
1608
    if (len < IP_HDRLEN)
1609
        return 0;
1610
    if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1611
        return 0;
1612
    if (get_ipproto(pkt) != IPPROTO_TCP)
1613
        return 1;
1614
    hlen = get_iphl(pkt) * 4;
1615
    if (len < hlen + TCP_HDRLEN)
1616
        return 0;
1617
    tcp = pkt + hlen;
1618
    if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
1619
        return 0;
1620
    return 1;
1621
}

powered by: WebSVN 2.1.0

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