summaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/facemgr.c
blob: 41e30de56e18b548716f92a04551bc4277647f19 (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
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file facemgr.c
 * \brief Implementation of Face manager library interface
 */

#include <stdio.h>

#include "common.h"
#include "event.h"
#include "facemgr.h"
#include "interface.h"
#include "util/log.h"

#ifdef __APPLE__
extern interface_ops_t network_framework_ops;
#endif
#ifdef __linux__
extern interface_ops_t netlink_ops;
#endif
#if 0
extern interface_ops_t dummy_ops;
#endif
extern interface_ops_t hicn_light_ops;

int
facemgr_initialize(facemgr_t * facemgr)
{
    int rc;

    rc = interface_map_initialize(&facemgr->interface_map);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_INTERFACE_MAP;

    rc = face_cache_initialize(&facemgr->face_cache);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_FACE_SET;

    rc = face_rules_initialize(&facemgr->rules);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_FACE_SET;

    return FACEMGR_SUCCESS;

ERR_FACE_SET:
    interface_map_finalize(&facemgr->interface_map);

ERR_INTERFACE_MAP:
    return FACEMGR_FAILURE;
}

int
facemgr_finalize(facemgr_t * facemgr)
{
    int rc;

    /* XXX Free all interfaces: pass free to map */
    rc = interface_map_finalize(&facemgr->interface_map);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR;

    rc = face_cache_finalize(&facemgr->face_cache);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR;

    rc = face_rules_finalize(&facemgr->rules);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR;

    return FACEMGR_SUCCESS;

ERR:
    return FACEMGR_FAILURE;
}

AUTOGENERATE_CREATE_FREE(facemgr);

int
facemgr_on_event(facemgr_t * facemgr, event_t * event)
{
    int rc;
    char face_s[MAXSZ_FACE];
    face_t * cached_face;

    if (!event->face) {
        printf("Event with empty face\n");
        return -1;
    }

    face_t face = *event->face;

    /* Complement unbound UDP faces */
    switch(face.type) {
        case FACE_TYPE_TCP:
        case FACE_TYPE_UDP:
            switch (face.params.tunnel.family) {
                case AF_INET:
                    if ((ip_address_empty(&face.params.tunnel.remote_addr)) &&
                            (!ip_address_empty(&facemgr->overlay_v4_remote_addr)))
                        face.params.tunnel.remote_addr = facemgr->overlay_v4_remote_addr;
                    if ((face.params.tunnel.local_port == 0) && (facemgr->overlay_v4_local_port != 0))
                        face.params.tunnel.local_port = facemgr->overlay_v4_local_port;
                    if ((face.params.tunnel.remote_port == 0) && (facemgr->overlay_v4_remote_port != 0))
                        face.params.tunnel.remote_port = facemgr->overlay_v4_remote_port;
                    break;
                case AF_INET6:
                    if ((ip_address_empty(&face.params.tunnel.remote_addr)) &&
                            (!ip_address_empty(&facemgr->overlay_v6_remote_addr)))
                        face.params.tunnel.remote_addr = facemgr->overlay_v6_remote_addr;
                    if ((face.params.tunnel.local_port == 0) && (facemgr->overlay_v6_local_port != 0))
                        face.params.tunnel.local_port = facemgr->overlay_v6_local_port;
                    if ((face.params.tunnel.remote_port == 0) && (facemgr->overlay_v6_remote_port != 0))
                        face.params.tunnel.remote_port = facemgr->overlay_v6_remote_port;
                default:
                    break;
            }
            break;
        default:
            break;
    }

    face_snprintf(face_s, MAXSZ_FACE, &face);

    /* TODO Here, we need to filter events based on our cache, and update the cache
     * based on our actions if they are successful */

    switch(event->type) {
        case EVENT_TYPE_CREATE:
            rc = face_cache_get(&facemgr->face_cache, &face, &cached_face);
            if (!FACEMGR_IS_ERROR(rc)) {
                DEBUG("Face found in cache");
                goto IGNORE_EVENT;
            }
            rc = face_cache_add(&facemgr->face_cache, &face);
            if (FACEMGR_IS_ERROR(rc))
                WARN("Failed to add face to cache");
            break;
        case EVENT_TYPE_DELETE:
            rc = face_cache_remove(&facemgr->face_cache, &face, &cached_face);
            if (FACEMGR_IS_ERROR(rc))
                WARN("Face not found in cache");
            break;
        case EVENT_TYPE_SET_UP:
        case EVENT_TYPE_SET_DOWN:
            /* TODO We need a return code to update the cache */
            break;
        default:
            printf("Not implemented!\n");
            break;
    }

    /* Process event */
    printf("[ FACE %s ] %s\n", event_type_str[event->type], face_s);
    /* Hardcoded hicn-light */
    rc = interface_on_event(facemgr->hl, event);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR;

IGNORE_EVENT:
    return FACEMGR_SUCCESS;

ERR:
    return FACEMGR_FAILURE;
}

#ifdef __linux__
void interface_callback(evutil_socket_t fd, short what, void * arg) {
    interface_t * interface = (interface_t *)arg;
    interface->ops->callback(interface);
}
#endif /* __linux__ */

int
facemgr_create_interface(facemgr_t * facemgr, const char * name, const char * type, interface_t ** interface)
{
    int fd, rc;

    INFO("Creating interface %s [%s]...\n", name, type);
    *interface = interface_create(name, type);
    if (!*interface) {
        ERROR("Error creating interface %s [%s]\n", name, type);
        return -1;
    }
    interface_set_callback(*interface, facemgr_on_event, facemgr);

    fd = interface_initialize(*interface, &facemgr->rules);
    if (fd < 0)
        return -2;
    if (fd != 0) {
#ifdef __linux__
        evutil_make_socket_nonblocking(fd);
        struct event * event = event_new(facemgr->loop, fd, EV_READ | EV_PERSIST, interface_callback, *interface);
        if (!event) {
            return -3;
        }

        if (event_add(event, NULL) < 0) {
            return -4;
        }
#else
        ERROR("Not implemented\n");
        return FACEMGR_FAILURE;
#endif /* __linux__ */
    }

    rc = interface_map_add(&facemgr->interface_map, (*interface)->name, *interface);
    if (FACEMGR_IS_ERROR(rc))
        return -5;

    DEBUG("Interface created successfully.\n");
    return FACEMGR_SUCCESS;
}

int
facemgr_bootstrap(facemgr_t * facemgr)
{
    int rc;

    DEBUG("Registering interfaces...");
    rc = interface_register(&hicn_light_ops);
    if (FACEMGR_IS_ERROR(rc)) {
        ERROR("Could not register interfaces");
        goto ERR_REGISTER;
    }

#ifdef __APPLE__
    rc = interface_register(&network_framework_ops);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_REGISTER;
#endif /* __APPLE__ */

#ifdef __linux__
    rc = interface_register(&netlink_ops);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_REGISTER;
#endif /* __linux__ */

#if 0
    rc = interface_register(&dummy_ops);
    if (FACEMGR_IS_ERROR(rc))
        goto ERR_REGISTER;
#endif

    rc = facemgr_create_interface(facemgr, "hl", "hicn_light", &facemgr->hl);
    if (rc < 0) {
        ERROR("Error creating 'hICN forwarder (hicn-light)' interface\n");
        goto ERR_HL_CREATE;
    }

#ifdef __APPLE__
    rc = facemgr_create_interface(facemgr, "nf", "network_framework", &facemgr->nf);
    if (rc < 0) {
        ERROR("Error creating 'Apple Network Framework' interface\n");
        goto ERR_NF_CREATE;
    }
#endif /* __APPLE__ */

#ifdef __linux__
    rc = facemgr_create_interface(facemgr, "nl", "netlink", &facemgr->nl);
    if (rc < 0) {
        ERROR("Error creating 'Netlink' interface\n");
        goto ERR_NF_CREATE;
    }
#endif /* __linux__ */

#if 0
    rc = facemgr_create_interface(facemgr, "dummy", "dummy", &facemgr->dummy);
    if (rc < 0) {
        ERROR("Error creating 'Dummy' interface\n");
        goto ERR_NF_CREATE;
    }
#endif

    DEBUG("Facemgr successfully initialized...");

    return FACEMGR_SUCCESS;

ERR_NF_CREATE:
    interface_free(facemgr->hl);
ERR_HL_CREATE:
    //interface_map_remove(&facemgr->interface_map, data->nf->name);
ERR_REGISTER:
    return FACEMGR_FAILURE;
}