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 * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 2928070a0eSRuslan Ermilov * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33df8bae1dSRodney W. Grimes #include <sys/param.h> 34960ed29cSSeigo Tanimura #include <sys/domain.h> 35748e0b0aSGarrett Wollman #include <sys/kernel.h> 36960ed29cSSeigo Tanimura #include <sys/jail.h> 374d1d4912SBruce Evans #include <sys/malloc.h> 38df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 39960ed29cSSeigo Tanimura #include <sys/proc.h> 40960ed29cSSeigo Tanimura #include <sys/protosw.h> 41960ed29cSSeigo Tanimura #include <sys/signalvar.h> 42df8bae1dSRodney W. Grimes #include <sys/socket.h> 43df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 44960ed29cSSeigo Tanimura #include <sys/sysctl.h> 45960ed29cSSeigo Tanimura #include <sys/systm.h> 46df8bae1dSRodney W. Grimes 47df8bae1dSRodney W. Grimes #include <net/if.h> 48df8bae1dSRodney W. Grimes #include <net/raw_cb.h> 49960ed29cSSeigo Tanimura #include <net/route.h> 50df8bae1dSRodney W. Grimes 51a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 52a1c995b6SPoul-Henning Kamp 53becc44d7SSam Leffler /* NB: these are not modified */ 5452041295SPoul-Henning Kamp static struct sockaddr route_dst = { 2, PF_ROUTE, }; 5552041295SPoul-Henning Kamp static struct sockaddr route_src = { 2, PF_ROUTE, }; 56076d0761SJulian Elischer static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 57becc44d7SSam Leffler 58becc44d7SSam Leffler static struct { 59becc44d7SSam Leffler int ip_count; /* attacked w/ AF_INET */ 60becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 61becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 62becc44d7SSam Leffler int any_count; /* total attached */ 63becc44d7SSam Leffler } route_cb; 64df8bae1dSRodney W. Grimes 65aea8b30fSSam Leffler struct mtx rtsock_mtx; 66aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 67aea8b30fSSam Leffler 68aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 69aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 70aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 71aea8b30fSSam Leffler 72df8bae1dSRodney W. Grimes struct walkarg { 7352041295SPoul-Henning Kamp int w_tmemsize; 7452041295SPoul-Henning Kamp int w_op, w_arg; 7552041295SPoul-Henning Kamp caddr_t w_tmem; 7652041295SPoul-Henning Kamp struct sysctl_req *w_req; 77df8bae1dSRodney W. Grimes }; 78df8bae1dSRodney W. Grimes 795dfc91d7SLuigi Rizzo static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 805dfc91d7SLuigi Rizzo static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 815dfc91d7SLuigi Rizzo caddr_t cp, struct walkarg *w); 825dfc91d7SLuigi Rizzo static int rt_xaddrs(caddr_t cp, caddr_t cplim, 835dfc91d7SLuigi Rizzo struct rt_addrinfo *rtinfo); 84929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 85929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 8605b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 875dfc91d7SLuigi Rizzo static int route_output(struct mbuf *m, struct socket *so); 885dfc91d7SLuigi Rizzo static void rt_setmetrics(u_long which, const struct rt_metrics *in, 895dfc91d7SLuigi Rizzo struct rt_metrics_lite *out); 905dfc91d7SLuigi Rizzo static void rt_getmetrics(const struct rt_metrics_lite *in, 915dfc91d7SLuigi Rizzo struct rt_metrics *out); 925dfc91d7SLuigi Rizzo static void rt_dispatch(struct mbuf *, const struct sockaddr *); 93df8bae1dSRodney W. Grimes 94a29f300eSGarrett Wollman /* 95a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 96a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 97a29f300eSGarrett Wollman */ 9852041295SPoul-Henning Kamp static int 99a29f300eSGarrett Wollman rts_abort(struct socket *so) 100df8bae1dSRodney W. Grimes { 101a29f300eSGarrett Wollman int s, error; 102df8bae1dSRodney W. Grimes s = splnet(); 103a29f300eSGarrett Wollman error = raw_usrreqs.pru_abort(so); 104df8bae1dSRodney W. Grimes splx(s); 105a29f300eSGarrett Wollman return error; 106df8bae1dSRodney W. Grimes } 107a29f300eSGarrett Wollman 108a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 109a29f300eSGarrett Wollman 110a29f300eSGarrett Wollman static int 111b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 112a29f300eSGarrett Wollman { 113a29f300eSGarrett Wollman struct rawcb *rp; 114a29f300eSGarrett Wollman int s, error; 115a29f300eSGarrett Wollman 1165dfc91d7SLuigi Rizzo if (sotorawcb(so) != NULL) 117a29f300eSGarrett Wollman return EISCONN; /* XXX panic? */ 1187cc0979fSDavid Malone /* XXX */ 119a163d034SWarner Losh MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 1205dfc91d7SLuigi Rizzo if (rp == NULL) 121a29f300eSGarrett Wollman return ENOBUFS; 122a29f300eSGarrett Wollman 123a29f300eSGarrett Wollman /* 124a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 125a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 126a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 127a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 128a29f300eSGarrett Wollman * eliminate the spl. 129a29f300eSGarrett Wollman */ 130a29f300eSGarrett Wollman s = splnet(); 131a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 132162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 133a29f300eSGarrett Wollman rp = sotorawcb(so); 134a29f300eSGarrett Wollman if (error) { 135a29f300eSGarrett Wollman splx(s); 1367ba271aeSJonathan Chen so->so_pcb = NULL; 137a29f300eSGarrett Wollman free(rp, M_PCB); 138a29f300eSGarrett Wollman return error; 139a29f300eSGarrett Wollman } 140aea8b30fSSam Leffler RTSOCK_LOCK(); 141a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 142a29f300eSGarrett Wollman case AF_INET: 143df8bae1dSRodney W. Grimes route_cb.ip_count++; 144a29f300eSGarrett Wollman break; 145899ce4f4SYoshinobu Inoue case AF_INET6: 146899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 147899ce4f4SYoshinobu Inoue break; 148a29f300eSGarrett Wollman case AF_IPX: 149cc6a66f2SJulian Elischer route_cb.ipx_count++; 150a29f300eSGarrett Wollman break; 151a29f300eSGarrett Wollman } 152df8bae1dSRodney W. Grimes rp->rcb_faddr = &route_src; 153df8bae1dSRodney W. Grimes route_cb.any_count++; 154aea8b30fSSam Leffler RTSOCK_UNLOCK(); 15503e49181SSeigo Tanimura soisconnected(so); 156df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 157df8bae1dSRodney W. Grimes splx(s); 158a29f300eSGarrett Wollman return 0; 159df8bae1dSRodney W. Grimes } 160df8bae1dSRodney W. Grimes 161a29f300eSGarrett Wollman static int 162b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 163a29f300eSGarrett Wollman { 164a29f300eSGarrett Wollman int s, error; 165a29f300eSGarrett Wollman s = splnet(); 166b40ce416SJulian Elischer error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */ 167a29f300eSGarrett Wollman splx(s); 168a29f300eSGarrett Wollman return error; 169a29f300eSGarrett Wollman } 170a29f300eSGarrett Wollman 171a29f300eSGarrett Wollman static int 172b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 173a29f300eSGarrett Wollman { 174a29f300eSGarrett Wollman int s, error; 175a29f300eSGarrett Wollman s = splnet(); 176b40ce416SJulian Elischer error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */ 177a29f300eSGarrett Wollman splx(s); 178a29f300eSGarrett Wollman return error; 179a29f300eSGarrett Wollman } 180a29f300eSGarrett Wollman 181a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 182a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 183a29f300eSGarrett Wollman 184a29f300eSGarrett Wollman static int 185a29f300eSGarrett Wollman rts_detach(struct socket *so) 186a29f300eSGarrett Wollman { 187a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 188a29f300eSGarrett Wollman int s, error; 189a29f300eSGarrett Wollman 190a29f300eSGarrett Wollman s = splnet(); 1915dfc91d7SLuigi Rizzo if (rp != NULL) { 192aea8b30fSSam Leffler RTSOCK_LOCK(); 193a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 194a29f300eSGarrett Wollman case AF_INET: 195a29f300eSGarrett Wollman route_cb.ip_count--; 196a29f300eSGarrett Wollman break; 197899ce4f4SYoshinobu Inoue case AF_INET6: 198899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 199899ce4f4SYoshinobu Inoue break; 200a29f300eSGarrett Wollman case AF_IPX: 201a29f300eSGarrett Wollman route_cb.ipx_count--; 202a29f300eSGarrett Wollman break; 203a29f300eSGarrett Wollman } 204a29f300eSGarrett Wollman route_cb.any_count--; 205aea8b30fSSam Leffler RTSOCK_UNLOCK(); 206a29f300eSGarrett Wollman } 207a29f300eSGarrett Wollman error = raw_usrreqs.pru_detach(so); 208a29f300eSGarrett Wollman splx(s); 209a29f300eSGarrett Wollman return error; 210a29f300eSGarrett Wollman } 211a29f300eSGarrett Wollman 212a29f300eSGarrett Wollman static int 213a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 214a29f300eSGarrett Wollman { 215a29f300eSGarrett Wollman int s, error; 216a29f300eSGarrett Wollman s = splnet(); 217a29f300eSGarrett Wollman error = raw_usrreqs.pru_disconnect(so); 218a29f300eSGarrett Wollman splx(s); 219a29f300eSGarrett Wollman return error; 220a29f300eSGarrett Wollman } 221a29f300eSGarrett Wollman 222a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 223a29f300eSGarrett Wollman 224a29f300eSGarrett Wollman static int 22557bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 226a29f300eSGarrett Wollman { 227a29f300eSGarrett Wollman int s, error; 228a29f300eSGarrett Wollman s = splnet(); 229a29f300eSGarrett Wollman error = raw_usrreqs.pru_peeraddr(so, nam); 230a29f300eSGarrett Wollman splx(s); 231a29f300eSGarrett Wollman return error; 232a29f300eSGarrett Wollman } 233a29f300eSGarrett Wollman 234a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 235a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 236a29f300eSGarrett Wollman 237a29f300eSGarrett Wollman static int 23857bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 239b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 240a29f300eSGarrett Wollman { 241a29f300eSGarrett Wollman int s, error; 242a29f300eSGarrett Wollman s = splnet(); 243b40ce416SJulian Elischer error = raw_usrreqs.pru_send(so, flags, m, nam, control, td); 244a29f300eSGarrett Wollman splx(s); 245a29f300eSGarrett Wollman return error; 246a29f300eSGarrett Wollman } 247a29f300eSGarrett Wollman 248a29f300eSGarrett Wollman /* pru_sense is null */ 249a29f300eSGarrett Wollman 250a29f300eSGarrett Wollman static int 251a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 252a29f300eSGarrett Wollman { 253a29f300eSGarrett Wollman int s, error; 254a29f300eSGarrett Wollman s = splnet(); 255a29f300eSGarrett Wollman error = raw_usrreqs.pru_shutdown(so); 256a29f300eSGarrett Wollman splx(s); 257a29f300eSGarrett Wollman return error; 258a29f300eSGarrett Wollman } 259a29f300eSGarrett Wollman 260a29f300eSGarrett Wollman static int 26157bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 262a29f300eSGarrett Wollman { 263a29f300eSGarrett Wollman int s, error; 264a29f300eSGarrett Wollman s = splnet(); 265a29f300eSGarrett Wollman error = raw_usrreqs.pru_sockaddr(so, nam); 266a29f300eSGarrett Wollman splx(s); 267a29f300eSGarrett Wollman return error; 268a29f300eSGarrett Wollman } 269a29f300eSGarrett Wollman 270a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 271a29f300eSGarrett Wollman rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect, 272a29f300eSGarrett Wollman pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect, 273a29f300eSGarrett Wollman pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp, 274a29f300eSGarrett Wollman rts_send, pru_sense_null, rts_shutdown, rts_sockaddr, 275a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 276a29f300eSGarrett Wollman }; 277a29f300eSGarrett Wollman 278df8bae1dSRodney W. Grimes /*ARGSUSED*/ 27952041295SPoul-Henning Kamp static int 2805dfc91d7SLuigi Rizzo route_output(struct mbuf *m, struct socket *so) 281df8bae1dSRodney W. Grimes { 282becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 2835dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = NULL; 2845dfc91d7SLuigi Rizzo struct rtentry *rt = NULL; 28578a82810SGarrett Wollman struct radix_node_head *rnh; 286df8bae1dSRodney W. Grimes struct rt_addrinfo info; 287df8bae1dSRodney W. Grimes int len, error = 0; 2885dfc91d7SLuigi Rizzo struct ifnet *ifp = NULL; 2895dfc91d7SLuigi Rizzo struct ifaddr *ifa = NULL; 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 2925dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 2935dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 294df8bae1dSRodney W. Grimes return (ENOBUFS); 295df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 296df8bae1dSRodney W. Grimes panic("route_output"); 297df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 298df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 299df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 3005dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 301df8bae1dSRodney W. Grimes senderr(EINVAL); 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 3045dfc91d7SLuigi Rizzo if (rtm == NULL) { 3055dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 306df8bae1dSRodney W. Grimes senderr(ENOBUFS); 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 309df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 3105dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 311df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 312df8bae1dSRodney W. Grimes } 313df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 3148071913dSRuslan Ermilov bzero(&info, sizeof(info)); 315df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 316076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 3175dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 318076d0761SJulian Elischer senderr(EINVAL); 319076d0761SJulian Elischer } 3208071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 3215dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 322becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 3235dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 324becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 325df8bae1dSRodney W. Grimes senderr(EINVAL); 326becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) { 327df8bae1dSRodney W. Grimes struct radix_node *t; 328becc44d7SSam Leffler t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1); 3296b96f1afSLuigi Rizzo /* XXX why this odd cast to (caddr_t *) ? Maybe wrong ? */ 3306b96f1afSLuigi Rizzo 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: 3505dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 351df8bae1dSRodney W. Grimes senderr(EINVAL); 3525dfc91d7SLuigi Rizzo saved_nrt = NULL; 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); 3587138d65cSSam Leffler RT_REMREF(saved_nrt); 359becc44d7SSam Leffler saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 360d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes break; 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes case RTM_DELETE: 3655dfc91d7SLuigi Rizzo saved_nrt = NULL; 3668071913dSRuslan Ermilov error = rtrequest1(RTM_DELETE, &info, &saved_nrt); 36778a82810SGarrett Wollman if (error == 0) { 368d1dd20beSSam Leffler RT_LOCK(saved_nrt); 36971eba915SRuslan Ermilov rt = saved_nrt; 37078a82810SGarrett Wollman goto report; 37178a82810SGarrett Wollman } 372df8bae1dSRodney W. Grimes break; 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes case RTM_GET: 375df8bae1dSRodney W. Grimes case RTM_CHANGE: 376df8bae1dSRodney W. Grimes case RTM_LOCK: 377becc44d7SSam Leffler rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family]; 3785dfc91d7SLuigi Rizzo if (rnh == NULL) 37978a82810SGarrett Wollman senderr(EAFNOSUPPORT); 380956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 381becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 382becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 383956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 384becc44d7SSam Leffler if (rt == NULL) /* XXX looks bogus */ 385df8bae1dSRodney W. Grimes senderr(ESRCH); 386d1dd20beSSam Leffler RT_LOCK(rt); 3877138d65cSSam Leffler RT_ADDREF(rt); 388956b0b65SJeffrey Hsu 389df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 390df8bae1dSRodney W. Grimes 391df8bae1dSRodney W. Grimes case RTM_GET: 39278a82810SGarrett Wollman report: 393d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 394becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 395becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 396becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 397becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 398df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 399df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 400df440948SPoul-Henning Kamp if (ifp) { 4019b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 4029b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 403becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 404becc44d7SSam Leffler rt->rt_ifa->ifa_addr; 40528070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 406becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 407becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 408df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 409df8bae1dSRodney W. Grimes } else { 4105dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 4115dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 412df8bae1dSRodney W. Grimes } 413df8bae1dSRodney W. Grimes } 414913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 415df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 416df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 417df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 4185dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 419d1dd20beSSam Leffler RT_UNLOCK(rt); 420df8bae1dSRodney W. Grimes senderr(ENOBUFS); 421becc44d7SSam Leffler } 4226b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 423df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 424df8bae1dSRodney W. Grimes } 425913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 426df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 42797d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 428df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 429df8bae1dSRodney W. Grimes break; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes case RTM_CHANGE: 432becc44d7SSam Leffler /* 433becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 434becc44d7SSam Leffler * flags may also be different; ifp may be specified 435becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 436becc44d7SSam Leffler */ 437becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 438becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 439becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 440becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 441becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 442becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 443becc44d7SSam Leffler if ((error = rt_getifa(&info)) != 0) { 444d1dd20beSSam Leffler RT_UNLOCK(rt); 4458071913dSRuslan Ermilov senderr(error); 44602a5d63eSBrian Somers } 447becc44d7SSam Leffler } 448becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] != NULL && 449becc44d7SSam Leffler (error = rt_setgate(rt, rt_key(rt), 450becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY])) != 0) { 451d1dd20beSSam Leffler RT_UNLOCK(rt); 4528071913dSRuslan Ermilov senderr(error); 453becc44d7SSam Leffler } 4548071913dSRuslan Ermilov if ((ifa = info.rti_ifa) != NULL) { 455becc44d7SSam Leffler struct ifaddr *oifa = rt->rt_ifa; 456df8bae1dSRodney W. Grimes if (oifa != ifa) { 45719fc74fbSJeffrey Hsu if (oifa) { 45819fc74fbSJeffrey Hsu if (oifa->ifa_rtrequest) 459becc44d7SSam Leffler oifa->ifa_rtrequest( 460becc44d7SSam Leffler RTM_DELETE, rt, 4618071913dSRuslan Ermilov &info); 46212e552d6SJeffrey Hsu IFAFREE(oifa); 46319fc74fbSJeffrey Hsu } 46419fc74fbSJeffrey Hsu IFAREF(ifa); 465df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 4668071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 470df8bae1dSRodney W. Grimes &rt->rt_rmx); 471df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 4728071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 473becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) 474becc44d7SSam Leffler rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 47593b0017fSPhilippe Charnier /* FALLTHROUGH */ 476df8bae1dSRodney W. Grimes case RTM_LOCK: 47797d8d152SAndre Oppermann /* We don't support locks anymore */ 478df8bae1dSRodney W. Grimes break; 479df8bae1dSRodney W. Grimes } 480d1dd20beSSam Leffler RT_UNLOCK(rt); 481df8bae1dSRodney W. Grimes break; 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes default: 484df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes flush: 488df8bae1dSRodney W. Grimes if (rtm) { 489df8bae1dSRodney W. Grimes if (error) 490df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 491df8bae1dSRodney W. Grimes else 492df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 493df8bae1dSRodney W. Grimes } 494becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 495becc44d7SSam Leffler RTFREE(rt); 496df8bae1dSRodney W. Grimes { 4975dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 498df8bae1dSRodney W. Grimes /* 499df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 500df8bae1dSRodney W. Grimes */ 501df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 502df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 503df8bae1dSRodney W. Grimes if (rtm) 504df8bae1dSRodney W. Grimes Free(rtm); 505df8bae1dSRodney W. Grimes m_freem(m); 506df8bae1dSRodney W. Grimes return (error); 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 509df8bae1dSRodney W. Grimes rp = sotorawcb(so); 5104cc20ab1SSeigo Tanimura } 511df8bae1dSRodney W. Grimes if (rtm) { 512df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 51303311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 51403311056SHajimu UMEMOTO m_freem(m); 51503311056SHajimu UMEMOTO m = NULL; 51603311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 51703311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 518df8bae1dSRodney W. Grimes Free(rtm); 519df8bae1dSRodney W. Grimes } 520becc44d7SSam Leffler if (m) { 521becc44d7SSam Leffler if (rp) { 522becc44d7SSam Leffler /* 523becc44d7SSam Leffler * XXX insure we don't get a copy by 524becc44d7SSam Leffler * invalidating our protocol 525becc44d7SSam Leffler */ 526becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 527becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 528becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 529becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 530becc44d7SSam Leffler } else 531becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 532becc44d7SSam Leffler } 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes return (error); 535becc44d7SSam Leffler #undef sa_equal 536df8bae1dSRodney W. Grimes } 537df8bae1dSRodney W. Grimes 53852041295SPoul-Henning Kamp static void 5395dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 5405dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 541df8bae1dSRodney W. Grimes { 542df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 54397d8d152SAndre Oppermann /* 54497d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 54597d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 54697d8d152SAndre Oppermann */ 547df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 548df8bae1dSRodney W. Grimes metric(RTV_EXPIRE, rmx_expire); 549df8bae1dSRodney W. Grimes #undef metric 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes 55297d8d152SAndre Oppermann static void 5535dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 55497d8d152SAndre Oppermann { 55597d8d152SAndre Oppermann #define metric(e) out->e = in->e; 55697d8d152SAndre Oppermann bzero(out, sizeof(*out)); 55797d8d152SAndre Oppermann metric(rmx_mtu); 55897d8d152SAndre Oppermann metric(rmx_expire); 55997d8d152SAndre Oppermann #undef metric 56097d8d152SAndre Oppermann } 56197d8d152SAndre Oppermann 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 { 570e74642dfSLuigi Rizzo struct sockaddr *sa; 571e74642dfSLuigi Rizzo int i; 572df8bae1dSRodney W. Grimes 573becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 574df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 575df8bae1dSRodney W. Grimes continue; 576ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 5777f33a738SJulian Elischer /* 578076d0761SJulian Elischer * It won't fit. 5797f33a738SJulian Elischer */ 580becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 581076d0761SJulian Elischer return (EINVAL); 5827f33a738SJulian Elischer /* 5837f33a738SJulian Elischer * there are no more.. quit now 5847f33a738SJulian Elischer * If there are more bits, they are in error. 5857f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 5867f33a738SJulian Elischer * This causes kernel to core dump. 587076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 5887f33a738SJulian Elischer */ 589076d0761SJulian Elischer if (sa->sa_len == 0) { 590076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 591076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 592df8bae1dSRodney W. Grimes } 593076d0761SJulian Elischer /* accept it */ 594076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 595e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 596076d0761SJulian Elischer } 597076d0761SJulian Elischer return (0); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes static struct mbuf * 601becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 602df8bae1dSRodney W. Grimes { 6035dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 6045dfc91d7SLuigi Rizzo struct mbuf *m; 6055dfc91d7SLuigi Rizzo int i; 6065dfc91d7SLuigi Rizzo struct sockaddr *sa; 607df8bae1dSRodney W. Grimes int len, dlen; 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes switch (type) { 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes case RTM_DELADDR: 612df8bae1dSRodney W. Grimes case RTM_NEWADDR: 613df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 614df8bae1dSRodney W. Grimes break; 615df8bae1dSRodney W. Grimes 616477180fbSGarrett Wollman case RTM_DELMADDR: 617477180fbSGarrett Wollman case RTM_NEWMADDR: 618477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 619477180fbSGarrett Wollman break; 620477180fbSGarrett Wollman 621df8bae1dSRodney W. Grimes case RTM_IFINFO: 622df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 623df8bae1dSRodney W. Grimes break; 624df8bae1dSRodney W. Grimes 6257b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 6267b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 6277b6edd04SRuslan Ermilov break; 6287b6edd04SRuslan Ermilov 629df8bae1dSRodney W. Grimes default: 630df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 631df8bae1dSRodney W. Grimes } 63233841545SHajimu UMEMOTO if (len > MCLBYTES) 633df8bae1dSRodney W. Grimes panic("rt_msg1"); 634a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 63533841545SHajimu UMEMOTO if (m && len > MHLEN) { 636a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 63733841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 63833841545SHajimu UMEMOTO m_free(m); 63933841545SHajimu UMEMOTO m = NULL; 64033841545SHajimu UMEMOTO } 64133841545SHajimu UMEMOTO } 6425dfc91d7SLuigi Rizzo if (m == NULL) 64333841545SHajimu UMEMOTO return (m); 644df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 6455dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 646df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 647df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 648df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 649df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 650df8bae1dSRodney W. Grimes continue; 651df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 652e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 653df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 654df8bae1dSRodney W. Grimes len += dlen; 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 657df8bae1dSRodney W. Grimes m_freem(m); 658df8bae1dSRodney W. Grimes return (NULL); 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 661df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 662df8bae1dSRodney W. Grimes rtm->rtm_type = type; 663df8bae1dSRodney W. Grimes return (m); 664df8bae1dSRodney W. Grimes } 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes static int 667becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 668df8bae1dSRodney W. Grimes { 6695dfc91d7SLuigi Rizzo int i; 670df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 671df8bae1dSRodney W. Grimes caddr_t cp0; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 674df8bae1dSRodney W. Grimes again: 675df8bae1dSRodney W. Grimes switch (type) { 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes case RTM_DELADDR: 678df8bae1dSRodney W. Grimes case RTM_NEWADDR: 679df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 680df8bae1dSRodney W. Grimes break; 681df8bae1dSRodney W. Grimes 682df8bae1dSRodney W. Grimes case RTM_IFINFO: 683df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 684df8bae1dSRodney W. Grimes break; 685df8bae1dSRodney W. Grimes 68605b2efe0SBruce M Simpson case RTM_NEWMADDR: 68705b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 68805b2efe0SBruce M Simpson break; 68905b2efe0SBruce M Simpson 690df8bae1dSRodney W. Grimes default: 691df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 692df8bae1dSRodney W. Grimes } 693df440948SPoul-Henning Kamp cp0 = cp; 694df440948SPoul-Henning Kamp if (cp0) 695df8bae1dSRodney W. Grimes cp += len; 696df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 6975dfc91d7SLuigi Rizzo struct sockaddr *sa; 698df8bae1dSRodney W. Grimes 6995dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 700df8bae1dSRodney W. Grimes continue; 701df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 702e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 703df8bae1dSRodney W. Grimes if (cp) { 704df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 705df8bae1dSRodney W. Grimes cp += dlen; 706df8bae1dSRodney W. Grimes } 707df8bae1dSRodney W. Grimes len += dlen; 708df8bae1dSRodney W. Grimes } 709694ff264SAndrew Gallatin len = ALIGN(len); 7105dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 7115dfc91d7SLuigi Rizzo struct walkarg *rw = w; 712df8bae1dSRodney W. Grimes 71352041295SPoul-Henning Kamp if (rw->w_req) { 714df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 715df8bae1dSRodney W. Grimes if (rw->w_tmem) 716df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 717df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 718df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 719df440948SPoul-Henning Kamp if (rw->w_tmem) 720df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 721df8bae1dSRodney W. Grimes } 722df8bae1dSRodney W. Grimes if (rw->w_tmem) { 723df8bae1dSRodney W. Grimes cp = rw->w_tmem; 724df8bae1dSRodney W. Grimes second_time = 1; 725df8bae1dSRodney W. Grimes goto again; 72652041295SPoul-Henning Kamp } 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes } 729df8bae1dSRodney W. Grimes if (cp) { 7305dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 731df8bae1dSRodney W. Grimes 732df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 733df8bae1dSRodney W. Grimes rtm->rtm_type = type; 734df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 735df8bae1dSRodney W. Grimes } 736df8bae1dSRodney W. Grimes return (len); 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes 739df8bae1dSRodney W. Grimes /* 740df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 741df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 742df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 743df8bae1dSRodney W. Grimes * destination. 744df8bae1dSRodney W. Grimes */ 745df8bae1dSRodney W. Grimes void 746becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 747df8bae1dSRodney W. Grimes { 748becc44d7SSam Leffler struct rt_msghdr *rtm; 749becc44d7SSam Leffler struct mbuf *m; 750df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 751df8bae1dSRodney W. Grimes 752df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 753df8bae1dSRodney W. Grimes return; 754df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 7555dfc91d7SLuigi Rizzo if (m == NULL) 756df8bae1dSRodney W. Grimes return; 757df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 758df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 759df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 760df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 761becc44d7SSam Leffler rt_dispatch(m, sa); 762df8bae1dSRodney W. Grimes } 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes /* 765df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 766df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 767df8bae1dSRodney W. Grimes */ 768df8bae1dSRodney W. Grimes void 769becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 770df8bae1dSRodney W. Grimes { 771becc44d7SSam Leffler struct if_msghdr *ifm; 772df8bae1dSRodney W. Grimes struct mbuf *m; 773df8bae1dSRodney W. Grimes struct rt_addrinfo info; 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 776df8bae1dSRodney W. Grimes return; 777df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 778df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 7795dfc91d7SLuigi Rizzo if (m == NULL) 780df8bae1dSRodney W. Grimes return; 781df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 782df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 78362f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 784df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 785df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 786becc44d7SSam Leffler rt_dispatch(m, NULL); 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes /* 790df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 791df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 792df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 793df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 794df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 795df8bae1dSRodney W. Grimes * copies of it. 796df8bae1dSRodney W. Grimes */ 797df8bae1dSRodney W. Grimes void 798becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 799df8bae1dSRodney W. Grimes { 800df8bae1dSRodney W. Grimes struct rt_addrinfo info; 8015dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 802df8bae1dSRodney W. Grimes int pass; 8035dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 804df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 805df8bae1dSRodney W. Grimes 806df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 807df8bae1dSRodney W. Grimes return; 808df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 809df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 810df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 811df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 8125dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 813df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 814df8bae1dSRodney W. Grimes 815becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 8169b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 8179b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 818becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 819becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 820df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 821df8bae1dSRodney W. Grimes continue; 822df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 823df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 824df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 825df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 826df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 829df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 8305dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 831df8bae1dSRodney W. Grimes 8325dfc91d7SLuigi Rizzo if (rt == NULL) 833df8bae1dSRodney W. Grimes continue; 834becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 835becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 836becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 837df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 838df8bae1dSRodney W. Grimes continue; 839df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 840df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 841df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 842df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 843df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 844df8bae1dSRodney W. Grimes } 845becc44d7SSam Leffler rt_dispatch(m, sa); 846df8bae1dSRodney W. Grimes } 847df8bae1dSRodney W. Grimes } 848df8bae1dSRodney W. Grimes 849477180fbSGarrett Wollman /* 850477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 851477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 852477180fbSGarrett Wollman * there is no route state to worry about. 853477180fbSGarrett Wollman */ 854477180fbSGarrett Wollman void 855becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 856477180fbSGarrett Wollman { 857477180fbSGarrett Wollman struct rt_addrinfo info; 8585dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 859477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 860477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 861477180fbSGarrett Wollman 862477180fbSGarrett Wollman if (route_cb.any_count == 0) 863477180fbSGarrett Wollman return; 864477180fbSGarrett Wollman 865477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 866becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 867becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 8689b98ee2cSLuigi Rizzo ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL; 869477180fbSGarrett Wollman /* 870477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 871477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 872477180fbSGarrett Wollman */ 873becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 874becc44d7SSam Leffler m = rt_msg1(cmd, &info); 875becc44d7SSam Leffler if (m == NULL) 876477180fbSGarrett Wollman return; 877477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 878477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 879477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 880becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 881477180fbSGarrett Wollman } 88252041295SPoul-Henning Kamp 883df8bae1dSRodney W. Grimes /* 8847b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 8857b6edd04SRuslan Ermilov * network interface arrival and departure. 8867b6edd04SRuslan Ermilov */ 8877b6edd04SRuslan Ermilov void 888becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 8897b6edd04SRuslan Ermilov { 8907b6edd04SRuslan Ermilov struct if_announcemsghdr *ifan; 8917b6edd04SRuslan Ermilov struct mbuf *m; 8927b6edd04SRuslan Ermilov struct rt_addrinfo info; 8937b6edd04SRuslan Ermilov 8947b6edd04SRuslan Ermilov if (route_cb.any_count == 0) 8957b6edd04SRuslan Ermilov return; 8967b6edd04SRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 8977b6edd04SRuslan Ermilov m = rt_msg1(RTM_IFANNOUNCE, &info); 8987b6edd04SRuslan Ermilov if (m == NULL) 8997b6edd04SRuslan Ermilov return; 9007b6edd04SRuslan Ermilov ifan = mtod(m, struct if_announcemsghdr *); 9017b6edd04SRuslan Ermilov ifan->ifan_index = ifp->if_index; 9029bf40edeSBrooks Davis strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 9037b6edd04SRuslan Ermilov ifan->ifan_what = what; 904becc44d7SSam Leffler rt_dispatch(m, NULL); 905becc44d7SSam Leffler } 906becc44d7SSam Leffler 907becc44d7SSam Leffler static void 9085dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 909becc44d7SSam Leffler { 910becc44d7SSam Leffler struct sockproto route_proto; 911becc44d7SSam Leffler 912becc44d7SSam Leffler route_proto.sp_family = PF_ROUTE; 913becc44d7SSam Leffler route_proto.sp_protocol = sa ? sa->sa_family : 0; 9147b6edd04SRuslan Ermilov raw_input(m, &route_proto, &route_src, &route_dst); 9157b6edd04SRuslan Ermilov } 9167b6edd04SRuslan Ermilov 9177b6edd04SRuslan Ermilov /* 918df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 919df8bae1dSRodney W. Grimes */ 92037c84183SPoul-Henning Kamp static int 921becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 922df8bae1dSRodney W. Grimes { 923becc44d7SSam Leffler struct walkarg *w = vw; 924becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 925df8bae1dSRodney W. Grimes int error = 0, size; 926df8bae1dSRodney W. Grimes struct rt_addrinfo info; 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 929df8bae1dSRodney W. Grimes return 0; 930df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 931becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 932becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 933becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 934becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 93528070a0eSRuslan Ermilov if (rt->rt_ifp) { 936becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 9379b98ee2cSLuigi Rizzo ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr; 938becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 93928070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 940becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 94128070a0eSRuslan Ermilov } 942913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 94352041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 944becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 945df8bae1dSRodney W. Grimes 946df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 94797d8d152SAndre Oppermann rtm->rtm_use = rt->rt_rmx.rmx_pksent; 94897d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 949df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 950df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 951df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 95252041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 95352041295SPoul-Henning Kamp return (error); 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes return (error); 956df8bae1dSRodney W. Grimes } 957df8bae1dSRodney W. Grimes 95837c84183SPoul-Henning Kamp static int 959becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 960df8bae1dSRodney W. Grimes { 961becc44d7SSam Leffler struct ifnet *ifp; 962becc44d7SSam Leffler struct ifaddr *ifa; 963df8bae1dSRodney W. Grimes struct rt_addrinfo info; 964df8bae1dSRodney W. Grimes int len, error = 0; 965df8bae1dSRodney W. Grimes 966df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 967b30a244cSJeffrey Hsu /* IFNET_RLOCK(); */ /* could sleep XXX */ 968fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 969df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 970df8bae1dSRodney W. Grimes continue; 9719b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 972becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 973913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 9745dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 97552041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 976becc44d7SSam Leffler struct if_msghdr *ifm; 977df8bae1dSRodney W. Grimes 978df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 979df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 98062f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 981df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 982df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 98352041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 984df440948SPoul-Henning Kamp if (error) 985a35b06c5SJonathan Lemon goto done; 986df8bae1dSRodney W. Grimes } 9875dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 988df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 989df8bae1dSRodney W. Grimes continue; 990a854ed98SJohn Baldwin if (jailed(curthread->td_ucred) && 991a854ed98SJohn Baldwin prison_if(curthread->td_ucred, ifa->ifa_addr)) 99275c13541SPoul-Henning Kamp continue; 993becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 994becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 995becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 996913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 99752041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 998becc44d7SSam Leffler struct ifa_msghdr *ifam; 999df8bae1dSRodney W. Grimes 1000df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1001df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1002df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1003df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1004df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 100552041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1006df440948SPoul-Henning Kamp if (error) 1007a35b06c5SJonathan Lemon goto done; 1008df8bae1dSRodney W. Grimes } 1009df8bae1dSRodney W. Grimes } 1010becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 10115dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1012df8bae1dSRodney W. Grimes } 1013a35b06c5SJonathan Lemon done: 1014b30a244cSJeffrey Hsu /* IFNET_RUNLOCK(); */ /* XXX */ 1015a35b06c5SJonathan Lemon return (error); 1016df8bae1dSRodney W. Grimes } 1017df8bae1dSRodney W. Grimes 101805b2efe0SBruce M Simpson int 10199b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 102005b2efe0SBruce M Simpson { 10219b98ee2cSLuigi Rizzo struct ifnet *ifp; 102205b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 102305b2efe0SBruce M Simpson struct rt_addrinfo info; 102405b2efe0SBruce M Simpson int len, error = 0; 10259b98ee2cSLuigi Rizzo struct ifaddr *ifa; 102605b2efe0SBruce M Simpson 102705b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 102805b2efe0SBruce M Simpson /* IFNET_RLOCK(); */ /* could sleep XXX */ 102905b2efe0SBruce M Simpson TAILQ_FOREACH(ifp, &ifnet, if_link) { 103005b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 103105b2efe0SBruce M Simpson continue; 10329b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 1033913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 103405b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 103505b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 103605b2efe0SBruce M Simpson continue; 103705b2efe0SBruce M Simpson if (jailed(curproc->p_ucred) && 103805b2efe0SBruce M Simpson prison_if(curproc->p_ucred, ifma->ifma_addr)) 103905b2efe0SBruce M Simpson continue; 104005b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1041913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1042913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1043913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1044913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 104505b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 10469b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 104705b2efe0SBruce M Simpson 104805b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 104905b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 105005b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 105105b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 105205b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 105305b2efe0SBruce M Simpson if (error) 105405b2efe0SBruce M Simpson goto done; 105505b2efe0SBruce M Simpson } 105605b2efe0SBruce M Simpson } 105705b2efe0SBruce M Simpson } 105805b2efe0SBruce M Simpson done: 105905b2efe0SBruce M Simpson /* IFNET_RUNLOCK(); */ /* XXX */ 106005b2efe0SBruce M Simpson return (error); 106105b2efe0SBruce M Simpson } 106205b2efe0SBruce M Simpson 106352041295SPoul-Henning Kamp static int 106482d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1065df8bae1dSRodney W. Grimes { 106652041295SPoul-Henning Kamp int *name = (int *)arg1; 106752041295SPoul-Henning Kamp u_int namelen = arg2; 1068becc44d7SSam Leffler struct radix_node_head *rnh; 1069a8b76c8fSLuigi Rizzo int i, lim, s, error = EINVAL; 1070df8bae1dSRodney W. Grimes u_char af; 1071df8bae1dSRodney W. Grimes struct walkarg w; 1072df8bae1dSRodney W. Grimes 107352041295SPoul-Henning Kamp name ++; 107452041295SPoul-Henning Kamp namelen--; 107552041295SPoul-Henning Kamp if (req->newptr) 1076df8bae1dSRodney W. Grimes return (EPERM); 1077df8bae1dSRodney W. Grimes if (namelen != 3) 1078f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1079df8bae1dSRodney W. Grimes af = name[0]; 1080b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1081b2aaf46eSJeffrey Hsu return (EINVAL); 10826b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1083df8bae1dSRodney W. Grimes w.w_op = name[1]; 1084df8bae1dSRodney W. Grimes w.w_arg = name[2]; 108552041295SPoul-Henning Kamp w.w_req = req; 1086df8bae1dSRodney W. Grimes 1087df8bae1dSRodney W. Grimes s = splnet(); 1088df8bae1dSRodney W. Grimes switch (w.w_op) { 1089df8bae1dSRodney W. Grimes 1090df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1091df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1092a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1093a8b76c8fSLuigi Rizzo i = 1; 1094a8b76c8fSLuigi Rizzo lim = AF_MAX; 1095a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1096a8b76c8fSLuigi Rizzo i = lim = af; 1097a8b76c8fSLuigi Rizzo for (error = 0; error == 0 && i <= lim; i++) 1098a8b76c8fSLuigi Rizzo if ((rnh = rt_tables[i]) != NULL) { 10997701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_LOCK(rnh); */ 1100956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 11017701e15bSJeffrey Hsu sysctl_dumpentry, &w);/* could sleep XXX */ 11027701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_UNLOCK(rnh); */ 1103a8b76c8fSLuigi Rizzo } else if (af != 0) 1104956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 1105df8bae1dSRodney W. Grimes break; 1106df8bae1dSRodney W. Grimes 1107df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1108df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 110905b2efe0SBruce M Simpson break; 111005b2efe0SBruce M Simpson 111105b2efe0SBruce M Simpson case NET_RT_IFMALIST: 111205b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 111305b2efe0SBruce M Simpson break; 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes splx(s); 1116df8bae1dSRodney W. Grimes if (w.w_tmem) 1117df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1118df8bae1dSRodney W. Grimes return (error); 1119df8bae1dSRodney W. Grimes } 1120df8bae1dSRodney W. Grimes 112152041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 112252041295SPoul-Henning Kamp 1123df8bae1dSRodney W. Grimes /* 1124df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1125df8bae1dSRodney W. Grimes */ 1126df8bae1dSRodney W. Grimes 1127df8bae1dSRodney W. Grimes extern struct domain routedomain; /* or at least forward */ 1128df8bae1dSRodney W. Grimes 112952041295SPoul-Henning Kamp static struct protosw routesw[] = { 1130df8bae1dSRodney W. Grimes { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 11316ddbf1e2SGary Palmer 0, route_output, raw_ctlinput, 0, 1132a29f300eSGarrett Wollman 0, 1133a29f300eSGarrett Wollman raw_init, 0, 0, 0, 1134a29f300eSGarrett Wollman &route_usrreqs 1135df8bae1dSRodney W. Grimes } 1136df8bae1dSRodney W. Grimes }; 1137df8bae1dSRodney W. Grimes 113852041295SPoul-Henning Kamp static struct domain routedomain = 1139cb64988fSLuoqi Chen { PF_ROUTE, "route", 0, 0, 0, 1140df8bae1dSRodney W. Grimes routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 114178a82810SGarrett Wollman 1142748e0b0aSGarrett Wollman DOMAIN_SET(route); 1143