Home | History | Annotate | Line # | Download | only in ifconfig
af_inet.c revision 1.4
      1  1.4  christos /*	$NetBSD: af_inet.c,v 1.4 2006/08/26 18:14:28 christos 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.4  christos __RCSID("$NetBSD: af_inet.c,v 1.4 2006/08/26 18:14:28 christos 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.4  christos #include <util.h>
     55  1.1   thorpej 
     56  1.1   thorpej #include "extern.h"
     57  1.1   thorpej #include "af_inet.h"
     58  1.1   thorpej 
     59  1.1   thorpej struct in_aliasreq in_addreq;
     60  1.1   thorpej 
     61  1.1   thorpej int	setipdst;
     62  1.1   thorpej 
     63  1.1   thorpej void
     64  1.1   thorpej setifipdst(const char *addr, int d)
     65  1.1   thorpej {
     66  1.1   thorpej 
     67  1.1   thorpej 	in_getaddr(addr, DSTADDR);
     68  1.1   thorpej 	setipdst++;
     69  1.1   thorpej 	clearaddr = 0;
     70  1.1   thorpej 	newaddr = 0;
     71  1.1   thorpej }
     72  1.1   thorpej 
     73  1.1   thorpej void
     74  1.1   thorpej in_alias(struct ifreq *creq)
     75  1.1   thorpej {
     76  1.1   thorpej 	struct sockaddr_in *iasin;
     77  1.1   thorpej 	int alias;
     78  1.1   thorpej 
     79  1.1   thorpej 	if (lflag)
     80  1.1   thorpej 		return;
     81  1.1   thorpej 
     82  1.1   thorpej 	alias = 1;
     83  1.1   thorpej 
     84  1.1   thorpej 	/* Get the non-alias address for this interface. */
     85  1.1   thorpej 	getsock(AF_INET);
     86  1.1   thorpej 	if (s < 0) {
     87  1.2      tron 		if (errno == EAFNOSUPPORT)
     88  1.1   thorpej 			return;
     89  1.1   thorpej 		err(EXIT_FAILURE, "socket");
     90  1.1   thorpej 	}
     91  1.1   thorpej 	(void) memset(&ifr, 0, sizeof(ifr));
     92  1.3      elad 	estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
     93  1.1   thorpej 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
     94  1.1   thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
     95  1.1   thorpej 			return;
     96  1.1   thorpej 		} else
     97  1.1   thorpej 			warn("SIOCGIFADDR");
     98  1.1   thorpej 	}
     99  1.1   thorpej 	/* If creq and ifr are the same address, this is not an alias. */
    100  1.1   thorpej 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
    101  1.1   thorpej 		   sizeof(creq->ifr_addr)) == 0)
    102  1.1   thorpej 		alias = 0;
    103  1.1   thorpej 	(void) memset(&in_addreq, 0, sizeof(in_addreq));
    104  1.3      elad 	estrlcpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
    105  1.1   thorpej 	memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
    106  1.1   thorpej 	    sizeof(in_addreq.ifra_addr));
    107  1.1   thorpej 	if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
    108  1.1   thorpej 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    109  1.1   thorpej 			return;
    110  1.1   thorpej 		} else
    111  1.1   thorpej 			warn("SIOCGIFALIAS");
    112  1.1   thorpej 	}
    113  1.1   thorpej 
    114  1.1   thorpej 	iasin = &in_addreq.ifra_addr;
    115  1.1   thorpej 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
    116  1.1   thorpej 
    117  1.1   thorpej 	if (flags & IFF_POINTOPOINT) {
    118  1.1   thorpej 		iasin = &in_addreq.ifra_dstaddr;
    119  1.1   thorpej 		printf(" -> %s", inet_ntoa(iasin->sin_addr));
    120  1.1   thorpej 	}
    121  1.1   thorpej 
    122  1.1   thorpej 	iasin = &in_addreq.ifra_mask;
    123  1.1   thorpej 	printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
    124  1.1   thorpej 
    125  1.1   thorpej 	if (flags & IFF_BROADCAST) {
    126  1.1   thorpej 		iasin = &in_addreq.ifra_broadaddr;
    127  1.1   thorpej 		printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
    128  1.1   thorpej 	}
    129  1.1   thorpej 	printf("\n");
    130  1.1   thorpej }
    131  1.1   thorpej 
    132  1.1   thorpej void
    133  1.1   thorpej in_status(int force)
    134  1.1   thorpej {
    135  1.1   thorpej 	struct ifaddrs *ifap, *ifa;
    136  1.1   thorpej 	struct ifreq isifr;
    137  1.1   thorpej 
    138  1.1   thorpej 	if (getifaddrs(&ifap) != 0)
    139  1.1   thorpej 		err(EXIT_FAILURE, "getifaddrs");
    140  1.1   thorpej 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    141  1.1   thorpej 		if (strcmp(name, ifa->ifa_name) != 0)
    142  1.1   thorpej 			continue;
    143  1.1   thorpej 		if (ifa->ifa_addr->sa_family != AF_INET)
    144  1.1   thorpej 			continue;
    145  1.1   thorpej 		if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
    146  1.1   thorpej 			continue;
    147  1.1   thorpej 
    148  1.1   thorpej 		memset(&isifr, 0, sizeof(isifr));
    149  1.3      elad 		estrlcpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
    150  1.1   thorpej 		memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    151  1.1   thorpej 		in_alias(&isifr);
    152  1.1   thorpej 	}
    153  1.1   thorpej 	freeifaddrs(ifap);
    154  1.1   thorpej }
    155  1.1   thorpej 
    156  1.1   thorpej #define SIN(x) ((struct sockaddr_in *) &(x))
    157  1.1   thorpej struct sockaddr_in *sintab[] = {
    158  1.1   thorpej     SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
    159  1.1   thorpej     SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
    160  1.1   thorpej 
    161  1.1   thorpej void
    162  1.1   thorpej in_getaddr(const char *str, int which)
    163  1.1   thorpej {
    164  1.1   thorpej 	struct sockaddr_in *gasin = sintab[which];
    165  1.1   thorpej 	struct hostent *hp;
    166  1.1   thorpej 	struct netent *np;
    167  1.1   thorpej 
    168  1.1   thorpej 	gasin->sin_len = sizeof(*gasin);
    169  1.1   thorpej 	if (which != MASK)
    170  1.1   thorpej 		gasin->sin_family = AF_INET;
    171  1.1   thorpej 
    172  1.1   thorpej 	if (which == ADDR) {
    173  1.1   thorpej 		char *p = NULL;
    174  1.1   thorpej 		if ((p = strrchr(str, '/')) != NULL) {
    175  1.1   thorpej 			*p = '\0';
    176  1.1   thorpej 			in_getprefix(p + 1, MASK);
    177  1.1   thorpej 		}
    178  1.1   thorpej 	}
    179  1.1   thorpej 
    180  1.1   thorpej 	if (inet_aton(str, &gasin->sin_addr) == 0) {
    181  1.1   thorpej 		if ((hp = gethostbyname(str)) != NULL)
    182  1.1   thorpej 			(void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
    183  1.1   thorpej 		else if ((np = getnetbyname(str)) != NULL)
    184  1.1   thorpej 			gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
    185  1.1   thorpej 		else
    186  1.1   thorpej 			errx(EXIT_FAILURE, "%s: bad value", str);
    187  1.1   thorpej 	}
    188  1.1   thorpej }
    189  1.1   thorpej 
    190  1.1   thorpej void
    191  1.1   thorpej in_getprefix(const char *plen, int which)
    192  1.1   thorpej {
    193  1.1   thorpej 	struct sockaddr_in *igsin = sintab[which];
    194  1.1   thorpej 	u_char *cp;
    195  1.1   thorpej 	int len = strtol(plen, (char **)NULL, 10);
    196  1.1   thorpej 
    197  1.1   thorpej 	if ((len < 0) || (len > 32))
    198  1.1   thorpej 		errx(EXIT_FAILURE, "%s: bad value", plen);
    199  1.1   thorpej 	igsin->sin_len = sizeof(*igsin);
    200  1.1   thorpej 	if (which != MASK)
    201  1.1   thorpej 		igsin->sin_family = AF_INET;
    202  1.1   thorpej 	if ((len == 0) || (len == 32)) {
    203  1.1   thorpej 		memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
    204  1.1   thorpej 		return;
    205  1.1   thorpej 	}
    206  1.1   thorpej 	memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
    207  1.1   thorpej 	for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
    208  1.1   thorpej 		*cp++ = 0xff;
    209  1.1   thorpej 	if (len)
    210  1.1   thorpej 		*cp = 0xff << (8 - len);
    211  1.1   thorpej }
    212