Skip to content

Commit 3d770ed

Browse files
committed
Dynamically create epair in bridge mode
Previously bastille in existing bridge mode created a static index for the epair. This has the unfortunate side effect of not handling existing epairs or new epairs being created outside bastille. Instead, use the returned name and create a bastille unique interface name to use rather than epair<NUM>.
1 parent c34ed5b commit 3d770ed

File tree

1 file changed

+25
-45
lines changed

1 file changed

+25
-45
lines changed

usr/local/share/bastille/common.sh

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ get_bastille_if_count() {
142142
if [ "${bastille_network_vnet_type}" = "if_bridge" ]; then
143143
for _config in /usr/local/etc/bastille/*.conf; do
144144
local bastille_jailsdir="$(sysrc -f "${_config}" -n bastille_jailsdir)"
145-
_bastille_if_list="$(printf '%s\n%s' "$( (grep -Ehos '(epair[0-9]+|bastille[0-9]+)' ${bastille_jailsdir}/*/jail.conf; ifconfig -g epair | grep -Eos "_bastille[0-9]+$"; ifconfig -g epair | grep -vs 'bastille' | grep -Eos 'e[0-9]+a_') | grep -Eos '[0-9]+')" "${_bastille_if_list}")"
145+
_bastille_if_list="$(printf '%s\n%s' "$( (grep -Ehos '(epair[0-9]+|bastille[0-9]+|e[0-9]+a_)' ${bastille_jailsdir}/*/jail.conf; ifconfig -g epair | grep -Eos "_bastille[0-9]+$") | grep -Eos '[0-9]+')" "${_bastille_if_list}")"
146146
done
147147
_bastille_if_count=$(printf '%s' "${_bastille_if_list}" | sort -u | wc -l | awk '{print $1}')
148148
export _bastille_if_list
@@ -388,30 +388,18 @@ generate_vnet_jail_netblock() {
388388
get_bastille_if_count
389389
local _bastille_if_num_range=$((_bastille_if_count + 1))
390390
if [ -n "${use_unique_bridge}" ]; then
391-
if [ "${_bastille_if_count}" -gt 0 ]; then
392-
for _num in $(seq 0 "${_bastille_if_num_range}"); do
393-
if ! echo "${_bastille_if_list}" | grep -oqswx "${_num}"; then
394-
if [ "$(echo -n "e${_num}a_${jail_name}" | awk '{print length}')" -lt 16 ]; then
395-
local host_epair=e${_num}a_${jail_name}
396-
local jail_epair=e${_num}b_${jail_name}
397-
else
398-
local host_epair=epair${_num}a
399-
local jail_epair=epair${_num}b
400-
fi
401-
break
391+
for _num in $(seq 0 "${_bastille_if_num_range}"); do
392+
if ! echo "${_bastille_if_list}" | grep -oqswx "${_num}"; then
393+
if [ "$(echo -n "e${_num}a_${jail_name}" | awk '{print length}')" -lt 16 ]; then
394+
local host_epair=e${_num}a_${jail_name}
395+
local jail_epair=e${_num}b_${jail_name}
396+
else
397+
local host_epair=e${_num}a__bastille
398+
local jail_epair=e${_num}b__bastille
402399
fi
403-
done
404-
else
405-
if [ "$(echo -n "e0a_${jail_name}" | awk '{print length}')" -lt 16 ]; then
406-
local _num=0
407-
local host_epair=e${_num}a_${jail_name}
408-
local jail_epair=e${_num}b_${jail_name}
409-
else
410-
local _num=0
411-
local host_epair=epair${_num}a
412-
local jail_epair=epair${_num}b
400+
break
413401
fi
414-
fi
402+
done
415403
else
416404
if [ "${_bastille_if_count}" -gt 0 ]; then
417405
for _num in $(seq 0 "${_bastille_if_num_range}"); do
@@ -426,36 +414,28 @@ generate_vnet_jail_netblock() {
426414
fi
427415
## If BRIDGE is enabled, generate bridge config, else generate VNET config
428416
if [ -n "${use_unique_bridge}" ]; then
417+
cat <<-EOF
418+
vnet;
419+
vnet.interface = ${jail_epair};
420+
exec.prestart += "epname=\\\$(ifconfig epair create) && ifconfig \\\${epname} name ${host_epair} && ifconfig \\\${epname%a}b name ${jail_epair}";
421+
exec.prestart += "ifconfig ${external_interface} addm ${host_epair}";
422+
exec.prestart += "ifconfig ${host_epair} description \"vnet host interface for Bastille jail ${jail_name}\"";
423+
exec.poststop += "ifconfig ${external_interface} deletem ${host_epair}";
424+
exec.poststop += "ifconfig ${host_epair} destroy";
425+
EOF
429426
if [ -n "${static_mac}" ]; then
430427
## Generate bridged VNET config with static MAC address
431428
generate_static_mac "${jail_name}" "${external_interface}"
432429
cat <<-EOF
433-
vnet;
434-
vnet.interface = ${jail_epair};
435-
exec.prestart += "ifconfig epair${_num} create";
436-
exec.prestart += "ifconfig ${external_interface} addm epair${_num}a";
437-
exec.prestart += "ifconfig epair${_num}a up name ${host_epair}";
438-
exec.prestart += "ifconfig epair${_num}b up name ${jail_epair}";
439430
exec.prestart += "ifconfig ${host_epair} ether ${macaddr}a";
440431
exec.prestart += "ifconfig ${jail_epair} ether ${macaddr}b";
441-
exec.prestart += "ifconfig ${host_epair} description \"vnet0 host interface for Bastille jail ${jail_name}\"";
442-
exec.poststop += "ifconfig ${external_interface} deletem ${host_epair}";
443-
exec.poststop += "ifconfig ${host_epair} destroy";
444432
EOF
445-
else
446-
## Generate bridged VNET config without static MAC address
447-
cat <<-EOF
448-
vnet;
449-
vnet.interface = ${jail_epair};
450-
exec.prestart += "ifconfig epair${_num} create";
451-
exec.prestart += "ifconfig ${external_interface} addm epair${_num}a";
452-
exec.prestart += "ifconfig epair${_num}a up name ${host_epair}";
453-
exec.prestart += "ifconfig epair${_num}b up name ${jail_epair}";
454-
exec.prestart += "ifconfig ${host_epair} description \"vnet0 host interface for Bastille jail ${jail_name}\"";
455-
exec.poststop += "ifconfig ${external_interface} deletem ${host_epair}";
456-
exec.poststop += "ifconfig ${host_epair} destroy";
433+
fi
434+
435+
cat <<-EOF
436+
exec.prestart += "ifconfig ${host_epair} up";
437+
exec.prestart += "ifconfig ${jail_epair} up";
457438
EOF
458-
fi
459439
else
460440
if [ "${bastille_network_vnet_type}" = "if_bridge" ]; then
461441
if [ -n "${static_mac}" ]; then

0 commit comments

Comments
 (0)