Home | History | Annotate | Line # | Download | only in ifconfig
agr.c revision 1.9
      1 /*	$NetBSD: agr.c,v 1.9 2008/05/07 19:55:24 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2005 YAMAMOTO Takashi,
      5  * 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if !defined(lint)
     31 __RCSID("$NetBSD: agr.c,v 1.9 2008/05/07 19:55:24 dyoung Exp $");
     32 #endif /* !defined(lint) */
     33 
     34 #include <sys/param.h>
     35 #include <sys/ioctl.h>
     36 
     37 #include <net/if.h>
     38 #include <net/agr/if_agrioctl.h>
     39 
     40 #include <ctype.h>
     41 #include <err.h>
     42 #include <errno.h>
     43 #include <string.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <util.h>
     47 
     48 #include "env.h"
     49 #include "parse.h"
     50 #include "extern.h"
     51 #include "agr.h"
     52 
     53 static int checkifname(const char *);
     54 static void assertifname(const char *);
     55 
     56 static const struct kwinst agrkw[] = {
     57 	  {.k_word = "agrport", .k_type = KW_T_NUM, .k_neg = true,
     58 	   .k_num = AGRCMD_ADDPORT, .k_negnum = AGRCMD_REMPORT,
     59 	   .k_exec = agrsetport}
     60 };
     61 
     62 struct pkw agr = PKW_INITIALIZER(&agr, "agr", NULL, NULL,
     63     agrkw, __arraycount(agrkw), NULL);
     64 
     65 static int
     66 checkifname(const char *ifname)
     67 {
     68 
     69 	return strncmp(ifname, "agr", 3) != 0 ||
     70 	    !isdigit((unsigned char)ifname[3]);
     71 }
     72 
     73 static void
     74 assertifname(const char *ifname)
     75 {
     76 
     77 	if (checkifname(ifname)) {
     78 		errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
     79 	}
     80 }
     81 
     82 int
     83 agrsetport(prop_dictionary_t env, prop_dictionary_t xenv)
     84 {
     85 	char buf[IFNAMSIZ];
     86 	struct agrreq ar;
     87 	int s;
     88 	const char *port;
     89 	const char *ifname;
     90 	struct ifreq ifr;
     91 	int64_t cmd;
     92 
     93 	if ((s = getsock(AF_UNSPEC)) == -1)
     94 		err(EXIT_FAILURE, "%s: getsock", __func__);
     95 
     96 	if (!prop_dictionary_get_int64(env, "agrcmd", &cmd)) {
     97 		warnx("%s.%d", __func__, __LINE__);
     98 		errno = ENOENT;
     99 		return -1;
    100 	}
    101 
    102 	if (!prop_dictionary_get_cstring_nocopy(env, "agrport", &port)) {
    103 		warnx("%s.%d", __func__, __LINE__);
    104 		errno = ENOENT;
    105 		return -1;
    106 	}
    107 	strlcpy(buf, port, sizeof(buf));
    108 
    109 	memset(&ifr, 0, sizeof(ifr));
    110 	if ((ifname = getifname(env)) == NULL)
    111 		err(EXIT_FAILURE, "%s: getifname", __func__);
    112 	assertifname(ifname);
    113 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    114 	memset(&ar, 0, sizeof(ar));
    115 	ar.ar_version = AGRREQ_VERSION;
    116 	ar.ar_cmd = cmd;
    117 	ar.ar_buf = buf;
    118 	ar.ar_buflen = strlen(buf);
    119 	ifr.ifr_data = &ar;
    120 
    121 	if (ioctl(s, SIOCSETAGR, &ifr) == -1)
    122 		err(EXIT_FAILURE, "SIOCSETAGR");
    123 	return 0;
    124 }
    125 
    126 void
    127 agr_status(prop_dictionary_t env, prop_dictionary_t oenv)
    128 {
    129 	struct agrreq ar;
    130 	void *buf = NULL;
    131 	size_t buflen = 0;
    132 	struct agrportlist *apl;
    133 	struct agrportinfo *api;
    134 	int i;
    135 	int s;
    136 	const char *ifname;
    137 	struct ifreq ifr;
    138 
    139 	if ((s = getsock(AF_UNSPEC)) == -1)
    140 		err(EXIT_FAILURE, "%s: getsock", __func__);
    141 
    142 	memset(&ifr, 0, sizeof(ifr));
    143 	if ((ifname = getifname(env)) == NULL)
    144 		err(EXIT_FAILURE, "%s: getifname", __func__);
    145 	if (checkifname(ifname))
    146 		return;
    147 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    148 
    149 again:
    150 	memset(&ar, 0, sizeof(ar));
    151 	ar.ar_version = AGRREQ_VERSION;
    152 	ar.ar_cmd = AGRCMD_PORTLIST;
    153 	ar.ar_buf = buf;
    154 	ar.ar_buflen = buflen;
    155 	ifr.ifr_data = &ar;
    156 
    157 	if (ioctl(s, SIOCGETAGR, &ifr) == -1) {
    158 		if (errno != E2BIG) {
    159 			warn("SIOCGETAGR");
    160 			return;
    161 		}
    162 
    163 		free(buf);
    164 		buf = NULL;
    165 		buflen = 0;
    166 		goto again;
    167 	}
    168 
    169 	if (buf == NULL) {
    170 		buflen = ar.ar_buflen;
    171 		buf = malloc(buflen);
    172 		if (buf == NULL) {
    173 			err(EXIT_FAILURE, "agr_status");
    174 		}
    175 		goto again;
    176 	}
    177 
    178 	apl = buf;
    179 	api = (void *)(apl + 1);
    180 
    181 	for (i = 0; i < apl->apl_nports; i++) {
    182 		char tmp[256];
    183 
    184 		snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
    185 		printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
    186 		api++;
    187 	}
    188 }
    189