xref: /freebsd/share/examples/jails/jng (revision acbbf33c47fbbb90e2708f733bcc75763a5a9c72)
1#!/bin/sh
2#-
3# Copyright (c) 2016 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29############################################################ IDENT(1)
30#
31# $Title: netgraph(4) management script for vnet jails $
32#
33############################################################ INFORMATION
34#
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
38# `poststop'.
39#
40# In jail.conf(5) format:
41#
42# ### BEGIN EXCERPT ###
43#
44# xxx {
45# 	host.hostname = "xxx.yyy";
46# 	path = "/vm/xxx";
47#
48# 	#
49# 	# NB: Below 2-lines required
50# 	# NB: The number of ngN_xxx interfaces should match the number of
51# 	#     arguments given to `jng bridge xxx' in exec.prestart value.
52# 	#
53# 	vnet;
54# 	vnet.interface = "ng0_xxx ng1_xxx ...";
55#
56# 	exec.clean;
57# 	exec.system_user = "root";
58# 	exec.jail_user = "root";
59#
60# 	#
61# 	# NB: Below 2-lines required
62# 	# NB: The number of arguments after `jng bridge xxx' should match
63# 	#     the number of ngN_xxx arguments in vnet.interface value.
64# 	#
65# 	exec.prestart += "jng bridge xxx em0 em1 ...";
66# 	exec.poststop += "jng shutdown xxx";
67#
68# 	# Standard recipe
69# 	exec.start += "/bin/sh /etc/rc";
70# 	exec.stop = "/bin/sh /etc/rc.shutdown";
71# 	exec.consolelog = "/var/log/jail_xxx_console.log";
72# 	mount.devfs;
73#
74# 	# Optional (default off)
75# 	#allow.mount;
76# 	#allow.set_hostname = 1;
77# 	#allow.sysvipc = 1;
78# 	#devfs_ruleset = "11"; # rule to unhide bpf for DHCP
79# }
80#
81# ### END EXCERPT ###
82#
83# In rc.conf(5) ``legacy'' format (used when /etc/jail.conf does not exist):
84#
85# ### BEGIN EXCERPT ###
86#
87# jail_enable="YES"
88# jail_list="xxx"
89#
90# #
91# # Global presets for all jails
92# #
93# jail_devfs_enable="YES"	# mount devfs
94#
95# #
96# # Global options (default off)
97# #
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.
101#
102# # xxx
103# jail_xxx_hostname="xxx.shxd.cx"		# hostname
104# jail_xxx_rootdir="/vm/xxx"			# root directory
105# jail_xxx_vnet_interfaces="ng0_xxx ng1xxx ..."	# vnet interface(s)
106# jail_xxx_exec_prestart0="jng bridge xxx em0 em1 ..."	# bridge interface(s)
107# jail_xxx_exec_poststop0="jng shutdown 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
110#
111# ### END EXCERPT ###
112#
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.
115#
116# ASIDE: dhclient(8) inside a vnet jail...
117#
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):
120#
121# 	[devfsrules_jail=11]
122# 	add include $devfsrules_hide_all
123# 	add include $devfsrules_unhide_basic
124# 	add include $devfsrules_unhide_login
125# 	add include $devfsrules_unhide_bpf
126#
127# And set ether devfs.ruleset="11" (jail.conf(5)) or
128# jail_{name}_devfs_ruleset="11" (rc.conf(5)).
129#
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.
132#
133############################################################ GLOBALS
134
135pgm="${0##*/}" # Program basename
136
137#
138# Global exit status
139#
140SUCCESS=0
141FAILURE=1
142
143############################################################ FUNCTIONS
144
145usage()
146{
147	local action usage descr
148	exec >&2
149	echo "Usage: $pgm action [arguments]"
150	echo "Actions:"
151	for action in \
152		bridge		\
153		graph		\
154		show		\
155		show1		\
156		shutdown	\
157	; do
158		eval usage=\"\$jng_${action}_usage\"
159		[ "$usage" ] || continue
160		eval descr=\"\$jng_${action}_descr\"
161		printf "\t%s\n\t\t%s\n" "$usage" "$descr"
162	done
163	exit $FAILURE
164}
165
166action_usage()
167{
168	local usage action="$1"
169	eval usage=\"\$jng_${action}_usage\"
170	echo "Usage: $pgm $usage" >&2
171	exit $FAILURE
172}
173
174mustberoot_to_continue()
175{
176	if [ "$( id -u )" -ne 0 ]; then
177		echo "Must run as root!" >&2
178		exit $FAILURE
179	fi
180}
181
182jng_bridge_usage="bridge [-b BRIDGE_NAME] NAME interface0 [interface1 ...]"
183jng_bridge_descr="Create ng0_NAME [ng1_NAME ...]"
184jng_bridge()
185{
186	local OPTIND=1 OPTARG flag bridge=bridge
187	while getopts b: flag; do
188		case "$flag" in
189		b) bridge="$OPTARG"
190		   [ "$bridge" ] || action_usage bridge ;; # NOTREACHED
191		*) action_usage bridge # NOTREACHED
192		esac
193	done
194	shift $(( $OPTIND - 1 ))
195
196	local name="$1"
197	[ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -gt 1 ] ||
198		action_usage bridge # NOTREACHED
199	shift 1 # name
200
201	mustberoot_to_continue
202
203	local iface iface_devid eiface eiface_devid
204	local new num quad i=0
205	for iface in $*; do
206
207		# 0. Make sure the interface doesn't exist already
208		eiface=ng${i}_$name
209		ngctl msg "$eiface:" getifname > /dev/null 2>&1 && continue
210
211		# 1. Bring the interface up
212		ifconfig $iface up || return
213
214		# 2. Set promiscuous mode and don't overwrite src addr
215		ngctl msg $iface: setpromisc 1 || return
216		ngctl msg $iface: setautosrc 0 || return
217
218		# 3. Make sure the interface has been bridged
219		if ! ngctl info ${iface}bridge: > /dev/null 2>&1; then
220			ngctl mkpeer $iface: bridge lower link0 || return
221			ngctl connect $iface: $iface:lower upper link1 ||
222				return
223			ngctl name $iface:lower ${iface}bridge || return
224		fi
225
226		# 3.5. Optionally create a secondary bridge
227		if [ "$bridge" != "bridge" ] &&
228		   ! ngctl info "$iface$bridge:" > /dev/null 2>&1
229		then
230			num=2
231			while ngctl msg ${iface}bridge: getstats $num \
232				> /dev/null 2>&1
233			do
234				num=$(( $num + 1 ))
235			done
236			ngctl mkpeer $iface:lower bridge link$num link1 ||
237				return
238			ngctl name ${iface}bridge:link$num "$iface$bridge" ||
239				return
240		fi
241
242		# 4. Create a new interface to the bridge
243		num=2
244		while ngctl msg "$iface$bridge:" getstats $num > /dev/null 2>&1
245		do
246			num=$(( $num + 1 ))
247		done
248		ngctl mkpeer "$iface$bridge:" eiface link$num ether || return
249
250		# 5. Rename the new interface
251		while [ ${#eiface} -gt 15 ]; do # OS limitation
252			eiface=${eiface%?}
253		done
254		new=$( set -- `ngctl show -n "$iface$bridge:link$num"` &&
255			echo $2 ) || return
256		ngctl name "$iface$bridge:link$num" $eiface || return
257		ifconfig $new name $eiface || return
258
259		#
260		# 6. Set the MAC address of the new interface using a sensible
261		# algorithm to prevent conflicts on the network.
262		#
263		# The formula I'm using is ``NP:SS:SS:II:II:II'' where:
264		# + N denotes 4 bits used as a counter to support branching
265		#   each parent interface up to 15 times under the same jail
266		#   name (see S below).
267		# + P denotes the special nibble whose value, if one of
268		#   2, 6, A, or E (but usually 2) denotes a privately
269		#   administered MAC address (while remaining routable).
270		# + S denotes 16 bits, the sum(1) value of the jail name.
271		# + I denotes bits that are inherited from parent interface.
272		#
273		# The S bits are a CRC-16 checksum of NAME, allowing the jail
274		# to change link numbers in ng_bridge(4) without affecting the
275		# MAC address. Meanwhile, if...
276		#   + the jail NAME changes (e.g., it was duplicated and given
277		#     a new name with no other changes)
278		#   + the underlying network interface changes
279		#   + the jail is moved to another host
280		# the MAC address will be recalculated to a new, similarly
281		# unique value preventing conflict.
282		#
283		iface_devid=$( ifconfig $iface ether | awk '/ether/,$0=$2' )
284		eiface_devid=${iface_devid#??:??:??}
285		num=$( set -- `echo -n $name | sum` && echo $1 )
286		quad=$(( $num & 15 ))
287		case "$quad" in
288		10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
289		13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
290		esac
291		eiface_devid=$quad$eiface_devid
292		num=$(( $num >> 4 ))
293		quad=$(( $num & 15 ))
294		case "$quad" in
295		10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
296		13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
297		esac
298		eiface_devid=$quad$eiface_devid
299		num=$(( $num >> 4 ))
300		quad=$(( $num & 15 ))
301		case "$quad" in
302		10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
303		13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
304		esac
305		eiface_devid=$quad:$eiface_devid
306		num=$(( $num >> 4 ))
307		quad=$(( $num & 15 ))
308		case "$quad" in
309		10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
310		13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
311		esac
312		case "$iface_devid" in
313		?2:*) eiface_devid=a:$quad$eiface_devid ;;
314		*) eiface_devid=2:$quad$eiface_devid
315		esac
316		eval num=\$_${iface}_num
317		if [ "$num" ]; then
318			num=$(( $num + 1 ))
319			eval _${iface}_num=$num
320		else
321			num=0
322			local _${iface}_num=$num
323		fi
324		quad=$(( $num & 15 ))
325		case "$quad" in
326		10) quad=a ;; 11) quad=b ;; 12) quad=c ;;
327		13) quad=d ;; 14) quad=e ;; 15) quad=f ;;
328		esac
329		eiface_devid=$quad$eiface_devid
330		ifconfig $eiface ether $eiface_devid > /dev/null 2>&1
331
332		i=$(( $i + 1 )) # on to next ng{i}_name
333	done # for iface
334}
335
336jng_graph_usage="graph [-f] [-T type] [-o output]"
337jng_graph_descr="Generate network graph (default output is \`jng.svg')"
338jng_graph()
339{
340	local OPTIND=1 OPTARG flag
341	local output=jng.svg output_type= force=
342	while getopts fo:T: flag; do
343		case "$flag" in
344		f) force=1 ;;
345		o) output="$OPTARG" ;;
346		T) output_type="$OPTARG" ;;
347		*) action_usage graph # NOTREACHED
348		esac
349	done
350	shift $(( $OPTIND - 1 ))
351	[ $# -eq 0 -a "$output" ] || action_usage graph # NOTREACHED
352	mustberoot_to_continue
353	if [ -e "$output" -a ! "$force" ]; then
354		echo "$output: Already exists (use \`-f' to overwrite)" >&2
355		return $FAILURE
356	fi
357	if [ ! "$output_type" ]; then
358		local valid suffix
359		valid=$( dot -Txxx 2>&1 )
360		for suffix in ${valid##*:}; do
361			[ "$output" != "${output%.$suffix}" ] || continue
362			output_type=$suffix
363			break
364		done
365	fi
366	ngctl dot | dot ${output_type:+-T "$output_type"} -o "$output"
367}
368
369jng_show_usage="show"
370jng_show_descr="List possible NAME values for \`show NAME'"
371jng_show1_usage="show NAME"
372jng_show1_descr="Lists ng0_NAME [ng1_NAME ...]"
373jng_show2_usage="show [NAME]"
374jng_show()
375{
376	local OPTIND=1 OPTARG flag
377	while getopts "" flag; do
378		case "$flag" in
379		*) action_usage show2 # NOTREACHED
380		esac
381	done
382	shift $(( $OPTIND - 1 ))
383	mustberoot_to_continue
384	if [ $# -eq 0 ]; then
385		ngctl ls | awk '$4=="bridge",$0=$2' |
386			xargs -rn1 -Ibridge ngctl show bridge: |
387			awk 'sub(/^ng[[:digit:]]+_/, "", $2), $0 = $2' |
388			sort -u
389		return
390	fi
391	ngctl ls | awk -v name="$1" '
392		match($2, /^ng[[:digit:]]+_/) &&
393			substr($2, RSTART + RLENGTH) == name &&
394			$4 == "eiface", $0 = $2
395	' | sort
396}
397
398jng_shutdown_usage="shutdown NAME"
399jng_shutdown_descr="Shutdown ng0_NAME [ng1_NAME ...]"
400jng_shutdown()
401{
402	local OPTIND=1 OPTARG flag
403	while getopts "" flag; do
404		case "$flag" in
405		*) action_usage shutdown # NOTREACHED
406		esac
407	done
408	shift $(( $OPTIND -1 ))
409	local name="$1"
410	[ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -eq 1 ] ||
411		action_usage shutdown # NOTREACHED
412	mustberoot_to_continue
413	jng_show "$name" | xargs -rn1 -I eiface ngctl shutdown eiface:
414}
415
416############################################################ MAIN
417
418#
419# Command-line arguments
420#
421action="$1"
422[ "$action" ] || usage # NOTREACHED
423
424#
425# Validate action argument
426#
427if [ "$BASH_VERSION" ]; then
428	type="$( type -t "jng_$action" )" || usage # NOTREACHED
429else
430	type="$( type "jng_$action" 2> /dev/null )" || usage # NOTREACHED
431fi
432case "$type" in
433*function)
434	shift 1 # action
435	eval "jng_$action" \"\$@\"
436	;;
437*) usage # NOTREACHED
438esac
439
440################################################################################
441# END
442################################################################################
443