1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char rcsid[] =
32  "$FreeBSD: stable/10/sbin/ifconfig/af_link.c 318430 2017-05-17 22:29:25Z rpokala $";
33#endif /* not lint */
34
35#include <sys/types.h>
36#include <sys/ioctl.h>
37#include <sys/socket.h>
38#include <net/if.h>
39
40#include <err.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ifaddrs.h>
45#include <unistd.h>
46
47#include <net/if_dl.h>
48#include <net/if_types.h>
49#include <net/ethernet.h>
50
51#include "ifconfig.h"
52
53static struct ifreq link_ridreq;
54
55static void
56link_status(int s __unused, const struct ifaddrs *ifa)
57{
58	/* XXX no const 'cuz LLADDR is defined wrong */
59	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
60
61	if (sdl != NULL && sdl->sdl_alen > 0) {
62		if ((sdl->sdl_type == IFT_ETHER ||
63		    sdl->sdl_type == IFT_L2VLAN ||
64		    sdl->sdl_type == IFT_BRIDGE) &&
65		    sdl->sdl_alen == ETHER_ADDR_LEN)
66			printf("\tether %s\n",
67			    ether_ntoa((struct ether_addr *)LLADDR(sdl)));
68		else {
69			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
70
71			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
72		}
73		/* Best-effort (i.e. failures are silent) to get original
74		 * hardware address, as read by NIC driver at attach time. Only
75		 * applies to Ethernet NICs (IFT_ETHER). However, laggX
76		 * interfaces claim to be IFT_ETHER, and re-type their component
77		 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
78		 * the MAC is zeroed, then it's actually a lagg.
79		 */
80		if ((sdl->sdl_type == IFT_ETHER ||
81		    sdl->sdl_type == IFT_IEEE8023ADLAG) &&
82		    sdl->sdl_alen == ETHER_ADDR_LEN) {
83			struct ifreq ifr;
84			int sock_hw;
85			int rc;
86			static const u_char laggaddr[6] = {0};
87
88			strncpy(ifr.ifr_name, ifa->ifa_name,
89			    sizeof(ifr.ifr_name));
90			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
91			    sizeof(ifa->ifa_addr->sa_len));
92			ifr.ifr_addr.sa_family = AF_LOCAL;
93			if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
94				warn("socket(AF_LOCAL,SOCK_DGRAM)");
95				return;
96			}
97			rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
98			close(sock_hw);
99			if (rc != 0) {
100				return;
101			}
102			if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0) {
103				return;
104			}
105			printf("\thwaddr %s\n", ether_ntoa((const struct ether_addr *)
106			    &ifr.ifr_addr.sa_data));
107		}
108	}
109}
110
111static void
112link_getaddr(const char *addr, int which)
113{
114	char *temp;
115	struct sockaddr_dl sdl;
116	struct sockaddr *sa = &link_ridreq.ifr_addr;
117
118	if (which != ADDR)
119		errx(1, "can't set link-level netmask or broadcast");
120	if ((temp = malloc(strlen(addr) + 2)) == NULL)
121		errx(1, "malloc failed");
122	temp[0] = ':';
123	strcpy(temp + 1, addr);
124	sdl.sdl_len = sizeof(sdl);
125	link_addr(temp, &sdl);
126	free(temp);
127	if (sdl.sdl_alen > sizeof(sa->sa_data))
128		errx(1, "malformed link-level address");
129	sa->sa_family = AF_LINK;
130	sa->sa_len = sdl.sdl_alen;
131	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
132}
133
134static struct afswtch af_link = {
135	.af_name	= "link",
136	.af_af		= AF_LINK,
137	.af_status	= link_status,
138	.af_getaddr	= link_getaddr,
139	.af_aifaddr	= SIOCSIFLLADDR,
140	.af_addreq	= &link_ridreq,
141};
142static struct afswtch af_ether = {
143	.af_name	= "ether",
144	.af_af		= AF_LINK,
145	.af_status	= link_status,
146	.af_getaddr	= link_getaddr,
147	.af_aifaddr	= SIOCSIFLLADDR,
148	.af_addreq	= &link_ridreq,
149};
150static struct afswtch af_lladdr = {
151	.af_name	= "lladdr",
152	.af_af		= AF_LINK,
153	.af_status	= link_status,
154	.af_getaddr	= link_getaddr,
155	.af_aifaddr	= SIOCSIFLLADDR,
156	.af_addreq	= &link_ridreq,
157};
158
159static __constructor void
160link_ctor(void)
161{
162	af_register(&af_link);
163	af_register(&af_ether);
164	af_register(&af_lladdr);
165}
166