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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP_132/] [src/] [netif/] [ppp/] [ipcp.c] - Blame information for rev 606

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/*****************************************************************************
2
* ipcp.c - Network PPP IP Control Protocol program file.
3
*
4
* Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5
* portions Copyright (c) 1997 by Global Election Systems Inc.
6
*
7
* The authors hereby grant permission to use, copy, modify, distribute,
8
* and license this software and its documentation for any purpose, provided
9
* that existing copyright notices are retained in all copies and that this
10
* notice and the following disclaimer are included verbatim in any
11
* distributions. No written agreement, license, or royalty fee is required
12
* for any of the authorized uses.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
*
25
******************************************************************************
26
* REVISION HISTORY
27
*
28
* 03-01-01 Marc Boucher <marc@mbsi.ca>
29
*   Ported to lwIP.
30
* 97-12-08 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
31
*   Original.
32
*****************************************************************************/
33
/*
34
 * ipcp.c - PPP IP Control Protocol.
35
 *
36
 * Copyright (c) 1989 Carnegie Mellon University.
37
 * All rights reserved.
38
 *
39
 * Redistribution and use in source and binary forms are permitted
40
 * provided that the above copyright notice and this paragraph are
41
 * duplicated in all such forms and that any documentation,
42
 * advertising materials, and other materials related to such
43
 * distribution and use acknowledge that the software was developed
44
 * by Carnegie Mellon University.  The name of the
45
 * University may not be used to endorse or promote products derived
46
 * from this software without specific prior written permission.
47
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50
 */
51
 
52
#include "lwip/opt.h"
53
 
54
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
55
 
56
#include "ppp.h"
57
#include "pppdebug.h"
58
 
59
#include "auth.h"
60
#include "fsm.h"
61
#include "vj.h"
62
#include "ipcp.h"
63
 
64
#include <string.h>
65
 
66
/*************************/
67
/*** LOCAL DEFINITIONS ***/
68
/*************************/
69
/* #define OLD_CI_ADDRS 1 */ /* Support deprecated address negotiation. */
70
 
71
/*
72
 * Lengths of configuration options.
73
 */
74
#define CILEN_VOID     2
75
#define CILEN_COMPRESS 4  /* min length for compression protocol opt. */
76
#define CILEN_VJ       6  /* length for RFC1332 Van-Jacobson opt. */
77
#define CILEN_ADDR     6  /* new-style single address option */
78
#define CILEN_ADDRS    10 /* old-style dual address option */
79
 
80
 
81
 
82
/***********************************/
83
/*** LOCAL FUNCTION DECLARATIONS ***/
84
/***********************************/
85
/*
86
 * Callbacks for fsm code.  (CI = Configuration Information)
87
 */
88
static void ipcp_resetci (fsm *);                     /* Reset our CI */
89
static int  ipcp_cilen (fsm *);                       /* Return length of our CI */
90
static void ipcp_addci (fsm *, u_char *, int *);      /* Add our CI */
91
static int  ipcp_ackci (fsm *, u_char *, int);        /* Peer ack'd our CI */
92
static int  ipcp_nakci (fsm *, u_char *, int);        /* Peer nak'd our CI */
93
static int  ipcp_rejci (fsm *, u_char *, int);        /* Peer rej'd our CI */
94
static int  ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
95
static void ipcp_up (fsm *);                          /* We're UP */
96
static void ipcp_down (fsm *);                        /* We're DOWN */
97
#if 0
98
static void ipcp_script (fsm *, char *); /* Run an up/down script */
99
#endif
100
static void ipcp_finished (fsm *);                    /* Don't need lower layer */
101
 
102
/*
103
 * Protocol entry points from main code.
104
 */
105
static void ipcp_init (int);
106
static void ipcp_open (int);
107
static void ipcp_close (int, char *);
108
static void ipcp_lowerup (int);
109
static void ipcp_lowerdown (int);
110
static void ipcp_input (int, u_char *, int);
111
static void ipcp_protrej (int);
112
 
113
static void ipcp_clear_addrs (int);
114
 
115
#define CODENAME(x) ((x) == CONFACK ? "ACK" : \
116
                     (x) == CONFNAK ? "NAK" : "REJ")
117
 
118
 
119
 
120
/******************************/
121
/*** PUBLIC DATA STRUCTURES ***/
122
/******************************/
123
/* global vars */
124
ipcp_options ipcp_wantoptions[NUM_PPP];  /* Options that we want to request */
125
ipcp_options ipcp_gotoptions[NUM_PPP];   /* Options that peer ack'd */
126
ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
127
ipcp_options ipcp_hisoptions[NUM_PPP];   /* Options that we ack'd */
128
 
129
fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
130
 
131
struct protent ipcp_protent = {
132
  PPP_IPCP,
133
  ipcp_init,
134
  ipcp_input,
135
  ipcp_protrej,
136
  ipcp_lowerup,
137
  ipcp_lowerdown,
138
  ipcp_open,
139
  ipcp_close,
140
#if 0
141
  ipcp_printpkt,
142
  NULL,
143
#endif
144
  1,
145
  "IPCP",
146
#if 0
147
  ip_check_options,
148
  NULL,
149
  ip_active_pkt
150
#endif
151
};
152
 
153
 
154
 
155
/*****************************/
156
/*** LOCAL DATA STRUCTURES ***/
157
/*****************************/
158
/* local vars */
159
static int cis_received[NUM_PPP];      /* # Conf-Reqs received */
160
static int default_route_set[NUM_PPP]; /* Have set up a default route */
161
 
162
static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
163
  ipcp_resetci,  /* Reset our Configuration Information */
164
  ipcp_cilen,    /* Length of our Configuration Information */
165
  ipcp_addci,    /* Add our Configuration Information */
166
  ipcp_ackci,    /* ACK our Configuration Information */
167
  ipcp_nakci,    /* NAK our Configuration Information */
168
  ipcp_rejci,    /* Reject our Configuration Information */
169
  ipcp_reqci,    /* Request peer's Configuration Information */
170
  ipcp_up,       /* Called when fsm reaches LS_OPENED state */
171
  ipcp_down,     /* Called when fsm leaves LS_OPENED state */
172
  NULL,          /* Called when we want the lower layer up */
173
  ipcp_finished, /* Called when we want the lower layer down */
174
  NULL,          /* Called when Protocol-Reject received */
