From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Subject: [PATCH 1/3] dhcp: Use -1 as "missing option" length instead of 0
Date: Mon, 25 Nov 2024 01:04:21 +0100 [thread overview]
Message-ID: <20241125000423.4131458-2-sbrivio@redhat.com> (raw)
In-Reply-To: <20241125000423.4131458-1-sbrivio@redhat.com>
We want to add support for option 80 (Rapid Commit, RFC 4039), whose
length is 0.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
dhcp.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/dhcp.c b/dhcp.c
index a06f143..2fe4a4d 100644
--- a/dhcp.c
+++ b/dhcp.c
@@ -36,9 +36,9 @@
/**
* struct opt - DHCP option
* @sent: Convenience flag, set while filling replies
- * @slen: Length of option defined for server
+ * @slen: Length of option defined for server, -1 if not going to be sent
* @s: Option payload from server
- * @clen: Length of option received from client
+ * @clen: Length of option received from client, -1 if not received
* @c: Option payload from client
*/
struct opt {
@@ -154,17 +154,17 @@ static int fill(struct msg *m)
* option 53 at the beginning of the list.
* Put it there explicitly, unless requested via option 55.
*/
- if (!memchr(opts[55].c, 53, opts[55].clen))
+ if (opts[55].clen > 0 && !memchr(opts[55].c, 53, opts[55].clen))
fill_one(m, 53, &offset);
for (i = 0; i < opts[55].clen; i++) {
o = opts[55].c[i];
- if (opts[o].slen)
+ if (opts[o].slen != -1)
fill_one(m, o, &offset);
}
for (o = 0; o < 255; o++) {
- if (opts[o].slen && !opts[o].sent)
+ if (opts[o].slen != -1 && !opts[o].sent)
fill_one(m, o, &offset);
}
@@ -264,6 +264,9 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
".\xc0");
}
}
+
+ if (!opts[119].slen)
+ opts[119].slen = -1;
}
/**
@@ -313,6 +316,13 @@ int dhcp(const struct ctx *c, const struct pool *p)
offset += offsetof(struct msg, o);
+ for (i = 0; i < ARRAY_SIZE(opts); i++) {
+ if (!opts[i].slen)
+ opts[i].slen = -1;
+
+ opts[i].clen = -1;
+ }
+
while (opt_off + 2 < opt_len) {
const uint8_t *olen, *val;
uint8_t *type;
@@ -334,8 +344,9 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (opts[53].c[0] == DHCPDISCOVER) {
info("DHCP: offer to discover");
opts[53].s[0] = DHCPOFFER;
- } else if (opts[53].c[0] == DHCPREQUEST || !opts[53].clen) {
- info("%s: ack to request", opts[53].clen ? "DHCP" : "BOOTP");
+ } else if (opts[53].c[0] == DHCPREQUEST || opts[53].clen <= 0) {
+ info("%s: ack to request",
+ (opts[53].clen <= 0) ? "DHCP" : "BOOTP");
opts[53].s[0] = DHCPACK;
} else {
return -1;
@@ -374,6 +385,8 @@ int dhcp(const struct ctx *c, const struct pool *p)
((struct in_addr *)opts[6].s)[i] = c->ip4.dns[i];
opts[6].slen += sizeof(uint32_t);
}
+ if (!opts[6].slen)
+ opts[6].slen = -1;
if (!c->no_dhcp_dns_search)
opt_set_dns_search(c, sizeof(m->o));
--
@@ -36,9 +36,9 @@
/**
* struct opt - DHCP option
* @sent: Convenience flag, set while filling replies
- * @slen: Length of option defined for server
+ * @slen: Length of option defined for server, -1 if not going to be sent
* @s: Option payload from server
- * @clen: Length of option received from client
+ * @clen: Length of option received from client, -1 if not received
* @c: Option payload from client
*/
struct opt {
@@ -154,17 +154,17 @@ static int fill(struct msg *m)
* option 53 at the beginning of the list.
* Put it there explicitly, unless requested via option 55.
*/
- if (!memchr(opts[55].c, 53, opts[55].clen))
+ if (opts[55].clen > 0 && !memchr(opts[55].c, 53, opts[55].clen))
fill_one(m, 53, &offset);
for (i = 0; i < opts[55].clen; i++) {
o = opts[55].c[i];
- if (opts[o].slen)
+ if (opts[o].slen != -1)
fill_one(m, o, &offset);
}
for (o = 0; o < 255; o++) {
- if (opts[o].slen && !opts[o].sent)
+ if (opts[o].slen != -1 && !opts[o].sent)
fill_one(m, o, &offset);
}
@@ -264,6 +264,9 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
".\xc0");
}
}
+
+ if (!opts[119].slen)
+ opts[119].slen = -1;
}
/**
@@ -313,6 +316,13 @@ int dhcp(const struct ctx *c, const struct pool *p)
offset += offsetof(struct msg, o);
+ for (i = 0; i < ARRAY_SIZE(opts); i++) {
+ if (!opts[i].slen)
+ opts[i].slen = -1;
+
+ opts[i].clen = -1;
+ }
+
while (opt_off + 2 < opt_len) {
const uint8_t *olen, *val;
uint8_t *type;
@@ -334,8 +344,9 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (opts[53].c[0] == DHCPDISCOVER) {
info("DHCP: offer to discover");
opts[53].s[0] = DHCPOFFER;
- } else if (opts[53].c[0] == DHCPREQUEST || !opts[53].clen) {
- info("%s: ack to request", opts[53].clen ? "DHCP" : "BOOTP");
+ } else if (opts[53].c[0] == DHCPREQUEST || opts[53].clen <= 0) {
+ info("%s: ack to request",
+ (opts[53].clen <= 0) ? "DHCP" : "BOOTP");
opts[53].s[0] = DHCPACK;
} else {
return -1;
@@ -374,6 +385,8 @@ int dhcp(const struct ctx *c, const struct pool *p)
((struct in_addr *)opts[6].s)[i] = c->ip4.dns[i];
opts[6].slen += sizeof(uint32_t);
}
+ if (!opts[6].slen)
+ opts[6].slen = -1;
if (!c->no_dhcp_dns_search)
opt_set_dns_search(c, sizeof(m->o));
--
2.43.0
next prev parent reply other threads:[~2024-11-25 0:04 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-25 0:04 [PATCH 0/3] dhcp: Add support for Rapid Commit, broadcast replies Stefano Brivio
2024-11-25 0:04 ` Stefano Brivio [this message]
2024-11-25 1:08 ` [PATCH 1/3] dhcp: Use -1 as "missing option" length instead of 0 David Gibson
2024-11-25 8:30 ` Stefano Brivio
2024-11-25 9:18 ` David Gibson
2024-11-25 0:04 ` [PATCH 2/3] dhcp: Introduce support for Rapid Commit (option 80, RFC 4039) Stefano Brivio
2024-11-25 1:09 ` David Gibson
2024-11-25 0:04 ` [PATCH 3/3] dhcp: Honour broadcast flag (RFC 2131, 4.1) Stefano Brivio
2024-11-25 1:11 ` David Gibson
2024-11-25 8:30 ` Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241125000423.4131458-2-sbrivio@redhat.com \
--to=sbrivio@redhat.com \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).