Replace local array size calculations with NELEM().
[dragonfly.git] / usr.sbin / ppp / ipcp.c
blobc2aa08be3105002c2268da6e2e8f9548f655a1bd
1 /*-
2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * $FreeBSD: src/usr.sbin/ppp/ipcp.c,v 1.90.2.12 2002/09/28 10:16:25 brian Exp $
31 #include <sys/param.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <net/route.h>
39 #include <netdb.h>
40 #include <sys/un.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <resolv.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/stat.h>
49 #include <termios.h>
50 #include <unistd.h>
52 #ifndef NONAT
53 #ifdef LOCALNAT
54 #include "alias.h"
55 #else
56 #include <alias.h>
57 #endif
58 #endif
60 #include "layer.h"
61 #include "ua.h"
62 #include "defs.h"
63 #include "command.h"
64 #include "mbuf.h"
65 #include "log.h"
66 #include "timer.h"
67 #include "fsm.h"
68 #include "proto.h"
69 #include "iplist.h"
70 #include "throughput.h"
71 #include "slcompress.h"
72 #include "lqr.h"
73 #include "hdlc.h"
74 #include "lcp.h"
75 #include "ncpaddr.h"
76 #include "ip.h"
77 #include "ipcp.h"
78 #include "filter.h"
79 #include "descriptor.h"
80 #include "vjcomp.h"
81 #include "async.h"
82 #include "ccp.h"
83 #include "link.h"
84 #include "physical.h"
85 #include "mp.h"
86 #ifndef NORADIUS
87 #include "radius.h"
88 #endif
89 #include "ipv6cp.h"
90 #include "ncp.h"
91 #include "bundle.h"
92 #include "id.h"
93 #include "arp.h"
94 #include "systems.h"
95 #include "prompt.h"
96 #include "route.h"
97 #include "iface.h"
99 #undef REJECTED
100 #define REJECTED(p, x) ((p)->peer_reject & (1<<(x)))
101 #define issep(ch) ((ch) == ' ' || (ch) == '\t')
102 #define isip(ch) (((ch) >= '0' && (ch) <= '9') || (ch) == '.')
104 struct compreq {
105 u_short proto;
106 u_char slots;
107 u_char compcid;
110 static int IpcpLayerUp(struct fsm *);
111 static void IpcpLayerDown(struct fsm *);
112 static void IpcpLayerStart(struct fsm *);
113 static void IpcpLayerFinish(struct fsm *);
114 static void IpcpInitRestartCounter(struct fsm *, int);
115 static void IpcpSendConfigReq(struct fsm *);
116 static void IpcpSentTerminateReq(struct fsm *);
117 static void IpcpSendTerminateAck(struct fsm *, u_char);
118 static void IpcpDecodeConfig(struct fsm *, u_char *, u_char *, int,
119 struct fsm_decode *);
121 static struct fsm_callbacks ipcp_Callbacks = {
122 IpcpLayerUp,
123 IpcpLayerDown,
124 IpcpLayerStart,
125 IpcpLayerFinish,
126 IpcpInitRestartCounter,
127 IpcpSendConfigReq,
128 IpcpSentTerminateReq,
129 IpcpSendTerminateAck,
130 IpcpDecodeConfig,
131 fsm_NullRecvResetReq,
132 fsm_NullRecvResetAck
135 static const char *
136 protoname(int proto)
138 static struct {
139 int id;
140 const char *txt;
141 } cftypes[] = {
142 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
143 { 1, "IPADDRS" }, /* IP-Addresses */ /* deprecated */
144 { 2, "COMPPROTO" }, /* IP-Compression-Protocol */
145 { 3, "IPADDR" }, /* IP-Address */
146 { 129, "PRIDNS" }, /* 129: Primary DNS Server Address */
147 { 130, "PRINBNS" }, /* 130: Primary NBNS Server Address */
148 { 131, "SECDNS" }, /* 131: Secondary DNS Server Address */
149 { 132, "SECNBNS" } /* 132: Secondary NBNS Server Address */
151 unsigned f;
153 for (f = 0; f < NELEM(cftypes); f++)
154 if (cftypes[f].id == proto)
155 return cftypes[f].txt;
157 return NumStr(proto, NULL, 0);
160 void
161 ipcp_AddInOctets(struct ipcp *ipcp, int n)
163 throughput_addin(&ipcp->throughput, n);
166 void
167 ipcp_AddOutOctets(struct ipcp *ipcp, int n)
169 throughput_addout(&ipcp->throughput, n);
172 void
173 ipcp_LoadDNS(struct ipcp *ipcp)
175 int fd;
177 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr = INADDR_NONE;
179 if (ipcp->ns.resolv != NULL) {
180 free(ipcp->ns.resolv);
181 ipcp->ns.resolv = NULL;
183 if (ipcp->ns.resolv_nons != NULL) {
184 free(ipcp->ns.resolv_nons);
185 ipcp->ns.resolv_nons = NULL;
187 ipcp->ns.resolver = 0;
189 if ((fd = open(_PATH_RESCONF, O_RDONLY)) != -1) {
190 struct stat st;
192 if (fstat(fd, &st) == 0) {
193 ssize_t got;
195 if ((ipcp->ns.resolv_nons = (char *)malloc(st.st_size + 1)) == NULL)
196 log_Printf(LogERROR, "Failed to malloc %lu for %s: %s\n",
197 (unsigned long)st.st_size, _PATH_RESCONF, strerror(errno));
198 else if ((ipcp->ns.resolv = (char *)malloc(st.st_size + 1)) == NULL) {
199 log_Printf(LogERROR, "Failed(2) to malloc %lu for %s: %s\n",
200 (unsigned long)st.st_size, _PATH_RESCONF, strerror(errno));
201 free(ipcp->ns.resolv_nons);
202 ipcp->ns.resolv_nons = NULL;
203 } else if ((got = read(fd, ipcp->ns.resolv, st.st_size)) != st.st_size) {
204 if (got == -1)
205 log_Printf(LogERROR, "Failed to read %s: %s\n",
206 _PATH_RESCONF, strerror(errno));
207 else
208 log_Printf(LogERROR, "Failed to read %s, got %lu not %lu\n",
209 _PATH_RESCONF, (unsigned long)got,
210 (unsigned long)st.st_size);
211 free(ipcp->ns.resolv_nons);
212 ipcp->ns.resolv_nons = NULL;
213 free(ipcp->ns.resolv);
214 ipcp->ns.resolv = NULL;
215 } else {
216 char *cp, *cp_nons, *ncp, ch;
217 int n;
219 ipcp->ns.resolv[st.st_size] = '\0';
220 ipcp->ns.resolver = 1;
222 cp_nons = ipcp->ns.resolv_nons;
223 cp = ipcp->ns.resolv;
224 n = 0;
226 while ((ncp = strstr(cp, "nameserver")) != NULL) {
227 if (ncp != cp) {
228 memcpy(cp_nons, cp, ncp - cp);
229 cp_nons += ncp - cp;
231 if ((ncp != cp && ncp[-1] != '\n') || !issep(ncp[10])) {
232 memcpy(cp_nons, ncp, 9);
233 cp_nons += 9;
234 cp = ncp + 9; /* Can't match "nameserver" at cp... */
235 continue;
238 for (cp = ncp + 11; issep(*cp); cp++) /* Skip whitespace */
241 for (ncp = cp; isip(*ncp); ncp++) /* Jump over IP */
244 ch = *ncp;
245 *ncp = '\0';
246 if (n < 2 && inet_aton(cp, ipcp->ns.dns))
247 n++;
248 *ncp = ch;
250 if ((cp = strchr(ncp, '\n')) == NULL) /* Point at next line */
251 cp = ncp + strlen(ncp);
252 else
253 cp++;
255 strcpy(cp_nons, cp); /* Copy the end - including the NUL */
256 cp_nons += strlen(cp_nons) - 1;
257 while (cp_nons >= ipcp->ns.resolv_nons && *cp_nons == '\n')
258 *cp_nons-- = '\0';
259 if (n == 2 && ipcp->ns.dns[0].s_addr == INADDR_ANY) {
260 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr;
261 ipcp->ns.dns[1].s_addr = INADDR_ANY;
263 bundle_AdjustDNS(ipcp->fsm.bundle);
265 } else
266 log_Printf(LogERROR, "Failed to stat opened %s: %s\n",
267 _PATH_RESCONF, strerror(errno));
269 close(fd);
274 ipcp_WriteDNS(struct ipcp *ipcp)
276 const char *paddr;
277 mode_t mask;
278 FILE *fp;
280 if (ipcp->ns.dns[0].s_addr == INADDR_ANY &&
281 ipcp->ns.dns[1].s_addr == INADDR_ANY) {
282 log_Printf(LogIPCP, "%s not modified: All nameservers NAKd\n",
283 _PATH_RESCONF);
284 return 0;
287 if (ipcp->ns.dns[0].s_addr == INADDR_ANY) {
288 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr;
289 ipcp->ns.dns[1].s_addr = INADDR_ANY;
292 mask = umask(022);
293 if ((fp = ID0fopen(_PATH_RESCONF, "w")) != NULL) {
294 umask(mask);
295 if (ipcp->ns.resolv_nons)
296 fputs(ipcp->ns.resolv_nons, fp);
297 paddr = inet_ntoa(ipcp->ns.dns[0]);
298 log_Printf(LogIPCP, "Primary nameserver set to %s\n", paddr);
299 fprintf(fp, "\nnameserver %s\n", paddr);
300 if (ipcp->ns.dns[1].s_addr != INADDR_ANY &&
301 ipcp->ns.dns[1].s_addr != INADDR_NONE &&
302 ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr) {
303 paddr = inet_ntoa(ipcp->ns.dns[1]);
304 log_Printf(LogIPCP, "Secondary nameserver set to %s\n", paddr);
305 fprintf(fp, "nameserver %s\n", paddr);
307 if (fclose(fp) == EOF) {
308 log_Printf(LogERROR, "write(): Failed updating %s: %s\n", _PATH_RESCONF,
309 strerror(errno));
310 return 0;
312 } else
313 umask(mask);
315 return 1;
318 void
319 ipcp_RestoreDNS(struct ipcp *ipcp)
321 if (ipcp->ns.resolver) {
322 ssize_t got, len;
323 int fd;
325 if ((fd = ID0open(_PATH_RESCONF, O_WRONLY|O_TRUNC, 0644)) != -1) {
326 len = strlen(ipcp->ns.resolv);
327 if ((got = write(fd, ipcp->ns.resolv, len)) != len) {
328 if (got == -1)
329 log_Printf(LogERROR, "Failed rewriting %s: write: %s\n",
330 _PATH_RESCONF, strerror(errno));
331 else
332 log_Printf(LogERROR, "Failed rewriting %s: wrote %ld of %ld\n",
333 _PATH_RESCONF, (long)got, (long)len);
335 close(fd);
336 } else
337 log_Printf(LogERROR, "Failed rewriting %s: open: %s\n", _PATH_RESCONF,
338 strerror(errno));
339 } else if (remove(_PATH_RESCONF) == -1)
340 log_Printf(LogERROR, "Failed removing %s: %s\n", _PATH_RESCONF,
341 strerror(errno));
346 ipcp_Show(struct cmdargs const *arg)
348 struct ipcp *ipcp = &arg->bundle->ncp.ipcp;
350 prompt_Printf(arg->prompt, "%s [%s]\n", ipcp->fsm.name,
351 State2Nam(ipcp->fsm.state));
352 if (ipcp->fsm.state == ST_OPENED) {
353 prompt_Printf(arg->prompt, " His side: %s, %s\n",
354 inet_ntoa(ipcp->peer_ip), vj2asc(ipcp->peer_compproto));
355 prompt_Printf(arg->prompt, " My side: %s, %s\n",
356 inet_ntoa(ipcp->my_ip), vj2asc(ipcp->my_compproto));
357 prompt_Printf(arg->prompt, " Queued packets: %lu\n",
358 (unsigned long)ipcp_QueueLen(ipcp));
361 prompt_Printf(arg->prompt, "\nDefaults:\n");
362 prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config"
363 " REQ%s, %u Term REQ%s\n", ipcp->cfg.fsm.timeout,
364 ipcp->cfg.fsm.maxreq, ipcp->cfg.fsm.maxreq == 1 ? "" : "s",
365 ipcp->cfg.fsm.maxtrm, ipcp->cfg.fsm.maxtrm == 1 ? "" : "s");
366 prompt_Printf(arg->prompt, " My Address: %s\n",
367 ncprange_ntoa(&ipcp->cfg.my_range));
368 if (ipcp->cfg.HaveTriggerAddress)
369 prompt_Printf(arg->prompt, " Trigger address: %s\n",
370 inet_ntoa(ipcp->cfg.TriggerAddress));
372 prompt_Printf(arg->prompt, " VJ compression: %s (%d slots %s slot "
373 "compression)\n", command_ShowNegval(ipcp->cfg.vj.neg),
374 ipcp->cfg.vj.slots, ipcp->cfg.vj.slotcomp ? "with" : "without");
376 if (iplist_isvalid(&ipcp->cfg.peer_list))
377 prompt_Printf(arg->prompt, " His Address: %s\n",
378 ipcp->cfg.peer_list.src);
379 else
380 prompt_Printf(arg->prompt, " His Address: %s\n",
381 ncprange_ntoa(&ipcp->cfg.peer_range));
383 prompt_Printf(arg->prompt, " DNS: %s",
384 ipcp->cfg.ns.dns[0].s_addr == INADDR_NONE ?
385 "none" : inet_ntoa(ipcp->cfg.ns.dns[0]));
386 if (ipcp->cfg.ns.dns[1].s_addr != INADDR_NONE)
387 prompt_Printf(arg->prompt, ", %s",
388 inet_ntoa(ipcp->cfg.ns.dns[1]));
389 prompt_Printf(arg->prompt, ", %s\n",
390 command_ShowNegval(ipcp->cfg.ns.dns_neg));
391 prompt_Printf(arg->prompt, " Resolver DNS: %s",
392 ipcp->ns.dns[0].s_addr == INADDR_NONE ?
393 "none" : inet_ntoa(ipcp->ns.dns[0]));
394 if (ipcp->ns.dns[1].s_addr != INADDR_NONE &&
395 ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr)
396 prompt_Printf(arg->prompt, ", %s",
397 inet_ntoa(ipcp->ns.dns[1]));
398 prompt_Printf(arg->prompt, "\n NetBIOS NS: %s, ",
399 inet_ntoa(ipcp->cfg.ns.nbns[0]));
400 prompt_Printf(arg->prompt, "%s\n\n",
401 inet_ntoa(ipcp->cfg.ns.nbns[1]));
403 throughput_disp(&ipcp->throughput, arg->prompt);
405 return 0;
409 ipcp_vjset(struct cmdargs const *arg)
411 if (arg->argc != arg->argn+2)
412 return -1;
413 if (!strcasecmp(arg->argv[arg->argn], "slots")) {
414 int slots;
416 slots = atoi(arg->argv[arg->argn+1]);
417 if (slots < 4 || slots > 16)
418 return 1;
419 arg->bundle->ncp.ipcp.cfg.vj.slots = slots;
420 return 0;
421 } else if (!strcasecmp(arg->argv[arg->argn], "slotcomp")) {
422 if (!strcasecmp(arg->argv[arg->argn+1], "on"))
423 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 1;
424 else if (!strcasecmp(arg->argv[arg->argn+1], "off"))
425 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 0;
426 else
427 return 2;
428 return 0;
430 return -1;
433 void
434 ipcp_Init(struct ipcp *ipcp, struct bundle *bundle, struct link *l,
435 const struct fsm_parent *parent)
437 struct hostent *hp;
438 struct in_addr host;
439 char name[MAXHOSTNAMELEN];
440 static const char * const timer_names[] =
441 {"IPCP restart", "IPCP openmode", "IPCP stopped"};
443 fsm_Init(&ipcp->fsm, "IPCP", PROTO_IPCP, 1, IPCP_MAXCODE, LogIPCP,
444 bundle, l, parent, &ipcp_Callbacks, timer_names);
446 ipcp->cfg.vj.slots = DEF_VJ_STATES;
447 ipcp->cfg.vj.slotcomp = 1;
448 memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range);
450 host.s_addr = htonl(INADDR_LOOPBACK);
451 ipcp->cfg.netmask.s_addr = INADDR_ANY;
452 if (gethostname(name, sizeof name) == 0) {
453 hp = gethostbyname(name);
454 if (hp && hp->h_addrtype == AF_INET && hp->h_length == sizeof host.s_addr)
455 memcpy(&host.s_addr, hp->h_addr, sizeof host.s_addr);
457 ncprange_setip4(&ipcp->cfg.my_range, host, ipcp->cfg.netmask);
458 ncprange_setip4(&ipcp->cfg.peer_range, ipcp->cfg.netmask, ipcp->cfg.netmask);
460 iplist_setsrc(&ipcp->cfg.peer_list, "");
461 ipcp->cfg.HaveTriggerAddress = 0;
463 ipcp->cfg.ns.dns[0].s_addr = INADDR_NONE;
464 ipcp->cfg.ns.dns[1].s_addr = INADDR_NONE;
465 ipcp->cfg.ns.dns_neg = 0;
466 ipcp->cfg.ns.nbns[0].s_addr = INADDR_ANY;
467 ipcp->cfg.ns.nbns[1].s_addr = INADDR_ANY;
469 ipcp->cfg.fsm.timeout = DEF_FSMRETRY;
470 ipcp->cfg.fsm.maxreq = DEF_FSMTRIES;
471 ipcp->cfg.fsm.maxtrm = DEF_FSMTRIES;
472 ipcp->cfg.vj.neg = NEG_ENABLED|NEG_ACCEPTED;
474 memset(&ipcp->vj, '\0', sizeof ipcp->vj);
476 ipcp->ns.resolv = NULL;
477 ipcp->ns.resolv_nons = NULL;
478 ipcp->ns.writable = 1;
479 ipcp_LoadDNS(ipcp);
481 throughput_init(&ipcp->throughput, SAMPLE_PERIOD);
482 memset(ipcp->Queue, '\0', sizeof ipcp->Queue);
483 ipcp_Setup(ipcp, INADDR_NONE);
486 void
487 ipcp_Destroy(struct ipcp *ipcp)
489 throughput_destroy(&ipcp->throughput);
491 if (ipcp->ns.resolv != NULL) {
492 free(ipcp->ns.resolv);
493 ipcp->ns.resolv = NULL;
495 if (ipcp->ns.resolv_nons != NULL) {
496 free(ipcp->ns.resolv_nons);
497 ipcp->ns.resolv_nons = NULL;
501 void
502 ipcp_SetLink(struct ipcp *ipcp, struct link *l)
504 ipcp->fsm.link = l;
507 void
508 ipcp_Setup(struct ipcp *ipcp, u_int32_t mask)
510 struct iface *iface = ipcp->fsm.bundle->iface;
511 struct ncpaddr ipaddr;
512 struct in_addr peer;
513 int pos;
514 unsigned n;
516 ipcp->fsm.open_mode = 0;
517 ipcp->ifmask.s_addr = mask == INADDR_NONE ? ipcp->cfg.netmask.s_addr : mask;
519 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
520 /* Try to give the peer a previously configured IP address */
521 for (n = 0; n < iface->addrs; n++) {
522 if (!ncpaddr_getip4(&iface->addr[n].peer, &peer))
523 continue;
524 if ((pos = iplist_ip2pos(&ipcp->cfg.peer_list, peer)) != -1) {
525 ncpaddr_setip4(&ipaddr, iplist_setcurpos(&ipcp->cfg.peer_list, pos));
526 break;
529 if (n == iface->addrs)
530 /* Ok, so none of 'em fit.... pick a random one */
531 ncpaddr_setip4(&ipaddr, iplist_setrandpos(&ipcp->cfg.peer_list));
533 ncprange_sethost(&ipcp->cfg.peer_range, &ipaddr);
536 ipcp->heis1172 = 0;
537 ipcp->peer_req = 0;
538 ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip);
539 ipcp->peer_compproto = 0;
541 if (ipcp->cfg.HaveTriggerAddress) {
543 * Some implementations of PPP require that we send a
544 * *special* value as our address, even though the rfc specifies
545 * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0").
547 ipcp->my_ip = ipcp->cfg.TriggerAddress;
548 log_Printf(LogIPCP, "Using trigger address %s\n",
549 inet_ntoa(ipcp->cfg.TriggerAddress));
550 } else {
552 * Otherwise, if we've used an IP number before and it's still within
553 * the network specified on the ``set ifaddr'' line, we really
554 * want to keep that IP number so that we can keep any existing
555 * connections that are bound to that IP.
557 for (n = 0; n < iface->addrs; n++) {
558 ncprange_getaddr(&iface->addr[n].ifa, &ipaddr);
559 if (ncprange_contains(&ipcp->cfg.my_range, &ipaddr)) {
560 ncpaddr_getip4(&ipaddr, &ipcp->my_ip);
561 break;
564 if (n == iface->addrs)
565 ncprange_getip4addr(&ipcp->cfg.my_range, &ipcp->my_ip);
568 if (IsEnabled(ipcp->cfg.vj.neg)
569 #ifndef NORADIUS
570 || (ipcp->fsm.bundle->radius.valid && ipcp->fsm.bundle->radius.vj)
571 #endif
573 ipcp->my_compproto = (PROTO_VJCOMP << 16) +
574 ((ipcp->cfg.vj.slots - 1) << 8) +
575 ipcp->cfg.vj.slotcomp;
576 else
577 ipcp->my_compproto = 0;
578 sl_compress_init(&ipcp->vj.cslc, ipcp->cfg.vj.slots - 1);
580 ipcp->peer_reject = 0;
581 ipcp->my_reject = 0;
583 /* Copy startup values into ipcp->ns.dns */
584 if (ipcp->cfg.ns.dns[0].s_addr != INADDR_NONE)
585 memcpy(ipcp->ns.dns, ipcp->cfg.ns.dns, sizeof ipcp->ns.dns);
588 static int
589 numaddresses(struct in_addr mask)
591 u_int32_t bit, haddr;
592 int n;
594 haddr = ntohl(mask.s_addr);
595 bit = 1;
596 n = 1;
598 do {
599 if (!(haddr & bit))
600 n <<= 1;
601 } while (bit <<= 1);
603 return n;
606 static int
607 ipcp_proxyarp(struct ipcp *ipcp,
608 int (*proxyfun)(struct bundle *, struct in_addr),
609 const struct iface_addr *addr)
611 struct bundle *bundle = ipcp->fsm.bundle;
612 struct in_addr peer, mask, ip;
613 int n, ret;
615 mask.s_addr = 0; /* avoid gcc warnings */
617 if (!ncpaddr_getip4(&addr->peer, &peer)) {
618 log_Printf(LogERROR, "Oops, ipcp_proxyarp() called with unexpected addr\n");
619 return 0;
622 ret = 0;
624 if (Enabled(bundle, OPT_PROXYALL)) {
625 ncprange_getip4mask(&addr->ifa, &mask);
626 if ((n = numaddresses(mask)) > 256) {
627 log_Printf(LogWARN, "%s: Too many addresses for proxyall\n",
628 ncprange_ntoa(&addr->ifa));
629 return 0;
631 ip.s_addr = peer.s_addr & mask.s_addr;
632 if (n >= 4) {
633 ip.s_addr = htonl(ntohl(ip.s_addr) + 1);
634 n -= 2;
636 while (n) {
637 if (!((ip.s_addr ^ peer.s_addr) & mask.s_addr)) {
638 if (!(ret = (*proxyfun)(bundle, ip)))
639 break;
640 n--;
642 ip.s_addr = htonl(ntohl(ip.s_addr) + 1);
644 ret = !n;
645 } else if (Enabled(bundle, OPT_PROXY))
646 ret = (*proxyfun)(bundle, peer);
648 return ret;
651 static int
652 ipcp_SetIPaddress(struct ipcp *ipcp, struct in_addr myaddr,
653 struct in_addr hisaddr)
655 struct bundle *bundle = ipcp->fsm.bundle;
656 struct ncpaddr myncpaddr, hisncpaddr;
657 struct ncprange myrange;
658 struct in_addr mask;
659 struct sockaddr_storage ssdst, ssgw, ssmask;
660 struct sockaddr *sadst, *sagw, *samask;
662 sadst = (struct sockaddr *)&ssdst;
663 sagw = (struct sockaddr *)&ssgw;
664 samask = (struct sockaddr *)&ssmask;
666 ncpaddr_setip4(&hisncpaddr, hisaddr);
667 ncpaddr_setip4(&myncpaddr, myaddr);
668 ncprange_sethost(&myrange, &myncpaddr);
670 mask = addr2mask(myaddr);
672 if (ipcp->ifmask.s_addr != INADDR_ANY &&
673 (ipcp->ifmask.s_addr & mask.s_addr) == mask.s_addr)
674 ncprange_setip4mask(&myrange, ipcp->ifmask);
676 if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &hisncpaddr,
677 IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM))
678 return 0;
680 if (!Enabled(bundle, OPT_IFACEALIAS))
681 iface_Clear(bundle->iface, &bundle->ncp, AF_INET,
682 IFACE_CLEAR_ALIASES|IFACE_SYSTEM);
684 if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) {
685 ncprange_getsa(&myrange, &ssgw, &ssmask);
686 ncpaddr_getsa(&hisncpaddr, &ssdst);
687 rt_Update(bundle, sadst, sagw, samask);
690 if (Enabled(bundle, OPT_SROUTES))
691 route_Change(bundle, bundle->ncp.route, &myncpaddr, &hisncpaddr);
693 #ifndef NORADIUS
694 if (bundle->radius.valid)
695 route_Change(bundle, bundle->radius.routes, &myncpaddr, &hisncpaddr);
696 #endif
698 return 1; /* Ok */
701 static struct in_addr
702 ChooseHisAddr(struct bundle *bundle, struct in_addr gw)
704 struct in_addr try;
705 u_long f;
707 for (f = 0; f < bundle->ncp.ipcp.cfg.peer_list.nItems; f++) {
708 try = iplist_next(&bundle->ncp.ipcp.cfg.peer_list);
709 log_Printf(LogDEBUG, "ChooseHisAddr: Check item %ld (%s)\n",
710 f, inet_ntoa(try));
711 if (ipcp_SetIPaddress(&bundle->ncp.ipcp, gw, try)) {
712 log_Printf(LogIPCP, "Selected IP address %s\n", inet_ntoa(try));
713 break;
717 if (f == bundle->ncp.ipcp.cfg.peer_list.nItems) {
718 log_Printf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n");
719 try.s_addr = INADDR_ANY;
722 return try;
725 static void
726 IpcpInitRestartCounter(struct fsm *fp, int what)
728 /* Set fsm timer load */
729 struct ipcp *ipcp = fsm2ipcp(fp);
731 fp->FsmTimer.load = ipcp->cfg.fsm.timeout * SECTICKS;
732 switch (what) {
733 case FSM_REQ_TIMER:
734 fp->restart = ipcp->cfg.fsm.maxreq;
735 break;
736 case FSM_TRM_TIMER:
737 fp->restart = ipcp->cfg.fsm.maxtrm;
738 break;
739 default:
740 fp->restart = 1;
741 break;
745 static void
746 IpcpSendConfigReq(struct fsm *fp)
748 /* Send config REQ please */
749 struct physical *p = link2physical(fp->link);
750 struct ipcp *ipcp = fsm2ipcp(fp);
751 u_char buff[24];
752 struct fsm_opt *o;
754 o = (struct fsm_opt *)buff;
756 if ((p && !physical_IsSync(p)) || !REJECTED(ipcp, TY_IPADDR)) {
757 memcpy(o->data, &ipcp->my_ip.s_addr, 4);
758 INC_FSM_OPT(TY_IPADDR, 6, o);
761 if (ipcp->my_compproto && !REJECTED(ipcp, TY_COMPPROTO)) {
762 if (ipcp->heis1172) {
763 u_int16_t proto = PROTO_VJCOMP;
765 ua_htons(&proto, o->data);
766 INC_FSM_OPT(TY_COMPPROTO, 4, o);
767 } else {
768 struct compreq req;
770 req.proto = htons(ipcp->my_compproto >> 16);
771 req.slots = (ipcp->my_compproto >> 8) & 255;
772 req.compcid = ipcp->my_compproto & 1;
773 memcpy(o->data, &req, 4);
774 INC_FSM_OPT(TY_COMPPROTO, 6, o);
778 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
779 if (!REJECTED(ipcp, TY_PRIMARY_DNS - TY_ADJUST_NS)) {
780 memcpy(o->data, &ipcp->ns.dns[0].s_addr, 4);
781 INC_FSM_OPT(TY_PRIMARY_DNS, 6, o);
784 if (!REJECTED(ipcp, TY_SECONDARY_DNS - TY_ADJUST_NS)) {
785 memcpy(o->data, &ipcp->ns.dns[1].s_addr, 4);
786 INC_FSM_OPT(TY_SECONDARY_DNS, 6, o);
790 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff,
791 MB_IPCPOUT);
794 static void
795 IpcpSentTerminateReq(struct fsm *fp __unused)
797 /* Term REQ just sent by FSM */
800 static void
801 IpcpSendTerminateAck(struct fsm *fp, u_char id)
803 /* Send Term ACK please */
804 fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPCPOUT);
807 static void
808 IpcpLayerStart(struct fsm *fp)
810 /* We're about to start up ! */
811 struct ipcp *ipcp = fsm2ipcp(fp);
813 log_Printf(LogIPCP, "%s: LayerStart.\n", fp->link->name);
814 throughput_start(&ipcp->throughput, "IPCP throughput",
815 Enabled(fp->bundle, OPT_THROUGHPUT));
816 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
817 ipcp->peer_req = 0;
820 static void
821 IpcpLayerFinish(struct fsm *fp)
823 /* We're now down */
824 struct ipcp *ipcp = fsm2ipcp(fp);
826 log_Printf(LogIPCP, "%s: LayerFinish.\n", fp->link->name);
827 throughput_stop(&ipcp->throughput);
828 throughput_log(&ipcp->throughput, LogIPCP, NULL);
832 * Called from iface_Add() via ncp_IfaceAddrAdded()
834 void
835 ipcp_IfaceAddrAdded(struct ipcp *ipcp, const struct iface_addr *addr)
837 struct bundle *bundle = ipcp->fsm.bundle;
839 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL))
840 ipcp_proxyarp(ipcp, arp_SetProxy, addr);
844 * Called from iface_Clear() and iface_Delete() via ncp_IfaceAddrDeleted()
846 void
847 ipcp_IfaceAddrDeleted(struct ipcp *ipcp, const struct iface_addr *addr)
849 struct bundle *bundle = ipcp->fsm.bundle;
851 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL))
852 ipcp_proxyarp(ipcp, arp_ClearProxy, addr);
855 static void
856 IpcpLayerDown(struct fsm *fp)
858 /* About to come down */
859 struct ipcp *ipcp = fsm2ipcp(fp);
860 static int recursing;
861 char addr[16];
863 if (!recursing++) {
864 snprintf(addr, sizeof addr, "%s", inet_ntoa(ipcp->my_ip));
865 log_Printf(LogIPCP, "%s: LayerDown: %s\n", fp->link->name, addr);
867 #ifndef NORADIUS
868 radius_Account(&fp->bundle->radius, &fp->bundle->radacct,
869 fp->bundle->links, RAD_STOP, &ipcp->peer_ip, &ipcp->ifmask,
870 &ipcp->throughput);
872 if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid)
873 system_Select(fp->bundle, fp->bundle->radius.filterid, LINKDOWNFILE,
874 NULL, NULL);
875 #endif
878 * XXX this stuff should really live in the FSM. Our config should
879 * associate executable sections in files with events.
881 if (system_Select(fp->bundle, addr, LINKDOWNFILE, NULL, NULL) < 0) {
882 if (bundle_GetLabel(fp->bundle)) {
883 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
884 LINKDOWNFILE, NULL, NULL) < 0)
885 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
886 } else
887 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
890 ipcp_Setup(ipcp, INADDR_NONE);
892 recursing--;
896 ipcp_InterfaceUp(struct ipcp *ipcp)
898 if (!ipcp_SetIPaddress(ipcp, ipcp->my_ip, ipcp->peer_ip)) {
899 log_Printf(LogERROR, "ipcp_InterfaceUp: unable to set ip address\n");
900 return 0;
903 if (!iface_SetFlags(ipcp->fsm.bundle->iface->name, IFF_UP)) {
904 log_Printf(LogERROR, "ipcp_InterfaceUp: Can't set the IFF_UP flag on %s\n",
905 ipcp->fsm.bundle->iface->name);
906 return 0;
909 #ifndef NONAT
910 if (ipcp->fsm.bundle->NatEnabled)
911 PacketAliasSetAddress(ipcp->my_ip);
912 #endif
914 return 1;
917 static int
918 IpcpLayerUp(struct fsm *fp)
920 /* We're now up */
921 struct ipcp *ipcp = fsm2ipcp(fp);
922 char tbuff[16];
924 log_Printf(LogIPCP, "%s: LayerUp.\n", fp->link->name);
925 snprintf(tbuff, sizeof tbuff, "%s", inet_ntoa(ipcp->my_ip));
926 log_Printf(LogIPCP, "myaddr %s hisaddr = %s\n",
927 tbuff, inet_ntoa(ipcp->peer_ip));
929 if (ipcp->peer_compproto >> 16 == PROTO_VJCOMP)
930 sl_compress_init(&ipcp->vj.cslc, (ipcp->peer_compproto >> 8) & 255);
932 if (!ipcp_InterfaceUp(ipcp))
933 return 0;
935 #ifndef NORADIUS
936 radius_Account(&fp->bundle->radius, &fp->bundle->radacct, fp->bundle->links,
937 RAD_START, &ipcp->peer_ip, &ipcp->ifmask, &ipcp->throughput);
939 if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid)
940 system_Select(fp->bundle, fp->bundle->radius.filterid, LINKUPFILE,
941 NULL, NULL);
942 #endif
945 * XXX this stuff should really live in the FSM. Our config should
946 * associate executable sections in files with events.
948 if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) {
949 if (bundle_GetLabel(fp->bundle)) {
950 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
951 LINKUPFILE, NULL, NULL) < 0)
952 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
953 } else
954 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
957 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
958 log_DisplayPrompts();
960 return 1;
963 static void
964 ipcp_ValidateReq(struct ipcp *ipcp, struct in_addr ip, struct fsm_decode *dec)
966 struct bundle *bundle = ipcp->fsm.bundle;
967 struct iface *iface = bundle->iface;
968 struct in_addr myaddr, peer;
969 unsigned n;
971 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
972 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
973 if (ip.s_addr == INADDR_ANY ||
974 iplist_ip2pos(&ipcp->cfg.peer_list, ip) < 0 ||
975 !ipcp_SetIPaddress(ipcp, myaddr, ip)) {
976 log_Printf(LogIPCP, "%s: Address invalid or already in use\n",
977 inet_ntoa(ip));
979 * If we've already had a valid address configured for the peer,
980 * try NAKing with that so that we don't have to upset things
981 * too much.
983 for (n = 0; n < iface->addrs; n++) {
984 if (!ncpaddr_getip4(&iface->addr[n].peer, &peer))
985 continue;
986 if (iplist_ip2pos(&ipcp->cfg.peer_list, peer) >= 0) {
987 ipcp->peer_ip = peer;
988 break;
992 if (n == iface->addrs) {
993 /* Just pick an IP number from our list */
994 ipcp->peer_ip = ChooseHisAddr(bundle, myaddr);
997 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
998 *dec->rejend++ = TY_IPADDR;
999 *dec->rejend++ = 6;
1000 memcpy(dec->rejend, &ip.s_addr, 4);
1001 dec->rejend += 4;
1002 } else {
1003 *dec->nakend++ = TY_IPADDR;
1004 *dec->nakend++ = 6;
1005 memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4);
1006 dec->nakend += 4;
1008 return;
1010 } else if (ip.s_addr == INADDR_ANY ||
1011 !ncprange_containsip4(&ipcp->cfg.peer_range, ip)) {
1013 * If the destination address is not acceptable, NAK with what we
1014 * want to use.
1016 *dec->nakend++ = TY_IPADDR;
1017 *dec->nakend++ = 6;
1018 for (n = 0; n < iface->addrs; n++)
1019 if (ncprange_contains(&ipcp->cfg.peer_range, &iface->addr[n].peer)) {
1020 /* We prefer the already-configured address */
1021 ncpaddr_getip4addr(&iface->addr[n].peer, (u_int32_t *)dec->nakend);
1022 break;
1025 if (n == iface->addrs)
1026 memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4);
1028 dec->nakend += 4;
1029 return;
1032 ipcp->peer_ip = ip;
1033 *dec->ackend++ = TY_IPADDR;
1034 *dec->ackend++ = 6;
1035 memcpy(dec->ackend, &ip.s_addr, 4);
1036 dec->ackend += 4;
1039 static void
1040 IpcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
1041 struct fsm_decode *dec)
1043 /* Deal with incoming PROTO_IPCP */
1044 struct ncpaddr ncpaddr;
1045 struct ipcp *ipcp = fsm2ipcp(fp);
1046 int gotdnsnak;
1047 u_int32_t compproto;
1048 struct compreq *pcomp;
1049 struct in_addr ipaddr, dstipaddr, have_ip;
1050 char tbuff[100], tbuff2[100];
1051 struct fsm_opt *opt, nak;
1053 gotdnsnak = 0;
1055 while (end - cp >= (int)sizeof(opt->hdr)) {
1056 if ((opt = fsm_readopt(&cp)) == NULL)
1057 break;
1059 snprintf(tbuff, sizeof tbuff, " %s[%d]", protoname(opt->hdr.id),
1060 opt->hdr.len);
1062 switch (opt->hdr.id) {
1063 case TY_IPADDR: /* RFC1332 */
1064 memcpy(&ipaddr.s_addr, opt->data, 4);
1065 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1067 switch (mode_type) {
1068 case MODE_REQ:
1069 ipcp->peer_req = 1;
1070 ipcp_ValidateReq(ipcp, ipaddr, dec);
1071 break;
1073 case MODE_NAK:
1074 if (ncprange_containsip4(&ipcp->cfg.my_range, ipaddr)) {
1075 /* Use address suggested by peer */
1076 snprintf(tbuff2, sizeof tbuff2, "%s changing address: %s ", tbuff,
1077 inet_ntoa(ipcp->my_ip));
1078 log_Printf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
1079 ipcp->my_ip = ipaddr;
1080 ncpaddr_setip4(&ncpaddr, ipcp->my_ip);
1081 bundle_AdjustFilters(fp->bundle, &ncpaddr, NULL);
1082 } else {
1083 log_Printf(log_IsKept(LogIPCP) ? LogIPCP : LogPHASE,
1084 "%s: Unacceptable address!\n", inet_ntoa(ipaddr));
1085 fsm_Close(&ipcp->fsm);
1087 break;
1089 case MODE_REJ:
1090 ipcp->peer_reject |= (1 << opt->hdr.id);
1091 break;
1093 break;
1095 case TY_COMPPROTO:
1096 pcomp = (struct compreq *)opt->data;
1097 compproto = (ntohs(pcomp->proto) << 16) + ((int)pcomp->slots << 8) +
1098 pcomp->compcid;
1099 log_Printf(LogIPCP, "%s %s\n", tbuff, vj2asc(compproto));
1101 switch (mode_type) {
1102 case MODE_REQ:
1103 if (!IsAccepted(ipcp->cfg.vj.neg))
1104 fsm_rej(dec, opt);
1105 else {
1106 switch (opt->hdr.len) {
1107 case 4: /* RFC1172 */
1108 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1109 log_Printf(LogWARN, "Peer is speaking RFC1172 compression "
1110 "protocol !\n");
1111 ipcp->heis1172 = 1;
1112 ipcp->peer_compproto = compproto;
1113 fsm_ack(dec, opt);
1114 } else {
1115 pcomp->proto = htons(PROTO_VJCOMP);
1116 nak.hdr.id = TY_COMPPROTO;
1117 nak.hdr.len = 4;
1118 memcpy(nak.data, &pcomp, 2);
1119 fsm_nak(dec, &nak);
1121 break;
1122 case 6: /* RFC1332 */
1123 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1124 /* We know pcomp->slots' max value == MAX_VJ_STATES */
1125 if (pcomp->slots >= MIN_VJ_STATES) {
1126 /* Ok, we can do that */
1127 ipcp->peer_compproto = compproto;
1128 ipcp->heis1172 = 0;
1129 fsm_ack(dec, opt);
1130 } else {
1131 /* Get as close as we can to what he wants */
1132 ipcp->heis1172 = 0;
1133 pcomp->slots = MIN_VJ_STATES;
1134 nak.hdr.id = TY_COMPPROTO;
1135 nak.hdr.len = 4;
1136 memcpy(nak.data, &pcomp, 2);
1137 fsm_nak(dec, &nak);
1139 } else {
1140 /* What we really want */
1141 pcomp->proto = htons(PROTO_VJCOMP);
1142 pcomp->slots = DEF_VJ_STATES;
1143 pcomp->compcid = 1;
1144 nak.hdr.id = TY_COMPPROTO;
1145 nak.hdr.len = 6;
1146 memcpy(nak.data, &pcomp, sizeof pcomp);
1147 fsm_nak(dec, &nak);
1149 break;
1150 default:
1151 fsm_rej(dec, opt);
1152 break;
1155 break;
1157 case MODE_NAK:
1158 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1159 /* We know pcomp->slots' max value == MAX_VJ_STATES */
1160 if (pcomp->slots < MIN_VJ_STATES)
1161 pcomp->slots = MIN_VJ_STATES;
1162 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
1163 pcomp->compcid;
1164 } else
1165 compproto = 0;
1166 log_Printf(LogIPCP, "%s changing compproto: %08x --> %08x\n",
1167 tbuff, ipcp->my_compproto, compproto);
1168 ipcp->my_compproto = compproto;
1169 break;
1171 case MODE_REJ:
1172 ipcp->peer_reject |= (1 << opt->hdr.id);
1173 break;
1175 break;
1177 case TY_IPADDRS: /* RFC1172 */
1178 memcpy(&ipaddr.s_addr, opt->data, 4);
1179 memcpy(&dstipaddr.s_addr, opt->data + 4, 4);
1180 snprintf(tbuff2, sizeof tbuff2, "%s %s,", tbuff, inet_ntoa(ipaddr));
1181 log_Printf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr));
1183 switch (mode_type) {
1184 case MODE_REQ:
1185 fsm_rej(dec, opt);
1186 break;
1188 case MODE_NAK:
1189 case MODE_REJ:
1190 break;
1192 break;
1194 case TY_PRIMARY_DNS: /* DNS negotiation (rfc1877) */
1195 case TY_SECONDARY_DNS:
1196 memcpy(&ipaddr.s_addr, opt->data, 4);
1197 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1199 switch (mode_type) {
1200 case MODE_REQ:
1201 if (!IsAccepted(ipcp->cfg.ns.dns_neg)) {
1202 ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1203 fsm_rej(dec, opt);
1204 break;
1206 have_ip = ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1];
1208 if (opt->hdr.id == TY_PRIMARY_DNS && ipaddr.s_addr != have_ip.s_addr &&
1209 ipaddr.s_addr == ipcp->ns.dns[1].s_addr) {
1210 /* Swap 'em 'round */
1211 ipcp->ns.dns[0] = ipcp->ns.dns[1];
1212 ipcp->ns.dns[1] = have_ip;
1213 have_ip = ipcp->ns.dns[0];
1216 if (ipaddr.s_addr != have_ip.s_addr) {
1218 * The client has got the DNS stuff wrong (first request) so
1219 * we'll tell 'em how it is
1221 nak.hdr.id = opt->hdr.id;
1222 nak.hdr.len = 6;
1223 memcpy(nak.data, &have_ip.s_addr, 4);
1224 fsm_nak(dec, &nak);
1225 } else {
1227 * Otherwise they have it right (this time) so we send a ack packet
1228 * back confirming it... end of story
1230 fsm_ack(dec, opt);
1232 break;
1234 case MODE_NAK:
1235 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
1236 gotdnsnak = 1;
1237 memcpy(&ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1].s_addr,
1238 opt->data, 4);
1240 break;
1242 case MODE_REJ: /* Can't do much, stop asking */
1243 ipcp->peer_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1244 break;
1246 break;
1248 case TY_PRIMARY_NBNS: /* M$ NetBIOS nameserver hack (rfc1877) */
1249 case TY_SECONDARY_NBNS:
1250 memcpy(&ipaddr.s_addr, opt->data, 4);
1251 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1253 switch (mode_type) {
1254 case MODE_REQ:
1255 have_ip.s_addr =
1256 ipcp->cfg.ns.nbns[opt->hdr.id == TY_PRIMARY_NBNS ? 0 : 1].s_addr;
1258 if (have_ip.s_addr == INADDR_ANY) {
1259 log_Printf(LogIPCP, "NBNS REQ - rejected - nbns not set\n");
1260 ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1261 fsm_rej(dec, opt);
1262 break;
1265 if (ipaddr.s_addr != have_ip.s_addr) {
1266 nak.hdr.id = opt->hdr.id;
1267 nak.hdr.len = 6;
1268 memcpy(nak.data, &have_ip.s_addr, 4);
1269 fsm_nak(dec, &nak);
1270 } else
1271 fsm_ack(dec, opt);
1272 break;
1274 case MODE_NAK:
1275 log_Printf(LogIPCP, "MS NBNS req %d - NAK??\n", opt->hdr.id);
1276 break;
1278 case MODE_REJ:
1279 log_Printf(LogIPCP, "MS NBNS req %d - REJ??\n", opt->hdr.id);
1280 break;
1282 break;
1284 default:
1285 if (mode_type != MODE_NOP) {
1286 ipcp->my_reject |= (1 << opt->hdr.id);
1287 fsm_rej(dec, opt);
1289 break;
1293 if (gotdnsnak) {
1294 if (ipcp->ns.writable) {
1295 log_Printf(LogDEBUG, "Updating resolver\n");
1296 if (!ipcp_WriteDNS(ipcp)) {
1297 ipcp->peer_reject |= (1 << (TY_PRIMARY_DNS - TY_ADJUST_NS));
1298 ipcp->peer_reject |= (1 << (TY_SECONDARY_DNS - TY_ADJUST_NS));
1299 } else
1300 bundle_AdjustDNS(fp->bundle);
1301 } else {
1302 log_Printf(LogDEBUG, "Not updating resolver (readonly)\n");
1303 bundle_AdjustDNS(fp->bundle);
1307 if (mode_type != MODE_NOP) {
1308 if (mode_type == MODE_REQ && !ipcp->peer_req) {
1309 if (dec->rejend == dec->rej && dec->nakend == dec->nak) {
1311 * Pretend the peer has requested an IP.
1312 * We do this to ensure that we only send one NAK if the only
1313 * reason for the NAK is because the peer isn't sending a
1314 * TY_IPADDR REQ. This stops us from repeatedly trying to tell
1315 * the peer that we have to have an IP address on their end.
1317 ipcp->peer_req = 1;
1319 ipaddr.s_addr = INADDR_ANY;
1320 ipcp_ValidateReq(ipcp, ipaddr, dec);
1322 fsm_opt_normalise(dec);
1326 extern struct mbuf *
1327 ipcp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
1329 /* Got PROTO_IPCP from link */
1330 m_settype(bp, MB_IPCPIN);
1331 if (bundle_Phase(bundle) == PHASE_NETWORK)
1332 fsm_Input(&bundle->ncp.ipcp.fsm, bp);
1333 else {
1334 if (bundle_Phase(bundle) < PHASE_NETWORK)
1335 log_Printf(LogIPCP, "%s: Error: Unexpected IPCP in phase %s (ignored)\n",
1336 l->name, bundle_PhaseName(bundle));
1337 m_freem(bp);
1339 return NULL;
1343 ipcp_UseHisIPaddr(struct bundle *bundle, struct in_addr hisaddr)
1345 struct ipcp *ipcp = &bundle->ncp.ipcp;
1346 struct in_addr myaddr;
1348 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1349 iplist_reset(&ipcp->cfg.peer_list);
1350 ipcp->peer_ip = hisaddr;
1351 ncprange_setip4host(&ipcp->cfg.peer_range, hisaddr);
1352 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
1354 return ipcp_SetIPaddress(ipcp, myaddr, hisaddr);
1358 ipcp_UseHisaddr(struct bundle *bundle, const char *hisaddr, int setaddr)
1360 struct in_addr myaddr;
1361 struct ncp *ncp = &bundle->ncp;
1362 struct ipcp *ipcp = &ncp->ipcp;
1363 struct ncpaddr ncpaddr;
1365 /* Use `hisaddr' for the peers address (set iface if `setaddr') */
1366 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1367 iplist_reset(&ipcp->cfg.peer_list);
1368 if (strpbrk(hisaddr, ",-")) {
1369 iplist_setsrc(&ipcp->cfg.peer_list, hisaddr);
1370 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
1371 iplist_setrandpos(&ipcp->cfg.peer_list);
1372 ipcp->peer_ip = ChooseHisAddr(bundle, ipcp->my_ip);
1373 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
1374 log_Printf(LogWARN, "%s: None available !\n", ipcp->cfg.peer_list.src);
1375 return 0;
1377 ncprange_setip4host(&ipcp->cfg.peer_range, ipcp->peer_ip);
1378 } else {
1379 log_Printf(LogWARN, "%s: Invalid range !\n", hisaddr);
1380 return 0;
1382 } else if (ncprange_aton(&ipcp->cfg.peer_range, ncp, hisaddr) != 0) {
1383 if (ncprange_family(&ipcp->cfg.my_range) != AF_INET) {
1384 log_Printf(LogWARN, "%s: Not an AF_INET address !\n", hisaddr);
1385 return 0;
1387 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
1388 ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip);
1390 if (setaddr && !ipcp_SetIPaddress(ipcp, myaddr, ipcp->peer_ip))
1391 return 0;
1392 } else
1393 return 0;
1395 ncpaddr_setip4(&ncpaddr, ipcp->peer_ip);
1396 bundle_AdjustFilters(bundle, NULL, &ncpaddr);
1398 return 1; /* Ok */
1401 struct in_addr
1402 addr2mask(struct in_addr addr)
1404 u_int32_t haddr = ntohl(addr.s_addr);
1406 haddr = IN_CLASSA(haddr) ? IN_CLASSA_NET :
1407 IN_CLASSB(haddr) ? IN_CLASSB_NET :
1408 IN_CLASSC_NET;
1409 addr.s_addr = htonl(haddr);
1411 return addr;
1414 size_t
1415 ipcp_QueueLen(struct ipcp *ipcp)
1417 struct mqueue *q;
1418 size_t result;
1420 result = 0;
1421 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
1422 result += q->len;
1424 return result;
1428 ipcp_PushPacket(struct ipcp *ipcp, struct link *l)
1430 struct bundle *bundle = ipcp->fsm.bundle;
1431 struct mqueue *queue;
1432 struct mbuf *bp;
1433 int m_len;
1434 u_int32_t secs = 0;
1435 unsigned alivesecs = 0;
1437 if (ipcp->fsm.state != ST_OPENED)
1438 return 0;
1441 * If ccp is not open but is required, do nothing.
1443 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
1444 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
1445 return 0;
1448 queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1;
1449 do {
1450 if (queue->top) {
1451 bp = m_dequeue(queue);
1452 bp = mbuf_Read(bp, &secs, sizeof secs);
1453 bp = m_pullup(bp);
1454 m_len = m_length(bp);
1455 if (!FilterCheck(MBUF_CTOP(bp), AF_INET, &bundle->filter.alive,
1456 &alivesecs)) {
1457 if (secs == 0)
1458 secs = alivesecs;
1459 bundle_StartIdleTimer(bundle, secs);
1461 link_PushPacket(l, bp, bundle, 0, PROTO_IP);
1462 ipcp_AddOutOctets(ipcp, m_len);
1463 return 1;
1465 } while (queue-- != ipcp->Queue);
1467 return 0;