xref: /freebsd/sys/net/rtsock.c (revision caccbaef8e263b1d769e7bcac1c4617bdc12d484)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1988, 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  */
318b6acd2bSConrad Meyer #include "opt_ddb.h"
32fedeb08bSAlexander V. Chernikov #include "opt_route.h"
33413628a7SBjoern A. Zeeb #include "opt_inet.h"
34413628a7SBjoern A. Zeeb #include "opt_inet6.h"
35e440aed9SQing Li 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37960ed29cSSeigo Tanimura #include <sys/jail.h>
38413628a7SBjoern A. Zeeb #include <sys/kernel.h>
39f73e4f6cSMateusz Guzik #include <sys/eventhandler.h>
40d0728d71SRobert Watson #include <sys/domain.h>
41609ff41fSWarner Losh #include <sys/lock.h>
424d1d4912SBruce Evans #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44acd3428bSRobert Watson #include <sys/priv.h>
45960ed29cSSeigo Tanimura #include <sys/proc.h>
46960ed29cSSeigo Tanimura #include <sys/protosw.h>
4720efcfc6SAndrey V. Elsukov #include <sys/rmlock.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>
5676039bc8SGleb Smirnoff #include <net/if_var.h>
572c2b37adSJustin Hibbits #include <net/if_private.h>
588eca593cSQing Li #include <net/if_dl.h>
596e6b3f7cSQing Li #include <net/if_llatbl.h>
600ed6142bSQing Li #include <net/if_types.h>
61d989c7b3SRobert Watson #include <net/netisr.h>
62960ed29cSSeigo Tanimura #include <net/route.h>
63a287a973SAlexander V. Chernikov #include <net/route/route_ctl.h>
64e7d8af4fSAlexander V. Chernikov #include <net/route/route_var.h>
654b79449eSBjoern A. Zeeb #include <net/vnet.h>
66df8bae1dSRodney W. Grimes 
675a59cefcSBosko Milekic #include <netinet/in.h>
68c7ab6602SQing Li #include <netinet/if_ether.h>
6908b68b0eSGleb Smirnoff #include <netinet/ip_carp.h>
70413628a7SBjoern A. Zeeb #ifdef INET6
712fe5a794SAlexander V. Chernikov #include <netinet6/in6_var.h>
720bebb544SHiroki Sato #include <netinet6/ip6_var.h>
73413628a7SBjoern A. Zeeb #include <netinet6/scope6_var.h>
74413628a7SBjoern A. Zeeb #endif
75a6663252SAlexander V. Chernikov #include <net/route/nhop.h>
765a59cefcSBosko Milekic 
770e87bab6SAlexander V. Chernikov #define	DEBUG_MOD_NAME	rtsock
780e87bab6SAlexander V. Chernikov #define	DEBUG_MAX_LEVEL	LOG_DEBUG
790e87bab6SAlexander V. Chernikov #include <net/route/route_debug.h>
8033a0803fSAlexander V. Chernikov _DECLARE_DEBUG(LOG_INFO);
810e87bab6SAlexander V. Chernikov 
82427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32
83427a928aSKonstantin Belousov #include <sys/mount.h>
84427a928aSKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
85427a928aSKonstantin Belousov 
86427a928aSKonstantin Belousov struct if_msghdr32 {
87427a928aSKonstantin Belousov 	uint16_t ifm_msglen;
88427a928aSKonstantin Belousov 	uint8_t	ifm_version;
89427a928aSKonstantin Belousov 	uint8_t	ifm_type;
90427a928aSKonstantin Belousov 	int32_t	ifm_addrs;
91427a928aSKonstantin Belousov 	int32_t	ifm_flags;
92427a928aSKonstantin Belousov 	uint16_t ifm_index;
93d25f8522SMark Johnston 	uint16_t _ifm_spare1;
94b245f96cSGleb Smirnoff 	struct	if_data ifm_data;
95427a928aSKonstantin Belousov };
966d076ae8SBjoern A. Zeeb 
976d076ae8SBjoern A. Zeeb struct if_msghdrl32 {
986d076ae8SBjoern A. Zeeb 	uint16_t ifm_msglen;
996d076ae8SBjoern A. Zeeb 	uint8_t	ifm_version;
1006d076ae8SBjoern A. Zeeb 	uint8_t	ifm_type;
1016d076ae8SBjoern A. Zeeb 	int32_t	ifm_addrs;
1026d076ae8SBjoern A. Zeeb 	int32_t	ifm_flags;
1036d076ae8SBjoern A. Zeeb 	uint16_t ifm_index;
1046d076ae8SBjoern A. Zeeb 	uint16_t _ifm_spare1;
1056d076ae8SBjoern A. Zeeb 	uint16_t ifm_len;
1066d076ae8SBjoern A. Zeeb 	uint16_t ifm_data_off;
107d25f8522SMark Johnston 	uint32_t _ifm_spare2;
108b245f96cSGleb Smirnoff 	struct	if_data ifm_data;
1096d076ae8SBjoern A. Zeeb };
1106d076ae8SBjoern A. Zeeb 
1116d076ae8SBjoern A. Zeeb struct ifa_msghdrl32 {
1126d076ae8SBjoern A. Zeeb 	uint16_t ifam_msglen;
1136d076ae8SBjoern A. Zeeb 	uint8_t	ifam_version;
1146d076ae8SBjoern A. Zeeb 	uint8_t	ifam_type;
1156d076ae8SBjoern A. Zeeb 	int32_t	ifam_addrs;
1166d076ae8SBjoern A. Zeeb 	int32_t	ifam_flags;
1176d076ae8SBjoern A. Zeeb 	uint16_t ifam_index;
1186d076ae8SBjoern A. Zeeb 	uint16_t _ifam_spare1;
1196d076ae8SBjoern A. Zeeb 	uint16_t ifam_len;
1206d076ae8SBjoern A. Zeeb 	uint16_t ifam_data_off;
1216d076ae8SBjoern A. Zeeb 	int32_t	ifam_metric;
122b245f96cSGleb Smirnoff 	struct	if_data ifam_data;
1236d076ae8SBjoern A. Zeeb };
124279e33d4SKonstantin Belousov 
125279e33d4SKonstantin Belousov #define SA_SIZE32(sa)						\
126279e33d4SKonstantin Belousov     (  (((struct sockaddr *)(sa))->sa_len == 0) ?		\
127279e33d4SKonstantin Belousov 	sizeof(int)		:				\
128279e33d4SKonstantin Belousov 	1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
129279e33d4SKonstantin Belousov 
1306d076ae8SBjoern A. Zeeb #endif /* COMPAT_FREEBSD32 */
131427a928aSKonstantin Belousov 
132b31fbebeSAlexander V. Chernikov struct linear_buffer {
133b31fbebeSAlexander V. Chernikov 	char		*base;	/* Base allocated memory pointer */
134b31fbebeSAlexander V. Chernikov 	uint32_t	offset;	/* Currently used offset */
135b31fbebeSAlexander V. Chernikov 	uint32_t	size;	/* Total buffer size */
136b31fbebeSAlexander V. Chernikov };
137b31fbebeSAlexander V. Chernikov #define	SCRATCH_BUFFER_SIZE	1024
138b31fbebeSAlexander V. Chernikov 
139ec1b18c7SMark Johnston #define	RTS_PID_LOG(_l, _fmt, ...)					\
140ec1b18c7SMark Johnston 	RT_LOG_##_l(_l, "PID %d: " _fmt, curproc ? curproc->p_pid : 0,	\
141ec1b18c7SMark Johnston 	    ## __VA_ARGS__)
142758c9d54SAlexander V. Chernikov 
143a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
144a1c995b6SPoul-Henning Kamp 
145becc44d7SSam Leffler /* NB: these are not modified */
14652041295SPoul-Henning Kamp static struct	sockaddr route_src = { 2, PF_ROUTE, };
147076d0761SJulian Elischer static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
148becc44d7SSam Leffler 
14908b68b0eSGleb Smirnoff /* These are external hooks for CARP. */
15008b68b0eSGleb Smirnoff int	(*carp_get_vhid_p)(struct ifaddr *);
15108b68b0eSGleb Smirnoff 
152528737fdSBjoern A. Zeeb /*
15336b10ac2SGleb Smirnoff  * Used by rtsock callback code to decide whether to filter the update
154528737fdSBjoern A. Zeeb  * notification to a socket bound to a particular FIB.
155528737fdSBjoern A. Zeeb  */
156528737fdSBjoern A. Zeeb #define	RTS_FILTER_FIB	M_PROTO8
157d94ec749SGleb Smirnoff /*
158d94ec749SGleb Smirnoff  * Used to store address family of the notification.
159d94ec749SGleb Smirnoff  */
160d94ec749SGleb Smirnoff #define	m_rtsock_family	m_pkthdr.PH_loc.eight[0]
161528737fdSBjoern A. Zeeb 
16236b10ac2SGleb Smirnoff struct rcb {
16336b10ac2SGleb Smirnoff 	LIST_ENTRY(rcb) list;
16436b10ac2SGleb Smirnoff 	struct socket	*rcb_socket;
16536b10ac2SGleb Smirnoff 	sa_family_t	rcb_family;
16636b10ac2SGleb Smirnoff };
16736b10ac2SGleb Smirnoff 
168c5d4eab6SMarko Zec typedef struct {
16936b10ac2SGleb Smirnoff 	LIST_HEAD(, rcb)	cblist;
17018aee723SPeter Pentchev 	int	ip_count;	/* attached w/ AF_INET */
171becc44d7SSam Leffler 	int	ip6_count;	/* attached w/ AF_INET6 */
172becc44d7SSam Leffler 	int	any_count;	/* total attached */
173c5d4eab6SMarko Zec } route_cb_t;
1745f901c92SAndrew Turner VNET_DEFINE_STATIC(route_cb_t, route_cb);
175c5d4eab6SMarko Zec #define	V_route_cb VNET(route_cb)
176df8bae1dSRodney W. Grimes 
177aea8b30fSSam Leffler struct mtx rtsock_mtx;
178aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
179aea8b30fSSam Leffler 
180aea8b30fSSam Leffler #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
181aea8b30fSSam Leffler #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
182aea8b30fSSam Leffler #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
183aea8b30fSSam Leffler 
184fedeb08bSAlexander V. Chernikov SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
185190a4c94SRobert Watson 
186df8bae1dSRodney W. Grimes struct walkarg {
187d68fb8d9SAlexander V. Chernikov 	int	family;
18852041295SPoul-Henning Kamp 	int	w_tmemsize;
18952041295SPoul-Henning Kamp 	int	w_op, w_arg;
19052041295SPoul-Henning Kamp 	caddr_t	w_tmem;
19152041295SPoul-Henning Kamp 	struct sysctl_req *w_req;
192d68fb8d9SAlexander V. Chernikov 	struct sockaddr *dst;
193d68fb8d9SAlexander V. Chernikov 	struct sockaddr *mask;
194df8bae1dSRodney W. Grimes };
195df8bae1dSRodney W. Grimes 
196d989c7b3SRobert Watson static void	rts_input(struct mbuf *m);
1976db47af4SAlexander V. Chernikov static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
198f2e5eb36SAlexander V. Chernikov static int	rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
199f2e5eb36SAlexander V. Chernikov 			struct walkarg *w, int *plen);
2005dfc91d7SLuigi Rizzo static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
2015dfc91d7SLuigi Rizzo 			struct rt_addrinfo *rtinfo);
202b31fbebeSAlexander V. Chernikov static int	cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
203d68fb8d9SAlexander V. Chernikov static int	sysctl_dumpentry(struct rtentry *rt, void *vw);
2042259a030SAlexander V. Chernikov static int	sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
2052259a030SAlexander V. Chernikov 			uint32_t weight, struct walkarg *w);
206929ddbbbSAlfred Perlstein static int	sysctl_iflist(int af, struct walkarg *w);
20705b2efe0SBruce M Simpson static int	sysctl_ifmalist(int af, struct walkarg *w);
20893bfd365SAlexander V. Chernikov static void	rt_getmetrics(const struct rtentry *rt,
20993bfd365SAlexander V. Chernikov 			const struct nhop_object *nh, struct rt_metrics *out);
2103ca1a2d6SMax Laier static void	rt_dispatch(struct mbuf *, sa_family_t);
211d8b42ddcSAlexander V. Chernikov static void	rt_ifannouncemsg(struct ifnet *ifp, int what);
212c83dda36SAlexander V. Chernikov static int	handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
213a287a973SAlexander V. Chernikov 			struct rt_msghdr *rtm, struct rib_cmd_info *rc);
214592d300eSAlexander V. Chernikov static int	update_rtm_from_rc(struct rt_addrinfo *info,
215c83dda36SAlexander V. Chernikov 			struct rt_msghdr **prtm, int alloc_len,
216592d300eSAlexander V. Chernikov 			struct rib_cmd_info *rc, struct nhop_object *nh);
217c83dda36SAlexander V. Chernikov static void	send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
218c83dda36SAlexander V. Chernikov 			struct mbuf *m, sa_family_t saf, u_int fibnum,
219c83dda36SAlexander V. Chernikov 			int rtm_errno);
2207e5bf684SAlexander V. Chernikov static void	rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc);
2211bcd230fSAlexander V. Chernikov static void	rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask);
222df8bae1dSRodney W. Grimes 
223d4b5cae4SRobert Watson static struct netisr_handler rtsock_nh = {
224d4b5cae4SRobert Watson 	.nh_name = "rtsock",
225d4b5cae4SRobert Watson 	.nh_handler = rts_input,
226d4b5cae4SRobert Watson 	.nh_proto = NETISR_ROUTE,
227d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_SOURCE,
228d4b5cae4SRobert Watson };
229d4b5cae4SRobert Watson 
230d4b5cae4SRobert Watson static int
sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)231d4b5cae4SRobert Watson sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
232d4b5cae4SRobert Watson {
233d4b5cae4SRobert Watson 	int error, qlimit;
234d4b5cae4SRobert Watson 
235d4b5cae4SRobert Watson 	netisr_getqlimit(&rtsock_nh, &qlimit);
236d4b5cae4SRobert Watson 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
237d4b5cae4SRobert Watson         if (error || !req->newptr)
238d4b5cae4SRobert Watson                 return (error);
239d4b5cae4SRobert Watson 	if (qlimit < 1)
240d4b5cae4SRobert Watson 		return (EINVAL);
241d4b5cae4SRobert Watson 	return (netisr_setqlimit(&rtsock_nh, qlimit));
242d4b5cae4SRobert Watson }
2437029da5cSPawel Biernacki SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
24421a722d9SZhenlei Huang     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
245d4b5cae4SRobert Watson     0, 0, sysctl_route_netisr_maxqlen, "I",
246d4b5cae4SRobert Watson     "maximum routing socket dispatch queue length");
247d4b5cae4SRobert Watson 
248d989c7b3SRobert Watson static void
vnet_rts_init(void)249484149deSBjoern A. Zeeb vnet_rts_init(void)
250d989c7b3SRobert Watson {
251b062951aSRobert Watson 	int tmp;
252d989c7b3SRobert Watson 
253484149deSBjoern A. Zeeb 	if (IS_DEFAULT_VNET(curvnet)) {
254b062951aSRobert Watson 		if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
255d4b5cae4SRobert Watson 			rtsock_nh.nh_qlimit = tmp;
256d4b5cae4SRobert Watson 		netisr_register(&rtsock_nh);
257d989c7b3SRobert Watson 	}
258484149deSBjoern A. Zeeb #ifdef VIMAGE
259484149deSBjoern A. Zeeb 	 else
260484149deSBjoern A. Zeeb 		netisr_register_vnet(&rtsock_nh);
261484149deSBjoern A. Zeeb #endif
262484149deSBjoern A. Zeeb }
263484149deSBjoern A. Zeeb VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
264adc573c8SZhenlei Huang     vnet_rts_init, NULL);
265484149deSBjoern A. Zeeb 
266484149deSBjoern A. Zeeb #ifdef VIMAGE
267484149deSBjoern A. Zeeb static void
vnet_rts_uninit(void)268484149deSBjoern A. Zeeb vnet_rts_uninit(void)
269484149deSBjoern A. Zeeb {
270484149deSBjoern A. Zeeb 
271484149deSBjoern A. Zeeb 	netisr_unregister_vnet(&rtsock_nh);
272484149deSBjoern A. Zeeb }
273484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
274adc573c8SZhenlei Huang     vnet_rts_uninit, NULL);
275484149deSBjoern A. Zeeb #endif
276d989c7b3SRobert Watson 
27736b10ac2SGleb Smirnoff static void
report_route_event(const struct rib_cmd_info * rc,void * _cbdata)2787e5bf684SAlexander V. Chernikov report_route_event(const struct rib_cmd_info *rc, void *_cbdata)
2797e5bf684SAlexander V. Chernikov {
2807e5bf684SAlexander V. Chernikov 	uint32_t fibnum = (uint32_t)(uintptr_t)_cbdata;
2817e5bf684SAlexander V. Chernikov 	struct nhop_object *nh;
2827e5bf684SAlexander V. Chernikov 
2837e5bf684SAlexander V. Chernikov 	nh = rc->rc_cmd == RTM_DELETE ? rc->rc_nh_old : rc->rc_nh_new;
2847e5bf684SAlexander V. Chernikov 	rt_routemsg(rc->rc_cmd, rc->rc_rt, nh, fibnum);
2857e5bf684SAlexander V. Chernikov }
2867e5bf684SAlexander V. Chernikov 
2877e5bf684SAlexander V. Chernikov static void
rts_handle_route_event(uint32_t fibnum,const struct rib_cmd_info * rc)2887e5bf684SAlexander V. Chernikov rts_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
2897e5bf684SAlexander V. Chernikov {
2907e5bf684SAlexander V. Chernikov #ifdef ROUTE_MPATH
2917e5bf684SAlexander V. Chernikov 	if ((rc->rc_nh_new && NH_IS_NHGRP(rc->rc_nh_new)) ||
2927e5bf684SAlexander V. Chernikov 	    (rc->rc_nh_old && NH_IS_NHGRP(rc->rc_nh_old))) {
2937e5bf684SAlexander V. Chernikov 		rib_decompose_notification(rc, report_route_event,
2947e5bf684SAlexander V. Chernikov 		    (void *)(uintptr_t)fibnum);
2957e5bf684SAlexander V. Chernikov 	} else
2967e5bf684SAlexander V. Chernikov #endif
2977e5bf684SAlexander V. Chernikov 		report_route_event(rc, (void *)(uintptr_t)fibnum);
2987e5bf684SAlexander V. Chernikov }
2991bcd230fSAlexander V. Chernikov static struct rtbridge rtsbridge = {
3001bcd230fSAlexander V. Chernikov 	.route_f = rts_handle_route_event,
3011bcd230fSAlexander V. Chernikov 	.ifmsg_f = rtsock_ifmsg,
3021bcd230fSAlexander V. Chernikov };
3037e5bf684SAlexander V. Chernikov static struct rtbridge *rtsbridge_orig_p;
3047e5bf684SAlexander V. Chernikov 
3057e5bf684SAlexander V. Chernikov static void
rtsock_notify_event(uint32_t fibnum,const struct rib_cmd_info * rc)3067e5bf684SAlexander V. Chernikov rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc)
3077e5bf684SAlexander V. Chernikov {
3087e5bf684SAlexander V. Chernikov 	netlink_callback_p->route_f(fibnum, rc);
3097e5bf684SAlexander V. Chernikov }
3107e5bf684SAlexander V. Chernikov 
3117e5bf684SAlexander V. Chernikov static void
rtsock_init(void)3127e5bf684SAlexander V. Chernikov rtsock_init(void)
3137e5bf684SAlexander V. Chernikov {
3147e5bf684SAlexander V. Chernikov 	rtsbridge_orig_p = rtsock_callback_p;
3157e5bf684SAlexander V. Chernikov 	rtsock_callback_p = &rtsbridge;
3167e5bf684SAlexander V. Chernikov }
3177e5bf684SAlexander V. Chernikov SYSINIT(rtsock_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtsock_init, NULL);
3187e5bf684SAlexander V. Chernikov 
3197e5bf684SAlexander V. Chernikov static void
rts_handle_ifnet_arrival(void * arg __unused,struct ifnet * ifp)320d8b42ddcSAlexander V. Chernikov rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
321d8b42ddcSAlexander V. Chernikov {
322d8b42ddcSAlexander V. Chernikov 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
323d8b42ddcSAlexander V. Chernikov }
324d8b42ddcSAlexander V. Chernikov EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
325d8b42ddcSAlexander V. Chernikov 
326d8b42ddcSAlexander V. Chernikov static void
rts_handle_ifnet_departure(void * arg __unused,struct ifnet * ifp)327d8b42ddcSAlexander V. Chernikov rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
328d8b42ddcSAlexander V. Chernikov {
329d8b42ddcSAlexander V. Chernikov 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
330d8b42ddcSAlexander V. Chernikov }
331d8b42ddcSAlexander V. Chernikov EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
332d8b42ddcSAlexander V. Chernikov 
333d8b42ddcSAlexander V. Chernikov static void
rts_append_data(struct socket * so,struct mbuf * m)33436b10ac2SGleb Smirnoff rts_append_data(struct socket *so, struct mbuf *m)
335528737fdSBjoern A. Zeeb {
336528737fdSBjoern A. Zeeb 
33736b10ac2SGleb Smirnoff 	if (sbappendaddr(&so->so_rcv, &route_src, m, NULL) == 0) {
33836b10ac2SGleb Smirnoff 		soroverflow(so);
33936b10ac2SGleb Smirnoff 		m_freem(m);
34036b10ac2SGleb Smirnoff 	} else
34136b10ac2SGleb Smirnoff 		sorwakeup(so);
342528737fdSBjoern A. Zeeb }
343528737fdSBjoern A. Zeeb 
344d989c7b3SRobert Watson static void
rts_input(struct mbuf * m)345d989c7b3SRobert Watson rts_input(struct mbuf *m)
346d989c7b3SRobert Watson {
34736b10ac2SGleb Smirnoff 	struct rcb *rcb;
34836b10ac2SGleb Smirnoff 	struct socket *last;
349d989c7b3SRobert Watson 
35036b10ac2SGleb Smirnoff 	last = NULL;
35136b10ac2SGleb Smirnoff 	RTSOCK_LOCK();
35236b10ac2SGleb Smirnoff 	LIST_FOREACH(rcb, &V_route_cb.cblist, list) {
35336b10ac2SGleb Smirnoff 		if (rcb->rcb_family != AF_UNSPEC &&
35436b10ac2SGleb Smirnoff 		    rcb->rcb_family != m->m_rtsock_family)
35536b10ac2SGleb Smirnoff 			continue;
35636b10ac2SGleb Smirnoff 		if ((m->m_flags & RTS_FILTER_FIB) &&
35736b10ac2SGleb Smirnoff 		    M_GETFIB(m) != rcb->rcb_socket->so_fibnum)
35836b10ac2SGleb Smirnoff 			continue;
35936b10ac2SGleb Smirnoff 		if (last != NULL) {
36036b10ac2SGleb Smirnoff 			struct mbuf *n;
361d989c7b3SRobert Watson 
36236b10ac2SGleb Smirnoff 			n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
36336b10ac2SGleb Smirnoff 			if (n != NULL)
36436b10ac2SGleb Smirnoff 				rts_append_data(last, n);
365d989c7b3SRobert Watson 		}
36636b10ac2SGleb Smirnoff 		last = rcb->rcb_socket;
36736b10ac2SGleb Smirnoff 	}
36836b10ac2SGleb Smirnoff 	if (last != NULL)
36936b10ac2SGleb Smirnoff 		rts_append_data(last, m);
37036b10ac2SGleb Smirnoff 	else
37136b10ac2SGleb Smirnoff 		m_freem(m);
37236b10ac2SGleb Smirnoff 	RTSOCK_UNLOCK();
373df8bae1dSRodney W. Grimes }
374a29f300eSGarrett Wollman 
375a152f8a3SRobert Watson static void
rts_close(struct socket * so)376a152f8a3SRobert Watson rts_close(struct socket *so)
377a152f8a3SRobert Watson {
378a152f8a3SRobert Watson 
37936b10ac2SGleb Smirnoff 	soisdisconnected(so);
380a152f8a3SRobert Watson }
381a152f8a3SRobert Watson 
38236b10ac2SGleb Smirnoff static SYSCTL_NODE(_net, OID_AUTO, rtsock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
38336b10ac2SGleb Smirnoff     "Routing socket infrastructure");
38436b10ac2SGleb Smirnoff static u_long rts_sendspace = 8192;
38536b10ac2SGleb Smirnoff SYSCTL_ULONG(_net_rtsock, OID_AUTO, sendspace, CTLFLAG_RW, &rts_sendspace, 0,
38636b10ac2SGleb Smirnoff     "Default routing socket send space");
38736b10ac2SGleb Smirnoff static u_long rts_recvspace = 8192;
38836b10ac2SGleb Smirnoff SYSCTL_ULONG(_net_rtsock, OID_AUTO, recvspace, CTLFLAG_RW, &rts_recvspace, 0,
38936b10ac2SGleb Smirnoff     "Default routing socket receive space");
390a29f300eSGarrett Wollman 
391a29f300eSGarrett Wollman static int
rts_attach(struct socket * so,int proto,struct thread * td)392b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td)
393a29f300eSGarrett Wollman {
39436b10ac2SGleb Smirnoff 	struct rcb *rcb;
395c9b652e3SAndre Oppermann 	int error;
396a29f300eSGarrett Wollman 
39736b10ac2SGleb Smirnoff 	error = soreserve(so, rts_sendspace, rts_recvspace);
39836b10ac2SGleb Smirnoff 	if (error)
39936b10ac2SGleb Smirnoff 		return (error);
400bc725eafSRobert Watson 
40136b10ac2SGleb Smirnoff 	rcb = malloc(sizeof(*rcb), M_PCB, M_WAITOK);
40236b10ac2SGleb Smirnoff 	rcb->rcb_socket = so;
40336b10ac2SGleb Smirnoff 	rcb->rcb_family = proto;
404a29f300eSGarrett Wollman 
40536b10ac2SGleb Smirnoff 	so->so_pcb = rcb;
4068b07e49aSJulian Elischer 	so->so_fibnum = td->td_proc->p_fibnum;
40736b10ac2SGleb Smirnoff 	so->so_options |= SO_USELOOPBACK;
40836b10ac2SGleb Smirnoff 
409aea8b30fSSam Leffler 	RTSOCK_LOCK();
41036b10ac2SGleb Smirnoff 	LIST_INSERT_HEAD(&V_route_cb.cblist, rcb, list);
41136b10ac2SGleb Smirnoff 	switch (proto) {
412a29f300eSGarrett Wollman 	case AF_INET:
413c5d4eab6SMarko Zec 		V_route_cb.ip_count++;
414a29f300eSGarrett Wollman 		break;
415899ce4f4SYoshinobu Inoue 	case AF_INET6:
416c5d4eab6SMarko Zec 		V_route_cb.ip6_count++;
417899ce4f4SYoshinobu Inoue 		break;
418a29f300eSGarrett Wollman 	}
419c5d4eab6SMarko Zec 	V_route_cb.any_count++;
420aea8b30fSSam Leffler 	RTSOCK_UNLOCK();
42103e49181SSeigo Tanimura 	soisconnected(so);
42236b10ac2SGleb Smirnoff 
42336b10ac2SGleb Smirnoff 	return (0);
424df8bae1dSRodney W. Grimes }
425df8bae1dSRodney W. Grimes 
426*caccbaefSMark Johnston static int
rts_ctloutput(struct socket * so,struct sockopt * sopt)427*caccbaefSMark Johnston rts_ctloutput(struct socket *so, struct sockopt *sopt)
428*caccbaefSMark Johnston {
429*caccbaefSMark Johnston 	int error, optval;
430*caccbaefSMark Johnston 
431*caccbaefSMark Johnston 	error = ENOPROTOOPT;
432*caccbaefSMark Johnston 	if (sopt->sopt_dir == SOPT_SET) {
433*caccbaefSMark Johnston 		switch (sopt->sopt_level) {
434*caccbaefSMark Johnston 		case SOL_SOCKET:
435*caccbaefSMark Johnston 			switch (sopt->sopt_name) {
436*caccbaefSMark Johnston 			case SO_SETFIB:
437*caccbaefSMark Johnston 				error = sooptcopyin(sopt, &optval,
438*caccbaefSMark Johnston 				    sizeof(optval), sizeof(optval));
439*caccbaefSMark Johnston 				if (error != 0)
440*caccbaefSMark Johnston 					break;
441*caccbaefSMark Johnston 				error = sosetfib(so, optval);
442*caccbaefSMark Johnston 				break;
443*caccbaefSMark Johnston 			}
444*caccbaefSMark Johnston 			break;
445*caccbaefSMark Johnston 		}
446*caccbaefSMark Johnston 	}
447*caccbaefSMark Johnston 	return (error);
448*caccbaefSMark Johnston }
449*caccbaefSMark Johnston 
450bc725eafSRobert Watson static void
rts_detach(struct socket * so)451a29f300eSGarrett Wollman rts_detach(struct socket *so)
452a29f300eSGarrett Wollman {
45336b10ac2SGleb Smirnoff 	struct rcb *rcb = so->so_pcb;
454bc725eafSRobert Watson 
455aea8b30fSSam Leffler 	RTSOCK_LOCK();
45636b10ac2SGleb Smirnoff 	LIST_REMOVE(rcb, list);
45736b10ac2SGleb Smirnoff 	switch(rcb->rcb_family) {
458a29f300eSGarrett Wollman 	case AF_INET:
459c5d4eab6SMarko Zec 		V_route_cb.ip_count--;
460a29f300eSGarrett Wollman 		break;
461899ce4f4SYoshinobu Inoue 	case AF_INET6:
462c5d4eab6SMarko Zec 		V_route_cb.ip6_count--;
463899ce4f4SYoshinobu Inoue 		break;
464a29f300eSGarrett Wollman 	}
465c5d4eab6SMarko Zec 	V_route_cb.any_count--;
466aea8b30fSSam Leffler 	RTSOCK_UNLOCK();
46736b10ac2SGleb Smirnoff 	free(rcb, M_PCB);
46836b10ac2SGleb Smirnoff 	so->so_pcb = NULL;
469a29f300eSGarrett Wollman }
470a29f300eSGarrett Wollman 
471a29f300eSGarrett Wollman static int
rts_disconnect(struct socket * so)47242904794SAlexander V. Chernikov rts_disconnect(struct socket *so)
47342904794SAlexander V. Chernikov {
47442904794SAlexander V. Chernikov 
47542904794SAlexander V. Chernikov 	return (ENOTCONN);
47642904794SAlexander V. Chernikov }
47742904794SAlexander V. Chernikov 
47842904794SAlexander V. Chernikov static int
rts_shutdown(struct socket * so,enum shutdown_how how)4795bba2728SGleb Smirnoff rts_shutdown(struct socket *so, enum shutdown_how how)
480a29f300eSGarrett Wollman {
4815bba2728SGleb Smirnoff 	/*
4825bba2728SGleb Smirnoff 	 * Note: route socket marks itself as connected through its lifetime.
4835bba2728SGleb Smirnoff 	 */
4845bba2728SGleb Smirnoff 	switch (how) {
4855bba2728SGleb Smirnoff 	case SHUT_RD:
486ce69e373SGleb Smirnoff 		sorflush(so);
4875bba2728SGleb Smirnoff 		break;
4885bba2728SGleb Smirnoff 	case SHUT_RDWR:
489ce69e373SGleb Smirnoff 		sorflush(so);
4905bba2728SGleb Smirnoff 		/* FALLTHROUGH */
4915bba2728SGleb Smirnoff 	case SHUT_WR:
49236b10ac2SGleb Smirnoff 		socantsendmore(so);
4935bba2728SGleb Smirnoff 	}
4945bba2728SGleb Smirnoff 
49536b10ac2SGleb Smirnoff 	return (0);
496a29f300eSGarrett Wollman }
497a29f300eSGarrett Wollman 
498413628a7SBjoern A. Zeeb #ifndef _SOCKADDR_UNION_DEFINED
499413628a7SBjoern A. Zeeb #define	_SOCKADDR_UNION_DEFINED
500413628a7SBjoern A. Zeeb /*
501413628a7SBjoern A. Zeeb  * The union of all possible address formats we handle.
502413628a7SBjoern A. Zeeb  */
503413628a7SBjoern A. Zeeb union sockaddr_union {
504413628a7SBjoern A. Zeeb 	struct sockaddr		sa;
505413628a7SBjoern A. Zeeb 	struct sockaddr_in	sin;
506413628a7SBjoern A. Zeeb 	struct sockaddr_in6	sin6;
507413628a7SBjoern A. Zeeb };
508413628a7SBjoern A. Zeeb #endif /* _SOCKADDR_UNION_DEFINED */
509413628a7SBjoern A. Zeeb 
510413628a7SBjoern A. Zeeb static int
rtm_get_jailed(struct rt_addrinfo * info,struct ifnet * ifp,struct nhop_object * nh,union sockaddr_union * saun,struct ucred * cred)511413628a7SBjoern A. Zeeb rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
512aaad3c4fSAlexander V. Chernikov     struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
513413628a7SBjoern A. Zeeb {
514a68cc388SGleb Smirnoff #if defined(INET) || defined(INET6)
515a68cc388SGleb Smirnoff 	struct epoch_tracker et;
516a68cc388SGleb Smirnoff #endif
517413628a7SBjoern A. Zeeb 
5189c79d243SJamie Gritton 	/* First, see if the returned address is part of the jail. */
519aaad3c4fSAlexander V. Chernikov 	if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
520aaad3c4fSAlexander V. Chernikov 		info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
5219c79d243SJamie Gritton 		return (0);
5229c79d243SJamie Gritton 	}
5239c79d243SJamie Gritton 
524413628a7SBjoern A. Zeeb 	switch (info->rti_info[RTAX_DST]->sa_family) {
525413628a7SBjoern A. Zeeb #ifdef INET
526413628a7SBjoern A. Zeeb 	case AF_INET:
527413628a7SBjoern A. Zeeb 	{
528413628a7SBjoern A. Zeeb 		struct in_addr ia;
529413628a7SBjoern A. Zeeb 		struct ifaddr *ifa;
530413628a7SBjoern A. Zeeb 		int found;
531413628a7SBjoern A. Zeeb 
532413628a7SBjoern A. Zeeb 		found = 0;
533413628a7SBjoern A. Zeeb 		/*
5349c79d243SJamie Gritton 		 * Try to find an address on the given outgoing interface
5359c79d243SJamie Gritton 		 * that belongs to the jail.
536413628a7SBjoern A. Zeeb 		 */
537a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
538d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
539413628a7SBjoern A. Zeeb 			struct sockaddr *sa;
540413628a7SBjoern A. Zeeb 			sa = ifa->ifa_addr;
541413628a7SBjoern A. Zeeb 			if (sa->sa_family != AF_INET)
542413628a7SBjoern A. Zeeb 				continue;
543413628a7SBjoern A. Zeeb 			ia = ((struct sockaddr_in *)sa)->sin_addr;
544b89e82ddSJamie Gritton 			if (prison_check_ip4(cred, &ia) == 0) {
545413628a7SBjoern A. Zeeb 				found = 1;
546413628a7SBjoern A. Zeeb 				break;
547413628a7SBjoern A. Zeeb 			}
548413628a7SBjoern A. Zeeb 		}
549a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
550413628a7SBjoern A. Zeeb 		if (!found) {
551413628a7SBjoern A. Zeeb 			/*
5529c79d243SJamie Gritton 			 * As a last resort return the 'default' jail address.
553413628a7SBjoern A. Zeeb 			 */
554aaad3c4fSAlexander V. Chernikov 			ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
5550304c731SJamie Gritton 			    sin_addr;
5561cecba0fSBjoern A. Zeeb 			if (prison_get_ip4(cred, &ia) != 0)
557413628a7SBjoern A. Zeeb 				return (ESRCH);
558413628a7SBjoern A. Zeeb 		}
559413628a7SBjoern A. Zeeb 		bzero(&saun->sin, sizeof(struct sockaddr_in));
560413628a7SBjoern A. Zeeb 		saun->sin.sin_len = sizeof(struct sockaddr_in);
561413628a7SBjoern A. Zeeb 		saun->sin.sin_family = AF_INET;
562413628a7SBjoern A. Zeeb 		saun->sin.sin_addr.s_addr = ia.s_addr;
5639c79d243SJamie Gritton 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
564413628a7SBjoern A. Zeeb 		break;
565413628a7SBjoern A. Zeeb 	}
566413628a7SBjoern A. Zeeb #endif
567413628a7SBjoern A. Zeeb #ifdef INET6
568413628a7SBjoern A. Zeeb 	case AF_INET6:
569413628a7SBjoern A. Zeeb 	{
570413628a7SBjoern A. Zeeb 		struct in6_addr ia6;
571413628a7SBjoern A. Zeeb 		struct ifaddr *ifa;
572413628a7SBjoern A. Zeeb 		int found;
573413628a7SBjoern A. Zeeb 
574413628a7SBjoern A. Zeeb 		found = 0;
575413628a7SBjoern A. Zeeb 		/*
5769c79d243SJamie Gritton 		 * Try to find an address on the given outgoing interface
5779c79d243SJamie Gritton 		 * that belongs to the jail.
578413628a7SBjoern A. Zeeb 		 */
579a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
580d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
581413628a7SBjoern A. Zeeb 			struct sockaddr *sa;
582413628a7SBjoern A. Zeeb 			sa = ifa->ifa_addr;
583413628a7SBjoern A. Zeeb 			if (sa->sa_family != AF_INET6)
584413628a7SBjoern A. Zeeb 				continue;
585413628a7SBjoern A. Zeeb 			bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
586413628a7SBjoern A. Zeeb 			    &ia6, sizeof(struct in6_addr));
587b89e82ddSJamie Gritton 			if (prison_check_ip6(cred, &ia6) == 0) {
588413628a7SBjoern A. Zeeb 				found = 1;
589413628a7SBjoern A. Zeeb 				break;
590413628a7SBjoern A. Zeeb 			}
591413628a7SBjoern A. Zeeb 		}
592a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
593413628a7SBjoern A. Zeeb 		if (!found) {
594413628a7SBjoern A. Zeeb 			/*
5959c79d243SJamie Gritton 			 * As a last resort return the 'default' jail address.
596413628a7SBjoern A. Zeeb 			 */
597aaad3c4fSAlexander V. Chernikov 			ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
5980304c731SJamie Gritton 			    sin6_addr;
5991cecba0fSBjoern A. Zeeb 			if (prison_get_ip6(cred, &ia6) != 0)
600413628a7SBjoern A. Zeeb 				return (ESRCH);
601413628a7SBjoern A. Zeeb 		}
602413628a7SBjoern A. Zeeb 		bzero(&saun->sin6, sizeof(struct sockaddr_in6));
603413628a7SBjoern A. Zeeb 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
604413628a7SBjoern A. Zeeb 		saun->sin6.sin6_family = AF_INET6;
6059c79d243SJamie Gritton 		bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
606413628a7SBjoern A. Zeeb 		if (sa6_recoverscope(&saun->sin6) != 0)
607413628a7SBjoern A. Zeeb 			return (ESRCH);
6089c79d243SJamie Gritton 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
609413628a7SBjoern A. Zeeb 		break;
610413628a7SBjoern A. Zeeb 	}
611413628a7SBjoern A. Zeeb #endif
612413628a7SBjoern A. Zeeb 	default:
613413628a7SBjoern A. Zeeb 		return (ESRCH);
614413628a7SBjoern A. Zeeb 	}
615413628a7SBjoern A. Zeeb 	return (0);
616413628a7SBjoern A. Zeeb }
617413628a7SBjoern A. Zeeb 
618145bf6c0SAlexander V. Chernikov static int
fill_blackholeinfo(struct rt_addrinfo * info,union sockaddr_union * saun)619145bf6c0SAlexander V. Chernikov fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
620145bf6c0SAlexander V. Chernikov {
621145bf6c0SAlexander V. Chernikov 	struct ifaddr *ifa;
622145bf6c0SAlexander V. Chernikov 	sa_family_t saf;
623145bf6c0SAlexander V. Chernikov 
624145bf6c0SAlexander V. Chernikov 	if (V_loif == NULL) {
6256fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
626145bf6c0SAlexander V. Chernikov 		return (ENOTSUP);
627145bf6c0SAlexander V. Chernikov 	}
628145bf6c0SAlexander V. Chernikov 	info->rti_ifp = V_loif;
629145bf6c0SAlexander V. Chernikov 
630145bf6c0SAlexander V. Chernikov 	saf = info->rti_info[RTAX_DST]->sa_family;
631145bf6c0SAlexander V. Chernikov 
632145bf6c0SAlexander V. Chernikov 	CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
633145bf6c0SAlexander V. Chernikov 		if (ifa->ifa_addr->sa_family == saf) {
634145bf6c0SAlexander V. Chernikov 			info->rti_ifa = ifa;
635145bf6c0SAlexander V. Chernikov 			break;
636145bf6c0SAlexander V. Chernikov 		}
637145bf6c0SAlexander V. Chernikov 	}
6386fa8ed43SAlexander V. Chernikov 	if (info->rti_ifa == NULL) {
6396fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
640145bf6c0SAlexander V. Chernikov 		return (ENOTSUP);
6416fa8ed43SAlexander V. Chernikov 	}
642145bf6c0SAlexander V. Chernikov 
643145bf6c0SAlexander V. Chernikov 	bzero(saun, sizeof(union sockaddr_union));
644145bf6c0SAlexander V. Chernikov 	switch (saf) {
6458ca99aecSAlexander V. Chernikov #ifdef INET
646145bf6c0SAlexander V. Chernikov 	case AF_INET:
647145bf6c0SAlexander V. Chernikov 		saun->sin.sin_family = AF_INET;
648145bf6c0SAlexander V. Chernikov 		saun->sin.sin_len = sizeof(struct sockaddr_in);
649145bf6c0SAlexander V. Chernikov 		saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
650145bf6c0SAlexander V. Chernikov 		break;
6518ca99aecSAlexander V. Chernikov #endif
6528ca99aecSAlexander V. Chernikov #ifdef INET6
653145bf6c0SAlexander V. Chernikov 	case AF_INET6:
654145bf6c0SAlexander V. Chernikov 		saun->sin6.sin6_family = AF_INET6;
655145bf6c0SAlexander V. Chernikov 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
656145bf6c0SAlexander V. Chernikov 		saun->sin6.sin6_addr = in6addr_loopback;
657145bf6c0SAlexander V. Chernikov 		break;
6588ca99aecSAlexander V. Chernikov #endif
659145bf6c0SAlexander V. Chernikov 	default:
6606fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
661145bf6c0SAlexander V. Chernikov 		return (ENOTSUP);
662145bf6c0SAlexander V. Chernikov 	}
663145bf6c0SAlexander V. Chernikov 	info->rti_info[RTAX_GATEWAY] = &saun->sa;
664145bf6c0SAlexander V. Chernikov 	info->rti_flags |= RTF_GATEWAY;
665145bf6c0SAlexander V. Chernikov 
666145bf6c0SAlexander V. Chernikov 	return (0);
667145bf6c0SAlexander V. Chernikov }
668145bf6c0SAlexander V. Chernikov 
669c83dda36SAlexander V. Chernikov /*
670c83dda36SAlexander V. Chernikov  * Fills in @info based on userland-provided @rtm message.
671c83dda36SAlexander V. Chernikov  *
672c83dda36SAlexander V. Chernikov  * Returns 0 on success.
673c83dda36SAlexander V. Chernikov  */
674c83dda36SAlexander V. Chernikov static int
fill_addrinfo(struct rt_msghdr * rtm,int len,struct linear_buffer * lb,u_int fibnum,struct rt_addrinfo * info)675b31fbebeSAlexander V. Chernikov fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
676b31fbebeSAlexander V. Chernikov     struct rt_addrinfo *info)
677c83dda36SAlexander V. Chernikov {
678c83dda36SAlexander V. Chernikov 	int error;
679c83dda36SAlexander V. Chernikov 
680c83dda36SAlexander V. Chernikov 	rtm->rtm_pid = curproc->p_pid;
681c83dda36SAlexander V. Chernikov 	info->rti_addrs = rtm->rtm_addrs;
682c83dda36SAlexander V. Chernikov 
683c83dda36SAlexander V. Chernikov 	info->rti_mflags = rtm->rtm_inits;
684c83dda36SAlexander V. Chernikov 	info->rti_rmx = &rtm->rtm_rmx;
685c83dda36SAlexander V. Chernikov 
686c83dda36SAlexander V. Chernikov 	/*
687c83dda36SAlexander V. Chernikov 	 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
688c83dda36SAlexander V. Chernikov 	 * link-local address because rtrequest requires addresses with
689c83dda36SAlexander V. Chernikov 	 * embedded scope id.
690c83dda36SAlexander V. Chernikov 	 */
691c83dda36SAlexander V. Chernikov 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
692c83dda36SAlexander V. Chernikov 		return (EINVAL);
693c83dda36SAlexander V. Chernikov 
694c83dda36SAlexander V. Chernikov 	info->rti_flags = rtm->rtm_flags;
695b31fbebeSAlexander V. Chernikov 	error = cleanup_xaddrs(info, lb);
6962fe5a794SAlexander V. Chernikov 	if (error != 0)
6972fe5a794SAlexander V. Chernikov 		return (error);
698c83dda36SAlexander V. Chernikov 	/*
699c83dda36SAlexander V. Chernikov 	 * Verify that the caller has the appropriate privilege; RTM_GET
700c83dda36SAlexander V. Chernikov 	 * is the only operation the non-superuser is allowed.
701c83dda36SAlexander V. Chernikov 	 */
702c83dda36SAlexander V. Chernikov 	if (rtm->rtm_type != RTM_GET) {
703c83dda36SAlexander V. Chernikov 		error = priv_check(curthread, PRIV_NET_ROUTE);
704c83dda36SAlexander V. Chernikov 		if (error != 0)
705c83dda36SAlexander V. Chernikov 			return (error);
706c83dda36SAlexander V. Chernikov 	}
707c83dda36SAlexander V. Chernikov 
708c83dda36SAlexander V. Chernikov 	/*
709c83dda36SAlexander V. Chernikov 	 * The given gateway address may be an interface address.
710c83dda36SAlexander V. Chernikov 	 * For example, issuing a "route change" command on a route
711c83dda36SAlexander V. Chernikov 	 * entry that was created from a tunnel, and the gateway
712c83dda36SAlexander V. Chernikov 	 * address given is the local end point. In this case the
713c83dda36SAlexander V. Chernikov 	 * RTF_GATEWAY flag must be cleared or the destination will
714c83dda36SAlexander V. Chernikov 	 * not be reachable even though there is no error message.
715c83dda36SAlexander V. Chernikov 	 */
716c83dda36SAlexander V. Chernikov 	if (info->rti_info[RTAX_GATEWAY] != NULL &&
717c83dda36SAlexander V. Chernikov 	    info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
718036f1bc6SAlexander V. Chernikov 		struct nhop_object *nh;
719c83dda36SAlexander V. Chernikov 
720c83dda36SAlexander V. Chernikov 		/*
721c83dda36SAlexander V. Chernikov 		 * A host route through the loopback interface is
722ab6d773dSGordon Bergling 		 * installed for each interface address. In pre 8.0
723c83dda36SAlexander V. Chernikov 		 * releases the interface address of a PPP link type
724c83dda36SAlexander V. Chernikov 		 * is not reachable locally. This behavior is fixed as
725c83dda36SAlexander V. Chernikov 		 * part of the new L2/L3 redesign and rewrite work. The
726c83dda36SAlexander V. Chernikov 		 * signature of this interface address route is the
727aaad3c4fSAlexander V. Chernikov 		 * AF_LINK sa_family type of the gateway, and the
728c83dda36SAlexander V. Chernikov 		 * rt_ifp has the IFF_LOOPBACK flag set.
729c83dda36SAlexander V. Chernikov 		 */
730036f1bc6SAlexander V. Chernikov 		nh = rib_lookup(fibnum, info->rti_info[RTAX_GATEWAY], NHR_NONE, 0);
731036f1bc6SAlexander V. Chernikov 		if (nh != NULL && nh->gw_sa.sa_family == AF_LINK &&
732036f1bc6SAlexander V. Chernikov 		    nh->nh_ifp->if_flags & IFF_LOOPBACK) {
733c83dda36SAlexander V. Chernikov 				info->rti_flags &= ~RTF_GATEWAY;
734c83dda36SAlexander V. Chernikov 				info->rti_flags |= RTF_GWFLAG_COMPAT;
735c83dda36SAlexander V. Chernikov 		}
736c83dda36SAlexander V. Chernikov 	}
737c83dda36SAlexander V. Chernikov 
738c83dda36SAlexander V. Chernikov 	return (0);
739c83dda36SAlexander V. Chernikov }
740c83dda36SAlexander V. Chernikov 
741fedeb08bSAlexander V. Chernikov static struct nhop_object *
select_nhop(struct nhop_object * nh,const struct sockaddr * gw)742fedeb08bSAlexander V. Chernikov select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
743fedeb08bSAlexander V. Chernikov {
744fedeb08bSAlexander V. Chernikov 	if (!NH_IS_NHGRP(nh))
745fedeb08bSAlexander V. Chernikov 		return (nh);
746fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
747ae6bfd12SAlexander V. Chernikov 	const struct weightened_nhop *wn;
748fedeb08bSAlexander V. Chernikov 	uint32_t num_nhops;
749fedeb08bSAlexander V. Chernikov 	wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
750fedeb08bSAlexander V. Chernikov 	if (gw == NULL)
751fedeb08bSAlexander V. Chernikov 		return (wn[0].nh);
752fedeb08bSAlexander V. Chernikov 	for (int i = 0; i < num_nhops; i++) {
753fedeb08bSAlexander V. Chernikov 		if (match_nhop_gw(wn[i].nh, gw))
754fedeb08bSAlexander V. Chernikov 			return (wn[i].nh);
755fedeb08bSAlexander V. Chernikov 	}
756fedeb08bSAlexander V. Chernikov #endif
757fedeb08bSAlexander V. Chernikov 	return (NULL);
758fedeb08bSAlexander V. Chernikov }
759fedeb08bSAlexander V. Chernikov 
760c83dda36SAlexander V. Chernikov /*
761c83dda36SAlexander V. Chernikov  * Handles RTM_GET message from routing socket, returning matching rt.
762c83dda36SAlexander V. Chernikov  *
763c83dda36SAlexander V. Chernikov  * Returns:
764c83dda36SAlexander V. Chernikov  * 0 on success, with locked and referenced matching rt in @rt_nrt
765c83dda36SAlexander V. Chernikov  * errno of failure
766c83dda36SAlexander V. Chernikov  */
767c83dda36SAlexander V. Chernikov static int
handle_rtm_get(struct rt_addrinfo * info,u_int fibnum,struct rt_msghdr * rtm,struct rib_cmd_info * rc)768c83dda36SAlexander V. Chernikov handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
769a287a973SAlexander V. Chernikov     struct rt_msghdr *rtm, struct rib_cmd_info *rc)
770c83dda36SAlexander V. Chernikov {
771c83dda36SAlexander V. Chernikov 	RIB_RLOCK_TRACKER;
772c83dda36SAlexander V. Chernikov 	struct rib_head *rnh;
773fedeb08bSAlexander V. Chernikov 	struct nhop_object *nh;
774c83dda36SAlexander V. Chernikov 	sa_family_t saf;
775c83dda36SAlexander V. Chernikov 
776c83dda36SAlexander V. Chernikov 	saf = info->rti_info[RTAX_DST]->sa_family;
777c83dda36SAlexander V. Chernikov 
778c83dda36SAlexander V. Chernikov 	rnh = rt_tables_get_rnh(fibnum, saf);
779c83dda36SAlexander V. Chernikov 	if (rnh == NULL)
780c83dda36SAlexander V. Chernikov 		return (EAFNOSUPPORT);
781c83dda36SAlexander V. Chernikov 
782c83dda36SAlexander V. Chernikov 	RIB_RLOCK(rnh);
783c83dda36SAlexander V. Chernikov 
7842fe5a794SAlexander V. Chernikov 	/*
7852fe5a794SAlexander V. Chernikov 	 * By (implicit) convention host route (one without netmask)
7862fe5a794SAlexander V. Chernikov 	 * means longest-prefix-match request and the route with netmask
7872fe5a794SAlexander V. Chernikov 	 * means exact-match lookup.
7882fe5a794SAlexander V. Chernikov 	 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
7892fe5a794SAlexander V. Chernikov 	 * prefixes, use original data to check for the netmask presence.
7902fe5a794SAlexander V. Chernikov 	 */
7912fe5a794SAlexander V. Chernikov 	if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
792c83dda36SAlexander V. Chernikov 		/*
793c83dda36SAlexander V. Chernikov 		 * Provide longest prefix match for
794c83dda36SAlexander V. Chernikov 		 * address lookup (no mask).
795c83dda36SAlexander V. Chernikov 		 * 'route -n get addr'
796c83dda36SAlexander V. Chernikov 		 */
797a287a973SAlexander V. Chernikov 		rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
798c83dda36SAlexander V. Chernikov 		    info->rti_info[RTAX_DST], &rnh->head);
799c83dda36SAlexander V. Chernikov 	} else
800a287a973SAlexander V. Chernikov 		rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
801c83dda36SAlexander V. Chernikov 		    info->rti_info[RTAX_DST],
802c83dda36SAlexander V. Chernikov 		    info->rti_info[RTAX_NETMASK], &rnh->head);
803c83dda36SAlexander V. Chernikov 
804a287a973SAlexander V. Chernikov 	if (rc->rc_rt == NULL) {
805c83dda36SAlexander V. Chernikov 		RIB_RUNLOCK(rnh);
806c83dda36SAlexander V. Chernikov 		return (ESRCH);
807c83dda36SAlexander V. Chernikov 	}
808fedeb08bSAlexander V. Chernikov 
809d68fb8d9SAlexander V. Chernikov 	nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
810fedeb08bSAlexander V. Chernikov 	if (nh == NULL) {
811c83dda36SAlexander V. Chernikov 		RIB_RUNLOCK(rnh);
812c83dda36SAlexander V. Chernikov 		return (ESRCH);
813c83dda36SAlexander V. Chernikov 	}
814c83dda36SAlexander V. Chernikov 	/*
815c83dda36SAlexander V. Chernikov 	 * If performing proxied L2 entry insertion, and
816c83dda36SAlexander V. Chernikov 	 * the actual PPP host entry is found, perform
817c83dda36SAlexander V. Chernikov 	 * another search to retrieve the prefix route of
818c83dda36SAlexander V. Chernikov 	 * the local end point of the PPP link.
819aaad3c4fSAlexander V. Chernikov 	 * TODO: move this logic to userland.
820c83dda36SAlexander V. Chernikov 	 */
821c83dda36SAlexander V. Chernikov 	if (rtm->rtm_flags & RTF_ANNOUNCE) {
8229573cc35SKurosawa Takahiro 		struct sockaddr_storage laddr;
823c83dda36SAlexander V. Chernikov 
824aaad3c4fSAlexander V. Chernikov 		if (nh->nh_ifp != NULL &&
825aaad3c4fSAlexander V. Chernikov 		    nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
826c83dda36SAlexander V. Chernikov 			struct ifaddr *ifa;
827c83dda36SAlexander V. Chernikov 
828c83dda36SAlexander V. Chernikov 			ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
829c83dda36SAlexander V. Chernikov 					RT_ALL_FIBS);
830c83dda36SAlexander V. Chernikov 			if (ifa != NULL)
831c83dda36SAlexander V. Chernikov 				rt_maskedcopy(ifa->ifa_addr,
8329573cc35SKurosawa Takahiro 					      (struct sockaddr *)&laddr,
833c83dda36SAlexander V. Chernikov 					      ifa->ifa_netmask);
834c83dda36SAlexander V. Chernikov 		} else
835aaad3c4fSAlexander V. Chernikov 			rt_maskedcopy(nh->nh_ifa->ifa_addr,
8369573cc35SKurosawa Takahiro 				      (struct sockaddr *)&laddr,
837aaad3c4fSAlexander V. Chernikov 				      nh->nh_ifa->ifa_netmask);
838c83dda36SAlexander V. Chernikov 		/*
839c83dda36SAlexander V. Chernikov 		 * refactor rt and no lock operation necessary
840c83dda36SAlexander V. Chernikov 		 */
8419573cc35SKurosawa Takahiro 		rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
8429573cc35SKurosawa Takahiro 		    (struct sockaddr *)&laddr, &rnh->head);
843a287a973SAlexander V. Chernikov 		if (rc->rc_rt == NULL) {
844c83dda36SAlexander V. Chernikov 			RIB_RUNLOCK(rnh);
845c83dda36SAlexander V. Chernikov 			return (ESRCH);
846c83dda36SAlexander V. Chernikov 		}
847d68fb8d9SAlexander V. Chernikov 		nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
848fedeb08bSAlexander V. Chernikov 		if (nh == NULL) {
849fedeb08bSAlexander V. Chernikov 			RIB_RUNLOCK(rnh);
850fedeb08bSAlexander V. Chernikov 			return (ESRCH);
851c83dda36SAlexander V. Chernikov 		}
852fedeb08bSAlexander V. Chernikov 	}
853fedeb08bSAlexander V. Chernikov 	rc->rc_nh_new = nh;
8542259a030SAlexander V. Chernikov 	rc->rc_nh_weight = rc->rc_rt->rt_weight;
855c83dda36SAlexander V. Chernikov 	RIB_RUNLOCK(rnh);
856c83dda36SAlexander V. Chernikov 
857c83dda36SAlexander V. Chernikov 	return (0);
858c83dda36SAlexander V. Chernikov }
859c83dda36SAlexander V. Chernikov 
860d68fb8d9SAlexander V. Chernikov static void
init_sockaddrs_family(int family,struct sockaddr * dst,struct sockaddr * mask)861d68fb8d9SAlexander V. Chernikov init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
862d68fb8d9SAlexander V. Chernikov {
863d68fb8d9SAlexander V. Chernikov #ifdef INET
864d68fb8d9SAlexander V. Chernikov 	if (family == AF_INET) {
865d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
866d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
867d68fb8d9SAlexander V. Chernikov 
868d68fb8d9SAlexander V. Chernikov 		bzero(dst4, sizeof(struct sockaddr_in));
869d68fb8d9SAlexander V. Chernikov 		bzero(mask4, sizeof(struct sockaddr_in));
870d68fb8d9SAlexander V. Chernikov 
871d68fb8d9SAlexander V. Chernikov 		dst4->sin_family = AF_INET;
872d68fb8d9SAlexander V. Chernikov 		dst4->sin_len = sizeof(struct sockaddr_in);
873d68fb8d9SAlexander V. Chernikov 		mask4->sin_family = AF_INET;
874d68fb8d9SAlexander V. Chernikov 		mask4->sin_len = sizeof(struct sockaddr_in);
875d68fb8d9SAlexander V. Chernikov 	}
876d68fb8d9SAlexander V. Chernikov #endif
877d68fb8d9SAlexander V. Chernikov #ifdef INET6
878d68fb8d9SAlexander V. Chernikov 	if (family == AF_INET6) {
879d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
880d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
881d68fb8d9SAlexander V. Chernikov 
882d68fb8d9SAlexander V. Chernikov 		bzero(dst6, sizeof(struct sockaddr_in6));
883d68fb8d9SAlexander V. Chernikov 		bzero(mask6, sizeof(struct sockaddr_in6));
884d68fb8d9SAlexander V. Chernikov 
885d68fb8d9SAlexander V. Chernikov 		dst6->sin6_family = AF_INET6;
886d68fb8d9SAlexander V. Chernikov 		dst6->sin6_len = sizeof(struct sockaddr_in6);
887d68fb8d9SAlexander V. Chernikov 		mask6->sin6_family = AF_INET6;
888d68fb8d9SAlexander V. Chernikov 		mask6->sin6_len = sizeof(struct sockaddr_in6);
889d68fb8d9SAlexander V. Chernikov 	}
890d68fb8d9SAlexander V. Chernikov #endif
891d68fb8d9SAlexander V. Chernikov }
892d68fb8d9SAlexander V. Chernikov 
893d68fb8d9SAlexander V. Chernikov static void
export_rtaddrs(const struct rtentry * rt,struct sockaddr * dst,struct sockaddr * mask)894d68fb8d9SAlexander V. Chernikov export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
895d68fb8d9SAlexander V. Chernikov     struct sockaddr *mask)
896d68fb8d9SAlexander V. Chernikov {
897d68fb8d9SAlexander V. Chernikov #ifdef INET
898d68fb8d9SAlexander V. Chernikov 	if (dst->sa_family == AF_INET) {
899d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
900d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
9012fb4a03dSRyan Libby 		uint32_t scopeid = 0;
902d68fb8d9SAlexander V. Chernikov 		rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
903d68fb8d9SAlexander V. Chernikov 		    &scopeid);
904d68fb8d9SAlexander V. Chernikov 		return;
905d68fb8d9SAlexander V. Chernikov 	}
906d68fb8d9SAlexander V. Chernikov #endif
907d68fb8d9SAlexander V. Chernikov #ifdef INET6
908d68fb8d9SAlexander V. Chernikov 	if (dst->sa_family == AF_INET6) {
909d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
910d68fb8d9SAlexander V. Chernikov 		struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
9112fb4a03dSRyan Libby 		uint32_t scopeid = 0;
9122fb4a03dSRyan Libby 		rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
9132fb4a03dSRyan Libby 		    &mask6->sin6_addr, &scopeid);
914d68fb8d9SAlexander V. Chernikov 		dst6->sin6_scope_id = scopeid;
915d68fb8d9SAlexander V. Chernikov 		return;
916d68fb8d9SAlexander V. Chernikov 	}
917d68fb8d9SAlexander V. Chernikov #endif
918d68fb8d9SAlexander V. Chernikov }
919d68fb8d9SAlexander V. Chernikov 
920b31fbebeSAlexander V. Chernikov static int
update_rtm_from_info(struct rt_addrinfo * info,struct rt_msghdr ** prtm,int alloc_len)921b31fbebeSAlexander V. Chernikov update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
922b31fbebeSAlexander V. Chernikov     int alloc_len)
923b31fbebeSAlexander V. Chernikov {
924b31fbebeSAlexander V. Chernikov 	struct rt_msghdr *rtm, *orig_rtm = NULL;
925b31fbebeSAlexander V. Chernikov 	struct walkarg w;
926b31fbebeSAlexander V. Chernikov 	int len;
927b31fbebeSAlexander V. Chernikov 
928b31fbebeSAlexander V. Chernikov 	rtm = *prtm;
929b31fbebeSAlexander V. Chernikov 	/* Check if we need to realloc storage */
930b31fbebeSAlexander V. Chernikov 	rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
931b31fbebeSAlexander V. Chernikov 	if (len > alloc_len) {
932b31fbebeSAlexander V. Chernikov 		struct rt_msghdr *tmp_rtm;
933b31fbebeSAlexander V. Chernikov 
934b31fbebeSAlexander V. Chernikov 		tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
935b31fbebeSAlexander V. Chernikov 		if (tmp_rtm == NULL)
936b31fbebeSAlexander V. Chernikov 			return (ENOBUFS);
937b31fbebeSAlexander V. Chernikov 		bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
938b31fbebeSAlexander V. Chernikov 		orig_rtm = rtm;
939b31fbebeSAlexander V. Chernikov 		rtm = tmp_rtm;
940b31fbebeSAlexander V. Chernikov 		alloc_len = len;
941b31fbebeSAlexander V. Chernikov 
942b31fbebeSAlexander V. Chernikov 		/*
943b31fbebeSAlexander V. Chernikov 		 * Delay freeing original rtm as info contains
944b31fbebeSAlexander V. Chernikov 		 * data referencing it.
945b31fbebeSAlexander V. Chernikov 		 */
946b31fbebeSAlexander V. Chernikov 	}
947b31fbebeSAlexander V. Chernikov 
948dae64402SGleb Smirnoff 	w = (struct walkarg ){
949dae64402SGleb Smirnoff 		.w_tmem = (caddr_t)rtm,
950dae64402SGleb Smirnoff 		.w_tmemsize = alloc_len,
951dae64402SGleb Smirnoff 	};
952b31fbebeSAlexander V. Chernikov 	rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
953b31fbebeSAlexander V. Chernikov 	rtm->rtm_addrs = info->rti_addrs;
954b31fbebeSAlexander V. Chernikov 
955b31fbebeSAlexander V. Chernikov 	if (orig_rtm != NULL)
956b31fbebeSAlexander V. Chernikov 		free(orig_rtm, M_TEMP);
957b31fbebeSAlexander V. Chernikov 	*prtm = rtm;
958b31fbebeSAlexander V. Chernikov 	return (0);
959b31fbebeSAlexander V. Chernikov }
960b31fbebeSAlexander V. Chernikov 
961d68fb8d9SAlexander V. Chernikov 
962c83dda36SAlexander V. Chernikov /*
963592d300eSAlexander V. Chernikov  * Update sockaddrs, flags, etc in @prtm based on @rc data.
964c83dda36SAlexander V. Chernikov  * rtm can be reallocated.
965c83dda36SAlexander V. Chernikov  *
966c83dda36SAlexander V. Chernikov  * Returns 0 on success, along with pointer to (potentially reallocated)
967c83dda36SAlexander V. Chernikov  *  rtm.
968c83dda36SAlexander V. Chernikov  *
969c83dda36SAlexander V. Chernikov  */
970c83dda36SAlexander V. Chernikov static int
update_rtm_from_rc(struct rt_addrinfo * info,struct rt_msghdr ** prtm,int alloc_len,struct rib_cmd_info * rc,struct nhop_object * nh)971592d300eSAlexander V. Chernikov update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
972592d300eSAlexander V. Chernikov     int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
973c83dda36SAlexander V. Chernikov {
974c83dda36SAlexander V. Chernikov 	union sockaddr_union saun;
975b31fbebeSAlexander V. Chernikov 	struct rt_msghdr *rtm;
976c83dda36SAlexander V. Chernikov 	struct ifnet *ifp;
977b31fbebeSAlexander V. Chernikov 	int error;
978c83dda36SAlexander V. Chernikov 
979c83dda36SAlexander V. Chernikov 	rtm = *prtm;
980d68fb8d9SAlexander V. Chernikov 	union sockaddr_union sa_dst, sa_mask;
981d68fb8d9SAlexander V. Chernikov 	int family = info->rti_info[RTAX_DST]->sa_family;
982d68fb8d9SAlexander V. Chernikov 	init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
983d68fb8d9SAlexander V. Chernikov 	export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
984c83dda36SAlexander V. Chernikov 
985d68fb8d9SAlexander V. Chernikov 	info->rti_info[RTAX_DST] = &sa_dst.sa;
986d68fb8d9SAlexander V. Chernikov 	info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
987aaad3c4fSAlexander V. Chernikov 	info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
988c83dda36SAlexander V. Chernikov 	info->rti_info[RTAX_GENMASK] = 0;
989aaad3c4fSAlexander V. Chernikov 	ifp = nh->nh_ifp;
990c83dda36SAlexander V. Chernikov 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
991c83dda36SAlexander V. Chernikov 		if (ifp) {
992c83dda36SAlexander V. Chernikov 			info->rti_info[RTAX_IFP] =
993c83dda36SAlexander V. Chernikov 			    ifp->if_addr->ifa_addr;
994aaad3c4fSAlexander V. Chernikov 			error = rtm_get_jailed(info, ifp, nh,
995c83dda36SAlexander V. Chernikov 			    &saun, curthread->td_ucred);
996c83dda36SAlexander V. Chernikov 			if (error != 0)
997c83dda36SAlexander V. Chernikov 				return (error);
998c83dda36SAlexander V. Chernikov 			if (ifp->if_flags & IFF_POINTOPOINT)
999c83dda36SAlexander V. Chernikov 				info->rti_info[RTAX_BRD] =
1000aaad3c4fSAlexander V. Chernikov 				    nh->nh_ifa->ifa_dstaddr;
1001c83dda36SAlexander V. Chernikov 			rtm->rtm_index = ifp->if_index;
1002c83dda36SAlexander V. Chernikov 		} else {
1003c83dda36SAlexander V. Chernikov 			info->rti_info[RTAX_IFP] = NULL;
1004c83dda36SAlexander V. Chernikov 			info->rti_info[RTAX_IFA] = NULL;
1005c83dda36SAlexander V. Chernikov 		}
1006c83dda36SAlexander V. Chernikov 	} else if (ifp != NULL)
1007c83dda36SAlexander V. Chernikov 		rtm->rtm_index = ifp->if_index;
1008c83dda36SAlexander V. Chernikov 
1009b31fbebeSAlexander V. Chernikov 	if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
1010b31fbebeSAlexander V. Chernikov 		return (error);
1011c83dda36SAlexander V. Chernikov 
101276cfc6faSAlexander V. Chernikov 	rtm = *prtm;
1013592d300eSAlexander V. Chernikov 	rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
1014592d300eSAlexander V. Chernikov 	if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
1015c83dda36SAlexander V. Chernikov 		rtm->rtm_flags = RTF_GATEWAY |
1016592d300eSAlexander V. Chernikov 			(rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
1017592d300eSAlexander V. Chernikov 	rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
1018592d300eSAlexander V. Chernikov 	rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
1019c83dda36SAlexander V. Chernikov 
1020c83dda36SAlexander V. Chernikov 	return (0);
1021c83dda36SAlexander V. Chernikov }
1022c83dda36SAlexander V. Chernikov 
10239c584fa4SAlexander V. Chernikov #ifdef ROUTE_MPATH
1024fedeb08bSAlexander V. Chernikov static void
save_del_notification(const struct rib_cmd_info * rc,void * _cbdata)1025177f04d5SAlexander V. Chernikov save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
1026fedeb08bSAlexander V. Chernikov {
1027fedeb08bSAlexander V. Chernikov 	struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1028fedeb08bSAlexander V. Chernikov 
1029fedeb08bSAlexander V. Chernikov 	if (rc->rc_cmd == RTM_DELETE)
1030fedeb08bSAlexander V. Chernikov 		*rc_new = *rc;
1031fedeb08bSAlexander V. Chernikov }
1032fedeb08bSAlexander V. Chernikov 
1033fedeb08bSAlexander V. Chernikov static void
save_add_notification(const struct rib_cmd_info * rc,void * _cbdata)1034177f04d5SAlexander V. Chernikov save_add_notification(const struct rib_cmd_info *rc, void *_cbdata)
1035fedeb08bSAlexander V. Chernikov {
1036fedeb08bSAlexander V. Chernikov 	struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1037fedeb08bSAlexander V. Chernikov 
1038fedeb08bSAlexander V. Chernikov 	if (rc->rc_cmd == RTM_ADD)
1039fedeb08bSAlexander V. Chernikov 		*rc_new = *rc;
1040fedeb08bSAlexander V. Chernikov }
10419c584fa4SAlexander V. Chernikov #endif
1042fedeb08bSAlexander V. Chernikov 
1043a3c2c06bSBjoern A. Zeeb #if defined(INET6) || defined(INET)
1044b31fbebeSAlexander V. Chernikov static struct sockaddr *
alloc_sockaddr_aligned(struct linear_buffer * lb,int len)1045b31fbebeSAlexander V. Chernikov alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1046b31fbebeSAlexander V. Chernikov {
104725682e6aSAlexander V. Chernikov 	len = roundup2(len, sizeof(uint64_t));
1048b31fbebeSAlexander V. Chernikov 	if (lb->offset + len > lb->size)
1049b31fbebeSAlexander V. Chernikov 		return (NULL);
1050b31fbebeSAlexander V. Chernikov 	struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1051b31fbebeSAlexander V. Chernikov 	lb->offset += len;
1052b31fbebeSAlexander V. Chernikov 	return (sa);
1053b31fbebeSAlexander V. Chernikov }
1054a3c2c06bSBjoern A. Zeeb #endif
1055b31fbebeSAlexander V. Chernikov 
105652041295SPoul-Henning Kamp static int
rts_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)105736b10ac2SGleb Smirnoff rts_send(struct socket *so, int flags, struct mbuf *m,
105836b10ac2SGleb Smirnoff     struct sockaddr *nam, struct mbuf *control, struct thread *td)
1059df8bae1dSRodney W. Grimes {
10605dfc91d7SLuigi Rizzo 	struct rt_msghdr *rtm = NULL;
1061df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
1062b8a6e03fSGleb Smirnoff 	struct epoch_tracker et;
10633deb3649SAlexander V. Chernikov #ifdef INET6
1064d9302031SAlexander V. Chernikov 	struct sockaddr_storage ss;
10656bbfef90SHiroki Sato 	struct sockaddr_in6 *sin6;
10660bebb544SHiroki Sato 	int i, rti_need_deembed = 0;
10676bbfef90SHiroki Sato #endif
1068f2e5eb36SAlexander V. Chernikov 	int alloc_len = 0, len, error = 0, fibnum;
10693ca1a2d6SMax Laier 	sa_family_t saf = AF_UNSPEC;
1070a287a973SAlexander V. Chernikov 	struct rib_cmd_info rc;
1071a287a973SAlexander V. Chernikov 	struct nhop_object *nh;
1072df8bae1dSRodney W. Grimes 
107336b10ac2SGleb Smirnoff 	if ((flags & PRUS_OOB) || control != NULL) {
107436b10ac2SGleb Smirnoff 		m_freem(m);
107536b10ac2SGleb Smirnoff 		if (control != NULL)
107636b10ac2SGleb Smirnoff 			m_freem(control);
107736b10ac2SGleb Smirnoff 		return (EOPNOTSUPP);
107836b10ac2SGleb Smirnoff 	}
107936b10ac2SGleb Smirnoff 
1080f5d9a696SAlexander V. Chernikov 	fibnum = so->so_fibnum;
1081df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;}
10825dfc91d7SLuigi Rizzo 	if (m == NULL || ((m->m_len < sizeof(long)) &&
10835dfc91d7SLuigi Rizzo 		       (m = m_pullup(m, sizeof(long))) == NULL))
1084df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1085df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
1086df8bae1dSRodney W. Grimes 		panic("route_output");
1087b8a6e03fSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1088df8bae1dSRodney W. Grimes 	len = m->m_pkthdr.len;
1089df8bae1dSRodney W. Grimes 	if (len < sizeof(*rtm) ||
1090f1fcb552SAlexander V. Chernikov 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1091df8bae1dSRodney W. Grimes 		senderr(EINVAL);
1092f1fcb552SAlexander V. Chernikov 
1093f2e5eb36SAlexander V. Chernikov 	/*
1094f2e5eb36SAlexander V. Chernikov 	 * Most of current messages are in range 200-240 bytes,
1095d9437c0fSAlexander V. Chernikov 	 * minimize possible re-allocation on reply using larger size
1096d9437c0fSAlexander V. Chernikov 	 * buffer aligned on 1k boundaty.
1097f2e5eb36SAlexander V. Chernikov 	 */
1098f2e5eb36SAlexander V. Chernikov 	alloc_len = roundup2(len, 1024);
1099b31fbebeSAlexander V. Chernikov 	int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1100b31fbebeSAlexander V. Chernikov 	if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1101df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
1102f2e5eb36SAlexander V. Chernikov 
1103df8bae1dSRodney W. Grimes 	m_copydata(m, 0, len, (caddr_t)rtm);
1104f1fcb552SAlexander V. Chernikov 	bzero(&info, sizeof(info));
1105a287a973SAlexander V. Chernikov 	nh = NULL;
1106b31fbebeSAlexander V. Chernikov 	struct linear_buffer lb = {
1107b31fbebeSAlexander V. Chernikov 		.base = (char *)rtm + alloc_len,
1108b31fbebeSAlexander V. Chernikov 		.size = SCRATCH_BUFFER_SIZE,
1109b31fbebeSAlexander V. Chernikov 	};
1110f1fcb552SAlexander V. Chernikov 
1111df8bae1dSRodney W. Grimes 	if (rtm->rtm_version != RTM_VERSION) {
1112f1fcb552SAlexander V. Chernikov 		/* Do not touch message since format is unknown */
1113f2e5eb36SAlexander V. Chernikov 		free(rtm, M_TEMP);
1114f1fcb552SAlexander V. Chernikov 		rtm = NULL;
1115df8bae1dSRodney W. Grimes 		senderr(EPROTONOSUPPORT);
1116df8bae1dSRodney W. Grimes 	}
1117f1fcb552SAlexander V. Chernikov 
1118f1fcb552SAlexander V. Chernikov 	/*
1119f1fcb552SAlexander V. Chernikov 	 * Starting from here, it is possible
1120f1fcb552SAlexander V. Chernikov 	 * to alter original message and insert
1121f1fcb552SAlexander V. Chernikov 	 * caller PID and error value.
1122f1fcb552SAlexander V. Chernikov 	 */
1123f1fcb552SAlexander V. Chernikov 
1124b31fbebeSAlexander V. Chernikov 	if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1125dadb6c3bSRuslan Ermilov 		senderr(error);
1126acd3428bSRobert Watson 	}
1127b31fbebeSAlexander V. Chernikov 	/* fill_addringo() embeds scope into IPv6 addresses */
1128b31fbebeSAlexander V. Chernikov #ifdef INET6
1129b31fbebeSAlexander V. Chernikov 	rti_need_deembed = 1;
1130b31fbebeSAlexander V. Chernikov #endif
1131162c0b2eSRuslan Ermilov 
1132c83dda36SAlexander V. Chernikov 	saf = info.rti_info[RTAX_DST]->sa_family;
11339231d35fSQing Li 
1134c83dda36SAlexander V. Chernikov 	/* support for new ARP code */
1135c83dda36SAlexander V. Chernikov 	if (rtm->rtm_flags & RTF_LLDATA) {
1136c83dda36SAlexander V. Chernikov 		error = lla_rt_output(rtm, &info);
1137c83dda36SAlexander V. Chernikov 		goto flush;
11389231d35fSQing Li 	}
11399231d35fSQing Li 
1140145bf6c0SAlexander V. Chernikov 	union sockaddr_union gw_saun;
1141145bf6c0SAlexander V. Chernikov 	int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1142145bf6c0SAlexander V. Chernikov 	if (blackhole_flags != 0) {
1143145bf6c0SAlexander V. Chernikov 		if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1144145bf6c0SAlexander V. Chernikov 			error = fill_blackholeinfo(&info, &gw_saun);
11456fa8ed43SAlexander V. Chernikov 		else {
11466fa8ed43SAlexander V. Chernikov 			RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1147145bf6c0SAlexander V. Chernikov 			error = EINVAL;
11486fa8ed43SAlexander V. Chernikov 		}
1149145bf6c0SAlexander V. Chernikov 		if (error != 0)
1150145bf6c0SAlexander V. Chernikov 			senderr(error);
1151145bf6c0SAlexander V. Chernikov 	}
1152145bf6c0SAlexander V. Chernikov 
1153df8bae1dSRodney W. Grimes 	switch (rtm->rtm_type) {
1154df8bae1dSRodney W. Grimes 	case RTM_ADD:
1155c77462ddSAlexander V. Chernikov 	case RTM_CHANGE:
1156b3b6ff23SRyan Stone 		if (rtm->rtm_type == RTM_ADD) {
11576fa8ed43SAlexander V. Chernikov 			if (info.rti_info[RTAX_GATEWAY] == NULL) {
11586fa8ed43SAlexander V. Chernikov 				RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1159df8bae1dSRodney W. Grimes 				senderr(EINVAL);
1160b3b6ff23SRyan Stone 			}
11616fa8ed43SAlexander V. Chernikov 		}
1162a287a973SAlexander V. Chernikov 		error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1163a287a973SAlexander V. Chernikov 		if (error == 0) {
11647e5bf684SAlexander V. Chernikov 			rtsock_notify_event(fibnum, &rc);
1165fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
1166fedeb08bSAlexander V. Chernikov 			if (NH_IS_NHGRP(rc.rc_nh_new) ||
1167fedeb08bSAlexander V. Chernikov 			    (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1168fedeb08bSAlexander V. Chernikov 				struct rib_cmd_info rc_simple = {};
1169fedeb08bSAlexander V. Chernikov 				rib_decompose_notification(&rc,
1170fedeb08bSAlexander V. Chernikov 				    save_add_notification, (void *)&rc_simple);
1171fedeb08bSAlexander V. Chernikov 				rc = rc_simple;
1172fedeb08bSAlexander V. Chernikov 			}
1173fedeb08bSAlexander V. Chernikov #endif
1174c260d5cdSAlexander V. Chernikov 			/* nh MAY be empty if RTM_CHANGE request is no-op */
1175a287a973SAlexander V. Chernikov 			nh = rc.rc_nh_new;
1176c260d5cdSAlexander V. Chernikov 			if (nh != NULL) {
1177592d300eSAlexander V. Chernikov 				rtm->rtm_index = nh->nh_ifp->if_index;
11781b95005eSAlexander V. Chernikov 				rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1179df8bae1dSRodney W. Grimes 			}
1180c260d5cdSAlexander V. Chernikov 		}
1181df8bae1dSRodney W. Grimes 		break;
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes 	case RTM_DELETE:
1184a287a973SAlexander V. Chernikov 		error = rib_action(fibnum, RTM_DELETE, &info, &rc);
118578a82810SGarrett Wollman 		if (error == 0) {
11867e5bf684SAlexander V. Chernikov 			rtsock_notify_event(fibnum, &rc);
1187fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
1188fedeb08bSAlexander V. Chernikov 			if (NH_IS_NHGRP(rc.rc_nh_old) ||
1189fedeb08bSAlexander V. Chernikov 			    (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1190fedeb08bSAlexander V. Chernikov 				struct rib_cmd_info rc_simple = {};
1191fedeb08bSAlexander V. Chernikov 				rib_decompose_notification(&rc,
1192fedeb08bSAlexander V. Chernikov 				    save_del_notification, (void *)&rc_simple);
1193fedeb08bSAlexander V. Chernikov 				rc = rc_simple;
1194fedeb08bSAlexander V. Chernikov 			}
1195fedeb08bSAlexander V. Chernikov #endif
1196a287a973SAlexander V. Chernikov 			nh = rc.rc_nh_old;
119778a82810SGarrett Wollman 		}
1198df8bae1dSRodney W. Grimes 		break;
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes 	case RTM_GET:
1201a287a973SAlexander V. Chernikov 		error = handle_rtm_get(&info, fibnum, rtm, &rc);
1202c83dda36SAlexander V. Chernikov 		if (error != 0)
1203c83dda36SAlexander V. Chernikov 			senderr(error);
1204a287a973SAlexander V. Chernikov 		nh = rc.rc_nh_new;
1205956b0b65SJeffrey Hsu 
12062cda6a2fSAlexander V. Chernikov 		if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1207813dd6aeSBjoern A. Zeeb 			senderr(ESRCH);
1208b31fbebeSAlexander V. Chernikov 		break;
1209a287a973SAlexander V. Chernikov 
1210b31fbebeSAlexander V. Chernikov 	default:
1211b31fbebeSAlexander V. Chernikov 		senderr(EOPNOTSUPP);
1212b31fbebeSAlexander V. Chernikov 	}
1213b31fbebeSAlexander V. Chernikov 
1214c260d5cdSAlexander V. Chernikov 	if (error == 0 && nh != NULL) {
1215592d300eSAlexander V. Chernikov 		error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1216c83dda36SAlexander V. Chernikov 		/*
1217c83dda36SAlexander V. Chernikov 		 * Note that some sockaddr pointers may have changed to
1218c83dda36SAlexander V. Chernikov 		 * point to memory outsize @rtm. Some may be pointing
1219c83dda36SAlexander V. Chernikov 		 * to the on-stack variables.
1220c83dda36SAlexander V. Chernikov 		 * Given that, any pointer in @info CANNOT BE USED.
1221c83dda36SAlexander V. Chernikov 		 */
1222c83dda36SAlexander V. Chernikov 
1223c83dda36SAlexander V. Chernikov 		/*
1224c83dda36SAlexander V. Chernikov 		 * scopeid deembedding has been performed while
1225c83dda36SAlexander V. Chernikov 		 * writing updated rtm in rtsock_msg_buffer().
1226c83dda36SAlexander V. Chernikov 		 * With that in mind, skip deembedding procedure below.
1227c83dda36SAlexander V. Chernikov 		 */
1228d9302031SAlexander V. Chernikov #ifdef INET6
1229c83dda36SAlexander V. Chernikov 		rti_need_deembed = 0;
1230d9302031SAlexander V. Chernikov #endif
1231df8bae1dSRodney W. Grimes 	}
1232df8bae1dSRodney W. Grimes 
1233df8bae1dSRodney W. Grimes flush:
1234b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
123592c227afSAlexander V. Chernikov 
12360bebb544SHiroki Sato #ifdef INET6
1237c83dda36SAlexander V. Chernikov 	if (rtm != NULL) {
12380bebb544SHiroki Sato 		if (rti_need_deembed) {
12390bebb544SHiroki Sato 			/* sin6_scope_id is recovered before sending rtm. */
12400bebb544SHiroki Sato 			sin6 = (struct sockaddr_in6 *)&ss;
124112bdf23aSHiroki Sato 			for (i = 0; i < RTAX_MAX; i++) {
12420bebb544SHiroki Sato 				if (info.rti_info[i] == NULL)
12430bebb544SHiroki Sato 					continue;
12440bebb544SHiroki Sato 				if (info.rti_info[i]->sa_family != AF_INET6)
12450bebb544SHiroki Sato 					continue;
12460bebb544SHiroki Sato 				bcopy(info.rti_info[i], sin6, sizeof(*sin6));
12470bebb544SHiroki Sato 				if (sa6_recoverscope(sin6) == 0)
12480bebb544SHiroki Sato 					bcopy(sin6, info.rti_info[i],
12490bebb544SHiroki Sato 						    sizeof(*sin6));
12500bebb544SHiroki Sato 			}
1251b31fbebeSAlexander V. Chernikov 			if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1252b31fbebeSAlexander V. Chernikov 				if (error != 0)
1253b31fbebeSAlexander V. Chernikov 					error = ENOBUFS;
1254b31fbebeSAlexander V. Chernikov 			}
12550bebb544SHiroki Sato 		}
1256c83dda36SAlexander V. Chernikov 	}
12570bebb544SHiroki Sato #endif
1258c83dda36SAlexander V. Chernikov 	send_rtm_reply(so, rtm, m, saf, fibnum, error);
1259c83dda36SAlexander V. Chernikov 
1260c83dda36SAlexander V. Chernikov 	return (error);
1261c83dda36SAlexander V. Chernikov }
1262c83dda36SAlexander V. Chernikov 
1263c83dda36SAlexander V. Chernikov /*
1264c83dda36SAlexander V. Chernikov  * Sends the prepared reply message in @rtm to all rtsock clients.
1265c83dda36SAlexander V. Chernikov  * Frees @m and @rtm.
1266c83dda36SAlexander V. Chernikov  *
1267c83dda36SAlexander V. Chernikov  */
1268c83dda36SAlexander V. Chernikov static void
send_rtm_reply(struct socket * so,struct rt_msghdr * rtm,struct mbuf * m,sa_family_t saf,u_int fibnum,int rtm_errno)1269c83dda36SAlexander V. Chernikov send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1270c83dda36SAlexander V. Chernikov     sa_family_t saf, u_int fibnum, int rtm_errno)
1271c83dda36SAlexander V. Chernikov {
127236b10ac2SGleb Smirnoff 	struct rcb *rcb = NULL;
1273c83dda36SAlexander V. Chernikov 
1274c83dda36SAlexander V. Chernikov 	/*
1275c83dda36SAlexander V. Chernikov 	 * Check to see if we don't want our own messages.
1276c83dda36SAlexander V. Chernikov 	 */
1277c83dda36SAlexander V. Chernikov 	if ((so->so_options & SO_USELOOPBACK) == 0) {
1278c83dda36SAlexander V. Chernikov 		if (V_route_cb.any_count <= 1) {
1279c83dda36SAlexander V. Chernikov 			if (rtm != NULL)
1280c83dda36SAlexander V. Chernikov 				free(rtm, M_TEMP);
1281c83dda36SAlexander V. Chernikov 			m_freem(m);
1282c83dda36SAlexander V. Chernikov 			return;
1283c83dda36SAlexander V. Chernikov 		}
1284c83dda36SAlexander V. Chernikov 		/* There is another listener, so construct message */
128536b10ac2SGleb Smirnoff 		rcb = so->so_pcb;
1286c83dda36SAlexander V. Chernikov 	}
1287c83dda36SAlexander V. Chernikov 
1288c83dda36SAlexander V. Chernikov 	if (rtm != NULL) {
1289c83dda36SAlexander V. Chernikov 		if (rtm_errno!= 0)
1290c83dda36SAlexander V. Chernikov 			rtm->rtm_errno = rtm_errno;
129192c227afSAlexander V. Chernikov 		else
129292c227afSAlexander V. Chernikov 			rtm->rtm_flags |= RTF_DONE;
129392c227afSAlexander V. Chernikov 
1294df8bae1dSRodney W. Grimes 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
129503311056SHajimu UMEMOTO 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
129603311056SHajimu UMEMOTO 			m_freem(m);
129703311056SHajimu UMEMOTO 			m = NULL;
129803311056SHajimu UMEMOTO 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
129903311056SHajimu UMEMOTO 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1300f2e5eb36SAlexander V. Chernikov 
1301f2e5eb36SAlexander V. Chernikov 		free(rtm, M_TEMP);
1302df8bae1dSRodney W. Grimes 	}
130392c227afSAlexander V. Chernikov 	if (m != NULL) {
1304773aa053SAlexander V. Chernikov 		M_SETFIB(m, fibnum);
1305528737fdSBjoern A. Zeeb 		m->m_flags |= RTS_FILTER_FIB;
130636b10ac2SGleb Smirnoff 		if (rcb) {
1307becc44d7SSam Leffler 			/*
1308becc44d7SSam Leffler 			 * XXX insure we don't get a copy by
1309becc44d7SSam Leffler 			 * invalidating our protocol
1310becc44d7SSam Leffler 			 */
131136b10ac2SGleb Smirnoff 			sa_family_t family = rcb->rcb_family;
131236b10ac2SGleb Smirnoff 			rcb->rcb_family = AF_UNSPEC;
13133ca1a2d6SMax Laier 			rt_dispatch(m, saf);
131436b10ac2SGleb Smirnoff 			rcb->rcb_family = family;
1315becc44d7SSam Leffler 		} else
13163ca1a2d6SMax Laier 			rt_dispatch(m, saf);
1317becc44d7SSam Leffler 	}
1318df8bae1dSRodney W. Grimes }
1319df8bae1dSRodney W. Grimes 
132052041295SPoul-Henning Kamp static void
rt_getmetrics(const struct rtentry * rt,const struct nhop_object * nh,struct rt_metrics * out)132193bfd365SAlexander V. Chernikov rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
132293bfd365SAlexander V. Chernikov     struct rt_metrics *out)
132397d8d152SAndre Oppermann {
1324e3a7aa6fSGleb Smirnoff 
132597d8d152SAndre Oppermann 	bzero(out, sizeof(*out));
132693bfd365SAlexander V. Chernikov 	out->rmx_mtu = nh->nh_mtu;
1327e3a7aa6fSGleb Smirnoff 	out->rmx_weight = rt->rt_weight;
132893bfd365SAlexander V. Chernikov 	out->rmx_nhidx = nhop_get_idx(nh);
1329e27c3f48SOleg Bulyzhin 	/* Kernel -> userland timebase conversion. */
13302717e958SAlexander V. Chernikov 	out->rmx_expire = nhop_get_expire(nh) ?
13312717e958SAlexander V. Chernikov 	    nhop_get_expire(nh) - time_uptime + time_second : 0;
133297d8d152SAndre Oppermann }
133397d8d152SAndre Oppermann 
13347f33a738SJulian Elischer /*
13357f33a738SJulian Elischer  * Extract the addresses of the passed sockaddrs.
13367f33a738SJulian Elischer  * Do a little sanity checking so as to avoid bad memory references.
1337076d0761SJulian Elischer  * This data is derived straight from userland.
13387f33a738SJulian Elischer  */
1339076d0761SJulian Elischer static int
rt_xaddrs(caddr_t cp,caddr_t cplim,struct rt_addrinfo * rtinfo)1340becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1341df8bae1dSRodney W. Grimes {
1342e74642dfSLuigi Rizzo 	struct sockaddr *sa;
1343e74642dfSLuigi Rizzo 	int i;
1344df8bae1dSRodney W. Grimes 
1345becc44d7SSam Leffler 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1346df8bae1dSRodney W. Grimes 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1347df8bae1dSRodney W. Grimes 			continue;
1348ff6d0a59SJulian Elischer 		sa = (struct sockaddr *)cp;
13497f33a738SJulian Elischer 		/*
1350076d0761SJulian Elischer 		 * It won't fit.
13517f33a738SJulian Elischer 		 */
13526fa8ed43SAlexander V. Chernikov 		if (cp + sa->sa_len > cplim) {
13536fa8ed43SAlexander V. Chernikov 			RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1354076d0761SJulian Elischer 			return (EINVAL);
13556fa8ed43SAlexander V. Chernikov 		}
13567f33a738SJulian Elischer 		/*
13577f33a738SJulian Elischer 		 * there are no more.. quit now
13587f33a738SJulian Elischer 		 * If there are more bits, they are in error.
13597f33a738SJulian Elischer 		 * I've seen this. route(1) can evidently generate these.
13607f33a738SJulian Elischer 		 * This causes kernel to core dump.
1361076d0761SJulian Elischer 		 * for compatibility, If we see this, point to a safe address.
13627f33a738SJulian Elischer 		 */
1363076d0761SJulian Elischer 		if (sa->sa_len == 0) {
1364076d0761SJulian Elischer 			rtinfo->rti_info[i] = &sa_zero;
1365076d0761SJulian Elischer 			return (0); /* should be EINVAL but for compat */
1366df8bae1dSRodney W. Grimes 		}
1367076d0761SJulian Elischer 		/* accept it */
13680bebb544SHiroki Sato #ifdef INET6
13690bebb544SHiroki Sato 		if (sa->sa_family == AF_INET6)
13700bebb544SHiroki Sato 			sa6_embedscope((struct sockaddr_in6 *)sa,
13710bebb544SHiroki Sato 			    V_ip6_use_defzone);
13720bebb544SHiroki Sato #endif
1373076d0761SJulian Elischer 		rtinfo->rti_info[i] = sa;
1374e74642dfSLuigi Rizzo 		cp += SA_SIZE(sa);
1375076d0761SJulian Elischer 	}
1376076d0761SJulian Elischer 	return (0);
1377df8bae1dSRodney W. Grimes }
1378df8bae1dSRodney W. Grimes 
1379a4513bacSAlexander V. Chernikov #ifdef INET
13802fe5a794SAlexander V. Chernikov static inline void
fill_sockaddr_inet(struct sockaddr_in * sin,struct in_addr addr)13812fe5a794SAlexander V. Chernikov fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
13822fe5a794SAlexander V. Chernikov {
13832fe5a794SAlexander V. Chernikov 
13842fe5a794SAlexander V. Chernikov 	const struct sockaddr_in nsin = {
13852fe5a794SAlexander V. Chernikov 		.sin_family = AF_INET,
13862fe5a794SAlexander V. Chernikov 		.sin_len = sizeof(struct sockaddr_in),
13872fe5a794SAlexander V. Chernikov 		.sin_addr = addr,
13882fe5a794SAlexander V. Chernikov 	};
13892fe5a794SAlexander V. Chernikov 	*sin = nsin;
13902fe5a794SAlexander V. Chernikov }
1391a4513bacSAlexander V. Chernikov #endif
13922fe5a794SAlexander V. Chernikov 
1393a4513bacSAlexander V. Chernikov #ifdef INET6
13942fe5a794SAlexander V. Chernikov static inline void
fill_sockaddr_inet6(struct sockaddr_in6 * sin6,const struct in6_addr * addr6,uint32_t scopeid)13952fe5a794SAlexander V. Chernikov fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
13962fe5a794SAlexander V. Chernikov     uint32_t scopeid)
13972fe5a794SAlexander V. Chernikov {
13982fe5a794SAlexander V. Chernikov 
13992fe5a794SAlexander V. Chernikov 	const struct sockaddr_in6 nsin6 = {
14002fe5a794SAlexander V. Chernikov 		.sin6_family = AF_INET6,
14012fe5a794SAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
14022fe5a794SAlexander V. Chernikov 		.sin6_addr = *addr6,
14032fe5a794SAlexander V. Chernikov 		.sin6_scope_id = scopeid,
14042fe5a794SAlexander V. Chernikov 	};
14052fe5a794SAlexander V. Chernikov 	*sin6 = nsin6;
14062fe5a794SAlexander V. Chernikov }
1407a4513bacSAlexander V. Chernikov #endif
14082fe5a794SAlexander V. Chernikov 
1409a3c2c06bSBjoern A. Zeeb #if defined(INET6) || defined(INET)
1410e5b394f2SAlexander V. Chernikov /*
1411e5b394f2SAlexander V. Chernikov  * Checks if gateway is suitable for lltable operations.
1412e5b394f2SAlexander V. Chernikov  * Lltable code requires AF_LINK gateway with ifindex
1413e5b394f2SAlexander V. Chernikov  *  and mac address specified.
1414e5b394f2SAlexander V. Chernikov  * Returns 0 on success.
1415e5b394f2SAlexander V. Chernikov  */
1416e5b394f2SAlexander V. Chernikov static int
cleanup_xaddrs_lladdr(struct rt_addrinfo * info)1417e5b394f2SAlexander V. Chernikov cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1418e5b394f2SAlexander V. Chernikov {
1419e5b394f2SAlexander V. Chernikov 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1420e5b394f2SAlexander V. Chernikov 
1421e5b394f2SAlexander V. Chernikov 	if (sdl->sdl_family != AF_LINK)
1422e5b394f2SAlexander V. Chernikov 		return (EINVAL);
1423e5b394f2SAlexander V. Chernikov 
14246fa8ed43SAlexander V. Chernikov 	if (sdl->sdl_index == 0) {
14256fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1426e5b394f2SAlexander V. Chernikov 		return (EINVAL);
14276fa8ed43SAlexander V. Chernikov 	}
1428e5b394f2SAlexander V. Chernikov 
14296fa8ed43SAlexander V. Chernikov 	if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
14306fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1431e5b394f2SAlexander V. Chernikov 		return (EINVAL);
14326fa8ed43SAlexander V. Chernikov 	}
1433e5b394f2SAlexander V. Chernikov 
1434e5b394f2SAlexander V. Chernikov 	return (0);
1435e5b394f2SAlexander V. Chernikov }
1436e5b394f2SAlexander V. Chernikov 
14372fe5a794SAlexander V. Chernikov static int
cleanup_xaddrs_gateway(struct rt_addrinfo * info,struct linear_buffer * lb)1438b31fbebeSAlexander V. Chernikov cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
14392fe5a794SAlexander V. Chernikov {
14402fe5a794SAlexander V. Chernikov 	struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1441b31fbebeSAlexander V. Chernikov 	struct sockaddr *sa;
14422fe5a794SAlexander V. Chernikov 
1443e5b394f2SAlexander V. Chernikov 	if (info->rti_flags & RTF_LLDATA)
1444e5b394f2SAlexander V. Chernikov 		return (cleanup_xaddrs_lladdr(info));
1445e5b394f2SAlexander V. Chernikov 
14462fe5a794SAlexander V. Chernikov 	switch (gw->sa_family) {
14472fe5a794SAlexander V. Chernikov #ifdef INET
14482fe5a794SAlexander V. Chernikov 	case AF_INET:
14492fe5a794SAlexander V. Chernikov 		{
14502fe5a794SAlexander V. Chernikov 			struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1451b31fbebeSAlexander V. Chernikov 
1452b31fbebeSAlexander V. Chernikov 			/* Ensure reads do not go beyoud SA boundary */
1453b31fbebeSAlexander V. Chernikov 			if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
14546fa8ed43SAlexander V. Chernikov 				RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
14556fa8ed43SAlexander V. Chernikov 				    gw->sa_len);
14562fe5a794SAlexander V. Chernikov 				return (EINVAL);
14572fe5a794SAlexander V. Chernikov 			}
1458b31fbebeSAlexander V. Chernikov 			sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1459b31fbebeSAlexander V. Chernikov 			if (sa == NULL)
1460b31fbebeSAlexander V. Chernikov 				return (ENOBUFS);
1461b31fbebeSAlexander V. Chernikov 			fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1462b31fbebeSAlexander V. Chernikov 			info->rti_info[RTAX_GATEWAY] = sa;
14632fe5a794SAlexander V. Chernikov 		}
14642fe5a794SAlexander V. Chernikov 		break;
14652fe5a794SAlexander V. Chernikov #endif
14662fe5a794SAlexander V. Chernikov #ifdef INET6
14672fe5a794SAlexander V. Chernikov 	case AF_INET6:
14682fe5a794SAlexander V. Chernikov 		{
14692fe5a794SAlexander V. Chernikov 			struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
14702fe5a794SAlexander V. Chernikov 			if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
14716fa8ed43SAlexander V. Chernikov 				RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
14726fa8ed43SAlexander V. Chernikov 				    gw->sa_len);
14732fe5a794SAlexander V. Chernikov 				return (EINVAL);
14742fe5a794SAlexander V. Chernikov 			}
14752fe5a794SAlexander V. Chernikov 			fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
14762fe5a794SAlexander V. Chernikov 			break;
14772fe5a794SAlexander V. Chernikov 		}
14782fe5a794SAlexander V. Chernikov #endif
14792fe5a794SAlexander V. Chernikov 	case AF_LINK:
14802fe5a794SAlexander V. Chernikov 		{
14817f5f3fccSAlexander V. Chernikov 			struct sockaddr_dl *gw_sdl;
14822fe5a794SAlexander V. Chernikov 
14837f5f3fccSAlexander V. Chernikov 			size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
14847f5f3fccSAlexander V. Chernikov 			gw_sdl = (struct sockaddr_dl *)gw;
14857f5f3fccSAlexander V. Chernikov 			if (gw_sdl->sdl_len < sdl_min_len) {
14866fa8ed43SAlexander V. Chernikov 				RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
14876fa8ed43SAlexander V. Chernikov 				    gw_sdl->sdl_len);
14882fe5a794SAlexander V. Chernikov 				return (EINVAL);
14892fe5a794SAlexander V. Chernikov 			}
1490b31fbebeSAlexander V. Chernikov 			sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1491b31fbebeSAlexander V. Chernikov 			if (sa == NULL)
1492b31fbebeSAlexander V. Chernikov 				return (ENOBUFS);
14932fe5a794SAlexander V. Chernikov 
14942fe5a794SAlexander V. Chernikov 			const struct sockaddr_dl_short sdl = {
14952fe5a794SAlexander V. Chernikov 				.sdl_family = AF_LINK,
1496b31fbebeSAlexander V. Chernikov 				.sdl_len = sizeof(struct sockaddr_dl_short),
14972fe5a794SAlexander V. Chernikov 				.sdl_index = gw_sdl->sdl_index,
14982fe5a794SAlexander V. Chernikov 			};
1499b31fbebeSAlexander V. Chernikov 			*((struct sockaddr_dl_short *)sa) = sdl;
1500b31fbebeSAlexander V. Chernikov 			info->rti_info[RTAX_GATEWAY] = sa;
15012fe5a794SAlexander V. Chernikov 			break;
15022fe5a794SAlexander V. Chernikov 		}
15032fe5a794SAlexander V. Chernikov 	}
15042fe5a794SAlexander V. Chernikov 
15052fe5a794SAlexander V. Chernikov 	return (0);
15062fe5a794SAlexander V. Chernikov }
1507a3c2c06bSBjoern A. Zeeb #endif
15082fe5a794SAlexander V. Chernikov 
1509f9e1cd6cSAlexander V. Chernikov static void
remove_netmask(struct rt_addrinfo * info)1510f9e1cd6cSAlexander V. Chernikov remove_netmask(struct rt_addrinfo *info)
1511f9e1cd6cSAlexander V. Chernikov {
1512f9e1cd6cSAlexander V. Chernikov 	info->rti_info[RTAX_NETMASK] = NULL;
1513f9e1cd6cSAlexander V. Chernikov 	info->rti_flags |= RTF_HOST;
1514f9e1cd6cSAlexander V. Chernikov 	info->rti_addrs &= ~RTA_NETMASK;
1515f9e1cd6cSAlexander V. Chernikov }
1516f9e1cd6cSAlexander V. Chernikov 
1517a4513bacSAlexander V. Chernikov #ifdef INET
15182fe5a794SAlexander V. Chernikov static int
cleanup_xaddrs_inet(struct rt_addrinfo * info,struct linear_buffer * lb)1519b31fbebeSAlexander V. Chernikov cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
15202fe5a794SAlexander V. Chernikov {
15212fe5a794SAlexander V. Chernikov 	struct sockaddr_in *dst_sa, *mask_sa;
1522b31fbebeSAlexander V. Chernikov 	const int sa_len = sizeof(struct sockaddr_in);
1523b31fbebeSAlexander V. Chernikov 	struct in_addr dst, mask;
15242fe5a794SAlexander V. Chernikov 
15252fe5a794SAlexander V. Chernikov 	/* Check & fixup dst/netmask combination first */
15262fe5a794SAlexander V. Chernikov 	dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
15272fe5a794SAlexander V. Chernikov 	mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
15282fe5a794SAlexander V. Chernikov 
1529b31fbebeSAlexander V. Chernikov 	/* Ensure reads do not go beyound the buffer size */
15306fa8ed43SAlexander V. Chernikov 	if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
15316fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
15326fa8ed43SAlexander V. Chernikov 		    dst_sa->sin_len);
15332fe5a794SAlexander V. Chernikov 		return (EINVAL);
15346fa8ed43SAlexander V. Chernikov 	}
1535b31fbebeSAlexander V. Chernikov 
1536b31fbebeSAlexander V. Chernikov 	if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1537b31fbebeSAlexander V. Chernikov 		/*
1538b31fbebeSAlexander V. Chernikov 		 * Some older routing software encode mask length into the
1539b31fbebeSAlexander V. Chernikov 		 * sin_len, thus resulting in "truncated" sockaddr.
1540b31fbebeSAlexander V. Chernikov 		 */
1541b31fbebeSAlexander V. Chernikov 		int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1542b31fbebeSAlexander V. Chernikov 		if (len >= 0) {
1543b31fbebeSAlexander V. Chernikov 			mask.s_addr = 0;
1544b31fbebeSAlexander V. Chernikov 			if (len > sizeof(struct in_addr))
1545b31fbebeSAlexander V. Chernikov 				len = sizeof(struct in_addr);
1546b31fbebeSAlexander V. Chernikov 			memcpy(&mask, &mask_sa->sin_addr, len);
1547b31fbebeSAlexander V. Chernikov 		} else {
15486fa8ed43SAlexander V. Chernikov 			RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
15496fa8ed43SAlexander V. Chernikov 			    mask_sa->sin_len);
15502fe5a794SAlexander V. Chernikov 			return (EINVAL);
15512fe5a794SAlexander V. Chernikov 		}
1552b31fbebeSAlexander V. Chernikov 	} else
1553b31fbebeSAlexander V. Chernikov 		mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
15542fe5a794SAlexander V. Chernikov 
1555b31fbebeSAlexander V. Chernikov 	dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1556b31fbebeSAlexander V. Chernikov 
1557b31fbebeSAlexander V. Chernikov 	/* Construct new "clean" dst/mask sockaddresses */
1558b31fbebeSAlexander V. Chernikov 	if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1559b31fbebeSAlexander V. Chernikov 		return (ENOBUFS);
1560b31fbebeSAlexander V. Chernikov 	fill_sockaddr_inet(dst_sa, dst);
1561b31fbebeSAlexander V. Chernikov 	info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1562b31fbebeSAlexander V. Chernikov 
1563b31fbebeSAlexander V. Chernikov 	if (mask.s_addr != INADDR_BROADCAST) {
1564b31fbebeSAlexander V. Chernikov 		if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1565b31fbebeSAlexander V. Chernikov 			return (ENOBUFS);
15662fe5a794SAlexander V. Chernikov 		fill_sockaddr_inet(mask_sa, mask);
1567b31fbebeSAlexander V. Chernikov 		info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
15685d1403a7SAlexander V. Chernikov 		info->rti_flags &= ~RTF_HOST;
1569b31fbebeSAlexander V. Chernikov 	} else
1570f9e1cd6cSAlexander V. Chernikov 		remove_netmask(info);
15712fe5a794SAlexander V. Chernikov 
15722fe5a794SAlexander V. Chernikov 	/* Check gateway */
15732fe5a794SAlexander V. Chernikov 	if (info->rti_info[RTAX_GATEWAY] != NULL)
1574b31fbebeSAlexander V. Chernikov 		return (cleanup_xaddrs_gateway(info, lb));
15752fe5a794SAlexander V. Chernikov 
15762fe5a794SAlexander V. Chernikov 	return (0);
15772fe5a794SAlexander V. Chernikov }
1578a4513bacSAlexander V. Chernikov #endif
15792fe5a794SAlexander V. Chernikov 
1580a4513bacSAlexander V. Chernikov #ifdef INET6
15812fe5a794SAlexander V. Chernikov static int
cleanup_xaddrs_inet6(struct rt_addrinfo * info,struct linear_buffer * lb)1582b31fbebeSAlexander V. Chernikov cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
15832fe5a794SAlexander V. Chernikov {
1584b31fbebeSAlexander V. Chernikov 	struct sockaddr *sa;
15852fe5a794SAlexander V. Chernikov 	struct sockaddr_in6 *dst_sa, *mask_sa;
1586b31fbebeSAlexander V. Chernikov 	struct in6_addr mask, *dst;
1587b31fbebeSAlexander V. Chernikov 	const int sa_len = sizeof(struct sockaddr_in6);
15882fe5a794SAlexander V. Chernikov 
15892fe5a794SAlexander V. Chernikov 	/* Check & fixup dst/netmask combination first */
15902fe5a794SAlexander V. Chernikov 	dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
15912fe5a794SAlexander V. Chernikov 	mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
15922fe5a794SAlexander V. Chernikov 
15932fe5a794SAlexander V. Chernikov 	if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
15946fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
15956fa8ed43SAlexander V. Chernikov 		    dst_sa->sin6_len);
15962fe5a794SAlexander V. Chernikov 		return (EINVAL);
15972fe5a794SAlexander V. Chernikov 	}
1598b31fbebeSAlexander V. Chernikov 
15992fe5a794SAlexander V. Chernikov 	if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1600b31fbebeSAlexander V. Chernikov 		/*
1601b31fbebeSAlexander V. Chernikov 		 * Some older routing software encode mask length into the
1602b31fbebeSAlexander V. Chernikov 		 * sin6_len, thus resulting in "truncated" sockaddr.
1603b31fbebeSAlexander V. Chernikov 		 */
1604b31fbebeSAlexander V. Chernikov 		int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1605b31fbebeSAlexander V. Chernikov 		if (len >= 0) {
1606b31fbebeSAlexander V. Chernikov 			bzero(&mask, sizeof(mask));
1607b31fbebeSAlexander V. Chernikov 			if (len > sizeof(struct in6_addr))
1608b31fbebeSAlexander V. Chernikov 				len = sizeof(struct in6_addr);
1609b31fbebeSAlexander V. Chernikov 			memcpy(&mask, &mask_sa->sin6_addr, len);
1610b31fbebeSAlexander V. Chernikov 		} else {
16116fa8ed43SAlexander V. Chernikov 			RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
16126fa8ed43SAlexander V. Chernikov 			    mask_sa->sin6_len);
16132fe5a794SAlexander V. Chernikov 			return (EINVAL);
16142fe5a794SAlexander V. Chernikov 		}
1615b31fbebeSAlexander V. Chernikov 	} else
1616b31fbebeSAlexander V. Chernikov 		mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
16172fe5a794SAlexander V. Chernikov 
1618b31fbebeSAlexander V. Chernikov 	dst = &dst_sa->sin6_addr;
1619b31fbebeSAlexander V. Chernikov 	IN6_MASK_ADDR(dst, &mask);
1620b31fbebeSAlexander V. Chernikov 
1621b31fbebeSAlexander V. Chernikov 	if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1622b31fbebeSAlexander V. Chernikov 		return (ENOBUFS);
1623b31fbebeSAlexander V. Chernikov 	fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1624b31fbebeSAlexander V. Chernikov 	info->rti_info[RTAX_DST] = sa;
1625b31fbebeSAlexander V. Chernikov 
1626b31fbebeSAlexander V. Chernikov 	if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1627b31fbebeSAlexander V. Chernikov 		if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1628b31fbebeSAlexander V. Chernikov 			return (ENOBUFS);
1629b31fbebeSAlexander V. Chernikov 		fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1630b31fbebeSAlexander V. Chernikov 		info->rti_info[RTAX_NETMASK] = sa;
16315d1403a7SAlexander V. Chernikov 		info->rti_flags &= ~RTF_HOST;
1632b31fbebeSAlexander V. Chernikov 	} else
1633f9e1cd6cSAlexander V. Chernikov 		remove_netmask(info);
16342fe5a794SAlexander V. Chernikov 
16352fe5a794SAlexander V. Chernikov 	/* Check gateway */
16362fe5a794SAlexander V. Chernikov 	if (info->rti_info[RTAX_GATEWAY] != NULL)
1637b31fbebeSAlexander V. Chernikov 		return (cleanup_xaddrs_gateway(info, lb));
16382fe5a794SAlexander V. Chernikov 
16392fe5a794SAlexander V. Chernikov 	return (0);
16402fe5a794SAlexander V. Chernikov }
1641a4513bacSAlexander V. Chernikov #endif
16422fe5a794SAlexander V. Chernikov 
16432fe5a794SAlexander V. Chernikov static int
cleanup_xaddrs(struct rt_addrinfo * info,struct linear_buffer * lb)1644b31fbebeSAlexander V. Chernikov cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
16452fe5a794SAlexander V. Chernikov {
16462fe5a794SAlexander V. Chernikov 	int error = EAFNOSUPPORT;
16472fe5a794SAlexander V. Chernikov 
16486fa8ed43SAlexander V. Chernikov 	if (info->rti_info[RTAX_DST] == NULL) {
16496fa8ed43SAlexander V. Chernikov 		RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
16502fe5a794SAlexander V. Chernikov 		return (EINVAL);
16516fa8ed43SAlexander V. Chernikov 	}
16522fe5a794SAlexander V. Chernikov 
1653f9e1cd6cSAlexander V. Chernikov 	if (info->rti_flags & RTF_LLDATA) {
1654f9e1cd6cSAlexander V. Chernikov 		/*
1655f9e1cd6cSAlexander V. Chernikov 		 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1656f9e1cd6cSAlexander V. Chernikov 		 * prefix along with the actual address in RTA_DST.
1657f9e1cd6cSAlexander V. Chernikov 		 * Remove netmask to avoid unnecessary address masking.
1658f9e1cd6cSAlexander V. Chernikov 		 */
1659f9e1cd6cSAlexander V. Chernikov 		remove_netmask(info);
1660f9e1cd6cSAlexander V. Chernikov 	}
1661f9e1cd6cSAlexander V. Chernikov 
16622fe5a794SAlexander V. Chernikov 	switch (info->rti_info[RTAX_DST]->sa_family) {
16632fe5a794SAlexander V. Chernikov #ifdef INET
16642fe5a794SAlexander V. Chernikov 	case AF_INET:
1665b31fbebeSAlexander V. Chernikov 		error = cleanup_xaddrs_inet(info, lb);
16662fe5a794SAlexander V. Chernikov 		break;
16672fe5a794SAlexander V. Chernikov #endif
16682fe5a794SAlexander V. Chernikov #ifdef INET6
16692fe5a794SAlexander V. Chernikov 	case AF_INET6:
1670b31fbebeSAlexander V. Chernikov 		error = cleanup_xaddrs_inet6(info, lb);
16712fe5a794SAlexander V. Chernikov 		break;
16722fe5a794SAlexander V. Chernikov #endif
16732fe5a794SAlexander V. Chernikov 	}
16742fe5a794SAlexander V. Chernikov 
16752fe5a794SAlexander V. Chernikov 	return (error);
16762fe5a794SAlexander V. Chernikov }
16772fe5a794SAlexander V. Chernikov 
16786d076ae8SBjoern A. Zeeb /*
16793deb3649SAlexander V. Chernikov  * Fill in @dmask with valid netmask leaving original @smask
16803deb3649SAlexander V. Chernikov  * intact. Mostly used with radix netmasks.
16813deb3649SAlexander V. Chernikov  */
1682fc88ecd3SAlexander V. Chernikov struct sockaddr *
rtsock_fix_netmask(const struct sockaddr * dst,const struct sockaddr * smask,struct sockaddr_storage * dmask)1683fc88ecd3SAlexander V. Chernikov rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
16843deb3649SAlexander V. Chernikov     struct sockaddr_storage *dmask)
16853deb3649SAlexander V. Chernikov {
16863deb3649SAlexander V. Chernikov 	if (dst == NULL || smask == NULL)
16873deb3649SAlexander V. Chernikov 		return (NULL);
16883deb3649SAlexander V. Chernikov 
16893deb3649SAlexander V. Chernikov 	memset(dmask, 0, dst->sa_len);
16903deb3649SAlexander V. Chernikov 	memcpy(dmask, smask, smask->sa_len);
16913deb3649SAlexander V. Chernikov 	dmask->ss_len = dst->sa_len;
16923deb3649SAlexander V. Chernikov 	dmask->ss_family = dst->sa_family;
16933deb3649SAlexander V. Chernikov 
16943deb3649SAlexander V. Chernikov 	return ((struct sockaddr *)dmask);
16953deb3649SAlexander V. Chernikov }
16963deb3649SAlexander V. Chernikov 
16973deb3649SAlexander V. Chernikov /*
16986db47af4SAlexander V. Chernikov  * Writes information related to @rtinfo object to newly-allocated mbuf.
16996db47af4SAlexander V. Chernikov  * Assumes MCLBYTES is enough to construct any message.
17006db47af4SAlexander V. Chernikov  * Used for OS notifications of vaious events (if/ifa announces,etc)
17016db47af4SAlexander V. Chernikov  *
17026db47af4SAlexander V. Chernikov  * Returns allocated mbuf or NULL on failure.
17036d076ae8SBjoern A. Zeeb  */
1704df8bae1dSRodney W. Grimes static struct mbuf *
rtsock_msg_mbuf(int type,struct rt_addrinfo * rtinfo)17056db47af4SAlexander V. Chernikov rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1706df8bae1dSRodney W. Grimes {
170792be2847SMark Johnston 	struct sockaddr_storage ss;
17085dfc91d7SLuigi Rizzo 	struct rt_msghdr *rtm;
17095dfc91d7SLuigi Rizzo 	struct mbuf *m;
17105dfc91d7SLuigi Rizzo 	int i;
17115dfc91d7SLuigi Rizzo 	struct sockaddr *sa;
17126bbfef90SHiroki Sato #ifdef INET6
17136bbfef90SHiroki Sato 	struct sockaddr_in6 *sin6;
17146bbfef90SHiroki Sato #endif
1715df8bae1dSRodney W. Grimes 	int len, dlen;
1716df8bae1dSRodney W. Grimes 
1717df8bae1dSRodney W. Grimes 	switch (type) {
1718df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
1719df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
1720df8bae1dSRodney W. Grimes 		len = sizeof(struct ifa_msghdr);
1721df8bae1dSRodney W. Grimes 		break;
1722df8bae1dSRodney W. Grimes 
1723477180fbSGarrett Wollman 	case RTM_DELMADDR:
1724477180fbSGarrett Wollman 	case RTM_NEWMADDR:
1725477180fbSGarrett Wollman 		len = sizeof(struct ifma_msghdr);
1726477180fbSGarrett Wollman 		break;
1727477180fbSGarrett Wollman 
1728df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
1729df8bae1dSRodney W. Grimes 		len = sizeof(struct if_msghdr);
1730df8bae1dSRodney W. Grimes 		break;
1731df8bae1dSRodney W. Grimes 
17327b6edd04SRuslan Ermilov 	case RTM_IFANNOUNCE:
1733b83a279fSSam Leffler 	case RTM_IEEE80211:
17347b6edd04SRuslan Ermilov 		len = sizeof(struct if_announcemsghdr);
17357b6edd04SRuslan Ermilov 		break;
17367b6edd04SRuslan Ermilov 
1737df8bae1dSRodney W. Grimes 	default:
1738df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
1739df8bae1dSRodney W. Grimes 	}
1740c69f77c3SGleb Smirnoff 
1741c69f77c3SGleb Smirnoff 	/* XXXGL: can we use MJUMPAGESIZE cluster here? */
1742c69f77c3SGleb Smirnoff 	KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1743c69f77c3SGleb Smirnoff 	if (len > MHLEN)
1744c69f77c3SGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1745c69f77c3SGleb Smirnoff 	else
1746eb1b1807SGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
17475dfc91d7SLuigi Rizzo 	if (m == NULL)
174833841545SHajimu UMEMOTO 		return (m);
1749c69f77c3SGleb Smirnoff 
1750df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len = len;
1751df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
1752df8bae1dSRodney W. Grimes 	bzero((caddr_t)rtm, len);
1753df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
1754df8bae1dSRodney W. Grimes 		if ((sa = rtinfo->rti_info[i]) == NULL)
1755df8bae1dSRodney W. Grimes 			continue;
1756df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
175792be2847SMark Johnston 
1758e74642dfSLuigi Rizzo 		dlen = SA_SIZE(sa);
175992be2847SMark Johnston 		KASSERT(dlen <= sizeof(ss),
176092be2847SMark Johnston 		    ("%s: sockaddr size overflow", __func__));
176192be2847SMark Johnston 		bzero(&ss, sizeof(ss));
176292be2847SMark Johnston 		bcopy(sa, &ss, sa->sa_len);
176392be2847SMark Johnston 		sa = (struct sockaddr *)&ss;
17646bbfef90SHiroki Sato #ifdef INET6
1765bec053ffSAlexander V. Chernikov 		if (sa->sa_family == AF_INET6) {
176692be2847SMark Johnston 			sin6 = (struct sockaddr_in6 *)sa;
176792be2847SMark Johnston 			(void)sa6_recoverscope(sin6);
17686bbfef90SHiroki Sato 		}
17696bbfef90SHiroki Sato #endif
1770df8bae1dSRodney W. Grimes 		m_copyback(m, len, dlen, (caddr_t)sa);
1771df8bae1dSRodney W. Grimes 		len += dlen;
1772df8bae1dSRodney W. Grimes 	}
1773df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len != len) {
1774df8bae1dSRodney W. Grimes 		m_freem(m);
1775df8bae1dSRodney W. Grimes 		return (NULL);
1776df8bae1dSRodney W. Grimes 	}
1777df8bae1dSRodney W. Grimes 	rtm->rtm_msglen = len;
1778df8bae1dSRodney W. Grimes 	rtm->rtm_version = RTM_VERSION;
1779df8bae1dSRodney W. Grimes 	rtm->rtm_type = type;
1780df8bae1dSRodney W. Grimes 	return (m);
1781df8bae1dSRodney W. Grimes }
1782df8bae1dSRodney W. Grimes 
17836d076ae8SBjoern A. Zeeb /*
1784f2e5eb36SAlexander V. Chernikov  * Writes information related to @rtinfo object to preallocated buffer.
1785f2e5eb36SAlexander V. Chernikov  * Stores needed size in @plen. If @w is NULL, calculates size without
1786f2e5eb36SAlexander V. Chernikov  * writing.
1787f2e5eb36SAlexander V. Chernikov  * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1788f2e5eb36SAlexander V. Chernikov  *
1789f2e5eb36SAlexander V. Chernikov  * Returns 0 on success.
1790f2e5eb36SAlexander V. Chernikov  *
17916d076ae8SBjoern A. Zeeb  */
1792df8bae1dSRodney W. Grimes static int
rtsock_msg_buffer(int type,struct rt_addrinfo * rtinfo,struct walkarg * w,int * plen)1793f2e5eb36SAlexander V. Chernikov rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1794df8bae1dSRodney W. Grimes {
179592be2847SMark Johnston 	struct sockaddr_storage ss;
179692be2847SMark Johnston 	int len, buflen = 0, dlen, i;
1797de46b2c6SAlexander V. Chernikov 	caddr_t cp = NULL;
1798f2e5eb36SAlexander V. Chernikov 	struct rt_msghdr *rtm = NULL;
17996bbfef90SHiroki Sato #ifdef INET6
18006bbfef90SHiroki Sato 	struct sockaddr_in6 *sin6;
18016bbfef90SHiroki Sato #endif
1802279e33d4SKonstantin Belousov #ifdef COMPAT_FREEBSD32
1803dae64402SGleb Smirnoff 	bool compat32;
1804dae64402SGleb Smirnoff 
1805dae64402SGleb Smirnoff 	compat32 = w != NULL && w->w_req != NULL &&
1806dae64402SGleb Smirnoff 	    (w->w_req->flags & SCTL_MASK32);
1807279e33d4SKonstantin Belousov #endif
1808df8bae1dSRodney W. Grimes 
1809df8bae1dSRodney W. Grimes 	switch (type) {
1810df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
1811df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
18126d076ae8SBjoern A. Zeeb 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
18136d076ae8SBjoern A. Zeeb #ifdef COMPAT_FREEBSD32
1814dae64402SGleb Smirnoff 			if (compat32)
18156d076ae8SBjoern A. Zeeb 				len = sizeof(struct ifa_msghdrl32);
1816dae64402SGleb Smirnoff 			else
18176d076ae8SBjoern A. Zeeb #endif
18186d076ae8SBjoern A. Zeeb 				len = sizeof(struct ifa_msghdrl);
18196d076ae8SBjoern A. Zeeb 		} else
1820df8bae1dSRodney W. Grimes 			len = sizeof(struct ifa_msghdr);
1821df8bae1dSRodney W. Grimes 		break;
1822df8bae1dSRodney W. Grimes 
1823df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
1824dae64402SGleb Smirnoff 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1825427a928aSKonstantin Belousov #ifdef COMPAT_FREEBSD32
1826dae64402SGleb Smirnoff 			if (compat32)
18276d076ae8SBjoern A. Zeeb 				len = sizeof(struct if_msghdrl32);
18286d076ae8SBjoern A. Zeeb 			else
1829427a928aSKonstantin Belousov #endif
18306d076ae8SBjoern A. Zeeb 				len = sizeof(struct if_msghdrl);
1831dae64402SGleb Smirnoff 		} else {
1832dae64402SGleb Smirnoff #ifdef COMPAT_FREEBSD32
1833dae64402SGleb Smirnoff 			if (compat32)
1834dae64402SGleb Smirnoff 				len = sizeof(struct if_msghdr32);
18356d076ae8SBjoern A. Zeeb 			else
1836dae64402SGleb Smirnoff #endif
1837df8bae1dSRodney W. Grimes 				len = sizeof(struct if_msghdr);
1838dae64402SGleb Smirnoff 		}
1839df8bae1dSRodney W. Grimes 		break;
1840df8bae1dSRodney W. Grimes 
184105b2efe0SBruce M Simpson 	case RTM_NEWMADDR:
184205b2efe0SBruce M Simpson 		len = sizeof(struct ifma_msghdr);
184305b2efe0SBruce M Simpson 		break;
184405b2efe0SBruce M Simpson 
1845df8bae1dSRodney W. Grimes 	default:
1846df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
1847df8bae1dSRodney W. Grimes 	}
1848f2e5eb36SAlexander V. Chernikov 
1849f2e5eb36SAlexander V. Chernikov 	if (w != NULL) {
1850f2e5eb36SAlexander V. Chernikov 		rtm = (struct rt_msghdr *)w->w_tmem;
1851f2e5eb36SAlexander V. Chernikov 		buflen = w->w_tmemsize - len;
1852f2e5eb36SAlexander V. Chernikov 		cp = (caddr_t)w->w_tmem + len;
1853f2e5eb36SAlexander V. Chernikov 	}
1854f2e5eb36SAlexander V. Chernikov 
1855f2e5eb36SAlexander V. Chernikov 	rtinfo->rti_addrs = 0;
1856df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
18575dfc91d7SLuigi Rizzo 		struct sockaddr *sa;
1858df8bae1dSRodney W. Grimes 
18595dfc91d7SLuigi Rizzo 		if ((sa = rtinfo->rti_info[i]) == NULL)
1860df8bae1dSRodney W. Grimes 			continue;
1861df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
1862279e33d4SKonstantin Belousov #ifdef COMPAT_FREEBSD32
1863279e33d4SKonstantin Belousov 		if (compat32)
1864279e33d4SKonstantin Belousov 			dlen = SA_SIZE32(sa);
1865279e33d4SKonstantin Belousov 		else
1866279e33d4SKonstantin Belousov #endif
1867e74642dfSLuigi Rizzo 			dlen = SA_SIZE(sa);
1868f2e5eb36SAlexander V. Chernikov 		if (cp != NULL && buflen >= dlen) {
186992be2847SMark Johnston 			KASSERT(dlen <= sizeof(ss),
187092be2847SMark Johnston 			    ("%s: sockaddr size overflow", __func__));
187192be2847SMark Johnston 			bzero(&ss, sizeof(ss));
187292be2847SMark Johnston 			bcopy(sa, &ss, sa->sa_len);
187392be2847SMark Johnston 			sa = (struct sockaddr *)&ss;
18746bbfef90SHiroki Sato #ifdef INET6
1875bec053ffSAlexander V. Chernikov 			if (sa->sa_family == AF_INET6) {
187692be2847SMark Johnston 				sin6 = (struct sockaddr_in6 *)sa;
187792be2847SMark Johnston 				(void)sa6_recoverscope(sin6);
18786bbfef90SHiroki Sato 			}
18796bbfef90SHiroki Sato #endif
1880df8bae1dSRodney W. Grimes 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
1881df8bae1dSRodney W. Grimes 			cp += dlen;
1882f2e5eb36SAlexander V. Chernikov 			buflen -= dlen;
1883f2e5eb36SAlexander V. Chernikov 		} else if (cp != NULL) {
1884f2e5eb36SAlexander V. Chernikov 			/*
1885f2e5eb36SAlexander V. Chernikov 			 * Buffer too small. Count needed size
1886f2e5eb36SAlexander V. Chernikov 			 * and return with error.
1887f2e5eb36SAlexander V. Chernikov 			 */
1888f2e5eb36SAlexander V. Chernikov 			cp = NULL;
1889df8bae1dSRodney W. Grimes 		}
1890f2e5eb36SAlexander V. Chernikov 
1891df8bae1dSRodney W. Grimes 		len += dlen;
1892df8bae1dSRodney W. Grimes 	}
1893f2e5eb36SAlexander V. Chernikov 
1894f2e5eb36SAlexander V. Chernikov 	if (cp != NULL) {
1895f2e5eb36SAlexander V. Chernikov 		dlen = ALIGN(len) - len;
1896f2e5eb36SAlexander V. Chernikov 		if (buflen < dlen)
1897f2e5eb36SAlexander V. Chernikov 			cp = NULL;
1898d25f8522SMark Johnston 		else {
1899d25f8522SMark Johnston 			bzero(cp, dlen);
1900d25f8522SMark Johnston 			cp += dlen;
1901f2e5eb36SAlexander V. Chernikov 			buflen -= dlen;
1902f2e5eb36SAlexander V. Chernikov 		}
1903d25f8522SMark Johnston 	}
1904694ff264SAndrew Gallatin 	len = ALIGN(len);
1905df8bae1dSRodney W. Grimes 
1906f2e5eb36SAlexander V. Chernikov 	if (cp != NULL) {
1907f2e5eb36SAlexander V. Chernikov 		/* fill header iff buffer is large enough */
1908df8bae1dSRodney W. Grimes 		rtm->rtm_version = RTM_VERSION;
1909df8bae1dSRodney W. Grimes 		rtm->rtm_type = type;
1910df8bae1dSRodney W. Grimes 		rtm->rtm_msglen = len;
1911df8bae1dSRodney W. Grimes 	}
1912f2e5eb36SAlexander V. Chernikov 
1913f2e5eb36SAlexander V. Chernikov 	*plen = len;
1914f2e5eb36SAlexander V. Chernikov 
1915f2e5eb36SAlexander V. Chernikov 	if (w != NULL && cp == NULL)
1916f2e5eb36SAlexander V. Chernikov 		return (ENOBUFS);
1917f2e5eb36SAlexander V. Chernikov 
1918f2e5eb36SAlexander V. Chernikov 	return (0);
1919df8bae1dSRodney W. Grimes }
1920df8bae1dSRodney W. Grimes 
1921df8bae1dSRodney W. Grimes /*
1922df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
1923a4641f4eSPedro F. Giffuni  * socket indicating that a redirect has occurred, a routing lookup
1924df8bae1dSRodney W. Grimes  * has failed, or that a protocol has detected timeouts to a particular
1925df8bae1dSRodney W. Grimes  * destination.
1926df8bae1dSRodney W. Grimes  */
1927df8bae1dSRodney W. Grimes void
rt_missmsg_fib(int type,struct rt_addrinfo * rtinfo,int flags,int error,int fibnum)1928528737fdSBjoern A. Zeeb rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1929528737fdSBjoern A. Zeeb     int fibnum)
1930df8bae1dSRodney W. Grimes {
1931becc44d7SSam Leffler 	struct rt_msghdr *rtm;
1932becc44d7SSam Leffler 	struct mbuf *m;
1933df8bae1dSRodney W. Grimes 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1934df8bae1dSRodney W. Grimes 
1935c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
1936df8bae1dSRodney W. Grimes 		return;
19376db47af4SAlexander V. Chernikov 	m = rtsock_msg_mbuf(type, rtinfo);
19385dfc91d7SLuigi Rizzo 	if (m == NULL)
1939df8bae1dSRodney W. Grimes 		return;
1940528737fdSBjoern A. Zeeb 
19417d9b6df1SAlexander V. Chernikov 	if (fibnum != RT_ALL_FIBS) {
1942528737fdSBjoern A. Zeeb 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1943528737fdSBjoern A. Zeeb 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1944528737fdSBjoern A. Zeeb 		M_SETFIB(m, fibnum);
1945528737fdSBjoern A. Zeeb 		m->m_flags |= RTS_FILTER_FIB;
1946528737fdSBjoern A. Zeeb 	}
1947528737fdSBjoern A. Zeeb 
1948df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
1949df8bae1dSRodney W. Grimes 	rtm->rtm_flags = RTF_DONE | flags;
1950df8bae1dSRodney W. Grimes 	rtm->rtm_errno = error;
1951df8bae1dSRodney W. Grimes 	rtm->rtm_addrs = rtinfo->rti_addrs;
19523ca1a2d6SMax Laier 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1953df8bae1dSRodney W. Grimes }
1954df8bae1dSRodney W. Grimes 
1955528737fdSBjoern A. Zeeb void
rt_missmsg(int type,struct rt_addrinfo * rtinfo,int flags,int error)1956528737fdSBjoern A. Zeeb rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1957528737fdSBjoern A. Zeeb {
1958528737fdSBjoern A. Zeeb 
19597d9b6df1SAlexander V. Chernikov 	rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1960528737fdSBjoern A. Zeeb }
1961528737fdSBjoern A. Zeeb 
1962df8bae1dSRodney W. Grimes /*
1963df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
1964df8bae1dSRodney W. Grimes  * socket indicating that the status of a network interface has changed.
1965df8bae1dSRodney W. Grimes  */
19661bcd230fSAlexander V. Chernikov static void
rtsock_ifmsg(struct ifnet * ifp,int if_flags_mask __unused)19671bcd230fSAlexander V. Chernikov rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1968df8bae1dSRodney W. Grimes {
1969becc44d7SSam Leffler 	struct if_msghdr *ifm;
1970df8bae1dSRodney W. Grimes 	struct mbuf *m;
1971df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
1972df8bae1dSRodney W. Grimes 
1973c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
1974df8bae1dSRodney W. Grimes 		return;
1975df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
19766db47af4SAlexander V. Chernikov 	m = rtsock_msg_mbuf(RTM_IFINFO, &info);
19775dfc91d7SLuigi Rizzo 	if (m == NULL)
1978df8bae1dSRodney W. Grimes 		return;
1979df8bae1dSRodney W. Grimes 	ifm = mtod(m, struct if_msghdr *);
1980df8bae1dSRodney W. Grimes 	ifm->ifm_index = ifp->if_index;
1981292ee7beSRobert Watson 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1982e6485f73SGleb Smirnoff 	if_data_copy(ifp, &ifm->ifm_data);
1983df8bae1dSRodney W. Grimes 	ifm->ifm_addrs = 0;
19843ca1a2d6SMax Laier 	rt_dispatch(m, AF_UNSPEC);
1985df8bae1dSRodney W. Grimes }
1986df8bae1dSRodney W. Grimes 
1987df8bae1dSRodney W. Grimes /*
19884cbac30bSAlexander V. Chernikov  * Announce interface address arrival/withdraw.
19894cbac30bSAlexander V. Chernikov  * Please do not call directly, use rt_addrmsg().
19904cbac30bSAlexander V. Chernikov  * Assume input data to be valid.
19914cbac30bSAlexander V. Chernikov  * Returns 0 on success.
1992df8bae1dSRodney W. Grimes  */
19934cbac30bSAlexander V. Chernikov int
rtsock_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)19944cbac30bSAlexander V. Chernikov rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1995df8bae1dSRodney W. Grimes {
1996df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
19974cbac30bSAlexander V. Chernikov 	struct sockaddr *sa;
19984cbac30bSAlexander V. Chernikov 	int ncmd;
19994cbac30bSAlexander V. Chernikov 	struct mbuf *m;
20004cbac30bSAlexander V. Chernikov 	struct ifa_msghdr *ifam;
2001df8bae1dSRodney W. Grimes 	struct ifnet *ifp = ifa->ifa_ifp;
20023deb3649SAlexander V. Chernikov 	struct sockaddr_storage ss;
2003df8bae1dSRodney W. Grimes 
2004c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
20054cbac30bSAlexander V. Chernikov 		return (0);
2006df8bae1dSRodney W. Grimes 
20074cbac30bSAlexander V. Chernikov 	ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
20084cbac30bSAlexander V. Chernikov 
20094cbac30bSAlexander V. Chernikov 	bzero((caddr_t)&info, sizeof(info));
2010becc44d7SSam Leffler 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
20114a0d6638SRuslan Ermilov 	info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
20123deb3649SAlexander V. Chernikov 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2013e02d3fe7SAlexander V. Chernikov 	    info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
2014becc44d7SSam Leffler 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
20156db47af4SAlexander V. Chernikov 	if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
20164cbac30bSAlexander V. Chernikov 		return (ENOBUFS);
2017df8bae1dSRodney W. Grimes 	ifam = mtod(m, struct ifa_msghdr *);
2018df8bae1dSRodney W. Grimes 	ifam->ifam_index = ifp->if_index;
20199753faf5SGleb Smirnoff 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2020df8bae1dSRodney W. Grimes 	ifam->ifam_flags = ifa->ifa_flags;
2021df8bae1dSRodney W. Grimes 	ifam->ifam_addrs = info.rti_addrs;
20224cbac30bSAlexander V. Chernikov 
20234cbac30bSAlexander V. Chernikov 	if (fibnum != RT_ALL_FIBS) {
20244cbac30bSAlexander V. Chernikov 		M_SETFIB(m, fibnum);
20254cbac30bSAlexander V. Chernikov 		m->m_flags |= RTS_FILTER_FIB;
2026df8bae1dSRodney W. Grimes 	}
20274cbac30bSAlexander V. Chernikov 
20284cbac30bSAlexander V. Chernikov 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
20294cbac30bSAlexander V. Chernikov 
20304cbac30bSAlexander V. Chernikov 	return (0);
20314cbac30bSAlexander V. Chernikov }
20324cbac30bSAlexander V. Chernikov 
20334cbac30bSAlexander V. Chernikov /*
2034e02d3fe7SAlexander V. Chernikov  * Announce route addition/removal to rtsock based on @rt data.
2035e02d3fe7SAlexander V. Chernikov  * Callers are advives to use rt_routemsg() instead of using this
2036e02d3fe7SAlexander V. Chernikov  *  function directly.
2037e02d3fe7SAlexander V. Chernikov  * Assume @rt data is consistent.
20384cbac30bSAlexander V. Chernikov  *
20394cbac30bSAlexander V. Chernikov  * Returns 0 on success.
20404cbac30bSAlexander V. Chernikov  */
20414cbac30bSAlexander V. Chernikov int
rtsock_routemsg(int cmd,struct rtentry * rt,struct nhop_object * nh,int fibnum)2042d68cf57bSAlexander V. Chernikov rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
20434cbac30bSAlexander V. Chernikov     int fibnum)
20444cbac30bSAlexander V. Chernikov {
2045d68cf57bSAlexander V. Chernikov 	union sockaddr_union dst, mask;
2046e02d3fe7SAlexander V. Chernikov 	struct rt_addrinfo info;
2047df8bae1dSRodney W. Grimes 
2048c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
20494cbac30bSAlexander V. Chernikov 		return (0);
20504cbac30bSAlexander V. Chernikov 
2051d68cf57bSAlexander V. Chernikov 	int family = rt_get_family(rt);
2052d68cf57bSAlexander V. Chernikov 	init_sockaddrs_family(family, &dst.sa, &mask.sa);
2053d68cf57bSAlexander V. Chernikov 	export_rtaddrs(rt, &dst.sa, &mask.sa);
2054d68cf57bSAlexander V. Chernikov 
20554cbac30bSAlexander V. Chernikov 	bzero((caddr_t)&info, sizeof(info));
2056d68cf57bSAlexander V. Chernikov 	info.rti_info[RTAX_DST] = &dst.sa;
2057d68cf57bSAlexander V. Chernikov 	info.rti_info[RTAX_NETMASK] = &mask.sa;
205893bfd365SAlexander V. Chernikov 	info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
205993bfd365SAlexander V. Chernikov 	info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2060d68cf57bSAlexander V. Chernikov 	info.rti_ifp = nh->nh_ifp;
2061e02d3fe7SAlexander V. Chernikov 
2062e02d3fe7SAlexander V. Chernikov 	return (rtsock_routemsg_info(cmd, &info, fibnum));
2063e02d3fe7SAlexander V. Chernikov }
2064e02d3fe7SAlexander V. Chernikov 
2065e02d3fe7SAlexander V. Chernikov int
rtsock_routemsg_info(int cmd,struct rt_addrinfo * info,int fibnum)2066e02d3fe7SAlexander V. Chernikov rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2067e02d3fe7SAlexander V. Chernikov {
2068e02d3fe7SAlexander V. Chernikov 	struct rt_msghdr *rtm;
2069e02d3fe7SAlexander V. Chernikov 	struct sockaddr *sa;
2070e02d3fe7SAlexander V. Chernikov 	struct mbuf *m;
2071e02d3fe7SAlexander V. Chernikov 
2072e02d3fe7SAlexander V. Chernikov 	if (V_route_cb.any_count == 0)
2073e02d3fe7SAlexander V. Chernikov 		return (0);
2074e02d3fe7SAlexander V. Chernikov 
2075e02d3fe7SAlexander V. Chernikov 	if (info->rti_flags & RTF_HOST)
2076e02d3fe7SAlexander V. Chernikov 		info->rti_info[RTAX_NETMASK] = NULL;
2077e02d3fe7SAlexander V. Chernikov 
2078e02d3fe7SAlexander V. Chernikov 	m = rtsock_msg_mbuf(cmd, info);
2079e02d3fe7SAlexander V. Chernikov 	if (m == NULL)
20804cbac30bSAlexander V. Chernikov 		return (ENOBUFS);
20814cbac30bSAlexander V. Chernikov 
20827d9b6df1SAlexander V. Chernikov 	if (fibnum != RT_ALL_FIBS) {
2083e02d3fe7SAlexander V. Chernikov 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2084e02d3fe7SAlexander V. Chernikov 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2085528737fdSBjoern A. Zeeb 		M_SETFIB(m, fibnum);
2086528737fdSBjoern A. Zeeb 		m->m_flags |= RTS_FILTER_FIB;
2087528737fdSBjoern A. Zeeb 	}
20884cbac30bSAlexander V. Chernikov 
2089e02d3fe7SAlexander V. Chernikov 	rtm = mtod(m, struct rt_msghdr *);
2090e02d3fe7SAlexander V. Chernikov 	rtm->rtm_addrs = info->rti_addrs;
2091e02d3fe7SAlexander V. Chernikov 	if (info->rti_ifp != NULL)
2092e02d3fe7SAlexander V. Chernikov 		rtm->rtm_index = info->rti_ifp->if_index;
2093e02d3fe7SAlexander V. Chernikov 	/* Add RTF_DONE to indicate command 'completion' required by API */
2094e02d3fe7SAlexander V. Chernikov 	info->rti_flags |= RTF_DONE;
2095e02d3fe7SAlexander V. Chernikov 	/* Reported routes has to be up */
2096e02d3fe7SAlexander V. Chernikov 	if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2097e02d3fe7SAlexander V. Chernikov 		info->rti_flags |= RTF_UP;
2098e02d3fe7SAlexander V. Chernikov 	rtm->rtm_flags = info->rti_flags;
2099e02d3fe7SAlexander V. Chernikov 
2100e02d3fe7SAlexander V. Chernikov 	sa = info->rti_info[RTAX_DST];
21013ca1a2d6SMax Laier 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
21024cbac30bSAlexander V. Chernikov 
21034cbac30bSAlexander V. Chernikov 	return (0);
2104df8bae1dSRodney W. Grimes }
2105df8bae1dSRodney W. Grimes 
2106477180fbSGarrett Wollman /*
2107477180fbSGarrett Wollman  * This is the analogue to the rt_newaddrmsg which performs the same
2108477180fbSGarrett Wollman  * function but for multicast group memberhips.  This is easier since
2109477180fbSGarrett Wollman  * there is no route state to worry about.
2110477180fbSGarrett Wollman  */
2111477180fbSGarrett Wollman void
rt_newmaddrmsg(int cmd,struct ifmultiaddr * ifma)2112becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2113477180fbSGarrett Wollman {
2114477180fbSGarrett Wollman 	struct rt_addrinfo info;
21155dfc91d7SLuigi Rizzo 	struct mbuf *m = NULL;
2116477180fbSGarrett Wollman 	struct ifnet *ifp = ifma->ifma_ifp;
2117477180fbSGarrett Wollman 	struct ifma_msghdr *ifmam;
2118477180fbSGarrett Wollman 
2119c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
2120477180fbSGarrett Wollman 		return;
2121477180fbSGarrett Wollman 
2122477180fbSGarrett Wollman 	bzero((caddr_t)&info, sizeof(info));
2123becc44d7SSam Leffler 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
21247edd877aSMatt Macy 	if (ifp && ifp->if_addr)
21257edd877aSMatt Macy 		info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
21267edd877aSMatt Macy 	else
21277edd877aSMatt Macy 		info.rti_info[RTAX_IFP] = NULL;
2128477180fbSGarrett Wollman 	/*
2129477180fbSGarrett Wollman 	 * If a link-layer address is present, present it as a ``gateway''
2130477180fbSGarrett Wollman 	 * (similarly to how ARP entries, e.g., are presented).
2131477180fbSGarrett Wollman 	 */
2132becc44d7SSam Leffler 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
21336db47af4SAlexander V. Chernikov 	m = rtsock_msg_mbuf(cmd, &info);
2134becc44d7SSam Leffler 	if (m == NULL)
2135477180fbSGarrett Wollman 		return;
2136477180fbSGarrett Wollman 	ifmam = mtod(m, struct ifma_msghdr *);
213775ae0c01SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
213875ae0c01SBruce M Simpson 	    __func__));
2139477180fbSGarrett Wollman 	ifmam->ifmam_index = ifp->if_index;
2140477180fbSGarrett Wollman 	ifmam->ifmam_addrs = info.rti_addrs;
21413ca1a2d6SMax Laier 	rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2142477180fbSGarrett Wollman }
214352041295SPoul-Henning Kamp 
2144b83a279fSSam Leffler static struct mbuf *
rt_makeifannouncemsg(struct ifnet * ifp,int type,int what,struct rt_addrinfo * info)2145b83a279fSSam Leffler rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2146b83a279fSSam Leffler 	struct rt_addrinfo *info)
2147b83a279fSSam Leffler {
2148b83a279fSSam Leffler 	struct if_announcemsghdr *ifan;
2149b83a279fSSam Leffler 	struct mbuf *m;
2150b83a279fSSam Leffler 
2151c5d4eab6SMarko Zec 	if (V_route_cb.any_count == 0)
2152b83a279fSSam Leffler 		return NULL;
2153b83a279fSSam Leffler 	bzero((caddr_t)info, sizeof(*info));
21546db47af4SAlexander V. Chernikov 	m = rtsock_msg_mbuf(type, info);
2155b83a279fSSam Leffler 	if (m != NULL) {
2156b83a279fSSam Leffler 		ifan = mtod(m, struct if_announcemsghdr *);
2157b83a279fSSam Leffler 		ifan->ifan_index = ifp->if_index;
2158b83a279fSSam Leffler 		strlcpy(ifan->ifan_name, ifp->if_xname,
2159b83a279fSSam Leffler 			sizeof(ifan->ifan_name));
2160b83a279fSSam Leffler 		ifan->ifan_what = what;
2161b83a279fSSam Leffler 	}
2162b83a279fSSam Leffler 	return m;
2163b83a279fSSam Leffler }
2164b83a279fSSam Leffler 
2165b83a279fSSam Leffler /*
2166b83a279fSSam Leffler  * This is called to generate routing socket messages indicating
2167b83a279fSSam Leffler  * IEEE80211 wireless events.
2168b83a279fSSam Leffler  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2169b83a279fSSam Leffler  */
2170b83a279fSSam Leffler void
rt_ieee80211msg(struct ifnet * ifp,int what,void * data,size_t data_len)2171b83a279fSSam Leffler rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2172b83a279fSSam Leffler {
2173b83a279fSSam Leffler 	struct mbuf *m;
2174b83a279fSSam Leffler 	struct rt_addrinfo info;
2175b83a279fSSam Leffler 
2176b83a279fSSam Leffler 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2177b83a279fSSam Leffler 	if (m != NULL) {
2178b83a279fSSam Leffler 		/*
2179b83a279fSSam Leffler 		 * Append the ieee80211 data.  Try to stick it in the
2180b83a279fSSam Leffler 		 * mbuf containing the ifannounce msg; otherwise allocate
2181b83a279fSSam Leffler 		 * a new mbuf and append.
2182b83a279fSSam Leffler 		 *
2183b83a279fSSam Leffler 		 * NB: we assume m is a single mbuf.
2184b83a279fSSam Leffler 		 */
2185b83a279fSSam Leffler 		if (data_len > M_TRAILINGSPACE(m)) {
2186b83a279fSSam Leffler 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2187b83a279fSSam Leffler 			if (n == NULL) {
2188b83a279fSSam Leffler 				m_freem(m);
2189b83a279fSSam Leffler 				return;
2190b83a279fSSam Leffler 			}
2191b83a279fSSam Leffler 			bcopy(data, mtod(n, void *), data_len);
2192b83a279fSSam Leffler 			n->m_len = data_len;
2193b83a279fSSam Leffler 			m->m_next = n;
2194b83a279fSSam Leffler 		} else if (data_len > 0) {
2195b83a279fSSam Leffler 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2196b83a279fSSam Leffler 			m->m_len += data_len;
2197b83a279fSSam Leffler 		}
2198b83a279fSSam Leffler 		if (m->m_flags & M_PKTHDR)
2199b83a279fSSam Leffler 			m->m_pkthdr.len += data_len;
2200b83a279fSSam Leffler 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
22013ca1a2d6SMax Laier 		rt_dispatch(m, AF_UNSPEC);
2202b83a279fSSam Leffler 	}
2203b83a279fSSam Leffler }
2204b83a279fSSam Leffler 
2205df8bae1dSRodney W. Grimes /*
22067b6edd04SRuslan Ermilov  * This is called to generate routing socket messages indicating
22077b6edd04SRuslan Ermilov  * network interface arrival and departure.
22087b6edd04SRuslan Ermilov  */
2209d8b42ddcSAlexander V. Chernikov static void
rt_ifannouncemsg(struct ifnet * ifp,int what)2210becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what)
22117b6edd04SRuslan Ermilov {
22127b6edd04SRuslan Ermilov 	struct mbuf *m;
22137b6edd04SRuslan Ermilov 	struct rt_addrinfo info;
22147b6edd04SRuslan Ermilov 
2215b83a279fSSam Leffler 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2216b83a279fSSam Leffler 	if (m != NULL)
22173ca1a2d6SMax Laier 		rt_dispatch(m, AF_UNSPEC);
2218becc44d7SSam Leffler }
2219becc44d7SSam Leffler 
2220becc44d7SSam Leffler static void
rt_dispatch(struct mbuf * m,sa_family_t saf)22213ca1a2d6SMax Laier rt_dispatch(struct mbuf *m, sa_family_t saf)
2222becc44d7SSam Leffler {
2223becc44d7SSam Leffler 
2224d94ec749SGleb Smirnoff 	M_ASSERTPKTHDR(m);
2225d94ec749SGleb Smirnoff 
2226d94ec749SGleb Smirnoff 	m->m_rtsock_family = saf;
222721ca7b57SMarko Zec 	if (V_loif)
222821ca7b57SMarko Zec 		m->m_pkthdr.rcvif = V_loif;
222921ca7b57SMarko Zec 	else {
223021ca7b57SMarko Zec 		m_freem(m);
223121ca7b57SMarko Zec 		return;
223221ca7b57SMarko Zec 	}
22333161f583SAndre Oppermann 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
22347b6edd04SRuslan Ermilov }
22357b6edd04SRuslan Ermilov 
22367b6edd04SRuslan Ermilov /*
2237df8bae1dSRodney W. Grimes  * This is used in dumping the kernel table via sysctl().
2238df8bae1dSRodney W. Grimes  */
223937c84183SPoul-Henning Kamp static int
sysctl_dumpentry(struct rtentry * rt,void * vw)2240d68fb8d9SAlexander V. Chernikov sysctl_dumpentry(struct rtentry *rt, void *vw)
2241df8bae1dSRodney W. Grimes {
2242becc44d7SSam Leffler 	struct walkarg *w = vw;
22438c61eb21SAlexander V. Chernikov 	struct nhop_object *nh;
2244df8bae1dSRodney W. Grimes 
2245a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
2246a716ad4aSAndrey V. Elsukov 
22472cda6a2fSAlexander V. Chernikov 	if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2248813dd6aeSBjoern A. Zeeb 		return (0);
22492cda6a2fSAlexander V. Chernikov 
22502cda6a2fSAlexander V. Chernikov 	export_rtaddrs(rt, w->dst, w->mask);
2251d68fb8d9SAlexander V. Chernikov 	nh = rt_get_raw_nhop(rt);
2252fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
2253fedeb08bSAlexander V. Chernikov 	if (NH_IS_NHGRP(nh)) {
2254ae6bfd12SAlexander V. Chernikov 		const struct weightened_nhop *wn;
2255fedeb08bSAlexander V. Chernikov 		uint32_t num_nhops;
2256e606e5d1SWarner Losh 		int error;
2257fedeb08bSAlexander V. Chernikov 		wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2258fedeb08bSAlexander V. Chernikov 		for (int i = 0; i < num_nhops; i++) {
2259fedeb08bSAlexander V. Chernikov 			error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2260fedeb08bSAlexander V. Chernikov 			if (error != 0)
2261fedeb08bSAlexander V. Chernikov 				return (error);
2262fedeb08bSAlexander V. Chernikov 		}
2263fedeb08bSAlexander V. Chernikov 	} else
2264fedeb08bSAlexander V. Chernikov #endif
2265e606e5d1SWarner Losh 		sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
22662259a030SAlexander V. Chernikov 
22672259a030SAlexander V. Chernikov 	return (0);
22682259a030SAlexander V. Chernikov }
22692259a030SAlexander V. Chernikov 
22702259a030SAlexander V. Chernikov 
22712259a030SAlexander V. Chernikov static int
sysctl_dumpnhop(struct rtentry * rt,struct nhop_object * nh,uint32_t weight,struct walkarg * w)22722259a030SAlexander V. Chernikov sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
22732259a030SAlexander V. Chernikov     struct walkarg *w)
22742259a030SAlexander V. Chernikov {
22752259a030SAlexander V. Chernikov 	struct rt_addrinfo info;
22762259a030SAlexander V. Chernikov 	int error = 0, size;
2277d68fb8d9SAlexander V. Chernikov 	uint32_t rtflags;
2278d68fb8d9SAlexander V. Chernikov 
2279d68fb8d9SAlexander V. Chernikov 	rtflags = nhop_get_rtflags(nh);
2280d68fb8d9SAlexander V. Chernikov 
2281d68fb8d9SAlexander V. Chernikov 	if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2282d68fb8d9SAlexander V. Chernikov 		return (0);
22832259a030SAlexander V. Chernikov 
2284df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
2285d68fb8d9SAlexander V. Chernikov 	info.rti_info[RTAX_DST] = w->dst;
228693bfd365SAlexander V. Chernikov 	info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2287d68fb8d9SAlexander V. Chernikov 	info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
22886e6b3f7cSQing Li 	info.rti_info[RTAX_GENMASK] = 0;
22898c61eb21SAlexander V. Chernikov 	if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
22908c61eb21SAlexander V. Chernikov 		info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
22918c61eb21SAlexander V. Chernikov 		info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
22928c61eb21SAlexander V. Chernikov 		if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
22938c61eb21SAlexander V. Chernikov 			info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
229428070a0eSRuslan Ermilov 	}
2295f2e5eb36SAlexander V. Chernikov 	if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2296f2e5eb36SAlexander V. Chernikov 		return (error);
229752041295SPoul-Henning Kamp 	if (w->w_req && w->w_tmem) {
2298becc44d7SSam Leffler 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2299df8bae1dSRodney W. Grimes 
2300d25f8522SMark Johnston 		bzero(&rtm->rtm_index,
2301d25f8522SMark Johnston 		    sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2302d68fb8d9SAlexander V. Chernikov 
2303d68fb8d9SAlexander V. Chernikov 		/*
2304d68fb8d9SAlexander V. Chernikov 		 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2305d68fb8d9SAlexander V. Chernikov 		 * and RTF_UP (if entry is linked, which is always true here).
2306d68fb8d9SAlexander V. Chernikov 		 * Given that, use nhop rtflags & add RTF_UP.
2307d68fb8d9SAlexander V. Chernikov 		 */
2308d68fb8d9SAlexander V. Chernikov 		rtm->rtm_flags = rtflags | RTF_UP;
2309d68fb8d9SAlexander V. Chernikov 		if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2310f672f56fSQing Li 			rtm->rtm_flags = RTF_GATEWAY |
2311d68fb8d9SAlexander V. Chernikov 				(rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
231293bfd365SAlexander V. Chernikov 		rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2313fedeb08bSAlexander V. Chernikov 		rtm->rtm_rmx.rmx_weight = weight;
23148c61eb21SAlexander V. Chernikov 		rtm->rtm_index = nh->nh_ifp->if_index;
2315df8bae1dSRodney W. Grimes 		rtm->rtm_addrs = info.rti_addrs;
231652041295SPoul-Henning Kamp 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
231752041295SPoul-Henning Kamp 		return (error);
2318df8bae1dSRodney W. Grimes 	}
2319df8bae1dSRodney W. Grimes 	return (error);
2320df8bae1dSRodney W. Grimes }
2321df8bae1dSRodney W. Grimes 
232237c84183SPoul-Henning Kamp static int
sysctl_iflist_ifml(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)2323ab607f28SRyan Stone sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2324ab607f28SRyan Stone     struct rt_addrinfo *info, struct walkarg *w, int len)
23256d076ae8SBjoern A. Zeeb {
23266d076ae8SBjoern A. Zeeb 	struct if_msghdrl *ifm;
2327b245f96cSGleb Smirnoff 	struct if_data *ifd;
2328b245f96cSGleb Smirnoff 
2329b245f96cSGleb Smirnoff 	ifm = (struct if_msghdrl *)w->w_tmem;
23306d076ae8SBjoern A. Zeeb 
23316d076ae8SBjoern A. Zeeb #ifdef COMPAT_FREEBSD32
23326d076ae8SBjoern A. Zeeb 	if (w->w_req->flags & SCTL_MASK32) {
23336d076ae8SBjoern A. Zeeb 		struct if_msghdrl32 *ifm32;
23346d076ae8SBjoern A. Zeeb 
2335b245f96cSGleb Smirnoff 		ifm32 = (struct if_msghdrl32 *)ifm;
23366d076ae8SBjoern A. Zeeb 		ifm32->ifm_addrs = info->rti_addrs;
23376d076ae8SBjoern A. Zeeb 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
23386d076ae8SBjoern A. Zeeb 		ifm32->ifm_index = ifp->if_index;
23396d076ae8SBjoern A. Zeeb 		ifm32->_ifm_spare1 = 0;
23406d076ae8SBjoern A. Zeeb 		ifm32->ifm_len = sizeof(*ifm32);
23416d076ae8SBjoern A. Zeeb 		ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2342d25f8522SMark Johnston 		ifm32->_ifm_spare2 = 0;
2343b245f96cSGleb Smirnoff 		ifd = &ifm32->ifm_data;
2344b245f96cSGleb Smirnoff 	} else
23456d076ae8SBjoern A. Zeeb #endif
2346b245f96cSGleb Smirnoff 	{
23476d076ae8SBjoern A. Zeeb 		ifm->ifm_addrs = info->rti_addrs;
23486d076ae8SBjoern A. Zeeb 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
23496d076ae8SBjoern A. Zeeb 		ifm->ifm_index = ifp->if_index;
23506d076ae8SBjoern A. Zeeb 		ifm->_ifm_spare1 = 0;
23516d076ae8SBjoern A. Zeeb 		ifm->ifm_len = sizeof(*ifm);
23526d076ae8SBjoern A. Zeeb 		ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2353d25f8522SMark Johnston 		ifm->_ifm_spare2 = 0;
2354b245f96cSGleb Smirnoff 		ifd = &ifm->ifm_data;
2355b245f96cSGleb Smirnoff 	}
23566d076ae8SBjoern A. Zeeb 
2357ab607f28SRyan Stone 	memcpy(ifd, src_ifd, sizeof(*ifd));
2358b245f96cSGleb Smirnoff 
23596d076ae8SBjoern A. Zeeb 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
23606d076ae8SBjoern A. Zeeb }
23616d076ae8SBjoern A. Zeeb 
23626d076ae8SBjoern A. Zeeb static int
sysctl_iflist_ifm(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)2363ab607f28SRyan Stone sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2364ab607f28SRyan Stone     struct rt_addrinfo *info, struct walkarg *w, int len)
23656d076ae8SBjoern A. Zeeb {
23666d076ae8SBjoern A. Zeeb 	struct if_msghdr *ifm;
2367b245f96cSGleb Smirnoff 	struct if_data *ifd;
2368b245f96cSGleb Smirnoff 
2369b245f96cSGleb Smirnoff 	ifm = (struct if_msghdr *)w->w_tmem;
23706d076ae8SBjoern A. Zeeb 
23716d076ae8SBjoern A. Zeeb #ifdef COMPAT_FREEBSD32
23726d076ae8SBjoern A. Zeeb 	if (w->w_req->flags & SCTL_MASK32) {
23736d076ae8SBjoern A. Zeeb 		struct if_msghdr32 *ifm32;
23746d076ae8SBjoern A. Zeeb 
2375b245f96cSGleb Smirnoff 		ifm32 = (struct if_msghdr32 *)ifm;
23766d076ae8SBjoern A. Zeeb 		ifm32->ifm_addrs = info->rti_addrs;
23776d076ae8SBjoern A. Zeeb 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
23786d076ae8SBjoern A. Zeeb 		ifm32->ifm_index = ifp->if_index;
2379d25f8522SMark Johnston 		ifm32->_ifm_spare1 = 0;
2380b245f96cSGleb Smirnoff 		ifd = &ifm32->ifm_data;
2381b245f96cSGleb Smirnoff 	} else
23826d076ae8SBjoern A. Zeeb #endif
2383b245f96cSGleb Smirnoff 	{
23846d076ae8SBjoern A. Zeeb 		ifm->ifm_addrs = info->rti_addrs;
23856d076ae8SBjoern A. Zeeb 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
23866d076ae8SBjoern A. Zeeb 		ifm->ifm_index = ifp->if_index;
2387d25f8522SMark Johnston 		ifm->_ifm_spare1 = 0;
2388b245f96cSGleb Smirnoff 		ifd = &ifm->ifm_data;
2389b245f96cSGleb Smirnoff 	}
23906d076ae8SBjoern A. Zeeb 
2391ab607f28SRyan Stone 	memcpy(ifd, src_ifd, sizeof(*ifd));
23922d70c0deSGleb Smirnoff 
23936d076ae8SBjoern A. Zeeb 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
23946d076ae8SBjoern A. Zeeb }
23956d076ae8SBjoern A. Zeeb 
23966d076ae8SBjoern A. Zeeb static int
sysctl_iflist_ifaml(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)23976d076ae8SBjoern A. Zeeb sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
23986d076ae8SBjoern A. Zeeb     struct walkarg *w, int len)
23996d076ae8SBjoern A. Zeeb {
24006d076ae8SBjoern A. Zeeb 	struct ifa_msghdrl *ifam;
2401b245f96cSGleb Smirnoff 	struct if_data *ifd;
2402b245f96cSGleb Smirnoff 
2403b245f96cSGleb Smirnoff 	ifam = (struct ifa_msghdrl *)w->w_tmem;
24046d076ae8SBjoern A. Zeeb 
24056d076ae8SBjoern A. Zeeb #ifdef COMPAT_FREEBSD32
24066d076ae8SBjoern A. Zeeb 	if (w->w_req->flags & SCTL_MASK32) {
24076d076ae8SBjoern A. Zeeb 		struct ifa_msghdrl32 *ifam32;
24086d076ae8SBjoern A. Zeeb 
2409b245f96cSGleb Smirnoff 		ifam32 = (struct ifa_msghdrl32 *)ifam;
24106d076ae8SBjoern A. Zeeb 		ifam32->ifam_addrs = info->rti_addrs;
24116d076ae8SBjoern A. Zeeb 		ifam32->ifam_flags = ifa->ifa_flags;
24126d076ae8SBjoern A. Zeeb 		ifam32->ifam_index = ifa->ifa_ifp->if_index;
24136d076ae8SBjoern A. Zeeb 		ifam32->_ifam_spare1 = 0;
24146d076ae8SBjoern A. Zeeb 		ifam32->ifam_len = sizeof(*ifam32);
24156d076ae8SBjoern A. Zeeb 		ifam32->ifam_data_off =
24166d076ae8SBjoern A. Zeeb 		    offsetof(struct ifa_msghdrl32, ifam_data);
24179753faf5SGleb Smirnoff 		ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2418b245f96cSGleb Smirnoff 		ifd = &ifam32->ifam_data;
2419b245f96cSGleb Smirnoff 	} else
24206d076ae8SBjoern A. Zeeb #endif
2421b245f96cSGleb Smirnoff 	{
24226d076ae8SBjoern A. Zeeb 		ifam->ifam_addrs = info->rti_addrs;
24236d076ae8SBjoern A. Zeeb 		ifam->ifam_flags = ifa->ifa_flags;
24246d076ae8SBjoern A. Zeeb 		ifam->ifam_index = ifa->ifa_ifp->if_index;
24256d076ae8SBjoern A. Zeeb 		ifam->_ifam_spare1 = 0;
24266d076ae8SBjoern A. Zeeb 		ifam->ifam_len = sizeof(*ifam);
24276d076ae8SBjoern A. Zeeb 		ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
24289753faf5SGleb Smirnoff 		ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2429b245f96cSGleb Smirnoff 		ifd = &ifam->ifam_data;
2430b245f96cSGleb Smirnoff 	}
24316d076ae8SBjoern A. Zeeb 
2432b245f96cSGleb Smirnoff 	bzero(ifd, sizeof(*ifd));
2433b245f96cSGleb Smirnoff 	ifd->ifi_datalen = sizeof(struct if_data);
2434b245f96cSGleb Smirnoff 	ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2435b245f96cSGleb Smirnoff 	ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2436b245f96cSGleb Smirnoff 	ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2437b245f96cSGleb Smirnoff 	ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
24387caf4ab7SGleb Smirnoff 
24396d076ae8SBjoern A. Zeeb 	/* Fixup if_data carp(4) vhid. */
24406d076ae8SBjoern A. Zeeb 	if (carp_get_vhid_p != NULL)
2441b245f96cSGleb Smirnoff 		ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
24426d076ae8SBjoern A. Zeeb 
24436d076ae8SBjoern A. Zeeb 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
24446d076ae8SBjoern A. Zeeb }
24456d076ae8SBjoern A. Zeeb 
24466d076ae8SBjoern A. Zeeb static int
sysctl_iflist_ifam(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)24476d076ae8SBjoern A. Zeeb sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
24486d076ae8SBjoern A. Zeeb     struct walkarg *w, int len)
24496d076ae8SBjoern A. Zeeb {
24506d076ae8SBjoern A. Zeeb 	struct ifa_msghdr *ifam;
24516d076ae8SBjoern A. Zeeb 
24526d076ae8SBjoern A. Zeeb 	ifam = (struct ifa_msghdr *)w->w_tmem;
24536d076ae8SBjoern A. Zeeb 	ifam->ifam_addrs = info->rti_addrs;
24546d076ae8SBjoern A. Zeeb 	ifam->ifam_flags = ifa->ifa_flags;
24556d076ae8SBjoern A. Zeeb 	ifam->ifam_index = ifa->ifa_ifp->if_index;
2456d25f8522SMark Johnston 	ifam->_ifam_spare1 = 0;
24579753faf5SGleb Smirnoff 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
24586d076ae8SBjoern A. Zeeb 
24596d076ae8SBjoern A. Zeeb 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
24606d076ae8SBjoern A. Zeeb }
24616d076ae8SBjoern A. Zeeb 
24626d076ae8SBjoern A. Zeeb static int
sysctl_iflist(int af,struct walkarg * w)2463becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w)
2464df8bae1dSRodney W. Grimes {
2465becc44d7SSam Leffler 	struct ifnet *ifp;
2466becc44d7SSam Leffler 	struct ifaddr *ifa;
2467ab607f28SRyan Stone 	struct if_data ifd;
2468df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
2469df8bae1dSRodney W. Grimes 	int len, error = 0;
24703deb3649SAlexander V. Chernikov 	struct sockaddr_storage ss;
2471df8bae1dSRodney W. Grimes 
2472df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
2473ab607f28SRyan Stone 	bzero(&ifd, sizeof(ifd));
24744f6c66ccSMatt Macy 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2475df8bae1dSRodney W. Grimes 		if (w->w_arg && w->w_arg != ifp->if_index)
2476df8bae1dSRodney W. Grimes 			continue;
2477ab607f28SRyan Stone 		if_data_copy(ifp, &ifd);
24784a0d6638SRuslan Ermilov 		ifa = ifp->if_addr;
2479becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2480f2e5eb36SAlexander V. Chernikov 		error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2481f2e5eb36SAlexander V. Chernikov 		if (error != 0)
2482f2e5eb36SAlexander V. Chernikov 			goto done;
24835dfc91d7SLuigi Rizzo 		info.rti_info[RTAX_IFP] = NULL;
248452041295SPoul-Henning Kamp 		if (w->w_req && w->w_tmem) {
24856d076ae8SBjoern A. Zeeb 			if (w->w_op == NET_RT_IFLISTL)
2486ab607f28SRyan Stone 				error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2487ab607f28SRyan Stone 				    len);
24886d076ae8SBjoern A. Zeeb 			else
2489ab607f28SRyan Stone 				error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2490ab607f28SRyan Stone 				    len);
2491df440948SPoul-Henning Kamp 			if (error)
2492a35b06c5SJonathan Lemon 				goto done;
2493df8bae1dSRodney W. Grimes 		}
2494d7c5a620SMatt Macy 		while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2495df8bae1dSRodney W. Grimes 			if (af && af != ifa->ifa_addr->sa_family)
2496df8bae1dSRodney W. Grimes 				continue;
2497b89e82ddSJamie Gritton 			if (prison_if(w->w_req->td->td_ucred,
2498b89e82ddSJamie Gritton 			    ifa->ifa_addr) != 0)
249975c13541SPoul-Henning Kamp 				continue;
2500becc44d7SSam Leffler 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
25013deb3649SAlexander V. Chernikov 			info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
25023deb3649SAlexander V. Chernikov 			    ifa->ifa_addr, ifa->ifa_netmask, &ss);
2503becc44d7SSam Leffler 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2504f2e5eb36SAlexander V. Chernikov 			error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2505f2e5eb36SAlexander V. Chernikov 			if (error != 0)
2506f2e5eb36SAlexander V. Chernikov 				goto done;
250752041295SPoul-Henning Kamp 			if (w->w_req && w->w_tmem) {
25086d076ae8SBjoern A. Zeeb 				if (w->w_op == NET_RT_IFLISTL)
25096d076ae8SBjoern A. Zeeb 					error = sysctl_iflist_ifaml(ifa, &info,
25106d076ae8SBjoern A. Zeeb 					    w, len);
25116d076ae8SBjoern A. Zeeb 				else
25126d076ae8SBjoern A. Zeeb 					error = sysctl_iflist_ifam(ifa, &info,
25136d076ae8SBjoern A. Zeeb 					    w, len);
2514df440948SPoul-Henning Kamp 				if (error)
2515a35b06c5SJonathan Lemon 					goto done;
2516df8bae1dSRodney W. Grimes 			}
2517df8bae1dSRodney W. Grimes 		}
25183deb3649SAlexander V. Chernikov 		info.rti_info[RTAX_IFA] = NULL;
25193deb3649SAlexander V. Chernikov 		info.rti_info[RTAX_NETMASK] = NULL;
25205dfc91d7SLuigi Rizzo 		info.rti_info[RTAX_BRD] = NULL;
2521df8bae1dSRodney W. Grimes 	}
2522a35b06c5SJonathan Lemon done:
2523a35b06c5SJonathan Lemon 	return (error);
2524df8bae1dSRodney W. Grimes }
2525df8bae1dSRodney W. Grimes 
2526ebda3fc3SBjoern A. Zeeb static int
sysctl_ifmalist(int af,struct walkarg * w)25279b98ee2cSLuigi Rizzo sysctl_ifmalist(int af, struct walkarg *w)
252805b2efe0SBruce M Simpson {
252905b2efe0SBruce M Simpson 	struct rt_addrinfo info;
25309b98ee2cSLuigi Rizzo 	struct ifaddr *ifa;
253155dfce58SMark Johnston 	struct ifmultiaddr *ifma;
253255dfce58SMark Johnston 	struct ifnet *ifp;
253355dfce58SMark Johnston 	int error, len;
253405b2efe0SBruce M Simpson 
2535b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
2536b8a6e03fSGleb Smirnoff 
253755dfce58SMark Johnston 	error = 0;
253805b2efe0SBruce M Simpson 	bzero((caddr_t)&info, sizeof(info));
253955dfce58SMark Johnston 
25404f6c66ccSMatt Macy 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
254105b2efe0SBruce M Simpson 		if (w->w_arg && w->w_arg != ifp->if_index)
254205b2efe0SBruce M Simpson 			continue;
25434a0d6638SRuslan Ermilov 		ifa = ifp->if_addr;
2544913af518SLuigi Rizzo 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2545d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
254605b2efe0SBruce M Simpson 			if (af && af != ifma->ifma_addr->sa_family)
254705b2efe0SBruce M Simpson 				continue;
2548b89e82ddSJamie Gritton 			if (prison_if(w->w_req->td->td_ucred,
2549b89e82ddSJamie Gritton 			    ifma->ifma_addr) != 0)
255005b2efe0SBruce M Simpson 				continue;
255105b2efe0SBruce M Simpson 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2552913af518SLuigi Rizzo 			info.rti_info[RTAX_GATEWAY] =
2553913af518SLuigi Rizzo 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
2554913af518SLuigi Rizzo 			    ifma->ifma_lladdr : NULL;
2555c9f98940SAlexander V. Chernikov 			error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2556f2e5eb36SAlexander V. Chernikov 			if (error != 0)
255755dfce58SMark Johnston 				break;
255805b2efe0SBruce M Simpson 			if (w->w_req && w->w_tmem) {
25599b98ee2cSLuigi Rizzo 				struct ifma_msghdr *ifmam;
256005b2efe0SBruce M Simpson 
256105b2efe0SBruce M Simpson 				ifmam = (struct ifma_msghdr *)w->w_tmem;
256205b2efe0SBruce M Simpson 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
256305b2efe0SBruce M Simpson 				ifmam->ifmam_flags = 0;
256405b2efe0SBruce M Simpson 				ifmam->ifmam_addrs = info.rti_addrs;
2565d25f8522SMark Johnston 				ifmam->_ifmam_spare1 = 0;
256605b2efe0SBruce M Simpson 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
256755dfce58SMark Johnston 				if (error != 0)
256855dfce58SMark Johnston 					break;
256905b2efe0SBruce M Simpson 			}
257005b2efe0SBruce M Simpson 		}
257155dfce58SMark Johnston 		if (error != 0)
257255dfce58SMark Johnston 			break;
2573fe0fc7efSChristian S.J. Peron 	}
257405b2efe0SBruce M Simpson 	return (error);
257505b2efe0SBruce M Simpson }
257605b2efe0SBruce M Simpson 
2577d68fb8d9SAlexander V. Chernikov static void
rtable_sysctl_dump(uint32_t fibnum,int family,struct walkarg * w)2578d68fb8d9SAlexander V. Chernikov rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2579d68fb8d9SAlexander V. Chernikov {
2580d68fb8d9SAlexander V. Chernikov 	union sockaddr_union sa_dst, sa_mask;
2581d68fb8d9SAlexander V. Chernikov 
2582d68fb8d9SAlexander V. Chernikov 	w->family = family;
2583d68fb8d9SAlexander V. Chernikov 	w->dst = (struct sockaddr *)&sa_dst;
2584d68fb8d9SAlexander V. Chernikov 	w->mask = (struct sockaddr *)&sa_mask;
2585d68fb8d9SAlexander V. Chernikov 
2586d68fb8d9SAlexander V. Chernikov 	init_sockaddrs_family(family, w->dst, w->mask);
2587d68fb8d9SAlexander V. Chernikov 
2588d68fb8d9SAlexander V. Chernikov 	rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2589d68fb8d9SAlexander V. Chernikov }
2590d68fb8d9SAlexander V. Chernikov 
259152041295SPoul-Henning Kamp static int
sysctl_rtsock(SYSCTL_HANDLER_ARGS)259282d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2593df8bae1dSRodney W. Grimes {
2594b8a6e03fSGleb Smirnoff 	struct epoch_tracker et;
259552041295SPoul-Henning Kamp 	int	*name = (int *)arg1;
259652041295SPoul-Henning Kamp 	u_int	namelen = arg2;
259761eee0e2SAlexander V. Chernikov 	struct rib_head *rnh = NULL; /* silence compiler. */
2598fe0fc7efSChristian S.J. Peron 	int	i, lim, error = EINVAL;
25994825b1e0SHiroki Sato 	int	fib = 0;
2600df8bae1dSRodney W. Grimes 	u_char	af;
2601df8bae1dSRodney W. Grimes 	struct	walkarg w;
2602df8bae1dSRodney W. Grimes 
26030dcef81dSMark Johnston 	if (namelen < 3)
26040dcef81dSMark Johnston 		return (EINVAL);
26050dcef81dSMark Johnston 
260652041295SPoul-Henning Kamp 	name++;
260752041295SPoul-Henning Kamp 	namelen--;
260852041295SPoul-Henning Kamp 	if (req->newptr)
2609df8bae1dSRodney W. Grimes 		return (EPERM);
2610fedeb08bSAlexander V. Chernikov 	if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
26114825b1e0SHiroki Sato 		if (namelen == 3)
26124825b1e0SHiroki Sato 			fib = req->td->td_proc->p_fibnum;
26134825b1e0SHiroki Sato 		else if (namelen == 4)
26147d9b6df1SAlexander V. Chernikov 			fib = (name[3] == RT_ALL_FIBS) ?
26154825b1e0SHiroki Sato 			    req->td->td_proc->p_fibnum : name[3];
26164825b1e0SHiroki Sato 		else
26174825b1e0SHiroki Sato 			return ((namelen < 3) ? EISDIR : ENOTDIR);
26184825b1e0SHiroki Sato 		if (fib < 0 || fib >= rt_numfibs)
26194825b1e0SHiroki Sato 			return (EINVAL);
26204825b1e0SHiroki Sato 	} else if (namelen != 3)
2621f7a54d06SCrist J. Clark 		return ((namelen < 3) ? EISDIR : ENOTDIR);
2622df8bae1dSRodney W. Grimes 	af = name[0];
2623b2aaf46eSJeffrey Hsu 	if (af > AF_MAX)
2624b2aaf46eSJeffrey Hsu 		return (EINVAL);
26256b96f1afSLuigi Rizzo 	bzero(&w, sizeof(w));
2626df8bae1dSRodney W. Grimes 	w.w_op = name[1];
2627df8bae1dSRodney W. Grimes 	w.w_arg = name[2];
262852041295SPoul-Henning Kamp 	w.w_req = req;
2629df8bae1dSRodney W. Grimes 
2630fe0fc7efSChristian S.J. Peron 	error = sysctl_wire_old_buffer(req, 0);
2631fe0fc7efSChristian S.J. Peron 	if (error)
2632fe0fc7efSChristian S.J. Peron 		return (error);
2633f2e5eb36SAlexander V. Chernikov 
2634f2e5eb36SAlexander V. Chernikov 	/*
2635f2e5eb36SAlexander V. Chernikov 	 * Allocate reply buffer in advance.
2636f2e5eb36SAlexander V. Chernikov 	 * All rtsock messages has maximum length of u_short.
2637f2e5eb36SAlexander V. Chernikov 	 */
2638f2e5eb36SAlexander V. Chernikov 	w.w_tmemsize = 65536;
2639f2e5eb36SAlexander V. Chernikov 	w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2640f2e5eb36SAlexander V. Chernikov 
2641b8a6e03fSGleb Smirnoff 	NET_EPOCH_ENTER(et);
2642df8bae1dSRodney W. Grimes 	switch (w.w_op) {
2643df8bae1dSRodney W. Grimes 	case NET_RT_DUMP:
2644df8bae1dSRodney W. Grimes 	case NET_RT_FLAGS:
2645a8b76c8fSLuigi Rizzo 		if (af == 0) {			/* dump all tables */
2646a8b76c8fSLuigi Rizzo 			i = 1;
2647a8b76c8fSLuigi Rizzo 			lim = AF_MAX;
2648a8b76c8fSLuigi Rizzo 		} else				/* dump only one table */
2649a8b76c8fSLuigi Rizzo 			i = lim = af;
26508eca593cSQing Li 
26518eca593cSQing Li 		/*
26528eca593cSQing Li 		 * take care of llinfo entries, the caller must
26538eca593cSQing Li 		 * specify an AF
26548eca593cSQing Li 		 */
265514981d80SQing Li 		if (w.w_op == NET_RT_FLAGS &&
265682b334e8SQing Li 		    (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
26578eca593cSQing Li 			if (af != 0)
26588eca593cSQing Li 				error = lltable_sysctl_dumparp(af, w.w_req);
26598eca593cSQing Li 			else
26608eca593cSQing Li 				error = EINVAL;
26618eca593cSQing Li 			break;
26628eca593cSQing Li 		}
26638eca593cSQing Li 		/*
26648eca593cSQing Li 		 * take care of routing entries
26658eca593cSQing Li 		 */
266620b0cdb7SBjoern A. Zeeb 		for (error = 0; error == 0 && i <= lim; i++) {
26674825b1e0SHiroki Sato 			rnh = rt_tables_get_rnh(fib, i);
2668c2c2a7c1SBjoern A. Zeeb 			if (rnh != NULL) {
2669d68fb8d9SAlexander V. Chernikov 				rtable_sysctl_dump(fib, i, &w);
2670a8b76c8fSLuigi Rizzo 			} else if (af != 0)
2671956b0b65SJeffrey Hsu 				error = EAFNOSUPPORT;
267220b0cdb7SBjoern A. Zeeb 		}
2673df8bae1dSRodney W. Grimes 		break;
2674a6663252SAlexander V. Chernikov 	case NET_RT_NHOP:
2675fedeb08bSAlexander V. Chernikov 	case NET_RT_NHGRP:
2676a6663252SAlexander V. Chernikov 		/* Allow dumping one specific af/fib at a time */
2677a6663252SAlexander V. Chernikov 		if (namelen < 4) {
2678a6663252SAlexander V. Chernikov 			error = EINVAL;
2679a6663252SAlexander V. Chernikov 			break;
2680a6663252SAlexander V. Chernikov 		}
2681a6663252SAlexander V. Chernikov 		fib = name[3];
2682a6663252SAlexander V. Chernikov 		if (fib < 0 || fib > rt_numfibs) {
2683a6663252SAlexander V. Chernikov 			error = EINVAL;
2684a6663252SAlexander V. Chernikov 			break;
2685a6663252SAlexander V. Chernikov 		}
2686a6663252SAlexander V. Chernikov 		rnh = rt_tables_get_rnh(fib, af);
2687a6663252SAlexander V. Chernikov 		if (rnh == NULL) {
2688a6663252SAlexander V. Chernikov 			error = EAFNOSUPPORT;
2689a6663252SAlexander V. Chernikov 			break;
2690a6663252SAlexander V. Chernikov 		}
2691a6663252SAlexander V. Chernikov 		if (w.w_op == NET_RT_NHOP)
2692a6663252SAlexander V. Chernikov 			error = nhops_dump_sysctl(rnh, w.w_req);
2693fedeb08bSAlexander V. Chernikov 		else
2694fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
2695fedeb08bSAlexander V. Chernikov 			error = nhgrp_dump_sysctl(rnh, w.w_req);
2696fedeb08bSAlexander V. Chernikov #else
2697fedeb08bSAlexander V. Chernikov 			error = ENOTSUP;
2698fedeb08bSAlexander V. Chernikov #endif
2699a6663252SAlexander V. Chernikov 		break;
2700df8bae1dSRodney W. Grimes 	case NET_RT_IFLIST:
27016d076ae8SBjoern A. Zeeb 	case NET_RT_IFLISTL:
2702df8bae1dSRodney W. Grimes 		error = sysctl_iflist(af, &w);
270305b2efe0SBruce M Simpson 		break;
270405b2efe0SBruce M Simpson 
270505b2efe0SBruce M Simpson 	case NET_RT_IFMALIST:
270605b2efe0SBruce M Simpson 		error = sysctl_ifmalist(af, &w);
270705b2efe0SBruce M Simpson 		break;
2708df8bae1dSRodney W. Grimes 	}
2709b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
2710f2e5eb36SAlexander V. Chernikov 
2711f2e5eb36SAlexander V. Chernikov 	free(w.w_tmem, M_TEMP);
2712df8bae1dSRodney W. Grimes 	return (error);
2713df8bae1dSRodney W. Grimes }
2714df8bae1dSRodney W. Grimes 
27158a9a28c4SGleb Smirnoff static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
27168a9a28c4SGleb Smirnoff     sysctl_rtsock, "Return route tables and interface/address lists");
271752041295SPoul-Henning Kamp 
2718df8bae1dSRodney W. Grimes /*
2719df8bae1dSRodney W. Grimes  * Definitions of protocols supported in the ROUTE domain.
2720df8bae1dSRodney W. Grimes  */
2721df8bae1dSRodney W. Grimes 
27225b1c0294SDavid E. O'Brien static struct domain routedomain;		/* or at least forward */
2723df8bae1dSRodney W. Grimes 
2724e7d02be1SGleb Smirnoff static struct protosw routesw = {
2725303989a2SRuslan Ermilov 	.pr_type =		SOCK_RAW,
2726303989a2SRuslan Ermilov 	.pr_flags =		PR_ATOMIC|PR_ADDR,
2727e7d02be1SGleb Smirnoff 	.pr_abort =		rts_close,
2728e7d02be1SGleb Smirnoff 	.pr_attach =		rts_attach,
2729*caccbaefSMark Johnston 	.pr_ctloutput =		rts_ctloutput,
2730e7d02be1SGleb Smirnoff 	.pr_detach =		rts_detach,
2731e7d02be1SGleb Smirnoff 	.pr_send =		rts_send,
2732e7d02be1SGleb Smirnoff 	.pr_shutdown =		rts_shutdown,
273342904794SAlexander V. Chernikov 	.pr_disconnect =	rts_disconnect,
2734e7d02be1SGleb Smirnoff 	.pr_close =		rts_close,
2735df8bae1dSRodney W. Grimes };
2736df8bae1dSRodney W. Grimes 
2737303989a2SRuslan Ermilov static struct domain routedomain = {
2738303989a2SRuslan Ermilov 	.dom_family =		PF_ROUTE,
2739303989a2SRuslan Ermilov 	.dom_name =		"route",
2740e7d02be1SGleb Smirnoff 	.dom_nprotosw =		1,
2741e7d02be1SGleb Smirnoff 	.dom_protosw =		{ &routesw },
2742303989a2SRuslan Ermilov };
274378a82810SGarrett Wollman 
2744644ca084SGleb Smirnoff DOMAIN_SET(route);
2745