diff --git a/files/etc/hotplug.d/iface/00-phantap-learn b/files/etc/hotplug.d/iface/00-phantap-learn deleted file mode 100644 index f69ebe5..0000000 --- a/files/etc/hotplug.d/iface/00-phantap-learn +++ /dev/null @@ -1,5 +0,0 @@ -BRIDGE=br-phantap - -if [ "$ACTION" = ifup -a "$DEVICE" == "$BRIDGE" ]; then - /etc/init.d/phantap-learn start -fi diff --git a/files/etc/init.d/phantap b/files/etc/init.d/phantap index d653e91..b0630dd 100755 --- a/files/etc/init.d/phantap +++ b/files/etc/init.d/phantap @@ -2,7 +2,7 @@ START=01 USE_PROCD=1 -EXTRA_COMMANDS="setup" +EXTRA_COMMANDS="setup block_net conf_net" boot() { @@ -16,12 +16,37 @@ boot() start_service() { procd_open_instance "phantap" - procd_set_param command /usr/bin/phantap + procd_set_param command /usr/sbin/phantap-learn -i br-phantap -v1 + procd_append_param command -b '/etc/init.d/phantap block_net' + procd_append_param command -c '/etc/init.d/phantap conf_net' procd_set_param stdout 1 procd_set_param stderr 1 procd_close_instance } +block_net() { + # block local output on br-phantap + ebtables -t filter -A phantap-drop -j DROP +} + +conf_net() { + # P_VICTIM_MAC P_VICTIM_IP P_NETMASK P_GATEWAY_MAC P_GATEWAY_IP P_DNS P_NTP + echo "conf_net: P_VICTIM_MAC=$P_VICTIM_MAC P_VICTIM_IP=$P_VICTIM_IP P_GATEWAY_MAC=$P_GATEWAY_MAC P_GATEWAY_IP=$P_GATEWAY_IP P_DNS=$P_DNS" + . /lib/functions.sh + + _config_firewall + + config_load phantap + # Run all commands in onnetconfig list + config_list_foreach main onnetconfig _handle_onconfig + + if [ "$P_DNS" != "0.0.0.0" ]; then + _config_dns + # Run all commands in ondnsconfig list + config_list_foreach main ondnsconfig _handle_onconfig + fi +} + setup() { uci set system.ntp.enabled=0 @@ -73,3 +98,68 @@ EOF uci commit } + +_config_dns() { + echo "nameserver $P_DNS" > /tmp/resolv.conf.auto + /etc/init.d/dnsmasq reload +} + +_config_firewall() { +. /lib/functions/network.sh + +P_INTF=phantap +network_get_device P_BRIDGE $P_INTF +[ -z "$P_BRIDGE" ] && { echo "Bridge not ready"; exit; } +P_BR_MAC=$(cat /sys/class/net/$P_BRIDGE/address) +network_get_ipaddr P_BR_IP $P_INTF +network_get_gateway P_GW_FAKEIP $P_INTF true + +# Integrate with OpenWRT firewall +cat > /tmp/phantap.firewall.$$ </dev/null \ +&& ebtables -t nat -I POSTROUTING -j phantap-snat +iptables -t nat -N phantap-snat 2>/dev/null +iptables -t nat -C POSTROUTING -o br-phantap -j phantap-snat \ +|| iptables -t nat -I POSTROUTING -o br-phantap -j phantap-snat + +# Cleanup old rules if exist +ebtables -t nat -F phantap-snat +iptables -t nat -F phantap-snat +$(if [ "$P_GATEWAY_IP" != "0.0.0.0" ]; then +cat < ([0-9a-f:]{17})[^:]+: (\d+\.\d+\.\d+\.\d+)\.?\d* > (\d+\.\d+\.\d+\.\d+)\.?\d*:.*/mysedworked \1 \2 \3 \4/') - # addresses="mysedworked 00:11:22:33:44:55 66:77:88:99:aa:bb 10.1.2.3 1.2.3.4" - set -- $addresses - if [ "$1" != "mysedworked" ]; then - echo "Unable to parse captured packet \"$packet\", retrying" - sleep 1 - continue - fi - victim_mac=$2 - gw_mac=$3 - victim_ip=$4 - echo "We got a winner! victim_mac=$2, gw_mac=$3, victim_ip=$4" - break -done - -br_mac=$(cat /sys/class/net/$BRIDGE/address) -network_get_ipaddr br_ip $INTF -network_get_gateway gw_fakeip $INTF true - -config_firewall() { - -# Integrate with OpenWRT firewall -cat > /tmp/phantap.firewall.$$ </dev/null \ -&& ebtables -t nat -I POSTROUTING -j phantap-snat -iptables -t nat -N phantap-snat 2>/dev/null -iptables -t nat -C POSTROUTING -o br-phantap -j phantap-snat \ -|| iptables -t nat -I POSTROUTING -o br-phantap -j phantap-snat - -# Cleanup old rules if exist -ebtables -t nat -F phantap-snat -iptables -t nat -F phantap-snat -$(if [ -n "${gw_ip}" ]; then -cat < [0-9a-f:]{17}.*+: (\d+\.\d+\.\d+\.\d+)\.\d+ > .*/mysedworked \1 \2/') - # dns="mysedworked 8.8.8.8" - set -- $dns - if [ "$1" != "mysedworked" ]; then - echo "Unable to parse captured packet \"$packet\", retrying" - sleep 1 - continue - fi - dns_ip=$2 - echo "We got a dns server! dns_ip=$2" - break -done - -echo "nameserver $dns_ip" > /tmp/resolv.conf.auto -/etc/init.d/dnsmasq reload - -# Run all commands in ondnsconfig list -config_list_foreach main ondnsconfig handle_onconfig - -echo "setup done, you now have access to internet" - -while true; do - gw_ip=$(ip neigh show | grep $gw_mac | grep -v 169.254.66.101 | sed -E 's/^(\d+\.\d+\.\d+\.\d+) dev .*/mysedworked \1 \2/') - # ip neigh show =="10.1.2.1 dev br-phantap lladdr 66:77:88:99:aa:bb PERMANENT" - if [ -z "$gw_ip" ]; then - sleep 1 - continue - fi - # gw_ip="mysedworked 10.1.2.1" - set -- $gw_ip - if [ "$1" != "mysedworked" ]; then - echo "Unable to parse neighbor information \"$gw_ip\", retrying" - sleep 1 - continue - fi - gw_ip=$2 - echo "We got the gateway IP! gw_ip=$gw_ip" - break -done - -config_firewall -