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