See the CAN hat docs for more detailed information.
You must enable the SPI interface by adding the the following lines to /boot/firmware/config.txt.
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
Save the contents and run sudo reboot.
Verify that the driver is running correctly using dmesg | grep -i '\(can\|spi\)'. If you see something along the lines of "mcp251x spi0.0 can0: MCP2515 successfully initialized" then you are good to go.
Use the following commands to configure the CAN interface. You can modify the bitrate by changing the number at the end of the first line.
sudo ip link set can0 up type can bitrate 1000000
sudo ifconfig can0 txqueuelen 65536
sudo ifconfig can0 up
You can bring the interface down with
sudo ip link set can0 down
And enable internal loopback with
sudo ip link set can0 up type can bitrate 1000000 loopback on
More documentation on CAN networking in the Linux kernel are available on the Linux kernel docs.
You can test using the provided Python script
python3 test.py --host 127.0.0.1 --port 5000 --compose --std --id 0x678 --data "12 34 56 78 00"