xref: /freebsd/share/examples/jails/jng (revision 7e4b7c797aedd8ca17aa921d235120eff101f787)
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 path 'bpf*' unhide
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		stats		\
158	; do
159		eval usage=\"\$jng_${action}_usage\"
160		[ "$usage" ] || continue
161		eval descr=\"\$jng_${action}_descr\"
162		printf "\t%s\n\t\t%s\n" "$usage" "$descr"
163	done
164	exit $FAILURE
165}
166
167action_usage()
168{
169	local usage action="$1"
170	eval usage=\"\$jng_${action}_usage\"
171	echo "Usage: $pgm $usage" >&2
172	exit $FAILURE
173}
174
175derive_mac()
176{
177	local OPTIND=1 OPTARG __flag
178	local __mac_num= __make_pair=
179	while getopts 2n: __flag; do
180		case "$__flag" in
181		2) __make_pair=1 ;;
182		n) __mac_num=${OPTARG%%[^0-9]*} ;;
183		esac
184	done
185	shift $(( $OPTIND - 1 ))
186
187	if [ ! "$__mac_num" ]; then
188		eval __mac_num=\${_${iface}_num:--1}
189		__mac_num=$(( $__mac_num + 1 ))
190		eval _${iface}_num=\$__mac_num
191	fi
192
193	local __iface="$1" __name="$2" __var_to_set="$3" __var_to_set_b="$4"
194	local __iface_devid __new_devid __num __new_devid_b
195	#
196	# Calculate MAC address derived from given iface.
197	#
198	# The formula I'm using is ``NP:SS:SS:II:II:II'' where:
199	# + N denotes 4 bits used as a counter to support branching
200	#   each parent interface up to 15 times under the same jail
201	#   name (see S below).
202	# + P denotes the special nibble whose value, if one of
203	#   2, 6, A, or E (but usually 2) denotes a privately
204	#   administered MAC address (while remaining routable).
205	# + S denotes 16 bits, the sum(1) value of the jail name.
206	# + I denotes bits that are inherited from parent interface.
207	#
208	# The S bits are a CRC-16 checksum of NAME, allowing the jail
209	# to change link numbers in ng_bridge(4) without affecting the
210	# MAC address. Meanwhile, if...
211	#   + the jail NAME changes (e.g., it was duplicated and given
212	#     a new name with no other changes)
213	#   + the underlying network interface changes
214	#   + the jail is moved to another host
215	# the MAC address will be recalculated to a new, similarly
216	# unique value preventing conflict.
217	#
218	__iface_devid=$( ifconfig $__iface ether | awk '/ether/,$0=$2' )
219	__new_devid=${__iface_devid#??:??:??}
220	# :II:II:II => S:II:II:II
221	__num=$( set -- `echo -n "$__name" | sum` && echo $1 )
222	__new_devid=$( printf %x $(( $__num & 15 )) )$__new_devid
223	# S:II:II:II => :SS:II:II:II
224	__num=$(( $__num >> 4 ))
225	__new_devid=:$( printf %x $(( $__num & 15 )) )$__new_devid
226	# :SS:II:II:II => S:SS:II:II:II
227	__num=$(( $__num >> 4 ))
228	__new_devid=$( printf %x $(( $__num & 15 )) )$__new_devid
229	# S:SS:II:II:II => :SS:SS:II:II:II
230	__new_devid=:$( printf %x $(( $__num & 15 )) )$__new_devid
231	# :SS:SS:II:II:II => P:SS:SS:II:II:II
232	case "$__iface_devid" in
233	   ?2:*) __new_devid=a$__new_devid __new_devid_b=e$__new_devid ;;
234	?[Ee]:*) __new_devid=2$__new_devid __new_devid_b=6$__new_devid ;;
235	      *) __new_devid=2$__new_devid __new_devid_b=e$__new_devid
236	esac
237	# P:SS:SS:II:II:II => NP:SS:SS:II:II:II
238	__new_devid=$( printf %x $(( $__mac_num & 15 )) )$__new_devid
239	__new_devid_b=$( printf %x $(( $__mac_num & 15 )) )$__new_devid_b
240
241	#
242	# Return derivative MAC address(es)
243	#
244	if [ "$__make_pair" ]; then
245		if [ "$__var_to_set" -a "$__var_to_set_b" ]; then
246			eval $__var_to_set=\$__new_devid
247			eval $__var_to_set_b=\$__new_devid_b
248		else
249			echo $__new_devid $__new_devid_b
250		fi
251	else
252		if [ "$__var_to_set" ]; then
253			eval $__var_to_set=\$__new_devid
254		else
255			echo $__new_devid
256		fi
257	fi
258}
259
260mustberoot_to_continue()
261{
262	if [ "$( id -u )" -ne 0 ]; then
263		echo "Must run as root!" >&2
264		exit $FAILURE
265	fi
266}
267
268jng_bridge_usage="bridge [-b BRIDGE_NAME] NAME interface0 [interface1 ...]"
269jng_bridge_descr="Create ng0_NAME [ng1_NAME ...]"
270jng_bridge()
271{
272	local OPTIND=1 OPTARG flag bridge=bridge
273	while getopts b: flag; do
274		case "$flag" in
275		b) bridge="$OPTARG"
276		   [ "$bridge" ] || action_usage bridge ;; # NOTREACHED
277		*) action_usage bridge # NOTREACHED
278		esac
279	done
280	shift $(( $OPTIND - 1 ))
281
282	local name="$1"
283	[ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -gt 1 ] ||
284		action_usage bridge # NOTREACHED
285	shift 1 # name
286
287	mustberoot_to_continue
288
289	local iface eiface eiface_devid
290	local new num quad i=0
291	for iface in $*; do
292
293		# 0. Make sure the interface doesn't exist already
294		eiface=ng${i}_$name
295		ngctl msg "$eiface:" getifname > /dev/null 2>&1 && continue
296
297		# 1. Bring the interface up
298		ifconfig $iface up || return
299
300		# 2. Set promiscuous mode and don't overwrite src addr
301		ngctl msg $iface: setpromisc 1 || return
302		ngctl msg $iface: setautosrc 0 || return
303
304		# 3. Make sure the interface has been bridged
305		if ! ngctl info ${iface}bridge: > /dev/null 2>&1; then
306			ngctl mkpeer $iface: bridge lower link0 || return
307			ngctl connect $iface: $iface:lower upper link1 ||
308				return
309			ngctl name $iface:lower ${iface}bridge || return
310		fi
311
312		# 3.5. Optionally create a secondary bridge
313		if [ "$bridge" != "bridge" ] &&
314		   ! ngctl info "$iface$bridge:" > /dev/null 2>&1
315		then
316			num=2
317			while ngctl msg ${iface}bridge: getstats $num \
318				> /dev/null 2>&1
319			do
320				num=$(( $num + 1 ))
321			done
322			ngctl mkpeer $iface:lower bridge link$num link1 ||
323				return
324			ngctl name ${iface}bridge:link$num "$iface$bridge" ||
325				return
326		fi
327
328		# 4. Create a new interface to the bridge
329		num=2
330		while ngctl msg "$iface$bridge:" getstats $num > /dev/null 2>&1
331		do
332			num=$(( $num + 1 ))
333		done
334		ngctl mkpeer "$iface$bridge:" eiface link$num ether || return
335
336		# 5. Rename the new interface
337		while [ ${#eiface} -gt 15 ]; do # OS limitation
338			eiface=${eiface%?}
339		done
340		new=$( set -- `ngctl show -n "$iface$bridge:link$num"` &&
341			echo $2 ) || return
342		ngctl name "$iface$bridge:link$num" $eiface || return
343		ifconfig $new name $eiface || return
344		ifconfig $eiface up || return
345
346		#
347		# 6. Set the MAC address of the new interface using a sensible
348		# algorithm to prevent conflicts on the network.
349		#
350		derive_mac $iface "$name" eiface_devid
351		ifconfig $eiface ether $eiface_devid
352
353		i=$(( $i + 1 )) # on to next ng{i}_name
354	done # for iface
355}
356
357jng_graph_usage="graph [-f] [-T type] [-o output]"
358jng_graph_descr="Generate network graph (default output is \`jng.svg')"
359jng_graph()
360{
361	local OPTIND=1 OPTARG flag
362	local output=jng.svg output_type= force=
363	while getopts fo:T: flag; do
364		case "$flag" in
365		f) force=1 ;;
366		o) output="$OPTARG" ;;
367		T) output_type="$OPTARG" ;;
368		*) action_usage graph # NOTREACHED
369		esac
370	done
371	shift $(( $OPTIND - 1 ))
372	[ $# -eq 0 -a "$output" ] || action_usage graph # NOTREACHED
373	mustberoot_to_continue
374	if [ -e "$output" -a ! "$force" ]; then
375		echo "$output: Already exists (use \`-f' to overwrite)" >&2
376		return $FAILURE
377	fi
378	if [ ! "$output_type" ]; then
379		local valid suffix
380		valid=$( dot -Txxx 2>&1 )
381		for suffix in ${valid##*:}; do
382			[ "$output" != "${output%.$suffix}" ] || continue
383			output_type=$suffix
384			break
385		done
386	fi
387	ngctl dot | dot ${output_type:+-T "$output_type"} -o "$output"
388}
389
390jng_show_usage="show"
391jng_show_descr="List possible NAME values for \`show NAME'"
392jng_show1_usage="show NAME"
393jng_show1_descr="Lists ng0_NAME [ng1_NAME ...]"
394jng_show2_usage="show [NAME]"
395jng_show()
396{
397	local OPTIND=1 OPTARG flag
398	while getopts "" flag; do
399		case "$flag" in
400		*) action_usage show2 # NOTREACHED
401		esac
402	done
403	shift $(( $OPTIND - 1 ))
404	mustberoot_to_continue
405	if [ $# -eq 0 ]; then
406		ngctl ls | awk '$4=="bridge",$0=$2' |
407			xargs -rn1 -Ibridge ngctl show bridge: |
408			awk 'sub(/^ng[[:digit:]]+_/, "", $2), $0 = $2' |
409			sort -u
410		return
411	fi
412	ngctl ls | awk -v name="$1" '
413		match($2, /^ng[[:digit:]]+_/) &&
414			substr($2, RSTART + RLENGTH) == name &&
415			$4 == "eiface", $0 = $2
416	' | sort
417}
418
419jng_shutdown_usage="shutdown NAME"
420jng_shutdown_descr="Shutdown ng0_NAME [ng1_NAME ...]"
421jng_shutdown()
422{
423	local OPTIND=1 OPTARG flag
424	while getopts "" flag; do
425		case "$flag" in
426		*) action_usage shutdown # NOTREACHED
427		esac
428	done
429	shift $(( $OPTIND -1 ))
430	local name="$1"
431	[ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -eq 1 ] ||
432		action_usage shutdown # NOTREACHED
433	mustberoot_to_continue
434	jng_show "$name" | xargs -rn1 -I eiface ngctl shutdown eiface:
435}
436
437jng_stats_usage="stats NAME"
438jng_stats_descr="Show ng_bridge link statistics for NAME interfaces"
439jng_stats()
440{
441	local OPTIND=1 OPTARG flag
442	while getopts "" flag; do
443		case "$flag" in
444		*) action_usage stats # NOTREACHED
445		esac
446	done
447	shift $(( $OPTIND -1 ))
448	local name="$1"
449	[ "${name:-x}" = "${name#*[!0-9a-zA-Z_]}" -a $# -eq 1 ] ||
450		action_usage stats # NOTREACHED
451	mustberoot_to_continue
452	for eiface in $( jng_show "$name" ); do
453		echo "$eiface:"
454		ngctl show $eiface: | awk '
455		$3 == "bridge" && $5 ~ /^link/ {
456			bridge = $2
457			link = substr($5, 5)
458			system(sprintf("ngctl msg %s: getstats %u",
459				bridge, link))
460		}' | fmt 2 | awk '
461			/=/ && fl = index($0, "=") {
462				printf "%20s = %s\n",
463					substr($0, 0, fl-1),
464					substr($0, 0, fl+1)
465			}
466		' # END-QUOTE
467	done
468}
469
470############################################################ MAIN
471
472#
473# Command-line arguments
474#
475action="$1"
476[ "$action" ] || usage # NOTREACHED
477
478#
479# Validate action argument
480#
481if [ "$BASH_VERSION" ]; then
482	type="$( type -t "jng_$action" )" || usage # NOTREACHED
483else
484	type="$( type "jng_$action" 2> /dev/null )" || usage # NOTREACHED
485fi
486case "$type" in
487*function)
488	shift 1 # action
489	eval "jng_$action" \"\$@\"
490	;;
491*) usage # NOTREACHED
492esac
493
494################################################################################
495# END
496################################################################################
497