af_link.c revision 139494
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: head/sbin/ifconfig/af_link.c 139494 2004-12-31 19:46:27Z sam $";
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>
39139494Ssam#include <net/route.h>		/* for RTX_IFA */
40138593Ssam
41138593Ssam#include <err.h>
42138593Ssam#include <stdio.h>
43138593Ssam#include <stdlib.h>
44138593Ssam#include <string.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
55138593Ssamlink_status(int s __unused, const struct rt_addrinfo *info)
56138593Ssam{
57139494Ssam	const struct sockaddr_dl *sdl =
58139494Ssam		(const struct sockaddr_dl *) info->rti_info[RTAX_IFA];
59138593Ssam
60139494Ssam	if (sdl != NULL && sdl->sdl_alen > 0) {
61138593Ssam		if (sdl->sdl_type == IFT_ETHER &&
62138593Ssam		    sdl->sdl_alen == ETHER_ADDR_LEN)
63138593Ssam			printf("\tether %s\n",
64138593Ssam			    ether_ntoa((const struct ether_addr *)LLADDR(sdl)));
65138593Ssam		else {
66138593Ssam			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
67138593Ssam
68138593Ssam			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
69138593Ssam		}
70138593Ssam	}
71138593Ssam}
72138593Ssam
73138593Ssamstatic void
74138593Ssamlink_getaddr(const char *addr, int which)
75138593Ssam{
76138593Ssam	char *temp;
77138593Ssam	struct sockaddr_dl sdl;
78138593Ssam	struct sockaddr *sa = &link_ridreq.ifr_addr;
79138593Ssam
80138593Ssam	if (which != ADDR)
81138593Ssam		errx(1, "can't set link-level netmask or broadcast");
82138593Ssam	if ((temp = malloc(strlen(addr) + 1)) == NULL)
83138593Ssam		errx(1, "malloc failed");
84138593Ssam	temp[0] = ':';
85138593Ssam	strcpy(temp + 1, addr);
86138593Ssam	sdl.sdl_len = sizeof(sdl);
87138593Ssam	link_addr(temp, &sdl);
88138593Ssam	free(temp);
89138593Ssam	if (sdl.sdl_alen > sizeof(sa->sa_data))
90138593Ssam		errx(1, "malformed link-level address");
91138593Ssam	sa->sa_family = AF_LINK;
92138593Ssam	sa->sa_len = sdl.sdl_alen;
93138593Ssam	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
94138593Ssam}
95138593Ssam
96138593Ssamstatic struct afswtch af_link = {
97138593Ssam	.af_name	= "link",
98138593Ssam	.af_af		= AF_LINK,
99138593Ssam	.af_status	= link_status,
100138593Ssam	.af_getaddr	= link_getaddr,
101138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
102138593Ssam	.af_addreq	= &link_ridreq,
103138593Ssam};
104138593Ssamstatic struct afswtch af_ether = {
105138593Ssam	.af_name	= "ether",
106138593Ssam	.af_af		= AF_LINK,
107138593Ssam	.af_status	= link_status,
108138593Ssam	.af_getaddr	= link_getaddr,
109138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
110138593Ssam	.af_addreq	= &link_ridreq,
111138593Ssam};
112138593Ssamstatic struct afswtch af_lladdr = {
113138593Ssam	.af_name	= "lladdr",
114138593Ssam	.af_af		= AF_LINK,
115138593Ssam	.af_status	= link_status,
116138593Ssam	.af_getaddr	= link_getaddr,
117138593Ssam	.af_aifaddr	= SIOCSIFLLADDR,
118138593Ssam	.af_addreq	= &link_ridreq,
119138593Ssam};
120138593Ssam
121138593Ssamstatic __constructor void
122138593Ssamlink_ctor(void)
123138593Ssam{
124138593Ssam	af_register(&af_link);
125138593Ssam	af_register(&af_ether);
126138593Ssam	af_register(&af_lladdr);
127138593Ssam}
128