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> 48d989c7b3SRobert Watson #include <net/netisr.h> 49df8bae1dSRodney W. Grimes #include <net/raw_cb.h> 50960ed29cSSeigo Tanimura #include <net/route.h> 51df8bae1dSRodney W. Grimes 525a59cefcSBosko Milekic #include <netinet/in.h> 535a59cefcSBosko Milekic 54a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 55a1c995b6SPoul-Henning Kamp 56becc44d7SSam Leffler /* NB: these are not modified */ 5752041295SPoul-Henning Kamp static struct sockaddr route_dst = { 2, PF_ROUTE, }; 5852041295SPoul-Henning Kamp static struct sockaddr route_src = { 2, PF_ROUTE, }; 59076d0761SJulian Elischer static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 60becc44d7SSam Leffler 61becc44d7SSam Leffler static struct { 62becc44d7SSam Leffler int ip_count; /* attacked w/ AF_INET */ 63becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 64becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 65becc44d7SSam Leffler int any_count; /* total attached */ 66becc44d7SSam Leffler } route_cb; 67df8bae1dSRodney W. Grimes 68aea8b30fSSam Leffler struct mtx rtsock_mtx; 69aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 70aea8b30fSSam Leffler 71aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 72aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 73aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 74aea8b30fSSam Leffler 75d989c7b3SRobert Watson static struct ifqueue rtsintrq; 76d989c7b3SRobert Watson 77190a4c94SRobert Watson SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 78190a4c94SRobert Watson SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW, 79190a4c94SRobert Watson &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length"); 80190a4c94SRobert Watson 81df8bae1dSRodney W. Grimes struct walkarg { 8252041295SPoul-Henning Kamp int w_tmemsize; 8352041295SPoul-Henning Kamp int w_op, w_arg; 8452041295SPoul-Henning Kamp caddr_t w_tmem; 8552041295SPoul-Henning Kamp struct sysctl_req *w_req; 86df8bae1dSRodney W. Grimes }; 87df8bae1dSRodney W. Grimes 88d989c7b3SRobert Watson static void rts_input(struct mbuf *m); 895dfc91d7SLuigi Rizzo static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 905dfc91d7SLuigi Rizzo static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 915dfc91d7SLuigi Rizzo caddr_t cp, struct walkarg *w); 925dfc91d7SLuigi Rizzo static int rt_xaddrs(caddr_t cp, caddr_t cplim, 935dfc91d7SLuigi Rizzo struct rt_addrinfo *rtinfo); 94929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 95929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 9605b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 975dfc91d7SLuigi Rizzo static int route_output(struct mbuf *m, struct socket *so); 985dfc91d7SLuigi Rizzo static void rt_setmetrics(u_long which, const struct rt_metrics *in, 995dfc91d7SLuigi Rizzo struct rt_metrics_lite *out); 1005dfc91d7SLuigi Rizzo static void rt_getmetrics(const struct rt_metrics_lite *in, 1015dfc91d7SLuigi Rizzo struct rt_metrics *out); 1025dfc91d7SLuigi Rizzo static void rt_dispatch(struct mbuf *, const struct sockaddr *); 103df8bae1dSRodney W. Grimes 104d989c7b3SRobert Watson static void 105d989c7b3SRobert Watson rts_init(void) 106d989c7b3SRobert Watson { 107b062951aSRobert Watson int tmp; 108d989c7b3SRobert Watson 109190a4c94SRobert Watson rtsintrq.ifq_maxlen = 256; 110b062951aSRobert Watson if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 111b062951aSRobert Watson rtsintrq.ifq_maxlen = tmp; 112d989c7b3SRobert Watson mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF); 113d989c7b3SRobert Watson netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE); 114d989c7b3SRobert Watson } 115d989c7b3SRobert Watson SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0) 116d989c7b3SRobert Watson 117d989c7b3SRobert Watson static void 118d989c7b3SRobert Watson rts_input(struct mbuf *m) 119d989c7b3SRobert Watson { 120d989c7b3SRobert Watson struct sockproto route_proto; 121d989c7b3SRobert Watson unsigned short *family; 122d989c7b3SRobert Watson struct m_tag *tag; 123d989c7b3SRobert Watson 124d989c7b3SRobert Watson route_proto.sp_family = PF_ROUTE; 125d989c7b3SRobert Watson tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 126d989c7b3SRobert Watson if (tag != NULL) { 127d989c7b3SRobert Watson family = (unsigned short *)(tag + 1); 128d989c7b3SRobert Watson route_proto.sp_protocol = *family; 129d989c7b3SRobert Watson m_tag_delete(m, tag); 130d989c7b3SRobert Watson } else 131d989c7b3SRobert Watson route_proto.sp_protocol = 0; 132d989c7b3SRobert Watson 133d989c7b3SRobert Watson raw_input(m, &route_proto, &route_src, &route_dst); 134d989c7b3SRobert Watson } 135d989c7b3SRobert Watson 136a29f300eSGarrett Wollman /* 137a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 138a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 139a29f300eSGarrett Wollman */ 14052041295SPoul-Henning Kamp static int 141a29f300eSGarrett Wollman rts_abort(struct socket *so) 142df8bae1dSRodney W. Grimes { 143a29f300eSGarrett Wollman int s, error; 144df8bae1dSRodney W. Grimes s = splnet(); 145a29f300eSGarrett Wollman error = raw_usrreqs.pru_abort(so); 146df8bae1dSRodney W. Grimes splx(s); 147a29f300eSGarrett Wollman return error; 148df8bae1dSRodney W. Grimes } 149a29f300eSGarrett Wollman 150a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 151a29f300eSGarrett Wollman 152a29f300eSGarrett Wollman static int 153b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 154a29f300eSGarrett Wollman { 155a29f300eSGarrett Wollman struct rawcb *rp; 156a29f300eSGarrett Wollman int s, error; 157a29f300eSGarrett Wollman 1585dfc91d7SLuigi Rizzo if (sotorawcb(so) != NULL) 159a29f300eSGarrett Wollman return EISCONN; /* XXX panic? */ 1607cc0979fSDavid Malone /* XXX */ 161a163d034SWarner Losh MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 1625dfc91d7SLuigi Rizzo if (rp == NULL) 163a29f300eSGarrett Wollman return ENOBUFS; 164a29f300eSGarrett Wollman 165a29f300eSGarrett Wollman /* 166a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 167a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 168a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 169a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 170a29f300eSGarrett Wollman * eliminate the spl. 171a29f300eSGarrett Wollman */ 172a29f300eSGarrett Wollman s = splnet(); 173a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 174162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 175a29f300eSGarrett Wollman rp = sotorawcb(so); 176a29f300eSGarrett Wollman if (error) { 177a29f300eSGarrett Wollman splx(s); 1787ba271aeSJonathan Chen so->so_pcb = NULL; 179a29f300eSGarrett Wollman free(rp, M_PCB); 180a29f300eSGarrett Wollman return error; 181a29f300eSGarrett Wollman } 182aea8b30fSSam Leffler RTSOCK_LOCK(); 183a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 184a29f300eSGarrett Wollman case AF_INET: 185df8bae1dSRodney W. Grimes route_cb.ip_count++; 186a29f300eSGarrett Wollman break; 187899ce4f4SYoshinobu Inoue case AF_INET6: 188899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 189899ce4f4SYoshinobu Inoue break; 190a29f300eSGarrett Wollman case AF_IPX: 191cc6a66f2SJulian Elischer route_cb.ipx_count++; 192a29f300eSGarrett Wollman break; 193a29f300eSGarrett Wollman } 194df8bae1dSRodney W. Grimes rp->rcb_faddr = &route_src; 195df8bae1dSRodney W. Grimes route_cb.any_count++; 196aea8b30fSSam Leffler RTSOCK_UNLOCK(); 19703e49181SSeigo Tanimura soisconnected(so); 198df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 199df8bae1dSRodney W. Grimes splx(s); 200a29f300eSGarrett Wollman return 0; 201df8bae1dSRodney W. Grimes } 202df8bae1dSRodney W. Grimes 203a29f300eSGarrett Wollman static int 204b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 205a29f300eSGarrett Wollman { 206a29f300eSGarrett Wollman int s, error; 207a29f300eSGarrett Wollman s = splnet(); 208b40ce416SJulian Elischer error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */ 209a29f300eSGarrett Wollman splx(s); 210a29f300eSGarrett Wollman return error; 211a29f300eSGarrett Wollman } 212a29f300eSGarrett Wollman 213a29f300eSGarrett Wollman static int 214b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 215a29f300eSGarrett Wollman { 216a29f300eSGarrett Wollman int s, error; 217a29f300eSGarrett Wollman s = splnet(); 218b40ce416SJulian Elischer error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */ 219a29f300eSGarrett Wollman splx(s); 220a29f300eSGarrett Wollman return error; 221a29f300eSGarrett Wollman } 222a29f300eSGarrett Wollman 223a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 224a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 225a29f300eSGarrett Wollman 226a29f300eSGarrett Wollman static int 227a29f300eSGarrett Wollman rts_detach(struct socket *so) 228a29f300eSGarrett Wollman { 229a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 230a29f300eSGarrett Wollman int s, error; 231a29f300eSGarrett Wollman 232a29f300eSGarrett Wollman s = splnet(); 2335dfc91d7SLuigi Rizzo if (rp != NULL) { 234aea8b30fSSam Leffler RTSOCK_LOCK(); 235a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 236a29f300eSGarrett Wollman case AF_INET: 237a29f300eSGarrett Wollman route_cb.ip_count--; 238a29f300eSGarrett Wollman break; 239899ce4f4SYoshinobu Inoue case AF_INET6: 240899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 241899ce4f4SYoshinobu Inoue break; 242a29f300eSGarrett Wollman case AF_IPX: 243a29f300eSGarrett Wollman route_cb.ipx_count--; 244a29f300eSGarrett Wollman break; 245a29f300eSGarrett Wollman } 246a29f300eSGarrett Wollman route_cb.any_count--; 247aea8b30fSSam Leffler RTSOCK_UNLOCK(); 248a29f300eSGarrett Wollman } 249a29f300eSGarrett Wollman error = raw_usrreqs.pru_detach(so); 250a29f300eSGarrett Wollman splx(s); 251a29f300eSGarrett Wollman return error; 252a29f300eSGarrett Wollman } 253a29f300eSGarrett Wollman 254a29f300eSGarrett Wollman static int 255a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 256a29f300eSGarrett Wollman { 257a29f300eSGarrett Wollman int s, error; 258a29f300eSGarrett Wollman s = splnet(); 259a29f300eSGarrett Wollman error = raw_usrreqs.pru_disconnect(so); 260a29f300eSGarrett Wollman splx(s); 261a29f300eSGarrett Wollman return error; 262a29f300eSGarrett Wollman } 263a29f300eSGarrett Wollman 264a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 265a29f300eSGarrett Wollman 266a29f300eSGarrett Wollman static int 26757bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 268a29f300eSGarrett Wollman { 269a29f300eSGarrett Wollman int s, error; 270a29f300eSGarrett Wollman s = splnet(); 271a29f300eSGarrett Wollman error = raw_usrreqs.pru_peeraddr(so, nam); 272a29f300eSGarrett Wollman splx(s); 273a29f300eSGarrett Wollman return error; 274a29f300eSGarrett Wollman } 275a29f300eSGarrett Wollman 276a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 277a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 278a29f300eSGarrett Wollman 279a29f300eSGarrett Wollman static int 28057bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 281b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 282a29f300eSGarrett Wollman { 283a29f300eSGarrett Wollman int s, error; 284a29f300eSGarrett Wollman s = splnet(); 285b40ce416SJulian Elischer error = raw_usrreqs.pru_send(so, flags, m, nam, control, td); 286a29f300eSGarrett Wollman splx(s); 287a29f300eSGarrett Wollman return error; 288a29f300eSGarrett Wollman } 289a29f300eSGarrett Wollman 290a29f300eSGarrett Wollman /* pru_sense is null */ 291a29f300eSGarrett Wollman 292a29f300eSGarrett Wollman static int 293a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 294a29f300eSGarrett Wollman { 295a29f300eSGarrett Wollman int s, error; 296a29f300eSGarrett Wollman s = splnet(); 297a29f300eSGarrett Wollman error = raw_usrreqs.pru_shutdown(so); 298a29f300eSGarrett Wollman splx(s); 299a29f300eSGarrett Wollman return error; 300a29f300eSGarrett Wollman } 301a29f300eSGarrett Wollman 302a29f300eSGarrett Wollman static int 30357bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 304a29f300eSGarrett Wollman { 305a29f300eSGarrett Wollman int s, error; 306a29f300eSGarrett Wollman s = splnet(); 307a29f300eSGarrett Wollman error = raw_usrreqs.pru_sockaddr(so, nam); 308a29f300eSGarrett Wollman splx(s); 309a29f300eSGarrett Wollman return error; 310a29f300eSGarrett Wollman } 311a29f300eSGarrett Wollman 312a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 313a29f300eSGarrett Wollman rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect, 314a29f300eSGarrett Wollman pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect, 315a29f300eSGarrett Wollman pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp, 316a29f300eSGarrett Wollman rts_send, pru_sense_null, rts_shutdown, rts_sockaddr, 317a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 318a29f300eSGarrett Wollman }; 319a29f300eSGarrett Wollman 320df8bae1dSRodney W. Grimes /*ARGSUSED*/ 32152041295SPoul-Henning Kamp static int 3225dfc91d7SLuigi Rizzo route_output(struct mbuf *m, struct socket *so) 323df8bae1dSRodney W. Grimes { 324becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 3255dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = NULL; 3265dfc91d7SLuigi Rizzo struct rtentry *rt = NULL; 32778a82810SGarrett Wollman struct radix_node_head *rnh; 328df8bae1dSRodney W. Grimes struct rt_addrinfo info; 329df8bae1dSRodney W. Grimes int len, error = 0; 3305dfc91d7SLuigi Rizzo struct ifnet *ifp = NULL; 3315dfc91d7SLuigi Rizzo struct ifaddr *ifa = NULL; 3325a59cefcSBosko Milekic struct sockaddr_in jail; 333df8bae1dSRodney W. Grimes 334df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 3355dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 3365dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 337df8bae1dSRodney W. Grimes return (ENOBUFS); 338df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 339df8bae1dSRodney W. Grimes panic("route_output"); 340df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 341df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 342df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 3435dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 344df8bae1dSRodney W. Grimes senderr(EINVAL); 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 3475dfc91d7SLuigi Rizzo if (rtm == NULL) { 3485dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 349df8bae1dSRodney W. Grimes senderr(ENOBUFS); 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 352df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 3535dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 354df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 3578071913dSRuslan Ermilov bzero(&info, sizeof(info)); 358df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 359076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 3605dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 361076d0761SJulian Elischer senderr(EINVAL); 362076d0761SJulian Elischer } 3638071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 3645dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 365becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 3665dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 367becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 368df8bae1dSRodney W. Grimes senderr(EINVAL); 369becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) { 370df8bae1dSRodney W. Grimes struct radix_node *t; 371becc44d7SSam Leffler t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1); 372ae24a36eSRuslan Ermilov if (t != NULL && 3739554c70bSRuslan Ermilov bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1, 3749554c70bSRuslan Ermilov (char *)(void *)t->rn_key + 1, 3759554c70bSRuslan Ermilov ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0) 376becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = 3779554c70bSRuslan Ermilov (struct sockaddr *)t->rn_key; 378df8bae1dSRodney W. Grimes else 379df8bae1dSRodney W. Grimes senderr(ENOBUFS); 380df8bae1dSRodney W. Grimes } 381162c0b2eSRuslan Ermilov 382162c0b2eSRuslan Ermilov /* 383162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 384162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 385162c0b2eSRuslan Ermilov */ 38644731cabSJohn Baldwin if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0) 387dadb6c3bSRuslan Ermilov senderr(error); 388162c0b2eSRuslan Ermilov 389df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 390becc44d7SSam Leffler struct rtentry *saved_nrt; 391df8bae1dSRodney W. Grimes 392df8bae1dSRodney W. Grimes case RTM_ADD: 3935dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 394df8bae1dSRodney W. Grimes senderr(EINVAL); 3955dfc91d7SLuigi Rizzo saved_nrt = NULL; 3968071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &saved_nrt); 397df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 398d1dd20beSSam Leffler RT_LOCK(saved_nrt); 399df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 400df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 4017138d65cSSam Leffler RT_REMREF(saved_nrt); 402becc44d7SSam Leffler saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 403d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 404df8bae1dSRodney W. Grimes } 405df8bae1dSRodney W. Grimes break; 406df8bae1dSRodney W. Grimes 407df8bae1dSRodney W. Grimes case RTM_DELETE: 4085dfc91d7SLuigi Rizzo saved_nrt = NULL; 4098071913dSRuslan Ermilov error = rtrequest1(RTM_DELETE, &info, &saved_nrt); 41078a82810SGarrett Wollman if (error == 0) { 411d1dd20beSSam Leffler RT_LOCK(saved_nrt); 41271eba915SRuslan Ermilov rt = saved_nrt; 41378a82810SGarrett Wollman goto report; 41478a82810SGarrett Wollman } 415df8bae1dSRodney W. Grimes break; 416df8bae1dSRodney W. Grimes 417df8bae1dSRodney W. Grimes case RTM_GET: 418df8bae1dSRodney W. Grimes case RTM_CHANGE: 419df8bae1dSRodney W. Grimes case RTM_LOCK: 420becc44d7SSam Leffler rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family]; 4215dfc91d7SLuigi Rizzo if (rnh == NULL) 42278a82810SGarrett Wollman senderr(EAFNOSUPPORT); 423956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 424becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 425becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 426956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 427becc44d7SSam Leffler if (rt == NULL) /* XXX looks bogus */ 428df8bae1dSRodney W. Grimes senderr(ESRCH); 429d1dd20beSSam Leffler RT_LOCK(rt); 4307138d65cSSam Leffler RT_ADDREF(rt); 431956b0b65SJeffrey Hsu 432df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes case RTM_GET: 43578a82810SGarrett Wollman report: 436d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 437becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 438becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 439becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 440becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 441df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 442df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 443df440948SPoul-Henning Kamp if (ifp) { 4449b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 4459b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 4465a59cefcSBosko Milekic if (jailed(so->so_cred)) { 4479b3d77e7SBruce M Simpson bzero(&jail, sizeof(jail)); 4485a59cefcSBosko Milekic jail.sin_family = PF_INET; 4495a59cefcSBosko Milekic jail.sin_len = sizeof(jail); 4505a59cefcSBosko Milekic jail.sin_addr.s_addr = 4515a59cefcSBosko Milekic htonl(prison_getip(so->so_cred)); 4525a59cefcSBosko Milekic info.rti_info[RTAX_IFA] = 4535a59cefcSBosko Milekic (struct sockaddr *)&jail; 4545a59cefcSBosko Milekic } else 455becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 456becc44d7SSam Leffler rt->rt_ifa->ifa_addr; 45728070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 458becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 459becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 460df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 461df8bae1dSRodney W. Grimes } else { 4625dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 4635dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 464df8bae1dSRodney W. Grimes } 465df8bae1dSRodney W. Grimes } 466913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 467df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 468df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 469df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 4705dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 471d1dd20beSSam Leffler RT_UNLOCK(rt); 472df8bae1dSRodney W. Grimes senderr(ENOBUFS); 473becc44d7SSam Leffler } 4746b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 475df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 476df8bae1dSRodney W. Grimes } 477913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 478df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 47997d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 480df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 481df8bae1dSRodney W. Grimes break; 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes case RTM_CHANGE: 484becc44d7SSam Leffler /* 485becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 486becc44d7SSam Leffler * flags may also be different; ifp may be specified 487becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 488becc44d7SSam Leffler */ 489becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 490becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 491becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 492becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 493becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 494becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 495becc44d7SSam Leffler if ((error = rt_getifa(&info)) != 0) { 496d1dd20beSSam Leffler RT_UNLOCK(rt); 4978071913dSRuslan Ermilov senderr(error); 49802a5d63eSBrian Somers } 499becc44d7SSam Leffler } 500becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] != NULL && 501becc44d7SSam Leffler (error = rt_setgate(rt, rt_key(rt), 502becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY])) != 0) { 503d1dd20beSSam Leffler RT_UNLOCK(rt); 5048071913dSRuslan Ermilov senderr(error); 505becc44d7SSam Leffler } 5068071913dSRuslan Ermilov if ((ifa = info.rti_ifa) != NULL) { 507becc44d7SSam Leffler struct ifaddr *oifa = rt->rt_ifa; 508df8bae1dSRodney W. Grimes if (oifa != ifa) { 50919fc74fbSJeffrey Hsu if (oifa) { 51019fc74fbSJeffrey Hsu if (oifa->ifa_rtrequest) 511becc44d7SSam Leffler oifa->ifa_rtrequest( 512becc44d7SSam Leffler RTM_DELETE, rt, 5138071913dSRuslan Ermilov &info); 51412e552d6SJeffrey Hsu IFAFREE(oifa); 51519fc74fbSJeffrey Hsu } 51619fc74fbSJeffrey Hsu IFAREF(ifa); 517df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 5188071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 519df8bae1dSRodney W. Grimes } 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 522df8bae1dSRodney W. Grimes &rt->rt_rmx); 523df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 5248071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 525becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) 526becc44d7SSam Leffler rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 52793b0017fSPhilippe Charnier /* FALLTHROUGH */ 528df8bae1dSRodney W. Grimes case RTM_LOCK: 52997d8d152SAndre Oppermann /* We don't support locks anymore */ 530df8bae1dSRodney W. Grimes break; 531df8bae1dSRodney W. Grimes } 532d1dd20beSSam Leffler RT_UNLOCK(rt); 533df8bae1dSRodney W. Grimes break; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes default: 536df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes flush: 540df8bae1dSRodney W. Grimes if (rtm) { 541df8bae1dSRodney W. Grimes if (error) 542df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 543df8bae1dSRodney W. Grimes else 544df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 545df8bae1dSRodney W. Grimes } 546becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 547becc44d7SSam Leffler RTFREE(rt); 548df8bae1dSRodney W. Grimes { 5495dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 550df8bae1dSRodney W. Grimes /* 551df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 552df8bae1dSRodney W. Grimes */ 553df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 554df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 555df8bae1dSRodney W. Grimes if (rtm) 556df8bae1dSRodney W. Grimes Free(rtm); 557df8bae1dSRodney W. Grimes m_freem(m); 558df8bae1dSRodney W. Grimes return (error); 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 561df8bae1dSRodney W. Grimes rp = sotorawcb(so); 5624cc20ab1SSeigo Tanimura } 563df8bae1dSRodney W. Grimes if (rtm) { 564df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 56503311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 56603311056SHajimu UMEMOTO m_freem(m); 56703311056SHajimu UMEMOTO m = NULL; 56803311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 56903311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 570df8bae1dSRodney W. Grimes Free(rtm); 571df8bae1dSRodney W. Grimes } 572becc44d7SSam Leffler if (m) { 573becc44d7SSam Leffler if (rp) { 574becc44d7SSam Leffler /* 575becc44d7SSam Leffler * XXX insure we don't get a copy by 576becc44d7SSam Leffler * invalidating our protocol 577becc44d7SSam Leffler */ 578becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 579becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 580becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 581becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 582becc44d7SSam Leffler } else 583becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 584becc44d7SSam Leffler } 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes return (error); 587becc44d7SSam Leffler #undef sa_equal 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 59052041295SPoul-Henning Kamp static void 5915dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 5925dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 593df8bae1dSRodney W. Grimes { 594df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 59597d8d152SAndre Oppermann /* 59697d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 59797d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 59897d8d152SAndre Oppermann */ 599df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 600df8bae1dSRodney W. Grimes metric(RTV_EXPIRE, rmx_expire); 601df8bae1dSRodney W. Grimes #undef metric 602df8bae1dSRodney W. Grimes } 603df8bae1dSRodney W. Grimes 60497d8d152SAndre Oppermann static void 6055dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 60697d8d152SAndre Oppermann { 60797d8d152SAndre Oppermann #define metric(e) out->e = in->e; 60897d8d152SAndre Oppermann bzero(out, sizeof(*out)); 60997d8d152SAndre Oppermann metric(rmx_mtu); 61097d8d152SAndre Oppermann metric(rmx_expire); 61197d8d152SAndre Oppermann #undef metric 61297d8d152SAndre Oppermann } 61397d8d152SAndre Oppermann 6147f33a738SJulian Elischer /* 6157f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 6167f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 617076d0761SJulian Elischer * This data is derived straight from userland. 6187f33a738SJulian Elischer */ 619076d0761SJulian Elischer static int 620becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 621df8bae1dSRodney W. Grimes { 622e74642dfSLuigi Rizzo struct sockaddr *sa; 623e74642dfSLuigi Rizzo int i; 624df8bae1dSRodney W. Grimes 625becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 626df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 627df8bae1dSRodney W. Grimes continue; 628ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 6297f33a738SJulian Elischer /* 630076d0761SJulian Elischer * It won't fit. 6317f33a738SJulian Elischer */ 632becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 633076d0761SJulian Elischer return (EINVAL); 6347f33a738SJulian Elischer /* 6357f33a738SJulian Elischer * there are no more.. quit now 6367f33a738SJulian Elischer * If there are more bits, they are in error. 6377f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 6387f33a738SJulian Elischer * This causes kernel to core dump. 639076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 6407f33a738SJulian Elischer */ 641076d0761SJulian Elischer if (sa->sa_len == 0) { 642076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 643076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 644df8bae1dSRodney W. Grimes } 645076d0761SJulian Elischer /* accept it */ 646076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 647e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 648076d0761SJulian Elischer } 649076d0761SJulian Elischer return (0); 650df8bae1dSRodney W. Grimes } 651df8bae1dSRodney W. Grimes 652df8bae1dSRodney W. Grimes static struct mbuf * 653becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 654df8bae1dSRodney W. Grimes { 6555dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 6565dfc91d7SLuigi Rizzo struct mbuf *m; 6575dfc91d7SLuigi Rizzo int i; 6585dfc91d7SLuigi Rizzo struct sockaddr *sa; 659df8bae1dSRodney W. Grimes int len, dlen; 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes switch (type) { 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes case RTM_DELADDR: 664df8bae1dSRodney W. Grimes case RTM_NEWADDR: 665df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 666df8bae1dSRodney W. Grimes break; 667df8bae1dSRodney W. Grimes 668477180fbSGarrett Wollman case RTM_DELMADDR: 669477180fbSGarrett Wollman case RTM_NEWMADDR: 670477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 671477180fbSGarrett Wollman break; 672477180fbSGarrett Wollman 673df8bae1dSRodney W. Grimes case RTM_IFINFO: 674df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 675df8bae1dSRodney W. Grimes break; 676df8bae1dSRodney W. Grimes 6777b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 6787b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 6797b6edd04SRuslan Ermilov break; 6807b6edd04SRuslan Ermilov 681df8bae1dSRodney W. Grimes default: 682df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 683df8bae1dSRodney W. Grimes } 68433841545SHajimu UMEMOTO if (len > MCLBYTES) 685df8bae1dSRodney W. Grimes panic("rt_msg1"); 686a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 68733841545SHajimu UMEMOTO if (m && len > MHLEN) { 688a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 68933841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 69033841545SHajimu UMEMOTO m_free(m); 69133841545SHajimu UMEMOTO m = NULL; 69233841545SHajimu UMEMOTO } 69333841545SHajimu UMEMOTO } 6945dfc91d7SLuigi Rizzo if (m == NULL) 69533841545SHajimu UMEMOTO return (m); 696df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 6975dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 698df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 699df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 700df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 701df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 702df8bae1dSRodney W. Grimes continue; 703df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 704e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 705df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 706df8bae1dSRodney W. Grimes len += dlen; 707df8bae1dSRodney W. Grimes } 708df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 709df8bae1dSRodney W. Grimes m_freem(m); 710df8bae1dSRodney W. Grimes return (NULL); 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 713df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 714df8bae1dSRodney W. Grimes rtm->rtm_type = type; 715df8bae1dSRodney W. Grimes return (m); 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes 718df8bae1dSRodney W. Grimes static int 719becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 720df8bae1dSRodney W. Grimes { 7215dfc91d7SLuigi Rizzo int i; 722df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 723df8bae1dSRodney W. Grimes caddr_t cp0; 724df8bae1dSRodney W. Grimes 725df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 726df8bae1dSRodney W. Grimes again: 727df8bae1dSRodney W. Grimes switch (type) { 728df8bae1dSRodney W. Grimes 729df8bae1dSRodney W. Grimes case RTM_DELADDR: 730df8bae1dSRodney W. Grimes case RTM_NEWADDR: 731df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 732df8bae1dSRodney W. Grimes break; 733df8bae1dSRodney W. Grimes 734df8bae1dSRodney W. Grimes case RTM_IFINFO: 735df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 736df8bae1dSRodney W. Grimes break; 737df8bae1dSRodney W. Grimes 73805b2efe0SBruce M Simpson case RTM_NEWMADDR: 73905b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 74005b2efe0SBruce M Simpson break; 74105b2efe0SBruce M Simpson 742df8bae1dSRodney W. Grimes default: 743df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 744df8bae1dSRodney W. Grimes } 745df440948SPoul-Henning Kamp cp0 = cp; 746df440948SPoul-Henning Kamp if (cp0) 747df8bae1dSRodney W. Grimes cp += len; 748df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 7495dfc91d7SLuigi Rizzo struct sockaddr *sa; 750df8bae1dSRodney W. Grimes 7515dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 752df8bae1dSRodney W. Grimes continue; 753df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 754e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 755df8bae1dSRodney W. Grimes if (cp) { 756df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 757df8bae1dSRodney W. Grimes cp += dlen; 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes len += dlen; 760df8bae1dSRodney W. Grimes } 761694ff264SAndrew Gallatin len = ALIGN(len); 7625dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 7635dfc91d7SLuigi Rizzo struct walkarg *rw = w; 764df8bae1dSRodney W. Grimes 76552041295SPoul-Henning Kamp if (rw->w_req) { 766df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 767df8bae1dSRodney W. Grimes if (rw->w_tmem) 768df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 769df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 770df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 771df440948SPoul-Henning Kamp if (rw->w_tmem) 772df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes if (rw->w_tmem) { 775df8bae1dSRodney W. Grimes cp = rw->w_tmem; 776df8bae1dSRodney W. Grimes second_time = 1; 777df8bae1dSRodney W. Grimes goto again; 77852041295SPoul-Henning Kamp } 779df8bae1dSRodney W. Grimes } 780df8bae1dSRodney W. Grimes } 781df8bae1dSRodney W. Grimes if (cp) { 7825dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 785df8bae1dSRodney W. Grimes rtm->rtm_type = type; 786df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes return (len); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes 791df8bae1dSRodney W. Grimes /* 792df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 793df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 794df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 795df8bae1dSRodney W. Grimes * destination. 796df8bae1dSRodney W. Grimes */ 797df8bae1dSRodney W. Grimes void 798becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 799df8bae1dSRodney W. Grimes { 800becc44d7SSam Leffler struct rt_msghdr *rtm; 801becc44d7SSam Leffler struct mbuf *m; 802df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 803df8bae1dSRodney W. Grimes 804df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 805df8bae1dSRodney W. Grimes return; 806df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 8075dfc91d7SLuigi Rizzo if (m == NULL) 808df8bae1dSRodney W. Grimes return; 809df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 810df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 811df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 812df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 813becc44d7SSam Leffler rt_dispatch(m, sa); 814df8bae1dSRodney W. Grimes } 815df8bae1dSRodney W. Grimes 816df8bae1dSRodney W. Grimes /* 817df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 818df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 819df8bae1dSRodney W. Grimes */ 820df8bae1dSRodney W. Grimes void 821becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 822df8bae1dSRodney W. Grimes { 823becc44d7SSam Leffler struct if_msghdr *ifm; 824df8bae1dSRodney W. Grimes struct mbuf *m; 825df8bae1dSRodney W. Grimes struct rt_addrinfo info; 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 828df8bae1dSRodney W. Grimes return; 829df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 830df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 8315dfc91d7SLuigi Rizzo if (m == NULL) 832df8bae1dSRodney W. Grimes return; 833df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 834df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 83562f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 836df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 837df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 838becc44d7SSam Leffler rt_dispatch(m, NULL); 839df8bae1dSRodney W. Grimes } 840df8bae1dSRodney W. Grimes 841df8bae1dSRodney W. Grimes /* 842df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 843df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 844df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 845df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 846df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 847df8bae1dSRodney W. Grimes * copies of it. 848df8bae1dSRodney W. Grimes */ 849df8bae1dSRodney W. Grimes void 850becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 851df8bae1dSRodney W. Grimes { 852df8bae1dSRodney W. Grimes struct rt_addrinfo info; 8535dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 854df8bae1dSRodney W. Grimes int pass; 8555dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 856df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 857df8bae1dSRodney W. Grimes 858df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 859df8bae1dSRodney W. Grimes return; 860df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 861df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 862df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 863df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 8645dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 865df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 866df8bae1dSRodney W. Grimes 867becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 8689b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 8699b98ee2cSLuigi Rizzo ifaddr_byindex(ifp->if_index)->ifa_addr; 870becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 871becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 872df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 873df8bae1dSRodney W. Grimes continue; 874df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 875df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 876df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 877df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 878df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 879df8bae1dSRodney W. Grimes } 880df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 881df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 8825dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 883df8bae1dSRodney W. Grimes 8845dfc91d7SLuigi Rizzo if (rt == NULL) 885df8bae1dSRodney W. Grimes continue; 886becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 887becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 888becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 889df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 890df8bae1dSRodney W. Grimes continue; 891df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 892df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 893df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 894df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 895df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 896df8bae1dSRodney W. Grimes } 897becc44d7SSam Leffler rt_dispatch(m, sa); 898df8bae1dSRodney W. Grimes } 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes 901477180fbSGarrett Wollman /* 902477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 903477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 904477180fbSGarrett Wollman * there is no route state to worry about. 905477180fbSGarrett Wollman */ 906477180fbSGarrett Wollman void 907becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 908477180fbSGarrett Wollman { 909477180fbSGarrett Wollman struct rt_addrinfo info; 9105dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 911477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 912477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 913477180fbSGarrett Wollman 914477180fbSGarrett Wollman if (route_cb.any_count == 0) 915477180fbSGarrett Wollman return; 916477180fbSGarrett Wollman 917477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 918becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 919becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 9209b98ee2cSLuigi Rizzo ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL; 921477180fbSGarrett Wollman /* 922477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 923477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 924477180fbSGarrett Wollman */ 925becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 926becc44d7SSam Leffler m = rt_msg1(cmd, &info); 927becc44d7SSam Leffler if (m == NULL) 928477180fbSGarrett Wollman return; 929477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 930477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 931477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 932becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 933477180fbSGarrett Wollman } 93452041295SPoul-Henning Kamp 935df8bae1dSRodney W. Grimes /* 9367b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 9377b6edd04SRuslan Ermilov * network interface arrival and departure. 9387b6edd04SRuslan Ermilov */ 9397b6edd04SRuslan Ermilov void 940becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 9417b6edd04SRuslan Ermilov { 9427b6edd04SRuslan Ermilov struct if_announcemsghdr *ifan; 9437b6edd04SRuslan Ermilov struct mbuf *m; 9447b6edd04SRuslan Ermilov struct rt_addrinfo info; 9457b6edd04SRuslan Ermilov 9467b6edd04SRuslan Ermilov if (route_cb.any_count == 0) 9477b6edd04SRuslan Ermilov return; 9487b6edd04SRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 9497b6edd04SRuslan Ermilov m = rt_msg1(RTM_IFANNOUNCE, &info); 9507b6edd04SRuslan Ermilov if (m == NULL) 9517b6edd04SRuslan Ermilov return; 9527b6edd04SRuslan Ermilov ifan = mtod(m, struct if_announcemsghdr *); 9537b6edd04SRuslan Ermilov ifan->ifan_index = ifp->if_index; 9549bf40edeSBrooks Davis strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name)); 9557b6edd04SRuslan Ermilov ifan->ifan_what = what; 956becc44d7SSam Leffler rt_dispatch(m, NULL); 957becc44d7SSam Leffler } 958becc44d7SSam Leffler 959becc44d7SSam Leffler static void 9605dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 961becc44d7SSam Leffler { 962d989c7b3SRobert Watson unsigned short *family; 963d989c7b3SRobert Watson struct m_tag *tag; 964becc44d7SSam Leffler 965d989c7b3SRobert Watson /* 966d989c7b3SRobert Watson * Preserve the family from the sockaddr, if any, in an m_tag for 967d989c7b3SRobert Watson * use when injecting the mbuf into the routing socket buffer from 968d989c7b3SRobert Watson * the netisr. 969d989c7b3SRobert Watson */ 970d989c7b3SRobert Watson if (sa != NULL) { 971d989c7b3SRobert Watson tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 972d989c7b3SRobert Watson M_NOWAIT); 973d989c7b3SRobert Watson if (tag == NULL) { 974d989c7b3SRobert Watson m_freem(m); 975d989c7b3SRobert Watson return; 976d989c7b3SRobert Watson } 977d989c7b3SRobert Watson family = (unsigned short *)(tag + 1); 978d989c7b3SRobert Watson *family = sa ? sa->sa_family : 0; 979d989c7b3SRobert Watson m_tag_prepend(m, tag); 980d989c7b3SRobert Watson } 981d989c7b3SRobert Watson netisr_queue(NETISR_ROUTE, m); 9827b6edd04SRuslan Ermilov } 9837b6edd04SRuslan Ermilov 9847b6edd04SRuslan Ermilov /* 985df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 986df8bae1dSRodney W. Grimes */ 98737c84183SPoul-Henning Kamp static int 988becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 989df8bae1dSRodney W. Grimes { 990becc44d7SSam Leffler struct walkarg *w = vw; 991becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 992df8bae1dSRodney W. Grimes int error = 0, size; 993df8bae1dSRodney W. Grimes struct rt_addrinfo info; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 996df8bae1dSRodney W. Grimes return 0; 997df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 998becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 999becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1000becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1001becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 100228070a0eSRuslan Ermilov if (rt->rt_ifp) { 1003becc44d7SSam Leffler info.rti_info[RTAX_IFP] = 10049b98ee2cSLuigi Rizzo ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr; 1005becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 100628070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1007becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 100828070a0eSRuslan Ermilov } 1009913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 101052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1011becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1012df8bae1dSRodney W. Grimes 1013df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 101497d8d152SAndre Oppermann rtm->rtm_use = rt->rt_rmx.rmx_pksent; 101597d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1016df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 1017df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1018df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 101952041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 102052041295SPoul-Henning Kamp return (error); 1021df8bae1dSRodney W. Grimes } 1022df8bae1dSRodney W. Grimes return (error); 1023df8bae1dSRodney W. Grimes } 1024df8bae1dSRodney W. Grimes 102537c84183SPoul-Henning Kamp static int 1026becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 1027df8bae1dSRodney W. Grimes { 1028becc44d7SSam Leffler struct ifnet *ifp; 1029becc44d7SSam Leffler struct ifaddr *ifa; 1030df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1031df8bae1dSRodney W. Grimes int len, error = 0; 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1034b30a244cSJeffrey Hsu /* IFNET_RLOCK(); */ /* could sleep XXX */ 1035fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 1036df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 1037df8bae1dSRodney W. Grimes continue; 10389b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 1039becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1040913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 10415dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 104252041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1043becc44d7SSam Leffler struct if_msghdr *ifm; 1044df8bae1dSRodney W. Grimes 1045df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 1046df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 104762f76486SMaxim Sobolev ifm->ifm_flags = ifp->if_flags; 1048df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1049df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 105052041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 1051df440948SPoul-Henning Kamp if (error) 1052a35b06c5SJonathan Lemon goto done; 1053df8bae1dSRodney W. Grimes } 10545dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1055df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 1056df8bae1dSRodney W. Grimes continue; 1057a854ed98SJohn Baldwin if (jailed(curthread->td_ucred) && 1058a854ed98SJohn Baldwin prison_if(curthread->td_ucred, ifa->ifa_addr)) 105975c13541SPoul-Henning Kamp continue; 1060becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1061becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1062becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1063913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 106452041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1065becc44d7SSam Leffler struct ifa_msghdr *ifam; 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1068df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1069df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1070df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1071df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 107252041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1073df440948SPoul-Henning Kamp if (error) 1074a35b06c5SJonathan Lemon goto done; 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes } 1077becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 10785dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1079df8bae1dSRodney W. Grimes } 1080a35b06c5SJonathan Lemon done: 1081b30a244cSJeffrey Hsu /* IFNET_RUNLOCK(); */ /* XXX */ 1082a35b06c5SJonathan Lemon return (error); 1083df8bae1dSRodney W. Grimes } 1084df8bae1dSRodney W. Grimes 108505b2efe0SBruce M Simpson int 10869b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 108705b2efe0SBruce M Simpson { 10889b98ee2cSLuigi Rizzo struct ifnet *ifp; 108905b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 109005b2efe0SBruce M Simpson struct rt_addrinfo info; 109105b2efe0SBruce M Simpson int len, error = 0; 10929b98ee2cSLuigi Rizzo struct ifaddr *ifa; 109305b2efe0SBruce M Simpson 109405b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 109505b2efe0SBruce M Simpson /* IFNET_RLOCK(); */ /* could sleep XXX */ 109605b2efe0SBruce M Simpson TAILQ_FOREACH(ifp, &ifnet, if_link) { 109705b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 109805b2efe0SBruce M Simpson continue; 10999b98ee2cSLuigi Rizzo ifa = ifaddr_byindex(ifp->if_index); 1100913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 110105b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 110205b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 110305b2efe0SBruce M Simpson continue; 110405b2efe0SBruce M Simpson if (jailed(curproc->p_ucred) && 110505b2efe0SBruce M Simpson prison_if(curproc->p_ucred, ifma->ifma_addr)) 110605b2efe0SBruce M Simpson continue; 110705b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1108913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1109913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1110913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1111913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 111205b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 11139b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 111405b2efe0SBruce M Simpson 111505b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 111605b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 111705b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 111805b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 111905b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 112005b2efe0SBruce M Simpson if (error) 112105b2efe0SBruce M Simpson goto done; 112205b2efe0SBruce M Simpson } 112305b2efe0SBruce M Simpson } 112405b2efe0SBruce M Simpson } 112505b2efe0SBruce M Simpson done: 112605b2efe0SBruce M Simpson /* IFNET_RUNLOCK(); */ /* XXX */ 112705b2efe0SBruce M Simpson return (error); 112805b2efe0SBruce M Simpson } 112905b2efe0SBruce M Simpson 113052041295SPoul-Henning Kamp static int 113182d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1132df8bae1dSRodney W. Grimes { 113352041295SPoul-Henning Kamp int *name = (int *)arg1; 113452041295SPoul-Henning Kamp u_int namelen = arg2; 1135becc44d7SSam Leffler struct radix_node_head *rnh; 1136a8b76c8fSLuigi Rizzo int i, lim, s, error = EINVAL; 1137df8bae1dSRodney W. Grimes u_char af; 1138df8bae1dSRodney W. Grimes struct walkarg w; 1139df8bae1dSRodney W. Grimes 114052041295SPoul-Henning Kamp name ++; 114152041295SPoul-Henning Kamp namelen--; 114252041295SPoul-Henning Kamp if (req->newptr) 1143df8bae1dSRodney W. Grimes return (EPERM); 1144df8bae1dSRodney W. Grimes if (namelen != 3) 1145f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1146df8bae1dSRodney W. Grimes af = name[0]; 1147b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1148b2aaf46eSJeffrey Hsu return (EINVAL); 11496b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1150df8bae1dSRodney W. Grimes w.w_op = name[1]; 1151df8bae1dSRodney W. Grimes w.w_arg = name[2]; 115252041295SPoul-Henning Kamp w.w_req = req; 1153df8bae1dSRodney W. Grimes 1154df8bae1dSRodney W. Grimes s = splnet(); 1155df8bae1dSRodney W. Grimes switch (w.w_op) { 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1158df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1159a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1160a8b76c8fSLuigi Rizzo i = 1; 1161a8b76c8fSLuigi Rizzo lim = AF_MAX; 1162a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1163a8b76c8fSLuigi Rizzo i = lim = af; 1164a8b76c8fSLuigi Rizzo for (error = 0; error == 0 && i <= lim; i++) 1165a8b76c8fSLuigi Rizzo if ((rnh = rt_tables[i]) != NULL) { 11667701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_LOCK(rnh); */ 1167956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 11687701e15bSJeffrey Hsu sysctl_dumpentry, &w);/* could sleep XXX */ 11697701e15bSJeffrey Hsu /* RADIX_NODE_HEAD_UNLOCK(rnh); */ 1170a8b76c8fSLuigi Rizzo } else if (af != 0) 1171956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 1172df8bae1dSRodney W. Grimes break; 1173df8bae1dSRodney W. Grimes 1174df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1175df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 117605b2efe0SBruce M Simpson break; 117705b2efe0SBruce M Simpson 117805b2efe0SBruce M Simpson case NET_RT_IFMALIST: 117905b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 118005b2efe0SBruce M Simpson break; 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes splx(s); 1183df8bae1dSRodney W. Grimes if (w.w_tmem) 1184df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1185df8bae1dSRodney W. Grimes return (error); 1186df8bae1dSRodney W. Grimes } 1187df8bae1dSRodney W. Grimes 118852041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 118952041295SPoul-Henning Kamp 1190df8bae1dSRodney W. Grimes /* 1191df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1192df8bae1dSRodney W. Grimes */ 1193df8bae1dSRodney W. Grimes 1194df8bae1dSRodney W. Grimes extern struct domain routedomain; /* or at least forward */ 1195df8bae1dSRodney W. Grimes 119652041295SPoul-Henning Kamp static struct protosw routesw[] = { 1197df8bae1dSRodney W. Grimes { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, 11986ddbf1e2SGary Palmer 0, route_output, raw_ctlinput, 0, 1199a29f300eSGarrett Wollman 0, 1200a29f300eSGarrett Wollman raw_init, 0, 0, 0, 1201a29f300eSGarrett Wollman &route_usrreqs 1202df8bae1dSRodney W. Grimes } 1203df8bae1dSRodney W. Grimes }; 1204df8bae1dSRodney W. Grimes 120552041295SPoul-Henning Kamp static struct domain routedomain = 1206cb64988fSLuoqi Chen { PF_ROUTE, "route", 0, 0, 0, 1207df8bae1dSRodney W. Grimes routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; 120878a82810SGarrett Wollman 1209748e0b0aSGarrett Wollman DOMAIN_SET(route); 1210