#!/bin/sh . /etc/functions.sh 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" >>/tmp/.startup_log 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" >>/tmp/.startup_log ifconfig "$ifc" up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $ifc" >>/tmp/.startup_log } setup_hostname() { local ifc="$1" echo "`uptime | awk -F" " '{print $1}'`: $0 setup_hostname: $ifc" >>/tmp/.startup_log 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 ifconfig "$dev" up } create_bridge() { local ifc="$1" echo "`uptime | awk -F" " '{print $1}'`: creating bridge $ifc" >>/tmp/.startup_log /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" >>/tmp/.startup_log } setup_channel() { if [ "$mode" != "wlan" ]; then echo "`uptime | awk -F" " '{print $1}'`: mode != \"wlan\"" >>/tmp/.startup_log 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}'`" >>/tmp/.startup_log } 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" >>/tmp/.startup_log for server in $wandns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "`uptime | awk -F" " '{print $1}'`: $0 writing 208.67.222.222 and 208.67.220.220 to /etc/resolv.conf" >>/tmp/.startup_log echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi } #################################################################################################### echo "`uptime | awk -F" " '{print $1}'`: entering ifup" >>/tmp/.startup_log 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 wankey wan key config_get wanenc wan enc config_get wanauthmode wan authmode 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" >>/tmp/.startup_log if [ "$mode" != "wlan" ]; then echo "`uptime | awk -F" " '{print $1}'`: mode != "wlan", ath2 keeps being down to secure system stability" >>/tmp/.startup_log else if [ "$wanbonly" = "1" ]; then iwpriv ath2 mode 11b echo "`uptime | awk -F" " '{print $1}'`: limiting wlan uplink to 11b" >>/tmp/.startup_log else iwpriv ath2 mode 0 iwconfig ath2 txpower 18 fi iwconfig ath2 channel 0 case "$wanenc" in wpa|WPA) iwpriv ath2 wpa 3 iwconfig ath2 essid "$wanrssid" wpa_supplicant -iath2 -c/etc/wpa_supplicant.conf -B -d ;; wep|WEP) iwconfig ath2 key "$wankey" # authmode # 1: Open # 2: shared key # 3: 02.1x # 4: auto select/accept (seems to work better then 2) [ $wanauthmode != 1 ] && [ $wanauthmode != 2 ] && [ $wanauthmode != 3 ] && wanauthmode="4" iwpriv ath2 authmode $wanauthmode iwconfig ath2 essid "$wanrssid" ;; *) iwconfig ath2 key open off iwconfig ath2 essid "$wanrssid" ;; esac echo "`uptime | awk -F" " '{print $1}'`: $0 wandhcp=$wandhcp" >>/tmp/.startup_log case "$wandhcp" in 1) ifconfig ath2 up udhcpc -H `cat /etc/hostname` -i ath2 -R & ;; *) ifconfig ath2 $wanipaddr netmask $wannetmask up route add default gw $wangateway 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" >>/tmp/.startup_log ;; ################################################## ath1) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2" >>/tmp/.startup_log bridge_created=0 if [ "$2" != "" ]; then echo "`uptime | awk -F" " '{print $1}'`: bridge to create=$2" >>/tmp/.startup_log 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" >>/tmp/.startup_log fi ifconfig ath1 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>/tmp/.startup_log 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" >>/tmp/.startup_log 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" >>/tmp/.startup_log 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\"" >>/tmp/.startup_log dnsmasq -C /etc/dnsmasq.conf -i ath1 -z -z -I lo,eth0 $dhcpopts & } } } echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 done" >>/tmp/.startup_log ;; ################################################## ath0) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2" >>/tmp/.startup_log 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}\"" >>/tmp/.startup_log iwconfig ath0 essid "FON_${publicessid:-AP}" echo "`uptime | awk -F" " '{print $1}'`: bringing up $1" >>/tmp/.startup_log ifconfig ath0 up if [ ! -f /tmp/.tun0_dnsmasq_supervisor ]; then ( # start supervising existense of FON hotspot and starting/killing dnsmasq echo "`uptime | awk -F" " '{print $1}'`: $0 tun0 dnsmasq supervisor: fork into background, checking hotspot status..." >>/tmp/.startup_log while :; do ifconfig tun0 >/dev/null 2>/dev/null if [ "$?" = "0" ]; then [ -z `cat /var/run/dnsmasq_tun0.pid` ] && { # if it isnīt running already dnsmasq -i tun0 -z -I lo,ath1 --no-dhcp-interface=tun0 -x /var/run/dnsmasq_tun0.pid && \ echo "`uptime | awk -F" " '{print $1}'`: tun0 dnsmasq supervisor: dnsmasq started" >>/tmp/.startup_log } else [ -n `cat /var/run/dnsmasq_tun0.pid` ] && { # if it is really running kill `cat /var/run/dnsmasq_tun0.pid` && \ echo "`uptime | awk -F" " '{print $1}'`: tun0 dnsmasq supervisor: dnsmasq stopped" >>/tmp/.startup_log } fi sleep 5 done ) & touch /tmp/.tun0_dnsmasq_supervisor fi echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 done" >>/tmp/.startup_log ;; ################################################## eth0) echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 $3" >>/tmp/.startup_log ifconfig eth0 up case "$2" in static) wannetmask=${wannetmask:-255.255.255.0} [ "$3" != "eth0" ] && ifconfig eth0 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $1" >>/tmp/.startup_log ifconfig $3 $wanipaddr netmask $wannetmask up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3, ip=$wanipaddr, netmask=$wannetmask" >>/tmp/.startup_log [ -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) [ "$3" != "eth0" ] && ifconfig eth0 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up eth0" >>/tmp/.startup_log ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>/tmp/.startup_log setup_hostname eth0 echo "`uptime | awk -F" " '{print $1}'`: $0 starting dhcp client @ $3" >>/tmp/.startup_log 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" >>/tmp/.startup_log [ "$3" != "eth0" ] && ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>/tmp/.startup_log 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..." >>/tmp/.startup_log 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..." >>/tmp/.startup_log && 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" >>/tmp/.startup_log [ "$3" != "eth0" ] && ifconfig $3 up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3" >>/tmp/.startup_log 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" >>/tmp/.startup_log 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" >>/tmp/.startup_log ifconfig $3 $eth0_ip netmask eth0_mask up && echo "`uptime | awk -F" " '{print $1}'`: $0 bringing up $3, ip=$eth0_ip, netmask=$eth0_netmask" >>/tmp/.startup_log 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\"" >>/tmp/.startup_log fi ;; 2) # bridge to ath2 ;; 3) # FON, bridge to ath0 echo "`uptime | awk -F" " '{print $1}'`: $0 eth0_on=$eth0_on, bridging with ath0" >>/tmp/.startup_log 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" >>/tmp/.startup_log ;; esac ;; esac echo "`uptime | awk -F" " '{print $1}'`: $0 configuring $1 $2 $3 done" >>/tmp/.startup_log ;; ################################################## esac