libnl  3.2.26
sa.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 sa Security Association
38  * @brief
39  */
40 
41 #include <netlink-private/netlink.h>
42 #include <netlink/netlink.h>
43 #include <netlink/cache.h>
44 #include <netlink/object.h>
45 #include <netlink/xfrm/selector.h>
46 #include <netlink/xfrm/lifetime.h>
47 #include <time.h>
48 
49 /** @cond SKIP */
50 #define XFRM_SA_ATTR_SEL 0x01
51 #define XFRM_SA_ATTR_DADDR 0x02
52 #define XFRM_SA_ATTR_SPI 0x04
53 #define XFRM_SA_ATTR_PROTO 0x08
54 #define XFRM_SA_ATTR_SADDR 0x10
55 #define XFRM_SA_ATTR_LTIME_CFG 0x20
56 #define XFRM_SA_ATTR_LTIME_CUR 0x40
57 #define XFRM_SA_ATTR_STATS 0x80
58 #define XFRM_SA_ATTR_SEQ 0x100
59 #define XFRM_SA_ATTR_REQID 0x200
60 #define XFRM_SA_ATTR_FAMILY 0x400
61 #define XFRM_SA_ATTR_MODE 0x800
62 #define XFRM_SA_ATTR_REPLAY_WIN 0x1000
63 #define XFRM_SA_ATTR_FLAGS 0x2000
64 #define XFRM_SA_ATTR_ALG_AEAD 0x4000
65 #define XFRM_SA_ATTR_ALG_AUTH 0x8000
66 #define XFRM_SA_ATTR_ALG_CRYPT 0x10000
67 #define XFRM_SA_ATTR_ALG_COMP 0x20000
68 #define XFRM_SA_ATTR_ENCAP 0x40000
69 #define XFRM_SA_ATTR_TFCPAD 0x80000
70 #define XFRM_SA_ATTR_COADDR 0x100000
71 #define XFRM_SA_ATTR_MARK 0x200000
72 #define XFRM_SA_ATTR_SECCTX 0x400000
73 #define XFRM_SA_ATTR_REPLAY_MAXAGE 0x800000
74 #define XFRM_SA_ATTR_REPLAY_MAXDIFF 0x1000000
75 #define XFRM_SA_ATTR_REPLAY_STATE 0x2000000
76 #define XFRM_SA_ATTR_EXPIRE 0x4000000
77 
78 static struct nl_cache_ops xfrmnl_sa_ops;
79 static struct nl_object_ops xfrm_sa_obj_ops;
80 /** @endcond */
81 
82 static void xfrm_sa_alloc_data(struct nl_object *c)
83 {
84  struct xfrmnl_sa* sa = nl_object_priv (c);
85 
86  if ((sa->sel = xfrmnl_sel_alloc ()) == NULL)
87  return;
88 
89  if ((sa->lft = xfrmnl_ltime_cfg_alloc ()) == NULL)
90  return;
91 }
92 
93 static void xfrm_sa_free_data(struct nl_object *c)
94 {
95  struct xfrmnl_sa* sa = nl_object_priv (c);
96 
97  if (sa == NULL)
98  return;
99 
100  xfrmnl_sel_put (sa->sel);
101  xfrmnl_ltime_cfg_put (sa->lft);
102  nl_addr_put (sa->id.daddr);
103  nl_addr_put (sa->saddr);
104 
105  if (sa->aead)
106  free (sa->aead);
107  if (sa->auth)
108  free (sa->auth);
109  if (sa->crypt)
110  free (sa->crypt);
111  if (sa->comp)
112  free (sa->comp);
113  if (sa->encap)
114  free (sa->encap);
115  if (sa->coaddr)
116  nl_addr_put (sa->coaddr);
117  if (sa->sec_ctx)
118  free (sa->sec_ctx);
119  if (sa->replay_state_esn)
120  free (sa->replay_state_esn);
121 }
122 
123 static int xfrm_sa_clone(struct nl_object *_dst, struct nl_object *_src)
124 {
125  struct xfrmnl_sa* dst = nl_object_priv(_dst);
126  struct xfrmnl_sa* src = nl_object_priv(_src);
127  uint32_t len = 0;
128 
129  if (src->sel)
130  if ((dst->sel = xfrmnl_sel_clone (src->sel)) == NULL)
131  return -NLE_NOMEM;
132 
133  if (src->lft)
134  if ((dst->lft = xfrmnl_ltime_cfg_clone (src->lft)) == NULL)
135  return -NLE_NOMEM;
136 
137  if (src->id.daddr)
138  if ((dst->id.daddr = nl_addr_clone (src->id.daddr)) == NULL)
139  return -NLE_NOMEM;
140 
141  if (src->saddr)
142  if ((dst->saddr = nl_addr_clone (src->saddr)) == NULL)
143  return -NLE_NOMEM;
144 
145  if (src->aead)
146  {
147  len = sizeof (struct xfrmnl_algo_aead) + ((src->aead->alg_key_len + 7) / 8);
148  if ((dst->aead = calloc (1, len)) == NULL)
149  return -NLE_NOMEM;
150  memcpy ((void *)dst->aead, (void *)src->aead, len);
151  }
152 
153  if (src->auth)
154  {
155  len = sizeof (struct xfrmnl_algo_auth) + ((src->auth->alg_key_len + 7) / 8);
156  if ((dst->auth = calloc (1, len)) == NULL)
157  return -NLE_NOMEM;
158  memcpy ((void *)dst->auth, (void *)src->auth, len);
159  }
160 
161  if (src->crypt)
162  {
163  len = sizeof (struct xfrmnl_algo) + ((src->crypt->alg_key_len + 7) / 8);
164  if ((dst->crypt = calloc (1, len)) == NULL)
165  return -NLE_NOMEM;
166  memcpy ((void *)dst->crypt, (void *)src->crypt, len);
167  }
168 
169  if (src->comp)
170  {
171  len = sizeof (struct xfrmnl_algo) + ((src->comp->alg_key_len + 7) / 8);
172  if ((dst->comp = calloc (1, len)) == NULL)
173  return -NLE_NOMEM;
174  memcpy ((void *)dst->comp, (void *)src->comp, len);
175  }
176 
177  if (src->encap)
178  {
179  len = sizeof (struct xfrmnl_encap_tmpl);
180  if ((dst->encap = calloc (1, len)) == NULL)
181  return -NLE_NOMEM;
182  memcpy ((void *)dst->encap, (void *)src->encap, len);
183  }
184 
185  if (src->coaddr)
186  if ((dst->coaddr = nl_addr_clone (src->coaddr)) == NULL)
187  return -NLE_NOMEM;
188 
189  if (src->sec_ctx)
190  {
191  len = sizeof (struct xfrmnl_sec_ctx) + src->sec_ctx->ctx_len;
192  if ((dst->sec_ctx = calloc (1, len)) == NULL)
193  return -NLE_NOMEM;
194  memcpy ((void *)dst->sec_ctx, (void *)src->sec_ctx, len);
195  }
196 
197  if (src->replay_state_esn)
198  {
199  len = sizeof (struct xfrmnl_replay_state_esn) + (src->replay_state_esn->bmp_len * sizeof (uint32_t));
200  if ((dst->replay_state_esn = calloc (1, len)) == NULL)
201  return -NLE_NOMEM;
202  memcpy ((void *)dst->replay_state_esn, (void *)src->replay_state_esn, len);
203  }
204 
205  return 0;
206 }
207 
208 static int xfrm_sa_compare(struct nl_object *_a, struct nl_object *_b, uint32_t attrs, int flags)
209 {
210  struct xfrmnl_sa* a = (struct xfrmnl_sa *) _a;
211  struct xfrmnl_sa* b = (struct xfrmnl_sa *) _b;
212  int diff = 0;
213  int found = 0;
214 
215 #define XFRM_SA_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, XFRM_SA_ATTR_##ATTR, a, b, EXPR)
216  diff |= XFRM_SA_DIFF(SEL, xfrmnl_sel_cmp(a->sel, b->sel));
217  diff |= XFRM_SA_DIFF(DADDR, nl_addr_cmp(a->id.daddr, b->id.daddr));
218  diff |= XFRM_SA_DIFF(SPI, a->id.spi != b->id.spi);
219  diff |= XFRM_SA_DIFF(PROTO, a->id.proto != b->id.proto);
220  diff |= XFRM_SA_DIFF(SADDR, nl_addr_cmp(a->saddr, b->saddr));
221  diff |= XFRM_SA_DIFF(LTIME_CFG, xfrmnl_ltime_cfg_cmp(a->lft, b->lft));
222  diff |= XFRM_SA_DIFF(REQID, a->reqid != b->reqid);
223  diff |= XFRM_SA_DIFF(FAMILY,a->family != b->family);
224  diff |= XFRM_SA_DIFF(MODE,a->mode != b->mode);
225  diff |= XFRM_SA_DIFF(REPLAY_WIN,a->replay_window != b->replay_window);
226  diff |= XFRM_SA_DIFF(FLAGS,a->flags != b->flags);
227  diff |= XFRM_SA_DIFF(ALG_AEAD,(strcmp(a->aead->alg_name, b->aead->alg_name) ||
228  (a->aead->alg_key_len != b->aead->alg_key_len) ||
229  (a->aead->alg_icv_len != b->aead->alg_icv_len) ||
230  memcmp(a->aead->alg_key, b->aead->alg_key,
231  ((a->aead->alg_key_len + 7)/8))));
232  diff |= XFRM_SA_DIFF(ALG_AUTH,(strcmp(a->auth->alg_name, b->auth->alg_name) ||
233  (a->auth->alg_key_len != b->auth->alg_key_len) ||
234  (a->auth->alg_trunc_len != b->auth->alg_trunc_len) ||
235  memcmp(a->auth->alg_key, b->auth->alg_key,
236  ((a->auth->alg_key_len + 7)/8))));
237  diff |= XFRM_SA_DIFF(ALG_CRYPT,(strcmp(a->crypt->alg_name, b->crypt->alg_name) ||
238  (a->crypt->alg_key_len != b->crypt->alg_key_len) ||
239  memcmp(a->crypt->alg_key, b->crypt->alg_key,
240  ((a->crypt->alg_key_len + 7)/8))));
241  diff |= XFRM_SA_DIFF(ALG_COMP,(strcmp(a->comp->alg_name, b->comp->alg_name) ||
242  (a->comp->alg_key_len != b->comp->alg_key_len) ||
243  memcmp(a->comp->alg_key, b->comp->alg_key,
244  ((a->comp->alg_key_len + 7)/8))));
245  diff |= XFRM_SA_DIFF(ENCAP,((a->encap->encap_type != b->encap->encap_type) ||
246  (a->encap->encap_sport != b->encap->encap_sport) ||
247  (a->encap->encap_dport != b->encap->encap_dport) ||
248  nl_addr_cmp(a->encap->encap_oa, b->encap->encap_oa)));
249  diff |= XFRM_SA_DIFF(TFCPAD,a->tfcpad != b->tfcpad);
250  diff |= XFRM_SA_DIFF(COADDR,nl_addr_cmp(a->coaddr, b->coaddr));
251  diff |= XFRM_SA_DIFF(MARK,(a->mark.m != b->mark.m) ||
252  (a->mark.v != b->mark.v));
253  diff |= XFRM_SA_DIFF(SECCTX,((a->sec_ctx->ctx_doi != b->sec_ctx->ctx_doi) ||
254  (a->sec_ctx->ctx_alg != b->sec_ctx->ctx_alg) ||
255  (a->sec_ctx->ctx_len != b->sec_ctx->ctx_len) ||
256  (a->sec_ctx->ctx_sid != b->sec_ctx->ctx_sid) ||
257  strcmp(a->sec_ctx->ctx_str, b->sec_ctx->ctx_str)));
258  diff |= XFRM_SA_DIFF(REPLAY_MAXAGE,a->replay_maxage != b->replay_maxage);
259  diff |= XFRM_SA_DIFF(REPLAY_MAXDIFF,a->replay_maxdiff != b->replay_maxdiff);
260  diff |= XFRM_SA_DIFF(EXPIRE,a->hard != b->hard);
261 
262  /* Compare replay states */
263  found = AVAILABLE_MISMATCH (a, b, XFRM_SA_ATTR_REPLAY_STATE);
264  if (found == 0) // attribute exists in both objects
265  {
266  if (((a->replay_state_esn != NULL) && (b->replay_state_esn == NULL)) ||
267  ((a->replay_state_esn == NULL) && (b->replay_state_esn != NULL)))
268  found |= 1;
269 
270  if (found == 0) // same replay type. compare actual values
271  {
272  if (a->replay_state_esn)
273  {
274  if (a->replay_state_esn->bmp_len != b->replay_state_esn->bmp_len)
275  diff |= 1;
276  else
277  {
278  uint32_t len = sizeof (struct xfrmnl_replay_state_esn) +
279  (a->replay_state_esn->bmp_len * sizeof (uint32_t));
280  diff |= memcmp (a->replay_state_esn, b->replay_state_esn, len);
281  }
282  }
283  else
284  {
285  if ((a->replay_state.oseq != b->replay_state.oseq) ||
286  (a->replay_state.seq != b->replay_state.seq) ||
287  (a->replay_state.bitmap != b->replay_state.bitmap))
288  diff |= 1;
289  }
290  }
291  }
292 #undef XFRM_SA_DIFF
293 
294  return diff;
295 }
296 
297 /**
298  * @name XFRM SA Attribute Translations
299  * @{
300  */
301 static const struct trans_tbl sa_attrs[] = {
302  __ADD(XFRM_SA_ATTR_SEL, selector),
303  __ADD(XFRM_SA_ATTR_DADDR, daddr),
304  __ADD(XFRM_SA_ATTR_SPI, spi),
305  __ADD(XFRM_SA_ATTR_PROTO, proto),
306  __ADD(XFRM_SA_ATTR_SADDR, saddr),
307  __ADD(XFRM_SA_ATTR_LTIME_CFG, lifetime_cfg),
308  __ADD(XFRM_SA_ATTR_LTIME_CUR, lifetime_cur),
309  __ADD(XFRM_SA_ATTR_STATS, stats),
310  __ADD(XFRM_SA_ATTR_SEQ, seqnum),
311  __ADD(XFRM_SA_ATTR_REQID, reqid),
312  __ADD(XFRM_SA_ATTR_FAMILY, family),
313  __ADD(XFRM_SA_ATTR_MODE, mode),
314  __ADD(XFRM_SA_ATTR_REPLAY_WIN, replay_window),
315  __ADD(XFRM_SA_ATTR_FLAGS, flags),
316  __ADD(XFRM_SA_ATTR_ALG_AEAD, alg_aead),
317  __ADD(XFRM_SA_ATTR_ALG_AUTH, alg_auth),
318  __ADD(XFRM_SA_ATTR_ALG_CRYPT, alg_crypto),
319  __ADD(XFRM_SA_ATTR_ALG_COMP, alg_comp),
320  __ADD(XFRM_SA_ATTR_ENCAP, encap),
321  __ADD(XFRM_SA_ATTR_TFCPAD, tfcpad),
322  __ADD(XFRM_SA_ATTR_COADDR, coaddr),
323  __ADD(XFRM_SA_ATTR_MARK, mark),
324  __ADD(XFRM_SA_ATTR_SECCTX, sec_ctx),
325  __ADD(XFRM_SA_ATTR_REPLAY_MAXAGE, replay_maxage),
326  __ADD(XFRM_SA_ATTR_REPLAY_MAXDIFF, replay_maxdiff),
327  __ADD(XFRM_SA_ATTR_REPLAY_STATE, replay_state),
328  __ADD(XFRM_SA_ATTR_EXPIRE, expire),
329 };
330 
331 static char* xfrm_sa_attrs2str(int attrs, char *buf, size_t len)
332 {
333  return __flags2str (attrs, buf, len, sa_attrs, ARRAY_SIZE(sa_attrs));
334 }
335 /** @} */
336 
337 /**
338  * @name XFRM SA Flags Translations
339  * @{
340  */
341 static const struct trans_tbl sa_flags[] = {
342  __ADD(XFRM_STATE_NOECN, no ecn),
343  __ADD(XFRM_STATE_DECAP_DSCP, decap dscp),
344  __ADD(XFRM_STATE_NOPMTUDISC, no pmtu discovery),
345  __ADD(XFRM_STATE_WILDRECV, wild receive),
346  __ADD(XFRM_STATE_ICMP, icmp),
347  __ADD(XFRM_STATE_AF_UNSPEC, unspecified),
348  __ADD(XFRM_STATE_ALIGN4, align4),
349  __ADD(XFRM_STATE_ESN, esn),
350 };
351 
352 char* xfrmnl_sa_flags2str(int flags, char *buf, size_t len)
353 {
354  return __flags2str (flags, buf, len, sa_flags, ARRAY_SIZE(sa_flags));
355 }
356 
357 int xfrmnl_sa_str2flag(const char *name)
358 {
359  return __str2flags (name, sa_flags, ARRAY_SIZE(sa_flags));
360 }
361 /** @} */
362 
363 /**
364  * @name XFRM SA Mode Translations
365  * @{
366  */
367 static const struct trans_tbl sa_modes[] = {
368  __ADD(XFRM_MODE_TRANSPORT, transport),
369  __ADD(XFRM_MODE_TUNNEL, tunnel),
370  __ADD(XFRM_MODE_ROUTEOPTIMIZATION, route optimization),
371  __ADD(XFRM_MODE_IN_TRIGGER, in trigger),
372  __ADD(XFRM_MODE_BEET, beet),
373 };
374 
375 char* xfrmnl_sa_mode2str(int mode, char *buf, size_t len)
376 {
377  return __type2str (mode, buf, len, sa_modes, ARRAY_SIZE(sa_modes));
378 }
379 
380 int xfrmnl_sa_str2mode(const char *name)
381 {
382  return __str2type (name, sa_modes, ARRAY_SIZE(sa_modes));
383 }
384 /** @} */
385 
386 
387 static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p)
388 {
389  char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
390  struct xfrmnl_sa* sa = (struct xfrmnl_sa *) a;
391  char flags[128], mode[128];
392  time_t add_time, use_time;
393  struct tm *add_time_tm, *use_time_tm;
394 
395  nl_dump_line(p, "src %s dst %s family: %s\n", nl_addr2str(sa->saddr, src, sizeof(src)),
396  nl_addr2str(sa->id.daddr, dst, sizeof(dst)),
397  nl_af2str (sa->family, flags, sizeof (flags)));
398 
399  nl_dump_line(p, "\tproto %s spi 0x%x reqid %u\n",
400  nl_ip_proto2str (sa->id.proto, flags, sizeof(flags)),
401  sa->id.spi, sa->reqid);
402 
403  xfrmnl_sa_flags2str(sa->flags, flags, sizeof (flags));
404  xfrmnl_sa_mode2str(sa->mode, mode, sizeof (mode));
405  nl_dump_line(p, "\tmode: %s flags: %s (0x%x) seq: %u replay window: %u\n",
406  mode, flags, sa->flags, sa->seq, sa->replay_window);
407 
408  nl_dump_line(p, "\tlifetime configuration: \n");
409  if (sa->lft->soft_byte_limit == XFRM_INF)
410  sprintf (flags, "INF");
411  else
412  sprintf (flags, "%" PRIu64, sa->lft->soft_byte_limit);
413  if (sa->lft->soft_packet_limit == XFRM_INF)
414  sprintf (mode, "INF");
415  else
416  sprintf (mode, "%" PRIu64, sa->lft->soft_packet_limit);
417  nl_dump_line(p, "\t\tsoft limit: %s (bytes), %s (packets)\n", flags, mode);
418  if (sa->lft->hard_byte_limit == XFRM_INF)
419  sprintf (flags, "INF");
420  else
421  sprintf (flags, "%" PRIu64, sa->lft->hard_byte_limit);
422  if (sa->lft->hard_packet_limit == XFRM_INF)
423  sprintf (mode, "INF");
424  else
425  sprintf (mode, "%" PRIu64, sa->lft->hard_packet_limit);
426  nl_dump_line(p, "\t\thard limit: %s (bytes), %s (packets)\n", flags, mode);
427  nl_dump_line(p, "\t\tsoft add_time: %llu (seconds), soft use_time: %llu (seconds) \n",
428  sa->lft->soft_add_expires_seconds, sa->lft->soft_use_expires_seconds);
429  nl_dump_line(p, "\t\thard add_time: %llu (seconds), hard use_time: %llu (seconds) \n",
430  sa->lft->hard_add_expires_seconds, sa->lft->hard_use_expires_seconds);
431 
432  nl_dump_line(p, "\tlifetime current: \n");
433  nl_dump_line(p, "\t\t%llu bytes, %llu packets\n", sa->curlft.bytes, sa->curlft.packets);
434  if (sa->curlft.add_time != 0)
435  {
436  add_time = sa->curlft.add_time;
437  add_time_tm = gmtime (&add_time);
438  strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
439  }
440  else
441  {
442  sprintf (flags, "%s", "-");
443  }
444 
445  if (sa->curlft.use_time != 0)
446  {
447  use_time = sa->curlft.use_time;
448  use_time_tm = gmtime (&use_time);
449  strftime (mode, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
450  }
451  else
452  {
453  sprintf (mode, "%s", "-");
454  }
455  nl_dump_line(p, "\t\tadd_time: %s, use_time: %s\n", flags, mode);
456 
457  if (sa->aead)
458  {
459  nl_dump_line(p, "\tAEAD Algo: \n");
460  nl_dump_line(p, "\t\tName: %s Key len(bits): %u ICV Len(bits): %u\n",
461  sa->aead->alg_name, sa->aead->alg_key_len, sa->aead->alg_icv_len);
462  }
463 
464  if (sa->auth)
465  {
466  nl_dump_line(p, "\tAuth Algo: \n");
467  nl_dump_line(p, "\t\tName: %s Key len(bits): %u Trunc len(bits): %u\n",
468  sa->auth->alg_name, sa->auth->alg_key_len, sa->auth->alg_trunc_len);
469  }
470 
471  if (sa->crypt)
472  {
473  nl_dump_line(p, "\tEncryption Algo: \n");
474  nl_dump_line(p, "\t\tName: %s Key len(bits): %u\n",
475  sa->crypt->alg_name, sa->crypt->alg_key_len);
476  }
477 
478  if (sa->comp)
479  {
480  nl_dump_line(p, "\tCompression Algo: \n");
481  nl_dump_line(p, "\t\tName: %s Key len(bits): %u\n",
482  sa->comp->alg_name, sa->comp->alg_key_len);
483  }
484 
485  if (sa->encap)
486  {
487  nl_dump_line(p, "\tEncapsulation template: \n");
488  nl_dump_line(p, "\t\tType: %d Src port: %d Dst port: %d Encap addr: %s\n",
489  sa->encap->encap_type, sa->encap->encap_sport, sa->encap->encap_dport,
490  nl_addr2str (sa->encap->encap_oa, dst, sizeof (dst)));
491  }
492 
493  if (sa->ce_mask & XFRM_SA_ATTR_TFCPAD)
494  nl_dump_line(p, "\tTFC Pad: %u\n", sa->tfcpad);
495 
496  if (sa->ce_mask & XFRM_SA_ATTR_COADDR)
497  nl_dump_line(p, "\tCO Address: %s\n", nl_addr2str (sa->coaddr, dst, sizeof (dst)));
498 
499  if (sa->ce_mask & XFRM_SA_ATTR_MARK)
500  nl_dump_line(p, "\tMark mask: 0x%x Mark value: 0x%x\n", sa->mark.m, sa->mark.v);
501 
502  if (sa->ce_mask & XFRM_SA_ATTR_SECCTX)
503  nl_dump_line(p, "\tDOI: %d Algo: %d Len: %u SID: %u ctx: %s\n", sa->sec_ctx->ctx_doi,
504  sa->sec_ctx->ctx_alg, sa->sec_ctx->ctx_len, sa->sec_ctx->ctx_sid, sa->sec_ctx->ctx_str);
505 
506  nl_dump_line(p, "\treplay info: \n");
507  nl_dump_line(p, "\t\tmax age %u max diff %u \n", sa->replay_maxage, sa->replay_maxdiff);
508 
509  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_STATE)
510  {
511  nl_dump_line(p, "\treplay state info: \n");
512  if (sa->replay_state_esn)
513  {
514  nl_dump_line(p, "\t\toseq %u seq %u oseq_hi %u seq_hi %u replay window: %u \n",
515  sa->replay_state_esn->oseq, sa->replay_state_esn->seq,
516  sa->replay_state_esn->oseq_hi, sa->replay_state_esn->seq_hi,
517  sa->replay_state_esn->replay_window);
518  }
519  else
520  {
521  nl_dump_line(p, "\t\toseq %u seq %u bitmap: %u \n", sa->replay_state.oseq,
522  sa->replay_state.seq, sa->replay_state.bitmap);
523  }
524  }
525 
526  nl_dump_line(p, "\tselector info: \n");
527  xfrmnl_sel_dump (sa->sel, p);
528 
529  nl_dump_line(p, "\tHard: %d\n", sa->hard);
530 
531  nl_dump(p, "\n");
532 }
533 
534 static void xfrm_sa_dump_stats(struct nl_object *a, struct nl_dump_params *p)
535 {
536  struct xfrmnl_sa* sa = (struct xfrmnl_sa*)a;
537 
538  nl_dump_line(p, "\tstats: \n");
539  nl_dump_line(p, "\t\treplay window: %u replay: %u integrity failed: %u \n",
540  sa->stats.replay_window, sa->stats.replay, sa->stats.integrity_failed);
541 
542  return;
543 }
544 
545 static void xfrm_sa_dump_details(struct nl_object *a, struct nl_dump_params *p)
546 {
547  xfrm_sa_dump_line(a, p);
548  xfrm_sa_dump_stats (a, p);
549 }
550 
551 /**
552  * @name XFRM SA Object Allocation/Freeage
553  * @{
554  */
555 
556 struct xfrmnl_sa* xfrmnl_sa_alloc(void)
557 {
558  return (struct xfrmnl_sa*) nl_object_alloc(&xfrm_sa_obj_ops);
559 }
560 
561 void xfrmnl_sa_put(struct xfrmnl_sa* sa)
562 {
563  nl_object_put((struct nl_object *) sa);
564 }
565 
566 /** @} */
567 
568 /**
569  * @name SA Cache Managament
570  * @{
571  */
572 
573 /**
574  * Build a SA cache including all SAs currently configured in the kernel.
575  * @arg sock Netlink socket.
576  * @arg result Pointer to store resulting cache.
577  *
578  * Allocates a new SA cache, initializes it properly and updates it
579  * to include all SAs currently configured in the kernel.
580  *
581  * @return 0 on success or a negative error code.
582  */
583 int xfrmnl_sa_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
584 {
585  return nl_cache_alloc_and_fill(&xfrmnl_sa_ops, sock, result);
586 }
587 
588 /**
589  * Look up a SA by destination address, SPI, protocol
590  * @arg cache SA cache
591  * @arg daddr destination address of the SA
592  * @arg spi SPI
593  * @arg proto protocol
594  * @return sa handle or NULL if no match was found.
595  */
596 struct xfrmnl_sa* xfrmnl_sa_get(struct nl_cache* cache, struct nl_addr* daddr,
597  unsigned int spi, unsigned int proto)
598 {
599  struct xfrmnl_sa *sa;
600 
601  //nl_list_for_each_entry(sa, &cache->c_items, ce_list) {
602  for (sa = (struct xfrmnl_sa*)nl_cache_get_first (cache);
603  sa != NULL;
604  sa = (struct xfrmnl_sa*)nl_cache_get_next ((struct nl_object*)sa))
605  {
606  if (sa->id.proto == proto &&
607  sa->id.spi == spi &&
608  !nl_addr_cmp(sa->id.daddr, daddr))
609  {
610  nl_object_get((struct nl_object *) sa);
611  return sa;
612  }
613 
614  }
615 
616  return NULL;
617 }
618 
619 
620 /** @} */
621 
622 
623 static struct nla_policy xfrm_sa_policy[XFRMA_MAX+1] = {
624  [XFRMA_SA] = { .minlen = sizeof(struct xfrm_usersa_info)},
625  [XFRMA_ALG_AUTH_TRUNC] = { .minlen = sizeof(struct xfrm_algo_auth)},
626  [XFRMA_ALG_AEAD] = { .minlen = sizeof(struct xfrm_algo_aead) },
627  [XFRMA_ALG_AUTH] = { .minlen = sizeof(struct xfrm_algo) },
628  [XFRMA_ALG_CRYPT] = { .minlen = sizeof(struct xfrm_algo) },
629  [XFRMA_ALG_COMP] = { .minlen = sizeof(struct xfrm_algo) },
630  [XFRMA_ENCAP] = { .minlen = sizeof(struct xfrm_encap_tmpl) },
631  [XFRMA_TMPL] = { .minlen = sizeof(struct xfrm_user_tmpl) },
632  [XFRMA_SEC_CTX] = { .minlen = sizeof(struct xfrm_sec_ctx) },
633  [XFRMA_LTIME_VAL] = { .minlen = sizeof(struct xfrm_lifetime_cur) },
634  [XFRMA_REPLAY_VAL] = { .minlen = sizeof(struct xfrm_replay_state) },
635  [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
636  [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
637  [XFRMA_SRCADDR] = { .minlen = sizeof(xfrm_address_t) },
638  [XFRMA_COADDR] = { .minlen = sizeof(xfrm_address_t) },
639  [XFRMA_MARK] = { .minlen = sizeof(struct xfrm_mark) },
640  [XFRMA_TFCPAD] = { .type = NLA_U32 },
641  [XFRMA_REPLAY_ESN_VAL] = { .minlen = sizeof(struct xfrm_replay_state_esn) },
642 };
643 
644 static int xfrm_sa_request_update(struct nl_cache *c, struct nl_sock *h)
645 {
646  struct xfrm_id sa_id;
647 
648  memset ((void *)&sa_id, 0, sizeof (struct xfrm_id));
649  return nl_send_simple (h, XFRM_MSG_GETSA, NLM_F_DUMP,(void*)&sa_id, sizeof (struct xfrm_id));
650 }
651 
652 int xfrmnl_sa_parse(struct nlmsghdr *n, struct xfrmnl_sa **result)
653 {
654  struct xfrmnl_sa* sa;
655  struct nlattr *tb[XFRMA_MAX + 1];
656  struct xfrm_usersa_info* sa_info;
657  struct xfrm_user_expire* ue;
658  int len, err;
659  struct nl_addr* addr;
660 
661  sa = xfrmnl_sa_alloc();
662  if (!sa) {
663  err = -NLE_NOMEM;
664  goto errout;
665  }
666 
667  sa->ce_msgtype = n->nlmsg_type;
668  if (n->nlmsg_type == XFRM_MSG_EXPIRE)
669  {
670  ue = nlmsg_data(n);
671  sa_info = &ue->state;
672  sa->hard = ue->hard;
673  sa->ce_mask |= XFRM_SA_ATTR_EXPIRE;
674  }
675  else if (n->nlmsg_type == XFRM_MSG_DELSA)
676  {
677  sa_info = (struct xfrm_usersa_info*)(nlmsg_data(n) + sizeof (struct xfrm_usersa_id) + NLA_HDRLEN);
678  }
679  else
680  {
681  sa_info = nlmsg_data(n);
682  }
683 
684  err = nlmsg_parse(n, sizeof(struct xfrm_usersa_info), tb, XFRMA_MAX, xfrm_sa_policy);
685  if (err < 0)
686  goto errout;
687 
688  if (sa_info->sel.family == AF_INET)
689  addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a4, sizeof (sa_info->sel.daddr.a4));
690  else
691  addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a6, sizeof (sa_info->sel.daddr.a6));
692  nl_addr_set_prefixlen (addr, sa_info->sel.prefixlen_d);
693  xfrmnl_sel_set_daddr (sa->sel, addr);
694  xfrmnl_sel_set_prefixlen_d (sa->sel, sa_info->sel.prefixlen_d);
695 
696  if (sa_info->sel.family == AF_INET)
697  addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a4, sizeof (sa_info->sel.saddr.a4));
698  else
699  addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a6, sizeof (sa_info->sel.saddr.a6));
700  nl_addr_set_prefixlen (addr, sa_info->sel.prefixlen_s);
701  xfrmnl_sel_set_saddr (sa->sel, addr);
702  xfrmnl_sel_set_prefixlen_s (sa->sel, sa_info->sel.prefixlen_s);
703 
704  xfrmnl_sel_set_dport (sa->sel, ntohs(sa_info->sel.dport));
705  xfrmnl_sel_set_dportmask (sa->sel, ntohs(sa_info->sel.dport_mask));
706  xfrmnl_sel_set_sport (sa->sel, ntohs(sa_info->sel.sport));
707  xfrmnl_sel_set_sportmask (sa->sel, ntohs(sa_info->sel.sport_mask));
708  xfrmnl_sel_set_family (sa->sel, sa_info->sel.family);
709  xfrmnl_sel_set_proto (sa->sel, sa_info->sel.proto);
710  xfrmnl_sel_set_ifindex (sa->sel, sa_info->sel.ifindex);
711  xfrmnl_sel_set_userid (sa->sel, sa_info->sel.user);
712  sa->ce_mask |= XFRM_SA_ATTR_SEL;
713 
714  if (sa_info->family == AF_INET)
715  sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a4, sizeof (sa_info->id.daddr.a4));
716  else
717  sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a6, sizeof (sa_info->id.daddr.a6));
718  sa->id.spi = ntohl(sa_info->id.spi);
719  sa->id.proto = sa_info->id.proto;
720  sa->ce_mask |= (XFRM_SA_ATTR_DADDR | XFRM_SA_ATTR_SPI | XFRM_SA_ATTR_PROTO);
721 
722  if (sa_info->family == AF_INET)
723  sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a4, sizeof (sa_info->saddr.a4));
724  else
725  sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a6, sizeof (sa_info->saddr.a6));
726  sa->ce_mask |= XFRM_SA_ATTR_SADDR;
727 
728  sa->lft->soft_byte_limit = sa_info->lft.soft_byte_limit;
729  sa->lft->hard_byte_limit = sa_info->lft.hard_byte_limit;
730  sa->lft->soft_packet_limit = sa_info->lft.soft_packet_limit;
731  sa->lft->hard_packet_limit = sa_info->lft.hard_packet_limit;
732  sa->lft->soft_add_expires_seconds = sa_info->lft.soft_add_expires_seconds;
733  sa->lft->hard_add_expires_seconds = sa_info->lft.hard_add_expires_seconds;
734  sa->lft->soft_use_expires_seconds = sa_info->lft.soft_use_expires_seconds;
735  sa->lft->hard_use_expires_seconds = sa_info->lft.hard_use_expires_seconds;
736  sa->ce_mask |= XFRM_SA_ATTR_LTIME_CFG;
737 
738  sa->curlft.bytes = sa_info->curlft.bytes;
739  sa->curlft.packets = sa_info->curlft.packets;
740  sa->curlft.add_time = sa_info->curlft.add_time;
741  sa->curlft.use_time = sa_info->curlft.use_time;
742  sa->ce_mask |= XFRM_SA_ATTR_LTIME_CUR;
743 
744  sa->stats.replay_window = sa_info->stats.replay_window;
745  sa->stats.replay = sa_info->stats.replay;
746  sa->stats.integrity_failed = sa_info->stats.integrity_failed;
747  sa->ce_mask |= XFRM_SA_ATTR_STATS;
748 
749  sa->seq = sa_info->seq;
750  sa->reqid = sa_info->reqid;
751  sa->family = sa_info->family;
752  sa->mode = sa_info->mode;
753  sa->replay_window = sa_info->replay_window;
754  sa->flags = sa_info->flags;
755  sa->ce_mask |= (XFRM_SA_ATTR_SEQ | XFRM_SA_ATTR_REQID |
756  XFRM_SA_ATTR_FAMILY | XFRM_SA_ATTR_MODE |
757  XFRM_SA_ATTR_REPLAY_WIN | XFRM_SA_ATTR_FLAGS);
758 
759  if (tb[XFRMA_ALG_AEAD]) {
760  struct xfrm_algo_aead* aead = nla_data(tb[XFRMA_ALG_AEAD]);
761  len = sizeof (struct xfrmnl_algo_aead) + ((aead->alg_key_len + 7) / 8);
762  if ((sa->aead = calloc (1, len)) == NULL)
763  {
764  err = -NLE_NOMEM;
765  goto errout;
766  }
767  memcpy ((void *)sa->aead, (void *)aead, len);
768  sa->ce_mask |= XFRM_SA_ATTR_ALG_AEAD;
769  }
770 
771  if (tb[XFRMA_ALG_AUTH_TRUNC]) {
772  struct xfrm_algo_auth* auth = nla_data(tb[XFRMA_ALG_AUTH_TRUNC]);
773  len = sizeof (struct xfrmnl_algo_auth) + ((auth->alg_key_len + 7) / 8);
774  if ((sa->auth = calloc (1, len)) == NULL)
775  {
776  err = -NLE_NOMEM;
777  goto errout;
778  }
779  memcpy ((void *)sa->auth, (void *)auth, len);
780  sa->ce_mask |= XFRM_SA_ATTR_ALG_AUTH;
781  }
782 
783  if (tb[XFRMA_ALG_AUTH] && !sa->auth) {
784  struct xfrm_algo* auth = nla_data(tb[XFRMA_ALG_AUTH]);
785  len = sizeof (struct xfrmnl_algo_auth) + ((auth->alg_key_len + 7) / 8);
786  if ((sa->auth = calloc (1, len)) == NULL)
787  {
788  err = -NLE_NOMEM;
789  goto errout;
790  }
791  strcpy(sa->auth->alg_name, auth->alg_name);
792  memcpy(sa->auth->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
793  sa->auth->alg_key_len = auth->alg_key_len;
794  sa->ce_mask |= XFRM_SA_ATTR_ALG_AUTH;
795  }
796 
797  if (tb[XFRMA_ALG_CRYPT]) {
798  struct xfrm_algo* crypt = nla_data(tb[XFRMA_ALG_CRYPT]);
799  len = sizeof (struct xfrmnl_algo) + ((crypt->alg_key_len + 7) / 8);
800  if ((sa->crypt = calloc (1, len)) == NULL)
801  {
802  err = -NLE_NOMEM;
803  goto errout;
804  }
805  memcpy ((void *)sa->crypt, (void *)crypt, len);
806  sa->ce_mask |= XFRM_SA_ATTR_ALG_CRYPT;
807  }
808 
809  if (tb[XFRMA_ALG_COMP]) {
810  struct xfrm_algo* comp = nla_data(tb[XFRMA_ALG_COMP]);
811  len = sizeof (struct xfrmnl_algo) + ((comp->alg_key_len + 7) / 8);
812  if ((sa->comp = calloc (1, len)) == NULL)
813  {
814  err = -NLE_NOMEM;
815  goto errout;
816  }
817  memcpy ((void *)sa->comp, (void *)comp, len);
818  sa->ce_mask |= XFRM_SA_ATTR_ALG_COMP;
819  }
820 
821  if (tb[XFRMA_ENCAP]) {
822  struct xfrm_encap_tmpl* encap = nla_data(tb[XFRMA_ENCAP]);
823  len = sizeof (struct xfrmnl_encap_tmpl);
824  if ((sa->encap = calloc (1, len)) == NULL)
825  {
826  err = -NLE_NOMEM;
827  goto errout;
828  }
829  sa->encap->encap_type = encap->encap_type;
830  sa->encap->encap_sport = ntohs(encap->encap_sport);
831  sa->encap->encap_dport = ntohs(encap->encap_dport);
832  if (sa_info->family == AF_INET)
833  sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a4, sizeof (encap->encap_oa.a4));
834  else
835  sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a6, sizeof (encap->encap_oa.a6));
836  sa->ce_mask |= XFRM_SA_ATTR_ENCAP;
837  }
838 
839  if (tb[XFRMA_TFCPAD]) {
840  sa->tfcpad = *(uint32_t*)nla_data(tb[XFRMA_TFCPAD]);
841  sa->ce_mask |= XFRM_SA_ATTR_TFCPAD;
842  }
843 
844  if (tb[XFRMA_COADDR]) {
845  if (sa_info->family == AF_INET)
846  {
847  sa->coaddr = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]),
848  sizeof (uint32_t));
849  }
850  else
851  {
852  sa->coaddr = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]),
853  sizeof (uint32_t) * 4);
854  }
855  sa->ce_mask |= XFRM_SA_ATTR_COADDR;
856  }
857 
858  if (tb[XFRMA_MARK]) {
859  struct xfrm_mark* m = nla_data(tb[XFRMA_MARK]);
860  sa->mark.m = m->m;
861  sa->mark.v = m->v;
862  sa->ce_mask |= XFRM_SA_ATTR_MARK;
863  }
864 
865  if (tb[XFRMA_SEC_CTX]) {
866  struct xfrm_sec_ctx* sec_ctx = nla_data(tb[XFRMA_SEC_CTX]);
867  len = sizeof (struct xfrmnl_sec_ctx) + sec_ctx->ctx_len;
868  if ((sa->sec_ctx = calloc (1, len)) == NULL)
869  {
870  err = -NLE_NOMEM;
871  goto errout;
872  }
873  memcpy ((void *)sa->sec_ctx, (void *)sec_ctx, len);
874  sa->ce_mask |= XFRM_SA_ATTR_SECCTX;
875  }
876 
877  if (tb[XFRMA_ETIMER_THRESH]) {
878  sa->replay_maxage = *(uint32_t*)nla_data(tb[XFRMA_ETIMER_THRESH]);
879  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_MAXAGE;
880  }
881 
882  if (tb[XFRMA_REPLAY_THRESH]) {
883  sa->replay_maxdiff = *(uint32_t*)nla_data(tb[XFRMA_REPLAY_THRESH]);
884  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_MAXDIFF;
885  }
886 
887  if (tb[XFRMA_REPLAY_ESN_VAL]) {
888  struct xfrm_replay_state_esn* esn = nla_data (tb[XFRMA_REPLAY_ESN_VAL]);
889  len = sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * esn->bmp_len);
890  if ((sa->replay_state_esn = calloc (1, len)) == NULL)
891  {
892  err = -NLE_NOMEM;
893  goto errout;
894  }
895  memcpy ((void *)sa->replay_state_esn, (void *)esn, len);
896  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_STATE;
897  }
898  else if (tb[XFRMA_REPLAY_VAL])
899  {
900  struct xfrm_replay_state* replay_state = nla_data (tb[XFRMA_REPLAY_VAL]);
901  sa->replay_state.oseq = replay_state->oseq;
902  sa->replay_state.seq = replay_state->seq;
903  sa->replay_state.bitmap = replay_state->bitmap;
904  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_STATE;
905  sa->replay_state_esn = NULL;
906  }
907 
908  *result = sa;
909  return 0;
910 
911 errout:
912  xfrmnl_sa_put(sa);
913  return err;
914 }
915 
916 static int xfrm_sa_update_cache (struct nl_cache *cache, struct nl_object *obj,
917  change_func_t change_cb, void *data)
918 {
919  struct nl_object* old_sa;
920  struct xfrmnl_sa* sa = (struct xfrmnl_sa*)obj;
921 
922  if (nl_object_get_msgtype (obj) == XFRM_MSG_EXPIRE)
923  {
924  /* On hard expiry, the SA gets deleted too from the kernel state without any
925  * further delete event. On Expire message, we are only updating the cache with
926  * the SA object's new state. In absence of the explicit delete event, the cache will
927  * be out of sync with the kernel state. To get around this, expiry messages cache
928  * operations are handled here (installed with NL_ACT_UNSPEC action) instead of
929  * in Libnl Cache module. */
930 
931  /* Do we already have this object in the cache? */
932  old_sa = nl_cache_search(cache, obj);
933  if (old_sa)
934  {
935  /* Found corresponding SA object in cache. Delete it */
936  nl_cache_remove (old_sa);
937  }
938 
939  /* Handle the expiry event now */
940  if (sa->hard == 0)
941  {
942  /* Soft expiry event: Save the new object to the
943  * cache and notify application of the expiry event. */
944  nl_cache_move (cache, obj);
945 
946  if (old_sa == NULL && change_cb)
947  {
948  /* Application CB present, no previous instance of SA object present.
949  * Notify application CB as a NEW event */
950  change_cb (cache, obj, NL_ACT_NEW, data);
951  }
952  else if (old_sa)
953  {
954  /* Application CB present, a previous instance of SA object present.
955  * Notify application CB as a CHANGE1 event */
956  if (nl_object_diff (old_sa, obj) && change_cb)
957  change_cb (cache, obj, NL_ACT_CHANGE, data);
958  nl_object_put (old_sa);
959  }
960  }
961  else
962  {
963  /* Hard expiry event: Delete the object from the
964  * cache and notify application of the expiry event. */
965  if (change_cb)
966  change_cb (cache, obj, NL_ACT_DEL, data);
967  nl_object_put (old_sa);
968  }
969 
970  /* Done handling expire message */
971  return 0;
972  }
973  else
974  {
975  /* All other messages other than Expire, let the standard Libnl cache
976  * module handle it. */
977  return nl_cache_include (cache, obj, change_cb, data);
978  }
979 }
980 
981 static int xfrm_sa_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
982  struct nlmsghdr *n, struct nl_parser_param *pp)
983 {
984  struct xfrmnl_sa* sa;
985  int err;
986 
987  if ((err = xfrmnl_sa_parse(n, &sa)) < 0)
988  return err;
989 
990  err = pp->pp_cb((struct nl_object *) sa, pp);
991 
992  xfrmnl_sa_put(sa);
993  return err;
994 }
995 
996 /**
997  * @name XFRM SA Get
998  * @{
999  */
1000 
1001 int xfrmnl_sa_build_get_request(struct nl_addr* daddr, unsigned int spi, unsigned int protocol, unsigned int mark_v, unsigned int mark_m, struct nl_msg **result)
1002 {
1003  struct nl_msg *msg;
1004  struct xfrm_usersa_id sa_id;
1005  struct xfrm_mark mark;
1006 
1007  if (!daddr || !spi)
1008  {
1009  fprintf(stderr, "APPLICATION BUG: %s:%d:%s: A valid destination address, spi must be specified\n",
1010  __FILE__, __LINE__, __PRETTY_FUNCTION__);
1011  assert(0);
1012  return -NLE_MISSING_ATTR;
1013  }
1014 
1015  memset(&sa_id, 0, sizeof(sa_id));
1016  memcpy (&sa_id.daddr, nl_addr_get_binary_addr (daddr), sizeof (uint8_t) * nl_addr_get_len (daddr));
1017  sa_id.family = nl_addr_get_family (daddr);
1018  sa_id.spi = htonl(spi);
1019  sa_id.proto = protocol;
1020 
1021  if (!(msg = nlmsg_alloc_simple(XFRM_MSG_GETSA, 0)))
1022  return -NLE_NOMEM;
1023 
1024  if (nlmsg_append(msg, &sa_id, sizeof(sa_id), NLMSG_ALIGNTO) < 0)
1025  goto nla_put_failure;
1026 
1027  if ((mark_m & mark_v) != 0)
1028  {
1029  memset(&mark, 0, sizeof(struct xfrm_mark));
1030  mark.m = mark_m;
1031  mark.v = mark_v;
1032 
1033  NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrm_mark), &mark);
1034  }
1035 
1036  *result = msg;
1037  return 0;
1038 
1039 nla_put_failure:
1040  nlmsg_free(msg);
1041  return -NLE_MSGSIZE;
1042 }
1043 
1044 int xfrmnl_sa_get_kernel(struct nl_sock* sock, struct nl_addr* daddr, unsigned int spi, unsigned int protocol, unsigned int mark_v, unsigned int mark_m, struct xfrmnl_sa** result)
1045 {
1046  struct nl_msg *msg = NULL;
1047  struct nl_object *obj;
1048  int err;
1049 
1050  if ((err = xfrmnl_sa_build_get_request(daddr, spi, protocol, mark_m, mark_v, &msg)) < 0)
1051  return err;
1052 
1053  err = nl_send_auto(sock, msg);
1054  nlmsg_free(msg);
1055  if (err < 0)
1056  return err;
1057 
1058  if ((err = nl_pickup(sock, &xfrm_sa_msg_parser, &obj)) < 0)
1059  return err;
1060 
1061  /* We have used xfrm_sa_msg_parser(), object is definitely a xfrm sa */
1062  *result = (struct xfrmnl_sa *) obj;
1063 
1064  /* If an object has been returned, we also need to wait for the ACK */
1065  if (err == 0 && obj)
1066  nl_wait_for_ack(sock);
1067 
1068  return 0;
1069 }
1070 
1071 /** @} */
1072 
1073 static int build_xfrm_sa_message(struct xfrmnl_sa *tmpl, int cmd, int flags, struct nl_msg **result)
1074 {
1075  struct nl_msg* msg;
1076  struct xfrm_usersa_info sa_info;
1077  uint32_t len;
1078  struct nl_addr* addr;
1079 
1080  if (!(tmpl->ce_mask & XFRM_SA_ATTR_DADDR) ||
1081  !(tmpl->ce_mask & XFRM_SA_ATTR_SPI) ||
1082  !(tmpl->ce_mask & XFRM_SA_ATTR_PROTO))
1083  return -NLE_MISSING_ATTR;
1084 
1085  memset ((void*)&sa_info, 0, sizeof (sa_info));
1086  if (tmpl->ce_mask & XFRM_SA_ATTR_SEL)
1087  {
1088  addr = xfrmnl_sel_get_daddr (tmpl->sel);
1089  memcpy ((void*)&sa_info.sel.daddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
1090  addr = xfrmnl_sel_get_saddr (tmpl->sel);
1091  memcpy ((void*)&sa_info.sel.saddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
1092  sa_info.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
1093  sa_info.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
1094  sa_info.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
1095  sa_info.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
1096  sa_info.sel.family = xfrmnl_sel_get_family (tmpl->sel);
1097  sa_info.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
1098  sa_info.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
1099  sa_info.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
1100  sa_info.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
1101  sa_info.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
1102  }
1103 
1104  memcpy (&sa_info.id.daddr, nl_addr_get_binary_addr (tmpl->id.daddr), sizeof (uint8_t) * nl_addr_get_len (tmpl->id.daddr));
1105  sa_info.id.spi = htonl(tmpl->id.spi);
1106  sa_info.id.proto = tmpl->id.proto;
1107 
1108  if (tmpl->ce_mask & XFRM_SA_ATTR_SADDR)
1109  memcpy (&sa_info.saddr, nl_addr_get_binary_addr (tmpl->saddr), sizeof (uint8_t) * nl_addr_get_len (tmpl->saddr));
1110 
1111  if (tmpl->ce_mask & XFRM_SA_ATTR_LTIME_CFG)
1112  {
1113  sa_info.lft.soft_byte_limit = xfrmnl_ltime_cfg_get_soft_bytelimit (tmpl->lft);
1114  sa_info.lft.hard_byte_limit = xfrmnl_ltime_cfg_get_hard_bytelimit (tmpl->lft);
1115  sa_info.lft.soft_packet_limit = xfrmnl_ltime_cfg_get_soft_packetlimit (tmpl->lft);
1116  sa_info.lft.hard_packet_limit = xfrmnl_ltime_cfg_get_hard_packetlimit (tmpl->lft);
1117  sa_info.lft.soft_add_expires_seconds = xfrmnl_ltime_cfg_get_soft_addexpires (tmpl->lft);
1118  sa_info.lft.hard_add_expires_seconds = xfrmnl_ltime_cfg_get_hard_addexpires (tmpl->lft);
1119  sa_info.lft.soft_use_expires_seconds = xfrmnl_ltime_cfg_get_soft_useexpires (tmpl->lft);
1120  sa_info.lft.hard_use_expires_seconds = xfrmnl_ltime_cfg_get_hard_useexpires (tmpl->lft);
1121  }
1122 
1123  //Skip current lifetime: cur lifetime can be updated only via AE
1124  //Skip stats: stats cant be updated
1125  //Skip seq: seq cant be updated
1126 
1127  if (tmpl->ce_mask & XFRM_SA_ATTR_REQID)
1128  sa_info.reqid = tmpl->reqid;
1129 
1130  if (tmpl->ce_mask & XFRM_SA_ATTR_FAMILY)
1131  sa_info.family = tmpl->family;
1132 
1133  if (tmpl->ce_mask & XFRM_SA_ATTR_MODE)
1134  sa_info.mode = tmpl->mode;
1135 
1136  if (tmpl->ce_mask & XFRM_SA_ATTR_REPLAY_WIN)
1137  sa_info.replay_window = tmpl->replay_window;
1138 
1139  if (tmpl->ce_mask & XFRM_SA_ATTR_FLAGS)
1140  sa_info.flags = tmpl->flags;
1141 
1142  msg = nlmsg_alloc_simple(cmd, flags);
1143  if (!msg)
1144  return -NLE_NOMEM;
1145 
1146  if (nlmsg_append(msg, &sa_info, sizeof(sa_info), NLMSG_ALIGNTO) < 0)
1147  goto nla_put_failure;
1148 
1149  if (tmpl->ce_mask & XFRM_SA_ATTR_ALG_AEAD) {
1150  len = sizeof (struct xfrm_algo_aead) + ((tmpl->aead->alg_key_len + 7) / 8);
1151  NLA_PUT (msg, XFRMA_ALG_AEAD, len, tmpl->aead);
1152  }
1153 
1154  if (tmpl->ce_mask & XFRM_SA_ATTR_ALG_AUTH) {
1155  struct xfrm_algo* auth;
1156  struct nlattr * auth_attr;
1157 
1158  len = sizeof (struct xfrm_algo) + ((tmpl->auth->alg_key_len + 7) / 8);
1159  auth_attr = nla_reserve(msg, XFRMA_ALG_AUTH, len);
1160  if (!auth_attr)
1161  goto nla_put_failure;
1162  auth = nla_data (auth_attr);
1163  strcpy(auth->alg_name, tmpl->auth->alg_name);
1164  memcpy(auth->alg_key, tmpl->auth->alg_key, (tmpl->auth->alg_key_len + 7) / 8);
1165  auth->alg_key_len = tmpl->auth->alg_key_len;
1166 
1167  len = sizeof (struct xfrm_algo_auth) + ((tmpl->auth->alg_key_len + 7) / 8);
1168  NLA_PUT (msg, XFRMA_ALG_AUTH_TRUNC, len, tmpl->auth);
1169  }
1170 
1171  if (tmpl->ce_mask & XFRM_SA_ATTR_ALG_CRYPT) {
1172  len = sizeof (struct xfrm_algo) + ((tmpl->crypt->alg_key_len + 7) / 8);
1173  NLA_PUT (msg, XFRMA_ALG_CRYPT, len, tmpl->crypt);
1174  }
1175 
1176  if (tmpl->ce_mask & XFRM_SA_ATTR_ALG_COMP) {
1177  len = sizeof (struct xfrm_algo) + ((tmpl->comp->alg_key_len + 7) / 8);
1178  NLA_PUT (msg, XFRMA_ALG_COMP, len, tmpl->comp);
1179  }
1180 
1181  if (tmpl->ce_mask & XFRM_SA_ATTR_ENCAP) {
1182  struct xfrm_encap_tmpl* encap_tmpl;
1183  struct nlattr* encap_attr;
1184 
1185  len = sizeof (struct xfrm_encap_tmpl);
1186  encap_attr = nla_reserve(msg, XFRMA_ENCAP, len);
1187  if (!encap_attr)
1188  goto nla_put_failure;
1189  encap_tmpl = nla_data (encap_attr);
1190  encap_tmpl->encap_type = tmpl->encap->encap_type;
1191  encap_tmpl->encap_sport = htons (tmpl->encap->encap_sport);
1192  encap_tmpl->encap_dport = htons (tmpl->encap->encap_dport);
1193  memcpy (&encap_tmpl->encap_oa, nl_addr_get_binary_addr (tmpl->encap->encap_oa), sizeof (uint8_t) * nl_addr_get_len (tmpl->encap->encap_oa));
1194  }
1195 
1196  if (tmpl->ce_mask & XFRM_SA_ATTR_TFCPAD) {
1197  NLA_PUT_U32 (msg, XFRMA_TFCPAD, tmpl->tfcpad);
1198  }
1199 
1200  if (tmpl->ce_mask & XFRM_SA_ATTR_COADDR) {
1201  NLA_PUT (msg, XFRMA_COADDR, sizeof (xfrm_address_t), tmpl->coaddr);
1202  }
1203 
1204  if (tmpl->ce_mask & XFRM_SA_ATTR_MARK) {
1205  NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrm_mark), &tmpl->mark);
1206  }
1207 
1208  if (tmpl->ce_mask & XFRM_SA_ATTR_SECCTX) {
1209  len = sizeof (struct xfrm_sec_ctx) + tmpl->sec_ctx->ctx_len;
1210  NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
1211  }
1212 
1213  if (tmpl->ce_mask & XFRM_SA_ATTR_REPLAY_MAXAGE) {
1214  NLA_PUT_U32 (msg, XFRMA_ETIMER_THRESH, tmpl->replay_maxage);
1215  }
1216 
1217  if (tmpl->ce_mask & XFRM_SA_ATTR_REPLAY_MAXDIFF) {
1218  NLA_PUT_U32 (msg, XFRMA_REPLAY_THRESH, tmpl->replay_maxdiff);
1219  }
1220 
1221  if (tmpl->ce_mask & XFRM_SA_ATTR_REPLAY_STATE) {
1222  if (tmpl->replay_state_esn) {
1223  len = sizeof (struct xfrm_replay_state_esn) + (sizeof (uint32_t) * tmpl->replay_state_esn->bmp_len);
1224  NLA_PUT (msg, XFRMA_REPLAY_ESN_VAL, len, tmpl->replay_state_esn);
1225  }
1226  else {
1227  NLA_PUT (msg, XFRMA_REPLAY_VAL, sizeof (struct xfrm_replay_state), &tmpl->replay_state);
1228  }
1229  }
1230 
1231  *result = msg;
1232  return 0;
1233 
1234 nla_put_failure:
1235  nlmsg_free(msg);
1236  return -NLE_MSGSIZE;
1237 }
1238 
1239 /**
1240  * @name XFRM SA Add
1241  * @{
1242  */
1243 
1244 int xfrmnl_sa_build_add_request(struct xfrmnl_sa* tmpl, int flags, struct nl_msg **result)
1245 {
1246  return build_xfrm_sa_message (tmpl, XFRM_MSG_NEWSA, flags, result);
1247 }
1248 
1249 int xfrmnl_sa_add(struct nl_sock* sk, struct xfrmnl_sa* tmpl, int flags)
1250 {
1251  int err;
1252  struct nl_msg *msg;
1253 
1254  if ((err = xfrmnl_sa_build_add_request(tmpl, flags, &msg)) < 0)
1255  return err;
1256 
1257  err = nl_send_auto_complete(sk, msg);
1258  nlmsg_free(msg);
1259  if (err < 0)
1260  return err;
1261 
1262  return nl_wait_for_ack(sk);
1263 }
1264 
1265 /**
1266  * @name XFRM SA Update
1267  * @{
1268  */
1269 
1270 int xfrmnl_sa_build_update_request(struct xfrmnl_sa* tmpl, int flags, struct nl_msg **result)
1271 {
1272  return build_xfrm_sa_message (tmpl, XFRM_MSG_UPDSA, flags, result);
1273 }
1274 
1275 int xfrmnl_sa_update(struct nl_sock* sk, struct xfrmnl_sa* tmpl, int flags)
1276 {
1277  int err;
1278  struct nl_msg *msg;
1279 
1280  if ((err = xfrmnl_sa_build_update_request(tmpl, flags, &msg)) < 0)
1281  return err;
1282 
1283  err = nl_send_auto_complete(sk, msg);
1284  nlmsg_free(msg);
1285  if (err < 0)
1286  return err;
1287 
1288  return nl_wait_for_ack(sk);
1289 }
1290 
1291 /** @} */
1292 
1293 static int build_xfrm_sa_delete_message(struct xfrmnl_sa *tmpl, int cmd, int flags, struct nl_msg **result)
1294 {
1295  struct nl_msg* msg;
1296  struct xfrm_usersa_id sa_id;
1297 
1298  if (!(tmpl->ce_mask & XFRM_SA_ATTR_DADDR) ||
1299  !(tmpl->ce_mask & XFRM_SA_ATTR_SPI) ||
1300  !(tmpl->ce_mask & XFRM_SA_ATTR_PROTO))
1301  return -NLE_MISSING_ATTR;
1302 
1303  memcpy (&sa_id.daddr, nl_addr_get_binary_addr (tmpl->id.daddr),
1304  sizeof (uint8_t) * nl_addr_get_len (tmpl->id.daddr));
1305  sa_id.family = nl_addr_get_family (tmpl->id.daddr);
1306  sa_id.spi = htonl(tmpl->id.spi);
1307  sa_id.proto = tmpl->id.proto;
1308 
1309  msg = nlmsg_alloc_simple(cmd, flags);
1310  if (!msg)
1311  return -NLE_NOMEM;
1312 
1313  if (nlmsg_append(msg, &sa_id, sizeof(sa_id), NLMSG_ALIGNTO) < 0)
1314  goto nla_put_failure;
1315 
1316  if (tmpl->ce_mask & XFRM_SA_ATTR_MARK) {
1317  NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrm_mark), &tmpl->mark);
1318  }
1319 
1320  *result = msg;
1321  return 0;
1322 
1323 nla_put_failure:
1324  nlmsg_free(msg);
1325  return -NLE_MSGSIZE;
1326 }
1327 
1328 /**
1329  * @name XFRM SA Delete
1330  * @{
1331  */
1332 
1333 int xfrmnl_sa_build_delete_request(struct xfrmnl_sa* tmpl, int flags, struct nl_msg **result)
1334 {
1335  return build_xfrm_sa_delete_message (tmpl, XFRM_MSG_DELSA, flags, result);
1336 }
1337 
1338 int xfrmnl_sa_delete(struct nl_sock* sk, struct xfrmnl_sa* tmpl, int flags)
1339 {
1340  int err;
1341  struct nl_msg *msg;
1342 
1343  if ((err = xfrmnl_sa_build_delete_request(tmpl, flags, &msg)) < 0)
1344  return err;
1345 
1346  err = nl_send_auto_complete(sk, msg);
1347  nlmsg_free(msg);
1348  if (err < 0)
1349  return err;
1350 
1351  return nl_wait_for_ack(sk);
1352 }
1353 
1354 /** @} */
1355 
1356 
1357 /**
1358  * @name Attributes
1359  * @{
1360  */
1361 
1362 struct xfrmnl_sel* xfrmnl_sa_get_sel (struct xfrmnl_sa* sa)
1363 {
1364  if (sa->ce_mask & XFRM_SA_ATTR_SEL)
1365  return sa->sel;
1366  else
1367  return NULL;
1368 }
1369 
1370 int xfrmnl_sa_set_sel (struct xfrmnl_sa* sa, struct xfrmnl_sel* sel)
1371 {
1372  /* Release any previously held selector object from the SA */
1373  if (sa->sel)
1374  xfrmnl_sel_put (sa->sel);
1375 
1376  /* Increment ref count on new selector and save it in the SA */
1377  xfrmnl_sel_get (sel);
1378  sa->sel = sel;
1379  sa->ce_mask |= XFRM_SA_ATTR_SEL;
1380 
1381  return 0;
1382 }
1383 
1384 static inline int __assign_addr(struct xfrmnl_sa* sa, struct nl_addr **pos,
1385  struct nl_addr *new, int flag, int nocheck)
1386 {
1387  if (!nocheck)
1388  {
1389  if (sa->ce_mask & XFRM_SA_ATTR_FAMILY)
1390  {
1391  if (nl_addr_get_family (new) != sa->family)
1392  return -NLE_AF_MISMATCH;
1393  }
1394  }
1395 
1396  if (*pos)
1397  nl_addr_put(*pos);
1398 
1399  nl_addr_get(new);
1400  *pos = new;
1401 
1402  sa->ce_mask |= flag;
1403 
1404  return 0;
1405 }
1406 
1407 
1408 struct nl_addr* xfrmnl_sa_get_daddr (struct xfrmnl_sa* sa)
1409 {
1410  if (sa->ce_mask & XFRM_SA_ATTR_DADDR)
1411  return sa->id.daddr;
1412  else
1413  return NULL;
1414 }
1415 
1416 int xfrmnl_sa_set_daddr (struct xfrmnl_sa* sa, struct nl_addr* addr)
1417 {
1418  return __assign_addr(sa, &sa->id.daddr, addr, XFRM_SA_ATTR_DADDR, 0);
1419 }
1420 
1421 int xfrmnl_sa_get_spi (struct xfrmnl_sa* sa)
1422 {
1423  if (sa->ce_mask & XFRM_SA_ATTR_SPI)
1424  return sa->id.spi;
1425  else
1426  return -1;
1427 }
1428 
1429 int xfrmnl_sa_set_spi (struct xfrmnl_sa* sa, unsigned int spi)
1430 {
1431  sa->id.spi = spi;
1432  sa->ce_mask |= XFRM_SA_ATTR_SPI;
1433 
1434  return 0;
1435 }
1436 
1437 int xfrmnl_sa_get_proto (struct xfrmnl_sa* sa)
1438 {
1439  if (sa->ce_mask & XFRM_SA_ATTR_PROTO)
1440  return sa->id.proto;
1441  else
1442  return -1;
1443 }
1444 
1445 int xfrmnl_sa_set_proto (struct xfrmnl_sa* sa, unsigned int protocol)
1446 {
1447  sa->id.proto = protocol;
1448  sa->ce_mask |= XFRM_SA_ATTR_PROTO;
1449 
1450  return 0;
1451 }
1452 
1453 struct nl_addr* xfrmnl_sa_get_saddr (struct xfrmnl_sa* sa)
1454 {
1455  if (sa->ce_mask & XFRM_SA_ATTR_SADDR)
1456  return sa->saddr;
1457  else
1458  return NULL;
1459 }
1460 
1461 int xfrmnl_sa_set_saddr (struct xfrmnl_sa* sa, struct nl_addr* addr)
1462 {
1463  return __assign_addr(sa, &sa->saddr, addr, XFRM_SA_ATTR_SADDR, 1);
1464 }
1465 
1466 struct xfrmnl_ltime_cfg* xfrmnl_sa_get_lifetime_cfg (struct xfrmnl_sa* sa)
1467 {
1468  if (sa->ce_mask & XFRM_SA_ATTR_LTIME_CFG)
1469  return sa->lft;
1470  else
1471  return NULL;
1472 }
1473 
1474 int xfrmnl_sa_set_lifetime_cfg (struct xfrmnl_sa* sa, struct xfrmnl_ltime_cfg* ltime)
1475 {
1476  /* Release any previously held lifetime cfg object from the SA */
1477  if (sa->lft)
1478  xfrmnl_ltime_cfg_put (sa->lft);
1479 
1480  /* Increment ref count on new lifetime object and save it in the SA */
1481  xfrmnl_ltime_cfg_get (ltime);
1482  sa->lft = ltime;
1483  sa->ce_mask |= XFRM_SA_ATTR_LTIME_CFG;
1484 
1485  return 0;
1486 }
1487 
1488 int xfrmnl_sa_get_curlifetime (struct xfrmnl_sa* sa, unsigned long long int* curr_bytes,
1489  unsigned long long int* curr_packets, unsigned long long int* curr_add_time, unsigned long long int* curr_use_time)
1490 {
1491  if (sa == NULL || curr_bytes == NULL || curr_packets == NULL || curr_add_time == NULL || curr_use_time == NULL)
1492  return -1;
1493 
1494  if (sa->ce_mask & XFRM_SA_ATTR_LTIME_CUR)
1495  {
1496  *curr_bytes = sa->curlft.bytes;
1497  *curr_packets = sa->curlft.packets;
1498  *curr_add_time = sa->curlft.add_time;
1499  *curr_use_time = sa->curlft.use_time;
1500  }
1501  else
1502  return -1;
1503 
1504  return 0;
1505 }
1506 
1507 int xfrmnl_sa_get_stats (struct xfrmnl_sa* sa, unsigned long long int* replay_window,
1508  unsigned long long int* replay, unsigned long long int* integrity_failed)
1509 {
1510  if (sa == NULL || replay_window == NULL || replay == NULL || integrity_failed == NULL)
1511  return -1;
1512 
1513  if (sa->ce_mask & XFRM_SA_ATTR_STATS)
1514  {
1515  *replay_window = sa->stats.replay_window;
1516  *replay = sa->stats.replay;
1517  *integrity_failed = sa->stats.integrity_failed;
1518  }
1519  else
1520  return -1;
1521 
1522  return 0;
1523 }
1524 
1525 int xfrmnl_sa_get_seq (struct xfrmnl_sa* sa)
1526 {
1527  if (sa->ce_mask & XFRM_SA_ATTR_SEQ)
1528  return sa->seq;
1529  else
1530  return -1;
1531 }
1532 
1533 int xfrmnl_sa_get_reqid (struct xfrmnl_sa* sa)
1534 {
1535  if (sa->ce_mask & XFRM_SA_ATTR_REQID)
1536  return sa->reqid;
1537  else
1538  return -1;
1539 }
1540 
1541 int xfrmnl_sa_set_reqid (struct xfrmnl_sa* sa, unsigned int reqid)
1542 {
1543  sa->reqid = reqid;
1544  sa->ce_mask |= XFRM_SA_ATTR_REQID;
1545 
1546  return 0;
1547 }
1548 
1549 int xfrmnl_sa_get_family (struct xfrmnl_sa* sa)
1550 {
1551  if (sa->ce_mask & XFRM_SA_ATTR_FAMILY)
1552  return sa->family;
1553  else
1554  return -1;
1555 }
1556 
1557 int xfrmnl_sa_set_family (struct xfrmnl_sa* sa, unsigned int family)
1558 {
1559  sa->family = family;
1560  sa->ce_mask |= XFRM_SA_ATTR_FAMILY;
1561 
1562  return 0;
1563 }
1564 
1565 int xfrmnl_sa_get_mode (struct xfrmnl_sa* sa)
1566 {
1567  if (sa->ce_mask & XFRM_SA_ATTR_MODE)
1568  return sa->mode;
1569  else
1570  return -1;
1571 }
1572 
1573 int xfrmnl_sa_set_mode (struct xfrmnl_sa* sa, unsigned int mode)
1574 {
1575  sa->mode = mode;
1576  sa->ce_mask |= XFRM_SA_ATTR_MODE;
1577 
1578  return 0;
1579 }
1580 
1581 int xfrmnl_sa_get_replay_window (struct xfrmnl_sa* sa)
1582 {
1583  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_WIN)
1584  return sa->replay_window;
1585  else
1586  return -1;
1587 }
1588 
1589 int xfrmnl_sa_set_replay_window (struct xfrmnl_sa* sa, unsigned int replay_window)
1590 {
1591  sa->replay_window = replay_window;
1592  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_WIN;
1593 
1594  return 0;
1595 }
1596 
1597 int xfrmnl_sa_get_flags (struct xfrmnl_sa* sa)
1598 {
1599  if (sa->ce_mask & XFRM_SA_ATTR_FLAGS)
1600  return sa->flags;
1601  else
1602  return -1;
1603 }
1604 
1605 int xfrmnl_sa_set_flags (struct xfrmnl_sa* sa, unsigned int flags)
1606 {
1607  sa->flags = flags;
1608  sa->ce_mask |= XFRM_SA_ATTR_FLAGS;
1609 
1610  return 0;
1611 }
1612 
1613 int xfrmnl_sa_get_aead_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int* key_len, unsigned int* icv_len, char* key)
1614 {
1615  if (sa->ce_mask & XFRM_SA_ATTR_ALG_AEAD)
1616  {
1617  strcpy (alg_name, sa->aead->alg_name);
1618  *key_len = sa->aead->alg_key_len;
1619  *icv_len = sa->aead->alg_icv_len;
1620  memcpy ((void *)key, (void *)sa->aead->alg_key, ((sa->aead->alg_key_len + 7)/8));
1621  }
1622  else
1623  return -1;
1624 
1625  return 0;
1626 }
1627 
1628 int xfrmnl_sa_set_aead_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int key_len, unsigned int icv_len, char* key)
1629 {
1630  uint32_t newlen = sizeof (struct xfrmnl_algo_aead) + (sizeof (uint8_t) * ((key_len + 7)/8));
1631 
1632  /* Free up the old key and allocate memory to hold new key */
1633  if (sa->aead)
1634  free (sa->aead);
1635  if ((sa->aead = calloc (1, newlen)) == NULL)
1636  return -1;
1637 
1638  /* Save the new info */
1639  strcpy (sa->aead->alg_name, alg_name);
1640  sa->aead->alg_key_len = key_len;
1641  sa->aead->alg_icv_len = icv_len;
1642  memcpy ((void *)sa->aead->alg_key, (void *)key, newlen);
1643 
1644  sa->ce_mask |= XFRM_SA_ATTR_ALG_AEAD;
1645 
1646  return 0;
1647 }
1648 
1649 int xfrmnl_sa_get_auth_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int* key_len, unsigned int* trunc_len, char* key)
1650 {
1651  if (sa->ce_mask & XFRM_SA_ATTR_ALG_AUTH)
1652  {
1653  strcpy (alg_name, sa->auth->alg_name);
1654  *key_len = sa->auth->alg_key_len;
1655  *trunc_len = sa->auth->alg_trunc_len;
1656  memcpy ((void *)key, (void *)sa->auth->alg_key, ((sa->auth->alg_key_len + 7)/8));
1657  }
1658  else
1659  return -1;
1660 
1661  return 0;
1662 }
1663 
1664 int xfrmnl_sa_set_auth_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int key_len, unsigned int trunc_len, char* key)
1665 {
1666  uint32_t newlen = sizeof (struct xfrmnl_algo_auth) + (sizeof (uint8_t) * ((key_len + 7)/8));
1667 
1668  /* Free up the old auth data and allocate new one */
1669  if (sa->auth)
1670  free (sa->auth);
1671  if ((sa->auth = calloc (1, newlen)) == NULL)
1672  return -1;
1673 
1674  /* Save the new info */
1675  strcpy (sa->auth->alg_name, alg_name);
1676  sa->auth->alg_key_len = key_len;
1677  sa->auth->alg_trunc_len = trunc_len;
1678  memcpy ((void *)sa->auth->alg_key, (void *)key, newlen);
1679 
1680  sa->ce_mask |= XFRM_SA_ATTR_ALG_AUTH;
1681 
1682  return 0;
1683 }
1684 
1685 int xfrmnl_sa_get_crypto_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int* key_len, char* key)
1686 {
1687  if (sa->ce_mask & XFRM_SA_ATTR_ALG_CRYPT)
1688  {
1689  strcpy (alg_name, sa->crypt->alg_name);
1690  *key_len = sa->crypt->alg_key_len;
1691  memcpy ((void *)key, (void *)sa->crypt->alg_key, ((sa->crypt->alg_key_len + 7)/8));
1692  }
1693  else
1694  return -1;
1695 
1696  return 0;
1697 }
1698 
1699 int xfrmnl_sa_set_crypto_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int key_len, char* key)
1700 {
1701  uint32_t newlen = sizeof (struct xfrmnl_algo) + (sizeof (uint8_t) * ((key_len + 7)/8));
1702 
1703  /* Free up the old crypto and allocate new one */
1704  if (sa->crypt)
1705  free (sa->crypt);
1706  if ((sa->crypt = calloc (1, newlen)) == NULL)
1707  return -1;
1708 
1709  /* Save the new info */
1710  strcpy (sa->crypt->alg_name, alg_name);
1711  sa->crypt->alg_key_len = key_len;
1712  memcpy ((void *)sa->crypt->alg_key, (void *)key, newlen);
1713 
1714  sa->ce_mask |= XFRM_SA_ATTR_ALG_CRYPT;
1715 
1716  return 0;
1717 }
1718 
1719 int xfrmnl_sa_get_comp_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int* key_len, char* key)
1720 {
1721  if (sa->ce_mask & XFRM_SA_ATTR_ALG_COMP)
1722  {
1723  strcpy (alg_name, sa->comp->alg_name);
1724  *key_len = sa->comp->alg_key_len;
1725  memcpy ((void *)key, (void *)sa->comp->alg_key, ((sa->comp->alg_key_len + 7)/8));
1726  }
1727  else
1728  return -1;
1729 
1730  return 0;
1731 }
1732 
1733 int xfrmnl_sa_set_comp_params (struct xfrmnl_sa* sa, char* alg_name, unsigned int key_len, char* key)
1734 {
1735  uint32_t newlen = sizeof (struct xfrmnl_algo) + (sizeof (uint8_t) * ((key_len + 7)/8));
1736 
1737  /* Free up the old compression algo params and allocate new one */
1738  if (sa->comp)
1739  free (sa->comp);
1740  if ((sa->comp = calloc (1, newlen)) == NULL)
1741  return -1;
1742 
1743  /* Save the new info */
1744  strcpy (sa->comp->alg_name, alg_name);
1745  sa->comp->alg_key_len = key_len;
1746  memcpy ((void *)sa->comp->alg_key, (void *)key, newlen);
1747 
1748  sa->ce_mask |= XFRM_SA_ATTR_ALG_COMP;
1749 
1750  return 0;
1751 }
1752 
1753 int xfrmnl_sa_get_encap_tmpl (struct xfrmnl_sa* sa, unsigned int* encap_type, unsigned int* encap_sport, unsigned int* encap_dport, struct nl_addr** encap_oa)
1754 {
1755  if (sa->ce_mask & XFRM_SA_ATTR_ENCAP)
1756  {
1757  *encap_type = sa->encap->encap_type;
1758  *encap_sport = sa->encap->encap_sport;
1759  *encap_dport = sa->encap->encap_dport;
1760  *encap_oa = nl_addr_clone (sa->encap->encap_oa);
1761  }
1762  else
1763  return -1;
1764 
1765  return 0;
1766 }
1767 
1768 int xfrmnl_sa_set_encap_tmpl (struct xfrmnl_sa* sa, unsigned int encap_type, unsigned int encap_sport, unsigned int encap_dport, struct nl_addr* encap_oa)
1769 {
1770  /* Free up the old encap OA */
1771  if (sa->encap->encap_oa)
1772  nl_addr_put (sa->encap->encap_oa);
1773 
1774  /* Save the new info */
1775  sa->encap->encap_type = encap_type;
1776  sa->encap->encap_sport = encap_sport;
1777  sa->encap->encap_dport = encap_dport;
1778  nl_addr_get (encap_oa);
1779  sa->encap->encap_oa = encap_oa;
1780 
1781  sa->ce_mask |= XFRM_SA_ATTR_ENCAP;
1782 
1783  return 0;
1784 }
1785 
1786 int xfrmnl_sa_get_tfcpad (struct xfrmnl_sa* sa)
1787 {
1788  if (sa->ce_mask & XFRM_SA_ATTR_TFCPAD)
1789  return sa->tfcpad;
1790  else
1791  return -1;
1792 }
1793 
1794 int xfrmnl_sa_set_tfcpad (struct xfrmnl_sa* sa, unsigned int tfcpad)
1795 {
1796  sa->tfcpad = tfcpad;
1797  sa->ce_mask |= XFRM_SA_ATTR_TFCPAD;
1798 
1799  return 0;
1800 }
1801 
1802 struct nl_addr* xfrmnl_sa_get_coaddr (struct xfrmnl_sa* sa)
1803 {
1804  if (sa->ce_mask & XFRM_SA_ATTR_COADDR)
1805  return sa->coaddr;
1806  else
1807  return NULL;
1808 }
1809 
1810 int xfrmnl_sa_set_coaddr (struct xfrmnl_sa* sa, struct nl_addr* coaddr)
1811 {
1812  /* Free up the old coaddr */
1813  if (sa->coaddr)
1814  nl_addr_put (sa->coaddr);
1815 
1816  /* Save the new info */
1817  nl_addr_get (coaddr);
1818  sa->coaddr = coaddr;
1819 
1820  sa->ce_mask |= XFRM_SA_ATTR_COADDR;
1821 
1822  return 0;
1823 }
1824 
1825 int xfrmnl_sa_get_mark (struct xfrmnl_sa* sa, unsigned int* mark_mask, unsigned int* mark_value)
1826 {
1827  if (mark_mask == NULL || mark_value == NULL)
1828  return -1;
1829 
1830  if (sa->ce_mask & XFRM_SA_ATTR_MARK)
1831  {
1832  *mark_mask = sa->mark.m;
1833  *mark_value = sa->mark.v;
1834 
1835  return 0;
1836  }
1837  else
1838  return -1;
1839 }
1840 
1841 int xfrmnl_sa_set_mark (struct xfrmnl_sa* sa, unsigned int value, unsigned int mask)
1842 {
1843  sa->mark.v = value;
1844  sa->mark.m = mask;
1845  sa->ce_mask |= XFRM_SA_ATTR_MARK;
1846 
1847  return 0;
1848 }
1849 
1850 int xfrmnl_sa_get_sec_ctx (struct xfrmnl_sa* sa, unsigned int* doi, unsigned int* alg, unsigned int* len, unsigned int* sid, char* ctx_str)
1851 {
1852  if (sa->ce_mask & XFRM_SA_ATTR_SECCTX)
1853  {
1854  *doi = sa->sec_ctx->ctx_doi;
1855  *alg = sa->sec_ctx->ctx_alg;
1856  *len = sa->sec_ctx->ctx_len;
1857  *sid = sa->sec_ctx->ctx_sid;
1858  memcpy ((void *)ctx_str, (void *)sa->sec_ctx->ctx_str, sizeof (uint8_t) * sa->sec_ctx->ctx_len);
1859  }
1860  else
1861  return -1;
1862 
1863  return 0;
1864 }
1865 
1866 int xfrmnl_sa_set_sec_ctx (struct xfrmnl_sa* sa, unsigned int doi, unsigned int alg, unsigned int len, unsigned int sid, char* ctx_str)
1867 {
1868  /* Free up the old context string and allocate new one */
1869  if (sa->sec_ctx)
1870  free (sa->sec_ctx);
1871  if ((sa->sec_ctx = calloc (1, sizeof (struct xfrmnl_sec_ctx) + (sizeof (uint8_t) * len))) == NULL)
1872  return -1;
1873 
1874  /* Save the new info */
1875  sa->sec_ctx->ctx_doi = doi;
1876  sa->sec_ctx->ctx_alg = alg;
1877  sa->sec_ctx->ctx_len = len;
1878  sa->sec_ctx->ctx_sid = sid;
1879  memcpy ((void *)sa->sec_ctx->ctx_str, (void *)ctx_str, sizeof (uint8_t) * len);
1880 
1881  sa->ce_mask |= XFRM_SA_ATTR_SECCTX;
1882 
1883  return 0;
1884 }
1885 
1886 
1887 int xfrmnl_sa_get_replay_maxage (struct xfrmnl_sa* sa)
1888 {
1889  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_MAXAGE)
1890  return sa->replay_maxage;
1891  else
1892  return -1;
1893 }
1894 
1895 int xfrmnl_sa_set_replay_maxage (struct xfrmnl_sa* sa, unsigned int replay_maxage)
1896 {
1897  sa->replay_maxage = replay_maxage;
1898  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_MAXAGE;
1899 
1900  return 0;
1901 }
1902 
1903 int xfrmnl_sa_get_replay_maxdiff (struct xfrmnl_sa* sa)
1904 {
1905  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_MAXDIFF)
1906  return sa->replay_maxdiff;
1907  else
1908  return -1;
1909 }
1910 
1911 int xfrmnl_sa_set_replay_maxdiff (struct xfrmnl_sa* sa, unsigned int replay_maxdiff)
1912 {
1913  sa->replay_maxdiff = replay_maxdiff;
1914  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_MAXDIFF;
1915 
1916  return 0;
1917 }
1918 
1919 int xfrmnl_sa_get_replay_state (struct xfrmnl_sa* sa, unsigned int* oseq, unsigned int* seq, unsigned int* bmp)
1920 {
1921  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_STATE)
1922  {
1923  if (sa->replay_state_esn == NULL)
1924  {
1925  *oseq = sa->replay_state.oseq;
1926  *seq = sa->replay_state.seq;
1927  *bmp = sa->replay_state.bitmap;
1928 
1929  return 0;
1930  }
1931  else
1932  {
1933  return -1;
1934  }
1935  }
1936  else
1937  return -1;
1938 }
1939 
1940 int xfrmnl_sa_set_replay_state (struct xfrmnl_sa* sa, unsigned int oseq, unsigned int seq, unsigned int bitmap)
1941 {
1942  sa->replay_state.oseq = oseq;
1943  sa->replay_state.seq = seq;
1944  sa->replay_state.bitmap = bitmap;
1945  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_STATE;
1946 
1947  return 0;
1948 }
1949 
1950 int xfrmnl_sa_get_replay_state_esn (struct xfrmnl_sa* sa, unsigned int* oseq, unsigned int* seq, unsigned int* oseq_hi,
1951  unsigned int* seq_hi, unsigned int* replay_window, unsigned int* bmp_len, unsigned int* bmp)
1952 {
1953  if (sa->ce_mask & XFRM_SA_ATTR_REPLAY_STATE)
1954  {
1955  if (sa->replay_state_esn)
1956  {
1957  *oseq = sa->replay_state_esn->oseq;
1958  *seq = sa->replay_state_esn->seq;
1959  *oseq_hi= sa->replay_state_esn->oseq_hi;
1960  *seq_hi = sa->replay_state_esn->seq_hi;
1961  *replay_window = sa->replay_state_esn->replay_window;
1962  *bmp_len = sa->replay_state_esn->bmp_len; // In number of 32 bit words
1963  memcpy (bmp, sa->replay_state_esn->bmp, sa->replay_state_esn->bmp_len * sizeof (uint32_t));
1964 
1965  return 0;
1966  }
1967  else
1968  {
1969  return -1;
1970  }
1971  }
1972  else
1973  return -1;
1974 }
1975 
1976 int xfrmnl_sa_set_replay_state_esn (struct xfrmnl_sa* sa, unsigned int oseq, unsigned int seq,
1977  unsigned int oseq_hi, unsigned int seq_hi, unsigned int replay_window,
1978  unsigned int bmp_len, unsigned int* bmp)
1979 {
1980  /* Free the old replay state and allocate space to hold new one */
1981  if (sa->replay_state_esn)
1982  free (sa->replay_state_esn);
1983 
1984  if ((sa->replay_state_esn = calloc (1, sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * bmp_len))) == NULL)
1985  return -1;
1986  sa->replay_state_esn->oseq = oseq;
1987  sa->replay_state_esn->seq = seq;
1988  sa->replay_state_esn->oseq_hi = oseq_hi;
1989  sa->replay_state_esn->seq_hi = seq_hi;
1990  sa->replay_state_esn->replay_window = replay_window;
1991  sa->replay_state_esn->bmp_len = bmp_len; // In number of 32 bit words
1992  memcpy (sa->replay_state_esn->bmp, bmp, bmp_len * sizeof (uint32_t));
1993  sa->ce_mask |= XFRM_SA_ATTR_REPLAY_STATE;
1994 
1995  return 0;
1996 }
1997 
1998 
1999 int xfrmnl_sa_is_hardexpiry_reached (struct xfrmnl_sa* sa)
2000 {
2001  if (sa->ce_mask & XFRM_SA_ATTR_EXPIRE)
2002  return (sa->hard > 0 ? 1: 0);
2003  else
2004  return 0;
2005 }
2006 
2007 int xfrmnl_sa_is_expiry_reached (struct xfrmnl_sa* sa)
2008 {
2009  if (sa->ce_mask & XFRM_SA_ATTR_EXPIRE)
2010  return 1;
2011  else
2012  return 0;
2013 }
2014 
2015 /** @} */
2016 
2017 static struct nl_object_ops xfrm_sa_obj_ops = {
2018  .oo_name = "xfrm/sa",
2019  .oo_size = sizeof(struct xfrmnl_sa),
2020  .oo_constructor = xfrm_sa_alloc_data,
2021  .oo_free_data = xfrm_sa_free_data,
2022  .oo_clone = xfrm_sa_clone,
2023  .oo_dump = {
2024  [NL_DUMP_LINE] = xfrm_sa_dump_line,
2025  [NL_DUMP_DETAILS] = xfrm_sa_dump_details,
2026  [NL_DUMP_STATS] = xfrm_sa_dump_stats,
2027  },
2028  .oo_compare = xfrm_sa_compare,
2029  .oo_attrs2str = xfrm_sa_attrs2str,
2030  .oo_id_attrs = (XFRM_SA_ATTR_DADDR | XFRM_SA_ATTR_SPI | XFRM_SA_ATTR_PROTO),
2031 };
2032 
2033 static struct nl_af_group xfrm_sa_groups[] = {
2034  { AF_UNSPEC, XFRMNLGRP_SA },
2035  { AF_UNSPEC, XFRMNLGRP_EXPIRE },
2036  { END_OF_GROUP_LIST },
2037 };
2038 
2039 static struct nl_cache_ops xfrmnl_sa_ops = {
2040  .co_name = "xfrm/sa",
2041  .co_hdrsize = sizeof(struct xfrm_usersa_info),
2042  .co_msgtypes = {
2043  { XFRM_MSG_NEWSA, NL_ACT_NEW, "new" },
2044  { XFRM_MSG_DELSA, NL_ACT_DEL, "del" },
2045  { XFRM_MSG_GETSA, NL_ACT_GET, "get" },
2046  { XFRM_MSG_EXPIRE, NL_ACT_UNSPEC, "expire"},
2047  { XFRM_MSG_UPDSA, NL_ACT_NEW, "update"},
2048  END_OF_MSGTYPES_LIST,
2049  },
2050  .co_protocol = NETLINK_XFRM,
2051  .co_groups = xfrm_sa_groups,
2052  .co_request_update = xfrm_sa_request_update,
2053  .co_msg_parser = xfrm_sa_msg_parser,
2054  .co_obj_ops = &xfrm_sa_obj_ops,
2055  .co_include_event = &xfrm_sa_update_cache
2056 };
2057 
2058 /**
2059  * @name XFRM SA Cache Managament
2060  * @{
2061  */
2062 
2063 static void __attribute__ ((constructor)) xfrm_sa_init(void)
2064 {
2065  nl_cache_mngt_register(&xfrmnl_sa_ops);
2066 }
2067 
2068 static void __attribute__ ((destructor)) xfrm_sa_exit(void)
2069 {
2070  nl_cache_mngt_unregister(&xfrmnl_sa_ops);
2071 }
2072 
2073 /** @} */
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1230
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 nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
Definition: addr.c:917
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
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition: cache_mngt.c:287
Attribute validation policy.
Definition: attr.h:60
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:204
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:1161
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 xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg *ltime)
Clone existing lifetime config object.
Definition: lifetime.c:93
struct nlattr * nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Reserve space for a attribute.
Definition: attr.c:456
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
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition: cache_mngt.c:252
struct nl_object * nl_cache_search(struct nl_cache *cache, struct nl_object *needle)
Search object in cache.
Definition: cache.c:1061
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:147
int nl_object_get_msgtype(const struct nl_object *obj)
Return the netlink message type the object was derived from.
Definition: object.c:504
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
Definition: cache.c:551
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
int xfrmnl_sel_cmp(struct xfrmnl_sel *a, struct xfrmnl_sel *b)
Compares two selector objects.
Definition: selector.c:159
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:191
uint16_t minlen
Minimal length of payload required.
Definition: attr.h:65
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition: nl.c:576
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
Definition: cache.c:145
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:1095
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:215
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
Move object from one cache to another.
Definition: cache.c:523
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
struct xfrmnl_sel * xfrmnl_sel_alloc()
Allocate new selector object.
Definition: selector.c:76
32 bit integer
Definition: attr.h:41
struct xfrmnl_sel * xfrmnl_sel_clone(struct xfrmnl_sel *sel)
Clone existing selector object.
Definition: selector.c:95
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_alloc()
Allocate new lifetime config object.
Definition: lifetime.c:74
Dumping parameters.
Definition: types.h:33
int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg *a, struct xfrmnl_ltime_cfg *b)
Compares two lifetime config objects.
Definition: lifetime.c:154
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:915
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition: nl.c:512
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
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
Definition: cache.c:119
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:893
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
Definition: cache.c:233
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
Definition: object.c:361
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