From patchwork Fri Dec 22 17:03:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [1/2] fff-network: Introduce function to set MAC on device From: Adrian Schmutzler X-Patchwork-Id: 718 Message-Id: <1513962185-3621-1-git-send-email-freifunk@adrianschmutzler.de> To: franken-dev@freifunk.net Date: Fri, 22 Dec 2017 18:03:04 +0100 Signed-off-by: Adrian Schmutzler Tested-by: Adrian Schmutzler --- This patch requires the patch - fff-network: Commit only network which must be applied BEFOREHAND. --- .../fff-network/files/lib/functions/fff/network | 31 +++++++++++++++++++ .../fff-network/files/usr/sbin/configurenetwork | 36 +++------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/packages/fff/fff-network/files/lib/functions/fff/network b/src/packages/fff/fff-network/files/lib/functions/fff/network index dc26938..95edba1 100644 --- a/src/packages/fff/fff-network/files/lib/functions/fff/network +++ b/src/packages/fff/fff-network/files/lib/functions/fff/network @@ -96,3 +96,34 @@ macFlipLocalBit() { echo "$mac" | awk -F: '{ printf("%02x:%s:%s:%s:%s:%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }' return 0 } + +fixMac() { + # Update MAC address on device/interface + # + # fixMac + # + # newmac: MAC address to be set + # dev: Device to be updated (e.g. br-mesh) + # iface: Interface to be updated (e.g. mesh) + # overwrite: If non-zero, the MAC is always replaced; if not set, the MAC is only written if none is present + + local newmac=$1 + local dev=$2 + local iface=$3 + local overwrite=$4 + + if uci get "network.${iface}.macaddr" && [ ! -n "$overwrite" ] ; then + echo "MAC for ${iface} is already set" + else + echo "Fixing MAC on ${dev} (${iface})" + sleep 10 + + uci set "network.${iface}.macaddr=$newmac" + uci commit network + + ifconfig "$dev" down + ifconfig "$dev" hw ether "$newmac" + ifconfig "$dev" up + /etc/init.d/network restart + fi +} diff --git a/src/packages/fff/fff-network/files/usr/sbin/configurenetwork b/src/packages/fff/fff-network/files/usr/sbin/configurenetwork index 96c223d..7f34d6f 100755 --- a/src/packages/fff/fff-network/files/usr/sbin/configurenetwork +++ b/src/packages/fff/fff-network/files/usr/sbin/configurenetwork @@ -157,40 +157,12 @@ fi /etc/init.d/network restart -if [[ -n "$ETHMESHMAC" ]]; then - if uci get network.ethmesh.macaddr - then - echo "MAC for ethmesh is set already" - else - echo "Fixing MAC on $SWITCHDEV.3 (ethmesh)" - sleep 10 - - uci set network.ethmesh.macaddr=$ETHMESHMAC - uci commit network - - ifconfig $SWITCHDEV.3 down - ifconfig $SWITCHDEV.3 hw ether $ETHMESHMAC - ifconfig $SWITCHDEV.3 up - /etc/init.d/network restart - fi +if [ -n "$ETHMESHMAC" ]; then + fixMac "$ETHMESHMAC" "${SWITCHDEV}.3" "ethmesh" fi -if [[ -n "$ROUTERMAC" ]]; then - if uci get network.mesh.macaddr - then - echo "MAC for mesh is set already" - else - echo "Fixing MAC on br-mesh (mesh)" - sleep 10 - - uci set network.mesh.macaddr=$ROUTERMAC - uci commit network - - ifconfig br-mesh down - ifconfig br-mesh hw ether $ROUTERMAC - ifconfig br-mesh up - /etc/init.d/network restart - fi +if [ -n "$ROUTERMAC" ]; then + fixMac "$ROUTERMAC" "br-mesh" "mesh" fi if [[ -n "$ETH0MAC" ]]; then