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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
|
/* $OpenBSD: kroute.c,v 1.6 2015/10/27 03:27:35 renato Exp $ */
/*
* Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
* Copyright (c) 2004 Esben Norby <norby@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/tree.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "eigrpd.h"
#include "log.h"
extern struct eigrpd_conf *eigrpd_conf;
struct {
uint32_t rtseq;
pid_t pid;
int fib_sync;
int fd;
struct event ev;
unsigned int rdomain;
} kr_state;
struct kroute_node {
TAILQ_ENTRY(kroute_node) entry;
struct kroute_priority *kprio; /* back pointer */
struct kroute r;
};
struct kroute_priority {
TAILQ_ENTRY(kroute_priority) entry;
struct kroute_prefix *kp; /* back pointer */
uint8_t priority;
TAILQ_HEAD(, kroute_node) nexthops;
};
struct kroute_prefix {
RB_ENTRY(kroute_prefix) entry;
int af;
union eigrpd_addr prefix;
uint8_t prefixlen;
TAILQ_HEAD(plist, kroute_priority) priorities;
};
struct kif_addr {
TAILQ_ENTRY(kif_addr) entry;
struct kaddr a;
};
struct kif_node {
RB_ENTRY(kif_node) entry;
TAILQ_HEAD(, kif_addr) addrs;
struct kif k;
};
void kr_redist_remove(struct kroute *);
int kr_redist_eval(struct kroute *);
void kr_redistribute(struct kroute_prefix *);
int kroute_compare(struct kroute_prefix *,
struct kroute_prefix *);
struct kroute_prefix *kroute_find_prefix(int, union eigrpd_addr *, uint8_t);
struct kroute_priority *kroute_find_prio(struct kroute_prefix *, uint8_t);
struct kroute_node *kroute_find_gw(struct kroute_priority *,
union eigrpd_addr *);
struct kroute_node *kroute_insert(struct kroute *);
int kroute_remove(struct kroute *);
void kroute_clear(void);
int kif_compare(struct kif_node *, struct kif_node *);
struct kif_node *kif_find(unsigned short);
struct kif_node *kif_insert(unsigned short);
int kif_remove(struct kif_node *);
struct kif *kif_update(unsigned short, int, struct if_data *,
struct sockaddr_dl *);
int kif_validate(unsigned short);
void protect_lo(void);
uint8_t prefixlen_classful(in_addr_t);
void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
void if_change(unsigned short, int, struct if_data *,
struct sockaddr_dl *);
void if_newaddr(unsigned short, struct sockaddr *, struct sockaddr *,
struct sockaddr *);
void if_deladdr(unsigned short, struct sockaddr *, struct sockaddr *,
struct sockaddr *);
void if_announce(void *);
int send_rtmsg(int, int, struct kroute *);
int dispatch_rtmsg(void);
int fetchtable(void);
int fetchifs(void);
int rtmsg_process(char *, size_t);
int rtmsg_process_route(struct rt_msghdr *,
struct sockaddr *[RTAX_MAX]);
RB_HEAD(kroute_tree, kroute_prefix) krt;
RB_PROTOTYPE(kroute_tree, kroute_prefix, entry, kroute_compare)
RB_GENERATE(kroute_tree, kroute_prefix, entry, kroute_compare)
RB_HEAD(kif_tree, kif_node) kit;
RB_PROTOTYPE(kif_tree, kif_node, entry, kif_compare)
RB_GENERATE(kif_tree, kif_node, entry, kif_compare)
int
kif_init(void)
{
RB_INIT(&kit);
kr_state.fib_sync = 0; /* decoupled */
if (fetchifs() == -1)
return (-1);
return (0);
}
int
kr_init(int fs, unsigned int rdomain)
{
int opt = 0, rcvbuf, default_rcvbuf;
socklen_t optlen;
kr_state.fib_sync = fs;
kr_state.rdomain = rdomain;
if ((kr_state.fd = socket(AF_ROUTE,
SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1) {
log_warn("%s: socket", __func__);
return (-1);
}
/* not interested in my own messages */
if (setsockopt(kr_state.fd, SOL_SOCKET, SO_USELOOPBACK,
&opt, sizeof(opt)) == -1)
log_warn("%s: setsockopt", __func__); /* not fatal */
/* grow receive buffer, don't wanna miss messages */
optlen = sizeof(default_rcvbuf);
if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF,
&default_rcvbuf, &optlen) == -1)
log_warn("%s: getsockopt SOL_SOCKET SO_RCVBUF", __func__);
else
for (rcvbuf = MAX_RTSOCK_BUF;
rcvbuf > default_rcvbuf &&
setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF,
&rcvbuf, sizeof(rcvbuf)) == -1 && errno == ENOBUFS;
rcvbuf /= 2)
; /* nothing */
kr_state.pid = getpid();
kr_state.rtseq = 1;
if (fetchtable() == -1)
return (-1);
protect_lo();
event_set(&kr_state.ev, kr_state.fd, EV_READ | EV_PERSIST,
kr_dispatch_msg, NULL);
event_add(&kr_state.ev, NULL);
return (0);
}
void
kif_redistribute(void)
{
struct kif_node *kif;
struct kif_addr *ka;
RB_FOREACH(kif, kif_tree, &kit) {
main_imsg_compose_eigrpe(IMSG_IFINFO, 0, &kif->k,
sizeof(struct kif));
TAILQ_FOREACH(ka, &kif->addrs, entry) {
main_imsg_compose_eigrpe(IMSG_NEWADDR, 0, &ka->a,
sizeof(struct kaddr));
}
}
}
int
kr_change(struct kroute *kr)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
int action = RTM_ADD;
kp = kroute_find_prefix(kr->af, &kr->prefix, kr->prefixlen);
if (kp == NULL)
kn = kroute_insert(kr);
else {
kprio = kroute_find_prio(kp, kr->priority);
if (kprio == NULL)
kn = kroute_insert(kr);
else {
kn = kroute_find_gw(kprio, &kr->nexthop);
if (kn == NULL)
kn = kroute_insert(kr);
else
action = RTM_CHANGE;
}
}
/* send update */
if (send_rtmsg(kr_state.fd, action, kr) == -1)
return (-1);
kn->r.flags |= F_EIGRPD_INSERTED;
return (0);
}
int
kr_delete(struct kroute *kr)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
kp = kroute_find_prefix(kr->af, &kr->prefix, kr->prefixlen);
if (kp == NULL)
return (0);
kprio = kroute_find_prio(kp, kr->priority);
if (kprio == NULL)
return (0);
kn = kroute_find_gw(kprio, &kr->nexthop);
if (kn == NULL)
return (0);
if (!(kn->r.flags & F_EIGRPD_INSERTED))
return (0);
if (send_rtmsg(kr_state.fd, RTM_DELETE, &kn->r) == -1)
return (-1);
if (kroute_remove(kr) == -1)
return (-1);
return (0);
}
void
kr_shutdown(void)
{
kr_fib_decouple();
kroute_clear();
kif_clear();
}
void
kr_fib_couple(void)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
if (kr_state.fib_sync == 1) /* already coupled */
return;
kr_state.fib_sync = 1;
RB_FOREACH(kp, kroute_tree, &krt)
TAILQ_FOREACH(kprio, &kp->priorities, entry)
TAILQ_FOREACH(kn, &kprio->nexthops, entry) {
if (!(kn->r.flags & F_EIGRPD_INSERTED))
continue;
send_rtmsg(kr_state.fd, RTM_ADD, &kn->r);
}
log_info("kernel routing table coupled");
}
void
kr_fib_decouple(void)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
if (kr_state.fib_sync == 0) /* already decoupled */
return;
RB_FOREACH(kp, kroute_tree, &krt)
TAILQ_FOREACH(kprio, &kp->priorities, entry)
TAILQ_FOREACH(kn, &kprio->nexthops, entry) {
if (!(kn->r.flags & F_EIGRPD_INSERTED))
continue;
send_rtmsg(kr_state.fd, RTM_DELETE, &kn->r);
}
kr_state.fib_sync = 0;
log_info("kernel routing table decoupled");
}
/* ARGSUSED */
void
kr_dispatch_msg(int fd, short event, void *bula)
{
if (dispatch_rtmsg() == -1)
event_loopexit(NULL);
}
void
kr_show_route(struct imsg *imsg)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
struct kroute kr;
int flags;
if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(flags)) {
log_warnx("%s: wrong imsg len", __func__);
return;
}
memcpy(&flags, imsg->data, sizeof(flags));
RB_FOREACH(kp, kroute_tree, &krt)
TAILQ_FOREACH(kprio, &kp->priorities, entry)
TAILQ_FOREACH(kn, &kprio->nexthops, entry) {
if (flags && !(kn->r.flags & flags))
continue;
memcpy(&kr, &kn->r, sizeof(kr));
if (kr.priority ==
eigrpd_conf->fib_priority_external)
kr.flags |= F_CTL_EXTERNAL;
main_imsg_compose_eigrpe(IMSG_CTL_KROUTE,
imsg->hdr.pid, &kr, sizeof(kr));
}
main_imsg_compose_eigrpe(IMSG_CTL_END, imsg->hdr.pid, NULL, 0);
}
void
kr_ifinfo(char *ifname, pid_t pid)
{
struct kif_node *kif;
RB_FOREACH(kif, kif_tree, &kit)
if (ifname == NULL || !strcmp(ifname, kif->k.ifname)) {
main_imsg_compose_eigrpe(IMSG_CTL_IFINFO,
pid, &kif->k, sizeof(kif->k));
}
main_imsg_compose_eigrpe(IMSG_CTL_END, pid, NULL, 0);
}
void
kr_redist_remove(struct kroute *kr)
{
/* was the route redistributed? */
if (!(kr->flags & F_REDISTRIBUTED))
return;
/* remove redistributed flag */
kr->flags &= ~F_REDISTRIBUTED;
main_imsg_compose_rde(IMSG_NETWORK_DEL, 0, kr, sizeof(*kr));
}
int
kr_redist_eval(struct kroute *kr)
{
in_addr_t a;
/* Only non-eigrpd routes are considered for redistribution. */
if (!(kr->flags & F_KERNEL))
goto dont_redistribute;
/* Dynamic routes are not redistributable. */
if (kr->flags & F_DYNAMIC)
goto dont_redistribute;
/* interface is not up and running so don't announce */
if (kr->flags & F_DOWN)
goto dont_redistribute;
/* filter-out non redistributable addresses */
switch (kr->af) {
case AF_INET:
a = ntohl(kr->prefix.v4.s_addr);
if (IN_MULTICAST(a) || IN_BADCLASS(a) ||
(a >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
goto dont_redistribute;
if (kr->nexthop.v4.s_addr == htonl(INADDR_LOOPBACK) &&
!(kr->flags & (F_BLACKHOLE|F_REJECT)))
goto dont_redistribute;
break;
case AF_INET6:
if (IN6_IS_ADDR_LOOPBACK(&kr->prefix.v6) ||
IN6_IS_ADDR_MULTICAST(&kr->prefix.v6) ||
IN6_IS_ADDR_LINKLOCAL(&kr->prefix.v6) ||
IN6_IS_ADDR_SITELOCAL(&kr->prefix.v6) ||
IN6_IS_ADDR_V4MAPPED(&kr->prefix.v6) ||
IN6_IS_ADDR_V4COMPAT(&kr->prefix.v6))
goto dont_redistribute;
if (IN6_IS_ADDR_LOOPBACK(&kr->nexthop.v6) &&
!(kr->flags & (F_BLACKHOLE|F_REJECT)))
goto dont_redistribute;
break;
default:
log_debug("%s: unexpected address-family", __func__);
break;
}
/* prefix should be redistributed */
kr->flags |= F_REDISTRIBUTED;
main_imsg_compose_rde(IMSG_NETWORK_ADD, 0, kr, sizeof(*kr));
return (1);
dont_redistribute:
kr_redist_remove(kr);
return (0);
}
void
kr_redistribute(struct kroute_prefix *kp)
{
struct kroute_priority *kprio;
struct kroute_node *kn;
/* only the highest prio route can be redistributed */
TAILQ_FOREACH_REVERSE(kprio, &kp->priorities, plist, entry) {
if (kprio == TAILQ_FIRST(&kp->priorities)) {
TAILQ_FOREACH(kn, &kprio->nexthops, entry)
/* pick just one entry in case of multipath */
if (kr_redist_eval(&kn->r))
break;
} else {
TAILQ_FOREACH(kn, &kprio->nexthops, entry)
kr_redist_remove(&kn->r);
}
}
}
int
kroute_compare(struct kroute_prefix *a, struct kroute_prefix *b)
{
if (a->af < b->af)
return (-1);
if (a->af > b->af)
return (1);
switch (a->af) {
case AF_INET:
if (ntohl(a->prefix.v4.s_addr) <
ntohl(b->prefix.v4.s_addr))
return (-1);
if (ntohl(a->prefix.v4.s_addr) >
ntohl(b->prefix.v4.s_addr))
return (1);
break;
case AF_INET6:
if (memcmp(a->prefix.v6.s6_addr,
b->prefix.v6.s6_addr, 16) < 0)
return (-1);
if (memcmp(a->prefix.v6.s6_addr,
b->prefix.v6.s6_addr, 16) > 0)
return (1);
break;
default:
log_debug("%s: unexpected address-family", __func__);
break;
}
if (a->prefixlen < b->prefixlen)
return (-1);
if (a->prefixlen > b->prefixlen)
return (1);
return (0);
}
/* tree management */
struct kroute_prefix *
kroute_find_prefix(int af, union eigrpd_addr *prefix, uint8_t prefixlen)
{
struct kroute_prefix s;
s.af = af;
memcpy(&s.prefix, prefix, sizeof(s.prefix));
s.prefixlen = prefixlen;
return (RB_FIND(kroute_tree, &krt, &s));
}
struct kroute_priority *
kroute_find_prio(struct kroute_prefix *kp, uint8_t prio)
{
struct kroute_priority *kprio;
/* RTP_ANY here picks the lowest priority node */
if (prio == RTP_ANY)
return (TAILQ_FIRST(&kp->priorities));
TAILQ_FOREACH(kprio, &kp->priorities, entry)
if (kprio->priority == prio)
return (kprio);
return (NULL);
}
struct kroute_node *
kroute_find_gw(struct kroute_priority *kprio, union eigrpd_addr *nh)
{
struct kroute_node *kn;
TAILQ_FOREACH(kn, &kprio->nexthops, entry)
if (eigrp_addrcmp(kprio->kp->af, &kn->r.nexthop, nh) == 0)
return (kn);
return (NULL);
}
struct kroute_node *
kroute_insert(struct kroute *kr)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio, *tmp = NULL;
struct kroute_node *kn;
kp = kroute_find_prefix(kr->af, &kr->prefix, kr->prefixlen);
if (kp == NULL) {
kp = calloc(1, sizeof((*kp)));
if (kp == NULL)
fatal("kroute_insert");
kp->af = kr->af;
memcpy(&kp->prefix, &kr->prefix, sizeof(kp->prefix));
kp->prefixlen = kr->prefixlen;
TAILQ_INIT(&kp->priorities);
RB_INSERT(kroute_tree, &krt, kp);
}
kprio = kroute_find_prio(kp, kr->priority);
if (kprio == NULL) {
kprio = calloc(1, sizeof(*kprio));
if (kprio == NULL)
fatal("kroute_insert");
kprio->kp = kp;
kprio->priority = kr->priority;
TAILQ_INIT(&kprio->nexthops);
/* lower priorities first */
TAILQ_FOREACH(tmp, &kp->priorities, entry)
if (tmp->priority > kprio->priority)
break;
if (tmp)
TAILQ_INSERT_BEFORE(tmp, kprio, entry);
else
TAILQ_INSERT_TAIL(&kp->priorities, kprio, entry);
}
kn = kroute_find_gw(kprio, &kr->nexthop);
if (kn == NULL) {
kn = calloc(1, sizeof(*kn));
if (kn == NULL)
fatal("kroute_insert");
kn->kprio = kprio;
memcpy(&kn->r, kr, sizeof(kn->r));
TAILQ_INSERT_TAIL(&kprio->nexthops, kn, entry);
}
if (!(kr->flags & F_KERNEL)) {
/* don't validate or redistribute eigrp route */
kr->flags &= ~F_DOWN;
return (kn);
}
if (kif_validate(kr->ifindex))
kr->flags &= ~F_DOWN;
else
kr->flags |= F_DOWN;
kr_redistribute(kp);
return (kn);
}
int
kroute_remove(struct kroute *kr)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
kp = kroute_find_prefix(kr->af, &kr->prefix, kr->prefixlen);
if (kp == NULL)
goto notfound;
kprio = kroute_find_prio(kp, kr->priority);
if (kprio == NULL)
goto notfound;
kn = kroute_find_gw(kprio, &kr->nexthop);
if (kn == NULL)
goto notfound;
kr_redist_remove(&kn->r);
TAILQ_REMOVE(&kprio->nexthops, kn, entry);
free(kn);
if (TAILQ_EMPTY(&kprio->nexthops)) {
TAILQ_REMOVE(&kp->priorities, kprio, entry);
free(kprio);
}
if (TAILQ_EMPTY(&kp->priorities)) {
if (RB_REMOVE(kroute_tree, &krt, kp) == NULL)
log_warnx("%s failed for %s/%u", __func__,
log_addr(kr->af, &kr->prefix), kp->prefixlen);
free(kp);
} else
kr_redistribute(kp);
return (0);
notfound:
log_warnx("%s failed to find %s/%u", __func__,
log_addr(kr->af, &kr->prefix), kr->prefixlen);
return (-1);
}
void
kroute_clear(void)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
while ((kp = RB_MIN(kroute_tree, &krt)) != NULL) {
while ((kprio = TAILQ_FIRST(&kp->priorities)) != NULL) {
while ((kn = TAILQ_FIRST(&kprio->nexthops)) != NULL) {
TAILQ_REMOVE(&kprio->nexthops, kn, entry);
free(kn);
}
TAILQ_REMOVE(&kp->priorities, kprio, entry);
free(kprio);
}
RB_REMOVE(kroute_tree, &krt, kp);
free(kp);
}
}
int
kif_compare(struct kif_node *a, struct kif_node *b)
{
return (b->k.ifindex - a->k.ifindex);
}
/* tree management */
struct kif_node *
kif_find(unsigned short ifindex)
{
struct kif_node s;
memset(&s, 0, sizeof(s));
s.k.ifindex = ifindex;
return (RB_FIND(kif_tree, &kit, &s));
}
struct kif *
kif_findname(char *ifname)
{
struct kif_node *kif;
RB_FOREACH(kif, kif_tree, &kit)
if (!strcmp(ifname, kif->k.ifname))
return (&kif->k);
return (NULL);
}
struct kif_node *
kif_insert(unsigned short ifindex)
{
struct kif_node *kif;
if ((kif = calloc(1, sizeof(struct kif_node))) == NULL)
return (NULL);
kif->k.ifindex = ifindex;
TAILQ_INIT(&kif->addrs);
if (RB_INSERT(kif_tree, &kit, kif) != NULL)
fatalx("kif_insert: RB_INSERT");
return (kif);
}
int
kif_remove(struct kif_node *kif)
{
struct kif_addr *ka;
if (RB_REMOVE(kif_tree, &kit, kif) == NULL) {
log_warnx("%s failed for inteface %s", __func__, kif->k.ifname);
return (-1);
}
while ((ka = TAILQ_FIRST(&kif->addrs)) != NULL) {
TAILQ_REMOVE(&kif->addrs, ka, entry);
free(ka);
}
free(kif);
return (0);
}
void
kif_clear(void)
{
struct kif_node *kif;
while ((kif = RB_MIN(kif_tree, &kit)) != NULL)
kif_remove(kif);
}
struct kif *
kif_update(unsigned short ifindex, int flags, struct if_data *ifd,
struct sockaddr_dl *sdl)
{
struct kif_node *kif;
if ((kif = kif_find(ifindex)) == NULL) {
if ((kif = kif_insert(ifindex)) == NULL)
return (NULL);
kif->k.nh_reachable = (flags & IFF_UP) &&
LINK_STATE_IS_UP(ifd->ifi_link_state);
}
kif->k.flags = flags;
kif->k.link_state = ifd->ifi_link_state;
kif->k.if_type = ifd->ifi_type;
kif->k.baudrate = ifd->ifi_baudrate;
kif->k.mtu = ifd->ifi_mtu;
if (sdl && sdl->sdl_family == AF_LINK) {
if (sdl->sdl_nlen >= sizeof(kif->k.ifname))
memcpy(kif->k.ifname, sdl->sdl_data,
sizeof(kif->k.ifname) - 1);
else if (sdl->sdl_nlen > 0)
memcpy(kif->k.ifname, sdl->sdl_data,
sdl->sdl_nlen);
/* string already terminated via calloc() */
}
return (&kif->k);
}
int
kif_validate(unsigned short ifindex)
{
struct kif_node *kif;
if ((kif = kif_find(ifindex)) == NULL)
return (0);
return (kif->k.nh_reachable);
}
/* misc */
void
protect_lo(void)
{
struct kroute kr4, kr6;
/* special protection for 127/8 */
memset(&kr4, 0, sizeof(kr4));
kr4.af = AF_INET;
kr4.prefix.v4.s_addr = htonl(INADDR_LOOPBACK & IN_CLASSA_NET);
kr4.prefixlen = 8;
kr4.flags = F_KERNEL|F_CONNECTED;
kroute_insert(&kr4);
/* special protection for ::1 */
memset(&kr6, 0, sizeof(kr6));
kr6.af = AF_INET6;
memcpy(&kr6.prefix.v6, &in6addr_loopback, sizeof(kr6.prefix.v6));
kr6.prefixlen = 128;
kr6.flags = F_KERNEL|F_CONNECTED;
kroute_insert(&kr6);
}
uint8_t
prefixlen_classful(in_addr_t ina)
{
/* it hurt to write this. */
if (ina >= 0xf0000000U) /* class E */
return (32);
else if (ina >= 0xe0000000U) /* class D */
return (4);
else if (ina >= 0xc0000000U) /* class C */
return (24);
else if (ina >= 0x80000000U) /* class B */
return (16);
else /* class A */
return (8);
}
#define ROUNDUP(a) \
(((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a))
void
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
{
int i;
for (i = 0; i < RTAX_MAX; i++) {
if (addrs & (1 << i)) {
rti_info[i] = sa;
sa = (struct sockaddr *)((char *)(sa) +
ROUNDUP(sa->sa_len));
} else
rti_info[i] = NULL;
}
}
void
if_change(unsigned short ifindex, int flags, struct if_data *ifd,
struct sockaddr_dl *sdl)
{
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
struct kif *kif;
uint8_t reachable;
if ((kif = kif_update(ifindex, flags, ifd, sdl)) == NULL) {
log_warn("%s: kif_update(%u)", __func__, ifindex);
return;
}
reachable = (kif->flags & IFF_UP) &&
LINK_STATE_IS_UP(kif->link_state);
if (reachable == kif->nh_reachable)
return; /* nothing changed wrt nexthop validity */
kif->nh_reachable = reachable;
/* notify eigrpe about link state */
main_imsg_compose_eigrpe(IMSG_IFINFO, 0, kif, sizeof(struct kif));
/* notify rde about link going down */
if (!kif->nh_reachable)
main_imsg_compose_rde(IMSG_IFDOWN, 0, kif, sizeof(struct kif));
/* update redistribute list */
RB_FOREACH(kp, kroute_tree, &krt) {
TAILQ_FOREACH(kprio, &kp->priorities, entry) {
TAILQ_FOREACH(kn, &kprio->nexthops, entry) {
if (kn->r.ifindex != ifindex)
continue;
if (reachable)
kn->r.flags &= ~F_DOWN;
else
kn->r.flags |= F_DOWN;
}
}
kr_redistribute(kp);
}
}
void
if_newaddr(unsigned short ifindex, struct sockaddr *ifa, struct sockaddr *mask,
struct sockaddr *brd)
{
struct kif_node *kif;
struct sockaddr_in *ifa4, *mask4, *brd4;
struct sockaddr_in6 *ifa6, *mask6, *brd6;
struct kif_addr *ka;
uint32_t a;
if (ifa == NULL)
return;
if ((kif = kif_find(ifindex)) == NULL) {
log_warnx("%s: corresponding if %d not found", __func__,
ifindex);
return;
}
switch (ifa->sa_family) {
case AF_INET:
ifa4 = (struct sockaddr_in *) ifa;
mask4 = (struct sockaddr_in *) mask;
brd4 = (struct sockaddr_in *) brd;
/* filter out unwanted addresses */
a = ntohl(ifa4->sin_addr.s_addr);
if (IN_MULTICAST(a) || IN_BADCLASS(a) ||
(a >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
return;
if ((ka = calloc(1, sizeof(struct kif_addr))) == NULL)
fatal("if_newaddr");
ka->a.addr.v4.s_addr = ifa4->sin_addr.s_addr;
if (mask4)
ka->a.prefixlen =
mask2prefixlen(mask4->sin_addr.s_addr);
if (brd4)
ka->a.dstbrd.v4.s_addr = brd4->sin_addr.s_addr;
break;
case AF_INET6:
ifa6 = (struct sockaddr_in6 *) ifa;
mask6 = (struct sockaddr_in6 *) mask;
brd6 = (struct sockaddr_in6 *) brd;
/* We only care about link-local and global-scope. */
if (IN6_IS_ADDR_UNSPECIFIED(&ifa6->sin6_addr) ||
IN6_IS_ADDR_LOOPBACK(&ifa6->sin6_addr) ||
IN6_IS_ADDR_MULTICAST(&ifa6->sin6_addr) ||
IN6_IS_ADDR_SITELOCAL(&ifa6->sin6_addr) ||
IN6_IS_ADDR_V4MAPPED(&ifa6->sin6_addr) ||
IN6_IS_ADDR_V4COMPAT(&ifa6->sin6_addr))
return;
clearscope(&ifa6->sin6_addr);
if ((ka = calloc(1, sizeof(struct kif_addr))) == NULL)
fatal("if_newaddr");
ka->a.addr.v6 = ifa6->sin6_addr;
if (mask6)
ka->a.prefixlen = mask2prefixlen6(mask6);
if (brd6)
ka->a.dstbrd.v6 = brd6->sin6_addr;
break;
default:
return;
}
ka->a.ifindex = ifindex;
ka->a.af = ifa->sa_family;
TAILQ_INSERT_TAIL(&kif->addrs, ka, entry);
/* notify eigrpe about new address */
main_imsg_compose_eigrpe(IMSG_NEWADDR, 0, &ka->a,
sizeof(struct kaddr));
}
void
if_deladdr(unsigned short ifindex, struct sockaddr *ifa, struct sockaddr *mask,
struct sockaddr *brd)
{
struct kif_node *kif;
struct sockaddr_in *ifa4, *mask4, *brd4;
struct sockaddr_in6 *ifa6, *mask6, *brd6;
struct kaddr k;
struct kif_addr *ka, *nka;
if (ifa == NULL)
return;
if ((kif = kif_find(ifindex)) == NULL) {
log_warnx("%s: corresponding if %d not found", __func__,
ifindex);
return;
}
memset(&k, 0, sizeof(k));
k.af = ifa->sa_family;
switch (ifa->sa_family) {
case AF_INET:
ifa4 = (struct sockaddr_in *) ifa;
mask4 = (struct sockaddr_in *) mask;
brd4 = (struct sockaddr_in *) brd;
k.addr.v4.s_addr = ifa4->sin_addr.s_addr;
if (mask4)
k.prefixlen = mask2prefixlen(mask4->sin_addr.s_addr);
if (brd4)
k.dstbrd.v4.s_addr = brd4->sin_addr.s_addr;
break;
case AF_INET6:
ifa6 = (struct sockaddr_in6 *) ifa;
mask6 = (struct sockaddr_in6 *) mask;
brd6 = (struct sockaddr_in6 *) brd;
/* We only care about link-local and global-scope. */
if (IN6_IS_ADDR_UNSPECIFIED(&ifa6->sin6_addr) ||
IN6_IS_ADDR_LOOPBACK(&ifa6->sin6_addr) ||
IN6_IS_ADDR_MULTICAST(&ifa6->sin6_addr) ||
IN6_IS_ADDR_SITELOCAL(&ifa6->sin6_addr) ||
IN6_IS_ADDR_V4MAPPED(&ifa6->sin6_addr) ||
IN6_IS_ADDR_V4COMPAT(&ifa6->sin6_addr))
return;
clearscope(&ifa6->sin6_addr);
k.addr.v6 = ifa6->sin6_addr;
if (mask6)
k.prefixlen = mask2prefixlen6(mask6);
if (brd6)
k.dstbrd.v6 = brd6->sin6_addr;
break;
default:
return;
}
for (ka = TAILQ_FIRST(&kif->addrs); ka != NULL; ka = nka) {
nka = TAILQ_NEXT(ka, entry);
if (ka->a.af != k.af ||
ka->a.prefixlen != k.prefixlen)
continue;
switch (ifa->sa_family) {
case AF_INET:
if (ka->a.addr.v4.s_addr != k.addr.v4.s_addr ||
ka->a.dstbrd.v4.s_addr != k.dstbrd.v4.s_addr)
continue;
break;
case AF_INET6:
if (!IN6_ARE_ADDR_EQUAL(&ka->a.addr.v6, &k.addr.v6) ||
!IN6_ARE_ADDR_EQUAL(&ka->a.dstbrd.v6, &k.dstbrd.v6))
continue;
break;
default:
break;
}
/* notify eigrpe about removed address */
main_imsg_compose_eigrpe(IMSG_DELADDR, 0, &ka->a,
sizeof(struct kaddr));
TAILQ_REMOVE(&kif->addrs, ka, entry);
free(ka);
return;
}
}
void
if_announce(void *msg)
{
struct if_announcemsghdr *ifan;
struct kif_node *kif;
ifan = msg;
switch (ifan->ifan_what) {
case IFAN_ARRIVAL:
kif = kif_insert(ifan->ifan_index);
if (kif)
strlcpy(kif->k.ifname, ifan->ifan_name,
sizeof(kif->k.ifname));
break;
case IFAN_DEPARTURE:
kif = kif_find(ifan->ifan_index);
if (kif)
kif_remove(kif);
break;
}
}
/* rtsock */
static int
send_rtmsg_v4(int fd, int action, struct kroute *kr)
{
struct iovec iov[5];
struct rt_msghdr hdr;
struct sockaddr_in prefix;
struct sockaddr_in nexthop;
struct sockaddr_in mask;
int iovcnt = 0;
if (kr_state.fib_sync == 0)
return (0);
/* initialize header */
memset(&hdr, 0, sizeof(hdr));
hdr.rtm_version = RTM_VERSION;
hdr.rtm_type = action;
hdr.rtm_priority = kr->priority;
hdr.rtm_tableid = kr_state.rdomain; /* rtableid */
if (action == RTM_CHANGE)
hdr.rtm_fmask = RTF_REJECT|RTF_BLACKHOLE;
else
hdr.rtm_flags = RTF_MPATH;
if (kr->flags & F_BLACKHOLE)
hdr.rtm_flags |= RTF_BLACKHOLE;
hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
hdr.rtm_msglen = sizeof(hdr);
/* adjust iovec */
iov[iovcnt].iov_base = &hdr;
iov[iovcnt++].iov_len = sizeof(hdr);
memset(&prefix, 0, sizeof(prefix));
prefix.sin_len = sizeof(prefix);
prefix.sin_family = AF_INET;
prefix.sin_addr.s_addr = kr->prefix.v4.s_addr;
/* adjust header */
hdr.rtm_addrs |= RTA_DST;
hdr.rtm_msglen += sizeof(prefix);
/* adjust iovec */
iov[iovcnt].iov_base = &prefix;
iov[iovcnt++].iov_len = sizeof(prefix);
if (kr->nexthop.v4.s_addr != 0) {
memset(&nexthop, 0, sizeof(nexthop));
nexthop.sin_len = sizeof(nexthop);
nexthop.sin_family = AF_INET;
nexthop.sin_addr.s_addr = kr->nexthop.v4.s_addr;
/* adjust header */
hdr.rtm_flags |= RTF_GATEWAY;
hdr.rtm_addrs |= RTA_GATEWAY;
hdr.rtm_msglen += sizeof(nexthop);
/* adjust iovec */
iov[iovcnt].iov_base = &nexthop;
iov[iovcnt++].iov_len = sizeof(nexthop);
}
memset(&mask, 0, sizeof(mask));
mask.sin_len = sizeof(mask);
mask.sin_family = AF_INET;
mask.sin_addr.s_addr = prefixlen2mask(kr->prefixlen);
/* adjust header */
hdr.rtm_addrs |= RTA_NETMASK;
hdr.rtm_msglen += sizeof(mask);
/* adjust iovec */
iov[iovcnt].iov_base = &mask;
iov[iovcnt++].iov_len = sizeof(mask);
retry:
if (writev(fd, iov, iovcnt) == -1) {
if (errno == ESRCH) {
if (hdr.rtm_type == RTM_CHANGE) {
hdr.rtm_type = RTM_ADD;
goto retry;
} else if (hdr.rtm_type == RTM_DELETE) {
log_info("route %s/%u vanished before delete",
inet_ntoa(kr->prefix.v4),
kr->prefixlen);
return (0);
}
}
log_warn("%s: action %u, prefix %s/%u", __func__, hdr.rtm_type,
inet_ntoa(kr->prefix.v4), kr->prefixlen);
return (0);
}
return (0);
}
static int
send_rtmsg_v6(int fd, int action, struct kroute *kr)
{
struct iovec iov[5];
struct rt_msghdr hdr;
struct pad {
struct sockaddr_in6 addr;
char pad[sizeof(long)]; /* thank you IPv6 */
} prefix, nexthop, mask;
int iovcnt = 0;
if (kr_state.fib_sync == 0)
return (0);
/* initialize header */
memset(&hdr, 0, sizeof(hdr));
hdr.rtm_version = RTM_VERSION;
hdr.rtm_type = action;
hdr.rtm_priority = kr->priority;
hdr.rtm_tableid = kr_state.rdomain; /* rtableid */
if (action == RTM_CHANGE)
hdr.rtm_fmask = RTF_REJECT|RTF_BLACKHOLE;
else
hdr.rtm_flags = RTF_MPATH;
hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
hdr.rtm_msglen = sizeof(hdr);
/* adjust iovec */
iov[iovcnt].iov_base = &hdr;
iov[iovcnt++].iov_len = sizeof(hdr);
memset(&prefix, 0, sizeof(prefix));
prefix.addr.sin6_len = sizeof(struct sockaddr_in6);
prefix.addr.sin6_family = AF_INET6;
prefix.addr.sin6_addr = kr->prefix.v6;
/* adjust header */
hdr.rtm_addrs |= RTA_DST;
hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
/* adjust iovec */
iov[iovcnt].iov_base = &prefix;
iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
if (!IN6_IS_ADDR_UNSPECIFIED(&kr->nexthop.v6)) {
memset(&nexthop, 0, sizeof(nexthop));
nexthop.addr.sin6_len = sizeof(struct sockaddr_in6);
nexthop.addr.sin6_family = AF_INET6;
nexthop.addr.sin6_addr = kr->nexthop.v6;
nexthop.addr.sin6_scope_id = kr->ifindex;
embedscope(&nexthop.addr);
/* adjust header */
hdr.rtm_flags |= RTF_GATEWAY;
hdr.rtm_addrs |= RTA_GATEWAY;
hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
/* adjust iovec */
iov[iovcnt].iov_base = &nexthop;
iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
}
memset(&mask, 0, sizeof(mask));
mask.addr.sin6_len = sizeof(struct sockaddr_in6);
mask.addr.sin6_family = AF_INET6;
mask.addr.sin6_addr = *prefixlen2mask6(kr->prefixlen);
/* adjust header */
if (kr->prefixlen == 128)
hdr.rtm_flags |= RTF_HOST;
hdr.rtm_addrs |= RTA_NETMASK;
hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
/* adjust iovec */
iov[iovcnt].iov_base = &mask;
iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
retry:
if (writev(fd, iov, iovcnt) == -1) {
if (errno == ESRCH) {
if (hdr.rtm_type == RTM_CHANGE) {
hdr.rtm_type = RTM_ADD;
goto retry;
} else if (hdr.rtm_type == RTM_DELETE) {
log_info("route %s/%u vanished before delete",
log_in6addr(&kr->prefix.v6),
kr->prefixlen);
return (0);
}
}
log_warn("%s: action %u, prefix %s/%u", __func__, hdr.rtm_type,
log_in6addr(&kr->prefix.v6), kr->prefixlen);
return (0);
}
return (0);
}
int
send_rtmsg(int fd, int action, struct kroute *kr)
{
switch (kr->af) {
case AF_INET:
return (send_rtmsg_v4(fd, action, kr));
case AF_INET6:
return (send_rtmsg_v6(fd, action, kr));
default:
break;
}
return (-1);
}
int
fetchtable(void)
{
size_t len;
int mib[7];
char *buf;
int rv;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = 0;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
mib[6] = kr_state.rdomain; /* rtableid */
if (sysctl(mib, 7, NULL, &len, NULL, 0) == -1) {
log_warn("sysctl");
return (-1);
}
if ((buf = malloc(len)) == NULL) {
log_warn("fetchtable");
return (-1);
}
if (sysctl(mib, 7, buf, &len, NULL, 0) == -1) {
log_warn("sysctl");
free(buf);
return (-1);
}
rv = rtmsg_process(buf, len);
free(buf);
return (rv);
}
int
fetchifs(void)
{
size_t len;
int mib[6];
char *buf;
int rv;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = 0; /* wildcard */
mib[4] = NET_RT_IFLIST;
mib[5] = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) {
log_warn("sysctl");
return (-1);
}
if ((buf = malloc(len)) == NULL) {
log_warn("fetchifs");
return (-1);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) == -1) {
log_warn("sysctl");
free(buf);
return (-1);
}
rv = rtmsg_process(buf, len);
free(buf);
return (rv);
}
int
dispatch_rtmsg(void)
{
char buf[RT_BUF_SIZE];
ssize_t n;
if ((n = read(kr_state.fd, &buf, sizeof(buf))) == -1) {
if (errno == EAGAIN || errno == EINTR)
return (0);
log_warn("%s: read error", __func__);
return (-1);
}
if (n == 0) {
log_warnx("routing socket closed");
return (-1);
}
return (rtmsg_process(buf, n));
}
int
rtmsg_process(char *buf, size_t len)
{
struct rt_msghdr *rtm;
struct if_msghdr ifm;
struct ifa_msghdr *ifam;
struct sockaddr *sa, *rti_info[RTAX_MAX];
size_t offset;
char *next;
for (offset = 0; offset < len; offset += rtm->rtm_msglen) {
next = buf + offset;
rtm = (struct rt_msghdr *)next;
if (len < offset + sizeof(unsigned short) ||
len < offset + rtm->rtm_msglen)
fatalx("rtmsg_process: partial rtm in buffer");
if (rtm->rtm_version != RTM_VERSION)
continue;
sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
switch (rtm->rtm_type) {
case RTM_ADD:
case RTM_GET:
case RTM_CHANGE:
case RTM_DELETE:
if (rtm->rtm_pid == kr_state.pid)
continue;
if (rtm->rtm_errno) /* failed attempts... */
continue;
if (rtm->rtm_tableid != kr_state.rdomain)
continue;
/* Skip ARP/ND cache and broadcast routes. */
if (rtm->rtm_flags & (RTF_LLINFO|RTF_BROADCAST))
continue;
if (rtmsg_process_route(rtm, rti_info) == -1)
return (-1);
}
switch (rtm->rtm_type) {
case RTM_IFINFO:
memcpy(&ifm, next, sizeof(ifm));
if_change(ifm.ifm_index, ifm.ifm_flags, &ifm.ifm_data,
(struct sockaddr_dl *)rti_info[RTAX_IFP]);
break;
case RTM_NEWADDR:
ifam = (struct ifa_msghdr *)rtm;
if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
RTA_BRD)) == 0)
break;
if_newaddr(ifam->ifam_index,
(struct sockaddr *)rti_info[RTAX_IFA],
(struct sockaddr *)rti_info[RTAX_NETMASK],
(struct sockaddr *)rti_info[RTAX_BRD]);
break;
case RTM_DELADDR:
ifam = (struct ifa_msghdr *)rtm;
if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
RTA_BRD)) == 0)
break;
if_deladdr(ifam->ifam_index,
(struct sockaddr *)rti_info[RTAX_IFA],
(struct sockaddr *)rti_info[RTAX_NETMASK],
(struct sockaddr *)rti_info[RTAX_BRD]);
break;
case RTM_IFANNOUNCE:
if_announce(next);
break;
default:
/* ignore for now */
break;
}
}
return (offset);
}
int
rtmsg_process_route(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX])
{
struct sockaddr *sa;
struct sockaddr_in *sa_in;
struct sockaddr_in6 *sa_in6;
struct kroute kr;
struct kroute_prefix *kp;
struct kroute_priority *kprio;
struct kroute_node *kn;
if ((sa = rti_info[RTAX_DST]) == NULL)
return (-1);
memset(&kr, 0, sizeof(kr));
kr.af = sa->sa_family;
switch (kr.af) {
case AF_INET:
kr.prefix.v4.s_addr =
((struct sockaddr_in *)sa)->sin_addr.s_addr;
sa_in = (struct sockaddr_in *) rti_info[RTAX_NETMASK];
if (sa_in != NULL && sa_in->sin_len != 0)
kr.prefixlen = mask2prefixlen(sa_in->sin_addr.s_addr);
else if (rtm->rtm_flags & RTF_HOST)
kr.prefixlen = 32;
else if (kr.prefix.v4.s_addr == INADDR_ANY)
kr.prefixlen = 0;
else
kr.prefixlen = prefixlen_classful(kr.prefix.v4.s_addr);
break;
case AF_INET6:
memcpy(&kr.prefix.v6, &((struct sockaddr_in6 *)sa)->sin6_addr,
sizeof(kr.prefix.v6));
sa_in6 = (struct sockaddr_in6 *)rti_info[RTAX_NETMASK];
if (sa_in6 != NULL && sa_in6->sin6_len != 0)
kr.prefixlen = mask2prefixlen6(sa_in6);
else if (rtm->rtm_flags & RTF_HOST)
kr.prefixlen = 128;
else if (IN6_IS_ADDR_UNSPECIFIED(&kr.prefix.v6))
kr.prefixlen = 0;
else
fatalx("in6 net addr without netmask");
break;
default:
return (0);
}
kr.ifindex = rtm->rtm_index;
if ((sa = rti_info[RTAX_GATEWAY]) != NULL) {
switch (sa->sa_family) {
case AF_INET:
kr.nexthop.v4.s_addr =
((struct sockaddr_in *)sa)->sin_addr.s_addr;
break;
case AF_INET6:
sa_in6 = (struct sockaddr_in6 *)sa;
recoverscope(sa_in6);
kr.nexthop.v6 = sa_in6->sin6_addr;
if (sa_in6->sin6_scope_id)
kr.ifindex = sa_in6->sin6_scope_id;
break;
case AF_LINK:
kr.flags |= F_CONNECTED;
break;
}
}
kr.flags |= F_KERNEL;
if (rtm->rtm_flags & RTF_STATIC)
kr.flags |= F_STATIC;
if (rtm->rtm_flags & RTF_BLACKHOLE)
kr.flags |= F_BLACKHOLE;
if (rtm->rtm_flags & RTF_REJECT)
kr.flags |= F_REJECT;
if (rtm->rtm_flags & RTF_DYNAMIC)
kr.flags |= F_DYNAMIC;
kr.priority = rtm->rtm_priority;
if (rtm->rtm_type == RTM_CHANGE) {
/*
* The kernel doesn't allow RTM_CHANGE for multipath routes.
* If we got this message we know that the route has only one
* nexthop and we should remove it before installing the same
* route with the new nexthop.
*/
kp = kroute_find_prefix(kr.af, &kr.prefix, kr.prefixlen);
if (kp) {
kprio = kroute_find_prio(kp, kr.priority);
if (kprio) {
kn = TAILQ_FIRST(&kprio->nexthops);
if (kn)
kroute_remove(&kn->r);
}
}
}
kn = NULL;
kp = kroute_find_prefix(kr.af, &kr.prefix, kr.prefixlen);
if (kp) {
kprio = kroute_find_prio(kp, kr.priority);
if (kprio)
kn = kroute_find_gw(kprio, &kr.nexthop);
}
if (rtm->rtm_type == RTM_DELETE) {
if (kn == NULL || !(kn->r.flags & F_KERNEL))
return (0);
return (kroute_remove(&kr));
}
if (!eigrp_addrisset(kr.af, &kr.nexthop) && !(kr.flags & F_CONNECTED)) {
log_warnx("%s: no nexthop for %s/%u", __func__,
log_addr(kr.af, &kr.prefix), kr.prefixlen);
return (-1);
}
if (kn != NULL) {
/* update route */
memcpy(&kn->r, &kr, sizeof(kn->r));
if (kif_validate(kn->r.ifindex))
kn->r.flags &= ~F_DOWN;
else
kn->r.flags |= F_DOWN;
kr_redistribute(kp);
} else {
if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_GET) &&
(kr.priority == eigrpd_conf->fib_priority_internal ||
kr.priority == eigrpd_conf->fib_priority_external ||
kr.priority == eigrpd_conf->fib_priority_summary)) {
log_warnx("alien EIGRP route %s/%d", log_addr(kr.af,
&kr.prefix), kr.prefixlen);
return (send_rtmsg(kr_state.fd, RTM_DELETE, &kr));
}
kroute_insert(&kr);
}
return (0);
}
|