175
  NULL,          /* Retransmission is necessary */
176
  NULL,          /* Called to handle protocol-specific codes */
177
  "IPCP"         /* String name of protocol */
178
};
179
 
180
 
181
 
182
/**********************************/
183
/*** LOCAL FUNCTION DEFINITIONS ***/
184
/**********************************/
185
 
186
#define inet_ntoa(addr) ip_ntoa(((struct ip_addr*)&(addr)))
187
 
188
/*
189
 * ipcp_init - Initialize IPCP.
190
 */
191
static void
192
ipcp_init(int unit)
193
{
194
  fsm           *f = &ipcp_fsm[unit];
195
  ipcp_options *wo = &ipcp_wantoptions[unit];
196
  ipcp_options *ao = &ipcp_allowoptions[unit];
197
 
198
  f->unit      = unit;
199
  f->protocol  = PPP_IPCP;
200
  f->callbacks = &ipcp_callbacks;
201
  fsm_init(&ipcp_fsm[unit]);
202
 
203
  memset(wo, 0, sizeof(*wo));
204
  memset(ao, 0, sizeof(*ao));
205
 
206
  wo->neg_addr      = 1;
207
  wo->ouraddr       = 0;
208
#if VJ_SUPPORT
209
  wo->neg_vj        = 1;
210
#else  /* VJ_SUPPORT */
211
  wo->neg_vj        = 0;
212
#endif /* VJ_SUPPORT */
213
  wo->vj_protocol   = IPCP_VJ_COMP;
214
  wo->maxslotindex  = MAX_SLOTS - 1;
215
  wo->cflag         = 0;
216
  wo->default_route = 1;
217
 
218
  ao->neg_addr      = 1;
219
#if VJ_SUPPORT
220
  ao->neg_vj        = 1;
221
#else  /* VJ_SUPPORT */
222
  ao->neg_vj        = 0;
223
#endif /* VJ_SUPPORT */
224
  ao->maxslotindex  = MAX_SLOTS - 1;
225
  ao->cflag         = 1;
226
  ao->default_route = 1;
227
}
228
 
229
 
230
/*
231
 * ipcp_open - IPCP is allowed to come up.
232
 */
233
static void
234
ipcp_open(int unit)
235
{
236
  fsm_open(&ipcp_fsm[unit]);
237
}
238
 
239
 
240
/*
241
 * ipcp_close - Take IPCP down.
242
 */
243
static void
244
ipcp_close(int unit, char *reason)
245
{
246
  fsm_close(&ipcp_fsm[unit], reason);
247
}
248
 
249
 
250
/*
251
 * ipcp_lowerup - The lower layer is up.
252
 */
253
static void
254
ipcp_lowerup(int unit)
255
{
256
  fsm_lowerup(&ipcp_fsm[unit]);
257
}
258
 
259
 
260
/*
261
 * ipcp_lowerdown - The lower layer is down.
262
 */
263
static void
264
ipcp_lowerdown(int unit)
265
{
266
  fsm_lowerdown(&ipcp_fsm[unit]);
267
}
268
 
269
 
270
/*
271
 * ipcp_input - Input IPCP packet.
272
 */
273
static void
274
ipcp_input(int unit, u_char *p, int len)
275
{
276
  fsm_input(&ipcp_fsm[unit], p, len);
277
}
278
 
279
 
280
/*
281
 * ipcp_protrej - A Protocol-Reject was received for IPCP.
282
 *
283
 * Pretend the lower layer went down, so we shut up.
284
 */
285
static void
286
ipcp_protrej(int unit)
287
{
288
  fsm_lowerdown(&ipcp_fsm[unit]);
289
}
290
 
291
 
292
/*
293
 * ipcp_resetci - Reset our CI.
294
 */
295
static void
296
ipcp_resetci(fsm *f)
297
{
298
  ipcp_options *wo = &ipcp_wantoptions[f->unit];
299
 
300
  wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
301
  if (wo->ouraddr == 0) {
302
    wo->accept_local = 1;
303
  }
304
  if (wo->hisaddr == 0) {
305
    wo->accept_remote = 1;
306
  }
307
  /* Request DNS addresses from the peer */
308
  wo->req_dns1 = ppp_settings.usepeerdns;
309
  wo->req_dns2 = ppp_settings.usepeerdns;
310
  ipcp_gotoptions[f->unit] = *wo;
311
  cis_received[f->unit] = 0;
312
}
313
 
314
 
315
/*
316
 * ipcp_cilen - Return length of our CI.
317
 */
318
static int
319
ipcp_cilen(fsm *f)
320
{
321
  ipcp_options *go = &ipcp_gotoptions[f->unit];
322
  ipcp_options *wo = &ipcp_wantoptions[f->unit];
323
  ipcp_options *ho = &ipcp_hisoptions[f->unit];
324
 
325
#define LENCIVJ(neg, old)   (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
326
#define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
327
#define LENCIDNS(neg)       (neg ? (CILEN_ADDR) : 0)
328
 
329
  /*
330
   * First see if we want to change our options to the old
331
   * forms because we have received old forms from the peer.
332
   */
333
  if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
334
    /* use the old style of address negotiation */
335
    go->neg_addr = 1;
336
    go->old_addrs = 1;
337
  }
338
  if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
339
    /* try an older style of VJ negotiation */
340
    if (cis_received[f->unit] == 0) {
341
      /* keep trying the new style until we see some CI from the peer */
342
      go->neg_vj = 1;
343
    } else {
344
      /* use the old style only if the peer did */
345
      if (ho->neg_vj && ho->old_vj) {
346
        go->neg_vj = 1;
347
        go->old_vj = 1;
348
        go->vj_protocol = ho->vj_protocol;
349
      }
350
    }
351
  }
352
 
353
  return (LENCIADDR(go->neg_addr, go->old_addrs) +
354
          LENCIVJ(go->neg_vj, go->old_vj) +
355
          LENCIDNS(go->req_dns1) +
356
          LENCIDNS(go->req_dns2));
357
}
358
 
359
 
360
/*
361
 * ipcp_addci - Add our desired CIs to a packet.
362
 */
