-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrtr.c
553 lines (470 loc) · 13.8 KB
/
rtr.c
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
#include "lib.h"
#include "db.h"
#include "udp.h"
#ifdef OPENLISP
#include "plugin_openlisp.h"
#include <net/lisp/lisp.h>
#include <net/lisp/maptables.h>
#endif
int
search_rloc_cmp(void *rloc, void *entry)
{
struct map_entry *me = (struct map_entry *)entry;
if (!me || !rloc)
return -1;
return addrcmp(rloc, &me->rloc);
}
int
rtr_forward_map_register(struct pk_req_entry *pke)
{
/* include ECM header in pke buffer */
pke->buf_len += (uint8_t *)pke->buf - (uint8_t *)pke->lh;
pke->buf = pke->lh;
/* set ECM header bits */
pke->lh->R = 0;
pke->lh->N = 1;
cp_log(LDEBUG, "Forward ECMed Map-Register to %s:%d\n",
sk_get_ip(&pke->ih_di, ip), sk_get_port(&pke->ih_di));
/* select socket for ds */
if (pke->ih_di.sa.sa_family != AF_INET) {
cp_log(LDEBUG, "unsupported address family\n");
return -1;
}
if (sendto(skfd, pke->lh, pke->buf_len, 0,
&pke->ih_di.sa, sizeof(struct sockaddr_in)) == -1) {
cp_log(LLOG, "sendto error: %s\n", strerror(errno));
return -1;
}
return 0;
}
int
rtr_process_map_register(struct pk_req_entry *pke)
{
struct map_register_hdr *lcm = (struct map_register_hdr *)pke->buf;
union map_register_record_generic *rec;
uint8_t *xtr_id;
struct db_table *db;
struct db_node *mapping;
struct prefix eid;
struct mapping_flags flags;
union map_register_locator_generic *loc;
uint8_t lcount;
size_t len;
int local_rloc_pass = 0;
struct list_entry_t *le, *le_tmp;
struct map_entry *me;
if (lcm->record_count > 1) {
cp_log(LDEBUG, "ECMed Map_Register should have only one mapping record\n");
return -1;
}
if (!lcm->I) {
cp_log(LDEBUG, "ECMed Map_Register record should have an xTR-ID\n");
return -1;
}
xtr_id = (uint8_t *)pke->buf + pke->buf_len - (16 + 8); /* 129bits xTR-ID + 64bits site-ID */
rec = (union map_register_record_generic *)CO(lcm,
sizeof(*lcm) + ntohs(lcm->auth_data_length));
/* get EID-prefix */
memset(&eid, 0, sizeof(eid));
switch (ntohs(rec->record.eid_prefix_afi)) {
case LISP_AFI_IP:
eid.family = AF_INET;
eid.u.prefix4 = rec->record.eid_prefix;
break;
case LISP_AFI_IPV6:
eid.family = AF_INET6;
eid.u.prefix6 = rec->record6.eid_prefix;
break;
default:
cp_log(LDEBUG, "unsuported address family\n");
return -1;
}
eid.prefixlen = rec->record.eid_mask_len;
/* find node if it already exists */
db = ms_get_db_table(ms_db, &eid);
mapping = db_node_match_prefix(db, &eid);
if (mapping) {
while (mapping != db->top && !ms_node_is_type(mapping, _MAPP))
mapping = mapping->parent;
}
if (!mapping || mapping == db->top) {
/* create new mapping */
memset(&flags, 0, sizeof(flags));
flags.act = rec->record.act;
flags.A = rec->record.a;
flags.version = rec->record.version;
flags.ttl = ntohl(rec->record.ttl);
flags.referral = 0;
flags.proxy = lcm->proxy_map_reply;
flags.range = _MAPP;
mapping = generic_mapping_new(&eid);
if (!mapping) {
cp_log(LLOG, "failed to create new mapping\n");
return -1;
}
cp_log(LDEBUG, "Creat new mapping for EID prefix %s\n",
prefix2str(&eid));
generic_mapping_set_flags(mapping, &flags);
}
lcount = rec->record.locator_count;
len = _get_reply_record_size(rec);
loc = (union map_register_locator_generic *)CO(rec, 0);
while (len? lcount-- : lcount) {
union sockunion rloc;
uint8_t R;
loc = (union map_register_locator_generic *)CO(loc, len);
len = sizeof(struct map_register_locator);
if (ntohs(loc->rloc.rloc_afi) != LISP_AFI_IP) {
cp_log(LDEBUG, "Unsupporte AFI for an ETR behind a NAT\n");
return -1;
}
/* Reachable locator that is no this RTR's RLOC, skip */
if (memcmp(&pke->di.sin.sin_addr, &loc->rloc.rloc,
sizeof(struct in_addr)) != 0 && loc->rloc.R)
continue;
/* do two pass for the RTR's RLOC : first for the implicit
* ETR's natted RLOC then for the RTR's local RLOC */
if (loc->rloc.R && local_rloc_pass == 0) {
memcpy(&rloc.sin.sin_addr, &pke->ih_si.sin.sin_addr,
sizeof(struct in_addr));
local_rloc_pass ++;
len = 0;
R = 0;
} else {
memcpy(&rloc.sin.sin_addr, &loc->rloc.rloc,
sizeof(struct in_addr));
R = loc->rloc.R;
}
rloc.sin.sin_family = AF_INET;
/* search for an already existing entry for this rloc */
le = list_search(mapping->info, &rloc, search_rloc_cmp);
if (le) {
me = le->data;
if (memcmp(me->xtr_id, xtr_id, sizeof(me->xtr_id)) == 0) {
me->nonce = ntohll(lcm->nonce);
me->unverified = 1;
/* do not update priority and weight with
* implicit ETR's natted RLOC */
if (len == 0)
continue;
me->priority = loc->rloc.priority;
me->weight = loc->rloc.weight;
me->m_priority = loc->rloc.m_priority;
me->m_weight = loc->rloc.m_weight;
cp_log(LDEBUG, "Update RLOC: %s, priority=%u, weight=%u, m_priority=%u, m_weight=%u\n",
sk_get_ip(&me->rloc, ip), me->priority,
me->weight, me->m_priority, me->m_weight);
}
continue;
}
/* Do not create an entry for unreachable RLOCs not issuing this
* Map-reguister */
if (memcmp(&pke->ih_si.sin.sin_addr, &rloc.sin.sin_addr,
sizeof(struct in_addr)) != 0 && !loc->rloc.R)
continue;
me = calloc(1, sizeof(*me));
if (!me) {
cp_log(LLOG, "memory allocation failed: %s\n",
strerror(errno));
return -1;
}
memcpy(&me->rloc, &rloc, sizeof(rloc));
me->priority = loc->rloc.priority;
me->weight = loc->rloc.weight;
me->m_priority = loc->rloc.m_priority;
me->m_weight = loc->rloc.m_weight;
me->r = R;
me->L = loc->rloc.L;
me->p = loc->rloc.p;
me->unverified = 1;
me->natted = 1;
memcpy(&me->nat_rloc, &pke->si, sizeof(me->nat_rloc));
memcpy(&me->rtr_rloc, &pke->di, sizeof(me->rtr_rloc));
me->nonce = ntohll(lcm->nonce);
if (xtr_id)
memcpy(me->xtr_id, xtr_id, sizeof(me->xtr_id));
list_insert(mapping->info, me, NULL);
cp_log(LDEBUG, "Add new RLOC: %s, priority=%u, weight=%u, m_priority=%u, m_weight=%u, r=%d, L=%d, p=%d\n",
sk_get_ip(&me->rloc, ip), me->priority, me->weight,
me->m_priority, me->m_weight, me->r, me->L, me->p);
}
/* purge not updated rlocs associated with this xTR (xTR-ID) */
le = ((struct list_t *)mapping->info)->head.next;
while(le != &((struct list_t *)mapping->info)->tail) {
me = le->data;
le_tmp = le;
le = le->next;
if (memcmp(me->xtr_id, xtr_id, sizeof(me->xtr_id)) == 0 &&
!me->unverified) {
cp_log(LDEBUG, "Remove deprecated RLOC: %s\n",
sk_get_ip(&me->rloc, ip));
list_remove(mapping->info, le_tmp, NULL);
free(me);
}
}
return rtr_forward_map_register(pke);
}
int
rtr_send_data_map_notify(struct pk_req_entry *pke, union sockunion *dest)
{
struct lisp_data_hdr ldh;
unsigned int instance_id = DATA_MAP_NOTIFY_INSTANCE_ID;
uint8_t *buf;
size_t buf_len;
char ip2[INET6_ADDRSTRLEN];
/* prepare lisp data header*/
memset(&ldh , 0, sizeof(ldh));
ldh.I = 1;
memcpy(ldh.instance_id, &instance_id, sizeof(ldh.instance_id));
/* encapsulate map-notify in the lisp_data_header */
buf = build_encap_pkt(pke->buf, pke->buf_len, &ldh, sizeof(ldh),
&pke->ih_si, &pke->ih_di, &buf_len);
if (!buf)
return -1;
cp_log(LDEBUG, "Forward ECMed Map-Notify to %s:%d via %s:%d\n",
sk_get_ip(&pke->ih_di, ip), sk_get_port(&pke->ih_di),
sk_get_ip(dest, ip2), sk_get_port(dest));
/* select socket */
if (dest->sa.sa_family != AF_INET) {
cp_log(LDEBUG, "unsupported address family\n");
free(buf);
return -1;
}
if (sendto(skfd, buf, buf_len, 0, &dest->sa,
sizeof(struct sockaddr_in)) == -1) {
cp_log(LLOG, "sendto error: %s\n", strerror(errno));
free(buf);
return -1;
}
free(buf);
return 0;
}
int
rtr_opl_add_rloc(void *buf, struct db_node *mapp)
{
#ifdef OPENLISP
void *mcm;
struct map_msghdr *mhdr;
struct list_t *rl_list = (struct list_t *)mapp->info;
struct list_entry_t *rl_entry;
struct map_entry *rl;
struct rloc_mtx *mx;
union sockunion *rloc, *skp;
if (!rl_list || rl_list->count == 0) {
cp_log(LLOG, "mapping should at least have on associated rloc\n");
return -1;
}
mhdr = (struct map_msghdr *)buf;
mhdr->map_rloc_count = 0;
mcm = CO(buf, mhdr->map_msglen);
rl_entry = rl_list->head.next;
while (rl_entry != &rl_list->tail) {
rl = (struct map_entry *)rl_entry->data;
uint16_t flags = 0;
if (rl->natted && rl->r) { /* RTR RLOC */
/* add local RTR RLOC */
/*rloc = &rl->rtr_rloc;
flags |= RLOCF_LIF | RLOCF_UP;*/
rl_entry = rl_entry->next;
continue;
} else if (rl->natted && !rl->r) { /* Natted RLOC */
/* add translated global RLOC (with priority) */
rloc = &rl->nat_rloc;
flags |= RLOCF_UP;
} else {
/* add RLOC */
rloc = &rl->rloc;
flags |= rl->r? RLOCF_UP:0;
}
skp = mcm;
memcpy(skp, rloc, SA_LEN(rloc->sa.sa_family));
skp->sa.sa_len = SA_LEN(rloc->sa.sa_family);
mx = (struct rloc_mtx *)CO(skp, SS_LEN(skp));
mx->priority = rl->priority;
mx->weight = rl->weight;
mx->flags |= flags;
mcm = CO(mx, sizeof(struct rloc_mtx));
mhdr->map_rloc_count++;
rl_entry = rl_entry->next;
}
mhdr->map_msglen = (char *)mcm - (char *)mhdr;
return mhdr->map_msglen;
#else
return 0;
#endif
}
int
rtr_opl_update(struct db_node *mapp)
{
#ifdef OPENLISP
static int sock = -1;
void *buf;
int buf_len;
if (sock < 0) {
sock = socket(PF_MAP, SOCK_RAW, 0);
if (sock < 0) {
cp_log(LLOG, "could not open mapping socket: %s\n",
strerror(errno));
return -1;
}
}
/* Delete mapping */
buf = opl_new_msg(MAPM_VERSION, MAPM_DELETE, MAPF_UP, MAPA_EID);
buf_len = opl_add_mapp(buf, mapp);
if (buf_len <= 0) {
free(buf);
return -1;
}
cp_log(LLOG, "Delete eid %s mapping from cache\n", prefix2str(&mapp->p));
/* send to openlisp data-plane */
if (write(sock, buf, buf_len) < 1 && errno != ESRCH) {
cp_log(LLOG, "failed to delete mapping from cache");
opl_errno(errno);
free(buf);
return -1;
}
/* Add mapping */
if (!mapp->info) {
cp_log(LLOG, "no rloc associated with this mapping\n");
return -1;
}
buf = opl_new_msg(MAPM_VERSION, MAPM_ADD, MAPF_UP,
MAPA_EID | MAPA_RLOC);
if (opl_add_mapp(buf, mapp) <= 0) {
free(buf);
return -1;
}
buf_len = rtr_opl_add_rloc(buf, mapp);
if (buf_len <= 0) {
free(buf);
return -1;
}
cp_log(LLOG, "Add mapping for eid %s to cache\n", prefix2str(&mapp->p));
/* send to openlisp data-plane */
if (write(sock, buf, buf_len) < 0 ) {
cp_log(LLOG, "failed to add mapping to cache");
opl_errno(errno);
free(buf);
return -1;
}
free(buf);
#endif
return 0;
}
int
rtr_process_map_notify(struct pk_req_entry *pke)
{
struct map_notify_hdr *lcm = (struct map_notify_hdr *)pke->buf;
union map_notify_record_generic *rec;
uint8_t *xtr_id;
struct db_table *db;
struct db_node *mapping;
struct prefix eid;
union map_notify_locator_generic *loc;
union sockunion *dest = NULL;
uint8_t lcount;
size_t len;
int local_rloc_pass = 0;
struct map_entry *me;
struct list_entry_t *le, *le_tmp;
if (lcm->record_count > 1) {
cp_log(LDEBUG, "ECMed Map_Notify should have only one mapping record\n");
return -1;
}
if (!lcm->I) {
cp_log(LDEBUG, "ECMed Map_Notify record should have an xTR-ID\n");
return -1;
}
xtr_id = (uint8_t *)pke->buf + pke->buf_len - (16 + 8); /* 128bits xTR-ID + 64bits site-ID */
rec = (union map_notify_record_generic *)CO(lcm,
sizeof(*lcm) + ntohs(lcm->auth_data_length));
/* get EID-prefix */
memset(&eid, 0, sizeof(eid));
switch (ntohs(rec->record.eid_prefix_afi)) {
case LISP_AFI_IP:
eid.family = AF_INET;
eid.u.prefix4 = rec->record.eid_prefix;
break;
case LISP_AFI_IPV6:
eid.family = AF_INET6;
eid.u.prefix6 = rec->record6.eid_prefix;
break;
default:
cp_log(LDEBUG, "unsuported address family\n");
return -1;
}
eid.prefixlen = rec->record.eid_mask_len;
/* find node */
db = ms_get_db_table(ms_db, &eid);
mapping = db_node_match_prefix(db, &eid);
if (mapping) {
while (mapping != db->top && !ms_node_is_type(mapping, _MAPP))
mapping = mapping->parent;
}
if(!mapping || mapping == db->top) {
cp_log(LDEBUG, "no mapping found for EID prefix %s\n",
prefix2str(&eid));
return -1;
}
lcount = rec->record.locator_count;
len = _get_reply_record_size(rec);
loc = (union map_notify_locator_generic *)CO(rec, 0);
while (len? lcount-- : lcount) {
struct list_entry_t *le;
union sockunion rloc;
loc = (union map_notify_locator_generic *)CO(loc, len);
len = sizeof(struct map_register_locator);
if (ntohs(loc->rloc.rloc_afi) != LISP_AFI_IP) {
cp_log(LDEBUG, "Unsupporte AFI for an ETR behind a NAT\n");
return -1;
}
/* Reachable locator that is no this RTR's RLOC, skip */
if (memcmp(&pke->di.sin.sin_addr, &loc->rloc.rloc,
sizeof(struct in_addr)) != 0 && loc->rloc.R)
continue;
/* do two pass for the RTR's RLOC : first for the implicit
* ETR's natted RLOC then for the RTR's local RLOC */
if (loc->rloc.R && local_rloc_pass == 0) {
memcpy(&rloc.sin.sin_addr, &pke->ih_di.sin.sin_addr,
sizeof(struct in_addr));
local_rloc_pass ++;
len = 0;
} else {
memcpy(&rloc.sin.sin_addr, &loc->rloc.rloc,
sizeof(struct in_addr));
}
rloc.sin.sin_family = AF_INET;
le = list_search(mapping->info, &rloc, search_rloc_cmp);
if (!le)
continue;
me = le->data;
if (memcmp(me->xtr_id, xtr_id, sizeof(me->xtr_id)) != 0 ||
me->nonce != ntohll(lcm->nonce))
continue;
me->unverified = 0;
/* global translated RLOC */
if (len == 0)
dest = &me->nat_rloc;
}
/* purge unverified rlocs of this xTR (xTR-ID) */
le = ((struct list_t *)mapping->info)->head.next;
while(le != &((struct list_t *)mapping->info)->tail) {
me = le->data;
le_tmp = le;
le = le->next;
if (memcmp(me->xtr_id, xtr_id, sizeof(me->xtr_id)) == 0 &&
me->unverified) {
cp_log(LDEBUG, "Remove unverified RLOC: %s",
sk_get_ip(&me->rloc, ip));
list_remove(mapping->info, le_tmp, NULL);
free(me);
}
}
if (!dest) {
cp_log(LDEBUG, "could not found destination nat rloc\n");
return -1;
}
if (rtr_opl_update(mapping))
return -1;
return rtr_send_data_map_notify(pke, dest);
}