dhcp: Use tap_udp4_send() helper in dhcp()

The IPv4 specific dhcp() manually constructs L2 and IP headers to send its
DHCP reply packet, unlike its IPv6 equivalent in dhcpv6.c which uses the
tap_udp6_send() helper.  Now that we've broaded the parameters to
tap_udp4_send() we can use it in dhcp() to avoid some duplicated logic.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2022-10-19 11:43:57 +11:00 committed by Stefano Brivio
parent 2dbc622f54
commit c6845f60a0
2 changed files with 2 additions and 17 deletions

18
dhcp.c
View file

@ -363,22 +363,8 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (!c->no_dhcp_dns_search) if (!c->no_dhcp_dns_search)
opt_set_dns_search(c, sizeof(m->o)); opt_set_dns_search(c, sizeof(m->o));
uh->len = htons(len = offsetof(struct msg, o) + fill(m) + sizeof(*uh)); len = offsetof(struct msg, o) + fill(m);
uh->source = htons(67); tap_udp4_send(c, c->ip4.gw, 67, c->ip4.addr, 68, m, len);
uh->dest = htons(68);
csum_udp4(uh, c->ip4.gw, c->ip4.addr, uh + 1, len - sizeof(*uh));
iph->tot_len = htons(len += sizeof(*iph));
iph->daddr = c->ip4.addr;
iph->saddr = c->ip4.gw;
csum_ip4_header(iph);
len += sizeof(*eh);
memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
memcpy(eh->h_source, c->mac, ETH_ALEN);
if (tap_send(c, eh, len) < 0)
perror("DHCP: send");
return 1; return 1;
} }

1
tap.c
View file

@ -170,7 +170,6 @@ static void *tap_push_ip4h(char *buf, in_addr_t src, in_addr_t dst,
* @in: UDP payload contents (not including UDP header) * @in: UDP payload contents (not including UDP header)
* @len: UDP payload length (not including UDP header) * @len: UDP payload length (not including UDP header)
*/ */
/* cppcheck-suppress unusedFunction */
void tap_udp4_send(const struct ctx *c, in_addr_t src, in_port_t sport, void tap_udp4_send(const struct ctx *c, in_addr_t src, in_port_t sport,
in_addr_t dst, in_port_t dport, in_addr_t dst, in_port_t dport,
const void *in, size_t len) const void *in, size_t len)