1 /* $NetBSD: rtsock.c,v 1.252 2019/09/01 18:54:38 roy Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1988, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.252 2019/09/01 18:54:38 roy Exp $"); 65 66 #ifdef _KERNEL_OPT 67 #include "opt_inet.h" 68 #include "opt_compat_netbsd.h" 69 #endif 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/proc.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/domain.h> 77 #include <sys/protosw.h> 78 #include <sys/sysctl.h> 79 #include <sys/kauth.h> 80 #include <sys/kmem.h> 81 #include <sys/intr.h> 82 #include <sys/condvar.h> 83 #include <sys/compat_stub.h> 84 85 #include <net/if.h> 86 #include <net/if_llatbl.h> 87 #include <net/if_types.h> 88 #include <net/route.h> 89 #include <net/raw_cb.h> 90 91 #include <netinet/in_var.h> 92 #include <netinet/if_inarp.h> 93 94 #include <netmpls/mpls.h> 95 96 #include <compat/net/if.h> 97 #include <compat/net/route.h> 98 99 #ifdef COMPAT_RTSOCK 100 #undef COMPAT_RTSOCK 101 #endif 102 103 static int if_addrflags(struct ifaddr *); 104 105 #include <net/rtsock_shared.c> 106 107 /* 108 * XXX avoid using void * once msghdr compat disappears. 109 */ 110 void 111 rt_setmetrics(void *in, struct rtentry *out) 112 { 113 const struct rt_xmsghdr *rtm = in; 114 115 _rt_setmetrics(rtm->rtm_inits, rtm, out); 116 } 117 118 int 119 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w, 120 int *lenp) 121 { 122 return rt_msg2(type, rtinfo, cpv, w, lenp); 123 } 124 125 static int 126 if_addrflags(struct ifaddr *ifa) 127 { 128 129 switch (ifa->ifa_addr->sa_family) { 130 #ifdef INET 131 case AF_INET: 132 return ((struct in_ifaddr *)ifa)->ia4_flags; 133 #endif 134 #ifdef INET6 135 case AF_INET6: 136 return ((struct in6_ifaddr *)ifa)->ia6_flags; 137 #endif 138 default: 139 return 0; 140 } 141 } 142 143 144 /* 145 * Send a routing message as mimicing that a cloned route is added. 146 */ 147 void 148 rt_clonedmsg(int type, const struct sockaddr *dst, const uint8_t *lladdr, 149 const struct ifnet *ifp) 150 { 151 struct rt_addrinfo info; 152 /* Mimic flags exactly */ 153 #define RTF_LLINFO 0x400 154 #define RTF_CLONED 0x2000 155 int flags = RTF_DONE; 156 union { 157 struct sockaddr sa; 158 struct sockaddr_storage ss; 159 struct sockaddr_dl sdl; 160 } u; 161 162 if (type != RTM_MISS) 163 flags |= RTF_HOST | RTF_CLONED | RTF_LLINFO; 164 if (type == RTM_ADD || type == RTM_CHANGE) 165 flags |= RTF_UP; 166 memset(&info, 0, sizeof(info)); 167 info.rti_info[RTAX_DST] = dst; 168 sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type, 169 NULL, 0, lladdr, ifp->if_addrlen); 170 info.rti_info[RTAX_GATEWAY] = &u.sa; 171 172 rt_missmsg(type, &info, flags, 0); 173 #undef RTF_LLINFO 174 #undef RTF_CLONED 175 } 176 177 178 /* 179 * The remaining code implements the routing-table sysctl node. It is 180 * compiled only for the non-COMPAT case. 181 */ 182 183 /* 184 * This is used in dumping the kernel table via sysctl(). 185 */ 186 static int 187 sysctl_dumpentry(struct rtentry *rt, void *v) 188 { 189 struct rt_walkarg *w = v; 190 int error = 0, size; 191 struct rt_addrinfo info; 192 193 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 194 return 0; 195 memset(&info, 0, sizeof(info)); 196 info.rti_info[RTAX_DST] = rt_getkey(rt); 197 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 198 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 199 info.rti_info[RTAX_TAG] = rt_gettag(rt); 200 if (rt->rt_ifp) { 201 const struct ifaddr *rtifa; 202 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr; 203 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL, 204 * then rt_get_ifa() != NULL. So this ought to still be safe. 205 * --dyoung 206 */ 207 rtifa = rt_get_ifa(rt); 208 info.rti_info[RTAX_IFA] = rtifa->ifa_addr; 209 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 210 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr; 211 } 212 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size))) 213 return error; 214 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 215 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem; 216 217 rtm->rtm_flags = rt->rt_flags; 218 rtm->rtm_use = rt->rt_use; 219 rtm_setmetrics(rt, rtm); 220 KASSERT(rt->rt_ifp != NULL); 221 rtm->rtm_index = rt->rt_ifp->if_index; 222 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 223 rtm->rtm_addrs = info.rti_addrs; 224 if ((error = copyout(rtm, w->w_where, size)) != 0) 225 w->w_where = NULL; 226 else 227 w->w_where = (char *)w->w_where + size; 228 } 229 return error; 230 } 231 232 static int 233 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w, 234 struct rt_addrinfo *info, size_t len) 235 { 236 struct if_xmsghdr *ifm; 237 int error; 238 239 ifm = (struct if_xmsghdr *)w->w_tmem; 240 ifm->ifm_index = ifp->if_index; 241 ifm->ifm_flags = ifp->if_flags; 242 ifm->ifm_data = ifp->if_data; 243 ifm->ifm_addrs = info->rti_addrs; 244 if ((error = copyout(ifm, w->w_where, len)) == 0) 245 w->w_where = (char *)w->w_where + len; 246 return error; 247 } 248 249 static int 250 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa, 251 struct rt_addrinfo *info) 252 { 253 int len, error; 254 255 if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len))) 256 return error; 257 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 258 struct ifa_xmsghdr *ifam; 259 260 ifam = (struct ifa_xmsghdr *)w->w_tmem; 261 ifam->ifam_index = ifa->ifa_ifp->if_index; 262 ifam->ifam_flags = ifa->ifa_flags; 263 ifam->ifam_metric = ifa->ifa_metric; 264 ifam->ifam_addrs = info->rti_addrs; 265 ifam->ifam_pid = 0; 266 ifam->ifam_addrflags = if_addrflags(ifa); 267 if ((error = copyout(w->w_tmem, w->w_where, len)) == 0) 268 w->w_where = (char *)w->w_where + len; 269 } 270 return error; 271 } 272 273 static int 274 sysctl_iflist(int af, struct rt_walkarg *w, int type) 275 { 276 struct ifnet *ifp; 277 struct ifaddr *ifa; 278 struct rt_addrinfo info; 279 int cmd, len, error = 0; 280 int s; 281 struct psref psref; 282 int bound; 283 284 switch (type) { 285 case NET_RT_IFLIST: 286 cmd = RTM_IFINFO; 287 break; 288 case NET_RT_OOOIFLIST: 289 cmd = RTM_OOIFINFO; 290 break; 291 case NET_RT_OOIFLIST: 292 cmd = RTM_OIFINFO; 293 break; 294 case NET_RT_OIFLIST: 295 cmd = RTM_IFINFO; 296 break; 297 default: 298 #ifdef RTSOCK_DEBUG 299 printf("%s: unsupported IFLIST type %d\n", __func__, type); 300 #endif 301 return EINVAL; 302 } 303 304 memset(&info, 0, sizeof(info)); 305 306 bound = curlwp_bind(); 307 s = pserialize_read_enter(); 308 IFNET_READER_FOREACH(ifp) { 309 int _s; 310 if (w->w_arg && w->w_arg != ifp->if_index) 311 continue; 312 if (IFADDR_READER_EMPTY(ifp)) 313 continue; 314 315 if_acquire(ifp, &psref); 316 pserialize_read_exit(s); 317 318 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr; 319 if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0) 320 goto release_exit; 321 info.rti_info[RTAX_IFP] = NULL; 322 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 323 switch (type) { 324 case NET_RT_OIFLIST: /* old _70 */ 325 if (!rtsock_iflist_70_hook.hooked) { 326 error = EINVAL; 327 break; 328 } 329 /* FALLTHROUGH */ 330 case NET_RT_IFLIST: /* current */ 331 error = sysctl_iflist_if(ifp, w, &info, len); 332 break; 333 case NET_RT_OOIFLIST: /* old _50 */ 334 MODULE_HOOK_CALL(rtsock_iflist_50_hook, 335 (ifp, w, &info, len), enosys(), error); 336 break; 337 case NET_RT_OOOIFLIST: /* old _14 */ 338 MODULE_HOOK_CALL(rtsock_iflist_14_hook, 339 (ifp, w, &info, len), enosys(), error); 340 break; 341 default: 342 error = EINVAL; 343 } 344 if (error != 0) { 345 if (error == ENOSYS) 346 error = EINVAL; 347 goto release_exit; 348 } 349 } 350 _s = pserialize_read_enter(); 351 IFADDR_READER_FOREACH(ifa, ifp) { 352 struct psref _psref; 353 if (af && af != ifa->ifa_addr->sa_family) 354 continue; 355 ifa_acquire(ifa, &_psref); 356 pserialize_read_exit(_s); 357 358 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 359 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 360 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 361 switch (type) { 362 case NET_RT_IFLIST: 363 error = sysctl_iflist_addr(w, ifa, &info); 364 break; 365 case NET_RT_OIFLIST: 366 case NET_RT_OOIFLIST: 367 case NET_RT_OOOIFLIST: 368 MODULE_HOOK_CALL(rtsock_iflist_70_hook, 369 (w, ifa, &info), enosys(), error); 370 break; 371 default: 372 error = EINVAL; 373 } 374 375 _s = pserialize_read_enter(); 376 ifa_release(ifa, &_psref); 377 if (error != 0) { 378 pserialize_read_exit(_s); 379 goto release_exit; 380 } 381 } 382 pserialize_read_exit(_s); 383 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 384 info.rti_info[RTAX_BRD] = NULL; 385 386 s = pserialize_read_enter(); 387 if_release(ifp, &psref); 388 } 389 pserialize_read_exit(s); 390 curlwp_bindx(bound); 391 392 return 0; 393 394 release_exit: 395 if_release(ifp, &psref); 396 curlwp_bindx(bound); 397 return error; 398 } 399 400 static int 401 sysctl_rtable(SYSCTLFN_ARGS) 402 { 403 void *where = oldp; 404 size_t *given = oldlenp; 405 int i, s, error = EINVAL; 406 u_char af; 407 struct rt_walkarg w; 408 409 if (namelen == 1 && name[0] == CTL_QUERY) 410 return sysctl_query(SYSCTLFN_CALL(rnode)); 411 412 if (newp) 413 return EPERM; 414 if (namelen != 3) 415 return EINVAL; 416 af = name[0]; 417 w.w_tmemneeded = 0; 418 w.w_tmemsize = 0; 419 w.w_tmem = NULL; 420 again: 421 /* we may return here if a later [re]alloc of the t_mem buffer fails */ 422 if (w.w_tmemneeded) { 423 w.w_tmem = kmem_zalloc(w.w_tmemneeded, KM_SLEEP); 424 w.w_tmemsize = w.w_tmemneeded; 425 w.w_tmemneeded = 0; 426 } 427 w.w_op = name[1]; 428 w.w_arg = name[2]; 429 w.w_given = *given; 430 w.w_needed = 0 - w.w_given; 431 w.w_where = where; 432 433 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 434 s = splsoftnet(); 435 switch (w.w_op) { 436 437 case NET_RT_DUMP: 438 case NET_RT_FLAGS: 439 #if defined(INET) || defined(INET6) 440 /* 441 * take care of llinfo entries, the caller must 442 * specify an AF 443 */ 444 if (w.w_op == NET_RT_FLAGS && 445 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) { 446 if (af != 0) 447 error = lltable_sysctl_dump(af, &w); 448 else 449 error = EINVAL; 450 break; 451 } 452 #endif 453 454 for (i = 1; i <= AF_MAX; i++) { 455 if (af == 0 || af == i) { 456 error = rt_walktree(i, sysctl_dumpentry, &w); 457 if (error != 0) 458 break; 459 #if defined(INET) || defined(INET6) 460 /* 461 * Return ARP/NDP entries too for 462 * backward compatibility. 463 */ 464 error = lltable_sysctl_dump(i, &w); 465 if (error != 0) 466 break; 467 #endif 468 } 469 } 470 break; 471 472 case NET_RT_OOOIFLIST: /* compat_14 */ 473 case NET_RT_OOIFLIST: /* compat_50 */ 474 case NET_RT_OIFLIST: /* compat_70 */ 475 case NET_RT_IFLIST: /* current */ 476 error = sysctl_iflist(af, &w, w.w_op); 477 break; 478 } 479 splx(s); 480 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 481 482 /* check to see if we couldn't allocate memory with NOWAIT */ 483 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded) 484 goto again; 485 486 if (w.w_tmem) 487 kmem_free(w.w_tmem, w.w_tmemsize); 488 w.w_needed += w.w_given; 489 if (where) { 490 *given = (char *)w.w_where - (char *)where; 491 if (*given < w.w_needed) 492 return ENOMEM; 493 } else { 494 *given = (11 * w.w_needed) / 10; 495 } 496 return error; 497 } 498 499 void 500 sysctl_net_route_setup(struct sysctllog **clog, int pf, const char *name) 501 { 502 const struct sysctlnode *rnode = NULL; 503 504 sysctl_createv(clog, 0, NULL, &rnode, 505 CTLFLAG_PERMANENT, 506 CTLTYPE_NODE, name, 507 SYSCTL_DESCR("PF_ROUTE information"), 508 NULL, 0, NULL, 0, 509 CTL_NET, pf, CTL_EOL); 510 511 sysctl_createv(clog, 0, NULL, NULL, 512 CTLFLAG_PERMANENT, 513 CTLTYPE_NODE, "rtable", 514 SYSCTL_DESCR("Routing table information"), 515 sysctl_rtable, 0, NULL, 0, 516 CTL_NET, pf, 0 /* any protocol */, CTL_EOL); 517 518 sysctl_createv(clog, 0, &rnode, NULL, 519 CTLFLAG_PERMANENT, 520 CTLTYPE_STRUCT, "stats", 521 SYSCTL_DESCR("Routing statistics"), 522 NULL, 0, &rtstat, sizeof(rtstat), 523 CTL_CREATE, CTL_EOL); 524 } 525