Home | History | Annotate | Line # | Download | only in ifconfig
af_inet.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: af_inet.c,v 1.1 2005/03/20 02:43:50 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1983, 1993
      5  1.1  thorpej  *      The Regents of the University of California.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     17  1.1  thorpej  *    without specific prior written permission.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  thorpej  * SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/cdefs.h>
     33  1.1  thorpej #ifndef lint
     34  1.1  thorpej __RCSID("$NetBSD: af_inet.c,v 1.1 2005/03/20 02:43:50 thorpej Exp $");
     35  1.1  thorpej #endif /* not lint */
     36  1.1  thorpej 
     37  1.1  thorpej #include <sys/param.h>
     38  1.1  thorpej #include <sys/ioctl.h>
     39  1.1  thorpej #include <sys/socket.h>
     40  1.1  thorpej 
     41  1.1  thorpej #include <net/if.h>
     42  1.1  thorpej #include <netinet/in.h>
     43  1.1  thorpej #include <netinet/in_var.h>
     44  1.1  thorpej 
     45  1.1  thorpej #include <arpa/inet.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <err.h>
     48  1.1  thorpej #include <errno.h>
     49  1.1  thorpej #include <ifaddrs.h>
     50  1.1  thorpej #include <netdb.h>
     51  1.1  thorpej #include <string.h>
     52  1.1  thorpej #include <stdlib.h>
     53  1.1  thorpej #include <stdio.h>
     54  1.1  thorpej 
     55  1.1  thorpej #include "extern.h"
     56  1.1  thorpej #include "af_inet.h"
     57  1.1  thorpej 
     58  1.1  thorpej struct in_aliasreq in_addreq;
     59  1.1  thorpej 
     60  1.1  thorpej int	setipdst;
     61  1.1  thorpej 
     62  1.1  thorpej void
     63  1.1  thorpej setifipdst(const char *addr, int d)
     64  1.1  thorpej {
     65  1.1  thorpej 
     66  1.1  thorpej 	in_getaddr(addr, DSTADDR);
     67  1.1  thorpej 	setipdst++;
     68  1.1  thorpej 	clearaddr = 0;
     69  1.1  thorpej 	newaddr = 0;
     70  1.1  thorpej }
     71  1.1  thorpej 
     72  1.1  thorpej void
     73  1.1  thorpej in_alias(struct ifreq *creq)
     74  1.1  thorpej {
     75  1.1  thorpej 	struct sockaddr_in *iasin;
     76  1.1  thorpej 	int alias;
     77  1.1  thorpej 
     78  1.1  thorpej 	if (lflag)
     79  1.1  thorpej 		return;
     80  1.1  thorpej 
     81  1.1  thorpej 	alias = 1;
     82  1.1  thorpej 
     83  1.1  thorpej 	/* Get the non-alias address for this interface. */
     84  1.1  thorpej 	getsock(AF_INET);
     85  1.1  thorpej 	if (s < 0) {
     86  1.1  thorpej 		if (errno == EPROTONOSUPPORT)
     87  1.1  thorpej 			return;
     88  1.1  thorpej 		err(EXIT_FAILURE, "socket");
     89  1.1  thorpej 	}
     90  1.1  thorpej 	(void) memset(&ifr, 0, sizeof(ifr));
     91  1.1  thorpej 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
     92  1.1  thorpej 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
     93  1.1  thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
     94  1.1  thorpej 			return;
     95  1.1  thorpej 		} else
     96  1.1  thorpej 			warn("SIOCGIFADDR");
     97  1.1  thorpej 	}
     98  1.1  thorpej 	/* If creq and ifr are the same address, this is not an alias. */
     99  1.1  thorpej 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
    100  1.1  thorpej 		   sizeof(creq->ifr_addr)) == 0)
    101  1.1  thorpej 		alias = 0;
    102  1.1  thorpej 	(void) memset(&in_addreq, 0, sizeof(in_addreq));
    103  1.1  thorpej 	(void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
    104  1.1  thorpej 	memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
    105  1.1  thorpej 	    sizeof(in_addreq.ifra_addr));
    106  1.1  thorpej 	if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
    107  1.1  thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    108  1.1  thorpej 			return;
    109  1.1  thorpej 		} else
    110  1.1  thorpej 			warn("SIOCGIFALIAS");
    111  1.1  thorpej 	}
    112  1.1  thorpej 
    113  1.1  thorpej 	iasin = &in_addreq.ifra_addr;
    114  1.1  thorpej 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
    115  1.1  thorpej 
    116  1.1  thorpej 	if (flags & IFF_POINTOPOINT) {
    117  1.1  thorpej 		iasin = &in_addreq.ifra_dstaddr;
    118  1.1  thorpej 		printf(" -> %s", inet_ntoa(iasin->sin_addr));
    119  1.1  thorpej 	}
    120  1.1  thorpej 
    121  1.1  thorpej 	iasin = &in_addreq.ifra_mask;
    122  1.1  thorpej 	printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
    123  1.1  thorpej 
    124  1.1  thorpej 	if (flags & IFF_BROADCAST) {
    125  1.1  thorpej 		iasin = &in_addreq.ifra_broadaddr;
    126  1.1  thorpej 		printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
    127  1.1  thorpej 	}
    128  1.1  thorpej 	printf("\n");
    129  1.1  thorpej }
    130  1.1  thorpej 
    131  1.1  thorpej void
    132  1.1  thorpej in_status(int force)
    133  1.1  thorpej {
    134  1.1  thorpej 	struct ifaddrs *ifap, *ifa;
    135  1.1  thorpej 	struct ifreq isifr;
    136  1.1  thorpej 
    137  1.1  thorpej 	if (getifaddrs(&ifap) != 0)
    138  1.1  thorpej 		err(EXIT_FAILURE, "getifaddrs");
    139  1.1  thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    140  1.1  thorpej 		if (strcmp(name, ifa->ifa_name) != 0)
    141  1.1  thorpej 			continue;
    142  1.1  thorpej 		if (ifa->ifa_addr->sa_family != AF_INET)
    143  1.1  thorpej 			continue;
    144  1.1  thorpej 		if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
    145  1.1  thorpej 			continue;
    146  1.1  thorpej 
    147  1.1  thorpej 		memset(&isifr, 0, sizeof(isifr));
    148  1.1  thorpej 		strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
    149  1.1  thorpej 		memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    150  1.1  thorpej 		in_alias(&isifr);
    151  1.1  thorpej 	}
    152  1.1  thorpej 	freeifaddrs(ifap);
    153  1.1  thorpej }
    154  1.1  thorpej 
    155  1.1  thorpej #define SIN(x) ((struct sockaddr_in *) &(x))
    156  1.1  thorpej struct sockaddr_in *sintab[] = {
    157  1.1  thorpej     SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
    158  1.1  thorpej     SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
    159  1.1  thorpej 
    160  1.1  thorpej void
    161  1.1  thorpej in_getaddr(const char *str, int which)
    162  1.1  thorpej {
    163  1.1  thorpej 	struct sockaddr_in *gasin = sintab[which];
    164  1.1  thorpej 	struct hostent *hp;
    165  1.1  thorpej 	struct netent *np;
    166  1.1  thorpej 
    167  1.1  thorpej 	gasin->sin_len = sizeof(*gasin);
    168  1.1  thorpej 	if (which != MASK)
    169  1.1  thorpej 		gasin->sin_family = AF_INET;
    170  1.1  thorpej 
    171  1.1  thorpej 	if (which == ADDR) {
    172  1.1  thorpej 		char *p = NULL;
    173  1.1  thorpej 		if ((p = strrchr(str, '/')) != NULL) {
    174  1.1  thorpej 			*p = '\0';
    175  1.1  thorpej 			in_getprefix(p + 1, MASK);
    176  1.1  thorpej 		}
    177  1.1  thorpej 	}
    178  1.1  thorpej 
    179  1.1  thorpej 	if (inet_aton(str, &gasin->sin_addr) == 0) {
    180  1.1  thorpej 		if ((hp = gethostbyname(str)) != NULL)
    181  1.1  thorpej 			(void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
    182  1.1  thorpej 		else if ((np = getnetbyname(str)) != NULL)
    183  1.1  thorpej 			gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
    184  1.1  thorpej 		else
    185  1.1  thorpej 			errx(EXIT_FAILURE, "%s: bad value", str);
    186  1.1  thorpej 	}
    187  1.1  thorpej }
    188  1.1  thorpej 
    189  1.1  thorpej void
    190  1.1  thorpej in_getprefix(const char *plen, int which)
    191  1.1  thorpej {
    192  1.1  thorpej 	struct sockaddr_in *igsin = sintab[which];
    193  1.1  thorpej 	u_char *cp;
    194  1.1  thorpej 	int len = strtol(plen, (char **)NULL, 10);
    195  1.1  thorpej 
    196  1.1  thorpej 	if ((len < 0) || (len > 32))
    197  1.1  thorpej 		errx(EXIT_FAILURE, "%s: bad value", plen);
    198  1.1  thorpej 	igsin->sin_len = sizeof(*igsin);
    199  1.1  thorpej 	if (which != MASK)
    200  1.1  thorpej 		igsin->sin_family = AF_INET;
    201  1.1  thorpej 	if ((len == 0) || (len == 32)) {
    202  1.1  thorpej 		memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
    203  1.1  thorpej 		return;
    204  1.1  thorpej 	}
    205  1.1  thorpej 	memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
    206  1.1  thorpej 	for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
    207  1.1  thorpej 		*cp++ = 0xff;
    208  1.1  thorpej 	if (len)
    209  1.1  thorpej 		*cp = 0xff << (8 - len);
    210  1.1  thorpej }
    211