Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy-DHCP support #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rjocoleman
Copy link

@rjocoleman rjocoleman commented Oct 10, 2024

This PR adds proxy-DHCP support:

  • What is proxy-DHCP?
    Proxy-DHCP allows a secondary DHCP server to provide boot configuration (such as next-server and boot file) while the primary DHCP server continues to assign IP addresses. This is useful in environments where modifying the primary DHCP server is not feasible, or the primary DHCP server doesn't have a static IP.

  • How proxy-DHCP works
    When a client sends out a DHCP request, the proxy-DHCP service will respond with boot options such as the next-server and boot filename, while leaving the IP address assignment to the primary DHCP server. This allows the client to chainload iPXE without requiring modifications to the existing DHCP server.

  • Pairs well with netboot.xyz PR #953
    This PR works alongside #953, which adds support for proxy-DHCP in the iPXE menus, allowing users to press a key to select the proxy offer and load netboot.xyz from there.

  • How to use it
    Set the DHCP_RANGE_START environment variable to the first IP in your network’s DHCP range. This will enable the optional proxy-DHCP mode. When enabled, dnsmasq calculates the range and handles proxy requests automatically.

    Edit: Ensure the docker container is on the same network e.g. --network host (or ipvlan, macvlan) so that it can receive broadcast DHCP messages and respond with its own broadcasts.

  • Moved dnsmasq config to a file
    To enable this functionality cleanly, the dnsmasq configuration has been moved into a config file, allowing for different config based on the presence of env DHCP_RANGE_START and substitution of some values via envsubst.

  • Proxy-DHCP behaviour
    When DHCP_RANGE_START is set, the provided dnsmasq will behave in proxy-DHCP mode (in addition to tftp), with the following key sections in the configuration:

    # DHCP Proxy range and enable verbose DHCP logging
    dhcp-range=${DHCP_RANGE_START},proxy
    log-dhcp
    leasefile-ro
    
    # Detect iPXE requests via user class (Option 175)
    dhcp-match=set:ipxe-bios,175,33
    dhcp-match=set:ipxe-efi,175,36
    
    # Serve appropriate bootloaders for non-iPXE clients (initial PXE boot)
    pxe-service=tag:bios,tag:!ipxe-ok,X86PC,"Legacy BIOS",netboot.xyz-undionly.kpxe
    ...

    This configuration sets up the proxy-DHCP to respond only to PXE clients (non-iPXE), serving the appropriate bootloaders for BIOS, UEFI, ARM64, and Raspberry Pi clients, while iPXE clients will be served an HTTP boot script.

  • Dynamic IP handling with envsubst
    The CONTAINER_IP is dynamically injected into the configuration using envsubst, after retrieving the container’s IP address at runtime from the container itself via init.sh. This ensures that the correct container next-server IP is set in the configuration.

  • User experience
    Users can start the container with the relevant environment variables set (DHCP_RANGE_START and optionally others). When a DHCP request is detected, this container sends a proxy offer with the next-server and boot file. With PR #953, netboot.xyz will detect the proxy next-server, allowing users to press p to boot from the proxy-DHCP server.

As it depends on a new env var being added DHCP_RANGE_START, this should be backwards compatible.

Docs & resources:
https://www.ipxe.org/appnote/proxydhcp
https://gist.github.com/NiKiZe/5c181471b96ac37a069af0a76688944d
https://thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html

@roger-
Copy link

roger- commented Nov 2, 2024

Does this accomplish the same as https://github.com/samdbmg/dhcp-netboot.xyz ?

EDIT

Gave it a test and got this error from docker compose:

process is missing required capability NET_ADMIN

Fixed by adding this to compose.yaml:

    cap_add:
      - NET_ADMIN

However spinning up a proxmox VM gives PXE-E16: No valid off received so something isn't working.

@rjocoleman
Copy link
Author

Does this accomplish the same as https://github.com/samdbmg/dhcp-netboot.xyz ?

Yes and no.

Yes - it offers up proxyDHCP with netboot.xyz files via dnsmasq

No:

  • samdbmg/dhcp-netboot.xyz is based on linuxserver/docker-netbootxyz, this is in netbootxyz/docker-netbootxyz
  • samdbmg/dhcp-netboot.xyz doesn't work with the current linuxserver/docker-netbootxyz due to changes in the underlying docker image structure (my understanding is the linuxserver release on 12.10.22 made it incompatible)
  • samdbmg/dhcp-netboot.xyz supports fewer architectures
pxe-service=tag:!ipxe-ok,X86PC,PXE,netboot.xyz-undionly.kpxe
pxe-service=tag:!ipxe-ok,BC_EFI,PXE,netboot.xyz.efi
pxe-service=tag:!ipxe-ok,X86-64_EFI,PXE,netboot.xyz.efi

this PR supports more

