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#ifndef lint
59static const char rcsid[] =
60  "$FreeBSD: src/sbin/ifconfig/af_link.c,v 1.4.6.1 2008/11/25 02:59:29 kensmith Exp $";
61#endif /* not lint */
62
63#include <sys/types.h>
64#include <sys/ioctl.h>
65#include <sys/socket.h>
66#include <net/if.h>
67
68#include <err.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <ifaddrs.h>
73
74#include <net/if_dl.h>
75#include <net/if_types.h>
76#include <net/ethernet.h>
77
78#include "ifconfig.h"
79
80static struct ifreq link_ridreq;
81
82static void
83link_status(int s __unused, const struct ifaddrs *ifa)
84{
85	/* XXX no const 'cuz LLADDR is defined wrong */
86	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
87
88	if (sdl != NULL && sdl->sdl_alen > 0) {
89#ifdef notyet
90		if (sdl->sdl_type == IFT_ETHER &&
91		    sdl->sdl_alen == ETHER_ADDR_LEN)
92			printf("\tether %s\n",
93			    ether_ntoa((struct ether_addr *)LLADDR(sdl)));
94		else {
95			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
96
97			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
98		}
99#else
100		char *cp = (char *)LLADDR(sdl);
101		int n = sdl->sdl_alen;
102
103		if (sdl->sdl_type == IFT_ETHER)
104			printf ("\tether ");
105		else
106			printf ("\tlladdr ");
107		while (--n >= 0)
108			printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
109		putchar('\n');
110#endif
111	}
112}
113
114static void
115link_getaddr(const char *addr, int which)
116{
117	char *temp;
118	struct sockaddr_dl sdl;
119	struct sockaddr *sa = &link_ridreq.ifr_addr;
120	size_t slen = strlen(addr);
121
122	if (which != ADDR)
123		errx(1, "can't set link-level netmask or broadcast");
124	if ((temp = malloc(slen + 2)) == NULL)
125		errx(1, "malloc failed");
126	temp[0] = ':';
127	strlcpy(temp + 1, addr, slen + 1);
128	sdl.sdl_len = sizeof(sdl);
129	link_addr(temp, &sdl);
130	free(temp);
131	if (sdl.sdl_alen > sizeof(sa->sa_data))
132		errx(1, "malformed link-level address");
133	sa->sa_family = AF_LINK;
134	sa->sa_len = sdl.sdl_alen;
135	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
136}
137
138static struct afswtch af_link = {
139	.af_name	= "link",
140	.af_af		= AF_LINK,
141	.af_status	= link_status,
142	.af_getaddr	= link_getaddr,
143	.af_aifaddr	= SIOCSIFLLADDR,
144	.af_addreq	= &link_ridreq,
145};
146static struct afswtch af_ether = {
147	.af_name	= "ether",
148	.af_af		= AF_LINK,
149	.af_status	= link_status,
150	.af_getaddr	= link_getaddr,
151	.af_aifaddr	= SIOCSIFLLADDR,
152	.af_addreq	= &link_ridreq,
153};
154static struct afswtch af_lladdr = {
155	.af_name	= "lladdr",
156	.af_af		= AF_LINK,
157	.af_status	= link_status,
158	.af_getaddr	= link_getaddr,
159	.af_aifaddr	= SIOCSIFLLADDR,
160	.af_addreq	= &link_ridreq,
161};
162
163static __constructor void
164link_ctor(void)
165{
166	af_register(&af_link);
167	af_register(&af_ether);
168	af_register(&af_lladdr);
169}
170