IfConfig 22.8 KB
Newer Older
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
/*
 * Copyright (c) 1983, 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.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
#endif /* not lint */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <net/if.h>
49
#include <net/if_var.h>
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
#include <net/if_dl.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>

#define	NSIP
#include <netns/ns.h>
#include <netns/ns_if.h>
#include <netdb.h>

#define EON
#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <sys/protosw.h>

#include <ctype.h>
#include <err.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nlist.h>
#include <kvm.h>
#ifdef __riscos
#include <sys/fcntl.h>
#include <riscos.h>
#include "swis.h"
Ben Avison's avatar
Ben Avison committed
78 79
#include "socklib.h"
#include "unixlib.h"
80
const char *__progname = "ifconfig";
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
#else
#include <fcntl.h>
#endif

struct	ifreq		ifr, ridreq;
struct	ifaliasreq	addreq;
struct	iso_ifreq	iso_ridreq;
struct	iso_aliasreq	iso_addreq;
struct	sockaddr_in	netmask;

char	name[30];
int	flags;
int	metric;
int	mtu;
int	nsellength = 1;
int	setaddr;
int	setipdst;
int	doalias;
int	clearaddr;
int	newaddr = 1;
int	s = -1;
kvm_t	*kvmd;
extern	int errno;
int	errvarflag;

Ben Avison's avatar
Ben Avison committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
void	setifflags();
void    setifaddr();
void    setifdstaddr();
void    setifnetmask();
void	setifmetric();
void    setifmtu();
void    setifbroadaddr();
void    setifipdst();
void	notealias();
void    setsnpaoffset();
void    setnsellength();
void Perror(char *cmd);
void status(void);
void adjust_nsellength(void);
void printb(char *s, register unsigned short v, register char *bits);
121 122 123 124 125 126 127 128 129
#ifdef __riscos
char   *gstrans(const char *s);
#endif

#define	NEXTARG		0xffffff

struct	cmd {
	char	*c_name;
	int	c_parameter;		/* NEXTARG means next argv */
Ben Avison's avatar
Ben Avison committed
130
	void	(*c_func)();
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
} cmds[] = {
	{ "up",		IFF_UP,		setifflags } ,
	{ "down",	-IFF_UP,	setifflags },
	{ "arp",	-IFF_NOARP,	setifflags },
	{ "-arp",	IFF_NOARP,	setifflags },
	{ "debug",	IFF_DEBUG,	setifflags },
	{ "-debug",	-IFF_DEBUG,	setifflags },
	{ "alias",	IFF_UP,		notealias },
	{ "-alias",	-IFF_UP,	notealias },
	{ "delete",	-IFF_UP,	notealias },
#ifdef notdef
#define	EN_SWABIPS	0x1000
	{ "swabips",	EN_SWABIPS,	setifflags },
	{ "-swabips",	-EN_SWABIPS,	setifflags },
#endif
	{ "netmask",	NEXTARG,	setifnetmask },
	{ "metric",	NEXTARG,	setifmetric },
	{ "broadcast",	NEXTARG,	setifbroadaddr },
	{ "ipdst",	NEXTARG,	setifipdst },
	{ "snpaoffset",	NEXTARG,	setsnpaoffset },
	{ "nsellength",	NEXTARG,	setnsellength },
	{ "link0",	IFF_LINK0,	setifflags },
	{ "-link0",	-IFF_LINK0,	setifflags },
	{ "link1",	IFF_LINK1,	setifflags },
	{ "-link1",	-IFF_LINK1,	setifflags },
	{ "link2",	IFF_LINK2,	setifflags },
	{ "-link2",	-IFF_LINK2,	setifflags },
	{ "normal",	-IFF_LINK0,	setifflags },
	{ "compress",	IFF_LINK0,	setifflags },
	{ "noicmp",	IFF_LINK1,	setifflags },
	{ "mtu",	NEXTARG,	setifmtu },
	{ 0,		0,		setifaddr },
	{ 0,		0,		setifdstaddr },
};

/*
 * XNS support liberally adapted from code written at the University of
 * Maryland principally by James O'Toole and Chris Torek.
 */
Ben Avison's avatar
Ben Avison committed
170 171 172 173 174 175 176
void	in_status();
int     in_getaddr();
void	xns_status();
int     xns_getaddr();
void	iso_status();
int     iso_getaddr();
void	ether_status();
177 178 179 180 181

