aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/socket/api.c
blob: e39ebf4b5e2ba660b82a5acf4a355e24c76d3e80 (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <arpa/inet.h>  // inet_ntop
#include <netdb.h>      // ''
#include <search.h>     // tfind(), tdestroy(), twalk(), preorder...
#include <stdbool.h>
#include <stdio.h>       // perror
#include <stdlib.h>      // calloc
#include <string.h>      // memcpy
#include <sys/socket.h>  // ''
#include <sys/types.h>   // getaddrinfo
#include <unistd.h>      // close

#include "api.h"
#include "ops.h"
#include <hicn/util/sstrncpy.h>

#if __linux__
#include "error.h"
#endif

#define INET_MAX_ADDRSTRLEN INET6_ADDRSTRLEN

#define IF_NAMESIZE 16
#define MAX_TABLES 256

#define DEFAULT_INTERVAL 1000
#define DEFAULT_IDENTIFIER "hicn"
#define DEFAULT_SOCKET_IDENTIFIER "main"
#define LOCAL_IPV6_PREFIX "fe80"

#define LOCAL_PRIORITY 32000

extern hicn_socket_ops_t ops;

/* Configuration stored as a global variable to allow access from signal
 * handlers for instance */

static hicn_conf_t hicn_default_conf = {
    .identifier = DEFAULT_IDENTIFIER,
    //.format = HF_INET6_TCP
};

/* Global state */

struct ip_rule_state_ {
  char tun_name[IF_NAMESIZE];
  ip_prefix_t prefix;
  uint32_t table_id;
  uint8_t priority;
  uint8_t address_family;
};

struct ip_route_state_ {
  char remote_ip_address[128];  // this is to big, but it is fine for now
  uint8_t address_family;
  uint32_t table_id;
};

typedef struct ip_rule_state_ ip_rule_state;
typedef struct ip_route_state_ ip_route_state;

int punting_table_id;
uint16_t rules_counter;
uint16_t routes_counter;
static ip_rule_state rules_to_remove[MAX_TABLES];
static ip_route_state routes_to_remove[MAX_TABLES];

hicn_socket_helper_t *hicn_create() {
  int rc;

  punting_table_id = -1;
  rules_counter = 0;

  hicn_socket_helper_t *hicn = malloc(sizeof(hicn_socket_helper_t));
  if (!hicn) {
    goto ERR_MALLOC;
  }

  hicn->conf = malloc(sizeof(hicn_conf_t));
  if (hicn->conf < 0) goto ERR_CONF;
  memcpy(hicn->conf, &hicn_default_conf, sizeof(hicn_conf_t));

  /* Initialize socket tree to empty */
  hicn->socket_root = NULL;

  // enable forwarding globally. Per-interface forwarding will be enabled when
  // interfaces are created (TODO)
  rc = ops.enable_v6_forwarding(NULL);
  if (rc < 0) {
    goto ERR_FW;
  }

  rc = ops.enable_v4_forwarding();
  if (rc < 0) {
    goto ERR_FW;
  }

  // modify priority of table local
  /* ip -6 rule del from all prio 0 table local */
  /* ip -6 rule add from all prio 32000 table local */

  rc = ops.del_lo_prio_rule(NULL, AF_INET6, 0);
  if (rc < 0) {
    goto ERR_FW;
  }

  rc = ops.del_lo_prio_rule(NULL, AF_INET, 0);
  if (rc < 0) {
    goto ERR_FW;
  }

  rc = ops.add_lo_prio_rule(NULL, AF_INET6, LOCAL_PRIORITY);
  if (rc < 0) {
    goto ERR_FW;
  }

  rc = ops.add_lo_prio_rule(NULL, AF_INET, LOCAL_PRIORITY);
  if (rc < 0) {
    goto ERR_FW;
  }

  return hicn;

ERR_FW:
  free(hicn->conf);
ERR_CONF:
  free(hicn);
ERR_MALLOC:
  return NULL;
}

void hicn_destroy() {
  int rc;
  int ret = 0;
  uint16_t i;

  /* Restore default rules */
  printf("Restoring default configuration.\n");
  rc = ops.del_lo_prio_rule(NULL, AF_INET6, LOCAL_PRIORITY);
  if (rc < 0) ret = -1;

  rc = ops.del_lo_prio_rule(NULL, AF_INET, LOCAL_PRIORITY);
  if (rc < 0) ret = -1;

  rc = ops.add_lo_prio_rule(NULL, AF_INET6, 0);
  if (rc < 0) ret = -1;

  rc = ops.add_lo_prio_rule(NULL, AF_INET, 0);
  if (rc < 0) ret = -1;

  for (i = 0; i < rules_counter; i++) {
    if (strcmp(rules_to_remove[i].tun_name, "NONE") != 0) {
      rc = ops.del_rule(rules_to_remove[i].tun_name,
                        rules_to_remove[i].address_family,
                        rules_to_remove[i].table_id);
    } else {
      rc = ops.del_prio_rule(
          &rules_to_remove[i].prefix, rules_to_remove[i].address_family,
          rules_to_remove[i].priority, rules_to_remove[i].table_id);
    }
    if (rc < 0) ret = -1;
  }

  for (i = 0; i < routes_counter; i++) {
    rc = ops.del_out_route(routes_to_remove[i].remote_ip_address,
                           routes_to_remove[i].address_family,
                           routes_to_remove[i].table_id);
    if (rc < 0) ret = -1;
  }

  if (ret < 0) printf("Unexpected exit. Some state may not be deleted.\n");
}

void hicn_free(hicn_socket_helper_t *hicn) {
  hicn_destroy();
  free(hicn->conf);
  free(hicn);
}

hicn_socket_t *hicn_socket_create() {
  hicn_socket_t *socket = calloc(1, sizeof(hicn_socket_t));
  if (!socket) {
    goto ERR_SOCKET;
  }
  socket->type = HS_UNSPEC;

  return socket;

ERR_SOCKET:
  return NULL;
}

int hicn_socket_cmp(hicn_socket_t *a, hicn_socket_t *b) {
  return b->fd - a->fd;
}

ip_prefix_t *hicn_socket_get_src_ip(hicn_socket_t *socket) {
  if (socket->type != HS_CONNECTION) {
    return NULL;
  }
  return &socket->connection.tun_ip_address;
}

typedef int (*cmp_t)(const void *, const void *);

int hicn_socket_add(hicn_socket_helper_t *hicn, hicn_socket_t *socket) {
  if (!(tsearch(socket, &hicn->socket_root, (cmp_t)hicn_socket_cmp))) {
    // ERROR("Could not insert field id into index");
    return -1;
  }
  return 0;
}

hicn_socket_t *hicn_socket_find(hicn_socket_helper_t *hicn, int fd) {
  hicn_socket_t search = {
      .fd = fd,
  };
  hicn_socket_t **socket =
      tfind(&search, &hicn->socket_root, (cmp_t)hicn_socket_cmp);
  return socket ? *socket : NULL;
}

/*******************************************************************************
 * New API
 *******************************************************************************/

int hicn_set_local_endpoint(hicn_socket_t *socket, const char *local_ip_address,
                            bool allow_null) {
  int rc = HICN_SOCKET_ERROR_NONE;

  if (!local_ip_address) {
    if (!allow_null) {
      rc = HICN_SOCKET_ERROR_SOCKET_LOCAL_NULL_ADDRESS;
    }
    goto end;
  }

  /* local_ip_address should be a prefix with global scope in which to pick
   * the locator address to use as the source.
   * If we expect to pick another IP for the tun, then it needs to be of size
   * less than 128.
   */

  /* Copy the local IP address inside the connection */
  rc = ip_prefix_pton(local_ip_address, &socket->connection.tun_ip_address);
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_SOCKET_LOCAL_REPR;
    goto end;
  }

end:
  return rc;
}

