summaryrefslogtreecommitdiff
path: root/src/tools/irm/irm_ipcp_bootstrap.c
blob: 2acb65166c4ea1aaf905c46c5bb11fc1f069e8ad (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
/*
 * Ouroboros - Copyright (C) 2016 - 2018
 *
 * Bootstrap IPC Processes
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#ifdef __FreeBSD__
#include <sys/socket.h>
#endif
#include <ouroboros/irm.h>
#include <ouroboros/ipcp.h>

#include "irm_ops.h"
#include "irm_utils.h"

#define NORMAL                 "normal"
#define UDP                    "udp"
#define ETH_LLC                "eth-llc"
#define LOCAL                  "local"
#define RAPTOR                 "raptor"

#define MD5                    "MD5"
#define SHA3_224               "SHA3_224"
#define SHA3_256               "SHA3_256"
#define SHA3_384               "SHA3_384"
#define SHA3_512               "SHA3_512"

#define DEFAULT_ADDR_SIZE      4
#define DEFAULT_FD_SIZE        2
#define DEFAULT_DDNS           0
#define DEFAULT_ADDR_AUTH      ADDR_AUTH_FLAT_RANDOM
#define DEFAULT_ROUTING        ROUTING_LINK_STATE
#define DEFAULT_PFF            PFF_SIMPLE
#define DEFAULT_HASH_ALGO      DIR_HASH_SHA3_256
#define FLAT_RANDOM_ADDR_AUTH  "flat"
#define LINK_STATE_ROUTING     "link_state"
#define LINK_STATE_LFA_ROUTING "lfa"
#define SIMPLE_PFF             "simple"
#define ALTERNATE_PFF          "alternate"

static void usage(void)
{
        /* FIXME: Add ipcp_config stuff */
        printf("Usage: irm ipcp bootstrap\n"
               "                name <ipcp name>\n"
               "                layer <layer name>\n"
               "                type [TYPE]\n"
               "where TYPE = {" NORMAL " " LOCAL " "
               UDP " " ETH_LLC " " RAPTOR "},\n\n"
               "if TYPE == " NORMAL "\n"
               "                [addr <address size> (default: %d)]\n"
               "                [fd <fd size> (default: %d)]\n"
               "                [ttl (add time to live value in the PCI)]\n"
               "                [addr_auth <ADDRESS_POLICY> (default: %s)]\n"
               "                [routing <ROUTING_POLICY> (default: %s)]\n"
               "                [pff [PFF_POLICY] (default: %s)]\n"
               "                [hash [ALGORITHM] (default: %s)]\n"
               "                [autobind]\n"
               "where ADDRESS_POLICY = {"FLAT_RANDOM_ADDR_AUTH"}\n"
               "      ROUTING_POLICY = {"LINK_STATE_ROUTING " "
               LINK_STATE_LFA_ROUTING "}\n"
               "      PFF_POLICY = {" SIMPLE_PFF " " ALTERNATE_PFF "}\n"
               "      ALGORITHM = {" SHA3_224 " " SHA3_256 " "
               SHA3_384 " " SHA3_512 "}\n\n"
               "if TYPE == " UDP "\n"
               "                ip <IP address in dotted notation>\n"
               "                [dns <DDNS IP address in dotted notation>"
               " (default: none)]\n\n"
               "if TYPE == " ETH_LLC "\n"
               "                if_name <interface name>\n"
               "                [hash [ALGORITHM] (default: %s)]\n"
               "where ALGORITHM = {" SHA3_224 " " SHA3_256 " "
               SHA3_384 " " SHA3_512 "}\n\n"
               "if TYPE == " LOCAL "\n"
               "                [hash [ALGORITHM] (default: %s)]\n"
               "where ALGORITHM = {" SHA3_224 " " SHA3_256 " "
               SHA3_384 " " SHA3_512 "}\n\n"
               "if TYPE == " RAPTOR "\n"
               "                [hash [ALGORITHM] (default: %s)]\n"
               "where ALGORITHM = {" SHA3_224 " " SHA3_256 " "
               SHA3_384 " " SHA3_512 "}\n\n",
               DEFAULT_ADDR_SIZE, DEFAULT_FD_SIZE, FLAT_RANDOM_ADDR_AUTH,
               LINK_STATE_ROUTING, SIMPLE_PFF, SHA3_256, SHA3_256, SHA3_256,
               SHA3_256);
}

