vlan.c revision 1.10 1 /* $NetBSD: vlan.c,v 1.10 2008/07/02 07:44:15 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.10 2008/07/02 07:44:15 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 "util.h"
55
56 static status_func_t status;
57 static cmdloop_branch_t branch;
58
59 static void vlan_constructor(void) __attribute__((constructor));
60 static void vlan_status(prop_dictionary_t, prop_dictionary_t);
61
62 static int setvlan(prop_dictionary_t, prop_dictionary_t);
63 static int setvlanif(prop_dictionary_t, prop_dictionary_t);
64
65 struct pinteger vlantag = PINTEGER_INITIALIZER1(&vlantag, "VLAN tag",
66 0, USHRT_MAX, 10, setvlan, "vlantag", &command_root.pb_parser);
67
68 struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
69 "vlanif", &command_root.pb_parser);
70
71 static const struct kwinst vlankw[] = {
72 {.k_word = "vlan", .k_nextparser = &vlantag.pi_parser}
73 , {.k_word = "vlanif", .k_act = "vlantag",
74 .k_nextparser = &vlanif.pif_parser}
75 , {.k_word = "-vlanif", .k_key = "vlanif", .k_type = KW_T_STR,
76 .k_str = "", .k_exec = setvlanif}
77 };
78
79 struct pkw vlan = PKW_INITIALIZER(&vlan, "vlan", NULL, NULL,
80 vlankw, __arraycount(vlankw), NULL);
81
82 static int
83 checkifname(prop_dictionary_t env)
84 {
85 const char *ifname;
86
87 if ((ifname = getifname(env)) == NULL)
88 return 1;
89
90 return strncmp(ifname, "vlan", 4) != 0 ||
91 !isdigit((unsigned char)ifname[4]);
92 }
93
94 static int
95 getvlan(prop_dictionary_t env, struct vlanreq *vlr, bool quiet)
96 {
97 memset(vlr, 0, sizeof(*vlr));
98
99 if (checkifname(env)) {
100 if (quiet)
101 return -1;
102 errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
103 }
104
105 if (indirect_ioctl(env, SIOCGETVLAN, vlr) == -1)
106 return -1;
107
108 return 0;
109 }
110
111 int
112 setvlan(prop_dictionary_t env, prop_dictionary_t xenv)
113 {
114 struct vlanreq vlr;
115 int64_t tag;
116
117 if (getvlan(env, &vlr, false) == -1)
118 err(EXIT_FAILURE, "%s: getvlan", __func__);
119
120 if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
121 errno = ENOENT;
122 return -1;
123 }
124
125 vlr.vlr_tag = tag;
126
127 if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
128 err(EXIT_FAILURE, "SIOCSETVLAN");
129 return 0;
130 }
131
132 int
133 setvlanif(prop_dictionary_t env, prop_dictionary_t xenv)
134 {
135 struct vlanreq vlr;
136 const char *parent;
137 int64_t tag;
138
139 if (getvlan(env, &vlr, false) == -1)
140 err(EXIT_FAILURE, "%s: getsock", __func__);
141
142 if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
143 errno = ENOENT;
144 return -1;
145 }
146
147 if (!prop_dictionary_get_cstring_nocopy(env, "vlanif", &parent)) {
148 errno = ENOENT;
149 return -1;
150 }
151 strlcpy(vlr.vlr_parent, parent, sizeof(vlr.vlr_parent));
152 if (strcmp(parent, "") != 0)
153 vlr.vlr_tag = (unsigned short)tag;
154
155 if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
156 err(EXIT_FAILURE, "SIOCSETVLAN");
157 return 0;
158 }
159
160 static void
161 vlan_status(prop_dictionary_t env, prop_dictionary_t oenv)
162 {
163 struct vlanreq vlr;
164
165 if (getvlan(env, &vlr, true) == -1)
166 return;
167
168 if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
169 printf("\tvlan: %d parent: %s\n",
170 vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
171 "<none>" : vlr.vlr_parent);
172 }
173
174 static void
175 vlan_constructor(void)
176 {
177 cmdloop_branch_init(&branch, &vlan.pk_parser);
178 register_cmdloop_branch(&branch);
179 status_func_init(&status, vlan_status);
180 register_status(&status);
181 }
182