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 125*528737fdSBjoern A. Zeeb /* 126*528737fdSBjoern A. Zeeb * Used by rtsock/raw_input callback code to decide whether to filter the update 127*528737fdSBjoern A. Zeeb * notification to a socket bound to a particular FIB. 128*528737fdSBjoern A. Zeeb */ 129*528737fdSBjoern A. Zeeb #define RTS_FILTER_FIB M_PROTO8 130*528737fdSBjoern A. Zeeb #define RTS_ALLFIBS -1 131*528737fdSBjoern 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); 1695dfc91d7SLuigi Rizzo static void rt_dispatch(struct mbuf *, const struct sockaddr *); 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 206*528737fdSBjoern A. Zeeb static int 207*528737fdSBjoern A. Zeeb raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src, 208*528737fdSBjoern A. Zeeb struct rawcb *rp) 209*528737fdSBjoern A. Zeeb { 210*528737fdSBjoern A. Zeeb int fibnum; 211*528737fdSBjoern A. Zeeb 212*528737fdSBjoern A. Zeeb KASSERT(m != NULL, ("%s: m is NULL", __func__)); 213*528737fdSBjoern A. Zeeb KASSERT(proto != NULL, ("%s: proto is NULL", __func__)); 214*528737fdSBjoern A. Zeeb KASSERT(rp != NULL, ("%s: rp is NULL", __func__)); 215*528737fdSBjoern A. Zeeb 216*528737fdSBjoern A. Zeeb /* No filtering requested. */ 217*528737fdSBjoern A. Zeeb if ((m->m_flags & RTS_FILTER_FIB) == 0) 218*528737fdSBjoern A. Zeeb return (0); 219*528737fdSBjoern A. Zeeb 220*528737fdSBjoern A. Zeeb /* Check if it is a rts and the fib matches the one of the socket. */ 221*528737fdSBjoern A. Zeeb fibnum = M_GETFIB(m); 222*528737fdSBjoern A. Zeeb if (proto->sp_family != PF_ROUTE || 223*528737fdSBjoern A. Zeeb rp->rcb_socket == NULL || 224*528737fdSBjoern A. Zeeb rp->rcb_socket->so_fibnum == fibnum) 225*528737fdSBjoern A. Zeeb return (0); 226*528737fdSBjoern A. Zeeb 227*528737fdSBjoern A. Zeeb /* Filtering requested and no match, the socket shall be skipped. */ 228*528737fdSBjoern A. Zeeb return (1); 229*528737fdSBjoern A. Zeeb } 230*528737fdSBjoern 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 247*528737fdSBjoern 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; 548df8bae1dSRodney W. Grimes 549df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 5505dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 5515dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 552df8bae1dSRodney W. Grimes return (ENOBUFS); 553df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 554df8bae1dSRodney W. Grimes panic("route_output"); 555df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 556df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 557df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 5585dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 559df8bae1dSRodney W. Grimes senderr(EINVAL); 560df8bae1dSRodney W. Grimes } 561df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 5625dfc91d7SLuigi Rizzo if (rtm == NULL) { 5635dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 564df8bae1dSRodney W. Grimes senderr(ENOBUFS); 565df8bae1dSRodney W. Grimes } 566df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 567df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 5685dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 569df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 5728071913dSRuslan Ermilov bzero(&info, sizeof(info)); 573df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 574076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 5755dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 576076d0761SJulian Elischer senderr(EINVAL); 577076d0761SJulian Elischer } 5788071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 5795dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 580becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 5815dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 582becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 583df8bae1dSRodney W. Grimes senderr(EINVAL); 584162c0b2eSRuslan Ermilov /* 585162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 586162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 587162c0b2eSRuslan Ermilov */ 588acd3428bSRobert Watson if (rtm->rtm_type != RTM_GET) { 589acd3428bSRobert Watson error = priv_check(curthread, PRIV_NET_ROUTE); 590acd3428bSRobert Watson if (error) 591dadb6c3bSRuslan Ermilov senderr(error); 592acd3428bSRobert Watson } 593162c0b2eSRuslan Ermilov 5949231d35fSQing Li /* 5959231d35fSQing Li * The given gateway address may be an interface address. 5969231d35fSQing Li * For example, issuing a "route change" command on a route 5979231d35fSQing Li * entry that was created from a tunnel, and the gateway 5989231d35fSQing Li * address given is the local end point. In this case the 5999231d35fSQing Li * RTF_GATEWAY flag must be cleared or the destination will 6009231d35fSQing Li * not be reachable even though there is no error message. 6019231d35fSQing Li */ 6029231d35fSQing Li if (info.rti_info[RTAX_GATEWAY] != NULL && 6039231d35fSQing Li info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 6049231d35fSQing Li struct route gw_ro; 6059231d35fSQing Li 6069231d35fSQing Li bzero(&gw_ro, sizeof(gw_ro)); 6079231d35fSQing Li gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY]; 6085311e988SQing Li rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum); 6099231d35fSQing Li /* 6109231d35fSQing Li * A host route through the loopback interface is 6119231d35fSQing Li * installed for each interface adddress. In pre 8.0 6129231d35fSQing Li * releases the interface address of a PPP link type 6139231d35fSQing Li * is not reachable locally. This behavior is fixed as 6149231d35fSQing Li * part of the new L2/L3 redesign and rewrite work. The 6159231d35fSQing Li * signature of this interface address route is the 6169231d35fSQing Li * AF_LINK sa_family type of the rt_gateway, and the 6179231d35fSQing Li * rt_ifp has the IFF_LOOPBACK flag set. 6189231d35fSQing Li */ 6199231d35fSQing Li if (gw_ro.ro_rt != NULL && 6209231d35fSQing Li gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK && 6219231d35fSQing Li gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) 6229231d35fSQing Li info.rti_flags &= ~RTF_GATEWAY; 6239231d35fSQing Li if (gw_ro.ro_rt != NULL) 6249231d35fSQing Li RTFREE(gw_ro.ro_rt); 6259231d35fSQing Li } 6269231d35fSQing Li 627df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 628becc44d7SSam Leffler struct rtentry *saved_nrt; 629df8bae1dSRodney W. Grimes 630df8bae1dSRodney W. Grimes case RTM_ADD: 6315dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 632df8bae1dSRodney W. Grimes senderr(EINVAL); 6335dfc91d7SLuigi Rizzo saved_nrt = NULL; 6348eca593cSQing Li 6356e6b3f7cSQing Li /* support for new ARP code */ 6368eca593cSQing Li if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 6378eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6386e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6396e6b3f7cSQing Li break; 6406e6b3f7cSQing Li } 6418b07e49aSJulian Elischer error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 6428b07e49aSJulian Elischer so->so_fibnum); 643df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 644d1dd20beSSam Leffler RT_LOCK(saved_nrt); 645df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 646df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 64722cafcf0SAndre Oppermann rtm->rtm_index = saved_nrt->rt_ifp->if_index; 6487138d65cSSam Leffler RT_REMREF(saved_nrt); 649d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 650df8bae1dSRodney W. Grimes } 651df8bae1dSRodney W. Grimes break; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes case RTM_DELETE: 6545dfc91d7SLuigi Rizzo saved_nrt = NULL; 6556e6b3f7cSQing Li /* support for new ARP code */ 6566e6b3f7cSQing Li if (info.rti_info[RTAX_GATEWAY] && 6578eca593cSQing Li (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 6588eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6596e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6606e6b3f7cSQing Li break; 6616e6b3f7cSQing Li } 6628b07e49aSJulian Elischer error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 6638b07e49aSJulian Elischer so->so_fibnum); 66478a82810SGarrett Wollman if (error == 0) { 665d1dd20beSSam Leffler RT_LOCK(saved_nrt); 66671eba915SRuslan Ermilov rt = saved_nrt; 66778a82810SGarrett Wollman goto report; 66878a82810SGarrett Wollman } 669df8bae1dSRodney W. Grimes break; 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes case RTM_GET: 672df8bae1dSRodney W. Grimes case RTM_CHANGE: 673df8bae1dSRodney W. Grimes case RTM_LOCK: 674c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(so->so_fibnum, 675c2c2a7c1SBjoern A. Zeeb info.rti_info[RTAX_DST]->sa_family); 6765dfc91d7SLuigi Rizzo if (rnh == NULL) 67778a82810SGarrett Wollman senderr(EAFNOSUPPORT); 6783120b9d4SKip Macy RADIX_NODE_HEAD_RLOCK(rnh); 679becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 680becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 68179188861SGleb Smirnoff if (rt == NULL) { /* XXX looks bogus */ 6823120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 683df8bae1dSRodney W. Grimes senderr(ESRCH); 68479188861SGleb Smirnoff } 685e440aed9SQing Li #ifdef RADIX_MPATH 686e440aed9SQing Li /* 687e440aed9SQing Li * for RTM_CHANGE/LOCK, if we got multipath routes, 688e440aed9SQing Li * we require users to specify a matching RTAX_GATEWAY. 689e440aed9SQing Li * 690e440aed9SQing Li * for RTM_GET, gate is optional even with multipath. 691e440aed9SQing Li * if gate == NULL the first match is returned. 692e440aed9SQing Li * (no need to call rt_mpath_matchgate if gate == NULL) 693e440aed9SQing Li */ 694e440aed9SQing Li if (rn_mpath_capable(rnh) && 695e440aed9SQing Li (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 696e440aed9SQing Li rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 697e440aed9SQing Li if (!rt) { 6983120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 699e440aed9SQing Li senderr(ESRCH); 700e440aed9SQing Li } 701e440aed9SQing Li } 702e440aed9SQing Li #endif 703c7ab6602SQing Li /* 704c7ab6602SQing Li * If performing proxied L2 entry insertion, and 705c7ab6602SQing Li * the actual PPP host entry is found, perform 706c7ab6602SQing Li * another search to retrieve the prefix route of 707c7ab6602SQing Li * the local end point of the PPP link. 708c7ab6602SQing Li */ 7090ed6142bSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE) { 710c7ab6602SQing Li struct sockaddr laddr; 7110ed6142bSQing Li 7120ed6142bSQing Li if (rt->rt_ifp != NULL && 7130ed6142bSQing Li rt->rt_ifp->if_type == IFT_PROPVIRTUAL) { 7140ed6142bSQing Li struct ifaddr *ifa; 7150ed6142bSQing Li 7160ed6142bSQing Li ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1); 7170ed6142bSQing Li if (ifa != NULL) 7180ed6142bSQing Li rt_maskedcopy(ifa->ifa_addr, 7190ed6142bSQing Li &laddr, 7200ed6142bSQing Li ifa->ifa_netmask); 7210ed6142bSQing Li } else 722c7ab6602SQing Li rt_maskedcopy(rt->rt_ifa->ifa_addr, 723c7ab6602SQing Li &laddr, 724c7ab6602SQing Li rt->rt_ifa->ifa_netmask); 725c7ab6602SQing Li /* 726c7ab6602SQing Li * refactor rt and no lock operation necessary 727c7ab6602SQing Li */ 728c7ab6602SQing Li rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh); 729c7ab6602SQing Li if (rt == NULL) { 730c7ab6602SQing Li RADIX_NODE_HEAD_RUNLOCK(rnh); 731c7ab6602SQing Li senderr(ESRCH); 732c7ab6602SQing Li } 733c7ab6602SQing Li } 734d1dd20beSSam Leffler RT_LOCK(rt); 7357138d65cSSam Leffler RT_ADDREF(rt); 7363120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 737956b0b65SJeffrey Hsu 738ba7be0a9SGeorge V. Neville-Neil /* 739ba7be0a9SGeorge V. Neville-Neil * Fix for PR: 82974 740ba7be0a9SGeorge V. Neville-Neil * 741ba7be0a9SGeorge V. Neville-Neil * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 742ba7be0a9SGeorge V. Neville-Neil * returns a perfect match in case a netmask is 743ba7be0a9SGeorge V. Neville-Neil * specified. For host routes only a longest prefix 744ba7be0a9SGeorge V. Neville-Neil * match is returned so it is necessary to compare the 745ba7be0a9SGeorge V. Neville-Neil * existence of the netmask. If both have a netmask 746ba7be0a9SGeorge V. Neville-Neil * rnh_lookup() did a perfect match and if none of them 747ba7be0a9SGeorge V. Neville-Neil * have a netmask both are host routes which is also a 748ba7be0a9SGeorge V. Neville-Neil * perfect match. 749ba7be0a9SGeorge V. Neville-Neil */ 750ba7be0a9SGeorge V. Neville-Neil 751ba7be0a9SGeorge V. Neville-Neil if (rtm->rtm_type != RTM_GET && 752ba7be0a9SGeorge V. Neville-Neil (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 753ba7be0a9SGeorge V. Neville-Neil RT_UNLOCK(rt); 754ba7be0a9SGeorge V. Neville-Neil senderr(ESRCH); 755ba7be0a9SGeorge V. Neville-Neil } 756ba7be0a9SGeorge V. Neville-Neil 757df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes case RTM_GET: 76078a82810SGarrett Wollman report: 761d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 762b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 763de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(curthread->td_ucred) 764b89e82ddSJamie Gritton : prison_if(curthread->td_ucred, 765b89e82ddSJamie Gritton rt_key(rt)) != 0) { 766813dd6aeSBjoern A. Zeeb RT_UNLOCK(rt); 767813dd6aeSBjoern A. Zeeb senderr(ESRCH); 768813dd6aeSBjoern A. Zeeb } 769becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 770becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 771becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 7726e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 773df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 774df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 775df440948SPoul-Henning Kamp if (ifp) { 7769b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 7774a0d6638SRuslan Ermilov ifp->if_addr->ifa_addr; 7789c79d243SJamie Gritton error = rtm_get_jailed(&info, ifp, rt, 7799c79d243SJamie Gritton &saun, curthread->td_ucred); 780413628a7SBjoern A. Zeeb if (error != 0) { 781413628a7SBjoern A. Zeeb RT_UNLOCK(rt); 7829c79d243SJamie Gritton senderr(error); 783413628a7SBjoern A. Zeeb } 78428070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 785becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 786becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 787df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 788df8bae1dSRodney W. Grimes } else { 7895dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 7905dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 791df8bae1dSRodney W. Grimes } 79225029d6cSHartmut Brandt } else if ((ifp = rt->rt_ifp) != NULL) { 79325029d6cSHartmut Brandt rtm->rtm_index = ifp->if_index; 794df8bae1dSRodney W. Grimes } 795913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 796df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 797df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 798df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 7995dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 800d1dd20beSSam Leffler RT_UNLOCK(rt); 801df8bae1dSRodney W. Grimes senderr(ENOBUFS); 802becc44d7SSam Leffler } 8036b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 804df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 805df8bae1dSRodney W. Grimes } 806913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 807df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 80897d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 809df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 810df8bae1dSRodney W. Grimes break; 811df8bae1dSRodney W. Grimes 812df8bae1dSRodney W. Grimes case RTM_CHANGE: 813becc44d7SSam Leffler /* 814becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 815becc44d7SSam Leffler * flags may also be different; ifp may be specified 816becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 817becc44d7SSam Leffler */ 818becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 819becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 820becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 821becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 822becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 823becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 824d1dd20beSSam Leffler RT_UNLOCK(rt); 8259b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 826a5a3926cSAndrew Thompson error = rt_getifa_fib(&info, rt->rt_fibnum); 8278c0fec80SRobert Watson /* 8288c0fec80SRobert Watson * XXXRW: Really we should release this 8298c0fec80SRobert Watson * reference later, but this maintains 8308c0fec80SRobert Watson * historical behavior. 8318c0fec80SRobert Watson */ 8328c0fec80SRobert Watson if (info.rti_ifa != NULL) 8338c0fec80SRobert Watson ifa_free(info.rti_ifa); 8349b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 835a5a3926cSAndrew Thompson if (error != 0) 836a5a3926cSAndrew Thompson senderr(error); 837a11faa9fSGleb Smirnoff RT_LOCK(rt); 838becc44d7SSam Leffler } 8395de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8405de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa && 8415de55821SGleb Smirnoff rt->rt_ifa != NULL && 8429406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest != NULL) { 8439406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 8449406b274SGleb Smirnoff &info); 8451099f828SRobert Watson ifa_free(rt->rt_ifa); 8469406b274SGleb Smirnoff } 8479406b274SGleb Smirnoff if (info.rti_info[RTAX_GATEWAY] != NULL) { 8489b20205dSKip Macy RT_UNLOCK(rt); 8499b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 8509b20205dSKip Macy RT_LOCK(rt); 8519b20205dSKip Macy 8529b20205dSKip Macy error = rt_setgate(rt, rt_key(rt), 8539b20205dSKip Macy info.rti_info[RTAX_GATEWAY]); 8549b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 8559b20205dSKip Macy if (error != 0) { 856d1dd20beSSam Leffler RT_UNLOCK(rt); 8578071913dSRuslan Ermilov senderr(error); 858becc44d7SSam Leffler } 8599231d35fSQing Li rt->rt_flags |= (RTF_GATEWAY & info.rti_flags); 86019fc74fbSJeffrey Hsu } 8615de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8625de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa) { 8631099f828SRobert Watson ifa_ref(info.rti_ifa); 8645de55821SGleb Smirnoff rt->rt_ifa = info.rti_ifa; 8658071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 866df8bae1dSRodney W. Grimes } 86722cafcf0SAndre Oppermann /* Allow some flags to be toggled on change. */ 868427ac07fSKip Macy rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) | 869427ac07fSKip Macy (rtm->rtm_flags & RTF_FMASK); 870df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 871df8bae1dSRodney W. Grimes &rt->rt_rmx); 87222cafcf0SAndre Oppermann rtm->rtm_index = rt->rt_ifp->if_index; 873df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 8748071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 87593b0017fSPhilippe Charnier /* FALLTHROUGH */ 876df8bae1dSRodney W. Grimes case RTM_LOCK: 87797d8d152SAndre Oppermann /* We don't support locks anymore */ 878df8bae1dSRodney W. Grimes break; 879df8bae1dSRodney W. Grimes } 880d1dd20beSSam Leffler RT_UNLOCK(rt); 881df8bae1dSRodney W. Grimes break; 882df8bae1dSRodney W. Grimes 883df8bae1dSRodney W. Grimes default: 884df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 885df8bae1dSRodney W. Grimes } 886df8bae1dSRodney W. Grimes 887df8bae1dSRodney W. Grimes flush: 888df8bae1dSRodney W. Grimes if (rtm) { 889df8bae1dSRodney W. Grimes if (error) 890df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 891df8bae1dSRodney W. Grimes else 892df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 893df8bae1dSRodney W. Grimes } 894becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 895becc44d7SSam Leffler RTFREE(rt); 896df8bae1dSRodney W. Grimes { 8975dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 898df8bae1dSRodney W. Grimes /* 899df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 900df8bae1dSRodney W. Grimes */ 901df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 902df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 903df8bae1dSRodney W. Grimes if (rtm) 904df8bae1dSRodney W. Grimes Free(rtm); 905df8bae1dSRodney W. Grimes m_freem(m); 906df8bae1dSRodney W. Grimes return (error); 907df8bae1dSRodney W. Grimes } 908df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 909df8bae1dSRodney W. Grimes rp = sotorawcb(so); 9104cc20ab1SSeigo Tanimura } 911df8bae1dSRodney W. Grimes if (rtm) { 912df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 91303311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 91403311056SHajimu UMEMOTO m_freem(m); 91503311056SHajimu UMEMOTO m = NULL; 91603311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 91703311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 918df8bae1dSRodney W. Grimes } 919becc44d7SSam Leffler if (m) { 920*528737fdSBjoern A. Zeeb M_SETFIB(m, so->so_fibnum); 921*528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 922becc44d7SSam Leffler if (rp) { 923becc44d7SSam Leffler /* 924becc44d7SSam Leffler * XXX insure we don't get a copy by 925becc44d7SSam Leffler * invalidating our protocol 926becc44d7SSam Leffler */ 927becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 928becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 929becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 930becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 931becc44d7SSam Leffler } else 932becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 933becc44d7SSam Leffler } 934826bf287SMax Laier /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */ 935826bf287SMax Laier if (rtm) 936826bf287SMax Laier Free(rtm); 937df8bae1dSRodney W. Grimes } 938df8bae1dSRodney W. Grimes return (error); 939becc44d7SSam Leffler #undef sa_equal 940df8bae1dSRodney W. Grimes } 941df8bae1dSRodney W. Grimes 94252041295SPoul-Henning Kamp static void 9435dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 9445dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 945df8bae1dSRodney W. Grimes { 946df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 94797d8d152SAndre Oppermann /* 94897d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 94997d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 95097d8d152SAndre Oppermann */ 951df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 952427ac07fSKip Macy metric(RTV_WEIGHT, rmx_weight); 953e27c3f48SOleg Bulyzhin /* Userland -> kernel timebase conversion. */ 954e27c3f48SOleg Bulyzhin if (which & RTV_EXPIRE) 955e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 956e27c3f48SOleg Bulyzhin in->rmx_expire - time_second + time_uptime : 0; 957df8bae1dSRodney W. Grimes #undef metric 958df8bae1dSRodney W. Grimes } 959df8bae1dSRodney W. Grimes 96097d8d152SAndre Oppermann static void 9615dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 96297d8d152SAndre Oppermann { 96397d8d152SAndre Oppermann #define metric(e) out->e = in->e; 96497d8d152SAndre Oppermann bzero(out, sizeof(*out)); 96597d8d152SAndre Oppermann metric(rmx_mtu); 966427ac07fSKip Macy metric(rmx_weight); 967e27c3f48SOleg Bulyzhin /* Kernel -> userland timebase conversion. */ 968e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 969e27c3f48SOleg Bulyzhin in->rmx_expire - time_uptime + time_second : 0; 97097d8d152SAndre Oppermann #undef metric 97197d8d152SAndre Oppermann } 97297d8d152SAndre Oppermann 9737f33a738SJulian Elischer /* 9747f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 9757f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 976076d0761SJulian Elischer * This data is derived straight from userland. 9777f33a738SJulian Elischer */ 978076d0761SJulian Elischer static int 979becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 980df8bae1dSRodney W. Grimes { 981e74642dfSLuigi Rizzo struct sockaddr *sa; 982e74642dfSLuigi Rizzo int i; 983df8bae1dSRodney W. Grimes 984becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 985df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 986df8bae1dSRodney W. Grimes continue; 987ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 9887f33a738SJulian Elischer /* 989076d0761SJulian Elischer * It won't fit. 9907f33a738SJulian Elischer */ 991becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 992076d0761SJulian Elischer return (EINVAL); 9937f33a738SJulian Elischer /* 9947f33a738SJulian Elischer * there are no more.. quit now 9957f33a738SJulian Elischer * If there are more bits, they are in error. 9967f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 9977f33a738SJulian Elischer * This causes kernel to core dump. 998076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 9997f33a738SJulian Elischer */ 1000076d0761SJulian Elischer if (sa->sa_len == 0) { 1001076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 1002076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 1003df8bae1dSRodney W. Grimes } 1004076d0761SJulian Elischer /* accept it */ 1005076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 1006e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 1007076d0761SJulian Elischer } 1008076d0761SJulian Elischer return (0); 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes static struct mbuf * 1012becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 1013df8bae1dSRodney W. Grimes { 10145dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 10155dfc91d7SLuigi Rizzo struct mbuf *m; 10165dfc91d7SLuigi Rizzo int i; 10175dfc91d7SLuigi Rizzo struct sockaddr *sa; 1018df8bae1dSRodney W. Grimes int len, dlen; 1019df8bae1dSRodney W. Grimes 1020df8bae1dSRodney W. Grimes switch (type) { 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes case RTM_DELADDR: 1023df8bae1dSRodney W. Grimes case RTM_NEWADDR: 1024df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 1025df8bae1dSRodney W. Grimes break; 1026df8bae1dSRodney W. Grimes 1027477180fbSGarrett Wollman case RTM_DELMADDR: 1028477180fbSGarrett Wollman case RTM_NEWMADDR: 1029477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 1030477180fbSGarrett Wollman break; 1031477180fbSGarrett Wollman 1032df8bae1dSRodney W. Grimes case RTM_IFINFO: 1033df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 1034df8bae1dSRodney W. Grimes break; 1035df8bae1dSRodney W. Grimes 10367b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 1037b83a279fSSam Leffler case RTM_IEEE80211: 10387b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 10397b6edd04SRuslan Ermilov break; 10407b6edd04SRuslan Ermilov 1041df8bae1dSRodney W. Grimes default: 1042df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1043df8bae1dSRodney W. Grimes } 104433841545SHajimu UMEMOTO if (len > MCLBYTES) 1045df8bae1dSRodney W. Grimes panic("rt_msg1"); 1046a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 104733841545SHajimu UMEMOTO if (m && len > MHLEN) { 1048a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 104933841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 105033841545SHajimu UMEMOTO m_free(m); 105133841545SHajimu UMEMOTO m = NULL; 105233841545SHajimu UMEMOTO } 105333841545SHajimu UMEMOTO } 10545dfc91d7SLuigi Rizzo if (m == NULL) 105533841545SHajimu UMEMOTO return (m); 1056df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 10575dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 1058df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1059df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 1060df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 1061df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 1062df8bae1dSRodney W. Grimes continue; 1063df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1064e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1065df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 1066df8bae1dSRodney W. Grimes len += dlen; 1067df8bae1dSRodney W. Grimes } 1068df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 1069df8bae1dSRodney W. Grimes m_freem(m); 1070df8bae1dSRodney W. Grimes return (NULL); 1071df8bae1dSRodney W. Grimes } 1072df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1073df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1074df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1075df8bae1dSRodney W. Grimes return (m); 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes 1078df8bae1dSRodney W. Grimes static int 1079becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 1080df8bae1dSRodney W. Grimes { 10815dfc91d7SLuigi Rizzo int i; 1082df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 1083df8bae1dSRodney W. Grimes caddr_t cp0; 1084df8bae1dSRodney W. Grimes 1085df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 1086df8bae1dSRodney W. Grimes again: 1087df8bae1dSRodney W. Grimes switch (type) { 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes case RTM_DELADDR: 1090df8bae1dSRodney W. Grimes case RTM_NEWADDR: 1091df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 1092df8bae1dSRodney W. Grimes break; 1093df8bae1dSRodney W. Grimes 1094df8bae1dSRodney W. Grimes case RTM_IFINFO: 1095427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1096427a928aSKonstantin Belousov if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1097427a928aSKonstantin Belousov len = sizeof(struct if_msghdr32); 1098427a928aSKonstantin Belousov break; 1099427a928aSKonstantin Belousov } 1100427a928aSKonstantin Belousov #endif 1101df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 1102df8bae1dSRodney W. Grimes break; 1103df8bae1dSRodney W. Grimes 110405b2efe0SBruce M Simpson case RTM_NEWMADDR: 110505b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 110605b2efe0SBruce M Simpson break; 110705b2efe0SBruce M Simpson 1108df8bae1dSRodney W. Grimes default: 1109df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1110df8bae1dSRodney W. Grimes } 1111df440948SPoul-Henning Kamp cp0 = cp; 1112df440948SPoul-Henning Kamp if (cp0) 1113df8bae1dSRodney W. Grimes cp += len; 1114df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 11155dfc91d7SLuigi Rizzo struct sockaddr *sa; 1116df8bae1dSRodney W. Grimes 11175dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 1118df8bae1dSRodney W. Grimes continue; 1119df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1120e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1121df8bae1dSRodney W. Grimes if (cp) { 1122df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 1123df8bae1dSRodney W. Grimes cp += dlen; 1124df8bae1dSRodney W. Grimes } 1125df8bae1dSRodney W. Grimes len += dlen; 1126df8bae1dSRodney W. Grimes } 1127694ff264SAndrew Gallatin len = ALIGN(len); 11285dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 11295dfc91d7SLuigi Rizzo struct walkarg *rw = w; 1130df8bae1dSRodney W. Grimes 113152041295SPoul-Henning Kamp if (rw->w_req) { 1132df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 1133df8bae1dSRodney W. Grimes if (rw->w_tmem) 1134df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 1135df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 1136df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 1137df440948SPoul-Henning Kamp if (rw->w_tmem) 1138df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes if (rw->w_tmem) { 1141df8bae1dSRodney W. Grimes cp = rw->w_tmem; 1142df8bae1dSRodney W. Grimes second_time = 1; 1143df8bae1dSRodney W. Grimes goto again; 114452041295SPoul-Henning Kamp } 1145df8bae1dSRodney W. Grimes } 1146df8bae1dSRodney W. Grimes } 1147df8bae1dSRodney W. Grimes if (cp) { 11485dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1149df8bae1dSRodney W. Grimes 1150df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1151df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1152df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1153df8bae1dSRodney W. Grimes } 1154df8bae1dSRodney W. Grimes return (len); 1155df8bae1dSRodney W. Grimes } 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes /* 1158df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1159df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 1160df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 1161df8bae1dSRodney W. Grimes * destination. 1162df8bae1dSRodney W. Grimes */ 1163df8bae1dSRodney W. Grimes void 1164*528737fdSBjoern A. Zeeb rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1165*528737fdSBjoern A. Zeeb int fibnum) 1166df8bae1dSRodney W. Grimes { 1167becc44d7SSam Leffler struct rt_msghdr *rtm; 1168becc44d7SSam Leffler struct mbuf *m; 1169df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1170df8bae1dSRodney W. Grimes 1171df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1172df8bae1dSRodney W. Grimes return; 1173df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 11745dfc91d7SLuigi Rizzo if (m == NULL) 1175df8bae1dSRodney W. Grimes return; 1176*528737fdSBjoern A. Zeeb 1177*528737fdSBjoern A. Zeeb if (fibnum != RTS_ALLFIBS) { 1178*528737fdSBjoern A. Zeeb KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1179*528737fdSBjoern A. Zeeb "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1180*528737fdSBjoern A. Zeeb M_SETFIB(m, fibnum); 1181*528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 1182*528737fdSBjoern A. Zeeb } 1183*528737fdSBjoern A. Zeeb 1184df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1185df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 1186df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1187df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 1188becc44d7SSam Leffler rt_dispatch(m, sa); 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes 1191*528737fdSBjoern A. Zeeb void 1192*528737fdSBjoern A. Zeeb rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1193*528737fdSBjoern A. Zeeb { 1194*528737fdSBjoern A. Zeeb 1195*528737fdSBjoern A. Zeeb rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS); 1196*528737fdSBjoern A. Zeeb } 1197*528737fdSBjoern A. Zeeb 1198df8bae1dSRodney W. Grimes /* 1199df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1200df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 1201df8bae1dSRodney W. Grimes */ 1202df8bae1dSRodney W. Grimes void 1203becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 1204df8bae1dSRodney W. Grimes { 1205becc44d7SSam Leffler struct if_msghdr *ifm; 1206df8bae1dSRodney W. Grimes struct mbuf *m; 1207df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1210df8bae1dSRodney W. Grimes return; 1211df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1212df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 12135dfc91d7SLuigi Rizzo if (m == NULL) 1214df8bae1dSRodney W. Grimes return; 1215df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 1216df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1217292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1218df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1219df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 1220becc44d7SSam Leffler rt_dispatch(m, NULL); 1221df8bae1dSRodney W. Grimes } 1222df8bae1dSRodney W. Grimes 1223df8bae1dSRodney W. Grimes /* 1224df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 1225df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 1226df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 1227df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 1228df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 1229df8bae1dSRodney W. Grimes * copies of it. 1230df8bae1dSRodney W. Grimes */ 1231df8bae1dSRodney W. Grimes void 1232*528737fdSBjoern A. Zeeb rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt, 1233*528737fdSBjoern A. Zeeb int fibnum) 1234df8bae1dSRodney W. Grimes { 1235df8bae1dSRodney W. Grimes struct rt_addrinfo info; 12365dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 1237df8bae1dSRodney W. Grimes int pass; 12385dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1239df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 1240df8bae1dSRodney W. Grimes 12417a7fa27bSSam Leffler KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 12427a7fa27bSSam Leffler ("unexpected cmd %u", cmd)); 1243c03528b6SBjoern A. Zeeb #if defined(INET) || defined(INET6) 1244f8829a4aSRandall Stewart #ifdef SCTP 1245f8829a4aSRandall Stewart /* 1246f8829a4aSRandall Stewart * notify the SCTP stack 1247f8829a4aSRandall Stewart * this will only get called when an address is added/deleted 1248f8829a4aSRandall Stewart * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1249f8829a4aSRandall Stewart */ 1250f8829a4aSRandall Stewart sctp_addr_change(ifa, cmd); 1251f8829a4aSRandall Stewart #endif /* SCTP */ 1252c03528b6SBjoern A. Zeeb #endif 1253df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1254df8bae1dSRodney W. Grimes return; 1255df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 1256df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1257df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 1258df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 12595dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 1260df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1261df8bae1dSRodney W. Grimes 1262becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 12634a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1264becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1265becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1266df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 1267df8bae1dSRodney W. Grimes continue; 1268df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 1269df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 1270df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1271df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1272df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 1273df8bae1dSRodney W. Grimes } 1274df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 1275df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 12765dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 1277df8bae1dSRodney W. Grimes 12785dfc91d7SLuigi Rizzo if (rt == NULL) 1279df8bae1dSRodney W. Grimes continue; 1280becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1281becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 1282becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1283df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 1284df8bae1dSRodney W. Grimes continue; 1285df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1286df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 1287df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 1288df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1289df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 1290df8bae1dSRodney W. Grimes } 1291*528737fdSBjoern A. Zeeb if (fibnum != RTS_ALLFIBS) { 1292*528737fdSBjoern A. Zeeb KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: " 1293*528737fdSBjoern A. Zeeb "fibnum out of range 0 <= %d < %d", __func__, 1294*528737fdSBjoern A. Zeeb fibnum, rt_numfibs)); 1295*528737fdSBjoern A. Zeeb M_SETFIB(m, fibnum); 1296*528737fdSBjoern A. Zeeb m->m_flags |= RTS_FILTER_FIB; 1297*528737fdSBjoern A. Zeeb } 1298becc44d7SSam Leffler rt_dispatch(m, sa); 1299df8bae1dSRodney W. Grimes } 1300df8bae1dSRodney W. Grimes } 1301df8bae1dSRodney W. Grimes 1302*528737fdSBjoern A. Zeeb void 1303*528737fdSBjoern A. Zeeb rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1304*528737fdSBjoern A. Zeeb { 1305*528737fdSBjoern A. Zeeb 1306*528737fdSBjoern A. Zeeb rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS); 1307*528737fdSBjoern A. Zeeb } 1308*528737fdSBjoern A. Zeeb 1309477180fbSGarrett Wollman /* 1310477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 1311477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 1312477180fbSGarrett Wollman * there is no route state to worry about. 1313477180fbSGarrett Wollman */ 1314477180fbSGarrett Wollman void 1315becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1316477180fbSGarrett Wollman { 1317477180fbSGarrett Wollman struct rt_addrinfo info; 13185dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1319477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 1320477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 1321477180fbSGarrett Wollman 1322477180fbSGarrett Wollman if (route_cb.any_count == 0) 1323477180fbSGarrett Wollman return; 1324477180fbSGarrett Wollman 1325477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 1326becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 13274a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 1328477180fbSGarrett Wollman /* 1329477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 1330477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 1331477180fbSGarrett Wollman */ 1332becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1333becc44d7SSam Leffler m = rt_msg1(cmd, &info); 1334becc44d7SSam Leffler if (m == NULL) 1335477180fbSGarrett Wollman return; 1336477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 133775ae0c01SBruce M Simpson KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 133875ae0c01SBruce M Simpson __func__)); 1339477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 1340477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 1341becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 1342477180fbSGarrett Wollman } 134352041295SPoul-Henning Kamp 1344b83a279fSSam Leffler static struct mbuf * 1345b83a279fSSam Leffler rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1346b83a279fSSam Leffler struct rt_addrinfo *info) 1347b83a279fSSam Leffler { 1348b83a279fSSam Leffler struct if_announcemsghdr *ifan; 1349b83a279fSSam Leffler struct mbuf *m; 1350b83a279fSSam Leffler 1351b83a279fSSam Leffler if (route_cb.any_count == 0) 1352b83a279fSSam Leffler return NULL; 1353b83a279fSSam Leffler bzero((caddr_t)info, sizeof(*info)); 1354b83a279fSSam Leffler m = rt_msg1(type, info); 1355b83a279fSSam Leffler if (m != NULL) { 1356b83a279fSSam Leffler ifan = mtod(m, struct if_announcemsghdr *); 1357b83a279fSSam Leffler ifan->ifan_index = ifp->if_index; 1358b83a279fSSam Leffler strlcpy(ifan->ifan_name, ifp->if_xname, 1359b83a279fSSam Leffler sizeof(ifan->ifan_name)); 1360b83a279fSSam Leffler ifan->ifan_what = what; 1361b83a279fSSam Leffler } 1362b83a279fSSam Leffler return m; 1363b83a279fSSam Leffler } 1364b83a279fSSam Leffler 1365b83a279fSSam Leffler /* 1366b83a279fSSam Leffler * This is called to generate routing socket messages indicating 1367b83a279fSSam Leffler * IEEE80211 wireless events. 1368b83a279fSSam Leffler * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1369b83a279fSSam Leffler */ 1370b83a279fSSam Leffler void 1371b83a279fSSam Leffler rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1372b83a279fSSam Leffler { 1373b83a279fSSam Leffler struct mbuf *m; 1374b83a279fSSam Leffler struct rt_addrinfo info; 1375b83a279fSSam Leffler 1376b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1377b83a279fSSam Leffler if (m != NULL) { 1378b83a279fSSam Leffler /* 1379b83a279fSSam Leffler * Append the ieee80211 data. Try to stick it in the 1380b83a279fSSam Leffler * mbuf containing the ifannounce msg; otherwise allocate 1381b83a279fSSam Leffler * a new mbuf and append. 1382b83a279fSSam Leffler * 1383b83a279fSSam Leffler * NB: we assume m is a single mbuf. 1384b83a279fSSam Leffler */ 1385b83a279fSSam Leffler if (data_len > M_TRAILINGSPACE(m)) { 1386b83a279fSSam Leffler struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1387b83a279fSSam Leffler if (n == NULL) { 1388b83a279fSSam Leffler m_freem(m); 1389b83a279fSSam Leffler return; 1390b83a279fSSam Leffler } 1391b83a279fSSam Leffler bcopy(data, mtod(n, void *), data_len); 1392b83a279fSSam Leffler n->m_len = data_len; 1393b83a279fSSam Leffler m->m_next = n; 1394b83a279fSSam Leffler } else if (data_len > 0) { 1395b83a279fSSam Leffler bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1396b83a279fSSam Leffler m->m_len += data_len; 1397b83a279fSSam Leffler } 1398b83a279fSSam Leffler if (m->m_flags & M_PKTHDR) 1399b83a279fSSam Leffler m->m_pkthdr.len += data_len; 1400b83a279fSSam Leffler mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1401b83a279fSSam Leffler rt_dispatch(m, NULL); 1402b83a279fSSam Leffler } 1403b83a279fSSam Leffler } 1404b83a279fSSam Leffler 1405df8bae1dSRodney W. Grimes /* 14067b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 14077b6edd04SRuslan Ermilov * network interface arrival and departure. 14087b6edd04SRuslan Ermilov */ 14097b6edd04SRuslan Ermilov void 1410becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 14117b6edd04SRuslan Ermilov { 14127b6edd04SRuslan Ermilov struct mbuf *m; 14137b6edd04SRuslan Ermilov struct rt_addrinfo info; 14147b6edd04SRuslan Ermilov 1415b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1416b83a279fSSam Leffler if (m != NULL) 1417becc44d7SSam Leffler rt_dispatch(m, NULL); 1418becc44d7SSam Leffler } 1419becc44d7SSam Leffler 1420becc44d7SSam Leffler static void 14215dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 1422becc44d7SSam Leffler { 1423d989c7b3SRobert Watson struct m_tag *tag; 1424becc44d7SSam Leffler 1425d989c7b3SRobert Watson /* 1426d989c7b3SRobert Watson * Preserve the family from the sockaddr, if any, in an m_tag for 1427d989c7b3SRobert Watson * use when injecting the mbuf into the routing socket buffer from 1428d989c7b3SRobert Watson * the netisr. 1429d989c7b3SRobert Watson */ 1430d989c7b3SRobert Watson if (sa != NULL) { 1431d989c7b3SRobert Watson tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1432d989c7b3SRobert Watson M_NOWAIT); 1433d989c7b3SRobert Watson if (tag == NULL) { 1434d989c7b3SRobert Watson m_freem(m); 1435d989c7b3SRobert Watson return; 1436d989c7b3SRobert Watson } 14378d78bea4SSam Leffler *(unsigned short *)(tag + 1) = sa->sa_family; 1438d989c7b3SRobert Watson m_tag_prepend(m, tag); 1439d989c7b3SRobert Watson } 144021ca7b57SMarko Zec #ifdef VIMAGE 144121ca7b57SMarko Zec if (V_loif) 144221ca7b57SMarko Zec m->m_pkthdr.rcvif = V_loif; 144321ca7b57SMarko Zec else { 144421ca7b57SMarko Zec m_freem(m); 144521ca7b57SMarko Zec return; 144621ca7b57SMarko Zec } 144721ca7b57SMarko Zec #endif 14483161f583SAndre Oppermann netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 14497b6edd04SRuslan Ermilov } 14507b6edd04SRuslan Ermilov 14517b6edd04SRuslan Ermilov /* 1452df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 1453df8bae1dSRodney W. Grimes */ 145437c84183SPoul-Henning Kamp static int 1455becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 1456df8bae1dSRodney W. Grimes { 1457becc44d7SSam Leffler struct walkarg *w = vw; 1458becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 1459df8bae1dSRodney W. Grimes int error = 0, size; 1460df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1461df8bae1dSRodney W. Grimes 1462df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1463df8bae1dSRodney W. Grimes return 0; 1464b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 1465de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(w->w_req->td->td_ucred) 1466b89e82ddSJamie Gritton : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1467813dd6aeSBjoern A. Zeeb return (0); 1468df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1469becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 1470becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1471becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 14726e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 147328070a0eSRuslan Ermilov if (rt->rt_ifp) { 14744a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1475becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 147628070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1477becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 147828070a0eSRuslan Ermilov } 1479913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 148052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1481becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1482df8bae1dSRodney W. Grimes 1483df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 1484427ac07fSKip Macy /* 1485427ac07fSKip Macy * let's be honest about this being a retarded hack 1486427ac07fSKip Macy */ 1487427ac07fSKip Macy rtm->rtm_fmask = rt->rt_rmx.rmx_pksent; 148897d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1489df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 1490df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1491df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 149252041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 149352041295SPoul-Henning Kamp return (error); 1494df8bae1dSRodney W. Grimes } 1495df8bae1dSRodney W. Grimes return (error); 1496df8bae1dSRodney W. Grimes } 1497df8bae1dSRodney W. Grimes 1498427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1499427a928aSKonstantin Belousov static void 1500427a928aSKonstantin Belousov copy_ifdata32(struct if_data *src, struct if_data32 *dst) 1501427a928aSKonstantin Belousov { 1502427a928aSKonstantin Belousov 1503427a928aSKonstantin Belousov bzero(dst, sizeof(*dst)); 1504427a928aSKonstantin Belousov CP(*src, *dst, ifi_type); 1505427a928aSKonstantin Belousov CP(*src, *dst, ifi_physical); 1506427a928aSKonstantin Belousov CP(*src, *dst, ifi_addrlen); 1507427a928aSKonstantin Belousov CP(*src, *dst, ifi_hdrlen); 1508427a928aSKonstantin Belousov CP(*src, *dst, ifi_link_state); 150904f32057SKonstantin Belousov dst->ifi_datalen = sizeof(struct if_data32); 1510427a928aSKonstantin Belousov CP(*src, *dst, ifi_mtu); 1511427a928aSKonstantin Belousov CP(*src, *dst, ifi_metric); 1512427a928aSKonstantin Belousov CP(*src, *dst, ifi_baudrate); 1513427a928aSKonstantin Belousov CP(*src, *dst, ifi_ipackets); 1514427a928aSKonstantin Belousov CP(*src, *dst, ifi_ierrors); 1515427a928aSKonstantin Belousov CP(*src, *dst, ifi_opackets); 1516427a928aSKonstantin Belousov CP(*src, *dst, ifi_oerrors); 1517427a928aSKonstantin Belousov CP(*src, *dst, ifi_collisions); 1518427a928aSKonstantin Belousov CP(*src, *dst, ifi_ibytes); 1519427a928aSKonstantin Belousov CP(*src, *dst, ifi_obytes); 1520427a928aSKonstantin Belousov CP(*src, *dst, ifi_imcasts); 1521427a928aSKonstantin Belousov CP(*src, *dst, ifi_omcasts); 1522427a928aSKonstantin Belousov CP(*src, *dst, ifi_iqdrops); 1523427a928aSKonstantin Belousov CP(*src, *dst, ifi_noproto); 1524427a928aSKonstantin Belousov CP(*src, *dst, ifi_hwassist); 1525427a928aSKonstantin Belousov CP(*src, *dst, ifi_epoch); 1526427a928aSKonstantin Belousov TV_CP(*src, *dst, ifi_lastchange); 1527427a928aSKonstantin Belousov } 1528427a928aSKonstantin Belousov #endif 1529427a928aSKonstantin Belousov 153037c84183SPoul-Henning Kamp static int 1531becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 1532df8bae1dSRodney W. Grimes { 1533becc44d7SSam Leffler struct ifnet *ifp; 1534becc44d7SSam Leffler struct ifaddr *ifa; 1535df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1536df8bae1dSRodney W. Grimes int len, error = 0; 1537df8bae1dSRodney W. Grimes 1538df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1539fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1540603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1541df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 1542df8bae1dSRodney W. Grimes continue; 1543ee7c7feeSBjoern A. Zeeb IF_ADDR_LOCK(ifp); 15444a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1545becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1546913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 15475dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 154852041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1549becc44d7SSam Leffler struct if_msghdr *ifm; 1550df8bae1dSRodney W. Grimes 1551427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1552427a928aSKonstantin Belousov if (w->w_req->flags & SCTL_MASK32) { 1553427a928aSKonstantin Belousov struct if_msghdr32 *ifm32; 1554427a928aSKonstantin Belousov 1555427a928aSKonstantin Belousov ifm32 = (struct if_msghdr32 *)w->w_tmem; 1556427a928aSKonstantin Belousov ifm32->ifm_index = ifp->if_index; 1557427a928aSKonstantin Belousov ifm32->ifm_flags = ifp->if_flags | 1558427a928aSKonstantin Belousov ifp->if_drv_flags; 1559427a928aSKonstantin Belousov copy_ifdata32(&ifp->if_data, &ifm32->ifm_data); 1560427a928aSKonstantin Belousov ifm32->ifm_addrs = info.rti_addrs; 1561427a928aSKonstantin Belousov error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32, 1562427a928aSKonstantin Belousov len); 1563427a928aSKonstantin Belousov goto sysctl_out; 1564427a928aSKonstantin Belousov } 1565427a928aSKonstantin Belousov #endif 1566df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 1567df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1568292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1569df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1570df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 157152041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len); 1572427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1573427a928aSKonstantin Belousov sysctl_out: 1574427a928aSKonstantin Belousov #endif 1575df440948SPoul-Henning Kamp if (error) 1576a35b06c5SJonathan Lemon goto done; 1577df8bae1dSRodney W. Grimes } 15785dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1579df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 1580df8bae1dSRodney W. Grimes continue; 1581b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1582b89e82ddSJamie Gritton ifa->ifa_addr) != 0) 158375c13541SPoul-Henning Kamp continue; 1584becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1585becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1586becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1587913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 158852041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1589becc44d7SSam Leffler struct ifa_msghdr *ifam; 1590df8bae1dSRodney W. Grimes 1591df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1592df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1593df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1594df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1595df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 159652041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1597df440948SPoul-Henning Kamp if (error) 1598a35b06c5SJonathan Lemon goto done; 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes } 1601ee7c7feeSBjoern A. Zeeb IF_ADDR_UNLOCK(ifp); 1602becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 16035dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1604df8bae1dSRodney W. Grimes } 1605a35b06c5SJonathan Lemon done: 1606ee7c7feeSBjoern A. Zeeb if (ifp != NULL) 1607ee7c7feeSBjoern A. Zeeb IF_ADDR_UNLOCK(ifp); 1608fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 1609a35b06c5SJonathan Lemon return (error); 1610df8bae1dSRodney W. Grimes } 1611df8bae1dSRodney W. Grimes 1612ebda3fc3SBjoern A. Zeeb static int 16139b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 161405b2efe0SBruce M Simpson { 16159b98ee2cSLuigi Rizzo struct ifnet *ifp; 161605b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 161705b2efe0SBruce M Simpson struct rt_addrinfo info; 161805b2efe0SBruce M Simpson int len, error = 0; 16199b98ee2cSLuigi Rizzo struct ifaddr *ifa; 162005b2efe0SBruce M Simpson 162105b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 1622fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1623603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 162405b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 162505b2efe0SBruce M Simpson continue; 16264a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1627913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1628fe0fc7efSChristian S.J. Peron IF_ADDR_LOCK(ifp); 162905b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 163005b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 163105b2efe0SBruce M Simpson continue; 1632b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1633b89e82ddSJamie Gritton ifma->ifma_addr) != 0) 163405b2efe0SBruce M Simpson continue; 163505b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1636913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1637913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1638913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1639913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 164005b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 16419b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 164205b2efe0SBruce M Simpson 164305b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 164405b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 164505b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 164605b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 164705b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1648fe0fc7efSChristian S.J. Peron if (error) { 1649fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 165005b2efe0SBruce M Simpson goto done; 165105b2efe0SBruce M Simpson } 165205b2efe0SBruce M Simpson } 165305b2efe0SBruce M Simpson } 1654fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 1655fe0fc7efSChristian S.J. Peron } 165605b2efe0SBruce M Simpson done: 1657fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 165805b2efe0SBruce M Simpson return (error); 165905b2efe0SBruce M Simpson } 166005b2efe0SBruce M Simpson 166152041295SPoul-Henning Kamp static int 166282d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1663df8bae1dSRodney W. Grimes { 166452041295SPoul-Henning Kamp int *name = (int *)arg1; 166552041295SPoul-Henning Kamp u_int namelen = arg2; 1666c2c2a7c1SBjoern A. Zeeb struct radix_node_head *rnh = NULL; /* silence compiler. */ 1667fe0fc7efSChristian S.J. Peron int i, lim, error = EINVAL; 1668df8bae1dSRodney W. Grimes u_char af; 1669df8bae1dSRodney W. Grimes struct walkarg w; 1670df8bae1dSRodney W. Grimes 167152041295SPoul-Henning Kamp name ++; 167252041295SPoul-Henning Kamp namelen--; 167352041295SPoul-Henning Kamp if (req->newptr) 1674df8bae1dSRodney W. Grimes return (EPERM); 1675df8bae1dSRodney W. Grimes if (namelen != 3) 1676f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1677df8bae1dSRodney W. Grimes af = name[0]; 1678b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1679b2aaf46eSJeffrey Hsu return (EINVAL); 16806b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1681df8bae1dSRodney W. Grimes w.w_op = name[1]; 1682df8bae1dSRodney W. Grimes w.w_arg = name[2]; 168352041295SPoul-Henning Kamp w.w_req = req; 1684df8bae1dSRodney W. Grimes 1685fe0fc7efSChristian S.J. Peron error = sysctl_wire_old_buffer(req, 0); 1686fe0fc7efSChristian S.J. Peron if (error) 1687fe0fc7efSChristian S.J. Peron return (error); 1688df8bae1dSRodney W. Grimes switch (w.w_op) { 1689df8bae1dSRodney W. Grimes 1690df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1691df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1692a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1693a8b76c8fSLuigi Rizzo i = 1; 1694a8b76c8fSLuigi Rizzo lim = AF_MAX; 1695a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1696a8b76c8fSLuigi Rizzo i = lim = af; 16978eca593cSQing Li 16988eca593cSQing Li /* 16998eca593cSQing Li * take care of llinfo entries, the caller must 17008eca593cSQing Li * specify an AF 17018eca593cSQing Li */ 170214981d80SQing Li if (w.w_op == NET_RT_FLAGS && 170382b334e8SQing Li (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 17048eca593cSQing Li if (af != 0) 17058eca593cSQing Li error = lltable_sysctl_dumparp(af, w.w_req); 17068eca593cSQing Li else 17078eca593cSQing Li error = EINVAL; 17088eca593cSQing Li break; 17098eca593cSQing Li } 17108eca593cSQing Li /* 17118eca593cSQing Li * take care of routing entries 17128eca593cSQing Li */ 171320b0cdb7SBjoern A. Zeeb for (error = 0; error == 0 && i <= lim; i++) { 1714c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i); 1715c2c2a7c1SBjoern A. Zeeb if (rnh != NULL) { 1716fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_LOCK(rnh); 1717956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 1718fe0fc7efSChristian S.J. Peron sysctl_dumpentry, &w); 1719fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_UNLOCK(rnh); 1720a8b76c8fSLuigi Rizzo } else if (af != 0) 1721956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 172220b0cdb7SBjoern A. Zeeb } 1723df8bae1dSRodney W. Grimes break; 1724df8bae1dSRodney W. Grimes 1725df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1726df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 172705b2efe0SBruce M Simpson break; 172805b2efe0SBruce M Simpson 172905b2efe0SBruce M Simpson case NET_RT_IFMALIST: 173005b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 173105b2efe0SBruce M Simpson break; 1732df8bae1dSRodney W. Grimes } 1733df8bae1dSRodney W. Grimes if (w.w_tmem) 1734df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1735df8bae1dSRodney W. Grimes return (error); 1736df8bae1dSRodney W. Grimes } 1737df8bae1dSRodney W. Grimes 173852041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 173952041295SPoul-Henning Kamp 1740df8bae1dSRodney W. Grimes /* 1741df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1742df8bae1dSRodney W. Grimes */ 1743df8bae1dSRodney W. Grimes 17445b1c0294SDavid E. O'Brien static struct domain routedomain; /* or at least forward */ 1745df8bae1dSRodney W. Grimes 174652041295SPoul-Henning Kamp static struct protosw routesw[] = { 1747303989a2SRuslan Ermilov { 1748303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 1749303989a2SRuslan Ermilov .pr_domain = &routedomain, 1750303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1751303989a2SRuslan Ermilov .pr_output = route_output, 1752303989a2SRuslan Ermilov .pr_ctlinput = raw_ctlinput, 1753303989a2SRuslan Ermilov .pr_init = raw_init, 1754303989a2SRuslan Ermilov .pr_usrreqs = &route_usrreqs 1755df8bae1dSRodney W. Grimes } 1756df8bae1dSRodney W. Grimes }; 1757df8bae1dSRodney W. Grimes 1758303989a2SRuslan Ermilov static struct domain routedomain = { 1759303989a2SRuslan Ermilov .dom_family = PF_ROUTE, 1760303989a2SRuslan Ermilov .dom_name = "route", 1761303989a2SRuslan Ermilov .dom_protosw = routesw, 1762303989a2SRuslan Ermilov .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1763303989a2SRuslan Ermilov }; 176478a82810SGarrett Wollman 1765d0728d71SRobert Watson VNET_DOMAIN_SET(route); 1766