rtsock.c revision 1.10 1 1.1 cgd /*
2 1.10 mycroft * Copyright (c) 1988, 1991, 1993
3 1.10 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.10 mycroft * from: @(#)rtsock.c 8.3 (Berkeley) 1/4/94
34 1.10 mycroft * $Id: rtsock.c,v 1.10 1994/05/13 06:03:22 mycroft Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.5 mycroft #include <sys/param.h>
38 1.5 mycroft #include <sys/systm.h>
39 1.10 mycroft #include <sys/proc.h>
40 1.5 mycroft #include <sys/mbuf.h>
41 1.5 mycroft #include <sys/socket.h>
42 1.5 mycroft #include <sys/socketvar.h>
43 1.5 mycroft #include <sys/domain.h>
44 1.5 mycroft #include <sys/protosw.h>
45 1.5 mycroft
46 1.5 mycroft #include <net/if.h>
47 1.5 mycroft #include <net/route.h>
48 1.5 mycroft #include <net/raw_cb.h>
49 1.1 cgd
50 1.10 mycroft struct sockaddr route_dst = { 2, PF_ROUTE, };
51 1.10 mycroft struct sockaddr route_src = { 2, PF_ROUTE, };
52 1.10 mycroft struct sockproto route_proto = { PF_ROUTE, };
53 1.10 mycroft
54 1.10 mycroft struct walkarg {
55 1.10 mycroft int w_op, w_arg, w_given, w_needed, w_tmemsize;
56 1.10 mycroft caddr_t w_where, w_tmem;
57 1.10 mycroft };
58 1.1 cgd
59 1.10 mycroft static struct mbuf *
60 1.10 mycroft rt_msg1 __P((int, struct rt_addrinfo *));
61 1.10 mycroft static int rt_msg2 __P((int,
62 1.10 mycroft struct rt_addrinfo *, caddr_t, struct walkarg *));
63 1.10 mycroft static void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
64 1.10 mycroft
65 1.10 mycroft /* Sleazy use of local variables throughout file, warning!!!! */
66 1.10 mycroft #define dst info.rti_info[RTAX_DST]
67 1.10 mycroft #define gate info.rti_info[RTAX_GATEWAY]
68 1.10 mycroft #define netmask info.rti_info[RTAX_NETMASK]
69 1.10 mycroft #define genmask info.rti_info[RTAX_GENMASK]
70 1.10 mycroft #define ifpaddr info.rti_info[RTAX_IFP]
71 1.10 mycroft #define ifaaddr info.rti_info[RTAX_IFA]
72 1.10 mycroft #define brdaddr info.rti_info[RTAX_BRD]
73 1.1 cgd
74 1.1 cgd /*ARGSUSED*/
75 1.9 mycroft int
76 1.1 cgd route_usrreq(so, req, m, nam, control)
77 1.1 cgd register struct socket *so;
78 1.1 cgd int req;
79 1.1 cgd struct mbuf *m, *nam, *control;
80 1.1 cgd {
81 1.1 cgd register int error = 0;
82 1.1 cgd register struct rawcb *rp = sotorawcb(so);
83 1.1 cgd int s;
84 1.10 mycroft
85 1.1 cgd if (req == PRU_ATTACH) {
86 1.1 cgd MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
87 1.1 cgd if (so->so_pcb = (caddr_t)rp)
88 1.1 cgd bzero(so->so_pcb, sizeof(*rp));
89 1.1 cgd
90 1.1 cgd }
91 1.1 cgd if (req == PRU_DETACH && rp) {
92 1.1 cgd int af = rp->rcb_proto.sp_protocol;
93 1.1 cgd if (af == AF_INET)
94 1.1 cgd route_cb.ip_count--;
95 1.1 cgd else if (af == AF_NS)
96 1.1 cgd route_cb.ns_count--;
97 1.1 cgd else if (af == AF_ISO)
98 1.1 cgd route_cb.iso_count--;
99 1.1 cgd route_cb.any_count--;
100 1.1 cgd }
101 1.1 cgd s = splnet();
102 1.1 cgd error = raw_usrreq(so, req, m, nam, control);
103 1.1 cgd rp = sotorawcb(so);
104 1.1 cgd if (req == PRU_ATTACH && rp) {
105 1.1 cgd int af = rp->rcb_proto.sp_protocol;
106 1.1 cgd if (error) {
107 1.1 cgd free((caddr_t)rp, M_PCB);
108 1.1 cgd splx(s);
109 1.1 cgd return (error);
110 1.1 cgd }
111 1.1 cgd if (af == AF_INET)
112 1.1 cgd route_cb.ip_count++;
113 1.1 cgd else if (af == AF_NS)
114 1.1 cgd route_cb.ns_count++;
115 1.1 cgd else if (af == AF_ISO)
116 1.1 cgd route_cb.iso_count++;
117 1.1 cgd rp->rcb_faddr = &route_src;
118 1.1 cgd route_cb.any_count++;
119 1.1 cgd soisconnected(so);
120 1.1 cgd so->so_options |= SO_USELOOPBACK;
121 1.1 cgd }
122 1.1 cgd splx(s);
123 1.1 cgd return (error);
124 1.1 cgd }
125 1.1 cgd
126 1.1 cgd /*ARGSUSED*/
127 1.9 mycroft int
128 1.1 cgd route_output(m, so)
129 1.1 cgd register struct mbuf *m;
130 1.1 cgd struct socket *so;
131 1.1 cgd {
132 1.1 cgd register struct rt_msghdr *rtm = 0;
133 1.1 cgd register struct rtentry *rt = 0;
134 1.1 cgd struct rtentry *saved_nrt = 0;
135 1.10 mycroft struct rt_addrinfo info;
136 1.1 cgd int len, error = 0;
137 1.1 cgd struct ifnet *ifp = 0;
138 1.1 cgd struct ifaddr *ifa = 0;
139 1.1 cgd
140 1.1 cgd #define senderr(e) { error = e; goto flush;}
141 1.10 mycroft if (m == 0 || ((m->m_len < sizeof(long)) &&
142 1.10 mycroft (m = m_pullup(m, sizeof(long))) == 0))
143 1.1 cgd return (ENOBUFS);
144 1.1 cgd if ((m->m_flags & M_PKTHDR) == 0)
145 1.1 cgd panic("route_output");
146 1.1 cgd len = m->m_pkthdr.len;
147 1.1 cgd if (len < sizeof(*rtm) ||
148 1.10 mycroft len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
149 1.10 mycroft dst = 0;
150 1.1 cgd senderr(EINVAL);
151 1.10 mycroft }
152 1.1 cgd R_Malloc(rtm, struct rt_msghdr *, len);
153 1.10 mycroft if (rtm == 0) {
154 1.10 mycroft dst = 0;
155 1.1 cgd senderr(ENOBUFS);
156 1.10 mycroft }
157 1.1 cgd m_copydata(m, 0, len, (caddr_t)rtm);
158 1.10 mycroft if (rtm->rtm_version != RTM_VERSION) {
159 1.10 mycroft dst = 0;
160 1.1 cgd senderr(EPROTONOSUPPORT);
161 1.10 mycroft }
162 1.1 cgd rtm->rtm_pid = curproc->p_pid;
163 1.10 mycroft info.rti_addrs = rtm->rtm_addrs;
164 1.10 mycroft rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
165 1.10 mycroft if (dst == 0)
166 1.1 cgd senderr(EINVAL);
167 1.10 mycroft if (genmask) {
168 1.10 mycroft struct radix_node *t;
169 1.10 mycroft t = rn_addmask((caddr_t)genmask, 1, 2);
170 1.1 cgd if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
171 1.1 cgd genmask = (struct sockaddr *)(t->rn_key);
172 1.1 cgd else
173 1.1 cgd senderr(ENOBUFS);
174 1.1 cgd }
175 1.1 cgd switch (rtm->rtm_type) {
176 1.10 mycroft
177 1.1 cgd case RTM_ADD:
178 1.1 cgd if (gate == 0)
179 1.1 cgd senderr(EINVAL);
180 1.1 cgd error = rtrequest(RTM_ADD, dst, gate, netmask,
181 1.1 cgd rtm->rtm_flags, &saved_nrt);
182 1.1 cgd if (error == 0 && saved_nrt) {
183 1.1 cgd rt_setmetrics(rtm->rtm_inits,
184 1.1 cgd &rtm->rtm_rmx, &saved_nrt->rt_rmx);
185 1.1 cgd saved_nrt->rt_refcnt--;
186 1.1 cgd saved_nrt->rt_genmask = genmask;
187 1.1 cgd }
188 1.1 cgd break;
189 1.1 cgd
190 1.1 cgd case RTM_DELETE:
191 1.1 cgd error = rtrequest(RTM_DELETE, dst, gate, netmask,
192 1.1 cgd rtm->rtm_flags, (struct rtentry **)0);
193 1.1 cgd break;
194 1.1 cgd
195 1.1 cgd case RTM_GET:
196 1.1 cgd case RTM_CHANGE:
197 1.1 cgd case RTM_LOCK:
198 1.1 cgd rt = rtalloc1(dst, 0);
199 1.1 cgd if (rt == 0)
200 1.1 cgd senderr(ESRCH);
201 1.10 mycroft if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
202 1.10 mycroft struct radix_node *rn;
203 1.10 mycroft extern struct radix_node_head *mask_rnhead;
204 1.10 mycroft
205 1.1 cgd if (Bcmp(dst, rt_key(rt), dst->sa_len) != 0)
206 1.1 cgd senderr(ESRCH);
207 1.10 mycroft if (netmask && (rn = rn_search(netmask,
208 1.10 mycroft mask_rnhead->rnh_treetop)))
209 1.10 mycroft netmask = (struct sockaddr *)rn->rn_key;
210 1.10 mycroft for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
211 1.10 mycroft if (netmask == (struct sockaddr *)rn->rn_mask)
212 1.10 mycroft break;
213 1.10 mycroft if (rn == 0)
214 1.1 cgd senderr(ETOOMANYREFS);
215 1.10 mycroft rt = (struct rtentry *)rn;
216 1.1 cgd }
217 1.1 cgd switch(rtm->rtm_type) {
218 1.1 cgd
219 1.1 cgd case RTM_GET:
220 1.10 mycroft dst = rt_key(rt);
221 1.10 mycroft gate = rt->rt_gateway;
222 1.10 mycroft netmask = rt_mask(rt);
223 1.10 mycroft genmask = rt->rt_genmask;
224 1.1 cgd if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
225 1.10 mycroft if (ifp = rt->rt_ifp) {
226 1.10 mycroft ifpaddr = ifp->if_addrlist->ifa_addr;
227 1.1 cgd ifaaddr = rt->rt_ifa->ifa_addr;
228 1.10 mycroft rtm->rtm_index = ifp->if_index;
229 1.1 cgd } else {
230 1.10 mycroft ifpaddr = 0;
231 1.10 mycroft ifaaddr = 0;
232 1.10 mycroft }
233 1.1 cgd }
234 1.10 mycroft len = rt_msg2(RTM_GET, &info, (caddr_t)0,
235 1.10 mycroft (struct walkarg *)0);
236 1.1 cgd if (len > rtm->rtm_msglen) {
237 1.1 cgd struct rt_msghdr *new_rtm;
238 1.1 cgd R_Malloc(new_rtm, struct rt_msghdr *, len);
239 1.1 cgd if (new_rtm == 0)
240 1.1 cgd senderr(ENOBUFS);
241 1.1 cgd Bcopy(rtm, new_rtm, rtm->rtm_msglen);
242 1.1 cgd Free(rtm); rtm = new_rtm;
243 1.1 cgd }
244 1.10 mycroft (void)rt_msg2(RTM_GET, &info, (caddr_t)rtm,
245 1.10 mycroft (struct walkarg *)0);
246 1.1 cgd rtm->rtm_flags = rt->rt_flags;
247 1.1 cgd rtm->rtm_rmx = rt->rt_rmx;
248 1.10 mycroft rtm->rtm_addrs = info.rti_addrs;
249 1.1 cgd break;
250 1.1 cgd
251 1.1 cgd case RTM_CHANGE:
252 1.10 mycroft if (gate && rt_setgate(rt, rt_key(rt), gate))
253 1.1 cgd senderr(EDQUOT);
254 1.1 cgd /* new gateway could require new ifaddr, ifp;
255 1.1 cgd flags may also be different; ifp may be specified
256 1.1 cgd by ll sockaddr when protocol address is ambiguous */
257 1.1 cgd if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
258 1.1 cgd (ifp = ifa->ifa_ifp))
259 1.1 cgd ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
260 1.1 cgd ifp);
261 1.1 cgd else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
262 1.1 cgd (ifa = ifa_ifwithroute(rt->rt_flags,
263 1.1 cgd rt_key(rt), gate)))
264 1.1 cgd ifp = ifa->ifa_ifp;
265 1.1 cgd if (ifa) {
266 1.1 cgd register struct ifaddr *oifa = rt->rt_ifa;
267 1.1 cgd if (oifa != ifa) {
268 1.1 cgd if (oifa && oifa->ifa_rtrequest)
269 1.1 cgd oifa->ifa_rtrequest(RTM_DELETE,
270 1.1 cgd rt, gate);
271 1.10 mycroft IFAFREE(rt->rt_ifa);
272 1.1 cgd rt->rt_ifa = ifa;
273 1.10 mycroft ifa->ifa_refcnt++;
274 1.1 cgd rt->rt_ifp = ifp;
275 1.1 cgd }
276 1.1 cgd }
277 1.1 cgd rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
278 1.1 cgd &rt->rt_rmx);
279 1.1 cgd if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
280 1.1 cgd rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
281 1.1 cgd if (genmask)
282 1.1 cgd rt->rt_genmask = genmask;
283 1.1 cgd /*
284 1.1 cgd * Fall into
285 1.1 cgd */
286 1.1 cgd case RTM_LOCK:
287 1.10 mycroft rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
288 1.1 cgd rt->rt_rmx.rmx_locks |=
289 1.1 cgd (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
290 1.1 cgd break;
291 1.1 cgd }
292 1.10 mycroft break;
293 1.1 cgd
294 1.1 cgd default:
295 1.1 cgd senderr(EOPNOTSUPP);
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd flush:
299 1.1 cgd if (rtm) {
300 1.1 cgd if (error)
301 1.1 cgd rtm->rtm_errno = error;
302 1.1 cgd else
303 1.1 cgd rtm->rtm_flags |= RTF_DONE;
304 1.1 cgd }
305 1.1 cgd if (rt)
306 1.1 cgd rtfree(rt);
307 1.1 cgd {
308 1.1 cgd register struct rawcb *rp = 0;
309 1.1 cgd /*
310 1.1 cgd * Check to see if we don't want our own messages.
311 1.1 cgd */
312 1.1 cgd if ((so->so_options & SO_USELOOPBACK) == 0) {
313 1.1 cgd if (route_cb.any_count <= 1) {
314 1.1 cgd if (rtm)
315 1.1 cgd Free(rtm);
316 1.1 cgd m_freem(m);
317 1.1 cgd return (error);
318 1.1 cgd }
319 1.1 cgd /* There is another listener, so construct message */
320 1.1 cgd rp = sotorawcb(so);
321 1.1 cgd }
322 1.1 cgd if (rtm) {
323 1.1 cgd m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
324 1.1 cgd Free(rtm);
325 1.1 cgd }
326 1.1 cgd if (rp)
327 1.1 cgd rp->rcb_proto.sp_family = 0; /* Avoid us */
328 1.1 cgd if (dst)
329 1.1 cgd route_proto.sp_protocol = dst->sa_family;
330 1.1 cgd raw_input(m, &route_proto, &route_src, &route_dst);
331 1.1 cgd if (rp)
332 1.1 cgd rp->rcb_proto.sp_family = PF_ROUTE;
333 1.1 cgd }
334 1.1 cgd return (error);
335 1.1 cgd }
336 1.1 cgd
337 1.9 mycroft void
338 1.1 cgd rt_setmetrics(which, in, out)
339 1.1 cgd u_long which;
340 1.1 cgd register struct rt_metrics *in, *out;
341 1.1 cgd {
342 1.1 cgd #define metric(f, e) if (which & (f)) out->e = in->e;
343 1.1 cgd metric(RTV_RPIPE, rmx_recvpipe);
344 1.1 cgd metric(RTV_SPIPE, rmx_sendpipe);
345 1.1 cgd metric(RTV_SSTHRESH, rmx_ssthresh);
346 1.1 cgd metric(RTV_RTT, rmx_rtt);
347 1.1 cgd metric(RTV_RTTVAR, rmx_rttvar);
348 1.1 cgd metric(RTV_HOPCOUNT, rmx_hopcount);
349 1.1 cgd metric(RTV_MTU, rmx_mtu);
350 1.1 cgd metric(RTV_EXPIRE, rmx_expire);
351 1.1 cgd #undef metric
352 1.1 cgd }
353 1.1 cgd
354 1.10 mycroft #define ROUNDUP(a) \
355 1.10 mycroft ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
356 1.10 mycroft #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
357 1.10 mycroft
358 1.10 mycroft static void
359 1.10 mycroft rt_xaddrs(cp, cplim, rtinfo)
360 1.10 mycroft register caddr_t cp, cplim;
361 1.10 mycroft register struct rt_addrinfo *rtinfo;
362 1.10 mycroft {
363 1.10 mycroft register struct sockaddr *sa;
364 1.10 mycroft register int i;
365 1.10 mycroft
366 1.10 mycroft bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
367 1.10 mycroft for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
368 1.10 mycroft if ((rtinfo->rti_addrs & (1 << i)) == 0)
369 1.10 mycroft continue;
370 1.10 mycroft rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
371 1.10 mycroft ADVANCE(cp, sa);
372 1.10 mycroft }
373 1.10 mycroft }
374 1.10 mycroft
375 1.1 cgd /*
376 1.1 cgd * Copy data from a buffer back into the indicated mbuf chain,
377 1.1 cgd * starting "off" bytes from the beginning, extending the mbuf
378 1.1 cgd * chain if necessary.
379 1.1 cgd */
380 1.10 mycroft void
381 1.1 cgd m_copyback(m0, off, len, cp)
382 1.1 cgd struct mbuf *m0;
383 1.1 cgd register int off;
384 1.1 cgd register int len;
385 1.1 cgd caddr_t cp;
386 1.1 cgd {
387 1.1 cgd register int mlen;
388 1.1 cgd register struct mbuf *m = m0, *n;
389 1.1 cgd int totlen = 0;
390 1.1 cgd
391 1.1 cgd if (m0 == 0)
392 1.1 cgd return;
393 1.1 cgd while (off > (mlen = m->m_len)) {
394 1.1 cgd off -= mlen;
395 1.1 cgd totlen += mlen;
396 1.1 cgd if (m->m_next == 0) {
397 1.1 cgd n = m_getclr(M_DONTWAIT, m->m_type);
398 1.1 cgd if (n == 0)
399 1.1 cgd goto out;
400 1.1 cgd n->m_len = min(MLEN, len + off);
401 1.1 cgd m->m_next = n;
402 1.1 cgd }
403 1.1 cgd m = m->m_next;
404 1.1 cgd }
405 1.1 cgd while (len > 0) {
406 1.1 cgd mlen = min (m->m_len - off, len);
407 1.1 cgd bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
408 1.1 cgd cp += mlen;
409 1.1 cgd len -= mlen;
410 1.1 cgd mlen += off;
411 1.1 cgd off = 0;
412 1.1 cgd totlen += mlen;
413 1.1 cgd if (len == 0)
414 1.1 cgd break;
415 1.1 cgd if (m->m_next == 0) {
416 1.1 cgd n = m_get(M_DONTWAIT, m->m_type);
417 1.1 cgd if (n == 0)
418 1.1 cgd break;
419 1.1 cgd n->m_len = min(MLEN, len);
420 1.1 cgd m->m_next = n;
421 1.1 cgd }
422 1.1 cgd m = m->m_next;
423 1.1 cgd }
424 1.1 cgd out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
425 1.1 cgd m->m_pkthdr.len = totlen;
426 1.1 cgd }
427 1.1 cgd
428 1.10 mycroft static struct mbuf *
429 1.10 mycroft rt_msg1(type, rtinfo)
430 1.10 mycroft int type;
431 1.10 mycroft register struct rt_addrinfo *rtinfo;
432 1.1 cgd {
433 1.1 cgd register struct rt_msghdr *rtm;
434 1.1 cgd register struct mbuf *m;
435 1.10 mycroft register int i;
436 1.10 mycroft register struct sockaddr *sa;
437 1.10 mycroft int len, dlen;
438 1.1 cgd
439 1.1 cgd m = m_gethdr(M_DONTWAIT, MT_DATA);
440 1.1 cgd if (m == 0)
441 1.10 mycroft return (m);
442 1.10 mycroft switch (type) {
443 1.10 mycroft
444 1.10 mycroft case RTM_DELADDR:
445 1.10 mycroft case RTM_NEWADDR:
446 1.10 mycroft len = sizeof(struct ifa_msghdr);
447 1.10 mycroft break;
448 1.10 mycroft
449 1.10 mycroft case RTM_IFINFO:
450 1.10 mycroft len = sizeof(struct if_msghdr);
451 1.10 mycroft break;
452 1.10 mycroft
453 1.10 mycroft default:
454 1.10 mycroft len = sizeof(struct rt_msghdr);
455 1.10 mycroft }
456 1.10 mycroft if (len > MHLEN)
457 1.10 mycroft panic("rt_msg1");
458 1.10 mycroft m->m_pkthdr.len = m->m_len = len;
459 1.1 cgd m->m_pkthdr.rcvif = 0;
460 1.1 cgd rtm = mtod(m, struct rt_msghdr *);
461 1.10 mycroft bzero((caddr_t)rtm, len);
462 1.10 mycroft for (i = 0; i < RTAX_MAX; i++) {
463 1.10 mycroft if ((sa = rtinfo->rti_info[i]) == NULL)
464 1.10 mycroft continue;
465 1.10 mycroft rtinfo->rti_addrs |= (1 << i);
466 1.10 mycroft dlen = ROUNDUP(sa->sa_len);
467 1.10 mycroft m_copyback(m, len, dlen, (caddr_t)sa);
468 1.10 mycroft len += dlen;
469 1.10 mycroft }
470 1.10 mycroft if (m->m_pkthdr.len != len) {
471 1.10 mycroft m_freem(m);
472 1.10 mycroft return (NULL);
473 1.10 mycroft }
474 1.1 cgd rtm->rtm_msglen = len;
475 1.1 cgd rtm->rtm_version = RTM_VERSION;
476 1.1 cgd rtm->rtm_type = type;
477 1.10 mycroft return (m);
478 1.10 mycroft }
479 1.10 mycroft
480 1.10 mycroft static int
481 1.10 mycroft rt_msg2(type, rtinfo, cp, w)
482 1.10 mycroft int type;
483 1.10 mycroft register struct rt_addrinfo *rtinfo;
484 1.10 mycroft caddr_t cp;
485 1.10 mycroft struct walkarg *w;
486 1.10 mycroft {
487 1.10 mycroft register int i;
488 1.10 mycroft int len, dlen, second_time = 0;
489 1.10 mycroft caddr_t cp0;
490 1.10 mycroft
491 1.10 mycroft rtinfo->rti_addrs = 0;
492 1.10 mycroft again:
493 1.10 mycroft switch (type) {
494 1.10 mycroft
495 1.10 mycroft case RTM_DELADDR:
496 1.10 mycroft case RTM_NEWADDR:
497 1.10 mycroft len = sizeof(struct ifa_msghdr);
498 1.10 mycroft break;
499 1.10 mycroft
500 1.10 mycroft case RTM_IFINFO:
501 1.10 mycroft len = sizeof(struct if_msghdr);
502 1.10 mycroft break;
503 1.10 mycroft
504 1.10 mycroft default:
505 1.10 mycroft len = sizeof(struct rt_msghdr);
506 1.10 mycroft }
507 1.10 mycroft if (cp0 = cp)
508 1.10 mycroft cp += len;
509 1.10 mycroft for (i = 0; i < RTAX_MAX; i++) {
510 1.10 mycroft register struct sockaddr *sa;
511 1.10 mycroft
512 1.10 mycroft if ((sa = rtinfo->rti_info[i]) == 0)
513 1.10 mycroft continue;
514 1.10 mycroft rtinfo->rti_addrs |= (1 << i);
515 1.10 mycroft dlen = ROUNDUP(sa->sa_len);
516 1.10 mycroft if (cp) {
517 1.10 mycroft bcopy((caddr_t)sa, cp, (unsigned)dlen);
518 1.10 mycroft cp += dlen;
519 1.10 mycroft }
520 1.1 cgd len += dlen;
521 1.1 cgd }
522 1.10 mycroft if (cp == 0 && w != NULL && !second_time) {
523 1.10 mycroft register struct walkarg *rw = w;
524 1.10 mycroft
525 1.10 mycroft rw->w_needed += len;
526 1.10 mycroft if (rw->w_needed <= 0 && rw->w_where) {
527 1.10 mycroft if (rw->w_tmemsize < len) {
528 1.10 mycroft if (rw->w_tmem)
529 1.10 mycroft free(rw->w_tmem, M_RTABLE);
530 1.10 mycroft if (rw->w_tmem = (caddr_t)
531 1.10 mycroft malloc(len, M_RTABLE, M_NOWAIT))
532 1.10 mycroft rw->w_tmemsize = len;
533 1.10 mycroft }
534 1.10 mycroft if (rw->w_tmem) {
535 1.10 mycroft cp = rw->w_tmem;
536 1.10 mycroft second_time = 1;
537 1.10 mycroft goto again;
538 1.10 mycroft } else
539 1.10 mycroft rw->w_where = 0;
540 1.10 mycroft }
541 1.1 cgd }
542 1.10 mycroft if (cp) {
543 1.10 mycroft register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
544 1.10 mycroft
545 1.10 mycroft rtm->rtm_version = RTM_VERSION;
546 1.10 mycroft rtm->rtm_type = type;
547 1.10 mycroft rtm->rtm_msglen = len;
548 1.1 cgd }
549 1.10 mycroft return (len);
550 1.10 mycroft }
551 1.10 mycroft
552 1.10 mycroft /*
553 1.10 mycroft * This routine is called to generate a message from the routing
554 1.10 mycroft * socket indicating that a redirect has occured, a routing lookup
555 1.10 mycroft * has failed, or that a protocol has detected timeouts to a particular
556 1.10 mycroft * destination.
557 1.10 mycroft */
558 1.10 mycroft void
559 1.10 mycroft rt_missmsg(type, rtinfo, flags, error)
560 1.10 mycroft int type, flags, error;
561 1.10 mycroft register struct rt_addrinfo *rtinfo;
562 1.10 mycroft {
563 1.10 mycroft register struct rt_msghdr *rtm;
564 1.10 mycroft register struct mbuf *m;
565 1.10 mycroft struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
566 1.10 mycroft
567 1.10 mycroft if (route_cb.any_count == 0)
568 1.10 mycroft return;
569 1.10 mycroft m = rt_msg1(type, rtinfo);
570 1.10 mycroft if (m == 0)
571 1.1 cgd return;
572 1.10 mycroft rtm = mtod(m, struct rt_msghdr *);
573 1.10 mycroft rtm->rtm_flags = RTF_DONE | flags;
574 1.1 cgd rtm->rtm_errno = error;
575 1.10 mycroft rtm->rtm_addrs = rtinfo->rti_addrs;
576 1.10 mycroft route_proto.sp_protocol = sa ? sa->sa_family : 0;
577 1.10 mycroft raw_input(m, &route_proto, &route_src, &route_dst);
578 1.10 mycroft }
579 1.10 mycroft
580 1.10 mycroft /*
581 1.10 mycroft * This routine is called to generate a message from the routing
582 1.10 mycroft * socket indicating that the status of a network interface has changed.
583 1.10 mycroft */
584 1.10 mycroft void
585 1.10 mycroft rt_ifmsg(ifp)
586 1.10 mycroft register struct ifnet *ifp;
587 1.10 mycroft {
588 1.10 mycroft register struct if_msghdr *ifm;
589 1.10 mycroft struct mbuf *m;
590 1.10 mycroft struct rt_addrinfo info;
591 1.10 mycroft
592 1.10 mycroft if (route_cb.any_count == 0)
593 1.10 mycroft return;
594 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
595 1.10 mycroft m = rt_msg1(RTM_IFINFO, &info);
596 1.10 mycroft if (m == 0)
597 1.10 mycroft return;
598 1.10 mycroft ifm = mtod(m, struct if_msghdr *);
599 1.10 mycroft ifm->ifm_index = ifp->if_index;
600 1.10 mycroft ifm->ifm_flags = ifp->if_flags;
601 1.10 mycroft ifm->ifm_data = ifp->if_data;
602 1.10 mycroft ifm->ifm_addrs = 0;
603 1.10 mycroft route_proto.sp_protocol = 0;
604 1.1 cgd raw_input(m, &route_proto, &route_src, &route_dst);
605 1.1 cgd }
606 1.1 cgd
607 1.1 cgd /*
608 1.10 mycroft * This is called to generate messages from the routing socket
609 1.10 mycroft * indicating a network interface has had addresses associated with it.
610 1.10 mycroft * if we ever reverse the logic and replace messages TO the routing
611 1.10 mycroft * socket indicate a request to configure interfaces, then it will
612 1.10 mycroft * be unnecessary as the routing socket will automatically generate
613 1.10 mycroft * copies of it.
614 1.10 mycroft */
615 1.10 mycroft void
616 1.10 mycroft rt_newaddrmsg(cmd, ifa, error, rt)
617 1.10 mycroft int cmd, error;
618 1.10 mycroft register struct ifaddr *ifa;
619 1.10 mycroft register struct rtentry *rt;
620 1.10 mycroft {
621 1.10 mycroft struct rt_addrinfo info;
622 1.10 mycroft struct sockaddr *sa;
623 1.10 mycroft int pass;
624 1.10 mycroft struct mbuf *m;
625 1.10 mycroft struct ifnet *ifp = ifa->ifa_ifp;
626 1.10 mycroft
627 1.10 mycroft if (route_cb.any_count == 0)
628 1.10 mycroft return;
629 1.10 mycroft for (pass = 1; pass < 3; pass++) {
630 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
631 1.10 mycroft if ((cmd == RTM_ADD && pass == 1) ||
632 1.10 mycroft (cmd == RTM_DELETE && pass == 2)) {
633 1.10 mycroft register struct ifa_msghdr *ifam;
634 1.10 mycroft int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
635 1.10 mycroft
636 1.10 mycroft ifaaddr = sa = ifa->ifa_addr;
637 1.10 mycroft ifpaddr = ifp->if_addrlist->ifa_addr;
638 1.10 mycroft netmask = ifa->ifa_netmask;
639 1.10 mycroft brdaddr = ifa->ifa_dstaddr;
640 1.10 mycroft if ((m = rt_msg1(ncmd, &info)) == NULL)
641 1.10 mycroft continue;
642 1.10 mycroft ifam = mtod(m, struct ifa_msghdr *);
643 1.10 mycroft ifam->ifam_index = ifp->if_index;
644 1.10 mycroft ifam->ifam_metric = ifa->ifa_metric;
645 1.10 mycroft ifam->ifam_flags = ifa->ifa_flags;
646 1.10 mycroft ifam->ifam_addrs = info.rti_addrs;
647 1.10 mycroft }
648 1.10 mycroft if ((cmd == RTM_ADD && pass == 2) ||
649 1.10 mycroft (cmd == RTM_DELETE && pass == 1)) {
650 1.10 mycroft register struct rt_msghdr *rtm;
651 1.10 mycroft
652 1.10 mycroft if (rt == 0)
653 1.10 mycroft continue;
654 1.10 mycroft netmask = rt_mask(rt);
655 1.10 mycroft dst = sa = rt_key(rt);
656 1.10 mycroft gate = rt->rt_gateway;
657 1.10 mycroft if ((m = rt_msg1(cmd, &info)) == NULL)
658 1.10 mycroft continue;
659 1.10 mycroft rtm = mtod(m, struct rt_msghdr *);
660 1.10 mycroft rtm->rtm_index = ifp->if_index;
661 1.10 mycroft rtm->rtm_flags |= rt->rt_flags;
662 1.10 mycroft rtm->rtm_errno = error;
663 1.10 mycroft rtm->rtm_addrs = info.rti_addrs;
664 1.10 mycroft }
665 1.10 mycroft route_proto.sp_protocol = sa ? sa->sa_family : 0;
666 1.10 mycroft raw_input(m, &route_proto, &route_src, &route_dst);
667 1.10 mycroft }
668 1.10 mycroft }
669 1.10 mycroft
670 1.10 mycroft /*
671 1.10 mycroft * This is used in dumping the kernel table via sysctl().
672 1.1 cgd */
673 1.10 mycroft int
674 1.10 mycroft sysctl_dumpentry(rn, w)
675 1.1 cgd struct radix_node *rn;
676 1.1 cgd register struct walkarg *w;
677 1.1 cgd {
678 1.10 mycroft register struct rtentry *rt = (struct rtentry *)rn;
679 1.10 mycroft int error = 0, size;
680 1.10 mycroft struct rt_addrinfo info;
681 1.1 cgd
682 1.10 mycroft if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
683 1.10 mycroft return 0;
684 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
685 1.10 mycroft dst = rt_key(rt);
686 1.10 mycroft gate = rt->rt_gateway;
687 1.10 mycroft netmask = rt_mask(rt);
688 1.10 mycroft genmask = rt->rt_genmask;
689 1.10 mycroft size = rt_msg2(RTM_GET, &info, 0, w);
690 1.10 mycroft if (w->w_where && w->w_tmem) {
691 1.10 mycroft register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
692 1.10 mycroft
693 1.10 mycroft rtm->rtm_flags = rt->rt_flags;
694 1.10 mycroft rtm->rtm_use = rt->rt_use;
695 1.10 mycroft rtm->rtm_rmx = rt->rt_rmx;
696 1.10 mycroft rtm->rtm_index = rt->rt_ifp->if_index;
697 1.10 mycroft rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
698 1.10 mycroft rtm->rtm_addrs = info.rti_addrs;
699 1.10 mycroft if (error = copyout((caddr_t)rtm, w->w_where, size))
700 1.10 mycroft w->w_where = NULL;
701 1.10 mycroft else
702 1.10 mycroft w->w_where += size;
703 1.10 mycroft }
704 1.10 mycroft return (error);
705 1.10 mycroft }
706 1.1 cgd
707 1.10 mycroft int
708 1.10 mycroft sysctl_iflist(af, w)
709 1.10 mycroft int af;
710 1.10 mycroft register struct walkarg *w;
711 1.10 mycroft {
712 1.10 mycroft register struct ifnet *ifp;
713 1.10 mycroft register struct ifaddr *ifa;
714 1.10 mycroft struct rt_addrinfo info;
715 1.10 mycroft int len, error = 0;
716 1.10 mycroft
717 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
718 1.10 mycroft for (ifp = ifnet; ifp; ifp = ifp->if_next) {
719 1.10 mycroft if (w->w_arg && w->w_arg != ifp->if_index)
720 1.10 mycroft continue;
721 1.10 mycroft ifa = ifp->if_addrlist;
722 1.10 mycroft ifpaddr = ifa->ifa_addr;
723 1.10 mycroft len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
724 1.10 mycroft ifpaddr = 0;
725 1.10 mycroft if (w->w_where && w->w_tmem) {
726 1.10 mycroft register struct if_msghdr *ifm;
727 1.10 mycroft
728 1.10 mycroft ifm = (struct if_msghdr *)w->w_tmem;
729 1.10 mycroft ifm->ifm_index = ifp->if_index;
730 1.10 mycroft ifm->ifm_flags = ifp->if_flags;
731 1.10 mycroft ifm->ifm_data = ifp->if_data;
732 1.10 mycroft ifm->ifm_addrs = info.rti_addrs;
733 1.10 mycroft if (error = copyout((caddr_t)ifm, w->w_where, len))
734 1.10 mycroft return (error);
735 1.10 mycroft w->w_where += len;
736 1.10 mycroft }
737 1.10 mycroft while (ifa = ifa->ifa_next) {
738 1.10 mycroft if (af && af != ifa->ifa_addr->sa_family)
739 1.10 mycroft continue;
740 1.10 mycroft ifaaddr = ifa->ifa_addr;
741 1.10 mycroft netmask = ifa->ifa_netmask;
742 1.10 mycroft brdaddr = ifa->ifa_dstaddr;
743 1.10 mycroft len = rt_msg2(RTM_NEWADDR, &info, 0, w);
744 1.10 mycroft if (w->w_where && w->w_tmem) {
745 1.10 mycroft register struct ifa_msghdr *ifam;
746 1.10 mycroft
747 1.10 mycroft ifam = (struct ifa_msghdr *)w->w_tmem;
748 1.10 mycroft ifam->ifam_index = ifa->ifa_ifp->if_index;
749 1.10 mycroft ifam->ifam_flags = ifa->ifa_flags;
750 1.10 mycroft ifam->ifam_metric = ifa->ifa_metric;
751 1.10 mycroft ifam->ifam_addrs = info.rti_addrs;
752 1.10 mycroft if (error = copyout(w->w_tmem, w->w_where, len))
753 1.10 mycroft return (error);
754 1.10 mycroft w->w_where += len;
755 1.10 mycroft }
756 1.10 mycroft }
757 1.10 mycroft ifaaddr = netmask = brdaddr = 0;
758 1.10 mycroft }
759 1.1 cgd return (0);
760 1.1 cgd }
761 1.1 cgd
762 1.10 mycroft int
763 1.10 mycroft sysctl_rtable(name, namelen, where, given, new, newlen)
764 1.10 mycroft int *name;
765 1.10 mycroft int namelen;
766 1.1 cgd caddr_t where;
767 1.10 mycroft size_t *given;
768 1.10 mycroft caddr_t *new;
769 1.10 mycroft size_t newlen;
770 1.1 cgd {
771 1.1 cgd register struct radix_node_head *rnh;
772 1.10 mycroft int i, s, error = EINVAL;
773 1.10 mycroft u_char af;
774 1.1 cgd struct walkarg w;
775 1.1 cgd
776 1.10 mycroft if (new)
777 1.10 mycroft return (EPERM);
778 1.10 mycroft if (namelen != 3)
779 1.1 cgd return (EINVAL);
780 1.10 mycroft af = name[0];
781 1.1 cgd Bzero(&w, sizeof(w));
782 1.10 mycroft w.w_where = where;
783 1.10 mycroft w.w_given = *given;
784 1.1 cgd w.w_needed = 0 - w.w_given;
785 1.10 mycroft w.w_op = name[1];
786 1.10 mycroft w.w_arg = name[2];
787 1.1 cgd
788 1.1 cgd s = splnet();
789 1.10 mycroft switch (w.w_op) {
790 1.10 mycroft
791 1.10 mycroft case NET_RT_DUMP:
792 1.10 mycroft case NET_RT_FLAGS:
793 1.10 mycroft for (i = 1; i <= AF_MAX; i++)
794 1.10 mycroft if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
795 1.10 mycroft (error = rnh->rnh_walktree(rnh,
796 1.10 mycroft sysctl_dumpentry, &w)))
797 1.10 mycroft break;
798 1.10 mycroft break;
799 1.10 mycroft
800 1.10 mycroft case NET_RT_IFLIST:
801 1.10 mycroft error = sysctl_iflist(af, &w);
802 1.1 cgd }
803 1.10 mycroft splx(s);
804 1.10 mycroft if (w.w_tmem)
805 1.10 mycroft free(w.w_tmem, M_RTABLE);
806 1.1 cgd w.w_needed += w.w_given;
807 1.10 mycroft if (where) {
808 1.1 cgd *given = w.w_where - where;
809 1.10 mycroft if (*given < w.w_needed)
810 1.10 mycroft return (ENOMEM);
811 1.10 mycroft } else {
812 1.10 mycroft *given = (11 * w.w_needed) / 10;
813 1.10 mycroft }
814 1.1 cgd return (error);
815 1.1 cgd }
816 1.1 cgd
817 1.1 cgd /*
818 1.1 cgd * Definitions of protocols supported in the ROUTE domain.
819 1.1 cgd */
820 1.1 cgd
821 1.1 cgd extern struct domain routedomain; /* or at least forward */
822 1.1 cgd
823 1.1 cgd struct protosw routesw[] = {
824 1.1 cgd { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
825 1.1 cgd raw_input, route_output, raw_ctlinput, 0,
826 1.1 cgd route_usrreq,
827 1.1 cgd raw_init, 0, 0, 0,
828 1.10 mycroft sysctl_rtable,
829 1.1 cgd }
830 1.1 cgd };
831 1.1 cgd
832 1.1 cgd struct domain routedomain =
833 1.10 mycroft { PF_ROUTE, "route", route_init, 0, 0,
834 1.1 cgd routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
835