1 /* $OpenBSD: rtsock.c,v 1.192 2016/06/14 09:48:52 mpi 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/protosw.h> 73 74 #include <net/if.h> 75 #include <net/if_dl.h> 76 #include <net/if_var.h> 77 #include <net/route.h> 78 #include <net/raw_cb.h> 79 80 #include <netinet/in.h> 81 82 #ifdef MPLS 83 #include <netmpls/mpls.h> 84 #endif 85 86 #include <sys/stdarg.h> 87 #include <sys/kernel.h> 88 #include <sys/timeout.h> 89 90 struct sockaddr route_dst = { 2, PF_ROUTE, }; 91 struct sockaddr route_src = { 2, PF_ROUTE, }; 92 struct sockproto route_proto = { PF_ROUTE, }; 93 94 struct walkarg { 95 int w_op, w_arg, w_given, w_needed, w_tmemsize; 96 caddr_t w_where, w_tmem; 97 }; 98 99 int route_ctloutput(int, struct socket *, int, int, struct mbuf **); 100 void route_input(struct mbuf *m0, ...); 101 int route_arp_conflict(struct rt_addrinfo *, unsigned int); 102 103 struct mbuf *rt_msg1(int, struct rt_addrinfo *); 104 int rt_msg2(int, int, struct rt_addrinfo *, caddr_t, 105 struct walkarg *); 106 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 107 108 int sysctl_iflist(int, struct walkarg *); 109 int sysctl_ifnames(struct walkarg *); 110 111 struct routecb { 112 struct rawcb rcb; 113 struct timeout timeout; 114 unsigned int msgfilter; 115 unsigned int flags; 116 u_int rtableid; 117 }; 118 #define sotoroutecb(so) ((struct routecb *)(so)->so_pcb) 119 120 struct route_cb { 121 int ip_count; 122 int ip6_count; 123 int mpls_count; 124 int any_count; 125 }; 126 127 struct route_cb route_cb; 128 129 /* 130 * These flags and timeout are used for indicating to userland (via a 131 * RTM_DESYNC msg) when the route socket has overflowed and messages 132 * have been lost. 133 */ 134 #define ROUTECB_FLAG_DESYNC 0x1 /* Route socket out of memory */ 135 #define ROUTECB_FLAG_FLUSH 0x2 /* Wait until socket is empty before 136 queueing more packets */ 137 138 #define ROUTE_DESYNC_RESEND_TIMEOUT (hz / 5) /* In hz */ 139 140 void rt_senddesync(void *); 141 142 int 143 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 144 struct mbuf *control, struct proc *p) 145 { 146 struct rawcb *rp; 147 struct routecb *rop; 148 int s, af; 149 int error = 0; 150 151 s = splsoftnet(); 152 rp = sotorawcb(so); 153 154 switch (req) { 155 case PRU_ATTACH: 156 /* 157 * use the rawcb but allocate a routecb, this 158 * code does not care about the additional fields 159 * and works directly on the raw socket. 160 */ 161 rop = malloc(sizeof(struct routecb), M_PCB, M_WAITOK|M_ZERO); 162 rp = &rop->rcb; 163 so->so_pcb = rp; 164 /* Init the timeout structure */ 165 timeout_set(&((struct routecb *)rp)->timeout, rt_senddesync, rp); 166 /* 167 * Don't call raw_usrreq() in the attach case, because 168 * we want to allow non-privileged processes to listen 169 * on and send "safe" commands to the routing socket. 170 */ 171 if (curproc == 0) 172 error = EACCES; 173 else 174 error = raw_attach(so, (int)(long)nam); 175 if (error) { 176 free(rop, M_PCB, sizeof(struct routecb)); 177 splx(s); 178 return (error); 179 } 180 rop->rtableid = curproc->p_p->ps_rtableid; 181 af = rp->rcb_proto.sp_protocol; 182 if (af == AF_INET) 183 route_cb.ip_count++; 184 else if (af == AF_INET6) 185 route_cb.ip6_count++; 186 #ifdef MPLS 187 else if (af == AF_MPLS) 188 route_cb.mpls_count++; 189 #endif 190 rp->rcb_faddr = &route_src; 191 route_cb.any_count++; 192 soisconnected(so); 193 so->so_options |= SO_USELOOPBACK; 194 break; 195 196 case PRU_RCVD: 197 rop = (struct routecb *)rp; 198 199 /* 200 * If we are in a FLUSH state, check if the buffer is 201 * empty so that we can clear the flag. 202 */ 203 if (((rop->flags & ROUTECB_FLAG_FLUSH) != 0) && 204 ((sbspace(&rp->rcb_socket->so_rcv) == 205 rp->rcb_socket->so_rcv.sb_hiwat))) 206 rop->flags &= ~ROUTECB_FLAG_FLUSH; 207 break; 208 209 case PRU_DETACH: 210 if (rp) { 211 timeout_del(&((struct routecb *)rp)->timeout); 212 af = rp->rcb_proto.sp_protocol; 213 if (af == AF_INET) 214 route_cb.ip_count--; 215 else if (af == AF_INET6) 216 route_cb.ip6_count--; 217 #ifdef MPLS 218 else if (af == AF_MPLS) 219 route_cb.mpls_count--; 220 #endif 221 route_cb.any_count--; 222 } 223 /* FALLTHROUGH */ 224 default: 225 error = raw_usrreq(so, req, m, nam, control, p); 226 } 227 228 splx(s); 229 return (error); 230 } 231 232 int 233 route_ctloutput(int op, struct socket *so, int level, int optname, 234 struct mbuf **mp) 235 { 236 struct routecb *rop = sotoroutecb(so); 237 struct mbuf *m = *mp; 238 int error = 0; 239 unsigned int tid; 240 241 if (level != AF_ROUTE) { 242 error = EINVAL; 243 if (op == PRCO_SETOPT && *mp) 244 m_free(*mp); 245 return (error); 246 } 247 248 switch (op) { 249 case PRCO_SETOPT: 250 switch (optname) { 251 case ROUTE_MSGFILTER: 252 if (m == NULL || m->m_len != sizeof(unsigned int)) 253 error = EINVAL; 254 else 255 rop->msgfilter = *mtod(m, unsigned int *); 256 break; 257 case ROUTE_TABLEFILTER: 258 if (m == NULL || m->m_len != sizeof(unsigned int)) { 259 error = EINVAL; 260 break; 261 } 262 tid = *mtod(m, unsigned int *); 263 if (tid != RTABLE_ANY && !rtable_exists(tid)) 264 error = ENOENT; 265 else 266 rop->rtableid = tid; 267 break; 268 default: 269 error = ENOPROTOOPT; 270 break; 271 } 272 if (m) 273 m_free(m); 274 break; 275 case PRCO_GETOPT: 276 switch (optname) { 277 case ROUTE_MSGFILTER: 278 *mp = m = m_get(M_WAIT, MT_SOOPTS); 279 m->m_len = sizeof(unsigned int); 280 *mtod(m, unsigned int *) = rop->msgfilter; 281 break; 282 case ROUTE_TABLEFILTER: 283 *mp = m = m_get(M_WAIT, MT_SOOPTS); 284 m->m_len = sizeof(unsigned int); 285 *mtod(m, unsigned int *) = rop->rtableid; 286 break; 287 default: 288 error = ENOPROTOOPT; 289 break; 290 } 291 } 292 return (error); 293 } 294 295 void 296 rt_senddesync(void *data) 297 { 298 struct rawcb *rp; 299 struct routecb *rop; 300 struct mbuf *desync_mbuf; 301 302 rp = (struct rawcb *)data; 303 rop = (struct routecb *)rp; 304 305 /* If we are in a DESYNC state, try to send a RTM_DESYNC packet */ 306 if ((rop->flags & ROUTECB_FLAG_DESYNC) != 0) { 307 /* 308 * If we fail to alloc memory or if sbappendaddr() 309 * fails, re-add timeout and try again. 310 */ 311 desync_mbuf = rt_msg1(RTM_DESYNC, NULL); 312 if ((desync_mbuf != NULL) && 313 (sbappendaddr(&rp->rcb_socket->so_rcv, &route_src, 314 desync_mbuf, (struct mbuf *)NULL) != 0)) { 315 rop->flags &= ~ROUTECB_FLAG_DESYNC; 316 sorwakeup(rp->rcb_socket); 317 } else { 318 m_freem(desync_mbuf); 319 /* Re-add timeout to try sending msg again */ 320 timeout_add(&rop->timeout, ROUTE_DESYNC_RESEND_TIMEOUT); 321 } 322 } 323 } 324 325 void 326 route_input(struct mbuf *m0, ...) 327 { 328 struct rawcb *rp; 329 struct routecb *rop; 330 struct rt_msghdr *rtm; 331 struct mbuf *m = m0; 332 int sockets = 0; 333 struct socket *last = NULL; 334 va_list ap; 335 struct sockproto *proto; 336 struct sockaddr *sosrc, *sodst; 337 338 va_start(ap, m0); 339 proto = va_arg(ap, struct sockproto *); 340 sosrc = va_arg(ap, struct sockaddr *); 341 sodst = va_arg(ap, struct sockaddr *); 342 va_end(ap); 343 344 /* ensure that we can access the rtm_type via mtod() */ 345 if (m->m_len < offsetof(struct rt_msghdr, rtm_type) + 1) { 346 m_freem(m); 347 return; 348 } 349 350 LIST_FOREACH(rp, &rawcb, rcb_list) { 351 if (rp->rcb_socket->so_state & SS_CANTRCVMORE) 352 continue; 353 if (rp->rcb_proto.sp_family != proto->sp_family) 354 continue; 355 if (rp->rcb_proto.sp_protocol && proto->sp_protocol && 356 rp->rcb_proto.sp_protocol != proto->sp_protocol) 357 continue; 358 /* 359 * We assume the lower level routines have 360 * placed the address in a canonical format 361 * suitable for a structure comparison. 362 * 363 * Note that if the lengths are not the same 364 * the comparison will fail at the first byte. 365 */ 366 #define equal(a1, a2) \ 367 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0) 368 if (rp->rcb_laddr && !equal(rp->rcb_laddr, sodst)) 369 continue; 370 if (rp->rcb_faddr && !equal(rp->rcb_faddr, sosrc)) 371 continue; 372 373 /* filter messages that the process does not want */ 374 rop = (struct routecb *)rp; 375 rtm = mtod(m, struct rt_msghdr *); 376 /* but RTM_DESYNC can't be filtered */ 377 if (rtm->rtm_type != RTM_DESYNC && rop->msgfilter != 0 && 378 !(rop->msgfilter & (1 << rtm->rtm_type))) 379 continue; 380 switch (rtm->rtm_type) { 381 case RTM_IFANNOUNCE: 382 case RTM_DESYNC: 383 /* no tableid */ 384 break; 385 case RTM_RESOLVE: 386 case RTM_NEWADDR: 387 case RTM_DELADDR: 388 case RTM_IFINFO: 389 /* check against rdomain id */ 390 if (rop->rtableid != RTABLE_ANY && 391 rtable_l2(rop->rtableid) != rtm->rtm_tableid) 392 continue; 393 break; 394 default: 395 /* check against rtable id */ 396 if (rop->rtableid != RTABLE_ANY && 397 rop->rtableid != rtm->rtm_tableid) 398 continue; 399 break; 400 } 401 402 /* 403 * Check to see if the flush flag is set. If so, don't queue 404 * any more messages until the flag is cleared. 405 */ 406 if ((rop->flags & ROUTECB_FLAG_FLUSH) != 0) 407 continue; 408 409 if (last) { 410 struct mbuf *n; 411 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) { 412 if (sbspace(&last->so_rcv) < (2 * MSIZE) || 413 sbappendaddr(&last->so_rcv, sosrc, 414 n, (struct mbuf *)NULL) == 0) { 415 /* 416 * Flag socket as desync'ed and 417 * flush required 418 */ 419 sotoroutecb(last)->flags |= 420 ROUTECB_FLAG_DESYNC | 421 ROUTECB_FLAG_FLUSH; 422 rt_senddesync((void *) sotorawcb(last)); 423 m_freem(n); 424 } else { 425 sorwakeup(last); 426 sockets++; 427 } 428 } 429 } 430 last = rp->rcb_socket; 431 } 432 if (last) { 433 if (sbspace(&last->so_rcv) < (2 * MSIZE) || 434 sbappendaddr(&last->so_rcv, sosrc, 435 m, (struct mbuf *)NULL) == 0) { 436 /* Flag socket as desync'ed and flush required */ 437 sotoroutecb(last)->flags |= 438 ROUTECB_FLAG_DESYNC | ROUTECB_FLAG_FLUSH; 439 rt_senddesync((void *) sotorawcb(last)); 440 m_freem(m); 441 } else { 442 sorwakeup(last); 443 sockets++; 444 } 445 } else 446 m_freem(m); 447 } 448 449 int 450 route_output(struct mbuf *m, ...) 451 { 452 struct rt_msghdr *rtm = NULL; 453 struct rtentry *rt = NULL; 454 struct rtentry *saved_nrt = NULL; 455 struct rt_addrinfo info; 456 int plen, len, newgate, error = 0; 457 struct ifnet *ifp = NULL; 458 struct ifaddr *ifa = NULL; 459 struct socket *so; 460 struct rawcb *rp = NULL; 461 struct sockaddr_rtlabel sa_rl; 462 struct sockaddr_in6 sa_mask; 463 #ifdef MPLS 464 struct sockaddr_mpls sa_mpls, *psa_mpls; 465 #endif 466 va_list ap; 467 u_int tableid; 468 u_int8_t prio; 469 u_char vers; 470 471 va_start(ap, m); 472 so = va_arg(ap, struct socket *); 473 va_end(ap); 474 475 info.rti_info[RTAX_DST] = NULL; /* for error handling (goto flush) */ 476 if (m == NULL || ((m->m_len < sizeof(int32_t)) && 477 (m = m_pullup(m, sizeof(int32_t))) == 0)) 478 return (ENOBUFS); 479 if ((m->m_flags & M_PKTHDR) == 0) 480 panic("route_output"); 481 len = m->m_pkthdr.len; 482 if (len < offsetof(struct rt_msghdr, rtm_type) + 1 || 483 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 484 error = EINVAL; 485 goto fail; 486 } 487 vers = mtod(m, struct rt_msghdr *)->rtm_version; 488 switch (vers) { 489 case RTM_VERSION: 490 if (len < sizeof(struct rt_msghdr)) { 491 error = EINVAL; 492 goto fail; 493 } 494 if (len > RTM_MAXSIZE) { 495 error = EMSGSIZE; 496 goto fail; 497 } 498 rtm = malloc(len, M_RTABLE, M_NOWAIT); 499 if (rtm == NULL) { 500 error = ENOBUFS; 501 goto fail; 502 } 503 m_copydata(m, 0, len, (caddr_t)rtm); 504 break; 505 default: 506 error = EPROTONOSUPPORT; 507 goto fail; 508 } 509 rtm->rtm_pid = curproc->p_p->ps_pid; 510 if (rtm->rtm_hdrlen == 0) /* old client */ 511 rtm->rtm_hdrlen = sizeof(struct rt_msghdr); 512 if (len < rtm->rtm_hdrlen) { 513 error = EINVAL; 514 goto fail; 515 } 516 517 /* Verify that the caller is sending an appropriate message early */ 518 switch (rtm->rtm_type) { 519 case RTM_ADD: 520 case RTM_DELETE: 521 case RTM_GET: 522 case RTM_CHANGE: 523 case RTM_LOCK: 524 break; 525 default: 526 error = EOPNOTSUPP; 527 goto fail; 528 } 529 530 /* 531 * Verify that the caller has the appropriate privilege; RTM_GET 532 * is the only operation the non-superuser is allowed. 533 */ 534 if (rtm->rtm_type != RTM_GET && suser(curproc, 0) != 0) { 535 error = EACCES; 536 goto fail; 537 } 538 539 tableid = rtm->rtm_tableid; 540 if (!rtable_exists(tableid)) { 541 if (rtm->rtm_type == RTM_ADD) { 542 if ((error = rtable_add(tableid)) != 0) 543 goto flush; 544 } else { 545 error = EINVAL; 546 goto flush; 547 } 548 } 549 550 551 /* Do not let userland play with kernel-only flags. */ 552 if ((rtm->rtm_flags & (RTF_LOCAL|RTF_BROADCAST)) != 0) { 553 error = EINVAL; 554 goto fail; 555 } 556 557 /* make sure that kernel-only bits are not set */ 558 rtm->rtm_priority &= RTP_MASK; 559 rtm->rtm_flags &= ~(RTF_DONE|RTF_CLONED); 560 rtm->rtm_fmask &= RTF_FMASK; 561 562 if (rtm->rtm_priority != 0) { 563 if (rtm->rtm_priority > RTP_MAX || 564 rtm->rtm_priority == RTP_LOCAL) { 565 error = EINVAL; 566 goto fail; 567 } 568 prio = rtm->rtm_priority; 569 } else if (rtm->rtm_type != RTM_ADD) 570 prio = RTP_ANY; 571 else if (rtm->rtm_flags & RTF_STATIC) 572 prio = 0; 573 else 574 prio = RTP_DEFAULT; 575 576 bzero(&info, sizeof(info)); 577 info.rti_addrs = rtm->rtm_addrs; 578 rt_xaddrs(rtm->rtm_hdrlen + (caddr_t)rtm, len + (caddr_t)rtm, &info); 579 info.rti_flags = rtm->rtm_flags; 580 if (info.rti_info[RTAX_DST] == NULL || 581 info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 582 (info.rti_info[RTAX_GATEWAY] != NULL && 583 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX) || 584 info.rti_info[RTAX_GENMASK] != NULL) { 585 error = EINVAL; 586 goto flush; 587 } 588 #ifdef MPLS 589 info.rti_mpls = rtm->rtm_mpls; 590 #endif 591 592 if (info.rti_info[RTAX_GATEWAY] != NULL && 593 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 594 (info.rti_flags & RTF_CLONING) == 0) { 595 info.rti_flags |= RTF_LLINFO; 596 } 597 598 switch (rtm->rtm_type) { 599 case RTM_ADD: 600 if (info.rti_info[RTAX_GATEWAY] == NULL) { 601 error = EINVAL; 602 goto flush; 603 } 604 if ((error = route_arp_conflict(&info, tableid))) 605 goto flush; 606 error = rtrequest(RTM_ADD, &info, prio, &saved_nrt, tableid); 607 if (error == 0) { 608 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 609 &saved_nrt->rt_rmx); 610 /* write back the priority the kernel used */ 611 rtm->rtm_priority = saved_nrt->rt_priority & RTP_MASK; 612 rtm->rtm_index = saved_nrt->rt_ifidx; 613 rtm->rtm_flags = saved_nrt->rt_flags; 614 rtfree(saved_nrt); 615 } 616 break; 617 case RTM_DELETE: 618 error = rtrequest(RTM_DELETE, &info, prio, &rt, tableid); 619 if (error == 0) 620 goto report; 621 break; 622 case RTM_GET: 623 case RTM_CHANGE: 624 case RTM_LOCK: 625 if (!rtable_exists(tableid)) { 626 error = EAFNOSUPPORT; 627 goto flush; 628 } 629 630 rt = rtable_lookup(tableid, info.rti_info[RTAX_DST], 631 info.rti_info[RTAX_NETMASK], info.rti_info[RTAX_GATEWAY], 632 prio); 633 #ifdef SMALL_KERNEL 634 /* 635 * If we got multipath routes, we require users to specify 636 * a matching gateway, except for RTM_GET. 637 */ 638 if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH) && 639 (info.rti_info[RTAX_GATEWAY] == NULL) && 640 (rtm->rtm_type != RTM_GET)) { 641 rtfree(rt); 642 rt = NULL; 643 } 644 #endif 645 /* 646 * If RTAX_GATEWAY is the argument we're trying to 647 * change, try to find a compatible route. 648 */ 649 if ((rt == NULL) && (info.rti_info[RTAX_GATEWAY] != NULL) && 650 (rtm->rtm_type == RTM_CHANGE)) { 651 rt = rtable_lookup(tableid, info.rti_info[RTAX_DST], 652 info.rti_info[RTAX_NETMASK], NULL, prio); 653 } 654 655 if (rt == NULL) { 656 error = ESRCH; 657 goto flush; 658 } 659 660 /* 661 * RTM_CHANGE/LOCK need a perfect match. 662 */ 663 plen = rtable_satoplen(info.rti_info[RTAX_DST]->sa_family, 664 info.rti_info[RTAX_NETMASK]); 665 if (rtm->rtm_type != RTM_GET && rt_plen(rt) != plen ) { 666 error = ESRCH; 667 goto flush; 668 } 669 670 switch (rtm->rtm_type) { 671 case RTM_GET: 672 report: 673 info.rti_info[RTAX_DST] = rt_key(rt); 674 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 675 info.rti_info[RTAX_NETMASK] = 676 rt_plen2mask(rt, &sa_mask); 677 info.rti_info[RTAX_LABEL] = 678 rtlabel_id2sa(rt->rt_labelid, &sa_rl); 679 #ifdef MPLS 680 if (rt->rt_flags & RTF_MPLS) { 681 bzero(&sa_mpls, sizeof(sa_mpls)); 682 sa_mpls.smpls_family = AF_MPLS; 683 sa_mpls.smpls_len = sizeof(sa_mpls); 684 sa_mpls.smpls_label = ((struct rt_mpls *) 685 rt->rt_llinfo)->mpls_label; 686 info.rti_info[RTAX_SRC] = 687 (struct sockaddr *)&sa_mpls; 688 info.rti_mpls = ((struct rt_mpls *) 689 rt->rt_llinfo)->mpls_operation; 690 rtm->rtm_mpls = info.rti_mpls; 691 } 692 #endif 693 info.rti_info[RTAX_IFP] = NULL; 694 info.rti_info[RTAX_IFA] = NULL; 695 ifp = if_get(rt->rt_ifidx); 696 if (ifp != NULL && rtm->rtm_addrs & (RTA_IFP|RTA_IFA)) { 697 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 698 info.rti_info[RTAX_IFA] = rt->rt_addr; 699 if (ifp->if_flags & IFF_POINTOPOINT) 700 info.rti_info[RTAX_BRD] = 701 rt->rt_ifa->ifa_dstaddr; 702 else 703 info.rti_info[RTAX_BRD] = NULL; 704 } 705 if_put(ifp); 706 len = rt_msg2(rtm->rtm_type, RTM_VERSION, &info, NULL, 707 NULL); 708 if (len > rtm->rtm_msglen) { 709 struct rt_msghdr *new_rtm; 710 new_rtm = malloc(len, M_RTABLE, M_NOWAIT); 711 if (new_rtm == NULL) { 712 error = ENOBUFS; 713 goto flush; 714 } 715 memcpy(new_rtm, rtm, rtm->rtm_msglen); 716 free(rtm, M_RTABLE, 0); 717 rtm = new_rtm; 718 } 719 rt_msg2(rtm->rtm_type, RTM_VERSION, &info, (caddr_t)rtm, 720 NULL); 721 rtm->rtm_flags = rt->rt_flags; 722 rtm->rtm_use = 0; 723 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 724 rtm->rtm_index = rt->rt_ifidx; 725 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 726 rtm->rtm_addrs = info.rti_addrs; 727 break; 728 729 case RTM_CHANGE: 730 newgate = 0; 731 if (info.rti_info[RTAX_GATEWAY] != NULL) 732 if (rt->rt_gateway == NULL || 733 bcmp(rt->rt_gateway, 734 info.rti_info[RTAX_GATEWAY], 735 info.rti_info[RTAX_GATEWAY]->sa_len)) { 736 newgate = 1; 737 } 738 /* 739 * Check reachable gateway before changing the route. 740 * New gateway could require new ifaddr, ifp; 741 * flags may also be different; ifp may be specified 742 * by ll sockaddr when protocol address is ambiguous. 743 */ 744 if (newgate || info.rti_info[RTAX_IFP] != NULL || 745 info.rti_info[RTAX_IFA] != NULL) { 746 if ((error = rt_getifa(&info, tableid)) != 0) 747 goto flush; 748 ifa = info.rti_ifa; 749 } 750 if (info.rti_info[RTAX_GATEWAY] != NULL && (error = 751 rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))) 752 goto flush; 753 if (ifa) { 754 if (rt->rt_ifa != ifa) { 755 ifp = if_get(rt->rt_ifidx); 756 KASSERT(ifp != NULL); 757 ifp->if_rtrequest(ifp, RTM_DELETE, rt); 758 ifafree(rt->rt_ifa); 759 if_put(ifp); 760 761 ifa->ifa_refcnt++; 762 rt->rt_ifa = ifa; 763 rt->rt_ifidx = ifa->ifa_ifp->if_index; 764 #ifndef SMALL_KERNEL 765 /* recheck link state after ifp change*/ 766 rt_if_linkstate_change(rt, ifa->ifa_ifp, 767 tableid); 768 #endif 769 } 770 } 771 #ifdef MPLS 772 if ((rtm->rtm_flags & RTF_MPLS) && 773 info.rti_info[RTAX_SRC] != NULL) { 774 struct rt_mpls *rt_mpls; 775 776 psa_mpls = (struct sockaddr_mpls *) 777 info.rti_info[RTAX_SRC]; 778 779 if (rt->rt_llinfo == NULL) { 780 rt->rt_llinfo = 781 malloc(sizeof(struct rt_mpls), 782 M_TEMP, M_NOWAIT|M_ZERO); 783 } 784 if (rt->rt_llinfo == NULL) { 785 error = ENOMEM; 786 goto flush; 787 } 788 789 rt_mpls = (struct rt_mpls *)rt->rt_llinfo; 790 791 if (psa_mpls != NULL) { 792 rt_mpls->mpls_label = 793 psa_mpls->smpls_label; 794 } 795 796 rt_mpls->mpls_operation = info.rti_mpls; 797 798 /* XXX: set experimental bits */ 799 800 rt->rt_flags |= RTF_MPLS; 801 } else if (newgate || ((rtm->rtm_fmask & RTF_MPLS) && 802 !(rtm->rtm_flags & RTF_MPLS))) { 803 /* if gateway changed remove MPLS information */ 804 if (rt->rt_llinfo != NULL && 805 rt->rt_flags & RTF_MPLS) { 806 free(rt->rt_llinfo, M_TEMP, 0); 807 rt->rt_llinfo = NULL; 808 rt->rt_flags &= ~RTF_MPLS; 809 } 810 } 811 #endif 812 /* Hack to allow some flags to be toggled */ 813 if (rtm->rtm_fmask) 814 rt->rt_flags = 815 (rt->rt_flags & ~rtm->rtm_fmask) | 816 (rtm->rtm_flags & rtm->rtm_fmask); 817 818 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 819 &rt->rt_rmx); 820 rtm->rtm_index = rt->rt_ifidx; 821 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 822 rtm->rtm_flags = rt->rt_flags; 823 824 ifp = if_get(rt->rt_ifidx); 825 KASSERT(ifp != NULL); 826 ifp->if_rtrequest(ifp, RTM_ADD, rt); 827 if_put(ifp); 828 829 if (info.rti_info[RTAX_LABEL] != NULL) { 830 char *rtlabel = ((struct sockaddr_rtlabel *) 831 info.rti_info[RTAX_LABEL])->sr_label; 832 rtlabel_unref(rt->rt_labelid); 833 rt->rt_labelid = rtlabel_name2id(rtlabel); 834 } 835 if_group_routechange(info.rti_info[RTAX_DST], 836 info.rti_info[RTAX_NETMASK]); 837 /* FALLTHROUGH */ 838 case RTM_LOCK: 839 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 840 rt->rt_rmx.rmx_locks |= 841 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 842 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 843 break; 844 } 845 break; 846 } 847 848 flush: 849 if (rtm) { 850 if (error) 851 rtm->rtm_errno = error; 852 else { 853 rtm->rtm_flags |= RTF_DONE; 854 } 855 } 856 if (info.rti_info[RTAX_DST]) 857 route_proto.sp_protocol = info.rti_info[RTAX_DST]->sa_family; 858 if (rt) 859 rtfree(rt); 860 861 /* 862 * Check to see if we don't want our own messages. 863 */ 864 if (!(so->so_options & SO_USELOOPBACK)) { 865 if (route_cb.any_count <= 1) { 866 fail: 867 free(rtm, M_RTABLE, 0); 868 m_freem(m); 869 return (error); 870 } 871 /* There is another listener, so construct message */ 872 rp = sotorawcb(so); 873 } 874 if (rp) 875 rp->rcb_proto.sp_family = 0; /* Avoid us */ 876 if (rtm) { 877 if (m_copyback(m, 0, rtm->rtm_msglen, rtm, M_NOWAIT)) { 878 m_freem(m); 879 m = NULL; 880 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 881 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 882 free(rtm, M_RTABLE, 0); 883 } 884 if (m) 885 route_input(m, &route_proto, &route_src, &route_dst); 886 if (rp) 887 rp->rcb_proto.sp_family = PF_ROUTE; 888 889 return (error); 890 } 891 892 /* 893 * Check if the user request to insert an ARP entry does not conflict 894 * with existing ones. 895 * 896 * Only two entries are allowed for a given IP address: a private one 897 * (priv) and a public one (pub). 898 */ 899 int 900 route_arp_conflict(struct rt_addrinfo *info, unsigned int tableid) 901 { 902 #if defined(ART) && !defined(SMALL_KERNEL) 903 struct rtentry *rt; 904 int proxy = (info->rti_flags & RTF_ANNOUNCE); 905 906 if ((info->rti_flags & RTF_LLINFO) == 0 || 907 (info->rti_info[RTAX_DST]->sa_family != AF_INET)) 908 return (0); 909 910 rt = rtalloc(info->rti_info[RTAX_DST], 0, tableid); 911 if (rt == NULL || !ISSET(rt->rt_flags, RTF_LLINFO)) { 912 rtfree(rt); 913 return (0); 914 } 915 916 /* 917 * Same destination and both "priv" or "pub" conflict. 918 * If a second entry exists, it always conflict. 919 */ 920 if ((ISSET(rt->rt_flags, RTF_ANNOUNCE) == proxy) || 921 (rtable_mpath_next(rt) != NULL)) { 922 rtfree(rt); 923 return (EEXIST); 924 } 925 926 /* No conflict but an entry exist so we need to force mpath. */ 927 info->rti_flags |= RTF_MPATH; 928 rtfree(rt); 929 #endif /* ART && !SMALL_KERNEL */ 930 return (0); 931 } 932 933 void 934 rt_setmetrics(u_long which, const struct rt_metrics *in, 935 struct rt_kmetrics *out) 936 { 937 int64_t expire; 938 939 if (which & RTV_MTU) 940 out->rmx_mtu = in->rmx_mtu; 941 if (which & RTV_EXPIRE) { 942 expire = in->rmx_expire; 943 if (expire != 0) { 944 expire -= time_second; 945 expire += time_uptime; 946 } 947 948 out->rmx_expire = expire; 949 } 950 /* RTV_PRIORITY handled before */ 951 } 952 953 void 954 rt_getmetrics(const struct rt_kmetrics *in, struct rt_metrics *out) 955 { 956 int64_t expire; 957 958 expire = in->rmx_expire; 959 if (expire != 0) { 960 expire -= time_uptime; 961 expire += time_second; 962 } 963 964 bzero(out, sizeof(*out)); 965 out->rmx_locks = in->rmx_locks; 966 out->rmx_mtu = in->rmx_mtu; 967 out->rmx_expire = expire; 968 out->rmx_pksent = in->rmx_pksent; 969 } 970 971 #define ROUNDUP(a) \ 972 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 973 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 974 975 void 976 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 977 { 978 struct sockaddr *sa; 979 int i; 980 981 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 982 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 983 if ((rtinfo->rti_addrs & (1 << i)) == 0) 984 continue; 985 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 986 ADVANCE(cp, sa); 987 } 988 } 989 990 struct mbuf * 991 rt_msg1(int type, struct rt_addrinfo *rtinfo) 992 { 993 struct rt_msghdr *rtm; 994 struct mbuf *m; 995 int i; 996 struct sockaddr *sa; 997 int len, dlen, hlen; 998 999 switch (type) { 1000 case RTM_DELADDR: 1001 case RTM_NEWADDR: 1002 len = sizeof(struct ifa_msghdr); 1003 break; 1004 case RTM_IFINFO: 1005 len = sizeof(struct if_msghdr); 1006 break; 1007 case RTM_IFANNOUNCE: 1008 len = sizeof(struct if_announcemsghdr); 1009 break; 1010 default: 1011 len = sizeof(struct rt_msghdr); 1012 break; 1013 } 1014 if (len > MCLBYTES) 1015 panic("rt_msg1"); 1016 m = m_gethdr(M_DONTWAIT, MT_DATA); 1017 if (m && len > MHLEN) { 1018 MCLGET(m, M_DONTWAIT); 1019 if ((m->m_flags & M_EXT) == 0) { 1020 m_free(m); 1021 m = NULL; 1022 } 1023 } 1024 if (m == NULL) 1025 return (m); 1026 m->m_pkthdr.len = m->m_len = hlen = len; 1027 m->m_pkthdr.ph_ifidx = 0; 1028 rtm = mtod(m, struct rt_msghdr *); 1029 bzero(rtm, len); 1030 for (i = 0; i < RTAX_MAX; i++) { 1031 if (rtinfo == NULL || (sa = rtinfo->rti_info[i]) == NULL) 1032 continue; 1033 rtinfo->rti_addrs |= (1 << i); 1034 dlen = ROUNDUP(sa->sa_len); 1035 if (m_copyback(m, len, dlen, sa, M_NOWAIT)) { 1036 m_freem(m); 1037 return (NULL); 1038 } 1039 len += dlen; 1040 } 1041 rtm->rtm_msglen = len; 1042 rtm->rtm_hdrlen = hlen; 1043 rtm->rtm_version = RTM_VERSION; 1044 rtm->rtm_type = type; 1045 return (m); 1046 } 1047 1048 int 1049 rt_msg2(int type, int vers, struct rt_addrinfo *rtinfo, caddr_t cp, 1050 struct walkarg *w) 1051 { 1052 int i; 1053 int len, dlen, hlen, second_time = 0; 1054 caddr_t cp0; 1055 1056 rtinfo->rti_addrs = 0; 1057 again: 1058 switch (type) { 1059 case RTM_DELADDR: 1060 case RTM_NEWADDR: 1061 len = sizeof(struct ifa_msghdr); 1062 break; 1063 case RTM_IFINFO: 1064 len = sizeof(struct if_msghdr); 1065 break; 1066 default: 1067 len = sizeof(struct rt_msghdr); 1068 break; 1069 } 1070 hlen = len; 1071 if ((cp0 = cp) != NULL) 1072 cp += len; 1073 for (i = 0; i < RTAX_MAX; i++) { 1074 struct sockaddr *sa; 1075 1076 if ((sa = rtinfo->rti_info[i]) == NULL) 1077 continue; 1078 rtinfo->rti_addrs |= (1 << i); 1079 dlen = ROUNDUP(sa->sa_len); 1080 if (cp) { 1081 bcopy(sa, cp, (size_t)dlen); 1082 cp += dlen; 1083 } 1084 len += dlen; 1085 } 1086 /* align message length to the next natural boundary */ 1087 len = ALIGN(len); 1088 if (cp == 0 && w != NULL && !second_time) { 1089 struct walkarg *rw = w; 1090 1091 rw->w_needed += len; 1092 if (rw->w_needed <= 0 && rw->w_where) { 1093 if (rw->w_tmemsize < len) { 1094 free(rw->w_tmem, M_RTABLE, 0); 1095 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT); 1096 if (rw->w_tmem) 1097 rw->w_tmemsize = len; 1098 } 1099 if (rw->w_tmem) { 1100 cp = rw->w_tmem; 1101 second_time = 1; 1102 goto again; 1103 } else 1104 rw->w_where = 0; 1105 } 1106 } 1107 if (cp && w) /* clear the message header */ 1108 bzero(cp0, hlen); 1109 1110 if (cp) { 1111 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1112 1113 rtm->rtm_version = RTM_VERSION; 1114 rtm->rtm_type = type; 1115 rtm->rtm_msglen = len; 1116 rtm->rtm_hdrlen = hlen; 1117 } 1118 return (len); 1119 } 1120 1121 /* 1122 * This routine is called to generate a message from the routing 1123 * socket indicating that a redirect has occurred, a routing lookup 1124 * has failed, or that a protocol has detected timeouts to a particular 1125 * destination. 1126 */ 1127 void 1128 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, uint8_t prio, 1129 u_int ifidx, int error, u_int tableid) 1130 { 1131 struct rt_msghdr *rtm; 1132 struct mbuf *m; 1133 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1134 1135 if (route_cb.any_count == 0) 1136 return; 1137 m = rt_msg1(type, rtinfo); 1138 if (m == NULL) 1139 return; 1140 rtm = mtod(m, struct rt_msghdr *); 1141 rtm->rtm_flags = RTF_DONE | flags; 1142 rtm->rtm_priority = prio; 1143 rtm->rtm_errno = error; 1144 rtm->rtm_tableid = tableid; 1145 rtm->rtm_addrs = rtinfo->rti_addrs; 1146 rtm->rtm_index = ifidx; 1147 if (sa == NULL) 1148 route_proto.sp_protocol = 0; 1149 else 1150 route_proto.sp_protocol = sa->sa_family; 1151 route_input(m, &route_proto, &route_src, &route_dst); 1152 } 1153 1154 /* 1155 * This routine is called to generate a message from the routing 1156 * socket indicating that the status of a network interface has changed. 1157 */ 1158 void 1159 rt_ifmsg(struct ifnet *ifp) 1160 { 1161 struct if_msghdr *ifm; 1162 struct mbuf *m; 1163 1164 if (route_cb.any_count == 0) 1165 return; 1166 m = rt_msg1(RTM_IFINFO, NULL); 1167 if (m == NULL) 1168 return; 1169 ifm = mtod(m, struct if_msghdr *); 1170 ifm->ifm_index = ifp->if_index; 1171 ifm->ifm_tableid = ifp->if_rdomain; 1172 ifm->ifm_flags = ifp->if_flags; 1173 ifm->ifm_xflags = ifp->if_xflags; 1174 ifm->ifm_data = ifp->if_data; 1175 ifm->ifm_addrs = 0; 1176 route_proto.sp_protocol = 0; 1177 route_input(m, &route_proto, &route_src, &route_dst); 1178 } 1179 1180 /* 1181 * This is called to generate messages from the routing socket 1182 * indicating a network interface has had addresses associated with it. 1183 * if we ever reverse the logic and replace messages TO the routing 1184 * socket indicate a request to configure interfaces, then it will 1185 * be unnecessary as the routing socket will automatically generate 1186 * copies of it. 1187 */ 1188 void 1189 rt_sendaddrmsg(struct rtentry *rt, int cmd, struct ifaddr *ifa) 1190 { 1191 struct ifnet *ifp = ifa->ifa_ifp; 1192 struct mbuf *m = NULL; 1193 struct rt_addrinfo info; 1194 struct ifa_msghdr *ifam; 1195 1196 if (route_cb.any_count == 0) 1197 return; 1198 1199 memset(&info, 0, sizeof(info)); 1200 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1201 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1202 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1203 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1204 if ((m = rt_msg1(cmd, &info)) == NULL) 1205 return; 1206 ifam = mtod(m, struct ifa_msghdr *); 1207 ifam->ifam_index = ifp->if_index; 1208 ifam->ifam_metric = ifa->ifa_metric; 1209 ifam->ifam_flags = ifa->ifa_flags; 1210 ifam->ifam_addrs = info.rti_addrs; 1211 ifam->ifam_tableid = ifp->if_rdomain; 1212 1213 if (ifa->ifa_addr == NULL) 1214 route_proto.sp_protocol = 0; 1215 else 1216 route_proto.sp_protocol = ifa->ifa_addr->sa_family; 1217 route_input(m, &route_proto, &route_src, &route_dst); 1218 } 1219 1220 /* 1221 * This is called to generate routing socket messages indicating 1222 * network interface arrival and departure. 1223 */ 1224 void 1225 rt_ifannouncemsg(struct ifnet *ifp, int what) 1226 { 1227 struct if_announcemsghdr *ifan; 1228 struct mbuf *m; 1229 1230 if (route_cb.any_count == 0) 1231 return; 1232 m = rt_msg1(RTM_IFANNOUNCE, NULL); 1233 if (m == NULL) 1234 return; 1235 ifan = mtod(m, struct if_announcemsghdr *); 1236 ifan->ifan_index = ifp->if_index; 1237 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 1238 ifan->ifan_what = what; 1239 route_proto.sp_protocol = 0; 1240 route_input(m, &route_proto, &route_src, &route_dst); 1241 } 1242 1243 /* 1244 * This is used in dumping the kernel table via sysctl(). 1245 */ 1246 int 1247 sysctl_dumpentry(struct rtentry *rt, void *v, unsigned int id) 1248 { 1249 struct walkarg *w = v; 1250 int error = 0, size; 1251 struct rt_addrinfo info; 1252 struct ifnet *ifp; 1253 #ifdef MPLS 1254 struct sockaddr_mpls sa_mpls; 1255 #endif 1256 struct sockaddr_rtlabel sa_rl; 1257 struct sockaddr_in6 sa_mask; 1258 1259 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1260 return 0; 1261 if (w->w_op == NET_RT_DUMP && w->w_arg) { 1262 u_int8_t prio = w->w_arg & RTP_MASK; 1263 if (w->w_arg < 0) { 1264 prio = (-w->w_arg) & RTP_MASK; 1265 /* Show all routes that are not this priority */ 1266 if (prio == (rt->rt_priority & RTP_MASK)) 1267 return 0; 1268 } else { 1269 if (prio != (rt->rt_priority & RTP_MASK) && 1270 prio != RTP_ANY) 1271 return 0; 1272 } 1273 } 1274 bzero(&info, sizeof(info)); 1275 info.rti_info[RTAX_DST] = rt_key(rt); 1276 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1277 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 1278 ifp = if_get(rt->rt_ifidx); 1279 if (ifp != NULL) { 1280 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1281 info.rti_info[RTAX_IFA] = rt->rt_addr; 1282 if (ifp->if_flags & IFF_POINTOPOINT) 1283 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1284 } 1285 if_put(ifp); 1286 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 1287 #ifdef MPLS 1288 if (rt->rt_flags & RTF_MPLS) { 1289 bzero(&sa_mpls, sizeof(sa_mpls)); 1290 sa_mpls.smpls_family = AF_MPLS; 1291 sa_mpls.smpls_len = sizeof(sa_mpls); 1292 sa_mpls.smpls_label = ((struct rt_mpls *) 1293 rt->rt_llinfo)->mpls_label; 1294 info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls; 1295 info.rti_mpls = ((struct rt_mpls *) 1296 rt->rt_llinfo)->mpls_operation; 1297 } 1298 #endif 1299 1300 size = rt_msg2(RTM_GET, RTM_VERSION, &info, NULL, w); 1301 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1302 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1303 1304 rtm->rtm_pid = curproc->p_p->ps_pid; 1305 rtm->rtm_flags = rt->rt_flags; 1306 rtm->rtm_priority = rt->rt_priority & RTP_MASK; 1307 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1308 /* Do not account the routing table's reference. */ 1309 rtm->rtm_rmx.rmx_refcnt = rt->rt_refcnt - 1; 1310 rtm->rtm_index = rt->rt_ifidx; 1311 rtm->rtm_addrs = info.rti_addrs; 1312 rtm->rtm_tableid = id; 1313 #ifdef MPLS 1314 rtm->rtm_mpls = info.rti_mpls; 1315 #endif 1316 if ((error = copyout(rtm, w->w_where, size)) != 0) 1317 w->w_where = NULL; 1318 else 1319 w->w_where += size; 1320 } 1321 return (error); 1322 } 1323 1324 int 1325 sysctl_iflist(int af, struct walkarg *w) 1326 { 1327 struct ifnet *ifp; 1328 struct ifaddr *ifa; 1329 struct rt_addrinfo info; 1330 int len, error = 0; 1331 1332 bzero(&info, sizeof(info)); 1333 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1334 if (w->w_arg && w->w_arg != ifp->if_index) 1335 continue; 1336 /* Copy the link-layer address first */ 1337 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 1338 len = rt_msg2(RTM_IFINFO, RTM_VERSION, &info, 0, w); 1339 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1340 struct if_msghdr *ifm; 1341 1342 ifm = (struct if_msghdr *)w->w_tmem; 1343 ifm->ifm_index = ifp->if_index; 1344 ifm->ifm_tableid = ifp->if_rdomain; 1345 ifm->ifm_flags = ifp->if_flags; 1346 ifm->ifm_data = ifp->if_data; 1347 ifm->ifm_addrs = info.rti_addrs; 1348 error = copyout(ifm, w->w_where, len); 1349 if (error) 1350 return (error); 1351 w->w_where += len; 1352 } 1353 info.rti_info[RTAX_IFP] = NULL; 1354 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1355 KASSERT(ifa->ifa_addr->sa_family != AF_LINK); 1356 if (af && af != ifa->ifa_addr->sa_family) 1357 continue; 1358 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1359 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1360 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1361 len = rt_msg2(RTM_NEWADDR, RTM_VERSION, &info, 0, w); 1362 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1363 struct ifa_msghdr *ifam; 1364 1365 ifam = (struct ifa_msghdr *)w->w_tmem; 1366 ifam->ifam_index = ifa->ifa_ifp->if_index; 1367 ifam->ifam_flags = ifa->ifa_flags; 1368 ifam->ifam_metric = ifa->ifa_metric; 1369 ifam->ifam_addrs = info.rti_addrs; 1370 error = copyout(w->w_tmem, w->w_where, len); 1371 if (error) 1372 return (error); 1373 w->w_where += len; 1374 } 1375 } 1376 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1377 info.rti_info[RTAX_BRD] = NULL; 1378 } 1379 return (0); 1380 } 1381 1382 int 1383 sysctl_ifnames(struct walkarg *w) 1384 { 1385 struct if_nameindex_msg ifn; 1386 struct ifnet *ifp; 1387 int error = 0; 1388 1389 /* XXX ignore tableid for now */ 1390 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1391 if (w->w_arg && w->w_arg != ifp->if_index) 1392 continue; 1393 w->w_needed += sizeof(ifn); 1394 if (w->w_where && w->w_needed <= 0) { 1395 1396 memset(&ifn, 0, sizeof(ifn)); 1397 ifn.if_index = ifp->if_index; 1398 strlcpy(ifn.if_name, ifp->if_xname, 1399 sizeof(ifn.if_name)); 1400 error = copyout(&ifn, w->w_where, sizeof(ifn)); 1401 if (error) 1402 return (error); 1403 w->w_where += sizeof(ifn); 1404 } 1405 } 1406 1407 return (0); 1408 } 1409 1410 int 1411 sysctl_rtable(int *name, u_int namelen, void *where, size_t *given, void *new, 1412 size_t newlen) 1413 { 1414 int i, s, error = EINVAL; 1415 u_char af; 1416 struct walkarg w; 1417 struct rt_tableinfo tableinfo; 1418 u_int tableid = 0; 1419 1420 if (new) 1421 return (EPERM); 1422 if (namelen < 3 || namelen > 4) 1423 return (EINVAL); 1424 af = name[0]; 1425 bzero(&w, sizeof(w)); 1426 w.w_where = where; 1427 w.w_given = *given; 1428 w.w_needed = 0 - w.w_given; 1429 w.w_op = name[1]; 1430 w.w_arg = name[2]; 1431 1432 if (namelen == 4) { 1433 tableid = name[3]; 1434 if (!rtable_exists(tableid)) 1435 return (ENOENT); 1436 } else 1437 tableid = curproc->p_p->ps_rtableid; 1438 1439 s = splsoftnet(); 1440 switch (w.w_op) { 1441 case NET_RT_DUMP: 1442 case NET_RT_FLAGS: 1443 for (i = 1; i <= AF_MAX; i++) { 1444 if (af != 0 && af != i) 1445 continue; 1446 1447 error = rtable_walk(tableid, i, sysctl_dumpentry, &w); 1448 if (error == EAFNOSUPPORT) 1449 error = 0; 1450 if (error) 1451 break; 1452 } 1453 break; 1454 1455 case NET_RT_IFLIST: 1456 error = sysctl_iflist(af, &w); 1457 break; 1458 1459 case NET_RT_STATS: 1460 error = sysctl_rdstruct(where, given, new, 1461 &rtstat, sizeof(rtstat)); 1462 splx(s); 1463 return (error); 1464 case NET_RT_TABLE: 1465 tableid = w.w_arg; 1466 if (!rtable_exists(tableid)) { 1467 splx(s); 1468 return (ENOENT); 1469 } 1470 tableinfo.rti_tableid = tableid; 1471 tableinfo.rti_domainid = rtable_l2(tableid); 1472 error = sysctl_rdstruct(where, given, new, 1473 &tableinfo, sizeof(tableinfo)); 1474 splx(s); 1475 return (error); 1476 case NET_RT_IFNAMES: 1477 error = sysctl_ifnames(&w); 1478 break; 1479 } 1480 splx(s); 1481 free(w.w_tmem, M_RTABLE, 0); 1482 w.w_needed += w.w_given; 1483 if (where) { 1484 *given = w.w_where - (caddr_t)where; 1485 if (*given < w.w_needed) 1486 return (ENOMEM); 1487 } else 1488 *given = (11 * w.w_needed) / 10; 1489 1490 return (error); 1491 } 1492 1493 /* 1494 * Definitions of protocols supported in the ROUTE domain. 1495 */ 1496 1497 extern struct domain routedomain; /* or at least forward */ 1498 1499 struct protosw routesw[] = { 1500 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR|PR_WANTRCVD, 1501 route_input, route_output, raw_ctlinput, route_ctloutput, 1502 route_usrreq, 1503 raw_init, 0, 0, 0, 1504 sysctl_rtable, 1505 } 1506 }; 1507 1508 struct domain routedomain = 1509 { PF_ROUTE, "route", route_init, 0, 0, 1510 routesw, &routesw[nitems(routesw)] }; 1511