/* Known address families */
struct afswtch {
	char *af_name;
	short af_af;
Ben Avison's avatar
Ben Avison committed
182
	void (*af_status)();
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	int (*af_getaddr)();
	int af_difaddr;
	int af_aifaddr;
	caddr_t af_ridreq;
	caddr_t af_addreq;
} afs[] = {
#define C(x) ((caddr_t) &x)
	{ "inet", AF_INET, in_status, in_getaddr,
	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
	{ "ns", AF_NS, xns_status, xns_getaddr,
	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
	{ "iso", AF_ISO, iso_status, iso_getaddr,
	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
	{ "ether", AF_INET, ether_status, NULL },
	{ 0,	0,	    0,		0 }
};

struct afswtch *afp;	/*the address family being set or asked about*/

Ben Avison's avatar
Ben Avison committed
202 203
int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp, int flag);

204 205
#ifdef __riscos
static void
Ben Avison's avatar
Ben Avison committed
206
sock_close(void)
207 208 209 210 211 212 213 214
{
	if (s >= 0)
		socketclose(s);
	s = -1;
}
#endif

static void
Ben Avison's avatar
Ben Avison committed
215
printhelp(void)
216
{
217 218 219 220 221
	printf("Usage:   ifconfig [-ea] <interface> [address [dest_addr]] [parameters]\n");
	printf("         configures network interface parameters\n");
	printf("Options: -e  write any errors to Inet$Error\n");
	printf("         -a  list all present interfaces.\n");
	printf("Parameters:\n");
222
    	printf("up               Mark an interface up\n");
223 224 225 226 227 228 229 230 231
   	printf("down             Mark an interface down\n");
    	printf("arp              Enable the use of ARP\n");
    	printf("-arp             Disable the use of ARP\n");
    	printf("metric <n>       Set the routing metric to n (default 0)\n");
    	printf("netmask <mask>   Set the interface netmask to mask\n");
    	printf("broadcast <addr> Set the network broadcast to addr\n");
    	printf("alias <addr>     Set an additional interface address\n");
    	printf("delete <addr>    Remove an interface address\n");
    	printf("mtu <n>          Set the interface maximum transmission unit to n\n");
232 233 234
}


Ben Avison's avatar
Ben Avison committed
235
int main(int argc, char *argv[])
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
{
	int af = AF_INET;
	register struct afswtch *rafp;

#ifdef __riscos
	atexit(sock_close);
#endif
	if (argc > 1 && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "-h") == 0)) {
		printhelp();
		exit(0);
	}
	argc--, argv++;
	if (argc > 0 && strcmp(*argv, "-e") == 0) {
		errvarflag = 1;
		argc--; argv++;
	}
	if (argc < 1) {
		printhelp();
		exit(1);
	}
#ifdef __riscos
	strncpy(name, gstrans(*argv), sizeof(name));
#else
	strncpy(name, *argv, sizeof(name));
#endif
	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
	argc--, argv++;
	if (argc > 0) {
		for (afp = rafp = afs; rafp->af_name; rafp++)
			if (strcmp(rafp->af_name, *argv) == 0) {
				afp = rafp; argc--; argv++;
				break;
			}
		rafp = afp;
		af = ifr.ifr_addr.sa_family = rafp->af_af;
	}
	s = socket(af, SOCK_DGRAM, 0);
	if (s < 0) {
		Perror("ifconfig: socket");
	}
	if (strstr(name, "-a")) {
		struct ifconf ifc;
#define MAX_INTERFACES 50	/* Yeah right. */
		char buffer[MAX_INTERFACES * sizeof(struct ifreq)];
		struct ifreq *ifptr, *end;
		int ifflags, selectflag = -1;

		if (strstr(name, "-au"))
			selectflag = 1;
		if (strstr(name, "-ad"))
			selectflag = 0;
		ifc.ifc_len = sizeof(buffer);
		ifc.ifc_buf = buffer;
		if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
			Perror("SIOCGIFCONF");
		}
		ifflags = ifc.ifc_req->ifr_flags;
		end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
		ifptr = ifc.ifc_req;
		while (ifptr < end) {
			sprintf(ifr.ifr_name,"%s",ifptr->ifr_name);
			sprintf(name,"%s",ifptr->ifr_name);
			close(s);
			if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
				Perror("socket");
			}
			if (ifptr->ifr_flags == ifflags)
				ifconfig(argc,argv,af,rafp,selectflag);
			if(ifptr->ifr_addr.sa_len)	/* Dohw! */
				ifptr = (struct ifreq *) ((caddr_t) ifptr +
				ifptr->ifr_addr.sa_len -
				sizeof(struct sockaddr));
			ifptr++;
		}
	} else
		ifconfig(argc,argv,af,rafp, -1);

	exit (0);
}



