aboutgitcodebugslistschat
path: root/dhcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'dhcp.c')
-rw-r--r--dhcp.c72
1 files changed, 51 insertions, 21 deletions
diff --git a/dhcp.c b/dhcp.c
index aa9f59d..d8515aa 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 {
@@ -68,6 +68,11 @@ static struct opt opts[255];
*/
void dhcp_init(void)
{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(opts); i++)
+ opts[i].slen = -1;
+
opts[1] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, }; /* Mask */
opts[3] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, }; /* Router */
opts[51] = (struct opt) { 0, 4, { 0xff,
@@ -107,6 +112,8 @@ struct msg {
uint32_t xid;
uint16_t secs;
uint16_t flags;
+#define FLAG_BROADCAST htons_constant(0x8000)
+
uint32_t ciaddr;
struct in_addr yiaddr;
uint32_t siaddr;
@@ -154,17 +161,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 +271,9 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
".\xc0");
}
}
+
+ if (!opts[119].slen)
+ opts[119].slen = -1;
}
/**
@@ -276,10 +286,11 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
int dhcp(const struct ctx *c, const struct pool *p)
{
size_t mlen, dlen, offset = 0, opt_len, opt_off = 0;
+ char macstr[ETH_ADDRSTRLEN];
+ struct in_addr mask, dst;
const struct ethhdr *eh;
const struct iphdr *iph;
const struct udphdr *uh;
- struct in_addr mask;
unsigned int i;
struct msg *m;
@@ -312,6 +323,9 @@ int dhcp(const struct ctx *c, const struct pool *p)
offset += offsetof(struct msg, o);
+ for (i = 0; i < ARRAY_SIZE(opts); i++)
+ opts[i].clen = -1;
+
while (opt_off + 2 < opt_len) {
const uint8_t *olen, *val;
uint8_t *type;
@@ -330,36 +344,44 @@ int dhcp(const struct ctx *c, const struct pool *p)
opt_off += *olen + 2;
}
- 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");
+ opts[80].slen = -1;
+ if (opts[53].clen > 0 && opts[53].c[0] == DHCPDISCOVER) {
+ if (opts[80].clen == -1) {
+ info("DHCP: offer to discover");
+ opts[53].s[0] = DHCPOFFER;
+ } else {
+ info("DHCP: ack to discover (Rapid Commit)");
+ opts[53].s[0] = DHCPACK;
+ opts[80].slen = 0;
+ }
+ } else if (opts[53].clen <= 0 || opts[53].c[0] == DHCPREQUEST) {
+ info("%s: ack to request", /* DHCP needs a valid message type */
+ (opts[53].clen <= 0) ? "BOOTP" : "DHCP");
opts[53].s[0] = DHCPACK;
} else {
return -1;
}
- info(" from %02x:%02x:%02x:%02x:%02x:%02x",
- m->chaddr[0], m->chaddr[1], m->chaddr[2],
- m->chaddr[3], m->chaddr[4], m->chaddr[5]);
+ info(" from %s", eth_ntop(m->chaddr, macstr, sizeof(macstr)));
m->yiaddr = c->ip4.addr;
mask.s_addr = htonl(0xffffffff << (32 - c->ip4.prefix_len));
- memcpy(opts[1].s, &mask, sizeof(mask));
- memcpy(opts[3].s, &c->ip4.gw, sizeof(c->ip4.gw));
- memcpy(opts[54].s, &c->ip4.gw, sizeof(c->ip4.gw));
+ memcpy(opts[1].s, &mask, sizeof(mask));
+ memcpy(opts[3].s, &c->ip4.guest_gw, sizeof(c->ip4.guest_gw));
+ memcpy(opts[54].s, &c->ip4.our_tap_addr, sizeof(c->ip4.our_tap_addr));
/* If the gateway is not on the assigned subnet, send an option 121
* (Classless Static Routing) adding a dummy route to it.
*/
if ((c->ip4.addr.s_addr & mask.s_addr)
- != (c->ip4.gw.s_addr & mask.s_addr)) {
+ != (c->ip4.guest_gw.s_addr & mask.s_addr)) {
/* a.b.c.d/32:0.0.0.0, 0:a.b.c.d */
opts[121].slen = 14;
opts[121].s[0] = 32;
- memcpy(opts[121].s + 1, &c->ip4.gw, sizeof(c->ip4.gw));
- memcpy(opts[121].s + 10, &c->ip4.gw, sizeof(c->ip4.gw));
+ memcpy(opts[121].s + 1,
+ &c->ip4.guest_gw, sizeof(c->ip4.guest_gw));
+ memcpy(opts[121].s + 10,
+ &c->ip4.guest_gw, sizeof(c->ip4.guest_gw));
}
if (c->mtu != -1) {
@@ -373,12 +395,20 @@ 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));
dlen = offsetof(struct msg, o) + fill(m);
- tap_udp4_send(c, c->ip4.gw, 67, c->ip4.addr, 68, m, dlen);
+
+ if (m->flags & FLAG_BROADCAST)
+ dst = in4addr_broadcast;
+ else
+ dst = c->ip4.addr;
+
+ tap_udp4_send(c, c->ip4.our_tap_addr, 67, dst, 68, m, dlen);
return 1;
}