1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1988, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 3328070a0eSRuslan Ermilov * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38960ed29cSSeigo Tanimura #include <sys/domain.h> 39748e0b0aSGarrett Wollman #include <sys/kernel.h> 40960ed29cSSeigo Tanimura #include <sys/jail.h> 414d1d4912SBruce Evans #include <sys/malloc.h> 42df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 43960ed29cSSeigo Tanimura #include <sys/proc.h> 44960ed29cSSeigo Tanimura #include <sys/protosw.h> 45960ed29cSSeigo Tanimura #include <sys/signalvar.h> 46df8bae1dSRodney W. Grimes #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48960ed29cSSeigo Tanimura #include <sys/sysctl.h> 49960ed29cSSeigo Tanimura #include <sys/systm.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/if.h> 52df8bae1dSRodney W. Grimes #include <net/raw_cb.h> 53960ed29cSSeigo Tanimura #include <net/route.h> 54df8bae1dSRodney W. Grimes 55a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 56a1c995b6SPoul-Henning Kamp 57becc44d7SSam Leffler /* NB: these are not modified */ 5852041295SPoul-Henning Kamp static struct sockaddr route_dst = { 2, PF_ROUTE, }; 5952041295SPoul-Henning Kamp static struct sockaddr route_src = { 2, PF_ROUTE, }; 60076d0761SJulian Elischer static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 61becc44d7SSam Leffler 62becc44d7SSam Leffler static struct { 63becc44d7SSam Leffler int ip_count; /* attacked w/ AF_INET */ 64becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 65becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 66becc44d7SSam Leffler int any_count; /* total attached */ 67becc44d7SSam Leffler } route_cb; 68df8bae1dSRodney W. Grimes 69aea8b30fSSam Leffler struct mtx rtsock_mtx; 70aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 71aea8b30fSSam Leffler 72aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 73aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 74aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 75aea8b30fSSam Leffler 76df8bae1dSRodney W. Grimes struct walkarg { 7752041295SPoul-Henning Kamp int w_tmemsize; 7852041295SPoul-Henning Kamp int w_op, w_arg; 7952041295SPoul-Henning Kamp caddr_t w_tmem; 8052041295SPoul-Henning Kamp struct sysctl_req *w_req; 81df8bae1dSRodney W. Grimes }; 82df8bae1dSRodney W. Grimes 83becc44d7SSam Leffler static struct mbuf *rt_msg1(int, struct rt_addrinfo *); 84929ddbbbSAlfred Perlstein static int rt_msg2(int, struct rt_addrinfo *, caddr_t, struct walkarg *); 85929ddbbbSAlfred Perlstein static int rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 86929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 87929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 8805b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 89929ddbbbSAlfred Perlstein static int route_output(struct mbuf *, struct socket *); 90929ddbbbSAlfred Perlstein static void rt_setmetrics(u_long, struct rt_metrics *, struct rt_metrics *); 91becc44d7SSam Leffler static void rt_dispatch(struct mbuf *, struct sockaddr *); 92df8bae1dSRodney W. Grimes 93a29f300eSGarrett Wollman /* 94a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 95a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 96a29f300eSGarrett Wollman */ 9752041295SPoul-Henning Kamp static int 98a29f300eSGarrett Wollman rts_abort(struct socket *so) 99df8bae1dSRodney W. Grimes { 100a29f300eSGarrett Wollman int s, error; 101df8bae1dSRodney W. Grimes s = splnet(); 102a29f300eSGarrett Wollman error = raw_usrreqs.pru_abort(so); 103df8bae1dSRodney W. Grimes splx(s); 104a29f300eSGarrett Wollman return error; 105df8bae1dSRodney W. Grimes } 106a29f300eSGarrett Wollman 107a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 108a29f300eSGarrett Wollman 109a29f300eSGarrett Wollman static int 110b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 111a29f300eSGarrett Wollman { 112a29f300eSGarrett Wollman struct rawcb *rp; 113a29f300eSGarrett Wollman int s, error; 114a29f300eSGarrett Wollman 115a29f300eSGarrett Wollman if (sotorawcb(so) != 0) 116a29f300eSGarrett Wollman return EISCONN; /* XXX panic? */ 1177cc0979fSDavid Malone /* XXX */ 118a163d034SWarner Losh MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 119a29f300eSGarrett Wollman if (rp == 0) 120a29f300eSGarrett Wollman return ENOBUFS; 121a29f300eSGarrett Wollman 122a29f300eSGarrett Wollman /* 123a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 124a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 125a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 126a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 127a29f300eSGarrett Wollman * eliminate the spl. 128a29f300eSGarrett Wollman */ 129a29f300eSGarrett Wollman s = splnet(); 130a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 131162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 132a29f300eSGarrett Wollman rp = sotorawcb(so); 133a29f300eSGarrett Wollman if (error) { 134a29f300eSGarrett Wollman splx(s); 1357ba271aeSJonathan Chen so->so_pcb = NULL; 136a29f300eSGarrett Wollman free(rp, M_PCB); 137a29f300eSGarrett Wollman return error; 138a29f300eSGarrett Wollman } 139aea8b30fSSam Leffler RTSOCK_LOCK(); 140a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 141a29f300eSGarrett Wollman case AF_INET: 142df8bae1dSRodney W. Grimes route_cb.ip_count++; 143a29f300eSGarrett Wollman break; 144899ce4f4SYoshinobu Inoue case AF_INET6: 145899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 146899ce4f4SYoshinobu Inoue break; 147a29f300eSGarrett Wollman case AF_IPX: 148cc6a66f2SJulian Elischer route_cb.ipx_count++; 149a29f300eSGarrett Wollman break; 150a29f300eSGarrett Wollman } 151df8bae1dSRodney W. Grimes rp->rcb_faddr = &route_src; 152df8bae1dSRodney W. Grimes route_cb.any_count++; 153aea8b30fSSam Leffler RTSOCK_UNLOCK(); 15403e49181SSeigo Tanimura soisconnected(so); 155df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 156df8bae1dSRodney W. Grimes splx(s); 157a29f300eSGarrett Wollman return 0; 158df8bae1dSRodney W. Grimes } 159df8bae1dSRodney W. Grimes 160a29f300eSGarrett Wollman static int 161b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 162a29f300eSGarrett Wollman { 163a29f300eSGarrett Wollman int s, error; 164a29f300eSGarrett Wollman s = splnet(); 165b40ce416SJulian Elischer error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */ 166a29f300eSGarrett Wollman splx(s); 167a29f300eSGarrett Wollman return error; 168a29f300eSGarrett Wollman } 169a29f300eSGarrett Wollman 170a29f300eSGarrett Wollman static int 171b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 172a29f300eSGarrett Wollman { 173a29f300eSGarrett Wollman int s, error; 174a29f300eSGarrett Wollman s = splnet(); 175b40ce416SJulian Elischer error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */ 176a29f300eSGarrett Wollman splx(s); 177a29f300eSGarrett Wollman return error; 178a29f300eSGarrett Wollman } 179a29f300eSGarrett Wollman 180a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 181a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 182a29f300eSGarrett Wollman 183a29f300eSGarrett Wollman static int 184a29f300eSGarrett Wollman rts_detach(struct socket *so) 185a29f300eSGarrett Wollman { 186a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 187a29f300eSGarrett Wollman int s, error; 188a29f300eSGarrett Wollman 189a29f300eSGarrett Wollman s = splnet(); 190a29f300eSGarrett Wollman if (rp != 0) { 191aea8b30fSSam Leffler RTSOCK_LOCK(); 192a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 193a29f300eSGarrett Wollman case AF_INET: 194a29f300eSGarrett Wollman route_cb.ip_count--; 195a29f300eSGarrett Wollman break; 196899ce4f4SYoshinobu Inoue case AF_INET6: 197899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 198899ce4f4SYoshinobu Inoue break; 199a29f300eSGarrett Wollman case AF_IPX: 200a29f300eSGarrett Wollman route_cb.ipx_count--; 201a29f300eSGarrett Wollman break; 202a29f300eSGarrett Wollman } 203a29f300eSGarrett Wollman route_cb.any_count--; 204aea8b30fSSam Leffler RTSOCK_UNLOCK(); 205a29f300eSGarrett Wollman } 206a29f300eSGarrett Wollman error = raw_usrreqs.pru_detach(so); 207a29f300eSGarrett Wollman splx(s); 208a29f300eSGarrett Wollman return error; 209a29f300eSGarrett Wollman } 210a29f300eSGarrett Wollman 211a29f300eSGarrett Wollman static int 212a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 213a29f300eSGarrett Wollman { 214a29f300eSGarrett Wollman int s, error; 215a29f300eSGarrett Wollman s = splnet(); 216a29f300eSGarrett Wollman error = raw_usrreqs.pru_disconnect(so); 217a29f300eSGarrett Wollman splx(s); 218a29f300eSGarrett Wollman return error; 219a29f300eSGarrett Wollman } 220a29f300eSGarrett Wollman 221a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 222a29f300eSGarrett Wollman 223a29f300eSGarrett Wollman static int 22457bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 225a29f300eSGarrett Wollman { 226a29f300eSGarrett Wollman int s, error; 227a29f300eSGarrett Wollman s = splnet(); 228a29f300eSGarrett Wollman error = raw_usrreqs.pru_peeraddr(so, nam); 229a29f300eSGarrett Wollman splx(s); 230a29f300eSGarrett Wollman return error; 231a29f300eSGarrett Wollman } 232a29f300eSGarrett Wollman 233a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 234a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 235a29f300eSGarrett Wollman 236a29f300eSGarrett Wollman static int 23757bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 238b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 239a29f300eSGarrett Wollman { 240a29f300eSGarrett Wollman int s, error; 241a29f300eSGarrett Wollman s = splnet(); 242b40ce416SJulian Elischer error = raw_usrreqs.pru_send(so, flags, m, nam, control, td); 243a29f300eSGarrett Wollman splx(s); 244a29f300eSGarrett Wollman return error; 245a29f300eSGarrett Wollman } 246a29f300eSGarrett Wollman 247a29f300eSGarrett Wollman /* pru_sense is null */ 248a29f300eSGarrett Wollman 249a29f300eSGarrett Wollman static int 250a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 251a29f300eSGarrett Wollman { 252a29f300eSGarrett Wollman int s, error; 253a29f300eSGarrett Wollman s = splnet(); 254a29f300eSGarrett Wollman error = raw_usrreqs.pru_shutdown(so); 255a29f300eSGarrett Wollman splx(s); 256a29f300eSGarrett Wollman return error; 257a29f300eSGarrett Wollman } 258a29f300eSGarrett Wollman 259a29f300eSGarrett Wollman static int 26057bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 261a29f300eSGarrett Wollman { 262a29f300eSGarrett Wollman int s, error; 263a29f300eSGarrett Wollman s = splnet(); 264a29f300eSGarrett Wollman error = raw_usrreqs.pru_sockaddr(so, nam); 265a29f300eSGarrett Wollman splx(s); 266a29f300eSGarrett Wollman return error; 267a29f300eSGarrett Wollman } 268a29f300eSGarrett Wollman 269a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 270a29f300eSGarrett Wollman rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect, 271a29f300eSGarrett Wollman pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect, 272a29f300eSGarrett Wollman pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp, 273a29f300eSGarrett Wollman rts_send, pru_sense_null, rts_shutdown, rts_sockaddr, 274f8f6cbbaSPeter Wemm sosend, soreceive, sopoll 275a29f300eSGarrett Wollman }; 276a29f300eSGarrett Wollman 277df8bae1dSRodney W. Grimes /*ARGSUSED*/ 27852041295SPoul-Henning Kamp static int 279df8bae1dSRodney W. Grimes route_output(m, so) 280df8bae1dSRodney W. Grimes register struct mbuf *m; 281df8bae1dSRodney W. Grimes struct socket *so; 282df8bae1dSRodney W. Grimes { 283becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 284df8bae1dSRodney W. Grimes register struct rt_msghdr *rtm = 0; 285df8bae1dSRodney W. Grimes register struct rtentry *rt = 0; 28678a82810SGarrett Wollman struct radix_node_head *rnh; 287df8bae1dSRodney W. Grimes struct rt_addrinfo info; 288df8bae1dSRodney W. Grimes int len, error = 0; 289df8bae1dSRodney W. Grimes struct ifnet *ifp = 0; 290df8bae1dSRodney W. Grimes struct ifaddr *ifa = 0; 291df8bae1dSRodney W. Grimes 292df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 293df8bae1dSRodney W. Grimes if (m == 0 || ((m->m_len < sizeof(long)) && 294df8bae1dSRodney W. Grimes (m = m_pullup(m, sizeof(long))) == 0)) 295df8bae1dSRodney W. Grimes return (ENOBUFS); 296df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 297df8bae1dSRodney W. Grimes panic("route_output"); 298df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 299df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 300df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 301becc44d7SSam Leffler info.rti_info[RTAX_DST] = 0; 302df8bae1dSRodney W. Grimes senderr(EINVAL); 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 305df8bae1dSRodney W. Grimes if (rtm == 0) { 306becc44d7SSam Leffler info.rti_info[RTAX_DST] = 0; 307df8bae1dSRodney W. Grimes senderr(ENOBUFS); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 310df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 311becc44d7SSam Leffler info.rti_info[RTAX_DST] = 0; 312df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 3158071913dSRuslan Ermilov bzero(&info, sizeof(info)); 316df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 317076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 318becc44d7SSam Leffler info.rti_info[RTAX_DST] = 0; 319076d0761SJulian Elischer senderr(EINVAL); 320076d0761SJulian Elischer } 3218071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 322becc44d7SSam Leffler if (info.rti_info[RTAX_DST] == 0 || 323becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 324becc44d7SSam Leffler (info.rti_info[RTAX_GATEWAY] != 0 && 325becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 326df8bae1dSRodney W. Grimes senderr(EINVAL); 327becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) { 328df8bae1dSRodney W. Grimes struct radix_node *t; 329becc44d7SSam Leffler t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1); 330becc44d7SSam Leffler if (t && Bcmp((caddr_t *) info.rti_info[RTAX_GENMASK] + 1, 331becc44d7SSam Leffler (caddr_t *)t->rn_key + 1, 332920eb79fSRuslan Ermilov *(u_char *)t->rn_key - 1) == 0) 333becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = 334becc44d7SSam Leffler (struct sockaddr *)(t->rn_key); 335df8bae1dSRodney W. Grimes else 336df8bae1dSRodney W. Grimes senderr(ENOBUFS); 337df8bae1dSRodney W. Grimes } 338162c0b2eSRuslan Ermilov 339162c0b2eSRuslan Ermilov /* 340162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 341162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 342162c0b2eSRuslan Ermilov */ 34344731cabSJohn Baldwin if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0) 344dadb6c3bSRuslan Ermilov senderr(error); 345162c0b2eSRuslan Ermilov 346df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 347becc44d7SSam Leffler struct rtentry *saved_nrt; 348df8bae1dSRodney W. Grimes 349df8bae1dSRodney W. Grimes case RTM_ADD: 350becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] == 0) 351df8bae1dSRodney W. Grimes senderr(EINVAL); 352becc44d7SSam Leffler saved_nrt = 0; 3538071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &saved_nrt); 354df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 355d1dd20beSSam Leffler RT_LOCK(saved_nrt); 356df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 357df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 35895b6073cSDavid Greenman saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 35995b6073cSDavid Greenman saved_nrt->rt_rmx.rmx_locks |= 36095b6073cSDavid Greenman (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 3617138d65cSSam Leffler RT_REMREF(saved_nrt); 362becc44d7SSam Leffler saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 363d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes break; 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes case RTM_DELETE: 368becc44d7SSam Leffler saved_nrt = 0; 3698071913dSRuslan Ermilov error = rtrequest1(RTM_DELETE, &info, &saved_nrt); 37078a82810SGarrett Wollman if (error == 0) { 371d1dd20beSSam Leffler RT_LOCK(saved_nrt); 37271eba915SRuslan Ermilov rt = saved_nrt; 37378a82810SGarrett Wollman goto report; 37478a82810SGarrett Wollman } 375df8bae1dSRodney W. Grimes break; 376df8bae1dSRodney W. Grimes 377df8bae1dSRodney W. Grimes case RTM_GET: 378df8bae1dSRodney W. Grimes case RTM_CHANGE: 379df8bae1dSRodney W. Grimes case RTM_LOCK: 380becc44d7SSam Leffler rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family]; 381becc44d7SSam Leffler if (rnh == 0) 38278a82810SGarrett Wollman senderr(EAFNOSUPPORT); 383956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 384becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 385becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 386956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 387becc44d7SSam Leffler if (rt == NULL) /* XXX looks bogus */ 388df8bae1dSRodney W. Grimes senderr(ESRCH); 389d1dd20beSSam Leffler RT_LOCK(rt); 3907138d65cSSam Leffler RT_ADDREF(rt); 391956b0b65SJeffrey Hsu 392df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 393df8bae1dSRodney W. Grimes 394df8bae1dSRodney W. Grimes case RTM_GET: 39578a82810SGarrett Wollman report: 396d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 397becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 398becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 399becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 400becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 401df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 402df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 403df440948SPoul-Henning Kamp if (ifp) { 404becc44d7SSam Leffler info.rti_info[RTAX_IFP] = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 405becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 406becc44d7SSam Leffler rt->rt_ifa->ifa_addr; 40728070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 408becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 409becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 410df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 411df8bae1dSRodney W. Grimes } else { 412becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 0; 413becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 0; 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes } 41678a82810SGarrett Wollman len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0, 417df8bae1dSRodney W. Grimes (struct walkarg *)0); 418df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 419df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 420df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 421becc44d7SSam Leffler if (new_rtm == 0) { 422d1dd20beSSam Leffler RT_UNLOCK(rt); 423df8bae1dSRodney W. Grimes senderr(ENOBUFS); 424becc44d7SSam Leffler } 425df8bae1dSRodney W. Grimes Bcopy(rtm, new_rtm, rtm->rtm_msglen); 426df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 427df8bae1dSRodney W. Grimes } 42878a82810SGarrett Wollman (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, 429df8bae1dSRodney W. Grimes (struct walkarg *)0); 430df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 431df8bae1dSRodney W. Grimes rtm->rtm_rmx = rt->rt_rmx; 432df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 433df8bae1dSRodney W. Grimes break; 434df8bae1dSRodney W. Grimes 435df8bae1dSRodney W. Grimes case RTM_CHANGE: 436becc44d7SSam Leffler /* 437becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 438becc44d7SSam Leffler * flags may also be different; ifp may be specified 439becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 440becc44d7SSam Leffler */ 441becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 442becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 443becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 444becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 445becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 446becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 447becc44d7SSam Leffler if ((error = rt_getifa(&info)) != 0) { 448d1dd20beSSam Leffler RT_UNLOCK(rt); 4498071913dSRuslan Ermilov senderr(error); 45002a5d63eSBrian Somers } 451becc44d7SSam Leffler } 452becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] != NULL && 453becc44d7SSam Leffler (error = rt_setgate(rt, rt_key(rt), 454becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY])) != 0) { 455d1dd20beSSam Leffler RT_UNLOCK(rt); 4568071913dSRuslan Ermilov senderr(error); 457becc44d7SSam Leffler } 4588071913dSRuslan Ermilov if ((ifa = info.rti_ifa) != NULL) { 459becc44d7SSam Leffler struct ifaddr *oifa = rt->rt_ifa; 460df8bae1dSRodney W. Grimes if (oifa != ifa) { 46119fc74fbSJeffrey Hsu if (oifa) { 46219fc74fbSJeffrey Hsu if (oifa->ifa_rtrequest) 463becc44d7SSam Leffler oifa->ifa_rtrequest( 464becc44d7SSam Leffler RTM_DELETE, rt, 4658071913dSRuslan Ermilov &info); 46612e552d6SJeffrey Hsu IFAFREE(oifa); 46719fc74fbSJeffrey Hsu } 46819fc74fbSJeffrey Hsu IFAREF(ifa); 469df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 4708071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 474df8bae1dSRodney W. Grimes &rt->rt_rmx); 475df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 4768071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 477becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) 478becc44d7SSam Leffler rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 47993b0017fSPhilippe Charnier /* FALLTHROUGH */ 480df8bae1dSRodney W. Grimes case RTM_LOCK: 481df8bae1dSRodney W. Grimes rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); 482df8bae1dSRodney W. Grimes rt->rt_rmx.rmx_locks |= 483df8bae1dSRodney W. Grimes (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); 484df8bae1dSRodney W. Grimes break; 485df8bae1dSRodney W. Grimes } 486d1dd20beSSam Leffler RT_UNLOCK(rt); 487df8bae1dSRodney W. Grimes break; 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes default: 490df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes flush: 494df8bae1dSRodney W. Grimes if (rtm) { 495df8bae1dSRodney W. Grimes if (error) 496df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 497df8bae1dSRodney W. Grimes else 498df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 499df8bae1dSRodney W. Grimes } 500becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 501becc44d7SSam Leffler RTFREE(rt); 502df8bae1dSRodney W. Grimes { 503df8bae1dSRodney W. Grimes register struct rawcb *rp = 0; 504df8bae1dSRodney W. Grimes /* 505df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 506df8bae1dSRodney W. Grimes */ 507df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 508df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 509df8bae1dSRodney W. Grimes if (rtm) 510df8bae1dSRodney W. Grimes Free(rtm); 511df8bae1dSRodney W. Grimes m_freem(m); 512df8bae1dSRodney W. Grimes return (error); 513df8bae1dSRodney W. Grimes } 514df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 515df8bae1dSRodney W. Grimes rp = sotorawcb(so); 5164cc20ab1SSeigo Tanimura } 517df8bae1dSRodney W. Grimes if (rtm) { 518df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 51903311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 52003311056SHajimu UMEMOTO m_freem(m); 52103311056SHajimu UMEMOTO m = NULL; 52203311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 52303311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 524df8bae1dSRodney W. Grimes Free(rtm); 525df8bae1dSRodney W. Grimes } 526becc44d7SSam Leffler if (m) { 527becc44d7SSam Leffler if (rp) { 528becc44d7SSam Leffler /* 529becc44d7SSam Leffler * XXX insure we don't get a copy by 530becc44d7SSam Leffler * invalidating our protocol 531becc44d7SSam Leffler */ 532becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 533becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 534becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 535becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 536becc44d7SSam Leffler } else 537becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 538becc44d7SSam Leffler } 539df8bae1dSRodney W. Grimes } 540df8bae1dSRodney W. Grimes return (error); 541becc44d7SSam Leffler #undef sa_equal 542df8bae1dSRodney W. Grimes } 543df8bae1dSRodney W. Grimes 54452041295SPoul-Henning Kamp static void 545becc44d7SSam Leffler rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out) 546df8bae1dSRodney W. Grimes { 547df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 548df8bae1dSRodney W. Grimes metric(RTV_RPIPE, rmx_recvpipe); 549df8bae1dSRodney W. Grimes metric(RTV_SPIPE, rmx_sendpipe); 550df8bae1dSRodney W. Grimes metric(RTV_SSTHRESH, rmx_ssthresh); 551df8bae1dSRodney W. Grimes metric(RTV_RTT, rmx_rtt); 552df8bae1dSRodney W. Grimes metric(RTV_RTTVAR, rmx_rttvar); 553df8bae1dSRodney W. Grimes metric(RTV_HOPCOUNT, rmx_hopcount); 554df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 555df8bae1dSRodney W. Grimes metric(RTV_EXPIRE, rmx_expire); 556df8bae1dSRodney W. Grimes #undef metric 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes #define ROUNDUP(a) \ 560df8bae1dSRodney W. Grimes ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 561076d0761SJulian Elischer 5627f33a738SJulian Elischer /* 5637f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 5647f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 565076d0761SJulian Elischer * This data is derived straight from userland. 5667f33a738SJulian Elischer */ 567076d0761SJulian Elischer static int 568becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 569df8bae1dSRodney W. Grimes { 570becc44d7SSam Leffler #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 571df8bae1dSRodney W. Grimes register struct sockaddr *sa; 572df8bae1dSRodney W. Grimes register int i; 573df8bae1dSRodney W. Grimes 574becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 575df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 576df8bae1dSRodney W. Grimes continue; 577ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 5787f33a738SJulian Elischer /* 579076d0761SJulian Elischer * It won't fit. 5807f33a738SJulian Elischer */ 581becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 582076d0761SJulian Elischer return (EINVAL); 5837f33a738SJulian Elischer /* 5847f33a738SJulian Elischer * there are no more.. quit now 5857f33a738SJulian Elischer * If there are more bits, they are in error. 5867f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 5877f33a738SJulian Elischer * This causes kernel to core dump. 588076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 5897f33a738SJulian Elischer */ 590076d0761SJulian Elischer if (sa->sa_len == 0) { 591076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 592076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 593df8bae1dSRodney W. Grimes } 594076d0761SJulian Elischer /* accept it */ 595076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 596076d0761SJulian Elischer ADVANCE(cp, sa); 597076d0761SJulian Elischer } 598076d0761SJulian Elischer return (0); 599becc44d7SSam Leffler #undef ADVANCE 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes 602df8bae1dSRodney W. Grimes static struct mbuf * 603becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 604df8bae1dSRodney W. Grimes { 605df8bae1dSRodney W. Grimes register struct rt_msghdr *rtm; 606df8bae1dSRodney W. Grimes register struct mbuf *m; 607df8bae1dSRodney W. Grimes register int i; 608df8bae1dSRodney W. Grimes register struct sockaddr *sa; 609df8bae1dSRodney W. Grimes int len, dlen; 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes switch (type) { 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes case RTM_DELADDR: 614df8bae1dSRodney W. Grimes case RTM_NEWADDR: 615df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 616df8bae1dSRodney W. Grimes break; 617df8bae1dSRodney W. Grimes 618477180fbSGarrett Wollman case RTM_DELMADDR: 619477180fbSGarrett Wollman case RTM_NEWMADDR: 620477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 621477180fbSGarrett Wollman break; 622477180fbSGarrett Wollman 623df8bae1dSRodney W. Grimes case RTM_IFINFO: 624df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 625df8bae1dSRodney W. Grimes break; 626df8bae1dSRodney W. Grimes 6277b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 6287b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 6297b6edd04SRuslan Ermilov break; 6307b6edd04SRuslan Ermilov 631df8bae1dSRodney W. Grimes default: 632df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 633df8bae1dSRodney W. Grimes } 63433841545SHajimu UMEMOTO if (len > MCLBYTES) 635df8bae1dSRodney W. Grimes panic("rt_msg1"); 636a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 63733841545SHajimu UMEMOTO if (m && len > MHLEN) { 638a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 63933841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 64033841545SHajimu UMEMOTO m_free(m); 64133841545SHajimu UMEMOTO m = NULL; 64233841545SHajimu UMEMOTO } 64333841545SHajimu UMEMOTO } 64433841545SHajimu UMEMOTO if (m == 0) 64533841545SHajimu UMEMOTO return (m); 646df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 647df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = 0; 648df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 649df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 650df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 651df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 652df8bae1dSRodney W. Grimes continue; 653df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 654df8bae1dSRodney W. Grimes dlen = ROUNDUP(sa->sa_len); 655df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 656df8bae1dSRodney W. Grimes len += dlen; 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 659df8bae1dSRodney W. Grimes m_freem(m); 660df8bae1dSRodney W. Grimes return (NULL); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 663df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 664df8bae1dSRodney W. Grimes rtm->rtm_type = type; 665df8bae1dSRodney W. Grimes return (m); 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes static int 669becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 670df8bae1dSRodney W. Grimes { 671df8bae1dSRodney W. Grimes register int i; 672df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 673df8bae1dSRodney W. Grimes caddr_t cp0; 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 676df8bae1dSRodney W. Grimes again: 677df8bae1dSRodney W. Grimes switch (type) { 678df8bae1dSRodney W. Grimes 679df8bae1dSRodney W. Grimes case RTM_DELADDR: 680df8bae1dSRodney W. Grimes case RTM_NEWADDR: 681df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 682df8bae1dSRodney W. Grimes break; 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes case RTM_IFINFO: 685df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 686df8bae1dSRodney W. Grimes break; 687df8bae1dSRodney W. Grimes 68805b2efe0SBruce M Simpson case RTM_NEWMADDR: 68905b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 69005b2efe0SBruce M Simpson break; 69105b2efe0SBruce M Simpson 692df8bae1dSRodney W. Grimes default: 693df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 694df8bae1dSRodney W. Grimes } 695df440948SPoul-Henning Kamp cp0 = cp; 696df440948SPoul-Henning Kamp if (cp0) 697df8bae1dSRodney W. Grimes cp += len; 698df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 699df8bae1dSRodney W. Grimes register struct sockaddr *sa; 700df8bae1dSRodney W. Grimes 701df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == 0) 702df8bae1dSRodney W. Grimes continue; 703df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 704df8bae1dSRodney W. Grimes dlen = ROUNDUP(sa->sa_len); 705df8bae1dSRodney W. Grimes if (cp) { 706df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 707df8bae1dSRodney W. Grimes cp += dlen; 708df8bae1dSRodney W. Grimes } 709df8bae1dSRodney W. Grimes len += dlen; 710df8bae1dSRodney W. Grimes } 711694ff264SAndrew Gallatin len = ALIGN(len); 712df8bae1dSRodney W. Grimes if (cp == 0 && w != NULL && !second_time) { 713df8bae1dSRodney W. Grimes register struct walkarg *rw = w; 714df8bae1dSRodney W. Grimes 71552041295SPoul-Henning Kamp if (rw->w_req) { 716df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 717df8bae1dSRodney W. Grimes if (rw->w_tmem) 718df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 719df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 720df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 721df440948SPoul-Henning Kamp if (rw->w_tmem) 722df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes if (rw->w_tmem) { 725df8bae1dSRodney W. Grimes cp = rw->w_tmem; 726df8bae1dSRodney W. Grimes second_time = 1; 727df8bae1dSRodney W. Grimes goto again; 72852041295SPoul-Henning Kamp } 729df8bae1dSRodney W. Grimes } 730df8bae1dSRodney W. Grimes } 731df8bae1dSRodney W. Grimes if (cp) { 732df8bae1dSRodney W. Grimes register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 733df8bae1dSRodney W. Grimes 734df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 735df8bae1dSRodney W. Grimes rtm->rtm_type = type; 736df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes return (len); 739df8bae1dSRodney W. Grimes } 740df8bae1dSRodney W. Grimes 741df8bae1dSRodney W. Grimes /* 742df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 743df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 744df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 745df8bae1dSRodney W. Grimes * destination. 746df8bae1dSRodney W. Grimes */ 747df8bae1dSRodney W. Grimes void 748becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 749df8bae1dSRodney W. Grimes { 750becc44d7SSam Leffler struct rt_msghdr *rtm; 751becc44d7SSam Leffler struct mbuf *m; 752df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 753df8bae1dSRodney W. Grimes 754df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 755df8bae1dSRodney W. Grimes return; 756df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 757df8bae1dSRodney W. Grimes if (m == 0) 758df8bae1dSRodney W. Grimes return; 759df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 760df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 761df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 762df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 763becc44d7SSam Leffler rt_dispatch(m, sa); 764df8bae1dSRodney W. Grimes } 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes /* 767df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 768df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes void 771becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 772df8bae1dSRodney W. Grimes { 773becc44d7SSam Leffler struct if_msghdr *ifm; 774df8bae1dSRodney W. Grimes struct mbuf *m; 775df8bae1dSRodney W. Grimes struct rt_addrinfo info; 776df8bae1dSRodney W. Grimes 777df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 778df8bae1dSRodney W. Grimes return; 779df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 780df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 781df8bae1dSRodney W. Grimes if (m == 0) 782df8bae1dSRodney W. Grimes return; 783df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 784df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 78562f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 786df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 787df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 788becc44d7SSam Leffler rt_dispatch(m, NULL); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes 791df8bae1dSRodney W. Grimes /* 792df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 793df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 794df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 795df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 796df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 797df8bae1dSRodney W. Grimes * copies of it. 798df8bae1dSRodney W. Grimes */ 799df8bae1dSRodney W. Grimes void 800becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 801df8bae1dSRodney W. Grimes { 802df8bae1dSRodney W. Grimes struct rt_addrinfo info; 80326f9a767SRodney W. Grimes struct sockaddr *sa = 0; 804df8bae1dSRodney W. Grimes int pass; 80526f9a767SRodney W. Grimes struct mbuf *m = 0; 806df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 807df8bae1dSRodney W. Grimes 808df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 809df8bae1dSRodney W. Grimes return; 810df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 811df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 812df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 813df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 814df8bae1dSRodney W. Grimes register struct ifa_msghdr *ifam; 815df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 816df8bae1dSRodney W. Grimes 817becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 818becc44d7SSam Leffler info.rti_info[RTAX_IFP] = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 819becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 820becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 821df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 822df8bae1dSRodney W. Grimes continue; 823df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 824df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 825df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 826df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 827df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 828df8bae1dSRodney W. Grimes } 829df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 830df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 831df8bae1dSRodney W. Grimes register struct rt_msghdr *rtm; 832df8bae1dSRodney W. Grimes 833df8bae1dSRodney W. Grimes if (rt == 0) 834df8bae1dSRodney W. Grimes continue; 835becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 836becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 837becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 838df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 839df8bae1dSRodney W. Grimes continue; 840df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 841df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 842df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 843df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 844df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 845df8bae1dSRodney W. Grimes } 846becc44d7SSam Leffler rt_dispatch(m, sa); 847df8bae1dSRodney W. Grimes } 848df8bae1dSRodney W. Grimes } 849df8bae1dSRodney W. Grimes 850477180fbSGarrett Wollman /* 851477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 852477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 853477180fbSGarrett Wollman * there is no route state to worry about. 854477180fbSGarrett Wollman */ 855477180fbSGarrett Wollman void 856becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 857477180fbSGarrett Wollman { 858477180fbSGarrett Wollman struct rt_addrinfo info; 859477180fbSGarrett Wollman struct mbuf *m = 0; 860477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 861477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 862477180fbSGarrett Wollman 863477180fbSGarrett Wollman if (route_cb.any_count == 0) 864477180fbSGarrett Wollman return; 865477180fbSGarrett Wollman 866477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 867becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 86822f29826SPoul-Henning Kamp if (ifp && TAILQ_FIRST(&ifp->if_addrhead)) 869becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 870becc44d7SSam Leffler TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 8718bf72aefSHajimu UMEMOTO else 872becc44d7SSam Leffler info.rti_info[RTAX_IFP] = NULL; 873477180fbSGarrett Wollman /* 874477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 875477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 876477180fbSGarrett Wollman */ 877becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 878becc44d7SSam Leffler m = rt_msg1(cmd, &info); 879becc44d7SSam Leffler if (m == NULL) 880477180fbSGarrett Wollman return; 881477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 882477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 883477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 884becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 885477180fbSGarrett Wollman } 88652041295SPoul-Henning Kamp 887df8bae1dSRodney W. Grimes /* 8887b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 8897b6edd04SRuslan Ermilov * network interface arrival and departure. 8907b6edd04SRuslan Ermilov */ 8917b6edd04SRuslan Ermilov void 892becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 8937b6edd04SRuslan Ermilov { 8947b6edd04SRuslan Ermilov struct if_announcemsghdr *ifan; 8957b6edd04SRuslan Ermilov struct mbuf *m; 8967b6edd04SRuslan Ermilov struct rt_addrinfo info; 8977b6edd04SRuslan Ermilov 8987b6edd04SRuslan Ermilov if (route_cb.any_count == 0) 8997b6edd04SRuslan Ermilov return; 9007b6edd04SRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 9017b6edd04SRuslan Ermilov m = rt_msg1(RTM_IFANNOUNCE, &info); 9027b6edd04SRuslan Ermilov if (m == NULL) 9037b6edd04SRuslan Ermilov return; 9047b6edd04SRuslan Ermilov ifan = mtod(m, struct if_announcemsghdr *); 9057b6edd04SRuslan Ermilov ifan->ifan_index = ifp->if_index; 9069bf40edeSBrooks Davis strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 9077b6edd04SRuslan Ermilov ifan->ifan_what = what; 908becc44d7SSam Leffler rt_dispatch(m, NULL); 909becc44d7SSam Leffler } 910becc44d7SSam Leffler 911becc44d7SSam Leffler static void 912becc44d7SSam Leffler rt_dispatch(struct mbuf *m, struct sockaddr *sa) 913becc44d7SSam Leffler { 914becc44d7SSam Leffler struct sockproto route_proto; 915becc44d7SSam Leffler 916becc44d7SSam Leffler route_proto.sp_family = PF_ROUTE; 917becc44d7SSam Leffler route_proto.sp_protocol = sa ? sa->sa_family : 0; 9187b6edd04SRuslan Ermilov raw_input(m, &route_proto, &route_src, &route_dst); 9197b6edd04SRuslan Ermilov } 9207b6edd04SRuslan Ermilov 9217b6edd04SRuslan Ermilov /* 922df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 923df8bae1dSRodney W. Grimes */ 92437c84183SPoul-Henning Kamp static int 925becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 926df8bae1dSRodney W. Grimes { 927becc44d7SSam Leffler struct walkarg *w = vw; 928becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 929df8bae1dSRodney W. Grimes int error = 0, size; 930df8bae1dSRodney W. Grimes struct rt_addrinfo info; 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 933df8bae1dSRodney W. Grimes return 0; 934df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 935becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 936becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 937becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 938becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 93928070a0eSRuslan Ermilov if (rt->rt_ifp) { 940becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 941becc44d7SSam Leffler TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr; 942becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 94328070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 944becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 94528070a0eSRuslan Ermilov } 946df8bae1dSRodney W. Grimes size = rt_msg2(RTM_GET, &info, 0, w); 94752041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 948becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 949df8bae1dSRodney W. Grimes 950df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 951df8bae1dSRodney W. Grimes rtm->rtm_use = rt->rt_use; 952df8bae1dSRodney W. Grimes rtm->rtm_rmx = rt->rt_rmx; 953df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 954df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 955df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 95652041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 95752041295SPoul-Henning Kamp return (error); 958df8bae1dSRodney W. Grimes } 959df8bae1dSRodney W. Grimes return (error); 960df8bae1dSRodney W. Grimes } 961df8bae1dSRodney W. Grimes 96237c84183SPoul-Henning Kamp static int 963becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 964df8bae1dSRodney W. Grimes { 965becc44d7SSam Leffler struct ifnet *ifp; 966becc44d7SSam Leffler struct ifaddr *ifa; 967df8bae1dSRodney W. Grimes struct rt_addrinfo info; 968df8bae1dSRodney W. Grimes int len, error = 0; 969df8bae1dSRodney W. Grimes 970df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 971b30a244cSJeffrey Hsu /* IFNET_RLOCK(); */ /* could sleep XXX */ 972fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 973df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 974df8bae1dSRodney W. Grimes continue; 97522f29826SPoul-Henning Kamp ifa = TAILQ_FIRST(&ifp->if_addrhead); 976becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 977df8bae1dSRodney W. Grimes len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w); 978becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 0; 97952041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 980becc44d7SSam Leffler struct if_msghdr *ifm; 981df8bae1dSRodney W. Grimes 982df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 983df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 98462f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 985df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 986df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 98752041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 988df440948SPoul-Henning Kamp if (error) 989a35b06c5SJonathan Lemon goto done; 990df8bae1dSRodney W. Grimes } 99122f29826SPoul-Henning Kamp while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) { 992df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 993df8bae1dSRodney W. Grimes continue; 994a854ed98SJohn Baldwin if (jailed(curthread->td_ucred) && 995a854ed98SJohn Baldwin prison_if(curthread->td_ucred, ifa->ifa_addr)) 99675c13541SPoul-Henning Kamp continue; 997becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 998becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 999becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1000df8bae1dSRodney W. Grimes len = rt_msg2(RTM_NEWADDR, &info, 0, w); 100152041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1002becc44d7SSam Leffler struct ifa_msghdr *ifam; 1003df8bae1dSRodney W. Grimes 1004df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1005df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1006df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1007df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1008df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 100952041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1010df440948SPoul-Henning Kamp if (error) 1011a35b06c5SJonathan Lemon goto done; 1012df8bae1dSRodney W. Grimes } 1013df8bae1dSRodney W. Grimes } 1014becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1015becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 0; 1016df8bae1dSRodney W. Grimes } 1017a35b06c5SJonathan Lemon done: 1018b30a244cSJeffrey Hsu /* IFNET_RUNLOCK(); */ /* XXX */ 1019a35b06c5SJonathan Lemon return (error); 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 102205b2efe0SBruce M Simpson int 102305b2efe0SBruce M Simpson sysctl_ifmalist(af, w) 102405b2efe0SBruce M Simpson int af; 102505b2efe0SBruce M Simpson register struct walkarg *w; 102605b2efe0SBruce M Simpson { 102705b2efe0SBruce M Simpson register struct ifnet *ifp; 102805b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 102905b2efe0SBruce M Simpson struct rt_addrinfo info; 103005b2efe0SBruce M Simpson int len, error = 0; 103105b2efe0SBruce M Simpson 103205b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 103305b2efe0SBruce M Simpson /* IFNET_RLOCK(); */ /* could sleep XXX */ 103405b2efe0SBruce M Simpson TAILQ_FOREACH(ifp, &ifnet, if_link) { 103505b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 103605b2efe0SBruce M Simpson continue; 103705b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 103805b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 103905b2efe0SBruce M Simpson continue; 104005b2efe0SBruce M Simpson if (jailed(curproc->p_ucred) && 104105b2efe0SBruce M Simpson prison_if(curproc->p_ucred, ifma->ifma_addr)) 104205b2efe0SBruce M Simpson continue; 104305b2efe0SBruce M Simpson info.rti_addrs = RTA_IFA; 104405b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 104505b2efe0SBruce M Simpson if (TAILQ_FIRST(&ifp->if_addrhead)) { 104605b2efe0SBruce M Simpson info.rti_addrs |= RTA_IFP; 104705b2efe0SBruce M Simpson info.rti_info[RTAX_IFP] = 104805b2efe0SBruce M Simpson TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr; 104905b2efe0SBruce M Simpson } else 105005b2efe0SBruce M Simpson info.rti_info[RTAX_IFP] = NULL; 105105b2efe0SBruce M Simpson 105205b2efe0SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_LINK) { 105305b2efe0SBruce M Simpson info.rti_addrs |= RTA_GATEWAY; 105405b2efe0SBruce M Simpson info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 105505b2efe0SBruce M Simpson } else 105605b2efe0SBruce M Simpson info.rti_info[RTAX_GATEWAY] = NULL; 105705b2efe0SBruce M Simpson 105805b2efe0SBruce M Simpson len = rt_msg2(RTM_NEWMADDR, &info, 0, w); 105905b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 106005b2efe0SBruce M Simpson register struct ifma_msghdr *ifmam; 106105b2efe0SBruce M Simpson 106205b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 106305b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 106405b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 106505b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 106605b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 106705b2efe0SBruce M Simpson if (error) 106805b2efe0SBruce M Simpson goto done; 106905b2efe0SBruce M Simpson } 107005b2efe0SBruce M Simpson } 107105b2efe0SBruce M Simpson } 107205b2efe0SBruce M Simpson done: 107305b2efe0SBruce M Simpson /* IFNET_RUNLOCK(); */ /* XXX */ 107405b2efe0SBruce M Simpson return (error); 107505b2efe0SBruce M Simpson } 107605b2efe0SBruce M Simpson 107752041295SPoul-Henning Kamp static int 107882d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1079df8bae1dSRodney W. Grimes { 108052041295SPoul-Henning Kamp int *name = (int *)arg1; 108152041295SPoul-Henning Kamp u_int namelen = arg2; 1082becc44d7SSam Leffler struct radix_node_head *rnh; 1083df8bae1dSRodney W. Grimes int i, s, error = EINVAL; 1084df8bae1dSRodney W. Grimes u_char af; 1085df8bae1dSRodney W. Grimes struct walkarg w; 1086df8bae1dSRodney W. Grimes 108752041295SPoul-Henning Kamp name ++; 108852041295SPoul-Henning Kamp namelen--; 108952041295SPoul-Henning Kamp if (req->newptr) 1090df8bae1dSRodney W. Grimes return (EPERM); 1091df8bae1dSRodney W. Grimes if (namelen != 3) 1092f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1093df8bae1dSRodney W. Grimes af = name[0]; 1094b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1095b2aaf46eSJeffrey Hsu return (EINVAL); 1096df8bae1dSRodney W. Grimes Bzero(&w, sizeof(w)); 1097df8bae1dSRodney W. Grimes w.w_op = name[1]; 1098df8bae1dSRodney W. Grimes w.w_arg = name[2]; 109952041295SPoul-Henning Kamp w.w_req = req; 1100df8bae1dSRodney W. Grimes 1101df8bae1dSRodney W. Grimes s = splnet(); 1102df8bae1dSRodney W. Grimes switch (w.w_op) { 1103df8bae1dSRodney W. Grimes 1104df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1105df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1106956b0b65SJeffrey Hsu if (af != 0) { 1107956b0b65SJeffrey Hsu if ((rnh = rt_tables[af]) != NULL) { 11087701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_LOCK(rnh); */ 1109956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 11107701e15bSJeffrey Hsu sysctl_dumpentry, &w);/* could sleep XXX */ 11117701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_UNLOCK(rnh); */ 1112956b0b65SJeffrey Hsu } else 1113956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 1114956b0b65SJeffrey Hsu } else { 1115df8bae1dSRodney W. Grimes for (i = 1; i <= AF_MAX; i++) 1116956b0b65SJeffrey Hsu if ((rnh = rt_tables[i]) != NULL) { 11177701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_LOCK(rnh); */ 1118956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 1119956b0b65SJeffrey Hsu sysctl_dumpentry, &w); 11207701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_UNLOCK(rnh); */ 1121956b0b65SJeffrey Hsu if (error) 1122df8bae1dSRodney W. Grimes break; 1123956b0b65SJeffrey Hsu } 1124956b0b65SJeffrey Hsu } 1125df8bae1dSRodney W. Grimes break; 1126df8bae1dSRodney W. Grimes 1127df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1128df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 112905b2efe0SBruce M Simpson break; 113005b2efe0SBruce M Simpson 113105b2efe0SBruce M Simpson case NET_RT_IFMALIST: 113205b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 113305b2efe0SBruce M Simpson break; 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes splx(s); 1136df8bae1dSRodney W. Grimes if (w.w_tmem) 1137df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1138df8bae1dSRodney W. Grimes return (error); 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes 114152041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 114252041295SPoul-Henning Kamp 1143df8bae1dSRodney W. Grimes /* 1144df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1145df8bae1dSRodney W. Grimes */ 1146df8bae1dSRodney W. Grimes 1147df8bae1dSRodney W. Grimes extern struct domain routedomain; /* or at least forward */ 1148df8bae1dSRodney W. Grimes 114952041295SPoul-Henning Kamp static struct protosw routesw[] = { 1150df8bae1dSRodney W. Grimes { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 11516ddbf1e2SGary Palmer 0, route_output, raw_ctlinput, 0, 1152a29f300eSGarrett Wollman 0, 1153a29f300eSGarrett Wollman raw_init, 0, 0, 0, 1154a29f300eSGarrett Wollman &route_usrreqs 1155df8bae1dSRodney W. Grimes } 1156df8bae1dSRodney W. Grimes }; 1157df8bae1dSRodney W. Grimes 115852041295SPoul-Henning Kamp static struct domain routedomain = 1159cb64988fSLuoqi Chen { PF_ROUTE, "route", 0, 0, 0, 1160df8bae1dSRodney W. Grimes routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 116178a82810SGarrett Wollman 1162748e0b0aSGarrett Wollman DOMAIN_SET(route); 1163