Skip to content

Commit f18d17c

Browse files
authored
Make Nat Access configurable (#1)
* Add NAT_ACCEPT_TRAFFIC to be able to configure if we want to accept traffic or not from the child namespace. * Add up and down for nat setup To be able to use IPTABLES for configure traffic. * Replace the iptable to a call to the script * Split net access in own method * Fix script to be run in the root namespace * Don't mixup start and stop * Add missing selector * Add missing spaces * Fix iptables parameters
1 parent ee7c5b2 commit f18d17c

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

configs/netns

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@
2323

2424
# If you need static MAC
2525
#MACADDR=00:11:22:33:44:55
26+
27+
#NAT Accepting traffic back from the NetNS
28+
#By default, if the root NS contact a service
29+
#in the created NS, it won't get a response.
30+
#
31+
#Setting this setting to 1 add a iptable rule
32+
#to accept returning traffic
33+
#NAT_ACCEPT_TRAFFIC=1

scripts/netnsinit

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44

55
display_usage() {
66
echo "Auto configuration for systemd-named-netns."
7-
echo -e "\nUsage:\n\t$0 network-type ns-name \n"
7+
echo -e "\nUsage:\n\t$0 network-type ns-name [optional params]\n"
88
echo "Note: you may need root privileges for this."
99
}
1010

@@ -27,19 +27,39 @@ autoconfigure_tunnel() {
2727
}
2828

2929
autoconfigure_nat() {
30+
3031
# add default route if gateway undefined
3132
if [ -z "${GATEWAY}" -a -n "${IPADDR_OUTSIDE}" ]; then
3233
/bin/ip route add default via ${IPADDR_OUTSIDE%%/*}
3334
fi
35+
3436
return 0 # additional precation against "set -e" in case of future mods of this function
3537
}
3638

39+
autoconfigure_nat-access() {
40+
41+
if [ "${NAT_ACCEPT_TRAFFIC}" != "1" ]; then
42+
return 0
43+
fi
44+
if [ "$3" == "up" ]; then
45+
#Accept related traffic
46+
iptables -I INPUT -i ${DEVNAME_OUTSIDE} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
47+
48+
elif [ "$3" == "down" ]; then
49+
iptables -D INPUT -i ${DEVNAME_OUTSIDE} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
50+
fi
51+
52+
53+
return 0 # additional precation against "set -e" in case of future mods of this function
54+
}
3755
autoconfigure() {
3856
local NSTYPE=$1
3957
local NSNAME=$2
4058

4159
echo "Starting autoconfigure for $NSTYPE ${NSNAME}"
4260
DEVNAME_INSIDE=vn-${NSNAME}1
61+
DEVNAME_OUTSIDE=vn-${NSNAME}0
62+
4363
source /etc/default/netns
4464
! source "/etc/default/netns-${NSNAME}"
4565

@@ -60,7 +80,7 @@ case "$1" in
6080
display_usage
6181
exit 0
6282
;;
63-
"tunnel"|"bridge"|"nat")
83+
"tunnel"|"bridge"|"nat"|"nat-access")
6484
autoconfigure "$@"
6585
exit 0
6686
;;

services/netns-nat@.service

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ EnvironmentFile=-/etc/default/netns-%I
2222
ExecStart=/usr/bin/env iptables -t nat -A POSTROUTING -s ${IPADDR_OUTSIDE} -j MASQUERADE
2323
ExecStart=/usr/bin/env iptables -A FORWARD -i ${DEVNAME_OUTSIDE} -j ACCEPT
2424
ExecStart=/usr/bin/env iptables -A FORWARD -o ${DEVNAME_OUTSIDE} -j ACCEPT
25-
ExecStart=/usr/bin/env iptables -I INPUT -I ${DEVNAME_OUTSIDE} -j ACCEPT
25+
ExecStart=/usr/bin/env netnsinit nat-access %I up
2626

2727
ExecStart=/usr/bin/env ip netns exec %I /usr/bin/env netnsinit nat %I
2828

2929
ExecStop=/usr/bin/env iptables -D FORWARD -o ${DEVNAME_OUTSIDE} -j ACCEPT
3030
ExecStop=/usr/bin/env iptables -D FORWARD -i ${DEVNAME_OUTSIDE} -j ACCEPT
3131
ExecStop=/usr/bin/env iptables -t nat -D POSTROUTING -s ${IPADDR_OUTSIDE} -j MASQUERADE
32-
ExecStop=/usr/bin/env iptables -D INPUT -I ${DEVNAME_OUTSIDE} -j ACCEPT
32+
ExecStop=/usr/bin/env netnsinit nat-access %I down

0 commit comments

Comments
 (0)