aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ietf/ietf_nat.c
blob: 86b700f32992d5fe4a118f713538c2bde4e86eb5 (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
516
517
518
519
520
521
522
523
524
525
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * 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.
 */

#include <scvpp/comm.h>
#include <scvpp/interface.h>
#include <scvpp/nat.h>

#include <sc_plugins.h>

#include <assert.h>
#include <string.h>
#include <sysrepo.h>
#include <sysrepo/xpath.h>
#include <sysrepo/values.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/**
 * @brief Wrapper struct for VAPI address range payload.
 */
struct address_range_t {
    nat44_add_del_address_range_t payload; //< VAPI payload for address range.
    bool last_ip_address_set; //< Variable last_ip_address in payload is set.
};

// Check input structure, set VAPI payload and call VAPI function to set
// nat44 address range.
static sr_error_t set_address_range(struct address_range_t *address_r)
{
    sr_error_t rc = SR_ERR_OK;
//     char tmp_ip1[VPP_IP4_ADDRESS_STRING_LEN];
//     char tmp_ip2[VPP_IP4_ADDRESS_STRING_LEN];

    ARG_CHECK(SR_ERR_INVAL_ARG, address_r);

    //if end of IP range not provided, then range size = 1 with only first ip
    if (!address_r->last_ip_address_set) {
        memcpy(&address_r->payload.last_ip_address[0],
               &address_r->payload.first_ip_address[0],
               VPP_IP4_ADDRESS_LEN);
    }

    if (hardntohlu32(address_r->payload.last_ip_address) <
        hardntohlu32(address_r->payload.first_ip_address)) {
        SRP_LOG_ERR_MSG("End address less than start address");
        return SR_ERR_INVAL_ARG;
    }

//     strncpy(tmp_ip1, sc_ntoa(address_r->payload.first_ip_address),
//             VPP_IP4_ADDRESS_STRING_LEN);
//     strncpy(tmp_ip2, sc_ntoa(address_r->payload.last_ip_address),
//             VPP_IP4_ADDRESS_STRING_LEN);
//     SRP_LOG_DBG("Fist ip address: %s, last ip address: %s, twice_nat: %u,"
//                 "is_add: %u", tmp_ip1, tmp_ip2, address_r->payload.twice_nat,
//                 address_r->payload.is_add);

    int rv = nat44_add_del_addr_range(&address_r->payload);
    if (0 != rv) {
        SRP_LOG_ERR_MSG("Failed set address range.");
        rc = SR_ERR_OPERATION_FAILED;
    }

    return rc;
}

// parse leaf from this xpath:
// /ietf-nat:nat/instances/instance[id='%s']/policy[id='%s']/external-ip-address-pool[pool-id='%s']/
static int parse_instance_policy_external_ip_address_pool(
    const sr_val_t *val, struct address_range_t *address_range)
{
    int rc;
    char tmp_str[VPP_IP4_PREFIX_STRING_LEN] = {0};
    uint8_t prefix = 0;

    ARG_CHECK2(SR_ERR_INVAL_ARG, val, address_range);

    if (sr_xpath_node_name_eq(val->xpath, "pool-id")) {
        SRP_LOG_WRN("%s not supported.", val->xpath);
    } else if(sr_xpath_node_name_eq(val->xpath, "external-ip-pool")) {
        rc = prefix2ip4(tmp_str, val->data.string_val, &prefix);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Error translate");
            return SR_ERR_INVAL_ARG;
        }

        rc = sc_aton(tmp_str, address_range->payload.first_ip_address,
                     VPP_IP4_ADDRESS_LEN);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Failed convert string IP address to int.");
            return SR_ERR_INVAL_ARG;
        }

        if (prefix < VPP_IP4_HOST_PREFIX_LEN) {
            // External IP pool is represented as IPv4 prefix in YANG module.
            // VPP support range from first IPv4 address to last IPv4 address, not prefix.
            // In this concept the broadcast IPv4 address of this IPv4 prefix,
            // represent last IPv4 address for VPP
            get_last_ip_address(
                (sc_ipv4_addr*) &address_range->payload.last_ip_address[0],
                (sc_ipv4_addr*) &address_range->payload.first_ip_address[0],
                prefix);
            address_range->last_ip_address_set = true;
        }
    }

    return SR_ERR_OK;
}

