1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 /***
5   Copyright © 2013 Intel Corporation. All rights reserved.
6 ***/
7 
8 #include "sd-dhcp-server.h"
9 #include "sd-event.h"
10 
11 #include "dhcp-internal.h"
12 #include "network-common.h"
13 #include "ordered-set.h"
14 #include "time-util.h"
15 
16 typedef enum DHCPRawOption {
17         DHCP_RAW_OPTION_DATA_UINT8,
18         DHCP_RAW_OPTION_DATA_UINT16,
19         DHCP_RAW_OPTION_DATA_UINT32,
20         DHCP_RAW_OPTION_DATA_STRING,
21         DHCP_RAW_OPTION_DATA_IPV4ADDRESS,
22         DHCP_RAW_OPTION_DATA_IPV6ADDRESS,
23         _DHCP_RAW_OPTION_DATA_MAX,
24         _DHCP_RAW_OPTION_DATA_INVALID,
25 } DHCPRawOption;
26 
27 typedef struct DHCPClientId {
28         size_t length;
29         uint8_t *data;
30 } DHCPClientId;
31 
32 typedef struct DHCPLease {
33         sd_dhcp_server *server;
34 
35         DHCPClientId client_id;
36 
37         uint8_t htype; /* e.g. ARPHRD_ETHER */
38         uint8_t hlen;  /* e.g. ETH_ALEN */
39         be32_t address;
40         be32_t gateway;
41         uint8_t chaddr[16];
42         usec_t expiration;
43 } DHCPLease;
44 
45 struct sd_dhcp_server {
46         unsigned n_ref;
47 
48         sd_event *event;
49         int event_priority;
50         sd_event_source *receive_message;
51         sd_event_source *receive_broadcast;
52         int fd;
53         int fd_raw;
54         int fd_broadcast;
55 
56         int ifindex;
57         char *ifname;
58         bool bind_to_interface;
59         be32_t address;
60         be32_t netmask;
61         be32_t subnet;
62         uint32_t pool_offset;
63         uint32_t pool_size;
64 
65         char *timezone;
66 
67         DHCPServerData servers[_SD_DHCP_LEASE_SERVER_TYPE_MAX];
68         struct in_addr boot_server_address;
69         char *boot_server_name;
70         char *boot_filename;
71 
72         OrderedSet *extra_options;
73         OrderedSet *vendor_options;
74 
75         bool emit_router;
76         struct in_addr router_address;
77 
78         Hashmap *bound_leases_by_client_id;
79         Hashmap *bound_leases_by_address;
80         Hashmap *static_leases_by_client_id;
81         Hashmap *static_leases_by_address;
82 
83         uint32_t max_lease_time, default_lease_time;
84 
85         sd_dhcp_server_callback_t callback;
86         void *callback_userdata;
87 
88         struct in_addr relay_target;
89 
90         char *agent_circuit_id;
91         char *agent_remote_id;
92 };
93 
94 typedef struct DHCPRequest {
95         /* received message */
96         DHCPMessage *message;
97 
98         /* options */
99         DHCPClientId client_id;
100         size_t max_optlen;
101         be32_t server_id;
102         be32_t requested_ip;
103         uint32_t lifetime;
104         const uint8_t *agent_info_option;
105 } DHCPRequest;
106 
107 extern const struct hash_ops dhcp_lease_hash_ops;
108 
109 int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
110                                size_t length);
111 int dhcp_server_send_packet(sd_dhcp_server *server,
112                             DHCPRequest *req, DHCPPacket *packet,
113                             int type, size_t optoffset);
114 
115 void client_id_hash_func(const DHCPClientId *p, struct siphash *state);
116 int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b);
117 
118 #define log_dhcp_server_errno(server, error, fmt, ...)          \
119         log_interface_prefix_full_errno(                        \
120                 "DHCPv4 server: ",                              \
121                 sd_dhcp_server, server,                         \
122                 error, fmt, ##__VA_ARGS__)
123 #define log_dhcp_server(server, fmt, ...)                       \
124         log_interface_prefix_full_errno_zerook(                 \
125                 "DHCPv4 server: ",                              \
126                 sd_dhcp_server, server,                         \
127                 0, fmt, ##__VA_ARGS__)
128