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