int hicn_get_local_address(const ip_prefix_t *remote_address,
                           ip_prefix_t *local_address) {
  int rc = 0;
  uint32_t interface_id;
  char remote_address_str[INET_MAX_ADDRSTRLEN + 4];

  rc = ip_prefix_ntop_short(remote_address, remote_address_str,
                            sizeof(remote_address_str));
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR;
    goto ERR;
  }

  rc = ops.get_output_ifid(remote_address_str, remote_address->family,
                           &interface_id);
  if (rc < 0 || interface_id == 0) {
    rc = HICN_SOCKET_ERROR_BIND_REMOTE_INTERFACE;
    goto ERR;
  }

  /* Local ip */
  rc = ops.get_ip_addr(interface_id, remote_address->family, local_address);
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_BIND_REMOTE_NETMASK;
    goto ERR;
  }

ERR:
  return rc;
}

/**
 *
 * sets socket->interface_id
 */
int hicn_set_remote_endpoint(hicn_socket_t *socket,
                             const char *remote_ip_address) {
  int af, rc = HICN_SOCKET_ERROR_NONE;
  ip_prefix_t addr;

  af = get_addr_family(remote_ip_address);
  if ((af != AF_INET6) && (af != AF_INET)) {
    return HICN_SOCKET_ERROR_INVALID_IP_ADDRESS;
  }

  /* Bind local endpoint if not done yet */
  if (ip_prefix_empty(&socket->connection.tun_ip_address)) {
    char local_ip_address[INET_MAX_ADDRSTRLEN + 4];

    /* Local interface id */
    // INFO("Getting interface_id from gateway IP address %s",
    // remote_ip_address);
    /////
    int addr_family = get_addr_family(remote_ip_address);
    if (addr_family < 0) {
      rc = addr_family;
      goto ERR;
    }

    rc = ops.get_output_ifid(remote_ip_address, (uint8_t)addr_family,
                             &socket->connection.interface_id);
    if (rc < 0 || socket->connection.interface_id == 0) {
      rc = HICN_SOCKET_ERROR_BIND_REMOTE_INTERFACE;
      goto ERR;
    }

    /* Local ip */
    rc = ops.get_ip_addr(socket->connection.interface_id, (uint8_t)addr_family,
                         &addr);
    if (rc < 0) {
      rc = HICN_SOCKET_ERROR_BIND_REMOTE_NETMASK;
      goto ERR;
    }
    /////

    /* Convert to representation format */
    rc =
        ip_prefix_ntop_short(&addr, local_ip_address, sizeof(local_ip_address));
    if (rc < 0) {
      rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR;
      goto ERR;
    }

    rc = hicn_set_local_endpoint(socket, local_ip_address, true);
    if (rc < 0) {
      switch (rc) {
        case HICN_SOCKET_ERROR_SOCKET_LOCAL_NULL_ADDRESS:
          rc = HICN_SOCKET_ERROR_BIND_REMOTE_LOCAL_NULL_ADDR;
          break;
        case HICN_SOCKET_ERROR_SOCKET_LOCAL_REPR:
          rc = HICN_SOCKET_ERROR_BIND_REMOTE_LOCAL_REPR;
          break;
        case HICN_SOCKET_ERROR_SOCKET_LOCAL_HEURISTIC:
          rc = HICN_SOCKET_ERROR_BIND_REMOTE_LOCAL_HEURISTIC;
          break;
        case HICN_SOCKET_ERROR_SOCKET_LOCAL_SET_TUN_IP:
          rc = HICN_SOCKET_ERROR_BIND_REMOTE_LOCAL_SET_TUN_IP;
          break;
      }
      goto ERR;
    }
  }
  return HICN_SOCKET_ERROR_NONE;

ERR:
  return rc;
}

