| #!/bin/sh |
| . /lib/functions.sh |
| |
| lookup_macaddr() { |
| local found=0 |
| |
| __lookup_macaddr() { |
| config_get macaddr $1 macaddr |
| [ -n "$macaddr" ] && found=1 |
| } |
| |
| config_load wireless |
| config_foreach __lookup_macaddr wifi-iface |
| return $found |
| } |
| |
| # |
| # Set the MAC address from MRD, if not exist generate |
| # randomly and save it |
| # |
| set_wifi_if_mac() { |
| local _ifname rootif save_name gen_mac |
| |
| config_get _ifname $1 ifname |
| case $_ifname in |
| *-vxd) |
| # VXD use same macaddr from root interface |
| rootif=${_ifname%-vxd*} |
| rootif=s_${rootif//[^0-9,a-z,A-Z]/_} |
| eval wifimac=\${wifimac_${rootif}} |
| gen_mac=1 |
| ;; |
| *-va[0-9]) |
| # VA generate mac from root interface |
| rootif=${_ifname%-va*} |
| rootif=s_${rootif//[^0-9,a-z,A-Z]/_} |
| eval wifimac=\${wifimac_${rootif}} |
| wifimac=${wifimac%:*} |
| wifimac=${wifimac}:`eval $RANDOM` |
| gen_mac=1 |
| ;; |
| *) |
| gen_mac=0 |
| wifimac= |
| ;; |
| esac |
| |
| # variable name should not has "-", make a change here |
| save_name=s_${_ifname//[^0-9,a-z,A-Z]/_} |
| [ -n "$wifimac_left" -a "$gen_mac" == "0" ] && { |
| eval wifimac=\${wifimac_${save_name}} |
| if [ "$wifimac" == "" ]; then |
| wifimac_left=$(echo ${wifimac_left} | sed 's/^[ \t]*//g') |
| wifimac_left=$(echo ${wifimac_left} | sed 's/[ \t]*$//g') |
| wifimac=${wifimac_left%% *} |
| wifimac_left=${wifimac_left}" " |
| wifimac_left=${wifimac_left#* } |
| eval wifimac_${save_name}=\${wifimac} |
| else |
| wifimac=${wifimac%:*} |
| wifimac=${wifimac}:`eval $RANDOM` |
| fi |
| } |
| |
| [ -z "$wifimac" ] && { |
| eval wifimac=\${wifimac_${save_name}} |
| if [ "$wifimac" == "" ]; then |
| wifimac=00:`eval $RANDOM`:`eval $RANDOM`:`eval $RANDOM`:`eval $RANDOM`:`eval $RANDOM` |
| eval wifimac_${save_name}=\${wifimac} |
| else |
| wifimac=${wifimac%:*} |
| wifimac=${wifimac}:`eval $RANDOM` |
| fi |
| echo "$wifimac_file not exist, create it with random address: $wifimac" |
| } |
| |
| uci_set wireless_tmp $1 macaddr $wifimac |
| } |
| |
| wlan_fixup_wifi_iface() { |
| local wifimac_file="/NVM/wifi_addr" |
| |
| __wlan_fixup_wifi_iface() { |
| local _ssid _macaddr |
| |
| config_get mode $1 mode |
| config_get ssid $1 ssid |
| |
| set_wifi_if_mac $1 |
| [ "$mode" == "ap" ] && { |
| FILTER_MAC="echo \$wifimac | sed 's/://g'" |
| _macaddr=`eval ${FILTER_MAC}` |
| _macaddr=${_macaddr:8:4} |
| _ssid=${ssid%%#[0-9,a-f][0-9,a-f][0-9,a-f][0-9,a-f]}#${_macaddr} |
| uci_set wireless_tmp $1 ssid $_ssid |
| } |
| } |
| |
| wifimac_left=`cat $wifimac_file 2>/dev/null` |
| RANDOM="head /dev/urandom | tr -dc "0123456789abcdef" | head -c2" |
| |
| config_load wireless_tmp |
| config_foreach __wlan_fixup_wifi_iface wifi-iface |
| uci_commit wireless_tmp |
| } |
| |
| [ -s /etc/config/wireless ] || return 1 |
| lookup_macaddr || return 1 |
| |
| # Load AP_MRD before |
| /bin/mrdloader |
| |
| UCI_CONFIG_DIR=/var/uci_conf |
| mkdir -p $UCI_CONFIG_DIR |
| cp /etc/config/wireless $UCI_CONFIG_DIR/wireless_tmp |
| wlan_fixup_wifi_iface |
| mv $UCI_CONFIG_DIR/wireless_tmp /etc/config/wireless |
| rm -f /var/.uci/wireless_tmp |
| rm -rf $UCI_CONFIG_DIR |
| UCI_CONFIG_DIR= |
| return 1 |