Home | History | Annotate | Line # | Download | only in ifconfig
af_inetany.c revision 1.3
      1 /*	$NetBSD: af_inetany.c,v 1.3 2008/05/06 17:29:04 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_inetany.c,v 1.3 2008/05/06 17:29:04 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 #include <netinet6/nd6.h>
     45 
     46 #include <arpa/inet.h>
     47 
     48 #include <assert.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <ifaddrs.h>
     52 #include <netdb.h>
     53 #include <string.h>
     54 #include <stdlib.h>
     55 #include <stdio.h>
     56 #include <util.h>
     57 
     58 #include "env.h"
     59 #include "extern.h"
     60 #include "af_inet.h"
     61 #include "af_inet6.h"
     62 
     63 #define	IFADDR_PARAM(__arg)	{.cmd = (__arg), .desc = #__arg}
     64 #define	BUFPARAM(__arg) 	{.buf = &(__arg), .buflen = sizeof(__arg)}
     65 
     66 struct apbuf {
     67 	void *buf;
     68 	size_t buflen;
     69 };
     70 
     71 struct afparam {
     72 	struct {
     73 		char *buf;
     74 		size_t buflen;
     75 	} name[2];
     76 	struct apbuf dgaddr, addr, brd, dst, mask, req, dgreq, defmask,
     77 	    pre_aifaddr_arg;
     78 	struct {
     79 		unsigned long cmd;
     80 		const char *desc;
     81 	} aifaddr, difaddr, gifaddr;
     82 	int (*pre_aifaddr)(prop_dictionary_t, void *);
     83 };
     84 
     85 static void *
     86 loadbuf(struct apbuf *b, const struct paddr_prefix *pfx)
     87 {
     88 	return memcpy(b->buf, &pfx->pfx_addr,
     89 	              MIN(b->buflen, pfx->pfx_addr.sa_len));
     90 }
     91 
     92 static int
     93 in6_pre_aifaddr(prop_dictionary_t env, void *arg)
     94 {
     95 	struct in6_aliasreq *ifra = arg;
     96 
     97 	setia6eui64_impl(env, ifra);
     98 	setia6vltime_impl(env, ifra);
     99 	setia6pltime_impl(env, ifra);
    100 	setia6flags_impl(env, ifra);
    101 
    102 	return 0;
    103 }
    104 
    105 void
    106 commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    107 {
    108 	const char *ifname;
    109 	struct ifreq in_ifr;
    110 	struct in_aliasreq in_ifra;
    111 	struct in6_ifreq in6_ifr;
    112 #if 0
    113 	 = {
    114 		.ifr_addr = {
    115 			.sin6_family = AF_INET6,
    116 			.sin6_addr = {
    117 				.s6_addr =
    118 				    {0xff, 0xff, 0xff, 0xff,
    119 				     0xff, 0xff, 0xff, 0xff}
    120 			}
    121 		}
    122 	};
    123 #endif
    124 	static struct sockaddr_in6 in6_defmask = {
    125 		.sin6_addr = {
    126 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    127 			            0xff, 0xff, 0xff, 0xff}
    128 		}
    129 	};
    130 
    131 	struct in6_aliasreq in6_ifra;
    132 #if 0
    133 	 = {
    134 		.ifra_prefixmask = {
    135 			.sin6_addr = {
    136 				.s6_addr =
    137 				    {0xff, 0xff, 0xff, 0xff,
    138 				     0xff, 0xff, 0xff, 0xff}}},
    139 		.ifra_lifetime = {
    140 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    141 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    142 		}
    143 	};
    144 #endif
    145 	int af, rc, s;
    146 	bool delete, replace;
    147 	prop_bool_t b;
    148 	prop_data_t d;
    149 	const struct paddr_prefix *addr, *brd, *dst, *mask;
    150 	unsigned short flags;
    151 	struct afparam inparam = {
    152 		  .req = BUFPARAM(in_ifra)
    153 		, .dgreq = BUFPARAM(in_ifr)
    154 		, .name = {
    155 			  {.buf = in_ifr.ifr_name,
    156 			   .buflen = sizeof(in_ifr.ifr_name)}
    157 			, {.buf = in_ifra.ifra_name,
    158 			   .buflen = sizeof(in_ifra.ifra_name)}
    159 		  }
    160 		, .dgaddr = BUFPARAM(in_ifr.ifr_addr)
    161 		, .addr = BUFPARAM(in_ifra.ifra_addr)
    162 		, .dst = BUFPARAM(in_ifra.ifra_dstaddr)
    163 		, .brd = BUFPARAM(in_ifra.ifra_broadaddr)
    164 		, .mask = BUFPARAM(in_ifra.ifra_mask)
    165 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
    166 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR)
    167 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
    168 		, .defmask = {.buf = NULL, .buflen = 0}
    169 	}, in6param = {
    170 		  .req = BUFPARAM(in6_ifra)
    171 		, .dgreq = BUFPARAM(in6_ifr)
    172 		, .name = {
    173 			{.buf = in6_ifr.ifr_name,
    174 			 .buflen = sizeof(in6_ifr.ifr_name)},
    175 			{.buf = in6_ifra.ifra_name,
    176 			 .buflen = sizeof(in6_ifra.ifra_name)}
    177 		  }
    178 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    179 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    180 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    181 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    182 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    183 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    184 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    185 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    186 		, .defmask = BUFPARAM(in6_defmask)
    187 		, .pre_aifaddr = in6_pre_aifaddr
    188 		, .pre_aifaddr_arg = BUFPARAM(in6_ifra)
    189 	}, *param;
    190 
    191 	if ((af = getaf(env)) == -1)
    192 		af = AF_INET;
    193 
    194 	switch (af) {
    195 	case AF_INET:
    196 		param = &inparam;
    197 		break;
    198 	case AF_INET6:
    199 		param = &in6param;
    200 		break;
    201 	default:
    202 		errx(EXIT_FAILURE, "%s: unknown address family %d", __func__,
    203 		    af);
    204 		break;
    205 	}
    206 	if ((s = getsock(af)) == -1)
    207 		err(EXIT_FAILURE, "%s: getsock", __func__);
    208 
    209 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
    210 		return;
    211 
    212 	if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
    213 		addr = prop_data_data_nocopy(d);
    214 	else
    215 		return;
    216 
    217 	if ((d = (prop_data_t)prop_dictionary_get(env, "dst")) != NULL)
    218 		dst = prop_data_data_nocopy(d);
    219 	else
    220 		dst = NULL;
    221 
    222 	if ((d = (prop_data_t)prop_dictionary_get(env, "netmask")) != NULL)
    223 		mask = prop_data_data_nocopy(d);
    224 	else
    225 		mask = NULL;
    226 
    227 	if ((d = (prop_data_t)prop_dictionary_get(env, "broadcast")) != NULL)
    228 		brd = prop_data_data_nocopy(d);
    229 	else
    230 		brd = NULL;
    231 
    232 	if ((b = (prop_bool_t)prop_dictionary_get(env, "alias")) == NULL) {
    233 		delete = false;
    234 		replace = (param->gifaddr.cmd != 0);
    235 	} else {
    236 		replace = false;
    237 		delete = !prop_bool_true(b);
    238 	}
    239 
    240 	memset(param->req.buf, 0, param->req.buflen);
    241 	memset(param->dgreq.buf, 0, param->dgreq.buflen);
    242 
    243 	strlcpy(param->name[0].buf, ifname, param->name[0].buflen);
    244 	strlcpy(param->name[1].buf, ifname, param->name[1].buflen);
    245 
    246 	loadbuf(&param->addr, addr);
    247 
    248 	/* TBD: read matching ifaddr from kernel, use the netmask as default
    249 	 * TBD: handle preference
    250 	 */
    251 	switch (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) {
    252 	case 0:
    253 		break;
    254 	case IFF_BROADCAST:
    255 		if (mask != NULL)
    256 			loadbuf(&param->mask, mask);
    257 		else if (param->defmask.buf != NULL) {
    258 			memcpy(param->mask.buf, param->defmask.buf,
    259 			    MIN(param->mask.buflen, param->defmask.buflen));
    260 		}
    261 		if (brd != NULL)
    262 			loadbuf(&param->brd, brd);
    263 		break;
    264 	case IFF_POINTOPOINT:
    265 		if (dst == NULL) {
    266 			errx(EXIT_FAILURE, "no point-to-point "
    267 			     "destination address");
    268 		}
    269 		if (brd != NULL) {
    270 			errx(EXIT_FAILURE, "%s is not a broadcast interface",
    271 			    ifname);
    272 		}
    273 		loadbuf(&param->dst, dst);
    274 		break;
    275 	case IFF_BROADCAST|IFF_POINTOPOINT:
    276 		errx(EXIT_FAILURE, "unsupported interface flags");
    277 	}
    278 	if (replace) {
    279 		if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == 0) {
    280 			rc = ioctl(s, param->difaddr.cmd, param->dgreq.buf);
    281 			if (rc == -1)
    282 				err(EXIT_FAILURE, param->difaddr.desc);
    283 		} else if (errno == EADDRNOTAVAIL)
    284 			;	/* No address was assigned yet. */
    285 		else
    286 			err(EXIT_FAILURE, param->gifaddr.desc);
    287 	} else if (delete) {
    288 		loadbuf(&param->dgaddr, addr);
    289 		if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
    290 			err(EXIT_FAILURE, param->difaddr.desc);
    291 		return;
    292 	}
    293 	if (param->pre_aifaddr != NULL &&
    294 	    (*param->pre_aifaddr)(env, param) == -1)
    295 		err(EXIT_FAILURE, "pre-%s", param->aifaddr.desc);
    296 	if (ioctl(s, param->aifaddr.cmd, param->req.buf) == -1)
    297 		err(EXIT_FAILURE, param->aifaddr.desc);
    298 }
    299