1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/*
* Ouroboros - Copyright (C) 2016 - 2023
*
* The API to instruct IPCPs
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#define _POSIX_C_SOURCE 200112L
#include "config.h"
#define OUROBOROS_PREFIX "irmd/ipcp"
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/utils.h>
#include <ouroboros/sockets.h>
#include "ipcp.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <spawn.h>
ipcp_msg_t * send_recv_ipcp_msg(pid_t pid,
ipcp_msg_t * msg)
{
int sockfd = 0;
uint8_t buf[SOCK_BUF_SIZE];
char * sock_path = NULL;
ssize_t len;
ipcp_msg_t * recv_msg = NULL;
struct timeval tv;
if (kill(pid, 0) < 0)
return NULL;
sock_path = ipcp_sock_path(pid);
if (sock_path == NULL)
return NULL;
sockfd = client_socket_open(sock_path);
if (sockfd < 0) {
free(sock_path);
return NULL;
}
free(sock_path);
len = ipcp_msg__get_packed_size(msg);
if (len == 0) {
close(sockfd);
return NULL;
}
switch (msg->code) {
case IPCP_MSG_CODE__IPCP_BOOTSTRAP:
tv.tv_sec = BOOTSTRAP_TIMEOUT / 1000;
tv.tv_usec = (BOOTSTRAP_TIMEOUT % 1000) * 1000;
break;
case IPCP_MSG_CODE__IPCP_ENROLL:
tv.tv_sec = ENROLL_TIMEOUT / 1000;
tv.tv_usec = (ENROLL_TIMEOUT % 1000) * 1000;
break;
case IPCP_MSG_CODE__IPCP_REG:
tv.tv_sec = REG_TIMEOUT / 1000;
tv.tv_usec = (REG_TIMEOUT % 1000) * 1000;
break;
case IPCP_MSG_CODE__IPCP_QUERY:
tv.tv_sec = QUERY_TIMEOUT / 1000;
tv.tv_usec = (QUERY_TIMEOUT % 1000) * 1000;
break;
case IPCP_MSG_CODE__IPCP_CONNECT:
tv.tv_sec = CONNECT_TIMEOUT / 1000;
tv.tv_usec = (CONNECT_TIMEOUT % 1000) * 1000;
break;
default:
tv.tv_sec = SOCKET_TIMEOUT / 1000;
tv.tv_usec = (SOCKET_TIMEOUT % 1000) * 1000;
break;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,
(void *) &tv, sizeof(tv)))
log_warn("Failed to set timeout on socket.");
pthread_cleanup_push(__cleanup_close_ptr, (void *) &sockfd);
ipcp_msg__pack(msg, buf);
if (write(sockfd, buf, len) != -1)
len = read(sockfd, buf, SOCK_BUF_SIZE);
if (len > 0)
recv_msg = ipcp_msg__unpack(NULL, len, buf);
pthread_cleanup_pop(true);
return recv_msg;
}
pid_t ipcp_create(const char * name,
enum ipcp_type ipcp_type)
{
pid_t pid = -1;
char * ipcp_dir = "/sbin/";
char * exec_name = NULL;
char irmd_pid[10];
char full_name[256];
char * argv[5];
switch(ipcp_type) {
case IPCP_UNICAST:
exec_name = IPCP_UNICAST_EXEC;
break;
case IPCP_BROADCAST:
exec_name = IPCP_BROADCAST_EXEC;
break;
case IPCP_UDP:
exec_name = IPCP_UDP_EXEC;
break;
case IPCP_ETH_LLC:
exec_name = IPCP_ETH_LLC_EXEC;
break;
case IPCP_ETH_DIX:
exec_name = IPCP_ETH_DIX_EXEC;
break;
case IPCP_LOCAL:
exec_name = IPCP_LOCAL_EXEC;
break;
default:
return -1;
}
if (strlen(exec_name) == 0) {
log_err("IPCP type not installed.");
return -1;
}
sprintf(irmd_pid, "%u", getpid());
strcpy(full_name, INSTALL_PREFIX);
strcat(full_name, ipcp_dir);
strcat(full_name, exec_name);
/* log_file to be placed at the end */
argv[0] = full_name;
argv[1] = irmd_pid;
argv[2] = (char *) name;
if (log_syslog)
argv[3] = "1";
else
argv[3] = NULL;
argv[4] = NULL;
if (posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL) != 0) {
log_err("Failed to spawn new process");
return -1;
}
return pid;
}
int ipcp_destroy(pid_t pid)
{
if (kill(pid, SIGTERM)) {
log_err("Failed to destroy IPCP");
return -1;
}
return 0;
}
int ipcp_bootstrap(pid_t pid,
struct ipcp_config * conf,
struct layer_info * info)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
if (conf == NULL)
return -EINVAL;
msg.code = IPCP_MSG_CODE__IPCP_BOOTSTRAP;
msg.conf = ipcp_config_s_to_msg(conf);
recv_msg = send_recv_ipcp_msg(pid, &msg);
ipcp_config_msg__free_unpacked(msg.conf, NULL);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
if (ret != 0) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
if (recv_msg->layer_info == NULL) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
info->dir_hash_algo = recv_msg->layer_info->dir_hash_algo;
strcpy(info->layer_name, recv_msg->layer_info->layer_name);
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_enroll(pid_t pid,
const char * dst,
struct layer_info * info)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
if (dst == NULL)
return -EINVAL;
msg.code = IPCP_MSG_CODE__IPCP_ENROLL;
msg.dst = (char *) dst;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
if (ret != 0) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
if (recv_msg->layer_info == NULL) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
info->dir_hash_algo = recv_msg->layer_info->dir_hash_algo;
strcpy(info->layer_name, recv_msg->layer_info->layer_name);
ipcp_msg__free_unpacked(recv_msg, NULL);
return 0;
}
int ipcp_connect(pid_t pid,
const char * dst,
const char * component,
qosspec_t qs)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
int ret = -1;
ipcp_msg_t * recv_msg;
msg.code = IPCP_MSG_CODE__IPCP_CONNECT;
msg.dst = (char *) dst;
msg.comp = (char *) component;
msg.has_pid = true;
msg.pid = pid;
msg.qosspec = qos_spec_s_to_msg(&qs);
recv_msg = send_recv_ipcp_msg(pid, &msg);
free(msg.qosspec);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_disconnect(pid_t pid,
const char * dst,
const char * component)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
msg.code = IPCP_MSG_CODE__IPCP_DISCONNECT;
msg.dst = (char *) dst;
msg.comp = (char *) component;
msg.has_pid = true;
msg.pid = pid;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_reg(pid_t pid,
const uint8_t * hash,
size_t len)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
assert(hash);
msg.code = IPCP_MSG_CODE__IPCP_REG;
msg.has_hash = true;
msg.hash.len = len;
msg.hash.data = (uint8_t *)hash;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_unreg(pid_t pid,
const uint8_t * hash,
size_t len)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
msg.code = IPCP_MSG_CODE__IPCP_UNREG;
msg.has_hash = true;
msg.hash.len = len;
msg.hash.data = (uint8_t *) hash;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_query(pid_t pid,
const uint8_t * hash,
size_t len)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
msg.code = IPCP_MSG_CODE__IPCP_QUERY;
msg.has_hash = true;
msg.hash.len = len;
msg.hash.data = (uint8_t *) hash;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
static int __ipcp_flow_alloc(pid_t pid,
int flow_id,
pid_t n_pid,
const uint8_t * dst,
size_t len,
qosspec_t qs,
bool join,
const buffer_t data)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
assert(dst);
msg.code = join ? IPCP_MSG_CODE__IPCP_FLOW_JOIN
: IPCP_MSG_CODE__IPCP_FLOW_ALLOC;
msg.has_flow_id = true;
msg.flow_id = flow_id;
msg.has_pid = true;
msg.pid = n_pid;
msg.has_hash = true;
msg.hash.len = len;
msg.hash.data = (uint8_t *) dst;
msg.qosspec = qos_spec_s_to_msg(&qs);;
msg.has_pk = true;
msg.pk.data = data.data;
msg.pk.len = data.len;
recv_msg = send_recv_ipcp_msg(pid, &msg);
free(msg.qosspec);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_flow_alloc(pid_t pid,
int flow_id,
pid_t n_pid,
const uint8_t * dst,
size_t len,
qosspec_t qs,
const buffer_t data)
{
return __ipcp_flow_alloc(pid, flow_id, n_pid, dst,
len, qs, false, data);
}
int ipcp_flow_join(pid_t pid,
int flow_id,
pid_t n_pid,
const uint8_t * dst,
size_t len,
qosspec_t qs)
{
buffer_t data = {NULL, 0};
return __ipcp_flow_alloc(pid, flow_id, n_pid, dst,
len, qs, true, data);
}
int ipcp_flow_alloc_resp(pid_t pid,
int flow_id,
pid_t n_pid,
int response,
const buffer_t data)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP;
msg.has_flow_id = true;
msg.flow_id = flow_id;
msg.has_pid = true;
msg.pid = n_pid;
msg.has_response = true;
msg.response = response;
msg.has_pk = true;
msg.pk.data = data.data;
msg.pk.len = data.len;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return -EIPCP;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return -EIPCP;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
int ipcp_flow_dealloc(pid_t pid,
int flow_id,
time_t timeo)
{
ipcp_msg_t msg = IPCP_MSG__INIT;
ipcp_msg_t * recv_msg = NULL;
int ret = -1;
msg.code = IPCP_MSG_CODE__IPCP_FLOW_DEALLOC;
msg.has_flow_id = true;
msg.flow_id = flow_id;
msg.has_timeo_sec = true;
msg.timeo_sec = timeo;
recv_msg = send_recv_ipcp_msg(pid, &msg);
if (recv_msg == NULL)
return 0;
if (!recv_msg->has_result) {
ipcp_msg__free_unpacked(recv_msg, NULL);
return 0;
}
ret = recv_msg->result;
ipcp_msg__free_unpacked(recv_msg, NULL);
return ret;
}
|