3 # VLAN management script v2.1
5 # by Bart Kos, bro at fast-stable-secure dot net
7 # http://www.fast-stable-secure.net
11 # uncomment the line below to see how the script works (e.g. for debugging)
15 # check for root privileges
18 echo "Error: You must be root to run this script."
22 ### search for required binaries
23 # search for `ip' binary (iproute2 suite)
24 if [ `which ip | wc -l` = 1 ]
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."
33 # see if the `vconfig' binary is present (linux vlan software)
34 if [ `which vconfig | wc -l` = 1 ]
36 vconfig
="`which vconfig`"
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."
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>
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
]
56 echo "Error: no VLAN kernel capabilities detected."
57 echo "You need a VLAN-enabled Linux kernel for this script to work."
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
]
70 $vconfig set_name_type VLAN_PLUS_VID_NO_PAD
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 ]
81 echo "ERROR: Activate the script with 'vcfg initialise' first."
85 if [ "$var5" != "link" ] && [ "$var5" != "global" ] && [ "$var5" != "host" ] && [ "$var5" != "site" ]
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."
93 if [ `grep -c "\<$var2\>" /proc/net/dev` = 1 ]
95 if [ -f /proc
/net
/vlan
/vlan
$var3 ]
97 echo "ERROR: trying to add VLAN #$var3 to IF -:$var2:- error: File exists"
101 if [ "$var3" -gt 4094 ]
103 $vconfig add
$var2 $var3
107 if [ `$ip link show dev $var2 | grep -c "\<UP\>"` = 0 ]
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
118 $vconfig add
$var2 $var3
123 # add an address to an existing VLAN interface
124 function add_vlan_addr
()
126 if [ "$var4" != "link" ] && [ "$var4" != "global" ] && [ "$var4" != "host" ] && [ "$var4" != "site" ]
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."
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 ]
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 ]
150 $ip link
set $var100 down
153 $vconfig rem vlan
$var2
157 # remove an address from a working VLAN interface
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
179 for hostinterfaces
in ${var100[*]}
181 $ip link
set $hostinterfaces down
182 $ip addr flush dev
$hostinterfaces 2> /dev
/null
> /dev
/null
187 # OBSOLETE, I think ;)
188 # test the first input var (add, rem, add-addr, rem-addr)
191 #if [ "$var1" != "add" ] && [ "$var1" != "rem" ] && [ "$var1" != "rem-all" ]
193 # echo "ERROR: unknown command."
194 # echo "The available choices are:"
195 # echo "'add' to create a VLAN interface over a host interface."
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."
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."
207 # print the help text if no command is passed to the script
208 if [ "$1" = "" ] ||
[ "$1" = "-h" ] ||
[ "$1" = "help" ]
211 echo "Available commands are:"
213 echo "add <interface> <vlan number> <ip address> <scope>"
214 echo " Creates a VLAN interface over a desired physical interface."
216 echo "add-addr <vlan number> <ip address> <scope>"
217 echo " Adds an IP address to an existing VLAN interface."
219 echo "rem <vlan number>"
220 echo " Removes a VLAN interface with all IP addresses attached to it."
223 #echo "rem-addr <vlan number> <ip address>"
224 #echo " Removes an IP address from an existing VLAN interface."
227 echo " Removes all VLAN interfaces and brings down their host interfaces."
230 echo " Prepares a working environment for the script to operate."
233 echo " Checks if the current Linux environment accepts VLANs."
236 # add a VLAN interface
237 elif [ "$1" = "add" ]
241 # remove a VLAN interface
242 elif [ "$1" = "rem" ]
246 # add an IP address to an existing VLAN interface
247 elif [ "$1" = "add-addr" ]
251 # remove an IP address from an existing VLAN interface
253 #elif [ "$1" = "rem-addr" ]
257 # initialise the Linux VLAN subsystem
258 elif [ "$1" = "initialise" ]
262 # check VLAN environment sanity
263 elif [ "$1" = "check" ]
267 # remove all VLANs and shutdown their host interfaces
268 elif [ "$1" = "rem-all" ]
272 # if there is no matching command, print an error message
274 echo "Error: unknown command."
275 echo "See 'vcfg -h' for the list of available commands."