if.c revision 1.21.6.1 1 /* $NetBSD: if.c,v 1.21.6.1 2013/02/25 00:30:48 tls Exp $ */
2 /* $KAME: if.c,v 1.36 2004/11/30 22:32:01 suz Exp $ */
3
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.
19 *
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.
31 */
32
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>
55
56 #include "rtadvd.h"
57 #include "if.h"
58
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
64
65 static void
66 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
67 {
68 int i;
69
70 for (i = 0; i < RTAX_MAX; i++) {
71 if (addrs & (1 << i)) {
72 rti_info[i] = sa;
73 RT_ADVANCE(sa, sa);
74 }
75 else
76 rti_info[i] = NULL;
77 }
78 }
79
80 struct sockaddr_dl *
81 if_nametosdl(const char *name)
82 {
83 struct ifaddrs *ifap, *ifa;
84 struct sockaddr_dl *sdl;
85
86 if (getifaddrs(&ifap) != 0)
87 return (NULL);
88
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;
94
95 sdl = malloc(ifa->ifa_addr->sa_len);
96 if (!sdl)
97 continue; /*XXX*/
98
99 memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
100 freeifaddrs(ifap);
101 return (sdl);
102 }
103
104 freeifaddrs(ifap);
105 return (NULL);
106 }
107
108 int
109 if_getmtu(const char *name)
110 {
111 struct ifreq ifr;
112 int s, mtu;
113
114 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
115 return 0;
116
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);
125
126 return mtu;
127 }
128
129 /* give interface index and its old flags, then new flags returned */
130 int
131 if_getflags(int ifindex, int oifflags)
132 {
133 struct ifreq ifr;
134 int s;
135
136 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
137 syslog(LOG_ERR, "<%s> socket: %s", __func__,
138 strerror(errno));
139 return (oifflags & ~IFF_UP);
140 }
141
142 memset(&ifr, 0, sizeof(ifr));
143 if_indextoname(ifindex, ifr.ifr_name);
144 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
145 syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
146 __func__, ifr.ifr_name);
147 close(s);
148 return (oifflags & ~IFF_UP);
149 }
150 close(s);
151 return (ifr.ifr_flags);
152 }
153
154 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
155 int
156 lladdropt_length(struct sockaddr_dl *sdl)
157 {
158 switch (sdl->sdl_type) {
159 case IFT_ETHER:
160 case IFT_FDDI:
161 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
162 default:
163 return(0);
164 }
165 }
166
167 void
168 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
169 {
170 char *addr;
171
172 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
173
174 switch (sdl->sdl_type) {
175 case IFT_ETHER:
176 case IFT_FDDI:
177 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
178 addr = (char *)(ndopt + 1);
179 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
180 break;
181 default:
182 syslog(LOG_ERR, "<%s> unsupported link type(%d)",
183 __func__, sdl->sdl_type);
184 exit(1);
185 }
186
187 return;
188 }
189
190 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
191 #define SIN6(s) ((struct sockaddr_in6 *)(s))
192 #define SDL(s) ((struct sockaddr_dl *)(s))
193 char *
194 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
195 {
196 struct rt_msghdr *rtm;
197 struct ifa_msghdr *ifam;
198 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
199
200 *lenp = 0;
201 for (rtm = (struct rt_msghdr *)buf;
202 rtm < (struct rt_msghdr *)lim;
203 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
204 /* just for safety */
205 if (!rtm->rtm_msglen) {
206 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
207 "(buf=%p lim=%p rtm=%p)", __func__,
208 buf, lim, rtm);
209 break;
210 }
211 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
212 continue;
213 }
214
215 switch (rtm->rtm_type) {
216 case RTM_GET:
217 case RTM_ADD:
218 case RTM_DELETE:
219 /* address related checks */
220 sa = (struct sockaddr *)(rtm + 1);
221 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
222 if ((dst = rti_info[RTAX_DST]) == NULL ||
223 dst->sa_family != AF_INET6)
224 continue;
225
226 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
227 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
228 continue;
229
230 if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
231 gw->sa_family != AF_LINK)
232 continue;
233 if (ifindex && SDL(gw)->sdl_index != ifindex)
234 continue;
235
236 if (rti_info[RTAX_NETMASK] == NULL)
237 continue;
238
239 /* found */
240 *lenp = rtm->rtm_msglen;
241 return (char *)rtm;
242 /* NOTREACHED */
243 case RTM_NEWADDR:
244 case RTM_DELADDR:
245 ifam = (struct ifa_msghdr *)rtm;
246
247 /* address related checks */
248 sa = (struct sockaddr *)(ifam + 1);
249 get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
250 if ((ifa = rti_info[RTAX_IFA]) == NULL ||
251 (ifa->sa_family != AF_INET &&
252 ifa->sa_family != AF_INET6))
253 continue;
254
255 if (ifa->sa_family == AF_INET6 &&
256 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
257 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
258 continue;
259
260 if (ifindex && ifam->ifam_index != ifindex)
261 continue;
262
263 /* found */
264 *lenp = ifam->ifam_msglen;
265 return (char *)rtm;
266 /* NOTREACHED */
267 #ifdef RTM_IFANNOUNCE
268 case RTM_IFANNOUNCE:
269 #endif
270 case RTM_IFINFO:
271 /* found */
272 *lenp = rtm->rtm_msglen;
273 return (char *)rtm;
274 /* NOTREACHED */
275 }
276 }
277
278 return (char *)rtm;
279 }
280 #undef FILTER_MATCH
281
282 struct in6_addr *
283 get_addr(char *buf)
284 {
285 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
286 struct sockaddr *sa, *rti_info[RTAX_MAX];
287
288 sa = (struct sockaddr *)(rtm + 1);
289 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
290
291 return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
292 }
293
294 int
295 get_rtm_ifindex(char *buf)
296 {
297 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
298 struct sockaddr *sa, *rti_info[RTAX_MAX];
299
300 sa = (struct sockaddr *)(rtm + 1);
301 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
302
303 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
304 }
305
306 int
307 get_ifm_ifindex(char *buf)
308 {
309 struct if_msghdr *ifm = (struct if_msghdr *)buf;
310
311 return ((int)ifm->ifm_index);
312 }
313
314 int
315 get_ifam_ifindex(char *buf)
316 {
317 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
318
319 return ((int)ifam->ifam_index);
320 }
321
322 int
323 get_ifm_flags(char *buf)
324 {
325 struct if_msghdr *ifm = (struct if_msghdr *)buf;
326
327 return (ifm->ifm_flags);
328 }
329
330 #ifdef RTM_IFANNOUNCE
331 int
332 get_ifan_ifindex(char *buf)
333 {
334 struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
335
336 return ((int)ifan->ifan_index);
337 }
338
339 int
340 get_ifan_what(char *buf)
341 {
342 struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
343
344 return ((int)ifan->ifan_what);
345 }
346 #endif
347
348 int
349 get_prefixlen(char *buf)
350 {
351 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
352 struct sockaddr *sa, *rti_info[RTAX_MAX];
353 unsigned char *p, *lim;
354
355 sa = (struct sockaddr *)(rtm + 1);
356 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
357 sa = rti_info[RTAX_NETMASK];
358
359 p = (unsigned char *)(&SIN6(sa)->sin6_addr);
360 lim = (unsigned char *)sa + sa->sa_len;
361 return prefixlen(p, lim);
362 }
363
364 int
365 prefixlen(const unsigned char *p, const unsigned char *lim)
366 {
367 int masklen;
368
369 for (masklen = 0; p < lim; p++) {
370 switch (*p) {
371 case 0xff:
372 masklen += 8;
373 break;
374 case 0xfe:
375 masklen += 7;
376 break;
377 case 0xfc:
378 masklen += 6;
379 break;
380 case 0xf8:
381 masklen += 5;
382 break;
383 case 0xf0:
384 masklen += 4;
385 break;
386 case 0xe0:
387 masklen += 3;
388 break;
389 case 0xc0:
390 masklen += 2;
391 break;
392 case 0x80:
393 masklen += 1;
394 break;
395 case 0x00:
396 break;
397 default:
398 return(-1);
399 }
400 }
401
402 return(masklen);
403 }
404
405 int
406 rtmsg_type(char *buf)
407 {
408 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
409
410 return(rtm->rtm_type);
411 }
412
413 int
414 rtmsg_len(char *buf)
415 {
416 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
417
418 return(rtm->rtm_msglen);
419 }
420