// XPATH: /ietf-nat:nat/instances/instance[id='%s']/policy[id='%s']/external-ip-address-pool[pool-id='%s']/
static int instances_instance_policy_external_ip_address_pool_cb(
    sr_session_ctx_t *ds, const char *xpath, sr_notif_event_t event,
    void *private_ctx)
{
    UNUSED(private_ctx);
    sr_error_t rc = SR_ERR_OK;
    sr_change_iter_t *it = NULL;
    sr_change_oper_t oper;
    sr_val_t *old_val = NULL;
    sr_val_t *new_val = NULL;
    bool create = false;
    bool delete = false;
    struct address_range_t new_address_r = {0};
    struct address_range_t old_address_r = {0};

    ARG_CHECK2(SR_ERR_INVAL_ARG, ds, xpath);

    SRP_LOG_INF("In %s", __FUNCTION__);

    new_address_r.payload.vrf_id = ~0;
    old_address_r.payload.vrf_id = ~0;

    SRP_LOG_DBG("'%s' modified, event=%d", xpath, event);

    if (event != SR_EV_APPLY) {
        return SR_ERR_OK;
    }

    if (sr_get_changes_iter(ds, (char *)xpath, &it) != SR_ERR_OK) {
        // in example he calls even on fale
        sr_free_change_iter(it);
        return SR_ERR_OK;
    }

    foreach_change (ds, it, oper, old_val, new_val) {

        SRP_LOG_DBG("A change detected in '%s', op=%d",
                    new_val ? new_val->xpath : old_val->xpath, oper);

        switch (oper) {
            case SR_OP_CREATED:
                create = true;
                rc = parse_instance_policy_external_ip_address_pool(new_val,
                                                                &new_address_r);
                break;

            case SR_OP_MODIFIED:
                delete = true;
                rc = parse_instance_policy_external_ip_address_pool(old_val,
                                                                &old_address_r);
                if (SR_ERR_OK != rc) {
                    break;
                }

                create = true;
                rc = parse_instance_policy_external_ip_address_pool(new_val,
                                                                &new_address_r);
                break;

            case SR_OP_MOVED:
                break;

            case SR_OP_DELETED:
                delete = true;
                rc = parse_instance_policy_external_ip_address_pool(old_val,
                                                                &old_address_r);
                break;
        }

        sr_free_val(old_val);
        sr_free_val(new_val);

        if (SR_ERR_OK != rc) {
            rc = SR_ERR_OPERATION_FAILED;
            goto error;
        }
    }

    if (delete) {
        old_address_r.payload.is_add = 0;
        rc = set_address_range(&old_address_r);
        if (SR_ERR_OK != rc) {
            goto error;
        }
    }

    if (create) {
        new_address_r.payload.is_add = 1;
        rc = set_address_range(&new_address_r);
        if (SR_ERR_OK != rc) {
            goto error;
        }
    }

error:
    sr_free_change_iter(it);
    return rc;
}

enum mapping_type {
    STATIC,
    DYNAMIC_IMPLICIT,
    DYNAMIC_EXPLICIT,
    UNKNOWN,
};

/**
 * @brief Wrapper struct for VAPI static mapping payload.
 */
struct static_mapping_t {
    enum mapping_type mtype; //< Mapping type
    bool local_ip_address_set; //< Variable are set in payload.
    bool external_ip_address_set; //< Variable are set in payload.
    bool protocol_set; //< Variable are set in payload.
    bool local_port_set; //< Variable are set in payload.
    bool external_port_set; //< Variable are set in payload.
    bool external_sw_if_index_set; //< Variable are set in payload.
    bool vrf_id_set; //< Variable are set in payload.
    bool twice_nat_set; //< Variable are set in payload.
    nat44_add_del_static_mapping_t payload; //< VAPI payload for static mapping
};

