#!/bin/sh . /etc/functions.sh LOGFILE=/tmp/.startup_log equal() { case "$1" in "$2") return 0 ;; *) return 255 ;; esac } start_ppp() { config_load fon config_get username wan username config_get password wan password config_get keepalive wan keepalive config_get mtu wan mtu config_get demand wan demand mtu="${mtu:-1480}" /usr/sbin/pppd "$@" \ lcp-echo-interval 5 \ lcp-echo-failure ${keepalive:-5} \ ${demand:+precompiled-active-filter /etc/ppp/filter idle demand }${demand:-persist} \ usepeerdns \ defaultroute \ replacedefaultroute \ user "$username" \ password "$password" \ linkname eth0 \ mtu $mtu mru $mtu \ ipparam eth0 } setup_management() { local ifc="$1" echo "`uptime | awk -F" " '{print $1}'`: setting up management @ $ifc:1" >>$LOGFILE ifconfig "$ifc:1" 127.1.2.3 up # use an invalid address as source for arping local ip=169.254.255.1 while :; do if arping -c 1 -I "$ifc" -s 127.1.6.8 "$ip" >/dev/null 2>/dev/null; then # already taken ip="169.254.255.$((${ip##*\.} + 1))" else break; fi # this should never happen: [ "${ip##*\.}" = "254" ] && { ifconfig "$ifc:1" 0.0.0.0 down return 1 } done ifconfig "$ifc:1" "$ip" netmask 255.255.0.0 && echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $ifc:1, ip=$ip, netmask=255.255.0.0" >>$LOGFILE ifconfig "$ifc" up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $ifc" >>$LOGFILE } setup_hostname() { local ifc="$1" echo "`uptime | awk -F" " '{print $1}'`: $0 setup_hostname: $ifc" >>$LOGFILE ipwan=`ifconfig $ifc 2>/dev/null | grep inet | awk -F'[: ]+' '{print $4}'` echo -e "127.0.0.1\tlocalhost" >/etc/hosts echo -e "$ipwan\t`cat /etc/hostname`" >>/etc/hosts } set_mac() { local ifc="$1" local dev="$2" config_get mac $ifc mac # Maybe we should move this code to a seperate file, but I think it belongs to this function. if [ ! -f /tmp/"$dev"_mac ] then echo $(ifconfig eth0 | grep HWaddr|sed -e "s/^.*HWaddr //" | sed -e "s/ //g")>/tmp/"$dev"_mac fi if [ -z $mac ] then mac=`cat /tmp/eth0_mac` fi ifconfig "$dev" down ifconfig "$dev" hw ether $mac && echo "`uptime | awk -F" " '{print $1}'`: MAC of $ifc/$dev set to $mac" >>$LOGFILE ifconfig "$dev" up } create_bridge() { local ifc="$1" echo "`uptime | awk -F" " '{print $1}'`: creating bridge $ifc" >>$LOGFILE /usr/sbin/brctl addbr $ifc /usr/sbin/brctl stp $ifc off /usr/sbin/brctl setfd $ifc 0 /usr/sbin/brctl addif $ifc ath$((`echo "$ifc" | awk -F"br" '{print $2}'`)) /usr/sbin/brctl addif $ifc eth0 echo "`uptime | awk -F" " '{print $1}'`: creating bridge $ifc done" >>$LOGFILE } setup_channel() { if [ "$mode" != "wlan" ]; then echo "`uptime | awk -F" " '{print $1}'`: mode != \"wlan\"" >>$LOGFILE if [ -n "$channel" -a -z "${channel%%[0-9][0-9]}" ]; then iwconfig ath1 channel "$channel" else iwconfig ath1 channel 0 fi else iwconfig ath1 channel 0 fi echo "`uptime | awk -F" " '{print $1}'`: set frequency to `iwlist ath1 channel | grep Freq | awk -F":" '{print $2}'`" >>$LOGFILE } write_resolv_conf() { rm -rf /etc/resolv.conf touch /etc/resolv.conf if [ -n "$wandns" ]; then echo "`uptime | awk -F" " '{print $1}'`: $0 writing $wandns to /etc/resolv.conf" >>$LOGFILE for server in $wandns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "`uptime | awk -F" " '{print $1}'`: $0 writing 4.2.2.2 and 4.2.2.5 to /etc/resolv.conf" >>$LOGFILE echo "nameserver 4.2.2.2" > /etc/resolv.conf echo "nameserver 4.2.2.5" >> /etc/resolv.conf fi } #################################################################################################### echo "`uptime | awk -F" " '{print $1}'`: entering ifup" >>$LOGFILE config_load fon config_get bgmode advanced bgmode config_get channel advanced channel config_get publicessid public essid config_get mode wan mode config_get privatessid private essid config_get privateenc private encryption config_get privatecrypto private wpa_crypto config_get privatepasswd private password #config_get auth private shared_auth config_get privatehidden private hidden config_get privatewepkey private wepkey config_get txp private txpower config_get lanipaddr lan ipaddr config_get lannetmask lan netmask config_get landhcp lan dhcp config_get wandhcp wan dhcp config_get wanbonly wan bonly config_get wanipaddr wan ipaddr config_get wannetmask wan netmask config_get wangateway wan gateway config_get wanrssid wan rssid config_get wanbssid wan bssid config_get wankey wan key config_get wanenc wan enc config_get wanauthmode wan authmode config_get wankeyidx wan key_idx config_get wandns wan dns config_get wanpptp_server wan pptp_server case "$1" in ################################################## ath2) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2" >>$LOGFILE if [ "$mode" != "wlan" ]; then echo "`uptime | awk -F" " '{print $1}'`: mode != "wlan", ath2 keeps being down to secure system stability" >>$LOGFILE else if [ "$wanbonly" = "1" ]; then iwpriv ath2 mode 11b echo "`uptime | awk -F" " '{print $1}'`: limiting wlan uplink to 11b" >>$LOGFILE else iwpriv ath2 mode 0 fi iwconfig ath2 txpower $txp if [ -z $wanrssid ] && [ -n $wanbssid ]; then # We got a BSSID only, guess Remote SSID echo "`uptime | awk -F" " '{print $1}'`: $0 getting Remote SSID from BSSID" >>$LOGFILE ifconfig ath2 0.0.0.0 up wanrssid=`iwlist ath2 scanning 2>/dev/null | grep -iA1 $wanbssid | grep ESSID | awk -F"\"" '{print $2}'` ifconfig ath2 down if [ ! -z $wanrssid ]; then echo "`uptime | awk -F" " '{print $1}'`: $0 getting Remote SSID \"$wanrssid\" DONE" >>$LOGFILE else echo "`uptime | awk -F" " '{print $1}'`: $0 getting Remote SSID from BSSID... FAILURE: exiting!" >>$LOGFILE exit 1; fi elif [ -n $wanrssid ] && [ -n $wanbssid ]; then # check if given BSSID corresponds with given SSID ifconfig ath2 0.0.0.0 up tmprssid=`iwlist ath2 scanning 2>/dev/null | grep -iA1 $wanbssid | grep ESSID | awk -F"\"" '{print $2}'` if ! equal $tmprssid $wanrssid; then if [ $tmprssid ]; then echo "`uptime | awk -F" " '{print $1}'`: $0 Remote SSID \"$wanrssid\" does not correspond with SSID associated to \"$wanbssid\", using \"$tmprssid\" instead" >>$LOGFILE wanrssid=$tmprssid else if ! iwlist ath2 scanning | grep -q $wanrssid; then # FIXME if $wanrssid is part of the output, it will fall through this test echo "`uptime | awk -F" " '{print $1}'`: $0 could not find given Remote SSID nor BSSID in range, exiting!" >>$LOGFILE ifconfig ath2 down exit 1; fi wanbssid="" echo "`uptime | awk -F" " '{print $1}'`: $0 given BSSID not found, ignoring entry" >>$LOGFILE fi else echo "`uptime | awk -F" " '{print $1}'`: $0 Remote SSID corresponds with BSSID, everything's fine" >>$LOGFILE fi ifconfig ath2 down fi # this shall(!) help in case of given passphrase is mistyped killall ath2watchdog 1>/dev/null 2>&1 nohup /bin/ath2watchdog & # wait 120sec, test conenction and reboot failsave (dhcp-mode) if no connection detected iwconfig ath2 channel 0 iwconfig ath2 essid "$wanrssid" echo 'ap_scan=1' > /tmp/wpa_supplicant.conf echo 'network={' >> /tmp/wpa_supplicant.conf echo 'ssid="'$wanrssid'"' >> /tmp/wpa_supplicant.conf [ -n "$wanbssid" ] && echo 'bssid='$wanbssid >> /tmp/wpa_supplicant.conf echo 'scan_ssid=1' >> /tmp/wpa_supplicant.conf echo 'priority=2' >> /tmp/wpa_supplicant.conf case "$wanenc" in wpa|WPA) echo 'psk="'$wankey'"' >> /tmp/wpa_supplicant.conf ;; wep|WEP) echo 'wep_key'$wankeyidx'='$wankey >> /tmp/wpa_supplicant.conf echo 'wep_tx_keyidx='$wankeyidx >> /tmp/wpa_supplicant.conf # authmode # 1: Open # 2: shared key # 3: 802.1x # 4: auto select/accept (seems to work better then 2) case "$wanauthmode" in 1) echo 'key_mgmt=NONE' >> /tmp/wpa_supplicant.conf echo 'auth_alg=OPEN' >> /tmp/wpa_supplicant.conf ;; 2) echo 'key_mgmt=NONE' >> /tmp/wpa_supplicant.conf echo 'auth_alg=SHARED' >> /tmp/wpa_supplicant.conf ;; 3) echo 'key_mgmt=IEEE8021X' >> /tmp/wpa_supplicant.conf echo 'auth_alg=OPEN SHARED' >> /tmp/wpa_supplicant.conf ;; *) echo 'key_mgmt=IEEE8021X NONE' >> /tmp/wpa_supplicant.conf echo 'auth_alg=OPEN SHARED' >> /tmp/wpa_supplicant.conf ;; esac ;; *) echo 'key_mgmt=NONE' >> /tmp/wpa_supplicant.conf ;; esac echo '}' >> /tmp/wpa_supplicant.conf /usr/sbin/wpa_supplicant -iath2 -c/tmp/wpa_supplicant.conf -B -d echo "`uptime | awk -F" " '{print $1}'`: $0 wandhcp=$wandhcp" >>$LOGFILE case "$wandhcp" in 1) ifconfig ath2 up && \ echo "`uptime | awk -F" " '{print $1}'`: $0 using udhcpc..." >>$LOGFILE udhcpc -H `cat /etc/hostname` -i ath2 -R & ;; *) ifconfig ath2 $wanipaddr netmask $wannetmask up && \ echo "`uptime | awk -F" " '{print $1}'`: $0 using ip: $wanipaddr, netmask: $wannetmask" >>$LOGFILE route add default gw $wangateway && \ echo "`uptime | awk -F" " '{print $1}'`: $0 adding proper route to gateway done" >>$LOGFILE env -i ACTION=ifup INTERFACE=wan /sbin/hotplug iface write_resolv_conf; ;; esac setup_hostname ath2 fi echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 done" >>$LOGFILE ;; ################################################## ath1) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2" >>$LOGFILE bridge_created=0 if [ "$2" != "" ]; then echo "`uptime | awk -F" " '{print $1}'`: bridge to create=$2" >>$LOGFILE create_bridge $2 bridge_created=1 fi [ -n "$privatessid" ] && { #sh_auth=1 case "$privateenc" in open|OPEN) #[ "$auth" = 1 ] && sh_auth=2 ;; wep|WEP) #[ "$auth" = 1 ] && sh_auth=2 ;; WPA|WPA1|wpa|wpa1) wpamode=1; crypt=TKIP;; WPA2|wpa2) wpamode=2; crypt="CCMP TKIP";; mixed|MIXED) wpamode=3; crypt="CCMP TKIP";; esac case "$privatecrypto" in aes|AES|ccmp|CCMP) crypt=CCMP;; tkip|TKIP) crypt=TKIP;; tkip+aes|TKIP+AES|aes+tkip|AES+TKIP) crypt="TKIP CCMP";; esac case "$bgmode" in b|B) bgmode=11b; pureg=0;; g|G) bgmode=11g; pureg=1;; # pureg=1 --> pure 11g (no 11b stations) *) bgmode=0; pureg=0;; # auto esac iwpriv ath1 mode $bgmode iwpriv ath1 pureg $pureg iwpriv ath1 ap_bridge 1 # 0 --> disable access between clients [ -z "$privatehidden" ] && privatehidden=0 iwpriv ath1 hide_ssid $privatehidden iwconfig ath1 essid "$privatessid" case "$privateenc" in wep|WEP) [ -z "$privatewepkey" ] || { for k in 1 2 3 4; do config_get key private key$k [ -z "$key" ] || iwconfig ath1 enc "[$k]" "$key" done iwconfig ath1 enc "[$privatewepkey]" } ;; open|OPEN) # http://madwifi.org/users-guide/node11.html #iwconfig ath1 key off ;; esac setup_channel; lannetmask=${lannetmask:-255.255.255.0} if [ "$bridge_created" = "0" ]; then ifconfig ath1 $lanipaddr netmask $lannetmask && echo "`uptime | awk -F" " '{print $1}'`: $0 set up $1, ip=$lanipaddr, netmask=$lannetmask" >>$LOGFILE fi ifconfig ath1 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>$LOGFILE case "$landhcp" in 0|off|disabled)dhcp_enabled=0 ;; *)dhcp_enabled=1 ;; esac [ -z "$txp" ] && txp="18" [ -f /etc/debug ] && iwconfig ath1 txpower $txp ############### ACHTUNG! Beeinflusst wifi0, damit alle VAPs. ############### [ -z "$crypt" -o -z "$wpamode" ] || { echo "`uptime | awk -F" " '{print $1}'`: $0 setting up hostapd, wpamode=$wpamode, crypt=$crypt" >>$LOGFILE killall hostapd 2>/dev/null >/dev/null && sleep 1 cat > /tmp/hostapd.conf <> /tmp/hostapd.conf hostapd -B /tmp/hostapd.conf || { # hostapd setup failed. destroy the interface rather than leaving it open echo "`uptime | awk -F" " '{print $1}'`: $0 setting up hostapd failed, bringing down $1" >>$LOGFILE dhcp_enabled=0 /sbin/ifdown ath1 } } [ "$bridge_created" = "0" ] && { [ "$dhcp_enabled" = "1" ] && { #[ "$mode" = "wlan" ] && dhcpopts="${dhcpopts:+$dhcpopts }--address=/#/213.134.45.154 -T 1" touch /tmp/dhcp.leases # calculate the dhcp pool settings eval $(ipcalc ${lanipaddr:-192.168.10.1} ${lannetmask:-255.255.255.0} ${start:-20} ${num:-180}) dhcpopts="${dhcpopts:+$dhcpopts } --dhcp-range=$START,$END,$NETMASK,${time:-12h}" echo "`uptime | awk -F" " '{print $1}'`: $0 setting up dnsmasq, dhcpopts=\"$dhcpopts\"" >>$LOGFILE dnsmasq -C /etc/dnsmasq.conf -i ath1 -z -z -I lo,eth0 $dhcpopts & } } } echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 done" >>$LOGFILE ;; ################################################## ath0) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2" >>$LOGFILE case "$bgmode" in b|B) bgmode=11b; pureg=0;; g|G) bgmode=11g; pureg=1;; # pureg=1 --> pure 11g (no 11b stations) *) bgmode=0; pureg=0;; # auto esac iwpriv ath0 mode $bgmode iwpriv ath0 pureg $pureg iwpriv ath0 ap_bridge 0 # 0 --> disable access between clients if [ "$mode" != "wlan" ]; then [ -n "$channel" -a -z "${channel%%[0-9][0-9]}" ] && { iwconfig ath0 channel "$channel" } || { iwconfig ath0 channel 0 # auto select channel } else iwconfig ath0 channel 0 fi echo "`uptime | awk -F" " '{print $1}'`: set ESSID to \"FON_${publicessid:-AP}\"" >>$LOGFILE iwconfig ath0 essid "FON_${publicessid:-AP}" echo "`uptime | awk -F" " '{print $1}'`: bringing up $1" >>$LOGFILE ifconfig ath0 up if [ ! -f /tmp/.tun0_dnsmasq_supervisor ]; then /bin/tun0_dnsmasq_supervisor & touch /tmp/.tun0_dnsmasq_supervisor fi echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 done" >>$LOGFILE ;; ################################################## eth0) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 $3" >>$LOGFILE ifconfig eth0 up case "$2" in static) #set_mac wan eth0 wannetmask=${wannetmask:-255.255.255.0} [ "$3" != "eth0" ] && ifconfig eth0 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>$LOGFILE ifconfig $3 $wanipaddr netmask $wannetmask up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3, ip=$wanipaddr, netmask=$wannetmask" >>$LOGFILE [ -n "$wangateway" ] && { while route del default >&- 2>&- ; do :; done route add default gw "$wangateway" } env -i ACTION=ifup INTERFACE=$3 /sbin/hotplug iface write_resolv_conf; setup_channel; ;; dhcp) #set_mac wan eth0 [ "$3" != "eth0" ] && ifconfig eth0 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up eth0" >>$LOGFILE ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>$LOGFILE setup_hostname eth0 echo "`uptime | awk -F" " '{print $1}'`: $0 starting dhcp client @ $3" >>$LOGFILE udhcpc -H `cat /etc/hostname` -i $3 ${wanipaddr:+ -r $wanipaddr} -R -b & setup_channel; ;; pppoe) set_mac wan eth0 ifconfig $1 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>$LOGFILE [ "$3" != "eth0" ] && ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>$LOGFILE for module in slhc ppp_generic pppox pppoe; do /sbin/insmod $module 2>&- >&- done start_ppp \ plugin rp-pppoe.so \ nic-$1 setup_hostname eth0 ( echo "`uptime | awk -F" " '{print $1}'`: $0 PPPoE nameserver check: fork into background, checking nameserver..." >>$LOGFILE while true; do [ -f /etc/ppp/resolv.conf ] && { grep -q "nameserver" /etc/ppp/resolv.conf && cp /etc/ppp/resolv.conf /etc/resolv.conf &&\ echo "`uptime | awk -F" " '{print $1}'`: $0 PPPoE nameserver check: nameserver successfully set..." >>$LOGFILE && break } sleep 2 done ) & ;; pptp) ### canīt test that, itīs only copied and pasted from the old architecture, fix it if nessesary set_mac wan eth0 ifconfig $1 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>$LOGFILE [ "$3" != "eth0" ] && ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>$LOGFILE udhcpc -H `cat /etc/hostname` -i eth0 ${ipaddr:+ -r $ipaddr} -n -q for module in slhc ppp_generic ppp_async ip_gre; do /sbin/insmod $module 2>&- >&- done start_ppp \ pty "/usr/sbin/pptp $wanpptp_server --loglevel 0 --nolaunchpppd" \ file /etc/ppp/options.pptp \ $PPPOPTS ;; mgmt) if [ "$3" != "" ]; then setup_management "$3" else setup_management "$1" fi ;; wlan) config_load router config_get eth0_on eth0 eth0_on config_get eth0_ip eth0 eth0_ip config_get eth0_mask eth0 eth0_mask config_get eth0_dhcp eth0 eth0_dhcp case "$eth0_on" in 1) # switched on echo "`uptime | awk -F" " '{print $1}'`: $0 eth0_on=$eth0_on" >>$LOGFILE case "$eth0_dhcp" in 0|off|disabled)dhcp_enabled=0;; *)dhcp_enabled=1;; esac [ "$3" != "eth0" ] && ifconfig eth0 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>$LOGFILE ifconfig $3 $eth0_ip netmask eth0_mask up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3, ip=$eth0_ip, netmask=$eth0_netmask" >>$LOGFILE if [ "$dhcp_enabled" = "1" ]; then dnsmasq -C /etc/dnsmasq.repeater -i $3 -x /var/run/dnsmasq.repeater.pid -z -I lo,ath1 && \ echo "`uptime | awk -F" " '{print $1}'`: $0 setting up dnsmasq, dhcpopts=\"-C /etc/dnsmasq.repeater -i $3 -x /var/run/dnsmasq.repeater.pid -z -I lo,ath1\"" >>$LOGFILE fi ;; 2) # bridge to ath2 ;; 3) # FON, bridge to ath0 echo "`uptime | awk -F" " '{print $1}'`: $0 eth0_on=$eth0_on, bridging with ath0" >>$LOGFILE kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 create_bridge br0 ifconfig eth0 up ;; *) # switched off echo "`uptime | awk -F" " '{print $1}'`: $0 eth0_on=$eth0_on, nothing to be done" >>$LOGFILE ;; esac ;; esac echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 $3 done" >>$LOGFILE ;; ################################################## esac