When needed, lladdr is more precise and enduring.
Suggested by deraadt@
Many improvments and OK kn@
#!/bin/ksh
-# $OpenBSD: install.sub,v 1.1215 2022/12/05 20:12:00 afresh1 Exp $
+# $OpenBSD: install.sub,v 1.1216 2022/12/16 04:04:25 afresh1 Exp $
#
# Copyright (c) 1997-2015 Todd Miller, Theo de Raadt, Ken Westerback
# Copyright (c) 2015, Robert Peichaer <rpe@openbsd.org>
done
}
+# Map an interface to its MAC address if it is unique
+if_name_to_lladdr() {
+ local _lladdr
+
+ _lladdr=$(ifconfig $1 2>/dev/null |
+ sed -n 's/^[[:space:]]*lladdr[[:space:]]//p')
+ [[ -n $_lladdr && -n $(ifconfig -M "$_lladdr") ]] && echo $_lladdr
+}
+
# Return the device name of the disk device $1, which may be a disklabel UID.
get_dkdev_name() {
local _dev=${1#/dev/} _d
# Start interface using the on-disk hostname.if file passed as argument $1.
# Much of this is gratuitously stolen from /etc/netstart.
ifstart() {
- local _if=$1 _hn=/mnt/etc/hostname.$1 _cmds _i=0 _line
+ local _if=$1 _lladdr _hn=/mnt/etc/hostname.$1 _cmds _i=0 _line
set -A _cmds
- if [[ $_if == ??:??:??:??:??:?? ]]; then
- _if=$(ifconfig -M $_if)
- [[ -z $_if ]] && return # invalid interface
- [[ -e /mnt/etc/hostname.$_if ]] && return # duplicate config
+ if [[ $_if == +([[:alpha:]])+([[:digit:]]) ]]; then
+ _lladdr=$(if_name_to_lladdr $_if)
+ [[ -n $_lladdr && -f /mnt/etc/hostname.$_lladdr ]] && return
+ elif [[ $_if == ??:??:??:??:??:?? ]]; then
+ _lladdr=$_if
+ _if=$(ifconfig -M $_lladdr)
+ [[ -z $_if ]] && return
+ else
+ return
fi
# Create interface if it does not yet exist.
#!/bin/sh -
#
-# $OpenBSD: netstart,v 1.230 2022/12/05 20:12:00 afresh1 Exp $
+# $OpenBSD: netstart,v 1.231 2022/12/16 04:04:25 afresh1 Exp $
# Turn off Strict Bourne shell mode.
set +o sh
# Start a single interface.
# Usage: ifstart if1
ifstart() {
- local _if=$1 _hn=/etc/hostname.$1 _cmds _i=0 _line _stat
+ local _if=$1 _lladdr _hn=/etc/hostname.$1 _cmds _i=0 _line _stat
set -A _cmds
- if [[ $_if == ??:??:??:??:??:?? ]]; then
- if ! _line=$( ifconfig -M $_if ); then
- print -u2 "${0##*/}: $_if is not unique."
+ # Interface names must be alphanumeric only. We check to avoid
+ # configuring backup or temp files, and to catch the "*" case.
+ if [[ $_if == +([[:alpha:]])+([[:digit:]]) ]]; then
+ _lladdr=$(ifconfig $_if 2>/dev/null |
+ sed -n 's/^[[:space:]]*lladdr[[:space:]]//p')
+ if [[ -n $_lladdr && -f /etc/hostname.$_lladdr && \
+ -n $(ifconfig -M "$_lladdr") ]]; then
+ print -u2 "${0##*/}: $_hn: /etc/hostname.$_lladdr overrides"
return
fi
- [[ -z $_line ]] && return
- _if=$_line
- _line=
-
- if [[ -e /etc/hostname.$_if ]]; then
- print -u2 "${0##*/}: $_hn: /etc/hostname.$_if overrides"
+ # We also support hostname.lladdr, but it must resolve to be valid
+ elif [[ $_if == ??:??:??:??:??:?? ]]; then
+ _lladdr=$_if
+ _if=$(ifconfig -M $_lladdr)
+ if (($? != 0)); then
+ print -u2 "${0##*/}: $_lladdr is not unique."
return
fi
- fi
- # Interface names must be alphanumeric only. We check to avoid
- # configuring backup or temp files, and to catch the "*" case.
- [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return
+ [[ -z $_if ]] && return
+ else
+ return
+ fi
if [[ ! -f $_hn ]]; then
print -u2 "${0##*/}: $_hn: No such file or directory."
-.\" $OpenBSD: hostname.if.5,v 1.81 2022/12/15 06:39:05 jmc Exp $
+.\" $OpenBSD: hostname.if.5,v 1.82 2022/12/16 04:04:25 afresh1 Exp $
.\" $NetBSD: hosts.5,v 1.4 1994/11/30 19:31:20 jtc Exp $
.\"
.\" Copyright (c) 1983, 1991, 1993
.\"
.\" @(#)hosts.5 8.2 (Berkeley) 12/11/93
.\"
-.Dd $Mdocdate: December 15 2022 $
+.Dd $Mdocdate: December 16 2022 $
.Dt HOSTNAME.IF 5
.Os
.Sh NAME
their link layer address (lladdr),
such as
.Dq hostname.00:00:5e:00:53:af .
-Priority is given to configuration by interface name/unit over lladdr.
+Priority is given to configuration by interface lladdr over name/unit.
A configuration file is not needed for lo0.
.Pp
The configuration information is expressed in a line-by-line packed format