Ver Fonte

iptables/nfsv4 support

Alfonso Gamboa há 4 anos atrás
pai
commit
57fd177328
37 ficheiros alterados com 311 adições e 97 exclusões
  1. 38 14
      README.md
  2. BIN
      SD_ROOT/factory_t31_ZMC6tiIDQN
  3. BIN
      SD_ROOT/wz_mini/bin/bash
  4. 14 1
      SD_ROOT/wz_mini/bin/busybox
  5. 2 0
      SD_ROOT/wz_mini/bin/ip6tables
  6. 2 0
      SD_ROOT/wz_mini/bin/iptables
  7. BIN
      SD_ROOT/wz_mini/bin/iwconfig.sh
  8. 11 3
      SD_ROOT/wz_mini/etc/init.d/v3_init.sh
  9. 5 0
      SD_ROOT/wz_mini/etc/init.d/v3_post.sh
  10. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/nfs/nfsv4.ko
  11. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/lib/oid_registry.ko
  12. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/ip_tunnel.ko
  13. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arp_tables.ko
  14. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arpt_mangle.ko
  15. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arptable_filter.ko
  16. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ip_tables.ko
  17. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_ECN.ko
  18. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_REJECT.ko
  19. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_rpfilter.ko
  20. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_filter.ko
  21. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_mangle.ko
  22. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/tunnel4.ko
  23. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6_tables.ko
  24. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6t_REJECT.ko
  25. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_filter.ko
  26. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_mangle.ko
  27. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/sit.ko
  28. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/x_tables.ko
  29. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_HL_.ko
  30. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_hl.ko
  31. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_tcpudp.ko
  32. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/sunrpc/auth_gss/auth_rpcgss.ko
  33. BIN
      SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/wireguard/wireguard.ko
  34. 0 0
      SD_ROOT/wz_mini/log/.gitignore
  35. 0 0
      SD_ROOT/wz_mini/mnt/.gitignore
  36. 0 0
      SD_ROOT/wz_mini/root/.gitignore
  37. 239 79
      SD_ROOT/wz_mini/run_mmc.sh

+ 38 - 14
README.md

@@ -34,6 +34,7 @@ Using this project can potentially expose your device to the open internet depen
 * USB Mass storage enabled, mount USB SSD/HDD/flash drives
 * CIFS Supported
 * Play .WAV files using "aplay <file> <vol>" command
+* iptables included
 
 * Inspired by HclX and WyzeHacks!  Bless you for all your work!  You are the master!
 