/**
 *
 * We need at least an identifier.
 */
int hicn_socket(hicn_socket_helper_t *hicn, const char *identifier,
                const char *local_ip_address) {
  int rc;

  hicn_socket_t *socket = hicn_socket_create();
  if (!socket) {
    rc = -5;
    goto ERR_SOCKET;
  }

  ops.get_tun_name(hicn->conf->identifier, identifier, socket->tun_name);

  // register the hicn face on which to bind prefixes, create the in/out TUN
  // device
  socket->fd = ops.tun_create(socket->tun_name);
  if (socket->fd <= 0) {
    rc = -2;
    goto ERR_TUN;
  }

  // INFO("Successfully created listener on TUN device %s", socket->tun_name);

  /* Retrieve interface id */
  socket->tun_id = ops.get_ifid(socket->tun_name);
  if (socket->tun_id < 0) {
    rc = -3;
    goto ERR_TUNIFID;
  }
  // INFO("Interface id=%d", socket->tun_id);

  // WARN("Need to set offload");

  // INFO("Setting interface up");
  rc = ops.up_if(socket->tun_id);
  if (rc < 0) {
    rc = -4;
    goto ERR_UP;
  }

  /* Update state */
  rc = hicn_socket_add(hicn, socket);
  if (rc < 0) {
    rc = -5;
    goto ERR_ADD;
  }

  rc = hicn_set_local_endpoint(socket, local_ip_address, true);
  if (rc < 0) {
    rc = -6;
    goto ERR_ADJACENCY;
  }

  return socket->fd;

ERR_ADJACENCY:
ERR_ADD:
ERR_UP:
ERR_TUNIFID:
ERR_TUN:
  free(socket);
ERR_SOCKET:
  // ERR_PARAMS:
  return rc;
}

int hicn_listen(hicn_socket_helper_t *hicn, int fd, const char *prefix) {
  int rc;
  hicn_socket_t *socket = hicn_socket_find(hicn, fd);
  if (!socket) {
    return -1;
  }

  /* Check socket is not a connection */
  if (socket->type == HS_CONNECTION) {
    return -1;
  }

  rc = ops.add_in_route_s(prefix, socket->tun_id);
  if (rc < 0) {
    return rc;
  }

  ip_prefix_t ip_prefix;
  rc = ip_prefix_pton(prefix, &ip_prefix);
  if (rc < 0) {
    return rc;
  }

  // ip -6 rule add from b001::/16 prio 0 table 100
  socket->connection.table_id =
      socket->tun_id % MAX_TABLES;  // this table should be unused

  if (punting_table_id == -1) punting_table_id = socket->connection.table_id;

  rc = ops.add_prio_rule(&ip_prefix, ip_prefix.family, 0,
                         socket->connection.table_id);
  if (rc < 0) {
    return rc;
  }

  rc = strcpy_s(rules_to_remove[rules_counter].tun_name,
                sizeof(rules_to_remove[rules_counter].tun_name), "NONE");
  if (rc != EOK) return -1;

  rules_to_remove[rules_counter].prefix = ip_prefix;
  rules_to_remove[rules_counter].address_family = ip_prefix.family;
  rules_to_remove[rules_counter].table_id = socket->connection.table_id;
  rules_to_remove[rules_counter].priority = 0;
  ++rules_counter;

  /* Update socket upon success */
  socket->type = HS_LISTENER;

  return 0;
}

