xref: /freebsd/sys/net/rtsock.c (revision a29f300e809cf6c0167aa2518e33d076f62db72f)
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  *
3378a82810SGarrett Wollman  *	@(#)rtsock.c	8.5 (Berkeley) 11/2/94
34a29f300eSGarrett Wollman  *	$Id: rtsock.c,v 1.26 1997/02/22 09:41:15 peter Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37a29f300eSGarrett Wollman 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
392ee45d7dSDavid Greenman #include <sys/queue.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41748e0b0aSGarrett Wollman #include <sys/kernel.h>
4252041295SPoul-Henning Kamp #include <sys/sysctl.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
47df8bae1dSRodney W. Grimes #include <sys/domain.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52df8bae1dSRodney W. Grimes #include <net/raw_cb.h>
53df8bae1dSRodney W. Grimes 
5452041295SPoul-Henning Kamp static struct	sockaddr route_dst = { 2, PF_ROUTE, };
5552041295SPoul-Henning Kamp static struct	sockaddr route_src = { 2, PF_ROUTE, };
5652041295SPoul-Henning Kamp static struct	sockproto route_proto = { PF_ROUTE, };
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes struct walkarg {
5952041295SPoul-Henning Kamp 	int	w_tmemsize;
6052041295SPoul-Henning Kamp 	int	w_op, w_arg;
6152041295SPoul-Henning Kamp 	caddr_t	w_tmem;
6252041295SPoul-Henning Kamp 	struct sysctl_req *w_req;
63df8bae1dSRodney W. Grimes };
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes static struct mbuf *
66df8bae1dSRodney W. Grimes 		rt_msg1 __P((int, struct rt_addrinfo *));
67df8bae1dSRodney W. Grimes static int	rt_msg2 __P((int,
68df8bae1dSRodney W. Grimes 		    struct rt_addrinfo *, caddr_t, struct walkarg *));
69df8bae1dSRodney W. Grimes static void	rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
7052041295SPoul-Henning Kamp static int	sysctl_dumpentry __P((struct radix_node *rn, void *vw));
7152041295SPoul-Henning Kamp static int	sysctl_iflist __P((int af, struct walkarg *w));
7252041295SPoul-Henning Kamp static int	 route_output __P((struct mbuf *, struct socket *));
7352041295SPoul-Henning Kamp static void	 rt_setmetrics __P((u_long, struct rt_metrics *, struct rt_metrics *));
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes /* Sleazy use of local variables throughout file, warning!!!! */
76df8bae1dSRodney W. Grimes #define dst	info.rti_info[RTAX_DST]
77df8bae1dSRodney W. Grimes #define gate	info.rti_info[RTAX_GATEWAY]
78df8bae1dSRodney W. Grimes #define netmask	info.rti_info[RTAX_NETMASK]
79df8bae1dSRodney W. Grimes #define genmask	info.rti_info[RTAX_GENMASK]
80df8bae1dSRodney W. Grimes #define ifpaddr	info.rti_info[RTAX_IFP]
81df8bae1dSRodney W. Grimes #define ifaaddr	info.rti_info[RTAX_IFA]
82df8bae1dSRodney W. Grimes #define brdaddr	info.rti_info[RTAX_BRD]
83df8bae1dSRodney W. Grimes 
84a29f300eSGarrett Wollman /*
85a29f300eSGarrett Wollman  * It really doesn't make any sense at all for this code to share much
86a29f300eSGarrett Wollman  * with raw_usrreq.c, since its functionality is so restricted.  XXX
87a29f300eSGarrett Wollman  */
8852041295SPoul-Henning Kamp static int
89a29f300eSGarrett Wollman rts_abort(struct socket *so)
90df8bae1dSRodney W. Grimes {
91a29f300eSGarrett Wollman 	int s, error;
92df8bae1dSRodney W. Grimes 	s = splnet();
93a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_abort(so);
94df8bae1dSRodney W. Grimes 	splx(s);
95a29f300eSGarrett Wollman 	return error;
96df8bae1dSRodney W. Grimes }
97a29f300eSGarrett Wollman 
98a29f300eSGarrett Wollman /* pru_accept is EOPNOTSUPP */
99a29f300eSGarrett Wollman 
100a29f300eSGarrett Wollman static int
101a29f300eSGarrett Wollman rts_attach(struct socket *so, int proto, struct proc *p)
102a29f300eSGarrett Wollman {
103a29f300eSGarrett Wollman 	struct rawcb *rp;
104a29f300eSGarrett Wollman 	int s, error;
105a29f300eSGarrett Wollman 
106a29f300eSGarrett Wollman 	if (sotorawcb(so) != 0)
107a29f300eSGarrett Wollman 		return EISCONN;	/* XXX panic? */
108a29f300eSGarrett Wollman 	MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK); /* XXX */
109a29f300eSGarrett Wollman 	if (rp == 0)
110a29f300eSGarrett Wollman 		return ENOBUFS;
111a29f300eSGarrett Wollman 	bzero(rp, sizeof *rp);
112a29f300eSGarrett Wollman 
113a29f300eSGarrett Wollman 	/*
114a29f300eSGarrett Wollman 	 * The splnet() is necessary to block protocols from sending
115a29f300eSGarrett Wollman 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
116a29f300eSGarrett Wollman 	 * this PCB is extant but incompletely initialized.
117a29f300eSGarrett Wollman 	 * Probably we should try to do more of this work beforehand and
118a29f300eSGarrett Wollman 	 * eliminate the spl.
119a29f300eSGarrett Wollman 	 */
120a29f300eSGarrett Wollman 	s = splnet();
121a29f300eSGarrett Wollman 	so->so_pcb = (caddr_t)rp;
122a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_attach(so, proto, p);
123a29f300eSGarrett Wollman 	rp = sotorawcb(so);
124a29f300eSGarrett Wollman 	if (error) {
125a29f300eSGarrett Wollman 		splx(s);
126a29f300eSGarrett Wollman 		free(rp, M_PCB);
127a29f300eSGarrett Wollman 		return error;
128a29f300eSGarrett Wollman 	}
129a29f300eSGarrett Wollman 	switch(rp->rcb_proto.sp_protocol) {
130a29f300eSGarrett Wollman 	case AF_INET:
131df8bae1dSRodney W. Grimes 		route_cb.ip_count++;
132a29f300eSGarrett Wollman 		break;
133a29f300eSGarrett Wollman 	case AF_IPX:
134cc6a66f2SJulian Elischer 		route_cb.ipx_count++;
135a29f300eSGarrett Wollman 		break;
136a29f300eSGarrett Wollman 	case AF_NS:
137df8bae1dSRodney W. Grimes 		route_cb.ns_count++;
138a29f300eSGarrett Wollman 		break;
139a29f300eSGarrett Wollman 	case AF_ISO:
140df8bae1dSRodney W. Grimes 		route_cb.iso_count++;
141a29f300eSGarrett Wollman 		break;
142a29f300eSGarrett Wollman 	}
143df8bae1dSRodney W. Grimes 	rp->rcb_faddr = &route_src;
144df8bae1dSRodney W. Grimes 	route_cb.any_count++;
145df8bae1dSRodney W. Grimes 	soisconnected(so);
146df8bae1dSRodney W. Grimes 	so->so_options |= SO_USELOOPBACK;
147df8bae1dSRodney W. Grimes 	splx(s);
148a29f300eSGarrett Wollman 	return 0;
149df8bae1dSRodney W. Grimes }
150df8bae1dSRodney W. Grimes 
151a29f300eSGarrett Wollman static int
152a29f300eSGarrett Wollman rts_bind(struct socket *so, struct mbuf *nam, struct proc *p)
153a29f300eSGarrett Wollman {
154a29f300eSGarrett Wollman 	int s, error;
155a29f300eSGarrett Wollman 	s = splnet();
156a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
157a29f300eSGarrett Wollman 	splx(s);
158a29f300eSGarrett Wollman 	return error;
159a29f300eSGarrett Wollman }
160a29f300eSGarrett Wollman 
161a29f300eSGarrett Wollman static int
162a29f300eSGarrett Wollman rts_connect(struct socket *so, struct mbuf *nam, struct proc *p)
163a29f300eSGarrett Wollman {
164a29f300eSGarrett Wollman 	int s, error;
165a29f300eSGarrett Wollman 	s = splnet();
166a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
167a29f300eSGarrett Wollman 	splx(s);
168a29f300eSGarrett Wollman 	return error;
169a29f300eSGarrett Wollman }
170a29f300eSGarrett Wollman 
171a29f300eSGarrett Wollman /* pru_connect2 is EOPNOTSUPP */
172a29f300eSGarrett Wollman /* pru_control is EOPNOTSUPP */
173a29f300eSGarrett Wollman 
174a29f300eSGarrett Wollman static int
175a29f300eSGarrett Wollman rts_detach(struct socket *so)
176a29f300eSGarrett Wollman {
177a29f300eSGarrett Wollman 	struct rawcb *rp = sotorawcb(so);
178a29f300eSGarrett Wollman 	int s, error;
179a29f300eSGarrett Wollman 
180a29f300eSGarrett Wollman 	s = splnet();
181a29f300eSGarrett Wollman 	if (rp != 0) {
182a29f300eSGarrett Wollman 		switch(rp->rcb_proto.sp_protocol) {
183a29f300eSGarrett Wollman 		case AF_INET:
184a29f300eSGarrett Wollman 			route_cb.ip_count--;
185a29f300eSGarrett Wollman 			break;
186a29f300eSGarrett Wollman 		case AF_IPX:
187a29f300eSGarrett Wollman 			route_cb.ipx_count--;
188a29f300eSGarrett Wollman 			break;
189a29f300eSGarrett Wollman 		case AF_NS:
190a29f300eSGarrett Wollman 			route_cb.ns_count--;
191a29f300eSGarrett Wollman 			break;
192a29f300eSGarrett Wollman 		case AF_ISO:
193a29f300eSGarrett Wollman 			route_cb.iso_count--;
194a29f300eSGarrett Wollman 			break;
195a29f300eSGarrett Wollman 		}
196a29f300eSGarrett Wollman 		route_cb.any_count--;
197a29f300eSGarrett Wollman 	}
198a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_detach(so);
199a29f300eSGarrett Wollman 	splx(s);
200a29f300eSGarrett Wollman 	return error;
201a29f300eSGarrett Wollman }
202a29f300eSGarrett Wollman 
203a29f300eSGarrett Wollman static int
204a29f300eSGarrett Wollman rts_disconnect(struct socket *so)
205a29f300eSGarrett Wollman {
206a29f300eSGarrett Wollman 	int s, error;
207a29f300eSGarrett Wollman 	s = splnet();
208a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_disconnect(so);
209a29f300eSGarrett Wollman 	splx(s);
210a29f300eSGarrett Wollman 	return error;
211a29f300eSGarrett Wollman }
212a29f300eSGarrett Wollman 
213a29f300eSGarrett Wollman /* pru_listen is EOPNOTSUPP */
214a29f300eSGarrett Wollman 
215a29f300eSGarrett Wollman static int
216a29f300eSGarrett Wollman rts_peeraddr(struct socket *so, struct mbuf *nam)
217a29f300eSGarrett Wollman {
218a29f300eSGarrett Wollman 	int s, error;
219a29f300eSGarrett Wollman 	s = splnet();
220a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_peeraddr(so, nam);
221a29f300eSGarrett Wollman 	splx(s);
222a29f300eSGarrett Wollman 	return error;
223a29f300eSGarrett Wollman }
224a29f300eSGarrett Wollman 
225a29f300eSGarrett Wollman /* pru_rcvd is EOPNOTSUPP */
226a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
227a29f300eSGarrett Wollman 
228a29f300eSGarrett Wollman static int
229a29f300eSGarrett Wollman rts_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
230a29f300eSGarrett Wollman 	 struct mbuf *control, struct proc *p)
231a29f300eSGarrett Wollman {
232a29f300eSGarrett Wollman 	int s, error;
233a29f300eSGarrett Wollman 	s = splnet();
234a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
235a29f300eSGarrett Wollman 	splx(s);
236a29f300eSGarrett Wollman 	return error;
237a29f300eSGarrett Wollman }
238a29f300eSGarrett Wollman 
239a29f300eSGarrett Wollman /* pru_sense is null */
240a29f300eSGarrett Wollman 
241a29f300eSGarrett Wollman static int
242a29f300eSGarrett Wollman rts_shutdown(struct socket *so)
243a29f300eSGarrett Wollman {
244a29f300eSGarrett Wollman 	int s, error;
245a29f300eSGarrett Wollman 	s = splnet();
246a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_shutdown(so);
247a29f300eSGarrett Wollman 	splx(s);
248a29f300eSGarrett Wollman 	return error;
249a29f300eSGarrett Wollman }
250a29f300eSGarrett Wollman 
251a29f300eSGarrett Wollman static int
252a29f300eSGarrett Wollman rts_sockaddr(struct socket *so, struct mbuf *nam)
253a29f300eSGarrett Wollman {
254a29f300eSGarrett Wollman 	int s, error;
255a29f300eSGarrett Wollman 	s = splnet();
256a29f300eSGarrett Wollman 	error = raw_usrreqs.pru_sockaddr(so, nam);
257a29f300eSGarrett Wollman 	splx(s);
258a29f300eSGarrett Wollman 	return error;
259a29f300eSGarrett Wollman }
260a29f300eSGarrett Wollman 
261a29f300eSGarrett Wollman static struct pr_usrreqs route_usrreqs = {
262a29f300eSGarrett Wollman 	rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
263a29f300eSGarrett Wollman 	pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
264a29f300eSGarrett Wollman 	pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
265a29f300eSGarrett Wollman 	rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
266a29f300eSGarrett Wollman 	sosend, soreceive, soselect
267a29f300eSGarrett Wollman };
268a29f300eSGarrett Wollman 
269df8bae1dSRodney W. Grimes /*ARGSUSED*/
27052041295SPoul-Henning Kamp static int
271df8bae1dSRodney W. Grimes route_output(m, so)
272df8bae1dSRodney W. Grimes 	register struct mbuf *m;
273df8bae1dSRodney W. Grimes 	struct socket *so;
274df8bae1dSRodney W. Grimes {
275df8bae1dSRodney W. Grimes 	register struct rt_msghdr *rtm = 0;
276df8bae1dSRodney W. Grimes 	register struct rtentry *rt = 0;
277df8bae1dSRodney W. Grimes 	struct rtentry *saved_nrt = 0;
27878a82810SGarrett Wollman 	struct radix_node_head *rnh;
279df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
280df8bae1dSRodney W. Grimes 	int len, error = 0;
281df8bae1dSRodney W. Grimes 	struct ifnet *ifp = 0;
282df8bae1dSRodney W. Grimes 	struct ifaddr *ifa = 0;
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes #define senderr(e) { error = e; goto flush;}
285df8bae1dSRodney W. Grimes 	if (m == 0 || ((m->m_len < sizeof(long)) &&
286df8bae1dSRodney W. Grimes 		       (m = m_pullup(m, sizeof(long))) == 0))
287df8bae1dSRodney W. Grimes 		return (ENOBUFS);
288df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
289df8bae1dSRodney W. Grimes 		panic("route_output");
290df8bae1dSRodney W. Grimes 	len = m->m_pkthdr.len;
291df8bae1dSRodney W. Grimes 	if (len < sizeof(*rtm) ||
292df8bae1dSRodney W. Grimes 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
293df8bae1dSRodney W. Grimes 		dst = 0;
294df8bae1dSRodney W. Grimes 		senderr(EINVAL);
295df8bae1dSRodney W. Grimes 	}
296df8bae1dSRodney W. Grimes 	R_Malloc(rtm, struct rt_msghdr *, len);
297df8bae1dSRodney W. Grimes 	if (rtm == 0) {
298df8bae1dSRodney W. Grimes 		dst = 0;
299df8bae1dSRodney W. Grimes 		senderr(ENOBUFS);
300df8bae1dSRodney W. Grimes 	}
301df8bae1dSRodney W. Grimes 	m_copydata(m, 0, len, (caddr_t)rtm);
302df8bae1dSRodney W. Grimes 	if (rtm->rtm_version != RTM_VERSION) {
303df8bae1dSRodney W. Grimes 		dst = 0;
304df8bae1dSRodney W. Grimes 		senderr(EPROTONOSUPPORT);
305df8bae1dSRodney W. Grimes 	}
306df8bae1dSRodney W. Grimes 	rtm->rtm_pid = curproc->p_pid;
307df8bae1dSRodney W. Grimes 	info.rti_addrs = rtm->rtm_addrs;
308df8bae1dSRodney W. Grimes 	rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
309ed07cc7fSGarrett Wollman 	if (dst == 0 || (dst->sa_family >= AF_MAX)
310ed07cc7fSGarrett Wollman 	    || (gate != 0 && (gate->sa_family >= AF_MAX)))
311df8bae1dSRodney W. Grimes 		senderr(EINVAL);
312df8bae1dSRodney W. Grimes 	if (genmask) {
313df8bae1dSRodney W. Grimes 		struct radix_node *t;
31478a82810SGarrett Wollman 		t = rn_addmask((caddr_t)genmask, 0, 1);
315df8bae1dSRodney W. Grimes 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
316df8bae1dSRodney W. Grimes 			genmask = (struct sockaddr *)(t->rn_key);
317df8bae1dSRodney W. Grimes 		else
318df8bae1dSRodney W. Grimes 			senderr(ENOBUFS);
319df8bae1dSRodney W. Grimes 	}
320df8bae1dSRodney W. Grimes 	switch (rtm->rtm_type) {
321df8bae1dSRodney W. Grimes 
322df8bae1dSRodney W. Grimes 	case RTM_ADD:
323df8bae1dSRodney W. Grimes 		if (gate == 0)
324df8bae1dSRodney W. Grimes 			senderr(EINVAL);
325df8bae1dSRodney W. Grimes 		error = rtrequest(RTM_ADD, dst, gate, netmask,
326df8bae1dSRodney W. Grimes 					rtm->rtm_flags, &saved_nrt);
327df8bae1dSRodney W. Grimes 		if (error == 0 && saved_nrt) {
328df8bae1dSRodney W. Grimes 			rt_setmetrics(rtm->rtm_inits,
329df8bae1dSRodney W. Grimes 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
330df8bae1dSRodney W. Grimes 			saved_nrt->rt_refcnt--;
331df8bae1dSRodney W. Grimes 			saved_nrt->rt_genmask = genmask;
332df8bae1dSRodney W. Grimes 		}
333df8bae1dSRodney W. Grimes 		break;
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes 	case RTM_DELETE:
336df8bae1dSRodney W. Grimes 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
33778a82810SGarrett Wollman 				rtm->rtm_flags, &saved_nrt);
33878a82810SGarrett Wollman 		if (error == 0) {
339c9a84156SDavid Greenman 			if ((rt = saved_nrt))
34078a82810SGarrett Wollman 				rt->rt_refcnt++;
34178a82810SGarrett Wollman 			goto report;
34278a82810SGarrett Wollman 		}
343df8bae1dSRodney W. Grimes 		break;
344df8bae1dSRodney W. Grimes 
345df8bae1dSRodney W. Grimes 	case RTM_GET:
346df8bae1dSRodney W. Grimes 	case RTM_CHANGE:
347df8bae1dSRodney W. Grimes 	case RTM_LOCK:
34878a82810SGarrett Wollman 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
34978a82810SGarrett Wollman 			senderr(EAFNOSUPPORT);
35078a82810SGarrett Wollman 		} else if (rt = (struct rtentry *)
35178a82810SGarrett Wollman 				rnh->rnh_lookup(dst, netmask, rnh))
35278a82810SGarrett Wollman 			rt->rt_refcnt++;
35378a82810SGarrett Wollman 		else
354df8bae1dSRodney W. Grimes 			senderr(ESRCH);
355df8bae1dSRodney W. Grimes 		switch(rtm->rtm_type) {
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 		case RTM_GET:
35878a82810SGarrett Wollman 		report:
359df8bae1dSRodney W. Grimes 			dst = rt_key(rt);
360df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
361df8bae1dSRodney W. Grimes 			netmask = rt_mask(rt);
362df8bae1dSRodney W. Grimes 			genmask = rt->rt_genmask;
363df8bae1dSRodney W. Grimes 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
364df440948SPoul-Henning Kamp 				ifp = rt->rt_ifp;
365df440948SPoul-Henning Kamp 				if (ifp) {
36659562606SGarrett Wollman 					ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr;
367df8bae1dSRodney W. Grimes 					ifaaddr = rt->rt_ifa->ifa_addr;
368df8bae1dSRodney W. Grimes 					rtm->rtm_index = ifp->if_index;
369df8bae1dSRodney W. Grimes 				} else {
370df8bae1dSRodney W. Grimes 					ifpaddr = 0;
371df8bae1dSRodney W. Grimes 					ifaaddr = 0;
372df8bae1dSRodney W. Grimes 			    }
373df8bae1dSRodney W. Grimes 			}
37478a82810SGarrett Wollman 			len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
375df8bae1dSRodney W. Grimes 				(struct walkarg *)0);
376df8bae1dSRodney W. Grimes 			if (len > rtm->rtm_msglen) {
377df8bae1dSRodney W. Grimes 				struct rt_msghdr *new_rtm;
378df8bae1dSRodney W. Grimes 				R_Malloc(new_rtm, struct rt_msghdr *, len);
379df8bae1dSRodney W. Grimes 				if (new_rtm == 0)
380df8bae1dSRodney W. Grimes 					senderr(ENOBUFS);
381df8bae1dSRodney W. Grimes 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
382df8bae1dSRodney W. Grimes 				Free(rtm); rtm = new_rtm;
383df8bae1dSRodney W. Grimes 			}
38478a82810SGarrett Wollman 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
385df8bae1dSRodney W. Grimes 				(struct walkarg *)0);
386df8bae1dSRodney W. Grimes 			rtm->rtm_flags = rt->rt_flags;
387df8bae1dSRodney W. Grimes 			rtm->rtm_rmx = rt->rt_rmx;
388df8bae1dSRodney W. Grimes 			rtm->rtm_addrs = info.rti_addrs;
389df8bae1dSRodney W. Grimes 			break;
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 		case RTM_CHANGE:
3921db1fffaSBill Fenner 			if (gate && (error = rt_setgate(rt, rt_key(rt), gate)))
3931db1fffaSBill Fenner 				senderr(error);
3945df72964SGarrett Wollman 
3955df72964SGarrett Wollman 			/*
3965df72964SGarrett Wollman 			 * If they tried to change things but didn't specify
3975df72964SGarrett Wollman 			 * the required gateway, then just use the old one.
3985df72964SGarrett Wollman 			 * This can happen if the user tries to change the
3995df72964SGarrett Wollman 			 * flags on the default route without changing the
4005df72964SGarrett Wollman 			 * default gateway.  Changing flags still doesn't work.
4015df72964SGarrett Wollman 			 */
4025df72964SGarrett Wollman 			if ((rt->rt_flags & RTF_GATEWAY) && !gate)
4035df72964SGarrett Wollman 				gate = rt->rt_gateway;
4045df72964SGarrett Wollman 
405df8bae1dSRodney W. Grimes 			/* new gateway could require new ifaddr, ifp;
406df8bae1dSRodney W. Grimes 			   flags may also be different; ifp may be specified
407df8bae1dSRodney W. Grimes 			   by ll sockaddr when protocol address is ambiguous */
408df8bae1dSRodney W. Grimes 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
4093aa72005SBill Fenner 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
410df8bae1dSRodney W. Grimes 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
411df8bae1dSRodney W. Grimes 							ifp);
412df8bae1dSRodney W. Grimes 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
4133aa72005SBill Fenner 				 (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
4143aa72005SBill Fenner 							rt_key(rt), gate))))
415df8bae1dSRodney W. Grimes 				ifp = ifa->ifa_ifp;
416df8bae1dSRodney W. Grimes 			if (ifa) {
417df8bae1dSRodney W. Grimes 				register struct ifaddr *oifa = rt->rt_ifa;
418df8bae1dSRodney W. Grimes 				if (oifa != ifa) {
419df8bae1dSRodney W. Grimes 				    if (oifa && oifa->ifa_rtrequest)
420df8bae1dSRodney W. Grimes 					oifa->ifa_rtrequest(RTM_DELETE,
421df8bae1dSRodney W. Grimes 								rt, gate);
422df8bae1dSRodney W. Grimes 				    IFAFREE(rt->rt_ifa);
423df8bae1dSRodney W. Grimes 				    rt->rt_ifa = ifa;
424df8bae1dSRodney W. Grimes 				    ifa->ifa_refcnt++;
425df8bae1dSRodney W. Grimes 				    rt->rt_ifp = ifp;
426df8bae1dSRodney W. Grimes 				}
427df8bae1dSRodney W. Grimes 			}
428df8bae1dSRodney W. Grimes 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
429df8bae1dSRodney W. Grimes 					&rt->rt_rmx);
430df8bae1dSRodney W. Grimes 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
431df8bae1dSRodney W. Grimes 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
432df8bae1dSRodney W. Grimes 			if (genmask)
433df8bae1dSRodney W. Grimes 				rt->rt_genmask = genmask;
434df8bae1dSRodney W. Grimes 			/*
435df8bae1dSRodney W. Grimes 			 * Fall into
436df8bae1dSRodney W. Grimes 			 */
437df8bae1dSRodney W. Grimes 		case RTM_LOCK:
438df8bae1dSRodney W. Grimes 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
439df8bae1dSRodney W. Grimes 			rt->rt_rmx.rmx_locks |=
440df8bae1dSRodney W. Grimes 				(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
441df8bae1dSRodney W. Grimes 			break;
442df8bae1dSRodney W. Grimes 		}
443df8bae1dSRodney W. Grimes 		break;
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	default:
446df8bae1dSRodney W. Grimes 		senderr(EOPNOTSUPP);
447df8bae1dSRodney W. Grimes 	}
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes flush:
450df8bae1dSRodney W. Grimes 	if (rtm) {
451df8bae1dSRodney W. Grimes 		if (error)
452df8bae1dSRodney W. Grimes 			rtm->rtm_errno = error;
453df8bae1dSRodney W. Grimes 		else
454df8bae1dSRodney W. Grimes 			rtm->rtm_flags |= RTF_DONE;
455df8bae1dSRodney W. Grimes 	}
456df8bae1dSRodney W. Grimes 	if (rt)
457df8bae1dSRodney W. Grimes 		rtfree(rt);
458df8bae1dSRodney W. Grimes     {
459df8bae1dSRodney W. Grimes 	register struct rawcb *rp = 0;
460df8bae1dSRodney W. Grimes 	/*
461df8bae1dSRodney W. Grimes 	 * Check to see if we don't want our own messages.
462df8bae1dSRodney W. Grimes 	 */
463df8bae1dSRodney W. Grimes 	if ((so->so_options & SO_USELOOPBACK) == 0) {
464df8bae1dSRodney W. Grimes 		if (route_cb.any_count <= 1) {
465df8bae1dSRodney W. Grimes 			if (rtm)
466df8bae1dSRodney W. Grimes 				Free(rtm);
467df8bae1dSRodney W. Grimes 			m_freem(m);
468df8bae1dSRodney W. Grimes 			return (error);
469df8bae1dSRodney W. Grimes 		}
470df8bae1dSRodney W. Grimes 		/* There is another listener, so construct message */
471df8bae1dSRodney W. Grimes 		rp = sotorawcb(so);
472df8bae1dSRodney W. Grimes 	}
473df8bae1dSRodney W. Grimes 	if (rtm) {
474df8bae1dSRodney W. Grimes 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
475df8bae1dSRodney W. Grimes 		Free(rtm);
476df8bae1dSRodney W. Grimes 	}
477df8bae1dSRodney W. Grimes 	if (rp)
478df8bae1dSRodney W. Grimes 		rp->rcb_proto.sp_family = 0; /* Avoid us */
479df8bae1dSRodney W. Grimes 	if (dst)
480df8bae1dSRodney W. Grimes 		route_proto.sp_protocol = dst->sa_family;
481df8bae1dSRodney W. Grimes 	raw_input(m, &route_proto, &route_src, &route_dst);
482df8bae1dSRodney W. Grimes 	if (rp)
483df8bae1dSRodney W. Grimes 		rp->rcb_proto.sp_family = PF_ROUTE;
484df8bae1dSRodney W. Grimes     }
485df8bae1dSRodney W. Grimes 	return (error);
486df8bae1dSRodney W. Grimes }
487df8bae1dSRodney W. Grimes 
48852041295SPoul-Henning Kamp static void
489df8bae1dSRodney W. Grimes rt_setmetrics(which, in, out)
490df8bae1dSRodney W. Grimes 	u_long which;
491df8bae1dSRodney W. Grimes 	register struct rt_metrics *in, *out;
492df8bae1dSRodney W. Grimes {
493df8bae1dSRodney W. Grimes #define metric(f, e) if (which & (f)) out->e = in->e;
494df8bae1dSRodney W. Grimes 	metric(RTV_RPIPE, rmx_recvpipe);
495df8bae1dSRodney W. Grimes 	metric(RTV_SPIPE, rmx_sendpipe);
496df8bae1dSRodney W. Grimes 	metric(RTV_SSTHRESH, rmx_ssthresh);
497df8bae1dSRodney W. Grimes 	metric(RTV_RTT, rmx_rtt);
498df8bae1dSRodney W. Grimes 	metric(RTV_RTTVAR, rmx_rttvar);
499df8bae1dSRodney W. Grimes 	metric(RTV_HOPCOUNT, rmx_hopcount);
500df8bae1dSRodney W. Grimes 	metric(RTV_MTU, rmx_mtu);
501df8bae1dSRodney W. Grimes 	metric(RTV_EXPIRE, rmx_expire);
502df8bae1dSRodney W. Grimes #undef metric
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes #define ROUNDUP(a) \
506df8bae1dSRodney W. Grimes 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
507df8bae1dSRodney W. Grimes #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes static void
510df8bae1dSRodney W. Grimes rt_xaddrs(cp, cplim, rtinfo)
511df8bae1dSRodney W. Grimes 	register caddr_t cp, cplim;
512df8bae1dSRodney W. Grimes 	register struct rt_addrinfo *rtinfo;
513df8bae1dSRodney W. Grimes {
514df8bae1dSRodney W. Grimes 	register struct sockaddr *sa;
515df8bae1dSRodney W. Grimes 	register int i;
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
518df8bae1dSRodney W. Grimes 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
519df8bae1dSRodney W. Grimes 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
520df8bae1dSRodney W. Grimes 			continue;
521df8bae1dSRodney W. Grimes 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
522df8bae1dSRodney W. Grimes 		ADVANCE(cp, sa);
523df8bae1dSRodney W. Grimes 	}
524df8bae1dSRodney W. Grimes }
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes static struct mbuf *
527df8bae1dSRodney W. Grimes rt_msg1(type, rtinfo)
528df8bae1dSRodney W. Grimes 	int type;
529df8bae1dSRodney W. Grimes 	register struct rt_addrinfo *rtinfo;
530df8bae1dSRodney W. Grimes {
531df8bae1dSRodney W. Grimes 	register struct rt_msghdr *rtm;
532df8bae1dSRodney W. Grimes 	register struct mbuf *m;
533df8bae1dSRodney W. Grimes 	register int i;
534df8bae1dSRodney W. Grimes 	register struct sockaddr *sa;
535df8bae1dSRodney W. Grimes 	int len, dlen;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 	m = m_gethdr(M_DONTWAIT, MT_DATA);
538df8bae1dSRodney W. Grimes 	if (m == 0)
539df8bae1dSRodney W. Grimes 		return (m);
540df8bae1dSRodney W. Grimes 	switch (type) {
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
543df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
544df8bae1dSRodney W. Grimes 		len = sizeof(struct ifa_msghdr);
545df8bae1dSRodney W. Grimes 		break;
546df8bae1dSRodney W. Grimes 
547477180fbSGarrett Wollman 	case RTM_DELMADDR:
548477180fbSGarrett Wollman 	case RTM_NEWMADDR:
549477180fbSGarrett Wollman 		len = sizeof(struct ifma_msghdr);
550477180fbSGarrett Wollman 		break;
551477180fbSGarrett Wollman 
552df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
553df8bae1dSRodney W. Grimes 		len = sizeof(struct if_msghdr);
554df8bae1dSRodney W. Grimes 		break;
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	default:
557df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
558df8bae1dSRodney W. Grimes 	}
559df8bae1dSRodney W. Grimes 	if (len > MHLEN)
560df8bae1dSRodney W. Grimes 		panic("rt_msg1");
561df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len = len;
562df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = 0;
563df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
564df8bae1dSRodney W. Grimes 	bzero((caddr_t)rtm, len);
565df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
566df8bae1dSRodney W. Grimes 		if ((sa = rtinfo->rti_info[i]) == NULL)
567df8bae1dSRodney W. Grimes 			continue;
568df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
569df8bae1dSRodney W. Grimes 		dlen = ROUNDUP(sa->sa_len);
570df8bae1dSRodney W. Grimes 		m_copyback(m, len, dlen, (caddr_t)sa);
571df8bae1dSRodney W. Grimes 		len += dlen;
572df8bae1dSRodney W. Grimes 	}
573df8bae1dSRodney W. Grimes 	if (m->m_pkthdr.len != len) {
574df8bae1dSRodney W. Grimes 		m_freem(m);
575df8bae1dSRodney W. Grimes 		return (NULL);
576df8bae1dSRodney W. Grimes 	}
577df8bae1dSRodney W. Grimes 	rtm->rtm_msglen = len;
578df8bae1dSRodney W. Grimes 	rtm->rtm_version = RTM_VERSION;
579df8bae1dSRodney W. Grimes 	rtm->rtm_type = type;
580df8bae1dSRodney W. Grimes 	return (m);
581df8bae1dSRodney W. Grimes }
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes static int
584df8bae1dSRodney W. Grimes rt_msg2(type, rtinfo, cp, w)
585df8bae1dSRodney W. Grimes 	int type;
586df8bae1dSRodney W. Grimes 	register struct rt_addrinfo *rtinfo;
587df8bae1dSRodney W. Grimes 	caddr_t cp;
588df8bae1dSRodney W. Grimes 	struct walkarg *w;
589df8bae1dSRodney W. Grimes {
590df8bae1dSRodney W. Grimes 	register int i;
591df8bae1dSRodney W. Grimes 	int len, dlen, second_time = 0;
592df8bae1dSRodney W. Grimes 	caddr_t cp0;
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 	rtinfo->rti_addrs = 0;
595df8bae1dSRodney W. Grimes again:
596df8bae1dSRodney W. Grimes 	switch (type) {
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes 	case RTM_DELADDR:
599df8bae1dSRodney W. Grimes 	case RTM_NEWADDR:
600df8bae1dSRodney W. Grimes 		len = sizeof(struct ifa_msghdr);
601df8bae1dSRodney W. Grimes 		break;
602df8bae1dSRodney W. Grimes 
603df8bae1dSRodney W. Grimes 	case RTM_IFINFO:
604df8bae1dSRodney W. Grimes 		len = sizeof(struct if_msghdr);
605df8bae1dSRodney W. Grimes 		break;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	default:
608df8bae1dSRodney W. Grimes 		len = sizeof(struct rt_msghdr);
609df8bae1dSRodney W. Grimes 	}
610df440948SPoul-Henning Kamp 	cp0 = cp;
611df440948SPoul-Henning Kamp 	if (cp0)
612df8bae1dSRodney W. Grimes 		cp += len;
613df8bae1dSRodney W. Grimes 	for (i = 0; i < RTAX_MAX; i++) {
614df8bae1dSRodney W. Grimes 		register struct sockaddr *sa;
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes 		if ((sa = rtinfo->rti_info[i]) == 0)
617df8bae1dSRodney W. Grimes 			continue;
618df8bae1dSRodney W. Grimes 		rtinfo->rti_addrs |= (1 << i);
619df8bae1dSRodney W. Grimes 		dlen = ROUNDUP(sa->sa_len);
620df8bae1dSRodney W. Grimes 		if (cp) {
621df8bae1dSRodney W. Grimes 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
622df8bae1dSRodney W. Grimes 			cp += dlen;
623df8bae1dSRodney W. Grimes 		}
624df8bae1dSRodney W. Grimes 		len += dlen;
625df8bae1dSRodney W. Grimes 	}
626df8bae1dSRodney W. Grimes 	if (cp == 0 && w != NULL && !second_time) {
627df8bae1dSRodney W. Grimes 		register struct walkarg *rw = w;
628df8bae1dSRodney W. Grimes 
62952041295SPoul-Henning Kamp 		if (rw->w_req) {
630df8bae1dSRodney W. Grimes 			if (rw->w_tmemsize < len) {
631df8bae1dSRodney W. Grimes 				if (rw->w_tmem)
632df8bae1dSRodney W. Grimes 					free(rw->w_tmem, M_RTABLE);
633df440948SPoul-Henning Kamp 				rw->w_tmem = (caddr_t)
634df440948SPoul-Henning Kamp 					malloc(len, M_RTABLE, M_NOWAIT);
635df440948SPoul-Henning Kamp 				if (rw->w_tmem)
636df8bae1dSRodney W. Grimes 					rw->w_tmemsize = len;
637df8bae1dSRodney W. Grimes 			}
638df8bae1dSRodney W. Grimes 			if (rw->w_tmem) {
639df8bae1dSRodney W. Grimes 				cp = rw->w_tmem;
640df8bae1dSRodney W. Grimes 				second_time = 1;
641df8bae1dSRodney W. Grimes 				goto again;
64252041295SPoul-Henning Kamp 			}
643df8bae1dSRodney W. Grimes 		}
644df8bae1dSRodney W. Grimes 	}
645df8bae1dSRodney W. Grimes 	if (cp) {
646df8bae1dSRodney W. Grimes 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 		rtm->rtm_version = RTM_VERSION;
649df8bae1dSRodney W. Grimes 		rtm->rtm_type = type;
650df8bae1dSRodney W. Grimes 		rtm->rtm_msglen = len;
651df8bae1dSRodney W. Grimes 	}
652df8bae1dSRodney W. Grimes 	return (len);
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes /*
656df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
657df8bae1dSRodney W. Grimes  * socket indicating that a redirect has occured, a routing lookup
658df8bae1dSRodney W. Grimes  * has failed, or that a protocol has detected timeouts to a particular
659df8bae1dSRodney W. Grimes  * destination.
660df8bae1dSRodney W. Grimes  */
661df8bae1dSRodney W. Grimes void
662df8bae1dSRodney W. Grimes rt_missmsg(type, rtinfo, flags, error)
663df8bae1dSRodney W. Grimes 	int type, flags, error;
664df8bae1dSRodney W. Grimes 	register struct rt_addrinfo *rtinfo;
665df8bae1dSRodney W. Grimes {
666df8bae1dSRodney W. Grimes 	register struct rt_msghdr *rtm;
667df8bae1dSRodney W. Grimes 	register struct mbuf *m;
668df8bae1dSRodney W. Grimes 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
671df8bae1dSRodney W. Grimes 		return;
672df8bae1dSRodney W. Grimes 	m = rt_msg1(type, rtinfo);
673df8bae1dSRodney W. Grimes 	if (m == 0)
674df8bae1dSRodney W. Grimes 		return;
675df8bae1dSRodney W. Grimes 	rtm = mtod(m, struct rt_msghdr *);
676df8bae1dSRodney W. Grimes 	rtm->rtm_flags = RTF_DONE | flags;
677df8bae1dSRodney W. Grimes 	rtm->rtm_errno = error;
678df8bae1dSRodney W. Grimes 	rtm->rtm_addrs = rtinfo->rti_addrs;
679df8bae1dSRodney W. Grimes 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
680df8bae1dSRodney W. Grimes 	raw_input(m, &route_proto, &route_src, &route_dst);
681df8bae1dSRodney W. Grimes }
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes /*
684df8bae1dSRodney W. Grimes  * This routine is called to generate a message from the routing
685df8bae1dSRodney W. Grimes  * socket indicating that the status of a network interface has changed.
686df8bae1dSRodney W. Grimes  */
687df8bae1dSRodney W. Grimes void
688df8bae1dSRodney W. Grimes rt_ifmsg(ifp)
689df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
690df8bae1dSRodney W. Grimes {
691df8bae1dSRodney W. Grimes 	register struct if_msghdr *ifm;
692df8bae1dSRodney W. Grimes 	struct mbuf *m;
693df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
696df8bae1dSRodney W. Grimes 		return;
697df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
698df8bae1dSRodney W. Grimes 	m = rt_msg1(RTM_IFINFO, &info);
699df8bae1dSRodney W. Grimes 	if (m == 0)
700df8bae1dSRodney W. Grimes 		return;
701df8bae1dSRodney W. Grimes 	ifm = mtod(m, struct if_msghdr *);
702df8bae1dSRodney W. Grimes 	ifm->ifm_index = ifp->if_index;
7032624cf89SGarrett Wollman 	ifm->ifm_flags = (u_short)ifp->if_flags;
704df8bae1dSRodney W. Grimes 	ifm->ifm_data = ifp->if_data;
705df8bae1dSRodney W. Grimes 	ifm->ifm_addrs = 0;
706df8bae1dSRodney W. Grimes 	route_proto.sp_protocol = 0;
707df8bae1dSRodney W. Grimes 	raw_input(m, &route_proto, &route_src, &route_dst);
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes /*
711df8bae1dSRodney W. Grimes  * This is called to generate messages from the routing socket
712df8bae1dSRodney W. Grimes  * indicating a network interface has had addresses associated with it.
713df8bae1dSRodney W. Grimes  * if we ever reverse the logic and replace messages TO the routing
714df8bae1dSRodney W. Grimes  * socket indicate a request to configure interfaces, then it will
715df8bae1dSRodney W. Grimes  * be unnecessary as the routing socket will automatically generate
716df8bae1dSRodney W. Grimes  * copies of it.
717df8bae1dSRodney W. Grimes  */
718df8bae1dSRodney W. Grimes void
719df8bae1dSRodney W. Grimes rt_newaddrmsg(cmd, ifa, error, rt)
720df8bae1dSRodney W. Grimes 	int cmd, error;
721df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
722df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
723df8bae1dSRodney W. Grimes {
724df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
72526f9a767SRodney W. Grimes 	struct sockaddr *sa = 0;
726df8bae1dSRodney W. Grimes 	int pass;
72726f9a767SRodney W. Grimes 	struct mbuf *m = 0;
728df8bae1dSRodney W. Grimes 	struct ifnet *ifp = ifa->ifa_ifp;
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	if (route_cb.any_count == 0)
731df8bae1dSRodney W. Grimes 		return;
732df8bae1dSRodney W. Grimes 	for (pass = 1; pass < 3; pass++) {
733df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
734df8bae1dSRodney W. Grimes 		if ((cmd == RTM_ADD && pass == 1) ||
735df8bae1dSRodney W. Grimes 		    (cmd == RTM_DELETE && pass == 2)) {
736df8bae1dSRodney W. Grimes 			register struct ifa_msghdr *ifam;
737df8bae1dSRodney W. Grimes 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
738df8bae1dSRodney W. Grimes 
739df8bae1dSRodney W. Grimes 			ifaaddr = sa = ifa->ifa_addr;
74059562606SGarrett Wollman 			ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr;
741df8bae1dSRodney W. Grimes 			netmask = ifa->ifa_netmask;
742df8bae1dSRodney W. Grimes 			brdaddr = ifa->ifa_dstaddr;
743df8bae1dSRodney W. Grimes 			if ((m = rt_msg1(ncmd, &info)) == NULL)
744df8bae1dSRodney W. Grimes 				continue;
745df8bae1dSRodney W. Grimes 			ifam = mtod(m, struct ifa_msghdr *);
746df8bae1dSRodney W. Grimes 			ifam->ifam_index = ifp->if_index;
747df8bae1dSRodney W. Grimes 			ifam->ifam_metric = ifa->ifa_metric;
748df8bae1dSRodney W. Grimes 			ifam->ifam_flags = ifa->ifa_flags;
749df8bae1dSRodney W. Grimes 			ifam->ifam_addrs = info.rti_addrs;
750df8bae1dSRodney W. Grimes 		}
751df8bae1dSRodney W. Grimes 		if ((cmd == RTM_ADD && pass == 2) ||
752df8bae1dSRodney W. Grimes 		    (cmd == RTM_DELETE && pass == 1)) {
753df8bae1dSRodney W. Grimes 			register struct rt_msghdr *rtm;
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 			if (rt == 0)
756df8bae1dSRodney W. Grimes 				continue;
757df8bae1dSRodney W. Grimes 			netmask = rt_mask(rt);
758df8bae1dSRodney W. Grimes 			dst = sa = rt_key(rt);
759df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
760df8bae1dSRodney W. Grimes 			if ((m = rt_msg1(cmd, &info)) == NULL)
761df8bae1dSRodney W. Grimes 				continue;
762df8bae1dSRodney W. Grimes 			rtm = mtod(m, struct rt_msghdr *);
763df8bae1dSRodney W. Grimes 			rtm->rtm_index = ifp->if_index;
764df8bae1dSRodney W. Grimes 			rtm->rtm_flags |= rt->rt_flags;
765df8bae1dSRodney W. Grimes 			rtm->rtm_errno = error;
766df8bae1dSRodney W. Grimes 			rtm->rtm_addrs = info.rti_addrs;
767df8bae1dSRodney W. Grimes 		}
768df8bae1dSRodney W. Grimes 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
769df8bae1dSRodney W. Grimes 		raw_input(m, &route_proto, &route_src, &route_dst);
770df8bae1dSRodney W. Grimes 	}
771df8bae1dSRodney W. Grimes }
772df8bae1dSRodney W. Grimes 
773477180fbSGarrett Wollman /*
774477180fbSGarrett Wollman  * This is the analogue to the rt_newaddrmsg which performs the same
775477180fbSGarrett Wollman  * function but for multicast group memberhips.  This is easier since
776477180fbSGarrett Wollman  * there is no route state to worry about.
777477180fbSGarrett Wollman  */
778477180fbSGarrett Wollman void
779477180fbSGarrett Wollman rt_newmaddrmsg(cmd, ifma)
780477180fbSGarrett Wollman 	int cmd;
781477180fbSGarrett Wollman 	struct ifmultiaddr *ifma;
782477180fbSGarrett Wollman {
783477180fbSGarrett Wollman 	struct rt_addrinfo info;
784477180fbSGarrett Wollman 	struct mbuf *m = 0;
785477180fbSGarrett Wollman 	struct ifnet *ifp = ifma->ifma_ifp;
786477180fbSGarrett Wollman 	struct ifma_msghdr *ifmam;
787477180fbSGarrett Wollman 
788477180fbSGarrett Wollman 	if (route_cb.any_count == 0)
789477180fbSGarrett Wollman 		return;
790477180fbSGarrett Wollman 
791477180fbSGarrett Wollman 	bzero((caddr_t)&info, sizeof(info));
792477180fbSGarrett Wollman 	ifaaddr = ifma->ifma_addr;
793477180fbSGarrett Wollman 	ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr;
794477180fbSGarrett Wollman 	/*
795477180fbSGarrett Wollman 	 * If a link-layer address is present, present it as a ``gateway''
796477180fbSGarrett Wollman 	 * (similarly to how ARP entries, e.g., are presented).
797477180fbSGarrett Wollman 	 */
798477180fbSGarrett Wollman 	gate = ifma->ifma_lladdr;
799477180fbSGarrett Wollman 	if ((m = rt_msg1(cmd, &info)) == NULL)
800477180fbSGarrett Wollman 		return;
801477180fbSGarrett Wollman 	ifmam = mtod(m, struct ifma_msghdr *);
802477180fbSGarrett Wollman 	ifmam->ifmam_index = ifp->if_index;
803477180fbSGarrett Wollman 	ifmam->ifmam_addrs = info.rti_addrs;
804477180fbSGarrett Wollman 	route_proto.sp_protocol = ifma->ifma_addr->sa_family;
805477180fbSGarrett Wollman 	raw_input(m, &route_proto, &route_src, &route_dst);
806477180fbSGarrett Wollman }
80752041295SPoul-Henning Kamp 
808df8bae1dSRodney W. Grimes /*
809df8bae1dSRodney W. Grimes  * This is used in dumping the kernel table via sysctl().
810df8bae1dSRodney W. Grimes  */
811df8bae1dSRodney W. Grimes int
8124ab32c34SBruce Evans sysctl_dumpentry(rn, vw)
813df8bae1dSRodney W. Grimes 	struct radix_node *rn;
8144ab32c34SBruce Evans 	void *vw;
815df8bae1dSRodney W. Grimes {
8164ab32c34SBruce Evans 	register struct walkarg *w = vw;
817df8bae1dSRodney W. Grimes 	register struct rtentry *rt = (struct rtentry *)rn;
818df8bae1dSRodney W. Grimes 	int error = 0, size;
819df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
822df8bae1dSRodney W. Grimes 		return 0;
823df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
824df8bae1dSRodney W. Grimes 	dst = rt_key(rt);
825df8bae1dSRodney W. Grimes 	gate = rt->rt_gateway;
826df8bae1dSRodney W. Grimes 	netmask = rt_mask(rt);
827df8bae1dSRodney W. Grimes 	genmask = rt->rt_genmask;
828df8bae1dSRodney W. Grimes 	size = rt_msg2(RTM_GET, &info, 0, w);
82952041295SPoul-Henning Kamp 	if (w->w_req && w->w_tmem) {
830df8bae1dSRodney W. Grimes 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 		rtm->rtm_flags = rt->rt_flags;
833df8bae1dSRodney W. Grimes 		rtm->rtm_use = rt->rt_use;
834df8bae1dSRodney W. Grimes 		rtm->rtm_rmx = rt->rt_rmx;
835df8bae1dSRodney W. Grimes 		rtm->rtm_index = rt->rt_ifp->if_index;
836df8bae1dSRodney W. Grimes 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
837df8bae1dSRodney W. Grimes 		rtm->rtm_addrs = info.rti_addrs;
83852041295SPoul-Henning Kamp 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
83952041295SPoul-Henning Kamp 		return (error);
840df8bae1dSRodney W. Grimes 	}
841df8bae1dSRodney W. Grimes 	return (error);
842df8bae1dSRodney W. Grimes }
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes int
845df8bae1dSRodney W. Grimes sysctl_iflist(af, w)
846df8bae1dSRodney W. Grimes 	int	af;
847df8bae1dSRodney W. Grimes 	register struct	walkarg *w;
848df8bae1dSRodney W. Grimes {
849df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
850df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
851df8bae1dSRodney W. Grimes 	struct	rt_addrinfo info;
852df8bae1dSRodney W. Grimes 	int	len, error = 0;
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes 	bzero((caddr_t)&info, sizeof(info));
85529412182SGarrett Wollman 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
856df8bae1dSRodney W. Grimes 		if (w->w_arg && w->w_arg != ifp->if_index)
857df8bae1dSRodney W. Grimes 			continue;
85859562606SGarrett Wollman 		ifa = ifp->if_addrhead.tqh_first;
859df8bae1dSRodney W. Grimes 		ifpaddr = ifa->ifa_addr;
860df8bae1dSRodney W. Grimes 		len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
861df8bae1dSRodney W. Grimes 		ifpaddr = 0;
86252041295SPoul-Henning Kamp 		if (w->w_req && w->w_tmem) {
863df8bae1dSRodney W. Grimes 			register struct if_msghdr *ifm;
864df8bae1dSRodney W. Grimes 
865df8bae1dSRodney W. Grimes 			ifm = (struct if_msghdr *)w->w_tmem;
866df8bae1dSRodney W. Grimes 			ifm->ifm_index = ifp->if_index;
8672624cf89SGarrett Wollman 			ifm->ifm_flags = (u_short)ifp->if_flags;
868df8bae1dSRodney W. Grimes 			ifm->ifm_data = ifp->if_data;
869df8bae1dSRodney W. Grimes 			ifm->ifm_addrs = info.rti_addrs;
87052041295SPoul-Henning Kamp 			error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
871df440948SPoul-Henning Kamp 			if (error)
872df8bae1dSRodney W. Grimes 				return (error);
873df8bae1dSRodney W. Grimes 		}
87459562606SGarrett Wollman 		while ((ifa = ifa->ifa_link.tqe_next) != 0) {
875df8bae1dSRodney W. Grimes 			if (af && af != ifa->ifa_addr->sa_family)
876df8bae1dSRodney W. Grimes 				continue;
877df8bae1dSRodney W. Grimes 			ifaaddr = ifa->ifa_addr;
878df8bae1dSRodney W. Grimes 			netmask = ifa->ifa_netmask;
879df8bae1dSRodney W. Grimes 			brdaddr = ifa->ifa_dstaddr;
880df8bae1dSRodney W. Grimes 			len = rt_msg2(RTM_NEWADDR, &info, 0, w);
88152041295SPoul-Henning Kamp 			if (w->w_req && w->w_tmem) {
882df8bae1dSRodney W. Grimes 				register struct ifa_msghdr *ifam;
883df8bae1dSRodney W. Grimes 
884df8bae1dSRodney W. Grimes 				ifam = (struct ifa_msghdr *)w->w_tmem;
885df8bae1dSRodney W. Grimes 				ifam->ifam_index = ifa->ifa_ifp->if_index;
886df8bae1dSRodney W. Grimes 				ifam->ifam_flags = ifa->ifa_flags;
887df8bae1dSRodney W. Grimes 				ifam->ifam_metric = ifa->ifa_metric;
888df8bae1dSRodney W. Grimes 				ifam->ifam_addrs = info.rti_addrs;
88952041295SPoul-Henning Kamp 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
890df440948SPoul-Henning Kamp 				if (error)
891df8bae1dSRodney W. Grimes 					return (error);
892df8bae1dSRodney W. Grimes 			}
893df8bae1dSRodney W. Grimes 		}
894df8bae1dSRodney W. Grimes 		ifaaddr = netmask = brdaddr = 0;
895df8bae1dSRodney W. Grimes 	}
896df8bae1dSRodney W. Grimes 	return (0);
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes 
89952041295SPoul-Henning Kamp static int
90052041295SPoul-Henning Kamp sysctl_rtsock SYSCTL_HANDLER_ARGS
901df8bae1dSRodney W. Grimes {
90252041295SPoul-Henning Kamp 	int	*name = (int *)arg1;
90352041295SPoul-Henning Kamp 	u_int	namelen = arg2;
904df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
905df8bae1dSRodney W. Grimes 	int	i, s, error = EINVAL;
906df8bae1dSRodney W. Grimes 	u_char  af;
907df8bae1dSRodney W. Grimes 	struct	walkarg w;
908df8bae1dSRodney W. Grimes 
90952041295SPoul-Henning Kamp 	name ++;
91052041295SPoul-Henning Kamp 	namelen--;
91152041295SPoul-Henning Kamp 	if (req->newptr)
912df8bae1dSRodney W. Grimes 		return (EPERM);
913df8bae1dSRodney W. Grimes 	if (namelen != 3)
914df8bae1dSRodney W. Grimes 		return (EINVAL);
915df8bae1dSRodney W. Grimes 	af = name[0];
916df8bae1dSRodney W. Grimes 	Bzero(&w, sizeof(w));
917df8bae1dSRodney W. Grimes 	w.w_op = name[1];
918df8bae1dSRodney W. Grimes 	w.w_arg = name[2];
91952041295SPoul-Henning Kamp 	w.w_req = req;
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	s = splnet();
922df8bae1dSRodney W. Grimes 	switch (w.w_op) {
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	case NET_RT_DUMP:
925df8bae1dSRodney W. Grimes 	case NET_RT_FLAGS:
926df8bae1dSRodney W. Grimes 		for (i = 1; i <= AF_MAX; i++)
927df8bae1dSRodney W. Grimes 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
928df8bae1dSRodney W. Grimes 			    (error = rnh->rnh_walktree(rnh,
929df8bae1dSRodney W. Grimes 							sysctl_dumpentry, &w)))
930df8bae1dSRodney W. Grimes 				break;
931df8bae1dSRodney W. Grimes 		break;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	case NET_RT_IFLIST:
934df8bae1dSRodney W. Grimes 		error = sysctl_iflist(af, &w);
935df8bae1dSRodney W. Grimes 	}
936df8bae1dSRodney W. Grimes 	splx(s);
937df8bae1dSRodney W. Grimes 	if (w.w_tmem)
938df8bae1dSRodney W. Grimes 		free(w.w_tmem, M_RTABLE);
939df8bae1dSRodney W. Grimes 	return (error);
940df8bae1dSRodney W. Grimes }
941df8bae1dSRodney W. Grimes 
94252041295SPoul-Henning Kamp SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
94352041295SPoul-Henning Kamp 
944df8bae1dSRodney W. Grimes /*
945df8bae1dSRodney W. Grimes  * Definitions of protocols supported in the ROUTE domain.
946df8bae1dSRodney W. Grimes  */
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes extern struct domain routedomain;		/* or at least forward */
949df8bae1dSRodney W. Grimes 
95052041295SPoul-Henning Kamp static struct protosw routesw[] = {
951df8bae1dSRodney W. Grimes { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
9526ddbf1e2SGary Palmer   0,		route_output,	raw_ctlinput,	0,
953a29f300eSGarrett Wollman   0,
954a29f300eSGarrett Wollman   raw_init,	0,		0,		0,
955a29f300eSGarrett Wollman   &route_usrreqs
956df8bae1dSRodney W. Grimes }
957df8bae1dSRodney W. Grimes };
958df8bae1dSRodney W. Grimes 
95952041295SPoul-Henning Kamp static struct domain routedomain =
960df8bae1dSRodney W. Grimes     { PF_ROUTE, "route", route_init, 0, 0,
961df8bae1dSRodney W. Grimes       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
96278a82810SGarrett Wollman 
963748e0b0aSGarrett Wollman DOMAIN_SET(route);
964