xref: /dragonfly/sbin/ifconfig/af_link.c (revision 0989c651)
1ca74a0a2SSepherosa Ziehau /*
2ca74a0a2SSepherosa Ziehau  * Copyright (c) 1983, 1993
3ca74a0a2SSepherosa Ziehau  *	The Regents of the University of California.  All rights reserved.
4ca74a0a2SSepherosa Ziehau  *
5ca74a0a2SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
6ca74a0a2SSepherosa Ziehau  * modification, are permitted provided that the following conditions
7ca74a0a2SSepherosa Ziehau  * are met:
8ca74a0a2SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
9ca74a0a2SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
10ca74a0a2SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
11ca74a0a2SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in the
12ca74a0a2SSepherosa Ziehau  *    documentation and/or other materials provided with the distribution.
13dc71b7abSJustin C. Sherrill  * 3. Neither the name of the University nor the names of its contributors
14ca74a0a2SSepherosa Ziehau  *    may be used to endorse or promote products derived from this software
15ca74a0a2SSepherosa Ziehau  *    without specific prior written permission.
16ca74a0a2SSepherosa Ziehau  *
17ca74a0a2SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18ca74a0a2SSepherosa Ziehau  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ca74a0a2SSepherosa Ziehau  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ca74a0a2SSepherosa Ziehau  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21ca74a0a2SSepherosa Ziehau  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ca74a0a2SSepherosa Ziehau  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ca74a0a2SSepherosa Ziehau  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ca74a0a2SSepherosa Ziehau  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ca74a0a2SSepherosa Ziehau  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ca74a0a2SSepherosa Ziehau  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ca74a0a2SSepherosa Ziehau  * SUCH DAMAGE.
28ca74a0a2SSepherosa Ziehau  *
29ca74a0a2SSepherosa Ziehau  * $FreeBSD: src/sbin/ifconfig/af_link.c,v 1.2 2004/12/31 19:46:27 sam Exp $
30ca74a0a2SSepherosa Ziehau  */
31ca74a0a2SSepherosa Ziehau 
32ca74a0a2SSepherosa Ziehau #include <sys/types.h>
33ca74a0a2SSepherosa Ziehau #include <sys/ioctl.h>
34ca74a0a2SSepherosa Ziehau #include <net/if.h>
3546158ff5SAaron LI #include <net/if_dl.h>
3646158ff5SAaron LI #include <net/if_types.h>
3746158ff5SAaron LI #include <net/ethernet.h>
38ca74a0a2SSepherosa Ziehau 
39ca74a0a2SSepherosa Ziehau #include <err.h>
405ecbd701SAaron LI #include <ifaddrs.h>
41ca74a0a2SSepherosa Ziehau #include <stdio.h>
42ca74a0a2SSepherosa Ziehau #include <stdlib.h>
43ca74a0a2SSepherosa Ziehau #include <string.h>
44ca74a0a2SSepherosa Ziehau 
45ca74a0a2SSepherosa Ziehau #include "ifconfig.h"
46ca74a0a2SSepherosa Ziehau 
47ca74a0a2SSepherosa Ziehau static struct ifreq link_ridreq;
48ca74a0a2SSepherosa Ziehau 
49ca74a0a2SSepherosa Ziehau static void
link_status(int s __unused,const struct ifaddrs * ifa)505ecbd701SAaron LI link_status(int s __unused, const struct ifaddrs *ifa)
51ca74a0a2SSepherosa Ziehau {
520c6980e1SAaron LI 	struct sockaddr_dl *sdl =
530c6980e1SAaron LI 		__DECONST(struct sockaddr_dl *, ifa->ifa_addr);
540c6980e1SAaron LI 		/* drop 'const' qualifier for LLADDR() to suppress warning */
550c6980e1SAaron LI 	char addr[18], *p;
560c6980e1SAaron LI 	int n;
57ca74a0a2SSepherosa Ziehau 
580c6980e1SAaron LI 	if (sdl == NULL || sdl->sdl_alen == 0)
590c6980e1SAaron LI 		return;
6051a3d09eSAaron LI 
610c6980e1SAaron LI 	if (sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == ETHER_ADDR_LEN) {
620c6980e1SAaron LI 		ether_ntoa_r((const struct ether_addr *)LLADDR(sdl), addr);
6351a3d09eSAaron LI 		if (f_ether != NULL && strcmp(f_ether, "dash") == 0) {
640c6980e1SAaron LI 			p = addr;
650c6980e1SAaron LI 			while ((p = strchr(p, ':')) != NULL)
660c6980e1SAaron LI 				*p = '-';
6751a3d09eSAaron LI 		}
680c6980e1SAaron LI 		printf("\tether %s\n", addr);
6951a3d09eSAaron LI 	} else {
700c6980e1SAaron LI 		n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
71ca74a0a2SSepherosa Ziehau 		printf("\tlladdr %s\n", link_ntoa(sdl) + n);
72ca74a0a2SSepherosa Ziehau 	}
73ca74a0a2SSepherosa Ziehau }
74ca74a0a2SSepherosa Ziehau 
75ca74a0a2SSepherosa Ziehau static void
link_getaddr(const char * addr,int which)76ca74a0a2SSepherosa Ziehau link_getaddr(const char *addr, int which)
77ca74a0a2SSepherosa Ziehau {
78ca74a0a2SSepherosa Ziehau 	struct sockaddr_dl sdl;
79ca74a0a2SSepherosa Ziehau 	struct sockaddr *sa = &link_ridreq.ifr_addr;
8093b0f758SAaron LI 	char *temp;
81ca74a0a2SSepherosa Ziehau 
82ca74a0a2SSepherosa Ziehau 	if (which != ADDR)
83ca74a0a2SSepherosa Ziehau 		errx(1, "can't set link-level netmask or broadcast");
84b1359e8eSSepherosa Ziehau 	if ((temp = malloc(strlen(addr) + 2)) == NULL)
85ca74a0a2SSepherosa Ziehau 		errx(1, "malloc failed");
86ca74a0a2SSepherosa Ziehau 	temp[0] = ':';
87ca74a0a2SSepherosa Ziehau 	strcpy(temp + 1, addr);
88ca74a0a2SSepherosa Ziehau 	sdl.sdl_len = sizeof(sdl);
89ca74a0a2SSepherosa Ziehau 	link_addr(temp, &sdl);
90ca74a0a2SSepherosa Ziehau 	free(temp);
91ca74a0a2SSepherosa Ziehau 	if (sdl.sdl_alen > sizeof(sa->sa_data))
92ca74a0a2SSepherosa Ziehau 		errx(1, "malformed link-level address");
93ca74a0a2SSepherosa Ziehau 	sa->sa_family = AF_LINK;
94ca74a0a2SSepherosa Ziehau 	sa->sa_len = sdl.sdl_alen;
95*0989c651SAaron LI 	memcpy(sa->sa_data, LLADDR(&sdl), sdl.sdl_alen);
96ca74a0a2SSepherosa Ziehau }
97ca74a0a2SSepherosa Ziehau 
98ca74a0a2SSepherosa Ziehau static struct afswtch af_link = {
99ca74a0a2SSepherosa Ziehau 	.af_name	= "link",
100ca74a0a2SSepherosa Ziehau 	.af_af		= AF_LINK,
101ca74a0a2SSepherosa Ziehau 	.af_status	= link_status,
102ca74a0a2SSepherosa Ziehau 	.af_getaddr	= link_getaddr,
103ca74a0a2SSepherosa Ziehau 	.af_aifaddr	= SIOCSIFLLADDR,
104ca74a0a2SSepherosa Ziehau 	.af_addreq	= &link_ridreq,
105ca74a0a2SSepherosa Ziehau };
106ca74a0a2SSepherosa Ziehau static struct afswtch af_ether = {
107ca74a0a2SSepherosa Ziehau 	.af_name	= "ether",
108ca74a0a2SSepherosa Ziehau 	.af_af		= AF_LINK,
109ca74a0a2SSepherosa Ziehau 	.af_status	= link_status,
110ca74a0a2SSepherosa Ziehau 	.af_getaddr	= link_getaddr,
111ca74a0a2SSepherosa Ziehau 	.af_aifaddr	= SIOCSIFLLADDR,
112ca74a0a2SSepherosa Ziehau 	.af_addreq	= &link_ridreq,
113ca74a0a2SSepherosa Ziehau };
114ca74a0a2SSepherosa Ziehau static struct afswtch af_lladdr = {
115ca74a0a2SSepherosa Ziehau 	.af_name	= "lladdr",
116ca74a0a2SSepherosa Ziehau 	.af_af		= AF_LINK,
117ca74a0a2SSepherosa Ziehau 	.af_status	= link_status,
118ca74a0a2SSepherosa Ziehau 	.af_getaddr	= link_getaddr,
119ca74a0a2SSepherosa Ziehau 	.af_aifaddr	= SIOCSIFLLADDR,
120ca74a0a2SSepherosa Ziehau 	.af_addreq	= &link_ridreq,
121ca74a0a2SSepherosa Ziehau };
122ca74a0a2SSepherosa Ziehau 
123ec7e0eebSAaron LI __constructor(111)
124ec7e0eebSAaron LI static void
link_ctor(void)125ca74a0a2SSepherosa Ziehau link_ctor(void)
126ca74a0a2SSepherosa Ziehau {
127ca74a0a2SSepherosa Ziehau 	af_register(&af_link);
128ca74a0a2SSepherosa Ziehau 	af_register(&af_ether);
129ca74a0a2SSepherosa Ziehau 	af_register(&af_lladdr);
130ca74a0a2SSepherosa Ziehau }
131