# This script controls actions to be taken when the tethering is started or stopped.
# @Version=17
# ------------------------------------------------------------------------------------
# It uses the "edify" language, which is also used for the android OTA update scripts. 
# See:
# http://android.git.kernel.org/?p=platform/bootable/recovery.git;a=tree;f=edify;h=04720f8aaa9a5e0079b79f8be7f11b7f74414162;hb=HEAD
# ------------------------------------------------------------------------------------

#
# Actions for starting tethering
#
action() == "start" && (
  #
  # Set "status"-Property
  #
  setprop("tether.status","running");
  #
  # Make sure to kill old processes
  #
  kill_pidfile("/data/data/com.googlecode.android.wifi.tether/var/fixpersist.pid");
  file_unlink("/data/data/com.googlecode.android.wifi.tether/var/fixpersist.pid");

  #
  # Device-specfic section
  #
  #getcfg("setup.section.generic") == "false" && (
  #	
  #);
  
  #
  # Generic section
  #
  getcfg("setup.section.generic") == "true" && (
	getcfg("wifi.driver") == "hostapd" && ( 
	  #
	  # Hostapd - Generic
	  #
	  module_loaded(getcfg("hostapd.module.name")) || log(insmod(getcfg("hostapd.module.path"), ""), "Loading kernel-module"); 
	  sleep("3");
	  getcfg("hostapd.loader.cmd") != "disabled" && (
	  		log("Loading firmware/configuration");
			run_program(getcfg("hostapd.loader.cmd"));	  
	  );
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	      run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");
	  sleep("1");
	  log(run_program(getcfg("hostapd.bin.path")+" -P /data/data/com.googlecode.android.wifi.tether/var/hostapd.pid -B /data/data/com.googlecode.android.wifi.tether/conf/hostapd.conf"), "Starting hostapd");
	);
	getcfg("wifi.driver") == "softap" && (  
	  #
	  # softap - Generic
	  getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.load.cmd") == "none" && (
	  	  log(load_wifi(), "Loading WiFi driver");
	  	);
	    getcfg("wifi.load.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	  log(run_program(getcfg("wifi.load.cmd")), "Loading WiFi driver (cmd)");
	  	);
	  	sleep("3");
	  );
	  log(softap_driverstop(getcfg("wifi.interface")), "Stopping driver");
	  log(load_firmware(getcfg("wifi.interface"), getcfg("wifi.firmware.path")), "Loading WiFi firmware");    
	  log(softap_driverstart(getcfg("wifi.interface")), "Starting driver");
	  log(softap_config(getcfg("wifi.interface"),getcfg("tether.interface"),getcfg("wifi.essid"),"ASCII_CMD=AP_CFG,SSID="+getcfg("wifi.essid")+",SEC="+getcfg("wifi.encryption")+",KEY="+gen_wpakey(getcfg("wifi.essid"),getcfg("wifi.encryption.key"))+",CHANNEL="+getcfg("wifi.channel")+",PREAMBLE=0,MAX_SCB=8,HIDE="+getcfg("wifi.essid.hide")+",END"), "Configuring softap");
	  log(softap_start(getcfg("tether.interface")), "Starting softap");
	  sleep("3");
	  #log(netd_cmd("interface setcfg "+getcfg("tether.interface")+" "+getcfg("ip.gateway")+" "+getcfg("ip.netmask")+" up"), "Configuring Ip-address");
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	      run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");   
	);

	getcfg("wifi.driver") == "softap_samsung" && (  
	  #
	  # softap - Samsung
	  #
	  getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.load.cmd") == "none" && (
	  	  log(load_wifi(), "Loading WiFi driver");
	  	);
	    getcfg("wifi.load.cmd") != "none" && (
	      run_program(getcfg("wifi.unload.cmd"));
	  	  log(run_program(getcfg("wifi.load.cmd")), "Loading WiFi driver (cmd)");
	  	);
	  	sleep("3");
	  );
	  log(softap_driverstop(getcfg("wifi.interface")), "Stopping driver");
	  log(load_firmware(getcfg("wifi.interface"), getcfg("wifi.firmware.path")), "Loading WiFi firmware");    
	  log(softap_driverstart(getcfg("wifi.interface")), "Starting driver");
	  getcfg("wifi.encryption") == "open" && (
		log(softap_config(getcfg("wifi.interface"),getcfg("tether.interface"),getcfg("wifi.essid"),"ASCII_CMD=AP_CFG,SSID="+getcfg("wifi.essid")+",SEC="+getcfg("wifi.encryption")+",KEY="+gen_wpakey(getcfg("wifi.essid"),getcfg("wifi.encryption.key"))+",CHANNEL="+getcfg("wifi.channel")+",PREAMBLE=0,MAX_SCB=5,HIDE="+getcfg("wifi.essid.hide")+",GROUP_CIPHER=4,802.11_MODE=1,WEP_KEY_INDEX=0,WEP_KEY=,WHITELIST_MAC_COUNT=0,WHITELIST_MAC_LIST=null,BLACKLIST_MAC_COUNT=0,BLACKLIST_MAC_LIST=null,END"), "Configuring softap");
	  );
	  getcfg("wifi.encryption") == "wpa2-psk" && (  
		log(softap_config(getcfg("wifi.interface"),getcfg("tether.interface"),getcfg("wifi.essid"),"ASCII_CMD=AP_CFG,SSID="+getcfg("wifi.essid")+",SEC="+getcfg("wifi.encryption")+",KEY="+gen_wpakey(getcfg("wifi.essid"),getcfg("wifi.encryption.key"))+",CHANNEL="+getcfg("wifi.channel")+",PREAMBLE=0,MAX_SCB=5,HIDE="+getcfg("wifi.essid.hide")+",GROUP_CIPHER=3,802.11_MODE=1,WEP_KEY_INDEX=0,WEP_KEY=,WHITELIST_MAC_COUNT=0,WHITELIST_MAC_LIST=null,BLACKLIST_MAC_COUNT=0,BLACKLIST_MAC_LIST=null,END"), "Configuring softap");
	  );
	  log(softap_start(getcfg("tether.interface")), "Starting softap");
	  sleep("3");   
	  #log(netd_cmd("interface setcfg "+getcfg("tether.interface")+" "+getcfg("ip.gateway")+" "+getcfg("ip.netmask")+" up"), "Configuring Ip-address");
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	        run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface")
	);
	
	getcfg("wifi.driver") == "netd" && (  
	  #
	  # Netd - Generic
	  # 
	  getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.load.cmd") == "none" && (
	  	  log(load_wifi(), "Loading WiFi driver");
	  	);
	    getcfg("wifi.load.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	  log(run_program(getcfg("wifi.load.cmd")), "Loading WiFi driver (cmd)");
	  	);
	  	sleep("3");
	  );
	  log(netd_cmd("softap stop "+getcfg("wifi.interface")),"Stopping WiFI interface");
	  log(netd_cmd("softap fwreload "+getcfg("wifi.interface")+" AP"),"Reloading firmware");
	  log(netd_cmd("softap start "+getcfg("wifi.interface")), "Starting AP interface");
	  log(netd_cmd("softap set "+getcfg("wifi.interface")+" "+getcfg("tether.interface")+" "+getcfg("wifi.essid")+" "+getcfg("wifi.encryption")+" "+getcfg("wifi.encryption.key")+" "+getcfg("wifi.channel")+" 0 8"), "Configuring AP");
	  log(netd_cmd("softap startap "+getcfg("tether.interface")), "Starting AP");
	  sleep("3");
	  log(netd_cmd("interface setcfg "+getcfg("tether.interface")+" "+getcfg("ip.gateway")+" "+getcfg("ip.netmask")+" up"), "Configuring Ip-address");
	  #log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	  #    run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");  	    
	);

	getcfg("wifi.driver") == "netdndc" && (  
	  #
	  # Netd - Generic
	  # 
	  getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.load.cmd") == "none" && (
	  	  log(load_wifi(), "Loading WiFi driver");
	  	);
	    getcfg("wifi.load.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	  log(run_program(getcfg("wifi.load.cmd")), "Loading WiFi driver (cmd)");
	  	);
	  	sleep("3");
	  );
	  log(run_program("/system/bin/ndc softap stopap"), "Stopping AP");
	  log(run_program("/system/bin/ndc softap stop "+getcfg("wifi.interface")),"Stopping WiFI interface");
	  log(run_program("/system/bin/ndc softap fwreload "+getcfg("wifi.interface")+" AP"),"Reloading firmware");
	  log(run_program("/system/bin/ndc softap start "+getcfg("wifi.interface")), "Starting AP interface");


	  log(run_program("/system/bin/ndc softap set "+getcfg("wifi.interface")+" "+getcfg("tether.interface")+" "+getcfg("wifi.essid")+" "+getcfg("wifi.encryption")+" "+getcfg("wifi.encryption.key")+" "+getcfg("wifi.channel")+" 0 8"), "Configuring AP");
	  log(run_program("/system/bin/ndc softap startap"), "Starting AP");
	  sleep("3");
	  log(run_program("/system/bin/ndc interface setcfg "+getcfg("tether.interface")+" "+getcfg("ip.gateway")+" "+getcfg("ip.netmask")+" up"), "Configuring Ip-address");
	  #log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	  #    run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");  	    
	);
	    
	getcfg("wifi.driver") == "wext" && (
	  #
	  # WEXT - Generic
	  # 
	  log(load_wifi(), "Loading WiFi driver");
	  sleep("3");
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	      run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");  	   
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	  getcfg("wifi.txpower") != "disabled" && (
		log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );      
	  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " commit");
	);
	
	getcfg("wifi.driver") == "tiwlan0" && (
      #
      # HTC dream/magic/tattoo/eris/hero
      #      
	  module_loaded("wlan") || log(insmod("/system/lib/modules/wlan.ko", ""), "Loading wlan.ko module");
      file_exists("/proc/calibration") && (
	      file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
	 	      log(run_program("/system/bin/wlan_loader -f /sdcard/android.tether/Fw1251r1c.bin -e " +
		                  "/proc/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /sdcard/android.tether)");
	      );
	      !file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
		      file_exists("/system/etc/wifi/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e " +
			                  "/proc/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/wifi)");
		      );
		      file_exists("/system/etc/firmware/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/firmware/Fw1251r1c.bin -e " +
			                  "/proc/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/firmware)");
		      );      
	      );
      );
      file_exists("/data/calibration") && (
 	      file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
	 	      log(run_program("/system/bin/wlan_loader -f /sdcard/android.tether/Fw1251r1c.bin -e " +
		                  "/data/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /sdcard/android.tether)");
	      );
	      !file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
		      file_exists("/system/etc/wifi/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e " +
			                  "/data/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/wifi)");
		      );
		      file_exists("/system/etc/firmware/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/firmware/Fw1251r1c.bin -e " +
			                  "/data/calibration -i /data/data/com.googlecode.android.wifi.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/firmware)");
		      );      
	      );     
      );
      #
      # Bring up wifi interface
      #
	  log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	      run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig " + getcfg("tether.interface") + " up"),  "Activating WiFi interface");  
    );
  );
    
  #
  # WEP-Encryption
  #
  getcfg("wifi.encryption") == "wep" && (
    getcfg("wifi.setup") == "iwconfig" && (      	
      log(run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " key s:"+getcfg("wifi.encryption.key")+"") &&
        run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " key restricted"), "Activating encryption<br/>(iwconfig)");
        run_program("/data/data/com.googlecode.android.wifi.tether/bin/iwconfig " + getcfg("tether.interface") + " commit");
    );
    getcfg("wifi.setup") == "wpa_supplicant" && (      	
      sleep("2");
	  log(run_program("cd /data/local/tmp; mkdir /data/local/tmp/wpa_supplicant; wpa_supplicant -B -D" +
	    getcfg("wifi.driver") +
	    " -i" + getcfg("tether.interface") +
	    " -c/data/data/com.googlecode.android.wifi.tether/conf/wpa_supplicant.conf"), "Activating encryption<br/>(wpa_supplicant)");
    );
  );

  #
  # Starting persist-fixer in background
  #    
  getcfg("tether.fix.persist") == "true" && (
    file_exists("/data/data/com.googlecode.android.wifi.tether/bin/fixpersist.sh") && (
	  run_program("exec /data/data/com.googlecode.android.wifi.tether/bin/fixpersist.sh "+ getcfg("tether.interface") +" "+ getcfg("wifi.essid") +" "+ getcfg("wifi.channel") +" & echo $! > /data/data/com.googlecode.android.wifi.tether/var/fixpersist.pid");
    );
  );

  #
  # Add ip route rule to use main routing table for LAN network
  #
  getcfg("tether.fix.route") == "true" && (
	log(run_program("/system/bin/ip rule add to "+ getcfg("ip.network") +"/24 lookup main priority 50") &&
	    run_program("/system/bin/ip route flush cache"), "Enabling Routing fix");
  );	
  
  #
  # Remove old rules
  #
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -N wireless-tether");
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -F wireless-tether");  
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -F FORWARD");
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F PREROUTING");
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F POSTROUTING");
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F");

  #
  # Bring up NAT rules
  #
  log(
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A wireless-tether -m state --state ESTABLISHED,RELATED -j ACCEPT") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A wireless-tether -s " + getcfg("ip.network") + "/24 -j ACCEPT") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A wireless-tether -p 47 -j ACCEPT") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A wireless-tether -j DROP") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A FORWARD -m state --state INVALID -j DROP") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -A FORWARD -j wireless-tether") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -I POSTROUTING -s " + getcfg("ip.network") + "/24 -j MASQUERADE"),
    "Enabling NAT rules");
  
  #
  # MSS Clamping
  #
  getcfg("mss.clamping") == "true" && ( 
    log(
      run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -I FORWARD -s " + getcfg("ip.network") + "/24 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu", "3"),
      "Enabling MSS Clamping");
  );

  #
  # IP forwarding
  #
  log(file_write("/proc/sys/net/ipv4/ip_forward", "1"), "Enabling IP forwarding");
 
  #
  # dnsmasq for wifi tether 
  #
  kill_process("dnsmasq");
  sleep("2");
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/dnsmasq -i " + getcfg("tether.interface") +" --dhcp-authoritative --no-negcache --user=root "+
                  "--no-resolv --no-hosts "+
                  "--server="+ getcfg("dns.primary") +" "+
                  "--server="+ getcfg("dns.secondary") +" "+
                  "--dhcp-range="+ getcfg("dhcp.iprange") +" " +
                  "--dhcp-leasefile=/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.leases " +
                  "--pid-file=/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.pid");
  
  #
  # Access control
  #
  file_exists("/data/data/com.googlecode.android.wifi.tether/conf/whitelist_mac.conf") &&
    log(
      run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -I PREROUTING -s " + getcfg("ip.network") + "/24 -j DROP") &&
      whitelist_macs("/data/data/com.googlecode.android.wifi.tether/conf/whitelist_mac.conf"),
    "Enabling access control."
    );
  log("Tethering now running");
  #
  # Set driver-status
  #
  setprop("wlan.driver.status","tether");
);

