Home | History | Annotate | Line # | Download | only in ifconfig
      1 /*	$NetBSD: agr.c,v 1.16 2021/06/21 03:14:40 christos 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.16 2021/06/21 03:14:40 christos 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 "extern.h"
     50 #include "parse.h"
     51 #include "util.h"
     52 
     53 static int agrsetport(prop_dictionary_t, prop_dictionary_t);
     54 static void agr_constructor(void) __attribute__((constructor));
     55 static int checkifname(prop_dictionary_t);
     56 static void assertifname(prop_dictionary_t);
     57 
     58 static struct piface agrif = PIFACE_INITIALIZER(&agrif, "agr interface",
     59     agrsetport, "agrport", &command_root.pb_parser);
     60 
     61 static const struct kwinst agrkw[] = {
     62 	  {.k_word = "agrport", .k_type = KW_T_INT, .k_int = AGRCMD_ADDPORT,
     63 	   .k_nextparser = &agrif.pif_parser}
     64 	, {.k_word = "-agrport", .k_type = KW_T_INT, .k_int = AGRCMD_REMPORT,
     65 	   .k_nextparser = &agrif.pif_parser}
     66 };
     67 
     68 struct pkw agr = PKW_INITIALIZER(&agr, "agr", NULL, "agrcmd",
     69     agrkw, __arraycount(agrkw), NULL);
     70 
     71 static int
     72 checkifname(prop_dictionary_t env)
     73 {
     74 	const char *ifname;
     75 
     76 	if ((ifname = getifname(env)) == NULL)
     77 		return 1;
     78 
     79 	return strncmp(ifname, "agr", 3) != 0 ||
     80 	    !isdigit((unsigned char)ifname[3]);
     81 }
     82 
     83 static void
     84 assertifname(prop_dictionary_t env)
     85 {
     86 	if (checkifname(env))
     87 		errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
     88 }
     89 
     90 int
     91 agrsetport(prop_dictionary_t env, prop_dictionary_t oenv)
     92 {
     93 	char buf[IFNAMSIZ];
     94 	struct agrreq ar;
     95 	const char *port;
     96 	int64_t cmd;
     97 
     98 	if (!prop_dictionary_get_int64(env, "agrcmd", &cmd)) {
     99 		warnx("%s.%d", __func__, __LINE__);
    100 		errno = ENOENT;
    101 		return -1;
    102 	}
    103 
    104 	if (!prop_dictionary_get_string(env, "agrport", &port)) {
    105 		warnx("%s.%d", __func__, __LINE__);
    106 		errno = ENOENT;
    107 		return -1;
    108 	}
    109 	strlcpy(buf, port, sizeof(buf));
    110 
    111 	assertifname(env);
    112 	memset(&ar, 0, sizeof(ar));
    113 	ar.ar_version = AGRREQ_VERSION;
    114 	ar.ar_cmd = cmd;
    115 	ar.ar_buf = buf;
    116 	ar.ar_buflen = strlen(buf);
    117 
    118 	if (indirect_ioctl(env, SIOCSETAGR, &ar) == -1)
    119 		err(EXIT_FAILURE, "SIOCSETAGR");
    120 	return 0;
    121 }
    122 
    123 static void
    124 agr_status(prop_dictionary_t env, prop_dictionary_t oenv)
    125 {
    126 	struct agrreq ar;
    127 	void *buf = NULL;
    128 	size_t buflen = 0;
    129 	struct agrportlist *apl;
    130 	struct agrportinfo *api;
    131 	int i;
    132 
    133 	if (checkifname(env))
    134 		return;
    135 
    136 again:
    137 	memset(&ar, 0, sizeof(ar));
    138 	ar.ar_version = AGRREQ_VERSION;
    139 	ar.ar_cmd = AGRCMD_PORTLIST;
    140 	ar.ar_buf = buf;
    141 	ar.ar_buflen = buflen;
    142 
    143 	if (indirect_ioctl(env, SIOCGETAGR, &ar) == -1) {
    144 		if (errno != E2BIG) {
    145 			warn("SIOCGETAGR");
    146 			return;
    147 		}
    148 
    149 		free(buf);
    150 		buf = NULL;
    151 		buflen = 0;
    152 		goto again;
    153 	}
    154 
    155 	if (buf == NULL) {
    156 		buflen = ar.ar_buflen;
    157 		buf = malloc(buflen);
    158 		if (buf == NULL) {
    159 			err(EXIT_FAILURE, "agr_status");
    160 		}
    161 		goto again;
    162 	}
    163 
    164 	apl = buf;
    165 	api = (void *)(apl + 1);
    166 
    167 	for (i = 0; i < apl->apl_nports; i++) {
    168 		char tmp[256];
    169 
    170 		snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
    171 		printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
    172 		api++;
    173 	}
    174 }
    175 
    176 static status_func_t status;
    177 static usage_func_t usage;
    178 static cmdloop_branch_t branch;
    179 
    180 static void
    181 agr_usage(prop_dictionary_t env)
    182 {
    183 	fprintf(stderr, "\t[ agrport i ] [ -agrport i ]\n");
    184 }
    185 
    186 static void
    187 agr_constructor(void)
    188 {
    189 	status_func_init(&status, agr_status);
    190 	usage_func_init(&usage, agr_usage);
    191 	register_status(&status);
    192 	register_usage(&usage);
    193 	cmdloop_branch_init(&branch, &agr.pk_parser);
    194 	register_cmdloop_branch(&branch);
    195 }
    196