363
static void
364
ipcp_addci(fsm *f, u_char *ucp, int *lenp)
365
{
366
  ipcp_options *go = &ipcp_gotoptions[f->unit];
367
  int len = *lenp;
368
 
369
#define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
370
  if (neg) { \
371
    int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
372
    if (len >= vjlen) { \
373
      PUTCHAR(opt, ucp); \
374
      PUTCHAR(vjlen, ucp); \
375
      PUTSHORT(val, ucp); \
376
      if (!old) { \
377
        PUTCHAR(maxslotindex, ucp); \
378
        PUTCHAR(cflag, ucp); \
379
      } \
380
      len -= vjlen; \
381
    } else { \
382
      neg = 0; \
383
    } \
384
  }
385
 
386
#define ADDCIADDR(opt, neg, old, val1, val2) \
387
  if (neg) { \
388
    int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
389
    if (len >= addrlen) { \
390
      u32_t l; \
391
      PUTCHAR(opt, ucp); \
392
      PUTCHAR(addrlen, ucp); \
393
      l = ntohl(val1); \
394
      PUTLONG(l, ucp); \
395
      if (old) { \
396
        l = ntohl(val2); \
397
        PUTLONG(l, ucp); \
398
      } \
399
      len -= addrlen; \
400
    } else { \
401
      neg = 0; \
402
    } \
403
  }
404
 
405
#define ADDCIDNS(opt, neg, addr) \
406
  if (neg) { \
407
    if (len >= CILEN_ADDR) { \
408
      u32_t l; \
409
      PUTCHAR(opt, ucp); \
410
      PUTCHAR(CILEN_ADDR, ucp); \
411
      l = ntohl(addr); \
412
      PUTLONG(l, ucp); \
413
      len -= CILEN_ADDR; \
414
    } else { \
415
      neg = 0; \
416
    } \
417
  }
418
 
419
  ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
420
      go->old_addrs, go->ouraddr, go->hisaddr);
421
 
422
  ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
423
      go->maxslotindex, go->cflag);
424
 
425
  ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
426
 
427
  ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
428
 
429
  *lenp -= len;
430
}
431
 
432
 
433
/*
434
 * ipcp_ackci - Ack our CIs.
435
 *
436
 * Returns:
437
 *  0 - Ack was bad.
438
 *  1 - Ack was good.
439
 */
440
static int
441
ipcp_ackci(fsm *f, u_char *p, int len)
442
{
443
  ipcp_options *go = &ipcp_gotoptions[f->unit];
444
  u_short cilen, citype, cishort;
445
  u32_t cilong;
446
  u_char cimaxslotindex, cicflag;
447
 
448
  /*
449
   * CIs must be in exactly the same order that we sent...
450
   * Check packet length and CI length at each step.
451
   * If we find any deviations, then this packet is bad.
452
   */
453
 
454
#define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
455
  if (neg) { \
456
    int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
457
    if ((len -= vjlen) < 0) { \
458
      goto bad; \
459
    } \
460
    GETCHAR(citype, p); \
461
    GETCHAR(cilen, p); \
462
    if (cilen != vjlen || \
463
        citype != opt) { \
464
      goto bad; \
465
    } \
466
    GETSHORT(cishort, p); \
467
    if (cishort != val) { \
468
      goto bad; \
469
    } \
470
    if (!old) { \
471
      GETCHAR(cimaxslotindex, p); \
472
      if (cimaxslotindex != maxslotindex) { \
473
        goto bad; \
474
      } \
475
      GETCHAR(cicflag, p); \
476
      if (cicflag != cflag) { \
477
        goto bad; \
478
      } \
479
    } \
480
  }
481
 
482
#define ACKCIADDR(opt, neg, old, val1, val2) \
483
  if (neg) { \
484
    int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
485
    u32_t l; \
486
    if ((len -= addrlen) < 0) { \
487
      goto bad; \
488
    } \
489
    GETCHAR(citype, p); \
490
    GETCHAR(cilen, p); \
491
    if (cilen != addrlen || \
492
        citype != opt) { \
493
      goto bad; \
494
    } \
495
    GETLONG(l, p); \
496
    cilong = htonl(l); \
497
    if (val1 != cilong) { \
498
      goto bad; \
499
    } \
500
    if (old) { \
501
      GETLONG(l, p); \
502
      cilong = htonl(l); \
503
      if (val2 != cilong) { \
504
        goto bad; \
505
      } \
506
    } \
507
  }
508
 
509
#define ACKCIDNS(opt, neg, addr) \
510
  if (neg) { \
511
    u32_t l; \
512
    if ((len -= CILEN_ADDR) < 0) { \
513
      goto bad; \
514
    } \
515
    GETCHAR(citype, p); \
516
    GETCHAR(cilen, p); \
517
    if (cilen != CILEN_ADDR || \
518
        citype != opt) { \
519
      goto bad; \
520
    } \
521
    GETLONG(l, p); \
522
    cilong = htonl(l); \
523
    if (addr != cilong) { \
524
      goto bad; \
525
    } \
526
  }
527
 
528
  ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
529
        go->old_addrs, go->ouraddr, go->hisaddr);
530
 
531
  ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
532
      go->maxslotindex, go->cflag);
533
 
534
  ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
535
 
536
  ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
537
 
538
  /*
539
   * If there are any remaining CIs, then this packet is bad.
540
   */
541
  if (len != 0) {
542
    goto bad;
543
  }
544
  return (1);
545
 
546
bad:
547
  IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!\n"));
548
  return (0);
549
}
550
 
551
/*
552
 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
553
 * This should not modify any state if the Nak is bad
554
 * or if IPCP is in the LS_OPENED state.
555
 *
556
 * Returns:
557
 *  0 - Nak was bad.
558
 *  1 - Nak was good.
559
 */
