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