1 /* 2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Jeffrey M. Hsu. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of The DragonFly Project nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific, prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * 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.7 (Berkeley) 10/12/95 62 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $ 63 */ 64 65 #include "opt_sctp.h" 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/sysctl.h> 71 #include <sys/proc.h> 72 #include <sys/priv.h> 73 #include <sys/malloc.h> 74 #include <sys/mbuf.h> 75 #include <sys/protosw.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/domain.h> 79 80 #include <sys/thread2.h> 81 #include <sys/socketvar2.h> 82 83 #include <net/if.h> 84 #include <net/route.h> 85 #include <net/raw_cb.h> 86 #include <net/netmsg2.h> 87 #include <net/netisr2.h> 88 89 #ifdef SCTP 90 extern void sctp_add_ip_address(struct ifaddr *ifa); 91 extern void sctp_delete_ip_address(struct ifaddr *ifa); 92 #endif /* SCTP */ 93 94 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 95 96 static struct route_cb { 97 int ip_count; 98 int ip6_count; 99 int ns_count; 100 int any_count; 101 } route_cb; 102 103 static const struct sockaddr route_src = { 2, PF_ROUTE, }; 104 105 struct walkarg { 106 int w_tmemsize; 107 int w_op, w_arg; 108 void *w_tmem; 109 struct sysctl_req *w_req; 110 }; 111 112 static struct mbuf * 113 rt_msg_mbuf (int, struct rt_addrinfo *); 114 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len); 115 static int rt_msgsize (int type, struct rt_addrinfo *rtinfo); 116 static int rt_xaddrs (char *, char *, struct rt_addrinfo *); 117 static int sysctl_dumpentry (struct radix_node *rn, void *vw); 118 static int sysctl_iflist (int af, struct walkarg *w); 119 static int route_output(struct mbuf *, struct socket *, ...); 120 static void rt_setmetrics (u_long, struct rt_metrics *, 121 struct rt_metrics *); 122 123 /* 124 * It really doesn't make any sense at all for this code to share much 125 * with raw_usrreq.c, since its functionality is so restricted. XXX 126 */ 127 static void 128 rts_abort(netmsg_t msg) 129 { 130 crit_enter(); 131 raw_usrreqs.pru_abort(msg); 132 /* msg invalid now */ 133 crit_exit(); 134 } 135 136 /* pru_accept is EOPNOTSUPP */ 137 138 static void 139 rts_attach(netmsg_t msg) 140 { 141 struct socket *so = msg->base.nm_so; 142 struct pru_attach_info *ai = msg->attach.nm_ai; 143 struct rawcb *rp; 144 int proto = msg->attach.nm_proto; 145 int error; 146 147 crit_enter(); 148 if (sotorawcb(so) != NULL) { 149 error = EISCONN; 150 goto done; 151 } 152 153 rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 154 155 /* 156 * The critical section is necessary to block protocols from sending 157 * error notifications (like RTM_REDIRECT or RTM_LOSING) while 158 * this PCB is extant but incompletely initialized. 159 * Probably we should try to do more of this work beforehand and 160 * eliminate the critical section. 161 */ 162 so->so_pcb = rp; 163 soreference(so); /* so_pcb assignment */ 164 error = raw_attach(so, proto, ai->sb_rlimit); 165 rp = sotorawcb(so); 166 if (error) { 167 kfree(rp, M_PCB); 168 goto done; 169 } 170 switch(rp->rcb_proto.sp_protocol) { 171 case AF_INET: 172 route_cb.ip_count++; 173 break; 174 case AF_INET6: 175 route_cb.ip6_count++; 176 break; 177 } 178 rp->rcb_faddr = &route_src; 179 route_cb.any_count++; 180 soisconnected(so); 181 so->so_options |= SO_USELOOPBACK; 182 error = 0; 183 done: 184 crit_exit(); 185 lwkt_replymsg(&msg->lmsg, error); 186 } 187 188 static void 189 rts_bind(netmsg_t msg) 190 { 191 crit_enter(); 192 raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */ 193 /* msg invalid now */ 194 crit_exit(); 195 } 196 197 static void 198 rts_connect(netmsg_t msg) 199 { 200 crit_enter(); 201 raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */ 202 /* msg invalid now */ 203 crit_exit(); 204 } 205 206 /* pru_connect2 is EOPNOTSUPP */ 207 /* pru_control is EOPNOTSUPP */ 208 209 static void 210 rts_detach(netmsg_t msg) 211 { 212 struct socket *so = msg->base.nm_so; 213 struct rawcb *rp = sotorawcb(so); 214 215 crit_enter(); 216 if (rp != NULL) { 217 switch(rp->rcb_proto.sp_protocol) { 218 case AF_INET: 219 route_cb.ip_count--; 220 break; 221 case AF_INET6: 222 route_cb.ip6_count--; 223 break; 224 } 225 route_cb.any_count--; 226 } 227 raw_usrreqs.pru_detach(msg); 228 /* msg invalid now */ 229 crit_exit(); 230 } 231 232 static void 233 rts_disconnect(netmsg_t msg) 234 { 235 crit_enter(); 236 raw_usrreqs.pru_disconnect(msg); 237 /* msg invalid now */ 238 crit_exit(); 239 } 240 241 /* pru_listen is EOPNOTSUPP */ 242 243 static void 244 rts_peeraddr(netmsg_t msg) 245 { 246 crit_enter(); 247 raw_usrreqs.pru_peeraddr(msg); 248 /* msg invalid now */ 249 crit_exit(); 250 } 251 252 /* pru_rcvd is EOPNOTSUPP */ 253 /* pru_rcvoob is EOPNOTSUPP */ 254 255 static void 256 rts_send(netmsg_t msg) 257 { 258 crit_enter(); 259 raw_usrreqs.pru_send(msg); 260 /* msg invalid now */ 261 crit_exit(); 262 } 263 264 /* pru_sense is null */ 265 266 static void 267 rts_shutdown(netmsg_t msg) 268 { 269 crit_enter(); 270 raw_usrreqs.pru_shutdown(msg); 271 /* msg invalid now */ 272 crit_exit(); 273 } 274 275 static void 276 rts_sockaddr(netmsg_t msg) 277 { 278 crit_enter(); 279 raw_usrreqs.pru_sockaddr(msg); 280 /* msg invalid now */ 281 crit_exit(); 282 } 283 284 static struct pr_usrreqs route_usrreqs = { 285 .pru_abort = rts_abort, 286 .pru_accept = pr_generic_notsupp, 287 .pru_attach = rts_attach, 288 .pru_bind = rts_bind, 289 .pru_connect = rts_connect, 290 .pru_connect2 = pr_generic_notsupp, 291 .pru_control = pr_generic_notsupp, 292 .pru_detach = rts_detach, 293 .pru_disconnect = rts_disconnect, 294 .pru_listen = pr_generic_notsupp, 295 .pru_peeraddr = rts_peeraddr, 296 .pru_rcvd = pr_generic_notsupp, 297 .pru_rcvoob = pr_generic_notsupp, 298 .pru_send = rts_send, 299 .pru_sense = pru_sense_null, 300 .pru_shutdown = rts_shutdown, 301 .pru_sockaddr = rts_sockaddr, 302 .pru_sosend = sosend, 303 .pru_soreceive = soreceive 304 }; 305 306 static __inline sa_family_t 307 familyof(struct sockaddr *sa) 308 { 309 return (sa != NULL ? sa->sa_family : 0); 310 } 311 312 /* 313 * Routing socket input function. The packet must be serialized onto cpu 0. 314 * We use the cpu0_soport() netisr processing loop to handle it. 315 * 316 * This looks messy but it means that anyone, including interrupt code, 317 * can send a message to the routing socket. 318 */ 319 static void 320 rts_input_handler(netmsg_t msg) 321 { 322 static const struct sockaddr route_dst = { 2, PF_ROUTE, }; 323 struct sockproto route_proto; 324 struct netmsg_packet *pmsg = &msg->packet; 325 struct mbuf *m; 326 sa_family_t family; 327 struct rawcb *skip; 328 329 family = pmsg->base.lmsg.u.ms_result; 330 route_proto.sp_family = PF_ROUTE; 331 route_proto.sp_protocol = family; 332 333 m = pmsg->nm_packet; 334 M_ASSERTPKTHDR(m); 335 336 skip = m->m_pkthdr.header; 337 m->m_pkthdr.header = NULL; 338 339 raw_input(m, &route_proto, &route_src, &route_dst, skip); 340 } 341 342 static void 343 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip) 344 { 345 struct netmsg_packet *pmsg; 346 lwkt_port_t port; 347 348 M_ASSERTPKTHDR(m); 349 350 port = netisr_cpuport(0); /* XXX same as for routing socket */ 351 pmsg = &m->m_hdr.mh_netmsg; 352 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 353 0, rts_input_handler); 354 pmsg->nm_packet = m; 355 pmsg->base.lmsg.u.ms_result = family; 356 m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */ 357 lwkt_sendmsg(port, &pmsg->base.lmsg); 358 } 359 360 static __inline void 361 rts_input(struct mbuf *m, sa_family_t family) 362 { 363 rts_input_skip(m, family, NULL); 364 } 365 366 static void * 367 reallocbuf_nofree(void *ptr, size_t len, size_t olen) 368 { 369 void *newptr; 370 371 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 372 if (newptr == NULL) 373 return NULL; 374 bcopy(ptr, newptr, olen); 375 return (newptr); 376 } 377 378 /* 379 * Internal helper routine for route_output(). 380 */ 381 static int 382 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt, 383 struct rt_addrinfo *rtinfo) 384 { 385 int msglen; 386 struct rt_msghdr *rtm = *prtm; 387 388 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */ 389 rtinfo->rti_dst = rt_key(rt); 390 rtinfo->rti_gateway = rt->rt_gateway; 391 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */ 392 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */ 393 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 394 if (rt->rt_ifp != NULL) { 395 rtinfo->rti_ifpaddr = 396 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid]) 397 ->ifa->ifa_addr; 398 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr; 399 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 400 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; 401 rtm->rtm_index = rt->rt_ifp->if_index; 402 } else { 403 rtinfo->rti_ifpaddr = NULL; 404 rtinfo->rti_ifaaddr = NULL; 405 } 406 } else if (rt->rt_ifp != NULL) { 407 rtm->rtm_index = rt->rt_ifp->if_index; 408 } 409 410 msglen = rt_msgsize(rtm->rtm_type, rtinfo); 411 if (rtm->rtm_msglen < msglen) { 412 /* NOTE: Caller will free the old rtm accordingly */ 413 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen); 414 if (rtm == NULL) 415 return (ENOBUFS); 416 *prtm = rtm; 417 } 418 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen); 419 420 rtm->rtm_flags = rt->rt_flags; 421 rtm->rtm_rmx = rt->rt_rmx; 422 rtm->rtm_addrs = rtinfo->rti_addrs; 423 424 return (0); 425 } 426 427 struct rtm_arg { 428 struct rt_msghdr *bak_rtm; 429 struct rt_msghdr *new_rtm; 430 }; 431 432 static int 433 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt, 434 struct rt_addrinfo *rtinfo) 435 { 436 struct rt_msghdr *rtm = arg->new_rtm; 437 int error; 438 439 error = _fillrtmsg(&rtm, rt, rtinfo); 440 if (!error) { 441 if (arg->new_rtm != rtm) { 442 /* 443 * _fillrtmsg() just allocated a new rtm; 444 * if the previously allocated rtm is not 445 * the backing rtm, it should be freed. 446 */ 447 if (arg->new_rtm != arg->bak_rtm) 448 kfree(arg->new_rtm, M_RTABLE); 449 arg->new_rtm = rtm; 450 } 451 } 452 return error; 453 } 454 455 static void route_output_add_callback(int, int, struct rt_addrinfo *, 456 struct rtentry *, void *); 457 static void route_output_delete_callback(int, int, struct rt_addrinfo *, 458 struct rtentry *, void *); 459 static int route_output_get_callback(int, struct rt_addrinfo *, 460 struct rtentry *, void *, int); 461 static int route_output_change_callback(int, struct rt_addrinfo *, 462 struct rtentry *, void *, int); 463 static int route_output_lock_callback(int, struct rt_addrinfo *, 464 struct rtentry *, void *, int); 465 466 /*ARGSUSED*/ 467 static int 468 route_output(struct mbuf *m, struct socket *so, ...) 469 { 470 struct rtm_arg arg; 471 struct rt_msghdr *rtm = NULL; 472 struct rawcb *rp = NULL; 473 struct pr_output_info *oi; 474 struct rt_addrinfo rtinfo; 475 sa_family_t family; 476 int len, error = 0; 477 __va_list ap; 478 479 M_ASSERTPKTHDR(m); 480 481 __va_start(ap, so); 482 oi = __va_arg(ap, struct pr_output_info *); 483 __va_end(ap); 484 485 family = familyof(NULL); 486 487 #define gotoerr(e) { error = e; goto flush;} 488 489 if (m == NULL || 490 (m->m_len < sizeof(long) && 491 (m = m_pullup(m, sizeof(long))) == NULL)) 492 return (ENOBUFS); 493 len = m->m_pkthdr.len; 494 if (len < sizeof(struct rt_msghdr) || 495 len != mtod(m, struct rt_msghdr *)->rtm_msglen) 496 gotoerr(EINVAL); 497 498 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 499 if (rtm == NULL) 500 gotoerr(ENOBUFS); 501 502 m_copydata(m, 0, len, (caddr_t)rtm); 503 if (rtm->rtm_version != RTM_VERSION) 504 gotoerr(EPROTONOSUPPORT); 505 506 rtm->rtm_pid = oi->p_pid; 507 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 508 rtinfo.rti_addrs = rtm->rtm_addrs; 509 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) 510 gotoerr(EINVAL); 511 512 rtinfo.rti_flags = rtm->rtm_flags; 513 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX || 514 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX)) 515 gotoerr(EINVAL); 516 517 family = familyof(rtinfo.rti_dst); 518 519 /* 520 * Verify that the caller has the appropriate privilege; RTM_GET 521 * is the only operation the non-superuser is allowed. 522 */ 523 if (rtm->rtm_type != RTM_GET && 524 priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0) 525 gotoerr(EPERM); 526 527 if (rtinfo.rti_genmask != NULL) { 528 error = rtmask_add_global(rtinfo.rti_genmask, 529 rtm->rtm_type != RTM_GET ? 530 RTREQ_PRIO_HIGH : RTREQ_PRIO_NORM); 531 if (error) 532 goto flush; 533 } 534 535 switch (rtm->rtm_type) { 536 case RTM_ADD: 537 if (rtinfo.rti_gateway == NULL) { 538 error = EINVAL; 539 } else { 540 error = rtrequest1_global(RTM_ADD, &rtinfo, 541 route_output_add_callback, rtm, RTREQ_PRIO_HIGH); 542 } 543 break; 544 case RTM_DELETE: 545 /* 546 * Backing rtm (bak_rtm) could _not_ be freed during 547 * rtrequest1_global or rtsearch_global, even if the 548 * callback reallocates the rtm due to its size changes, 549 * since rtinfo points to the backing rtm's memory area. 550 * After rtrequest1_global or rtsearch_global returns, 551 * it is safe to free the backing rtm, since rtinfo will 552 * not be used anymore. 553 * 554 * new_rtm will be used to save the new rtm allocated 555 * by rtrequest1_global or rtsearch_global. 556 */ 557 arg.bak_rtm = rtm; 558 arg.new_rtm = rtm; 559 error = rtrequest1_global(RTM_DELETE, &rtinfo, 560 route_output_delete_callback, &arg, RTREQ_PRIO_HIGH); 561 rtm = arg.new_rtm; 562 if (rtm != arg.bak_rtm) 563 kfree(arg.bak_rtm, M_RTABLE); 564 break; 565 case RTM_GET: 566 /* See the comment in RTM_DELETE */ 567 arg.bak_rtm = rtm; 568 arg.new_rtm = rtm; 569 error = rtsearch_global(RTM_GET, &rtinfo, 570 route_output_get_callback, &arg, RTS_NOEXACTMATCH, 571 RTREQ_PRIO_NORM); 572 rtm = arg.new_rtm; 573 if (rtm != arg.bak_rtm) 574 kfree(arg.bak_rtm, M_RTABLE); 575 break; 576 case RTM_CHANGE: 577 error = rtsearch_global(RTM_CHANGE, &rtinfo, 578 route_output_change_callback, rtm, RTS_EXACTMATCH, 579 RTREQ_PRIO_HIGH); 580 break; 581 case RTM_LOCK: 582 error = rtsearch_global(RTM_LOCK, &rtinfo, 583 route_output_lock_callback, rtm, RTS_EXACTMATCH, 584 RTREQ_PRIO_HIGH); 585 break; 586 default: 587 error = EOPNOTSUPP; 588 break; 589 } 590 flush: 591 if (rtm != NULL) { 592 if (error != 0) 593 rtm->rtm_errno = error; 594 else 595 rtm->rtm_flags |= RTF_DONE; 596 } 597 598 /* 599 * Check to see if we don't want our own messages. 600 */ 601 if (!(so->so_options & SO_USELOOPBACK)) { 602 if (route_cb.any_count <= 1) { 603 if (rtm != NULL) 604 kfree(rtm, M_RTABLE); 605 m_freem(m); 606 return (error); 607 } 608 /* There is another listener, so construct message */ 609 rp = sotorawcb(so); 610 } 611 if (rtm != NULL) { 612 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 613 if (m->m_pkthdr.len < rtm->rtm_msglen) { 614 m_freem(m); 615 m = NULL; 616 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 617 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 618 kfree(rtm, M_RTABLE); 619 } 620 if (m != NULL) 621 rts_input_skip(m, family, rp); 622 return (error); 623 } 624 625 static void 626 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 627 struct rtentry *rt, void *arg) 628 { 629 struct rt_msghdr *rtm = arg; 630 631 if (error == 0 && rt != NULL) { 632 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 633 &rt->rt_rmx); 634 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 635 rt->rt_rmx.rmx_locks |= 636 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 637 if (rtinfo->rti_genmask != NULL) { 638 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask); 639 if (rt->rt_genmask == NULL) { 640 /* 641 * This should not happen, since we 642 * have already installed genmask 643 * on each CPU before we reach here. 644 */ 645 panic("genmask is gone!?"); 646 } 647 } else { 648 rt->rt_genmask = NULL; 649 } 650 rtm->rtm_index = rt->rt_ifp->if_index; 651 } 652 } 653 654 static void 655 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 656 struct rtentry *rt, void *arg) 657 { 658 if (error == 0 && rt) { 659 ++rt->rt_refcnt; 660 if (fillrtmsg(arg, rt, rtinfo) != 0) { 661 error = ENOBUFS; 662 /* XXX no way to return the error */ 663 } 664 --rt->rt_refcnt; 665 } 666 if (rt && rt->rt_refcnt == 0) { 667 ++rt->rt_refcnt; 668 rtfree(rt); 669 } 670 } 671 672 static int 673 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo, 674 struct rtentry *rt, void *arg, int found_cnt) 675 { 676 int error, found = 0; 677 678 if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0) 679 found = 1; 680 681 error = fillrtmsg(arg, rt, rtinfo); 682 if (!error && found) { 683 /* Got the exact match, we could return now! */ 684 error = EJUSTRETURN; 685 } 686 return error; 687 } 688 689 static int 690 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo, 691 struct rtentry *rt, void *arg, int found_cnt) 692 { 693 struct rt_msghdr *rtm = arg; 694 struct ifaddr *ifa; 695 int error = 0; 696 697 /* 698 * new gateway could require new ifaddr, ifp; 699 * flags may also be different; ifp may be specified 700 * by ll sockaddr when protocol address is ambiguous 701 */ 702 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) || 703 rtinfo->rti_ifpaddr != NULL || 704 (rtinfo->rti_ifaaddr != NULL && 705 !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) { 706 error = rt_getifa(rtinfo); 707 if (error != 0) 708 goto done; 709 } 710 if (rtinfo->rti_gateway != NULL) { 711 /* 712 * We only need to generate rtmsg upon the 713 * first route to be changed. 714 */ 715 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway, 716 found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT); 717 if (error != 0) 718 goto done; 719 } 720 if ((ifa = rtinfo->rti_ifa) != NULL) { 721 struct ifaddr *oifa = rt->rt_ifa; 722 723 if (oifa != ifa) { 724 if (oifa && oifa->ifa_rtrequest) 725 oifa->ifa_rtrequest(RTM_DELETE, rt); 726 IFAFREE(rt->rt_ifa); 727 IFAREF(ifa); 728 rt->rt_ifa = ifa; 729 rt->rt_ifp = rtinfo->rti_ifp; 730 } 731 } 732 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx); 733 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 734 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt); 735 if (rtinfo->rti_genmask != NULL) { 736 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask); 737 if (rt->rt_genmask == NULL) { 738 /* 739 * This should not happen, since we 740 * have already installed genmask 741 * on each CPU before we reach here. 742 */ 743 panic("genmask is gone!?"); 744 } 745 } 746 rtm->rtm_index = rt->rt_ifp->if_index; 747 done: 748 return error; 749 } 750 751 static int 752 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo, 753 struct rtentry *rt, void *arg, 754 int found_cnt __unused) 755 { 756 struct rt_msghdr *rtm = arg; 757 758 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 759 rt->rt_rmx.rmx_locks |= 760 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 761 return 0; 762 } 763 764 static void 765 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out) 766 { 767 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt; 768 setmetric(RTV_RPIPE, rmx_recvpipe); 769 setmetric(RTV_SPIPE, rmx_sendpipe); 770 setmetric(RTV_SSTHRESH, rmx_ssthresh); 771 setmetric(RTV_RTT, rmx_rtt); 772 setmetric(RTV_RTTVAR, rmx_rttvar); 773 setmetric(RTV_HOPCOUNT, rmx_hopcount); 774 setmetric(RTV_MTU, rmx_mtu); 775 setmetric(RTV_EXPIRE, rmx_expire); 776 setmetric(RTV_MSL, rmx_msl); 777 setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs); 778 setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs); 779 #undef setmetric 780 } 781 782 #define ROUNDUP(a) \ 783 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 784 785 /* 786 * Extract the addresses of the passed sockaddrs. 787 * Do a little sanity checking so as to avoid bad memory references. 788 * This data is derived straight from userland. 789 */ 790 static int 791 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo) 792 { 793 struct sockaddr *sa; 794 int i; 795 796 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 797 if ((rtinfo->rti_addrs & (1 << i)) == 0) 798 continue; 799 sa = (struct sockaddr *)cp; 800 /* 801 * It won't fit. 802 */ 803 if ((cp + sa->sa_len) > cplim) { 804 return (EINVAL); 805 } 806 807 /* 808 * There are no more... Quit now. 809 * If there are more bits, they are in error. 810 * I've seen this. route(1) can evidently generate these. 811 * This causes kernel to core dump. 812 * For compatibility, if we see this, point to a safe address. 813 */ 814 if (sa->sa_len == 0) { 815 static struct sockaddr sa_zero = { 816 sizeof sa_zero, AF_INET, 817 }; 818 819 rtinfo->rti_info[i] = &sa_zero; 820 kprintf("rtsock: received more addr bits than sockaddrs.\n"); 821 return (0); /* should be EINVAL but for compat */ 822 } 823 824 /* Accept the sockaddr. */ 825 rtinfo->rti_info[i] = sa; 826 cp += ROUNDUP(sa->sa_len); 827 } 828 return (0); 829 } 830 831 static int 832 rt_msghdrsize(int type) 833 { 834 switch (type) { 835 case RTM_DELADDR: 836 case RTM_NEWADDR: 837 return sizeof(struct ifa_msghdr); 838 case RTM_DELMADDR: 839 case RTM_NEWMADDR: 840 return sizeof(struct ifma_msghdr); 841 case RTM_IFINFO: 842 return sizeof(struct if_msghdr); 843 case RTM_IFANNOUNCE: 844 case RTM_IEEE80211: 845 return sizeof(struct if_announcemsghdr); 846 default: 847 return sizeof(struct rt_msghdr); 848 } 849 } 850 851 static int 852 rt_msgsize(int type, struct rt_addrinfo *rtinfo) 853 { 854 int len, i; 855 856 len = rt_msghdrsize(type); 857 for (i = 0; i < RTAX_MAX; i++) { 858 if (rtinfo->rti_info[i] != NULL) 859 len += ROUNDUP(rtinfo->rti_info[i]->sa_len); 860 } 861 len = ALIGN(len); 862 return len; 863 } 864 865 /* 866 * Build a routing message in a buffer. 867 * Copy the addresses in the rtinfo->rti_info[] sockaddr array 868 * to the end of the buffer after the message header. 869 * 870 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. 871 * This side-effect can be avoided if we reorder the addrs bitmask field in all 872 * the route messages to line up so we can set it here instead of back in the 873 * calling routine. 874 */ 875 static void 876 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen) 877 { 878 struct rt_msghdr *rtm; 879 char *cp; 880 int dlen, i; 881 882 rtm = (struct rt_msghdr *) buf; 883 rtm->rtm_version = RTM_VERSION; 884 rtm->rtm_type = type; 885 rtm->rtm_msglen = msglen; 886 887 cp = (char *)buf + rt_msghdrsize(type); 888 rtinfo->rti_addrs = 0; 889 for (i = 0; i < RTAX_MAX; i++) { 890 struct sockaddr *sa; 891 892 if ((sa = rtinfo->rti_info[i]) == NULL) 893 continue; 894 rtinfo->rti_addrs |= (1 << i); 895 dlen = ROUNDUP(sa->sa_len); 896 bcopy(sa, cp, dlen); 897 cp += dlen; 898 } 899 } 900 901 /* 902 * Build a routing message in a mbuf chain. 903 * Copy the addresses in the rtinfo->rti_info[] sockaddr array 904 * to the end of the mbuf after the message header. 905 * 906 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. 907 * This side-effect can be avoided if we reorder the addrs bitmask field in all 908 * the route messages to line up so we can set it here instead of back in the 909 * calling routine. 910 */ 911 static struct mbuf * 912 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo) 913 { 914 struct mbuf *m; 915 struct rt_msghdr *rtm; 916 int hlen, len; 917 int i; 918 919 hlen = rt_msghdrsize(type); 920 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen)); 921 922 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL); 923 if (m == NULL) 924 return (NULL); 925 mbuftrackid(m, 32); 926 m->m_pkthdr.len = m->m_len = hlen; 927 m->m_pkthdr.rcvif = NULL; 928 rtinfo->rti_addrs = 0; 929 len = hlen; 930 for (i = 0; i < RTAX_MAX; i++) { 931 struct sockaddr *sa; 932 int dlen; 933 934 if ((sa = rtinfo->rti_info[i]) == NULL) 935 continue; 936 rtinfo->rti_addrs |= (1 << i); 937 dlen = ROUNDUP(sa->sa_len); 938 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */ 939 len += dlen; 940 } 941 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */ 942 m_freem(m); 943 return (NULL); 944 } 945 rtm = mtod(m, struct rt_msghdr *); 946 bzero(rtm, hlen); 947 rtm->rtm_msglen = len; 948 rtm->rtm_version = RTM_VERSION; 949 rtm->rtm_type = type; 950 return (m); 951 } 952 953 /* 954 * This routine is called to generate a message from the routing 955 * socket indicating that a redirect has occurred, a routing lookup 956 * has failed, or that a protocol has detected timeouts to a particular 957 * destination. 958 */ 959 void 960 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 961 { 962 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; 963 struct rt_msghdr *rtm; 964 struct mbuf *m; 965 966 if (route_cb.any_count == 0) 967 return; 968 m = rt_msg_mbuf(type, rtinfo); 969 if (m == NULL) 970 return; 971 rtm = mtod(m, struct rt_msghdr *); 972 rtm->rtm_flags = RTF_DONE | flags; 973 rtm->rtm_errno = error; 974 rtm->rtm_addrs = rtinfo->rti_addrs; 975 rts_input(m, familyof(dst)); 976 } 977 978 void 979 rt_dstmsg(int type, struct sockaddr *dst, int error) 980 { 981 struct rt_msghdr *rtm; 982 struct rt_addrinfo addrs; 983 struct mbuf *m; 984 985 if (route_cb.any_count == 0) 986 return; 987 bzero(&addrs, sizeof(struct rt_addrinfo)); 988 addrs.rti_info[RTAX_DST] = dst; 989 m = rt_msg_mbuf(type, &addrs); 990 if (m == NULL) 991 return; 992 rtm = mtod(m, struct rt_msghdr *); 993 rtm->rtm_flags = RTF_DONE; 994 rtm->rtm_errno = error; 995 rtm->rtm_addrs = addrs.rti_addrs; 996 rts_input(m, familyof(dst)); 997 } 998 999 /* 1000 * This routine is called to generate a message from the routing 1001 * socket indicating that the status of a network interface has changed. 1002 */ 1003 void 1004 rt_ifmsg(struct ifnet *ifp) 1005 { 1006 struct if_msghdr *ifm; 1007 struct mbuf *m; 1008 struct rt_addrinfo rtinfo; 1009 1010 if (route_cb.any_count == 0) 1011 return; 1012 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1013 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo); 1014 if (m == NULL) 1015 return; 1016 ifm = mtod(m, struct if_msghdr *); 1017 ifm->ifm_index = ifp->if_index; 1018 ifm->ifm_flags = ifp->if_flags; 1019 ifm->ifm_data = ifp->if_data; 1020 ifm->ifm_addrs = 0; 1021 rts_input(m, 0); 1022 } 1023 1024 static void 1025 rt_ifamsg(int cmd, struct ifaddr *ifa) 1026 { 1027 struct ifa_msghdr *ifam; 1028 struct rt_addrinfo rtinfo; 1029 struct mbuf *m; 1030 struct ifnet *ifp = ifa->ifa_ifp; 1031 1032 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1033 rtinfo.rti_ifaaddr = ifa->ifa_addr; 1034 rtinfo.rti_ifpaddr = 1035 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1036 rtinfo.rti_netmask = ifa->ifa_netmask; 1037 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; 1038 1039 m = rt_msg_mbuf(cmd, &rtinfo); 1040 if (m == NULL) 1041 return; 1042 1043 ifam = mtod(m, struct ifa_msghdr *); 1044 ifam->ifam_index = ifp->if_index; 1045 ifam->ifam_metric = ifa->ifa_metric; 1046 ifam->ifam_flags = ifa->ifa_flags; 1047 ifam->ifam_addrs = rtinfo.rti_addrs; 1048 1049 rts_input(m, familyof(ifa->ifa_addr)); 1050 } 1051 1052 void 1053 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error) 1054 { 1055 struct rt_msghdr *rtm; 1056 struct rt_addrinfo rtinfo; 1057 struct mbuf *m; 1058 struct sockaddr *dst; 1059 1060 if (rt == NULL) 1061 return; 1062 1063 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1064 rtinfo.rti_dst = dst = rt_key(rt); 1065 rtinfo.rti_gateway = rt->rt_gateway; 1066 rtinfo.rti_netmask = rt_mask(rt); 1067 if (ifp != NULL) { 1068 rtinfo.rti_ifpaddr = 1069 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1070 } 1071 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr; 1072 1073 m = rt_msg_mbuf(cmd, &rtinfo); 1074 if (m == NULL) 1075 return; 1076 1077 rtm = mtod(m, struct rt_msghdr *); 1078 if (ifp != NULL) 1079 rtm->rtm_index = ifp->if_index; 1080 rtm->rtm_flags |= rt->rt_flags; 1081 rtm->rtm_errno = error; 1082 rtm->rtm_addrs = rtinfo.rti_addrs; 1083 1084 rts_input(m, familyof(dst)); 1085 } 1086 1087 /* 1088 * This is called to generate messages from the routing socket 1089 * indicating a network interface has had addresses associated with it. 1090 * if we ever reverse the logic and replace messages TO the routing 1091 * socket indicate a request to configure interfaces, then it will 1092 * be unnecessary as the routing socket will automatically generate 1093 * copies of it. 1094 */ 1095 void 1096 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1097 { 1098 #ifdef SCTP 1099 /* 1100 * notify the SCTP stack 1101 * this will only get called when an address is added/deleted 1102 * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1103 */ 1104 if (cmd == RTM_ADD) 1105 sctp_add_ip_address(ifa); 1106 else if (cmd == RTM_DELETE) 1107 sctp_delete_ip_address(ifa); 1108 #endif /* SCTP */ 1109 1110 if (route_cb.any_count == 0) 1111 return; 1112 1113 if (cmd == RTM_ADD) { 1114 rt_ifamsg(RTM_NEWADDR, ifa); 1115 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error); 1116 } else { 1117 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd)); 1118 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error); 1119 rt_ifamsg(RTM_DELADDR, ifa); 1120 } 1121 } 1122 1123 /* 1124 * This is the analogue to the rt_newaddrmsg which performs the same 1125 * function but for multicast group memberhips. This is easier since 1126 * there is no route state to worry about. 1127 */ 1128 void 1129 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1130 { 1131 struct rt_addrinfo rtinfo; 1132 struct mbuf *m = NULL; 1133 struct ifnet *ifp = ifma->ifma_ifp; 1134 struct ifma_msghdr *ifmam; 1135 1136 if (route_cb.any_count == 0) 1137 return; 1138 1139 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1140 rtinfo.rti_ifaaddr = ifma->ifma_addr; 1141 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) { 1142 rtinfo.rti_ifpaddr = 1143 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1144 } 1145 /* 1146 * If a link-layer address is present, present it as a ``gateway'' 1147 * (similarly to how ARP entries, e.g., are presented). 1148 */ 1149 rtinfo.rti_gateway = ifma->ifma_lladdr; 1150 1151 m = rt_msg_mbuf(cmd, &rtinfo); 1152 if (m == NULL) 1153 return; 1154 1155 ifmam = mtod(m, struct ifma_msghdr *); 1156 ifmam->ifmam_index = ifp->if_index; 1157 ifmam->ifmam_addrs = rtinfo.rti_addrs; 1158 1159 rts_input(m, familyof(ifma->ifma_addr)); 1160 } 1161 1162 static struct mbuf * 1163 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1164 struct rt_addrinfo *info) 1165 { 1166 struct if_announcemsghdr *ifan; 1167 struct mbuf *m; 1168 1169 if (route_cb.any_count == 0) 1170 return NULL; 1171 1172 bzero(info, sizeof(*info)); 1173 m = rt_msg_mbuf(type, info); 1174 if (m == NULL) 1175 return NULL; 1176 1177 ifan = mtod(m, struct if_announcemsghdr *); 1178 ifan->ifan_index = ifp->if_index; 1179 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name); 1180 ifan->ifan_what = what; 1181 return m; 1182 } 1183 1184 /* 1185 * This is called to generate routing socket messages indicating 1186 * IEEE80211 wireless events. 1187 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1188 */ 1189 void 1190 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1191 { 1192 struct rt_addrinfo info; 1193 struct mbuf *m; 1194 1195 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1196 if (m == NULL) 1197 return; 1198 1199 /* 1200 * Append the ieee80211 data. Try to stick it in the 1201 * mbuf containing the ifannounce msg; otherwise allocate 1202 * a new mbuf and append. 1203 * 1204 * NB: we assume m is a single mbuf. 1205 */ 1206 if (data_len > M_TRAILINGSPACE(m)) { 1207 /* XXX use m_getb(data_len, MB_DONTWAIT, MT_DATA, 0); */ 1208 struct mbuf *n = m_get(MB_DONTWAIT, MT_DATA); 1209 if (n == NULL) { 1210 m_freem(m); 1211 return; 1212 } 1213 KKASSERT(data_len <= M_TRAILINGSPACE(n)); 1214 bcopy(data, mtod(n, void *), data_len); 1215 n->m_len = data_len; 1216 m->m_next = n; 1217 } else if (data_len > 0) { 1218 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1219 m->m_len += data_len; 1220 } 1221 mbuftrackid(m, 33); 1222 if (m->m_flags & M_PKTHDR) 1223 m->m_pkthdr.len += data_len; 1224 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1225 rts_input(m, 0); 1226 } 1227 1228 /* 1229 * This is called to generate routing socket messages indicating 1230 * network interface arrival and departure. 1231 */ 1232 void 1233 rt_ifannouncemsg(struct ifnet *ifp, int what) 1234 { 1235 struct rt_addrinfo addrinfo; 1236 struct mbuf *m; 1237 1238 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo); 1239 if (m != NULL) 1240 rts_input(m, 0); 1241 } 1242 1243 static int 1244 resizewalkarg(struct walkarg *w, int len) 1245 { 1246 void *newptr; 1247 1248 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 1249 if (newptr == NULL) 1250 return (ENOMEM); 1251 if (w->w_tmem != NULL) 1252 kfree(w->w_tmem, M_RTABLE); 1253 w->w_tmem = newptr; 1254 w->w_tmemsize = len; 1255 return (0); 1256 } 1257 1258 /* 1259 * This is used in dumping the kernel table via sysctl(). 1260 */ 1261 int 1262 sysctl_dumpentry(struct radix_node *rn, void *vw) 1263 { 1264 struct walkarg *w = vw; 1265 struct rtentry *rt = (struct rtentry *)rn; 1266 struct rt_addrinfo rtinfo; 1267 int error, msglen; 1268 1269 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1270 return 0; 1271 1272 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1273 rtinfo.rti_dst = rt_key(rt); 1274 rtinfo.rti_gateway = rt->rt_gateway; 1275 rtinfo.rti_netmask = rt_mask(rt); 1276 rtinfo.rti_genmask = rt->rt_genmask; 1277 if (rt->rt_ifp != NULL) { 1278 rtinfo.rti_ifpaddr = 1279 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1280 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr; 1281 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1282 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; 1283 } 1284 msglen = rt_msgsize(RTM_GET, &rtinfo); 1285 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) 1286 return (ENOMEM); 1287 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen); 1288 if (w->w_req != NULL) { 1289 struct rt_msghdr *rtm = w->w_tmem; 1290 1291 rtm->rtm_flags = rt->rt_flags; 1292 rtm->rtm_use = rt->rt_use; 1293 rtm->rtm_rmx = rt->rt_rmx; 1294 rtm->rtm_index = rt->rt_ifp->if_index; 1295 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1296 rtm->rtm_addrs = rtinfo.rti_addrs; 1297 error = SYSCTL_OUT(w->w_req, rtm, msglen); 1298 return (error); 1299 } 1300 return (0); 1301 } 1302 1303 static void 1304 ifnet_compute_stats(struct ifnet *ifp) 1305 { 1306 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets); 1307 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors); 1308 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets); 1309 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions); 1310 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes); 1311 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes); 1312 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts); 1313 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts); 1314 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops); 1315 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto); 1316 } 1317 1318 static int 1319 sysctl_iflist(int af, struct walkarg *w) 1320 { 1321 struct ifnet *ifp; 1322 struct rt_addrinfo rtinfo; 1323 int msglen, error; 1324 1325 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1326 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1327 struct ifaddr_container *ifac; 1328 struct ifaddr *ifa; 1329 1330 if (w->w_arg && w->w_arg != ifp->if_index) 1331 continue; 1332 ifac = TAILQ_FIRST(&ifp->if_addrheads[mycpuid]); 1333 ifa = ifac->ifa; 1334 rtinfo.rti_ifpaddr = ifa->ifa_addr; 1335 msglen = rt_msgsize(RTM_IFINFO, &rtinfo); 1336 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) 1337 return (ENOMEM); 1338 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen); 1339 rtinfo.rti_ifpaddr = NULL; 1340 if (w->w_req != NULL && w->w_tmem != NULL) { 1341 struct if_msghdr *ifm = w->w_tmem; 1342 1343 ifm->ifm_index = ifp->if_index; 1344 ifm->ifm_flags = ifp->if_flags; 1345 ifnet_compute_stats(ifp); 1346 ifm->ifm_data = ifp->if_data; 1347 ifm->ifm_addrs = rtinfo.rti_addrs; 1348 error = SYSCTL_OUT(w->w_req, ifm, msglen); 1349 if (error) 1350 return (error); 1351 } 1352 while ((ifac = TAILQ_NEXT(ifac, ifa_link)) != NULL) { 1353 ifa = ifac->ifa; 1354 1355 if (af && af != ifa->ifa_addr->sa_family) 1356 continue; 1357 if (curproc->p_ucred->cr_prison && 1358 prison_if(curproc->p_ucred, ifa->ifa_addr)) 1359 continue; 1360 rtinfo.rti_ifaaddr = ifa->ifa_addr; 1361 rtinfo.rti_netmask = ifa->ifa_netmask; 1362 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; 1363 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo); 1364 if (w->w_tmemsize < msglen && 1365 resizewalkarg(w, msglen) != 0) 1366 return (ENOMEM); 1367 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen); 1368 if (w->w_req != NULL) { 1369 struct ifa_msghdr *ifam = w->w_tmem; 1370 1371 ifam->ifam_index = ifa->ifa_ifp->if_index; 1372 ifam->ifam_flags = ifa->ifa_flags; 1373 ifam->ifam_metric = ifa->ifa_metric; 1374 ifam->ifam_addrs = rtinfo.rti_addrs; 1375 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen); 1376 if (error) 1377 return (error); 1378 } 1379 } 1380 rtinfo.rti_netmask = NULL; 1381 rtinfo.rti_ifaaddr = NULL; 1382 rtinfo.rti_bcastaddr = NULL; 1383 } 1384 return (0); 1385 } 1386 1387 static int 1388 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1389 { 1390 int *name = (int *)arg1; 1391 u_int namelen = arg2; 1392 struct radix_node_head *rnh; 1393 int i, error = EINVAL; 1394 int origcpu; 1395 u_char af; 1396 struct walkarg w; 1397 1398 name ++; 1399 namelen--; 1400 if (req->newptr) 1401 return (EPERM); 1402 if (namelen != 3 && namelen != 4) 1403 return (EINVAL); 1404 af = name[0]; 1405 bzero(&w, sizeof w); 1406 w.w_op = name[1]; 1407 w.w_arg = name[2]; 1408 w.w_req = req; 1409 1410 /* 1411 * Optional third argument specifies cpu, used primarily for 1412 * debugging the route table. 1413 */ 1414 if (namelen == 4) { 1415 if (name[3] < 0 || name[3] >= ncpus) 1416 return (EINVAL); 1417 origcpu = mycpuid; 1418 lwkt_migratecpu(name[3]); 1419 } else { 1420 origcpu = -1; 1421 } 1422 crit_enter(); 1423 switch (w.w_op) { 1424 case NET_RT_DUMP: 1425 case NET_RT_FLAGS: 1426 for (i = 1; i <= AF_MAX; i++) 1427 if ((rnh = rt_tables[mycpuid][i]) && 1428 (af == 0 || af == i) && 1429 (error = rnh->rnh_walktree(rnh, 1430 sysctl_dumpentry, &w))) 1431 break; 1432 break; 1433 1434 case NET_RT_IFLIST: 1435 error = sysctl_iflist(af, &w); 1436 } 1437 crit_exit(); 1438 if (w.w_tmem != NULL) 1439 kfree(w.w_tmem, M_RTABLE); 1440 if (origcpu >= 0) 1441 lwkt_migratecpu(origcpu); 1442 return (error); 1443 } 1444 1445 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1446 1447 /* 1448 * Definitions of protocols supported in the ROUTE domain. 1449 */ 1450 1451 static struct domain routedomain; /* or at least forward */ 1452 1453 static struct protosw routesw[] = { 1454 { 1455 .pr_type = SOCK_RAW, 1456 .pr_domain = &routedomain, 1457 .pr_protocol = 0, 1458 .pr_flags = PR_ATOMIC|PR_ADDR, 1459 .pr_input = NULL, 1460 .pr_output = route_output, 1461 .pr_ctlinput = raw_ctlinput, 1462 .pr_ctloutput = NULL, 1463 .pr_ctlport = cpu0_ctlport, 1464 1465 .pr_init = raw_init, 1466 .pr_usrreqs = &route_usrreqs 1467 } 1468 }; 1469 1470 static struct domain routedomain = { 1471 PF_ROUTE, "route", NULL, NULL, NULL, 1472 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])], 1473 }; 1474 1475 DOMAIN_SET(route); 1476 1477