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