On a VLAN in proxy mode the site's DHCP server hands out addresses and
Rollcall answers only PXE. With dhcp_reservations in config.json the
machines still get the addresses Rollcall planned: each address on a
proxy-mode IPv4 subnet becomes a fixed lease (MAC of its interface ->
IP, hostname) on that server, reconciled on every address change and
every sync_interval.
- kea: control API (Kea 3.0's HTTP socket or the Control Agent; the
"service" field is learned) with the host_cmds hook, open source
since Kea 3.0: reservation-add/-del/-get-all, subnet ids from
config-get, ownership in user-context {"rollcall": machine},
config-write afterwards (persist) for reservations kept in memory;
a clear error without host_cmds (Debian's Kea 2.6 lacks it)
- openwrt: rpcd/ubus JSON-RPC, host sections in /etc/config/dhcp with
option rollcall, uci commit and dnsmasq reload; relogin on expiry
- reservations others made are never touched; one holding the MAC or
the address is a conflict
rollcall reservations [status] | sync [-dry]; API under
/api/v1/dhcp-reservations.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
336 lines
9.8 KiB
Go
336 lines
9.8 KiB
Go
package main
|
|
|
|
// DHCP reservations on the site's DHCP server. On a VLAN in proxy mode the
|
|
// site's server hands out addresses and Rollcall only answers PXE; with a
|
|
// reservation driver the machine still gets the address Rollcall planned for
|
|
// it, as a fixed lease (MAC -> IP) on that server (Infoblox's fixedaddress).
|
|
//
|
|
// kea ISC Kea with the host_cmds hook (open source since Kea 3.0):
|
|
// reservation-add/-del/-get-all over the control API; Rollcall's
|
|
// reservations carry user-context {"rollcall": <machine>}
|
|
// openwrt rpcd/ubus: "host" sections in /etc/config/dhcp with an
|
|
// option rollcall '<machine>'
|
|
//
|
|
// Reservations Rollcall did not make are never changed or deleted; one in
|
|
// the way (same MAC or address) is reported as a conflict.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type DHCPResConfig struct {
|
|
Provider string `json:"provider"` // "" | kea | openwrt
|
|
URL string `json:"url"` // kea: control API (http://kea:8000); openwrt: http(s)://router (rpcd at /ubus)
|
|
User string `json:"user"`
|
|
PasswordFile string `json:"password_file"`
|
|
CAFile string `json:"ca_file"`
|
|
SyncInterval string `json:"sync_interval"`
|
|
// kea: write the configuration file after changes (reservations kept in
|
|
// the server's memory, i.e. no hosts database); default true
|
|
Persist *bool `json:"persist,omitempty"`
|
|
}
|
|
|
|
// dhcpRes is one fixed lease.
|
|
type dhcpRes struct {
|
|
MAC string `json:"mac"`
|
|
IP string `json:"ip"`
|
|
Hostname string `json:"hostname,omitempty"`
|
|
Machine string `json:"machine,omitempty"`
|
|
Subnet string `json:"subnet"` // CIDR
|
|
ref string // provider's handle (OpenWrt section)
|
|
}
|
|
|
|
func (r dhcpRes) key() string { return r.Subnet + " " + r.MAC }
|
|
|
|
type dhcpResProvider interface {
|
|
Name() string
|
|
// Owned: Rollcall's reservations in these subnets, and the MAC/IP pairs others hold.
|
|
Owned(ctx context.Context, subnets []string) (owned []dhcpRes, foreign []dhcpRes, err error)
|
|
Put(ctx context.Context, r dhcpRes) error
|
|
Delete(ctx context.Context, r dhcpRes) error
|
|
// Done is called after a pass that changed something (Kea: config-write).
|
|
Done(ctx context.Context) error
|
|
}
|
|
|
|
type dhcpResReport struct {
|
|
Provider string `json:"provider"`
|
|
At time.Time `json:"at"`
|
|
DryRun bool `json:"dry_run,omitempty"`
|
|
Put []string `json:"put"`
|
|
Deleted []string `json:"deleted"`
|
|
Conflicts []string `json:"conflicts"`
|
|
Skipped []string `json:"skipped,omitempty"` // addresses without a known MAC
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func (r *dhcpResReport) fail(e string) { r.Error = joinErr(r.Error, e) }
|
|
|
|
type dhcpResState struct {
|
|
mu sync.Mutex
|
|
last dhcpResReport
|
|
kick chan struct{}
|
|
}
|
|
|
|
func newDHCPResProvider(c DHCPResConfig) (dhcpResProvider, error) {
|
|
if c.Provider == "" {
|
|
return nil, nil
|
|
}
|
|
if c.URL == "" {
|
|
return nil, errors.New("dhcp_reservations.url is required")
|
|
}
|
|
pw := ""
|
|
if c.PasswordFile != "" {
|
|
b, err := os.ReadFile(c.PasswordFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("dhcp_reservations.password_file: %w", err)
|
|
}
|
|
pw = strings.TrimSpace(string(b))
|
|
}
|
|
hc, err := httpClientCA(c.CAFile, 30*time.Second)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("dhcp_reservations.ca_file: %w", err)
|
|
}
|
|
switch c.Provider {
|
|
case "kea":
|
|
return &keaProvider{url: strings.TrimRight(c.URL, "/"), user: c.User, pass: pw, hc: hc, persist: c.Persist == nil || *c.Persist}, nil
|
|
case "openwrt":
|
|
return &openwrtProvider{url: strings.TrimRight(c.URL, "/") + "/ubus", user: orDefault(c.User, "root"), pass: pw, hc: hc}, nil
|
|
}
|
|
return nil, fmt.Errorf("dhcp_reservations.provider %q: kea or openwrt", c.Provider)
|
|
}
|
|
|
|
func (a *App) startDHCPRes() error {
|
|
p, err := newDHCPResProvider(a.cfg.DHCPRes)
|
|
if err != nil || p == nil {
|
|
return err
|
|
}
|
|
a.dhcpRes = p
|
|
a.dhcpResSt = &dhcpResState{kick: make(chan struct{}, 1)}
|
|
every := 5 * time.Minute
|
|
if d, err := time.ParseDuration(a.cfg.DHCPRes.SyncInterval); err == nil && d >= 10*time.Second {
|
|
every = d
|
|
}
|
|
go func() {
|
|
a.kickDHCPRes()
|
|
for {
|
|
select {
|
|
case <-a.dhcpResSt.kick:
|
|
case <-time.After(every):
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
rep := a.reconcileDHCPRes(ctx, false)
|
|
cancel()
|
|
if rep.Error != "" {
|
|
log.Printf("dhcp reservations %s: %s", p.Name(), rep.Error)
|
|
} else if n := len(rep.Put) + len(rep.Deleted) + len(rep.Conflicts); n > 0 {
|
|
log.Printf("dhcp reservations %s: put %d, deleted %d, conflicts %d %v", p.Name(), len(rep.Put), len(rep.Deleted), len(rep.Conflicts), rep.Conflicts)
|
|
}
|
|
}
|
|
}()
|
|
log.Printf("dhcp reservations: %s at %s", p.Name(), a.cfg.DHCPRes.URL)
|
|
return nil
|
|
}
|
|
|
|
func (a *App) kickDHCPRes() {
|
|
if a.dhcpResSt == nil {
|
|
return
|
|
}
|
|
select {
|
|
case a.dhcpResSt.kick <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// desiredDHCPRes: every address Rollcall gave a machine on a proxy-mode
|
|
// subnet, with the MAC of the interface it belongs to.
|
|
func desiredDHCPRes(ips []IPAddr, subnets []Subnet, machines map[string]*Machine) (map[string]dhcpRes, []string) {
|
|
want := map[string]dhcpRes{}
|
|
var skipped []string
|
|
for _, x := range ips {
|
|
sn := subnetFor(subnets, x.IP)
|
|
m := machines[x.MachineID]
|
|
if sn == nil || sn.DHCPMode != "proxy" || m == nil || len(m.MACs) == 0 {
|
|
continue
|
|
}
|
|
ad, ok := parseAddr(x.IP)
|
|
if !ok || !ad.Is4() { // DHCPv6 reservations key on DUIDs, not MACs
|
|
continue
|
|
}
|
|
mac := ""
|
|
if x.IP == deref(m.MgmtIP) || x.Iface == "" {
|
|
mac = m.MACs[0]
|
|
}
|
|
for _, n := range machineNICs(m) {
|
|
if n.Name == x.Iface && n.MAC != "" {
|
|
mac = n.MAC
|
|
}
|
|
}
|
|
if mac == "" {
|
|
skipped = append(skipped, x.IP+" ("+x.Iface+": no single MAC)")
|
|
continue
|
|
}
|
|
r := dhcpRes{MAC: mac, IP: ad.String(), Hostname: deref(m.NodeName), Machine: m.ID, Subnet: sn.CIDR}
|
|
want[r.key()] = r
|
|
}
|
|
return want, skipped
|
|
}
|
|
|
|
func (a *App) reconcileDHCPRes(ctx context.Context, dry bool) dhcpResReport {
|
|
rep := dhcpResReport{At: time.Now().UTC(), DryRun: dry, Put: []string{}, Deleted: []string{}, Conflicts: []string{}}
|
|
if a.dhcpRes == nil {
|
|
rep.Error = "no DHCP reservation provider configured (dhcp_reservations.provider in config.json)"
|
|
return rep
|
|
}
|
|
rep.Provider = a.dhcpRes.Name()
|
|
a.dhcpResSt.mu.Lock()
|
|
defer a.dhcpResSt.mu.Unlock()
|
|
defer func() {
|
|
if !dry {
|
|
a.dhcpResSt.last = rep
|
|
}
|
|
}()
|
|
ips, err := a.store.IPs(ctx, "", "")
|
|
if err != nil {
|
|
rep.Error = err.Error()
|
|
return rep
|
|
}
|
|
list, err := a.store.List(ctx)
|
|
if err != nil {
|
|
rep.Error = err.Error()
|
|
return rep
|
|
}
|
|
machines := map[string]*Machine{}
|
|
for _, m := range list {
|
|
machines[m.ID] = m
|
|
}
|
|
subnets := a.subnetsSnapshot()
|
|
want, skipped := desiredDHCPRes(ips, subnets, machines)
|
|
rep.Skipped = skipped
|
|
var proxied []string
|
|
for _, sn := range subnets {
|
|
if sn.DHCPMode == "proxy" && familyOf(sn.CIDR) == 4 {
|
|
proxied = append(proxied, sn.CIDR)
|
|
}
|
|
}
|
|
if len(proxied) == 0 {
|
|
return rep
|
|
}
|
|
a.dhcpResWith(ctx, want, proxied, &rep)
|
|
return rep
|
|
}
|
|
|
|
func (a *App) dhcpResWith(ctx context.Context, want map[string]dhcpRes, subnets []string, rep *dhcpResReport) {
|
|
owned, foreign, err := a.dhcpRes.Owned(ctx, subnets)
|
|
if err != nil {
|
|
rep.Error = err.Error()
|
|
return
|
|
}
|
|
have := map[string]dhcpRes{}
|
|
for _, o := range owned {
|
|
have[o.key()] = o
|
|
}
|
|
keys := make([]string, 0, len(want))
|
|
for k := range want {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
changed := false
|
|
for _, k := range keys {
|
|
w := want[k]
|
|
if o, ok := have[k]; ok {
|
|
delete(have, k)
|
|
if o.IP == w.IP && o.Hostname == w.Hostname && o.Machine == w.Machine {
|
|
continue
|
|
}
|
|
if rep.DryRun {
|
|
rep.Put = append(rep.Put, w.MAC+" "+w.IP)
|
|
continue
|
|
}
|
|
if err := a.dhcpRes.Delete(ctx, o); err != nil { // replaced: the old one goes first
|
|
rep.fail(fmt.Sprintf("replace %s: %v", w.MAC, err))
|
|
continue
|
|
}
|
|
changed = true
|
|
} else if c := clash(foreign, w); c != "" {
|
|
rep.Conflicts = append(rep.Conflicts, fmt.Sprintf("%s %s: %s", w.MAC, w.IP, c))
|
|
continue
|
|
}
|
|
if rep.DryRun {
|
|
rep.Put = append(rep.Put, w.MAC+" "+w.IP)
|
|
continue
|
|
}
|
|
if err := a.dhcpRes.Put(ctx, w); err != nil {
|
|
rep.fail(fmt.Sprintf("put %s %s: %v", w.MAC, w.IP, err))
|
|
continue
|
|
}
|
|
changed = true
|
|
rep.Put = append(rep.Put, w.MAC+" "+w.IP)
|
|
}
|
|
gone := make([]dhcpRes, 0, len(have))
|
|
for _, o := range have {
|
|
gone = append(gone, o)
|
|
}
|
|
sort.Slice(gone, func(i, j int) bool { return gone[i].key() < gone[j].key() })
|
|
for _, o := range gone {
|
|
if !rep.DryRun {
|
|
if err := a.dhcpRes.Delete(ctx, o); err != nil {
|
|
rep.fail(fmt.Sprintf("delete %s: %v", o.MAC, err))
|
|
continue
|
|
}
|
|
changed = true
|
|
}
|
|
rep.Deleted = append(rep.Deleted, o.MAC+" "+o.IP)
|
|
}
|
|
if changed {
|
|
if err := a.dhcpRes.Done(ctx); err != nil {
|
|
rep.fail("save: " + err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
// clash: a reservation someone else holds for this MAC or this address.
|
|
func clash(foreign []dhcpRes, w dhcpRes) string {
|
|
for _, f := range foreign {
|
|
if f.Subnet != w.Subnet {
|
|
continue
|
|
}
|
|
switch {
|
|
case strings.EqualFold(f.MAC, w.MAC):
|
|
return "the MAC is reserved there for " + f.IP + " (not Rollcall's)"
|
|
case f.IP == w.IP:
|
|
return "the address is reserved there for " + f.MAC + " (not Rollcall's)"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (a *App) handleDHCPRes(w http.ResponseWriter, r *http.Request) {
|
|
if a.dhcpRes == nil {
|
|
writeJSON(w, 200, obj{"provider": "", "comment": "no DHCP reservation provider (dhcp_reservations in config.json)"})
|
|
return
|
|
}
|
|
a.dhcpResSt.mu.Lock()
|
|
last := a.dhcpResSt.last
|
|
a.dhcpResSt.mu.Unlock()
|
|
writeJSON(w, 200, obj{"provider": a.dhcpRes.Name(), "url": a.cfg.DHCPRes.URL, "last_sync": last})
|
|
}
|
|
|
|
func (a *App) handleDHCPResSync(w http.ResponseWriter, r *http.Request) {
|
|
if a.dhcpRes == nil {
|
|
fail(w, 409, errors.New("no DHCP reservation provider configured"))
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
|
|
defer cancel()
|
|
writeJSON(w, 200, a.reconcileDHCPRes(ctx, r.URL.Query().Get("dry") == "1"))
|
|
}
|