libnl 3.9.0
ae.c
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/**
37 * @ingroup xfrmnl
38 * @defgroup ae Attribute Element
39 * @brief
40 *
41 * The AE interface allows a user to retrieve and update various
42 * Security Association (SA) attributes such as lifetime, replay state etc.
43 *
44 * @par AE Flags
45 * @code
46 * XFRM_AE_UNSPEC
47 * XFRM_AE_RTHR=1
48 * XFRM_AE_RVAL=2
49 * XFRM_AE_LVAL=4
50 * XFRM_AE_ETHR=8
51 * XFRM_AE_CR=16
52 * XFRM_AE_CE=32
53 * XFRM_AE_CU=64
54 * @endcode
55 *
56 * @par AE Identification
57 * An AE is uniquely identified by the attributes listed below, whenever
58 * you refer to an existing AE all of the attributes must be set. There is
59 * no cache support for AE since you can retrieve the AE for any given combination
60 * of attributes mentioned below, but not all at once since they just characterize
61 * an SA.
62 * - destination address (xfrmnl_ae_set_daddr())
63 * - SPI (xfrmnl_ae_set_spi)
64 * - protocol (xfrmnl_ae_set_proto)
65 * - mark (xfrmnl_ae_set_mark)
66 *
67 * @par Changeable Attributes
68 * \anchor ae_changeable
69 * - current lifetime (xfrmnl_ae_set_curlifetime())
70 * - replay properties (xfrmnl_ae_set_replay_maxage(), xfrmnl_ae_set_replay_maxdiff())
71 * - replay state (xfrmnl_ae_set_replay_state(), xfrmnl_ae_set_replay_state_esn))
72 *
73 * @par Required Caches for Dumping
74 * None
75 *
76 * @par TODO
77 * None
78 *
79 * @par 1) Retrieving AE information for a given SA tuple
80 * @code
81 * // Create a netlink socket and connect it to XFRM subsystem in
82 * the kernel to be able to send/receive info from userspace.
83 * struct nl_sock* sk = nl_socket_alloc ();
84 * nl_connect (sk, NETLINK_XFRM);
85 *
86 * // AEs can then be looked up by the SA tuple, destination address,
87 * SPI, protocol, mark:
88 * struct xfrmnl_ae *ae;
89 * xfrmnl_ae_get_kernel(sk, dst_addr, spi, proto,mark_mask, mark_value, &ae);
90 *
91 * // After successful usage, the object must be freed
92 * xfrmnl_ae_put(ae);
93 * @endcode
94 *
95 * @par 2) Updating AE
96 * @code
97 * // Allocate an empty AE handle to be filled out with the attributes
98 * // of the new AE.
99 * struct xfrmnl_ae *ae = xfrmnl_ae_alloc();
100 *
101 * // Fill out the attributes of the new AE
102 * xfrmnl_ae_set_daddr(ae, dst_addr);
103 * xfrmnl_ae_set_spi(ae, 0xDEADBEEF);
104 * xfrmnl_ae_set_proto(ae, 50);
105 * xfrmnl_ae_set_mark(ae, 0x0);
106 * xfrmnl_ae_set_saddr(ae, src_addr);
107 * xfrmnl_ae_set_curlifetime(ae, 540, 10, 0xAABB1122, 0x0);
108 *
109 * // Build the netlink message and send it to the kernel, the operation will
110 * // block until the operation has been completed. Alternatively, a netlink message
111 * // can be built using xfrmnl_ae_build_get_request () API and be sent using
112 * // nl_send_auto(). Further the result from the kernel can be parsed using
113 * // xfrmnl_ae_parse() API.
114 * xfrmnl_ae_set(sk, ae, NLM_F_REPLACE);
115 *
116 * // Free the memory
117 * xfrmnl_ae_put(ae);
118 * @endcode
119 *
120 * @{
121 */
122
123#include "nl-default.h"
124
125#include <linux/xfrm.h>
126
127#include <netlink/netlink.h>
128#include <netlink/cache.h>
129#include <netlink/object.h>
130#include <netlink/xfrm/ae.h>
131
132#include "nl-xfrm.h"
133#include "nl-priv-dynamic-core/object-api.h"
134#include "nl-priv-dynamic-core/nl-core.h"
135#include "nl-priv-dynamic-core/cache-api.h"
136#include "nl-aux-core/nl-core.h"
137#include "nl-aux-xfrm/nl-xfrm.h"
138
139/** @cond SKIP */
140
141struct xfrmnl_sa_id {
142 struct nl_addr* daddr;
143 uint32_t spi;
144 uint16_t family;
145 uint8_t proto;
146};
147
148struct xfrmnl_ae {
149 NLHDR_COMMON
150
151 struct xfrmnl_sa_id sa_id;
152 struct nl_addr* saddr;
153 uint32_t flags;
154 uint32_t reqid;
155 struct xfrmnl_mark mark;
156 struct xfrmnl_lifetime_cur lifetime_cur;
157 uint32_t replay_maxage;
158 uint32_t replay_maxdiff;
159 struct xfrmnl_replay_state replay_state;
160 struct xfrmnl_replay_state_esn* replay_state_esn;
161};
162
163#define XFRM_AE_ATTR_DADDR 0x01
164#define XFRM_AE_ATTR_SPI 0x02
165#define XFRM_AE_ATTR_PROTO 0x04
166#define XFRM_AE_ATTR_SADDR 0x08
167#define XFRM_AE_ATTR_FLAGS 0x10
168#define XFRM_AE_ATTR_REQID 0x20
169#define XFRM_AE_ATTR_MARK 0x40
170#define XFRM_AE_ATTR_LIFETIME 0x80
171#define XFRM_AE_ATTR_REPLAY_MAXAGE 0x100
172#define XFRM_AE_ATTR_REPLAY_MAXDIFF 0x200
173#define XFRM_AE_ATTR_REPLAY_STATE 0x400
174#define XFRM_AE_ATTR_FAMILY 0x800
175
176static struct nl_object_ops xfrm_ae_obj_ops;
177/** @endcond */
178
179
180static void xfrm_ae_free_data(struct nl_object *c)
181{
182 struct xfrmnl_ae* ae = nl_object_priv (c);
183
184 if (ae == NULL)
185 return;
186
187 nl_addr_put (ae->sa_id.daddr);
188 nl_addr_put (ae->saddr);
189
190 if (ae->replay_state_esn)
191 free (ae->replay_state_esn);
192}
193
194static int xfrm_ae_clone(struct nl_object *_dst, struct nl_object *_src)
195{
196 struct xfrmnl_ae* dst = nl_object_priv(_dst);
197 struct xfrmnl_ae* src = nl_object_priv(_src);
198
199 dst->sa_id.daddr = NULL;
200 dst->saddr = NULL;
201 dst->replay_state_esn = NULL;
202
203 if (src->sa_id.daddr) {
204 if ((dst->sa_id.daddr = nl_addr_clone (src->sa_id.daddr)) == NULL)
205 return -NLE_NOMEM;
206 }
207
208 if (src->saddr) {
209 if ((dst->saddr = nl_addr_clone (src->saddr)) == NULL)
210 return -NLE_NOMEM;
211 }
212
213 if (src->replay_state_esn) {
214 uint32_t len = sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * src->replay_state_esn->bmp_len);
215
216 if ((dst->replay_state_esn = malloc (len)) == NULL)
217 return -NLE_NOMEM;
218 memcpy (dst->replay_state_esn, src->replay_state_esn, len);
219 }
220
221 return 0;
222}
223
224static uint64_t xfrm_ae_compare(struct nl_object *_a, struct nl_object *_b,
225 uint64_t attrs, int flags)
226{
227 struct xfrmnl_ae* a = (struct xfrmnl_ae *) _a;
228 struct xfrmnl_ae* b = (struct xfrmnl_ae *) _b;
229 uint64_t diff = 0;
230 int found = 0;
231
232#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
233 diff |= _DIFF(XFRM_AE_ATTR_DADDR,
234 nl_addr_cmp(a->sa_id.daddr, b->sa_id.daddr));
235 diff |= _DIFF(XFRM_AE_ATTR_SPI, a->sa_id.spi != b->sa_id.spi);
236 diff |= _DIFF(XFRM_AE_ATTR_PROTO, a->sa_id.proto != b->sa_id.proto);
237 diff |= _DIFF(XFRM_AE_ATTR_SADDR, nl_addr_cmp(a->saddr, b->saddr));
238 diff |= _DIFF(XFRM_AE_ATTR_FLAGS, a->flags != b->flags);
239 diff |= _DIFF(XFRM_AE_ATTR_REQID, a->reqid != b->reqid);
240 diff |= _DIFF(XFRM_AE_ATTR_MARK,
241 (a->mark.v & a->mark.m) != (b->mark.v & b->mark.m));
242 diff |= _DIFF(XFRM_AE_ATTR_REPLAY_MAXAGE,
243 a->replay_maxage != b->replay_maxage);
244 diff |= _DIFF(XFRM_AE_ATTR_REPLAY_MAXDIFF,
245 a->replay_maxdiff != b->replay_maxdiff);
246
247 /* Compare replay states */
248 found = AVAILABLE_MISMATCH (a, b, XFRM_AE_ATTR_REPLAY_STATE);
249 if (found == 0) // attribute exists in both objects
250 {
251 if (((a->replay_state_esn != NULL) && (b->replay_state_esn == NULL)) ||
252 ((a->replay_state_esn == NULL) && (b->replay_state_esn != NULL)))
253 found |= 1;
254
255 if (found == 0) // same replay type. compare actual values
256 {
257 if (a->replay_state_esn)
258 {
259 if (a->replay_state_esn->bmp_len != b->replay_state_esn->bmp_len)
260 diff |= 1;
261 else
262 {
263 uint32_t len = sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * a->replay_state_esn->bmp_len);
264 diff |= memcmp (a->replay_state_esn, b->replay_state_esn, len);
265 }
266 }
267 else
268 {
269 if ((a->replay_state.oseq != b->replay_state.oseq) ||
270 (a->replay_state.seq != b->replay_state.seq) ||
271 (a->replay_state.bitmap != b->replay_state.bitmap))
272 diff |= 1;
273 }
274 }
275 }
276#undef _DIFF
277
278 return diff;
279}
280
281/**
282 * @name XFRM AE Attribute Translations
283 * @{
284 */
285static const struct trans_tbl ae_attrs[] =
286{
287 __ADD(XFRM_AE_ATTR_DADDR, daddr),
288 __ADD(XFRM_AE_ATTR_SPI, spi),
289 __ADD(XFRM_AE_ATTR_PROTO, protocol),
290 __ADD(XFRM_AE_ATTR_SADDR, saddr),
291 __ADD(XFRM_AE_ATTR_FLAGS, flags),
292 __ADD(XFRM_AE_ATTR_REQID, reqid),
293 __ADD(XFRM_AE_ATTR_MARK, mark),
294 __ADD(XFRM_AE_ATTR_LIFETIME, cur_lifetime),
295 __ADD(XFRM_AE_ATTR_REPLAY_MAXAGE, replay_maxage),
296 __ADD(XFRM_AE_ATTR_REPLAY_MAXDIFF, replay_maxdiff),
297 __ADD(XFRM_AE_ATTR_REPLAY_STATE, replay_state),
298};
299
300static char* xfrm_ae_attrs2str (int attrs, char *buf, size_t len)
301{
302 return __flags2str(attrs, buf, len, ae_attrs, ARRAY_SIZE(ae_attrs));
303}
304/** @} */
305
306/**
307 * @name XFRM AE Flags Translations
308 * @{
309 */
310
311static const struct trans_tbl ae_flags[] = {
312 __ADD(XFRM_AE_UNSPEC, unspecified),
313 __ADD(XFRM_AE_RTHR, replay threshold),
314 __ADD(XFRM_AE_RVAL, replay value),
315 __ADD(XFRM_AE_LVAL, lifetime value),
316 __ADD(XFRM_AE_ETHR, expiry time threshold),
317 __ADD(XFRM_AE_CR, replay update event),
318 __ADD(XFRM_AE_CE, timer expiry event),
319 __ADD(XFRM_AE_CU, policy update event),
320};
321
322char* xfrmnl_ae_flags2str(int flags, char *buf, size_t len)
323{
324 return __flags2str (flags, buf, len, ae_flags, ARRAY_SIZE(ae_flags));
325}
326
327int xfrmnl_ae_str2flag(const char *name)
328{
329 return __str2flags(name, ae_flags, ARRAY_SIZE(ae_flags));
330}
331/** @} */
332
333static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p)
334{
335 char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
336 struct xfrmnl_ae* ae = (struct xfrmnl_ae *) a;
337 char flags[128], buf[128];
338 time_t add_time, use_time;
339 struct tm *add_time_tm, *use_time_tm;
340 struct tm tm_buf;
341
342 nl_dump_line(p, "src %s dst %s \n", nl_addr2str(ae->saddr, src, sizeof(src)),
343 nl_addr2str(ae->sa_id.daddr, dst, sizeof(dst)));
344
345 nl_dump_line(p, "\tproto %s spi 0x%x reqid %u ",
346 nl_ip_proto2str (ae->sa_id.proto, buf, sizeof (buf)),
347 ae->sa_id.spi, ae->reqid);
348
349 xfrmnl_ae_flags2str(ae->flags, flags, sizeof (flags));
350 nl_dump_line(p, "flags %s(0x%x) mark mask/value 0x%x/0x%x \n", flags,
351 ae->flags, ae->mark.m, ae->mark.v);
352
353 nl_dump_line(p, "\tlifetime current: \n");
354 nl_dump_line(p, "\t\tbytes %llu packets %llu \n",
355 (long long unsigned)ae->lifetime_cur.bytes,
356 (long long unsigned)ae->lifetime_cur.packets);
357 if (ae->lifetime_cur.add_time != 0)
358 {
359 add_time = ae->lifetime_cur.add_time;
360 add_time_tm = gmtime_r (&add_time, &tm_buf);
361 strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
362 }
363 else
364 {
365 sprintf (flags, "%s", "-");
366 }
367
368 if (ae->lifetime_cur.use_time != 0)
369 {
370 use_time = ae->lifetime_cur.use_time;
371 use_time_tm = gmtime_r (&use_time, &tm_buf);
372 strftime (buf, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
373 }
374 else
375 {
376 sprintf (buf, "%s", "-");
377 }
378 nl_dump_line(p, "\t\tadd_time: %s, use_time: %s\n", flags, buf);
379
380 nl_dump_line(p, "\treplay info: \n");
381 nl_dump_line(p, "\t\tmax age %u max diff %u \n", ae->replay_maxage, ae->replay_maxdiff);
382
383 nl_dump_line(p, "\treplay state info: \n");
384 if (ae->replay_state_esn)
385 {
386 nl_dump_line(p, "\t\toseq %u seq %u oseq_hi %u seq_hi %u replay window: %u \n",
387 ae->replay_state_esn->oseq, ae->replay_state_esn->seq,
388 ae->replay_state_esn->oseq_hi, ae->replay_state_esn->seq_hi,
389 ae->replay_state_esn->replay_window);
390 }
391 else
392 {
393 nl_dump_line(p, "\t\toseq %u seq %u bitmap: %u \n", ae->replay_state.oseq,
394 ae->replay_state.seq, ae->replay_state.bitmap);
395 }
396
397 nl_dump(p, "\n");
398}
399
400static void xfrm_ae_dump_details(struct nl_object *a, struct nl_dump_params *p)
401{
402 xfrm_ae_dump_line(a, p);
403}
404
405static void xfrm_ae_dump_stats(struct nl_object *a, struct nl_dump_params *p)
406{
407 xfrm_ae_dump_details(a, p);
408}
409
410
411static int build_xfrm_ae_message(struct xfrmnl_ae *tmpl, int cmd, int flags,
412 struct nl_msg **result)
413{
414 struct nl_msg* msg;
415 struct xfrm_aevent_id ae_id;
416
417 if (!(tmpl->ce_mask & XFRM_AE_ATTR_DADDR) ||
418 !(tmpl->ce_mask & XFRM_AE_ATTR_SPI) ||
419 !(tmpl->ce_mask & XFRM_AE_ATTR_PROTO))
420 return -NLE_MISSING_ATTR;
421
422 memset(&ae_id, 0, sizeof(ae_id));
423
424 memcpy (&ae_id.sa_id.daddr, nl_addr_get_binary_addr (tmpl->sa_id.daddr), sizeof (uint8_t) * nl_addr_get_len (tmpl->sa_id.daddr));
425 ae_id.sa_id.spi = htonl(tmpl->sa_id.spi);
426 ae_id.sa_id.family = tmpl->sa_id.family;
427 ae_id.sa_id.proto = tmpl->sa_id.proto;
428
429 if (tmpl->ce_mask & XFRM_AE_ATTR_SADDR)
430 memcpy (&ae_id.saddr, nl_addr_get_binary_addr (tmpl->saddr), sizeof (uint8_t) * nl_addr_get_len (tmpl->saddr));
431
432 if (tmpl->ce_mask & XFRM_AE_ATTR_FLAGS)
433 ae_id.flags = tmpl->flags;
434
435 if (tmpl->ce_mask & XFRM_AE_ATTR_REQID)
436 ae_id.reqid = tmpl->reqid;
437
438 msg = nlmsg_alloc_simple(cmd, flags);
439 if (!msg)
440 return -NLE_NOMEM;
441
442 if (nlmsg_append(msg, &ae_id, sizeof(ae_id), NLMSG_ALIGNTO) < 0)
443 goto nla_put_failure;
444
445 if (tmpl->ce_mask & XFRM_AE_ATTR_MARK)
446 NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrmnl_mark), &tmpl->mark);
447
448 if (tmpl->ce_mask & XFRM_AE_ATTR_LIFETIME)
449 NLA_PUT (msg, XFRMA_LTIME_VAL, sizeof (struct xfrmnl_lifetime_cur), &tmpl->lifetime_cur);
450
451 if (tmpl->ce_mask & XFRM_AE_ATTR_REPLAY_MAXAGE)
452 NLA_PUT_U32 (msg, XFRMA_ETIMER_THRESH, tmpl->replay_maxage);
453
454 if (tmpl->ce_mask & XFRM_AE_ATTR_REPLAY_MAXDIFF)
455 NLA_PUT_U32 (msg, XFRMA_REPLAY_THRESH, tmpl->replay_maxdiff);
456
457 if (tmpl->ce_mask & XFRM_AE_ATTR_REPLAY_STATE) {
458 if (tmpl->replay_state_esn) {
459 uint32_t len = sizeof (struct xfrm_replay_state_esn) + (sizeof (uint32_t) * tmpl->replay_state_esn->bmp_len);
460 NLA_PUT (msg, XFRMA_REPLAY_ESN_VAL, len, tmpl->replay_state_esn);
461 }
462 else {
463 NLA_PUT (msg, XFRMA_REPLAY_VAL, sizeof (struct xfrmnl_replay_state), &tmpl->replay_state);
464 }
465 }
466
467 *result = msg;
468 return 0;
469
470nla_put_failure:
471 nlmsg_free(msg);
472 return -NLE_MSGSIZE;
473}
474
475/**
476 * @name XFRM AE Update
477 * @{
478 */
479
480int xfrmnl_ae_set(struct nl_sock* sk, struct xfrmnl_ae* ae, int flags)
481{
482 int err;
483 struct nl_msg *msg;
484
485 if ((err = build_xfrm_ae_message(ae, XFRM_MSG_NEWAE, flags|NLM_F_REPLACE, &msg)) < 0)
486 return err;
487
488 err = nl_send_auto_complete(sk, msg);
489 nlmsg_free(msg);
490 if (err < 0)
491 return err;
492
493 return nl_wait_for_ack(sk);
494}
495
496/** @} */
497
498/**
499 * @name XFRM AE Object Allocation/Freeage
500 * @{
501 */
502
503struct xfrmnl_ae* xfrmnl_ae_alloc(void)
504{
505 return (struct xfrmnl_ae*) nl_object_alloc(&xfrm_ae_obj_ops);
506}
507
508void xfrmnl_ae_put(struct xfrmnl_ae* ae)
509{
510 nl_object_put((struct nl_object *) ae);
511}
512
513/** @} */
514
515static struct nla_policy xfrm_ae_policy[XFRMA_MAX+1] = {
516 [XFRMA_LTIME_VAL] = { .minlen = sizeof(struct xfrm_lifetime_cur) },
517 [XFRMA_REPLAY_VAL] = { .minlen = sizeof(struct xfrm_replay_state) },
518 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
519 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
520 [XFRMA_SRCADDR] = { .minlen = sizeof(xfrm_address_t) },
521 [XFRMA_MARK] = { .minlen = sizeof(struct xfrm_mark) },
522 [XFRMA_REPLAY_ESN_VAL] = { .minlen = sizeof(struct xfrm_replay_state_esn) },
523};
524
525int xfrmnl_ae_parse(struct nlmsghdr *n, struct xfrmnl_ae **result)
526{
527 _nl_auto_xfrmnl_ae struct xfrmnl_ae *ae = NULL;
528 struct nlattr *tb[XFRMA_MAX + 1];
529 struct xfrm_aevent_id* ae_id;
530 int err;
531
532 ae = xfrmnl_ae_alloc();
533 if (!ae)
534 return -NLE_NOMEM;
535
536 ae->ce_msgtype = n->nlmsg_type;
537 ae_id = nlmsg_data(n);
538
539 err = nlmsg_parse(n, sizeof(struct xfrm_aevent_id), tb, XFRMA_MAX, xfrm_ae_policy);
540 if (err < 0)
541 return err;
542
543 if (!(ae->sa_id.daddr =
544 _nl_addr_build(ae_id->sa_id.family, &ae_id->sa_id.daddr)))
545 return -NLE_NOMEM;
546 ae->sa_id.family= ae_id->sa_id.family;
547 ae->sa_id.spi = ntohl(ae_id->sa_id.spi);
548 ae->sa_id.proto = ae_id->sa_id.proto;
549 if (!(ae->saddr = _nl_addr_build(ae_id->sa_id.family, &ae_id->saddr)))
550 return -NLE_NOMEM;
551 ae->reqid = ae_id->reqid;
552 ae->flags = ae_id->flags;
553 ae->ce_mask |= (XFRM_AE_ATTR_DADDR | XFRM_AE_ATTR_FAMILY | XFRM_AE_ATTR_SPI |
554 XFRM_AE_ATTR_PROTO | XFRM_AE_ATTR_SADDR | XFRM_AE_ATTR_REQID |
555 XFRM_AE_ATTR_FLAGS);
556
557 if (tb[XFRMA_MARK]) {
558 struct xfrm_mark* m = nla_data(tb[XFRMA_MARK]);
559 ae->mark.m = m->m;
560 ae->mark.v = m->v;
561 ae->ce_mask |= XFRM_AE_ATTR_MARK;
562 }
563
564 if (tb[XFRMA_LTIME_VAL]) {
565 struct xfrm_lifetime_cur* cur = nla_data(tb[XFRMA_LTIME_VAL]);
566
567 ae->lifetime_cur.bytes = cur->bytes;
568 ae->lifetime_cur.packets = cur->packets;
569 ae->lifetime_cur.add_time = cur->add_time;
570 ae->lifetime_cur.use_time = cur->use_time;
571 ae->ce_mask |= XFRM_AE_ATTR_LIFETIME;
572 }
573
574 if (tb[XFRM_AE_ETHR]) {
575 ae->replay_maxage = *(uint32_t*)nla_data(tb[XFRM_AE_ETHR]);
576 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_MAXAGE;
577 }
578
579 if (tb[XFRM_AE_RTHR]) {
580 ae->replay_maxdiff = *(uint32_t*)nla_data(tb[XFRM_AE_RTHR]);
581 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_MAXDIFF;
582 }
583
584 if (tb[XFRMA_REPLAY_ESN_VAL]) {
585 struct xfrm_replay_state_esn* esn = nla_data (tb[XFRMA_REPLAY_ESN_VAL]);
586 uint32_t len = sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * esn->bmp_len);
587
588 if ((ae->replay_state_esn = calloc (1, len)) == NULL)
589 return -NLE_NOMEM;
590 ae->replay_state_esn->oseq = esn->oseq;
591 ae->replay_state_esn->seq = esn->seq;
592 ae->replay_state_esn->oseq_hi = esn->oseq_hi;
593 ae->replay_state_esn->seq_hi = esn->seq_hi;
594 ae->replay_state_esn->replay_window = esn->replay_window;
595 ae->replay_state_esn->bmp_len = esn->bmp_len;
596 memcpy (ae->replay_state_esn->bmp, esn->bmp, sizeof (uint32_t) * esn->bmp_len);
597 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_STATE;
598 }
599 else
600 {
601 struct xfrm_replay_state* replay_state = nla_data (tb[XFRMA_REPLAY_VAL]);
602 ae->replay_state.oseq = replay_state->oseq;
603 ae->replay_state.seq = replay_state->seq;
604 ae->replay_state.bitmap = replay_state->bitmap;
605 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_STATE;
606
607 ae->replay_state_esn = NULL;
608 }
609
610 *result = _nl_steal_pointer(&ae);
611 return 0;
612}
613
614static int xfrm_ae_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
615 struct nlmsghdr *n, struct nl_parser_param *pp)
616{
617 struct xfrmnl_ae* ae;
618 int err;
619
620 if ((err = xfrmnl_ae_parse(n, &ae)) < 0)
621 return err;
622
623 err = pp->pp_cb((struct nl_object *) ae, pp);
624
625 xfrmnl_ae_put(ae);
626 return err;
627}
628
629/**
630 * @name XFRM AE Get
631 * @{
632 */
633
634int xfrmnl_ae_build_get_request(struct nl_addr* daddr, unsigned int spi, unsigned int protocol,
635 unsigned int mark_mask, unsigned int mark_value, struct nl_msg **result)
636{
637 struct nl_msg *msg;
638 struct xfrm_aevent_id ae_id;
639 struct xfrmnl_mark mark;
640
641 if (!daddr || !spi)
642 {
643 fprintf(stderr, "APPLICATION BUG: %s:%d:%s: A valid destination address, spi must be specified\n",
644 __FILE__, __LINE__, __func__);
645 assert(0);
646 return -NLE_MISSING_ATTR;
647 }
648
649 memset(&ae_id, 0, sizeof(ae_id));
650 memcpy (&ae_id.sa_id.daddr, nl_addr_get_binary_addr (daddr), sizeof (uint8_t) * nl_addr_get_len (daddr));
651 ae_id.sa_id.spi = htonl(spi);
652 ae_id.sa_id.family = nl_addr_get_family (daddr);
653 ae_id.sa_id.proto = protocol;
654
655 if (!(msg = nlmsg_alloc_simple(XFRM_MSG_GETAE, 0)))
656 return -NLE_NOMEM;
657
658 if (nlmsg_append(msg, &ae_id, sizeof(ae_id), NLMSG_ALIGNTO) < 0)
659 goto nla_put_failure;
660
661 mark.m = mark_mask;
662 mark.v = mark_value;
663 NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrmnl_mark), &mark);
664
665 *result = msg;
666 return 0;
667
668nla_put_failure:
669 nlmsg_free(msg);
670 return -NLE_MSGSIZE;
671}
672
673int xfrmnl_ae_get_kernel(struct nl_sock* sock, struct nl_addr* daddr, unsigned int spi, unsigned int protocol,
674 unsigned int mark_mask, unsigned int mark_value, struct xfrmnl_ae** result)
675{
676 struct nl_msg *msg = NULL;
677 struct nl_object *obj;
678 int err;
679
680 if ((err = xfrmnl_ae_build_get_request(daddr, spi, protocol, mark_mask, mark_value, &msg)) < 0)
681 return err;
682
683 err = nl_send_auto(sock, msg);
684 nlmsg_free(msg);
685 if (err < 0)
686 return err;
687
688 if ((err = nl_pickup(sock, &xfrm_ae_msg_parser, &obj)) < 0)
689 return err;
690
691 /* We have used xfrm_ae_msg_parser(), object is definitely a xfrm ae */
692 *result = (struct xfrmnl_ae *) obj;
693
694 /* If an object has been returned, we also need to wait for the ACK */
695 if (err == 0 && obj)
696 nl_wait_for_ack(sock);
697
698 return 0;
699}
700
701/** @} */
702
703/**
704 * @name Attributes
705 * @{
706 */
707
708static inline int __assign_addr(struct xfrmnl_ae* ae, struct nl_addr **pos,
709 struct nl_addr *new, int flag, int nocheck)
710{
711 if (!nocheck) {
712 if (ae->ce_mask & XFRM_AE_ATTR_FAMILY) {
713 if (nl_addr_get_family (new) != ae->sa_id.family)
714 return -NLE_AF_MISMATCH;
715 } else {
716 ae->sa_id.family = nl_addr_get_family (new);
717 ae->ce_mask |= XFRM_AE_ATTR_FAMILY;
718 }
719 }
720
721 if (*pos)
722 nl_addr_put(*pos);
723
724 nl_addr_get(new);
725 *pos = new;
726
727 ae->ce_mask |= flag;
728
729 return 0;
730}
731
732
733struct nl_addr* xfrmnl_ae_get_daddr (struct xfrmnl_ae* ae)
734{
735 if (ae->ce_mask & XFRM_AE_ATTR_DADDR)
736 return ae->sa_id.daddr;
737 else
738 return NULL;
739}
740
741int xfrmnl_ae_set_daddr (struct xfrmnl_ae* ae, struct nl_addr* addr)
742{
743 return __assign_addr(ae, &ae->sa_id.daddr, addr, XFRM_AE_ATTR_DADDR, 0);
744}
745
746int xfrmnl_ae_get_spi (struct xfrmnl_ae* ae)
747{
748 if (ae->ce_mask & XFRM_AE_ATTR_SPI)
749 return ae->sa_id.spi;
750 else
751 return -1;
752}
753
754int xfrmnl_ae_set_spi (struct xfrmnl_ae* ae, unsigned int spi)
755{
756 ae->sa_id.spi = spi;
757 ae->ce_mask |= XFRM_AE_ATTR_SPI;
758
759 return 0;
760}
761
762int xfrmnl_ae_get_family (struct xfrmnl_ae* ae)
763{
764 if (ae->ce_mask & XFRM_AE_ATTR_FAMILY)
765 return ae->sa_id.family;
766 else
767 return -1;
768}
769
770int xfrmnl_ae_set_family (struct xfrmnl_ae* ae, unsigned int family)
771{
772 ae->sa_id.family = family;
773 ae->ce_mask |= XFRM_AE_ATTR_FAMILY;
774
775 return 0;
776}
777
778int xfrmnl_ae_get_proto (struct xfrmnl_ae* ae)
779{
780 if (ae->ce_mask & XFRM_AE_ATTR_PROTO)
781 return ae->sa_id.proto;
782 else
783 return -1;
784}
785
786int xfrmnl_ae_set_proto (struct xfrmnl_ae* ae, unsigned int protocol)
787{
788 ae->sa_id.proto = protocol;
789 ae->ce_mask |= XFRM_AE_ATTR_PROTO;
790
791 return 0;
792}
793
794struct nl_addr* xfrmnl_ae_get_saddr (struct xfrmnl_ae* ae)
795{
796 if (ae->ce_mask & XFRM_AE_ATTR_SADDR)
797 return ae->saddr;
798 else
799 return NULL;
800}
801
802int xfrmnl_ae_set_saddr (struct xfrmnl_ae* ae, struct nl_addr* addr)
803{
804 return __assign_addr(ae, &ae->saddr, addr, XFRM_AE_ATTR_SADDR, 1);
805}
806
807int xfrmnl_ae_get_flags (struct xfrmnl_ae* ae)
808{
809 if (ae->ce_mask & XFRM_AE_ATTR_FLAGS)
810 return ae->flags;
811 else
812 return -1;
813}
814
815int xfrmnl_ae_set_flags (struct xfrmnl_ae* ae, unsigned int flags)
816{
817 ae->flags = flags;
818 ae->ce_mask |= XFRM_AE_ATTR_FLAGS;
819
820 return 0;
821}
822
823int xfrmnl_ae_get_reqid (struct xfrmnl_ae* ae)
824{
825 if (ae->ce_mask & XFRM_AE_ATTR_REQID)
826 return ae->reqid;
827 else
828 return -1;
829}
830
831int xfrmnl_ae_set_reqid (struct xfrmnl_ae* ae, unsigned int reqid)
832{
833 ae->reqid = reqid;
834 ae->ce_mask |= XFRM_AE_ATTR_REQID;
835
836 return 0;
837}
838
839int xfrmnl_ae_get_mark (struct xfrmnl_ae* ae, unsigned int* mark_mask, unsigned int* mark_value)
840{
841 if (mark_mask == NULL || mark_value == NULL)
842 return -1;
843
844 if (ae->ce_mask & XFRM_AE_ATTR_MARK)
845 {
846 *mark_mask = ae->mark.m;
847 *mark_value = ae->mark.v;
848
849 return 0;
850 }
851 else
852 return -1;
853}
854
855int xfrmnl_ae_set_mark (struct xfrmnl_ae* ae, unsigned int value, unsigned int mask)
856{
857 ae->mark.v = value;
858 ae->mark.m = mask;
859 ae->ce_mask |= XFRM_AE_ATTR_MARK;
860
861 return 0;
862}
863
864int xfrmnl_ae_get_curlifetime (struct xfrmnl_ae* ae, unsigned long long int* curr_bytes,
865 unsigned long long int* curr_packets, unsigned long long int* curr_add_time,
866 unsigned long long int* curr_use_time)
867{
868 if (curr_bytes == NULL || curr_packets == NULL || curr_add_time == NULL || curr_use_time == NULL)
869 return -1;
870
871 if (ae->ce_mask & XFRM_AE_ATTR_LIFETIME)
872 {
873 *curr_bytes = ae->lifetime_cur.bytes;
874 *curr_packets = ae->lifetime_cur.packets;
875 *curr_add_time = ae->lifetime_cur.add_time;
876 *curr_use_time = ae->lifetime_cur.use_time;
877
878 return 0;
879 }
880 else
881 return -1;
882}
883
884int xfrmnl_ae_set_curlifetime (struct xfrmnl_ae* ae, unsigned long long int curr_bytes,
885 unsigned long long int curr_packets, unsigned long long int curr_add_time,
886 unsigned long long int curr_use_time)
887{
888 ae->lifetime_cur.bytes = curr_bytes;
889 ae->lifetime_cur.packets = curr_packets;
890 ae->lifetime_cur.add_time = curr_add_time;
891 ae->lifetime_cur.use_time = curr_use_time;
892 ae->ce_mask |= XFRM_AE_ATTR_LIFETIME;
893
894 return 0;
895}
896
897int xfrmnl_ae_get_replay_maxage (struct xfrmnl_ae* ae)
898{
899 if (ae->ce_mask & XFRM_AE_ATTR_REPLAY_MAXAGE)
900 return ae->replay_maxage;
901 else
902 return -1;
903}
904
905int xfrmnl_ae_set_replay_maxage (struct xfrmnl_ae* ae, unsigned int replay_maxage)
906{
907 ae->replay_maxage = replay_maxage;
908 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_MAXAGE;
909
910 return 0;
911}
912
913int xfrmnl_ae_get_replay_maxdiff (struct xfrmnl_ae* ae)
914{
915 if (ae->ce_mask & XFRM_AE_ATTR_REPLAY_MAXDIFF)
916 return ae->replay_maxdiff;
917 else
918 return -1;
919}
920
921int xfrmnl_ae_set_replay_maxdiff (struct xfrmnl_ae* ae, unsigned int replay_maxdiff)
922{
923 ae->replay_maxdiff = replay_maxdiff;
924 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_MAXDIFF;
925
926 return 0;
927}
928
929int xfrmnl_ae_get_replay_state (struct xfrmnl_ae* ae, unsigned int* oseq, unsigned int* seq, unsigned int* bmp)
930{
931 if (ae->ce_mask & XFRM_AE_ATTR_REPLAY_STATE)
932 {
933 if (ae->replay_state_esn == NULL)
934 {
935 *oseq = ae->replay_state.oseq;
936 *seq = ae->replay_state.seq;
937 *bmp = ae->replay_state.bitmap;
938
939 return 0;
940 }
941 else
942 {
943 return -1;
944 }
945 }
946 else
947 return -1;
948}
949
950int xfrmnl_ae_set_replay_state (struct xfrmnl_ae* ae, unsigned int oseq, unsigned int seq, unsigned int bitmap)
951{
952 ae->replay_state.oseq = oseq;
953 ae->replay_state.seq = seq;
954 ae->replay_state.bitmap = bitmap;
955 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_STATE;
956
957 return 0;
958}
959
960int xfrmnl_ae_get_replay_state_esn(struct xfrmnl_ae* ae, unsigned int* oseq, unsigned int* seq, unsigned int* oseq_hi,
961 unsigned int* seq_hi, unsigned int* replay_window, unsigned int* bmp_len, unsigned int* bmp)
962{
963 if (ae->ce_mask & XFRM_AE_ATTR_REPLAY_STATE)
964 {
965 if (ae->replay_state_esn)
966 {
967 *oseq = ae->replay_state_esn->oseq;
968 *seq = ae->replay_state_esn->seq;
969 *oseq_hi= ae->replay_state_esn->oseq_hi;
970 *seq_hi = ae->replay_state_esn->seq_hi;
971 *replay_window = ae->replay_state_esn->replay_window;
972 *bmp_len = ae->replay_state_esn->bmp_len; // In number of 32 bit words
973 memcpy (bmp, ae->replay_state_esn->bmp, ae->replay_state_esn->bmp_len * sizeof (uint32_t));
974
975 return 0;
976 }
977 else
978 {
979 return -1;
980 }
981 }
982 else
983 return -1;
984}
985
986int xfrmnl_ae_set_replay_state_esn(struct xfrmnl_ae* ae, unsigned int oseq, unsigned int seq,
987 unsigned int oseq_hi, unsigned int seq_hi, unsigned int replay_window,
988 unsigned int bmp_len, unsigned int* bmp)
989{
990 /* Free the old replay ESN state and allocate new one */
991 if (ae->replay_state_esn)
992 free (ae->replay_state_esn);
993
994 if ((ae->replay_state_esn = calloc (1, sizeof (struct xfrmnl_replay_state_esn) + sizeof (uint32_t) * bmp_len)) == NULL)
995 return -1;
996
997 ae->replay_state_esn->oseq = oseq;
998 ae->replay_state_esn->seq = seq;
999 ae->replay_state_esn->oseq_hi = oseq_hi;
1000 ae->replay_state_esn->seq_hi = seq_hi;
1001 ae->replay_state_esn->replay_window = replay_window;
1002 ae->replay_state_esn->bmp_len = bmp_len; // In number of 32 bit words
1003 memcpy (ae->replay_state_esn->bmp, bmp, bmp_len * sizeof (uint32_t));
1004 ae->ce_mask |= XFRM_AE_ATTR_REPLAY_STATE;
1005
1006 return 0;
1007}
1008
1009/** @} */
1010
1011static struct nl_object_ops xfrm_ae_obj_ops = {
1012 .oo_name = "xfrm/ae",
1013 .oo_size = sizeof(struct xfrmnl_ae),
1014 .oo_free_data = xfrm_ae_free_data,
1015 .oo_clone = xfrm_ae_clone,
1016 .oo_dump = {
1017 [NL_DUMP_LINE] = xfrm_ae_dump_line,
1018 [NL_DUMP_DETAILS] = xfrm_ae_dump_details,
1019 [NL_DUMP_STATS] = xfrm_ae_dump_stats,
1020 },
1021 .oo_compare = xfrm_ae_compare,
1022 .oo_attrs2str = xfrm_ae_attrs2str,
1023 .oo_id_attrs = (XFRM_AE_ATTR_DADDR | XFRM_AE_ATTR_SPI | XFRM_AE_ATTR_PROTO),
1024};
1025
1026/** @} */
1027
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition addr.c:525
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
Definition addr.c:943
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
Definition addr.c:587
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition addr.c:495
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
Definition addr.c:895
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition addr.c:1001
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition addr.c:955
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition addr.c:541
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition attr.c:119
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition attr.h:159
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition attr.h:230
@ NLA_U32
32 bit integer
Definition attr.h:37
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:349
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition msg.c:108
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:566
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
Definition msg.c:216
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:450
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition nl.c:515
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1246
int nl_pickup(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result)
Pickup netlink answer, parse is and return object.
Definition nl.c:1177
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
Definition nl.c:1111
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1017
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
Dumping parameters.
Definition types.h:32
Attribute validation policy.
Definition attr.h:63
uint16_t minlen
Minimal length of payload required.
Definition attr.h:68