1 /* $OpenBSD: rtsock.c,v 1.319 2021/06/23 16:10:45 cheloha 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 goto change; 939 940 rtfree(rt); 941 rt = NULL; 942 943 NET_LOCK(); 944 if ((error = rtm_getifa(info, tableid)) != 0) { 945 NET_UNLOCK(); 946 break; 947 } 948 error = rtrequest(RTM_ADD, info, prio, &rt, tableid); 949 NET_UNLOCK(); 950 if (error == 0) 951 rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 952 &rt->rt_rmx); 953 break; 954 case RTM_DELETE: 955 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 956 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 957 prio); 958 if (rt == NULL) { 959 error = ESRCH; 960 break; 961 } 962 963 /* 964 * If we got multipath routes, we require users to specify 965 * a matching gateway. 966 */ 967 if (ISSET(rt->rt_flags, RTF_MPATH) && 968 info->rti_info[RTAX_GATEWAY] == NULL) { 969 error = ESRCH; 970 break; 971 } 972 973 /* Detaching an interface requires the KERNEL_LOCK(). */ 974 ifp = if_get(rt->rt_ifidx); 975 KASSERT(ifp != NULL); 976 977 /* 978 * Invalidate the cache of automagically created and 979 * referenced L2 entries to make sure that ``rt_gwroute'' 980 * pointer stays valid for other CPUs. 981 */ 982 if ((ISSET(rt->rt_flags, RTF_CACHED))) { 983 NET_LOCK(); 984 ifp->if_rtrequest(ifp, RTM_INVALIDATE, rt); 985 /* Reset the MTU of the gateway route. */ 986 rtable_walk(tableid, rt_key(rt)->sa_family, NULL, 987 route_cleargateway, rt); 988 NET_UNLOCK(); 989 if_put(ifp); 990 break; 991 } 992 993 /* 994 * Make sure that local routes are only modified by the 995 * kernel. 996 */ 997 if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) { 998 if_put(ifp); 999 error = EINVAL; 1000 break; 1001 } 1002 1003 rtfree(rt); 1004 rt = NULL; 1005 1006 NET_LOCK(); 1007 error = rtrequest_delete(info, prio, ifp, &rt, tableid); 1008 NET_UNLOCK(); 1009 if_put(ifp); 1010 break; 1011 case RTM_CHANGE: 1012 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1013 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 1014 prio); 1015 /* 1016 * If we got multipath routes, we require users to specify 1017 * a matching gateway. 1018 */ 1019 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH) && 1020 (info->rti_info[RTAX_GATEWAY] == NULL)) { 1021 rtfree(rt); 1022 rt = NULL; 1023 } 1024 /* 1025 * If RTAX_GATEWAY is the argument we're trying to 1026 * change, try to find a compatible route. 1027 */ 1028 if ((rt == NULL) && (info->rti_info[RTAX_GATEWAY] != NULL)) { 1029 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1030 info->rti_info[RTAX_NETMASK], NULL, prio); 1031 /* Ensure we don't pick a multipath one. */ 1032 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH)) { 1033 rtfree(rt); 1034 rt = NULL; 1035 } 1036 } 1037 1038 if (rt == NULL) { 1039 error = ESRCH; 1040 break; 1041 } 1042 1043 /* 1044 * Make sure that local routes are only modified by the 1045 * kernel. 1046 */ 1047 if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) { 1048 error = EINVAL; 1049 break; 1050 } 1051 1052 /* 1053 * RTM_CHANGE needs a perfect match. 1054 */ 1055 plen = rtable_satoplen(info->rti_info[RTAX_DST]->sa_family, 1056 info->rti_info[RTAX_NETMASK]); 1057 if (rt_plen(rt) != plen) { 1058 error = ESRCH; 1059 break; 1060 } 1061 1062 if (info->rti_info[RTAX_GATEWAY] != NULL) 1063 if (rt->rt_gateway == NULL || 1064 bcmp(rt->rt_gateway, 1065 info->rti_info[RTAX_GATEWAY], 1066 info->rti_info[RTAX_GATEWAY]->sa_len)) { 1067 newgate = 1; 1068 } 1069 /* 1070 * Check reachable gateway before changing the route. 1071 * New gateway could require new ifaddr, ifp; 1072 * flags may also be different; ifp may be specified 1073 * by ll sockaddr when protocol address is ambiguous. 1074 */ 1075 if (newgate || info->rti_info[RTAX_IFP] != NULL || 1076 info->rti_info[RTAX_IFA] != NULL) { 1077 struct ifaddr *ifa = NULL; 1078 1079 NET_LOCK(); 1080 if ((error = rtm_getifa(info, tableid)) != 0) { 1081 NET_UNLOCK(); 1082 break; 1083 } 1084 ifa = info->rti_ifa; 1085 if (rt->rt_ifa != ifa) { 1086 ifp = if_get(rt->rt_ifidx); 1087 KASSERT(ifp != NULL); 1088 ifp->if_rtrequest(ifp, RTM_DELETE, rt); 1089 ifafree(rt->rt_ifa); 1090 if_put(ifp); 1091 1092 ifa->ifa_refcnt++; 1093 rt->rt_ifa = ifa; 1094 rt->rt_ifidx = ifa->ifa_ifp->if_index; 1095 /* recheck link state after ifp change */ 1096 rt_if_linkstate_change(rt, ifa->ifa_ifp, 1097 tableid); 1098 } 1099 NET_UNLOCK(); 1100 } 1101 change: 1102 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1103 /* When updating the gateway, make sure it is valid. */ 1104 if (!newgate && rt->rt_gateway->sa_family != 1105 info->rti_info[RTAX_GATEWAY]->sa_family) { 1106 error = EINVAL; 1107 break; 1108 } 1109 1110 NET_LOCK(); 1111 error = rt_setgate(rt, 1112 info->rti_info[RTAX_GATEWAY], tableid); 1113 NET_UNLOCK(); 1114 if (error) 1115 break; 1116 } 1117 #ifdef MPLS 1118 if (rtm->rtm_flags & RTF_MPLS) { 1119 NET_LOCK(); 1120 error = rt_mpls_set(rt, 1121 info->rti_info[RTAX_SRC], info->rti_mpls); 1122 NET_UNLOCK(); 1123 if (error) 1124 break; 1125 } else if (newgate || (rtm->rtm_fmask & RTF_MPLS)) { 1126 NET_LOCK(); 1127 /* if gateway changed remove MPLS information */ 1128 rt_mpls_clear(rt); 1129 NET_UNLOCK(); 1130 } 1131 #endif 1132 1133 #ifdef BFD 1134 if (ISSET(rtm->rtm_flags, RTF_BFD)) { 1135 if ((error = bfdset(rt))) 1136 break; 1137 } else if (!ISSET(rtm->rtm_flags, RTF_BFD) && 1138 ISSET(rtm->rtm_fmask, RTF_BFD)) { 1139 bfdclear(rt); 1140 } 1141 #endif 1142 1143 NET_LOCK(); 1144 /* Hack to allow some flags to be toggled */ 1145 if (rtm->rtm_fmask) { 1146 /* MPLS flag it is set by rt_mpls_set() */ 1147 rtm->rtm_fmask &= ~RTF_MPLS; 1148 rtm->rtm_flags &= ~RTF_MPLS; 1149 rt->rt_flags = 1150 (rt->rt_flags & ~rtm->rtm_fmask) | 1151 (rtm->rtm_flags & rtm->rtm_fmask); 1152 } 1153 rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx); 1154 1155 ifp = if_get(rt->rt_ifidx); 1156 KASSERT(ifp != NULL); 1157 ifp->if_rtrequest(ifp, RTM_ADD, rt); 1158 if_put(ifp); 1159 1160 if (info->rti_info[RTAX_LABEL] != NULL) { 1161 char *rtlabel = ((struct sockaddr_rtlabel *) 1162 info->rti_info[RTAX_LABEL])->sr_label; 1163 rtlabel_unref(rt->rt_labelid); 1164 rt->rt_labelid = rtlabel_name2id(rtlabel); 1165 } 1166 if_group_routechange(info->rti_info[RTAX_DST], 1167 info->rti_info[RTAX_NETMASK]); 1168 rt->rt_locks &= ~(rtm->rtm_inits); 1169 rt->rt_locks |= (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 1170 NET_UNLOCK(); 1171 break; 1172 case RTM_GET: 1173 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 1174 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 1175 prio); 1176 if (rt == NULL) 1177 error = ESRCH; 1178 break; 1179 } 1180 1181 *prt = rt; 1182 return (error); 1183 } 1184 1185 struct ifaddr * 1186 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway, 1187 unsigned int rtableid) 1188 { 1189 struct ifaddr *ifa; 1190 1191 if ((flags & RTF_GATEWAY) == 0) { 1192 /* 1193 * If we are adding a route to an interface, 1194 * and the interface is a pt to pt link 1195 * we should search for the destination 1196 * as our clue to the interface. Otherwise 1197 * we can use the local address. 1198 */ 1199 ifa = NULL; 1200 if (flags & RTF_HOST) 1201 ifa = ifa_ifwithdstaddr(dst, rtableid); 1202 if (ifa == NULL) 1203 ifa = ifa_ifwithaddr(gateway, rtableid); 1204 } else { 1205 /* 1206 * If we are adding a route to a remote net 1207 * or host, the gateway may still be on the 1208 * other end of a pt to pt link. 1209 */ 1210 ifa = ifa_ifwithdstaddr(gateway, rtableid); 1211 } 1212 if (ifa == NULL) { 1213 if (gateway->sa_family == AF_LINK) { 1214 struct sockaddr_dl *sdl = satosdl(gateway); 1215 struct ifnet *ifp = if_get(sdl->sdl_index); 1216 1217 if (ifp != NULL) 1218 ifa = ifaof_ifpforaddr(dst, ifp); 1219 if_put(ifp); 1220 } else { 1221 struct rtentry *rt; 1222 1223 rt = rtalloc(gateway, RT_RESOLVE, rtable_l2(rtableid)); 1224 if (rt != NULL) 1225 ifa = rt->rt_ifa; 1226 rtfree(rt); 1227 } 1228 } 1229 if (ifa == NULL) 1230 return (NULL); 1231 if (ifa->ifa_addr->sa_family != dst->sa_family) { 1232 struct ifaddr *oifa = ifa; 1233 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 1234 if (ifa == NULL) 1235 ifa = oifa; 1236 } 1237 return (ifa); 1238 } 1239 1240 int 1241 rtm_getifa(struct rt_addrinfo *info, unsigned int rtid) 1242 { 1243 struct ifnet *ifp = NULL; 1244 1245 /* 1246 * The "returned" `ifa' is guaranteed to be alive only if 1247 * the NET_LOCK() is held. 1248 */ 1249 NET_ASSERT_LOCKED(); 1250 1251 /* 1252 * ifp may be specified by sockaddr_dl when protocol address 1253 * is ambiguous 1254 */ 1255 if (info->rti_info[RTAX_IFP] != NULL) { 1256 struct sockaddr_dl *sdl; 1257 1258 sdl = satosdl(info->rti_info[RTAX_IFP]); 1259 ifp = if_get(sdl->sdl_index); 1260 } 1261 1262 #ifdef IPSEC 1263 /* 1264 * If the destination is a PF_KEY address, we'll look 1265 * for the existence of a encap interface number or address 1266 * in the options list of the gateway. By default, we'll return 1267 * enc0. 1268 */ 1269 if (info->rti_info[RTAX_DST] && 1270 info->rti_info[RTAX_DST]->sa_family == PF_KEY) 1271 info->rti_ifa = enc_getifa(rtid, 0); 1272 #endif 1273 1274 if (info->rti_ifa == NULL && info->rti_info[RTAX_IFA] != NULL) 1275 info->rti_ifa = ifa_ifwithaddr(info->rti_info[RTAX_IFA], rtid); 1276 1277 if (info->rti_ifa == NULL) { 1278 struct sockaddr *sa; 1279 1280 if ((sa = info->rti_info[RTAX_IFA]) == NULL) 1281 if ((sa = info->rti_info[RTAX_GATEWAY]) == NULL) 1282 sa = info->rti_info[RTAX_DST]; 1283 1284 if (sa != NULL && ifp != NULL) 1285 info->rti_ifa = ifaof_ifpforaddr(sa, ifp); 1286 else if (info->rti_info[RTAX_DST] != NULL && 1287 info->rti_info[RTAX_GATEWAY] != NULL) 1288 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 1289 info->rti_info[RTAX_DST], 1290 info->rti_info[RTAX_GATEWAY], 1291 rtid); 1292 else if (sa != NULL) 1293 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 1294 sa, sa, rtid); 1295 } 1296 1297 if_put(ifp); 1298 1299 if (info->rti_ifa == NULL) 1300 return (ENETUNREACH); 1301 1302 return (0); 1303 } 1304 1305 int 1306 route_cleargateway(struct rtentry *rt, void *arg, unsigned int rtableid) 1307 { 1308 struct rtentry *nhrt = arg; 1309 1310 if (ISSET(rt->rt_flags, RTF_GATEWAY) && rt->rt_gwroute == nhrt && 1311 !ISSET(rt->rt_locks, RTV_MTU)) 1312 rt->rt_mtu = 0; 1313 1314 return (0); 1315 } 1316 1317 /* 1318 * Check if the user request to insert an ARP entry does not conflict 1319 * with existing ones. 1320 * 1321 * Only two entries are allowed for a given IP address: a private one 1322 * (priv) and a public one (pub). 1323 */ 1324 int 1325 route_arp_conflict(struct rtentry *rt, struct rt_addrinfo *info) 1326 { 1327 int proxy = (info->rti_flags & RTF_ANNOUNCE); 1328 1329 if ((info->rti_flags & RTF_LLINFO) == 0 || 1330 (info->rti_info[RTAX_DST]->sa_family != AF_INET)) 1331 return (0); 1332 1333 if (rt == NULL || !ISSET(rt->rt_flags, RTF_LLINFO)) 1334 return (0); 1335 1336 /* If the entry is cached, it can be updated. */ 1337 if (ISSET(rt->rt_flags, RTF_CACHED)) 1338 return (0); 1339 1340 /* 1341 * Same destination, not cached and both "priv" or "pub" conflict. 1342 * If a second entry exists, it always conflict. 1343 */ 1344 if ((ISSET(rt->rt_flags, RTF_ANNOUNCE) == proxy) || 1345 ISSET(rt->rt_flags, RTF_MPATH)) 1346 return (EEXIST); 1347 1348 /* No conflict but an entry exist so we need to force mpath. */ 1349 info->rti_flags |= RTF_MPATH; 1350 return (0); 1351 } 1352 1353 void 1354 rtm_setmetrics(u_long which, const struct rt_metrics *in, 1355 struct rt_kmetrics *out) 1356 { 1357 int64_t expire; 1358 1359 if (which & RTV_MTU) 1360 out->rmx_mtu = in->rmx_mtu; 1361 if (which & RTV_EXPIRE) { 1362 expire = in->rmx_expire; 1363 if (expire != 0) { 1364 expire -= gettime(); 1365 expire += getuptime(); 1366 } 1367 1368 out->rmx_expire = expire; 1369 } 1370 } 1371 1372 void 1373 rtm_getmetrics(const struct rt_kmetrics *in, struct rt_metrics *out) 1374 { 1375 int64_t expire; 1376 1377 expire = in->rmx_expire; 1378 if (expire != 0) { 1379 expire -= getuptime(); 1380 expire += gettime(); 1381 } 1382 1383 bzero(out, sizeof(*out)); 1384 out->rmx_locks = in->rmx_locks; 1385 out->rmx_mtu = in->rmx_mtu; 1386 out->rmx_expire = expire; 1387 out->rmx_pksent = in->rmx_pksent; 1388 } 1389 1390 #define ROUNDUP(a) \ 1391 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 1392 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 1393 1394 int 1395 rtm_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 1396 { 1397 struct sockaddr *sa; 1398 int i; 1399 1400 /* 1401 * Parse address bits, split address storage in chunks, and 1402 * set info pointers. Use sa_len for traversing the memory 1403 * and check that we stay within in the limit. 1404 */ 1405 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 1406 for (i = 0; i < sizeof(rtinfo->rti_addrs) * 8; i++) { 1407 if ((rtinfo->rti_addrs & (1 << i)) == 0) 1408 continue; 1409 if (i >= RTAX_MAX || cp + sizeof(socklen_t) > cplim) 1410 return (EINVAL); 1411 sa = (struct sockaddr *)cp; 1412 if (cp + sa->sa_len > cplim) 1413 return (EINVAL); 1414 rtinfo->rti_info[i] = sa; 1415 ADVANCE(cp, sa); 1416 } 1417 /* 1418 * Check that the address family is suitable for the route address 1419 * type. Check that each address has a size that fits its family 1420 * and its length is within the size. Strings within addresses must 1421 * be NUL terminated. 1422 */ 1423 for (i = 0; i < RTAX_MAX; i++) { 1424 size_t len, maxlen, size; 1425 1426 sa = rtinfo->rti_info[i]; 1427 if (sa == NULL) 1428 continue; 1429 maxlen = size = 0; 1430 switch (i) { 1431 case RTAX_DST: 1432 case RTAX_GATEWAY: 1433 case RTAX_SRC: 1434 switch (sa->sa_family) { 1435 case AF_INET: 1436 size = sizeof(struct sockaddr_in); 1437 break; 1438 case AF_LINK: 1439 size = sizeof(struct sockaddr_dl); 1440 break; 1441 #ifdef INET6 1442 case AF_INET6: 1443 size = sizeof(struct sockaddr_in6); 1444 break; 1445 #endif 1446 #ifdef MPLS 1447 case AF_MPLS: 1448 size = sizeof(struct sockaddr_mpls); 1449 break; 1450 #endif 1451 } 1452 break; 1453 case RTAX_IFP: 1454 if (sa->sa_family != AF_LINK) 1455 return (EAFNOSUPPORT); 1456 /* 1457 * XXX Should be sizeof(struct sockaddr_dl), but 1458 * route(8) has a bug and provides less memory. 1459 * arp(8) has another bug and uses sizeof pointer. 1460 */ 1461 size = 4; 1462 break; 1463 case RTAX_IFA: 1464 switch (sa->sa_family) { 1465 case AF_INET: 1466 size = sizeof(struct sockaddr_in); 1467 break; 1468 #ifdef INET6 1469 case AF_INET6: 1470 size = sizeof(struct sockaddr_in6); 1471 break; 1472 #endif 1473 default: 1474 return (EAFNOSUPPORT); 1475 } 1476 break; 1477 case RTAX_LABEL: 1478 sa->sa_family = AF_UNSPEC; 1479 maxlen = RTLABEL_LEN; 1480 size = sizeof(struct sockaddr_rtlabel); 1481 break; 1482 #ifdef BFD 1483 case RTAX_BFD: 1484 sa->sa_family = AF_UNSPEC; 1485 size = sizeof(struct sockaddr_bfd); 1486 break; 1487 #endif 1488 case RTAX_DNS: 1489 /* more validation in rtm_validate_proposal */ 1490 if (sa->sa_len > sizeof(struct sockaddr_rtdns)) 1491 return (EINVAL); 1492 if (sa->sa_len < offsetof(struct sockaddr_rtdns, 1493 sr_dns)) 1494 return (EINVAL); 1495 switch (sa->sa_family) { 1496 case AF_INET: 1497 #ifdef INET6 1498 case AF_INET6: 1499 #endif 1500 break; 1501 default: 1502 return (EAFNOSUPPORT); 1503 } 1504 break; 1505 case RTAX_STATIC: 1506 sa->sa_family = AF_UNSPEC; 1507 maxlen = RTSTATIC_LEN; 1508 size = sizeof(struct sockaddr_rtstatic); 1509 break; 1510 case RTAX_SEARCH: 1511 sa->sa_family = AF_UNSPEC; 1512 maxlen = RTSEARCH_LEN; 1513 size = sizeof(struct sockaddr_rtsearch); 1514 break; 1515 } 1516 if (size) { 1517 /* memory for the full struct must be provided */ 1518 if (sa->sa_len < size) 1519 return (EINVAL); 1520 } 1521 if (maxlen) { 1522 /* this should not happen */ 1523 if (2 + maxlen > size) 1524 return (EINVAL); 1525 /* strings must be NUL terminated within the struct */ 1526 len = strnlen(sa->sa_data, maxlen); 1527 if (len >= maxlen || 2 + len >= sa->sa_len) 1528 return (EINVAL); 1529 break; 1530 } 1531 } 1532 return (0); 1533 } 1534 1535 struct mbuf * 1536 rtm_msg1(int type, struct rt_addrinfo *rtinfo) 1537 { 1538 struct rt_msghdr *rtm; 1539 struct mbuf *m; 1540 int i; 1541 struct sockaddr *sa; 1542 int len, dlen, hlen; 1543 1544 switch (type) { 1545 case RTM_DELADDR: 1546 case RTM_NEWADDR: 1547 len = sizeof(struct ifa_msghdr); 1548 break; 1549 case RTM_IFINFO: 1550 len = sizeof(struct if_msghdr); 1551 break; 1552 case RTM_IFANNOUNCE: 1553 len = sizeof(struct if_announcemsghdr); 1554 break; 1555 #ifdef BFD 1556 case RTM_BFD: 1557 len = sizeof(struct bfd_msghdr); 1558 break; 1559 #endif 1560 case RTM_80211INFO: 1561 len = sizeof(struct if_ieee80211_msghdr); 1562 break; 1563 default: 1564 len = sizeof(struct rt_msghdr); 1565 break; 1566 } 1567 if (len > MCLBYTES) 1568 panic("rtm_msg1"); 1569 m = m_gethdr(M_DONTWAIT, MT_DATA); 1570 if (m && len > MHLEN) { 1571 MCLGET(m, M_DONTWAIT); 1572 if ((m->m_flags & M_EXT) == 0) { 1573 m_free(m); 1574 m = NULL; 1575 } 1576 } 1577 if (m == NULL) 1578 return (m); 1579 m->m_pkthdr.len = m->m_len = hlen = len; 1580 m->m_pkthdr.ph_ifidx = 0; 1581 rtm = mtod(m, struct rt_msghdr *); 1582 bzero(rtm, len); 1583 for (i = 0; i < RTAX_MAX; i++) { 1584 if (rtinfo == NULL || (sa = rtinfo->rti_info[i]) == NULL) 1585 continue; 1586 rtinfo->rti_addrs |= (1 << i); 1587 dlen = ROUNDUP(sa->sa_len); 1588 if (m_copyback(m, len, dlen, sa, M_NOWAIT)) { 1589 m_freem(m); 1590 return (NULL); 1591 } 1592 len += dlen; 1593 } 1594 rtm->rtm_msglen = len; 1595 rtm->rtm_hdrlen = hlen; 1596 rtm->rtm_version = RTM_VERSION; 1597 rtm->rtm_type = type; 1598 return (m); 1599 } 1600 1601 int 1602 rtm_msg2(int type, int vers, struct rt_addrinfo *rtinfo, caddr_t cp, 1603 struct walkarg *w) 1604 { 1605 int i; 1606 int len, dlen, hlen, second_time = 0; 1607 caddr_t cp0; 1608 1609 rtinfo->rti_addrs = 0; 1610 again: 1611 switch (type) { 1612 case RTM_DELADDR: 1613 case RTM_NEWADDR: 1614 len = sizeof(struct ifa_msghdr); 1615 break; 1616 case RTM_IFINFO: 1617 len = sizeof(struct if_msghdr); 1618 break; 1619 default: 1620 len = sizeof(struct rt_msghdr); 1621 break; 1622 } 1623 hlen = len; 1624 if ((cp0 = cp) != NULL) 1625 cp += len; 1626 for (i = 0; i < RTAX_MAX; i++) { 1627 struct sockaddr *sa; 1628 1629 if ((sa = rtinfo->rti_info[i]) == NULL) 1630 continue; 1631 rtinfo->rti_addrs |= (1 << i); 1632 dlen = ROUNDUP(sa->sa_len); 1633 if (cp) { 1634 bcopy(sa, cp, (size_t)dlen); 1635 cp += dlen; 1636 } 1637 len += dlen; 1638 } 1639 /* align message length to the next natural boundary */ 1640 len = ALIGN(len); 1641 if (cp == 0 && w != NULL && !second_time) { 1642 w->w_needed += len; 1643 if (w->w_needed <= 0 && w->w_where) { 1644 if (w->w_tmemsize < len) { 1645 free(w->w_tmem, M_RTABLE, w->w_tmemsize); 1646 w->w_tmem = malloc(len, M_RTABLE, 1647 M_NOWAIT | M_ZERO); 1648 if (w->w_tmem) 1649 w->w_tmemsize = len; 1650 } 1651 if (w->w_tmem) { 1652 cp = w->w_tmem; 1653 second_time = 1; 1654 goto again; 1655 } else 1656 w->w_where = 0; 1657 } 1658 } 1659 if (cp && w) /* clear the message header */ 1660 bzero(cp0, hlen); 1661 1662 if (cp) { 1663 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1664 1665 rtm->rtm_version = RTM_VERSION; 1666 rtm->rtm_type = type; 1667 rtm->rtm_msglen = len; 1668 rtm->rtm_hdrlen = hlen; 1669 } 1670 return (len); 1671 } 1672 1673 void 1674 rtm_send(struct rtentry *rt, int cmd, int error, unsigned int rtableid) 1675 { 1676 struct rt_addrinfo info; 1677 struct ifnet *ifp; 1678 struct sockaddr_rtlabel sa_rl; 1679 struct sockaddr_in6 sa_mask; 1680 1681 memset(&info, 0, sizeof(info)); 1682 info.rti_info[RTAX_DST] = rt_key(rt); 1683 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1684 if (!ISSET(rt->rt_flags, RTF_HOST)) 1685 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 1686 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 1687 ifp = if_get(rt->rt_ifidx); 1688 if (ifp != NULL) { 1689 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1690 info.rti_info[RTAX_IFA] = 1691 rtable_getsource(rtableid, info.rti_info[RTAX_DST]->sa_family); 1692 if (info.rti_info[RTAX_IFA] == NULL) 1693 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1694 } 1695 1696 rtm_miss(cmd, &info, rt->rt_flags, rt->rt_priority, rt->rt_ifidx, error, 1697 rtableid); 1698 if_put(ifp); 1699 } 1700 1701 /* 1702 * This routine is called to generate a message from the routing 1703 * socket indicating that a redirect has occurred, a routing lookup 1704 * has failed, or that a protocol has detected timeouts to a particular 1705 * destination. 1706 */ 1707 void 1708 rtm_miss(int type, struct rt_addrinfo *rtinfo, int flags, uint8_t prio, 1709 u_int ifidx, int error, u_int tableid) 1710 { 1711 struct rt_msghdr *rtm; 1712 struct mbuf *m; 1713 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1714 1715 if (rtptable.rtp_count == 0) 1716 return; 1717 m = rtm_msg1(type, rtinfo); 1718 if (m == NULL) 1719 return; 1720 rtm = mtod(m, struct rt_msghdr *); 1721 rtm->rtm_flags = RTF_DONE | flags; 1722 rtm->rtm_priority = prio; 1723 rtm->rtm_errno = error; 1724 rtm->rtm_tableid = tableid; 1725 rtm->rtm_addrs = rtinfo->rti_addrs; 1726 rtm->rtm_index = ifidx; 1727 route_input(m, NULL, sa ? sa->sa_family : AF_UNSPEC); 1728 } 1729 1730 /* 1731 * This routine is called to generate a message from the routing 1732 * socket indicating that the status of a network interface has changed. 1733 */ 1734 void 1735 rtm_ifchg(struct ifnet *ifp) 1736 { 1737 struct rt_addrinfo info; 1738 struct if_msghdr *ifm; 1739 struct mbuf *m; 1740 1741 if (rtptable.rtp_count == 0) 1742 return; 1743 memset(&info, 0, sizeof(info)); 1744 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1745 m = rtm_msg1(RTM_IFINFO, &info); 1746 if (m == NULL) 1747 return; 1748 ifm = mtod(m, struct if_msghdr *); 1749 ifm->ifm_index = ifp->if_index; 1750 ifm->ifm_tableid = ifp->if_rdomain; 1751 ifm->ifm_flags = ifp->if_flags; 1752 ifm->ifm_xflags = ifp->if_xflags; 1753 if_getdata(ifp, &ifm->ifm_data); 1754 ifm->ifm_addrs = info.rti_addrs; 1755 route_input(m, NULL, AF_UNSPEC); 1756 } 1757 1758 /* 1759 * This is called to generate messages from the routing socket 1760 * indicating a network interface has had addresses associated with it. 1761 * if we ever reverse the logic and replace messages TO the routing 1762 * socket indicate a request to configure interfaces, then it will 1763 * be unnecessary as the routing socket will automatically generate 1764 * copies of it. 1765 */ 1766 void 1767 rtm_addr(int cmd, struct ifaddr *ifa) 1768 { 1769 struct ifnet *ifp = ifa->ifa_ifp; 1770 struct mbuf *m; 1771 struct rt_addrinfo info; 1772 struct ifa_msghdr *ifam; 1773 1774 if (rtptable.rtp_count == 0) 1775 return; 1776 1777 memset(&info, 0, sizeof(info)); 1778 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1779 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1780 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1781 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1782 if ((m = rtm_msg1(cmd, &info)) == NULL) 1783 return; 1784 ifam = mtod(m, struct ifa_msghdr *); 1785 ifam->ifam_index = ifp->if_index; 1786 ifam->ifam_metric = ifa->ifa_metric; 1787 ifam->ifam_flags = ifa->ifa_flags; 1788 ifam->ifam_addrs = info.rti_addrs; 1789 ifam->ifam_tableid = ifp->if_rdomain; 1790 1791 route_input(m, NULL, 1792 ifa->ifa_addr ? ifa->ifa_addr->sa_family : AF_UNSPEC); 1793 } 1794 1795 /* 1796 * This is called to generate routing socket messages indicating 1797 * network interface arrival and departure. 1798 */ 1799 void 1800 rtm_ifannounce(struct ifnet *ifp, int what) 1801 { 1802 struct if_announcemsghdr *ifan; 1803 struct mbuf *m; 1804 1805 if (rtptable.rtp_count == 0) 1806 return; 1807 m = rtm_msg1(RTM_IFANNOUNCE, NULL); 1808 if (m == NULL) 1809 return; 1810 ifan = mtod(m, struct if_announcemsghdr *); 1811 ifan->ifan_index = ifp->if_index; 1812 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 1813 ifan->ifan_what = what; 1814 route_input(m, NULL, AF_UNSPEC); 1815 } 1816 1817 #ifdef BFD 1818 /* 1819 * This is used to generate routing socket messages indicating 1820 * the state of a BFD session. 1821 */ 1822 void 1823 rtm_bfd(struct bfd_config *bfd) 1824 { 1825 struct bfd_msghdr *bfdm; 1826 struct sockaddr_bfd sa_bfd; 1827 struct mbuf *m; 1828 struct rt_addrinfo info; 1829 1830 if (rtptable.rtp_count == 0) 1831 return; 1832 memset(&info, 0, sizeof(info)); 1833 info.rti_info[RTAX_DST] = rt_key(bfd->bc_rt); 1834 info.rti_info[RTAX_IFA] = bfd->bc_rt->rt_ifa->ifa_addr; 1835 1836 m = rtm_msg1(RTM_BFD, &info); 1837 if (m == NULL) 1838 return; 1839 bfdm = mtod(m, struct bfd_msghdr *); 1840 bfdm->bm_addrs = info.rti_addrs; 1841 1842 bfd2sa(bfd->bc_rt, &sa_bfd); 1843 memcpy(&bfdm->bm_sa, &sa_bfd, sizeof(sa_bfd)); 1844 1845 route_input(m, NULL, info.rti_info[RTAX_DST]->sa_family); 1846 } 1847 #endif /* BFD */ 1848 1849 /* 1850 * This is used to generate routing socket messages indicating 1851 * the state of an ieee80211 interface. 1852 */ 1853 void 1854 rtm_80211info(struct ifnet *ifp, struct if_ieee80211_data *ifie) 1855 { 1856 struct if_ieee80211_msghdr *ifim; 1857 struct mbuf *m; 1858 1859 if (rtptable.rtp_count == 0) 1860 return; 1861 m = rtm_msg1(RTM_80211INFO, NULL); 1862 if (m == NULL) 1863 return; 1864 ifim = mtod(m, struct if_ieee80211_msghdr *); 1865 ifim->ifim_index = ifp->if_index; 1866 ifim->ifim_tableid = ifp->if_rdomain; 1867 1868 memcpy(&ifim->ifim_ifie, ifie, sizeof(ifim->ifim_ifie)); 1869 route_input(m, NULL, AF_UNSPEC); 1870 } 1871 1872 /* 1873 * This is used to generate routing socket messages indicating 1874 * the address selection proposal from an interface. 1875 */ 1876 void 1877 rtm_proposal(struct ifnet *ifp, struct rt_addrinfo *rtinfo, int flags, 1878 uint8_t prio) 1879 { 1880 struct rt_msghdr *rtm; 1881 struct mbuf *m; 1882 1883 m = rtm_msg1(RTM_PROPOSAL, rtinfo); 1884 if (m == NULL) 1885 return; 1886 rtm = mtod(m, struct rt_msghdr *); 1887 rtm->rtm_flags = RTF_DONE | flags; 1888 rtm->rtm_priority = prio; 1889 rtm->rtm_tableid = ifp->if_rdomain; 1890 rtm->rtm_index = ifp->if_index; 1891 rtm->rtm_addrs = rtinfo->rti_addrs; 1892 1893 route_input(m, NULL, rtinfo->rti_info[RTAX_DNS]->sa_family); 1894 } 1895 1896 /* 1897 * This is used in dumping the kernel table via sysctl(). 1898 */ 1899 int 1900 sysctl_dumpentry(struct rtentry *rt, void *v, unsigned int id) 1901 { 1902 struct walkarg *w = v; 1903 int error = 0, size; 1904 struct rt_addrinfo info; 1905 struct ifnet *ifp; 1906 #ifdef BFD 1907 struct sockaddr_bfd sa_bfd; 1908 #endif 1909 struct sockaddr_rtlabel sa_rl; 1910 struct sockaddr_in6 sa_mask; 1911 1912 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1913 return 0; 1914 if (w->w_op == NET_RT_DUMP && w->w_arg) { 1915 u_int8_t prio = w->w_arg & RTP_MASK; 1916 if (w->w_arg < 0) { 1917 prio = (-w->w_arg) & RTP_MASK; 1918 /* Show all routes that are not this priority */ 1919 if (prio == (rt->rt_priority & RTP_MASK)) 1920 return 0; 1921 } else { 1922 if (prio != (rt->rt_priority & RTP_MASK) && 1923 prio != RTP_ANY) 1924 return 0; 1925 } 1926 } 1927 bzero(&info, sizeof(info)); 1928 info.rti_info[RTAX_DST] = rt_key(rt); 1929 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1930 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 1931 ifp = if_get(rt->rt_ifidx); 1932 if (ifp != NULL) { 1933 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1934 info.rti_info[RTAX_IFA] = 1935 rtable_getsource(id, info.rti_info[RTAX_DST]->sa_family); 1936 if (info.rti_info[RTAX_IFA] == NULL) 1937 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1938 if (ifp->if_flags & IFF_POINTOPOINT) 1939 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1940 } 1941 if_put(ifp); 1942 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 1943 #ifdef BFD 1944 if (rt->rt_flags & RTF_BFD) 1945 info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd); 1946 #endif 1947 #ifdef MPLS 1948 if (rt->rt_flags & RTF_MPLS) { 1949 struct sockaddr_mpls sa_mpls; 1950 1951 bzero(&sa_mpls, sizeof(sa_mpls)); 1952 sa_mpls.smpls_family = AF_MPLS; 1953 sa_mpls.smpls_len = sizeof(sa_mpls); 1954 sa_mpls.smpls_label = ((struct rt_mpls *) 1955 rt->rt_llinfo)->mpls_label; 1956 info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls; 1957 info.rti_mpls = ((struct rt_mpls *) 1958 rt->rt_llinfo)->mpls_operation; 1959 } 1960 #endif 1961 1962 size = rtm_msg2(RTM_GET, RTM_VERSION, &info, NULL, w); 1963 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1964 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1965 1966 rtm->rtm_pid = curproc->p_p->ps_pid; 1967 rtm->rtm_flags = rt->rt_flags; 1968 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 1969 rtm_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1970 /* Do not account the routing table's reference. */ 1971 rtm->rtm_rmx.rmx_refcnt = rt->rt_refcnt - 1; 1972 rtm->rtm_index = rt->rt_ifidx; 1973 rtm->rtm_addrs = info.rti_addrs; 1974 rtm->rtm_tableid = id; 1975 #ifdef MPLS 1976 rtm->rtm_mpls = info.rti_mpls; 1977 #endif 1978 if ((error = copyout(rtm, w->w_where, size)) != 0) 1979 w->w_where = NULL; 1980 else 1981 w->w_where += size; 1982 } 1983 return (error); 1984 } 1985 1986 int 1987 sysctl_iflist(int af, struct walkarg *w) 1988 { 1989 struct ifnet *ifp; 1990 struct ifaddr *ifa; 1991 struct rt_addrinfo info; 1992 int len, error = 0; 1993 1994 bzero(&info, sizeof(info)); 1995 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1996 if (w->w_arg && w->w_arg != ifp->if_index) 1997 continue; 1998 /* Copy the link-layer address first */ 1999 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 2000 len = rtm_msg2(RTM_IFINFO, RTM_VERSION, &info, 0, w); 2001 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 2002 struct if_msghdr *ifm; 2003 2004 ifm = (struct if_msghdr *)w->w_tmem; 2005 ifm->ifm_index = ifp->if_index; 2006 ifm->ifm_tableid = ifp->if_rdomain; 2007 ifm->ifm_flags = ifp->if_flags; 2008 if_getdata(ifp, &ifm->ifm_data); 2009 ifm->ifm_addrs = info.rti_addrs; 2010 error = copyout(ifm, w->w_where, len); 2011 if (error) 2012 return (error); 2013 w->w_where += len; 2014 } 2015 info.rti_info[RTAX_IFP] = NULL; 2016 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2017 KASSERT(ifa->ifa_addr->sa_family != AF_LINK); 2018 if (af && af != ifa->ifa_addr->sa_family) 2019 continue; 2020 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 2021 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 2022 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 2023 len = rtm_msg2(RTM_NEWADDR, RTM_VERSION, &info, 0, w); 2024 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 2025 struct ifa_msghdr *ifam; 2026 2027 ifam = (struct ifa_msghdr *)w->w_tmem; 2028 ifam->ifam_index = ifa->ifa_ifp->if_index; 2029 ifam->ifam_flags = ifa->ifa_flags; 2030 ifam->ifam_metric = ifa->ifa_metric; 2031 ifam->ifam_addrs = info.rti_addrs; 2032 error = copyout(w->w_tmem, w->w_where, len); 2033 if (error) 2034 return (error); 2035 w->w_where += len; 2036 } 2037 } 2038 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 2039 info.rti_info[RTAX_BRD] = NULL; 2040 } 2041 return (0); 2042 } 2043 2044 int 2045 sysctl_ifnames(struct walkarg *w) 2046 { 2047 struct if_nameindex_msg ifn; 2048 struct ifnet *ifp; 2049 int error = 0; 2050 2051 /* XXX ignore tableid for now */ 2052 TAILQ_FOREACH(ifp, &ifnet, if_list) { 2053 if (w->w_arg && w->w_arg != ifp->if_index) 2054 continue; 2055 w->w_needed += sizeof(ifn); 2056 if (w->w_where && w->w_needed <= 0) { 2057 2058 memset(&ifn, 0, sizeof(ifn)); 2059 ifn.if_index = ifp->if_index; 2060 strlcpy(ifn.if_name, ifp->if_xname, 2061 sizeof(ifn.if_name)); 2062 error = copyout(&ifn, w->w_where, sizeof(ifn)); 2063 if (error) 2064 return (error); 2065 w->w_where += sizeof(ifn); 2066 } 2067 } 2068 2069 return (0); 2070 } 2071 2072 int 2073 sysctl_source(int af, u_int tableid, struct walkarg *w) 2074 { 2075 struct sockaddr *sa; 2076 int size, error = 0; 2077 2078 sa = rtable_getsource(tableid, af); 2079 if (sa) { 2080 switch (sa->sa_family) { 2081 case AF_INET: 2082 size = sizeof(struct sockaddr_in); 2083 break; 2084 #ifdef INET6 2085 case AF_INET6: 2086 size = sizeof(struct sockaddr_in6); 2087 break; 2088 #endif 2089 default: 2090 return (0); 2091 } 2092 w->w_needed += size; 2093 if (w->w_where && w->w_needed <= 0) { 2094 if ((error = copyout(sa, w->w_where, size))) 2095 return (error); 2096 w->w_where += size; 2097 } 2098 } 2099 return (0); 2100 } 2101 2102 int 2103 sysctl_rtable(int *name, u_int namelen, void *where, size_t *given, void *new, 2104 size_t newlen) 2105 { 2106 int i, error = EINVAL; 2107 u_char af; 2108 struct walkarg w; 2109 struct rt_tableinfo tableinfo; 2110 u_int tableid = 0; 2111 2112 if (new) 2113 return (EPERM); 2114 if (namelen < 3 || namelen > 4) 2115 return (EINVAL); 2116 af = name[0]; 2117 bzero(&w, sizeof(w)); 2118 w.w_where = where; 2119 w.w_given = *given; 2120 w.w_needed = 0 - w.w_given; 2121 w.w_op = name[1]; 2122 w.w_arg = name[2]; 2123 2124 if (namelen == 4) { 2125 tableid = name[3]; 2126 if (!rtable_exists(tableid)) 2127 return (ENOENT); 2128 } else 2129 tableid = curproc->p_p->ps_rtableid; 2130 2131 switch (w.w_op) { 2132 case NET_RT_DUMP: 2133 case NET_RT_FLAGS: 2134 NET_LOCK(); 2135 for (i = 1; i <= AF_MAX; i++) { 2136 if (af != 0 && af != i) 2137 continue; 2138 2139 error = rtable_walk(tableid, i, NULL, sysctl_dumpentry, 2140 &w); 2141 if (error == EAFNOSUPPORT) 2142 error = 0; 2143 if (error) 2144 break; 2145 } 2146 NET_UNLOCK(); 2147 break; 2148 2149 case NET_RT_IFLIST: 2150 NET_LOCK(); 2151 error = sysctl_iflist(af, &w); 2152 NET_UNLOCK(); 2153 break; 2154 2155 case NET_RT_STATS: 2156 return (sysctl_rtable_rtstat(where, given, new)); 2157 case NET_RT_TABLE: 2158 tableid = w.w_arg; 2159 if (!rtable_exists(tableid)) 2160 return (ENOENT); 2161 memset(&tableinfo, 0, sizeof tableinfo); 2162 tableinfo.rti_tableid = tableid; 2163 tableinfo.rti_domainid = rtable_l2(tableid); 2164 error = sysctl_rdstruct(where, given, new, 2165 &tableinfo, sizeof(tableinfo)); 2166 return (error); 2167 case NET_RT_IFNAMES: 2168 NET_LOCK(); 2169 error = sysctl_ifnames(&w); 2170 NET_UNLOCK(); 2171 break; 2172 case NET_RT_SOURCE: 2173 tableid = w.w_arg; 2174 if (!rtable_exists(tableid)) 2175 return (ENOENT); 2176 NET_LOCK(); 2177 for (i = 1; i <= AF_MAX; i++) { 2178 if (af != 0 && af != i) 2179 continue; 2180 2181 error = sysctl_source(i, tableid, &w); 2182 if (error == EAFNOSUPPORT) 2183 error = 0; 2184 if (error) 2185 break; 2186 } 2187 NET_UNLOCK(); 2188 break; 2189 } 2190 free(w.w_tmem, M_RTABLE, w.w_tmemsize); 2191 w.w_needed += w.w_given; 2192 if (where) { 2193 *given = w.w_where - (caddr_t)where; 2194 if (*given < w.w_needed) 2195 return (ENOMEM); 2196 } else 2197 *given = (11 * w.w_needed) / 10; 2198 2199 return (error); 2200 } 2201 2202 int 2203 sysctl_rtable_rtstat(void *oldp, size_t *oldlenp, void *newp) 2204 { 2205 extern struct cpumem *rtcounters; 2206 uint64_t counters[rts_ncounters]; 2207 struct rtstat rtstat; 2208 uint32_t *words = (uint32_t *)&rtstat; 2209 int i; 2210 2211 CTASSERT(sizeof(rtstat) == (nitems(counters) * sizeof(uint32_t))); 2212 memset(&rtstat, 0, sizeof rtstat); 2213 counters_read(rtcounters, counters, nitems(counters)); 2214 2215 for (i = 0; i < nitems(counters); i++) 2216 words[i] = (uint32_t)counters[i]; 2217 2218 return (sysctl_rdstruct(oldp, oldlenp, newp, &rtstat, sizeof(rtstat))); 2219 } 2220 2221 int 2222 rtm_validate_proposal(struct rt_addrinfo *info) 2223 { 2224 if (info->rti_addrs & ~(RTA_NETMASK | RTA_IFA | RTA_DNS | RTA_STATIC | 2225 RTA_SEARCH)) { 2226 return -1; 2227 } 2228 2229 if (ISSET(info->rti_addrs, RTA_NETMASK)) { 2230 struct sockaddr *sa = info->rti_info[RTAX_NETMASK]; 2231 if (sa == NULL) 2232 return -1; 2233 switch (sa->sa_family) { 2234 case AF_INET: 2235 if (sa->sa_len != sizeof(struct sockaddr_in)) 2236 return -1; 2237 break; 2238 case AF_INET6: 2239 if (sa->sa_len != sizeof(struct sockaddr_in6)) 2240 return -1; 2241 break; 2242 default: 2243 return -1; 2244 } 2245 } 2246 2247 if (ISSET(info->rti_addrs, RTA_IFA)) { 2248 struct sockaddr *sa = info->rti_info[RTAX_IFA]; 2249 if (sa == NULL) 2250 return -1; 2251 switch (sa->sa_family) { 2252 case AF_INET: 2253 if (sa->sa_len != sizeof(struct sockaddr_in)) 2254 return -1; 2255 break; 2256 case AF_INET6: 2257 if (sa->sa_len != sizeof(struct sockaddr_in6)) 2258 return -1; 2259 break; 2260 default: 2261 return -1; 2262 } 2263 } 2264 2265 if (ISSET(info->rti_addrs, RTA_DNS)) { 2266 struct sockaddr_rtdns *rtdns = 2267 (struct sockaddr_rtdns *)info->rti_info[RTAX_DNS]; 2268 if (rtdns == NULL) 2269 return -1; 2270 if (rtdns->sr_len > sizeof(*rtdns)) 2271 return -1; 2272 if (rtdns->sr_len < offsetof(struct sockaddr_rtdns, sr_dns)) 2273 return -1; 2274 switch (rtdns->sr_family) { 2275 case AF_INET: 2276 if ((rtdns->sr_len - offsetof(struct sockaddr_rtdns, 2277 sr_dns)) % sizeof(struct in_addr) != 0) 2278 return -1; 2279 break; 2280 #ifdef INET6 2281 case AF_INET6: 2282 if ((rtdns->sr_len - offsetof(struct sockaddr_rtdns, 2283 sr_dns)) % sizeof(struct in6_addr) != 0) 2284 return -1; 2285 break; 2286 #endif 2287 default: 2288 return -1; 2289 } 2290 } 2291 2292 if (ISSET(info->rti_addrs, RTA_STATIC)) { 2293 struct sockaddr_rtstatic *rtstatic = 2294 (struct sockaddr_rtstatic *)info->rti_info[RTAX_STATIC]; 2295 if (rtstatic == NULL) 2296 return -1; 2297 if (rtstatic->sr_len > sizeof(*rtstatic)) 2298 return -1; 2299 if (rtstatic->sr_len <= 2300 offsetof(struct sockaddr_rtstatic, sr_static)) 2301 return -1; 2302 } 2303 2304 if (ISSET(info->rti_addrs, RTA_SEARCH)) { 2305 struct sockaddr_rtsearch *rtsearch = 2306 (struct sockaddr_rtsearch *)info->rti_info[RTAX_SEARCH]; 2307 if (rtsearch == NULL) 2308 return -1; 2309 if (rtsearch->sr_len > sizeof(*rtsearch)) 2310 return -1; 2311 if (rtsearch->sr_len <= 2312 offsetof(struct sockaddr_rtsearch, sr_search)) 2313 return -1; 2314 } 2315 2316 return 0; 2317 } 2318 2319 int 2320 rt_setsource(unsigned int rtableid, struct sockaddr *src) 2321 { 2322 struct ifaddr *ifa; 2323 int error; 2324 /* 2325 * If source address is 0.0.0.0 or :: 2326 * use automatic source selection 2327 */ 2328 switch(src->sa_family) { 2329 case AF_INET: 2330 if(satosin(src)->sin_addr.s_addr == INADDR_ANY) { 2331 rtable_setsource(rtableid, AF_INET, NULL); 2332 return (0); 2333 } 2334 break; 2335 #ifdef INET6 2336 case AF_INET6: 2337 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)) { 2338 rtable_setsource(rtableid, AF_INET6, NULL); 2339 return (0); 2340 } 2341 break; 2342 #endif 2343 default: 2344 return (EAFNOSUPPORT); 2345 } 2346 2347 KERNEL_LOCK(); 2348 /* 2349 * Check if source address is assigned to an interface in the 2350 * same rdomain 2351 */ 2352 if ((ifa = ifa_ifwithaddr(src, rtableid)) == NULL) { 2353 KERNEL_UNLOCK(); 2354 return (EINVAL); 2355 } 2356 2357 error = rtable_setsource(rtableid, src->sa_family, ifa->ifa_addr); 2358 KERNEL_UNLOCK(); 2359 2360 return (error); 2361 } 2362 2363 /* 2364 * Definitions of protocols supported in the ROUTE domain. 2365 */ 2366 2367 const struct protosw routesw[] = { 2368 { 2369 .pr_type = SOCK_RAW, 2370 .pr_domain = &routedomain, 2371 .pr_flags = PR_ATOMIC|PR_ADDR|PR_WANTRCVD, 2372 .pr_output = route_output, 2373 .pr_ctloutput = route_ctloutput, 2374 .pr_usrreq = route_usrreq, 2375 .pr_attach = route_attach, 2376 .pr_detach = route_detach, 2377 .pr_init = route_prinit, 2378 .pr_sysctl = sysctl_rtable 2379 } 2380 }; 2381 2382 const struct domain routedomain = { 2383 .dom_family = PF_ROUTE, 2384 .dom_name = "route", 2385 .dom_init = route_init, 2386 .dom_protosw = routesw, 2387 .dom_protoswNPROTOSW = &routesw[nitems(routesw)] 2388 }; 2389