2 # Copyright (C) 2017 Ian Kelling
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
19 # https://savannah.nongnu.org/projects/bash-bear-trap/
20 set -e; .
/usr
/local
/lib
/bash-bear
; set +e
25 usage: ${0##*/} [OPTS] start|stop NS_NAME
26 Nat a network namespace. systemd friendly
28 Also creates a mount namespace with a cloned /run/resolvconf.
30 -c, --create Create or destroy a named network namespace. When running from
31 the same network namespace as pid 1, this is set automatically.
32 A systemd created private network is in a network namespace
34 -n NETWORK x.x.x /24 private network to use. If not specified, uses
35 the first unused one starting at 10.173.1
36 -h, --help Show this help and exit.
40 If we do create the netns, to join it with a shell, we can do (as root)
41 /usr/bin/nsenter --mount=/root/mount_namespaces/NAME --net=/var/run/netns/NAME bash
43 If you dont care about the mount namespace, you can leave that option off.
48 From within a systemd network namespace, we nat it to the outside. This
49 would be called from ExecStartPre, and or subsequent units called with
50 JoinsNamespaceOf= and PrivateNetwork=true.
52 If resolvconf is installed, we create a named mount namespace under
53 /root/mount_namespaces, so we can alter some system config for this
54 namespace. systemd command lines would be prefixed with:
56 /usr/bin/nsenter --mount=/root/mount_namespaces/NS_NAME
58 Note, this means that they can't run as unpriveledged users, but once
59 systemd 233 comes out, it will have a bind mount option from within unit
60 files, so the mount namespace won't be needed for most use cases, and I
61 will update the script to that the mount namespace not created unless a
62 flag is passed in. Patch welcome to add that flag before then.
65 This script has a dependency
66 https://savannah.nongnu.org/projects/bash-bear-trap/ . Search the script for "source" to see where to install or modify the installed location.
69 Background on this project (you can skip if you like):
71 If we aren't creating a named network namespace, to join the namespace
73 nsenter -n -m -t \$(pgrep PROCESS_IN_NAMESPACE) bash
75 Note: if I knew how to easily ask systemd what pid a unit has, i would
78 "ip netns new ..." also does a mount namespace, then bind
79 mounts each file/dir in /etc/netns/NS_NAME to /etc/NS_NAME. Note,
80 for openvpn having it's own resolv.conf by using it's user script which
81 calls resolvconf, this doesn't help much. What we actually want to do is
82 copy /run/resolvconf somehwere then bind mount it on top of
86 Note: for debugging, adding set -x is a pretty good option.
88 Please email me if you have a patches, bugs, feedback, or republish this
89 somewhere else: Ian Kelling <ian@iankelling.org>.
95 #### begin arg parsing ####
97 temp
=$
(getopt
-l help,create hcn
: "$@") || usage
1
101 -c|
--create) create
=true
; shift ;;
102 -n) network
=$2; shift 2 ;;
105 *) echo "$0: Internal error!" ; exit 1 ;;
108 if (( $# != 2 )); then
113 nn
=$2 # namespace name
114 #### end arg parsing ####
116 #### begin sanity checking ####
118 if ! type -p ip
&>/dev
/null
; then
119 echo "please install the iproute2 package"
122 if ! type -p iptables
&>/dev
/null
; then
123 echo "please install the iptables package"
126 if $install_error; then
129 #### end sanity checking ####
135 if ! $create && [[ $
(readlink
/proc
/self
/ns
/net
) == "$(readlink /proc/1/ns/net)" ]]; then
139 # make the default network namespace be named
142 target
=/run
/netns
/default
143 if [[ ! -e $target && ! -L $target ]]; then
144 # -f to avoid a race condition with running twice
145 ln -sf /proc
/1/ns
/net
$target
148 ipd
() { ip
-n default
"$@"; }
151 # otherwise we are already in the network namespace and it's unnamed.
155 # run ip in the network namespace
156 ipnn
() { ip
$ipnnargs "$@"; }
158 # default network namespace exec
159 dexec
() { ip netns
exec default
"$@"; }
160 # mount namespace exec
161 mexec
() { /usr
/bin
/nsenter
--mount=/root
/mount_namespaces
/$nn "$@"; }
165 # note, in a previous commit i specified the output interface with -o,
166 # but that broke things when my gateway interface changed, and I can't
167 # see any advantage to it, so I removed it.
168 dexec iptables
-t nat
$1 POSTROUTING
-s $network.0/24 -j MASQUERADE \
169 -m comment
--comment "systemd network namespace nat"
174 if ! dexec iptables
-C "$@" &>/dev
/null
; then
175 dexec iptables
-I "$@"
181 if [[ $network ]]; then
186 ips
="$(ipd addr show | awk '$1 == "inet
" {print $2}')"
187 for ((i
=1; i
<= 254; i
++)); do
189 if printf "%s\n" "$ips" |
grep "^${network//./\\.}" >/dev
/null
; then
201 echo "$0: error: no open network found"
205 #### begin mount namespace setup ####
206 mkdir
-p /root
/mount_namespaces
207 if ! mountpoint
/root
/mount_namespaces
>/dev
/null
; then
208 mount
--bind /root
/mount_namespaces
/root
/mount_namespaces
210 # note: This is outside the mount condition because I've mysteriously
211 # had this become shared instead of private, perhaps it
212 # got remounted somehow and lost the setting.
213 mount
--make-private /root
/mount_namespaces
214 if [[ ! -e /root
/mount_namespaces
/$nn ]]; then
215 touch /root
/mount_namespaces
/$nn
217 if ! mountpoint
/root
/mount_namespaces
/$nn >/dev
/null
; then
218 # Here, we specify that we only want mount changes changes under
219 # this mountpoint to be propagated into the bind, but changes
220 # from within the bind do not propagate to outside the bind.
222 # slave is documented in.
223 # /usr/share/doc/linux-doc-4.9/Documentation/filesystems/sharedsubtree.txt.gz
224 # documentation on propagation is a bit weird because it
225 # confusingly talks about binds, namespaces, and mirrors (which
226 # seems to be just another name for bind), shared subtrees
227 # (which seems to a term for binds and namespaces), and does not
228 # properly specify whether the documentation applies to binds,
229 # namespaces, or both. Notably, propagation for binds is marked
230 # on the original mount point, and propagation for a mount
231 # namespace is marked on mounts within the namespace.
232 unshare
--propagation slave
--mount=/root
/mount_namespaces
/$nn /bin
/true
235 #### end mount namespace setup ####
240 ip
-n $nn link
set dev lo up
243 echo 1 | dexec
dd of
=/proc
/sys
/net
/ipv
4/ip_forward status
=none
245 # docker helpfully changes the default FORWARD to drop...
246 diptables-add FORWARD
-i $v0 -j ACCEPT
247 diptables-add FORWARD
-o $v0 -j ACCEPT
250 err-cleanup
() { stop
; }
251 ipnn link add
$v0 type veth peer name
$v1
252 ipnn link
set $v0 netns default
253 ipd addr add
$network.1/24 dev
$v0
255 nat
-C &>/dev
/null || nat
-A
256 ipnn addr add
$network.2/24 dev
$v1
258 ipnn route add default via
$network.1
260 ###### begin setup resolvconf
261 if [[ -e /run
/resolvconf
]]; then # resolvconf probably installed
262 resolv_copy
=/root
/resolvconf-
$nn
264 # this condition should never happen, just coding defensively
265 if mexec mountpoint
/run
/resolvconf
&>/dev
/null
; then
266 mexec umount
/run
/resolvconf
268 cp -aT /run
/resolvconf
$resolv_copy
269 if ! mexec mount
-o bind $resolv_copy /run
/resolvconf
; then
270 echo "error: resolv-conf bindmount failed"
273 # if running dnsmasq, we have 127.0.0.1 for dns, but it can't listen on the loopback
274 # in the network namespace, so adjust the address.
275 if mexec
[ -s /run
/resolvconf
/interface
/lo.dnsmasq
]; then
276 mexec
sed --follow-symlinks -i "s/nameserver 127\..*/nameserver $network.1/" /run
/resolvconf
/interface
/lo.dnsmasq
279 # and in debian based distros at least, it runs with --local-service, and needs a restart
280 # to know about the new local network
281 if [[ $
(systemctl
--no-pager show
-p ActiveState dnsmasq
) == ActiveState
=active
]]; then
282 systemctl restart dnsmasq
285 # background: if we did this in openvpn's resolv-conf script, we could guard it in
286 # if capsh --print|grep '\bcap_sys_admin\b' &>/dev/null
287 # and we could get $nn by
288 # config_basename=${config%%.*}
289 # config_basename=${config_basename##*/}
290 # but dnsmasq forces us to do it earlier.
292 fi # end if [[ -e /run/resolvconf ]]
293 ###### end setup resolvconf
299 if ipd link list
$v0 &>/dev
/null
; then
300 # this also deletes $v1 and the route we added.
305 if nat
-C &>/dev
/null
; then nat
-D; fi
307 dexec iptables
-D FORWARD
-i $v0 -j ACCEPT
&>/dev
/null ||
:
308 if $create && [[ -e /var
/run
/netns
/$nn ]]; then
312 # not sure this is necessary since we are tearing down the mount namespace
313 if mexec mountpoint
/run
/resolvconf
&>/dev
/null
; then
314 mexec umount
/run
/resolvconf
317 if mountpoint
/root
/mount_namespaces
/$nn >/dev
/null
; then
318 umount
/root
/mount_namespaces
/$nn
327 echo "$0: error: unsupported action"