#
# Actions when stopping tether
#
action() == "stop" && (
  #
  # Set "status"-Property
  #
  setprop("tether.status","stopped");

  #
  # Disable forwarding and remove NAT rules.
  #
  log(file_write("/proc/sys/net/ipv4/ip_forward", "0"), "Disabling forwarding");
  
  log(
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -F wireless-tether") &&  
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -F FORWARD") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F PREROUTING") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F POSTROUTING") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -X wireless-tether"),
  "Disabling NAT rules");

  #
  # Wifi mode, bring interface down, kill dnsmasq/wpa_supplicant, remove module.
  #
  kill_process("wpa_supplicant");
  kill_process("dnsmasq");
  kill_pidfile("/data/data/com.googlecode.android.wifi.tether/var/hostapd.pid");
  kill_pidfile("/data/data/com.googlecode.android.wifi.tether/var/fixpersist.pid");
  file_unlink("/data/data/com.googlecode.android.wifi.tether/var/fixpersist.pid");

  #
  # Remove ip route rule to use main routing table for LAN network
  #
  getcfg("tether.fix.route") == "true" && (
	log(run_program("/system/bin/ip rule del to "+ getcfg("ip.network") +"/24 lookup main priority 50") &&
	    run_program("/system/bin/ip route flush cache") , "Disabling Routing fix");
  );
  
  run_program("/data/data/com.googlecode.android.wifi.tether/bin/ifconfig "+getcfg("tether.interface")+" down");

  is_substring("softap", getcfg("wifi.driver")) && (  
	log(softap_stop(getcfg("tether.interface")),"Stopping softap");
	log(softap_driverstop(getcfg("tether.interface")),"Stopping tether interface");
	log(softap_driverstop(getcfg("wifi.interface")),"Stopping driver");
	getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.unload.cmd") == "none" && (
	  	  unload_wifi();
	  	);
	    getcfg("wifi.unload.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	);
	  	setprop("wlan.driver.status","unloaded");
	);	
  );

  getcfg("wifi.driver") == "netd" && (  
    log(netd_cmd("softap stopap "+getcfg("tether.interface")), "Stopping AP interface");
    log(netd_cmd("softap stop "+getcfg("tether.interface")), "Stopping tether interface");
    log(netd_cmd("softap stop "+getcfg("wifi.interface")), "Stopping WiFi interface");
    log(netd_cmd("softap fwreload "+getcfg("wifi.interface")+""),"Reloading firmware");
	getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.unload.cmd") == "none" && (
	  	  unload_wifi();
	  	);
	    getcfg("wifi.unload.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	);
	  	setprop("wlan.driver.status","unloaded");
	);
  );

  getcfg("wifi.driver") == "netdndc" && (
	log(run_program("/system/bin/ndc softap stopap"), "Stopping AP");
	log(run_program("/system/bin/ndc softap stop "+getcfg("tether.interface")), "Stopping tether interface");
	log(run_program("/system/bin/ndc softap stop "+getcfg("wifi.interface")), "Stopping WiFi interface");
	log(run_program("/system/bin/ndc softap fwreload "+getcfg("wifi.interface")), "Reloading firmware");
	getcfg("wifi.driver.reload") == "true" && (
	    getcfg("wifi.unload.cmd") == "none" && (
	  	  unload_wifi();
	  	);
	    getcfg("wifi.unload.cmd") != "none" && (
	  	  run_program(getcfg("wifi.unload.cmd"));
	  	);
	  	setprop("wlan.driver.status","unloaded");
	);
  );

  getcfg("wifi.driver") == "hostapd" && ( 
    module_loaded(getcfg("hostapd.module.name")) &&  rmmod(getcfg("hostapd.module.name"));
    setprop("wlan.driver.status","unloaded");
  );
  
  getcfg("wifi.driver") == "wext" && (
    unload_wifi();
    setprop("wlan.driver.status","unloaded");
  );
  
  #
  # Make sure kernel-modules are unloaded
  #  
  getcfg("wifi.driver") == "wext" && (
  	module_loaded("bcm4330") && rmmod("bcm4330");
  	module_loaded("bcm4329") && rmmod("bcm4329");
  	module_loaded("bcm4325") && rmmod("bcm4325");
  	module_loaded("wlan") && rmmod("wlan");
  	module_loaded("tiwlan_drv") && rmmod("tiwlan_drv");
  	module_loaded("tiap_drv") && rmmod("tiap_drv");
  	module_loaded("sdio") && rmmod("sdio");
  	module_loaded("wireless") && rmmod("wireless");
  	module_loaded("ar6000") && rmmod("ar6000");
  );
  getcfg("wifi.driver") == "tiwlan0" && (
  	module_loaded("wlan") && rmmod("wlan");
  );
  
  #
  # Remove old dnsmasq.leases and pid-file
  #
  file_exists("/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.leases") && (
  	file_unlink("/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.leases");
  );
  file_exists("/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.pid") && (
    file_unlink("/data/data/com.googlecode.android.wifi.tether/var/dnsmasq.pid");
  );
  file_exists("/data/data/com.googlecode.android.wifi.tether/var/setssid.pid") && (
    file_unlink("/data/data/com.googlecode.android.wifi.tether/var/setssid.pid");
  );
  log("Tethering now stopped");
);

#
# Actions when restarting access control
#
action() == "restartsecwifi" && (
  #
  # Remove old rules
  #
  log (
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F PREROUTING") &&
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -F POSTROUTING"),
  "Disabling NAT rules");

  #
  # Bring up NAT rules
  #
  log(
    run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -I POSTROUTING -s " +
               getcfg("ip.network") + "/24 -j MASQUERADE"),
    "Enabling NAT rules");

  #
  # Access control
  #
  file_exists("/data/data/com.googlecode.android.wifi.tether/conf/whitelist_mac.conf") &&
    log(
      run_program("/data/data/com.googlecode.android.wifi.tether/bin/iptables -t nat -I PREROUTING -s " + getcfg("ip.network") + "/24 -j DROP") &&
      whitelist_macs("/data/data/com.googlecode.android.wifi.tether/conf/whitelist_mac.conf"),
    "Enabling access control."
    );
);

#
# Update log-file permission
#
run_program("/system/bin/chmod 666 /data/data/com.googlecode.android.wifi.tether/var/tether.log");