2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
53 static struct ifreq link_ridreq;
58 link_status(int s __unused, const struct ifaddrs *ifa)
60 /* XXX no const 'cuz LLADDR is defined wrong */
61 struct sockaddr_dl *sdl;
62 char *ether_format, *format_char;
65 static const u_char laggaddr[6] = {0};
67 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
68 if (sdl == NULL || sdl->sdl_alen == 0)
71 if ((sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_L2VLAN ||
72 sdl->sdl_type == IFT_BRIDGE) && sdl->sdl_alen == ETHER_ADDR_LEN) {
73 ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl));
74 if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
75 for (format_char = strchr(ether_format, ':');
77 format_char = strchr(ether_format, ':'))
80 printf("\tether %s\n", ether_format);
82 n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
83 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
87 * Best-effort (i.e. failures are silent) to get original
88 * hardware address, as read by NIC driver at attach time. Only
89 * applies to Ethernet NICs (IFT_ETHER). However, laggX
90 * interfaces claim to be IFT_ETHER, and re-type their component
91 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
92 * the MAC is zeroed, then it's actually a lagg.
94 if ((sdl->sdl_type != IFT_ETHER &&
95 sdl->sdl_type != IFT_IEEE8023ADLAG) ||
96 sdl->sdl_alen != ETHER_ADDR_LEN)
99 strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
100 memcpy(&ifr.ifr_addr, ifa->ifa_addr, sizeof(ifa->ifa_addr->sa_len));
101 ifr.ifr_addr.sa_family = AF_LOCAL;
102 if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
103 warn("socket(AF_LOCAL,SOCK_DGRAM)");
106 rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
111 if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0)
114 ether_format = ether_ntoa((const struct ether_addr *)
115 &ifr.ifr_addr.sa_data);
116 if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
117 for (format_char = strchr(ether_format, ':');
119 format_char = strchr(ether_format, ':'))
122 printf("\thwaddr %s\n", ether_format);
125 if (ioctl(s, SIOCGLANPCP, (caddr_t)&ifr) == 0 &&
126 ifr.ifr_lan_pcp != IFNET_PCP_NONE)
127 printf("\tpcp %d\n", ifr.ifr_lan_pcp);
131 link_getaddr(const char *addr, int which)
134 struct sockaddr_dl sdl;
135 struct sockaddr *sa = &link_ridreq.ifr_addr;
138 errx(1, "can't set link-level netmask or broadcast");
139 if (!strcmp(addr, "random")) {
140 sdl.sdl_len = sizeof(sdl);
141 sdl.sdl_alen = ETHER_ADDR_LEN;
143 sdl.sdl_family = AF_LINK;
144 arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
145 /* Non-multicast and claim it is locally administered. */
146 sdl.sdl_data[0] &= 0xfc;
147 sdl.sdl_data[0] |= 0x02;
149 if ((temp = malloc(strlen(addr) + 2)) == NULL)
150 errx(1, "malloc failed");
152 strcpy(temp + 1, addr);
153 sdl.sdl_len = sizeof(sdl);
154 link_addr(temp, &sdl);
157 if (sdl.sdl_alen > sizeof(sa->sa_data))
158 errx(1, "malformed link-level address");
159 sa->sa_family = AF_LINK;
160 sa->sa_len = sdl.sdl_alen;
161 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
164 static struct afswtch af_link = {
167 .af_status = link_status,
168 .af_getaddr = link_getaddr,
169 .af_aifaddr = SIOCSIFLLADDR,
170 .af_addreq = &link_ridreq,
172 static struct afswtch af_ether = {
175 .af_status = link_status,
176 .af_getaddr = link_getaddr,
177 .af_aifaddr = SIOCSIFLLADDR,
178 .af_addreq = &link_ridreq,
180 static struct afswtch af_lladdr = {
183 .af_status = link_status,
184 .af_getaddr = link_getaddr,
185 .af_aifaddr = SIOCSIFLLADDR,
186 .af_addreq = &link_ridreq,
189 static __constructor void
192 af_register(&af_link);
193 af_register(&af_ether);
194 af_register(&af_lladdr);