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