Home | History | Annotate | Line # | Download | only in ifconfig
af_link.c revision 1.2.4.2
      1  1.2.4.2  mjf /*	$NetBSD: af_link.c,v 1.2.4.2 2008/06/02 13:21:22 mjf Exp $	*/
      2  1.2.4.2  mjf 
      3  1.2.4.2  mjf /*-
      4  1.2.4.2  mjf  * Copyright (c) 2008 David Young.  All rights reserved.
      5  1.2.4.2  mjf  *
      6  1.2.4.2  mjf  * Redistribution and use in source and binary forms, with or without
      7  1.2.4.2  mjf  * modification, are permitted provided that the following conditions
      8  1.2.4.2  mjf  * are met:
      9  1.2.4.2  mjf  * 1. Redistributions of source code must retain the above copyright
     10  1.2.4.2  mjf  *    notice, this list of conditions and the following disclaimer.
     11  1.2.4.2  mjf  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2.4.2  mjf  *    notice, this list of conditions and the following disclaimer in the
     13  1.2.4.2  mjf  *    documentation and/or other materials provided with the distribution.
     14  1.2.4.2  mjf  *
     15  1.2.4.2  mjf  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.2.4.2  mjf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.2.4.2  mjf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.2.4.2  mjf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.2.4.2  mjf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.2.4.2  mjf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.2.4.2  mjf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.2.4.2  mjf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.2.4.2  mjf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.2.4.2  mjf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.2.4.2  mjf  * SUCH DAMAGE.
     26  1.2.4.2  mjf  */
     27  1.2.4.2  mjf 
     28  1.2.4.2  mjf #include <sys/cdefs.h>
     29  1.2.4.2  mjf #ifndef lint
     30  1.2.4.2  mjf __RCSID("$NetBSD: af_link.c,v 1.2.4.2 2008/06/02 13:21:22 mjf Exp $");
     31  1.2.4.2  mjf #endif /* not lint */
     32  1.2.4.2  mjf 
     33  1.2.4.2  mjf #include <sys/param.h>
     34  1.2.4.2  mjf #include <sys/ioctl.h>
     35  1.2.4.2  mjf #include <sys/socket.h>
     36  1.2.4.2  mjf 
     37  1.2.4.2  mjf #include <net/if.h>
     38  1.2.4.2  mjf #include <net/if_dl.h>
     39  1.2.4.2  mjf 
     40  1.2.4.2  mjf #include <assert.h>
     41  1.2.4.2  mjf #include <err.h>
     42  1.2.4.2  mjf #include <errno.h>
     43  1.2.4.2  mjf #include <ifaddrs.h>
     44  1.2.4.2  mjf #include <netdb.h>
     45  1.2.4.2  mjf #include <string.h>
     46  1.2.4.2  mjf #include <stdlib.h>
     47  1.2.4.2  mjf #include <stdio.h>
     48  1.2.4.2  mjf #include <util.h>
     49  1.2.4.2  mjf 
     50  1.2.4.2  mjf #include "env.h"
     51  1.2.4.2  mjf #include "extern.h"
     52  1.2.4.2  mjf #include "af_link.h"
     53  1.2.4.2  mjf #include "af_inetany.h"
     54  1.2.4.2  mjf 
     55  1.2.4.2  mjf void
     56  1.2.4.2  mjf link_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
     57  1.2.4.2  mjf {
     58  1.2.4.2  mjf 	const char *delim, *ifname;
     59  1.2.4.2  mjf 	int i, s;
     60  1.2.4.2  mjf 	struct ifaddrs *ifa, *ifap;
     61  1.2.4.2  mjf 	const struct sockaddr_dl *sdl;
     62  1.2.4.2  mjf 	struct if_laddrreq iflr;
     63  1.2.4.2  mjf 	const uint8_t *octets;
     64  1.2.4.2  mjf 
     65  1.2.4.2  mjf 	if ((ifname = getifname(env)) == NULL)
     66  1.2.4.2  mjf 		err(EXIT_FAILURE, "%s: getifname", __func__);
     67  1.2.4.2  mjf 
     68  1.2.4.2  mjf 	if ((s = getsock(AF_LINK)) == -1)
     69  1.2.4.2  mjf 		err(EXIT_FAILURE, "%s: getsock", __func__);
     70  1.2.4.2  mjf 
     71  1.2.4.2  mjf 	if (getifaddrs(&ifap) == -1)
     72  1.2.4.2  mjf 		err(EXIT_FAILURE, "%s: getifaddrs", __func__);
     73  1.2.4.2  mjf 
     74  1.2.4.2  mjf 	memset(&iflr, 0, sizeof(iflr));
     75  1.2.4.2  mjf 
     76  1.2.4.2  mjf 	strlcpy(iflr.iflr_name, ifname, sizeof(iflr.iflr_name));
     77  1.2.4.2  mjf 
     78  1.2.4.2  mjf 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
     79  1.2.4.2  mjf 		if (strcmp(ifname, ifa->ifa_name) != 0)
     80  1.2.4.2  mjf 			continue;
     81  1.2.4.2  mjf 		if (ifa->ifa_addr->sa_family != AF_LINK)
     82  1.2.4.2  mjf 			continue;
     83  1.2.4.2  mjf 		if (ifa->ifa_data != NULL)
     84  1.2.4.2  mjf 			continue;
     85  1.2.4.2  mjf 
     86  1.2.4.2  mjf 		sdl = satocsdl(ifa->ifa_addr);
     87  1.2.4.2  mjf 
     88  1.2.4.2  mjf 		memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
     89  1.2.4.2  mjf 		    sizeof(iflr.addr)));
     90  1.2.4.2  mjf 		iflr.flags = IFLR_PREFIX;
     91  1.2.4.2  mjf 		iflr.prefixlen = sdl->sdl_alen * NBBY;
     92  1.2.4.2  mjf 
     93  1.2.4.2  mjf 		if (ioctl(s, SIOCGLIFADDR, &iflr) == -1)
     94  1.2.4.2  mjf 			err(EXIT_FAILURE, "%s: ioctl", __func__);
     95  1.2.4.2  mjf 
     96  1.2.4.2  mjf                 if ((iflr.flags & IFLR_ACTIVE) != 0)
     97  1.2.4.2  mjf 			continue;
     98  1.2.4.2  mjf 
     99  1.2.4.2  mjf 		octets = (const uint8_t *)&sdl->sdl_data[sdl->sdl_nlen];
    100  1.2.4.2  mjf 
    101  1.2.4.2  mjf 		delim = "\tlink ";
    102  1.2.4.2  mjf 		for (i = 0; i < sdl->sdl_alen; i++) {
    103  1.2.4.2  mjf 			printf("%s%02" PRIx8, delim, octets[i]);
    104  1.2.4.2  mjf 			delim = ":";
    105  1.2.4.2  mjf 		}
    106  1.2.4.2  mjf 		printf("\n");
    107  1.2.4.2  mjf 	}
    108  1.2.4.2  mjf }
    109  1.2.4.2  mjf 
    110  1.2.4.2  mjf static int
    111  1.2.4.2  mjf link_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
    112  1.2.4.2  mjf {
    113  1.2.4.2  mjf 	bool active;
    114  1.2.4.2  mjf 	struct if_laddrreq *iflr = param->req.buf;
    115  1.2.4.2  mjf 
    116  1.2.4.2  mjf 	if (prop_dictionary_get_bool(env, "active", &active) && active)
    117  1.2.4.2  mjf 		iflr->flags |= IFLR_ACTIVE;
    118  1.2.4.2  mjf 
    119  1.2.4.2  mjf 	return 0;
    120  1.2.4.2  mjf }
    121  1.2.4.2  mjf 
    122  1.2.4.2  mjf void
    123  1.2.4.2  mjf link_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    124  1.2.4.2  mjf {
    125  1.2.4.2  mjf 	struct if_laddrreq dgreq = {
    126  1.2.4.2  mjf 		.addr = {
    127  1.2.4.2  mjf 			.ss_family = AF_LINK,
    128  1.2.4.2  mjf 			.ss_len = sizeof(dgreq.addr),
    129  1.2.4.2  mjf 		},
    130  1.2.4.2  mjf 	};
    131  1.2.4.2  mjf 	struct if_laddrreq req = {
    132  1.2.4.2  mjf 		.addr = {
    133  1.2.4.2  mjf 			.ss_family = AF_LINK,
    134  1.2.4.2  mjf 			.ss_len = sizeof(req.addr),
    135  1.2.4.2  mjf 		}
    136  1.2.4.2  mjf 	};
    137  1.2.4.2  mjf 	struct afparam linkparam = {
    138  1.2.4.2  mjf 		  .req = BUFPARAM(req)
    139  1.2.4.2  mjf 		, .dgreq = BUFPARAM(dgreq)
    140  1.2.4.2  mjf 		, .name = {
    141  1.2.4.2  mjf 			{.buf = dgreq.iflr_name,
    142  1.2.4.2  mjf 			 .buflen = sizeof(dgreq.iflr_name)},
    143  1.2.4.2  mjf 			{.buf = req.iflr_name,
    144  1.2.4.2  mjf 			 .buflen = sizeof(req.iflr_name)}
    145  1.2.4.2  mjf 		  }
    146  1.2.4.2  mjf 		, .dgaddr = BUFPARAM(dgreq.addr)
    147  1.2.4.2  mjf 		, .addr = BUFPARAM(req.addr)
    148  1.2.4.2  mjf 		, .aifaddr = IFADDR_PARAM(SIOCALIFADDR)
    149  1.2.4.2  mjf 		, .difaddr = IFADDR_PARAM(SIOCDLIFADDR)
    150  1.2.4.2  mjf 		, .gifaddr = IFADDR_PARAM(0)
    151  1.2.4.2  mjf 		, .pre_aifaddr = link_pre_aifaddr
    152  1.2.4.2  mjf 	};
    153  1.2.4.2  mjf 	commit_address(env, oenv, &linkparam);
    154  1.2.4.2  mjf }
    155