#!/bin/sh . /etc/functions.sh start_ppp() { config_get username $ifn username config_get password $ifn password config_get keepalive $ifn keepalive config_get mtu $ifn mtu config_get demand $ifn 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 "$ifn" \ mtu $mtu mru $mtu \ ipparam "$ifn" } setup_interface() { local ifn="$1" local dev="$2" dhcp_enabled= config_get mode $ifn mode config_get dhcp $ifn dhcp config_get ipaddr $ifn ipaddr touch /tmp/resolv.conf case "$mode" in static) config_get netmask $ifn netmask config_get dhcp $ifn dhcp netmask=${netmask:-255.255.255.0} ifconfig "$dev" $ipaddr netmask $netmask up config_get gateway $ifn gateway [ -n "$gateway" ] && { while route del default >&- 2>&- ; do :; done route add default gw "$gateway" } #config_get dns "$ifn" dns #[ -n "$dns" ] && { # echo > /tmp/resolv.conf # for server in $dns; do # echo "nameserver $server" >> /tmp/resolv.conf # done #} case "$dhcp" in 0|off|disabled);; *)dhcp_enabled=1;; esac env -i ACTION=ifup INTERFACE="$ifn" /sbin/hotplug iface ;; dhcp) ifconfig "$dev" up udhcpc -H `cat /etc/hostname` -i "$dev" ${ipaddr:+ -r $ipaddr} -R & ;; pppoe|pptp) ifconfig "$dev" up case "$mode" in pppoe) for module in slhc ppp_generic pppox pppoe; do /sbin/insmod $module 2>&- >&- done start_ppp \ plugin rp-pppoe.so \ nic-$dev ;; pptp) config_get server $ifn pptp_server udhcpc -H `cat /etc/hostname` -i "$dev" ${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 $server --loglevel 0 --nolaunchpppd" \ file /etc/ppp/options.pptp \ $PPPOPTS ;; esac ;; wlan) ;; esac [ -n "$dhcp_enabled" ] && { # calculate the dhcp pool settings config_get start $ifn dhcp_start config_get num $ifn dhcp_num config_get time $ifn dhcp_leasetime eval $(ipcalc $ipaddr $netmask ${start:-100} ${num:-150}) dhcpopts="${dhcpopts:+$dhcpopts } --dhcp-range=$START,$END,$NETMASK,${time:-12h}" } } setup_wifi_common() { local vif="$1" config_get bgmode advanced bgmode config_get channel advanced channel case "$bgmode" in b|B) bgmode=11b; pureg=0;; g|G) bgmode=11g; pureg=1;; *) bgmode=0; pureg=0;; # auto esac iwconfig $vif channel 0 ifconfig $vif up iwpriv $vif mode $bgmode iwpriv $vif pureg $pureg [ -n "$channel" -a -z "${channel%%[0-9][0-9]}" ] && { iwconfig $vif channel "$channel" } || { # auto iwconfig $vif channel 0 } } setup_hotspot_wifi() { wlanconfig ath0 create wlandev wifi0 wlanmode ap >&- 2>&- setup_wifi_common ath0 config_load fon config_get ssid public essid iwconfig ath0 essid "FON_${ssid:-AP}" ifconfig eth0 up ifconfig ath0 up iwpriv ath0 ap_bridge 0 } setup_lan_wifi() { config_get ssid private essid [ -n "$ssid" ] && { config_get enc private encryption config_get crypto private wpa_crypto config_get passwd private password #config_get auth private shared_auth config_get hidden private hidden if [ -z "$hidden" ]; then hidden=0 fi #sh_auth=1 case "$enc" in open) #[ "$auth" = 1 ] && sh_auth=2 ;; wep) #[ "$auth" = 1 ] && sh_auth=2 config_get wep private wepkey ;; WPA|WPA1|wpa|wpa1) wpa=1; crypt=TKIP;; WPA2|wpa2) wpa=2; crypt="CCMP TKIP";; mixed|Mixed) wpa=3; crypt="CCMP TKIP";; esac case "$crypto" in aes|AES|ccmp|CCMP) crypt=CCMP;; tkip|TKIP) crypt=TKIP;; tkip+aes|TKIP+AES|aes+tkip|AES+TKIP) crypt="TKIP CCMP";; esac wlanconfig ath1 create wlandev wifi0 wlanmode ap >&- 2>&- setup_wifi_common ath1 iwconfig ath1 essid "$ssid" # Someone explain me this line! Are the FON coders on crack, or why do they configure ath0 while we set up ath1??? #iwpriv ath0 authmode $sh_auth iwpriv ath1 hide_ssid $hidden [ -z "$wep" ] || { 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 "[$wep]" } [ -z "$crypt" -o -z "$wpa" ] || ( killall hostapd 2>/dev/null >/dev/null && sleep 1 cat > /tmp/hostapd.conf < /dev/null 2>/dev/null /dev/null 2>/dev/null; then # already taken ip="169.254.255.$((${ip##*\.} + 1))" else break; fi # this should never happen: [ "${ip##*\.}" = "254" ] && { ifconfig "$ifn:1" 0.0.0.0 down return 1 } done ifconfig "$ifn:1" "$ip" netmask 255.255.0.0 ifconfig "$ifn" up } setup_hostname() { ipwan=`ifconfig $1 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 ifn="$1" local dev="$2" config_get mac $ifn 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=$(ifconfig eth0|grep HWaddr|sed -e "s/^.*HWaddr //" |sed -e "s/ //g") fi ifconfig "$dev" down ifconfig "$dev" hw ether $mac ifconfig "$dev" up } setup_bridge() { config_load fon config_get mode wan mode # create bridge interface /usr/sbin/brctl addbr br0 /usr/sbin/brctl stp br0 off /usr/sbin/brctl setfd br0 0 # bring up the bridge interface /sbin/ifconfig br0 up # add the old interfaces to the bridge /usr/sbin/brctl addif br0 ath1 /usr/sbin/brctl addif br0 eth0 # Set IP for the bridge if [ "$mode" = "static" ]; then config_get ipaddr wan ipaddr config_get netmask wan netmask config_get gateway wan gateway /sbin/ifconfig br0 "$ipaddr" netmask "$netmask" /sbin/route add default gw "$gateway" env -i ACTION=ifup INTERFACE=wan /sbin/hotplug iface else # get new IP via dhcp /sbin/udhcpc -H `cat /etc/hostname` -i br0 -R fi # add bridge to hostapd.conf and restart hostapd (allows to use WPA) [ `grep -c bridge=br0 /tmp/hostapd.conf` = "0" ] && echo bridge=br0 >> /tmp/hostapd.conf /usr/bin/killall killall hostapd /usr/sbin/hostapd -B /tmp/hostapd.conf } setup_qrm_wan() { local ifn="$1" local dev="$2" config_load fon config_get dhcp wan dhcp config_get ip wan ipaddr config_get netmask wan netmask config_get gateway wan gateway setup_qrm_common "$dev" case "$dhcp" in 1) udhcpc -H `cat /etc/hostname` -i "$dev" -R ;; *) ifconfig "$dev" $ip netmask $netmask up route add default gw $gateway env -i ACTION=ifup INTERFACE=wan /sbin/hotplug iface ;; esac } setup_qrm_common() { local dev="$1" config_load fon config_get rssid wan rssid config_get key wan key config_get enc wan enc # VAP ath0 as station managed wlanconfig "$dev" create wlandev wifi0 wlanmode sta nosbeacon setup_wifi_common "$dev" ifconfig "$dev" up case "$enc" in wpa|WPA) iwpriv "$dev" wpa 3 iwconfig "$dev" essid "$rssid" wpa_supplicant -i"$dev" -c/etc/wpa_supplicant.conf -B -d ;; wep|WEP) iwconfig "$dev" key "$key" # authmode # 1: Open # 2: shared key # 3: 02.1x # 4: auto select/accept (seems to work better then 2) iwpriv "$dev" authmode 4 iwconfig "$dev" essid "$rssid" ;; *) iwconfig "$dev" essid "$rssid" ;; esac } setup_qrm_bridge() { local lan_dev="$1" local wifi_dev="$2" setup_qrm_common "$wifi_dev" config_load fon config_get dhcp wan dhcp # create bridge interface /usr/sbin/brctl addbr br0 /usr/sbin/brctl stp br0 off /usr/sbin/brctl setfd br0 0 # bring up the bridge interface /sbin/ifconfig br0 up # add the old interfaces to the bridge /usr/sbin/brctl addif br0 "$lan_dev" /usr/sbin/brctl addif br0 "$wifi_dev" # Set IP for the bridge if enabled $dhcp; then # get new IP via dhcp /sbin/udhcpc -i br0 -R else config_get ipaddr mode ipaddr config_get netmask mode netmask config_get gateway mode gateway /sbin/ifconfig br0 "$ipaddr" netmask "$netmask" /sbin/route add default gw "$gateway" env -i ACTION=ifup INTERFACE=wan /sbin/hotplug iface fi # add bridge to hostapd.conf and restart hostapd (allows to use WPA) [ `grep -c bridge=br0 /tmp/hostapd.conf` = "0" ] && echo bridge=br0 >> /tmp/hostapd.conf /usr/bin/killall killall hostapd /usr/sbin/hostapd -B /tmp/hostapd.conf } set_txpower() { config_load fon config_get txp private txpower # iwconfig ath1 txpower $txp } config_load fon config_get proto wan mode case "$proto" in pppoe|pptp) . /tmp/network-config cat > /tmp/network-config </dev/null 2>/dev/null && { setup_interface lan ath1 } [ "$1" = lan_noinet ] && dhcpopts="${dhcpopts:+$dhcpopts }--address=/#/213.134.45.154 -T 1" dnsmasq -C /etc/dnsmasq.conf -i ath1 -z -z -I lo,eth0 $dhcpopts & # set_txpower ;; wan) ifdown wan ifdown br_all kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 kill `cat /var/run/dnsmasq.pid` >/dev/null 2>&1 /usr/bin/killall -9 udhcpc set_mac wan eth0 setup_interface wan eth0 config_get mode wan mode config_get dhcp wan dhcp [ -z "$dhcp" ] && dhcp=0 if [ $dhcp = 0 ];then rm -f /etc/resolv.conf config_get dns wan dns if [ -n "$dns" ]; then touch /etc/resolv.conf for server in $dns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi setup_hostname eth0 fi ;; hotspot) ifdown hotspot setup_hotspot_wifi ;; bridge) ifdown wan ifdown br_all # Kill DHCP server+ client kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 kill `cat /var/run/dnsmasq.pid` >/dev/null 2>&1 /usr/bin/killall -9 udhcpc # shutdown/remove IPs from the old interfaces /sbin/ifconfig eth0 0.0.0.0 /sbin/ifconfig ath1 0.0.0.0 set_mac wan eth0 setup_bridge ifconfig eth0:1 0.0.0.0 down setup_management br0 # restarting dnsmasq but without dhcp dnsmasq rm -f /etc/resolv.conf config_get dns wan dns if [ -n "$dns" ]; then touch /etc/resolv.conf for server in $dns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi setup_hostname eth0 ;; qrm_wan) ifconfig ath1 0.0.0.0 ifconfig eth0 0.0.0.0 ifdown wan ifdown lan #ifdown wifi ifdown br_all # Stopping daemons killall -9 udhcpc kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 kill `cat /var/run/dnsmasq.pid` >/dev/null 2>&1 killall wpa_supplicant set_mac wan eth0 ifconfig eth0 up # create new VAP devices wlanconfig ath1 destroy wlanconfig ath2 destroy setup_qrm_wan wan ath2 setup_hostname ath2 # dns rm -f /etc/resolv.conf config_load fon config_get dns wan dns config_get dhcp wan dhcp if enabled $dhcp; then config_get fdns wan dns if [ -n "$fdns" ]; then touch /etc/resolv.conf for server in $fdns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi else if [ -n "$dns" ]; then touch /etc/resolv.conf for server in $dns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi fi ;; qrm_lan) config_load router config_get eth0_ip eth0 eth0_ip config_get eth0_mask eth0 eth0_mask config_get eth0_dhcp eth0 eth0_dhcp ifconfig eth0 $eth0_ip netmask eth0_mask up kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 if [ $eth0_dhcp = 1 ];then dnsmasq -C /etc/dnsmasq.repeater \ -i eth0 \ -x /var/run/dnsmasq.repeater.pid \ -z \ -I lo,ath1 fi ;; qrm_eth0_fon) kill `cat /var/run/dnsmasq.repeater.pid` >/dev/null 2>&1 ifconfig eth0 0.0.0.0 up ifconfig ath0 0.0.0.0 up brctl delif br1 eth0 brctl delif br1 ath0 brctl delbr br1 brctl addbr br1 brctl stp br1 off brctl setfd br1 0 brctl addif br1 eth0 brctl addif br1 ath0 ;; qrm_bridge) # This doesn't work at the moment, anyone feel like fixing it? ifdown wan ifdown lan ifdown wifi ifdown br_all ifconfig ath1 0.0.0.0 ifconfig eth0 0.0.0.0 # Stopping daemons killall -9 udhcpc killall dnsmasq killall N50chillispot killall chilli killall wpa_supplicant # create new VAP devices wlanconfig ath0 destroy wlanconfig ath1 destroy set_mac wan eth0 setup_qrm_bridge eth0 ath1 # dns rm -f /etc/resolv.conf config_load router config_get dns mode dns config_get dhcp mode dhcp if enabled $dhcp; then config_load fon config_get fdns wan dns if [ -n "$fdns" ]; then touch /etc/resolv.conf for server in $fdns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi else if [ -n "$dns" ]; then touch /etc/resolv.conf for server in $dns; do echo "nameserver $server" >> /etc/resolv.conf done else echo "nameserver 208.67.222.222" > /etc/resolv.conf echo "nameserver 208.67.220.220" >> /etc/resolv.conf fi fi ;; esac