1c398230bSWarner Losh /*- 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 */ 32f8829a4aSRandall Stewart #include "opt_sctp.h" 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> 39acd3428bSRobert Watson #include <sys/priv.h> 40960ed29cSSeigo Tanimura #include <sys/proc.h> 41960ed29cSSeigo Tanimura #include <sys/protosw.h> 42960ed29cSSeigo Tanimura #include <sys/signalvar.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 44df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 45960ed29cSSeigo Tanimura #include <sys/sysctl.h> 46960ed29cSSeigo Tanimura #include <sys/systm.h> 47df8bae1dSRodney W. Grimes 48df8bae1dSRodney W. Grimes #include <net/if.h> 49d989c7b3SRobert Watson #include <net/netisr.h> 50df8bae1dSRodney W. Grimes #include <net/raw_cb.h> 51960ed29cSSeigo Tanimura #include <net/route.h> 52df8bae1dSRodney W. Grimes 535a59cefcSBosko Milekic #include <netinet/in.h> 545a59cefcSBosko Milekic 55f8829a4aSRandall Stewart #ifdef SCTP 56f8829a4aSRandall Stewart extern void sctp_addr_change(struct ifaddr *ifa, int cmd); 57f8829a4aSRandall Stewart #endif /* SCTP */ 58f8829a4aSRandall Stewart 59a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 60a1c995b6SPoul-Henning Kamp 61becc44d7SSam Leffler /* NB: these are not modified */ 6252041295SPoul-Henning Kamp static struct sockaddr route_dst = { 2, PF_ROUTE, }; 6352041295SPoul-Henning Kamp static struct sockaddr route_src = { 2, PF_ROUTE, }; 64076d0761SJulian Elischer static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 65becc44d7SSam Leffler 66becc44d7SSam Leffler static struct { 6718aee723SPeter Pentchev int ip_count; /* attached w/ AF_INET */ 68becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 69becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 70becc44d7SSam Leffler int any_count; /* total attached */ 71becc44d7SSam Leffler } route_cb; 72df8bae1dSRodney W. Grimes 73aea8b30fSSam Leffler struct mtx rtsock_mtx; 74aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 75aea8b30fSSam Leffler 76aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 77aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 78aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 79aea8b30fSSam Leffler 80d989c7b3SRobert Watson static struct ifqueue rtsintrq; 81d989c7b3SRobert Watson 82190a4c94SRobert Watson SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 83190a4c94SRobert Watson SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW, 84190a4c94SRobert Watson &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length"); 85190a4c94SRobert Watson 86df8bae1dSRodney W. Grimes struct walkarg { 8752041295SPoul-Henning Kamp int w_tmemsize; 8852041295SPoul-Henning Kamp int w_op, w_arg; 8952041295SPoul-Henning Kamp caddr_t w_tmem; 9052041295SPoul-Henning Kamp struct sysctl_req *w_req; 91df8bae1dSRodney W. Grimes }; 92df8bae1dSRodney W. Grimes 93d989c7b3SRobert Watson static void rts_input(struct mbuf *m); 945dfc91d7SLuigi Rizzo static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 955dfc91d7SLuigi Rizzo static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 965dfc91d7SLuigi Rizzo caddr_t cp, struct walkarg *w); 975dfc91d7SLuigi Rizzo static int rt_xaddrs(caddr_t cp, caddr_t cplim, 985dfc91d7SLuigi Rizzo struct rt_addrinfo *rtinfo); 99929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 100929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 10105b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 1025dfc91d7SLuigi Rizzo static int route_output(struct mbuf *m, struct socket *so); 1035dfc91d7SLuigi Rizzo static void rt_setmetrics(u_long which, const struct rt_metrics *in, 1045dfc91d7SLuigi Rizzo struct rt_metrics_lite *out); 1055dfc91d7SLuigi Rizzo static void rt_getmetrics(const struct rt_metrics_lite *in, 1065dfc91d7SLuigi Rizzo struct rt_metrics *out); 1075dfc91d7SLuigi Rizzo static void rt_dispatch(struct mbuf *, const struct sockaddr *); 108df8bae1dSRodney W. Grimes 109d989c7b3SRobert Watson static void 110d989c7b3SRobert Watson rts_init(void) 111d989c7b3SRobert Watson { 112b062951aSRobert Watson int tmp; 113d989c7b3SRobert Watson 114190a4c94SRobert Watson rtsintrq.ifq_maxlen = 256; 115b062951aSRobert Watson if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 116b062951aSRobert Watson rtsintrq.ifq_maxlen = tmp; 117d989c7b3SRobert Watson mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF); 118d989c7b3SRobert Watson netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE); 119d989c7b3SRobert Watson } 120d989c7b3SRobert Watson SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0) 121d989c7b3SRobert Watson 122d989c7b3SRobert Watson static void 123d989c7b3SRobert Watson rts_input(struct mbuf *m) 124d989c7b3SRobert Watson { 125d989c7b3SRobert Watson struct sockproto route_proto; 126d989c7b3SRobert Watson unsigned short *family; 127d989c7b3SRobert Watson struct m_tag *tag; 128d989c7b3SRobert Watson 129d989c7b3SRobert Watson route_proto.sp_family = PF_ROUTE; 130d989c7b3SRobert Watson tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 131d989c7b3SRobert Watson if (tag != NULL) { 132d989c7b3SRobert Watson family = (unsigned short *)(tag + 1); 133d989c7b3SRobert Watson route_proto.sp_protocol = *family; 134d989c7b3SRobert Watson m_tag_delete(m, tag); 135d989c7b3SRobert Watson } else 136d989c7b3SRobert Watson route_proto.sp_protocol = 0; 137d989c7b3SRobert Watson 138d989c7b3SRobert Watson raw_input(m, &route_proto, &route_src, &route_dst); 139d989c7b3SRobert Watson } 140d989c7b3SRobert Watson 141a29f300eSGarrett Wollman /* 142a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 143a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 144a29f300eSGarrett Wollman */ 145ac45e92fSRobert Watson static void 146a29f300eSGarrett Wollman rts_abort(struct socket *so) 147df8bae1dSRodney W. Grimes { 1487e994955SRobert Watson 149ac45e92fSRobert Watson raw_usrreqs.pru_abort(so); 150df8bae1dSRodney W. Grimes } 151a29f300eSGarrett Wollman 152a152f8a3SRobert Watson static void 153a152f8a3SRobert Watson rts_close(struct socket *so) 154a152f8a3SRobert Watson { 155a152f8a3SRobert Watson 156a152f8a3SRobert Watson raw_usrreqs.pru_close(so); 157a152f8a3SRobert Watson } 158a152f8a3SRobert Watson 159a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 160a29f300eSGarrett Wollman 161a29f300eSGarrett Wollman static int 162b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 163a29f300eSGarrett Wollman { 164a29f300eSGarrett Wollman struct rawcb *rp; 165a29f300eSGarrett Wollman int s, error; 166a29f300eSGarrett Wollman 167bc725eafSRobert Watson KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 168bc725eafSRobert Watson 1697cc0979fSDavid Malone /* XXX */ 170a163d034SWarner Losh MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 1715dfc91d7SLuigi Rizzo if (rp == NULL) 172a29f300eSGarrett Wollman return ENOBUFS; 173a29f300eSGarrett Wollman 174a29f300eSGarrett Wollman /* 175a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 176a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 177a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 178a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 179a29f300eSGarrett Wollman * eliminate the spl. 180a29f300eSGarrett Wollman */ 181a29f300eSGarrett Wollman s = splnet(); 182a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 183162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 184a29f300eSGarrett Wollman rp = sotorawcb(so); 185a29f300eSGarrett Wollman if (error) { 186a29f300eSGarrett Wollman splx(s); 1877ba271aeSJonathan Chen so->so_pcb = NULL; 188a29f300eSGarrett Wollman free(rp, M_PCB); 189a29f300eSGarrett Wollman return error; 190a29f300eSGarrett Wollman } 191aea8b30fSSam Leffler RTSOCK_LOCK(); 192a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 193a29f300eSGarrett Wollman case AF_INET: 194df8bae1dSRodney W. Grimes route_cb.ip_count++; 195a29f300eSGarrett Wollman break; 196899ce4f4SYoshinobu Inoue case AF_INET6: 197899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 198899ce4f4SYoshinobu Inoue break; 199a29f300eSGarrett Wollman case AF_IPX: 200cc6a66f2SJulian Elischer route_cb.ipx_count++; 201a29f300eSGarrett Wollman break; 202a29f300eSGarrett Wollman } 203df8bae1dSRodney W. Grimes rp->rcb_faddr = &route_src; 204df8bae1dSRodney W. Grimes route_cb.any_count++; 205aea8b30fSSam Leffler RTSOCK_UNLOCK(); 20603e49181SSeigo Tanimura soisconnected(so); 207df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 208df8bae1dSRodney W. Grimes splx(s); 209a29f300eSGarrett Wollman return 0; 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes 212a29f300eSGarrett Wollman static int 213b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 214a29f300eSGarrett Wollman { 2157e994955SRobert Watson 2167e994955SRobert Watson return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 217a29f300eSGarrett Wollman } 218a29f300eSGarrett Wollman 219a29f300eSGarrett Wollman static int 220b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 221a29f300eSGarrett Wollman { 2227e994955SRobert Watson 2237e994955SRobert Watson return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 224a29f300eSGarrett Wollman } 225a29f300eSGarrett Wollman 226a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 227a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 228a29f300eSGarrett Wollman 229bc725eafSRobert Watson static void 230a29f300eSGarrett Wollman rts_detach(struct socket *so) 231a29f300eSGarrett Wollman { 232a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 233a29f300eSGarrett Wollman 234bc725eafSRobert Watson KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 235bc725eafSRobert Watson 236aea8b30fSSam Leffler RTSOCK_LOCK(); 237a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 238a29f300eSGarrett Wollman case AF_INET: 239a29f300eSGarrett Wollman route_cb.ip_count--; 240a29f300eSGarrett Wollman break; 241899ce4f4SYoshinobu Inoue case AF_INET6: 242899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 243899ce4f4SYoshinobu Inoue break; 244a29f300eSGarrett Wollman case AF_IPX: 245a29f300eSGarrett Wollman route_cb.ipx_count--; 246a29f300eSGarrett Wollman break; 247a29f300eSGarrett Wollman } 248a29f300eSGarrett Wollman route_cb.any_count--; 249aea8b30fSSam Leffler RTSOCK_UNLOCK(); 250bc725eafSRobert Watson raw_usrreqs.pru_detach(so); 251a29f300eSGarrett Wollman } 252a29f300eSGarrett Wollman 253a29f300eSGarrett Wollman static int 254a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 255a29f300eSGarrett Wollman { 2567e994955SRobert Watson 2577e994955SRobert Watson return (raw_usrreqs.pru_disconnect(so)); 258a29f300eSGarrett Wollman } 259a29f300eSGarrett Wollman 260a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 261a29f300eSGarrett Wollman 262a29f300eSGarrett Wollman static int 26357bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 264a29f300eSGarrett Wollman { 2657e994955SRobert Watson 2667e994955SRobert Watson return (raw_usrreqs.pru_peeraddr(so, nam)); 267a29f300eSGarrett Wollman } 268a29f300eSGarrett Wollman 269a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 270a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 271a29f300eSGarrett Wollman 272a29f300eSGarrett Wollman static int 27357bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 274b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 275a29f300eSGarrett Wollman { 2767e994955SRobert Watson 2777e994955SRobert Watson return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 278a29f300eSGarrett Wollman } 279a29f300eSGarrett Wollman 280a29f300eSGarrett Wollman /* pru_sense is null */ 281a29f300eSGarrett Wollman 282a29f300eSGarrett Wollman static int 283a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 284a29f300eSGarrett Wollman { 2857e994955SRobert Watson 2867e994955SRobert Watson return (raw_usrreqs.pru_shutdown(so)); 287a29f300eSGarrett Wollman } 288a29f300eSGarrett Wollman 289a29f300eSGarrett Wollman static int 29057bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 291a29f300eSGarrett Wollman { 2927e994955SRobert Watson 2937e994955SRobert Watson return (raw_usrreqs.pru_sockaddr(so, nam)); 294a29f300eSGarrett Wollman } 295a29f300eSGarrett Wollman 296a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 297756d52a1SPoul-Henning Kamp .pru_abort = rts_abort, 298756d52a1SPoul-Henning Kamp .pru_attach = rts_attach, 299756d52a1SPoul-Henning Kamp .pru_bind = rts_bind, 300756d52a1SPoul-Henning Kamp .pru_connect = rts_connect, 301756d52a1SPoul-Henning Kamp .pru_detach = rts_detach, 302756d52a1SPoul-Henning Kamp .pru_disconnect = rts_disconnect, 303756d52a1SPoul-Henning Kamp .pru_peeraddr = rts_peeraddr, 304756d52a1SPoul-Henning Kamp .pru_send = rts_send, 305756d52a1SPoul-Henning Kamp .pru_shutdown = rts_shutdown, 306756d52a1SPoul-Henning Kamp .pru_sockaddr = rts_sockaddr, 307a152f8a3SRobert Watson .pru_close = rts_close, 308a29f300eSGarrett Wollman }; 309a29f300eSGarrett Wollman 310df8bae1dSRodney W. Grimes /*ARGSUSED*/ 31152041295SPoul-Henning Kamp static int 3125dfc91d7SLuigi Rizzo route_output(struct mbuf *m, struct socket *so) 313df8bae1dSRodney W. Grimes { 314becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 3155dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = NULL; 3165dfc91d7SLuigi Rizzo struct rtentry *rt = NULL; 31778a82810SGarrett Wollman struct radix_node_head *rnh; 318df8bae1dSRodney W. Grimes struct rt_addrinfo info; 319df8bae1dSRodney W. Grimes int len, error = 0; 3205dfc91d7SLuigi Rizzo struct ifnet *ifp = NULL; 3215dfc91d7SLuigi Rizzo struct ifaddr *ifa = NULL; 3225a59cefcSBosko Milekic struct sockaddr_in jail; 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 3255dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 3265dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 327df8bae1dSRodney W. Grimes return (ENOBUFS); 328df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 329df8bae1dSRodney W. Grimes panic("route_output"); 330df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 331df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 332df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 3335dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 334df8bae1dSRodney W. Grimes senderr(EINVAL); 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 3375dfc91d7SLuigi Rizzo if (rtm == NULL) { 3385dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 339df8bae1dSRodney W. Grimes senderr(ENOBUFS); 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 342df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 3435dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 344df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 3478071913dSRuslan Ermilov bzero(&info, sizeof(info)); 348df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 349076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 3505dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 351076d0761SJulian Elischer senderr(EINVAL); 352076d0761SJulian Elischer } 3538071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 3545dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 355becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 3565dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 357becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 358df8bae1dSRodney W. Grimes senderr(EINVAL); 359becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) { 360df8bae1dSRodney W. Grimes struct radix_node *t; 361becc44d7SSam Leffler t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1); 362ae24a36eSRuslan Ermilov if (t != NULL && 3639554c70bSRuslan Ermilov bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1, 3649554c70bSRuslan Ermilov (char *)(void *)t->rn_key + 1, 3659554c70bSRuslan Ermilov ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0) 366becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = 3679554c70bSRuslan Ermilov (struct sockaddr *)t->rn_key; 368df8bae1dSRodney W. Grimes else 369df8bae1dSRodney W. Grimes senderr(ENOBUFS); 370df8bae1dSRodney W. Grimes } 371162c0b2eSRuslan Ermilov 372162c0b2eSRuslan Ermilov /* 373162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 374162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 375162c0b2eSRuslan Ermilov */ 376acd3428bSRobert Watson if (rtm->rtm_type != RTM_GET) { 377acd3428bSRobert Watson error = priv_check(curthread, PRIV_NET_ROUTE); 378acd3428bSRobert Watson if (error) 379dadb6c3bSRuslan Ermilov senderr(error); 380acd3428bSRobert Watson } 381162c0b2eSRuslan Ermilov 382df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 383becc44d7SSam Leffler struct rtentry *saved_nrt; 384df8bae1dSRodney W. Grimes 385df8bae1dSRodney W. Grimes case RTM_ADD: 3865dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 387df8bae1dSRodney W. Grimes senderr(EINVAL); 3885dfc91d7SLuigi Rizzo saved_nrt = NULL; 3898071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &saved_nrt); 390df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 391d1dd20beSSam Leffler RT_LOCK(saved_nrt); 392df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 393df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 39422cafcf0SAndre Oppermann rtm->rtm_index = saved_nrt->rt_ifp->if_index; 3957138d65cSSam Leffler RT_REMREF(saved_nrt); 396becc44d7SSam Leffler saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK]; 397d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes break; 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes case RTM_DELETE: 4025dfc91d7SLuigi Rizzo saved_nrt = NULL; 4038071913dSRuslan Ermilov error = rtrequest1(RTM_DELETE, &info, &saved_nrt); 40478a82810SGarrett Wollman if (error == 0) { 405d1dd20beSSam Leffler RT_LOCK(saved_nrt); 40671eba915SRuslan Ermilov rt = saved_nrt; 40778a82810SGarrett Wollman goto report; 40878a82810SGarrett Wollman } 409df8bae1dSRodney W. Grimes break; 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes case RTM_GET: 412df8bae1dSRodney W. Grimes case RTM_CHANGE: 413df8bae1dSRodney W. Grimes case RTM_LOCK: 414becc44d7SSam Leffler rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family]; 4155dfc91d7SLuigi Rizzo if (rnh == NULL) 41678a82810SGarrett Wollman senderr(EAFNOSUPPORT); 417956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 418becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 419becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 42079188861SGleb Smirnoff if (rt == NULL) { /* XXX looks bogus */ 421956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 422df8bae1dSRodney W. Grimes senderr(ESRCH); 42379188861SGleb Smirnoff } 424d1dd20beSSam Leffler RT_LOCK(rt); 4257138d65cSSam Leffler RT_ADDREF(rt); 42679188861SGleb Smirnoff RADIX_NODE_HEAD_UNLOCK(rnh); 427956b0b65SJeffrey Hsu 428ba7be0a9SGeorge V. Neville-Neil /* 429ba7be0a9SGeorge V. Neville-Neil * Fix for PR: 82974 430ba7be0a9SGeorge V. Neville-Neil * 431ba7be0a9SGeorge V. Neville-Neil * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 432ba7be0a9SGeorge V. Neville-Neil * returns a perfect match in case a netmask is 433ba7be0a9SGeorge V. Neville-Neil * specified. For host routes only a longest prefix 434ba7be0a9SGeorge V. Neville-Neil * match is returned so it is necessary to compare the 435ba7be0a9SGeorge V. Neville-Neil * existence of the netmask. If both have a netmask 436ba7be0a9SGeorge V. Neville-Neil * rnh_lookup() did a perfect match and if none of them 437ba7be0a9SGeorge V. Neville-Neil * have a netmask both are host routes which is also a 438ba7be0a9SGeorge V. Neville-Neil * perfect match. 439ba7be0a9SGeorge V. Neville-Neil */ 440ba7be0a9SGeorge V. Neville-Neil 441ba7be0a9SGeorge V. Neville-Neil if (rtm->rtm_type != RTM_GET && 442ba7be0a9SGeorge V. Neville-Neil (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 443ba7be0a9SGeorge V. Neville-Neil RT_UNLOCK(rt); 444ba7be0a9SGeorge V. Neville-Neil senderr(ESRCH); 445ba7be0a9SGeorge V. Neville-Neil } 446ba7be0a9SGeorge V. Neville-Neil 447df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 448df8bae1dSRodney W. Grimes 449df8bae1dSRodney W. Grimes case RTM_GET: 45078a82810SGarrett Wollman report: 451d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 452becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 453becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 454becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 455becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 456df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 457df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 458df440948SPoul-Henning Kamp if (ifp) { 4599b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 4604a0d6638SRuslan Ermilov ifp->if_addr->ifa_addr; 4615a59cefcSBosko Milekic if (jailed(so->so_cred)) { 4629b3d77e7SBruce M Simpson bzero(&jail, sizeof(jail)); 4635a59cefcSBosko Milekic jail.sin_family = PF_INET; 4645a59cefcSBosko Milekic jail.sin_len = sizeof(jail); 4655a59cefcSBosko Milekic jail.sin_addr.s_addr = 4665a59cefcSBosko Milekic htonl(prison_getip(so->so_cred)); 4675a59cefcSBosko Milekic info.rti_info[RTAX_IFA] = 4685a59cefcSBosko Milekic (struct sockaddr *)&jail; 4695a59cefcSBosko Milekic } else 470becc44d7SSam Leffler info.rti_info[RTAX_IFA] = 471becc44d7SSam Leffler rt->rt_ifa->ifa_addr; 47228070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 473becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 474becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 475df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 476df8bae1dSRodney W. Grimes } else { 4775dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 4785dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 479df8bae1dSRodney W. Grimes } 48025029d6cSHartmut Brandt } else if ((ifp = rt->rt_ifp) != NULL) { 48125029d6cSHartmut Brandt rtm->rtm_index = ifp->if_index; 482df8bae1dSRodney W. Grimes } 483913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 484df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 485df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 486df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 4875dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 488d1dd20beSSam Leffler RT_UNLOCK(rt); 489df8bae1dSRodney W. Grimes senderr(ENOBUFS); 490becc44d7SSam Leffler } 4916b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 492df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 493df8bae1dSRodney W. Grimes } 494913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 495df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 49622cafcf0SAndre Oppermann rtm->rtm_use = 0; 49797d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 498df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 499df8bae1dSRodney W. Grimes break; 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes case RTM_CHANGE: 502becc44d7SSam Leffler /* 503becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 504becc44d7SSam Leffler * flags may also be different; ifp may be specified 505becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 506becc44d7SSam Leffler */ 507becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 508becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 509becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 510becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 511becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 512becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 513d1dd20beSSam Leffler RT_UNLOCK(rt); 514a11faa9fSGleb Smirnoff if ((error = rt_getifa(&info)) != 0) 5158071913dSRuslan Ermilov senderr(error); 516a11faa9fSGleb Smirnoff RT_LOCK(rt); 517becc44d7SSam Leffler } 518becc44d7SSam Leffler if (info.rti_info[RTAX_GATEWAY] != NULL && 519becc44d7SSam Leffler (error = rt_setgate(rt, rt_key(rt), 520becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY])) != 0) { 521d1dd20beSSam Leffler RT_UNLOCK(rt); 5228071913dSRuslan Ermilov senderr(error); 523becc44d7SSam Leffler } 5248071913dSRuslan Ermilov if ((ifa = info.rti_ifa) != NULL) { 525becc44d7SSam Leffler struct ifaddr *oifa = rt->rt_ifa; 526df8bae1dSRodney W. Grimes if (oifa != ifa) { 52719fc74fbSJeffrey Hsu if (oifa) { 52819fc74fbSJeffrey Hsu if (oifa->ifa_rtrequest) 529becc44d7SSam Leffler oifa->ifa_rtrequest( 530becc44d7SSam Leffler RTM_DELETE, rt, 5318071913dSRuslan Ermilov &info); 53212e552d6SJeffrey Hsu IFAFREE(oifa); 53319fc74fbSJeffrey Hsu } 53419fc74fbSJeffrey Hsu IFAREF(ifa); 535df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 5368071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes } 53922cafcf0SAndre Oppermann /* Allow some flags to be toggled on change. */ 54022cafcf0SAndre Oppermann if (rtm->rtm_fmask & RTF_FMASK) 54122cafcf0SAndre Oppermann rt->rt_flags = (rt->rt_flags & 54222cafcf0SAndre Oppermann ~rtm->rtm_fmask) | 54322cafcf0SAndre Oppermann (rtm->rtm_flags & rtm->rtm_fmask); 544df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 545df8bae1dSRodney W. Grimes &rt->rt_rmx); 54622cafcf0SAndre Oppermann rtm->rtm_index = rt->rt_ifp->if_index; 547df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 5488071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 549becc44d7SSam Leffler if (info.rti_info[RTAX_GENMASK]) 550becc44d7SSam Leffler rt->rt_genmask = info.rti_info[RTAX_GENMASK]; 55193b0017fSPhilippe Charnier /* FALLTHROUGH */ 552df8bae1dSRodney W. Grimes case RTM_LOCK: 55397d8d152SAndre Oppermann /* We don't support locks anymore */ 554df8bae1dSRodney W. Grimes break; 555df8bae1dSRodney W. Grimes } 556d1dd20beSSam Leffler RT_UNLOCK(rt); 557df8bae1dSRodney W. Grimes break; 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes default: 560df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 561df8bae1dSRodney W. Grimes } 562df8bae1dSRodney W. Grimes 563df8bae1dSRodney W. Grimes flush: 564df8bae1dSRodney W. Grimes if (rtm) { 565df8bae1dSRodney W. Grimes if (error) 566df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 567df8bae1dSRodney W. Grimes else 568df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 569df8bae1dSRodney W. Grimes } 570becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 571becc44d7SSam Leffler RTFREE(rt); 572df8bae1dSRodney W. Grimes { 5735dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 574df8bae1dSRodney W. Grimes /* 575df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 576df8bae1dSRodney W. Grimes */ 577df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 578df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 579df8bae1dSRodney W. Grimes if (rtm) 580df8bae1dSRodney W. Grimes Free(rtm); 581df8bae1dSRodney W. Grimes m_freem(m); 582df8bae1dSRodney W. Grimes return (error); 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 585df8bae1dSRodney W. Grimes rp = sotorawcb(so); 5864cc20ab1SSeigo Tanimura } 587df8bae1dSRodney W. Grimes if (rtm) { 588df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 58903311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 59003311056SHajimu UMEMOTO m_freem(m); 59103311056SHajimu UMEMOTO m = NULL; 59203311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 59303311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 594df8bae1dSRodney W. Grimes Free(rtm); 595df8bae1dSRodney W. Grimes } 596becc44d7SSam Leffler if (m) { 597becc44d7SSam Leffler if (rp) { 598becc44d7SSam Leffler /* 599becc44d7SSam Leffler * XXX insure we don't get a copy by 600becc44d7SSam Leffler * invalidating our protocol 601becc44d7SSam Leffler */ 602becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 603becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 604becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 605becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 606becc44d7SSam Leffler } else 607becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 608becc44d7SSam Leffler } 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes return (error); 611becc44d7SSam Leffler #undef sa_equal 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes 61452041295SPoul-Henning Kamp static void 6155dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 6165dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 617df8bae1dSRodney W. Grimes { 618df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 61997d8d152SAndre Oppermann /* 62097d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 62197d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 62297d8d152SAndre Oppermann */ 623df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 624e27c3f48SOleg Bulyzhin /* Userland -> kernel timebase conversion. */ 625e27c3f48SOleg Bulyzhin if (which & RTV_EXPIRE) 626e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 627e27c3f48SOleg Bulyzhin in->rmx_expire - time_second + time_uptime : 0; 628df8bae1dSRodney W. Grimes #undef metric 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 63197d8d152SAndre Oppermann static void 6325dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 63397d8d152SAndre Oppermann { 63497d8d152SAndre Oppermann #define metric(e) out->e = in->e; 63597d8d152SAndre Oppermann bzero(out, sizeof(*out)); 63697d8d152SAndre Oppermann metric(rmx_mtu); 637e27c3f48SOleg Bulyzhin /* Kernel -> userland timebase conversion. */ 638e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 639e27c3f48SOleg Bulyzhin in->rmx_expire - time_uptime + time_second : 0; 64097d8d152SAndre Oppermann #undef metric 64197d8d152SAndre Oppermann } 64297d8d152SAndre Oppermann 6437f33a738SJulian Elischer /* 6447f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 6457f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 646076d0761SJulian Elischer * This data is derived straight from userland. 6477f33a738SJulian Elischer */ 648076d0761SJulian Elischer static int 649becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 650df8bae1dSRodney W. Grimes { 651e74642dfSLuigi Rizzo struct sockaddr *sa; 652e74642dfSLuigi Rizzo int i; 653df8bae1dSRodney W. Grimes 654becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 655df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 656df8bae1dSRodney W. Grimes continue; 657ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 6587f33a738SJulian Elischer /* 659076d0761SJulian Elischer * It won't fit. 6607f33a738SJulian Elischer */ 661becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 662076d0761SJulian Elischer return (EINVAL); 6637f33a738SJulian Elischer /* 6647f33a738SJulian Elischer * there are no more.. quit now 6657f33a738SJulian Elischer * If there are more bits, they are in error. 6667f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 6677f33a738SJulian Elischer * This causes kernel to core dump. 668076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 6697f33a738SJulian Elischer */ 670076d0761SJulian Elischer if (sa->sa_len == 0) { 671076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 672076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 673df8bae1dSRodney W. Grimes } 674076d0761SJulian Elischer /* accept it */ 675076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 676e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 677076d0761SJulian Elischer } 678076d0761SJulian Elischer return (0); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes static struct mbuf * 682becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 683df8bae1dSRodney W. Grimes { 6845dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 6855dfc91d7SLuigi Rizzo struct mbuf *m; 6865dfc91d7SLuigi Rizzo int i; 6875dfc91d7SLuigi Rizzo struct sockaddr *sa; 688df8bae1dSRodney W. Grimes int len, dlen; 689df8bae1dSRodney W. Grimes 690df8bae1dSRodney W. Grimes switch (type) { 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes case RTM_DELADDR: 693df8bae1dSRodney W. Grimes case RTM_NEWADDR: 694df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 695df8bae1dSRodney W. Grimes break; 696df8bae1dSRodney W. Grimes 697477180fbSGarrett Wollman case RTM_DELMADDR: 698477180fbSGarrett Wollman case RTM_NEWMADDR: 699477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 700477180fbSGarrett Wollman break; 701477180fbSGarrett Wollman 702df8bae1dSRodney W. Grimes case RTM_IFINFO: 703df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 704df8bae1dSRodney W. Grimes break; 705df8bae1dSRodney W. Grimes 7067b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 707b83a279fSSam Leffler case RTM_IEEE80211: 7087b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 7097b6edd04SRuslan Ermilov break; 7107b6edd04SRuslan Ermilov 711df8bae1dSRodney W. Grimes default: 712df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 713df8bae1dSRodney W. Grimes } 71433841545SHajimu UMEMOTO if (len > MCLBYTES) 715df8bae1dSRodney W. Grimes panic("rt_msg1"); 716a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 71733841545SHajimu UMEMOTO if (m && len > MHLEN) { 718a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 71933841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 72033841545SHajimu UMEMOTO m_free(m); 72133841545SHajimu UMEMOTO m = NULL; 72233841545SHajimu UMEMOTO } 72333841545SHajimu UMEMOTO } 7245dfc91d7SLuigi Rizzo if (m == NULL) 72533841545SHajimu UMEMOTO return (m); 726df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 7275dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 728df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 729df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 730df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 731df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 732df8bae1dSRodney W. Grimes continue; 733df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 734e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 735df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 736df8bae1dSRodney W. Grimes len += dlen; 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 739df8bae1dSRodney W. Grimes m_freem(m); 740df8bae1dSRodney W. Grimes return (NULL); 741df8bae1dSRodney W. Grimes } 742df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 743df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 744df8bae1dSRodney W. Grimes rtm->rtm_type = type; 745df8bae1dSRodney W. Grimes return (m); 746df8bae1dSRodney W. Grimes } 747df8bae1dSRodney W. Grimes 748df8bae1dSRodney W. Grimes static int 749becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 750df8bae1dSRodney W. Grimes { 7515dfc91d7SLuigi Rizzo int i; 752df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 753df8bae1dSRodney W. Grimes caddr_t cp0; 754df8bae1dSRodney W. Grimes 755df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 756df8bae1dSRodney W. Grimes again: 757df8bae1dSRodney W. Grimes switch (type) { 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes case RTM_DELADDR: 760df8bae1dSRodney W. Grimes case RTM_NEWADDR: 761df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 762df8bae1dSRodney W. Grimes break; 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes case RTM_IFINFO: 765df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 766df8bae1dSRodney W. Grimes break; 767df8bae1dSRodney W. Grimes 76805b2efe0SBruce M Simpson case RTM_NEWMADDR: 76905b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 77005b2efe0SBruce M Simpson break; 77105b2efe0SBruce M Simpson 772df8bae1dSRodney W. Grimes default: 773df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 774df8bae1dSRodney W. Grimes } 775df440948SPoul-Henning Kamp cp0 = cp; 776df440948SPoul-Henning Kamp if (cp0) 777df8bae1dSRodney W. Grimes cp += len; 778df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 7795dfc91d7SLuigi Rizzo struct sockaddr *sa; 780df8bae1dSRodney W. Grimes 7815dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 782df8bae1dSRodney W. Grimes continue; 783df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 784e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 785df8bae1dSRodney W. Grimes if (cp) { 786df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 787df8bae1dSRodney W. Grimes cp += dlen; 788df8bae1dSRodney W. Grimes } 789df8bae1dSRodney W. Grimes len += dlen; 790df8bae1dSRodney W. Grimes } 791694ff264SAndrew Gallatin len = ALIGN(len); 7925dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 7935dfc91d7SLuigi Rizzo struct walkarg *rw = w; 794df8bae1dSRodney W. Grimes 79552041295SPoul-Henning Kamp if (rw->w_req) { 796df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 797df8bae1dSRodney W. Grimes if (rw->w_tmem) 798df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 799df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 800df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 801df440948SPoul-Henning Kamp if (rw->w_tmem) 802df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 803df8bae1dSRodney W. Grimes } 804df8bae1dSRodney W. Grimes if (rw->w_tmem) { 805df8bae1dSRodney W. Grimes cp = rw->w_tmem; 806df8bae1dSRodney W. Grimes second_time = 1; 807df8bae1dSRodney W. Grimes goto again; 80852041295SPoul-Henning Kamp } 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes } 811df8bae1dSRodney W. Grimes if (cp) { 8125dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 813df8bae1dSRodney W. Grimes 814df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 815df8bae1dSRodney W. Grimes rtm->rtm_type = type; 816df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 817df8bae1dSRodney W. Grimes } 818df8bae1dSRodney W. Grimes return (len); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes 821df8bae1dSRodney W. Grimes /* 822df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 823df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 824df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 825df8bae1dSRodney W. Grimes * destination. 826df8bae1dSRodney W. Grimes */ 827df8bae1dSRodney W. Grimes void 828becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 829df8bae1dSRodney W. Grimes { 830becc44d7SSam Leffler struct rt_msghdr *rtm; 831becc44d7SSam Leffler struct mbuf *m; 832df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 833df8bae1dSRodney W. Grimes 834df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 835df8bae1dSRodney W. Grimes return; 836df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 8375dfc91d7SLuigi Rizzo if (m == NULL) 838df8bae1dSRodney W. Grimes return; 839df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 840df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 841df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 842df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 843becc44d7SSam Leffler rt_dispatch(m, sa); 844df8bae1dSRodney W. Grimes } 845df8bae1dSRodney W. Grimes 846df8bae1dSRodney W. Grimes /* 847df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 848df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 849df8bae1dSRodney W. Grimes */ 850df8bae1dSRodney W. Grimes void 851becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 852df8bae1dSRodney W. Grimes { 853becc44d7SSam Leffler struct if_msghdr *ifm; 854df8bae1dSRodney W. Grimes struct mbuf *m; 855df8bae1dSRodney W. Grimes struct rt_addrinfo info; 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 858df8bae1dSRodney W. Grimes return; 859df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 860df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 8615dfc91d7SLuigi Rizzo if (m == NULL) 862df8bae1dSRodney W. Grimes return; 863df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 864df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 865292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 866df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 867df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 868becc44d7SSam Leffler rt_dispatch(m, NULL); 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes 871df8bae1dSRodney W. Grimes /* 872df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 873df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 874df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 875df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 876df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 877df8bae1dSRodney W. Grimes * copies of it. 878df8bae1dSRodney W. Grimes */ 879df8bae1dSRodney W. Grimes void 880becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 881df8bae1dSRodney W. Grimes { 882df8bae1dSRodney W. Grimes struct rt_addrinfo info; 8835dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 884df8bae1dSRodney W. Grimes int pass; 8855dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 886df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 887df8bae1dSRodney W. Grimes 8887a7fa27bSSam Leffler KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 8897a7fa27bSSam Leffler ("unexpected cmd %u", cmd)); 890f8829a4aSRandall Stewart #ifdef SCTP 891f8829a4aSRandall Stewart /* 892f8829a4aSRandall Stewart * notify the SCTP stack 893f8829a4aSRandall Stewart * this will only get called when an address is added/deleted 894f8829a4aSRandall Stewart * XXX pass the ifaddr struct instead if ifa->ifa_addr... 895f8829a4aSRandall Stewart */ 896f8829a4aSRandall Stewart sctp_addr_change(ifa, cmd); 897f8829a4aSRandall Stewart #endif /* SCTP */ 898df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 899df8bae1dSRodney W. Grimes return; 900df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 901df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 902df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 903df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 9045dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 905df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 906df8bae1dSRodney W. Grimes 907becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 9084a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 909becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 910becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 911df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 912df8bae1dSRodney W. Grimes continue; 913df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 914df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 915df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 916df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 917df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 918df8bae1dSRodney W. Grimes } 919df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 920df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 9215dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 922df8bae1dSRodney W. Grimes 9235dfc91d7SLuigi Rizzo if (rt == NULL) 924df8bae1dSRodney W. Grimes continue; 925becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 926becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 927becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 928df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 929df8bae1dSRodney W. Grimes continue; 930df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 931df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 932df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 933df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 934df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 935df8bae1dSRodney W. Grimes } 936becc44d7SSam Leffler rt_dispatch(m, sa); 937df8bae1dSRodney W. Grimes } 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes 940477180fbSGarrett Wollman /* 941477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 942477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 943477180fbSGarrett Wollman * there is no route state to worry about. 944477180fbSGarrett Wollman */ 945477180fbSGarrett Wollman void 946becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 947477180fbSGarrett Wollman { 948477180fbSGarrett Wollman struct rt_addrinfo info; 9495dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 950477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 951477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 952477180fbSGarrett Wollman 953477180fbSGarrett Wollman if (route_cb.any_count == 0) 954477180fbSGarrett Wollman return; 955477180fbSGarrett Wollman 956477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 957becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 9584a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 959477180fbSGarrett Wollman /* 960477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 961477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 962477180fbSGarrett Wollman */ 963becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 964becc44d7SSam Leffler m = rt_msg1(cmd, &info); 965becc44d7SSam Leffler if (m == NULL) 966477180fbSGarrett Wollman return; 967477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 968477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 969477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 970becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 971477180fbSGarrett Wollman } 97252041295SPoul-Henning Kamp 973b83a279fSSam Leffler static struct mbuf * 974b83a279fSSam Leffler rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 975b83a279fSSam Leffler struct rt_addrinfo *info) 976b83a279fSSam Leffler { 977b83a279fSSam Leffler struct if_announcemsghdr *ifan; 978b83a279fSSam Leffler struct mbuf *m; 979b83a279fSSam Leffler 980b83a279fSSam Leffler if (route_cb.any_count == 0) 981b83a279fSSam Leffler return NULL; 982b83a279fSSam Leffler bzero((caddr_t)info, sizeof(*info)); 983b83a279fSSam Leffler m = rt_msg1(type, info); 984b83a279fSSam Leffler if (m != NULL) { 985b83a279fSSam Leffler ifan = mtod(m, struct if_announcemsghdr *); 986b83a279fSSam Leffler ifan->ifan_index = ifp->if_index; 987b83a279fSSam Leffler strlcpy(ifan->ifan_name, ifp->if_xname, 988b83a279fSSam Leffler sizeof(ifan->ifan_name)); 989b83a279fSSam Leffler ifan->ifan_what = what; 990b83a279fSSam Leffler } 991b83a279fSSam Leffler return m; 992b83a279fSSam Leffler } 993b83a279fSSam Leffler 994b83a279fSSam Leffler /* 995b83a279fSSam Leffler * This is called to generate routing socket messages indicating 996b83a279fSSam Leffler * IEEE80211 wireless events. 997b83a279fSSam Leffler * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 998b83a279fSSam Leffler */ 999b83a279fSSam Leffler void 1000b83a279fSSam Leffler rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1001b83a279fSSam Leffler { 1002b83a279fSSam Leffler struct mbuf *m; 1003b83a279fSSam Leffler struct rt_addrinfo info; 1004b83a279fSSam Leffler 1005b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1006b83a279fSSam Leffler if (m != NULL) { 1007b83a279fSSam Leffler /* 1008b83a279fSSam Leffler * Append the ieee80211 data. Try to stick it in the 1009b83a279fSSam Leffler * mbuf containing the ifannounce msg; otherwise allocate 1010b83a279fSSam Leffler * a new mbuf and append. 1011b83a279fSSam Leffler * 1012b83a279fSSam Leffler * NB: we assume m is a single mbuf. 1013b83a279fSSam Leffler */ 1014b83a279fSSam Leffler if (data_len > M_TRAILINGSPACE(m)) { 1015b83a279fSSam Leffler struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1016b83a279fSSam Leffler if (n == NULL) { 1017b83a279fSSam Leffler m_freem(m); 1018b83a279fSSam Leffler return; 1019b83a279fSSam Leffler } 1020b83a279fSSam Leffler bcopy(data, mtod(n, void *), data_len); 1021b83a279fSSam Leffler n->m_len = data_len; 1022b83a279fSSam Leffler m->m_next = n; 1023b83a279fSSam Leffler } else if (data_len > 0) { 1024b83a279fSSam Leffler bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1025b83a279fSSam Leffler m->m_len += data_len; 1026b83a279fSSam Leffler } 1027b83a279fSSam Leffler if (m->m_flags & M_PKTHDR) 1028b83a279fSSam Leffler m->m_pkthdr.len += data_len; 1029b83a279fSSam Leffler mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1030b83a279fSSam Leffler rt_dispatch(m, NULL); 1031b83a279fSSam Leffler } 1032b83a279fSSam Leffler } 1033b83a279fSSam Leffler 1034df8bae1dSRodney W. Grimes /* 10357b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 10367b6edd04SRuslan Ermilov * network interface arrival and departure. 10377b6edd04SRuslan Ermilov */ 10387b6edd04SRuslan Ermilov void 1039becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 10407b6edd04SRuslan Ermilov { 10417b6edd04SRuslan Ermilov struct mbuf *m; 10427b6edd04SRuslan Ermilov struct rt_addrinfo info; 10437b6edd04SRuslan Ermilov 1044b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1045b83a279fSSam Leffler if (m != NULL) 1046becc44d7SSam Leffler rt_dispatch(m, NULL); 1047becc44d7SSam Leffler } 1048becc44d7SSam Leffler 1049becc44d7SSam Leffler static void 10505dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 1051becc44d7SSam Leffler { 1052d989c7b3SRobert Watson struct m_tag *tag; 1053becc44d7SSam Leffler 1054d989c7b3SRobert Watson /* 1055d989c7b3SRobert Watson * Preserve the family from the sockaddr, if any, in an m_tag for 1056d989c7b3SRobert Watson * use when injecting the mbuf into the routing socket buffer from 1057d989c7b3SRobert Watson * the netisr. 1058d989c7b3SRobert Watson */ 1059d989c7b3SRobert Watson if (sa != NULL) { 1060d989c7b3SRobert Watson tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1061d989c7b3SRobert Watson M_NOWAIT); 1062d989c7b3SRobert Watson if (tag == NULL) { 1063d989c7b3SRobert Watson m_freem(m); 1064d989c7b3SRobert Watson return; 1065d989c7b3SRobert Watson } 10668d78bea4SSam Leffler *(unsigned short *)(tag + 1) = sa->sa_family; 1067d989c7b3SRobert Watson m_tag_prepend(m, tag); 1068d989c7b3SRobert Watson } 10693161f583SAndre Oppermann netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 10707b6edd04SRuslan Ermilov } 10717b6edd04SRuslan Ermilov 10727b6edd04SRuslan Ermilov /* 1073df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 1074df8bae1dSRodney W. Grimes */ 107537c84183SPoul-Henning Kamp static int 1076becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 1077df8bae1dSRodney W. Grimes { 1078becc44d7SSam Leffler struct walkarg *w = vw; 1079becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 1080df8bae1dSRodney W. Grimes int error = 0, size; 1081df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1082df8bae1dSRodney W. Grimes 1083df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1084df8bae1dSRodney W. Grimes return 0; 1085df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1086becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 1087becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1088becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1089becc44d7SSam Leffler info.rti_info[RTAX_GENMASK] = rt->rt_genmask; 109028070a0eSRuslan Ermilov if (rt->rt_ifp) { 10914a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1092becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 109328070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1094becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 109528070a0eSRuslan Ermilov } 1096913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 109752041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1098becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1099df8bae1dSRodney W. Grimes 1100df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 110197d8d152SAndre Oppermann rtm->rtm_use = rt->rt_rmx.rmx_pksent; 110297d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1103df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 1104df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1105df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 110652041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 110752041295SPoul-Henning Kamp return (error); 1108df8bae1dSRodney W. Grimes } 1109df8bae1dSRodney W. Grimes return (error); 1110df8bae1dSRodney W. Grimes } 1111df8bae1dSRodney W. Grimes 111237c84183SPoul-Henning Kamp static int 1113becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 1114df8bae1dSRodney W. Grimes { 1115becc44d7SSam Leffler struct ifnet *ifp; 1116becc44d7SSam Leffler struct ifaddr *ifa; 1117df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1118df8bae1dSRodney W. Grimes int len, error = 0; 1119df8bae1dSRodney W. Grimes 1120df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1121fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1122fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 1123df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 1124df8bae1dSRodney W. Grimes continue; 11254a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1126becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1127913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 11285dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 112952041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1130becc44d7SSam Leffler struct if_msghdr *ifm; 1131df8bae1dSRodney W. Grimes 1132df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 1133df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1134292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1135df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1136df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 113752041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 1138df440948SPoul-Henning Kamp if (error) 1139a35b06c5SJonathan Lemon goto done; 1140df8bae1dSRodney W. Grimes } 11415dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1142df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 1143df8bae1dSRodney W. Grimes continue; 1144a854ed98SJohn Baldwin if (jailed(curthread->td_ucred) && 1145a854ed98SJohn Baldwin prison_if(curthread->td_ucred, ifa->ifa_addr)) 114675c13541SPoul-Henning Kamp continue; 1147becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1148becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1149becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1150913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 115152041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1152becc44d7SSam Leffler struct ifa_msghdr *ifam; 1153df8bae1dSRodney W. Grimes 1154df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1155df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1156df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1157df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1158df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 115952041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1160df440948SPoul-Henning Kamp if (error) 1161a35b06c5SJonathan Lemon goto done; 1162df8bae1dSRodney W. Grimes } 1163df8bae1dSRodney W. Grimes } 1164becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 11655dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1166df8bae1dSRodney W. Grimes } 1167a35b06c5SJonathan Lemon done: 1168fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 1169a35b06c5SJonathan Lemon return (error); 1170df8bae1dSRodney W. Grimes } 1171df8bae1dSRodney W. Grimes 117205b2efe0SBruce M Simpson int 11739b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 117405b2efe0SBruce M Simpson { 11759b98ee2cSLuigi Rizzo struct ifnet *ifp; 117605b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 117705b2efe0SBruce M Simpson struct rt_addrinfo info; 117805b2efe0SBruce M Simpson int len, error = 0; 11799b98ee2cSLuigi Rizzo struct ifaddr *ifa; 118005b2efe0SBruce M Simpson 118105b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 1182fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 118305b2efe0SBruce M Simpson TAILQ_FOREACH(ifp, &ifnet, if_link) { 118405b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 118505b2efe0SBruce M Simpson continue; 11864a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1187913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1188fe0fc7efSChristian S.J. Peron IF_ADDR_LOCK(ifp); 118905b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 119005b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 119105b2efe0SBruce M Simpson continue; 119205b2efe0SBruce M Simpson if (jailed(curproc->p_ucred) && 119305b2efe0SBruce M Simpson prison_if(curproc->p_ucred, ifma->ifma_addr)) 119405b2efe0SBruce M Simpson continue; 119505b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1196913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1197913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1198913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1199913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 120005b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 12019b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 120205b2efe0SBruce M Simpson 120305b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 120405b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 120505b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 120605b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 120705b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1208fe0fc7efSChristian S.J. Peron if (error) { 1209fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 121005b2efe0SBruce M Simpson goto done; 121105b2efe0SBruce M Simpson } 121205b2efe0SBruce M Simpson } 121305b2efe0SBruce M Simpson } 1214fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 1215fe0fc7efSChristian S.J. Peron } 121605b2efe0SBruce M Simpson done: 1217fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 121805b2efe0SBruce M Simpson return (error); 121905b2efe0SBruce M Simpson } 122005b2efe0SBruce M Simpson 122152041295SPoul-Henning Kamp static int 122282d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1223df8bae1dSRodney W. Grimes { 122452041295SPoul-Henning Kamp int *name = (int *)arg1; 122552041295SPoul-Henning Kamp u_int namelen = arg2; 1226becc44d7SSam Leffler struct radix_node_head *rnh; 1227fe0fc7efSChristian S.J. Peron int i, lim, error = EINVAL; 1228df8bae1dSRodney W. Grimes u_char af; 1229df8bae1dSRodney W. Grimes struct walkarg w; 1230df8bae1dSRodney W. Grimes 123152041295SPoul-Henning Kamp name ++; 123252041295SPoul-Henning Kamp namelen--; 123352041295SPoul-Henning Kamp if (req->newptr) 1234df8bae1dSRodney W. Grimes return (EPERM); 1235df8bae1dSRodney W. Grimes if (namelen != 3) 1236f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1237df8bae1dSRodney W. Grimes af = name[0]; 1238b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1239b2aaf46eSJeffrey Hsu return (EINVAL); 12406b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1241df8bae1dSRodney W. Grimes w.w_op = name[1]; 1242df8bae1dSRodney W. Grimes w.w_arg = name[2]; 124352041295SPoul-Henning Kamp w.w_req = req; 1244df8bae1dSRodney W. Grimes 1245fe0fc7efSChristian S.J. Peron error = sysctl_wire_old_buffer(req, 0); 1246fe0fc7efSChristian S.J. Peron if (error) 1247fe0fc7efSChristian S.J. Peron return (error); 1248df8bae1dSRodney W. Grimes switch (w.w_op) { 1249df8bae1dSRodney W. Grimes 1250df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1251df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1252a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1253a8b76c8fSLuigi Rizzo i = 1; 1254a8b76c8fSLuigi Rizzo lim = AF_MAX; 1255a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1256a8b76c8fSLuigi Rizzo i = lim = af; 1257a8b76c8fSLuigi Rizzo for (error = 0; error == 0 && i <= lim; i++) 1258a8b76c8fSLuigi Rizzo if ((rnh = rt_tables[i]) != NULL) { 1259fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_LOCK(rnh); 1260956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 1261fe0fc7efSChristian S.J. Peron sysctl_dumpentry, &w); 1262fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_UNLOCK(rnh); 1263a8b76c8fSLuigi Rizzo } else if (af != 0) 1264956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 1265df8bae1dSRodney W. Grimes break; 1266df8bae1dSRodney W. Grimes 1267df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1268df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 126905b2efe0SBruce M Simpson break; 127005b2efe0SBruce M Simpson 127105b2efe0SBruce M Simpson case NET_RT_IFMALIST: 127205b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 127305b2efe0SBruce M Simpson break; 1274df8bae1dSRodney W. Grimes } 1275df8bae1dSRodney W. Grimes if (w.w_tmem) 1276df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1277df8bae1dSRodney W. Grimes return (error); 1278df8bae1dSRodney W. Grimes } 1279df8bae1dSRodney W. Grimes 128052041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 128152041295SPoul-Henning Kamp 1282df8bae1dSRodney W. Grimes /* 1283df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1284df8bae1dSRodney W. Grimes */ 1285df8bae1dSRodney W. Grimes 12865b1c0294SDavid E. O'Brien static struct domain routedomain; /* or at least forward */ 1287df8bae1dSRodney W. Grimes 128852041295SPoul-Henning Kamp static struct protosw routesw[] = { 1289303989a2SRuslan Ermilov { 1290303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 1291303989a2SRuslan Ermilov .pr_domain = &routedomain, 1292303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1293303989a2SRuslan Ermilov .pr_output = route_output, 1294303989a2SRuslan Ermilov .pr_ctlinput = raw_ctlinput, 1295303989a2SRuslan Ermilov .pr_init = raw_init, 1296303989a2SRuslan Ermilov .pr_usrreqs = &route_usrreqs 1297df8bae1dSRodney W. Grimes } 1298df8bae1dSRodney W. Grimes }; 1299df8bae1dSRodney W. Grimes 1300303989a2SRuslan Ermilov static struct domain routedomain = { 1301303989a2SRuslan Ermilov .dom_family = PF_ROUTE, 1302303989a2SRuslan Ermilov .dom_name = "route", 1303303989a2SRuslan Ermilov .dom_protosw = routesw, 1304303989a2SRuslan Ermilov .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1305303989a2SRuslan Ermilov }; 130678a82810SGarrett Wollman 1307748e0b0aSGarrett Wollman DOMAIN_SET(route); 1308