From nobody Thu Oct 19 20:03:02 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SBJYN4y3Gz4xpvC; Thu, 19 Oct 2023 20:03:04 +0000 (UTC) (envelope-from kp@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SBJYN4HFlz4F7X; Thu, 19 Oct 2023 20:03:04 +0000 (UTC) (envelope-from kp@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697745784; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=z5VcjUBoMMWpNWBCIUAIrmxZq8e9OwIxn1dNsA7IlIY=; b=RSlI6EcPaB9BKNCugaaHEDFw6aRMkqTxLy6C4DB+aVoiL8sIHtE0qpgkuwjIGzPCHhNAeq 5Nue9FXr3FE3Ju+8suD8PQh87/o4oL9pyASL/uITpyA0Uc6jIfkrnLJn3cTZ9cwHBHFVHL VAsYyjVuEV0672dxpCGuScL7AG2tRKY91CuFP35PA7y0Sh1vMG4HXhKmoBPfhR/8a7hdvT aQks9zX5CIBCk2+S1aPfErNjkwSD1mT1j77ZY7eJ7TH1kSM0W8ZClQC0RQmiQxdv+CNVpz E5l9MMRVXUEFL1k5JxChiyxRkZLMzIWRFal/7uSqiQQigdfs1PU4fBuYPtRh5w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1697745784; a=rsa-sha256; cv=none; b=MFwgC7AD5ffZjOTWwbexia3ICr4R7aECeEOIHQME+3p4FbuA9+rciBYwttpT0CWPqjvFB1 ss/OmTxBPtz3JQ8pATff/+11YHaFp+bwQtA3QJrhfbk7GwYkHvV8VYClHL7IM3MqEBrBlJ j10QUUsfbBGSLHZQ3uEj6rZ+vZokeWguLJYrtJiIg/dCua5e0jTGaq6whouIz+XQJNRxGp tbZ5vHs7B+5/FEMYxzKM8nyAr5iMZCTsYvl1yFZfzfFQJGEkCW8LaBal0XFfGva+SwKCny CJBqmcHcbYUAgjtzhqYNl0pNhJEV1e7yibXHETHB6tssnPbVIpgbHRn7JPg1wA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697745784; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=z5VcjUBoMMWpNWBCIUAIrmxZq8e9OwIxn1dNsA7IlIY=; b=k/UVcPUt15p1SjGL9AnbmUkv6n2GDjIXHIKgehoC5Ob3xbiZ42DpiWFB+reHnk3k5LpR2m HSNG1yAfC8pHoIAY3LKy7CWx14kMML6Mhao1LiX0ict1Sx4mnh4ubqLFBqIh3wNCNYo//k jTkm8DR/WWX705gzDPmTg8ruU2QmDwZP/+pOAUaqL6ZWBYDhC1WqaqAAaxvdguxE+e0doz nxvzEqEkm04N9nfHLU/fDRAerwZ2lTslHFLUe7nRdOm/W0qWPino7tr5I01NofThuwhSk1 mKi+oXxwGtRP2HfPRjXHd+/pw66RUy59M0CYLa25hndlT7UyOfuyHnLHzs7ULg== Received: from venus.codepro.be (venus.codepro.be [5.9.86.228]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "mx1.codepro.be", Issuer "R3" (verified OK)) (Authenticated sender: kp) by smtp.freebsd.org (Postfix) with ESMTPSA id 4SBJYN28JRz1C9J; Thu, 19 Oct 2023 20:03:04 +0000 (UTC) (envelope-from kp@FreeBSD.org) Received: by venus.codepro.be (Postfix, authenticated sender kp) id 9F9D28BA4; Thu, 19 Oct 2023 22:03:02 +0200 (CEST) From: Kristof Provost To: Gleb Smirnoff Cc: Igor Ostapenko , src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: fabf705f4b5a - main - pf: fix pf divert-to loop Date: Thu, 19 Oct 2023 22:03:02 +0200 X-Mailer: MailMate (1.14r5937) Message-ID: <86D6E74C-3062-4718-816E-07CBA4F2903F@FreeBSD.org> In-Reply-To: References: <202310191237.39JCbdXp094554@gitrepo.freebsd.org> List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_MailMate_C0B0683C-C6CE-412C-88A4-DEEE8300C497_=" --=_MailMate_C0B0683C-C6CE-412C-88A4-DEEE8300C497_= Content-Type: text/plain; format=flowed On 19 Oct 2023, at 18:14, Gleb Smirnoff wrote: > On Thu, Oct 19, 2023 at 12:37:39PM +0000, Kristof Provost wrote: > K> +++ b/sys/netinet/ip_var.h > ... > K> +/* pf specific mtag for divert(4) support */ > K> +enum { PF_DIVERT_MTAG_DIR_IN=1, PF_DIVERT_MTAG_DIR_OUT=2 }; > K> +struct pf_divert_mtag { > K> + uint16_t idir; // initial pkt direction > K> + uint16_t ndir; // a) divert(4) port upon initial diversion > K> + // b) new direction upon pkt re-enter > K> +}; > > This can be written as: > > typedef enum { > PF_DIVERT_MTAG_DIR_IN = 1, > PF_DIVERT_MTAG_DIR_OUT = 2, > } pf_mtag_dir; > struct pf_divert_mtag { > pf_mtag_dir idir; /* Initial packet direction. */ > union { > pf_mtag_dir ndir; /* New direction after re-enter. > */ > uint16_t port; /* Initial divert(4) port. */ > }; > }; > > 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. > > This will grow structure from 4 bytes to 8, as enum is always an int. > However, > uma allocator backing m_tag_alloc() will allocate same amount of > memory. > Something like this? 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..0f3facc54d4e 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 + 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 @@ pf_test(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, 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 : Best regards, Kristof --=_MailMate_C0B0683C-C6CE-412C-88A4-DEEE8300C497_= Content-Type: text/html Content-Transfer-Encoding: quoted-printable

On 19 Oct 2023, at 18:14, Gleb Smirnoff wrote:

On Thu, Oct 19, 2023 at 12:37:39PM = +0000, Kristof Provost wrote:
K> +++ b/sys/netinet/ip_var.h
=2E..
K> +/* pf specific mtag for divert(4) support */
K> +enum { PF_DIVERT_MTAG_DIR_IN=3D1, PF_DIVERT_MTAG_DIR_OUT=3D2 };
K> +struct pf_divert_mtag {
K> + uint16_t idir; // initial pkt direction
K> + uint16_t ndir; // a) divert(4) port upon initial diversion
K> + // b) new direction upon pkt re-enter
K> +};

This can be written as:

typedef enum {
PF_DIVERT_MTAG_DIR_IN =3D 1,
PF_DIVERT_MTAG_DIR_OUT =3D 2,
} pf_mtag_dir;
struct pf_divert_mtag {
pf_mtag_dir idir; /* Initial packet direction. */
union {
pf_mtag_dir ndir; /* New direction after re-enter. */=
uint16_t port; /* Initial divert(4) port. */
};
};

The benefit is that in the debugger you will see PF_DIVER= T_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.

This will grow structure from 4 bytes to 8, as enum is al= ways an int. However,
uma allocator backing m_tag_alloc() will allocate same amount of memory.<= /p>

Something like this?

di=
ff --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 =3D m_tag_locate(m, MTAG_PF_DIVERT, 0, NULL)) !=3D=
 NULL) {
                cookie =3D ((struct pf_divert_mtag *)(mtag+1))->idir;
-               nport =3D htons(((struct pf_divert_mtag *)(mtag+1))->n=
dir);
+               nport =3D htons(((struct pf_divert_mtag *)(mtag+1))->p=
ort);
        } else {
                m_freem(m);
                return;
diff --git a/sys/netinet/ip_var.h b/sys/netinet/ip_var.h
index a8c687682af9..0f3facc54d4e 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=3D1, PF_DIVERT_MTAG_DIR_OUT=3D2 };
+__enum_uint8_decl(pf_mtag_dir) {
+       PF_DIVERT_MTAG_DIR_IN =3D 1,
+       PF_DIVERT_MTAG_DIR_OUT =3D 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
+       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 @@ pf_test(int dir, int pflags, struct ifnet *ifp, str=
uct mbuf **m0,
                mtag =3D m_tag_alloc(MTAG_PF_DIVERT, 0,
                    sizeof(struct pf_divert_mtag), M_NOWAIT | M_ZERO);
                if (mtag !=3D NULL) {
-                       ((struct pf_divert_mtag *)(mtag+1))->ndir =3D
+                       ((struct pf_divert_mtag *)(mtag+1))->port =3D
                            ntohs(r->divert.port);
                        ((struct pf_divert_mtag *)(mtag+1))->idir =3D
                            (dir =3D=3D PF_IN) ? PF_DIVERT_MTAG_DIR_IN :

Best regards,
Kristof

--=_MailMate_C0B0683C-C6CE-412C-88A4-DEEE8300C497_=--