// Check input structure, set VAPI payload and call VAPI function to set
// nat44 static mapping.
static sr_error_t set_static_mapping(struct static_mapping_t *mapping)
{
    int rc = 0;
//     char tmp_ip1[VPP_IP4_ADDRESS_STRING_LEN];
//     char tmp_ip2[VPP_IP4_ADDRESS_STRING_LEN];

    ARG_CHECK(SR_ERR_INVAL_ARG, mapping);

    if ((mapping->local_port_set != mapping->external_port_set) ||
        !mapping->local_ip_address_set || !mapping->external_ip_address_set) {
        SRP_LOG_ERR_MSG("NAT44 parameter missing.");

        return SR_ERR_VALIDATION_FAILED;
    }

    if (!mapping->local_port_set && !mapping->external_port_set) {
        //FIXME!!!!! Compile error
//         mapping->payload.flags = NAT_IS_ADDR_ONLY;
    } else {
        //FIXME!!!!! Compile error
//         mapping->payload.flags = NAT_IS_TWICE_NAT;
        if (!mapping->protocol_set) {

            SRP_LOG_ERR_MSG("NAT44 protocol missing.");
            return SR_ERR_VALIDATION_FAILED;
        }
    }

    mapping->payload.external_sw_if_index = ~0;

//     strncpy(tmp_ip1, sc_ntoa(mapping->payload.local_ip_address),
//             VPP_IP4_ADDRESS_STRING_LEN);
//     strncpy(tmp_ip2, sc_ntoa(mapping->payload.external_ip_address),
//             VPP_IP4_ADDRESS_STRING_LEN);
//     SRP_LOG_DBG("Local ip address: %s, external ip address: %s, addr_only: %u,"
//                 " protocol: %u, local port: %u, external port: %u, twice_nat: %u,"
//                 "is_add: %u", tmp_ip1, tmp_ip2, mapping->payload.addr_only,
//                 mapping->payload.protocol, mapping->payload.local_port,
//                 mapping->payload.external_port,  mapping->payload.twice_nat,
//                 mapping->payload.is_add);

    rc = nat44_add_del_static_mapping(&mapping->payload);
    if (0 != rc) {
        SRP_LOG_ERR_MSG("Failed set static mapping");
        return SR_ERR_OPERATION_FAILED;
    }

    return SR_ERR_OK;
}

// parse leaf from this xpath:
// /ietf-nat:nat/instances/instance[id='%s']/mapping-table/mapping-entry[index='%s']/
static int parse_instance_mapping_table_mapping_entry(
    const sr_val_t *val,
    struct static_mapping_t *mapping)
{
    int rc;
    char tmp_str[VPP_IP4_PREFIX_STRING_LEN] = {0};

    ARG_CHECK2(SR_ERR_INVAL_ARG, val, mapping);

    sr_xpath_ctx_t state = {0};

    if(sr_xpath_node_name_eq(val->xpath, "type")) {
        if (!strncmp("static", val->data.string_val, strlen("static"))) {
            mapping->mtype = STATIC;
        } else if (!strncmp("dynamic-implicit", val->data.string_val,
            strlen("dynamic-implicit"))) {
            mapping->mtype = DYNAMIC_IMPLICIT;
        } else if (!strncmp("dynamic-explicit", val->data.string_val,
            strlen("dynamic-explicit"))) {
                mapping->mtype = DYNAMIC_EXPLICIT;
        }
    } else if(sr_xpath_node_name_eq(val->xpath, "transport-protocol")) {
        if (SR_UINT8_T != val->type) {
            SRP_LOG_ERR("Wrong transport-protocol, type, current type: %d.",
                        val->type);
            return SR_ERR_INVAL_ARG;
        }

        mapping->payload.protocol = val->data.uint8_val;
        mapping->protocol_set = true;
    } else if(sr_xpath_node_name_eq(val->xpath, "internal-src-address")) {
        if (SR_STRING_T != val->type) {
            SRP_LOG_ERR("Wrong internal-src-address, type, current type: %d.",
                        val->type);
            return SR_ERR_INVAL_ARG;
        }

        rc = prefix2ip4(tmp_str, val->data.string_val, NULL);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Error translate");
            return SR_ERR_INVAL_ARG;
        }

        rc = sc_aton(tmp_str, mapping->payload.local_ip_address,
                     VPP_IP4_ADDRESS_LEN);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Failed convert string IP address to int.");
            return SR_ERR_INVAL_ARG;
        }

        mapping->local_ip_address_set = true;
    } else if(sr_xpath_node_name_eq(val->xpath, "external-src-address")) {
        if (SR_STRING_T != val->type) {
            SRP_LOG_ERR("Wrong external-src-address, type, current type: %d.",
                        val->type);
            return SR_ERR_INVAL_ARG;
        }

        rc = prefix2ip4(tmp_str, val->data.string_val, NULL);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Error translate");
            return SR_ERR_INVAL_ARG;
        }

        rc = sc_aton(tmp_str, mapping->payload.external_ip_address,
                     VPP_IP4_ADDRESS_LEN);
        if (0 != rc) {
            SRP_LOG_ERR_MSG("Failed convert string IP address to int.");
            return SR_ERR_INVAL_ARG;
        }

        mapping->external_ip_address_set = true;
    } else if (sr_xpath_node(val->xpath, "internal-src-port", &state)) {
        sr_xpath_recover(&state);
        if(sr_xpath_node_name_eq(val->xpath, "start-port-number")) {
            mapping->local_port_set = true;
            mapping->payload.local_port = val->data.uint16_val;
        }
    } else if (sr_xpath_node(val->xpath, "external-src-port", &state)) {
        sr_xpath_recover(&state);
        if(sr_xpath_node_name_eq(val->xpath, "start-port-number")) {
            mapping->external_port_set = true;
            mapping->payload.external_port = val->data.uint16_val;
        }
    }

    return SR_ERR_OK;
}

