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 <netlink/xfrm/lifetime.h>
51 #include <netlink-private/netlink.h>
52 
ltime_cfg_destroy(struct xfrmnl_ltime_cfg * ltime)53 static void ltime_cfg_destroy(struct xfrmnl_ltime_cfg* ltime)
54 {
55 	if (!ltime)
56 		return;
57 
58 	if (ltime->refcnt != 1)
59 	{
60 		fprintf(stderr, "BUG: %s:%d\n", __FILE__, __LINE__);
61 		assert(0);
62 	}
63 
64 	free(ltime);
65 }
66 
67 /**
68  * @name Creating Selector
69  * @{
70  */
71 
72 /**
73  * Allocate new lifetime config object.
74  * @return Newly allocated lifetime config object or NULL
75  */
xfrmnl_ltime_cfg_alloc()76 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_alloc()
77 {
78 	struct xfrmnl_ltime_cfg* ltime;
79 
80 	ltime = calloc(1, sizeof(struct xfrmnl_ltime_cfg));
81 	if (!ltime)
82 		return NULL;
83 
84 	ltime->refcnt = 1;
85 
86 	return ltime;
87 }
88 
89 /**
90  * Clone existing lifetime config object.
91  * @arg ltime		Selector object.
92  * @return Newly allocated lifetime config object being a duplicate of the
93  *         specified lifetime config object or NULL if a failure occured.
94  */
xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg * ltime)95 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg* ltime)
96 {
97 	struct xfrmnl_ltime_cfg* new;
98 
99 	new = xfrmnl_ltime_cfg_alloc();
100 	if (new)
101 		memcpy ((void*)new, (void*)ltime, sizeof (struct xfrmnl_ltime_cfg));
102 
103 	return new;
104 }
105 
106 /** @} */
107 
108 /**
109  * @name Managing Usage References
110  * @{
111  */
112 
xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg * ltime)113 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg* ltime)
114 {
115 	ltime->refcnt++;
116 
117 	return ltime;
118 }
119 
xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg * ltime)120 void xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg* ltime)
121 {
122 	if (!ltime)
123 		return;
124 
125 	if (ltime->refcnt == 1)
126 		ltime_cfg_destroy(ltime);
127 	else
128 		ltime->refcnt--;
129 }
130 
131 /**
132  * Check whether an lifetime config object is shared.
133  * @arg addr		Selector object.
134  * @return Non-zero if the lifetime config object is shared, otherwise 0.
135  */
xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg * ltime)136 int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg* ltime)
137 {
138 	return ltime->refcnt > 1;
139 }
140 
141 /** @} */
142 
143 /**
144  * @name Miscellaneous
145  * @{
146  */
147 
148 /**
149  * Compares two lifetime config objects.
150  * @arg a		A lifetime config object.
151  * @arg b		Another lifetime config object.
152  *
153  * @return Non zero if difference is found, 0 otherwise if both
154  * the objects are identical.
155  */
xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg * a,struct xfrmnl_ltime_cfg * b)156 int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg* a, struct xfrmnl_ltime_cfg* b)
157 {
158 	/* Check for any differences */
159 	if ((a->soft_byte_limit != b->soft_byte_limit) ||
160 		(a->soft_packet_limit != b->soft_packet_limit) ||
161 		(a->hard_byte_limit != b->hard_byte_limit) ||
162 		(a->hard_packet_limit != b->hard_packet_limit) ||
163 		(a->soft_add_expires_seconds != b->soft_add_expires_seconds) ||
164 		(a->hard_add_expires_seconds != b->hard_add_expires_seconds) ||
165 		(a->soft_use_expires_seconds != b->soft_use_expires_seconds) ||
166 		(a->hard_use_expires_seconds != b->hard_use_expires_seconds))
167 		return 1;
168 
169 	/* The objects are identical */
170 	return 0;
171 }
172 
173 /** @} */
174 
175 /**
176  * @name Attributes
177  * @{
178  */
xfrmnl_ltime_cfg_get_soft_bytelimit(struct xfrmnl_ltime_cfg * ltime)179 unsigned long long xfrmnl_ltime_cfg_get_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime)
180 {
181 	return ltime->soft_byte_limit;
182 }
183 
xfrmnl_ltime_cfg_set_soft_bytelimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_byte_limit)184 int xfrmnl_ltime_cfg_set_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_byte_limit)
185 {
186 	ltime->soft_byte_limit = soft_byte_limit;
187 
188 	return 0;
189 }
190 
xfrmnl_ltime_cfg_get_hard_bytelimit(struct xfrmnl_ltime_cfg * ltime)191 unsigned long long xfrmnl_ltime_cfg_get_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime)
192 {
193 	return ltime->hard_byte_limit;
194 }
195 
xfrmnl_ltime_cfg_set_hard_bytelimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_byte_limit)196 int xfrmnl_ltime_cfg_set_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_byte_limit)
197 {
198 	ltime->hard_byte_limit = hard_byte_limit;
199 
200 	return 0;
201 }
202 
xfrmnl_ltime_cfg_get_soft_packetlimit(struct xfrmnl_ltime_cfg * ltime)203 unsigned long long xfrmnl_ltime_cfg_get_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime)
204 {
205 	return ltime->soft_packet_limit;
206 }
207 
xfrmnl_ltime_cfg_set_soft_packetlimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_packet_limit)208 int xfrmnl_ltime_cfg_set_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_packet_limit)
209 {
210 	ltime->soft_packet_limit = soft_packet_limit;
211 
212 	return 0;
213 }
214 
xfrmnl_ltime_cfg_get_hard_packetlimit(struct xfrmnl_ltime_cfg * ltime)215 unsigned long long xfrmnl_ltime_cfg_get_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime)
216 {
217 	return ltime->hard_packet_limit;
218 }
219 
xfrmnl_ltime_cfg_set_hard_packetlimit(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_packet_limit)220 int xfrmnl_ltime_cfg_set_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_packet_limit)
221 {
222 	ltime->hard_packet_limit = hard_packet_limit;
223 
224 	return 0;
225 }
226 
xfrmnl_ltime_cfg_get_soft_addexpires(struct xfrmnl_ltime_cfg * ltime)227 unsigned long long xfrmnl_ltime_cfg_get_soft_addexpires (struct xfrmnl_ltime_cfg* ltime)
228 {
229 	return ltime->soft_add_expires_seconds;
230 }
231 
xfrmnl_ltime_cfg_set_soft_addexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_add_expires_seconds)232 int xfrmnl_ltime_cfg_set_soft_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_add_expires_seconds)
233 {
234 	ltime->soft_add_expires_seconds = soft_add_expires_seconds;
235 
236 	return 0;
237 }
238 
xfrmnl_ltime_cfg_get_hard_addexpires(struct xfrmnl_ltime_cfg * ltime)239 unsigned long long xfrmnl_ltime_cfg_get_hard_addexpires (struct xfrmnl_ltime_cfg* ltime)
240 {
241 	return ltime->hard_add_expires_seconds;
242 }
243 
xfrmnl_ltime_cfg_set_hard_addexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_add_expires_seconds)244 int xfrmnl_ltime_cfg_set_hard_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_add_expires_seconds)
245 {
246 	ltime->hard_add_expires_seconds = hard_add_expires_seconds;
247 
248 	return 0;
249 }
250 
xfrmnl_ltime_cfg_get_soft_useexpires(struct xfrmnl_ltime_cfg * ltime)251 unsigned long long xfrmnl_ltime_cfg_get_soft_useexpires (struct xfrmnl_ltime_cfg* ltime)
252 {
253 	return ltime->soft_use_expires_seconds;
254 }
255 
xfrmnl_ltime_cfg_set_soft_useexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long soft_use_expires_seconds)256 int xfrmnl_ltime_cfg_set_soft_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_use_expires_seconds)
257 {
258 	ltime->soft_use_expires_seconds = soft_use_expires_seconds;
259 
260 	return 0;
261 }
262 
xfrmnl_ltime_cfg_get_hard_useexpires(struct xfrmnl_ltime_cfg * ltime)263 unsigned long long xfrmnl_ltime_cfg_get_hard_useexpires (struct xfrmnl_ltime_cfg* ltime)
264 {
265 	return ltime->hard_use_expires_seconds;
266 }
267 
xfrmnl_ltime_cfg_set_hard_useexpires(struct xfrmnl_ltime_cfg * ltime,unsigned long long hard_use_expires_seconds)268 int xfrmnl_ltime_cfg_set_hard_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_use_expires_seconds)
269 {
270 	ltime->hard_use_expires_seconds = hard_use_expires_seconds;
271 
272 	return 0;
273 }
274 
275 /** @} */
276