xref: /freebsd/sys/net/rtsock.c (revision aea8b30f12f5005bae5723fbbcaa606f4a0ac486)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
3328070a0eSRuslan Ermilov  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38960ed29cSSeigo Tanimura #include <sys/domain.h>
39748e0b0aSGarrett Wollman #include <sys/kernel.h>
40960ed29cSSeigo Tanimura #include <sys/jail.h>
414d1d4912SBruce Evans #include <sys/malloc.h>
42df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
43960ed29cSSeigo Tanimura #include <sys/proc.h>
44960ed29cSSeigo Tanimura #include <sys/protosw.h>
45960ed29cSSeigo Tanimura #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/socket.h>
47df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
48960ed29cSSeigo Tanimura #include <sys/sysctl.h>
49960ed29cSSeigo Tanimura #include <sys/systm.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <net/if.h>
52df8bae1dSRodney W. Grimes #include <net/raw_cb.h>
53960ed29cSSeigo Tanimura #include <net/route.h>
54df8bae1dSRodney W. Grimes 
55a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
56a1c995b6SPoul-Henning Kamp 
57becc44d7SSam Leffler /* NB: these are not modified */
5852041295SPoul-Henning Kamp static struct	sockaddr route_dst = { 2, PF_ROUTE, };
5952041295SPoul-Henning Kamp static struct	sockaddr route_src = { 2, PF_ROUTE, };
60076d0761SJulian Elischer static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
61becc44d7SSam Leffler 
62becc44d7SSam Leffler static struct {
63becc44d7SSam Leffler 	int	ip_count;	/* attacked w/ AF_INET */
64becc44d7SSam Leffler 	int	ip6_count;	/* attached w/ AF_INET6 */
65becc44d7SSam Leffler 	int	ipx_count;	/* attached w/ AF_IPX */
66becc44d7SSam Leffler 	int	any_count;	/* total attached */
67becc44d7SSam Leffler } route_cb;
68df8bae1dSRodney W. Grimes 
69aea8b30fSSam Leffler struct mtx rtsock_mtx;
70aea8b30fSSam Leffler MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
71aea8b30fSSam Leffler 
72aea8b30fSSam Leffler #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
73aea8b30fSSam Leffler #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
74aea8b30fSSam Leffler #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
75aea8b30fSSam Leffler 
76df8bae1dSRodney W. Grimes struct walkarg {
7752041295SPoul-Henning Kamp 	int	w_tmemsize;
7852041295SPoul-Henning Kamp 	int	w_op, w_arg;
7952041295SPoul-Henning Kamp 	caddr_t	w_tmem;
8052041295SPoul-Henning Kamp 	struct sysctl_req *w_req;
81df8bae1dSRodney W. Grimes };
82df8bae1dSRodney W. Grimes 
83becc44d7SSam Leffler static struct mbuf *rt_msg1(int, struct rt_addrinfo *);
84929ddbbbSAlfred Perlstein static int	rt_msg2(int, struct rt_addrinfo *, caddr_t, struct walkarg *);
85929ddbbbSAlfred Perlstein static int	rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
86929ddbbbSAlfred Perlstein static int	sysctl_dumpentry(struct radix_node *rn, void *vw);
87929ddbbbSAlfred Perlstein static int	sysctl_iflist(int af, struct walkarg *w);
88929ddbbbSAlfred Perlstein static int	route_output(struct mbuf *, struct socket *);
89929ddbbbSAlfred Perlstein static void	rt_setmetrics(u_long, struct rt_metrics *, struct rt_metrics *);
90becc44d7SSam Leffler static void	rt_dispatch(struct mbuf *, struct sockaddr *);
91df8bae1dSRodney W. Grimes 
92a29f300eSGarrett Wollman /*
93a29f300eSGarrett Wollman  * It really doesn't make any sense at all for this code to share much
94a29f300eSGarrett Wollman  * with raw_usrreq.c, since its functionality is so restricted.  XXX
95a29f300eSGarrett Wollman  */
9652041295SPoul-Henning Kamp static int
97a29f300eSGarrett Wollman rts_abort(struct socket *so)
98df8bae1dSRodney W. Grimes {
99a29f300eSGarrett Wollman 	int s, error;
100df8bae1dSRodney W. Grimes 	s = splnet();
101a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_abort(so);
102df8bae1dSRodney W. Grimes 	splx(s);
103a29f300eSGarrett Wollman 	return error;
104df8bae1dSRodney W. Grimes }
105a29f300eSGarrett Wollman 
106a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */
107a29f300eSGarrett Wollman 
108a29f300eSGarrett Wollman static int
109b40ce416SJulian Elischer rts_attach(struct socket *so, int proto, struct thread *td)
110a29f300eSGarrett Wollman {
111a29f300eSGarrett Wollman 	struct rawcb *rp;
112a29f300eSGarrett Wollman 	int s, error;
113a29f300eSGarrett Wollman 
114a29f300eSGarrett Wollman 	if (sotorawcb(so) != 0)
115a29f300eSGarrett Wollman 		return EISCONN;	/* XXX panic? */
1167cc0979fSDavid Malone 	/* XXX */
117a163d034SWarner Losh 	MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
118a29f300eSGarrett Wollman 	if (rp == 0)
119a29f300eSGarrett Wollman 		return ENOBUFS;
120a29f300eSGarrett Wollman 
121a29f300eSGarrett Wollman 	/*
122a29f300eSGarrett Wollman 	 * The splnet() is necessary to block protocols from sending
123a29f300eSGarrett Wollman 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
124a29f300eSGarrett Wollman 	 * this PCB is extant but incompletely initialized.
125a29f300eSGarrett Wollman 	 * Probably we should try to do more of this work beforehand and
126a29f300eSGarrett Wollman 	 * eliminate the spl.
127a29f300eSGarrett Wollman 	 */
128a29f300eSGarrett Wollman 	s = splnet();
129a29f300eSGarrett Wollman 	so->so_pcb = (caddr_t)rp;
130162c0b2eSRuslan Ermilov 	error = raw_attach(so, proto);
131a29f300eSGarrett Wollman 	rp = sotorawcb(so);
132a29f300eSGarrett Wollman 	if (error) {
133a29f300eSGarrett Wollman 		splx(s);
1347ba271aeSJonathan Chen 		so->so_pcb = NULL;
135a29f300eSGarrett Wollman 		free(rp, M_PCB);
136a29f300eSGarrett Wollman 		return error;
137a29f300eSGarrett Wollman 	}
138aea8b30fSSam Leffler 	RTSOCK_LOCK();
139a29f300eSGarrett Wollman 	switch(rp->rcb_proto.sp_protocol) {
140a29f300eSGarrett Wollman 	case AF_INET:
141df8bae1dSRodney W. Grimes 		route_cb.ip_count++;
142a29f300eSGarrett Wollman 		break;
143899ce4f4SYoshinobu Inoue 	case AF_INET6:
144899ce4f4SYoshinobu Inoue 		route_cb.ip6_count++;
145899ce4f4SYoshinobu Inoue 		break;
146a29f300eSGarrett Wollman 	case AF_IPX:
147cc6a66f2SJulian Elischer 		route_cb.ipx_count++;
148a29f300eSGarrett Wollman 		break;
149a29f300eSGarrett Wollman 	}
150df8bae1dSRodney W. Grimes 	rp->rcb_faddr = &route_src;
151df8bae1dSRodney W. Grimes 	route_cb.any_count++;
152aea8b30fSSam Leffler 	RTSOCK_UNLOCK();
15303e49181SSeigo Tanimura 	soisconnected(so);
154df8bae1dSRodney W. Grimes 	so->so_options |= SO_USELOOPBACK;
155df8bae1dSRodney W. Grimes 	splx(s);
156a29f300eSGarrett Wollman 	return 0;
157df8bae1dSRodney W. Grimes }
158df8bae1dSRodney W. Grimes 
159a29f300eSGarrett Wollman static int
160b40ce416SJulian Elischer rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
161a29f300eSGarrett Wollman {
162a29f300eSGarrett Wollman 	int s, error;
163a29f300eSGarrett Wollman 	s = splnet();
164b40ce416SJulian Elischer 	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
165a29f300eSGarrett Wollman 	splx(s);
166a29f300eSGarrett Wollman 	return error;
167a29f300eSGarrett Wollman }
168a29f300eSGarrett Wollman 
169a29f300eSGarrett Wollman static int
170b40ce416SJulian Elischer rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
171a29f300eSGarrett Wollman {
172a29f300eSGarrett Wollman 	int s, error;
173a29f300eSGarrett Wollman 	s = splnet();
174b40ce416SJulian Elischer 	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
175a29f300eSGarrett Wollman 	splx(s);
176a29f300eSGarrett Wollman 	return error;
177a29f300eSGarrett Wollman }
178a29f300eSGarrett Wollman 
179a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */
180a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */
181a29f300eSGarrett Wollman 
182a29f300eSGarrett Wollman static int
183a29f300eSGarrett Wollman rts_detach(struct socket *so)
184a29f300eSGarrett Wollman {
185a29f300eSGarrett Wollman 	struct rawcb *rp = sotorawcb(so);
186a29f300eSGarrett Wollman 	int s, error;
187a29f300eSGarrett Wollman 
188a29f300eSGarrett Wollman 	s = splnet();
189a29f300eSGarrett Wollman 	if (rp != 0) {
190aea8b30fSSam Leffler 		RTSOCK_LOCK();
191a29f300eSGarrett Wollman 		switch(rp->rcb_proto.sp_protocol) {
192a29f300eSGarrett Wollman 		case AF_INET:
193a29f300eSGarrett Wollman 			route_cb.ip_count--;
194a29f300eSGarrett Wollman 			break;
195899ce4f4SYoshinobu Inoue 		case AF_INET6:
196899ce4f4SYoshinobu Inoue 			route_cb.ip6_count--;
197899ce4f4SYoshinobu Inoue 			break;
198a29f300eSGarrett Wollman 		case AF_IPX:
199a29f300eSGarrett Wollman 			route_cb.ipx_count--;
200a29f300eSGarrett Wollman 			break;
201a29f300eSGarrett Wollman 		}
202a29f300eSGarrett Wollman 		route_cb.any_count--;
203aea8b30fSSam Leffler 		RTSOCK_UNLOCK();
204a29f300eSGarrett Wollman 	}
205a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_detach(so);
206a29f300eSGarrett Wollman 	splx(s);
207a29f300eSGarrett Wollman 	return error;
208a29f300eSGarrett Wollman }
209a29f300eSGarrett Wollman 
210a29f300eSGarrett Wollman static int
211a29f300eSGarrett Wollman rts_disconnect(struct socket *so)
212a29f300eSGarrett Wollman {
213a29f300eSGarrett Wollman 	int s, error;
214a29f300eSGarrett Wollman 	s = splnet();
215a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_disconnect(so);
216a29f300eSGarrett Wollman 	splx(s);
217a29f300eSGarrett Wollman 	return error;
218a29f300eSGarrett Wollman }
219a29f300eSGarrett Wollman 
220a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */
221a29f300eSGarrett Wollman 
222a29f300eSGarrett Wollman static int
22357bf258eSGarrett Wollman rts_peeraddr(struct socket *so, struct sockaddr **nam)
224a29f300eSGarrett Wollman {
225a29f300eSGarrett Wollman 	int s, error;
226a29f300eSGarrett Wollman 	s = splnet();
227a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_peeraddr(so, nam);
228a29f300eSGarrett Wollman 	splx(s);
229a29f300eSGarrett Wollman 	return error;
230a29f300eSGarrett Wollman }
231a29f300eSGarrett Wollman 
232a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */
233a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
234a29f300eSGarrett Wollman 
235a29f300eSGarrett Wollman static int
23657bf258eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
237b40ce416SJulian Elischer 	 struct mbuf *control, struct thread *td)
238a29f300eSGarrett Wollman {
239a29f300eSGarrett Wollman 	int s, error;
240a29f300eSGarrett Wollman 	s = splnet();
241b40ce416SJulian Elischer 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
242a29f300eSGarrett Wollman 	splx(s);
243a29f300eSGarrett Wollman 	return error;
244a29f300eSGarrett Wollman }
245a29f300eSGarrett Wollman 
246a29f300eSGarrett Wollman /* pru_sense is null */
247a29f300eSGarrett Wollman 
248a29f300eSGarrett Wollman static int
249a29f300eSGarrett Wollman rts_shutdown(struct socket *so)
250a29f300eSGarrett Wollman {
251a29f300eSGarrett Wollman 	int s, error;
252a29f300eSGarrett Wollman 	s = splnet();
253a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_shutdown(so);
254a29f300eSGarrett Wollman 	splx(s);
255a29f300eSGarrett Wollman 	return error;
256a29f300eSGarrett Wollman }
257a29f300eSGarrett Wollman 
258a29f300eSGarrett Wollman static int
25957bf258eSGarrett Wollman rts_sockaddr(struct socket *so, struct sockaddr **nam)
260a29f300eSGarrett Wollman {
261a29f300eSGarrett Wollman 	int s, error;
262a29f300eSGarrett Wollman 	s = splnet();
263a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_sockaddr(so, nam);
264a29f300eSGarrett Wollman 	splx(s);
265a29f300eSGarrett Wollman 	return error;
266a29f300eSGarrett Wollman }
267a29f300eSGarrett Wollman 
268a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = {
269a29f300eSGarrett Wollman 	rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
270a29f300eSGarrett Wollman 	pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
271a29f300eSGarrett Wollman 	pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
272a29f300eSGarrett Wollman 	rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
273f8f6cbbaSPeter Wemm 	sosend, soreceive, sopoll
274a29f300eSGarrett Wollman };
275a29f300eSGarrett Wollman 
276df8bae1dSRodney W. Grimes /*ARGSUSED*/
27752041295SPoul-Henning Kamp static int
278df8bae1dSRodney W. Grimes route_output(m, so)
279df8bae1dSRodney W. Grimes 	register struct mbuf *m;
280df8bae1dSRodney W. Grimes 	struct socket *so;
281df8bae1dSRodney W. Grimes {
282becc44d7SSam Leffler #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
283df8bae1dSRodney W. Grimes 	register struct rt_msghdr *rtm = 0;
284df8bae1dSRodney W. Grimes 	register struct rtentry *rt = 0;
28578a82810SGarrett Wollman 	struct radix_node_head *rnh;
286df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
287df8bae1dSRodney W. Grimes 	int len, error = 0;
288df8bae1dSRodney W. Grimes 	struct ifnet *ifp = 0;
289df8bae1dSRodney W. Grimes 	struct ifaddr *ifa = 0;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;}
292df8bae1dSRodney W. Grimes 	if (m == 0 || ((m->m_len < sizeof(long)) &&
293df8bae1dSRodney W. Grimes 		       (m = m_pullup(m, sizeof(long))) == 0))
294df8bae1dSRodney W. Grimes 		return (ENOBUFS);
295df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
296df8bae1dSRodney W. Grimes 		panic("route_output");
297df8bae1dSRodney W. Grimes 	len = m->m_pkthdr.len;
298df8bae1dSRodney W. Grimes 	if (len < sizeof(*rtm) ||
299df8bae1dSRodney W. Grimes 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
300becc44d7SSam Leffler 		info.rti_info[RTAX_DST] = 0;
301df8bae1dSRodney W. Grimes 		senderr(EINVAL);
302df8bae1dSRodney W. Grimes 	}
303df8bae1dSRodney W. Grimes 	R_Malloc(rtm, struct rt_msghdr *, len);
304df8bae1dSRodney W. Grimes 	if (rtm == 0) {
305becc44d7SSam Leffler 		info.rti_info[RTAX_DST] = 0;
306df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
307df8bae1dSRodney W. Grimes 	}
308df8bae1dSRodney W. Grimes 	m_copydata(m, 0, len, (caddr_t)rtm);
309df8bae1dSRodney W. Grimes 	if (rtm->rtm_version != RTM_VERSION) {
310becc44d7SSam Leffler 		info.rti_info[RTAX_DST] = 0;
311df8bae1dSRodney W. Grimes 		senderr(EPROTONOSUPPORT);
312df8bae1dSRodney W. Grimes 	}
313df8bae1dSRodney W. Grimes 	rtm->rtm_pid = curproc->p_pid;
3148071913dSRuslan Ermilov 	bzero(&info, sizeof(info));
315df8bae1dSRodney W. Grimes 	info.rti_addrs = rtm->rtm_addrs;
316076d0761SJulian Elischer 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
317becc44d7SSam Leffler 		info.rti_info[RTAX_DST] = 0;
318076d0761SJulian Elischer 		senderr(EINVAL);
319076d0761SJulian Elischer 	}
3208071913dSRuslan Ermilov 	info.rti_flags = rtm->rtm_flags;
321becc44d7SSam Leffler 	if (info.rti_info[RTAX_DST] == 0 ||
322becc44d7SSam Leffler 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
323becc44d7SSam Leffler 	    (info.rti_info[RTAX_GATEWAY] != 0 &&
324becc44d7SSam Leffler 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
325df8bae1dSRodney W. Grimes 		senderr(EINVAL);
326becc44d7SSam Leffler 	if (info.rti_info[RTAX_GENMASK]) {
327df8bae1dSRodney W. Grimes 		struct radix_node *t;
328becc44d7SSam Leffler 		t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
329becc44d7SSam Leffler 		if (t && Bcmp((caddr_t *) info.rti_info[RTAX_GENMASK] + 1,
330becc44d7SSam Leffler 			      (caddr_t *)t->rn_key + 1,
331920eb79fSRuslan Ermilov 			      *(u_char *)t->rn_key - 1) == 0)
332becc44d7SSam Leffler 			info.rti_info[RTAX_GENMASK] =
333becc44d7SSam Leffler 				(struct sockaddr *)(t->rn_key);
334df8bae1dSRodney W. Grimes 		else
335df8bae1dSRodney W. Grimes 			senderr(ENOBUFS);
336df8bae1dSRodney W. Grimes 	}
337162c0b2eSRuslan Ermilov 
338162c0b2eSRuslan Ermilov 	/*
339162c0b2eSRuslan Ermilov 	 * Verify that the caller has the appropriate privilege; RTM_GET
340162c0b2eSRuslan Ermilov 	 * is the only operation the non-superuser is allowed.
341162c0b2eSRuslan Ermilov 	 */
34244731cabSJohn Baldwin 	if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0)
343dadb6c3bSRuslan Ermilov 		senderr(error);
344162c0b2eSRuslan Ermilov 
345df8bae1dSRodney W. Grimes 	switch (rtm->rtm_type) {
346becc44d7SSam Leffler 		struct rtentry *saved_nrt;
347df8bae1dSRodney W. Grimes 
348df8bae1dSRodney W. Grimes 	case RTM_ADD:
349becc44d7SSam Leffler 		if (info.rti_info[RTAX_GATEWAY] == 0)
350df8bae1dSRodney W. Grimes 			senderr(EINVAL);
351becc44d7SSam Leffler 		saved_nrt = 0;
3528071913dSRuslan Ermilov 		error = rtrequest1(RTM_ADD, &info, &saved_nrt);
353df8bae1dSRodney W. Grimes 		if (error == 0 && saved_nrt) {
354df8bae1dSRodney W. Grimes 			rt_setmetrics(rtm->rtm_inits,
355df8bae1dSRodney W. Grimes 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
35695b6073cSDavid Greenman 			saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
35795b6073cSDavid Greenman 			saved_nrt->rt_rmx.rmx_locks |=
35895b6073cSDavid Greenman 				(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
359df8bae1dSRodney W. Grimes 			saved_nrt->rt_refcnt--;
360becc44d7SSam Leffler 			saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
361df8bae1dSRodney W. Grimes 		}
362df8bae1dSRodney W. Grimes 		break;
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes 	case RTM_DELETE:
365becc44d7SSam Leffler 		saved_nrt = 0;
3668071913dSRuslan Ermilov 		error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
36778a82810SGarrett Wollman 		if (error == 0) {
36871eba915SRuslan Ermilov 			rt = saved_nrt;
36978a82810SGarrett Wollman 			goto report;
37078a82810SGarrett Wollman 		}
371df8bae1dSRodney W. Grimes 		break;
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes 	case RTM_GET:
374df8bae1dSRodney W. Grimes 	case RTM_CHANGE:
375df8bae1dSRodney W. Grimes 	case RTM_LOCK:
376becc44d7SSam Leffler 		rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
377becc44d7SSam Leffler 		if (rnh == 0)
37878a82810SGarrett Wollman 			senderr(EAFNOSUPPORT);
379956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
380becc44d7SSam Leffler 		rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
381becc44d7SSam Leffler 			info.rti_info[RTAX_NETMASK], rnh);
382956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
383becc44d7SSam Leffler 		if (rt == NULL)		/* XXX looks bogus */
384df8bae1dSRodney W. Grimes 			senderr(ESRCH);
385becc44d7SSam Leffler 		rt->rt_refcnt++;
386956b0b65SJeffrey Hsu 
387df8bae1dSRodney W. Grimes 		switch(rtm->rtm_type) {
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 		case RTM_GET:
39078a82810SGarrett Wollman 		report:
391becc44d7SSam Leffler 			info.rti_info[RTAX_DST] = rt_key(rt);
392becc44d7SSam Leffler 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
393becc44d7SSam Leffler 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
394becc44d7SSam Leffler 			info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
395df8bae1dSRodney W. Grimes 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
396df440948SPoul-Henning Kamp 				ifp = rt->rt_ifp;
397df440948SPoul-Henning Kamp 				if (ifp) {
398becc44d7SSam Leffler 					info.rti_info[RTAX_IFP] = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
399becc44d7SSam Leffler 					info.rti_info[RTAX_IFA] =
400becc44d7SSam Leffler 						rt->rt_ifa->ifa_addr;
40128070a0eSRuslan Ermilov 					if (ifp->if_flags & IFF_POINTOPOINT)
402becc44d7SSam Leffler 						 info.rti_info[RTAX_BRD] =
403becc44d7SSam Leffler 							rt->rt_ifa->ifa_dstaddr;
404df8bae1dSRodney W. Grimes 					rtm->rtm_index = ifp->if_index;
405df8bae1dSRodney W. Grimes 				} else {
406becc44d7SSam Leffler 					info.rti_info[RTAX_IFP] = 0;
407becc44d7SSam Leffler 					info.rti_info[RTAX_IFA] = 0;
408df8bae1dSRodney W. Grimes 				}
409df8bae1dSRodney W. Grimes 			}
41078a82810SGarrett Wollman 			len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
411df8bae1dSRodney W. Grimes 				(struct walkarg *)0);
412df8bae1dSRodney W. Grimes 			if (len > rtm->rtm_msglen) {
413df8bae1dSRodney W. Grimes 				struct rt_msghdr *new_rtm;
414df8bae1dSRodney W. Grimes 				R_Malloc(new_rtm, struct rt_msghdr *, len);
415becc44d7SSam Leffler 				if (new_rtm == 0) {
416df8bae1dSRodney W. Grimes 					senderr(ENOBUFS);
417becc44d7SSam Leffler 				}
418df8bae1dSRodney W. Grimes 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
419df8bae1dSRodney W. Grimes 				Free(rtm); rtm = new_rtm;
420df8bae1dSRodney W. Grimes 			}
42178a82810SGarrett Wollman 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
422df8bae1dSRodney W. Grimes 				(struct walkarg *)0);
423df8bae1dSRodney W. Grimes 			rtm->rtm_flags = rt->rt_flags;
424df8bae1dSRodney W. Grimes 			rtm->rtm_rmx = rt->rt_rmx;
425df8bae1dSRodney W. Grimes 			rtm->rtm_addrs = info.rti_addrs;
426df8bae1dSRodney W. Grimes 			break;
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 		case RTM_CHANGE:
429becc44d7SSam Leffler 			/*
430becc44d7SSam Leffler 			 * New gateway could require new ifaddr, ifp;
431becc44d7SSam Leffler 			 * flags may also be different; ifp may be specified
432becc44d7SSam Leffler 			 * by ll sockaddr when protocol address is ambiguous
433becc44d7SSam Leffler 			 */
434becc44d7SSam Leffler 			if (((rt->rt_flags & RTF_GATEWAY) &&
435becc44d7SSam Leffler 			     info.rti_info[RTAX_GATEWAY] != NULL) ||
436becc44d7SSam Leffler 			    info.rti_info[RTAX_IFP] != NULL ||
437becc44d7SSam Leffler 			    (info.rti_info[RTAX_IFA] != NULL &&
438becc44d7SSam Leffler 			     !sa_equal(info.rti_info[RTAX_IFA],
439becc44d7SSam Leffler 				       rt->rt_ifa->ifa_addr))) {
440becc44d7SSam Leffler 				if ((error = rt_getifa(&info)) != 0) {
4418071913dSRuslan Ermilov 					senderr(error);
44202a5d63eSBrian Somers 				}
443becc44d7SSam Leffler 			}
444becc44d7SSam Leffler 			if (info.rti_info[RTAX_GATEWAY] != NULL &&
445becc44d7SSam Leffler 			    (error = rt_setgate(rt, rt_key(rt),
446becc44d7SSam Leffler 					info.rti_info[RTAX_GATEWAY])) != 0) {
4478071913dSRuslan Ermilov 				senderr(error);
448becc44d7SSam Leffler 			}
4498071913dSRuslan Ermilov 			if ((ifa = info.rti_ifa) != NULL) {
450becc44d7SSam Leffler 				struct ifaddr *oifa = rt->rt_ifa;
451df8bae1dSRodney W. Grimes 				if (oifa != ifa) {
45219fc74fbSJeffrey Hsu 					if (oifa) {
45319fc74fbSJeffrey Hsu 						if (oifa->ifa_rtrequest)
454becc44d7SSam Leffler 							oifa->ifa_rtrequest(
455becc44d7SSam Leffler 								RTM_DELETE, rt,
4568071913dSRuslan Ermilov 								&info);
45712e552d6SJeffrey Hsu 						IFAFREE(oifa);
45819fc74fbSJeffrey Hsu 					}
45919fc74fbSJeffrey Hsu 				        IFAREF(ifa);
460df8bae1dSRodney W. Grimes 				        rt->rt_ifa = ifa;
4618071913dSRuslan Ermilov 				        rt->rt_ifp = info.rti_ifp;
462df8bae1dSRodney W. Grimes 				}
463df8bae1dSRodney W. Grimes 			}
464df8bae1dSRodney W. Grimes 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
465df8bae1dSRodney W. Grimes 					&rt->rt_rmx);
466df8bae1dSRodney W. Grimes 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
4678071913dSRuslan Ermilov 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
468becc44d7SSam Leffler 			if (info.rti_info[RTAX_GENMASK])
469becc44d7SSam Leffler 				rt->rt_genmask = info.rti_info[RTAX_GENMASK];
47093b0017fSPhilippe Charnier 			/* FALLTHROUGH */
471df8bae1dSRodney W. Grimes 		case RTM_LOCK:
472df8bae1dSRodney W. Grimes 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
473df8bae1dSRodney W. Grimes 			rt->rt_rmx.rmx_locks |=
474df8bae1dSRodney W. Grimes 				(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
475df8bae1dSRodney W. Grimes 			break;
476df8bae1dSRodney W. Grimes 		}
477df8bae1dSRodney W. Grimes 		break;
478df8bae1dSRodney W. Grimes 
479df8bae1dSRodney W. Grimes 	default:
480df8bae1dSRodney W. Grimes 		senderr(EOPNOTSUPP);
481df8bae1dSRodney W. Grimes 	}
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes flush:
484df8bae1dSRodney W. Grimes 	if (rtm) {
485df8bae1dSRodney W. Grimes 		if (error)
486df8bae1dSRodney W. Grimes 			rtm->rtm_errno = error;
487df8bae1dSRodney W. Grimes 		else
488df8bae1dSRodney W. Grimes 			rtm->rtm_flags |= RTF_DONE;
489df8bae1dSRodney W. Grimes 	}
490becc44d7SSam Leffler 	if (rt)		/* XXX can this be true? */
491becc44d7SSam Leffler 		RTFREE(rt);
492df8bae1dSRodney W. Grimes     {
493df8bae1dSRodney W. Grimes 	register struct rawcb *rp = 0;
494df8bae1dSRodney W. Grimes 	/*
495df8bae1dSRodney W. Grimes 	 * Check to see if we don't want our own messages.
496df8bae1dSRodney W. Grimes 	 */
497df8bae1dSRodney W. Grimes 	if ((so->so_options & SO_USELOOPBACK) == 0) {
498df8bae1dSRodney W. Grimes 		if (route_cb.any_count <= 1) {
499df8bae1dSRodney W. Grimes 			if (rtm)
500df8bae1dSRodney W. Grimes 				Free(rtm);
501df8bae1dSRodney W. Grimes 			m_freem(m);
502df8bae1dSRodney W. Grimes 			return (error);
503df8bae1dSRodney W. Grimes 		}
504df8bae1dSRodney W. Grimes 		/* There is another listener, so construct message */
505df8bae1dSRodney W. Grimes 		rp = sotorawcb(so);
5064cc20ab1SSeigo Tanimura 	}
507df8bae1dSRodney W. Grimes 	if (rtm) {
508df8bae1dSRodney W. Grimes 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
50903311056SHajimu UMEMOTO 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
51003311056SHajimu UMEMOTO 			m_freem(m);
51103311056SHajimu UMEMOTO 			m = NULL;
51203311056SHajimu UMEMOTO 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
51303311056SHajimu UMEMOTO 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
514df8bae1dSRodney W. Grimes 		Free(rtm);
515df8bae1dSRodney W. Grimes 	}
516becc44d7SSam Leffler 	if (m) {
517becc44d7SSam Leffler 		if (rp) {
518becc44d7SSam Leffler 			/*
519becc44d7SSam Leffler 			 * XXX insure we don't get a copy by
520becc44d7SSam Leffler 			 * invalidating our protocol
521becc44d7SSam Leffler 			 */
522becc44d7SSam Leffler 			unsigned short family = rp->rcb_proto.sp_family;
523becc44d7SSam Leffler 			rp->rcb_proto.sp_family = 0;
524becc44d7SSam Leffler 			rt_dispatch(m, info.rti_info[RTAX_DST]);
525becc44d7SSam Leffler 			rp->rcb_proto.sp_family = family;
526becc44d7SSam Leffler 		} else
527becc44d7SSam Leffler 			rt_dispatch(m, info.rti_info[RTAX_DST]);
528becc44d7SSam Leffler 	}
529df8bae1dSRodney W. Grimes     }
530df8bae1dSRodney W. Grimes 	return (error);
531becc44d7SSam Leffler #undef	sa_equal
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
53452041295SPoul-Henning Kamp static void
535becc44d7SSam Leffler rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
536df8bae1dSRodney W. Grimes {
537df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e;
538df8bae1dSRodney W. Grimes 	metric(RTV_RPIPE, rmx_recvpipe);
539df8bae1dSRodney W. Grimes 	metric(RTV_SPIPE, rmx_sendpipe);
540df8bae1dSRodney W. Grimes 	metric(RTV_SSTHRESH, rmx_ssthresh);
541df8bae1dSRodney W. Grimes 	metric(RTV_RTT, rmx_rtt);
542df8bae1dSRodney W. Grimes 	metric(RTV_RTTVAR, rmx_rttvar);
543df8bae1dSRodney W. Grimes 	metric(RTV_HOPCOUNT, rmx_hopcount);
544df8bae1dSRodney W. Grimes 	metric(RTV_MTU, rmx_mtu);
545df8bae1dSRodney W. Grimes 	metric(RTV_EXPIRE, rmx_expire);
546df8bae1dSRodney W. Grimes #undef metric
547df8bae1dSRodney W. Grimes }
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes #define ROUNDUP(a) \
550df8bae1dSRodney W. Grimes 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
551076d0761SJulian Elischer 
5527f33a738SJulian Elischer /*
5537f33a738SJulian Elischer  * Extract the addresses of the passed sockaddrs.
5547f33a738SJulian Elischer  * Do a little sanity checking so as to avoid bad memory references.
555076d0761SJulian Elischer  * This data is derived straight from userland.
5567f33a738SJulian Elischer  */
557076d0761SJulian Elischer static int
558becc44d7SSam Leffler rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
559df8bae1dSRodney W. Grimes {
560becc44d7SSam Leffler #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
561df8bae1dSRodney W. Grimes 	register struct sockaddr *sa;
562df8bae1dSRodney W. Grimes 	register int i;
563df8bae1dSRodney W. Grimes 
564becc44d7SSam Leffler 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
565df8bae1dSRodney W. Grimes 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
566df8bae1dSRodney W. Grimes 			continue;
567ff6d0a59SJulian Elischer 		sa = (struct sockaddr *)cp;
5687f33a738SJulian Elischer 		/*
569076d0761SJulian Elischer 		 * It won't fit.
5707f33a738SJulian Elischer 		 */
571becc44d7SSam Leffler 		if (cp + sa->sa_len > cplim)
572076d0761SJulian Elischer 			return (EINVAL);
5737f33a738SJulian Elischer 		/*
5747f33a738SJulian Elischer 		 * there are no more.. quit now
5757f33a738SJulian Elischer 		 * If there are more bits, they are in error.
5767f33a738SJulian Elischer 		 * I've seen this. route(1) can evidently generate these.
5777f33a738SJulian Elischer 		 * This causes kernel to core dump.
578076d0761SJulian Elischer 		 * for compatibility, If we see this, point to a safe address.
5797f33a738SJulian Elischer 		 */
580076d0761SJulian Elischer 		if (sa->sa_len == 0) {
581076d0761SJulian Elischer 			rtinfo->rti_info[i] = &sa_zero;
582076d0761SJulian Elischer 			return (0); /* should be EINVAL but for compat */
583df8bae1dSRodney W. Grimes 		}
584076d0761SJulian Elischer 		/* accept it */
585076d0761SJulian Elischer 		rtinfo->rti_info[i] = sa;
586076d0761SJulian Elischer 		ADVANCE(cp, sa);
587076d0761SJulian Elischer 	}
588076d0761SJulian Elischer 	return (0);
589becc44d7SSam Leffler #undef ADVANCE
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
592df8bae1dSRodney W. Grimes static struct mbuf *
593becc44d7SSam Leffler rt_msg1(int type, struct rt_addrinfo *rtinfo)
594df8bae1dSRodney W. Grimes {
595df8bae1dSRodney W. Grimes 	register struct rt_msghdr *rtm;
596df8bae1dSRodney W. Grimes 	register struct mbuf *m;
597df8bae1dSRodney W. Grimes 	register int i;
598df8bae1dSRodney W. Grimes 	register struct sockaddr *sa;
599df8bae1dSRodney W. Grimes 	int len, dlen;
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes 	switch (type) {
602df8bae1dSRodney W. Grimes 
603df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
604df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
605df8bae1dSRodney W. Grimes 		len = sizeof(struct ifa_msghdr);
606df8bae1dSRodney W. Grimes 		break;
607df8bae1dSRodney W. Grimes 
608477180fbSGarrett Wollman 	case RTM_DELMADDR:
609477180fbSGarrett Wollman 	case RTM_NEWMADDR:
610477180fbSGarrett Wollman 		len = sizeof(struct ifma_msghdr);
611477180fbSGarrett Wollman 		break;
612477180fbSGarrett Wollman 
613df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
614df8bae1dSRodney W. Grimes 		len = sizeof(struct if_msghdr);
615df8bae1dSRodney W. Grimes 		break;
616df8bae1dSRodney W. Grimes 
6177b6edd04SRuslan Ermilov 	case RTM_IFANNOUNCE:
6187b6edd04SRuslan Ermilov 		len = sizeof(struct if_announcemsghdr);
6197b6edd04SRuslan Ermilov 		break;
6207b6edd04SRuslan Ermilov 
621df8bae1dSRodney W. Grimes 	default:
622df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
623df8bae1dSRodney W. Grimes 	}
62433841545SHajimu UMEMOTO 	if (len > MCLBYTES)
625df8bae1dSRodney W. Grimes 		panic("rt_msg1");
626a163d034SWarner Losh 	m = m_gethdr(M_DONTWAIT, MT_DATA);
62733841545SHajimu UMEMOTO 	if (m && len > MHLEN) {
628a163d034SWarner Losh 		MCLGET(m, M_DONTWAIT);
62933841545SHajimu UMEMOTO 		if ((m->m_flags & M_EXT) == 0) {
63033841545SHajimu UMEMOTO 			m_free(m);
63133841545SHajimu UMEMOTO 			m = NULL;
63233841545SHajimu UMEMOTO 		}
63333841545SHajimu UMEMOTO 	}
63433841545SHajimu UMEMOTO 	if (m == 0)
63533841545SHajimu UMEMOTO 		return (m);
636df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len = len;
637df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = 0;
638df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
639df8bae1dSRodney W. Grimes 	bzero((caddr_t)rtm, len);
640df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
641df8bae1dSRodney W. Grimes 		if ((sa = rtinfo->rti_info[i]) == NULL)
642df8bae1dSRodney W. Grimes 			continue;
643df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
644df8bae1dSRodney W. Grimes 		dlen = ROUNDUP(sa->sa_len);
645df8bae1dSRodney W. Grimes 		m_copyback(m, len, dlen, (caddr_t)sa);
646df8bae1dSRodney W. Grimes 		len += dlen;
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len != len) {
649df8bae1dSRodney W. Grimes 		m_freem(m);
650df8bae1dSRodney W. Grimes 		return (NULL);
651df8bae1dSRodney W. Grimes 	}
652df8bae1dSRodney W. Grimes 	rtm->rtm_msglen = len;
653df8bae1dSRodney W. Grimes 	rtm->rtm_version = RTM_VERSION;
654df8bae1dSRodney W. Grimes 	rtm->rtm_type = type;
655df8bae1dSRodney W. Grimes 	return (m);
656df8bae1dSRodney W. Grimes }
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes static int
659becc44d7SSam Leffler rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
660df8bae1dSRodney W. Grimes {
661df8bae1dSRodney W. Grimes 	register int i;
662df8bae1dSRodney W. Grimes 	int len, dlen, second_time = 0;
663df8bae1dSRodney W. Grimes 	caddr_t cp0;
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 	rtinfo->rti_addrs = 0;
666df8bae1dSRodney W. Grimes again:
667df8bae1dSRodney W. Grimes 	switch (type) {
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
670df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
671df8bae1dSRodney W. Grimes 		len = sizeof(struct ifa_msghdr);
672df8bae1dSRodney W. Grimes 		break;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
675df8bae1dSRodney W. Grimes 		len = sizeof(struct if_msghdr);
676df8bae1dSRodney W. Grimes 		break;
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 	default:
679df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
680df8bae1dSRodney W. Grimes 	}
681df440948SPoul-Henning Kamp 	cp0 = cp;
682df440948SPoul-Henning Kamp 	if (cp0)
683df8bae1dSRodney W. Grimes 		cp += len;
684df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
685df8bae1dSRodney W. Grimes 		register struct sockaddr *sa;
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes 		if ((sa = rtinfo->rti_info[i]) == 0)
688df8bae1dSRodney W. Grimes 			continue;
689df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
690df8bae1dSRodney W. Grimes 		dlen = ROUNDUP(sa->sa_len);
691df8bae1dSRodney W. Grimes 		if (cp) {
692df8bae1dSRodney W. Grimes 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
693df8bae1dSRodney W. Grimes 			cp += dlen;
694df8bae1dSRodney W. Grimes 		}
695df8bae1dSRodney W. Grimes 		len += dlen;
696df8bae1dSRodney W. Grimes 	}
697694ff264SAndrew Gallatin 	len = ALIGN(len);
698df8bae1dSRodney W. Grimes 	if (cp == 0 && w != NULL && !second_time) {
699df8bae1dSRodney W. Grimes 		register struct walkarg *rw = w;
700df8bae1dSRodney W. Grimes 
70152041295SPoul-Henning Kamp 		if (rw->w_req) {
702df8bae1dSRodney W. Grimes 			if (rw->w_tmemsize < len) {
703df8bae1dSRodney W. Grimes 				if (rw->w_tmem)
704df8bae1dSRodney W. Grimes 					free(rw->w_tmem, M_RTABLE);
705df440948SPoul-Henning Kamp 				rw->w_tmem = (caddr_t)
706df440948SPoul-Henning Kamp 					malloc(len, M_RTABLE, M_NOWAIT);
707df440948SPoul-Henning Kamp 				if (rw->w_tmem)
708df8bae1dSRodney W. Grimes 					rw->w_tmemsize = len;
709df8bae1dSRodney W. Grimes 			}
710df8bae1dSRodney W. Grimes 			if (rw->w_tmem) {
711df8bae1dSRodney W. Grimes 				cp = rw->w_tmem;
712df8bae1dSRodney W. Grimes 				second_time = 1;
713df8bae1dSRodney W. Grimes 				goto again;
71452041295SPoul-Henning Kamp 			}
715df8bae1dSRodney W. Grimes 		}
716df8bae1dSRodney W. Grimes 	}
717df8bae1dSRodney W. Grimes 	if (cp) {
718df8bae1dSRodney W. Grimes 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 		rtm->rtm_version = RTM_VERSION;
721df8bae1dSRodney W. Grimes 		rtm->rtm_type = type;
722df8bae1dSRodney W. Grimes 		rtm->rtm_msglen = len;
723df8bae1dSRodney W. Grimes 	}
724df8bae1dSRodney W. Grimes 	return (len);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes /*
728df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
729df8bae1dSRodney W. Grimes  * socket indicating that a redirect has occured, a routing lookup
730df8bae1dSRodney W. Grimes  * has failed, or that a protocol has detected timeouts to a particular
731df8bae1dSRodney W. Grimes  * destination.
732df8bae1dSRodney W. Grimes  */
733df8bae1dSRodney W. Grimes void
734becc44d7SSam Leffler rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
735df8bae1dSRodney W. Grimes {
736becc44d7SSam Leffler 	struct rt_msghdr *rtm;
737becc44d7SSam Leffler 	struct mbuf *m;
738df8bae1dSRodney W. Grimes 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
739df8bae1dSRodney W. Grimes 
740df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
741df8bae1dSRodney W. Grimes 		return;
742df8bae1dSRodney W. Grimes 	m = rt_msg1(type, rtinfo);
743df8bae1dSRodney W. Grimes 	if (m == 0)
744df8bae1dSRodney W. Grimes 		return;
745df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
746df8bae1dSRodney W. Grimes 	rtm->rtm_flags = RTF_DONE | flags;
747df8bae1dSRodney W. Grimes 	rtm->rtm_errno = error;
748df8bae1dSRodney W. Grimes 	rtm->rtm_addrs = rtinfo->rti_addrs;
749becc44d7SSam Leffler 	rt_dispatch(m, sa);
750df8bae1dSRodney W. Grimes }
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes /*
753df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
754df8bae1dSRodney W. Grimes  * socket indicating that the status of a network interface has changed.
755df8bae1dSRodney W. Grimes  */
756df8bae1dSRodney W. Grimes void
757becc44d7SSam Leffler rt_ifmsg(struct ifnet *ifp)
758df8bae1dSRodney W. Grimes {
759becc44d7SSam Leffler 	struct if_msghdr *ifm;
760df8bae1dSRodney W. Grimes 	struct mbuf *m;
761df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
764df8bae1dSRodney W. Grimes 		return;
765df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
766df8bae1dSRodney W. Grimes 	m = rt_msg1(RTM_IFINFO, &info);
767df8bae1dSRodney W. Grimes 	if (m == 0)
768df8bae1dSRodney W. Grimes 		return;
769df8bae1dSRodney W. Grimes 	ifm = mtod(m, struct if_msghdr *);
770df8bae1dSRodney W. Grimes 	ifm->ifm_index = ifp->if_index;
77162f76486SMaxim Sobolev 	ifm->ifm_flags = ifp->if_flags;
772df8bae1dSRodney W. Grimes 	ifm->ifm_data = ifp->if_data;
773df8bae1dSRodney W. Grimes 	ifm->ifm_addrs = 0;
774becc44d7SSam Leffler 	rt_dispatch(m, NULL);
775df8bae1dSRodney W. Grimes }
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes /*
778df8bae1dSRodney W. Grimes  * This is called to generate messages from the routing socket
779df8bae1dSRodney W. Grimes  * indicating a network interface has had addresses associated with it.
780df8bae1dSRodney W. Grimes  * if we ever reverse the logic and replace messages TO the routing
781df8bae1dSRodney W. Grimes  * socket indicate a request to configure interfaces, then it will
782df8bae1dSRodney W. Grimes  * be unnecessary as the routing socket will automatically generate
783df8bae1dSRodney W. Grimes  * copies of it.
784df8bae1dSRodney W. Grimes  */
785df8bae1dSRodney W. Grimes void
786becc44d7SSam Leffler rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
787df8bae1dSRodney W. Grimes {
788df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
78926f9a767SRodney W. Grimes 	struct sockaddr *sa = 0;
790df8bae1dSRodney W. Grimes 	int pass;
79126f9a767SRodney W. Grimes 	struct mbuf *m = 0;
792df8bae1dSRodney W. Grimes 	struct ifnet *ifp = ifa->ifa_ifp;
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
795df8bae1dSRodney W. Grimes 		return;
796df8bae1dSRodney W. Grimes 	for (pass = 1; pass < 3; pass++) {
797df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
798df8bae1dSRodney W. Grimes 		if ((cmd == RTM_ADD && pass == 1) ||
799df8bae1dSRodney W. Grimes 		    (cmd == RTM_DELETE && pass == 2)) {
800df8bae1dSRodney W. Grimes 			register struct ifa_msghdr *ifam;
801df8bae1dSRodney W. Grimes 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
802df8bae1dSRodney W. Grimes 
803becc44d7SSam Leffler 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
804becc44d7SSam Leffler 			info.rti_info[RTAX_IFP] = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
805becc44d7SSam Leffler 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
806becc44d7SSam Leffler 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
807df8bae1dSRodney W. Grimes 			if ((m = rt_msg1(ncmd, &info)) == NULL)
808df8bae1dSRodney W. Grimes 				continue;
809df8bae1dSRodney W. Grimes 			ifam = mtod(m, struct ifa_msghdr *);
810df8bae1dSRodney W. Grimes 			ifam->ifam_index = ifp->if_index;
811df8bae1dSRodney W. Grimes 			ifam->ifam_metric = ifa->ifa_metric;
812df8bae1dSRodney W. Grimes 			ifam->ifam_flags = ifa->ifa_flags;
813df8bae1dSRodney W. Grimes 			ifam->ifam_addrs = info.rti_addrs;
814df8bae1dSRodney W. Grimes 		}
815df8bae1dSRodney W. Grimes 		if ((cmd == RTM_ADD && pass == 2) ||
816df8bae1dSRodney W. Grimes 		    (cmd == RTM_DELETE && pass == 1)) {
817df8bae1dSRodney W. Grimes 			register struct rt_msghdr *rtm;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 			if (rt == 0)
820df8bae1dSRodney W. Grimes 				continue;
821becc44d7SSam Leffler 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
822becc44d7SSam Leffler 			info.rti_info[RTAX_DST] = sa = rt_key(rt);
823becc44d7SSam Leffler 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
824df8bae1dSRodney W. Grimes 			if ((m = rt_msg1(cmd, &info)) == NULL)
825df8bae1dSRodney W. Grimes 				continue;
826df8bae1dSRodney W. Grimes 			rtm = mtod(m, struct rt_msghdr *);
827df8bae1dSRodney W. Grimes 			rtm->rtm_index = ifp->if_index;
828df8bae1dSRodney W. Grimes 			rtm->rtm_flags |= rt->rt_flags;
829df8bae1dSRodney W. Grimes 			rtm->rtm_errno = error;
830df8bae1dSRodney W. Grimes 			rtm->rtm_addrs = info.rti_addrs;
831df8bae1dSRodney W. Grimes 		}
832becc44d7SSam Leffler 		rt_dispatch(m, sa);
833df8bae1dSRodney W. Grimes 	}
834df8bae1dSRodney W. Grimes }
835df8bae1dSRodney W. Grimes 
836477180fbSGarrett Wollman /*
837477180fbSGarrett Wollman  * This is the analogue to the rt_newaddrmsg which performs the same
838477180fbSGarrett Wollman  * function but for multicast group memberhips.  This is easier since
839477180fbSGarrett Wollman  * there is no route state to worry about.
840477180fbSGarrett Wollman  */
841477180fbSGarrett Wollman void
842becc44d7SSam Leffler rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
843477180fbSGarrett Wollman {
844477180fbSGarrett Wollman 	struct rt_addrinfo info;
845477180fbSGarrett Wollman 	struct mbuf *m = 0;
846477180fbSGarrett Wollman 	struct ifnet *ifp = ifma->ifma_ifp;
847477180fbSGarrett Wollman 	struct ifma_msghdr *ifmam;
848477180fbSGarrett Wollman 
849477180fbSGarrett Wollman 	if (route_cb.any_count == 0)
850477180fbSGarrett Wollman 		return;
851477180fbSGarrett Wollman 
852477180fbSGarrett Wollman 	bzero((caddr_t)&info, sizeof(info));
853becc44d7SSam Leffler 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
85422f29826SPoul-Henning Kamp 	if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
855becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] =
856becc44d7SSam Leffler 			TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
8578bf72aefSHajimu UMEMOTO 	else
858becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] = NULL;
859477180fbSGarrett Wollman 	/*
860477180fbSGarrett Wollman 	 * If a link-layer address is present, present it as a ``gateway''
861477180fbSGarrett Wollman 	 * (similarly to how ARP entries, e.g., are presented).
862477180fbSGarrett Wollman 	 */
863becc44d7SSam Leffler 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
864becc44d7SSam Leffler 	m = rt_msg1(cmd, &info);
865becc44d7SSam Leffler 	if (m == NULL)
866477180fbSGarrett Wollman 		return;
867477180fbSGarrett Wollman 	ifmam = mtod(m, struct ifma_msghdr *);
868477180fbSGarrett Wollman 	ifmam->ifmam_index = ifp->if_index;
869477180fbSGarrett Wollman 	ifmam->ifmam_addrs = info.rti_addrs;
870becc44d7SSam Leffler 	rt_dispatch(m, ifma->ifma_addr);
871477180fbSGarrett Wollman }
87252041295SPoul-Henning Kamp 
873df8bae1dSRodney W. Grimes /*
8747b6edd04SRuslan Ermilov  * This is called to generate routing socket messages indicating
8757b6edd04SRuslan Ermilov  * network interface arrival and departure.
8767b6edd04SRuslan Ermilov  */
8777b6edd04SRuslan Ermilov void
878becc44d7SSam Leffler rt_ifannouncemsg(struct ifnet *ifp, int what)
8797b6edd04SRuslan Ermilov {
8807b6edd04SRuslan Ermilov 	struct if_announcemsghdr *ifan;
8817b6edd04SRuslan Ermilov 	struct mbuf *m;
8827b6edd04SRuslan Ermilov 	struct rt_addrinfo info;
8837b6edd04SRuslan Ermilov 
8847b6edd04SRuslan Ermilov 	if (route_cb.any_count == 0)
8857b6edd04SRuslan Ermilov 		return;
8867b6edd04SRuslan Ermilov 	bzero((caddr_t)&info, sizeof(info));
8877b6edd04SRuslan Ermilov 	m = rt_msg1(RTM_IFANNOUNCE, &info);
8887b6edd04SRuslan Ermilov 	if (m == NULL)
8897b6edd04SRuslan Ermilov 		return;
8907b6edd04SRuslan Ermilov 	ifan = mtod(m, struct if_announcemsghdr *);
8917b6edd04SRuslan Ermilov 	ifan->ifan_index = ifp->if_index;
8927b6edd04SRuslan Ermilov 	snprintf(ifan->ifan_name, sizeof(ifan->ifan_name),
8937b6edd04SRuslan Ermilov 	    "%s%d", ifp->if_name, ifp->if_unit);
8947b6edd04SRuslan Ermilov 	ifan->ifan_what = what;
895becc44d7SSam Leffler 	rt_dispatch(m, NULL);
896becc44d7SSam Leffler  }
897becc44d7SSam Leffler 
898becc44d7SSam Leffler static void
899becc44d7SSam Leffler rt_dispatch(struct mbuf *m, struct sockaddr *sa)
900becc44d7SSam Leffler {
901becc44d7SSam Leffler 	struct sockproto route_proto;
902becc44d7SSam Leffler 
903becc44d7SSam Leffler 	route_proto.sp_family = PF_ROUTE;
904becc44d7SSam Leffler 	route_proto.sp_protocol = sa ?  sa->sa_family : 0;
9057b6edd04SRuslan Ermilov 	raw_input(m, &route_proto, &route_src, &route_dst);
9067b6edd04SRuslan Ermilov }
9077b6edd04SRuslan Ermilov 
9087b6edd04SRuslan Ermilov /*
909df8bae1dSRodney W. Grimes  * This is used in dumping the kernel table via sysctl().
910df8bae1dSRodney W. Grimes  */
91137c84183SPoul-Henning Kamp static int
912becc44d7SSam Leffler sysctl_dumpentry(struct radix_node *rn, void *vw)
913df8bae1dSRodney W. Grimes {
914becc44d7SSam Leffler 	struct walkarg *w = vw;
915becc44d7SSam Leffler 	struct rtentry *rt = (struct rtentry *)rn;
916df8bae1dSRodney W. Grimes 	int error = 0, size;
917df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
918df8bae1dSRodney W. Grimes 
919df8bae1dSRodney W. Grimes 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
920df8bae1dSRodney W. Grimes 		return 0;
921df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
922becc44d7SSam Leffler 	info.rti_info[RTAX_DST] = rt_key(rt);
923becc44d7SSam Leffler 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
924becc44d7SSam Leffler 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
925becc44d7SSam Leffler 	info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
92628070a0eSRuslan Ermilov 	if (rt->rt_ifp) {
927becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] =
928becc44d7SSam Leffler 			TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
929becc44d7SSam Leffler 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
93028070a0eSRuslan Ermilov 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
931becc44d7SSam Leffler 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
93228070a0eSRuslan Ermilov 	}
933df8bae1dSRodney W. Grimes 	size = rt_msg2(RTM_GET, &info, 0, w);
93452041295SPoul-Henning Kamp 	if (w->w_req && w->w_tmem) {
935becc44d7SSam Leffler 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 		rtm->rtm_flags = rt->rt_flags;
938df8bae1dSRodney W. Grimes 		rtm->rtm_use = rt->rt_use;
939df8bae1dSRodney W. Grimes 		rtm->rtm_rmx = rt->rt_rmx;
940df8bae1dSRodney W. Grimes 		rtm->rtm_index = rt->rt_ifp->if_index;
941df8bae1dSRodney W. Grimes 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
942df8bae1dSRodney W. Grimes 		rtm->rtm_addrs = info.rti_addrs;
94352041295SPoul-Henning Kamp 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
94452041295SPoul-Henning Kamp 		return (error);
945df8bae1dSRodney W. Grimes 	}
946df8bae1dSRodney W. Grimes 	return (error);
947df8bae1dSRodney W. Grimes }
948df8bae1dSRodney W. Grimes 
94937c84183SPoul-Henning Kamp static int
950becc44d7SSam Leffler sysctl_iflist(int af, struct walkarg *w)
951df8bae1dSRodney W. Grimes {
952becc44d7SSam Leffler 	struct ifnet *ifp;
953becc44d7SSam Leffler 	struct ifaddr *ifa;
954df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
955df8bae1dSRodney W. Grimes 	int len, error = 0;
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
958b30a244cSJeffrey Hsu 	/* IFNET_RLOCK(); */		/* could sleep XXX */
959fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
960df8bae1dSRodney W. Grimes 		if (w->w_arg && w->w_arg != ifp->if_index)
961df8bae1dSRodney W. Grimes 			continue;
96222f29826SPoul-Henning Kamp 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
963becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
964df8bae1dSRodney W. Grimes 		len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
965becc44d7SSam Leffler 		info.rti_info[RTAX_IFP] = 0;
96652041295SPoul-Henning Kamp 		if (w->w_req && w->w_tmem) {
967becc44d7SSam Leffler 			struct if_msghdr *ifm;
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes 			ifm = (struct if_msghdr *)w->w_tmem;
970df8bae1dSRodney W. Grimes 			ifm->ifm_index = ifp->if_index;
97162f76486SMaxim Sobolev 			ifm->ifm_flags = ifp->if_flags;
972df8bae1dSRodney W. Grimes 			ifm->ifm_data = ifp->if_data;
973df8bae1dSRodney W. Grimes 			ifm->ifm_addrs = info.rti_addrs;
97452041295SPoul-Henning Kamp 			error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
975df440948SPoul-Henning Kamp 			if (error)
976a35b06c5SJonathan Lemon 				goto done;
977df8bae1dSRodney W. Grimes 		}
97822f29826SPoul-Henning Kamp 		while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
979df8bae1dSRodney W. Grimes 			if (af && af != ifa->ifa_addr->sa_family)
980df8bae1dSRodney W. Grimes 				continue;
981a854ed98SJohn Baldwin 			if (jailed(curthread->td_ucred) &&
982a854ed98SJohn Baldwin 			    prison_if(curthread->td_ucred, ifa->ifa_addr))
98375c13541SPoul-Henning Kamp 				continue;
984becc44d7SSam Leffler 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
985becc44d7SSam Leffler 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
986becc44d7SSam Leffler 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
987df8bae1dSRodney W. Grimes 			len = rt_msg2(RTM_NEWADDR, &info, 0, w);
98852041295SPoul-Henning Kamp 			if (w->w_req && w->w_tmem) {
989becc44d7SSam Leffler 				struct ifa_msghdr *ifam;
990df8bae1dSRodney W. Grimes 
991df8bae1dSRodney W. Grimes 				ifam = (struct ifa_msghdr *)w->w_tmem;
992df8bae1dSRodney W. Grimes 				ifam->ifam_index = ifa->ifa_ifp->if_index;
993df8bae1dSRodney W. Grimes 				ifam->ifam_flags = ifa->ifa_flags;
994df8bae1dSRodney W. Grimes 				ifam->ifam_metric = ifa->ifa_metric;
995df8bae1dSRodney W. Grimes 				ifam->ifam_addrs = info.rti_addrs;
99652041295SPoul-Henning Kamp 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
997df440948SPoul-Henning Kamp 				if (error)
998a35b06c5SJonathan Lemon 					goto done;
999df8bae1dSRodney W. Grimes 			}
1000df8bae1dSRodney W. Grimes 		}
1001becc44d7SSam Leffler 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1002becc44d7SSam Leffler 			info.rti_info[RTAX_BRD] = 0;
1003df8bae1dSRodney W. Grimes 	}
1004a35b06c5SJonathan Lemon done:
1005b30a244cSJeffrey Hsu 	/* IFNET_RUNLOCK(); */ /* XXX */
1006a35b06c5SJonathan Lemon 	return (error);
1007df8bae1dSRodney W. Grimes }
1008df8bae1dSRodney W. Grimes 
100952041295SPoul-Henning Kamp static int
101082d9ae4eSPoul-Henning Kamp sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1011df8bae1dSRodney W. Grimes {
101252041295SPoul-Henning Kamp 	int	*name = (int *)arg1;
101352041295SPoul-Henning Kamp 	u_int	namelen = arg2;
1014becc44d7SSam Leffler 	struct radix_node_head *rnh;
1015df8bae1dSRodney W. Grimes 	int	i, s, error = EINVAL;
1016df8bae1dSRodney W. Grimes 	u_char  af;
1017df8bae1dSRodney W. Grimes 	struct	walkarg w;
1018df8bae1dSRodney W. Grimes 
101952041295SPoul-Henning Kamp 	name ++;
102052041295SPoul-Henning Kamp 	namelen--;
102152041295SPoul-Henning Kamp 	if (req->newptr)
1022df8bae1dSRodney W. Grimes 		return (EPERM);
1023df8bae1dSRodney W. Grimes 	if (namelen != 3)
1024f7a54d06SCrist J. Clark 		return ((namelen < 3) ? EISDIR : ENOTDIR);
1025df8bae1dSRodney W. Grimes 	af = name[0];
1026b2aaf46eSJeffrey Hsu 	if (af > AF_MAX)
1027b2aaf46eSJeffrey Hsu 		return (EINVAL);
1028df8bae1dSRodney W. Grimes 	Bzero(&w, sizeof(w));
1029df8bae1dSRodney W. Grimes 	w.w_op = name[1];
1030df8bae1dSRodney W. Grimes 	w.w_arg = name[2];
103152041295SPoul-Henning Kamp 	w.w_req = req;
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes 	s = splnet();
1034df8bae1dSRodney W. Grimes 	switch (w.w_op) {
1035df8bae1dSRodney W. Grimes 
1036df8bae1dSRodney W. Grimes 	case NET_RT_DUMP:
1037df8bae1dSRodney W. Grimes 	case NET_RT_FLAGS:
1038956b0b65SJeffrey Hsu 		if (af != 0) {
1039956b0b65SJeffrey Hsu 			if ((rnh = rt_tables[af]) != NULL) {
10407701e15bSJeffrey Hsu 				/* RADIX_NODE_HEAD_LOCK(rnh); */
1041956b0b65SJeffrey Hsu 			    	error = rnh->rnh_walktree(rnh,
10427701e15bSJeffrey Hsu 				    sysctl_dumpentry, &w);/* could sleep XXX */
10437701e15bSJeffrey Hsu 				/* RADIX_NODE_HEAD_UNLOCK(rnh); */
1044956b0b65SJeffrey Hsu 			} else
1045956b0b65SJeffrey Hsu 				error = EAFNOSUPPORT;
1046956b0b65SJeffrey Hsu 		} else {
1047df8bae1dSRodney W. Grimes 			for (i = 1; i <= AF_MAX; i++)
1048956b0b65SJeffrey Hsu 				if ((rnh = rt_tables[i]) != NULL) {
10497701e15bSJeffrey Hsu 					/* RADIX_NODE_HEAD_LOCK(rnh); */
1050956b0b65SJeffrey Hsu 					error = rnh->rnh_walktree(rnh,
1051956b0b65SJeffrey Hsu 					    sysctl_dumpentry, &w);
10527701e15bSJeffrey Hsu 					/* RADIX_NODE_HEAD_UNLOCK(rnh); */
1053956b0b65SJeffrey Hsu 					if (error)
1054df8bae1dSRodney W. Grimes 						break;
1055956b0b65SJeffrey Hsu 				}
1056956b0b65SJeffrey Hsu 		}
1057df8bae1dSRodney W. Grimes 		break;
1058df8bae1dSRodney W. Grimes 
1059df8bae1dSRodney W. Grimes 	case NET_RT_IFLIST:
1060df8bae1dSRodney W. Grimes 		error = sysctl_iflist(af, &w);
1061df8bae1dSRodney W. Grimes 	}
1062df8bae1dSRodney W. Grimes 	splx(s);
1063df8bae1dSRodney W. Grimes 	if (w.w_tmem)
1064df8bae1dSRodney W. Grimes 		free(w.w_tmem, M_RTABLE);
1065df8bae1dSRodney W. Grimes 	return (error);
1066df8bae1dSRodney W. Grimes }
1067df8bae1dSRodney W. Grimes 
106852041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
106952041295SPoul-Henning Kamp 
1070df8bae1dSRodney W. Grimes /*
1071df8bae1dSRodney W. Grimes  * Definitions of protocols supported in the ROUTE domain.
1072df8bae1dSRodney W. Grimes  */
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes extern struct domain routedomain;		/* or at least forward */
1075df8bae1dSRodney W. Grimes 
107652041295SPoul-Henning Kamp static struct protosw routesw[] = {
1077df8bae1dSRodney W. Grimes { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
10786ddbf1e2SGary Palmer   0,		route_output,	raw_ctlinput,	0,
1079a29f300eSGarrett Wollman   0,
1080a29f300eSGarrett Wollman   raw_init,	0,		0,		0,
1081a29f300eSGarrett Wollman   &route_usrreqs
1082df8bae1dSRodney W. Grimes }
1083df8bae1dSRodney W. Grimes };
1084df8bae1dSRodney W. Grimes 
108552041295SPoul-Henning Kamp static struct domain routedomain =
1086cb64988fSLuoqi Chen     { PF_ROUTE, "route", 0, 0, 0,
1087df8bae1dSRodney W. Grimes       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
108878a82810SGarrett Wollman 
1089748e0b0aSGarrett Wollman DOMAIN_SET(route);
1090