Ben Avison's avatar
Ben Avison committed
318 319
int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp,
         int flag)
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
{
	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
		Perror("ioctl (SIOCGIFFLAGS)");
		exit(1);
	}

	switch(flag) {
	case 0:
		if (ifr.ifr_flags & IFF_UP)
			return(0);
		break;
	case 1:
		if (!(ifr.ifr_flags & IFF_UP))
			return(0);
		break;
	case -1:
	default:
		break;
	}

	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
	flags = ifr.ifr_flags;
	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
		Perror("ioctl (SIOCGIFMETRIC)");
	else
		metric = ifr.ifr_metric;
	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
		Perror("ioctl (SIOCGIFMTU)");
	else
		mtu = ifr.ifr_mtu;
	if (argc == 0) {
		status();
		return(0);
	}
	while (argc > 0) {
		register struct cmd *p;

		for (p = cmds; p->c_name; p++)
			if (strcmp(*argv, p->c_name) == 0)
				break;
		if (p->c_name == 0 && setaddr)
			p++;	/* got src, do dst */
		if (p->c_func) {
			if (p->c_parameter == NEXTARG) {
				if (argv[1] == NULL)
					errx(1, "'%s' requires argument",
					    p->c_name);
				(*p->c_func)(argv[1]);
				argc--, argv++;
			} else
				(*p->c_func)(*argv, p->c_parameter);
		}
		argc--, argv++;
	}
	if (af == AF_ISO)
		adjust_nsellength();
	if (setipdst && af==AF_NS) {
		struct nsip_req rq;
		int size = sizeof(rq);

		rq.rq_ns = addreq.ifra_addr;
		rq.rq_ip = addreq.ifra_dstaddr;

		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
			Perror("Encapsulation Routing");
	}
	if (clearaddr) {
		int ret;
		strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
		if ((ret = ioctl(s, rafp->af_difaddr, rafp->af_ridreq)) < 0) {
			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
				/* means no previous address for interface */
			} else
				Perror("ioctl (SIOCDIFADDR)");
		}
	}
	if (newaddr) {
		strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
		if (ioctl(s, rafp->af_aifaddr, rafp->af_addreq) < 0)
			Perror("ioctl (SIOCAIFADDR)");
	}
	return(0);
}
#define RIDADDR 0
#define ADDR	1
#define MASK	2
#define DSTADDR	3

/*ARGSUSED*/
Ben Avison's avatar
Ben Avison committed
409
void setifaddr(char *addr, short param)
410 411 412 413 414 415 416 417 418 419 420 421
{
	/*
	 * Delay the ioctl to set the interface addr until flags are all set.
	 * The address interpretation may depend on the flags,
	 * and the flags may change when the address is set.
	 */
	setaddr++;
	if (doalias == 0)
		clearaddr = 1;
	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
}

Ben Avison's avatar
Ben Avison committed
422
void setifnetmask(char *addr)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
	/*
	 * 950504 KWelton
	 *
	 * allow a special netmask of "default", which lets the Internet module
	 * set a default netmask as if this cmd line option was never supplied
	 */
#ifdef __riscos
	/*
	 * gstrans() copies data into a static area - use our
	 * own buffer to avoid problems when gstrans() is
	 * called from in_getaddr()
	 */
	char addrbuf[16];

	addr = strncpy(addrbuf, gstrans(addr), sizeof(addrbuf) - 1);
	if (strcasecmp(addr, "default"))
#endif
	(*afp->af_getaddr)(addr, MASK);
}

Ben Avison's avatar
Ben Avison committed
444
void setifbroadaddr(char *addr)
445 446 447 448
{
	(*afp->af_getaddr)(addr, DSTADDR);
}

Ben Avison's avatar
Ben Avison committed
449
void setifipdst(char *addr)
450 451 452 453 454 455 456 457
{
	in_getaddr(addr, DSTADDR);
	setipdst++;
	clearaddr = 0;
	newaddr = 0;
}
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
/*ARGSUSED*/
Ben Avison's avatar
Ben Avison committed
458
void notealias(char *addr, int param)
459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
	if (setaddr && doalias == 0 && param < 0)
		bcopy((caddr_t)rqtosa(af_addreq),
		      (caddr_t)rqtosa(af_ridreq),
		      rqtosa(af_addreq)->sa_len);
	doalias = param;
	if (param < 0) {
		clearaddr = 1;
		newaddr = 0;
	} else
		clearaddr = 0;
}