560
static int
561
ipcp_nakci(fsm *f, u_char *p, int len)
562
{
563
  ipcp_options *go = &ipcp_gotoptions[f->unit];
564
  u_char cimaxslotindex, cicflag;
565
  u_char citype, cilen, *next;
566
  u_short cishort;
567
  u32_t ciaddr1, ciaddr2, l, cidnsaddr;
568
  ipcp_options no;    /* options we've seen Naks for */
569
  ipcp_options try;    /* options to request next time */
570
 
571
  BZERO(&no, sizeof(no));
572
  try = *go;
573
 
574
  /*
575
   * Any Nak'd CIs must be in exactly the same order that we sent.
576
   * Check packet length and CI length at each step.
577
   * If we find any deviations, then this packet is bad.
578
   */
579
#define NAKCIADDR(opt, neg, old, code) \
580
  if (go->neg && \
581
      len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
582
      p[1] == cilen && \
583
      p[0] == opt) { \
584
    len -= cilen; \
585
    INCPTR(2, p); \
586
    GETLONG(l, p); \
587
    ciaddr1 = htonl(l); \
588
    if (old) { \
589
      GETLONG(l, p); \
590
      ciaddr2 = htonl(l); \
591
      no.old_addrs = 1; \
592
    } else { \
593
      ciaddr2 = 0; \
594
    } \
595
    no.neg = 1; \
596
    code \
597
  }
598
 
599
#define NAKCIVJ(opt, neg, code) \
600
  if (go->neg && \
601
      ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
602
      len >= cilen && \
603
      p[0] == opt) { \
604
    len -= cilen; \
605
    INCPTR(2, p); \
606
    GETSHORT(cishort, p); \
607
    no.neg = 1; \
608
    code \
609
  }
610
 
611
#define NAKCIDNS(opt, neg, code) \
612
  if (go->neg && \
613
      ((cilen = p[1]) == CILEN_ADDR) && \
614
      len >= cilen && \
615
      p[0] == opt) { \
616
    len -= cilen; \
617
    INCPTR(2, p); \
618
    GETLONG(l, p); \
619
    cidnsaddr = htonl(l); \
620
    no.neg = 1; \
621
    code \
622
  }
623
 
624
  /*
625
   * Accept the peer's idea of {our,his} address, if different
626
   * from our idea, only if the accept_{local,remote} flag is set.
627
   */
628
  NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
629
    if (go->accept_local && ciaddr1) { /* Do we know our address? */
630
      try.ouraddr = ciaddr1;
631
      IPCPDEBUG((LOG_INFO, "local IP address %s\n",
632
           inet_ntoa(ciaddr1)));
633
    }
634
    if (go->accept_remote && ciaddr2) { /* Does he know his? */
635
      try.hisaddr = ciaddr2;
636
      IPCPDEBUG((LOG_INFO, "remote IP address %s\n",
637
           inet_ntoa(ciaddr2)));
638
    }
639
  );
640
 
641
  /*
642
   * Accept the peer's value of maxslotindex provided that it
643
   * is less than what we asked for.  Turn off slot-ID compression
644
   * if the peer wants.  Send old-style compress-type option if
645
   * the peer wants.
646
   */
647
  NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
648
    if (cilen == CILEN_VJ) {
649
      GETCHAR(cimaxslotindex, p);
650
      GETCHAR(cicflag, p);
651
      if (cishort == IPCP_VJ_COMP) {
652
        try.old_vj = 0;
653
        if (cimaxslotindex < go->maxslotindex) {
654
          try.maxslotindex = cimaxslotindex;
655
        }
656
        if (!cicflag) {
657
          try.cflag = 0;
658
        }
659
      } else {
660
        try.neg_vj = 0;
661
      }
662
    } else {
663
      if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
664
        try.old_vj = 1;
665
        try.vj_protocol = cishort;
666
      } else {
667
        try.neg_vj = 0;
668
      }
669
    }
670
  );
671
 
672
  NAKCIDNS(CI_MS_DNS1, req_dns1,
673
      try.dnsaddr[0] = cidnsaddr;
674
        IPCPDEBUG((LOG_INFO, "primary DNS address %s\n", inet_ntoa(cidnsaddr)));
675
      );
676
 
677
  NAKCIDNS(CI_MS_DNS2, req_dns2,
678
      try.dnsaddr[1] = cidnsaddr;
679
        IPCPDEBUG((LOG_INFO, "secondary DNS address %s\n", inet_ntoa(cidnsaddr)));
680
      );
681
 
682
  /*
683
  * There may be remaining CIs, if the peer is requesting negotiation
684
  * on an option that we didn't include in our request packet.
685
  * If they want to negotiate about IP addresses, we comply.
686
  * If they want us to ask for compression, we refuse.
687
  */
688
  while (len > CILEN_VOID) {
689
    GETCHAR(citype, p);
690
    GETCHAR(cilen, p);
691
    if( (len -= cilen) < 0 ) {
692
      goto bad;
693
    }
694
    next = p + cilen - 2;
695
 
696
    switch (citype) {
697
      case CI_COMPRESSTYPE:
698
        if (go->neg_vj || no.neg_vj ||
699
            (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
700
          goto bad;
701
        }
702
        no.neg_vj = 1;
703
        break;
704
      case CI_ADDRS:
705
        if ((go->neg_addr && go->old_addrs) || no.old_addrs
706
            || cilen != CILEN_ADDRS) {
707
          goto bad;
708
        }
709
        try.neg_addr = 1;
710
        try.old_addrs = 1;
711
        GETLONG(l, p);
712
        ciaddr1 = htonl(l);
713
        if (ciaddr1 && go->accept_local) {
714
          try.ouraddr = ciaddr1;
715
        }
716
        GETLONG(l, p);
717
        ciaddr2 = htonl(l);
718
        if (ciaddr2 && go->accept_remote) {
719
          try.hisaddr = ciaddr2;
720
        }
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
        }
727
        try.old_addrs = 0;
728
        GETLONG(l, p);
729
        ciaddr1 = htonl(l);
730
        if (ciaddr1 && go->accept_local) {
731
          try.ouraddr = ciaddr1;
732
        }
733
        if (try.ouraddr != 0) {
734
          try.neg_addr = 1;
735
        }
736
        no.neg_addr = 1;
737
        break;
738
    }
739
    p = next;
740
  }
741
 
742
  /* If there is still anything left, this packet is bad. */
743
  if (len != 0) {
744
    goto bad;
745
  }
746
 
747
  /*
748
   * OK, the Nak is good.  Now we can update state.
749
   */
750
  if (f->state != LS_OPENED) {
751
    *go = try;
752
  }
753
 
754
  return 1;
755
 
756
bad:
757
  IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!\n"));
758
  return 0;
759
}
760
 
761
 
762
/*
763
 * ipcp_rejci - Reject some of our CIs.
764
 */
