xref: /freebsd/sbin/ifconfig/af_link.c (revision 7ee6b0f125a092ed99d327bb8d608dd2ff77b7aa)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36 
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ifaddrs.h>
47 #include <unistd.h>
48 
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 
53 #include "ifconfig.h"
54 #include "ifconfig_netlink.h"
55 
56 static struct ifreq link_ridreq;
57 
58 extern char *f_ether;
59 
60 static void
61 print_ether(const struct ether_addr *addr, const char *prefix)
62 {
63 	char *ether_format = ether_ntoa(addr);
64 
65 	if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
66 		char *format_char;
67 
68 		while ((format_char = strchr(ether_format, ':')) != NULL) {
69 			*format_char = '-';
70 		}
71 	}
72 	printf("\t%s %s\n", prefix, ether_format);
73 }
74 
75 static void
76 print_lladdr(struct sockaddr_dl *sdl)
77 {
78 	if (match_ether(sdl)) {
79 		print_ether((struct ether_addr *)LLADDR(sdl), "ether");
80 	} else {
81 		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
82 		printf("\tlladdr %s\n", link_ntoa(sdl) + n);
83 	}
84 }
85 
86 static void
87 print_pcp(int s)
88 {
89 	if (ioctl(s, SIOCGLANPCP, (caddr_t)&ifr) == 0 &&
90 	    ifr.ifr_lan_pcp != IFNET_PCP_NONE)
91 		printf("\tpcp %d\n", ifr.ifr_lan_pcp);
92 }
93 
94 #ifdef WITHOUT_NETLINK
95 static void
96 link_status(if_ctx *ctx, const struct ifaddrs *ifa)
97 {
98 	/* XXX no const 'cuz LLADDR is defined wrong */
99 	struct sockaddr_dl *sdl;
100 	struct ifreq ifr;
101 	int rc, sock_hw;
102 	static const u_char laggaddr[6] = {0};
103 
104 	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
105 	if (sdl == NULL || sdl->sdl_alen == 0)
106 		return;
107 
108 	print_lladdr(sdl);
109 
110 	/*
111 	 * Best-effort (i.e. failures are silent) to get original
112 	 * hardware address, as read by NIC driver at attach time. Only
113 	 * applies to Ethernet NICs (IFT_ETHER). However, laggX
114 	 * interfaces claim to be IFT_ETHER, and re-type their component
115 	 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
116 	 * the MAC is zeroed, then it's actually a lagg.
117 	 */
118 	if ((sdl->sdl_type != IFT_ETHER &&
119 	    sdl->sdl_type != IFT_IEEE8023ADLAG) ||
120 	    sdl->sdl_alen != ETHER_ADDR_LEN)
121 		return;
122 
123 	strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
124 	memcpy(&ifr.ifr_addr, ifa->ifa_addr, sizeof(ifa->ifa_addr->sa_len));
125 	ifr.ifr_addr.sa_family = AF_LOCAL;
126 	if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
127 		warn("socket(AF_LOCAL,SOCK_DGRAM)");
128 		return;
129 	}
130 	rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
131 	close(sock_hw);
132 	if (rc != 0)
133 		return;
134 
135 	/*
136 	 * If this is definitely a lagg device or the hwaddr
137 	 * matches the link addr, don't bother.
138 	 */
139 	if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0 ||
140 	    memcmp(ifr.ifr_addr.sa_data, LLADDR(sdl), sdl->sdl_alen) == 0)
141 		goto pcp;
142 
143 	print_ether((const struct ether_addr *)&ifr.ifr_addr.sa_data, "hwaddr");
144 pcp:
145 	print_pcp(ctx->io_s);
146 }
147 
148 #else
149 static uint8_t
150 convert_iftype(uint8_t iftype)
151 {
152 	switch (iftype) {
153 	case IFT_IEEE8023ADLAG:
154 		return (IFT_ETHER);
155 	case IFT_INFINIBANDLAG:
156 		return (IFT_INFINIBAND);
157 	}
158 	return (iftype);
159 }
160 
161 static void
162 link_status_nl(if_ctx *ctx, if_link_t *link, if_addr_t *ifa __unused)
163 {
164 	if (link->ifla_address != NULL) {
165 		struct sockaddr_dl sdl = {
166 			.sdl_len = sizeof(struct sockaddr_dl),
167 			.sdl_family = AF_LINK,
168 			.sdl_type = convert_iftype(link->ifi_type),
169 			.sdl_alen = NLA_DATA_LEN(link->ifla_address),
170 		};
171 		memcpy(LLADDR(&sdl), NLA_DATA(link->ifla_address), sdl.sdl_alen);
172 		print_lladdr(&sdl);
173 
174 		if (link->iflaf_orig_hwaddr != NULL) {
175 			struct nlattr *hwaddr = link->iflaf_orig_hwaddr;
176 
177 			if (memcmp(NLA_DATA(hwaddr), NLA_DATA(link->ifla_address), sdl.sdl_alen))
178 				print_ether((struct ether_addr *)NLA_DATA(hwaddr), "hwaddr");
179 		}
180 	}
181 	if (convert_iftype(link->ifi_type) == IFT_ETHER)
182 		print_pcp(ctx->io_s);
183 }
184 #endif
185 
186 static void
187 link_getaddr(const char *addr, int which)
188 {
189 	char *temp;
190 	struct sockaddr_dl sdl;
191 	struct sockaddr *sa = &link_ridreq.ifr_addr;
192 
193 	if (which != ADDR)
194 		errx(1, "can't set link-level netmask or broadcast");
195 	if (!strcmp(addr, "random")) {
196 		sdl.sdl_len = sizeof(sdl);
197 		sdl.sdl_alen = ETHER_ADDR_LEN;
198 		sdl.sdl_nlen = 0;
199 		sdl.sdl_family = AF_LINK;
200 		arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
201 		/* Non-multicast and claim it is locally administered. */
202 		sdl.sdl_data[0] &= 0xfc;
203 		sdl.sdl_data[0] |= 0x02;
204 	} else {
205 		if ((temp = malloc(strlen(addr) + 2)) == NULL)
206 			errx(1, "malloc failed");
207 		temp[0] = ':';
208 		strcpy(temp + 1, addr);
209 		sdl.sdl_len = sizeof(sdl);
210 		link_addr(temp, &sdl);
211 		free(temp);
212 	}
213 	if (sdl.sdl_alen > sizeof(sa->sa_data))
214 		errx(1, "malformed link-level address");
215 	sa->sa_family = AF_LINK;
216 	sa->sa_len = sdl.sdl_alen;
217 	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
218 }
219 
220 static struct afswtch af_link = {
221 	.af_name	= "link",
222 	.af_af		= AF_LINK,
223 #ifdef WITHOUT_NETLINK
224 	.af_status	= link_status,
225 #else
226 	.af_status	= link_status_nl,
227 #endif
228 	.af_getaddr	= link_getaddr,
229 	.af_aifaddr	= SIOCSIFLLADDR,
230 	.af_addreq	= &link_ridreq,
231 	.af_exec	= af_exec_ioctl,
232 };
233 static struct afswtch af_ether = {
234 	.af_name	= "ether",
235 	.af_af		= AF_LINK,
236 #ifdef WITHOUT_NETLINK
237 	.af_status	= link_status,
238 #else
239 	.af_status	= link_status_nl,
240 #endif
241 	.af_getaddr	= link_getaddr,
242 	.af_aifaddr	= SIOCSIFLLADDR,
243 	.af_addreq	= &link_ridreq,
244 	.af_exec	= af_exec_ioctl,
245 };
246 static struct afswtch af_lladdr = {
247 	.af_name	= "lladdr",
248 	.af_af		= AF_LINK,
249 #ifdef WITHOUT_NETLINK
250 	.af_status	= link_status,
251 #else
252 	.af_status	= link_status_nl,
253 #endif
254 	.af_getaddr	= link_getaddr,
255 	.af_aifaddr	= SIOCSIFLLADDR,
256 	.af_addreq	= &link_ridreq,
257 	.af_exec	= af_exec_ioctl,
258 };
259 
260 static __constructor void
261 link_ctor(void)
262 {
263 	af_register(&af_link);
264 	af_register(&af_ether);
265 	af_register(&af_lladdr);
266 }
267