summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/main.c
blob: b9cc6e57ed18a0175e51c79ab3c306451a02c1bb (plain)
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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * Normal IPC Process
 *
 *    Sander Vrijders   <sander.vrijders@intec.ugent.be>
 *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define OUROBOROS_PREFIX "normal-ipcp"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/dev.h>
#include <ouroboros/ipcp-dev.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/irm.h>
#include <ouroboros/rib.h>
#include <ouroboros/irm_config.h>

#include "addr_auth.h"
#include "ae.h"
#include "dir.h"
#include "enroll.h"
#include "fmgr.h"
#include "frct.h"
#include "ipcp.h"
#include "ribmgr.h"

#include <stdbool.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#define DLR          "/"
#define DIF_PATH     DLR DIF_NAME
#define BOOT_PATH    DLR BOOT_NAME
#define MEMBERS_PATH DLR MEMBERS_NAME

#define THIS_TYPE    IPCP_NORMAL

struct {
        pthread_t acceptor;
        struct addr_auth * auth;
} normal;

void ipcp_sig_handler(int         sig,
                      siginfo_t * info,
                      void *      c)
{
        (void) c;

        switch(sig) {
        case SIGINT:
        case SIGTERM:
        case SIGHUP:
                if (info->si_pid == ipcpi.irmd_api) {
                        pthread_rwlock_wrlock(&ipcpi.state_lock);

                        if (ipcp_get_state() == IPCP_INIT)
                                ipcp_set_state(IPCP_NULL);

                        if (ipcp_get_state() == IPCP_OPERATIONAL)
                                ipcp_set_state(IPCP_SHUTDOWN);

                        pthread_rwlock_unlock(&ipcpi.state_lock);
                }
        default:
                return;
        }
}

static void * flow_acceptor(void * o)
{
        int       fd;
        char *    ae_name;
        qosspec_t qs;

        (void) o;

        while (true) {
                pthread_rwlock_rdlock(&ipcpi.state_lock);

                if (ipcp_get_state() != IPCP_OPERATIONAL) {
                        pthread_rwlock_unlock(&ipcpi.state_lock);
                        LOG_INFO("Shutting down flow acceptor.");
                        return 0;
                }

                pthread_rwlock_unlock(&ipcpi.state_lock);

                fd = flow_accept(&ae_name, &qs);
                if (fd < 0) {
                        LOG_WARN("Flow accept failed.");
                        continue;
                }

                LOG_DBG("New flow allocation request for AE %s.", ae_name);

                if (strcmp(ae_name, ENROLL_AE) == 0) {
                        enroll_handle(fd);
                } else if (strcmp(ae_name, MGMT_AE) == 0) {
                        ribmgr_flow_arr(fd, qs);
                } else if (strcmp(ae_name, DT_AE) == 0) {
                        fmgr_nm1_flow_arr(fd, qs);
                } else {
                        LOG_DBG("Flow allocation request for unknown AE %s.",
                                ae_name);
                        if (flow_alloc_resp(fd, -1))
                                LOG_WARN("Failed to reply to flow allocation.");
                        flow_dealloc(fd);
                }

                free(ae_name);
        }

        return (void *) 0;
}

/*
 * Boots the IPCP off information in the rib.
 * Common function after bootstrap or enroll.
 * Call under ipcpi.state_lock
 */
static int boot_components(void)
{
        char buf[256];
        ssize_t len;
        enum pol_addr_auth pa;

        len = rib_read(DIF_PATH, &buf, 256);
        if (len < 0) {
                LOG_ERR("Failed to read DIF name: %ld.", len);
                return -1;
        }

        ipcpi.data->dif_name = strdup(buf);
        if (ipcpi.data->dif_name == NULL) {
                LOG_ERR("Failed to set DIF name.");
                return -1;
        }

        if (rib_add(MEMBERS_PATH, ipcpi.name)) {
                LOG_WARN("Failed to add name to " MEMBERS_PATH);
                return -1;
        }

        LOG_DBG("Starting components.");

        if (rib_read(BOOT_PATH "/addr_auth/type", &pa, sizeof(pa))
            != sizeof(pa)) {
                LOG_ERR("Failed to read policy for address authority.");
                return -1;
        }

        normal.auth = addr_auth_create(pa);
        if (normal.auth == NULL) {
                LOG_ERR("Failed to init address authority.");
                return -1;
        }

        ipcpi.address = normal.auth->address();
        if (ipcpi.address == 0) {
                LOG_ERR("Failed to get a valid address.");
                addr_auth_destroy(normal.auth);
                return -1;
        }

        LOG_DBG("IPCP got address %lu.", ipcpi.address);

        LOG_DBG("Starting ribmgr.");

        if (ribmgr_init()) {
                LOG_ERR("Failed to initialize RIB manager.");
                addr_auth_destroy(normal.auth);
                return -1;
        }

        if (dir_init()) {
                LOG_ERR("Failed to initialize directory.");
                ribmgr_fini();
                addr_auth_destroy(normal.auth);
                return -1;
        }

        LOG_DBG("Ribmgr started.");

        if (fmgr_init()) {
                dir_fini();
                ribmgr_fini();
                addr_auth_destroy(normal.auth);
                LOG_ERR("Failed to start flow manager.");
                return -1;
        }

        if (frct_init()) {
                fmgr_fini();
                dir_fini();
                ribmgr_fini();
                addr_auth_destroy(normal.auth);
                LOG_ERR("Failed to initialize FRCT.");
                return -1;
        }

        ipcp_set_state(IPCP_OPERATIONAL);

        if (pthread_create(&normal.acceptor, NULL, flow_acceptor, NULL)) {
                ipcp_set_state(IPCP_INIT);
                fmgr_fini();
                dir_fini();
                ribmgr_fini();
                addr_auth_destroy(normal.auth);
                LOG_ERR("Failed to create acceptor thread.");
                return -1;
        }

        return 0;
}

