forked from willglynn/python-rdnbrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdnbrd
executable file
·1154 lines (922 loc) · 44 KB
/
rdnbrd
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
#!/usr/bin/python
# Copyright (C) 2014-2017 Cumulus Networks, inc.
#
# All Rights reserved.
#
# This software is subject to the Cumulus Networks End User License Agreement available
# at the following locations:.
#
# Internet: https://cumulusnetworks.com/downloads/eula/latest/view/
#
# Cumulus Linux systems: /usr/share/cumulus/EULA.txt
#
#
# 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 - see the file COPYING.
#
#
# This is the redistribute ARP/ND daemon. It monitors the ip neighbor and link
# groups via netlink and for every valid entry added/deleted in the neighbor
# table, adds/deletes a route entry corresponding to the neighbor in the
# specified table.
from daemon import DaemonContext
from ipaddr import IPAddress
from nlmanager.nllistener import NetlinkManagerWithListener, NetlinkListener
from nlmanager.nlpacket import (Address, Neighbor, Route,
RTMGRP_LINK, RTMGRP_NEIGH, RTMGRP_IPV4_IFADDR,
RTM_NEWNEIGH, RTM_DELNEIGH, RTM_NEWLINK, RTM_DELLINK, RTM_NEWADDR, RTM_DELADDR)
from nlmanager.nlpacket import Link as nlLink
from socket import AF_INET, AF_INET6, AF_BRIDGE
from subprocess import CalledProcessError, check_call, check_output
from select import select
from time import sleep
from threading import Thread, Event, Lock
import argparse
import binascii
import datetime
import lockfile
import logging
import logging.handlers
import os
import re
import signal
import socket
import struct
import sys
log = logging.getLogger('rdnbrd')
ARP_ETHERTYPE = 0x0806
def logger_init(loglevel):
loglevel = loglevel.upper()
# Set the log level for the root logger
logging.getLogger().setLevel(loglevel)
syslog_h = logging.handlers.SysLogHandler(address='/dev/log')
formatter = logging.Formatter('rdnbrd: %(levelname)7s: %(message)s')
syslog_h.setFormatter(formatter)
syslog_h.setLevel(loglevel)
log.addHandler(syslog_h)
logging.getLogger('nlmanager.nlmanager').addHandler(syslog_h)
logging.getLogger('nlmanager.nlpacket').addHandler(syslog_h)
logging.getLogger('nlmanager.nllistener').addHandler(syslog_h)
log.info("logging to syslog with level %s" % loglevel)
def get_ifindex(ifname):
output = check_output(['/sbin/ip', 'link', 'show', ifname])
for line in output.splitlines():
re_line = re.search("^(\d+): %s: " % ifname, line)
if re_line:
return int(re_line.group(1))
return None
def get_ifname(ifindex):
output = check_output(['/sbin/ip', 'link', 'show'])
for line in output.splitlines():
re_line = re.search("^%d: (\S+):" % ifindex, line)
if re_line:
return re_line.group(1)
return None
def get_my_macs():
"""
Return a dictionary of MAC addresses used by our own interfaces
"""
macs = {}
output = check_output(['/sbin/ip', 'link', 'show'])
for line in output.splitlines():
re_line = re.search("ether (\S+)", line)
if re_line:
mac = re_line.group(1).replace(':', '').upper()
mac = "%s.%s.%s" % (mac[0:4], mac[4:8], mac[8:12])
macs[mac] = True
return macs
class ArpListener(Thread):
"""
Process ARP packets RXed for each Link. Extract the source IP/MAC
and update last_arp_rxed_epoch for that Host.
"""
def __init__(self, parent):
Thread.__init__(self)
self.shutdown_event = Event()
self.parent = parent
def run(self):
ARP_REQUEST = 1
ARP_REPLY = 2
ARP_PROTOCOL_IP = 0x0800
try:
while True:
rx_sockets = []
rx_socket2link = {}
for link in self.parent.links_by_index.values():
if link.up and link.src_ip and link.rx_socket:
rx_sockets.append(link.rx_socket)
rx_socket2link[link.rx_socket] = link
# Wait for at least one of the sockets to be ready for processing.
# We only let this run for 1 second so we can check to see if the
# shutdown flag has been set.
try:
readable, writeable, exceptional = select(rx_sockets, [], [], 1)
except Exception as e:
# 9 is 'Bad file descriptor', this can happen if the link goes
# down between when we added link.rx_socket to rx_sockets and
# when we call select()
if isinstance(e.args, tuple):
if e[0] == 9:
continue
else:
raise
else:
raise
if self.shutdown_event.is_set():
log.info("ArpListener shutting down")
break
arps_rxed = []
# Parse all RXed ARP packets and create a workq entry so our parent
# can process the fact that these IPs are alive
for s in readable:
link = rx_socket2link.get(s)
try:
for pkt in s.recvfrom(512):
# recvfrom() can also return a tuple like the following...ignore these
# ('swp3', 2054, 0, 1, '\x00\x02\x00\x00\x00\x01')
if isinstance(pkt, tuple):
continue
arp_protocol_type = struct.unpack('>H', pkt[16:18])[0]
arp_opcode = struct.unpack('>H', pkt[20:22])[0]
if arp_protocol_type == ARP_PROTOCOL_IP and (arp_opcode == ARP_REQUEST or arp_opcode == ARP_REPLY):
arp_src_mac_raw = pkt[22:28]
arp_src_ip = socket.inet_ntoa(pkt[28:32])
arp_src_mac = ".".join(binascii.hexlify(arp_src_mac_raw).upper()[x:x + 4]
for x in xrange(0, len(binascii.hexlify(arp_src_mac_raw)), 4))
if link.debug_arp:
log.debug("%s: RXed ARP %s from %s with MAC %s" %
(link, 'request' if arp_opcode == ARP_REQUEST else 'reply',
arp_src_ip, arp_src_mac))
arps_rxed.append((link, arp_src_ip, arp_src_mac, arp_src_mac_raw))
except Exception as e:
if isinstance(e.args, tuple):
# 'Network is down'...can happen if someone does an ifdown
# while we are trying to read from the raw socket
if e[0] == 100:
log.debug("%s: 'Network is down', recvfrom() failed" % link)
elif e[0] == 9:
log.debug("%s: 'Bad file descriptor', recvfrom() failed" % link)
elif e[0] == 6:
log.debug("%s: 'No such device or address', recvfrom() failed" % self)
else:
raise
else:
raise
if arps_rxed:
self.parent.workq.put(('ARP_REQUEST_OR_REPLY_RXED', arps_rxed))
self.parent.alarm.set()
except Exception as e:
log.exception(e)
self.parent.caught_exception = True
self.parent.shutdown_event.set()
class TimerWheel(Thread):
"""
A basic timer wheel with 1s granularity
"""
def __init__(self, parent):
Thread.__init__(self)
self.parent = parent
self.shutdown_event = Event()
self.events = {}
self.events_lock = Lock()
def run(self):
# Use os.times() (instead of time.time()) so that changes in the system
# date/time do not throw us off
epoch_previous = int(os.times()[4])
try:
while True:
if self.shutdown_event.is_set():
log.info("TimerWheel shutting down")
break
# We should only sleep for 1 second each time through this loop but
# if somehow we sleep for more than 1 second we need to make sure
# we do all of the work that we should have done in the second that
# we skipped. Keep track of the previous epoch that we serviced
# and loop from the second after that to the current second.
#
# We don't actually do the heavy lifting here, instead we stick the
# task on our parent's workq.
epoch_current = int(os.times()[4])
set_alarm = False
# log.info("TimerWheel service %s to %s" %
# (datetime.datetime.fromtimestamp(epoch_previous).strftime('%H:%M:%S'),
# datetime.datetime.fromtimestamp(epoch_current+1).strftime('%H:%M:%S')))
for x in range(epoch_previous, epoch_current + 1):
# log.info("TimerWheel servicing %s" % datetime.datetime.fromtimestamp(x).strftime('%H:%M:%S'))
while x in self.events and self.events[x]:
(workq_event, workq_option) = self.events[x].pop(0)
# log.info("TimerWheel service entries for %s with Event %s, Options %s" %
# (datetime.datetime.fromtimestamp(x).strftime('%H:%M:%S'), workq_event, workq_option))
self.parent.workq.put((workq_event, workq_option))
set_alarm = True
# Remove this epoch from self.events
self.events_lock.acquire()
if x in self.events:
del self.events[x]
self.events_lock.release()
if set_alarm:
self.parent.alarm.set()
epoch_previous = epoch_current
sleep(1)
except Exception as e:
log.exception(e)
self.parent.caught_exception = True
self.parent.shutdown_event.set()
# epoch is the second that this event should take place
def add_event(self, epoch, workq_event, workq_option):
self.events_lock.acquire()
if epoch not in self.events:
self.events[epoch] = []
else:
if (workq_event, (workq_option, epoch)) in self.events[epoch]:
log.warning("TimerWheel attempt to add duplicate entry at %s for Event %s, Options %s" %
(datetime.datetime.fromtimestamp(epoch).strftime('%H:%M:%S'), workq_event, workq_option))
self.events_lock.release()
return
self.events[epoch].append((workq_event, (workq_option, epoch)))
self.events_lock.release()
# log.info("TimerWheel added entry at %s for Event %s, Options %s" %
# (datetime.datetime.fromtimestamp(epoch).strftime('%H:%M:%S'), workq_event, workq_option))
class Host(object):
def __init__(self, ip, mac):
self.ip = ip
self.ip_obj = IPAddress(ip)
self.mac = mac.upper()
self.mac_raw = self.mac.replace('.', '').replace(':', '').decode('hex')
self.last_arp_rxed_epoch = int(os.times()[4])
# True if a /32 for this host is installed in Link.route_table
self.rib_installed = False
def __str__(self):
return "%s(MAC %s)" % (self.ip, self.mac)
class Link(object):
def __init__(self, name, index, route_table, unicast_arp_requests, debug_arp, reason):
self.name = str(name)
self.index = index
self.bridge = None
self.up = False
self.hosts = {}
self.hosts_by_mac = {}
self.route_table = route_table
self.unicast_arp_requests = unicast_arp_requests
self.src_ip = None
self.src_mac = None
self.src_mac_raw = None
self.tx_socket = None
self.rx_socket = None
self.eth_hdr_broadcast = None
self.arp_request_hdr = None
self.arp_reply_hdr = None
self.arp_src = None
log.debug("%s: created Link (%s)" % (self, reason))
# Generating debug output for all of the ARP packets can be very chatty
# so use a knob to control whether or not we print these. debug_arp can
# be None so that is why we do not just do "self.debug_arp = debug_arp" here.
self.debug_arp = True if debug_arp is True else False
def __str__(self):
return self.name
def sockets_create(self):
if not self.tx_socket or not self.rx_socket:
log.debug("%s: creating raw sockets" % self)
# Note that tx_socket and rx_socket are bound slightly differently
if not self.tx_socket:
self.tx_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(ARP_ETHERTYPE))
try:
self.tx_socket.bind((self.name, socket.htons(ARP_ETHERTYPE)))
except Exception:
log.info("%s: failed to create tx_socket" % self)
self.tx_socket = None
if not self.rx_socket:
self.rx_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(ARP_ETHERTYPE))
try:
self.rx_socket.bind((self.name, 0))
except Exception:
log.info("%s: failed to create rx_socket" % self)
self.rx_socket = None
def sockets_free(self):
if self.tx_socket or self.rx_socket:
log.debug("%s: closing raw sockets" % self)
if self.tx_socket:
self.tx_socket.close()
self.tx_socket = None
if self.rx_socket:
self.rx_socket.close()
self.rx_socket = None
def send_gratuitous_arp(self, reason):
# If we do not have an IP addresss then self.eth_hdr_broadcast will be None
if self.eth_hdr_broadcast:
log.info("%s: TX gratuitious ARP (%s)" % (self, reason))
pkt = self.eth_hdr_broadcast + self.arp_reply_hdr + self.arp_src + self.arp_src
self.tx_packet(pkt)
def set_link_up(self):
"""
If the interface comes up we need to determine which hosts are reachable
"""
if not self.up:
self.up = True
self.up_epoch = int(os.times()[4])
if not self.bridge:
log.info("%s: UP...resume verifying hosts on this link %s" % (self, [x for x in self.hosts.iterkeys()]))
self.sockets_create()
self.send_gratuitous_arp('link UP')
else:
log.debug("%s: UP...ignoring because we were already up" % self)
def set_link_down(self):
"""
If the interface goes down we need to purge the host /32s via that interface
"""
if self.up:
self.up = False
log.info("%s: DOWN...delete routes via this link" % self)
self.sockets_free()
else:
log.debug("%s: DOWN...ignoring because we were already down" % self)
def tx_packet(self, pkt):
try:
self.tx_socket.send(pkt)
except Exception as e:
if isinstance(e.args, tuple):
# 'Network is down'...can happen if someone does an ifdown
# while we are trying to send to the socket
if e[0] == 100:
log.debug("%s: 'Network is down', send() failed" % self)
elif e[0] == 9:
log.debug("%s: 'Bad file descriptor', send() failed" % self)
elif e[0] == 6:
log.debug("%s: 'No such device or address', send() failed" % self)
else:
raise
else:
raise
def arp_ping(self, current_epoch, keepalive_interval):
"""
TX an ARP request for each Host
"""
if not self.up:
if self.debug_arp:
log.debug("%s: ARP ping bulk abort (interface down)" % self)
return
if not self.hosts:
if self.debug_arp:
log.debug("%s: ARP ping bulk abort (no hosts)" % self)
return
if not self.src_ip:
log.info("%s: ARP ping bulk abort (no IP)" % self)
return
if self.debug_arp:
log.debug("%s: ARP ping bulk for %s" % (self, [x for x in self.hosts.iterkeys()]))
# If there are 1000s of hosts via a single interface we do not want to arping all
# of them at the exact same time. Split them up into groups based on the keepalive
# interval. If our keepalive interval is 5s then arping 1/5 of the hosts each time
# this method is called. This requires that arping be called once every second.
epoch_remainder = current_epoch % keepalive_interval
hosts_to_arping = []
for host in self.hosts.itervalues():
host_ip_remainder = int(host.ip_obj) % keepalive_interval
if host_ip_remainder == epoch_remainder:
hosts_to_arping.append(host)
# TX an ARP request to each host
for host in hosts_to_arping:
arp_dst = struct.pack("!6s4s", '\x00\x00\x00\x00\x00\x00', socket.inet_aton(host.ip))
if self.unicast_arp_requests and host.mac_raw:
eth_hdr = struct.pack("!6s6s2s", host.mac_raw, self.src_mac_raw, '\x08\x06')
else:
eth_hdr = self.eth_hdr_broadcast
pkt = eth_hdr + self.arp_request_hdr + self.arp_src + arp_dst
self.tx_packet(pkt)
def update_arp_headers(self):
if self.src_ip and self.src_mac_raw:
self.eth_hdr_broadcast = struct.pack("!6s6s2s", '\xff\xff\xff\xff\xff\xff', self.src_mac_raw, '\x08\x06')
self.arp_request_hdr = struct.pack("!2s2s1s1s2s", '\x00\x01', '\x08\x00', '\x06', '\x04', '\x00\x01')
self.arp_reply_hdr = struct.pack("!2s2s1s1s2s", '\x00\x01', '\x08\x00', '\x06', '\x04', '\x00\x02')
self.arp_src = struct.pack("!6s4s", self.src_mac_raw, socket.inet_aton(self.src_ip))
else:
self.eth_hdr_broadcast = None
self.arp_request_hdr = None
self.arp_reply_hdr = None
self.arp_src = None
class RedistributeNeighbor(NetlinkManagerWithListener):
def __init__(self, args, groups):
NetlinkManagerWithListener.__init__(self, groups, start_listener=False)
self.args = args
self.shutdown_event = Event()
self.links_by_index = {}
self.debug_arp = {}
self.caught_exception = False
self.loglevel = 'INFO'
self.keepalive = 3
self.holdtime = 9
self.route_table = 10
self.unicast_arp_requests = True
self.name = os.path.basename(sys.argv[0])
self.pidfile = os.path.abspath("/var/run/%s.pid" % self.name)
self.lock = lockfile.FileLock(self.pidfile)
self.my_macs = get_my_macs()
self.ignore_ifindex = {}
# Logging has not been initialized yet so store errors until we can log them
self.errors_to_log = []
def signal_handler(self, signal, frame):
self.shutdown_event.set()
self.arp_listener.shutdown_event.set()
self.timer_wheel.shutdown_event.set()
self.shutdown_flag = True # For NetlinkManager shutdown
self.listener.shutdown_event.set()
def signal_term_handler(self, signal, frame):
log.info("rdnbrd: Caught SIGTERM")
self.signal_handler(signal, frame)
def signal_int_handler(self, signal, frame):
log.info("rdnbrd: Caught SIGINT")
self.signal_handler(signal, frame)
def pidfile_write(self):
"""
check/create/delete pidfile to avoid duplicate instances
"""
MAX_PIDFILE_ATTEMPTS = 10
for x in xrange(MAX_PIDFILE_ATTEMPTS):
if self.shutdown_event.is_set():
return
elif os.path.isfile(self.pidfile):
log.info("Waiting for pid file %s to be deleted (attempt %d/%d)" %
(self.pidfile, x + 1, MAX_PIDFILE_ATTEMPTS))
sleep(1)
else:
break
else:
raise RuntimeError("Another instance of %s is running, see %s" % (self.name, self.pidfile))
try:
fh = open(self.pidfile, 'w')
except Exception as e:
log.exception(e)
self.lock.release()
raise RuntimeError("Cannot open pid file %s" % self.pidfile)
fh.write("{0}\n".format(self.pid))
fh.flush()
self.pidfh = fh
log.info("PID %d written to %s" % (self.pid, self.pidfile))
def pidfile_delete(self):
os.unlink(self.pidfile)
def get_link_from_ifindex(self, ifindex, ifname, reason):
if ifindex not in self.links_by_index:
if not ifname:
ifname = get_ifname(ifindex)
self.links_by_index[ifindex] = Link(
ifname,
ifindex,
self.route_table,
self.unicast_arp_requests,
self.debug_arp.get(ifname),
reason)
return self.links_by_index[ifindex]
def ip_host_add(self, link, ip, mac, reason):
if ip is None:
host = link.hosts_by_mac.get(mac)
if host:
ip = host.ip
else:
# We sometimes get RTM_NEWNEIGH messages for our own MACs (the
# MACs of the interfaces in that bridge)...ignore them
if mac not in self.my_macs:
log.debug("%s: could not find IP for MAC %s (add %s)" % (link, mac, reason))
return None
# Ignore IPv4 link-local addresses. We see these if BGP's ENHE (RFC 5549)
# feature is enabled.
if ip.startswith('169.254.'):
return None
# Ignore unspecified source address
if ip == '0.0.0.0':
return None
# We will add the host to the bridge itself (we are a member of a bridge)
if link.bridge:
return None
if ip not in link.hosts:
link.hosts[ip] = Host(ip, mac)
link.hosts_by_mac[mac] = link.hosts[ip]
log.debug("%s: created Host with IP %s MAC %s (%s)" % (link, ip, mac, reason))
host = link.hosts[ip]
add_to_list = []
# Add to the routing table
if not host.rib_installed:
add_to_list.append('RIB')
routes = []
ecmp_routes = []
nexthop = None
prefixlen = 32
routes.append((AF_INET, int(host.ip_obj), prefixlen, nexthop, link.index))
self.routes_add(routes,
ecmp_routes,
table=self.route_table,
protocol=Route.RT_PROT_BOOT,
route_scope=Route.RT_SCOPE_LINK)
host.rib_installed = True
if add_to_list:
log.info("%s: adding host %s to %s (%s)" % (link, host, ', '.join(add_to_list), reason))
return host
def ip_host_del(self, link, ip, mac, reason):
if ip is None:
host = link.hosts_by_mac.get(mac)
if host:
ip = host.ip
else:
# We sometimes get RTM_NEWNEIGH messages for our own MACs (the
# MACs of the interfaces in that bridge)...ignore them
if mac not in self.my_macs:
log.debug("%s: could not find IP for MAC %s (del %s)" % (link, mac, reason))
return
# Ignore IPv4 link-local addresses. We see these if BGP's ENHE (RFC 5549)
# feature is enabled.
if ip.startswith('169.254.'):
return
# Ignore unspecified source address
if ip == '0.0.0.0':
return None
if ip not in link.hosts:
log.debug("%s: No information about %s to delete (%s)" % (link, ip, reason))
return
host = link.hosts[ip]
delete_from_list = []
# Delete from the routing table
if host.rib_installed:
delete_from_list.append('RIB')
routes = []
ecmp_routes = []
nexthop = None
prefixlen = 32
routes.append((AF_INET, int(host.ip_obj), prefixlen, nexthop, link.index))
self.routes_del(routes,
ecmp_routes,
table=self.route_table,
protocol=Route.RT_PROT_BOOT,
route_scope=Route.RT_SCOPE_LINK)
host.rib_installed = False
# Delete from the neighbor table
delete_from_list.append('neighbor table')
self.neighbor_del(AF_INET, link.index, int(host.ip_obj), host.mac)
if delete_from_list:
log.info("%s: deleting host %s from %s (%s)" % (link, host, ', '.join(delete_from_list), reason))
def rx_rtm_newaddr(self, msg):
NetlinkManagerWithListener.rx_rtm_newaddr(self, msg)
if msg.ifindex in self.ignore_ifindex:
return
ifname = msg.get_attribute_value(Address.IFA_LABEL)
link = self.get_link_from_ifindex(msg.ifindex, ifname, msg.get_type_string())
ip = msg.get_attribute_value(Address.IFA_ADDRESS)
if ip and not link.src_ip:
ip = str(ip)
log.info("%s: ARP source IP is now %s" % (link, ip))
link.src_ip = ip
link.update_arp_headers()
def rx_rtm_deladdr(self, msg):
NetlinkManagerWithListener.rx_rtm_deladdr(self, msg)
if msg.ifindex in self.ignore_ifindex:
return
ifname = msg.get_attribute_value(Address.IFA_LABEL)
link = self.get_link_from_ifindex(msg.ifindex, ifname, msg.get_type_string())
ip = msg.get_attribute_value(Address.IFA_ADDRESS)
if ip and link.src_ip == ip:
ip = str(ip)
log.info("%s: ARP source IP %s was removed" % (link, ip))
link.src_ip = None
link.update_arp_headers()
def rx_rtm_newlink(self, msg):
NetlinkManagerWithListener.rx_rtm_newlink(self, msg)
ifname = msg.get_attribute_value(nlLink.IFLA_IFNAME)
bridge_ifindex = msg.get_attribute_value(nlLink.IFLA_MASTER)
link = self.get_link_from_ifindex(msg.ifindex, ifname, msg.get_type_string())
linkinfo = msg.get_attribute_value(nlLink.IFLA_LINKINFO)
if linkinfo and (linkinfo.get(nlLink.IFLA_INFO_KIND) == 'vrf' or linkinfo.get(nlLink.IFLA_INFO_SLAVE_KIND) == 'vrf'):
log.info("%s: vrf interface...ignoring" % link)
self.ignore_ifindex[msg.ifindex] = True
return
else:
if msg.ifindex in self.ignore_ifindex:
log.info("%s: used to be a vrf interface but is no longer a vrf interface" % link)
del self.ignore_ifindex[msg.ifindex]
if linkinfo and (linkinfo.get(nlLink.IFLA_INFO_KIND) == 'macvlan'):
lowerintf = msg.get_attribute_value(nlLink.IFLA_LINK)
if lowerintf:
lowername = self.get_link_from_ifindex(lowerintf,"",msg.get_type_string()).name
(base, sep, idx) = ifname.partition("-v")
if lowername == base and idx.isdigit():
log.info("%s: vrr virtual interface...ignoring" % link)
self.ignore_ifindex[msg.ifindex] = True
return
link.src_mac = msg.get_attribute_value(nlLink.IFLA_ADDRESS)
if link.src_mac:
# IFLA_ADDRESS is normally a MAC address but in the case of GRE tunnels
# it will be an IPv4Address in which case link.src_mac will be an IPv4Address
# object instead of a string
if isinstance(link.src_mac, str):
link.src_mac_raw = link.src_mac.replace('.', '').replace(':', '').decode('hex')
else:
link.src_mac_raw = None
else:
link.src_mac_raw = None
link.update_arp_headers()
# For the most part we ignore links that are members of a bridge, we will
# work with the bridge interface itself. The exception is when a port
# comes UP, we need the bridge to send a gratuitous ARP.
if bridge_ifindex:
if msg.is_up():
bridge_id = self.get_link_from_ifindex(bridge_ifindex, None, msg.get_type_string())
# If we were not in a bridge before we need to call ip_host_del() for all of our hosts
if not link.bridge and bridge_id:
hosts_to_delete = []
for host in link.hosts.itervalues():
hosts_to_delete.append(host)
for host in hosts_to_delete:
self.ip_host_del(link, host.ip, host.mac, 'interface transition to bridge/bond')
del link.hosts[host.ip]
link.bridge = bridge_id
if link.bridge and not link.up:
link.set_link_up()
if link.bridge.up:
reason = "%s %s" % (link, msg.get_type_string())
link.bridge.send_gratuitous_arp(reason)
else:
log.debug("%s: is up but bridge %s is down, skip sending a gratuitous ARP" %
(link, link.bridge))
else:
link.set_link_down()
else:
if msg.is_up():
link.set_link_up()
else:
if link.up:
for host in link.hosts.itervalues():
self.ip_host_del(link, host.ip, host.mac, 'link down')
link.set_link_down()
def rx_rtm_dellink(self, msg):
NetlinkManagerWithListener.rx_rtm_dellink(self, msg)
ifname = msg.get_attribute_value(nlLink.IFLA_IFNAME)
link = self.get_link_from_ifindex(msg.ifindex, ifname, msg.get_type_string())
linkinfo = msg.get_attribute_value(nlLink.IFLA_LINKINFO)
if linkinfo and (linkinfo.get(nlLink.IFLA_INFO_KIND) == 'vrf' or linkinfo.get(nlLink.IFLA_INFO_SLAVE_KIND) == 'vrf'):
log.info("%s: vrf interface...ignoring" % link)
if msg.ifindex in self.ignore_ifindex:
del self.ignore_ifindex[msg.ifindex]
return
if link.up:
for host in link.hosts.itervalues():
self.ip_host_del(link, host.ip, host.mac, 'link deleted')
link.set_link_down()
log.info("%s: deleting link due to RTM_DELLINK" % link)
del self.links_by_index[msg.ifindex]
def rx_rtm_newneigh(self, msg):
NetlinkManagerWithListener.rx_rtm_newneigh(self, msg)
if msg.ifindex in self.ignore_ifindex:
return
ip = msg.get_attribute_value(Neighbor.NDA_DST)
mac = msg.get_attribute_value(Neighbor.NDA_LLADDR)
link = self.get_link_from_ifindex(msg.ifindex, None, msg.get_type_string())
reason = "%s %s" % (msg.get_type_string(), Neighbor.state_to_string.get(msg.state))
# If we bounce a bridge we get IPless NEWNEIGHs for the ports in that bridge
if ip:
ip = str(ip)
if link.bridge:
link = link.bridge
if msg.state in (Neighbor.NUD_PERMANENT, Neighbor.NUD_REACHABLE, Neighbor.NUD_STALE):
self.ip_host_add(link, ip, mac, reason)
else:
self.ip_host_del(link, ip, mac, reason)
def rx_rtm_delneigh(self, msg):
NetlinkManagerWithListener.rx_rtm_delneigh(self, msg)
if msg.ifindex in self.ignore_ifindex:
return
ip = msg.get_attribute_value(Neighbor.NDA_DST)
mac = msg.get_attribute_value(Neighbor.NDA_LLADDR)
link = self.get_link_from_ifindex(msg.ifindex, None, msg.get_type_string())
reason = "%s %s" % (msg.get_type_string(), Neighbor.state_to_string.get(msg.state))
# If we bounce a bridge we get IPless NEWNEIGHs for the ports in that bridge
if ip:
ip = str(ip)
if link.bridge:
link = link.bridge
self.ip_host_del(link, ip, mac, reason)
def process_config_file(self):
filename = '/etc/rdnbrd.conf'
if not os.path.isfile(filename):
self.errors_to_log.append("%s: does not exist" % filename)
return
valid_loglevels = ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG')
with open(filename) as fh:
for line in fh.readlines():
line = line.strip()
# Ignore comments and blank lines
if line.startswith('#') or not line:
pass
elif '=' in line:
(key, value) = line.split('=')
key = key.strip()
value = value.strip()
if key == 'keepalive' or key == 'holdtime' or key == 'route_table':
if value.isdigit() and int(value) > 0:
if key == 'keepalive':
self.keepalive = int(value)
elif key == 'holdtime':
self.holdtime = int(value)
elif key == 'route_table':
self.route_table = int(value)
else:
self.errors_to_log.append("%s: %s value must be a positive integer" %
(filename, key))
elif key == 'loglevel':
value = value.upper()
if value in valid_loglevels:
self.loglevel = value
else:
self.errors_to_log.append("%s: loglevel must be one of %s" %
(filename, ', '.join(valid_loglevels)))
elif key == 'unicast_arp_requests':
if value.upper() == 'TRUE':
self.unicast_arp_requests = True
else:
self.unicast_arp_requests = False
elif key == 'debug_arp':
# Forgive the user if they use commas to separate interface names
value = value.replace(',', ' ')
value = value.strip()
for ifname in value.split():
self.debug_arp[ifname] = True
else:
self.errors_to_log.append("%s: %s is not a supported key" % (filename, key))
else:
self.errors_to_log.append("%s: Unable to parse '%s'" % (filename, line))
min_holdtime = self.keepalive * 3
if self.holdtime < min_holdtime:
self.errors_to_log.append("%s: holdtime %d must be at least '3 x keepalive', setting holdtime to %d" %
(filename, self.holdtime, min_holdtime))
self.holdtime = min_holdtime
def main(self):
self.pidfile = '/var/run/rdnbrd.pid'
# Extract options from the config file
self.process_config_file()
# Now that we have parsed the config file we can setup logging
try:
logger_init(self.loglevel)
except Exception as e:
# If daemonized, stderr is /dev/null!
sys.stderr.write("Unable to set up logging:\n%s\n" % str(e))
sys.exit(1)
# Log any errors we found while parsing the config file
for line in self.errors_to_log:
log.error(line)
self.pidfh = None
self.pid = os.getpid()
self.listener = NetlinkListener(self, self.groups)
self.arp_listener = ArpListener(self)
self.timer_wheel = TimerWheel(self)
# Write the PID file
try:
self.pidfile_write()
log.info("rdnbrd daemon started, pid %d" % self.pid)
except RuntimeError as e:
log.error("rdnbrd already running, pid %d did not start!" % self.pid)
sys.exit(1)
self.arp_listener.start()
self.timer_wheel.start()
self.listener.start()
# uncomment to enable color coded hexdump
# rn.debug_address(True)
# rn.debug_link(True)
# rn.debug_neighbor(True)
# rn.debug_route(True)
self.workq.put(('GET_ALL_LINKS', None))
self.workq.put(('GET_ALL_ADDRESSES', None))
self.workq.put(('GET_ALL_NEIGHBORS', None))
current_epoch = int(os.times()[4])
self.timer_wheel.add_event(current_epoch + 1, 'CHECK_HOLDTIMES', None)
self.timer_wheel.add_event(current_epoch + 1, 'ARP_PING_TX', None)
while True:
try:
# Sleep until our alarm goes off...NetlinkListener will set the alarm once it
# has placed a NetlinkPacket object on our netlinkq. If someone places an item on