Home | History | Annotate | Line # | Download | only in ifconfig
af_inet.c revision 1.8
      1 /*	$NetBSD: af_inet.c,v 1.8 2008/05/08 07:13:20 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.8 2008/05/08 07:13:20 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 #include "af_inetany.h"
     61 
     62 static void in_preference(const char *, const struct sockaddr *);
     63 
     64 void
     65 in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
     66     struct ifreq *creq)
     67 {
     68 	struct sockaddr_in *sin;
     69 	struct ifreq ifr;
     70 	int alias, s;
     71 	unsigned short flags;
     72 	struct in_aliasreq in_addreq;
     73 
     74 	if (lflag)
     75 		return;
     76 
     77 	alias = 1;
     78 
     79 	/* Get the non-alias address for this interface. */
     80 	if ((s = getsock(AF_INET)) == -1) {
     81 		if (errno == EAFNOSUPPORT)
     82 			return;
     83 		err(EXIT_FAILURE, "socket");
     84 	}
     85 	memset(&ifr, 0, sizeof(ifr));
     86 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     87 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
     88 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
     89 			return;
     90 		} else
     91 			warn("SIOCGIFADDR");
     92 	}
     93 	/* If creq and ifr are the same address, this is not an alias. */
     94 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr, sizeof(creq->ifr_addr)) == 0)
     95 		alias = 0;
     96 	memset(&in_addreq, 0, sizeof(in_addreq));
     97 	estrlcpy(in_addreq.ifra_name, ifname, sizeof(in_addreq.ifra_name));
     98 	memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
     99 	    sizeof(in_addreq.ifra_addr));
    100 	if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
    101 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
    102 			return;
    103 		} else
    104 			warn("SIOCGIFALIAS");
    105 	}
    106 
    107 	sin = &in_addreq.ifra_addr;
    108 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(sin->sin_addr));
    109 
    110 	if (getifflags(env, oenv, &flags) == -1)
    111 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    112 
    113 	if (flags & IFF_POINTOPOINT) {
    114 		sin = &in_addreq.ifra_dstaddr;
    115 		printf(" -> %s", inet_ntoa(sin->sin_addr));
    116 	}
    117 
    118 	sin = &in_addreq.ifra_mask;
    119 	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
    120 
    121 	if (flags & IFF_BROADCAST) {
    122 		sin = &in_addreq.ifra_broadaddr;
    123 		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
    124 	}
    125 }
    126 
    127 static uint16_t
    128 in_get_preference(const char *ifname, const struct sockaddr *sa)
    129 {
    130 	struct if_addrprefreq ifap;
    131 	int s;
    132 
    133 	if ((s = getsock(AF_INET)) == -1) {
    134 		if (errno == EPROTONOSUPPORT)
    135 			return 0;
    136 		err(EXIT_FAILURE, "socket");
    137 	}
    138 	memset(&ifap, 0, sizeof(ifap));
    139 	strncpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
    140 	memcpy(&ifap.ifap_addr, sa, MIN(sizeof(ifap.ifap_addr), sa->sa_len));
    141 	if (ioctl(s, SIOCGIFADDRPREF, &ifap) == -1) {
    142 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
    143 			return 0;
    144 		warn("SIOCGIFADDRPREF");
    145 	}
    146 	return ifap.ifap_preference;
    147 }
    148 
    149 static void
    150 in_preference(const char *ifname, const struct sockaddr *sa)
    151 {
    152 	uint16_t preference;
    153 
    154 	if (lflag)
    155 		return;
    156 
    157 	preference = in_get_preference(ifname, sa);
    158 	printf(" preference %" PRIu16, preference);
    159 }
    160 
    161 void
    162 in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    163 {
    164 	struct ifaddrs *ifap, *ifa;
    165 	struct ifreq ifr;
    166 	int printprefs = 0;
    167 	const char *ifname;
    168 
    169 	if ((ifname = getifname(env)) == NULL)
    170 		err(EXIT_FAILURE, "%s: getifname", __func__);
    171 
    172 	if (getifaddrs(&ifap) != 0)
    173 		err(EXIT_FAILURE, "getifaddrs");
    174 	/* Print address preference numbers if any address has a non-zero
    175 	 * preference assigned.
    176 	 */
    177 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    178 		if (strcmp(ifname, ifa->ifa_name) != 0)
    179 			continue;
    180 		if (ifa->ifa_addr->sa_family != AF_INET)
    181 			continue;
    182 		if (in_get_preference(ifa->ifa_name, ifa->ifa_addr) != 0) {
    183 			printprefs = 1;
    184 			break;
    185 		}
    186 	}
    187 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    188 		if (strcmp(ifname, ifa->ifa_name) != 0)
    189 			continue;
    190 		if (ifa->ifa_addr->sa_family != AF_INET)
    191 			continue;
    192 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    193 			continue;
    194 
    195 		memset(&ifr, 0, sizeof(ifr));
    196 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    197 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    198 		in_alias(ifa->ifa_name, env, oenv, &ifr);
    199 		if (printprefs)
    200 			in_preference(ifa->ifa_name, ifa->ifa_addr);
    201 		printf("\n");
    202 	}
    203 	if (ifa != NULL) {
    204 		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    205 			if (strcmp(ifname, ifa->ifa_name) != 0)
    206 				continue;
    207 			if (ifa->ifa_addr->sa_family != AF_INET)
    208 				continue;
    209 		}
    210 	}
    211 	freeifaddrs(ifap);
    212 }
    213 
    214 void
    215 in_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    216 {
    217 	struct ifreq in_ifr;
    218 	struct in_aliasreq in_ifra;
    219 	struct afparam inparam = {
    220 		  .req = BUFPARAM(in_ifra)
    221 		, .dgreq = BUFPARAM(in_ifr)
    222 		, .name = {
    223 			  {.buf = in_ifr.ifr_name,
    224 			   .buflen = sizeof(in_ifr.ifr_name)}
    225 			, {.buf = in_ifra.ifra_name,
    226 			   .buflen = sizeof(in_ifra.ifra_name)}
    227 		  }
    228 		, .dgaddr = BUFPARAM(in_ifr.ifr_addr)
    229 		, .addr = BUFPARAM(in_ifra.ifra_addr)
    230 		, .dst = BUFPARAM(in_ifra.ifra_dstaddr)
    231 		, .brd = BUFPARAM(in_ifra.ifra_broadaddr)
    232 		, .mask = BUFPARAM(in_ifra.ifra_mask)
    233 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
    234 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR)
    235 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
    236 		, .defmask = {.buf = NULL, .buflen = 0}
    237 	};
    238 	commit_address(env, oenv, &inparam);
    239 }
    240