static int normal_ipcp_enroll(char * dst_name)
{
        pthread_rwlock_wrlock(&ipcpi.state_lock);

        if (ipcp_get_state() != IPCP_INIT) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("IPCP in wrong state.");
                return -1; /* -ENOTINIT */
        }

        if (rib_add(RIB_ROOT, MEMBERS_NAME)) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("Failed to create members.");
                return -1;
        }

        /* Get boot state from peer */
        if (enroll_boot(dst_name)) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("Failed to boot IPCP components.");
                return -1;
        }

        if (boot_components()) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("Failed to boot IPCP components.");
                return -1;
        }

        pthread_rwlock_unlock(&ipcpi.state_lock);

        LOG_DBG("Enrolled with %s.", dst_name);

        return 0;
}

const struct ros {
        char * parent;
        char * child;
} ros[] = {
        /* GENERAL IPCP INFO */
        {RIB_ROOT, DIF_NAME},
        /* BOOT INFO */
        {RIB_ROOT, BOOT_NAME},
        /* OTHER RIB STRUCTURES */
        {RIB_ROOT, MEMBERS_NAME},
        /* DT COMPONENT */
        {BOOT_PATH, "dt"},

        {BOOT_PATH "/dt", "gam"},
        {BOOT_PATH "/dt/gam", "type"},
        {BOOT_PATH "/dt/gam", "cacep"},
        {BOOT_PATH "/dt", "const"},
        {BOOT_PATH "/dt/const", "addr_size"},
        {BOOT_PATH "/dt/const", "cep_id_size"},
        {BOOT_PATH "/dt/const", "pdu_length_size"},
        {BOOT_PATH "/dt/const", "seqno_size"},
        {BOOT_PATH "/dt/const", "has_ttl"},
        {BOOT_PATH "/dt/const", "has_chk"},
        {BOOT_PATH "/dt/const", "min_pdu_size"},
        {BOOT_PATH "/dt/const", "max_pdu_size"},

        /* RIB MGR COMPONENT */
        {BOOT_PATH, "rm"},

        {BOOT_PATH "/rm","gam"},
        {BOOT_PATH "/rm/gam", "type"},
        {BOOT_PATH "/rm/gam", "cacep"},

        /* ADDR AUTH COMPONENT */
        {BOOT_PATH, "addr_auth"},
        {BOOT_PATH "/addr_auth", "type"},
        {NULL, NULL}
};

int normal_rib_init(void)
{
        struct ros * r;

        for (r = (struct ros *) ros; r->parent; ++r) {
                if (rib_add(r->parent, r->child)) {
                        LOG_ERR("Failed to create %s/%s",
                                r->parent, r->child);
                        return -1;
                }
        }

        return 0;
}