@@ -81,28 +82,29 @@ Edit run_mmc.sh, this is a script stored on the micro sd card that is run when t
 Wireguard support is available as a kernel module:
 
 ```
-ENABLE_WIREGUARD="false"
+ENABLE_WIREGUARD="true"
 ```
 
 Use the command ```wg``` to setup.  See [https://www.wireguard.com/quickstart/](https://www.wireguard.com/quickstart/) for more info.
 
+Some users have asked about tailscale support, I have tested and it works.  See the issue #30 for further information.
 ---
 Disable automatic firmware updates:
 
 ```
-DISABLE_FW_UPGRADE="false"
+DISABLE_FW_UPGRADE="true"
 ```
 
 If a remote or app update is initiated, the camera will reboot due to the failure of the update.  The firmware update should not proceed again for some time, or at all.  
 
-When a firmware update is initiated, due to a bootloader bug, we intercept the update process and flash it manually.  This should now result in a successful update, if it doesn't, please restore the unit's firmware manually using demo_wcv3.bin on the micro sd card.
+When a firmware update is initiated, due to a bootloader issue (bug?), we intercept the update process and flash it manually.  This should now result in a successful update, if it doesn't, please restore the unit's firmware manually using demo_wcv3.bin on the micro sd card.
 
 ---
 
 USB Ethernet Adapter support:
 
 ```
-ENABLE_USB_ETH="false"
+ENABLE_USB_ETH="true"
 ```
 
 the next time you boot your camera, make sure your USB Ethernet Adapter is connected to the camera and ethernet.  The camera has to be setup initially with Wi-Fi for this to work.  After setup, Wi-Fi is no longer needed, as long as you are using the USB Ethernet Adapter.  Note that using USB Ethernet disables the onboard Wi-Fi.
@@ -112,13 +114,15 @@ the next time you boot your camera, make sure your USB Ethernet Adapter is conne
 USB Direct Support:
 
 ```
-ENABLE_USB_DIRECT="false"
+ENABLE_USB_DIRECT="true"
 ```
 
 the next time you boot your camera, make sure your USB cable is connected to the router.  Remember, the camera has to be setup initially with Wi-Fi for this to work.  After setup, Wi-Fi is no longer needed.  Note that using USB Direct disables the onboard Wi-Fi.  Change the MAC Address if you desire via USB_DIRECT_MAC_ADDR variable.
 
 Connectivity is supported using a direct USB connection only... this means a single cable from the camera, to a supported host (An OpenWRT router, for example) that supports the usb-cdc-ncm specification. (NCM, not ECM) If you have an OpenWrt based router, install the ```kmod-usb-net-cdc-ncm``` package.  The camera should automatically pull the IP from the router with most configurations.  You can also use any modern linux distro to provide internet to the camera, provided it supports CDC_NCM.  enjoy!
 
+Note: In my testing, the micro-usb cables included with the various cameras do not pass data, so they will not work.  Make sure you have a micro-usb cable that passes data!
+
 ---
 
 Remote Accessories:
@@ -142,12 +146,12 @@ Change ```/dev/ttyUSB0``` to whatever path your spotlight enumerated to if neces
 USB Mass Storage Support:
 
 ```
-ENABLE_USB_STORAGE="false"
+ENABLE_USB_STORAGE="true"
 ```
 If you would like to mount an EXT3/4 filesystem, also change:
 
 ```
-ENABLE_EXT4="false"
+ENABLE_EXT4="true"
 ```
 
 ---
@@ -155,27 +159,47 @@ ENABLE_EXT4="false"
 CIFS is now supported:
 
 ```
-ENABLE_CIFS="false"
+ENABLE_CIFS="true"
+```
+
+---
+
+iptables (ipv4 and ipv6) is available:
+
+```
+ENABLE_IPTABLES="true"
 ```
 
 ---
-To enable RTSP streaming, change the following lines, you can choose to enable or disable audio.  Set your login credentials here, you can also change the port the server listens on.
 
+NFSv4 support is available:
+
+```
+ENABLE_NFSv4="true"
 ```
-RTSP_ENABLED="false"
-RTSP_ENABLE_AUDIO="false"
+
+---
+RTSP streaming:
+You can choose to enable or disable audio.  Set your login credentials here, you can also change the port the server listens on.
+
+```
+RTSP_ENABLED="true"
+RTSP_ENABLE_AUDIO="true"
 RTSP_LOGIN="admin"
 RTSP_PASSWORD=""
 RTSP_PORT="8554"
 ```
 the stream will be located at ```rtsp://login:password@IP_ADDRESS:8554/unicast```
 
-Note:  If you don't set the password, then the password will be the unique MAC address of the camera, in all uppercase, including the colons... for example:. AA:BB:CC:00:11:22.  It's typically printed on the camera.  VLC seems to work fine for playback, ffmpeg and others have severe artifacts in the stream during playback.  Huge credit to @mnakada for his libcallback library: [https://github.com/mnakada/atomcam_tools](https://github.com/mnakada/atomcam_tools)
+Notes:  If you don't set the password, then the password will be the unique MAC address of the camera, in all uppercase, including the colons... for example:. AA:BB:CC:00:11:22.  It's typically printed on the camera.  VLC seems to work fine for playback, ffmpeg and others have severe artifacts in the stream during playback.
+
+Huge credit to @mnakada for his libcallback library: [https://github.com/mnakada/atomcam_tools](https://github.com/mnakada/atomcam_tools)
 
 ---
 
 ## Latest Updates
 
+* 05-08-22:  Include iptables and NFSv4 kernel modules, enable swap ON by default.
 * 05-07-22:  RTSP Server fixed, ported latest full libcallback from @mnakada with modifications.
 * 05-01-22:  Removed dropbearmulti, replaced with individual binaries.  dropbear dbclient dropbearkey dropbearconvert scp now included.
 * 04-30-22:  Recompiled uClibc with LD_DEBUG enabled. Enable in v3_post.sh, for debugging.
@@ -192,8 +216,8 @@ Note:  If you don't set the password, then the password will be the unique MAC a
 * 04-14-22:  Possible memory leak with some USB adapters used, added 128MB swap file and logic as workaround to prevent oom killing
 * 04-13-22:  Firmware updates are disabled by default, there is a bug in the bootloader that corrupts the kernel partition requiring the re-flash of the camera if an update is processed and the memory card is removed before next boot.  The bootloader proceeds to copy the partitions and the system will not boot unless re-flashed.  pending investigation.
 * 04-12-22:  Updated, custom kernel loads all required items from micro sd card.  System modification no longer needed.
-* 04-05-22:  Update readme to indicate that telnet mod nor DNS spoofing is required for installation, and add per-requisites section.
-* 04-02-22:  Update to automatic install method, remove manual install.  
+* 04-05-22:  Update readme to indicate that telnet mod nor DNS spoofing is required for installation, and add pre-requisites section.
+* 04-02-22:  Update to automatic install method, remove manual install.
 
 ## BYO
 

BIN
SD_ROOT/factory_t31_ZMC6tiIDQN


BIN
SD_ROOT/wz_mini/bin/bash


+ 14 - 1
SD_ROOT/wz_mini/bin/busybox

@@ -1,2 +1,15 @@
 #!/bin/sh
-echo "$*" | /opt/wz_mini/bin/busybox nc localhost 4000
+
+if [ "$1" == "" ]; then
+	echo -e "welcome to: cmd <arg>\n"
+	echo -e "arg can be:"
+	echo -e "jpeg (will dump raw jpeg to stdout)"
+	echo -e "video on or off"
+	echo -e "audio on or off"
+	echo -e "move"
+	echo -e "waitMotion <timeout>"
+	echo -e "irled on or off"
+	echo -e "aplay <file path> <volume 1-100>"
+else
+	echo "$*" | /opt/wz_mini/bin/busybox nc localhost 4000
+fi

+ 2 - 0
SD_ROOT/wz_mini/bin/ip6tables

@@ -0,0 +1,2 @@
+#!/bin/sh
+/opt/wz_mini/bin/xtables-multi ip6tables $@

+ 2 - 0
SD_ROOT/wz_mini/bin/iptables

@@ -0,0 +1,2 @@
+#!/bin/sh
+/opt/wz_mini/bin/xtables-multi iptables $@

BIN
SD_ROOT/wz_mini/bin/iwconfig.sh


+ 11 - 3
SD_ROOT/wz_mini/etc/init.d/v3_init.sh

@@ -3,6 +3,11 @@
 ###DO NOT MODIFY UNLESS YOU KNOW WHAT YOU ARE DOING
 ###
 
+exec 1> /opt/wz_mini/log/v3_init.log 2>&1
+
+echo "welcome to v3_init.sh"
+echo "PID $$"
+
 echo '
  __          ________  __  __ _____ _   _ _____
  \ \        / |___  / |  \/  |_   _| \ | |_   _|
@@ -43,7 +48,7 @@ mount --bind /opt/wz_mini/etc/inittab /etc/inittab
 echo "bind /etc/profile for local/ssh shells"
 mount --bind /opt/wz_mini/etc/profile /etc/profile
 
-echo "mounting tempfs for workspace"
+echo "mounting tmpfs"
 mount -t tmpfs /tmp
 
 echo "mount system to replace factorycheck with dummy, to prevent bind unmount"
@@ -83,6 +88,9 @@ cp /opt/wz_mini/etc/shadow /opt/wz_mini/tmp/.storage/shadow
 mount --bind /opt/wz_mini/tmp/.storage/shadow /etc/shadow
 chmod 400 /etc/shadow
 
+echo "mount kernel modules"
+mount --bind /opt/wz_mini/lib/modules /lib/modules
+
 if [[ -e /opt/wz_mini/swap.gz ]]; then
 	insmod /opt/wz_mini/lib/modules/audio.ko spk_gpio=$GPIO alc_mode=0 mic_gain=0
 	/opt/wz_mini/bin/audioplay_t31 /opt/wz_mini/usr/share/audio/swap.wav 50
@@ -90,7 +98,7 @@ if [[ -e /opt/wz_mini/swap.gz ]]; then
 	echo "swap archive present, extracting"
         gzip -d /opt/wz_mini/swap.gz
         mkswap /opt/wz_mini/swap
-	sync;echo 3 > /proc/sys/vm/drop_caches;free
+	sync;echo 3 > /proc/sys/vm/drop_caches
 else
 	echo "swap archive not present, not extracting"
 fi
@@ -113,7 +121,7 @@ if [[ $(cat /opt/wz_mini/run_mmc.sh | grep "DEBUG_ENABLED\=") == "DEBUG_ENABLED\
 fi
 
 if ! [[ -e /tmp/dbgflag ]]; then
-	{ sleep 30; /media/mmc/wz_mini/run_mmc.sh 2> /media/mmc/wz_mini/log/wz_mini_hacks.log; } &
+		/opt/wz_mini/run_mmc.sh &
 else
 	echo "debug enabled, ignore run_mmc.sh"
 fi

+ 5 - 0
SD_ROOT/wz_mini/etc/init.d/v3_post.sh

@@ -2,6 +2,11 @@
 
 ##THIS FILE IS CALLED BY rcS, EXECUTED BEFORE app_init.sh IS RUN.
 
+exec 1> /opt/wz_mini/log/v3_post.log 2>&1
+
+echo "welcome to v3_post.sh"
+echo "PID $$"
+
 set -x
 
 echo "v3_post.sh exec"

BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/nfs/nfsv4.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/lib/oid_registry.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/ip_tunnel.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arp_tables.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arpt_mangle.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/arptable_filter.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ip_tables.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_ECN.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_REJECT.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_rpfilter.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_filter.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_mangle.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/tunnel4.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6_tables.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6t_REJECT.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_filter.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_mangle.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/sit.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/x_tables.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_HL_.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_hl.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/xt_tcpudp.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/sunrpc/auth_gss/auth_rpcgss.ko


BIN
SD_ROOT/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/wireguard/wireguard.ko


+ 0 - 0
SD_ROOT/wz_mini/log/wz_mini_hacks.log → SD_ROOT/wz_mini/log/.gitignore


+ 0 - 0
SD_ROOT/wz_mini/mnt/.gitignore


+ 0 - 0
SD_ROOT/wz_mini/root/.gitignore


+ 239 - 79
SD_ROOT/wz_mini/run_mmc.sh

@@ -1,5 +1,10 @@
 #!/bin/sh
 
+exec 1> /opt/wz_mini/log/run_mmc.log 2>&1
+
+echo "welcome to run_mmc.sh"
+echo "PID $$"
+
 set -x
 
 HOSTNAME="WCV3"
@@ -16,6 +21,10 @@ ENABLE_IPV6="false"
 
 ENABLE_WIREGUARD="false"
 
+ENABLE_IPTABLES="false"
+
+ENABLE_NFSv4="false"
+
 #####ACCESSORIES#####
 REMOTE_SPOTLIGHT="false"
 REMOTE_SPOTLIGHT_HOST="0.0.0.0"
@@ -28,6 +37,7 @@ RTSP_PASSWORD=""
 RTSP_PORT="8554"
 
 #####GENERAL#####
+ENABLE_SWAP="true"
 ENABLE_USB_STORAGE="false"
 ENABLE_EXT4="false"
 ENABLE_CIFS="false"
@@ -43,100 +53,186 @@ DEBUG_ENABLED="false"
 
 echo  "run_mmc.sh start" > /dev/kmsg
 
-echo "store original mac"
-cat /sys/class/net/wlan0/address | tr '[:lower:]' '[:upper:]' > /opt/wz_mini/tmp/wlan0_mac
+hostname_set() {
+	echo "set hostname"
+	hostname $HOSTNAME
+}
 
-swap_enable() {
-        if [[ -e /media/mmc/wz_mini/swap ]]; then
-                echo "swap exists, enable"
-                swapon /media/mmc/wz_mini/swap
-        else
-                echo "swap missing, system stability with usb potentially comprimised"
-        fi
+first_run_check() {
+	if [[ -e /opt/wz_mini/tmp/.run_mmc_firstrun ]]; then
+	echo "run_mmc.sh already run once, exit."
+	exit 0
+	fi
 }
 
-if [[ "$ENABLE_USB_RNDIS" == "true" ]]; then
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/usbnet.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/cdc_ether.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/rndis_host.ko
+wait_sdroot() {
+##Stall execution if the micro-sd card isn't mounted yet, iCamera controls this internally.
+    while true
+    do
+	if [[ -d /media/mmc/wz_mini ]]; then
+	echo "sd card ready"
+	break
+	fi
+        echo "sdcard not ready yet..."
+        sleep 5
+    done
 
-        swap_enable
+}
 
-        ifconfig usb0 down
-        ifconfig wlan0 down
+wait_wlan() {
+##Check if the driver has been loaded for the onboard wlan0, store the MAC.
+    while true
+    do
+        if  ifconfig wlan0 | grep "inet addr";
+        then
+	echo "store original mac"
+	cat /sys/class/net/wlan0/address | tr '[:lower:]' '[:upper:]' > /opt/wz_mini/tmp/wlan0_mac
+        break
+	elif [[ "$ENABLE_USB_ETH" == "true" || "$ENABLE_USB_DIRECT" == "true" ]]; then
+	break
+        fi
+        echo " wlan0 not ready yet..."
+        sleep 5
+    done
+}
 
+rename_interface() {
+##Fool iCamera by renaming the hardline interface to wlan0
+	echo "renaming interfaces"
+	ifconfig $1 down
+	ifconfig wlan0 down
         /media/mmc/wz_mini/bin/busybox ip link set wlan0 name wlanold
         /media/mmc/wz_mini/bin/busybox ip addr flush dev wlanold
-        /media/mmc/wz_mini/bin/busybox ip link set usb0 name wlan0
+        /media/mmc/wz_mini/bin/busybox ip link set $1 name wlan0
+	eth_wlan_up
+}
 
+eth_wlan_up() {
+##Run DHCP client, and bind mount our fake wpa_cli.sh to fool iCamera
+	if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
+		/media/mmc/wz_mini/bin/busybox ip link set wlan0 address $USB_DIRECT_MAC_ADDR
+	fi
         ifconfig wlan0 up
-        pkill udhcpc
+	pkill udhcpc
         udhcpc -i wlan0 -x hostname:$HOSTNAME -p /var/run/udhcpc.pid -b
-#        sleep 5
         mount -o bind /media/mmc/wz_mini/bin/wpa_cli.sh /bin/wpa_cli
+	break
+}
 
-else
-	echo "rndis disabled"
-fi
+wpa_check() {
+#Check if wpa_supplicant has been created by iCamera
+	if [[ -e /tmp/wpa_supplicant.conf ]]; then
+		echo "wpa_supplicant.conf ready"
+		wlanold_check $1
+	else
+		echo "wpa_supplicant.conf not ready, wait some time for creation."
+		COUNT=0
+		ATTEMPTS=15
+		until [[ -e /tmp/wpa_supplicant.conf ]] || [[ $COUNT -eq $ATTEMPTS ]]; do
+		echo -e "$(( COUNT++ ))... \c"
+		sleep 5
+		done
+		[[ $COUNT -eq $ATTEMPTS ]] && echo "time exceeded waiting for iCamera, continue potentially broken condition without network." && wlanold_check $1
+	fi
+}
 
-if [[ "$ENABLE_WIREGUARD" == "true" ]]; then
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/tunnel4.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/ip_tunnel.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/wireguard/wireguard.ko
-else
-	echo "wireguard disabled"
-fi
+wlanold_check() {
+#Have we renamed interfaces yet?
+	if [[ -d /sys/class/net/wlanold ]]; then
+		echo "wlanold exist"
+		eth_wlan_up
+	else
+		echo "wlanold doesn't exist"
+		rename_interface $1
+	fi
+}
 
-if [[ "$ENABLE_CIFS" == "true" ]]; then
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/cifs/cifs.ko
-else
-	echo "cifs disabled"
+netloop() {
+##While loop for check
+        while true
+        do
+        wpa_check $1
+        echo "wlan0 not ready yet..."
+        sleep 5
+        done
+}
+
+swap_enable() {
+        if [[ -e /media/mmc/wz_mini/swap ]]; then
+                echo "Swap exists, enable"
+                swapon /media/mmc/wz_mini/swap
+        else
+                echo "Swap file missing!"
+        fi
+}
+
+first_run_check
+hostname_set
+wait_sdroot
+wait_wlan
+
+
+if [[ "$ENABLE_SWAP" == "true" ]]; then
+        if cat /proc/swaps | grep "mini" ; then
+        echo "Swap is already enabled"
+        else
+        echo "Swap not enabled, enabling"
+	swap_enable
+        fi
 fi
 
-if [[ "$ENABLE_USB_STORAGE" == "true" ]]; then
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/scsi/scsi_mod.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/scsi/sd_mod.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/storage/usb-storage.ko
+if [[ "$ENABLE_IPV6" == "true" ]]; then
+	echo "ipv6 enabled"
 else
-	echo "usb_storage disabled"
+	echo "ipv6 disabled"
+	sysctl -w net.ipv6.conf.all.disable_ipv6=1
 fi
 
-if [[ "$ENABLE_EXT4" == "true" ]]; then
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/jbd2/jbd2.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/mbcache.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/ext4/ext4.ko
+if [[ "$ENABLE_NFSv4" == "true" ]]; then
+        insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/lib/oid_registry.ko
+        insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/dns_resolver/dns_resolver.ko
+        insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/nfs/nfsv4.ko
+        insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/sunrpc/auth_gss/auth_rpcgss.ko
+        echo nfsv4 enabled
 else
-	echo "ext4 disabled"
+        echo nfsv4 disabled
 fi
 
-if [[ "$ENABLE_IPV6" == "true" ]]; then
-echo "ipv6 enabled"
-else
-echo "ipv6 disabled"
-sysctl -w net.ipv6.conf.all.disable_ipv6=1
+if [[ "$ENABLE_IPTABLES" == "true" ]]; then
+
+	insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/x_tables.ko
+	insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ip_tables.ko
+	insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_REJECT.ko
+	insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_filter.ko
+	insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_mangle.ko
+	echo "iptables ipv4 enabled"
+
+	if [[ "$ENABLE_IPV6" == "true" ]]; then
+		insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6_tables.ko
+		insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6t_REJECT.ko
+		insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_filter.ko
+		insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv6/netfilter/ip6table_mangle.ko
+		echo "iptables ipv6 enabled"
+	fi
+	else
+		echo "iptables disabled"
 fi
 
 if [[ "$ENABLE_USB_ETH" == "true" ]]; then
 
-	swap_enable
-
 	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/usbnet.ko
-	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/asix.ko
+        insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/asix.ko
 
-        ifconfig eth0 down
-        ifconfig wlan0 down
+	if [[ "$ENABLE_SWAP" == "true" ]]; then
+	echo "swap already enabled"
+	else
+	swap_enable
+	fi
 
-        /media/mmc/wz_mini/bin/busybox ip link set wlan0 name wlanold
-        /media/mmc/wz_mini/bin/busybox ip addr flush dev wlanold
-        /media/mmc/wz_mini/bin/busybox ip link set eth0 name wlan0
+	netloop eth0
 
-        ifconfig wlan0 up
-	pkill udhcpc
-        udhcpc -i wlan0 -x hostname:$HOSTNAME -p /var/run/udhcpc.pid -b
-#        sleep 5
-        mount -o bind /media/mmc/wz_mini/bin/wpa_cli.sh /bin/wpa_cli
 	else
-	        echo "usb ethernet disabled"
+	echo "usb ethernet disabled"
 fi
 
 if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
@@ -154,20 +250,76 @@ if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
 
 	sleep 1
 
-        ifconfig usb0 down
-        ifconfig wlan0 down
-        /media/mmc/wz_mini/bin/busybox ip link set wlan0 name wlanold
-        /media/mmc/wz_mini/bin/busybox ip addr flush dev wlanold
-        /media/mmc/wz_mini/bin/busybox ip link set usb0 name wlan0
-	/media/mmc/wz_mini/bin/busybox ip link set wlan0 address $USB_DIRECT_MAC_ADDR
+	if [[ "$ENABLE_SWAP" == "true" ]]; then
+	echo "swap already enabled"
+	else
+	swap_enable
+	fi
 
-        ifconfig wlan0 up
-	pkill udhcpc
-        udhcpc -i wlan0 -x hostname:$HOSTNAME -p /var/run/udhcpc.pid -b
-        sleep 5
-        mount -o bind /media/mmc/wz_mini/bin/wpa_cli.sh /bin/wpa_cli
+	#loop begin
+	while true
+	do
+	wpa_check usb0
+	echo "wlan0 not ready yet..."
+        sleep 1
+	done
+	else
+	echo "usb direct disabled"
+fi
+
+if [[ "$ENABLE_USB_RNDIS" == "true" ]]; then
+
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/usbnet.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/cdc_ether.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/net/usb/rndis_host.ko
+
+	sleep 1
+
+	if [[ "$ENABLE_SWAP" == "true" ]]; then
+	echo "swap already enabled"
 	else
-		echo "usb direct disabled"
+	swap_enable
+	fi
+
+	#loop begin
+	while true
+	do
+	wpa_check usb0
+	echo "wlan0 not ready yet..."
+        sleep 1
+	done
+	else
+	echo "usb direct disabled"
+fi
+
+if [[ "$ENABLE_WIREGUARD" == "true" ]]; then
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/tunnel4.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/ip_tunnel.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/wireguard/wireguard.ko
+else
+	echo "wireguard disabled"
+fi
+
+if [[ "$ENABLE_CIFS" == "true" ]]; then
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/cifs/cifs.ko
+else
+	echo "cifs disabled"
+fi
+
+if [[ "$ENABLE_USB_STORAGE" == "true" ]]; then
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/scsi/scsi_mod.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/scsi/sd_mod.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/storage/usb-storage.ko
+else
+	echo "usb_storage disabled"
+fi
+
+if [[ "$ENABLE_EXT4" == "true" ]]; then
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/jbd2/jbd2.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/mbcache.ko
+	insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/fs/ext4/ext4.ko
+else
+	echo "ext4 disabled"
 fi
 
 if [[ "$DISABLE_FW_UPGRADE" == "true" ]]; then
@@ -181,17 +333,26 @@ else
 fi
 
 if [[ "$REMOTE_SPOTLIGHT" == "true" ]]; then
-	{ sleep 10; /media/mmc/wz_mini/bin/socat pty,link=/dev/ttyUSB0,raw tcp:$REMOTE_SPOTLIGHT_HOST:9000; } &
-
+	/media/mmc/wz_mini/bin/socat pty,link=/dev/ttyUSB0,raw tcp:$REMOTE_SPOTLIGHT_HOST:9000 &
+	echo "remote accessory enabled"
+else
+	echo "remote accessory disabled"
 fi
 
 if [[ "$RTSP_ENABLED" == "true" ]]; then
+
+	if [[ "$ENABLE_SWAP" == "true" ]]; then
+	echo "swap already enabled"
+	else
 	swap_enable
+	fi
+
         mkdir /tmp/alsa
         cp /media/mmc/wz_mini/etc/alsa.conf /tmp/alsa
 	/opt/wz_mini/bin/cmd video on
 	/opt/wz_mini/bin/cmd audio on
 
+
 	if [[ "$RTSP_PASSWORD" = "" ]]; then
 	RTSP_PASSWORD=$(cat /opt/wz_mini/tmp/wlan0_mac)
 	fi
@@ -206,9 +367,8 @@ if [[ "$RTSP_ENABLED" == "true" ]]; then
         echo "rtsp disabled"
 fi
 
-echo "set hostname"
-hostname $HOSTNAME
-
+touch /opt/wz_mini/tmp/.run_mmc_firstrun
+sync;echo 3 > /proc/sys/vm/drop_caches
 sleep 3
 
 #################################################