1 /* $NetBSD: salloc.c,v 1.3 2022/04/03 01:11:00 christos Exp $ */
2
3 /* salloc.c
4
5 Memory allocation for the DHCP server... */
6
7 /*
8 * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1996-2003 by Internet Software Consortium
10 *
11 * This Source Code Form is subject to the terms of the Mozilla Public
12 * License, v. 2.0. If a copy of the MPL was not distributed with this
13 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Internet Systems Consortium, Inc.
24 * PO Box 360
25 * Newmarket, NH 03857 USA
26 * <info@isc.org>
27 * https://www.isc.org/
28 *
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: salloc.c,v 1.3 2022/04/03 01:11:00 christos Exp $");
33
34 #include "dhcpd.h"
35 #include <omapip/omapip_p.h>
36
37 #if defined (COMPACT_LEASES)
38 struct lease *free_leases;
39
40 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
41 struct lease *lease_hunks;
42
relinquish_lease_hunks()43 void relinquish_lease_hunks ()
44 {
45 struct lease *c, *n, **p;
46 int i;
47
48 /* Account for all the leases on the free list. */
49 for (n = lease_hunks; n; n = n->next) {
50 for (i = 1; i < n->starts + 1; i++) {
51 p = &free_leases;
52 for (c = free_leases; c; c = c->next) {
53 if (c == &n[i]) {
54 *p = c->next;
55 n->ends++;
56 break;
57 }
58 p = &c->next;
59 }
60 if (!c) {
61 log_info("lease %s refcnt %d",
62 piaddr (n[i].ip_addr), n[i].refcnt);
63 #if defined (DEBUG_RC_HISTORY)
64 dump_rc_history(&n[i]);
65 #endif
66 }
67 }
68 }
69
70 for (c = lease_hunks; c; c = n) {
71 n = c->next;
72 if (c->ends != c->starts) {
73 log_info("lease hunk %lx leases %ld free %ld",
74 (unsigned long)c, (unsigned long)(c->starts),
75 (unsigned long)(c->ends));
76 }
77 dfree(c, MDL);
78 }
79
80 /* Free all the rogue leases. */
81 for (c = free_leases; c; c = n) {
82 n = c->next;
83 dfree(c, MDL);
84 }
85 }
86
87 #endif
88
new_leases(n,file,line)89 struct lease *new_leases (n, file, line)
90 unsigned n;
91 const char *file;
92 int line;
93 {
94 struct lease *rval;
95 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
96 rval = dmalloc ((n + 1) * sizeof (struct lease), file, line);
97 if (rval != NULL) {
98 memset (rval, 0, sizeof (struct lease));
99 rval -> starts = n;
100 rval -> next = lease_hunks;
101 lease_hunks = rval;
102 rval++;
103 }
104 #else
105 rval = dmalloc (n * sizeof (struct lease), file, line);
106 #endif
107 return rval;
108 }
109
110 /* If we are allocating leases in aggregations, there's really no way
111 to free one, although perhaps we can maintain a free list. */
112
dhcp_lease_free(omapi_object_t * lo,const char * file,int line)113 isc_result_t dhcp_lease_free (omapi_object_t *lo,
114 const char *file, int line)
115 {
116 struct lease *lease;
117 if (lo -> type != dhcp_type_lease)
118 return DHCP_R_INVALIDARG;
119 lease = (struct lease *)lo;
120 memset (lease, 0, sizeof (struct lease));
121 lease -> next = free_leases;
122 free_leases = lease;
123 return ISC_R_SUCCESS;
124 }
125
dhcp_lease_get(omapi_object_t ** lp,const char * file,int line)126 isc_result_t dhcp_lease_get (omapi_object_t **lp,
127 const char *file, int line)
128 {
129 struct lease **lease = (struct lease **)lp;
130 struct lease *lt;
131
132 if (free_leases) {
133 lt = free_leases;
134 free_leases = lt -> next;
135 *lease = lt;
136 return ISC_R_SUCCESS;
137 }
138 return ISC_R_NOMEMORY;
139 }
140 #endif /* COMPACT_LEASES */
141
142 OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
143 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
144 OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
145 OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool)
146
147 #if !defined (NO_HOST_FREES) /* Scary debugging mode - don't enable! */
148 OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host)
149 #else
150 isc_result_t host_allocate (struct host_decl **p, const char *file, int line)
151 {
152 return omapi_object_allocate ((omapi_object_t **)p,
153 dhcp_type_host, 0, file, line);
154 }
155
156 isc_result_t host_reference (struct host_decl **pptr, struct host_decl *ptr,
157 const char *file, int line)
158 {
159 return omapi_object_reference ((omapi_object_t **)pptr,
160 (omapi_object_t *)ptr, file, line);
161 }
162
163 isc_result_t host_dereference (struct host_decl **ptr,
164 const char *file, int line)
165 {
166 if ((*ptr) -> refcnt == 1) {
167 log_error ("host dereferenced with refcnt == 1.");
168 #if defined (DEBUG_RC_HISTORY)
169 dump_rc_history ();
170 #endif
171 abort ();
172 }
173 return omapi_object_dereference ((omapi_object_t **)ptr, file, line);
174 }
175 #endif
176
177 struct lease_state *free_lease_states;
178
new_lease_state(file,line)179 struct lease_state *new_lease_state (file, line)
180 const char *file;
181 int line;
182 {
183 struct lease_state *rval;
184
185 if (free_lease_states) {
186 rval = free_lease_states;
187 free_lease_states =
188 (struct lease_state *)(free_lease_states -> next);
189 dmalloc_reuse (rval, file, line, 0);
190 } else {
191 rval = dmalloc (sizeof (struct lease_state), file, line);
192 if (!rval)
193 return rval;
194 }
195 memset (rval, 0, sizeof *rval);
196 if (!option_state_allocate (&rval -> options, file, line)) {
197 free_lease_state (rval, file, line);
198 return (struct lease_state *)0;
199 }
200 return rval;
201 }
202
free_lease_state(ptr,file,line)203 void free_lease_state (ptr, file, line)
204 struct lease_state *ptr;
205 const char *file;
206 int line;
207 {
208 if (ptr -> options)
209 option_state_dereference (&ptr -> options, file, line);
210 if (ptr -> packet)
211 packet_dereference (&ptr -> packet, file, line);
212 if (ptr -> shared_network)
213 shared_network_dereference (&ptr -> shared_network,
214 file, line);
215
216 data_string_forget (&ptr -> parameter_request_list, file, line);
217 data_string_forget (&ptr -> filename, file, line);
218 data_string_forget (&ptr -> server_name, file, line);
219 ptr -> next = free_lease_states;
220 free_lease_states = ptr;
221 dmalloc_reuse (free_lease_states, (char *)0, 0, 0);
222 }
223
224 #if defined (DEBUG_MEMORY_LEAKAGE) || \
225 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
relinquish_free_lease_states()226 void relinquish_free_lease_states ()
227 {
228 struct lease_state *cs, *ns;
229
230 for (cs = free_lease_states; cs; cs = ns) {
231 ns = cs -> next;
232 dfree (cs, MDL);
233 }
234 free_lease_states = (struct lease_state *)0;
235 }
236 #endif
237
new_permit(file,line)238 struct permit *new_permit (file, line)
239 const char *file;
240 int line;
241 {
242 struct permit *permit = ((struct permit *)
243 dmalloc (sizeof (struct permit), file, line));
244 if (!permit)
245 return permit;
246 memset (permit, 0, sizeof *permit);
247 return permit;
248 }
249
free_permit(permit,file,line)250 void free_permit (permit, file, line)
251 struct permit *permit;
252 const char *file;
253 int line;
254 {
255 if (permit -> type == permit_class)
256 class_dereference (&permit -> class, MDL);
257 dfree (permit, file, line);
258 }
259