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