Deleted Added
full compact
ifvlan.c (138593) ifvlan.c (139494)
1/*
2 * Copyright (c) 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
19 *
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.
31 */
32
33#include <sys/param.h>
34#include <sys/ioctl.h>
35#include <sys/socket.h>
36#include <sys/sockio.h>
37
38#include <stdlib.h>
39#include <unistd.h>
40
41#include <net/ethernet.h>
42#include <net/if.h>
43#include <net/if_var.h>
44#include <net/if_vlan_var.h>
45#include <net/route.h>
46
47#include <ctype.h>
48#include <stdio.h>
49#include <string.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <err.h>
53#include <errno.h>
54
55#include "ifconfig.h"
56
57#ifndef lint
58static const char rcsid[] =
1/*
2 * Copyright (c) 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
19 *
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.
31 */
32
33#include <sys/param.h>
34#include <sys/ioctl.h>
35#include <sys/socket.h>
36#include <sys/sockio.h>
37
38#include <stdlib.h>
39#include <unistd.h>
40
41#include <net/ethernet.h>
42#include <net/if.h>
43#include <net/if_var.h>
44#include <net/if_vlan_var.h>
45#include <net/route.h>
46
47#include <ctype.h>
48#include <stdio.h>
49#include <string.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <err.h>
53#include <errno.h>
54
55#include "ifconfig.h"
56
57#ifndef lint
58static const char rcsid[] =
59 "$FreeBSD: head/sbin/ifconfig/ifvlan.c 138593 2004-12-08 19:18:07Z sam $";
59 "$FreeBSD: head/sbin/ifconfig/ifvlan.c 139494 2004-12-31 19:46:27Z sam $";
60#endif
61static int __tag = 0;
62static int __have_tag = 0;
63
64static void
60#endif
61static int __tag = 0;
62static int __have_tag = 0;
63
64static void
65vlan_status(int s, const struct rt_addrinfo *info __unused)
65vlan_status(int s)
66{
67 struct vlanreq vreq;
68
69 bzero((char *)&vreq, sizeof(struct vlanreq));
70 ifr.ifr_data = (caddr_t)&vreq;
71
72 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
73 return;
74
75 printf("\tvlan: %d parent interface: %s\n",
76 vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
77 "<none>" : vreq.vlr_parent);
78
79 return;
80}
81
82static void
83setvlantag(const char *val, int d, int s, const struct afswtch *afp)
84{
85 u_int16_t tag;
86 struct vlanreq vreq;
87
88 __tag = tag = atoi(val);
89 __have_tag = 1;
90
91 bzero((char *)&vreq, sizeof(struct vlanreq));
92 ifr.ifr_data = (caddr_t)&vreq;
93
94 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
95 err(1, "SIOCGETVLAN");
96
97 vreq.vlr_tag = tag;
98
99 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
100 err(1, "SIOCSETVLAN");
101
102 return;
103}
104
105static void
106setvlandev(const char *val, int d, int s, const struct afswtch *afp)
107{
108 struct vlanreq vreq;
109
110 if (!__have_tag)
111 errx(1, "must specify both vlan tag and device");
112
113 bzero((char *)&vreq, sizeof(struct vlanreq));
114 ifr.ifr_data = (caddr_t)&vreq;
115
116 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
117 err(1, "SIOCGETVLAN");
118
119 strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent));
120 vreq.vlr_tag = __tag;
121
122 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
123 err(1, "SIOCSETVLAN");
124
125 return;
126}
127
128static void
129unsetvlandev(const char *val, int d, int s, const struct afswtch *afp)
130{
131 struct vlanreq vreq;
132
133 bzero((char *)&vreq, sizeof(struct vlanreq));
134 ifr.ifr_data = (caddr_t)&vreq;
135
136 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
137 err(1, "SIOCGETVLAN");
138
139 bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
140 vreq.vlr_tag = 0;
141
142 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
143 err(1, "SIOCSETVLAN");
144
145 return;
146}
147
148static struct cmd vlan_cmds[] = {
149 DEF_CMD_ARG("vlan", setvlantag),
150 DEF_CMD_ARG("vlandev", setvlandev),
151 DEF_CMD_ARG("-vlandev", unsetvlandev),
152 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap),
153 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap),
154 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
155 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap),
156};
157static struct afswtch af_vlan = {
158 .af_name = "af_vlan",
159 .af_af = AF_UNSPEC,
66{
67 struct vlanreq vreq;
68
69 bzero((char *)&vreq, sizeof(struct vlanreq));
70 ifr.ifr_data = (caddr_t)&vreq;
71
72 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
73 return;
74
75 printf("\tvlan: %d parent interface: %s\n",
76 vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
77 "<none>" : vreq.vlr_parent);
78
79 return;
80}
81
82static void
83setvlantag(const char *val, int d, int s, const struct afswtch *afp)
84{
85 u_int16_t tag;
86 struct vlanreq vreq;
87
88 __tag = tag = atoi(val);
89 __have_tag = 1;
90
91 bzero((char *)&vreq, sizeof(struct vlanreq));
92 ifr.ifr_data = (caddr_t)&vreq;
93
94 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
95 err(1, "SIOCGETVLAN");
96
97 vreq.vlr_tag = tag;
98
99 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
100 err(1, "SIOCSETVLAN");
101
102 return;
103}
104
105static void
106setvlandev(const char *val, int d, int s, const struct afswtch *afp)
107{
108 struct vlanreq vreq;
109
110 if (!__have_tag)
111 errx(1, "must specify both vlan tag and device");
112
113 bzero((char *)&vreq, sizeof(struct vlanreq));
114 ifr.ifr_data = (caddr_t)&vreq;
115
116 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
117 err(1, "SIOCGETVLAN");
118
119 strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent));
120 vreq.vlr_tag = __tag;
121
122 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
123 err(1, "SIOCSETVLAN");
124
125 return;
126}
127
128static void
129unsetvlandev(const char *val, int d, int s, const struct afswtch *afp)
130{
131 struct vlanreq vreq;
132
133 bzero((char *)&vreq, sizeof(struct vlanreq));
134 ifr.ifr_data = (caddr_t)&vreq;
135
136 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
137 err(1, "SIOCGETVLAN");
138
139 bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
140 vreq.vlr_tag = 0;
141
142 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
143 err(1, "SIOCSETVLAN");
144
145 return;
146}
147
148static struct cmd vlan_cmds[] = {
149 DEF_CMD_ARG("vlan", setvlantag),
150 DEF_CMD_ARG("vlandev", setvlandev),
151 DEF_CMD_ARG("-vlandev", unsetvlandev),
152 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap),
153 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap),
154 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
155 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap),
156};
157static struct afswtch af_vlan = {
158 .af_name = "af_vlan",
159 .af_af = AF_UNSPEC,
160 .af_status = vlan_status,
160 .af_other_status = vlan_status,
161};
162
163static __constructor void
164vlan_ctor(void)
165{
166#define N(a) (sizeof(a) / sizeof(a[0]))
167 int i;
168
169 for (i = 0; i < N(vlan_cmds); i++)
170 cmd_register(&vlan_cmds[i]);
171 af_register(&af_vlan);
172#undef N
173}
161};
162
163static __constructor void
164vlan_ctor(void)
165{
166#define N(a) (sizeof(a) / sizeof(a[0]))
167 int i;
168
169 for (i = 0; i < N(vlan_cmds); i++)
170 cmd_register(&vlan_cmds[i]);
171 af_register(&af_vlan);
172#undef N
173}