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 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_INET6: 137 route_cb.ip6_count++; 138 break; 139 case AF_IPX: 140 route_cb.ipx_count++; 141 break; 142 case AF_NS: 143 route_cb.ns_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_INET6: 190 route_cb.ip6_count--; 191 break; 192 case AF_IPX: 193 route_cb.ipx_count--; 194 break; 195 case AF_NS: 196 route_cb.ns_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((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1, 322 *(u_char *)t->rn_key - 1) == 0) 323 genmask = (struct sockaddr *)(t->rn_key); 324 else 325 senderr(ENOBUFS); 326 } 327 switch (rtm->rtm_type) { 328 329 case RTM_ADD: 330 if (gate == 0) 331 senderr(EINVAL); 332 error = rtrequest(RTM_ADD, dst, gate, netmask, 333 rtm->rtm_flags, &saved_nrt); 334 if (error == 0 && saved_nrt) { 335 rt_setmetrics(rtm->rtm_inits, 336 &rtm->rtm_rmx, &saved_nrt->rt_rmx); 337 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 338 saved_nrt->rt_rmx.rmx_locks |= 339 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 340 saved_nrt->rt_refcnt--; 341 saved_nrt->rt_genmask = genmask; 342 } 343 break; 344 345 case RTM_DELETE: 346 error = rtrequest(RTM_DELETE, dst, gate, netmask, 347 rtm->rtm_flags, &saved_nrt); 348 if (error == 0) { 349 if ((rt = saved_nrt)) 350 rt->rt_refcnt++; 351 goto report; 352 } 353 break; 354 355 case RTM_GET: 356 case RTM_CHANGE: 357 case RTM_LOCK: 358 if ((rnh = rt_tables[dst->sa_family]) == 0) { 359 senderr(EAFNOSUPPORT); 360 } else if ((rt = (struct rtentry *) 361 rnh->rnh_lookup(dst, netmask, rnh)) != NULL) 362 rt->rt_refcnt++; 363 else 364 senderr(ESRCH); 365 switch(rtm->rtm_type) { 366 367 case RTM_GET: 368 report: 369 dst = rt_key(rt); 370 gate = rt->rt_gateway; 371 netmask = rt_mask(rt); 372 genmask = rt->rt_genmask; 373 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 374 ifp = rt->rt_ifp; 375 if (ifp) { 376 ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; 377 ifaaddr = rt->rt_ifa->ifa_addr; 378 rtm->rtm_index = ifp->if_index; 379 } else { 380 ifpaddr = 0; 381 ifaaddr = 0; 382 } 383 } 384 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0, 385 (struct walkarg *)0); 386 if (len > rtm->rtm_msglen) { 387 struct rt_msghdr *new_rtm; 388 R_Malloc(new_rtm, struct rt_msghdr *, len); 389 if (new_rtm == 0) 390 senderr(ENOBUFS); 391 Bcopy(rtm, new_rtm, rtm->rtm_msglen); 392 Free(rtm); rtm = new_rtm; 393 } 394 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, 395 (struct walkarg *)0); 396 rtm->rtm_flags = rt->rt_flags; 397 rtm->rtm_rmx = rt->rt_rmx; 398 rtm->rtm_addrs = info.rti_addrs; 399 break; 400 401 case RTM_CHANGE: 402 if (gate && (error = rt_setgate(rt, rt_key(rt), gate))) 403 senderr(error); 404 405 /* 406 * If they tried to change things but didn't specify 407 * the required gateway, then just use the old one. 408 * This can happen if the user tries to change the 409 * flags on the default route without changing the 410 * default gateway. Changing flags still doesn't work. 411 */ 412 if ((rt->rt_flags & RTF_GATEWAY) && !gate) 413 gate = rt->rt_gateway; 414 415 /* new gateway could require new ifaddr, ifp; 416 flags may also be different; ifp may be specified 417 by ll sockaddr when protocol address is ambiguous */ 418 if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) && 419 (ifp = ifa->ifa_ifp) && (ifaaddr || gate)) 420 ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate, 421 ifp); 422 else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) || 423 (gate && (ifa = ifa_ifwithroute(rt->rt_flags, 424 rt_key(rt), gate)))) 425 ifp = ifa->ifa_ifp; 426 if (ifa) { 427 register struct ifaddr *oifa = rt->rt_ifa; 428 if (oifa != ifa) { 429 if (oifa && oifa->ifa_rtrequest) 430 oifa->ifa_rtrequest(RTM_DELETE, 431 rt, gate); 432 IFAFREE(rt->rt_ifa); 433 rt->rt_ifa = ifa; 434 ifa->ifa_refcnt++; 435 rt->rt_ifp = ifp; 436 } 437 } 438 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 439 &rt->rt_rmx); 440 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 441 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate); 442 if (genmask) 443 rt->rt_genmask = genmask; 444 /* 445 * Fall into 446 */ 447 case RTM_LOCK: 448 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 449 rt->rt_rmx.rmx_locks |= 450 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 451 break; 452 } 453 break; 454 455 default: 456 senderr(EOPNOTSUPP); 457 } 458 459 flush: 460 if (rtm) { 461 if (error) 462 rtm->rtm_errno = error; 463 else 464 rtm->rtm_flags |= RTF_DONE; 465 } 466 if (rt) 467 rtfree(rt); 468 { 469 register struct rawcb *rp = 0; 470 /* 471 * Check to see if we don't want our own messages. 472 */ 473 if ((so->so_options & SO_USELOOPBACK) == 0) { 474 if (route_cb.any_count <= 1) { 475 if (rtm) 476 Free(rtm); 477 m_freem(m); 478 return (error); 479 } 480 /* There is another listener, so construct message */ 481 rp = sotorawcb(so); 482 } 483 if (rtm) { 484 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 485 Free(rtm); 486 } 487 if (rp) 488 rp->rcb_proto.sp_family = 0; /* Avoid us */ 489 if (dst) 490 route_proto.sp_protocol = dst->sa_family; 491 raw_input(m, &route_proto, &route_src, &route_dst); 492 if (rp) 493 rp->rcb_proto.sp_family = PF_ROUTE; 494 } 495 return (error); 496 } 497 498 static void 499 rt_setmetrics(which, in, out) 500 u_long which; 501 register struct rt_metrics *in, *out; 502 { 503 #define metric(f, e) if (which & (f)) out->e = in->e; 504 metric(RTV_RPIPE, rmx_recvpipe); 505 metric(RTV_SPIPE, rmx_sendpipe); 506 metric(RTV_SSTHRESH, rmx_ssthresh); 507 metric(RTV_RTT, rmx_rtt); 508 metric(RTV_RTTVAR, rmx_rttvar); 509 metric(RTV_HOPCOUNT, rmx_hopcount); 510 metric(RTV_MTU, rmx_mtu); 511 metric(RTV_EXPIRE, rmx_expire); 512 #undef metric 513 } 514 515 #define ROUNDUP(a) \ 516 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 517 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 518 519 520 /* 521 * Extract the addresses of the passed sockaddrs. 522 * Do a little sanity checking so as to avoid bad memory references. 523 * This data is derived straight from userland. 524 */ 525 static int 526 rt_xaddrs(cp, cplim, rtinfo) 527 register caddr_t cp, cplim; 528 register struct rt_addrinfo *rtinfo; 529 { 530 register struct sockaddr *sa; 531 register int i; 532 533 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 534 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 535 if ((rtinfo->rti_addrs & (1 << i)) == 0) 536 continue; 537 sa = (struct sockaddr *)cp; 538 /* 539 * It won't fit. 540 */ 541 if ( (cp + sa->sa_len) > cplim ) { 542 return (EINVAL); 543 } 544 545 /* 546 * there are no more.. quit now 547 * If there are more bits, they are in error. 548 * I've seen this. route(1) can evidently generate these. 549 * This causes kernel to core dump. 550 * for compatibility, If we see this, point to a safe address. 551 */ 552 if (sa->sa_len == 0) { 553 rtinfo->rti_info[i] = &sa_zero; 554 return (0); /* should be EINVAL but for compat */ 555 } 556 557 /* accept it */ 558 rtinfo->rti_info[i] = sa; 559 ADVANCE(cp, sa); 560 } 561 return (0); 562 } 563 564 static struct mbuf * 565 rt_msg1(type, rtinfo) 566 int type; 567 register struct rt_addrinfo *rtinfo; 568 { 569 register struct rt_msghdr *rtm; 570 register struct mbuf *m; 571 register int i; 572 register struct sockaddr *sa; 573 int len, dlen; 574 575 m = m_gethdr(M_DONTWAIT, MT_DATA); 576 if (m == 0) 577 return (m); 578 switch (type) { 579 580 case RTM_DELADDR: 581 case RTM_NEWADDR: 582 len = sizeof(struct ifa_msghdr); 583 break; 584 585 case RTM_DELMADDR: 586 case RTM_NEWMADDR: 587 len = sizeof(struct ifma_msghdr); 588 break; 589 590 case RTM_IFINFO: 591 len = sizeof(struct if_msghdr); 592 break; 593 594 default: 595 len = sizeof(struct rt_msghdr); 596 } 597 if (len > MHLEN) 598 panic("rt_msg1"); 599 m->m_pkthdr.len = m->m_len = len; 600 m->m_pkthdr.rcvif = 0; 601 rtm = mtod(m, struct rt_msghdr *); 602 bzero((caddr_t)rtm, len); 603 for (i = 0; i < RTAX_MAX; i++) { 604 if ((sa = rtinfo->rti_info[i]) == NULL) 605 continue; 606 rtinfo->rti_addrs |= (1 << i); 607 dlen = ROUNDUP(sa->sa_len); 608 m_copyback(m, len, dlen, (caddr_t)sa); 609 len += dlen; 610 } 611 if (m->m_pkthdr.len != len) { 612 m_freem(m); 613 return (NULL); 614 } 615 rtm->rtm_msglen = len; 616 rtm->rtm_version = RTM_VERSION; 617 rtm->rtm_type = type; 618 return (m); 619 } 620 621 static int 622 rt_msg2(type, rtinfo, cp, w) 623 int type; 624 register struct rt_addrinfo *rtinfo; 625 caddr_t cp; 626 struct walkarg *w; 627 { 628 register int i; 629 int len, dlen, second_time = 0; 630 caddr_t cp0; 631 632 rtinfo->rti_addrs = 0; 633 again: 634 switch (type) { 635 636 case RTM_DELADDR: 637 case RTM_NEWADDR: 638 len = sizeof(struct ifa_msghdr); 639 break; 640 641 case RTM_IFINFO: 642 len = sizeof(struct if_msghdr); 643 break; 644 645 default: 646 len = sizeof(struct rt_msghdr); 647 } 648 cp0 = cp; 649 if (cp0) 650 cp += len; 651 for (i = 0; i < RTAX_MAX; i++) { 652 register struct sockaddr *sa; 653 654 if ((sa = rtinfo->rti_info[i]) == 0) 655 continue; 656 rtinfo->rti_addrs |= (1 << i); 657 dlen = ROUNDUP(sa->sa_len); 658 if (cp) { 659 bcopy((caddr_t)sa, cp, (unsigned)dlen); 660 cp += dlen; 661 } 662 len += dlen; 663 } 664 if (cp == 0 && w != NULL && !second_time) { 665 register struct walkarg *rw = w; 666 667 if (rw->w_req) { 668 if (rw->w_tmemsize < len) { 669 if (rw->w_tmem) 670 free(rw->w_tmem, M_RTABLE); 671 rw->w_tmem = (caddr_t) 672 malloc(len, M_RTABLE, M_NOWAIT); 673 if (rw->w_tmem) 674 rw->w_tmemsize = len; 675 } 676 if (rw->w_tmem) { 677 cp = rw->w_tmem; 678 second_time = 1; 679 goto again; 680 } 681 } 682 } 683 if (cp) { 684 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 685 686 rtm->rtm_version = RTM_VERSION; 687 rtm->rtm_type = type; 688 rtm->rtm_msglen = len; 689 } 690 return (len); 691 } 692 693 /* 694 * This routine is called to generate a message from the routing 695 * socket indicating that a redirect has occured, a routing lookup 696 * has failed, or that a protocol has detected timeouts to a particular 697 * destination. 698 */ 699 void 700 rt_missmsg(type, rtinfo, flags, error) 701 int type, flags, error; 702 register struct rt_addrinfo *rtinfo; 703 { 704 register struct rt_msghdr *rtm; 705 register struct mbuf *m; 706 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 707 708 if (route_cb.any_count == 0) 709 return; 710 m = rt_msg1(type, rtinfo); 711 if (m == 0) 712 return; 713 rtm = mtod(m, struct rt_msghdr *); 714 rtm->rtm_flags = RTF_DONE | flags; 715 rtm->rtm_errno = error; 716 rtm->rtm_addrs = rtinfo->rti_addrs; 717 route_proto.sp_protocol = sa ? sa->sa_family : 0; 718 raw_input(m, &route_proto, &route_src, &route_dst); 719 } 720 721 /* 722 * This routine is called to generate a message from the routing 723 * socket indicating that the status of a network interface has changed. 724 */ 725 void 726 rt_ifmsg(ifp) 727 register struct ifnet *ifp; 728 { 729 register struct if_msghdr *ifm; 730 struct mbuf *m; 731 struct rt_addrinfo info; 732 733 if (route_cb.any_count == 0) 734 return; 735 bzero((caddr_t)&info, sizeof(info)); 736 m = rt_msg1(RTM_IFINFO, &info); 737 if (m == 0) 738 return; 739 ifm = mtod(m, struct if_msghdr *); 740 ifm->ifm_index = ifp->if_index; 741 ifm->ifm_flags = (u_short)ifp->if_flags; 742 ifm->ifm_data = ifp->if_data; 743 ifm->ifm_addrs = 0; 744 route_proto.sp_protocol = 0; 745 raw_input(m, &route_proto, &route_src, &route_dst); 746 } 747 748 /* 749 * This is called to generate messages from the routing socket 750 * indicating a network interface has had addresses associated with it. 751 * if we ever reverse the logic and replace messages TO the routing 752 * socket indicate a request to configure interfaces, then it will 753 * be unnecessary as the routing socket will automatically generate 754 * copies of it. 755 */ 756 void 757 rt_newaddrmsg(cmd, ifa, error, rt) 758 int cmd, error; 759 register struct ifaddr *ifa; 760 register struct rtentry *rt; 761 { 762 struct rt_addrinfo info; 763 struct sockaddr *sa = 0; 764 int pass; 765 struct mbuf *m = 0; 766 struct ifnet *ifp = ifa->ifa_ifp; 767 768 if (route_cb.any_count == 0) 769 return; 770 for (pass = 1; pass < 3; pass++) { 771 bzero((caddr_t)&info, sizeof(info)); 772 if ((cmd == RTM_ADD && pass == 1) || 773 (cmd == RTM_DELETE && pass == 2)) { 774 register struct ifa_msghdr *ifam; 775 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 776 777 ifaaddr = sa = ifa->ifa_addr; 778 ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; 779 netmask = ifa->ifa_netmask; 780 brdaddr = ifa->ifa_dstaddr; 781 if ((m = rt_msg1(ncmd, &info)) == NULL) 782 continue; 783 ifam = mtod(m, struct ifa_msghdr *); 784 ifam->ifam_index = ifp->if_index; 785 ifam->ifam_metric = ifa->ifa_metric; 786 ifam->ifam_flags = ifa->ifa_flags; 787 ifam->ifam_addrs = info.rti_addrs; 788 } 789 if ((cmd == RTM_ADD && pass == 2) || 790 (cmd == RTM_DELETE && pass == 1)) { 791 register struct rt_msghdr *rtm; 792 793 if (rt == 0) 794 continue; 795 netmask = rt_mask(rt); 796 dst = sa = rt_key(rt); 797 gate = rt->rt_gateway; 798 if ((m = rt_msg1(cmd, &info)) == NULL) 799 continue; 800 rtm = mtod(m, struct rt_msghdr *); 801 rtm->rtm_index = ifp->if_index; 802 rtm->rtm_flags |= rt->rt_flags; 803 rtm->rtm_errno = error; 804 rtm->rtm_addrs = info.rti_addrs; 805 } 806 route_proto.sp_protocol = sa ? sa->sa_family : 0; 807 raw_input(m, &route_proto, &route_src, &route_dst); 808 } 809 } 810 811 /* 812 * This is the analogue to the rt_newaddrmsg which performs the same 813 * function but for multicast group memberhips. This is easier since 814 * there is no route state to worry about. 815 */ 816 void 817 rt_newmaddrmsg(cmd, ifma) 818 int cmd; 819 struct ifmultiaddr *ifma; 820 { 821 struct rt_addrinfo info; 822 struct mbuf *m = 0; 823 struct ifnet *ifp = ifma->ifma_ifp; 824 struct ifma_msghdr *ifmam; 825 826 if (route_cb.any_count == 0) 827 return; 828 829 bzero((caddr_t)&info, sizeof(info)); 830 ifaaddr = ifma->ifma_addr; 831 if (ifp && ifp->if_addrhead.tqh_first) 832 ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; 833 else 834 ifpaddr = NULL; 835 /* 836 * If a link-layer address is present, present it as a ``gateway'' 837 * (similarly to how ARP entries, e.g., are presented). 838 */ 839 gate = ifma->ifma_lladdr; 840 if ((m = rt_msg1(cmd, &info)) == NULL) 841 return; 842 ifmam = mtod(m, struct ifma_msghdr *); 843 ifmam->ifmam_index = ifp->if_index; 844 ifmam->ifmam_addrs = info.rti_addrs; 845 route_proto.sp_protocol = ifma->ifma_addr->sa_family; 846 raw_input(m, &route_proto, &route_src, &route_dst); 847 } 848 849 /* 850 * This is used in dumping the kernel table via sysctl(). 851 */ 852 int 853 sysctl_dumpentry(rn, vw) 854 struct radix_node *rn; 855 void *vw; 856 { 857 register struct walkarg *w = vw; 858 register struct rtentry *rt = (struct rtentry *)rn; 859 int error = 0, size; 860 struct rt_addrinfo info; 861 862 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 863 return 0; 864 bzero((caddr_t)&info, sizeof(info)); 865 dst = rt_key(rt); 866 gate = rt->rt_gateway; 867 netmask = rt_mask(rt); 868 genmask = rt->rt_genmask; 869 size = rt_msg2(RTM_GET, &info, 0, w); 870 if (w->w_req && w->w_tmem) { 871 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 872 873 rtm->rtm_flags = rt->rt_flags; 874 rtm->rtm_use = rt->rt_use; 875 rtm->rtm_rmx = rt->rt_rmx; 876 rtm->rtm_index = rt->rt_ifp->if_index; 877 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 878 rtm->rtm_addrs = info.rti_addrs; 879 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 880 return (error); 881 } 882 return (error); 883 } 884 885 int 886 sysctl_iflist(af, w) 887 int af; 888 register struct walkarg *w; 889 { 890 register struct ifnet *ifp; 891 register struct ifaddr *ifa; 892 struct rt_addrinfo info; 893 int len, error = 0; 894 895 bzero((caddr_t)&info, sizeof(info)); 896 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 897 if (w->w_arg && w->w_arg != ifp->if_index) 898 continue; 899 ifa = ifp->if_addrhead.tqh_first; 900 ifpaddr = ifa->ifa_addr; 901 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w); 902 ifpaddr = 0; 903 if (w->w_req && w->w_tmem) { 904 register struct if_msghdr *ifm; 905 906 ifm = (struct if_msghdr *)w->w_tmem; 907 ifm->ifm_index = ifp->if_index; 908 ifm->ifm_flags = (u_short)ifp->if_flags; 909 ifm->ifm_data = ifp->if_data; 910 ifm->ifm_addrs = info.rti_addrs; 911 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 912 if (error) 913 return (error); 914 } 915 while ((ifa = ifa->ifa_link.tqe_next) != 0) { 916 if (af && af != ifa->ifa_addr->sa_family) 917 continue; 918 if (curproc->p_prison && prison_if(curproc, ifa->ifa_addr)) 919 continue; 920 ifaaddr = ifa->ifa_addr; 921 netmask = ifa->ifa_netmask; 922 brdaddr = ifa->ifa_dstaddr; 923 len = rt_msg2(RTM_NEWADDR, &info, 0, w); 924 if (w->w_req && w->w_tmem) { 925 register struct ifa_msghdr *ifam; 926 927 ifam = (struct ifa_msghdr *)w->w_tmem; 928 ifam->ifam_index = ifa->ifa_ifp->if_index; 929 ifam->ifam_flags = ifa->ifa_flags; 930 ifam->ifam_metric = ifa->ifa_metric; 931 ifam->ifam_addrs = info.rti_addrs; 932 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 933 if (error) 934 return (error); 935 } 936 } 937 ifaaddr = netmask = brdaddr = 0; 938 } 939 return (0); 940 } 941 942 static int 943 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 944 { 945 int *name = (int *)arg1; 946 u_int namelen = arg2; 947 register struct radix_node_head *rnh; 948 int i, s, error = EINVAL; 949 u_char af; 950 struct walkarg w; 951 952 name ++; 953 namelen--; 954 if (req->newptr) 955 return (EPERM); 956 if (namelen != 3) 957 return (EINVAL); 958 af = name[0]; 959 Bzero(&w, sizeof(w)); 960 w.w_op = name[1]; 961 w.w_arg = name[2]; 962 w.w_req = req; 963 964 s = splnet(); 965 switch (w.w_op) { 966 967 case NET_RT_DUMP: 968 case NET_RT_FLAGS: 969 for (i = 1; i <= AF_MAX; i++) 970 if ((rnh = rt_tables[i]) && (af == 0 || af == i) && 971 (error = rnh->rnh_walktree(rnh, 972 sysctl_dumpentry, &w))) 973 break; 974 break; 975 976 case NET_RT_IFLIST: 977 error = sysctl_iflist(af, &w); 978 } 979 splx(s); 980 if (w.w_tmem) 981 free(w.w_tmem, M_RTABLE); 982 return (error); 983 } 984 985 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 986 987 /* 988 * Definitions of protocols supported in the ROUTE domain. 989 */ 990 991 extern struct domain routedomain; /* or at least forward */ 992 993 static struct protosw routesw[] = { 994 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 995 0, route_output, raw_ctlinput, 0, 996 0, 997 raw_init, 0, 0, 0, 998 &route_usrreqs 999 } 1000 }; 1001 1002 static struct domain routedomain = 1003 { PF_ROUTE, "route", 0, 0, 0, 1004 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 1005 1006 DOMAIN_SET(route); 1007