765
static int
766
ipcp_rejci(fsm *f, u_char *p, int len)
767
{
768
  ipcp_options *go = &ipcp_gotoptions[f->unit];
769
  u_char cimaxslotindex, ciflag, cilen;
770
  u_short cishort;
771
  u32_t cilong;
772
  ipcp_options try;    /* options to request next time */
773
 
774
  try = *go;
775
  /*
776
   * Any Rejected CIs must be in exactly the same order that we sent.
777
   * Check packet length and CI length at each step.
778
   * If we find any deviations, then this packet is bad.
779
   */
780
#define REJCIADDR(opt, neg, old, val1, val2) \
781
  if (go->neg && \
782
      len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
783
      p[1] == cilen && \
784
      p[0] == opt) { \
785
    u32_t l; \
786
    len -= cilen; \
787
    INCPTR(2, p); \
788
    GETLONG(l, p); \
789
    cilong = htonl(l); \
790
    /* Check rejected value. */ \
791
    if (cilong != val1) { \
792
      goto bad; \
793
    } \
794
    if (old) { \
795
      GETLONG(l, p); \
796
      cilong = htonl(l); \
797
      /* Check rejected value. */ \
798
      if (cilong != val2) { \
799
        goto bad; \
800
      } \
801
    } \
802
    try.neg = 0; \
803
  }
804
 
805
#define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
806
  if (go->neg && \
807
      p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
808
      len >= p[1] && \
809
      p[0] == opt) { \
810
    len -= p[1]; \
811
    INCPTR(2, p); \
812
    GETSHORT(cishort, p); \
813
    /* Check rejected value. */  \
814
    if (cishort != val) { \
815
      goto bad; \
816
    } \
817
    if (!old) { \
818
      GETCHAR(cimaxslotindex, p); \
819
      if (cimaxslotindex != maxslot) { \
820
        goto bad; \
821
      } \
822
      GETCHAR(ciflag, p); \
823
      if (ciflag != cflag) { \
824
        goto bad; \
825
      } \
826
    } \
827
    try.neg = 0; \
828
  }
829
 
830
#define REJCIDNS(opt, neg, dnsaddr) \
831
  if (go->neg && \
832
      ((cilen = p[1]) == CILEN_ADDR) && \
833
      len >= cilen && \
834
      p[0] == opt) { \
835
    u32_t l; \
836
    len -= cilen; \
837
    INCPTR(2, p); \
838
    GETLONG(l, p); \
839
    cilong = htonl(l); \
840
    /* Check rejected value. */ \
841
    if (cilong != dnsaddr) { \
842
      goto bad; \
843
    } \
844
    try.neg = 0; \
845
  }
846
 
847
  REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
848
        go->old_addrs, go->ouraddr, go->hisaddr);
849
 
850
  REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
851
      go->maxslotindex, go->cflag);
852
 
853
  REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
854
 
855
  REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
856
 
857
  /*
858
   * If there are any remaining CIs, then this packet is bad.
859
   */
860
  if (len != 0) {
861
    goto bad;
862
  }
863
  /*
864
   * Now we can update state.
865
   */
866
  if (f->state != LS_OPENED) {
867
    *go = try;
868
  }
869
  return 1;
870
 
871
bad:
872
  IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!\n"));
873
  return 0;
874
}
875
 
876
 
877
/*
878
 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
879
 *
880
 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
881
 * appropriately.  If reject_if_disagree is non-zero, doesn't return
882
 * CONFNAK; returns CONFREJ if it can't return CONFACK.
883
 */
