Skip to content

Commit

Permalink
[mac2ip] add leasequery response.
Browse files Browse the repository at this point in the history
Add support for IP and MAC leasequeries. Respond with LEASEUNKNOWN for
client-identifier queries (no way to map that to MAC or IP). Add
a ip->mac for looking up MAC from the IP (ciaddr).
  • Loading branch information
kanaka committed Jan 11, 2024
1 parent 121e834 commit 5717283
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/dhcp/mac2ip_server.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,60 @@ Options:
:mac-end (-> r :mac-end addrs/mac->int)})]
(assoc cfg :ranges ranges)))

(defn mac->ip [msg-map ranges]
(let [mac-int (addrs/mac->int (:chaddr msg-map))
(defn mac->ip [chaddr ranges]
(let [mac-int (addrs/mac->int chaddr)
r (first (filter #(and (>= mac-int (:mac-start %))
(<= mac-int (:mac-end %)))
ranges))]
(when r
(addrs/int->ip (+ (:ip-start r) (- mac-int (:mac-start r)))))))

(defn mac2ip-handler [{:keys [ranges server-info log-msg
log-level] :as cfg} msg-map]
(let [ip (mac->ip msg-map ranges)]
(if (not ip)
(do
(log-msg :error (str "MAC " (:chaddr msg-map) " is out of range"))
nil)
(defn ip->mac [ip ranges]
(let [ip-int (addrs/ip->int ip)
r (first (filter #(and (>= ip-int (:ip-start %))
(<= ip-int (:ip-end %)))
ranges))]
(when r
(addrs/int->mac (+ (:mac-start r) (- ip-int (:ip-start r)))))))

(defn mac2ip-handler [{:keys [ranges server-info log-msg log-level] :as cfg}
{:keys [chaddr ciaddr] :as msg-map}]
(if (= :LEASEQUERY (:opt/msg-type msg-map))
(let [[mac ip kind] (cond
ciaddr [(ip->mac ciaddr ranges) ciaddr "IP"]
chaddr [chaddr (mac->ip chaddr ranges) "MAC"]
:else [nil nil "Client-identifier"])
msg-resp (if (and mac ip)
(merge
(select-keys msg-map [:giaddr :opt/relay-agent-info])
{:opt/msg-type :LEASEACTIVE
:yiaddr ip})
{:opt/msg-type :LEASEUNKNOWN})]
(do
(condp = log-level
2 (log-msg :info (str "Assigning " ip " to " (:chaddr msg-map)))
2 (log-msg :info (str "Leasequery by " kind " "
(condp = kind
"IP" (str ip " to " chaddr)
"MAC" (str chaddr " to " ip)
:else (str "unsupported"))))
nil)
(merge (dhcp/default-response msg-map server-info)
(:fields cfg) ;; config file field/option overrides
msg-resp)))

(let [ip (mac->ip chaddr ranges)]
(if (not ip)
(do
(log-msg :error (str "MAC " chaddr " is out of range"))
nil)
(merge
(dhcp/default-response msg-map server-info)
(select-keys msg-map [:giaddr :opt/relay-agent-info])
(:fields cfg) ;; config file field/option overrides
{:yiaddr ip})))))
(do
(condp = log-level
2 (log-msg :info (str "Assigning " ip " to " chaddr))
nil)
(merge (dhcp/default-response msg-map server-info)
(select-keys msg-map [:giaddr :opt/relay-agent-info])
(:fields cfg) ;; config file field/option overrides
{:yiaddr ip}))))))

(defn worker [user-cfg]
(let [log-msg logging/log-message
Expand Down

0 comments on commit 5717283

Please sign in to comment.