Deleted Added
full compact
rtsock.c (234572) rtsock.c (241686)
1/*-
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
1/*-
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
30 * $FreeBSD: head/sys/net/rtsock.c 234572 2012-04-22 16:13:23Z melifaro $
30 * $FreeBSD: head/sys/net/rtsock.c 241686 2012-10-18 13:57:24Z andre $
31 */
32#include "opt_compat.h"
33#include "opt_sctp.h"
34#include "opt_mpath.h"
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/jail.h>
40#include <sys/kernel.h>
41#include <sys/domain.h>
42#include <sys/lock.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/priv.h>
46#include <sys/proc.h>
47#include <sys/protosw.h>
48#include <sys/rwlock.h>
49#include <sys/signalvar.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/sysctl.h>
53#include <sys/systm.h>
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_llatbl.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/raw_cb.h>
61#include <net/route.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#include <netinet/ip_carp.h>
67#ifdef INET6
68#include <netinet6/scope6_var.h>
69#endif
70
71#if defined(INET) || defined(INET6)
72#ifdef SCTP
73extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
74#endif /* SCTP */
75#endif
76
77#ifdef COMPAT_FREEBSD32
78#include <sys/mount.h>
79#include <compat/freebsd32/freebsd32.h>
80
81struct if_data32 {
82 uint8_t ifi_type;
83 uint8_t ifi_physical;
84 uint8_t ifi_addrlen;
85 uint8_t ifi_hdrlen;
86 uint8_t ifi_link_state;
87 uint8_t ifi_vhid;
88 uint8_t ifi_spare_char2;
89 uint8_t ifi_datalen;
90 uint32_t ifi_mtu;
91 uint32_t ifi_metric;
92 uint32_t ifi_baudrate;
93 uint32_t ifi_ipackets;
94 uint32_t ifi_ierrors;
95 uint32_t ifi_opackets;
96 uint32_t ifi_oerrors;
97 uint32_t ifi_collisions;
98 uint32_t ifi_ibytes;
99 uint32_t ifi_obytes;
100 uint32_t ifi_imcasts;
101 uint32_t ifi_omcasts;
102 uint32_t ifi_iqdrops;
103 uint32_t ifi_noproto;
104 uint32_t ifi_hwassist;
105 int32_t ifi_epoch;
106 struct timeval32 ifi_lastchange;
107};
108
109struct if_msghdr32 {
110 uint16_t ifm_msglen;
111 uint8_t ifm_version;
112 uint8_t ifm_type;
113 int32_t ifm_addrs;
114 int32_t ifm_flags;
115 uint16_t ifm_index;
116 struct if_data32 ifm_data;
117};
118
119struct if_msghdrl32 {
120 uint16_t ifm_msglen;
121 uint8_t ifm_version;
122 uint8_t ifm_type;
123 int32_t ifm_addrs;
124 int32_t ifm_flags;
125 uint16_t ifm_index;
126 uint16_t _ifm_spare1;
127 uint16_t ifm_len;
128 uint16_t ifm_data_off;
129 struct if_data32 ifm_data;
130};
131
132struct ifa_msghdrl32 {
133 uint16_t ifam_msglen;
134 uint8_t ifam_version;
135 uint8_t ifam_type;
136 int32_t ifam_addrs;
137 int32_t ifam_flags;
138 uint16_t ifam_index;
139 uint16_t _ifam_spare1;
140 uint16_t ifam_len;
141 uint16_t ifam_data_off;
142 int32_t ifam_metric;
143 struct if_data32 ifam_data;
144};
145#endif /* COMPAT_FREEBSD32 */
146
147MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
148
149/* NB: these are not modified */
150static struct sockaddr route_src = { 2, PF_ROUTE, };
151static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
152
153/* These are external hooks for CARP. */
154int (*carp_get_vhid_p)(struct ifaddr *);
155
156/*
157 * Used by rtsock/raw_input callback code to decide whether to filter the update
158 * notification to a socket bound to a particular FIB.
159 */
160#define RTS_FILTER_FIB M_PROTO8
161#define RTS_ALLFIBS -1
162
163static struct {
164 int ip_count; /* attached w/ AF_INET */
165 int ip6_count; /* attached w/ AF_INET6 */
166 int ipx_count; /* attached w/ AF_IPX */
167 int any_count; /* total attached */
168} route_cb;
169
170struct mtx rtsock_mtx;
171MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
172
173#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
174#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
175#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
176
177static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
178
179struct walkarg {
180 int w_tmemsize;
181 int w_op, w_arg;
182 caddr_t w_tmem;
183 struct sysctl_req *w_req;
184};
185
186static void rts_input(struct mbuf *m);
187static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
188static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
189 caddr_t cp, struct walkarg *w);
190static int rt_xaddrs(caddr_t cp, caddr_t cplim,
191 struct rt_addrinfo *rtinfo);
192static int sysctl_dumpentry(struct radix_node *rn, void *vw);
193static int sysctl_iflist(int af, struct walkarg *w);
194static int sysctl_ifmalist(int af, struct walkarg *w);
195static int route_output(struct mbuf *m, struct socket *so);
196static void rt_setmetrics(u_long which, const struct rt_metrics *in,
197 struct rt_metrics_lite *out);
198static void rt_getmetrics(const struct rt_metrics_lite *in,
199 struct rt_metrics *out);
200static void rt_dispatch(struct mbuf *, sa_family_t);
201
202static struct netisr_handler rtsock_nh = {
203 .nh_name = "rtsock",
204 .nh_handler = rts_input,
205 .nh_proto = NETISR_ROUTE,
206 .nh_policy = NETISR_POLICY_SOURCE,
207};
208
209static int
210sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
211{
212 int error, qlimit;
213
214 netisr_getqlimit(&rtsock_nh, &qlimit);
215 error = sysctl_handle_int(oidp, &qlimit, 0, req);
216 if (error || !req->newptr)
217 return (error);
218 if (qlimit < 1)
219 return (EINVAL);
220 return (netisr_setqlimit(&rtsock_nh, qlimit));
221}
222SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223 0, 0, sysctl_route_netisr_maxqlen, "I",
224 "maximum routing socket dispatch queue length");
225
226static void
227rts_init(void)
228{
229 int tmp;
230
231 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232 rtsock_nh.nh_qlimit = tmp;
233 netisr_register(&rtsock_nh);
234}
235SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
236
237static int
238raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
239 struct rawcb *rp)
240{
241 int fibnum;
242
243 KASSERT(m != NULL, ("%s: m is NULL", __func__));
244 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
246
247 /* No filtering requested. */
248 if ((m->m_flags & RTS_FILTER_FIB) == 0)
249 return (0);
250
251 /* Check if it is a rts and the fib matches the one of the socket. */
252 fibnum = M_GETFIB(m);
253 if (proto->sp_family != PF_ROUTE ||
254 rp->rcb_socket == NULL ||
255 rp->rcb_socket->so_fibnum == fibnum)
256 return (0);
257
258 /* Filtering requested and no match, the socket shall be skipped. */
259 return (1);
260}
261
262static void
263rts_input(struct mbuf *m)
264{
265 struct sockproto route_proto;
266 unsigned short *family;
267 struct m_tag *tag;
268
269 route_proto.sp_family = PF_ROUTE;
270 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
271 if (tag != NULL) {
272 family = (unsigned short *)(tag + 1);
273 route_proto.sp_protocol = *family;
274 m_tag_delete(m, tag);
275 } else
276 route_proto.sp_protocol = 0;
277
278 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
279}
280
281/*
282 * It really doesn't make any sense at all for this code to share much
283 * with raw_usrreq.c, since its functionality is so restricted. XXX
284 */
285static void
286rts_abort(struct socket *so)
287{
288
289 raw_usrreqs.pru_abort(so);
290}
291
292static void
293rts_close(struct socket *so)
294{
295
296 raw_usrreqs.pru_close(so);
297}
298
299/* pru_accept is EOPNOTSUPP */
300
301static int
302rts_attach(struct socket *so, int proto, struct thread *td)
303{
304 struct rawcb *rp;
31 */
32#include "opt_compat.h"
33#include "opt_sctp.h"
34#include "opt_mpath.h"
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/jail.h>
40#include <sys/kernel.h>
41#include <sys/domain.h>
42#include <sys/lock.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/priv.h>
46#include <sys/proc.h>
47#include <sys/protosw.h>
48#include <sys/rwlock.h>
49#include <sys/signalvar.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/sysctl.h>
53#include <sys/systm.h>
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_llatbl.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/raw_cb.h>
61#include <net/route.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#include <netinet/ip_carp.h>
67#ifdef INET6
68#include <netinet6/scope6_var.h>
69#endif
70
71#if defined(INET) || defined(INET6)
72#ifdef SCTP
73extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
74#endif /* SCTP */
75#endif
76
77#ifdef COMPAT_FREEBSD32
78#include <sys/mount.h>
79#include <compat/freebsd32/freebsd32.h>
80
81struct if_data32 {
82 uint8_t ifi_type;
83 uint8_t ifi_physical;
84 uint8_t ifi_addrlen;
85 uint8_t ifi_hdrlen;
86 uint8_t ifi_link_state;
87 uint8_t ifi_vhid;
88 uint8_t ifi_spare_char2;
89 uint8_t ifi_datalen;
90 uint32_t ifi_mtu;
91 uint32_t ifi_metric;
92 uint32_t ifi_baudrate;
93 uint32_t ifi_ipackets;
94 uint32_t ifi_ierrors;
95 uint32_t ifi_opackets;
96 uint32_t ifi_oerrors;
97 uint32_t ifi_collisions;
98 uint32_t ifi_ibytes;
99 uint32_t ifi_obytes;
100 uint32_t ifi_imcasts;
101 uint32_t ifi_omcasts;
102 uint32_t ifi_iqdrops;
103 uint32_t ifi_noproto;
104 uint32_t ifi_hwassist;
105 int32_t ifi_epoch;
106 struct timeval32 ifi_lastchange;
107};
108
109struct if_msghdr32 {
110 uint16_t ifm_msglen;
111 uint8_t ifm_version;
112 uint8_t ifm_type;
113 int32_t ifm_addrs;
114 int32_t ifm_flags;
115 uint16_t ifm_index;
116 struct if_data32 ifm_data;
117};
118
119struct if_msghdrl32 {
120 uint16_t ifm_msglen;
121 uint8_t ifm_version;
122 uint8_t ifm_type;
123 int32_t ifm_addrs;
124 int32_t ifm_flags;
125 uint16_t ifm_index;
126 uint16_t _ifm_spare1;
127 uint16_t ifm_len;
128 uint16_t ifm_data_off;
129 struct if_data32 ifm_data;
130};
131
132struct ifa_msghdrl32 {
133 uint16_t ifam_msglen;
134 uint8_t ifam_version;
135 uint8_t ifam_type;
136 int32_t ifam_addrs;
137 int32_t ifam_flags;
138 uint16_t ifam_index;
139 uint16_t _ifam_spare1;
140 uint16_t ifam_len;
141 uint16_t ifam_data_off;
142 int32_t ifam_metric;
143 struct if_data32 ifam_data;
144};
145#endif /* COMPAT_FREEBSD32 */
146
147MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
148
149/* NB: these are not modified */
150static struct sockaddr route_src = { 2, PF_ROUTE, };
151static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
152
153/* These are external hooks for CARP. */
154int (*carp_get_vhid_p)(struct ifaddr *);
155
156/*
157 * Used by rtsock/raw_input callback code to decide whether to filter the update
158 * notification to a socket bound to a particular FIB.
159 */
160#define RTS_FILTER_FIB M_PROTO8
161#define RTS_ALLFIBS -1
162
163static struct {
164 int ip_count; /* attached w/ AF_INET */
165 int ip6_count; /* attached w/ AF_INET6 */
166 int ipx_count; /* attached w/ AF_IPX */
167 int any_count; /* total attached */
168} route_cb;
169
170struct mtx rtsock_mtx;
171MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
172
173#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
174#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
175#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
176
177static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
178
179struct walkarg {
180 int w_tmemsize;
181 int w_op, w_arg;
182 caddr_t w_tmem;
183 struct sysctl_req *w_req;
184};
185
186static void rts_input(struct mbuf *m);
187static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
188static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
189 caddr_t cp, struct walkarg *w);
190static int rt_xaddrs(caddr_t cp, caddr_t cplim,
191 struct rt_addrinfo *rtinfo);
192static int sysctl_dumpentry(struct radix_node *rn, void *vw);
193static int sysctl_iflist(int af, struct walkarg *w);
194static int sysctl_ifmalist(int af, struct walkarg *w);
195static int route_output(struct mbuf *m, struct socket *so);
196static void rt_setmetrics(u_long which, const struct rt_metrics *in,
197 struct rt_metrics_lite *out);
198static void rt_getmetrics(const struct rt_metrics_lite *in,
199 struct rt_metrics *out);
200static void rt_dispatch(struct mbuf *, sa_family_t);
201
202static struct netisr_handler rtsock_nh = {
203 .nh_name = "rtsock",
204 .nh_handler = rts_input,
205 .nh_proto = NETISR_ROUTE,
206 .nh_policy = NETISR_POLICY_SOURCE,
207};
208
209static int
210sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
211{
212 int error, qlimit;
213
214 netisr_getqlimit(&rtsock_nh, &qlimit);
215 error = sysctl_handle_int(oidp, &qlimit, 0, req);
216 if (error || !req->newptr)
217 return (error);
218 if (qlimit < 1)
219 return (EINVAL);
220 return (netisr_setqlimit(&rtsock_nh, qlimit));
221}
222SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223 0, 0, sysctl_route_netisr_maxqlen, "I",
224 "maximum routing socket dispatch queue length");
225
226static void
227rts_init(void)
228{
229 int tmp;
230
231 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232 rtsock_nh.nh_qlimit = tmp;
233 netisr_register(&rtsock_nh);
234}
235SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
236
237static int
238raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
239 struct rawcb *rp)
240{
241 int fibnum;
242
243 KASSERT(m != NULL, ("%s: m is NULL", __func__));
244 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
246
247 /* No filtering requested. */
248 if ((m->m_flags & RTS_FILTER_FIB) == 0)
249 return (0);
250
251 /* Check if it is a rts and the fib matches the one of the socket. */
252 fibnum = M_GETFIB(m);
253 if (proto->sp_family != PF_ROUTE ||
254 rp->rcb_socket == NULL ||
255 rp->rcb_socket->so_fibnum == fibnum)
256 return (0);
257
258 /* Filtering requested and no match, the socket shall be skipped. */
259 return (1);
260}
261
262static void
263rts_input(struct mbuf *m)
264{
265 struct sockproto route_proto;
266 unsigned short *family;
267 struct m_tag *tag;
268
269 route_proto.sp_family = PF_ROUTE;
270 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
271 if (tag != NULL) {
272 family = (unsigned short *)(tag + 1);
273 route_proto.sp_protocol = *family;
274 m_tag_delete(m, tag);
275 } else
276 route_proto.sp_protocol = 0;
277
278 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
279}
280
281/*
282 * It really doesn't make any sense at all for this code to share much
283 * with raw_usrreq.c, since its functionality is so restricted. XXX
284 */
285static void
286rts_abort(struct socket *so)
287{
288
289 raw_usrreqs.pru_abort(so);
290}
291
292static void
293rts_close(struct socket *so)
294{
295
296 raw_usrreqs.pru_close(so);
297}
298
299/* pru_accept is EOPNOTSUPP */
300
301static int
302rts_attach(struct socket *so, int proto, struct thread *td)
303{
304 struct rawcb *rp;
305 int s, error;
305 int error;
306
307 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
308
309 /* XXX */
310 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
311 if (rp == NULL)
312 return ENOBUFS;
313
306
307 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
308
309 /* XXX */
310 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
311 if (rp == NULL)
312 return ENOBUFS;
313
314 /*
315 * The splnet() is necessary to block protocols from sending
316 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
317 * this PCB is extant but incompletely initialized.
318 * Probably we should try to do more of this work beforehand and
319 * eliminate the spl.
320 */
321 s = splnet();
322 so->so_pcb = (caddr_t)rp;
323 so->so_fibnum = td->td_proc->p_fibnum;
324 error = raw_attach(so, proto);
325 rp = sotorawcb(so);
326 if (error) {
314 so->so_pcb = (caddr_t)rp;
315 so->so_fibnum = td->td_proc->p_fibnum;
316 error = raw_attach(so, proto);
317 rp = sotorawcb(so);
318 if (error) {
327 splx(s);
328 so->so_pcb = NULL;
329 free(rp, M_PCB);
330 return error;
331 }
332 RTSOCK_LOCK();
333 switch(rp->rcb_proto.sp_protocol) {
334 case AF_INET:
335 route_cb.ip_count++;
336 break;
337 case AF_INET6:
338 route_cb.ip6_count++;
339 break;
340 case AF_IPX:
341 route_cb.ipx_count++;
342 break;
343 }
344 route_cb.any_count++;
345 RTSOCK_UNLOCK();
346 soisconnected(so);
347 so->so_options |= SO_USELOOPBACK;
319 so->so_pcb = NULL;
320 free(rp, M_PCB);
321 return error;
322 }
323 RTSOCK_LOCK();
324 switch(rp->rcb_proto.sp_protocol) {
325 case AF_INET:
326 route_cb.ip_count++;
327 break;
328 case AF_INET6:
329 route_cb.ip6_count++;
330 break;
331 case AF_IPX:
332 route_cb.ipx_count++;
333 break;
334 }
335 route_cb.any_count++;
336 RTSOCK_UNLOCK();
337 soisconnected(so);
338 so->so_options |= SO_USELOOPBACK;
348 splx(s);
349 return 0;
350}
351
352static int
353rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
354{
355
356 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
357}
358
359static int
360rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
361{
362
363 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
364}
365
366/* pru_connect2 is EOPNOTSUPP */
367/* pru_control is EOPNOTSUPP */
368
369static void
370rts_detach(struct socket *so)
371{
372 struct rawcb *rp = sotorawcb(so);
373
374 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
375
376 RTSOCK_LOCK();
377 switch(rp->rcb_proto.sp_protocol) {
378 case AF_INET:
379 route_cb.ip_count--;
380 break;
381 case AF_INET6:
382 route_cb.ip6_count--;
383 break;
384 case AF_IPX:
385 route_cb.ipx_count--;
386 break;
387 }
388 route_cb.any_count--;
389 RTSOCK_UNLOCK();
390 raw_usrreqs.pru_detach(so);
391}
392
393static int
394rts_disconnect(struct socket *so)
395{
396
397 return (raw_usrreqs.pru_disconnect(so));
398}
399
400/* pru_listen is EOPNOTSUPP */
401
402static int
403rts_peeraddr(struct socket *so, struct sockaddr **nam)
404{
405
406 return (raw_usrreqs.pru_peeraddr(so, nam));
407}
408
409/* pru_rcvd is EOPNOTSUPP */
410/* pru_rcvoob is EOPNOTSUPP */
411
412static int
413rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
414 struct mbuf *control, struct thread *td)
415{
416
417 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
418}
419
420/* pru_sense is null */
421
422static int
423rts_shutdown(struct socket *so)
424{
425
426 return (raw_usrreqs.pru_shutdown(so));
427}
428
429static int
430rts_sockaddr(struct socket *so, struct sockaddr **nam)
431{
432
433 return (raw_usrreqs.pru_sockaddr(so, nam));
434}
435
436static struct pr_usrreqs route_usrreqs = {
437 .pru_abort = rts_abort,
438 .pru_attach = rts_attach,
439 .pru_bind = rts_bind,
440 .pru_connect = rts_connect,
441 .pru_detach = rts_detach,
442 .pru_disconnect = rts_disconnect,
443 .pru_peeraddr = rts_peeraddr,
444 .pru_send = rts_send,
445 .pru_shutdown = rts_shutdown,
446 .pru_sockaddr = rts_sockaddr,
447 .pru_close = rts_close,
448};
449
450#ifndef _SOCKADDR_UNION_DEFINED
451#define _SOCKADDR_UNION_DEFINED
452/*
453 * The union of all possible address formats we handle.
454 */
455union sockaddr_union {
456 struct sockaddr sa;
457 struct sockaddr_in sin;
458 struct sockaddr_in6 sin6;
459};
460#endif /* _SOCKADDR_UNION_DEFINED */
461
462static int
463rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
464 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
465{
466
467 /* First, see if the returned address is part of the jail. */
468 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
469 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
470 return (0);
471 }
472
473 switch (info->rti_info[RTAX_DST]->sa_family) {
474#ifdef INET
475 case AF_INET:
476 {
477 struct in_addr ia;
478 struct ifaddr *ifa;
479 int found;
480
481 found = 0;
482 /*
483 * Try to find an address on the given outgoing interface
484 * that belongs to the jail.
485 */
486 IF_ADDR_RLOCK(ifp);
487 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
488 struct sockaddr *sa;
489 sa = ifa->ifa_addr;
490 if (sa->sa_family != AF_INET)
491 continue;
492 ia = ((struct sockaddr_in *)sa)->sin_addr;
493 if (prison_check_ip4(cred, &ia) == 0) {
494 found = 1;
495 break;
496 }
497 }
498 IF_ADDR_RUNLOCK(ifp);
499 if (!found) {
500 /*
501 * As a last resort return the 'default' jail address.
502 */
503 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
504 sin_addr;
505 if (prison_get_ip4(cred, &ia) != 0)
506 return (ESRCH);
507 }
508 bzero(&saun->sin, sizeof(struct sockaddr_in));
509 saun->sin.sin_len = sizeof(struct sockaddr_in);
510 saun->sin.sin_family = AF_INET;
511 saun->sin.sin_addr.s_addr = ia.s_addr;
512 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
513 break;
514 }
515#endif
516#ifdef INET6
517 case AF_INET6:
518 {
519 struct in6_addr ia6;
520 struct ifaddr *ifa;
521 int found;
522
523 found = 0;
524 /*
525 * Try to find an address on the given outgoing interface
526 * that belongs to the jail.
527 */
528 IF_ADDR_RLOCK(ifp);
529 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
530 struct sockaddr *sa;
531 sa = ifa->ifa_addr;
532 if (sa->sa_family != AF_INET6)
533 continue;
534 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
535 &ia6, sizeof(struct in6_addr));
536 if (prison_check_ip6(cred, &ia6) == 0) {
537 found = 1;
538 break;
539 }
540 }
541 IF_ADDR_RUNLOCK(ifp);
542 if (!found) {
543 /*
544 * As a last resort return the 'default' jail address.
545 */
546 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
547 sin6_addr;
548 if (prison_get_ip6(cred, &ia6) != 0)
549 return (ESRCH);
550 }
551 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
552 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
553 saun->sin6.sin6_family = AF_INET6;
554 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
555 if (sa6_recoverscope(&saun->sin6) != 0)
556 return (ESRCH);
557 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
558 break;
559 }
560#endif
561 default:
562 return (ESRCH);
563 }
564 return (0);
565}
566
567/*ARGSUSED*/
568static int
569route_output(struct mbuf *m, struct socket *so)
570{
571#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
572 struct rt_msghdr *rtm = NULL;
573 struct rtentry *rt = NULL;
574 struct radix_node_head *rnh;
575 struct rt_addrinfo info;
576 int len, error = 0;
577 struct ifnet *ifp = NULL;
578 union sockaddr_union saun;
579 sa_family_t saf = AF_UNSPEC;
580
581#define senderr(e) { error = e; goto flush;}
582 if (m == NULL || ((m->m_len < sizeof(long)) &&
583 (m = m_pullup(m, sizeof(long))) == NULL))
584 return (ENOBUFS);
585 if ((m->m_flags & M_PKTHDR) == 0)
586 panic("route_output");
587 len = m->m_pkthdr.len;
588 if (len < sizeof(*rtm) ||
589 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
590 info.rti_info[RTAX_DST] = NULL;
591 senderr(EINVAL);
592 }
593 R_Malloc(rtm, struct rt_msghdr *, len);
594 if (rtm == NULL) {
595 info.rti_info[RTAX_DST] = NULL;
596 senderr(ENOBUFS);
597 }
598 m_copydata(m, 0, len, (caddr_t)rtm);
599 if (rtm->rtm_version != RTM_VERSION) {
600 info.rti_info[RTAX_DST] = NULL;
601 senderr(EPROTONOSUPPORT);
602 }
603 rtm->rtm_pid = curproc->p_pid;
604 bzero(&info, sizeof(info));
605 info.rti_addrs = rtm->rtm_addrs;
606 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
607 info.rti_info[RTAX_DST] = NULL;
608 senderr(EINVAL);
609 }
610 info.rti_flags = rtm->rtm_flags;
611 if (info.rti_info[RTAX_DST] == NULL ||
612 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
613 (info.rti_info[RTAX_GATEWAY] != NULL &&
614 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
615 senderr(EINVAL);
616 saf = info.rti_info[RTAX_DST]->sa_family;
617 /*
618 * Verify that the caller has the appropriate privilege; RTM_GET
619 * is the only operation the non-superuser is allowed.
620 */
621 if (rtm->rtm_type != RTM_GET) {
622 error = priv_check(curthread, PRIV_NET_ROUTE);
623 if (error)
624 senderr(error);
625 }
626
627 /*
628 * The given gateway address may be an interface address.
629 * For example, issuing a "route change" command on a route
630 * entry that was created from a tunnel, and the gateway
631 * address given is the local end point. In this case the
632 * RTF_GATEWAY flag must be cleared or the destination will
633 * not be reachable even though there is no error message.
634 */
635 if (info.rti_info[RTAX_GATEWAY] != NULL &&
636 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
637 struct route gw_ro;
638
639 bzero(&gw_ro, sizeof(gw_ro));
640 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
641 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
642 /*
643 * A host route through the loopback interface is
644 * installed for each interface adddress. In pre 8.0
645 * releases the interface address of a PPP link type
646 * is not reachable locally. This behavior is fixed as
647 * part of the new L2/L3 redesign and rewrite work. The
648 * signature of this interface address route is the
649 * AF_LINK sa_family type of the rt_gateway, and the
650 * rt_ifp has the IFF_LOOPBACK flag set.
651 */
652 if (gw_ro.ro_rt != NULL &&
653 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
654 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
655 info.rti_flags &= ~RTF_GATEWAY;
656 if (gw_ro.ro_rt != NULL)
657 RTFREE(gw_ro.ro_rt);
658 }
659
660 switch (rtm->rtm_type) {
661 struct rtentry *saved_nrt;
662
663 case RTM_ADD:
664 if (info.rti_info[RTAX_GATEWAY] == NULL)
665 senderr(EINVAL);
666 saved_nrt = NULL;
667
668 /* support for new ARP code */
669 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
670 (rtm->rtm_flags & RTF_LLDATA) != 0) {
671 error = lla_rt_output(rtm, &info);
672 break;
673 }
674 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
675 so->so_fibnum);
676 if (error == 0 && saved_nrt) {
677 RT_LOCK(saved_nrt);
678 rt_setmetrics(rtm->rtm_inits,
679 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
680 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
681 RT_REMREF(saved_nrt);
682 RT_UNLOCK(saved_nrt);
683 }
684 break;
685
686 case RTM_DELETE:
687 saved_nrt = NULL;
688 /* support for new ARP code */
689 if (info.rti_info[RTAX_GATEWAY] &&
690 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
691 (rtm->rtm_flags & RTF_LLDATA) != 0) {
692 error = lla_rt_output(rtm, &info);
693 break;
694 }
695 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
696 so->so_fibnum);
697 if (error == 0) {
698 RT_LOCK(saved_nrt);
699 rt = saved_nrt;
700 goto report;
701 }
702 break;
703
704 case RTM_GET:
705 case RTM_CHANGE:
706 case RTM_LOCK:
707 rnh = rt_tables_get_rnh(so->so_fibnum,
708 info.rti_info[RTAX_DST]->sa_family);
709 if (rnh == NULL)
710 senderr(EAFNOSUPPORT);
711 RADIX_NODE_HEAD_RLOCK(rnh);
712 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
713 info.rti_info[RTAX_NETMASK], rnh);
714 if (rt == NULL) { /* XXX looks bogus */
715 RADIX_NODE_HEAD_RUNLOCK(rnh);
716 senderr(ESRCH);
717 }
718#ifdef RADIX_MPATH
719 /*
720 * for RTM_CHANGE/LOCK, if we got multipath routes,
721 * we require users to specify a matching RTAX_GATEWAY.
722 *
723 * for RTM_GET, gate is optional even with multipath.
724 * if gate == NULL the first match is returned.
725 * (no need to call rt_mpath_matchgate if gate == NULL)
726 */
727 if (rn_mpath_capable(rnh) &&
728 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
729 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
730 if (!rt) {
731 RADIX_NODE_HEAD_RUNLOCK(rnh);
732 senderr(ESRCH);
733 }
734 }
735#endif
736 /*
737 * If performing proxied L2 entry insertion, and
738 * the actual PPP host entry is found, perform
739 * another search to retrieve the prefix route of
740 * the local end point of the PPP link.
741 */
742 if (rtm->rtm_flags & RTF_ANNOUNCE) {
743 struct sockaddr laddr;
744
745 if (rt->rt_ifp != NULL &&
746 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
747 struct ifaddr *ifa;
748
749 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
750 if (ifa != NULL)
751 rt_maskedcopy(ifa->ifa_addr,
752 &laddr,
753 ifa->ifa_netmask);
754 } else
755 rt_maskedcopy(rt->rt_ifa->ifa_addr,
756 &laddr,
757 rt->rt_ifa->ifa_netmask);
758 /*
759 * refactor rt and no lock operation necessary
760 */
761 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
762 if (rt == NULL) {
763 RADIX_NODE_HEAD_RUNLOCK(rnh);
764 senderr(ESRCH);
765 }
766 }
767 RT_LOCK(rt);
768 RT_ADDREF(rt);
769 RADIX_NODE_HEAD_RUNLOCK(rnh);
770
771 /*
772 * Fix for PR: 82974
773 *
774 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
775 * returns a perfect match in case a netmask is
776 * specified. For host routes only a longest prefix
777 * match is returned so it is necessary to compare the
778 * existence of the netmask. If both have a netmask
779 * rnh_lookup() did a perfect match and if none of them
780 * have a netmask both are host routes which is also a
781 * perfect match.
782 */
783
784 if (rtm->rtm_type != RTM_GET &&
785 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
786 RT_UNLOCK(rt);
787 senderr(ESRCH);
788 }
789
790 switch(rtm->rtm_type) {
791
792 case RTM_GET:
793 report:
794 RT_LOCK_ASSERT(rt);
795 if ((rt->rt_flags & RTF_HOST) == 0
796 ? jailed_without_vnet(curthread->td_ucred)
797 : prison_if(curthread->td_ucred,
798 rt_key(rt)) != 0) {
799 RT_UNLOCK(rt);
800 senderr(ESRCH);
801 }
802 info.rti_info[RTAX_DST] = rt_key(rt);
803 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
804 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
805 info.rti_info[RTAX_GENMASK] = 0;
806 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
807 ifp = rt->rt_ifp;
808 if (ifp) {
809 info.rti_info[RTAX_IFP] =
810 ifp->if_addr->ifa_addr;
811 error = rtm_get_jailed(&info, ifp, rt,
812 &saun, curthread->td_ucred);
813 if (error != 0) {
814 RT_UNLOCK(rt);
815 senderr(error);
816 }
817 if (ifp->if_flags & IFF_POINTOPOINT)
818 info.rti_info[RTAX_BRD] =
819 rt->rt_ifa->ifa_dstaddr;
820 rtm->rtm_index = ifp->if_index;
821 } else {
822 info.rti_info[RTAX_IFP] = NULL;
823 info.rti_info[RTAX_IFA] = NULL;
824 }
825 } else if ((ifp = rt->rt_ifp) != NULL) {
826 rtm->rtm_index = ifp->if_index;
827 }
828 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
829 if (len > rtm->rtm_msglen) {
830 struct rt_msghdr *new_rtm;
831 R_Malloc(new_rtm, struct rt_msghdr *, len);
832 if (new_rtm == NULL) {
833 RT_UNLOCK(rt);
834 senderr(ENOBUFS);
835 }
836 bcopy(rtm, new_rtm, rtm->rtm_msglen);
837 Free(rtm); rtm = new_rtm;
838 }
839 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
840 rtm->rtm_flags = rt->rt_flags;
841 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
842 rtm->rtm_addrs = info.rti_addrs;
843 break;
844
845 case RTM_CHANGE:
846 /*
847 * New gateway could require new ifaddr, ifp;
848 * flags may also be different; ifp may be specified
849 * by ll sockaddr when protocol address is ambiguous
850 */
851 if (((rt->rt_flags & RTF_GATEWAY) &&
852 info.rti_info[RTAX_GATEWAY] != NULL) ||
853 info.rti_info[RTAX_IFP] != NULL ||
854 (info.rti_info[RTAX_IFA] != NULL &&
855 !sa_equal(info.rti_info[RTAX_IFA],
856 rt->rt_ifa->ifa_addr))) {
857 RT_UNLOCK(rt);
858 RADIX_NODE_HEAD_LOCK(rnh);
859 error = rt_getifa_fib(&info, rt->rt_fibnum);
860 /*
861 * XXXRW: Really we should release this
862 * reference later, but this maintains
863 * historical behavior.
864 */
865 if (info.rti_ifa != NULL)
866 ifa_free(info.rti_ifa);
867 RADIX_NODE_HEAD_UNLOCK(rnh);
868 if (error != 0)
869 senderr(error);
870 RT_LOCK(rt);
871 }
872 if (info.rti_ifa != NULL &&
873 info.rti_ifa != rt->rt_ifa &&
874 rt->rt_ifa != NULL &&
875 rt->rt_ifa->ifa_rtrequest != NULL) {
876 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
877 &info);
878 ifa_free(rt->rt_ifa);
879 }
880 if (info.rti_info[RTAX_GATEWAY] != NULL) {
881 RT_UNLOCK(rt);
882 RADIX_NODE_HEAD_LOCK(rnh);
883 RT_LOCK(rt);
884
885 error = rt_setgate(rt, rt_key(rt),
886 info.rti_info[RTAX_GATEWAY]);
887 RADIX_NODE_HEAD_UNLOCK(rnh);
888 if (error != 0) {
889 RT_UNLOCK(rt);
890 senderr(error);
891 }
892 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
893 }
894 if (info.rti_ifa != NULL &&
895 info.rti_ifa != rt->rt_ifa) {
896 ifa_ref(info.rti_ifa);
897 rt->rt_ifa = info.rti_ifa;
898 rt->rt_ifp = info.rti_ifp;
899 }
900 /* Allow some flags to be toggled on change. */
901 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
902 (rtm->rtm_flags & RTF_FMASK);
903 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
904 &rt->rt_rmx);
905 rtm->rtm_index = rt->rt_ifp->if_index;
906 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
907 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
908 /* FALLTHROUGH */
909 case RTM_LOCK:
910 /* We don't support locks anymore */
911 break;
912 }
913 RT_UNLOCK(rt);
914 break;
915
916 default:
917 senderr(EOPNOTSUPP);
918 }
919
920flush:
921 if (rtm) {
922 if (error)
923 rtm->rtm_errno = error;
924 else
925 rtm->rtm_flags |= RTF_DONE;
926 }
927 if (rt) /* XXX can this be true? */
928 RTFREE(rt);
929 {
930 struct rawcb *rp = NULL;
931 /*
932 * Check to see if we don't want our own messages.
933 */
934 if ((so->so_options & SO_USELOOPBACK) == 0) {
935 if (route_cb.any_count <= 1) {
936 if (rtm)
937 Free(rtm);
938 m_freem(m);
939 return (error);
940 }
941 /* There is another listener, so construct message */
942 rp = sotorawcb(so);
943 }
944 if (rtm) {
945 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
946 if (m->m_pkthdr.len < rtm->rtm_msglen) {
947 m_freem(m);
948 m = NULL;
949 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
950 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
951 }
952 if (m) {
953 M_SETFIB(m, so->so_fibnum);
954 m->m_flags |= RTS_FILTER_FIB;
955 if (rp) {
956 /*
957 * XXX insure we don't get a copy by
958 * invalidating our protocol
959 */
960 unsigned short family = rp->rcb_proto.sp_family;
961 rp->rcb_proto.sp_family = 0;
962 rt_dispatch(m, saf);
963 rp->rcb_proto.sp_family = family;
964 } else
965 rt_dispatch(m, saf);
966 }
967 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
968 if (rtm)
969 Free(rtm);
970 }
971 return (error);
972#undef sa_equal
973}
974
975static void
976rt_setmetrics(u_long which, const struct rt_metrics *in,
977 struct rt_metrics_lite *out)
978{
979#define metric(f, e) if (which & (f)) out->e = in->e;
980 /*
981 * Only these are stored in the routing entry since introduction
982 * of tcp hostcache. The rest is ignored.
983 */
984 metric(RTV_MTU, rmx_mtu);
985 metric(RTV_WEIGHT, rmx_weight);
986 /* Userland -> kernel timebase conversion. */
987 if (which & RTV_EXPIRE)
988 out->rmx_expire = in->rmx_expire ?
989 in->rmx_expire - time_second + time_uptime : 0;
990#undef metric
991}
992
993static void
994rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
995{
996#define metric(e) out->e = in->e;
997 bzero(out, sizeof(*out));
998 metric(rmx_mtu);
999 metric(rmx_weight);
1000 /* Kernel -> userland timebase conversion. */
1001 out->rmx_expire = in->rmx_expire ?
1002 in->rmx_expire - time_uptime + time_second : 0;
1003#undef metric
1004}
1005
1006/*
1007 * Extract the addresses of the passed sockaddrs.
1008 * Do a little sanity checking so as to avoid bad memory references.
1009 * This data is derived straight from userland.
1010 */
1011static int
1012rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1013{
1014 struct sockaddr *sa;
1015 int i;
1016
1017 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1018 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1019 continue;
1020 sa = (struct sockaddr *)cp;
1021 /*
1022 * It won't fit.
1023 */
1024 if (cp + sa->sa_len > cplim)
1025 return (EINVAL);
1026 /*
1027 * there are no more.. quit now
1028 * If there are more bits, they are in error.
1029 * I've seen this. route(1) can evidently generate these.
1030 * This causes kernel to core dump.
1031 * for compatibility, If we see this, point to a safe address.
1032 */
1033 if (sa->sa_len == 0) {
1034 rtinfo->rti_info[i] = &sa_zero;
1035 return (0); /* should be EINVAL but for compat */
1036 }
1037 /* accept it */
1038 rtinfo->rti_info[i] = sa;
1039 cp += SA_SIZE(sa);
1040 }
1041 return (0);
1042}
1043
1044/*
1045 * Used by the routing socket.
1046 */
1047static struct mbuf *
1048rt_msg1(int type, struct rt_addrinfo *rtinfo)
1049{
1050 struct rt_msghdr *rtm;
1051 struct mbuf *m;
1052 int i;
1053 struct sockaddr *sa;
1054 int len, dlen;
1055
1056 switch (type) {
1057
1058 case RTM_DELADDR:
1059 case RTM_NEWADDR:
1060 len = sizeof(struct ifa_msghdr);
1061 break;
1062
1063 case RTM_DELMADDR:
1064 case RTM_NEWMADDR:
1065 len = sizeof(struct ifma_msghdr);
1066 break;
1067
1068 case RTM_IFINFO:
1069 len = sizeof(struct if_msghdr);
1070 break;
1071
1072 case RTM_IFANNOUNCE:
1073 case RTM_IEEE80211:
1074 len = sizeof(struct if_announcemsghdr);
1075 break;
1076
1077 default:
1078 len = sizeof(struct rt_msghdr);
1079 }
1080 if (len > MCLBYTES)
1081 panic("rt_msg1");
1082 m = m_gethdr(M_DONTWAIT, MT_DATA);
1083 if (m && len > MHLEN) {
1084 MCLGET(m, M_DONTWAIT);
1085 if ((m->m_flags & M_EXT) == 0) {
1086 m_free(m);
1087 m = NULL;
1088 }
1089 }
1090 if (m == NULL)
1091 return (m);
1092 m->m_pkthdr.len = m->m_len = len;
1093 m->m_pkthdr.rcvif = NULL;
1094 rtm = mtod(m, struct rt_msghdr *);
1095 bzero((caddr_t)rtm, len);
1096 for (i = 0; i < RTAX_MAX; i++) {
1097 if ((sa = rtinfo->rti_info[i]) == NULL)
1098 continue;
1099 rtinfo->rti_addrs |= (1 << i);
1100 dlen = SA_SIZE(sa);
1101 m_copyback(m, len, dlen, (caddr_t)sa);
1102 len += dlen;
1103 }
1104 if (m->m_pkthdr.len != len) {
1105 m_freem(m);
1106 return (NULL);
1107 }
1108 rtm->rtm_msglen = len;
1109 rtm->rtm_version = RTM_VERSION;
1110 rtm->rtm_type = type;
1111 return (m);
1112}
1113
1114/*
1115 * Used by the sysctl code and routing socket.
1116 */
1117static int
1118rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1119{
1120 int i;
1121 int len, dlen, second_time = 0;
1122 caddr_t cp0;
1123
1124 rtinfo->rti_addrs = 0;
1125again:
1126 switch (type) {
1127
1128 case RTM_DELADDR:
1129 case RTM_NEWADDR:
1130 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1131#ifdef COMPAT_FREEBSD32
1132 if (w->w_req->flags & SCTL_MASK32)
1133 len = sizeof(struct ifa_msghdrl32);
1134 else
1135#endif
1136 len = sizeof(struct ifa_msghdrl);
1137 } else
1138 len = sizeof(struct ifa_msghdr);
1139 break;
1140
1141 case RTM_IFINFO:
1142#ifdef COMPAT_FREEBSD32
1143 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1144 if (w->w_op == NET_RT_IFLISTL)
1145 len = sizeof(struct if_msghdrl32);
1146 else
1147 len = sizeof(struct if_msghdr32);
1148 break;
1149 }
1150#endif
1151 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1152 len = sizeof(struct if_msghdrl);
1153 else
1154 len = sizeof(struct if_msghdr);
1155 break;
1156
1157 case RTM_NEWMADDR:
1158 len = sizeof(struct ifma_msghdr);
1159 break;
1160
1161 default:
1162 len = sizeof(struct rt_msghdr);
1163 }
1164 cp0 = cp;
1165 if (cp0)
1166 cp += len;
1167 for (i = 0; i < RTAX_MAX; i++) {
1168 struct sockaddr *sa;
1169
1170 if ((sa = rtinfo->rti_info[i]) == NULL)
1171 continue;
1172 rtinfo->rti_addrs |= (1 << i);
1173 dlen = SA_SIZE(sa);
1174 if (cp) {
1175 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1176 cp += dlen;
1177 }
1178 len += dlen;
1179 }
1180 len = ALIGN(len);
1181 if (cp == NULL && w != NULL && !second_time) {
1182 struct walkarg *rw = w;
1183
1184 if (rw->w_req) {
1185 if (rw->w_tmemsize < len) {
1186 if (rw->w_tmem)
1187 free(rw->w_tmem, M_RTABLE);
1188 rw->w_tmem = (caddr_t)
1189 malloc(len, M_RTABLE, M_NOWAIT);
1190 if (rw->w_tmem)
1191 rw->w_tmemsize = len;
1192 }
1193 if (rw->w_tmem) {
1194 cp = rw->w_tmem;
1195 second_time = 1;
1196 goto again;
1197 }
1198 }
1199 }
1200 if (cp) {
1201 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1202
1203 rtm->rtm_version = RTM_VERSION;
1204 rtm->rtm_type = type;
1205 rtm->rtm_msglen = len;
1206 }
1207 return (len);
1208}
1209
1210/*
1211 * This routine is called to generate a message from the routing
1212 * socket indicating that a redirect has occured, a routing lookup
1213 * has failed, or that a protocol has detected timeouts to a particular
1214 * destination.
1215 */
1216void
1217rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1218 int fibnum)
1219{
1220 struct rt_msghdr *rtm;
1221 struct mbuf *m;
1222 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1223
1224 if (route_cb.any_count == 0)
1225 return;
1226 m = rt_msg1(type, rtinfo);
1227 if (m == NULL)
1228 return;
1229
1230 if (fibnum != RTS_ALLFIBS) {
1231 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1232 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1233 M_SETFIB(m, fibnum);
1234 m->m_flags |= RTS_FILTER_FIB;
1235 }
1236
1237 rtm = mtod(m, struct rt_msghdr *);
1238 rtm->rtm_flags = RTF_DONE | flags;
1239 rtm->rtm_errno = error;
1240 rtm->rtm_addrs = rtinfo->rti_addrs;
1241 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1242}
1243
1244void
1245rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1246{
1247
1248 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1249}
1250
1251/*
1252 * This routine is called to generate a message from the routing
1253 * socket indicating that the status of a network interface has changed.
1254 */
1255void
1256rt_ifmsg(struct ifnet *ifp)
1257{
1258 struct if_msghdr *ifm;
1259 struct mbuf *m;
1260 struct rt_addrinfo info;
1261
1262 if (route_cb.any_count == 0)
1263 return;
1264 bzero((caddr_t)&info, sizeof(info));
1265 m = rt_msg1(RTM_IFINFO, &info);
1266 if (m == NULL)
1267 return;
1268 ifm = mtod(m, struct if_msghdr *);
1269 ifm->ifm_index = ifp->if_index;
1270 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1271 ifm->ifm_data = ifp->if_data;
1272 ifm->ifm_addrs = 0;
1273 rt_dispatch(m, AF_UNSPEC);
1274}
1275
1276/*
1277 * This is called to generate messages from the routing socket
1278 * indicating a network interface has had addresses associated with it.
1279 * if we ever reverse the logic and replace messages TO the routing
1280 * socket indicate a request to configure interfaces, then it will
1281 * be unnecessary as the routing socket will automatically generate
1282 * copies of it.
1283 */
1284void
1285rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1286 int fibnum)
1287{
1288 struct rt_addrinfo info;
1289 struct sockaddr *sa = NULL;
1290 int pass;
1291 struct mbuf *m = NULL;
1292 struct ifnet *ifp = ifa->ifa_ifp;
1293
1294 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1295 ("unexpected cmd %u", cmd));
1296#if defined(INET) || defined(INET6)
1297#ifdef SCTP
1298 /*
1299 * notify the SCTP stack
1300 * this will only get called when an address is added/deleted
1301 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1302 */
1303 sctp_addr_change(ifa, cmd);
1304#endif /* SCTP */
1305#endif
1306 if (route_cb.any_count == 0)
1307 return;
1308 for (pass = 1; pass < 3; pass++) {
1309 bzero((caddr_t)&info, sizeof(info));
1310 if ((cmd == RTM_ADD && pass == 1) ||
1311 (cmd == RTM_DELETE && pass == 2)) {
1312 struct ifa_msghdr *ifam;
1313 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1314
1315 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1316 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1317 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1318 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1319 if ((m = rt_msg1(ncmd, &info)) == NULL)
1320 continue;
1321 ifam = mtod(m, struct ifa_msghdr *);
1322 ifam->ifam_index = ifp->if_index;
1323 ifam->ifam_metric = ifa->ifa_metric;
1324 ifam->ifam_flags = ifa->ifa_flags;
1325 ifam->ifam_addrs = info.rti_addrs;
1326 }
1327 if ((cmd == RTM_ADD && pass == 2) ||
1328 (cmd == RTM_DELETE && pass == 1)) {
1329 struct rt_msghdr *rtm;
1330
1331 if (rt == NULL)
1332 continue;
1333 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1334 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1335 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1336 if ((m = rt_msg1(cmd, &info)) == NULL)
1337 continue;
1338 rtm = mtod(m, struct rt_msghdr *);
1339 rtm->rtm_index = ifp->if_index;
1340 rtm->rtm_flags |= rt->rt_flags;
1341 rtm->rtm_errno = error;
1342 rtm->rtm_addrs = info.rti_addrs;
1343 }
1344 if (fibnum != RTS_ALLFIBS) {
1345 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1346 "fibnum out of range 0 <= %d < %d", __func__,
1347 fibnum, rt_numfibs));
1348 M_SETFIB(m, fibnum);
1349 m->m_flags |= RTS_FILTER_FIB;
1350 }
1351 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1352 }
1353}
1354
1355void
1356rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1357{
1358
1359 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1360}
1361
1362/*
1363 * This is the analogue to the rt_newaddrmsg which performs the same
1364 * function but for multicast group memberhips. This is easier since
1365 * there is no route state to worry about.
1366 */
1367void
1368rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1369{
1370 struct rt_addrinfo info;
1371 struct mbuf *m = NULL;
1372 struct ifnet *ifp = ifma->ifma_ifp;
1373 struct ifma_msghdr *ifmam;
1374
1375 if (route_cb.any_count == 0)
1376 return;
1377
1378 bzero((caddr_t)&info, sizeof(info));
1379 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1380 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1381 /*
1382 * If a link-layer address is present, present it as a ``gateway''
1383 * (similarly to how ARP entries, e.g., are presented).
1384 */
1385 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1386 m = rt_msg1(cmd, &info);
1387 if (m == NULL)
1388 return;
1389 ifmam = mtod(m, struct ifma_msghdr *);
1390 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1391 __func__));
1392 ifmam->ifmam_index = ifp->if_index;
1393 ifmam->ifmam_addrs = info.rti_addrs;
1394 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1395}
1396
1397static struct mbuf *
1398rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1399 struct rt_addrinfo *info)
1400{
1401 struct if_announcemsghdr *ifan;
1402 struct mbuf *m;
1403
1404 if (route_cb.any_count == 0)
1405 return NULL;
1406 bzero((caddr_t)info, sizeof(*info));
1407 m = rt_msg1(type, info);
1408 if (m != NULL) {
1409 ifan = mtod(m, struct if_announcemsghdr *);
1410 ifan->ifan_index = ifp->if_index;
1411 strlcpy(ifan->ifan_name, ifp->if_xname,
1412 sizeof(ifan->ifan_name));
1413 ifan->ifan_what = what;
1414 }
1415 return m;
1416}
1417
1418/*
1419 * This is called to generate routing socket messages indicating
1420 * IEEE80211 wireless events.
1421 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1422 */
1423void
1424rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1425{
1426 struct mbuf *m;
1427 struct rt_addrinfo info;
1428
1429 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1430 if (m != NULL) {
1431 /*
1432 * Append the ieee80211 data. Try to stick it in the
1433 * mbuf containing the ifannounce msg; otherwise allocate
1434 * a new mbuf and append.
1435 *
1436 * NB: we assume m is a single mbuf.
1437 */
1438 if (data_len > M_TRAILINGSPACE(m)) {
1439 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1440 if (n == NULL) {
1441 m_freem(m);
1442 return;
1443 }
1444 bcopy(data, mtod(n, void *), data_len);
1445 n->m_len = data_len;
1446 m->m_next = n;
1447 } else if (data_len > 0) {
1448 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1449 m->m_len += data_len;
1450 }
1451 if (m->m_flags & M_PKTHDR)
1452 m->m_pkthdr.len += data_len;
1453 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1454 rt_dispatch(m, AF_UNSPEC);
1455 }
1456}
1457
1458/*
1459 * This is called to generate routing socket messages indicating
1460 * network interface arrival and departure.
1461 */
1462void
1463rt_ifannouncemsg(struct ifnet *ifp, int what)
1464{
1465 struct mbuf *m;
1466 struct rt_addrinfo info;
1467
1468 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1469 if (m != NULL)
1470 rt_dispatch(m, AF_UNSPEC);
1471}
1472
1473static void
1474rt_dispatch(struct mbuf *m, sa_family_t saf)
1475{
1476 struct m_tag *tag;
1477
1478 /*
1479 * Preserve the family from the sockaddr, if any, in an m_tag for
1480 * use when injecting the mbuf into the routing socket buffer from
1481 * the netisr.
1482 */
1483 if (saf != AF_UNSPEC) {
1484 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1485 M_NOWAIT);
1486 if (tag == NULL) {
1487 m_freem(m);
1488 return;
1489 }
1490 *(unsigned short *)(tag + 1) = saf;
1491 m_tag_prepend(m, tag);
1492 }
1493#ifdef VIMAGE
1494 if (V_loif)
1495 m->m_pkthdr.rcvif = V_loif;
1496 else {
1497 m_freem(m);
1498 return;
1499 }
1500#endif
1501 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1502}
1503
1504/*
1505 * This is used in dumping the kernel table via sysctl().
1506 */
1507static int
1508sysctl_dumpentry(struct radix_node *rn, void *vw)
1509{
1510 struct walkarg *w = vw;
1511 struct rtentry *rt = (struct rtentry *)rn;
1512 int error = 0, size;
1513 struct rt_addrinfo info;
1514
1515 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1516 return 0;
1517 if ((rt->rt_flags & RTF_HOST) == 0
1518 ? jailed_without_vnet(w->w_req->td->td_ucred)
1519 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1520 return (0);
1521 bzero((caddr_t)&info, sizeof(info));
1522 info.rti_info[RTAX_DST] = rt_key(rt);
1523 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1524 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1525 info.rti_info[RTAX_GENMASK] = 0;
1526 if (rt->rt_ifp) {
1527 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1528 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1529 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1530 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1531 }
1532 size = rt_msg2(RTM_GET, &info, NULL, w);
1533 if (w->w_req && w->w_tmem) {
1534 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1535
1536 rtm->rtm_flags = rt->rt_flags;
1537 /*
1538 * let's be honest about this being a retarded hack
1539 */
1540 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1541 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1542 rtm->rtm_index = rt->rt_ifp->if_index;
1543 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1544 rtm->rtm_addrs = info.rti_addrs;
1545 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1546 return (error);
1547 }
1548 return (error);
1549}
1550
1551#ifdef COMPAT_FREEBSD32
1552static void
1553copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1554{
1555
1556 bzero(dst, sizeof(*dst));
1557 CP(*src, *dst, ifi_type);
1558 CP(*src, *dst, ifi_physical);
1559 CP(*src, *dst, ifi_addrlen);
1560 CP(*src, *dst, ifi_hdrlen);
1561 CP(*src, *dst, ifi_link_state);
1562 CP(*src, *dst, ifi_vhid);
1563 dst->ifi_datalen = sizeof(struct if_data32);
1564 CP(*src, *dst, ifi_mtu);
1565 CP(*src, *dst, ifi_metric);
1566 CP(*src, *dst, ifi_baudrate);
1567 CP(*src, *dst, ifi_ipackets);
1568 CP(*src, *dst, ifi_ierrors);
1569 CP(*src, *dst, ifi_opackets);
1570 CP(*src, *dst, ifi_oerrors);
1571 CP(*src, *dst, ifi_collisions);
1572 CP(*src, *dst, ifi_ibytes);
1573 CP(*src, *dst, ifi_obytes);
1574 CP(*src, *dst, ifi_imcasts);
1575 CP(*src, *dst, ifi_omcasts);
1576 CP(*src, *dst, ifi_iqdrops);
1577 CP(*src, *dst, ifi_noproto);
1578 CP(*src, *dst, ifi_hwassist);
1579 CP(*src, *dst, ifi_epoch);
1580 TV_CP(*src, *dst, ifi_lastchange);
1581}
1582#endif
1583
1584static int
1585sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1586 struct walkarg *w, int len)
1587{
1588 struct if_msghdrl *ifm;
1589
1590#ifdef COMPAT_FREEBSD32
1591 if (w->w_req->flags & SCTL_MASK32) {
1592 struct if_msghdrl32 *ifm32;
1593
1594 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1595 ifm32->ifm_addrs = info->rti_addrs;
1596 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1597 ifm32->ifm_index = ifp->if_index;
1598 ifm32->_ifm_spare1 = 0;
1599 ifm32->ifm_len = sizeof(*ifm32);
1600 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1601
1602 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1603 /* Fixup if_data carp(4) vhid. */
1604 if (carp_get_vhid_p != NULL)
1605 ifm32->ifm_data.ifi_vhid =
1606 (*carp_get_vhid_p)(ifp->if_addr);
1607
1608 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1609 }
1610#endif
1611 ifm = (struct if_msghdrl *)w->w_tmem;
1612 ifm->ifm_addrs = info->rti_addrs;
1613 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1614 ifm->ifm_index = ifp->if_index;
1615 ifm->_ifm_spare1 = 0;
1616 ifm->ifm_len = sizeof(*ifm);
1617 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1618
1619 ifm->ifm_data = ifp->if_data;
1620 /* Fixup if_data carp(4) vhid. */
1621 if (carp_get_vhid_p != NULL)
1622 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1623
1624 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1625}
1626
1627static int
1628sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1629 struct walkarg *w, int len)
1630{
1631 struct if_msghdr *ifm;
1632
1633#ifdef COMPAT_FREEBSD32
1634 if (w->w_req->flags & SCTL_MASK32) {
1635 struct if_msghdr32 *ifm32;
1636
1637 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1638 ifm32->ifm_addrs = info->rti_addrs;
1639 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1640 ifm32->ifm_index = ifp->if_index;
1641
1642 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1643 /* Fixup if_data carp(4) vhid. */
1644 if (carp_get_vhid_p != NULL)
1645 ifm32->ifm_data.ifi_vhid =
1646 (*carp_get_vhid_p)(ifp->if_addr);
1647
1648 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1649 }
1650#endif
1651 ifm = (struct if_msghdr *)w->w_tmem;
1652 ifm->ifm_addrs = info->rti_addrs;
1653 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1654 ifm->ifm_index = ifp->if_index;
1655
1656 ifm->ifm_data = ifp->if_data;
1657 /* Fixup if_data carp(4) vhid. */
1658 if (carp_get_vhid_p != NULL)
1659 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1660
1661 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1662}
1663
1664static int
1665sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1666 struct walkarg *w, int len)
1667{
1668 struct ifa_msghdrl *ifam;
1669
1670#ifdef COMPAT_FREEBSD32
1671 if (w->w_req->flags & SCTL_MASK32) {
1672 struct ifa_msghdrl32 *ifam32;
1673
1674 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1675 ifam32->ifam_addrs = info->rti_addrs;
1676 ifam32->ifam_flags = ifa->ifa_flags;
1677 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1678 ifam32->_ifam_spare1 = 0;
1679 ifam32->ifam_len = sizeof(*ifam32);
1680 ifam32->ifam_data_off =
1681 offsetof(struct ifa_msghdrl32, ifam_data);
1682 ifam32->ifam_metric = ifa->ifa_metric;
1683
1684 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1685 /* Fixup if_data carp(4) vhid. */
1686 if (carp_get_vhid_p != NULL)
1687 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1688
1689 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1690 }
1691#endif
1692
1693 ifam = (struct ifa_msghdrl *)w->w_tmem;
1694 ifam->ifam_addrs = info->rti_addrs;
1695 ifam->ifam_flags = ifa->ifa_flags;
1696 ifam->ifam_index = ifa->ifa_ifp->if_index;
1697 ifam->_ifam_spare1 = 0;
1698 ifam->ifam_len = sizeof(*ifam);
1699 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1700 ifam->ifam_metric = ifa->ifa_metric;
1701
1702 ifam->ifam_data = ifa->if_data;
1703 /* Fixup if_data carp(4) vhid. */
1704 if (carp_get_vhid_p != NULL)
1705 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1706
1707 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1708}
1709
1710static int
1711sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1712 struct walkarg *w, int len)
1713{
1714 struct ifa_msghdr *ifam;
1715
1716 ifam = (struct ifa_msghdr *)w->w_tmem;
1717 ifam->ifam_addrs = info->rti_addrs;
1718 ifam->ifam_flags = ifa->ifa_flags;
1719 ifam->ifam_index = ifa->ifa_ifp->if_index;
1720 ifam->ifam_metric = ifa->ifa_metric;
1721
1722 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1723}
1724
1725static int
1726sysctl_iflist(int af, struct walkarg *w)
1727{
1728 struct ifnet *ifp;
1729 struct ifaddr *ifa;
1730 struct rt_addrinfo info;
1731 int len, error = 0;
1732
1733 bzero((caddr_t)&info, sizeof(info));
1734 IFNET_RLOCK();
1735 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1736 if (w->w_arg && w->w_arg != ifp->if_index)
1737 continue;
1738 IF_ADDR_RLOCK(ifp);
1739 ifa = ifp->if_addr;
1740 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1741 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1742 info.rti_info[RTAX_IFP] = NULL;
1743 if (w->w_req && w->w_tmem) {
1744 if (w->w_op == NET_RT_IFLISTL)
1745 error = sysctl_iflist_ifml(ifp, &info, w, len);
1746 else
1747 error = sysctl_iflist_ifm(ifp, &info, w, len);
1748 if (error)
1749 goto done;
1750 }
1751 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1752 if (af && af != ifa->ifa_addr->sa_family)
1753 continue;
1754 if (prison_if(w->w_req->td->td_ucred,
1755 ifa->ifa_addr) != 0)
1756 continue;
1757 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1758 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1759 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1760 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1761 if (w->w_req && w->w_tmem) {
1762 if (w->w_op == NET_RT_IFLISTL)
1763 error = sysctl_iflist_ifaml(ifa, &info,
1764 w, len);
1765 else
1766 error = sysctl_iflist_ifam(ifa, &info,
1767 w, len);
1768 if (error)
1769 goto done;
1770 }
1771 }
1772 IF_ADDR_RUNLOCK(ifp);
1773 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1774 info.rti_info[RTAX_BRD] = NULL;
1775 }
1776done:
1777 if (ifp != NULL)
1778 IF_ADDR_RUNLOCK(ifp);
1779 IFNET_RUNLOCK();
1780 return (error);
1781}
1782
1783static int
1784sysctl_ifmalist(int af, struct walkarg *w)
1785{
1786 struct ifnet *ifp;
1787 struct ifmultiaddr *ifma;
1788 struct rt_addrinfo info;
1789 int len, error = 0;
1790 struct ifaddr *ifa;
1791
1792 bzero((caddr_t)&info, sizeof(info));
1793 IFNET_RLOCK();
1794 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1795 if (w->w_arg && w->w_arg != ifp->if_index)
1796 continue;
1797 ifa = ifp->if_addr;
1798 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1799 IF_ADDR_RLOCK(ifp);
1800 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1801 if (af && af != ifma->ifma_addr->sa_family)
1802 continue;
1803 if (prison_if(w->w_req->td->td_ucred,
1804 ifma->ifma_addr) != 0)
1805 continue;
1806 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1807 info.rti_info[RTAX_GATEWAY] =
1808 (ifma->ifma_addr->sa_family != AF_LINK) ?
1809 ifma->ifma_lladdr : NULL;
1810 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1811 if (w->w_req && w->w_tmem) {
1812 struct ifma_msghdr *ifmam;
1813
1814 ifmam = (struct ifma_msghdr *)w->w_tmem;
1815 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1816 ifmam->ifmam_flags = 0;
1817 ifmam->ifmam_addrs = info.rti_addrs;
1818 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1819 if (error) {
1820 IF_ADDR_RUNLOCK(ifp);
1821 goto done;
1822 }
1823 }
1824 }
1825 IF_ADDR_RUNLOCK(ifp);
1826 }
1827done:
1828 IFNET_RUNLOCK();
1829 return (error);
1830}
1831
1832static int
1833sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1834{
1835 int *name = (int *)arg1;
1836 u_int namelen = arg2;
1837 struct radix_node_head *rnh = NULL; /* silence compiler. */
1838 int i, lim, error = EINVAL;
1839 u_char af;
1840 struct walkarg w;
1841
1842 name ++;
1843 namelen--;
1844 if (req->newptr)
1845 return (EPERM);
1846 if (namelen != 3)
1847 return ((namelen < 3) ? EISDIR : ENOTDIR);
1848 af = name[0];
1849 if (af > AF_MAX)
1850 return (EINVAL);
1851 bzero(&w, sizeof(w));
1852 w.w_op = name[1];
1853 w.w_arg = name[2];
1854 w.w_req = req;
1855
1856 error = sysctl_wire_old_buffer(req, 0);
1857 if (error)
1858 return (error);
1859 switch (w.w_op) {
1860
1861 case NET_RT_DUMP:
1862 case NET_RT_FLAGS:
1863 if (af == 0) { /* dump all tables */
1864 i = 1;
1865 lim = AF_MAX;
1866 } else /* dump only one table */
1867 i = lim = af;
1868
1869 /*
1870 * take care of llinfo entries, the caller must
1871 * specify an AF
1872 */
1873 if (w.w_op == NET_RT_FLAGS &&
1874 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1875 if (af != 0)
1876 error = lltable_sysctl_dumparp(af, w.w_req);
1877 else
1878 error = EINVAL;
1879 break;
1880 }
1881 /*
1882 * take care of routing entries
1883 */
1884 for (error = 0; error == 0 && i <= lim; i++) {
1885 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1886 if (rnh != NULL) {
1887 RADIX_NODE_HEAD_RLOCK(rnh);
1888 error = rnh->rnh_walktree(rnh,
1889 sysctl_dumpentry, &w);
1890 RADIX_NODE_HEAD_RUNLOCK(rnh);
1891 } else if (af != 0)
1892 error = EAFNOSUPPORT;
1893 }
1894 break;
1895
1896 case NET_RT_IFLIST:
1897 case NET_RT_IFLISTL:
1898 error = sysctl_iflist(af, &w);
1899 break;
1900
1901 case NET_RT_IFMALIST:
1902 error = sysctl_ifmalist(af, &w);
1903 break;
1904 }
1905 if (w.w_tmem)
1906 free(w.w_tmem, M_RTABLE);
1907 return (error);
1908}
1909
1910static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1911
1912/*
1913 * Definitions of protocols supported in the ROUTE domain.
1914 */
1915
1916static struct domain routedomain; /* or at least forward */
1917
1918static struct protosw routesw[] = {
1919{
1920 .pr_type = SOCK_RAW,
1921 .pr_domain = &routedomain,
1922 .pr_flags = PR_ATOMIC|PR_ADDR,
1923 .pr_output = route_output,
1924 .pr_ctlinput = raw_ctlinput,
1925 .pr_init = raw_init,
1926 .pr_usrreqs = &route_usrreqs
1927}
1928};
1929
1930static struct domain routedomain = {
1931 .dom_family = PF_ROUTE,
1932 .dom_name = "route",
1933 .dom_protosw = routesw,
1934 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1935};
1936
1937VNET_DOMAIN_SET(route);
339 return 0;
340}
341
342static int
343rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
344{
345
346 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
347}
348
349static int
350rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
351{
352
353 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
354}
355
356/* pru_connect2 is EOPNOTSUPP */
357/* pru_control is EOPNOTSUPP */
358
359static void
360rts_detach(struct socket *so)
361{
362 struct rawcb *rp = sotorawcb(so);
363
364 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
365
366 RTSOCK_LOCK();
367 switch(rp->rcb_proto.sp_protocol) {
368 case AF_INET:
369 route_cb.ip_count--;
370 break;
371 case AF_INET6:
372 route_cb.ip6_count--;
373 break;
374 case AF_IPX:
375 route_cb.ipx_count--;
376 break;
377 }
378 route_cb.any_count--;
379 RTSOCK_UNLOCK();
380 raw_usrreqs.pru_detach(so);
381}
382
383static int
384rts_disconnect(struct socket *so)
385{
386
387 return (raw_usrreqs.pru_disconnect(so));
388}
389
390/* pru_listen is EOPNOTSUPP */
391
392static int
393rts_peeraddr(struct socket *so, struct sockaddr **nam)
394{
395
396 return (raw_usrreqs.pru_peeraddr(so, nam));
397}
398
399/* pru_rcvd is EOPNOTSUPP */
400/* pru_rcvoob is EOPNOTSUPP */
401
402static int
403rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
404 struct mbuf *control, struct thread *td)
405{
406
407 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
408}
409
410/* pru_sense is null */
411
412static int
413rts_shutdown(struct socket *so)
414{
415
416 return (raw_usrreqs.pru_shutdown(so));
417}
418
419static int
420rts_sockaddr(struct socket *so, struct sockaddr **nam)
421{
422
423 return (raw_usrreqs.pru_sockaddr(so, nam));
424}
425
426static struct pr_usrreqs route_usrreqs = {
427 .pru_abort = rts_abort,
428 .pru_attach = rts_attach,
429 .pru_bind = rts_bind,
430 .pru_connect = rts_connect,
431 .pru_detach = rts_detach,
432 .pru_disconnect = rts_disconnect,
433 .pru_peeraddr = rts_peeraddr,
434 .pru_send = rts_send,
435 .pru_shutdown = rts_shutdown,
436 .pru_sockaddr = rts_sockaddr,
437 .pru_close = rts_close,
438};
439
440#ifndef _SOCKADDR_UNION_DEFINED
441#define _SOCKADDR_UNION_DEFINED
442/*
443 * The union of all possible address formats we handle.
444 */
445union sockaddr_union {
446 struct sockaddr sa;
447 struct sockaddr_in sin;
448 struct sockaddr_in6 sin6;
449};
450#endif /* _SOCKADDR_UNION_DEFINED */
451
452static int
453rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
454 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
455{
456
457 /* First, see if the returned address is part of the jail. */
458 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
459 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
460 return (0);
461 }
462
463 switch (info->rti_info[RTAX_DST]->sa_family) {
464#ifdef INET
465 case AF_INET:
466 {
467 struct in_addr ia;
468 struct ifaddr *ifa;
469 int found;
470
471 found = 0;
472 /*
473 * Try to find an address on the given outgoing interface
474 * that belongs to the jail.
475 */
476 IF_ADDR_RLOCK(ifp);
477 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
478 struct sockaddr *sa;
479 sa = ifa->ifa_addr;
480 if (sa->sa_family != AF_INET)
481 continue;
482 ia = ((struct sockaddr_in *)sa)->sin_addr;
483 if (prison_check_ip4(cred, &ia) == 0) {
484 found = 1;
485 break;
486 }
487 }
488 IF_ADDR_RUNLOCK(ifp);
489 if (!found) {
490 /*
491 * As a last resort return the 'default' jail address.
492 */
493 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
494 sin_addr;
495 if (prison_get_ip4(cred, &ia) != 0)
496 return (ESRCH);
497 }
498 bzero(&saun->sin, sizeof(struct sockaddr_in));
499 saun->sin.sin_len = sizeof(struct sockaddr_in);
500 saun->sin.sin_family = AF_INET;
501 saun->sin.sin_addr.s_addr = ia.s_addr;
502 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
503 break;
504 }
505#endif
506#ifdef INET6
507 case AF_INET6:
508 {
509 struct in6_addr ia6;
510 struct ifaddr *ifa;
511 int found;
512
513 found = 0;
514 /*
515 * Try to find an address on the given outgoing interface
516 * that belongs to the jail.
517 */
518 IF_ADDR_RLOCK(ifp);
519 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
520 struct sockaddr *sa;
521 sa = ifa->ifa_addr;
522 if (sa->sa_family != AF_INET6)
523 continue;
524 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
525 &ia6, sizeof(struct in6_addr));
526 if (prison_check_ip6(cred, &ia6) == 0) {
527 found = 1;
528 break;
529 }
530 }
531 IF_ADDR_RUNLOCK(ifp);
532 if (!found) {
533 /*
534 * As a last resort return the 'default' jail address.
535 */
536 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
537 sin6_addr;
538 if (prison_get_ip6(cred, &ia6) != 0)
539 return (ESRCH);
540 }
541 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
542 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
543 saun->sin6.sin6_family = AF_INET6;
544 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
545 if (sa6_recoverscope(&saun->sin6) != 0)
546 return (ESRCH);
547 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
548 break;
549 }
550#endif
551 default:
552 return (ESRCH);
553 }
554 return (0);
555}
556
557/*ARGSUSED*/
558static int
559route_output(struct mbuf *m, struct socket *so)
560{
561#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
562 struct rt_msghdr *rtm = NULL;
563 struct rtentry *rt = NULL;
564 struct radix_node_head *rnh;
565 struct rt_addrinfo info;
566 int len, error = 0;
567 struct ifnet *ifp = NULL;
568 union sockaddr_union saun;
569 sa_family_t saf = AF_UNSPEC;
570
571#define senderr(e) { error = e; goto flush;}
572 if (m == NULL || ((m->m_len < sizeof(long)) &&
573 (m = m_pullup(m, sizeof(long))) == NULL))
574 return (ENOBUFS);
575 if ((m->m_flags & M_PKTHDR) == 0)
576 panic("route_output");
577 len = m->m_pkthdr.len;
578 if (len < sizeof(*rtm) ||
579 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
580 info.rti_info[RTAX_DST] = NULL;
581 senderr(EINVAL);
582 }
583 R_Malloc(rtm, struct rt_msghdr *, len);
584 if (rtm == NULL) {
585 info.rti_info[RTAX_DST] = NULL;
586 senderr(ENOBUFS);
587 }
588 m_copydata(m, 0, len, (caddr_t)rtm);
589 if (rtm->rtm_version != RTM_VERSION) {
590 info.rti_info[RTAX_DST] = NULL;
591 senderr(EPROTONOSUPPORT);
592 }
593 rtm->rtm_pid = curproc->p_pid;
594 bzero(&info, sizeof(info));
595 info.rti_addrs = rtm->rtm_addrs;
596 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
597 info.rti_info[RTAX_DST] = NULL;
598 senderr(EINVAL);
599 }
600 info.rti_flags = rtm->rtm_flags;
601 if (info.rti_info[RTAX_DST] == NULL ||
602 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
603 (info.rti_info[RTAX_GATEWAY] != NULL &&
604 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
605 senderr(EINVAL);
606 saf = info.rti_info[RTAX_DST]->sa_family;
607 /*
608 * Verify that the caller has the appropriate privilege; RTM_GET
609 * is the only operation the non-superuser is allowed.
610 */
611 if (rtm->rtm_type != RTM_GET) {
612 error = priv_check(curthread, PRIV_NET_ROUTE);
613 if (error)
614 senderr(error);
615 }
616
617 /*
618 * The given gateway address may be an interface address.
619 * For example, issuing a "route change" command on a route
620 * entry that was created from a tunnel, and the gateway
621 * address given is the local end point. In this case the
622 * RTF_GATEWAY flag must be cleared or the destination will
623 * not be reachable even though there is no error message.
624 */
625 if (info.rti_info[RTAX_GATEWAY] != NULL &&
626 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
627 struct route gw_ro;
628
629 bzero(&gw_ro, sizeof(gw_ro));
630 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
631 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
632 /*
633 * A host route through the loopback interface is
634 * installed for each interface adddress. In pre 8.0
635 * releases the interface address of a PPP link type
636 * is not reachable locally. This behavior is fixed as
637 * part of the new L2/L3 redesign and rewrite work. The
638 * signature of this interface address route is the
639 * AF_LINK sa_family type of the rt_gateway, and the
640 * rt_ifp has the IFF_LOOPBACK flag set.
641 */
642 if (gw_ro.ro_rt != NULL &&
643 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
644 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
645 info.rti_flags &= ~RTF_GATEWAY;
646 if (gw_ro.ro_rt != NULL)
647 RTFREE(gw_ro.ro_rt);
648 }
649
650 switch (rtm->rtm_type) {
651 struct rtentry *saved_nrt;
652
653 case RTM_ADD:
654 if (info.rti_info[RTAX_GATEWAY] == NULL)
655 senderr(EINVAL);
656 saved_nrt = NULL;
657
658 /* support for new ARP code */
659 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
660 (rtm->rtm_flags & RTF_LLDATA) != 0) {
661 error = lla_rt_output(rtm, &info);
662 break;
663 }
664 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
665 so->so_fibnum);
666 if (error == 0 && saved_nrt) {
667 RT_LOCK(saved_nrt);
668 rt_setmetrics(rtm->rtm_inits,
669 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
670 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
671 RT_REMREF(saved_nrt);
672 RT_UNLOCK(saved_nrt);
673 }
674 break;
675
676 case RTM_DELETE:
677 saved_nrt = NULL;
678 /* support for new ARP code */
679 if (info.rti_info[RTAX_GATEWAY] &&
680 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
681 (rtm->rtm_flags & RTF_LLDATA) != 0) {
682 error = lla_rt_output(rtm, &info);
683 break;
684 }
685 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
686 so->so_fibnum);
687 if (error == 0) {
688 RT_LOCK(saved_nrt);
689 rt = saved_nrt;
690 goto report;
691 }
692 break;
693
694 case RTM_GET:
695 case RTM_CHANGE:
696 case RTM_LOCK:
697 rnh = rt_tables_get_rnh(so->so_fibnum,
698 info.rti_info[RTAX_DST]->sa_family);
699 if (rnh == NULL)
700 senderr(EAFNOSUPPORT);
701 RADIX_NODE_HEAD_RLOCK(rnh);
702 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
703 info.rti_info[RTAX_NETMASK], rnh);
704 if (rt == NULL) { /* XXX looks bogus */
705 RADIX_NODE_HEAD_RUNLOCK(rnh);
706 senderr(ESRCH);
707 }
708#ifdef RADIX_MPATH
709 /*
710 * for RTM_CHANGE/LOCK, if we got multipath routes,
711 * we require users to specify a matching RTAX_GATEWAY.
712 *
713 * for RTM_GET, gate is optional even with multipath.
714 * if gate == NULL the first match is returned.
715 * (no need to call rt_mpath_matchgate if gate == NULL)
716 */
717 if (rn_mpath_capable(rnh) &&
718 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
719 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
720 if (!rt) {
721 RADIX_NODE_HEAD_RUNLOCK(rnh);
722 senderr(ESRCH);
723 }
724 }
725#endif
726 /*
727 * If performing proxied L2 entry insertion, and
728 * the actual PPP host entry is found, perform
729 * another search to retrieve the prefix route of
730 * the local end point of the PPP link.
731 */
732 if (rtm->rtm_flags & RTF_ANNOUNCE) {
733 struct sockaddr laddr;
734
735 if (rt->rt_ifp != NULL &&
736 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
737 struct ifaddr *ifa;
738
739 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
740 if (ifa != NULL)
741 rt_maskedcopy(ifa->ifa_addr,
742 &laddr,
743 ifa->ifa_netmask);
744 } else
745 rt_maskedcopy(rt->rt_ifa->ifa_addr,
746 &laddr,
747 rt->rt_ifa->ifa_netmask);
748 /*
749 * refactor rt and no lock operation necessary
750 */
751 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
752 if (rt == NULL) {
753 RADIX_NODE_HEAD_RUNLOCK(rnh);
754 senderr(ESRCH);
755 }
756 }
757 RT_LOCK(rt);
758 RT_ADDREF(rt);
759 RADIX_NODE_HEAD_RUNLOCK(rnh);
760
761 /*
762 * Fix for PR: 82974
763 *
764 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
765 * returns a perfect match in case a netmask is
766 * specified. For host routes only a longest prefix
767 * match is returned so it is necessary to compare the
768 * existence of the netmask. If both have a netmask
769 * rnh_lookup() did a perfect match and if none of them
770 * have a netmask both are host routes which is also a
771 * perfect match.
772 */
773
774 if (rtm->rtm_type != RTM_GET &&
775 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
776 RT_UNLOCK(rt);
777 senderr(ESRCH);
778 }
779
780 switch(rtm->rtm_type) {
781
782 case RTM_GET:
783 report:
784 RT_LOCK_ASSERT(rt);
785 if ((rt->rt_flags & RTF_HOST) == 0
786 ? jailed_without_vnet(curthread->td_ucred)
787 : prison_if(curthread->td_ucred,
788 rt_key(rt)) != 0) {
789 RT_UNLOCK(rt);
790 senderr(ESRCH);
791 }
792 info.rti_info[RTAX_DST] = rt_key(rt);
793 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
794 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
795 info.rti_info[RTAX_GENMASK] = 0;
796 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
797 ifp = rt->rt_ifp;
798 if (ifp) {
799 info.rti_info[RTAX_IFP] =
800 ifp->if_addr->ifa_addr;
801 error = rtm_get_jailed(&info, ifp, rt,
802 &saun, curthread->td_ucred);
803 if (error != 0) {
804 RT_UNLOCK(rt);
805 senderr(error);
806 }
807 if (ifp->if_flags & IFF_POINTOPOINT)
808 info.rti_info[RTAX_BRD] =
809 rt->rt_ifa->ifa_dstaddr;
810 rtm->rtm_index = ifp->if_index;
811 } else {
812 info.rti_info[RTAX_IFP] = NULL;
813 info.rti_info[RTAX_IFA] = NULL;
814 }
815 } else if ((ifp = rt->rt_ifp) != NULL) {
816 rtm->rtm_index = ifp->if_index;
817 }
818 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
819 if (len > rtm->rtm_msglen) {
820 struct rt_msghdr *new_rtm;
821 R_Malloc(new_rtm, struct rt_msghdr *, len);
822 if (new_rtm == NULL) {
823 RT_UNLOCK(rt);
824 senderr(ENOBUFS);
825 }
826 bcopy(rtm, new_rtm, rtm->rtm_msglen);
827 Free(rtm); rtm = new_rtm;
828 }
829 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
830 rtm->rtm_flags = rt->rt_flags;
831 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
832 rtm->rtm_addrs = info.rti_addrs;
833 break;
834
835 case RTM_CHANGE:
836 /*
837 * New gateway could require new ifaddr, ifp;
838 * flags may also be different; ifp may be specified
839 * by ll sockaddr when protocol address is ambiguous
840 */
841 if (((rt->rt_flags & RTF_GATEWAY) &&
842 info.rti_info[RTAX_GATEWAY] != NULL) ||
843 info.rti_info[RTAX_IFP] != NULL ||
844 (info.rti_info[RTAX_IFA] != NULL &&
845 !sa_equal(info.rti_info[RTAX_IFA],
846 rt->rt_ifa->ifa_addr))) {
847 RT_UNLOCK(rt);
848 RADIX_NODE_HEAD_LOCK(rnh);
849 error = rt_getifa_fib(&info, rt->rt_fibnum);
850 /*
851 * XXXRW: Really we should release this
852 * reference later, but this maintains
853 * historical behavior.
854 */
855 if (info.rti_ifa != NULL)
856 ifa_free(info.rti_ifa);
857 RADIX_NODE_HEAD_UNLOCK(rnh);
858 if (error != 0)
859 senderr(error);
860 RT_LOCK(rt);
861 }
862 if (info.rti_ifa != NULL &&
863 info.rti_ifa != rt->rt_ifa &&
864 rt->rt_ifa != NULL &&
865 rt->rt_ifa->ifa_rtrequest != NULL) {
866 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
867 &info);
868 ifa_free(rt->rt_ifa);
869 }
870 if (info.rti_info[RTAX_GATEWAY] != NULL) {
871 RT_UNLOCK(rt);
872 RADIX_NODE_HEAD_LOCK(rnh);
873 RT_LOCK(rt);
874
875 error = rt_setgate(rt, rt_key(rt),
876 info.rti_info[RTAX_GATEWAY]);
877 RADIX_NODE_HEAD_UNLOCK(rnh);
878 if (error != 0) {
879 RT_UNLOCK(rt);
880 senderr(error);
881 }
882 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
883 }
884 if (info.rti_ifa != NULL &&
885 info.rti_ifa != rt->rt_ifa) {
886 ifa_ref(info.rti_ifa);
887 rt->rt_ifa = info.rti_ifa;
888 rt->rt_ifp = info.rti_ifp;
889 }
890 /* Allow some flags to be toggled on change. */
891 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
892 (rtm->rtm_flags & RTF_FMASK);
893 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
894 &rt->rt_rmx);
895 rtm->rtm_index = rt->rt_ifp->if_index;
896 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
897 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
898 /* FALLTHROUGH */
899 case RTM_LOCK:
900 /* We don't support locks anymore */
901 break;
902 }
903 RT_UNLOCK(rt);
904 break;
905
906 default:
907 senderr(EOPNOTSUPP);
908 }
909
910flush:
911 if (rtm) {
912 if (error)
913 rtm->rtm_errno = error;
914 else
915 rtm->rtm_flags |= RTF_DONE;
916 }
917 if (rt) /* XXX can this be true? */
918 RTFREE(rt);
919 {
920 struct rawcb *rp = NULL;
921 /*
922 * Check to see if we don't want our own messages.
923 */
924 if ((so->so_options & SO_USELOOPBACK) == 0) {
925 if (route_cb.any_count <= 1) {
926 if (rtm)
927 Free(rtm);
928 m_freem(m);
929 return (error);
930 }
931 /* There is another listener, so construct message */
932 rp = sotorawcb(so);
933 }
934 if (rtm) {
935 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
936 if (m->m_pkthdr.len < rtm->rtm_msglen) {
937 m_freem(m);
938 m = NULL;
939 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
940 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
941 }
942 if (m) {
943 M_SETFIB(m, so->so_fibnum);
944 m->m_flags |= RTS_FILTER_FIB;
945 if (rp) {
946 /*
947 * XXX insure we don't get a copy by
948 * invalidating our protocol
949 */
950 unsigned short family = rp->rcb_proto.sp_family;
951 rp->rcb_proto.sp_family = 0;
952 rt_dispatch(m, saf);
953 rp->rcb_proto.sp_family = family;
954 } else
955 rt_dispatch(m, saf);
956 }
957 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
958 if (rtm)
959 Free(rtm);
960 }
961 return (error);
962#undef sa_equal
963}
964
965static void
966rt_setmetrics(u_long which, const struct rt_metrics *in,
967 struct rt_metrics_lite *out)
968{
969#define metric(f, e) if (which & (f)) out->e = in->e;
970 /*
971 * Only these are stored in the routing entry since introduction
972 * of tcp hostcache. The rest is ignored.
973 */
974 metric(RTV_MTU, rmx_mtu);
975 metric(RTV_WEIGHT, rmx_weight);
976 /* Userland -> kernel timebase conversion. */
977 if (which & RTV_EXPIRE)
978 out->rmx_expire = in->rmx_expire ?
979 in->rmx_expire - time_second + time_uptime : 0;
980#undef metric
981}
982
983static void
984rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
985{
986#define metric(e) out->e = in->e;
987 bzero(out, sizeof(*out));
988 metric(rmx_mtu);
989 metric(rmx_weight);
990 /* Kernel -> userland timebase conversion. */
991 out->rmx_expire = in->rmx_expire ?
992 in->rmx_expire - time_uptime + time_second : 0;
993#undef metric
994}
995
996/*
997 * Extract the addresses of the passed sockaddrs.
998 * Do a little sanity checking so as to avoid bad memory references.
999 * This data is derived straight from userland.
1000 */
1001static int
1002rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1003{
1004 struct sockaddr *sa;
1005 int i;
1006
1007 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1008 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1009 continue;
1010 sa = (struct sockaddr *)cp;
1011 /*
1012 * It won't fit.
1013 */
1014 if (cp + sa->sa_len > cplim)
1015 return (EINVAL);
1016 /*
1017 * there are no more.. quit now
1018 * If there are more bits, they are in error.
1019 * I've seen this. route(1) can evidently generate these.
1020 * This causes kernel to core dump.
1021 * for compatibility, If we see this, point to a safe address.
1022 */
1023 if (sa->sa_len == 0) {
1024 rtinfo->rti_info[i] = &sa_zero;
1025 return (0); /* should be EINVAL but for compat */
1026 }
1027 /* accept it */
1028 rtinfo->rti_info[i] = sa;
1029 cp += SA_SIZE(sa);
1030 }
1031 return (0);
1032}
1033
1034/*
1035 * Used by the routing socket.
1036 */
1037static struct mbuf *
1038rt_msg1(int type, struct rt_addrinfo *rtinfo)
1039{
1040 struct rt_msghdr *rtm;
1041 struct mbuf *m;
1042 int i;
1043 struct sockaddr *sa;
1044 int len, dlen;
1045
1046 switch (type) {
1047
1048 case RTM_DELADDR:
1049 case RTM_NEWADDR:
1050 len = sizeof(struct ifa_msghdr);
1051 break;
1052
1053 case RTM_DELMADDR:
1054 case RTM_NEWMADDR:
1055 len = sizeof(struct ifma_msghdr);
1056 break;
1057
1058 case RTM_IFINFO:
1059 len = sizeof(struct if_msghdr);
1060 break;
1061
1062 case RTM_IFANNOUNCE:
1063 case RTM_IEEE80211:
1064 len = sizeof(struct if_announcemsghdr);
1065 break;
1066
1067 default:
1068 len = sizeof(struct rt_msghdr);
1069 }
1070 if (len > MCLBYTES)
1071 panic("rt_msg1");
1072 m = m_gethdr(M_DONTWAIT, MT_DATA);
1073 if (m && len > MHLEN) {
1074 MCLGET(m, M_DONTWAIT);
1075 if ((m->m_flags & M_EXT) == 0) {
1076 m_free(m);
1077 m = NULL;
1078 }
1079 }
1080 if (m == NULL)
1081 return (m);
1082 m->m_pkthdr.len = m->m_len = len;
1083 m->m_pkthdr.rcvif = NULL;
1084 rtm = mtod(m, struct rt_msghdr *);
1085 bzero((caddr_t)rtm, len);
1086 for (i = 0; i < RTAX_MAX; i++) {
1087 if ((sa = rtinfo->rti_info[i]) == NULL)
1088 continue;
1089 rtinfo->rti_addrs |= (1 << i);
1090 dlen = SA_SIZE(sa);
1091 m_copyback(m, len, dlen, (caddr_t)sa);
1092 len += dlen;
1093 }
1094 if (m->m_pkthdr.len != len) {
1095 m_freem(m);
1096 return (NULL);
1097 }
1098 rtm->rtm_msglen = len;
1099 rtm->rtm_version = RTM_VERSION;
1100 rtm->rtm_type = type;
1101 return (m);
1102}
1103
1104/*
1105 * Used by the sysctl code and routing socket.
1106 */
1107static int
1108rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1109{
1110 int i;
1111 int len, dlen, second_time = 0;
1112 caddr_t cp0;
1113
1114 rtinfo->rti_addrs = 0;
1115again:
1116 switch (type) {
1117
1118 case RTM_DELADDR:
1119 case RTM_NEWADDR:
1120 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1121#ifdef COMPAT_FREEBSD32
1122 if (w->w_req->flags & SCTL_MASK32)
1123 len = sizeof(struct ifa_msghdrl32);
1124 else
1125#endif
1126 len = sizeof(struct ifa_msghdrl);
1127 } else
1128 len = sizeof(struct ifa_msghdr);
1129 break;
1130
1131 case RTM_IFINFO:
1132#ifdef COMPAT_FREEBSD32
1133 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1134 if (w->w_op == NET_RT_IFLISTL)
1135 len = sizeof(struct if_msghdrl32);
1136 else
1137 len = sizeof(struct if_msghdr32);
1138 break;
1139 }
1140#endif
1141 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1142 len = sizeof(struct if_msghdrl);
1143 else
1144 len = sizeof(struct if_msghdr);
1145 break;
1146
1147 case RTM_NEWMADDR:
1148 len = sizeof(struct ifma_msghdr);
1149 break;
1150
1151 default:
1152 len = sizeof(struct rt_msghdr);
1153 }
1154 cp0 = cp;
1155 if (cp0)
1156 cp += len;
1157 for (i = 0; i < RTAX_MAX; i++) {
1158 struct sockaddr *sa;
1159
1160 if ((sa = rtinfo->rti_info[i]) == NULL)
1161 continue;
1162 rtinfo->rti_addrs |= (1 << i);
1163 dlen = SA_SIZE(sa);
1164 if (cp) {
1165 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1166 cp += dlen;
1167 }
1168 len += dlen;
1169 }
1170 len = ALIGN(len);
1171 if (cp == NULL && w != NULL && !second_time) {
1172 struct walkarg *rw = w;
1173
1174 if (rw->w_req) {
1175 if (rw->w_tmemsize < len) {
1176 if (rw->w_tmem)
1177 free(rw->w_tmem, M_RTABLE);
1178 rw->w_tmem = (caddr_t)
1179 malloc(len, M_RTABLE, M_NOWAIT);
1180 if (rw->w_tmem)
1181 rw->w_tmemsize = len;
1182 }
1183 if (rw->w_tmem) {
1184 cp = rw->w_tmem;
1185 second_time = 1;
1186 goto again;
1187 }
1188 }
1189 }
1190 if (cp) {
1191 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1192
1193 rtm->rtm_version = RTM_VERSION;
1194 rtm->rtm_type = type;
1195 rtm->rtm_msglen = len;
1196 }
1197 return (len);
1198}
1199
1200/*
1201 * This routine is called to generate a message from the routing
1202 * socket indicating that a redirect has occured, a routing lookup
1203 * has failed, or that a protocol has detected timeouts to a particular
1204 * destination.
1205 */
1206void
1207rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1208 int fibnum)
1209{
1210 struct rt_msghdr *rtm;
1211 struct mbuf *m;
1212 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1213
1214 if (route_cb.any_count == 0)
1215 return;
1216 m = rt_msg1(type, rtinfo);
1217 if (m == NULL)
1218 return;
1219
1220 if (fibnum != RTS_ALLFIBS) {
1221 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1222 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1223 M_SETFIB(m, fibnum);
1224 m->m_flags |= RTS_FILTER_FIB;
1225 }
1226
1227 rtm = mtod(m, struct rt_msghdr *);
1228 rtm->rtm_flags = RTF_DONE | flags;
1229 rtm->rtm_errno = error;
1230 rtm->rtm_addrs = rtinfo->rti_addrs;
1231 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1232}
1233
1234void
1235rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1236{
1237
1238 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1239}
1240
1241/*
1242 * This routine is called to generate a message from the routing
1243 * socket indicating that the status of a network interface has changed.
1244 */
1245void
1246rt_ifmsg(struct ifnet *ifp)
1247{
1248 struct if_msghdr *ifm;
1249 struct mbuf *m;
1250 struct rt_addrinfo info;
1251
1252 if (route_cb.any_count == 0)
1253 return;
1254 bzero((caddr_t)&info, sizeof(info));
1255 m = rt_msg1(RTM_IFINFO, &info);
1256 if (m == NULL)
1257 return;
1258 ifm = mtod(m, struct if_msghdr *);
1259 ifm->ifm_index = ifp->if_index;
1260 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1261 ifm->ifm_data = ifp->if_data;
1262 ifm->ifm_addrs = 0;
1263 rt_dispatch(m, AF_UNSPEC);
1264}
1265
1266/*
1267 * This is called to generate messages from the routing socket
1268 * indicating a network interface has had addresses associated with it.
1269 * if we ever reverse the logic and replace messages TO the routing
1270 * socket indicate a request to configure interfaces, then it will
1271 * be unnecessary as the routing socket will automatically generate
1272 * copies of it.
1273 */
1274void
1275rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1276 int fibnum)
1277{
1278 struct rt_addrinfo info;
1279 struct sockaddr *sa = NULL;
1280 int pass;
1281 struct mbuf *m = NULL;
1282 struct ifnet *ifp = ifa->ifa_ifp;
1283
1284 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1285 ("unexpected cmd %u", cmd));
1286#if defined(INET) || defined(INET6)
1287#ifdef SCTP
1288 /*
1289 * notify the SCTP stack
1290 * this will only get called when an address is added/deleted
1291 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1292 */
1293 sctp_addr_change(ifa, cmd);
1294#endif /* SCTP */
1295#endif
1296 if (route_cb.any_count == 0)
1297 return;
1298 for (pass = 1; pass < 3; pass++) {
1299 bzero((caddr_t)&info, sizeof(info));
1300 if ((cmd == RTM_ADD && pass == 1) ||
1301 (cmd == RTM_DELETE && pass == 2)) {
1302 struct ifa_msghdr *ifam;
1303 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1304
1305 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1306 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1307 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1308 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1309 if ((m = rt_msg1(ncmd, &info)) == NULL)
1310 continue;
1311 ifam = mtod(m, struct ifa_msghdr *);
1312 ifam->ifam_index = ifp->if_index;
1313 ifam->ifam_metric = ifa->ifa_metric;
1314 ifam->ifam_flags = ifa->ifa_flags;
1315 ifam->ifam_addrs = info.rti_addrs;
1316 }
1317 if ((cmd == RTM_ADD && pass == 2) ||
1318 (cmd == RTM_DELETE && pass == 1)) {
1319 struct rt_msghdr *rtm;
1320
1321 if (rt == NULL)
1322 continue;
1323 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1324 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1325 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1326 if ((m = rt_msg1(cmd, &info)) == NULL)
1327 continue;
1328 rtm = mtod(m, struct rt_msghdr *);
1329 rtm->rtm_index = ifp->if_index;
1330 rtm->rtm_flags |= rt->rt_flags;
1331 rtm->rtm_errno = error;
1332 rtm->rtm_addrs = info.rti_addrs;
1333 }
1334 if (fibnum != RTS_ALLFIBS) {
1335 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1336 "fibnum out of range 0 <= %d < %d", __func__,
1337 fibnum, rt_numfibs));
1338 M_SETFIB(m, fibnum);
1339 m->m_flags |= RTS_FILTER_FIB;
1340 }
1341 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1342 }
1343}
1344
1345void
1346rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1347{
1348
1349 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1350}
1351
1352/*
1353 * This is the analogue to the rt_newaddrmsg which performs the same
1354 * function but for multicast group memberhips. This is easier since
1355 * there is no route state to worry about.
1356 */
1357void
1358rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1359{
1360 struct rt_addrinfo info;
1361 struct mbuf *m = NULL;
1362 struct ifnet *ifp = ifma->ifma_ifp;
1363 struct ifma_msghdr *ifmam;
1364
1365 if (route_cb.any_count == 0)
1366 return;
1367
1368 bzero((caddr_t)&info, sizeof(info));
1369 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1370 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1371 /*
1372 * If a link-layer address is present, present it as a ``gateway''
1373 * (similarly to how ARP entries, e.g., are presented).
1374 */
1375 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1376 m = rt_msg1(cmd, &info);
1377 if (m == NULL)
1378 return;
1379 ifmam = mtod(m, struct ifma_msghdr *);
1380 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1381 __func__));
1382 ifmam->ifmam_index = ifp->if_index;
1383 ifmam->ifmam_addrs = info.rti_addrs;
1384 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1385}
1386
1387static struct mbuf *
1388rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1389 struct rt_addrinfo *info)
1390{
1391 struct if_announcemsghdr *ifan;
1392 struct mbuf *m;
1393
1394 if (route_cb.any_count == 0)
1395 return NULL;
1396 bzero((caddr_t)info, sizeof(*info));
1397 m = rt_msg1(type, info);
1398 if (m != NULL) {
1399 ifan = mtod(m, struct if_announcemsghdr *);
1400 ifan->ifan_index = ifp->if_index;
1401 strlcpy(ifan->ifan_name, ifp->if_xname,
1402 sizeof(ifan->ifan_name));
1403 ifan->ifan_what = what;
1404 }
1405 return m;
1406}
1407
1408/*
1409 * This is called to generate routing socket messages indicating
1410 * IEEE80211 wireless events.
1411 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1412 */
1413void
1414rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1415{
1416 struct mbuf *m;
1417 struct rt_addrinfo info;
1418
1419 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1420 if (m != NULL) {
1421 /*
1422 * Append the ieee80211 data. Try to stick it in the
1423 * mbuf containing the ifannounce msg; otherwise allocate
1424 * a new mbuf and append.
1425 *
1426 * NB: we assume m is a single mbuf.
1427 */
1428 if (data_len > M_TRAILINGSPACE(m)) {
1429 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1430 if (n == NULL) {
1431 m_freem(m);
1432 return;
1433 }
1434 bcopy(data, mtod(n, void *), data_len);
1435 n->m_len = data_len;
1436 m->m_next = n;
1437 } else if (data_len > 0) {
1438 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1439 m->m_len += data_len;
1440 }
1441 if (m->m_flags & M_PKTHDR)
1442 m->m_pkthdr.len += data_len;
1443 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1444 rt_dispatch(m, AF_UNSPEC);
1445 }
1446}
1447
1448/*
1449 * This is called to generate routing socket messages indicating
1450 * network interface arrival and departure.
1451 */
1452void
1453rt_ifannouncemsg(struct ifnet *ifp, int what)
1454{
1455 struct mbuf *m;
1456 struct rt_addrinfo info;
1457
1458 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1459 if (m != NULL)
1460 rt_dispatch(m, AF_UNSPEC);
1461}
1462
1463static void
1464rt_dispatch(struct mbuf *m, sa_family_t saf)
1465{
1466 struct m_tag *tag;
1467
1468 /*
1469 * Preserve the family from the sockaddr, if any, in an m_tag for
1470 * use when injecting the mbuf into the routing socket buffer from
1471 * the netisr.
1472 */
1473 if (saf != AF_UNSPEC) {
1474 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1475 M_NOWAIT);
1476 if (tag == NULL) {
1477 m_freem(m);
1478 return;
1479 }
1480 *(unsigned short *)(tag + 1) = saf;
1481 m_tag_prepend(m, tag);
1482 }
1483#ifdef VIMAGE
1484 if (V_loif)
1485 m->m_pkthdr.rcvif = V_loif;
1486 else {
1487 m_freem(m);
1488 return;
1489 }
1490#endif
1491 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1492}
1493
1494/*
1495 * This is used in dumping the kernel table via sysctl().
1496 */
1497static int
1498sysctl_dumpentry(struct radix_node *rn, void *vw)
1499{
1500 struct walkarg *w = vw;
1501 struct rtentry *rt = (struct rtentry *)rn;
1502 int error = 0, size;
1503 struct rt_addrinfo info;
1504
1505 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1506 return 0;
1507 if ((rt->rt_flags & RTF_HOST) == 0
1508 ? jailed_without_vnet(w->w_req->td->td_ucred)
1509 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1510 return (0);
1511 bzero((caddr_t)&info, sizeof(info));
1512 info.rti_info[RTAX_DST] = rt_key(rt);
1513 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1514 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1515 info.rti_info[RTAX_GENMASK] = 0;
1516 if (rt->rt_ifp) {
1517 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1518 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1519 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1520 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1521 }
1522 size = rt_msg2(RTM_GET, &info, NULL, w);
1523 if (w->w_req && w->w_tmem) {
1524 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1525
1526 rtm->rtm_flags = rt->rt_flags;
1527 /*
1528 * let's be honest about this being a retarded hack
1529 */
1530 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1531 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1532 rtm->rtm_index = rt->rt_ifp->if_index;
1533 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1534 rtm->rtm_addrs = info.rti_addrs;
1535 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1536 return (error);
1537 }
1538 return (error);
1539}
1540
1541#ifdef COMPAT_FREEBSD32
1542static void
1543copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1544{
1545
1546 bzero(dst, sizeof(*dst));
1547 CP(*src, *dst, ifi_type);
1548 CP(*src, *dst, ifi_physical);
1549 CP(*src, *dst, ifi_addrlen);
1550 CP(*src, *dst, ifi_hdrlen);
1551 CP(*src, *dst, ifi_link_state);
1552 CP(*src, *dst, ifi_vhid);
1553 dst->ifi_datalen = sizeof(struct if_data32);
1554 CP(*src, *dst, ifi_mtu);
1555 CP(*src, *dst, ifi_metric);
1556 CP(*src, *dst, ifi_baudrate);
1557 CP(*src, *dst, ifi_ipackets);
1558 CP(*src, *dst, ifi_ierrors);
1559 CP(*src, *dst, ifi_opackets);
1560 CP(*src, *dst, ifi_oerrors);
1561 CP(*src, *dst, ifi_collisions);
1562 CP(*src, *dst, ifi_ibytes);
1563 CP(*src, *dst, ifi_obytes);
1564 CP(*src, *dst, ifi_imcasts);
1565 CP(*src, *dst, ifi_omcasts);
1566 CP(*src, *dst, ifi_iqdrops);
1567 CP(*src, *dst, ifi_noproto);
1568 CP(*src, *dst, ifi_hwassist);
1569 CP(*src, *dst, ifi_epoch);
1570 TV_CP(*src, *dst, ifi_lastchange);
1571}
1572#endif
1573
1574static int
1575sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1576 struct walkarg *w, int len)
1577{
1578 struct if_msghdrl *ifm;
1579
1580#ifdef COMPAT_FREEBSD32
1581 if (w->w_req->flags & SCTL_MASK32) {
1582 struct if_msghdrl32 *ifm32;
1583
1584 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1585 ifm32->ifm_addrs = info->rti_addrs;
1586 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1587 ifm32->ifm_index = ifp->if_index;
1588 ifm32->_ifm_spare1 = 0;
1589 ifm32->ifm_len = sizeof(*ifm32);
1590 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1591
1592 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1593 /* Fixup if_data carp(4) vhid. */
1594 if (carp_get_vhid_p != NULL)
1595 ifm32->ifm_data.ifi_vhid =
1596 (*carp_get_vhid_p)(ifp->if_addr);
1597
1598 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1599 }
1600#endif
1601 ifm = (struct if_msghdrl *)w->w_tmem;
1602 ifm->ifm_addrs = info->rti_addrs;
1603 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1604 ifm->ifm_index = ifp->if_index;
1605 ifm->_ifm_spare1 = 0;
1606 ifm->ifm_len = sizeof(*ifm);
1607 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1608
1609 ifm->ifm_data = ifp->if_data;
1610 /* Fixup if_data carp(4) vhid. */
1611 if (carp_get_vhid_p != NULL)
1612 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1613
1614 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1615}
1616
1617static int
1618sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1619 struct walkarg *w, int len)
1620{
1621 struct if_msghdr *ifm;
1622
1623#ifdef COMPAT_FREEBSD32
1624 if (w->w_req->flags & SCTL_MASK32) {
1625 struct if_msghdr32 *ifm32;
1626
1627 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1628 ifm32->ifm_addrs = info->rti_addrs;
1629 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1630 ifm32->ifm_index = ifp->if_index;
1631
1632 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1633 /* Fixup if_data carp(4) vhid. */
1634 if (carp_get_vhid_p != NULL)
1635 ifm32->ifm_data.ifi_vhid =
1636 (*carp_get_vhid_p)(ifp->if_addr);
1637
1638 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1639 }
1640#endif
1641 ifm = (struct if_msghdr *)w->w_tmem;
1642 ifm->ifm_addrs = info->rti_addrs;
1643 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1644 ifm->ifm_index = ifp->if_index;
1645
1646 ifm->ifm_data = ifp->if_data;
1647 /* Fixup if_data carp(4) vhid. */
1648 if (carp_get_vhid_p != NULL)
1649 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1650
1651 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1652}
1653
1654static int
1655sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1656 struct walkarg *w, int len)
1657{
1658 struct ifa_msghdrl *ifam;
1659
1660#ifdef COMPAT_FREEBSD32
1661 if (w->w_req->flags & SCTL_MASK32) {
1662 struct ifa_msghdrl32 *ifam32;
1663
1664 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1665 ifam32->ifam_addrs = info->rti_addrs;
1666 ifam32->ifam_flags = ifa->ifa_flags;
1667 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1668 ifam32->_ifam_spare1 = 0;
1669 ifam32->ifam_len = sizeof(*ifam32);
1670 ifam32->ifam_data_off =
1671 offsetof(struct ifa_msghdrl32, ifam_data);
1672 ifam32->ifam_metric = ifa->ifa_metric;
1673
1674 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1675 /* Fixup if_data carp(4) vhid. */
1676 if (carp_get_vhid_p != NULL)
1677 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1678
1679 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1680 }
1681#endif
1682
1683 ifam = (struct ifa_msghdrl *)w->w_tmem;
1684 ifam->ifam_addrs = info->rti_addrs;
1685 ifam->ifam_flags = ifa->ifa_flags;
1686 ifam->ifam_index = ifa->ifa_ifp->if_index;
1687 ifam->_ifam_spare1 = 0;
1688 ifam->ifam_len = sizeof(*ifam);
1689 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1690 ifam->ifam_metric = ifa->ifa_metric;
1691
1692 ifam->ifam_data = ifa->if_data;
1693 /* Fixup if_data carp(4) vhid. */
1694 if (carp_get_vhid_p != NULL)
1695 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1696
1697 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1698}
1699
1700static int
1701sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1702 struct walkarg *w, int len)
1703{
1704 struct ifa_msghdr *ifam;
1705
1706 ifam = (struct ifa_msghdr *)w->w_tmem;
1707 ifam->ifam_addrs = info->rti_addrs;
1708 ifam->ifam_flags = ifa->ifa_flags;
1709 ifam->ifam_index = ifa->ifa_ifp->if_index;
1710 ifam->ifam_metric = ifa->ifa_metric;
1711
1712 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1713}
1714
1715static int
1716sysctl_iflist(int af, struct walkarg *w)
1717{
1718 struct ifnet *ifp;
1719 struct ifaddr *ifa;
1720 struct rt_addrinfo info;
1721 int len, error = 0;
1722
1723 bzero((caddr_t)&info, sizeof(info));
1724 IFNET_RLOCK();
1725 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1726 if (w->w_arg && w->w_arg != ifp->if_index)
1727 continue;
1728 IF_ADDR_RLOCK(ifp);
1729 ifa = ifp->if_addr;
1730 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1731 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1732 info.rti_info[RTAX_IFP] = NULL;
1733 if (w->w_req && w->w_tmem) {
1734 if (w->w_op == NET_RT_IFLISTL)
1735 error = sysctl_iflist_ifml(ifp, &info, w, len);
1736 else
1737 error = sysctl_iflist_ifm(ifp, &info, w, len);
1738 if (error)
1739 goto done;
1740 }
1741 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1742 if (af && af != ifa->ifa_addr->sa_family)
1743 continue;
1744 if (prison_if(w->w_req->td->td_ucred,
1745 ifa->ifa_addr) != 0)
1746 continue;
1747 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1748 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1749 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1750 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1751 if (w->w_req && w->w_tmem) {
1752 if (w->w_op == NET_RT_IFLISTL)
1753 error = sysctl_iflist_ifaml(ifa, &info,
1754 w, len);
1755 else
1756 error = sysctl_iflist_ifam(ifa, &info,
1757 w, len);
1758 if (error)
1759 goto done;
1760 }
1761 }
1762 IF_ADDR_RUNLOCK(ifp);
1763 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1764 info.rti_info[RTAX_BRD] = NULL;
1765 }
1766done:
1767 if (ifp != NULL)
1768 IF_ADDR_RUNLOCK(ifp);
1769 IFNET_RUNLOCK();
1770 return (error);
1771}
1772
1773static int
1774sysctl_ifmalist(int af, struct walkarg *w)
1775{
1776 struct ifnet *ifp;
1777 struct ifmultiaddr *ifma;
1778 struct rt_addrinfo info;
1779 int len, error = 0;
1780 struct ifaddr *ifa;
1781
1782 bzero((caddr_t)&info, sizeof(info));
1783 IFNET_RLOCK();
1784 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1785 if (w->w_arg && w->w_arg != ifp->if_index)
1786 continue;
1787 ifa = ifp->if_addr;
1788 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1789 IF_ADDR_RLOCK(ifp);
1790 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1791 if (af && af != ifma->ifma_addr->sa_family)
1792 continue;
1793 if (prison_if(w->w_req->td->td_ucred,
1794 ifma->ifma_addr) != 0)
1795 continue;
1796 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1797 info.rti_info[RTAX_GATEWAY] =
1798 (ifma->ifma_addr->sa_family != AF_LINK) ?
1799 ifma->ifma_lladdr : NULL;
1800 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1801 if (w->w_req && w->w_tmem) {
1802 struct ifma_msghdr *ifmam;
1803
1804 ifmam = (struct ifma_msghdr *)w->w_tmem;
1805 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1806 ifmam->ifmam_flags = 0;
1807 ifmam->ifmam_addrs = info.rti_addrs;
1808 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1809 if (error) {
1810 IF_ADDR_RUNLOCK(ifp);
1811 goto done;
1812 }
1813 }
1814 }
1815 IF_ADDR_RUNLOCK(ifp);
1816 }
1817done:
1818 IFNET_RUNLOCK();
1819 return (error);
1820}
1821
1822static int
1823sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1824{
1825 int *name = (int *)arg1;
1826 u_int namelen = arg2;
1827 struct radix_node_head *rnh = NULL; /* silence compiler. */
1828 int i, lim, error = EINVAL;
1829 u_char af;
1830 struct walkarg w;
1831
1832 name ++;
1833 namelen--;
1834 if (req->newptr)
1835 return (EPERM);
1836 if (namelen != 3)
1837 return ((namelen < 3) ? EISDIR : ENOTDIR);
1838 af = name[0];
1839 if (af > AF_MAX)
1840 return (EINVAL);
1841 bzero(&w, sizeof(w));
1842 w.w_op = name[1];
1843 w.w_arg = name[2];
1844 w.w_req = req;
1845
1846 error = sysctl_wire_old_buffer(req, 0);
1847 if (error)
1848 return (error);
1849 switch (w.w_op) {
1850
1851 case NET_RT_DUMP:
1852 case NET_RT_FLAGS:
1853 if (af == 0) { /* dump all tables */
1854 i = 1;
1855 lim = AF_MAX;
1856 } else /* dump only one table */
1857 i = lim = af;
1858
1859 /*
1860 * take care of llinfo entries, the caller must
1861 * specify an AF
1862 */
1863 if (w.w_op == NET_RT_FLAGS &&
1864 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1865 if (af != 0)
1866 error = lltable_sysctl_dumparp(af, w.w_req);
1867 else
1868 error = EINVAL;
1869 break;
1870 }
1871 /*
1872 * take care of routing entries
1873 */
1874 for (error = 0; error == 0 && i <= lim; i++) {
1875 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1876 if (rnh != NULL) {
1877 RADIX_NODE_HEAD_RLOCK(rnh);
1878 error = rnh->rnh_walktree(rnh,
1879 sysctl_dumpentry, &w);
1880 RADIX_NODE_HEAD_RUNLOCK(rnh);
1881 } else if (af != 0)
1882 error = EAFNOSUPPORT;
1883 }
1884 break;
1885
1886 case NET_RT_IFLIST:
1887 case NET_RT_IFLISTL:
1888 error = sysctl_iflist(af, &w);
1889 break;
1890
1891 case NET_RT_IFMALIST:
1892 error = sysctl_ifmalist(af, &w);
1893 break;
1894 }
1895 if (w.w_tmem)
1896 free(w.w_tmem, M_RTABLE);
1897 return (error);
1898}
1899
1900static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1901
1902/*
1903 * Definitions of protocols supported in the ROUTE domain.
1904 */
1905
1906static struct domain routedomain; /* or at least forward */
1907
1908static struct protosw routesw[] = {
1909{
1910 .pr_type = SOCK_RAW,
1911 .pr_domain = &routedomain,
1912 .pr_flags = PR_ATOMIC|PR_ADDR,
1913 .pr_output = route_output,
1914 .pr_ctlinput = raw_ctlinput,
1915 .pr_init = raw_init,
1916 .pr_usrreqs = &route_usrreqs
1917}
1918};
1919
1920static struct domain routedomain = {
1921 .dom_family = PF_ROUTE,
1922 .dom_name = "route",
1923 .dom_protosw = routesw,
1924 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1925};
1926
1927VNET_DOMAIN_SET(route);