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>
205 lines
5.8 KiB
Go
205 lines
5.8 KiB
Go
package main
|
|
|
|
// OpenWrt: static leases are "host" sections of /etc/config/dhcp, reached
|
|
// through rpcd's ubus JSON-RPC (POST /ubus). Rollcall's carry option
|
|
// rollcall '<machine>'. The rpcd user needs uci read/write on "dhcp" (and rc
|
|
// to reload dnsmasq): see the README for the ACL.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type openwrtProvider struct {
|
|
url, user, pass string
|
|
hc *http.Client
|
|
mu sync.Mutex
|
|
sid string
|
|
}
|
|
|
|
func (o *openwrtProvider) Name() string { return "openwrt" }
|
|
|
|
var errUbusDenied = errors.New("ubus: access denied")
|
|
|
|
// rpc is one ubus call; it logs in (again) when the session is missing or expired.
|
|
func (o *openwrtProvider) rpc(ctx context.Context, object, method string, args map[string]any) (json.RawMessage, error) {
|
|
o.mu.Lock()
|
|
sid := o.sid
|
|
o.mu.Unlock()
|
|
if sid == "" {
|
|
if err := o.login(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
sid = o.sid
|
|
}
|
|
res, err := o.raw(ctx, sid, object, method, args)
|
|
if errors.Is(err, errUbusDenied) { // expired session
|
|
if err := o.login(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
res, err = o.raw(ctx, o.sid, object, method, args)
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (o *openwrtProvider) login(ctx context.Context) error {
|
|
res, err := o.raw(ctx, "00000000000000000000000000000000", "session", "login", map[string]any{"username": o.user, "password": o.pass})
|
|
if err != nil {
|
|
return fmt.Errorf("openwrt login: %w", err)
|
|
}
|
|
var s struct {
|
|
SID string `json:"ubus_rpc_session"`
|
|
}
|
|
if json.Unmarshal(res, &s) != nil || s.SID == "" {
|
|
return errors.New("openwrt login: no session")
|
|
}
|
|
o.mu.Lock()
|
|
o.sid = s.SID
|
|
o.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
var ubusErrors = map[int]string{1: "invalid command", 2: "invalid argument", 3: "method not found", 4: "not found", 5: "no data", 6: "permission denied", 7: "timeout", 8: "not supported", 9: "unknown error", 10: "connection failed"}
|
|
|
|
func (o *openwrtProvider) raw(ctx context.Context, sid, object, method string, args map[string]any) (json.RawMessage, error) {
|
|
if args == nil {
|
|
args = map[string]any{}
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "call", "params": []any{sid, object, method, args}})
|
|
req, err := http.NewRequestWithContext(ctx, "POST", o.url, bytes.NewReader(b))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := o.hc.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
data, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
|
|
var out struct {
|
|
Result []json.RawMessage `json:"result"`
|
|
Error *struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
return nil, fmt.Errorf("ubus %s %s: %s %s", object, method, resp.Status, oneLine(string(data)))
|
|
}
|
|
if out.Error != nil {
|
|
if out.Error.Code == -32002 {
|
|
return nil, errUbusDenied
|
|
}
|
|
return nil, fmt.Errorf("ubus %s %s: %s", object, method, out.Error.Message)
|
|
}
|
|
if len(out.Result) == 0 {
|
|
return nil, fmt.Errorf("ubus %s %s: empty answer", object, method)
|
|
}
|
|
var code int
|
|
_ = json.Unmarshal(out.Result[0], &code)
|
|
if code != 0 {
|
|
if code == 6 {
|
|
return nil, fmt.Errorf("ubus %s %s: permission denied (rpcd ACL for this user)", object, method)
|
|
}
|
|
return nil, fmt.Errorf("ubus %s %s: %s", object, method, orDefault(ubusErrors[code], fmt.Sprint(code)))
|
|
}
|
|
if len(out.Result) < 2 {
|
|
return json.RawMessage("{}"), nil
|
|
}
|
|
return out.Result[1], nil
|
|
}
|
|
|
|
func (o *openwrtProvider) Owned(ctx context.Context, subnets []string) ([]dhcpRes, []dhcpRes, error) {
|
|
res, err := o.rpc(ctx, "uci", "get", map[string]any{"config": "dhcp", "type": "host"})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "not found") { // no host sections yet
|
|
return nil, nil, nil
|
|
}
|
|
return nil, nil, err
|
|
}
|
|
var got struct {
|
|
Values map[string]map[string]any `json:"values"`
|
|
}
|
|
if err := json.Unmarshal(res, &got); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
var prefixes []netip.Prefix
|
|
for _, s := range subnets {
|
|
if p, err := netip.ParsePrefix(s); err == nil {
|
|
prefixes = append(prefixes, p)
|
|
}
|
|
}
|
|
var owned, foreign []dhcpRes
|
|
for name, sec := range got.Values {
|
|
ip, _ := sec["ip"].(string)
|
|
ad, err := netip.ParseAddr(ip)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
subnet := ""
|
|
for _, p := range prefixes {
|
|
if p.Contains(ad) {
|
|
subnet = p.Masked().String()
|
|
}
|
|
}
|
|
if subnet == "" {
|
|
continue
|
|
}
|
|
var mac string
|
|
switch v := sec["mac"].(type) { // a string, or a list of MACs
|
|
case string:
|
|
mac = strings.Fields(v)[0]
|
|
case []any:
|
|
if len(v) > 0 {
|
|
mac, _ = v[0].(string)
|
|
}
|
|
}
|
|
r := dhcpRes{MAC: normMAC(mac), IP: ad.String(), Subnet: subnet, ref: name}
|
|
r.Hostname, _ = sec["name"].(string)
|
|
if m, _ := sec["rollcall"].(string); m != "" {
|
|
r.Machine = m
|
|
owned = append(owned, r)
|
|
} else {
|
|
foreign = append(foreign, r)
|
|
}
|
|
}
|
|
return owned, foreign, nil
|
|
}
|
|
|
|
func (o *openwrtProvider) Put(ctx context.Context, r dhcpRes) error {
|
|
values := map[string]any{"mac": r.MAC, "ip": r.IP, "rollcall": r.Machine}
|
|
if r.Hostname != "" {
|
|
values["name"] = r.Hostname
|
|
}
|
|
_, err := o.rpc(ctx, "uci", "add", map[string]any{"config": "dhcp", "type": "host", "values": values})
|
|
return err
|
|
}
|
|
|
|
func (o *openwrtProvider) Delete(ctx context.Context, r dhcpRes) error {
|
|
if r.ref == "" {
|
|
return errors.New("no section to delete")
|
|
}
|
|
_, err := o.rpc(ctx, "uci", "delete", map[string]any{"config": "dhcp", "section": r.ref})
|
|
return err
|
|
}
|
|
|
|
// Done commits /etc/config/dhcp and reloads dnsmasq.
|
|
func (o *openwrtProvider) Done(ctx context.Context) error {
|
|
if _, err := o.rpc(ctx, "uci", "commit", map[string]any{"config": "dhcp"}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := o.rpc(ctx, "rc", "init", map[string]any{"name": "dnsmasq", "action": "reload"}); err != nil {
|
|
return fmt.Errorf("committed, but dnsmasq not reloaded: %w", err)
|
|
}
|
|
return nil
|
|
}
|