Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 18 Nov 2007 22:57:02 +0000
From:      Thomas Hurst <tom@hur.st>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   conf/118111: [patch] Add MAC address based interface renaming
Message-ID:  <E1Itt4Y-000Mxg-CJ@voi.aagh.net>
Resent-Message-ID: <200711182300.lAIN05x9005503@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         118111
>Category:       conf
>Synopsis:       [patch] Add MAC address based interface renaming
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Nov 18 23:00:04 UTC 2007
>Closed-Date:
>Last-Modified:
>Originator:     Thomas Hurst
>Release:        FreeBSD 6.2-STABLE amd64
>Organization:
>Environment:
System: FreeBSD valisk.nightsdawn.sf 7.0-BETA3 FreeBSD 7.0-BETA3 #0: Sun Nov 18 21:39:35 GMT 2007     root@valisk.nightsdawn.sf:/usr/obj/usr/src/sys/VALISK  i386

>Description:
	I recently temporarily added an additional network card to my system for testing,
	thus moving all my interface names about; em0 becomes em1, etc, necessitating
	annoying configuration changes and/or cable juggling.

	Many Linux distribtions solve this problem with /etc/iftab or /etc/mactab, wiring
	an interface name to a MAC address so they're no longer dependent on probe order.

	This patch adds similar functionality to network.subr's ifnet_rename function,
	so things like the following can be added to rc.conf:

	ifconfig_mac_names="lan0 inet0"
	ifconfig_mac_lan0="00:04:23:c4:68:a8"
	ifconfig_mac_inet0="00:04:23:c4:68:ab"

	Also included is a small patch against the rc.conf(5) manpage.

	Both patches are against RELENG_7, but should apply relatively cleanly against
	RELENG_6.
>How-To-Repeat:
	
>Fix:

--- network.subr.orig	2007-11-18 21:58:49.000000000 +0000
+++ network.subr	2007-11-18 22:52:43.000000000 +0000
@@ -585,18 +585,54 @@
 	return $_ret
 }
 
+get_if_mac()
+{
+	ifconfig $1 2>/dev/null | while read proto addr rest; do
+		case ${proto} in
+		ether)
+			echo $addr
+		esac
+	done
+}
+
 # ifnet_rename
 #	Rename all requested interfaces.
 #
 ifnet_rename()
 {
-
 	_ifn_list="`ifconfig -l`"
 	[ -z "$_ifn_list" ] && return 0
 	for _if in ${_ifn_list} ; do
 		_ifname=`get_if_var $_if ifconfig_IF_name`
 		if [ ! -z "$_ifname" ]; then
 			ifconfig $_if name $_ifname
+			# ifconfig returns EX_OK even if it fails
+			_if=$_ifname
+		fi
+
+		if [ ! -z "$ifconfig_mac_names" ]; then
+			mac=`get_if_mac $_if | sed -e 's/[^a-f0-9]//g'`
+			if [ ! -z "$mac" ]; then
+				eval _ifmac_${mac}_name=$_if
+			fi
+		fi
+	done
+
+	[ -z "$ifconfig_mac_names" ] && return 0
+	for _ifname in ${ifconfig_mac_names} ; do
+		_ifmac=`get_if_var $_ifname ifconfig_mac_IF`
+		if [ ! -z "$_ifmac" ] ; then
+			_mac="`echo $_ifmac |tr '[:upper:]' '[:lower:]' |sed -e 's/[^a-f0-9]//g'`"
+			_if=`eval echo \\${_ifmac_${_mac}_name}`
+			if [ ! -z $_if ] ; then
+				if [ ! $_if = $_ifname ]; then
+					ifconfig $_if name $_ifname
+				fi
+			else
+				warn "Unable to locate interface with MAC $_ifmac for $_ifname"
+			fi
+		else
+			warn "MAC not configured for $_ifname, please set ifconfig_mac_${_ifname}"
 		fi
 	done
 	return 0


--- rc.conf.5.orig	2007-09-06 22:00:48.000000000 +0100
+++ rc.conf.5	2007-11-18 22:49:54.000000000 +0000
@@ -1158,11 +1158,20 @@
 .Va pccard_ifconfig
 variable.
 .Pp
-It is also possible to rename interface by doing:
+It is also possible to rename an interface by doing:
 .Bd -literal
 ifconfig_ed0_name="net0"
 ifconfig_net0="inet 10.0.0.1 netmask 0xffff0000"
 .Ed
+.Pp
+or by interface MAC address with:
+.Bd -literal
+ifconfig_mac_names="net0 net1"
+ifconfig_mac_net0="02:0d:61:37:e3:2d"
+ifconfig_mac_net1="02:0d:61:37:e3:2e"
+ifconfig_net0="inet 10.0.0.1 netmask 0xffff0000"
+ifconfig_net1="DHCP"
+.Ed
 .It Va ipv6_network_interfaces
 .Pq Vt str
 This is the IPv6 equivalent of

>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?E1Itt4Y-000Mxg-CJ>