int do_bootstrap_ipcp(int     argc,
                      char ** argv)
{
        char *             name           = NULL;
        pid_t              pid;
        struct ipcp_config conf;
        uint8_t            addr_size      = DEFAULT_ADDR_SIZE;
        uint8_t            fd_size        = DEFAULT_FD_SIZE;
        bool               has_ttl        = false;
        enum pol_addr_auth addr_auth_type = DEFAULT_ADDR_AUTH;
        enum pol_routing   routing_type   = DEFAULT_ROUTING;
        enum pol_pff       pff_type       = DEFAULT_PFF;
        enum pol_dir_hash  hash_algo      = DEFAULT_HASH_ALGO;
        uint32_t           ip_addr        = 0;
        uint32_t           dns_addr       = DEFAULT_DDNS;
        char *             ipcp_type      = NULL;
        char *             dif_name       = NULL;
        char *             if_name        = NULL;
        pid_t *            pids           = NULL;
        ssize_t            len            = 0;
        int                i              = 0;
        bool               autobind       = false;
        int                cargs;

        while (argc > 0) {
                cargs = 2;
                if (matches(*argv, "type") == 0) {
                        ipcp_type = *(argv + 1);
                } else if (matches(*argv, "layer") == 0) {
                        dif_name = *(argv + 1);
                } else if (matches(*argv, "name") == 0) {
                        name = *(argv + 1);
                } else if (matches(*argv, "hash") == 0) {
                        if (strcmp(*(argv + 1), SHA3_224) == 0)
                                hash_algo = DIR_HASH_SHA3_224;
                        else if (strcmp(*(argv + 1), SHA3_256) == 0)
                                hash_algo = DIR_HASH_SHA3_256;
                        else if (strcmp(*(argv + 1), SHA3_384) == 0)
                                hash_algo = DIR_HASH_SHA3_384;
                        else if (strcmp(*(argv + 1), SHA3_512) == 0)
                                hash_algo = DIR_HASH_SHA3_512;
                        else
                                goto unknown_param;
                } else if (matches(*argv, "ip") == 0) {
                        if (inet_pton (AF_INET, *(argv + 1), &ip_addr) != 1)
                                goto unknown_param;
                } else if (matches(*argv, "dns") == 0) {
                        if (inet_pton(AF_INET, *(argv + 1), &dns_addr) != 1)
                                goto unknown_param;
                } else if (matches(*argv, "if_name") == 0) {
                        if_name = *(argv + 1);
                } else if (matches(*argv, "addr") == 0) {
                        addr_size = atoi(*(argv + 1));
                } else if (matches(*argv, "fd") == 0) {
                        fd_size = atoi(*(argv + 1));
                } else if (matches(*argv, "ttl") == 0) {
                        has_ttl = true;
                        cargs = 1;
                } else if (matches(*argv, "autobind") == 0) {
                        autobind = true;
                        cargs = 1;
                } else if (matches(*argv, "addr_auth") == 0) {
                        if (strcmp(FLAT_RANDOM_ADDR_AUTH, *(argv + 1)) == 0)
                                addr_auth_type = ADDR_AUTH_FLAT_RANDOM;
                        else
                                goto unknown_param;
                } else if (matches(*argv, "routing") == 0) {
                        if (strcmp(LINK_STATE_ROUTING, *(argv + 1)) == 0)
                                routing_type = ROUTING_LINK_STATE;
                        else if (strcmp(LINK_STATE_LFA_ROUTING,
                                        *(argv + 1)) == 0)
                                routing_type = ROUTING_LINK_STATE_LFA;
                        else
                                goto unknown_param;
                } else if (matches(*argv, "pff") == 0) {
                        if (strcmp(SIMPLE_PFF, *(argv + 1)) == 0)
                                pff_type = PFF_SIMPLE;
                        else if (strcmp(ALTERNATE_PFF, *(argv + 1)) == 0)
                                pff_type = PFF_ALTERNATE;
                        else
                                goto unknown_param;
                } else {
                        printf("Unknown option: \"%s\".\n", *argv);
                        return -1;
                }

                argc -= cargs;
                argv += cargs;
        }

        if (name == NULL || dif_name == NULL || ipcp_type == NULL) {
                usage();
                return -1;
        }

        strcpy(conf.dif_info.dif_name, dif_name);
        if (strcmp(ipcp_type, UDP) != 0)
                conf.dif_info.dir_hash_algo = hash_algo;

        if (strcmp(ipcp_type, NORMAL) == 0) {
                conf.type = IPCP_NORMAL;
                conf.addr_size = addr_size;
                conf.fd_size = fd_size;
                conf.has_ttl = has_ttl;
                conf.addr_auth_type = addr_auth_type;
                conf.routing_type = routing_type;
                conf.pff_type = pff_type;
        } else if (strcmp(ipcp_type, UDP) == 0) {
                conf.type = IPCP_UDP;
                if (ip_addr == 0) {
                        usage();
                        return -1;
                }
                conf.ip_addr = ip_addr;
                conf.dns_addr = dns_addr;
        } else if (strcmp(ipcp_type, LOCAL) == 0) {
                conf.type = IPCP_LOCAL;
        } else if (strcmp(ipcp_type, RAPTOR) == 0) {
                conf.type = IPCP_RAPTOR;
        } else if (strcmp(ipcp_type, ETH_LLC) == 0) {
                conf.type = IPCP_ETH_LLC;
                if (if_name == NULL) {
                        usage();
                        return -1;
                }
                conf.if_name = if_name;
        } else {
                usage();
                return -1;
        }

        if (autobind && conf.type != IPCP_NORMAL) {
                printf("Can only bind normal IPCPs, autobind disabled.\n");
                autobind = false;
        }

        len = irm_list_ipcps(name, &pids);
        if (len <= 0) {
                pid = irm_create_ipcp(name, conf.type);
                if (pid <= 0)
                        return -1;
                len = irm_list_ipcps(name, &pids);
        }

        for (i = 0; i < len; i++) {
                if (autobind && irm_bind_process(pids[i], name)) {
                        printf("Failed to bind %d to %s.\n", pids[i], name);
                        free(pids);
                        return -1;
                }

                if (autobind && irm_bind_process(pids[i], dif_name)) {
                        printf("Failed to bind %d to %s.\n", pids[i], dif_name);
                        irm_unbind_process(pids[i], name);
                        free(pids);
                        return -1;
                }

                if (irm_bootstrap_ipcp(pids[i], &conf)) {
                        if (autobind) {
                                irm_unbind_process(pids[i], name);
                                irm_unbind_process(pids[i], dif_name);
                        }
                        free(pids);
                        return -1;
                }
        }

        free(pids);

        return 0;

 unknown_param:
        printf("Unknown parameter for %s: \"%s\".\n", *argv, *(argv + 1));
        return -1;
}