1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* From: @(#)startup.c 5.19 (Berkeley) 2/28/91
* From: @(#)startup.c 8.1 (Berkeley) 6/5/93
*/
char startup_rcsid[] =
"$Id: startup.c,v 1.7 1996/11/25 17:28:24 dholland Exp $";
/*
* Routing Table Management Daemon
*/
#include "defs.h"
#include <sys/ioctl.h>
#include <net/if.h>
#include <syslog.h>
#include <stdlib.h>
#include <errno.h>
#include "pathnames.h"
struct interface *ifnet;
struct interface **ifnext = &ifnet;
int lookforinterfaces = 1;
int foundloopback; /* valid flag for loopaddr */
struct sockaddr loopaddr; /* our address on loopback */
static int externalinterfaces = 0; /* # of remote and local interfaces */
void add_ptopt_localrt(struct interface *);
int getnetorhostname(char *, char *, struct sockaddr_in *);
int gethostnameornumber(char *, struct sockaddr_in *);
void quit(char *s)
{
int sverrno = errno;
(void) fprintf(stderr, "route: ");
if (s)
(void) fprintf(stderr, "%s: ", s);
(void) fprintf(stderr, "%s\n", strerror(sverrno));
exit(1);
}
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
/*
* Find the network interfaces which have configured themselves.
* If the interface is present but not yet up (for example an
* ARPANET IMP), set the lookforinterfaces flag so we'll
* come back later and look again.
*/
void ifinit(void)
{
struct interface ifs, *ifp;
int s;
char buf[BUFSIZ], *cp, *cplim;
struct ifconf ifc;
struct ifreq ifreq, *ifr;
struct sockaddr_in *sin;
u_long i;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
syslog(LOG_ERR, "socket: %m");
close(s);
return;
}
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
syslog(LOG_ERR, "ioctl (get interface configuration)");
close(s);
return;
}
ifr = ifc.ifc_req;
lookforinterfaces = 0;
#define size(p) (sizeof (p))
cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
for (cp = buf; cp < cplim;
cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
ifr = (struct ifreq *)cp;
bzero((char *)&ifs, sizeof(ifs));
ifs.int_addr = ifr->ifr_addr;
ifreq = *ifr;
if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
syslog(LOG_ERR, "%s: ioctl (get interface flags)",
ifr->ifr_name);
continue;
}
ifs.int_flags =
ifreq.ifr_flags | IFF_INTERFACE;
if ((ifs.int_flags & IFF_UP) == 0 ||
ifr->ifr_addr.sa_family == AF_UNSPEC) {
lookforinterfaces = 1;
continue;
}
/* argh, this'll have to change sometime */
if (ifs.int_addr.sa_family != AF_INET)
continue;
if (ifs.int_flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
syslog(LOG_ERR, "%s: ioctl (get dstaddr)",
ifr->ifr_name);
continue;
}
if (ifr->ifr_addr.sa_family == AF_UNSPEC) {
lookforinterfaces = 1;
continue;
}
ifs.int_dstaddr = ifreq.ifr_dstaddr;
}
/*
* already known to us?
* This allows multiple point-to-point links
* to share a source address (possibly with one
* other link), but assumes that there will not be
* multiple links with the same destination address.
*/
if (ifs.int_flags & IFF_POINTOPOINT) {
if (if_ifwithdstaddr(&ifs.int_dstaddr))
continue;
} else if (if_ifwithaddr(&ifs.int_addr))
continue;
if (ifs.int_flags & IFF_LOOPBACK) {
ifs.int_flags |= IFF_PASSIVE;
foundloopback = 1;
loopaddr = ifs.int_addr;
for (ifp = ifnet; ifp; ifp = ifp->int_next)
if (ifp->int_flags & IFF_POINTOPOINT)
add_ptopt_localrt(ifp);
}
if (ifs.int_flags & IFF_BROADCAST) {
if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
syslog(LOG_ERR, "%s: ioctl (get broadaddr)",
ifr->ifr_name);
continue;
}
ifs.int_broadaddr = ifreq.ifr_broadaddr;
}
#ifdef SIOCGIFMETRIC
if (ioctl(s, SIOCGIFMETRIC, (char *)&ifreq) < 0) {
syslog(LOG_ERR, "%s: ioctl (get metric)",
ifr->ifr_name);
ifs.int_metric = 0;
} else
ifs.int_metric = ifreq.ifr_metric;
#else
ifs.int_metric = 0;
#endif
/*
* Use a minimum metric of one;
* treat the interface metric (default 0)
* as an increment to the hop count of one.
*/
ifs.int_metric++;
if (ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
syslog(LOG_ERR, "%s: ioctl (get netmask)",
ifr->ifr_name);
continue;
}
sin = (struct sockaddr_in *)&ifreq.ifr_addr;
ifs.int_subnetmask = ntohl(sin->sin_addr.s_addr);
sin = (struct sockaddr_in *)&ifs.int_addr;
i = ntohl(sin->sin_addr.s_addr);
if (IN_CLASSA(i))
ifs.int_netmask = IN_CLASSA_NET;
else if (IN_CLASSB(i))
ifs.int_netmask = IN_CLASSB_NET;
else
ifs.int_netmask = IN_CLASSC_NET;
ifs.int_net = i & ifs.int_netmask;
ifs.int_subnet = i & ifs.int_subnetmask;
if (ifs.int_subnetmask != ifs.int_netmask)
ifs.int_flags |= IFF_SUBNET;
ifp = (struct interface *)malloc(sizeof (struct interface));
if (ifp == 0) {
printf("routed: out of memory\n");
break;
}
*ifp = ifs;
/*
* Count the # of directly connected networks
* and point to point links which aren't looped
* back to ourself. This is used below to
* decide if we should be a routing ``supplier''.
*/
if ((ifs.int_flags & IFF_LOOPBACK) == 0 &&
((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
if_ifwithaddr(&ifs.int_dstaddr) == 0))
externalinterfaces++;
/*
* If we have a point-to-point link, we want to act
* as a supplier even if it's our only interface,
* as that's the only way our peer on the other end
* can tell that the link is up.
*/
if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0)
supplier = 1;
ifp->int_name = malloc(strlen(ifr->ifr_name) + 1);
if (ifp->int_name == 0) {
fprintf(stderr, "routed: ifinit: out of memory\n");
syslog(LOG_ERR, "routed: ifinit: out of memory\n");
close(s);
return;
}
strcpy(ifp->int_name, ifr->ifr_name);
*ifnext = ifp;
ifnext = &ifp->int_next;
traceinit(ifp);
addrouteforif(ifp);
}
if (externalinterfaces > 1 && supplier < 0)
supplier = 1;
close(s);
}
/*
* Add route for interface if not currently installed.
* Create route to other end if a point-to-point link,
* otherwise a route to this (sub)network.
* INTERNET SPECIFIC.
*/
void addrouteforif(struct interface *ifp)
{
struct sockaddr_in net;
struct sockaddr *dst;
int state;
struct rt_entry *rt;
if (ifp->int_flags & IFF_POINTOPOINT)
dst = &ifp->int_dstaddr;
else {
memset(&net, 0, sizeof (net));
net.sin_family = AF_INET;
net.sin_addr = inet_makeaddr(ifp->int_subnet, INADDR_ANY);
dst = (struct sockaddr *)&net;
}
rt = rtfind(dst);
if (rt &&
(rt->rt_state & (RTS_INTERFACE | RTS_INTERNAL)) == RTS_INTERFACE)
return;
if (rt)
rtdelete(rt);
/*
* If interface on subnetted network,
* install route to network as well.
* This is meant for external viewers.
*/
if ((ifp->int_flags & (IFF_SUBNET|IFF_POINTOPOINT)) == IFF_SUBNET) {
struct in_addr subnet;
subnet = net.sin_addr;
net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
rt = rtfind(dst);
if (rt == 0)
rtadd(dst, &ifp->int_addr, ifp->int_metric,
((ifp->int_flags & (IFF_INTERFACE|IFF_REMOTE)) |
RTS_PASSIVE | RTS_INTERNAL | RTS_SUBNET));
else if ((rt->rt_state & (RTS_INTERNAL|RTS_SUBNET)) ==
(RTS_INTERNAL|RTS_SUBNET) &&
ifp->int_metric < rt->rt_metric)
rtchange(rt, &rt->rt_router, ifp->int_metric);
net.sin_addr = subnet;
}
if (ifp->int_transitions++ > 0)
syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
state = ifp->int_flags &
(IFF_INTERFACE | IFF_PASSIVE | IFF_REMOTE | IFF_SUBNET);
if (ifp->int_flags & IFF_POINTOPOINT &&
(ntohl(((struct sockaddr_in *)&ifp->int_dstaddr)->sin_addr.s_addr) &
ifp->int_netmask) != ifp->int_net)
state &= ~RTS_SUBNET;
if (ifp->int_flags & IFF_LOOPBACK)
state |= RTS_EXTERNAL;
rtadd(dst, &ifp->int_addr, ifp->int_metric, state);
if (ifp->int_flags & IFF_POINTOPOINT && foundloopback)
add_ptopt_localrt(ifp);
}
/*
* Add route to local end of point-to-point using loopback.
* If a route to this network is being sent to neighbors on other nets,
* mark this route as subnet so we don't have to propagate it too.
*/
void add_ptopt_localrt(struct interface *ifp)
{
struct rt_entry *rt;
struct sockaddr *dst;
struct sockaddr_in net;
int state;
state = RTS_INTERFACE | RTS_PASSIVE;
/* look for route to logical network */
memset(&net, 0, sizeof (net));
net.sin_family = AF_INET;
net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
dst = (struct sockaddr *)&net;
rt = rtfind(dst);
if (rt && rt->rt_state & RTS_INTERNAL)
state |= RTS_SUBNET;
dst = &ifp->int_addr;
if ((rt = rtfind(dst))!=NULL) {
if (rt && rt->rt_state & RTS_INTERFACE)
return;
rtdelete(rt);
}
rtadd(dst, &loopaddr, 1, state);
}
/*
* As a concession to the ARPANET we read a list of gateways
* from /etc/gateways and add them to our tables. This file
* exists at each ARPANET gateway and indicates a set of ``remote''
* gateways (i.e. a gateway which we can't immediately determine
* if it's present or not as we can do for those directly connected
* at the hardware level). If a gateway is marked ``passive''
* in the file, then we assume it doesn't have a routing process
* of our design and simply assume it's always present. Those
* not marked passive are treated as if they were directly
* connected -- they're added into the interface list so we'll
* send them routing updates.
*
* PASSIVE ENTRIES AREN'T NEEDED OR USED ON GATEWAYS RUNNING EGP.
*/
void gwkludge(void)
{
struct sockaddr_in dst, gate;
FILE *fp;
char *type, *dname, *gname, *qual, buf[BUFSIZ];
struct interface *ifp;
int metric, n;
struct rt_entry route;
fp = fopen(_PATH_GATEWAYS, "r");
if (fp == NULL)
return;
qual = buf;
dname = buf + 64;
gname = buf + ((BUFSIZ - 64) / 3);
type = buf + (((BUFSIZ - 64) * 2) / 3);
memset(&dst, 0, sizeof (dst));
memset(&gate, 0, sizeof (gate));
memset(&route, 0, sizeof(route));
/* format: {net | host} XX gateway XX metric DD [passive | external]\n */
#define readentry(fp) \
fscanf((fp), "%s %s gateway %s metric %d %s\n", \
type, dname, gname, &metric, qual)
for (;;) {
if ((n = readentry(fp)) == EOF)
break;
/*
* Lusertrap. Vendors should ship the line
*
* CONFIGME CONFIGME gateway CONFIGME metric 1
*
*/
if (strcmp(type,"CONFIGME")==0)
{
fprintf(stderr,"Not starting gated. Please configure first.\n");
exit(1);
}
if (!getnetorhostname(type, dname, &dst))
continue;
if (!gethostnameornumber(gname, &gate))
continue;
if (metric == 0) /* XXX */
metric = 1;
if (strcmp(qual, "passive") == 0) {
/*
* Passive entries aren't placed in our tables,
* only the kernel's, so we don't copy all of the
* external routing information within a net.
* Internal machines should use the default
* route to a suitable gateway (like us).
*/
route.rt_dst = *(struct sockaddr *) &dst;
route.rt_router = *(struct sockaddr *) &gate;
route.rt_flags = RTF_UP;
if (strcmp(type, "host") == 0)
route.rt_flags |= RTF_HOST;
if (metric)
route.rt_flags |= RTF_GATEWAY;
(void) rtioctl(ADD, &route.rt_rt);
continue;
}
if (strcmp(qual, "external") == 0) {
/*
* Entries marked external are handled
* by other means, e.g. EGP,
* and are placed in our tables only
* to prevent overriding them
* with something else.
*/
rtadd((struct sockaddr *)&dst,
(struct sockaddr *)&gate, metric,
RTS_EXTERNAL|RTS_PASSIVE);
continue;
}
/* assume no duplicate entries */
externalinterfaces++;
ifp = (struct interface *)malloc(sizeof (*ifp));
memset(ifp, 0, sizeof (*ifp));
ifp->int_flags = IFF_REMOTE;
/* can't identify broadcast capability */
ifp->int_net = inet_netof_subnet(dst.sin_addr);
if (strcmp(type, "host") == 0) {
ifp->int_flags |= IFF_POINTOPOINT;
ifp->int_dstaddr = *((struct sockaddr *)&dst);
}
ifp->int_addr = *((struct sockaddr *)&gate);
ifp->int_metric = metric;
ifp->int_next = ifnet;
ifnet = ifp;
addrouteforif(ifp);
}
fclose(fp);
}
int getnetorhostname(char *type, char *name, struct sockaddr_in *sin)
{
if (strcmp(type, "net") == 0) {
struct netent *np = getnetbyname(name);
int n;
if (np == 0)
n = inet_network(name);
else {
if (np->n_addrtype != AF_INET)
return (0);
n = np->n_net;
/*
* getnetbyname returns right-adjusted value.
*/
if (n < 128)
n <<= IN_CLASSA_NSHIFT;
else if (n < 65536)
n <<= IN_CLASSB_NSHIFT;
else
n <<= IN_CLASSC_NSHIFT;
}
sin->sin_family = AF_INET;
sin->sin_addr = inet_makeaddr(n, INADDR_ANY);
return (1);
}
if (strcmp(type, "host") == 0)
return (gethostnameornumber(name, sin));
return (0);
}
int gethostnameornumber(char *name, struct sockaddr_in *sin)
{
struct hostent *hp;
if (inet_aton(name, &sin->sin_addr) == 0) {
hp = gethostbyname(name);
if (hp == 0)
return (0);
memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
sin->sin_family = hp->h_addrtype;
} else
sin->sin_family = AF_INET;
return (1);
}
|