git: fd1aa866eb22 - main - routing: add rt_tables_get_rnh_safe() that doesn't panic when af/fib is incorrect.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Tue, 25 Apr 2023 11:15:27 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=fd1aa866eb22bcaa01dbedfbe7778c29c0c2fa25

commit fd1aa866eb22bcaa01dbedfbe7778c29c0c2fa25
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-04-25 10:53:51 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-04-25 10:53:51 +0000

    routing: add rt_tables_get_rnh_safe() that doesn't panic when af/fib is
      incorrect.
    
    MFC after:      2 weeks
---
 sys/net/route/route_ctl.h    |  2 ++
 sys/net/route/route_tables.c | 11 +++++++++++
 2 files changed, 13 insertions(+)

diff --git a/sys/net/route/route_ctl.h b/sys/net/route/route_ctl.h
index 7a4ed804feb7..d3c457cdeaae 100644
--- a/sys/net/route/route_ctl.h
+++ b/sys/net/route/route_ctl.h
@@ -35,6 +35,8 @@
 #ifndef	_NET_ROUTE_ROUTE_CTL_H_
 #define	_NET_ROUTE_ROUTE_CTL_H_
 
+struct rib_head *rt_tables_get_rnh_safe(uint32_t table, sa_family_t family);
+
 struct rib_cmd_info {
 	uint8_t			rc_cmd;		/* RTM_ADD|RTM_DEL|RTM_CHANGE */
 	uint8_t			spare[3];
diff --git a/sys/net/route/route_tables.c b/sys/net/route/route_tables.c
index be67556915d0..31696e847488 100644
--- a/sys/net/route/route_tables.c
+++ b/sys/net/route/route_tables.c
@@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$");
 
 #include <net/vnet.h>
 #include <net/route.h>
+#include <net/route/route_ctl.h>
 #include <net/route/route_var.h>
 
 /* Kernel config default option. */
@@ -387,6 +388,16 @@ rt_tables_get_rnh(uint32_t table, sa_family_t family)
 	return (rt_tables_get_rnh_ptr(table, family));
 }
 
+struct rib_head *
+rt_tables_get_rnh_safe(uint32_t table, sa_family_t family)
+{
+	if (__predict_false(table >= V_rt_numfibs))
+		return (NULL);
+	if (__predict_false(family >= (AF_MAX + 1)))
+		return (NULL);
+	return (rt_tables_get_rnh_ptr(table, family));
+}
+
 u_int
 rt_tables_get_gen(uint32_t table, sa_family_t family)
 {