]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifvlan.c
eec3ef15c2c7566a3411e8c103761f62b858852f
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
41 #include <net/ethernet.h>
43 #include <net/if_var.h>
44 #include <net/if_vlan_var.h>
45 #include <net/route.h>
58 static const char rcsid
[] =
59 "$FreeBSD: src/sbin/ifconfig/ifvlan.c,v 1.12.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $";
62 #define NOTAG ((u_short) -1)
64 static struct vlanreq params
= {
69 getvlan(int s
, struct ifreq
*ifr
, struct vlanreq
*vreq
)
71 bzero((char *)vreq
, sizeof(*vreq
));
72 ifr
->ifr_data
= (caddr_t
)vreq
;
74 return ioctl(s
, SIOCGETVLAN
, (caddr_t
)ifr
);
82 if (getvlan(s
, &ifr
, &vreq
) != -1)
83 printf("\tvlan: %d parent interface: %s\n",
84 vreq
.vlr_tag
, vreq
.vlr_parent
[0] == '\0' ?
85 "<none>" : vreq
.vlr_parent
);
89 vlan_create(int s
, struct ifreq
*ifr
)
91 if (params
.vlr_tag
!= NOTAG
|| params
.vlr_parent
[0] != '\0') {
93 * One or both parameters were specified, make sure both.
95 if (params
.vlr_tag
== NOTAG
)
96 errx(1, "must specify a tag for vlan create");
97 if (params
.vlr_parent
[0] == '\0')
98 errx(1, "must specify a parent device for vlan create");
99 ifr
->ifr_data
= (caddr_t
) ¶ms
;
102 if (ioctl(s
, SIOCIFCREATE2
, ifr
) < 0)
103 err(1, "SIOCIFCREATE2");
105 if (ioctl(s
, SIOCIFCREATE
, ifr
) < 0)
106 err(1, "SIOCIFCREATE");
111 vlan_cb(int s
, void *arg
)
113 if ((params
.vlr_tag
!= NOTAG
) ^ (params
.vlr_parent
[0] != '\0'))
114 errx(1, "both vlan and vlandev must be specified");
118 vlan_set(int s
, struct ifreq
*ifr
)
120 if (params
.vlr_tag
!= NOTAG
&& params
.vlr_parent
[0] != '\0') {
121 ifr
->ifr_data
= (caddr_t
) ¶ms
;
122 if (ioctl(s
, SIOCSETVLAN
, (caddr_t
)ifr
) == -1)
123 err(1, "SIOCSETVLAN");
128 DECL_CMD_FUNC(setvlantag
, val
, d
)
134 ul
= strtoul(val
, &endp
, 0);
136 errx(1, "invalid value for vlan");
138 /* check if the value can be represented in vlr_tag */
139 if (params
.vlr_tag
!= ul
)
140 errx(1, "value for vlan out of range");
142 if (getvlan(s
, &ifr
, &vreq
) != -1)
145 clone_setcallback(vlan_create
);
149 DECL_CMD_FUNC(setvlandev
, val
, d
)
153 strlcpy(params
.vlr_parent
, val
, sizeof(params
.vlr_parent
));
155 if (getvlan(s
, &ifr
, &vreq
) != -1)
158 clone_setcallback(vlan_create
);
162 DECL_CMD_FUNC(unsetvlandev
, val
, d
)
166 bzero((char *)&vreq
, sizeof(struct vlanreq
));
167 ifr
.ifr_data
= (caddr_t
)&vreq
;
169 if (ioctl(s
, SIOCGETVLAN
, (caddr_t
)&ifr
) == -1)
170 err(1, "SIOCGETVLAN");
172 bzero((char *)&vreq
.vlr_parent
, sizeof(vreq
.vlr_parent
));
175 if (ioctl(s
, SIOCSETVLAN
, (caddr_t
)&ifr
) == -1)
176 err(1, "SIOCSETVLAN");
179 static struct cmd vlan_cmds
[] = {
180 DEF_CLONE_CMD_ARG("vlan", setvlantag
),
181 DEF_CLONE_CMD_ARG("vlandev", setvlandev
),
182 /* XXX For compatibility. Should become DEF_CMD() some day. */
183 DEF_CMD_OPTARG("-vlandev", unsetvlandev
),
185 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU
, setifcap
),
186 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU
, setifcap
),
187 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING
, setifcap
),
188 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING
, setifcap
),
191 static struct afswtch af_vlan
= {
192 .af_name
= "af_vlan",
194 .af_other_status
= vlan_status
,
197 static __constructor
void
200 #define N(a) (sizeof(a) / sizeof(a[0]))
203 for (i
= 0; i
< N(vlan_cmds
); i
++)
204 cmd_register(&vlan_cmds
[i
]);
205 af_register(&af_vlan
);
206 callback_register(vlan_cb
, NULL
);