mac80211: use the original mac address for the first virtual interface
[openwrt/staging/yousong.git] / package / mac80211 / files / lib / wifi / mac80211.sh
index 1ad3eb5086a5dc902ffaa4170c50437af15c4604..081dc4d8863261444517090585c8220e13f40c06 100644 (file)
@@ -270,17 +270,35 @@ get_freq() {
 }
 
 mac80211_generate_mac() {
-       local off="$1"
-       local mac="$2"
-       local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
+       local id="$1"
+       local ref="$2"
+       local mask="$3"
+
+       [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
+       local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
+
+       local mask1=$1
+       local mask6=$6
+
+       local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
+       [ "$((0x$mask1))" -gt 0 ] && {
+               b1="0x$1"
+               [ "$id" -gt 0 ] && \
+                       b1=$((($b1 | 0x2) ^ (($id - 1) << 2)))
+               printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
+               return
+       }
 
-       local b2mask=0x00
-       [ $off -gt 0 ] && b2mask=0x02
+       [ "$((0x$mask6))" -lt 255 ] && {
+               printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
+               return
+       }
 
-       printf "%02x:%s:%s:%s:%02x:%02x" \
-               $(( 0x$1 | $b2mask )) $2 $3 $4 \
-               $(( (0x$5 + ($off / 0x100)) % 0x100 )) \
-               $(( (0x$6 + $off) % 0x100 ))
+       off2=$(( (0x$6 + $id) / 0x100 ))
+       printf "%s:%s:%s:%s:%02x:%02x" \
+               $1 $2 $3 $4 \
+               $(( (0x$5 + $off2) % 0x100 )) \
+               $(( (0x$6 + $id) % 0x100 ))
 }
 
 enable_mac80211() {
@@ -375,7 +393,7 @@ enable_mac80211() {
                config_get macaddr "$device" macaddr
                config_get vif_mac "$vif" macaddr
                [ -n "$vif_mac" ] || {
-                       vif_mac="$(mac80211_generate_mac $macidx $macaddr)"
+                       vif_mac="$(mac80211_generate_mac $macidx $macaddr $(cat /sys/class/ieee80211/${phy}/address_mask))"
                        macidx="$(($macidx + 1))"
                }
                [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"