884
static int
885
ipcp_reqci(fsm *f, u_char *inp/* Requested CIs */,int *len/* Length of requested CIs */,int reject_if_disagree)
886
{
887
  ipcp_options *wo = &ipcp_wantoptions[f->unit];
888
  ipcp_options *ho = &ipcp_hisoptions[f->unit];
889
  ipcp_options *ao = &ipcp_allowoptions[f->unit];
890
#ifdef OLD_CI_ADDRS
891
  ipcp_options *go = &ipcp_gotoptions[f->unit];
892
#endif
893
  u_char *cip, *next;     /* Pointer to current and next CIs */
894
  u_short cilen, citype;  /* Parsed len, type */
895
  u_short cishort;        /* Parsed short value */
896
  u32_t tl, ciaddr1;      /* Parsed address values */
897
#ifdef OLD_CI_ADDRS
898
  u32_t ciaddr2;          /* Parsed address values */
899
#endif
900
  int rc = CONFACK;       /* Final packet return code */
901
  int orc;                /* Individual option return code */
902
  u_char *p;              /* Pointer to next char to parse */
903
  u_char *ucp = inp;      /* Pointer to current output char */
904
  int l = *len;           /* Length left */
905
  u_char maxslotindex, cflag;
906
  int d;
907
 
908
  cis_received[f->unit] = 1;
909
 
910
  /*
911
   * Reset all his options.
912
   */
913
  BZERO(ho, sizeof(*ho));
914
 
915
  /*
916
   * Process all his options.
917
   */
918
  next = inp;
919
  while (l) {
920
    orc = CONFACK;       /* Assume success */
921
    cip = p = next;      /* Remember begining of CI */
922
    if (l < 2 ||         /* Not enough data for CI header or */
923
        p[1] < 2 ||      /*  CI length too small or */
924
        p[1] > l) {      /*  CI length too big? */
925
      IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!\n"));
926
      orc = CONFREJ;     /* Reject bad CI */
927
      cilen = l;         /* Reject till end of packet */
928
      l = 0;             /* Don't loop again */
929
      goto endswitch;
930
    }
931
    GETCHAR(citype, p);  /* Parse CI type */
932
    GETCHAR(cilen, p);   /* Parse CI length */
933
    l -= cilen;          /* Adjust remaining length */
934
    next += cilen;       /* Step to next CI */
935
 
936
    switch (citype) {      /* Check CI type */
937
#ifdef OLD_CI_ADDRS /* Need to save space... */
938
      case CI_ADDRS:
939
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: received ADDRS\n"));
940
        if (!ao->neg_addr ||
941
            cilen != CILEN_ADDRS) {  /* Check CI length */
942
          orc = CONFREJ;    /* Reject CI */
943
          break;
944
        }
945
 
946
        /*
947
         * If he has no address, or if we both have his address but
948
         * disagree about it, then NAK it with our idea.
949
         * In particular, if we don't know his address, but he does,
950
         * then accept it.
951
         */
952
        GETLONG(tl, p);    /* Parse source address (his) */
953
        ciaddr1 = htonl(tl);
954
        IPCPDEBUG((LOG_INFO, "his addr %s\n", inet_ntoa(ciaddr1)));
955
        if (ciaddr1 != wo->hisaddr
956
            && (ciaddr1 == 0 || !wo->accept_remote)) {
957
          orc = CONFNAK;
958
          if (!reject_if_disagree) {
959
            DECPTR(sizeof(u32_t), p);
960
            tl = ntohl(wo->hisaddr);
961
            PUTLONG(tl, p);
962
          }
963
        } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
964
          /*
965
           * If neither we nor he knows his address, reject the option.
966
           */
967
          orc = CONFREJ;
968
          wo->req_addr = 0;  /* don't NAK with 0.0.0.0 later */
969
          break;
970
        }
971
 
972
        /*
973
         * If he doesn't know our address, or if we both have our address
974
         * but disagree about it, then NAK it with our idea.
975
         */
976
        GETLONG(tl, p);    /* Parse desination address (ours) */
977
        ciaddr2 = htonl(tl);
978
        IPCPDEBUG((LOG_INFO, "our addr %s\n", inet_ntoa(ciaddr2)));
979
        if (ciaddr2 != wo->ouraddr) {
980
          if (ciaddr2 == 0 || !wo->accept_local) {
981
            orc = CONFNAK;
982
            if (!reject_if_disagree) {
983
              DECPTR(sizeof(u32_t), p);
984
              tl = ntohl(wo->ouraddr);
985
              PUTLONG(tl, p);
986
            }
987
          } else {
988
            go->ouraddr = ciaddr2;  /* accept peer's idea */
989
          }
990
        }
991
 
992
        ho->neg_addr = 1;
993
        ho->old_addrs = 1;
994
        ho->hisaddr = ciaddr1;
995
        ho->ouraddr = ciaddr2;
996
        break;
997
#endif
998
 
999
      case CI_ADDR:
1000
        if (!ao->neg_addr) {
1001
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Reject ADDR not allowed\n"));
1002
          orc = CONFREJ;        /* Reject CI */
1003
          break;
1004
        } else if (cilen != CILEN_ADDR) {  /* Check CI length */
1005
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Reject ADDR bad len\n"));
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
        if (ciaddr1 != wo->hisaddr
1019
            && (ciaddr1 == 0 || !wo->accept_remote)) {
1020
          orc = CONFNAK;
1021
          if (!reject_if_disagree) {
1022
            DECPTR(sizeof(u32_t), p);
1023
            tl = ntohl(wo->hisaddr);
1024
            PUTLONG(tl, p);
1025
          }
1026
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Nak ADDR %s\n", inet_ntoa(ciaddr1)));
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
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Reject ADDR %s\n", inet_ntoa(ciaddr1)));
1032
          orc = CONFREJ;
1033
          wo->req_addr = 0;  /* don't NAK with 0.0.0.0 later */
1034
          break;
1035
        }
1036
 
1037
        ho->neg_addr = 1;
1038
        ho->hisaddr = ciaddr1;
1039
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: ADDR %s\n", inet_ntoa(ciaddr1)));
1040
        break;
1041
 
1042
      case CI_MS_DNS1:
1043
      case CI_MS_DNS2:
1044
        /* Microsoft primary or secondary DNS request */
1045
        d = citype == CI_MS_DNS2;
1046
 
1047
        /* If we do not have a DNS address then we cannot send it */
1048
        if (ao->dnsaddr[d] == 0 ||
1049
            cilen != CILEN_ADDR) {  /* Check CI length */
1050
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting DNS%d Request\n", d+1));
1051
          orc = CONFREJ;        /* Reject CI */
1052
          break;
1053
        }
1054
        GETLONG(tl, p);
1055
        if (htonl(tl) != ao->dnsaddr[d]) {
1056
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Naking DNS%d Request %d\n",
1057
                d+1, inet_ntoa(tl)));
1058
          DECPTR(sizeof(u32_t), p);
1059
          tl = ntohl(ao->dnsaddr[d]);
1060
          PUTLONG(tl, p);
1061
          orc = CONFNAK;
1062
        }
1063
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: received DNS%d Request\n", d+1));
1064
        break;
1065
 
1066
      case CI_MS_WINS1:
1067
      case CI_MS_WINS2:
1068
        /* Microsoft primary or secondary WINS request */
1069
        d = citype == CI_MS_WINS2;
1070
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: received WINS%d Request\n", d+1));
1071
 
1072
        /* If we do not have a DNS address then we cannot send it */
1073
        if (ao->winsaddr[d] == 0 ||
1074
          cilen != CILEN_ADDR) {  /* Check CI length */
1075
          orc = CONFREJ;      /* Reject CI */
1076
          break;
1077
        }
1078
        GETLONG(tl, p);
1079
        if (htonl(tl) != ao->winsaddr[d]) {
1080
          DECPTR(sizeof(u32_t), p);
1081
          tl = ntohl(ao->winsaddr[d]);
1082
          PUTLONG(tl, p);
1083
          orc = CONFNAK;
1084
        }
1085
        break;
1086
 
1087
      case CI_COMPRESSTYPE:
1088
        if (!ao->neg_vj) {
1089
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting COMPRESSTYPE not allowed\n"));
1090
          orc = CONFREJ;
1091
          break;
1092
        } else if (cilen != CILEN_VJ && cilen != CILEN_COMPRESS) {
1093
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting COMPRESSTYPE len=%d\n", cilen));
1094
          orc = CONFREJ;
1095
          break;
1096
        }
1097
        GETSHORT(cishort, p);
1098
 
1099
        if (!(cishort == IPCP_VJ_COMP ||
1100
            (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1101
          IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting COMPRESSTYPE %d\n", cishort));
1102
          orc = CONFREJ;
1103
          break;
1104
        }
1105
 
1106
        ho->neg_vj = 1;
1107
        ho->vj_protocol = cishort;
1108
        if (cilen == CILEN_VJ) {
1109
          GETCHAR(maxslotindex, p);
1110
          if (maxslotindex > ao->maxslotindex) {
1111
            IPCPDEBUG((LOG_INFO, "ipcp_reqci: Naking VJ max slot %d\n", maxslotindex));
1112
            orc = CONFNAK;
1113
            if (!reject_if_disagree) {
1114
              DECPTR(1, p);
1115
              PUTCHAR(ao->maxslotindex, p);
1116
            }
1117
          }
1118
          GETCHAR(cflag, p);
1119
          if (cflag && !ao->cflag) {
1120
            IPCPDEBUG((LOG_INFO, "ipcp_reqci: Naking VJ cflag %d\n", cflag));
1121
            orc = CONFNAK;
1122
            if (!reject_if_disagree) {
1123
              DECPTR(1, p);
1124
              PUTCHAR(wo->cflag, p);
1125
            }
1126
          }
1127
          ho->maxslotindex = maxslotindex;
1128
          ho->cflag = cflag;
1129
        } else {
1130
          ho->old_vj = 1;
1131
          ho->maxslotindex = MAX_SLOTS - 1;
1132
          ho->cflag = 1;
1133
        }
1134
        IPCPDEBUG((LOG_INFO,
1135
              "ipcp_reqci: received COMPRESSTYPE p=%d old=%d maxslot=%d cflag=%d\n",
1136
              ho->vj_protocol, ho->old_vj, ho->maxslotindex, ho->cflag));
1137
        break;
1138
 
1139
      default:
1140
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting unknown CI type %d\n", citype));
1141
        orc = CONFREJ;
1142
        break;
1143
    }
1144
 
1145
endswitch:
1146
    if (orc == CONFACK &&    /* Good CI */
1147
        rc != CONFACK) {     /*  but prior CI wasnt? */
1148
      continue;              /* Don't send this one */
1149
    }
1150
 
1151
    if (orc == CONFNAK) {    /* Nak this CI? */
1152
      if (reject_if_disagree) {  /* Getting fed up with sending NAKs? */
1153
        IPCPDEBUG((LOG_INFO, "ipcp_reqci: Rejecting too many naks\n"));
1154
        orc = CONFREJ;       /* Get tough if so */
1155
      } else {
1156
        if (rc == CONFREJ) { /* Rejecting prior CI? */
1157
          continue;          /* Don't send this one */
1158
        }
1159
        if (rc == CONFACK) { /* Ack'd all prior CIs? */
1160
          rc = CONFNAK;      /* Not anymore... */
1161
          ucp = inp;         /* Backup */
1162
        }
1163
      }
1164
    }
1165
 
1166
    if (orc == CONFREJ &&    /* Reject this CI */
1167
        rc != CONFREJ) {  /*  but no prior ones? */
1168
      rc = CONFREJ;
1169
      ucp = inp;        /* Backup */
1170
    }
1171
 
1172
    /* Need to move CI? */
1173
    if (ucp != cip) {
1174
      BCOPY(cip, ucp, cilen);  /* Move it */
1175
    }
1176
 
1177
    /* Update output pointer */
1178
    INCPTR(cilen, ucp);
1179
  }
1180
 
1181
  /*
1182
   * If we aren't rejecting this packet, and we want to negotiate
1183
   * their address, and they didn't send their address, then we
1184
   * send a NAK with a CI_ADDR option appended.  We assume the
1185
   * input buffer is long enough that we can append the extra
1186
   * option safely.
1187
   */
1188
  if (rc != CONFREJ && !ho->neg_addr &&
1189
      wo->req_addr && !reject_if_disagree) {
1190
    IPCPDEBUG((LOG_INFO, "ipcp_reqci: Requesting peer address\n"));
1191
    if (rc == CONFACK) {
1192
      rc = CONFNAK;
1193
      ucp = inp;        /* reset pointer */
1194
      wo->req_addr = 0;    /* don't ask again */
1195
    }
1196
    PUTCHAR(CI_ADDR, ucp);
1197
    PUTCHAR(CILEN_ADDR, ucp);
1198
    tl = ntohl(wo->hisaddr);
1199
    PUTLONG(tl, ucp);
1200
  }
1201
 
1202
  *len = (int)(ucp - inp);    /* Compute output length */
1203
  IPCPDEBUG((LOG_INFO, "ipcp_reqci: returning Configure-%s\n", CODENAME(rc)));
1204
  return (rc);      /* Return final code */
1205
}
1206
 
