rtsock_shared.c revision 1.1.2.3 1 1.1.2.3 pgoyette /* $NetBSD: rtsock_shared.c,v 1.1.2.3 2019/01/15 04:10:34 pgoyette Exp $ */
2 1.1.2.1 pgoyette
3 1.1.2.1 pgoyette /*
4 1.1.2.1 pgoyette * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.1.2.1 pgoyette * All rights reserved.
6 1.1.2.1 pgoyette *
7 1.1.2.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.1 pgoyette * are met:
10 1.1.2.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.1 pgoyette * 3. Neither the name of the project nor the names of its contributors
16 1.1.2.1 pgoyette * may be used to endorse or promote products derived from this software
17 1.1.2.1 pgoyette * without specific prior written permission.
18 1.1.2.1 pgoyette *
19 1.1.2.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1.2.1 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1.2.1 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1.2.1 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1.2.1 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1.2.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1.2.1 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1.2.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1.2.1 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1.2.1 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1.2.1 pgoyette * SUCH DAMAGE.
30 1.1.2.1 pgoyette */
31 1.1.2.1 pgoyette
32 1.1.2.1 pgoyette /*
33 1.1.2.1 pgoyette * Copyright (c) 1988, 1991, 1993
34 1.1.2.1 pgoyette * The Regents of the University of California. All rights reserved.
35 1.1.2.1 pgoyette *
36 1.1.2.1 pgoyette * Redistribution and use in source and binary forms, with or without
37 1.1.2.1 pgoyette * modification, are permitted provided that the following conditions
38 1.1.2.1 pgoyette * are met:
39 1.1.2.1 pgoyette * 1. Redistributions of source code must retain the above copyright
40 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer.
41 1.1.2.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
42 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer in the
43 1.1.2.1 pgoyette * documentation and/or other materials provided with the distribution.
44 1.1.2.1 pgoyette * 3. Neither the name of the University nor the names of its contributors
45 1.1.2.1 pgoyette * may be used to endorse or promote products derived from this software
46 1.1.2.1 pgoyette * without specific prior written permission.
47 1.1.2.1 pgoyette *
48 1.1.2.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1.2.1 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1.2.1 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1.2.1 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1.2.1 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1.2.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1.2.1 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1.2.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1.2.1 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1.2.1 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1.2.1 pgoyette * SUCH DAMAGE.
59 1.1.2.1 pgoyette *
60 1.1.2.1 pgoyette * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 1.1.2.1 pgoyette */
62 1.1.2.1 pgoyette
63 1.1.2.1 pgoyette #include <sys/cdefs.h>
64 1.1.2.3 pgoyette __KERNEL_RCSID(0, "$NetBSD: rtsock_shared.c,v 1.1.2.3 2019/01/15 04:10:34 pgoyette Exp $");
65 1.1.2.1 pgoyette
66 1.1.2.1 pgoyette #ifdef _KERNEL_OPT
67 1.1.2.1 pgoyette #include "opt_inet.h"
68 1.1.2.1 pgoyette #include "opt_mpls.h"
69 1.1.2.1 pgoyette #include "opt_compat_netbsd.h"
70 1.1.2.1 pgoyette #include "opt_net_mpsafe.h"
71 1.1.2.1 pgoyette #endif
72 1.1.2.1 pgoyette
73 1.1.2.1 pgoyette #include <sys/param.h>
74 1.1.2.1 pgoyette #include <sys/systm.h>
75 1.1.2.1 pgoyette #include <sys/proc.h>
76 1.1.2.1 pgoyette #include <sys/socket.h>
77 1.1.2.1 pgoyette #include <sys/socketvar.h>
78 1.1.2.1 pgoyette #include <sys/domain.h>
79 1.1.2.1 pgoyette #include <sys/protosw.h>
80 1.1.2.1 pgoyette #include <sys/sysctl.h>
81 1.1.2.1 pgoyette #include <sys/kauth.h>
82 1.1.2.1 pgoyette #include <sys/kmem.h>
83 1.1.2.1 pgoyette #include <sys/intr.h>
84 1.1.2.1 pgoyette #include <sys/condvar.h>
85 1.1.2.1 pgoyette #include <sys/compat_stub.h>
86 1.1.2.1 pgoyette
87 1.1.2.1 pgoyette #include <net/if.h>
88 1.1.2.1 pgoyette #include <net/if_llatbl.h>
89 1.1.2.1 pgoyette #include <net/if_types.h>
90 1.1.2.1 pgoyette #include <net/route.h>
91 1.1.2.1 pgoyette #include <net/raw_cb.h>
92 1.1.2.1 pgoyette
93 1.1.2.1 pgoyette #include <netinet/in_var.h>
94 1.1.2.1 pgoyette #include <netinet/if_inarp.h>
95 1.1.2.1 pgoyette
96 1.1.2.1 pgoyette #include <netmpls/mpls.h>
97 1.1.2.1 pgoyette
98 1.1.2.1 pgoyette #include <compat/net/if.h>
99 1.1.2.1 pgoyette #include <compat/net/route.h>
100 1.1.2.1 pgoyette
101 1.1.2.2 pgoyette #ifdef COMPAT_RTSOCK
102 1.1.2.2 pgoyette /*
103 1.1.2.2 pgoyette * These are used when #include-d from compat/common/rtsock_50.c
104 1.1.2.2 pgoyette */
105 1.1.2.1 pgoyette #define RTM_XVERSION RTM_OVERSION
106 1.1.2.1 pgoyette #define RTM_XNEWADDR RTM_ONEWADDR
107 1.1.2.1 pgoyette #define RTM_XDELADDR RTM_ODELADDR
108 1.1.2.1 pgoyette #define RTM_XCHGADDR RTM_OCHGADDR
109 1.1.2.1 pgoyette #define RT_XADVANCE(a,b) RT_OADVANCE(a,b)
110 1.1.2.1 pgoyette #define RT_XROUNDUP(n) RT_OROUNDUP(n)
111 1.1.2.1 pgoyette #define PF_XROUTE PF_OROUTE
112 1.1.2.1 pgoyette #define rt_xmsghdr rt_msghdr50
113 1.1.2.1 pgoyette #define if_xmsghdr if_msghdr /* if_msghdr50 is for RTM_OIFINFO */
114 1.1.2.1 pgoyette #define ifa_xmsghdr ifa_msghdr50
115 1.1.2.1 pgoyette #define if_xannouncemsghdr if_announcemsghdr50
116 1.1.2.1 pgoyette #define COMPATNAME(x) compat_50_ ## x
117 1.1.2.1 pgoyette #define DOMAINNAME "oroute"
118 1.1.2.2 pgoyette #define COMPATCALL(name, args) rtsock_50_ ## name ## _hook_call args
119 1.1.2.1 pgoyette #define RTS_CTASSERT(x) __nothing
120 1.1.2.1 pgoyette CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
121 1.1.2.1 pgoyette DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
122 1.1.2.2 pgoyette #else /* COMPAT_RTSOCK */
123 1.1.2.2 pgoyette /*
124 1.1.2.2 pgoyette * These are used when #include-d from compat/common/rtsock_50.c
125 1.1.2.2 pgoyette */
126 1.1.2.1 pgoyette #define RTM_XVERSION RTM_VERSION
127 1.1.2.1 pgoyette #define RTM_XNEWADDR RTM_NEWADDR
128 1.1.2.1 pgoyette #define RTM_XDELADDR RTM_DELADDR
129 1.1.2.1 pgoyette #define RTM_XCHGADDR RTM_CHGADDR
130 1.1.2.1 pgoyette #define RT_XADVANCE(a,b) RT_ADVANCE(a,b)
131 1.1.2.1 pgoyette #define RT_XROUNDUP(n) RT_ROUNDUP(n)
132 1.1.2.1 pgoyette #define PF_XROUTE PF_ROUTE
133 1.1.2.1 pgoyette #define rt_xmsghdr rt_msghdr
134 1.1.2.1 pgoyette #define if_xmsghdr if_msghdr
135 1.1.2.1 pgoyette #define ifa_xmsghdr ifa_msghdr
136 1.1.2.1 pgoyette #define if_xannouncemsghdr if_announcemsghdr
137 1.1.2.1 pgoyette #define COMPATNAME(x) x
138 1.1.2.1 pgoyette #define DOMAINNAME "route"
139 1.1.2.2 pgoyette #define COMPATCALL(name, args) __nothing;
140 1.1.2.1 pgoyette #define RTS_CTASSERT(x) CTASSERT(x)
141 1.1.2.1 pgoyette CTASSERT(sizeof(struct ifa_xmsghdr) == 32);
142 1.1.2.1 pgoyette DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
143 1.1.2.1 pgoyette #endif /* COMPAT_RTSOCK */
144 1.1.2.1 pgoyette
145 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
146 1.1.2.1 pgoyette #define RT_IN_PRINT(info, b, a) (in_print((b), sizeof(b), \
147 1.1.2.1 pgoyette &((const struct sockaddr_in *)(info)->rti_info[(a)])->sin_addr), (b))
148 1.1.2.1 pgoyette #endif /* RTSOCK_DEBUG */
149 1.1.2.1 pgoyette
150 1.1.2.1 pgoyette struct route_info COMPATNAME(route_info) = {
151 1.1.2.1 pgoyette .ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
152 1.1.2.1 pgoyette .ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
153 1.1.2.1 pgoyette .ri_maxqlen = IFQ_MAXLEN,
154 1.1.2.1 pgoyette };
155 1.1.2.1 pgoyette
156 1.1.2.1 pgoyette static void COMPATNAME(route_init)(void);
157 1.1.2.1 pgoyette static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
158 1.1.2.1 pgoyette
159 1.1.2.1 pgoyette static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
160 1.1.2.1 pgoyette static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
161 1.1.2.1 pgoyette struct rt_addrinfo *);
162 1.1.2.1 pgoyette static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
163 1.1.2.1 pgoyette static void _rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
164 1.1.2.1 pgoyette static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
165 1.1.2.1 pgoyette static void rt_adjustcount(int, int);
166 1.1.2.1 pgoyette
167 1.1.2.1 pgoyette static const struct protosw COMPATNAME(route_protosw)[];
168 1.1.2.1 pgoyette
169 1.1.2.1 pgoyette struct routecb {
170 1.1.2.1 pgoyette struct rawcb rocb_rcb;
171 1.1.2.1 pgoyette unsigned int rocb_msgfilter;
172 1.1.2.1 pgoyette #define RTMSGFILTER(m) (1U << (m))
173 1.1.2.1 pgoyette };
174 1.1.2.1 pgoyette #define sotoroutecb(so) ((struct routecb *)(so)->so_pcb)
175 1.1.2.1 pgoyette
176 1.1.2.1 pgoyette static struct rawcbhead rt_rawcb;
177 1.1.2.1 pgoyette #ifdef NET_MPSAFE
178 1.1.2.1 pgoyette static kmutex_t *rt_so_mtx;
179 1.1.2.1 pgoyette
180 1.1.2.1 pgoyette static bool rt_updating = false;
181 1.1.2.1 pgoyette static kcondvar_t rt_update_cv;
182 1.1.2.1 pgoyette #endif
183 1.1.2.1 pgoyette
184 1.1.2.1 pgoyette static void
185 1.1.2.1 pgoyette rt_adjustcount(int af, int cnt)
186 1.1.2.1 pgoyette {
187 1.1.2.1 pgoyette struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
188 1.1.2.1 pgoyette
189 1.1.2.1 pgoyette cb->any_count += cnt;
190 1.1.2.1 pgoyette
191 1.1.2.1 pgoyette switch (af) {
192 1.1.2.1 pgoyette case AF_INET:
193 1.1.2.1 pgoyette cb->ip_count += cnt;
194 1.1.2.1 pgoyette return;
195 1.1.2.1 pgoyette #ifdef INET6
196 1.1.2.1 pgoyette case AF_INET6:
197 1.1.2.1 pgoyette cb->ip6_count += cnt;
198 1.1.2.1 pgoyette return;
199 1.1.2.1 pgoyette #endif
200 1.1.2.1 pgoyette case AF_MPLS:
201 1.1.2.1 pgoyette cb->mpls_count += cnt;
202 1.1.2.1 pgoyette return;
203 1.1.2.1 pgoyette }
204 1.1.2.1 pgoyette }
205 1.1.2.1 pgoyette
206 1.1.2.1 pgoyette static int
207 1.1.2.1 pgoyette COMPATNAME(route_filter)(struct mbuf *m, struct sockproto *proto,
208 1.1.2.1 pgoyette struct rawcb *rp)
209 1.1.2.1 pgoyette {
210 1.1.2.1 pgoyette struct routecb *rop = (struct routecb *)rp;
211 1.1.2.1 pgoyette struct rt_xmsghdr *rtm;
212 1.1.2.1 pgoyette
213 1.1.2.1 pgoyette KASSERT(m != NULL);
214 1.1.2.1 pgoyette KASSERT(proto != NULL);
215 1.1.2.1 pgoyette KASSERT(rp != NULL);
216 1.1.2.1 pgoyette
217 1.1.2.1 pgoyette /* Wrong family for this socket. */
218 1.1.2.1 pgoyette if (proto->sp_family != PF_ROUTE)
219 1.1.2.1 pgoyette return ENOPROTOOPT;
220 1.1.2.1 pgoyette
221 1.1.2.1 pgoyette /* If no filter set, just return. */
222 1.1.2.1 pgoyette if (rop->rocb_msgfilter == 0)
223 1.1.2.1 pgoyette return 0;
224 1.1.2.1 pgoyette
225 1.1.2.1 pgoyette /* Ensure we can access rtm_type */
226 1.1.2.1 pgoyette if (m->m_len <
227 1.1.2.1 pgoyette offsetof(struct rt_xmsghdr, rtm_type) + sizeof(rtm->rtm_type))
228 1.1.2.1 pgoyette return EINVAL;
229 1.1.2.1 pgoyette
230 1.1.2.1 pgoyette rtm = mtod(m, struct rt_xmsghdr *);
231 1.1.2.1 pgoyette /* If the rtm type is filtered out, return a positive. */
232 1.1.2.1 pgoyette if (!(rop->rocb_msgfilter & RTMSGFILTER(rtm->rtm_type)))
233 1.1.2.1 pgoyette return EEXIST;
234 1.1.2.1 pgoyette
235 1.1.2.1 pgoyette /* Passed the filter. */
236 1.1.2.1 pgoyette return 0;
237 1.1.2.1 pgoyette }
238 1.1.2.1 pgoyette
239 1.1.2.1 pgoyette static void
240 1.1.2.1 pgoyette rt_pr_init(void)
241 1.1.2.1 pgoyette {
242 1.1.2.1 pgoyette
243 1.1.2.1 pgoyette LIST_INIT(&rt_rawcb);
244 1.1.2.1 pgoyette }
245 1.1.2.1 pgoyette
246 1.1.2.1 pgoyette static int
247 1.1.2.1 pgoyette COMPATNAME(route_attach)(struct socket *so, int proto)
248 1.1.2.1 pgoyette {
249 1.1.2.1 pgoyette struct rawcb *rp;
250 1.1.2.1 pgoyette struct routecb *rop;
251 1.1.2.1 pgoyette int s, error;
252 1.1.2.1 pgoyette
253 1.1.2.1 pgoyette KASSERT(sotorawcb(so) == NULL);
254 1.1.2.1 pgoyette rop = kmem_zalloc(sizeof(*rop), KM_SLEEP);
255 1.1.2.1 pgoyette rp = &rop->rocb_rcb;
256 1.1.2.1 pgoyette rp->rcb_len = sizeof(*rop);
257 1.1.2.1 pgoyette so->so_pcb = rp;
258 1.1.2.1 pgoyette
259 1.1.2.1 pgoyette s = splsoftnet();
260 1.1.2.1 pgoyette
261 1.1.2.1 pgoyette #ifdef NET_MPSAFE
262 1.1.2.1 pgoyette KASSERT(so->so_lock == NULL);
263 1.1.2.1 pgoyette mutex_obj_hold(rt_so_mtx);
264 1.1.2.1 pgoyette so->so_lock = rt_so_mtx;
265 1.1.2.1 pgoyette solock(so);
266 1.1.2.1 pgoyette #endif
267 1.1.2.1 pgoyette
268 1.1.2.1 pgoyette if ((error = raw_attach(so, proto, &rt_rawcb)) == 0) {
269 1.1.2.1 pgoyette rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
270 1.1.2.1 pgoyette rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
271 1.1.2.1 pgoyette rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
272 1.1.2.1 pgoyette rp->rcb_filter = COMPATNAME(route_filter);
273 1.1.2.1 pgoyette }
274 1.1.2.1 pgoyette splx(s);
275 1.1.2.1 pgoyette
276 1.1.2.1 pgoyette if (error) {
277 1.1.2.1 pgoyette kmem_free(rop, sizeof(*rop));
278 1.1.2.1 pgoyette so->so_pcb = NULL;
279 1.1.2.1 pgoyette return error;
280 1.1.2.1 pgoyette }
281 1.1.2.1 pgoyette
282 1.1.2.1 pgoyette soisconnected(so);
283 1.1.2.1 pgoyette so->so_options |= SO_USELOOPBACK;
284 1.1.2.1 pgoyette KASSERT(solocked(so));
285 1.1.2.1 pgoyette
286 1.1.2.1 pgoyette return error;
287 1.1.2.1 pgoyette }
288 1.1.2.1 pgoyette
289 1.1.2.1 pgoyette static void
290 1.1.2.1 pgoyette COMPATNAME(route_detach)(struct socket *so)
291 1.1.2.1 pgoyette {
292 1.1.2.1 pgoyette struct rawcb *rp = sotorawcb(so);
293 1.1.2.1 pgoyette int s;
294 1.1.2.1 pgoyette
295 1.1.2.1 pgoyette KASSERT(rp != NULL);
296 1.1.2.1 pgoyette KASSERT(solocked(so));
297 1.1.2.1 pgoyette
298 1.1.2.1 pgoyette s = splsoftnet();
299 1.1.2.1 pgoyette rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
300 1.1.2.1 pgoyette raw_detach(so);
301 1.1.2.1 pgoyette splx(s);
302 1.1.2.1 pgoyette }
303 1.1.2.1 pgoyette
304 1.1.2.1 pgoyette static int
305 1.1.2.1 pgoyette COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
306 1.1.2.1 pgoyette {
307 1.1.2.1 pgoyette KASSERT(solocked(so));
308 1.1.2.1 pgoyette
309 1.1.2.1 pgoyette panic("route_accept");
310 1.1.2.1 pgoyette
311 1.1.2.1 pgoyette return EOPNOTSUPP;
312 1.1.2.1 pgoyette }
313 1.1.2.1 pgoyette
314 1.1.2.1 pgoyette static int
315 1.1.2.1 pgoyette COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
316 1.1.2.1 pgoyette {
317 1.1.2.1 pgoyette KASSERT(solocked(so));
318 1.1.2.1 pgoyette
319 1.1.2.1 pgoyette return EOPNOTSUPP;
320 1.1.2.1 pgoyette }
321 1.1.2.1 pgoyette
322 1.1.2.1 pgoyette static int
323 1.1.2.1 pgoyette COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
324 1.1.2.1 pgoyette {
325 1.1.2.1 pgoyette KASSERT(solocked(so));
326 1.1.2.1 pgoyette
327 1.1.2.1 pgoyette return EOPNOTSUPP;
328 1.1.2.1 pgoyette }
329 1.1.2.1 pgoyette
330 1.1.2.1 pgoyette static int
331 1.1.2.1 pgoyette COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
332 1.1.2.1 pgoyette {
333 1.1.2.1 pgoyette KASSERT(solocked(so));
334 1.1.2.1 pgoyette
335 1.1.2.1 pgoyette return EOPNOTSUPP;
336 1.1.2.1 pgoyette }
337 1.1.2.1 pgoyette
338 1.1.2.1 pgoyette static int
339 1.1.2.1 pgoyette COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
340 1.1.2.1 pgoyette {
341 1.1.2.1 pgoyette KASSERT(solocked(so));
342 1.1.2.1 pgoyette
343 1.1.2.1 pgoyette return EOPNOTSUPP;
344 1.1.2.1 pgoyette }
345 1.1.2.1 pgoyette
346 1.1.2.1 pgoyette static int
347 1.1.2.1 pgoyette COMPATNAME(route_disconnect)(struct socket *so)
348 1.1.2.1 pgoyette {
349 1.1.2.1 pgoyette struct rawcb *rp = sotorawcb(so);
350 1.1.2.1 pgoyette int s;
351 1.1.2.1 pgoyette
352 1.1.2.1 pgoyette KASSERT(solocked(so));
353 1.1.2.1 pgoyette KASSERT(rp != NULL);
354 1.1.2.1 pgoyette
355 1.1.2.1 pgoyette s = splsoftnet();
356 1.1.2.1 pgoyette soisdisconnected(so);
357 1.1.2.1 pgoyette raw_disconnect(rp);
358 1.1.2.1 pgoyette splx(s);
359 1.1.2.1 pgoyette
360 1.1.2.1 pgoyette return 0;
361 1.1.2.1 pgoyette }
362 1.1.2.1 pgoyette
363 1.1.2.1 pgoyette static int
364 1.1.2.1 pgoyette COMPATNAME(route_shutdown)(struct socket *so)
365 1.1.2.1 pgoyette {
366 1.1.2.1 pgoyette int s;
367 1.1.2.1 pgoyette
368 1.1.2.1 pgoyette KASSERT(solocked(so));
369 1.1.2.1 pgoyette
370 1.1.2.1 pgoyette /*
371 1.1.2.1 pgoyette * Mark the connection as being incapable of further input.
372 1.1.2.1 pgoyette */
373 1.1.2.1 pgoyette s = splsoftnet();
374 1.1.2.1 pgoyette socantsendmore(so);
375 1.1.2.1 pgoyette splx(s);
376 1.1.2.1 pgoyette return 0;
377 1.1.2.1 pgoyette }
378 1.1.2.1 pgoyette
379 1.1.2.1 pgoyette static int
380 1.1.2.1 pgoyette COMPATNAME(route_abort)(struct socket *so)
381 1.1.2.1 pgoyette {
382 1.1.2.1 pgoyette KASSERT(solocked(so));
383 1.1.2.1 pgoyette
384 1.1.2.1 pgoyette panic("route_abort");
385 1.1.2.1 pgoyette
386 1.1.2.1 pgoyette return EOPNOTSUPP;
387 1.1.2.1 pgoyette }
388 1.1.2.1 pgoyette
389 1.1.2.1 pgoyette static int
390 1.1.2.1 pgoyette COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
391 1.1.2.1 pgoyette struct ifnet * ifp)
392 1.1.2.1 pgoyette {
393 1.1.2.1 pgoyette return EOPNOTSUPP;
394 1.1.2.1 pgoyette }
395 1.1.2.1 pgoyette
396 1.1.2.1 pgoyette static int
397 1.1.2.1 pgoyette COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
398 1.1.2.1 pgoyette {
399 1.1.2.1 pgoyette KASSERT(solocked(so));
400 1.1.2.1 pgoyette
401 1.1.2.1 pgoyette return 0;
402 1.1.2.1 pgoyette }
403 1.1.2.1 pgoyette
404 1.1.2.1 pgoyette static int
405 1.1.2.1 pgoyette COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
406 1.1.2.1 pgoyette {
407 1.1.2.1 pgoyette struct rawcb *rp = sotorawcb(so);
408 1.1.2.1 pgoyette
409 1.1.2.1 pgoyette KASSERT(solocked(so));
410 1.1.2.1 pgoyette KASSERT(rp != NULL);
411 1.1.2.1 pgoyette KASSERT(nam != NULL);
412 1.1.2.1 pgoyette
413 1.1.2.1 pgoyette if (rp->rcb_faddr == NULL)
414 1.1.2.1 pgoyette return ENOTCONN;
415 1.1.2.1 pgoyette
416 1.1.2.1 pgoyette raw_setpeeraddr(rp, nam);
417 1.1.2.1 pgoyette return 0;
418 1.1.2.1 pgoyette }
419 1.1.2.1 pgoyette
420 1.1.2.1 pgoyette static int
421 1.1.2.1 pgoyette COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
422 1.1.2.1 pgoyette {
423 1.1.2.1 pgoyette struct rawcb *rp = sotorawcb(so);
424 1.1.2.1 pgoyette
425 1.1.2.1 pgoyette KASSERT(solocked(so));
426 1.1.2.1 pgoyette KASSERT(rp != NULL);
427 1.1.2.1 pgoyette KASSERT(nam != NULL);
428 1.1.2.1 pgoyette
429 1.1.2.1 pgoyette if (rp->rcb_faddr == NULL)
430 1.1.2.1 pgoyette return ENOTCONN;
431 1.1.2.1 pgoyette
432 1.1.2.1 pgoyette raw_setsockaddr(rp, nam);
433 1.1.2.1 pgoyette return 0;
434 1.1.2.1 pgoyette }
435 1.1.2.1 pgoyette
436 1.1.2.1 pgoyette static int
437 1.1.2.1 pgoyette COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
438 1.1.2.1 pgoyette {
439 1.1.2.1 pgoyette KASSERT(solocked(so));
440 1.1.2.1 pgoyette
441 1.1.2.1 pgoyette return EOPNOTSUPP;
442 1.1.2.1 pgoyette }
443 1.1.2.1 pgoyette
444 1.1.2.1 pgoyette static int
445 1.1.2.1 pgoyette COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
446 1.1.2.1 pgoyette {
447 1.1.2.1 pgoyette KASSERT(solocked(so));
448 1.1.2.1 pgoyette
449 1.1.2.1 pgoyette return EOPNOTSUPP;
450 1.1.2.1 pgoyette }
451 1.1.2.1 pgoyette
452 1.1.2.1 pgoyette static int
453 1.1.2.1 pgoyette COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
454 1.1.2.1 pgoyette struct sockaddr *nam, struct mbuf *control, struct lwp *l)
455 1.1.2.1 pgoyette {
456 1.1.2.1 pgoyette int error = 0;
457 1.1.2.1 pgoyette int s;
458 1.1.2.1 pgoyette
459 1.1.2.1 pgoyette KASSERT(solocked(so));
460 1.1.2.1 pgoyette KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
461 1.1.2.1 pgoyette
462 1.1.2.1 pgoyette s = splsoftnet();
463 1.1.2.1 pgoyette error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
464 1.1.2.1 pgoyette splx(s);
465 1.1.2.1 pgoyette
466 1.1.2.1 pgoyette return error;
467 1.1.2.1 pgoyette }
468 1.1.2.1 pgoyette
469 1.1.2.1 pgoyette static int
470 1.1.2.1 pgoyette COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
471 1.1.2.1 pgoyette struct mbuf *control)
472 1.1.2.1 pgoyette {
473 1.1.2.1 pgoyette KASSERT(solocked(so));
474 1.1.2.1 pgoyette
475 1.1.2.1 pgoyette m_freem(m);
476 1.1.2.1 pgoyette m_freem(control);
477 1.1.2.1 pgoyette
478 1.1.2.1 pgoyette return EOPNOTSUPP;
479 1.1.2.1 pgoyette }
480 1.1.2.1 pgoyette static int
481 1.1.2.1 pgoyette COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
482 1.1.2.1 pgoyette {
483 1.1.2.1 pgoyette
484 1.1.2.1 pgoyette panic("route_purgeif");
485 1.1.2.1 pgoyette
486 1.1.2.1 pgoyette return EOPNOTSUPP;
487 1.1.2.1 pgoyette }
488 1.1.2.1 pgoyette
489 1.1.2.1 pgoyette #if defined(INET) || defined(INET6)
490 1.1.2.1 pgoyette static int
491 1.1.2.1 pgoyette route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
492 1.1.2.1 pgoyette {
493 1.1.2.1 pgoyette struct rtentry *nrt;
494 1.1.2.1 pgoyette int error;
495 1.1.2.1 pgoyette
496 1.1.2.1 pgoyette error = rtrequest1(RTM_GET, info, &nrt);
497 1.1.2.1 pgoyette if (error != 0)
498 1.1.2.1 pgoyette return error;
499 1.1.2.1 pgoyette /*
500 1.1.2.1 pgoyette * nrt->rt_ifp->if_index may not be correct
501 1.1.2.1 pgoyette * due to changing to ifplo0.
502 1.1.2.1 pgoyette */
503 1.1.2.1 pgoyette *sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
504 1.1.2.1 pgoyette rt_unref(nrt);
505 1.1.2.1 pgoyette
506 1.1.2.1 pgoyette return 0;
507 1.1.2.1 pgoyette }
508 1.1.2.1 pgoyette #endif
509 1.1.2.1 pgoyette
510 1.1.2.1 pgoyette static void
511 1.1.2.1 pgoyette route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
512 1.1.2.1 pgoyette struct sockaddr_dl *sdl, int *flags)
513 1.1.2.1 pgoyette {
514 1.1.2.1 pgoyette struct llentry *la;
515 1.1.2.1 pgoyette
516 1.1.2.1 pgoyette KASSERT(ifp != NULL);
517 1.1.2.1 pgoyette
518 1.1.2.1 pgoyette IF_AFDATA_RLOCK(ifp);
519 1.1.2.1 pgoyette switch (dst->sa_family) {
520 1.1.2.1 pgoyette case AF_INET:
521 1.1.2.1 pgoyette la = lla_lookup(LLTABLE(ifp), 0, dst);
522 1.1.2.1 pgoyette break;
523 1.1.2.1 pgoyette case AF_INET6:
524 1.1.2.1 pgoyette la = lla_lookup(LLTABLE6(ifp), 0, dst);
525 1.1.2.1 pgoyette break;
526 1.1.2.1 pgoyette default:
527 1.1.2.1 pgoyette la = NULL;
528 1.1.2.1 pgoyette KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
529 1.1.2.1 pgoyette break;
530 1.1.2.1 pgoyette }
531 1.1.2.1 pgoyette IF_AFDATA_RUNLOCK(ifp);
532 1.1.2.1 pgoyette
533 1.1.2.1 pgoyette void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
534 1.1.2.1 pgoyette ? &la->ll_addr : NULL;
535 1.1.2.1 pgoyette
536 1.1.2.1 pgoyette a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
537 1.1.2.1 pgoyette NULL, 0, a, ifp->if_addrlen);
538 1.1.2.1 pgoyette KASSERT(a != NULL);
539 1.1.2.1 pgoyette
540 1.1.2.1 pgoyette if (la != NULL) {
541 1.1.2.1 pgoyette *flags = la->la_flags;
542 1.1.2.1 pgoyette LLE_RUNLOCK(la);
543 1.1.2.1 pgoyette }
544 1.1.2.1 pgoyette }
545 1.1.2.1 pgoyette
546 1.1.2.1 pgoyette static int
547 1.1.2.1 pgoyette route_output_report(struct rtentry *rt, struct rt_addrinfo *info,
548 1.1.2.1 pgoyette struct rt_xmsghdr *rtm, struct rt_xmsghdr **new_rtm)
549 1.1.2.1 pgoyette {
550 1.1.2.1 pgoyette int len;
551 1.1.2.1 pgoyette
552 1.1.2.1 pgoyette if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
553 1.1.2.1 pgoyette const struct ifaddr *rtifa;
554 1.1.2.1 pgoyette const struct ifnet *ifp = rt->rt_ifp;
555 1.1.2.1 pgoyette
556 1.1.2.1 pgoyette info->rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
557 1.1.2.1 pgoyette /* rtifa used to be simply rt->rt_ifa.
558 1.1.2.1 pgoyette * If rt->rt_ifa != NULL, then
559 1.1.2.1 pgoyette * rt_get_ifa() != NULL. So this
560 1.1.2.1 pgoyette * ought to still be safe. --dyoung
561 1.1.2.1 pgoyette */
562 1.1.2.1 pgoyette rtifa = rt_get_ifa(rt);
563 1.1.2.1 pgoyette info->rti_info[RTAX_IFA] = rtifa->ifa_addr;
564 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
565 1.1.2.1 pgoyette if (info->rti_info[RTAX_IFA]->sa_family == AF_INET) {
566 1.1.2.1 pgoyette char ibuf[INET_ADDRSTRLEN];
567 1.1.2.1 pgoyette char abuf[INET_ADDRSTRLEN];
568 1.1.2.1 pgoyette printf("%s: copying out RTAX_IFA %s "
569 1.1.2.1 pgoyette "for info->rti_info[RTAX_DST] %s "
570 1.1.2.1 pgoyette "ifa_getifa %p ifa_seqno %p\n",
571 1.1.2.1 pgoyette __func__,
572 1.1.2.1 pgoyette RT_IN_PRINT(info, ibuf, RTAX_IFA),
573 1.1.2.1 pgoyette RT_IN_PRINT(info, abuf, RTAX_DST),
574 1.1.2.1 pgoyette (void *)rtifa->ifa_getifa,
575 1.1.2.1 pgoyette rtifa->ifa_seqno);
576 1.1.2.1 pgoyette }
577 1.1.2.1 pgoyette #endif /* RTSOCK_DEBUG */
578 1.1.2.1 pgoyette if (ifp->if_flags & IFF_POINTOPOINT)
579 1.1.2.1 pgoyette info->rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
580 1.1.2.1 pgoyette else
581 1.1.2.1 pgoyette info->rti_info[RTAX_BRD] = NULL;
582 1.1.2.1 pgoyette rtm->rtm_index = ifp->if_index;
583 1.1.2.1 pgoyette }
584 1.1.2.1 pgoyette (void)rt_msg2(rtm->rtm_type, info, NULL, NULL, &len);
585 1.1.2.1 pgoyette if (len > rtm->rtm_msglen) {
586 1.1.2.1 pgoyette struct rt_xmsghdr *old_rtm = rtm;
587 1.1.2.1 pgoyette R_Malloc(*new_rtm, struct rt_xmsghdr *, len);
588 1.1.2.1 pgoyette if (*new_rtm == NULL)
589 1.1.2.1 pgoyette return ENOBUFS;
590 1.1.2.1 pgoyette (void)memcpy(*new_rtm, old_rtm, old_rtm->rtm_msglen);
591 1.1.2.1 pgoyette rtm = *new_rtm;
592 1.1.2.1 pgoyette }
593 1.1.2.1 pgoyette (void)rt_msg2(rtm->rtm_type, info, rtm, NULL, 0);
594 1.1.2.1 pgoyette rtm->rtm_flags = rt->rt_flags;
595 1.1.2.1 pgoyette rtm_setmetrics(rt, rtm);
596 1.1.2.1 pgoyette rtm->rtm_addrs = info->rti_addrs;
597 1.1.2.1 pgoyette
598 1.1.2.1 pgoyette return 0;
599 1.1.2.1 pgoyette }
600 1.1.2.1 pgoyette
601 1.1.2.1 pgoyette /*ARGSUSED*/
602 1.1.2.1 pgoyette int
603 1.1.2.1 pgoyette COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
604 1.1.2.1 pgoyette {
605 1.1.2.1 pgoyette struct sockproto proto = { .sp_family = PF_XROUTE, };
606 1.1.2.1 pgoyette struct rt_xmsghdr *rtm = NULL;
607 1.1.2.1 pgoyette struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
608 1.1.2.1 pgoyette struct rtentry *rt = NULL;
609 1.1.2.1 pgoyette struct rtentry *saved_nrt = NULL;
610 1.1.2.1 pgoyette struct rt_addrinfo info;
611 1.1.2.1 pgoyette int len, error = 0;
612 1.1.2.1 pgoyette sa_family_t family;
613 1.1.2.1 pgoyette struct sockaddr_dl sdl;
614 1.1.2.1 pgoyette int bound = curlwp_bind();
615 1.1.2.1 pgoyette bool do_rt_free = false;
616 1.1.2.1 pgoyette struct sockaddr_storage netmask;
617 1.1.2.1 pgoyette
618 1.1.2.1 pgoyette #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
619 1.1.2.1 pgoyette if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
620 1.1.2.1 pgoyette (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
621 1.1.2.1 pgoyette error = ENOBUFS;
622 1.1.2.1 pgoyette goto out;
623 1.1.2.1 pgoyette }
624 1.1.2.1 pgoyette if ((m->m_flags & M_PKTHDR) == 0)
625 1.1.2.1 pgoyette panic("%s", __func__);
626 1.1.2.1 pgoyette len = m->m_pkthdr.len;
627 1.1.2.1 pgoyette if (len < sizeof(*rtm) ||
628 1.1.2.1 pgoyette len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
629 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = NULL;
630 1.1.2.1 pgoyette senderr(EINVAL);
631 1.1.2.1 pgoyette }
632 1.1.2.1 pgoyette R_Malloc(rtm, struct rt_xmsghdr *, len);
633 1.1.2.1 pgoyette if (rtm == NULL) {
634 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = NULL;
635 1.1.2.1 pgoyette senderr(ENOBUFS);
636 1.1.2.1 pgoyette }
637 1.1.2.1 pgoyette m_copydata(m, 0, len, rtm);
638 1.1.2.1 pgoyette if (rtm->rtm_version != RTM_XVERSION) {
639 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = NULL;
640 1.1.2.1 pgoyette senderr(EPROTONOSUPPORT);
641 1.1.2.1 pgoyette }
642 1.1.2.1 pgoyette rtm->rtm_pid = curproc->p_pid;
643 1.1.2.1 pgoyette memset(&info, 0, sizeof(info));
644 1.1.2.1 pgoyette info.rti_addrs = rtm->rtm_addrs;
645 1.1.2.1 pgoyette if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
646 1.1.2.1 pgoyette &info)) {
647 1.1.2.1 pgoyette senderr(EINVAL);
648 1.1.2.1 pgoyette }
649 1.1.2.1 pgoyette info.rti_flags = rtm->rtm_flags;
650 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
651 1.1.2.1 pgoyette if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
652 1.1.2.1 pgoyette char abuf[INET_ADDRSTRLEN];
653 1.1.2.1 pgoyette printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
654 1.1.2.1 pgoyette RT_IN_PRINT(&info, abuf, RTAX_DST));
655 1.1.2.1 pgoyette }
656 1.1.2.1 pgoyette #endif /* RTSOCK_DEBUG */
657 1.1.2.1 pgoyette if (info.rti_info[RTAX_DST] == NULL ||
658 1.1.2.1 pgoyette (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
659 1.1.2.1 pgoyette senderr(EINVAL);
660 1.1.2.1 pgoyette }
661 1.1.2.1 pgoyette if (info.rti_info[RTAX_GATEWAY] != NULL &&
662 1.1.2.1 pgoyette (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
663 1.1.2.1 pgoyette senderr(EINVAL);
664 1.1.2.1 pgoyette }
665 1.1.2.1 pgoyette
666 1.1.2.1 pgoyette /*
667 1.1.2.1 pgoyette * Verify that the caller has the appropriate privilege; RTM_GET
668 1.1.2.1 pgoyette * is the only operation the non-superuser is allowed.
669 1.1.2.1 pgoyette */
670 1.1.2.1 pgoyette if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
671 1.1.2.1 pgoyette 0, rtm, NULL, NULL) != 0)
672 1.1.2.1 pgoyette senderr(EACCES);
673 1.1.2.1 pgoyette
674 1.1.2.1 pgoyette /*
675 1.1.2.1 pgoyette * route(8) passes a sockaddr truncated with prefixlen.
676 1.1.2.1 pgoyette * The kernel doesn't expect such sockaddr and need to
677 1.1.2.1 pgoyette * use a buffer that is big enough for the sockaddr expected
678 1.1.2.1 pgoyette * (padded with 0's). We keep the original length of the sockaddr.
679 1.1.2.1 pgoyette */
680 1.1.2.1 pgoyette if (info.rti_info[RTAX_NETMASK]) {
681 1.1.2.1 pgoyette /*
682 1.1.2.1 pgoyette * Use the family of RTAX_DST, because RTAX_NETMASK
683 1.1.2.1 pgoyette * can have a zero family if it comes from the radix
684 1.1.2.1 pgoyette * tree via rt_mask().
685 1.1.2.1 pgoyette */
686 1.1.2.1 pgoyette socklen_t sa_len = sockaddr_getsize_by_family(
687 1.1.2.1 pgoyette info.rti_info[RTAX_DST]->sa_family);
688 1.1.2.1 pgoyette socklen_t masklen = sockaddr_getlen(
689 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK]);
690 1.1.2.1 pgoyette if (sa_len != 0 && sa_len > masklen) {
691 1.1.2.1 pgoyette KASSERT(sa_len <= sizeof(netmask));
692 1.1.2.1 pgoyette memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
693 1.1.2.1 pgoyette memset((char *)&netmask + masklen, 0, sa_len - masklen);
694 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK] = sstocsa(&netmask);
695 1.1.2.1 pgoyette }
696 1.1.2.1 pgoyette }
697 1.1.2.1 pgoyette
698 1.1.2.1 pgoyette switch (rtm->rtm_type) {
699 1.1.2.1 pgoyette
700 1.1.2.1 pgoyette case RTM_ADD:
701 1.1.2.1 pgoyette if (info.rti_info[RTAX_GATEWAY] == NULL) {
702 1.1.2.1 pgoyette senderr(EINVAL);
703 1.1.2.1 pgoyette }
704 1.1.2.1 pgoyette #if defined(INET) || defined(INET6)
705 1.1.2.1 pgoyette /* support for new ARP/NDP code with keeping backcompat */
706 1.1.2.1 pgoyette if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
707 1.1.2.1 pgoyette const struct sockaddr_dl *sdlp =
708 1.1.2.1 pgoyette satocsdl(info.rti_info[RTAX_GATEWAY]);
709 1.1.2.1 pgoyette
710 1.1.2.1 pgoyette /* Allow routing requests by interface index */
711 1.1.2.1 pgoyette if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
712 1.1.2.1 pgoyette && sdlp->sdl_slen == 0)
713 1.1.2.1 pgoyette goto fallback;
714 1.1.2.1 pgoyette /*
715 1.1.2.1 pgoyette * Old arp binaries don't set the sdl_index
716 1.1.2.1 pgoyette * so we have to complement it.
717 1.1.2.1 pgoyette */
718 1.1.2.1 pgoyette int sdl_index = sdlp->sdl_index;
719 1.1.2.1 pgoyette if (sdl_index == 0) {
720 1.1.2.1 pgoyette error = route_get_sdl_index(&info, &sdl_index);
721 1.1.2.1 pgoyette if (error != 0)
722 1.1.2.1 pgoyette goto fallback;
723 1.1.2.1 pgoyette } else if (
724 1.1.2.1 pgoyette info.rti_info[RTAX_DST]->sa_family == AF_INET) {
725 1.1.2.1 pgoyette /*
726 1.1.2.1 pgoyette * XXX workaround for SIN_PROXY case; proxy arp
727 1.1.2.1 pgoyette * entry should be in an interface that has
728 1.1.2.1 pgoyette * a network route including the destination,
729 1.1.2.1 pgoyette * not a local (link) route that may not be a
730 1.1.2.1 pgoyette * desired place, for example a tap.
731 1.1.2.1 pgoyette */
732 1.1.2.1 pgoyette const struct sockaddr_inarp *sina =
733 1.1.2.1 pgoyette (const struct sockaddr_inarp *)
734 1.1.2.1 pgoyette info.rti_info[RTAX_DST];
735 1.1.2.1 pgoyette if (sina->sin_other & SIN_PROXY) {
736 1.1.2.1 pgoyette error = route_get_sdl_index(&info,
737 1.1.2.1 pgoyette &sdl_index);
738 1.1.2.1 pgoyette if (error != 0)
739 1.1.2.1 pgoyette goto fallback;
740 1.1.2.1 pgoyette }
741 1.1.2.1 pgoyette }
742 1.1.2.1 pgoyette error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
743 1.1.2.1 pgoyette rtm->rtm_rmx.rmx_expire, &info, sdl_index);
744 1.1.2.1 pgoyette break;
745 1.1.2.1 pgoyette }
746 1.1.2.1 pgoyette fallback:
747 1.1.2.1 pgoyette #endif /* defined(INET) || defined(INET6) */
748 1.1.2.1 pgoyette error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
749 1.1.2.1 pgoyette if (error == 0) {
750 1.1.2.1 pgoyette _rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
751 1.1.2.1 pgoyette rt_unref(saved_nrt);
752 1.1.2.1 pgoyette }
753 1.1.2.1 pgoyette break;
754 1.1.2.1 pgoyette
755 1.1.2.1 pgoyette case RTM_DELETE:
756 1.1.2.1 pgoyette #if defined(INET) || defined(INET6)
757 1.1.2.1 pgoyette /* support for new ARP/NDP code */
758 1.1.2.1 pgoyette if (info.rti_info[RTAX_GATEWAY] &&
759 1.1.2.1 pgoyette (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
760 1.1.2.1 pgoyette (rtm->rtm_flags & RTF_LLDATA) != 0) {
761 1.1.2.1 pgoyette const struct sockaddr_dl *sdlp =
762 1.1.2.1 pgoyette satocsdl(info.rti_info[RTAX_GATEWAY]);
763 1.1.2.1 pgoyette error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
764 1.1.2.1 pgoyette rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
765 1.1.2.1 pgoyette rtm->rtm_flags &= ~RTF_UP;
766 1.1.2.1 pgoyette break;
767 1.1.2.1 pgoyette }
768 1.1.2.1 pgoyette #endif
769 1.1.2.1 pgoyette error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
770 1.1.2.1 pgoyette if (error != 0)
771 1.1.2.1 pgoyette break;
772 1.1.2.1 pgoyette
773 1.1.2.1 pgoyette rt = saved_nrt;
774 1.1.2.1 pgoyette do_rt_free = true;
775 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = rt_getkey(rt);
776 1.1.2.1 pgoyette info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
777 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK] = rt_mask(rt);
778 1.1.2.1 pgoyette info.rti_info[RTAX_TAG] = rt_gettag(rt);
779 1.1.2.1 pgoyette error = route_output_report(rt, &info, rtm, &new_rtm);
780 1.1.2.1 pgoyette if (error)
781 1.1.2.1 pgoyette senderr(error);
782 1.1.2.1 pgoyette if (new_rtm != NULL) {
783 1.1.2.1 pgoyette old_rtm = rtm;
784 1.1.2.1 pgoyette rtm = new_rtm;
785 1.1.2.1 pgoyette }
786 1.1.2.1 pgoyette break;
787 1.1.2.1 pgoyette
788 1.1.2.1 pgoyette case RTM_GET:
789 1.1.2.1 pgoyette case RTM_CHANGE:
790 1.1.2.1 pgoyette case RTM_LOCK:
791 1.1.2.1 pgoyette /* XXX This will mask info.rti_info[RTAX_DST] with
792 1.1.2.1 pgoyette * info.rti_info[RTAX_NETMASK] before
793 1.1.2.1 pgoyette * searching. It did not used to do that. --dyoung
794 1.1.2.1 pgoyette */
795 1.1.2.1 pgoyette rt = NULL;
796 1.1.2.1 pgoyette error = rtrequest1(RTM_GET, &info, &rt);
797 1.1.2.1 pgoyette if (error != 0)
798 1.1.2.1 pgoyette senderr(error);
799 1.1.2.1 pgoyette if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
800 1.1.2.1 pgoyette if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
801 1.1.2.1 pgoyette info.rti_info[RTAX_DST]->sa_len) != 0)
802 1.1.2.1 pgoyette senderr(ESRCH);
803 1.1.2.1 pgoyette if (info.rti_info[RTAX_NETMASK] == NULL &&
804 1.1.2.1 pgoyette rt_mask(rt) != NULL)
805 1.1.2.1 pgoyette senderr(ETOOMANYREFS);
806 1.1.2.1 pgoyette }
807 1.1.2.1 pgoyette
808 1.1.2.1 pgoyette /*
809 1.1.2.1 pgoyette * XXX if arp/ndp requests an L2 entry, we have to obtain
810 1.1.2.1 pgoyette * it from lltable while for the route command we have to
811 1.1.2.1 pgoyette * return a route as it is. How to distinguish them?
812 1.1.2.1 pgoyette * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
813 1.1.2.1 pgoyette * indicates an L2 entry is requested. For old arp/ndp
814 1.1.2.1 pgoyette * binaries, we check RTF_UP flag is NOT set; it works
815 1.1.2.1 pgoyette * by the fact that arp/ndp don't set it while the route
816 1.1.2.1 pgoyette * command sets it.
817 1.1.2.1 pgoyette */
818 1.1.2.1 pgoyette if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
819 1.1.2.1 pgoyette (rtm->rtm_flags & RTF_UP) == 0) &&
820 1.1.2.1 pgoyette rtm->rtm_type == RTM_GET &&
821 1.1.2.1 pgoyette sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
822 1.1.2.1 pgoyette int ll_flags = 0;
823 1.1.2.1 pgoyette route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
824 1.1.2.1 pgoyette &ll_flags);
825 1.1.2.1 pgoyette info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
826 1.1.2.1 pgoyette error = route_output_report(rt, &info, rtm, &new_rtm);
827 1.1.2.1 pgoyette if (error)
828 1.1.2.1 pgoyette senderr(error);
829 1.1.2.1 pgoyette if (new_rtm != NULL) {
830 1.1.2.1 pgoyette old_rtm = rtm;
831 1.1.2.1 pgoyette rtm = new_rtm;
832 1.1.2.1 pgoyette }
833 1.1.2.1 pgoyette rtm->rtm_flags |= RTF_LLDATA;
834 1.1.2.1 pgoyette rtm->rtm_flags &= ~RTF_CONNECTED;
835 1.1.2.1 pgoyette rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
836 1.1.2.1 pgoyette break;
837 1.1.2.1 pgoyette }
838 1.1.2.1 pgoyette
839 1.1.2.1 pgoyette switch (rtm->rtm_type) {
840 1.1.2.1 pgoyette case RTM_GET:
841 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = rt_getkey(rt);
842 1.1.2.1 pgoyette info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
843 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK] = rt_mask(rt);
844 1.1.2.1 pgoyette info.rti_info[RTAX_TAG] = rt_gettag(rt);
845 1.1.2.1 pgoyette error = route_output_report(rt, &info, rtm, &new_rtm);
846 1.1.2.1 pgoyette if (error)
847 1.1.2.1 pgoyette senderr(error);
848 1.1.2.1 pgoyette if (new_rtm != NULL) {
849 1.1.2.1 pgoyette old_rtm = rtm;
850 1.1.2.1 pgoyette rtm = new_rtm;
851 1.1.2.1 pgoyette }
852 1.1.2.1 pgoyette break;
853 1.1.2.1 pgoyette
854 1.1.2.1 pgoyette case RTM_CHANGE:
855 1.1.2.1 pgoyette #ifdef NET_MPSAFE
856 1.1.2.1 pgoyette /*
857 1.1.2.1 pgoyette * Release rt_so_mtx to avoid a deadlock with route_intr
858 1.1.2.1 pgoyette * and also serialize updating routes to avoid another.
859 1.1.2.1 pgoyette */
860 1.1.2.1 pgoyette if (rt_updating) {
861 1.1.2.1 pgoyette /* Release to allow the updater to proceed */
862 1.1.2.1 pgoyette rt_unref(rt);
863 1.1.2.1 pgoyette rt = NULL;
864 1.1.2.1 pgoyette }
865 1.1.2.1 pgoyette while (rt_updating) {
866 1.1.2.1 pgoyette error = cv_wait_sig(&rt_update_cv, rt_so_mtx);
867 1.1.2.1 pgoyette if (error != 0)
868 1.1.2.1 pgoyette goto flush;
869 1.1.2.1 pgoyette }
870 1.1.2.1 pgoyette if (rt == NULL) {
871 1.1.2.1 pgoyette error = rtrequest1(RTM_GET, &info, &rt);
872 1.1.2.1 pgoyette if (error != 0)
873 1.1.2.1 pgoyette goto flush;
874 1.1.2.1 pgoyette }
875 1.1.2.1 pgoyette rt_updating = true;
876 1.1.2.1 pgoyette mutex_exit(rt_so_mtx);
877 1.1.2.1 pgoyette
878 1.1.2.1 pgoyette error = rt_update_prepare(rt);
879 1.1.2.1 pgoyette if (error == 0) {
880 1.1.2.1 pgoyette error = rt_update(rt, &info, rtm);
881 1.1.2.1 pgoyette rt_update_finish(rt);
882 1.1.2.1 pgoyette }
883 1.1.2.1 pgoyette
884 1.1.2.1 pgoyette mutex_enter(rt_so_mtx);
885 1.1.2.1 pgoyette rt_updating = false;
886 1.1.2.1 pgoyette cv_broadcast(&rt_update_cv);
887 1.1.2.1 pgoyette #else
888 1.1.2.1 pgoyette error = rt_update(rt, &info, rtm);
889 1.1.2.1 pgoyette #endif
890 1.1.2.1 pgoyette if (error != 0)
891 1.1.2.1 pgoyette goto flush;
892 1.1.2.1 pgoyette /*FALLTHROUGH*/
893 1.1.2.1 pgoyette case RTM_LOCK:
894 1.1.2.1 pgoyette rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
895 1.1.2.1 pgoyette rt->rt_rmx.rmx_locks |=
896 1.1.2.1 pgoyette (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
897 1.1.2.1 pgoyette break;
898 1.1.2.1 pgoyette }
899 1.1.2.1 pgoyette break;
900 1.1.2.1 pgoyette
901 1.1.2.1 pgoyette default:
902 1.1.2.1 pgoyette senderr(EOPNOTSUPP);
903 1.1.2.1 pgoyette }
904 1.1.2.1 pgoyette
905 1.1.2.1 pgoyette flush:
906 1.1.2.1 pgoyette if (rtm) {
907 1.1.2.1 pgoyette if (error)
908 1.1.2.1 pgoyette rtm->rtm_errno = error;
909 1.1.2.1 pgoyette else
910 1.1.2.1 pgoyette rtm->rtm_flags |= RTF_DONE;
911 1.1.2.1 pgoyette }
912 1.1.2.1 pgoyette family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
913 1.1.2.1 pgoyette 0;
914 1.1.2.1 pgoyette /* We cannot free old_rtm until we have stopped using the
915 1.1.2.1 pgoyette * pointers in info, some of which may point to sockaddrs
916 1.1.2.1 pgoyette * in old_rtm.
917 1.1.2.1 pgoyette */
918 1.1.2.1 pgoyette if (old_rtm != NULL)
919 1.1.2.1 pgoyette Free(old_rtm);
920 1.1.2.1 pgoyette if (rt) {
921 1.1.2.1 pgoyette if (do_rt_free) {
922 1.1.2.1 pgoyette #ifdef NET_MPSAFE
923 1.1.2.1 pgoyette /*
924 1.1.2.1 pgoyette * Release rt_so_mtx to avoid a deadlock with
925 1.1.2.1 pgoyette * route_intr.
926 1.1.2.1 pgoyette */
927 1.1.2.1 pgoyette mutex_exit(rt_so_mtx);
928 1.1.2.1 pgoyette rt_free(rt);
929 1.1.2.1 pgoyette mutex_enter(rt_so_mtx);
930 1.1.2.1 pgoyette #else
931 1.1.2.1 pgoyette rt_free(rt);
932 1.1.2.1 pgoyette #endif
933 1.1.2.1 pgoyette } else
934 1.1.2.1 pgoyette rt_unref(rt);
935 1.1.2.1 pgoyette }
936 1.1.2.1 pgoyette {
937 1.1.2.1 pgoyette struct rawcb *rp = NULL;
938 1.1.2.1 pgoyette /*
939 1.1.2.1 pgoyette * Check to see if we don't want our own messages.
940 1.1.2.1 pgoyette */
941 1.1.2.1 pgoyette if ((so->so_options & SO_USELOOPBACK) == 0) {
942 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
943 1.1.2.1 pgoyette if (rtm)
944 1.1.2.1 pgoyette Free(rtm);
945 1.1.2.1 pgoyette m_freem(m);
946 1.1.2.1 pgoyette goto out;
947 1.1.2.1 pgoyette }
948 1.1.2.1 pgoyette /* There is another listener, so construct message */
949 1.1.2.1 pgoyette rp = sotorawcb(so);
950 1.1.2.1 pgoyette }
951 1.1.2.1 pgoyette if (rtm) {
952 1.1.2.1 pgoyette m_copyback(m, 0, rtm->rtm_msglen, rtm);
953 1.1.2.1 pgoyette if (m->m_pkthdr.len < rtm->rtm_msglen) {
954 1.1.2.1 pgoyette m_freem(m);
955 1.1.2.1 pgoyette m = NULL;
956 1.1.2.1 pgoyette } else if (m->m_pkthdr.len > rtm->rtm_msglen)
957 1.1.2.1 pgoyette m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
958 1.1.2.1 pgoyette Free(rtm);
959 1.1.2.1 pgoyette }
960 1.1.2.1 pgoyette if (rp)
961 1.1.2.1 pgoyette rp->rcb_proto.sp_family = 0; /* Avoid us */
962 1.1.2.1 pgoyette if (family)
963 1.1.2.1 pgoyette proto.sp_protocol = family;
964 1.1.2.1 pgoyette if (m)
965 1.1.2.1 pgoyette raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
966 1.1.2.1 pgoyette &COMPATNAME(route_info).ri_dst, &rt_rawcb);
967 1.1.2.1 pgoyette if (rp)
968 1.1.2.1 pgoyette rp->rcb_proto.sp_family = PF_XROUTE;
969 1.1.2.1 pgoyette }
970 1.1.2.1 pgoyette out:
971 1.1.2.1 pgoyette curlwp_bindx(bound);
972 1.1.2.1 pgoyette return error;
973 1.1.2.1 pgoyette }
974 1.1.2.1 pgoyette
975 1.1.2.1 pgoyette static int
976 1.1.2.1 pgoyette route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
977 1.1.2.1 pgoyette {
978 1.1.2.1 pgoyette struct routecb *rop = sotoroutecb(so);
979 1.1.2.1 pgoyette int error = 0;
980 1.1.2.1 pgoyette unsigned char *rtm_type;
981 1.1.2.1 pgoyette size_t len;
982 1.1.2.1 pgoyette unsigned int msgfilter;
983 1.1.2.1 pgoyette
984 1.1.2.1 pgoyette KASSERT(solocked(so));
985 1.1.2.1 pgoyette
986 1.1.2.1 pgoyette if (sopt->sopt_level != AF_ROUTE) {
987 1.1.2.1 pgoyette error = ENOPROTOOPT;
988 1.1.2.1 pgoyette } else switch (op) {
989 1.1.2.1 pgoyette case PRCO_SETOPT:
990 1.1.2.1 pgoyette switch (sopt->sopt_name) {
991 1.1.2.1 pgoyette case RO_MSGFILTER:
992 1.1.2.1 pgoyette msgfilter = 0;
993 1.1.2.1 pgoyette for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
994 1.1.2.1 pgoyette len != 0;
995 1.1.2.1 pgoyette rtm_type++, len -= sizeof(*rtm_type))
996 1.1.2.1 pgoyette {
997 1.1.2.1 pgoyette /* Guard against overflowing our storage. */
998 1.1.2.1 pgoyette if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
999 1.1.2.1 pgoyette error = EOVERFLOW;
1000 1.1.2.1 pgoyette break;
1001 1.1.2.1 pgoyette }
1002 1.1.2.1 pgoyette msgfilter |= RTMSGFILTER(*rtm_type);
1003 1.1.2.1 pgoyette }
1004 1.1.2.1 pgoyette if (error == 0)
1005 1.1.2.1 pgoyette rop->rocb_msgfilter = msgfilter;
1006 1.1.2.1 pgoyette break;
1007 1.1.2.1 pgoyette default:
1008 1.1.2.1 pgoyette error = ENOPROTOOPT;
1009 1.1.2.1 pgoyette break;
1010 1.1.2.1 pgoyette }
1011 1.1.2.1 pgoyette break;
1012 1.1.2.1 pgoyette case PRCO_GETOPT:
1013 1.1.2.1 pgoyette switch (sopt->sopt_name) {
1014 1.1.2.1 pgoyette case RO_MSGFILTER:
1015 1.1.2.1 pgoyette error = ENOTSUP;
1016 1.1.2.1 pgoyette break;
1017 1.1.2.1 pgoyette default:
1018 1.1.2.1 pgoyette error = ENOPROTOOPT;
1019 1.1.2.1 pgoyette break;
1020 1.1.2.1 pgoyette }
1021 1.1.2.1 pgoyette }
1022 1.1.2.1 pgoyette return error;
1023 1.1.2.1 pgoyette }
1024 1.1.2.1 pgoyette
1025 1.1.2.1 pgoyette static void
1026 1.1.2.1 pgoyette _rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
1027 1.1.2.1 pgoyette {
1028 1.1.2.1 pgoyette #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
1029 1.1.2.1 pgoyette metric(RTV_RPIPE, rmx_recvpipe);
1030 1.1.2.1 pgoyette metric(RTV_SPIPE, rmx_sendpipe);
1031 1.1.2.1 pgoyette metric(RTV_SSTHRESH, rmx_ssthresh);
1032 1.1.2.1 pgoyette metric(RTV_RTT, rmx_rtt);
1033 1.1.2.1 pgoyette metric(RTV_RTTVAR, rmx_rttvar);
1034 1.1.2.1 pgoyette metric(RTV_HOPCOUNT, rmx_hopcount);
1035 1.1.2.1 pgoyette metric(RTV_MTU, rmx_mtu);
1036 1.1.2.1 pgoyette #undef metric
1037 1.1.2.1 pgoyette if (which & RTV_EXPIRE) {
1038 1.1.2.1 pgoyette out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
1039 1.1.2.1 pgoyette time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
1040 1.1.2.1 pgoyette }
1041 1.1.2.1 pgoyette }
1042 1.1.2.1 pgoyette
1043 1.1.2.1 pgoyette static void
1044 1.1.2.1 pgoyette rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
1045 1.1.2.1 pgoyette {
1046 1.1.2.1 pgoyette #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
1047 1.1.2.1 pgoyette metric(rmx_recvpipe);
1048 1.1.2.1 pgoyette metric(rmx_sendpipe);
1049 1.1.2.1 pgoyette metric(rmx_ssthresh);
1050 1.1.2.1 pgoyette metric(rmx_rtt);
1051 1.1.2.1 pgoyette metric(rmx_rttvar);
1052 1.1.2.1 pgoyette metric(rmx_hopcount);
1053 1.1.2.1 pgoyette metric(rmx_mtu);
1054 1.1.2.1 pgoyette metric(rmx_locks);
1055 1.1.2.1 pgoyette #undef metric
1056 1.1.2.1 pgoyette out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1057 1.1.2.1 pgoyette time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1058 1.1.2.1 pgoyette }
1059 1.1.2.1 pgoyette
1060 1.1.2.1 pgoyette static int
1061 1.1.2.1 pgoyette rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1062 1.1.2.1 pgoyette struct rt_addrinfo *rtinfo)
1063 1.1.2.1 pgoyette {
1064 1.1.2.1 pgoyette const struct sockaddr *sa = NULL; /* Quell compiler warning */
1065 1.1.2.1 pgoyette int i;
1066 1.1.2.1 pgoyette
1067 1.1.2.1 pgoyette for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1068 1.1.2.1 pgoyette if ((rtinfo->rti_addrs & (1 << i)) == 0)
1069 1.1.2.1 pgoyette continue;
1070 1.1.2.1 pgoyette rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1071 1.1.2.1 pgoyette RT_XADVANCE(cp, sa);
1072 1.1.2.1 pgoyette }
1073 1.1.2.1 pgoyette
1074 1.1.2.1 pgoyette /*
1075 1.1.2.1 pgoyette * Check for extra addresses specified, except RTM_GET asking
1076 1.1.2.1 pgoyette * for interface info.
1077 1.1.2.1 pgoyette */
1078 1.1.2.1 pgoyette if (rtmtype == RTM_GET) {
1079 1.1.2.1 pgoyette if (((rtinfo->rti_addrs &
1080 1.1.2.1 pgoyette (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1081 1.1.2.1 pgoyette return 1;
1082 1.1.2.1 pgoyette } else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1083 1.1.2.1 pgoyette return 1;
1084 1.1.2.1 pgoyette /* Check for bad data length. */
1085 1.1.2.1 pgoyette if (cp != cplim) {
1086 1.1.2.1 pgoyette if (i == RTAX_NETMASK + 1 && sa != NULL &&
1087 1.1.2.1 pgoyette cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1088 1.1.2.1 pgoyette /*
1089 1.1.2.1 pgoyette * The last sockaddr was info.rti_info[RTAX_NETMASK].
1090 1.1.2.1 pgoyette * We accept this for now for the sake of old
1091 1.1.2.1 pgoyette * binaries or third party softwares.
1092 1.1.2.1 pgoyette */
1093 1.1.2.1 pgoyette ;
1094 1.1.2.1 pgoyette else
1095 1.1.2.1 pgoyette return 1;
1096 1.1.2.1 pgoyette }
1097 1.1.2.1 pgoyette return 0;
1098 1.1.2.1 pgoyette }
1099 1.1.2.1 pgoyette
1100 1.1.2.1 pgoyette static int
1101 1.1.2.1 pgoyette rt_getlen(int type)
1102 1.1.2.1 pgoyette {
1103 1.1.2.1 pgoyette RTS_CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1104 1.1.2.1 pgoyette RTS_CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1105 1.1.2.1 pgoyette RTS_CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1106 1.1.2.1 pgoyette RTS_CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1107 1.1.2.1 pgoyette
1108 1.1.2.1 pgoyette switch (type) {
1109 1.1.2.1 pgoyette case RTM_ODELADDR:
1110 1.1.2.1 pgoyette case RTM_ONEWADDR:
1111 1.1.2.1 pgoyette case RTM_OCHGADDR:
1112 1.1.2.1 pgoyette if (rtsock_70_iflist_hook.hooked)
1113 1.1.2.1 pgoyette return sizeof(struct ifa_msghdr70);
1114 1.1.2.1 pgoyette else {
1115 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
1116 1.1.2.1 pgoyette printf("%s: unsupported RTM type %d\n", __func__, type);
1117 1.1.2.1 pgoyette #endif
1118 1.1.2.1 pgoyette return -1;
1119 1.1.2.1 pgoyette }
1120 1.1.2.1 pgoyette
1121 1.1.2.1 pgoyette case RTM_DELADDR:
1122 1.1.2.1 pgoyette case RTM_NEWADDR:
1123 1.1.2.1 pgoyette case RTM_CHGADDR:
1124 1.1.2.1 pgoyette return sizeof(struct ifa_xmsghdr);
1125 1.1.2.1 pgoyette
1126 1.1.2.1 pgoyette case RTM_OOIFINFO:
1127 1.1.2.1 pgoyette if (rtsock_14_iflist_hook.hooked)
1128 1.1.2.1 pgoyette return sizeof(struct if_msghdr14);
1129 1.1.2.1 pgoyette else {
1130 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
1131 1.1.2.1 pgoyette printf("%s: unsupported RTM type RTM_OOIFINFO\n",
1132 1.1.2.1 pgoyette __func__);
1133 1.1.2.1 pgoyette #endif
1134 1.1.2.1 pgoyette return -1;
1135 1.1.2.1 pgoyette }
1136 1.1.2.1 pgoyette
1137 1.1.2.1 pgoyette case RTM_OIFINFO:
1138 1.1.2.1 pgoyette if (rtsock_50_iflist_hook.hooked)
1139 1.1.2.1 pgoyette return sizeof(struct if_msghdr50);
1140 1.1.2.1 pgoyette else {
1141 1.1.2.1 pgoyette #ifdef RTSOCK_DEBUG
1142 1.1.2.1 pgoyette printf("%s: unsupported RTM type RTM_OIFINFO\n",
1143 1.1.2.1 pgoyette __func__);
1144 1.1.2.1 pgoyette #endif
1145 1.1.2.1 pgoyette return -1;
1146 1.1.2.1 pgoyette }
1147 1.1.2.1 pgoyette
1148 1.1.2.1 pgoyette case RTM_IFINFO:
1149 1.1.2.1 pgoyette return sizeof(struct if_xmsghdr);
1150 1.1.2.1 pgoyette
1151 1.1.2.1 pgoyette case RTM_IFANNOUNCE:
1152 1.1.2.1 pgoyette case RTM_IEEE80211:
1153 1.1.2.1 pgoyette return sizeof(struct if_xannouncemsghdr);
1154 1.1.2.1 pgoyette
1155 1.1.2.1 pgoyette default:
1156 1.1.2.1 pgoyette return sizeof(struct rt_xmsghdr);
1157 1.1.2.1 pgoyette }
1158 1.1.2.1 pgoyette }
1159 1.1.2.1 pgoyette
1160 1.1.2.1 pgoyette
1161 1.1.2.1 pgoyette struct mbuf *
1162 1.1.2.1 pgoyette COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1163 1.1.2.1 pgoyette {
1164 1.1.2.1 pgoyette struct rt_xmsghdr *rtm;
1165 1.1.2.1 pgoyette struct mbuf *m;
1166 1.1.2.1 pgoyette int i;
1167 1.1.2.1 pgoyette const struct sockaddr *sa;
1168 1.1.2.1 pgoyette int len, dlen;
1169 1.1.2.1 pgoyette
1170 1.1.2.1 pgoyette m = m_gethdr(M_DONTWAIT, MT_DATA);
1171 1.1.2.1 pgoyette if (m == NULL)
1172 1.1.2.1 pgoyette return m;
1173 1.1.2.1 pgoyette MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1174 1.1.2.1 pgoyette
1175 1.1.2.1 pgoyette if ((len = rt_getlen(type)) == -1)
1176 1.1.2.1 pgoyette goto out;
1177 1.1.2.1 pgoyette if (len > MHLEN + MLEN)
1178 1.1.2.1 pgoyette panic("%s: message too long", __func__);
1179 1.1.2.1 pgoyette else if (len > MHLEN) {
1180 1.1.2.1 pgoyette m->m_next = m_get(M_DONTWAIT, MT_DATA);
1181 1.1.2.1 pgoyette if (m->m_next == NULL)
1182 1.1.2.1 pgoyette goto out;
1183 1.1.2.1 pgoyette MCLAIM(m->m_next, m->m_owner);
1184 1.1.2.1 pgoyette m->m_pkthdr.len = len;
1185 1.1.2.1 pgoyette m->m_len = MHLEN;
1186 1.1.2.1 pgoyette m->m_next->m_len = len - MHLEN;
1187 1.1.2.1 pgoyette } else {
1188 1.1.2.1 pgoyette m->m_pkthdr.len = m->m_len = len;
1189 1.1.2.1 pgoyette }
1190 1.1.2.1 pgoyette m_reset_rcvif(m);
1191 1.1.2.1 pgoyette m_copyback(m, 0, datalen, data);
1192 1.1.2.1 pgoyette if (len > datalen)
1193 1.1.2.1 pgoyette (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1194 1.1.2.1 pgoyette rtm = mtod(m, struct rt_xmsghdr *);
1195 1.1.2.1 pgoyette for (i = 0; i < RTAX_MAX; i++) {
1196 1.1.2.1 pgoyette if ((sa = rtinfo->rti_info[i]) == NULL)
1197 1.1.2.1 pgoyette continue;
1198 1.1.2.1 pgoyette rtinfo->rti_addrs |= (1 << i);
1199 1.1.2.1 pgoyette dlen = RT_XROUNDUP(sa->sa_len);
1200 1.1.2.1 pgoyette m_copyback(m, len, sa->sa_len, sa);
1201 1.1.2.1 pgoyette if (dlen != sa->sa_len) {
1202 1.1.2.1 pgoyette /*
1203 1.1.2.1 pgoyette * Up to 7 + 1 nul's since roundup is to
1204 1.1.2.1 pgoyette * sizeof(uint64_t) (8 bytes)
1205 1.1.2.1 pgoyette */
1206 1.1.2.1 pgoyette m_copyback(m, len + sa->sa_len,
1207 1.1.2.1 pgoyette dlen - sa->sa_len, "\0\0\0\0\0\0\0");
1208 1.1.2.1 pgoyette }
1209 1.1.2.1 pgoyette len += dlen;
1210 1.1.2.1 pgoyette }
1211 1.1.2.1 pgoyette if (m->m_pkthdr.len != len)
1212 1.1.2.1 pgoyette goto out;
1213 1.1.2.1 pgoyette rtm->rtm_msglen = len;
1214 1.1.2.1 pgoyette rtm->rtm_version = RTM_XVERSION;
1215 1.1.2.1 pgoyette rtm->rtm_type = type;
1216 1.1.2.1 pgoyette return m;
1217 1.1.2.1 pgoyette out:
1218 1.1.2.1 pgoyette m_freem(m);
1219 1.1.2.1 pgoyette return NULL;
1220 1.1.2.1 pgoyette }
1221 1.1.2.1 pgoyette
1222 1.1.2.1 pgoyette /*
1223 1.1.2.1 pgoyette * rt_msg2
1224 1.1.2.1 pgoyette *
1225 1.1.2.1 pgoyette * fills 'cp' or 'w'.w_tmem with the routing socket message and
1226 1.1.2.1 pgoyette * returns the length of the message in 'lenp'.
1227 1.1.2.1 pgoyette *
1228 1.1.2.1 pgoyette * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1229 1.1.2.1 pgoyette * the message
1230 1.1.2.1 pgoyette * otherwise walkarg's w_needed is updated and if the user buffer is
1231 1.1.2.1 pgoyette * specified and w_needed indicates space exists the information is copied
1232 1.1.2.1 pgoyette * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1233 1.1.2.1 pgoyette * if the allocation fails ENOBUFS is returned.
1234 1.1.2.1 pgoyette */
1235 1.1.2.1 pgoyette static int
1236 1.1.2.1 pgoyette rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1237 1.1.2.1 pgoyette int *lenp)
1238 1.1.2.1 pgoyette {
1239 1.1.2.1 pgoyette int i;
1240 1.1.2.1 pgoyette int len, dlen, second_time = 0;
1241 1.1.2.1 pgoyette char *cp0, *cp = cpv;
1242 1.1.2.1 pgoyette
1243 1.1.2.1 pgoyette rtinfo->rti_addrs = 0;
1244 1.1.2.1 pgoyette again:
1245 1.1.2.1 pgoyette if ((len = rt_getlen(type)) == -1)
1246 1.1.2.1 pgoyette return EINVAL;
1247 1.1.2.1 pgoyette
1248 1.1.2.1 pgoyette if ((cp0 = cp) != NULL)
1249 1.1.2.1 pgoyette cp += len;
1250 1.1.2.1 pgoyette for (i = 0; i < RTAX_MAX; i++) {
1251 1.1.2.1 pgoyette const struct sockaddr *sa;
1252 1.1.2.1 pgoyette
1253 1.1.2.1 pgoyette if ((sa = rtinfo->rti_info[i]) == NULL)
1254 1.1.2.1 pgoyette continue;
1255 1.1.2.1 pgoyette rtinfo->rti_addrs |= (1 << i);
1256 1.1.2.1 pgoyette dlen = RT_XROUNDUP(sa->sa_len);
1257 1.1.2.1 pgoyette if (cp) {
1258 1.1.2.1 pgoyette int diff = dlen - sa->sa_len;
1259 1.1.2.1 pgoyette (void)memcpy(cp, sa, (size_t)sa->sa_len);
1260 1.1.2.1 pgoyette cp += sa->sa_len;
1261 1.1.2.1 pgoyette if (diff > 0) {
1262 1.1.2.1 pgoyette (void)memset(cp, 0, (size_t)diff);
1263 1.1.2.1 pgoyette cp += diff;
1264 1.1.2.1 pgoyette }
1265 1.1.2.1 pgoyette }
1266 1.1.2.1 pgoyette len += dlen;
1267 1.1.2.1 pgoyette }
1268 1.1.2.1 pgoyette if (cp == NULL && w != NULL && !second_time) {
1269 1.1.2.1 pgoyette struct rt_walkarg *rw = w;
1270 1.1.2.1 pgoyette
1271 1.1.2.1 pgoyette rw->w_needed += len;
1272 1.1.2.1 pgoyette if (rw->w_needed <= 0 && rw->w_where) {
1273 1.1.2.1 pgoyette if (rw->w_tmemsize < len) {
1274 1.1.2.1 pgoyette if (rw->w_tmem)
1275 1.1.2.1 pgoyette kmem_free(rw->w_tmem, rw->w_tmemsize);
1276 1.1.2.1 pgoyette rw->w_tmem = kmem_zalloc(len, KM_SLEEP);
1277 1.1.2.1 pgoyette rw->w_tmemsize = len;
1278 1.1.2.1 pgoyette }
1279 1.1.2.1 pgoyette if (rw->w_tmem) {
1280 1.1.2.1 pgoyette cp = rw->w_tmem;
1281 1.1.2.1 pgoyette second_time = 1;
1282 1.1.2.1 pgoyette goto again;
1283 1.1.2.1 pgoyette } else {
1284 1.1.2.1 pgoyette rw->w_tmemneeded = len;
1285 1.1.2.1 pgoyette return ENOBUFS;
1286 1.1.2.1 pgoyette }
1287 1.1.2.1 pgoyette }
1288 1.1.2.1 pgoyette }
1289 1.1.2.1 pgoyette if (cp) {
1290 1.1.2.1 pgoyette struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1291 1.1.2.1 pgoyette
1292 1.1.2.1 pgoyette rtm->rtm_version = RTM_XVERSION;
1293 1.1.2.1 pgoyette rtm->rtm_type = type;
1294 1.1.2.1 pgoyette rtm->rtm_msglen = len;
1295 1.1.2.1 pgoyette }
1296 1.1.2.1 pgoyette if (lenp)
1297 1.1.2.1 pgoyette *lenp = len;
1298 1.1.2.1 pgoyette return 0;
1299 1.1.2.1 pgoyette }
1300 1.1.2.1 pgoyette
1301 1.1.2.1 pgoyette /*
1302 1.1.2.1 pgoyette * MODULE_HOOK glue for rtsock_14_oifmsg and rtsock_14_iflist
1303 1.1.2.1 pgoyette */
1304 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_14_oifmsg_hook, (struct ifnet *ifp));
1305 1.1.2.1 pgoyette
1306 1.1.2.1 pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_14_iflist_hook,
1307 1.1.2.1 pgoyette (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
1308 1.1.2.1 pgoyette size_t len));
1309 1.1.2.1 pgoyette
1310 1.1.2.1 pgoyette /*
1311 1.1.2.1 pgoyette * MODULE_HOOK glue for rtsock_50 ifaddr_list and various message routines
1312 1.1.2.1 pgoyette */
1313 1.1.2.1 pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_50_iflist_hook,
1314 1.1.2.1 pgoyette (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
1315 1.1.2.1 pgoyette size_t len));
1316 1.1.2.1 pgoyette
1317 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_missmsg_hook,
1318 1.1.2.1 pgoyette (int, const struct rt_addrinfo *, int, int));
1319 1.1.2.1 pgoyette
1320 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifmsg_hook, (struct ifnet *));
1321 1.1.2.1 pgoyette
1322 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_newaddrmsg_hook,
1323 1.1.2.1 pgoyette (int, struct ifaddr *, int, struct rtentry *));
1324 1.1.2.1 pgoyette
1325 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifannouncemsg_hook,
1326 1.1.2.1 pgoyette (struct ifnet *, int what));
1327 1.1.2.1 pgoyette
1328 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ieee80211msg_hook,
1329 1.1.2.1 pgoyette (struct ifnet *, int, void *, size_t));
1330 1.1.2.1 pgoyette
1331 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_oifmsg_hook, (struct ifnet *ifp));
1332 1.1.2.1 pgoyette
1333 1.1.2.1 pgoyette /*
1334 1.1.2.1 pgoyette * MODULE_HOOK glue for rtsock70_newaddrmsg1, rtsock70_ifaddr_listaddr,
1335 1.1.2.1 pgoyette * and rtsock70_ifaddr_listif
1336 1.1.2.1 pgoyette */
1337 1.1.2.1 pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_70_newaddr_hook, (int, struct ifaddr *));
1338 1.1.2.1 pgoyette
1339 1.1.2.1 pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_70_iflist_hook,
1340 1.1.2.1 pgoyette (struct rt_walkarg *, struct ifaddr *, struct rt_addrinfo *));
1341 1.1.2.1 pgoyette
1342 1.1.2.1 pgoyette /*
1343 1.1.2.1 pgoyette * This routine is called to generate a message from the routing
1344 1.1.2.1 pgoyette * socket indicating that a redirect has occurred, a routing lookup
1345 1.1.2.1 pgoyette * has failed, or that a protocol has detected timeouts to a particular
1346 1.1.2.1 pgoyette * destination.
1347 1.1.2.1 pgoyette */
1348 1.1.2.1 pgoyette void
1349 1.1.2.1 pgoyette COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1350 1.1.2.1 pgoyette int error)
1351 1.1.2.1 pgoyette {
1352 1.1.2.1 pgoyette struct rt_xmsghdr rtm;
1353 1.1.2.1 pgoyette struct mbuf *m;
1354 1.1.2.1 pgoyette const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1355 1.1.2.1 pgoyette struct rt_addrinfo info = *rtinfo;
1356 1.1.2.1 pgoyette
1357 1.1.2.1 pgoyette COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1358 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count == 0)
1359 1.1.2.1 pgoyette return;
1360 1.1.2.1 pgoyette memset(&rtm, 0, sizeof(rtm));
1361 1.1.2.1 pgoyette rtm.rtm_pid = curproc->p_pid;
1362 1.1.2.1 pgoyette rtm.rtm_flags = RTF_DONE | flags;
1363 1.1.2.1 pgoyette rtm.rtm_errno = error;
1364 1.1.2.1 pgoyette m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1365 1.1.2.1 pgoyette if (m == NULL)
1366 1.1.2.1 pgoyette return;
1367 1.1.2.1 pgoyette mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1368 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1369 1.1.2.1 pgoyette }
1370 1.1.2.1 pgoyette
1371 1.1.2.1 pgoyette /*
1372 1.1.2.1 pgoyette * This routine is called to generate a message from the routing
1373 1.1.2.1 pgoyette * socket indicating that the status of a network interface has changed.
1374 1.1.2.1 pgoyette */
1375 1.1.2.1 pgoyette void
1376 1.1.2.1 pgoyette COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1377 1.1.2.1 pgoyette {
1378 1.1.2.1 pgoyette struct if_xmsghdr ifm;
1379 1.1.2.1 pgoyette struct mbuf *m;
1380 1.1.2.1 pgoyette struct rt_addrinfo info;
1381 1.1.2.1 pgoyette
1382 1.1.2.1 pgoyette COMPATCALL(rt_ifmsg, (ifp));
1383 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count == 0)
1384 1.1.2.1 pgoyette return;
1385 1.1.2.1 pgoyette (void)memset(&info, 0, sizeof(info));
1386 1.1.2.1 pgoyette (void)memset(&ifm, 0, sizeof(ifm));
1387 1.1.2.1 pgoyette ifm.ifm_index = ifp->if_index;
1388 1.1.2.1 pgoyette ifm.ifm_flags = ifp->if_flags;
1389 1.1.2.1 pgoyette ifm.ifm_data = ifp->if_data;
1390 1.1.2.1 pgoyette ifm.ifm_addrs = 0;
1391 1.1.2.1 pgoyette m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1392 1.1.2.1 pgoyette if (m == NULL)
1393 1.1.2.1 pgoyette return;
1394 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(m, 0);
1395 1.1.2.1 pgoyette rtsock_14_oifmsg_hook_call(ifp);
1396 1.1.2.1 pgoyette rtsock_50_oifmsg_hook_call(ifp);
1397 1.1.2.1 pgoyette }
1398 1.1.2.1 pgoyette
1399 1.1.2.1 pgoyette /*
1400 1.1.2.1 pgoyette * This is called to generate messages from the routing socket
1401 1.1.2.1 pgoyette * indicating a network interface has had addresses associated with it.
1402 1.1.2.1 pgoyette * if we ever reverse the logic and replace messages TO the routing
1403 1.1.2.1 pgoyette * socket indicate a request to configure interfaces, then it will
1404 1.1.2.1 pgoyette * be unnecessary as the routing socket will automatically generate
1405 1.1.2.1 pgoyette * copies of it.
1406 1.1.2.1 pgoyette */
1407 1.1.2.1 pgoyette void
1408 1.1.2.1 pgoyette COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1409 1.1.2.1 pgoyette struct rtentry *rt)
1410 1.1.2.1 pgoyette {
1411 1.1.2.1 pgoyette #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1412 1.1.2.1 pgoyette struct rt_addrinfo info;
1413 1.1.2.1 pgoyette const struct sockaddr *sa;
1414 1.1.2.1 pgoyette int pass;
1415 1.1.2.1 pgoyette struct mbuf *m;
1416 1.1.2.1 pgoyette struct ifnet *ifp;
1417 1.1.2.1 pgoyette struct rt_xmsghdr rtm;
1418 1.1.2.1 pgoyette struct ifa_xmsghdr ifam;
1419 1.1.2.1 pgoyette int ncmd;
1420 1.1.2.1 pgoyette
1421 1.1.2.1 pgoyette KASSERT(ifa != NULL);
1422 1.1.2.1 pgoyette KASSERT(ifa->ifa_addr != NULL);
1423 1.1.2.1 pgoyette ifp = ifa->ifa_ifp;
1424 1.1.2.3 pgoyette if (cmd == RTM_ADD && vec_sctp_add_ip_address != NULL) {
1425 1.1.2.3 pgoyette (*vec_sctp_add_ip_address)(ifa);
1426 1.1.2.3 pgoyette } else if (cmd == RTM_DELETE && vec_sctp_delete_ip_address != NULL) {
1427 1.1.2.3 pgoyette (*vec_sctp_delete_ip_address)(ifa);
1428 1.1.2.1 pgoyette }
1429 1.1.2.1 pgoyette
1430 1.1.2.1 pgoyette COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1431 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count == 0)
1432 1.1.2.1 pgoyette return;
1433 1.1.2.1 pgoyette for (pass = 1; pass < 3; pass++) {
1434 1.1.2.1 pgoyette memset(&info, 0, sizeof(info));
1435 1.1.2.1 pgoyette switch (cmdpass(cmd, pass)) {
1436 1.1.2.1 pgoyette case cmdpass(RTM_ADD, 1):
1437 1.1.2.1 pgoyette case cmdpass(RTM_CHANGE, 1):
1438 1.1.2.1 pgoyette case cmdpass(RTM_DELETE, 2):
1439 1.1.2.1 pgoyette case cmdpass(RTM_NEWADDR, 1):
1440 1.1.2.1 pgoyette case cmdpass(RTM_DELADDR, 1):
1441 1.1.2.1 pgoyette case cmdpass(RTM_CHGADDR, 1):
1442 1.1.2.1 pgoyette switch (cmd) {
1443 1.1.2.1 pgoyette case RTM_ADD:
1444 1.1.2.1 pgoyette ncmd = RTM_XNEWADDR;
1445 1.1.2.1 pgoyette break;
1446 1.1.2.1 pgoyette case RTM_DELETE:
1447 1.1.2.1 pgoyette ncmd = RTM_XDELADDR;
1448 1.1.2.1 pgoyette break;
1449 1.1.2.1 pgoyette case RTM_CHANGE:
1450 1.1.2.1 pgoyette ncmd = RTM_XCHGADDR;
1451 1.1.2.1 pgoyette break;
1452 1.1.2.1 pgoyette case RTM_NEWADDR:
1453 1.1.2.1 pgoyette ncmd = RTM_XNEWADDR;
1454 1.1.2.1 pgoyette break;
1455 1.1.2.1 pgoyette case RTM_DELADDR:
1456 1.1.2.1 pgoyette ncmd = RTM_XDELADDR;
1457 1.1.2.1 pgoyette break;
1458 1.1.2.1 pgoyette case RTM_CHGADDR:
1459 1.1.2.1 pgoyette ncmd = RTM_XCHGADDR;
1460 1.1.2.1 pgoyette break;
1461 1.1.2.1 pgoyette default:
1462 1.1.2.1 pgoyette panic("%s: unknown command %d", __func__, cmd);
1463 1.1.2.1 pgoyette }
1464 1.1.2.1 pgoyette rtsock_70_newaddr_hook_call(ncmd, ifa);
1465 1.1.2.1 pgoyette info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1466 1.1.2.1 pgoyette KASSERT(ifp->if_dl != NULL);
1467 1.1.2.1 pgoyette info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1468 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1469 1.1.2.1 pgoyette info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1470 1.1.2.1 pgoyette memset(&ifam, 0, sizeof(ifam));
1471 1.1.2.1 pgoyette ifam.ifam_index = ifp->if_index;
1472 1.1.2.1 pgoyette ifam.ifam_metric = ifa->ifa_metric;
1473 1.1.2.1 pgoyette ifam.ifam_flags = ifa->ifa_flags;
1474 1.1.2.1 pgoyette #ifndef COMPAT_RTSOCK
1475 1.1.2.1 pgoyette ifam.ifam_pid = curproc->p_pid;
1476 1.1.2.1 pgoyette ifam.ifam_addrflags = if_addrflags(ifa);
1477 1.1.2.1 pgoyette #endif
1478 1.1.2.1 pgoyette m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1479 1.1.2.1 pgoyette if (m == NULL)
1480 1.1.2.1 pgoyette continue;
1481 1.1.2.1 pgoyette mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1482 1.1.2.1 pgoyette info.rti_addrs;
1483 1.1.2.1 pgoyette break;
1484 1.1.2.1 pgoyette case cmdpass(RTM_ADD, 2):
1485 1.1.2.1 pgoyette case cmdpass(RTM_CHANGE, 2):
1486 1.1.2.1 pgoyette case cmdpass(RTM_DELETE, 1):
1487 1.1.2.1 pgoyette if (rt == NULL)
1488 1.1.2.1 pgoyette continue;
1489 1.1.2.1 pgoyette info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1490 1.1.2.1 pgoyette info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1491 1.1.2.1 pgoyette info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1492 1.1.2.1 pgoyette memset(&rtm, 0, sizeof(rtm));
1493 1.1.2.1 pgoyette rtm.rtm_pid = curproc->p_pid;
1494 1.1.2.1 pgoyette rtm.rtm_index = ifp->if_index;
1495 1.1.2.1 pgoyette rtm.rtm_flags |= rt->rt_flags;
1496 1.1.2.1 pgoyette rtm.rtm_errno = error;
1497 1.1.2.1 pgoyette m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1498 1.1.2.1 pgoyette if (m == NULL)
1499 1.1.2.1 pgoyette continue;
1500 1.1.2.1 pgoyette mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1501 1.1.2.1 pgoyette break;
1502 1.1.2.1 pgoyette default:
1503 1.1.2.1 pgoyette continue;
1504 1.1.2.1 pgoyette }
1505 1.1.2.1 pgoyette KASSERTMSG(m != NULL, "called with wrong command");
1506 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1507 1.1.2.1 pgoyette }
1508 1.1.2.1 pgoyette #undef cmdpass
1509 1.1.2.1 pgoyette }
1510 1.1.2.1 pgoyette
1511 1.1.2.1 pgoyette static struct mbuf *
1512 1.1.2.1 pgoyette rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1513 1.1.2.1 pgoyette struct rt_addrinfo *info)
1514 1.1.2.1 pgoyette {
1515 1.1.2.1 pgoyette struct if_xannouncemsghdr ifan;
1516 1.1.2.1 pgoyette
1517 1.1.2.1 pgoyette memset(info, 0, sizeof(*info));
1518 1.1.2.1 pgoyette memset(&ifan, 0, sizeof(ifan));
1519 1.1.2.1 pgoyette ifan.ifan_index = ifp->if_index;
1520 1.1.2.1 pgoyette strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1521 1.1.2.1 pgoyette ifan.ifan_what = what;
1522 1.1.2.1 pgoyette return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1523 1.1.2.1 pgoyette }
1524 1.1.2.1 pgoyette
1525 1.1.2.1 pgoyette /*
1526 1.1.2.1 pgoyette * This is called to generate routing socket messages indicating
1527 1.1.2.1 pgoyette * network interface arrival and departure.
1528 1.1.2.1 pgoyette */
1529 1.1.2.1 pgoyette void
1530 1.1.2.1 pgoyette COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1531 1.1.2.1 pgoyette {
1532 1.1.2.1 pgoyette struct mbuf *m;
1533 1.1.2.1 pgoyette struct rt_addrinfo info;
1534 1.1.2.1 pgoyette
1535 1.1.2.1 pgoyette COMPATCALL(rt_ifannouncemsg, (ifp, what));
1536 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count == 0)
1537 1.1.2.1 pgoyette return;
1538 1.1.2.1 pgoyette m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1539 1.1.2.1 pgoyette if (m == NULL)
1540 1.1.2.1 pgoyette return;
1541 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(m, 0);
1542 1.1.2.1 pgoyette }
1543 1.1.2.1 pgoyette
1544 1.1.2.1 pgoyette /*
1545 1.1.2.1 pgoyette * This is called to generate routing socket messages indicating
1546 1.1.2.1 pgoyette * IEEE80211 wireless events.
1547 1.1.2.1 pgoyette * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1548 1.1.2.1 pgoyette */
1549 1.1.2.1 pgoyette void
1550 1.1.2.1 pgoyette COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1551 1.1.2.1 pgoyette size_t data_len)
1552 1.1.2.1 pgoyette {
1553 1.1.2.1 pgoyette struct mbuf *m;
1554 1.1.2.1 pgoyette struct rt_addrinfo info;
1555 1.1.2.1 pgoyette
1556 1.1.2.1 pgoyette COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1557 1.1.2.1 pgoyette if (COMPATNAME(route_info).ri_cb.any_count == 0)
1558 1.1.2.1 pgoyette return;
1559 1.1.2.1 pgoyette m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1560 1.1.2.1 pgoyette if (m == NULL)
1561 1.1.2.1 pgoyette return;
1562 1.1.2.1 pgoyette /*
1563 1.1.2.1 pgoyette * Append the ieee80211 data. Try to stick it in the
1564 1.1.2.1 pgoyette * mbuf containing the ifannounce msg; otherwise allocate
1565 1.1.2.1 pgoyette * a new mbuf and append.
1566 1.1.2.1 pgoyette *
1567 1.1.2.1 pgoyette * NB: we assume m is a single mbuf.
1568 1.1.2.1 pgoyette */
1569 1.1.2.1 pgoyette if (data_len > M_TRAILINGSPACE(m)) {
1570 1.1.2.1 pgoyette struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1571 1.1.2.1 pgoyette if (n == NULL) {
1572 1.1.2.1 pgoyette m_freem(m);
1573 1.1.2.1 pgoyette return;
1574 1.1.2.1 pgoyette }
1575 1.1.2.1 pgoyette (void)memcpy(mtod(n, void *), data, data_len);
1576 1.1.2.1 pgoyette n->m_len = data_len;
1577 1.1.2.1 pgoyette m->m_next = n;
1578 1.1.2.1 pgoyette } else if (data_len > 0) {
1579 1.1.2.1 pgoyette (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1580 1.1.2.1 pgoyette m->m_len += data_len;
1581 1.1.2.1 pgoyette }
1582 1.1.2.1 pgoyette if (m->m_flags & M_PKTHDR)
1583 1.1.2.1 pgoyette m->m_pkthdr.len += data_len;
1584 1.1.2.1 pgoyette mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1585 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(m, 0);
1586 1.1.2.1 pgoyette }
1587 1.1.2.1 pgoyette
1588 1.1.2.1 pgoyette /*
1589 1.1.2.1 pgoyette * Routing message software interrupt routine
1590 1.1.2.1 pgoyette */
1591 1.1.2.1 pgoyette static void
1592 1.1.2.1 pgoyette COMPATNAME(route_intr)(void *cookie)
1593 1.1.2.1 pgoyette {
1594 1.1.2.1 pgoyette struct sockproto proto = { .sp_family = PF_XROUTE, };
1595 1.1.2.1 pgoyette struct route_info * const ri = &COMPATNAME(route_info);
1596 1.1.2.1 pgoyette struct mbuf *m;
1597 1.1.2.1 pgoyette
1598 1.1.2.1 pgoyette SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
1599 1.1.2.1 pgoyette for (;;) {
1600 1.1.2.1 pgoyette IFQ_LOCK(&ri->ri_intrq);
1601 1.1.2.1 pgoyette IF_DEQUEUE(&ri->ri_intrq, m);
1602 1.1.2.1 pgoyette IFQ_UNLOCK(&ri->ri_intrq);
1603 1.1.2.1 pgoyette if (m == NULL)
1604 1.1.2.1 pgoyette break;
1605 1.1.2.1 pgoyette proto.sp_protocol = M_GETCTX(m, uintptr_t);
1606 1.1.2.1 pgoyette #ifdef NET_MPSAFE
1607 1.1.2.1 pgoyette mutex_enter(rt_so_mtx);
1608 1.1.2.1 pgoyette #endif
1609 1.1.2.1 pgoyette raw_input(m, &proto, &ri->ri_src, &ri->ri_dst, &rt_rawcb);
1610 1.1.2.1 pgoyette #ifdef NET_MPSAFE
1611 1.1.2.1 pgoyette mutex_exit(rt_so_mtx);
1612 1.1.2.1 pgoyette #endif
1613 1.1.2.1 pgoyette }
1614 1.1.2.1 pgoyette SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1615 1.1.2.1 pgoyette }
1616 1.1.2.1 pgoyette
1617 1.1.2.1 pgoyette /*
1618 1.1.2.1 pgoyette * Enqueue a message to the software interrupt routine.
1619 1.1.2.1 pgoyette */
1620 1.1.2.1 pgoyette void
1621 1.1.2.1 pgoyette COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1622 1.1.2.1 pgoyette {
1623 1.1.2.1 pgoyette struct route_info * const ri = &COMPATNAME(route_info);
1624 1.1.2.1 pgoyette int wasempty;
1625 1.1.2.1 pgoyette
1626 1.1.2.1 pgoyette IFQ_LOCK(&ri->ri_intrq);
1627 1.1.2.1 pgoyette if (IF_QFULL(&ri->ri_intrq)) {
1628 1.1.2.1 pgoyette printf("%s: queue full, dropped message\n", __func__);
1629 1.1.2.1 pgoyette IF_DROP(&ri->ri_intrq);
1630 1.1.2.1 pgoyette IFQ_UNLOCK(&ri->ri_intrq);
1631 1.1.2.1 pgoyette m_freem(m);
1632 1.1.2.1 pgoyette } else {
1633 1.1.2.1 pgoyette wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1634 1.1.2.1 pgoyette M_SETCTX(m, (uintptr_t)family);
1635 1.1.2.1 pgoyette IF_ENQUEUE(&ri->ri_intrq, m);
1636 1.1.2.1 pgoyette IFQ_UNLOCK(&ri->ri_intrq);
1637 1.1.2.1 pgoyette if (wasempty) {
1638 1.1.2.1 pgoyette kpreempt_disable();
1639 1.1.2.1 pgoyette softint_schedule(ri->ri_sih);
1640 1.1.2.1 pgoyette kpreempt_enable();
1641 1.1.2.1 pgoyette }
1642 1.1.2.1 pgoyette }
1643 1.1.2.1 pgoyette }
1644 1.1.2.1 pgoyette
1645 1.1.2.1 pgoyette static void
1646 1.1.2.1 pgoyette COMPATNAME(route_init)(void)
1647 1.1.2.1 pgoyette {
1648 1.1.2.1 pgoyette struct route_info * const ri = &COMPATNAME(route_info);
1649 1.1.2.1 pgoyette
1650 1.1.2.1 pgoyette #ifndef COMPAT_RTSOCK
1651 1.1.2.1 pgoyette rt_init();
1652 1.1.2.1 pgoyette #endif
1653 1.1.2.1 pgoyette #ifdef NET_MPSAFE
1654 1.1.2.1 pgoyette rt_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1655 1.1.2.1 pgoyette
1656 1.1.2.1 pgoyette cv_init(&rt_update_cv, "rtsock_cv");
1657 1.1.2.1 pgoyette #endif
1658 1.1.2.1 pgoyette
1659 1.1.2.1 pgoyette #ifndef COMPAT_RTSOCK
1660 1.1.2.1 pgoyette sysctl_net_route_setup(NULL);
1661 1.1.2.1 pgoyette #endif
1662 1.1.2.1 pgoyette ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1663 1.1.2.1 pgoyette ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1664 1.1.2.1 pgoyette COMPATNAME(route_intr), NULL);
1665 1.1.2.1 pgoyette IFQ_LOCK_INIT(&ri->ri_intrq);
1666 1.1.2.1 pgoyette }
1667 1.1.2.1 pgoyette
1668 1.1.2.1 pgoyette /*
1669 1.1.2.1 pgoyette * Definitions of protocols supported in the ROUTE domain.
1670 1.1.2.1 pgoyette */
1671 1.1.2.1 pgoyette #ifndef COMPAT_RTSOCK
1672 1.1.2.1 pgoyette PR_WRAP_USRREQS(route);
1673 1.1.2.1 pgoyette #else
1674 1.1.2.1 pgoyette PR_WRAP_USRREQS(compat_50_route);
1675 1.1.2.1 pgoyette #endif
1676 1.1.2.1 pgoyette
1677 1.1.2.1 pgoyette static const struct pr_usrreqs route_usrreqs = {
1678 1.1.2.1 pgoyette .pr_attach = COMPATNAME(route_attach_wrapper),
1679 1.1.2.1 pgoyette .pr_detach = COMPATNAME(route_detach_wrapper),
1680 1.1.2.1 pgoyette .pr_accept = COMPATNAME(route_accept_wrapper),
1681 1.1.2.1 pgoyette .pr_bind = COMPATNAME(route_bind_wrapper),
1682 1.1.2.1 pgoyette .pr_listen = COMPATNAME(route_listen_wrapper),
1683 1.1.2.1 pgoyette .pr_connect = COMPATNAME(route_connect_wrapper),
1684 1.1.2.1 pgoyette .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1685 1.1.2.1 pgoyette .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1686 1.1.2.1 pgoyette .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1687 1.1.2.1 pgoyette .pr_abort = COMPATNAME(route_abort_wrapper),
1688 1.1.2.1 pgoyette .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1689 1.1.2.1 pgoyette .pr_stat = COMPATNAME(route_stat_wrapper),
1690 1.1.2.1 pgoyette .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1691 1.1.2.1 pgoyette .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1692 1.1.2.1 pgoyette .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1693 1.1.2.1 pgoyette .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1694 1.1.2.1 pgoyette .pr_send = COMPATNAME(route_send_wrapper),
1695 1.1.2.1 pgoyette .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1696 1.1.2.1 pgoyette .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1697 1.1.2.1 pgoyette };
1698 1.1.2.1 pgoyette
1699 1.1.2.1 pgoyette static const struct protosw COMPATNAME(route_protosw)[] = {
1700 1.1.2.1 pgoyette {
1701 1.1.2.1 pgoyette .pr_type = SOCK_RAW,
1702 1.1.2.1 pgoyette .pr_domain = &COMPATNAME(routedomain),
1703 1.1.2.1 pgoyette .pr_flags = PR_ATOMIC|PR_ADDR,
1704 1.1.2.1 pgoyette .pr_ctlinput = raw_ctlinput,
1705 1.1.2.1 pgoyette .pr_ctloutput = route_ctloutput,
1706 1.1.2.1 pgoyette .pr_usrreqs = &route_usrreqs,
1707 1.1.2.1 pgoyette .pr_init = rt_pr_init,
1708 1.1.2.1 pgoyette },
1709 1.1.2.1 pgoyette };
1710 1.1.2.1 pgoyette
1711 1.1.2.1 pgoyette struct domain COMPATNAME(routedomain) = {
1712 1.1.2.1 pgoyette .dom_family = PF_XROUTE,
1713 1.1.2.1 pgoyette .dom_name = DOMAINNAME,
1714 1.1.2.1 pgoyette .dom_init = COMPATNAME(route_init),
1715 1.1.2.1 pgoyette .dom_protosw = COMPATNAME(route_protosw),
1716 1.1.2.1 pgoyette .dom_protoswNPROTOSW =
1717 1.1.2.1 pgoyette &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1718 1.1.2.1 pgoyette };
1719