Debian Codegraph @

src/network/networkd-dhcp-server-static-lease.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "conf-parser.h"
5 #include "dns-domain.h"
6 #include "ether-addr-util.h"
7 #include "hashmap.h"
8 #include "networkd-dhcp-server-static-lease.h"
9 #include "networkd-network.h"
10 #include "string-util.h"
11
12 static DHCPStaticLease* dhcp_static_lease_free(DHCPStaticLease *static_lease) {
13 if (!static_lease)
14 return NULL;
15
16 if (static_lease->network && static_lease->section)
17 hashmap_remove(static_lease->network->dhcp_static_leases_by_section, static_lease->section);
18
19 config_section_free(static_lease->section);
20 free(static_lease->client_id);
21 free(static_lease->hostname);
22 return mfree(static_lease);
23 }
24
25 DEFINE_SECTION_CLEANUP_FUNCTIONS(DHCPStaticLease, dhcp_static_lease_free);
26
27 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
28 static_lease_hash_ops_by_section,
29 ConfigSection, config_section_hash_func, config_section_compare_func,
30 DHCPStaticLease, dhcp_static_lease_free);
31
32 static int dhcp_static_lease_new(DHCPStaticLease **ret) {
33 DHCPStaticLease *p;
34
35 assert(ret);
36
37 p = new0(DHCPStaticLease, 1);
38 if (!p)
39 return -ENOMEM;
40
41 *ret = TAKE_PTR(p);
42 return 0;
43 }
44
45 static int lease_new_static(Network *network, const char *filename, unsigned section_line, DHCPStaticLease **ret) {
46 _cleanup_(config_section_freep) ConfigSection *n = NULL;
47 _cleanup_(dhcp_static_lease_freep) DHCPStaticLease *static_lease = NULL;
48 int r;
49
50 assert(network);
51 assert(filename);
52 assert(section_line > 0);
53 assert(ret);
54
55 r = config_section_new(filename, section_line, &n);
56 if (r < 0)
57 return r;
58
59 static_lease = hashmap_get(network->dhcp_static_leases_by_section, n);
60 if (static_lease) {
61 *ret = TAKE_PTR(static_lease);
62 return 0;
63 }
64
65 r = dhcp_static_lease_new(&static_lease);
66 if (r < 0)
67 return r;
68
69 static_lease->network = network;
70 static_lease->section = TAKE_PTR(n);
71
72 r = hashmap_ensure_put(&network->dhcp_static_leases_by_section, &static_lease_hash_ops_by_section, static_lease->section, static_lease);
73 if (r < 0)
74 return r;
75
76 *ret = TAKE_PTR(static_lease);
77 return 0;
78 }
79
80 static int static_lease_verify(DHCPStaticLease *static_lease) {
81 if (section_is_invalid(static_lease->section))
82 return -EINVAL;
83
84 if (in4_addr_is_null(&static_lease->address))
85 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
86 "%s: DHCP static lease without Address= field configured. "
87 "Ignoring [DHCPServerStaticLease] section from line %u.",
88 static_lease->section->filename, static_lease->section->line);
89
90 /* TODO: check that the address is in the pool. */
91
92 if (static_lease->client_id_size == 0 || !static_lease->client_id)
93 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
94 "%s: DHCP static lease without MACAddress= field configured. "
95 "Ignoring [DHCPServerStaticLease] section from line %u.",
96 static_lease->section->filename, static_lease->section->line);
97
98 assert(static_lease->client_id_size == ETH_ALEN + 1);
99
100 return 0;
101 }
102
103 void network_drop_invalid_static_leases(Network *network) {
104 DHCPStaticLease *static_lease;
105
106 assert(network);
107
108 HASHMAP_FOREACH(static_lease, network->dhcp_static_leases_by_section)
109 if (static_lease_verify(static_lease) < 0)
110 dhcp_static_lease_free(static_lease);
111 }
112
113 int config_parse_dhcp_static_lease_address(
114 const char *unit,
115 const char *filename,
116 unsigned line,
117 const char *section,
118 unsigned section_line,
119 const char *lvalue,
120 int ltype,
121 const char *rvalue,
122 void *data,
123 void *userdata) {
124
125 _cleanup_(dhcp_static_lease_free_or_set_invalidp) DHCPStaticLease *lease = NULL;
126 Network *network = ASSERT_PTR(userdata);
127 union in_addr_union addr;
128 int r;
129
130 assert(filename);
131 assert(lvalue);
132 assert(rvalue);
133
134 r = lease_new_static(network, filename, section_line, &lease);
135 if (r < 0)
136 return log_oom();
137
138 if (isempty(rvalue)) {
139 lease->address.s_addr = 0;
140 TAKE_PTR(lease);
141 return 0;
142 }
143
144 r = in_addr_from_string(AF_INET, rvalue, &addr);
145 if (r < 0) {
146 log_syntax(unit, LOG_WARNING, filename, line, r,
147 "Failed to parse IPv4 address for DHCPv4 static lease, ignoring assignment: %s", rvalue);
148 return 0;
149 }
150 if (in4_addr_is_null(&addr.in)) {
151 log_syntax(unit, LOG_WARNING, filename, line, 0,
152 "IPv4 address for DHCPv4 static lease cannot be the ANY address, ignoring assignment: %s", rvalue);
153 return 0;
154 }
155
156 lease->address = addr.in;
157
158 TAKE_PTR(lease);
159 return 0;
160 }
161
162 int config_parse_dhcp_static_lease_hwaddr(
163 const char *unit,
164 const char *filename,
165 unsigned line,
166 const char *section,
167 unsigned section_line,
168 const char *lvalue,
169 int ltype,
170 const char *rvalue,
171 void *data,
172 void *userdata) {
173
174 _cleanup_(dhcp_static_lease_free_or_set_invalidp) DHCPStaticLease *lease = NULL;
175 Network *network = ASSERT_PTR(userdata);
176 struct ether_addr hwaddr;
177 uint8_t *c;
178 int r;
179
180 assert(filename);
181 assert(lvalue);
182 assert(rvalue);
183
184 r = lease_new_static(network, filename, section_line, &lease);
185 if (r < 0)
186 return log_oom();
187
188 if (isempty(rvalue)) {
189 lease->client_id = mfree(lease->client_id);
190 lease->client_id_size = 0;
191 TAKE_PTR(lease);
192 return 0;
193 }
194
195 r = parse_ether_addr(rvalue, &hwaddr);
196 if (r < 0) {
197 log_syntax(unit, LOG_WARNING, filename, line, r,
198 "Failed to parse MAC address for DHCPv4 static lease, ignoring assignment: %s", rvalue);
199 return 0;
200 }
201 if (ether_addr_is_null(&hwaddr) || (hwaddr.ether_addr_octet[0] & 0x01)) {
202 log_syntax(unit, LOG_WARNING, filename, line, 0,
203 "MAC address for DHCPv4 static lease cannot be null or multicast, ignoring assignment: %s", rvalue);
204 return 0;
205 }
206
207 c = new(uint8_t, ETH_ALEN + 1);
208 if (!c)
209 return log_oom();
210
211 /* set client id type to 1: Ethernet Link-Layer (RFC 2132) */
212 c[0] = 0x01;
213 memcpy(c + 1, &hwaddr, ETH_ALEN);
214
215 free_and_replace(lease->client_id, c);
216 lease->client_id_size = ETH_ALEN + 1;
217
218 TAKE_PTR(lease);
219 return 0;
220 }
221
222 int config_parse_dhcp_static_lease_hostname(
223 const char *unit,
224 const char *filename,
225 unsigned line,
226 const char *section,
227 unsigned section_line,
228 const char *lvalue,
229 int ltype,
230 const char *rvalue,
231 void *data,
232 void *userdata) {
233
234 _cleanup_(dhcp_static_lease_free_or_set_invalidp) DHCPStaticLease *lease = NULL;
235 Network *network = ASSERT_PTR(userdata);
236 int r;
237
238 assert(filename);
239 assert(lvalue);
240 assert(rvalue);
241
242 r = lease_new_static(network, filename, section_line, &lease);
243 if (r < 0)
244 return log_oom();
245
246 if (isempty(rvalue)) {
247 lease->hostname = mfree(lease->hostname);
248 TAKE_PTR(lease);
249 return 0;
250 }
251
252 r = dns_name_is_valid_ldh(rvalue);
253 if (r <= 0)
254 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
255
256 r = free_and_strdup(&lease->hostname, rvalue);
257 if (r < 0)
258 return log_oom();
259
260 TAKE_PTR(lease);
261 return 0;
262 }