This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ospfd_linux.C
1160 lines (1036 loc) · 32.3 KB
/
ospfd_linux.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
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
/*
* OSPFD routing daemon
* Copyright (C) 1998 by John T. Moy
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <tcl/tcl.h>
#if LINUX_VERSION_CODE >= LINUX22
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#else
#include <sys/socket.h>
#endif
#include <net/route.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <linux/version.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
// Hack to include mroute.h file
#define _LINUX_SOCKIOS_H
#define _LINUX_IN_H
#include <linux/mroute.h>
#include <netinet/ip.h>
#include <linux/if_tunnel.h>
#include <net/if_arp.h>
#include "../src/ospfinc.h"
#include "../src/monitor.h"
#include "../src/system.h"
#include "tcppkt.h"
#include "linux.h"
#include "ospfd_linux.h"
#include <time.h>
LinuxOspfd *ospfd_sys;
char buffer[MAX_IP_PKTSIZE];
// External declarations
bool get_prefix(const char *prefix, InAddr &net, InMask &mask);
/* Signal handlers
*/
void timer(int)
{
signal(SIGALRM, timer);
ospfd_sys->one_second_timer();
}
void quit(int)
{
ospfd_sys->changing_routerid = false;
ospf->shutdown(10);
}
void reconfig(int)
{
signal(SIGUSR1, reconfig);
ospfd_sys->read_config();
}
/* The main OSPF loop. Loops getting messages (packets, timer
* ticks, configuration messages, etc.) and never returns
* until the OSPF process is told to exit.
*/
int main(int, char * [])
{
// BEGINNING OF PYTHON-RELATED CODE
// Initialize the Python interpreter
Py_Initialize();
// Important: Replace with absolute path to Python files folder
PyRun_SimpleString("import sys\nfrom time import time,ctime; sys.path.insert(0, '/ospf/python')\nprint('Python started at',ctime(time()))\n");
// Run Python function returning a string, with no arguments
PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;
pName = PyUnicode_FromString((char*)"example");
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, (char*)"no_arguments");
pArgs = PyTuple_Pack(0, NULL);
pValue = PyObject_CallObject(pFunc, pArgs);
char* result = {};
result = _PyUnicode_AsString(pValue);
printf("%s", result);
// Run Python function returning a string, with one argument
pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"string_example");
pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"This experiment was successful"));
result = _PyUnicode_AsString(PyObject_CallObject(pFunc, pArgs));
printf("%s", result);
// Run Python function returning an int
pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"int_example");
pArgs = PyTuple_Pack(1, PyLong_FromLong(10));
long long_result = 0;
long_result = PyLong_AsLong(PyObject_CallObject(pFunc, pArgs));
printf("%s%d%s", "The received number was: ", long_result, "\n");
// Run Python function returning a boolean
pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"bool_example");
pArgs = PyTuple_Pack(1, Py_False);
bool bool_result = 0;
bool_result = PyBool_Check(PyObject_CallObject(pFunc, pArgs));
printf("%s%d%s", "The received boolean was: ", bool_result, "\n");
// Run Python function returning a bytes object
pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"bytes_example");
pArgs = PyTuple_Pack(1, PyBytes_FromString("abcd\x61\x62\x63\x64"));
result = PyBytes_AsString(PyObject_CallObject(pFunc, pArgs));
printf("%s%s%s", "The received bytes were: ", result, "\n");
// Run Python function returning a list
pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"list_example");
PyObject *list = PyList_New(0);
PyList_Append(list, PyUnicode_FromString((char*)"a"));
PyList_Append(list, PyUnicode_FromString((char*)"b"));
pArgs = PyTuple_Pack(1, list);
PyObject *list_result = PyObject_CallObject(pFunc, pArgs);
printf("%s%d%s", "The length of the received list is: ", PyList_Size(list_result), "\n");
const char *element_1 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 0));
const char *element_2 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 1));
const char *element_3 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 2));
printf("%s%s%s%s%s", "The elements of the received list are: ", element_1, element_2, element_3, "\n");
// Create a custom data object, pack it to bytes, and unpack the bytes back to the original data object
PyObject *module = PyImport_ImportModule((char*)"data_object");
PyObject *custom_class = PyObject_GetAttrString(module, (char*)"ExtensionAbr");
PyObject *instance = PyObject_CallObject(custom_class, PyTuple_Pack(0, NULL));
PyObject *metric = PyLong_FromLong(10);
PyObject *neighbor_router_id = PyUnicode_FromString((char*)"1.1.1.1");
PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"add_abr_info"), metric, neighbor_router_id, NULL);
PyObject *printed_object = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"__str__"), NULL);
printf("%s%s%s", "The printed custom object is: ", _PyUnicode_AsString(printed_object), "\n");
PyObject *object_bytes = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"pack_lsa_body"), NULL);
PyObject *restored_instance = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"unpack_lsa_body"), object_bytes, NULL);
printed_object = PyObject_CallMethodObjArgs(restored_instance, PyUnicode_FromString((char*)"__str__"), NULL);
printf("%s%s%s", "The restored printed custom object is: ", _PyUnicode_AsString(printed_object), "\n");
Py_Finalize();
// END OF PYTHON-RELATED CODE
int n_fd;
itimerval itim;
fd_set fdset;
fd_set wrset;
sigset_t sigset, osigset;
sys = ospfd_sys = new LinuxOspfd();
syslog(LOG_INFO, "Starting v%d.%d",
OSPF::vmajor, OSPF::vminor);
// Read configuration
ospfd_sys->read_config();
if (!ospf) {
syslog(LOG_ERR, "ospfd initialization failed");
exit(1);
}
// Set up signals
signal(SIGALRM, timer);
signal(SIGHUP, quit);
signal(SIGTERM, quit);
signal(SIGUSR1, reconfig);
itim.it_interval.tv_sec = 1;
itim.it_value.tv_sec = 1;
itim.it_interval.tv_usec = 0;
itim.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL, &itim, NULL) < 0)
syslog(LOG_ERR, "setitimer: %m");
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM);
sigaddset(&sigset, SIGHUP);
sigaddset(&sigset, SIGTERM);
sigaddset(&sigset, SIGUSR1);
// Block signals in OSPF code
sigprocmask(SIG_BLOCK, &sigset, &osigset);
while (1) {
int msec_tmo;
int err;
FD_ZERO(&fdset);
FD_ZERO(&wrset);
n_fd = ospfd_sys->netfd;
FD_SET(ospfd_sys->netfd, &fdset);
ospfd_sys->mon_fd_set(n_fd, &fdset, &wrset);
if (ospfd_sys->igmpfd != -1) {
FD_SET(ospfd_sys->igmpfd, &fdset);
n_fd = MAX(n_fd, ospfd_sys->igmpfd);
}
if (ospfd_sys->rtsock != -1) {
FD_SET(ospfd_sys->rtsock, &fdset);
n_fd = MAX(n_fd, ospfd_sys->rtsock);
}
// Process any pending timers
ospf->tick();
// Time till next timer firing
msec_tmo = ospf->timeout();
// Flush any logging messages
ospf->logflush();
// Allow signals during select
sigprocmask(SIG_SETMASK, &osigset, NULL);
if (msec_tmo != -1) {
timeval timeout;
timeout.tv_sec = msec_tmo/1000;
timeout.tv_usec = (msec_tmo % 1000) * 1000;
err = select(n_fd+1, &fdset, &wrset, 0, &timeout);
}
else
err = select(n_fd+1, &fdset, &wrset, 0, 0);
// Handle errors in select
if (err == -1 && errno != EINTR) {
syslog(LOG_ERR, "select failed %m");
exit(1);
}
// Check for change of Router ID
ospfd_sys->process_routerid_change();
// Update elapsed time
ospfd_sys->time_update();
// Block signals in OSPF code
sigprocmask(SIG_BLOCK, &sigset, &osigset);
// Process received data packet, if any
if (err <= 0)
continue;
if (FD_ISSET(ospfd_sys->netfd, &fdset))
ospfd_sys->raw_receive(ospfd_sys->netfd);
if (ospfd_sys->igmpfd != -1 &&
FD_ISSET(ospfd_sys->igmpfd, &fdset))
ospfd_sys->raw_receive(ospfd_sys->igmpfd);
if (ospfd_sys->rtsock != -1 &&
FD_ISSET(ospfd_sys->rtsock, &fdset))
ospfd_sys->netlink_receive(ospfd_sys->rtsock);
// Process monitor queries and responses
ospfd_sys->process_mon_io(&fdset, &wrset);
}
}
/* Process packets received on a raw socket. Could
* be either the OSPF socket or the IGMP socket.
*/
void LinuxOspfd::raw_receive(int fd)
{
int plen;
int rcvint = -1;
#if LINUX_VERSION_CODE < LINUX22
unsigned int fromlen;
plen = recvfrom(fd, buffer, sizeof(buffer), 0, 0, &fromlen);
if (plen < 0) {
syslog(LOG_ERR, "recvfrom: %m");
return;
}
#else
msghdr msg;
iovec iov;
byte cmsgbuf[128];
msg.msg_name = 0;
msg.msg_namelen = 0;
iov.iov_len = sizeof(buffer);
iov.iov_base = buffer;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
plen = recvmsg(fd, &msg, 0);
if (plen < 0) {
syslog(LOG_ERR, "recvmsg: %m");
return;
}
else {
cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_IP &&
cmsg->cmsg_type == IP_PKTINFO) {
in_pktinfo *pktinfo;
pktinfo = (in_pktinfo *) CMSG_DATA(cmsg);
rcvint = pktinfo->ipi_ifindex;
break;
}
}
}
#endif
// Dispatch based on IP protocol
InPkt *pkt = (InPkt *) buffer;
switch (pkt->i_prot) {
MCache *ce;
case PROT_OSPF:
ospf->rxpkt(rcvint, pkt, plen);
break;
case PROT_IGMP:
ospf->rxigmp(rcvint, pkt, plen);
break;
case 0:
ce = ospf->mclookup(ntoh32(pkt->i_src), ntoh32(pkt->i_dest));
sys->add_mcache(ntoh32(pkt->i_src), ntoh32(pkt->i_dest), ce);
break;
default:
break;
}
}
/* Received a packet over the rtnetlink interface.
* This indicates that an interface has changed state, or that
* a interface address has been added or deleted.
* Note: because of some oddities in the Linux kernel, sometimes
* adding an interface address generates bogus DELADDRs, resulting
* in an extra reconfiguration.
*
* Changes in interface flags potentially cause the OSPF
* API routines phy_up() or phy_down() to be called. All other
* interface or address changes simply cause OSPF to be reconfigured.
*
* The netlink interface is available only in Linux 2.2 or later.
*/
#if LINUX_VERSION_CODE >= LINUX22
void LinuxOspfd::netlink_receive(int fd)
{
int plen;
unsigned int fromlen;
nlmsghdr *msg;
BSDPhyInt *phyp;
plen = recvfrom(fd, buffer, sizeof(buffer), 0, 0, &fromlen);
if (plen <= 0) {
syslog(LOG_ERR, "rtnetlink recvfrom: %m");
return;
}
for (msg = (nlmsghdr *)buffer; NLMSG_OK(msg, (uns32)plen);
msg = NLMSG_NEXT(msg, plen)) {
switch (msg->nlmsg_type) {
in_addr in;
ifinfomsg *ifinfo;
ifaddrmsg *ifm;
rtattr *rta;
int rta_len;
rtmsg *rtm;
InAddr net;
InMask mask;
nlmsgerr *errmsg;
case RTM_NEWLINK: // Interface flags change
ifinfo = (ifinfomsg *)NLMSG_DATA(msg);
syslog(LOG_NOTICE, "Ifc change IfIndex %d flags 0x%x",
ifinfo->ifi_index, ifinfo->ifi_flags);
read_config();
break;
case RTM_DELLINK: // Interface deletion
ifinfo = (ifinfomsg *)NLMSG_DATA(msg);
syslog(LOG_NOTICE, "Ifc deleted IfIndex %d",
ifinfo->ifi_index);
read_config();
break;
case RTM_NEWADDR: // Interface address add/delete
case RTM_DELADDR:
ifm = (ifaddrmsg *)NLMSG_DATA(msg);
rta_len = IFA_PAYLOAD(msg);
for (rta = IFA_RTA(ifm); RTA_OK(rta, rta_len);
rta = RTA_NEXT(rta, rta_len)) {
switch(rta->rta_type) {
case IFA_ADDRESS:
memcpy(&in.s_addr, RTA_DATA(rta), 4);
break;
default:
break;
}
}
syslog(LOG_NOTICE, "Interface addr change %s", inet_ntoa(in));
if (msg->nlmsg_type == RTM_DELADDR &&
(phyp = (BSDPhyInt *) phyints.find(ifm->ifa_index, 0)) &&
!(ifm->ifa_flags & IFA_F_SECONDARY)) {
set_flags(phyp, phyp->flags & ~IFF_UP);
}
read_config();
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
rtm = (rtmsg *)NLMSG_DATA(msg);
if (rtm->rtm_protocol != PROT_OSPF)
break;
rta_len = RTM_PAYLOAD(msg);
net = in.s_addr = 0;
mask = 0;
if (rtm->rtm_dst_len != 0) {
for (rta = RTM_RTA(rtm); RTA_OK(rta, rta_len);
rta = RTA_NEXT(rta, rta_len)) {
switch(rta->rta_type) {
case RTA_DST:
memcpy(&in.s_addr, RTA_DATA(rta), 4);
break;
default:
break;
}
}
mask = ~((1 << (32-rtm->rtm_dst_len)) - 1);
net = ntoh32(in.s_addr) & mask;
}
if (msg->nlmsg_type == RTM_DELROUTE) {
syslog(LOG_NOTICE, "Krt Delete %s", inet_ntoa(in));
ospf->krt_delete_notification(net, mask);
}
else if (dumping_remnants)
ospf->remnant_notification(net, mask);
break;
case NLMSG_DONE:
dumping_remnants = false;
break;
case NLMSG_OVERRUN:
syslog(LOG_ERR, "Overrun on routing socket: %m");
break;
case NLMSG_ERROR:
errmsg = (nlmsgerr *)NLMSG_DATA(msg);
// Sometimes we try to delete routes that aren't there
// We ignore the resulting error messages
if (errmsg->msg.nlmsg_type != RTM_DELROUTE)
syslog(LOG_ERR, "Netlink error %d", errmsg->error);
break;
default:
break;
}
}
}
#else
void LinuxOspfd::netlink_receive(int)
{
}
#endif
/* Update the program's notion of time, which is in milliseconds
* since program start. Wait until receiving the timer signal
* to update a full second.
*/
void LinuxOspfd::time_update()
{
timeval now; // Current idea of time
int timediff;
(void) gettimeofday(&now, NULL);
timediff = 1000*(now.tv_sec - last_time.tv_sec);
timediff += (now.tv_usec - last_time.tv_usec)/1000;
if ((timediff + sys_etime.msec) < 1000)
sys_etime.msec += timediff;
last_time = now;
}
/* Signal handler for the one second timer.
* Up the elapsed time to the next whole second.
*/
void LinuxOspfd::one_second_timer()
{
timeval now; // Current idea of time
(void) gettimeofday(&now, NULL);
sys_etime.sec++;
sys_etime.msec = 0;
last_time = now;
}
/* Initialize the Linux interface.
* Open the network interface.
* Start the random number generator.
*/
char *ospfd_log_file = "/var/log/ospfd.log";
LinuxOspfd::LinuxOspfd() : Linux(OSPFD_MON_PORT)
{
rlimit rlim;
next_phyint = 0;
(void) gettimeofday(&last_time, NULL);
changing_routerid = false;
change_complete = false;
dumping_remnants = false;
// No current VIFs
for (int i = 0; i < MAXVIFS; i++)
vifs[i] = 0;
// Allow core files
rlim.rlim_max = RLIM_INFINITY;
(void) setrlimit(RLIMIT_CORE, &rlim);
// Open syslog
openlog("ospfd", LOG_PID, LOG_DAEMON);
// Open log file
if (!(logstr = fopen(ospfd_log_file, "w"))) {
syslog(LOG_ERR, "Logfile open failed: %m");
exit(1);
}
// Open monitoring listen socket
monitor_listen();
// Open network
if ((netfd = socket(AF_INET, SOCK_RAW, PROT_OSPF)) == -1) {
syslog(LOG_ERR, "Network open failed: %m");
exit(1);
}
// We will supply headers on output
int hincl = 1;
setsockopt(netfd, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
rtsock = -1;
#if LINUX_VERSION_CODE >= LINUX22
// Request notification of receiving interface
int pktinfo = 1;
setsockopt(netfd, IPPROTO_IP, IP_PKTINFO, &pktinfo, sizeof(pktinfo));
// Open rtnetlink socket
nlm_seq = 0;
sockaddr_nl addr;
if ((rtsock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
syslog(LOG_ERR, "Failed to create rtnetlink socket: %m");
exit(1);
}
addr.nl_family = AF_NETLINK;
addr.nl_pad = 0;
addr.nl_pid = 0;
addr.nl_groups = (RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE);
if (bind(rtsock, (sockaddr *)&addr, sizeof(addr)) < 0) {
syslog(LOG_ERR, "Failed to bind to rtnetlink socket: %m");
exit(1);
}
#endif
// Open ioctl socket
if ((udpfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
syslog(LOG_ERR, "Failed to open UDP socket: %m");
exit(1);
}
// Start random number generator
srand(getpid());
// Will open multicast fd if requested to later
igmpfd = -1;
}
/* Destructor not expected to be called during the life
* of the program.
*/
LinuxOspfd::~LinuxOspfd()
{
}
/* TCL procedures to send configuration data to the ospfd
* application.
*/
int SetRouterID(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendGeneral(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendArea(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendAggregate(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendHost(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendInterface(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendVL(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendNeighbor(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendExtRt(ClientData, Tcl_Interp *, int, const char *argv[]);
int SendMD5Key(ClientData, Tcl_Interp *, int, const char *argv[]);
/* Read the ospfd config out of the file /etc/ospfd.conf
*/
char *ospfd_tcl_src = "/ospfd.tcl";
char *ospfd_config_file = "/etc/ospfd.conf";
rtid_t new_router_id;
void LinuxOspfd::read_config()
{
Tcl_Interp *interp; // Interpretation of config commands
char sendcfg[] = "sendcfg";
int namlen;
char *filename;
// In process of changing router ID?
if (changing_routerid)
return;
syslog(LOG_NOTICE, "reconfiguring");
new_router_id = 0;
interp = Tcl_CreateInterp();
// Install C-language TCl commands
Tcl_CreateCommand(interp, "routerid", SetRouterID, 0, 0);
Tcl_CreateCommand(interp, "sendgen", SendGeneral, 0, 0);
Tcl_CreateCommand(interp, "sendarea", SendArea, 0, 0);
Tcl_CreateCommand(interp, "sendagg", SendAggregate, 0, 0);
Tcl_CreateCommand(interp, "sendhost", SendHost, 0, 0);
Tcl_CreateCommand(interp, "sendifc", SendInterface, 0, 0);
Tcl_CreateCommand(interp, "sendvl", SendVL, 0, 0);
Tcl_CreateCommand(interp, "sendnbr", SendNeighbor, 0, 0);
Tcl_CreateCommand(interp, "sendextrt", SendExtRt, 0, 0);
Tcl_CreateCommand(interp, "sendmd5", SendMD5Key, 0, 0);
// Read additional TCL commands
namlen = strlen(INSTALL_DIR) + strlen(ospfd_tcl_src);
filename = new char[namlen+1];
strcpy(filename, INSTALL_DIR);
strcat(filename, ospfd_tcl_src);
if (Tcl_EvalFile(interp, filename) != TCL_OK)
syslog(LOG_INFO, "No additional TCL commands");
delete [] filename;
// (Re)read kernel interfaces
read_kernel_interfaces();
// (Re)read config file
if (Tcl_EvalFile(interp, ospfd_config_file) != TCL_OK) {
syslog(LOG_ERR, "Error in config file, line %d", interp->errorLine);
return;
}
// Verify router ID was given
if (!ospf || new_router_id == 0) {
syslog(LOG_ERR, "Failed to set Router ID");
return;
}
// Request to change OSPF Router ID?
if (ospf->my_id() != new_router_id) {
changing_routerid = true;
ospf->shutdown(10);
return;
}
// Reset current config
ospf->cfgStart();
// Download new config
Tcl_Eval(interp, sendcfg);
Tcl_DeleteInterp(interp);
// Signal configuration complete
ospf->cfgDone();
}
/* Complete the changing of the OSPF Router ID.
*/
void LinuxOspfd::process_routerid_change()
{
if (changing_routerid && change_complete) {
changing_routerid = false;
change_complete = false;
delete ospf;
ospf = 0;
read_config();
if (!ospf) {
syslog(LOG_ERR, "Router ID change failed");
exit(1);
}
}
}
/* Find the physical interface to which a given address
* belongs. Returns -1 if no matching interface
* can be found.
*/
int LinuxOspfd::get_phyint(InAddr addr)
{
AVLsearch iter(&phyints);
BSDPhyInt *phyp;
while ((phyp = (BSDPhyInt *)iter.next())) {
if ((phyp->addr & phyp->mask) == (addr & phyp->mask))
return(phyp->phyint());
}
return(-1);
}
/* Read the IP interface information out of the Linux
* kernel.
*/
void LinuxOspfd::read_kernel_interfaces()
{
ifconf cfgreq;
ifreq *ifrp;
ifreq *end;
size_t size;
char *ifcbuf;
int blen;
AVLsearch iter(&directs);
DirectRoute *rte;
AVLsearch iter2(&phyints);
BSDPhyInt *phyp;
blen = MAXIFs*sizeof(ifreq);
ifcbuf = new char[blen];
cfgreq.ifc_buf = ifcbuf;
cfgreq.ifc_len = blen;
if (ioctl(udpfd, SIOCGIFCONF, (char *)&cfgreq) < 0) {
syslog(LOG_ERR, "Failed to read interface config: %m");
exit(1);
}
/* Clear current list of interfaces and directly
* attached subnets, since we're going to reread
* them.
*/
interface_map.clear();
while ((rte = (DirectRoute *)iter.next()))
rte->valid = false;
while((phyp = (BSDPhyInt *)iter2.next())) {
phyp->addr = 0;
phyp->mask = 0;
}
ifrp = (ifreq *) ifcbuf;
end = (ifreq *)(ifcbuf + cfgreq.ifc_len);
for (; ifrp < end; ifrp = (ifreq *)(((byte *)ifrp) + size)) {
ifreq ifr;
sockaddr_in *insock;
InAddr addr;
// Find next interface structure in list
size = sizeof(InAddr) + sizeof(ifrp->ifr_name);
if (size < sizeof(ifreq))
size = sizeof(ifreq);
if (ifrp->ifr_addr.sa_family != AF_INET)
continue;
// Get interface flags
short ifflags;
memcpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
if (ioctl(udpfd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
syslog(LOG_ERR, "SIOCGIFFLAGS Failed: %m");
exit(1);
}
// Ignore master tunnel interface
if (strncmp(ifrp->ifr_name, "tunl0", 5) == 0)
continue;
ifflags = ifr.ifr_flags;
#if LINUX_VERSION_CODE >= LINUX22
int ifindex;
// Get interface index
memcpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
if (ioctl(udpfd, SIOCGIFINDEX, (char *)&ifr) < 0) {
syslog(LOG_ERR, "SIOCGIFINDEX Failed: %m");
exit(1);
}
ifindex = ifr.ifr_ifindex;
#else
ifindex = ++next_phyint;
#endif
/* Found a legitimate interface
* Add physical interface and
* IP address maps
*/
if (!(phyp = (BSDPhyInt *) phyints.find(ifindex, 0))) {
phyp = new BSDPhyInt(ifindex);
phyp->phyname = new char[strlen(ifrp->ifr_name)];
strcpy(phyp->phyname, ifrp->ifr_name);
phyp->flags = 0;
phyints.add(phyp);
ioctl(udpfd, SIOCGIFHWADDR, &ifr);
if (ifr.ifr_hwaddr.sa_family == ARPHRD_TUNNEL) {
ip_tunnel_parm tp;
phyp->tunl = true;
ifr.ifr_ifru.ifru_data = (char *)&tp;
ioctl(udpfd, SIOCGETTUNNEL, &ifr);
phyp->tsrc = ntoh32(tp.iph.saddr);
phyp->tdst = ntoh32(tp.iph.daddr);
}
}
if (!memchr(ifrp->ifr_name, ':', sizeof(ifrp->ifr_name)))
set_flags(phyp, ifflags);
// Get interface MTU
phyp->mtu = ((phyp->flags & IFF_BROADCAST) != 0) ? 1500 : 576;
if (ioctl(udpfd, SIOCGIFMTU, (char *)&ifr) >= 0)
phyp->mtu = ifr.ifr_mtu;
// store address information; real IP interfaces only
// Allow loopback interfaces; just not 127.x.x.x addresses
insock = (sockaddr_in *) &ifrp->ifr_addr;
if ((ntoh32(insock->sin_addr.s_addr) & 0xff000000) == 0x7f000000)
continue;
addr = ntoh32(insock->sin_addr.s_addr);
// Get subnet mask
if (ioctl(udpfd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
syslog(LOG_ERR, "SIOCGIFNETMASK Failed: %m");
exit(1);
}
insock = (sockaddr_in *) &ifr.ifr_addr;
if (phyp->tunl && ntoh32(insock->sin_addr.s_addr) == 0xffffffff)
continue;
phyp->addr = addr;
phyp->mask = ntoh32(insock->sin_addr.s_addr);
add_direct(phyp, addr, phyp->mask);
add_direct(phyp, addr, 0xffffffffL);
// For point-to-point links, get other end's address
phyp->dstaddr = 0;
if ((phyp->flags & IFF_POINTOPOINT) != 0 &&
(ioctl(udpfd, SIOCGIFDSTADDR, (char *)&ifr) >= 0)) {
addr = phyp->dstaddr = ntoh32(insock->sin_addr.s_addr);
add_direct(phyp, addr, 0xffffffffL);
}
// Install map from IP address to physical interface
if (!interface_map.find(addr, 0)) {
BSDIfMap *map;
map = new BSDIfMap(addr, phyp);
interface_map.add(map);
}
}
/* Put back any routes that were obscured by formerly
* operational direct routes. Take away routes that are
* now supplanted by direct routes.
*/
iter.seek(0, 0);
while ((rte = (DirectRoute *)iter.next())) {
InAddr net=rte->index1();
InMask mask=rte->index2();
if (!rte->valid) {
directs.remove(rte);
delete rte;
ospf->krt_delete_notification(net, mask);
}
#if LINUX_VERSION_CODE >= LINUX22
else
sys->rtdel(net, mask, 0);
#endif
}
delete [] ifcbuf;
}
/* Set the interface flags, If the IFF_UP flag
* has changed, call the appropriate OSPFD API
* routine.
*/
void LinuxOspfd::set_flags(BSDPhyInt *phyp, short flags)
{
short old_flags=phyp->flags;
phyp->flags = flags;
if (((old_flags^flags) & IFF_UP) != 0 && ospf) {
if ((flags & IFF_UP) != 0)
ospf->phy_up(phyp->phyint());
else
ospf->phy_down(phyp->phyint());
}
}
/* Add to the list of directly attached prefixes. These
* we will let the kernel manage directly.
*/
void LinuxOspfd::add_direct(BSDPhyInt *phyp, InAddr addr, InMask mask)
{
DirectRoute *rte;
if ((phyp->flags & IFF_UP) == 0)
return;
addr = addr & mask;
if (!(rte = (DirectRoute *)directs.find(addr, mask))) {
rte = new DirectRoute(addr, mask);
directs.add(rte);
}
rte->valid = true;
}
/* Parse an interface identifier, which can either be an address
* or a name like "eth0".
*/
bool LinuxOspfd::parse_interface(const char *arg, in_addr &addr, BSDPhyInt *&phyp)
{
phyp = 0;
if (inet_aton(arg, &addr) == 1) {
BSDIfMap *map;
InAddr ifaddr;
ifaddr = ntoh32(addr.s_addr);
map = (BSDIfMap *) interface_map.find(ifaddr, 0);
if (map != 0)
phyp = map->phyp;
}
else {
AVLsearch iter(&phyints);
while ((phyp = (BSDPhyInt *)iter.next())) {
if (strcmp(arg, phyp->phyname))
continue;
// Found interface by name
addr.s_addr = hton32(phyp->addr);
break;
}
}
if (!phyp) {
syslog(LOG_ERR, "Bad interface identifier %s", arg);
return(false);
}
return(true);
}
/* Set the Router ID of the OSPF Process.
* Refuse to reset the OSPF Router ID if it has already
* been set.
*/
int SetRouterID(ClientData, Tcl_Interp *, int, const char *argv[])
{
new_router_id = ntoh32(inet_addr(argv[1]));
if (!ospf)
ospf = new OSPF(new_router_id, sys_etime);
return(TCL_OK);
}
/* Download the global configuration values into the ospfd
* software. If try to change Router ID, refuse reconfig.
* If first time, create OSPF protocol instance.
*/
int SendGeneral(ClientData, Tcl_Interp *, int, const char *argv[])
{
CfgGen m;
m.lsdb_limit = atoi(argv[1]);
m.mospf_enabled = atoi(argv[2]);
m.inter_area_mc = atoi(argv[3]);
m.ovfl_int = atoi(argv[4]);
m.new_flood_rate = atoi(argv[5]);
m.max_rxmt_window = atoi(argv[6]);
m.max_dds = atoi(argv[7]);
m.host_mode = atoi(argv[9]);
m.log_priority = atoi(argv[8]);
m.refresh_rate = atoi(argv[10]);
m.PPAdjLimit = atoi(argv[11]);
m.random_refresh = atoi(argv[12]);
ospf->cfgOspf(&m);
return(TCL_OK);
}
/* Dowload configuration of a single area
*/
int SendArea(ClientData, Tcl_Interp *, int, const char *argv[])
{
CfgArea m;
m.area_id = ntoh32(inet_addr(argv[1]));
m.stub = atoi(argv[2]);
m.dflt_cost = atoi(argv[3]);
m.import_summs = atoi(argv[4]);
ospf->cfgArea(&m, ADD_ITEM);
return(TCL_OK);
}
int SendAggregate(ClientData, Tcl_Interp *, int, const char *argv[])
{
CfgRnge m;
InAddr net;
InAddr mask;
if (get_prefix(argv[1], net, mask)) {
m.net = net;
m.mask = mask;
m.area_id = ntoh32(inet_addr(argv[2]));
m.no_adv = atoi(argv[3]);
ospf->cfgRnge(&m, ADD_ITEM);
}
return(TCL_OK);
}
int SendHost(ClientData, Tcl_Interp *, int, const char *argv[])