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.
6 *
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.
15 *
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.
27 *
28 * $OpenBSD: ipcp.c,v 1.41 2005/07/17 19:13:24 brad Exp $
29 */
30
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>
41
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>
51
52 #ifndef NONAT
53 #ifdef LOCALNAT
54 #include "alias.h"
55 #else
56 #include <alias.h>
57 #endif
58 #endif
59
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"
98
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) == '.')
103
104 struct compreq {
105 u_short proto;
106 u_char slots;
107 u_char compcid;
108 };
109
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 *);
120
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
133 };
134
135 static const char *
protoname(int proto)136 protoname(int proto)
137 {
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 */
150 };
151 int f;
152
153 for (f = 0; f < sizeof cftypes / sizeof *cftypes; f++)
154 if (cftypes[f].id == proto)
155 return cftypes[f].txt;
156
157 return NumStr(proto, NULL, 0);
158 }
159
160 void
ipcp_AddInOctets(struct ipcp * ipcp,int n)161 ipcp_AddInOctets(struct ipcp *ipcp, int n)
162 {
163 throughput_addin(&ipcp->throughput, n);
164 }
165
166 void
ipcp_AddOutOctets(struct ipcp * ipcp,int n)167 ipcp_AddOutOctets(struct ipcp *ipcp, int n)
168 {
169 throughput_addout(&ipcp->throughput, n);
170 }
171
172 void
ipcp_LoadDNS(struct ipcp * ipcp)173 ipcp_LoadDNS(struct ipcp *ipcp)
174 {
175 int fd;
176
177 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr = INADDR_NONE;
178
179 if (ipcp->ns.resolv != NULL) {
180 free(ipcp->ns.resolv);
181 ipcp->ns.resolv = NULL;
182 }
183 if (ipcp->ns.resolv_nons != NULL) {
184 free(ipcp->ns.resolv_nons);
185 ipcp->ns.resolv_nons = NULL;
186 }
187 ipcp->ns.resolver = 0;
188
189 if ((fd = open(_PATH_RESCONF, O_RDONLY)) != -1) {
190 struct stat st;
191
192 if (fstat(fd, &st) == 0) {
193 ssize_t got;
194
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;
218
219 ipcp->ns.resolv[st.st_size] = '\0';
220 ipcp->ns.resolver = 1;
221
222 cp_nons = ipcp->ns.resolv_nons;
223 cp = ipcp->ns.resolv;
224 n = 0;
225
226 while ((ncp = strstr(cp, "nameserver")) != NULL) {
227 if (ncp != cp) {
228 memcpy(cp_nons, cp, ncp - cp);
229 cp_nons += ncp - cp;
230 }
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;
236 }
237
238 for (cp = ncp + 11; issep(*cp); cp++) /* Skip whitespace */
239 ;
240
241 for (ncp = cp; isip(*ncp); ncp++) /* Jump over IP */
242 ;
243
244 ch = *ncp;
245 *ncp = '\0';
246 if (n < 2 && inet_aton(cp, ipcp->ns.dns))
247 n++;
248 *ncp = ch;
249
250 if ((cp = strchr(ncp, '\n')) == NULL) /* Point at next line */
251 cp = ncp + strlen(ncp);
252 else
253 cp++;
254 }
255 strlcpy(cp_nons, cp, st.st_size + 1 - (cp_nons -
256 ipcp->ns.resolv_nons)); /* Copy the end - including the NUL */
257 cp_nons += strlen(cp_nons) - 1;
258 while (cp_nons >= ipcp->ns.resolv_nons && *cp_nons == '\n')
259 *cp_nons-- = '\0';
260 if (n == 2 && ipcp->ns.dns[0].s_addr == INADDR_ANY) {
261 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr;
262 ipcp->ns.dns[1].s_addr = INADDR_ANY;
263 }
264 bundle_AdjustDNS(ipcp->fsm.bundle);
265 }
266 } else
267 log_Printf(LogERROR, "Failed to stat opened %s: %s\n",
268 _PATH_RESCONF, strerror(errno));
269
270 close(fd);
271 }
272 }
273
274 int
ipcp_WriteDNS(struct ipcp * ipcp)275 ipcp_WriteDNS(struct ipcp *ipcp)
276 {
277 const char *paddr;
278 mode_t mask;
279 FILE *fp;
280
281 if (ipcp->ns.dns[0].s_addr == INADDR_ANY &&
282 ipcp->ns.dns[1].s_addr == INADDR_ANY) {
283 log_Printf(LogIPCP, "%s not modified: All nameservers NAKd\n",
284 _PATH_RESCONF);
285 return 0;
286 }
287
288 if (ipcp->ns.dns[0].s_addr == INADDR_ANY) {
289 ipcp->ns.dns[0].s_addr = ipcp->ns.dns[1].s_addr;
290 ipcp->ns.dns[1].s_addr = INADDR_ANY;
291 }
292
293 mask = umask(022);
294 if ((fp = ID0fopen(_PATH_RESCONF, "w")) != NULL) {
295 umask(mask);
296 if (ipcp->ns.resolv_nons)
297 fputs(ipcp->ns.resolv_nons, fp);
298 paddr = inet_ntoa(ipcp->ns.dns[0]);
299 log_Printf(LogIPCP, "Primary nameserver set to %s\n", paddr);
300 fprintf(fp, "\nnameserver %s\n", paddr);
301 if (ipcp->ns.dns[1].s_addr != INADDR_ANY &&
302 ipcp->ns.dns[1].s_addr != INADDR_NONE &&
303 ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr) {
304 paddr = inet_ntoa(ipcp->ns.dns[1]);
305 log_Printf(LogIPCP, "Secondary nameserver set to %s\n", paddr);
306 fprintf(fp, "nameserver %s\n", paddr);
307 }
308 if (fclose(fp) == EOF) {
309 log_Printf(LogERROR, "write(): Failed updating %s: %s\n", _PATH_RESCONF,
310 strerror(errno));
311 return 0;
312 }
313 } else
314 umask(mask);
315
316 return 1;
317 }
318
319 void
ipcp_RestoreDNS(struct ipcp * ipcp)320 ipcp_RestoreDNS(struct ipcp *ipcp)
321 {
322 if (ipcp->ns.resolver) {
323 ssize_t got;
324 size_t len;
325 int fd;
326
327 if ((fd = ID0open(_PATH_RESCONF, O_WRONLY|O_TRUNC, 0644)) != -1) {
328 len = strlen(ipcp->ns.resolv);
329 if ((got = write(fd, ipcp->ns.resolv, len)) != len) {
330 if (got == -1)
331 log_Printf(LogERROR, "Failed rewriting %s: write: %s\n",
332 _PATH_RESCONF, strerror(errno));
333 else
334 log_Printf(LogERROR, "Failed rewriting %s: wrote %lu of %lu\n",
335 _PATH_RESCONF, (unsigned long)got, (unsigned long)len);
336 }
337 close(fd);
338 } else
339 log_Printf(LogERROR, "Failed rewriting %s: open: %s\n", _PATH_RESCONF,
340 strerror(errno));
341 } else if (remove(_PATH_RESCONF) == -1)
342 log_Printf(LogERROR, "Failed removing %s: %s\n", _PATH_RESCONF,
343 strerror(errno));
344
345 }
346
347 int
ipcp_Show(struct cmdargs const * arg)348 ipcp_Show(struct cmdargs const *arg)
349 {
350 struct ipcp *ipcp = &arg->bundle->ncp.ipcp;
351
352 prompt_Printf(arg->prompt, "%s [%s]\n", ipcp->fsm.name,
353 State2Nam(ipcp->fsm.state));
354 if (ipcp->fsm.state == ST_OPENED) {
355 prompt_Printf(arg->prompt, " His side: %s, %s\n",
356 inet_ntoa(ipcp->peer_ip), vj2asc(ipcp->peer_compproto));
357 prompt_Printf(arg->prompt, " My side: %s, %s\n",
358 inet_ntoa(ipcp->my_ip), vj2asc(ipcp->my_compproto));
359 prompt_Printf(arg->prompt, " Queued packets: %lu\n",
360 (unsigned long)ipcp_QueueLen(ipcp));
361 }
362
363 prompt_Printf(arg->prompt, "\nDefaults:\n");
364 prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config"
365 " REQ%s, %u Term REQ%s\n", ipcp->cfg.fsm.timeout,
366 ipcp->cfg.fsm.maxreq, ipcp->cfg.fsm.maxreq == 1 ? "" : "s",
367 ipcp->cfg.fsm.maxtrm, ipcp->cfg.fsm.maxtrm == 1 ? "" : "s");
368 prompt_Printf(arg->prompt, " My Address: %s\n",
369 ncprange_ntoa(&ipcp->cfg.my_range));
370 if (ipcp->cfg.HaveTriggerAddress)
371 prompt_Printf(arg->prompt, " Trigger address: %s\n",
372 inet_ntoa(ipcp->cfg.TriggerAddress));
373
374 prompt_Printf(arg->prompt, " VJ compression: %s (%d slots %s slot "
375 "compression)\n", command_ShowNegval(ipcp->cfg.vj.neg),
376 ipcp->cfg.vj.slots, ipcp->cfg.vj.slotcomp ? "with" : "without");
377
378 if (iplist_isvalid(&ipcp->cfg.peer_list))
379 prompt_Printf(arg->prompt, " His Address: %s\n",
380 ipcp->cfg.peer_list.src);
381 else
382 prompt_Printf(arg->prompt, " His Address: %s\n",
383 ncprange_ntoa(&ipcp->cfg.peer_range));
384
385 prompt_Printf(arg->prompt, " DNS: %s",
386 ipcp->cfg.ns.dns[0].s_addr == INADDR_NONE ?
387 "none" : inet_ntoa(ipcp->cfg.ns.dns[0]));
388 if (ipcp->cfg.ns.dns[1].s_addr != INADDR_NONE)
389 prompt_Printf(arg->prompt, ", %s",
390 inet_ntoa(ipcp->cfg.ns.dns[1]));
391 prompt_Printf(arg->prompt, ", %s\n",
392 command_ShowNegval(ipcp->cfg.ns.dns_neg));
393 prompt_Printf(arg->prompt, " Resolver DNS: %s",
394 ipcp->ns.dns[0].s_addr == INADDR_NONE ?
395 "none" : inet_ntoa(ipcp->ns.dns[0]));
396 if (ipcp->ns.dns[1].s_addr != INADDR_NONE &&
397 ipcp->ns.dns[1].s_addr != ipcp->ns.dns[0].s_addr)
398 prompt_Printf(arg->prompt, ", %s",
399 inet_ntoa(ipcp->ns.dns[1]));
400 prompt_Printf(arg->prompt, "\n NetBIOS NS: %s, ",
401 inet_ntoa(ipcp->cfg.ns.nbns[0]));
402 prompt_Printf(arg->prompt, "%s\n\n",
403 inet_ntoa(ipcp->cfg.ns.nbns[1]));
404
405 throughput_disp(&ipcp->throughput, arg->prompt);
406
407 return 0;
408 }
409
410 int
ipcp_vjset(struct cmdargs const * arg)411 ipcp_vjset(struct cmdargs const *arg)
412 {
413 if (arg->argc != arg->argn+2)
414 return -1;
415 if (!strcasecmp(arg->argv[arg->argn], "slots")) {
416 int slots;
417
418 slots = atoi(arg->argv[arg->argn+1]);
419 if (slots < 4 || slots > 16)
420 return 1;
421 arg->bundle->ncp.ipcp.cfg.vj.slots = slots;
422 return 0;
423 } else if (!strcasecmp(arg->argv[arg->argn], "slotcomp")) {
424 if (!strcasecmp(arg->argv[arg->argn+1], "on"))
425 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 1;
426 else if (!strcasecmp(arg->argv[arg->argn+1], "off"))
427 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 0;
428 else
429 return 2;
430 return 0;
431 }
432 return -1;
433 }
434
435 void
ipcp_Init(struct ipcp * ipcp,struct bundle * bundle,struct link * l,const struct fsm_parent * parent)436 ipcp_Init(struct ipcp *ipcp, struct bundle *bundle, struct link *l,
437 const struct fsm_parent *parent)
438 {
439 struct hostent *hp;
440 struct in_addr host;
441 char name[MAXHOSTNAMELEN];
442 static const char * const timer_names[] =
443 {"IPCP restart", "IPCP openmode", "IPCP stopped"};
444
445 fsm_Init(&ipcp->fsm, "IPCP", PROTO_IPCP, 1, IPCP_MAXCODE, LogIPCP,
446 bundle, l, parent, &ipcp_Callbacks, timer_names);
447
448 ipcp->cfg.vj.slots = DEF_VJ_STATES;
449 ipcp->cfg.vj.slotcomp = 1;
450 memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range);
451
452 host.s_addr = htonl(INADDR_LOOPBACK);
453 ipcp->cfg.netmask.s_addr = INADDR_ANY;
454 if (gethostname(name, sizeof name) == 0) {
455 hp = gethostbyname(name);
456 if (hp && hp->h_addrtype == AF_INET && hp->h_length == sizeof host.s_addr)
457 memcpy(&host.s_addr, hp->h_addr, sizeof host.s_addr);
458 }
459 ncprange_setip4(&ipcp->cfg.my_range, host, ipcp->cfg.netmask);
460 ncprange_setip4(&ipcp->cfg.peer_range, ipcp->cfg.netmask, ipcp->cfg.netmask);
461
462 iplist_setsrc(&ipcp->cfg.peer_list, "");
463 ipcp->cfg.HaveTriggerAddress = 0;
464
465 ipcp->cfg.ns.dns[0].s_addr = INADDR_NONE;
466 ipcp->cfg.ns.dns[1].s_addr = INADDR_NONE;
467 ipcp->cfg.ns.dns_neg = 0;
468 ipcp->cfg.ns.nbns[0].s_addr = INADDR_ANY;
469 ipcp->cfg.ns.nbns[1].s_addr = INADDR_ANY;
470
471 ipcp->cfg.fsm.timeout = DEF_FSMRETRY;
472 ipcp->cfg.fsm.maxreq = DEF_FSMTRIES;
473 ipcp->cfg.fsm.maxtrm = DEF_FSMTRIES;
474 ipcp->cfg.vj.neg = NEG_ENABLED|NEG_ACCEPTED;
475
476 memset(&ipcp->vj, '\0', sizeof ipcp->vj);
477
478 ipcp->ns.resolv = NULL;
479 ipcp->ns.resolv_nons = NULL;
480 ipcp->ns.writable = 1;
481 ipcp_LoadDNS(ipcp);
482
483 throughput_init(&ipcp->throughput, SAMPLE_PERIOD);
484 memset(ipcp->Queue, '\0', sizeof ipcp->Queue);
485 ipcp_Setup(ipcp, INADDR_NONE);
486 }
487
488 void
ipcp_Destroy(struct ipcp * ipcp)489 ipcp_Destroy(struct ipcp *ipcp)
490 {
491 throughput_destroy(&ipcp->throughput);
492
493 if (ipcp->ns.resolv != NULL) {
494 free(ipcp->ns.resolv);
495 ipcp->ns.resolv = NULL;
496 }
497 if (ipcp->ns.resolv_nons != NULL) {
498 free(ipcp->ns.resolv_nons);
499 ipcp->ns.resolv_nons = NULL;
500 }
501 }
502
503 void
ipcp_SetLink(struct ipcp * ipcp,struct link * l)504 ipcp_SetLink(struct ipcp *ipcp, struct link *l)
505 {
506 ipcp->fsm.link = l;
507 }
508
509 void
ipcp_Setup(struct ipcp * ipcp,u_int32_t mask)510 ipcp_Setup(struct ipcp *ipcp, u_int32_t mask)
511 {
512 struct iface *iface = ipcp->fsm.bundle->iface;
513 struct ncpaddr ipaddr;
514 struct in_addr peer;
515 int pos, n;
516
517 ipcp->fsm.open_mode = 0;
518 ipcp->ifmask.s_addr = mask == INADDR_NONE ? ipcp->cfg.netmask.s_addr : mask;
519
520 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
521 /* Try to give the peer a previously configured IP address */
522 for (n = 0; n < iface->addrs; n++) {
523 if (!ncpaddr_getip4(&iface->addr[n].peer, &peer))
524 continue;
525 if ((pos = iplist_ip2pos(&ipcp->cfg.peer_list, peer)) != -1) {
526 ncpaddr_setip4(&ipaddr, iplist_setcurpos(&ipcp->cfg.peer_list, pos));
527 break;
528 }
529 }
530 if (n == iface->addrs)
531 /* Ok, so none of 'em fit.... pick a random one */
532 ncpaddr_setip4(&ipaddr, iplist_setrandpos(&ipcp->cfg.peer_list));
533
534 ncprange_sethost(&ipcp->cfg.peer_range, &ipaddr);
535 }
536
537 ipcp->heis1172 = 0;
538 ipcp->peer_req = 0;
539 ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip);
540 ipcp->peer_compproto = 0;
541
542 if (ipcp->cfg.HaveTriggerAddress) {
543 /*
544 * Some implementations of PPP require that we send a
545 * *special* value as our address, even though the rfc specifies
546 * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0").
547 */
548 ipcp->my_ip = ipcp->cfg.TriggerAddress;
549 log_Printf(LogIPCP, "Using trigger address %s\n",
550 inet_ntoa(ipcp->cfg.TriggerAddress));
551 } else {
552 /*
553 * Otherwise, if we've used an IP number before and it's still within
554 * the network specified on the ``set ifaddr'' line, we really
555 * want to keep that IP number so that we can keep any existing
556 * connections that are bound to that IP.
557 */
558 for (n = 0; n < iface->addrs; n++) {
559 ncprange_getaddr(&iface->addr[n].ifa, &ipaddr);
560 if (ncprange_contains(&ipcp->cfg.my_range, &ipaddr)) {
561 ncpaddr_getip4(&ipaddr, &ipcp->my_ip);
562 break;
563 }
564 }
565 if (n == iface->addrs)
566 ncprange_getip4addr(&ipcp->cfg.my_range, &ipcp->my_ip);
567 }
568
569 if (IsEnabled(ipcp->cfg.vj.neg)
570 #ifndef NORADIUS
571 || (ipcp->fsm.bundle->radius.valid && ipcp->fsm.bundle->radius.vj)
572 #endif
573 )
574 ipcp->my_compproto = (PROTO_VJCOMP << 16) +
575 ((ipcp->cfg.vj.slots - 1) << 8) +
576 ipcp->cfg.vj.slotcomp;
577 else
578 ipcp->my_compproto = 0;
579 sl_compress_init(&ipcp->vj.cslc, ipcp->cfg.vj.slots - 1);
580
581 ipcp->peer_reject = 0;
582 ipcp->my_reject = 0;
583
584 /* Copy startup values into ipcp->ns.dns */
585 if (ipcp->cfg.ns.dns[0].s_addr != INADDR_NONE)
586 memcpy(ipcp->ns.dns, ipcp->cfg.ns.dns, sizeof ipcp->ns.dns);
587 }
588
589 static int
numaddresses(struct in_addr mask)590 numaddresses(struct in_addr mask)
591 {
592 u_int32_t bit, haddr;
593 int n;
594
595 haddr = ntohl(mask.s_addr);
596 bit = 1;
597 n = 1;
598
599 do {
600 if (!(haddr & bit))
601 n <<= 1;
602 } while (bit <<= 1);
603
604 return n;
605 }
606
607 static int
ipcp_proxyarp(struct ipcp * ipcp,int (* proxyfun)(struct bundle *,struct in_addr,int),const struct iface_addr * addr)608 ipcp_proxyarp(struct ipcp *ipcp,
609 int (*proxyfun)(struct bundle *, struct in_addr, int),
610 const struct iface_addr *addr)
611 {
612 struct bundle *bundle = ipcp->fsm.bundle;
613 struct in_addr peer, mask, ip;
614 int n, ret, s;
615
616 if (!ncpaddr_getip4(&addr->peer, &peer)) {
617 log_Printf(LogERROR, "Oops, ipcp_proxyarp() called with unexpected addr\n");
618 return 0;
619 }
620
621 if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
622 log_Printf(LogERROR, "ipcp_proxyarp: socket: %s\n",
623 strerror(errno));
624 return 0;
625 }
626
627 ret = 0;
628
629 if (Enabled(bundle, OPT_PROXYALL)) {
630 ncprange_getip4mask(&addr->ifa, &mask);
631 if ((n = numaddresses(mask)) > 256) {
632 log_Printf(LogWARN, "%s: Too many addresses for proxyall\n",
633 ncprange_ntoa(&addr->ifa));
634 return 0;
635 }
636 ip.s_addr = peer.s_addr & mask.s_addr;
637 if (n >= 4) {
638 ip.s_addr = htonl(ntohl(ip.s_addr) + 1);
639 n -= 2;
640 }
641 while (n) {
642 if (!((ip.s_addr ^ peer.s_addr) & mask.s_addr)) {
643 if (!(ret = (*proxyfun)(bundle, ip, s)))
644 break;
645 n--;
646 }
647 ip.s_addr = htonl(ntohl(ip.s_addr) + 1);
648 }
649 ret = !n;
650 } else if (Enabled(bundle, OPT_PROXY))
651 ret = (*proxyfun)(bundle, peer, s);
652
653 close(s);
654
655 return ret;
656 }
657
658 static int
ipcp_SetIPaddress(struct ipcp * ipcp,struct in_addr myaddr,struct in_addr hisaddr)659 ipcp_SetIPaddress(struct ipcp *ipcp, struct in_addr myaddr,
660 struct in_addr hisaddr)
661 {
662 struct bundle *bundle = ipcp->fsm.bundle;
663 struct ncpaddr myncpaddr, hisncpaddr;
664 struct ncprange myrange;
665 struct in_addr mask;
666 struct sockaddr_storage ssdst, ssgw, ssmask;
667 struct sockaddr *sadst, *sagw, *samask;
668
669 sadst = (struct sockaddr *)&ssdst;
670 sagw = (struct sockaddr *)&ssgw;
671 samask = (struct sockaddr *)&ssmask;
672
673 ncpaddr_setip4(&hisncpaddr, hisaddr);
674 ncpaddr_setip4(&myncpaddr, myaddr);
675 ncprange_sethost(&myrange, &myncpaddr);
676
677 mask = addr2mask(myaddr);
678
679 if (ipcp->ifmask.s_addr != INADDR_ANY &&
680 (ipcp->ifmask.s_addr & mask.s_addr) == mask.s_addr)
681 ncprange_setip4mask(&myrange, ipcp->ifmask);
682
683 if (!iface_Add(bundle->iface, &bundle->ncp, &myrange, &hisncpaddr,
684 IFACE_ADD_FIRST|IFACE_FORCE_ADD|IFACE_SYSTEM))
685 return 0;
686
687 if (!Enabled(bundle, OPT_IFACEALIAS))
688 iface_Clear(bundle->iface, &bundle->ncp, AF_INET,
689 IFACE_CLEAR_ALIASES|IFACE_SYSTEM);
690
691 if (bundle->ncp.cfg.sendpipe > 0 || bundle->ncp.cfg.recvpipe > 0) {
692 ncprange_getsa(&myrange, &ssgw, &ssmask);
693 ncpaddr_getsa(&hisncpaddr, &ssdst);
694 rt_Update(bundle, sadst, sagw, samask);
695 }
696
697 if (Enabled(bundle, OPT_SROUTES))
698 route_Change(bundle, bundle->ncp.route, &myncpaddr, &hisncpaddr);
699
700 #ifndef NORADIUS
701 if (bundle->radius.valid)
702 route_Change(bundle, bundle->radius.routes, &myncpaddr, &hisncpaddr);
703 #endif
704
705 return 1; /* Ok */
706 }
707
708 static struct in_addr
ChooseHisAddr(struct bundle * bundle,struct in_addr gw)709 ChooseHisAddr(struct bundle *bundle, struct in_addr gw)
710 {
711 struct in_addr try;
712 u_long f;
713
714 for (f = 0; f < bundle->ncp.ipcp.cfg.peer_list.nItems; f++) {
715 try = iplist_next(&bundle->ncp.ipcp.cfg.peer_list);
716 log_Printf(LogDEBUG, "ChooseHisAddr: Check item %ld (%s)\n",
717 f, inet_ntoa(try));
718 if (ipcp_SetIPaddress(&bundle->ncp.ipcp, gw, try)) {
719 log_Printf(LogIPCP, "Selected IP address %s\n", inet_ntoa(try));
720 break;
721 }
722 }
723
724 if (f == bundle->ncp.ipcp.cfg.peer_list.nItems) {
725 log_Printf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n");
726 try.s_addr = INADDR_ANY;
727 }
728
729 return try;
730 }
731
732 static void
IpcpInitRestartCounter(struct fsm * fp,int what)733 IpcpInitRestartCounter(struct fsm *fp, int what)
734 {
735 /* Set fsm timer load */
736 struct ipcp *ipcp = fsm2ipcp(fp);
737
738 fp->FsmTimer.load = ipcp->cfg.fsm.timeout * SECTICKS;
739 switch (what) {
740 case FSM_REQ_TIMER:
741 fp->restart = ipcp->cfg.fsm.maxreq;
742 break;
743 case FSM_TRM_TIMER:
744 fp->restart = ipcp->cfg.fsm.maxtrm;
745 break;
746 default:
747 fp->restart = 1;
748 break;
749 }
750 }
751
752 static void
IpcpSendConfigReq(struct fsm * fp)753 IpcpSendConfigReq(struct fsm *fp)
754 {
755 /* Send config REQ please */
756 struct physical *p = link2physical(fp->link);
757 struct ipcp *ipcp = fsm2ipcp(fp);
758 u_char buff[MAX_FSM_OPT_LEN];
759 struct fsm_opt *o;
760
761 o = (struct fsm_opt *)buff;
762
763 if ((p && !physical_IsSync(p)) || !REJECTED(ipcp, TY_IPADDR)) {
764 memcpy(o->data, &ipcp->my_ip.s_addr, 4);
765 INC_FSM_OPT(TY_IPADDR, 6, o);
766 }
767
768 if (ipcp->my_compproto && !REJECTED(ipcp, TY_COMPPROTO)) {
769 if (ipcp->heis1172) {
770 u_int16_t proto = PROTO_VJCOMP;
771
772 ua_htons(&proto, o->data);
773 INC_FSM_OPT(TY_COMPPROTO, 4, o);
774 } else {
775 struct compreq req;
776
777 req.proto = htons(ipcp->my_compproto >> 16);
778 req.slots = (ipcp->my_compproto >> 8) & 255;
779 req.compcid = ipcp->my_compproto & 1;
780 memcpy(o->data, &req, 4);
781 INC_FSM_OPT(TY_COMPPROTO, 6, o);
782 }
783 }
784
785 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
786 if (!REJECTED(ipcp, TY_PRIMARY_DNS - TY_ADJUST_NS)) {
787 memcpy(o->data, &ipcp->ns.dns[0].s_addr, 4);
788 INC_FSM_OPT(TY_PRIMARY_DNS, 6, o);
789 }
790
791 if (!REJECTED(ipcp, TY_SECONDARY_DNS - TY_ADJUST_NS)) {
792 memcpy(o->data, &ipcp->ns.dns[1].s_addr, 4);
793 INC_FSM_OPT(TY_SECONDARY_DNS, 6, o);
794 }
795 }
796
797 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff,
798 MB_IPCPOUT);
799 }
800
801 static void
IpcpSentTerminateReq(struct fsm * fp)802 IpcpSentTerminateReq(struct fsm *fp)
803 {
804 /* Term REQ just sent by FSM */
805 }
806
807 static void
IpcpSendTerminateAck(struct fsm * fp,u_char id)808 IpcpSendTerminateAck(struct fsm *fp, u_char id)
809 {
810 /* Send Term ACK please */
811 fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_IPCPOUT);
812 }
813
814 static void
IpcpLayerStart(struct fsm * fp)815 IpcpLayerStart(struct fsm *fp)
816 {
817 /* We're about to start up ! */
818 struct ipcp *ipcp = fsm2ipcp(fp);
819
820 log_Printf(LogIPCP, "%s: LayerStart.\n", fp->link->name);
821 throughput_start(&ipcp->throughput, "IPCP throughput",
822 Enabled(fp->bundle, OPT_THROUGHPUT));
823 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
824 ipcp->peer_req = 0;
825 }
826
827 static void
IpcpLayerFinish(struct fsm * fp)828 IpcpLayerFinish(struct fsm *fp)
829 {
830 /* We're now down */
831 struct ipcp *ipcp = fsm2ipcp(fp);
832
833 log_Printf(LogIPCP, "%s: LayerFinish.\n", fp->link->name);
834 throughput_stop(&ipcp->throughput);
835 throughput_log(&ipcp->throughput, LogIPCP, NULL);
836 }
837
838 /*
839 * Called from iface_Add() via ncp_IfaceAddrAdded()
840 */
841 void
ipcp_IfaceAddrAdded(struct ipcp * ipcp,const struct iface_addr * addr)842 ipcp_IfaceAddrAdded(struct ipcp *ipcp, const struct iface_addr *addr)
843 {
844 struct bundle *bundle = ipcp->fsm.bundle;
845
846 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL))
847 ipcp_proxyarp(ipcp, arp_SetProxy, addr);
848 }
849
850 /*
851 * Called from iface_Clear() and iface_Delete() via ncp_IfaceAddrDeleted()
852 */
853 void
ipcp_IfaceAddrDeleted(struct ipcp * ipcp,const struct iface_addr * addr)854 ipcp_IfaceAddrDeleted(struct ipcp *ipcp, const struct iface_addr *addr)
855 {
856 struct bundle *bundle = ipcp->fsm.bundle;
857
858 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL))
859 ipcp_proxyarp(ipcp, arp_ClearProxy, addr);
860 }
861
862 static void
IpcpLayerDown(struct fsm * fp)863 IpcpLayerDown(struct fsm *fp)
864 {
865 /* About to come down */
866 struct ipcp *ipcp = fsm2ipcp(fp);
867 static int recursing;
868 char addr[16];
869
870 if (!recursing++) {
871 snprintf(addr, sizeof addr, "%s", inet_ntoa(ipcp->my_ip));
872 log_Printf(LogIPCP, "%s: LayerDown: %s\n", fp->link->name, addr);
873
874 #ifndef NORADIUS
875 radius_Account(&fp->bundle->radius, &fp->bundle->radacct,
876 fp->bundle->links, RAD_STOP, &ipcp->peer_ip, &ipcp->ifmask,
877 &ipcp->throughput);
878
879 if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid)
880 system_Select(fp->bundle, fp->bundle->radius.filterid, LINKDOWNFILE,
881 NULL, NULL);
882 #endif
883
884 /*
885 * XXX this stuff should really live in the FSM. Our config should
886 * associate executable sections in files with events.
887 */
888 if (system_Select(fp->bundle, addr, LINKDOWNFILE, NULL, NULL) < 0) {
889 if (bundle_GetLabel(fp->bundle)) {
890 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
891 LINKDOWNFILE, NULL, NULL) < 0)
892 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
893 } else
894 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
895 }
896
897 ipcp_Setup(ipcp, INADDR_NONE);
898 }
899 recursing--;
900 }
901
902 int
ipcp_InterfaceUp(struct ipcp * ipcp)903 ipcp_InterfaceUp(struct ipcp *ipcp)
904 {
905 if (!ipcp_SetIPaddress(ipcp, ipcp->my_ip, ipcp->peer_ip)) {
906 log_Printf(LogERROR, "ipcp_InterfaceUp: unable to set ip address\n");
907 return 0;
908 }
909
910 if (!iface_SetFlags(ipcp->fsm.bundle->iface->name, IFF_UP)) {
911 log_Printf(LogERROR, "ipcp_InterfaceUp: Can't set the IFF_UP flag on %s\n",
912 ipcp->fsm.bundle->iface->name);
913 return 0;
914 }
915
916 #ifndef NONAT
917 if (ipcp->fsm.bundle->NatEnabled)
918 PacketAliasSetAddress(ipcp->my_ip);
919 #endif
920
921 return 1;
922 }
923
924 static int
IpcpLayerUp(struct fsm * fp)925 IpcpLayerUp(struct fsm *fp)
926 {
927 /* We're now up */
928 struct ipcp *ipcp = fsm2ipcp(fp);
929 char tbuff[16];
930
931 log_Printf(LogIPCP, "%s: LayerUp.\n", fp->link->name);
932 snprintf(tbuff, sizeof tbuff, "%s", inet_ntoa(ipcp->my_ip));
933 log_Printf(LogIPCP, "myaddr %s hisaddr = %s\n",
934 tbuff, inet_ntoa(ipcp->peer_ip));
935
936 if (ipcp->peer_compproto >> 16 == PROTO_VJCOMP)
937 sl_compress_init(&ipcp->vj.cslc, (ipcp->peer_compproto >> 8) & 255);
938
939 if (!ipcp_InterfaceUp(ipcp))
940 return 0;
941
942 #ifndef NORADIUS
943 radius_Account(&fp->bundle->radius, &fp->bundle->radacct, fp->bundle->links,
944 RAD_START, &ipcp->peer_ip, &ipcp->ifmask, &ipcp->throughput);
945
946 if (fp->bundle->radius.cfg.file && fp->bundle->radius.filterid)
947 system_Select(fp->bundle, fp->bundle->radius.filterid, LINKUPFILE,
948 NULL, NULL);
949 #endif
950
951 /*
952 * XXX this stuff should really live in the FSM. Our config should
953 * associate executable sections in files with events.
954 */
955 if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) {
956 if (bundle_GetLabel(fp->bundle)) {
957 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
958 LINKUPFILE, NULL, NULL) < 0)
959 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
960 } else
961 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
962 }
963
964 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
965 log_DisplayPrompts();
966
967 return 1;
968 }
969
970 static void
ipcp_ValidateReq(struct ipcp * ipcp,struct in_addr ip,struct fsm_decode * dec)971 ipcp_ValidateReq(struct ipcp *ipcp, struct in_addr ip, struct fsm_decode *dec)
972 {
973 struct bundle *bundle = ipcp->fsm.bundle;
974 struct iface *iface = bundle->iface;
975 struct in_addr myaddr, peer;
976 int n;
977
978 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
979 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
980 if (ip.s_addr == INADDR_ANY ||
981 iplist_ip2pos(&ipcp->cfg.peer_list, ip) < 0 ||
982 !ipcp_SetIPaddress(ipcp, myaddr, ip)) {
983 log_Printf(LogIPCP, "%s: Address invalid or already in use\n",
984 inet_ntoa(ip));
985 /*
986 * If we've already had a valid address configured for the peer,
987 * try NAKing with that so that we don't have to upset things
988 * too much.
989 */
990 for (n = 0; n < iface->addrs; n++) {
991 if (!ncpaddr_getip4(&iface->addr[n].peer, &peer))
992 continue;
993 if (iplist_ip2pos(&ipcp->cfg.peer_list, peer) >= 0) {
994 ipcp->peer_ip = peer;
995 break;
996 }
997 }
998
999 if (n == iface->addrs) {
1000 /* Just pick an IP number from our list */
1001 ipcp->peer_ip = ChooseHisAddr(bundle, myaddr);
1002 }
1003
1004 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
1005 *dec->rejend++ = TY_IPADDR;
1006 *dec->rejend++ = 6;
1007 memcpy(dec->rejend, &ip.s_addr, 4);
1008 dec->rejend += 4;
1009 } else {
1010 *dec->nakend++ = TY_IPADDR;
1011 *dec->nakend++ = 6;
1012 memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4);
1013 dec->nakend += 4;
1014 }
1015 return;
1016 }
1017 } else if (ip.s_addr == INADDR_ANY ||
1018 !ncprange_containsip4(&ipcp->cfg.peer_range, ip)) {
1019 /*
1020 * If the destination address is not acceptable, NAK with what we
1021 * want to use.
1022 */
1023 *dec->nakend++ = TY_IPADDR;
1024 *dec->nakend++ = 6;
1025 for (n = 0; n < iface->addrs; n++)
1026 if (ncprange_contains(&ipcp->cfg.peer_range, &iface->addr[n].peer)) {
1027 /* We prefer the already-configured address */
1028 ncpaddr_getip4addr(&iface->addr[n].peer, (u_int32_t *)dec->nakend);
1029 break;
1030 }
1031
1032 if (n == iface->addrs)
1033 memcpy(dec->nakend, &ipcp->peer_ip.s_addr, 4);
1034
1035 dec->nakend += 4;
1036 return;
1037 }
1038
1039 ipcp->peer_ip = ip;
1040 *dec->ackend++ = TY_IPADDR;
1041 *dec->ackend++ = 6;
1042 memcpy(dec->ackend, &ip.s_addr, 4);
1043 dec->ackend += 4;
1044 }
1045
1046 static void
IpcpDecodeConfig(struct fsm * fp,u_char * cp,u_char * end,int mode_type,struct fsm_decode * dec)1047 IpcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
1048 struct fsm_decode *dec)
1049 {
1050 /* Deal with incoming PROTO_IPCP */
1051 struct ncpaddr ncpaddr;
1052 struct ipcp *ipcp = fsm2ipcp(fp);
1053 int gotdnsnak;
1054 u_int32_t compproto;
1055 struct compreq *pcomp, pcompbuff;
1056 struct in_addr ipaddr, dstipaddr, have_ip;
1057 char tbuff[100], tbuff2[100];
1058 struct fsm_opt *opt, nak;
1059
1060 gotdnsnak = 0;
1061
1062 while (end - cp >= sizeof(opt->hdr)) {
1063 if ((opt = fsm_readopt(&cp)) == NULL)
1064 break;
1065
1066 snprintf(tbuff, sizeof tbuff, " %s[%d]", protoname(opt->hdr.id),
1067 opt->hdr.len);
1068
1069 switch (opt->hdr.id) {
1070 case TY_IPADDR: /* RFC1332 */
1071 memcpy(&ipaddr.s_addr, opt->data, 4);
1072 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1073
1074 switch (mode_type) {
1075 case MODE_REQ:
1076 ipcp->peer_req = 1;
1077 ipcp_ValidateReq(ipcp, ipaddr, dec);
1078 break;
1079
1080 case MODE_NAK:
1081 if (ncprange_containsip4(&ipcp->cfg.my_range, ipaddr)) {
1082 /* Use address suggested by peer */
1083 snprintf(tbuff2, sizeof tbuff2, "%s changing address: %s ", tbuff,
1084 inet_ntoa(ipcp->my_ip));
1085 log_Printf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
1086 ipcp->my_ip = ipaddr;
1087 ncpaddr_setip4(&ncpaddr, ipcp->my_ip);
1088 bundle_AdjustFilters(fp->bundle, &ncpaddr, NULL);
1089 } else {
1090 log_Printf(log_IsKept(LogIPCP) ? LogIPCP : LogPHASE,
1091 "%s: Unacceptable address!\n", inet_ntoa(ipaddr));
1092 fsm_Close(&ipcp->fsm);
1093 }
1094 break;
1095
1096 case MODE_REJ:
1097 ipcp->peer_reject |= (1 << opt->hdr.id);
1098 break;
1099 }
1100 break;
1101
1102 case TY_COMPPROTO:
1103 pcomp = &pcompbuff;
1104 memcpy(pcomp, opt->data, sizeof(pcompbuff));
1105 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
1106 pcomp->compcid;
1107 log_Printf(LogIPCP, "%s %s\n", tbuff, vj2asc(compproto));
1108
1109 switch (mode_type) {
1110 case MODE_REQ:
1111 if (!IsAccepted(ipcp->cfg.vj.neg))
1112 fsm_rej(dec, opt);
1113 else {
1114 switch (opt->hdr.len) {
1115 case 4: /* RFC1172 */
1116 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1117 log_Printf(LogWARN, "Peer is speaking RFC1172 compression "
1118 "protocol !\n");
1119 ipcp->heis1172 = 1;
1120 ipcp->peer_compproto = compproto;
1121 fsm_ack(dec, opt);
1122 } else {
1123 pcomp->proto = htons(PROTO_VJCOMP);
1124 nak.hdr.id = TY_COMPPROTO;
1125 nak.hdr.len = 4;
1126 memcpy(nak.data, &pcomp, 2);
1127 fsm_nak(dec, &nak);
1128 }
1129 break;
1130 case 6: /* RFC1332 */
1131 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1132 if (/* pcomp->slots <= MAX_VJ_STATES
1133 && */ pcomp->slots >= MIN_VJ_STATES) {
1134 /* Ok, we can do that */
1135 ipcp->peer_compproto = compproto;
1136 ipcp->heis1172 = 0;
1137 fsm_ack(dec, opt);
1138 } else {
1139 /* Get as close as we can to what he wants */
1140 ipcp->heis1172 = 0;
1141 pcomp->slots = pcomp->slots < MIN_VJ_STATES ?
1142 MIN_VJ_STATES : MAX_VJ_STATES;
1143 nak.hdr.id = TY_COMPPROTO;
1144 nak.hdr.len = 4;
1145 memcpy(nak.data, &pcomp, 2);
1146 fsm_nak(dec, &nak);
1147 }
1148 } else {
1149 /* What we really want */
1150 pcomp->proto = htons(PROTO_VJCOMP);
1151 pcomp->slots = DEF_VJ_STATES;
1152 pcomp->compcid = 1;
1153 nak.hdr.id = TY_COMPPROTO;
1154 nak.hdr.len = 6;
1155 memcpy(nak.data, &pcomp, sizeof pcomp);
1156 fsm_nak(dec, &nak);
1157 }
1158 break;
1159 default:
1160 fsm_rej(dec, opt);
1161 break;
1162 }
1163 }
1164 break;
1165
1166 case MODE_NAK:
1167 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
1168 #if 0
1169 if (pcomp->slots > MAX_VJ_STATES)
1170 pcomp->slots = MAX_VJ_STATES;
1171 else
1172 #endif
1173 if (pcomp->slots < MIN_VJ_STATES)
1174 pcomp->slots = MIN_VJ_STATES;
1175 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
1176 pcomp->compcid;
1177 } else
1178 compproto = 0;
1179 log_Printf(LogIPCP, "%s changing compproto: %08x --> %08x\n",
1180 tbuff, ipcp->my_compproto, compproto);
1181 ipcp->my_compproto = compproto;
1182 break;
1183
1184 case MODE_REJ:
1185 ipcp->peer_reject |= (1 << opt->hdr.id);
1186 break;
1187 }
1188 break;
1189
1190 case TY_IPADDRS: /* RFC1172 */
1191 memcpy(&ipaddr.s_addr, opt->data, 4);
1192 memcpy(&dstipaddr.s_addr, opt->data + 4, 4);
1193 snprintf(tbuff2, sizeof tbuff2, "%s %s,", tbuff, inet_ntoa(ipaddr));
1194 log_Printf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr));
1195
1196 switch (mode_type) {
1197 case MODE_REQ:
1198 fsm_rej(dec, opt);
1199 break;
1200
1201 case MODE_NAK:
1202 case MODE_REJ:
1203 break;
1204 }
1205 break;
1206
1207 case TY_PRIMARY_DNS: /* DNS negotiation (rfc1877) */
1208 case TY_SECONDARY_DNS:
1209 memcpy(&ipaddr.s_addr, opt->data, 4);
1210 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1211
1212 switch (mode_type) {
1213 case MODE_REQ:
1214 if (!IsAccepted(ipcp->cfg.ns.dns_neg)) {
1215 ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1216 fsm_rej(dec, opt);
1217 break;
1218 }
1219 have_ip = ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1];
1220
1221 if (opt->hdr.id == TY_PRIMARY_DNS && ipaddr.s_addr != have_ip.s_addr &&
1222 ipaddr.s_addr == ipcp->ns.dns[1].s_addr) {
1223 /* Swap 'em 'round */
1224 ipcp->ns.dns[0] = ipcp->ns.dns[1];
1225 ipcp->ns.dns[1] = have_ip;
1226 have_ip = ipcp->ns.dns[0];
1227 }
1228
1229 if (ipaddr.s_addr != have_ip.s_addr) {
1230 /*
1231 * The client has got the DNS stuff wrong (first request) so
1232 * we'll tell 'em how it is
1233 */
1234 nak.hdr.id = opt->hdr.id;
1235 nak.hdr.len = 6;
1236 memcpy(nak.data, &have_ip.s_addr, 4);
1237 fsm_nak(dec, &nak);
1238 } else {
1239 /*
1240 * Otherwise they have it right (this time) so we send a ack packet
1241 * back confirming it... end of story
1242 */
1243 fsm_ack(dec, opt);
1244 }
1245 break;
1246
1247 case MODE_NAK:
1248 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
1249 gotdnsnak = 1;
1250 memcpy(&ipcp->ns.dns[opt->hdr.id == TY_PRIMARY_DNS ? 0 : 1].s_addr,
1251 opt->data, 4);
1252 }
1253 break;
1254
1255 case MODE_REJ: /* Can't do much, stop asking */
1256 ipcp->peer_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1257 break;
1258 }
1259 break;
1260
1261 case TY_PRIMARY_NBNS: /* M$ NetBIOS nameserver hack (rfc1877) */
1262 case TY_SECONDARY_NBNS:
1263 memcpy(&ipaddr.s_addr, opt->data, 4);
1264 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1265
1266 switch (mode_type) {
1267 case MODE_REQ:
1268 have_ip.s_addr =
1269 ipcp->cfg.ns.nbns[opt->hdr.id == TY_PRIMARY_NBNS ? 0 : 1].s_addr;
1270
1271 if (have_ip.s_addr == INADDR_ANY) {
1272 log_Printf(LogIPCP, "NBNS REQ - rejected - nbns not set\n");
1273 ipcp->my_reject |= (1 << (opt->hdr.id - TY_ADJUST_NS));
1274 fsm_rej(dec, opt);
1275 break;
1276 }
1277
1278 if (ipaddr.s_addr != have_ip.s_addr) {
1279 nak.hdr.id = opt->hdr.id;
1280 nak.hdr.len = 6;
1281 memcpy(nak.data, &have_ip.s_addr, 4);
1282 fsm_nak(dec, &nak);
1283 } else
1284 fsm_ack(dec, opt);
1285 break;
1286
1287 case MODE_NAK:
1288 log_Printf(LogIPCP, "MS NBNS req %d - NAK??\n", opt->hdr.id);
1289 break;
1290
1291 case MODE_REJ:
1292 log_Printf(LogIPCP, "MS NBNS req %d - REJ??\n", opt->hdr.id);
1293 break;
1294 }
1295 break;
1296
1297 default:
1298 if (mode_type != MODE_NOP) {
1299 ipcp->my_reject |= (1 << opt->hdr.id);
1300 fsm_rej(dec, opt);
1301 }
1302 break;
1303 }
1304 }
1305
1306 if (gotdnsnak) {
1307 if (ipcp->ns.writable) {
1308 log_Printf(LogDEBUG, "Updating resolver\n");
1309 if (!ipcp_WriteDNS(ipcp)) {
1310 ipcp->peer_reject |= (1 << (TY_PRIMARY_DNS - TY_ADJUST_NS));
1311 ipcp->peer_reject |= (1 << (TY_SECONDARY_DNS - TY_ADJUST_NS));
1312 } else
1313 bundle_AdjustDNS(fp->bundle);
1314 } else {
1315 log_Printf(LogDEBUG, "Not updating resolver (readonly)\n");
1316 bundle_AdjustDNS(fp->bundle);
1317 }
1318 }
1319
1320 if (mode_type != MODE_NOP) {
1321 if (mode_type == MODE_REQ && !ipcp->peer_req) {
1322 if (dec->rejend == dec->rej && dec->nakend == dec->nak) {
1323 /*
1324 * Pretend the peer has requested an IP.
1325 * We do this to ensure that we only send one NAK if the only
1326 * reason for the NAK is because the peer isn't sending a
1327 * TY_IPADDR REQ. This stops us from repeatedly trying to tell
1328 * the peer that we have to have an IP address on their end.
1329 */
1330 ipcp->peer_req = 1;
1331 }
1332 ipaddr.s_addr = INADDR_ANY;
1333 ipcp_ValidateReq(ipcp, ipaddr, dec);
1334 }
1335 fsm_opt_normalise(dec);
1336 }
1337 }
1338
1339 extern struct mbuf *
ipcp_Input(struct bundle * bundle,struct link * l,struct mbuf * bp)1340 ipcp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
1341 {
1342 /* Got PROTO_IPCP from link */
1343 m_settype(bp, MB_IPCPIN);
1344 if (bundle_Phase(bundle) == PHASE_NETWORK)
1345 fsm_Input(&bundle->ncp.ipcp.fsm, bp);
1346 else {
1347 if (bundle_Phase(bundle) < PHASE_NETWORK)
1348 log_Printf(LogIPCP, "%s: Error: Unexpected IPCP in phase %s (ignored)\n",
1349 l->name, bundle_PhaseName(bundle));
1350 m_freem(bp);
1351 }
1352 return NULL;
1353 }
1354
1355 int
ipcp_UseHisIPaddr(struct bundle * bundle,struct in_addr hisaddr)1356 ipcp_UseHisIPaddr(struct bundle *bundle, struct in_addr hisaddr)
1357 {
1358 struct ipcp *ipcp = &bundle->ncp.ipcp;
1359 struct in_addr myaddr;
1360
1361 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1362 iplist_reset(&ipcp->cfg.peer_list);
1363 ipcp->peer_ip = hisaddr;
1364 ncprange_setip4host(&ipcp->cfg.peer_range, hisaddr);
1365 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
1366
1367 return ipcp_SetIPaddress(ipcp, myaddr, hisaddr);
1368 }
1369
1370 int
ipcp_UseHisaddr(struct bundle * bundle,const char * hisaddr,int setaddr)1371 ipcp_UseHisaddr(struct bundle *bundle, const char *hisaddr, int setaddr)
1372 {
1373 struct in_addr myaddr;
1374 struct ncp *ncp = &bundle->ncp;
1375 struct ipcp *ipcp = &ncp->ipcp;
1376 struct ncpaddr ncpaddr;
1377
1378 /* Use `hisaddr' for the peers address (set iface if `setaddr') */
1379 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1380 iplist_reset(&ipcp->cfg.peer_list);
1381 if (strpbrk(hisaddr, ",-")) {
1382 iplist_setsrc(&ipcp->cfg.peer_list, hisaddr);
1383 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
1384 iplist_setrandpos(&ipcp->cfg.peer_list);
1385 ipcp->peer_ip = ChooseHisAddr(bundle, ipcp->my_ip);
1386 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
1387 log_Printf(LogWARN, "%s: None available !\n", ipcp->cfg.peer_list.src);
1388 return 0;
1389 }
1390 ncprange_setip4host(&ipcp->cfg.peer_range, ipcp->peer_ip);
1391 } else {
1392 log_Printf(LogWARN, "%s: Invalid range !\n", hisaddr);
1393 return 0;
1394 }
1395 } else if (ncprange_aton(&ipcp->cfg.peer_range, ncp, hisaddr) != 0) {
1396 if (ncprange_family(&ipcp->cfg.my_range) != AF_INET) {
1397 log_Printf(LogWARN, "%s: Not an AF_INET address !\n", hisaddr);
1398 return 0;
1399 }
1400 ncprange_getip4addr(&ipcp->cfg.my_range, &myaddr);
1401 ncprange_getip4addr(&ipcp->cfg.peer_range, &ipcp->peer_ip);
1402
1403 if (setaddr && !ipcp_SetIPaddress(ipcp, myaddr, ipcp->peer_ip))
1404 return 0;
1405 } else
1406 return 0;
1407
1408 ncpaddr_setip4(&ncpaddr, ipcp->peer_ip);
1409 bundle_AdjustFilters(bundle, NULL, &ncpaddr);
1410
1411 return 1; /* Ok */
1412 }
1413
1414 struct in_addr
addr2mask(struct in_addr addr)1415 addr2mask(struct in_addr addr)
1416 {
1417 u_int32_t haddr = ntohl(addr.s_addr);
1418
1419 haddr = IN_CLASSA(haddr) ? IN_CLASSA_NET :
1420 IN_CLASSB(haddr) ? IN_CLASSB_NET :
1421 IN_CLASSC_NET;
1422 addr.s_addr = htonl(haddr);
1423
1424 return addr;
1425 }
1426
1427 size_t
ipcp_QueueLen(struct ipcp * ipcp)1428 ipcp_QueueLen(struct ipcp *ipcp)
1429 {
1430 struct mqueue *q;
1431 size_t result;
1432
1433 result = 0;
1434 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
1435 result += q->len;
1436
1437 return result;
1438 }
1439
1440 int
ipcp_PushPacket(struct ipcp * ipcp,struct link * l)1441 ipcp_PushPacket(struct ipcp *ipcp, struct link *l)
1442 {
1443 struct bundle *bundle = ipcp->fsm.bundle;
1444 struct mqueue *queue;
1445 struct mbuf *bp;
1446 int m_len;
1447 u_int32_t secs = 0;
1448 unsigned alivesecs = 0;
1449
1450 if (ipcp->fsm.state != ST_OPENED)
1451 return 0;
1452
1453 /*
1454 * If ccp is not open but is required, do nothing.
1455 */
1456 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
1457 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
1458 return 0;
1459 }
1460
1461 queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1;
1462 do {
1463 if (queue->top) {
1464 bp = m_dequeue(queue);
1465 bp = mbuf_Read(bp, &secs, sizeof secs);
1466 bp = m_pullup(bp);
1467 m_len = m_length(bp);
1468 if (!FilterCheck(MBUF_CTOP(bp), AF_INET, &bundle->filter.alive,
1469 &alivesecs)) {
1470 if (secs == 0)
1471 secs = alivesecs;
1472 bundle_StartIdleTimer(bundle, secs);
1473 }
1474 link_PushPacket(l, bp, bundle, 0, PROTO_IP);
1475 ipcp_AddOutOctets(ipcp, m_len);
1476 return 1;
1477 }
1478 } while (queue-- != ipcp->Queue);
1479
1480 return 0;
1481 }
1482