11ed73e7joerg/*-
21ed73e7joerg * SPDX-License-Identifier: BSD-3-Clause
31ed73e7joerg *
41ed73e7joerg * Copyright (c) 1983, 1993
51ed73e7joerg *	The Regents of the University of California.  All rights reserved.
61ed73e7joerg *
71ed73e7joerg * Redistribution and use in source and binary forms, with or without
81ed73e7joerg * modification, are permitted provided that the following conditions
91ed73e7joerg * are met:
101ed73e7joerg * 1. Redistributions of source code must retain the above copyright
111ed73e7joerg *    notice, this list of conditions and the following disclaimer.
121ed73e7joerg * 2. Redistributions in binary form must reproduce the above copyright
131ed73e7joerg *    notice, this list of conditions and the following disclaimer in the
141ed73e7joerg *    documentation and/or other materials provided with the distribution.
151ed73e7joerg * 3. Neither the name of the University nor the names of its contributors
161ed73e7joerg *    may be used to endorse or promote products derived from this software
171ed73e7joerg *    without specific prior written permission.
181ed73e7joerg *
191ed73e7joerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201ed73e7joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211ed73e7joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221ed73e7joerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231ed73e7joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241ed73e7joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251ed73e7joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261ed73e7joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271ed73e7joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281ed73e7joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291ed73e7joerg * SUCH DAMAGE.
301ed73e7joerg */
311ed73e7joerg
321ed73e7joerg#ifndef lint
331ed73e7joergstatic const char rcsid[] =
341ed73e7joerg  "$FreeBSD$";
351ed73e7joerg#endif /* not lint */
361ed73e7joerg
371ed73e7joerg#include <sys/types.h>
381ed73e7joerg#include <sys/ioctl.h>
391ed73e7joerg#include <sys/socket.h>
401ed73e7joerg#include <net/if.h>
411ed73e7joerg
421ed73e7joerg#include <err.h>
431ed73e7joerg#include <stdio.h>
441ed73e7joerg#include <stdlib.h>
451ed73e7joerg#include <string.h>
461ed73e7joerg#include <ifaddrs.h>
471ed73e7joerg#include <unistd.h>
481ed73e7joerg
491ed73e7joerg#include <net/if_dl.h>
501ed73e7joerg#include <net/if_types.h>
511ed73e7joerg#include <net/ethernet.h>
521ed73e7joerg
531ed73e7joerg#include "ifconfig.h"
541ed73e7joerg
551ed73e7joergstatic struct ifreq link_ridreq;
561ed73e7joerg
571ed73e7joergextern char *f_ether;
581ed73e7joerg
591ed73e7joergstatic void
601ed73e7joerglink_status(int s __unused, const struct ifaddrs *ifa)
611ed73e7joerg{
621ed73e7joerg	/* XXX no const 'cuz LLADDR is defined wrong */
631ed73e7joerg	struct sockaddr_dl *sdl;
641ed73e7joerg	char *ether_format, *format_char;
651ed73e7joerg	struct ifreq ifr;
661ed73e7joerg	int n, rc, sock_hw;
671ed73e7joerg	static const u_char laggaddr[6] = {0};
681ed73e7joerg
691ed73e7joerg	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
701ed73e7joerg	if (sdl == NULL || sdl->sdl_alen == 0)
711ed73e7joerg		return;
721ed73e7joerg
731ed73e7joerg	if ((sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_L2VLAN ||
741ed73e7joerg	    sdl->sdl_type == IFT_BRIDGE) && sdl->sdl_alen == ETHER_ADDR_LEN) {
751ed73e7joerg		ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl));
761ed73e7joerg		if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
771ed73e7joerg			for (format_char = strchr(ether_format, ':');
781ed73e7joerg			    format_char != NULL;
791ed73e7joerg			    format_char = strchr(ether_format, ':'))
801ed73e7joerg				*format_char = '-';
811ed73e7joerg		}
821ed73e7joerg		printf("\tether %s\n", ether_format);
831ed73e7joerg	} else {
841ed73e7joerg		n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
851ed73e7joerg		printf("\tlladdr %s\n", link_ntoa(sdl) + n);
861ed73e7joerg	}
871ed73e7joerg
881ed73e7joerg	/*
891ed73e7joerg	 * Best-effort (i.e. failures are silent) to get original
901ed73e7joerg	 * hardware address, as read by NIC driver at attach time. Only
911ed73e7joerg	 * applies to Ethernet NICs (IFT_ETHER). However, laggX
921ed73e7joerg	 * interfaces claim to be IFT_ETHER, and re-type their component
931ed73e7joerg	 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
941ed73e7joerg	 * the MAC is zeroed, then it's actually a lagg.
951ed73e7joerg	 */
961ed73e7joerg	if ((sdl->sdl_type != IFT_ETHER &&
971ed73e7joerg	    sdl->sdl_type != IFT_IEEE8023ADLAG) ||
981ed73e7joerg	    sdl->sdl_alen != ETHER_ADDR_LEN)
991ed73e7joerg		return;
1001ed73e7joerg
1011ed73e7joerg	strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
1021ed73e7joerg	memcpy(&ifr.ifr_addr, ifa->ifa_addr, sizeof(ifa->ifa_addr->sa_len));
1031ed73e7joerg	ifr.ifr_addr.sa_family = AF_LOCAL;
1041ed73e7joerg	if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
1051ed73e7joerg		warn("socket(AF_LOCAL,SOCK_DGRAM)");
1061ed73e7joerg		return;
1071ed73e7joerg	}
1081ed73e7joerg	rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
109	close(sock_hw);
110	if (rc != 0)
111		return;
112
113	/*
114	 * If this is definitely a lagg device or the hwaddr
115	 * matches the link addr, don't bother.
116	 */
117	if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0 ||
118	    memcmp(ifr.ifr_addr.sa_data, LLADDR(sdl), sdl->sdl_alen) == 0)
119		goto pcp;
120
121	ether_format = ether_ntoa((const struct ether_addr *)
122	    &ifr.ifr_addr.sa_data);
123	if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
124		for (format_char = strchr(ether_format, ':');
125		     format_char != NULL;
126		     format_char = strchr(ether_format, ':'))
127			*format_char = '-';
128	}
129	printf("\thwaddr %s\n", ether_format);
130
131pcp:
132	if (ioctl(s, SIOCGLANPCP, (caddr_t)&ifr) == 0 &&
133	    ifr.ifr_lan_pcp != IFNET_PCP_NONE)
134		printf("\tpcp %d\n", ifr.ifr_lan_pcp);
135}
136
137static void
138link_getaddr(const char *addr, int which)
139{
140	char *temp;
141	struct sockaddr_dl sdl;
142	struct sockaddr *sa = &link_ridreq.ifr_addr;
143
144	if (which != ADDR)
145		errx(1, "can't set link-level netmask or broadcast");
146	if (!strcmp(addr, "random")) {
147		sdl.sdl_len = sizeof(sdl);
148		sdl.sdl_alen = ETHER_ADDR_LEN;
149		sdl.sdl_nlen = 0;
150		sdl.sdl_family = AF_LINK;
151		arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
152		/* Non-multicast and claim it is locally administered. */
153		sdl.sdl_data[0] &= 0xfc;
154		sdl.sdl_data[0] |= 0x02;
155	} else {
156		if ((temp = malloc(strlen(addr) + 2)) == NULL)
157			errx(1, "malloc failed");
158		temp[0] = ':';
159		strcpy(temp + 1, addr);
160		sdl.sdl_len = sizeof(sdl);
161		link_addr(temp, &sdl);
162		free(temp);
163	}
164	if (sdl.sdl_alen > sizeof(sa->sa_data))
165		errx(1, "malformed link-level address");
166	sa->sa_family = AF_LINK;
167	sa->sa_len = sdl.sdl_alen;
168	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
169}
170
171static struct afswtch af_link = {
172	.af_name	= "link",
173	.af_af		= AF_LINK,
174	.af_status	= link_status,
175	.af_getaddr	= link_getaddr,
176	.af_aifaddr	= SIOCSIFLLADDR,
177	.af_addreq	= &link_ridreq,
178};
179static struct afswtch af_ether = {
180	.af_name	= "ether",
181	.af_af		= AF_LINK,
182	.af_status	= link_status,
183	.af_getaddr	= link_getaddr,
184	.af_aifaddr	= SIOCSIFLLADDR,
185	.af_addreq	= &link_ridreq,
186};
187static struct afswtch af_lladdr = {
188	.af_name	= "lladdr",
189	.af_af		= AF_LINK,
190	.af_status	= link_status,
191	.af_getaddr	= link_getaddr,
192	.af_aifaddr	= SIOCSIFLLADDR,
193	.af_addreq	= &link_ridreq,
194};
195
196static __constructor void
197link_ctor(void)
198{
199	af_register(&af_link);
200	af_register(&af_ether);
201	af_register(&af_lladdr);
202}
203