1 /* $NetBSD: rtsock.c,v 1.174 2015/10/13 21:28:34 rjs 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.174 2015/10/13 21:28:34 rjs Exp $"); 65 66 #ifdef _KERNEL_OPT 67 #include "opt_inet.h" 68 #include "opt_mpls.h" 69 #include "opt_compat_netbsd.h" 70 #include "opt_sctp.h" 71 #endif 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/proc.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/domain.h> 79 #include <sys/protosw.h> 80 #include <sys/sysctl.h> 81 #include <sys/kauth.h> 82 #include <sys/kmem.h> 83 #include <sys/intr.h> 84 #ifdef RTSOCK_DEBUG 85 #include <netinet/in.h> 86 #endif /* RTSOCK_DEBUG */ 87 88 #include <net/if.h> 89 #include <net/route.h> 90 #include <net/raw_cb.h> 91 92 #include <netmpls/mpls.h> 93 94 #ifdef SCTP 95 extern void sctp_add_ip_address(struct ifaddr *); 96 extern void sctp_delete_ip_address(struct ifaddr *); 97 #endif 98 99 #if defined(COMPAT_14) || defined(COMPAT_50) 100 #include <compat/net/if.h> 101 #include <compat/net/route.h> 102 #endif 103 #ifdef COMPAT_RTSOCK 104 #define RTM_XVERSION RTM_OVERSION 105 #define RT_XADVANCE(a,b) RT_OADVANCE(a,b) 106 #define RT_XROUNDUP(n) RT_OROUNDUP(n) 107 #define PF_XROUTE PF_OROUTE 108 #define rt_xmsghdr rt_msghdr50 109 #define if_xmsghdr if_msghdr /* if_msghdr50 is for RTM_OIFINFO */ 110 #define ifa_xmsghdr ifa_msghdr50 111 #define if_xannouncemsghdr if_announcemsghdr50 112 #define COMPATNAME(x) compat_50_ ## x 113 #define DOMAINNAME "oroute" 114 CTASSERT(sizeof(struct ifa_xmsghdr) == 20); 115 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */ 116 #else /* COMPAT_RTSOCK */ 117 #define RTM_XVERSION RTM_VERSION 118 #define RT_XADVANCE(a,b) RT_ADVANCE(a,b) 119 #define RT_XROUNDUP(n) RT_ROUNDUP(n) 120 #define PF_XROUTE PF_ROUTE 121 #define rt_xmsghdr rt_msghdr 122 #define if_xmsghdr if_msghdr 123 #define ifa_xmsghdr ifa_msghdr 124 #define if_xannouncemsghdr if_announcemsghdr 125 #define COMPATNAME(x) x 126 #define DOMAINNAME "route" 127 CTASSERT(sizeof(struct ifa_xmsghdr) == 24); 128 #ifdef COMPAT_50 129 #define COMPATCALL(name, args) compat_50_ ## name args 130 #endif 131 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */ 132 #undef COMPAT_50 133 #undef COMPAT_14 134 #endif /* COMPAT_RTSOCK */ 135 136 #ifndef COMPATCALL 137 #define COMPATCALL(name, args) do { } while (/*CONSTCOND*/ 0) 138 #endif 139 140 #ifdef RTSOCK_DEBUG 141 #define RT_IN_PRINT(b, a) (in_print((b), sizeof(b), \ 142 &((const struct sockaddr_in *)info.rti_info[(a)])->sin_addr), (b)) 143 #endif /* RTSOCK_DEBUG */ 144 145 struct route_info COMPATNAME(route_info) = { 146 .ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, }, 147 .ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, }, 148 .ri_maxqlen = IFQ_MAXLEN, 149 }; 150 151 #define PRESERVED_RTF (RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK) 152 153 static void COMPATNAME(route_init)(void); 154 static int COMPATNAME(route_output)(struct mbuf *, ...); 155 156 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *); 157 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *); 158 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int, 159 struct rt_addrinfo *); 160 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *); 161 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *); 162 static void sysctl_net_route_setup(struct sysctllog **); 163 static int sysctl_dumpentry(struct rtentry *, void *); 164 static int sysctl_iflist(int, struct rt_walkarg *, int); 165 static int sysctl_rtable(SYSCTLFN_PROTO); 166 static void rt_adjustcount(int, int); 167 168 static void 169 rt_adjustcount(int af, int cnt) 170 { 171 struct route_cb * const cb = &COMPATNAME(route_info).ri_cb; 172 173 cb->any_count += cnt; 174 175 switch (af) { 176 case AF_INET: 177 cb->ip_count += cnt; 178 return; 179 #ifdef INET6 180 case AF_INET6: 181 cb->ip6_count += cnt; 182 return; 183 #endif 184 case AF_MPLS: 185 cb->mpls_count += cnt; 186 return; 187 } 188 } 189 190 static int 191 COMPATNAME(route_attach)(struct socket *so, int proto) 192 { 193 struct rawcb *rp; 194 int s, error; 195 196 KASSERT(sotorawcb(so) == NULL); 197 rp = kmem_zalloc(sizeof(*rp), KM_SLEEP); 198 rp->rcb_len = sizeof(*rp); 199 so->so_pcb = rp; 200 201 s = splsoftnet(); 202 if ((error = raw_attach(so, proto)) == 0) { 203 rt_adjustcount(rp->rcb_proto.sp_protocol, 1); 204 rp->rcb_laddr = &COMPATNAME(route_info).ri_src; 205 rp->rcb_faddr = &COMPATNAME(route_info).ri_dst; 206 } 207 splx(s); 208 209 if (error) { 210 kmem_free(rp, sizeof(*rp)); 211 so->so_pcb = NULL; 212 return error; 213 } 214 215 soisconnected(so); 216 so->so_options |= SO_USELOOPBACK; 217 KASSERT(solocked(so)); 218 219 return error; 220 } 221 222 static void 223 COMPATNAME(route_detach)(struct socket *so) 224 { 225 struct rawcb *rp = sotorawcb(so); 226 int s; 227 228 KASSERT(rp != NULL); 229 KASSERT(solocked(so)); 230 231 s = splsoftnet(); 232 rt_adjustcount(rp->rcb_proto.sp_protocol, -1); 233 raw_detach(so); 234 splx(s); 235 } 236 237 static int 238 COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam) 239 { 240 KASSERT(solocked(so)); 241 242 panic("route_accept"); 243 244 return EOPNOTSUPP; 245 } 246 247 static int 248 COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l) 249 { 250 KASSERT(solocked(so)); 251 252 return EOPNOTSUPP; 253 } 254 255 static int 256 COMPATNAME(route_listen)(struct socket *so, struct lwp *l) 257 { 258 KASSERT(solocked(so)); 259 260 return EOPNOTSUPP; 261 } 262 263 static int 264 COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l) 265 { 266 KASSERT(solocked(so)); 267 268 return EOPNOTSUPP; 269 } 270 271 static int 272 COMPATNAME(route_connect2)(struct socket *so, struct socket *so2) 273 { 274 KASSERT(solocked(so)); 275 276 return EOPNOTSUPP; 277 } 278 279 static int 280 COMPATNAME(route_disconnect)(struct socket *so) 281 { 282 struct rawcb *rp = sotorawcb(so); 283 int s; 284 285 KASSERT(solocked(so)); 286 KASSERT(rp != NULL); 287 288 s = splsoftnet(); 289 soisdisconnected(so); 290 raw_disconnect(rp); 291 splx(s); 292 293 return 0; 294 } 295 296 static int 297 COMPATNAME(route_shutdown)(struct socket *so) 298 { 299 int s; 300 301 KASSERT(solocked(so)); 302 303 /* 304 * Mark the connection as being incapable of further input. 305 */ 306 s = splsoftnet(); 307 socantsendmore(so); 308 splx(s); 309 return 0; 310 } 311 312 static int 313 COMPATNAME(route_abort)(struct socket *so) 314 { 315 KASSERT(solocked(so)); 316 317 panic("route_abort"); 318 319 return EOPNOTSUPP; 320 } 321 322 static int 323 COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam, 324 struct ifnet * ifp) 325 { 326 return EOPNOTSUPP; 327 } 328 329 static int 330 COMPATNAME(route_stat)(struct socket *so, struct stat *ub) 331 { 332 KASSERT(solocked(so)); 333 334 return 0; 335 } 336 337 static int 338 COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam) 339 { 340 struct rawcb *rp = sotorawcb(so); 341 342 KASSERT(solocked(so)); 343 KASSERT(rp != NULL); 344 KASSERT(nam != NULL); 345 346 if (rp->rcb_faddr == NULL) 347 return ENOTCONN; 348 349 raw_setpeeraddr(rp, nam); 350 return 0; 351 } 352 353 static int 354 COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam) 355 { 356 struct rawcb *rp = sotorawcb(so); 357 358 KASSERT(solocked(so)); 359 KASSERT(rp != NULL); 360 KASSERT(nam != NULL); 361 362 if (rp->rcb_faddr == NULL) 363 return ENOTCONN; 364 365 raw_setsockaddr(rp, nam); 366 return 0; 367 } 368 369 static int 370 COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l) 371 { 372 KASSERT(solocked(so)); 373 374 return EOPNOTSUPP; 375 } 376 377 static int 378 COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags) 379 { 380 KASSERT(solocked(so)); 381 382 return EOPNOTSUPP; 383 } 384 385 static int 386 COMPATNAME(route_send)(struct socket *so, struct mbuf *m, 387 struct sockaddr *nam, struct mbuf *control, struct lwp *l) 388 { 389 int error = 0; 390 int s; 391 392 KASSERT(solocked(so)); 393 394 s = splsoftnet(); 395 error = raw_send(so, m, nam, control, l); 396 splx(s); 397 398 return error; 399 } 400 401 static int 402 COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m, 403 struct mbuf *control) 404 { 405 KASSERT(solocked(so)); 406 407 m_freem(m); 408 m_freem(control); 409 410 return EOPNOTSUPP; 411 } 412 static int 413 COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp) 414 { 415 416 panic("route_purgeif"); 417 418 return EOPNOTSUPP; 419 } 420 421 /*ARGSUSED*/ 422 int 423 COMPATNAME(route_output)(struct mbuf *m, ...) 424 { 425 struct sockproto proto = { .sp_family = PF_XROUTE, }; 426 struct rt_xmsghdr *rtm = NULL; 427 struct rt_xmsghdr *old_rtm = NULL; 428 struct rtentry *rt = NULL; 429 struct rtentry *saved_nrt = NULL; 430 struct rt_addrinfo info; 431 int len, error = 0; 432 struct ifnet *ifp = NULL; 433 struct ifaddr *ifa = NULL; 434 struct socket *so; 435 va_list ap; 436 sa_family_t family; 437 438 va_start(ap, m); 439 so = va_arg(ap, struct socket *); 440 va_end(ap); 441 442 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0) 443 if (m == NULL || ((m->m_len < sizeof(int32_t)) && 444 (m = m_pullup(m, sizeof(int32_t))) == NULL)) 445 return ENOBUFS; 446 if ((m->m_flags & M_PKTHDR) == 0) 447 panic("%s", __func__); 448 len = m->m_pkthdr.len; 449 if (len < sizeof(*rtm) || 450 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) { 451 info.rti_info[RTAX_DST] = NULL; 452 senderr(EINVAL); 453 } 454 R_Malloc(rtm, struct rt_xmsghdr *, len); 455 if (rtm == NULL) { 456 info.rti_info[RTAX_DST] = NULL; 457 senderr(ENOBUFS); 458 } 459 m_copydata(m, 0, len, rtm); 460 if (rtm->rtm_version != RTM_XVERSION) { 461 info.rti_info[RTAX_DST] = NULL; 462 senderr(EPROTONOSUPPORT); 463 } 464 rtm->rtm_pid = curproc->p_pid; 465 memset(&info, 0, sizeof(info)); 466 info.rti_addrs = rtm->rtm_addrs; 467 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm, 468 &info)) { 469 senderr(EINVAL); 470 } 471 info.rti_flags = rtm->rtm_flags; 472 #ifdef RTSOCK_DEBUG 473 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) { 474 char abuf[INET_ADDRSTRLEN]; 475 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__, 476 RT_IN_PRINT(abuf, RTAX_DST)); 477 } 478 #endif /* RTSOCK_DEBUG */ 479 if (info.rti_info[RTAX_DST] == NULL || 480 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) { 481 senderr(EINVAL); 482 } 483 if (info.rti_info[RTAX_GATEWAY] != NULL && 484 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) { 485 senderr(EINVAL); 486 } 487 488 /* 489 * Verify that the caller has the appropriate privilege; RTM_GET 490 * is the only operation the non-superuser is allowed. 491 */ 492 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE, 493 0, rtm, NULL, NULL) != 0) 494 senderr(EACCES); 495 496 switch (rtm->rtm_type) { 497 498 case RTM_ADD: 499 if (info.rti_info[RTAX_GATEWAY] == NULL) { 500 senderr(EINVAL); 501 } 502 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt); 503 if (error == 0) { 504 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt); 505 rtfree(saved_nrt); 506 } 507 break; 508 509 case RTM_DELETE: 510 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt); 511 if (error == 0) { 512 rt = saved_nrt; 513 goto report; 514 } 515 break; 516 517 case RTM_GET: 518 case RTM_CHANGE: 519 case RTM_LOCK: 520 /* XXX This will mask info.rti_info[RTAX_DST] with 521 * info.rti_info[RTAX_NETMASK] before 522 * searching. It did not used to do that. --dyoung 523 */ 524 rt = NULL; 525 error = rtrequest1(RTM_GET, &info, &rt); 526 if (error != 0) 527 senderr(error); 528 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */ 529 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt), 530 info.rti_info[RTAX_DST]->sa_len) != 0) 531 senderr(ESRCH); 532 if (info.rti_info[RTAX_NETMASK] == NULL && 533 rt_mask(rt) != NULL) 534 senderr(ETOOMANYREFS); 535 } 536 537 switch (rtm->rtm_type) { 538 case RTM_GET: 539 report: 540 info.rti_info[RTAX_DST] = rt_getkey(rt); 541 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 542 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 543 info.rti_info[RTAX_TAG] = rt_gettag(rt); 544 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0) 545 ; 546 else if ((ifp = rt->rt_ifp) != NULL) { 547 const struct ifaddr *rtifa; 548 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr; 549 /* rtifa used to be simply rt->rt_ifa. 550 * If rt->rt_ifa != NULL, then 551 * rt_get_ifa() != NULL. So this 552 * ought to still be safe. --dyoung 553 */ 554 rtifa = rt_get_ifa(rt); 555 info.rti_info[RTAX_IFA] = rtifa->ifa_addr; 556 #ifdef RTSOCK_DEBUG 557 if (info.rti_info[RTAX_IFA]->sa_family == 558 AF_INET) { 559 char ibuf[INET_ADDRSTRLEN]; 560 char abuf[INET_ADDRSTRLEN]; 561 printf("%s: copying out RTAX_IFA %s " 562 "for info.rti_info[RTAX_DST] %s " 563 "ifa_getifa %p ifa_seqno %p\n", 564 __func__, 565 RT_IN_PRINT(ibuf, RTAX_IFA), 566 RT_IN_PRINT(abuf, RTAX_DST), 567 (void *)rtifa->ifa_getifa, 568 rtifa->ifa_seqno); 569 } 570 #endif /* RTSOCK_DEBUG */ 571 if (ifp->if_flags & IFF_POINTOPOINT) { 572 info.rti_info[RTAX_BRD] = 573 rtifa->ifa_dstaddr; 574 } else 575 info.rti_info[RTAX_BRD] = NULL; 576 rtm->rtm_index = ifp->if_index; 577 } else { 578 info.rti_info[RTAX_IFP] = NULL; 579 info.rti_info[RTAX_IFA] = NULL; 580 } 581 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len); 582 if (len > rtm->rtm_msglen) { 583 old_rtm = rtm; 584 R_Malloc(rtm, struct rt_xmsghdr *, len); 585 if (rtm == NULL) 586 senderr(ENOBUFS); 587 (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen); 588 } 589 (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0); 590 rtm->rtm_flags = rt->rt_flags; 591 rtm_setmetrics(rt, rtm); 592 rtm->rtm_addrs = info.rti_addrs; 593 break; 594 595 case RTM_CHANGE: 596 /* 597 * new gateway could require new ifaddr, ifp; 598 * flags may also be different; ifp may be specified 599 * by ll sockaddr when protocol address is ambiguous 600 */ 601 if ((error = rt_getifa(&info)) != 0) 602 senderr(error); 603 if (info.rti_info[RTAX_GATEWAY] && 604 rt_setgate(rt, info.rti_info[RTAX_GATEWAY])) 605 senderr(EDQUOT); 606 if (info.rti_info[RTAX_TAG]) 607 rt_settag(rt, info.rti_info[RTAX_TAG]); 608 /* new gateway could require new ifaddr, ifp; 609 flags may also be different; ifp may be specified 610 by ll sockaddr when protocol address is ambiguous */ 611 if (info.rti_info[RTAX_IFP] && 612 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) && 613 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] || 614 info.rti_info[RTAX_GATEWAY])) { 615 if (info.rti_info[RTAX_IFA] == NULL || 616 (ifa = ifa_ifwithaddr( 617 info.rti_info[RTAX_IFA])) == NULL) 618 ifa = ifaof_ifpforaddr( 619 info.rti_info[RTAX_IFA] ? 620 info.rti_info[RTAX_IFA] : 621 info.rti_info[RTAX_GATEWAY], ifp); 622 } else if ((info.rti_info[RTAX_IFA] && 623 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) || 624 (info.rti_info[RTAX_GATEWAY] && 625 (ifa = ifa_ifwithroute(rt->rt_flags, 626 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) { 627 ifp = ifa->ifa_ifp; 628 } 629 if (ifa) { 630 struct ifaddr *oifa = rt->rt_ifa; 631 if (oifa != ifa) { 632 if (oifa && oifa->ifa_rtrequest) { 633 oifa->ifa_rtrequest(RTM_DELETE, 634 rt, &info); 635 } 636 rt_replace_ifa(rt, ifa); 637 rt->rt_ifp = ifp; 638 } 639 } 640 if (ifp && rt->rt_ifp != ifp) 641 rt->rt_ifp = ifp; 642 rt_setmetrics(rtm->rtm_inits, rtm, rt); 643 if (rt->rt_flags != info.rti_flags) 644 rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF) 645 | (rt->rt_flags & PRESERVED_RTF); 646 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 647 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 648 /*FALLTHROUGH*/ 649 case RTM_LOCK: 650 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 651 rt->rt_rmx.rmx_locks |= 652 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 653 break; 654 } 655 break; 656 657 default: 658 senderr(EOPNOTSUPP); 659 } 660 661 flush: 662 if (rtm) { 663 if (error) 664 rtm->rtm_errno = error; 665 else 666 rtm->rtm_flags |= RTF_DONE; 667 } 668 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family : 669 0; 670 /* We cannot free old_rtm until we have stopped using the 671 * pointers in info, some of which may point to sockaddrs 672 * in old_rtm. 673 */ 674 if (old_rtm != NULL) 675 Free(old_rtm); 676 if (rt) 677 rtfree(rt); 678 { 679 struct rawcb *rp = NULL; 680 /* 681 * Check to see if we don't want our own messages. 682 */ 683 if ((so->so_options & SO_USELOOPBACK) == 0) { 684 if (COMPATNAME(route_info).ri_cb.any_count <= 1) { 685 if (rtm) 686 Free(rtm); 687 m_freem(m); 688 return error; 689 } 690 /* There is another listener, so construct message */ 691 rp = sotorawcb(so); 692 } 693 if (rtm) { 694 m_copyback(m, 0, rtm->rtm_msglen, rtm); 695 if (m->m_pkthdr.len < rtm->rtm_msglen) { 696 m_freem(m); 697 m = NULL; 698 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 699 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 700 Free(rtm); 701 } 702 if (rp) 703 rp->rcb_proto.sp_family = 0; /* Avoid us */ 704 if (family) 705 proto.sp_protocol = family; 706 if (m) 707 raw_input(m, &proto, &COMPATNAME(route_info).ri_src, 708 &COMPATNAME(route_info).ri_dst); 709 if (rp) 710 rp->rcb_proto.sp_family = PF_XROUTE; 711 } 712 return error; 713 } 714 715 static void 716 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out) 717 { 718 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e; 719 metric(RTV_RPIPE, rmx_recvpipe); 720 metric(RTV_SPIPE, rmx_sendpipe); 721 metric(RTV_SSTHRESH, rmx_ssthresh); 722 metric(RTV_RTT, rmx_rtt); 723 metric(RTV_RTTVAR, rmx_rttvar); 724 metric(RTV_HOPCOUNT, rmx_hopcount); 725 metric(RTV_MTU, rmx_mtu); 726 #undef metric 727 if (which & RTV_EXPIRE) { 728 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ? 729 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0; 730 } 731 } 732 733 static void 734 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out) 735 { 736 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e; 737 metric(rmx_recvpipe); 738 metric(rmx_sendpipe); 739 metric(rmx_ssthresh); 740 metric(rmx_rtt); 741 metric(rmx_rttvar); 742 metric(rmx_hopcount); 743 metric(rmx_mtu); 744 #undef metric 745 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ? 746 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0; 747 } 748 749 static int 750 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim, 751 struct rt_addrinfo *rtinfo) 752 { 753 const struct sockaddr *sa = NULL; /* Quell compiler warning */ 754 int i; 755 756 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 757 if ((rtinfo->rti_addrs & (1 << i)) == 0) 758 continue; 759 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp; 760 RT_XADVANCE(cp, sa); 761 } 762 763 /* 764 * Check for extra addresses specified, except RTM_GET asking 765 * for interface info. 766 */ 767 if (rtmtype == RTM_GET) { 768 if (((rtinfo->rti_addrs & 769 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0) 770 return 1; 771 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0) 772 return 1; 773 /* Check for bad data length. */ 774 if (cp != cplim) { 775 if (i == RTAX_NETMASK + 1 && sa != NULL && 776 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim) 777 /* 778 * The last sockaddr was info.rti_info[RTAX_NETMASK]. 779 * We accept this for now for the sake of old 780 * binaries or third party softwares. 781 */ 782 ; 783 else 784 return 1; 785 } 786 return 0; 787 } 788 789 static int 790 rt_getlen(int type) 791 { 792 #ifndef COMPAT_RTSOCK 793 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t)); 794 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t)); 795 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t)); 796 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t)); 797 #endif 798 799 switch (type) { 800 case RTM_DELADDR: 801 case RTM_NEWADDR: 802 case RTM_CHGADDR: 803 return sizeof(struct ifa_xmsghdr); 804 805 case RTM_OOIFINFO: 806 #ifdef COMPAT_14 807 return sizeof(struct if_msghdr14); 808 #else 809 #ifdef DIAGNOSTIC 810 printf("RTM_OOIFINFO\n"); 811 #endif 812 return -1; 813 #endif 814 case RTM_OIFINFO: 815 #ifdef COMPAT_50 816 return sizeof(struct if_msghdr50); 817 #else 818 #ifdef DIAGNOSTIC 819 printf("RTM_OIFINFO\n"); 820 #endif 821 return -1; 822 #endif 823 824 case RTM_IFINFO: 825 return sizeof(struct if_xmsghdr); 826 827 case RTM_IFANNOUNCE: 828 case RTM_IEEE80211: 829 return sizeof(struct if_xannouncemsghdr); 830 831 default: 832 return sizeof(struct rt_xmsghdr); 833 } 834 } 835 836 837 struct mbuf * 838 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen) 839 { 840 struct rt_xmsghdr *rtm; 841 struct mbuf *m; 842 int i; 843 const struct sockaddr *sa; 844 int len, dlen; 845 846 m = m_gethdr(M_DONTWAIT, MT_DATA); 847 if (m == NULL) 848 return m; 849 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner); 850 851 if ((len = rt_getlen(type)) == -1) 852 goto out; 853 if (len > MHLEN + MLEN) 854 panic("%s: message too long", __func__); 855 else if (len > MHLEN) { 856 m->m_next = m_get(M_DONTWAIT, MT_DATA); 857 if (m->m_next == NULL) 858 goto out; 859 MCLAIM(m->m_next, m->m_owner); 860 m->m_pkthdr.len = len; 861 m->m_len = MHLEN; 862 m->m_next->m_len = len - MHLEN; 863 } else { 864 m->m_pkthdr.len = m->m_len = len; 865 } 866 m->m_pkthdr.rcvif = NULL; 867 m_copyback(m, 0, datalen, data); 868 if (len > datalen) 869 (void)memset(mtod(m, char *) + datalen, 0, len - datalen); 870 rtm = mtod(m, struct rt_xmsghdr *); 871 for (i = 0; i < RTAX_MAX; i++) { 872 if ((sa = rtinfo->rti_info[i]) == NULL) 873 continue; 874 rtinfo->rti_addrs |= (1 << i); 875 dlen = RT_XROUNDUP(sa->sa_len); 876 m_copyback(m, len, sa->sa_len, sa); 877 if (dlen != sa->sa_len) { 878 /* 879 * Up to 6 + 1 nul's since roundup is to 880 * sizeof(uint64_t) (8 bytes) 881 */ 882 m_copyback(m, len + sa->sa_len, 883 dlen - sa->sa_len, "\0\0\0\0\0\0"); 884 } 885 len += dlen; 886 } 887 if (m->m_pkthdr.len != len) 888 goto out; 889 rtm->rtm_msglen = len; 890 rtm->rtm_version = RTM_XVERSION; 891 rtm->rtm_type = type; 892 return m; 893 out: 894 m_freem(m); 895 return NULL; 896 } 897 898 /* 899 * rt_msg2 900 * 901 * fills 'cp' or 'w'.w_tmem with the routing socket message and 902 * returns the length of the message in 'lenp'. 903 * 904 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold 905 * the message 906 * otherwise walkarg's w_needed is updated and if the user buffer is 907 * specified and w_needed indicates space exists the information is copied 908 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary, 909 * if the allocation fails ENOBUFS is returned. 910 */ 911 static int 912 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w, 913 int *lenp) 914 { 915 int i; 916 int len, dlen, second_time = 0; 917 char *cp0, *cp = cpv; 918 919 rtinfo->rti_addrs = 0; 920 again: 921 if ((len = rt_getlen(type)) == -1) 922 return EINVAL; 923 924 if ((cp0 = cp) != NULL) 925 cp += len; 926 for (i = 0; i < RTAX_MAX; i++) { 927 const struct sockaddr *sa; 928 929 if ((sa = rtinfo->rti_info[i]) == NULL) 930 continue; 931 rtinfo->rti_addrs |= (1 << i); 932 dlen = RT_XROUNDUP(sa->sa_len); 933 if (cp) { 934 int diff = dlen - sa->sa_len; 935 (void)memcpy(cp, sa, (size_t)sa->sa_len); 936 cp += sa->sa_len; 937 if (diff > 0) { 938 (void)memset(cp, 0, (size_t)diff); 939 cp += diff; 940 } 941 } 942 len += dlen; 943 } 944 if (cp == NULL && w != NULL && !second_time) { 945 struct rt_walkarg *rw = w; 946 947 rw->w_needed += len; 948 if (rw->w_needed <= 0 && rw->w_where) { 949 if (rw->w_tmemsize < len) { 950 if (rw->w_tmem) 951 free(rw->w_tmem, M_RTABLE); 952 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT); 953 if (rw->w_tmem) 954 rw->w_tmemsize = len; 955 else 956 rw->w_tmemsize = 0; 957 } 958 if (rw->w_tmem) { 959 cp = rw->w_tmem; 960 second_time = 1; 961 goto again; 962 } else { 963 rw->w_tmemneeded = len; 964 return ENOBUFS; 965 } 966 } 967 } 968 if (cp) { 969 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0; 970 971 rtm->rtm_version = RTM_XVERSION; 972 rtm->rtm_type = type; 973 rtm->rtm_msglen = len; 974 } 975 if (lenp) 976 *lenp = len; 977 return 0; 978 } 979 980 /* 981 * This routine is called to generate a message from the routing 982 * socket indicating that a redirect has occurred, a routing lookup 983 * has failed, or that a protocol has detected timeouts to a particular 984 * destination. 985 */ 986 void 987 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags, 988 int error) 989 { 990 struct rt_xmsghdr rtm; 991 struct mbuf *m; 992 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 993 struct rt_addrinfo info = *rtinfo; 994 995 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error)); 996 if (COMPATNAME(route_info).ri_cb.any_count == 0) 997 return; 998 memset(&rtm, 0, sizeof(rtm)); 999 rtm.rtm_flags = RTF_DONE | flags; 1000 rtm.rtm_errno = error; 1001 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm)); 1002 if (m == NULL) 1003 return; 1004 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs; 1005 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0); 1006 } 1007 1008 /* 1009 * This routine is called to generate a message from the routing 1010 * socket indicating that the status of a network interface has changed. 1011 */ 1012 void 1013 COMPATNAME(rt_ifmsg)(struct ifnet *ifp) 1014 { 1015 struct if_xmsghdr ifm; 1016 struct mbuf *m; 1017 struct rt_addrinfo info; 1018 1019 COMPATCALL(rt_ifmsg, (ifp)); 1020 if (COMPATNAME(route_info).ri_cb.any_count == 0) 1021 return; 1022 (void)memset(&info, 0, sizeof(info)); 1023 (void)memset(&ifm, 0, sizeof(ifm)); 1024 ifm.ifm_index = ifp->if_index; 1025 ifm.ifm_flags = ifp->if_flags; 1026 ifm.ifm_data = ifp->if_data; 1027 ifm.ifm_addrs = 0; 1028 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm)); 1029 if (m == NULL) 1030 return; 1031 COMPATNAME(route_enqueue)(m, 0); 1032 #ifdef COMPAT_14 1033 compat_14_rt_oifmsg(ifp); 1034 #endif 1035 #ifdef COMPAT_50 1036 compat_50_rt_oifmsg(ifp); 1037 #endif 1038 } 1039 1040 1041 /* 1042 * This is called to generate messages from the routing socket 1043 * indicating a network interface has had addresses associated with it. 1044 * if we ever reverse the logic and replace messages TO the routing 1045 * socket indicate a request to configure interfaces, then it will 1046 * be unnecessary as the routing socket will automatically generate 1047 * copies of it. 1048 */ 1049 void 1050 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error, 1051 struct rtentry *rt) 1052 { 1053 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass)) 1054 struct rt_addrinfo info; 1055 const struct sockaddr *sa; 1056 int pass; 1057 struct mbuf *m; 1058 struct ifnet *ifp; 1059 struct rt_xmsghdr rtm; 1060 struct ifa_xmsghdr ifam; 1061 int ncmd; 1062 1063 KASSERT(ifa != NULL); 1064 ifp = ifa->ifa_ifp; 1065 #ifdef SCTP 1066 if (cmd == RTM_ADD) { 1067 sctp_add_ip_address(ifa); 1068 } else if (cmd == RTM_DELETE) { 1069 sctp_delete_ip_address(ifa); 1070 } 1071 #endif 1072 1073 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt)); 1074 if (COMPATNAME(route_info).ri_cb.any_count == 0) 1075 return; 1076 for (pass = 1; pass < 3; pass++) { 1077 memset(&info, 0, sizeof(info)); 1078 switch (cmdpass(cmd, pass)) { 1079 case cmdpass(RTM_ADD, 1): 1080 case cmdpass(RTM_CHANGE, 1): 1081 case cmdpass(RTM_DELETE, 2): 1082 case cmdpass(RTM_NEWADDR, 1): 1083 case cmdpass(RTM_DELADDR, 1): 1084 case cmdpass(RTM_CHGADDR, 1): 1085 switch (cmd) { 1086 case RTM_ADD: 1087 ncmd = RTM_NEWADDR; 1088 break; 1089 case RTM_DELETE: 1090 ncmd = RTM_DELADDR; 1091 break; 1092 case RTM_CHANGE: 1093 ncmd = RTM_CHGADDR; 1094 break; 1095 default: 1096 ncmd = cmd; 1097 } 1098 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1099 KASSERT(ifp->if_dl != NULL); 1100 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr; 1101 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1102 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1103 memset(&ifam, 0, sizeof(ifam)); 1104 ifam.ifam_index = ifp->if_index; 1105 ifam.ifam_metric = ifa->ifa_metric; 1106 ifam.ifam_flags = ifa->ifa_flags; 1107 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam)); 1108 if (m == NULL) 1109 continue; 1110 mtod(m, struct ifa_xmsghdr *)->ifam_addrs = 1111 info.rti_addrs; 1112 break; 1113 case cmdpass(RTM_ADD, 2): 1114 case cmdpass(RTM_CHANGE, 2): 1115 case cmdpass(RTM_DELETE, 1): 1116 if (rt == NULL) 1117 continue; 1118 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1119 info.rti_info[RTAX_DST] = sa = rt_getkey(rt); 1120 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1121 memset(&rtm, 0, sizeof(rtm)); 1122 rtm.rtm_index = ifp->if_index; 1123 rtm.rtm_flags |= rt->rt_flags; 1124 rtm.rtm_errno = error; 1125 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm)); 1126 if (m == NULL) 1127 continue; 1128 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs; 1129 break; 1130 default: 1131 continue; 1132 } 1133 #ifdef DIAGNOSTIC 1134 if (m == NULL) 1135 panic("%s: called with wrong command", __func__); 1136 #endif 1137 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0); 1138 } 1139 #undef cmdpass 1140 } 1141 1142 static struct mbuf * 1143 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1144 struct rt_addrinfo *info) 1145 { 1146 struct if_xannouncemsghdr ifan; 1147 1148 memset(info, 0, sizeof(*info)); 1149 memset(&ifan, 0, sizeof(ifan)); 1150 ifan.ifan_index = ifp->if_index; 1151 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name)); 1152 ifan.ifan_what = what; 1153 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan)); 1154 } 1155 1156 /* 1157 * This is called to generate routing socket messages indicating 1158 * network interface arrival and departure. 1159 */ 1160 void 1161 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what) 1162 { 1163 struct mbuf *m; 1164 struct rt_addrinfo info; 1165 1166 COMPATCALL(rt_ifannouncemsg, (ifp, what)); 1167 if (COMPATNAME(route_info).ri_cb.any_count == 0) 1168 return; 1169 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1170 if (m == NULL) 1171 return; 1172 COMPATNAME(route_enqueue)(m, 0); 1173 } 1174 1175 /* 1176 * This is called to generate routing socket messages indicating 1177 * IEEE80211 wireless events. 1178 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1179 */ 1180 void 1181 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data, 1182 size_t data_len) 1183 { 1184 struct mbuf *m; 1185 struct rt_addrinfo info; 1186 1187 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len)); 1188 if (COMPATNAME(route_info).ri_cb.any_count == 0) 1189 return; 1190 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1191 if (m == NULL) 1192 return; 1193 /* 1194 * Append the ieee80211 data. Try to stick it in the 1195 * mbuf containing the ifannounce msg; otherwise allocate 1196 * a new mbuf and append. 1197 * 1198 * NB: we assume m is a single mbuf. 1199 */ 1200 if (data_len > M_TRAILINGSPACE(m)) { 1201 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1202 if (n == NULL) { 1203 m_freem(m); 1204 return; 1205 } 1206 (void)memcpy(mtod(n, void *), data, data_len); 1207 n->m_len = data_len; 1208 m->m_next = n; 1209 } else if (data_len > 0) { 1210 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len); 1211 m->m_len += data_len; 1212 } 1213 if (m->m_flags & M_PKTHDR) 1214 m->m_pkthdr.len += data_len; 1215 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len; 1216 COMPATNAME(route_enqueue)(m, 0); 1217 } 1218 1219 /* 1220 * This is used in dumping the kernel table via sysctl(). 1221 */ 1222 static int 1223 sysctl_dumpentry(struct rtentry *rt, void *v) 1224 { 1225 struct rt_walkarg *w = v; 1226 int error = 0, size; 1227 struct rt_addrinfo info; 1228 1229 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1230 return 0; 1231 memset(&info, 0, sizeof(info)); 1232 info.rti_info[RTAX_DST] = rt_getkey(rt); 1233 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1234 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1235 info.rti_info[RTAX_TAG] = rt_gettag(rt); 1236 if (rt->rt_ifp) { 1237 const struct ifaddr *rtifa; 1238 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr; 1239 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL, 1240 * then rt_get_ifa() != NULL. So this ought to still be safe. 1241 * --dyoung 1242 */ 1243 rtifa = rt_get_ifa(rt); 1244 info.rti_info[RTAX_IFA] = rtifa->ifa_addr; 1245 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1246 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr; 1247 } 1248 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size))) 1249 return error; 1250 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1251 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem; 1252 1253 rtm->rtm_flags = rt->rt_flags; 1254 rtm->rtm_use = rt->rt_use; 1255 rtm_setmetrics(rt, rtm); 1256 KASSERT(rt->rt_ifp != NULL); 1257 rtm->rtm_index = rt->rt_ifp->if_index; 1258 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1259 rtm->rtm_addrs = info.rti_addrs; 1260 if ((error = copyout(rtm, w->w_where, size)) != 0) 1261 w->w_where = NULL; 1262 else 1263 w->w_where = (char *)w->w_where + size; 1264 } 1265 return error; 1266 } 1267 1268 static int 1269 sysctl_iflist(int af, struct rt_walkarg *w, int type) 1270 { 1271 struct ifnet *ifp; 1272 struct ifaddr *ifa; 1273 struct rt_addrinfo info; 1274 int len, error = 0; 1275 1276 memset(&info, 0, sizeof(info)); 1277 IFNET_FOREACH(ifp) { 1278 if (w->w_arg && w->w_arg != ifp->if_index) 1279 continue; 1280 if (IFADDR_EMPTY(ifp)) 1281 continue; 1282 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr; 1283 switch (type) { 1284 case NET_RT_IFLIST: 1285 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len); 1286 break; 1287 #ifdef COMPAT_14 1288 case NET_RT_OOIFLIST: 1289 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len); 1290 break; 1291 #endif 1292 #ifdef COMPAT_50 1293 case NET_RT_OIFLIST: 1294 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len); 1295 break; 1296 #endif 1297 default: 1298 panic("sysctl_iflist(1)"); 1299 } 1300 if (error) 1301 return error; 1302 info.rti_info[RTAX_IFP] = NULL; 1303 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1304 switch (type) { 1305 case NET_RT_IFLIST: { 1306 struct if_xmsghdr *ifm; 1307 1308 ifm = (struct if_xmsghdr *)w->w_tmem; 1309 ifm->ifm_index = ifp->if_index; 1310 ifm->ifm_flags = ifp->if_flags; 1311 ifm->ifm_data = ifp->if_data; 1312 ifm->ifm_addrs = info.rti_addrs; 1313 error = copyout(ifm, w->w_where, len); 1314 if (error) 1315 return error; 1316 w->w_where = (char *)w->w_where + len; 1317 break; 1318 } 1319 1320 #ifdef COMPAT_14 1321 case NET_RT_OOIFLIST: 1322 error = compat_14_iflist(ifp, w, &info, len); 1323 if (error) 1324 return error; 1325 break; 1326 #endif 1327 #ifdef COMPAT_50 1328 case NET_RT_OIFLIST: 1329 error = compat_50_iflist(ifp, w, &info, len); 1330 if (error) 1331 return error; 1332 break; 1333 #endif 1334 default: 1335 panic("sysctl_iflist(2)"); 1336 } 1337 } 1338 IFADDR_FOREACH(ifa, ifp) { 1339 if (af && af != ifa->ifa_addr->sa_family) 1340 continue; 1341 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1342 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1343 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1344 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len))) 1345 return error; 1346 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1347 struct ifa_xmsghdr *ifam; 1348 1349 ifam = (struct ifa_xmsghdr *)w->w_tmem; 1350 ifam->ifam_index = ifa->ifa_ifp->if_index; 1351 ifam->ifam_flags = ifa->ifa_flags; 1352 ifam->ifam_metric = ifa->ifa_metric; 1353 ifam->ifam_addrs = info.rti_addrs; 1354 error = copyout(w->w_tmem, w->w_where, len); 1355 if (error) 1356 return error; 1357 w->w_where = (char *)w->w_where + len; 1358 } 1359 } 1360 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1361 info.rti_info[RTAX_BRD] = NULL; 1362 } 1363 return 0; 1364 } 1365 1366 static int 1367 sysctl_rtable(SYSCTLFN_ARGS) 1368 { 1369 void *where = oldp; 1370 size_t *given = oldlenp; 1371 int i, s, error = EINVAL; 1372 u_char af; 1373 struct rt_walkarg w; 1374 1375 if (namelen == 1 && name[0] == CTL_QUERY) 1376 return sysctl_query(SYSCTLFN_CALL(rnode)); 1377 1378 if (newp) 1379 return EPERM; 1380 if (namelen != 3) 1381 return EINVAL; 1382 af = name[0]; 1383 w.w_tmemneeded = 0; 1384 w.w_tmemsize = 0; 1385 w.w_tmem = NULL; 1386 again: 1387 /* we may return here if a later [re]alloc of the t_mem buffer fails */ 1388 if (w.w_tmemneeded) { 1389 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK); 1390 w.w_tmemsize = w.w_tmemneeded; 1391 w.w_tmemneeded = 0; 1392 } 1393 w.w_op = name[1]; 1394 w.w_arg = name[2]; 1395 w.w_given = *given; 1396 w.w_needed = 0 - w.w_given; 1397 w.w_where = where; 1398 1399 s = splsoftnet(); 1400 switch (w.w_op) { 1401 1402 case NET_RT_DUMP: 1403 case NET_RT_FLAGS: 1404 for (i = 1; i <= AF_MAX; i++) 1405 if ((af == 0 || af == i) && 1406 (error = rt_walktree(i, sysctl_dumpentry, &w))) 1407 break; 1408 break; 1409 1410 #ifdef COMPAT_14 1411 case NET_RT_OOIFLIST: 1412 error = sysctl_iflist(af, &w, w.w_op); 1413 break; 1414 #endif 1415 #ifdef COMPAT_50 1416 case NET_RT_OIFLIST: 1417 error = sysctl_iflist(af, &w, w.w_op); 1418 break; 1419 #endif 1420 case NET_RT_IFLIST: 1421 error = sysctl_iflist(af, &w, w.w_op); 1422 break; 1423 } 1424 splx(s); 1425 1426 /* check to see if we couldn't allocate memory with NOWAIT */ 1427 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded) 1428 goto again; 1429 1430 if (w.w_tmem) 1431 free(w.w_tmem, M_RTABLE); 1432 w.w_needed += w.w_given; 1433 if (where) { 1434 *given = (char *)w.w_where - (char *)where; 1435 if (*given < w.w_needed) 1436 return ENOMEM; 1437 } else { 1438 *given = (11 * w.w_needed) / 10; 1439 } 1440 return error; 1441 } 1442 1443 /* 1444 * Routing message software interrupt routine 1445 */ 1446 static void 1447 COMPATNAME(route_intr)(void *cookie) 1448 { 1449 struct sockproto proto = { .sp_family = PF_XROUTE, }; 1450 struct route_info * const ri = &COMPATNAME(route_info); 1451 struct mbuf *m; 1452 int s; 1453 1454 mutex_enter(softnet_lock); 1455 KERNEL_LOCK(1, NULL); 1456 while (!IF_IS_EMPTY(&ri->ri_intrq)) { 1457 s = splnet(); 1458 IF_DEQUEUE(&ri->ri_intrq, m); 1459 splx(s); 1460 if (m == NULL) 1461 break; 1462 proto.sp_protocol = M_GETCTX(m, uintptr_t); 1463 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst); 1464 } 1465 KERNEL_UNLOCK_ONE(NULL); 1466 mutex_exit(softnet_lock); 1467 } 1468 1469 /* 1470 * Enqueue a message to the software interrupt routine. 1471 */ 1472 void 1473 COMPATNAME(route_enqueue)(struct mbuf *m, int family) 1474 { 1475 struct route_info * const ri = &COMPATNAME(route_info); 1476 int s, wasempty; 1477 1478 s = splnet(); 1479 if (IF_QFULL(&ri->ri_intrq)) { 1480 IF_DROP(&ri->ri_intrq); 1481 m_freem(m); 1482 } else { 1483 wasempty = IF_IS_EMPTY(&ri->ri_intrq); 1484 M_SETCTX(m, (uintptr_t)family); 1485 IF_ENQUEUE(&ri->ri_intrq, m); 1486 if (wasempty) 1487 softint_schedule(ri->ri_sih); 1488 } 1489 splx(s); 1490 } 1491 1492 static void 1493 COMPATNAME(route_init)(void) 1494 { 1495 struct route_info * const ri = &COMPATNAME(route_info); 1496 1497 #ifndef COMPAT_RTSOCK 1498 rt_init(); 1499 #endif 1500 1501 sysctl_net_route_setup(NULL); 1502 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen; 1503 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE, 1504 COMPATNAME(route_intr), NULL); 1505 } 1506 1507 /* 1508 * Definitions of protocols supported in the ROUTE domain. 1509 */ 1510 #ifndef COMPAT_RTSOCK 1511 PR_WRAP_USRREQS(route); 1512 #else 1513 PR_WRAP_USRREQS(compat_50_route); 1514 #endif 1515 1516 static const struct pr_usrreqs route_usrreqs = { 1517 .pr_attach = COMPATNAME(route_attach_wrapper), 1518 .pr_detach = COMPATNAME(route_detach_wrapper), 1519 .pr_accept = COMPATNAME(route_accept_wrapper), 1520 .pr_bind = COMPATNAME(route_bind_wrapper), 1521 .pr_listen = COMPATNAME(route_listen_wrapper), 1522 .pr_connect = COMPATNAME(route_connect_wrapper), 1523 .pr_connect2 = COMPATNAME(route_connect2_wrapper), 1524 .pr_disconnect = COMPATNAME(route_disconnect_wrapper), 1525 .pr_shutdown = COMPATNAME(route_shutdown_wrapper), 1526 .pr_abort = COMPATNAME(route_abort_wrapper), 1527 .pr_ioctl = COMPATNAME(route_ioctl_wrapper), 1528 .pr_stat = COMPATNAME(route_stat_wrapper), 1529 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper), 1530 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper), 1531 .pr_rcvd = COMPATNAME(route_rcvd_wrapper), 1532 .pr_recvoob = COMPATNAME(route_recvoob_wrapper), 1533 .pr_send = COMPATNAME(route_send_wrapper), 1534 .pr_sendoob = COMPATNAME(route_sendoob_wrapper), 1535 .pr_purgeif = COMPATNAME(route_purgeif_wrapper), 1536 }; 1537 1538 static const struct protosw COMPATNAME(route_protosw)[] = { 1539 { 1540 .pr_type = SOCK_RAW, 1541 .pr_domain = &COMPATNAME(routedomain), 1542 .pr_flags = PR_ATOMIC|PR_ADDR, 1543 .pr_input = raw_input, 1544 .pr_output = COMPATNAME(route_output), 1545 .pr_ctlinput = raw_ctlinput, 1546 .pr_usrreqs = &route_usrreqs, 1547 .pr_init = raw_init, 1548 }, 1549 }; 1550 1551 struct domain COMPATNAME(routedomain) = { 1552 .dom_family = PF_XROUTE, 1553 .dom_name = DOMAINNAME, 1554 .dom_init = COMPATNAME(route_init), 1555 .dom_protosw = COMPATNAME(route_protosw), 1556 .dom_protoswNPROTOSW = 1557 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))], 1558 }; 1559 1560 static void 1561 sysctl_net_route_setup(struct sysctllog **clog) 1562 { 1563 const struct sysctlnode *rnode = NULL; 1564 1565 sysctl_createv(clog, 0, NULL, &rnode, 1566 CTLFLAG_PERMANENT, 1567 CTLTYPE_NODE, DOMAINNAME, 1568 SYSCTL_DESCR("PF_ROUTE information"), 1569 NULL, 0, NULL, 0, 1570 CTL_NET, PF_XROUTE, CTL_EOL); 1571 1572 sysctl_createv(clog, 0, NULL, NULL, 1573 CTLFLAG_PERMANENT, 1574 CTLTYPE_NODE, "rtable", 1575 SYSCTL_DESCR("Routing table information"), 1576 sysctl_rtable, 0, NULL, 0, 1577 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL); 1578 1579 sysctl_createv(clog, 0, &rnode, NULL, 1580 CTLFLAG_PERMANENT, 1581 CTLTYPE_STRUCT, "stats", 1582 SYSCTL_DESCR("Routing statistics"), 1583 NULL, 0, &rtstat, sizeof(rtstat), 1584 CTL_CREATE, CTL_EOL); 1585 } 1586