3 # Copyright (c) 2016 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ IDENT(1)
31 # $Title: if_bridge(4) management script for vnet jails $
33 ############################################################ INFORMATION
35 # Use this tool with jail.conf(5) (or rc.conf(5) ``legacy'' configuration) to
36 # manage `vnet' interfaces for jails. Designed to automate the creation of vnet
37 # interface(s) during jail `prestart' and destroy said interface(s) during jail
40 # In jail.conf(5) format:
42 # ### BEGIN EXCERPT ###
45 # host.hostname = "xxx.yyy";
49 # # NB: Below 2-lines required
50 # # NB: The number of eNb_xxx interfaces should match the number of
51 # # arguments given to `jib addm xxx' in exec.prestart value.
54 # vnet.interface = "e0b_xxx e1b_xxx ...";
57 # exec.system_user = "root";
58 # exec.jail_user = "root";
61 # # NB: Below 2-lines required
62 # # NB: The number of arguments after `jib addm xxx' should match
63 # # the number of eNb_xxx arguments in vnet.interface value.
65 # exec.prestart += "jib addm xxx em0 em1 ...";
66 # exec.poststop += "jib destroy xxx";
69 # exec.start += "/bin/sh /etc/rc";
70 # exec.stop = "/bin/sh /etc/rc.shutdown";
71 # exec.consolelog = "/var/log/jail_xxx_console.log";
74 # # Optional (default off)
76 # #allow.set_hostname = 1;
78 # #devfs_ruleset = "11"; # rule to unhide bpf for DHCP
83 # In rc.conf(5) ``legacy'' format (used when /etc/jail.conf does not exist):
85 # ### BEGIN EXCERPT ###
91 # # Global presets for all jails
93 # jail_devfs_enable="YES" # mount devfs
96 # # Global options (default off)
98 # #jail_mount_enable="YES" # mount /etc/fstab.{name}
99 # #jail_set_hostname_allow="YES" # Allow hostname to change
100 # #jail_sysvipc_allow="YES" # Allow SysV Interprocess Comm.
103 # jail_xxx_hostname="xxx.shxd.cx" # hostname
104 # jail_xxx_rootdir="/vm/xxx" # root directory
105 # jail_xxx_vnet_interfaces="e0b_xxx e1bxxx ..." # vnet interface(s)
106 # jail_xxx_exec_prestart0="jib addm xxx em0 em1 ..." # bridge interface(s)
107 # jail_xxx_exec_poststop0="jib destroy xxx" # destroy interface(s)
108 # #jail_xxx_mount_enable="YES" # mount /etc/fstab.xxx
109 # #jail_xxx_devfs_ruleset="11" # rule to unhide bpf for DHCP
111 # ### END EXCERPT ###
113 # Note that the legacy rc.conf(5) format is converted to
114 # /var/run/jail.{name}.conf by /etc/rc.d/jail if jail.conf(5) is missing.
116 # ASIDE: dhclient(8) inside a vnet jail...
118 # To allow dhclient(8) to work inside a vnet jail, make sure the following
119 # appears in /etc/devfs.rules (which should be created if it doesn't exist):
121 # [devfsrules_jail=11]
122 # add include $devfsrules_hide_all
123 # add include $devfsrules_unhide_basic
124 # add include $devfsrules_unhide_login
125 # add path 'bpf*' unhide
127 # And set ether devfs.ruleset="11" (jail.conf(5)) or
128 # jail_{name}_devfs_ruleset="11" (rc.conf(5)).
130 # NB: While this tool can't create every type of desirable topology, it should
131 # handle most setups, minus some which considered exotic or purpose-built.
133 ############################################################ GLOBALS
135 pgm="${0##*/}" # Program basename
143 ############################################################ FUNCTIONS
147 local action usage descr
149 echo "Usage: $pgm action [arguments]"
157 eval usage=\"\$jib_${action}_usage\"
158 [ "$usage" ] || continue
159 eval descr=\"\$jib_${action}_descr\"
160 printf "\t%s\n\t\t%s\n" "$usage" "$descr"
167 local usage action="$1"
168 eval usage=\"\$jib_${action}_usage\"
169 echo "Usage: $pgm $usage" >&2
173 mustberoot_to_continue()
175 if [ "$( id -u )" -ne 0 ]; then
176 echo "Must run as root!" >&2
181 jib_addm_usage="addm [-b BRIDGE_NAME] NAME interface0 [interface1 ...]"
182 jib_addm_descr="Creates e0b_NAME [e1b_NAME ...]"
185 local OPTIND=1 OPTARG flag bridge=bridge
186 while getopts b: flag; do
188 b) bridge="${OPTARG:-bridge}" ;;
189 *) action_usage addm # NOTREACHED
192 shift $(( $OPTIND - 1 ))
195 [ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -gt 1 ] ||
196 action_usage addm # NOTREACHED
199 mustberoot_to_continue
201 local iface iface_devid eiface_devid
202 local eiface_devid_a eiface_devid_b
203 local new num quad i=0
206 # 1. Make sure the interface doesn't exist already
207 ifconfig "e${i}a_$name" > /dev/null 2>&1 && continue
209 # 2. Bring the interface up
210 ifconfig $iface up || return
212 # 3. Make sure the interface has been bridged
213 if ! ifconfig "$iface$bridge" > /dev/null 2>&1; then
214 new=$( ifconfig bridge create ) || return
215 ifconfig $new addm $iface || return
216 ifconfig $new name "$iface$bridge" || return
217 ifconfig "$iface$bridge" up || return
220 # 4. Create a new interface to the bridge
221 new=$( ifconfig epair create ) || return
222 ifconfig "$iface$bridge" addm $new || return
224 # 5. Rename the new interface
225 ifconfig $new name "e${i}a_$name" || return
226 ifconfig ${new%a}b name "e${i}b_$name" || return
227 ifconfig "e${i}a_$name" up || return
228 ifconfig "e${i}b_$name" up || return
231 # 6. Set the MAC address of the new interface using a sensible
232 # algorithm to prevent conflicts on the network.
234 # The formula I'm using is ``NP:SS:SS:II:II:II'' where:
235 # + N denotes 4 bits used as a counter to support branching
236 # each parent interface up to 15 times under the same jail
237 # name (see S below).
238 # + P denotes the special nibble whose value, if one of
239 # 2, 6, A, or E (but usually 2) denotes a privately
240 # administered MAC address (while remaining routable).
241 # + S denotes 16 bits, the sum(1) value of the jail name.
242 # + I denotes bits that are inherited from parent interface.
244 # The S bits are a CRC-16 checksum of NAME, allowing the jail
245 # to change the epair(4) generation order without affecting the
246 # MAC address. Meanwhile, if...
247 # + the jail NAME changes (e.g., it was duplicated and given
248 # a new name with no other changes)
249 # + the underlying network interface changes
250 # + the jail is moved to another host
251 # the MAC address will be recalculated to a new, similarly
252 # unique value preventing conflict.
254 iface_devid=$( ifconfig $iface ether | awk '/ether/,$0=$2' )
255 eiface_devid=${iface_devid#??:??:??}
256 num=$( set -- `echo -n $name | sum` && echo $1 )
257 quad=$(( $num & 15 ))
259 10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
260 13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
262 eiface_devid=$quad$eiface_devid
264 quad=$(( $num & 15 ))
266 10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
267 13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
269 eiface_devid=$quad$eiface_devid
271 quad=$(( $num & 15 ))
273 10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
274 13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
276 eiface_devid=$quad:$eiface_devid
278 quad=$(( $num & 15 ))
280 10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
281 13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
283 case "$iface_devid" in
285 eiface_devid_a=2:$quad$eiface_devid
286 eiface_devid_b=6:$quad$eiface_devid
289 eiface_devid_a=2:$quad$eiface_devid
290 eiface_devid_b=e:$quad$eiface_devid
292 eval num=\$_${iface}_num
295 eval _${iface}_num=$num
298 local _${iface}_num=$num
300 quad=$(( $num & 15 ))
302 10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
303 13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
305 eiface_devid_a=$quad$eiface_devid_a
306 eiface_devid_b=$quad$eiface_devid_b
307 ifconfig "e${i}a_$name" ether $eiface_devid_a > /dev/null 2>&1
308 ifconfig "e${i}b_$name" ether $eiface_devid_b > /dev/null 2>&1
310 i=$(( $i + 1 )) # on to next e{i}b_name
314 jib_show_usage="show"
315 jib_show_descr="List possible NAME values for \`show NAME'"
316 jib_show1_usage="show NAME"
317 jib_show1_descr="Lists e0b_NAME [e1b_NAME ...]"
318 jib_show2_usage="show [NAME]"
321 local OPTIND=1 OPTARG flag
322 while getopts "" flag; do
324 *) action_usage show2 # NOTREACHED
327 shift $(( $OPTIND - 1 ))
328 if [ $# -eq 0 ]; then
332 sub(/:.*/, "", iface)
336 for (n = split($0, group); n > 1; n--) {
337 if (group[n] != "bridge") continue
342 xargs -rn1 ifconfig |
343 awk '$1 == "member:" &&
344 sub(/^e[[:digit:]]+a_/, "", $2), $0 = $2' |
348 ifconfig | awk -v name="$1" '
349 match($0, /^e[[:digit:]]+a_/) && sub(/:.*/, "") &&
350 substr($1, RSTART + RLENGTH) == name
354 jib_destroy_usage="destroy NAME"
355 jib_destroy_descr="Destroy e0b_NAME [e1b_NAME ...]"
358 local OPTIND=1 OPTARG flag
359 while getopts "" flag; do
361 *) action_usage destroy # NOTREACHED
364 shift $(( $OPTIND -1 ))
366 [ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -eq 1 ] ||
367 action_usage destroy # NOTREACHED
368 mustberoot_to_continue
369 jib_show "$name" | xargs -rn1 -I eiface ifconfig eiface destroy
372 ############################################################ MAIN
375 # Command-line arguments
378 [ "$action" ] || usage # NOTREACHED
381 # Validate action argument
383 if [ "$BASH_VERSION" ]; then
384 type="$( type -t "jib_$action" )" || usage # NOTREACHED
386 type="$( type "jib_$action" 2> /dev/null )" || usage # NOTREACHED
391 eval "jib_$action" \"\$@\"
393 *) usage # NOTREACHED
396 ################################################################################
398 ################################################################################