/**
 *
 * We can pass all adjacency parameters but identifier
 */
int hicn_bind(hicn_socket_helper_t *hicn, int fd,
              const char *remote_ip_address) {
  // uint32_t interface_id;
  int rc = HICN_SOCKET_ERROR_NONE;

  hicn_socket_t *socket = hicn_socket_find(hicn, fd);
  if (!socket) {
    rc = HICN_SOCKET_ERROR_BIND_SOCKET_NOT_FOUND;
    goto ERR;
  }

  /* We allow reuse */
  if (socket->type == HS_CONNECTION) return rc;

  /* Check socket is not a connection */
  if (socket->type != HS_UNSPEC) {
    rc = HICN_SOCKET_ERROR_BIND_SOCKET_ALREADY_BOUND;
    goto ERR;
  }
  socket->type = HS_CONNECTION;

  // each connection is associated a table id, let's take it equal to the
  // tun ID by default (% MAX_TABLES, assuming TUN IDs do not overlap modulo
  // 256...).
  socket->connection.table_id =
      socket->tun_id % MAX_TABLES;  // interface_id; // ops.get_free_table_id();

  rc = hicn_set_remote_endpoint(socket, remote_ip_address);
  if (rc < 0) {
    goto ERR;
  }

  // rule
  // ip -6 rule from all iif eth0 lookup 200
  // INFO("Adding output rule for %s in table %d", socket->tun_name,
  //        socket->connection.table_id);
  int addr_family = get_addr_family(remote_ip_address);
  if (addr_family < 0) {
    rc = addr_family;
    goto ERR;
  }

  rc = ops.add_rule(socket->tun_name, (uint8_t)addr_family,
                    socket->connection.table_id);
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_BIND_RULE;
    goto ERR;
  }

  rc = strcpy_s(rules_to_remove[rules_counter].tun_name,
                sizeof(rules_to_remove[rules_counter].tun_name),
                socket->tun_name);
  if (rc != EOK) return -1;
  rules_to_remove[rules_counter].address_family = addr_family;
  rules_to_remove[rules_counter].table_id = socket->connection.table_id;
  ++rules_counter;

  // route
  // ip -6 route add default via 2002::2 table 28
  // INFO("Adding output route in table %d via gateway %s",
  // socket->connection.table_id,
  //        remote_ip_address);

  // if the address is an IPv6 and start with fe80 we need to specify the device
  // in the route
  u32 default_interface = ~0;
  if (addr_family == AF_INET6 && strncmp(LOCAL_IPV6_PREFIX, remote_ip_address,
                                         strlen(LOCAL_IPV6_PREFIX)) == 0) {
    rc = ops.get_output_ifid(remote_ip_address, (uint8_t)addr_family,
                             &default_interface);
    if (rc < 0) {
      goto ERR;
    }
  }

  rc = ops.add_out_route(remote_ip_address, (uint8_t)addr_family,
                         socket->connection.table_id, default_interface);
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_BIND_ROUTE;
    goto ERR;
  }

  rc = strcpy_s(routes_to_remove[routes_counter].remote_ip_address,
                sizeof(rules_to_remove[rules_counter].tun_name),
                remote_ip_address);
  if (rc != EOK) {
    rc = HICN_SOCKET_ERROR_UNSPEC;
    goto ERR;
  }
  routes_to_remove[routes_counter].table_id = socket->connection.table_id;
  routes_to_remove[routes_counter].address_family = (uint8_t)addr_family;
  ++routes_counter;

  // add route for data
  // ip -6 route add 0:1::/64 dev hicn-if0 table 100
  // this routes are deleted by removing the tun interfaces

  if (punting_table_id == -1) {
    // the punting_table_id was not initialized beacause no main-tun was created
    // we use as an id (socket->tun_id - 1) % MAX_TABLES, so that we will hava a
    // collision only after 255 new interfaces
    punting_table_id = (socket->tun_id - 1) % MAX_TABLES;
  }
  rc = ops.add_in_route_table(&socket->connection.tun_ip_address,
                              socket->tun_id, punting_table_id);
  if (rc < 0) {
    rc = HICN_SOCKET_ERROR_BIND_ROUTE;
    goto ERR;
  }

ERR:
  return rc;
}