Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177863515
D61385.1795457046.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
D61385.1795457046.diff
View Options
diff --git a/sys/net/route.h b/sys/net/route.h
--- a/sys/net/route.h
+++ b/sys/net/route.h
@@ -72,6 +72,7 @@
#define RT_MAY_LOOP (1 << RT_MAY_LOOP_BIT) /* 0x0008 */
#define RT_HAS_HEADER (1 << RT_HAS_HEADER_BIT) /* 0x0010 */
+#define RT_NH_NOREF 0x0002 /* ro_nh is not referenced */
#define RT_REJECT 0x0020 /* Destination is reject */
#define RT_BLACKHOLE 0x0040 /* Destination is blackhole */
#define RT_HAS_GW 0x0080 /* Destination has GW */
@@ -370,9 +371,25 @@
#ifdef _KERNEL
+/*
+ * A route with RT_NH_NOREF set borrows its nexthop: the nexthop was looked
+ * up without NHR_REF, and the route is only used in the network epoch
+ * section of that lookup. This is for a route on the stack, never for one
+ * that is cached, such as the route of a PCB; RO_NH_ASSERT_REF() is for
+ * code that is handed a route which may be cached. Dropping the nexthop
+ * of a route that borrows it releases nothing and clears the flag, so that
+ * a nexthop stored in the route afterwards is referenced as usual.
+ */
+#define RO_NH_ASSERT_REF(_ro) \
+ KASSERT((_ro) == NULL || ((_ro)->ro_flags & RT_NH_NOREF) == 0, \
+ ("%s: route %p borrows its nexthop", __func__, (_ro)))
+
#define RO_NHFREE(_ro) do { \
if ((_ro)->ro_nh) { \
- NH_FREE((_ro)->ro_nh); \
+ if (((_ro)->ro_flags & RT_NH_NOREF) != 0) \
+ (_ro)->ro_flags &= ~RT_NH_NOREF; \
+ else \
+ NH_FREE((_ro)->ro_nh); \
(_ro)->ro_nh = NULL; \
} \
} while (0)
@@ -382,10 +399,7 @@
LLE_FREE((ro)->ro_lle); \
(ro)->ro_lle = NULL; \
} \
- if ((ro)->ro_nh != NULL) { \
- NH_FREE((ro)->ro_nh); \
- (ro)->ro_nh = NULL; \
- } \
+ RO_NHFREE(ro); \
} while (0)
#define RO_GET_FAMILY(ro, dst) ((ro) != NULL && \
diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -964,17 +964,22 @@
sin->sin_len = sizeof(*sin);
sin->sin_addr = ip->ip_dst;
flowid = m->m_pkthdr.flowid;
- ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
+ /*
+ * The nexthop is not referenced: nexthops are destroyed through
+ * NET_EPOCH_CALL(), so this one stays valid until we return, and
+ * the route does not outlive this function. RT_NH_NOREF tells
+ * ip_output() to drop it without releasing it.
+ */
+ ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, flowid);
if (ro.ro_nh != NULL) {
+ ro.ro_flags = RT_NH_NOREF;
if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
- NH_FREE(ro.ro_nh);
return;
}
if (ro.ro_nh->nh_flags & NHF_REJECT) {
IPSTAT_INC(ips_cantforward);
- NH_FREE(ro.ro_nh);
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
return;
}
@@ -1021,7 +1026,6 @@
if (IPSEC_ENABLED(ipv4)) {
if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
/* mbuf consumed by IPsec */
- RO_NHFREE(&ro);
m_freem(mcopy);
if (error != EINPROGRESS)
IPSTAT_INC(ips_cantforward);
@@ -1069,6 +1073,7 @@
if (error == EMSGSIZE && ro.ro_nh)
mtu = ro.ro_nh->nh_mtu;
+ /* ip_output() may have replaced the nexthop with a referenced one. */
RO_NHFREE(&ro);
if (error)
diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c
--- a/sys/netinet/ip_output.c
+++ b/sys/netinet/ip_output.c
@@ -341,6 +341,7 @@
if (inp != NULL) {
INP_LOCK_ASSERT(inp);
+ RO_NH_ASSERT_REF(ro);
M_SETFIB(m, inp->inp_inc.inc_fibnum);
if ((flags & IP_NODEFAULTFLOWID) == 0) {
m->m_pkthdr.flowid = inp->inp_flowid;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 23, 6:04 PM (22 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41937310
Default Alt Text
D61385.1795457046.diff (3 KB)
Attached To
Mode
D61385: ip_forward: Do not reference the nexthop
Attached
Detach File
Event Timeline