/*ARGSUSED*/
Ben Avison's avatar
Ben Avison committed
473
void setifdstaddr(char *addr, int param)
474 475 476 477
{
	(*afp->af_getaddr)(addr, DSTADDR);
}

Ben Avison's avatar
Ben Avison committed
478
void setifflags(char *vname, short value)
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
 		Perror("ioctl (SIOCGIFFLAGS)");
 		exit(1);
 	}
	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
 	flags = ifr.ifr_flags;

	if (value < 0) {
		value = -value;
		flags &= ~value;
	} else
		flags |= value;
	ifr.ifr_flags = flags;
	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
		Perror(vname);
}

Ben Avison's avatar
Ben Avison committed
497
void setifmetric(char *val)
498 499 500 501 502 503 504
{
	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
	ifr.ifr_metric = atoi(val);
	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
		Perror("ioctl (set metric)");
}

Ben Avison's avatar
Ben Avison committed
505
void setifmtu(char *val)
506 507 508 509 510 511 512
{
	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
	ifr.ifr_mtu = atoi(val);
	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
		Perror("ioctl (set mtu)");
}

Ben Avison's avatar
Ben Avison committed
513
void setsnpaoffset(char *val)
514 515 516 517 518
{
	iso_addreq.ifra_snpaoffset = atoi(val);
}

#define	IFFBITS \
519 520 521
"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING"\
"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2"\
"\20MULTICAST"
522 523 524 525 526

/*
 * Print the status of the interface.  If an address family was
 * specified, show it and it only; otherwise, show them all.
 */
Ben Avison's avatar
Ben Avison committed
527
void status(void)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
{
	register struct afswtch *p = afp;
	short af = ifr.ifr_addr.sa_family;

	printf("%s: ", name);
	printb("flags", flags, IFFBITS);
	if (metric)
		printf(" metric %d", metric);
	if (mtu)
		printf(" mtu %d", mtu);
	putchar('\n');
	if ((p = afp) != NULL) {
		(*p->af_status)(1);
	} else for (p = afs; p->af_name; p++) {
		ifr.ifr_addr.sa_family = p->af_af;
		(*p->af_status)(0);
	}
}

Ben Avison's avatar
Ben Avison committed
547
void in_status(int force)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
{
	struct sockaddr_in *sin;
	char *inet_ntoa();

	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
			if (!force)
				return;
			bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
		} else
			Perror("ioctl (SIOCGIFADDR)");
	}
	sin = (struct sockaddr_in *)&ifr.ifr_addr;
	printf("        inet %s ", inet_ntoa(sin->sin_addr));
	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
		if (errno != EADDRNOTAVAIL)
			Perror("ioctl (SIOCGIFNETMASK)");
		bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
	} else
		netmask.sin_addr =
		    ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
	if (flags & IFF_POINTOPOINT) {
		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
			if (errno == EADDRNOTAVAIL)
			    bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
			else
			    Perror("ioctl (SIOCGIFDSTADDR)");
		}
		strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
		sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
		printf("--> %s ", inet_ntoa(sin->sin_addr));
	}
	printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
	if (flags & IFF_BROADCAST) {
		if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
			if (errno == EADDRNOTAVAIL)
			    bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
			else
			    Perror("ioctl (SIOCGIFADDR)");
		}
		strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
		sin = (struct sockaddr_in *)&ifr.ifr_addr;
		if (sin->sin_addr.s_addr != 0)
			printf("broadcast %s", inet_ntoa(sin->sin_addr));
	}
	putchar('\n');
}


Ben Avison's avatar
Ben Avison committed
599
void xns_status(int force)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
{
	struct sockaddr_ns *sns;

	close(s);
	s = socket(AF_NS, SOCK_DGRAM, 0);
	if (s < 0) {
		if (errno == EPROTONOSUPPORT)
			return;
		Perror("socket");
	}
	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
			if (!force)
				return;
			bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
		} else
			Perror("ioctl (SIOCGIFADDR)");
	}
	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
	printf("        ns %s ", ns_ntoa(sns->sns_addr));
	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
			if (errno == EADDRNOTAVAIL)
			    bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
			else
			    Perror("ioctl (SIOCGIFDSTADDR)");
		}
		strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
		printf("--> %s ", ns_ntoa(sns->sns_addr));
	}
	putchar('\n');
}

