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