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 <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/kernel.h> 68 #include <sys/sysctl.h> 69 #include <sys/proc.h> 70 #include <sys/priv.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/protosw.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/domain.h> 77 #include <sys/jail.h> 78 79 #include <sys/thread2.h> 80 #include <sys/socketvar2.h> 81 82 #include <net/if.h> 83 #include <net/if_var.h> 84 #include <net/route.h> 85 #include <net/raw_cb.h> 86 #include <net/netmsg2.h> 87 #include <net/netisr2.h> 88 89 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 90 91 static struct route_cb { 92 int ip_count; 93 int ip6_count; 94 int ns_count; 95 int any_count; 96 } route_cb; 97 98 static const struct sockaddr route_src = { 2, PF_ROUTE, }; 99 100 struct walkarg { 101 int w_tmemsize; 102 int w_op, w_arg; 103 void *w_tmem; 104 struct sysctl_req *w_req; 105 }; 106 107 #ifndef RTTABLE_DUMP_MSGCNT_MAX 108 /* Should be large enough for dupkeys */ 109 #define RTTABLE_DUMP_MSGCNT_MAX 64 110 #endif 111 112 struct rttable_walkarg { 113 int w_op; 114 int w_arg; 115 int w_bufsz; 116 void *w_buf; 117 118 int w_buflen; 119 120 const char *w_key; 121 const char *w_mask; 122 123 struct sockaddr_storage w_key0; 124 struct sockaddr_storage w_mask0; 125 }; 126 127 struct netmsg_rttable_walk { 128 struct netmsg_base base; 129 int af; 130 struct rttable_walkarg *w; 131 }; 132 133 struct routecb { 134 struct rawcb rocb_rcb; 135 unsigned int rocb_msgfilter; 136 }; 137 #define sotoroutecb(so) ((struct routecb *)(so)->so_pcb) 138 139 static struct mbuf * 140 rt_msg_mbuf (int, struct rt_addrinfo *); 141 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len); 142 static int rt_msgsize(int type, const struct rt_addrinfo *rtinfo); 143 static int rt_xaddrs (char *, char *, struct rt_addrinfo *); 144 static int sysctl_rttable(int af, struct sysctl_req *req, int op, int arg); 145 static int sysctl_iflist (int af, struct walkarg *w); 146 static int route_output(struct mbuf *, struct socket *, ...); 147 static void rt_setmetrics (u_long, struct rt_metrics *, 148 struct rt_metrics *); 149 150 /* 151 * It really doesn't make any sense at all for this code to share much 152 * with raw_usrreq.c, since its functionality is so restricted. XXX 153 */ 154 static void 155 rts_abort(netmsg_t msg) 156 { 157 crit_enter(); 158 raw_usrreqs.pru_abort(msg); 159 /* msg invalid now */ 160 crit_exit(); 161 } 162 163 static int 164 rts_filter(struct mbuf *m, const struct sockproto *proto, 165 const struct rawcb *rp) 166 { 167 const struct routecb *rop = (const struct routecb *)rp; 168 const struct rt_msghdr *rtm; 169 170 KKASSERT(m != NULL); 171 KKASSERT(proto != NULL); 172 KKASSERT(rp != NULL); 173 174 /* Wrong family for this socket. */ 175 if (proto->sp_family != PF_ROUTE) 176 return ENOPROTOOPT; 177 178 /* If no filter set, just return. */ 179 if (rop->rocb_msgfilter == 0) 180 return 0; 181 182 /* Ensure we can access rtm_type */ 183 if (m->m_len < 184 offsetof(struct rt_msghdr, rtm_type) + sizeof(rtm->rtm_type)) 185 return EINVAL; 186 187 rtm = mtod(m, const struct rt_msghdr *); 188 /* If the rtm type is filtered out, return a positive. */ 189 if (!(rop->rocb_msgfilter & ROUTE_FILTER(rtm->rtm_type))) 190 return EEXIST; 191 192 /* Passed the filter. */ 193 return 0; 194 } 195 196 197 /* pru_accept is EOPNOTSUPP */ 198 199 static void 200 rts_attach(netmsg_t msg) 201 { 202 struct socket *so = msg->base.nm_so; 203 struct pru_attach_info *ai = msg->attach.nm_ai; 204 struct rawcb *rp; 205 struct routecb *rop; 206 int proto = msg->attach.nm_proto; 207 int error; 208 209 crit_enter(); 210 if (sotorawcb(so) != NULL) { 211 error = EISCONN; 212 goto done; 213 } 214 215 rop = kmalloc(sizeof *rop, M_PCB, M_WAITOK | M_ZERO); 216 rp = &rop->rocb_rcb; 217 218 /* 219 * The critical section is necessary to block protocols from sending 220 * error notifications (like RTM_REDIRECT or RTM_LOSING) while 221 * this PCB is extant but incompletely initialized. 222 * Probably we should try to do more of this work beforehand and 223 * eliminate the critical section. 224 */ 225 so->so_pcb = rp; 226 soreference(so); /* so_pcb assignment */ 227 error = raw_attach(so, proto, ai->sb_rlimit); 228 rp = sotorawcb(so); 229 if (error) { 230 kfree(rop, M_PCB); 231 goto done; 232 } 233 switch(rp->rcb_proto.sp_protocol) { 234 case AF_INET: 235 route_cb.ip_count++; 236 break; 237 case AF_INET6: 238 route_cb.ip6_count++; 239 break; 240 } 241 rp->rcb_faddr = &route_src; 242 rp->rcb_filter = rts_filter; 243 route_cb.any_count++; 244 soisconnected(so); 245 so->so_options |= SO_USELOOPBACK; 246 error = 0; 247 done: 248 crit_exit(); 249 lwkt_replymsg(&msg->lmsg, error); 250 } 251 252 static void 253 rts_bind(netmsg_t msg) 254 { 255 crit_enter(); 256 raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */ 257 /* msg invalid now */ 258 crit_exit(); 259 } 260 261 static void 262 rts_connect(netmsg_t msg) 263 { 264 crit_enter(); 265 raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */ 266 /* msg invalid now */ 267 crit_exit(); 268 } 269 270 /* pru_connect2 is EOPNOTSUPP */ 271 /* pru_control is EOPNOTSUPP */ 272 273 static void 274 rts_detach(netmsg_t msg) 275 { 276 struct socket *so = msg->base.nm_so; 277 struct rawcb *rp = sotorawcb(so); 278 279 crit_enter(); 280 if (rp != NULL) { 281 switch(rp->rcb_proto.sp_protocol) { 282 case AF_INET: 283 route_cb.ip_count--; 284 break; 285 case AF_INET6: 286 route_cb.ip6_count--; 287 break; 288 } 289 route_cb.any_count--; 290 } 291 raw_usrreqs.pru_detach(msg); 292 /* msg invalid now */ 293 crit_exit(); 294 } 295 296 static void 297 rts_disconnect(netmsg_t msg) 298 { 299 crit_enter(); 300 raw_usrreqs.pru_disconnect(msg); 301 /* msg invalid now */ 302 crit_exit(); 303 } 304 305 /* pru_listen is EOPNOTSUPP */ 306 307 static void 308 rts_peeraddr(netmsg_t msg) 309 { 310 crit_enter(); 311 raw_usrreqs.pru_peeraddr(msg); 312 /* msg invalid now */ 313 crit_exit(); 314 } 315 316 /* pru_rcvd is EOPNOTSUPP */ 317 /* pru_rcvoob is EOPNOTSUPP */ 318 319 static void 320 rts_send(netmsg_t msg) 321 { 322 crit_enter(); 323 raw_usrreqs.pru_send(msg); 324 /* msg invalid now */ 325 crit_exit(); 326 } 327 328 /* pru_sense is null */ 329 330 static void 331 rts_shutdown(netmsg_t msg) 332 { 333 crit_enter(); 334 raw_usrreqs.pru_shutdown(msg); 335 /* msg invalid now */ 336 crit_exit(); 337 } 338 339 static void 340 rts_sockaddr(netmsg_t msg) 341 { 342 crit_enter(); 343 raw_usrreqs.pru_sockaddr(msg); 344 /* msg invalid now */ 345 crit_exit(); 346 } 347 348 static struct pr_usrreqs route_usrreqs = { 349 .pru_abort = rts_abort, 350 .pru_accept = pr_generic_notsupp, 351 .pru_attach = rts_attach, 352 .pru_bind = rts_bind, 353 .pru_connect = rts_connect, 354 .pru_connect2 = pr_generic_notsupp, 355 .pru_control = pr_generic_notsupp, 356 .pru_detach = rts_detach, 357 .pru_disconnect = rts_disconnect, 358 .pru_listen = pr_generic_notsupp, 359 .pru_peeraddr = rts_peeraddr, 360 .pru_rcvd = pr_generic_notsupp, 361 .pru_rcvoob = pr_generic_notsupp, 362 .pru_send = rts_send, 363 .pru_sense = pru_sense_null, 364 .pru_shutdown = rts_shutdown, 365 .pru_sockaddr = rts_sockaddr, 366 .pru_sosend = sosend, 367 .pru_soreceive = soreceive 368 }; 369 370 static __inline sa_family_t 371 familyof(struct sockaddr *sa) 372 { 373 return (sa != NULL ? sa->sa_family : 0); 374 } 375 376 /* 377 * Routing socket input function. The packet must be serialized onto cpu 0. 378 * We use the cpu0_soport() netisr processing loop to handle it. 379 * 380 * This looks messy but it means that anyone, including interrupt code, 381 * can send a message to the routing socket. 382 */ 383 static void 384 rts_input_handler(netmsg_t msg) 385 { 386 static const struct sockaddr route_dst = { 2, PF_ROUTE, }; 387 struct sockproto route_proto; 388 struct netmsg_packet *pmsg = &msg->packet; 389 struct mbuf *m; 390 sa_family_t family; 391 struct rawcb *skip; 392 393 family = pmsg->base.lmsg.u.ms_result; 394 route_proto.sp_family = PF_ROUTE; 395 route_proto.sp_protocol = family; 396 397 m = pmsg->nm_packet; 398 M_ASSERTPKTHDR(m); 399 400 skip = m->m_pkthdr.header; 401 m->m_pkthdr.header = NULL; 402 403 raw_input(m, &route_proto, &route_src, &route_dst, skip); 404 } 405 406 static void 407 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip) 408 { 409 struct netmsg_packet *pmsg; 410 lwkt_port_t port; 411 412 M_ASSERTPKTHDR(m); 413 414 port = netisr_cpuport(0); /* XXX same as for routing socket */ 415 pmsg = &m->m_hdr.mh_netmsg; 416 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 417 0, rts_input_handler); 418 pmsg->nm_packet = m; 419 pmsg->base.lmsg.u.ms_result = family; 420 m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */ 421 lwkt_sendmsg(port, &pmsg->base.lmsg); 422 } 423 424 static __inline void 425 rts_input(struct mbuf *m, sa_family_t family) 426 { 427 rts_input_skip(m, family, NULL); 428 } 429 430 static void 431 route_ctloutput(netmsg_t msg) 432 { 433 struct socket *so = msg->ctloutput.base.nm_so; 434 struct sockopt *sopt = msg->ctloutput.nm_sopt; 435 struct routecb *rop = sotoroutecb(so); 436 int error; 437 unsigned int msgfilter; 438 439 if (sopt->sopt_level != AF_ROUTE) { 440 error = EINVAL; 441 goto out; 442 } 443 444 error = 0; 445 446 switch (sopt->sopt_dir) { 447 case SOPT_SET: 448 switch (sopt->sopt_name) { 449 case ROUTE_MSGFILTER: 450 error = soopt_to_kbuf(sopt, &msgfilter, 451 sizeof(msgfilter), sizeof(msgfilter)); 452 if (error == 0) 453 rop->rocb_msgfilter = msgfilter; 454 break; 455 default: 456 error = ENOPROTOOPT; 457 break; 458 } 459 break; 460 case SOPT_GET: 461 switch (sopt->sopt_name) { 462 case ROUTE_MSGFILTER: 463 msgfilter = rop->rocb_msgfilter; 464 soopt_from_kbuf(sopt, &msgfilter, sizeof(msgfilter)); 465 break; 466 default: 467 error = ENOPROTOOPT; 468 break; 469 } 470 } 471 out: 472 lwkt_replymsg(&msg->ctloutput.base.lmsg, error); 473 } 474 475 476 477 static void * 478 reallocbuf_nofree(void *ptr, size_t len, size_t olen) 479 { 480 void *newptr; 481 482 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 483 if (newptr == NULL) 484 return NULL; 485 bcopy(ptr, newptr, olen); 486 return (newptr); 487 } 488 489 /* 490 * Internal helper routine for route_output(). 491 */ 492 static int 493 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt, 494 struct rt_addrinfo *rtinfo) 495 { 496 int msglen; 497 struct rt_msghdr *rtm = *prtm; 498 499 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */ 500 rtinfo->rti_dst = rt_key(rt); 501 rtinfo->rti_gateway = rt->rt_gateway; 502 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */ 503 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */ 504 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 505 if (rt->rt_ifp != NULL) { 506 rtinfo->rti_ifpaddr = 507 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid]) 508 ->ifa->ifa_addr; 509 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr; 510 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 511 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; 512 rtm->rtm_index = rt->rt_ifp->if_index; 513 } else { 514 rtinfo->rti_ifpaddr = NULL; 515 rtinfo->rti_ifaaddr = NULL; 516 } 517 } else if (rt->rt_ifp != NULL) { 518 rtm->rtm_index = rt->rt_ifp->if_index; 519 } 520 521 msglen = rt_msgsize(rtm->rtm_type, rtinfo); 522 if (rtm->rtm_msglen < msglen) { 523 /* NOTE: Caller will free the old rtm accordingly */ 524 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen); 525 if (rtm == NULL) 526 return (ENOBUFS); 527 *prtm = rtm; 528 } 529 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen); 530 531 rtm->rtm_flags = rt->rt_flags; 532 rtm->rtm_rmx = rt->rt_rmx; 533 rtm->rtm_addrs = rtinfo->rti_addrs; 534 535 return (0); 536 } 537 538 struct rtm_arg { 539 struct rt_msghdr *bak_rtm; 540 struct rt_msghdr *new_rtm; 541 }; 542 543 static int 544 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt, 545 struct rt_addrinfo *rtinfo) 546 { 547 struct rt_msghdr *rtm = arg->new_rtm; 548 int error; 549 550 error = _fillrtmsg(&rtm, rt, rtinfo); 551 if (!error) { 552 if (arg->new_rtm != rtm) { 553 /* 554 * _fillrtmsg() just allocated a new rtm; 555 * if the previously allocated rtm is not 556 * the backing rtm, it should be freed. 557 */ 558 if (arg->new_rtm != arg->bak_rtm) 559 kfree(arg->new_rtm, M_RTABLE); 560 arg->new_rtm = rtm; 561 } 562 } 563 return error; 564 } 565 566 static void route_output_add_callback(int, int, struct rt_addrinfo *, 567 struct rtentry *, void *); 568 static void route_output_delete_callback(int, int, struct rt_addrinfo *, 569 struct rtentry *, void *); 570 static int route_output_get_callback(int, struct rt_addrinfo *, 571 struct rtentry *, void *, int); 572 static int route_output_change_callback(int, struct rt_addrinfo *, 573 struct rtentry *, void *, int); 574 static int route_output_lock_callback(int, struct rt_addrinfo *, 575 struct rtentry *, void *, int); 576 577 /*ARGSUSED*/ 578 static int 579 route_output(struct mbuf *m, struct socket *so, ...) 580 { 581 struct rtm_arg arg; 582 struct rt_msghdr *rtm = NULL; 583 struct rawcb *rp = NULL; 584 struct pr_output_info *oi; 585 struct rt_addrinfo rtinfo; 586 sa_family_t family; 587 int len, error = 0; 588 __va_list ap; 589 590 M_ASSERTPKTHDR(m); 591 592 __va_start(ap, so); 593 oi = __va_arg(ap, struct pr_output_info *); 594 __va_end(ap); 595 596 family = familyof(NULL); 597 598 #define gotoerr(e) { error = e; goto flush;} 599 600 if (m == NULL || 601 (m->m_len < sizeof(long) && 602 (m = m_pullup(m, sizeof(long))) == NULL)) 603 return (ENOBUFS); 604 len = m->m_pkthdr.len; 605 if (len < sizeof(struct rt_msghdr) || 606 len != mtod(m, struct rt_msghdr *)->rtm_msglen) 607 gotoerr(EINVAL); 608 609 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 610 if (rtm == NULL) 611 gotoerr(ENOBUFS); 612 613 m_copydata(m, 0, len, (caddr_t)rtm); 614 if (rtm->rtm_version != RTM_VERSION) 615 gotoerr(EPROTONOSUPPORT); 616 617 rtm->rtm_pid = oi->p_pid; 618 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 619 rtinfo.rti_addrs = rtm->rtm_addrs; 620 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) 621 gotoerr(EINVAL); 622 623 rtinfo.rti_flags = rtm->rtm_flags; 624 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX || 625 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX)) 626 gotoerr(EINVAL); 627 628 family = familyof(rtinfo.rti_dst); 629 630 /* 631 * Verify that the caller has the appropriate privilege; RTM_GET 632 * is the only operation the non-superuser is allowed. 633 */ 634 if (rtm->rtm_type != RTM_GET && 635 priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0) 636 gotoerr(EPERM); 637 638 if (rtinfo.rti_genmask != NULL) { 639 error = rtmask_add_global(rtinfo.rti_genmask, 640 rtm->rtm_type != RTM_GET ? 641 RTREQ_PRIO_HIGH : RTREQ_PRIO_NORM); 642 if (error) 643 goto flush; 644 } 645 646 switch (rtm->rtm_type) { 647 case RTM_ADD: 648 if (rtinfo.rti_gateway == NULL) { 649 error = EINVAL; 650 } else { 651 error = rtrequest1_global(RTM_ADD, &rtinfo, 652 route_output_add_callback, rtm, RTREQ_PRIO_HIGH); 653 } 654 break; 655 case RTM_DELETE: 656 /* 657 * Backing rtm (bak_rtm) could _not_ be freed during 658 * rtrequest1_global or rtsearch_global, even if the 659 * callback reallocates the rtm due to its size changes, 660 * since rtinfo points to the backing rtm's memory area. 661 * After rtrequest1_global or rtsearch_global returns, 662 * it is safe to free the backing rtm, since rtinfo will 663 * not be used anymore. 664 * 665 * new_rtm will be used to save the new rtm allocated 666 * by rtrequest1_global or rtsearch_global. 667 */ 668 arg.bak_rtm = rtm; 669 arg.new_rtm = rtm; 670 error = rtrequest1_global(RTM_DELETE, &rtinfo, 671 route_output_delete_callback, &arg, RTREQ_PRIO_HIGH); 672 rtm = arg.new_rtm; 673 if (rtm != arg.bak_rtm) 674 kfree(arg.bak_rtm, M_RTABLE); 675 break; 676 case RTM_GET: 677 /* See the comment in RTM_DELETE */ 678 arg.bak_rtm = rtm; 679 arg.new_rtm = rtm; 680 error = rtsearch_global(RTM_GET, &rtinfo, 681 route_output_get_callback, &arg, RTS_NOEXACTMATCH, 682 RTREQ_PRIO_NORM); 683 rtm = arg.new_rtm; 684 if (rtm != arg.bak_rtm) 685 kfree(arg.bak_rtm, M_RTABLE); 686 break; 687 case RTM_CHANGE: 688 error = rtsearch_global(RTM_CHANGE, &rtinfo, 689 route_output_change_callback, rtm, RTS_EXACTMATCH, 690 RTREQ_PRIO_HIGH); 691 break; 692 case RTM_LOCK: 693 error = rtsearch_global(RTM_LOCK, &rtinfo, 694 route_output_lock_callback, rtm, RTS_EXACTMATCH, 695 RTREQ_PRIO_HIGH); 696 break; 697 default: 698 error = EOPNOTSUPP; 699 break; 700 } 701 flush: 702 if (rtm != NULL) { 703 if (error != 0) 704 rtm->rtm_errno = error; 705 else 706 rtm->rtm_flags |= RTF_DONE; 707 } 708 709 /* 710 * Check to see if we don't want our own messages. 711 */ 712 if (!(so->so_options & SO_USELOOPBACK)) { 713 if (route_cb.any_count <= 1) { 714 if (rtm != NULL) 715 kfree(rtm, M_RTABLE); 716 m_freem(m); 717 return (error); 718 } 719 /* There is another listener, so construct message */ 720 rp = sotorawcb(so); 721 } 722 if (rtm != NULL) { 723 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 724 if (m->m_pkthdr.len < rtm->rtm_msglen) { 725 m_freem(m); 726 m = NULL; 727 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 728 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 729 kfree(rtm, M_RTABLE); 730 } 731 if (m != NULL) 732 rts_input_skip(m, family, rp); 733 return (error); 734 } 735 736 static void 737 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 738 struct rtentry *rt, void *arg) 739 { 740 struct rt_msghdr *rtm = arg; 741 742 if (error == 0 && rt != NULL) { 743 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 744 &rt->rt_rmx); 745 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 746 rt->rt_rmx.rmx_locks |= 747 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 748 if (rtinfo->rti_genmask != NULL) { 749 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask); 750 if (rt->rt_genmask == NULL) { 751 /* 752 * This should not happen, since we 753 * have already installed genmask 754 * on each CPU before we reach here. 755 */ 756 panic("genmask is gone!?"); 757 } 758 } else { 759 rt->rt_genmask = NULL; 760 } 761 rtm->rtm_index = rt->rt_ifp->if_index; 762 } 763 } 764 765 static void 766 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo, 767 struct rtentry *rt, void *arg) 768 { 769 if (error == 0 && rt) { 770 ++rt->rt_refcnt; 771 if (fillrtmsg(arg, rt, rtinfo) != 0) { 772 error = ENOBUFS; 773 /* XXX no way to return the error */ 774 } 775 --rt->rt_refcnt; 776 } 777 if (rt && rt->rt_refcnt == 0) { 778 ++rt->rt_refcnt; 779 rtfree(rt); 780 } 781 } 782 783 static int 784 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo, 785 struct rtentry *rt, void *arg, int found_cnt) 786 { 787 int error, found = 0; 788 789 if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0) 790 found = 1; 791 792 error = fillrtmsg(arg, rt, rtinfo); 793 if (!error && found) { 794 /* Got the exact match, we could return now! */ 795 error = EJUSTRETURN; 796 } 797 return error; 798 } 799 800 static int 801 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo, 802 struct rtentry *rt, void *arg, int found_cnt) 803 { 804 struct rt_msghdr *rtm = arg; 805 struct ifaddr *ifa; 806 int error = 0; 807 808 /* 809 * new gateway could require new ifaddr, ifp; 810 * flags may also be different; ifp may be specified 811 * by ll sockaddr when protocol address is ambiguous 812 */ 813 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) || 814 rtinfo->rti_ifpaddr != NULL || 815 (rtinfo->rti_ifaaddr != NULL && 816 !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) { 817 error = rt_getifa(rtinfo); 818 if (error != 0) 819 goto done; 820 } 821 if (rtinfo->rti_gateway != NULL) { 822 /* 823 * We only need to generate rtmsg upon the 824 * first route to be changed. 825 */ 826 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway, 827 found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT); 828 if (error != 0) 829 goto done; 830 } 831 if ((ifa = rtinfo->rti_ifa) != NULL) { 832 struct ifaddr *oifa = rt->rt_ifa; 833 834 if (oifa != ifa) { 835 if (oifa && oifa->ifa_rtrequest) 836 oifa->ifa_rtrequest(RTM_DELETE, rt); 837 IFAFREE(rt->rt_ifa); 838 IFAREF(ifa); 839 rt->rt_ifa = ifa; 840 rt->rt_ifp = rtinfo->rti_ifp; 841 } 842 } 843 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx); 844 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 845 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt); 846 if (rtinfo->rti_genmask != NULL) { 847 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask); 848 if (rt->rt_genmask == NULL) { 849 /* 850 * This should not happen, since we 851 * have already installed genmask 852 * on each CPU before we reach here. 853 */ 854 panic("genmask is gone!?"); 855 } 856 } 857 rtm->rtm_index = rt->rt_ifp->if_index; 858 done: 859 return error; 860 } 861 862 static int 863 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo, 864 struct rtentry *rt, void *arg, 865 int found_cnt __unused) 866 { 867 struct rt_msghdr *rtm = arg; 868 869 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 870 rt->rt_rmx.rmx_locks |= 871 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 872 return 0; 873 } 874 875 static void 876 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out) 877 { 878 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt; 879 setmetric(RTV_RPIPE, rmx_recvpipe); 880 setmetric(RTV_SPIPE, rmx_sendpipe); 881 setmetric(RTV_SSTHRESH, rmx_ssthresh); 882 setmetric(RTV_RTT, rmx_rtt); 883 setmetric(RTV_RTTVAR, rmx_rttvar); 884 setmetric(RTV_HOPCOUNT, rmx_hopcount); 885 setmetric(RTV_MTU, rmx_mtu); 886 setmetric(RTV_EXPIRE, rmx_expire); 887 setmetric(RTV_MSL, rmx_msl); 888 setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs); 889 setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs); 890 #undef setmetric 891 } 892 893 /* 894 * Extract the addresses of the passed sockaddrs. 895 * Do a little sanity checking so as to avoid bad memory references. 896 * This data is derived straight from userland. 897 */ 898 static int 899 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo) 900 { 901 struct sockaddr *sa; 902 int i; 903 904 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 905 if ((rtinfo->rti_addrs & (1 << i)) == 0) 906 continue; 907 sa = (struct sockaddr *)cp; 908 /* 909 * It won't fit. 910 */ 911 if ((cp + sa->sa_len) > cplim) { 912 return (EINVAL); 913 } 914 915 /* 916 * There are no more... Quit now. 917 * If there are more bits, they are in error. 918 * I've seen this. route(1) can evidently generate these. 919 * This causes kernel to core dump. 920 * For compatibility, if we see this, point to a safe address. 921 */ 922 if (sa->sa_len == 0) { 923 static struct sockaddr sa_zero = { 924 sizeof sa_zero, AF_INET, 925 }; 926 927 rtinfo->rti_info[i] = &sa_zero; 928 kprintf("rtsock: received more addr bits than sockaddrs.\n"); 929 return (0); /* should be EINVAL but for compat */ 930 } 931 932 /* Accept the sockaddr. */ 933 rtinfo->rti_info[i] = sa; 934 cp += RT_ROUNDUP(sa->sa_len); 935 } 936 return (0); 937 } 938 939 static int 940 rt_msghdrsize(int type) 941 { 942 switch (type) { 943 case RTM_DELADDR: 944 case RTM_NEWADDR: 945 return sizeof(struct ifa_msghdr); 946 case RTM_DELMADDR: 947 case RTM_NEWMADDR: 948 return sizeof(struct ifma_msghdr); 949 case RTM_IFINFO: 950 return sizeof(struct if_msghdr); 951 case RTM_IFANNOUNCE: 952 case RTM_IEEE80211: 953 return sizeof(struct if_announcemsghdr); 954 default: 955 return sizeof(struct rt_msghdr); 956 } 957 } 958 959 static int 960 rt_msgsize(int type, const struct rt_addrinfo *rtinfo) 961 { 962 int len, i; 963 964 len = rt_msghdrsize(type); 965 for (i = 0; i < RTAX_MAX; i++) { 966 if (rtinfo->rti_info[i] != NULL) 967 len += RT_ROUNDUP(rtinfo->rti_info[i]->sa_len); 968 } 969 len = ALIGN(len); 970 return len; 971 } 972 973 /* 974 * Build a routing message in a buffer. 975 * Copy the addresses in the rtinfo->rti_info[] sockaddr array 976 * to the end of the buffer after the message header. 977 * 978 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. 979 * This side-effect can be avoided if we reorder the addrs bitmask field in all 980 * the route messages to line up so we can set it here instead of back in the 981 * calling routine. 982 */ 983 static void 984 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen) 985 { 986 struct rt_msghdr *rtm; 987 char *cp; 988 int dlen, i; 989 990 rtm = (struct rt_msghdr *) buf; 991 rtm->rtm_version = RTM_VERSION; 992 rtm->rtm_type = type; 993 rtm->rtm_msglen = msglen; 994 995 cp = (char *)buf + rt_msghdrsize(type); 996 rtinfo->rti_addrs = 0; 997 for (i = 0; i < RTAX_MAX; i++) { 998 struct sockaddr *sa; 999 1000 if ((sa = rtinfo->rti_info[i]) == NULL) 1001 continue; 1002 rtinfo->rti_addrs |= (1 << i); 1003 dlen = RT_ROUNDUP(sa->sa_len); 1004 bcopy(sa, cp, dlen); 1005 cp += dlen; 1006 } 1007 } 1008 1009 /* 1010 * Build a routing message in a mbuf chain. 1011 * Copy the addresses in the rtinfo->rti_info[] sockaddr array 1012 * to the end of the mbuf after the message header. 1013 * 1014 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. 1015 * This side-effect can be avoided if we reorder the addrs bitmask field in all 1016 * the route messages to line up so we can set it here instead of back in the 1017 * calling routine. 1018 */ 1019 static struct mbuf * 1020 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo) 1021 { 1022 struct mbuf *m; 1023 struct rt_msghdr *rtm; 1024 int hlen, len; 1025 int i; 1026 1027 hlen = rt_msghdrsize(type); 1028 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen)); 1029 1030 m = m_getl(hlen, M_NOWAIT, MT_DATA, M_PKTHDR, NULL); 1031 if (m == NULL) 1032 return (NULL); 1033 mbuftrackid(m, 32); 1034 m->m_pkthdr.len = m->m_len = hlen; 1035 m->m_pkthdr.rcvif = NULL; 1036 rtinfo->rti_addrs = 0; 1037 len = hlen; 1038 for (i = 0; i < RTAX_MAX; i++) { 1039 struct sockaddr *sa; 1040 int dlen; 1041 1042 if ((sa = rtinfo->rti_info[i]) == NULL) 1043 continue; 1044 rtinfo->rti_addrs |= (1 << i); 1045 dlen = RT_ROUNDUP(sa->sa_len); 1046 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */ 1047 len += dlen; 1048 } 1049 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */ 1050 m_freem(m); 1051 return (NULL); 1052 } 1053 rtm = mtod(m, struct rt_msghdr *); 1054 bzero(rtm, hlen); 1055 rtm->rtm_msglen = len; 1056 rtm->rtm_version = RTM_VERSION; 1057 rtm->rtm_type = type; 1058 return (m); 1059 } 1060 1061 /* 1062 * This routine is called to generate a message from the routing 1063 * socket indicating that a redirect has occurred, a routing lookup 1064 * has failed, or that a protocol has detected timeouts to a particular 1065 * destination. 1066 */ 1067 void 1068 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1069 { 1070 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; 1071 struct rt_msghdr *rtm; 1072 struct mbuf *m; 1073 1074 if (route_cb.any_count == 0) 1075 return; 1076 m = rt_msg_mbuf(type, rtinfo); 1077 if (m == NULL) 1078 return; 1079 rtm = mtod(m, struct rt_msghdr *); 1080 rtm->rtm_flags = RTF_DONE | flags; 1081 rtm->rtm_errno = error; 1082 rtm->rtm_addrs = rtinfo->rti_addrs; 1083 rts_input(m, familyof(dst)); 1084 } 1085 1086 void 1087 rt_dstmsg(int type, struct sockaddr *dst, int error) 1088 { 1089 struct rt_msghdr *rtm; 1090 struct rt_addrinfo addrs; 1091 struct mbuf *m; 1092 1093 if (route_cb.any_count == 0) 1094 return; 1095 bzero(&addrs, sizeof(struct rt_addrinfo)); 1096 addrs.rti_info[RTAX_DST] = dst; 1097 m = rt_msg_mbuf(type, &addrs); 1098 if (m == NULL) 1099 return; 1100 rtm = mtod(m, struct rt_msghdr *); 1101 rtm->rtm_flags = RTF_DONE; 1102 rtm->rtm_errno = error; 1103 rtm->rtm_addrs = addrs.rti_addrs; 1104 rts_input(m, familyof(dst)); 1105 } 1106 1107 /* 1108 * This routine is called to generate a message from the routing 1109 * socket indicating that the status of a network interface has changed. 1110 */ 1111 void 1112 rt_ifmsg(struct ifnet *ifp) 1113 { 1114 struct if_msghdr *ifm; 1115 struct mbuf *m; 1116 struct rt_addrinfo rtinfo; 1117 1118 if (route_cb.any_count == 0) 1119 return; 1120 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1121 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo); 1122 if (m == NULL) 1123 return; 1124 ifm = mtod(m, struct if_msghdr *); 1125 ifm->ifm_index = ifp->if_index; 1126 ifm->ifm_flags = ifp->if_flags; 1127 ifm->ifm_data = ifp->if_data; 1128 ifm->ifm_addrs = 0; 1129 rts_input(m, 0); 1130 } 1131 1132 static void 1133 rt_ifamsg(int cmd, struct ifaddr *ifa) 1134 { 1135 struct ifa_msghdr *ifam; 1136 struct rt_addrinfo rtinfo; 1137 struct mbuf *m; 1138 struct ifnet *ifp = ifa->ifa_ifp; 1139 1140 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1141 rtinfo.rti_ifaaddr = ifa->ifa_addr; 1142 rtinfo.rti_ifpaddr = 1143 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1144 rtinfo.rti_netmask = ifa->ifa_netmask; 1145 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; 1146 1147 m = rt_msg_mbuf(cmd, &rtinfo); 1148 if (m == NULL) 1149 return; 1150 1151 ifam = mtod(m, struct ifa_msghdr *); 1152 ifam->ifam_index = ifp->if_index; 1153 ifam->ifam_metric = ifa->ifa_metric; 1154 ifam->ifam_flags = ifa->ifa_flags; 1155 ifam->ifam_addrs = rtinfo.rti_addrs; 1156 1157 rts_input(m, familyof(ifa->ifa_addr)); 1158 } 1159 1160 void 1161 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error) 1162 { 1163 struct rt_msghdr *rtm; 1164 struct rt_addrinfo rtinfo; 1165 struct mbuf *m; 1166 struct sockaddr *dst; 1167 1168 if (rt == NULL) 1169 return; 1170 1171 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1172 rtinfo.rti_dst = dst = rt_key(rt); 1173 rtinfo.rti_gateway = rt->rt_gateway; 1174 rtinfo.rti_netmask = rt_mask(rt); 1175 if (ifp != NULL) { 1176 rtinfo.rti_ifpaddr = 1177 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1178 } 1179 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr; 1180 1181 m = rt_msg_mbuf(cmd, &rtinfo); 1182 if (m == NULL) 1183 return; 1184 1185 rtm = mtod(m, struct rt_msghdr *); 1186 if (ifp != NULL) 1187 rtm->rtm_index = ifp->if_index; 1188 rtm->rtm_flags |= rt->rt_flags; 1189 rtm->rtm_errno = error; 1190 rtm->rtm_addrs = rtinfo.rti_addrs; 1191 1192 rts_input(m, familyof(dst)); 1193 } 1194 1195 /* 1196 * This is called to generate messages from the routing socket 1197 * indicating a network interface has had addresses associated with it. 1198 * if we ever reverse the logic and replace messages TO the routing 1199 * socket indicate a request to configure interfaces, then it will 1200 * be unnecessary as the routing socket will automatically generate 1201 * copies of it. 1202 */ 1203 void 1204 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1205 { 1206 if (route_cb.any_count == 0) 1207 return; 1208 1209 if (cmd == RTM_ADD) { 1210 rt_ifamsg(RTM_NEWADDR, ifa); 1211 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error); 1212 } else { 1213 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd)); 1214 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error); 1215 rt_ifamsg(RTM_DELADDR, ifa); 1216 } 1217 } 1218 1219 /* 1220 * This is the analogue to the rt_newaddrmsg which performs the same 1221 * function but for multicast group memberhips. This is easier since 1222 * there is no route state to worry about. 1223 */ 1224 void 1225 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1226 { 1227 struct rt_addrinfo rtinfo; 1228 struct mbuf *m = NULL; 1229 struct ifnet *ifp = ifma->ifma_ifp; 1230 struct ifma_msghdr *ifmam; 1231 1232 if (route_cb.any_count == 0) 1233 return; 1234 1235 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1236 rtinfo.rti_ifaaddr = ifma->ifma_addr; 1237 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) { 1238 rtinfo.rti_ifpaddr = 1239 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1240 } 1241 /* 1242 * If a link-layer address is present, present it as a ``gateway'' 1243 * (similarly to how ARP entries, e.g., are presented). 1244 */ 1245 rtinfo.rti_gateway = ifma->ifma_lladdr; 1246 1247 m = rt_msg_mbuf(cmd, &rtinfo); 1248 if (m == NULL) 1249 return; 1250 1251 ifmam = mtod(m, struct ifma_msghdr *); 1252 ifmam->ifmam_index = ifp->if_index; 1253 ifmam->ifmam_addrs = rtinfo.rti_addrs; 1254 1255 rts_input(m, familyof(ifma->ifma_addr)); 1256 } 1257 1258 static struct mbuf * 1259 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1260 struct rt_addrinfo *info) 1261 { 1262 struct if_announcemsghdr *ifan; 1263 struct mbuf *m; 1264 1265 if (route_cb.any_count == 0) 1266 return NULL; 1267 1268 bzero(info, sizeof(*info)); 1269 m = rt_msg_mbuf(type, info); 1270 if (m == NULL) 1271 return NULL; 1272 1273 ifan = mtod(m, struct if_announcemsghdr *); 1274 ifan->ifan_index = ifp->if_index; 1275 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name); 1276 ifan->ifan_what = what; 1277 return m; 1278 } 1279 1280 /* 1281 * This is called to generate routing socket messages indicating 1282 * IEEE80211 wireless events. 1283 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1284 */ 1285 void 1286 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1287 { 1288 struct rt_addrinfo info; 1289 struct mbuf *m; 1290 1291 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1292 if (m == NULL) 1293 return; 1294 1295 /* 1296 * Append the ieee80211 data. Try to stick it in the 1297 * mbuf containing the ifannounce msg; otherwise allocate 1298 * a new mbuf and append. 1299 * 1300 * NB: we assume m is a single mbuf. 1301 */ 1302 if (data_len > M_TRAILINGSPACE(m)) { 1303 /* XXX use m_getb(data_len, M_NOWAIT, MT_DATA, 0); */ 1304 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1305 if (n == NULL) { 1306 m_freem(m); 1307 return; 1308 } 1309 KKASSERT(data_len <= M_TRAILINGSPACE(n)); 1310 bcopy(data, mtod(n, void *), data_len); 1311 n->m_len = data_len; 1312 m->m_next = n; 1313 } else if (data_len > 0) { 1314 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1315 m->m_len += data_len; 1316 } 1317 mbuftrackid(m, 33); 1318 if (m->m_flags & M_PKTHDR) 1319 m->m_pkthdr.len += data_len; 1320 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1321 rts_input(m, 0); 1322 } 1323 1324 /* 1325 * This is called to generate routing socket messages indicating 1326 * network interface arrival and departure. 1327 */ 1328 void 1329 rt_ifannouncemsg(struct ifnet *ifp, int what) 1330 { 1331 struct rt_addrinfo addrinfo; 1332 struct mbuf *m; 1333 1334 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo); 1335 if (m != NULL) 1336 rts_input(m, 0); 1337 } 1338 1339 static int 1340 resizewalkarg(struct walkarg *w, int len) 1341 { 1342 void *newptr; 1343 1344 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); 1345 if (newptr == NULL) 1346 return (ENOMEM); 1347 if (w->w_tmem != NULL) 1348 kfree(w->w_tmem, M_RTABLE); 1349 w->w_tmem = newptr; 1350 w->w_tmemsize = len; 1351 return (0); 1352 } 1353 1354 static void 1355 ifnet_compute_stats(struct ifnet *ifp) 1356 { 1357 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets); 1358 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors); 1359 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets); 1360 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions); 1361 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes); 1362 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes); 1363 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts); 1364 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts); 1365 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops); 1366 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto); 1367 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops); 1368 } 1369 1370 static int 1371 sysctl_iflist(int af, struct walkarg *w) 1372 { 1373 struct ifnet *ifp; 1374 struct rt_addrinfo rtinfo; 1375 int msglen, error; 1376 1377 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1378 1379 ifnet_lock(); 1380 TAILQ_FOREACH(ifp, &ifnetlist, if_link) { 1381 struct ifaddr_container *ifac, *ifac_mark; 1382 struct ifaddr_marker mark; 1383 struct ifaddrhead *head; 1384 struct ifaddr *ifa; 1385 1386 if (w->w_arg && w->w_arg != ifp->if_index) 1387 continue; 1388 head = &ifp->if_addrheads[mycpuid]; 1389 /* 1390 * There is no need to reference the first ifaddr 1391 * even if the following resizewalkarg() blocks, 1392 * since the first ifaddr will not be destroyed 1393 * when the ifnet lock is held. 1394 */ 1395 ifac = TAILQ_FIRST(head); 1396 ifa = ifac->ifa; 1397 rtinfo.rti_ifpaddr = ifa->ifa_addr; 1398 msglen = rt_msgsize(RTM_IFINFO, &rtinfo); 1399 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) { 1400 ifnet_unlock(); 1401 return (ENOMEM); 1402 } 1403 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen); 1404 rtinfo.rti_ifpaddr = NULL; 1405 if (w->w_req != NULL && w->w_tmem != NULL) { 1406 struct if_msghdr *ifm = w->w_tmem; 1407 1408 ifm->ifm_index = ifp->if_index; 1409 ifm->ifm_flags = ifp->if_flags; 1410 ifnet_compute_stats(ifp); 1411 ifm->ifm_data = ifp->if_data; 1412 ifm->ifm_addrs = rtinfo.rti_addrs; 1413 error = SYSCTL_OUT(w->w_req, ifm, msglen); 1414 if (error) { 1415 ifnet_unlock(); 1416 return (error); 1417 } 1418 } 1419 /* 1420 * Add a marker, since SYSCTL_OUT() could block and during 1421 * that period the list could be changed. 1422 */ 1423 ifa_marker_init(&mark, ifp); 1424 ifac_mark = &mark.ifac; 1425 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link); 1426 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) { 1427 TAILQ_REMOVE(head, ifac_mark, ifa_link); 1428 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link); 1429 1430 ifa = ifac->ifa; 1431 1432 /* Ignore marker */ 1433 if (ifa->ifa_addr->sa_family == AF_UNSPEC) 1434 continue; 1435 1436 if (af && af != ifa->ifa_addr->sa_family) 1437 continue; 1438 if (curproc->p_ucred->cr_prison && 1439 prison_if(curproc->p_ucred, ifa->ifa_addr)) 1440 continue; 1441 rtinfo.rti_ifaaddr = ifa->ifa_addr; 1442 rtinfo.rti_netmask = ifa->ifa_netmask; 1443 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; 1444 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo); 1445 /* 1446 * Keep a reference on this ifaddr, so that it will 1447 * not be destroyed if the following resizewalkarg() 1448 * blocks. 1449 */ 1450 IFAREF(ifa); 1451 if (w->w_tmemsize < msglen && 1452 resizewalkarg(w, msglen) != 0) { 1453 IFAFREE(ifa); 1454 TAILQ_REMOVE(head, ifac_mark, ifa_link); 1455 ifnet_unlock(); 1456 return (ENOMEM); 1457 } 1458 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen); 1459 if (w->w_req != NULL) { 1460 struct ifa_msghdr *ifam = w->w_tmem; 1461 1462 ifam->ifam_index = ifa->ifa_ifp->if_index; 1463 ifam->ifam_flags = ifa->ifa_flags; 1464 ifam->ifam_metric = ifa->ifa_metric; 1465 ifam->ifam_addrs = rtinfo.rti_addrs; 1466 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen); 1467 if (error) { 1468 IFAFREE(ifa); 1469 TAILQ_REMOVE(head, ifac_mark, ifa_link); 1470 ifnet_unlock(); 1471 return (error); 1472 } 1473 } 1474 IFAFREE(ifa); 1475 } 1476 TAILQ_REMOVE(head, ifac_mark, ifa_link); 1477 rtinfo.rti_netmask = NULL; 1478 rtinfo.rti_ifaaddr = NULL; 1479 rtinfo.rti_bcastaddr = NULL; 1480 } 1481 ifnet_unlock(); 1482 return (0); 1483 } 1484 1485 static int 1486 rttable_walkarg_create(struct rttable_walkarg *w, int op, int arg) 1487 { 1488 struct rt_addrinfo rtinfo; 1489 struct sockaddr_storage ss; 1490 int i, msglen; 1491 1492 memset(w, 0, sizeof(*w)); 1493 w->w_op = op; 1494 w->w_arg = arg; 1495 1496 memset(&ss, 0, sizeof(ss)); 1497 ss.ss_len = sizeof(ss); 1498 1499 memset(&rtinfo, 0, sizeof(rtinfo)); 1500 for (i = 0; i < RTAX_MAX; ++i) 1501 rtinfo.rti_info[i] = (struct sockaddr *)&ss; 1502 msglen = rt_msgsize(RTM_GET, &rtinfo); 1503 1504 w->w_bufsz = msglen * RTTABLE_DUMP_MSGCNT_MAX; 1505 w->w_buf = kmalloc(w->w_bufsz, M_TEMP, M_WAITOK | M_NULLOK); 1506 if (w->w_buf == NULL) 1507 return ENOMEM; 1508 return 0; 1509 } 1510 1511 static void 1512 rttable_walkarg_destroy(struct rttable_walkarg *w) 1513 { 1514 kfree(w->w_buf, M_TEMP); 1515 } 1516 1517 static void 1518 rttable_entry_rtinfo(struct rt_addrinfo *rtinfo, struct radix_node *rn) 1519 { 1520 struct rtentry *rt = (struct rtentry *)rn; 1521 1522 bzero(rtinfo, sizeof(*rtinfo)); 1523 rtinfo->rti_dst = rt_key(rt); 1524 rtinfo->rti_gateway = rt->rt_gateway; 1525 rtinfo->rti_netmask = rt_mask(rt); 1526 rtinfo->rti_genmask = rt->rt_genmask; 1527 if (rt->rt_ifp != NULL) { 1528 rtinfo->rti_ifpaddr = 1529 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr; 1530 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr; 1531 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1532 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; 1533 } 1534 } 1535 1536 static int 1537 rttable_walk_entry(struct radix_node *rn, void *xw) 1538 { 1539 struct rttable_walkarg *w = xw; 1540 struct rtentry *rt = (struct rtentry *)rn; 1541 struct rt_addrinfo rtinfo; 1542 struct rt_msghdr *rtm; 1543 boolean_t save = FALSE; 1544 int msglen, w_bufleft; 1545 void *ptr; 1546 1547 rttable_entry_rtinfo(&rtinfo, rn); 1548 msglen = rt_msgsize(RTM_GET, &rtinfo); 1549 1550 w_bufleft = w->w_bufsz - w->w_buflen; 1551 1552 if (rn->rn_dupedkey != NULL) { 1553 struct radix_node *rn1 = rn; 1554 int total_msglen = msglen; 1555 1556 /* 1557 * Make sure that we have enough space left for all 1558 * dupedkeys, since rn_walktree_at always starts 1559 * from the first dupedkey. 1560 */ 1561 while ((rn1 = rn1->rn_dupedkey) != NULL) { 1562 struct rt_addrinfo rtinfo1; 1563 int msglen1; 1564 1565 if (rn1->rn_flags & RNF_ROOT) 1566 continue; 1567 1568 rttable_entry_rtinfo(&rtinfo1, rn1); 1569 msglen1 = rt_msgsize(RTM_GET, &rtinfo1); 1570 total_msglen += msglen1; 1571 } 1572 1573 if (total_msglen > w_bufleft) { 1574 if (total_msglen > w->w_bufsz) { 1575 static int logged = 0; 1576 1577 if (!logged) { 1578 kprintf("buffer is too small for " 1579 "all dupedkeys, increase " 1580 "RTTABLE_DUMP_MSGCNT_MAX\n"); 1581 logged = 1; 1582 } 1583 return ENOMEM; 1584 } 1585 save = TRUE; 1586 } 1587 } else if (msglen > w_bufleft) { 1588 save = TRUE; 1589 } 1590 1591 if (save) { 1592 /* 1593 * Not enough buffer left; remember the position 1594 * to start from upon next round. 1595 */ 1596 KASSERT(msglen <= w->w_bufsz, ("msg too long %d", msglen)); 1597 1598 KASSERT(rtinfo.rti_dst->sa_len <= sizeof(w->w_key0), 1599 ("key too long %d", rtinfo.rti_dst->sa_len)); 1600 memset(&w->w_key0, 0, sizeof(w->w_key0)); 1601 memcpy(&w->w_key0, rtinfo.rti_dst, rtinfo.rti_dst->sa_len); 1602 w->w_key = (const char *)&w->w_key0; 1603 1604 if (rtinfo.rti_netmask != NULL) { 1605 KASSERT( 1606 rtinfo.rti_netmask->sa_len <= sizeof(w->w_mask0), 1607 ("mask too long %d", rtinfo.rti_netmask->sa_len)); 1608 memset(&w->w_mask0, 0, sizeof(w->w_mask0)); 1609 memcpy(&w->w_mask0, rtinfo.rti_netmask, 1610 rtinfo.rti_netmask->sa_len); 1611 w->w_mask = (const char *)&w->w_mask0; 1612 } else { 1613 w->w_mask = NULL; 1614 } 1615 return EJUSTRETURN; 1616 } 1617 1618 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1619 return 0; 1620 1621 ptr = ((uint8_t *)w->w_buf) + w->w_buflen; 1622 rt_msg_buffer(RTM_GET, &rtinfo, ptr, msglen); 1623 1624 rtm = (struct rt_msghdr *)ptr; 1625 rtm->rtm_flags = rt->rt_flags; 1626 rtm->rtm_use = rt->rt_use; 1627 rtm->rtm_rmx = rt->rt_rmx; 1628 rtm->rtm_index = rt->rt_ifp->if_index; 1629 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1630 rtm->rtm_addrs = rtinfo.rti_addrs; 1631 1632 w->w_buflen += msglen; 1633 1634 return 0; 1635 } 1636 1637 static void 1638 rttable_walk_dispatch(netmsg_t msg) 1639 { 1640 struct netmsg_rttable_walk *nmsg = (struct netmsg_rttable_walk *)msg; 1641 struct radix_node_head *rnh = rt_tables[mycpuid][nmsg->af]; 1642 struct rttable_walkarg *w = nmsg->w; 1643 int error; 1644 1645 error = rnh->rnh_walktree_at(rnh, w->w_key, w->w_mask, 1646 rttable_walk_entry, w); 1647 lwkt_replymsg(&nmsg->base.lmsg, error); 1648 } 1649 1650 static int 1651 sysctl_rttable(int af, struct sysctl_req *req, int op, int arg) 1652 { 1653 struct rttable_walkarg w; 1654 int error, i; 1655 1656 error = rttable_walkarg_create(&w, op, arg); 1657 if (error) 1658 return error; 1659 1660 error = EINVAL; 1661 for (i = 1; i <= AF_MAX; i++) { 1662 if (rt_tables[mycpuid][i] != NULL && (af == 0 || af == i)) { 1663 w.w_key = NULL; 1664 w.w_mask = NULL; 1665 for (;;) { 1666 struct netmsg_rttable_walk nmsg; 1667 1668 netmsg_init(&nmsg.base, NULL, 1669 &curthread->td_msgport, 0, 1670 rttable_walk_dispatch); 1671 nmsg.af = i; 1672 nmsg.w = &w; 1673 1674 w.w_buflen = 0; 1675 1676 error = lwkt_domsg(netisr_cpuport(mycpuid), 1677 &nmsg.base.lmsg, 0); 1678 if (error && error != EJUSTRETURN) 1679 goto done; 1680 1681 if (req != NULL && w.w_buflen > 0) { 1682 int error1; 1683 1684 error1 = SYSCTL_OUT(req, w.w_buf, 1685 w.w_buflen); 1686 if (error1) { 1687 error = error1; 1688 goto done; 1689 } 1690 } 1691 if (error == 0) /* done */ 1692 break; 1693 } 1694 } 1695 } 1696 done: 1697 rttable_walkarg_destroy(&w); 1698 return error; 1699 } 1700 1701 static int 1702 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1703 { 1704 int *name = (int *)arg1; 1705 u_int namelen = arg2; 1706 int error = EINVAL; 1707 int origcpu, cpu; 1708 u_char af; 1709 struct walkarg w; 1710 1711 name ++; 1712 namelen--; 1713 if (req->newptr) 1714 return (EPERM); 1715 if (namelen != 3 && namelen != 4) 1716 return (EINVAL); 1717 af = name[0]; 1718 bzero(&w, sizeof w); 1719 w.w_op = name[1]; 1720 w.w_arg = name[2]; 1721 w.w_req = req; 1722 1723 /* 1724 * Optional third argument specifies cpu, used primarily for 1725 * debugging the route table. 1726 */ 1727 if (namelen == 4) { 1728 if (name[3] < 0 || name[3] >= netisr_ncpus) 1729 return (EINVAL); 1730 cpu = name[3]; 1731 } else { 1732 /* 1733 * Target cpu is not specified, use cpu0 then, so that 1734 * the result set will be relatively stable. 1735 */ 1736 cpu = 0; 1737 } 1738 origcpu = mycpuid; 1739 lwkt_migratecpu(cpu); 1740 1741 switch (w.w_op) { 1742 case NET_RT_DUMP: 1743 case NET_RT_FLAGS: 1744 error = sysctl_rttable(af, w.w_req, w.w_op, w.w_arg); 1745 break; 1746 1747 case NET_RT_IFLIST: 1748 error = sysctl_iflist(af, &w); 1749 break; 1750 } 1751 if (w.w_tmem != NULL) 1752 kfree(w.w_tmem, M_RTABLE); 1753 1754 lwkt_migratecpu(origcpu); 1755 return (error); 1756 } 1757 1758 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1759 1760 /* 1761 * Definitions of protocols supported in the ROUTE domain. 1762 */ 1763 1764 static struct domain routedomain; /* or at least forward */ 1765 1766 static struct protosw routesw[] = { 1767 { 1768 .pr_type = SOCK_RAW, 1769 .pr_domain = &routedomain, 1770 .pr_protocol = 0, 1771 .pr_flags = PR_ATOMIC|PR_ADDR, 1772 .pr_input = NULL, 1773 .pr_output = route_output, 1774 .pr_ctlinput = raw_ctlinput, 1775 .pr_ctloutput = route_ctloutput, 1776 .pr_ctlport = cpu0_ctlport, 1777 1778 .pr_init = raw_init, 1779 .pr_usrreqs = &route_usrreqs 1780 } 1781 }; 1782 1783 static struct domain routedomain = { 1784 .dom_family = AF_ROUTE, 1785 .dom_name = "route", 1786 .dom_init = NULL, 1787 .dom_externalize = NULL, 1788 .dom_dispose = NULL, 1789 .dom_protosw = routesw, 1790 .dom_protoswNPROTOSW = &routesw[(sizeof routesw)/(sizeof routesw[0])], 1791 .dom_next = SLIST_ENTRY_INITIALIZER, 1792 .dom_rtattach = NULL, 1793 .dom_rtoffset = 0, 1794 .dom_maxrtkey = 0, 1795 .dom_ifattach = NULL, 1796 .dom_ifdetach = NULL 1797 }; 1798 1799 DOMAIN_SET(route); 1800 1801