static int normal_ipcp_bootstrap(struct dif_config * conf)
{
        /* FIXME: get CACEP policies from conf */
        enum pol_cacep pol = NO_AUTH;

        (void) pol;

        if (conf == NULL || conf->type != THIS_TYPE) {
                LOG_ERR("Bad DIF configuration.");
                return -EINVAL;
        }

        pthread_rwlock_wrlock(&ipcpi.state_lock);

        if (ipcp_get_state() != IPCP_INIT) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("IPCP in wrong state.");
                return -1; /* -ENOTINIT */
        }

        if (normal_rib_init()) {
                pthread_rwlock_unlock(&ipcpi.state_lock);
                LOG_ERR("Failed to write initial structure to the RIB.");
                return -1;
        }

        if (rib_write(DIF_PATH,
                      conf->dif_name,
                      strlen(conf->dif_name) + 1) ||
            rib_write(BOOT_PATH "/dt/const/addr_size",
                      &conf->addr_size,
                      sizeof(conf->addr_size)) ||
            rib_write(BOOT_PATH "/dt/const/cep_id_size",
                      &conf->cep_id_size,
                      sizeof(conf->cep_id_size)) ||
            rib_write(BOOT_PATH "/dt/const/seqno_size",
                      &conf->seqno_size,
                      sizeof(conf->seqno_size)) ||
            rib_write(BOOT_PATH "/dt/const/has_ttl",
                      &conf->has_ttl,
                      sizeof(conf->has_ttl)) ||
            rib_write(BOOT_PATH "/dt/const/has_chk",
                      &conf->has_chk,
                      sizeof(conf->has_chk)) ||
            rib_write(BOOT_PATH "/dt/const/min_pdu_size",
                      &conf->min_pdu_size,
                      sizeof(conf->min_pdu_size)) ||
            rib_write(BOOT_PATH "/dt/const/max_pdu_size",
                      &conf->max_pdu_size,
                      sizeof(conf->max_pdu_size)) ||
            rib_write(BOOT_PATH "/dt/gam/type",
                      &conf->dt_gam_type,
                      sizeof(conf->dt_gam_type)) ||
            rib_write(BOOT_PATH "/rm/gam/type",
                      &conf->rm_gam_type,
                      sizeof(conf->rm_gam_type)) ||
            rib_write(BOOT_PATH "/rm/gam/cacep",
                      &pol,
                      sizeof(pol)) ||
            rib_write(BOOT_PATH "/dt/gam/cacep",
                      &pol,
                      sizeof(pol)) ||
            rib_write(BOOT_PATH "/addr_auth/type",
                      &conf->addr_auth_type,
                      sizeof(conf->addr_auth_type))) {
                LOG_ERR("Failed to write boot info to RIB.");
                pthread_rwlock_unlock(&ipcpi.state_lock);
                return -1;
        }

        if (boot_components()) {
                LOG_ERR("Failed to boot IPCP components.");
                pthread_rwlock_unlock(&ipcpi.state_lock);
                return -1;
        }

        pthread_rwlock_unlock(&ipcpi.state_lock);

        LOG_DBG("Bootstrapped in DIF %s.", conf->dif_name);

        return 0;
}

static struct ipcp_ops normal_ops = {
        .ipcp_bootstrap       = normal_ipcp_bootstrap,
        .ipcp_enroll          = normal_ipcp_enroll,
        .ipcp_name_reg        = dir_name_reg,
        .ipcp_name_unreg      = dir_name_unreg,
        .ipcp_name_query      = dir_name_query,
        .ipcp_flow_alloc      = NULL, /* fmgr_np1_alloc, */
        .ipcp_flow_alloc_resp = NULL, /* fmgr_np1_alloc_resp, */
        .ipcp_flow_dealloc    = NULL, /* fmgr_np1_dealloc */
};

int main(int    argc,
         char * argv[])
{
        struct sigaction sig_act;
        sigset_t         sigset;

        if (ap_init(argv[0])) {
                LOG_ERR("Failed to init AP");
                exit(EXIT_FAILURE);
        }

        sigemptyset(&sigset);
        sigaddset(&sigset, SIGINT);
        sigaddset(&sigset, SIGQUIT);
        sigaddset(&sigset, SIGHUP);
        sigaddset(&sigset, SIGPIPE);

        if (ipcp_parse_arg(argc, argv)) {
                LOG_ERR("Failed to parse arguments.");
                exit(EXIT_FAILURE);
        }

        if (irm_bind_api(getpid(), ipcpi.name)) {
                LOG_ERR("Failed to bind AP name.");
                close_logfile();
                exit(EXIT_FAILURE);
        }

        /* init sig_act */
        memset(&sig_act, 0, sizeof(sig_act));

        /* install signal traps */
        sig_act.sa_sigaction = &ipcp_sig_handler;
        sig_act.sa_flags     = SA_SIGINFO;

        sigaction(SIGINT,  &sig_act, NULL);
        sigaction(SIGTERM, &sig_act, NULL);
        sigaction(SIGHUP,  &sig_act, NULL);
        sigaction(SIGPIPE, &sig_act, NULL);

        if (rib_init()) {
                LOG_ERR("Failed to initialize RIB.");
                close_logfile();
                exit(EXIT_FAILURE);
        }

        if (ipcp_init(THIS_TYPE, &normal_ops) < 0) {
                LOG_ERR("Failed to create instance.");
                rib_fini();
                close_logfile();
                exit(EXIT_FAILURE);
        }

        pthread_sigmask(SIG_BLOCK, &sigset, NULL);

        if (ipcp_boot() < 0) {
                LOG_ERR("Failed to boot IPCP.");
                ipcp_fini();
                rib_fini();
                close_logfile();
                exit(EXIT_FAILURE);
        }

        pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);

        if (ipcp_create_r(getpid())) {
                LOG_ERR("Failed to notify IRMd we are initialized.");
                ipcp_fini();
                rib_fini();
                close_logfile();
                exit(EXIT_FAILURE);
        }

        ipcp_shutdown();

        if (ipcp_get_state() == IPCP_SHUTDOWN) {
                pthread_cancel(normal.acceptor);
                pthread_join(normal.acceptor, NULL);
        }

        ribmgr_fini();

        dir_fini();

        addr_auth_destroy(normal.auth);

        rib_fini();

        ipcp_fini();

        close_logfile();

        ap_fini();

        exit(EXIT_SUCCESS);
}