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