1 /*- 2 * Copyright (c) 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 30 * $FreeBSD$ 31 */ 32 #include "opt_sctp.h" 33 #include "opt_mpath.h" 34 35 #include <sys/param.h> 36 #include <sys/domain.h> 37 #include <sys/kernel.h> 38 #include <sys/jail.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/priv.h> 42 #include <sys/proc.h> 43 #include <sys/protosw.h> 44 #include <sys/signalvar.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/systm.h> 49 #include <sys/vimage.h> 50 51 #include <net/if.h> 52 #include <net/netisr.h> 53 #include <net/raw_cb.h> 54 #include <net/route.h> 55 56 #include <netinet/in.h> 57 58 #ifdef SCTP 59 extern void sctp_addr_change(struct ifaddr *ifa, int cmd); 60 #endif /* SCTP */ 61 62 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 63 64 /* NB: these are not modified */ 65 static struct sockaddr route_src = { 2, PF_ROUTE, }; 66 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 67 68 static struct { 69 int ip_count; /* attached w/ AF_INET */ 70 int ip6_count; /* attached w/ AF_INET6 */ 71 int ipx_count; /* attached w/ AF_IPX */ 72 int any_count; /* total attached */ 73 } route_cb; 74 75 struct mtx rtsock_mtx; 76 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 77 78 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 79 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 80 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 81 82 static struct ifqueue rtsintrq; 83 84 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 85 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW, 86 &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length"); 87 88 struct walkarg { 89 int w_tmemsize; 90 int w_op, w_arg; 91 caddr_t w_tmem; 92 struct sysctl_req *w_req; 93 }; 94 95 static void rts_input(struct mbuf *m); 96 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 97 static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 98 caddr_t cp, struct walkarg *w); 99 static int rt_xaddrs(caddr_t cp, caddr_t cplim, 100 struct rt_addrinfo *rtinfo); 101 static int sysctl_dumpentry(struct radix_node *rn, void *vw); 102 static int sysctl_iflist(int af, struct walkarg *w); 103 static int sysctl_ifmalist(int af, struct walkarg *w); 104 static int route_output(struct mbuf *m, struct socket *so); 105 static void rt_setmetrics(u_long which, const struct rt_metrics *in, 106 struct rt_metrics_lite *out); 107 static void rt_getmetrics(const struct rt_metrics_lite *in, 108 struct rt_metrics *out); 109 static void rt_dispatch(struct mbuf *, const struct sockaddr *); 110 111 static void 112 rts_init(void) 113 { 114 int tmp; 115 116 rtsintrq.ifq_maxlen = 256; 117 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 118 rtsintrq.ifq_maxlen = tmp; 119 mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF); 120 netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, 0); 121 } 122 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0); 123 124 static void 125 rts_input(struct mbuf *m) 126 { 127 struct sockproto route_proto; 128 unsigned short *family; 129 struct m_tag *tag; 130 131 route_proto.sp_family = PF_ROUTE; 132 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 133 if (tag != NULL) { 134 family = (unsigned short *)(tag + 1); 135 route_proto.sp_protocol = *family; 136 m_tag_delete(m, tag); 137 } else 138 route_proto.sp_protocol = 0; 139 140 raw_input(m, &route_proto, &route_src); 141 } 142 143 /* 144 * It really doesn't make any sense at all for this code to share much 145 * with raw_usrreq.c, since its functionality is so restricted. XXX 146 */ 147 static void 148 rts_abort(struct socket *so) 149 { 150 151 raw_usrreqs.pru_abort(so); 152 } 153 154 static void 155 rts_close(struct socket *so) 156 { 157 158 raw_usrreqs.pru_close(so); 159 } 160 161 /* pru_accept is EOPNOTSUPP */ 162 163 static int 164 rts_attach(struct socket *so, int proto, struct thread *td) 165 { 166 struct rawcb *rp; 167 int s, error; 168 169 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 170 171 /* XXX */ 172 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 173 if (rp == NULL) 174 return ENOBUFS; 175 176 /* 177 * The splnet() is necessary to block protocols from sending 178 * error notifications (like RTM_REDIRECT or RTM_LOSING) while 179 * this PCB is extant but incompletely initialized. 180 * Probably we should try to do more of this work beforehand and 181 * eliminate the spl. 182 */ 183 s = splnet(); 184 so->so_pcb = (caddr_t)rp; 185 so->so_fibnum = td->td_proc->p_fibnum; 186 error = raw_attach(so, proto); 187 rp = sotorawcb(so); 188 if (error) { 189 splx(s); 190 so->so_pcb = NULL; 191 free(rp, M_PCB); 192 return error; 193 } 194 RTSOCK_LOCK(); 195 switch(rp->rcb_proto.sp_protocol) { 196 case AF_INET: 197 route_cb.ip_count++; 198 break; 199 case AF_INET6: 200 route_cb.ip6_count++; 201 break; 202 case AF_IPX: 203 route_cb.ipx_count++; 204 break; 205 } 206 route_cb.any_count++; 207 RTSOCK_UNLOCK(); 208 soisconnected(so); 209 so->so_options |= SO_USELOOPBACK; 210 splx(s); 211 return 0; 212 } 213 214 static int 215 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 216 { 217 218 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 219 } 220 221 static int 222 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 223 { 224 225 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 226 } 227 228 /* pru_connect2 is EOPNOTSUPP */ 229 /* pru_control is EOPNOTSUPP */ 230 231 static void 232 rts_detach(struct socket *so) 233 { 234 struct rawcb *rp = sotorawcb(so); 235 236 KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 237 238 RTSOCK_LOCK(); 239 switch(rp->rcb_proto.sp_protocol) { 240 case AF_INET: 241 route_cb.ip_count--; 242 break; 243 case AF_INET6: 244 route_cb.ip6_count--; 245 break; 246 case AF_IPX: 247 route_cb.ipx_count--; 248 break; 249 } 250 route_cb.any_count--; 251 RTSOCK_UNLOCK(); 252 raw_usrreqs.pru_detach(so); 253 } 254 255 static int 256 rts_disconnect(struct socket *so) 257 { 258 259 return (raw_usrreqs.pru_disconnect(so)); 260 } 261 262 /* pru_listen is EOPNOTSUPP */ 263 264 static int 265 rts_peeraddr(struct socket *so, struct sockaddr **nam) 266 { 267 268 return (raw_usrreqs.pru_peeraddr(so, nam)); 269 } 270 271 /* pru_rcvd is EOPNOTSUPP */ 272 /* pru_rcvoob is EOPNOTSUPP */ 273 274 static int 275 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 276 struct mbuf *control, struct thread *td) 277 { 278 279 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 280 } 281 282 /* pru_sense is null */ 283 284 static int 285 rts_shutdown(struct socket *so) 286 { 287 288 return (raw_usrreqs.pru_shutdown(so)); 289 } 290 291 static int 292 rts_sockaddr(struct socket *so, struct sockaddr **nam) 293 { 294 295 return (raw_usrreqs.pru_sockaddr(so, nam)); 296 } 297 298 static struct pr_usrreqs route_usrreqs = { 299 .pru_abort = rts_abort, 300 .pru_attach = rts_attach, 301 .pru_bind = rts_bind, 302 .pru_connect = rts_connect, 303 .pru_detach = rts_detach, 304 .pru_disconnect = rts_disconnect, 305 .pru_peeraddr = rts_peeraddr, 306 .pru_send = rts_send, 307 .pru_shutdown = rts_shutdown, 308 .pru_sockaddr = rts_sockaddr, 309 .pru_close = rts_close, 310 }; 311 312 /*ARGSUSED*/ 313 static int 314 route_output(struct mbuf *m, struct socket *so) 315 { 316 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 317 struct rt_msghdr *rtm = NULL; 318 struct rtentry *rt = NULL; 319 struct radix_node_head *rnh; 320 struct rt_addrinfo info; 321 int len, error = 0; 322 struct ifnet *ifp = NULL; 323 struct sockaddr_in jail; 324 325 #define senderr(e) { error = e; goto flush;} 326 if (m == NULL || ((m->m_len < sizeof(long)) && 327 (m = m_pullup(m, sizeof(long))) == NULL)) 328 return (ENOBUFS); 329 if ((m->m_flags & M_PKTHDR) == 0) 330 panic("route_output"); 331 len = m->m_pkthdr.len; 332 if (len < sizeof(*rtm) || 333 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 334 info.rti_info[RTAX_DST] = NULL; 335 senderr(EINVAL); 336 } 337 R_Malloc(rtm, struct rt_msghdr *, len); 338 if (rtm == NULL) { 339 info.rti_info[RTAX_DST] = NULL; 340 senderr(ENOBUFS); 341 } 342 m_copydata(m, 0, len, (caddr_t)rtm); 343 if (rtm->rtm_version != RTM_VERSION) { 344 info.rti_info[RTAX_DST] = NULL; 345 senderr(EPROTONOSUPPORT); 346 } 347 rtm->rtm_pid = curproc->p_pid; 348 bzero(&info, sizeof(info)); 349 info.rti_addrs = rtm->rtm_addrs; 350 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 351 info.rti_info[RTAX_DST] = NULL; 352 senderr(EINVAL); 353 } 354 info.rti_flags = rtm->rtm_flags; 355 if (info.rti_info[RTAX_DST] == NULL || 356 info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 357 (info.rti_info[RTAX_GATEWAY] != NULL && 358 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 359 senderr(EINVAL); 360 if (info.rti_info[RTAX_GENMASK]) { 361 struct radix_node *t; 362 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1); 363 if (t != NULL && 364 bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1, 365 (char *)(void *)t->rn_key + 1, 366 ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0) 367 info.rti_info[RTAX_GENMASK] = 368 (struct sockaddr *)t->rn_key; 369 else 370 senderr(ENOBUFS); 371 } 372 373 /* 374 * Verify that the caller has the appropriate privilege; RTM_GET 375 * is the only operation the non-superuser is allowed. 376 */ 377 if (rtm->rtm_type != RTM_GET) { 378 error = priv_check(curthread, PRIV_NET_ROUTE); 379 if (error) 380 senderr(error); 381 } 382 383 switch (rtm->rtm_type) { 384 struct rtentry *saved_nrt; 385 386 case RTM_ADD: 387 if (info.rti_info[RTAX_GATEWAY] == NULL) 388 senderr(EINVAL); 389 saved_nrt = NULL; 390 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 391 so->so_fibnum); 392 if (error == 0 && saved_nrt) { 393 RT_LOCK(saved_nrt); 394 rt_setmetrics(rtm->rtm_inits, 395 &rtm->rtm_rmx, &saved_nrt->rt_rmx); 396 rtm->rtm_index = saved_nrt->rt_ifp->if_index; 397 RT_REMREF(saved_nrt); 398 saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 399 RT_UNLOCK(saved_nrt); 400 } 401 break; 402 403 case RTM_DELETE: 404 saved_nrt = NULL; 405 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 406 so->so_fibnum); 407 if (error == 0) { 408 RT_LOCK(saved_nrt); 409 rt = saved_nrt; 410 goto report; 411 } 412 break; 413 414 case RTM_GET: 415 case RTM_CHANGE: 416 case RTM_LOCK: 417 rnh = V_rt_tables[so->so_fibnum][info.rti_info[RTAX_DST]->sa_family]; 418 if (rnh == NULL) 419 senderr(EAFNOSUPPORT); 420 RADIX_NODE_HEAD_LOCK(rnh); 421 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 422 info.rti_info[RTAX_NETMASK], rnh); 423 if (rt == NULL) { /* XXX looks bogus */ 424 RADIX_NODE_HEAD_UNLOCK(rnh); 425 senderr(ESRCH); 426 } 427 #ifdef RADIX_MPATH 428 /* 429 * for RTM_CHANGE/LOCK, if we got multipath routes, 430 * we require users to specify a matching RTAX_GATEWAY. 431 * 432 * for RTM_GET, gate is optional even with multipath. 433 * if gate == NULL the first match is returned. 434 * (no need to call rt_mpath_matchgate if gate == NULL) 435 */ 436 if (rn_mpath_capable(rnh) && 437 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 438 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 439 if (!rt) { 440 RADIX_NODE_HEAD_UNLOCK(rnh); 441 senderr(ESRCH); 442 } 443 } 444 #endif 445 RT_LOCK(rt); 446 RT_ADDREF(rt); 447 RADIX_NODE_HEAD_UNLOCK(rnh); 448 449 /* 450 * Fix for PR: 82974 451 * 452 * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 453 * returns a perfect match in case a netmask is 454 * specified. For host routes only a longest prefix 455 * match is returned so it is necessary to compare the 456 * existence of the netmask. If both have a netmask 457 * rnh_lookup() did a perfect match and if none of them 458 * have a netmask both are host routes which is also a 459 * perfect match. 460 */ 461 462 if (rtm->rtm_type != RTM_GET && 463 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 464 RT_UNLOCK(rt); 465 senderr(ESRCH); 466 } 467 468 switch(rtm->rtm_type) { 469 470 case RTM_GET: 471 report: 472 RT_LOCK_ASSERT(rt); 473 info.rti_info[RTAX_DST] = rt_key(rt); 474 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 475 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 476 info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 477 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 478 ifp = rt->rt_ifp; 479 if (ifp) { 480 info.rti_info[RTAX_IFP] = 481 ifp->if_addr->ifa_addr; 482 if (jailed(so->so_cred)) { 483 bzero(&jail, sizeof(jail)); 484 jail.sin_family = PF_INET; 485 jail.sin_len = sizeof(jail); 486 jail.sin_addr.s_addr = 487 htonl(prison_getip(so->so_cred)); 488 info.rti_info[RTAX_IFA] = 489 (struct sockaddr *)&jail; 490 } else 491 info.rti_info[RTAX_IFA] = 492 rt->rt_ifa->ifa_addr; 493 if (ifp->if_flags & IFF_POINTOPOINT) 494 info.rti_info[RTAX_BRD] = 495 rt->rt_ifa->ifa_dstaddr; 496 rtm->rtm_index = ifp->if_index; 497 } else { 498 info.rti_info[RTAX_IFP] = NULL; 499 info.rti_info[RTAX_IFA] = NULL; 500 } 501 } else if ((ifp = rt->rt_ifp) != NULL) { 502 rtm->rtm_index = ifp->if_index; 503 } 504 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 505 if (len > rtm->rtm_msglen) { 506 struct rt_msghdr *new_rtm; 507 R_Malloc(new_rtm, struct rt_msghdr *, len); 508 if (new_rtm == NULL) { 509 RT_UNLOCK(rt); 510 senderr(ENOBUFS); 511 } 512 bcopy(rtm, new_rtm, rtm->rtm_msglen); 513 Free(rtm); rtm = new_rtm; 514 } 515 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 516 rtm->rtm_flags = rt->rt_flags; 517 rtm->rtm_use = 0; 518 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 519 rtm->rtm_addrs = info.rti_addrs; 520 break; 521 522 case RTM_CHANGE: 523 /* 524 * New gateway could require new ifaddr, ifp; 525 * flags may also be different; ifp may be specified 526 * by ll sockaddr when protocol address is ambiguous 527 */ 528 if (((rt->rt_flags & RTF_GATEWAY) && 529 info.rti_info[RTAX_GATEWAY] != NULL) || 530 info.rti_info[RTAX_IFP] != NULL || 531 (info.rti_info[RTAX_IFA] != NULL && 532 !sa_equal(info.rti_info[RTAX_IFA], 533 rt->rt_ifa->ifa_addr))) { 534 RT_UNLOCK(rt); 535 if ((error = rt_getifa_fib(&info, 536 rt->rt_fibnum)) != 0) 537 senderr(error); 538 RT_LOCK(rt); 539 } 540 if (info.rti_ifa != NULL && 541 info.rti_ifa != rt->rt_ifa && 542 rt->rt_ifa != NULL && 543 rt->rt_ifa->ifa_rtrequest != NULL) { 544 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 545 &info); 546 IFAFREE(rt->rt_ifa); 547 } 548 if (info.rti_info[RTAX_GATEWAY] != NULL) { 549 if ((error = rt_setgate(rt, rt_key(rt), 550 info.rti_info[RTAX_GATEWAY])) != 0) { 551 RT_UNLOCK(rt); 552 senderr(error); 553 } 554 if (!(rt->rt_flags & RTF_LLINFO)) 555 rt->rt_flags |= RTF_GATEWAY; 556 } 557 if (info.rti_ifa != NULL && 558 info.rti_ifa != rt->rt_ifa) { 559 IFAREF(info.rti_ifa); 560 rt->rt_ifa = info.rti_ifa; 561 rt->rt_ifp = info.rti_ifp; 562 } 563 /* Allow some flags to be toggled on change. */ 564 if (rtm->rtm_fmask & RTF_FMASK) 565 rt->rt_flags = (rt->rt_flags & 566 ~rtm->rtm_fmask) | 567 (rtm->rtm_flags & rtm->rtm_fmask); 568 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 569 &rt->rt_rmx); 570 rtm->rtm_index = rt->rt_ifp->if_index; 571 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 572 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 573 if (info.rti_info[RTAX_GENMASK]) 574 rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 575 /* FALLTHROUGH */ 576 case RTM_LOCK: 577 /* We don't support locks anymore */ 578 break; 579 } 580 RT_UNLOCK(rt); 581 break; 582 583 default: 584 senderr(EOPNOTSUPP); 585 } 586 587 flush: 588 if (rtm) { 589 if (error) 590 rtm->rtm_errno = error; 591 else 592 rtm->rtm_flags |= RTF_DONE; 593 } 594 if (rt) /* XXX can this be true? */ 595 RTFREE(rt); 596 { 597 struct rawcb *rp = NULL; 598 /* 599 * Check to see if we don't want our own messages. 600 */ 601 if ((so->so_options & SO_USELOOPBACK) == 0) { 602 if (route_cb.any_count <= 1) { 603 if (rtm) 604 Free(rtm); 605 m_freem(m); 606 return (error); 607 } 608 /* There is another listener, so construct message */ 609 rp = sotorawcb(so); 610 } 611 if (rtm) { 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 Free(rtm); 619 } 620 if (m) { 621 if (rp) { 622 /* 623 * XXX insure we don't get a copy by 624 * invalidating our protocol 625 */ 626 unsigned short family = rp->rcb_proto.sp_family; 627 rp->rcb_proto.sp_family = 0; 628 rt_dispatch(m, info.rti_info[RTAX_DST]); 629 rp->rcb_proto.sp_family = family; 630 } else 631 rt_dispatch(m, info.rti_info[RTAX_DST]); 632 } 633 } 634 return (error); 635 #undef sa_equal 636 } 637 638 static void 639 rt_setmetrics(u_long which, const struct rt_metrics *in, 640 struct rt_metrics_lite *out) 641 { 642 #define metric(f, e) if (which & (f)) out->e = in->e; 643 /* 644 * Only these are stored in the routing entry since introduction 645 * of tcp hostcache. The rest is ignored. 646 */ 647 metric(RTV_MTU, rmx_mtu); 648 /* Userland -> kernel timebase conversion. */ 649 if (which & RTV_EXPIRE) 650 out->rmx_expire = in->rmx_expire ? 651 in->rmx_expire - time_second + time_uptime : 0; 652 #undef metric 653 } 654 655 static void 656 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 657 { 658 #define metric(e) out->e = in->e; 659 bzero(out, sizeof(*out)); 660 metric(rmx_mtu); 661 /* Kernel -> userland timebase conversion. */ 662 out->rmx_expire = in->rmx_expire ? 663 in->rmx_expire - time_uptime + time_second : 0; 664 #undef metric 665 } 666 667 /* 668 * Extract the addresses of the passed sockaddrs. 669 * Do a little sanity checking so as to avoid bad memory references. 670 * This data is derived straight from userland. 671 */ 672 static int 673 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 674 { 675 struct sockaddr *sa; 676 int i; 677 678 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 679 if ((rtinfo->rti_addrs & (1 << i)) == 0) 680 continue; 681 sa = (struct sockaddr *)cp; 682 /* 683 * It won't fit. 684 */ 685 if (cp + sa->sa_len > cplim) 686 return (EINVAL); 687 /* 688 * there are no more.. quit now 689 * If there are more bits, they are in error. 690 * I've seen this. route(1) can evidently generate these. 691 * This causes kernel to core dump. 692 * for compatibility, If we see this, point to a safe address. 693 */ 694 if (sa->sa_len == 0) { 695 rtinfo->rti_info[i] = &sa_zero; 696 return (0); /* should be EINVAL but for compat */ 697 } 698 /* accept it */ 699 rtinfo->rti_info[i] = sa; 700 cp += SA_SIZE(sa); 701 } 702 return (0); 703 } 704 705 static struct mbuf * 706 rt_msg1(int type, struct rt_addrinfo *rtinfo) 707 { 708 struct rt_msghdr *rtm; 709 struct mbuf *m; 710 int i; 711 struct sockaddr *sa; 712 int len, dlen; 713 714 switch (type) { 715 716 case RTM_DELADDR: 717 case RTM_NEWADDR: 718 len = sizeof(struct ifa_msghdr); 719 break; 720 721 case RTM_DELMADDR: 722 case RTM_NEWMADDR: 723 len = sizeof(struct ifma_msghdr); 724 break; 725 726 case RTM_IFINFO: 727 len = sizeof(struct if_msghdr); 728 break; 729 730 case RTM_IFANNOUNCE: 731 case RTM_IEEE80211: 732 len = sizeof(struct if_announcemsghdr); 733 break; 734 735 default: 736 len = sizeof(struct rt_msghdr); 737 } 738 if (len > MCLBYTES) 739 panic("rt_msg1"); 740 m = m_gethdr(M_DONTWAIT, MT_DATA); 741 if (m && len > MHLEN) { 742 MCLGET(m, M_DONTWAIT); 743 if ((m->m_flags & M_EXT) == 0) { 744 m_free(m); 745 m = NULL; 746 } 747 } 748 if (m == NULL) 749 return (m); 750 m->m_pkthdr.len = m->m_len = len; 751 m->m_pkthdr.rcvif = NULL; 752 rtm = mtod(m, struct rt_msghdr *); 753 bzero((caddr_t)rtm, len); 754 for (i = 0; i < RTAX_MAX; i++) { 755 if ((sa = rtinfo->rti_info[i]) == NULL) 756 continue; 757 rtinfo->rti_addrs |= (1 << i); 758 dlen = SA_SIZE(sa); 759 m_copyback(m, len, dlen, (caddr_t)sa); 760 len += dlen; 761 } 762 if (m->m_pkthdr.len != len) { 763 m_freem(m); 764 return (NULL); 765 } 766 rtm->rtm_msglen = len; 767 rtm->rtm_version = RTM_VERSION; 768 rtm->rtm_type = type; 769 return (m); 770 } 771 772 static int 773 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 774 { 775 int i; 776 int len, dlen, second_time = 0; 777 caddr_t cp0; 778 779 rtinfo->rti_addrs = 0; 780 again: 781 switch (type) { 782 783 case RTM_DELADDR: 784 case RTM_NEWADDR: 785 len = sizeof(struct ifa_msghdr); 786 break; 787 788 case RTM_IFINFO: 789 len = sizeof(struct if_msghdr); 790 break; 791 792 case RTM_NEWMADDR: 793 len = sizeof(struct ifma_msghdr); 794 break; 795 796 default: 797 len = sizeof(struct rt_msghdr); 798 } 799 cp0 = cp; 800 if (cp0) 801 cp += len; 802 for (i = 0; i < RTAX_MAX; i++) { 803 struct sockaddr *sa; 804 805 if ((sa = rtinfo->rti_info[i]) == NULL) 806 continue; 807 rtinfo->rti_addrs |= (1 << i); 808 dlen = SA_SIZE(sa); 809 if (cp) { 810 bcopy((caddr_t)sa, cp, (unsigned)dlen); 811 cp += dlen; 812 } 813 len += dlen; 814 } 815 len = ALIGN(len); 816 if (cp == NULL && w != NULL && !second_time) { 817 struct walkarg *rw = w; 818 819 if (rw->w_req) { 820 if (rw->w_tmemsize < len) { 821 if (rw->w_tmem) 822 free(rw->w_tmem, M_RTABLE); 823 rw->w_tmem = (caddr_t) 824 malloc(len, M_RTABLE, M_NOWAIT); 825 if (rw->w_tmem) 826 rw->w_tmemsize = len; 827 } 828 if (rw->w_tmem) { 829 cp = rw->w_tmem; 830 second_time = 1; 831 goto again; 832 } 833 } 834 } 835 if (cp) { 836 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 837 838 rtm->rtm_version = RTM_VERSION; 839 rtm->rtm_type = type; 840 rtm->rtm_msglen = len; 841 } 842 return (len); 843 } 844 845 /* 846 * This routine is called to generate a message from the routing 847 * socket indicating that a redirect has occured, a routing lookup 848 * has failed, or that a protocol has detected timeouts to a particular 849 * destination. 850 */ 851 void 852 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 853 { 854 struct rt_msghdr *rtm; 855 struct mbuf *m; 856 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 857 858 if (route_cb.any_count == 0) 859 return; 860 m = rt_msg1(type, rtinfo); 861 if (m == NULL) 862 return; 863 rtm = mtod(m, struct rt_msghdr *); 864 rtm->rtm_flags = RTF_DONE | flags; 865 rtm->rtm_errno = error; 866 rtm->rtm_addrs = rtinfo->rti_addrs; 867 rt_dispatch(m, sa); 868 } 869 870 /* 871 * This routine is called to generate a message from the routing 872 * socket indicating that the status of a network interface has changed. 873 */ 874 void 875 rt_ifmsg(struct ifnet *ifp) 876 { 877 struct if_msghdr *ifm; 878 struct mbuf *m; 879 struct rt_addrinfo info; 880 881 if (route_cb.any_count == 0) 882 return; 883 bzero((caddr_t)&info, sizeof(info)); 884 m = rt_msg1(RTM_IFINFO, &info); 885 if (m == NULL) 886 return; 887 ifm = mtod(m, struct if_msghdr *); 888 ifm->ifm_index = ifp->if_index; 889 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 890 ifm->ifm_data = ifp->if_data; 891 ifm->ifm_addrs = 0; 892 rt_dispatch(m, NULL); 893 } 894 895 /* 896 * This is called to generate messages from the routing socket 897 * indicating a network interface has had addresses associated with it. 898 * if we ever reverse the logic and replace messages TO the routing 899 * socket indicate a request to configure interfaces, then it will 900 * be unnecessary as the routing socket will automatically generate 901 * copies of it. 902 */ 903 void 904 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 905 { 906 struct rt_addrinfo info; 907 struct sockaddr *sa = NULL; 908 int pass; 909 struct mbuf *m = NULL; 910 struct ifnet *ifp = ifa->ifa_ifp; 911 912 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 913 ("unexpected cmd %u", cmd)); 914 #ifdef SCTP 915 /* 916 * notify the SCTP stack 917 * this will only get called when an address is added/deleted 918 * XXX pass the ifaddr struct instead if ifa->ifa_addr... 919 */ 920 sctp_addr_change(ifa, cmd); 921 #endif /* SCTP */ 922 if (route_cb.any_count == 0) 923 return; 924 for (pass = 1; pass < 3; pass++) { 925 bzero((caddr_t)&info, sizeof(info)); 926 if ((cmd == RTM_ADD && pass == 1) || 927 (cmd == RTM_DELETE && pass == 2)) { 928 struct ifa_msghdr *ifam; 929 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 930 931 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 932 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 933 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 934 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 935 if ((m = rt_msg1(ncmd, &info)) == NULL) 936 continue; 937 ifam = mtod(m, struct ifa_msghdr *); 938 ifam->ifam_index = ifp->if_index; 939 ifam->ifam_metric = ifa->ifa_metric; 940 ifam->ifam_flags = ifa->ifa_flags; 941 ifam->ifam_addrs = info.rti_addrs; 942 } 943 if ((cmd == RTM_ADD && pass == 2) || 944 (cmd == RTM_DELETE && pass == 1)) { 945 struct rt_msghdr *rtm; 946 947 if (rt == NULL) 948 continue; 949 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 950 info.rti_info[RTAX_DST] = sa = rt_key(rt); 951 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 952 if ((m = rt_msg1(cmd, &info)) == NULL) 953 continue; 954 rtm = mtod(m, struct rt_msghdr *); 955 rtm->rtm_index = ifp->if_index; 956 rtm->rtm_flags |= rt->rt_flags; 957 rtm->rtm_errno = error; 958 rtm->rtm_addrs = info.rti_addrs; 959 } 960 rt_dispatch(m, sa); 961 } 962 } 963 964 /* 965 * This is the analogue to the rt_newaddrmsg which performs the same 966 * function but for multicast group memberhips. This is easier since 967 * there is no route state to worry about. 968 */ 969 void 970 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 971 { 972 struct rt_addrinfo info; 973 struct mbuf *m = NULL; 974 struct ifnet *ifp = ifma->ifma_ifp; 975 struct ifma_msghdr *ifmam; 976 977 if (route_cb.any_count == 0) 978 return; 979 980 bzero((caddr_t)&info, sizeof(info)); 981 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 982 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 983 /* 984 * If a link-layer address is present, present it as a ``gateway'' 985 * (similarly to how ARP entries, e.g., are presented). 986 */ 987 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 988 m = rt_msg1(cmd, &info); 989 if (m == NULL) 990 return; 991 ifmam = mtod(m, struct ifma_msghdr *); 992 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 993 __func__)); 994 ifmam->ifmam_index = ifp->if_index; 995 ifmam->ifmam_addrs = info.rti_addrs; 996 rt_dispatch(m, ifma->ifma_addr); 997 } 998 999 static struct mbuf * 1000 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1001 struct rt_addrinfo *info) 1002 { 1003 struct if_announcemsghdr *ifan; 1004 struct mbuf *m; 1005 1006 if (route_cb.any_count == 0) 1007 return NULL; 1008 bzero((caddr_t)info, sizeof(*info)); 1009 m = rt_msg1(type, info); 1010 if (m != NULL) { 1011 ifan = mtod(m, struct if_announcemsghdr *); 1012 ifan->ifan_index = ifp->if_index; 1013 strlcpy(ifan->ifan_name, ifp->if_xname, 1014 sizeof(ifan->ifan_name)); 1015 ifan->ifan_what = what; 1016 } 1017 return m; 1018 } 1019 1020 /* 1021 * This is called to generate routing socket messages indicating 1022 * IEEE80211 wireless events. 1023 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1024 */ 1025 void 1026 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1027 { 1028 struct mbuf *m; 1029 struct rt_addrinfo info; 1030 1031 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1032 if (m != NULL) { 1033 /* 1034 * Append the ieee80211 data. Try to stick it in the 1035 * mbuf containing the ifannounce msg; otherwise allocate 1036 * a new mbuf and append. 1037 * 1038 * NB: we assume m is a single mbuf. 1039 */ 1040 if (data_len > M_TRAILINGSPACE(m)) { 1041 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1042 if (n == NULL) { 1043 m_freem(m); 1044 return; 1045 } 1046 bcopy(data, mtod(n, void *), data_len); 1047 n->m_len = data_len; 1048 m->m_next = n; 1049 } else if (data_len > 0) { 1050 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1051 m->m_len += data_len; 1052 } 1053 if (m->m_flags & M_PKTHDR) 1054 m->m_pkthdr.len += data_len; 1055 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1056 rt_dispatch(m, NULL); 1057 } 1058 } 1059 1060 /* 1061 * This is called to generate routing socket messages indicating 1062 * network interface arrival and departure. 1063 */ 1064 void 1065 rt_ifannouncemsg(struct ifnet *ifp, int what) 1066 { 1067 struct mbuf *m; 1068 struct rt_addrinfo info; 1069 1070 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1071 if (m != NULL) 1072 rt_dispatch(m, NULL); 1073 } 1074 1075 static void 1076 rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 1077 { 1078 struct m_tag *tag; 1079 1080 /* 1081 * Preserve the family from the sockaddr, if any, in an m_tag for 1082 * use when injecting the mbuf into the routing socket buffer from 1083 * the netisr. 1084 */ 1085 if (sa != NULL) { 1086 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1087 M_NOWAIT); 1088 if (tag == NULL) { 1089 m_freem(m); 1090 return; 1091 } 1092 *(unsigned short *)(tag + 1) = sa->sa_family; 1093 m_tag_prepend(m, tag); 1094 } 1095 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 1096 } 1097 1098 /* 1099 * This is used in dumping the kernel table via sysctl(). 1100 */ 1101 static int 1102 sysctl_dumpentry(struct radix_node *rn, void *vw) 1103 { 1104 struct walkarg *w = vw; 1105 struct rtentry *rt = (struct rtentry *)rn; 1106 int error = 0, size; 1107 struct rt_addrinfo info; 1108 1109 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1110 return 0; 1111 bzero((caddr_t)&info, sizeof(info)); 1112 info.rti_info[RTAX_DST] = rt_key(rt); 1113 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1114 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1115 info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 1116 if (rt->rt_ifp) { 1117 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1118 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1119 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1120 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1121 } 1122 size = rt_msg2(RTM_GET, &info, NULL, w); 1123 if (w->w_req && w->w_tmem) { 1124 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1125 1126 rtm->rtm_flags = rt->rt_flags; 1127 rtm->rtm_use = rt->rt_rmx.rmx_pksent; 1128 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1129 rtm->rtm_index = rt->rt_ifp->if_index; 1130 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1131 rtm->rtm_addrs = info.rti_addrs; 1132 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 1133 return (error); 1134 } 1135 return (error); 1136 } 1137 1138 static int 1139 sysctl_iflist(int af, struct walkarg *w) 1140 { 1141 struct ifnet *ifp; 1142 struct ifaddr *ifa; 1143 struct rt_addrinfo info; 1144 int len, error = 0; 1145 1146 bzero((caddr_t)&info, sizeof(info)); 1147 IFNET_RLOCK(); 1148 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1149 if (w->w_arg && w->w_arg != ifp->if_index) 1150 continue; 1151 ifa = ifp->if_addr; 1152 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1153 len = rt_msg2(RTM_IFINFO, &info, NULL, w); 1154 info.rti_info[RTAX_IFP] = NULL; 1155 if (w->w_req && w->w_tmem) { 1156 struct if_msghdr *ifm; 1157 1158 ifm = (struct if_msghdr *)w->w_tmem; 1159 ifm->ifm_index = ifp->if_index; 1160 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1161 ifm->ifm_data = ifp->if_data; 1162 ifm->ifm_addrs = info.rti_addrs; 1163 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 1164 if (error) 1165 goto done; 1166 } 1167 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1168 if (af && af != ifa->ifa_addr->sa_family) 1169 continue; 1170 if (jailed(curthread->td_ucred) && 1171 prison_if(curthread->td_ucred, ifa->ifa_addr)) 1172 continue; 1173 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1174 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1175 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1176 len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 1177 if (w->w_req && w->w_tmem) { 1178 struct ifa_msghdr *ifam; 1179 1180 ifam = (struct ifa_msghdr *)w->w_tmem; 1181 ifam->ifam_index = ifa->ifa_ifp->if_index; 1182 ifam->ifam_flags = ifa->ifa_flags; 1183 ifam->ifam_metric = ifa->ifa_metric; 1184 ifam->ifam_addrs = info.rti_addrs; 1185 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1186 if (error) 1187 goto done; 1188 } 1189 } 1190 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1191 info.rti_info[RTAX_BRD] = NULL; 1192 } 1193 done: 1194 IFNET_RUNLOCK(); 1195 return (error); 1196 } 1197 1198 int 1199 sysctl_ifmalist(int af, struct walkarg *w) 1200 { 1201 struct ifnet *ifp; 1202 struct ifmultiaddr *ifma; 1203 struct rt_addrinfo info; 1204 int len, error = 0; 1205 struct ifaddr *ifa; 1206 1207 bzero((caddr_t)&info, sizeof(info)); 1208 IFNET_RLOCK(); 1209 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1210 if (w->w_arg && w->w_arg != ifp->if_index) 1211 continue; 1212 ifa = ifp->if_addr; 1213 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1214 IF_ADDR_LOCK(ifp); 1215 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1216 if (af && af != ifma->ifma_addr->sa_family) 1217 continue; 1218 if (jailed(curproc->p_ucred) && 1219 prison_if(curproc->p_ucred, ifma->ifma_addr)) 1220 continue; 1221 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1222 info.rti_info[RTAX_GATEWAY] = 1223 (ifma->ifma_addr->sa_family != AF_LINK) ? 1224 ifma->ifma_lladdr : NULL; 1225 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 1226 if (w->w_req && w->w_tmem) { 1227 struct ifma_msghdr *ifmam; 1228 1229 ifmam = (struct ifma_msghdr *)w->w_tmem; 1230 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 1231 ifmam->ifmam_flags = 0; 1232 ifmam->ifmam_addrs = info.rti_addrs; 1233 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1234 if (error) { 1235 IF_ADDR_UNLOCK(ifp); 1236 goto done; 1237 } 1238 } 1239 } 1240 IF_ADDR_UNLOCK(ifp); 1241 } 1242 done: 1243 IFNET_RUNLOCK(); 1244 return (error); 1245 } 1246 1247 static int 1248 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1249 { 1250 int *name = (int *)arg1; 1251 u_int namelen = arg2; 1252 struct radix_node_head *rnh; 1253 int i, lim, error = EINVAL; 1254 u_char af; 1255 struct walkarg w; 1256 1257 name ++; 1258 namelen--; 1259 if (req->newptr) 1260 return (EPERM); 1261 if (namelen != 3) 1262 return ((namelen < 3) ? EISDIR : ENOTDIR); 1263 af = name[0]; 1264 if (af > AF_MAX) 1265 return (EINVAL); 1266 bzero(&w, sizeof(w)); 1267 w.w_op = name[1]; 1268 w.w_arg = name[2]; 1269 w.w_req = req; 1270 1271 error = sysctl_wire_old_buffer(req, 0); 1272 if (error) 1273 return (error); 1274 switch (w.w_op) { 1275 1276 case NET_RT_DUMP: 1277 case NET_RT_FLAGS: 1278 if (af == 0) { /* dump all tables */ 1279 i = 1; 1280 lim = AF_MAX; 1281 } else /* dump only one table */ 1282 i = lim = af; 1283 for (error = 0; error == 0 && i <= lim; i++) 1284 if ((rnh = V_rt_tables[curthread->td_proc->p_fibnum][i]) != NULL) { 1285 RADIX_NODE_HEAD_LOCK(rnh); 1286 error = rnh->rnh_walktree(rnh, 1287 sysctl_dumpentry, &w); 1288 RADIX_NODE_HEAD_UNLOCK(rnh); 1289 } else if (af != 0) 1290 error = EAFNOSUPPORT; 1291 break; 1292 1293 case NET_RT_IFLIST: 1294 error = sysctl_iflist(af, &w); 1295 break; 1296 1297 case NET_RT_IFMALIST: 1298 error = sysctl_ifmalist(af, &w); 1299 break; 1300 } 1301 if (w.w_tmem) 1302 free(w.w_tmem, M_RTABLE); 1303 return (error); 1304 } 1305 1306 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1307 1308 /* 1309 * Definitions of protocols supported in the ROUTE domain. 1310 */ 1311 1312 static struct domain routedomain; /* or at least forward */ 1313 1314 static struct protosw routesw[] = { 1315 { 1316 .pr_type = SOCK_RAW, 1317 .pr_domain = &routedomain, 1318 .pr_flags = PR_ATOMIC|PR_ADDR, 1319 .pr_output = route_output, 1320 .pr_ctlinput = raw_ctlinput, 1321 .pr_init = raw_init, 1322 .pr_usrreqs = &route_usrreqs 1323 } 1324 }; 1325 1326 static struct domain routedomain = { 1327 .dom_family = PF_ROUTE, 1328 .dom_name = "route", 1329 .dom_protosw = routesw, 1330 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1331 }; 1332 1333 DOMAIN_SET(route); 1334