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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94 34 * $FreeBSD$ 35 */ 36 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/sysctl.h> 42 #include <sys/proc.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/domain.h> 48 #include <sys/protosw.h> 49 #include <sys/jail.h> 50 51 #include <net/if.h> 52 #include <net/route.h> 53 #include <net/raw_cb.h> 54 55 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 56 57 static struct sockaddr route_dst = { 2, PF_ROUTE, }; 58 static struct sockaddr route_src = { 2, PF_ROUTE, }; 59 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 60 static struct sockproto route_proto = { PF_ROUTE, }; 61 62 struct walkarg { 63 int w_tmemsize; 64 int w_op, w_arg; 65 caddr_t w_tmem; 66 struct sysctl_req *w_req; 67 }; 68 69 static struct mbuf * 70 rt_msg1 __P((int, struct rt_addrinfo *)); 71 static int rt_msg2 __P((int, 72 struct rt_addrinfo *, caddr_t, struct walkarg *)); 73 static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 74 static int sysctl_dumpentry __P((struct radix_node *rn, void *vw)); 75 static int sysctl_iflist __P((int af, struct walkarg *w)); 76 static int route_output __P((struct mbuf *, struct socket *)); 77 static void rt_setmetrics __P((u_long, struct rt_metrics *, struct rt_metrics *)); 78 79 /* Sleazy use of local variables throughout file, warning!!!! */ 80 #define dst info.rti_info[RTAX_DST] 81 #define gate info.rti_info[RTAX_GATEWAY] 82 #define netmask info.rti_info[RTAX_NETMASK] 83 #define genmask info.rti_info[RTAX_GENMASK] 84 #define ifpaddr info.rti_info[RTAX_IFP] 85 #define ifaaddr info.rti_info[RTAX_IFA] 86 #define brdaddr info.rti_info[RTAX_BRD] 87 88 /* 89 * It really doesn't make any sense at all for this code to share much 90 * with raw_usrreq.c, since its functionality is so restricted. XXX 91 */ 92 static int 93 rts_abort(struct socket *so) 94 { 95 int s, error; 96 s = splnet(); 97 error = raw_usrreqs.pru_abort(so); 98 splx(s); 99 return error; 100 } 101 102 /* pru_accept is EOPNOTSUPP */ 103 104 static int 105 rts_attach(struct socket *so, int proto, struct thread *td) 106 { 107 struct rawcb *rp; 108 int s, error; 109 110 if (sotorawcb(so) != 0) 111 return EISCONN; /* XXX panic? */ 112 /* XXX */ 113 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 114 if (rp == 0) 115 return ENOBUFS; 116 117 /* 118 * The splnet() is necessary to block protocols from sending 119 * error notifications (like RTM_REDIRECT or RTM_LOSING) while 120 * this PCB is extant but incompletely initialized. 121 * Probably we should try to do more of this work beforehand and 122 * eliminate the spl. 123 */ 124 s = splnet(); 125 so->so_pcb = (caddr_t)rp; 126 error = raw_attach(so, proto); 127 rp = sotorawcb(so); 128 if (error) { 129 splx(s); 130 so->so_pcb = NULL; 131 free(rp, M_PCB); 132 return error; 133 } 134 switch(rp->rcb_proto.sp_protocol) { 135 case AF_INET: 136 route_cb.ip_count++; 137 break; 138 case AF_INET6: 139 route_cb.ip6_count++; 140 break; 141 case AF_IPX: 142 route_cb.ipx_count++; 143 break; 144 case AF_NS: 145 route_cb.ns_count++; 146 break; 147 } 148 rp->rcb_faddr = &route_src; 149 route_cb.any_count++; 150 soisconnected(so); 151 so->so_options |= SO_USELOOPBACK; 152 splx(s); 153 return 0; 154 } 155 156 static int 157 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 158 { 159 int s, error; 160 s = splnet(); 161 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */ 162 splx(s); 163 return error; 164 } 165 166 static int 167 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 168 { 169 int s, error; 170 s = splnet(); 171 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */ 172 splx(s); 173 return error; 174 } 175 176 /* pru_connect2 is EOPNOTSUPP */ 177 /* pru_control is EOPNOTSUPP */ 178 179 static int 180 rts_detach(struct socket *so) 181 { 182 struct rawcb *rp = sotorawcb(so); 183 int s, error; 184 185 s = splnet(); 186 if (rp != 0) { 187 switch(rp->rcb_proto.sp_protocol) { 188 case AF_INET: 189 route_cb.ip_count--; 190 break; 191 case AF_INET6: 192 route_cb.ip6_count--; 193 break; 194 case AF_IPX: 195 route_cb.ipx_count--; 196 break; 197 case AF_NS: 198 route_cb.ns_count--; 199 break; 200 } 201 route_cb.any_count--; 202 } 203 error = raw_usrreqs.pru_detach(so); 204 splx(s); 205 return error; 206 } 207 208 static int 209 rts_disconnect(struct socket *so) 210 { 211 int s, error; 212 s = splnet(); 213 error = raw_usrreqs.pru_disconnect(so); 214 splx(s); 215 return error; 216 } 217 218 /* pru_listen is EOPNOTSUPP */ 219 220 static int 221 rts_peeraddr(struct socket *so, struct sockaddr **nam) 222 { 223 int s, error; 224 s = splnet(); 225 error = raw_usrreqs.pru_peeraddr(so, nam); 226 splx(s); 227 return error; 228 } 229 230 /* pru_rcvd is EOPNOTSUPP */ 231 /* pru_rcvoob is EOPNOTSUPP */ 232 233 static int 234 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 235 struct mbuf *control, struct thread *td) 236 { 237 int s, error; 238 s = splnet(); 239 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td); 240 splx(s); 241 return error; 242 } 243 244 /* pru_sense is null */ 245 246 static int 247 rts_shutdown(struct socket *so) 248 { 249 int s, error; 250 s = splnet(); 251 error = raw_usrreqs.pru_shutdown(so); 252 splx(s); 253 return error; 254 } 255 256 static int 257 rts_sockaddr(struct socket *so, struct sockaddr **nam) 258 { 259 int s, error; 260 s = splnet(); 261 error = raw_usrreqs.pru_sockaddr(so, nam); 262 splx(s); 263 return error; 264 } 265 266 static struct pr_usrreqs route_usrreqs = { 267 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect, 268 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect, 269 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp, 270 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr, 271 sosend, soreceive, sopoll 272 }; 273 274 /*ARGSUSED*/ 275 static int 276 route_output(m, so) 277 register struct mbuf *m; 278 struct socket *so; 279 { 280 register struct rt_msghdr *rtm = 0; 281 register struct rtentry *rt = 0; 282 struct rtentry *saved_nrt = 0; 283 struct radix_node_head *rnh; 284 struct rt_addrinfo info; 285 int len, error = 0; 286 struct ifnet *ifp = 0; 287 struct ifaddr *ifa = 0; 288 289 #define senderr(e) { error = e; goto flush;} 290 if (m == 0 || ((m->m_len < sizeof(long)) && 291 (m = m_pullup(m, sizeof(long))) == 0)) 292 return (ENOBUFS); 293 if ((m->m_flags & M_PKTHDR) == 0) 294 panic("route_output"); 295 len = m->m_pkthdr.len; 296 if (len < sizeof(*rtm) || 297 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 298 dst = 0; 299 senderr(EINVAL); 300 } 301 R_Malloc(rtm, struct rt_msghdr *, len); 302 if (rtm == 0) { 303 dst = 0; 304 senderr(ENOBUFS); 305 } 306 m_copydata(m, 0, len, (caddr_t)rtm); 307 if (rtm->rtm_version != RTM_VERSION) { 308 dst = 0; 309 senderr(EPROTONOSUPPORT); 310 } 311 rtm->rtm_pid = curproc->p_pid; 312 info.rti_addrs = rtm->rtm_addrs; 313 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 314 dst = 0; 315 senderr(EINVAL); 316 } 317 if (dst == 0 || (dst->sa_family >= AF_MAX) 318 || (gate != 0 && (gate->sa_family >= AF_MAX))) 319 senderr(EINVAL); 320 if (genmask) { 321 struct radix_node *t; 322 t = rn_addmask((caddr_t)genmask, 0, 1); 323 if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1, 324 *(u_char *)t->rn_key - 1) == 0) 325 genmask = (struct sockaddr *)(t->rn_key); 326 else 327 senderr(ENOBUFS); 328 } 329 330 /* 331 * Verify that the caller has the appropriate privilege; RTM_GET 332 * is the only operation the non-superuser is allowed. 333 */ 334 if (rtm->rtm_type != RTM_GET && suser_xxx(so->so_cred, NULL, 0) != 0) 335 senderr(EPERM); 336 337 switch (rtm->rtm_type) { 338 339 case RTM_ADD: 340 if (gate == 0) 341 senderr(EINVAL); 342 error = rtrequest(RTM_ADD, dst, gate, netmask, 343 rtm->rtm_flags, &saved_nrt); 344 if (error == 0 && saved_nrt) { 345 rt_setmetrics(rtm->rtm_inits, 346 &rtm->rtm_rmx, &saved_nrt->rt_rmx); 347 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 348 saved_nrt->rt_rmx.rmx_locks |= 349 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 350 saved_nrt->rt_refcnt--; 351 saved_nrt->rt_genmask = genmask; 352 } 353 break; 354 355 case RTM_DELETE: 356 error = rtrequest(RTM_DELETE, dst, gate, netmask, 357 rtm->rtm_flags, &saved_nrt); 358 if (error == 0) { 359 if ((rt = saved_nrt)) 360 rt->rt_refcnt++; 361 goto report; 362 } 363 break; 364 365 case RTM_GET: 366 case RTM_CHANGE: 367 case RTM_LOCK: 368 if ((rnh = rt_tables[dst->sa_family]) == 0) { 369 senderr(EAFNOSUPPORT); 370 } else if ((rt = (struct rtentry *) 371 rnh->rnh_lookup(dst, netmask, rnh)) != NULL) 372 rt->rt_refcnt++; 373 else 374 senderr(ESRCH); 375 switch(rtm->rtm_type) { 376 377 case RTM_GET: 378 report: 379 dst = rt_key(rt); 380 gate = rt->rt_gateway; 381 netmask = rt_mask(rt); 382 genmask = rt->rt_genmask; 383 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 384 ifp = rt->rt_ifp; 385 if (ifp) { 386 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 387 ifaaddr = rt->rt_ifa->ifa_addr; 388 rtm->rtm_index = ifp->if_index; 389 } else { 390 ifpaddr = 0; 391 ifaaddr = 0; 392 } 393 } 394 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0, 395 (struct walkarg *)0); 396 if (len > rtm->rtm_msglen) { 397 struct rt_msghdr *new_rtm; 398 R_Malloc(new_rtm, struct rt_msghdr *, len); 399 if (new_rtm == 0) 400 senderr(ENOBUFS); 401 Bcopy(rtm, new_rtm, rtm->rtm_msglen); 402 Free(rtm); rtm = new_rtm; 403 } 404 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, 405 (struct walkarg *)0); 406 rtm->rtm_flags = rt->rt_flags; 407 rtm->rtm_rmx = rt->rt_rmx; 408 rtm->rtm_addrs = info.rti_addrs; 409 break; 410 411 case RTM_CHANGE: 412 if (gate && (error = rt_setgate(rt, rt_key(rt), gate))) 413 senderr(error); 414 415 /* 416 * If they tried to change things but didn't specify 417 * the required gateway, then just use the old one. 418 * This can happen if the user tries to change the 419 * flags on the default route without changing the 420 * default gateway. Changing flags still doesn't work. 421 */ 422 if ((rt->rt_flags & RTF_GATEWAY) && !gate) 423 gate = rt->rt_gateway; 424 425 /* new gateway could require new ifaddr, ifp; 426 flags may also be different; ifp may be specified 427 by ll sockaddr when protocol address is ambiguous */ 428 if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) && 429 (ifp = ifa->ifa_ifp) && (ifaaddr || gate)) 430 ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate, 431 ifp); 432 else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) || 433 (gate && (ifa = ifa_ifwithroute(rt->rt_flags, 434 rt_key(rt), gate)))) 435 ifp = ifa->ifa_ifp; 436 if (ifa) { 437 register struct ifaddr *oifa = rt->rt_ifa; 438 if (oifa != ifa) { 439 if (oifa && oifa->ifa_rtrequest) 440 oifa->ifa_rtrequest(RTM_DELETE, 441 rt, gate); 442 IFAFREE(rt->rt_ifa); 443 rt->rt_ifa = ifa; 444 ifa->ifa_refcnt++; 445 rt->rt_ifp = ifp; 446 } 447 } 448 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 449 &rt->rt_rmx); 450 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 451 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate); 452 if (genmask) 453 rt->rt_genmask = genmask; 454 /* 455 * Fall into 456 */ 457 case RTM_LOCK: 458 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 459 rt->rt_rmx.rmx_locks |= 460 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 461 break; 462 } 463 break; 464 465 default: 466 senderr(EOPNOTSUPP); 467 } 468 469 flush: 470 if (rtm) { 471 if (error) 472 rtm->rtm_errno = error; 473 else 474 rtm->rtm_flags |= RTF_DONE; 475 } 476 if (rt) 477 rtfree(rt); 478 { 479 register struct rawcb *rp = 0; 480 /* 481 * Check to see if we don't want our own messages. 482 */ 483 if ((so->so_options & SO_USELOOPBACK) == 0) { 484 if (route_cb.any_count <= 1) { 485 if (rtm) 486 Free(rtm); 487 m_freem(m); 488 return (error); 489 } 490 /* There is another listener, so construct message */ 491 rp = sotorawcb(so); 492 } 493 if (rtm) { 494 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 495 if (m->m_pkthdr.len < rtm->rtm_msglen) { 496 m_freem(m); 497 m = NULL; 498 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 499 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 500 Free(rtm); 501 } 502 if (rp) 503 rp->rcb_proto.sp_family = 0; /* Avoid us */ 504 if (dst) 505 route_proto.sp_protocol = dst->sa_family; 506 if (m) 507 raw_input(m, &route_proto, &route_src, &route_dst); 508 if (rp) 509 rp->rcb_proto.sp_family = PF_ROUTE; 510 } 511 return (error); 512 } 513 514 static void 515 rt_setmetrics(which, in, out) 516 u_long which; 517 register struct rt_metrics *in, *out; 518 { 519 #define metric(f, e) if (which & (f)) out->e = in->e; 520 metric(RTV_RPIPE, rmx_recvpipe); 521 metric(RTV_SPIPE, rmx_sendpipe); 522 metric(RTV_SSTHRESH, rmx_ssthresh); 523 metric(RTV_RTT, rmx_rtt); 524 metric(RTV_RTTVAR, rmx_rttvar); 525 metric(RTV_HOPCOUNT, rmx_hopcount); 526 metric(RTV_MTU, rmx_mtu); 527 metric(RTV_EXPIRE, rmx_expire); 528 #undef metric 529 } 530 531 #define ROUNDUP(a) \ 532 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 533 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 534 535 536 /* 537 * Extract the addresses of the passed sockaddrs. 538 * Do a little sanity checking so as to avoid bad memory references. 539 * This data is derived straight from userland. 540 */ 541 static int 542 rt_xaddrs(cp, cplim, rtinfo) 543 register caddr_t cp, cplim; 544 register struct rt_addrinfo *rtinfo; 545 { 546 register struct sockaddr *sa; 547 register int i; 548 549 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 550 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 551 if ((rtinfo->rti_addrs & (1 << i)) == 0) 552 continue; 553 sa = (struct sockaddr *)cp; 554 /* 555 * It won't fit. 556 */ 557 if ( (cp + sa->sa_len) > cplim ) { 558 return (EINVAL); 559 } 560 561 /* 562 * there are no more.. quit now 563 * If there are more bits, they are in error. 564 * I've seen this. route(1) can evidently generate these. 565 * This causes kernel to core dump. 566 * for compatibility, If we see this, point to a safe address. 567 */ 568 if (sa->sa_len == 0) { 569 rtinfo->rti_info[i] = &sa_zero; 570 return (0); /* should be EINVAL but for compat */ 571 } 572 573 /* accept it */ 574 rtinfo->rti_info[i] = sa; 575 ADVANCE(cp, sa); 576 } 577 return (0); 578 } 579 580 static struct mbuf * 581 rt_msg1(type, rtinfo) 582 int type; 583 register struct rt_addrinfo *rtinfo; 584 { 585 register struct rt_msghdr *rtm; 586 register struct mbuf *m; 587 register int i; 588 register struct sockaddr *sa; 589 int len, dlen; 590 591 switch (type) { 592 593 case RTM_DELADDR: 594 case RTM_NEWADDR: 595 len = sizeof(struct ifa_msghdr); 596 break; 597 598 case RTM_DELMADDR: 599 case RTM_NEWMADDR: 600 len = sizeof(struct ifma_msghdr); 601 break; 602 603 case RTM_IFINFO: 604 len = sizeof(struct if_msghdr); 605 break; 606 607 default: 608 len = sizeof(struct rt_msghdr); 609 } 610 if (len > MCLBYTES) 611 panic("rt_msg1"); 612 m = m_gethdr(M_DONTWAIT, MT_DATA); 613 if (m && len > MHLEN) { 614 MCLGET(m, M_DONTWAIT); 615 if ((m->m_flags & M_EXT) == 0) { 616 m_free(m); 617 m = NULL; 618 } 619 } 620 if (m == 0) 621 return (m); 622 m->m_pkthdr.len = m->m_len = len; 623 m->m_pkthdr.rcvif = 0; 624 rtm = mtod(m, struct rt_msghdr *); 625 bzero((caddr_t)rtm, len); 626 for (i = 0; i < RTAX_MAX; i++) { 627 if ((sa = rtinfo->rti_info[i]) == NULL) 628 continue; 629 rtinfo->rti_addrs |= (1 << i); 630 dlen = ROUNDUP(sa->sa_len); 631 m_copyback(m, len, dlen, (caddr_t)sa); 632 len += dlen; 633 } 634 if (m->m_pkthdr.len != len) { 635 m_freem(m); 636 return (NULL); 637 } 638 rtm->rtm_msglen = len; 639 rtm->rtm_version = RTM_VERSION; 640 rtm->rtm_type = type; 641 return (m); 642 } 643 644 static int 645 rt_msg2(type, rtinfo, cp, w) 646 int type; 647 register struct rt_addrinfo *rtinfo; 648 caddr_t cp; 649 struct walkarg *w; 650 { 651 register int i; 652 int len, dlen, second_time = 0; 653 caddr_t cp0; 654 655 rtinfo->rti_addrs = 0; 656 again: 657 switch (type) { 658 659 case RTM_DELADDR: 660 case RTM_NEWADDR: 661 len = sizeof(struct ifa_msghdr); 662 break; 663 664 case RTM_IFINFO: 665 len = sizeof(struct if_msghdr); 666 break; 667 668 default: 669 len = sizeof(struct rt_msghdr); 670 } 671 cp0 = cp; 672 if (cp0) 673 cp += len; 674 for (i = 0; i < RTAX_MAX; i++) { 675 register struct sockaddr *sa; 676 677 if ((sa = rtinfo->rti_info[i]) == 0) 678 continue; 679 rtinfo->rti_addrs |= (1 << i); 680 dlen = ROUNDUP(sa->sa_len); 681 if (cp) { 682 bcopy((caddr_t)sa, cp, (unsigned)dlen); 683 cp += dlen; 684 } 685 len += dlen; 686 } 687 if (cp == 0 && w != NULL && !second_time) { 688 register struct walkarg *rw = w; 689 690 if (rw->w_req) { 691 if (rw->w_tmemsize < len) { 692 if (rw->w_tmem) 693 free(rw->w_tmem, M_RTABLE); 694 rw->w_tmem = (caddr_t) 695 malloc(len, M_RTABLE, M_NOWAIT); 696 if (rw->w_tmem) 697 rw->w_tmemsize = len; 698 } 699 if (rw->w_tmem) { 700 cp = rw->w_tmem; 701 second_time = 1; 702 goto again; 703 } 704 } 705 } 706 if (cp) { 707 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 708 709 rtm->rtm_version = RTM_VERSION; 710 rtm->rtm_type = type; 711 rtm->rtm_msglen = len; 712 } 713 return (len); 714 } 715 716 /* 717 * This routine is called to generate a message from the routing 718 * socket indicating that a redirect has occured, a routing lookup 719 * has failed, or that a protocol has detected timeouts to a particular 720 * destination. 721 */ 722 void 723 rt_missmsg(type, rtinfo, flags, error) 724 int type, flags, error; 725 register struct rt_addrinfo *rtinfo; 726 { 727 register struct rt_msghdr *rtm; 728 register struct mbuf *m; 729 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 730 731 if (route_cb.any_count == 0) 732 return; 733 m = rt_msg1(type, rtinfo); 734 if (m == 0) 735 return; 736 rtm = mtod(m, struct rt_msghdr *); 737 rtm->rtm_flags = RTF_DONE | flags; 738 rtm->rtm_errno = error; 739 rtm->rtm_addrs = rtinfo->rti_addrs; 740 route_proto.sp_protocol = sa ? sa->sa_family : 0; 741 raw_input(m, &route_proto, &route_src, &route_dst); 742 } 743 744 /* 745 * This routine is called to generate a message from the routing 746 * socket indicating that the status of a network interface has changed. 747 */ 748 void 749 rt_ifmsg(ifp) 750 register struct ifnet *ifp; 751 { 752 register struct if_msghdr *ifm; 753 struct mbuf *m; 754 struct rt_addrinfo info; 755 756 if (route_cb.any_count == 0) 757 return; 758 bzero((caddr_t)&info, sizeof(info)); 759 m = rt_msg1(RTM_IFINFO, &info); 760 if (m == 0) 761 return; 762 ifm = mtod(m, struct if_msghdr *); 763 ifm->ifm_index = ifp->if_index; 764 ifm->ifm_flags = (u_short)ifp->if_flags; 765 ifm->ifm_data = ifp->if_data; 766 ifm->ifm_addrs = 0; 767 route_proto.sp_protocol = 0; 768 raw_input(m, &route_proto, &route_src, &route_dst); 769 } 770 771 /* 772 * This is called to generate messages from the routing socket 773 * indicating a network interface has had addresses associated with it. 774 * if we ever reverse the logic and replace messages TO the routing 775 * socket indicate a request to configure interfaces, then it will 776 * be unnecessary as the routing socket will automatically generate 777 * copies of it. 778 */ 779 void 780 rt_newaddrmsg(cmd, ifa, error, rt) 781 int cmd, error; 782 register struct ifaddr *ifa; 783 register struct rtentry *rt; 784 { 785 struct rt_addrinfo info; 786 struct sockaddr *sa = 0; 787 int pass; 788 struct mbuf *m = 0; 789 struct ifnet *ifp = ifa->ifa_ifp; 790 791 if (route_cb.any_count == 0) 792 return; 793 for (pass = 1; pass < 3; pass++) { 794 bzero((caddr_t)&info, sizeof(info)); 795 if ((cmd == RTM_ADD && pass == 1) || 796 (cmd == RTM_DELETE && pass == 2)) { 797 register struct ifa_msghdr *ifam; 798 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 799 800 ifaaddr = sa = ifa->ifa_addr; 801 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 802 netmask = ifa->ifa_netmask; 803 brdaddr = ifa->ifa_dstaddr; 804 if ((m = rt_msg1(ncmd, &info)) == NULL) 805 continue; 806 ifam = mtod(m, struct ifa_msghdr *); 807 ifam->ifam_index = ifp->if_index; 808 ifam->ifam_metric = ifa->ifa_metric; 809 ifam->ifam_flags = ifa->ifa_flags; 810 ifam->ifam_addrs = info.rti_addrs; 811 } 812 if ((cmd == RTM_ADD && pass == 2) || 813 (cmd == RTM_DELETE && pass == 1)) { 814 register struct rt_msghdr *rtm; 815 816 if (rt == 0) 817 continue; 818 netmask = rt_mask(rt); 819 dst = sa = rt_key(rt); 820 gate = rt->rt_gateway; 821 if ((m = rt_msg1(cmd, &info)) == NULL) 822 continue; 823 rtm = mtod(m, struct rt_msghdr *); 824 rtm->rtm_index = ifp->if_index; 825 rtm->rtm_flags |= rt->rt_flags; 826 rtm->rtm_errno = error; 827 rtm->rtm_addrs = info.rti_addrs; 828 } 829 route_proto.sp_protocol = sa ? sa->sa_family : 0; 830 raw_input(m, &route_proto, &route_src, &route_dst); 831 } 832 } 833 834 /* 835 * This is the analogue to the rt_newaddrmsg which performs the same 836 * function but for multicast group memberhips. This is easier since 837 * there is no route state to worry about. 838 */ 839 void 840 rt_newmaddrmsg(cmd, ifma) 841 int cmd; 842 struct ifmultiaddr *ifma; 843 { 844 struct rt_addrinfo info; 845 struct mbuf *m = 0; 846 struct ifnet *ifp = ifma->ifma_ifp; 847 struct ifma_msghdr *ifmam; 848 849 if (route_cb.any_count == 0) 850 return; 851 852 bzero((caddr_t)&info, sizeof(info)); 853 ifaaddr = ifma->ifma_addr; 854 if (ifp && TAILQ_FIRST(&ifp->if_addrhead)) 855 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 856 else 857 ifpaddr = NULL; 858 /* 859 * If a link-layer address is present, present it as a ``gateway'' 860 * (similarly to how ARP entries, e.g., are presented). 861 */ 862 gate = ifma->ifma_lladdr; 863 if ((m = rt_msg1(cmd, &info)) == NULL) 864 return; 865 ifmam = mtod(m, struct ifma_msghdr *); 866 ifmam->ifmam_index = ifp->if_index; 867 ifmam->ifmam_addrs = info.rti_addrs; 868 route_proto.sp_protocol = ifma->ifma_addr->sa_family; 869 raw_input(m, &route_proto, &route_src, &route_dst); 870 } 871 872 /* 873 * This is used in dumping the kernel table via sysctl(). 874 */ 875 int 876 sysctl_dumpentry(rn, vw) 877 struct radix_node *rn; 878 void *vw; 879 { 880 register struct walkarg *w = vw; 881 register struct rtentry *rt = (struct rtentry *)rn; 882 int error = 0, size; 883 struct rt_addrinfo info; 884 885 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 886 return 0; 887 bzero((caddr_t)&info, sizeof(info)); 888 dst = rt_key(rt); 889 gate = rt->rt_gateway; 890 netmask = rt_mask(rt); 891 genmask = rt->rt_genmask; 892 size = rt_msg2(RTM_GET, &info, 0, w); 893 if (w->w_req && w->w_tmem) { 894 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 895 896 rtm->rtm_flags = rt->rt_flags; 897 rtm->rtm_use = rt->rt_use; 898 rtm->rtm_rmx = rt->rt_rmx; 899 rtm->rtm_index = rt->rt_ifp->if_index; 900 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 901 rtm->rtm_addrs = info.rti_addrs; 902 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 903 return (error); 904 } 905 return (error); 906 } 907 908 int 909 sysctl_iflist(af, w) 910 int af; 911 register struct walkarg *w; 912 { 913 register struct ifnet *ifp; 914 register struct ifaddr *ifa; 915 struct rt_addrinfo info; 916 int len, error = 0; 917 918 bzero((caddr_t)&info, sizeof(info)); 919 TAILQ_FOREACH(ifp, &ifnet, if_link) { 920 if (w->w_arg && w->w_arg != ifp->if_index) 921 continue; 922 ifa = TAILQ_FIRST(&ifp->if_addrhead); 923 ifpaddr = ifa->ifa_addr; 924 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w); 925 ifpaddr = 0; 926 if (w->w_req && w->w_tmem) { 927 register struct if_msghdr *ifm; 928 929 ifm = (struct if_msghdr *)w->w_tmem; 930 ifm->ifm_index = ifp->if_index; 931 ifm->ifm_flags = (u_short)ifp->if_flags; 932 ifm->ifm_data = ifp->if_data; 933 ifm->ifm_addrs = info.rti_addrs; 934 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 935 if (error) 936 return (error); 937 } 938 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) { 939 if (af && af != ifa->ifa_addr->sa_family) 940 continue; 941 if (jailed(curproc->p_ucred) && 942 prison_if(curproc->p_ucred, ifa->ifa_addr)) 943 continue; 944 ifaaddr = ifa->ifa_addr; 945 netmask = ifa->ifa_netmask; 946 brdaddr = ifa->ifa_dstaddr; 947 len = rt_msg2(RTM_NEWADDR, &info, 0, w); 948 if (w->w_req && w->w_tmem) { 949 register struct ifa_msghdr *ifam; 950 951 ifam = (struct ifa_msghdr *)w->w_tmem; 952 ifam->ifam_index = ifa->ifa_ifp->if_index; 953 ifam->ifam_flags = ifa->ifa_flags; 954 ifam->ifam_metric = ifa->ifa_metric; 955 ifam->ifam_addrs = info.rti_addrs; 956 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 957 if (error) 958 return (error); 959 } 960 } 961 ifaaddr = netmask = brdaddr = 0; 962 } 963 return (0); 964 } 965 966 static int 967 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 968 { 969 int *name = (int *)arg1; 970 u_int namelen = arg2; 971 register struct radix_node_head *rnh; 972 int i, s, error = EINVAL; 973 u_char af; 974 struct walkarg w; 975 976 name ++; 977 namelen--; 978 if (req->newptr) 979 return (EPERM); 980 if (namelen != 3) 981 return (EINVAL); 982 af = name[0]; 983 Bzero(&w, sizeof(w)); 984 w.w_op = name[1]; 985 w.w_arg = name[2]; 986 w.w_req = req; 987 988 s = splnet(); 989 switch (w.w_op) { 990 991 case NET_RT_DUMP: 992 case NET_RT_FLAGS: 993 for (i = 1; i <= AF_MAX; i++) 994 if ((rnh = rt_tables[i]) && (af == 0 || af == i) && 995 (error = rnh->rnh_walktree(rnh, 996 sysctl_dumpentry, &w))) 997 break; 998 break; 999 1000 case NET_RT_IFLIST: 1001 error = sysctl_iflist(af, &w); 1002 } 1003 splx(s); 1004 if (w.w_tmem) 1005 free(w.w_tmem, M_RTABLE); 1006 return (error); 1007 } 1008 1009 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1010 1011 /* 1012 * Definitions of protocols supported in the ROUTE domain. 1013 */ 1014 1015 extern struct domain routedomain; /* or at least forward */ 1016 1017 static struct protosw routesw[] = { 1018 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 1019 0, route_output, raw_ctlinput, 0, 1020 0, 1021 raw_init, 0, 0, 0, 1022 &route_usrreqs 1023 } 1024 }; 1025 1026 static struct domain routedomain = 1027 { PF_ROUTE, "route", 0, 0, 0, 1028 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 1029 1030 DOMAIN_SET(route); 1031