git: 3903a749c8bf - stable/14 - pf: use an enum for packet direction in divert tag
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 09 Nov 2023 14:39:21 UTC
The branch stable/14 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=3903a749c8bfcb50f85711ec95eecb8ce50644d9 commit 3903a749c8bfcb50f85711ec95eecb8ce50644d9 Author: Kristof Provost <kp@FreeBSD.org> AuthorDate: 2023-10-20 07:13:56 +0000 Commit: Kristof Provost <kp@FreeBSD.org> CommitDate: 2023-11-09 09:56:20 +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 (cherry picked from commit c1146e6ad67fb866c2472a1cbe5609fd939fd5ef) --- 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 eb2e09b2e6f2..4990dce653b1 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -8249,7 +8249,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 :