summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden@aidenw.net>2025-12-28 18:28:21 +0000
committerAiden Woodruff <aiden@aidenw.net>2025-12-28 18:28:21 +0000
commitd1f9a0bf22485c751b43b9c5c4e019194fd26a4c (patch)
tree9bb24d20a7da9679e8e657bc38c96fb610c39a9d
parent581d8fdfdc4e33ca480bf0cc614f8ca5ca41ba20 (diff)
downloadwjail-net.tar.gz
wjail-net.tar.bz2
wjail-net.zip
add new netup script and designnet
- design.md: new design idea to integrate all commands into wjail executable. - netup.sh: add all steps and options as used now. - netupgrade.sh: add upgrade steps that need to move to netup.sh - netdowngrade.sh: add downgrade steps that will move to netup.sh Signed-off-by: Aiden Woodruff <aiden@aidenw.net>
-rw-r--r--design.md7
-rwxr-xr-xnetdowngrade.sh8
-rwxr-xr-xnetup.sh119
-rwxr-xr-xnetupgrade.sh8
4 files changed, 118 insertions, 24 deletions
diff --git a/design.md b/design.md
new file mode 100644
index 0000000..26364d9
--- /dev/null
+++ b/design.md
@@ -0,0 +1,7 @@
1wjail start
2 stop
3 attach
4 net create
5 delete
6 masq
7 unmasq
diff --git a/netdowngrade.sh b/netdowngrade.sh
new file mode 100755
index 0000000..7b5ae3f
--- /dev/null
+++ b/netdowngrade.sh
@@ -0,0 +1,8 @@
1#!/bin/sh
2echo Removing iptables rule
3iptables -t nat -D POSTROUTING -s 10.2.1.1/24 -j MASQUERADE
4echo Remove container default gateway.
5ip netns exec www route del default gw 10.2.1.1
6echo Bring virtual interfaces down.
7ip netns exec www ip link set veth0 down
8ip link set veth-wjail-www down
diff --git a/netup.sh b/netup.sh
index 43a6c08..bd9ded8 100755
--- a/netup.sh
+++ b/netup.sh
@@ -1,41 +1,112 @@
1#!/bin/sh 1#!/bin/sh
2 2
3if [ "$(id -u)" -ne 0 ]; then 3# OPTIONS
4echo "You are not root." 4netup_host_ifname=
5exit 1 5netup_host_addr=10.1.1.1
6netup_ns=
7netup_ns_ifname=
8netup_ns_addr=10.1.1.2
9netup_pid=
10netup_pidfile=
11netup_quiet=
12netup_sandbox=
13print_usage () {
14 cat <<EOF
15USAGE: $0 [-fFhiInpPqs] ( (-p PIDFILE | -P PID) | (-u | -d) )
16EOF
17}
18print_help () {
19 cat <<EOF
20-f IFNAME Set host interface name
21-F IFNAME Set container interface name.
22-h Print this help message.
23-i ADDR Set host address (which is also the container default gateway,
24 except in sandbox mode).
25-I ADDR Set container address.
26-n NETNS Name for newly created network namespace to create
27-p PIDFILE Container pidfile.
28-P PID Container PID.
29-q Quiet mode.
30-s Sandbox mode (do not setup container gateway or NAT forwarding)
31EOF
32}
33OPTIND=0
34while getopts 'f:F:hi:I:n:p:P:qs' opt; do
35 case $opt in
36 f) netup_host_ifname=$OPTARG ;;
37 F) netup_ns_ifname=$OPTARG ;;
38 h) print_usage; print_help; exit 1 ;;
39 i) netup_host_addr=$OPTARG ;;
40 I) netup_ns_addr=$OPTARG ;;
41 n) netup_ns=$OPTARG ;;
42 p) netup_pidfile="$OPTARG" ;;
43 P) netup_pid=$OPTARG ;;
44 q) netup_quiet=1 ;;
45 s) netup_sandbox=1 ;;
46 ?) print_usage $0; exit 1 ;;
47 *) print_usage $0; exit 1 ;;
48 esac
49done
50shift $((OPTIND - 1))
51if [ "x$netup_ns" = x ]; then
52 echo "$0: Network namespace name (-n) is required." 2>&1
53 print_usage $0
54 exit 1
55elif [ "x$netup_pid" = x ] && [ "x$netup_pidfile" = x ]; then
56 echo "Exactly one of -p or -P must be specified." 2>&1
57 print_usage $0
58 exit 1
59elif [ "x$netup_pid" != x ] && [ "x$netup_pidfile" != x ]; then
60 echo "Exactly one of -p or -P must be specified." 2>&1
61 print_usage $0
62 exit 1
6fi 63fi
7 64
8pidfile=/srv/gitbot/wjail.pid 65if [ "$(id -u)" -ne 0 ]; then
66 echo "You are not root."
67 exit 1
68fi
9 69
10if ! [ -f "$pidfile" ]; then 70if [ "x$netup_pidfile" != x ]; then
11echo "wjail not running." 71 if ! netup_pid="$(cat "$netup_pidfile" 2>/dev/null)"; then
12exit 1 72 echo "$0: pidfile $netup_pidfile cannot be read." 3>&1
73 exit 1
74 fi
13fi 75fi
14 76
15PID="$(cat "$pidfile")" 77message () {
78 if ! [ "$netns_quiet" ]; then
79 echo $*
80 fi
81}
16 82
17# Attach iproute2 netns 83message [STATUS] Create container namespace name.
18ip netns attach gitbot $PID 84ip netns attach $netup_ns $netup_pid
19 85
20# Add veth devices 86message [STATUS] Create veth devices.
21ip link add veth-wjail type veth peer veth0 netns gitbot 87ip link add $netup_host_ifname type veth peer $netup_ns_ifname netns $netup_ns
22 88
23# Assign ip addresses 89message [STATUS] Assign ip addresses.
24ip addr add 10.1.1.1/24 dev veth-wjail 90ip addr add $netup_host_addr/24 dev $netup_host_ifname
25ip netns exec gitbot ip addr add 10.1.1.2/24 dev veth0 91ip netns exec $netup_ns ip addr add $netup_ns_addr/24 dev $netup_ns_ifname
26 92
27# Bring interfaces up 93message [STATUS] Bring veth interfaces up.
28ip link set veth-wjail up 94ip link set $netup_host_ifname up
29ip netns exec gitbot ip link set veth0 up 95ip netns exec $netup_ns ip link set $netup_ns_ifname up
30 96
31# Assign default gateway 97if [ "$netup_sandbox" ]; then
32ip netns exec gitbot route add default gw 10.1.1.1 98 message [STATUS] Adding firewall rule to block forwarded connections.
99 iptables -I FORWARD 1 -i $netup_host_ifname -o !$netup_host_ifname -j DROP
100else
101 message [STATUS] Enabling IP forwarding.
102 echo 1 > /proc/sys/net/ipv4/ip_forward
33 103
34# Enable IP forwarding 104 message [STATUS] Add NAT forwarding rule.
35echo 1 > /proc/sys/net/ipv4/ip_forward 105 iptables -t nat -A POSTROUTING -s $netup_ns_addr/24 -j MASQUERADE
36 106
37# Add NAT forwarding rule 107 message [STATUS] Assigning container default gateway.
38iptables -t nat -A POSTROUTING -s 10.1.1.2/16 -j MASQUERADE 108 ip netns exec $netup_ns route add default gw $netup_host_addr
109fi
39 110
40##################################################################### 111#####################################################################
41# If there are "no route to host" errors in the container, check: 112# If there are "no route to host" errors in the container, check:
diff --git a/netupgrade.sh b/netupgrade.sh
new file mode 100755
index 0000000..6164d9f
--- /dev/null
+++ b/netupgrade.sh
@@ -0,0 +1,8 @@
1#!/bin/sh
2echo Bringing virtual interfaces up.
3ip link set veth-wjail-www up
4ip netns exec www ip link set veth0 up
5echo Adding container default gateway.
6ip netns exec www route add default gw 10.2.1.1
7echo Add iptables masquerade
8iptables -t nat -A POSTROUTING -s 10.2.1.1/24 -j MASQUERADE