1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #ifndef foosddhcpclienthfoo
3 #define foosddhcpclienthfoo
4 
5 /***
6   Copyright © 2013 Intel Corporation. All rights reserved.
7 
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12 
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17 
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21 
22 #include <inttypes.h>
23 #include <net/ethernet.h>
24 #include <netinet/in.h>
25 #include <sys/types.h>
26 #include <stdbool.h>
27 
28 #include "sd-dhcp-lease.h"
29 #include "sd-dhcp-option.h"
30 #include "sd-event.h"
31 
32 #include "_sd-common.h"
33 
34 _SD_BEGIN_DECLARATIONS;
35 
36 enum {
37         SD_DHCP_CLIENT_EVENT_STOP               = 0,
38         SD_DHCP_CLIENT_EVENT_IP_ACQUIRE         = 1,
39         SD_DHCP_CLIENT_EVENT_IP_CHANGE          = 2,
40         SD_DHCP_CLIENT_EVENT_EXPIRED            = 3,
41         SD_DHCP_CLIENT_EVENT_RENEW              = 4,
42         SD_DHCP_CLIENT_EVENT_SELECTING          = 5,
43         SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE  = 6, /* Sent when we have not received a reply after the first few attempts.
44                                                       * The client may want to start acquiring link-local addresses. */
45 };
46 
47 /* https://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.xhtml#options */
48 enum {
49         SD_DHCP_OPTION_PAD                            = 0,   /* [RFC2132] */
50         SD_DHCP_OPTION_SUBNET_MASK                    = 1,   /* [RFC2132] */
51         SD_DHCP_OPTION_TIME_OFFSET                    = 2,   /* [RFC2132], deprecated by 100 and 101 */
52         SD_DHCP_OPTION_ROUTER                         = 3,   /* [RFC2132] */
53         SD_DHCP_OPTION_TIME_SERVER                    = 4,   /* [RFC2132] */
54         SD_DHCP_OPTION_NAME_SERVER                    = 5,   /* [RFC2132] */
55         SD_DHCP_OPTION_DOMAIN_NAME_SERVER             = 6,   /* [RFC2132] */
56         SD_DHCP_OPTION_LOG_SERVER                     = 7,   /* [RFC2132] */
57         SD_DHCP_OPTION_QUOTES_SERVER                  = 8,   /* [RFC2132] */
58         SD_DHCP_OPTION_LPR_SERVER                     = 9,   /* [RFC2132] */
59         SD_DHCP_OPTION_IMPRESS_SERVER                 = 10,  /* [RFC2132] */
60         SD_DHCP_OPTION_RLP_SERVER                     = 11,  /* [RFC2132] */
61         SD_DHCP_OPTION_HOST_NAME                      = 12,  /* [RFC2132] */
62         SD_DHCP_OPTION_BOOT_FILE_SIZE                 = 13,  /* [RFC2132] */
63         SD_DHCP_OPTION_MERIT_DUMP_FILE                = 14,  /* [RFC2132] */
64         SD_DHCP_OPTION_DOMAIN_NAME                    = 15,  /* [RFC2132] */
65         SD_DHCP_OPTION_SWAP_SERVER                    = 16,  /* [RFC2132] */
66         SD_DHCP_OPTION_ROOT_PATH                      = 17,  /* [RFC2132] */
67         SD_DHCP_OPTION_EXTENSION_FILE                 = 18,  /* [RFC2132] */
68         SD_DHCP_OPTION_FORWARD                        = 19,  /* [RFC2132] */
69         SD_DHCP_OPTION_SOURCE_ROUTE                   = 20,  /* [RFC2132] */
70         SD_DHCP_OPTION_POLICY_FILTER                  = 21,  /* [RFC2132] */
71         SD_DHCP_OPTION_MAX_DATAGRAM_ASSEMBLY          = 22,  /* [RFC2132] */
72         SD_DHCP_OPTION_DEFAULT_IP_TTL                 = 23,  /* [RFC2132] */
73         SD_DHCP_OPTION_MTU_TIMEOUT                    = 24,  /* [RFC2132] */
74         SD_DHCP_OPTION_MTU_PLATEAU                    = 25,  /* [RFC2132] */
75         SD_DHCP_OPTION_MTU_INTERFACE                  = 26,  /* [RFC2132] */
76         SD_DHCP_OPTION_MTU_SUBNET                     = 27,  /* [RFC2132] */
77         SD_DHCP_OPTION_BROADCAST                      = 28,  /* [RFC2132] */
78         SD_DHCP_OPTION_MASK_DISCOVERY                 = 29,  /* [RFC2132] */
79         SD_DHCP_OPTION_MASK_SUPPLIER                  = 30,  /* [RFC2132] */
80         SD_DHCP_OPTION_ROUTER_DISCOVERY               = 31,  /* [RFC2132] */
81         SD_DHCP_OPTION_ROUTER_REQUEST                 = 32,  /* [RFC2132] */
82         SD_DHCP_OPTION_STATIC_ROUTE                   = 33,  /* [RFC2132] */
83         SD_DHCP_OPTION_TRAILERS                       = 34,  /* [RFC2132] */
84         SD_DHCP_OPTION_ARP_TIMEOUT                    = 35,  /* [RFC2132] */
85         SD_DHCP_OPTION_ETHERNET                       = 36,  /* [RFC2132] */
86         SD_DHCP_OPTION_DEFAULT_TCP_TTL                = 37,  /* [RFC2132] */
87         SD_DHCP_OPTION_KEEPALIVE_TIME                 = 38,  /* [RFC2132] */
88         SD_DHCP_OPTION_KEEPALIVE_DATA                 = 39,  /* [RFC2132] */
89         SD_DHCP_OPTION_NIS_DOMAIN                     = 40,  /* [RFC2132] */
90         SD_DHCP_OPTION_NIS_SERVER                     = 41,  /* [RFC2132] */
91         SD_DHCP_OPTION_NTP_SERVER                     = 42,  /* [RFC2132] */
92         SD_DHCP_OPTION_VENDOR_SPECIFIC                = 43,  /* [RFC2132] */
93         SD_DHCP_OPTION_NETBIOS_NAME_SERVER            = 44,  /* [RFC2132] */
94         SD_DHCP_OPTION_NETBIOS_DIST_SERVER            = 45,  /* [RFC2132] */
95         SD_DHCP_OPTION_NETBIOS_NODE_TYPE              = 46,  /* [RFC2132] */
96         SD_DHCP_OPTION_NETBIOS_SCOPE                  = 47,  /* [RFC2132] */
97         SD_DHCP_OPTION_X_WINDOW_FONT                  = 48,  /* [RFC2132] */
98         SD_DHCP_OPTION_X_WINDOW_MANAGER               = 49,  /* [RFC2132] */
99         SD_DHCP_OPTION_REQUESTED_IP_ADDRESS           = 50,  /* [RFC2132] */
100         SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME          = 51,  /* [RFC2132] */
101         SD_DHCP_OPTION_OVERLOAD                       = 52,  /* [RFC2132] */
102         SD_DHCP_OPTION_MESSAGE_TYPE                   = 53,  /* [RFC2132] */
103         SD_DHCP_OPTION_SERVER_IDENTIFIER              = 54,  /* [RFC2132] */
104         SD_DHCP_OPTION_PARAMETER_REQUEST_LIST         = 55,  /* [RFC2132] */
105         SD_DHCP_OPTION_ERROR_MESSAGE                  = 56,  /* [RFC2132] */
106         SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE           = 57,  /* [RFC2132] */
107         SD_DHCP_OPTION_RENEWAL_TIME                   = 58,  /* [RFC2132] */
108         SD_DHCP_OPTION_REBINDING_TIME                 = 59,  /* [RFC2132] */
109         SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER        = 60,  /* [RFC2132] */
110         SD_DHCP_OPTION_CLIENT_IDENTIFIER              = 61,  /* [RFC2132] */
111         SD_DHCP_OPTION_NETWARE_IP_DOMAIN              = 62,  /* [RFC2242] */
112         SD_DHCP_OPTION_NETWARE_IP_OPTION              = 63,  /* [RFC2242] */
113         SD_DHCP_OPTION_NIS_DOMAIN_NAME                = 64,  /* [RFC2132] */
114         SD_DHCP_OPTION_NIS_SERVER_ADDR                = 65,  /* [RFC2132] */
115         SD_DHCP_OPTION_BOOT_SERVER_NAME               = 66,  /* [RFC2132] */
116         SD_DHCP_OPTION_BOOT_FILENAME                  = 67,  /* [RFC2132] */
117         SD_DHCP_OPTION_HOME_AGENT_ADDRESSES           = 68,  /* [RFC2132] */
118         SD_DHCP_OPTION_SMTP_SERVER                    = 69,  /* [RFC2132] */
119         SD_DHCP_OPTION_POP3_SERVER                    = 70,  /* [RFC2132] */
120         SD_DHCP_OPTION_NNTP_SERVER                    = 71,  /* [RFC2132] */
121         SD_DHCP_OPTION_WWW_SERVER                     = 72,  /* [RFC2132] */
122         SD_DHCP_OPTION_FINGER_SERVER                  = 73,  /* [RFC2132] */
123         SD_DHCP_OPTION_IRC_SERVER                     = 74,  /* [RFC2132] */
124         SD_DHCP_OPTION_STREETTALK_SERVER              = 75,  /* [RFC2132] */
125         SD_DHCP_OPTION_STDA_SERVER                    = 76,  /* [RFC2132] */
126         SD_DHCP_OPTION_USER_CLASS                     = 77,  /* [RFC3004] */
127         SD_DHCP_OPTION_DIRECTORY_AGENT                = 78,  /* [RFC2610] */
128         SD_DHCP_OPTION_SERVICE_SCOPE                  = 79,  /* [RFC2610] */
129         SD_DHCP_OPTION_RAPID_COMMIT                   = 80,  /* [RFC4039] */
130         SD_DHCP_OPTION_FQDN                           = 81,  /* [RFC4702] */
131         SD_DHCP_OPTION_RELAY_AGENT_INFORMATION        = 82,  /* [RFC3046] */
132         SD_DHCP_OPTION_ISNS                           = 83,  /* [RFC4174] */
133         /* option code 84 is unassigned [RFC3679] */
134         SD_DHCP_OPTION_NDS_SERVER                     = 85,  /* [RFC2241] */
135         SD_DHCP_OPTION_NDS_TREE_NAME                  = 86,  /* [RFC2241] */
136         SD_DHCP_OPTION_NDS_CONTEXT                    = 87,  /* [RFC2241] */
137         SD_DHCP_OPTION_BCMCS_CONTROLLER_DOMAIN_NAME   = 88,  /* [RFC4280] */
138         SD_DHCP_OPTION_BCMCS_CONTROLLER_ADDRESS       = 89,  /* [RFC4280] */
139         SD_DHCP_OPTION_AUTHENTICATION                 = 90,  /* [RFC3118] */
140         SD_DHCP_OPTION_CLIENT_LAST_TRANSACTION_TIME   = 91,  /* [RFC4388] */
141         SD_DHCP_OPTION_ASSOCIATED_IP                  = 92,  /* [RFC4388] */
142         SD_DHCP_OPTION_CLIENT_SYSTEM                  = 93,  /* [RFC4578] */
143         SD_DHCP_OPTION_CLIENT_NDI                     = 94,  /* [RFC4578] */
144         SD_DHCP_OPTION_LDAP                           = 95,  /* [RFC3679] */
145         /* option code 96 is unassigned [RFC3679] */
146         SD_DHCP_OPTION_UUID                           = 97,  /* [RFC4578] */
147         SD_DHCP_OPTION_USER_AUTHENTICATION            = 98,  /* [RFC2485] */
148         SD_DHCP_OPTION_GEOCONF_CIVIC                  = 99,  /* [RFC4776] */
149         SD_DHCP_OPTION_POSIX_TIMEZONE                 = 100, /* [RFC4833] */
150         SD_DHCP_OPTION_TZDB_TIMEZONE                  = 101, /* [RFC4833] */
151         /* option codes 102-107 are unassigned [RFC3679] */
152         SD_DHCP_OPTION_IPV6_ONLY_PREFERRED            = 108, /* [RFC8925] */
153         SD_DHCP_OPTION_DHCP4O6_SOURCE_ADDRESS         = 109, /* [RFC8539] */
154         /* option codes 110-111 are unassigned [RFC3679] */
155         SD_DHCP_OPTION_NETINFO_ADDRESS                = 112, /* [RFC3679] */
156         SD_DHCP_OPTION_NETINFO_TAG                    = 113, /* [RFC3679] */
157         SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL            = 114, /* [RFC8910] */
158         /* option code 115 is unassigned [RFC3679] */
159         SD_DHCP_OPTION_AUTO_CONFIG                    = 116, /* [RFC2563] */
160         SD_DHCP_OPTION_NAME_SERVICE_SEARCH            = 117, /* [RFC2937] */
161         SD_DHCP_OPTION_SUBNET_SELECTION               = 118, /* [RFC3011] */
162         SD_DHCP_OPTION_DOMAIN_SEARCH                  = 119, /* [RFC3397] */
163         SD_DHCP_OPTION_SIP_SERVER                     = 120, /* [RFC3361] */
164         SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE         = 121, /* [RFC3442] */
165         SD_DHCP_OPTION_CABLELABS_CLIENT_CONFIGURATION = 122, /* [RFC3495] */
166         SD_DHCP_OPTION_GEOCONF                        = 123, /* [RFC6225] */
167         SD_DHCP_OPTION_VENDOR_CLASS                   = 124, /* [RFC3925] */
168         SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION    = 125, /* [RFC3925] */
169         /* option codes 126-127 are unassigned [RFC3679] */
170         /* option codes 128-135 are assigned to use by PXE, but they are vendor specific [RFC4578] */
171         SD_DHCP_OPTION_PANA_AGENT                     = 136, /* [RFC5192] */
172         SD_DHCP_OPTION_LOST_SERVER_FQDN               = 137, /* [RFC5223] */
173         SD_DHCP_OPTION_CAPWAP_AC_ADDRESS              = 138, /* [RFC5417] */
174         SD_DHCP_OPTION_MOS_ADDRESS                    = 139, /* [RFC5678] */
175         SD_DHCP_OPTION_MOS_FQDN                       = 140, /* [RFC5678] */
176         SD_DHCP_OPTION_SIP_SERVICE_DOMAIN             = 141, /* [RFC6011] */
177         SD_DHCP_OPTION_ANDSF_ADDRESS                  = 142, /* [RFC6153] */
178         SD_DHCP_OPTION_SZTP_REDIRECT                  = 143, /* [RFC8572] */
179         SD_DHCP_OPTION_GEOLOC                         = 144, /* [RFC6225] */
180         SD_DHCP_OPTION_FORCERENEW_NONCE_CAPABLE       = 145, /* [RFC6704] */
181         SD_DHCP_OPTION_RDNSS_SELECTION                = 146, /* [RFC6731] */
182         SD_DHCP_OPTION_DOTS_RI                        = 147, /* [RFC8973] */
183         SD_DHCP_OPTION_DOTS_ADDRESS                   = 148, /* [RFC8973] */
184         /* option code 149 is unassigned [RFC3942] */
185         SD_DHCP_OPTION_TFTP_SERVER_ADDRESS            = 150, /* [RFC5859] */
186         SD_DHCP_OPTION_STATUS_CODE                    = 151, /* [RFC6926] */
187         SD_DHCP_OPTION_BASE_TIME                      = 152, /* [RFC6926] */
188         SD_DHCP_OPTION_START_TIME_OF_STATE            = 153, /* [RFC6926] */
189         SD_DHCP_OPTION_QUERY_START_TIME               = 154, /* [RFC6926] */
190         SD_DHCP_OPTION_QUERY_END_TIME                 = 155, /* [RFC6926] */
191         SD_DHCP_OPTION_DHCP_STATE                     = 156, /* [RFC6926] */
192         SD_DHCP_OPTION_DATA_SOURCE                    = 157, /* [RFC6926] */
193         SD_DHCP_OPTION_PCP_SERVER                     = 158, /* [RFC7291] */
194         SD_DHCP_OPTION_PORT_PARAMS                    = 159, /* [RFC7618] */
195         /* option code 160 is unassigned [RFC7710][RFC8910] */
196         SD_DHCP_OPTION_MUD_URL                        = 161, /* [RFC8520] */
197         /* option codes 162-174 are unassigned [RFC3942] */
198         /* option codes 175-177 are temporary assigned. */
199         /* option codes 178-207 are unassigned [RFC3942] */
200         SD_DHCP_OPTION_PXELINUX_MAGIC                 = 208, /* [RFC5071] Deprecated */
201         SD_DHCP_OPTION_CONFIGURATION_FILE             = 209, /* [RFC5071] */
202         SD_DHCP_OPTION_PATH_PREFIX                    = 210, /* [RFC5071] */
203         SD_DHCP_OPTION_REBOOT_TIME                    = 211, /* [RFC5071] */
204         SD_DHCP_OPTION_6RD                            = 212, /* [RFC5969] */
205         SD_DHCP_OPTION_ACCESS_DOMAIN                  = 213, /* [RFC5986] */
206         /* option codes 214-219 are unassigned */
207         SD_DHCP_OPTION_SUBNET_ALLOCATION              = 220, /* [RFC6656] */
208         SD_DHCP_OPTION_VIRTUAL_SUBNET_SELECTION       = 221, /* [RFC6607] */
209         /* option codes 222-223 are unassigned [RFC3942] */
210         /* option codes 224-254 are reserved for private use */
211         SD_DHCP_OPTION_PRIVATE_BASE                   = 224,
212         SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE = 249, /* [RFC7844] */
213         SD_DHCP_OPTION_PRIVATE_PROXY_AUTODISCOVERY    = 252, /* [RFC7844] */
214         SD_DHCP_OPTION_PRIVATE_LAST                   = 254,
215         SD_DHCP_OPTION_END                            = 255, /* [RFC2132] */
216 };
217 
218 /* Suboptions for SD_DHCP_OPTION_RELAY_AGENT_INFORMATION option */
219 enum {
220         SD_DHCP_RELAY_AGENT_CIRCUIT_ID             = 1,
221         SD_DHCP_RELAY_AGENT_REMOTE_ID              = 2,
222 };
223 
224 typedef struct sd_dhcp_client sd_dhcp_client;
225 
226 typedef int (*sd_dhcp_client_callback_t)(sd_dhcp_client *client, int event, void *userdata);
227 int sd_dhcp_client_set_callback(
228                 sd_dhcp_client *client,
229                 sd_dhcp_client_callback_t cb,
230                 void *userdata);
231 
232 int sd_dhcp_client_set_request_option(
233                 sd_dhcp_client *client,
234                 uint8_t option);
235 int sd_dhcp_client_set_request_address(
236                 sd_dhcp_client *client,
237                 const struct in_addr *last_address);
238 int sd_dhcp_client_set_request_broadcast(
239                 sd_dhcp_client *client,
240                 int broadcast);
241 int sd_dhcp_client_set_ifindex(
242                 sd_dhcp_client *client,
243                 int interface_index);
244 int sd_dhcp_client_set_ifname(
245                 sd_dhcp_client *client,
246                 const char *interface_name);
247 int sd_dhcp_client_get_ifname(sd_dhcp_client *client, const char **ret);
248 int sd_dhcp_client_set_mac(
249                 sd_dhcp_client *client,
250                 const uint8_t *addr,
251                 const uint8_t *bcast_addr,
252                 size_t addr_len,
253                 uint16_t arp_type);
254 int sd_dhcp_client_set_client_id(
255                 sd_dhcp_client *client,
256                 uint8_t type,
257                 const uint8_t *data,
258                 size_t data_len);
259 int sd_dhcp_client_set_iaid_duid(
260                 sd_dhcp_client *client,
261                 bool iaid_set,
262                 uint32_t iaid,
263                 uint16_t duid_type,
264                 const void *duid,
265                 size_t duid_len);
266 int sd_dhcp_client_set_iaid_duid_llt(
267                 sd_dhcp_client *client,
268                 bool iaid_set,
269                 uint32_t iaid,
270                 uint64_t llt_time);
271 int sd_dhcp_client_set_duid(
272                 sd_dhcp_client *client,
273                 uint16_t duid_type,
274                 const void *duid,
275                 size_t duid_len);
276 int sd_dhcp_client_set_duid_llt(
277                 sd_dhcp_client *client,
278                 uint64_t llt_time);
279 int sd_dhcp_client_get_client_id(
280                 sd_dhcp_client *client,
281                 uint8_t *type,
282                 const uint8_t **data,
283                 size_t *data_len);
284 int sd_dhcp_client_set_mtu(
285                 sd_dhcp_client *client,
286                 uint32_t mtu);
287 int sd_dhcp_client_set_max_attempts(
288                 sd_dhcp_client *client,
289                 uint64_t attempt);
290 int sd_dhcp_client_set_client_port(
291                 sd_dhcp_client *client,
292                 uint16_t port);
293 int sd_dhcp_client_set_hostname(
294                 sd_dhcp_client *client,
295                 const char *hostname);
296 int sd_dhcp_client_set_vendor_class_identifier(
297                 sd_dhcp_client *client,
298                 const char *vci);
299 int sd_dhcp_client_set_mud_url(
300                 sd_dhcp_client *client,
301                 const char *mudurl);
302 int sd_dhcp_client_set_user_class(
303                 sd_dhcp_client *client,
304                 char * const *user_class);
305 int sd_dhcp_client_get_lease(
306                 sd_dhcp_client *client,
307                 sd_dhcp_lease **ret);
308 int sd_dhcp_client_set_service_type(
309                 sd_dhcp_client *client,
310                 int type);
311 int sd_dhcp_client_set_fallback_lease_lifetime(
312                 sd_dhcp_client *client,
313                 uint32_t fallback_lease_lifetime);
314 
315 int sd_dhcp_client_add_option(sd_dhcp_client *client, sd_dhcp_option *v);
316 int sd_dhcp_client_add_vendor_option(sd_dhcp_client *client, sd_dhcp_option *v);
317 
318 int sd_dhcp_client_is_running(sd_dhcp_client *client);
319 int sd_dhcp_client_stop(sd_dhcp_client *client);
320 int sd_dhcp_client_start(sd_dhcp_client *client);
321 int sd_dhcp_client_send_release(sd_dhcp_client *client);
322 int sd_dhcp_client_send_decline(sd_dhcp_client *client);
323 int sd_dhcp_client_send_renew(sd_dhcp_client *client);
324 
325 sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client);
326 sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client);
327 
328 /* NOTE: anonymize parameter is used to initialize PRL memory with different
329  * options when using RFC7844 Anonymity Profiles */
330 int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize);
331 
332 int sd_dhcp_client_id_to_string(const void *data, size_t len, char **ret);
333 
334 int sd_dhcp_client_attach_event(
335                 sd_dhcp_client *client,
336                 sd_event *event,
337                 int64_t priority);
338 int sd_dhcp_client_detach_event(sd_dhcp_client *client);
339 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client);
340 
341 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_client, sd_dhcp_client_unref);
342 
343 _SD_END_DECLARATIONS;
344 
345 #endif
346