1 /* $OpenBSD: route.c,v 1.54 2005/06/08 06:43:07 henning Exp $ */
2 /* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1980, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)route.c 8.2 (Berkeley) 11/15/93
62 */
63
64 /*
65 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
66 *
67 * NRL grants permission for redistribution and use in source and binary
68 * forms, with or without modification, of the software and documentation
69 * created at NRL provided that the following conditions are met:
70 *
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
76 * 3. All advertising materials mentioning features or use of this software
77 * must display the following acknowledgements:
78 * This product includes software developed by the University of
79 * California, Berkeley and its contributors.
80 * This product includes software developed at the Information
81 * Technology Division, US Naval Research Laboratory.
82 * 4. Neither the name of the NRL nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
90 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 *
98 * The views and conclusions contained in the software and documentation
99 * are those of the authors and should not be interpreted as representing
100 * official policies, either expressed or implied, of the US Naval
101 * Research Laboratory (NRL).
102 */
103
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/proc.h>
107 #include <sys/mbuf.h>
108 #include <sys/socket.h>
109 #include <sys/socketvar.h>
110 #include <sys/domain.h>
111 #include <sys/protosw.h>
112 #include <sys/ioctl.h>
113 #include <sys/kernel.h>
114 #include <sys/queue.h>
115 #include <sys/pool.h>
116
117 #include <net/if.h>
118 #include <net/route.h>
119 #include <net/raw_cb.h>
120
121 #include <netinet/in.h>
122 #include <netinet/in_var.h>
123
124 #ifdef IPSEC
125 #include <netinet/ip_ipsp.h>
126
127 extern struct ifnet encif;
128 struct ifaddr * encap_findgwifa(struct sockaddr *);
129 #endif
130
131 #define SA(p) ((struct sockaddr *)(p))
132
133 struct route_cb route_cb;
134 struct rtstat rtstat;
135 struct radix_node_head *rt_tables[AF_MAX+1];
136
137 int rttrash; /* routes not in table but not freed */
138 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
139
140 struct pool rtentry_pool; /* pool for rtentry structures */
141 struct pool rttimer_pool; /* pool for rttimer structures */
142
143 int okaytoclone(u_int, int);
144 int rtdeletemsg(struct rtentry *);
145 int rtflushclone1(struct radix_node *, void *);
146 void rtflushclone(struct radix_node_head *, struct rtentry *);
147
148 #define LABELID_MAX 50000
149
150 struct rt_label {
151 TAILQ_ENTRY(rt_label) rtl_entry;
152 char rtl_name[RTLABEL_LEN];
153 u_int16_t rtl_id;
154 int rtl_ref;
155 };
156
157 TAILQ_HEAD(rt_labels, rt_label) rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels);
158
159 #ifdef IPSEC
160
161 struct ifaddr *
encap_findgwifa(struct sockaddr * gw)162 encap_findgwifa(struct sockaddr *gw)
163 {
164 return (TAILQ_FIRST(&encif.if_addrlist));
165 }
166
167 #endif
168
169 void
rtable_init(void ** table)170 rtable_init(void **table)
171 {
172 struct domain *dom;
173 for (dom = domains; dom != NULL; dom = dom->dom_next)
174 if (dom->dom_rtattach)
175 dom->dom_rtattach(&table[dom->dom_family],
176 dom->dom_rtoffset);
177 }
178
179 void
route_init()180 route_init()
181 {
182 pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
183 NULL);
184 rn_init(); /* initialize all zeroes, all ones, mask table */
185 rtable_init((void **)rt_tables);
186 }
187
188 void
rtalloc_noclone(struct route * ro,int howstrict)189 rtalloc_noclone(struct route *ro, int howstrict)
190 {
191 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
192 return; /* XXX */
193 ro->ro_rt = rtalloc2(&ro->ro_dst, 1, howstrict);
194 }
195
196 int
okaytoclone(u_int flags,int howstrict)197 okaytoclone(u_int flags, int howstrict)
198 {
199 if (howstrict == ALL_CLONING)
200 return (1);
201 if (howstrict == ONNET_CLONING && !(flags & RTF_GATEWAY))
202 return (1);
203 return (0);
204 }
205
206 struct rtentry *
rtalloc2(struct sockaddr * dst,int report,int howstrict)207 rtalloc2(struct sockaddr *dst, int report, int howstrict)
208 {
209 struct radix_node_head *rnh = rt_tables[dst->sa_family];
210 struct rtentry *rt;
211 struct radix_node *rn;
212 struct rtentry *newrt = 0;
213 struct rt_addrinfo info;
214 int s = splnet(), err = 0, msgtype = RTM_MISS;
215
216 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
217 ((rn->rn_flags & RNF_ROOT) == 0)) {
218 newrt = rt = (struct rtentry *)rn;
219 if (report && (rt->rt_flags & RTF_CLONING) &&
220 okaytoclone(rt->rt_flags, howstrict)) {
221 err = rtrequest(RTM_RESOLVE, dst, SA(0), SA(0), 0,
222 &newrt);
223 if (err) {
224 newrt = rt;
225 rt->rt_refcnt++;
226 goto miss;
227 }
228 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
229 msgtype = RTM_RESOLVE;
230 goto miss;
231 }
232 } else
233 rt->rt_refcnt++;
234 } else {
235 rtstat.rts_unreach++;
236 miss: if (report) {
237 bzero((caddr_t)&info, sizeof(info));
238 info.rti_info[RTAX_DST] = dst;
239 rt_missmsg(msgtype, &info, 0, err);
240 }
241 }
242 splx(s);
243 return (newrt);
244 }
245
246 /*
247 * Packet routing routines.
248 */
249 void
rtalloc(struct route * ro)250 rtalloc(struct route *ro)
251 {
252 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
253 return; /* XXX */
254 ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
255 }
256
257 struct rtentry *
rtalloc1(struct sockaddr * dst,int report)258 rtalloc1(struct sockaddr *dst, int report)
259 {
260 struct radix_node_head *rnh = rt_tables[dst->sa_family];
261 struct rtentry *rt;
262 struct radix_node *rn;
263 struct rtentry *newrt = 0;
264 struct rt_addrinfo info;
265 int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
266
267 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
268 ((rn->rn_flags & RNF_ROOT) == 0)) {
269 newrt = rt = (struct rtentry *)rn;
270 if (report && (rt->rt_flags & RTF_CLONING)) {
271 err = rtrequest(RTM_RESOLVE, dst, SA(NULL),
272 SA(NULL), 0, &newrt);
273 if (err) {
274 newrt = rt;
275 rt->rt_refcnt++;
276 goto miss;
277 }
278 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
279 msgtype = RTM_RESOLVE;
280 goto miss;
281 }
282 /* Inform listeners of the new route */
283 bzero(&info, sizeof(info));
284 info.rti_info[RTAX_DST] = rt_key(rt);
285 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
286 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
287 if (rt->rt_ifp != NULL) {
288 info.rti_info[RTAX_IFP] =
289 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
290 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
291 }
292 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
293 } else
294 rt->rt_refcnt++;
295 } else {
296 if (dst->sa_family != PF_KEY)
297 rtstat.rts_unreach++;
298 /*
299 * IP encapsulation does lots of lookups where we don't need nor want
300 * the RTM_MISSes that would be generated. It causes RTM_MISS storms
301 * sent upward breaking user-level routing queries.
302 */
303 miss: if (report && dst->sa_family != PF_KEY) {
304 bzero((caddr_t)&info, sizeof(info));
305 info.rti_info[RTAX_DST] = dst;
306 rt_missmsg(msgtype, &info, 0, err);
307 }
308 }
309 splx(s);
310 return (newrt);
311 }
312
313 void
rtfree(struct rtentry * rt)314 rtfree(struct rtentry *rt)
315 {
316 struct ifaddr *ifa;
317
318 if (rt == NULL)
319 panic("rtfree");
320 rt->rt_refcnt--;
321 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
322 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
323 panic ("rtfree 2");
324 rttrash--;
325 if (rt->rt_refcnt < 0) {
326 printf("rtfree: %p not freed (neg refs)\n", rt);
327 return;
328 }
329 rt_timer_remove_all(rt);
330 ifa = rt->rt_ifa;
331 if (ifa)
332 IFAFREE(ifa);
333 rtlabel_unref(rt->rt_labelid);
334 Free(rt_key(rt));
335 pool_put(&rtentry_pool, rt);
336 }
337 }
338
339 void
ifafree(struct ifaddr * ifa)340 ifafree(struct ifaddr *ifa)
341 {
342 if (ifa == NULL)
343 panic("ifafree");
344 if (ifa->ifa_refcnt == 0)
345 free(ifa, M_IFADDR);
346 else
347 ifa->ifa_refcnt--;
348 }
349
350 /*
351 * Force a routing table entry to the specified
352 * destination to go through the given gateway.
353 * Normally called as a result of a routing redirect
354 * message from the network layer.
355 *
356 * N.B.: must be called at splsoftnet
357 */
358 void
rtredirect(struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct sockaddr * src,struct rtentry ** rtp)359 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
360 struct sockaddr *netmask, int flags, struct sockaddr *src,
361 struct rtentry **rtp)
362 {
363 struct rtentry *rt;
364 int error = 0;
365 u_int32_t *stat = NULL;
366 struct rt_addrinfo info;
367 struct ifaddr *ifa;
368
369 splassert(IPL_SOFTNET);
370
371 /* verify the gateway is directly reachable */
372 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
373 error = ENETUNREACH;
374 goto out;
375 }
376 rt = rtalloc1(dst, 0);
377 /*
378 * If the redirect isn't from our current router for this dst,
379 * it's either old or wrong. If it redirects us to ourselves,
380 * we have a routing loop, perhaps as a result of an interface
381 * going down recently.
382 */
383 #define equal(a1, a2) \
384 ((a1)->sa_len == (a2)->sa_len && \
385 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
386 if (!(flags & RTF_DONE) && rt &&
387 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
388 error = EINVAL;
389 else if (ifa_ifwithaddr(gateway) != NULL)
390 error = EHOSTUNREACH;
391 if (error)
392 goto done;
393 /*
394 * Create a new entry if we just got back a wildcard entry
395 * or the lookup failed. This is necessary for hosts
396 * which use routing redirects generated by smart gateways
397 * to dynamically build the routing tables.
398 */
399 if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
400 goto create;
401 /*
402 * Don't listen to the redirect if it's
403 * for a route to an interface.
404 */
405 if (rt->rt_flags & RTF_GATEWAY) {
406 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
407 /*
408 * Changing from route to net => route to host.
409 * Create new route, rather than smashing route to net.
410 */
411 create:
412 if (rt)
413 rtfree(rt);
414 flags |= RTF_GATEWAY | RTF_DYNAMIC;
415 bzero(&info, sizeof(info));
416 info.rti_info[RTAX_DST] = dst;
417 info.rti_info[RTAX_GATEWAY] = gateway;
418 info.rti_info[RTAX_NETMASK] = netmask;
419 info.rti_ifa = ifa;
420 info.rti_flags = flags;
421 rt = NULL;
422 error = rtrequest1(RTM_ADD, &info, &rt);
423 if (rt != NULL)
424 flags = rt->rt_flags;
425 stat = &rtstat.rts_dynamic;
426 } else {
427 /*
428 * Smash the current notion of the gateway to
429 * this destination. Should check about netmask!!!
430 */
431 rt->rt_flags |= RTF_MODIFIED;
432 flags |= RTF_MODIFIED;
433 stat = &rtstat.rts_newgateway;
434 rt_setgate(rt, rt_key(rt), gateway);
435 }
436 } else
437 error = EHOSTUNREACH;
438 done:
439 if (rt) {
440 if (rtp && !error)
441 *rtp = rt;
442 else
443 rtfree(rt);
444 }
445 out:
446 if (error)
447 rtstat.rts_badredirect++;
448 else if (stat != NULL)
449 (*stat)++;
450 bzero((caddr_t)&info, sizeof(info));
451 info.rti_info[RTAX_DST] = dst;
452 info.rti_info[RTAX_GATEWAY] = gateway;
453 info.rti_info[RTAX_NETMASK] = netmask;
454 info.rti_info[RTAX_AUTHOR] = src;
455 rt_missmsg(RTM_REDIRECT, &info, flags, error);
456 }
457
458 /*
459 * Delete a route and generate a message
460 */
461 int
rtdeletemsg(struct rtentry * rt)462 rtdeletemsg(struct rtentry *rt)
463 {
464 int error;
465 struct rt_addrinfo info;
466
467 /*
468 * Request the new route so that the entry is not actually
469 * deleted. That will allow the information being reported to
470 * be accurate (and consistent with route_output()).
471 */
472 bzero((caddr_t)&info, sizeof(info));
473 info.rti_info[RTAX_DST] = rt_key(rt);
474 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
475 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
476 info.rti_flags = rt->rt_flags;
477 error = rtrequest1(RTM_DELETE, &info, &rt);
478
479 rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
480
481 /* Adjust the refcount */
482 if (error == 0 && rt->rt_refcnt <= 0) {
483 rt->rt_refcnt++;
484 rtfree(rt);
485 }
486 return (error);
487 }
488
489 int
rtflushclone1(struct radix_node * rn,void * arg)490 rtflushclone1(struct radix_node *rn, void *arg)
491 {
492 struct rtentry *rt, *parent;
493
494 rt = (struct rtentry *)rn;
495 parent = (struct rtentry *)arg;
496 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
497 rtdeletemsg(rt);
498 return 0;
499 }
500
501 void
rtflushclone(struct radix_node_head * rnh,struct rtentry * parent)502 rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
503 {
504
505 #ifdef DIAGNOSTIC
506 if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
507 panic("rtflushclone: called with a non-cloning route");
508 if (!rnh->rnh_walktree)
509 panic("rtflushclone: no rnh_walktree");
510 #endif
511 rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
512 }
513
514 /*
515 * Routing table ioctl interface.
516 */
517 int
rtioctl(u_long req,caddr_t data,struct proc * p)518 rtioctl(u_long req, caddr_t data, struct proc *p)
519 {
520 return (EOPNOTSUPP);
521 }
522
523 struct ifaddr *
ifa_ifwithroute(int flags,struct sockaddr * dst,struct sockaddr * gateway)524 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
525 {
526 struct ifaddr *ifa;
527
528 #ifdef IPSEC
529 /*
530 * If the destination is a PF_KEY address, we'll look
531 * for the existence of a encap interface number or address
532 * in the options list of the gateway. By default, we'll return
533 * enc0.
534 */
535 if (dst && (dst->sa_family == PF_KEY))
536 return encap_findgwifa(gateway);
537 #endif
538
539 if ((flags & RTF_GATEWAY) == 0) {
540 /*
541 * If we are adding a route to an interface,
542 * and the interface is a pt to pt link
543 * we should search for the destination
544 * as our clue to the interface. Otherwise
545 * we can use the local address.
546 */
547 ifa = NULL;
548 if (flags & RTF_HOST)
549 ifa = ifa_ifwithdstaddr(dst);
550 if (ifa == NULL)
551 ifa = ifa_ifwithaddr(gateway);
552 } else {
553 /*
554 * If we are adding a route to a remote net
555 * or host, the gateway may still be on the
556 * other end of a pt to pt link.
557 */
558 ifa = ifa_ifwithdstaddr(gateway);
559 }
560 if (ifa == NULL)
561 ifa = ifa_ifwithnet(gateway);
562 if (ifa == NULL) {
563 struct rtentry *rt = rtalloc1(gateway, 0);
564 if (rt == NULL)
565 return (NULL);
566 rt->rt_refcnt--;
567 /* The gateway must be local if the same address family. */
568 if ((rt->rt_flags & RTF_GATEWAY) &&
569 rt_key(rt)->sa_family == dst->sa_family)
570 return (0);
571 if ((ifa = rt->rt_ifa) == NULL)
572 return (NULL);
573 }
574 if (ifa->ifa_addr->sa_family != dst->sa_family) {
575 struct ifaddr *oifa = ifa;
576 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
577 if (ifa == NULL)
578 ifa = oifa;
579 }
580 return (ifa);
581 }
582
583 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
584
585 int
rtrequest(int req,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct rtentry ** ret_nrt)586 rtrequest(int req, struct sockaddr *dst, struct sockaddr *gateway,
587 struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
588 {
589 struct rt_addrinfo info;
590
591 bzero(&info, sizeof(info));
592 info.rti_flags = flags;
593 info.rti_info[RTAX_DST] = dst;
594 info.rti_info[RTAX_GATEWAY] = gateway;
595 info.rti_info[RTAX_NETMASK] = netmask;
596 return (rtrequest1(req, &info, ret_nrt));
597 }
598
599 /*
600 * These (questionable) definitions of apparent local variables apply
601 * to the next function. XXXXXX!!!
602 */
603 #define dst info->rti_info[RTAX_DST]
604 #define gateway info->rti_info[RTAX_GATEWAY]
605 #define netmask info->rti_info[RTAX_NETMASK]
606 #define ifaaddr info->rti_info[RTAX_IFA]
607 #define ifpaddr info->rti_info[RTAX_IFP]
608 #define flags info->rti_flags
609
610 int
rt_getifa(struct rt_addrinfo * info)611 rt_getifa(struct rt_addrinfo *info)
612 {
613 struct ifaddr *ifa;
614 int error = 0;
615
616 /*
617 * ifp may be specified by sockaddr_dl when protocol address
618 * is ambiguous
619 */
620 if (info->rti_ifp == NULL && ifpaddr != NULL
621 && ifpaddr->sa_family == AF_LINK &&
622 (ifa = ifa_ifwithnet((struct sockaddr *)ifpaddr)) != NULL)
623 info->rti_ifp = ifa->ifa_ifp;
624 if (info->rti_ifa == NULL && ifaaddr != NULL)
625 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
626 if (info->rti_ifa == NULL) {
627 struct sockaddr *sa;
628
629 sa = ifaaddr != NULL ? ifaaddr :
630 (gateway != NULL ? gateway : dst);
631 if (sa != NULL && info->rti_ifp != NULL)
632 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
633 else if (dst != NULL && gateway != NULL)
634 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
635 else if (sa != NULL)
636 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
637 }
638 if ((ifa = info->rti_ifa) != NULL) {
639 if (info->rti_ifp == NULL)
640 info->rti_ifp = ifa->ifa_ifp;
641 } else
642 error = ENETUNREACH;
643 return (error);
644 }
645
646 int
rtrequest1(int req,struct rt_addrinfo * info,struct rtentry ** ret_nrt)647 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
648 {
649 int s = splsoftnet(); int error = 0;
650 struct rtentry *rt, *crt;
651 struct radix_node *rn;
652 struct radix_node_head *rnh;
653 struct ifaddr *ifa;
654 struct sockaddr *ndst;
655 struct sockaddr_rtlabel *sa_rl;
656 #define senderr(x) { error = x ; goto bad; }
657
658 if ((rnh = rt_tables[dst->sa_family]) == 0)
659 senderr(EAFNOSUPPORT);
660 if (flags & RTF_HOST)
661 netmask = 0;
662 switch (req) {
663 case RTM_DELETE:
664 if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
665 senderr(ESRCH);
666 rt = (struct rtentry *)rn;
667 #ifndef SMALL_KERNEL
668 /*
669 * if we got multipath routes, we require users to specify
670 * a matching RTAX_GATEWAY.
671 */
672 if (rn_mpath_capable(rnh)) {
673 rt = rt_mpath_matchgate(rt, gateway);
674 rn = (struct radix_node *)rt;
675 if (!rt)
676 senderr(ESRCH);
677 }
678 #endif
679 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh, rn)) == NULL)
680 senderr(ESRCH);
681 rt = (struct rtentry *)rn;
682 if ((rt->rt_flags & RTF_CLONING) != 0) {
683 /* clean up any cloned children */
684 rtflushclone(rnh, rt);
685 }
686 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
687 panic ("rtrequest delete");
688 rt = (struct rtentry *)rn;
689 if (rt->rt_gwroute) {
690 rt = rt->rt_gwroute; RTFREE(rt);
691 (rt = (struct rtentry *)rn)->rt_gwroute = NULL;
692 }
693 if (rt->rt_parent) {
694 rt->rt_parent->rt_refcnt--;
695 rt->rt_parent = NULL;
696 }
697 rt->rt_flags &= ~RTF_UP;
698 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
699 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
700 rttrash++;
701 if (ret_nrt)
702 *ret_nrt = rt;
703 else if (rt->rt_refcnt <= 0) {
704 rt->rt_refcnt++;
705 rtfree(rt);
706 }
707 break;
708
709 case RTM_RESOLVE:
710 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
711 senderr(EINVAL);
712 if ((rt->rt_flags & RTF_CLONING) == 0)
713 senderr(EINVAL);
714 ifa = rt->rt_ifa;
715 flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
716 flags |= RTF_CLONED;
717 gateway = rt->rt_gateway;
718 if ((netmask = rt->rt_genmask) == NULL)
719 flags |= RTF_HOST;
720 goto makeroute;
721
722 case RTM_ADD:
723 if (info->rti_ifa == 0 && (error = rt_getifa(info)))
724 senderr(error);
725 ifa = info->rti_ifa;
726 makeroute:
727 rt = pool_get(&rtentry_pool, PR_NOWAIT);
728 if (rt == NULL)
729 senderr(ENOBUFS);
730 Bzero(rt, sizeof(*rt));
731 rt->rt_flags = RTF_UP | flags;
732 LIST_INIT(&rt->rt_timer);
733 if (rt_setgate(rt, dst, gateway)) {
734 pool_put(&rtentry_pool, rt);
735 senderr(ENOBUFS);
736 }
737 ndst = rt_key(rt);
738 if (netmask) {
739 rt_maskedcopy(dst, ndst, netmask);
740 } else
741 Bcopy(dst, ndst, dst->sa_len);
742 #ifndef SMALL_KERNEL
743 /* do not permit exactly the same dst/mask/gw pair */
744 if (rn_mpath_capable(rnh) &&
745 rt_mpath_conflict(rnh, rt, netmask)) {
746 if (rt->rt_gwroute)
747 rtfree(rt->rt_gwroute);
748 Free(rt_key(rt));
749 pool_put(&rtentry_pool, rt);
750 senderr(EEXIST);
751 }
752 #endif
753
754 if (info->rti_info[RTAX_LABEL] != NULL) {
755 sa_rl = (struct sockaddr_rtlabel *)
756 info->rti_info[RTAX_LABEL];
757 rt->rt_labelid = rtlabel_name2id(sa_rl->sr_label);
758 }
759
760 ifa->ifa_refcnt++;
761 rt->rt_ifa = ifa;
762 rt->rt_ifp = ifa->ifa_ifp;
763 if (req == RTM_RESOLVE) {
764 /*
765 * Copy both metrics and a back pointer to the cloned
766 * route's parent.
767 */
768 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
769 rt->rt_parent = *ret_nrt; /* Back ptr. to parent. */
770 rt->rt_parent->rt_refcnt++;
771 }
772 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
773 rnh, rt->rt_nodes);
774 if (rn == NULL && (crt = rtalloc1(ndst, 0)) != NULL) {
775 /* overwrite cloned route */
776 if ((crt->rt_flags & RTF_CLONED) != 0) {
777 rtdeletemsg(crt);
778 rn = rnh->rnh_addaddr((caddr_t)ndst,
779 (caddr_t)netmask, rnh, rt->rt_nodes);
780 }
781 RTFREE(crt);
782 }
783 if (rn == 0) {
784 IFAFREE(ifa);
785 if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
786 rtfree(rt->rt_parent);
787 if (rt->rt_gwroute)
788 rtfree(rt->rt_gwroute);
789 Free(rt_key(rt));
790 pool_put(&rtentry_pool, rt);
791 senderr(EEXIST);
792 }
793 if (ifa->ifa_rtrequest)
794 ifa->ifa_rtrequest(req, rt, info);
795 if (ret_nrt) {
796 *ret_nrt = rt;
797 rt->rt_refcnt++;
798 }
799 if ((rt->rt_flags & RTF_CLONING) != 0) {
800 /* clean up any cloned children */
801 rtflushclone(rnh, rt);
802 }
803 break;
804 }
805 bad:
806 splx(s);
807 return (error);
808 }
809
810 #undef dst
811 #undef gateway
812 #undef netmask
813 #undef ifaaddr
814 #undef ifpaddr
815 #undef flags
816
817 int
rt_setgate(struct rtentry * rt0,struct sockaddr * dst,struct sockaddr * gate)818 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
819 {
820 caddr_t new, old;
821 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
822 struct rtentry *rt = rt0;
823
824 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
825 old = (caddr_t)rt_key(rt);
826 R_Malloc(new, caddr_t, dlen + glen);
827 if (new == NULL)
828 return 1;
829 rt->rt_nodes->rn_key = new;
830 } else {
831 new = rt->rt_nodes->rn_key;
832 old = NULL;
833 }
834 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
835 if (old) {
836 Bcopy(dst, new, dlen);
837 Free(old);
838 }
839 if (rt->rt_gwroute != NULL) {
840 rt = rt->rt_gwroute;
841 RTFREE(rt);
842 rt = rt0;
843 rt->rt_gwroute = NULL;
844 }
845 if (rt->rt_flags & RTF_GATEWAY) {
846 rt->rt_gwroute = rtalloc1(gate, 1);
847 /*
848 * If we switched gateways, grab the MTU from the new
849 * gateway route if the current MTU is 0 or greater
850 * than the MTU of gateway.
851 * Note that, if the MTU of gateway is 0, we will reset the
852 * MTU of the route to run PMTUD again from scratch. XXX
853 */
854 if (rt->rt_gwroute && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
855 rt->rt_rmx.rmx_mtu &&
856 rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
857 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
858 }
859 }
860 return (0);
861 }
862
863 void
rt_maskedcopy(struct sockaddr * src,struct sockaddr * dst,struct sockaddr * netmask)864 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst,
865 struct sockaddr *netmask)
866 {
867 u_char *cp1 = (u_char *)src;
868 u_char *cp2 = (u_char *)dst;
869 u_char *cp3 = (u_char *)netmask;
870 u_char *cplim = cp2 + *cp3;
871 u_char *cplim2 = cp2 + *cp1;
872
873 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
874 cp3 += 2;
875 if (cplim > cplim2)
876 cplim = cplim2;
877 while (cp2 < cplim)
878 *cp2++ = *cp1++ & *cp3++;
879 if (cp2 < cplim2)
880 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
881 }
882
883 /*
884 * Set up a routing table entry, normally
885 * for an interface.
886 */
887 int
rtinit(struct ifaddr * ifa,int cmd,int flags)888 rtinit(struct ifaddr *ifa, int cmd, int flags)
889 {
890 struct rtentry *rt;
891 struct sockaddr *dst;
892 struct sockaddr *deldst;
893 struct mbuf *m = NULL;
894 struct rtentry *nrt = NULL;
895 int error;
896 struct rt_addrinfo info;
897
898 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
899 if (cmd == RTM_DELETE) {
900 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
901 m = m_get(M_DONTWAIT, MT_SONAME);
902 if (m == NULL)
903 return (ENOBUFS);
904 deldst = mtod(m, struct sockaddr *);
905 rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
906 dst = deldst;
907 }
908 if ((rt = rtalloc1(dst, 0)) != NULL) {
909 rt->rt_refcnt--;
910 if (rt->rt_ifa != ifa) {
911 if (m != NULL)
912 (void) m_free(m);
913 return (flags & RTF_HOST ? EHOSTUNREACH
914 : ENETUNREACH);
915 }
916 }
917 }
918 bzero(&info, sizeof(info));
919 info.rti_ifa = ifa;
920 info.rti_flags = flags | ifa->ifa_flags;
921 info.rti_info[RTAX_DST] = dst;
922 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
923 /*
924 * XXX here, it seems that we are assuming that ifa_netmask is NULL
925 * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
926 * variable) when RTF_HOST is 1. still not sure if i can safely
927 * change it to meet bsdi4 behavior.
928 */
929 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
930 error = rtrequest1(cmd, &info, &nrt);
931 if (cmd == RTM_DELETE && error == 0 && (rt = nrt) != NULL) {
932 rt_newaddrmsg(cmd, ifa, error, nrt);
933 if (rt->rt_refcnt <= 0) {
934 rt->rt_refcnt++;
935 rtfree(rt);
936 }
937 }
938 if (cmd == RTM_ADD && error == 0 && (rt = nrt) != NULL) {
939 rt->rt_refcnt--;
940 if (rt->rt_ifa != ifa) {
941 printf("rtinit: wrong ifa (%p) was (%p)\n",
942 ifa, rt->rt_ifa);
943 if (rt->rt_ifa->ifa_rtrequest)
944 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
945 IFAFREE(rt->rt_ifa);
946 rt->rt_ifa = ifa;
947 rt->rt_ifp = ifa->ifa_ifp;
948 ifa->ifa_refcnt++;
949 if (ifa->ifa_rtrequest)
950 ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
951 }
952 rt_newaddrmsg(cmd, ifa, error, nrt);
953 }
954 return (error);
955 }
956
957 /*
958 * Route timer routines. These routes allow functions to be called
959 * for various routes at any time. This is useful in supporting
960 * path MTU discovery and redirect route deletion.
961 *
962 * This is similar to some BSDI internal functions, but it provides
963 * for multiple queues for efficiency's sake...
964 */
965
966 LIST_HEAD(, rttimer_queue) rttimer_queue_head;
967 static int rt_init_done = 0;
968
969 #define RTTIMER_CALLOUT(r) { \
970 if (r->rtt_func != NULL) { \
971 (*r->rtt_func)(r->rtt_rt, r); \
972 } else { \
973 rtrequest((int) RTM_DELETE, \
974 (struct sockaddr *)rt_key(r->rtt_rt), \
975 0, 0, 0, 0); \
976 } \
977 }
978
979 /*
980 * Some subtle order problems with domain initialization mean that
981 * we cannot count on this being run from rt_init before various
982 * protocol initializations are done. Therefore, we make sure
983 * that this is run when the first queue is added...
984 */
985
986 void
rt_timer_init()987 rt_timer_init()
988 {
989 static struct timeout rt_timer_timeout;
990
991 KASSERT(rt_init_done == 0);
992
993 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
994 NULL);
995
996 LIST_INIT(&rttimer_queue_head);
997 timeout_set(&rt_timer_timeout, rt_timer_timer, &rt_timer_timeout);
998 timeout_add(&rt_timer_timeout, hz); /* every second */
999 rt_init_done = 1;
1000 }
1001
1002 struct rttimer_queue *
rt_timer_queue_create(u_int timeout)1003 rt_timer_queue_create(u_int timeout)
1004 {
1005 struct rttimer_queue *rtq;
1006
1007 if (rt_init_done == 0)
1008 rt_timer_init();
1009
1010 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
1011 if (rtq == NULL)
1012 return (NULL);
1013 Bzero(rtq, sizeof *rtq);
1014
1015 rtq->rtq_timeout = timeout;
1016 rtq->rtq_count = 0;
1017 TAILQ_INIT(&rtq->rtq_head);
1018 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
1019
1020 return (rtq);
1021 }
1022
1023 void
rt_timer_queue_change(struct rttimer_queue * rtq,long timeout)1024 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
1025 {
1026
1027 rtq->rtq_timeout = timeout;
1028 }
1029
1030 void
rt_timer_queue_destroy(struct rttimer_queue * rtq,int destroy)1031 rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
1032 {
1033 struct rttimer *r;
1034
1035 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
1036 LIST_REMOVE(r, rtt_link);
1037 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1038 if (destroy)
1039 RTTIMER_CALLOUT(r);
1040 pool_put(&rttimer_pool, r);
1041 if (rtq->rtq_count > 0)
1042 rtq->rtq_count--;
1043 else
1044 printf("rt_timer_queue_destroy: rtq_count reached 0\n");
1045 }
1046
1047 LIST_REMOVE(rtq, rtq_link);
1048
1049 /*
1050 * Caller is responsible for freeing the rttimer_queue structure.
1051 */
1052 }
1053
1054 unsigned long
rt_timer_count(struct rttimer_queue * rtq)1055 rt_timer_count(struct rttimer_queue *rtq)
1056 {
1057
1058 return (rtq->rtq_count);
1059 }
1060
1061 void
rt_timer_remove_all(struct rtentry * rt)1062 rt_timer_remove_all(struct rtentry *rt)
1063 {
1064 struct rttimer *r;
1065
1066 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1067 LIST_REMOVE(r, rtt_link);
1068 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1069 if (r->rtt_queue->rtq_count > 0)
1070 r->rtt_queue->rtq_count--;
1071 else
1072 printf("rt_timer_remove_all: rtq_count reached 0\n");
1073 pool_put(&rttimer_pool, r);
1074 }
1075 }
1076
1077 int
rt_timer_add(struct rtentry * rt,void (* func)(struct rtentry *,struct rttimer *),struct rttimer_queue * queue)1078 rt_timer_add(struct rtentry *rt, void (*func)(struct rtentry *,
1079 struct rttimer *), struct rttimer_queue *queue)
1080 {
1081 struct rttimer *r;
1082 time_t current_time;
1083
1084 current_time = mono_time.tv_sec;
1085
1086 /*
1087 * If there's already a timer with this action, destroy it before
1088 * we add a new one.
1089 */
1090 for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
1091 r = LIST_NEXT(r, rtt_link)) {
1092 if (r->rtt_func == func) {
1093 LIST_REMOVE(r, rtt_link);
1094 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1095 if (r->rtt_queue->rtq_count > 0)
1096 r->rtt_queue->rtq_count--;
1097 else
1098 printf("rt_timer_add: rtq_count reached 0\n");
1099 pool_put(&rttimer_pool, r);
1100 break; /* only one per list, so we can quit... */
1101 }
1102 }
1103
1104 r = pool_get(&rttimer_pool, PR_NOWAIT);
1105 if (r == NULL)
1106 return (ENOBUFS);
1107 Bzero(r, sizeof(*r));
1108
1109 r->rtt_rt = rt;
1110 r->rtt_time = current_time;
1111 r->rtt_func = func;
1112 r->rtt_queue = queue;
1113 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1114 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1115 r->rtt_queue->rtq_count++;
1116
1117 return (0);
1118 }
1119
1120 /* ARGSUSED */
1121 void
rt_timer_timer(void * arg)1122 rt_timer_timer(void *arg)
1123 {
1124 struct timeout *to = (struct timeout *)arg;
1125 struct rttimer_queue *rtq;
1126 struct rttimer *r;
1127 time_t current_time;
1128 int s;
1129
1130 current_time = mono_time.tv_sec;
1131
1132 s = splsoftnet();
1133 for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
1134 rtq = LIST_NEXT(rtq, rtq_link)) {
1135 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1136 (r->rtt_time + rtq->rtq_timeout) < current_time) {
1137 LIST_REMOVE(r, rtt_link);
1138 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1139 RTTIMER_CALLOUT(r);
1140 pool_put(&rttimer_pool, r);
1141 if (rtq->rtq_count > 0)
1142 rtq->rtq_count--;
1143 else
1144 printf("rt_timer_timer: rtq_count reached 0\n");
1145 }
1146 }
1147 splx(s);
1148
1149 timeout_add(to, hz); /* every second */
1150 }
1151
1152 u_int16_t
rtlabel_name2id(char * name)1153 rtlabel_name2id(char *name)
1154 {
1155 struct rt_label *label, *p = NULL;
1156 u_int16_t new_id = 1;
1157
1158 if (!name[0])
1159 return (0);
1160
1161 TAILQ_FOREACH(label, &rt_labels, rtl_entry)
1162 if (strcmp(name, label->rtl_name) == 0) {
1163 label->rtl_ref++;
1164 return (label->rtl_id);
1165 }
1166
1167 /*
1168 * to avoid fragmentation, we do a linear search from the beginning
1169 * and take the first free slot we find. if there is none or the list
1170 * is empty, append a new entry at the end.
1171 */
1172
1173 if (!TAILQ_EMPTY(&rt_labels))
1174 for (p = TAILQ_FIRST(&rt_labels); p != NULL &&
1175 p->rtl_id == new_id; p = TAILQ_NEXT(p, rtl_entry))
1176 new_id = p->rtl_id + 1;
1177
1178 if (new_id > LABELID_MAX)
1179 return (0);
1180
1181 label = (struct rt_label *)malloc(sizeof(struct rt_label),
1182 M_TEMP, M_NOWAIT);
1183 if (label == NULL)
1184 return (0);
1185 bzero(label, sizeof(struct rt_label));
1186 strlcpy(label->rtl_name, name, sizeof(label->rtl_name));
1187 label->rtl_id = new_id;
1188 label->rtl_ref++;
1189
1190 if (p != NULL) /* insert new entry before p */
1191 TAILQ_INSERT_BEFORE(p, label, rtl_entry);
1192 else /* either list empty or no free slot in between */
1193 TAILQ_INSERT_TAIL(&rt_labels, label, rtl_entry);
1194
1195 return (label->rtl_id);
1196 }
1197
1198 const char *
rtlabel_id2name(u_int16_t id)1199 rtlabel_id2name(u_int16_t id)
1200 {
1201 struct rt_label *label;
1202
1203 TAILQ_FOREACH(label, &rt_labels, rtl_entry)
1204 if (label->rtl_id == id)
1205 return (label->rtl_name);
1206
1207 return (NULL);
1208 }
1209
1210 void
rtlabel_unref(u_int16_t id)1211 rtlabel_unref(u_int16_t id)
1212 {
1213 struct rt_label *p, *next;
1214
1215 if (id == 0)
1216 return;
1217
1218 for (p = TAILQ_FIRST(&rt_labels); p != NULL; p = next) {
1219 next = TAILQ_NEXT(p, rtl_entry);
1220 if (id == p->rtl_id) {
1221 if (--p->rtl_ref == 0) {
1222 TAILQ_REMOVE(&rt_labels, p, rtl_entry);
1223 free(p, M_TEMP);
1224 }
1225 break;
1226 }
1227 }
1228 }
1229