]> git.saurik.com Git - apple/network_cmds.git/blob - rtsol.tproj/if.c
network_cmds-396.6.tar.gz
[apple/network_cmds.git] / rtsol.tproj / if.c
1 /*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $KAME: if.c,v 1.15 2001/05/22 06:04:17 jinmei Exp $ */
30
31 /*
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * $FreeBSD: src/usr.sbin/rtsold/if.c,v 1.2.2.3 2001/07/03 11:02:16 ume Exp $
60 */
61
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/ioctl.h>
66 #include <sys/queue.h>
67
68 #include <net/if.h>
69 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
70 #include <net/if_var.h>
71 #endif /* __FreeBSD__ >= 3 */
72 #include <net/if_types.h>
73 #include <net/route.h>
74 #include <net/if_dl.h>
75 #include <net/if_media.h>
76 #if defined ( __FreeBSD__) || defined (__APPLE__)
77 # include <net/ethernet.h>
78 #endif
79 #ifdef __NetBSD__
80 #include <net/if_ether.h>
81 #endif
82 #if defined(__bsdi__) || defined(__OpenBSD__)
83 # include <netinet/in.h>
84 # include <netinet/if_ether.h>
85 #endif
86 #include <netinet/in.h>
87 #include <netinet/icmp6.h>
88
89 #include <netinet6/in6_var.h>
90
91 #include <stdio.h>
92 #include <unistd.h>
93 #include <stdlib.h>
94 #include <syslog.h>
95 #include <string.h>
96 #include <fcntl.h>
97 #include <errno.h>
98 #include <limits.h>
99 #ifdef HAVE_GETIFADDRS
100 #include <ifaddrs.h>
101 #endif
102
103 #include "rtsold.h"
104
105 extern int rssock;
106 static int ifsock;
107
108 static int get_llflag __P((const char *name));
109 #ifndef HAVE_GETIFADDRS
110 static unsigned int if_maxindex __P((void));
111 #endif
112 static void get_rtaddrs __P((int addrs, struct sockaddr *sa,
113 struct sockaddr **rti_info));
114
115 int
116 ifinit()
117 {
118 ifsock = rssock;
119
120 return(0);
121 }
122
123 int
124 interface_up(char *name)
125 {
126 struct ifreq ifr;
127 int llflag;
128
129 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
130
131 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
132 warnmsg(LOG_WARNING, __FUNCTION__, "ioctl(SIOCGIFFLAGS): %s",
133 strerror(errno));
134 return(-1);
135 }
136 if (!(ifr.ifr_flags & IFF_UP)) {
137 ifr.ifr_flags |= IFF_UP;
138 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
139 warnmsg(LOG_ERR, __FUNCTION__,
140 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
141 }
142 return(-1);
143 }
144
145 warnmsg(LOG_DEBUG, __FUNCTION__, "checking if %s is ready...", name);
146
147 llflag = get_llflag(name);
148 if (llflag < 0) {
149 warnmsg(LOG_WARNING, __FUNCTION__,
150 "get_llflag() failed, anyway I'll try");
151 return 0;
152 }
153
154 if (!(llflag & (IN6_IFF_NOTREADY | IN6_IFF_DADPROGRESS))) {
155 warnmsg(LOG_DEBUG, __FUNCTION__,
156 "%s is ready", name);
157 return(0);
158 } else {
159 if (llflag & IN6_IFF_TENTATIVE) {
160 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is tentative",
161 name);
162 return IFS_TENTATIVE;
163 }
164 if (llflag & IN6_IFF_OPTIMISTIC) {
165 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is optimistic",
166 name);
167 return IFS_OPTIMISTIC;
168 }
169 if (llflag & IN6_IFF_DUPLICATED)
170 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is duplicated",
171 name);
172 return -1;
173 }
174 }
175
176 int
177 interface_status(struct ifinfo *ifinfo)
178 {
179 char *ifname = ifinfo->ifname;
180 struct ifreq ifr;
181 struct ifmediareq ifmr;
182
183 /* get interface flags */
184 memset(&ifr, 0, sizeof(ifr));
185 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
186 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
187 warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFFLAGS) on %s: %s",
188 ifname, strerror(errno));
189 return(-1);
190 }
191 /*
192 * if one of UP and RUNNING flags is dropped,
193 * the interface is not active.
194 */
195 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
196 goto inactive;
197 }
198
199 /* Next, check carrier on the interface, if possible */
200 if (!ifinfo->mediareqok)
201 goto active;
202 memset(&ifmr, 0, sizeof(ifmr));
203 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
204
205 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
206 if (errno != EINVAL) {
207 warnmsg(LOG_DEBUG, __FUNCTION__,
208 "ioctl(SIOCGIFMEDIA) on %s: %s",
209 ifname, strerror(errno));
210 return(-1);
211 }
212 /*
213 * EINVAL simply means that the interface does not support
214 * the SIOCGIFMEDIA ioctl. We regard it alive.
215 */
216 ifinfo->mediareqok = 0;
217 goto active;
218 }
219
220 if (ifmr.ifm_status & IFM_AVALID) {
221 switch(ifmr.ifm_active & IFM_NMASK) {
222 case IFM_ETHER:
223 if (ifmr.ifm_status & IFM_ACTIVE)
224 goto active;
225 else
226 goto inactive;
227 break;
228 default:
229 goto inactive;
230 }
231 }
232
233 inactive:
234 return(0);
235
236 active:
237 return(1);
238 }
239
240 #define ROUNDUP(a, size) \
241 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
242
243 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
244 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
245 sizeof(uint32_t)) :\
246 sizeof(uint32_t)))
247 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
248
249 int
250 lladdropt_length(struct sockaddr_dl *sdl)
251 {
252 switch(sdl->sdl_type) {
253 case IFT_ETHER:
254 #ifdef IFT_IEEE80211
255 case IFT_IEEE80211:
256 #endif
257 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
258 default:
259 return(0);
260 }
261 }
262
263 void
264 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
265 {
266 char *addr;
267
268 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
269
270 switch(sdl->sdl_type) {
271 case IFT_ETHER:
272 #ifdef IFT_IEEE80211
273 case IFT_IEEE80211:
274 #endif
275 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
276 addr = (char *)(ndopt + 1);
277 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
278 break;
279 default:
280 warnmsg(LOG_ERR, __FUNCTION__,
281 "unsupported link type(%d)", sdl->sdl_type);
282 exit(1);
283 }
284
285 return;
286 }
287
288 struct sockaddr_dl *
289 if_nametosdl(char *name)
290 {
291 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
292 char *buf, *next, *lim;
293 size_t len;
294 struct if_msghdr *ifm;
295 struct sockaddr *sa, *rti_info[RTAX_MAX];
296 struct sockaddr_dl *sdl = NULL, *ret_sdl;
297
298 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
299 return(NULL);
300 if ((buf = malloc(len)) == NULL)
301 return(NULL);
302 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
303 free(buf);
304 return(NULL);
305 }
306
307 lim = buf + len;
308 for (next = buf; next < lim; next += ifm->ifm_msglen) {
309 ifm = (struct if_msghdr *)next;
310 if (ifm->ifm_type == RTM_IFINFO) {
311 sa = (struct sockaddr *)(ifm + 1);
312 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
313 if ((sa = rti_info[RTAX_IFP]) != NULL) {
314 if (sa->sa_family == AF_LINK) {
315 sdl = (struct sockaddr_dl *)sa;
316 if (strlen(name) != sdl->sdl_nlen)
317 continue; /* not same len */
318 if (strncmp(&sdl->sdl_data[0],
319 name,
320 sdl->sdl_nlen) == 0) {
321 break;
322 }
323 }
324 }
325 }
326 }
327 if (next == lim) {
328 /* search failed */
329 free(buf);
330 return(NULL);
331 }
332
333 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
334 return(NULL);
335 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
336
337 free(buf);
338 return(ret_sdl);
339 }
340
341 int
342 getinet6sysctl(int code)
343 {
344 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
345 int value;
346 size_t size;
347
348 mib[3] = code;
349 size = sizeof(value);
350 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0)
351 return -1;
352 else
353 return value;
354 }
355
356 /*------------------------------------------------------------*/
357
358 /* get ia6_flags for link-local addr on if. returns -1 on error. */
359 static int
360 get_llflag(const char *name)
361 {
362 #ifdef HAVE_GETIFADDRS
363 struct ifaddrs *ifap, *ifa;
364 struct in6_ifreq ifr6;
365 struct sockaddr_in6 *sin6;
366 int s;
367
368 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
369 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
370 strerror(errno));
371 exit(1);
372 }
373 if (getifaddrs(&ifap) != 0) {
374 warnmsg(LOG_ERR, __FUNCTION__, "etifaddrs: %s",
375 strerror(errno));
376 exit(1);
377 }
378
379 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
380 if (strlen(ifa->ifa_name) != strlen(name)
381 || strncmp(ifa->ifa_name, name, strlen(name)) != 0)
382 continue;
383 if (ifa->ifa_addr->sa_family != AF_INET6)
384 continue;
385 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
386 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
387 continue;
388
389 memset(&ifr6, 0, sizeof(ifr6));
390 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
391 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
392 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
393 warnmsg(LOG_ERR, __FUNCTION__,
394 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
395 exit(1);
396 }
397
398 freeifaddrs(ifap);
399 close(s);
400 return ifr6.ifr_ifru.ifru_flags6;
401 }
402
403 freeifaddrs(ifap);
404 close(s);
405 return -1;
406 #else
407 int s;
408 unsigned int maxif;
409 struct ifreq *iflist;
410 struct ifconf ifconf;
411 struct ifreq *ifr, *ifr_end;
412 struct sockaddr_in6 *sin6;
413 struct in6_ifreq ifr6;
414
415 maxif = if_maxindex() + 1;
416 iflist = (struct ifreq *)malloc(maxif * BUFSIZ); /* XXX */
417 if (iflist == NULL) {
418 warnmsg(LOG_ERR, __FUNCTION__, "not enough core");
419 exit(1);
420 }
421
422 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
423 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
424 strerror(errno));
425 exit(1);
426 }
427 memset(&ifconf, 0, sizeof(ifconf));
428 ifconf.ifc_req = iflist;
429 ifconf.ifc_len = maxif * BUFSIZ; /* XXX */
430 if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) {
431 warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFCONF): %s",
432 strerror(errno));
433 exit(1);
434 }
435
436 /* Look for this interface in the list */
437 ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len);
438 for (ifr = ifconf.ifc_req;
439 ifr < ifr_end;
440 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
441 + ifr->ifr_addr.sa_len)) {
442 if (strlen(ifr->ifr_name) != strlen(name)
443 || strncmp(ifr->ifr_name, name, strlen(name)) != 0)
444 continue;
445 if (ifr->ifr_addr.sa_family != AF_INET6)
446 continue;
447 sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr;
448 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
449 continue;
450
451 memset(&ifr6, 0, sizeof(ifr6));
452 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
453 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
454 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
455 warnmsg(LOG_ERR, __FUNCTION__,
456 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
457 exit(1);
458 }
459
460 free(iflist);
461 close(s);
462 return ifr6.ifr_ifru.ifru_flags6;
463 }
464
465 free(iflist);
466 close(s);
467 return -1;
468 #endif
469 }
470
471 #ifndef HAVE_GETIFADDRS
472 static unsigned int
473 if_maxindex()
474 {
475 struct if_nameindex *p, *p0;
476 unsigned int max = 0;
477
478 p0 = if_nameindex();
479 for (p = p0; p && p->if_index && p->if_name; p++) {
480 if (max < p->if_index)
481 max = p->if_index;
482 }
483 if_freenameindex(p0);
484 return max;
485 }
486 #endif
487
488 static void
489 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
490 {
491 int i;
492
493 for (i = 0; i < RTAX_MAX; i++) {
494 if (addrs & (1 << i)) {
495 rti_info[i] = sa;
496 NEXT_SA(sa);
497 }
498 else
499 rti_info[i] = NULL;
500 }
501 }