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