1 /* $NetBSD: vlan.c,v 1.12 2008/07/15 21:27:58 dyoung Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: vlan.c,v 1.12 2008/07/15 21:27:58 dyoung Exp $");
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
41 #include <net/if_ether.h>
42 #include <net/if_vlanvar.h>
56 static status_func_t status
;
57 static usage_func_t usage
;
58 static cmdloop_branch_t branch
;
60 static void vlan_constructor(void) __attribute__((constructor
));
61 static void vlan_status(prop_dictionary_t
, prop_dictionary_t
);
63 static int setvlan(prop_dictionary_t
, prop_dictionary_t
);
64 static int setvlanif(prop_dictionary_t
, prop_dictionary_t
);
66 struct pinteger vlantag
= PINTEGER_INITIALIZER1(&vlantag
, "VLAN tag",
67 0, USHRT_MAX
, 10, setvlan
, "vlantag", &command_root
.pb_parser
);
69 struct piface vlanif
= PIFACE_INITIALIZER(&vlanif
, "vlanif", setvlanif
,
70 "vlanif", &command_root
.pb_parser
);
72 static const struct kwinst vlankw
[] = {
73 {.k_word
= "vlan", .k_nextparser
= &vlantag
.pi_parser
}
74 , {.k_word
= "vlanif", .k_act
= "vlantag",
75 .k_nextparser
= &vlanif
.pif_parser
}
76 , {.k_word
= "-vlanif", .k_key
= "vlanif", .k_type
= KW_T_STR
,
77 .k_str
= "", .k_exec
= setvlanif
}
80 struct pkw vlan
= PKW_INITIALIZER(&vlan
, "vlan", NULL
, NULL
,
81 vlankw
, __arraycount(vlankw
), NULL
);
84 checkifname(prop_dictionary_t env
)
88 if ((ifname
= getifname(env
)) == NULL
)
91 return strncmp(ifname
, "vlan", 4) != 0 ||
92 !isdigit((unsigned char)ifname
[4]);
96 getvlan(prop_dictionary_t env
, struct vlanreq
*vlr
, bool quiet
)
98 memset(vlr
, 0, sizeof(*vlr
));
100 if (checkifname(env
)) {
103 errx(EXIT_FAILURE
, "valid only with vlan(4) interfaces");
106 if (indirect_ioctl(env
, SIOCGETVLAN
, vlr
) == -1)
113 setvlan(prop_dictionary_t env
, prop_dictionary_t oenv
)
118 if (getvlan(env
, &vlr
, false) == -1)
119 err(EXIT_FAILURE
, "%s: getvlan", __func__
);
121 if (!prop_dictionary_get_int64(env
, "vlantag", &tag
)) {
128 if (indirect_ioctl(env
, SIOCSETVLAN
, &vlr
) == -1)
129 err(EXIT_FAILURE
, "SIOCSETVLAN");
134 setvlanif(prop_dictionary_t env
, prop_dictionary_t oenv
)
140 if (getvlan(env
, &vlr
, false) == -1)
141 err(EXIT_FAILURE
, "%s: getsock", __func__
);
143 if (!prop_dictionary_get_cstring_nocopy(env
, "vlanif", &parent
)) {
147 strlcpy(vlr
.vlr_parent
, parent
, sizeof(vlr
.vlr_parent
));
148 if (strcmp(parent
, "") == 0)
150 else if (!prop_dictionary_get_int64(env
, "vlantag", &tag
)) {
154 vlr
.vlr_tag
= (unsigned short)tag
;
156 if (indirect_ioctl(env
, SIOCSETVLAN
, &vlr
) == -1)
157 err(EXIT_FAILURE
, "SIOCSETVLAN");
162 vlan_status(prop_dictionary_t env
, prop_dictionary_t oenv
)
166 if (getvlan(env
, &vlr
, true) == -1)
169 if (vlr
.vlr_tag
|| vlr
.vlr_parent
[0] != '\0')
170 printf("\tvlan: %d parent: %s\n",
171 vlr
.vlr_tag
, vlr
.vlr_parent
[0] == '\0' ?
172 "<none>" : vlr
.vlr_parent
);
176 vlan_usage(prop_dictionary_t env
)
178 fprintf(stderr
, "\t[ vlan n vlanif i ]\n");
182 vlan_constructor(void)
184 cmdloop_branch_init(&branch
, &vlan
.pk_parser
);
185 register_cmdloop_branch(&branch
);
186 status_func_init(&status
, vlan_status
);
187 usage_func_init(&usage
, vlan_usage
);
188 register_status(&status
);
189 register_usage(&usage
);