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