-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlispd_iface_mgmt.c
More file actions
1744 lines (1536 loc) · 54.3 KB
/
Copy pathlispd_iface_mgmt.c
File metadata and controls
1744 lines (1536 loc) · 54.3 KB
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* lispd_iface_mgmt.c
*
* This file is part of LISP Mobile Node Implementation.
* Netlink support and related routines for interface management.
*
* Copyright (C) 2011 Cisco Systems, Inc, 2011. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Please send any bug reports or fixes you make to the email address(es):
* LISP-MN developers <devel@lispmob.org>
*
* Written or modified by:
* Preethi Natarajan <prenatar@cisco.com>
* Vijay Subramanian <vijaynsu@cisco.com>
* Pere Monclus <pmonclus@cisco.com>
* Lorand Jakab <ljakab@ac.upc.edu>
* Pranathi Mamidi <pranathi.3961@gmail.com>
*
*/
#include "lispd_iface_mgmt.h"
#include "lispd_external.h"
#include "lispd_smr.h"
/*
* This function sends a netlink message
* to the kernel
*/
static int nlsock_talk(n)
struct nlmsghdr *n;
{
struct sockaddr_nl nladdr;
/*
* Set the netlink socket addr details so that the message
* is received by the kernel
*/
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pad = 0;
nladdr.nl_pid = 0; // destination == kernel
nladdr.nl_groups = 0;
/*
* Package the netlink msg inside an iovec
*/
struct iovec iov = {
(void *)n, // actual vector to be txd
n->nlmsg_len // length of vector
};
struct msghdr msg = {
(void *)&nladdr, // destination
sizeof(nladdr), // destination length
&iov, // Vector data
1, // Vector data len
NULL, // Ancillary data
0, // Ancillary data len
0 // falgs
};
if(sendmsg(nlh.fd, &msg, 0) < 0) {
syslog (LOG_DAEMON, "sendmsg (nlsock_talk()) failed: %s\n",
strerror(errno));
return 0;
}
return 1;
}
/*
* This function populates the tail of a netlink msg
* with new rtattr struct
*/
static int addattr_l(n, maxlen, type, data, alen)
struct nlmsghdr *n; // the netlink msg header
int maxlen; // max length of the netlink msg
int type; // RTA attr type of the data
void *data; // data to add
int alen; // data length
{
int len = RTA_LENGTH(alen);
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
syslog(LOG_DAEMON, "Align issue (addattr_l): netlink msg buf too small for data\n");
return 0;
}
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
return 1;
}
/*
* As a result of this function, the kernel will send a RTM_NEWROUTE
* message for each of its routing entries
*/
int dump_routing_table(afi, table)
uint16_t afi;
int table;
{
#ifdef DEBUG
syslog(LOG_DAEMON, "RTNETLINK: ip route list table %d", table);
#endif
return(route_mod(RTM_GETROUTE, afi, NULL, 0, NULL, 0, NULL, NULL, table, 0, 0));
}
/*
* This function deletes a specific route
*/
static int route_del(afi, src, src_plen,
dst, dst_plen,
gateway, device_id, table)
uint16_t afi;
lisp_addr_t *src;
int src_plen;
lisp_addr_t *dst;
int dst_plen;
lisp_addr_t *gateway;
int device_id;
int table;
{
#ifdef DEBUG
char buf[BUF_SIZE];
char tmp[BUF_SIZE];
sprintf(buf, "RTNETLINK: ip route del");
if (dst == NULL)
strcat(buf, " default");
if (table != RT_TABLE_MAIN) {
sprintf(tmp, " table %d", table);
strcat(buf, tmp);
}
syslog(LOG_DAEMON, "%s", buf);
#endif
return (route_mod(RTM_DELROUTE, afi,
src, src_plen,
dst, dst_plen,
gateway, device_id, table, 0, 0));
}
/*
* This function adds a specific route
*/
int route_add(afi, src, src_plen,
dst, dst_plen,
gateway, device_id, table, metric, realm)
uint16_t afi;
lisp_addr_t *src;
int src_plen;
lisp_addr_t *dst;
int dst_plen;
lisp_addr_t *gateway;
int device_id;
int table;
int metric;
int realm;
{
#ifdef DEBUG
char buf[BUF_SIZE];
char tmp[BUF_SIZE];
sprintf(buf, "RTNETLINK: ip route add");
if (dst == NULL)
strcat(buf, " default");
if (table != RT_TABLE_MAIN) {
sprintf(tmp, " table %d", table);
strcat(buf, tmp);
}
syslog(LOG_DAEMON, "%s", buf);
#endif
return (route_mod(RTM_NEWROUTE, afi,
src, src_plen,
dst, dst_plen,
gateway, device_id, table, metric, realm));
}
/*
* This function modifies (add/del) a route via
* netlink
*/
int route_mod(cmd, afi, src, src_plen, dst, dst_plen,
gateway, device_id, table, metric, realm)
int cmd; /* add or del */
uint16_t afi; /* IPv4 or IPv6 routing table */
lisp_addr_t *src; /* src address */
int src_plen; /* src addr prefix length */
lisp_addr_t *dst; /* dst address */
int dst_plen; /* dst addr prefix length */
lisp_addr_t *gateway; /* gateway addr */
int device_id; /* outgoing iface id */
int table; /* routing table number */
int metric; /* route metric (priority) */
int realm; /* route realm */
{
reqmsg_t req;
int attr_size =0;
memset(&req, 0, sizeof(req));
/*
* Fill up the netlink msg with appropriate flags and data
*/
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.n.nlmsg_type = cmd;
req.n.nlmsg_flags = NLM_F_REQUEST;
if (cmd == RTM_NEWROUTE) {
req.n.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
}
if (cmd == RTM_GETROUTE) {
req.n.nlmsg_flags |= NLM_F_DUMP;
}
req.n.nlmsg_seq = ++nlh.seq;
req.n.nlmsg_pid = getpid();
req.r.rtm_table = table;
req.r.rtm_protocol = RTPROT_BOOT;
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
req.r.rtm_type = RTN_UNICAST;
req.r.rtm_src_len = src_plen;
req.r.rtm_dst_len = dst_plen;
req.r.rtm_family = afi;
if (gateway)
req.r.rtm_family = gateway->afi;
if(device_id)
addattr_l(&req.n, sizeof(req), RTA_OIF, &device_id,
sizeof(int));
if(metric)
addattr_l(&req.n, sizeof(req), RTA_PRIORITY, &metric,
sizeof(int));
if(realm)
addattr_l(&req.n, sizeof(req), RTA_FLOW, &realm,
sizeof(int));
if(src) {
attr_size = ((src->afi == AF_INET6) ?
sizeof(struct in6_addr) : sizeof(struct in_addr));
addattr_l(&req.n, sizeof(req), RTA_DST,
&(src->address), attr_size);
}
if(dst) {
attr_size = ((dst->afi == AF_INET6) ?
sizeof(struct in6_addr) : sizeof(struct in_addr));
addattr_l(&req.n, sizeof(req), RTA_DST,
&(dst->address), attr_size);
}
if(gateway) {
attr_size = ((gateway->afi == AF_INET6) ?
sizeof(struct in6_addr) : sizeof(struct in_addr));
addattr_l(&req.n, sizeof(req), RTA_GATEWAY,
&(gateway->address), attr_size);
}
/*
* Send netlink msg to kernel
*/
if (!nlsock_talk(&req.n)) {
syslog (LOG_DAEMON, "nlsock_talk (route_mod()) failed\n");
return (0);
}
return 1;
}
/*
* This function parses netlink error messages
*/
static void parse_nl_error(nlHdr)
struct nlmsghdr *nlHdr;
{
struct nlmsgerr *nlErr;
nlErr = (struct nlmsgerr *)NLMSG_DATA(nlHdr);
#ifndef DEBUG
if(nlErr->error)
#endif
syslog(LOG_DAEMON, "RTNETLINK answers: %s\n",
strerror(-nlErr->error));
}
/*
* This function parses and gathers information from
* netlink route messages
*/
static iface_list_elt *parse_nl_route (nlHdr, gateway, dev, metric, realm)
struct nlmsghdr * nlHdr;
lisp_addr_t * gateway;
int * dev;
int * metric;
int * realm;
{
struct rtmsg *rt;
struct rtattr *rtAttr;
int rtLen;
char tempBuf[BUF_SIZE];
iface_list_elt *iface_elt = NULL;
switch (nlHdr->nlmsg_type) {
case RTM_NEWROUTE:
sprintf(tempBuf, "Parsing RTM_NEWROUTE Message:\n");
break;
case RTM_DELROUTE:
sprintf(tempBuf, "Parsing RTM_DELROUTE Message:\n");
break;
default:
syslog(LOG_DAEMON,"parse_nl_route(): Unknown message type\n");
return (NULL);
}
rt = (struct rtmsg *)NLMSG_DATA(nlHdr);
if ((rt->rtm_family != AF_INET) && (rt->rtm_family != AF_INET6)) {
syslog(LOG_DAEMON, "parse_nl_route: Unknown adddress family\n");
return NULL;
}
if (rt->rtm_table != RT_TABLE_MAIN) {
/* not interested in routes/gateways affecting
* tables other the main routing table
*/
return NULL;
}
syslog(LOG_DAEMON,"%s", tempBuf);
rtAttr = (struct rtattr *)RTM_RTA(rt);
rtLen = RTM_PAYLOAD(nlHdr);
for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
switch (rtAttr->rta_type) {
case RTA_OIF:
if_indextoname(*(int *)RTA_DATA(rtAttr), tempBuf);
syslog(LOG_DAEMON, " Output interface: %s\n", tempBuf);
iface_elt = search_iface_list(tempBuf);
if(dev)
memcpy(dev, (int *)RTA_DATA(rtAttr), sizeof(int));
break;
case RTA_PRIORITY:
if(metric) {
memcpy(metric, (int *)RTA_DATA(rtAttr), sizeof(int));
syslog(LOG_DAEMON, " Metric: %d\n", *metric);
}
break;
case RTA_FLOW:
if(realm) {
memcpy(realm, (int *)RTA_DATA(rtAttr), sizeof(int));
syslog(LOG_DAEMON, " Realm: %d\n", *realm);
}
break;
case RTA_GATEWAY:
inet_ntop(rt->rtm_family, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, " Gateway address: %s\n", tempBuf);
gateway->afi = rt->rtm_family;
switch (gateway->afi) {
case AF_INET:
memcpy(&(gateway->address),
(struct in_addr *)RTA_DATA(rtAttr),
sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(&(gateway->address),
(struct in6_addr *)RTA_DATA(rtAttr),
sizeof(struct in6_addr));
break;
}
break;
case RTA_DST:
inet_ntop(rt->rtm_family, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, " Destination address: %s\n", tempBuf);
/* We are only interested in default gateway changes */
syslog(LOG_DAEMON, "Not a default route, ignored...");
return NULL;
break;
}
}
return (iface_elt);
}
/*
* This function parses and gathers information from
* netlink address messages
*/
static iface_list_elt *parse_nl_addr(nlHdr, addr)
struct nlmsghdr *nlHdr;
lisp_addr_t *addr;
{
struct ifaddrmsg *ifaddr;
struct rtattr *rtAttr;
int rtLen;
char tempBuf[BUF_SIZE];
iface_list_elt *iface_elt = NULL;
switch (nlHdr->nlmsg_type) {
case RTM_NEWADDR:
sprintf(tempBuf, "Parsing RTM_NEWADDR Message:\n");
break;
case RTM_DELADDR:
sprintf(tempBuf, "Parsing RTM_DELADDR Message:\n");
break;
default:
syslog(LOG_DAEMON, "parse_nl_addr(): Unknown Message Type\n\n");
return NULL;
break;
}
ifaddr = (struct ifaddrmsg *)NLMSG_DATA(nlHdr);
if ((ifaddr->ifa_family != AF_INET) &&
(ifaddr->ifa_family != AF_INET6)) {
syslog(LOG_DAEMON, "parse_nl_addr(): Unknown address family\n");
return NULL;
}
syslog(LOG_DAEMON, "%s", tempBuf);
addr->afi = ifaddr->ifa_family;
rtAttr = (struct rtattr *)IFA_RTA(ifaddr);
rtLen = IFA_PAYLOAD(nlHdr);
for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
switch (rtAttr->rta_type) {
case IFA_LOCAL:
inet_ntop(addr->afi, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, "Local address: %s\n", tempBuf);
break;
case IFA_BROADCAST:
inet_ntop(addr->afi, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, "Broadcast address: %s\n", tempBuf);
break;
case IFA_ANYCAST:
inet_ntop(addr->afi, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, "Anycast address: %s\n", tempBuf);
break;
case IFA_ADDRESS:
inet_ntop(addr->afi, RTA_DATA(rtAttr), tempBuf,
sizeof(tempBuf));
syslog(LOG_DAEMON, "Interface address: %s\n", tempBuf);
switch (addr->afi) {
case AF_INET:
memcpy(&(addr->address),
(struct in_addr *)RTA_DATA(rtAttr),
sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(&(addr->address),
(struct in6_addr *)RTA_DATA(rtAttr),
sizeof(struct in6_addr));
break;
}
break;
case IFA_LABEL:
syslog(LOG_DAEMON, "Interface name: %s\n",
(char *)RTA_DATA(rtAttr));
iface_elt = search_iface_list(
(char *)RTA_DATA(rtAttr));
break;
}
}
return iface_elt;
}
/*
* This function modifies kernel's list of ip rules
*/
static int rule_mod (if_index, cmd,
table, priority, type,
src, src_plen, dst, dst_plen, flags)
int if_index; // interface index
int cmd; // add or del the rule?
uint8_t table; // rule for which routing table?
uint32_t priority; // rule priority
uint8_t type; // type of route
lisp_addr_t *src; // src addr to match
int src_plen; // src addr prefix length
lisp_addr_t *dst; // dst addr to match
int dst_plen; // dst addr prefix length
int flags; // flags, if any
{
uint8_t buf[BUF_SIZE];
struct nlmsghdr *n;
struct rtmsg *rtm;
int attr_size;
memset(buf, 0, sizeof(buf));
n = (struct nlmsghdr *)buf;
/*
* Fill up the netlink message flags and attributes
*/
n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
n->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
if (cmd == RTM_NEWRULE) {
n->nlmsg_flags |= NLM_F_CREATE;
}
n->nlmsg_type = cmd;
n->nlmsg_seq = ++nlh.seq;
n->nlmsg_pid = getpid();
rtm = NLMSG_DATA(n);
if (src) rtm->rtm_family = src->afi; /* assume family == src family */
else rtm->rtm_family = AF_INET;
rtm->rtm_dst_len = dst_plen;
rtm->rtm_src_len = src_plen;
rtm->rtm_table = table;
rtm->rtm_scope = RT_SCOPE_UNIVERSE;
rtm->rtm_type = type;
rtm->rtm_flags = flags;
if(dst) {
attr_size = ((dst->afi == AF_INET6) ?
sizeof(struct in6_addr) : sizeof(struct in_addr));
addattr_l(n, sizeof(buf), RTA_DST, &(dst->address), attr_size);
}
if (src) {
attr_size = ((src->afi == AF_INET6) ?
sizeof(struct in6_addr) : sizeof(struct in_addr));
addattr_l(n, sizeof(buf), RTA_SRC, &(src->address), attr_size);
}
if (priority)
addattr_l(n, sizeof(buf), RTA_PRIORITY, &priority,
sizeof(uint32_t));
if (if_index)
addattr_l(n, sizeof(buf), RTA_IIF, &if_index,
sizeof(int));
/*
* Send the netlink message to kernel
*/
if (!nlsock_talk(n)) {
syslog (LOG_DAEMON, "nlsock_talk (rule_mod()) failed\n");
return (0);
}
return 1;
}
/*
* This function adds a specific ip rule to
* kernel's rule list
*/
static int rule_add(int if_index, uint8_t table,
uint32_t priority, uint8_t type,
lisp_addr_t *src, int src_plen,
lisp_addr_t *dst, int dst_plen, int flags)
{
#ifdef DEBUG
syslog(LOG_DAEMON, "RTNETLINK: ip rule add (...)");
#endif
return rule_mod(if_index, RTM_NEWRULE, table,
priority, type,
src, src_plen, dst, dst_plen, flags);
}
/*
* This function deletes a specific ip rule to
* kernel's rule list
*/
static int rule_del(int if_index, uint8_t table,
uint32_t priority, uint8_t type,
lisp_addr_t *src, int src_plen,
lisp_addr_t *dst, int dst_plen, int flags)
{
#ifdef DEBUG
syslog(LOG_DAEMON, "RTNETLINK: ip rule del (...)");
#endif
return rule_mod(if_index, RTM_DELRULE, table,
priority, type,
src, src_plen, dst, dst_plen, flags);
}
/* This function adds policy routing associated to an interface rloc.
* It is also used to decouple addition of policy rules from
* the population of the table
*/
int setup_source_routing_policy(iface_name, src_rloc, rt_num)
char *iface_name; // outgoing interface
lisp_addr_t *src_rloc; // src address to match
int rt_num;
{
/*
* Step 1:
* add the ip rule for the LISP_MN routing table
* ip rule add from <src_rloc> table <rt_num>
* It does a priory preventive 'cleaning'
*/
if (!rule_del(0, rt_num, LISP_MN_IP_RULE_PRIORITY,
RTN_UNICAST, src_rloc,
((src_rloc->afi == AF_INET6) ? 128 : 32),
NULL, 0, 0 )) {
syslog(LOG_DAEMON, "rule_del (setup_source_routing_policy()) failed\n");
return 0;
}
if (!rule_add(0, rt_num, LISP_MN_IP_RULE_PRIORITY,
RTN_UNICAST, src_rloc,
((src_rloc->afi == AF_INET6) ? 128 : 32),
NULL, 0, 0 )) {
syslog(LOG_DAEMON, "rule_add (setup_source_routing_policy()) failed\n");
return 0;
}
return 1;
}
/*
* This function configures source address based
* policy routing in the kernel
*/
static int setup_source_routing(iface_name, src_rloc, gateway, rt_num)
char *iface_name; // outgoing interface
lisp_addr_t *src_rloc; // src address to match
lisp_addr_t *gateway; // default gateway address
int rt_num; // routing table number
{
//Step 1 moved to another function
/*
* Step 2:
* add the default gateway for this rule
* ip route add default via <gtw> dev <iface> table RT_TABLE_LISP_MN
*/
int if_index = if_nametoindex(iface_name);
if (!route_add(0, NULL, 0,
NULL, 0,
gateway, if_index, rt_num, 0, 0)) {
syslog(LOG_DAEMON, "route_add (setup_source_routing()) failed\n");
return 0;
}
return 1;
}
/* This function deletes the rule and default gateway
* for a particular policy route
*/
static int delete_source_routing(iface_name, src_rloc, gateway,rt_num)
char *iface_name; // outgoing interface
lisp_addr_t *src_rloc; // src address to match
lisp_addr_t *gateway; // default gateway address
int rt_num; // routing table number
{
int if_index = if_nametoindex(iface_name);
/*
* Step 1:
* delete the ip rule for the LISP_MN routing table
* ip rule del from <src_rloc> table RT_TABLE_LISP_MN
*/
if (!rule_del(0, rt_num, LISP_MN_IP_RULE_PRIORITY,
RTN_UNICAST, src_rloc,
((src_rloc->afi == AF_INET6) ? 128 : 32),
NULL, 0, 0 )) {
syslog(LOG_DAEMON, "rule_del (delete_source_routing()) failed\n");
return 0;
}
/*
* Step 2:
* delete the default gateway for this rule
* ip route del default via <gtw> dev <iface> table RT_TABLE_LISP_MN
*/
if (!route_del(0, NULL, 0,
NULL, 0, gateway, if_index, rt_num)) {
syslog(LOG_DAEMON, "route_del (delete_source_routing()) failed\n");
return 0;
}
return 1;
}
/*
* This function deletes the rloc from lispd's
* patricia tree database and updates interface list
*/
int delete_rloc (iface_elt, rloc, node)
iface_list_elt *iface_elt;
lisp_addr_t *rloc;
patricia_node_t *node;
{
lispd_locator_chain_t *locator_chain = NULL;
lispd_db_entry_t *db_entry = NULL;
lispd_locator_chain_elt_t *del_elt = NULL;
lispd_locator_chain_elt_t *prev_elt = NULL;
char *eid = NULL;
char addr_str[MAX_INET_ADDRSTRLEN];
/*
* First find the eid associated with this interface
* How do we know which eid to use -- the v4 or v6 one?
* XXX: Assume eid's afi == rloc's afi
* Then, find the patricia node associated with the eid
*/
if (node == NULL) {
syslog(LOG_DAEMON, "delete_rloc(): EID (%s) not found in database", eid);
free(eid);
return(0);
}
if (node->data == NULL) {
syslog(LOG_DAEMON, "delete_rloc(): NULL locator chain for eid (%s)\n", eid);
free(eid);
return(0);
}
/*
* Find the matching locator_chain_elt
* Note: There can be situations where an interface is
* associated with multiple rlocs. That's why its better
* to search for the excat rloc match instead of the simpler
* match by interface name (locator_chain_elt->locator_name)
*/
locator_chain = (lispd_locator_chain_t *)node->data;
del_elt = locator_chain->head;
prev_elt = locator_chain->head;
while (del_elt) {
if (!(memcmp(&del_elt->db_entry->locator,
&rloc->address, sizeof(lisp_addr_t)))) {
/* Found the matching locator;
* Delete the locator form the locator chain
*/
db_entry = del_elt->db_entry;
if ((del_elt == locator_chain->head) &&
(del_elt == locator_chain->tail)) {
/* single entry in locator chain
*/
locator_chain->head = NULL;
locator_chain->tail = NULL;
}
else {
prev_elt->next = del_elt->next;
if (del_elt == locator_chain->head) {
/* set the new head */
locator_chain->head = del_elt->next;
}
if (del_elt == locator_chain->tail) {
/* set the new tail */
locator_chain->tail = prev_elt;
}
}
locator_chain->locator_count -= 1;
syslog(LOG_DAEMON, "delete_rloc(): %s deleted from interface %s",
inet_ntop(db_entry->locator.afi,
&(db_entry->locator.address), addr_str,
MAX_INET_ADDRSTRLEN),
iface_elt->iface_name);
/*
* Update iface_elt by deleting the corresponding
* db_entry from iface_elt
*/
switch (rloc->afi) {
case AF_INET:
del_item_from_db_entry_list(iface_elt->AF4_locators, db_entry);
break;
case AF_INET6:
del_item_from_db_entry_list(iface_elt->AF6_locators, db_entry);
break;
}
free(db_entry);
free(del_elt->locator_name);
free(del_elt);
free (eid);
return (1); // success
}
prev_elt = del_elt;
del_elt = del_elt->next;
}
/* we didn't find the locator */
syslog(LOG_DAEMON, "delete_rloc(): %s not found in patricia tree\n",
inet_ntop (rloc->afi,
&(rloc->address), addr_str,
MAX_INET_ADDRSTRLEN));
free(eid);
return(0);
}
lispd_db_entry_t *add_rloc (iface_elt, rloc, node, eid)
iface_list_elt *iface_elt;
lisp_addr_t *rloc;
patricia_node_t *node;
char *eid;
{
lispd_locator_chain_t *locator_chain = NULL;
lispd_db_entry_t *db_entry = NULL;
lispd_locator_chain_elt_t *add_elt = NULL;
char *token = NULL;
db_entry_list_elt *db_elt = NULL;
int afi;
char addr_str[MAX_INET_ADDRSTRLEN];
/*
* First find the eid associated with this interface
* How do we know which eid to use -- the v4 or v6 one?
* XXX: Assume eid's afi == rloc's afi
* Then, find the patricia node associated with the eid
*/
if (node == NULL) {
syslog(LOG_DAEMON, "add_rloc(): EID (%s) not found in database", eid);
free(eid);
return(0);
}
if ((db_entry = malloc(sizeof(lispd_db_entry_t))) == NULL) {
syslog(LOG_DAEMON,"add_rloc(): malloc(sizeof(lispd_database_t)): %s", strerror(errno));
free (eid);
return(0);
}
memset(db_entry,0,sizeof(lispd_db_entry_t));
/*
* Fill up db_entry
*/
db_entry->locator_name = strdup(iface_elt->iface_name);
memcpy((void *) &(db_entry->locator), rloc, sizeof(lisp_addr_t));
afi = get_afi(eid);
if ((token = strtok(eid, "/")) == NULL) {
syslog(LOG_DAEMON,"eid prefix not of the form prefix/length");
free (eid);
free(db_entry);
return(0);
}
/*
* get the EID prefix into the right place/format
*/
if (inet_pton(afi, token, &(db_entry->eid_prefix.address)) != 1) {
syslog(LOG_DAEMON, "inet_pton: %s", strerror(errno));
free(db_entry);
free (eid);
return(0);
}
/*
* get the prefix length into token
*/
if ((token = strtok(NULL,"/")) == NULL) {
syslog(LOG_DAEMON, "strtok: %s", strerror(errno));
free(db_entry);
free (eid);
return(0);
}
db_entry->eid_prefix_length = atoi(token);
db_entry->eid_prefix.afi = afi;
/*
* XXX: Assume priority and weight are
* identical for all locators of this iface
*/
db_entry->priority = iface_elt->priority;
db_entry->weight = iface_elt->weight;
/*
* link up db_entry into the patricia tree
*/
if ((add_elt = malloc(sizeof(lispd_locator_chain_elt_t))) == NULL) {
syslog(LOG_DAEMON, "add_rloc(): Can't malloc(sizeof(lispd_locator_chain_elt_t)): %s", strerror(errno));
free(db_entry);
free(eid);
return(0);
}
memset(add_elt, 0, sizeof(lispd_locator_chain_elt_t));
add_elt->db_entry = db_entry;
add_elt->locator_name = db_entry->locator_name;
if (node->data == NULL) {
/*
* Setup node->data
*/
if ((locator_chain = malloc(sizeof(lispd_locator_chain_t))) == NULL) {
syslog(LOG_DAEMON, "Can't malloc(sizeof(lispd_locator_chain_t))");
free(db_entry);
free(eid);
free(add_elt);
return(0);
}
memset(locator_chain,0,sizeof(lispd_locator_chain_t));
node->data = (lispd_locator_chain_t *) locator_chain;
/*
* put the eid_prefix information into the locator_chain
*/
copy_lisp_addr_t(&(locator_chain->eid_prefix),
&(db_entry->eid_prefix),
0);
locator_chain->eid_prefix_length = db_entry->eid_prefix_length;
locator_chain->eid_prefix.afi = db_entry->eid_prefix.afi;
locator_chain->eid_name = strdup(eid);
locator_chain->has_dynamic_locators = DYNAMIC_LOCATOR;
locator_chain->timer = DEFAULT_MAP_REGISTER_TIMEOUT;
} else {
/* there's an existing locator_chain */
locator_chain = (lispd_locator_chain_t *) node->data;
}
/*
* Setup a new locator_chain_elt for this rloc
*/
if ((add_elt = malloc(sizeof(lispd_locator_chain_elt_t))) == NULL) {
syslog(LOG_DAEMON,
"add_rloc(): Can't malloc(sizeof(lispd_locator_chain_elt_t)): %s",
strerror(errno));
free(db_entry);
free(eid);
return(0);
}
memset(add_elt, 0, sizeof(lispd_locator_chain_elt_t));
add_elt->db_entry = db_entry;
add_elt->locator_name = db_entry->locator_name;
/*
* connect up the locator_chain and locator_chain_elt sorted by RLOCs
*/
add_locator_chain_elt (locator_chain, add_elt);
syslog(LOG_DAEMON, "add_rloc(): %s added to interface %s",
inet_ntop (db_entry->locator.afi,
&(db_entry->locator.address), addr_str,
MAX_INET_ADDRSTRLEN),
iface_elt->iface_name);
/*
* Update iface_elt with the new db_entry element
*/
if ((db_elt = malloc (sizeof(db_entry_list_elt))) == NULL) {
syslog(LOG_DAEMON, "add_rloc(): Can't malloc(sizeof(db_entry_list_elt))\n");
free(eid);
return (0);
}
memset (db_elt, 0, sizeof(db_entry_list_elt));
db_elt->db_entry = db_entry;
db_elt->next = NULL;
switch (rloc->afi) {
case AF_INET:
add_item_to_db_entry_list(iface_elt->AF4_locators, db_elt);
break;
case AF_INET6:
add_item_to_db_entry_list(iface_elt->AF6_locators, db_elt);
break;
default:
free(db_elt);
break;
}
return db_entry;
}
int setup_netlink_iface ()
{
struct sockaddr_nl addr;