Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions scripts/network/prepare-tap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CONFIGURE_IPTABLE="false"
CONFIGURE_FIREWALLD="false"
CONFIGURE_TUNTAP_IF_EXISTS="false"

while getopts ":a:t:u:i:fo" opt; do
while getopts ":a:t:u:ifo" opt; do
case $opt in
a) ADDRESS="${OPTARG}"
;;
Expand Down Expand Up @@ -62,34 +62,35 @@ ip link set "${NAME}" up
echo "Assigning address ${ADDRESS} to device ${NAME}..."
ip addr add "${ADDRESS}" dev "${NAME}"

echo "Enabling ip forward..."
sysctl net.ipv4.ip_forward=1

if [[ "${CONFIGURE_FIREWALLD}" == "true" ]];
then
which firewall-cmd &>/dev/null || stop "Don't have the firewal-cmd tool"

echo "Adding to the trusted zone..."
firewall-cmd --zone=trusted --add-interface="${NAME}"
firewall-cmd --zone=trusted --add-interface="${NAME}" || true
fi

echo "${CONFIGURE_IPTABLE}"
if [[ "${CONFIGURE_IPTABLE}" == "true" ]];
then
which iptables &>/dev/null || stop "Don't have the iptables tool"

echo "Enabling ip forward..."
sysctl net.ipv4.ip_forward=1
which iptables-nft &>/dev/null || stop "Don't have the iptables tool"

echo "Preparing iptable..."
iptables -t nat -A POSTROUTING -s "${ADDRESS}" -j MASQUERADE
iptables -A FORWARD -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
iptables -A FORWARD -o "${NAME}" -d "${ADDRESS}" -j ACCEPT
iptables-nft -t nat -A POSTROUTING -s "${ADDRESS}" -j MASQUERADE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth it to make some checks instead of hardcoding the iptables command to be used? Something like:

IPTABLES=iptables
if command -v iptables-nft &> /dev/null; then
  IPTABLES=iptables-nft
fi

Then you can use it like this:

Suggested change
iptables-nft -t nat -A POSTROUTING -s "${ADDRESS}" -j MASQUERADE
"${IPTABLES}" -t nat -A POSTROUTING -s "${ADDRESS}" -j MASQUERADE

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this, but eventually decided that this script is purely a development help for us, and since we have more or less uniform dev environment it makes little sense. But if you insist I can add this.

iptables-nft -A FORWARD -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
iptables-nft -A FORWARD -o "${NAME}" -d "${ADDRESS}" -j ACCEPT

RULE_NR=$(iptables -t filter -L INPUT --line-numbers |\
RULE_NR=$(iptables-nft -t filter -L INPUT --line-numbers |\
grep "REJECT all" |\
awk '{print $1}')

# Excempt tun device from potentiall reject all rule
if [[ $RULE_NR == "" ]]; then
iptables -I INPUT -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
iptables-nft -I INPUT -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
else
iptables -I INPUT $((RULE_NR - 1)) -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
iptables-nft -I INPUT $((RULE_NR - 1)) -i "${NAME}" -s "${ADDRESS}" -j ACCEPT
fi
fi