]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_link.c
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
59 static const char rcsid
[] =
60 "$FreeBSD: src/sbin/ifconfig/af_link.c,v 1.4.6.1 2008/11/25 02:59:29 kensmith Exp $";
63 #include <sys/types.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/ethernet.h>
80 static struct ifreq link_ridreq
;
83 link_status(int s __unused
, const struct ifaddrs
*ifa
)
85 /* XXX no const 'cuz LLADDR is defined wrong */
86 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) ifa
->ifa_addr
;
88 if (sdl
!= NULL
&& sdl
->sdl_alen
> 0) {
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
)));
95 int n
= sdl
->sdl_nlen
> 0 ? sdl
->sdl_nlen
+ 1 : 0;
97 printf("\tlladdr %s\n", link_ntoa(sdl
) + n
);
100 char *cp
= (char *)LLADDR(sdl
);
101 int n
= sdl
->sdl_alen
;
103 if (sdl
->sdl_type
== IFT_ETHER
)
106 printf ("\tlladdr ");
108 printf("%02x%c",*cp
++ & 0xff, n
>0? ':' : ' ');
115 link_getaddr(const char *addr
, int which
)
118 struct sockaddr_dl sdl
;
119 struct sockaddr
*sa
= &link_ridreq
.ifr_addr
;
120 size_t slen
= strlen(addr
);
123 errx(1, "can't set link-level netmask or broadcast");
124 if ((temp
= malloc(slen
+ 2)) == NULL
)
125 errx(1, "malloc failed");
127 strlcpy(temp
+ 1, addr
, slen
+ 1);
128 sdl
.sdl_len
= sizeof(sdl
);
129 link_addr(temp
, &sdl
);
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
);
138 static struct afswtch af_link
= {
141 .af_status
= link_status
,
142 .af_getaddr
= link_getaddr
,
143 .af_aifaddr
= SIOCSIFLLADDR
,
144 .af_addreq
= &link_ridreq
,
146 static struct afswtch af_ether
= {
149 .af_status
= link_status
,
150 .af_getaddr
= link_getaddr
,
151 .af_aifaddr
= SIOCSIFLLADDR
,
152 .af_addreq
= &link_ridreq
,
154 static struct afswtch af_lladdr
= {
157 .af_status
= link_status
,
158 .af_getaddr
= link_getaddr
,
159 .af_aifaddr
= SIOCSIFLLADDR
,
160 .af_addreq
= &link_ridreq
,
163 static __constructor
void
166 af_register(&af_link
);
167 af_register(&af_ether
);
168 af_register(&af_lladdr
);