Home | History | Annotate | Line # | Download | only in ifconfig
af_inetany.c revision 1.9
      1 /*	$NetBSD: af_inetany.c,v 1.9 2008/05/28 17:17:14 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 David Young.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #ifndef lint
     30 __RCSID("$NetBSD: af_inetany.c,v 1.9 2008/05/28 17:17:14 dyoung Exp $");
     31 #endif /* not lint */
     32 
     33 #include <sys/param.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/socket.h>
     36 
     37 #include <net/if.h>
     38 #include <netinet/in.h>
     39 #include <netinet/in_var.h>
     40 #include <netinet6/nd6.h>
     41 
     42 #include <arpa/inet.h>
     43 
     44 #include <assert.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <ifaddrs.h>
     48 #include <netdb.h>
     49 #include <string.h>
     50 #include <stdlib.h>
     51 #include <stdio.h>
     52 #include <util.h>
     53 
     54 #include "env.h"
     55 #include "extern.h"
     56 #include "af_inet.h"
     57 #include "af_inet6.h"
     58 #include "af_inetany.h"
     59 
     60 static void *
     61 loadbuf(struct apbuf *b, const struct paddr_prefix *pfx)
     62 {
     63 	return memcpy(b->buf, &pfx->pfx_addr,
     64 	              MIN(b->buflen, pfx->pfx_addr.sa_len));
     65 }
     66 
     67 void
     68 commit_address(prop_dictionary_t env, prop_dictionary_t oenv,
     69     struct afparam *param)
     70 {
     71 	const char *ifname;
     72 	int af, rc, s;
     73 	bool alias, delete, replace;
     74 	prop_data_t d;
     75 	const struct paddr_prefix *addr, *brd, *dst, *mask;
     76 	unsigned short flags;
     77 
     78 	if ((af = getaf(env)) == -1)
     79 		af = AF_INET;
     80 
     81 	if ((s = getsock(af)) == -1)
     82 		err(EXIT_FAILURE, "%s: getsock", __func__);
     83 
     84 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
     85 		return;
     86 
     87 	strlcpy(param->name[0].buf, ifname, param->name[0].buflen);
     88 	strlcpy(param->name[1].buf, ifname, param->name[1].buflen);
     89 
     90 	if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
     91 		addr = prop_data_data_nocopy(d);
     92 	else if (!prop_dictionary_get_bool(env, "alias", &alias) || alias ||
     93 	    param->gifaddr.cmd == 0)
     94 		return;
     95 	else if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == -1)
     96 		err(EXIT_FAILURE, param->gifaddr.desc);
     97 	else if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
     98 		err(EXIT_FAILURE, param->difaddr.desc);
     99 	else
    100 		return;
    101 
    102 	if ((d = (prop_data_t)prop_dictionary_get(env, "dst")) != NULL)
    103 		dst = prop_data_data_nocopy(d);
    104 	else
    105 		dst = NULL;
    106 
    107 	if ((d = (prop_data_t)prop_dictionary_get(env, "netmask")) != NULL)
    108 		mask = prop_data_data_nocopy(d);
    109 	else
    110 		mask = NULL;
    111 
    112 	if ((d = (prop_data_t)prop_dictionary_get(env, "broadcast")) != NULL)
    113 		brd = prop_data_data_nocopy(d);
    114 	else
    115 		brd = NULL;
    116 
    117 	if (!prop_dictionary_get_bool(env, "alias", &alias)) {
    118 		delete = false;
    119 		replace = (param->gifaddr.cmd != 0);
    120 	} else {
    121 		replace = false;
    122 		delete = !alias;
    123 	}
    124 
    125 	loadbuf(&param->addr, addr);
    126 
    127 	/* TBD: read matching ifaddr from kernel, use the netmask as default
    128 	 * TBD: handle preference
    129 	 */
    130 	switch (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) {
    131 	case IFF_BROADCAST:
    132 		if (brd != NULL)
    133 			loadbuf(&param->brd, brd);
    134 	case 0:
    135 		if (mask != NULL)
    136 			loadbuf(&param->mask, mask);
    137 		else if (param->defmask.buf != NULL) {
    138 			memcpy(param->mask.buf, param->defmask.buf,
    139 			    MIN(param->mask.buflen, param->defmask.buflen));
    140 		}
    141 		break;
    142 	case IFF_POINTOPOINT:
    143 		if (dst == NULL) {
    144 			errx(EXIT_FAILURE, "no point-to-point "
    145 			     "destination address");
    146 		}
    147 		if (brd != NULL) {
    148 			errx(EXIT_FAILURE, "%s is not a broadcast interface",
    149 			    ifname);
    150 		}
    151 		loadbuf(&param->dst, dst);
    152 		break;
    153 	case IFF_BROADCAST|IFF_POINTOPOINT:
    154 		errx(EXIT_FAILURE, "unsupported interface flags");
    155 	}
    156 	if (replace) {
    157 		if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == 0) {
    158 			rc = ioctl(s, param->difaddr.cmd, param->dgreq.buf);
    159 			if (rc == -1)
    160 				err(EXIT_FAILURE, param->difaddr.desc);
    161 		} else if (errno == EADDRNOTAVAIL)
    162 			;	/* No address was assigned yet. */
    163 		else
    164 			err(EXIT_FAILURE, param->gifaddr.desc);
    165 	} else if (delete) {
    166 		loadbuf(&param->dgaddr, addr);
    167 		if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
    168 			err(EXIT_FAILURE, param->difaddr.desc);
    169 		return;
    170 	}
    171 	if (param->pre_aifaddr != NULL &&
    172 	    (*param->pre_aifaddr)(env, param) == -1)
    173 		err(EXIT_FAILURE, "pre-%s", param->aifaddr.desc);
    174 	if (ioctl(s, param->aifaddr.cmd, param->req.buf) == -1)
    175 		err(EXIT_FAILURE, param->aifaddr.desc);
    176 }
    177