1207
 
1208
#if 0
1209
/*
1210
 * ip_check_options - check that any IP-related options are OK,
1211
 * and assign appropriate defaults.
1212
 */
1213
static void
1214
ip_check_options(u_long localAddr)
1215
{
1216
  ipcp_options *wo = &ipcp_wantoptions[0];
1217
 
1218
  /*
1219
   * Load our default IP address but allow the remote host to give us
1220
   * a new address.
1221
   */
1222
  if (wo->ouraddr == 0 && !ppp_settings.disable_defaultip) {
1223
    wo->accept_local = 1;  /* don't insist on this default value */
1224
    wo->ouraddr = htonl(localAddr);
1225
  }
1226
}
1227
#endif
1228
 
1229
 
1230
/*
1231
 * ipcp_up - IPCP has come UP.
1232
 *
1233
 * Configure the IP network interface appropriately and bring it up.
1234
 */
1235
static void
1236
ipcp_up(fsm *f)
1237
{
1238
  u32_t mask;
1239
  ipcp_options *ho = &ipcp_hisoptions[f->unit];
1240
  ipcp_options *go = &ipcp_gotoptions[f->unit];
1241
  ipcp_options *wo = &ipcp_wantoptions[f->unit];
1242
 
1243
  np_up(f->unit, PPP_IP);
1244
  IPCPDEBUG((LOG_INFO, "ipcp: up\n"));
1245
 
1246
  /*
1247
   * We must have a non-zero IP address for both ends of the link.
1248
   */
1249
  if (!ho->neg_addr) {
1250
    ho->hisaddr = wo->hisaddr;
1251
  }
1252
 
1253
  if (ho->hisaddr == 0) {
1254
    IPCPDEBUG((LOG_ERR, "Could not determine remote IP address\n"));
1255
    ipcp_close(f->unit, "Could not determine remote IP address");
1256
    return;
1257
  }
1258
  if (go->ouraddr == 0) {
1259
    IPCPDEBUG((LOG_ERR, "Could not determine local IP address\n"));
1260
    ipcp_close(f->unit, "Could not determine local IP address");
1261
    return;
1262
  }
1263
 
1264
  if (ppp_settings.usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1265
    /*pppGotDNSAddrs(go->dnsaddr[0], go->dnsaddr[1]);*/
1266
  }
1267
 
1268
  /*
1269
   * Check that the peer is allowed to use the IP address it wants.
1270
   */
1271
  if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1272
    IPCPDEBUG((LOG_ERR, "Peer is not authorized to use remote address %s\n",
1273
        inet_ntoa(ho->hisaddr)));
1274
    ipcp_close(f->unit, "Unauthorized remote IP address");
1275
    return;
1276
  }
