Skip to content

Commit

Permalink
NAT: possibility to use NIC name instead of IP for aliasing, feature …
Browse files Browse the repository at this point in the history
…request/issues: cbsd#61
  • Loading branch information
olevole committed Feb 11, 2016
1 parent 57d9986 commit 09c4a25
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
31 changes: 24 additions & 7 deletions initenv.subr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ init_items_desc()
jnameserver_desc="Jails default DNS name-server"
nodeippool_desc="Jail pool IP address range"
nat_enable_desc="Enable NAT for RFC1918 networks"
natip_desc="NAT IP"
natip_desc="NAT IP or NIC"
fbsdrepo_desc="Use official FreeBSD repository? When no (0) repository of CBSD is preferred (usefull for stable=1)"
mdtmp_desc="Configure memory disk"
repo_desc="Use repository for images and template?"
Expand All @@ -27,7 +27,7 @@ init_items_desc()

init_items_default()
{
local INITIP=$(/sbin/ifconfig $( /sbin/route -n get 0.0.0.0 2>/dev/null | /usr/bin/awk '/interface/{print $2}' ) 2>/dev/null|/usr/bin/awk '/inet /{print $2}'|/usr/bin/head -n1)
local INITIP=$(/sbin/ifconfig $( /sbin/route -n get 0.0.0.0 2>/dev/null | /usr/bin/awk '/interface/{print $2}' ) 2>/dev/null|/usr/bin/awk '/inet [0-9]+/{print $2}'|/usr/bin/head -n1)

##default area
nodename_default=$( hostname )
Expand Down Expand Up @@ -70,10 +70,23 @@ make_nat()
local rfc1918="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16"
local _extiface

_extiface=$( /sbin/route -n get 0.0.0.0 | /usr/bin/awk '/interface:/{print $2}' )
iptype ${natip} >/dev/null 2>&1

/sbin/ifconfig ${_extiface} >/dev/null 2>&1
# if natip is not valid IPv4, assume it is NIC variable.
# so try to find out first IPv4 for aliasing
if [ $? -ne 1 ]; then
_extiface="${natip}"
natip=$( getip-by-nics nic=${_extiface} 2>/dev/null )
if [ $? -ne 0 ]; then
${ECHO} "${MAGENTA}Unable to determine first IPv4 for nic: ${GREEN}${_extiface}${NORMAL}"
return 1
fi
else
# natip is valid IPv4, search for default-gateway nic
_extiface=$( /sbin/route -n get 0.0.0.0 | /usr/bin/awk '/interface:/{print $2}' )
fi

/sbin/ifconfig ${_extiface} >/dev/null 2>&1
if [ $? -ne 0 ]; then
${ECHO} "Unable to determine default interface"
return 1
Expand Down Expand Up @@ -157,7 +170,7 @@ configure_nat()
{
ext_iface=$( /sbin/route -n get 0.0.0.0 |/usr/bin/awk '/interface/{print $2}' )

local _default
local _default _tmpdef
local _net _nm

. ${tools}
Expand All @@ -173,11 +186,15 @@ configure_nat()

iptype ${_default} >/dev/null 2>&1

# if natip is not valid IPv4, assume it is NIC variable.
# so try to find out first IPv4 for aliasing
if [ $? -ne 1 ]; then
_default=$( cbsd -c "cbsdsql local SELECT nodeip FROM local" )
_extiface="${_default}"
_tmpdef=$( getip-by-nics nic=${_extiface} 2>/dev/null )
[ -z "${_tmpdef}" ] && _default=$( cbsd -c "cbsdsql local SELECT nodeip FROM local" )
fi

${ECHO} "${BOLD}Set IP address as the aliasing NAT address, e.g: ${GREEN}${_default}${NORMAL}"
${ECHO} "${BOLD}Set IP address or NIC as the aliasing NAT address or interface, e.g: ${GREEN}${_default}${NORMAL}"
read natip
[ -z "${natip}" ] && natip="${_default}"

Expand Down
25 changes: 25 additions & 0 deletions tools/getip-by-nics
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/local/bin/cbsd
#v10.1.4
MYARG="nic"
MYOPTARG=""
MYDESC="Return first IPv4 on specified interface"

. ${subr}
. ${tools}

init $*

ifs=$( ${miscdir}/nics-list -s bridge |/usr/bin/xargs )


A=$( /sbin/ifconfig "${nic}" 2>/dev/null| /usr/bin/awk '/inet /{print $2}' 2>/dev/null | /usr/bin/head -n1 2>/dev/null )

[ -z "${A}" ] && err 1 "${A}"

iptype ${A} >/dev/null 2>&1

if [ $? -ne 1 ]; then
err 1 "Not IPv4: ${A}"
fi

err 0 "${A}"
4 changes: 2 additions & 2 deletions tools/getnics-by-ip
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ _inet=$?

[ ${_inet} -eq 0 ] && err 1 "not ip"

ifs=$( ${miscdir}/nics-list -s bridge |xargs)
ifs=$( ${miscdir}/nics-list -s bridge |/usr/bin/xargs )

for iface in ${ifs}; do
A=$( /sbin/ifconfig "${iface}" | /usr/bin/awk '/inet /{print $2}' | ${GREP_CMD} "^${IWM}$" 2>/dev/null )
A=$( /sbin/ifconfig "${iface}" | /usr/bin/awk '/inet [0-9]+/{print $2}' | ${GREP_CMD} "^${IWM}$" 2>/dev/null )
[ -z "${A}" ] || err 0 ${iface}
done

Expand Down
18 changes: 17 additions & 1 deletion tools/naton
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ADDHELP="nat= name of framework. default in inventory config\n"

. ${subr}
. ${system}
. ${tools}
. ${initenv}

init $*

Expand All @@ -23,6 +25,20 @@ ${ECHO} "${MAGENTA}CBSD: Enable IP forwarding for NAT service${NORMAL}"
/sbin/sysctl net.inet6.ip6.forwarding=1 >/dev/null
/sbin/sysctl net.inet.ip.forwarding=1 >/dev/null

iptype ${natip} >/dev/null 2>&1
# if natip is not valid IPv4, assume it is NIC variable.
# so try to find out first IPv4 for aliasing
if [ $? -ne 1 ]; then
_extiface="${natip}"
natip=$( getip-by-nics nic=${_extiface} 2>/dev/null )
if [ $? -ne 0 ]; then
${ECHO} "${MAGENTA}Unable to determine first IPv4 for nic: ${GREEN}${_extiface}${NORMAL}"
return 1
fi
ok="${nat_enable}" # used by make_nat
make_nat
fi

case "${nat_enable}" in
"pf")
[ ! -f "${etcdir}/pfnat.conf" ] && err 1 "${MAGENTA}No pfnat.conf. run ${GREEN}cbsd natcfg${MAGENTA} first${NORMAL}"
Expand All @@ -41,7 +57,7 @@ case "${nat_enable}" in
"ipfilter")
[ ! -f "${etcdir}/ipfilter.conf" ] && err 1 "No ipfilter.conf. run cbsd natcfg first"
/sbin/kldstat -qm ipfilter || /sbin/kldload ipfilter
/sbin/ping -c1 -S ${natip} 127.0.0.1 > /dev/null 2>&1 || err 1 "Cant assign nat address ${natip}. Probably wrong ip. Nat cfg skipped"
check_nat_ip
/sbin/ipnat -CF -f ${etcdir}/ipfilter.conf
;;
esac
2 changes: 1 addition & 1 deletion tools/netinv
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ update_sql()

_nic="${1}"
_mac=$( /sbin/ifconfig ${_nic} | /usr/bin/awk '/ether / {print $2}' )
_main4=$( /sbin/ifconfig $1 | /usr/bin/awk '/inet / { print $2, $4}' | /usr/bin/head -n 1 )
_main4=$( /sbin/ifconfig $1 | /usr/bin/awk '/inet [0-9]+/ { print $2, $4}' | /usr/bin/head -n 1 )
_main5=$( /sbin/ifconfig $1 | /usr/bin/awk '/inet6 / { print $2, $4}' | /usr/bin/head -n 1 )

if [ -n "${_main4}" ]; then
Expand Down
1 change: 0 additions & 1 deletion vnet.subr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ init_vnet()
gw_enable
}


# $1 - nicname (eg: bridge)
# if nicname=epair we search as epairXa
# show first available nic by type
Expand Down

0 comments on commit 09c4a25

Please sign in to comment.