Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 Oct 2023 08:42:26 GMT
From:      Kristof Provost <kp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: c1146e6ad67f - main - pf: use an enum for packet direction in divert tag
Message-ID:  <202310200842.39K8gQfS005799@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by kp:

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

commit c1146e6ad67fb866c2472a1cbe5609fd939fd5ef
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2023-10-20 07:13:56 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2023-10-20 07:16:08 +0000

    pf: use an enum for packet direction in divert tag
    
    The benefit is that in the debugger you will see PF_DIVERT_MTAG_DIR_IN
    instead of 1 when looking at a structure. And compilation time failure
    if anybody sets it to a wrong value. Using "port" instead of "ndir" when
    assigning a port improves readability of code.
    
    Suggested by:   glebius
    MFC after:      3 weeks
    X-MFC-With:     fabf705f4b
---
 sys/netinet/ip_divert.c |  2 +-
 sys/netinet/ip_var.h    | 14 ++++++++++----
 sys/netpfil/pf/pf.c     |  2 +-
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/sys/netinet/ip_divert.c b/sys/netinet/ip_divert.c
index ad95a1ce0d76..78ca36fc2a0f 100644
--- a/sys/netinet/ip_divert.c
+++ b/sys/netinet/ip_divert.c
@@ -182,7 +182,7 @@ divert_packet(struct mbuf *m, bool incoming)
 		    (((struct ipfw_rule_ref *)(mtag+1))->info));
 	} else if ((mtag = m_tag_locate(m, MTAG_PF_DIVERT, 0, NULL)) != NULL) {
 		cookie = ((struct pf_divert_mtag *)(mtag+1))->idir;
-		nport = htons(((struct pf_divert_mtag *)(mtag+1))->ndir);
+		nport = htons(((struct pf_divert_mtag *)(mtag+1))->port);
 	} else {
 		m_freem(m);
 		return;
diff --git a/sys/netinet/ip_var.h b/sys/netinet/ip_var.h
index a8c687682af9..a8a9adc1d4c6 100644
--- a/sys/netinet/ip_var.h
+++ b/sys/netinet/ip_var.h
@@ -328,11 +328,17 @@ extern int	(*ip_dn_ctl_ptr)(struct sockopt *);
 extern int	(*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
 
 /* pf specific mtag for divert(4) support */
-enum { PF_DIVERT_MTAG_DIR_IN=1, PF_DIVERT_MTAG_DIR_OUT=2 };
+__enum_uint8_decl(pf_mtag_dir) {
+	PF_DIVERT_MTAG_DIR_IN = 1,
+	PF_DIVERT_MTAG_DIR_OUT = 2
+};
 struct pf_divert_mtag {
-	uint16_t idir;	// initial pkt direction
-	uint16_t ndir;	// a) divert(4) port upon initial diversion
-			// b) new direction upon pkt re-enter
+	__enum_uint8(pf_mtag_dir) idir;	// initial pkt direction
+	union {
+		__enum_uint8(pf_mtag_dir) ndir;	// a) divert(4) port upon initial diversion
+				// b) new direction upon pkt re-enter
+		uint16_t port;	/* Initial divert(4) port */
+	};
 };
 #define MTAG_PF_DIVERT	1262273569
 
diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index a6c7ee359416..1cd8412193dc 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -8022,7 +8022,7 @@ done:
 		mtag = m_tag_alloc(MTAG_PF_DIVERT, 0,
 		    sizeof(struct pf_divert_mtag), M_NOWAIT | M_ZERO);
 		if (mtag != NULL) {
-			((struct pf_divert_mtag *)(mtag+1))->ndir =
+			((struct pf_divert_mtag *)(mtag+1))->port =
 			    ntohs(r->divert.port);
 			((struct pf_divert_mtag *)(mtag+1))->idir =
 			    (dir == PF_IN) ? PF_DIVERT_MTAG_DIR_IN :



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202310200842.39K8gQfS005799>