summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/main.c
blob: 0b8a26ab785bb9544b25eb32b91da10234a3f2ba (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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * Normal IPC Process
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@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/endian.h>
#include <ouroboros/logs.h>
#include <ouroboros/ipcp-dev.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/irm.h>
#include <ouroboros/rib.h>
#include <ouroboros/hash.h>
#include <ouroboros/errno.h>

#include "addr_auth.h"
#include "connmgr.h"
#include "dir.h"
#include "enroll.h"
#include "fa.h"
#include "dt.h"
#include "ipcp.h"
#include "ribconfig.h"
#include "ribmgr.h"

#include <stdbool.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>

#define THIS_TYPE IPCP_NORMAL

static int boot_components(void)
{
        char buf[256];
        ssize_t len;
        enum pol_addr_auth pa;
        char path[RIB_MAX_PATH_LEN + 1];

        len = rib_read(BOOT_PATH "/general/dif_name", buf, 256);
        if (len < 0) {
                log_err("Failed to read DIF name: %zd.", len);
                return -1;
        }

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

        len = rib_read(BOOT_PATH "/general/dir_hash_algo",
                       &ipcpi.dir_hash_algo, sizeof(ipcpi.dir_hash_algo));
        if (len < 0) {
                log_err("Failed to read hash length: %zd.", len);
                goto fail_addr_auth;
        }

        ipcpi.dir_hash_algo = ntoh32(ipcpi.dir_hash_algo);

        assert(ipcp_dir_hash_len() != 0);

        if (rib_add(MEMBERS_PATH, ipcpi.name)) {
                log_err("Failed to add name to " MEMBERS_PATH);
                goto fail_addr_auth;
        }

        log_dbg("Starting components.");

        if (rib_read(BOOT_PATH "/dt/addr_auth/type", &pa, sizeof(pa))
            != sizeof(pa)) {
                log_err("Failed to read policy for address authority.");
                goto fail_addr_auth;
        }

        if (addr_auth_init(pa)) {
                log_err("Failed to init address authority.");
                goto fail_addr_auth;
        }

        ipcpi.dt_addr = addr_auth_address();
        if (ipcpi.dt_addr == 0) {
                log_err("Failed to get a valid address.");
                goto fail_dir;
        }

        path[0] = '\0';
        rib_path_append(rib_path_append(path, MEMBERS_NAME), ipcpi.name);
        if (rib_write(path, &ipcpi.dt_addr, sizeof(&ipcpi.dt_addr))) {
                log_err("Failed to write address to member object.");
                goto fail_dir;
        }

        log_dbg("IPCP got address %" PRIu64 ".", ipcpi.dt_addr);

        log_dbg("Starting ribmgr.");

        if (dir_init()) {
                log_err("Failed to initialize directory.");
                goto fail_dir;
        }

        if (ribmgr_init()) {
                log_err("Failed to initialize RIB manager.");
                goto fail_ribmgr;
        }

        log_dbg("Ribmgr started.");

        if (fa_init()) {
                log_err("Failed to initialize flow allocator ae.");
                goto fail_fa;
        }

        if (dt_init()) {
                log_err("Failed to initialize data transfer ae.");
                goto fail_dt;
        }

        if (fa_start()) {
                log_err("Failed to start flow allocator.");
                goto fail_fa_start;
        }

        if (dt_start()) {
                log_err("Failed to start data transfer ae.");
                goto fail_dt_start;
        }

        if (enroll_start()) {
                log_err("Failed to start enroll.");
                goto fail_enroll_start;
        }

        ipcp_set_state(IPCP_OPERATIONAL);

        if (connmgr_start()) {
                log_err("Failed to start AP connection manager.");
                goto fail_connmgr_start;
        }

        return 0;

 fail_connmgr_start:
        ipcp_set_state(IPCP_INIT);
        enroll_stop();
 fail_enroll_start:
        dt_stop();
 fail_dt_start:
        fa_stop();
 fail_fa_start:
        dt_fini();
 fail_dt:
        fa_fini();
 fail_fa:
        ribmgr_fini();
 fail_ribmgr:
        dir_fini();
 fail_dir:
        addr_auth_fini();
 fail_addr_auth:
        free(ipcpi.dif_name);

        return -1;
}

void shutdown_components(void)
{
        connmgr_stop();

        enroll_stop();

        dt_stop();

        fa_stop();

        dt_fini();

        fa_fini();

        ribmgr_fini();

        dir_fini();

        addr_auth_fini();

        free(ipcpi.dif_name);
}

static int normal_ipcp_enroll(const char *      dst,
                              struct dif_info * info)
{
        if (rib_add(RIB_ROOT, MEMBERS_NAME)) {
                log_err("Failed to create members.");
                return -1;
        }

        /* Get boot state from peer */
        if (enroll_boot(dst)) {
                log_err("Failed to boot IPCP components.");
                return -1;
        }

        if (boot_components()) {
                log_err("Failed to boot IPCP components.");
                return -1;
        }

        log_dbg("Enrolled with " HASH_FMT, HASH_VAL(dst));

        info->dir_hash_algo = ipcpi.dir_hash_algo;

        strcpy(info->dif_name, ipcpi.dif_name);

        return 0;
}

const struct ros {
        char * parent;
        char * child;
} ros[] = {
        /* BOOT INFO */
        {RIB_ROOT, BOOT_NAME},
        /* OTHER RIB STRUCTURES */
        {RIB_ROOT, MEMBERS_NAME},

        /* GENERAL IPCP INFO */
        {BOOT_PATH, "general"},

        {BOOT_PATH "/general", "dif_name"},
        {BOOT_PATH "/general", "dir_hash_algo"},

        /* 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", "fd_size"},
        {BOOT_PATH "/dt/const", "has_ttl"},
        {BOOT_PATH "/dt", "addr_auth"},
        {BOOT_PATH "/dt/addr_auth", "type"},
        {BOOT_PATH "/dt", "routing"},
        {BOOT_PATH "/dt/routing", "type"},

        /* RIB MGR COMPONENT */
        {BOOT_PATH, "rm"},
        {BOOT_PATH "/rm","gam"},
        {BOOT_PATH "/rm/gam", "type"},
        {BOOT_PATH "/rm/gam", "cacep"},

        {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(const struct ipcp_config * conf)
{
        uint32_t hash_algo;

        assert(conf);
        assert(conf->type == THIS_TYPE);

        hash_algo = hton32((uint32_t) conf->dif_info.dir_hash_algo);

        assert(ntoh32(hash_algo) != 0);

        if (normal_rib_init()) {
                log_err("Failed to write initial structure to the RIB.");
                return -1;
        }

        if (rib_write(BOOT_PATH "/general/dif_name",
                      conf->dif_info.dif_name,
                      strlen(conf->dif_info.dif_name) + 1) ||
            rib_write(BOOT_PATH "/general/dir_hash_algo",
                      &hash_algo,
                      sizeof(hash_algo)) ||
            rib_write(BOOT_PATH "/dt/const/addr_size",
                      &conf->addr_size,
                      sizeof(conf->addr_size)) ||
            rib_write(BOOT_PATH "/dt/const/fd_size",
                      &conf->fd_size,
                      sizeof(conf->fd_size)) ||
            rib_write(BOOT_PATH "/dt/const/has_ttl",
                      &conf->has_ttl,
                      sizeof(conf->has_ttl)) ||
            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 "/dt/addr_auth/type",
                      &conf->addr_auth_type,
                      sizeof(conf->addr_auth_type)) ||
            rib_write(BOOT_PATH "/dt/routing/type",
                      &conf->routing_type,
                      sizeof(conf->routing_type))) {
                log_err("Failed to write boot info to RIB.");
                return -1;
        }

        if (boot_components()) {
                log_err("Failed to boot IPCP components.");
                return -1;
        }

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

        return 0;
}

static struct ipcp_ops normal_ops = {
        .ipcp_bootstrap       = normal_ipcp_bootstrap,
        .ipcp_enroll          = normal_ipcp_enroll,
        .ipcp_reg             = dir_reg,
        .ipcp_unreg           = dir_unreg,
        .ipcp_query           = dir_query,
        .ipcp_flow_alloc      = fa_alloc,
        .ipcp_flow_alloc_resp = fa_alloc_resp,
        .ipcp_flow_dealloc    = fa_dealloc
};

int main(int    argc,
         char * argv[])
{
        if (ipcp_init(argc, argv, THIS_TYPE, &normal_ops) < 0) {
                ipcp_create_r(getpid(), -1);
                exit(EXIT_FAILURE);
        }

        if (irm_bind_api(getpid(), ipcpi.name)) {
                log_err("Failed to bind AP name.");
                ipcp_create_r(getpid(), -1);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }

        if (rib_init()) {
                log_err("Failed to initialize RIB.");
                ipcp_create_r(getpid(), -1);
                irm_unbind_api(getpid(), ipcpi.name);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }

        if (connmgr_init()) {
                log_err("Failed to initialize connection manager.");
                ipcp_create_r(getpid(), -1);
                rib_fini();
                irm_unbind_api(getpid(), ipcpi.name);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }

        if (enroll_init()) {
                log_err("Failed to initialize enroll component.");
                ipcp_create_r(getpid(), -1);
                connmgr_fini();
                rib_fini();
                irm_unbind_api(getpid(), ipcpi.name);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }


        if (ipcp_boot() < 0) {
                log_err("Failed to boot IPCP.");
                ipcp_create_r(getpid(), -1);
                enroll_fini();
                connmgr_fini();
                rib_fini();
                irm_unbind_api(getpid(), ipcpi.name);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }

        if (ipcp_create_r(getpid(), 0)) {
                log_err("Failed to notify IRMd we are initialized.");
                ipcp_set_state(IPCP_NULL);
                ipcp_shutdown();
                enroll_fini();
                connmgr_fini();
                rib_fini();
                irm_unbind_api(getpid(), ipcpi.name);
                ipcp_fini();
                exit(EXIT_FAILURE);
        }

        ipcp_shutdown();

        if (ipcp_get_state() == IPCP_SHUTDOWN)
                shutdown_components();

        enroll_fini();

        connmgr_fini();

        rib_fini();

        irm_unbind_api(getpid(), ipcpi.name);

        ipcp_fini();

        exit(EXIT_SUCCESS);
}