1/* 2 * Copyright (c) 2009 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29/* 30 * Copyright (c) 1983, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 4. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58#include <sys/types.h> 59#include <sys/ioctl.h> 60#include <sys/socket.h> 61#include <net/if.h> 62 63#include <err.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#include <ifaddrs.h> 68 69#include <net/if_dl.h> 70#include <net/if_types.h> 71#include <net/ethernet.h> 72 73#include "ifconfig.h" 74 75static struct ifreq link_ridreq; 76 77static void 78link_status(int s __unused, const struct ifaddrs *ifa) 79{ 80 /* XXX no const 'cuz LLADDR is defined wrong */ 81 struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr; 82 83 if (sdl != NULL && sdl->sdl_alen > 0) { 84#ifdef notyet 85 if (sdl->sdl_type == IFT_ETHER && 86 sdl->sdl_alen == ETHER_ADDR_LEN) 87 printf("\tether %s\n", 88 ether_ntoa((struct ether_addr *)LLADDR(sdl))); 89 else { 90 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 91 92 printf("\tlladdr %s\n", link_ntoa(sdl) + n); 93 } 94#else 95 char *cp = (char *)LLADDR(sdl); 96 int n = sdl->sdl_alen; 97 98 if (sdl->sdl_type == IFT_ETHER) 99 printf ("\tether "); 100 else 101 printf ("\tlladdr "); 102 while (--n >= 0) 103 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' '); 104 putchar('\n'); 105#endif 106 } 107} 108 109static void 110link_getaddr(const char *addr, int which) 111{ 112 char *temp; 113 struct sockaddr_dl sdl; 114 struct sockaddr *sa = &link_ridreq.ifr_addr; 115 size_t slen = strlen(addr); 116 117 if (which != ADDR) 118 errx(1, "can't set link-level netmask or broadcast"); 119 if ((temp = malloc(slen + 2)) == NULL) 120 errx(1, "malloc failed"); 121 temp[0] = ':'; 122 strlcpy(temp + 1, addr, slen + 1); 123 sdl.sdl_len = sizeof(sdl); 124 link_addr(temp, &sdl); 125 free(temp); 126 if (sdl.sdl_alen > sizeof(sa->sa_data)) 127 errx(1, "malformed link-level address"); 128 sa->sa_family = AF_LINK; 129 sa->sa_len = sdl.sdl_alen; 130 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen); 131} 132 133static struct afswtch af_link = { 134 .af_name = "link", 135 .af_af = AF_LINK, 136 .af_status = link_status, 137 .af_getaddr = link_getaddr, 138 .af_aifaddr = SIOCSIFLLADDR, 139 .af_addreq = &link_ridreq, 140}; 141static struct afswtch af_ether = { 142 .af_name = "ether", 143 .af_af = AF_LINK, 144 .af_status = link_status, 145 .af_getaddr = link_getaddr, 146 .af_aifaddr = SIOCSIFLLADDR, 147 .af_addreq = &link_ridreq, 148}; 149static struct afswtch af_lladdr = { 150 .af_name = "lladdr", 151 .af_af = AF_LINK, 152 .af_status = link_status, 153 .af_getaddr = link_getaddr, 154 .af_aifaddr = SIOCSIFLLADDR, 155 .af_addreq = &link_ridreq, 156}; 157 158static __constructor void 159link_ctor(void) 160{ 161 af_register(&af_link); 162 af_register(&af_ether); 163 af_register(&af_lladdr); 164} 165