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