rename err to bash-bear
[distro-setup] / mail-route
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6
7 # http://www.apache.org/licenses/LICENSE-2.0
8
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
16
17 #set -x
18 #exec &> >(logger)
19
20 source /a/bin/bash-bear-trap/bash-bear
21
22 usage() {
23 cat <<'EOF'
24 Usage: mail-route up|down|show
25
26 Marks tcp packets on port 25, 143 and 587 to be routed through
27 a vpn ip. If called from --up/--down in openvpn, (we have multiple args) $1 is the
28 tun_dev, and action is from $script_type env variable, openvpn sets this.
29
30 It is idempotent.
31
32 I'm not using this script currently:
33 The problem with this approach is that if we dont have a non-vpn global
34 ipv6 address and route, we need to send all ipv6 traffic through the
35 vpn. Using a network namespace for the vpn fixes that, so I've switched
36 over to that. We could still use this script inside the network
37 namespace, and I have tested it, but it is just unneeded
38 complexity. Simpler to just have a default route to the vpn. This script
39 does separate out lan ip ranges for ipv4, but we could also do that with
40 a simple route.
41
42 EOF
43 exit $1
44 }
45
46 if (( $# < 1 )); then
47 usage 1
48 fi
49
50 up() {
51 start=true
52 stop=false
53 iptables_op=-A
54 ip_op=add
55 timeout_secs=20
56 if [[ ! $tun_dev ]]; then
57 # delays because I was running this outside of openvpn before
58 found=false
59 for ((i=1; i<=timeout_secs; i++)); do
60 tun_dev=$(ip a show to 10.8.0.4/24 | sed -rn '1s/^\S+\s+([^:]+).*/\1/p')
61 if [[ $tun_dev == tun* ]]; then
62 found=true
63 break
64 fi
65 sleep 1
66 done
67 if ! $found; then
68 echo "$0: error: timeout after $timeout_secs waiting for valid tun_dev, currently:$tun_dev"
69 exit 1
70 fi
71 fi
72 e() { echo "$0: $*"; "$@"; }
73 err-cleanup() { stop; }
74 modify
75 # we leave it as is even when stopping, because we would like it to be default, but the only way
76 # to change the default is for every device, and I want to avoid that, even though I wouldn't mind, others users of this script might.
77 val=$(sysctl -n net.ipv4.conf.$tun_dev.rp_filter)
78 if [[ $val != 2 ]]; then
79 echo "net.ipv4.conf.$tun_dev.rp_filter = $val"
80 e sysctl net.ipv4.conf.$tun_dev.rp_filter=2
81 fi
82
83 }
84 down() {
85 start=false
86 stop=true
87 iptables_op=-D
88 ip_op=del
89 # note, this is not going to work if the interface has been deleted.
90 # we could also check for an iptable rule that on some tun interface like the one
91 # we use, but meh, the way I'm using the script now, tun_dev is supplied by openvpn
92 if [[ ! $tun_dev ]]; then
93 tun_dev=$(ip a show to 10.8.0.4/24 | sed -rn '1s/^\S+\s+([^:]+).*/\1/p')
94 fi
95 e() { echo "$0: $*"; "$@" || printf "maybe ok failure: %s\n" "$*"; }
96 modify
97 }
98
99 show() {
100 e() { printf "=================================\n# %s\n\n" "$*"; "$@"; }
101 e iptables -t mangle -S
102 e ip6tables -t mangle -S
103 e iptables -t nat -S
104 e ip rule
105 e ip -6 rule
106 e ip route show table 1
107 e ip -6 route show table 1
108 e ip -6 route show default
109
110 tun_dev=$(ip a show to 10.8.0.4/24 | sed -rn '1s/^\S+\s+([^:]+).*/\1/p')
111 if [[ $tun_dev == tun* ]]; then
112 e sysctl net.ipv4.conf.$tun_dev.rp_filter
113 else
114 echo "$0: note, no tun device found"
115 fi
116 exit 0
117 }
118
119 runtest() {
120 # debugging:
121 #echo start=$start stop=$stop exists=$exists
122 { $start && ! $exists; } || { $stop && $exists; }
123 }
124
125 iptmod() { #iptables modify
126 local cmd="$*"
127 local exists=true
128 ${cmd/-[AD]/-C} &>/dev/null || exists=false
129 if runtest; then e $cmd; fi
130 }
131
132 # code common to start and stop.
133 modify() {
134 # match source or dest port. note, when we send to a port, it picks a random high port as
135 # the source.
136
137 if [[ $HOSTNAME == bk ]]; then
138 ports=25
139 ipsuf=5
140 else
141 ports=25,143,587
142 ipsuf=4
143 fi
144 iptcommon="OUTPUT -m tcp -p tcp -m multiport --ports $ports -j MARK --set-mark"
145 iptmod iptables -t mangle $iptables_op $iptcommon 0x1
146 iptmod iptables -t mangle $iptables_op $iptcommon 0x0 -d 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,127.0.0.0/8
147 # note, we could have used a custom chain and returned instead of setting the mark again.
148 # in case anyone was ever curious, the inverse of private ips is: #0.0.0.0/5,8.0.0.0/7,11.0.0.0/8,12.0.0.0/6,16.0.0.0/4,32.0.0.0/3,64.0.0.0/2,128.0.0.0/3,160.0.0.0/5,168.0.0.0/6,172.0.0.0/12,172.32.0.0/11,172.64.0.0/10,172.128.0.0/9,173.0.0.0/8,174.0.0.0/7,176.0.0.0/4,192.0.0.0/9,192.128.0.0/11,192.160.0.0/13,192.169.0.0/16,192.170.0.0/15,192.172.0.0/14,192.176.0.0/12,192.192.0.0/10,193.0.0.0/8,194.0.0.0/7,196.0.0.0/6,200.0.0.0/5,208.0.0.0/4,224.0.0.0/3
149 iptmod ip6tables -t mangle $iptables_op $iptcommon 0x1 -d 2000::/3
150
151 if [[ $tun_dev ]]; then
152 # when $tun_dev goes away, so does this rule.
153 iptmod iptables -t nat $iptables_op POSTROUTING -o $tun_dev -m mark --mark 0x1 -j SNAT --to-source 10.8.0.$ipsuf
154 iptmod ip6tables -t nat $iptables_op POSTROUTING -o $tun_dev -m mark --mark 0x1 -j SNAT --to-source 2600:3c00:e002:3800::$ipsuf
155 fi
156
157
158 iprulecmd="fwmark 1 table 1"
159 for v in -4 -6; do
160 exists=true; ip $v rule show $iprulecmd | grep . &>/dev/null || exists=false
161 if runtest; then e ip $v rule $ip_op $iprulecmd; fi
162 done
163
164 iproutecmd="default via 10.8.0.1 table 1"
165 exists=true; ip route show $iproutecmd 2>/dev/null | grep . &>/dev/null || exists=false
166 if runtest; then e ip route $ip_op $iproutecmd; fi
167 if [[ $tun_dev ]]; then
168 # when $tun_dev goes away, so does this route.
169 iproutecmd="default dev $tun_dev table 1"
170 exists=true; ip -6 route show $iproutecmd | grep . &>/dev/null || exists=false
171 if runtest; then e ip -6 route $ip_op $iproutecmd; fi
172
173 # We have to ensure we have a normal default ipv6 route, or else linux will
174 # refuse to do send ipv6 packets at all. When running in a network namespace,
175 # having the table 1 route is totally redundant since I dont setup a
176 # normal default ipv6 route. But maybe I will someday.
177 #
178 # We could only do this if we dont have a default route with [[ ! $(ip -6 r show default) ]] but
179 # metric seems to be perfectly good. 6000 because on my home comp,
180 # its about 6 times slower to ping google than the default 1024 metric.
181 # See commit 572dd5847440e58481ee2d7b9080 for using an ipv6 test.
182 iproutecmd="default dev $tun_dev"
183 exists=true; ip -6 route show $iproutecmd | grep . &>/dev/null || exists=false
184 if runtest; then e ip -6 route $ip_op $iproutecmd metric 6000; fi
185 fi
186 # on debian this is 0 (no filter), on ubuntu it\'s 1, which is no good. 0 or 2 both work fine.
187 # 2 drops it if the packet is not routable, martian address, or my default route is screwed up,
188 # so, eh, might as well. some rhel docs recommend using it.
189
190
191 }
192
193 if (( $# > 1 )); then
194 tun_dev=$1
195 # environment var from openvpn
196 # shellcheck disable=SC2154
197 $script_type
198 else
199 case $1 in
200 up|down|show) $1 ;;
201 *) usage 1 ;;
202 esac
203 fi
204
205 exit 0
206
207
208 # background: something like this does not work for packets which
209 # exim is replying to. I don't know why.
210 #iptables -t mangle -A OUTPUT -m owner --uid-owner Debian-exim -j MARK --set-mark 0x1
211 #
212 # note: exim will misreport the I= interface for remote hosts that would
213 # not use the default route. It still goes through the vpn, you can
214 # verify with tcpdump.