In WireGuard on a sending peer, how is packet sent from the `wg0` interface to the UDP port?

Solution 1:

WireGuard does create an UDP socket and binds it to an address with special kernel functions: sock_create_kern and kernel_bind.

Here is WireGuard's source code for creating the IPv4 socket. There is a similar code for the IPv6 socket.

WireGuard binds the socket to an UDP tunnel with the setup_upd_tunnel call as well.