]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: net_helper: accept an Ethernet header ending with the protocol
authorMartin Mirchev <mirchevmartin2203@gmail.com>
Wed, 18 Nov 2026 03:25:34 +0000 (11:25 +0800)
committerWilly Tarreau <w@1wt.eu>
Fri, 20 Nov 2026 11:36:57 +0000 (12:36 +0100)
The eth.data, eth.hdr, eth.proto and eth.vlan converters walk the
Ethernet header with "idx + 2 < data", which requires at least one byte
after the 2-byte protocol field. An input that ends exactly with the
header is thus reported as incomplete, so eth.proto and eth.vlan return
nothing on the output of eth.hdr: "eth.hdr,eth.proto" never works, and
neither does any frame truncated right after the protocol field.

The protocol field is complete when idx + 2 <= data, so let's use this
condition in the 4 loops. Frames that carry data after the header are
not affected. The net_helper reg-test now checks eth.hdr followed by
eth.proto and eth.vlan.

This should be backported to 3.4.

reg-tests/converter/net_helper.vtc
src/net_helper.c

index 85d6450cbc543df253abf00f614864df8c382e88..7fc8bb824e66126e5fdf1ad06c45af80390987ec 100644 (file)
@@ -26,6 +26,14 @@ haproxy h1 -conf {
        http-after-response set-header ws-ok "%[bin(00010050000000000000000060020000ffff000001030307),tcp.options.wscale]"
        http-after-response set-header ws-len2 "%[bin(00010050000000000000000060020000ffff000001010302aa),tcp.options.wscale]"
        http-after-response set-header tsval-len2 "%[bin(00010050000000000000000060020000ffff00000101080212345678),tcp.options.tsval]"
+
+       # Ethernet header with one VLAN tag (ID 100) and IPv4, followed by
+       # some data, and the same header alone as returned by eth.hdr.
+       http-after-response set-header eth-proto "%[bin(000000000000000000000000810000640800450000),eth.proto]"
+       http-after-response set-header eth-hdr-proto "%[bin(000000000000000000000000810000640800450000),eth.hdr,eth.proto]"
+       http-after-response set-header eth-hdr-vlan "%[bin(000000000000000000000000810000640800450000),eth.hdr,eth.vlan]"
+       http-after-response set-header eth-data "%[bin(000000000000000000000000810000640800450000),eth.data,hex]"
+       http-after-response set-header eth-noproto "%[bin(00000000000000000000000081000064),eth.proto]"
 } -start
 
 client c1 -connect ${h1_fe_sock} {
@@ -37,4 +45,9 @@ client c1 -connect ${h1_fe_sock} {
        expect resp.http.ws-ok == "7"
        expect resp.http.ws-len2 == "0"
        expect resp.http.tsval-len2 == "0"
+       expect resp.http.eth-proto == "2048"
+       expect resp.http.eth-hdr-proto == "2048"
+       expect resp.http.eth-hdr-vlan == "100"
+       expect resp.http.eth-data == "450000"
+       expect resp.http.eth-noproto == ""
 } -run
index 6a0c7d7d311b3a111bc733682c90bba70fbaed6f..e9a062b67ee0cef3e8c91142e90a436f20019f8b 100644 (file)
@@ -23,7 +23,7 @@ static int sample_conv_eth_data(const struct arg *arg_p, struct sample *smp, voi
 {
        size_t idx;
 
-       for (idx = 12; idx + 2 < smp->data.u.str.data; idx += 4) {
+       for (idx = 12; idx + 2 <= smp->data.u.str.data; idx += 4) {
                if (read_n16(smp->data.u.str.area + idx) != 0x8100) {
                        if (smp->data.u.str.size)
                                smp->data.u.str.size -= idx + 2;
@@ -54,7 +54,7 @@ static int sample_conv_eth_hdr(const struct arg *arg_p, struct sample *smp, void
 {
        size_t idx;
 
-       for (idx = 12; idx + 2 < smp->data.u.str.data; idx += 4) {
+       for (idx = 12; idx + 2 <= smp->data.u.str.data; idx += 4) {
                if (read_n16(smp->data.u.str.area + idx) != 0x8100) {
                        smp->data.u.str.data = idx + 2;
                        return 1;
@@ -72,7 +72,7 @@ static int sample_conv_eth_proto(const struct arg *arg_p, struct sample *smp, vo
        ushort proto;
        size_t idx;
 
-       for (idx = 12; idx + 2 < smp->data.u.str.data; idx += 4) {
+       for (idx = 12; idx + 2 <= smp->data.u.str.data; idx += 4) {
                proto = read_n16(smp->data.u.str.area + idx);
                if (proto != 0x8100) {
                        smp->data.u.sint = proto;
@@ -107,7 +107,7 @@ static int sample_conv_eth_vlan(const struct arg *arg_p, struct sample *smp, voi
        ushort vlan = 0;
        size_t idx;
 
-       for (idx = 12; idx + 2 < smp->data.u.str.data; idx += 4) {
+       for (idx = 12; idx + 2 <= smp->data.u.str.data; idx += 4) {
                if (read_n16(smp->data.u.str.area + idx) != 0x8100) {
                        smp->data.u.sint = vlan;
                        smp->data.type = SMP_T_SINT;