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