FreeBSD/Linux Kernel Cross Reference
sys/net/rtsock.c
1 /*-
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
30 * $FreeBSD: releng/6.0/sys/net/rtsock.c 150355 2005-09-20 01:59:54Z csjp $
31 */
32
33 #include <sys/param.h>
34 #include <sys/domain.h>
35 #include <sys/kernel.h>
36 #include <sys/jail.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/proc.h>
40 #include <sys/protosw.h>
41 #include <sys/signalvar.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46
47 #include <net/if.h>
48 #include <net/netisr.h>
49 #include <net/raw_cb.h>
50 #include <net/route.h>
51
52 #include <netinet/in.h>
53
54 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
55
56 /* NB: these are not modified */
57 static struct sockaddr route_dst = { 2, PF_ROUTE, };
58 static struct sockaddr route_src = { 2, PF_ROUTE, };
59 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
60
61 static struct {
62 int ip_count; /* attached w/ AF_INET */
63 int ip6_count; /* attached w/ AF_INET6 */
64 int ipx_count; /* attached w/ AF_IPX */
65 int any_count; /* total attached */
66 } route_cb;
67
68 struct mtx rtsock_mtx;
69 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
70
71 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
72 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
73 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
74
75 static struct ifqueue rtsintrq;
76
77 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
78 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
79 &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
80
81 struct walkarg {
82 int w_tmemsize;
83 int w_op, w_arg;
84 caddr_t w_tmem;
85 struct sysctl_req *w_req;
86 };
87
88 static void rts_input(struct mbuf *m);
89 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
90 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
91 caddr_t cp, struct walkarg *w);
92 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
93 struct rt_addrinfo *rtinfo);
94 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
95 static int sysctl_iflist(int af, struct walkarg *w);
96 static int sysctl_ifmalist(int af, struct walkarg *w);
97 static int route_output(struct mbuf *m, struct socket *so);
98 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
99 struct rt_metrics_lite *out);
100 static void rt_getmetrics(const struct rt_metrics_lite *in,
101 struct rt_metrics *out);
102 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
103
104 static void
105 rts_init(void)
106 {
107 int tmp;
108
109 rtsintrq.ifq_maxlen = 256;
110 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
111 rtsintrq.ifq_maxlen = tmp;
112 mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
113 netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
114 }
115 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0)
116
117 static void
118 rts_input(struct mbuf *m)
119 {
120 struct sockproto route_proto;
121 unsigned short *family;
122 struct m_tag *tag;
123
124 route_proto.sp_family = PF_ROUTE;
125 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
126 if (tag != NULL) {
127 family = (unsigned short *)(tag + 1);
128 route_proto.sp_protocol = *family;
129 m_tag_delete(m, tag);
130 } else
131 route_proto.sp_protocol = 0;
132
133 raw_input(m, &route_proto, &route_src, &route_dst);
134 }
135
136 /*
137 * It really doesn't make any sense at all for this code to share much
138 * with raw_usrreq.c, since its functionality is so restricted. XXX
139 */
140 static int
141 rts_abort(struct socket *so)
142 {
143
144 return (raw_usrreqs.pru_abort(so));
145 }
146
147 /* pru_accept is EOPNOTSUPP */
148
149 static int
150 rts_attach(struct socket *so, int proto, struct thread *td)
151 {
152 struct rawcb *rp;
153 int s, error;
154
155 if (sotorawcb(so) != NULL)
156 return EISCONN; /* XXX panic? */
157 /* XXX */
158 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
159 if (rp == NULL)
160 return ENOBUFS;
161
162 /*
163 * The splnet() is necessary to block protocols from sending
164 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
165 * this PCB is extant but incompletely initialized.
166 * Probably we should try to do more of this work beforehand and
167 * eliminate the spl.
168 */
169 s = splnet();
170 so->so_pcb = (caddr_t)rp;
171 error = raw_attach(so, proto);
172 rp = sotorawcb(so);
173 if (error) {
174 splx(s);
175 so->so_pcb = NULL;
176 free(rp, M_PCB);
177 return error;
178 }
179 RTSOCK_LOCK();
180 switch(rp->rcb_proto.sp_protocol) {
181 case AF_INET:
182 route_cb.ip_count++;
183 break;
184 case AF_INET6:
185 route_cb.ip6_count++;
186 break;
187 case AF_IPX:
188 route_cb.ipx_count++;
189 break;
190 }
191 rp->rcb_faddr = &route_src;
192 route_cb.any_count++;
193 RTSOCK_UNLOCK();
194 soisconnected(so);
195 so->so_options |= SO_USELOOPBACK;
196 splx(s);
197 return 0;
198 }
199
200 static int
201 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
202 {
203
204 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
205 }
206
207 static int
208 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
209 {
210
211 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
212 }
213
214 /* pru_connect2 is EOPNOTSUPP */
215 /* pru_control is EOPNOTSUPP */
216
217 static int
218 rts_detach(struct socket *so)
219 {
220 struct rawcb *rp = sotorawcb(so);
221 int s, error;
222
223 s = splnet();
224 if (rp != NULL) {
225 RTSOCK_LOCK();
226 switch(rp->rcb_proto.sp_protocol) {
227 case AF_INET:
228 route_cb.ip_count--;
229 break;
230 case AF_INET6:
231 route_cb.ip6_count--;
232 break;
233 case AF_IPX:
234 route_cb.ipx_count--;
235 break;
236 }
237 route_cb.any_count--;
238 RTSOCK_UNLOCK();
239 }
240 error = raw_usrreqs.pru_detach(so);
241 splx(s);
242 return error;
243 }
244
245 static int
246 rts_disconnect(struct socket *so)
247 {
248
249 return (raw_usrreqs.pru_disconnect(so));
250 }
251
252 /* pru_listen is EOPNOTSUPP */
253
254 static int
255 rts_peeraddr(struct socket *so, struct sockaddr **nam)
256 {
257
258 return (raw_usrreqs.pru_peeraddr(so, nam));
259 }
260
261 /* pru_rcvd is EOPNOTSUPP */
262 /* pru_rcvoob is EOPNOTSUPP */
263
264 static int
265 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
266 struct mbuf *control, struct thread *td)
267 {
268
269 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
270 }
271
272 /* pru_sense is null */
273
274 static int
275 rts_shutdown(struct socket *so)
276 {
277
278 return (raw_usrreqs.pru_shutdown(so));
279 }
280
281 static int
282 rts_sockaddr(struct socket *so, struct sockaddr **nam)
283 {
284
285 return (raw_usrreqs.pru_sockaddr(so, nam));
286 }
287
288 static struct pr_usrreqs route_usrreqs = {
289 .pru_abort = rts_abort,
290 .pru_attach = rts_attach,
291 .pru_bind = rts_bind,
292 .pru_connect = rts_connect,
293 .pru_detach = rts_detach,
294 .pru_disconnect = rts_disconnect,
295 .pru_peeraddr = rts_peeraddr,
296 .pru_send = rts_send,
297 .pru_shutdown = rts_shutdown,
298 .pru_sockaddr = rts_sockaddr,
299 };
300
301 /*ARGSUSED*/
302 static int
303 route_output(struct mbuf *m, struct socket *so)
304 {
305 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
306 struct rt_msghdr *rtm = NULL;
307 struct rtentry *rt = NULL;
308 struct radix_node_head *rnh;
309 struct rt_addrinfo info;
310 int len, error = 0;
311 struct ifnet *ifp = NULL;
312 struct ifaddr *ifa = NULL;
313 struct sockaddr_in jail;
314
315 #define senderr(e) { error = e; goto flush;}
316 if (m == NULL || ((m->m_len < sizeof(long)) &&
317 (m = m_pullup(m, sizeof(long))) == NULL))
318 return (ENOBUFS);
319 if ((m->m_flags & M_PKTHDR) == 0)
320 panic("route_output");
321 len = m->m_pkthdr.len;
322 if (len < sizeof(*rtm) ||
323 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
324 info.rti_info[RTAX_DST] = NULL;
325 senderr(EINVAL);
326 }
327 R_Malloc(rtm, struct rt_msghdr *, len);
328 if (rtm == NULL) {
329 info.rti_info[RTAX_DST] = NULL;
330 senderr(ENOBUFS);
331 }
332 m_copydata(m, 0, len, (caddr_t)rtm);
333 if (rtm->rtm_version != RTM_VERSION) {
334 info.rti_info[RTAX_DST] = NULL;
335 senderr(EPROTONOSUPPORT);
336 }
337 rtm->rtm_pid = curproc->p_pid;
338 bzero(&info, sizeof(info));
339 info.rti_addrs = rtm->rtm_addrs;
340 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
341 info.rti_info[RTAX_DST] = NULL;
342 senderr(EINVAL);
343 }
344 info.rti_flags = rtm->rtm_flags;
345 if (info.rti_info[RTAX_DST] == NULL ||
346 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
347 (info.rti_info[RTAX_GATEWAY] != NULL &&
348 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
349 senderr(EINVAL);
350 if (info.rti_info[RTAX_GENMASK]) {
351 struct radix_node *t;
352 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
353 if (t != NULL &&
354 bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
355 (char *)(void *)t->rn_key + 1,
356 ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
357 info.rti_info[RTAX_GENMASK] =
358 (struct sockaddr *)t->rn_key;
359 else
360 senderr(ENOBUFS);
361 }
362
363 /*
364 * Verify that the caller has the appropriate privilege; RTM_GET
365 * is the only operation the non-superuser is allowed.
366 */
367 if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0)
368 senderr(error);
369
370 switch (rtm->rtm_type) {
371 struct rtentry *saved_nrt;
372
373 case RTM_ADD:
374 if (info.rti_info[RTAX_GATEWAY] == NULL)
375 senderr(EINVAL);
376 saved_nrt = NULL;
377 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
378 if (error == 0 && saved_nrt) {
379 RT_LOCK(saved_nrt);
380 rt_setmetrics(rtm->rtm_inits,
381 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
382 RT_REMREF(saved_nrt);
383 saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
384 RT_UNLOCK(saved_nrt);
385 }
386 break;
387
388 case RTM_DELETE:
389 saved_nrt = NULL;
390 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
391 if (error == 0) {
392 RT_LOCK(saved_nrt);
393 rt = saved_nrt;
394 goto report;
395 }
396 break;
397
398 case RTM_GET:
399 case RTM_CHANGE:
400 case RTM_LOCK:
401 rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
402 if (rnh == NULL)
403 senderr(EAFNOSUPPORT);
404 RADIX_NODE_HEAD_LOCK(rnh);
405 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
406 info.rti_info[RTAX_NETMASK], rnh);
407 if (rt == NULL) { /* XXX looks bogus */
408 RADIX_NODE_HEAD_UNLOCK(rnh);
409 senderr(ESRCH);
410 }
411 RT_LOCK(rt);
412 RT_ADDREF(rt);
413 RADIX_NODE_HEAD_UNLOCK(rnh);
414
415 switch(rtm->rtm_type) {
416
417 case RTM_GET:
418 report:
419 RT_LOCK_ASSERT(rt);
420 info.rti_info[RTAX_DST] = rt_key(rt);
421 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
422 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
423 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
424 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
425 ifp = rt->rt_ifp;
426 if (ifp) {
427 info.rti_info[RTAX_IFP] =
428 ifaddr_byindex(ifp->if_index)->ifa_addr;
429 if (jailed(so->so_cred)) {
430 bzero(&jail, sizeof(jail));
431 jail.sin_family = PF_INET;
432 jail.sin_len = sizeof(jail);
433 jail.sin_addr.s_addr =
434 htonl(prison_getip(so->so_cred));
435 info.rti_info[RTAX_IFA] =
436 (struct sockaddr *)&jail;
437 } else
438 info.rti_info[RTAX_IFA] =
439 rt->rt_ifa->ifa_addr;
440 if (ifp->if_flags & IFF_POINTOPOINT)
441 info.rti_info[RTAX_BRD] =
442 rt->rt_ifa->ifa_dstaddr;
443 rtm->rtm_index = ifp->if_index;
444 } else {
445 info.rti_info[RTAX_IFP] = NULL;
446 info.rti_info[RTAX_IFA] = NULL;
447 }
448 } else if ((ifp = rt->rt_ifp) != NULL) {
449 rtm->rtm_index = ifp->if_index;
450 }
451 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
452 if (len > rtm->rtm_msglen) {
453 struct rt_msghdr *new_rtm;
454 R_Malloc(new_rtm, struct rt_msghdr *, len);
455 if (new_rtm == NULL) {
456 RT_UNLOCK(rt);
457 senderr(ENOBUFS);
458 }
459 bcopy(rtm, new_rtm, rtm->rtm_msglen);
460 Free(rtm); rtm = new_rtm;
461 }
462 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
463 rtm->rtm_flags = rt->rt_flags;
464 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
465 rtm->rtm_addrs = info.rti_addrs;
466 break;
467
468 case RTM_CHANGE:
469 /*
470 * New gateway could require new ifaddr, ifp;
471 * flags may also be different; ifp may be specified
472 * by ll sockaddr when protocol address is ambiguous
473 */
474 if (((rt->rt_flags & RTF_GATEWAY) &&
475 info.rti_info[RTAX_GATEWAY] != NULL) ||
476 info.rti_info[RTAX_IFP] != NULL ||
477 (info.rti_info[RTAX_IFA] != NULL &&
478 !sa_equal(info.rti_info[RTAX_IFA],
479 rt->rt_ifa->ifa_addr))) {
480 if ((error = rt_getifa(&info)) != 0) {
481 RT_UNLOCK(rt);
482 senderr(error);
483 }
484 }
485 if (info.rti_info[RTAX_GATEWAY] != NULL &&
486 (error = rt_setgate(rt, rt_key(rt),
487 info.rti_info[RTAX_GATEWAY])) != 0) {
488 RT_UNLOCK(rt);
489 senderr(error);
490 }
491 if ((ifa = info.rti_ifa) != NULL) {
492 struct ifaddr *oifa = rt->rt_ifa;
493 if (oifa != ifa) {
494 if (oifa) {
495 if (oifa->ifa_rtrequest)
496 oifa->ifa_rtrequest(
497 RTM_DELETE, rt,
498 &info);
499 IFAFREE(oifa);
500 }
501 IFAREF(ifa);
502 rt->rt_ifa = ifa;
503 rt->rt_ifp = info.rti_ifp;
504 }
505 }
506 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
507 &rt->rt_rmx);
508 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
509 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
510 if (info.rti_info[RTAX_GENMASK])
511 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
512 /* FALLTHROUGH */
513 case RTM_LOCK:
514 /* We don't support locks anymore */
515 break;
516 }
517 RT_UNLOCK(rt);
518 break;
519
520 default:
521 senderr(EOPNOTSUPP);
522 }
523
524 flush:
525 if (rtm) {
526 if (error)
527 rtm->rtm_errno = error;
528 else
529 rtm->rtm_flags |= RTF_DONE;
530 }
531 if (rt) /* XXX can this be true? */
532 RTFREE(rt);
533 {
534 struct rawcb *rp = NULL;
535 /*
536 * Check to see if we don't want our own messages.
537 */
538 if ((so->so_options & SO_USELOOPBACK) == 0) {
539 if (route_cb.any_count <= 1) {
540 if (rtm)
541 Free(rtm);
542 m_freem(m);
543 return (error);
544 }
545 /* There is another listener, so construct message */
546 rp = sotorawcb(so);
547 }
548 if (rtm) {
549 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
550 if (m->m_pkthdr.len < rtm->rtm_msglen) {
551 m_freem(m);
552 m = NULL;
553 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
554 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
555 Free(rtm);
556 }
557 if (m) {
558 if (rp) {
559 /*
560 * XXX insure we don't get a copy by
561 * invalidating our protocol
562 */
563 unsigned short family = rp->rcb_proto.sp_family;
564 rp->rcb_proto.sp_family = 0;
565 rt_dispatch(m, info.rti_info[RTAX_DST]);
566 rp->rcb_proto.sp_family = family;
567 } else
568 rt_dispatch(m, info.rti_info[RTAX_DST]);
569 }
570 }
571 return (error);
572 #undef sa_equal
573 }
574
575 static void
576 rt_setmetrics(u_long which, const struct rt_metrics *in,
577 struct rt_metrics_lite *out)
578 {
579 #define metric(f, e) if (which & (f)) out->e = in->e;
580 /*
581 * Only these are stored in the routing entry since introduction
582 * of tcp hostcache. The rest is ignored.
583 */
584 metric(RTV_MTU, rmx_mtu);
585 metric(RTV_EXPIRE, rmx_expire);
586 #undef metric
587 }
588
589 static void
590 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
591 {
592 #define metric(e) out->e = in->e;
593 bzero(out, sizeof(*out));
594 metric(rmx_mtu);
595 metric(rmx_expire);
596 #undef metric
597 }
598
599 /*
600 * Extract the addresses of the passed sockaddrs.
601 * Do a little sanity checking so as to avoid bad memory references.
602 * This data is derived straight from userland.
603 */
604 static int
605 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
606 {
607 struct sockaddr *sa;
608 int i;
609
610 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
611 if ((rtinfo->rti_addrs & (1 << i)) == 0)
612 continue;
613 sa = (struct sockaddr *)cp;
614 /*
615 * It won't fit.
616 */
617 if (cp + sa->sa_len > cplim)
618 return (EINVAL);
619 /*
620 * there are no more.. quit now
621 * If there are more bits, they are in error.
622 * I've seen this. route(1) can evidently generate these.
623 * This causes kernel to core dump.
624 * for compatibility, If we see this, point to a safe address.
625 */
626 if (sa->sa_len == 0) {
627 rtinfo->rti_info[i] = &sa_zero;
628 return (0); /* should be EINVAL but for compat */
629 }
630 /* accept it */
631 rtinfo->rti_info[i] = sa;
632 cp += SA_SIZE(sa);
633 }
634 return (0);
635 }
636
637 static struct mbuf *
638 rt_msg1(int type, struct rt_addrinfo *rtinfo)
639 {
640 struct rt_msghdr *rtm;
641 struct mbuf *m;
642 int i;
643 struct sockaddr *sa;
644 int len, dlen;
645
646 switch (type) {
647
648 case RTM_DELADDR:
649 case RTM_NEWADDR:
650 len = sizeof(struct ifa_msghdr);
651 break;
652
653 case RTM_DELMADDR:
654 case RTM_NEWMADDR:
655 len = sizeof(struct ifma_msghdr);
656 break;
657
658 case RTM_IFINFO:
659 len = sizeof(struct if_msghdr);
660 break;
661
662 case RTM_IFANNOUNCE:
663 case RTM_IEEE80211:
664 len = sizeof(struct if_announcemsghdr);
665 break;
666
667 default:
668 len = sizeof(struct rt_msghdr);
669 }
670 if (len > MCLBYTES)
671 panic("rt_msg1");
672 m = m_gethdr(M_DONTWAIT, MT_DATA);
673 if (m && len > MHLEN) {
674 MCLGET(m, M_DONTWAIT);
675 if ((m->m_flags & M_EXT) == 0) {
676 m_free(m);
677 m = NULL;
678 }
679 }
680 if (m == NULL)
681 return (m);
682 m->m_pkthdr.len = m->m_len = len;
683 m->m_pkthdr.rcvif = NULL;
684 rtm = mtod(m, struct rt_msghdr *);
685 bzero((caddr_t)rtm, len);
686 for (i = 0; i < RTAX_MAX; i++) {
687 if ((sa = rtinfo->rti_info[i]) == NULL)
688 continue;
689 rtinfo->rti_addrs |= (1 << i);
690 dlen = SA_SIZE(sa);
691 m_copyback(m, len, dlen, (caddr_t)sa);
692 len += dlen;
693 }
694 if (m->m_pkthdr.len != len) {
695 m_freem(m);
696 return (NULL);
697 }
698 rtm->rtm_msglen = len;
699 rtm->rtm_version = RTM_VERSION;
700 rtm->rtm_type = type;
701 return (m);
702 }
703
704 static int
705 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
706 {
707 int i;
708 int len, dlen, second_time = 0;
709 caddr_t cp0;
710
711 rtinfo->rti_addrs = 0;
712 again:
713 switch (type) {
714
715 case RTM_DELADDR:
716 case RTM_NEWADDR:
717 len = sizeof(struct ifa_msghdr);
718 break;
719
720 case RTM_IFINFO:
721 len = sizeof(struct if_msghdr);
722 break;
723
724 case RTM_NEWMADDR:
725 len = sizeof(struct ifma_msghdr);
726 break;
727
728 default:
729 len = sizeof(struct rt_msghdr);
730 }
731 cp0 = cp;
732 if (cp0)
733 cp += len;
734 for (i = 0; i < RTAX_MAX; i++) {
735 struct sockaddr *sa;
736
737 if ((sa = rtinfo->rti_info[i]) == NULL)
738 continue;
739 rtinfo->rti_addrs |= (1 << i);
740 dlen = SA_SIZE(sa);
741 if (cp) {
742 bcopy((caddr_t)sa, cp, (unsigned)dlen);
743 cp += dlen;
744 }
745 len += dlen;
746 }
747 len = ALIGN(len);
748 if (cp == NULL && w != NULL && !second_time) {
749 struct walkarg *rw = w;
750
751 if (rw->w_req) {
752 if (rw->w_tmemsize < len) {
753 if (rw->w_tmem)
754 free(rw->w_tmem, M_RTABLE);
755 rw->w_tmem = (caddr_t)
756 malloc(len, M_RTABLE, M_NOWAIT);
757 if (rw->w_tmem)
758 rw->w_tmemsize = len;
759 }
760 if (rw->w_tmem) {
761 cp = rw->w_tmem;
762 second_time = 1;
763 goto again;
764 }
765 }
766 }
767 if (cp) {
768 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
769
770 rtm->rtm_version = RTM_VERSION;
771 rtm->rtm_type = type;
772 rtm->rtm_msglen = len;
773 }
774 return (len);
775 }
776
777 /*
778 * This routine is called to generate a message from the routing
779 * socket indicating that a redirect has occured, a routing lookup
780 * has failed, or that a protocol has detected timeouts to a particular
781 * destination.
782 */
783 void
784 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
785 {
786 struct rt_msghdr *rtm;
787 struct mbuf *m;
788 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
789
790 if (route_cb.any_count == 0)
791 return;
792 m = rt_msg1(type, rtinfo);
793 if (m == NULL)
794 return;
795 rtm = mtod(m, struct rt_msghdr *);
796 rtm->rtm_flags = RTF_DONE | flags;
797 rtm->rtm_errno = error;
798 rtm->rtm_addrs = rtinfo->rti_addrs;
799 rt_dispatch(m, sa);
800 }
801
802 /*
803 * This routine is called to generate a message from the routing
804 * socket indicating that the status of a network interface has changed.
805 */
806 void
807 rt_ifmsg(struct ifnet *ifp)
808 {
809 struct if_msghdr *ifm;
810 struct mbuf *m;
811 struct rt_addrinfo info;
812
813 if (route_cb.any_count == 0)
814 return;
815 bzero((caddr_t)&info, sizeof(info));
816 m = rt_msg1(RTM_IFINFO, &info);
817 if (m == NULL)
818 return;
819 ifm = mtod(m, struct if_msghdr *);
820 ifm->ifm_index = ifp->if_index;
821 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
822 ifm->ifm_data = ifp->if_data;
823 ifm->ifm_addrs = 0;
824 rt_dispatch(m, NULL);
825 }
826
827 /*
828 * This is called to generate messages from the routing socket
829 * indicating a network interface has had addresses associated with it.
830 * if we ever reverse the logic and replace messages TO the routing
831 * socket indicate a request to configure interfaces, then it will
832 * be unnecessary as the routing socket will automatically generate
833 * copies of it.
834 */
835 void
836 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
837 {
838 struct rt_addrinfo info;
839 struct sockaddr *sa = NULL;
840 int pass;
841 struct mbuf *m = NULL;
842 struct ifnet *ifp = ifa->ifa_ifp;
843
844 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
845 ("unexpected cmd %u", cmd));
846
847 if (route_cb.any_count == 0)
848 return;
849 for (pass = 1; pass < 3; pass++) {
850 bzero((caddr_t)&info, sizeof(info));
851 if ((cmd == RTM_ADD && pass == 1) ||
852 (cmd == RTM_DELETE && pass == 2)) {
853 struct ifa_msghdr *ifam;
854 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
855
856 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
857 info.rti_info[RTAX_IFP] =
858 ifaddr_byindex(ifp->if_index)->ifa_addr;
859 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
860 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
861 if ((m = rt_msg1(ncmd, &info)) == NULL)
862 continue;
863 ifam = mtod(m, struct ifa_msghdr *);
864 ifam->ifam_index = ifp->if_index;
865 ifam->ifam_metric = ifa->ifa_metric;
866 ifam->ifam_flags = ifa->ifa_flags;
867 ifam->ifam_addrs = info.rti_addrs;
868 }
869 if ((cmd == RTM_ADD && pass == 2) ||
870 (cmd == RTM_DELETE && pass == 1)) {
871 struct rt_msghdr *rtm;
872
873 if (rt == NULL)
874 continue;
875 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
876 info.rti_info[RTAX_DST] = sa = rt_key(rt);
877 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
878 if ((m = rt_msg1(cmd, &info)) == NULL)
879 continue;
880 rtm = mtod(m, struct rt_msghdr *);
881 rtm->rtm_index = ifp->if_index;
882 rtm->rtm_flags |= rt->rt_flags;
883 rtm->rtm_errno = error;
884 rtm->rtm_addrs = info.rti_addrs;
885 }
886 rt_dispatch(m, sa);
887 }
888 }
889
890 /*
891 * This is the analogue to the rt_newaddrmsg which performs the same
892 * function but for multicast group memberhips. This is easier since
893 * there is no route state to worry about.
894 */
895 void
896 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
897 {
898 struct rt_addrinfo info;
899 struct mbuf *m = NULL;
900 struct ifnet *ifp = ifma->ifma_ifp;
901 struct ifma_msghdr *ifmam;
902
903 if (route_cb.any_count == 0)
904 return;
905
906 bzero((caddr_t)&info, sizeof(info));
907 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
908 info.rti_info[RTAX_IFP] =
909 ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL;
910 /*
911 * If a link-layer address is present, present it as a ``gateway''
912 * (similarly to how ARP entries, e.g., are presented).
913 */
914 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
915 m = rt_msg1(cmd, &info);
916 if (m == NULL)
917 return;
918 ifmam = mtod(m, struct ifma_msghdr *);
919 ifmam->ifmam_index = ifp->if_index;
920 ifmam->ifmam_addrs = info.rti_addrs;
921 rt_dispatch(m, ifma->ifma_addr);
922 }
923
924 static struct mbuf *
925 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
926 struct rt_addrinfo *info)
927 {
928 struct if_announcemsghdr *ifan;
929 struct mbuf *m;
930
931 if (route_cb.any_count == 0)
932 return NULL;
933 bzero((caddr_t)info, sizeof(*info));
934 m = rt_msg1(type, info);
935 if (m != NULL) {
936 ifan = mtod(m, struct if_announcemsghdr *);
937 ifan->ifan_index = ifp->if_index;
938 strlcpy(ifan->ifan_name, ifp->if_xname,
939 sizeof(ifan->ifan_name));
940 ifan->ifan_what = what;
941 }
942 return m;
943 }
944
945 /*
946 * This is called to generate routing socket messages indicating
947 * IEEE80211 wireless events.
948 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
949 */
950 void
951 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
952 {
953 struct mbuf *m;
954 struct rt_addrinfo info;
955
956 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
957 if (m != NULL) {
958 /*
959 * Append the ieee80211 data. Try to stick it in the
960 * mbuf containing the ifannounce msg; otherwise allocate
961 * a new mbuf and append.
962 *
963 * NB: we assume m is a single mbuf.
964 */
965 if (data_len > M_TRAILINGSPACE(m)) {
966 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
967 if (n == NULL) {
968 m_freem(m);
969 return;
970 }
971 bcopy(data, mtod(n, void *), data_len);
972 n->m_len = data_len;
973 m->m_next = n;
974 } else if (data_len > 0) {
975 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
976 m->m_len += data_len;
977 }
978 if (m->m_flags & M_PKTHDR)
979 m->m_pkthdr.len += data_len;
980 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
981 rt_dispatch(m, NULL);
982 }
983 }
984
985 /*
986 * This is called to generate routing socket messages indicating
987 * network interface arrival and departure.
988 */
989 void
990 rt_ifannouncemsg(struct ifnet *ifp, int what)
991 {
992 struct mbuf *m;
993 struct rt_addrinfo info;
994
995 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
996 if (m != NULL)
997 rt_dispatch(m, NULL);
998 }
999
1000 static void
1001 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1002 {
1003 struct m_tag *tag;
1004
1005 /*
1006 * Preserve the family from the sockaddr, if any, in an m_tag for
1007 * use when injecting the mbuf into the routing socket buffer from
1008 * the netisr.
1009 */
1010 if (sa != NULL) {
1011 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1012 M_NOWAIT);
1013 if (tag == NULL) {
1014 m_freem(m);
1015 return;
1016 }
1017 *(unsigned short *)(tag + 1) = sa->sa_family;
1018 m_tag_prepend(m, tag);
1019 }
1020 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1021 }
1022
1023 /*
1024 * This is used in dumping the kernel table via sysctl().
1025 */
1026 static int
1027 sysctl_dumpentry(struct radix_node *rn, void *vw)
1028 {
1029 struct walkarg *w = vw;
1030 struct rtentry *rt = (struct rtentry *)rn;
1031 int error = 0, size;
1032 struct rt_addrinfo info;
1033
1034 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1035 return 0;
1036 bzero((caddr_t)&info, sizeof(info));
1037 info.rti_info[RTAX_DST] = rt_key(rt);
1038 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1039 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1040 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1041 if (rt->rt_ifp) {
1042 info.rti_info[RTAX_IFP] =
1043 ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr;
1044 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1045 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1046 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1047 }
1048 size = rt_msg2(RTM_GET, &info, NULL, w);
1049 if (w->w_req && w->w_tmem) {
1050 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1051
1052 rtm->rtm_flags = rt->rt_flags;
1053 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1054 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1055 rtm->rtm_index = rt->rt_ifp->if_index;
1056 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1057 rtm->rtm_addrs = info.rti_addrs;
1058 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1059 return (error);
1060 }
1061 return (error);
1062 }
1063
1064 static int
1065 sysctl_iflist(int af, struct walkarg *w)
1066 {
1067 struct ifnet *ifp;
1068 struct ifaddr *ifa;
1069 struct rt_addrinfo info;
1070 int len, error = 0;
1071
1072 bzero((caddr_t)&info, sizeof(info));
1073 IFNET_RLOCK();
1074 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1075 if (w->w_arg && w->w_arg != ifp->if_index)
1076 continue;
1077 ifa = ifaddr_byindex(ifp->if_index);
1078 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1079 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1080 info.rti_info[RTAX_IFP] = NULL;
1081 if (w->w_req && w->w_tmem) {
1082 struct if_msghdr *ifm;
1083
1084 ifm = (struct if_msghdr *)w->w_tmem;
1085 ifm->ifm_index = ifp->if_index;
1086 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1087 ifm->ifm_data = ifp->if_data;
1088 ifm->ifm_addrs = info.rti_addrs;
1089 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1090 if (error)
1091 goto done;
1092 }
1093 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1094 if (af && af != ifa->ifa_addr->sa_family)
1095 continue;
1096 if (jailed(curthread->td_ucred) &&
1097 prison_if(curthread->td_ucred, ifa->ifa_addr))
1098 continue;
1099 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1100 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1101 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1102 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1103 if (w->w_req && w->w_tmem) {
1104 struct ifa_msghdr *ifam;
1105
1106 ifam = (struct ifa_msghdr *)w->w_tmem;
1107 ifam->ifam_index = ifa->ifa_ifp->if_index;
1108 ifam->ifam_flags = ifa->ifa_flags;
1109 ifam->ifam_metric = ifa->ifa_metric;
1110 ifam->ifam_addrs = info.rti_addrs;
1111 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1112 if (error)
1113 goto done;
1114 }
1115 }
1116 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1117 info.rti_info[RTAX_BRD] = NULL;
1118 }
1119 done:
1120 IFNET_RUNLOCK();
1121 return (error);
1122 }
1123
1124 int
1125 sysctl_ifmalist(int af, struct walkarg *w)
1126 {
1127 struct ifnet *ifp;
1128 struct ifmultiaddr *ifma;
1129 struct rt_addrinfo info;
1130 int len, error = 0;
1131 struct ifaddr *ifa;
1132
1133 bzero((caddr_t)&info, sizeof(info));
1134 IFNET_RLOCK();
1135 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1136 if (w->w_arg && w->w_arg != ifp->if_index)
1137 continue;
1138 ifa = ifaddr_byindex(ifp->if_index);
1139 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1140 IF_ADDR_LOCK(ifp);
1141 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1142 if (af && af != ifma->ifma_addr->sa_family)
1143 continue;
1144 if (jailed(curproc->p_ucred) &&
1145 prison_if(curproc->p_ucred, ifma->ifma_addr))
1146 continue;
1147 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1148 info.rti_info[RTAX_GATEWAY] =
1149 (ifma->ifma_addr->sa_family != AF_LINK) ?
1150 ifma->ifma_lladdr : NULL;
1151 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1152 if (w->w_req && w->w_tmem) {
1153 struct ifma_msghdr *ifmam;
1154
1155 ifmam = (struct ifma_msghdr *)w->w_tmem;
1156 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1157 ifmam->ifmam_flags = 0;
1158 ifmam->ifmam_addrs = info.rti_addrs;
1159 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1160 if (error) {
1161 IF_ADDR_UNLOCK(ifp);
1162 goto done;
1163 }
1164 }
1165 }
1166 IF_ADDR_UNLOCK(ifp);
1167 }
1168 done:
1169 IFNET_RUNLOCK();
1170 return (error);
1171 }
1172
1173 static int
1174 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1175 {
1176 int *name = (int *)arg1;
1177 u_int namelen = arg2;
1178 struct radix_node_head *rnh;
1179 int i, lim, error = EINVAL;
1180 u_char af;
1181 struct walkarg w;
1182
1183 name ++;
1184 namelen--;
1185 if (req->newptr)
1186 return (EPERM);
1187 if (namelen != 3)
1188 return ((namelen < 3) ? EISDIR : ENOTDIR);
1189 af = name[0];
1190 if (af > AF_MAX)
1191 return (EINVAL);
1192 bzero(&w, sizeof(w));
1193 w.w_op = name[1];
1194 w.w_arg = name[2];
1195 w.w_req = req;
1196
1197 error = sysctl_wire_old_buffer(req, 0);
1198 if (error)
1199 return (error);
1200 switch (w.w_op) {
1201
1202 case NET_RT_DUMP:
1203 case NET_RT_FLAGS:
1204 if (af == 0) { /* dump all tables */
1205 i = 1;
1206 lim = AF_MAX;
1207 } else /* dump only one table */
1208 i = lim = af;
1209 for (error = 0; error == 0 && i <= lim; i++)
1210 if ((rnh = rt_tables[i]) != NULL) {
1211 RADIX_NODE_HEAD_LOCK(rnh);
1212 error = rnh->rnh_walktree(rnh,
1213 sysctl_dumpentry, &w);
1214 RADIX_NODE_HEAD_UNLOCK(rnh);
1215 } else if (af != 0)
1216 error = EAFNOSUPPORT;
1217 break;
1218
1219 case NET_RT_IFLIST:
1220 error = sysctl_iflist(af, &w);
1221 break;
1222
1223 case NET_RT_IFMALIST:
1224 error = sysctl_ifmalist(af, &w);
1225 break;
1226 }
1227 if (w.w_tmem)
1228 free(w.w_tmem, M_RTABLE);
1229 return (error);
1230 }
1231
1232 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1233
1234 /*
1235 * Definitions of protocols supported in the ROUTE domain.
1236 */
1237
1238 extern struct domain routedomain; /* or at least forward */
1239
1240 static struct protosw routesw[] = {
1241 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1242 0, route_output, raw_ctlinput, 0,
1243 0,
1244 raw_init, 0, 0, 0,
1245 &route_usrreqs
1246 }
1247 };
1248
1249 static struct domain routedomain =
1250 { PF_ROUTE, "route", 0, 0, 0,
1251 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1252
1253 DOMAIN_SET(route);
Cache object: feb147f5c39b4d94badddbd935e6fe5a
|