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