xref: /minix3/external/bsd/dhcp/dist/server/salloc.c (revision d56f51ea7d8b9045e5c8e2028422523d3f9a5840)
1 /*	$NetBSD: salloc.c,v 1.1.1.3 2014/07/12 11:58:16 spz Exp $	*/
2 /* salloc.c
3 
4    Memory allocation for the DHCP server... */
5 
6 /*
7  * Copyright (c) 2009,2012,2014 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1996-2003 by Internet Software Consortium
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
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.1.3 2014/07/12 11:58:16 spz 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 
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 #endif
87 
88 struct lease *new_leases (n, file, line)
89 	unsigned n;
90 	const char *file;
91 	int line;
92 {
93 	struct lease *rval;
94 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
95 	rval = dmalloc ((n + 1) * sizeof (struct lease), file, line);
96 	memset (rval, 0, sizeof (struct lease));
97 	rval -> starts = n;
98 	rval -> next = lease_hunks;
99 	lease_hunks = rval;
100 	rval++;
101 #else
102 	rval = dmalloc (n * sizeof (struct lease), file, line);
103 #endif
104 	return rval;
105 }
106 
107 /* If we are allocating leases in aggregations, there's really no way
108    to free one, although perhaps we can maintain a free list. */
109 
110 isc_result_t dhcp_lease_free (omapi_object_t *lo,
111 			      const char *file, int line)
112 {
113 	struct lease *lease;
114 	if (lo -> type != dhcp_type_lease)
115 		return DHCP_R_INVALIDARG;
116 	lease = (struct lease *)lo;
117 	memset (lease, 0, sizeof (struct lease));
118 	lease -> next = free_leases;
119 	free_leases = lease;
120 	return ISC_R_SUCCESS;
121 }
122 
123 isc_result_t dhcp_lease_get (omapi_object_t **lp,
124 			     const char *file, int line)
125 {
126 	struct lease **lease = (struct lease **)lp;
127 	struct lease *lt;
128 
129 	if (free_leases) {
130 		lt = free_leases;
131 		free_leases = lt -> next;
132 		*lease = lt;
133 		return ISC_R_SUCCESS;
134 	}
135 	return ISC_R_NOMEMORY;
136 }
137 #endif /* COMPACT_LEASES */
138 
139 OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
140 OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
141 OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
142 OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool)
143 
144 #if !defined (NO_HOST_FREES)	/* Scary debugging mode - don't enable! */
145 OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host)
146 #else
147 isc_result_t host_allocate (struct host_decl **p, const char *file, int line)
148 {
149 	return omapi_object_allocate ((omapi_object_t **)p,
150 				      dhcp_type_host, 0, file, line);
151 }
152 
153 isc_result_t host_reference (struct host_decl **pptr, struct host_decl *ptr,
154 			       const char *file, int line)
155 {
156 	return omapi_object_reference ((omapi_object_t **)pptr,
157 				       (omapi_object_t *)ptr, file, line);
158 }
159 
160 isc_result_t host_dereference (struct host_decl **ptr,
161 			       const char *file, int line)
162 {
163 	if ((*ptr) -> refcnt == 1) {
164 		log_error ("host dereferenced with refcnt == 1.");
165 #if defined (DEBUG_RC_HISTORY)
166 		dump_rc_history ();
167 #endif
168 		abort ();
169 	}
170 	return omapi_object_dereference ((omapi_object_t **)ptr, file, line);
171 }
172 #endif
173 
174 struct lease_state *free_lease_states;
175 
176 struct lease_state *new_lease_state (file, line)
177 	const char *file;
178 	int line;
179 {
180 	struct lease_state *rval;
181 
182 	if (free_lease_states) {
183 		rval = free_lease_states;
184 		free_lease_states =
185 			(struct lease_state *)(free_lease_states -> next);
186  		dmalloc_reuse (rval, file, line, 0);
187 	} else {
188 		rval = dmalloc (sizeof (struct lease_state), file, line);
189 		if (!rval)
190 			return rval;
191 	}
192 	memset (rval, 0, sizeof *rval);
193 	if (!option_state_allocate (&rval -> options, file, line)) {
194 		free_lease_state (rval, file, line);
195 		return (struct lease_state *)0;
196 	}
197 	return rval;
198 }
199 
200 void free_lease_state (ptr, file, line)
201 	struct lease_state *ptr;
202 	const char *file;
203 	int line;
204 {
205 	if (ptr -> options)
206 		option_state_dereference (&ptr -> options, file, line);
207 	if (ptr -> packet)
208 		packet_dereference (&ptr -> packet, file, line);
209 	if (ptr -> shared_network)
210 		shared_network_dereference (&ptr -> shared_network,
211 					    file, line);
212 
213 	data_string_forget (&ptr -> parameter_request_list, file, line);
214 	data_string_forget (&ptr -> filename, file, line);
215 	data_string_forget (&ptr -> server_name, file, line);
216 	ptr -> next = free_lease_states;
217 	free_lease_states = ptr;
218 	dmalloc_reuse (free_lease_states, (char *)0, 0, 0);
219 }
220 
221 #if defined (DEBUG_MEMORY_LEAKAGE) || \
222 		defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
223 void relinquish_free_lease_states ()
224 {
225 	struct lease_state *cs, *ns;
226 
227 	for (cs = free_lease_states; cs; cs = ns) {
228 		ns = cs -> next;
229 		dfree (cs, MDL);
230 	}
231 	free_lease_states = (struct lease_state *)0;
232 }
233 #endif
234 
235 struct permit *new_permit (file, line)
236 	const char *file;
237 	int line;
238 {
239 	struct permit *permit = ((struct permit *)
240 				 dmalloc (sizeof (struct permit), file, line));
241 	if (!permit)
242 		return permit;
243 	memset (permit, 0, sizeof *permit);
244 	return permit;
245 }
246 
247 void free_permit (permit, file, line)
248 	struct permit *permit;
249 	const char *file;
250 	int line;
251 {
252 	if (permit -> type == permit_class)
253 		class_dereference (&permit -> class, MDL);
254 	dfree (permit, file, line);
255 }
256