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