xref: /freebsd/sbin/ifconfig/af_link.c (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
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) {
66 		if (strcmp(f_ether, "dash") == 0) {
67 			char *format_char;
68 
69 			while ((format_char = strchr(ether_format, ':')) != NULL) {
70 				*format_char = '-';
71 			}
72 		} else if (strcmp(f_ether, "dotted") == 0) {
73 			/* Indices 0 and 1 is kept as is. */
74 			ether_format[ 2] = ether_format[ 3];
75 			ether_format[ 3] = ether_format[ 4];
76 			ether_format[ 4] = '.';
77 			ether_format[ 5] = ether_format[ 6];
78 			ether_format[ 6] = ether_format[ 7];
79 			ether_format[ 7] = ether_format[ 9];
80 			ether_format[ 8] = ether_format[10];
81 			ether_format[ 9] = '.';
82 			ether_format[10] = ether_format[12];
83 			ether_format[11] = ether_format[13];
84 			ether_format[12] = ether_format[15];
85 			ether_format[13] = ether_format[16];
86 			ether_format[14] = '\0';
87 		}
88 	}
89 	printf("\t%s %s\n", prefix, ether_format);
90 }
91 
92 static void
93 print_lladdr(struct sockaddr_dl *sdl)
94 {
95 	if (match_ether(sdl)) {
96 		print_ether((struct ether_addr *)LLADDR(sdl), "ether");
97 	} else {
98 		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
99 		printf("\tlladdr %s\n", link_ntoa(sdl) + n);
100 	}
101 }
102 
103 static void
104 print_pcp(int s)
105 {
106 	if (ioctl(s, SIOCGLANPCP, (caddr_t)&ifr) == 0 &&
107 	    ifr.ifr_lan_pcp != IFNET_PCP_NONE)
108 		printf("\tpcp %d\n", ifr.ifr_lan_pcp);
109 }
110 
111 #ifdef WITHOUT_NETLINK
112 static void
113 link_status(if_ctx *ctx, const struct ifaddrs *ifa)
114 {
115 	/* XXX no const 'cuz LLADDR is defined wrong */
116 	struct sockaddr_dl *sdl;
117 	struct ifreq ifr;
118 	int rc, sock_hw;
119 	static const u_char laggaddr[6] = {0};
120 
121 	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
122 	if (sdl == NULL || sdl->sdl_alen == 0)
123 		return;
124 
125 	print_lladdr(sdl);
126 
127 	/*
128 	 * Best-effort (i.e. failures are silent) to get original
129 	 * hardware address, as read by NIC driver at attach time. Only
130 	 * applies to Ethernet NICs (IFT_ETHER). However, laggX
131 	 * interfaces claim to be IFT_ETHER, and re-type their component
132 	 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
133 	 * the MAC is zeroed, then it's actually a lagg.
134 	 */
135 	if ((sdl->sdl_type != IFT_ETHER &&
136 	    sdl->sdl_type != IFT_IEEE8023ADLAG) ||
137 	    sdl->sdl_alen != ETHER_ADDR_LEN)
138 		return;
139 
140 	strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
141 	memcpy(&ifr.ifr_addr, ifa->ifa_addr, sizeof(ifa->ifa_addr->sa_len));
142 	ifr.ifr_addr.sa_family = AF_LOCAL;
143 	if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
144 		warn("socket(AF_LOCAL,SOCK_DGRAM)");
145 		return;
146 	}
147 	rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
148 	close(sock_hw);
149 	if (rc != 0)
150 		return;
151 
152 	/*
153 	 * If this is definitely a lagg device or the hwaddr
154 	 * matches the link addr, don't bother.
155 	 */
156 	if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0 ||
157 	    memcmp(ifr.ifr_addr.sa_data, LLADDR(sdl), sdl->sdl_alen) == 0)
158 		goto pcp;
159 
160 	print_ether((const struct ether_addr *)&ifr.ifr_addr.sa_data, "hwaddr");
161 pcp:
162 	print_pcp(ctx->io_s);
163 }
164 
165 #else
166 static uint8_t
167 convert_iftype(uint8_t iftype)
168 {
169 	switch (iftype) {
170 	case IFT_IEEE8023ADLAG:
171 		return (IFT_ETHER);
172 	case IFT_INFINIBANDLAG:
173 		return (IFT_INFINIBAND);
174 	}
175 	return (iftype);
176 }
177 
178 static void
179 link_status_nl(if_ctx *ctx, if_link_t *link, if_addr_t *ifa __unused)
180 {
181 	if (link->ifla_address != NULL) {
182 		struct sockaddr_dl sdl = {
183 			.sdl_len = sizeof(struct sockaddr_dl),
184 			.sdl_family = AF_LINK,
185 			.sdl_type = convert_iftype(link->ifi_type),
186 			.sdl_alen = NLA_DATA_LEN(link->ifla_address),
187 		};
188 		memcpy(LLADDR(&sdl), NLA_DATA(link->ifla_address), sdl.sdl_alen);
189 		print_lladdr(&sdl);
190 
191 		if (link->iflaf_orig_hwaddr != NULL) {
192 			struct nlattr *hwaddr = link->iflaf_orig_hwaddr;
193 
194 			if (memcmp(NLA_DATA(hwaddr), NLA_DATA(link->ifla_address), sdl.sdl_alen))
195 				print_ether((struct ether_addr *)NLA_DATA(hwaddr), "hwaddr");
196 		}
197 	}
198 	if (convert_iftype(link->ifi_type) == IFT_ETHER)
199 		print_pcp(ctx->io_s);
200 }
201 #endif
202 
203 static void
204 link_getaddr(const char *addr, int which)
205 {
206 	char *temp;
207 	struct sockaddr_dl sdl;
208 	struct sockaddr *sa = &link_ridreq.ifr_addr;
209 
210 	if (which != ADDR)
211 		errx(1, "can't set link-level netmask or broadcast");
212 	if (!strcmp(addr, "random")) {
213 		sdl.sdl_len = sizeof(sdl);
214 		sdl.sdl_alen = ETHER_ADDR_LEN;
215 		sdl.sdl_nlen = 0;
216 		sdl.sdl_family = AF_LINK;
217 		arc4random_buf(&sdl.sdl_data, ETHER_ADDR_LEN);
218 		/* Non-multicast and claim it is locally administered. */
219 		sdl.sdl_data[0] &= 0xfc;
220 		sdl.sdl_data[0] |= 0x02;
221 	} else {
222 		if ((temp = malloc(strlen(addr) + 2)) == NULL)
223 			errx(1, "malloc failed");
224 		temp[0] = ':';
225 		strcpy(temp + 1, addr);
226 		sdl.sdl_len = sizeof(sdl);
227 		link_addr(temp, &sdl);
228 		free(temp);
229 	}
230 	if (sdl.sdl_alen > sizeof(sa->sa_data))
231 		errx(1, "malformed link-level address");
232 	sa->sa_family = AF_LINK;
233 	sa->sa_len = sdl.sdl_alen;
234 	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
235 }
236 
237 static struct afswtch af_link = {
238 	.af_name	= "link",
239 	.af_af		= AF_LINK,
240 #ifdef WITHOUT_NETLINK
241 	.af_status	= link_status,
242 #else
243 	.af_status	= link_status_nl,
244 #endif
245 	.af_getaddr	= link_getaddr,
246 	.af_aifaddr	= SIOCSIFLLADDR,
247 	.af_addreq	= &link_ridreq,
248 	.af_exec	= af_exec_ioctl,
249 };
250 static struct afswtch af_ether = {
251 	.af_name	= "ether",
252 	.af_af		= AF_LINK,
253 #ifdef WITHOUT_NETLINK
254 	.af_status	= link_status,
255 #else
256 	.af_status	= link_status_nl,
257 #endif
258 	.af_getaddr	= link_getaddr,
259 	.af_aifaddr	= SIOCSIFLLADDR,
260 	.af_addreq	= &link_ridreq,
261 	.af_exec	= af_exec_ioctl,
262 };
263 static struct afswtch af_lladdr = {
264 	.af_name	= "lladdr",
265 	.af_af		= AF_LINK,
266 #ifdef WITHOUT_NETLINK
267 	.af_status	= link_status,
268 #else
269 	.af_status	= link_status_nl,
270 #endif
271 	.af_getaddr	= link_getaddr,
272 	.af_aifaddr	= SIOCSIFLLADDR,
273 	.af_addreq	= &link_ridreq,
274 	.af_exec	= af_exec_ioctl,
275 };
276 
277 static __constructor void
278 link_ctor(void)
279 {
280 	af_register(&af_link);
281 	af_register(&af_ether);
282 	af_register(&af_lladdr);
283 }
284