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> 58*0ed6142bSQing 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 125becc44d7SSam Leffler static struct { 12618aee723SPeter Pentchev int ip_count; /* attached w/ AF_INET */ 127becc44d7SSam Leffler int ip6_count; /* attached w/ AF_INET6 */ 128becc44d7SSam Leffler int ipx_count; /* attached w/ AF_IPX */ 129becc44d7SSam Leffler int any_count; /* total attached */ 130becc44d7SSam Leffler } route_cb; 131df8bae1dSRodney W. Grimes 132aea8b30fSSam Leffler struct mtx rtsock_mtx; 133aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 134aea8b30fSSam Leffler 135aea8b30fSSam Leffler #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 136aea8b30fSSam Leffler #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 137aea8b30fSSam Leffler #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 138aea8b30fSSam Leffler 139190a4c94SRobert Watson SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 140190a4c94SRobert Watson 141df8bae1dSRodney W. Grimes struct walkarg { 14252041295SPoul-Henning Kamp int w_tmemsize; 14352041295SPoul-Henning Kamp int w_op, w_arg; 14452041295SPoul-Henning Kamp caddr_t w_tmem; 14552041295SPoul-Henning Kamp struct sysctl_req *w_req; 146df8bae1dSRodney W. Grimes }; 147df8bae1dSRodney W. Grimes 148d989c7b3SRobert Watson static void rts_input(struct mbuf *m); 1495dfc91d7SLuigi Rizzo static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 1505dfc91d7SLuigi Rizzo static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 1515dfc91d7SLuigi Rizzo caddr_t cp, struct walkarg *w); 1525dfc91d7SLuigi Rizzo static int rt_xaddrs(caddr_t cp, caddr_t cplim, 1535dfc91d7SLuigi Rizzo struct rt_addrinfo *rtinfo); 154929ddbbbSAlfred Perlstein static int sysctl_dumpentry(struct radix_node *rn, void *vw); 155929ddbbbSAlfred Perlstein static int sysctl_iflist(int af, struct walkarg *w); 15605b2efe0SBruce M Simpson static int sysctl_ifmalist(int af, struct walkarg *w); 1575dfc91d7SLuigi Rizzo static int route_output(struct mbuf *m, struct socket *so); 1585dfc91d7SLuigi Rizzo static void rt_setmetrics(u_long which, const struct rt_metrics *in, 1595dfc91d7SLuigi Rizzo struct rt_metrics_lite *out); 1605dfc91d7SLuigi Rizzo static void rt_getmetrics(const struct rt_metrics_lite *in, 1615dfc91d7SLuigi Rizzo struct rt_metrics *out); 1625dfc91d7SLuigi Rizzo static void rt_dispatch(struct mbuf *, const struct sockaddr *); 163df8bae1dSRodney W. Grimes 164d4b5cae4SRobert Watson static struct netisr_handler rtsock_nh = { 165d4b5cae4SRobert Watson .nh_name = "rtsock", 166d4b5cae4SRobert Watson .nh_handler = rts_input, 167d4b5cae4SRobert Watson .nh_proto = NETISR_ROUTE, 168d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 169d4b5cae4SRobert Watson }; 170d4b5cae4SRobert Watson 171d4b5cae4SRobert Watson static int 172d4b5cae4SRobert Watson sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS) 173d4b5cae4SRobert Watson { 174d4b5cae4SRobert Watson int error, qlimit; 175d4b5cae4SRobert Watson 176d4b5cae4SRobert Watson netisr_getqlimit(&rtsock_nh, &qlimit); 177d4b5cae4SRobert Watson error = sysctl_handle_int(oidp, &qlimit, 0, req); 178d4b5cae4SRobert Watson if (error || !req->newptr) 179d4b5cae4SRobert Watson return (error); 180d4b5cae4SRobert Watson if (qlimit < 1) 181d4b5cae4SRobert Watson return (EINVAL); 182d4b5cae4SRobert Watson return (netisr_setqlimit(&rtsock_nh, qlimit)); 183d4b5cae4SRobert Watson } 184d4b5cae4SRobert Watson SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW, 185d4b5cae4SRobert Watson 0, 0, sysctl_route_netisr_maxqlen, "I", 186d4b5cae4SRobert Watson "maximum routing socket dispatch queue length"); 187d4b5cae4SRobert Watson 188d989c7b3SRobert Watson static void 189d989c7b3SRobert Watson rts_init(void) 190d989c7b3SRobert Watson { 191b062951aSRobert Watson int tmp; 192d989c7b3SRobert Watson 193b062951aSRobert Watson if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 194d4b5cae4SRobert Watson rtsock_nh.nh_qlimit = tmp; 195d4b5cae4SRobert Watson netisr_register(&rtsock_nh); 196d989c7b3SRobert Watson } 197237fdd78SRobert Watson SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0); 198d989c7b3SRobert Watson 199d989c7b3SRobert Watson static void 200d989c7b3SRobert Watson rts_input(struct mbuf *m) 201d989c7b3SRobert Watson { 202d989c7b3SRobert Watson struct sockproto route_proto; 203d989c7b3SRobert Watson unsigned short *family; 204d989c7b3SRobert Watson struct m_tag *tag; 205d989c7b3SRobert Watson 206d989c7b3SRobert Watson route_proto.sp_family = PF_ROUTE; 207d989c7b3SRobert Watson tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 208d989c7b3SRobert Watson if (tag != NULL) { 209d989c7b3SRobert Watson family = (unsigned short *)(tag + 1); 210d989c7b3SRobert Watson route_proto.sp_protocol = *family; 211d989c7b3SRobert Watson m_tag_delete(m, tag); 212d989c7b3SRobert Watson } else 213d989c7b3SRobert Watson route_proto.sp_protocol = 0; 214d989c7b3SRobert Watson 2154d896055SRobert Watson raw_input(m, &route_proto, &route_src); 216d989c7b3SRobert Watson } 217d989c7b3SRobert Watson 218a29f300eSGarrett Wollman /* 219a29f300eSGarrett Wollman * It really doesn't make any sense at all for this code to share much 220a29f300eSGarrett Wollman * with raw_usrreq.c, since its functionality is so restricted. XXX 221a29f300eSGarrett Wollman */ 222ac45e92fSRobert Watson static void 223a29f300eSGarrett Wollman rts_abort(struct socket *so) 224df8bae1dSRodney W. Grimes { 2257e994955SRobert Watson 226ac45e92fSRobert Watson raw_usrreqs.pru_abort(so); 227df8bae1dSRodney W. Grimes } 228a29f300eSGarrett Wollman 229a152f8a3SRobert Watson static void 230a152f8a3SRobert Watson rts_close(struct socket *so) 231a152f8a3SRobert Watson { 232a152f8a3SRobert Watson 233a152f8a3SRobert Watson raw_usrreqs.pru_close(so); 234a152f8a3SRobert Watson } 235a152f8a3SRobert Watson 236a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */ 237a29f300eSGarrett Wollman 238a29f300eSGarrett Wollman static int 239b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td) 240a29f300eSGarrett Wollman { 241a29f300eSGarrett Wollman struct rawcb *rp; 242a29f300eSGarrett Wollman int s, error; 243a29f300eSGarrett Wollman 244bc725eafSRobert Watson KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 245bc725eafSRobert Watson 2467cc0979fSDavid Malone /* XXX */ 2471ede983cSDag-Erling Smørgrav rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 2485dfc91d7SLuigi Rizzo if (rp == NULL) 249a29f300eSGarrett Wollman return ENOBUFS; 250a29f300eSGarrett Wollman 251a29f300eSGarrett Wollman /* 252a29f300eSGarrett Wollman * The splnet() is necessary to block protocols from sending 253a29f300eSGarrett Wollman * error notifications (like RTM_REDIRECT or RTM_LOSING) while 254a29f300eSGarrett Wollman * this PCB is extant but incompletely initialized. 255a29f300eSGarrett Wollman * Probably we should try to do more of this work beforehand and 256a29f300eSGarrett Wollman * eliminate the spl. 257a29f300eSGarrett Wollman */ 258a29f300eSGarrett Wollman s = splnet(); 259a29f300eSGarrett Wollman so->so_pcb = (caddr_t)rp; 2608b07e49aSJulian Elischer so->so_fibnum = td->td_proc->p_fibnum; 261162c0b2eSRuslan Ermilov error = raw_attach(so, proto); 262a29f300eSGarrett Wollman rp = sotorawcb(so); 263a29f300eSGarrett Wollman if (error) { 264a29f300eSGarrett Wollman splx(s); 2657ba271aeSJonathan Chen so->so_pcb = NULL; 266a29f300eSGarrett Wollman free(rp, M_PCB); 267a29f300eSGarrett Wollman return error; 268a29f300eSGarrett Wollman } 269aea8b30fSSam Leffler RTSOCK_LOCK(); 270a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 271a29f300eSGarrett Wollman case AF_INET: 272df8bae1dSRodney W. Grimes route_cb.ip_count++; 273a29f300eSGarrett Wollman break; 274899ce4f4SYoshinobu Inoue case AF_INET6: 275899ce4f4SYoshinobu Inoue route_cb.ip6_count++; 276899ce4f4SYoshinobu Inoue break; 277a29f300eSGarrett Wollman case AF_IPX: 278cc6a66f2SJulian Elischer route_cb.ipx_count++; 279a29f300eSGarrett Wollman break; 280a29f300eSGarrett Wollman } 281df8bae1dSRodney W. Grimes route_cb.any_count++; 282aea8b30fSSam Leffler RTSOCK_UNLOCK(); 28303e49181SSeigo Tanimura soisconnected(so); 284df8bae1dSRodney W. Grimes so->so_options |= SO_USELOOPBACK; 285df8bae1dSRodney W. Grimes splx(s); 286a29f300eSGarrett Wollman return 0; 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes 289a29f300eSGarrett Wollman static int 290b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 291a29f300eSGarrett Wollman { 2927e994955SRobert Watson 2937e994955SRobert Watson return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 294a29f300eSGarrett Wollman } 295a29f300eSGarrett Wollman 296a29f300eSGarrett Wollman static int 297b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 298a29f300eSGarrett Wollman { 2997e994955SRobert Watson 3007e994955SRobert Watson return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 301a29f300eSGarrett Wollman } 302a29f300eSGarrett Wollman 303a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */ 304a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */ 305a29f300eSGarrett Wollman 306bc725eafSRobert Watson static void 307a29f300eSGarrett Wollman rts_detach(struct socket *so) 308a29f300eSGarrett Wollman { 309a29f300eSGarrett Wollman struct rawcb *rp = sotorawcb(so); 310a29f300eSGarrett Wollman 311bc725eafSRobert Watson KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 312bc725eafSRobert Watson 313aea8b30fSSam Leffler RTSOCK_LOCK(); 314a29f300eSGarrett Wollman switch(rp->rcb_proto.sp_protocol) { 315a29f300eSGarrett Wollman case AF_INET: 316a29f300eSGarrett Wollman route_cb.ip_count--; 317a29f300eSGarrett Wollman break; 318899ce4f4SYoshinobu Inoue case AF_INET6: 319899ce4f4SYoshinobu Inoue route_cb.ip6_count--; 320899ce4f4SYoshinobu Inoue break; 321a29f300eSGarrett Wollman case AF_IPX: 322a29f300eSGarrett Wollman route_cb.ipx_count--; 323a29f300eSGarrett Wollman break; 324a29f300eSGarrett Wollman } 325a29f300eSGarrett Wollman route_cb.any_count--; 326aea8b30fSSam Leffler RTSOCK_UNLOCK(); 327bc725eafSRobert Watson raw_usrreqs.pru_detach(so); 328a29f300eSGarrett Wollman } 329a29f300eSGarrett Wollman 330a29f300eSGarrett Wollman static int 331a29f300eSGarrett Wollman rts_disconnect(struct socket *so) 332a29f300eSGarrett Wollman { 3337e994955SRobert Watson 3347e994955SRobert Watson return (raw_usrreqs.pru_disconnect(so)); 335a29f300eSGarrett Wollman } 336a29f300eSGarrett Wollman 337a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */ 338a29f300eSGarrett Wollman 339a29f300eSGarrett Wollman static int 34057bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam) 341a29f300eSGarrett Wollman { 3427e994955SRobert Watson 3437e994955SRobert Watson return (raw_usrreqs.pru_peeraddr(so, nam)); 344a29f300eSGarrett Wollman } 345a29f300eSGarrett Wollman 346a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */ 347a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 348a29f300eSGarrett Wollman 349a29f300eSGarrett Wollman static int 35057bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 351b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 352a29f300eSGarrett Wollman { 3537e994955SRobert Watson 3547e994955SRobert Watson return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 355a29f300eSGarrett Wollman } 356a29f300eSGarrett Wollman 357a29f300eSGarrett Wollman /* pru_sense is null */ 358a29f300eSGarrett Wollman 359a29f300eSGarrett Wollman static int 360a29f300eSGarrett Wollman rts_shutdown(struct socket *so) 361a29f300eSGarrett Wollman { 3627e994955SRobert Watson 3637e994955SRobert Watson return (raw_usrreqs.pru_shutdown(so)); 364a29f300eSGarrett Wollman } 365a29f300eSGarrett Wollman 366a29f300eSGarrett Wollman static int 36757bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam) 368a29f300eSGarrett Wollman { 3697e994955SRobert Watson 3707e994955SRobert Watson return (raw_usrreqs.pru_sockaddr(so, nam)); 371a29f300eSGarrett Wollman } 372a29f300eSGarrett Wollman 373a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = { 374756d52a1SPoul-Henning Kamp .pru_abort = rts_abort, 375756d52a1SPoul-Henning Kamp .pru_attach = rts_attach, 376756d52a1SPoul-Henning Kamp .pru_bind = rts_bind, 377756d52a1SPoul-Henning Kamp .pru_connect = rts_connect, 378756d52a1SPoul-Henning Kamp .pru_detach = rts_detach, 379756d52a1SPoul-Henning Kamp .pru_disconnect = rts_disconnect, 380756d52a1SPoul-Henning Kamp .pru_peeraddr = rts_peeraddr, 381756d52a1SPoul-Henning Kamp .pru_send = rts_send, 382756d52a1SPoul-Henning Kamp .pru_shutdown = rts_shutdown, 383756d52a1SPoul-Henning Kamp .pru_sockaddr = rts_sockaddr, 384a152f8a3SRobert Watson .pru_close = rts_close, 385a29f300eSGarrett Wollman }; 386a29f300eSGarrett Wollman 387413628a7SBjoern A. Zeeb #ifndef _SOCKADDR_UNION_DEFINED 388413628a7SBjoern A. Zeeb #define _SOCKADDR_UNION_DEFINED 389413628a7SBjoern A. Zeeb /* 390413628a7SBjoern A. Zeeb * The union of all possible address formats we handle. 391413628a7SBjoern A. Zeeb */ 392413628a7SBjoern A. Zeeb union sockaddr_union { 393413628a7SBjoern A. Zeeb struct sockaddr sa; 394413628a7SBjoern A. Zeeb struct sockaddr_in sin; 395413628a7SBjoern A. Zeeb struct sockaddr_in6 sin6; 396413628a7SBjoern A. Zeeb }; 397413628a7SBjoern A. Zeeb #endif /* _SOCKADDR_UNION_DEFINED */ 398413628a7SBjoern A. Zeeb 399413628a7SBjoern A. Zeeb static int 400413628a7SBjoern A. Zeeb rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp, 401413628a7SBjoern A. Zeeb struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred) 402413628a7SBjoern A. Zeeb { 403413628a7SBjoern A. Zeeb 4049c79d243SJamie Gritton /* First, see if the returned address is part of the jail. */ 4059c79d243SJamie Gritton if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) { 4069c79d243SJamie Gritton info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 4079c79d243SJamie Gritton return (0); 4089c79d243SJamie Gritton } 4099c79d243SJamie Gritton 410413628a7SBjoern A. Zeeb switch (info->rti_info[RTAX_DST]->sa_family) { 411413628a7SBjoern A. Zeeb #ifdef INET 412413628a7SBjoern A. Zeeb case AF_INET: 413413628a7SBjoern A. Zeeb { 414413628a7SBjoern A. Zeeb struct in_addr ia; 415413628a7SBjoern A. Zeeb struct ifaddr *ifa; 416413628a7SBjoern A. Zeeb int found; 417413628a7SBjoern A. Zeeb 418413628a7SBjoern A. Zeeb found = 0; 419413628a7SBjoern A. Zeeb /* 4209c79d243SJamie Gritton * Try to find an address on the given outgoing interface 4219c79d243SJamie Gritton * that belongs to the jail. 422413628a7SBjoern A. Zeeb */ 42377ee4c0eSRobert Watson IF_ADDR_LOCK(ifp); 424413628a7SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 425413628a7SBjoern A. Zeeb struct sockaddr *sa; 426413628a7SBjoern A. Zeeb sa = ifa->ifa_addr; 427413628a7SBjoern A. Zeeb if (sa->sa_family != AF_INET) 428413628a7SBjoern A. Zeeb continue; 429413628a7SBjoern A. Zeeb ia = ((struct sockaddr_in *)sa)->sin_addr; 430b89e82ddSJamie Gritton if (prison_check_ip4(cred, &ia) == 0) { 431413628a7SBjoern A. Zeeb found = 1; 432413628a7SBjoern A. Zeeb break; 433413628a7SBjoern A. Zeeb } 434413628a7SBjoern A. Zeeb } 43577ee4c0eSRobert Watson IF_ADDR_UNLOCK(ifp); 436413628a7SBjoern A. Zeeb if (!found) { 437413628a7SBjoern A. Zeeb /* 4389c79d243SJamie Gritton * As a last resort return the 'default' jail address. 439413628a7SBjoern A. Zeeb */ 4400304c731SJamie Gritton ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)-> 4410304c731SJamie Gritton sin_addr; 4421cecba0fSBjoern A. Zeeb if (prison_get_ip4(cred, &ia) != 0) 443413628a7SBjoern A. Zeeb return (ESRCH); 444413628a7SBjoern A. Zeeb } 445413628a7SBjoern A. Zeeb bzero(&saun->sin, sizeof(struct sockaddr_in)); 446413628a7SBjoern A. Zeeb saun->sin.sin_len = sizeof(struct sockaddr_in); 447413628a7SBjoern A. Zeeb saun->sin.sin_family = AF_INET; 448413628a7SBjoern A. Zeeb saun->sin.sin_addr.s_addr = ia.s_addr; 4499c79d243SJamie Gritton info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin; 450413628a7SBjoern A. Zeeb break; 451413628a7SBjoern A. Zeeb } 452413628a7SBjoern A. Zeeb #endif 453413628a7SBjoern A. Zeeb #ifdef INET6 454413628a7SBjoern A. Zeeb case AF_INET6: 455413628a7SBjoern A. Zeeb { 456413628a7SBjoern A. Zeeb struct in6_addr ia6; 457413628a7SBjoern A. Zeeb struct ifaddr *ifa; 458413628a7SBjoern A. Zeeb int found; 459413628a7SBjoern A. Zeeb 460413628a7SBjoern A. Zeeb found = 0; 461413628a7SBjoern A. Zeeb /* 4629c79d243SJamie Gritton * Try to find an address on the given outgoing interface 4639c79d243SJamie Gritton * that belongs to the jail. 464413628a7SBjoern A. Zeeb */ 46577ee4c0eSRobert Watson IF_ADDR_LOCK(ifp); 466413628a7SBjoern A. Zeeb TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 467413628a7SBjoern A. Zeeb struct sockaddr *sa; 468413628a7SBjoern A. Zeeb sa = ifa->ifa_addr; 469413628a7SBjoern A. Zeeb if (sa->sa_family != AF_INET6) 470413628a7SBjoern A. Zeeb continue; 471413628a7SBjoern A. Zeeb bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr, 472413628a7SBjoern A. Zeeb &ia6, sizeof(struct in6_addr)); 473b89e82ddSJamie Gritton if (prison_check_ip6(cred, &ia6) == 0) { 474413628a7SBjoern A. Zeeb found = 1; 475413628a7SBjoern A. Zeeb break; 476413628a7SBjoern A. Zeeb } 477413628a7SBjoern A. Zeeb } 47877ee4c0eSRobert Watson IF_ADDR_UNLOCK(ifp); 479413628a7SBjoern A. Zeeb if (!found) { 480413628a7SBjoern A. Zeeb /* 4819c79d243SJamie Gritton * As a last resort return the 'default' jail address. 482413628a7SBjoern A. Zeeb */ 4830304c731SJamie Gritton ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)-> 4840304c731SJamie Gritton sin6_addr; 4851cecba0fSBjoern A. Zeeb if (prison_get_ip6(cred, &ia6) != 0) 486413628a7SBjoern A. Zeeb return (ESRCH); 487413628a7SBjoern A. Zeeb } 488413628a7SBjoern A. Zeeb bzero(&saun->sin6, sizeof(struct sockaddr_in6)); 489413628a7SBjoern A. Zeeb saun->sin6.sin6_len = sizeof(struct sockaddr_in6); 490413628a7SBjoern A. Zeeb saun->sin6.sin6_family = AF_INET6; 4919c79d243SJamie Gritton bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr)); 492413628a7SBjoern A. Zeeb if (sa6_recoverscope(&saun->sin6) != 0) 493413628a7SBjoern A. Zeeb return (ESRCH); 4949c79d243SJamie Gritton info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6; 495413628a7SBjoern A. Zeeb break; 496413628a7SBjoern A. Zeeb } 497413628a7SBjoern A. Zeeb #endif 498413628a7SBjoern A. Zeeb default: 499413628a7SBjoern A. Zeeb return (ESRCH); 500413628a7SBjoern A. Zeeb } 501413628a7SBjoern A. Zeeb return (0); 502413628a7SBjoern A. Zeeb } 503413628a7SBjoern A. Zeeb 504df8bae1dSRodney W. Grimes /*ARGSUSED*/ 50552041295SPoul-Henning Kamp static int 5065dfc91d7SLuigi Rizzo route_output(struct mbuf *m, struct socket *so) 507df8bae1dSRodney W. Grimes { 508becc44d7SSam Leffler #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 5095dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = NULL; 5105dfc91d7SLuigi Rizzo struct rtentry *rt = NULL; 51178a82810SGarrett Wollman struct radix_node_head *rnh; 512df8bae1dSRodney W. Grimes struct rt_addrinfo info; 513df8bae1dSRodney W. Grimes int len, error = 0; 5145dfc91d7SLuigi Rizzo struct ifnet *ifp = NULL; 515413628a7SBjoern A. Zeeb union sockaddr_union saun; 516df8bae1dSRodney W. Grimes 517df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;} 5185dfc91d7SLuigi Rizzo if (m == NULL || ((m->m_len < sizeof(long)) && 5195dfc91d7SLuigi Rizzo (m = m_pullup(m, sizeof(long))) == NULL)) 520df8bae1dSRodney W. Grimes return (ENOBUFS); 521df8bae1dSRodney W. Grimes if ((m->m_flags & M_PKTHDR) == 0) 522df8bae1dSRodney W. Grimes panic("route_output"); 523df8bae1dSRodney W. Grimes len = m->m_pkthdr.len; 524df8bae1dSRodney W. Grimes if (len < sizeof(*rtm) || 525df8bae1dSRodney W. Grimes len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 5265dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 527df8bae1dSRodney W. Grimes senderr(EINVAL); 528df8bae1dSRodney W. Grimes } 529df8bae1dSRodney W. Grimes R_Malloc(rtm, struct rt_msghdr *, len); 5305dfc91d7SLuigi Rizzo if (rtm == NULL) { 5315dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 532df8bae1dSRodney W. Grimes senderr(ENOBUFS); 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes m_copydata(m, 0, len, (caddr_t)rtm); 535df8bae1dSRodney W. Grimes if (rtm->rtm_version != RTM_VERSION) { 5365dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 537df8bae1dSRodney W. Grimes senderr(EPROTONOSUPPORT); 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes rtm->rtm_pid = curproc->p_pid; 5408071913dSRuslan Ermilov bzero(&info, sizeof(info)); 541df8bae1dSRodney W. Grimes info.rti_addrs = rtm->rtm_addrs; 542076d0761SJulian Elischer if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 5435dfc91d7SLuigi Rizzo info.rti_info[RTAX_DST] = NULL; 544076d0761SJulian Elischer senderr(EINVAL); 545076d0761SJulian Elischer } 5468071913dSRuslan Ermilov info.rti_flags = rtm->rtm_flags; 5475dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_DST] == NULL || 548becc44d7SSam Leffler info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 5495dfc91d7SLuigi Rizzo (info.rti_info[RTAX_GATEWAY] != NULL && 550becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 551df8bae1dSRodney W. Grimes senderr(EINVAL); 552162c0b2eSRuslan Ermilov /* 553162c0b2eSRuslan Ermilov * Verify that the caller has the appropriate privilege; RTM_GET 554162c0b2eSRuslan Ermilov * is the only operation the non-superuser is allowed. 555162c0b2eSRuslan Ermilov */ 556acd3428bSRobert Watson if (rtm->rtm_type != RTM_GET) { 557acd3428bSRobert Watson error = priv_check(curthread, PRIV_NET_ROUTE); 558acd3428bSRobert Watson if (error) 559dadb6c3bSRuslan Ermilov senderr(error); 560acd3428bSRobert Watson } 561162c0b2eSRuslan Ermilov 5629231d35fSQing Li /* 5639231d35fSQing Li * The given gateway address may be an interface address. 5649231d35fSQing Li * For example, issuing a "route change" command on a route 5659231d35fSQing Li * entry that was created from a tunnel, and the gateway 5669231d35fSQing Li * address given is the local end point. In this case the 5679231d35fSQing Li * RTF_GATEWAY flag must be cleared or the destination will 5689231d35fSQing Li * not be reachable even though there is no error message. 5699231d35fSQing Li */ 5709231d35fSQing Li if (info.rti_info[RTAX_GATEWAY] != NULL && 5719231d35fSQing Li info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 5729231d35fSQing Li struct route gw_ro; 5739231d35fSQing Li 5749231d35fSQing Li bzero(&gw_ro, sizeof(gw_ro)); 5759231d35fSQing Li gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY]; 5765311e988SQing Li rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum); 5779231d35fSQing Li /* 5789231d35fSQing Li * A host route through the loopback interface is 5799231d35fSQing Li * installed for each interface adddress. In pre 8.0 5809231d35fSQing Li * releases the interface address of a PPP link type 5819231d35fSQing Li * is not reachable locally. This behavior is fixed as 5829231d35fSQing Li * part of the new L2/L3 redesign and rewrite work. The 5839231d35fSQing Li * signature of this interface address route is the 5849231d35fSQing Li * AF_LINK sa_family type of the rt_gateway, and the 5859231d35fSQing Li * rt_ifp has the IFF_LOOPBACK flag set. 5869231d35fSQing Li */ 5879231d35fSQing Li if (gw_ro.ro_rt != NULL && 5889231d35fSQing Li gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK && 5899231d35fSQing Li gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) 5909231d35fSQing Li info.rti_flags &= ~RTF_GATEWAY; 5919231d35fSQing Li if (gw_ro.ro_rt != NULL) 5929231d35fSQing Li RTFREE(gw_ro.ro_rt); 5939231d35fSQing Li } 5949231d35fSQing Li 595df8bae1dSRodney W. Grimes switch (rtm->rtm_type) { 596becc44d7SSam Leffler struct rtentry *saved_nrt; 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes case RTM_ADD: 5995dfc91d7SLuigi Rizzo if (info.rti_info[RTAX_GATEWAY] == NULL) 600df8bae1dSRodney W. Grimes senderr(EINVAL); 6015dfc91d7SLuigi Rizzo saved_nrt = NULL; 6028eca593cSQing Li 6036e6b3f7cSQing Li /* support for new ARP code */ 6048eca593cSQing Li if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 6058eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6066e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6076e6b3f7cSQing Li break; 6086e6b3f7cSQing Li } 6098b07e49aSJulian Elischer error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 6108b07e49aSJulian Elischer so->so_fibnum); 611df8bae1dSRodney W. Grimes if (error == 0 && saved_nrt) { 612d1dd20beSSam Leffler RT_LOCK(saved_nrt); 613df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, 614df8bae1dSRodney W. Grimes &rtm->rtm_rmx, &saved_nrt->rt_rmx); 61522cafcf0SAndre Oppermann rtm->rtm_index = saved_nrt->rt_ifp->if_index; 6167138d65cSSam Leffler RT_REMREF(saved_nrt); 617d1dd20beSSam Leffler RT_UNLOCK(saved_nrt); 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes break; 620df8bae1dSRodney W. Grimes 621df8bae1dSRodney W. Grimes case RTM_DELETE: 6225dfc91d7SLuigi Rizzo saved_nrt = NULL; 6236e6b3f7cSQing Li /* support for new ARP code */ 6246e6b3f7cSQing Li if (info.rti_info[RTAX_GATEWAY] && 6258eca593cSQing Li (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 6268eca593cSQing Li (rtm->rtm_flags & RTF_LLDATA) != 0) { 6276e6b3f7cSQing Li error = lla_rt_output(rtm, &info); 6286e6b3f7cSQing Li break; 6296e6b3f7cSQing Li } 6308b07e49aSJulian Elischer error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 6318b07e49aSJulian Elischer so->so_fibnum); 63278a82810SGarrett Wollman if (error == 0) { 633d1dd20beSSam Leffler RT_LOCK(saved_nrt); 63471eba915SRuslan Ermilov rt = saved_nrt; 63578a82810SGarrett Wollman goto report; 63678a82810SGarrett Wollman } 637df8bae1dSRodney W. Grimes break; 638df8bae1dSRodney W. Grimes 639df8bae1dSRodney W. Grimes case RTM_GET: 640df8bae1dSRodney W. Grimes case RTM_CHANGE: 641df8bae1dSRodney W. Grimes case RTM_LOCK: 642c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(so->so_fibnum, 643c2c2a7c1SBjoern A. Zeeb info.rti_info[RTAX_DST]->sa_family); 6445dfc91d7SLuigi Rizzo if (rnh == NULL) 64578a82810SGarrett Wollman senderr(EAFNOSUPPORT); 6463120b9d4SKip Macy RADIX_NODE_HEAD_RLOCK(rnh); 647becc44d7SSam Leffler rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 648becc44d7SSam Leffler info.rti_info[RTAX_NETMASK], rnh); 64979188861SGleb Smirnoff if (rt == NULL) { /* XXX looks bogus */ 6503120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 651df8bae1dSRodney W. Grimes senderr(ESRCH); 65279188861SGleb Smirnoff } 653e440aed9SQing Li #ifdef RADIX_MPATH 654e440aed9SQing Li /* 655e440aed9SQing Li * for RTM_CHANGE/LOCK, if we got multipath routes, 656e440aed9SQing Li * we require users to specify a matching RTAX_GATEWAY. 657e440aed9SQing Li * 658e440aed9SQing Li * for RTM_GET, gate is optional even with multipath. 659e440aed9SQing Li * if gate == NULL the first match is returned. 660e440aed9SQing Li * (no need to call rt_mpath_matchgate if gate == NULL) 661e440aed9SQing Li */ 662e440aed9SQing Li if (rn_mpath_capable(rnh) && 663e440aed9SQing Li (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 664e440aed9SQing Li rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 665e440aed9SQing Li if (!rt) { 6663120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 667e440aed9SQing Li senderr(ESRCH); 668e440aed9SQing Li } 669e440aed9SQing Li } 670e440aed9SQing Li #endif 671c7ab6602SQing Li /* 672c7ab6602SQing Li * If performing proxied L2 entry insertion, and 673c7ab6602SQing Li * the actual PPP host entry is found, perform 674c7ab6602SQing Li * another search to retrieve the prefix route of 675c7ab6602SQing Li * the local end point of the PPP link. 676c7ab6602SQing Li */ 677*0ed6142bSQing Li if (rtm->rtm_flags & RTF_ANNOUNCE) { 678c7ab6602SQing Li struct sockaddr laddr; 679*0ed6142bSQing Li 680*0ed6142bSQing Li if (rt->rt_ifp != NULL && 681*0ed6142bSQing Li rt->rt_ifp->if_type == IFT_PROPVIRTUAL) { 682*0ed6142bSQing Li struct ifaddr *ifa; 683*0ed6142bSQing Li 684*0ed6142bSQing Li ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1); 685*0ed6142bSQing Li if (ifa != NULL) 686*0ed6142bSQing Li rt_maskedcopy(ifa->ifa_addr, 687*0ed6142bSQing Li &laddr, 688*0ed6142bSQing Li ifa->ifa_netmask); 689*0ed6142bSQing Li } else 690c7ab6602SQing Li rt_maskedcopy(rt->rt_ifa->ifa_addr, 691c7ab6602SQing Li &laddr, 692c7ab6602SQing Li rt->rt_ifa->ifa_netmask); 693c7ab6602SQing Li /* 694c7ab6602SQing Li * refactor rt and no lock operation necessary 695c7ab6602SQing Li */ 696c7ab6602SQing Li rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh); 697c7ab6602SQing Li if (rt == NULL) { 698c7ab6602SQing Li RADIX_NODE_HEAD_RUNLOCK(rnh); 699c7ab6602SQing Li senderr(ESRCH); 700c7ab6602SQing Li } 701c7ab6602SQing Li } 702d1dd20beSSam Leffler RT_LOCK(rt); 7037138d65cSSam Leffler RT_ADDREF(rt); 7043120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 705956b0b65SJeffrey Hsu 706ba7be0a9SGeorge V. Neville-Neil /* 707ba7be0a9SGeorge V. Neville-Neil * Fix for PR: 82974 708ba7be0a9SGeorge V. Neville-Neil * 709ba7be0a9SGeorge V. Neville-Neil * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 710ba7be0a9SGeorge V. Neville-Neil * returns a perfect match in case a netmask is 711ba7be0a9SGeorge V. Neville-Neil * specified. For host routes only a longest prefix 712ba7be0a9SGeorge V. Neville-Neil * match is returned so it is necessary to compare the 713ba7be0a9SGeorge V. Neville-Neil * existence of the netmask. If both have a netmask 714ba7be0a9SGeorge V. Neville-Neil * rnh_lookup() did a perfect match and if none of them 715ba7be0a9SGeorge V. Neville-Neil * have a netmask both are host routes which is also a 716ba7be0a9SGeorge V. Neville-Neil * perfect match. 717ba7be0a9SGeorge V. Neville-Neil */ 718ba7be0a9SGeorge V. Neville-Neil 719ba7be0a9SGeorge V. Neville-Neil if (rtm->rtm_type != RTM_GET && 720ba7be0a9SGeorge V. Neville-Neil (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 721ba7be0a9SGeorge V. Neville-Neil RT_UNLOCK(rt); 722ba7be0a9SGeorge V. Neville-Neil senderr(ESRCH); 723ba7be0a9SGeorge V. Neville-Neil } 724ba7be0a9SGeorge V. Neville-Neil 725df8bae1dSRodney W. Grimes switch(rtm->rtm_type) { 726df8bae1dSRodney W. Grimes 727df8bae1dSRodney W. Grimes case RTM_GET: 72878a82810SGarrett Wollman report: 729d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 730b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 731de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(curthread->td_ucred) 732b89e82ddSJamie Gritton : prison_if(curthread->td_ucred, 733b89e82ddSJamie Gritton rt_key(rt)) != 0) { 734813dd6aeSBjoern A. Zeeb RT_UNLOCK(rt); 735813dd6aeSBjoern A. Zeeb senderr(ESRCH); 736813dd6aeSBjoern A. Zeeb } 737becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 738becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 739becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 7406e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 741df8bae1dSRodney W. Grimes if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 742df440948SPoul-Henning Kamp ifp = rt->rt_ifp; 743df440948SPoul-Henning Kamp if (ifp) { 7449b98ee2cSLuigi Rizzo info.rti_info[RTAX_IFP] = 7454a0d6638SRuslan Ermilov ifp->if_addr->ifa_addr; 7469c79d243SJamie Gritton error = rtm_get_jailed(&info, ifp, rt, 7479c79d243SJamie Gritton &saun, curthread->td_ucred); 748413628a7SBjoern A. Zeeb if (error != 0) { 749413628a7SBjoern A. Zeeb RT_UNLOCK(rt); 7509c79d243SJamie Gritton senderr(error); 751413628a7SBjoern A. Zeeb } 75228070a0eSRuslan Ermilov if (ifp->if_flags & IFF_POINTOPOINT) 753becc44d7SSam Leffler info.rti_info[RTAX_BRD] = 754becc44d7SSam Leffler rt->rt_ifa->ifa_dstaddr; 755df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 756df8bae1dSRodney W. Grimes } else { 7575dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 7585dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFA] = NULL; 759df8bae1dSRodney W. Grimes } 76025029d6cSHartmut Brandt } else if ((ifp = rt->rt_ifp) != NULL) { 76125029d6cSHartmut Brandt rtm->rtm_index = ifp->if_index; 762df8bae1dSRodney W. Grimes } 763913af518SLuigi Rizzo len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 764df8bae1dSRodney W. Grimes if (len > rtm->rtm_msglen) { 765df8bae1dSRodney W. Grimes struct rt_msghdr *new_rtm; 766df8bae1dSRodney W. Grimes R_Malloc(new_rtm, struct rt_msghdr *, len); 7675dfc91d7SLuigi Rizzo if (new_rtm == NULL) { 768d1dd20beSSam Leffler RT_UNLOCK(rt); 769df8bae1dSRodney W. Grimes senderr(ENOBUFS); 770becc44d7SSam Leffler } 7716b96f1afSLuigi Rizzo bcopy(rtm, new_rtm, rtm->rtm_msglen); 772df8bae1dSRodney W. Grimes Free(rtm); rtm = new_rtm; 773df8bae1dSRodney W. Grimes } 774913af518SLuigi Rizzo (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 775df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 77697d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 777df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 778df8bae1dSRodney W. Grimes break; 779df8bae1dSRodney W. Grimes 780df8bae1dSRodney W. Grimes case RTM_CHANGE: 781becc44d7SSam Leffler /* 782becc44d7SSam Leffler * New gateway could require new ifaddr, ifp; 783becc44d7SSam Leffler * flags may also be different; ifp may be specified 784becc44d7SSam Leffler * by ll sockaddr when protocol address is ambiguous 785becc44d7SSam Leffler */ 786becc44d7SSam Leffler if (((rt->rt_flags & RTF_GATEWAY) && 787becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] != NULL) || 788becc44d7SSam Leffler info.rti_info[RTAX_IFP] != NULL || 789becc44d7SSam Leffler (info.rti_info[RTAX_IFA] != NULL && 790becc44d7SSam Leffler !sa_equal(info.rti_info[RTAX_IFA], 791becc44d7SSam Leffler rt->rt_ifa->ifa_addr))) { 792d1dd20beSSam Leffler RT_UNLOCK(rt); 7939b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 794a5a3926cSAndrew Thompson error = rt_getifa_fib(&info, rt->rt_fibnum); 7958c0fec80SRobert Watson /* 7968c0fec80SRobert Watson * XXXRW: Really we should release this 7978c0fec80SRobert Watson * reference later, but this maintains 7988c0fec80SRobert Watson * historical behavior. 7998c0fec80SRobert Watson */ 8008c0fec80SRobert Watson if (info.rti_ifa != NULL) 8018c0fec80SRobert Watson ifa_free(info.rti_ifa); 8029b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 803a5a3926cSAndrew Thompson if (error != 0) 804a5a3926cSAndrew Thompson senderr(error); 805a11faa9fSGleb Smirnoff RT_LOCK(rt); 806becc44d7SSam Leffler } 8075de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8085de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa && 8095de55821SGleb Smirnoff rt->rt_ifa != NULL && 8109406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest != NULL) { 8119406b274SGleb Smirnoff rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 8129406b274SGleb Smirnoff &info); 8131099f828SRobert Watson ifa_free(rt->rt_ifa); 8149406b274SGleb Smirnoff } 8159406b274SGleb Smirnoff if (info.rti_info[RTAX_GATEWAY] != NULL) { 8169b20205dSKip Macy RT_UNLOCK(rt); 8179b20205dSKip Macy RADIX_NODE_HEAD_LOCK(rnh); 8189b20205dSKip Macy RT_LOCK(rt); 8199b20205dSKip Macy 8209b20205dSKip Macy error = rt_setgate(rt, rt_key(rt), 8219b20205dSKip Macy info.rti_info[RTAX_GATEWAY]); 8229b20205dSKip Macy RADIX_NODE_HEAD_UNLOCK(rnh); 8239b20205dSKip Macy if (error != 0) { 824d1dd20beSSam Leffler RT_UNLOCK(rt); 8258071913dSRuslan Ermilov senderr(error); 826becc44d7SSam Leffler } 8279231d35fSQing Li rt->rt_flags |= (RTF_GATEWAY & info.rti_flags); 82819fc74fbSJeffrey Hsu } 8295de55821SGleb Smirnoff if (info.rti_ifa != NULL && 8305de55821SGleb Smirnoff info.rti_ifa != rt->rt_ifa) { 8311099f828SRobert Watson ifa_ref(info.rti_ifa); 8325de55821SGleb Smirnoff rt->rt_ifa = info.rti_ifa; 8338071913dSRuslan Ermilov rt->rt_ifp = info.rti_ifp; 834df8bae1dSRodney W. Grimes } 83522cafcf0SAndre Oppermann /* Allow some flags to be toggled on change. */ 836427ac07fSKip Macy rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) | 837427ac07fSKip Macy (rtm->rtm_flags & RTF_FMASK); 838df8bae1dSRodney W. Grimes rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 839df8bae1dSRodney W. Grimes &rt->rt_rmx); 84022cafcf0SAndre Oppermann rtm->rtm_index = rt->rt_ifp->if_index; 841df8bae1dSRodney W. Grimes if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 8428071913dSRuslan Ermilov rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 84393b0017fSPhilippe Charnier /* FALLTHROUGH */ 844df8bae1dSRodney W. Grimes case RTM_LOCK: 84597d8d152SAndre Oppermann /* We don't support locks anymore */ 846df8bae1dSRodney W. Grimes break; 847df8bae1dSRodney W. Grimes } 848d1dd20beSSam Leffler RT_UNLOCK(rt); 849df8bae1dSRodney W. Grimes break; 850df8bae1dSRodney W. Grimes 851df8bae1dSRodney W. Grimes default: 852df8bae1dSRodney W. Grimes senderr(EOPNOTSUPP); 853df8bae1dSRodney W. Grimes } 854df8bae1dSRodney W. Grimes 855df8bae1dSRodney W. Grimes flush: 856df8bae1dSRodney W. Grimes if (rtm) { 857df8bae1dSRodney W. Grimes if (error) 858df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 859df8bae1dSRodney W. Grimes else 860df8bae1dSRodney W. Grimes rtm->rtm_flags |= RTF_DONE; 861df8bae1dSRodney W. Grimes } 862becc44d7SSam Leffler if (rt) /* XXX can this be true? */ 863becc44d7SSam Leffler RTFREE(rt); 864df8bae1dSRodney W. Grimes { 8655dfc91d7SLuigi Rizzo struct rawcb *rp = NULL; 866df8bae1dSRodney W. Grimes /* 867df8bae1dSRodney W. Grimes * Check to see if we don't want our own messages. 868df8bae1dSRodney W. Grimes */ 869df8bae1dSRodney W. Grimes if ((so->so_options & SO_USELOOPBACK) == 0) { 870df8bae1dSRodney W. Grimes if (route_cb.any_count <= 1) { 871df8bae1dSRodney W. Grimes if (rtm) 872df8bae1dSRodney W. Grimes Free(rtm); 873df8bae1dSRodney W. Grimes m_freem(m); 874df8bae1dSRodney W. Grimes return (error); 875df8bae1dSRodney W. Grimes } 876df8bae1dSRodney W. Grimes /* There is another listener, so construct message */ 877df8bae1dSRodney W. Grimes rp = sotorawcb(so); 8784cc20ab1SSeigo Tanimura } 879df8bae1dSRodney W. Grimes if (rtm) { 880df8bae1dSRodney W. Grimes m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 88103311056SHajimu UMEMOTO if (m->m_pkthdr.len < rtm->rtm_msglen) { 88203311056SHajimu UMEMOTO m_freem(m); 88303311056SHajimu UMEMOTO m = NULL; 88403311056SHajimu UMEMOTO } else if (m->m_pkthdr.len > rtm->rtm_msglen) 88503311056SHajimu UMEMOTO m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 886df8bae1dSRodney W. Grimes Free(rtm); 887df8bae1dSRodney W. Grimes } 888becc44d7SSam Leffler if (m) { 889becc44d7SSam Leffler if (rp) { 890becc44d7SSam Leffler /* 891becc44d7SSam Leffler * XXX insure we don't get a copy by 892becc44d7SSam Leffler * invalidating our protocol 893becc44d7SSam Leffler */ 894becc44d7SSam Leffler unsigned short family = rp->rcb_proto.sp_family; 895becc44d7SSam Leffler rp->rcb_proto.sp_family = 0; 896becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 897becc44d7SSam Leffler rp->rcb_proto.sp_family = family; 898becc44d7SSam Leffler } else 899becc44d7SSam Leffler rt_dispatch(m, info.rti_info[RTAX_DST]); 900becc44d7SSam Leffler } 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes return (error); 903becc44d7SSam Leffler #undef sa_equal 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes 90652041295SPoul-Henning Kamp static void 9075dfc91d7SLuigi Rizzo rt_setmetrics(u_long which, const struct rt_metrics *in, 9085dfc91d7SLuigi Rizzo struct rt_metrics_lite *out) 909df8bae1dSRodney W. Grimes { 910df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e; 91197d8d152SAndre Oppermann /* 91297d8d152SAndre Oppermann * Only these are stored in the routing entry since introduction 91397d8d152SAndre Oppermann * of tcp hostcache. The rest is ignored. 91497d8d152SAndre Oppermann */ 915df8bae1dSRodney W. Grimes metric(RTV_MTU, rmx_mtu); 916427ac07fSKip Macy metric(RTV_WEIGHT, rmx_weight); 917e27c3f48SOleg Bulyzhin /* Userland -> kernel timebase conversion. */ 918e27c3f48SOleg Bulyzhin if (which & RTV_EXPIRE) 919e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 920e27c3f48SOleg Bulyzhin in->rmx_expire - time_second + time_uptime : 0; 921df8bae1dSRodney W. Grimes #undef metric 922df8bae1dSRodney W. Grimes } 923df8bae1dSRodney W. Grimes 92497d8d152SAndre Oppermann static void 9255dfc91d7SLuigi Rizzo rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 92697d8d152SAndre Oppermann { 92797d8d152SAndre Oppermann #define metric(e) out->e = in->e; 92897d8d152SAndre Oppermann bzero(out, sizeof(*out)); 92997d8d152SAndre Oppermann metric(rmx_mtu); 930427ac07fSKip Macy metric(rmx_weight); 931e27c3f48SOleg Bulyzhin /* Kernel -> userland timebase conversion. */ 932e27c3f48SOleg Bulyzhin out->rmx_expire = in->rmx_expire ? 933e27c3f48SOleg Bulyzhin in->rmx_expire - time_uptime + time_second : 0; 93497d8d152SAndre Oppermann #undef metric 93597d8d152SAndre Oppermann } 93697d8d152SAndre Oppermann 9377f33a738SJulian Elischer /* 9387f33a738SJulian Elischer * Extract the addresses of the passed sockaddrs. 9397f33a738SJulian Elischer * Do a little sanity checking so as to avoid bad memory references. 940076d0761SJulian Elischer * This data is derived straight from userland. 9417f33a738SJulian Elischer */ 942076d0761SJulian Elischer static int 943becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 944df8bae1dSRodney W. Grimes { 945e74642dfSLuigi Rizzo struct sockaddr *sa; 946e74642dfSLuigi Rizzo int i; 947df8bae1dSRodney W. Grimes 948becc44d7SSam Leffler for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 949df8bae1dSRodney W. Grimes if ((rtinfo->rti_addrs & (1 << i)) == 0) 950df8bae1dSRodney W. Grimes continue; 951ff6d0a59SJulian Elischer sa = (struct sockaddr *)cp; 9527f33a738SJulian Elischer /* 953076d0761SJulian Elischer * It won't fit. 9547f33a738SJulian Elischer */ 955becc44d7SSam Leffler if (cp + sa->sa_len > cplim) 956076d0761SJulian Elischer return (EINVAL); 9577f33a738SJulian Elischer /* 9587f33a738SJulian Elischer * there are no more.. quit now 9597f33a738SJulian Elischer * If there are more bits, they are in error. 9607f33a738SJulian Elischer * I've seen this. route(1) can evidently generate these. 9617f33a738SJulian Elischer * This causes kernel to core dump. 962076d0761SJulian Elischer * for compatibility, If we see this, point to a safe address. 9637f33a738SJulian Elischer */ 964076d0761SJulian Elischer if (sa->sa_len == 0) { 965076d0761SJulian Elischer rtinfo->rti_info[i] = &sa_zero; 966076d0761SJulian Elischer return (0); /* should be EINVAL but for compat */ 967df8bae1dSRodney W. Grimes } 968076d0761SJulian Elischer /* accept it */ 969076d0761SJulian Elischer rtinfo->rti_info[i] = sa; 970e74642dfSLuigi Rizzo cp += SA_SIZE(sa); 971076d0761SJulian Elischer } 972076d0761SJulian Elischer return (0); 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes 975df8bae1dSRodney W. Grimes static struct mbuf * 976becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo) 977df8bae1dSRodney W. Grimes { 9785dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 9795dfc91d7SLuigi Rizzo struct mbuf *m; 9805dfc91d7SLuigi Rizzo int i; 9815dfc91d7SLuigi Rizzo struct sockaddr *sa; 982df8bae1dSRodney W. Grimes int len, dlen; 983df8bae1dSRodney W. Grimes 984df8bae1dSRodney W. Grimes switch (type) { 985df8bae1dSRodney W. Grimes 986df8bae1dSRodney W. Grimes case RTM_DELADDR: 987df8bae1dSRodney W. Grimes case RTM_NEWADDR: 988df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 989df8bae1dSRodney W. Grimes break; 990df8bae1dSRodney W. Grimes 991477180fbSGarrett Wollman case RTM_DELMADDR: 992477180fbSGarrett Wollman case RTM_NEWMADDR: 993477180fbSGarrett Wollman len = sizeof(struct ifma_msghdr); 994477180fbSGarrett Wollman break; 995477180fbSGarrett Wollman 996df8bae1dSRodney W. Grimes case RTM_IFINFO: 997df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 998df8bae1dSRodney W. Grimes break; 999df8bae1dSRodney W. Grimes 10007b6edd04SRuslan Ermilov case RTM_IFANNOUNCE: 1001b83a279fSSam Leffler case RTM_IEEE80211: 10027b6edd04SRuslan Ermilov len = sizeof(struct if_announcemsghdr); 10037b6edd04SRuslan Ermilov break; 10047b6edd04SRuslan Ermilov 1005df8bae1dSRodney W. Grimes default: 1006df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1007df8bae1dSRodney W. Grimes } 100833841545SHajimu UMEMOTO if (len > MCLBYTES) 1009df8bae1dSRodney W. Grimes panic("rt_msg1"); 1010a163d034SWarner Losh m = m_gethdr(M_DONTWAIT, MT_DATA); 101133841545SHajimu UMEMOTO if (m && len > MHLEN) { 1012a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 101333841545SHajimu UMEMOTO if ((m->m_flags & M_EXT) == 0) { 101433841545SHajimu UMEMOTO m_free(m); 101533841545SHajimu UMEMOTO m = NULL; 101633841545SHajimu UMEMOTO } 101733841545SHajimu UMEMOTO } 10185dfc91d7SLuigi Rizzo if (m == NULL) 101933841545SHajimu UMEMOTO return (m); 1020df8bae1dSRodney W. Grimes m->m_pkthdr.len = m->m_len = len; 10215dfc91d7SLuigi Rizzo m->m_pkthdr.rcvif = NULL; 1022df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1023df8bae1dSRodney W. Grimes bzero((caddr_t)rtm, len); 1024df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 1025df8bae1dSRodney W. Grimes if ((sa = rtinfo->rti_info[i]) == NULL) 1026df8bae1dSRodney W. Grimes continue; 1027df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1028e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1029df8bae1dSRodney W. Grimes m_copyback(m, len, dlen, (caddr_t)sa); 1030df8bae1dSRodney W. Grimes len += dlen; 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes if (m->m_pkthdr.len != len) { 1033df8bae1dSRodney W. Grimes m_freem(m); 1034df8bae1dSRodney W. Grimes return (NULL); 1035df8bae1dSRodney W. Grimes } 1036df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1037df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1038df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1039df8bae1dSRodney W. Grimes return (m); 1040df8bae1dSRodney W. Grimes } 1041df8bae1dSRodney W. Grimes 1042df8bae1dSRodney W. Grimes static int 1043becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 1044df8bae1dSRodney W. Grimes { 10455dfc91d7SLuigi Rizzo int i; 1046df8bae1dSRodney W. Grimes int len, dlen, second_time = 0; 1047df8bae1dSRodney W. Grimes caddr_t cp0; 1048df8bae1dSRodney W. Grimes 1049df8bae1dSRodney W. Grimes rtinfo->rti_addrs = 0; 1050df8bae1dSRodney W. Grimes again: 1051df8bae1dSRodney W. Grimes switch (type) { 1052df8bae1dSRodney W. Grimes 1053df8bae1dSRodney W. Grimes case RTM_DELADDR: 1054df8bae1dSRodney W. Grimes case RTM_NEWADDR: 1055df8bae1dSRodney W. Grimes len = sizeof(struct ifa_msghdr); 1056df8bae1dSRodney W. Grimes break; 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes case RTM_IFINFO: 1059427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1060427a928aSKonstantin Belousov if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1061427a928aSKonstantin Belousov len = sizeof(struct if_msghdr32); 1062427a928aSKonstantin Belousov break; 1063427a928aSKonstantin Belousov } 1064427a928aSKonstantin Belousov #endif 1065df8bae1dSRodney W. Grimes len = sizeof(struct if_msghdr); 1066df8bae1dSRodney W. Grimes break; 1067df8bae1dSRodney W. Grimes 106805b2efe0SBruce M Simpson case RTM_NEWMADDR: 106905b2efe0SBruce M Simpson len = sizeof(struct ifma_msghdr); 107005b2efe0SBruce M Simpson break; 107105b2efe0SBruce M Simpson 1072df8bae1dSRodney W. Grimes default: 1073df8bae1dSRodney W. Grimes len = sizeof(struct rt_msghdr); 1074df8bae1dSRodney W. Grimes } 1075df440948SPoul-Henning Kamp cp0 = cp; 1076df440948SPoul-Henning Kamp if (cp0) 1077df8bae1dSRodney W. Grimes cp += len; 1078df8bae1dSRodney W. Grimes for (i = 0; i < RTAX_MAX; i++) { 10795dfc91d7SLuigi Rizzo struct sockaddr *sa; 1080df8bae1dSRodney W. Grimes 10815dfc91d7SLuigi Rizzo if ((sa = rtinfo->rti_info[i]) == NULL) 1082df8bae1dSRodney W. Grimes continue; 1083df8bae1dSRodney W. Grimes rtinfo->rti_addrs |= (1 << i); 1084e74642dfSLuigi Rizzo dlen = SA_SIZE(sa); 1085df8bae1dSRodney W. Grimes if (cp) { 1086df8bae1dSRodney W. Grimes bcopy((caddr_t)sa, cp, (unsigned)dlen); 1087df8bae1dSRodney W. Grimes cp += dlen; 1088df8bae1dSRodney W. Grimes } 1089df8bae1dSRodney W. Grimes len += dlen; 1090df8bae1dSRodney W. Grimes } 1091694ff264SAndrew Gallatin len = ALIGN(len); 10925dfc91d7SLuigi Rizzo if (cp == NULL && w != NULL && !second_time) { 10935dfc91d7SLuigi Rizzo struct walkarg *rw = w; 1094df8bae1dSRodney W. Grimes 109552041295SPoul-Henning Kamp if (rw->w_req) { 1096df8bae1dSRodney W. Grimes if (rw->w_tmemsize < len) { 1097df8bae1dSRodney W. Grimes if (rw->w_tmem) 1098df8bae1dSRodney W. Grimes free(rw->w_tmem, M_RTABLE); 1099df440948SPoul-Henning Kamp rw->w_tmem = (caddr_t) 1100df440948SPoul-Henning Kamp malloc(len, M_RTABLE, M_NOWAIT); 1101df440948SPoul-Henning Kamp if (rw->w_tmem) 1102df8bae1dSRodney W. Grimes rw->w_tmemsize = len; 1103df8bae1dSRodney W. Grimes } 1104df8bae1dSRodney W. Grimes if (rw->w_tmem) { 1105df8bae1dSRodney W. Grimes cp = rw->w_tmem; 1106df8bae1dSRodney W. Grimes second_time = 1; 1107df8bae1dSRodney W. Grimes goto again; 110852041295SPoul-Henning Kamp } 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes } 1111df8bae1dSRodney W. Grimes if (cp) { 11125dfc91d7SLuigi Rizzo struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes rtm->rtm_version = RTM_VERSION; 1115df8bae1dSRodney W. Grimes rtm->rtm_type = type; 1116df8bae1dSRodney W. Grimes rtm->rtm_msglen = len; 1117df8bae1dSRodney W. Grimes } 1118df8bae1dSRodney W. Grimes return (len); 1119df8bae1dSRodney W. Grimes } 1120df8bae1dSRodney W. Grimes 1121df8bae1dSRodney W. Grimes /* 1122df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1123df8bae1dSRodney W. Grimes * socket indicating that a redirect has occured, a routing lookup 1124df8bae1dSRodney W. Grimes * has failed, or that a protocol has detected timeouts to a particular 1125df8bae1dSRodney W. Grimes * destination. 1126df8bae1dSRodney W. Grimes */ 1127df8bae1dSRodney W. Grimes void 1128becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1129df8bae1dSRodney W. Grimes { 1130becc44d7SSam Leffler struct rt_msghdr *rtm; 1131becc44d7SSam Leffler struct mbuf *m; 1132df8bae1dSRodney W. Grimes struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1133df8bae1dSRodney W. Grimes 1134df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1135df8bae1dSRodney W. Grimes return; 1136df8bae1dSRodney W. Grimes m = rt_msg1(type, rtinfo); 11375dfc91d7SLuigi Rizzo if (m == NULL) 1138df8bae1dSRodney W. Grimes return; 1139df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1140df8bae1dSRodney W. Grimes rtm->rtm_flags = RTF_DONE | flags; 1141df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1142df8bae1dSRodney W. Grimes rtm->rtm_addrs = rtinfo->rti_addrs; 1143becc44d7SSam Leffler rt_dispatch(m, sa); 1144df8bae1dSRodney W. Grimes } 1145df8bae1dSRodney W. Grimes 1146df8bae1dSRodney W. Grimes /* 1147df8bae1dSRodney W. Grimes * This routine is called to generate a message from the routing 1148df8bae1dSRodney W. Grimes * socket indicating that the status of a network interface has changed. 1149df8bae1dSRodney W. Grimes */ 1150df8bae1dSRodney W. Grimes void 1151becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp) 1152df8bae1dSRodney W. Grimes { 1153becc44d7SSam Leffler struct if_msghdr *ifm; 1154df8bae1dSRodney W. Grimes struct mbuf *m; 1155df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1158df8bae1dSRodney W. Grimes return; 1159df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1160df8bae1dSRodney W. Grimes m = rt_msg1(RTM_IFINFO, &info); 11615dfc91d7SLuigi Rizzo if (m == NULL) 1162df8bae1dSRodney W. Grimes return; 1163df8bae1dSRodney W. Grimes ifm = mtod(m, struct if_msghdr *); 1164df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1165292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1166df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1167df8bae1dSRodney W. Grimes ifm->ifm_addrs = 0; 1168becc44d7SSam Leffler rt_dispatch(m, NULL); 1169df8bae1dSRodney W. Grimes } 1170df8bae1dSRodney W. Grimes 1171df8bae1dSRodney W. Grimes /* 1172df8bae1dSRodney W. Grimes * This is called to generate messages from the routing socket 1173df8bae1dSRodney W. Grimes * indicating a network interface has had addresses associated with it. 1174df8bae1dSRodney W. Grimes * if we ever reverse the logic and replace messages TO the routing 1175df8bae1dSRodney W. Grimes * socket indicate a request to configure interfaces, then it will 1176df8bae1dSRodney W. Grimes * be unnecessary as the routing socket will automatically generate 1177df8bae1dSRodney W. Grimes * copies of it. 1178df8bae1dSRodney W. Grimes */ 1179df8bae1dSRodney W. Grimes void 1180becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1181df8bae1dSRodney W. Grimes { 1182df8bae1dSRodney W. Grimes struct rt_addrinfo info; 11835dfc91d7SLuigi Rizzo struct sockaddr *sa = NULL; 1184df8bae1dSRodney W. Grimes int pass; 11855dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1186df8bae1dSRodney W. Grimes struct ifnet *ifp = ifa->ifa_ifp; 1187df8bae1dSRodney W. Grimes 11887a7fa27bSSam Leffler KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 11897a7fa27bSSam Leffler ("unexpected cmd %u", cmd)); 1190c03528b6SBjoern A. Zeeb #if defined(INET) || defined(INET6) 1191f8829a4aSRandall Stewart #ifdef SCTP 1192f8829a4aSRandall Stewart /* 1193f8829a4aSRandall Stewart * notify the SCTP stack 1194f8829a4aSRandall Stewart * this will only get called when an address is added/deleted 1195f8829a4aSRandall Stewart * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1196f8829a4aSRandall Stewart */ 1197f8829a4aSRandall Stewart sctp_addr_change(ifa, cmd); 1198f8829a4aSRandall Stewart #endif /* SCTP */ 1199c03528b6SBjoern A. Zeeb #endif 1200df8bae1dSRodney W. Grimes if (route_cb.any_count == 0) 1201df8bae1dSRodney W. Grimes return; 1202df8bae1dSRodney W. Grimes for (pass = 1; pass < 3; pass++) { 1203df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1204df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 1) || 1205df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 2)) { 12065dfc91d7SLuigi Rizzo struct ifa_msghdr *ifam; 1207df8bae1dSRodney W. Grimes int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1208df8bae1dSRodney W. Grimes 1209becc44d7SSam Leffler info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 12104a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1211becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1212becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1213df8bae1dSRodney W. Grimes if ((m = rt_msg1(ncmd, &info)) == NULL) 1214df8bae1dSRodney W. Grimes continue; 1215df8bae1dSRodney W. Grimes ifam = mtod(m, struct ifa_msghdr *); 1216df8bae1dSRodney W. Grimes ifam->ifam_index = ifp->if_index; 1217df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1218df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1219df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 1220df8bae1dSRodney W. Grimes } 1221df8bae1dSRodney W. Grimes if ((cmd == RTM_ADD && pass == 2) || 1222df8bae1dSRodney W. Grimes (cmd == RTM_DELETE && pass == 1)) { 12235dfc91d7SLuigi Rizzo struct rt_msghdr *rtm; 1224df8bae1dSRodney W. Grimes 12255dfc91d7SLuigi Rizzo if (rt == NULL) 1226df8bae1dSRodney W. Grimes continue; 1227becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1228becc44d7SSam Leffler info.rti_info[RTAX_DST] = sa = rt_key(rt); 1229becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1230df8bae1dSRodney W. Grimes if ((m = rt_msg1(cmd, &info)) == NULL) 1231df8bae1dSRodney W. Grimes continue; 1232df8bae1dSRodney W. Grimes rtm = mtod(m, struct rt_msghdr *); 1233df8bae1dSRodney W. Grimes rtm->rtm_index = ifp->if_index; 1234df8bae1dSRodney W. Grimes rtm->rtm_flags |= rt->rt_flags; 1235df8bae1dSRodney W. Grimes rtm->rtm_errno = error; 1236df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 1237df8bae1dSRodney W. Grimes } 1238becc44d7SSam Leffler rt_dispatch(m, sa); 1239df8bae1dSRodney W. Grimes } 1240df8bae1dSRodney W. Grimes } 1241df8bae1dSRodney W. Grimes 1242477180fbSGarrett Wollman /* 1243477180fbSGarrett Wollman * This is the analogue to the rt_newaddrmsg which performs the same 1244477180fbSGarrett Wollman * function but for multicast group memberhips. This is easier since 1245477180fbSGarrett Wollman * there is no route state to worry about. 1246477180fbSGarrett Wollman */ 1247477180fbSGarrett Wollman void 1248becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1249477180fbSGarrett Wollman { 1250477180fbSGarrett Wollman struct rt_addrinfo info; 12515dfc91d7SLuigi Rizzo struct mbuf *m = NULL; 1252477180fbSGarrett Wollman struct ifnet *ifp = ifma->ifma_ifp; 1253477180fbSGarrett Wollman struct ifma_msghdr *ifmam; 1254477180fbSGarrett Wollman 1255477180fbSGarrett Wollman if (route_cb.any_count == 0) 1256477180fbSGarrett Wollman return; 1257477180fbSGarrett Wollman 1258477180fbSGarrett Wollman bzero((caddr_t)&info, sizeof(info)); 1259becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifma->ifma_addr; 12604a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 1261477180fbSGarrett Wollman /* 1262477180fbSGarrett Wollman * If a link-layer address is present, present it as a ``gateway'' 1263477180fbSGarrett Wollman * (similarly to how ARP entries, e.g., are presented). 1264477180fbSGarrett Wollman */ 1265becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1266becc44d7SSam Leffler m = rt_msg1(cmd, &info); 1267becc44d7SSam Leffler if (m == NULL) 1268477180fbSGarrett Wollman return; 1269477180fbSGarrett Wollman ifmam = mtod(m, struct ifma_msghdr *); 127075ae0c01SBruce M Simpson KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 127175ae0c01SBruce M Simpson __func__)); 1272477180fbSGarrett Wollman ifmam->ifmam_index = ifp->if_index; 1273477180fbSGarrett Wollman ifmam->ifmam_addrs = info.rti_addrs; 1274becc44d7SSam Leffler rt_dispatch(m, ifma->ifma_addr); 1275477180fbSGarrett Wollman } 127652041295SPoul-Henning Kamp 1277b83a279fSSam Leffler static struct mbuf * 1278b83a279fSSam Leffler rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1279b83a279fSSam Leffler struct rt_addrinfo *info) 1280b83a279fSSam Leffler { 1281b83a279fSSam Leffler struct if_announcemsghdr *ifan; 1282b83a279fSSam Leffler struct mbuf *m; 1283b83a279fSSam Leffler 1284b83a279fSSam Leffler if (route_cb.any_count == 0) 1285b83a279fSSam Leffler return NULL; 1286b83a279fSSam Leffler bzero((caddr_t)info, sizeof(*info)); 1287b83a279fSSam Leffler m = rt_msg1(type, info); 1288b83a279fSSam Leffler if (m != NULL) { 1289b83a279fSSam Leffler ifan = mtod(m, struct if_announcemsghdr *); 1290b83a279fSSam Leffler ifan->ifan_index = ifp->if_index; 1291b83a279fSSam Leffler strlcpy(ifan->ifan_name, ifp->if_xname, 1292b83a279fSSam Leffler sizeof(ifan->ifan_name)); 1293b83a279fSSam Leffler ifan->ifan_what = what; 1294b83a279fSSam Leffler } 1295b83a279fSSam Leffler return m; 1296b83a279fSSam Leffler } 1297b83a279fSSam Leffler 1298b83a279fSSam Leffler /* 1299b83a279fSSam Leffler * This is called to generate routing socket messages indicating 1300b83a279fSSam Leffler * IEEE80211 wireless events. 1301b83a279fSSam Leffler * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1302b83a279fSSam Leffler */ 1303b83a279fSSam Leffler void 1304b83a279fSSam Leffler rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1305b83a279fSSam Leffler { 1306b83a279fSSam Leffler struct mbuf *m; 1307b83a279fSSam Leffler struct rt_addrinfo info; 1308b83a279fSSam Leffler 1309b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1310b83a279fSSam Leffler if (m != NULL) { 1311b83a279fSSam Leffler /* 1312b83a279fSSam Leffler * Append the ieee80211 data. Try to stick it in the 1313b83a279fSSam Leffler * mbuf containing the ifannounce msg; otherwise allocate 1314b83a279fSSam Leffler * a new mbuf and append. 1315b83a279fSSam Leffler * 1316b83a279fSSam Leffler * NB: we assume m is a single mbuf. 1317b83a279fSSam Leffler */ 1318b83a279fSSam Leffler if (data_len > M_TRAILINGSPACE(m)) { 1319b83a279fSSam Leffler struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1320b83a279fSSam Leffler if (n == NULL) { 1321b83a279fSSam Leffler m_freem(m); 1322b83a279fSSam Leffler return; 1323b83a279fSSam Leffler } 1324b83a279fSSam Leffler bcopy(data, mtod(n, void *), data_len); 1325b83a279fSSam Leffler n->m_len = data_len; 1326b83a279fSSam Leffler m->m_next = n; 1327b83a279fSSam Leffler } else if (data_len > 0) { 1328b83a279fSSam Leffler bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1329b83a279fSSam Leffler m->m_len += data_len; 1330b83a279fSSam Leffler } 1331b83a279fSSam Leffler if (m->m_flags & M_PKTHDR) 1332b83a279fSSam Leffler m->m_pkthdr.len += data_len; 1333b83a279fSSam Leffler mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1334b83a279fSSam Leffler rt_dispatch(m, NULL); 1335b83a279fSSam Leffler } 1336b83a279fSSam Leffler } 1337b83a279fSSam Leffler 1338df8bae1dSRodney W. Grimes /* 13397b6edd04SRuslan Ermilov * This is called to generate routing socket messages indicating 13407b6edd04SRuslan Ermilov * network interface arrival and departure. 13417b6edd04SRuslan Ermilov */ 13427b6edd04SRuslan Ermilov void 1343becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what) 13447b6edd04SRuslan Ermilov { 13457b6edd04SRuslan Ermilov struct mbuf *m; 13467b6edd04SRuslan Ermilov struct rt_addrinfo info; 13477b6edd04SRuslan Ermilov 1348b83a279fSSam Leffler m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1349b83a279fSSam Leffler if (m != NULL) 1350becc44d7SSam Leffler rt_dispatch(m, NULL); 1351becc44d7SSam Leffler } 1352becc44d7SSam Leffler 1353becc44d7SSam Leffler static void 13545dfc91d7SLuigi Rizzo rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 1355becc44d7SSam Leffler { 1356d989c7b3SRobert Watson struct m_tag *tag; 1357becc44d7SSam Leffler 1358d989c7b3SRobert Watson /* 1359d989c7b3SRobert Watson * Preserve the family from the sockaddr, if any, in an m_tag for 1360d989c7b3SRobert Watson * use when injecting the mbuf into the routing socket buffer from 1361d989c7b3SRobert Watson * the netisr. 1362d989c7b3SRobert Watson */ 1363d989c7b3SRobert Watson if (sa != NULL) { 1364d989c7b3SRobert Watson tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1365d989c7b3SRobert Watson M_NOWAIT); 1366d989c7b3SRobert Watson if (tag == NULL) { 1367d989c7b3SRobert Watson m_freem(m); 1368d989c7b3SRobert Watson return; 1369d989c7b3SRobert Watson } 13708d78bea4SSam Leffler *(unsigned short *)(tag + 1) = sa->sa_family; 1371d989c7b3SRobert Watson m_tag_prepend(m, tag); 1372d989c7b3SRobert Watson } 137321ca7b57SMarko Zec #ifdef VIMAGE 137421ca7b57SMarko Zec if (V_loif) 137521ca7b57SMarko Zec m->m_pkthdr.rcvif = V_loif; 137621ca7b57SMarko Zec else { 137721ca7b57SMarko Zec m_freem(m); 137821ca7b57SMarko Zec return; 137921ca7b57SMarko Zec } 138021ca7b57SMarko Zec #endif 13813161f583SAndre Oppermann netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 13827b6edd04SRuslan Ermilov } 13837b6edd04SRuslan Ermilov 13847b6edd04SRuslan Ermilov /* 1385df8bae1dSRodney W. Grimes * This is used in dumping the kernel table via sysctl(). 1386df8bae1dSRodney W. Grimes */ 138737c84183SPoul-Henning Kamp static int 1388becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw) 1389df8bae1dSRodney W. Grimes { 1390becc44d7SSam Leffler struct walkarg *w = vw; 1391becc44d7SSam Leffler struct rtentry *rt = (struct rtentry *)rn; 1392df8bae1dSRodney W. Grimes int error = 0, size; 1393df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1394df8bae1dSRodney W. Grimes 1395df8bae1dSRodney W. Grimes if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1396df8bae1dSRodney W. Grimes return 0; 1397b89e82ddSJamie Gritton if ((rt->rt_flags & RTF_HOST) == 0 1398de0bd6f7SBjoern A. Zeeb ? jailed_without_vnet(w->w_req->td->td_ucred) 1399b89e82ddSJamie Gritton : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1400813dd6aeSBjoern A. Zeeb return (0); 1401df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1402becc44d7SSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 1403becc44d7SSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1404becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 14056e6b3f7cSQing Li info.rti_info[RTAX_GENMASK] = 0; 140628070a0eSRuslan Ermilov if (rt->rt_ifp) { 14074a0d6638SRuslan Ermilov info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1408becc44d7SSam Leffler info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 140928070a0eSRuslan Ermilov if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1410becc44d7SSam Leffler info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 141128070a0eSRuslan Ermilov } 1412913af518SLuigi Rizzo size = rt_msg2(RTM_GET, &info, NULL, w); 141352041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1414becc44d7SSam Leffler struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1415df8bae1dSRodney W. Grimes 1416df8bae1dSRodney W. Grimes rtm->rtm_flags = rt->rt_flags; 1417427ac07fSKip Macy /* 1418427ac07fSKip Macy * let's be honest about this being a retarded hack 1419427ac07fSKip Macy */ 1420427ac07fSKip Macy rtm->rtm_fmask = rt->rt_rmx.rmx_pksent; 142197d8d152SAndre Oppermann rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1422df8bae1dSRodney W. Grimes rtm->rtm_index = rt->rt_ifp->if_index; 1423df8bae1dSRodney W. Grimes rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1424df8bae1dSRodney W. Grimes rtm->rtm_addrs = info.rti_addrs; 142552041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 142652041295SPoul-Henning Kamp return (error); 1427df8bae1dSRodney W. Grimes } 1428df8bae1dSRodney W. Grimes return (error); 1429df8bae1dSRodney W. Grimes } 1430df8bae1dSRodney W. Grimes 1431427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1432427a928aSKonstantin Belousov static void 1433427a928aSKonstantin Belousov copy_ifdata32(struct if_data *src, struct if_data32 *dst) 1434427a928aSKonstantin Belousov { 1435427a928aSKonstantin Belousov 1436427a928aSKonstantin Belousov bzero(dst, sizeof(*dst)); 1437427a928aSKonstantin Belousov CP(*src, *dst, ifi_type); 1438427a928aSKonstantin Belousov CP(*src, *dst, ifi_physical); 1439427a928aSKonstantin Belousov CP(*src, *dst, ifi_addrlen); 1440427a928aSKonstantin Belousov CP(*src, *dst, ifi_hdrlen); 1441427a928aSKonstantin Belousov CP(*src, *dst, ifi_link_state); 1442427a928aSKonstantin Belousov CP(*src, *dst, ifi_datalen); 1443427a928aSKonstantin Belousov CP(*src, *dst, ifi_mtu); 1444427a928aSKonstantin Belousov CP(*src, *dst, ifi_metric); 1445427a928aSKonstantin Belousov CP(*src, *dst, ifi_baudrate); 1446427a928aSKonstantin Belousov CP(*src, *dst, ifi_ipackets); 1447427a928aSKonstantin Belousov CP(*src, *dst, ifi_ierrors); 1448427a928aSKonstantin Belousov CP(*src, *dst, ifi_opackets); 1449427a928aSKonstantin Belousov CP(*src, *dst, ifi_oerrors); 1450427a928aSKonstantin Belousov CP(*src, *dst, ifi_collisions); 1451427a928aSKonstantin Belousov CP(*src, *dst, ifi_ibytes); 1452427a928aSKonstantin Belousov CP(*src, *dst, ifi_obytes); 1453427a928aSKonstantin Belousov CP(*src, *dst, ifi_imcasts); 1454427a928aSKonstantin Belousov CP(*src, *dst, ifi_omcasts); 1455427a928aSKonstantin Belousov CP(*src, *dst, ifi_iqdrops); 1456427a928aSKonstantin Belousov CP(*src, *dst, ifi_noproto); 1457427a928aSKonstantin Belousov CP(*src, *dst, ifi_hwassist); 1458427a928aSKonstantin Belousov CP(*src, *dst, ifi_epoch); 1459427a928aSKonstantin Belousov TV_CP(*src, *dst, ifi_lastchange); 1460427a928aSKonstantin Belousov } 1461427a928aSKonstantin Belousov #endif 1462427a928aSKonstantin Belousov 146337c84183SPoul-Henning Kamp static int 1464becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w) 1465df8bae1dSRodney W. Grimes { 1466becc44d7SSam Leffler struct ifnet *ifp; 1467becc44d7SSam Leffler struct ifaddr *ifa; 1468df8bae1dSRodney W. Grimes struct rt_addrinfo info; 1469df8bae1dSRodney W. Grimes int len, error = 0; 1470df8bae1dSRodney W. Grimes 1471df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 1472fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1473603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1474df8bae1dSRodney W. Grimes if (w->w_arg && w->w_arg != ifp->if_index) 1475df8bae1dSRodney W. Grimes continue; 14764a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1477becc44d7SSam Leffler info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1478913af518SLuigi Rizzo len = rt_msg2(RTM_IFINFO, &info, NULL, w); 14795dfc91d7SLuigi Rizzo info.rti_info[RTAX_IFP] = NULL; 148052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1481becc44d7SSam Leffler struct if_msghdr *ifm; 1482df8bae1dSRodney W. Grimes 1483427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1484427a928aSKonstantin Belousov if (w->w_req->flags & SCTL_MASK32) { 1485427a928aSKonstantin Belousov struct if_msghdr32 *ifm32; 1486427a928aSKonstantin Belousov 1487427a928aSKonstantin Belousov ifm32 = (struct if_msghdr32 *)w->w_tmem; 1488427a928aSKonstantin Belousov ifm32->ifm_index = ifp->if_index; 1489427a928aSKonstantin Belousov ifm32->ifm_flags = ifp->if_flags | 1490427a928aSKonstantin Belousov ifp->if_drv_flags; 1491427a928aSKonstantin Belousov copy_ifdata32(&ifp->if_data, &ifm32->ifm_data); 1492427a928aSKonstantin Belousov ifm32->ifm_addrs = info.rti_addrs; 1493427a928aSKonstantin Belousov error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32, 1494427a928aSKonstantin Belousov len); 1495427a928aSKonstantin Belousov goto sysctl_out; 1496427a928aSKonstantin Belousov } 1497427a928aSKonstantin Belousov #endif 1498df8bae1dSRodney W. Grimes ifm = (struct if_msghdr *)w->w_tmem; 1499df8bae1dSRodney W. Grimes ifm->ifm_index = ifp->if_index; 1500292ee7beSRobert Watson ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1501df8bae1dSRodney W. Grimes ifm->ifm_data = ifp->if_data; 1502df8bae1dSRodney W. Grimes ifm->ifm_addrs = info.rti_addrs; 150352041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len); 1504427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32 1505427a928aSKonstantin Belousov sysctl_out: 1506427a928aSKonstantin Belousov #endif 1507df440948SPoul-Henning Kamp if (error) 1508a35b06c5SJonathan Lemon goto done; 1509df8bae1dSRodney W. Grimes } 15105dfc91d7SLuigi Rizzo while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1511df8bae1dSRodney W. Grimes if (af && af != ifa->ifa_addr->sa_family) 1512df8bae1dSRodney W. Grimes continue; 1513b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1514b89e82ddSJamie Gritton ifa->ifa_addr) != 0) 151575c13541SPoul-Henning Kamp continue; 1516becc44d7SSam Leffler info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1517becc44d7SSam Leffler info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1518becc44d7SSam Leffler info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1519913af518SLuigi Rizzo len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 152052041295SPoul-Henning Kamp if (w->w_req && w->w_tmem) { 1521becc44d7SSam Leffler struct ifa_msghdr *ifam; 1522df8bae1dSRodney W. Grimes 1523df8bae1dSRodney W. Grimes ifam = (struct ifa_msghdr *)w->w_tmem; 1524df8bae1dSRodney W. Grimes ifam->ifam_index = ifa->ifa_ifp->if_index; 1525df8bae1dSRodney W. Grimes ifam->ifam_flags = ifa->ifa_flags; 1526df8bae1dSRodney W. Grimes ifam->ifam_metric = ifa->ifa_metric; 1527df8bae1dSRodney W. Grimes ifam->ifam_addrs = info.rti_addrs; 152852041295SPoul-Henning Kamp error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1529df440948SPoul-Henning Kamp if (error) 1530a35b06c5SJonathan Lemon goto done; 1531df8bae1dSRodney W. Grimes } 1532df8bae1dSRodney W. Grimes } 1533becc44d7SSam Leffler info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 15345dfc91d7SLuigi Rizzo info.rti_info[RTAX_BRD] = NULL; 1535df8bae1dSRodney W. Grimes } 1536a35b06c5SJonathan Lemon done: 1537fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 1538a35b06c5SJonathan Lemon return (error); 1539df8bae1dSRodney W. Grimes } 1540df8bae1dSRodney W. Grimes 1541ebda3fc3SBjoern A. Zeeb static int 15429b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w) 154305b2efe0SBruce M Simpson { 15449b98ee2cSLuigi Rizzo struct ifnet *ifp; 154505b2efe0SBruce M Simpson struct ifmultiaddr *ifma; 154605b2efe0SBruce M Simpson struct rt_addrinfo info; 154705b2efe0SBruce M Simpson int len, error = 0; 15489b98ee2cSLuigi Rizzo struct ifaddr *ifa; 154905b2efe0SBruce M Simpson 155005b2efe0SBruce M Simpson bzero((caddr_t)&info, sizeof(info)); 1551fe0fc7efSChristian S.J. Peron IFNET_RLOCK(); 1552603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 155305b2efe0SBruce M Simpson if (w->w_arg && w->w_arg != ifp->if_index) 155405b2efe0SBruce M Simpson continue; 15554a0d6638SRuslan Ermilov ifa = ifp->if_addr; 1556913af518SLuigi Rizzo info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1557fe0fc7efSChristian S.J. Peron IF_ADDR_LOCK(ifp); 155805b2efe0SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 155905b2efe0SBruce M Simpson if (af && af != ifma->ifma_addr->sa_family) 156005b2efe0SBruce M Simpson continue; 1561b89e82ddSJamie Gritton if (prison_if(w->w_req->td->td_ucred, 1562b89e82ddSJamie Gritton ifma->ifma_addr) != 0) 156305b2efe0SBruce M Simpson continue; 156405b2efe0SBruce M Simpson info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1565913af518SLuigi Rizzo info.rti_info[RTAX_GATEWAY] = 1566913af518SLuigi Rizzo (ifma->ifma_addr->sa_family != AF_LINK) ? 1567913af518SLuigi Rizzo ifma->ifma_lladdr : NULL; 1568913af518SLuigi Rizzo len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 156905b2efe0SBruce M Simpson if (w->w_req && w->w_tmem) { 15709b98ee2cSLuigi Rizzo struct ifma_msghdr *ifmam; 157105b2efe0SBruce M Simpson 157205b2efe0SBruce M Simpson ifmam = (struct ifma_msghdr *)w->w_tmem; 157305b2efe0SBruce M Simpson ifmam->ifmam_index = ifma->ifma_ifp->if_index; 157405b2efe0SBruce M Simpson ifmam->ifmam_flags = 0; 157505b2efe0SBruce M Simpson ifmam->ifmam_addrs = info.rti_addrs; 157605b2efe0SBruce M Simpson error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1577fe0fc7efSChristian S.J. Peron if (error) { 1578fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 157905b2efe0SBruce M Simpson goto done; 158005b2efe0SBruce M Simpson } 158105b2efe0SBruce M Simpson } 158205b2efe0SBruce M Simpson } 1583fe0fc7efSChristian S.J. Peron IF_ADDR_UNLOCK(ifp); 1584fe0fc7efSChristian S.J. Peron } 158505b2efe0SBruce M Simpson done: 1586fe0fc7efSChristian S.J. Peron IFNET_RUNLOCK(); 158705b2efe0SBruce M Simpson return (error); 158805b2efe0SBruce M Simpson } 158905b2efe0SBruce M Simpson 159052041295SPoul-Henning Kamp static int 159182d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1592df8bae1dSRodney W. Grimes { 159352041295SPoul-Henning Kamp int *name = (int *)arg1; 159452041295SPoul-Henning Kamp u_int namelen = arg2; 1595c2c2a7c1SBjoern A. Zeeb struct radix_node_head *rnh = NULL; /* silence compiler. */ 1596fe0fc7efSChristian S.J. Peron int i, lim, error = EINVAL; 1597df8bae1dSRodney W. Grimes u_char af; 1598df8bae1dSRodney W. Grimes struct walkarg w; 1599df8bae1dSRodney W. Grimes 160052041295SPoul-Henning Kamp name ++; 160152041295SPoul-Henning Kamp namelen--; 160252041295SPoul-Henning Kamp if (req->newptr) 1603df8bae1dSRodney W. Grimes return (EPERM); 1604df8bae1dSRodney W. Grimes if (namelen != 3) 1605f7a54d06SCrist J. Clark return ((namelen < 3) ? EISDIR : ENOTDIR); 1606df8bae1dSRodney W. Grimes af = name[0]; 1607b2aaf46eSJeffrey Hsu if (af > AF_MAX) 1608b2aaf46eSJeffrey Hsu return (EINVAL); 16096b96f1afSLuigi Rizzo bzero(&w, sizeof(w)); 1610df8bae1dSRodney W. Grimes w.w_op = name[1]; 1611df8bae1dSRodney W. Grimes w.w_arg = name[2]; 161252041295SPoul-Henning Kamp w.w_req = req; 1613df8bae1dSRodney W. Grimes 1614fe0fc7efSChristian S.J. Peron error = sysctl_wire_old_buffer(req, 0); 1615fe0fc7efSChristian S.J. Peron if (error) 1616fe0fc7efSChristian S.J. Peron return (error); 1617df8bae1dSRodney W. Grimes switch (w.w_op) { 1618df8bae1dSRodney W. Grimes 1619df8bae1dSRodney W. Grimes case NET_RT_DUMP: 1620df8bae1dSRodney W. Grimes case NET_RT_FLAGS: 1621a8b76c8fSLuigi Rizzo if (af == 0) { /* dump all tables */ 1622a8b76c8fSLuigi Rizzo i = 1; 1623a8b76c8fSLuigi Rizzo lim = AF_MAX; 1624a8b76c8fSLuigi Rizzo } else /* dump only one table */ 1625a8b76c8fSLuigi Rizzo i = lim = af; 16268eca593cSQing Li 16278eca593cSQing Li /* 16288eca593cSQing Li * take care of llinfo entries, the caller must 16298eca593cSQing Li * specify an AF 16308eca593cSQing Li */ 163114981d80SQing Li if (w.w_op == NET_RT_FLAGS && 163282b334e8SQing Li (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 16338eca593cSQing Li if (af != 0) 16348eca593cSQing Li error = lltable_sysctl_dumparp(af, w.w_req); 16358eca593cSQing Li else 16368eca593cSQing Li error = EINVAL; 16378eca593cSQing Li break; 16388eca593cSQing Li } 16398eca593cSQing Li /* 16408eca593cSQing Li * take care of routing entries 16418eca593cSQing Li */ 164220b0cdb7SBjoern A. Zeeb for (error = 0; error == 0 && i <= lim; i++) { 1643c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i); 1644c2c2a7c1SBjoern A. Zeeb if (rnh != NULL) { 1645fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_LOCK(rnh); 1646956b0b65SJeffrey Hsu error = rnh->rnh_walktree(rnh, 1647fe0fc7efSChristian S.J. Peron sysctl_dumpentry, &w); 1648fe0fc7efSChristian S.J. Peron RADIX_NODE_HEAD_UNLOCK(rnh); 1649a8b76c8fSLuigi Rizzo } else if (af != 0) 1650956b0b65SJeffrey Hsu error = EAFNOSUPPORT; 165120b0cdb7SBjoern A. Zeeb } 1652df8bae1dSRodney W. Grimes break; 1653df8bae1dSRodney W. Grimes 1654df8bae1dSRodney W. Grimes case NET_RT_IFLIST: 1655df8bae1dSRodney W. Grimes error = sysctl_iflist(af, &w); 165605b2efe0SBruce M Simpson break; 165705b2efe0SBruce M Simpson 165805b2efe0SBruce M Simpson case NET_RT_IFMALIST: 165905b2efe0SBruce M Simpson error = sysctl_ifmalist(af, &w); 166005b2efe0SBruce M Simpson break; 1661df8bae1dSRodney W. Grimes } 1662df8bae1dSRodney W. Grimes if (w.w_tmem) 1663df8bae1dSRodney W. Grimes free(w.w_tmem, M_RTABLE); 1664df8bae1dSRodney W. Grimes return (error); 1665df8bae1dSRodney W. Grimes } 1666df8bae1dSRodney W. Grimes 166752041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 166852041295SPoul-Henning Kamp 1669df8bae1dSRodney W. Grimes /* 1670df8bae1dSRodney W. Grimes * Definitions of protocols supported in the ROUTE domain. 1671df8bae1dSRodney W. Grimes */ 1672df8bae1dSRodney W. Grimes 16735b1c0294SDavid E. O'Brien static struct domain routedomain; /* or at least forward */ 1674df8bae1dSRodney W. Grimes 167552041295SPoul-Henning Kamp static struct protosw routesw[] = { 1676303989a2SRuslan Ermilov { 1677303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 1678303989a2SRuslan Ermilov .pr_domain = &routedomain, 1679303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1680303989a2SRuslan Ermilov .pr_output = route_output, 1681303989a2SRuslan Ermilov .pr_ctlinput = raw_ctlinput, 1682303989a2SRuslan Ermilov .pr_init = raw_init, 1683303989a2SRuslan Ermilov .pr_usrreqs = &route_usrreqs 1684df8bae1dSRodney W. Grimes } 1685df8bae1dSRodney W. Grimes }; 1686df8bae1dSRodney W. Grimes 1687303989a2SRuslan Ermilov static struct domain routedomain = { 1688303989a2SRuslan Ermilov .dom_family = PF_ROUTE, 1689303989a2SRuslan Ermilov .dom_name = "route", 1690303989a2SRuslan Ermilov .dom_protosw = routesw, 1691303989a2SRuslan Ermilov .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1692303989a2SRuslan Ermilov }; 169378a82810SGarrett Wollman 1694d0728d71SRobert Watson VNET_DOMAIN_SET(route); 1695