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