summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormpi <mpi@openbsd.org>2015-09-01 12:50:03 +0000
committermpi <mpi@openbsd.org>2015-09-01 12:50:03 +0000
commit0cc6bee5d9359ee9ad922524d9b19d019222dd46 (patch)
treee7cbb83d9f95cb5967a840497336f95cda7d6a29
parentditch a few prototypes for non-existant functions (diff)
downloadwireguard-openbsd-0cc6bee5d9359ee9ad922524d9b19d019222dd46.tar.xz
wireguard-openbsd-0cc6bee5d9359ee9ad922524d9b19d019222dd46.zip
Introduce rtisvalid(9) a function to check if a (cached) route entry
can be used or should be released by rtfree(9). It currently checks if the route is UP and is not attached to a stall ifa. ok bluhm@, claudio@
-rw-r--r--share/man/man9/Makefile4
-rw-r--r--share/man/man9/rtalloc.921
-rw-r--r--sys/net/route.c31
-rw-r--r--sys/net/route.h3
4 files changed, 44 insertions, 15 deletions
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile
index 47ee2f46a23..67f2460b8d1 100644
--- a/share/man/man9/Makefile
+++ b/share/man/man9/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.239 2015/09/01 03:47:58 dlg Exp $
+# $OpenBSD: Makefile,v 1.240 2015/09/01 12:50:03 mpi Exp $
# $NetBSD: Makefile,v 1.4 1996/01/09 03:23:01 thorpej Exp $
# Makefile for section 9 (kernel function and variable) manual pages.
@@ -332,7 +332,7 @@ MLINKS+=rt_timer_add.9 rt_timer_queue_create.9 \
rt_timer_add.9 rt_timer_queue_change.9 \
rt_timer_add.9 rt_timer_queue_destroy.9 \
rt_timer_add.9 rt_timer_remove_all.9
-MLINKS+=rtalloc.9 rtalloc_mpath.9 rtalloc.9 rtfree.9
+MLINKS+=rtalloc.9 rtalloc_mpath.9 rtalloc.9 rtisvalid.9 rtalloc.9 rtfree.9
MLINKS+=rtable_add.9 rtable_exists.9 rtable_add.9 rtable_get.9 \
rtable_add.9 rtable_l2.9 rtable_add.9 rtable_l2set.9
MLINKS+=rtlabel_id2name.9 rtlabel_name2id.9 \
diff --git a/share/man/man9/rtalloc.9 b/share/man/man9/rtalloc.9
index 1b3afe47fd6..6bb32c2ed60 100644
--- a/share/man/man9/rtalloc.9
+++ b/share/man/man9/rtalloc.9
@@ -1,6 +1,6 @@
-.\" $OpenBSD: rtalloc.9,v 1.3 2014/12/05 16:55:53 schwarze Exp $
+.\" $OpenBSD: rtalloc.9,v 1.4 2015/09/01 12:50:03 mpi Exp $
.\"
-.\" Copyright (c) 2014 Martin Pieuchot
+.\" Copyright (c) 2014-2015 Martin Pieuchot
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -14,12 +14,13 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: December 5 2014 $
+.Dd $Mdocdate: September 1 2015 $
.Dt RTALLOC 9
.Os
.Sh NAME
.Nm rtalloc ,
.Nm rtalloc_mpath ,
+.Nm rtisvalid,
.Nm rtfree
.Nd routing entry interface
.Sh SYNOPSIS
@@ -30,6 +31,8 @@
.Fn rtalloc "struct sockaddr *dst" "int flags" "unsigned int rtableid"
.Ft struct rtentry *
.Fn rtalloc_mpath "struct sockaddr *dst" "uint32_t *src" "unsigned int rtableid"
+.Ft int
+.Fn rtisvalid "struct rtentry *rt"
.Ft void
.Fn rtfree "struct rtentry *rt"
.Sh DESCRIPTION
@@ -65,6 +68,14 @@ but selects a multipath routing entry corresponding to
when possible.
.Pp
The
+.Fn rtisvalid
+function checks if the route entry
+.Fa rt
+is still valid and can be used.
+Cached entries that are no longer valid should be released by calling
+.Fn rtfree .
+.Pp
+The
.Fn rtfree
function releases a reference to the routing entry
.Fa rt ,
@@ -72,9 +83,13 @@ freeing it if the reference count drops to 0.
.Sh CONTEXT
.Fn rtalloc ,
.Fn rtalloc_mpath ,
+.Fn rtisvalid ,
and
.Fn rtfree
can be called during autoconf, from process context, or from interrupt context.
+.Sh RETURN VALUES
+.Fn rtisvalid
+returns 1 if the route entry is valid, otherwise 0.
.Sh SEE ALSO
.Xr route 4 ,
.Xr rtrequest1 9
diff --git a/sys/net/route.c b/sys/net/route.c
index 7ae25cb5a93..b6b5e7e03d7 100644
--- a/sys/net/route.c
+++ b/sys/net/route.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: route.c,v 1.227 2015/09/01 10:04:51 mpi Exp $ */
+/* $OpenBSD: route.c,v 1.228 2015/09/01 12:50:03 mpi Exp $ */
/* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */
/*
@@ -300,6 +300,25 @@ rtable_exists(u_int id) /* verify table with that ID exists */
return (1);
}
+/*
+ * Returns 1 if the (cached) ``rt'' entry is still valid, 0 otherwise.
+ */
+int
+rtisvalid(struct rtentry *rt)
+{
+ if (rt == NULL)
+ return (0);
+
+ if ((rt->rt_flags & RTF_UP) == 0)
+ return (0);
+
+ /* Routes attached to stall ifas should be freed. */
+ if (rt->rt_ifa == NULL || rt->rt_ifa->ifa_ifp == NULL)
+ return (0);
+
+ return (1);
+}
+
struct rtentry *
rtalloc(struct sockaddr *dst, int flags, unsigned int tableid)
{
@@ -659,19 +678,13 @@ ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway,
}
if (ifa == NULL) {
struct rtentry *rt = rtalloc(gateway, 0, rtableid);
- if (rt == NULL)
- return (NULL);
/* The gateway must be local if the same address family. */
- if ((rt->rt_flags & RTF_GATEWAY) &&
- rt_key(rt)->sa_family == dst->sa_family) {
+ if (!rtisvalid(rt) || ((rt->rt_flags & RTF_GATEWAY) &&
+ rt_key(rt)->sa_family == dst->sa_family)) {
rtfree(rt);
return (NULL);
}
ifa = rt->rt_ifa;
- if (ifa == NULL || ifa->ifa_ifp == NULL) {
- rtfree(rt);
- return (NULL);
- }
rtfree(rt);
}
if (ifa->ifa_addr->sa_family != dst->sa_family) {
diff --git a/sys/net/route.h b/sys/net/route.h
index 5491b45ccab..26d58590cc4 100644
--- a/sys/net/route.h
+++ b/sys/net/route.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: route.h,v 1.110 2015/08/20 12:39:43 mpi Exp $ */
+/* $OpenBSD: route.h,v 1.111 2015/09/01 12:50:03 mpi Exp $ */
/* $NetBSD: route.h,v 1.9 1996/02/13 22:00:49 christos Exp $ */
/*
@@ -377,6 +377,7 @@ void rt_timer_queue_destroy(struct rttimer_queue *);
unsigned long rt_timer_queue_count(struct rttimer_queue *);
void rt_timer_timer(void *);
+int rtisvalid(struct rtentry *);
#ifdef SMALL_KERNEL
#define rtalloc_mpath(dst, s, rid) rtalloc((dst), RT_REPORT|RT_RESOLVE, (rid))
#else