Skip to content

Commit 815fb29

Browse files
committed
Some platforms have rules for retrieving the MAC address for an interface
beyond what properties exist. For example, a local address maybe be present in a device tree property, but a system-wide property may indicate that it should not be used (in favor of e.g. a singular system MAC addres - LOOKIN' AT YOU, SUNW!). So, the ether-get-mac-address device call is introduced to handle this situation. Consult it before the standard properites, and if it succeeds, use its result.
1 parent e64dc40 commit 815fb29

4 files changed

Lines changed: 161 additions & 3 deletions

File tree

sys/net/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $NetBSD: Makefile,v 1.46 2022/09/03 02:47:59 thorpej Exp $
1+
# $NetBSD: Makefile,v 1.47 2025/10/12 23:41:09 thorpej Exp $
22

33
INCSDIR= /usr/include/net
44

@@ -13,6 +13,12 @@ INCS= bpf.h bpfjit.h bpfdesc.h dlt.h ethertypes.h if.h if_arc.h if_arp.h \
1313

1414
SUBDIR= agr npf lagg
1515

16+
ether_calls.h: ${.CURDIR}/ether_calls
17+
echo "${TOOL_AWK} -f ${.CURDIR}/../kern/gendevcalls.awk \
18+
${.CURDIR}/ether_calls > ${.CURDIR}/ether_calls.h"
19+
${TOOL_AWK} -f ${.CURDIR}/../kern/gendevcalls.awk \
20+
${.CURDIR}/ether_calls > ${.CURDIR}/ether_calls.h
21+
1622
.include <bsd.kinc.mk>
1723

1824
.PATH: ${NETBSDSRCDIR}/sys/dist/pf/net

sys/net/ether_calls

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
$NetBSD: ether_calls,v 1.1 2025/10/12 23:41:08 thorpej Exp $
2+
3+
/*-
4+
* Copyright (c) 2025 The NetBSD Foundation, Inc.
5+
* All rights reserved.
6+
*
7+
* This code is derived from software contributed to The NetBSD Foundation
8+
* by Jason R. Thorpe.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
/*
33+
* Device calls used by the Ethernet subsystem.
34+
*/
35+
36+
subsystem ether;
37+
38+
#include <net/if_ether.h>
39+
40+
/*
41+
* ether-get-mac-address
42+
*
43+
* Retrieve the MAC address for the Ethernet interface associated
44+
* with the device handle.
45+
*
46+
* While it is common for platform device trees to provide the MAC
47+
* address in a property, some platforms may use single system-wide
48+
* MAC address in addition to interface-specific addresses, and the
49+
* rules about which one to use on a given interface are encoded into
50+
* the device tree itself, hence this hook.
51+
*/
52+
ether-get-mac-address {
53+
/* This buffer is assumed to be ETHER_ADDR_LEN bytes in size. */
54+
uint8_t * enaddr; /* OUT */
55+
};

sys/net/ether_calls.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* $NetBSD: ether_calls.h,v 1.1 2025/10/12 23:41:08 thorpej Exp $ */
2+
3+
/*
4+
* THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
5+
*
6+
* generated from:
7+
* NetBSD
8+
*/
9+
10+
/*-
11+
* Copyright (c) 2025 The NetBSD Foundation, Inc.
12+
* All rights reserved.
13+
*
14+
* This code is derived from software contributed to The NetBSD Foundation
15+
* by Jason R. Thorpe.
16+
*
17+
* Redistribution and use in source and binary forms, with or without
18+
* modification, are permitted provided that the following conditions
19+
* are met:
20+
* 1. Redistributions of source code must retain the above copyright
21+
* notice, this list of conditions and the following disclaimer.
22+
* 2. Redistributions in binary form must reproduce the above copyright
23+
* notice, this list of conditions and the following disclaimer in the
24+
* documentation and/or other materials provided with the distribution.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
* POSSIBILITY OF SUCH DAMAGE.
37+
*/
38+
39+
/*
40+
* Device calls used by the Ethernet subsystem.
41+
*/
42+
43+
#ifndef _ETHER_CALLS_H_
44+
#define _ETHER_CALLS_H_
45+
46+
#include <sys/device.h>
47+
48+
#include <net/if_ether.h>
49+
50+
/*
51+
* ether-get-mac-address
52+
*
53+
* Retrieve the MAC address for the Ethernet interface associated
54+
* with the device handle.
55+
*
56+
* While it is common for platform device trees to provide the MAC
57+
* address in a property, some platforms may use single system-wide
58+
* MAC address in addition to interface-specific addresses, and the
59+
* rules about which one to use on a given interface are encoded into
60+
* the device tree itself, hence this hook.
61+
*/
62+
struct ether_get_mac_address_args {
63+
/* This buffer is assumed to be ETHER_ADDR_LEN bytes in size. */
64+
uint8_t * enaddr; /* OUT */
65+
};
66+
67+
union ether_get_mac_address_binding {
68+
struct device_call_generic generic;
69+
struct {
70+
const char *name;
71+
struct ether_get_mac_address_args *args;
72+
} binding;
73+
};
74+
75+
#define ETHER_GET_MAC_ADDRESS_STR "ether-get-mac-address"
76+
77+
#define ETHER_GET_MAC_ADDRESS(_args_) \
78+
&((const union ether_get_mac_address_binding){ \
79+
.binding.name = "ether-get-mac-address", \
80+
.binding.args = (_args_), \
81+
})
82+
83+
#endif /* _ETHER_CALLS_H_ */

sys/net/if_ethersubr.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: if_ethersubr.c,v 1.332 2025/10/04 04:44:19 thorpej Exp $ */
1+
/* $NetBSD: if_ethersubr.c,v 1.333 2025/10/12 23:41:09 thorpej Exp $ */
22

33
/*
44
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
6161
*/
6262

6363
#include <sys/cdefs.h>
64-
__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.332 2025/10/04 04:44:19 thorpej Exp $");
64+
__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.333 2025/10/12 23:41:09 thorpej Exp $");
6565

6666
#ifdef _KERNEL_OPT
6767
#include "opt_inet.h"
@@ -113,6 +113,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.332 2025/10/04 04:44:19 thorpej E
113113

114114
#include <net/if_ether.h>
115115
#include <net/if_vlanvar.h>
116+
#include <net/ether_calls.h>
116117

117118
#if NPPPOE > 0
118119
#include <net/if_pppoe.h>
@@ -192,6 +193,19 @@ static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
192193
bool
193194
ether_getaddr(device_t dev, uint8_t enaddr[ETHER_ADDR_LEN])
194195
{
196+
/*
197+
* First check the platform device tree; it may have specific
198+
* rules about how Ethernet addresses are assigned, separate
199+
* from properties associated with a given device.
200+
*/
201+
struct ether_get_mac_address_args args = {
202+
.enaddr = enaddr,
203+
};
204+
if (device_call(dev, ETHER_GET_MAC_ADDRESS(&args)) == 0) {
205+
/* Got it from the platform device tree. */
206+
return true;
207+
}
208+
195209
/*
196210
* Check first for the "mac-address" property. The bindings
197211
* say that this would be used only if it is different then the

0 commit comments

Comments
 (0)