1277
 
1278
  /* set tcp compression */
1279
  sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1280
 
1281
  /*
1282
   * Set IP addresses and (if specified) netmask.
1283
   */
1284
  mask = GetMask(go->ouraddr);
1285
 
1286
  if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask, go->dnsaddr[0], go->dnsaddr[1])) {
1287
    IPCPDEBUG((LOG_WARNING, "sifaddr failed\n"));
1288
    ipcp_close(f->unit, "Interface configuration failed");
1289
    return;
1290
  }
1291
 
1292
  /* bring the interface up for IP */
1293
  if (!sifup(f->unit)) {
1294
    IPCPDEBUG((LOG_WARNING, "sifup failed\n"));
1295
    ipcp_close(f->unit, "Interface configuration failed");
1296
    return;
1297
  }
1298
 
1299
  sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1300
 
1301
  /* assign a default route through the interface if required */
1302
  if (ipcp_wantoptions[f->unit].default_route) {
1303
    if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr)) {
1304
      default_route_set[f->unit] = 1;
1305
    }
1306
  }
1307
 
1308
  IPCPDEBUG((LOG_NOTICE, "local  IP address %s\n", inet_ntoa(go->ouraddr)));
1309
  IPCPDEBUG((LOG_NOTICE, "remote IP address %s\n", inet_ntoa(ho->hisaddr)));
1310
  if (go->dnsaddr[0]) {
1311
    IPCPDEBUG((LOG_NOTICE, "primary   DNS address %s\n", inet_ntoa(go->dnsaddr[0])));
1312
  }
1313
  if (go->dnsaddr[1]) {
1314
    IPCPDEBUG((LOG_NOTICE, "secondary DNS address %s\n", inet_ntoa(go->dnsaddr[1])));
1315
  }
1316
}
1317
 
1318
 
1319
/*
1320
 * ipcp_down - IPCP has gone DOWN.
1321
 *
1322
 * Take the IP network interface down, clear its addresses
1323
 * and delete routes through it.
1324
 */
1325
static void
1326
ipcp_down(fsm *f)
1327
{
1328
  IPCPDEBUG((LOG_INFO, "ipcp: down\n"));
1329
  np_down(f->unit, PPP_IP);
1330
  sifvjcomp(f->unit, 0, 0, 0);
1331
 
1332
  sifdown(f->unit);
1333
  ipcp_clear_addrs(f->unit);
1334
}
1335
 
1336
 
1337
/*
1338
 * ipcp_clear_addrs() - clear the interface addresses, routes, etc.
1339
 */
1340
static void
1341
ipcp_clear_addrs(int unit)
1342
{
1343
  u32_t ouraddr, hisaddr;
1344
 
1345
  ouraddr = ipcp_gotoptions[unit].ouraddr;
1346
  hisaddr = ipcp_hisoptions[unit].hisaddr;
1347
  if (default_route_set[unit]) {
1348
    cifdefaultroute(unit, ouraddr, hisaddr);
1349
    default_route_set[unit] = 0;
1350
  }
1351
  cifaddr(unit, ouraddr, hisaddr);
1352
}
1353
 
1354
 
1355
/*
1356
 * ipcp_finished - possibly shut down the lower layers.
1357
 */
1358
static void
1359
ipcp_finished(fsm *f)
1360
{
1361
  np_finished(f->unit, PPP_IP);
1362
}
1363
 
1364
#if 0
1365
static int
1366
ipcp_printpkt(u_char *p, int plen, void (*printer) (void *, char *, ...), void *arg)
1367
{
1368
  LWIP_UNUSED_ARG(p);
1369
  LWIP_UNUSED_ARG(plen);
1370
  LWIP_UNUSED_ARG(printer);
1371
  LWIP_UNUSED_ARG(arg);
1372
  return 0;
1373
}
1374
 
1375
/*
1376
 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1377
 * We don't bring the link up for IP fragments or for TCP FIN packets
1378
 * with no data.
1379
 */
1380
#define IP_HDRLEN   20  /* bytes */
1381
#define IP_OFFMASK  0x1fff
1382
#define IPPROTO_TCP 6
1383
#define TCP_HDRLEN  20
1384
#define TH_FIN      0x01
1385
 
1386
/*
1387
 * We use these macros because the IP header may be at an odd address,
1388
 * and some compilers might use word loads to get th_off or ip_hl.
1389
 */
1390
 
1391
#define net_short(x)    (((x)[0] << 8) + (x)[1])
1392
#define get_iphl(x)     (((unsigned char *)(x))[0] & 0xF)
1393
#define get_ipoff(x)    net_short((unsigned char *)(x) + 6)
1394
#define get_ipproto(x)  (((unsigned char *)(x))[9])
1395
#define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
1396
#define get_tcpflags(x) (((unsigned char *)(x))[13])
1397
 
1398
static int
1399
ip_active_pkt(u_char *pkt, int len)
1400
{
1401
  u_char *tcp;
1402
  int hlen;
1403
 
1404
  len -= PPP_HDRLEN;
1405
  pkt += PPP_HDRLEN;
1406
  if (len < IP_HDRLEN) {
1407
    return 0;
1408
  }
1409
  if ((get_ipoff(pkt) & IP_OFFMASK) != 0) {
1410
    return 0;
1411
  }
1412
  if (get_ipproto(pkt) != IPPROTO_TCP) {
1413
    return 1;
1414
  }
1415
  hlen = get_iphl(pkt) * 4;
1416
  if (len < hlen + TCP_HDRLEN) {
1417
    return 0;
1418
  }
1419
  tcp = pkt + hlen;
1420
  if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4) {
1421
    return 0;
1422
  }
1423
  return 1;
1424
}
1425
#endif
1426
 
1427
#endif /* PPP_SUPPORT */

powered by: WebSVN 2.1.0

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