1 /** @file
2  *  @brief DHCPv4 Server API
3  */
4 
5 /*
6  * Copyright (c) 2024 Nordic Semiconductor ASA
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef ZEPHYR_INCLUDE_NET_DHCPV4_SERVER_H_
12 #define ZEPHYR_INCLUDE_NET_DHCPV4_SERVER_H_
13 
14 #include <zephyr/net/net_ip.h>
15 #include <zephyr/sys_clock.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /**
22  * @brief DHCPv4 server
23  * @defgroup dhcpv4_server DHCPv4 server
24  * @ingroup networking
25  * @{
26  */
27 
28 /** @cond INTERNAL_HIDDEN */
29 
30 struct net_if;
31 
32 #define DHCPV4_CLIENT_ID_MAX_SIZE 20
33 
34 enum dhcpv4_server_addr_state {
35 	DHCPV4_SERVER_ADDR_FREE,
36 	DHCPV4_SERVER_ADDR_RESERVED,
37 	DHCPV4_SERVER_ADDR_ALLOCATED,
38 	DHCPV4_SERVER_ADDR_DECLINED,
39 };
40 
41 struct dhcpv4_client_id {
42 	uint8_t buf[DHCPV4_CLIENT_ID_MAX_SIZE];
43 	uint8_t len;
44 };
45 
46 struct dhcpv4_addr_slot {
47 	enum dhcpv4_server_addr_state state;
48 	struct dhcpv4_client_id client_id;
49 	struct in_addr addr;
50 	uint32_t lease_time;
51 	k_timepoint_t expiry;
52 };
53 
54 /** @endcond */
55 
56 /**
57  *  @brief Start DHCPv4 server instance on an iface
58  *
59  *  @details Start DHCPv4 server on a given interface. The server will start
60  *  listening for DHCPv4 Discover/Request messages on the interface and assign
61  *  IPv4 addresses from the configured address pool accordingly.
62  *
63  *  @param iface A valid pointer on an interface
64  *  @param base_addr First IPv4 address from the DHCPv4 address pool. The number
65  *  of addresses in the pool is configured statically with Kconfig
66  *  (CONFIG_NET_DHCPV4_SERVER_ADDR_COUNT).
67  *
68  *  @return 0 on success, a negative error code otherwise.
69  */
70 int net_dhcpv4_server_start(struct net_if *iface, struct in_addr *base_addr);
71 
72 /**
73  *  @brief Stop DHCPv4 server instance on an iface
74  *
75  *  @details Stop DHCPv4 server on a given interface. DHCPv4 requests will no
76  *  longer be handled on the interface, and all of the allocations are cleared.
77  *
78  *  @param iface A valid pointer on an interface
79  *
80  *  @return 0 on success, a negative error code otherwise.
81  */
82 int net_dhcpv4_server_stop(struct net_if *iface);
83 
84 /**
85  * @typedef net_dhcpv4_lease_cb_t
86  * @brief Callback used while iterating over active DHCPv4 address leases
87  *
88  * @param iface Pointer to the network interface
89  * @param lease Pointer to the DHPCv4 address lease slot
90  * @param user_data A valid pointer to user data or NULL
91  */
92 typedef void (*net_dhcpv4_lease_cb_t)(struct net_if *iface,
93 				      struct dhcpv4_addr_slot *lease,
94 				      void *user_data);
95 
96 /**
97  * @brief Iterate over all DHCPv4 address leases on a given network interface
98  * and call callback for each lease. In case no network interface is provided
99  * (NULL interface pointer), will iterate over all interfaces running DHCPv4
100  * server instance.
101  *
102  * @param iface Pointer to the network interface, can be NULL
103  * @param cb User-supplied callback function to call
104  * @param user_data User specified data
105  */
106 int net_dhcpv4_server_foreach_lease(struct net_if *iface,
107 				    net_dhcpv4_lease_cb_t cb,
108 				    void *user_data);
109 
110 /**
111  * @}
112  */
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* ZEPHYR_INCLUDE_NET_DHCPV4_SERVER_H_ */
119