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$";
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>
45138593Ssam
46138593Ssam#include <net/if_dl.h>
47138593Ssam#include <net/if_types.h>
48138593Ssam#include <net/ethernet.h>
49138593Ssam
50138593Ssam#include "ifconfig.h"
51138593Ssam
52138593Ssamstatic struct ifreq link_ridreq;
53138593Ssam
54138593Ssamstatic void
55166956Ssamlink_status(int s __unused, const struct ifaddrs *ifa)
56138593Ssam{
57166956Ssam	/* XXX no const 'cuz LLADDR is defined wrong */
58166956Ssam	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;
59138593Ssam
60139494Ssam	if (sdl != NULL && sdl->sdl_alen > 0) {
61210936Sjhb		if ((sdl->sdl_type == IFT_ETHER ||
62210936Sjhb		    sdl->sdl_type == IFT_L2VLAN ||
63210936Sjhb		    sdl->sdl_type == IFT_BRIDGE) &&
64138593Ssam		    sdl->sdl_alen == ETHER_ADDR_LEN)
65138593Ssam			printf("\tether %s\n",
66166956Ssam			    ether_ntoa((struct ether_addr *)LLADDR(sdl)));
67138593Ssam		else {
68138593Ssam			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
69138593Ssam
70138593Ssam			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
71138593Ssam		}
72138593Ssam	}
73138593Ssam}
74138593Ssam
75138593Ssamstatic void
76138593Ssamlink_getaddr(const char *addr, int which)
77138593Ssam{
78138593Ssam	char *temp;
79138593Ssam	struct sockaddr_dl sdl;
80138593Ssam	struct sockaddr *sa = &link_ridreq.ifr_addr;
81138593Ssam
82138593Ssam	if (which != ADDR)
83138593Ssam		errx(1, "can't set link-level netmask or broadcast");
84154401Sru	if ((temp = malloc(strlen(addr) + 2)) == NULL)
85138593Ssam		errx(1, "malloc failed");
86138593Ssam	temp[0] = ':';
87138593Ssam	strcpy(temp + 1, addr);
88138593Ssam	sdl.sdl_len = sizeof(sdl);
89138593Ssam	link_addr(temp, &sdl);
90138593Ssam	free(temp);
91138593Ssam	if (sdl.sdl_alen > sizeof(sa->sa_data))
92138593Ssam		errx(1, "malformed link-level address");
93138593Ssam	sa->sa_family = AF_LINK;
94138593Ssam	sa->sa_len = sdl.sdl_alen;
95138593Ssam	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
96138593Ssam}
97138593Ssam
98138593Ssamstatic struct afswtch af_link = {
99138593Ssam	.af_name	= "link",
100138593Ssam	.af_af		= AF_LINK,
101138593Ssam	.af_status	= link_status,
102138593Ssam	.af_getaddr	= link_getaddr,
103138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
104138593Ssam	.af_addreq	= &link_ridreq,
105138593Ssam};
106138593Ssamstatic struct afswtch af_ether = {
107138593Ssam	.af_name	= "ether",
108138593Ssam	.af_af		= AF_LINK,
109138593Ssam	.af_status	= link_status,
110138593Ssam	.af_getaddr	= link_getaddr,
111138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
112138593Ssam	.af_addreq	= &link_ridreq,
113138593Ssam};
114138593Ssamstatic struct afswtch af_lladdr = {
115138593Ssam	.af_name	= "lladdr",
116138593Ssam	.af_af		= AF_LINK,
117138593Ssam	.af_status	= link_status,
118138593Ssam	.af_getaddr	= link_getaddr,
119138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
120138593Ssam	.af_addreq	= &link_ridreq,
121138593Ssam};
122138593Ssam
123138593Ssamstatic __constructor void
124138593Ssamlink_ctor(void)
125138593Ssam{
126138593Ssam	af_register(&af_link);
127138593Ssam	af_register(&af_ether);
128138593Ssam	af_register(&af_lladdr);
129138593Ssam}
130