Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/devices/tap_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ static void handle_dhcp(tap_dev_t* tap, const uint8_t* buffer, size_t size, net_

uint8_t frame[TAP_FRAME_SIZE];
uint8_t* ipv4 = create_eth_frame(tap, frame, ETH2_IPv4);
uint8_t* udp = create_ipv4_frame(ipv4, 278 + UDP_HDR_SIZE, IP_PROTO_UDP, (const uint8_t*)"\xFF\xFF\xFF\xFF", GATEWAY_IP);
const uint8_t* reply_ip = read_uint32_be_m(buffer + 12) ? buffer + 12 : (const uint8_t*)"\xFF\xFF\xFF\xFF";
uint8_t* udp = create_ipv4_frame(ipv4, 278 + UDP_HDR_SIZE, IP_PROTO_UDP, reply_ip, GATEWAY_IP);
uint8_t* dhcp = create_udp_datagram(udp, 278, 68, 67);

dhcp[0] = OP_RESPONSE;
Expand Down Expand Up @@ -450,15 +451,16 @@ static void handle_udp(tap_dev_t* tap, const uint8_t* buffer, size_t size, net_a
}
udp_size -= UDP_HDR_SIZE;

// Renewals use the leased source IP; intercept before any UDP forwarding entry.
if (src->type == NET_TYPE_IPV4 && src->port == 68 && dst->port == 67
&& (memcmp(dst->ip, GATEWAY_IP, PLEN_IPv4) == 0 || read_uint32_be_m(dst->ip) == 0xFFFFFFFF)) {
handle_dhcp(tap, udb_buff, udp_size, src);
return;
}

spin_lock(&tap->lock);
tap_sock_t* ts = (tap_sock_t*)hashmap_get(&tap->udp_ports, src->port);
if (ts == NULL) {
if (dst->port == 67 && (read_uint32_be_m(src->ip) == 0)) {
spin_unlock(&tap->lock);
handle_dhcp(tap, udb_buff, udp_size, src);
return;
}

net_sock_t* sock = net_udp_bind(NET_IPV4_ANY);
net_sock_set_blocking(sock, false);
if (sock) {
Expand Down
Loading