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