salloc.c revision 1.1
1/*	$NetBSD: salloc.c,v 1.1 2018/04/07 22:34:28 christos Exp $	*/
2
3/* salloc.c
4
5   Memory allocation for the DHCP server... */
6
7/*
8 * Copyright (c) 2004-2017 by 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 *   950 Charter Street
25 *   Redwood City, CA 94063
26 *   <info@isc.org>
27 *   https://www.isc.org/
28 *
29 */
30
31#include <sys/cdefs.h>
32__RCSID("$NetBSD: salloc.c,v 1.1 2018/04/07 22:34:28 christos Exp $");
33
34#include "dhcpd.h"
35#include <omapip/omapip_p.h>
36
37#if defined (COMPACT_LEASES)
38struct lease *free_leases;
39
40#if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
41struct lease *lease_hunks;
42
43void 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
89struct 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
113isc_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
126isc_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
142OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
143OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
144OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
145OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool)
146
147#if !defined (NO_HOST_FREES)	/* Scary debugging mode - don't enable! */
148OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host)
149#else
150isc_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
156isc_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
163isc_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
177struct lease_state *free_lease_states;
178
179struct 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
203void 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)
226void 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
238struct 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
250void 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