| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <inttypes.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <string.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <net/if.h> |
| 17 |
#include <netinet/in.h> |
| 18 |
|
| 19 |
#include <linux/ovpn_dco.h> |
| 20 |
#include <linux/types.h> |
| 21 |
#include <linux/netlink.h> |
| 22 |
|
| 23 |
#include <netlink/socket.h> |
| 24 |
#include <netlink/netlink.h> |
| 25 |
#include <netlink/genl/genl.h> |
| 26 |
#include <netlink/genl/family.h> |
| 27 |
#include <netlink/genl/ctrl.h> |
| 28 |
|
| 29 |
#include <mbedtls/base64.h> |
| 30 |
#include <mbedtls/error.h> |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#define nla_nest_start(_msg, _type) \ |
| 37 |
nla_nest_start(_msg, (_type) | NLA_F_NESTED) |
| 38 |
|
| 39 |
typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg); |
| 40 |
|
| 41 |
enum ovpn_key_direction { |
| 42 |
KEY_DIR_IN = 0, |
| 43 |
KEY_DIR_OUT, |
| 44 |
}; |
| 45 |
|
| 46 |
#define KEY_LEN (256 / 8) |
| 47 |
#define NONCE_LEN 8 |
| 48 |
|
| 49 |
#define PEER_ID_UNDEF 0x00FFFFFF |
| 50 |
|
| 51 |
struct nl_ctx { |
| 52 |
struct nl_sock *nl_sock; |
| 53 |
struct nl_msg *nl_msg; |
| 54 |
struct nl_cb *nl_cb; |
| 55 |
|
| 56 |
int ovpn_dco_id; |
| 57 |
}; |
| 58 |
|
| 59 |
struct ovpn_ctx { |
| 60 |
__u8 key_enc[KEY_LEN]; |
| 61 |
__u8 key_dec[KEY_LEN]; |
| 62 |
__u8 nonce[NONCE_LEN]; |
| 63 |
|
| 64 |
enum ovpn_cipher_alg cipher; |
| 65 |
|
| 66 |
sa_family_t sa_family; |
| 67 |
|
| 68 |
__u32 peer_id; |
| 69 |
__u16 lport; |
| 70 |
|
| 71 |
union { |
| 72 |
struct sockaddr_in in4; |
| 73 |
struct sockaddr_in6 in6; |
| 74 |
} remote; |
| 75 |
|
| 76 |
union { |
| 77 |
struct sockaddr_in in4; |
| 78 |
struct sockaddr_in6 in6; |
| 79 |
} peer_ip; |
| 80 |
|
| 81 |
unsigned int ifindex; |
| 82 |
|
| 83 |
int socket; |
| 84 |
|
| 85 |
__u32 keepalive_interval; |
| 86 |
__u32 keepalive_timeout; |
| 87 |
|
| 88 |
enum ovpn_key_direction key_dir; |
| 89 |
}; |
| 90 |
|
| 91 |
static int ovpn_nl_recvmsgs(struct nl_ctx *ctx) |
| 92 |
{ |
| 93 |
int ret; |
| 94 |
|
| 95 |
ret = nl_recvmsgs(ctx->nl_sock, ctx->nl_cb); |
| 96 |
|
| 97 |
switch (ret) { |
| 98 |
case -NLE_INTR: |
| 99 |
fprintf(stderr, |
| 100 |
"netlink received interrupt due to signal - ignoring\n"); |
| 101 |
break; |
| 102 |
case -NLE_NOMEM: |
| 103 |
fprintf(stderr, "netlink out of memory error\n"); |
| 104 |
break; |
| 105 |
case -NLE_AGAIN: |
| 106 |
fprintf(stderr, |
| 107 |
"netlink reports blocking read - aborting wait\n"); |
| 108 |
break; |
| 109 |
default: |
| 110 |
if (ret) |
| 111 |
fprintf(stderr, "netlink reports error (%d): %s\n", |
| 112 |
ret, nl_geterror(-ret)); |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
return ret; |
| 117 |
} |
| 118 |
|
| 119 |
static struct nl_ctx *nl_ctx_alloc_flags(struct ovpn_ctx *ovpn, |
| 120 |
enum ovpn_nl_commands cmd, int flags) |
| 121 |
{ |
| 122 |
struct nl_ctx *ctx; |
| 123 |
int ret; |
| 124 |
|
| 125 |
ctx = calloc(1, sizeof(*ctx)); |
| 126 |
if (!ctx) |
| 127 |
return NULL; |
| 128 |
|
| 129 |
ctx->nl_sock = nl_socket_alloc(); |
| 130 |
if (!ctx->nl_sock) { |
| 131 |
fprintf(stderr, "cannot allocate netlink socket\n"); |
| 132 |
goto err_free; |
| 133 |
} |
| 134 |
|
| 135 |
nl_socket_set_buffer_size(ctx->nl_sock, 8192, 8192); |
| 136 |
|
| 137 |
ret = genl_connect(ctx->nl_sock); |
| 138 |
if (ret) { |
| 139 |
fprintf(stderr, "cannot connect to generic netlink: %s\n", |
| 140 |
nl_geterror(ret)); |
| 141 |
goto err_sock; |
| 142 |
} |
| 143 |
|
| 144 |
ctx->ovpn_dco_id = genl_ctrl_resolve(ctx->nl_sock, OVPN_NL_NAME); |
| 145 |
if (ctx->ovpn_dco_id < 0) { |
| 146 |
fprintf(stderr, "cannot find ovpn_dco netlink component: %d\n", |
| 147 |
ctx->ovpn_dco_id); |
| 148 |
goto err_free; |
| 149 |
} |
| 150 |
|
| 151 |
ctx->nl_msg = nlmsg_alloc(); |
| 152 |
if (!ctx->nl_msg) { |
| 153 |
fprintf(stderr, "cannot allocate netlink message\n"); |
| 154 |
goto err_sock; |
| 155 |
} |
| 156 |
|
| 157 |
ctx->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); |
| 158 |
if (!ctx->nl_cb) { |
| 159 |
fprintf(stderr, "failed to allocate netlink callback\n"); |
| 160 |
goto err_msg; |
| 161 |
} |
| 162 |
|
| 163 |
nl_socket_set_cb(ctx->nl_sock, ctx->nl_cb); |
| 164 |
|
| 165 |
genlmsg_put(ctx->nl_msg, 0, 0, ctx->ovpn_dco_id, 0, flags, cmd, 0); |
| 166 |
NLA_PUT_U32(ctx->nl_msg, OVPN_ATTR_IFINDEX, ovpn->ifindex); |
| 167 |
|
| 168 |
return ctx; |
| 169 |
nla_put_failure: |
| 170 |
err_msg: |
| 171 |
nlmsg_free(ctx->nl_msg); |
| 172 |
err_sock: |
| 173 |
nl_socket_free(ctx->nl_sock); |
| 174 |
err_free: |
| 175 |
free(ctx); |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
static struct nl_ctx *nl_ctx_alloc(struct ovpn_ctx *ovpn, |
| 180 |
enum ovpn_nl_commands cmd) |
| 181 |
{ |
| 182 |
return nl_ctx_alloc_flags(ovpn, cmd, 0); |
| 183 |
} |
| 184 |
|
| 185 |
static void nl_ctx_free(struct nl_ctx *ctx) |
| 186 |
{ |
| 187 |
if (!ctx) |
| 188 |
return; |
| 189 |
|
| 190 |
nl_socket_free(ctx->nl_sock); |
| 191 |
nlmsg_free(ctx->nl_msg); |
| 192 |
nl_cb_put(ctx->nl_cb); |
| 193 |
free(ctx); |
| 194 |
} |
| 195 |
|
| 196 |
static int ovpn_nl_cb_error(struct sockaddr_nl (*nla)__attribute__((unused)), |
| 197 |
struct nlmsgerr *err, void *arg) |
| 198 |
{ |
| 199 |
struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1; |
| 200 |
struct nlattr *tb_msg[NLMSGERR_ATTR_MAX + 1]; |
| 201 |
int len = nlh->nlmsg_len; |
| 202 |
struct nlattr *attrs; |
| 203 |
int *ret = arg; |
| 204 |
int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh); |
| 205 |
|
| 206 |
*ret = err->error; |
| 207 |
|
| 208 |
if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS)) |
| 209 |
return NL_STOP; |
| 210 |
|
| 211 |
if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) |
| 212 |
ack_len += err->msg.nlmsg_len - sizeof(*nlh); |
| 213 |
|
| 214 |
if (len <= ack_len) |
| 215 |
return NL_STOP; |
| 216 |
|
| 217 |
attrs = (void *)((unsigned char *)nlh + ack_len); |
| 218 |
len -= ack_len; |
| 219 |
|
| 220 |
nla_parse(tb_msg, NLMSGERR_ATTR_MAX, attrs, len, NULL); |
| 221 |
if (tb_msg[NLMSGERR_ATTR_MSG]) { |
| 222 |
len = strnlen((char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]), |
| 223 |
nla_len(tb_msg[NLMSGERR_ATTR_MSG])); |
| 224 |
fprintf(stderr, "kernel error: %*s\n", len, |
| 225 |
(char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG])); |
| 226 |
} |
| 227 |
|
| 228 |
return NL_STOP; |
| 229 |
} |
| 230 |
|
| 231 |
static int ovpn_nl_cb_finish(struct nl_msg (*msg)__attribute__((unused)), |
| 232 |
void *arg) |
| 233 |
{ |
| 234 |
int *status = arg; |
| 235 |
|
| 236 |
*status = 0; |
| 237 |
return NL_SKIP; |
| 238 |
} |
| 239 |
|
| 240 |
static int ovpn_nl_msg_send(struct nl_ctx *ctx, ovpn_nl_cb cb) |
| 241 |
{ |
| 242 |
int status = 1; |
| 243 |
|
| 244 |
nl_cb_err(ctx->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &status); |
| 245 |
nl_cb_set(ctx->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish, |
| 246 |
&status); |
| 247 |
nl_cb_set(ctx->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_nl_cb_finish, |
| 248 |
&status); |
| 249 |
|
| 250 |
if (cb) |
| 251 |
nl_cb_set(ctx->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, cb, ctx); |
| 252 |
|
| 253 |
nl_send_auto_complete(ctx->nl_sock, ctx->nl_msg); |
| 254 |
|
| 255 |
while (status == 1) |
| 256 |
ovpn_nl_recvmsgs(ctx); |
| 257 |
|
| 258 |
if (status < 0) |
| 259 |
fprintf(stderr, "failed to send netlink message: %s (%d)\n", |
| 260 |
strerror(-status), status); |
| 261 |
|
| 262 |
return status; |
| 263 |
} |
| 264 |
|
| 265 |
static int ovpn_read_key(const char *file, struct ovpn_ctx *ctx) |
| 266 |
{ |
| 267 |
int idx_enc, idx_dec, ret = -1; |
| 268 |
unsigned char *ckey = NULL; |
| 269 |
__u8 *bkey = NULL; |
| 270 |
size_t olen = 0; |
| 271 |
long ckey_len; |
| 272 |
FILE *fp; |
| 273 |
|
| 274 |
fp = fopen(file, "r"); |
| 275 |
if (!fp) { |
| 276 |
fprintf(stderr, "cannot open: %s\n", file); |
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
fseek(fp, 0L, SEEK_END); |
| 282 |
ckey_len = ftell(fp); |
| 283 |
rewind(fp); |
| 284 |
|
| 285 |
|
| 286 |
if (ckey_len > 256) |
| 287 |
ckey_len = 256; |
| 288 |
|
| 289 |
ckey = malloc(ckey_len); |
| 290 |
if (!ckey) |
| 291 |
goto err; |
| 292 |
|
| 293 |
ret = fread(ckey, 1, ckey_len, fp); |
| 294 |
if (ret != ckey_len) { |
| 295 |
fprintf(stderr, |
| 296 |
"couldn't read enough data from key file: %dbytes read\n", |
| 297 |
ret); |
| 298 |
goto err; |
| 299 |
} |
| 300 |
|
| 301 |
olen = 0; |
| 302 |
ret = mbedtls_base64_decode(NULL, 0, &olen, ckey, ckey_len); |
| 303 |
if (ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) { |
| 304 |
char buf[256]; |
| 305 |
|
| 306 |
mbedtls_strerror(ret, buf, sizeof(buf)); |
| 307 |
fprintf(stderr, "unexpected base64 error1: %s (%d)\n", buf, |
| 308 |
ret); |
| 309 |
|
| 310 |
goto err; |
| 311 |
} |
| 312 |
|
| 313 |
bkey = malloc(olen); |
| 314 |
if (!bkey) { |
| 315 |
fprintf(stderr, "cannot allocate binary key buffer\n"); |
| 316 |
goto err; |
| 317 |
} |
| 318 |
|
| 319 |
ret = mbedtls_base64_decode(bkey, olen, &olen, ckey, ckey_len); |
| 320 |
if (ret) { |
| 321 |
char buf[256]; |
| 322 |
|
| 323 |
mbedtls_strerror(ret, buf, sizeof(buf)); |
| 324 |
fprintf(stderr, "unexpected base64 error2: %s (%d)\n", buf, |
| 325 |
ret); |
| 326 |
|
| 327 |
goto err; |
| 328 |
} |
| 329 |
|
| 330 |
if (olen < 2 * KEY_LEN + NONCE_LEN) { |
| 331 |
fprintf(stderr, |
| 332 |
"not enough data in key file, found %zdB but needs %dB\n", |
| 333 |
olen, 2 * KEY_LEN + NONCE_LEN); |
| 334 |
goto err; |
| 335 |
} |
| 336 |
|
| 337 |
switch (ctx->key_dir) { |
| 338 |
case KEY_DIR_IN: |
| 339 |
idx_enc = 0; |
| 340 |
idx_dec = 1; |
| 341 |
break; |
| 342 |
case KEY_DIR_OUT: |
| 343 |
idx_enc = 1; |
| 344 |
idx_dec = 0; |
| 345 |
break; |
| 346 |
} |
| 347 |
|
| 348 |
memcpy(ctx->key_enc, bkey + KEY_LEN * idx_enc, KEY_LEN); |
| 349 |
memcpy(ctx->key_dec, bkey + KEY_LEN * idx_dec, KEY_LEN); |
| 350 |
memcpy(ctx->nonce, bkey + 2 * KEY_LEN, NONCE_LEN); |
| 351 |
|
| 352 |
ret = 0; |
| 353 |
|
| 354 |
err: |
| 355 |
fclose(fp); |
| 356 |
free(bkey); |
| 357 |
free(ckey); |
| 358 |
|
| 359 |
return ret; |
| 360 |
} |
| 361 |
|
| 362 |
static int ovpn_read_cipher(const char *cipher, struct ovpn_ctx *ctx) |
| 363 |
{ |
| 364 |
if (strcmp(cipher, "aes") == 0) |
| 365 |
ctx->cipher = OVPN_CIPHER_ALG_AES_GCM; |
| 366 |
else if (strcmp(cipher, "chachapoly") == 0) |
| 367 |
ctx->cipher = OVPN_CIPHER_ALG_CHACHA20_POLY1305; |
| 368 |
else if (strcmp(cipher, "none") == 0) |
| 369 |
ctx->cipher = OVPN_CIPHER_ALG_NONE; |
| 370 |
else |
| 371 |
return -ENOTSUP; |
| 372 |
|
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int ovpn_read_key_direction(const char *dir, struct ovpn_ctx *ctx) |
| 377 |
{ |
| 378 |
int in_dir; |
| 379 |
|
| 380 |
in_dir = strtoll(dir, NULL, 10); |
| 381 |
switch (in_dir) { |
| 382 |
case KEY_DIR_IN: |
| 383 |
case KEY_DIR_OUT: |
| 384 |
ctx->key_dir = in_dir; |
| 385 |
break; |
| 386 |
default: |
| 387 |
fprintf(stderr, |
| 388 |
"invalid key direction provided. Can be 0 or 1 only\n"); |
| 389 |
return -1; |
| 390 |
} |
| 391 |
|
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto) |
| 396 |
{ |
| 397 |
struct sockaddr_storage local_sock; |
| 398 |
struct sockaddr_in6 *in6; |
| 399 |
struct sockaddr_in *in; |
| 400 |
int ret, s, sock_type; |
| 401 |
size_t sock_len; |
| 402 |
|
| 403 |
if (proto == IPPROTO_UDP) |
| 404 |
sock_type = SOCK_DGRAM; |
| 405 |
else if (proto == IPPROTO_TCP) |
| 406 |
sock_type = SOCK_STREAM; |
| 407 |
else |
| 408 |
return -EINVAL; |
| 409 |
|
| 410 |
s = socket(family, sock_type, 0); |
| 411 |
if (s < 0) { |
| 412 |
perror("cannot create socket"); |
| 413 |
return -1; |
| 414 |
} |
| 415 |
|
| 416 |
memset((char *)&local_sock, 0, sizeof(local_sock)); |
| 417 |
|
| 418 |
switch (family) { |
| 419 |
case AF_INET: |
| 420 |
in = (struct sockaddr_in *)&local_sock; |
| 421 |
in->sin_family = family; |
| 422 |
in->sin_port = htons(ctx->lport); |
| 423 |
in->sin_addr.s_addr = htonl(INADDR_ANY); |
| 424 |
sock_len = sizeof(*in); |
| 425 |
break; |
| 426 |
case AF_INET6: |
| 427 |
in6 = (struct sockaddr_in6 *)&local_sock; |
| 428 |
in6->sin6_family = family; |
| 429 |
in6->sin6_port = htons(ctx->lport); |
| 430 |
in6->sin6_addr = in6addr_any; |
| 431 |
sock_len = sizeof(*in6); |
| 432 |
break; |
| 433 |
default: |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
int opt = 1; |
| 438 |
ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 439 |
if (ret < 0) { |
| 440 |
perror("setsockopt for SO_REUSEADDR"); |
| 441 |
return ret; |
| 442 |
} |
| 443 |
|
| 444 |
ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); |
| 445 |
if (ret < 0) { |
| 446 |
perror("setsockopt for SO_REUSEPORT"); |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
if (family == AF_INET6) { |
| 451 |
opt = 0; |
| 452 |
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt))) { |
| 453 |
perror("failed to set IPV6_V6ONLY"); |
| 454 |
return -1; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
ret = bind(s, (struct sockaddr *)&local_sock, sock_len); |
| 459 |
if (ret < 0) { |
| 460 |
perror("cannot bind socket"); |
| 461 |
goto err_socket; |
| 462 |
} |
| 463 |
|
| 464 |
ctx->socket = s; |
| 465 |
ctx->sa_family = family; |
| 466 |
return 0; |
| 467 |
|
| 468 |
err_socket: |
| 469 |
close(s); |
| 470 |
return -1; |
| 471 |
} |
| 472 |
|
| 473 |
static int ovpn_udp_socket(struct ovpn_ctx *ctx, sa_family_t family) |
| 474 |
{ |
| 475 |
return ovpn_socket(ctx, family, IPPROTO_UDP); |
| 476 |
} |
| 477 |
|
| 478 |
static int ovpn_listen(struct ovpn_ctx *ctx, sa_family_t family) |
| 479 |
{ |
| 480 |
int ret; |
| 481 |
|
| 482 |
ret = ovpn_socket(ctx, family, IPPROTO_TCP); |
| 483 |
if (ret < 0) |
| 484 |
return ret; |
| 485 |
|
| 486 |
ret = listen(ctx->socket, 10); |
| 487 |
if (ret < 0) { |
| 488 |
perror("listen"); |
| 489 |
close(ctx->socket); |
| 490 |
return -1; |
| 491 |
} |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static int ovpn_accept(struct ovpn_ctx *ctx) |
| 497 |
{ |
| 498 |
socklen_t socklen; |
| 499 |
int ret; |
| 500 |
|
| 501 |
socklen = sizeof(ctx->remote); |
| 502 |
ret = accept(ctx->socket, (struct sockaddr *)&ctx->remote, &socklen); |
| 503 |
if (ret < 0) { |
| 504 |
perror("accept"); |
| 505 |
goto err; |
| 506 |
} |
| 507 |
|
| 508 |
fprintf(stderr, "Connection received!\n"); |
| 509 |
|
| 510 |
switch (socklen) { |
| 511 |
case sizeof(struct sockaddr_in): |
| 512 |
case sizeof(struct sockaddr_in6): |
| 513 |
break; |
| 514 |
default: |
| 515 |
fprintf(stderr, "error: expecting IPv4 or IPv6 connection\n"); |
| 516 |
close(ret); |
| 517 |
ret = -EINVAL; |
| 518 |
goto err; |
| 519 |
} |
| 520 |
|
| 521 |
return ret; |
| 522 |
err: |
| 523 |
close(ctx->socket); |
| 524 |
return ret; |
| 525 |
} |
| 526 |
|
| 527 |
static int ovpn_connect(struct ovpn_ctx *ovpn) |
| 528 |
{ |
| 529 |
socklen_t socklen; |
| 530 |
int s, ret; |
| 531 |
|
| 532 |
s = socket(ovpn->remote.in4.sin_family, SOCK_STREAM, 0); |
| 533 |
if (s < 0) { |
| 534 |
perror("cannot create socket"); |
| 535 |
return -1; |
| 536 |
} |
| 537 |
|
| 538 |
switch (ovpn->remote.in4.sin_family) { |
| 539 |
case AF_INET: |
| 540 |
socklen = sizeof(struct sockaddr_in); |
| 541 |
break; |
| 542 |
case AF_INET6: |
| 543 |
socklen = sizeof(struct sockaddr_in6); |
| 544 |
break; |
| 545 |
default: |
| 546 |
return -EOPNOTSUPP; |
| 547 |
} |
| 548 |
|
| 549 |
ret = connect(s, (struct sockaddr *)&ovpn->remote, socklen); |
| 550 |
if (ret < 0) { |
| 551 |
perror("connect"); |
| 552 |
goto err; |
| 553 |
} |
| 554 |
|
| 555 |
fprintf(stderr, "connected\n"); |
| 556 |
|
| 557 |
ovpn->socket = s; |
| 558 |
|
| 559 |
return 0; |
| 560 |
err: |
| 561 |
close(s); |
| 562 |
return ret; |
| 563 |
} |
| 564 |
|
| 565 |
static int ovpn_new_peer(struct ovpn_ctx *ovpn, bool is_tcp) |
| 566 |
{ |
| 567 |
struct nlattr *attr; |
| 568 |
struct nl_ctx *ctx; |
| 569 |
size_t alen; |
| 570 |
int ret = -1; |
| 571 |
|
| 572 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_NEW_PEER); |
| 573 |
if (!ctx) |
| 574 |
return -ENOMEM; |
| 575 |
|
| 576 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_NEW_PEER); |
| 577 |
NLA_PUT_U32(ctx->nl_msg, OVPN_NEW_PEER_ATTR_PEER_ID, ovpn->peer_id); |
| 578 |
NLA_PUT_U32(ctx->nl_msg, OVPN_NEW_PEER_ATTR_SOCKET, ovpn->socket); |
| 579 |
|
| 580 |
if (!is_tcp) { |
| 581 |
switch (ovpn->remote.in4.sin_family) { |
| 582 |
case AF_INET: |
| 583 |
alen = sizeof(struct sockaddr_in); |
| 584 |
break; |
| 585 |
case AF_INET6: |
| 586 |
alen = sizeof(struct sockaddr_in6); |
| 587 |
break; |
| 588 |
default: |
| 589 |
fprintf(stderr, "Invalid family for remote socket address\n"); |
| 590 |
goto nla_put_failure; |
| 591 |
} |
| 592 |
NLA_PUT(ctx->nl_msg, OVPN_NEW_PEER_ATTR_SOCKADDR_REMOTE, alen, &ovpn->remote); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
switch (ovpn->peer_ip.in4.sin_family) { |
| 597 |
case AF_INET: |
| 598 |
NLA_PUT_U32(ctx->nl_msg, OVPN_NEW_PEER_ATTR_IPV4, |
| 599 |
ovpn->peer_ip.in4.sin_addr.s_addr); |
| 600 |
break; |
| 601 |
case AF_INET6: |
| 602 |
NLA_PUT(ctx->nl_msg, OVPN_NEW_PEER_ATTR_IPV6, sizeof(struct in6_addr), |
| 603 |
&ovpn->peer_ip.in6.sin6_addr); |
| 604 |
break; |
| 605 |
default: |
| 606 |
fprintf(stderr, "Invalid family for peer address\n"); |
| 607 |
goto nla_put_failure; |
| 608 |
} |
| 609 |
|
| 610 |
nla_nest_end(ctx->nl_msg, attr); |
| 611 |
|
| 612 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 613 |
nla_put_failure: |
| 614 |
nl_ctx_free(ctx); |
| 615 |
return ret; |
| 616 |
} |
| 617 |
|
| 618 |
static int ovpn_set_peer(struct ovpn_ctx *ovpn) |
| 619 |
{ |
| 620 |
struct nlattr *attr; |
| 621 |
struct nl_ctx *ctx; |
| 622 |
int ret = -1; |
| 623 |
|
| 624 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_SET_PEER); |
| 625 |
if (!ctx) |
| 626 |
return -ENOMEM; |
| 627 |
|
| 628 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_SET_PEER); |
| 629 |
NLA_PUT_U32(ctx->nl_msg, OVPN_SET_PEER_ATTR_PEER_ID, ovpn->peer_id); |
| 630 |
NLA_PUT_U32(ctx->nl_msg, OVPN_SET_PEER_ATTR_KEEPALIVE_INTERVAL, |
| 631 |
ovpn->keepalive_interval); |
| 632 |
NLA_PUT_U32(ctx->nl_msg, OVPN_SET_PEER_ATTR_KEEPALIVE_TIMEOUT, |
| 633 |
ovpn->keepalive_timeout); |
| 634 |
nla_nest_end(ctx->nl_msg, attr); |
| 635 |
|
| 636 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 637 |
nla_put_failure: |
| 638 |
nl_ctx_free(ctx); |
| 639 |
return ret; |
| 640 |
} |
| 641 |
|
| 642 |
static int ovpn_del_peer(struct ovpn_ctx *ovpn) |
| 643 |
{ |
| 644 |
struct nlattr *attr; |
| 645 |
struct nl_ctx *ctx; |
| 646 |
int ret = -1; |
| 647 |
|
| 648 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_DEL_PEER); |
| 649 |
if (!ctx) |
| 650 |
return -ENOMEM; |
| 651 |
|
| 652 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_DEL_PEER); |
| 653 |
NLA_PUT_U32(ctx->nl_msg, OVPN_DEL_PEER_ATTR_PEER_ID, ovpn->peer_id); |
| 654 |
nla_nest_end(ctx->nl_msg, attr); |
| 655 |
|
| 656 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 657 |
nla_put_failure: |
| 658 |
nl_ctx_free(ctx); |
| 659 |
return ret; |
| 660 |
} |
| 661 |
|
| 662 |
static int ovpn_handle_peer(struct nl_msg *msg, void *arg) |
| 663 |
{ |
| 664 |
struct nlattr *attrs_peer[OVPN_GET_PEER_RESP_ATTR_MAX + 1]; |
| 665 |
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 666 |
struct nlattr *attrs[OVPN_ATTR_MAX + 1]; |
| 667 |
__u16 port = 0; |
| 668 |
|
| 669 |
nla_parse(attrs, OVPN_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 670 |
genlmsg_attrlen(gnlh, 0), NULL); |
| 671 |
|
| 672 |
if (!attrs[OVPN_ATTR_GET_PEER]) { |
| 673 |
fprintf(stderr, "no packet content in netlink message\n"); |
| 674 |
return NL_SKIP; |
| 675 |
} |
| 676 |
|
| 677 |
nla_parse(attrs_peer, OVPN_GET_PEER_RESP_ATTR_MAX, nla_data(attrs[OVPN_ATTR_GET_PEER]), |
| 678 |
nla_len(attrs[OVPN_ATTR_GET_PEER]), NULL); |
| 679 |
|
| 680 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_PEER_ID]) |
| 681 |
fprintf(stderr, "* Peer %u\n", |
| 682 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_PEER_ID])); |
| 683 |
|
| 684 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_IPV4]) { |
| 685 |
char buf[INET_ADDRSTRLEN]; |
| 686 |
inet_ntop(AF_INET, nla_data(attrs_peer[OVPN_GET_PEER_RESP_ATTR_IPV4]), buf, |
| 687 |
sizeof(buf)); |
| 688 |
fprintf(stderr, "\tVPN IPv4: %s\n", buf); |
| 689 |
} |
| 690 |
|
| 691 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_IPV6]) { |
| 692 |
char buf[INET6_ADDRSTRLEN]; |
| 693 |
inet_ntop(AF_INET6, nla_data(attrs_peer[OVPN_GET_PEER_RESP_ATTR_IPV6]), buf, |
| 694 |
sizeof(buf)); |
| 695 |
fprintf(stderr, "\tVPN IPv6: %s\n", buf); |
| 696 |
} |
| 697 |
|
| 698 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_PORT]) |
| 699 |
port = ntohs(nla_get_u16(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_PORT])); |
| 700 |
|
| 701 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_SOCKADDR_REMOTE]) { |
| 702 |
struct sockaddr_storage ss; |
| 703 |
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss; |
| 704 |
struct sockaddr_in *in = (struct sockaddr_in *)&ss; |
| 705 |
|
| 706 |
memcpy(&ss, nla_data(attrs_peer[OVPN_GET_PEER_RESP_ATTR_SOCKADDR_REMOTE]), |
| 707 |
nla_len(attrs_peer[OVPN_GET_PEER_RESP_ATTR_SOCKADDR_REMOTE])); |
| 708 |
|
| 709 |
if (in->sin_family == AF_INET) { |
| 710 |
char buf[INET_ADDRSTRLEN]; |
| 711 |
|
| 712 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_IP]) { |
| 713 |
inet_ntop(AF_INET, |
| 714 |
nla_data(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_IP]), |
| 715 |
buf, sizeof(buf)); |
| 716 |
fprintf(stderr, "\tLocal: %s:%hu\n", buf, port); |
| 717 |
} |
| 718 |
|
| 719 |
inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf)); |
| 720 |
fprintf(stderr, "\tRemote: %s:%u\n", buf, ntohs(in->sin_port)); |
| 721 |
} else if (in->sin_family == AF_INET6) { |
| 722 |
char buf[INET6_ADDRSTRLEN]; |
| 723 |
|
| 724 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_IP]) { |
| 725 |
inet_ntop(AF_INET6, |
| 726 |
nla_data(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LOCAL_IP]), |
| 727 |
buf, sizeof(buf)); |
| 728 |
fprintf(stderr, "\tLocal: %s\n", buf); |
| 729 |
} |
| 730 |
|
| 731 |
inet_ntop(AF_INET6, &in6->sin6_addr, buf, sizeof(buf)); |
| 732 |
fprintf(stderr, "\tRemote: %s:%u (scope-id: %u)\n", buf, |
| 733 |
ntohs(in6->sin6_port), ntohl(in6->sin6_scope_id)); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_KEEPALIVE_INTERVAL]) |
| 738 |
fprintf(stderr, "\tKeepalive interval: %u sec\n", |
| 739 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_KEEPALIVE_INTERVAL])); |
| 740 |
|
| 741 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_KEEPALIVE_TIMEOUT]) |
| 742 |
fprintf(stderr, "\tKeepalive timeout: %u sec\n", |
| 743 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_KEEPALIVE_TIMEOUT])); |
| 744 |
|
| 745 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_RX_BYTES]) |
| 746 |
fprintf(stderr, "\tVPN RX bytes: %" PRIu64 "\n", |
| 747 |
nla_get_u64(attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_RX_BYTES])); |
| 748 |
|
| 749 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_TX_BYTES]) |
| 750 |
fprintf(stderr, "\tVPN TX bytes: %" PRIu64 "\n", |
| 751 |
nla_get_u64(attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_TX_BYTES])); |
| 752 |
|
| 753 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_RX_PACKETS]) |
| 754 |
fprintf(stderr, "\tVPN RX packets: %u\n", |
| 755 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_RX_PACKETS])); |
| 756 |
|
| 757 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_TX_PACKETS]) |
| 758 |
fprintf(stderr, "\tVPN TX packets: %u\n", |
| 759 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_VPN_TX_PACKETS])); |
| 760 |
|
| 761 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_RX_BYTES]) |
| 762 |
fprintf(stderr, "\tLINK RX bytes: %" PRIu64 "\n", |
| 763 |
nla_get_u64(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_RX_BYTES])); |
| 764 |
|
| 765 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_TX_BYTES]) |
| 766 |
fprintf(stderr, "\tLINK TX bytes: %" PRIu64 "\n", |
| 767 |
nla_get_u64(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_TX_BYTES])); |
| 768 |
|
| 769 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_RX_PACKETS]) |
| 770 |
fprintf(stderr, "\tLINK RX packets: %u\n", |
| 771 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_RX_PACKETS])); |
| 772 |
|
| 773 |
if (attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_TX_PACKETS]) |
| 774 |
fprintf(stderr, "\tLINK TX packets: %u\n", |
| 775 |
nla_get_u32(attrs_peer[OVPN_GET_PEER_RESP_ATTR_LINK_TX_PACKETS])); |
| 776 |
|
| 777 |
return NL_SKIP; |
| 778 |
} |
| 779 |
|
| 780 |
static int ovpn_get_peer(struct ovpn_ctx *ovpn) |
| 781 |
{ |
| 782 |
int flags = 0, ret = -1; |
| 783 |
struct nlattr *attr; |
| 784 |
struct nl_ctx *ctx; |
| 785 |
|
| 786 |
if (ovpn->peer_id == PEER_ID_UNDEF) |
| 787 |
flags = NLM_F_DUMP; |
| 788 |
|
| 789 |
ctx = nl_ctx_alloc_flags(ovpn, OVPN_CMD_GET_PEER, flags); |
| 790 |
if (!ctx) |
| 791 |
return -ENOMEM; |
| 792 |
|
| 793 |
if (ovpn->peer_id >= 0) { |
| 794 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_GET_PEER); |
| 795 |
NLA_PUT_U32(ctx->nl_msg, OVPN_GET_PEER_ATTR_PEER_ID, ovpn->peer_id); |
| 796 |
nla_nest_end(ctx->nl_msg, attr); |
| 797 |
} |
| 798 |
|
| 799 |
ret = ovpn_nl_msg_send(ctx, ovpn_handle_peer); |
| 800 |
nla_put_failure: |
| 801 |
nl_ctx_free(ctx); |
| 802 |
return ret; |
| 803 |
} |
| 804 |
|
| 805 |
static int ovpn_new_key(struct ovpn_ctx *ovpn) |
| 806 |
{ |
| 807 |
struct nlattr *attr, *key_dir; |
| 808 |
struct nl_ctx *ctx; |
| 809 |
int ret = -1; |
| 810 |
|
| 811 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_NEW_KEY); |
| 812 |
if (!ctx) |
| 813 |
return -ENOMEM; |
| 814 |
|
| 815 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_NEW_KEY); |
| 816 |
NLA_PUT_U32(ctx->nl_msg, OVPN_NEW_KEY_ATTR_PEER_ID, ovpn->peer_id); |
| 817 |
NLA_PUT_U8(ctx->nl_msg, OVPN_NEW_KEY_ATTR_KEY_SLOT, OVPN_KEY_SLOT_PRIMARY); |
| 818 |
NLA_PUT_U8(ctx->nl_msg, OVPN_NEW_KEY_ATTR_KEY_ID, 0); |
| 819 |
|
| 820 |
NLA_PUT_U16(ctx->nl_msg, OVPN_NEW_KEY_ATTR_CIPHER_ALG, ovpn->cipher); |
| 821 |
|
| 822 |
key_dir = nla_nest_start(ctx->nl_msg, OVPN_NEW_KEY_ATTR_ENCRYPT_KEY); |
| 823 |
NLA_PUT(ctx->nl_msg, OVPN_KEY_DIR_ATTR_CIPHER_KEY, KEY_LEN, |
| 824 |
ovpn->key_enc); |
| 825 |
NLA_PUT(ctx->nl_msg, OVPN_KEY_DIR_ATTR_NONCE_TAIL, NONCE_LEN, |
| 826 |
ovpn->nonce); |
| 827 |
nla_nest_end(ctx->nl_msg, key_dir); |
| 828 |
|
| 829 |
key_dir = nla_nest_start(ctx->nl_msg, OVPN_NEW_KEY_ATTR_DECRYPT_KEY); |
| 830 |
NLA_PUT(ctx->nl_msg, OVPN_KEY_DIR_ATTR_CIPHER_KEY, KEY_LEN, |
| 831 |
ovpn->key_dec); |
| 832 |
NLA_PUT(ctx->nl_msg, OVPN_KEY_DIR_ATTR_NONCE_TAIL, NONCE_LEN, |
| 833 |
ovpn->nonce); |
| 834 |
nla_nest_end(ctx->nl_msg, key_dir); |
| 835 |
nla_nest_end(ctx->nl_msg, attr); |
| 836 |
|
| 837 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 838 |
nla_put_failure: |
| 839 |
nl_ctx_free(ctx); |
| 840 |
return ret; |
| 841 |
} |
| 842 |
|
| 843 |
static int ovpn_del_key(struct ovpn_ctx *ovpn) |
| 844 |
{ |
| 845 |
struct nlattr *attr; |
| 846 |
struct nl_ctx *ctx; |
| 847 |
int ret = -1; |
| 848 |
|
| 849 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_DEL_KEY); |
| 850 |
if (!ctx) |
| 851 |
return -ENOMEM; |
| 852 |
|
| 853 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_DEL_KEY); |
| 854 |
NLA_PUT_U32(ctx->nl_msg, OVPN_DEL_KEY_ATTR_PEER_ID, ovpn->peer_id); |
| 855 |
NLA_PUT_U8(ctx->nl_msg, OVPN_DEL_KEY_ATTR_KEY_SLOT, OVPN_KEY_SLOT_PRIMARY); |
| 856 |
nla_nest_end(ctx->nl_msg, attr); |
| 857 |
|
| 858 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 859 |
nla_put_failure: |
| 860 |
nl_ctx_free(ctx); |
| 861 |
return ret; |
| 862 |
} |
| 863 |
|
| 864 |
static int ovpn_swap_keys(struct ovpn_ctx *ovpn) |
| 865 |
{ |
| 866 |
struct nlattr *attr; |
| 867 |
struct nl_ctx *ctx; |
| 868 |
int ret = -1; |
| 869 |
|
| 870 |
ctx = nl_ctx_alloc(ovpn, OVPN_CMD_SWAP_KEYS); |
| 871 |
if (!ctx) |
| 872 |
return -ENOMEM; |
| 873 |
|
| 874 |
attr = nla_nest_start(ctx->nl_msg, OVPN_ATTR_SWAP_KEYS); |
| 875 |
NLA_PUT_U32(ctx->nl_msg, OVPN_SWAP_KEYS_ATTR_PEER_ID, ovpn->peer_id); |
| 876 |
nla_nest_end(ctx->nl_msg, attr); |
| 877 |
|
| 878 |
ret = ovpn_nl_msg_send(ctx, NULL); |
| 879 |
nla_put_failure: |
| 880 |
nl_ctx_free(ctx); |
| 881 |
return ret; |
| 882 |
} |
| 883 |
|
| 884 |
static int nl_seq_check(struct nl_msg *msg, void *arg) |
| 885 |
{ |
| 886 |
return NL_OK; |
| 887 |
} |
| 888 |
|
| 889 |
struct mcast_handler_args { |
| 890 |
const char *group; |
| 891 |
int id; |
| 892 |
}; |
| 893 |
|
| 894 |
static int mcast_family_handler(struct nl_msg *msg, void *arg) |
| 895 |
{ |
| 896 |
struct mcast_handler_args *grp = arg; |
| 897 |
struct nlattr *tb[CTRL_ATTR_MAX + 1]; |
| 898 |
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 899 |
struct nlattr *mcgrp; |
| 900 |
int rem_mcgrp; |
| 901 |
|
| 902 |
nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 903 |
genlmsg_attrlen(gnlh, 0), NULL); |
| 904 |
|
| 905 |
if (!tb[CTRL_ATTR_MCAST_GROUPS]) |
| 906 |
return NL_SKIP; |
| 907 |
|
| 908 |
nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) { |
| 909 |
struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1]; |
| 910 |
|
| 911 |
nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX, |
| 912 |
nla_data(mcgrp), nla_len(mcgrp), NULL); |
| 913 |
|
| 914 |
if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] || |
| 915 |
!tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]) |
| 916 |
continue; |
| 917 |
if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]), |
| 918 |
grp->group, nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]))) |
| 919 |
continue; |
| 920 |
grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]); |
| 921 |
break; |
| 922 |
} |
| 923 |
|
| 924 |
return NL_SKIP; |
| 925 |
} |
| 926 |
|
| 927 |
static int mcast_error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
| 928 |
void *arg) |
| 929 |
{ |
| 930 |
int *ret = arg; |
| 931 |
|
| 932 |
*ret = err->error; |
| 933 |
return NL_STOP; |
| 934 |
} |
| 935 |
|
| 936 |
static int mcast_ack_handler(struct nl_msg *msg, void *arg) |
| 937 |
{ |
| 938 |
int *ret = arg; |
| 939 |
|
| 940 |
*ret = 0; |
| 941 |
return NL_STOP; |
| 942 |
} |
| 943 |
|
| 944 |
static int ovpn_handle_msg(struct nl_msg *msg, void *arg) |
| 945 |
{ |
| 946 |
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 947 |
struct nlattr *attrs[OVPN_ATTR_MAX + 1]; |
| 948 |
struct nlmsghdr *nlh = nlmsg_hdr(msg); |
| 949 |
|
| 950 |
char ifname[IF_NAMESIZE]; |
| 951 |
__u32 ifindex; |
| 952 |
|
| 953 |
fprintf(stderr, "received message from ovpn-dco\n"); |
| 954 |
|
| 955 |
if (!genlmsg_valid_hdr(nlh, 0)) { |
| 956 |
fprintf(stderr, "invalid header\n"); |
| 957 |
return NL_STOP; |
| 958 |
} |
| 959 |
|
| 960 |
if (nla_parse(attrs, OVPN_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 961 |
genlmsg_attrlen(gnlh, 0), NULL)) { |
| 962 |
fprintf(stderr, "received bogus data from ovpn-dco\n"); |
| 963 |
return NL_STOP; |
| 964 |
} |
| 965 |
|
| 966 |
if (!attrs[OVPN_ATTR_IFINDEX]) { |
| 967 |
fprintf(stderr, "no ifindex in this message\n"); |
| 968 |
return NL_STOP; |
| 969 |
} |
| 970 |
|
| 971 |
ifindex = nla_get_u32(attrs[OVPN_ATTR_IFINDEX]); |
| 972 |
if (!if_indextoname(ifindex, ifname)) { |
| 973 |
fprintf(stderr, "cannot resolve ifname for ifinxed: %u\n", |
| 974 |
ifindex); |
| 975 |
return NL_STOP; |
| 976 |
} |
| 977 |
|
| 978 |
switch (gnlh->cmd) { |
| 979 |
case OVPN_CMD_DEL_PEER: |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
fprintf(stdout, "received CMD_DEL_PEER\n"); |
| 990 |
break; |
| 991 |
default: |
| 992 |
fprintf(stderr, "received unknown command: %d\n", gnlh->cmd); |
| 993 |
return NL_STOP; |
| 994 |
} |
| 995 |
|
| 996 |
return NL_OK; |
| 997 |
} |
| 998 |
|
| 999 |
static int ovpn_get_mcast_id(struct nl_sock *sock, const char *family, |
| 1000 |
const char *group) |
| 1001 |
{ |
| 1002 |
struct nl_msg *msg; |
| 1003 |
struct nl_cb *cb; |
| 1004 |
int ret, ctrlid; |
| 1005 |
struct mcast_handler_args grp = { |
| 1006 |
.group = group, |
| 1007 |
.id = -ENOENT, |
| 1008 |
}; |
| 1009 |
|
| 1010 |
msg = nlmsg_alloc(); |
| 1011 |
if (!msg) |
| 1012 |
return -ENOMEM; |
| 1013 |
|
| 1014 |
cb = nl_cb_alloc(NL_CB_DEFAULT); |
| 1015 |
if (!cb) { |
| 1016 |
ret = -ENOMEM; |
| 1017 |
goto out_fail_cb; |
| 1018 |
} |
| 1019 |
|
| 1020 |
ctrlid = genl_ctrl_resolve(sock, "nlctrl"); |
| 1021 |
|
| 1022 |
genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); |
| 1023 |
|
| 1024 |
ret = -ENOBUFS; |
| 1025 |
NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); |
| 1026 |
|
| 1027 |
ret = nl_send_auto_complete(sock, msg); |
| 1028 |
if (ret < 0) |
| 1029 |
goto nla_put_failure; |
| 1030 |
|
| 1031 |
ret = 1; |
| 1032 |
|
| 1033 |
nl_cb_err(cb, NL_CB_CUSTOM, mcast_error_handler, &ret); |
| 1034 |
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, mcast_ack_handler, &ret); |
| 1035 |
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, mcast_family_handler, &grp); |
| 1036 |
|
| 1037 |
while (ret > 0) |
| 1038 |
nl_recvmsgs(sock, cb); |
| 1039 |
|
| 1040 |
if (ret == 0) |
| 1041 |
ret = grp.id; |
| 1042 |
nla_put_failure: |
| 1043 |
nl_cb_put(cb); |
| 1044 |
out_fail_cb: |
| 1045 |
nlmsg_free(msg); |
| 1046 |
return ret; |
| 1047 |
} |
| 1048 |
|
| 1049 |
static void ovpn_listen_mcast(void) |
| 1050 |
{ |
| 1051 |
struct nl_sock *sock; |
| 1052 |
struct nl_cb *cb; |
| 1053 |
int mcid, ret; |
| 1054 |
|
| 1055 |
sock = nl_socket_alloc(); |
| 1056 |
if (!sock) { |
| 1057 |
fprintf(stderr, "cannot allocate netlink socket\n"); |
| 1058 |
goto err_free; |
| 1059 |
} |
| 1060 |
|
| 1061 |
nl_socket_set_buffer_size(sock, 8192, 8192); |
| 1062 |
|
| 1063 |
ret = genl_connect(sock); |
| 1064 |
if (ret < 0) { |
| 1065 |
fprintf(stderr, "cannot connect to generic netlink: %s\n", |
| 1066 |
nl_geterror(ret)); |
| 1067 |
goto err_free; |
| 1068 |
} |
| 1069 |
|
| 1070 |
mcid = ovpn_get_mcast_id(sock, OVPN_NL_NAME, |
| 1071 |
OVPN_NL_MULTICAST_GROUP_PEERS); |
| 1072 |
if (mcid < 0) { |
| 1073 |
fprintf(stderr, "cannot get mcast group: %s\n", |
| 1074 |
nl_geterror(mcid)); |
| 1075 |
goto err_free; |
| 1076 |
} |
| 1077 |
|
| 1078 |
ret = nl_socket_add_membership(sock, mcid); |
| 1079 |
if (ret) { |
| 1080 |
fprintf(stderr, "failed to join mcast group: %d\n", ret); |
| 1081 |
goto err_free; |
| 1082 |
} |
| 1083 |
|
| 1084 |
ret = 0; |
| 1085 |
cb = nl_cb_alloc(NL_CB_DEFAULT); |
| 1086 |
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl_seq_check, NULL); |
| 1087 |
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, &ret); |
| 1088 |
nl_cb_err(cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &ret); |
| 1089 |
|
| 1090 |
while (ret != -EINTR) |
| 1091 |
ret = nl_recvmsgs(sock, cb); |
| 1092 |
|
| 1093 |
nl_cb_put(cb); |
| 1094 |
err_free: |
| 1095 |
nl_socket_free(sock); |
| 1096 |
} |
| 1097 |
|
| 1098 |
static void usage(const char *cmd) |
| 1099 |
{ |
| 1100 |
fprintf(stderr, "Error: invalid arguments.\n\n"); |
| 1101 |
fprintf(stderr, |
| 1102 |
"Usage %s <iface> <connect|listen|new_peer|new_multi_peer|set_peer|del_peer|new_key|del_key|recv|send|listen_mcast> [arguments..]\n", |
| 1103 |
cmd); |
| 1104 |
fprintf(stderr, "\tiface: tun interface name\n\n"); |
| 1105 |
|
| 1106 |
fprintf(stderr, "* connect <peer_id> <raddr> <rport> <vpnaddr>: start connecting peer of TCP-based VPN session\n"); |
| 1107 |
fprintf(stderr, "\tpeer-id: peer ID of the connecting peer\n"); |
| 1108 |
fprintf(stderr, "\tremote-addr: peer IP address\n"); |
| 1109 |
fprintf(stderr, "\tremote-port: peer TCP port\n"); |
| 1110 |
fprintf(stderr, "\tvpn-ip: peer VPN IP\n\n"); |
| 1111 |
|
| 1112 |
fprintf(stderr, "* listen <lport> <peers_file>: listen for incoming peer TCP connections\n"); |
| 1113 |
fprintf(stderr, "\tlport: src TCP port\n"); |
| 1114 |
fprintf(stderr, "\tpeers_file: file containing one peer per line: Line format:\n"); |
| 1115 |
fprintf(stderr, "\t\t<peer_id> <vpnaddr>\n\n"); |
| 1116 |
|
| 1117 |
fprintf(stderr, "* new_peer <lport> <peer-id> <raddr> <rport> <vpnaddr>: add new peer\n"); |
| 1118 |
fprintf(stderr, "\tpeer-id: peer ID to be used in data packets to/from this peer\n"); |
| 1119 |
fprintf(stderr, "\tlocal-port: local UDP port\n"); |
| 1120 |
fprintf(stderr, "\tremote-addr: peer IP address\n"); |
| 1121 |
fprintf(stderr, "\tremote-port: peer UDP port\n"); |
| 1122 |
fprintf(stderr, "\tvpnaddr: peer VPN IP\n\n"); |
| 1123 |
|
| 1124 |
fprintf(stderr, "* new_multi_peer <lport> <file>: add multiple peers as listed in the file\n"); |
| 1125 |
fprintf(stderr, "\tlport: local UDP port to bind to\n"); |
| 1126 |
fprintf(stderr, "\tfile: text file containing one peer per line. Line format:\n"); |
| 1127 |
fprintf(stderr, "\t\t<peer-id> <raddr> <rport> <vpnaddr>\n\n"); |
| 1128 |
|
| 1129 |
fprintf(stderr, |
| 1130 |
"* set_peer <peer-id> <keepalive_interval> <keepalive_timeout>: set peer attributes\n"); |
| 1131 |
fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n"); |
| 1132 |
fprintf(stderr, |
| 1133 |
"\tkeepalive_interval: interval for sending ping messages\n"); |
| 1134 |
fprintf(stderr, |
| 1135 |
"\tkeepalive_timeout: time after which a peer is timed out\n\n"); |
| 1136 |
|
| 1137 |
fprintf(stderr, "* del_peer <peer-id>: delete peer\n"); |
| 1138 |
fprintf(stderr, "\tpeer-id: peer ID of the peer to delete\n\n"); |
| 1139 |
|
| 1140 |
fprintf(stderr, |
| 1141 |
"* new_key <peer-id> <cipher> <key_dir> <key_file>: set data channel key\n"); |
| 1142 |
fprintf(stderr, "\tpeer-id: peer ID of the peer to configure the key for\n"); |
| 1143 |
fprintf(stderr, |
| 1144 |
"\tcipher: cipher to use, supported: aes (AES-GCM), chachapoly (CHACHA20POLY1305), none\n"); |
| 1145 |
fprintf(stderr, |
| 1146 |
"\tkey_dir: key direction, must 0 on one host and 1 on the other\n"); |
| 1147 |
fprintf(stderr, "\tkey_file: file containing the pre-shared key\n\n"); |
| 1148 |
|
| 1149 |
fprintf(stderr, "* del_key <peer-id>: erase existing data channel key\n"); |
| 1150 |
fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n\n"); |
| 1151 |
|
| 1152 |
fprintf(stderr, "* swap_keys <peer-id>: swap primary and seconday key slots\n"); |
| 1153 |
fprintf(stderr, "\tpeer-id: peer ID of the peer to modify\n\n"); |
| 1154 |
|
| 1155 |
fprintf(stderr, "* listen_mcast: listen to ovpn-dco netlink multicast messages\n"); |
| 1156 |
} |
| 1157 |
|
| 1158 |
static int ovpn_parse_remote(struct ovpn_ctx *ovpn, const char *host, const char *service, |
| 1159 |
const char *vpn_addr) |
| 1160 |
{ |
| 1161 |
int ret; |
| 1162 |
struct addrinfo *result; |
| 1163 |
struct addrinfo hints = { |
| 1164 |
.ai_family = ovpn->sa_family, |
| 1165 |
.ai_socktype = SOCK_DGRAM, |
| 1166 |
.ai_protocol = IPPROTO_UDP |
| 1167 |
}; |
| 1168 |
|
| 1169 |
if (host) { |
| 1170 |
ret = getaddrinfo(host, service, &hints, &result); |
| 1171 |
if (ret == EAI_NONAME || ret == EAI_FAIL) |
| 1172 |
return -1; |
| 1173 |
|
| 1174 |
if (!(result->ai_family == AF_INET && result->ai_addrlen == sizeof(struct sockaddr_in)) && |
| 1175 |
!(result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct sockaddr_in6))) { |
| 1176 |
ret = -EINVAL; |
| 1177 |
goto out; |
| 1178 |
} |
| 1179 |
|
| 1180 |
memcpy(&ovpn->remote, result->ai_addr, result->ai_addrlen); |
| 1181 |
} |
| 1182 |
|
| 1183 |
ret = getaddrinfo(vpn_addr, NULL, &hints, &result); |
| 1184 |
if (ret == EAI_NONAME || ret == EAI_FAIL) |
| 1185 |
return -1; |
| 1186 |
|
| 1187 |
if (!(result->ai_family == AF_INET && result->ai_addrlen == sizeof(struct sockaddr_in)) && |
| 1188 |
!(result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct sockaddr_in6))) { |
| 1189 |
ret = -EINVAL; |
| 1190 |
goto out; |
| 1191 |
} |
| 1192 |
|
| 1193 |
memcpy(&ovpn->peer_ip, result->ai_addr, result->ai_addrlen); |
| 1194 |
ovpn->sa_family = result->ai_family; |
| 1195 |
|
| 1196 |
ret = 0; |
| 1197 |
out: |
| 1198 |
freeaddrinfo(result); |
| 1199 |
return ret; |
| 1200 |
} |
| 1201 |
|
| 1202 |
static int ovpn_parse_new_peer(struct ovpn_ctx *ovpn, const char *peer_id, const char *raddr, |
| 1203 |
const char *rport, const char *vpnip) |
| 1204 |
{ |
| 1205 |
ovpn->peer_id = strtoul(peer_id, NULL, 10); |
| 1206 |
if (errno == ERANGE) { |
| 1207 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1208 |
return -1; |
| 1209 |
} |
| 1210 |
|
| 1211 |
return ovpn_parse_remote(ovpn, raddr, rport, vpnip); |
| 1212 |
} |
| 1213 |
|
| 1214 |
static int ovpn_parse_set_peer(struct ovpn_ctx *ovpn, int argc, char *argv[]) |
| 1215 |
{ |
| 1216 |
if (argc < 5) { |
| 1217 |
usage(argv[0]); |
| 1218 |
return -1; |
| 1219 |
} |
| 1220 |
|
| 1221 |
ovpn->keepalive_interval = strtoul(argv[3], NULL, 10); |
| 1222 |
if (errno == ERANGE) { |
| 1223 |
fprintf(stderr, "keepalive interval value out of range\n"); |
| 1224 |
return -1; |
| 1225 |
} |
| 1226 |
|
| 1227 |
ovpn->keepalive_timeout = strtoul(argv[4], NULL, 10); |
| 1228 |
if (errno == ERANGE) { |
| 1229 |
fprintf(stderr, "keepalive interval value out of range\n"); |
| 1230 |
return -1; |
| 1231 |
} |
| 1232 |
|
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
|
| 1236 |
int main(int argc, char *argv[]) |
| 1237 |
{ |
| 1238 |
struct ovpn_ctx ovpn; |
| 1239 |
|
| 1240 |
int ret; |
| 1241 |
|
| 1242 |
if (argc < 3) { |
| 1243 |
usage(argv[0]); |
| 1244 |
return -1; |
| 1245 |
} |
| 1246 |
|
| 1247 |
memset(&ovpn, 0, sizeof(ovpn)); |
| 1248 |
ovpn.sa_family = AF_INET; |
| 1249 |
|
| 1250 |
ovpn.ifindex = if_nametoindex(argv[1]); |
| 1251 |
if (!ovpn.ifindex) { |
| 1252 |
fprintf(stderr, "cannot find interface: %s\n", |
| 1253 |
strerror(errno)); |
| 1254 |
return -1; |
| 1255 |
} |
| 1256 |
|
| 1257 |
if (!strcmp(argv[2], "listen")) { |
| 1258 |
char peer_id[10], vpnip[100]; |
| 1259 |
int n; |
| 1260 |
FILE *fp; |
| 1261 |
|
| 1262 |
if (argc < 4) { |
| 1263 |
usage(argv[0]); |
| 1264 |
return -1; |
| 1265 |
} |
| 1266 |
|
| 1267 |
ovpn.lport = strtoul(argv[3], NULL, 10); |
| 1268 |
if (errno == ERANGE || ovpn.lport > 65535) { |
| 1269 |
fprintf(stderr, "lport value out of range\n"); |
| 1270 |
return -1; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (argc > 4 && !strcmp(argv[4], "ipv6")) |
| 1274 |
ovpn.sa_family = AF_INET6; |
| 1275 |
|
| 1276 |
ret = ovpn_listen(&ovpn, ovpn.sa_family); |
| 1277 |
if (ret < 0) { |
| 1278 |
fprintf(stderr, "cannot listen on TCP socket\n"); |
| 1279 |
return ret; |
| 1280 |
} |
| 1281 |
|
| 1282 |
fp = fopen(argv[4], "r"); |
| 1283 |
if (!fp) { |
| 1284 |
fprintf(stderr, "cannot open file: %s\n", argv[4]); |
| 1285 |
return -1; |
| 1286 |
} |
| 1287 |
|
| 1288 |
while ((n = fscanf(fp, "%s %s\n", peer_id, vpnip)) == 2) { |
| 1289 |
struct ovpn_ctx peer_ctx = { 0 }; |
| 1290 |
|
| 1291 |
peer_ctx.ifindex = if_nametoindex(argv[1]); |
| 1292 |
peer_ctx.sa_family = ovpn.sa_family; |
| 1293 |
|
| 1294 |
peer_ctx.socket = ovpn_accept(&ovpn); |
| 1295 |
if (peer_ctx.socket < 0) { |
| 1296 |
fprintf(stderr, "cannot accept connection!\n"); |
| 1297 |
return -1; |
| 1298 |
} |
| 1299 |
|
| 1300 |
ret = ovpn_parse_new_peer(&peer_ctx, peer_id, NULL, NULL, vpnip); |
| 1301 |
if (ret < 0) { |
| 1302 |
fprintf(stderr, "error while parsing line\n"); |
| 1303 |
return -1; |
| 1304 |
} |
| 1305 |
|
| 1306 |
ret = ovpn_new_peer(&peer_ctx, true); |
| 1307 |
if (ret < 0) { |
| 1308 |
fprintf(stderr, "cannot add peer to VPN: %s %s\n", peer_id, vpnip); |
| 1309 |
return ret; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} else if (!strcmp(argv[2], "connect")) { |
| 1313 |
if (argc < 6) { |
| 1314 |
usage(argv[0]); |
| 1315 |
return -1; |
| 1316 |
} |
| 1317 |
|
| 1318 |
ovpn.sa_family = AF_INET; |
| 1319 |
|
| 1320 |
ret = ovpn_parse_new_peer(&ovpn, argv[3], argv[4], argv[5], argv[6]); |
| 1321 |
if (ret < 0) { |
| 1322 |
fprintf(stderr, "Cannot parse remote peer data\n"); |
| 1323 |
return ret; |
| 1324 |
} |
| 1325 |
|
| 1326 |
ret = ovpn_connect(&ovpn); |
| 1327 |
if (ret < 0) { |
| 1328 |
fprintf(stderr, "cannot connect TCP socket\n"); |
| 1329 |
return ret; |
| 1330 |
} |
| 1331 |
|
| 1332 |
ret = ovpn_new_peer(&ovpn, true); |
| 1333 |
if (ret < 0) { |
| 1334 |
fprintf(stderr, "cannot add peer to VPN\n"); |
| 1335 |
close(ovpn.socket); |
| 1336 |
return ret; |
| 1337 |
} |
| 1338 |
} else if (!strcmp(argv[2], "new_peer")) { |
| 1339 |
if (argc < 8) { |
| 1340 |
usage(argv[0]); |
| 1341 |
return -1; |
| 1342 |
} |
| 1343 |
|
| 1344 |
ovpn.lport = strtoul(argv[3], NULL, 10); |
| 1345 |
if (errno == ERANGE || ovpn.lport > 65535) { |
| 1346 |
fprintf(stderr, "lport value out of range\n"); |
| 1347 |
return -1; |
| 1348 |
} |
| 1349 |
|
| 1350 |
ret = ovpn_parse_new_peer(&ovpn, argv[4], argv[5], argv[6], argv[7]); |
| 1351 |
if (ret < 0) |
| 1352 |
return ret; |
| 1353 |
|
| 1354 |
ret = ovpn_udp_socket(&ovpn, AF_INET6); |
| 1355 |
if (ret < 0) |
| 1356 |
return ret; |
| 1357 |
|
| 1358 |
ret = ovpn_new_peer(&ovpn, false); |
| 1359 |
if (ret < 0) { |
| 1360 |
fprintf(stderr, "cannot add peer to VPN\n"); |
| 1361 |
return ret; |
| 1362 |
} |
| 1363 |
} else if (!strcmp(argv[2], "new_multi_peer")) { |
| 1364 |
char peer_id[10], raddr[128], rport[10], vpnip[100]; |
| 1365 |
FILE *fp; |
| 1366 |
int n; |
| 1367 |
|
| 1368 |
if (argc < 5) { |
| 1369 |
usage(argv[0]); |
| 1370 |
return -1; |
| 1371 |
} |
| 1372 |
|
| 1373 |
ovpn.lport = strtoul(argv[3], NULL, 10); |
| 1374 |
if (errno == ERANGE || ovpn.lport > 65535) { |
| 1375 |
fprintf(stderr, "lport value out of range\n"); |
| 1376 |
return -1; |
| 1377 |
} |
| 1378 |
|
| 1379 |
fp = fopen(argv[4], "r"); |
| 1380 |
if (!fp) { |
| 1381 |
fprintf(stderr, "cannot open file: %s\n", argv[4]); |
| 1382 |
return -1; |
| 1383 |
} |
| 1384 |
|
| 1385 |
ret = ovpn_udp_socket(&ovpn, AF_INET6); |
| 1386 |
if (ret < 0) |
| 1387 |
return ret; |
| 1388 |
|
| 1389 |
while ((n = fscanf(fp, "%s %s %s %s\n", peer_id, raddr, rport, vpnip)) == 4) { |
| 1390 |
struct ovpn_ctx peer_ctx = { 0 }; |
| 1391 |
|
| 1392 |
peer_ctx.ifindex = if_nametoindex(argv[1]); |
| 1393 |
peer_ctx.socket = ovpn.socket; |
| 1394 |
peer_ctx.sa_family = AF_UNSPEC; |
| 1395 |
|
| 1396 |
ret = ovpn_parse_new_peer(&peer_ctx, peer_id, raddr, rport, vpnip); |
| 1397 |
if (ret < 0) { |
| 1398 |
fprintf(stderr, "error while parsing line\n"); |
| 1399 |
return -1; |
| 1400 |
} |
| 1401 |
|
| 1402 |
ret = ovpn_new_peer(&peer_ctx, false); |
| 1403 |
if (ret < 0) { |
| 1404 |
fprintf(stderr, "cannot add peer to VPN: %s %s %s %s\n", peer_id, |
| 1405 |
raddr, rport, vpnip); |
| 1406 |
return ret; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} else if (!strcmp(argv[2], "set_peer")) { |
| 1410 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1411 |
if (errno == ERANGE) { |
| 1412 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1413 |
return -1; |
| 1414 |
} |
| 1415 |
|
| 1416 |
argv++; |
| 1417 |
argc--; |
| 1418 |
|
| 1419 |
ret = ovpn_parse_set_peer(&ovpn, argc, argv); |
| 1420 |
if (ret < 0) |
| 1421 |
return ret; |
| 1422 |
|
| 1423 |
ret = ovpn_set_peer(&ovpn); |
| 1424 |
if (ret < 0) { |
| 1425 |
fprintf(stderr, "cannot set peer to VPN\n"); |
| 1426 |
return ret; |
| 1427 |
} |
| 1428 |
} else if (!strcmp(argv[2], "del_peer")) { |
| 1429 |
if (argc < 4) { |
| 1430 |
usage(argv[0]); |
| 1431 |
return -1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1435 |
if (errno == ERANGE) { |
| 1436 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1437 |
return -1; |
| 1438 |
} |
| 1439 |
|
| 1440 |
ret = ovpn_del_peer(&ovpn); |
| 1441 |
if (ret < 0) { |
| 1442 |
fprintf(stderr, "cannot delete peer to VPN\n"); |
| 1443 |
return ret; |
| 1444 |
} |
| 1445 |
} else if (!strcmp(argv[2], "get_peer")) { |
| 1446 |
ovpn.peer_id = PEER_ID_UNDEF; |
| 1447 |
if (argc > 3) |
| 1448 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1449 |
|
| 1450 |
fprintf(stderr, "List of peers connected to: %s\n", argv[1]); |
| 1451 |
|
| 1452 |
ret = ovpn_get_peer(&ovpn); |
| 1453 |
if (ret < 0) { |
| 1454 |
fprintf(stderr, "cannot get peer(s): %d\n", ret); |
| 1455 |
return ret; |
| 1456 |
} |
| 1457 |
} else if (!strcmp(argv[2], "new_key")) { |
| 1458 |
if (argc < 6) { |
| 1459 |
usage(argv[0]); |
| 1460 |
return -1; |
| 1461 |
} |
| 1462 |
|
| 1463 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1464 |
if (errno == ERANGE) { |
| 1465 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1466 |
return -1; |
| 1467 |
} |
| 1468 |
|
| 1469 |
ret = ovpn_read_cipher(argv[4], &ovpn); |
| 1470 |
if (ret < 0) |
| 1471 |
return ret; |
| 1472 |
|
| 1473 |
ret = ovpn_read_key_direction(argv[5], &ovpn); |
| 1474 |
if (ret < 0) |
| 1475 |
return ret; |
| 1476 |
|
| 1477 |
ret = ovpn_read_key(argv[6], &ovpn); |
| 1478 |
if (ret) |
| 1479 |
return ret; |
| 1480 |
|
| 1481 |
ret = ovpn_new_key(&ovpn); |
| 1482 |
if (ret < 0) { |
| 1483 |
fprintf(stderr, "cannot set key\n"); |
| 1484 |
return ret; |
| 1485 |
} |
| 1486 |
} else if (!strcmp(argv[2], "del_key")) { |
| 1487 |
if (argc < 3) { |
| 1488 |
usage(argv[0]); |
| 1489 |
return -1; |
| 1490 |
} |
| 1491 |
|
| 1492 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1493 |
if (errno == ERANGE) { |
| 1494 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1495 |
return -1; |
| 1496 |
} |
| 1497 |
|
| 1498 |
argv++; |
| 1499 |
argc--; |
| 1500 |
|
| 1501 |
ret = ovpn_del_key(&ovpn); |
| 1502 |
if (ret < 0) { |
| 1503 |
fprintf(stderr, "cannot delete key\n"); |
| 1504 |
return ret; |
| 1505 |
} |
| 1506 |
} else if (!strcmp(argv[2], "swap_keys")) { |
| 1507 |
if (argc < 3) { |
| 1508 |
usage(argv[0]); |
| 1509 |
return -1; |
| 1510 |
} |
| 1511 |
|
| 1512 |
ovpn.peer_id = strtoul(argv[3], NULL, 10); |
| 1513 |
if (errno == ERANGE) { |
| 1514 |
fprintf(stderr, "peer ID value out of range\n"); |
| 1515 |
return -1; |
| 1516 |
} |
| 1517 |
|
| 1518 |
argv++; |
| 1519 |
argc--; |
| 1520 |
|
| 1521 |
ret = ovpn_swap_keys(&ovpn); |
| 1522 |
if (ret < 0) { |
| 1523 |
fprintf(stderr, "cannot swap keys\n"); |
| 1524 |
return ret; |
| 1525 |
} |
| 1526 |
} else if (!strcmp(argv[2], "listen_mcast")) { |
| 1527 |
ovpn_listen_mcast(); |
| 1528 |
} else { |
| 1529 |
usage(argv[0]); |
| 1530 |
return -1; |
| 1531 |
} |
| 1532 |
|
| 1533 |
return ret; |
| 1534 |
} |