| 1 | /* |
| 2 | * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved. |
| 3 | * |
| 4 | * This code is derived from software contributed to The DragonFly Project |
| 5 | * by Jeffrey M. Hsu. |
| 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 DragonFly Project nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific, prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Copyright (c) 2004, 2005 Jeffrey M. Hsu. All rights reserved. |
| 35 | * |
| 36 | * License terms: all terms for the DragonFly license above plus the following: |
| 37 | * |
| 38 | * 4. All advertising materials mentioning features or use of this software |
| 39 | * must display the following acknowledgement: |
| 40 | * |
| 41 | * This product includes software developed by Jeffrey M. Hsu |
| 42 | * for the DragonFly Project. |
| 43 | * |
| 44 | * This requirement may be waived with permission from Jeffrey Hsu. |
| 45 | * Permission will be granted to any DragonFly user for free. |
| 46 | * This requirement will sunset and may be removed on Jan 31, 2006, |
| 47 | * after which the standard DragonFly license (as shown above) will |
| 48 | * apply. |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * Copyright (c) 1988, 1991, 1993 |
| 53 | * The Regents of the University of California. All rights reserved. |
| 54 | * |
| 55 | * Redistribution and use in source and binary forms, with or without |
| 56 | * modification, are permitted provided that the following conditions |
| 57 | * are met: |
| 58 | * 1. Redistributions of source code must retain the above copyright |
| 59 | * notice, this list of conditions and the following disclaimer. |
| 60 | * 2. Redistributions in binary form must reproduce the above copyright |
| 61 | * notice, this list of conditions and the following disclaimer in the |
| 62 | * documentation and/or other materials provided with the distribution. |
| 63 | * 3. All advertising materials mentioning features or use of this software |
| 64 | * must display the following acknowledgement: |
| 65 | * This product includes software developed by the University of |
| 66 | * California, Berkeley and its contributors. |
| 67 | * 4. Neither the name of the University nor the names of its contributors |
| 68 | * may be used to endorse or promote products derived from this software |
| 69 | * without specific prior written permission. |
| 70 | * |
| 71 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 72 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 73 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 74 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 75 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 76 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 77 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 78 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 79 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 80 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 81 | * SUCH DAMAGE. |
| 82 | * |
| 83 | * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 |
| 84 | * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $ |
| 85 | * $DragonFly: src/sys/net/rtsock.c,v 1.29 2005/07/15 17:54:47 eirikn Exp $ |
| 86 | */ |
| 87 | |
| 88 | #include "opt_sctp.h" |
| 89 | |
| 90 | #include <sys/param.h> |
| 91 | #include <sys/systm.h> |
| 92 | #include <sys/kernel.h> |
| 93 | #include <sys/sysctl.h> |
| 94 | #include <sys/proc.h> |
| 95 | #include <sys/malloc.h> |
| 96 | #include <sys/mbuf.h> |
| 97 | #include <sys/protosw.h> |
| 98 | #include <sys/socket.h> |
| 99 | #include <sys/socketvar.h> |
| 100 | #include <sys/domain.h> |
| 101 | #include <sys/thread2.h> |
| 102 | |
| 103 | #include <net/if.h> |
| 104 | #include <net/route.h> |
| 105 | #include <net/raw_cb.h> |
| 106 | |
| 107 | #ifdef SCTP |
| 108 | extern void sctp_add_ip_address(struct ifaddr *ifa); |
| 109 | extern void sctp_delete_ip_address(struct ifaddr *ifa); |
| 110 | #endif /* SCTP */ |
| 111 | |
| 112 | MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); |
| 113 | |
| 114 | static struct route_cb { |
| 115 | int ip_count; |
| 116 | int ip6_count; |
| 117 | int ipx_count; |
| 118 | int ns_count; |
| 119 | int any_count; |
| 120 | } route_cb; |
| 121 | |
| 122 | static const struct sockaddr route_src = { 2, PF_ROUTE, }; |
| 123 | |
| 124 | struct walkarg { |
| 125 | int w_tmemsize; |
| 126 | int w_op, w_arg; |
| 127 | void *w_tmem; |
| 128 | struct sysctl_req *w_req; |
| 129 | }; |
| 130 | |
| 131 | static struct mbuf * |
| 132 | rt_msg_mbuf (int, struct rt_addrinfo *); |
| 133 | static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len); |
| 134 | static int rt_msgsize (int type, struct rt_addrinfo *rtinfo); |
| 135 | static int rt_xaddrs (char *, char *, struct rt_addrinfo *); |
| 136 | static int sysctl_dumpentry (struct radix_node *rn, void *vw); |
| 137 | static int sysctl_iflist (int af, struct walkarg *w); |
| 138 | static int route_output(struct mbuf *, struct socket *, ...); |
| 139 | static void rt_setmetrics (u_long, struct rt_metrics *, |
| 140 | struct rt_metrics *); |
| 141 | |
| 142 | /* |
| 143 | * It really doesn't make any sense at all for this code to share much |
| 144 | * with raw_usrreq.c, since its functionality is so restricted. XXX |
| 145 | */ |
| 146 | static int |
| 147 | rts_abort(struct socket *so) |
| 148 | { |
| 149 | int error; |
| 150 | |
| 151 | crit_enter(); |
| 152 | error = raw_usrreqs.pru_abort(so); |
| 153 | crit_exit(); |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | /* pru_accept is EOPNOTSUPP */ |
| 158 | |
| 159 | static int |
| 160 | rts_attach(struct socket *so, int proto, struct pru_attach_info *ai) |
| 161 | { |
| 162 | struct rawcb *rp; |
| 163 | int error; |
| 164 | |
| 165 | if (sotorawcb(so) != NULL) |
| 166 | return EISCONN; /* XXX panic? */ |
| 167 | |
| 168 | rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); |
| 169 | if (rp == NULL) |
| 170 | return ENOBUFS; |
| 171 | |
| 172 | /* |
| 173 | * The critical section is necessary to block protocols from sending |
| 174 | * error notifications (like RTM_REDIRECT or RTM_LOSING) while |
| 175 | * this PCB is extant but incompletely initialized. |
| 176 | * Probably we should try to do more of this work beforehand and |
| 177 | * eliminate the critical section. |
| 178 | */ |
| 179 | crit_enter(); |
| 180 | so->so_pcb = rp; |
| 181 | error = raw_attach(so, proto, ai->sb_rlimit); |
| 182 | rp = sotorawcb(so); |
| 183 | if (error) { |
| 184 | crit_exit(); |
| 185 | free(rp, M_PCB); |
| 186 | return error; |
| 187 | } |
| 188 | switch(rp->rcb_proto.sp_protocol) { |
| 189 | case AF_INET: |
| 190 | route_cb.ip_count++; |
| 191 | break; |
| 192 | case AF_INET6: |
| 193 | route_cb.ip6_count++; |
| 194 | break; |
| 195 | case AF_IPX: |
| 196 | route_cb.ipx_count++; |
| 197 | break; |
| 198 | case AF_NS: |
| 199 | route_cb.ns_count++; |
| 200 | break; |
| 201 | } |
| 202 | rp->rcb_faddr = &route_src; |
| 203 | route_cb.any_count++; |
| 204 | soisconnected(so); |
| 205 | so->so_options |= SO_USELOOPBACK; |
| 206 | crit_exit(); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int |
| 211 | rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) |
| 212 | { |
| 213 | int error; |
| 214 | |
| 215 | crit_enter(); |
| 216 | error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */ |
| 217 | crit_exit(); |
| 218 | return error; |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) |
| 223 | { |
| 224 | int error; |
| 225 | |
| 226 | crit_enter(); |
| 227 | error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */ |
| 228 | crit_exit(); |
| 229 | return error; |
| 230 | } |
| 231 | |
| 232 | /* pru_connect2 is EOPNOTSUPP */ |
| 233 | /* pru_control is EOPNOTSUPP */ |
| 234 | |
| 235 | static int |
| 236 | rts_detach(struct socket *so) |
| 237 | { |
| 238 | struct rawcb *rp = sotorawcb(so); |
| 239 | int error; |
| 240 | |
| 241 | crit_enter(); |
| 242 | if (rp != NULL) { |
| 243 | switch(rp->rcb_proto.sp_protocol) { |
| 244 | case AF_INET: |
| 245 | route_cb.ip_count--; |
| 246 | break; |
| 247 | case AF_INET6: |
| 248 | route_cb.ip6_count--; |
| 249 | break; |
| 250 | case AF_IPX: |
| 251 | route_cb.ipx_count--; |
| 252 | break; |
| 253 | case AF_NS: |
| 254 | route_cb.ns_count--; |
| 255 | break; |
| 256 | } |
| 257 | route_cb.any_count--; |
| 258 | } |
| 259 | error = raw_usrreqs.pru_detach(so); |
| 260 | crit_exit(); |
| 261 | return error; |
| 262 | } |
| 263 | |
| 264 | static int |
| 265 | rts_disconnect(struct socket *so) |
| 266 | { |
| 267 | int error; |
| 268 | |
| 269 | crit_enter(); |
| 270 | error = raw_usrreqs.pru_disconnect(so); |
| 271 | crit_exit(); |
| 272 | return error; |
| 273 | } |
| 274 | |
| 275 | /* pru_listen is EOPNOTSUPP */ |
| 276 | |
| 277 | static int |
| 278 | rts_peeraddr(struct socket *so, struct sockaddr **nam) |
| 279 | { |
| 280 | int error; |
| 281 | |
| 282 | crit_enter(); |
| 283 | error = raw_usrreqs.pru_peeraddr(so, nam); |
| 284 | crit_exit(); |
| 285 | return error; |
| 286 | } |
| 287 | |
| 288 | /* pru_rcvd is EOPNOTSUPP */ |
| 289 | /* pru_rcvoob is EOPNOTSUPP */ |
| 290 | |
| 291 | static int |
| 292 | rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, |
| 293 | struct mbuf *control, struct thread *td) |
| 294 | { |
| 295 | int error; |
| 296 | |
| 297 | crit_enter(); |
| 298 | error = raw_usrreqs.pru_send(so, flags, m, nam, control, td); |
| 299 | crit_exit(); |
| 300 | return error; |
| 301 | } |
| 302 | |
| 303 | /* pru_sense is null */ |
| 304 | |
| 305 | static int |
| 306 | rts_shutdown(struct socket *so) |
| 307 | { |
| 308 | int error; |
| 309 | |
| 310 | crit_enter(); |
| 311 | error = raw_usrreqs.pru_shutdown(so); |
| 312 | crit_exit(); |
| 313 | return error; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | rts_sockaddr(struct socket *so, struct sockaddr **nam) |
| 318 | { |
| 319 | int error; |
| 320 | |
| 321 | crit_enter(); |
| 322 | error = raw_usrreqs.pru_sockaddr(so, nam); |
| 323 | crit_exit(); |
| 324 | return error; |
| 325 | } |
| 326 | |
| 327 | static struct pr_usrreqs route_usrreqs = { |
| 328 | rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect, |
| 329 | pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect, |
| 330 | pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp, |
| 331 | rts_send, pru_sense_null, rts_shutdown, rts_sockaddr, |
| 332 | sosend, soreceive, sopoll |
| 333 | }; |
| 334 | |
| 335 | static __inline sa_family_t |
| 336 | familyof(struct sockaddr *sa) |
| 337 | { |
| 338 | return (sa != NULL ? sa->sa_family : 0); |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | rts_input(struct mbuf *m, sa_family_t family) |
| 343 | { |
| 344 | static const struct sockaddr route_dst = { 2, PF_ROUTE, }; |
| 345 | struct sockproto route_proto = { PF_ROUTE, family }; |
| 346 | |
| 347 | raw_input(m, &route_proto, &route_src, &route_dst); |
| 348 | } |
| 349 | |
| 350 | static void * |
| 351 | reallocbuf(void *ptr, size_t len, size_t olen) |
| 352 | { |
| 353 | void *newptr; |
| 354 | |
| 355 | newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); |
| 356 | if (newptr == NULL) |
| 357 | return NULL; |
| 358 | bcopy(ptr, newptr, olen); |
| 359 | free(ptr, M_RTABLE); |
| 360 | return (newptr); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Internal helper routine for route_output(). |
| 365 | */ |
| 366 | static int |
| 367 | fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt, |
| 368 | struct rt_addrinfo *rtinfo) |
| 369 | { |
| 370 | int msglen; |
| 371 | struct rt_msghdr *rtm = *prtm; |
| 372 | |
| 373 | /* Fill in rt_addrinfo for call to rt_msg_buffer(). */ |
| 374 | rtinfo->rti_dst = rt_key(rt); |
| 375 | rtinfo->rti_gateway = rt->rt_gateway; |
| 376 | rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */ |
| 377 | rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */ |
| 378 | if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { |
| 379 | if (rt->rt_ifp != NULL) { |
| 380 | rtinfo->rti_ifpaddr = |
| 381 | TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr; |
| 382 | rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr; |
| 383 | if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) |
| 384 | rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; |
| 385 | rtm->rtm_index = rt->rt_ifp->if_index; |
| 386 | } else { |
| 387 | rtinfo->rti_ifpaddr = NULL; |
| 388 | rtinfo->rti_ifaaddr = NULL; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | msglen = rt_msgsize(rtm->rtm_type, rtinfo); |
| 393 | if (rtm->rtm_msglen < msglen) { |
| 394 | rtm = reallocbuf(rtm, msglen, rtm->rtm_msglen); |
| 395 | if (rtm == NULL) |
| 396 | return (ENOBUFS); |
| 397 | *prtm = rtm; |
| 398 | } |
| 399 | rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen); |
| 400 | |
| 401 | rtm->rtm_flags = rt->rt_flags; |
| 402 | rtm->rtm_rmx = rt->rt_rmx; |
| 403 | rtm->rtm_addrs = rtinfo->rti_addrs; |
| 404 | |
| 405 | return (0); |
| 406 | } |
| 407 | |
| 408 | /*ARGSUSED*/ |
| 409 | static int |
| 410 | route_output(struct mbuf *m, struct socket *so, ...) |
| 411 | { |
| 412 | struct rt_msghdr *rtm = NULL; |
| 413 | struct rtentry *rt = NULL; |
| 414 | struct rtentry *saved_nrt = NULL; |
| 415 | struct radix_node_head *rnh; |
| 416 | struct ifaddr *ifa = NULL; |
| 417 | struct rawcb *rp = NULL; |
| 418 | struct pr_output_info *oi; |
| 419 | struct rt_addrinfo rtinfo; |
| 420 | int len, error = 0; |
| 421 | __va_list ap; |
| 422 | |
| 423 | __va_start(ap, so); |
| 424 | oi = __va_arg(ap, struct pr_output_info *); |
| 425 | __va_end(ap); |
| 426 | |
| 427 | #define gotoerr(e) { error = e; goto flush;} |
| 428 | |
| 429 | if (m == NULL || |
| 430 | (m->m_len < sizeof(long) && |
| 431 | (m = m_pullup(m, sizeof(long))) == NULL)) |
| 432 | return (ENOBUFS); |
| 433 | if (!(m->m_flags & M_PKTHDR)) |
| 434 | panic("route_output"); |
| 435 | len = m->m_pkthdr.len; |
| 436 | if (len < sizeof(struct rt_msghdr) || |
| 437 | len != mtod(m, struct rt_msghdr *)->rtm_msglen) { |
| 438 | rtinfo.rti_dst = NULL; |
| 439 | gotoerr(EINVAL); |
| 440 | } |
| 441 | rtm = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); |
| 442 | if (rtm == NULL) { |
| 443 | rtinfo.rti_dst = NULL; |
| 444 | gotoerr(ENOBUFS); |
| 445 | } |
| 446 | m_copydata(m, 0, len, (caddr_t)rtm); |
| 447 | if (rtm->rtm_version != RTM_VERSION) { |
| 448 | rtinfo.rti_dst = NULL; |
| 449 | gotoerr(EPROTONOSUPPORT); |
| 450 | } |
| 451 | rtm->rtm_pid = oi->p_pid; |
| 452 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 453 | rtinfo.rti_addrs = rtm->rtm_addrs; |
| 454 | if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) { |
| 455 | rtinfo.rti_dst = NULL; |
| 456 | gotoerr(EINVAL); |
| 457 | } |
| 458 | rtinfo.rti_flags = rtm->rtm_flags; |
| 459 | if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX || |
| 460 | (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX)) |
| 461 | gotoerr(EINVAL); |
| 462 | |
| 463 | if (rtinfo.rti_genmask != NULL) { |
| 464 | struct radix_node *n; |
| 465 | |
| 466 | #define clen(s) (*(u_char *)(s)) |
| 467 | n = rn_addmask((char *)rtinfo.rti_genmask, TRUE, 1); |
| 468 | if (n != NULL && |
| 469 | rtinfo.rti_genmask->sa_len >= clen(n->rn_key) && |
| 470 | bcmp((char *)rtinfo.rti_genmask + 1, |
| 471 | (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) |
| 472 | rtinfo.rti_genmask = (struct sockaddr *)n->rn_key; |
| 473 | else |
| 474 | gotoerr(ENOBUFS); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Verify that the caller has the appropriate privilege; RTM_GET |
| 479 | * is the only operation the non-superuser is allowed. |
| 480 | */ |
| 481 | if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0) |
| 482 | gotoerr(EPERM); |
| 483 | |
| 484 | switch (rtm->rtm_type) { |
| 485 | case RTM_ADD: |
| 486 | if (rtinfo.rti_gateway == NULL) |
| 487 | gotoerr(EINVAL); |
| 488 | error = rtrequest1(RTM_ADD, &rtinfo, &saved_nrt); |
| 489 | if (error == 0 && saved_nrt != NULL) { |
| 490 | rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, |
| 491 | &saved_nrt->rt_rmx); |
| 492 | saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); |
| 493 | saved_nrt->rt_rmx.rmx_locks |= |
| 494 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); |
| 495 | --saved_nrt->rt_refcnt; |
| 496 | saved_nrt->rt_genmask = rtinfo.rti_genmask; |
| 497 | } |
| 498 | break; |
| 499 | case RTM_DELETE: |
| 500 | error = rtrequest1(RTM_DELETE, &rtinfo, &saved_nrt); |
| 501 | if (error == 0) { |
| 502 | if ((rt = saved_nrt)) |
| 503 | rt->rt_refcnt++; |
| 504 | if (fillrtmsg(&rtm, rt, &rtinfo) != 0) |
| 505 | gotoerr(ENOBUFS); |
| 506 | } |
| 507 | break; |
| 508 | case RTM_GET: |
| 509 | case RTM_CHANGE: |
| 510 | case RTM_LOCK: |
| 511 | if ((rnh = rt_tables[rtinfo.rti_dst->sa_family]) == NULL) |
| 512 | gotoerr(EAFNOSUPPORT); |
| 513 | rt = (struct rtentry *) |
| 514 | rnh->rnh_lookup((char *)rtinfo.rti_dst, |
| 515 | (char *)rtinfo.rti_netmask, rnh); |
| 516 | if (rt == NULL) |
| 517 | gotoerr(ESRCH); |
| 518 | rt->rt_refcnt++; |
| 519 | |
| 520 | switch(rtm->rtm_type) { |
| 521 | case RTM_GET: |
| 522 | if (fillrtmsg(&rtm, rt, &rtinfo) != 0) |
| 523 | gotoerr(ENOBUFS); |
| 524 | break; |
| 525 | case RTM_CHANGE: |
| 526 | /* |
| 527 | * new gateway could require new ifaddr, ifp; |
| 528 | * flags may also be different; ifp may be specified |
| 529 | * by ll sockaddr when protocol address is ambiguous |
| 530 | */ |
| 531 | if (((rt->rt_flags & RTF_GATEWAY) && |
| 532 | rtinfo.rti_gateway != NULL) || |
| 533 | rtinfo.rti_ifpaddr != NULL || |
| 534 | (rtinfo.rti_ifaaddr != NULL && |
| 535 | sa_equal(rtinfo.rti_ifaaddr, |
| 536 | rt->rt_ifa->ifa_addr))) { |
| 537 | error = rt_getifa(&rtinfo); |
| 538 | if (error != 0) |
| 539 | gotoerr(error); |
| 540 | } |
| 541 | if (rtinfo.rti_gateway != NULL) { |
| 542 | error = rt_setgate(rt, rt_key(rt), |
| 543 | rtinfo.rti_gateway); |
| 544 | if (error != 0) |
| 545 | gotoerr(error); |
| 546 | } |
| 547 | if ((ifa = rtinfo.rti_ifa) != NULL) { |
| 548 | struct ifaddr *oifa = rt->rt_ifa; |
| 549 | |
| 550 | if (oifa != ifa) { |
| 551 | if (oifa && oifa->ifa_rtrequest) |
| 552 | oifa->ifa_rtrequest(RTM_DELETE, |
| 553 | rt, &rtinfo); |
| 554 | IFAFREE(rt->rt_ifa); |
| 555 | IFAREF(ifa); |
| 556 | rt->rt_ifa = ifa; |
| 557 | rt->rt_ifp = rtinfo.rti_ifp; |
| 558 | } |
| 559 | } |
| 560 | rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, |
| 561 | &rt->rt_rmx); |
| 562 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) |
| 563 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &rtinfo); |
| 564 | if (rtinfo.rti_genmask != NULL) |
| 565 | rt->rt_genmask = rtinfo.rti_genmask; |
| 566 | /* |
| 567 | * Fall into |
| 568 | */ |
| 569 | case RTM_LOCK: |
| 570 | rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); |
| 571 | rt->rt_rmx.rmx_locks |= |
| 572 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | break; |
| 577 | default: |
| 578 | gotoerr(EOPNOTSUPP); |
| 579 | } |
| 580 | |
| 581 | flush: |
| 582 | if (rtm != NULL) { |
| 583 | if (error != 0) |
| 584 | rtm->rtm_errno = error; |
| 585 | else |
| 586 | rtm->rtm_flags |= RTF_DONE; |
| 587 | } |
| 588 | if (rt != NULL) |
| 589 | rtfree(rt); |
| 590 | /* |
| 591 | * Check to see if we don't want our own messages. |
| 592 | */ |
| 593 | if (!(so->so_options & SO_USELOOPBACK)) { |
| 594 | if (route_cb.any_count <= 1) { |
| 595 | if (rtm != NULL) |
| 596 | free(rtm, M_RTABLE); |
| 597 | m_freem(m); |
| 598 | return (error); |
| 599 | } |
| 600 | /* There is another listener, so construct message */ |
| 601 | rp = sotorawcb(so); |
| 602 | } |
| 603 | if (rtm != NULL) { |
| 604 | m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); |
| 605 | if (m->m_pkthdr.len < rtm->rtm_msglen) { |
| 606 | m_freem(m); |
| 607 | m = NULL; |
| 608 | } else if (m->m_pkthdr.len > rtm->rtm_msglen) |
| 609 | m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); |
| 610 | free(rtm, M_RTABLE); |
| 611 | } |
| 612 | if (rp != NULL) |
| 613 | rp->rcb_proto.sp_family = 0; /* Avoid us */ |
| 614 | if (m != NULL) |
| 615 | rts_input(m, familyof(rtinfo.rti_dst)); |
| 616 | if (rp != NULL) |
| 617 | rp->rcb_proto.sp_family = PF_ROUTE; |
| 618 | return (error); |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out) |
| 623 | { |
| 624 | #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt; |
| 625 | setmetric(RTV_RPIPE, rmx_recvpipe); |
| 626 | setmetric(RTV_SPIPE, rmx_sendpipe); |
| 627 | setmetric(RTV_SSTHRESH, rmx_ssthresh); |
| 628 | setmetric(RTV_RTT, rmx_rtt); |
| 629 | setmetric(RTV_RTTVAR, rmx_rttvar); |
| 630 | setmetric(RTV_HOPCOUNT, rmx_hopcount); |
| 631 | setmetric(RTV_MTU, rmx_mtu); |
| 632 | setmetric(RTV_EXPIRE, rmx_expire); |
| 633 | #undef setmetric |
| 634 | } |
| 635 | |
| 636 | #define ROUNDUP(a) \ |
| 637 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 638 | |
| 639 | /* |
| 640 | * Extract the addresses of the passed sockaddrs. |
| 641 | * Do a little sanity checking so as to avoid bad memory references. |
| 642 | * This data is derived straight from userland. |
| 643 | */ |
| 644 | static int |
| 645 | rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo) |
| 646 | { |
| 647 | struct sockaddr *sa; |
| 648 | int i; |
| 649 | |
| 650 | for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { |
| 651 | if ((rtinfo->rti_addrs & (1 << i)) == 0) |
| 652 | continue; |
| 653 | sa = (struct sockaddr *)cp; |
| 654 | /* |
| 655 | * It won't fit. |
| 656 | */ |
| 657 | if ((cp + sa->sa_len) > cplim) { |
| 658 | return (EINVAL); |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * There are no more... Quit now. |
| 663 | * If there are more bits, they are in error. |
| 664 | * I've seen this. route(1) can evidently generate these. |
| 665 | * This causes kernel to core dump. |
| 666 | * For compatibility, if we see this, point to a safe address. |
| 667 | */ |
| 668 | if (sa->sa_len == 0) { |
| 669 | static struct sockaddr sa_zero = { |
| 670 | sizeof sa_zero, AF_INET, |
| 671 | }; |
| 672 | |
| 673 | rtinfo->rti_info[i] = &sa_zero; |
| 674 | return (0); /* should be EINVAL but for compat */ |
| 675 | } |
| 676 | |
| 677 | /* Accept the sockaddr. */ |
| 678 | rtinfo->rti_info[i] = sa; |
| 679 | cp += ROUNDUP(sa->sa_len); |
| 680 | } |
| 681 | return (0); |
| 682 | } |
| 683 | |
| 684 | static int |
| 685 | rt_msghdrsize(int type) |
| 686 | { |
| 687 | switch (type) { |
| 688 | case RTM_DELADDR: |
| 689 | case RTM_NEWADDR: |
| 690 | return sizeof(struct ifa_msghdr); |
| 691 | case RTM_DELMADDR: |
| 692 | case RTM_NEWMADDR: |
| 693 | return sizeof(struct ifma_msghdr); |
| 694 | case RTM_IFINFO: |
| 695 | return sizeof(struct if_msghdr); |
| 696 | case RTM_IFANNOUNCE: |
| 697 | return sizeof(struct if_announcemsghdr); |
| 698 | default: |
| 699 | return sizeof(struct rt_msghdr); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | static int |
| 704 | rt_msgsize(int type, struct rt_addrinfo *rtinfo) |
| 705 | { |
| 706 | int len, i; |
| 707 | |
| 708 | len = rt_msghdrsize(type); |
| 709 | for (i = 0; i < RTAX_MAX; i++) { |
| 710 | if (rtinfo->rti_info[i] != NULL) |
| 711 | len += ROUNDUP(rtinfo->rti_info[i]->sa_len); |
| 712 | } |
| 713 | len = ALIGN(len); |
| 714 | return len; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Build a routing message in a buffer. |
| 719 | * Copy the addresses in the rtinfo->rti_info[] sockaddr array |
| 720 | * to the end of the buffer after the message header. |
| 721 | * |
| 722 | * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. |
| 723 | * This side-effect can be avoided if we reorder the addrs bitmask field in all |
| 724 | * the route messages to line up so we can set it here instead of back in the |
| 725 | * calling routine. |
| 726 | */ |
| 727 | static void |
| 728 | rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen) |
| 729 | { |
| 730 | struct rt_msghdr *rtm; |
| 731 | char *cp; |
| 732 | int dlen, i; |
| 733 | |
| 734 | rtm = (struct rt_msghdr *) buf; |
| 735 | rtm->rtm_version = RTM_VERSION; |
| 736 | rtm->rtm_type = type; |
| 737 | rtm->rtm_msglen = msglen; |
| 738 | |
| 739 | cp = (char *)buf + rt_msghdrsize(type); |
| 740 | rtinfo->rti_addrs = 0; |
| 741 | for (i = 0; i < RTAX_MAX; i++) { |
| 742 | struct sockaddr *sa; |
| 743 | |
| 744 | if ((sa = rtinfo->rti_info[i]) == NULL) |
| 745 | continue; |
| 746 | rtinfo->rti_addrs |= (1 << i); |
| 747 | dlen = ROUNDUP(sa->sa_len); |
| 748 | bcopy(sa, cp, dlen); |
| 749 | cp += dlen; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Build a routing message in a mbuf chain. |
| 755 | * Copy the addresses in the rtinfo->rti_info[] sockaddr array |
| 756 | * to the end of the mbuf after the message header. |
| 757 | * |
| 758 | * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[]. |
| 759 | * This side-effect can be avoided if we reorder the addrs bitmask field in all |
| 760 | * the route messages to line up so we can set it here instead of back in the |
| 761 | * calling routine. |
| 762 | */ |
| 763 | static struct mbuf * |
| 764 | rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo) |
| 765 | { |
| 766 | struct mbuf *m; |
| 767 | struct rt_msghdr *rtm; |
| 768 | int hlen, len; |
| 769 | int i; |
| 770 | |
| 771 | hlen = rt_msghdrsize(type); |
| 772 | KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen)); |
| 773 | |
| 774 | m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL); |
| 775 | if (m == NULL) |
| 776 | return (NULL); |
| 777 | m->m_pkthdr.len = m->m_len = hlen; |
| 778 | m->m_pkthdr.rcvif = NULL; |
| 779 | rtinfo->rti_addrs = 0; |
| 780 | len = hlen; |
| 781 | for (i = 0; i < RTAX_MAX; i++) { |
| 782 | struct sockaddr *sa; |
| 783 | int dlen; |
| 784 | |
| 785 | if ((sa = rtinfo->rti_info[i]) == NULL) |
| 786 | continue; |
| 787 | rtinfo->rti_addrs |= (1 << i); |
| 788 | dlen = ROUNDUP(sa->sa_len); |
| 789 | m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */ |
| 790 | len += dlen; |
| 791 | } |
| 792 | if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */ |
| 793 | m_freem(m); |
| 794 | return (NULL); |
| 795 | } |
| 796 | rtm = mtod(m, struct rt_msghdr *); |
| 797 | bzero(rtm, hlen); |
| 798 | rtm->rtm_msglen = len; |
| 799 | rtm->rtm_version = RTM_VERSION; |
| 800 | rtm->rtm_type = type; |
| 801 | return (m); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * This routine is called to generate a message from the routing |
| 806 | * socket indicating that a redirect has occurred, a routing lookup |
| 807 | * has failed, or that a protocol has detected timeouts to a particular |
| 808 | * destination. |
| 809 | */ |
| 810 | void |
| 811 | rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) |
| 812 | { |
| 813 | struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; |
| 814 | struct rt_msghdr *rtm; |
| 815 | struct mbuf *m; |
| 816 | |
| 817 | if (route_cb.any_count == 0) |
| 818 | return; |
| 819 | m = rt_msg_mbuf(type, rtinfo); |
| 820 | if (m == NULL) |
| 821 | return; |
| 822 | rtm = mtod(m, struct rt_msghdr *); |
| 823 | rtm->rtm_flags = RTF_DONE | flags; |
| 824 | rtm->rtm_errno = error; |
| 825 | rtm->rtm_addrs = rtinfo->rti_addrs; |
| 826 | rts_input(m, familyof(dst)); |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | rt_dstmsg(int type, struct sockaddr *dst, int error) |
| 831 | { |
| 832 | struct rt_msghdr *rtm; |
| 833 | struct rt_addrinfo addrs; |
| 834 | struct mbuf *m; |
| 835 | |
| 836 | if (route_cb.any_count == 0) |
| 837 | return; |
| 838 | bzero(&addrs, sizeof(struct rt_addrinfo)); |
| 839 | addrs.rti_info[RTAX_DST] = dst; |
| 840 | m = rt_msg_mbuf(type, &addrs); |
| 841 | if (m == NULL) |
| 842 | return; |
| 843 | rtm = mtod(m, struct rt_msghdr *); |
| 844 | rtm->rtm_flags = RTF_DONE; |
| 845 | rtm->rtm_errno = error; |
| 846 | rtm->rtm_addrs = addrs.rti_addrs; |
| 847 | rts_input(m, familyof(dst)); |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * This routine is called to generate a message from the routing |
| 852 | * socket indicating that the status of a network interface has changed. |
| 853 | */ |
| 854 | void |
| 855 | rt_ifmsg(struct ifnet *ifp) |
| 856 | { |
| 857 | struct if_msghdr *ifm; |
| 858 | struct mbuf *m; |
| 859 | struct rt_addrinfo rtinfo; |
| 860 | |
| 861 | if (route_cb.any_count == 0) |
| 862 | return; |
| 863 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 864 | m = rt_msg_mbuf(RTM_IFINFO, &rtinfo); |
| 865 | if (m == NULL) |
| 866 | return; |
| 867 | ifm = mtod(m, struct if_msghdr *); |
| 868 | ifm->ifm_index = ifp->if_index; |
| 869 | ifm->ifm_flags = ifp->if_flags; |
| 870 | ifm->ifm_data = ifp->if_data; |
| 871 | ifm->ifm_addrs = 0; |
| 872 | rts_input(m, 0); |
| 873 | } |
| 874 | |
| 875 | static void |
| 876 | rt_ifamsg(int cmd, struct ifaddr *ifa) |
| 877 | { |
| 878 | struct ifa_msghdr *ifam; |
| 879 | struct rt_addrinfo rtinfo; |
| 880 | struct mbuf *m; |
| 881 | struct ifnet *ifp = ifa->ifa_ifp; |
| 882 | |
| 883 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 884 | rtinfo.rti_ifaaddr = ifa->ifa_addr; |
| 885 | rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; |
| 886 | rtinfo.rti_netmask = ifa->ifa_netmask; |
| 887 | rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; |
| 888 | |
| 889 | m = rt_msg_mbuf(cmd, &rtinfo); |
| 890 | if (m == NULL) |
| 891 | return; |
| 892 | |
| 893 | ifam = mtod(m, struct ifa_msghdr *); |
| 894 | ifam->ifam_index = ifp->if_index; |
| 895 | ifam->ifam_metric = ifa->ifa_metric; |
| 896 | ifam->ifam_flags = ifa->ifa_flags; |
| 897 | ifam->ifam_addrs = rtinfo.rti_addrs; |
| 898 | |
| 899 | rts_input(m, familyof(ifa->ifa_addr)); |
| 900 | } |
| 901 | |
| 902 | void |
| 903 | rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error) |
| 904 | { |
| 905 | struct rt_msghdr *rtm; |
| 906 | struct rt_addrinfo rtinfo; |
| 907 | struct mbuf *m; |
| 908 | struct sockaddr *dst; |
| 909 | |
| 910 | if (rt == NULL) |
| 911 | return; |
| 912 | |
| 913 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 914 | rtinfo.rti_dst = dst = rt_key(rt); |
| 915 | rtinfo.rti_gateway = rt->rt_gateway; |
| 916 | rtinfo.rti_netmask = rt_mask(rt); |
| 917 | if (ifp != NULL) |
| 918 | rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; |
| 919 | rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr; |
| 920 | |
| 921 | m = rt_msg_mbuf(cmd, &rtinfo); |
| 922 | if (m == NULL) |
| 923 | return; |
| 924 | |
| 925 | rtm = mtod(m, struct rt_msghdr *); |
| 926 | if (ifp != NULL) |
| 927 | rtm->rtm_index = ifp->if_index; |
| 928 | rtm->rtm_flags |= rt->rt_flags; |
| 929 | rtm->rtm_errno = error; |
| 930 | rtm->rtm_addrs = rtinfo.rti_addrs; |
| 931 | |
| 932 | rts_input(m, familyof(dst)); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * This is called to generate messages from the routing socket |
| 937 | * indicating a network interface has had addresses associated with it. |
| 938 | * if we ever reverse the logic and replace messages TO the routing |
| 939 | * socket indicate a request to configure interfaces, then it will |
| 940 | * be unnecessary as the routing socket will automatically generate |
| 941 | * copies of it. |
| 942 | */ |
| 943 | void |
| 944 | rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) |
| 945 | { |
| 946 | #ifdef SCTP |
| 947 | /* |
| 948 | * notify the SCTP stack |
| 949 | * this will only get called when an address is added/deleted |
| 950 | * XXX pass the ifaddr struct instead if ifa->ifa_addr... |
| 951 | */ |
| 952 | if (cmd == RTM_ADD) |
| 953 | sctp_add_ip_address(ifa); |
| 954 | else if (cmd == RTM_DELETE) |
| 955 | sctp_delete_ip_address(ifa); |
| 956 | #endif /* SCTP */ |
| 957 | |
| 958 | if (route_cb.any_count == 0) |
| 959 | return; |
| 960 | |
| 961 | if (cmd == RTM_ADD) { |
| 962 | rt_ifamsg(RTM_NEWADDR, ifa); |
| 963 | rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error); |
| 964 | } else { |
| 965 | KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd)); |
| 966 | rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error); |
| 967 | rt_ifamsg(RTM_DELADDR, ifa); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * This is the analogue to the rt_newaddrmsg which performs the same |
| 973 | * function but for multicast group memberhips. This is easier since |
| 974 | * there is no route state to worry about. |
| 975 | */ |
| 976 | void |
| 977 | rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) |
| 978 | { |
| 979 | struct rt_addrinfo rtinfo; |
| 980 | struct mbuf *m = NULL; |
| 981 | struct ifnet *ifp = ifma->ifma_ifp; |
| 982 | struct ifma_msghdr *ifmam; |
| 983 | |
| 984 | if (route_cb.any_count == 0) |
| 985 | return; |
| 986 | |
| 987 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 988 | rtinfo.rti_ifaaddr = ifma->ifma_addr; |
| 989 | if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrhead)) |
| 990 | rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; |
| 991 | /* |
| 992 | * If a link-layer address is present, present it as a ``gateway'' |
| 993 | * (similarly to how ARP entries, e.g., are presented). |
| 994 | */ |
| 995 | rtinfo.rti_gateway = ifma->ifma_lladdr; |
| 996 | |
| 997 | m = rt_msg_mbuf(cmd, &rtinfo); |
| 998 | if (m == NULL) |
| 999 | return; |
| 1000 | |
| 1001 | ifmam = mtod(m, struct ifma_msghdr *); |
| 1002 | ifmam->ifmam_index = ifp->if_index; |
| 1003 | ifmam->ifmam_addrs = rtinfo.rti_addrs; |
| 1004 | |
| 1005 | rts_input(m, familyof(ifma->ifma_addr)); |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * This is called to generate routing socket messages indicating |
| 1010 | * network interface arrival and departure. |
| 1011 | */ |
| 1012 | void |
| 1013 | rt_ifannouncemsg(struct ifnet *ifp, int what) |
| 1014 | { |
| 1015 | struct rt_addrinfo addrinfo; |
| 1016 | struct mbuf *m; |
| 1017 | struct if_announcemsghdr *ifan; |
| 1018 | |
| 1019 | if (route_cb.any_count == 0) |
| 1020 | return; |
| 1021 | |
| 1022 | bzero(&addrinfo, sizeof addrinfo); |
| 1023 | m = rt_msg_mbuf(RTM_IFANNOUNCE, &addrinfo); |
| 1024 | if (m == NULL) |
| 1025 | return; |
| 1026 | |
| 1027 | ifan = mtod(m, struct if_announcemsghdr *); |
| 1028 | ifan->ifan_index = ifp->if_index; |
| 1029 | strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name); |
| 1030 | ifan->ifan_what = what; |
| 1031 | |
| 1032 | rts_input(m, 0); |
| 1033 | } |
| 1034 | |
| 1035 | static int |
| 1036 | resizewalkarg(struct walkarg *w, int len) |
| 1037 | { |
| 1038 | void *newptr; |
| 1039 | |
| 1040 | newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK); |
| 1041 | if (newptr == NULL) |
| 1042 | return (ENOMEM); |
| 1043 | if (w->w_tmem != NULL) |
| 1044 | free(w->w_tmem, M_RTABLE); |
| 1045 | w->w_tmem = newptr; |
| 1046 | w->w_tmemsize = len; |
| 1047 | return (0); |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * This is used in dumping the kernel table via sysctl(). |
| 1052 | */ |
| 1053 | int |
| 1054 | sysctl_dumpentry(struct radix_node *rn, void *vw) |
| 1055 | { |
| 1056 | struct walkarg *w = vw; |
| 1057 | struct rtentry *rt = (struct rtentry *)rn; |
| 1058 | struct rt_addrinfo rtinfo; |
| 1059 | int error, msglen; |
| 1060 | |
| 1061 | if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) |
| 1062 | return 0; |
| 1063 | |
| 1064 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 1065 | rtinfo.rti_dst = rt_key(rt); |
| 1066 | rtinfo.rti_gateway = rt->rt_gateway; |
| 1067 | rtinfo.rti_netmask = rt_mask(rt); |
| 1068 | rtinfo.rti_genmask = rt->rt_genmask; |
| 1069 | if (rt->rt_ifp != NULL) { |
| 1070 | rtinfo.rti_ifpaddr = |
| 1071 | TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr; |
| 1072 | rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr; |
| 1073 | if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) |
| 1074 | rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr; |
| 1075 | } |
| 1076 | msglen = rt_msgsize(RTM_GET, &rtinfo); |
| 1077 | if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) |
| 1078 | return (ENOMEM); |
| 1079 | rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen); |
| 1080 | if (w->w_req != NULL) { |
| 1081 | struct rt_msghdr *rtm = w->w_tmem; |
| 1082 | |
| 1083 | rtm->rtm_flags = rt->rt_flags; |
| 1084 | rtm->rtm_use = rt->rt_use; |
| 1085 | rtm->rtm_rmx = rt->rt_rmx; |
| 1086 | rtm->rtm_index = rt->rt_ifp->if_index; |
| 1087 | rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; |
| 1088 | rtm->rtm_addrs = rtinfo.rti_addrs; |
| 1089 | error = SYSCTL_OUT(w->w_req, rtm, msglen); |
| 1090 | return (error); |
| 1091 | } |
| 1092 | return (0); |
| 1093 | } |
| 1094 | |
| 1095 | static int |
| 1096 | sysctl_iflist(int af, struct walkarg *w) |
| 1097 | { |
| 1098 | struct ifnet *ifp; |
| 1099 | struct ifaddr *ifa; |
| 1100 | struct rt_addrinfo rtinfo; |
| 1101 | int msglen, error; |
| 1102 | |
| 1103 | bzero(&rtinfo, sizeof(struct rt_addrinfo)); |
| 1104 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 1105 | if (w->w_arg && w->w_arg != ifp->if_index) |
| 1106 | continue; |
| 1107 | ifa = TAILQ_FIRST(&ifp->if_addrhead); |
| 1108 | rtinfo.rti_ifpaddr = ifa->ifa_addr; |
| 1109 | msglen = rt_msgsize(RTM_IFINFO, &rtinfo); |
| 1110 | if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) |
| 1111 | return (ENOMEM); |
| 1112 | rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen); |
| 1113 | rtinfo.rti_ifpaddr = NULL; |
| 1114 | if (w->w_req != NULL && w->w_tmem != NULL) { |
| 1115 | struct if_msghdr *ifm = w->w_tmem; |
| 1116 | |
| 1117 | ifm->ifm_index = ifp->if_index; |
| 1118 | ifm->ifm_flags = ifp->if_flags; |
| 1119 | ifm->ifm_data = ifp->if_data; |
| 1120 | ifm->ifm_addrs = rtinfo.rti_addrs; |
| 1121 | error = SYSCTL_OUT(w->w_req, ifm, msglen); |
| 1122 | if (error) |
| 1123 | return (error); |
| 1124 | } |
| 1125 | while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { |
| 1126 | if (af && af != ifa->ifa_addr->sa_family) |
| 1127 | continue; |
| 1128 | if (curproc->p_ucred->cr_prison && |
| 1129 | prison_if(curthread, ifa->ifa_addr)) |
| 1130 | continue; |
| 1131 | rtinfo.rti_ifaaddr = ifa->ifa_addr; |
| 1132 | rtinfo.rti_netmask = ifa->ifa_netmask; |
| 1133 | rtinfo.rti_bcastaddr = ifa->ifa_dstaddr; |
| 1134 | msglen = rt_msgsize(RTM_NEWADDR, &rtinfo); |
| 1135 | if (w->w_tmemsize < msglen && |
| 1136 | resizewalkarg(w, msglen) != 0) |
| 1137 | return (ENOMEM); |
| 1138 | rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen); |
| 1139 | if (w->w_req != NULL) { |
| 1140 | struct ifa_msghdr *ifam = w->w_tmem; |
| 1141 | |
| 1142 | ifam->ifam_index = ifa->ifa_ifp->if_index; |
| 1143 | ifam->ifam_flags = ifa->ifa_flags; |
| 1144 | ifam->ifam_metric = ifa->ifa_metric; |
| 1145 | ifam->ifam_addrs = rtinfo.rti_addrs; |
| 1146 | error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen); |
| 1147 | if (error) |
| 1148 | return (error); |
| 1149 | } |
| 1150 | } |
| 1151 | rtinfo.rti_netmask = NULL; |
| 1152 | rtinfo.rti_ifaaddr = NULL; |
| 1153 | rtinfo.rti_bcastaddr = NULL; |
| 1154 | } |
| 1155 | return (0); |
| 1156 | } |
| 1157 | |
| 1158 | static int |
| 1159 | sysctl_rtsock(SYSCTL_HANDLER_ARGS) |
| 1160 | { |
| 1161 | int *name = (int *)arg1; |
| 1162 | u_int namelen = arg2; |
| 1163 | struct radix_node_head *rnh; |
| 1164 | int i, error = EINVAL; |
| 1165 | u_char af; |
| 1166 | struct walkarg w; |
| 1167 | |
| 1168 | name ++; |
| 1169 | namelen--; |
| 1170 | if (req->newptr) |
| 1171 | return (EPERM); |
| 1172 | if (namelen != 3) |
| 1173 | return (EINVAL); |
| 1174 | af = name[0]; |
| 1175 | bzero(&w, sizeof w); |
| 1176 | w.w_op = name[1]; |
| 1177 | w.w_arg = name[2]; |
| 1178 | w.w_req = req; |
| 1179 | |
| 1180 | crit_enter(); |
| 1181 | switch (w.w_op) { |
| 1182 | |
| 1183 | case NET_RT_DUMP: |
| 1184 | case NET_RT_FLAGS: |
| 1185 | for (i = 1; i <= AF_MAX; i++) |
| 1186 | if ((rnh = rt_tables[i]) && (af == 0 || af == i) && |
| 1187 | (error = rnh->rnh_walktree(rnh, |
| 1188 | sysctl_dumpentry, &w))) |
| 1189 | break; |
| 1190 | break; |
| 1191 | |
| 1192 | case NET_RT_IFLIST: |
| 1193 | error = sysctl_iflist(af, &w); |
| 1194 | } |
| 1195 | crit_exit(); |
| 1196 | if (w.w_tmem != NULL) |
| 1197 | free(w.w_tmem, M_RTABLE); |
| 1198 | return (error); |
| 1199 | } |
| 1200 | |
| 1201 | SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); |
| 1202 | |
| 1203 | /* |
| 1204 | * Definitions of protocols supported in the ROUTE domain. |
| 1205 | */ |
| 1206 | |
| 1207 | extern struct domain routedomain; /* or at least forward */ |
| 1208 | |
| 1209 | static struct protosw routesw[] = { |
| 1210 | { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, |
| 1211 | 0, route_output, raw_ctlinput, 0, |
| 1212 | cpu0_soport, |
| 1213 | raw_init, 0, 0, 0, |
| 1214 | &route_usrreqs |
| 1215 | } |
| 1216 | }; |
| 1217 | |
| 1218 | static struct domain routedomain = { |
| 1219 | PF_ROUTE, "route", NULL, NULL, NULL, |
| 1220 | routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])], |
| 1221 | }; |
| 1222 | |
| 1223 | DOMAIN_SET(route); |