vlan.c revision 1.4
1/*	$NetBSD: vlan.c,v 1.4 2008/05/06 04:33:42 dyoung Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *      The Regents of the University of California.  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 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34__RCSID("$NetBSD: vlan.c,v 1.4 2008/05/06 04:33:42 dyoung Exp $");
35#endif /* not lint */
36
37#include <sys/param.h>
38#include <sys/ioctl.h>
39
40#include <net/if.h>
41#include <net/if_ether.h>
42#include <net/if_vlanvar.h>
43
44#include <ctype.h>
45#include <err.h>
46#include <errno.h>
47#include <string.h>
48#include <stdlib.h>
49#include <stdio.h>
50#include <util.h>
51
52#include "env.h"
53#include "extern.h"
54#include "vlan.h"
55
56struct pinteger vlan = PINTEGER_INITIALIZER1(&vlan, "vlan", 0, USHRT_MAX, 10,
57    setvlan, "vlan", &command_root.pb_parser);
58
59struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
60    "vlanif", &command_root.pb_parser);
61
62static int
63checkifname(const char *ifname)
64{
65
66	return strncmp(ifname, "vlan", 4) != 0 ||
67	    !isdigit((unsigned char)ifname[4]);
68}
69
70static int
71getvlan(prop_dictionary_t env, struct ifreq *ifr, struct vlanreq *vlr,
72    bool quiet)
73{
74	int s;
75	const char *ifname;
76
77	if ((s = getsock(AF_UNSPEC)) == -1)
78		err(EXIT_FAILURE, "%s: getsock", __func__);
79	if ((ifname = getifname(env)) == NULL)
80		err(EXIT_FAILURE, "%s: getifname", __func__);
81
82	memset(ifr, 0, sizeof(*ifr));
83	memset(vlr, 0, sizeof(*vlr));
84
85	if (checkifname(ifname)) {
86		if (quiet)
87			return -1;
88		errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
89	}
90
91	estrlcpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
92	ifr->ifr_data = vlr;
93
94	if (ioctl(s, SIOCGETVLAN, ifr) == -1)
95		return -1;
96
97	return s;
98}
99
100int
101setvlan(prop_dictionary_t env, prop_dictionary_t xenv)
102{
103	struct vlanreq vlr;
104	int s;
105	prop_number_t num;
106	unsigned short tag;
107	struct ifreq ifr;
108
109	if ((s = getvlan(env, &ifr, &vlr, false)) == -1)
110		err(EXIT_FAILURE, "%s: getvlan", __func__);
111
112	num = (prop_number_t)prop_dictionary_get(env, "vlantag");
113	if (num == NULL) {
114		errno = ENOENT;
115		return -1;
116	}
117	tag = (unsigned short)prop_number_integer_value(num);
118
119	vlr.vlr_tag = tag;
120
121	if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
122		err(EXIT_FAILURE, "SIOCSETVLAN");
123	return 0;
124}
125
126int
127setvlanif(prop_dictionary_t env, prop_dictionary_t xenv)
128{
129	struct vlanreq vlr;
130	int s;
131	prop_number_t num;
132	prop_string_t str;
133	unsigned short tag;
134	struct ifreq ifr;
135
136	if ((s = getvlan(env, &ifr, &vlr, false)) == -1)
137		err(EXIT_FAILURE, "%s: getsock", __func__);
138
139	num = (prop_number_t)prop_dictionary_get(env, "vlantag");
140	if (num == NULL) {
141		errno = ENOENT;
142		return -1;
143	}
144	tag = (unsigned short)prop_number_integer_value(num);
145
146	str = (prop_string_t)prop_dictionary_get(env, "vlanif");
147	if (str == NULL) {
148		errno = ENOENT;
149		return -1;
150	}
151	strlcpy(vlr.vlr_parent, prop_string_cstring_nocopy(str),
152	    sizeof(vlr.vlr_parent));
153	if (!prop_string_equals_cstring(str, ""))
154		vlr.vlr_tag = tag;
155
156	if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
157		err(EXIT_FAILURE, "SIOCSETVLAN");
158	return 0;
159}
160
161void
162vlan_status(prop_dictionary_t env)
163{
164	struct vlanreq vlr;
165	int s;
166	struct ifreq ifr;
167
168	if ((s = getvlan(env, &ifr, &vlr, true)) == -1)
169		return;
170
171	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
172		printf("\tvlan: %d parent: %s\n",
173		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
174		    "<none>" : vlr.vlr_parent);
175}
176