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