Ben Avison's avatar
Ben Avison committed
635
void iso_status(int force)
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
{
	struct sockaddr_iso *siso;
	struct iso_ifreq ifr;

	close(s);
	s = socket(AF_ISO, SOCK_DGRAM, 0);
	if (s < 0) {
		if (errno == EPROTONOSUPPORT)
			return;
		Perror("socket");
	}
	bzero((caddr_t)&ifr, sizeof(ifr));
	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
			if (!force)
				return;
			bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
		} else {
			Perror("ioctl (SIOCGIFADDR_ISO)");
		}
	}
	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
	siso = &ifr.ifr_Addr;
	printf("        iso %s ", iso_ntoa(&siso->siso_addr));
	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
		if (errno != EADDRNOTAVAIL)
			Perror("ioctl (SIOCGIFNETMASK_ISO)");
	} else {
		printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
	}
	if (flags & IFF_POINTOPOINT) {
		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
			if (errno == EADDRNOTAVAIL)
			    bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
			else
			    Perror("ioctl (SIOCGIFDSTADDR_ISO)");
		}
		strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
		siso = &ifr.ifr_Addr;
		printf("--> %s ", iso_ntoa(&siso->siso_addr));
	}
	putchar('\n');
}

Ben Avison's avatar
Ben Avison committed
681
int kread(u_long addr, char *buf, int size)
682 683 684 685 686 687 688 689
{

        if (kvm_read(kvmd, addr, buf, size) != size)
                return (-1);
        return (0);
}

/* Unashamedly stolen from netstat -- maybe someday we can us sysctl() */
Ben Avison's avatar
Ben Avison committed
690
void ether_status(void)
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
{
	struct nlist nl[] = { { "_ifnet" } , "" };
	u_long addr, addr2;
	struct ifnet ifnet;
	union {
		struct ifaddr ifa;
		struct in_ifaddr in;
		struct ns_ifaddr ns;
		struct iso_ifaddr iso;
	} ifaddr;
	char *cp;
	struct sockaddr *sa;
	struct sockaddr_dl *sdl;
	int n,m;
	char ifacename[IFNAMSIZ];

	/*
	 * If we fail here it probably means we don't have permission to
	 * read /dev/kmem. Best to just silently bail out. If we have
	 * an error *after* we succeed in opening /dev/kmem, then we
	 * should report it.
	 */
	if ((kvmd = kvm_open(NULL,NULL,NULL,O_RDONLY,NULL)) == NULL)
		return;
	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
		Perror("ifconfig: kvm_nlist()");
		return;
	}
	if (kread(nl[0].n_value, (char *)&addr, sizeof(addr))) {
		Perror("_ifnet");
		return;
	}
	addr2 = 0;
	while (addr || addr2) {
		if (addr2 == 0) {
                        if (kread(addr, (char *)&ifnet, sizeof ifnet) ||
                           kread((u_long)ifnet.if_name, ifacename, IFNAMSIZ)){
					Perror("ifconfig: kvm_read()");
					return;
			}
			addr = (u_long)ifnet.if_next;
			addr2 = (u_long)ifnet.if_addrlist;
		}
		if (kread(addr2, (char *)&ifaddr, sizeof ifaddr)) {
			addr2 = 0;
                        continue;
                }
		sprintf(ifacename,"%s%d",ifacename, ifnet.if_unit);
		if (!strncmp(name, ifacename, strlen(name))) {
#define CP(x) ((char *)(x))
			cp = (CP(ifaddr.ifa.ifa_addr) - CP(addr2)) +
				CP(&ifaddr); sa = (struct sockaddr *)cp;
			if (sa->sa_family == AF_LINK) {
				sdl = (struct sockaddr_dl *)sa;
				cp = (char *)LLADDR(sdl);
				if ((n = sdl->sdl_alen) > 0) {
					printf ("        ether ");
                               		while (--n >= 0)
						m += printf("%02x%c",
							*cp++ & 0xff,
							n > 0 ? ':' : ' ');
					putchar('\n');
				}
				break;
			}
		}
		addr2 = (u_long)ifaddr.ifa.ifa_next;
	}
	kvm_close(kvmd);
}

