1138593Ssam/* 2138593Ssam * Copyright (c) 1983, 1993 3138593Ssam * The Regents of the University of California. All rights reserved. 4138593Ssam * 5138593Ssam * Redistribution and use in source and binary forms, with or without 6138593Ssam * modification, are permitted provided that the following conditions 7138593Ssam * are met: 8138593Ssam * 1. Redistributions of source code must retain the above copyright 9138593Ssam * notice, this list of conditions and the following disclaimer. 10138593Ssam * 2. Redistributions in binary form must reproduce the above copyright 11138593Ssam * notice, this list of conditions and the following disclaimer in the 12138593Ssam * documentation and/or other materials provided with the distribution. 13138593Ssam * 4. Neither the name of the University nor the names of its contributors 14138593Ssam * may be used to endorse or promote products derived from this software 15138593Ssam * without specific prior written permission. 16138593Ssam * 17138593Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18138593Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19138593Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20138593Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21138593Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22138593Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23138593Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24138593Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25138593Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26138593Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27138593Ssam * SUCH DAMAGE. 28138593Ssam */ 29138593Ssam 30138593Ssam#ifndef lint 31138593Ssamstatic const char rcsid[] = 32138593Ssam "$FreeBSD: stable/10/sbin/ifconfig/af_link.c 318430 2017-05-17 22:29:25Z rpokala $"; 33138593Ssam#endif /* not lint */ 34138593Ssam 35138593Ssam#include <sys/types.h> 36138593Ssam#include <sys/ioctl.h> 37138593Ssam#include <sys/socket.h> 38138593Ssam#include <net/if.h> 39138593Ssam 40138593Ssam#include <err.h> 41138593Ssam#include <stdio.h> 42138593Ssam#include <stdlib.h> 43138593Ssam#include <string.h> 44166956Ssam#include <ifaddrs.h> 45318430Srpokala#include <unistd.h> 46138593Ssam 47138593Ssam#include <net/if_dl.h> 48138593Ssam#include <net/if_types.h> 49138593Ssam#include <net/ethernet.h> 50138593Ssam 51138593Ssam#include "ifconfig.h" 52138593Ssam 53138593Ssamstatic struct ifreq link_ridreq; 54138593Ssam 55138593Ssamstatic void 56166956Ssamlink_status(int s __unused, const struct ifaddrs *ifa) 57138593Ssam{ 58166956Ssam /* XXX no const 'cuz LLADDR is defined wrong */ 59166956Ssam struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr; 60138593Ssam 61139494Ssam if (sdl != NULL && sdl->sdl_alen > 0) { 62210936Sjhb if ((sdl->sdl_type == IFT_ETHER || 63210936Sjhb sdl->sdl_type == IFT_L2VLAN || 64210936Sjhb sdl->sdl_type == IFT_BRIDGE) && 65138593Ssam sdl->sdl_alen == ETHER_ADDR_LEN) 66138593Ssam printf("\tether %s\n", 67166956Ssam ether_ntoa((struct ether_addr *)LLADDR(sdl))); 68138593Ssam else { 69138593Ssam int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 70138593Ssam 71138593Ssam printf("\tlladdr %s\n", link_ntoa(sdl) + n); 72138593Ssam } 73318430Srpokala /* Best-effort (i.e. failures are silent) to get original 74318430Srpokala * hardware address, as read by NIC driver at attach time. Only 75318430Srpokala * applies to Ethernet NICs (IFT_ETHER). However, laggX 76318430Srpokala * interfaces claim to be IFT_ETHER, and re-type their component 77318430Srpokala * Ethernet NICs as IFT_IEEE8023ADLAG. So, check for both. If 78318430Srpokala * the MAC is zeroed, then it's actually a lagg. 79318430Srpokala */ 80318430Srpokala if ((sdl->sdl_type == IFT_ETHER || 81318430Srpokala sdl->sdl_type == IFT_IEEE8023ADLAG) && 82318430Srpokala sdl->sdl_alen == ETHER_ADDR_LEN) { 83318430Srpokala struct ifreq ifr; 84318430Srpokala int sock_hw; 85318430Srpokala int rc; 86318430Srpokala static const u_char laggaddr[6] = {0}; 87318430Srpokala 88318430Srpokala strncpy(ifr.ifr_name, ifa->ifa_name, 89318430Srpokala sizeof(ifr.ifr_name)); 90318430Srpokala memcpy(&ifr.ifr_addr, ifa->ifa_addr, 91318430Srpokala sizeof(ifa->ifa_addr->sa_len)); 92318430Srpokala ifr.ifr_addr.sa_family = AF_LOCAL; 93318430Srpokala if ((sock_hw = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) { 94318430Srpokala warn("socket(AF_LOCAL,SOCK_DGRAM)"); 95318430Srpokala return; 96318430Srpokala } 97318430Srpokala rc = ioctl(sock_hw, SIOCGHWADDR, &ifr); 98318430Srpokala close(sock_hw); 99318430Srpokala if (rc != 0) { 100318430Srpokala return; 101318430Srpokala } 102318430Srpokala if (memcmp(ifr.ifr_addr.sa_data, laggaddr, sdl->sdl_alen) == 0) { 103318430Srpokala return; 104318430Srpokala } 105318430Srpokala printf("\thwaddr %s\n", ether_ntoa((const struct ether_addr *) 106318430Srpokala &ifr.ifr_addr.sa_data)); 107318430Srpokala } 108138593Ssam } 109138593Ssam} 110138593Ssam 111138593Ssamstatic void 112138593Ssamlink_getaddr(const char *addr, int which) 113138593Ssam{ 114138593Ssam char *temp; 115138593Ssam struct sockaddr_dl sdl; 116138593Ssam struct sockaddr *sa = &link_ridreq.ifr_addr; 117138593Ssam 118138593Ssam if (which != ADDR) 119138593Ssam errx(1, "can't set link-level netmask or broadcast"); 120154401Sru if ((temp = malloc(strlen(addr) + 2)) == NULL) 121138593Ssam errx(1, "malloc failed"); 122138593Ssam temp[0] = ':'; 123138593Ssam strcpy(temp + 1, addr); 124138593Ssam sdl.sdl_len = sizeof(sdl); 125138593Ssam link_addr(temp, &sdl); 126138593Ssam free(temp); 127138593Ssam if (sdl.sdl_alen > sizeof(sa->sa_data)) 128138593Ssam errx(1, "malformed link-level address"); 129138593Ssam sa->sa_family = AF_LINK; 130138593Ssam sa->sa_len = sdl.sdl_alen; 131138593Ssam bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen); 132138593Ssam} 133138593Ssam 134138593Ssamstatic struct afswtch af_link = { 135138593Ssam .af_name = "link", 136138593Ssam .af_af = AF_LINK, 137138593Ssam .af_status = link_status, 138138593Ssam .af_getaddr = link_getaddr, 139138593Ssam .af_aifaddr = SIOCSIFLLADDR, 140138593Ssam .af_addreq = &link_ridreq, 141138593Ssam}; 142138593Ssamstatic struct afswtch af_ether = { 143138593Ssam .af_name = "ether", 144138593Ssam .af_af = AF_LINK, 145138593Ssam .af_status = link_status, 146138593Ssam .af_getaddr = link_getaddr, 147138593Ssam .af_aifaddr = SIOCSIFLLADDR, 148138593Ssam .af_addreq = &link_ridreq, 149138593Ssam}; 150138593Ssamstatic struct afswtch af_lladdr = { 151138593Ssam .af_name = "lladdr", 152138593Ssam .af_af = AF_LINK, 153138593Ssam .af_status = link_status, 154138593Ssam .af_getaddr = link_getaddr, 155138593Ssam .af_aifaddr = SIOCSIFLLADDR, 156138593Ssam .af_addreq = &link_ridreq, 157138593Ssam}; 158138593Ssam 159138593Ssamstatic __constructor void 160138593Ssamlink_ctor(void) 161138593Ssam{ 162138593Ssam af_register(&af_link); 163138593Ssam af_register(&af_ether); 164138593Ssam af_register(&af_lladdr); 165138593Ssam} 166