Remove building with NOCRYPTO option
[minix.git] / usr.sbin / rtadvd / if.c
blob37d045c52c54ba75b4fc71ba2dc9635ff8f87562
1 /* $NetBSD: if.c,v 1.23 2015/06/05 15:41:59 roy Exp $ */
2 /* $KAME: if.c,v 1.36 2004/11/30 22:32:01 suz Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <net/if_types.h>
40 #include <ifaddrs.h>
41 #ifdef __FreeBSD__
42 #include <net/ethernet.h>
43 #else
44 #include <net/if_ether.h>
45 #endif
46 #include <net/route.h>
47 #include <net/if_dl.h>
48 #include <netinet/in.h>
49 #include <netinet/icmp6.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
56 #include "rtadvd.h"
57 #include "if.h"
59 #ifndef RT_ROUNDUP
60 #define RT_ROUNDUP(a) \
61 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
62 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
63 #endif
65 static void
66 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
68 int i;
70 for (i = 0; i < RTAX_MAX; i++) {
71 if (addrs & (1 << i)) {
72 rti_info[i] = sa;
73 RT_ADVANCE(sa, sa);
75 else
76 rti_info[i] = NULL;
80 struct sockaddr_dl *
81 if_nametosdl(const char *name)
83 struct ifaddrs *ifap, *ifa;
84 struct sockaddr_dl *sdl;
86 if (getifaddrs(&ifap) != 0)
87 return (NULL);
89 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
90 if (strcmp(ifa->ifa_name, name) != 0)
91 continue;
92 if (ifa->ifa_addr->sa_family != AF_LINK)
93 continue;
95 sdl = malloc(ifa->ifa_addr->sa_len);
96 if (!sdl)
97 continue; /*XXX*/
99 memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
100 freeifaddrs(ifap);
101 return (sdl);
104 freeifaddrs(ifap);
105 return (NULL);
109 if_getmtu(const char *name)
111 struct ifreq ifr;
112 int s, mtu;
114 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
115 return 0;
117 memset(&ifr, 0, sizeof(ifr));
118 ifr.ifr_addr.sa_family = AF_INET6;
119 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
120 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
121 mtu = ifr.ifr_mtu;
122 else
123 mtu = 0;
124 close(s);
126 return mtu;
129 /* give interface index and its old flags, then new flags returned */
131 if_getflags(int ifindex, int oifflags)
133 struct ifreq ifr;
134 int s;
136 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
137 syslog(LOG_ERR, "<%s> socket: %m", __func__);
138 return (oifflags & ~IFF_UP);
141 memset(&ifr, 0, sizeof(ifr));
142 if_indextoname(ifindex, ifr.ifr_name);
143 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
144 syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
145 __func__, ifr.ifr_name);
146 close(s);
147 return (oifflags & ~IFF_UP);
149 close(s);
150 return (ifr.ifr_flags);
153 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
155 lladdropt_length(struct sockaddr_dl *sdl)
157 switch (sdl->sdl_type) {
158 case IFT_ETHER:
159 case IFT_FDDI:
160 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
161 default:
162 return(0);
166 void
167 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
169 char *addr;
171 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
173 switch (sdl->sdl_type) {
174 case IFT_ETHER:
175 case IFT_FDDI:
176 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
177 addr = (char *)(ndopt + 1);
178 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
179 break;
180 default:
181 syslog(LOG_ERR, "<%s> unsupported link type(%d)",
182 __func__, sdl->sdl_type);
183 exit(1);
186 return;
189 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
190 #define SIN6(s) ((struct sockaddr_in6 *)(s))
191 #define SDL(s) ((struct sockaddr_dl *)(s))
192 char *
193 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
195 struct rt_msghdr *rtm;
196 struct ifa_msghdr *ifam;
197 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
199 *lenp = 0;
200 for (rtm = (struct rt_msghdr *)buf;
201 rtm < (struct rt_msghdr *)lim;
202 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
203 /* just for safety */
204 if (!rtm->rtm_msglen) {
205 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
206 "(buf=%p lim=%p rtm=%p)", __func__,
207 buf, lim, rtm);
208 break;
210 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
211 continue;
214 switch (rtm->rtm_type) {
215 case RTM_GET:
216 case RTM_ADD:
217 case RTM_DELETE:
218 /* address related checks */
219 sa = (struct sockaddr *)(rtm + 1);
220 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
221 if ((dst = rti_info[RTAX_DST]) == NULL ||
222 dst->sa_family != AF_INET6)
223 continue;
225 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
226 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
227 continue;
229 if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
230 gw->sa_family != AF_LINK)
231 continue;
232 if (ifindex && SDL(gw)->sdl_index != ifindex)
233 continue;
235 if (rti_info[RTAX_NETMASK] == NULL)
236 continue;
238 /* found */
239 *lenp = rtm->rtm_msglen;
240 return (char *)rtm;
241 /* NOTREACHED */
242 case RTM_NEWADDR:
243 case RTM_DELADDR:
244 ifam = (struct ifa_msghdr *)rtm;
246 /* address related checks */
247 sa = (struct sockaddr *)(ifam + 1);
248 get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
249 if ((ifa = rti_info[RTAX_IFA]) == NULL ||
250 (ifa->sa_family != AF_INET &&
251 ifa->sa_family != AF_INET6))
252 continue;
254 if (ifa->sa_family == AF_INET6 &&
255 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
256 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
257 continue;
259 if (ifindex && ifam->ifam_index != ifindex)
260 continue;
262 /* found */
263 *lenp = ifam->ifam_msglen;
264 return (char *)rtm;
265 /* NOTREACHED */
266 #ifdef RTM_IFANNOUNCE
267 case RTM_IFANNOUNCE:
268 #endif
269 case RTM_IFINFO:
270 /* found */
271 *lenp = rtm->rtm_msglen;
272 return (char *)rtm;
273 /* NOTREACHED */
277 return (char *)rtm;
279 #undef FILTER_MATCH
281 struct in6_addr *
282 get_addr(char *buf)
284 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
285 struct sockaddr *sa, *rti_info[RTAX_MAX];
287 sa = (struct sockaddr *)(rtm + 1);
288 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
290 return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
294 get_rtm_ifindex(char *buf)
296 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
297 struct sockaddr *sa, *rti_info[RTAX_MAX];
299 sa = (struct sockaddr *)(rtm + 1);
300 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
302 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
306 get_ifm_ifindex(char *buf)
308 struct if_msghdr *ifm = (struct if_msghdr *)buf;
310 return ((int)ifm->ifm_index);
314 get_ifam_ifindex(char *buf)
316 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
318 return ((int)ifam->ifam_index);
322 get_ifm_flags(char *buf)
324 struct if_msghdr *ifm = (struct if_msghdr *)buf;
326 return (ifm->ifm_flags);
329 #ifdef RTM_IFANNOUNCE
331 get_ifan_ifindex(char *buf)
333 struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
335 return ((int)ifan->ifan_index);
339 get_ifan_what(char *buf)
341 struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
343 return ((int)ifan->ifan_what);
345 #endif
348 get_prefixlen(char *buf)
350 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
351 struct sockaddr *sa, *rti_info[RTAX_MAX];
352 unsigned char *p, *lim;
354 sa = (struct sockaddr *)(rtm + 1);
355 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
356 sa = rti_info[RTAX_NETMASK];
358 p = (unsigned char *)(&SIN6(sa)->sin6_addr);
359 lim = (unsigned char *)sa + sa->sa_len;
360 return prefixlen(p, lim);
364 prefixlen(const unsigned char *p, const unsigned char *lim)
366 int masklen;
368 for (masklen = 0; p < lim; p++) {
369 switch (*p) {
370 case 0xff:
371 masklen += 8;
372 break;
373 case 0xfe:
374 masklen += 7;
375 break;
376 case 0xfc:
377 masklen += 6;
378 break;
379 case 0xf8:
380 masklen += 5;
381 break;
382 case 0xf0:
383 masklen += 4;
384 break;
385 case 0xe0:
386 masklen += 3;
387 break;
388 case 0xc0:
389 masklen += 2;
390 break;
391 case 0x80:
392 masklen += 1;
393 break;
394 case 0x00:
395 break;
396 default:
397 return(-1);
401 return(masklen);
405 rtmsg_type(char *buf)
407 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
409 return(rtm->rtm_type);
413 rtmsg_len(char *buf)
415 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
417 return(rtm->rtm_msglen);