Ben Avison's avatar
Ben Avison committed
762
void Perror(char *cmd)
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
{
	extern int errno;
	const char *errmess;
	char errtext[128];

	switch (errno) {

	case ENXIO:
		errmess = "no such interface";
		break;

	case EPERM:
		errmess = "permission denied";
		break;

	default:
		errmess = _inet_error.errmess;
	}
	if (!errvarflag)
		fprintf(stderr, "%s: %s: %s\n", __progname, cmd, errmess);
	else
	{
		sprintf(errtext, "%s: %s: %s", __progname, cmd, errmess);
		_kernel_setenv("Inet$Error", errtext);
	}
	exit(1);
}

struct	in_addr inet_makeaddr();

#define SIN(x) ((struct sockaddr_in *) &(x))
struct sockaddr_in *sintab[] = {
SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};

Ben Avison's avatar
Ben Avison committed
798
int in_getaddr(char *s, int which)
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
	register struct sockaddr_in *sin = sintab[which];
	struct hostent *hp;
	struct netent *np;
	int val;

#ifdef __riscos
	s = gstrans(s);
#endif
	sin->sin_len = sizeof(*sin);
	if (which != MASK)
		sin->sin_family = AF_INET;

	if (inet_aton(s, &sin->sin_addr))
		;
	else if (hp = gethostbyname(s))
		bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
	else if (np = getnetbyname(s))
		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
	else
		errx(1, "%s: bad value", s);
}

/*
 * Print a value a la the %b format of the kernel's printf
 */
Ben Avison's avatar
Ben Avison committed
825
void printb(char *s, register unsigned short v, register char *bits)
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
{
	register int i, any = 0;
	register char c;

	if (bits && *bits == 8)
		printf("%s=%o", s, v);
	else
		printf("%s=%x", s, v);
	bits++;
	if (bits) {
		putchar('<');
		while (i = *bits++) {
			if (v & (1 << (i-1))) {
				if (any)
					putchar(',');
				any = 1;
				for (; (c = *bits) > 32; bits++)
					putchar(c);
			} else
				for (; *bits > 32; bits++)
					;
		}
		putchar('>');
	}
}

#define SNS(x) ((struct sockaddr_ns *) &(x))
struct sockaddr_ns *snstab[] = {
SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};

Ben Avison's avatar
Ben Avison committed
857
int xns_getaddr(char *addr, int which)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
{
	struct sockaddr_ns *sns = snstab[which];
	struct ns_addr ns_addr();

	sns->sns_family = AF_NS;
	sns->sns_len = sizeof(*sns);
	sns->sns_addr = ns_addr(addr);
	if (which == MASK)
		printf("Attempt to set XNS netmask will be ineffectual\n");
}

#define SISO(x) ((struct sockaddr_iso *) &(x))
struct sockaddr_iso *sisotab[] = {
SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};

Ben Avison's avatar
Ben Avison committed
874
int iso_getaddr(char *addr, int which)
875 876 877 878 879 880 881 882 883 884 885 886 887 888
{
	register struct sockaddr_iso *siso = sisotab[which];
	struct iso_addr *iso_addr();
	siso->siso_addr = *iso_addr(addr);

	if (which == MASK) {
		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
		siso->siso_nlen = 0;
	} else {
		siso->siso_len = sizeof(*siso);
		siso->siso_family = AF_ISO;
	}
}

Ben Avison's avatar
Ben Avison committed
889
void setnsellength(char *val)
890 891 892 893 894 895 896 897
{
	nsellength = atoi(val);
	if (nsellength < 0)
		errx(1, "Negative NSEL length is absurd");
	if (afp == 0 || afp->af_af != AF_ISO)
		errx(1, "Setting NSEL length valid only for iso");
}

Ben Avison's avatar
Ben Avison committed
898
void fixnsel(register struct sockaddr_iso *s)
899 900 901 902 903 904
{
	if (s->siso_family == 0)
		return;
	s->siso_tlen = nsellength;
}

Ben Avison's avatar
Ben Avison committed
905
void adjust_nsellength(void)
906 907 908 909 910 911 912 913
{
	fixnsel(sisotab[RIDADDR]);
	fixnsel(sisotab[ADDR]);
	fixnsel(sisotab[DSTADDR]);
}

#ifdef __riscos
char *
Ben Avison's avatar
Ben Avison committed
914
gstrans(const char *s)
915 916 917 918 919 920 921 922 923 924
{
	static char gstbuf[256];
	int len;

	gstbuf[0] = 0;
	_swix(OS_GSTrans, _INR(0,2)|_OUT(2), s, gstbuf, sizeof(gstbuf), &len);
	gstbuf[len] = 0;
	return (gstbuf);
}
#endif