// XPATH: /ietf-nat:nat/instances/instance[id='%s']/mapping-table/mapping-entry[index='%s']/
static int instances_instance_mapping_table_mapping_entry_cb(
    sr_session_ctx_t *ds, const char *xpath, sr_notif_event_t event,
    void *private_ctx)
{
    UNUSED(private_ctx);
    sr_error_t rc = SR_ERR_OK;
    sr_change_iter_t *it = NULL;
    sr_change_oper_t oper;
    sr_val_t *old_val = NULL;
    sr_val_t *new_val = NULL;
    bool create = false;
    bool delete = false;
    struct static_mapping_t new_mapping = {0};
    struct static_mapping_t old_mapping = {0};

    ARG_CHECK2(SR_ERR_INVAL_ARG, ds, xpath);

    SRP_LOG_INF("In %s", __FUNCTION__);

    new_mapping.mtype = UNKNOWN;
    old_mapping.mtype = UNKNOWN;

    SRP_LOG_DBG("'%s' modified, event=%d", xpath, event);

    if (event != SR_EV_APPLY) {
        return SR_ERR_OK;
    }

    if (sr_get_changes_iter(ds, (char *)xpath, &it) != SR_ERR_OK) {
        // in example he calls even on fale
        sr_free_change_iter(it);
        return SR_ERR_OK;
    }

    foreach_change (ds, it, oper, old_val, new_val) {

        SRP_LOG_DBG("A change detected in '%s', op=%d",
                    new_val ? new_val->xpath : old_val->xpath, oper);

        switch (oper) {
            case SR_OP_CREATED:
                create = true;
                rc = parse_instance_mapping_table_mapping_entry(new_val,
                                                                &new_mapping);
                break;

            case SR_OP_MODIFIED:
                delete = true;
                rc = parse_instance_mapping_table_mapping_entry(old_val,
                                                                &old_mapping);
                if (SR_ERR_OK != rc) {
                    break;
                }

                create = true;
                rc = parse_instance_mapping_table_mapping_entry(new_val,
                                                                &new_mapping);
               break;

            case SR_OP_MOVED:
                break;

            case SR_OP_DELETED:
                delete = true;
                rc = parse_instance_mapping_table_mapping_entry(old_val,
                                                                &old_mapping);
                break;
        }

        sr_free_val(old_val);
        sr_free_val(new_val);

        if (SR_ERR_OK != rc) {
            rc = SR_ERR_OPERATION_FAILED;
            goto error;
        }
    }

    if (delete) {
        old_mapping.payload.is_add = 0;
        if (STATIC == old_mapping.mtype) {
            rc = set_static_mapping(&old_mapping);
            if (SR_ERR_OK != rc) {
                goto error;
            }
        }
    }

    if (create) {
        new_mapping.payload.is_add = 1;
        if (STATIC == new_mapping.mtype) {
            rc = set_static_mapping(&new_mapping);
            if (SR_ERR_OK != rc) {
                goto error;
            }
        }
    }

error:
    sr_free_change_iter(it);
    return rc;
}

int
ietf_nat_init(sc_plugin_main_t *pm)
{
    int rc = SR_ERR_OK;
    SRP_LOG_DBG_MSG("Initializing ietf-nat plugin.");

    rc = sr_subtree_change_subscribe(pm->session, "/ietf-nat:nat/instances/instance/policy/external-ip-address-pool",
            instances_instance_policy_external_ip_address_pool_cb, NULL, 0, SR_SUBSCR_CTX_REUSE, &pm->subscription);
    if (rc != SR_ERR_OK) {
        goto error;
    }

    rc = sr_subtree_change_subscribe(pm->session, "/ietf-nat:nat/instances/instance/mapping-table/mapping-entry",
            instances_instance_mapping_table_mapping_entry_cb, NULL, 0, SR_SUBSCR_CTX_REUSE, &pm->subscription);
    if (rc != SR_ERR_OK) {
        goto error;
    }

    SRP_LOG_DBG_MSG("ietf-nat plugin initialized successfully.");
    return SR_ERR_OK;

error:
    SRP_LOG_ERR("Error by initialization of ietf-nat plugin. Error : %d", rc);
    return rc;
}

void
ietf_nat_exit(__attribute__((unused)) sc_plugin_main_t *pm)
{
}

SC_INIT_FUNCTION(ietf_nat_init);
SC_EXIT_FUNCTION(ietf_nat_exit);