Skip to contentSkip to conversation

ipopt: use ipv4 address datatype for address field in ip options

PATCH Superseded Pablo Neira Ayuso 2 participants 6 messages Jan 31, 2025 – Feb 6, 2025 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 ipopt: use ipv4 address datatype for address field in ip options

So user does not have to play integer arithmetics to match on IPv4 address.

Before:

 # nft describe ip option lsrr addr
 exthdr expression, datatype integer (integer), 32 bits

After:

 # nft describe ip option lsrr addr
 exthdr expression, datatype ipv4_addr (IPv4 address) (basetype integer), 32 bits
---
 src/ipopt.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/ipopt.c +4 -4
index 37f779d468ab..ddb42f5712d4 100644
@@ -24,7 +24,7 @@ static const struct exthdr_desc ipopt_lsrr = {
 		[IPOPT_FIELD_TYPE]		= PHT("type",    0,  8),
 		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
 		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
-		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
+		[IPOPT_FIELD_ADDR_0]		= PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
 	},
 };
 
@@ -35,7 +35,7 @@ static const struct exthdr_desc ipopt_rr = {
 		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
 		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
 		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
-		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
+		[IPOPT_FIELD_ADDR_0]		= PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
 	},
 };
 
@@ -46,7 +46,7 @@ static const struct exthdr_desc ipopt_ssrr = {
 		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
 		[IPOPT_FIELD_LENGTH]		= PHT("length",  8,  8),
 		[IPOPT_FIELD_PTR]		= PHT("ptr",    16,  8),
-		[IPOPT_FIELD_ADDR_0]		= PHT("addr",   24, 32),
+		[IPOPT_FIELD_ADDR_0]		= PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
 	},
 };
 
@@ -56,7 +56,7 @@ static const struct exthdr_desc ipopt_ra = {
 	.templates	= {
 		[IPOPT_FIELD_TYPE]		= PHT("type",   0,   8),
 		[IPOPT_FIELD_LENGTH]		= PHT("length", 8,   8),
-		[IPOPT_FIELD_VALUE]		= PHT("value",  16, 16),
+		[IPOPT_FIELD_ADDR_0]		= PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
 	},
 };
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 parser_bison: turn redudant ip option type field match into boolean

The ip option expression allows for non-sense matching like:

ip option lsrr type 1

because 'lsrr' already provides the type field, this never results in a matching.

Turn this expression into:

ip option lsrr exists

And update documentation to hide this redundant type field.

---
 doc/payload-expression.txt | 8 ++++----
 src/parser_bison.y         | 3 +++
 2 files changed, 7 insertions(+), 4 deletions(-)
doc/payload-expression.txt +4 -4
index 7bc24a8a6502..2a155aa87b6f 100644
@@ -808,16 +808,16 @@ TCP option matching also supports raw expression syntax to access arbitrary opti
 |Keyword| Description | IP option fields
 |lsrr|
 Loose Source Route |
-type, length, ptr, addr
+length, ptr, addr
 |ra|
 Router Alert |
-type, length, value
+length, value
 |rr|
 Record Route |
-type, length, ptr, addr
+length, ptr, addr
 |ssrr|
 Strict Source Route |
-type, length, ptr, addr
+length, ptr, addr
 |============================
 
 .finding TCP options
src/parser_bison.y +3 -0
index c8714812532d..d15bf212489d 100644
@@ -5698,6 +5698,9 @@ ip_hdr_expr		:	IP	ip_hdr_field	close_scope_ip
 					erec_queue(error(&@1, "unknown ip option type/field"), state->msgs);
 					YYERROR;
 				}
+
+				if ($4 == IPOPT_FIELD_TYPE)
+					$$->exthdr.flags = NFT_EXTHDR_F_PRESENT;
 			}
 			|	IP	OPTION	ip_option_type close_scope_ip
 			{
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH nft 2/2] parser_bison: turn redudant ip option type field match into boolean

I suggest adding the following note about the addr field.

doc/payload-expression.txt +2 -0
index 7bc24a8a..9a7ac396 100644
@@ -820,6 +820,8 @@ Strict Source Route |
 type, length, ptr, addr
 |============================
 
+Note: Only the first IP address is specified in the addr field.
+
 .finding TCP options
 --------------------
 filter input tcp option sack-perm exists counter
author maintainer
#3 lore raw

Re: [PATCH nft 2/2] parser_bison: turn redudant ip option type field match into boolean

Hi,

doc/payload-expression.txt Strict Source Route | · line 823

Quoted 11 lines · Alexey Kashavkin

I suggest adding the following note about the addr field.

doc/payload-expression.txt +1 -0
index 7bc24a8a..9a7ac396 100644
@@ -820,6 +820,8 @@ Strict Source Route |
 type, length, ptr, addr
 |============================
 
+Note: Only the first IP address is specified in the addr field.

It should be not too complicated to extend this to match on any address.

Quoted 6 lines
doc/payload-expression.txt +1 -0
+
 .finding TCP options
 --------------------
 filter input tcp option sack-perm exists counter
#4 lore raw

Re: [PATCH nft 2/2] parser_bison: turn redudant ip option type field match into boolean

Hi Pablo,

Yes, I agree. If a template field like IPOPT_FIELD_ADDR_N with the required offset will be added.

Quoted 4 lines · Pablo Neira Ayuso

It should be not too complicated to extend this to match on any address.

author maintainer
#5 lore raw

Re: [PATCH nft 2/2] parser_bison: turn redudant ip option type field match into boolean

Quoted 3 lines · Alexey Kashavkin

Hi Pablo,

Yes, I agree. If a template field like IPOPT_FIELD_ADDR_N with the required offset will be added.

Something like:

addr[1] -> first IP address
addr[2] -> second IP address
...