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); 329becc44d7SSam Leffler if (t && Bcmp((caddr_t *) info.rti_info[RTAX_GENMASK] + 1, 330becc44d7SSam Leffler (caddr_t *)t->rn_key + 1, 331920eb79fSRuslan Ermilov *(u_char *)t->rn_key - 1) == 0) 332becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = 333becc44d7SSam Leffler (struct sockaddr *)(t->rn_key); 334df8bae1dSRodney W. Grimes else 335df8bae1dSRodney W. Grimes senderr(ENOBUFS); 336df8bae1dSRodney W. Grimes } 337162c0b2eSRuslan Ermilov 338162c0b2eSRuslan Ermilov /* 339162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 340162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 341162c0b2eSRuslan Ermilov */ 34244731cabSJohn Baldwin if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0) 343dadb6c3bSRuslan Ermilov senderr(error); 344162c0b2eSRuslan Ermilov 345df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 346becc44d7SSam Leffler struct rtentry *saved_nrt; 347df8bae1dSRodney W. Grimes 348df8bae1dSRodney W. Grimes case RTM_ADD: 3495dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 350df8bae1dSRodney W. Grimes senderr(EINVAL); 3515dfc91d7SLuigi Rizzo saved_nrt = NULL; 3528071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &saved_nrt); 353df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 354d1dd20beSSam Leffler RT_LOCK(saved_nrt); 355df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 356df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 3577138d65cSSam Leffler RT_REMREF(saved_nrt); 358becc44d7SSam Leffler saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 359d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 360df8bae1dSRodney W. Grimes } 361df8bae1dSRodney W. Grimes break; 362df8bae1dSRodney W. Grimes 363df8bae1dSRodney W. Grimes case RTM_DELETE: 3645dfc91d7SLuigi Rizzo saved_nrt = NULL; 3658071913dSRuslan Ermilov error = rtrequest1(RTM_DELETE, &info, &saved_nrt); 36678a82810SGarrett Wollman if (error == 0) { 367d1dd20beSSam Leffler RT_LOCK(saved_nrt); 36871eba915SRuslan Ermilov rt = saved_nrt; 36978a82810SGarrett Wollman goto report; 37078a82810SGarrett Wollman } 371df8bae1dSRodney W. Grimes break; 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes case RTM_GET: 374df8bae1dSRodney W. Grimes case RTM_CHANGE: 375df8bae1dSRodney W. Grimes case RTM_LOCK: 376becc44d7SSam Leffler rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family]; 3775dfc91d7SLuigi Rizzo if (rnh == NULL) 37878a82810SGarrett Wollman senderr(EAFNOSUPPORT); 379956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 380becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 381becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 382956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 383becc44d7SSam Leffler if (rt == NULL) /* XXX looks bogus */ 384df8bae1dSRodney W. Grimes senderr(ESRCH); 385d1dd20beSSam Leffler RT_LOCK(rt); 3867138d65cSSam Leffler RT_ADDREF(rt); 387956b0b65SJeffrey Hsu 388df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 389df8bae1dSRodney W. Grimes 390df8bae1dSRodney W. Grimes case RTM_GET: 39178a82810SGarrett Wollman report: 392d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 393becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 394becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 395becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 396becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 397df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 398df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 399df440948SPoul-Henning Kamp if (ifp) { 4009b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 4019b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 402becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 403becc44d7SSam Leffler rt->rt_ifa->ifa_addr; 40428070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 405becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 406becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 407df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 408df8bae1dSRodney W. Grimes } else { 4095dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 4105dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 411df8bae1dSRodney W. Grimes } 412df8bae1dSRodney W. Grimes } 413913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 414df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 415df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 416df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 4175dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 418d1dd20beSSam Leffler RT_UNLOCK(rt); 419df8bae1dSRodney W. Grimes senderr(ENOBUFS); 420becc44d7SSam Leffler } 421df8bae1dSRodney W. Grimes Bcopy(rtm, new_rtm, rtm->rtm_msglen); 422df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 423df8bae1dSRodney W. Grimes } 424913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 425df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 42697d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 427df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 428df8bae1dSRodney W. Grimes break; 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes case RTM_CHANGE: 431becc44d7SSam Leffler /* 432becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 433becc44d7SSam Leffler * flags may also be different; ifp may be specified 434becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 435becc44d7SSam Leffler */ 436becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 437becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 438becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 439becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 440becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 441becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 442becc44d7SSam Leffler if ((error = rt_getifa(&info)) != 0) { 443d1dd20beSSam Leffler RT_UNLOCK(rt); 4448071913dSRuslan Ermilov senderr(error); 44502a5d63eSBrian Somers } 446becc44d7SSam Leffler } 447becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] != NULL && 448becc44d7SSam Leffler (error = rt_setgate(rt, rt_key(rt), 449becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY])) != 0) { 450d1dd20beSSam Leffler RT_UNLOCK(rt); 4518071913dSRuslan Ermilov senderr(error); 452becc44d7SSam Leffler } 4538071913dSRuslan Ermilov if ((ifa = info.rti_ifa) != NULL) { 454becc44d7SSam Leffler struct ifaddr *oifa = rt->rt_ifa; 455df8bae1dSRodney W. Grimes if (oifa != ifa) { 45619fc74fbSJeffrey Hsu if (oifa) { 45719fc74fbSJeffrey Hsu if (oifa->ifa_rtrequest) 458becc44d7SSam Leffler oifa->ifa_rtrequest( 459becc44d7SSam Leffler RTM_DELETE, rt, 4608071913dSRuslan Ermilov &info); 46112e552d6SJeffrey Hsu IFAFREE(oifa); 46219fc74fbSJeffrey Hsu } 46319fc74fbSJeffrey Hsu IFAREF(ifa); 464df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 4658071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 466df8bae1dSRodney W. Grimes } 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 469df8bae1dSRodney W. Grimes &rt->rt_rmx); 470df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 4718071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 472becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) 473becc44d7SSam Leffler rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 47493b0017fSPhilippe Charnier /* FALLTHROUGH */ 475df8bae1dSRodney W. Grimes case RTM_LOCK: 47697d8d152SAndre Oppermann /* We don't support locks anymore */ 477df8bae1dSRodney W. Grimes break; 478df8bae1dSRodney W. Grimes } 479d1dd20beSSam Leffler RT_UNLOCK(rt); 480df8bae1dSRodney W. Grimes break; 481df8bae1dSRodney W. Grimes 482df8bae1dSRodney W. Grimes default: 483df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes 486df8bae1dSRodney W. Grimes flush: 487df8bae1dSRodney W. Grimes if (rtm) { 488df8bae1dSRodney W. Grimes if (error) 489df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 490df8bae1dSRodney W. Grimes else 491df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 492df8bae1dSRodney W. Grimes } 493becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 494becc44d7SSam Leffler RTFREE(rt); 495df8bae1dSRodney W. Grimes { 4965dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 497df8bae1dSRodney W. Grimes /* 498df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 499df8bae1dSRodney W. Grimes */ 500df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 501df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 502df8bae1dSRodney W. Grimes if (rtm) 503df8bae1dSRodney W. Grimes Free(rtm); 504df8bae1dSRodney W. Grimes m_freem(m); 505df8bae1dSRodney W. Grimes return (error); 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 508df8bae1dSRodney W. Grimes rp = sotorawcb(so); 5094cc20ab1SSeigo Tanimura } 510df8bae1dSRodney W. Grimes if (rtm) { 511df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 51203311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 51303311056SHajimu UMEMOTO m_freem(m); 51403311056SHajimu UMEMOTO m = NULL; 51503311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 51603311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 517df8bae1dSRodney W. Grimes Free(rtm); 518df8bae1dSRodney W. Grimes } 519becc44d7SSam Leffler if (m) { 520becc44d7SSam Leffler if (rp) { 521becc44d7SSam Leffler /* 522becc44d7SSam Leffler * XXX insure we don't get a copy by 523becc44d7SSam Leffler * invalidating our protocol 524becc44d7SSam Leffler */ 525becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 526becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 527becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 528becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 529becc44d7SSam Leffler } else 530becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 531becc44d7SSam Leffler } 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes return (error); 534becc44d7SSam Leffler #undef sa_equal 535df8bae1dSRodney W. Grimes } 536df8bae1dSRodney W. Grimes 53752041295SPoul-Henning Kamp static void 5385dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 5395dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 540df8bae1dSRodney W. Grimes { 541df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 54297d8d152SAndre Oppermann /* 54397d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 54497d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 54597d8d152SAndre Oppermann */ 546df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 547df8bae1dSRodney W. Grimes metric(RTV_EXPIRE, rmx_expire); 548df8bae1dSRodney W. Grimes #undef metric 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes 55197d8d152SAndre Oppermann static void 5525dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 55397d8d152SAndre Oppermann { 55497d8d152SAndre Oppermann #define metric(e) out->e = in->e; 55597d8d152SAndre Oppermann bzero(out, sizeof(*out)); 55697d8d152SAndre Oppermann metric(rmx_mtu); 55797d8d152SAndre Oppermann metric(rmx_expire); 55897d8d152SAndre Oppermann #undef metric 55997d8d152SAndre Oppermann } 56097d8d152SAndre Oppermann 5617f33a738SJulian Elischer /* 5627f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 5637f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 564076d0761SJulian Elischer * This data is derived straight from userland. 5657f33a738SJulian Elischer */ 566076d0761SJulian Elischer static int 567becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 568df8bae1dSRodney W. Grimes { 569e74642dfSLuigi Rizzo struct sockaddr *sa; 570e74642dfSLuigi Rizzo int i; 571df8bae1dSRodney W. Grimes 572becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 573df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 574df8bae1dSRodney W. Grimes continue; 575ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 5767f33a738SJulian Elischer /* 577076d0761SJulian Elischer * It won't fit. 5787f33a738SJulian Elischer */ 579becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 580076d0761SJulian Elischer return (EINVAL); 5817f33a738SJulian Elischer /* 5827f33a738SJulian Elischer * there are no more.. quit now 5837f33a738SJulian Elischer * If there are more bits, they are in error. 5847f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 5857f33a738SJulian Elischer * This causes kernel to core dump. 586076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 5877f33a738SJulian Elischer */ 588076d0761SJulian Elischer if (sa->sa_len == 0) { 589076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 590076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 591df8bae1dSRodney W. Grimes } 592076d0761SJulian Elischer /* accept it */ 593076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 594e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 595076d0761SJulian Elischer } 596076d0761SJulian Elischer return (0); 597df8bae1dSRodney W. Grimes } 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes static struct mbuf * 600becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 601df8bae1dSRodney W. Grimes { 6025dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 6035dfc91d7SLuigi Rizzo struct mbuf *m; 6045dfc91d7SLuigi Rizzo int i; 6055dfc91d7SLuigi Rizzo struct sockaddr *sa; 606df8bae1dSRodney W. Grimes int len, dlen; 607df8bae1dSRodney W. Grimes 608df8bae1dSRodney W. Grimes switch (type) { 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes case RTM_DELADDR: 611df8bae1dSRodney W. Grimes case RTM_NEWADDR: 612df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 613df8bae1dSRodney W. Grimes break; 614df8bae1dSRodney W. Grimes 615477180fbSGarrett Wollman case RTM_DELMADDR: 616477180fbSGarrett Wollman case RTM_NEWMADDR: 617477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 618477180fbSGarrett Wollman break; 619477180fbSGarrett Wollman 620df8bae1dSRodney W. Grimes case RTM_IFINFO: 621df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 622df8bae1dSRodney W. Grimes break; 623df8bae1dSRodney W. Grimes 6247b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 6257b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 6267b6edd04SRuslan Ermilov break; 6277b6edd04SRuslan Ermilov 628df8bae1dSRodney W. Grimes default: 629df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 630df8bae1dSRodney W. Grimes } 63133841545SHajimu UMEMOTO if (len > MCLBYTES) 632df8bae1dSRodney W. Grimes panic("rt_msg1"); 633a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 63433841545SHajimu UMEMOTO if (m && len > MHLEN) { 635a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 63633841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 63733841545SHajimu UMEMOTO m_free(m); 63833841545SHajimu UMEMOTO m = NULL; 63933841545SHajimu UMEMOTO } 64033841545SHajimu UMEMOTO } 6415dfc91d7SLuigi Rizzo if (m == NULL) 64233841545SHajimu UMEMOTO return (m); 643df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 6445dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 645df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 646df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 647df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 648df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 649df8bae1dSRodney W. Grimes continue; 650df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 651e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 652df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 653df8bae1dSRodney W. Grimes len += dlen; 654df8bae1dSRodney W. Grimes } 655df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 656df8bae1dSRodney W. Grimes m_freem(m); 657df8bae1dSRodney W. Grimes return (NULL); 658df8bae1dSRodney W. Grimes } 659df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 660df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 661df8bae1dSRodney W. Grimes rtm->rtm_type = type; 662df8bae1dSRodney W. Grimes return (m); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes 665df8bae1dSRodney W. Grimes static int 666becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 667df8bae1dSRodney W. Grimes { 6685dfc91d7SLuigi Rizzo int i; 669df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 670df8bae1dSRodney W. Grimes caddr_t cp0; 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 673df8bae1dSRodney W. Grimes again: 674df8bae1dSRodney W. Grimes switch (type) { 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes case RTM_DELADDR: 677df8bae1dSRodney W. Grimes case RTM_NEWADDR: 678df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 679df8bae1dSRodney W. Grimes break; 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes case RTM_IFINFO: 682df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 683df8bae1dSRodney W. Grimes break; 684df8bae1dSRodney W. Grimes 68505b2efe0SBruce M Simpson case RTM_NEWMADDR: 68605b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 68705b2efe0SBruce M Simpson break; 68805b2efe0SBruce M Simpson 689df8bae1dSRodney W. Grimes default: 690df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 691df8bae1dSRodney W. Grimes } 692df440948SPoul-Henning Kamp cp0 = cp; 693df440948SPoul-Henning Kamp if (cp0) 694df8bae1dSRodney W. Grimes cp += len; 695df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 6965dfc91d7SLuigi Rizzo struct sockaddr *sa; 697df8bae1dSRodney W. Grimes 6985dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 699df8bae1dSRodney W. Grimes continue; 700df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 701e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 702df8bae1dSRodney W. Grimes if (cp) { 703df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 704df8bae1dSRodney W. Grimes cp += dlen; 705df8bae1dSRodney W. Grimes } 706df8bae1dSRodney W. Grimes len += dlen; 707df8bae1dSRodney W. Grimes } 708694ff264SAndrew Gallatin len = ALIGN(len); 7095dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 7105dfc91d7SLuigi Rizzo struct walkarg *rw = w; 711df8bae1dSRodney W. Grimes 71252041295SPoul-Henning Kamp if (rw->w_req) { 713df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 714df8bae1dSRodney W. Grimes if (rw->w_tmem) 715df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 716df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 717df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 718df440948SPoul-Henning Kamp if (rw->w_tmem) 719df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 720df8bae1dSRodney W. Grimes } 721df8bae1dSRodney W. Grimes if (rw->w_tmem) { 722df8bae1dSRodney W. Grimes cp = rw->w_tmem; 723df8bae1dSRodney W. Grimes second_time = 1; 724df8bae1dSRodney W. Grimes goto again; 72552041295SPoul-Henning Kamp } 726df8bae1dSRodney W. Grimes } 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes if (cp) { 7295dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 730df8bae1dSRodney W. Grimes 731df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 732df8bae1dSRodney W. Grimes rtm->rtm_type = type; 733df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 734df8bae1dSRodney W. Grimes } 735df8bae1dSRodney W. Grimes return (len); 736df8bae1dSRodney W. Grimes } 737df8bae1dSRodney W. Grimes 738df8bae1dSRodney W. Grimes /* 739df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 740df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 741df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 742df8bae1dSRodney W. Grimes * destination. 743df8bae1dSRodney W. Grimes */ 744df8bae1dSRodney W. Grimes void 745becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 746df8bae1dSRodney W. Grimes { 747becc44d7SSam Leffler struct rt_msghdr *rtm; 748becc44d7SSam Leffler struct mbuf *m; 749df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 752df8bae1dSRodney W. Grimes return; 753df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 7545dfc91d7SLuigi Rizzo if (m == NULL) 755df8bae1dSRodney W. Grimes return; 756df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 757df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 758df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 759df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 760becc44d7SSam Leffler rt_dispatch(m, sa); 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes /* 764df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 765df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 766df8bae1dSRodney W. Grimes */ 767df8bae1dSRodney W. Grimes void 768becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 769df8bae1dSRodney W. Grimes { 770becc44d7SSam Leffler struct if_msghdr *ifm; 771df8bae1dSRodney W. Grimes struct mbuf *m; 772df8bae1dSRodney W. Grimes struct rt_addrinfo info; 773df8bae1dSRodney W. Grimes 774df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 775df8bae1dSRodney W. Grimes return; 776df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 777df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 7785dfc91d7SLuigi Rizzo if (m == NULL) 779df8bae1dSRodney W. Grimes return; 780df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 781df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 78262f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 783df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 784df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 785becc44d7SSam Leffler rt_dispatch(m, NULL); 786df8bae1dSRodney W. Grimes } 787df8bae1dSRodney W. Grimes 788df8bae1dSRodney W. Grimes /* 789df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 790df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 791df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 792df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 793df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 794df8bae1dSRodney W. Grimes * copies of it. 795df8bae1dSRodney W. Grimes */ 796df8bae1dSRodney W. Grimes void 797becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 798df8bae1dSRodney W. Grimes { 799df8bae1dSRodney W. Grimes struct rt_addrinfo info; 8005dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 801df8bae1dSRodney W. Grimes int pass; 8025dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 803df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 804df8bae1dSRodney W. Grimes 805df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 806df8bae1dSRodney W. Grimes return; 807df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 808df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 809df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 810df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 8115dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 812df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 813df8bae1dSRodney W. Grimes 814becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 8159b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 8169b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 817becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 818becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 819df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 820df8bae1dSRodney W. Grimes continue; 821df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 822df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 823df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 824df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 825df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 826df8bae1dSRodney W. Grimes } 827df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 828df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 8295dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 830df8bae1dSRodney W. Grimes 8315dfc91d7SLuigi Rizzo if (rt == NULL) 832df8bae1dSRodney W. Grimes continue; 833becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 834becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 835becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 836df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 837df8bae1dSRodney W. Grimes continue; 838df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 839df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 840df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 841df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 842df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 843df8bae1dSRodney W. Grimes } 844becc44d7SSam Leffler rt_dispatch(m, sa); 845df8bae1dSRodney W. Grimes } 846df8bae1dSRodney W. Grimes } 847df8bae1dSRodney W. Grimes 848477180fbSGarrett Wollman /* 849477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 850477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 851477180fbSGarrett Wollman * there is no route state to worry about. 852477180fbSGarrett Wollman */ 853477180fbSGarrett Wollman void 854becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 855477180fbSGarrett Wollman { 856477180fbSGarrett Wollman struct rt_addrinfo info; 8575dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 858477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 859477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 860477180fbSGarrett Wollman 861477180fbSGarrett Wollman if (route_cb.any_count == 0) 862477180fbSGarrett Wollman return; 863477180fbSGarrett Wollman 864477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 865becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 866becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 8679b98ee2cSLuigi Rizzo ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL; 868477180fbSGarrett Wollman /* 869477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 870477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 871477180fbSGarrett Wollman */ 872becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 873becc44d7SSam Leffler m = rt_msg1(cmd, &info); 874becc44d7SSam Leffler if (m == NULL) 875477180fbSGarrett Wollman return; 876477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 877477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 878477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 879becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 880477180fbSGarrett Wollman } 88152041295SPoul-Henning Kamp 882df8bae1dSRodney W. Grimes /* 8837b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 8847b6edd04SRuslan Ermilov * network interface arrival and departure. 8857b6edd04SRuslan Ermilov */ 8867b6edd04SRuslan Ermilov void 887becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 8887b6edd04SRuslan Ermilov { 8897b6edd04SRuslan Ermilov struct if_announcemsghdr *ifan; 8907b6edd04SRuslan Ermilov struct mbuf *m; 8917b6edd04SRuslan Ermilov struct rt_addrinfo info; 8927b6edd04SRuslan Ermilov 8937b6edd04SRuslan Ermilov if (route_cb.any_count == 0) 8947b6edd04SRuslan Ermilov return; 8957b6edd04SRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 8967b6edd04SRuslan Ermilov m = rt_msg1(RTM_IFANNOUNCE, &info); 8977b6edd04SRuslan Ermilov if (m == NULL) 8987b6edd04SRuslan Ermilov return; 8997b6edd04SRuslan Ermilov ifan = mtod(m, struct if_announcemsghdr *); 9007b6edd04SRuslan Ermilov ifan->ifan_index = ifp->if_index; 9019bf40edeSBrooks Davis strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 9027b6edd04SRuslan Ermilov ifan->ifan_what = what; 903becc44d7SSam Leffler rt_dispatch(m, NULL); 904becc44d7SSam Leffler } 905becc44d7SSam Leffler 906becc44d7SSam Leffler static void 9075dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 908becc44d7SSam Leffler { 909becc44d7SSam Leffler struct sockproto route_proto; 910becc44d7SSam Leffler 911becc44d7SSam Leffler route_proto.sp_family = PF_ROUTE; 912becc44d7SSam Leffler route_proto.sp_protocol = sa ? sa->sa_family : 0; 9137b6edd04SRuslan Ermilov raw_input(m, &route_proto, &route_src, &route_dst); 9147b6edd04SRuslan Ermilov } 9157b6edd04SRuslan Ermilov 9167b6edd04SRuslan Ermilov /* 917df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 918df8bae1dSRodney W. Grimes */ 91937c84183SPoul-Henning Kamp static int 920becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 921df8bae1dSRodney W. Grimes { 922becc44d7SSam Leffler struct walkarg *w = vw; 923becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 924df8bae1dSRodney W. Grimes int error = 0, size; 925df8bae1dSRodney W. Grimes struct rt_addrinfo info; 926df8bae1dSRodney W. Grimes 927df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 928df8bae1dSRodney W. Grimes return 0; 929df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 930becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 931becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 932becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 933becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 93428070a0eSRuslan Ermilov if (rt->rt_ifp) { 935becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 9369b98ee2cSLuigi Rizzo ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr; 937becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 93828070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 939becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 94028070a0eSRuslan Ermilov } 941913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 94252041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 943becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 944df8bae1dSRodney W. Grimes 945df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 94697d8d152SAndre Oppermann rtm->rtm_use = rt->rt_rmx.rmx_pksent; 94797d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 948df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 949df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 950df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 95152041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 95252041295SPoul-Henning Kamp return (error); 953df8bae1dSRodney W. Grimes } 954df8bae1dSRodney W. Grimes return (error); 955df8bae1dSRodney W. Grimes } 956df8bae1dSRodney W. Grimes 95737c84183SPoul-Henning Kamp static int 958becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 959df8bae1dSRodney W. Grimes { 960becc44d7SSam Leffler struct ifnet *ifp; 961becc44d7SSam Leffler struct ifaddr *ifa; 962df8bae1dSRodney W. Grimes struct rt_addrinfo info; 963df8bae1dSRodney W. Grimes int len, error = 0; 964df8bae1dSRodney W. Grimes 965df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 966b30a244cSJeffrey Hsu /* IFNET_RLOCK(); */ /* could sleep XXX */ 967fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 968df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 969df8bae1dSRodney W. Grimes continue; 9709b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 971becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 972913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 9735dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 97452041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 975becc44d7SSam Leffler struct if_msghdr *ifm; 976df8bae1dSRodney W. Grimes 977df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 978df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 97962f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 980df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 981df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 98252041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 983df440948SPoul-Henning Kamp if (error) 984a35b06c5SJonathan Lemon goto done; 985df8bae1dSRodney W. Grimes } 9865dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 987df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 988df8bae1dSRodney W. Grimes continue; 989a854ed98SJohn Baldwin if (jailed(curthread->td_ucred) && 990a854ed98SJohn Baldwin prison_if(curthread->td_ucred, ifa->ifa_addr)) 99175c13541SPoul-Henning Kamp continue; 992becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 993becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 994becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 995913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 99652041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 997becc44d7SSam Leffler struct ifa_msghdr *ifam; 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1000df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1001df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1002df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1003df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 100452041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1005df440948SPoul-Henning Kamp if (error) 1006a35b06c5SJonathan Lemon goto done; 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes } 1009becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 10105dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1011df8bae1dSRodney W. Grimes } 1012a35b06c5SJonathan Lemon done: 1013b30a244cSJeffrey Hsu /* IFNET_RUNLOCK(); */ /* XXX */ 1014a35b06c5SJonathan Lemon return (error); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 101705b2efe0SBruce M Simpson int 10189b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 101905b2efe0SBruce M Simpson { 10209b98ee2cSLuigi Rizzo struct ifnet *ifp; 102105b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 102205b2efe0SBruce M Simpson struct rt_addrinfo info; 102305b2efe0SBruce M Simpson int len, error = 0; 10249b98ee2cSLuigi Rizzo struct ifaddr *ifa; 102505b2efe0SBruce M Simpson 102605b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 102705b2efe0SBruce M Simpson /* IFNET_RLOCK(); */ /* could sleep XXX */ 102805b2efe0SBruce M Simpson TAILQ_FOREACH(ifp, &ifnet, if_link) { 102905b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 103005b2efe0SBruce M Simpson continue; 10319b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 1032913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 103305b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 103405b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 103505b2efe0SBruce M Simpson continue; 103605b2efe0SBruce M Simpson if (jailed(curproc->p_ucred) && 103705b2efe0SBruce M Simpson prison_if(curproc->p_ucred, ifma->ifma_addr)) 103805b2efe0SBruce M Simpson continue; 103905b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1040913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1041913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1042913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1043913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 104405b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 10459b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 104605b2efe0SBruce M Simpson 104705b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 104805b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 104905b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 105005b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 105105b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 105205b2efe0SBruce M Simpson if (error) 105305b2efe0SBruce M Simpson goto done; 105405b2efe0SBruce M Simpson } 105505b2efe0SBruce M Simpson } 105605b2efe0SBruce M Simpson } 105705b2efe0SBruce M Simpson done: 105805b2efe0SBruce M Simpson /* IFNET_RUNLOCK(); */ /* XXX */ 105905b2efe0SBruce M Simpson return (error); 106005b2efe0SBruce M Simpson } 106105b2efe0SBruce M Simpson 106252041295SPoul-Henning Kamp static int 106382d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1064df8bae1dSRodney W. Grimes { 106552041295SPoul-Henning Kamp int *name = (int *)arg1; 106652041295SPoul-Henning Kamp u_int namelen = arg2; 1067becc44d7SSam Leffler struct radix_node_head *rnh; 1068a8b76c8fSLuigi Rizzo int i, lim, s, error = EINVAL; 1069df8bae1dSRodney W. Grimes u_char af; 1070df8bae1dSRodney W. Grimes struct walkarg w; 1071df8bae1dSRodney W. Grimes 107252041295SPoul-Henning Kamp name ++; 107352041295SPoul-Henning Kamp namelen--; 107452041295SPoul-Henning Kamp if (req->newptr) 1075df8bae1dSRodney W. Grimes return (EPERM); 1076df8bae1dSRodney W. Grimes if (namelen != 3) 1077f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1078df8bae1dSRodney W. Grimes af = name[0]; 1079b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1080b2aaf46eSJeffrey Hsu return (EINVAL); 1081df8bae1dSRodney W. Grimes Bzero(&w, sizeof(w)); 1082df8bae1dSRodney W. Grimes w.w_op = name[1]; 1083df8bae1dSRodney W. Grimes w.w_arg = name[2]; 108452041295SPoul-Henning Kamp w.w_req = req; 1085df8bae1dSRodney W. Grimes 1086df8bae1dSRodney W. Grimes s = splnet(); 1087df8bae1dSRodney W. Grimes switch (w.w_op) { 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1090df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1091a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1092a8b76c8fSLuigi Rizzo i = 1; 1093a8b76c8fSLuigi Rizzo lim = AF_MAX; 1094a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1095a8b76c8fSLuigi Rizzo i = lim = af; 1096a8b76c8fSLuigi Rizzo for (error = 0; error == 0 && i <= lim; i++) 1097a8b76c8fSLuigi Rizzo if ((rnh = rt_tables[i]) != NULL) { 10987701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_LOCK(rnh); */ 1099956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 11007701e15bSJeffrey Hsu sysctl_dumpentry, &w);/* could sleep XXX */ 11017701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_UNLOCK(rnh); */ 1102a8b76c8fSLuigi Rizzo } else if (af != 0) 1103956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 1104df8bae1dSRodney W. Grimes break; 1105df8bae1dSRodney W. Grimes 1106df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1107df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 110805b2efe0SBruce M Simpson break; 110905b2efe0SBruce M Simpson 111005b2efe0SBruce M Simpson case NET_RT_IFMALIST: 111105b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 111205b2efe0SBruce M Simpson break; 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes splx(s); 1115df8bae1dSRodney W. Grimes if (w.w_tmem) 1116df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1117df8bae1dSRodney W. Grimes return (error); 1118df8bae1dSRodney W. Grimes } 1119df8bae1dSRodney W. Grimes 112052041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 112152041295SPoul-Henning Kamp 1122df8bae1dSRodney W. Grimes /* 1123df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1124df8bae1dSRodney W. Grimes */ 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes extern struct domain routedomain; /* or at least forward */ 1127df8bae1dSRodney W. Grimes 112852041295SPoul-Henning Kamp static struct protosw routesw[] = { 1129df8bae1dSRodney W. Grimes { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 11306ddbf1e2SGary Palmer 0, route_output, raw_ctlinput, 0, 1131a29f300eSGarrett Wollman 0, 1132a29f300eSGarrett Wollman raw_init, 0, 0, 0, 1133a29f300eSGarrett Wollman &route_usrreqs 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes }; 1136df8bae1dSRodney W. Grimes 113752041295SPoul-Henning Kamp static struct domain routedomain = 1138cb64988fSLuoqi Chen { PF_ROUTE, "route", 0, 0, 0, 1139df8bae1dSRodney W. Grimes routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 114078a82810SGarrett Wollman 1141748e0b0aSGarrett Wollman DOMAIN_SET(route); 1142