xref: /freebsd/sbin/ifconfig/af_link.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
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 
55 static struct ifreq link_ridreq;
56 
57 extern char *f_ether;
58 
59 static void
60 link_status(int s __unused, const struct ifaddrs *ifa)
61 {
62 	/* XXX no const 'cuz LLADDR is defined wrong */
63 	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
64 	char *ether_format, *format_char;
65 
66 	if (sdl != NULL && sdl->sdl_alen > 0) {
67 		if ((sdl->sdl_type == IFT_ETHER ||
68 		    sdl->sdl_type == IFT_L2VLAN ||
69 		    sdl->sdl_type == IFT_BRIDGE) &&
70 		    sdl->sdl_alen == ETHER_ADDR_LEN) {
71 			ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl));
72 			if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
73 				for (format_char = strchr(ether_format, ':');
74 				    format_char != NULL;
75 				    format_char = strchr(ether_format, ':'))
76 					*format_char = '-';
77 			}
78 			printf("\tether %s\n", ether_format);
79 		} else {
80 			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
81 
82 			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
83 		}
84 		/* Best-effort (i.e. failures are silent) to get original
85 		 * hardware address, as read by NIC driver at attach time. Only
86 		 * applies to Ethernet NICs (IFT_ETHER). However, laggX
87 		 * interfaces claim to be IFT_ETHER, and re-type their component
88 		 * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If
89 		 * the MAC is zeroed, then it's actually a lagg.
90 		 */
91 		if ((sdl->sdl_type == IFT_ETHER ||
92 		    sdl->sdl_type == IFT_IEEE8023ADLAG) &&
93 		    sdl->sdl_alen == ETHER_ADDR_LEN) {
94 			struct ifreq ifr;
95 			int sock_hw;
96 			int rc;
97 			static const u_char laggaddr[6] = {0};
98 
99 			strncpy(ifr.ifr_name, ifa->ifa_name,
100 			    sizeof(ifr.ifr_name));
101 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
102 			    sizeof(ifa->ifa_addr->sa_len));
103 			ifr.ifr_addr.sa_family = AF_LOCAL;
104 			if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) {
105 				warn("socket(AF_LOCAL,SOCK_DGRAM)");
106 				return;
107 			}
108 			rc = ioctl(sock_hw, SIOCGHWADDR, &ifr);
109 			close(sock_hw);
110 			if (rc != 0) {
111 				return;
112 			}
113 
114 			/*
115 			 * If this is definitely a lagg device or the hwaddr
116 			 * matches the link addr, don't bother.
117 			 */
118 			if (memcmp(ifr.ifr_addr.sa_data, laggaddr,
119 			    sdl->sdl_alen) == 0 ||
120 			    memcmp(ifr.ifr_addr.sa_data, LLADDR(sdl),
121 			    sdl->sdl_alen) == 0) {
122 				return;
123 			}
124 			ether_format = ether_ntoa((const struct ether_addr *)
125 			    &ifr.ifr_addr.sa_data);
126 			if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
127 				for (format_char = strchr(ether_format, ':');
128 				    format_char != NULL;
129 				    format_char = strchr(ether_format, ':'))
130 					*format_char = '-';
131 			}
132 			printf("\thwaddr %s\n", ether_format);
133 		}
134 	}
135 }
136 
137 static void
138 link_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 
171 static 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 };
179 static 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 };
187 static 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 
196 static __constructor void
197 link_ctor(void)
198 {
199 	af_register(&af_link);
200 	af_register(&af_ether);
201 	af_register(&af_lladdr);
202 }
203