vlan.c revision 1.1 1 1.1 thorpej /* $NetBSD: vlan.c,v 1.1 2005/03/19 03:53:55 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1983, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
16 1.1 thorpej * may be used to endorse or promote products derived from this software
17 1.1 thorpej * without specific prior written permission.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.1 thorpej #ifndef lint
34 1.1 thorpej __RCSID("$NetBSD: vlan.c,v 1.1 2005/03/19 03:53:55 thorpej Exp $");
35 1.1 thorpej #endif /* not lint */
36 1.1 thorpej
37 1.1 thorpej #include <sys/param.h>
38 1.1 thorpej #include <sys/ioctl.h>
39 1.1 thorpej
40 1.1 thorpej #include <net/if.h>
41 1.1 thorpej #include <net/if_ether.h>
42 1.1 thorpej #include <net/if_vlanvar.h>
43 1.1 thorpej
44 1.1 thorpej #include <ctype.h>
45 1.1 thorpej #include <err.h>
46 1.1 thorpej #include <string.h>
47 1.1 thorpej #include <stdlib.h>
48 1.1 thorpej #include <stdio.h>
49 1.1 thorpej
50 1.1 thorpej #include "vlan.h"
51 1.1 thorpej
52 1.1 thorpej extern struct ifreq ifr;
53 1.1 thorpej extern int s;
54 1.1 thorpej
55 1.1 thorpej static u_int vlan_tag = (u_int)-1;
56 1.1 thorpej
57 1.1 thorpej static int
58 1.1 thorpej checkifname(const char *name)
59 1.1 thorpej {
60 1.1 thorpej
61 1.1 thorpej return strncmp(name, "vlan", 4) != 0 ||
62 1.1 thorpej !isdigit((unsigned char)name[4]);
63 1.1 thorpej }
64 1.1 thorpej
65 1.1 thorpej static void
66 1.1 thorpej assertifname(const char *name)
67 1.1 thorpej {
68 1.1 thorpej
69 1.1 thorpej if (checkifname(name)) {
70 1.1 thorpej errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
71 1.1 thorpej }
72 1.1 thorpej }
73 1.1 thorpej
74 1.1 thorpej void
75 1.1 thorpej setvlan(const char *val, int d)
76 1.1 thorpej {
77 1.1 thorpej struct vlanreq vlr;
78 1.1 thorpej
79 1.1 thorpej assertifname(ifr.ifr_name);
80 1.1 thorpej
81 1.1 thorpej vlan_tag = atoi(val);
82 1.1 thorpej
83 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
84 1.1 thorpej ifr.ifr_data = (void *)&vlr;
85 1.1 thorpej if (ioctl(s, SIOCGETVLAN, &ifr) == -1)
86 1.1 thorpej err(EXIT_FAILURE, "SIOCGETVLAN");
87 1.1 thorpej
88 1.1 thorpej vlr.vlr_tag = vlan_tag;
89 1.1 thorpej
90 1.1 thorpej if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
91 1.1 thorpej err(EXIT_FAILURE, "SIOCSETVLAN");
92 1.1 thorpej }
93 1.1 thorpej
94 1.1 thorpej void
95 1.1 thorpej setvlanif(const char *val, int d)
96 1.1 thorpej {
97 1.1 thorpej struct vlanreq vlr;
98 1.1 thorpej
99 1.1 thorpej assertifname(ifr.ifr_name);
100 1.1 thorpej
101 1.1 thorpej if (vlan_tag == (u_int)-1)
102 1.1 thorpej errx(EXIT_FAILURE,
103 1.1 thorpej "must specify both ``vlan'' and ``vlanif''");
104 1.1 thorpej
105 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
106 1.1 thorpej ifr.ifr_data = (void *)&vlr;
107 1.1 thorpej
108 1.1 thorpej if (ioctl(s, SIOCGETVLAN, &ifr) == -1)
109 1.1 thorpej err(EXIT_FAILURE, "SIOCGETVLAN");
110 1.1 thorpej
111 1.1 thorpej strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
112 1.1 thorpej vlr.vlr_tag = vlan_tag;
113 1.1 thorpej
114 1.1 thorpej if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
115 1.1 thorpej err(EXIT_FAILURE, "SIOCSETVLAN");
116 1.1 thorpej }
117 1.1 thorpej
118 1.1 thorpej void
119 1.1 thorpej unsetvlanif(const char *val, int d)
120 1.1 thorpej {
121 1.1 thorpej struct vlanreq vlr;
122 1.1 thorpej
123 1.1 thorpej assertifname(ifr.ifr_name);
124 1.1 thorpej
125 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
126 1.1 thorpej ifr.ifr_data = (void *)&vlr;
127 1.1 thorpej
128 1.1 thorpej if (ioctl(s, SIOCGETVLAN, &ifr) == -1)
129 1.1 thorpej err(EXIT_FAILURE, "SIOCGETVLAN");
130 1.1 thorpej
131 1.1 thorpej vlr.vlr_parent[0] = '\0';
132 1.1 thorpej vlr.vlr_tag = 0;
133 1.1 thorpej
134 1.1 thorpej if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
135 1.1 thorpej err(EXIT_FAILURE, "SIOCSETVLAN");
136 1.1 thorpej }
137 1.1 thorpej
138 1.1 thorpej void
139 1.1 thorpej vlan_status(void)
140 1.1 thorpej {
141 1.1 thorpej struct vlanreq vlr;
142 1.1 thorpej
143 1.1 thorpej if (checkifname(ifr.ifr_name))
144 1.1 thorpej return;
145 1.1 thorpej
146 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
147 1.1 thorpej ifr.ifr_data = (void *)&vlr;
148 1.1 thorpej
149 1.1 thorpej if (ioctl(s, SIOCGETVLAN, &ifr) == -1)
150 1.1 thorpej return;
151 1.1 thorpej
152 1.1 thorpej if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
153 1.1 thorpej printf("\tvlan: %d parent: %s\n",
154 1.1 thorpej vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
155 1.1 thorpej "<none>" : vlr.vlr_parent);
156 1.1 thorpej }
157