Kernel tree reorganization stage 2: Major cvs repository work.
[dragonfly.git] / sbin / ifconfig / ifvlan.c
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  * $FreeBSD: src/sbin/ifconfig/ifvlan.c,v 1.2 1999/08/28 00:13:09 peter Exp $
33  * $DragonFly: src/sbin/ifconfig/ifvlan.c,v 1.3 2003/08/08 04:18:38 dillon Exp $
34  *
35  * $FreeBSD: src/sbin/ifconfig/ifvlan.c,v 1.2 1999/08/28 00:13:09 peter Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/mbuf.h>
43
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/vlan/if_vlan_var.h>
51 #include <net/route.h>
52
53 #include <ctype.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <err.h>
59 #include <errno.h>
60
61 #include "ifconfig.h"
62
63 static int                      __tag = 0;
64 static int                      __have_tag = 0;
65
66 void vlan_status(s, info)
67         int                     s;
68         struct rt_addrinfo *info __unused;
69 {
70         struct vlanreq          vreq;
71
72         bzero((char *)&vreq, sizeof(struct vlanreq));
73         ifr.ifr_data = (caddr_t)&vreq;
74
75         if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
76                 return;
77
78         printf("\tvlan: %d parent interface: %s\n",
79             vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
80             "<none>" : vreq.vlr_parent);
81
82         return;
83 }
84
85 void setvlantag(val, d, s, afp)
86         const char              *val;
87         int                     d, s;
88         const struct afswtch    *afp;
89 {
90         u_int16_t               tag;
91         struct vlanreq          vreq;
92
93         __tag = tag = atoi(val);
94         __have_tag = 1;
95
96         bzero((char *)&vreq, sizeof(struct vlanreq));
97         ifr.ifr_data = (caddr_t)&vreq;
98
99         if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
100                 err(1, "SIOCGETVLAN");
101
102         vreq.vlr_tag = tag;
103
104         if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
105                 err(1, "SIOCSETVLAN");
106
107         return;
108 }
109
110 void setvlandev(val, d, s, afp)
111         const char              *val;
112         int                     d, s;
113         const struct afswtch    *afp;
114 {
115         struct vlanreq          vreq;
116
117         if (!__have_tag)
118                 errx(1, "must specify both vlan tag and device");
119
120         bzero((char *)&vreq, sizeof(struct vlanreq));
121         ifr.ifr_data = (caddr_t)&vreq;
122
123         if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
124                 err(1, "SIOCGETVLAN");
125
126         strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent));
127         vreq.vlr_tag = __tag;
128
129         if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
130                 err(1, "SIOCSETVLAN");
131
132         return;
133 }
134
135 void unsetvlandev(val, d, s, afp)
136         const char              *val;
137         int                     d, s;
138         const struct afswtch    *afp;
139 {
140         struct vlanreq          vreq;
141
142         bzero((char *)&vreq, sizeof(struct vlanreq));
143         ifr.ifr_data = (caddr_t)&vreq;
144
145         if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
146                 err(1, "SIOCGETVLAN");
147
148         bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
149         vreq.vlr_tag = 0;
150
151         if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
152                 err(1, "SIOCSETVLAN");
153
154         return;
155 }