if.c revision 124525
1181074Sdas/*	$KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $	*/
2181074Sdas
3181074Sdas/*
4181074Sdas * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5181074Sdas * All rights reserved.
6181074Sdas *
7181074Sdas * Redistribution and use in source and binary forms, with or without
8181074Sdas * modification, are permitted provided that the following conditions
9181074Sdas * are met:
10181074Sdas * 1. Redistributions of source code must retain the above copyright
11181074Sdas *    notice, this list of conditions and the following disclaimer.
12181074Sdas * 2. Redistributions in binary form must reproduce the above copyright
13181074Sdas *    notice, this list of conditions and the following disclaimer in the
14181074Sdas *    documentation and/or other materials provided with the distribution.
15181074Sdas * 3. Neither the name of the project nor the names of its contributors
16181074Sdas *    may be used to endorse or promote products derived from this software
17181074Sdas *    without specific prior written permission.
18181074Sdas *
19181074Sdas * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20181074Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21181074Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22181074Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23181074Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24181074Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25181074Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26181074Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27181074Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28181074Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29181074Sdas * SUCH DAMAGE.
30181074Sdas *
31181074Sdas * $FreeBSD: head/usr.sbin/rtsold/if.c 124525 2004-01-14 17:42:03Z ume $
32181074Sdas */
33181074Sdas
34181074Sdas#include <sys/param.h>
35181074Sdas#include <sys/socket.h>
36181074Sdas#include <sys/sysctl.h>
37181074Sdas#include <sys/ioctl.h>
38181074Sdas#include <sys/queue.h>
39181074Sdas
40181074Sdas#include <net/if.h>
41181074Sdas#include <net/if_var.h>
42181074Sdas#include <net/if_types.h>
43181074Sdas#include <net/route.h>
44181074Sdas#include <net/if_dl.h>
45181074Sdas#include <net/if_media.h>
46181074Sdas#include <net/ethernet.h>
47181074Sdas#include <netinet/in.h>
48181074Sdas#include <netinet/icmp6.h>
49181074Sdas
50181074Sdas#include <netinet6/in6_var.h>
51181074Sdas
52181074Sdas#include <stdio.h>
53181074Sdas#include <unistd.h>
54181074Sdas#include <stdlib.h>
55181074Sdas#include <syslog.h>
56181074Sdas#include <string.h>
57181074Sdas#include <fcntl.h>
58181074Sdas#include <errno.h>
59181074Sdas#include <limits.h>
60181074Sdas#include <ifaddrs.h>
61181074Sdas#include "rtsold.h"
62181074Sdas
63181074Sdasextern int rssock;
64181074Sdasstatic int ifsock;
65181074Sdas
66181074Sdasstatic int get_llflag __P((const char *));
67181074Sdasstatic void get_rtaddrs __P((int, struct sockaddr *, struct sockaddr **));
68181074Sdas
69181074Sdasint
70181074Sdasifinit(void)
71181074Sdas{
72181074Sdas	ifsock = rssock;
73181074Sdas
74181074Sdas	return(0);
75181074Sdas}
76181074Sdas
77181074Sdasint
78181074Sdasinterface_up(char *name)
79181074Sdas{
80181074Sdas	struct ifreq ifr;
81181074Sdas	int llflag;
82181074Sdas
83181074Sdas	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
84181074Sdas
85181074Sdas	if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
86		warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
87		    strerror(errno));
88		return(-1);
89	}
90	if (!(ifr.ifr_flags & IFF_UP)) {
91		ifr.ifr_flags |= IFF_UP;
92		if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
93			warnmsg(LOG_ERR, __func__,
94			    "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
95		return(-1);
96	}
97
98	warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
99
100	llflag = get_llflag(name);
101	if (llflag < 0) {
102		warnmsg(LOG_WARNING, __func__,
103		    "get_llflag() failed, anyway I'll try");
104		return 0;
105	}
106
107	if (!(llflag & IN6_IFF_NOTREADY)) {
108		warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
109		return(0);
110	} else {
111		if (llflag & IN6_IFF_TENTATIVE) {
112			warnmsg(LOG_DEBUG, __func__, "%s is tentative",
113			    name);
114			return IFS_TENTATIVE;
115		}
116		if (llflag & IN6_IFF_DUPLICATED)
117			warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
118			    name);
119		return -1;
120	}
121}
122
123int
124interface_status(struct ifinfo *ifinfo)
125{
126	char *ifname = ifinfo->ifname;
127	struct ifreq ifr;
128	struct ifmediareq ifmr;
129
130	/* get interface flags */
131	memset(&ifr, 0, sizeof(ifr));
132	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
133	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
134		warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
135		    ifname, strerror(errno));
136		return(-1);
137	}
138	/*
139	 * if one of UP and RUNNING flags is dropped,
140	 * the interface is not active.
141	 */
142	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
143		goto inactive;
144	}
145
146	/* Next, check carrier on the interface, if possible */
147	if (!ifinfo->mediareqok)
148		goto active;
149	memset(&ifmr, 0, sizeof(ifmr));
150	strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
151
152	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
153		if (errno != EINVAL) {
154			warnmsg(LOG_DEBUG, __func__,
155			    "ioctl(SIOCGIFMEDIA) on %s: %s",
156			    ifname, strerror(errno));
157			return(-1);
158		}
159		/*
160		 * EINVAL simply means that the interface does not support
161		 * the SIOCGIFMEDIA ioctl. We regard it alive.
162		 */
163		ifinfo->mediareqok = 0;
164		goto active;
165	}
166
167	if (ifmr.ifm_status & IFM_AVALID) {
168		switch (ifmr.ifm_active & IFM_NMASK) {
169		case IFM_ETHER:
170		case IFM_IEEE80211:
171			if (ifmr.ifm_status & IFM_ACTIVE)
172				goto active;
173			else
174				goto inactive;
175			break;
176		default:
177			goto inactive;
178		}
179	}
180
181  inactive:
182	return(0);
183
184  active:
185	return(1);
186}
187
188#define ROUNDUP(a, size) \
189	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
190
191#define NEXT_SA(ap) (ap) = (struct sockaddr *) \
192	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
193	sizeof(u_long)) : sizeof(u_long)))
194#define ROUNDUP8(a) (1 + (((a) - 1) | 7))
195
196int
197lladdropt_length(struct sockaddr_dl *sdl)
198{
199	switch (sdl->sdl_type) {
200	case IFT_ETHER:
201#ifdef IFT_IEEE80211
202	case IFT_IEEE80211:
203#endif
204		return(ROUNDUP8(ETHER_ADDR_LEN + 2));
205	default:
206		return(0);
207	}
208}
209
210void
211lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
212{
213	char *addr;
214
215	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
216
217	switch (sdl->sdl_type) {
218	case IFT_ETHER:
219#ifdef IFT_IEEE80211
220	case IFT_IEEE80211:
221#endif
222		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
223		addr = (char *)(ndopt + 1);
224		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
225		break;
226	default:
227		warnmsg(LOG_ERR, __func__,
228		    "unsupported link type(%d)", sdl->sdl_type);
229		exit(1);
230	}
231
232	return;
233}
234
235struct sockaddr_dl *
236if_nametosdl(char *name)
237{
238	int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
239	char *buf, *next, *lim;
240	size_t len;
241	struct if_msghdr *ifm;
242	struct sockaddr *sa, *rti_info[RTAX_MAX];
243	struct sockaddr_dl *sdl = NULL, *ret_sdl;
244
245	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
246		return(NULL);
247	if ((buf = malloc(len)) == NULL)
248		return(NULL);
249	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
250		free(buf);
251		return(NULL);
252	}
253
254	lim = buf + len;
255	for (next = buf; next < lim; next += ifm->ifm_msglen) {
256		ifm = (struct if_msghdr *)next;
257		if (ifm->ifm_type == RTM_IFINFO) {
258			sa = (struct sockaddr *)(ifm + 1);
259			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
260			if ((sa = rti_info[RTAX_IFP]) != NULL) {
261				if (sa->sa_family == AF_LINK) {
262					sdl = (struct sockaddr_dl *)sa;
263					if (strlen(name) != sdl->sdl_nlen)
264						continue; /* not same len */
265					if (strncmp(&sdl->sdl_data[0],
266						    name,
267						    sdl->sdl_nlen) == 0) {
268						break;
269					}
270				}
271			}
272		}
273	}
274	if (next == lim) {
275		/* search failed */
276		free(buf);
277		return(NULL);
278	}
279
280	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
281		return(NULL);
282	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
283
284	free(buf);
285	return(ret_sdl);
286}
287
288int
289getinet6sysctl(int code)
290{
291	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
292	int value;
293	size_t size;
294
295	mib[3] = code;
296	size = sizeof(value);
297	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0)
298		return -1;
299	else
300		return value;
301}
302
303int
304setinet6sysctl(int code, int newval)
305{
306	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
307	int value;
308	size_t size;
309
310	mib[3] = code;
311	size = sizeof(value);
312	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size,
313	    &newval, sizeof(newval)) < 0)
314		return -1;
315	else
316		return value;
317}
318
319/*------------------------------------------------------------*/
320
321/* get ia6_flags for link-local addr on if.  returns -1 on error. */
322static int
323get_llflag(const char *name)
324{
325	struct ifaddrs *ifap, *ifa;
326	struct in6_ifreq ifr6;
327	struct sockaddr_in6 *sin6;
328	int s;
329
330	if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
331		warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s",
332		    strerror(errno));
333		exit(1);
334	}
335	if (getifaddrs(&ifap) != 0) {
336		warnmsg(LOG_ERR, __func__, "getifaddrs: %s",
337		    strerror(errno));
338		exit(1);
339	}
340
341	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
342		if (strlen(ifa->ifa_name) != strlen(name) ||
343		    strncmp(ifa->ifa_name, name, strlen(name)) != 0)
344			continue;
345		if (ifa->ifa_addr->sa_family != AF_INET6)
346			continue;
347		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
348		if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
349			continue;
350
351		memset(&ifr6, 0, sizeof(ifr6));
352		strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
353		memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
354		if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
355			warnmsg(LOG_ERR, __func__,
356			    "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
357			exit(1);
358		}
359
360		freeifaddrs(ifap);
361		close(s);
362		return ifr6.ifr_ifru.ifru_flags6;
363	}
364
365	freeifaddrs(ifap);
366	close(s);
367	return -1;
368}
369
370
371static void
372get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
373{
374	int i;
375
376	for (i = 0; i < RTAX_MAX; i++) {
377		if (addrs & (1 << i)) {
378			rti_info[i] = sa;
379			NEXT_SA(sa);
380		} else
381			rti_info[i] = NULL;
382	}
383}
384