summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/pol/flat.c
blob: d982f5aca181f1279c6f8c44794fa5f97500c4cb (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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * Policy for flat addresses in a distributed way
 *
 *    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 "flat-addr-auth"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/rib.h>
#include <ouroboros/utils.h>

#include "ipcp.h"
#include "ribconfig.h"

#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>

#define NAME_LEN 8
#define REC_DIF_SIZE 10000

/* convert 32 bit addr to a hex string */
static void addr_name(char *   name,
                      uint32_t addr)
{
        sprintf(name, "%8x", (uint32_t) (addr));
}

static int addr_taken(char *  name,
                      char ** members,
                      size_t  len)
{
        size_t i;
        char path[RIB_MAX_PATH_LEN + 1];

        size_t reset;
        strcpy(path, "/" MEMBERS_NAME);

        reset = strlen(path);

        for (i = 0; i < len; ++i) {
                ssize_t j;
                ssize_t c;
                char ** addrs;
                rib_path_append(path, members[i]);
                c = rib_children(path, &addrs);
                for (j = 0; j < c; ++j)
                        if (strcmp(addrs[j], name) == 0) {
                                freepp(char, addrs, c);
                                return 1;
                        }
                freepp(char, addrs, c);
                path[reset] = '\0';
        }

        return 0;
}

#define INVALID_ADDRESS 0

uint64_t flat_address(void)
{
        struct timespec t;

        char path[RIB_MAX_PATH_LEN];
        char name[NAME_LEN + 1];
        uint32_t addr;
        uint8_t addr_size;

        char ** members;
        ssize_t n_members;

        strcpy(path, "/" MEMBERS_NAME);

        if (!rib_has(path)) {
                log_err("Could not read members from RIB.");
                return INVALID_ADDRESS;
        }

        if (rib_read("/" BOOT_NAME "/dt/const/addr_size",
                     &addr_size, sizeof(addr_size)) != sizeof(addr_size)) {
                log_err("Failed to read address size.");
                return INVALID_ADDRESS;
        }

        if (addr_size != 4) {
                log_err("Flat address policy mandates 4 byte addresses.");
                return INVALID_ADDRESS;
        }

        n_members = rib_children(path, &members);
        if (n_members > REC_DIF_SIZE)
                log_warn("DIF exceeding recommended size for flat addresses.");

        rib_path_append(path, ipcpi.name);

        if (!rib_has(path)) {
                log_err("This ipcp is not a member.");
                freepp(char, members, n_members);
                return INVALID_ADDRESS;
        }

        clock_gettime(CLOCK_REALTIME, &t);
        srand(t.tv_nsec);

        assert(n_members > 0);

        do {
                addr = (rand() % (RAND_MAX - 1) + 1) & 0xFFFFFFFF;
                addr_name(name, addr);
        } while (addr_taken(name, members, n_members));

        freepp(char, members, n_members);

        if (rib_add(path, name)) {
                log_err("Failed to add address to RIB.");
                return INVALID_ADDRESS;
        }

        if (rib_write(path, &addr, sizeof(addr))) {
                log_err("Failed to write address in RIB.");
                return INVALID_ADDRESS;
        }

        return addr;
}