xref: /aosp_15_r20/external/libnl/lib/xfrm/lifetime.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
4  *
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *
10  *    Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  *    Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the
16  *    distribution.
17  *
18  *    Neither the name of Texas Instruments Incorporated nor the names of
19  *    its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 /**
36  * @ingroup xfrmnl
37  * @defgroup XFRM Lifetime Configuration Object
38  *
39  * Abstract data type representing XFRM SA lifetime properties
40  *
41  * @{
42  *
43  * Header
44  * ------
45  * ~~~~{.c}
46  * #include <netlink/xfrm/lifetime.h>
47  * ~~~~
48  */
49 
50 #include "nl-default.h"
51 
52 #include <netlink/xfrm/lifetime.h>
53 
54 #include "nl-xfrm.h"
55 
ltime_cfg_destroy(struct xfrmnl_ltime_cfg * ltime)56 static void ltime_cfg_destroy(struct xfrmnl_ltime_cfg* ltime)
57 {
58 	if (!ltime)
59 		return;
60 
61 	if (ltime->refcnt != 1)
62 	{
63 		fprintf(stderr, "BUG: %s:%d\n", __FILE__, __LINE__);
64 		assert(0);
65 	}
66 
67 	free(ltime);
68 }
69 
70 /**
71  * @name Creating Selector
72  * @{
73  */
74 
75 /**
76  * Allocate new lifetime config object.
77  * @return Newly allocated lifetime config object or NULL
78  */
xfrmnl_ltime_cfg_alloc()79 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_alloc()
80 {
81 	struct xfrmnl_ltime_cfg* ltime;
82 
83 	ltime = calloc(1, sizeof(struct xfrmnl_ltime_cfg));
84 	if (!ltime)
85 		return NULL;
86 
87 	ltime->refcnt = 1;
88 
89 	return ltime;
90 }
91 
92 /**
93  * Clone existing lifetime config object.
94  * @arg ltime		Selector object.
95  * @return Newly allocated lifetime config object being a duplicate of the
96  *         specified lifetime config object or NULL if a failure occured.
97  */
xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg * ltime)98 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg* ltime)
99 {
100 	struct xfrmnl_ltime_cfg* new;
101 
102 	new = xfrmnl_ltime_cfg_alloc();
103 	if (new)
104 		memcpy ((void*)new, (void*)ltime, sizeof (struct xfrmnl_ltime_cfg));
105 
106 	return new;
107 }
108 
109 /** @} */
110 
111 /**
112  * @name Managing Usage References
113  * @{
114  */
115 
xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg * ltime)116 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg* ltime)
117 {
118 	ltime->refcnt++;
119 
120 	return ltime;
121 }
122 
xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg * ltime)123 void xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg* ltime)
124 {
125 	if (!ltime)
126 		return;
127 
128 	if (ltime->refcnt == 1)
129 		ltime_cfg_destroy(ltime);
130 	else
131 		ltime->refcnt--;
132 }
133 
134 /**
135  * Check whether an lifetime config object is shared.
136  * @arg addr		Selector object.
137  * @return Non-zero if the lifetime config object is shared, otherwise 0.
138  */
xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg * ltime)139 int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg* ltime)
140 {
141 	return ltime->refcnt > 1;
142 }
143 
144 /** @} */
145 
146 /**
147  * @name Miscellaneous
148  * @{
149  */
150 
151 /**
152  * Compares two lifetime config objects.
153  * @arg a		A lifetime config object.
154  * @arg b		Another lifetime config object.
155  *
156  * @return Non zero if difference is found, 0 otherwise if both
157  * the objects are identical.
158  */
xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg * a,struct xfrmnl_ltime_cfg * b)159 int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg* a, struct xfrmnl_ltime_cfg* b)
160 {
161 	/* Check for any differences */
162 	if ((a->soft_byte_limit != b->soft_byte_limit) ||
163 		(a->soft_packet_limit != b->soft_packet_limit) ||
164 		(a->hard_byte_limit != b->hard_byte_limit) ||
165 		(a->hard_packet_limit != b->hard_packet_limit) ||
166 		(a->soft_add_expires_seconds != b->soft_add_expires_seconds) ||
167 		(a->hard_add_expires_seconds != b->hard_add_expires_seconds) ||
168 		(a->soft_use_expires_seconds != b->soft_use_expires_seconds) ||
169 		(a->hard_use_expires_seconds != b->hard_use_expires_seconds))
170 		return 1;
171 
172 	/* The objects are identical */
173 	return 0;
174 }
175 
176 /** @} */
177 
178 /**
179  * @name Attributes
180  * @{
181  */
xfrmnl_ltime_cfg_get_soft_bytelimit(struct xfrmnl_ltime_cfg * ltime)182 unsigned long long xfrmnl_ltime_cfg_get_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime)
183 {
184 	return ltime->soft_byte_limit;
185 }
186 
xfrmnl_ltime_cfg_set_soft_bytelimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_byte_limit)187 int xfrmnl_ltime_cfg_set_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_byte_limit)
188 {
189 	ltime->soft_byte_limit = soft_byte_limit;
190 
191 	return 0;
192 }
193 
xfrmnl_ltime_cfg_get_hard_bytelimit(struct xfrmnl_ltime_cfg * ltime)194 unsigned long long xfrmnl_ltime_cfg_get_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime)
195 {
196 	return ltime->hard_byte_limit;
197 }
198 
xfrmnl_ltime_cfg_set_hard_bytelimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_byte_limit)199 int xfrmnl_ltime_cfg_set_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_byte_limit)
200 {
201 	ltime->hard_byte_limit = hard_byte_limit;
202 
203 	return 0;
204 }
205 
xfrmnl_ltime_cfg_get_soft_packetlimit(struct xfrmnl_ltime_cfg * ltime)206 unsigned long long xfrmnl_ltime_cfg_get_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime)
207 {
208 	return ltime->soft_packet_limit;
209 }
210 
xfrmnl_ltime_cfg_set_soft_packetlimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_packet_limit)211 int xfrmnl_ltime_cfg_set_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_packet_limit)
212 {
213 	ltime->soft_packet_limit = soft_packet_limit;
214 
215 	return 0;
216 }
217 
xfrmnl_ltime_cfg_get_hard_packetlimit(struct xfrmnl_ltime_cfg * ltime)218 unsigned long long xfrmnl_ltime_cfg_get_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime)
219 {
220 	return ltime->hard_packet_limit;
221 }
222 
xfrmnl_ltime_cfg_set_hard_packetlimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_packet_limit)223 int xfrmnl_ltime_cfg_set_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_packet_limit)
224 {
225 	ltime->hard_packet_limit = hard_packet_limit;
226 
227 	return 0;
228 }
229 
xfrmnl_ltime_cfg_get_soft_addexpires(struct xfrmnl_ltime_cfg * ltime)230 unsigned long long xfrmnl_ltime_cfg_get_soft_addexpires (struct xfrmnl_ltime_cfg* ltime)
231 {
232 	return ltime->soft_add_expires_seconds;
233 }
234 
xfrmnl_ltime_cfg_set_soft_addexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_add_expires_seconds)235 int xfrmnl_ltime_cfg_set_soft_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_add_expires_seconds)
236 {
237 	ltime->soft_add_expires_seconds = soft_add_expires_seconds;
238 
239 	return 0;
240 }
241 
xfrmnl_ltime_cfg_get_hard_addexpires(struct xfrmnl_ltime_cfg * ltime)242 unsigned long long xfrmnl_ltime_cfg_get_hard_addexpires (struct xfrmnl_ltime_cfg* ltime)
243 {
244 	return ltime->hard_add_expires_seconds;
245 }
246 
xfrmnl_ltime_cfg_set_hard_addexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_add_expires_seconds)247 int xfrmnl_ltime_cfg_set_hard_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_add_expires_seconds)
248 {
249 	ltime->hard_add_expires_seconds = hard_add_expires_seconds;
250 
251 	return 0;
252 }
253 
xfrmnl_ltime_cfg_get_soft_useexpires(struct xfrmnl_ltime_cfg * ltime)254 unsigned long long xfrmnl_ltime_cfg_get_soft_useexpires (struct xfrmnl_ltime_cfg* ltime)
255 {
256 	return ltime->soft_use_expires_seconds;
257 }
258 
xfrmnl_ltime_cfg_set_soft_useexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_use_expires_seconds)259 int xfrmnl_ltime_cfg_set_soft_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_use_expires_seconds)
260 {
261 	ltime->soft_use_expires_seconds = soft_use_expires_seconds;
262 
263 	return 0;
264 }
265 
xfrmnl_ltime_cfg_get_hard_useexpires(struct xfrmnl_ltime_cfg * ltime)266 unsigned long long xfrmnl_ltime_cfg_get_hard_useexpires (struct xfrmnl_ltime_cfg* ltime)
267 {
268 	return ltime->hard_use_expires_seconds;
269 }
270 
xfrmnl_ltime_cfg_set_hard_useexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_use_expires_seconds)271 int xfrmnl_ltime_cfg_set_hard_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_use_expires_seconds)
272 {
273 	ltime->hard_use_expires_seconds = hard_use_expires_seconds;
274 
275 	return 0;
276 }
277 
278 /** @} */
279