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 */ 32427a928aSKonstantin Belousov #include "opt_compat.h" 33f8829a4aSRandall Stewart #include "opt_sctp.h" 34e440aed9SQing Li #include "opt_mpath.h" 35413628a7SBjoern A. Zeeb #include "opt_inet.h" 36413628a7SBjoern A. Zeeb #include "opt_inet6.h" 37e440aed9SQing Li 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39960ed29cSSeigo Tanimura #include <sys/jail.h> 40413628a7SBjoern A. Zeeb #include <sys/kernel.h> 41d0728d71SRobert Watson #include <sys/domain.h> 42609ff41fSWarner Losh #include <sys/lock.h> 434d1d4912SBruce Evans #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45acd3428bSRobert Watson #include <sys/priv.h> 46960ed29cSSeigo Tanimura #include <sys/proc.h> 47960ed29cSSeigo Tanimura #include <sys/protosw.h> 483120b9d4SKip Macy #include <sys/rwlock.h> 49960ed29cSSeigo Tanimura #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 52960ed29cSSeigo Tanimura #include <sys/sysctl.h> 53960ed29cSSeigo Tanimura #include <sys/systm.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <net/if.h> 568eca593cSQing Li #include <net/if_dl.h> 576e6b3f7cSQing Li #include <net/if_llatbl.h> 580ed6142bSQing Li #include <net/if_types.h> 59d989c7b3SRobert Watson #include <net/netisr.h> 60df8bae1dSRodney W. Grimes #include <net/raw_cb.h> 61960ed29cSSeigo Tanimura #include <net/route.h> 624b79449eSBjoern A. Zeeb #include <net/vnet.h> 63df8bae1dSRodney W. Grimes 645a59cefcSBosko Milekic #include <netinet/in.h> 65c7ab6602SQing Li #include <netinet/if_ether.h> 66413628a7SBjoern A. Zeeb #ifdef INET6 67413628a7SBjoern A. Zeeb #include <netinet6/scope6_var.h> 68413628a7SBjoern A. Zeeb #endif 695a59cefcSBosko Milekic 70c03528b6SBjoern A. Zeeb #if defined(INET) || defined(INET6) 71f8829a4aSRandall Stewart #ifdef SCTP 72f8829a4aSRandall Stewart extern void sctp_addr_change(struct ifaddr *ifa, int cmd); 73f8829a4aSRandall Stewart #endif /* SCTP */ 74c03528b6SBjoern A. Zeeb #endif 75f8829a4aSRandall Stewart 76427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 77427a928aSKonstantin Belousov #include <sys/mount.h> 78427a928aSKonstantin Belousov #include <compat/freebsd32/freebsd32.h> 79427a928aSKonstantin Belousov 80427a928aSKonstantin Belousov struct if_data32 { 81427a928aSKonstantin Belousov uint8_t ifi_type; 82427a928aSKonstantin Belousov uint8_t ifi_physical; 83427a928aSKonstantin Belousov uint8_t ifi_addrlen; 84427a928aSKonstantin Belousov uint8_t ifi_hdrlen; 85427a928aSKonstantin Belousov uint8_t ifi_link_state; 86427a928aSKonstantin Belousov uint8_t ifi_spare_char1; 87427a928aSKonstantin Belousov uint8_t ifi_spare_char2; 88427a928aSKonstantin Belousov uint8_t ifi_datalen; 89427a928aSKonstantin Belousov uint32_t ifi_mtu; 90427a928aSKonstantin Belousov uint32_t ifi_metric; 91427a928aSKonstantin Belousov uint32_t ifi_baudrate; 92427a928aSKonstantin Belousov uint32_t ifi_ipackets; 93427a928aSKonstantin Belousov uint32_t ifi_ierrors; 94427a928aSKonstantin Belousov uint32_t ifi_opackets; 95427a928aSKonstantin Belousov uint32_t ifi_oerrors; 96427a928aSKonstantin Belousov uint32_t ifi_collisions; 97427a928aSKonstantin Belousov uint32_t ifi_ibytes; 98427a928aSKonstantin Belousov uint32_t ifi_obytes; 99427a928aSKonstantin Belousov uint32_t ifi_imcasts; 100427a928aSKonstantin Belousov uint32_t ifi_omcasts; 101427a928aSKonstantin Belousov uint32_t ifi_iqdrops; 102427a928aSKonstantin Belousov uint32_t ifi_noproto; 103427a928aSKonstantin Belousov uint32_t ifi_hwassist; 104427a928aSKonstantin Belousov int32_t ifi_epoch; 105427a928aSKonstantin Belousov struct timeval32 ifi_lastchange; 106427a928aSKonstantin Belousov }; 107427a928aSKonstantin Belousov 108427a928aSKonstantin Belousov struct if_msghdr32 { 109427a928aSKonstantin Belousov uint16_t ifm_msglen; 110427a928aSKonstantin Belousov uint8_t ifm_version; 111427a928aSKonstantin Belousov uint8_t ifm_type; 112427a928aSKonstantin Belousov int32_t ifm_addrs; 113427a928aSKonstantin Belousov int32_t ifm_flags; 114427a928aSKonstantin Belousov uint16_t ifm_index; 115427a928aSKonstantin Belousov struct if_data32 ifm_data; 116427a928aSKonstantin Belousov }; 117427a928aSKonstantin Belousov #endif 118427a928aSKonstantin Belousov 119a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 120a1c995b6SPoul-Henning Kamp 121becc44d7SSam Leffler /* NB: these are not modified */ 12252041295SPoul-Henning Kamp static struct sockaddr route_src = { 2, PF_ROUTE, }; 123076d0761SJulian Elischer static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 124becc44d7SSam Leffler 125528737fdSBjoern A. Zeeb /* 126528737fdSBjoern A. Zeeb * Used by rtsock/raw_input callback code to decide whether to filter the update 127528737fdSBjoern A. Zeeb * notification to a socket bound to a particular FIB. 128528737fdSBjoern A. Zeeb */ 129528737fdSBjoern A. Zeeb #define RTS_FILTER_FIB M_PROTO8 130528737fdSBjoern A. Zeeb #define RTS_ALLFIBS -1 131528737fdSBjoern A. Zeeb 132becc44d7SSam Leffler static struct { 13318aee723SPeter Pentchev int ip_count; /* attached w/ AF_INET */ 134becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 135becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 136becc44d7SSam Leffler int any_count; /* total attached */ 137becc44d7SSam Leffler } route_cb; 138df8bae1dSRodney W. Grimes 139aea8b30fSSam Leffler struct mtx rtsock_mtx; 140aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 141aea8b30fSSam Leffler 142aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 143aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 144aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 145aea8b30fSSam Leffler 146190a4c94SRobert Watson SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 147190a4c94SRobert Watson 148df8bae1dSRodney W. Grimes struct walkarg { 14952041295SPoul-Henning Kamp int w_tmemsize; 15052041295SPoul-Henning Kamp int w_op, w_arg; 15152041295SPoul-Henning Kamp caddr_t w_tmem; 15252041295SPoul-Henning Kamp struct sysctl_req *w_req; 153df8bae1dSRodney W. Grimes }; 154df8bae1dSRodney W. Grimes 155d989c7b3SRobert Watson static void rts_input(struct mbuf *m); 1565dfc91d7SLuigi Rizzo static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 1575dfc91d7SLuigi Rizzo static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 1585dfc91d7SLuigi Rizzo caddr_t cp, struct walkarg *w); 1595dfc91d7SLuigi Rizzo static int rt_xaddrs(caddr_t cp, caddr_t cplim, 1605dfc91d7SLuigi Rizzo struct rt_addrinfo *rtinfo); 161929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 162929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 16305b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 1645dfc91d7SLuigi Rizzo static int route_output(struct mbuf *m, struct socket *so); 1655dfc91d7SLuigi Rizzo static void rt_setmetrics(u_long which, const struct rt_metrics *in, 1665dfc91d7SLuigi Rizzo struct rt_metrics_lite *out); 1675dfc91d7SLuigi Rizzo static void rt_getmetrics(const struct rt_metrics_lite *in, 1685dfc91d7SLuigi Rizzo struct rt_metrics *out); 169*3ca1a2d6SMax Laier static void rt_dispatch(struct mbuf *, sa_family_t); 170df8bae1dSRodney W. Grimes 171d4b5cae4SRobert Watson static struct netisr_handler rtsock_nh = { 172d4b5cae4SRobert Watson .nh_name = "rtsock", 173d4b5cae4SRobert Watson .nh_handler = rts_input, 174d4b5cae4SRobert Watson .nh_proto = NETISR_ROUTE, 175d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 176d4b5cae4SRobert Watson }; 177d4b5cae4SRobert Watson 178d4b5cae4SRobert Watson static int 179d4b5cae4SRobert Watson sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS) 180d4b5cae4SRobert Watson { 181d4b5cae4SRobert Watson int error, qlimit; 182d4b5cae4SRobert Watson 183d4b5cae4SRobert Watson netisr_getqlimit(&rtsock_nh, &qlimit); 184d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qlimit, 0, req); 185d4b5cae4SRobert Watson if (error || !req->newptr) 186d4b5cae4SRobert Watson return (error); 187d4b5cae4SRobert Watson if (qlimit < 1) 188d4b5cae4SRobert Watson return (EINVAL); 189d4b5cae4SRobert Watson return (netisr_setqlimit(&rtsock_nh, qlimit)); 190d4b5cae4SRobert Watson } 191d4b5cae4SRobert Watson SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW, 192d4b5cae4SRobert Watson 0, 0, sysctl_route_netisr_maxqlen, "I", 193d4b5cae4SRobert Watson "maximum routing socket dispatch queue length"); 194d4b5cae4SRobert Watson 195d989c7b3SRobert Watson static void 196d989c7b3SRobert Watson rts_init(void) 197d989c7b3SRobert Watson { 198b062951aSRobert Watson int tmp; 199d989c7b3SRobert Watson 200b062951aSRobert Watson if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 201d4b5cae4SRobert Watson rtsock_nh.nh_qlimit = tmp; 202d4b5cae4SRobert Watson netisr_register(&rtsock_nh); 203d989c7b3SRobert Watson } 204237fdd78SRobert Watson SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0); 205d989c7b3SRobert Watson 206528737fdSBjoern A. Zeeb static int 207528737fdSBjoern A. Zeeb raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src, 208528737fdSBjoern A. Zeeb struct rawcb *rp) 209528737fdSBjoern A. Zeeb { 210528737fdSBjoern A. Zeeb int fibnum; 211528737fdSBjoern A. Zeeb 212528737fdSBjoern A. Zeeb KASSERT(m != NULL, ("%s: m is NULL", __func__)); 213528737fdSBjoern A. Zeeb KASSERT(proto != NULL, ("%s: proto is NULL", __func__)); 214528737fdSBjoern A. Zeeb KASSERT(rp != NULL, ("%s: rp is NULL", __func__)); 215528737fdSBjoern A. Zeeb 216528737fdSBjoern A. Zeeb /* No filtering requested. */ 217528737fdSBjoern A. Zeeb if ((m->m_flags & RTS_FILTER_FIB) == 0) 218528737fdSBjoern A. Zeeb return (0); 219528737fdSBjoern A. Zeeb 220528737fdSBjoern A. Zeeb /* Check if it is a rts and the fib matches the one of the socket. */ 221528737fdSBjoern A. Zeeb fibnum = M_GETFIB(m); 222528737fdSBjoern A. Zeeb if (proto->sp_family != PF_ROUTE || 223528737fdSBjoern A. Zeeb rp->rcb_socket == NULL || 224528737fdSBjoern A. Zeeb rp->rcb_socket->so_fibnum == fibnum) 225528737fdSBjoern A. Zeeb return (0); 226528737fdSBjoern A. Zeeb 227528737fdSBjoern A. Zeeb /* Filtering requested and no match, the socket shall be skipped. */ 228528737fdSBjoern A. Zeeb return (1); 229528737fdSBjoern A. Zeeb } 230528737fdSBjoern A. Zeeb 231d989c7b3SRobert Watson static void 232d989c7b3SRobert Watson rts_input(struct mbuf *m) 233d989c7b3SRobert Watson { 234d989c7b3SRobert Watson struct sockproto route_proto; 235d989c7b3SRobert Watson unsigned short *family; 236d989c7b3SRobert Watson struct m_tag *tag; 237d989c7b3SRobert Watson 238d989c7b3SRobert Watson route_proto.sp_family = PF_ROUTE; 239d989c7b3SRobert Watson tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 240d989c7b3SRobert Watson if (tag != NULL) { 241d989c7b3SRobert Watson family = (unsigned short *)(tag + 1); 242d989c7b3SRobert Watson route_proto.sp_protocol = *family; 243d989c7b3SRobert Watson m_tag_delete(m, tag); 244d989c7b3SRobert Watson } else 245d989c7b3SRobert Watson route_proto.sp_protocol = 0; 246d989c7b3SRobert Watson 247528737fdSBjoern A. Zeeb raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb); 248d989c7b3SRobert Watson } 249d989c7b3SRobert Watson 250a29f300eSGarrett Wollman /* 251a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 252a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 253a29f300eSGarrett Wollman */ 254ac45e92fSRobert Watson static void 255a29f300eSGarrett Wollman rts_abort(struct socket *so) 256df8bae1dSRodney W. Grimes { 2577e994955SRobert Watson 258ac45e92fSRobert Watson raw_usrreqs.pru_abort(so); 259df8bae1dSRodney W. Grimes } 260a29f300eSGarrett Wollman 261a152f8a3SRobert Watson static void 262a152f8a3SRobert Watson rts_close(struct socket *so) 263a152f8a3SRobert Watson { 264a152f8a3SRobert Watson 265a152f8a3SRobert Watson raw_usrreqs.pru_close(so); 266a152f8a3SRobert Watson } 267a152f8a3SRobert Watson 268a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 269a29f300eSGarrett Wollman 270a29f300eSGarrett Wollman static int 271b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 272a29f300eSGarrett Wollman { 273a29f300eSGarrett Wollman struct rawcb *rp; 274a29f300eSGarrett Wollman int s, error; 275a29f300eSGarrett Wollman 276bc725eafSRobert Watson KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 277bc725eafSRobert Watson 2787cc0979fSDavid Malone /* XXX */ 2791ede983cSDag-Erling Smørgrav rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 2805dfc91d7SLuigi Rizzo if (rp == NULL) 281a29f300eSGarrett Wollman return ENOBUFS; 282a29f300eSGarrett Wollman 283a29f300eSGarrett Wollman /* 284a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 285a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 286a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 287a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 288a29f300eSGarrett Wollman * eliminate the spl. 289a29f300eSGarrett Wollman */ 290a29f300eSGarrett Wollman s = splnet(); 291a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 2928b07e49aSJulian Elischer so->so_fibnum = td->td_proc->p_fibnum; 293162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 294a29f300eSGarrett Wollman rp = sotorawcb(so); 295a29f300eSGarrett Wollman if (error) { 296a29f300eSGarrett Wollman splx(s); 2977ba271aeSJonathan Chen so->so_pcb = NULL; 298a29f300eSGarrett Wollman free(rp, M_PCB); 299a29f300eSGarrett Wollman return error; 300a29f300eSGarrett Wollman } 301aea8b30fSSam Leffler RTSOCK_LOCK(); 302a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 303a29f300eSGarrett Wollman case AF_INET: 304df8bae1dSRodney W. Grimes route_cb.ip_count++; 305a29f300eSGarrett Wollman break; 306899ce4f4SYoshinobu Inoue case AF_INET6: 307899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 308899ce4f4SYoshinobu Inoue break; 309a29f300eSGarrett Wollman case AF_IPX: 310cc6a66f2SJulian Elischer route_cb.ipx_count++; 311a29f300eSGarrett Wollman break; 312a29f300eSGarrett Wollman } 313df8bae1dSRodney W. Grimes route_cb.any_count++; 314aea8b30fSSam Leffler RTSOCK_UNLOCK(); 31503e49181SSeigo Tanimura soisconnected(so); 316df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 317df8bae1dSRodney W. Grimes splx(s); 318a29f300eSGarrett Wollman return 0; 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes 321a29f300eSGarrett Wollman static int 322b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 323a29f300eSGarrett Wollman { 3247e994955SRobert Watson 3257e994955SRobert Watson return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 326a29f300eSGarrett Wollman } 327a29f300eSGarrett Wollman 328a29f300eSGarrett Wollman static int 329b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 330a29f300eSGarrett Wollman { 3317e994955SRobert Watson 3327e994955SRobert Watson return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 333a29f300eSGarrett Wollman } 334a29f300eSGarrett Wollman 335a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 336a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 337a29f300eSGarrett Wollman 338bc725eafSRobert Watson static void 339a29f300eSGarrett Wollman rts_detach(struct socket *so) 340a29f300eSGarrett Wollman { 341a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 342a29f300eSGarrett Wollman 343bc725eafSRobert Watson KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 344bc725eafSRobert Watson 345aea8b30fSSam Leffler RTSOCK_LOCK(); 346a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 347a29f300eSGarrett Wollman case AF_INET: 348a29f300eSGarrett Wollman route_cb.ip_count--; 349a29f300eSGarrett Wollman break; 350899ce4f4SYoshinobu Inoue case AF_INET6: 351899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 352899ce4f4SYoshinobu Inoue break; 353a29f300eSGarrett Wollman case AF_IPX: 354a29f300eSGarrett Wollman route_cb.ipx_count--; 355a29f300eSGarrett Wollman break; 356a29f300eSGarrett Wollman } 357a29f300eSGarrett Wollman route_cb.any_count--; 358aea8b30fSSam Leffler RTSOCK_UNLOCK(); 359bc725eafSRobert Watson raw_usrreqs.pru_detach(so); 360a29f300eSGarrett Wollman } 361a29f300eSGarrett Wollman 362a29f300eSGarrett Wollman static int 363a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 364a29f300eSGarrett Wollman { 3657e994955SRobert Watson 3667e994955SRobert Watson return (raw_usrreqs.pru_disconnect(so)); 367a29f300eSGarrett Wollman } 368a29f300eSGarrett Wollman 369a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 370a29f300eSGarrett Wollman 371a29f300eSGarrett Wollman static int 37257bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 373a29f300eSGarrett Wollman { 3747e994955SRobert Watson 3757e994955SRobert Watson return (raw_usrreqs.pru_peeraddr(so, nam)); 376a29f300eSGarrett Wollman } 377a29f300eSGarrett Wollman 378a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 379a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 380a29f300eSGarrett Wollman 381a29f300eSGarrett Wollman static int 38257bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 383b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 384a29f300eSGarrett Wollman { 3857e994955SRobert Watson 3867e994955SRobert Watson return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 387a29f300eSGarrett Wollman } 388a29f300eSGarrett Wollman 389a29f300eSGarrett Wollman /* pru_sense is null */ 390a29f300eSGarrett Wollman 391a29f300eSGarrett Wollman static int 392a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 393a29f300eSGarrett Wollman { 3947e994955SRobert Watson 3957e994955SRobert Watson return (raw_usrreqs.pru_shutdown(so)); 396a29f300eSGarrett Wollman } 397a29f300eSGarrett Wollman 398a29f300eSGarrett Wollman static int 39957bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 400a29f300eSGarrett Wollman { 4017e994955SRobert Watson 4027e994955SRobert Watson return (raw_usrreqs.pru_sockaddr(so, nam)); 403a29f300eSGarrett Wollman } 404a29f300eSGarrett Wollman 405a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 406756d52a1SPoul-Henning Kamp .pru_abort = rts_abort, 407756d52a1SPoul-Henning Kamp .pru_attach = rts_attach, 408756d52a1SPoul-Henning Kamp .pru_bind = rts_bind, 409756d52a1SPoul-Henning Kamp .pru_connect = rts_connect, 410756d52a1SPoul-Henning Kamp .pru_detach = rts_detach, 411756d52a1SPoul-Henning Kamp .pru_disconnect = rts_disconnect, 412756d52a1SPoul-Henning Kamp .pru_peeraddr = rts_peeraddr, 413756d52a1SPoul-Henning Kamp .pru_send = rts_send, 414756d52a1SPoul-Henning Kamp .pru_shutdown = rts_shutdown, 415756d52a1SPoul-Henning Kamp .pru_sockaddr = rts_sockaddr, 416a152f8a3SRobert Watson .pru_close = rts_close, 417a29f300eSGarrett Wollman }; 418a29f300eSGarrett Wollman 419413628a7SBjoern A. Zeeb #ifndef _SOCKADDR_UNION_DEFINED 420413628a7SBjoern A. Zeeb #define _SOCKADDR_UNION_DEFINED 421413628a7SBjoern A. Zeeb /* 422413628a7SBjoern A. Zeeb * The union of all possible address formats we handle. 423413628a7SBjoern A. Zeeb */ 424413628a7SBjoern A. Zeeb union sockaddr_union { 425413628a7SBjoern A. Zeeb struct sockaddr sa; 426413628a7SBjoern A. Zeeb struct sockaddr_in sin; 427413628a7SBjoern A. Zeeb struct sockaddr_in6 sin6; 428413628a7SBjoern A. Zeeb }; 429413628a7SBjoern A. Zeeb #endif /* _SOCKADDR_UNION_DEFINED */ 430413628a7SBjoern A. Zeeb 431413628a7SBjoern A. Zeeb static int 432413628a7SBjoern A. Zeeb rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp, 433413628a7SBjoern A. Zeeb struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred) 434413628a7SBjoern A. Zeeb { 435413628a7SBjoern A. Zeeb 4369c79d243SJamie Gritton /* First, see if the returned address is part of the jail. */ 4379c79d243SJamie Gritton if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) { 4389c79d243SJamie Gritton info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 4399c79d243SJamie Gritton return (0); 4409c79d243SJamie Gritton } 4419c79d243SJamie Gritton 442413628a7SBjoern A. Zeeb switch (info->rti_info[RTAX_DST]->sa_family) { 443413628a7SBjoern A. Zeeb #ifdef INET 444413628a7SBjoern A. Zeeb case AF_INET: 445413628a7SBjoern A. Zeeb { 446413628a7SBjoern A. Zeeb struct in_addr ia; 447413628a7SBjoern A. Zeeb struct ifaddr *ifa; 448413628a7SBjoern A. Zeeb int found; 449413628a7SBjoern A. Zeeb 450413628a7SBjoern A. Zeeb found = 0; 451413628a7SBjoern A. Zeeb /* 4529c79d243SJamie Gritton * Try to find an address on the given outgoing interface 4539c79d243SJamie Gritton * that belongs to the jail. 454413628a7SBjoern A. Zeeb */ 45577ee4c0eSRobert Watson IF_ADDR_LOCK(ifp); 456413628a7SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 457413628a7SBjoern A. Zeeb struct sockaddr *sa; 458413628a7SBjoern A. Zeeb sa = ifa->ifa_addr; 459413628a7SBjoern A. Zeeb if (sa->sa_family != AF_INET) 460413628a7SBjoern A. Zeeb continue; 461413628a7SBjoern A. Zeeb ia = ((struct sockaddr_in *)sa)->sin_addr; 462b89e82ddSJamie Gritton if (prison_check_ip4(cred, &ia) == 0) { 463413628a7SBjoern A. Zeeb found = 1; 464413628a7SBjoern A. Zeeb break; 465413628a7SBjoern A. Zeeb } 466413628a7SBjoern A. Zeeb } 46777ee4c0eSRobert Watson IF_ADDR_UNLOCK(ifp); 468413628a7SBjoern A. Zeeb if (!found) { 469413628a7SBjoern A. Zeeb /* 4709c79d243SJamie Gritton * As a last resort return the 'default' jail address. 471413628a7SBjoern A. Zeeb */ 4720304c731SJamie Gritton ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)-> 4730304c731SJamie Gritton sin_addr; 4741cecba0fSBjoern A. Zeeb if (prison_get_ip4(cred, &ia) != 0) 475413628a7SBjoern A. Zeeb return (ESRCH); 476413628a7SBjoern A. Zeeb } 477413628a7SBjoern A. Zeeb bzero(&saun->sin, sizeof(struct sockaddr_in)); 478413628a7SBjoern A. Zeeb saun->sin.sin_len = sizeof(struct sockaddr_in); 479413628a7SBjoern A. Zeeb saun->sin.sin_family = AF_INET; 480413628a7SBjoern A. Zeeb saun->sin.sin_addr.s_addr = ia.s_addr; 4819c79d243SJamie Gritton info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin; 482413628a7SBjoern A. Zeeb break; 483413628a7SBjoern A. Zeeb } 484413628a7SBjoern A. Zeeb #endif 485413628a7SBjoern A. Zeeb #ifdef INET6 486413628a7SBjoern A. Zeeb case AF_INET6: 487413628a7SBjoern A. Zeeb { 488413628a7SBjoern A. Zeeb struct in6_addr ia6; 489413628a7SBjoern A. Zeeb struct ifaddr *ifa; 490413628a7SBjoern A. Zeeb int found; 491413628a7SBjoern A. Zeeb 492413628a7SBjoern A. Zeeb found = 0; 493413628a7SBjoern A. Zeeb /* 4949c79d243SJamie Gritton * Try to find an address on the given outgoing interface 4959c79d243SJamie Gritton * that belongs to the jail. 496413628a7SBjoern A. Zeeb */ 49777ee4c0eSRobert Watson IF_ADDR_LOCK(ifp); 498413628a7SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 499413628a7SBjoern A. Zeeb struct sockaddr *sa; 500413628a7SBjoern A. Zeeb sa = ifa->ifa_addr; 501413628a7SBjoern A. Zeeb if (sa->sa_family != AF_INET6) 502413628a7SBjoern A. Zeeb continue; 503413628a7SBjoern A. Zeeb bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr, 504413628a7SBjoern A. Zeeb &ia6, sizeof(struct in6_addr)); 505b89e82ddSJamie Gritton if (prison_check_ip6(cred, &ia6) == 0) { 506413628a7SBjoern A. Zeeb found = 1; 507413628a7SBjoern A. Zeeb break; 508413628a7SBjoern A. Zeeb } 509413628a7SBjoern A. Zeeb } 51077ee4c0eSRobert Watson IF_ADDR_UNLOCK(ifp); 511413628a7SBjoern A. Zeeb if (!found) { 512413628a7SBjoern A. Zeeb /* 5139c79d243SJamie Gritton * As a last resort return the 'default' jail address. 514413628a7SBjoern A. Zeeb */ 5150304c731SJamie Gritton ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)-> 5160304c731SJamie Gritton sin6_addr; 5171cecba0fSBjoern A. Zeeb if (prison_get_ip6(cred, &ia6) != 0) 518413628a7SBjoern A. Zeeb return (ESRCH); 519413628a7SBjoern A. Zeeb } 520413628a7SBjoern A. Zeeb bzero(&saun->sin6, sizeof(struct sockaddr_in6)); 521413628a7SBjoern A. Zeeb saun->sin6.sin6_len = sizeof(struct sockaddr_in6); 522413628a7SBjoern A. Zeeb saun->sin6.sin6_family = AF_INET6; 5239c79d243SJamie Gritton bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr)); 524413628a7SBjoern A. Zeeb if (sa6_recoverscope(&saun->sin6) != 0) 525413628a7SBjoern A. Zeeb return (ESRCH); 5269c79d243SJamie Gritton info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6; 527413628a7SBjoern A. Zeeb break; 528413628a7SBjoern A. Zeeb } 529413628a7SBjoern A. Zeeb #endif 530413628a7SBjoern A. Zeeb default: 531413628a7SBjoern A. Zeeb return (ESRCH); 532413628a7SBjoern A. Zeeb } 533413628a7SBjoern A. Zeeb return (0); 534413628a7SBjoern A. Zeeb } 535413628a7SBjoern A. Zeeb 536df8bae1dSRodney W. Grimes /*ARGSUSED*/ 53752041295SPoul-Henning Kamp static int 5385dfc91d7SLuigi Rizzo route_output(struct mbuf *m, struct socket *so) 539df8bae1dSRodney W. Grimes { 540becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 5415dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = NULL; 5425dfc91d7SLuigi Rizzo struct rtentry *rt = NULL; 54378a82810SGarrett Wollman struct radix_node_head *rnh; 544df8bae1dSRodney W. Grimes struct rt_addrinfo info; 545df8bae1dSRodney W. Grimes int len, error = 0; 5465dfc91d7SLuigi Rizzo struct ifnet *ifp = NULL; 547413628a7SBjoern A. Zeeb union sockaddr_union saun; 548*3ca1a2d6SMax Laier sa_family_t saf = AF_UNSPEC; 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 5515dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 5525dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 553df8bae1dSRodney W. Grimes return (ENOBUFS); 554df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 555df8bae1dSRodney W. Grimes panic("route_output"); 556df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 557df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 558df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 5595dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 560df8bae1dSRodney W. Grimes senderr(EINVAL); 561df8bae1dSRodney W. Grimes } 562df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 5635dfc91d7SLuigi Rizzo if (rtm == NULL) { 5645dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 565df8bae1dSRodney W. Grimes senderr(ENOBUFS); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 568df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 5695dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 570df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 571df8bae1dSRodney W. Grimes } 572df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 5738071913dSRuslan Ermilov bzero(&info, sizeof(info)); 574df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 575076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 5765dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 577076d0761SJulian Elischer senderr(EINVAL); 578076d0761SJulian Elischer } 5798071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 5805dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 581becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 5825dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 583becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 584df8bae1dSRodney W. Grimes senderr(EINVAL); 585*3ca1a2d6SMax Laier saf = info.rti_info[RTAX_DST]->sa_family; 586162c0b2eSRuslan Ermilov /* 587162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 588162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 589162c0b2eSRuslan Ermilov */ 590acd3428bSRobert Watson if (rtm->rtm_type != RTM_GET) { 591acd3428bSRobert Watson error = priv_check(curthread, PRIV_NET_ROUTE); 592acd3428bSRobert Watson if (error) 593dadb6c3bSRuslan Ermilov senderr(error); 594acd3428bSRobert Watson } 595162c0b2eSRuslan Ermilov 5969231d35fSQing Li /* 5979231d35fSQing Li * The given gateway address may be an interface address. 5989231d35fSQing Li * For example, issuing a "route change" command on a route 5999231d35fSQing Li * entry that was created from a tunnel, and the gateway 6009231d35fSQing Li * address given is the local end point. In this case the 6019231d35fSQing Li * RTF_GATEWAY flag must be cleared or the destination will 6029231d35fSQing Li * not be reachable even though there is no error message. 6039231d35fSQing Li */ 6049231d35fSQing Li if (info.rti_info[RTAX_GATEWAY] != NULL && 6059231d35fSQing Li info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 6069231d35fSQing Li struct route gw_ro; 6079231d35fSQing Li 6089231d35fSQing Li bzero(&gw_ro, sizeof(gw_ro)); 6099231d35fSQing Li gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY]; 6105311e988SQing Li rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum); 6119231d35fSQing Li /* 6129231d35fSQing Li * A host route through the loopback interface is 6139231d35fSQing Li * installed for each interface adddress. In pre 8.0 6149231d35fSQing Li * releases the interface address of a PPP link type 6159231d35fSQing Li * is not reachable locally. This behavior is fixed as 6169231d35fSQing Li * part of the new L2/L3 redesign and rewrite work. The 6179231d35fSQing Li * signature of this interface address route is the 6189231d35fSQing Li * AF_LINK sa_family type of the rt_gateway, and the 6199231d35fSQing Li * rt_ifp has the IFF_LOOPBACK flag set. 6209231d35fSQing Li */ 6219231d35fSQing Li if (gw_ro.ro_rt != NULL && 6229231d35fSQing Li gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK && 6239231d35fSQing Li gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) 6249231d35fSQing Li info.rti_flags &= ~RTF_GATEWAY; 6259231d35fSQing Li if (gw_ro.ro_rt != NULL) 6269231d35fSQing Li RTFREE(gw_ro.ro_rt); 6279231d35fSQing Li } 6289231d35fSQing Li 629df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 630becc44d7SSam Leffler struct rtentry *saved_nrt; 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes case RTM_ADD: 6335dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 634df8bae1dSRodney W. Grimes senderr(EINVAL); 6355dfc91d7SLuigi Rizzo saved_nrt = NULL; 6368eca593cSQing Li 6376e6b3f7cSQing Li /* support for new ARP code */ 6388eca593cSQing Li if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 6398eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6406e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6416e6b3f7cSQing Li break; 6426e6b3f7cSQing Li } 6438b07e49aSJulian Elischer error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 6448b07e49aSJulian Elischer so->so_fibnum); 645df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 646d1dd20beSSam Leffler RT_LOCK(saved_nrt); 647df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 648df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 64922cafcf0SAndre Oppermann rtm->rtm_index = saved_nrt->rt_ifp->if_index; 6507138d65cSSam Leffler RT_REMREF(saved_nrt); 651d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 652df8bae1dSRodney W. Grimes } 653df8bae1dSRodney W. Grimes break; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes case RTM_DELETE: 6565dfc91d7SLuigi Rizzo saved_nrt = NULL; 6576e6b3f7cSQing Li /* support for new ARP code */ 6586e6b3f7cSQing Li if (info.rti_info[RTAX_GATEWAY] && 6598eca593cSQing Li (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 6608eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6616e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6626e6b3f7cSQing Li break; 6636e6b3f7cSQing Li } 6648b07e49aSJulian Elischer error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 6658b07e49aSJulian Elischer so->so_fibnum); 66678a82810SGarrett Wollman if (error == 0) { 667d1dd20beSSam Leffler RT_LOCK(saved_nrt); 66871eba915SRuslan Ermilov rt = saved_nrt; 66978a82810SGarrett Wollman goto report; 67078a82810SGarrett Wollman } 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes case RTM_GET: 674df8bae1dSRodney W. Grimes case RTM_CHANGE: 675df8bae1dSRodney W. Grimes case RTM_LOCK: 676c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(so->so_fibnum, 677c2c2a7c1SBjoern A. Zeeb info.rti_info[RTAX_DST]->sa_family); 6785dfc91d7SLuigi Rizzo if (rnh == NULL) 67978a82810SGarrett Wollman senderr(EAFNOSUPPORT); 6803120b9d4SKip Macy RADIX_NODE_HEAD_RLOCK(rnh); 681becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 682becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 68379188861SGleb Smirnoff if (rt == NULL) { /* XXX looks bogus */ 6843120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 685df8bae1dSRodney W. Grimes senderr(ESRCH); 68679188861SGleb Smirnoff } 687e440aed9SQing Li #ifdef RADIX_MPATH 688e440aed9SQing Li /* 689e440aed9SQing Li * for RTM_CHANGE/LOCK, if we got multipath routes, 690e440aed9SQing Li * we require users to specify a matching RTAX_GATEWAY. 691e440aed9SQing Li * 692e440aed9SQing Li * for RTM_GET, gate is optional even with multipath. 693e440aed9SQing Li * if gate == NULL the first match is returned. 694e440aed9SQing Li * (no need to call rt_mpath_matchgate if gate == NULL) 695e440aed9SQing Li */ 696e440aed9SQing Li if (rn_mpath_capable(rnh) && 697e440aed9SQing Li (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 698e440aed9SQing Li rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 699e440aed9SQing Li if (!rt) { 7003120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 701e440aed9SQing Li senderr(ESRCH); 702e440aed9SQing Li } 703e440aed9SQing Li } 704e440aed9SQing Li #endif 705c7ab6602SQing Li /* 706c7ab6602SQing Li * If performing proxied L2 entry insertion, and 707c7ab6602SQing Li * the actual PPP host entry is found, perform 708c7ab6602SQing Li * another search to retrieve the prefix route of 709c7ab6602SQing Li * the local end point of the PPP link. 710c7ab6602SQing Li */ 7110ed6142bSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE) { 712c7ab6602SQing Li struct sockaddr laddr; 7130ed6142bSQing Li 7140ed6142bSQing Li if (rt->rt_ifp != NULL && 7150ed6142bSQing Li rt->rt_ifp->if_type == IFT_PROPVIRTUAL) { 7160ed6142bSQing Li struct ifaddr *ifa; 7170ed6142bSQing Li 7180ed6142bSQing Li ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1); 7190ed6142bSQing Li if (ifa != NULL) 7200ed6142bSQing Li rt_maskedcopy(ifa->ifa_addr, 7210ed6142bSQing Li &laddr, 7220ed6142bSQing Li ifa->ifa_netmask); 7230ed6142bSQing Li } else 724c7ab6602SQing Li rt_maskedcopy(rt->rt_ifa->ifa_addr, 725c7ab6602SQing Li &laddr, 726c7ab6602SQing Li rt->rt_ifa->ifa_netmask); 727c7ab6602SQing Li /* 728c7ab6602SQing Li * refactor rt and no lock operation necessary 729c7ab6602SQing Li */ 730c7ab6602SQing Li rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh); 731c7ab6602SQing Li if (rt == NULL) { 732c7ab6602SQing Li RADIX_NODE_HEAD_RUNLOCK(rnh); 733c7ab6602SQing Li senderr(ESRCH); 734c7ab6602SQing Li } 735c7ab6602SQing Li } 736d1dd20beSSam Leffler RT_LOCK(rt); 7377138d65cSSam Leffler RT_ADDREF(rt); 7383120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 739956b0b65SJeffrey Hsu 740ba7be0a9SGeorge V. Neville-Neil /* 741ba7be0a9SGeorge V. Neville-Neil * Fix for PR: 82974 742ba7be0a9SGeorge V. Neville-Neil * 743ba7be0a9SGeorge V. Neville-Neil * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 744ba7be0a9SGeorge V. Neville-Neil * returns a perfect match in case a netmask is 745ba7be0a9SGeorge V. Neville-Neil * specified. For host routes only a longest prefix 746ba7be0a9SGeorge V. Neville-Neil * match is returned so it is necessary to compare the 747ba7be0a9SGeorge V. Neville-Neil * existence of the netmask. If both have a netmask 748ba7be0a9SGeorge V. Neville-Neil * rnh_lookup() did a perfect match and if none of them 749ba7be0a9SGeorge V. Neville-Neil * have a netmask both are host routes which is also a 750ba7be0a9SGeorge V. Neville-Neil * perfect match. 751ba7be0a9SGeorge V. Neville-Neil */ 752ba7be0a9SGeorge V. Neville-Neil 753ba7be0a9SGeorge V. Neville-Neil if (rtm->rtm_type != RTM_GET && 754ba7be0a9SGeorge V. Neville-Neil (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 755ba7be0a9SGeorge V. Neville-Neil RT_UNLOCK(rt); 756ba7be0a9SGeorge V. Neville-Neil senderr(ESRCH); 757ba7be0a9SGeorge V. Neville-Neil } 758ba7be0a9SGeorge V. Neville-Neil 759df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 760df8bae1dSRodney W. Grimes 761df8bae1dSRodney W. Grimes case RTM_GET: 76278a82810SGarrett Wollman report: 763d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 764b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 765de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(curthread->td_ucred) 766b89e82ddSJamie Gritton : prison_if(curthread->td_ucred, 767b89e82ddSJamie Gritton rt_key(rt)) != 0) { 768813dd6aeSBjoern A. Zeeb RT_UNLOCK(rt); 769813dd6aeSBjoern A. Zeeb senderr(ESRCH); 770813dd6aeSBjoern A. Zeeb } 771becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 772becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 773becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 7746e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 775df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 776df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 777df440948SPoul-Henning Kamp if (ifp) { 7789b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 7794a0d6638SRuslan Ermilov ifp->if_addr->ifa_addr; 7809c79d243SJamie Gritton error = rtm_get_jailed(&info, ifp, rt, 7819c79d243SJamie Gritton &saun, curthread->td_ucred); 782413628a7SBjoern A. Zeeb if (error != 0) { 783413628a7SBjoern A. Zeeb RT_UNLOCK(rt); 7849c79d243SJamie Gritton senderr(error); 785413628a7SBjoern A. Zeeb } 78628070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 787becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 788becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 789df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 790df8bae1dSRodney W. Grimes } else { 7915dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 7925dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 793df8bae1dSRodney W. Grimes } 79425029d6cSHartmut Brandt } else if ((ifp = rt->rt_ifp) != NULL) { 79525029d6cSHartmut Brandt rtm->rtm_index = ifp->if_index; 796df8bae1dSRodney W. Grimes } 797913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 798df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 799df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 800df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 8015dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 802d1dd20beSSam Leffler RT_UNLOCK(rt); 803df8bae1dSRodney W. Grimes senderr(ENOBUFS); 804becc44d7SSam Leffler } 8056b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 806df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 807df8bae1dSRodney W. Grimes } 808913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 809df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 81097d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 811df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 812df8bae1dSRodney W. Grimes break; 813df8bae1dSRodney W. Grimes 814df8bae1dSRodney W. Grimes case RTM_CHANGE: 815becc44d7SSam Leffler /* 816becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 817becc44d7SSam Leffler * flags may also be different; ifp may be specified 818becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 819becc44d7SSam Leffler */ 820becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 821becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 822becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 823becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 824becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 825becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 826d1dd20beSSam Leffler RT_UNLOCK(rt); 8279b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 828a5a3926cSAndrew Thompson error = rt_getifa_fib(&info, rt->rt_fibnum); 8298c0fec80SRobert Watson /* 8308c0fec80SRobert Watson * XXXRW: Really we should release this 8318c0fec80SRobert Watson * reference later, but this maintains 8328c0fec80SRobert Watson * historical behavior. 8338c0fec80SRobert Watson */ 8348c0fec80SRobert Watson if (info.rti_ifa != NULL) 8358c0fec80SRobert Watson ifa_free(info.rti_ifa); 8369b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 837a5a3926cSAndrew Thompson if (error != 0) 838a5a3926cSAndrew Thompson senderr(error); 839a11faa9fSGleb Smirnoff RT_LOCK(rt); 840becc44d7SSam Leffler } 8415de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8425de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa && 8435de55821SGleb Smirnoff rt->rt_ifa != NULL && 8449406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest != NULL) { 8459406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 8469406b274SGleb Smirnoff &info); 8471099f828SRobert Watson ifa_free(rt->rt_ifa); 8489406b274SGleb Smirnoff } 8499406b274SGleb Smirnoff if (info.rti_info[RTAX_GATEWAY] != NULL) { 8509b20205dSKip Macy RT_UNLOCK(rt); 8519b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 8529b20205dSKip Macy RT_LOCK(rt); 8539b20205dSKip Macy 8549b20205dSKip Macy error = rt_setgate(rt, rt_key(rt), 8559b20205dSKip Macy info.rti_info[RTAX_GATEWAY]); 8569b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 8579b20205dSKip Macy if (error != 0) { 858d1dd20beSSam Leffler RT_UNLOCK(rt); 8598071913dSRuslan Ermilov senderr(error); 860becc44d7SSam Leffler } 8619231d35fSQing Li rt->rt_flags |= (RTF_GATEWAY & info.rti_flags); 86219fc74fbSJeffrey Hsu } 8635de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8645de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa) { 8651099f828SRobert Watson ifa_ref(info.rti_ifa); 8665de55821SGleb Smirnoff rt->rt_ifa = info.rti_ifa; 8678071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 868df8bae1dSRodney W. Grimes } 86922cafcf0SAndre Oppermann /* Allow some flags to be toggled on change. */ 870427ac07fSKip Macy rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) | 871427ac07fSKip Macy (rtm->rtm_flags & RTF_FMASK); 872df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 873df8bae1dSRodney W. Grimes &rt->rt_rmx); 87422cafcf0SAndre Oppermann rtm->rtm_index = rt->rt_ifp->if_index; 875df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 8768071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 87793b0017fSPhilippe Charnier /* FALLTHROUGH */ 878df8bae1dSRodney W. Grimes case RTM_LOCK: 87997d8d152SAndre Oppermann /* We don't support locks anymore */ 880df8bae1dSRodney W. Grimes break; 881df8bae1dSRodney W. Grimes } 882d1dd20beSSam Leffler RT_UNLOCK(rt); 883df8bae1dSRodney W. Grimes break; 884df8bae1dSRodney W. Grimes 885df8bae1dSRodney W. Grimes default: 886df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 887df8bae1dSRodney W. Grimes } 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes flush: 890df8bae1dSRodney W. Grimes if (rtm) { 891df8bae1dSRodney W. Grimes if (error) 892df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 893df8bae1dSRodney W. Grimes else 894df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 895df8bae1dSRodney W. Grimes } 896becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 897becc44d7SSam Leffler RTFREE(rt); 898df8bae1dSRodney W. Grimes { 8995dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 900df8bae1dSRodney W. Grimes /* 901df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 902df8bae1dSRodney W. Grimes */ 903df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 904df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 905df8bae1dSRodney W. Grimes if (rtm) 906df8bae1dSRodney W. Grimes Free(rtm); 907df8bae1dSRodney W. Grimes m_freem(m); 908df8bae1dSRodney W. Grimes return (error); 909df8bae1dSRodney W. Grimes } 910df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 911df8bae1dSRodney W. Grimes rp = sotorawcb(so); 9124cc20ab1SSeigo Tanimura } 913df8bae1dSRodney W. Grimes if (rtm) { 914df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 91503311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 91603311056SHajimu UMEMOTO m_freem(m); 91703311056SHajimu UMEMOTO m = NULL; 91803311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 91903311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 920df8bae1dSRodney W. Grimes } 921becc44d7SSam Leffler if (m) { 922528737fdSBjoern A. Zeeb M_SETFIB(m, so->so_fibnum); 923528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 924becc44d7SSam Leffler if (rp) { 925becc44d7SSam Leffler /* 926becc44d7SSam Leffler * XXX insure we don't get a copy by 927becc44d7SSam Leffler * invalidating our protocol 928becc44d7SSam Leffler */ 929becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 930becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 931*3ca1a2d6SMax Laier rt_dispatch(m, saf); 932becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 933becc44d7SSam Leffler } else 934*3ca1a2d6SMax Laier rt_dispatch(m, saf); 935becc44d7SSam Leffler } 936826bf287SMax Laier /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */ 937826bf287SMax Laier if (rtm) 938826bf287SMax Laier Free(rtm); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes return (error); 941becc44d7SSam Leffler #undef sa_equal 942df8bae1dSRodney W. Grimes } 943df8bae1dSRodney W. Grimes 94452041295SPoul-Henning Kamp static void 9455dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 9465dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 947df8bae1dSRodney W. Grimes { 948df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 94997d8d152SAndre Oppermann /* 95097d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 95197d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 95297d8d152SAndre Oppermann */ 953df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 954427ac07fSKip Macy metric(RTV_WEIGHT, rmx_weight); 955e27c3f48SOleg Bulyzhin /* Userland -> kernel timebase conversion. */ 956e27c3f48SOleg Bulyzhin if (which & RTV_EXPIRE) 957e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 958e27c3f48SOleg Bulyzhin in->rmx_expire - time_second + time_uptime : 0; 959df8bae1dSRodney W. Grimes #undef metric 960df8bae1dSRodney W. Grimes } 961df8bae1dSRodney W. Grimes 96297d8d152SAndre Oppermann static void 9635dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 96497d8d152SAndre Oppermann { 96597d8d152SAndre Oppermann #define metric(e) out->e = in->e; 96697d8d152SAndre Oppermann bzero(out, sizeof(*out)); 96797d8d152SAndre Oppermann metric(rmx_mtu); 968427ac07fSKip Macy metric(rmx_weight); 969e27c3f48SOleg Bulyzhin /* Kernel -> userland timebase conversion. */ 970e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 971e27c3f48SOleg Bulyzhin in->rmx_expire - time_uptime + time_second : 0; 97297d8d152SAndre Oppermann #undef metric 97397d8d152SAndre Oppermann } 97497d8d152SAndre Oppermann 9757f33a738SJulian Elischer /* 9767f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 9777f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 978076d0761SJulian Elischer * This data is derived straight from userland. 9797f33a738SJulian Elischer */ 980076d0761SJulian Elischer static int 981becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 982df8bae1dSRodney W. Grimes { 983e74642dfSLuigi Rizzo struct sockaddr *sa; 984e74642dfSLuigi Rizzo int i; 985df8bae1dSRodney W. Grimes 986becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 987df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 988df8bae1dSRodney W. Grimes continue; 989ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 9907f33a738SJulian Elischer /* 991076d0761SJulian Elischer * It won't fit. 9927f33a738SJulian Elischer */ 993becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 994076d0761SJulian Elischer return (EINVAL); 9957f33a738SJulian Elischer /* 9967f33a738SJulian Elischer * there are no more.. quit now 9977f33a738SJulian Elischer * If there are more bits, they are in error. 9987f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 9997f33a738SJulian Elischer * This causes kernel to core dump. 1000076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 10017f33a738SJulian Elischer */ 1002076d0761SJulian Elischer if (sa->sa_len == 0) { 1003076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 1004076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 1005df8bae1dSRodney W. Grimes } 1006076d0761SJulian Elischer /* accept it */ 1007076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 1008e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 1009076d0761SJulian Elischer } 1010076d0761SJulian Elischer return (0); 1011df8bae1dSRodney W. Grimes } 1012df8bae1dSRodney W. Grimes 1013df8bae1dSRodney W. Grimes static struct mbuf * 1014becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 1015df8bae1dSRodney W. Grimes { 10165dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 10175dfc91d7SLuigi Rizzo struct mbuf *m; 10185dfc91d7SLuigi Rizzo int i; 10195dfc91d7SLuigi Rizzo struct sockaddr *sa; 1020df8bae1dSRodney W. Grimes int len, dlen; 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes switch (type) { 1023df8bae1dSRodney W. Grimes 1024df8bae1dSRodney W. Grimes case RTM_DELADDR: 1025df8bae1dSRodney W. Grimes case RTM_NEWADDR: 1026df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 1027df8bae1dSRodney W. Grimes break; 1028df8bae1dSRodney W. Grimes 1029477180fbSGarrett Wollman case RTM_DELMADDR: 1030477180fbSGarrett Wollman case RTM_NEWMADDR: 1031477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 1032477180fbSGarrett Wollman break; 1033477180fbSGarrett Wollman 1034df8bae1dSRodney W. Grimes case RTM_IFINFO: 1035df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 1036df8bae1dSRodney W. Grimes break; 1037df8bae1dSRodney W. Grimes 10387b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 1039b83a279fSSam Leffler case RTM_IEEE80211: 10407b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 10417b6edd04SRuslan Ermilov break; 10427b6edd04SRuslan Ermilov 1043df8bae1dSRodney W. Grimes default: 1044df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1045df8bae1dSRodney W. Grimes } 104633841545SHajimu UMEMOTO if (len > MCLBYTES) 1047df8bae1dSRodney W. Grimes panic("rt_msg1"); 1048a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 104933841545SHajimu UMEMOTO if (m && len > MHLEN) { 1050a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 105133841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 105233841545SHajimu UMEMOTO m_free(m); 105333841545SHajimu UMEMOTO m = NULL; 105433841545SHajimu UMEMOTO } 105533841545SHajimu UMEMOTO } 10565dfc91d7SLuigi Rizzo if (m == NULL) 105733841545SHajimu UMEMOTO return (m); 1058df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 10595dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 1060df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1061df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 1062df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 1063df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 1064df8bae1dSRodney W. Grimes continue; 1065df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1066e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1067df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 1068df8bae1dSRodney W. Grimes len += dlen; 1069df8bae1dSRodney W. Grimes } 1070df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 1071df8bae1dSRodney W. Grimes m_freem(m); 1072df8bae1dSRodney W. Grimes return (NULL); 1073df8bae1dSRodney W. Grimes } 1074df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1075df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1076df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1077df8bae1dSRodney W. Grimes return (m); 1078df8bae1dSRodney W. Grimes } 1079df8bae1dSRodney W. Grimes 1080df8bae1dSRodney W. Grimes static int 1081becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 1082df8bae1dSRodney W. Grimes { 10835dfc91d7SLuigi Rizzo int i; 1084df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 1085df8bae1dSRodney W. Grimes caddr_t cp0; 1086df8bae1dSRodney W. Grimes 1087df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 1088df8bae1dSRodney W. Grimes again: 1089df8bae1dSRodney W. Grimes switch (type) { 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes case RTM_DELADDR: 1092df8bae1dSRodney W. Grimes case RTM_NEWADDR: 1093df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 1094df8bae1dSRodney W. Grimes break; 1095df8bae1dSRodney W. Grimes 1096df8bae1dSRodney W. Grimes case RTM_IFINFO: 1097427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1098427a928aSKonstantin Belousov if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1099427a928aSKonstantin Belousov len = sizeof(struct if_msghdr32); 1100427a928aSKonstantin Belousov break; 1101427a928aSKonstantin Belousov } 1102427a928aSKonstantin Belousov #endif 1103df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 1104df8bae1dSRodney W. Grimes break; 1105df8bae1dSRodney W. Grimes 110605b2efe0SBruce M Simpson case RTM_NEWMADDR: 110705b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 110805b2efe0SBruce M Simpson break; 110905b2efe0SBruce M Simpson 1110df8bae1dSRodney W. Grimes default: 1111df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1112df8bae1dSRodney W. Grimes } 1113df440948SPoul-Henning Kamp cp0 = cp; 1114df440948SPoul-Henning Kamp if (cp0) 1115df8bae1dSRodney W. Grimes cp += len; 1116df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 11175dfc91d7SLuigi Rizzo struct sockaddr *sa; 1118df8bae1dSRodney W. Grimes 11195dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 1120df8bae1dSRodney W. Grimes continue; 1121df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1122e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1123df8bae1dSRodney W. Grimes if (cp) { 1124df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 1125df8bae1dSRodney W. Grimes cp += dlen; 1126df8bae1dSRodney W. Grimes } 1127df8bae1dSRodney W. Grimes len += dlen; 1128df8bae1dSRodney W. Grimes } 1129694ff264SAndrew Gallatin len = ALIGN(len); 11305dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 11315dfc91d7SLuigi Rizzo struct walkarg *rw = w; 1132df8bae1dSRodney W. Grimes 113352041295SPoul-Henning Kamp if (rw->w_req) { 1134df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 1135df8bae1dSRodney W. Grimes if (rw->w_tmem) 1136df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 1137df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 1138df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 1139df440948SPoul-Henning Kamp if (rw->w_tmem) 1140df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 1141df8bae1dSRodney W. Grimes } 1142df8bae1dSRodney W. Grimes if (rw->w_tmem) { 1143df8bae1dSRodney W. Grimes cp = rw->w_tmem; 1144df8bae1dSRodney W. Grimes second_time = 1; 1145df8bae1dSRodney W. Grimes goto again; 114652041295SPoul-Henning Kamp } 1147df8bae1dSRodney W. Grimes } 1148df8bae1dSRodney W. Grimes } 1149df8bae1dSRodney W. Grimes if (cp) { 11505dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1151df8bae1dSRodney W. Grimes 1152df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1153df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1154df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1155df8bae1dSRodney W. Grimes } 1156df8bae1dSRodney W. Grimes return (len); 1157df8bae1dSRodney W. Grimes } 1158df8bae1dSRodney W. Grimes 1159df8bae1dSRodney W. Grimes /* 1160df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1161df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 1162df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 1163df8bae1dSRodney W. Grimes * destination. 1164df8bae1dSRodney W. Grimes */ 1165df8bae1dSRodney W. Grimes void 1166528737fdSBjoern A. Zeeb rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1167528737fdSBjoern A. Zeeb int fibnum) 1168df8bae1dSRodney W. Grimes { 1169becc44d7SSam Leffler struct rt_msghdr *rtm; 1170becc44d7SSam Leffler struct mbuf *m; 1171df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1172df8bae1dSRodney W. Grimes 1173df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1174df8bae1dSRodney W. Grimes return; 1175df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 11765dfc91d7SLuigi Rizzo if (m == NULL) 1177df8bae1dSRodney W. Grimes return; 1178528737fdSBjoern A. Zeeb 1179528737fdSBjoern A. Zeeb if (fibnum != RTS_ALLFIBS) { 1180528737fdSBjoern A. Zeeb KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1181528737fdSBjoern A. Zeeb "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1182528737fdSBjoern A. Zeeb M_SETFIB(m, fibnum); 1183528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 1184528737fdSBjoern A. Zeeb } 1185528737fdSBjoern A. Zeeb 1186df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1187df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 1188df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1189df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 1190*3ca1a2d6SMax Laier rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1191df8bae1dSRodney W. Grimes } 1192df8bae1dSRodney W. Grimes 1193528737fdSBjoern A. Zeeb void 1194528737fdSBjoern A. Zeeb rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1195528737fdSBjoern A. Zeeb { 1196528737fdSBjoern A. Zeeb 1197528737fdSBjoern A. Zeeb rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS); 1198528737fdSBjoern A. Zeeb } 1199528737fdSBjoern A. Zeeb 1200df8bae1dSRodney W. Grimes /* 1201df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1202df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 1203df8bae1dSRodney W. Grimes */ 1204df8bae1dSRodney W. Grimes void 1205becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 1206df8bae1dSRodney W. Grimes { 1207becc44d7SSam Leffler struct if_msghdr *ifm; 1208df8bae1dSRodney W. Grimes struct mbuf *m; 1209df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1212df8bae1dSRodney W. Grimes return; 1213df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1214df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 12155dfc91d7SLuigi Rizzo if (m == NULL) 1216df8bae1dSRodney W. Grimes return; 1217df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 1218df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1219292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1220df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1221df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 1222*3ca1a2d6SMax Laier rt_dispatch(m, AF_UNSPEC); 1223df8bae1dSRodney W. Grimes } 1224df8bae1dSRodney W. Grimes 1225df8bae1dSRodney W. Grimes /* 1226df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 1227df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 1228df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 1229df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 1230df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 1231df8bae1dSRodney W. Grimes * copies of it. 1232df8bae1dSRodney W. Grimes */ 1233df8bae1dSRodney W. Grimes void 1234528737fdSBjoern A. Zeeb rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt, 1235528737fdSBjoern A. Zeeb int fibnum) 1236df8bae1dSRodney W. Grimes { 1237df8bae1dSRodney W. Grimes struct rt_addrinfo info; 12385dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 1239df8bae1dSRodney W. Grimes int pass; 12405dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1241df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 1242df8bae1dSRodney W. Grimes 12437a7fa27bSSam Leffler KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 12447a7fa27bSSam Leffler ("unexpected cmd %u", cmd)); 1245c03528b6SBjoern A. Zeeb #if defined(INET) || defined(INET6) 1246f8829a4aSRandall Stewart #ifdef SCTP 1247f8829a4aSRandall Stewart /* 1248f8829a4aSRandall Stewart * notify the SCTP stack 1249f8829a4aSRandall Stewart * this will only get called when an address is added/deleted 1250f8829a4aSRandall Stewart * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1251f8829a4aSRandall Stewart */ 1252f8829a4aSRandall Stewart sctp_addr_change(ifa, cmd); 1253f8829a4aSRandall Stewart #endif /* SCTP */ 1254c03528b6SBjoern A. Zeeb #endif 1255df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1256df8bae1dSRodney W. Grimes return; 1257df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 1258df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1259df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 1260df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 12615dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 1262df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1263df8bae1dSRodney W. Grimes 1264becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 12654a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1266becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1267becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1268df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 1269df8bae1dSRodney W. Grimes continue; 1270df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 1271df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 1272df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1273df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1274df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 1275df8bae1dSRodney W. Grimes } 1276df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 1277df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 12785dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 1279df8bae1dSRodney W. Grimes 12805dfc91d7SLuigi Rizzo if (rt == NULL) 1281df8bae1dSRodney W. Grimes continue; 1282becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1283becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 1284becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1285df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 1286df8bae1dSRodney W. Grimes continue; 1287df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1288df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 1289df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 1290df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1291df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 1292df8bae1dSRodney W. Grimes } 1293528737fdSBjoern A. Zeeb if (fibnum != RTS_ALLFIBS) { 1294528737fdSBjoern A. Zeeb KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: " 1295528737fdSBjoern A. Zeeb "fibnum out of range 0 <= %d < %d", __func__, 1296528737fdSBjoern A. Zeeb fibnum, rt_numfibs)); 1297528737fdSBjoern A. Zeeb M_SETFIB(m, fibnum); 1298528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 1299528737fdSBjoern A. Zeeb } 1300*3ca1a2d6SMax Laier rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1301df8bae1dSRodney W. Grimes } 1302df8bae1dSRodney W. Grimes } 1303df8bae1dSRodney W. Grimes 1304528737fdSBjoern A. Zeeb void 1305528737fdSBjoern A. Zeeb rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1306528737fdSBjoern A. Zeeb { 1307528737fdSBjoern A. Zeeb 1308528737fdSBjoern A. Zeeb rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS); 1309528737fdSBjoern A. Zeeb } 1310528737fdSBjoern A. Zeeb 1311477180fbSGarrett Wollman /* 1312477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 1313477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 1314477180fbSGarrett Wollman * there is no route state to worry about. 1315477180fbSGarrett Wollman */ 1316477180fbSGarrett Wollman void 1317becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1318477180fbSGarrett Wollman { 1319477180fbSGarrett Wollman struct rt_addrinfo info; 13205dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1321477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 1322477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 1323477180fbSGarrett Wollman 1324477180fbSGarrett Wollman if (route_cb.any_count == 0) 1325477180fbSGarrett Wollman return; 1326477180fbSGarrett Wollman 1327477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 1328becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 13294a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 1330477180fbSGarrett Wollman /* 1331477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 1332477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 1333477180fbSGarrett Wollman */ 1334becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1335becc44d7SSam Leffler m = rt_msg1(cmd, &info); 1336becc44d7SSam Leffler if (m == NULL) 1337477180fbSGarrett Wollman return; 1338477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 133975ae0c01SBruce M Simpson KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 134075ae0c01SBruce M Simpson __func__)); 1341477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 1342477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 1343*3ca1a2d6SMax Laier rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC); 1344477180fbSGarrett Wollman } 134552041295SPoul-Henning Kamp 1346b83a279fSSam Leffler static struct mbuf * 1347b83a279fSSam Leffler rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1348b83a279fSSam Leffler struct rt_addrinfo *info) 1349b83a279fSSam Leffler { 1350b83a279fSSam Leffler struct if_announcemsghdr *ifan; 1351b83a279fSSam Leffler struct mbuf *m; 1352b83a279fSSam Leffler 1353b83a279fSSam Leffler if (route_cb.any_count == 0) 1354b83a279fSSam Leffler return NULL; 1355b83a279fSSam Leffler bzero((caddr_t)info, sizeof(*info)); 1356b83a279fSSam Leffler m = rt_msg1(type, info); 1357b83a279fSSam Leffler if (m != NULL) { 1358b83a279fSSam Leffler ifan = mtod(m, struct if_announcemsghdr *); 1359b83a279fSSam Leffler ifan->ifan_index = ifp->if_index; 1360b83a279fSSam Leffler strlcpy(ifan->ifan_name, ifp->if_xname, 1361b83a279fSSam Leffler sizeof(ifan->ifan_name)); 1362b83a279fSSam Leffler ifan->ifan_what = what; 1363b83a279fSSam Leffler } 1364b83a279fSSam Leffler return m; 1365b83a279fSSam Leffler } 1366b83a279fSSam Leffler 1367b83a279fSSam Leffler /* 1368b83a279fSSam Leffler * This is called to generate routing socket messages indicating 1369b83a279fSSam Leffler * IEEE80211 wireless events. 1370b83a279fSSam Leffler * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1371b83a279fSSam Leffler */ 1372b83a279fSSam Leffler void 1373b83a279fSSam Leffler rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1374b83a279fSSam Leffler { 1375b83a279fSSam Leffler struct mbuf *m; 1376b83a279fSSam Leffler struct rt_addrinfo info; 1377b83a279fSSam Leffler 1378b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1379b83a279fSSam Leffler if (m != NULL) { 1380b83a279fSSam Leffler /* 1381b83a279fSSam Leffler * Append the ieee80211 data. Try to stick it in the 1382b83a279fSSam Leffler * mbuf containing the ifannounce msg; otherwise allocate 1383b83a279fSSam Leffler * a new mbuf and append. 1384b83a279fSSam Leffler * 1385b83a279fSSam Leffler * NB: we assume m is a single mbuf. 1386b83a279fSSam Leffler */ 1387b83a279fSSam Leffler if (data_len > M_TRAILINGSPACE(m)) { 1388b83a279fSSam Leffler struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1389b83a279fSSam Leffler if (n == NULL) { 1390b83a279fSSam Leffler m_freem(m); 1391b83a279fSSam Leffler return; 1392b83a279fSSam Leffler } 1393b83a279fSSam Leffler bcopy(data, mtod(n, void *), data_len); 1394b83a279fSSam Leffler n->m_len = data_len; 1395b83a279fSSam Leffler m->m_next = n; 1396b83a279fSSam Leffler } else if (data_len > 0) { 1397b83a279fSSam Leffler bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1398b83a279fSSam Leffler m->m_len += data_len; 1399b83a279fSSam Leffler } 1400b83a279fSSam Leffler if (m->m_flags & M_PKTHDR) 1401b83a279fSSam Leffler m->m_pkthdr.len += data_len; 1402b83a279fSSam Leffler mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1403*3ca1a2d6SMax Laier rt_dispatch(m, AF_UNSPEC); 1404b83a279fSSam Leffler } 1405b83a279fSSam Leffler } 1406b83a279fSSam Leffler 1407df8bae1dSRodney W. Grimes /* 14087b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 14097b6edd04SRuslan Ermilov * network interface arrival and departure. 14107b6edd04SRuslan Ermilov */ 14117b6edd04SRuslan Ermilov void 1412becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 14137b6edd04SRuslan Ermilov { 14147b6edd04SRuslan Ermilov struct mbuf *m; 14157b6edd04SRuslan Ermilov struct rt_addrinfo info; 14167b6edd04SRuslan Ermilov 1417b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1418b83a279fSSam Leffler if (m != NULL) 1419*3ca1a2d6SMax Laier rt_dispatch(m, AF_UNSPEC); 1420becc44d7SSam Leffler } 1421becc44d7SSam Leffler 1422becc44d7SSam Leffler static void 1423*3ca1a2d6SMax Laier rt_dispatch(struct mbuf *m, sa_family_t saf) 1424becc44d7SSam Leffler { 1425d989c7b3SRobert Watson struct m_tag *tag; 1426becc44d7SSam Leffler 1427d989c7b3SRobert Watson /* 1428d989c7b3SRobert Watson * Preserve the family from the sockaddr, if any, in an m_tag for 1429d989c7b3SRobert Watson * use when injecting the mbuf into the routing socket buffer from 1430d989c7b3SRobert Watson * the netisr. 1431d989c7b3SRobert Watson */ 1432*3ca1a2d6SMax Laier if (saf != AF_UNSPEC) { 1433d989c7b3SRobert Watson tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1434d989c7b3SRobert Watson M_NOWAIT); 1435d989c7b3SRobert Watson if (tag == NULL) { 1436d989c7b3SRobert Watson m_freem(m); 1437d989c7b3SRobert Watson return; 1438d989c7b3SRobert Watson } 1439*3ca1a2d6SMax Laier *(unsigned short *)(tag + 1) = saf; 1440d989c7b3SRobert Watson m_tag_prepend(m, tag); 1441d989c7b3SRobert Watson } 144221ca7b57SMarko Zec #ifdef VIMAGE 144321ca7b57SMarko Zec if (V_loif) 144421ca7b57SMarko Zec m->m_pkthdr.rcvif = V_loif; 144521ca7b57SMarko Zec else { 144621ca7b57SMarko Zec m_freem(m); 144721ca7b57SMarko Zec return; 144821ca7b57SMarko Zec } 144921ca7b57SMarko Zec #endif 14503161f583SAndre Oppermann netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 14517b6edd04SRuslan Ermilov } 14527b6edd04SRuslan Ermilov 14537b6edd04SRuslan Ermilov /* 1454df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 1455df8bae1dSRodney W. Grimes */ 145637c84183SPoul-Henning Kamp static int 1457becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 1458df8bae1dSRodney W. Grimes { 1459becc44d7SSam Leffler struct walkarg *w = vw; 1460becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 1461df8bae1dSRodney W. Grimes int error = 0, size; 1462df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1463df8bae1dSRodney W. Grimes 1464df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1465df8bae1dSRodney W. Grimes return 0; 1466b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 1467de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(w->w_req->td->td_ucred) 1468b89e82ddSJamie Gritton : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1469813dd6aeSBjoern A. Zeeb return (0); 1470df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1471becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 1472becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1473becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 14746e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 147528070a0eSRuslan Ermilov if (rt->rt_ifp) { 14764a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1477becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 147828070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1479becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 148028070a0eSRuslan Ermilov } 1481913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 148252041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1483becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1484df8bae1dSRodney W. Grimes 1485df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 1486427ac07fSKip Macy /* 1487427ac07fSKip Macy * let's be honest about this being a retarded hack 1488427ac07fSKip Macy */ 1489427ac07fSKip Macy rtm->rtm_fmask = rt->rt_rmx.rmx_pksent; 149097d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1491df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 1492df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1493df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 149452041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 149552041295SPoul-Henning Kamp return (error); 1496df8bae1dSRodney W. Grimes } 1497df8bae1dSRodney W. Grimes return (error); 1498df8bae1dSRodney W. Grimes } 1499df8bae1dSRodney W. Grimes 1500427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1501427a928aSKonstantin Belousov static void 1502427a928aSKonstantin Belousov copy_ifdata32(struct if_data *src, struct if_data32 *dst) 1503427a928aSKonstantin Belousov { 1504427a928aSKonstantin Belousov 1505427a928aSKonstantin Belousov bzero(dst, sizeof(*dst)); 1506427a928aSKonstantin Belousov CP(*src, *dst, ifi_type); 1507427a928aSKonstantin Belousov CP(*src, *dst, ifi_physical); 1508427a928aSKonstantin Belousov CP(*src, *dst, ifi_addrlen); 1509427a928aSKonstantin Belousov CP(*src, *dst, ifi_hdrlen); 1510427a928aSKonstantin Belousov CP(*src, *dst, ifi_link_state); 151104f32057SKonstantin Belousov dst->ifi_datalen = sizeof(struct if_data32); 1512427a928aSKonstantin Belousov CP(*src, *dst, ifi_mtu); 1513427a928aSKonstantin Belousov CP(*src, *dst, ifi_metric); 1514427a928aSKonstantin Belousov CP(*src, *dst, ifi_baudrate); 1515427a928aSKonstantin Belousov CP(*src, *dst, ifi_ipackets); 1516427a928aSKonstantin Belousov CP(*src, *dst, ifi_ierrors); 1517427a928aSKonstantin Belousov CP(*src, *dst, ifi_opackets); 1518427a928aSKonstantin Belousov CP(*src, *dst, ifi_oerrors); 1519427a928aSKonstantin Belousov CP(*src, *dst, ifi_collisions); 1520427a928aSKonstantin Belousov CP(*src, *dst, ifi_ibytes); 1521427a928aSKonstantin Belousov CP(*src, *dst, ifi_obytes); 1522427a928aSKonstantin Belousov CP(*src, *dst, ifi_imcasts); 1523427a928aSKonstantin Belousov CP(*src, *dst, ifi_omcasts); 1524427a928aSKonstantin Belousov CP(*src, *dst, ifi_iqdrops); 1525427a928aSKonstantin Belousov CP(*src, *dst, ifi_noproto); 1526427a928aSKonstantin Belousov CP(*src, *dst, ifi_hwassist); 1527427a928aSKonstantin Belousov CP(*src, *dst, ifi_epoch); 1528427a928aSKonstantin Belousov TV_CP(*src, *dst, ifi_lastchange); 1529427a928aSKonstantin Belousov } 1530427a928aSKonstantin Belousov #endif 1531427a928aSKonstantin Belousov 153237c84183SPoul-Henning Kamp static int 1533becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 1534df8bae1dSRodney W. Grimes { 1535becc44d7SSam Leffler struct ifnet *ifp; 1536becc44d7SSam Leffler struct ifaddr *ifa; 1537df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1538df8bae1dSRodney W. Grimes int len, error = 0; 1539df8bae1dSRodney W. Grimes 1540df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1541fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1542603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1543df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 1544df8bae1dSRodney W. Grimes continue; 1545ee7c7feeSBjoern A. Zeeb IF_ADDR_LOCK(ifp); 15464a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1547becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1548913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 15495dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 155052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1551becc44d7SSam Leffler struct if_msghdr *ifm; 1552df8bae1dSRodney W. Grimes 1553427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1554427a928aSKonstantin Belousov if (w->w_req->flags & SCTL_MASK32) { 1555427a928aSKonstantin Belousov struct if_msghdr32 *ifm32; 1556427a928aSKonstantin Belousov 1557427a928aSKonstantin Belousov ifm32 = (struct if_msghdr32 *)w->w_tmem; 1558427a928aSKonstantin Belousov ifm32->ifm_index = ifp->if_index; 1559427a928aSKonstantin Belousov ifm32->ifm_flags = ifp->if_flags | 1560427a928aSKonstantin Belousov ifp->if_drv_flags; 1561427a928aSKonstantin Belousov copy_ifdata32(&ifp->if_data, &ifm32->ifm_data); 1562427a928aSKonstantin Belousov ifm32->ifm_addrs = info.rti_addrs; 1563427a928aSKonstantin Belousov error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32, 1564427a928aSKonstantin Belousov len); 1565427a928aSKonstantin Belousov goto sysctl_out; 1566427a928aSKonstantin Belousov } 1567427a928aSKonstantin Belousov #endif 1568df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 1569df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1570292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1571df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1572df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 157352041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len); 1574427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1575427a928aSKonstantin Belousov sysctl_out: 1576427a928aSKonstantin Belousov #endif 1577df440948SPoul-Henning Kamp if (error) 1578a35b06c5SJonathan Lemon goto done; 1579df8bae1dSRodney W. Grimes } 15805dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1581df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 1582df8bae1dSRodney W. Grimes continue; 1583b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1584b89e82ddSJamie Gritton ifa->ifa_addr) != 0) 158575c13541SPoul-Henning Kamp continue; 1586becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1587becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1588becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1589913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 159052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1591becc44d7SSam Leffler struct ifa_msghdr *ifam; 1592df8bae1dSRodney W. Grimes 1593df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1594df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1595df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1596df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1597df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 159852041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1599df440948SPoul-Henning Kamp if (error) 1600a35b06c5SJonathan Lemon goto done; 1601df8bae1dSRodney W. Grimes } 1602df8bae1dSRodney W. Grimes } 1603ee7c7feeSBjoern A. Zeeb IF_ADDR_UNLOCK(ifp); 1604becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 16055dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1606df8bae1dSRodney W. Grimes } 1607a35b06c5SJonathan Lemon done: 1608ee7c7feeSBjoern A. Zeeb if (ifp != NULL) 1609ee7c7feeSBjoern A. Zeeb IF_ADDR_UNLOCK(ifp); 1610fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 1611a35b06c5SJonathan Lemon return (error); 1612df8bae1dSRodney W. Grimes } 1613df8bae1dSRodney W. Grimes 1614ebda3fc3SBjoern A. Zeeb static int 16159b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 161605b2efe0SBruce M Simpson { 16179b98ee2cSLuigi Rizzo struct ifnet *ifp; 161805b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 161905b2efe0SBruce M Simpson struct rt_addrinfo info; 162005b2efe0SBruce M Simpson int len, error = 0; 16219b98ee2cSLuigi Rizzo struct ifaddr *ifa; 162205b2efe0SBruce M Simpson 162305b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 1624fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1625603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 162605b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 162705b2efe0SBruce M Simpson continue; 16284a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1629913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1630fe0fc7efSChristian S.J. Peron IF_ADDR_LOCK(ifp); 163105b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 163205b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 163305b2efe0SBruce M Simpson continue; 1634b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1635b89e82ddSJamie Gritton ifma->ifma_addr) != 0) 163605b2efe0SBruce M Simpson continue; 163705b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1638913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1639913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1640913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1641913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 164205b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 16439b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 164405b2efe0SBruce M Simpson 164505b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 164605b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 164705b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 164805b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 164905b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1650fe0fc7efSChristian S.J. Peron if (error) { 1651fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 165205b2efe0SBruce M Simpson goto done; 165305b2efe0SBruce M Simpson } 165405b2efe0SBruce M Simpson } 165505b2efe0SBruce M Simpson } 1656fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 1657fe0fc7efSChristian S.J. Peron } 165805b2efe0SBruce M Simpson done: 1659fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 166005b2efe0SBruce M Simpson return (error); 166105b2efe0SBruce M Simpson } 166205b2efe0SBruce M Simpson 166352041295SPoul-Henning Kamp static int 166482d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1665df8bae1dSRodney W. Grimes { 166652041295SPoul-Henning Kamp int *name = (int *)arg1; 166752041295SPoul-Henning Kamp u_int namelen = arg2; 1668c2c2a7c1SBjoern A. Zeeb struct radix_node_head *rnh = NULL; /* silence compiler. */ 1669fe0fc7efSChristian S.J. Peron int i, lim, error = EINVAL; 1670df8bae1dSRodney W. Grimes u_char af; 1671df8bae1dSRodney W. Grimes struct walkarg w; 1672df8bae1dSRodney W. Grimes 167352041295SPoul-Henning Kamp name ++; 167452041295SPoul-Henning Kamp namelen--; 167552041295SPoul-Henning Kamp if (req->newptr) 1676df8bae1dSRodney W. Grimes return (EPERM); 1677df8bae1dSRodney W. Grimes if (namelen != 3) 1678f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1679df8bae1dSRodney W. Grimes af = name[0]; 1680b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1681b2aaf46eSJeffrey Hsu return (EINVAL); 16826b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1683df8bae1dSRodney W. Grimes w.w_op = name[1]; 1684df8bae1dSRodney W. Grimes w.w_arg = name[2]; 168552041295SPoul-Henning Kamp w.w_req = req; 1686df8bae1dSRodney W. Grimes 1687fe0fc7efSChristian S.J. Peron error = sysctl_wire_old_buffer(req, 0); 1688fe0fc7efSChristian S.J. Peron if (error) 1689fe0fc7efSChristian S.J. Peron return (error); 1690df8bae1dSRodney W. Grimes switch (w.w_op) { 1691df8bae1dSRodney W. Grimes 1692df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1693df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1694a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1695a8b76c8fSLuigi Rizzo i = 1; 1696a8b76c8fSLuigi Rizzo lim = AF_MAX; 1697a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1698a8b76c8fSLuigi Rizzo i = lim = af; 16998eca593cSQing Li 17008eca593cSQing Li /* 17018eca593cSQing Li * take care of llinfo entries, the caller must 17028eca593cSQing Li * specify an AF 17038eca593cSQing Li */ 170414981d80SQing Li if (w.w_op == NET_RT_FLAGS && 170582b334e8SQing Li (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 17068eca593cSQing Li if (af != 0) 17078eca593cSQing Li error = lltable_sysctl_dumparp(af, w.w_req); 17088eca593cSQing Li else 17098eca593cSQing Li error = EINVAL; 17108eca593cSQing Li break; 17118eca593cSQing Li } 17128eca593cSQing Li /* 17138eca593cSQing Li * take care of routing entries 17148eca593cSQing Li */ 171520b0cdb7SBjoern A. Zeeb for (error = 0; error == 0 && i <= lim; i++) { 1716c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i); 1717c2c2a7c1SBjoern A. Zeeb if (rnh != NULL) { 1718fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_LOCK(rnh); 1719956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 1720fe0fc7efSChristian S.J. Peron sysctl_dumpentry, &w); 1721fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_UNLOCK(rnh); 1722a8b76c8fSLuigi Rizzo } else if (af != 0) 1723956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 172420b0cdb7SBjoern A. Zeeb } 1725df8bae1dSRodney W. Grimes break; 1726df8bae1dSRodney W. Grimes 1727df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1728df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 172905b2efe0SBruce M Simpson break; 173005b2efe0SBruce M Simpson 173105b2efe0SBruce M Simpson case NET_RT_IFMALIST: 173205b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 173305b2efe0SBruce M Simpson break; 1734df8bae1dSRodney W. Grimes } 1735df8bae1dSRodney W. Grimes if (w.w_tmem) 1736df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1737df8bae1dSRodney W. Grimes return (error); 1738df8bae1dSRodney W. Grimes } 1739df8bae1dSRodney W. Grimes 174052041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 174152041295SPoul-Henning Kamp 1742df8bae1dSRodney W. Grimes /* 1743df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1744df8bae1dSRodney W. Grimes */ 1745df8bae1dSRodney W. Grimes 17465b1c0294SDavid E. O'Brien static struct domain routedomain; /* or at least forward */ 1747df8bae1dSRodney W. Grimes 174852041295SPoul-Henning Kamp static struct protosw routesw[] = { 1749303989a2SRuslan Ermilov { 1750303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 1751303989a2SRuslan Ermilov .pr_domain = &routedomain, 1752303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1753303989a2SRuslan Ermilov .pr_output = route_output, 1754303989a2SRuslan Ermilov .pr_ctlinput = raw_ctlinput, 1755303989a2SRuslan Ermilov .pr_init = raw_init, 1756303989a2SRuslan Ermilov .pr_usrreqs = &route_usrreqs 1757df8bae1dSRodney W. Grimes } 1758df8bae1dSRodney W. Grimes }; 1759df8bae1dSRodney W. Grimes 1760303989a2SRuslan Ermilov static struct domain routedomain = { 1761303989a2SRuslan Ermilov .dom_family = PF_ROUTE, 1762303989a2SRuslan Ermilov .dom_name = "route", 1763303989a2SRuslan Ermilov .dom_protosw = routesw, 1764303989a2SRuslan Ermilov .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1765303989a2SRuslan Ermilov }; 176678a82810SGarrett Wollman 1767d0728d71SRobert Watson VNET_DOMAIN_SET(route); 1768