1 /* $NetBSD: rtsock.c,v 1.54 2001/11/12 23:49:48 lukem Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1988, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.54 2001/11/12 23:49:48 lukem Exp $"); 69 70 #include "opt_inet.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/proc.h> 75 #include <sys/mbuf.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/domain.h> 79 #include <sys/protosw.h> 80 #include <sys/sysctl.h> 81 82 #include <net/if.h> 83 #include <net/route.h> 84 #include <net/raw_cb.h> 85 86 #include <machine/stdarg.h> 87 88 struct sockaddr route_dst = { 2, PF_ROUTE, }; 89 struct sockaddr route_src = { 2, PF_ROUTE, }; 90 struct sockproto route_proto = { PF_ROUTE, }; 91 92 struct walkarg { 93 int w_op; 94 int w_arg; 95 int w_given; 96 int w_needed; 97 caddr_t w_where; 98 int w_tmemsize; 99 int w_tmemneeded; 100 caddr_t w_tmem; 101 }; 102 103 static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *, caddr_t, int)); 104 static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *, 105 int *)); 106 static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 107 static int sysctl_dumpentry __P((struct radix_node *, void *)); 108 static int sysctl_iflist __P((int, struct walkarg *, int)); 109 static int sysctl_rtable __P((int *, u_int, void *, size_t *, void *, size_t)); 110 static __inline void rt_adjustcount __P((int, int)); 111 112 /* Sleazy use of local variables throughout file, warning!!!! */ 113 #define dst info.rti_info[RTAX_DST] 114 #define gate info.rti_info[RTAX_GATEWAY] 115 #define netmask info.rti_info[RTAX_NETMASK] 116 #define genmask info.rti_info[RTAX_GENMASK] 117 #define ifpaddr info.rti_info[RTAX_IFP] 118 #define ifaaddr info.rti_info[RTAX_IFA] 119 #define brdaddr info.rti_info[RTAX_BRD] 120 121 static __inline void 122 rt_adjustcount(af, cnt) 123 int af, cnt; 124 { 125 route_cb.any_count += cnt; 126 switch (af) { 127 case AF_INET: 128 route_cb.ip_count += cnt; 129 return; 130 #ifdef INET6 131 case AF_INET6: 132 route_cb.ip6_count += cnt; 133 return; 134 #endif 135 case AF_IPX: 136 route_cb.ipx_count += cnt; 137 return; 138 case AF_NS: 139 route_cb.ns_count += cnt; 140 return; 141 case AF_ISO: 142 route_cb.iso_count += cnt; 143 return; 144 } 145 } 146 147 /*ARGSUSED*/ 148 int 149 route_usrreq(so, req, m, nam, control, p) 150 struct socket *so; 151 int req; 152 struct mbuf *m, *nam, *control; 153 struct proc *p; 154 { 155 int error = 0; 156 struct rawcb *rp = sotorawcb(so); 157 int s; 158 159 if (req == PRU_ATTACH) { 160 MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK); 161 if ((so->so_pcb = rp) != NULL) 162 memset(so->so_pcb, 0, sizeof(*rp)); 163 164 } 165 if (req == PRU_DETACH && rp) 166 rt_adjustcount(rp->rcb_proto.sp_protocol, -1); 167 s = splsoftnet(); 168 169 /* 170 * Don't call raw_usrreq() in the attach case, because 171 * we want to allow non-privileged processes to listen on 172 * and send "safe" commands to the routing socket. 173 */ 174 if (req == PRU_ATTACH) { 175 if (p == 0) 176 error = EACCES; 177 else 178 error = raw_attach(so, (int)(long)nam); 179 } else 180 error = raw_usrreq(so, req, m, nam, control, p); 181 182 rp = sotorawcb(so); 183 if (req == PRU_ATTACH && rp) { 184 if (error) { 185 free((caddr_t)rp, M_PCB); 186 splx(s); 187 return (error); 188 } 189 rt_adjustcount(rp->rcb_proto.sp_protocol, 1); 190 rp->rcb_laddr = &route_src; 191 rp->rcb_faddr = &route_dst; 192 soisconnected(so); 193 so->so_options |= SO_USELOOPBACK; 194 } 195 splx(s); 196 return (error); 197 } 198 199 /*ARGSUSED*/ 200 int 201 #if __STDC__ 202 route_output(struct mbuf *m, ...) 203 #else 204 route_output(m, va_alist) 205 struct mbuf *m; 206 va_dcl 207 #endif 208 { 209 struct rt_msghdr *rtm = 0; 210 struct radix_node *rn = 0; 211 struct rtentry *rt = 0; 212 struct rtentry *saved_nrt = 0; 213 struct radix_node_head *rnh; 214 struct rt_addrinfo info; 215 int len, error = 0; 216 struct ifnet *ifp = 0; 217 struct ifaddr *ifa = 0; 218 struct socket *so; 219 va_list ap; 220 221 va_start(ap, m); 222 so = va_arg(ap, struct socket *); 223 va_end(ap); 224 225 #define senderr(e) do { error = e; goto flush;} while (0) 226 if (m == 0 || ((m->m_len < sizeof(int32_t)) && 227 (m = m_pullup(m, sizeof(int32_t))) == 0)) 228 return (ENOBUFS); 229 if ((m->m_flags & M_PKTHDR) == 0) 230 panic("route_output"); 231 len = m->m_pkthdr.len; 232 if (len < sizeof(*rtm) || 233 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 234 dst = 0; 235 senderr(EINVAL); 236 } 237 R_Malloc(rtm, struct rt_msghdr *, len); 238 if (rtm == 0) { 239 dst = 0; 240 senderr(ENOBUFS); 241 } 242 m_copydata(m, 0, len, (caddr_t)rtm); 243 if (rtm->rtm_version != RTM_VERSION) { 244 dst = 0; 245 senderr(EPROTONOSUPPORT); 246 } 247 rtm->rtm_pid = curproc->p_pid; 248 memset(&info, 0, sizeof(info)); 249 info.rti_addrs = rtm->rtm_addrs; 250 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) 251 senderr(EINVAL); 252 info.rti_flags = rtm->rtm_flags; 253 if (dst == 0 || (dst->sa_family >= AF_MAX)) 254 senderr(EINVAL); 255 if (gate != 0 && (gate->sa_family >= AF_MAX)) 256 senderr(EINVAL); 257 if (genmask) { 258 struct radix_node *t; 259 t = rn_addmask((caddr_t)genmask, 0, 1); 260 if (t && genmask->sa_len >= ((struct sockaddr *)t->rn_key)->sa_len && 261 Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1, 262 ((struct sockaddr *)t->rn_key)->sa_len) - 1) 263 genmask = (struct sockaddr *)(t->rn_key); 264 else 265 senderr(ENOBUFS); 266 } 267 268 /* 269 * Verify that the caller has the appropriate privilege; RTM_GET 270 * is the only operation the non-superuser is allowed. 271 */ 272 if (rtm->rtm_type != RTM_GET && 273 suser(curproc->p_ucred, &curproc->p_acflag) != 0) 274 senderr(EACCES); 275 276 switch (rtm->rtm_type) { 277 278 case RTM_ADD: 279 if (gate == 0) 280 senderr(EINVAL); 281 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt); 282 if (error == 0 && saved_nrt) { 283 rt_setmetrics(rtm->rtm_inits, 284 &rtm->rtm_rmx, &saved_nrt->rt_rmx); 285 saved_nrt->rt_refcnt--; 286 saved_nrt->rt_genmask = genmask; 287 } 288 break; 289 290 case RTM_DELETE: 291 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt); 292 if (error == 0) { 293 (rt = saved_nrt)->rt_refcnt++; 294 goto report; 295 } 296 break; 297 298 case RTM_GET: 299 case RTM_CHANGE: 300 case RTM_LOCK: 301 if ((rnh = rt_tables[dst->sa_family]) == 0) { 302 senderr(EAFNOSUPPORT); 303 } 304 rn = rnh->rnh_lookup(dst, netmask, rnh); 305 if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) { 306 senderr(ESRCH); 307 } 308 rt = (struct rtentry *)rn; 309 rt->rt_refcnt++; 310 311 switch(rtm->rtm_type) { 312 313 case RTM_GET: 314 report: 315 dst = rt_key(rt); 316 gate = rt->rt_gateway; 317 netmask = rt_mask(rt); 318 genmask = rt->rt_genmask; 319 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 320 if ((ifp = rt->rt_ifp) != NULL) { 321 ifpaddr = TAILQ_FIRST(&ifp->if_addrlist)->ifa_addr; 322 ifaaddr = rt->rt_ifa->ifa_addr; 323 if (ifp->if_flags & IFF_POINTOPOINT) 324 brdaddr = rt->rt_ifa->ifa_dstaddr; 325 else 326 brdaddr = 0; 327 rtm->rtm_index = ifp->if_index; 328 } else { 329 ifpaddr = 0; 330 ifaaddr = 0; 331 } 332 } 333 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0, 334 (struct walkarg *)0, &len); 335 if (len > rtm->rtm_msglen) { 336 struct rt_msghdr *new_rtm; 337 R_Malloc(new_rtm, struct rt_msghdr *, len); 338 if (new_rtm == 0) 339 senderr(ENOBUFS); 340 Bcopy(rtm, new_rtm, rtm->rtm_msglen); 341 Free(rtm); rtm = new_rtm; 342 } 343 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, 344 (struct walkarg *)0, 0); 345 rtm->rtm_flags = rt->rt_flags; 346 rtm->rtm_rmx = rt->rt_rmx; 347 rtm->rtm_addrs = info.rti_addrs; 348 break; 349 350 case RTM_CHANGE: 351 /* 352 * new gateway could require new ifaddr, ifp; 353 * flags may also be different; ifp may be specified 354 * by ll sockaddr when protocol address is ambiguous 355 */ 356 if ((error = rt_getifa(&info)) != 0) 357 senderr(error); 358 if (gate && rt_setgate(rt, rt_key(rt), gate)) 359 senderr(EDQUOT); 360 /* new gateway could require new ifaddr, ifp; 361 flags may also be different; ifp may be specified 362 by ll sockaddr when protocol address is ambiguous */ 363 if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) && 364 (ifp = ifa->ifa_ifp) && (ifaaddr || gate)) 365 ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate, 366 ifp); 367 else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) || 368 (gate && (ifa = ifa_ifwithroute(rt->rt_flags, 369 rt_key(rt), gate)))) 370 ifp = ifa->ifa_ifp; 371 if (ifa) { 372 struct ifaddr *oifa = rt->rt_ifa; 373 if (oifa != ifa) { 374 if (oifa && oifa->ifa_rtrequest) 375 oifa->ifa_rtrequest(RTM_DELETE, rt, 376 &info); 377 IFAFREE(rt->rt_ifa); 378 rt->rt_ifa = ifa; 379 IFAREF(rt->rt_ifa); 380 rt->rt_ifp = ifp; 381 } 382 } 383 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 384 &rt->rt_rmx); 385 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 386 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 387 if (genmask) 388 rt->rt_genmask = genmask; 389 /* 390 * Fall into 391 */ 392 case RTM_LOCK: 393 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 394 rt->rt_rmx.rmx_locks |= 395 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 396 break; 397 } 398 break; 399 400 default: 401 senderr(EOPNOTSUPP); 402 } 403 404 flush: 405 if (rtm) { 406 if (error) 407 rtm->rtm_errno = error; 408 else 409 rtm->rtm_flags |= RTF_DONE; 410 } 411 if (rt) 412 rtfree(rt); 413 { 414 struct rawcb *rp = 0; 415 /* 416 * Check to see if we don't want our own messages. 417 */ 418 if ((so->so_options & SO_USELOOPBACK) == 0) { 419 if (route_cb.any_count <= 1) { 420 if (rtm) 421 Free(rtm); 422 m_freem(m); 423 return (error); 424 } 425 /* There is another listener, so construct message */ 426 rp = sotorawcb(so); 427 } 428 if (rtm) { 429 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 430 if (m->m_pkthdr.len < rtm->rtm_msglen) { 431 m_freem(m); 432 m = NULL; 433 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 434 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 435 Free(rtm); 436 } 437 if (rp) 438 rp->rcb_proto.sp_family = 0; /* Avoid us */ 439 if (dst) 440 route_proto.sp_protocol = dst->sa_family; 441 if (m) 442 raw_input(m, &route_proto, &route_src, &route_dst); 443 if (rp) 444 rp->rcb_proto.sp_family = PF_ROUTE; 445 } 446 return (error); 447 } 448 449 void 450 rt_setmetrics(which, in, out) 451 u_long which; 452 struct rt_metrics *in, *out; 453 { 454 #define metric(f, e) if (which & (f)) out->e = in->e; 455 metric(RTV_RPIPE, rmx_recvpipe); 456 metric(RTV_SPIPE, rmx_sendpipe); 457 metric(RTV_SSTHRESH, rmx_ssthresh); 458 metric(RTV_RTT, rmx_rtt); 459 metric(RTV_RTTVAR, rmx_rttvar); 460 metric(RTV_HOPCOUNT, rmx_hopcount); 461 metric(RTV_MTU, rmx_mtu); 462 metric(RTV_EXPIRE, rmx_expire); 463 #undef metric 464 } 465 466 #define ROUNDUP(a) \ 467 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 468 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 469 470 static int 471 rt_xaddrs(cp, cplim, rtinfo) 472 caddr_t cp, cplim; 473 struct rt_addrinfo *rtinfo; 474 { 475 struct sockaddr *sa; 476 int i; 477 478 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 479 if ((rtinfo->rti_addrs & (1 << i)) == 0) 480 continue; 481 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 482 ADVANCE(cp, sa); 483 } 484 485 /* Check for extra addresses specified. */ 486 if ((rtinfo->rti_addrs & (~0 << i)) != 0) 487 return (1); 488 /* Check for bad data length. */ 489 if (cp != cplim) { 490 if (i == RTAX_NETMASK + 1 && 491 cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim) 492 /* 493 * The last sockaddr was netmask. 494 * We accept this for now for the sake of old 495 * binaries or third party softwares. 496 */ 497 ; 498 else 499 return (1); 500 } 501 return (0); 502 } 503 504 static struct mbuf * 505 rt_msg1(type, rtinfo, data, datalen) 506 int type; 507 struct rt_addrinfo *rtinfo; 508 caddr_t data; 509 int datalen; 510 { 511 struct rt_msghdr *rtm; 512 struct mbuf *m; 513 int i; 514 struct sockaddr *sa; 515 int len, dlen; 516 517 m = m_gethdr(M_DONTWAIT, MT_DATA); 518 if (m == 0) 519 return (m); 520 switch (type) { 521 522 case RTM_DELADDR: 523 case RTM_NEWADDR: 524 len = sizeof(struct ifa_msghdr); 525 break; 526 527 #ifdef COMPAT_14 528 case RTM_OIFINFO: 529 len = sizeof(struct if_msghdr14); 530 break; 531 #endif 532 533 case RTM_IFINFO: 534 len = sizeof(struct if_msghdr); 535 break; 536 537 case RTM_IFANNOUNCE: 538 len = sizeof(struct if_announcemsghdr); 539 break; 540 541 default: 542 len = sizeof(struct rt_msghdr); 543 } 544 if (len > MHLEN + MLEN) 545 panic("rt_msg1: message too long"); 546 else if (len > MHLEN) { 547 m->m_next = m_get(M_DONTWAIT, MT_DATA); 548 if (m->m_next == NULL) { 549 m_freem(m); 550 return (NULL); 551 } 552 m->m_pkthdr.len = len; 553 m->m_len = MHLEN; 554 m->m_next->m_len = len - MHLEN; 555 } else { 556 m->m_pkthdr.len = m->m_len = len; 557 } 558 m->m_pkthdr.rcvif = 0; 559 m_copyback(m, 0, datalen, data); 560 rtm = mtod(m, struct rt_msghdr *); 561 for (i = 0; i < RTAX_MAX; i++) { 562 if ((sa = rtinfo->rti_info[i]) == NULL) 563 continue; 564 rtinfo->rti_addrs |= (1 << i); 565 dlen = ROUNDUP(sa->sa_len); 566 m_copyback(m, len, dlen, (caddr_t)sa); 567 len += dlen; 568 } 569 if (m->m_pkthdr.len != len) { 570 m_freem(m); 571 return (NULL); 572 } 573 rtm->rtm_msglen = len; 574 rtm->rtm_version = RTM_VERSION; 575 rtm->rtm_type = type; 576 return (m); 577 } 578 579 /* 580 * rt_msg2 581 * 582 * fills 'cp' or 'w'.w_tmem with the routing socket message and 583 * returns the length of the message in 'lenp'. 584 * 585 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold 586 * the message 587 * otherwise walkarg's w_needed is updated and if the user buffer is 588 * specified and w_needed indicates space exists the information is copied 589 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary, 590 * if the allocation fails ENOBUFS is returned. 591 */ 592 static int 593 rt_msg2(type, rtinfo, cp, w, lenp) 594 int type; 595 struct rt_addrinfo *rtinfo; 596 caddr_t cp; 597 struct walkarg *w; 598 int *lenp; 599 { 600 int i; 601 int len, dlen, second_time = 0; 602 caddr_t cp0; 603 604 rtinfo->rti_addrs = 0; 605 again: 606 switch (type) { 607 608 case RTM_DELADDR: 609 case RTM_NEWADDR: 610 len = sizeof(struct ifa_msghdr); 611 break; 612 #ifdef COMPAT_14 613 case RTM_OIFINFO: 614 len = sizeof(struct if_msghdr14); 615 break; 616 #endif 617 618 case RTM_IFINFO: 619 len = sizeof(struct if_msghdr); 620 break; 621 622 default: 623 len = sizeof(struct rt_msghdr); 624 } 625 if ((cp0 = cp) != NULL) 626 cp += len; 627 for (i = 0; i < RTAX_MAX; i++) { 628 struct sockaddr *sa; 629 630 if ((sa = rtinfo->rti_info[i]) == 0) 631 continue; 632 rtinfo->rti_addrs |= (1 << i); 633 dlen = ROUNDUP(sa->sa_len); 634 if (cp) { 635 bcopy(sa, cp, (unsigned)dlen); 636 cp += dlen; 637 } 638 len += dlen; 639 } 640 if (cp == 0 && w != NULL && !second_time) { 641 struct walkarg *rw = w; 642 643 rw->w_needed += len; 644 if (rw->w_needed <= 0 && rw->w_where) { 645 if (rw->w_tmemsize < len) { 646 if (rw->w_tmem) 647 free(rw->w_tmem, M_RTABLE); 648 rw->w_tmem = (caddr_t) malloc(len, M_RTABLE, 649 M_NOWAIT); 650 if (rw->w_tmem) 651 rw->w_tmemsize = len; 652 } 653 if (rw->w_tmem) { 654 cp = rw->w_tmem; 655 second_time = 1; 656 goto again; 657 } else { 658 rw->w_tmemneeded = len; 659 return (ENOBUFS); 660 } 661 } 662 } 663 if (cp) { 664 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 665 666 rtm->rtm_version = RTM_VERSION; 667 rtm->rtm_type = type; 668 rtm->rtm_msglen = len; 669 } 670 if (lenp) 671 *lenp = len; 672 return (0); 673 } 674 675 /* 676 * This routine is called to generate a message from the routing 677 * socket indicating that a redirect has occurred, a routing lookup 678 * has failed, or that a protocol has detected timeouts to a particular 679 * destination. 680 */ 681 void 682 rt_missmsg(type, rtinfo, flags, error) 683 int type, flags, error; 684 struct rt_addrinfo *rtinfo; 685 { 686 struct rt_msghdr rtm; 687 struct mbuf *m; 688 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 689 690 if (route_cb.any_count == 0) 691 return; 692 memset(&rtm, 0, sizeof(rtm)); 693 rtm.rtm_flags = RTF_DONE | flags; 694 rtm.rtm_errno = error; 695 m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm)); 696 if (m == 0) 697 return; 698 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs; 699 route_proto.sp_protocol = sa ? sa->sa_family : 0; 700 raw_input(m, &route_proto, &route_src, &route_dst); 701 } 702 703 /* 704 * This routine is called to generate a message from the routing 705 * socket indicating that the status of a network interface has changed. 706 */ 707 void 708 rt_ifmsg(ifp) 709 struct ifnet *ifp; 710 { 711 struct if_msghdr ifm; 712 #ifdef COMPAT_14 713 struct if_msghdr14 oifm; 714 #endif 715 struct mbuf *m; 716 struct rt_addrinfo info; 717 718 if (route_cb.any_count == 0) 719 return; 720 memset(&info, 0, sizeof(info)); 721 memset(&ifm, 0, sizeof(ifm)); 722 ifm.ifm_index = ifp->if_index; 723 ifm.ifm_flags = ifp->if_flags; 724 ifm.ifm_data = ifp->if_data; 725 ifm.ifm_addrs = 0; 726 m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm)); 727 if (m == 0) 728 return; 729 route_proto.sp_protocol = 0; 730 raw_input(m, &route_proto, &route_src, &route_dst); 731 #ifdef COMPAT_14 732 memset(&info, 0, sizeof(info)); 733 memset(&oifm, 0, sizeof(oifm)); 734 oifm.ifm_index = ifp->if_index; 735 oifm.ifm_flags = ifp->if_flags; 736 oifm.ifm_data.ifi_type = ifp->if_data.ifi_type; 737 oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen; 738 oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen; 739 oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu; 740 oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric; 741 oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate; 742 oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets; 743 oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors; 744 oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets; 745 oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors; 746 oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions; 747 oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes; 748 oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes; 749 oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts; 750 oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts; 751 oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops; 752 oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto; 753 oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange; 754 oifm.ifm_addrs = 0; 755 m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm)); 756 if (m == 0) 757 return; 758 route_proto.sp_protocol = 0; 759 raw_input(m, &route_proto, &route_src, &route_dst); 760 #endif 761 } 762 763 /* 764 * This is called to generate messages from the routing socket 765 * indicating a network interface has had addresses associated with it. 766 * if we ever reverse the logic and replace messages TO the routing 767 * socket indicate a request to configure interfaces, then it will 768 * be unnecessary as the routing socket will automatically generate 769 * copies of it. 770 */ 771 void 772 rt_newaddrmsg(cmd, ifa, error, rt) 773 int cmd, error; 774 struct ifaddr *ifa; 775 struct rtentry *rt; 776 { 777 struct rt_addrinfo info; 778 struct sockaddr *sa = NULL; 779 int pass; 780 struct mbuf *m = NULL; 781 struct ifnet *ifp = ifa->ifa_ifp; 782 783 if (route_cb.any_count == 0) 784 return; 785 for (pass = 1; pass < 3; pass++) { 786 memset(&info, 0, sizeof(info)); 787 if ((cmd == RTM_ADD && pass == 1) || 788 (cmd == RTM_DELETE && pass == 2)) { 789 struct ifa_msghdr ifam; 790 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 791 792 ifaaddr = sa = ifa->ifa_addr; 793 ifpaddr = TAILQ_FIRST(&ifp->if_addrlist)->ifa_addr; 794 netmask = ifa->ifa_netmask; 795 brdaddr = ifa->ifa_dstaddr; 796 memset(&ifam, 0, sizeof(ifam)); 797 ifam.ifam_index = ifp->if_index; 798 ifam.ifam_metric = ifa->ifa_metric; 799 ifam.ifam_flags = ifa->ifa_flags; 800 m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam)); 801 if (m == NULL) 802 continue; 803 mtod(m, struct ifa_msghdr *)->ifam_addrs = 804 info.rti_addrs; 805 } 806 if ((cmd == RTM_ADD && pass == 2) || 807 (cmd == RTM_DELETE && pass == 1)) { 808 struct rt_msghdr rtm; 809 810 if (rt == 0) 811 continue; 812 netmask = rt_mask(rt); 813 dst = sa = rt_key(rt); 814 gate = rt->rt_gateway; 815 memset(&rtm, 0, sizeof(rtm)); 816 rtm.rtm_index = ifp->if_index; 817 rtm.rtm_flags |= rt->rt_flags; 818 rtm.rtm_errno = error; 819 m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm)); 820 if (m == NULL) 821 continue; 822 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs; 823 } 824 route_proto.sp_protocol = sa ? sa->sa_family : 0; 825 raw_input(m, &route_proto, &route_src, &route_dst); 826 } 827 } 828 829 /* 830 * This is called to generate routing socket messages indicating 831 * network interface arrival and departure. 832 */ 833 void 834 rt_ifannouncemsg(ifp, what) 835 struct ifnet *ifp; 836 int what; 837 { 838 struct if_announcemsghdr ifan; 839 struct mbuf *m; 840 struct rt_addrinfo info; 841 842 if (route_cb.any_count == 0) 843 return; 844 memset(&info, 0, sizeof(info)); 845 memset(&ifan, 0, sizeof(ifan)); 846 ifan.ifan_index = ifp->if_index; 847 strcpy(ifan.ifan_name, ifp->if_xname); 848 ifan.ifan_what = what; 849 m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan)); 850 if (m == 0) 851 return; 852 route_proto.sp_protocol = 0; 853 raw_input(m, &route_proto, &route_src, &route_dst); 854 } 855 856 /* 857 * This is used in dumping the kernel table via sysctl(). 858 */ 859 static int 860 sysctl_dumpentry(rn, v) 861 struct radix_node *rn; 862 void *v; 863 { 864 struct walkarg *w = v; 865 struct rtentry *rt = (struct rtentry *)rn; 866 int error = 0, size; 867 struct rt_addrinfo info; 868 869 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 870 return 0; 871 memset(&info, 0, sizeof(info)); 872 dst = rt_key(rt); 873 gate = rt->rt_gateway; 874 netmask = rt_mask(rt); 875 genmask = rt->rt_genmask; 876 if (rt->rt_ifp) { 877 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr; 878 ifaaddr = rt->rt_ifa->ifa_addr; 879 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 880 brdaddr = rt->rt_ifa->ifa_dstaddr; 881 } 882 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size))) 883 return (error); 884 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 885 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 886 887 rtm->rtm_flags = rt->rt_flags; 888 rtm->rtm_use = rt->rt_use; 889 rtm->rtm_rmx = rt->rt_rmx; 890 rtm->rtm_index = rt->rt_ifp->if_index; 891 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 892 rtm->rtm_addrs = info.rti_addrs; 893 if ((error = copyout(rtm, w->w_where, size)) != 0) 894 w->w_where = NULL; 895 else 896 w->w_where += size; 897 } 898 return (error); 899 } 900 901 static int 902 sysctl_iflist(af, w, type) 903 int af; 904 struct walkarg *w; 905 int type; 906 { 907 struct ifnet *ifp; 908 struct ifaddr *ifa; 909 struct rt_addrinfo info; 910 int len, error = 0; 911 912 memset(&info, 0, sizeof(info)); 913 TAILQ_FOREACH(ifp, &ifnet, if_list) { 914 if (w->w_arg && w->w_arg != ifp->if_index) 915 continue; 916 ifa = TAILQ_FIRST(&ifp->if_addrlist); 917 ifpaddr = ifa->ifa_addr; 918 switch(type) { 919 case NET_RT_IFLIST: 920 error = 921 rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len); 922 break; 923 #ifdef COMPAT_14 924 case NET_RT_OIFLIST: 925 error = 926 rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len); 927 break; 928 #endif 929 default: 930 panic("sysctl_iflist(1)"); 931 } 932 if (error) 933 return (error); 934 ifpaddr = 0; 935 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 936 switch(type) { 937 case NET_RT_IFLIST: { 938 struct if_msghdr *ifm; 939 940 ifm = (struct if_msghdr *)w->w_tmem; 941 ifm->ifm_index = ifp->if_index; 942 ifm->ifm_flags = ifp->if_flags; 943 ifm->ifm_data = ifp->if_data; 944 ifm->ifm_addrs = info.rti_addrs; 945 error = copyout(ifm, w->w_where, len); 946 if (error) 947 return (error); 948 w->w_where += len; 949 break; 950 } 951 952 #ifdef COMPAT_14 953 case NET_RT_OIFLIST: { 954 struct if_msghdr14 *ifm; 955 956 ifm = (struct if_msghdr14 *)w->w_tmem; 957 ifm->ifm_index = ifp->if_index; 958 ifm->ifm_flags = ifp->if_flags; 959 ifm->ifm_data.ifi_type = ifp->if_data.ifi_type; 960 ifm->ifm_data.ifi_addrlen = 961 ifp->if_data.ifi_addrlen; 962 ifm->ifm_data.ifi_hdrlen = 963 ifp->if_data.ifi_hdrlen; 964 ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu; 965 ifm->ifm_data.ifi_metric = 966 ifp->if_data.ifi_metric; 967 ifm->ifm_data.ifi_baudrate = 968 ifp->if_data.ifi_baudrate; 969 ifm->ifm_data.ifi_ipackets = 970 ifp->if_data.ifi_ipackets; 971 ifm->ifm_data.ifi_ierrors = 972 ifp->if_data.ifi_ierrors; 973 ifm->ifm_data.ifi_opackets = 974 ifp->if_data.ifi_opackets; 975 ifm->ifm_data.ifi_oerrors = 976 ifp->if_data.ifi_oerrors; 977 ifm->ifm_data.ifi_collisions = 978 ifp->if_data.ifi_collisions; 979 ifm->ifm_data.ifi_ibytes = 980 ifp->if_data.ifi_ibytes; 981 ifm->ifm_data.ifi_obytes = 982 ifp->if_data.ifi_obytes; 983 ifm->ifm_data.ifi_imcasts = 984 ifp->if_data.ifi_imcasts; 985 ifm->ifm_data.ifi_omcasts = 986 ifp->if_data.ifi_omcasts; 987 ifm->ifm_data.ifi_iqdrops = 988 ifp->if_data.ifi_iqdrops; 989 ifm->ifm_data.ifi_noproto = 990 ifp->if_data.ifi_noproto; 991 ifm->ifm_data.ifi_lastchange = 992 ifp->if_data.ifi_lastchange; 993 ifm->ifm_addrs = info.rti_addrs; 994 error = copyout(ifm, w->w_where, len); 995 if (error) 996 return (error); 997 w->w_where += len; 998 break; 999 } 1000 #endif 1001 default: 1002 panic("sysctl_iflist(2)"); 1003 } 1004 } 1005 while ((ifa = TAILQ_NEXT(ifa, ifa_list)) != NULL) { 1006 if (af && af != ifa->ifa_addr->sa_family) 1007 continue; 1008 ifaaddr = ifa->ifa_addr; 1009 netmask = ifa->ifa_netmask; 1010 brdaddr = ifa->ifa_dstaddr; 1011 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len))) 1012 return (error); 1013 if (w->w_where && w->w_tmem && w->w_needed <= 0) { 1014 struct ifa_msghdr *ifam; 1015 1016 ifam = (struct ifa_msghdr *)w->w_tmem; 1017 ifam->ifam_index = ifa->ifa_ifp->if_index; 1018 ifam->ifam_flags = ifa->ifa_flags; 1019 ifam->ifam_metric = ifa->ifa_metric; 1020 ifam->ifam_addrs = info.rti_addrs; 1021 error = copyout(w->w_tmem, w->w_where, len); 1022 if (error) 1023 return (error); 1024 w->w_where += len; 1025 } 1026 } 1027 ifaaddr = netmask = brdaddr = 0; 1028 } 1029 return (0); 1030 } 1031 1032 static int 1033 sysctl_rtable(name, namelen, where, given, new, newlen) 1034 int *name; 1035 u_int namelen; 1036 void *where; 1037 size_t *given; 1038 void *new; 1039 size_t newlen; 1040 { 1041 struct radix_node_head *rnh; 1042 int i, s, error = EINVAL; 1043 u_char af; 1044 struct walkarg w; 1045 1046 if (new) 1047 return (EPERM); 1048 if (namelen != 3) 1049 return (EINVAL); 1050 af = name[0]; 1051 w.w_tmemneeded = 0; 1052 w.w_tmemsize = 0; 1053 w.w_tmem = NULL; 1054 again: 1055 /* we may return here if a later [re]alloc of the t_mem buffer fails */ 1056 if (w.w_tmemneeded) { 1057 w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK); 1058 w.w_tmemsize = w.w_tmemneeded; 1059 w.w_tmemneeded = 0; 1060 } 1061 w.w_op = name[1]; 1062 w.w_arg = name[2]; 1063 w.w_given = *given; 1064 w.w_needed = 0 - w.w_given; 1065 w.w_where = where; 1066 1067 s = splsoftnet(); 1068 switch (w.w_op) { 1069 1070 case NET_RT_DUMP: 1071 case NET_RT_FLAGS: 1072 for (i = 1; i <= AF_MAX; i++) 1073 if ((rnh = rt_tables[i]) && (af == 0 || af == i) && 1074 (error = (*rnh->rnh_walktree)(rnh, 1075 sysctl_dumpentry, &w))) 1076 break; 1077 break; 1078 1079 #ifdef COMPAT_14 1080 case NET_RT_OIFLIST: 1081 error = sysctl_iflist(af, &w, w.w_op); 1082 break; 1083 #endif 1084 1085 case NET_RT_IFLIST: 1086 error = sysctl_iflist(af, &w, w.w_op); 1087 } 1088 splx(s); 1089 1090 /* check to see if we couldn't allocate memory with NOWAIT */ 1091 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded) 1092 goto again; 1093 1094 if (w.w_tmem) 1095 free(w.w_tmem, M_RTABLE); 1096 w.w_needed += w.w_given; 1097 if (where) { 1098 *given = w.w_where - (caddr_t) where; 1099 if (*given < w.w_needed) 1100 return (ENOMEM); 1101 } else { 1102 *given = (11 * w.w_needed) / 10; 1103 } 1104 return (error); 1105 } 1106 1107 /* 1108 * Definitions of protocols supported in the ROUTE domain. 1109 */ 1110 1111 extern struct domain routedomain; /* or at least forward */ 1112 1113 struct protosw routesw[] = { 1114 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 1115 raw_input, route_output, raw_ctlinput, 0, 1116 route_usrreq, 1117 raw_init, 0, 0, 0, 1118 sysctl_rtable, 1119 } 1120 }; 1121 1122 struct domain routedomain = 1123 { PF_ROUTE, "route", route_init, 0, 0, 1124 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 1125