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