1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# Copyright (c) 2017-2019 David Steele <dsteele@gmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# License-Filename: LICENSE
#
# Copyright 2017 David Steele <steele@debian.org>
# This file is part of comitup
# Available under the terms of the GNU General Public License version 2
# or later
#
import logging
import subprocess
from comitup import modemgr
from comitup import nm
start_cmds = [
# HOTSPOT rules
"iptables -w -N COMITUP-OUT",
"iptables -w -A COMITUP-OUT "
"-p icmp --icmp-type destination-unreachable -j DROP", # noqa
"iptables -w -A COMITUP-OUT "
"-p icmp --icmp-type port-unreachable -j DROP", # noqa
"iptables -w -A COMITUP-OUT -j RETURN",
"iptables -w -I OUTPUT -o {ap} -j COMITUP-OUT",
]
end_cmds = [
# Clear HOTSPOT rules
"iptables -w -D OUTPUT -o {ap} -j COMITUP-OUT >/dev/null 2>&1",
"iptables -w -D COMITUP-OUT "
"-p icmp --icmp-type destination-unreachable " # noqa
"-j DROP >/dev/null 2>&1", # noqa
"iptables -w -D COMITUP-OUT "
"-p icmp --icmp-type port-unreachable " # noqa
"-j DROP >/dev/null 2>&1", # noqa
"iptables -w -D COMITUP-OUT -j RETURN >/dev/null 2>&1",
"iptables -w -X COMITUP-OUT >/dev/null 2>&1",
]
appliance_cmds = [
"iptables -w -t nat -N COMITUP-FWD",
"iptables -w -t nat -A COMITUP-FWD -o {link} -j MASQUERADE",
"iptables -w -t nat -A COMITUP-FWD -j RETURN",
"iptables -w -t nat -A POSTROUTING -j COMITUP-FWD",
"echo 1 > /proc/sys/net/ipv4/ip_forward",
]
appliance_clear = [
"iptables -w -t nat -D POSTROUTING -j COMITUP-FWD >/dev/null 2>&1",
"iptables -w -t nat -D COMITUP-FWD -o {link} "
"-j MASQUERADE >/dev/null 2>&1", # noqa
"iptables -w -t nat -D COMITUP-FWD -j RETURN >/dev/null 2>&1",
"iptables -w -t nat -X COMITUP-FWD >/dev/null 2>&1",
]
log = logging.getLogger('comitup')
def run_cmds(cmds):
linkdev = nm.device_name(modemgr.get_link_device())
apdev = nm.device_name(modemgr.get_ap_device())
for cmd in cmds:
subprocess.call(cmd.format(link=linkdev, ap=apdev), shell=True)
def state_callback(state, action):
if (state, action) == ('HOTSPOT', 'start'):
log.debug("Running iptables commands for HOTSPOT")
run_cmds(end_cmds)
run_cmds(start_cmds)
if modemgr.get_mode() == 'router':
run_cmds(appliance_clear)
log.debug("Done with iptables commands for HOTSPOT")
elif (state, action) == ('CONNECTED', 'start'):
log.debug("Running iptables commands for CONNECTED")
run_cmds(end_cmds)
if modemgr.get_mode() == 'router':
run_cmds(appliance_clear)
run_cmds(appliance_cmds)
log.debug("Done with iptables commands for CONNECTED")
def init_iptmgr():
pass
def main():
import six
print("applying rules")
run_cmds(start_cmds)
six.input("Press Enter to continue...")
run_cmds(end_cmds)
if __name__ == '__main__':
main()
|