]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_inet.c
2 * Copyright (c) 2009-2011, 2020 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
58 #include <sys/types.h>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <net/if_var.h> /* for struct ifaddr */
72 #include <netinet/in_var.h>
73 #include <arpa/inet.h>
78 static struct ifaliasreq in_addreq
;
79 static struct ifreq in_ridreq
;
82 in_status(int s __unused
, const struct ifaddrs
*ifa
)
84 struct sockaddr_in
*sin
, null_sin
;
86 memset(&null_sin
, 0, sizeof(null_sin
));
88 sin
= (struct sockaddr_in
*)ifa
->ifa_addr
;
92 printf("\tinet %s ", inet_ntoa(sin
->sin_addr
));
94 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
95 sin
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
98 printf("--> %s ", inet_ntoa(sin
->sin_addr
));
101 sin
= (struct sockaddr_in
*)ifa
->ifa_netmask
;
104 printf("netmask 0x%lx ", (unsigned long)ntohl(sin
->sin_addr
.s_addr
));
106 if (ifa
->ifa_flags
& IFF_BROADCAST
) {
107 sin
= (struct sockaddr_in
*)ifa
->ifa_broadaddr
;
108 if (sin
!= NULL
&& sin
->sin_addr
.s_addr
!= 0)
109 printf("broadcast %s", inet_ntoa(sin
->sin_addr
));
114 #define SIN(x) ((struct sockaddr_in *) &(x))
115 static struct sockaddr_in
*sintab
[] = {
116 SIN(in_ridreq
.ifr_addr
), SIN(in_addreq
.ifra_addr
),
117 SIN(in_addreq
.ifra_mask
), SIN(in_addreq
.ifra_broadaddr
)
121 in_getaddr(const char *s
, int which
)
124 #define MIN(a,b) ((a)<(b)?(a):(b))
126 struct sockaddr_in
*sin
= sintab
[which
];
130 sin
->sin_len
= sizeof(*sin
);
132 sin
->sin_family
= AF_INET
;
137 if((p
= strrchr(s
, '/')) != NULL
) {
138 /* address is `name/masklen' */
141 struct sockaddr_in
*min
= sintab
[MASK
];
143 ret
= sscanf(p
+1, "%u", &masklen
);
144 if(ret
!= 1 || (masklen
< 0 || masklen
> 32)) {
146 errx(1, "%s: bad value", s
);
148 min
->sin_len
= sizeof(*min
);
149 min
->sin_addr
.s_addr
= htonl(~((1LL << (32 - masklen
)) - 1) &
154 if (inet_aton(s
, &sin
->sin_addr
))
156 if ((hp
= gethostbyname(s
)) != 0)
157 bcopy(hp
->h_addr
, (char *)&sin
->sin_addr
,
158 MIN(hp
->h_length
, sizeof(sin
->sin_addr
)));
159 else if ((np
= getnetbyname(s
)) != 0)
160 sin
->sin_addr
= inet_makeaddr(np
->n_net
, INADDR_ANY
);
162 errx(1, "%s: bad value", s
);
167 in_status_tunnel(int s
)
169 char src
[NI_MAXHOST
];
170 char dst
[NI_MAXHOST
];
172 const struct sockaddr
*sa
= (const struct sockaddr
*) &ifr
.ifr_addr
;
174 memset(&ifr
, 0, sizeof(ifr
));
175 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
177 if (ioctl(s
, SIOCGIFPSRCADDR
, (caddr_t
)&ifr
) < 0)
179 if (sa
->sa_family
!= AF_INET
)
181 if (getnameinfo(sa
, sa
->sa_len
, src
, sizeof(src
), 0, 0, NI_NUMERICHOST
) != 0)
184 if (ioctl(s
, SIOCGIFPDSTADDR
, (caddr_t
)&ifr
) < 0)
186 if (sa
->sa_family
!= AF_INET
)
188 if (getnameinfo(sa
, sa
->sa_len
, dst
, sizeof(dst
), 0, 0, NI_NUMERICHOST
) != 0)
191 printf("\ttunnel inet %s --> %s\n", src
, dst
);
195 in_set_tunnel(int s
, struct addrinfo
*srcres
, struct addrinfo
*dstres
)
197 struct ifaliasreq addreq
;
199 memset(&addreq
, 0, sizeof(addreq
));
200 strlcpy(addreq
.ifra_name
, name
, sizeof(addreq
.ifra_name
));
201 memcpy(&addreq
.ifra_addr
, srcres
->ai_addr
, srcres
->ai_addr
->sa_len
);
202 memcpy(&addreq
.ifra_dstaddr
, dstres
->ai_addr
, dstres
->ai_addr
->sa_len
);
204 if (ioctl(s
, SIOCSIFPHYADDR
, &addreq
) < 0)
205 warn("SIOCSIFPHYADDR");
209 in_set_router(int s
, int enable
)
213 bzero(&ifr
, sizeof (ifr
));
214 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
215 ifr
.ifr_intval
= enable
;
217 if (ioctl(s
, SIOCSETROUTERMODE
, &ifr
) < 0)
218 warn("SIOCSETROUTERMODE");
222 routermode_from_string(char * str
, int *mode_p
)
226 if (strcasecmp(str
, "enabled") == 0) {
228 } else if (strcasecmp(str
, "disabled") == 0) {
237 routermode_string(int mode
)
256 in_routermode(int s
, int argc
, char *const*argv
)
261 bzero(&ifr
, sizeof (ifr
));
262 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
265 #ifndef SIOCGETROUTERMODE
266 #define SIOCGETROUTERMODE _IOWR('i', 209, struct ifreq) /* get IPv4 router mode state */
267 #endif /* SIOCGETROUTERMODE */
268 if (ioctl(s
, SIOCGETROUTERMODE
, &ifr
) < 0) {
270 warn("SIOCGETROUTERMODE");
273 /* argv is NULL if we're called from status() */
275 (argv
== NULL
) ? "\troutermode4: " : "",
276 routermode_string(ifr
.ifr_intval
));
282 if (routermode_from_string(argv
[0], &mode
) == 0) {
284 "mode '%s' invalid, must be one of "
285 "disabled or enabled",
288 ifr
.ifr_intval
= mode
;
289 if (ioctl(s
, SIOCSETROUTERMODE
, &ifr
) < 0) {
290 warn("SIOCSETROUTERMODE");
297 static struct afswtch af_inet
= {
300 .af_status
= in_status
,
301 .af_getaddr
= in_getaddr
,
302 .af_status_tunnel
= in_status_tunnel
,
303 .af_settunnel
= in_set_tunnel
,
304 .af_setrouter
= in_set_router
,
305 .af_routermode
= in_routermode
,
306 .af_difaddr
= SIOCDIFADDR
,
307 .af_aifaddr
= SIOCAIFADDR
,
308 .af_ridreq
= &in_ridreq
,
309 .af_addreq
= &in_addreq
,
312 static __constructor
void
315 af_register(&af_inet
);