1 /* $OpenBSD: rtsock.c,v 1.321 2021/09/07 16:07:46 mvs Exp $ */ 2 /* $NetBSD: rtsock.c,v 1.18 1996/03/29 00:32:10 cgd 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 /* 34 * Copyright (c) 1988, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)rtsock.c 8.6 (Berkeley) 2/11/95 62 */ 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/proc.h> 67 #include <sys/sysctl.h> 68 #include <sys/mbuf.h> 69 #include <sys/socket.h> 70 #include <sys/socketvar.h> 71 #include <sys/domain.h> 72 #include <sys/pool.h> 73 #include <sys/protosw.h> 74 #include <sys/srp.h> 75 76 #include <net/if.h> 77 #include <net/if_dl.h> 78 #include <net/if_var.h> 79 #include <net/route.h> 80 81 #include <netinet/in.h> 82 83 #ifdef MPLS 84 #include <netmpls/mpls.h> 85 #endif 86 #ifdef IPSEC 87 #include <netinet/ip_ipsp.h> 88 #include <net/if_enc.h> 89 #endif 90 #ifdef BFD 91 #include <net/bfd.h> 92 #endif 93 94 #include <sys/stdarg.h> 95 #include <sys/kernel.h> 96 #include <sys/timeout.h> 97 98 #define ROUTESNDQ 8192 99 #define ROUTERCVQ 8192 100 101 const struct sockaddr route_src = { 2, PF_ROUTE, }; 102 103 struct walkarg { 104 int w_op, w_arg, w_given, w_needed, w_tmemsize; 105 caddr_t w_where, w_tmem; 106 }; 107 108 void route_prinit(void); 109 void rcb_ref(void *, void *); 110 void rcb_unref(void *, void *); 111 int route_output(struct mbuf *, struct socket *, struct sockaddr *, 112 struct mbuf *); 113 int route_ctloutput(int, struct socket *, int, int, struct mbuf *); 114 int route_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, 115 struct mbuf *, struct proc *); 116 void route_input(struct mbuf *m0, struct socket *, sa_family_t); 117 int route_arp_conflict(struct rtentry *, struct rt_addrinfo *); 118 int route_cleargateway(struct rtentry *, void *, unsigned int); 119 void rtm_senddesync_timer(void *); 120 void rtm_senddesync(struct socket *); 121 int rtm_sendup(struct socket *, struct mbuf *); 122 123 int rtm_getifa(struct rt_addrinfo *, unsigned int); 124 int rtm_output(struct rt_msghdr *, struct rtentry **, struct rt_addrinfo *, 125 uint8_t, unsigned int); 126 struct rt_msghdr *rtm_report(struct rtentry *, u_char, int, int); 127 struct mbuf *rtm_msg1(int, struct rt_addrinfo *); 128 int rtm_msg2(int, int, struct rt_addrinfo *, caddr_t, 129 struct walkarg *); 130 int rtm_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 131 int rtm_validate_proposal(struct rt_addrinfo *); 132 void rtm_setmetrics(u_long, const struct rt_metrics *, 133 struct rt_kmetrics *); 134 void rtm_getmetrics(const struct rt_kmetrics *, 135 struct rt_metrics *); 136 137 int sysctl_iflist(int, struct walkarg *); 138 int sysctl_ifnames(struct walkarg *); 139 int sysctl_rtable_rtstat(void *, size_t *, void *); 140 141 int rt_setsource(unsigned int, struct sockaddr *); 142 143 /* 144 * Locks used to protect struct members 145 * I immutable after creation 146 * s solock 147 */ 148 struct rtpcb { 149 struct socket *rop_socket; /* [I] */ 150 151 SRPL_ENTRY(rtpcb) rop_list; 152 struct refcnt rop_refcnt; 153 struct timeout rop_timeout; 154 unsigned int rop_msgfilter; /* [s] */ 155 unsigned int rop_flagfilter; /* [s] */ 156 unsigned int rop_flags; /* [s] */ 157 u_int rop_rtableid; /* [s] */ 158 unsigned short rop_proto; /* [I] */ 159 u_char rop_priority; /* [s] */ 160 }; 161 #define sotortpcb(so) ((struct rtpcb *)(so)->so_pcb) 162 163 struct rtptable { 164 SRPL_HEAD(, rtpcb) rtp_list; 165 struct srpl_rc rtp_rc; 166 struct rwlock rtp_lk; 167 unsigned int rtp_count; 168 }; 169 170 struct pool rtpcb_pool; 171 struct rtptable rtptable; 172 173 /* 174 * These flags and timeout are used for indicating to userland (via a 175 * RTM_DESYNC msg) when the route socket has overflowed and messages 176 * have been lost. 177 */ 178 #define ROUTECB_FLAG_DESYNC 0x1 /* Route socket out of memory */ 179 #define ROUTECB_FLAG_FLUSH 0x2 /* Wait until socket is empty before 180 queueing more packets */ 181 182 #define ROUTE_DESYNC_RESEND_TIMEOUT 200 /* In ms */ 183 184 void 185 route_prinit(void) 186 { 187 srpl_rc_init(&rtptable.rtp_rc, rcb_ref, rcb_unref, NULL); 188 rw_init(&rtptable.rtp_lk, "rtsock"); 189 SRPL_INIT(&rtptable.rtp_list); 190 pool_init(&rtpcb_pool, sizeof(struct rtpcb), 0, 191 IPL_SOFTNET, PR_WAITOK, "rtpcb", NULL); 192 } 193 194 void 195 rcb_ref(void *null, void *v) 196 { 197 struct rtpcb *rop = v; 198 199 refcnt_take(&rop->rop_refcnt); 200 } 201 202 void 203 rcb_unref(void *null, void *v) 204 { 205 struct rtpcb *rop = v; 206 207 refcnt_rele_wake(&rop->rop_refcnt); 208 } 209 210 int 211 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 212 struct mbuf *control, struct proc *p) 213 { 214 struct rtpcb *rop; 215 int error = 0; 216 217 if (req == PRU_CONTROL) 218 return (EOPNOTSUPP); 219 220 soassertlocked(so); 221 222 if (control && control->m_len) { 223 error = EOPNOTSUPP; 224 goto release; 225 } 226 227 rop = sotortpcb(so); 228 if (rop == NULL) { 229 error = EINVAL; 230 goto release; 231 } 232 233 switch (req) { 234 /* no connect, bind, accept. Socket is connected from the start */ 235 case PRU_CONNECT: 236 case PRU_BIND: 237 case PRU_CONNECT2: 238 case PRU_LISTEN: 239 case PRU_ACCEPT: 240 error = EOPNOTSUPP; 241 break; 242 243 case PRU_DISCONNECT: 244 case PRU_ABORT: 245 soisdisconnected(so); 246 break; 247 case PRU_SHUTDOWN: 248 socantsendmore(so); 249 break; 250 case PRU_SENSE: 251 /* stat: don't bother with a blocksize. */ 252 break; 253 254 /* minimal support, just implement a fake peer address */ 255 case PRU_SOCKADDR: 256 error = EINVAL; 257 break; 258 case PRU_PEERADDR: 259 bcopy(&route_src, mtod(nam, caddr_t), route_src.sa_len); 260 nam->m_len = route_src.sa_len; 261 break; 262 263 case PRU_RCVD: 264 /* 265 * If we are in a FLUSH state, check if the buffer is 266 * empty so that we can clear the flag. 267 */ 268 if (((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0) && 269 ((sbspace(rop->rop_socket, &rop->rop_socket->so_rcv) == 270 rop->rop_socket->so_rcv.sb_hiwat))) 271 rop->rop_flags &= ~ROUTECB_FLAG_FLUSH; 272 break; 273 274 case PRU_RCVOOB: 275 case PRU_SENDOOB: 276 error = EOPNOTSUPP; 277 break; 278 case PRU_SEND: 279 if (nam) { 280 error = EISCONN; 281 break; 282 } 283 error = (*so->so_proto->pr_output)(m, so, NULL, NULL); 284 m = NULL; 285 break; 286 default: 287 panic("route_usrreq"); 288 } 289 290 release: 291 if (req != PRU_RCVD && req != PRU_RCVOOB && req != PRU_SENSE) { 292 m_freem(control); 293 m_freem(m); 294 } 295 return (error); 296 } 297 298 int 299 route_attach(struct socket *so, int proto) 300 { 301 struct rtpcb *rop; 302 int error; 303 304 error = soreserve(so, ROUTESNDQ, ROUTERCVQ); 305 if (error) 306 return (error); 307 /* 308 * use the rawcb but allocate a rtpcb, this 309 * code does not care about the additional fields 310 * and works directly on the raw socket. 311 */ 312 rop = pool_get(&rtpcb_pool, PR_WAITOK|PR_ZERO); 313 so->so_pcb = rop; 314 /* Init the timeout structure */ 315 timeout_set_proc(&rop->rop_timeout, rtm_senddesync_timer, so); 316 refcnt_init(&rop->rop_refcnt); 317 318 rop->rop_socket = so; 319 rop->rop_proto = proto; 320 321 rop->rop_rtableid = curproc->p_p->ps_rtableid; 322 323 soisconnected(so); 324 so->so_options |= SO_USELOOPBACK; 325 326 rw_enter(&rtptable.rtp_lk, RW_WRITE); 327 SRPL_INSERT_HEAD_LOCKED(&rtptable.rtp_rc, &rtptable.rtp_list, rop, 328 rop_list); 329 rtptable.rtp_count++; 330 rw_exit(&rtptable.rtp_lk); 331 332 return (0); 333 } 334 335 int 336 route_detach(struct socket *so) 337 { 338 struct rtpcb *rop; 339 340 soassertlocked(so); 341 342 rop = sotortpcb(so); 343 if (rop == NULL) 344 return (EINVAL); 345 346 rw_enter(&rtptable.rtp_lk, RW_WRITE); 347 348 rtptable.rtp_count--; 349 SRPL_REMOVE_LOCKED(&rtptable.rtp_rc, &rtptable.rtp_list, rop, rtpcb, 350 rop_list); 351 rw_exit(&rtptable.rtp_lk); 352 353 sounlock(so, SL_LOCKED); 354 355 /* wait for all references to drop */ 356 refcnt_finalize(&rop->rop_refcnt, "rtsockrefs"); 357 timeout_del_barrier(&rop->rop_timeout); 358 359 solock(so); 360 361 so->so_pcb = NULL; 362 KASSERT((so->so_state & SS_NOFDREF) == 0); 363 pool_put(&rtpcb_pool, rop); 364 365 return (0); 366 } 367 368 int 369 route_ctloutput(int op, struct socket *so, int level, int optname, 370 struct mbuf *m) 371 { 372 struct rtpcb *rop = sotortpcb(so); 373 int error = 0; 374 unsigned int tid, prio; 375 376 if (level != AF_ROUTE) 377 return (EINVAL); 378 379 switch (op) { 380 case PRCO_SETOPT: 381 switch (optname) { 382 case ROUTE_MSGFILTER: 383 if (m == NULL || m->m_len != sizeof(unsigned int)) 384 error = EINVAL; 385 else 386 rop->rop_msgfilter = *mtod(m, unsigned int *); 387 break; 388 case ROUTE_TABLEFILTER: 389 if (m == NULL || m->m_len != sizeof(unsigned int)) { 390 error = EINVAL; 391 break; 392 } 393 tid = *mtod(m, unsigned int *); 394 if (tid != RTABLE_ANY && !rtable_exists(tid)) 395 error = ENOENT; 396 else 397 rop->rop_rtableid = tid; 398 break; 399 case ROUTE_PRIOFILTER: 400 if (m == NULL || m->m_len != sizeof(unsigned int)) { 401 error = EINVAL; 402 break; 403 } 404 prio = *mtod(m, unsigned int *); 405 if (prio > RTP_MAX) 406 error = EINVAL; 407 else 408 rop->rop_priority = prio; 409 break; 410 case ROUTE_FLAGFILTER: 411 if (m == NULL || m->m_len != sizeof(unsigned int)) 412 error = EINVAL; 413 else 414 rop->rop_flagfilter = *mtod(m, unsigned int *); 415 break; 416 default: 417 error = ENOPROTOOPT; 418 break; 419 } 420 break; 421 case PRCO_GETOPT: 422 switch (optname) { 423 case ROUTE_MSGFILTER: 424 m->m_len = sizeof(unsigned int); 425 *mtod(m, unsigned int *) = rop->rop_msgfilter; 426 break; 427 case ROUTE_TABLEFILTER: 428 m->m_len = sizeof(unsigned int); 429 *mtod(m, unsigned int *) = rop->rop_rtableid; 430 break; 431 case ROUTE_PRIOFILTER: 432 m->m_len = sizeof(unsigned int); 433 *mtod(m, unsigned int *) = rop->rop_priority; 434 break; 435 case ROUTE_FLAGFILTER: 436 m->m_len = sizeof(unsigned int); 437 *mtod(m, unsigned int *) = rop->rop_flagfilter; 438 break; 439 default: 440 error = ENOPROTOOPT; 441 break; 442 } 443 } 444 return (error); 445 } 446 447 void 448 rtm_senddesync_timer(void *xso) 449 { 450 struct socket *so = xso; 451 int s; 452 453 s = solock(so); 454 rtm_senddesync(so); 455 sounlock(so, s); 456 } 457 458 void 459 rtm_senddesync(struct socket *so) 460 { 461 struct rtpcb *rop = sotortpcb(so); 462 struct mbuf *desync_mbuf; 463 464 soassertlocked(so); 465 466 /* 467 * Dying socket is disconnected by upper layer and there is 468 * no reason to send packet. Also we shouldn't reschedule 469 * timeout(9), otherwise timeout_del_barrier(9) can't help us. 470 */ 471 if ((so->so_state & SS_ISCONNECTED) == 0 || 472 (so->so_state & SS_CANTRCVMORE)) 473 return; 474 475 /* If we are in a DESYNC state, try to send a RTM_DESYNC packet */ 476 if ((rop->rop_flags & ROUTECB_FLAG_DESYNC) == 0) 477 return; 478 479 /* 480 * If we fail to alloc memory or if sbappendaddr() 481 * fails, re-add timeout and try again. 482 */ 483 desync_mbuf = rtm_msg1(RTM_DESYNC, NULL); 484 if (desync_mbuf != NULL) { 485 if (sbappendaddr(so, &so->so_rcv, &route_src, 486 desync_mbuf, NULL) != 0) { 487 rop->rop_flags &= ~ROUTECB_FLAG_DESYNC; 488 sorwakeup(rop->rop_socket); 489 return; 490 } 491 m_freem(desync_mbuf); 492 } 493 /* Re-add timeout to try sending msg again */ 494 timeout_add_msec(&rop->rop_timeout, ROUTE_DESYNC_RESEND_TIMEOUT); 495 } 496 497 void 498 route_input(struct mbuf *m0, struct socket *so0, sa_family_t sa_family) 499 { 500 struct socket *so; 501 struct rtpcb *rop; 502 struct rt_msghdr *rtm; 503 struct mbuf *m = m0; 504 struct srp_ref sr; 505 int s; 506 507 /* ensure that we can access the rtm_type via mtod() */ 508 if (m->m_len < offsetof(struct rt_msghdr, rtm_type) + 1) { 509 m_freem(m); 510 return; 511 } 512 513 SRPL_FOREACH(rop, &sr, &rtptable.rtp_list, rop_list) { 514 /* 515 * If route socket is bound to an address family only send 516 * messages that match the address family. Address family 517 * agnostic messages are always sent. 518 */ 519 if (sa_family != AF_UNSPEC && rop->rop_proto != AF_UNSPEC && 520 rop->rop_proto != sa_family) 521 continue; 522 523 524 so = rop->rop_socket; 525 s = solock(so); 526 527 /* 528 * Check to see if we don't want our own messages and 529 * if we can receive anything. 530 */ 531 if ((so0 == so && !(so0->so_options & SO_USELOOPBACK)) || 532 !(so->so_state & SS_ISCONNECTED) || 533 (so->so_state & SS_CANTRCVMORE)) 534 goto next; 535 536 /* filter messages that the process does not want */ 537 rtm = mtod(m, struct rt_msghdr *); 538 /* but RTM_DESYNC can't be filtered */ 539 if (rtm->rtm_type != RTM_DESYNC) { 540 if (rop->rop_msgfilter != 0 && 541 !(rop->rop_msgfilter & (1 << rtm->rtm_type))) 542 goto next; 543 if (ISSET(rop->rop_flagfilter, rtm->rtm_flags)) 544 goto next; 545 } 546 switch (rtm->rtm_type) { 547 case RTM_IFANNOUNCE: 548 case RTM_DESYNC: 549 /* no tableid */ 550 break; 551 case RTM_RESOLVE: 552 case RTM_NEWADDR: 553 case RTM_DELADDR: 554 case RTM_IFINFO: 555 case RTM_80211INFO: 556 case RTM_BFD: 557 /* check against rdomain id */ 558 if (rop->rop_rtableid != RTABLE_ANY && 559 rtable_l2(rop->rop_rtableid) != rtm->rtm_tableid) 560 goto next; 561 break; 562 default: 563 if (rop->rop_priority != 0 && 564 rop->rop_priority < rtm->rtm_priority) 565 goto next; 566 /* check against rtable id */ 567 if (rop->rop_rtableid != RTABLE_ANY && 568 rop->rop_rtableid != rtm->rtm_tableid) 569 goto next; 570 break; 571 } 572 573 /* 574 * Check to see if the flush flag is set. If so, don't queue 575 * any more messages until the flag is cleared. 576 */ 577 if ((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0) 578 goto next; 579 580 rtm_sendup(so, m); 581 next: 582 sounlock(so, s); 583 } 584 SRPL_LEAVE(&sr); 585 586 m_freem(m); 587 } 588 589 int 590 rtm_sendup(struct socket *so, struct mbuf *m0) 591 { 592 struct rtpcb *rop = sotortpcb(so); 593 struct mbuf *m; 594 595 soassertlocked(so); 596 597 m = m_copym(m0, 0, M_COPYALL, M_NOWAIT); 598 if (m == NULL) 599 return (ENOMEM); 600 601 if (sbspace(so, &so->so_rcv) < (2 * MSIZE) || 602 sbappendaddr(so, &so->so_rcv, &route_src, m, NULL) == 0) { 603 /* Flag socket as desync'ed and flush required */ 604 rop->rop_flags |= ROUTECB_FLAG_DESYNC | ROUTECB_FLAG_FLUSH; 605 rtm_senddesync(so); 606 m_freem(m); 607 return (ENOBUFS); 608 } 609 610 sorwakeup(so); 611 return (0); 612 } 613 614 struct rt_msghdr * 615 rtm_report(struct rtentry *rt, u_char type, int seq, int tableid) 616 { 617 struct rt_msghdr *rtm; 618 struct rt_addrinfo info; 619 struct sockaddr_rtlabel sa_rl; 620 struct sockaddr_in6 sa_mask; 621 #ifdef BFD 622 struct sockaddr_bfd sa_bfd; 623 #endif 624 struct ifnet *ifp = NULL; 625 int len; 626 627 bzero(&info, sizeof(info)); 628 info.rti_info[RTAX_DST] = rt_key(rt); 629 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 630 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 631 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 632 #ifdef BFD 633 if (rt->rt_flags & RTF_BFD) 634 info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd); 635 #endif 636 #ifdef MPLS 637 if (rt->rt_flags & RTF_MPLS) { 638 struct sockaddr_mpls sa_mpls; 639 640 bzero(&sa_mpls, sizeof(sa_mpls)); 641 sa_mpls.smpls_family = AF_MPLS; 642 sa_mpls.smpls_len = sizeof(sa_mpls); 643 sa_mpls.smpls_label = ((struct rt_mpls *) 644 rt->rt_llinfo)->mpls_label; 645 info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls; 646 info.rti_mpls = ((struct rt_mpls *) 647 rt->rt_llinfo)->mpls_operation; 648 } 649 #endif 650 ifp = if_get(rt->rt_ifidx); 651 if (ifp != NULL) { 652 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 653 info.rti_info[RTAX_IFA] = 654 rtable_getsource(tableid, info.rti_info[RTAX_DST]->sa_family); 655 if (info.rti_info[RTAX_IFA] == NULL) 656 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 657 if (ifp->if_flags & IFF_POINTOPOINT) 658 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 659 } 660 if_put(ifp); 661 /* RTAX_GENMASK, RTAX_AUTHOR, RTAX_SRCMASK ignored */ 662 663 /* build new route message */ 664 len = rtm_msg2(type, RTM_VERSION, &info, NULL, NULL); 665 rtm = malloc(len, M_RTABLE, M_WAITOK | M_ZERO); 666 667 rtm_msg2(type, RTM_VERSION, &info, (caddr_t)rtm, NULL); 668 rtm->rtm_type = type; 669 rtm->rtm_index = rt->rt_ifidx; 670 rtm->rtm_tableid = tableid; 671 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 672 rtm->rtm_flags = rt->rt_flags; 673 rtm->rtm_pid = curproc->p_p->ps_pid; 674 rtm->rtm_seq = seq; 675 rtm_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 676 rtm->rtm_addrs = info.rti_addrs; 677 #ifdef MPLS 678 rtm->rtm_mpls = info.rti_mpls; 679 #endif 680 return rtm; 681 } 682 683 int 684 route_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr, 685 struct mbuf *control) 686 { 687 struct rt_msghdr *rtm = NULL; 688 struct rtentry *rt = NULL; 689 struct rt_addrinfo info; 690 struct ifnet *ifp; 691 int len, seq, useloopback, error = 0; 692 u_int tableid; 693 u_int8_t prio; 694 u_char vers, type; 695 696 if (m == NULL || ((m->m_len < sizeof(int32_t)) && 697 (m = m_pullup(m, sizeof(int32_t))) == 0)) 698 return (ENOBUFS); 699 if ((m->m_flags & M_PKTHDR) == 0) 700 panic("route_output"); 701 702 useloopback = so->so_options & SO_USELOOPBACK; 703 704 /* 705 * The socket can't be closed concurrently because the file 706 * descriptor reference is still held. 707 */ 708 709 sounlock(so, SL_LOCKED); 710 711 len = m->m_pkthdr.len; 712 if (len < offsetof(struct rt_msghdr, rtm_hdrlen) + 1 || 713 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 714 error = EINVAL; 715 goto fail; 716 } 717 vers = mtod(m, struct rt_msghdr *)->rtm_version; 718 switch (vers) { 719 case RTM_VERSION: 720 if (len < sizeof(struct rt_msghdr)) { 721 error = EINVAL; 722 goto fail; 723 } 724 if (len > RTM_MAXSIZE) { 725 error = EMSGSIZE; 726 goto fail; 727 } 728 rtm = malloc(len, M_RTABLE, M_WAITOK); 729 m_copydata(m, 0, len, rtm); 730 break; 731 default: 732 error = EPROTONOSUPPORT; 733 goto fail; 734 } 735 736 /* Verify that the caller is sending an appropriate message early */ 737 switch (rtm->rtm_type) { 738 case RTM_ADD: 739 case RTM_DELETE: 740 case RTM_GET: 741 case RTM_CHANGE: 742 case RTM_PROPOSAL: 743 case RTM_SOURCE: 744 break; 745 default: 746 error = EOPNOTSUPP; 747 goto fail; 748 } 749 /* 750 * Verify that the header length is valid. 751 * All messages from userland start with a struct rt_msghdr. 752 */ 753 if (rtm->rtm_hdrlen == 0) /* old client */ 754 rtm->rtm_hdrlen = sizeof(struct rt_msghdr); 755 if (rtm->rtm_hdrlen < sizeof(struct rt_msghdr) || 756 len < rtm->rtm_hdrlen) { 757 error = EINVAL; 758 goto fail; 759 } 760 761 rtm->rtm_pid = curproc->p_p->ps_pid; 762 763 /* 764 * Verify that the caller has the appropriate privilege; RTM_GET 765 * is the only operation the non-superuser is allowed. 766 */ 767 if (rtm->rtm_type != RTM_GET && suser(curproc) != 0) { 768 error = EACCES; 769 goto fail; 770 } 771 tableid = rtm->rtm_tableid; 772 if (!rtable_exists(tableid)) { 773 if (rtm->rtm_type == RTM_ADD) { 774 if ((error = rtable_add(tableid)) != 0) 775 goto fail; 776 } else { 777 error = EINVAL; 778 goto fail; 779 } 780 } 781 782 /* Do not let userland play with kernel-only flags. */ 783 if ((rtm->rtm_flags & (RTF_LOCAL|RTF_BROADCAST)) != 0) { 784 error = EINVAL; 785 goto fail; 786 } 787 788 /* make sure that kernel-only bits are not set */ 789 rtm->rtm_priority &= RTP_MASK; 790 rtm->rtm_flags &= ~(RTF_DONE|RTF_CLONED|RTF_CACHED); 791 rtm->rtm_fmask &= RTF_FMASK; 792 793 if (rtm->rtm_priority != 0) { 794 if (rtm->rtm_priority > RTP_MAX || 795 rtm->rtm_priority == RTP_LOCAL) { 796 error = EINVAL; 797 goto fail; 798 } 799 prio = rtm->rtm_priority; 800 } else if (rtm->rtm_type != RTM_ADD) 801 prio = RTP_ANY; 802 else if (rtm->rtm_flags & RTF_STATIC) 803 prio = 0; 804 else 805 prio = RTP_DEFAULT; 806 807 bzero(&info, sizeof(info)); 808 info.rti_addrs = rtm->rtm_addrs; 809 if ((error = rtm_xaddrs(rtm->rtm_hdrlen + (caddr_t)rtm, 810 len + (caddr_t)rtm, &info)) != 0) 811 goto fail; 812 813 info.rti_flags = rtm->rtm_flags; 814 815 if (rtm->rtm_type != RTM_SOURCE && 816 rtm->rtm_type != RTM_PROPOSAL && 817 (info.rti_info[RTAX_DST] == NULL || 818 info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 819 (info.rti_info[RTAX_GATEWAY] != NULL && 820 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX) || 821 info.rti_info[RTAX_GENMASK] != NULL)) { 822 error = EINVAL; 823 goto fail; 824 } 825 #ifdef MPLS 826 info.rti_mpls = rtm->rtm_mpls; 827 #endif 828 829 if (info.rti_info[RTAX_GATEWAY] != NULL && 830 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 831 (info.rti_flags & RTF_CLONING) == 0) { 832 info.rti_flags |= RTF_LLINFO; 833 } 834 835 /* 836 * Validate RTM_PROPOSAL and pass it along or error out. 837 */ 838 if (rtm->rtm_type == RTM_PROPOSAL) { 839 if (rtm_validate_proposal(&info) == -1) { 840 error = EINVAL; 841 goto fail; 842 } 843 /* 844 * If this is a solicitation proposal forward request to 845 * all interfaces. Most handlers will ignore it but at least 846 * umb(4) will send a response to this event. 847 */ 848 if (rtm->rtm_priority == RTP_PROPOSAL_SOLICIT) { 849 NET_LOCK(); 850 TAILQ_FOREACH(ifp, &ifnet, if_list) { 851 ifp->if_rtrequest(ifp, RTM_PROPOSAL, NULL); 852 } 853 NET_UNLOCK(); 854 } 855 } else if (rtm->rtm_type == RTM_SOURCE) { 856 if (info.rti_info[RTAX_IFA] == NULL) { 857 error = EINVAL; 858 goto fail; 859 } 860 if ((error = 861 rt_setsource(tableid, info.rti_info[RTAX_IFA])) != 0) 862 goto fail; 863 } else { 864 error = rtm_output(rtm, &rt, &info, prio, tableid); 865 if (!error) { 866 type = rtm->rtm_type; 867 seq = rtm->rtm_seq; 868 free(rtm, M_RTABLE, len); 869 rtm = rtm_report(rt, type, seq, tableid); 870 len = rtm->rtm_msglen; 871 } 872 } 873 874 rtfree(rt); 875 if (error) { 876 rtm->rtm_errno = error; 877 } else { 878 rtm->rtm_flags |= RTF_DONE; 879 } 880 881 /* 882 * Check to see if we don't want our own messages. 883 */ 884 if (!useloopback) { 885 if (rtptable.rtp_count == 0) { 886 /* no other listener and no loopback of messages */ 887 goto fail; 888 } 889 } 890 if (m_copyback(m, 0, len, rtm, M_NOWAIT)) { 891 m_freem(m); 892 m = NULL; 893 } else if (m->m_pkthdr.len > len) 894 m_adj(m, len - m->m_pkthdr.len); 895 free(rtm, M_RTABLE, len); 896 if (m) 897 route_input(m, so, info.rti_info[RTAX_DST] ? 898 info.rti_info[RTAX_DST]->sa_family : AF_UNSPEC); 899 solock(so); 900 901 return (error); 902 fail: 903 free(rtm, M_RTABLE, len); 904 m_freem(m); 905 solock(so); 906 907 return (error); 908 } 909 910 int 911 rtm_output(struct rt_msghdr *rtm, struct rtentry **prt, 912 struct rt_addrinfo *info, uint8_t prio, unsigned int tableid) 913 { 914 struct rtentry *rt = *prt; 915 struct ifnet *ifp = NULL; 916 int plen, newgate = 0, error = 0; 917 918 switch (rtm->rtm_type) { 919 case RTM_ADD: 920 if (info->rti_info[RTAX_GATEWAY] == NULL) { 921 error = EINVAL; 922 break; 923 } 924 925 rt = rtable_match(tableid, info->rti_info[RTAX_DST], NULL); 926 if ((error = route_arp_conflict(rt, info))) { 927 rtfree(rt); 928 rt = NULL; 929 break; 930 } 931 932 /* 933 * We cannot go through a delete/create/insert cycle for 934 * cached route because this can lead to races in the 935 * receive path. Instead we update the L2 cache. 936 */ 937 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_CACHED)) { 938 ifp = if_get(rt->rt_ifidx); 939 if (ifp == NULL) { 940 rtfree(rt); 941 rt = NULL; 942 error = ESRCH; 943 break; 944 } 945 946 goto change; 947 } 948 949 rtfree(rt); 950 rt = NULL; 951 952 NET_LOCK(); 953 if ((error = rtm_getifa(info, tableid)) != 0) { 954 NET_UNLOCK(); 955 break; 956 } 957 error = rtrequest(RTM_ADD, info, prio, &rt, tableid); 958 NET_UNLOCK(); 959 if (error == 0) 960 rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 961 &rt->rt_rmx); 962 break; 963 case RTM_DELETE: 964 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 965 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 966 prio); 967 if (rt == NULL) { 968 error = ESRCH; 969 break; 970 } 971 972 /* 973 * If we got multipath routes, we require users to specify 974 * a matching gateway. 975 */ 976 if (ISSET(rt->rt_flags, RTF_MPATH) && 977 info->rti_info[RTAX_GATEWAY] == NULL) { 978 error = ESRCH; 979 break; 980 } 981 982 ifp = if_get(rt->rt_ifidx); 983 if (ifp == NULL) { 984 rtfree(rt); 985 rt = NULL; 986 error = ESRCH; 987 break; 988 } 989 990 /* 991 * Invalidate the cache of automagically created and 992 * referenced L2 entries to make sure that ``rt_gwroute'' 993 * pointer stays valid for other CPUs. 994 */ 995 if ((ISSET(rt->rt_flags, RTF_CACHED))) { 996 NET_LOCK(); 997 ifp->if_rtrequest(ifp, RTM_INVALIDATE, rt); 998 /* Reset the MTU of the gateway route. */ 999 rtable_walk(tableid, rt_key(rt)->sa_family, NULL, 1000 route_cleargateway, rt); 1001 NET_UNLOCK(); 1002 break; 1003 } 1004 1005 /* 1006 * Make sure that local routes are only modified by the 1007 * kernel. 1008 */ 1009 if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) { 1010 error = EINVAL; 1011 break; 1012 } 1013 1014 rtfree(rt); 1015 rt = NULL; 1016 1017 NET_LOCK(); 1018 error = rtrequest_delete(info, prio, ifp, &rt, tableid); 1019 NET_UNLOCK(); 1020 break; 1021 case RTM_CHANGE: 1022 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1023 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 1024 prio); 1025 /* 1026 * If we got multipath routes, we require users to specify 1027 * a matching gateway. 1028 */ 1029 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH) && 1030 (info->rti_info[RTAX_GATEWAY] == NULL)) { 1031 rtfree(rt); 1032 rt = NULL; 1033 } 1034 1035 /* 1036 * If RTAX_GATEWAY is the argument we're trying to 1037 * change, try to find a compatible route. 1038 */ 1039 if ((rt == NULL) && (info->rti_info[RTAX_GATEWAY] != NULL)) { 1040 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1041 info->rti_info[RTAX_NETMASK], NULL, prio); 1042 /* Ensure we don't pick a multipath one. */ 1043 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH)) { 1044 rtfree(rt); 1045 rt = NULL; 1046 } 1047 } 1048 1049 if (rt == NULL) { 1050 error = ESRCH; 1051 break; 1052 } 1053 1054 /* 1055 * Make sure that local routes are only modified by the 1056 * kernel. 1057 */ 1058 if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) { 1059 error = EINVAL; 1060 break; 1061 } 1062 1063 ifp = if_get(rt->rt_ifidx); 1064 if (ifp == NULL) { 1065 rtfree(rt); 1066 rt = NULL; 1067 error = ESRCH; 1068 break; 1069 } 1070 1071 /* 1072 * RTM_CHANGE needs a perfect match. 1073 */ 1074 plen = rtable_satoplen(info->rti_info[RTAX_DST]->sa_family, 1075 info->rti_info[RTAX_NETMASK]); 1076 if (rt_plen(rt) != plen) { 1077 error = ESRCH; 1078 break; 1079 } 1080 1081 if (info->rti_info[RTAX_GATEWAY] != NULL) 1082 if (rt->rt_gateway == NULL || 1083 bcmp(rt->rt_gateway, 1084 info->rti_info[RTAX_GATEWAY], 1085 info->rti_info[RTAX_GATEWAY]->sa_len)) { 1086 newgate = 1; 1087 } 1088 /* 1089 * Check reachable gateway before changing the route. 1090 * New gateway could require new ifaddr, ifp; 1091 * flags may also be different; ifp may be specified 1092 * by ll sockaddr when protocol address is ambiguous. 1093 */ 1094 if (newgate || info->rti_info[RTAX_IFP] != NULL || 1095 info->rti_info[RTAX_IFA] != NULL) { 1096 struct ifaddr *ifa = NULL; 1097 1098 NET_LOCK(); 1099 if ((error = rtm_getifa(info, tableid)) != 0) { 1100 NET_UNLOCK(); 1101 break; 1102 } 1103 ifa = info->rti_ifa; 1104 if (rt->rt_ifa != ifa) { 1105 ifp->if_rtrequest(ifp, RTM_DELETE, rt); 1106 ifafree(rt->rt_ifa); 1107 1108 ifa->ifa_refcnt++; 1109 rt->rt_ifa = ifa; 1110 rt->rt_ifidx = ifa->ifa_ifp->if_index; 1111 /* recheck link state after ifp change */ 1112 rt_if_linkstate_change(rt, ifa->ifa_ifp, 1113 tableid); 1114 } 1115 NET_UNLOCK(); 1116 } 1117 change: 1118 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1119 /* When updating the gateway, make sure it is valid. */ 1120 if (!newgate && rt->rt_gateway->sa_family != 1121 info->rti_info[RTAX_GATEWAY]->sa_family) { 1122 error = EINVAL; 1123 break; 1124 } 1125 1126 NET_LOCK(); 1127 error = rt_setgate(rt, 1128 info->rti_info[RTAX_GATEWAY], tableid); 1129 NET_UNLOCK(); 1130 if (error) 1131 break; 1132 } 1133 #ifdef MPLS 1134 if (rtm->rtm_flags & RTF_MPLS) { 1135 NET_LOCK(); 1136 error = rt_mpls_set(rt, 1137 info->rti_info[RTAX_SRC], info->rti_mpls); 1138 NET_UNLOCK(); 1139 if (error) 1140 break; 1141 } else if (newgate || (rtm->rtm_fmask & RTF_MPLS)) { 1142 NET_LOCK(); 1143 /* if gateway changed remove MPLS information */ 1144 rt_mpls_clear(rt); 1145 NET_UNLOCK(); 1146 } 1147 #endif 1148 1149 #ifdef BFD 1150 if (ISSET(rtm->rtm_flags, RTF_BFD)) { 1151 if ((error = bfdset(rt))) 1152 break; 1153 } else if (!ISSET(rtm->rtm_flags, RTF_BFD) && 1154 ISSET(rtm->rtm_fmask, RTF_BFD)) { 1155 bfdclear(rt); 1156 } 1157 #endif 1158 1159 NET_LOCK(); 1160 /* Hack to allow some flags to be toggled */ 1161 if (rtm->rtm_fmask) { 1162 /* MPLS flag it is set by rt_mpls_set() */ 1163 rtm->rtm_fmask &= ~RTF_MPLS; 1164 rtm->rtm_flags &= ~RTF_MPLS; 1165 rt->rt_flags = 1166 (rt->rt_flags & ~rtm->rtm_fmask) | 1167 (rtm->rtm_flags & rtm->rtm_fmask); 1168 } 1169 rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx); 1170 1171 ifp->if_rtrequest(ifp, RTM_ADD, rt); 1172 1173 if (info->rti_info[RTAX_LABEL] != NULL) { 1174 char *rtlabel = ((struct sockaddr_rtlabel *) 1175 info->rti_info[RTAX_LABEL])->sr_label; 1176 rtlabel_unref(rt->rt_labelid); 1177 rt->rt_labelid = rtlabel_name2id(rtlabel); 1178 } 1179 if_group_routechange(info->rti_info[RTAX_DST], 1180 info->rti_info[RTAX_NETMASK]); 1181 rt->rt_locks &= ~(rtm->rtm_inits); 1182 rt->rt_locks |= (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 1183 NET_UNLOCK(); 1184 break; 1185 case RTM_GET: 1186 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1187 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 1188 prio); 1189 if (rt == NULL) 1190 error = ESRCH; 1191 break; 1192 } 1193 1194 if_put(ifp); 1195 *prt = rt; 1196 return (error); 1197 } 1198 1199 struct ifaddr * 1200 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway, 1201 unsigned int rtableid) 1202 { 1203 struct ifaddr *ifa; 1204 1205 if ((flags & RTF_GATEWAY) == 0) { 1206 /* 1207 * If we are adding a route to an interface, 1208 * and the interface is a pt to pt link 1209 * we should search for the destination 1210 * as our clue to the interface. Otherwise 1211 * we can use the local address. 1212 */ 1213 ifa = NULL; 1214 if (flags & RTF_HOST) 1215 ifa = ifa_ifwithdstaddr(dst, rtableid); 1216 if (ifa == NULL) 1217 ifa = ifa_ifwithaddr(gateway, rtableid); 1218 } else { 1219 /* 1220 * If we are adding a route to a remote net 1221 * or host, the gateway may still be on the 1222 * other end of a pt to pt link. 1223 */ 1224 ifa = ifa_ifwithdstaddr(gateway, rtableid); 1225 } 1226 if (ifa == NULL) { 1227 if (gateway->sa_family == AF_LINK) { 1228 struct sockaddr_dl *sdl = satosdl(gateway); 1229 struct ifnet *ifp = if_get(sdl->sdl_index); 1230 1231 if (ifp != NULL) 1232 ifa = ifaof_ifpforaddr(dst, ifp); 1233 if_put(ifp); 1234 } else { 1235 struct rtentry *rt; 1236 1237 rt = rtalloc(gateway, RT_RESOLVE, rtable_l2(rtableid)); 1238 if (rt != NULL) 1239 ifa = rt->rt_ifa; 1240 rtfree(rt); 1241 } 1242 } 1243 if (ifa == NULL) 1244 return (NULL); 1245 if (ifa->ifa_addr->sa_family != dst->sa_family) { 1246 struct ifaddr *oifa = ifa; 1247 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 1248 if (ifa == NULL) 1249 ifa = oifa; 1250 } 1251 return (ifa); 1252 } 1253 1254 int 1255 rtm_getifa(struct rt_addrinfo *info, unsigned int rtid) 1256 { 1257 struct ifnet *ifp = NULL; 1258 1259 /* 1260 * The "returned" `ifa' is guaranteed to be alive only if 1261 * the NET_LOCK() is held. 1262 */ 1263 NET_ASSERT_LOCKED(); 1264 1265 /* 1266 * ifp may be specified by sockaddr_dl when protocol address 1267 * is ambiguous 1268 */ 1269 if (info->rti_info[RTAX_IFP] != NULL) { 1270 struct sockaddr_dl *sdl; 1271 1272 sdl = satosdl(info->rti_info[RTAX_IFP]); 1273 ifp = if_get(sdl->sdl_index); 1274 } 1275 1276 #ifdef IPSEC 1277 /* 1278 * If the destination is a PF_KEY address, we'll look 1279 * for the existence of a encap interface number or address 1280 * in the options list of the gateway. By default, we'll return 1281 * enc0. 1282 */ 1283 if (info->rti_info[RTAX_DST] && 1284 info->rti_info[RTAX_DST]->sa_family == PF_KEY) 1285 info->rti_ifa = enc_getifa(rtid, 0); 1286 #endif 1287 1288 if (info->rti_ifa == NULL && info->rti_info[RTAX_IFA] != NULL) 1289 info->rti_ifa = ifa_ifwithaddr(info->rti_info[RTAX_IFA], rtid); 1290 1291 if (info->rti_ifa == NULL) { 1292 struct sockaddr *sa; 1293 1294 if ((sa = info->rti_info[RTAX_IFA]) == NULL) 1295 if ((sa = info->rti_info[RTAX_GATEWAY]) == NULL) 1296 sa = info->rti_info[RTAX_DST]; 1297 1298 if (sa != NULL && ifp != NULL) 1299 info->rti_ifa = ifaof_ifpforaddr(sa, ifp); 1300 else if (info->rti_info[RTAX_DST] != NULL && 1301 info->rti_info[RTAX_GATEWAY] != NULL) 1302 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 1303 info->rti_info[RTAX_DST], 1304 info->rti_info[RTAX_GATEWAY], 1305 rtid); 1306 else if (sa != NULL) 1307 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 1308 sa, sa, rtid); 1309 } 1310 1311 if_put(ifp); 1312 1313 if (info->rti_ifa == NULL) 1314 return (ENETUNREACH); 1315 1316 return (0); 1317 } 1318 1319 int 1320 route_cleargateway(struct rtentry *rt, void *arg, unsigned int rtableid) 1321 { 1322 struct rtentry *nhrt = arg; 1323 1324 if (ISSET(rt->rt_flags, RTF_GATEWAY) && rt->rt_gwroute == nhrt && 1325 !ISSET(rt->rt_locks, RTV_MTU)) 1326 rt->rt_mtu = 0; 1327 1328 return (0); 1329 } 1330 1331 /* 1332 * Check if the user request to insert an ARP entry does not conflict 1333 * with existing ones. 1334 * 1335 * Only two entries are allowed for a given IP address: a private one 1336 * (priv) and a public one (pub). 1337 */ 1338 int 1339 route_arp_conflict(struct rtentry *rt, struct rt_addrinfo *info) 1340 { 1341 int proxy = (info->rti_flags & RTF_ANNOUNCE); 1342 1343 if ((info->rti_flags & RTF_LLINFO) == 0 || 1344 (info->rti_info[RTAX_DST]->sa_family != AF_INET)) 1345 return (0); 1346 1347 if (rt == NULL || !ISSET(rt->rt_flags, RTF_LLINFO)) 1348 return (0); 1349 1350 /* If the entry is cached, it can be updated. */ 1351 if (ISSET(rt->rt_flags, RTF_CACHED)) 1352 return (0); 1353 1354 /* 1355 * Same destination, not cached and both "priv" or "pub" conflict. 1356 * If a second entry exists, it always conflict. 1357 */ 1358 if ((ISSET(rt->rt_flags, RTF_ANNOUNCE) == proxy) || 1359 ISSET(rt->rt_flags, RTF_MPATH)) 1360 return (EEXIST); 1361 1362 /* No conflict but an entry exist so we need to force mpath. */ 1363 info->rti_flags |= RTF_MPATH; 1364 return (0); 1365 } 1366 1367 void 1368 rtm_setmetrics(u_long which, const struct rt_metrics *in, 1369 struct rt_kmetrics *out) 1370 { 1371 int64_t expire; 1372 1373 if (which & RTV_MTU) 1374 out->rmx_mtu = in->rmx_mtu; 1375 if (which & RTV_EXPIRE) { 1376 expire = in->rmx_expire; 1377 if (expire != 0) { 1378 expire -= gettime(); 1379 expire += getuptime(); 1380 } 1381 1382 out->rmx_expire = expire; 1383 } 1384 } 1385 1386 void 1387 rtm_getmetrics(const struct rt_kmetrics *in, struct rt_metrics *out) 1388 { 1389 int64_t expire; 1390 1391 expire = in->rmx_expire; 1392 if (expire != 0) { 1393 expire -= getuptime(); 1394 expire += gettime(); 1395 } 1396 1397 bzero(out, sizeof(*out)); 1398 out->rmx_locks = in->rmx_locks; 1399 out->rmx_mtu = in->rmx_mtu; 1400 out->rmx_expire = expire; 1401 out->rmx_pksent = in->rmx_pksent; 1402 } 1403 1404 #define ROUNDUP(a) \ 1405 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 1406 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 1407 1408 int 1409 rtm_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 1410 { 1411 struct sockaddr *sa; 1412 int i; 1413 1414 /* 1415 * Parse address bits, split address storage in chunks, and 1416 * set info pointers. Use sa_len for traversing the memory 1417 * and check that we stay within in the limit. 1418 */ 1419 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 1420 for (i = 0; i < sizeof(rtinfo->rti_addrs) * 8; i++) { 1421 if ((rtinfo->rti_addrs & (1 << i)) == 0) 1422 continue; 1423 if (i >= RTAX_MAX || cp + sizeof(socklen_t) > cplim) 1424 return (EINVAL); 1425 sa = (struct sockaddr *)cp; 1426 if (cp + sa->sa_len > cplim) 1427 return (EINVAL); 1428 rtinfo->rti_info[i] = sa; 1429 ADVANCE(cp, sa); 1430 } 1431 /* 1432 * Check that the address family is suitable for the route address 1433 * type. Check that each address has a size that fits its family 1434 * and its length is within the size. Strings within addresses must 1435 * be NUL terminated. 1436 */ 1437 for (i = 0; i < RTAX_MAX; i++) { 1438 size_t len, maxlen, size; 1439 1440 sa = rtinfo->rti_info[i]; 1441 if (sa == NULL) 1442 continue; 1443 maxlen = size = 0; 1444 switch (i) { 1445 case RTAX_DST: 1446 case RTAX_GATEWAY: 1447 case RTAX_SRC: 1448 switch (sa->sa_family) { 1449 case AF_INET: 1450 size = sizeof(struct sockaddr_in); 1451 break; 1452 case AF_LINK: 1453 size = sizeof(struct sockaddr_dl); 1454 break; 1455 #ifdef INET6 1456 case AF_INET6: 1457 size = sizeof(struct sockaddr_in6); 1458 break; 1459 #endif 1460 #ifdef MPLS 1461 case AF_MPLS: 1462 size = sizeof(struct sockaddr_mpls); 1463 break; 1464 #endif 1465 } 1466 break; 1467 case RTAX_IFP: 1468 if (sa->sa_family != AF_LINK) 1469 return (EAFNOSUPPORT); 1470 /* 1471 * XXX Should be sizeof(struct sockaddr_dl), but 1472 * route(8) has a bug and provides less memory. 1473 * arp(8) has another bug and uses sizeof pointer. 1474 */ 1475 size = 4; 1476 break; 1477 case RTAX_IFA: 1478 switch (sa->sa_family) { 1479 case AF_INET: 1480 size = sizeof(struct sockaddr_in); 1481 break; 1482 #ifdef INET6 1483 case AF_INET6: 1484 size = sizeof(struct sockaddr_in6); 1485 break; 1486 #endif 1487 default: 1488 return (EAFNOSUPPORT); 1489 } 1490 break; 1491 case RTAX_LABEL: 1492 sa->sa_family = AF_UNSPEC; 1493 maxlen = RTLABEL_LEN; 1494 size = sizeof(struct sockaddr_rtlabel); 1495 break; 1496 #ifdef BFD 1497 case RTAX_BFD: 1498 sa->sa_family = AF_UNSPEC; 1499 size = sizeof(struct sockaddr_bfd); 1500 break; 1501 #endif 1502 case RTAX_DNS: 1503 /* more validation in rtm_validate_proposal */ 1504 if (sa->sa_len > sizeof(struct sockaddr_rtdns)) 1505 return (EINVAL); 1506 if (sa->sa_len < offsetof(struct sockaddr_rtdns, 1507 sr_dns)) 1508 return (EINVAL); 1509 switch (sa->sa_family) { 1510 case AF_INET: 1511 #ifdef INET6 1512 case AF_INET6: 1513 #endif 1514 break; 1515 default: 1516 return (EAFNOSUPPORT); 1517 } 1518 break; 1519 case RTAX_STATIC: 1520 sa->sa_family = AF_UNSPEC; 1521 maxlen = RTSTATIC_LEN; 1522 size = sizeof(struct sockaddr_rtstatic); 1523 break; 1524 case RTAX_SEARCH: 1525 sa->sa_family = AF_UNSPEC; 1526 maxlen = RTSEARCH_LEN; 1527 size = sizeof(struct sockaddr_rtsearch); 1528 break; 1529 } 1530 if (size) { 1531 /* memory for the full struct must be provided */ 1532 if (sa->sa_len < size) 1533 return (EINVAL); 1534 } 1535 if (maxlen) { 1536 /* this should not happen */ 1537 if (2 + maxlen > size) 1538 return (EINVAL); 1539 /* strings must be NUL terminated within the struct */ 1540 len = strnlen(sa->sa_data, maxlen); 1541 if (len >= maxlen || 2 + len >= sa->sa_len) 1542 return (EINVAL); 1543 break; 1544 } 1545 } 1546 return (0); 1547 } 1548 1549 struct mbuf * 1550 rtm_msg1(int type, struct rt_addrinfo *rtinfo) 1551 { 1552 struct rt_msghdr *rtm; 1553 struct mbuf *m; 1554 int i; 1555 struct sockaddr *sa; 1556 int len, dlen, hlen; 1557 1558 switch (type) { 1559 case RTM_DELADDR: 1560 case RTM_NEWADDR: 1561 len = sizeof(struct ifa_msghdr); 1562 break; 1563 case RTM_IFINFO: 1564 len = sizeof(struct if_msghdr); 1565 break; 1566 case RTM_IFANNOUNCE: 1567 len = sizeof(struct if_announcemsghdr); 1568 break; 1569 #ifdef BFD 1570 case RTM_BFD: 1571 len = sizeof(struct bfd_msghdr); 1572 break; 1573 #endif 1574 case RTM_80211INFO: 1575 len = sizeof(struct if_ieee80211_msghdr); 1576 break; 1577 default: 1578 len = sizeof(struct rt_msghdr); 1579 break; 1580 } 1581 if (len > MCLBYTES) 1582 panic("rtm_msg1"); 1583 m = m_gethdr(M_DONTWAIT, MT_DATA); 1584 if (m && len > MHLEN) { 1585 MCLGET(m, M_DONTWAIT); 1586 if ((m->m_flags & M_EXT) == 0) { 1587 m_free(m); 1588 m = NULL; 1589 } 1590 } 1591 if (m == NULL) 1592 return (m); 1593 m->m_pkthdr.len = m->m_len = hlen = len; 1594 m->m_pkthdr.ph_ifidx = 0; 1595 rtm = mtod(m, struct rt_msghdr *); 1596 bzero(rtm, len); 1597 for (i = 0; i < RTAX_MAX; i++) { 1598 if (rtinfo == NULL || (sa = rtinfo->rti_info[i]) == NULL) 1599 continue; 1600 rtinfo->rti_addrs |= (1 << i); 1601 dlen = ROUNDUP(sa->sa_len); 1602 if (m_copyback(m, len, dlen, sa, M_NOWAIT)) { 1603 m_freem(m); 1604 return (NULL); 1605 } 1606 len += dlen; 1607 } 1608 rtm->rtm_msglen = len; 1609 rtm->rtm_hdrlen = hlen; 1610 rtm->rtm_version = RTM_VERSION; 1611 rtm->rtm_type = type; 1612 return (m); 1613 } 1614 1615 int 1616 rtm_msg2(int type, int vers, struct rt_addrinfo *rtinfo, caddr_t cp, 1617 struct walkarg *w) 1618 { 1619 int i; 1620 int len, dlen, hlen, second_time = 0; 1621 caddr_t cp0; 1622 1623 rtinfo->rti_addrs = 0; 1624 again: 1625 switch (type) { 1626 case RTM_DELADDR: 1627 case RTM_NEWADDR: 1628 len = sizeof(struct ifa_msghdr); 1629 break; 1630 case RTM_IFINFO: 1631 len = sizeof(struct if_msghdr); 1632 break; 1633 default: 1634 len = sizeof(struct rt_msghdr); 1635 break; 1636 } 1637 hlen = len; 1638 if ((cp0 = cp) != NULL) 1639 cp += len; 1640 for (i = 0; i < RTAX_MAX; i++) { 1641 struct sockaddr *sa; 1642 1643 if ((sa = rtinfo->rti_info[i]) == NULL) 1644 continue; 1645 rtinfo->rti_addrs |= (1 << i); 1646 dlen = ROUNDUP(sa->sa_len); 1647 if (cp) { 1648 bcopy(sa, cp, (size_t)dlen); 1649 cp += dlen; 1650 } 1651 len += dlen; 1652 } 1653 /* align message length to the next natural boundary */ 1654 len = ALIGN(len); 1655 if (cp == 0 && w != NULL && !second_time) { 1656 w->w_needed += len; 1657 if (w->w_needed <= 0 && w->w_where) { 1658 if (w->w_tmemsize < len) { 1659 free(w->w_tmem, M_RTABLE, w->w_tmemsize); 1660 w->w_tmem = malloc(len, M_RTABLE, 1661 M_NOWAIT | M_ZERO); 1662 if (w->w_tmem) 1663 w->w_tmemsize = len; 1664 } 1665 if (w->w_tmem) { 1666 cp = w->w_tmem; 1667 second_time = 1; 1668 goto again; 1669 } else 1670 w->w_where = 0; 1671 } 1672 } 1673 if (cp && w) /* clear the message header */ 1674 bzero(cp0, hlen); 1675 1676 if (cp) { 1677 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1678 1679 rtm->rtm_version = RTM_VERSION; 1680 rtm->rtm_type = type; 1681 rtm->rtm_msglen = len; 1682 rtm->rtm_hdrlen = hlen; 1683 } 1684 return (len); 1685 } 1686 1687 void 1688 rtm_send(struct rtentry *rt, int cmd, int error, unsigned int rtableid) 1689 { 1690 struct rt_addrinfo info; 1691 struct ifnet *ifp; 1692 struct sockaddr_rtlabel sa_rl; 1693 struct sockaddr_in6 sa_mask; 1694 1695 memset(&info, 0, sizeof(info)); 1696 info.rti_info[RTAX_DST] = rt_key(rt); 1697 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1698 if (!ISSET(rt->rt_flags, RTF_HOST)) 1699 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 1700 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 1701 ifp = if_get(rt->rt_ifidx); 1702 if (ifp != NULL) { 1703 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1704 info.rti_info[RTAX_IFA] = 1705 rtable_getsource(rtableid, info.rti_info[RTAX_DST]->sa_family); 1706 if (info.rti_info[RTAX_IFA] == NULL) 1707 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1708 } 1709 1710 rtm_miss(cmd, &info, rt->rt_flags, rt->rt_priority, rt->rt_ifidx, error, 1711 rtableid); 1712 if_put(ifp); 1713 } 1714 1715 /* 1716 * This routine is called to generate a message from the routing 1717 * socket indicating that a redirect has occurred, a routing lookup 1718 * has failed, or that a protocol has detected timeouts to a particular 1719 * destination. 1720 */ 1721 void 1722 rtm_miss(int type, struct rt_addrinfo *rtinfo, int flags, uint8_t prio, 1723 u_int ifidx, int error, u_int tableid) 1724 { 1725 struct rt_msghdr *rtm; 1726 struct mbuf *m; 1727 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1728 1729 if (rtptable.rtp_count == 0) 1730 return; 1731 m = rtm_msg1(type, rtinfo); 1732 if (m == NULL) 1733 return; 1734 rtm = mtod(m, struct rt_msghdr *); 1735 rtm->rtm_flags = RTF_DONE | flags; 1736 rtm->rtm_priority = prio; 1737 rtm->rtm_errno = error; 1738 rtm->rtm_tableid = tableid; 1739 rtm->rtm_addrs = rtinfo->rti_addrs; 1740 rtm->rtm_index = ifidx; 1741 route_input(m, NULL, sa ? sa->sa_family : AF_UNSPEC); 1742 } 1743 1744 /* 1745 * This routine is called to generate a message from the routing 1746 * socket indicating that the status of a network interface has changed. 1747 */ 1748 void 1749 rtm_ifchg(struct ifnet *ifp) 1750 { 1751 struct rt_addrinfo info; 1752 struct if_msghdr *ifm; 1753 struct mbuf *m; 1754 1755 if (rtptable.rtp_count == 0) 1756 return; 1757 memset(&info, 0, sizeof(info)); 1758 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1759 m = rtm_msg1(RTM_IFINFO, &info); 1760 if (m == NULL) 1761 return; 1762 ifm = mtod(m, struct if_msghdr *); 1763 ifm->ifm_index = ifp->if_index; 1764 ifm->ifm_tableid = ifp->if_rdomain; 1765 ifm->ifm_flags = ifp->if_flags; 1766 ifm->ifm_xflags = ifp->if_xflags; 1767 if_getdata(ifp, &ifm->ifm_data); 1768 ifm->ifm_addrs = info.rti_addrs; 1769 route_input(m, NULL, AF_UNSPEC); 1770 } 1771 1772 /* 1773 * This is called to generate messages from the routing socket 1774 * indicating a network interface has had addresses associated with it. 1775 * if we ever reverse the logic and replace messages TO the routing 1776 * socket indicate a request to configure interfaces, then it will 1777 * be unnecessary as the routing socket will automatically generate 1778 * copies of it. 1779 */ 1780 void 1781 rtm_addr(int cmd, struct ifaddr *ifa) 1782 { 1783 struct ifnet *ifp = ifa->ifa_ifp; 1784 struct mbuf *m; 1785 struct rt_addrinfo info; 1786 struct ifa_msghdr *ifam; 1787 1788 if (rtptable.rtp_count == 0) 1789 return; 1790 1791 memset(&info, 0, sizeof(info)); 1792 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1793 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1794 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1795 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1796 if ((m = rtm_msg1(cmd, &info)) == NULL) 1797 return; 1798 ifam = mtod(m, struct ifa_msghdr *); 1799 ifam->ifam_index = ifp->if_index; 1800 ifam->ifam_metric = ifa->ifa_metric; 1801 ifam->ifam_flags = ifa->ifa_flags; 1802 ifam->ifam_addrs = info.rti_addrs; 1803 ifam->ifam_tableid = ifp->if_rdomain; 1804 1805 route_input(m, NULL, 1806 ifa->ifa_addr ? ifa->ifa_addr->sa_family : AF_UNSPEC); 1807 } 1808 1809 /* 1810 * This is called to generate routing socket messages indicating 1811 * network interface arrival and departure. 1812 */ 1813 void 1814 rtm_ifannounce(struct ifnet *ifp, int what) 1815 { 1816 struct if_announcemsghdr *ifan; 1817 struct mbuf *m; 1818 1819 if (rtptable.rtp_count == 0) 1820 return; 1821 m = rtm_msg1(RTM_IFANNOUNCE, NULL); 1822 if (m == NULL) 1823 return; 1824 ifan = mtod(m, struct if_announcemsghdr *); 1825 ifan->ifan_index = ifp->if_index; 1826 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 1827 ifan->ifan_what = what; 1828 route_input(m, NULL, AF_UNSPEC); 1829 } 1830 1831 #ifdef BFD 1832 /* 1833 * This is used to generate routing socket messages indicating 1834 * the state of a BFD session. 1835 */ 1836 void 1837 rtm_bfd(struct bfd_config *bfd) 1838 { 1839 struct bfd_msghdr *bfdm; 1840 struct sockaddr_bfd sa_bfd; 1841 struct mbuf *m; 1842 struct rt_addrinfo info; 1843 1844 if (rtptable.rtp_count == 0) 1845 return; 1846 memset(&info, 0, sizeof(info)); 1847 info.rti_info[RTAX_DST] = rt_key(bfd->bc_rt); 1848 info.rti_info[RTAX_IFA] = bfd->bc_rt->rt_ifa->ifa_addr; 1849 1850 m = rtm_msg1(RTM_BFD, &info); 1851 if (m == NULL) 1852 return; 1853 bfdm = mtod(m, struct bfd_msghdr *); 1854 bfdm->bm_addrs = info.rti_addrs; 1855 1856 bfd2sa(bfd->bc_rt, &sa_bfd); 1857 memcpy(&bfdm->bm_sa, &sa_bfd, sizeof(sa_bfd)); 1858 1859 route_input(m, NULL, info.rti_info[RTAX_DST]->sa_family); 1860 } 1861 #endif /* BFD */ 1862 1863 /* 1864 * This is used to generate routing socket messages indicating 1865 * the state of an ieee80211 interface. 1866 */ 1867 void 1868 rtm_80211info(struct ifnet *ifp, struct if_ieee80211_data *ifie) 1869 { 1870 struct if_ieee80211_msghdr *ifim; 1871 struct mbuf *m; 1872 1873 if (rtptable.rtp_count == 0) 1874 return; 1875 m = rtm_msg1(RTM_80211INFO, NULL); 1876 if (m == NULL) 1877 return; 1878 ifim = mtod(m, struct if_ieee80211_msghdr *); 1879 ifim->ifim_index = ifp->if_index; 1880 ifim->ifim_tableid = ifp->if_rdomain; 1881 1882 memcpy(&ifim->ifim_ifie, ifie, sizeof(ifim->ifim_ifie)); 1883 route_input(m, NULL, AF_UNSPEC); 1884 } 1885 1886 /* 1887 * This is used to generate routing socket messages indicating 1888 * the address selection proposal from an interface. 1889 */ 1890 void 1891 rtm_proposal(struct ifnet *ifp, struct rt_addrinfo *rtinfo, int flags, 1892 uint8_t prio) 1893 { 1894 struct rt_msghdr *rtm; 1895 struct mbuf *m; 1896 1897 m = rtm_msg1(RTM_PROPOSAL, rtinfo); 1898 if (m == NULL) 1899 return; 1900 rtm = mtod(m, struct rt_msghdr *); 1901 rtm->rtm_flags = RTF_DONE | flags; 1902 rtm->rtm_priority = prio; 1903 rtm->rtm_tableid = ifp->if_rdomain; 1904 rtm->rtm_index = ifp->if_index; 1905 rtm->rtm_addrs = rtinfo->rti_addrs; 1906 1907 route_input(m, NULL, rtinfo->rti_info[RTAX_DNS]->sa_family); 1908 } 1909 1910 /* 1911 * This is used in dumping the kernel table via sysctl(). 1912 */ 1913 int 1914 sysctl_dumpentry(struct rtentry *rt, void *v, unsigned int id) 1915 { 1916 struct walkarg *w = v; 1917 int error = 0, size; 1918 struct rt_addrinfo info; 1919 struct ifnet *ifp; 1920 #ifdef BFD 1921 struct sockaddr_bfd sa_bfd; 1922 #endif 1923 struct sockaddr_rtlabel sa_rl; 1924 struct sockaddr_in6 sa_mask; 1925 1926 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1927 return 0; 1928 if (w->w_op == NET_RT_DUMP && w->w_arg) { 1929 u_int8_t prio = w->w_arg & RTP_MASK; 1930 if (w->w_arg < 0) { 1931 prio = (-w->w_arg) & RTP_MASK; 1932 /* Show all routes that are not this priority */ 1933 if (prio == (rt->rt_priority & RTP_MASK)) 1934 return 0; 1935 } else { 1936 if (prio != (rt->rt_priority & RTP_MASK) && 1937 prio != RTP_ANY) 1938 return 0; 1939 } 1940 } 1941 bzero(&info, sizeof(info)); 1942 info.rti_info[RTAX_DST] = rt_key(rt); 1943 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1944 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 1945 ifp = if_get(rt->rt_ifidx); 1946 if (ifp != NULL) { 1947 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1948 info.rti_info[RTAX_IFA] = 1949 rtable_getsource(id, info.rti_info[RTAX_DST]->sa_family); 1950 if (info.rti_info[RTAX_IFA] == NULL) 1951 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1952 if (ifp->if_flags & IFF_POINTOPOINT) 1953 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1954 } 1955 if_put(ifp); 1956 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 1957 #ifdef BFD 1958 if (rt->rt_flags & RTF_BFD) 1959 info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd); 1960 #endif 1961 #ifdef MPLS 1962 if (rt->rt_flags & RTF_MPLS) { 1963 struct sockaddr_mpls sa_mpls; 1964 1965 bzero(&sa_mpls, sizeof(sa_mpls)); 1966 sa_mpls.smpls_family = AF_MPLS; 1967 sa_mpls.smpls_len = sizeof(sa_mpls); 1968 sa_mpls.smpls_label = ((struct rt_mpls *) 1969 rt->rt_llinfo)->mpls_label; 1970 info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls; 1971 info.rti_mpls = ((struct rt_mpls *) 1972 rt->rt_llinfo)->mpls_operation; 1973 } 1974 #endif 1975 1976 size = rtm_msg2(RTM_GET, RTM_VERSION, &info, NULL, w); 1977 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1978 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1979 1980 rtm->rtm_pid = curproc->p_p->ps_pid; 1981 rtm->rtm_flags = rt->rt_flags; 1982 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 1983 rtm_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1984 /* Do not account the routing table's reference. */ 1985 rtm->rtm_rmx.rmx_refcnt = rt->rt_refcnt - 1; 1986 rtm->rtm_index = rt->rt_ifidx; 1987 rtm->rtm_addrs = info.rti_addrs; 1988 rtm->rtm_tableid = id; 1989 #ifdef MPLS 1990 rtm->rtm_mpls = info.rti_mpls; 1991 #endif 1992 if ((error = copyout(rtm, w->w_where, size)) != 0) 1993 w->w_where = NULL; 1994 else 1995 w->w_where += size; 1996 } 1997 return (error); 1998 } 1999 2000 int 2001 sysctl_iflist(int af, struct walkarg *w) 2002 { 2003 struct ifnet *ifp; 2004 struct ifaddr *ifa; 2005 struct rt_addrinfo info; 2006 int len, error = 0; 2007 2008 bzero(&info, sizeof(info)); 2009 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2010 if (w->w_arg && w->w_arg != ifp->if_index) 2011 continue; 2012 /* Copy the link-layer address first */ 2013 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 2014 len = rtm_msg2(RTM_IFINFO, RTM_VERSION, &info, 0, w); 2015 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 2016 struct if_msghdr *ifm; 2017 2018 ifm = (struct if_msghdr *)w->w_tmem; 2019 ifm->ifm_index = ifp->if_index; 2020 ifm->ifm_tableid = ifp->if_rdomain; 2021 ifm->ifm_flags = ifp->if_flags; 2022 if_getdata(ifp, &ifm->ifm_data); 2023 ifm->ifm_addrs = info.rti_addrs; 2024 error = copyout(ifm, w->w_where, len); 2025 if (error) 2026 return (error); 2027 w->w_where += len; 2028 } 2029 info.rti_info[RTAX_IFP] = NULL; 2030 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2031 KASSERT(ifa->ifa_addr->sa_family != AF_LINK); 2032 if (af && af != ifa->ifa_addr->sa_family) 2033 continue; 2034 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 2035 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 2036 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 2037 len = rtm_msg2(RTM_NEWADDR, RTM_VERSION, &info, 0, w); 2038 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 2039 struct ifa_msghdr *ifam; 2040 2041 ifam = (struct ifa_msghdr *)w->w_tmem; 2042 ifam->ifam_index = ifa->ifa_ifp->if_index; 2043 ifam->ifam_flags = ifa->ifa_flags; 2044 ifam->ifam_metric = ifa->ifa_metric; 2045 ifam->ifam_addrs = info.rti_addrs; 2046 error = copyout(w->w_tmem, w->w_where, len); 2047 if (error) 2048 return (error); 2049 w->w_where += len; 2050 } 2051 } 2052 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 2053 info.rti_info[RTAX_BRD] = NULL; 2054 } 2055 return (0); 2056 } 2057 2058 int 2059 sysctl_ifnames(struct walkarg *w) 2060 { 2061 struct if_nameindex_msg ifn; 2062 struct ifnet *ifp; 2063 int error = 0; 2064 2065 /* XXX ignore tableid for now */ 2066 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2067 if (w->w_arg && w->w_arg != ifp->if_index) 2068 continue; 2069 w->w_needed += sizeof(ifn); 2070 if (w->w_where && w->w_needed <= 0) { 2071 2072 memset(&ifn, 0, sizeof(ifn)); 2073 ifn.if_index = ifp->if_index; 2074 strlcpy(ifn.if_name, ifp->if_xname, 2075 sizeof(ifn.if_name)); 2076 error = copyout(&ifn, w->w_where, sizeof(ifn)); 2077 if (error) 2078 return (error); 2079 w->w_where += sizeof(ifn); 2080 } 2081 } 2082 2083 return (0); 2084 } 2085 2086 int 2087 sysctl_source(int af, u_int tableid, struct walkarg *w) 2088 { 2089 struct sockaddr *sa; 2090 int size, error = 0; 2091 2092 sa = rtable_getsource(tableid, af); 2093 if (sa) { 2094 switch (sa->sa_family) { 2095 case AF_INET: 2096 size = sizeof(struct sockaddr_in); 2097 break; 2098 #ifdef INET6 2099 case AF_INET6: 2100 size = sizeof(struct sockaddr_in6); 2101 break; 2102 #endif 2103 default: 2104 return (0); 2105 } 2106 w->w_needed += size; 2107 if (w->w_where && w->w_needed <= 0) { 2108 if ((error = copyout(sa, w->w_where, size))) 2109 return (error); 2110 w->w_where += size; 2111 } 2112 } 2113 return (0); 2114 } 2115 2116 int 2117 sysctl_rtable(int *name, u_int namelen, void *where, size_t *given, void *new, 2118 size_t newlen) 2119 { 2120 int i, error = EINVAL; 2121 u_char af; 2122 struct walkarg w; 2123 struct rt_tableinfo tableinfo; 2124 u_int tableid = 0; 2125 2126 if (new) 2127 return (EPERM); 2128 if (namelen < 3 || namelen > 4) 2129 return (EINVAL); 2130 af = name[0]; 2131 bzero(&w, sizeof(w)); 2132 w.w_where = where; 2133 w.w_given = *given; 2134 w.w_needed = 0 - w.w_given; 2135 w.w_op = name[1]; 2136 w.w_arg = name[2]; 2137 2138 if (namelen == 4) { 2139 tableid = name[3]; 2140 if (!rtable_exists(tableid)) 2141 return (ENOENT); 2142 } else 2143 tableid = curproc->p_p->ps_rtableid; 2144 2145 switch (w.w_op) { 2146 case NET_RT_DUMP: 2147 case NET_RT_FLAGS: 2148 NET_LOCK(); 2149 for (i = 1; i <= AF_MAX; i++) { 2150 if (af != 0 && af != i) 2151 continue; 2152 2153 error = rtable_walk(tableid, i, NULL, sysctl_dumpentry, 2154 &w); 2155 if (error == EAFNOSUPPORT) 2156 error = 0; 2157 if (error) 2158 break; 2159 } 2160 NET_UNLOCK(); 2161 break; 2162 2163 case NET_RT_IFLIST: 2164 NET_LOCK(); 2165 error = sysctl_iflist(af, &w); 2166 NET_UNLOCK(); 2167 break; 2168 2169 case NET_RT_STATS: 2170 return (sysctl_rtable_rtstat(where, given, new)); 2171 case NET_RT_TABLE: 2172 tableid = w.w_arg; 2173 if (!rtable_exists(tableid)) 2174 return (ENOENT); 2175 memset(&tableinfo, 0, sizeof tableinfo); 2176 tableinfo.rti_tableid = tableid; 2177 tableinfo.rti_domainid = rtable_l2(tableid); 2178 error = sysctl_rdstruct(where, given, new, 2179 &tableinfo, sizeof(tableinfo)); 2180 return (error); 2181 case NET_RT_IFNAMES: 2182 NET_LOCK(); 2183 error = sysctl_ifnames(&w); 2184 NET_UNLOCK(); 2185 break; 2186 case NET_RT_SOURCE: 2187 tableid = w.w_arg; 2188 if (!rtable_exists(tableid)) 2189 return (ENOENT); 2190 NET_LOCK(); 2191 for (i = 1; i <= AF_MAX; i++) { 2192 if (af != 0 && af != i) 2193 continue; 2194 2195 error = sysctl_source(i, tableid, &w); 2196 if (error == EAFNOSUPPORT) 2197 error = 0; 2198 if (error) 2199 break; 2200 } 2201 NET_UNLOCK(); 2202 break; 2203 } 2204 free(w.w_tmem, M_RTABLE, w.w_tmemsize); 2205 w.w_needed += w.w_given; 2206 if (where) { 2207 *given = w.w_where - (caddr_t)where; 2208 if (*given < w.w_needed) 2209 return (ENOMEM); 2210 } else 2211 *given = (11 * w.w_needed) / 10; 2212 2213 return (error); 2214 } 2215 2216 int 2217 sysctl_rtable_rtstat(void *oldp, size_t *oldlenp, void *newp) 2218 { 2219 extern struct cpumem *rtcounters; 2220 uint64_t counters[rts_ncounters]; 2221 struct rtstat rtstat; 2222 uint32_t *words = (uint32_t *)&rtstat; 2223 int i; 2224 2225 CTASSERT(sizeof(rtstat) == (nitems(counters) * sizeof(uint32_t))); 2226 memset(&rtstat, 0, sizeof rtstat); 2227 counters_read(rtcounters, counters, nitems(counters)); 2228 2229 for (i = 0; i < nitems(counters); i++) 2230 words[i] = (uint32_t)counters[i]; 2231 2232 return (sysctl_rdstruct(oldp, oldlenp, newp, &rtstat, sizeof(rtstat))); 2233 } 2234 2235 int 2236 rtm_validate_proposal(struct rt_addrinfo *info) 2237 { 2238 if (info->rti_addrs & ~(RTA_NETMASK | RTA_IFA | RTA_DNS | RTA_STATIC | 2239 RTA_SEARCH)) { 2240 return -1; 2241 } 2242 2243 if (ISSET(info->rti_addrs, RTA_NETMASK)) { 2244 struct sockaddr *sa = info->rti_info[RTAX_NETMASK]; 2245 if (sa == NULL) 2246 return -1; 2247 switch (sa->sa_family) { 2248 case AF_INET: 2249 if (sa->sa_len != sizeof(struct sockaddr_in)) 2250 return -1; 2251 break; 2252 case AF_INET6: 2253 if (sa->sa_len != sizeof(struct sockaddr_in6)) 2254 return -1; 2255 break; 2256 default: 2257 return -1; 2258 } 2259 } 2260 2261 if (ISSET(info->rti_addrs, RTA_IFA)) { 2262 struct sockaddr *sa = info->rti_info[RTAX_IFA]; 2263 if (sa == NULL) 2264 return -1; 2265 switch (sa->sa_family) { 2266 case AF_INET: 2267 if (sa->sa_len != sizeof(struct sockaddr_in)) 2268 return -1; 2269 break; 2270 case AF_INET6: 2271 if (sa->sa_len != sizeof(struct sockaddr_in6)) 2272 return -1; 2273 break; 2274 default: 2275 return -1; 2276 } 2277 } 2278 2279 if (ISSET(info->rti_addrs, RTA_DNS)) { 2280 struct sockaddr_rtdns *rtdns = 2281 (struct sockaddr_rtdns *)info->rti_info[RTAX_DNS]; 2282 if (rtdns == NULL) 2283 return -1; 2284 if (rtdns->sr_len > sizeof(*rtdns)) 2285 return -1; 2286 if (rtdns->sr_len < offsetof(struct sockaddr_rtdns, sr_dns)) 2287 return -1; 2288 switch (rtdns->sr_family) { 2289 case AF_INET: 2290 if ((rtdns->sr_len - offsetof(struct sockaddr_rtdns, 2291 sr_dns)) % sizeof(struct in_addr) != 0) 2292 return -1; 2293 break; 2294 #ifdef INET6 2295 case AF_INET6: 2296 if ((rtdns->sr_len - offsetof(struct sockaddr_rtdns, 2297 sr_dns)) % sizeof(struct in6_addr) != 0) 2298 return -1; 2299 break; 2300 #endif 2301 default: 2302 return -1; 2303 } 2304 } 2305 2306 if (ISSET(info->rti_addrs, RTA_STATIC)) { 2307 struct sockaddr_rtstatic *rtstatic = 2308 (struct sockaddr_rtstatic *)info->rti_info[RTAX_STATIC]; 2309 if (rtstatic == NULL) 2310 return -1; 2311 if (rtstatic->sr_len > sizeof(*rtstatic)) 2312 return -1; 2313 if (rtstatic->sr_len <= 2314 offsetof(struct sockaddr_rtstatic, sr_static)) 2315 return -1; 2316 } 2317 2318 if (ISSET(info->rti_addrs, RTA_SEARCH)) { 2319 struct sockaddr_rtsearch *rtsearch = 2320 (struct sockaddr_rtsearch *)info->rti_info[RTAX_SEARCH]; 2321 if (rtsearch == NULL) 2322 return -1; 2323 if (rtsearch->sr_len > sizeof(*rtsearch)) 2324 return -1; 2325 if (rtsearch->sr_len <= 2326 offsetof(struct sockaddr_rtsearch, sr_search)) 2327 return -1; 2328 } 2329 2330 return 0; 2331 } 2332 2333 int 2334 rt_setsource(unsigned int rtableid, struct sockaddr *src) 2335 { 2336 struct ifaddr *ifa; 2337 int error; 2338 /* 2339 * If source address is 0.0.0.0 or :: 2340 * use automatic source selection 2341 */ 2342 switch(src->sa_family) { 2343 case AF_INET: 2344 if(satosin(src)->sin_addr.s_addr == INADDR_ANY) { 2345 rtable_setsource(rtableid, AF_INET, NULL); 2346 return (0); 2347 } 2348 break; 2349 #ifdef INET6 2350 case AF_INET6: 2351 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)) { 2352 rtable_setsource(rtableid, AF_INET6, NULL); 2353 return (0); 2354 } 2355 break; 2356 #endif 2357 default: 2358 return (EAFNOSUPPORT); 2359 } 2360 2361 KERNEL_LOCK(); 2362 /* 2363 * Check if source address is assigned to an interface in the 2364 * same rdomain 2365 */ 2366 if ((ifa = ifa_ifwithaddr(src, rtableid)) == NULL) { 2367 KERNEL_UNLOCK(); 2368 return (EINVAL); 2369 } 2370 2371 error = rtable_setsource(rtableid, src->sa_family, ifa->ifa_addr); 2372 KERNEL_UNLOCK(); 2373 2374 return (error); 2375 } 2376 2377 /* 2378 * Definitions of protocols supported in the ROUTE domain. 2379 */ 2380 2381 const struct protosw routesw[] = { 2382 { 2383 .pr_type = SOCK_RAW, 2384 .pr_domain = &routedomain, 2385 .pr_flags = PR_ATOMIC|PR_ADDR|PR_WANTRCVD, 2386 .pr_output = route_output, 2387 .pr_ctloutput = route_ctloutput, 2388 .pr_usrreq = route_usrreq, 2389 .pr_attach = route_attach, 2390 .pr_detach = route_detach, 2391 .pr_init = route_prinit, 2392 .pr_sysctl = sysctl_rtable 2393 } 2394 }; 2395 2396 const struct domain routedomain = { 2397 .dom_family = PF_ROUTE, 2398 .dom_name = "route", 2399 .dom_init = route_init, 2400 .dom_protosw = routesw, 2401 .dom_protoswNPROTOSW = &routesw[nitems(routesw)] 2402 }; 2403