]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/vici.c
*: Properly use memset() when zeroing
[mirror_frr.git] / nhrpd / vici.c
CommitLineData
2fb975da
TT
1/* strongSwan VICI protocol implementation for NHRP
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
b45ac5f5
DL
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
2fb975da
TT
14#include <string.h>
15#include <sys/socket.h>
16#include <sys/un.h>
17
18#include "thread.h"
19#include "zbuf.h"
20#include "log.h"
aed07011 21#include "lib_errors.h"
2fb975da 22
aed07011 23#include "nhrpd.h"
2fb975da 24#include "vici.h"
aed07011 25#include "nhrp_errors.h"
2fb975da
TT
26
27#define ERRNO_IO_RETRY(EN) (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
28
29struct blob {
30 char *ptr;
31 int len;
32};
33
34static int blob_equal(const struct blob *b, const char *str)
35{
996c9314
LB
36 if (!b || b->len != (int)strlen(str))
37 return 0;
2fb975da
TT
38 return memcmp(b->ptr, str, b->len) == 0;
39}
40
41static int blob2buf(const struct blob *b, char *buf, size_t n)
42{
996c9314
LB
43 if (!b || b->len >= (int)n)
44 return 0;
2fb975da
TT
45 memcpy(buf, b->ptr, b->len);
46 buf[b->len] = 0;
47 return 1;
48}
49
50struct vici_conn {
51 struct thread *t_reconnect, *t_read, *t_write;
52 struct zbuf ibuf;
53 struct zbuf_queue obuf;
54 int fd;
55 uint8_t ibuf_data[VICI_MAX_MSGLEN];
56};
57
58struct vici_message_ctx {
59 const char *sections[8];
60 int nsections;
61};
62
cc9f21da 63static void vici_reconnect(struct thread *t);
2fb975da
TT
64static void vici_submit_request(struct vici_conn *vici, const char *name, ...);
65
66static void vici_zbuf_puts(struct zbuf *obuf, const char *str)
67{
68 size_t len = strlen(str);
69 zbuf_put8(obuf, len);
70 zbuf_put(obuf, str, len);
71}
72
73static void vici_connection_error(struct vici_conn *vici)
74{
75 nhrp_vc_reset();
76
77 THREAD_OFF(vici->t_read);
78 THREAD_OFF(vici->t_write);
79 zbuf_reset(&vici->ibuf);
80 zbufq_reset(&vici->obuf);
81
82 close(vici->fd);
83 vici->fd = -1;
ffa2c898 84 thread_add_timer(master, vici_reconnect, vici, 2, &vici->t_reconnect);
2fb975da
TT
85}
86
996c9314
LB
87static void vici_parse_message(struct vici_conn *vici, struct zbuf *msg,
88 void (*parser)(struct vici_message_ctx *ctx,
89 enum vici_type_t msgtype,
90 const struct blob *key,
91 const struct blob *val),
92 struct vici_message_ctx *ctx)
2fb975da
TT
93{
94 uint8_t *type;
996c9314
LB
95 struct blob key = {0};
96 struct blob val = {0};
2fb975da
TT
97
98 while ((type = zbuf_may_pull(msg, uint8_t)) != NULL) {
99 switch (*type) {
100 case VICI_SECTION_START:
101 key.len = zbuf_get8(msg);
102 key.ptr = zbuf_pulln(msg, key.len);
996c9314
LB
103 debugf(NHRP_DEBUG_VICI, "VICI: Section start '%.*s'",
104 key.len, key.ptr);
2fb975da
TT
105 parser(ctx, *type, &key, NULL);
106 ctx->nsections++;
107 break;
108 case VICI_SECTION_END:
109 debugf(NHRP_DEBUG_VICI, "VICI: Section end");
110 parser(ctx, *type, NULL, NULL);
111 ctx->nsections--;
112 break;
113 case VICI_KEY_VALUE:
114 key.len = zbuf_get8(msg);
115 key.ptr = zbuf_pulln(msg, key.len);
116 val.len = zbuf_get_be16(msg);
117 val.ptr = zbuf_pulln(msg, val.len);
996c9314
LB
118 debugf(NHRP_DEBUG_VICI, "VICI: Key '%.*s'='%.*s'",
119 key.len, key.ptr, val.len, val.ptr);
2fb975da
TT
120 parser(ctx, *type, &key, &val);
121 break;
122 case VICI_LIST_START:
123 key.len = zbuf_get8(msg);
124 key.ptr = zbuf_pulln(msg, key.len);
996c9314
LB
125 debugf(NHRP_DEBUG_VICI, "VICI: List start '%.*s'",
126 key.len, key.ptr);
2fb975da
TT
127 break;
128 case VICI_LIST_ITEM:
129 val.len = zbuf_get_be16(msg);
130 val.ptr = zbuf_pulln(msg, val.len);
996c9314
LB
131 debugf(NHRP_DEBUG_VICI, "VICI: List item: '%.*s'",
132 val.len, val.ptr);
2fb975da
TT
133 parser(ctx, *type, &key, &val);
134 break;
135 case VICI_LIST_END:
136 debugf(NHRP_DEBUG_VICI, "VICI: List end");
137 break;
138 default:
996c9314
LB
139 debugf(NHRP_DEBUG_VICI,
140 "VICI: Unsupported message component type %d",
141 *type);
2fb975da
TT
142 return;
143 }
144 }
145}
146
147struct handle_sa_ctx {
148 struct vici_message_ctx msgctx;
149 int event;
150 int child_ok;
151 int kill_ikesa;
152 uint32_t child_uniqueid, ike_uniqueid;
153 struct {
154 union sockunion host;
155 struct blob id, cert;
156 } local, remote;
157};
158
996c9314
LB
159static void parse_sa_message(struct vici_message_ctx *ctx,
160 enum vici_type_t msgtype, const struct blob *key,
161 const struct blob *val)
2fb975da 162{
996c9314
LB
163 struct handle_sa_ctx *sactx =
164 container_of(ctx, struct handle_sa_ctx, msgctx);
2fb975da
TT
165 struct nhrp_vc *vc;
166 char buf[512];
167
168 switch (msgtype) {
169 case VICI_SECTION_START:
170 if (ctx->nsections == 3) {
171 /* Begin of child-sa section, reset child vars */
172 sactx->child_uniqueid = 0;
173 sactx->child_ok = 0;
174 }
175 break;
176 case VICI_SECTION_END:
177 if (ctx->nsections == 3) {
178 /* End of child-sa section, update nhrp_vc */
179 int up = sactx->child_ok || sactx->event == 1;
180 if (up) {
996c9314
LB
181 vc = nhrp_vc_get(&sactx->local.host,
182 &sactx->remote.host, up);
2fb975da 183 if (vc) {
996c9314
LB
184 blob2buf(&sactx->local.id, vc->local.id,
185 sizeof(vc->local.id));
186 if (blob2buf(&sactx->local.cert,
187 (char *)vc->local.cert,
188 sizeof(vc->local.cert)))
189 vc->local.certlen =
190 sactx->local.cert.len;
191 blob2buf(&sactx->remote.id,
192 vc->remote.id,
193 sizeof(vc->remote.id));
194 if (blob2buf(&sactx->remote.cert,
195 (char *)vc->remote.cert,
196 sizeof(vc->remote.cert)))
197 vc->remote.certlen =
198 sactx->remote.cert.len;
199 sactx->kill_ikesa |=
200 nhrp_vc_ipsec_updown(
201 sactx->child_uniqueid,
202 vc);
4cbaf956 203 vc->ike_uniqueid = sactx->ike_uniqueid;
2fb975da
TT
204 }
205 } else {
206 nhrp_vc_ipsec_updown(sactx->child_uniqueid, 0);
207 }
208 }
209 break;
210 default:
fa3bf3a2 211 if (!key || !key->ptr)
6b07f6e1
JB
212 break;
213
2fb975da
TT
214 switch (key->ptr[0]) {
215 case 'l':
996c9314
LB
216 if (blob_equal(key, "local-host")
217 && ctx->nsections == 1) {
2fb975da 218 if (blob2buf(val, buf, sizeof(buf)))
996c9314
LB
219 if (str2sockunion(buf,
220 &sactx->local.host)
221 < 0)
1c50c1c0
QY
222 flog_err(
223 EC_NHRP_SWAN,
224 "VICI: bad strongSwan local-host: %s",
225 buf);
996c9314
LB
226 } else if (blob_equal(key, "local-id")
227 && ctx->nsections == 1) {
2fb975da 228 sactx->local.id = *val;
996c9314
LB
229 } else if (blob_equal(key, "local-cert-data")
230 && ctx->nsections == 1) {
2fb975da
TT
231 sactx->local.cert = *val;
232 }
233 break;
234 case 'r':
996c9314
LB
235 if (blob_equal(key, "remote-host")
236 && ctx->nsections == 1) {
2fb975da 237 if (blob2buf(val, buf, sizeof(buf)))
996c9314
LB
238 if (str2sockunion(buf,
239 &sactx->remote.host)
240 < 0)
1c50c1c0
QY
241 flog_err(
242 EC_NHRP_SWAN,
243 "VICI: bad strongSwan remote-host: %s",
244 buf);
996c9314
LB
245 } else if (blob_equal(key, "remote-id")
246 && ctx->nsections == 1) {
2fb975da 247 sactx->remote.id = *val;
996c9314
LB
248 } else if (blob_equal(key, "remote-cert-data")
249 && ctx->nsections == 1) {
2fb975da
TT
250 sactx->remote.cert = *val;
251 }
252 break;
253 case 'u':
996c9314
LB
254 if (blob_equal(key, "uniqueid")
255 && blob2buf(val, buf, sizeof(buf))) {
2fb975da 256 if (ctx->nsections == 3)
996c9314
LB
257 sactx->child_uniqueid =
258 strtoul(buf, NULL, 0);
2fb975da 259 else if (ctx->nsections == 1)
996c9314
LB
260 sactx->ike_uniqueid =
261 strtoul(buf, NULL, 0);
2fb975da
TT
262 }
263 break;
264 case 's':
265 if (blob_equal(key, "state") && ctx->nsections == 3) {
266 sactx->child_ok =
996c9314
LB
267 (sactx->event == 0
268 && (blob_equal(val, "INSTALLED")
269 || blob_equal(val, "REKEYED")));
2fb975da
TT
270 }
271 break;
272 }
273 break;
274 }
275}
276
996c9314
LB
277static void parse_cmd_response(struct vici_message_ctx *ctx,
278 enum vici_type_t msgtype, const struct blob *key,
279 const struct blob *val)
d139786a
TT
280{
281 char buf[512];
282
283 switch (msgtype) {
284 case VICI_KEY_VALUE:
996c9314
LB
285 if (blob_equal(key, "errmsg")
286 && blob2buf(val, buf, sizeof(buf)))
2b84a521 287 flog_err(EC_NHRP_SWAN, "VICI: strongSwan: %s", buf);
d139786a
TT
288 break;
289 default:
290 break;
291 }
292}
293
2fb975da
TT
294static void vici_recv_sa(struct vici_conn *vici, struct zbuf *msg, int event)
295{
296 char buf[32];
297 struct handle_sa_ctx ctx = {
298 .event = event,
0a939f4f 299 .msgctx.nsections = 0
2fb975da
TT
300 };
301
302 vici_parse_message(vici, msg, parse_sa_message, &ctx.msgctx);
303
304 if (ctx.kill_ikesa && ctx.ike_uniqueid) {
996c9314
LB
305 debugf(NHRP_DEBUG_COMMON, "VICI: Deleting IKE_SA %u",
306 ctx.ike_uniqueid);
0d6f7fd6 307 snprintf(buf, sizeof(buf), "%u", ctx.ike_uniqueid);
996c9314
LB
308 vici_submit_request(vici, "terminate", VICI_KEY_VALUE, "ike-id",
309 strlen(buf), buf, VICI_END);
2fb975da
TT
310 }
311}
312
313static void vici_recv_message(struct vici_conn *vici, struct zbuf *msg)
314{
315 uint32_t msglen;
316 uint8_t msgtype;
317 struct blob name;
7ea5df54 318 struct vici_message_ctx ctx = { .nsections = 0 };
2fb975da
TT
319
320 msglen = zbuf_get_be32(msg);
321 msgtype = zbuf_get8(msg);
322 debugf(NHRP_DEBUG_VICI, "VICI: Message %d, %d bytes", msgtype, msglen);
323
324 switch (msgtype) {
325 case VICI_EVENT:
326 name.len = zbuf_get8(msg);
327 name.ptr = zbuf_pulln(msg, name.len);
328
996c9314
LB
329 debugf(NHRP_DEBUG_VICI, "VICI: Event '%.*s'", name.len,
330 name.ptr);
331 if (blob_equal(&name, "list-sa")
332 || blob_equal(&name, "child-updown")
333 || blob_equal(&name, "child-rekey"))
2fb975da 334 vici_recv_sa(vici, msg, 0);
996c9314
LB
335 else if (blob_equal(&name, "child-state-installed")
336 || blob_equal(&name, "child-state-rekeyed"))
2fb975da
TT
337 vici_recv_sa(vici, msg, 1);
338 else if (blob_equal(&name, "child-state-destroying"))
339 vici_recv_sa(vici, msg, 2);
340 break;
d139786a 341 case VICI_CMD_RESPONSE:
6c8ca260 342 vici_parse_message(vici, msg, parse_cmd_response, &ctx);
d139786a 343 break;
2fb975da 344 case VICI_EVENT_UNKNOWN:
d139786a 345 case VICI_CMD_UNKNOWN:
1c50c1c0
QY
346 flog_err(
347 EC_NHRP_SWAN,
996c9314 348 "VICI: StrongSwan does not support mandatory events (unpatched?)");
2fb975da
TT
349 break;
350 case VICI_EVENT_CONFIRM:
2fb975da
TT
351 break;
352 default:
353 zlog_notice("VICI: Unrecognized message type %d", msgtype);
354 break;
355 }
356}
357
cc9f21da 358static void vici_read(struct thread *t)
2fb975da
TT
359{
360 struct vici_conn *vici = THREAD_ARG(t);
361 struct zbuf *ibuf = &vici->ibuf;
362 struct zbuf pktbuf;
363
996c9314 364 if (zbuf_read(ibuf, vici->fd, (size_t)-1) < 0) {
2fb975da 365 vici_connection_error(vici);
cc9f21da 366 return;
2fb975da
TT
367 }
368
369 /* Process all messages in buffer */
370 do {
371 uint32_t *hdrlen = zbuf_may_pull(ibuf, uint32_t);
372 if (!hdrlen)
373 break;
374 if (!zbuf_may_pulln(ibuf, ntohl(*hdrlen))) {
375 zbuf_reset_head(ibuf, hdrlen);
376 break;
377 }
378
379 /* Handle packet */
996c9314
LB
380 zbuf_init(&pktbuf, hdrlen, htonl(*hdrlen) + 4,
381 htonl(*hdrlen) + 4);
2fb975da
TT
382 vici_recv_message(vici, &pktbuf);
383 } while (1);
384
ffa2c898 385 thread_add_read(master, vici_read, vici, vici->fd, &vici->t_read);
2fb975da
TT
386}
387
cc9f21da 388static void vici_write(struct thread *t)
2fb975da
TT
389{
390 struct vici_conn *vici = THREAD_ARG(t);
391 int r;
392
2fb975da
TT
393 r = zbufq_write(&vici->obuf, vici->fd);
394 if (r > 0) {
ffa2c898
QY
395 thread_add_write(master, vici_write, vici, vici->fd,
396 &vici->t_write);
2fb975da
TT
397 } else if (r < 0) {
398 vici_connection_error(vici);
399 }
2fb975da
TT
400}
401
402static void vici_submit(struct vici_conn *vici, struct zbuf *obuf)
403{
404 if (vici->fd < 0) {
405 zbuf_free(obuf);
406 return;
407 }
408
409 zbufq_queue(&vici->obuf, obuf);
ffa2c898 410 thread_add_write(master, vici_write, vici, vici->fd, &vici->t_write);
2fb975da
TT
411}
412
413static void vici_submit_request(struct vici_conn *vici, const char *name, ...)
414{
415 struct zbuf *obuf;
416 uint32_t *hdrlen;
417 va_list va;
418 size_t len;
419 int type;
420
421 obuf = zbuf_alloc(256);
996c9314
LB
422 if (!obuf)
423 return;
2fb975da
TT
424
425 hdrlen = zbuf_push(obuf, uint32_t);
426 zbuf_put8(obuf, VICI_CMD_REQUEST);
427 vici_zbuf_puts(obuf, name);
428
429 va_start(va, name);
430 for (type = va_arg(va, int); type != VICI_END; type = va_arg(va, int)) {
431 zbuf_put8(obuf, type);
432 switch (type) {
433 case VICI_KEY_VALUE:
434 vici_zbuf_puts(obuf, va_arg(va, const char *));
435 len = va_arg(va, size_t);
436 zbuf_put_be16(obuf, len);
437 zbuf_put(obuf, va_arg(va, void *), len);
438 break;
2fb975da
TT
439 default:
440 break;
441 }
442 }
443 va_end(va);
444 *hdrlen = htonl(zbuf_used(obuf) - 4);
445 vici_submit(vici, obuf);
446}
447
448static void vici_register_event(struct vici_conn *vici, const char *name)
449{
450 struct zbuf *obuf;
451 uint32_t *hdrlen;
452 uint8_t namelen;
453
454 namelen = strlen(name);
455 obuf = zbuf_alloc(4 + 1 + 1 + namelen);
996c9314
LB
456 if (!obuf)
457 return;
2fb975da
TT
458
459 hdrlen = zbuf_push(obuf, uint32_t);
460 zbuf_put8(obuf, VICI_EVENT_REGISTER);
461 zbuf_put8(obuf, namelen);
462 zbuf_put(obuf, name, namelen);
463 *hdrlen = htonl(zbuf_used(obuf) - 4);
464
465 vici_submit(vici, obuf);
466}
467
40307370
PG
468static bool vici_charon_filepath_done;
469static bool vici_charon_not_found;
470
471static char *vici_get_charon_filepath(void)
472{
473 static char buff[1200];
474 FILE *fp;
475 char *ptr;
476 char line[1024];
477
478 if (vici_charon_filepath_done)
479 return (char *)buff;
480 fp = popen("ipsec --piddir", "r");
481 if (!fp) {
482 if (!vici_charon_not_found) {
483 flog_err(EC_NHRP_SWAN,
484 "VICI: Failed to retrieve charon file path");
485 vici_charon_not_found = true;
486 }
487 return NULL;
488 }
489 /* last line of output is used to get vici path */
490 while (fgets(line, sizeof(line), fp) != NULL) {
491 ptr = strchr(line, '\n');
492 if (ptr)
493 *ptr = '\0';
494 snprintf(buff, sizeof(buff), "%s/charon.vici", line);
495 }
496 pclose(fp);
497 vici_charon_filepath_done = true;
498 return buff;
499}
500
cc9f21da 501static void vici_reconnect(struct thread *t)
2fb975da
TT
502{
503 struct vici_conn *vici = THREAD_ARG(t);
504 int fd;
40307370 505 char *file_path;
2fb975da 506
996c9314 507 if (vici->fd >= 0)
cc9f21da 508 return;
2fb975da 509
354196c0 510 fd = sock_open_unix(VICI_SOCKET);
40307370
PG
511 if (fd < 0) {
512 file_path = vici_get_charon_filepath();
513 if (file_path)
514 fd = sock_open_unix(file_path);
515 }
2fb975da 516 if (fd < 0) {
996c9314 517 debugf(NHRP_DEBUG_VICI,
15569c58
DA
518 "%s: failure connecting VICI socket: %s", __func__,
519 strerror(errno));
ffa2c898
QY
520 thread_add_timer(master, vici_reconnect, vici, 2,
521 &vici->t_reconnect);
cc9f21da 522 return;
2fb975da
TT
523 }
524
525 debugf(NHRP_DEBUG_COMMON, "VICI: Connected");
526 vici->fd = fd;
ffa2c898 527 thread_add_read(master, vici_read, vici, vici->fd, &vici->t_read);
2fb975da
TT
528
529 /* Send event subscribtions */
996c9314
LB
530 // vici_register_event(vici, "child-updown");
531 // vici_register_event(vici, "child-rekey");
2fb975da
TT
532 vici_register_event(vici, "child-state-installed");
533 vici_register_event(vici, "child-state-rekeyed");
534 vici_register_event(vici, "child-state-destroying");
535 vici_register_event(vici, "list-sa");
536 vici_submit_request(vici, "list-sas", VICI_END);
2fb975da
TT
537}
538
539static struct vici_conn vici_connection;
540
541void vici_init(void)
542{
543 struct vici_conn *vici = &vici_connection;
544
545 vici->fd = -1;
546 zbuf_init(&vici->ibuf, vici->ibuf_data, sizeof(vici->ibuf_data), 0);
547 zbufq_init(&vici->obuf);
ffa2c898
QY
548 thread_add_timer_msec(master, vici_reconnect, vici, 10,
549 &vici->t_reconnect);
2fb975da
TT
550}
551
552void vici_terminate(void)
553{
554}
555
083bbfae
GG
556void vici_terminate_vc_by_profile_name(char *profile_name)
557{
558 struct vici_conn *vici = &vici_connection;
74e5ba3a 559
58ef1668 560 debugf(NHRP_DEBUG_VICI, "Terminate profile = %s", profile_name);
083bbfae
GG
561 vici_submit_request(vici, "terminate", VICI_KEY_VALUE, "ike",
562 strlen(profile_name), profile_name, VICI_END);
563}
564
565void vici_terminate_vc_by_ike_id(unsigned int ike_id)
4cbaf956
GG
566{
567 struct vici_conn *vici = &vici_connection;
74e5ba3a
RD
568 char ike_id_str[10];
569
4cbaf956 570 snprintf(ike_id_str, sizeof(ike_id_str), "%d", ike_id);
58ef1668 571 debugf(NHRP_DEBUG_VICI, "Terminate ike_id_str = %s", ike_id_str);
4cbaf956
GG
572 vici_submit_request(vici, "terminate", VICI_KEY_VALUE, "ike-id",
573 strlen(ike_id_str), ike_id_str, VICI_END);
574}
575
996c9314
LB
576void vici_request_vc(const char *profile, union sockunion *src,
577 union sockunion *dst, int prio)
2fb975da
TT
578{
579 struct vici_conn *vici = &vici_connection;
580 char buf[2][SU_ADDRSTRLEN];
581
0d6f7fd6
DA
582 sockunion2str(src, buf[0], sizeof(buf[0]));
583 sockunion2str(dst, buf[1], sizeof(buf[1]));
2fb975da 584
996c9314
LB
585 vici_submit_request(vici, "initiate", VICI_KEY_VALUE, "child",
586 strlen(profile), profile, VICI_KEY_VALUE, "timeout",
587 (size_t)2, "-1", VICI_KEY_VALUE, "async", (size_t)1,
588 "1", VICI_KEY_VALUE, "init-limits", (size_t)1,
589 prio ? "0" : "1", VICI_KEY_VALUE, "my-host",
590 strlen(buf[0]), buf[0], VICI_KEY_VALUE,
591 "other-host", strlen(buf[1]), buf[1], VICI_END);
2fb975da
TT
592}
593
594int sock_open_unix(const char *path)
595{
596 int ret, fd;
597 struct sockaddr_un addr;
598
599 fd = socket(AF_UNIX, SOCK_STREAM, 0);
600 if (fd < 0)
601 return -1;
602
6006b807 603 memset(&addr, 0, sizeof(addr));
2fb975da 604 addr.sun_family = AF_UNIX;
fa3bf3a2 605 strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
2fb975da 606
996c9314
LB
607 ret = connect(fd, (struct sockaddr *)&addr,
608 sizeof(addr.sun_family) + strlen(addr.sun_path));
2fb975da
TT
609 if (ret < 0) {
610 close(fd);
611 return -1;
612 }
613
6c8ca260
JB
614 ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
615 if (ret < 0) {
616 close(fd);
617 return -1;
618 }
2fb975da
TT
619
620 return fd;
621}