updated on Sat Jan 21 16:18:39 UTC 2012
[aur-mirror.git] / sova / vcfg
blobc5016bc517b15d6fefc931e25eec775f6b182044
1 #!/bin/bash
3 # VLAN management script v2.1
5 # by Bart Kos, bro at fast-stable-secure dot net
7 # http://www.fast-stable-secure.net
10 ### devel
11 # uncomment the line below to see how the script works (e.g. for debugging)
12 #set -x
14 ### pre-run checks
15 # check for root privileges
16 if [ "$UID" != "0" ]
17 then
18 echo "Error: You must be root to run this script."
19 exit
22 ### search for required binaries
23 # search for `ip' binary (iproute2 suite)
24 if [ `which ip | wc -l` = 1 ]
25 then
26 ip="`which ip`"
27 else
28 echo "Error: You require the 'ip' binary from the iproute2 suite to run this script."
29 echo "Install the suite and/or update your PATH setting."
30 exit
33 # see if the `vconfig' binary is present (linux vlan software)
34 if [ `which vconfig | wc -l` = 1 ]
35 then
36 vconfig="`which vconfig`"
37 else
38 echo "Error: You require the 'vconfig' binary from the Linux VLAN package to run this script."
39 echo "Install the missing software and/or update your PATH setting."
40 exit
43 ### vars definition
44 var1="$1" # add/rem or add-addr/rem-addr
45 var2="$2" # <interface> or <vlan number>
46 var3="$3" # <vlan number> or <ip address>
47 var4="$4" # <ip address> or <scope>
48 var5="$5" # <scope>
50 ### functions
51 # check if the running Linux kernel supports VLAN architecture
52 function check_for_vlans()
54 if [ ! -f /proc/net/vlan/config ] && [ ! -f /lib/modules/`uname -r`/kernel/net/8021q/8021q.ko ]
55 then
56 echo "Error: no VLAN kernel capabilities detected."
57 echo "You need a VLAN-enabled Linux kernel for this script to work."
58 exit
59 else
60 echo "Linux VLAN environment sane."
64 # load the kernel module (if necessary) and set the VLAN naming scheme
65 function prepare_vlan_env()
67 if [ ! -f /proc/net/vlan/config ]
68 then
69 modprobe -q 8021q
70 $vconfig set_name_type VLAN_PLUS_VID_NO_PAD
71 else
72 $vconfig set_name_type VLAN_PLUS_VID_NO_PAD
76 # create a VLAN virtual interface on top of a (physical) host interface
77 function add_vlan_if()
79 if [ `grep -c "\<VLAN_NAME_TYPE_PLUS_VID_NO_PAD\>" /proc/net/vlan/config` = 0 ]
80 then
81 echo "ERROR: Activate the script with 'vcfg initialise' first."
82 exit
85 if [ "$var5" != "link" ] && [ "$var5" != "global" ] && [ "$var5" != "host" ] && [ "$var5" != "site" ]
86 then
87 echo "ERROR: No or wrong parameters specified."
88 echo "See 'vcfg -h' for help on scipt usage."
89 echo "See 'man ip' for more info on the <scope> parameter."
90 exit
93 if [ `grep -c "\<$var2\>" /proc/net/dev` = 1 ]
94 then
95 if [ -f /proc/net/vlan/vlan$var3 ]
96 then
97 echo "ERROR: trying to add VLAN #$var3 to IF -:$var2:- error: File exists"
98 exit
101 if [ "$var3" -gt 4094 ]
102 then
103 $vconfig add $var2 $var3
104 exit
107 if [ `$ip link show dev $var2 | grep -c "\<UP\>"` = 0 ]
108 then
109 $ip link set $var2 up
110 $ip addr flush dev $var2 2> /dev/null > /dev/null
113 $vconfig add $var2 $var3
114 $ip link set vlan$var3 up
115 $ip addr flush dev $var2 2> /dev/null > /dev/null
116 $ip addr add $var4 dev vlan$var3 broadcast + scope $var5
117 else
118 $vconfig add $var2 $var3
119 exit
123 # add an address to an existing VLAN interface
124 function add_vlan_addr()
126 if [ "$var4" != "link" ] && [ "$var4" != "global" ] && [ "$var4" != "host" ] && [ "$var4" != "site" ]
127 then
128 echo "Error: No or wrong parameters specified."
129 echo "See 'vcfg -h' for help on scipt usage."
130 echo "See 'man ip' for more info on the <scope> parameter."
131 exit
134 $ip addr add $var3 dev vlan$var2 broadcast + scope $var4
137 # remove a VLAN interface
138 function rem_vlan_if()
140 if [ -f /proc/net/vlan/vlan$var2 ]
141 then
142 var100="`grep "Device:" /proc/net/vlan/vlan$var2 | awk '{print $2}'`"
144 $ip link set vlan$var2 down
145 $ip addr flush dev vlan$var2 2> /dev/null > /dev/null
146 $vconfig rem vlan$var2
148 if [ `grep -c "\<$var100\>" /proc/net/vlan/config` = 0 ]
149 then
150 $ip link set $var100 down
152 else
153 $vconfig rem vlan$var2
157 # remove an address from a working VLAN interface
158 # OBSOLETE
159 #function rem_vlan_addr()
161 #$ip addr del $var3 dev vlan$var2
164 # remove all vlan interfaces and shutdown their host interfaces
165 function rem_all_vlans()
167 # host interfaces list
168 var100=(`grep "vlan" /proc/net/vlan/config | awk '{print $5}'`)
169 # vlan interfaces list
170 var101=(`ls /proc/net/vlan/ | grep "vlan"`)
172 for vlans in ${var101[*]}
174 $ip link set $vlans down
175 $ip addr flush dev $vlans 2> /dev/null > /dev/null
176 $vconfig rem $vlans
177 done
179 for hostinterfaces in ${var100[*]}
181 $ip link set $hostinterfaces down
182 $ip addr flush dev $hostinterfaces 2> /dev/null > /dev/null
183 done
186 ## tests
187 # OBSOLETE, I think ;)
188 # test the first input var (add, rem, add-addr, rem-addr)
189 #function test_s1()
191 #if [ "$var1" != "add" ] && [ "$var1" != "rem" ] && [ "$var1" != "rem-all" ]
192 #then
193 # echo "ERROR: unknown command."
194 # echo "The available choices are:"
195 # echo "'add' to create a VLAN interface over a host interface."
196 # # OBSOLETE
197 # #echo "'add-addr' to add an IP address to an existing vlan address."
198 # echo "'rem' to remove a VLAN interface with its IP addresses."
199 # # OBSOLETE
200 # #echo "'rem-addr' to remove an IP address from an existing VLAN interface."
201 # echo "'rem-all' to remove all VLAN interfaces and shutdown their host IFs."
202 # exit
206 ### the main part
207 # print the help text if no command is passed to the script
208 if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]
209 then
210 echo ""
211 echo "Available commands are:"
212 echo ""
213 echo "add <interface> <vlan number> <ip address> <scope>"
214 echo " Creates a VLAN interface over a desired physical interface."
215 echo ""
216 echo "add-addr <vlan number> <ip address> <scope>"
217 echo " Adds an IP address to an existing VLAN interface."
218 echo ""
219 echo "rem <vlan number>"
220 echo " Removes a VLAN interface with all IP addresses attached to it."
221 echo ""
222 # OBSOLETE
223 #echo "rem-addr <vlan number> <ip address>"
224 #echo " Removes an IP address from an existing VLAN interface."
225 #echo ""
226 echo "rem-all"
227 echo " Removes all VLAN interfaces and brings down their host interfaces."
228 echo ""
229 echo "initialise"
230 echo " Prepares a working environment for the script to operate."
231 echo ""
232 echo "check"
233 echo " Checks if the current Linux environment accepts VLANs."
234 echo ""
236 # add a VLAN interface
237 elif [ "$1" = "add" ]
238 then
239 add_vlan_if
241 # remove a VLAN interface
242 elif [ "$1" = "rem" ]
243 then
244 rem_vlan_if
246 # add an IP address to an existing VLAN interface
247 elif [ "$1" = "add-addr" ]
248 then
249 add_vlan_addr
251 # remove an IP address from an existing VLAN interface
252 # OBSOLETE
253 #elif [ "$1" = "rem-addr" ]
254 #then
255 # rem_vlan_addr
257 # initialise the Linux VLAN subsystem
258 elif [ "$1" = "initialise" ]
259 then
260 prepare_vlan_env
262 # check VLAN environment sanity
263 elif [ "$1" = "check" ]
264 then
265 check_for_vlans
267 # remove all VLANs and shutdown their host interfaces
268 elif [ "$1" = "rem-all" ]
269 then
270 rem_all_vlans
272 # if there is no matching command, print an error message
273 else
274 echo "Error: unknown command."
275 echo "See 'vcfg -h' for the list of available commands."
278 # EOF