# Legacy BIOS (not iPXE)
pxe-service=tag:bios,tag:!ipxe-ok,X86PC,"Legacy BIOS",netboot.xyz-undionly.kpxe
# UEFI 32-bit (not iPXE)
pxe-service=tag:efi32,tag:!ipxe-ok,BC_EFI,"UEFI 32-bit",netboot.xyz.efi
# UEFI 64-bit (not iPXE)
pxe-service=tag:efi64,tag:!ipxe-ok,X86-64_EFI,"UEFI 64-bit",netboot.xyz.efi
# ARM64 UEFI (not iPXE)
pxe-service=tag:arm64-efi,tag:!ipxe-ok,ARM64_EFI,"ARM64 UEFI",netboot.xyz-arm64.efi
# Raspberry Pi Boot (using rpi4 tag, not iPXE)
pxe-service=tag:rpi4,tag:!ipxe-ok,0,"Raspberry Pi Boot",netboot.xyz-rpi4-snp.efi

EDIT

Gave it a test and got this error from docker compose:

process is missing required capability NET_ADMIN

Fixed by adding this to compose.yaml:

    cap_add:
      - NET_ADMIN

However spinning up a proxmox VM gives PXE-E16: No valid off received so something isn't working.

Thanks for the feedback on the cap_add. I hadn't used the example docker compose file and missed it.

I was also missing the required networking configuration.
The issue you've seen is due to the proxy DHCP response not being received in your VM, this is because it wasn't sent.
ProxyDHCP works by observing broadcast DHCP requests and responding to them, thus the docker container needs to be on the network in a way that it can receive broadcast messages.

The simplest way to do this is network_mode: host in docker compose yaml (or --network host for docker cli). But ipvlan/macvlan are better choices in some environments however they're slightly more involved for the user to set up.

I have added a commit that documents these requirements.

For reference my minimal docker-compose.yml based off the example from this repo is:

services:
  netbootxyz:
    build: .
    container_name: netbootxyz
    environment:
      - MENU_VERSION=2.0.82 # optional
      - NGINX_PORT=80 # optional
      - WEB_APP_PORT=3000 # optional
      - DHCP_RANGE_START=192.168.0.1  # optional, enables DHCP Proxy mode. set to your network's DHCP range first IP.
    # volumes:
    #   - /path/to/config:/config # optional
    #   - /path/to/assets:/assets # optional
    ports:
      - 3000:3000  # optional, destination should match ${WEB_APP_PORT} variable above.
      - 69:69/udp
      - 8080:80  # optional, destination should match ${NGINX_PORT} variable above.
    restart: unless-stopped
    cap_add:
      - NET_ADMIN  # required for DHCP Proxy mode.
    network_mode: host

As long as the machines/vms are on the same network proxydhcp responses should be offered up.

@roger-
Copy link

roger- commented Nov 2, 2024

Thanks, adding network_mode: host helped, but port redirection doesn't work in host mode so I had to disable that and edit the environment variables.

When I boot up the VM now I do see some activity in the docker logs, but nothing happens on the VM side:

netbootxyz  | 2024-11-02 21:38:25,513 INFO success: messages-log entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
netbootxyz  | 2024-11-02 21:38:22 info dnsmasq[18]: started, version 2.90 DNS disabled
netbootxyz  | 2024-11-02 21:38:51 warning dnsmasq[18]: overflow: 4 log entries lost
netbootxyz  | 2024-11-02 21:38:55 warning dnsmasq-dhcp[18]: no address range available for DHCP request via eth0
netbootxyz  | 2024-11-02 21:39:03 warning dnsmasq-dhcp[18]: no address range available for DHCP request via eth0
netbootxyz  | 2024-11-02 21:39:19 warning dnsmasq-dhcp[18]: no address range available for DHCP request via eth0

@rjocoleman
Copy link
Author

rjocoleman commented Nov 2, 2024

The error cited is saying that it's seeing a DHCP request in a range that the netboot.xyz proxydhcp dnsmasq is not configured to handle.

The most likely cause is that DHCP_RANGE_START env var in docker compose isn't configured to the first IP address in your dhcp range.

It should be noted that i retained the project pattern of not overwriting generated config, so if DHCP_RANGE_START is changed you need to delete the /config/dnsmasq/dnsmasq.conf if it's persisited to a volume (etc) for it to be regenerated with the changed env value.
(i dont like this behaviour but it is the pattern in this project for files being generated in the same way)

@rjocoleman
Copy link
Author

port redirection doesn't work in host mode so I had to disable that and edit the environment variables.

as an aside, this is why macvlan would be preferable in some cases. it's likely you have something on port 80 or 3000 on the host already. macvlan would give the container its own mac address and functionally let it sit on the network as if it was directly connected (to get it's own IP and interact with broadcast messages). it is more involved to set up and environment specific so i think out of scope for the minimum functional examples presented in the docs here

@roger-
Copy link

roger- commented Nov 2, 2024

Thanks, deleting ./config/ and checking the range fixed it. Also had to go to my Proxmox VM BIOS in Device Manager -> Secure Boot Configuration and then delete keys.

@h1ght
Copy link

h1ght commented Nov 3, 2024

this looks promising

@ColinHebert
Copy link

@antonym an chance to have this reviewed/merged?

@antonym
Copy link
Member

antonym commented Dec 8, 2024

Yeah, I looked at it a few weeks ago and it looks good, there were one or two things I wanted to bring up, but they slip my mind for the moment. I'll try and look at it once more and provide some feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants