Deleted Added
full compact
ipcp.c (46686) ipcp.c (46828)
1/*
2 * PPP IP Control Protocol (IPCP) Module
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
1/*
2 * PPP IP Control Protocol (IPCP) Module
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: ipcp.c,v 1.75 1999/04/26 08:54:34 brian Exp $
20 * $Id: ipcp.c,v 1.76 1999/05/08 11:06:45 brian Exp $
21 *
22 * TODO:
23 * o Support IPADDRS properly
24 * o Validate the length in IpcpDecodeConfig
25 */
26#include <sys/param.h>
27#include <netinet/in_systm.h>
28#include <netinet/in.h>
29#include <netinet/ip.h>
30#include <arpa/inet.h>
31#include <sys/socket.h>
32#include <net/route.h>
33#include <netdb.h>
34#include <sys/un.h>
35
36#include <errno.h>
37#include <fcntl.h>
38#include <resolv.h>
39#include <stdlib.h>
40#include <string.h>
41#include <termios.h>
42#include <unistd.h>
43
44#ifndef NOALIAS
45#ifdef __FreeBSD__
46#include <alias.h>
47#else
48#include "alias.h"
49#endif
50#endif
51#include "layer.h"
52#include "ua.h"
53#include "defs.h"
54#include "command.h"
55#include "mbuf.h"
56#include "log.h"
57#include "timer.h"
58#include "fsm.h"
59#include "proto.h"
60#include "lcp.h"
61#include "iplist.h"
62#include "throughput.h"
63#include "slcompress.h"
64#include "lqr.h"
65#include "hdlc.h"
66#include "ipcp.h"
67#include "filter.h"
68#include "descriptor.h"
69#include "vjcomp.h"
70#include "async.h"
71#include "ccp.h"
72#include "link.h"
73#include "physical.h"
74#include "mp.h"
75#ifndef NORADIUS
76#include "radius.h"
77#endif
78#include "bundle.h"
79#include "id.h"
80#include "arp.h"
81#include "systems.h"
82#include "prompt.h"
83#include "route.h"
84#include "iface.h"
85
86#undef REJECTED
87#define REJECTED(p, x) ((p)->peer_reject & (1<<(x)))
88#define issep(ch) ((ch) == ' ' || (ch) == '\t')
89#define isip(ch) (((ch) >= '0' && (ch) <= '9') || (ch) == '.')
90
91struct compreq {
92 u_short proto;
93 u_char slots;
94 u_char compcid;
95};
96
97static int IpcpLayerUp(struct fsm *);
98static void IpcpLayerDown(struct fsm *);
99static void IpcpLayerStart(struct fsm *);
100static void IpcpLayerFinish(struct fsm *);
101static void IpcpInitRestartCounter(struct fsm *, int);
102static void IpcpSendConfigReq(struct fsm *);
103static void IpcpSentTerminateReq(struct fsm *);
104static void IpcpSendTerminateAck(struct fsm *, u_char);
105static void IpcpDecodeConfig(struct fsm *, u_char *, int, int,
106 struct fsm_decode *);
107
108static struct fsm_callbacks ipcp_Callbacks = {
109 IpcpLayerUp,
110 IpcpLayerDown,
111 IpcpLayerStart,
112 IpcpLayerFinish,
113 IpcpInitRestartCounter,
114 IpcpSendConfigReq,
115 IpcpSentTerminateReq,
116 IpcpSendTerminateAck,
117 IpcpDecodeConfig,
118 fsm_NullRecvResetReq,
119 fsm_NullRecvResetAck
120};
121
122static const char *cftypes[] = {
123 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
124 "???",
125 "IPADDRS", /* 1: IP-Addresses */ /* deprecated */
126 "COMPPROTO", /* 2: IP-Compression-Protocol */
127 "IPADDR", /* 3: IP-Address */
128};
129
130#define NCFTYPES (sizeof cftypes/sizeof cftypes[0])
131
132static const char *cftypes128[] = {
133 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
134 "???",
135 "PRIDNS", /* 129: Primary DNS Server Address */
136 "PRINBNS", /* 130: Primary NBNS Server Address */
137 "SECDNS", /* 131: Secondary DNS Server Address */
138 "SECNBNS", /* 132: Secondary NBNS Server Address */
139};
140
141#define NCFTYPES128 (sizeof cftypes128/sizeof cftypes128[0])
142
143void
144ipcp_AddInOctets(struct ipcp *ipcp, int n)
145{
146 throughput_addin(&ipcp->throughput, n);
147}
148
149void
150ipcp_AddOutOctets(struct ipcp *ipcp, int n)
151{
152 throughput_addout(&ipcp->throughput, n);
153}
154
155static void
156getdns(struct ipcp *ipcp, struct in_addr addr[2])
157{
158 FILE *fp;
159
160 addr[0].s_addr = addr[1].s_addr = INADDR_ANY;
161 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
162 char buf[LINE_LEN], *cp, *end;
163 int n;
164
165 n = 0;
166 buf[sizeof buf - 1] = '\0';
167 while (fgets(buf, sizeof buf - 1, fp)) {
168 if (!strncmp(buf, "nameserver", 10) && issep(buf[10])) {
169 for (cp = buf + 11; issep(*cp); cp++)
170 ;
171 for (end = cp; isip(*end); end++)
172 ;
173 *end = '\0';
174 if (inet_aton(cp, addr+n) && ++n == 2)
175 break;
176 }
177 }
178 if (n == 1)
179 addr[1] = addr[0];
180 fclose(fp);
181 }
182}
183
184static int
185setdns(struct ipcp *ipcp, struct in_addr addr[2])
186{
187 FILE *fp;
188 char wbuf[LINE_LEN + 54];
189 int wlen;
190
191 if (addr[0].s_addr == INADDR_ANY || addr[1].s_addr == INADDR_ANY) {
192 struct in_addr old[2];
193
194 getdns(ipcp, old);
195 if (addr[0].s_addr == INADDR_ANY)
196 addr[0] = old[0];
197 if (addr[1].s_addr == INADDR_ANY)
198 addr[1] = old[1];
199 }
200
201 if (addr[0].s_addr == INADDR_ANY && addr[1].s_addr == INADDR_ANY) {
202 log_Printf(LogWARN, "%s not modified: All nameservers NAKd\n",
203 _PATH_RESCONF);
204 return 0;
205 }
206
207 wlen = 0;
208 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
209 char buf[LINE_LEN];
210 int len;
211
212 buf[sizeof buf - 1] = '\0';
213 while (fgets(buf, sizeof buf - 1, fp)) {
214 if (strncmp(buf, "nameserver", 10) || !issep(buf[10])) {
215 len = strlen(buf);
216 if (len > sizeof wbuf - wlen) {
217 log_Printf(LogWARN, "%s: Can only cope with max file size %d\n",
218 _PATH_RESCONF, LINE_LEN);
219 fclose(fp);
220 return 0;
221 }
222 memcpy(wbuf + wlen, buf, len);
223 wlen += len;
224 }
225 }
226 fclose(fp);
227 }
228
229 if (addr[0].s_addr != INADDR_ANY) {
230 snprintf(wbuf + wlen, sizeof wbuf - wlen, "nameserver %s\n",
231 inet_ntoa(addr[0]));
232 log_Printf(LogIPCP, "Primary nameserver set to %s", wbuf + wlen + 11);
233 wlen += strlen(wbuf + wlen);
234 }
235
236 if (addr[1].s_addr != INADDR_ANY && addr[1].s_addr != addr[0].s_addr) {
237 snprintf(wbuf + wlen, sizeof wbuf - wlen, "nameserver %s\n",
238 inet_ntoa(addr[1]));
239 log_Printf(LogIPCP, "Secondary nameserver set to %s", wbuf + wlen + 11);
240 wlen += strlen(wbuf + wlen);
241 }
242
243 if (wlen) {
244 int fd;
245
246 if ((fd = ID0open(_PATH_RESCONF, O_WRONLY|O_CREAT, 0644)) != -1) {
247 if (write(fd, wbuf, wlen) != wlen) {
248 log_Printf(LogERROR, "setdns: write(): %s\n", strerror(errno));
249 close(fd);
250 return 0;
251 }
252 if (ftruncate(fd, wlen) == -1) {
253 log_Printf(LogERROR, "setdns: truncate(): %s\n", strerror(errno));
254 close(fd);
255 return 0;
256 }
257 close(fd);
258 } else {
259 log_Printf(LogERROR, "setdns: open(): %s\n", strerror(errno));
260 return 0;
261 }
262 }
263
264 return 1;
265}
266
267int
268ipcp_Show(struct cmdargs const *arg)
269{
270 struct ipcp *ipcp = &arg->bundle->ncp.ipcp;
271
272 prompt_Printf(arg->prompt, "%s [%s]\n", ipcp->fsm.name,
273 State2Nam(ipcp->fsm.state));
274 if (ipcp->fsm.state == ST_OPENED) {
275 prompt_Printf(arg->prompt, " His side: %s, %s\n",
276 inet_ntoa(ipcp->peer_ip), vj2asc(ipcp->peer_compproto));
277 prompt_Printf(arg->prompt, " My side: %s, %s\n",
278 inet_ntoa(ipcp->my_ip), vj2asc(ipcp->my_compproto));
279 }
280
281 if (ipcp->route) {
282 prompt_Printf(arg->prompt, "\n");
283 route_ShowSticky(arg->prompt, ipcp->route, "Sticky routes", 1);
284 }
285
286 prompt_Printf(arg->prompt, "\nDefaults:\n");
287 prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config"
288 " REQ%s, %u Term REQ%s\n", ipcp->cfg.fsm.timeout,
289 ipcp->cfg.fsm.maxreq, ipcp->cfg.fsm.maxreq == 1 ? "" : "s",
290 ipcp->cfg.fsm.maxtrm, ipcp->cfg.fsm.maxtrm == 1 ? "" : "s");
291 prompt_Printf(arg->prompt, " My Address: %s/%d",
292 inet_ntoa(ipcp->cfg.my_range.ipaddr), ipcp->cfg.my_range.width);
293 prompt_Printf(arg->prompt, ", netmask %s\n", inet_ntoa(ipcp->cfg.netmask));
294 if (ipcp->cfg.HaveTriggerAddress)
295 prompt_Printf(arg->prompt, " Trigger address: %s\n",
296 inet_ntoa(ipcp->cfg.TriggerAddress));
297
298 prompt_Printf(arg->prompt, " VJ compression: %s (%d slots %s slot "
299 "compression)\n", command_ShowNegval(ipcp->cfg.vj.neg),
300 ipcp->cfg.vj.slots, ipcp->cfg.vj.slotcomp ? "with" : "without");
301
302 if (iplist_isvalid(&ipcp->cfg.peer_list))
303 prompt_Printf(arg->prompt, " His Address: %s\n",
304 ipcp->cfg.peer_list.src);
305 else
306 prompt_Printf(arg->prompt, " His Address: %s/%d\n",
307 inet_ntoa(ipcp->cfg.peer_range.ipaddr),
308 ipcp->cfg.peer_range.width);
309
310 prompt_Printf(arg->prompt, " DNS: %s, ",
311 inet_ntoa(ipcp->cfg.ns.dns[0]));
312 prompt_Printf(arg->prompt, "%s, %s\n", inet_ntoa(ipcp->cfg.ns.dns[1]),
313 command_ShowNegval(ipcp->cfg.ns.dns_neg));
314 prompt_Printf(arg->prompt, " NetBIOS NS: %s, ",
315 inet_ntoa(ipcp->cfg.ns.nbns[0]));
316 prompt_Printf(arg->prompt, "%s\n", inet_ntoa(ipcp->cfg.ns.nbns[1]));
317
318 prompt_Printf(arg->prompt, "\n");
319 throughput_disp(&ipcp->throughput, arg->prompt);
320
321 return 0;
322}
323
324int
325ipcp_vjset(struct cmdargs const *arg)
326{
327 if (arg->argc != arg->argn+2)
328 return -1;
329 if (!strcasecmp(arg->argv[arg->argn], "slots")) {
330 int slots;
331
332 slots = atoi(arg->argv[arg->argn+1]);
333 if (slots < 4 || slots > 16)
334 return 1;
335 arg->bundle->ncp.ipcp.cfg.vj.slots = slots;
336 return 0;
337 } else if (!strcasecmp(arg->argv[arg->argn], "slotcomp")) {
338 if (!strcasecmp(arg->argv[arg->argn+1], "on"))
339 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 1;
340 else if (!strcasecmp(arg->argv[arg->argn+1], "off"))
341 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 0;
342 else
343 return 2;
344 return 0;
345 }
346 return -1;
347}
348
349void
350ipcp_Init(struct ipcp *ipcp, struct bundle *bundle, struct link *l,
351 const struct fsm_parent *parent)
352{
353 struct hostent *hp;
354 char name[MAXHOSTNAMELEN];
355 static const char *timer_names[] =
356 {"IPCP restart", "IPCP openmode", "IPCP stopped"};
357
358 fsm_Init(&ipcp->fsm, "IPCP", PROTO_IPCP, 1, IPCP_MAXCODE, LogIPCP,
359 bundle, l, parent, &ipcp_Callbacks, timer_names);
360
361 ipcp->route = NULL;
362 ipcp->cfg.vj.slots = DEF_VJ_STATES;
363 ipcp->cfg.vj.slotcomp = 1;
364 memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range);
365 if (gethostname(name, sizeof name) == 0) {
366 hp = gethostbyname(name);
367 if (hp && hp->h_addrtype == AF_INET)
368 memcpy(&ipcp->cfg.my_range.ipaddr.s_addr, hp->h_addr, hp->h_length);
369 }
370 ipcp->cfg.netmask.s_addr = INADDR_ANY;
371 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
372 iplist_setsrc(&ipcp->cfg.peer_list, "");
373 ipcp->cfg.HaveTriggerAddress = 0;
374
375 ipcp->cfg.ns.dns[0].s_addr = INADDR_ANY;
376 ipcp->cfg.ns.dns[1].s_addr = INADDR_ANY;
377 ipcp->cfg.ns.dns_neg = 0;
378 ipcp->cfg.ns.nbns[0].s_addr = INADDR_ANY;
379 ipcp->cfg.ns.nbns[1].s_addr = INADDR_ANY;
380
381 ipcp->cfg.fsm.timeout = DEF_FSMRETRY;
382 ipcp->cfg.fsm.maxreq = DEF_FSMTRIES;
383 ipcp->cfg.fsm.maxtrm = DEF_FSMTRIES;
384 ipcp->cfg.vj.neg = NEG_ENABLED|NEG_ACCEPTED;
385
386 memset(&ipcp->vj, '\0', sizeof ipcp->vj);
387
388 throughput_init(&ipcp->throughput);
389 memset(ipcp->Queue, '\0', sizeof ipcp->Queue);
390 ipcp_Setup(ipcp, INADDR_NONE);
391}
392
393void
394ipcp_SetLink(struct ipcp *ipcp, struct link *l)
395{
396 ipcp->fsm.link = l;
397}
398
399void
400ipcp_Setup(struct ipcp *ipcp, u_int32_t mask)
401{
402 struct iface *iface = ipcp->fsm.bundle->iface;
403 int pos, n;
404
405 ipcp->fsm.open_mode = 0;
406 ipcp->ifmask.s_addr = mask == INADDR_NONE ? ipcp->cfg.netmask.s_addr : mask;
407
408 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
409 /* Try to give the peer a previously configured IP address */
410 for (n = 0; n < iface->in_addrs; n++) {
411 pos = iplist_ip2pos(&ipcp->cfg.peer_list, iface->in_addr[n].brd);
412 if (pos != -1) {
413 ipcp->cfg.peer_range.ipaddr =
414 iplist_setcurpos(&ipcp->cfg.peer_list, pos);
415 break;
416 }
417 }
418 if (n == iface->in_addrs)
419 /* Ok, so none of 'em fit.... pick a random one */
420 ipcp->cfg.peer_range.ipaddr = iplist_setrandpos(&ipcp->cfg.peer_list);
421
422 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
423 ipcp->cfg.peer_range.width = 32;
424 }
425
426 ipcp->heis1172 = 0;
427
428 ipcp->peer_ip = ipcp->cfg.peer_range.ipaddr;
429 ipcp->peer_compproto = 0;
430
431 if (ipcp->cfg.HaveTriggerAddress) {
432 /*
433 * Some implementations of PPP require that we send a
434 * *special* value as our address, even though the rfc specifies
435 * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0").
436 */
437 ipcp->my_ip = ipcp->cfg.TriggerAddress;
438 log_Printf(LogIPCP, "Using trigger address %s\n",
439 inet_ntoa(ipcp->cfg.TriggerAddress));
440 } else {
441 /*
442 * Otherwise, if we've used an IP number before and it's still within
443 * the network specified on the ``set ifaddr'' line, we really
444 * want to keep that IP number so that we can keep any existing
445 * connections that are bound to that IP (assuming we're not
446 * ``iface-alias''ing).
447 */
448 for (n = 0; n < iface->in_addrs; n++)
449 if ((iface->in_addr[n].ifa.s_addr & ipcp->cfg.my_range.mask.s_addr) ==
450 (ipcp->cfg.my_range.ipaddr.s_addr & ipcp->cfg.my_range.mask.s_addr)) {
451 ipcp->my_ip = iface->in_addr[n].ifa;
452 break;
453 }
454 if (n == iface->in_addrs)
455 ipcp->my_ip = ipcp->cfg.my_range.ipaddr;
456 }
457
458 if (IsEnabled(ipcp->cfg.vj.neg)
459#ifndef NORADIUS
460 || (ipcp->fsm.bundle->radius.valid && ipcp->fsm.bundle->radius.vj)
461#endif
462 )
463 ipcp->my_compproto = (PROTO_VJCOMP << 16) +
464 ((ipcp->cfg.vj.slots - 1) << 8) +
465 ipcp->cfg.vj.slotcomp;
466 else
467 ipcp->my_compproto = 0;
468 sl_compress_init(&ipcp->vj.cslc, ipcp->cfg.vj.slots - 1);
469
470 ipcp->peer_reject = 0;
471 ipcp->my_reject = 0;
472}
473
474static int
475ipcp_doproxyall(struct bundle *bundle,
476 int (*proxyfun)(struct bundle *, struct in_addr, int), int s)
477{
478 int n, ret;
479 struct sticky_route *rp;
480 struct in_addr addr;
481 struct ipcp *ipcp;
482
483 ipcp = &bundle->ncp.ipcp;
484 for (rp = ipcp->route; rp != NULL; rp = rp->next) {
485 if (rp->mask.s_addr == INADDR_BROADCAST)
486 continue;
487 n = ntohl(INADDR_BROADCAST) - ntohl(rp->mask.s_addr) - 1;
488 if (n > 0 && n <= 254 && rp->dst.s_addr != INADDR_ANY) {
489 addr = rp->dst;
490 while (n--) {
491 addr.s_addr = htonl(ntohl(addr.s_addr) + 1);
492 log_Printf(LogDEBUG, "ipcp_doproxyall: %s\n", inet_ntoa(addr));
493 ret = (*proxyfun)(bundle, addr, s);
494 if (!ret)
495 return ret;
496 }
497 }
498 }
499
500 return 0;
501}
502
503static int
504ipcp_SetIPaddress(struct bundle *bundle, struct in_addr myaddr,
505 struct in_addr hisaddr, int silent)
506{
507 static struct in_addr none = { INADDR_ANY };
508 struct in_addr mask, oaddr;
509
510 mask = addr2mask(myaddr);
511
512 if (bundle->ncp.ipcp.ifmask.s_addr != INADDR_ANY &&
513 (bundle->ncp.ipcp.ifmask.s_addr & mask.s_addr) == mask.s_addr)
514 mask.s_addr = bundle->ncp.ipcp.ifmask.s_addr;
515
516 oaddr.s_addr = bundle->iface->in_addrs ?
517 bundle->iface->in_addr[0].ifa.s_addr : INADDR_ANY;
518 if (!iface_inAdd(bundle->iface, myaddr, mask, hisaddr,
519 IFACE_ADD_FIRST|IFACE_FORCE_ADD))
520 return -1;
521
522 if (!Enabled(bundle, OPT_IFACEALIAS) && bundle->iface->in_addrs > 1
523 && myaddr.s_addr != oaddr.s_addr)
524 /* Nuke the old one */
525 iface_inDelete(bundle->iface, oaddr);
526
527 if (bundle->ncp.ipcp.cfg.sendpipe > 0 || bundle->ncp.ipcp.cfg.recvpipe > 0)
528 bundle_SetRoute(bundle, RTM_CHANGE, hisaddr, myaddr, none, 0, 0);
529
530 if (Enabled(bundle, OPT_SROUTES))
531 route_Change(bundle, bundle->ncp.ipcp.route, myaddr, hisaddr);
532
533#ifndef NORADIUS
534 if (bundle->radius.valid)
535 route_Change(bundle, bundle->radius.routes, myaddr, hisaddr);
536#endif
537
538 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL)) {
539 int s = ID0socket(AF_INET, SOCK_DGRAM, 0);
540 if (s < 0)
541 log_Printf(LogERROR, "ipcp_SetIPaddress: socket(): %s\n",
542 strerror(errno));
543 else {
544 if (Enabled(bundle, OPT_PROXYALL))
545 ipcp_doproxyall(bundle, arp_SetProxy, s);
546 else if (Enabled(bundle, OPT_PROXY))
547 arp_SetProxy(bundle, hisaddr, s);
548 close(s);
549 }
550 }
551
552 return 0;
553}
554
555static struct in_addr
556ChooseHisAddr(struct bundle *bundle, struct in_addr gw)
557{
558 struct in_addr try;
559 u_long f;
560
561 for (f = 0; f < bundle->ncp.ipcp.cfg.peer_list.nItems; f++) {
562 try = iplist_next(&bundle->ncp.ipcp.cfg.peer_list);
563 log_Printf(LogDEBUG, "ChooseHisAddr: Check item %ld (%s)\n",
564 f, inet_ntoa(try));
565 if (ipcp_SetIPaddress(bundle, gw, try, 1) == 0) {
566 log_Printf(LogIPCP, "Selected IP address %s\n", inet_ntoa(try));
567 break;
568 }
569 }
570
571 if (f == bundle->ncp.ipcp.cfg.peer_list.nItems) {
572 log_Printf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n");
573 try.s_addr = INADDR_ANY;
574 }
575
576 return try;
577}
578
579static void
580IpcpInitRestartCounter(struct fsm *fp, int what)
581{
582 /* Set fsm timer load */
583 struct ipcp *ipcp = fsm2ipcp(fp);
584
585 fp->FsmTimer.load = ipcp->cfg.fsm.timeout * SECTICKS;
586 switch (what) {
587 case FSM_REQ_TIMER:
588 fp->restart = ipcp->cfg.fsm.maxreq;
589 break;
590 case FSM_TRM_TIMER:
591 fp->restart = ipcp->cfg.fsm.maxtrm;
592 break;
593 default:
594 fp->restart = 1;
595 break;
596 }
597}
598
599static void
600IpcpSendConfigReq(struct fsm *fp)
601{
602 /* Send config REQ please */
603 struct physical *p = link2physical(fp->link);
604 struct ipcp *ipcp = fsm2ipcp(fp);
605 u_char buff[24];
606 struct lcp_opt *o;
607
608 o = (struct lcp_opt *)buff;
609
610 if ((p && !physical_IsSync(p)) || !REJECTED(ipcp, TY_IPADDR)) {
611 memcpy(o->data, &ipcp->my_ip.s_addr, 4);
612 INC_LCP_OPT(TY_IPADDR, 6, o);
613 }
614
615 if (ipcp->my_compproto && !REJECTED(ipcp, TY_COMPPROTO)) {
616 if (ipcp->heis1172) {
617 u_int16_t proto = PROTO_VJCOMP;
618
619 ua_htons(&proto, o->data);
620 INC_LCP_OPT(TY_COMPPROTO, 4, o);
621 } else {
622 struct compreq req;
623
624 req.proto = htons(ipcp->my_compproto >> 16);
625 req.slots = (ipcp->my_compproto >> 8) & 255;
626 req.compcid = ipcp->my_compproto & 1;
627 memcpy(o->data, &req, 4);
628 INC_LCP_OPT(TY_COMPPROTO, 6, o);
629 }
630 }
631
632 if (IsEnabled(ipcp->cfg.ns.dns_neg) &&
633 !REJECTED(ipcp, TY_PRIMARY_DNS - TY_ADJUST_NS) &&
634 !REJECTED(ipcp, TY_SECONDARY_DNS - TY_ADJUST_NS)) {
635 struct in_addr dns[2];
636 getdns(ipcp, dns);
637 memcpy(o->data, &dns[0].s_addr, 4);
638 INC_LCP_OPT(TY_PRIMARY_DNS, 6, o);
639 memcpy(o->data, &dns[1].s_addr, 4);
640 INC_LCP_OPT(TY_SECONDARY_DNS, 6, o);
641 }
642
643 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff);
644}
645
646static void
647IpcpSentTerminateReq(struct fsm *fp)
648{
649 /* Term REQ just sent by FSM */
650}
651
652static void
653IpcpSendTerminateAck(struct fsm *fp, u_char id)
654{
655 /* Send Term ACK please */
656 fsm_Output(fp, CODE_TERMACK, id, NULL, 0);
657}
658
659static void
660IpcpLayerStart(struct fsm *fp)
661{
662 /* We're about to start up ! */
663 struct ipcp *ipcp = fsm2ipcp(fp);
664
665 log_Printf(LogIPCP, "%s: LayerStart.\n", fp->link->name);
666 throughput_start(&ipcp->throughput, "IPCP throughput",
667 Enabled(fp->bundle, OPT_THROUGHPUT));
668 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
669}
670
671static void
672IpcpLayerFinish(struct fsm *fp)
673{
674 /* We're now down */
675 struct ipcp *ipcp = fsm2ipcp(fp);
676
677 log_Printf(LogIPCP, "%s: LayerFinish.\n", fp->link->name);
678 throughput_stop(&ipcp->throughput);
679 throughput_log(&ipcp->throughput, LogIPCP, NULL);
680}
681
682void
683ipcp_CleanInterface(struct ipcp *ipcp)
684{
685 struct iface *iface = ipcp->fsm.bundle->iface;
686
687 route_Clean(ipcp->fsm.bundle, ipcp->route);
688
689 if (iface->in_addrs && (Enabled(ipcp->fsm.bundle, OPT_PROXY) ||
690 Enabled(ipcp->fsm.bundle, OPT_PROXYALL))) {
691 int s = ID0socket(AF_INET, SOCK_DGRAM, 0);
692 if (s < 0)
693 log_Printf(LogERROR, "ipcp_CleanInterface: socket: %s\n",
694 strerror(errno));
695 else {
696 if (Enabled(ipcp->fsm.bundle, OPT_PROXYALL))
697 ipcp_doproxyall(ipcp->fsm.bundle, arp_ClearProxy, s);
698 else if (Enabled(ipcp->fsm.bundle, OPT_PROXY))
699 arp_ClearProxy(ipcp->fsm.bundle, iface->in_addr[0].brd, s);
700 close(s);
701 }
702 }
703
704 iface_inClear(ipcp->fsm.bundle->iface, IFACE_CLEAR_ALL);
705}
706
707static void
708IpcpLayerDown(struct fsm *fp)
709{
710 /* About to come down */
711 struct ipcp *ipcp = fsm2ipcp(fp);
712 const char *s;
713
714 if (ipcp->fsm.bundle->iface->in_addrs)
715 s = inet_ntoa(ipcp->fsm.bundle->iface->in_addr[0].ifa);
716 else
717 s = "Interface configuration error !";
718 log_Printf(LogIPCP, "%s: LayerDown: %s\n", fp->link->name, s);
719
720 /*
721 * XXX this stuff should really live in the FSM. Our config should
722 * associate executable sections in files with events.
723 */
724 if (system_Select(fp->bundle, s, LINKDOWNFILE, NULL, NULL) < 0) {
725 if (bundle_GetLabel(fp->bundle)) {
726 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
727 LINKDOWNFILE, NULL, NULL) < 0)
728 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
729 } else
730 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
731 }
732
733 ipcp_Setup(ipcp, INADDR_NONE);
734}
735
736int
737ipcp_InterfaceUp(struct ipcp *ipcp)
738{
739 if (ipcp_SetIPaddress(ipcp->fsm.bundle, ipcp->my_ip, ipcp->peer_ip, 0) < 0) {
740 log_Printf(LogERROR, "ipcp_InterfaceUp: unable to set ip address\n");
741 return 0;
742 }
743
744#ifndef NOALIAS
745 if (ipcp->fsm.bundle->AliasEnabled)
746 PacketAliasSetAddress(ipcp->my_ip);
747#endif
748
749 return 1;
750}
751
752static int
753IpcpLayerUp(struct fsm *fp)
754{
755 /* We're now up */
756 struct ipcp *ipcp = fsm2ipcp(fp);
757 char tbuff[16];
758
759 log_Printf(LogIPCP, "%s: LayerUp.\n", fp->link->name);
760 snprintf(tbuff, sizeof tbuff, "%s", inet_ntoa(ipcp->my_ip));
761 log_Printf(LogIPCP, "myaddr %s hisaddr = %s\n",
762 tbuff, inet_ntoa(ipcp->peer_ip));
763
764 if (ipcp->peer_compproto >> 16 == PROTO_VJCOMP)
765 sl_compress_init(&ipcp->vj.cslc, (ipcp->peer_compproto >> 8) & 255);
766
767 if (!ipcp_InterfaceUp(ipcp))
768 return 0;
769
770 /*
771 * XXX this stuff should really live in the FSM. Our config should
772 * associate executable sections in files with events.
773 */
774 if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) {
775 if (bundle_GetLabel(fp->bundle)) {
776 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
777 LINKUPFILE, NULL, NULL) < 0)
778 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
779 } else
780 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
781 }
782
783 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
784 log_DisplayPrompts();
785
786 return 1;
787}
788
789static int
790AcceptableAddr(const struct in_range *prange, struct in_addr ipaddr)
791{
792 /* Is the given IP in the given range ? */
793 return (prange->ipaddr.s_addr & prange->mask.s_addr) ==
794 (ipaddr.s_addr & prange->mask.s_addr) && ipaddr.s_addr;
795}
796
797static void
21 *
22 * TODO:
23 * o Support IPADDRS properly
24 * o Validate the length in IpcpDecodeConfig
25 */
26#include <sys/param.h>
27#include <netinet/in_systm.h>
28#include <netinet/in.h>
29#include <netinet/ip.h>
30#include <arpa/inet.h>
31#include <sys/socket.h>
32#include <net/route.h>
33#include <netdb.h>
34#include <sys/un.h>
35
36#include <errno.h>
37#include <fcntl.h>
38#include <resolv.h>
39#include <stdlib.h>
40#include <string.h>
41#include <termios.h>
42#include <unistd.h>
43
44#ifndef NOALIAS
45#ifdef __FreeBSD__
46#include <alias.h>
47#else
48#include "alias.h"
49#endif
50#endif
51#include "layer.h"
52#include "ua.h"
53#include "defs.h"
54#include "command.h"
55#include "mbuf.h"
56#include "log.h"
57#include "timer.h"
58#include "fsm.h"
59#include "proto.h"
60#include "lcp.h"
61#include "iplist.h"
62#include "throughput.h"
63#include "slcompress.h"
64#include "lqr.h"
65#include "hdlc.h"
66#include "ipcp.h"
67#include "filter.h"
68#include "descriptor.h"
69#include "vjcomp.h"
70#include "async.h"
71#include "ccp.h"
72#include "link.h"
73#include "physical.h"
74#include "mp.h"
75#ifndef NORADIUS
76#include "radius.h"
77#endif
78#include "bundle.h"
79#include "id.h"
80#include "arp.h"
81#include "systems.h"
82#include "prompt.h"
83#include "route.h"
84#include "iface.h"
85
86#undef REJECTED
87#define REJECTED(p, x) ((p)->peer_reject & (1<<(x)))
88#define issep(ch) ((ch) == ' ' || (ch) == '\t')
89#define isip(ch) (((ch) >= '0' && (ch) <= '9') || (ch) == '.')
90
91struct compreq {
92 u_short proto;
93 u_char slots;
94 u_char compcid;
95};
96
97static int IpcpLayerUp(struct fsm *);
98static void IpcpLayerDown(struct fsm *);
99static void IpcpLayerStart(struct fsm *);
100static void IpcpLayerFinish(struct fsm *);
101static void IpcpInitRestartCounter(struct fsm *, int);
102static void IpcpSendConfigReq(struct fsm *);
103static void IpcpSentTerminateReq(struct fsm *);
104static void IpcpSendTerminateAck(struct fsm *, u_char);
105static void IpcpDecodeConfig(struct fsm *, u_char *, int, int,
106 struct fsm_decode *);
107
108static struct fsm_callbacks ipcp_Callbacks = {
109 IpcpLayerUp,
110 IpcpLayerDown,
111 IpcpLayerStart,
112 IpcpLayerFinish,
113 IpcpInitRestartCounter,
114 IpcpSendConfigReq,
115 IpcpSentTerminateReq,
116 IpcpSendTerminateAck,
117 IpcpDecodeConfig,
118 fsm_NullRecvResetReq,
119 fsm_NullRecvResetAck
120};
121
122static const char *cftypes[] = {
123 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
124 "???",
125 "IPADDRS", /* 1: IP-Addresses */ /* deprecated */
126 "COMPPROTO", /* 2: IP-Compression-Protocol */
127 "IPADDR", /* 3: IP-Address */
128};
129
130#define NCFTYPES (sizeof cftypes/sizeof cftypes[0])
131
132static const char *cftypes128[] = {
133 /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
134 "???",
135 "PRIDNS", /* 129: Primary DNS Server Address */
136 "PRINBNS", /* 130: Primary NBNS Server Address */
137 "SECDNS", /* 131: Secondary DNS Server Address */
138 "SECNBNS", /* 132: Secondary NBNS Server Address */
139};
140
141#define NCFTYPES128 (sizeof cftypes128/sizeof cftypes128[0])
142
143void
144ipcp_AddInOctets(struct ipcp *ipcp, int n)
145{
146 throughput_addin(&ipcp->throughput, n);
147}
148
149void
150ipcp_AddOutOctets(struct ipcp *ipcp, int n)
151{
152 throughput_addout(&ipcp->throughput, n);
153}
154
155static void
156getdns(struct ipcp *ipcp, struct in_addr addr[2])
157{
158 FILE *fp;
159
160 addr[0].s_addr = addr[1].s_addr = INADDR_ANY;
161 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
162 char buf[LINE_LEN], *cp, *end;
163 int n;
164
165 n = 0;
166 buf[sizeof buf - 1] = '\0';
167 while (fgets(buf, sizeof buf - 1, fp)) {
168 if (!strncmp(buf, "nameserver", 10) && issep(buf[10])) {
169 for (cp = buf + 11; issep(*cp); cp++)
170 ;
171 for (end = cp; isip(*end); end++)
172 ;
173 *end = '\0';
174 if (inet_aton(cp, addr+n) && ++n == 2)
175 break;
176 }
177 }
178 if (n == 1)
179 addr[1] = addr[0];
180 fclose(fp);
181 }
182}
183
184static int
185setdns(struct ipcp *ipcp, struct in_addr addr[2])
186{
187 FILE *fp;
188 char wbuf[LINE_LEN + 54];
189 int wlen;
190
191 if (addr[0].s_addr == INADDR_ANY || addr[1].s_addr == INADDR_ANY) {
192 struct in_addr old[2];
193
194 getdns(ipcp, old);
195 if (addr[0].s_addr == INADDR_ANY)
196 addr[0] = old[0];
197 if (addr[1].s_addr == INADDR_ANY)
198 addr[1] = old[1];
199 }
200
201 if (addr[0].s_addr == INADDR_ANY && addr[1].s_addr == INADDR_ANY) {
202 log_Printf(LogWARN, "%s not modified: All nameservers NAKd\n",
203 _PATH_RESCONF);
204 return 0;
205 }
206
207 wlen = 0;
208 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
209 char buf[LINE_LEN];
210 int len;
211
212 buf[sizeof buf - 1] = '\0';
213 while (fgets(buf, sizeof buf - 1, fp)) {
214 if (strncmp(buf, "nameserver", 10) || !issep(buf[10])) {
215 len = strlen(buf);
216 if (len > sizeof wbuf - wlen) {
217 log_Printf(LogWARN, "%s: Can only cope with max file size %d\n",
218 _PATH_RESCONF, LINE_LEN);
219 fclose(fp);
220 return 0;
221 }
222 memcpy(wbuf + wlen, buf, len);
223 wlen += len;
224 }
225 }
226 fclose(fp);
227 }
228
229 if (addr[0].s_addr != INADDR_ANY) {
230 snprintf(wbuf + wlen, sizeof wbuf - wlen, "nameserver %s\n",
231 inet_ntoa(addr[0]));
232 log_Printf(LogIPCP, "Primary nameserver set to %s", wbuf + wlen + 11);
233 wlen += strlen(wbuf + wlen);
234 }
235
236 if (addr[1].s_addr != INADDR_ANY && addr[1].s_addr != addr[0].s_addr) {
237 snprintf(wbuf + wlen, sizeof wbuf - wlen, "nameserver %s\n",
238 inet_ntoa(addr[1]));
239 log_Printf(LogIPCP, "Secondary nameserver set to %s", wbuf + wlen + 11);
240 wlen += strlen(wbuf + wlen);
241 }
242
243 if (wlen) {
244 int fd;
245
246 if ((fd = ID0open(_PATH_RESCONF, O_WRONLY|O_CREAT, 0644)) != -1) {
247 if (write(fd, wbuf, wlen) != wlen) {
248 log_Printf(LogERROR, "setdns: write(): %s\n", strerror(errno));
249 close(fd);
250 return 0;
251 }
252 if (ftruncate(fd, wlen) == -1) {
253 log_Printf(LogERROR, "setdns: truncate(): %s\n", strerror(errno));
254 close(fd);
255 return 0;
256 }
257 close(fd);
258 } else {
259 log_Printf(LogERROR, "setdns: open(): %s\n", strerror(errno));
260 return 0;
261 }
262 }
263
264 return 1;
265}
266
267int
268ipcp_Show(struct cmdargs const *arg)
269{
270 struct ipcp *ipcp = &arg->bundle->ncp.ipcp;
271
272 prompt_Printf(arg->prompt, "%s [%s]\n", ipcp->fsm.name,
273 State2Nam(ipcp->fsm.state));
274 if (ipcp->fsm.state == ST_OPENED) {
275 prompt_Printf(arg->prompt, " His side: %s, %s\n",
276 inet_ntoa(ipcp->peer_ip), vj2asc(ipcp->peer_compproto));
277 prompt_Printf(arg->prompt, " My side: %s, %s\n",
278 inet_ntoa(ipcp->my_ip), vj2asc(ipcp->my_compproto));
279 }
280
281 if (ipcp->route) {
282 prompt_Printf(arg->prompt, "\n");
283 route_ShowSticky(arg->prompt, ipcp->route, "Sticky routes", 1);
284 }
285
286 prompt_Printf(arg->prompt, "\nDefaults:\n");
287 prompt_Printf(arg->prompt, " FSM retry = %us, max %u Config"
288 " REQ%s, %u Term REQ%s\n", ipcp->cfg.fsm.timeout,
289 ipcp->cfg.fsm.maxreq, ipcp->cfg.fsm.maxreq == 1 ? "" : "s",
290 ipcp->cfg.fsm.maxtrm, ipcp->cfg.fsm.maxtrm == 1 ? "" : "s");
291 prompt_Printf(arg->prompt, " My Address: %s/%d",
292 inet_ntoa(ipcp->cfg.my_range.ipaddr), ipcp->cfg.my_range.width);
293 prompt_Printf(arg->prompt, ", netmask %s\n", inet_ntoa(ipcp->cfg.netmask));
294 if (ipcp->cfg.HaveTriggerAddress)
295 prompt_Printf(arg->prompt, " Trigger address: %s\n",
296 inet_ntoa(ipcp->cfg.TriggerAddress));
297
298 prompt_Printf(arg->prompt, " VJ compression: %s (%d slots %s slot "
299 "compression)\n", command_ShowNegval(ipcp->cfg.vj.neg),
300 ipcp->cfg.vj.slots, ipcp->cfg.vj.slotcomp ? "with" : "without");
301
302 if (iplist_isvalid(&ipcp->cfg.peer_list))
303 prompt_Printf(arg->prompt, " His Address: %s\n",
304 ipcp->cfg.peer_list.src);
305 else
306 prompt_Printf(arg->prompt, " His Address: %s/%d\n",
307 inet_ntoa(ipcp->cfg.peer_range.ipaddr),
308 ipcp->cfg.peer_range.width);
309
310 prompt_Printf(arg->prompt, " DNS: %s, ",
311 inet_ntoa(ipcp->cfg.ns.dns[0]));
312 prompt_Printf(arg->prompt, "%s, %s\n", inet_ntoa(ipcp->cfg.ns.dns[1]),
313 command_ShowNegval(ipcp->cfg.ns.dns_neg));
314 prompt_Printf(arg->prompt, " NetBIOS NS: %s, ",
315 inet_ntoa(ipcp->cfg.ns.nbns[0]));
316 prompt_Printf(arg->prompt, "%s\n", inet_ntoa(ipcp->cfg.ns.nbns[1]));
317
318 prompt_Printf(arg->prompt, "\n");
319 throughput_disp(&ipcp->throughput, arg->prompt);
320
321 return 0;
322}
323
324int
325ipcp_vjset(struct cmdargs const *arg)
326{
327 if (arg->argc != arg->argn+2)
328 return -1;
329 if (!strcasecmp(arg->argv[arg->argn], "slots")) {
330 int slots;
331
332 slots = atoi(arg->argv[arg->argn+1]);
333 if (slots < 4 || slots > 16)
334 return 1;
335 arg->bundle->ncp.ipcp.cfg.vj.slots = slots;
336 return 0;
337 } else if (!strcasecmp(arg->argv[arg->argn], "slotcomp")) {
338 if (!strcasecmp(arg->argv[arg->argn+1], "on"))
339 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 1;
340 else if (!strcasecmp(arg->argv[arg->argn+1], "off"))
341 arg->bundle->ncp.ipcp.cfg.vj.slotcomp = 0;
342 else
343 return 2;
344 return 0;
345 }
346 return -1;
347}
348
349void
350ipcp_Init(struct ipcp *ipcp, struct bundle *bundle, struct link *l,
351 const struct fsm_parent *parent)
352{
353 struct hostent *hp;
354 char name[MAXHOSTNAMELEN];
355 static const char *timer_names[] =
356 {"IPCP restart", "IPCP openmode", "IPCP stopped"};
357
358 fsm_Init(&ipcp->fsm, "IPCP", PROTO_IPCP, 1, IPCP_MAXCODE, LogIPCP,
359 bundle, l, parent, &ipcp_Callbacks, timer_names);
360
361 ipcp->route = NULL;
362 ipcp->cfg.vj.slots = DEF_VJ_STATES;
363 ipcp->cfg.vj.slotcomp = 1;
364 memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range);
365 if (gethostname(name, sizeof name) == 0) {
366 hp = gethostbyname(name);
367 if (hp && hp->h_addrtype == AF_INET)
368 memcpy(&ipcp->cfg.my_range.ipaddr.s_addr, hp->h_addr, hp->h_length);
369 }
370 ipcp->cfg.netmask.s_addr = INADDR_ANY;
371 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
372 iplist_setsrc(&ipcp->cfg.peer_list, "");
373 ipcp->cfg.HaveTriggerAddress = 0;
374
375 ipcp->cfg.ns.dns[0].s_addr = INADDR_ANY;
376 ipcp->cfg.ns.dns[1].s_addr = INADDR_ANY;
377 ipcp->cfg.ns.dns_neg = 0;
378 ipcp->cfg.ns.nbns[0].s_addr = INADDR_ANY;
379 ipcp->cfg.ns.nbns[1].s_addr = INADDR_ANY;
380
381 ipcp->cfg.fsm.timeout = DEF_FSMRETRY;
382 ipcp->cfg.fsm.maxreq = DEF_FSMTRIES;
383 ipcp->cfg.fsm.maxtrm = DEF_FSMTRIES;
384 ipcp->cfg.vj.neg = NEG_ENABLED|NEG_ACCEPTED;
385
386 memset(&ipcp->vj, '\0', sizeof ipcp->vj);
387
388 throughput_init(&ipcp->throughput);
389 memset(ipcp->Queue, '\0', sizeof ipcp->Queue);
390 ipcp_Setup(ipcp, INADDR_NONE);
391}
392
393void
394ipcp_SetLink(struct ipcp *ipcp, struct link *l)
395{
396 ipcp->fsm.link = l;
397}
398
399void
400ipcp_Setup(struct ipcp *ipcp, u_int32_t mask)
401{
402 struct iface *iface = ipcp->fsm.bundle->iface;
403 int pos, n;
404
405 ipcp->fsm.open_mode = 0;
406 ipcp->ifmask.s_addr = mask == INADDR_NONE ? ipcp->cfg.netmask.s_addr : mask;
407
408 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
409 /* Try to give the peer a previously configured IP address */
410 for (n = 0; n < iface->in_addrs; n++) {
411 pos = iplist_ip2pos(&ipcp->cfg.peer_list, iface->in_addr[n].brd);
412 if (pos != -1) {
413 ipcp->cfg.peer_range.ipaddr =
414 iplist_setcurpos(&ipcp->cfg.peer_list, pos);
415 break;
416 }
417 }
418 if (n == iface->in_addrs)
419 /* Ok, so none of 'em fit.... pick a random one */
420 ipcp->cfg.peer_range.ipaddr = iplist_setrandpos(&ipcp->cfg.peer_list);
421
422 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
423 ipcp->cfg.peer_range.width = 32;
424 }
425
426 ipcp->heis1172 = 0;
427
428 ipcp->peer_ip = ipcp->cfg.peer_range.ipaddr;
429 ipcp->peer_compproto = 0;
430
431 if (ipcp->cfg.HaveTriggerAddress) {
432 /*
433 * Some implementations of PPP require that we send a
434 * *special* value as our address, even though the rfc specifies
435 * full negotiation (e.g. "0.0.0.0" or Not "0.0.0.0").
436 */
437 ipcp->my_ip = ipcp->cfg.TriggerAddress;
438 log_Printf(LogIPCP, "Using trigger address %s\n",
439 inet_ntoa(ipcp->cfg.TriggerAddress));
440 } else {
441 /*
442 * Otherwise, if we've used an IP number before and it's still within
443 * the network specified on the ``set ifaddr'' line, we really
444 * want to keep that IP number so that we can keep any existing
445 * connections that are bound to that IP (assuming we're not
446 * ``iface-alias''ing).
447 */
448 for (n = 0; n < iface->in_addrs; n++)
449 if ((iface->in_addr[n].ifa.s_addr & ipcp->cfg.my_range.mask.s_addr) ==
450 (ipcp->cfg.my_range.ipaddr.s_addr & ipcp->cfg.my_range.mask.s_addr)) {
451 ipcp->my_ip = iface->in_addr[n].ifa;
452 break;
453 }
454 if (n == iface->in_addrs)
455 ipcp->my_ip = ipcp->cfg.my_range.ipaddr;
456 }
457
458 if (IsEnabled(ipcp->cfg.vj.neg)
459#ifndef NORADIUS
460 || (ipcp->fsm.bundle->radius.valid && ipcp->fsm.bundle->radius.vj)
461#endif
462 )
463 ipcp->my_compproto = (PROTO_VJCOMP << 16) +
464 ((ipcp->cfg.vj.slots - 1) << 8) +
465 ipcp->cfg.vj.slotcomp;
466 else
467 ipcp->my_compproto = 0;
468 sl_compress_init(&ipcp->vj.cslc, ipcp->cfg.vj.slots - 1);
469
470 ipcp->peer_reject = 0;
471 ipcp->my_reject = 0;
472}
473
474static int
475ipcp_doproxyall(struct bundle *bundle,
476 int (*proxyfun)(struct bundle *, struct in_addr, int), int s)
477{
478 int n, ret;
479 struct sticky_route *rp;
480 struct in_addr addr;
481 struct ipcp *ipcp;
482
483 ipcp = &bundle->ncp.ipcp;
484 for (rp = ipcp->route; rp != NULL; rp = rp->next) {
485 if (rp->mask.s_addr == INADDR_BROADCAST)
486 continue;
487 n = ntohl(INADDR_BROADCAST) - ntohl(rp->mask.s_addr) - 1;
488 if (n > 0 && n <= 254 && rp->dst.s_addr != INADDR_ANY) {
489 addr = rp->dst;
490 while (n--) {
491 addr.s_addr = htonl(ntohl(addr.s_addr) + 1);
492 log_Printf(LogDEBUG, "ipcp_doproxyall: %s\n", inet_ntoa(addr));
493 ret = (*proxyfun)(bundle, addr, s);
494 if (!ret)
495 return ret;
496 }
497 }
498 }
499
500 return 0;
501}
502
503static int
504ipcp_SetIPaddress(struct bundle *bundle, struct in_addr myaddr,
505 struct in_addr hisaddr, int silent)
506{
507 static struct in_addr none = { INADDR_ANY };
508 struct in_addr mask, oaddr;
509
510 mask = addr2mask(myaddr);
511
512 if (bundle->ncp.ipcp.ifmask.s_addr != INADDR_ANY &&
513 (bundle->ncp.ipcp.ifmask.s_addr & mask.s_addr) == mask.s_addr)
514 mask.s_addr = bundle->ncp.ipcp.ifmask.s_addr;
515
516 oaddr.s_addr = bundle->iface->in_addrs ?
517 bundle->iface->in_addr[0].ifa.s_addr : INADDR_ANY;
518 if (!iface_inAdd(bundle->iface, myaddr, mask, hisaddr,
519 IFACE_ADD_FIRST|IFACE_FORCE_ADD))
520 return -1;
521
522 if (!Enabled(bundle, OPT_IFACEALIAS) && bundle->iface->in_addrs > 1
523 && myaddr.s_addr != oaddr.s_addr)
524 /* Nuke the old one */
525 iface_inDelete(bundle->iface, oaddr);
526
527 if (bundle->ncp.ipcp.cfg.sendpipe > 0 || bundle->ncp.ipcp.cfg.recvpipe > 0)
528 bundle_SetRoute(bundle, RTM_CHANGE, hisaddr, myaddr, none, 0, 0);
529
530 if (Enabled(bundle, OPT_SROUTES))
531 route_Change(bundle, bundle->ncp.ipcp.route, myaddr, hisaddr);
532
533#ifndef NORADIUS
534 if (bundle->radius.valid)
535 route_Change(bundle, bundle->radius.routes, myaddr, hisaddr);
536#endif
537
538 if (Enabled(bundle, OPT_PROXY) || Enabled(bundle, OPT_PROXYALL)) {
539 int s = ID0socket(AF_INET, SOCK_DGRAM, 0);
540 if (s < 0)
541 log_Printf(LogERROR, "ipcp_SetIPaddress: socket(): %s\n",
542 strerror(errno));
543 else {
544 if (Enabled(bundle, OPT_PROXYALL))
545 ipcp_doproxyall(bundle, arp_SetProxy, s);
546 else if (Enabled(bundle, OPT_PROXY))
547 arp_SetProxy(bundle, hisaddr, s);
548 close(s);
549 }
550 }
551
552 return 0;
553}
554
555static struct in_addr
556ChooseHisAddr(struct bundle *bundle, struct in_addr gw)
557{
558 struct in_addr try;
559 u_long f;
560
561 for (f = 0; f < bundle->ncp.ipcp.cfg.peer_list.nItems; f++) {
562 try = iplist_next(&bundle->ncp.ipcp.cfg.peer_list);
563 log_Printf(LogDEBUG, "ChooseHisAddr: Check item %ld (%s)\n",
564 f, inet_ntoa(try));
565 if (ipcp_SetIPaddress(bundle, gw, try, 1) == 0) {
566 log_Printf(LogIPCP, "Selected IP address %s\n", inet_ntoa(try));
567 break;
568 }
569 }
570
571 if (f == bundle->ncp.ipcp.cfg.peer_list.nItems) {
572 log_Printf(LogDEBUG, "ChooseHisAddr: All addresses in use !\n");
573 try.s_addr = INADDR_ANY;
574 }
575
576 return try;
577}
578
579static void
580IpcpInitRestartCounter(struct fsm *fp, int what)
581{
582 /* Set fsm timer load */
583 struct ipcp *ipcp = fsm2ipcp(fp);
584
585 fp->FsmTimer.load = ipcp->cfg.fsm.timeout * SECTICKS;
586 switch (what) {
587 case FSM_REQ_TIMER:
588 fp->restart = ipcp->cfg.fsm.maxreq;
589 break;
590 case FSM_TRM_TIMER:
591 fp->restart = ipcp->cfg.fsm.maxtrm;
592 break;
593 default:
594 fp->restart = 1;
595 break;
596 }
597}
598
599static void
600IpcpSendConfigReq(struct fsm *fp)
601{
602 /* Send config REQ please */
603 struct physical *p = link2physical(fp->link);
604 struct ipcp *ipcp = fsm2ipcp(fp);
605 u_char buff[24];
606 struct lcp_opt *o;
607
608 o = (struct lcp_opt *)buff;
609
610 if ((p && !physical_IsSync(p)) || !REJECTED(ipcp, TY_IPADDR)) {
611 memcpy(o->data, &ipcp->my_ip.s_addr, 4);
612 INC_LCP_OPT(TY_IPADDR, 6, o);
613 }
614
615 if (ipcp->my_compproto && !REJECTED(ipcp, TY_COMPPROTO)) {
616 if (ipcp->heis1172) {
617 u_int16_t proto = PROTO_VJCOMP;
618
619 ua_htons(&proto, o->data);
620 INC_LCP_OPT(TY_COMPPROTO, 4, o);
621 } else {
622 struct compreq req;
623
624 req.proto = htons(ipcp->my_compproto >> 16);
625 req.slots = (ipcp->my_compproto >> 8) & 255;
626 req.compcid = ipcp->my_compproto & 1;
627 memcpy(o->data, &req, 4);
628 INC_LCP_OPT(TY_COMPPROTO, 6, o);
629 }
630 }
631
632 if (IsEnabled(ipcp->cfg.ns.dns_neg) &&
633 !REJECTED(ipcp, TY_PRIMARY_DNS - TY_ADJUST_NS) &&
634 !REJECTED(ipcp, TY_SECONDARY_DNS - TY_ADJUST_NS)) {
635 struct in_addr dns[2];
636 getdns(ipcp, dns);
637 memcpy(o->data, &dns[0].s_addr, 4);
638 INC_LCP_OPT(TY_PRIMARY_DNS, 6, o);
639 memcpy(o->data, &dns[1].s_addr, 4);
640 INC_LCP_OPT(TY_SECONDARY_DNS, 6, o);
641 }
642
643 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, (u_char *)o - buff);
644}
645
646static void
647IpcpSentTerminateReq(struct fsm *fp)
648{
649 /* Term REQ just sent by FSM */
650}
651
652static void
653IpcpSendTerminateAck(struct fsm *fp, u_char id)
654{
655 /* Send Term ACK please */
656 fsm_Output(fp, CODE_TERMACK, id, NULL, 0);
657}
658
659static void
660IpcpLayerStart(struct fsm *fp)
661{
662 /* We're about to start up ! */
663 struct ipcp *ipcp = fsm2ipcp(fp);
664
665 log_Printf(LogIPCP, "%s: LayerStart.\n", fp->link->name);
666 throughput_start(&ipcp->throughput, "IPCP throughput",
667 Enabled(fp->bundle, OPT_THROUGHPUT));
668 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
669}
670
671static void
672IpcpLayerFinish(struct fsm *fp)
673{
674 /* We're now down */
675 struct ipcp *ipcp = fsm2ipcp(fp);
676
677 log_Printf(LogIPCP, "%s: LayerFinish.\n", fp->link->name);
678 throughput_stop(&ipcp->throughput);
679 throughput_log(&ipcp->throughput, LogIPCP, NULL);
680}
681
682void
683ipcp_CleanInterface(struct ipcp *ipcp)
684{
685 struct iface *iface = ipcp->fsm.bundle->iface;
686
687 route_Clean(ipcp->fsm.bundle, ipcp->route);
688
689 if (iface->in_addrs && (Enabled(ipcp->fsm.bundle, OPT_PROXY) ||
690 Enabled(ipcp->fsm.bundle, OPT_PROXYALL))) {
691 int s = ID0socket(AF_INET, SOCK_DGRAM, 0);
692 if (s < 0)
693 log_Printf(LogERROR, "ipcp_CleanInterface: socket: %s\n",
694 strerror(errno));
695 else {
696 if (Enabled(ipcp->fsm.bundle, OPT_PROXYALL))
697 ipcp_doproxyall(ipcp->fsm.bundle, arp_ClearProxy, s);
698 else if (Enabled(ipcp->fsm.bundle, OPT_PROXY))
699 arp_ClearProxy(ipcp->fsm.bundle, iface->in_addr[0].brd, s);
700 close(s);
701 }
702 }
703
704 iface_inClear(ipcp->fsm.bundle->iface, IFACE_CLEAR_ALL);
705}
706
707static void
708IpcpLayerDown(struct fsm *fp)
709{
710 /* About to come down */
711 struct ipcp *ipcp = fsm2ipcp(fp);
712 const char *s;
713
714 if (ipcp->fsm.bundle->iface->in_addrs)
715 s = inet_ntoa(ipcp->fsm.bundle->iface->in_addr[0].ifa);
716 else
717 s = "Interface configuration error !";
718 log_Printf(LogIPCP, "%s: LayerDown: %s\n", fp->link->name, s);
719
720 /*
721 * XXX this stuff should really live in the FSM. Our config should
722 * associate executable sections in files with events.
723 */
724 if (system_Select(fp->bundle, s, LINKDOWNFILE, NULL, NULL) < 0) {
725 if (bundle_GetLabel(fp->bundle)) {
726 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
727 LINKDOWNFILE, NULL, NULL) < 0)
728 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
729 } else
730 system_Select(fp->bundle, "MYADDR", LINKDOWNFILE, NULL, NULL);
731 }
732
733 ipcp_Setup(ipcp, INADDR_NONE);
734}
735
736int
737ipcp_InterfaceUp(struct ipcp *ipcp)
738{
739 if (ipcp_SetIPaddress(ipcp->fsm.bundle, ipcp->my_ip, ipcp->peer_ip, 0) < 0) {
740 log_Printf(LogERROR, "ipcp_InterfaceUp: unable to set ip address\n");
741 return 0;
742 }
743
744#ifndef NOALIAS
745 if (ipcp->fsm.bundle->AliasEnabled)
746 PacketAliasSetAddress(ipcp->my_ip);
747#endif
748
749 return 1;
750}
751
752static int
753IpcpLayerUp(struct fsm *fp)
754{
755 /* We're now up */
756 struct ipcp *ipcp = fsm2ipcp(fp);
757 char tbuff[16];
758
759 log_Printf(LogIPCP, "%s: LayerUp.\n", fp->link->name);
760 snprintf(tbuff, sizeof tbuff, "%s", inet_ntoa(ipcp->my_ip));
761 log_Printf(LogIPCP, "myaddr %s hisaddr = %s\n",
762 tbuff, inet_ntoa(ipcp->peer_ip));
763
764 if (ipcp->peer_compproto >> 16 == PROTO_VJCOMP)
765 sl_compress_init(&ipcp->vj.cslc, (ipcp->peer_compproto >> 8) & 255);
766
767 if (!ipcp_InterfaceUp(ipcp))
768 return 0;
769
770 /*
771 * XXX this stuff should really live in the FSM. Our config should
772 * associate executable sections in files with events.
773 */
774 if (system_Select(fp->bundle, tbuff, LINKUPFILE, NULL, NULL) < 0) {
775 if (bundle_GetLabel(fp->bundle)) {
776 if (system_Select(fp->bundle, bundle_GetLabel(fp->bundle),
777 LINKUPFILE, NULL, NULL) < 0)
778 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
779 } else
780 system_Select(fp->bundle, "MYADDR", LINKUPFILE, NULL, NULL);
781 }
782
783 fp->more.reqs = fp->more.naks = fp->more.rejs = ipcp->cfg.fsm.maxreq * 3;
784 log_DisplayPrompts();
785
786 return 1;
787}
788
789static int
790AcceptableAddr(const struct in_range *prange, struct in_addr ipaddr)
791{
792 /* Is the given IP in the given range ? */
793 return (prange->ipaddr.s_addr & prange->mask.s_addr) ==
794 (ipaddr.s_addr & prange->mask.s_addr) && ipaddr.s_addr;
795}
796
797static void
798IpcpDecodeConfig(struct fsm *fp, u_char * cp, int plen, int mode_type,
798IpcpDecodeConfig(struct fsm *fp, u_char *cp, int plen, int mode_type,
799 struct fsm_decode *dec)
800{
801 /* Deal with incoming PROTO_IPCP */
802 struct iface *iface = fp->bundle->iface;
803 struct ipcp *ipcp = fsm2ipcp(fp);
804 int type, length, gotdns, gotdnsnak, n;
805 u_int32_t compproto;
806 struct compreq *pcomp;
807 struct in_addr ipaddr, dstipaddr, have_ip, dns[2], dnsnak[2];
808 char tbuff[100], tbuff2[100];
809
810 gotdns = 0;
811 gotdnsnak = 0;
812 dnsnak[0].s_addr = dnsnak[1].s_addr = INADDR_ANY;
813
814 while (plen >= sizeof(struct fsmconfig)) {
815 type = *cp;
816 length = cp[1];
817
818 if (length == 0) {
819 log_Printf(LogIPCP, "%s: IPCP size zero\n", fp->link->name);
820 break;
821 }
822
823 if (type < NCFTYPES)
824 snprintf(tbuff, sizeof tbuff, " %s[%d] ", cftypes[type], length);
825 else if (type > 128 && type < 128 + NCFTYPES128)
826 snprintf(tbuff, sizeof tbuff, " %s[%d] ", cftypes128[type-128], length);
827 else
828 snprintf(tbuff, sizeof tbuff, " <%d>[%d] ", type, length);
829
830 switch (type) {
831 case TY_IPADDR: /* RFC1332 */
832 memcpy(&ipaddr.s_addr, cp + 2, 4);
833 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
834
835 switch (mode_type) {
836 case MODE_REQ:
837 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
838 if (ipaddr.s_addr == INADDR_ANY ||
839 iplist_ip2pos(&ipcp->cfg.peer_list, ipaddr) < 0 ||
840 ipcp_SetIPaddress(fp->bundle, ipcp->cfg.my_range.ipaddr,
841 ipaddr, 1)) {
842 log_Printf(LogIPCP, "%s: Address invalid or already in use\n",
843 inet_ntoa(ipaddr));
844 /*
845 * If we've already had a valid address configured for the peer,
846 * try NAKing with that so that we don't have to upset things
847 * too much.
848 */
849 for (n = 0; n < iface->in_addrs; n++)
850 if (iplist_ip2pos(&ipcp->cfg.peer_list, iface->in_addr[n].brd)
851 >=0) {
852 ipcp->peer_ip = iface->in_addr[n].brd;
853 break;
854 }
855
856 if (n == iface->in_addrs)
857 /* Just pick an IP number from our list */
858 ipcp->peer_ip = ChooseHisAddr
859 (fp->bundle, ipcp->cfg.my_range.ipaddr);
860
861 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
862 memcpy(dec->rejend, cp, length);
863 dec->rejend += length;
864 } else {
865 memcpy(dec->nakend, cp, 2);
866 memcpy(dec->nakend + 2, &ipcp->peer_ip.s_addr, length - 2);
867 dec->nakend += length;
868 }
869 break;
870 }
871 } else if (!AcceptableAddr(&ipcp->cfg.peer_range, ipaddr)) {
872 /*
873 * If destination address is not acceptable, NAK with what we
874 * want to use.
875 */
876 memcpy(dec->nakend, cp, 2);
877 for (n = 0; n < iface->in_addrs; n++)
878 if ((iface->in_addr[n].brd.s_addr &
879 ipcp->cfg.peer_range.mask.s_addr)
880 == (ipcp->cfg.peer_range.ipaddr.s_addr &
881 ipcp->cfg.peer_range.mask.s_addr)) {
882 /* We prefer the already-configured address */
883 memcpy(dec->nakend + 2, &iface->in_addr[n].brd.s_addr,
884 length - 2);
885 break;
886 }
887
888 if (n == iface->in_addrs)
889 memcpy(dec->nakend + 2, &ipcp->peer_ip.s_addr, length - 2);
890
891 dec->nakend += length;
892 break;
893 }
894 ipcp->peer_ip = ipaddr;
895 memcpy(dec->ackend, cp, length);
896 dec->ackend += length;
897 break;
898
899 case MODE_NAK:
900 if (AcceptableAddr(&ipcp->cfg.my_range, ipaddr)) {
901 /* Use address suggested by peer */
902 snprintf(tbuff2, sizeof tbuff2, "%s changing address: %s ", tbuff,
903 inet_ntoa(ipcp->my_ip));
904 log_Printf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
905 ipcp->my_ip = ipaddr;
906 } else {
907 log_Printf(log_IsKept(LogIPCP) ? LogIPCP : LogPHASE,
908 "%s: Unacceptable address!\n", inet_ntoa(ipaddr));
909 fsm_Close(&ipcp->fsm);
910 }
911 break;
912
913 case MODE_REJ:
914 ipcp->peer_reject |= (1 << type);
915 break;
916 }
917 break;
918
919 case TY_COMPPROTO:
920 pcomp = (struct compreq *)(cp + 2);
921 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
922 pcomp->compcid;
923 log_Printf(LogIPCP, "%s %s\n", tbuff, vj2asc(compproto));
924
925 switch (mode_type) {
926 case MODE_REQ:
927 if (!IsAccepted(ipcp->cfg.vj.neg)) {
928 memcpy(dec->rejend, cp, length);
929 dec->rejend += length;
930 } else {
931 switch (length) {
932 case 4: /* RFC1172 */
933 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
934 log_Printf(LogWARN, "Peer is speaking RFC1172 compression "
935 "protocol !\n");
936 ipcp->heis1172 = 1;
937 ipcp->peer_compproto = compproto;
938 memcpy(dec->ackend, cp, length);
939 dec->ackend += length;
940 } else {
941 memcpy(dec->nakend, cp, 2);
942 pcomp->proto = htons(PROTO_VJCOMP);
943 memcpy(dec->nakend+2, &pcomp, 2);
944 dec->nakend += length;
945 }
946 break;
947 case 6: /* RFC1332 */
948 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
949 if (pcomp->slots <= MAX_VJ_STATES
950 && pcomp->slots >= MIN_VJ_STATES) {
951 /* Ok, we can do that */
952 ipcp->peer_compproto = compproto;
953 ipcp->heis1172 = 0;
954 memcpy(dec->ackend, cp, length);
955 dec->ackend += length;
956 } else {
957 /* Get as close as we can to what he wants */
958 ipcp->heis1172 = 0;
959 memcpy(dec->nakend, cp, 2);
960 pcomp->slots = pcomp->slots < MIN_VJ_STATES ?
961 MIN_VJ_STATES : MAX_VJ_STATES;
962 memcpy(dec->nakend+2, &pcomp, sizeof pcomp);
963 dec->nakend += length;
964 }
965 } else {
966 /* What we really want */
967 memcpy(dec->nakend, cp, 2);
968 pcomp->proto = htons(PROTO_VJCOMP);
969 pcomp->slots = DEF_VJ_STATES;
970 pcomp->compcid = 1;
971 memcpy(dec->nakend+2, &pcomp, sizeof pcomp);
972 dec->nakend += length;
973 }
974 break;
975 default:
976 memcpy(dec->rejend, cp, length);
977 dec->rejend += length;
978 break;
979 }
980 }
981 break;
982
983 case MODE_NAK:
984 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
985 if (pcomp->slots > MAX_VJ_STATES)
986 pcomp->slots = MAX_VJ_STATES;
987 else if (pcomp->slots < MIN_VJ_STATES)
988 pcomp->slots = MIN_VJ_STATES;
989 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
990 pcomp->compcid;
991 } else
992 compproto = 0;
993 log_Printf(LogIPCP, "%s changing compproto: %08x --> %08x\n",
994 tbuff, ipcp->my_compproto, compproto);
995 ipcp->my_compproto = compproto;
996 break;
997
998 case MODE_REJ:
999 ipcp->peer_reject |= (1 << type);
1000 break;
1001 }
1002 break;
1003
1004 case TY_IPADDRS: /* RFC1172 */
1005 memcpy(&ipaddr.s_addr, cp + 2, 4);
1006 memcpy(&dstipaddr.s_addr, cp + 6, 4);
1007 snprintf(tbuff2, sizeof tbuff2, "%s %s,", tbuff, inet_ntoa(ipaddr));
1008 log_Printf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr));
1009
1010 switch (mode_type) {
1011 case MODE_REQ:
1012 memcpy(dec->rejend, cp, length);
1013 dec->rejend += length;
1014 break;
1015
1016 case MODE_NAK:
1017 case MODE_REJ:
1018 break;
1019 }
1020 break;
1021
1022 case TY_PRIMARY_DNS: /* DNS negotiation (rfc1877) */
1023 case TY_SECONDARY_DNS:
1024 memcpy(&ipaddr.s_addr, cp + 2, 4);
1025 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1026
1027 switch (mode_type) {
1028 case MODE_REQ:
1029 if (!IsAccepted(ipcp->cfg.ns.dns_neg)) {
1030 ipcp->my_reject |= (1 << (type - TY_ADJUST_NS));
1031 memcpy(dec->rejend, cp, length);
1032 dec->rejend += length;
1033 break;
1034 }
1035 if (!gotdns) {
1036 dns[0] = ipcp->cfg.ns.dns[0];
1037 dns[1] = ipcp->cfg.ns.dns[1];
1038 if (dns[0].s_addr == INADDR_ANY && dns[1].s_addr == INADDR_ANY)
1039 getdns(ipcp, dns);
1040 gotdns = 1;
1041 }
1042 have_ip = dns[type == TY_PRIMARY_DNS ? 0 : 1];
1043
1044 if (ipaddr.s_addr != have_ip.s_addr) {
1045 /*
1046 * The client has got the DNS stuff wrong (first request) so
1047 * we'll tell 'em how it is
1048 */
1049 memcpy(dec->nakend, cp, 2); /* copy first two (type/length) */
1050 memcpy(dec->nakend + 2, &have_ip.s_addr, length - 2);
1051 dec->nakend += length;
1052 } else {
1053 /*
1054 * Otherwise they have it right (this time) so we send a ack packet
1055 * back confirming it... end of story
1056 */
1057 memcpy(dec->ackend, cp, length);
1058 dec->ackend += length;
1059 }
1060 break;
1061
1062 case MODE_NAK: /* what does this mean?? */
1063 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
1064 gotdnsnak = 1;
1065 memcpy(&dnsnak[type == TY_PRIMARY_DNS ? 0 : 1].s_addr, cp + 2, 4);
1066 }
1067 break;
1068
1069 case MODE_REJ: /* Can't do much, stop asking */
1070 ipcp->peer_reject |= (1 << (type - TY_ADJUST_NS));
1071 break;
1072 }
1073 break;
1074
1075 case TY_PRIMARY_NBNS: /* M$ NetBIOS nameserver hack (rfc1877) */
1076 case TY_SECONDARY_NBNS:
1077 memcpy(&ipaddr.s_addr, cp + 2, 4);
1078 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1079
1080 switch (mode_type) {
1081 case MODE_REQ:
1082 have_ip.s_addr =
1083 ipcp->cfg.ns.nbns[type == TY_PRIMARY_NBNS ? 0 : 1].s_addr;
1084
1085 if (have_ip.s_addr == INADDR_ANY) {
1086 log_Printf(LogIPCP, "NBNS REQ - rejected - nbns not set\n");
1087 ipcp->my_reject |= (1 << (type - TY_ADJUST_NS));
1088 memcpy(dec->rejend, cp, length);
1089 dec->rejend += length;
1090 break;
1091 }
1092
1093 if (ipaddr.s_addr != have_ip.s_addr) {
1094 memcpy(dec->nakend, cp, 2);
1095 memcpy(dec->nakend+2, &have_ip.s_addr, length);
1096 dec->nakend += length;
1097 } else {
1098 memcpy(dec->ackend, cp, length);
1099 dec->ackend += length;
1100 }
1101 break;
1102
1103 case MODE_NAK:
1104 log_Printf(LogIPCP, "MS NBNS req %d - NAK??\n", type);
1105 break;
1106
1107 case MODE_REJ:
1108 log_Printf(LogIPCP, "MS NBNS req %d - REJ??\n", type);
1109 break;
1110 }
1111 break;
1112
1113 default:
1114 if (mode_type != MODE_NOP) {
1115 ipcp->my_reject |= (1 << type);
1116 memcpy(dec->rejend, cp, length);
1117 dec->rejend += length;
1118 }
1119 break;
1120 }
1121 plen -= length;
1122 cp += length;
1123 }
1124
1125 if (gotdnsnak)
1126 if (!setdns(ipcp, dnsnak)) {
1127 ipcp->peer_reject |= (1 << (TY_PRIMARY_DNS - TY_ADJUST_NS));
1128 ipcp->peer_reject |= (1 << (TY_SECONDARY_DNS - TY_ADJUST_NS));
1129 }
1130
1131 if (mode_type != MODE_NOP) {
1132 if (dec->rejend != dec->rej) {
1133 /* rejects are preferred */
1134 dec->ackend = dec->ack;
1135 dec->nakend = dec->nak;
1136 } else if (dec->nakend != dec->nak)
1137 /* then NAKs */
1138 dec->ackend = dec->ack;
1139 }
1140}
1141
1142extern struct mbuf *
1143ipcp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
1144{
1145 /* Got PROTO_IPCP from link */
1146 if (bundle_Phase(bundle) == PHASE_NETWORK)
1147 fsm_Input(&bundle->ncp.ipcp.fsm, bp);
1148 else {
1149 if (bundle_Phase(bundle) < PHASE_NETWORK)
1150 log_Printf(LogIPCP, "%s: Error: Unexpected IPCP in phase %s (ignored)\n",
1151 l->name, bundle_PhaseName(bundle));
1152 mbuf_Free(bp);
1153 }
1154 return NULL;
1155}
1156
1157int
1158ipcp_UseHisIPaddr(struct bundle *bundle, struct in_addr hisaddr)
1159{
1160 struct ipcp *ipcp = &bundle->ncp.ipcp;
1161
1162 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1163 iplist_reset(&ipcp->cfg.peer_list);
1164 ipcp->peer_ip = ipcp->cfg.peer_range.ipaddr = hisaddr;
1165 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
1166 ipcp->cfg.peer_range.width = 32;
1167
1168 if (ipcp_SetIPaddress(bundle, ipcp->cfg.my_range.ipaddr, hisaddr, 0) < 0)
1169 return 0;
1170
1171 return 1; /* Ok */
1172}
1173
1174int
1175ipcp_UseHisaddr(struct bundle *bundle, const char *hisaddr, int setaddr)
1176{
1177 struct ipcp *ipcp = &bundle->ncp.ipcp;
1178
1179 /* Use `hisaddr' for the peers address (set iface if `setaddr') */
1180 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1181 iplist_reset(&ipcp->cfg.peer_list);
1182 if (strpbrk(hisaddr, ",-")) {
1183 iplist_setsrc(&ipcp->cfg.peer_list, hisaddr);
1184 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
1185 iplist_setrandpos(&ipcp->cfg.peer_list);
1186 ipcp->peer_ip = ChooseHisAddr(bundle, ipcp->my_ip);
1187 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
1188 log_Printf(LogWARN, "%s: None available !\n", ipcp->cfg.peer_list.src);
1189 return(0);
1190 }
1191 ipcp->cfg.peer_range.ipaddr.s_addr = ipcp->peer_ip.s_addr;
1192 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
1193 ipcp->cfg.peer_range.width = 32;
1194 } else {
1195 log_Printf(LogWARN, "%s: Invalid range !\n", hisaddr);
1196 return 0;
1197 }
1198 } else if (ParseAddr(ipcp, hisaddr, &ipcp->cfg.peer_range.ipaddr,
1199 &ipcp->cfg.peer_range.mask,
1200 &ipcp->cfg.peer_range.width) != 0) {
1201 ipcp->peer_ip.s_addr = ipcp->cfg.peer_range.ipaddr.s_addr;
1202
1203 if (setaddr && ipcp_SetIPaddress(bundle, ipcp->cfg.my_range.ipaddr,
1204 ipcp->cfg.peer_range.ipaddr, 0) < 0)
1205 return 0;
1206 } else
1207 return 0;
1208
1209 return 1;
1210}
1211
1212struct in_addr
1213addr2mask(struct in_addr addr)
1214{
1215 u_int32_t haddr = ntohl(addr.s_addr);
1216
1217 haddr = IN_CLASSA(haddr) ? IN_CLASSA_NET :
1218 IN_CLASSB(haddr) ? IN_CLASSB_NET :
1219 IN_CLASSC_NET;
1220 addr.s_addr = htonl(haddr);
1221
1222 return addr;
1223}
799 struct fsm_decode *dec)
800{
801 /* Deal with incoming PROTO_IPCP */
802 struct iface *iface = fp->bundle->iface;
803 struct ipcp *ipcp = fsm2ipcp(fp);
804 int type, length, gotdns, gotdnsnak, n;
805 u_int32_t compproto;
806 struct compreq *pcomp;
807 struct in_addr ipaddr, dstipaddr, have_ip, dns[2], dnsnak[2];
808 char tbuff[100], tbuff2[100];
809
810 gotdns = 0;
811 gotdnsnak = 0;
812 dnsnak[0].s_addr = dnsnak[1].s_addr = INADDR_ANY;
813
814 while (plen >= sizeof(struct fsmconfig)) {
815 type = *cp;
816 length = cp[1];
817
818 if (length == 0) {
819 log_Printf(LogIPCP, "%s: IPCP size zero\n", fp->link->name);
820 break;
821 }
822
823 if (type < NCFTYPES)
824 snprintf(tbuff, sizeof tbuff, " %s[%d] ", cftypes[type], length);
825 else if (type > 128 && type < 128 + NCFTYPES128)
826 snprintf(tbuff, sizeof tbuff, " %s[%d] ", cftypes128[type-128], length);
827 else
828 snprintf(tbuff, sizeof tbuff, " <%d>[%d] ", type, length);
829
830 switch (type) {
831 case TY_IPADDR: /* RFC1332 */
832 memcpy(&ipaddr.s_addr, cp + 2, 4);
833 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
834
835 switch (mode_type) {
836 case MODE_REQ:
837 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
838 if (ipaddr.s_addr == INADDR_ANY ||
839 iplist_ip2pos(&ipcp->cfg.peer_list, ipaddr) < 0 ||
840 ipcp_SetIPaddress(fp->bundle, ipcp->cfg.my_range.ipaddr,
841 ipaddr, 1)) {
842 log_Printf(LogIPCP, "%s: Address invalid or already in use\n",
843 inet_ntoa(ipaddr));
844 /*
845 * If we've already had a valid address configured for the peer,
846 * try NAKing with that so that we don't have to upset things
847 * too much.
848 */
849 for (n = 0; n < iface->in_addrs; n++)
850 if (iplist_ip2pos(&ipcp->cfg.peer_list, iface->in_addr[n].brd)
851 >=0) {
852 ipcp->peer_ip = iface->in_addr[n].brd;
853 break;
854 }
855
856 if (n == iface->in_addrs)
857 /* Just pick an IP number from our list */
858 ipcp->peer_ip = ChooseHisAddr
859 (fp->bundle, ipcp->cfg.my_range.ipaddr);
860
861 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
862 memcpy(dec->rejend, cp, length);
863 dec->rejend += length;
864 } else {
865 memcpy(dec->nakend, cp, 2);
866 memcpy(dec->nakend + 2, &ipcp->peer_ip.s_addr, length - 2);
867 dec->nakend += length;
868 }
869 break;
870 }
871 } else if (!AcceptableAddr(&ipcp->cfg.peer_range, ipaddr)) {
872 /*
873 * If destination address is not acceptable, NAK with what we
874 * want to use.
875 */
876 memcpy(dec->nakend, cp, 2);
877 for (n = 0; n < iface->in_addrs; n++)
878 if ((iface->in_addr[n].brd.s_addr &
879 ipcp->cfg.peer_range.mask.s_addr)
880 == (ipcp->cfg.peer_range.ipaddr.s_addr &
881 ipcp->cfg.peer_range.mask.s_addr)) {
882 /* We prefer the already-configured address */
883 memcpy(dec->nakend + 2, &iface->in_addr[n].brd.s_addr,
884 length - 2);
885 break;
886 }
887
888 if (n == iface->in_addrs)
889 memcpy(dec->nakend + 2, &ipcp->peer_ip.s_addr, length - 2);
890
891 dec->nakend += length;
892 break;
893 }
894 ipcp->peer_ip = ipaddr;
895 memcpy(dec->ackend, cp, length);
896 dec->ackend += length;
897 break;
898
899 case MODE_NAK:
900 if (AcceptableAddr(&ipcp->cfg.my_range, ipaddr)) {
901 /* Use address suggested by peer */
902 snprintf(tbuff2, sizeof tbuff2, "%s changing address: %s ", tbuff,
903 inet_ntoa(ipcp->my_ip));
904 log_Printf(LogIPCP, "%s --> %s\n", tbuff2, inet_ntoa(ipaddr));
905 ipcp->my_ip = ipaddr;
906 } else {
907 log_Printf(log_IsKept(LogIPCP) ? LogIPCP : LogPHASE,
908 "%s: Unacceptable address!\n", inet_ntoa(ipaddr));
909 fsm_Close(&ipcp->fsm);
910 }
911 break;
912
913 case MODE_REJ:
914 ipcp->peer_reject |= (1 << type);
915 break;
916 }
917 break;
918
919 case TY_COMPPROTO:
920 pcomp = (struct compreq *)(cp + 2);
921 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
922 pcomp->compcid;
923 log_Printf(LogIPCP, "%s %s\n", tbuff, vj2asc(compproto));
924
925 switch (mode_type) {
926 case MODE_REQ:
927 if (!IsAccepted(ipcp->cfg.vj.neg)) {
928 memcpy(dec->rejend, cp, length);
929 dec->rejend += length;
930 } else {
931 switch (length) {
932 case 4: /* RFC1172 */
933 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
934 log_Printf(LogWARN, "Peer is speaking RFC1172 compression "
935 "protocol !\n");
936 ipcp->heis1172 = 1;
937 ipcp->peer_compproto = compproto;
938 memcpy(dec->ackend, cp, length);
939 dec->ackend += length;
940 } else {
941 memcpy(dec->nakend, cp, 2);
942 pcomp->proto = htons(PROTO_VJCOMP);
943 memcpy(dec->nakend+2, &pcomp, 2);
944 dec->nakend += length;
945 }
946 break;
947 case 6: /* RFC1332 */
948 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
949 if (pcomp->slots <= MAX_VJ_STATES
950 && pcomp->slots >= MIN_VJ_STATES) {
951 /* Ok, we can do that */
952 ipcp->peer_compproto = compproto;
953 ipcp->heis1172 = 0;
954 memcpy(dec->ackend, cp, length);
955 dec->ackend += length;
956 } else {
957 /* Get as close as we can to what he wants */
958 ipcp->heis1172 = 0;
959 memcpy(dec->nakend, cp, 2);
960 pcomp->slots = pcomp->slots < MIN_VJ_STATES ?
961 MIN_VJ_STATES : MAX_VJ_STATES;
962 memcpy(dec->nakend+2, &pcomp, sizeof pcomp);
963 dec->nakend += length;
964 }
965 } else {
966 /* What we really want */
967 memcpy(dec->nakend, cp, 2);
968 pcomp->proto = htons(PROTO_VJCOMP);
969 pcomp->slots = DEF_VJ_STATES;
970 pcomp->compcid = 1;
971 memcpy(dec->nakend+2, &pcomp, sizeof pcomp);
972 dec->nakend += length;
973 }
974 break;
975 default:
976 memcpy(dec->rejend, cp, length);
977 dec->rejend += length;
978 break;
979 }
980 }
981 break;
982
983 case MODE_NAK:
984 if (ntohs(pcomp->proto) == PROTO_VJCOMP) {
985 if (pcomp->slots > MAX_VJ_STATES)
986 pcomp->slots = MAX_VJ_STATES;
987 else if (pcomp->slots < MIN_VJ_STATES)
988 pcomp->slots = MIN_VJ_STATES;
989 compproto = (ntohs(pcomp->proto) << 16) + (pcomp->slots << 8) +
990 pcomp->compcid;
991 } else
992 compproto = 0;
993 log_Printf(LogIPCP, "%s changing compproto: %08x --> %08x\n",
994 tbuff, ipcp->my_compproto, compproto);
995 ipcp->my_compproto = compproto;
996 break;
997
998 case MODE_REJ:
999 ipcp->peer_reject |= (1 << type);
1000 break;
1001 }
1002 break;
1003
1004 case TY_IPADDRS: /* RFC1172 */
1005 memcpy(&ipaddr.s_addr, cp + 2, 4);
1006 memcpy(&dstipaddr.s_addr, cp + 6, 4);
1007 snprintf(tbuff2, sizeof tbuff2, "%s %s,", tbuff, inet_ntoa(ipaddr));
1008 log_Printf(LogIPCP, "%s %s\n", tbuff2, inet_ntoa(dstipaddr));
1009
1010 switch (mode_type) {
1011 case MODE_REQ:
1012 memcpy(dec->rejend, cp, length);
1013 dec->rejend += length;
1014 break;
1015
1016 case MODE_NAK:
1017 case MODE_REJ:
1018 break;
1019 }
1020 break;
1021
1022 case TY_PRIMARY_DNS: /* DNS negotiation (rfc1877) */
1023 case TY_SECONDARY_DNS:
1024 memcpy(&ipaddr.s_addr, cp + 2, 4);
1025 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1026
1027 switch (mode_type) {
1028 case MODE_REQ:
1029 if (!IsAccepted(ipcp->cfg.ns.dns_neg)) {
1030 ipcp->my_reject |= (1 << (type - TY_ADJUST_NS));
1031 memcpy(dec->rejend, cp, length);
1032 dec->rejend += length;
1033 break;
1034 }
1035 if (!gotdns) {
1036 dns[0] = ipcp->cfg.ns.dns[0];
1037 dns[1] = ipcp->cfg.ns.dns[1];
1038 if (dns[0].s_addr == INADDR_ANY && dns[1].s_addr == INADDR_ANY)
1039 getdns(ipcp, dns);
1040 gotdns = 1;
1041 }
1042 have_ip = dns[type == TY_PRIMARY_DNS ? 0 : 1];
1043
1044 if (ipaddr.s_addr != have_ip.s_addr) {
1045 /*
1046 * The client has got the DNS stuff wrong (first request) so
1047 * we'll tell 'em how it is
1048 */
1049 memcpy(dec->nakend, cp, 2); /* copy first two (type/length) */
1050 memcpy(dec->nakend + 2, &have_ip.s_addr, length - 2);
1051 dec->nakend += length;
1052 } else {
1053 /*
1054 * Otherwise they have it right (this time) so we send a ack packet
1055 * back confirming it... end of story
1056 */
1057 memcpy(dec->ackend, cp, length);
1058 dec->ackend += length;
1059 }
1060 break;
1061
1062 case MODE_NAK: /* what does this mean?? */
1063 if (IsEnabled(ipcp->cfg.ns.dns_neg)) {
1064 gotdnsnak = 1;
1065 memcpy(&dnsnak[type == TY_PRIMARY_DNS ? 0 : 1].s_addr, cp + 2, 4);
1066 }
1067 break;
1068
1069 case MODE_REJ: /* Can't do much, stop asking */
1070 ipcp->peer_reject |= (1 << (type - TY_ADJUST_NS));
1071 break;
1072 }
1073 break;
1074
1075 case TY_PRIMARY_NBNS: /* M$ NetBIOS nameserver hack (rfc1877) */
1076 case TY_SECONDARY_NBNS:
1077 memcpy(&ipaddr.s_addr, cp + 2, 4);
1078 log_Printf(LogIPCP, "%s %s\n", tbuff, inet_ntoa(ipaddr));
1079
1080 switch (mode_type) {
1081 case MODE_REQ:
1082 have_ip.s_addr =
1083 ipcp->cfg.ns.nbns[type == TY_PRIMARY_NBNS ? 0 : 1].s_addr;
1084
1085 if (have_ip.s_addr == INADDR_ANY) {
1086 log_Printf(LogIPCP, "NBNS REQ - rejected - nbns not set\n");
1087 ipcp->my_reject |= (1 << (type - TY_ADJUST_NS));
1088 memcpy(dec->rejend, cp, length);
1089 dec->rejend += length;
1090 break;
1091 }
1092
1093 if (ipaddr.s_addr != have_ip.s_addr) {
1094 memcpy(dec->nakend, cp, 2);
1095 memcpy(dec->nakend+2, &have_ip.s_addr, length);
1096 dec->nakend += length;
1097 } else {
1098 memcpy(dec->ackend, cp, length);
1099 dec->ackend += length;
1100 }
1101 break;
1102
1103 case MODE_NAK:
1104 log_Printf(LogIPCP, "MS NBNS req %d - NAK??\n", type);
1105 break;
1106
1107 case MODE_REJ:
1108 log_Printf(LogIPCP, "MS NBNS req %d - REJ??\n", type);
1109 break;
1110 }
1111 break;
1112
1113 default:
1114 if (mode_type != MODE_NOP) {
1115 ipcp->my_reject |= (1 << type);
1116 memcpy(dec->rejend, cp, length);
1117 dec->rejend += length;
1118 }
1119 break;
1120 }
1121 plen -= length;
1122 cp += length;
1123 }
1124
1125 if (gotdnsnak)
1126 if (!setdns(ipcp, dnsnak)) {
1127 ipcp->peer_reject |= (1 << (TY_PRIMARY_DNS - TY_ADJUST_NS));
1128 ipcp->peer_reject |= (1 << (TY_SECONDARY_DNS - TY_ADJUST_NS));
1129 }
1130
1131 if (mode_type != MODE_NOP) {
1132 if (dec->rejend != dec->rej) {
1133 /* rejects are preferred */
1134 dec->ackend = dec->ack;
1135 dec->nakend = dec->nak;
1136 } else if (dec->nakend != dec->nak)
1137 /* then NAKs */
1138 dec->ackend = dec->ack;
1139 }
1140}
1141
1142extern struct mbuf *
1143ipcp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
1144{
1145 /* Got PROTO_IPCP from link */
1146 if (bundle_Phase(bundle) == PHASE_NETWORK)
1147 fsm_Input(&bundle->ncp.ipcp.fsm, bp);
1148 else {
1149 if (bundle_Phase(bundle) < PHASE_NETWORK)
1150 log_Printf(LogIPCP, "%s: Error: Unexpected IPCP in phase %s (ignored)\n",
1151 l->name, bundle_PhaseName(bundle));
1152 mbuf_Free(bp);
1153 }
1154 return NULL;
1155}
1156
1157int
1158ipcp_UseHisIPaddr(struct bundle *bundle, struct in_addr hisaddr)
1159{
1160 struct ipcp *ipcp = &bundle->ncp.ipcp;
1161
1162 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1163 iplist_reset(&ipcp->cfg.peer_list);
1164 ipcp->peer_ip = ipcp->cfg.peer_range.ipaddr = hisaddr;
1165 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
1166 ipcp->cfg.peer_range.width = 32;
1167
1168 if (ipcp_SetIPaddress(bundle, ipcp->cfg.my_range.ipaddr, hisaddr, 0) < 0)
1169 return 0;
1170
1171 return 1; /* Ok */
1172}
1173
1174int
1175ipcp_UseHisaddr(struct bundle *bundle, const char *hisaddr, int setaddr)
1176{
1177 struct ipcp *ipcp = &bundle->ncp.ipcp;
1178
1179 /* Use `hisaddr' for the peers address (set iface if `setaddr') */
1180 memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1181 iplist_reset(&ipcp->cfg.peer_list);
1182 if (strpbrk(hisaddr, ",-")) {
1183 iplist_setsrc(&ipcp->cfg.peer_list, hisaddr);
1184 if (iplist_isvalid(&ipcp->cfg.peer_list)) {
1185 iplist_setrandpos(&ipcp->cfg.peer_list);
1186 ipcp->peer_ip = ChooseHisAddr(bundle, ipcp->my_ip);
1187 if (ipcp->peer_ip.s_addr == INADDR_ANY) {
1188 log_Printf(LogWARN, "%s: None available !\n", ipcp->cfg.peer_list.src);
1189 return(0);
1190 }
1191 ipcp->cfg.peer_range.ipaddr.s_addr = ipcp->peer_ip.s_addr;
1192 ipcp->cfg.peer_range.mask.s_addr = INADDR_BROADCAST;
1193 ipcp->cfg.peer_range.width = 32;
1194 } else {
1195 log_Printf(LogWARN, "%s: Invalid range !\n", hisaddr);
1196 return 0;
1197 }
1198 } else if (ParseAddr(ipcp, hisaddr, &ipcp->cfg.peer_range.ipaddr,
1199 &ipcp->cfg.peer_range.mask,
1200 &ipcp->cfg.peer_range.width) != 0) {
1201 ipcp->peer_ip.s_addr = ipcp->cfg.peer_range.ipaddr.s_addr;
1202
1203 if (setaddr && ipcp_SetIPaddress(bundle, ipcp->cfg.my_range.ipaddr,
1204 ipcp->cfg.peer_range.ipaddr, 0) < 0)
1205 return 0;
1206 } else
1207 return 0;
1208
1209 return 1;
1210}
1211
1212struct in_addr
1213addr2mask(struct in_addr addr)
1214{
1215 u_int32_t haddr = ntohl(addr.s_addr);
1216
1217 haddr = IN_CLASSA(haddr) ? IN_CLASSA_NET :
1218 IN_CLASSB(haddr) ? IN_CLASSB_NET :
1219 IN_CLASSC_NET;
1220 addr.s_addr = htonl(haddr);
1221
1222 return addr;
1223}