]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/route/route_ctl.c
MFH
[FreeBSD/FreeBSD.git] / sys / net / route / route_ctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_mpath.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/rmlock.h>
44
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/vnet.h>
49 #include <net/route.h>
50 #include <net/route/route_ctl.h>
51 #include <net/route/route_var.h>
52 #include <net/route/nhop_utils.h>
53 #include <net/route/nhop.h>
54 #include <net/route/nhop_var.h>
55 #include <net/route/shared.h>
56 #include <netinet/in.h>
57
58 #ifdef RADIX_MPATH
59 #include <net/radix_mpath.h>
60 #endif
61
62 #include <vm/uma.h>
63
64
65 /*
66  * This file contains control plane routing tables functions.
67  *
68  * All functions assumes they are called in net epoch.
69  */
70
71 struct rib_subscription {
72         CK_STAILQ_ENTRY(rib_subscription)       next;
73         rib_subscription_cb_t                   *func;
74         void                                    *arg;
75         enum rib_subscription_type              type;
76         struct epoch_context                    epoch_ctx;
77 };
78
79 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
80     struct rib_cmd_info *rc);
81 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
82     struct rib_cmd_info *rc);
83 static int change_route(struct rib_head *, struct rt_addrinfo *,
84     struct rib_cmd_info *rc);
85 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
86     struct rib_cmd_info *rc);
87
88 static void destroy_subscription_epoch(epoch_context_t ctx);
89
90 static struct rib_head *
91 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
92 {
93         struct rib_head *rnh;
94         struct sockaddr *dst;
95
96         KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
97
98         dst = info->rti_info[RTAX_DST];
99         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
100
101         return (rnh);
102 }
103
104 /*
105  * Adds route defined by @info into the kernel table specified by @fibnum and
106  * sa_family in @info->rti_info[RTAX_DST].
107  *
108  * Returns 0 on success and fills in operation metadata into @rc.
109  */
110 int
111 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
112     struct rib_cmd_info *rc)
113 {
114         struct rib_head *rnh;
115
116         NET_EPOCH_ASSERT();
117
118         rnh = get_rnh(fibnum, info);
119         if (rnh == NULL)
120                 return (EAFNOSUPPORT);
121
122         /*
123          * Check consistency between RTF_HOST flag and netmask
124          * existence.
125          */
126         if (info->rti_flags & RTF_HOST)
127                 info->rti_info[RTAX_NETMASK] = NULL;
128         else if (info->rti_info[RTAX_NETMASK] == NULL)
129                 return (EINVAL);
130
131         bzero(rc, sizeof(struct rib_cmd_info));
132         rc->rc_cmd = RTM_ADD;
133
134         return (add_route(rnh, info, rc));
135 }
136
137 static int
138 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
139     struct rib_cmd_info *rc)
140 {
141         struct sockaddr *dst, *ndst, *gateway, *netmask;
142         struct rtentry *rt, *rt_old;
143         struct nhop_object *nh;
144         struct radix_node *rn;
145         struct ifaddr *ifa;
146         int error, flags;
147
148         dst = info->rti_info[RTAX_DST];
149         gateway = info->rti_info[RTAX_GATEWAY];
150         netmask = info->rti_info[RTAX_NETMASK];
151         flags = info->rti_flags;
152
153         if ((flags & RTF_GATEWAY) && !gateway)
154                 return (EINVAL);
155         if (dst && gateway && (dst->sa_family != gateway->sa_family) && 
156             (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
157                 return (EINVAL);
158
159         if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
160                 return (EINVAL);
161
162         if (info->rti_ifa == NULL) {
163                 error = rt_getifa_fib(info, rnh->rib_fibnum);
164                 if (error)
165                         return (error);
166         } else {
167                 ifa_ref(info->rti_ifa);
168         }
169
170         error = nhop_create_from_info(rnh, info, &nh);
171         if (error != 0) {
172                 ifa_free(info->rti_ifa);
173                 return (error);
174         }
175
176         rt = uma_zalloc(V_rtzone, M_NOWAIT);
177         if (rt == NULL) {
178                 ifa_free(info->rti_ifa);
179                 nhop_free(nh);
180                 return (ENOBUFS);
181         }
182         rt->rt_flags = RTF_UP | flags;
183         rt->rt_nhop = nh;
184
185         /* Fill in dst */
186         memcpy(&rt->rt_dst, dst, dst->sa_len);
187         rt_key(rt) = &rt->rt_dst;
188
189         /*
190          * point to the (possibly newly malloc'd) dest address.
191          */
192         ndst = (struct sockaddr *)rt_key(rt);
193
194         /*
195          * make sure it contains the value we want (masked if needed).
196          */
197         if (netmask) {
198                 rt_maskedcopy(dst, ndst, netmask);
199         } else
200                 bcopy(dst, ndst, dst->sa_len);
201
202         /*
203          * We use the ifa reference returned by rt_getifa_fib().
204          * This moved from below so that rnh->rnh_addaddr() can
205          * examine the ifa and  ifa->ifa_ifp if it so desires.
206          */
207         ifa = info->rti_ifa;
208         rt->rt_weight = 1;
209
210         rt_setmetrics(info, rt);
211         rt_old = NULL;
212
213         RIB_WLOCK(rnh);
214         RT_LOCK(rt);
215 #ifdef RADIX_MPATH
216         /* do not permit exactly the same dst/mask/gw pair */
217         if (rt_mpath_capable(rnh) &&
218                 rt_mpath_conflict(rnh, rt, netmask)) {
219                 RIB_WUNLOCK(rnh);
220
221                 nhop_free(nh);
222                 uma_zfree(V_rtzone, rt);
223                 return (EEXIST);
224         }
225 #endif
226
227         rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
228
229         if (rn != NULL) {
230                 /* Most common usecase */
231                 if (rt->rt_expire > 0)
232                         tmproutes_update(rnh, rt);
233
234                 /* Finalize notification */
235                 rnh->rnh_gen++;
236
237                 rc->rc_rt = RNTORT(rn);
238                 rc->rc_nh_new = nh;
239
240                 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
241         } else if ((info->rti_flags & RTF_PINNED) != 0) {
242
243                 /*
244                  * Force removal and re-try addition
245                  * TODO: better multipath&pinned support
246                  */
247                 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
248                 info->rti_info[RTAX_DST] = ndst;
249                 /* Do not delete existing PINNED(interface) routes */
250                 info->rti_flags &= ~RTF_PINNED;
251                 rt_old = rt_unlinkrte(rnh, info, &error);
252                 info->rti_flags |= RTF_PINNED;
253                 info->rti_info[RTAX_DST] = info_dst;
254                 if (rt_old != NULL) {
255                         rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
256                             rt->rt_nodes);
257
258                         /* Finalize notification */
259                         rnh->rnh_gen++;
260
261                         if (rn != NULL) {
262                                 rc->rc_cmd = RTM_CHANGE;
263                                 rc->rc_rt = RNTORT(rn);
264                                 rc->rc_nh_old = rt_old->rt_nhop;
265                                 rc->rc_nh_new = nh;
266                         } else {
267                                 rc->rc_cmd = RTM_DELETE;
268                                 rc->rc_rt = RNTORT(rn);
269                                 rc->rc_nh_old = rt_old->rt_nhop;
270                                 rc->rc_nh_new = nh;
271                         }
272                         rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
273                 }
274         }
275         RIB_WUNLOCK(rnh);
276
277         if ((rn != NULL) || (rt_old != NULL))
278                 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
279
280         if (rt_old != NULL)
281                 rtfree(rt_old);
282
283         /*
284          * If it still failed to go into the tree,
285          * then un-make it (this should be a function)
286          */
287         if (rn == NULL) {
288                 nhop_free(nh);
289                 uma_zfree(V_rtzone, rt);
290                 return (EEXIST);
291         }
292
293         RT_UNLOCK(rt);
294
295         return (0);
296 }
297
298
299 /*
300  * Removes route defined by @info from the kernel table specified by @fibnum and
301  * sa_family in @info->rti_info[RTAX_DST].
302  *
303  * Returns 0 on success and fills in operation metadata into @rc.
304  */
305 int
306 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
307 {
308         struct rib_head *rnh;
309
310         NET_EPOCH_ASSERT();
311
312         rnh = get_rnh(fibnum, info);
313         if (rnh == NULL)
314                 return (EAFNOSUPPORT);
315
316         bzero(rc, sizeof(struct rib_cmd_info));
317         rc->rc_cmd = RTM_DELETE;
318
319         return (del_route(rnh, info, rc));
320 }
321
322 /*
323  * Conditionally unlinks rtentry matching data inside @info from @rnh.
324  * Returns unlinked, locked and referenced @rtentry on success,
325  * Returns NULL and sets @perror to:
326  * ESRCH - if prefix was not found,
327  * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
328  * ENOENT - if supplied filter function returned 0 (not matched).
329  */
330 struct rtentry *
331 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
332 {
333         struct sockaddr *dst, *netmask;
334         struct rtentry *rt;
335         struct radix_node *rn;
336
337         dst = info->rti_info[RTAX_DST];
338         netmask = info->rti_info[RTAX_NETMASK];
339
340         rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
341         if (rt == NULL) {
342                 *perror = ESRCH;
343                 return (NULL);
344         }
345
346         if ((info->rti_flags & RTF_PINNED) == 0) {
347                 /* Check if target route can be deleted */
348                 if (rt->rt_flags & RTF_PINNED) {
349                         *perror = EADDRINUSE;
350                         return (NULL);
351                 }
352         }
353
354         if (info->rti_filter != NULL) {
355                 if (info->rti_filter(rt, rt->rt_nhop, info->rti_filterdata)==0){
356                         /* Not matched */
357                         *perror = ENOENT;
358                         return (NULL);
359                 }
360
361                 /*
362                  * Filter function requested rte deletion.
363                  * Ease the caller work by filling in remaining info
364                  * from that particular entry.
365                  */
366                 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
367         }
368
369         /*
370          * Remove the item from the tree and return it.
371          * Complain if it is not there and do no more processing.
372          */
373         *perror = ESRCH;
374 #ifdef RADIX_MPATH
375         if (rt_mpath_capable(rnh))
376                 rn = rt_mpath_unlink(rnh, info, rt, perror);
377         else
378 #endif
379         rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
380         if (rn == NULL)
381                 return (NULL);
382
383         if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
384                 panic ("rtrequest delete");
385
386         rt = RNTORT(rn);
387         RT_LOCK(rt);
388         rt->rt_flags &= ~RTF_UP;
389
390         *perror = 0;
391
392         return (rt);
393 }
394
395 static int
396 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
397     struct rib_cmd_info *rc)
398 {
399         struct sockaddr *dst, *netmask;
400         struct sockaddr_storage mdst;
401         struct rtentry *rt;
402         int error;
403
404         dst = info->rti_info[RTAX_DST];
405         netmask = info->rti_info[RTAX_NETMASK];
406
407         if (netmask) {
408                 if (dst->sa_len > sizeof(mdst))
409                         return (EINVAL);
410                 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
411                 dst = (struct sockaddr *)&mdst;
412         }
413
414         RIB_WLOCK(rnh);
415         rt = rt_unlinkrte(rnh, info, &error);
416         if (rt != NULL) {
417                 /* Finalize notification */
418                 rnh->rnh_gen++;
419                 rc->rc_rt = rt;
420                 rc->rc_nh_old = rt->rt_nhop;
421                 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
422         }
423         RIB_WUNLOCK(rnh);
424         if (error != 0)
425                 return (error);
426
427         rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
428
429         /*
430          * If the caller wants it, then it can have it,
431          * the entry will be deleted after the end of the current epoch.
432          */
433         rtfree(rt);
434
435         return (0);
436 }
437
438 int
439 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
440     struct rib_cmd_info *rc)
441 {
442         struct rib_head *rnh;
443
444         NET_EPOCH_ASSERT();
445
446         rnh = get_rnh(fibnum, info);
447         if (rnh == NULL)
448                 return (EAFNOSUPPORT);
449
450         bzero(rc, sizeof(struct rib_cmd_info));
451         rc->rc_cmd = RTM_CHANGE;
452
453         return (change_route(rnh, info, rc));
454 }
455
456 static int
457 change_route_one(struct rib_head *rnh, struct rt_addrinfo *info,
458     struct rib_cmd_info *rc)
459 {
460         RIB_RLOCK_TRACKER;
461         struct rtentry *rt = NULL;
462         int error = 0;
463         int free_ifa = 0;
464         struct nhop_object *nh, *nh_orig;
465
466         RIB_RLOCK(rnh);
467         rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
468             info->rti_info[RTAX_NETMASK], &rnh->head);
469
470         if (rt == NULL) {
471                 RIB_RUNLOCK(rnh);
472                 return (ESRCH);
473         }
474
475 #ifdef RADIX_MPATH
476         /*
477          * If we got multipath routes,
478          * we require users to specify a matching RTAX_GATEWAY.
479          */
480         if (rt_mpath_capable(rnh)) {
481                 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
482                 if (rt == NULL) {
483                         RIB_RUNLOCK(rnh);
484                         return (ESRCH);
485                 }
486         }
487 #endif
488         nh_orig = rt->rt_nhop;
489
490         RIB_RUNLOCK(rnh);
491
492         rt = NULL;
493         nh = NULL;
494
495         /*
496          * New gateway could require new ifaddr, ifp;
497          * flags may also be different; ifp may be specified
498          * by ll sockaddr when protocol address is ambiguous
499          */
500         if (((nh_orig->nh_flags & NHF_GATEWAY) &&
501             info->rti_info[RTAX_GATEWAY] != NULL) ||
502             info->rti_info[RTAX_IFP] != NULL ||
503             (info->rti_info[RTAX_IFA] != NULL &&
504              !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
505                 error = rt_getifa_fib(info, rnh->rib_fibnum);
506                 if (info->rti_ifa != NULL)
507                         free_ifa = 1;
508
509                 if (error != 0) {
510                         if (free_ifa) {
511                                 ifa_free(info->rti_ifa);
512                                 info->rti_ifa = NULL;
513                         }
514
515                         return (error);
516                 }
517         }
518
519         error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
520         if (free_ifa) {
521                 ifa_free(info->rti_ifa);
522                 info->rti_ifa = NULL;
523         }
524         if (error != 0)
525                 return (error);
526
527         RIB_WLOCK(rnh);
528
529         /* Lookup rtentry once again and check if nexthop is still the same */
530         rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
531             info->rti_info[RTAX_NETMASK], &rnh->head);
532
533         if (rt == NULL) {
534                 RIB_WUNLOCK(rnh);
535                 nhop_free(nh);
536                 return (ESRCH);
537         }
538
539         if (rt->rt_nhop != nh_orig) {
540                 RIB_WUNLOCK(rnh);
541                 nhop_free(nh);
542                 return (EAGAIN);
543         }
544
545         /* Proceed with the update */
546         RT_LOCK(rt);
547
548         /* Provide notification to the protocols.*/
549         rt->rt_nhop = nh;
550         rt_setmetrics(info, rt);
551
552         /* Finalize notification */
553         rc->rc_rt = rt;
554         rc->rc_nh_old = nh_orig;
555         rc->rc_nh_new = rt->rt_nhop;
556
557         RT_UNLOCK(rt);
558
559         /* Update generation id to reflect rtable change */
560         rnh->rnh_gen++;
561         rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
562
563         RIB_WUNLOCK(rnh);
564
565         rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
566
567         nhop_free(nh_orig);
568
569         return (0);
570 }
571
572 static int
573 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
574     struct rib_cmd_info *rc)
575 {
576         int error;
577
578         /* Check if updated gateway exists */
579         if ((info->rti_flags & RTF_GATEWAY) &&
580             (info->rti_info[RTAX_GATEWAY] == NULL))
581                 return (EINVAL);
582
583         /*
584          * route change is done in multiple steps, with dropping and
585          * reacquiring lock. In the situations with multiple processes
586          * changes the same route in can lead to the case when route
587          * is changed between the steps. Address it by retrying the operation
588          * multiple times before failing.
589          */
590         for (int i = 0; i < RIB_MAX_RETRIES; i++) {
591                 error = change_route_one(rnh, info, rc);
592                 if (error != EAGAIN)
593                         break;
594         }
595
596         return (error);
597 }
598
599 /*
600  * Performs modification of routing table specificed by @action.
601  * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
602  * Needs to be run in network epoch.
603  *
604  * Returns 0 on success and fills in @rc with action result.
605  */
606 int
607 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
608     struct rib_cmd_info *rc)
609 {
610         int error;
611
612         switch (action) {
613         case RTM_ADD:
614                 error = rib_add_route(fibnum, info, rc);
615                 break;
616         case RTM_DELETE:
617                 error = rib_del_route(fibnum, info, rc);
618                 break;
619         case RTM_CHANGE:
620                 error = rib_change_route(fibnum, info, rc);
621                 break;
622         default:
623                 error = ENOTSUP;
624         }
625
626         return (error);
627 }
628
629
630 struct rt_delinfo
631 {
632         struct rt_addrinfo info;
633         struct rib_head *rnh;
634         struct rtentry *head;
635         struct rib_cmd_info rc;
636 };
637
638 /*
639  * Conditionally unlinks @rn from radix tree based
640  * on info data passed in @arg.
641  */
642 static int
643 rt_checkdelroute(struct radix_node *rn, void *arg)
644 {
645         struct rt_delinfo *di;
646         struct rt_addrinfo *info;
647         struct rtentry *rt;
648         int error;
649
650         di = (struct rt_delinfo *)arg;
651         rt = (struct rtentry *)rn;
652         info = &di->info;
653         error = 0;
654
655         info->rti_info[RTAX_DST] = rt_key(rt);
656         info->rti_info[RTAX_NETMASK] = rt_mask(rt);
657         info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
658
659         rt = rt_unlinkrte(di->rnh, info, &error);
660         if (rt == NULL) {
661                 /* Either not allowed or not matched. Skip entry */
662                 return (0);
663         }
664
665         /* Entry was unlinked. Notify subscribers */
666         di->rnh->rnh_gen++;
667         di->rc.rc_rt = rt;
668         di->rc.rc_nh_old = rt->rt_nhop;
669         rib_notify(di->rnh, RIB_NOTIFY_IMMEDIATE, &di->rc);
670
671         /* Add to the list and return */
672         rt->rt_chain = di->head;
673         di->head = rt;
674
675         return (0);
676 }
677
678 /*
679  * Iterates over a routing table specified by @fibnum and @family and
680  *  deletes elements marked by @filter_f.
681  * @fibnum: rtable id
682  * @family: AF_ address family
683  * @filter_f: function returning non-zero value for items to delete
684  * @arg: data to pass to the @filter_f function
685  * @report: true if rtsock notification is needed.
686  */
687 void
688 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
689 {
690         struct rib_head *rnh;
691         struct rt_delinfo di;
692         struct rtentry *rt;
693         struct epoch_tracker et;
694
695         rnh = rt_tables_get_rnh(fibnum, family);
696         if (rnh == NULL)
697                 return;
698
699         bzero(&di, sizeof(di));
700         di.info.rti_filter = filter_f;
701         di.info.rti_filterdata = arg;
702         di.rnh = rnh;
703         di.rc.rc_cmd = RTM_DELETE;
704
705         NET_EPOCH_ENTER(et);
706
707         RIB_WLOCK(rnh);
708         rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
709         RIB_WUNLOCK(rnh);
710
711         /* We might have something to reclaim. */
712         while (di.head != NULL) {
713                 rt = di.head;
714                 di.head = rt->rt_chain;
715                 rt->rt_chain = NULL;
716
717                 di.rc.rc_rt = rt;
718                 di.rc.rc_nh_old = rt->rt_nhop;
719                 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
720
721                 /* TODO std rt -> rt_addrinfo export */
722                 di.info.rti_info[RTAX_DST] = rt_key(rt);
723                 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
724
725                 if (report)
726                         rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
727                             fibnum);
728                 rtfree(rt);
729         }
730
731         NET_EPOCH_EXIT(et);
732 }
733
734 static void
735 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
736     struct rib_cmd_info *rc)
737 {
738         struct rib_subscription *rs;
739
740         CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
741                 if (rs->type == type)
742                         rs->func(rnh, rc, rs->arg);
743         }
744 }
745
746 /*
747  * Subscribe for the changes in the routing table specified by @fibnum and
748  *  @family.
749  *
750  * Returns pointer to the subscription structure on success.
751  */
752 struct rib_subscription *
753 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
754     enum rib_subscription_type type, bool waitok)
755 {
756         struct rib_head *rnh;
757         struct rib_subscription *rs;
758         struct epoch_tracker et;
759         int flags = M_ZERO | (waitok ? M_WAITOK : 0);
760
761         rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
762         if (rs == NULL)
763                 return (NULL);
764
765         NET_EPOCH_ENTER(et);
766         KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
767         rnh = rt_tables_get_rnh(fibnum, family);
768
769         rs->func = f;
770         rs->arg = arg;
771         rs->type = type;
772
773         RIB_WLOCK(rnh);
774         CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
775         RIB_WUNLOCK(rnh);
776         NET_EPOCH_EXIT(et);
777
778         return (rs);
779 }
780
781 /*
782  * Remove rtable subscription @rs from the table specified by @fibnum
783  *  and @family.
784  * Needs to be run in network epoch.
785  *
786  * Returns 0 on success.
787  */
788 int
789 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
790 {
791         struct rib_head *rnh;
792
793         NET_EPOCH_ASSERT();
794         KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
795         rnh = rt_tables_get_rnh(fibnum, family);
796
797         if (rnh == NULL)
798                 return (ENOENT);
799
800         RIB_WLOCK(rnh);
801         CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
802         RIB_WUNLOCK(rnh);
803
804         epoch_call(net_epoch_preempt, destroy_subscription_epoch,
805             &rs->epoch_ctx);
806
807         return (0);
808 }
809
810 /*
811  * Epoch callback indicating subscription is safe to destroy
812  */
813 static void
814 destroy_subscription_epoch(epoch_context_t ctx)
815 {
816         struct rib_subscription *rs;
817
818         rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
819
820         free(rs, M_RTABLE);
821 }
822
823 void
824 rib_init_subscriptions(struct rib_head *rnh)
825 {
826
827         CK_STAILQ_INIT(&rnh->rnh_subscribers);
828 }
829
830 void
831 rib_destroy_subscriptions(struct rib_head *rnh)
832 {
833         struct rib_subscription *rs;
834         struct epoch_tracker et;
835
836         NET_EPOCH_ENTER(et);
837         RIB_WLOCK(rnh);
838         while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
839                 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
840                 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
841                     &rs->epoch_ctx);
842         }
843         RIB_WUNLOCK(rnh);
844         NET_EPOCH_EXIT(et);
845 }
846