rtsock.c revision 1.126 1 1.126 tsutsui /* $NetBSD: rtsock.c,v 1.126 2009/09/12 18:09:25 tsutsui Exp $ */
2 1.30 itojun
3 1.30 itojun /*
4 1.30 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.30 itojun * All rights reserved.
6 1.75 perry *
7 1.30 itojun * Redistribution and use in source and binary forms, with or without
8 1.30 itojun * modification, are permitted provided that the following conditions
9 1.30 itojun * are met:
10 1.30 itojun * 1. Redistributions of source code must retain the above copyright
11 1.30 itojun * notice, this list of conditions and the following disclaimer.
12 1.30 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.30 itojun * notice, this list of conditions and the following disclaimer in the
14 1.30 itojun * documentation and/or other materials provided with the distribution.
15 1.30 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.30 itojun * may be used to endorse or promote products derived from this software
17 1.30 itojun * without specific prior written permission.
18 1.75 perry *
19 1.30 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.30 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.30 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.30 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.30 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.30 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.30 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.30 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.30 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.30 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.30 itojun * SUCH DAMAGE.
30 1.30 itojun */
31 1.11 cgd
32 1.1 cgd /*
33 1.10 mycroft * Copyright (c) 1988, 1991, 1993
34 1.10 mycroft * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.64 agc * 3. Neither the name of the University nor the names of its contributors
45 1.1 cgd * may be used to endorse or promote products derived from this software
46 1.1 cgd * without specific prior written permission.
47 1.1 cgd *
48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 cgd * SUCH DAMAGE.
59 1.1 cgd *
60 1.26 fvdl * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 1.1 cgd */
62 1.54 lukem
63 1.54 lukem #include <sys/cdefs.h>
64 1.126 tsutsui __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.126 2009/09/12 18:09:25 tsutsui Exp $");
65 1.31 thorpej
66 1.31 thorpej #include "opt_inet.h"
67 1.120 christos #ifdef _KERNEL_OPT
68 1.120 christos #include "opt_compat_netbsd.h"
69 1.120 christos #endif
70 1.1 cgd
71 1.5 mycroft #include <sys/param.h>
72 1.5 mycroft #include <sys/systm.h>
73 1.10 mycroft #include <sys/proc.h>
74 1.5 mycroft #include <sys/mbuf.h>
75 1.5 mycroft #include <sys/socket.h>
76 1.5 mycroft #include <sys/socketvar.h>
77 1.5 mycroft #include <sys/domain.h>
78 1.5 mycroft #include <sys/protosw.h>
79 1.17 christos #include <sys/sysctl.h>
80 1.84 elad #include <sys/kauth.h>
81 1.99 ad #include <sys/intr.h>
82 1.91 dyoung #ifdef RTSOCK_DEBUG
83 1.91 dyoung #include <netinet/in.h>
84 1.91 dyoung #endif /* RTSOCK_DEBUG */
85 1.17 christos
86 1.5 mycroft #include <net/if.h>
87 1.5 mycroft #include <net/route.h>
88 1.5 mycroft #include <net/raw_cb.h>
89 1.1 cgd
90 1.120 christos #if defined(COMPAT_14) || defined(COMPAT_50)
91 1.120 christos #include <compat/net/if.h>
92 1.120 christos #endif
93 1.120 christos
94 1.17 christos #include <machine/stdarg.h>
95 1.17 christos
96 1.73 matt DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
97 1.58 matt
98 1.87 christos struct sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, };
99 1.87 christos struct sockaddr route_src = { .sa_len = 2, .sa_family = PF_ROUTE, };
100 1.99 ad
101 1.99 ad int route_maxqlen = IFQ_MAXLEN;
102 1.99 ad static struct ifqueue route_intrq;
103 1.99 ad static void *route_sih;
104 1.10 mycroft
105 1.120 christos static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
106 1.72 christos static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
107 1.78 dyoung static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
108 1.78 dyoung struct rt_addrinfo *);
109 1.94 dyoung static int sysctl_dumpentry(struct rtentry *, void *);
110 1.120 christos static int sysctl_iflist(int, struct rt_walkarg *, int);
111 1.69 matt static int sysctl_rtable(SYSCTLFN_PROTO);
112 1.123 yamt static void rt_adjustcount(int, int);
113 1.10 mycroft
114 1.123 yamt static void
115 1.69 matt rt_adjustcount(int af, int cnt)
116 1.27 christos {
117 1.28 christos route_cb.any_count += cnt;
118 1.27 christos switch (af) {
119 1.27 christos case AF_INET:
120 1.27 christos route_cb.ip_count += cnt;
121 1.27 christos return;
122 1.30 itojun #ifdef INET6
123 1.30 itojun case AF_INET6:
124 1.30 itojun route_cb.ip6_count += cnt;
125 1.30 itojun return;
126 1.30 itojun #endif
127 1.27 christos case AF_IPX:
128 1.27 christos route_cb.ipx_count += cnt;
129 1.27 christos return;
130 1.27 christos case AF_NS:
131 1.27 christos route_cb.ns_count += cnt;
132 1.27 christos return;
133 1.27 christos case AF_ISO:
134 1.27 christos route_cb.iso_count += cnt;
135 1.27 christos return;
136 1.27 christos }
137 1.27 christos }
138 1.123 yamt
139 1.123 yamt static void
140 1.120 christos cvtmetrics(struct rt_metrics *ortm, const struct nrt_metrics *rtm)
141 1.120 christos {
142 1.120 christos ortm->rmx_locks = rtm->rmx_locks;
143 1.120 christos ortm->rmx_mtu = rtm->rmx_mtu;
144 1.120 christos ortm->rmx_hopcount = rtm->rmx_hopcount;
145 1.120 christos ortm->rmx_expire = rtm->rmx_expire;
146 1.120 christos ortm->rmx_recvpipe = rtm->rmx_recvpipe;
147 1.120 christos ortm->rmx_sendpipe = rtm->rmx_sendpipe;
148 1.120 christos ortm->rmx_ssthresh = rtm->rmx_ssthresh;
149 1.120 christos ortm->rmx_rtt = rtm->rmx_rtt;
150 1.120 christos ortm->rmx_rttvar = rtm->rmx_rttvar;
151 1.120 christos ortm->rmx_pksent = rtm->rmx_pksent;
152 1.120 christos }
153 1.27 christos
154 1.1 cgd /*ARGSUSED*/
155 1.9 mycroft int
156 1.69 matt route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
157 1.79 christos struct mbuf *control, struct lwp *l)
158 1.1 cgd {
159 1.39 augustss int error = 0;
160 1.39 augustss struct rawcb *rp = sotorawcb(so);
161 1.1 cgd int s;
162 1.10 mycroft
163 1.1 cgd if (req == PRU_ATTACH) {
164 1.101 ad sosetlock(so);
165 1.118 cegger rp = malloc(sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
166 1.109 cube so->so_pcb = rp;
167 1.1 cgd }
168 1.27 christos if (req == PRU_DETACH && rp)
169 1.27 christos rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
170 1.14 mycroft s = splsoftnet();
171 1.23 thorpej
172 1.23 thorpej /*
173 1.23 thorpej * Don't call raw_usrreq() in the attach case, because
174 1.23 thorpej * we want to allow non-privileged processes to listen on
175 1.23 thorpej * and send "safe" commands to the routing socket.
176 1.23 thorpej */
177 1.23 thorpej if (req == PRU_ATTACH) {
178 1.95 dyoung if (l == NULL)
179 1.23 thorpej error = EACCES;
180 1.23 thorpej else
181 1.23 thorpej error = raw_attach(so, (int)(long)nam);
182 1.23 thorpej } else
183 1.79 christos error = raw_usrreq(so, req, m, nam, control, l);
184 1.23 thorpej
185 1.1 cgd rp = sotorawcb(so);
186 1.1 cgd if (req == PRU_ATTACH && rp) {
187 1.1 cgd if (error) {
188 1.112 dyoung free(rp, M_PCB);
189 1.1 cgd splx(s);
190 1.95 dyoung return error;
191 1.1 cgd }
192 1.27 christos rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
193 1.20 mycroft rp->rcb_laddr = &route_src;
194 1.20 mycroft rp->rcb_faddr = &route_dst;
195 1.1 cgd soisconnected(so);
196 1.1 cgd so->so_options |= SO_USELOOPBACK;
197 1.1 cgd }
198 1.1 cgd splx(s);
199 1.95 dyoung return error;
200 1.95 dyoung }
201 1.95 dyoung
202 1.95 dyoung static const struct sockaddr *
203 1.95 dyoung intern_netmask(const struct sockaddr *mask)
204 1.95 dyoung {
205 1.95 dyoung struct radix_node *rn;
206 1.95 dyoung extern struct radix_node_head *mask_rnhead;
207 1.95 dyoung
208 1.95 dyoung if (mask != NULL &&
209 1.95 dyoung (rn = rn_search(mask, mask_rnhead->rnh_treetop)))
210 1.95 dyoung mask = (const struct sockaddr *)rn->rn_key;
211 1.95 dyoung
212 1.95 dyoung return mask;
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd /*ARGSUSED*/
216 1.9 mycroft int
217 1.17 christos route_output(struct mbuf *m, ...)
218 1.1 cgd {
219 1.99 ad struct sockproto proto = { .sp_family = PF_ROUTE, };
220 1.95 dyoung struct rt_msghdr *rtm = NULL;
221 1.117 christos struct rt_msghdr *old_rtm = NULL;
222 1.95 dyoung struct rtentry *rt = NULL;
223 1.95 dyoung struct rtentry *saved_nrt = NULL;
224 1.10 mycroft struct rt_addrinfo info;
225 1.124 roy int len, error = 0;
226 1.95 dyoung struct ifnet *ifp = NULL;
227 1.124 roy struct ifaddr *ifa = NULL;
228 1.17 christos struct socket *so;
229 1.17 christos va_list ap;
230 1.55 christos sa_family_t family;
231 1.17 christos
232 1.17 christos va_start(ap, m);
233 1.17 christos so = va_arg(ap, struct socket *);
234 1.17 christos va_end(ap);
235 1.17 christos
236 1.56 perry #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
237 1.95 dyoung if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
238 1.95 dyoung (m = m_pullup(m, sizeof(int32_t))) == NULL))
239 1.95 dyoung return ENOBUFS;
240 1.1 cgd if ((m->m_flags & M_PKTHDR) == 0)
241 1.1 cgd panic("route_output");
242 1.1 cgd len = m->m_pkthdr.len;
243 1.1 cgd if (len < sizeof(*rtm) ||
244 1.10 mycroft len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
245 1.114 dyoung info.rti_info[RTAX_DST] = NULL;
246 1.1 cgd senderr(EINVAL);
247 1.10 mycroft }
248 1.1 cgd R_Malloc(rtm, struct rt_msghdr *, len);
249 1.95 dyoung if (rtm == NULL) {
250 1.114 dyoung info.rti_info[RTAX_DST] = NULL;
251 1.1 cgd senderr(ENOBUFS);
252 1.10 mycroft }
253 1.112 dyoung m_copydata(m, 0, len, rtm);
254 1.10 mycroft if (rtm->rtm_version != RTM_VERSION) {
255 1.114 dyoung info.rti_info[RTAX_DST] = NULL;
256 1.1 cgd senderr(EPROTONOSUPPORT);
257 1.10 mycroft }
258 1.1 cgd rtm->rtm_pid = curproc->p_pid;
259 1.48 thorpej memset(&info, 0, sizeof(info));
260 1.10 mycroft info.rti_addrs = rtm->rtm_addrs;
261 1.112 dyoung if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
262 1.112 dyoung &info))
263 1.42 erh senderr(EINVAL);
264 1.45 itojun info.rti_flags = rtm->rtm_flags;
265 1.91 dyoung #ifdef RTSOCK_DEBUG
266 1.114 dyoung if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
267 1.114 dyoung printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
268 1.115 christos inet_ntoa(((const struct sockaddr_in *)
269 1.115 christos info.rti_info[RTAX_DST])->sin_addr));
270 1.91 dyoung }
271 1.91 dyoung #endif /* RTSOCK_DEBUG */
272 1.115 christos if (info.rti_info[RTAX_DST] == NULL ||
273 1.115 christos (info.rti_info[RTAX_DST]->sa_family >= AF_MAX))
274 1.26 fvdl senderr(EINVAL);
275 1.115 christos if (info.rti_info[RTAX_GATEWAY] != NULL &&
276 1.115 christos (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
277 1.1 cgd senderr(EINVAL);
278 1.23 thorpej
279 1.23 thorpej /*
280 1.23 thorpej * Verify that the caller has the appropriate privilege; RTM_GET
281 1.23 thorpej * is the only operation the non-superuser is allowed.
282 1.23 thorpej */
283 1.88 elad if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
284 1.89 elad 0, rtm, NULL, NULL) != 0)
285 1.23 thorpej senderr(EACCES);
286 1.23 thorpej
287 1.1 cgd switch (rtm->rtm_type) {
288 1.10 mycroft
289 1.1 cgd case RTM_ADD:
290 1.114 dyoung if (info.rti_info[RTAX_GATEWAY] == NULL)
291 1.1 cgd senderr(EINVAL);
292 1.45 itojun error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
293 1.1 cgd if (error == 0 && saved_nrt) {
294 1.1 cgd rt_setmetrics(rtm->rtm_inits,
295 1.21 christos &rtm->rtm_rmx, &saved_nrt->rt_rmx);
296 1.1 cgd saved_nrt->rt_refcnt--;
297 1.1 cgd }
298 1.1 cgd break;
299 1.1 cgd
300 1.1 cgd case RTM_DELETE:
301 1.45 itojun error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
302 1.16 cgd if (error == 0) {
303 1.16 cgd (rt = saved_nrt)->rt_refcnt++;
304 1.16 cgd goto report;
305 1.16 cgd }
306 1.1 cgd break;
307 1.1 cgd
308 1.1 cgd case RTM_GET:
309 1.1 cgd case RTM_CHANGE:
310 1.1 cgd case RTM_LOCK:
311 1.115 christos /* XXX This will mask info.rti_info[RTAX_DST] with
312 1.115 christos * info.rti_info[RTAX_NETMASK] before
313 1.95 dyoung * searching. It did not used to do that. --dyoung
314 1.95 dyoung */
315 1.103 dyoung error = rtrequest1(RTM_GET, &info, &rt);
316 1.95 dyoung if (error != 0)
317 1.95 dyoung senderr(error);
318 1.61 itojun if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
319 1.95 dyoung struct radix_node *rn;
320 1.61 itojun
321 1.115 christos if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
322 1.115 christos info.rti_info[RTAX_DST]->sa_len) != 0)
323 1.61 itojun senderr(ESRCH);
324 1.115 christos info.rti_info[RTAX_NETMASK] = intern_netmask(
325 1.115 christos info.rti_info[RTAX_NETMASK]);
326 1.95 dyoung for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
327 1.115 christos if (info.rti_info[RTAX_NETMASK] ==
328 1.115 christos (const struct sockaddr *)rn->rn_mask)
329 1.61 itojun break;
330 1.95 dyoung if (rn == NULL)
331 1.61 itojun senderr(ETOOMANYREFS);
332 1.95 dyoung rt = (struct rtentry *)rn;
333 1.61 itojun }
334 1.37 itojun
335 1.59 itojun switch (rtm->rtm_type) {
336 1.1 cgd case RTM_GET:
337 1.16 cgd report:
338 1.114 dyoung info.rti_info[RTAX_DST] = rt_getkey(rt);
339 1.114 dyoung info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
340 1.114 dyoung info.rti_info[RTAX_NETMASK] = rt_mask(rt);
341 1.91 dyoung if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
342 1.91 dyoung ;
343 1.91 dyoung else if ((ifp = rt->rt_ifp) != NULL) {
344 1.91 dyoung const struct ifaddr *rtifa;
345 1.114 dyoung info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
346 1.91 dyoung /* rtifa used to be simply rt->rt_ifa.
347 1.91 dyoung * If rt->rt_ifa != NULL, then
348 1.91 dyoung * rt_get_ifa() != NULL. So this
349 1.91 dyoung * ought to still be safe. --dyoung
350 1.91 dyoung */
351 1.91 dyoung rtifa = rt_get_ifa(rt);
352 1.114 dyoung info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
353 1.91 dyoung #ifdef RTSOCK_DEBUG
354 1.115 christos if (info.rti_info[RTAX_IFA]->sa_family ==
355 1.115 christos AF_INET) {
356 1.91 dyoung printf("%s: copying out RTAX_IFA %s ",
357 1.115 christos __func__, inet_ntoa(
358 1.126 tsutsui ((const struct sockaddr_in *)
359 1.126 tsutsui info.rti_info[RTAX_IFA])->sin_addr)
360 1.126 tsutsui );
361 1.115 christos printf("for info.rti_info[RTAX_DST] %s "
362 1.115 christos "ifa_getifa %p ifa_seqno %p\n",
363 1.115 christos inet_ntoa(
364 1.126 tsutsui ((const struct sockaddr_in *)
365 1.115 christos info.rti_info[RTAX_DST])->sin_addr),
366 1.115 christos (void *)rtifa->ifa_getifa,
367 1.115 christos rtifa->ifa_seqno);
368 1.37 itojun }
369 1.91 dyoung #endif /* RTSOCK_DEBUG */
370 1.115 christos if (ifp->if_flags & IFF_POINTOPOINT) {
371 1.115 christos info.rti_info[RTAX_BRD] =
372 1.115 christos rtifa->ifa_dstaddr;
373 1.115 christos } else
374 1.114 dyoung info.rti_info[RTAX_BRD] = NULL;
375 1.91 dyoung rtm->rtm_index = ifp->if_index;
376 1.91 dyoung } else {
377 1.114 dyoung info.rti_info[RTAX_IFP] = NULL;
378 1.114 dyoung info.rti_info[RTAX_IFA] = NULL;
379 1.1 cgd }
380 1.95 dyoung (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
381 1.1 cgd if (len > rtm->rtm_msglen) {
382 1.117 christos old_rtm = rtm;
383 1.117 christos R_Malloc(rtm, struct rt_msghdr *, len);
384 1.117 christos if (rtm == NULL)
385 1.1 cgd senderr(ENOBUFS);
386 1.117 christos (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
387 1.1 cgd }
388 1.111 christos (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
389 1.1 cgd rtm->rtm_flags = rt->rt_flags;
390 1.120 christos cvtmetrics(&rtm->rtm_rmx, &rt->rt_rmx);
391 1.10 mycroft rtm->rtm_addrs = info.rti_addrs;
392 1.1 cgd break;
393 1.1 cgd
394 1.1 cgd case RTM_CHANGE:
395 1.45 itojun /*
396 1.45 itojun * new gateway could require new ifaddr, ifp;
397 1.45 itojun * flags may also be different; ifp may be specified
398 1.45 itojun * by ll sockaddr when protocol address is ambiguous
399 1.45 itojun */
400 1.45 itojun if ((error = rt_getifa(&info)) != 0)
401 1.45 itojun senderr(error);
402 1.115 christos if (info.rti_info[RTAX_GATEWAY] &&
403 1.115 christos rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
404 1.1 cgd senderr(EDQUOT);
405 1.35 itojun /* new gateway could require new ifaddr, ifp;
406 1.35 itojun flags may also be different; ifp may be specified
407 1.35 itojun by ll sockaddr when protocol address is ambiguous */
408 1.115 christos if (info.rti_info[RTAX_IFP] &&
409 1.115 christos (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
410 1.115 christos (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
411 1.115 christos info.rti_info[RTAX_GATEWAY])) {
412 1.115 christos ifa = ifaof_ifpforaddr(info.rti_info[RTAX_IFA] ?
413 1.115 christos info.rti_info[RTAX_IFA] :
414 1.115 christos info.rti_info[RTAX_GATEWAY], ifp);
415 1.115 christos } else if ((info.rti_info[RTAX_IFA] &&
416 1.115 christos (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
417 1.115 christos (info.rti_info[RTAX_GATEWAY] &&
418 1.115 christos (ifa = ifa_ifwithroute(rt->rt_flags,
419 1.115 christos rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
420 1.35 itojun ifp = ifa->ifa_ifp;
421 1.115 christos }
422 1.35 itojun if (ifa) {
423 1.124 roy struct ifaddr *oifa = rt->rt_ifa;
424 1.35 itojun if (oifa != ifa) {
425 1.90 dyoung if (oifa && oifa->ifa_rtrequest) {
426 1.90 dyoung oifa->ifa_rtrequest(RTM_DELETE,
427 1.90 dyoung rt, &info);
428 1.90 dyoung }
429 1.90 dyoung rt_replace_ifa(rt, ifa);
430 1.90 dyoung rt->rt_ifp = ifp;
431 1.35 itojun }
432 1.35 itojun }
433 1.1 cgd rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
434 1.21 christos &rt->rt_rmx);
435 1.35 itojun if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
436 1.45 itojun rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
437 1.115 christos /*FALLTHROUGH*/
438 1.1 cgd case RTM_LOCK:
439 1.10 mycroft rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
440 1.1 cgd rt->rt_rmx.rmx_locks |=
441 1.21 christos (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
442 1.1 cgd break;
443 1.1 cgd }
444 1.10 mycroft break;
445 1.1 cgd
446 1.1 cgd default:
447 1.1 cgd senderr(EOPNOTSUPP);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd flush:
451 1.1 cgd if (rtm) {
452 1.1 cgd if (error)
453 1.1 cgd rtm->rtm_errno = error;
454 1.75 perry else
455 1.1 cgd rtm->rtm_flags |= RTF_DONE;
456 1.1 cgd }
457 1.115 christos family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
458 1.115 christos 0;
459 1.117 christos /* We cannot free old_rtm until we have stopped using the
460 1.117 christos * pointers in info, some of which may point to sockaddrs
461 1.117 christos * in old_rtm.
462 1.117 christos */
463 1.117 christos if (old_rtm != NULL)
464 1.117 christos Free(old_rtm);
465 1.1 cgd if (rt)
466 1.1 cgd rtfree(rt);
467 1.1 cgd {
468 1.95 dyoung struct rawcb *rp = NULL;
469 1.1 cgd /*
470 1.1 cgd * Check to see if we don't want our own messages.
471 1.1 cgd */
472 1.1 cgd if ((so->so_options & SO_USELOOPBACK) == 0) {
473 1.1 cgd if (route_cb.any_count <= 1) {
474 1.1 cgd if (rtm)
475 1.1 cgd Free(rtm);
476 1.1 cgd m_freem(m);
477 1.95 dyoung return error;
478 1.1 cgd }
479 1.1 cgd /* There is another listener, so construct message */
480 1.1 cgd rp = sotorawcb(so);
481 1.1 cgd }
482 1.1 cgd if (rtm) {
483 1.112 dyoung m_copyback(m, 0, rtm->rtm_msglen, rtm);
484 1.47 itojun if (m->m_pkthdr.len < rtm->rtm_msglen) {
485 1.46 itojun m_freem(m);
486 1.46 itojun m = NULL;
487 1.47 itojun } else if (m->m_pkthdr.len > rtm->rtm_msglen)
488 1.46 itojun m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
489 1.1 cgd Free(rtm);
490 1.1 cgd }
491 1.1 cgd if (rp)
492 1.1 cgd rp->rcb_proto.sp_family = 0; /* Avoid us */
493 1.55 christos if (family)
494 1.99 ad proto.sp_protocol = family;
495 1.46 itojun if (m)
496 1.99 ad raw_input(m, &proto, &route_src, &route_dst);
497 1.1 cgd if (rp)
498 1.1 cgd rp->rcb_proto.sp_family = PF_ROUTE;
499 1.1 cgd }
500 1.95 dyoung return error;
501 1.1 cgd }
502 1.1 cgd
503 1.9 mycroft void
504 1.120 christos rt_setmetrics(u_long which, const struct rt_metrics *in, struct nrt_metrics *out)
505 1.1 cgd {
506 1.1 cgd #define metric(f, e) if (which & (f)) out->e = in->e;
507 1.1 cgd metric(RTV_RPIPE, rmx_recvpipe);
508 1.1 cgd metric(RTV_SPIPE, rmx_sendpipe);
509 1.1 cgd metric(RTV_SSTHRESH, rmx_ssthresh);
510 1.1 cgd metric(RTV_RTT, rmx_rtt);
511 1.1 cgd metric(RTV_RTTVAR, rmx_rttvar);
512 1.1 cgd metric(RTV_HOPCOUNT, rmx_hopcount);
513 1.1 cgd metric(RTV_MTU, rmx_mtu);
514 1.122 christos /* XXX time_t: Will not work after February 2145 (u_long time) */
515 1.1 cgd metric(RTV_EXPIRE, rmx_expire);
516 1.1 cgd #undef metric
517 1.1 cgd }
518 1.1 cgd
519 1.42 erh static int
520 1.115 christos rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
521 1.115 christos struct rt_addrinfo *rtinfo)
522 1.10 mycroft {
523 1.69 matt const struct sockaddr *sa = NULL; /* Quell compiler warning */
524 1.39 augustss int i;
525 1.10 mycroft
526 1.112 dyoung for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
527 1.10 mycroft if ((rtinfo->rti_addrs & (1 << i)) == 0)
528 1.10 mycroft continue;
529 1.117 christos rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
530 1.125 christos RT_ADVANCE(cp, sa);
531 1.10 mycroft }
532 1.44 enami
533 1.115 christos /*
534 1.115 christos * Check for extra addresses specified, except RTM_GET asking
535 1.115 christos * for interface info.
536 1.115 christos */
537 1.72 christos if (rtmtype == RTM_GET) {
538 1.115 christos if (((rtinfo->rti_addrs &
539 1.115 christos (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
540 1.95 dyoung return 1;
541 1.114 dyoung } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
542 1.114 dyoung return 1;
543 1.44 enami /* Check for bad data length. */
544 1.44 enami if (cp != cplim) {
545 1.112 dyoung if (i == RTAX_NETMASK + 1 && sa != NULL &&
546 1.125 christos cp - RT_ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
547 1.44 enami /*
548 1.114 dyoung * The last sockaddr was info.rti_info[RTAX_NETMASK].
549 1.44 enami * We accept this for now for the sake of old
550 1.44 enami * binaries or third party softwares.
551 1.44 enami */
552 1.44 enami ;
553 1.44 enami else
554 1.95 dyoung return 1;
555 1.44 enami }
556 1.95 dyoung return 0;
557 1.1 cgd }
558 1.1 cgd
559 1.120 christos struct mbuf *
560 1.93 christos rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
561 1.1 cgd {
562 1.39 augustss struct rt_msghdr *rtm;
563 1.39 augustss struct mbuf *m;
564 1.39 augustss int i;
565 1.68 matt const struct sockaddr *sa;
566 1.47 itojun int len, dlen;
567 1.1 cgd
568 1.47 itojun m = m_gethdr(M_DONTWAIT, MT_DATA);
569 1.95 dyoung if (m == NULL)
570 1.95 dyoung return m;
571 1.58 matt MCLAIM(m, &routedomain.dom_mowner);
572 1.10 mycroft switch (type) {
573 1.47 itojun
574 1.10 mycroft case RTM_DELADDR:
575 1.10 mycroft case RTM_NEWADDR:
576 1.47 itojun len = sizeof(struct ifa_msghdr);
577 1.10 mycroft break;
578 1.10 mycroft
579 1.32 bouyer #ifdef COMPAT_14
580 1.120 christos case RTM_OOIFINFO:
581 1.120 christos len = sizeof(struct if_msghdr14);
582 1.120 christos break;
583 1.120 christos #endif
584 1.120 christos #ifdef COMPAT_50
585 1.32 bouyer case RTM_OIFINFO:
586 1.120 christos len = sizeof(struct if_msghdr50);
587 1.32 bouyer break;
588 1.32 bouyer #endif
589 1.32 bouyer
590 1.10 mycroft case RTM_IFINFO:
591 1.47 itojun len = sizeof(struct if_msghdr);
592 1.10 mycroft break;
593 1.10 mycroft
594 1.36 thorpej case RTM_IFANNOUNCE:
595 1.78 dyoung case RTM_IEEE80211:
596 1.47 itojun len = sizeof(struct if_announcemsghdr);
597 1.36 thorpej break;
598 1.36 thorpej
599 1.10 mycroft default:
600 1.47 itojun len = sizeof(struct rt_msghdr);
601 1.46 itojun }
602 1.47 itojun if (len > MHLEN + MLEN)
603 1.34 itojun panic("rt_msg1: message too long");
604 1.47 itojun else if (len > MHLEN) {
605 1.32 bouyer m->m_next = m_get(M_DONTWAIT, MT_DATA);
606 1.47 itojun if (m->m_next == NULL) {
607 1.32 bouyer m_freem(m);
608 1.95 dyoung return NULL;
609 1.32 bouyer }
610 1.58 matt MCLAIM(m->m_next, m->m_owner);
611 1.47 itojun m->m_pkthdr.len = len;
612 1.47 itojun m->m_len = MHLEN;
613 1.47 itojun m->m_next->m_len = len - MHLEN;
614 1.47 itojun } else {
615 1.47 itojun m->m_pkthdr.len = m->m_len = len;
616 1.32 bouyer }
617 1.95 dyoung m->m_pkthdr.rcvif = NULL;
618 1.32 bouyer m_copyback(m, 0, datalen, data);
619 1.107 christos if (len > datalen)
620 1.107 christos (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
621 1.1 cgd rtm = mtod(m, struct rt_msghdr *);
622 1.10 mycroft for (i = 0; i < RTAX_MAX; i++) {
623 1.10 mycroft if ((sa = rtinfo->rti_info[i]) == NULL)
624 1.10 mycroft continue;
625 1.10 mycroft rtinfo->rti_addrs |= (1 << i);
626 1.125 christos dlen = RT_ROUNDUP(sa->sa_len);
627 1.76 christos m_copyback(m, len, dlen, sa);
628 1.10 mycroft len += dlen;
629 1.47 itojun }
630 1.47 itojun if (m->m_pkthdr.len != len) {
631 1.47 itojun m_freem(m);
632 1.95 dyoung return NULL;
633 1.10 mycroft }
634 1.1 cgd rtm->rtm_msglen = len;
635 1.1 cgd rtm->rtm_version = RTM_VERSION;
636 1.1 cgd rtm->rtm_type = type;
637 1.95 dyoung return m;
638 1.10 mycroft }
639 1.10 mycroft
640 1.29 chopps /*
641 1.29 chopps * rt_msg2
642 1.29 chopps *
643 1.29 chopps * fills 'cp' or 'w'.w_tmem with the routing socket message and
644 1.29 chopps * returns the length of the message in 'lenp'.
645 1.29 chopps *
646 1.29 chopps * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
647 1.29 chopps * the message
648 1.29 chopps * otherwise walkarg's w_needed is updated and if the user buffer is
649 1.29 chopps * specified and w_needed indicates space exists the information is copied
650 1.29 chopps * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
651 1.29 chopps * if the allocation fails ENOBUFS is returned.
652 1.29 chopps */
653 1.10 mycroft static int
654 1.120 christos rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
655 1.69 matt int *lenp)
656 1.10 mycroft {
657 1.39 augustss int i;
658 1.10 mycroft int len, dlen, second_time = 0;
659 1.93 christos char *cp0, *cp = cpv;
660 1.10 mycroft
661 1.10 mycroft rtinfo->rti_addrs = 0;
662 1.10 mycroft again:
663 1.10 mycroft switch (type) {
664 1.10 mycroft
665 1.10 mycroft case RTM_DELADDR:
666 1.10 mycroft case RTM_NEWADDR:
667 1.10 mycroft len = sizeof(struct ifa_msghdr);
668 1.10 mycroft break;
669 1.32 bouyer #ifdef COMPAT_14
670 1.120 christos case RTM_OOIFINFO:
671 1.120 christos len = sizeof(struct if_msghdr14);
672 1.120 christos break;
673 1.120 christos #endif
674 1.120 christos #ifdef COMPAT_50
675 1.32 bouyer case RTM_OIFINFO:
676 1.120 christos len = sizeof(struct if_msghdr50);
677 1.32 bouyer break;
678 1.32 bouyer #endif
679 1.10 mycroft
680 1.10 mycroft case RTM_IFINFO:
681 1.10 mycroft len = sizeof(struct if_msghdr);
682 1.10 mycroft break;
683 1.10 mycroft
684 1.10 mycroft default:
685 1.10 mycroft len = sizeof(struct rt_msghdr);
686 1.10 mycroft }
687 1.17 christos if ((cp0 = cp) != NULL)
688 1.10 mycroft cp += len;
689 1.10 mycroft for (i = 0; i < RTAX_MAX; i++) {
690 1.68 matt const struct sockaddr *sa;
691 1.10 mycroft
692 1.95 dyoung if ((sa = rtinfo->rti_info[i]) == NULL)
693 1.10 mycroft continue;
694 1.10 mycroft rtinfo->rti_addrs |= (1 << i);
695 1.125 christos dlen = RT_ROUNDUP(sa->sa_len);
696 1.10 mycroft if (cp) {
697 1.111 christos (void)memcpy(cp, sa, (size_t)dlen);
698 1.10 mycroft cp += dlen;
699 1.10 mycroft }
700 1.1 cgd len += dlen;
701 1.1 cgd }
702 1.95 dyoung if (cp == NULL && w != NULL && !second_time) {
703 1.120 christos struct rt_walkarg *rw = w;
704 1.10 mycroft
705 1.10 mycroft rw->w_needed += len;
706 1.10 mycroft if (rw->w_needed <= 0 && rw->w_where) {
707 1.10 mycroft if (rw->w_tmemsize < len) {
708 1.10 mycroft if (rw->w_tmem)
709 1.10 mycroft free(rw->w_tmem, M_RTABLE);
710 1.111 christos rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
711 1.17 christos if (rw->w_tmem)
712 1.10 mycroft rw->w_tmemsize = len;
713 1.111 christos else
714 1.111 christos rw->w_tmemsize = 0;
715 1.10 mycroft }
716 1.10 mycroft if (rw->w_tmem) {
717 1.10 mycroft cp = rw->w_tmem;
718 1.10 mycroft second_time = 1;
719 1.10 mycroft goto again;
720 1.29 chopps } else {
721 1.29 chopps rw->w_tmemneeded = len;
722 1.95 dyoung return ENOBUFS;
723 1.29 chopps }
724 1.10 mycroft }
725 1.1 cgd }
726 1.10 mycroft if (cp) {
727 1.39 augustss struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
728 1.10 mycroft
729 1.10 mycroft rtm->rtm_version = RTM_VERSION;
730 1.10 mycroft rtm->rtm_type = type;
731 1.10 mycroft rtm->rtm_msglen = len;
732 1.1 cgd }
733 1.29 chopps if (lenp)
734 1.29 chopps *lenp = len;
735 1.95 dyoung return 0;
736 1.10 mycroft }
737 1.10 mycroft
738 1.10 mycroft /*
739 1.10 mycroft * This routine is called to generate a message from the routing
740 1.51 wiz * socket indicating that a redirect has occurred, a routing lookup
741 1.10 mycroft * has failed, or that a protocol has detected timeouts to a particular
742 1.10 mycroft * destination.
743 1.10 mycroft */
744 1.10 mycroft void
745 1.69 matt rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
746 1.10 mycroft {
747 1.32 bouyer struct rt_msghdr rtm;
748 1.39 augustss struct mbuf *m;
749 1.68 matt const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
750 1.10 mycroft
751 1.10 mycroft if (route_cb.any_count == 0)
752 1.10 mycroft return;
753 1.48 thorpej memset(&rtm, 0, sizeof(rtm));
754 1.32 bouyer rtm.rtm_flags = RTF_DONE | flags;
755 1.32 bouyer rtm.rtm_errno = error;
756 1.110 dyoung m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
757 1.95 dyoung if (m == NULL)
758 1.1 cgd return;
759 1.32 bouyer mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
760 1.99 ad route_enqueue(m, sa ? sa->sa_family : 0);
761 1.10 mycroft }
762 1.10 mycroft
763 1.10 mycroft /*
764 1.10 mycroft * This routine is called to generate a message from the routing
765 1.10 mycroft * socket indicating that the status of a network interface has changed.
766 1.10 mycroft */
767 1.10 mycroft void
768 1.69 matt rt_ifmsg(struct ifnet *ifp)
769 1.10 mycroft {
770 1.32 bouyer struct if_msghdr ifm;
771 1.10 mycroft struct mbuf *m;
772 1.10 mycroft struct rt_addrinfo info;
773 1.10 mycroft
774 1.10 mycroft if (route_cb.any_count == 0)
775 1.10 mycroft return;
776 1.120 christos (void)memset(&info, 0, sizeof(info));
777 1.120 christos (void)memset(&ifm, 0, sizeof(ifm));
778 1.32 bouyer ifm.ifm_index = ifp->if_index;
779 1.32 bouyer ifm.ifm_flags = ifp->if_flags;
780 1.32 bouyer ifm.ifm_data = ifp->if_data;
781 1.32 bouyer ifm.ifm_addrs = 0;
782 1.110 dyoung m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
783 1.95 dyoung if (m == NULL)
784 1.32 bouyer return;
785 1.99 ad route_enqueue(m, 0);
786 1.32 bouyer #ifdef COMPAT_14
787 1.120 christos compat_14_rt_ifmsg(ifp, &ifm);
788 1.120 christos #endif
789 1.120 christos #ifdef COMPAT_50
790 1.120 christos compat_50_rt_ifmsg(ifp, &ifm);
791 1.32 bouyer #endif
792 1.1 cgd }
793 1.1 cgd
794 1.120 christos
795 1.1 cgd /*
796 1.10 mycroft * This is called to generate messages from the routing socket
797 1.10 mycroft * indicating a network interface has had addresses associated with it.
798 1.10 mycroft * if we ever reverse the logic and replace messages TO the routing
799 1.10 mycroft * socket indicate a request to configure interfaces, then it will
800 1.10 mycroft * be unnecessary as the routing socket will automatically generate
801 1.10 mycroft * copies of it.
802 1.10 mycroft */
803 1.10 mycroft void
804 1.69 matt rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
805 1.10 mycroft {
806 1.116 dyoung #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
807 1.10 mycroft struct rt_addrinfo info;
808 1.116 dyoung const struct sockaddr *sa;
809 1.10 mycroft int pass;
810 1.116 dyoung struct mbuf *m;
811 1.10 mycroft struct ifnet *ifp = ifa->ifa_ifp;
812 1.116 dyoung struct rt_msghdr rtm;
813 1.116 dyoung struct ifa_msghdr ifam;
814 1.116 dyoung int ncmd;
815 1.10 mycroft
816 1.10 mycroft if (route_cb.any_count == 0)
817 1.10 mycroft return;
818 1.10 mycroft for (pass = 1; pass < 3; pass++) {
819 1.48 thorpej memset(&info, 0, sizeof(info));
820 1.116 dyoung switch (cmdpass(cmd, pass)) {
821 1.116 dyoung case cmdpass(RTM_ADD, 1):
822 1.116 dyoung case cmdpass(RTM_CHANGE, 1):
823 1.116 dyoung case cmdpass(RTM_DELETE, 2):
824 1.116 dyoung if (cmd == RTM_ADD)
825 1.116 dyoung ncmd = RTM_NEWADDR;
826 1.116 dyoung else
827 1.116 dyoung ncmd = RTM_DELADDR;
828 1.10 mycroft
829 1.114 dyoung info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
830 1.114 dyoung info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
831 1.114 dyoung info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
832 1.114 dyoung info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
833 1.48 thorpej memset(&ifam, 0, sizeof(ifam));
834 1.32 bouyer ifam.ifam_index = ifp->if_index;
835 1.32 bouyer ifam.ifam_metric = ifa->ifa_metric;
836 1.32 bouyer ifam.ifam_flags = ifa->ifa_flags;
837 1.110 dyoung m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
838 1.32 bouyer if (m == NULL)
839 1.10 mycroft continue;
840 1.32 bouyer mtod(m, struct ifa_msghdr *)->ifam_addrs =
841 1.32 bouyer info.rti_addrs;
842 1.116 dyoung break;
843 1.116 dyoung case cmdpass(RTM_ADD, 2):
844 1.116 dyoung case cmdpass(RTM_CHANGE, 2):
845 1.116 dyoung case cmdpass(RTM_DELETE, 1):
846 1.95 dyoung if (rt == NULL)
847 1.10 mycroft continue;
848 1.114 dyoung info.rti_info[RTAX_NETMASK] = rt_mask(rt);
849 1.114 dyoung info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
850 1.114 dyoung info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
851 1.48 thorpej memset(&rtm, 0, sizeof(rtm));
852 1.32 bouyer rtm.rtm_index = ifp->if_index;
853 1.32 bouyer rtm.rtm_flags |= rt->rt_flags;
854 1.32 bouyer rtm.rtm_errno = error;
855 1.110 dyoung m = rt_msg1(cmd, &info, &rtm, sizeof(rtm));
856 1.32 bouyer if (m == NULL)
857 1.10 mycroft continue;
858 1.32 bouyer mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
859 1.116 dyoung break;
860 1.116 dyoung default:
861 1.116 dyoung continue;
862 1.10 mycroft }
863 1.104 christos #ifdef DIAGNOSTIC
864 1.104 christos if (m == NULL)
865 1.105 dholland panic("%s: called with wrong command", __func__);
866 1.104 christos #endif
867 1.99 ad route_enqueue(m, sa ? sa->sa_family : 0);
868 1.10 mycroft }
869 1.116 dyoung #undef cmdpass
870 1.36 thorpej }
871 1.36 thorpej
872 1.78 dyoung static struct mbuf *
873 1.78 dyoung rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
874 1.78 dyoung struct rt_addrinfo *info)
875 1.78 dyoung {
876 1.78 dyoung struct if_announcemsghdr ifan;
877 1.78 dyoung
878 1.78 dyoung memset(info, 0, sizeof(*info));
879 1.78 dyoung memset(&ifan, 0, sizeof(ifan));
880 1.78 dyoung ifan.ifan_index = ifp->if_index;
881 1.78 dyoung strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
882 1.78 dyoung ifan.ifan_what = what;
883 1.111 christos return rt_msg1(type, info, &ifan, sizeof(ifan));
884 1.78 dyoung }
885 1.78 dyoung
886 1.36 thorpej /*
887 1.36 thorpej * This is called to generate routing socket messages indicating
888 1.36 thorpej * network interface arrival and departure.
889 1.36 thorpej */
890 1.36 thorpej void
891 1.69 matt rt_ifannouncemsg(struct ifnet *ifp, int what)
892 1.36 thorpej {
893 1.36 thorpej struct mbuf *m;
894 1.36 thorpej struct rt_addrinfo info;
895 1.36 thorpej
896 1.36 thorpej if (route_cb.any_count == 0)
897 1.36 thorpej return;
898 1.78 dyoung m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
899 1.78 dyoung if (m == NULL)
900 1.78 dyoung return;
901 1.99 ad route_enqueue(m, 0);
902 1.78 dyoung }
903 1.78 dyoung
904 1.78 dyoung /*
905 1.78 dyoung * This is called to generate routing socket messages indicating
906 1.78 dyoung * IEEE80211 wireless events.
907 1.78 dyoung * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
908 1.78 dyoung */
909 1.78 dyoung void
910 1.78 dyoung rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
911 1.78 dyoung {
912 1.78 dyoung struct mbuf *m;
913 1.78 dyoung struct rt_addrinfo info;
914 1.78 dyoung
915 1.78 dyoung if (route_cb.any_count == 0)
916 1.78 dyoung return;
917 1.78 dyoung m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
918 1.78 dyoung if (m == NULL)
919 1.36 thorpej return;
920 1.78 dyoung /*
921 1.78 dyoung * Append the ieee80211 data. Try to stick it in the
922 1.78 dyoung * mbuf containing the ifannounce msg; otherwise allocate
923 1.78 dyoung * a new mbuf and append.
924 1.78 dyoung *
925 1.78 dyoung * NB: we assume m is a single mbuf.
926 1.78 dyoung */
927 1.78 dyoung if (data_len > M_TRAILINGSPACE(m)) {
928 1.78 dyoung struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
929 1.78 dyoung if (n == NULL) {
930 1.78 dyoung m_freem(m);
931 1.78 dyoung return;
932 1.78 dyoung }
933 1.78 dyoung (void)memcpy(mtod(n, void *), data, data_len);
934 1.78 dyoung n->m_len = data_len;
935 1.78 dyoung m->m_next = n;
936 1.78 dyoung } else if (data_len > 0) {
937 1.98 matt (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
938 1.78 dyoung m->m_len += data_len;
939 1.78 dyoung }
940 1.78 dyoung if (m->m_flags & M_PKTHDR)
941 1.78 dyoung m->m_pkthdr.len += data_len;
942 1.78 dyoung mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
943 1.99 ad route_enqueue(m, 0);
944 1.10 mycroft }
945 1.10 mycroft
946 1.10 mycroft /*
947 1.10 mycroft * This is used in dumping the kernel table via sysctl().
948 1.1 cgd */
949 1.40 simonb static int
950 1.94 dyoung sysctl_dumpentry(struct rtentry *rt, void *v)
951 1.1 cgd {
952 1.120 christos struct rt_walkarg *w = v;
953 1.10 mycroft int error = 0, size;
954 1.10 mycroft struct rt_addrinfo info;
955 1.1 cgd
956 1.10 mycroft if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
957 1.10 mycroft return 0;
958 1.48 thorpej memset(&info, 0, sizeof(info));
959 1.114 dyoung info.rti_info[RTAX_DST] = rt_getkey(rt);
960 1.114 dyoung info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
961 1.114 dyoung info.rti_info[RTAX_NETMASK] = rt_mask(rt);
962 1.16 cgd if (rt->rt_ifp) {
963 1.91 dyoung const struct ifaddr *rtifa;
964 1.114 dyoung info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
965 1.91 dyoung /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
966 1.91 dyoung * then rt_get_ifa() != NULL. So this ought to still be safe.
967 1.91 dyoung * --dyoung
968 1.91 dyoung */
969 1.91 dyoung rtifa = rt_get_ifa(rt);
970 1.114 dyoung info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
971 1.16 cgd if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
972 1.114 dyoung info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
973 1.16 cgd }
974 1.29 chopps if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
975 1.95 dyoung return error;
976 1.29 chopps if (w->w_where && w->w_tmem && w->w_needed <= 0) {
977 1.39 augustss struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
978 1.10 mycroft
979 1.10 mycroft rtm->rtm_flags = rt->rt_flags;
980 1.10 mycroft rtm->rtm_use = rt->rt_use;
981 1.120 christos cvtmetrics(&rtm->rtm_rmx, &rt->rt_rmx);
982 1.83 christos KASSERT(rt->rt_ifp != NULL);
983 1.10 mycroft rtm->rtm_index = rt->rt_ifp->if_index;
984 1.10 mycroft rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
985 1.10 mycroft rtm->rtm_addrs = info.rti_addrs;
986 1.21 christos if ((error = copyout(rtm, w->w_where, size)) != 0)
987 1.10 mycroft w->w_where = NULL;
988 1.10 mycroft else
989 1.93 christos w->w_where = (char *)w->w_where + size;
990 1.10 mycroft }
991 1.95 dyoung return error;
992 1.10 mycroft }
993 1.1 cgd
994 1.40 simonb static int
995 1.120 christos sysctl_iflist(int af, struct rt_walkarg *w, int type)
996 1.10 mycroft {
997 1.39 augustss struct ifnet *ifp;
998 1.39 augustss struct ifaddr *ifa;
999 1.10 mycroft struct rt_addrinfo info;
1000 1.10 mycroft int len, error = 0;
1001 1.10 mycroft
1002 1.48 thorpej memset(&info, 0, sizeof(info));
1003 1.74 matt IFNET_FOREACH(ifp) {
1004 1.10 mycroft if (w->w_arg && w->w_arg != ifp->if_index)
1005 1.10 mycroft continue;
1006 1.97 dyoung if (IFADDR_EMPTY(ifp))
1007 1.81 rpaulo continue;
1008 1.114 dyoung info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1009 1.59 itojun switch (type) {
1010 1.32 bouyer case NET_RT_IFLIST:
1011 1.111 christos error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1012 1.32 bouyer break;
1013 1.32 bouyer #ifdef COMPAT_14
1014 1.120 christos case NET_RT_OOIFLIST:
1015 1.120 christos error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1016 1.120 christos break;
1017 1.120 christos #endif
1018 1.120 christos #ifdef COMPAT_50
1019 1.32 bouyer case NET_RT_OIFLIST:
1020 1.111 christos error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1021 1.32 bouyer break;
1022 1.32 bouyer #endif
1023 1.32 bouyer default:
1024 1.32 bouyer panic("sysctl_iflist(1)");
1025 1.32 bouyer }
1026 1.32 bouyer if (error)
1027 1.95 dyoung return error;
1028 1.114 dyoung info.rti_info[RTAX_IFP] = NULL;
1029 1.29 chopps if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1030 1.59 itojun switch (type) {
1031 1.32 bouyer case NET_RT_IFLIST: {
1032 1.39 augustss struct if_msghdr *ifm;
1033 1.32 bouyer
1034 1.32 bouyer ifm = (struct if_msghdr *)w->w_tmem;
1035 1.32 bouyer ifm->ifm_index = ifp->if_index;
1036 1.32 bouyer ifm->ifm_flags = ifp->if_flags;
1037 1.32 bouyer ifm->ifm_data = ifp->if_data;
1038 1.32 bouyer ifm->ifm_addrs = info.rti_addrs;
1039 1.32 bouyer error = copyout(ifm, w->w_where, len);
1040 1.32 bouyer if (error)
1041 1.95 dyoung return error;
1042 1.93 christos w->w_where = (char *)w->w_where + len;
1043 1.32 bouyer break;
1044 1.32 bouyer }
1045 1.10 mycroft
1046 1.32 bouyer #ifdef COMPAT_14
1047 1.120 christos case NET_RT_OOIFLIST:
1048 1.120 christos error = compat_14_iflist(ifp, w, &info, len);
1049 1.120 christos if (error)
1050 1.120 christos return error;
1051 1.120 christos break;
1052 1.120 christos #endif
1053 1.120 christos #ifdef COMPAT_50
1054 1.120 christos case NET_RT_OIFLIST:
1055 1.120 christos error = compat_50_iflist(ifp, w, &info, len);
1056 1.32 bouyer if (error)
1057 1.95 dyoung return error;
1058 1.32 bouyer break;
1059 1.32 bouyer #endif
1060 1.32 bouyer default:
1061 1.32 bouyer panic("sysctl_iflist(2)");
1062 1.32 bouyer }
1063 1.10 mycroft }
1064 1.97 dyoung IFADDR_FOREACH(ifa, ifp) {
1065 1.10 mycroft if (af && af != ifa->ifa_addr->sa_family)
1066 1.10 mycroft continue;
1067 1.114 dyoung info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1068 1.114 dyoung info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1069 1.114 dyoung info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1070 1.29 chopps if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1071 1.95 dyoung return error;
1072 1.29 chopps if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1073 1.39 augustss struct ifa_msghdr *ifam;
1074 1.10 mycroft
1075 1.10 mycroft ifam = (struct ifa_msghdr *)w->w_tmem;
1076 1.10 mycroft ifam->ifam_index = ifa->ifa_ifp->if_index;
1077 1.10 mycroft ifam->ifam_flags = ifa->ifa_flags;
1078 1.10 mycroft ifam->ifam_metric = ifa->ifa_metric;
1079 1.10 mycroft ifam->ifam_addrs = info.rti_addrs;
1080 1.17 christos error = copyout(w->w_tmem, w->w_where, len);
1081 1.17 christos if (error)
1082 1.95 dyoung return error;
1083 1.93 christos w->w_where = (char *)w->w_where + len;
1084 1.10 mycroft }
1085 1.10 mycroft }
1086 1.115 christos info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1087 1.115 christos info.rti_info[RTAX_BRD] = NULL;
1088 1.10 mycroft }
1089 1.95 dyoung return 0;
1090 1.1 cgd }
1091 1.1 cgd
1092 1.40 simonb static int
1093 1.65 atatat sysctl_rtable(SYSCTLFN_ARGS)
1094 1.1 cgd {
1095 1.65 atatat void *where = oldp;
1096 1.65 atatat size_t *given = oldlenp;
1097 1.65 atatat const void *new = newp;
1098 1.10 mycroft int i, s, error = EINVAL;
1099 1.10 mycroft u_char af;
1100 1.120 christos struct rt_walkarg w;
1101 1.1 cgd
1102 1.66 atatat if (namelen == 1 && name[0] == CTL_QUERY)
1103 1.95 dyoung return sysctl_query(SYSCTLFN_CALL(rnode));
1104 1.66 atatat
1105 1.10 mycroft if (new)
1106 1.95 dyoung return EPERM;
1107 1.10 mycroft if (namelen != 3)
1108 1.95 dyoung return EINVAL;
1109 1.10 mycroft af = name[0];
1110 1.29 chopps w.w_tmemneeded = 0;
1111 1.29 chopps w.w_tmemsize = 0;
1112 1.29 chopps w.w_tmem = NULL;
1113 1.29 chopps again:
1114 1.29 chopps /* we may return here if a later [re]alloc of the t_mem buffer fails */
1115 1.29 chopps if (w.w_tmemneeded) {
1116 1.111 christos w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1117 1.29 chopps w.w_tmemsize = w.w_tmemneeded;
1118 1.29 chopps w.w_tmemneeded = 0;
1119 1.29 chopps }
1120 1.29 chopps w.w_op = name[1];
1121 1.29 chopps w.w_arg = name[2];
1122 1.10 mycroft w.w_given = *given;
1123 1.1 cgd w.w_needed = 0 - w.w_given;
1124 1.29 chopps w.w_where = where;
1125 1.1 cgd
1126 1.14 mycroft s = splsoftnet();
1127 1.10 mycroft switch (w.w_op) {
1128 1.10 mycroft
1129 1.10 mycroft case NET_RT_DUMP:
1130 1.10 mycroft case NET_RT_FLAGS:
1131 1.10 mycroft for (i = 1; i <= AF_MAX; i++)
1132 1.94 dyoung if ((af == 0 || af == i) &&
1133 1.94 dyoung (error = rt_walktree(i, sysctl_dumpentry, &w)))
1134 1.10 mycroft break;
1135 1.10 mycroft break;
1136 1.10 mycroft
1137 1.32 bouyer #ifdef COMPAT_14
1138 1.120 christos case NET_RT_OOIFLIST:
1139 1.120 christos error = sysctl_iflist(af, &w, w.w_op);
1140 1.120 christos break;
1141 1.120 christos #endif
1142 1.120 christos #ifdef COMPAT_50
1143 1.32 bouyer case NET_RT_OIFLIST:
1144 1.32 bouyer error = sysctl_iflist(af, &w, w.w_op);
1145 1.32 bouyer break;
1146 1.32 bouyer #endif
1147 1.32 bouyer
1148 1.10 mycroft case NET_RT_IFLIST:
1149 1.32 bouyer error = sysctl_iflist(af, &w, w.w_op);
1150 1.1 cgd }
1151 1.10 mycroft splx(s);
1152 1.29 chopps
1153 1.29 chopps /* check to see if we couldn't allocate memory with NOWAIT */
1154 1.29 chopps if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1155 1.29 chopps goto again;
1156 1.29 chopps
1157 1.10 mycroft if (w.w_tmem)
1158 1.10 mycroft free(w.w_tmem, M_RTABLE);
1159 1.1 cgd w.w_needed += w.w_given;
1160 1.10 mycroft if (where) {
1161 1.93 christos *given = (char *)w.w_where - (char *)where;
1162 1.10 mycroft if (*given < w.w_needed)
1163 1.95 dyoung return ENOMEM;
1164 1.10 mycroft } else {
1165 1.10 mycroft *given = (11 * w.w_needed) / 10;
1166 1.10 mycroft }
1167 1.95 dyoung return error;
1168 1.1 cgd }
1169 1.1 cgd
1170 1.1 cgd /*
1171 1.99 ad * Routing message software interrupt routine
1172 1.99 ad */
1173 1.99 ad static void
1174 1.99 ad route_intr(void *cookie)
1175 1.99 ad {
1176 1.99 ad struct sockproto proto = { .sp_family = PF_ROUTE, };
1177 1.99 ad struct mbuf *m;
1178 1.99 ad int s;
1179 1.99 ad
1180 1.101 ad mutex_enter(softnet_lock);
1181 1.101 ad KERNEL_LOCK(1, NULL);
1182 1.99 ad while (!IF_IS_EMPTY(&route_intrq)) {
1183 1.99 ad s = splnet();
1184 1.99 ad IF_DEQUEUE(&route_intrq, m);
1185 1.99 ad splx(s);
1186 1.99 ad if (m == NULL)
1187 1.99 ad break;
1188 1.100 yamt proto.sp_protocol = M_GETCTX(m, uintptr_t);
1189 1.99 ad raw_input(m, &proto, &route_src, &route_dst);
1190 1.99 ad }
1191 1.101 ad KERNEL_UNLOCK_ONE(NULL);
1192 1.101 ad mutex_exit(softnet_lock);
1193 1.99 ad }
1194 1.99 ad
1195 1.99 ad /*
1196 1.99 ad * Enqueue a message to the software interrupt routine.
1197 1.99 ad */
1198 1.120 christos void
1199 1.99 ad route_enqueue(struct mbuf *m, int family)
1200 1.99 ad {
1201 1.99 ad int s, wasempty;
1202 1.99 ad
1203 1.99 ad s = splnet();
1204 1.99 ad if (IF_QFULL(&route_intrq)) {
1205 1.99 ad IF_DROP(&route_intrq);
1206 1.99 ad m_freem(m);
1207 1.99 ad } else {
1208 1.99 ad wasempty = IF_IS_EMPTY(&route_intrq);
1209 1.99 ad M_SETCTX(m, (uintptr_t)family);
1210 1.99 ad IF_ENQUEUE(&route_intrq, m);
1211 1.99 ad if (wasempty)
1212 1.99 ad softint_schedule(route_sih);
1213 1.99 ad }
1214 1.99 ad splx(s);
1215 1.99 ad }
1216 1.99 ad
1217 1.99 ad void
1218 1.99 ad rt_init(void)
1219 1.99 ad {
1220 1.99 ad
1221 1.99 ad route_intrq.ifq_maxlen = route_maxqlen;
1222 1.101 ad route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1223 1.101 ad route_intr, NULL);
1224 1.99 ad }
1225 1.99 ad
1226 1.99 ad /*
1227 1.1 cgd * Definitions of protocols supported in the ROUTE domain.
1228 1.1 cgd */
1229 1.101 ad PR_WRAP_USRREQ(route_usrreq)
1230 1.101 ad #define route_usrreq route_usrreq_wrapper
1231 1.1 cgd
1232 1.70 matt const struct protosw routesw[] = {
1233 1.92 matt {
1234 1.92 matt .pr_type = SOCK_RAW,
1235 1.92 matt .pr_domain = &routedomain,
1236 1.92 matt .pr_flags = PR_ATOMIC|PR_ADDR,
1237 1.92 matt .pr_input = raw_input,
1238 1.92 matt .pr_output = route_output,
1239 1.92 matt .pr_ctlinput = raw_ctlinput,
1240 1.92 matt .pr_usrreq = route_usrreq,
1241 1.92 matt .pr_init = raw_init,
1242 1.92 matt },
1243 1.92 matt };
1244 1.69 matt
1245 1.69 matt struct domain routedomain = {
1246 1.87 christos .dom_family = PF_ROUTE,
1247 1.87 christos .dom_name = "route",
1248 1.87 christos .dom_init = route_init,
1249 1.87 christos .dom_protosw = routesw,
1250 1.95 dyoung .dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
1251 1.1 cgd };
1252 1.1 cgd
1253 1.65 atatat SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
1254 1.65 atatat {
1255 1.85 elad const struct sysctlnode *rnode = NULL;
1256 1.85 elad
1257 1.67 atatat sysctl_createv(clog, 0, NULL, NULL,
1258 1.67 atatat CTLFLAG_PERMANENT,
1259 1.65 atatat CTLTYPE_NODE, "net", NULL,
1260 1.65 atatat NULL, 0, NULL, 0,
1261 1.65 atatat CTL_NET, CTL_EOL);
1262 1.65 atatat
1263 1.85 elad sysctl_createv(clog, 0, NULL, &rnode,
1264 1.67 atatat CTLFLAG_PERMANENT,
1265 1.71 atatat CTLTYPE_NODE, "route",
1266 1.71 atatat SYSCTL_DESCR("PF_ROUTE information"),
1267 1.65 atatat NULL, 0, NULL, 0,
1268 1.65 atatat CTL_NET, PF_ROUTE, CTL_EOL);
1269 1.67 atatat sysctl_createv(clog, 0, NULL, NULL,
1270 1.67 atatat CTLFLAG_PERMANENT,
1271 1.71 atatat CTLTYPE_NODE, "rtable",
1272 1.71 atatat SYSCTL_DESCR("Routing table information"),
1273 1.65 atatat sysctl_rtable, 0, NULL, 0,
1274 1.65 atatat CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
1275 1.85 elad sysctl_createv(clog, 0, &rnode, NULL,
1276 1.85 elad CTLFLAG_PERMANENT,
1277 1.85 elad CTLTYPE_STRUCT, "stats",
1278 1.85 elad SYSCTL_DESCR("Routing statistics"),
1279 1.85 elad NULL, 0, &rtstat, sizeof(rtstat),
1280 1.85 elad CTL_CREATE, CTL_EOL);
1281 1.65 atatat }
1282