summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/bmp_collector/bmp.py
blob: b07329cd523f76d885239451f3d495e175915b01 (plain)
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
# SPDX-License-Identifier: ISC

# Copyright 2023 6WIND S.A.
# Authored by Farid Mihoub <farid.mihoub@6wind.com>
#
"""
BMP main module:
    - dissect monitoring messages in the way to get updated/withdrawed prefixes
    - XXX: missing RFCs references
    - XXX: more bmp messages types to dissect
    - XXX: complete bgp message dissection
"""
import datetime
import ipaddress
import json
import os
import struct

from bgp.update import BGPUpdate
from bgp.update.rd import RouteDistinguisher


SEQ = 0
LOG_DIR = "/var/log/"
LOG_FILE = "/var/log/bmp.log"

IS_ADJ_RIB_OUT = 1 << 4
IS_AS_PATH = 1 << 5
IS_POST_POLICY = 1 << 6
IS_IPV6 = 1 << 7
IS_FILTERED = 1 << 7

if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)

def bin2str_ipaddress(ip_bytes, is_ipv6=False):
    if is_ipv6:
        return str(ipaddress.IPv6Address(ip_bytes))
    return str(ipaddress.IPv4Address(ip_bytes[-4:]))

def log2file(logs):
    """
    XXX: extract the useful information and save it in a flat dictionnary
    """
    with open(LOG_FILE, 'a') as f:
        f.write(json.dumps(logs) + "\n")


#------------------------------------------------------------------------------
class BMPCodes:
    """
    XXX: complete the list, provide RFCs.
    """
    VERSION = 0x3

    BMP_MSG_TYPE_ROUTE_MONITORING = 0x00
    BMP_MSG_TYPE_STATISTICS_REPORT = 0x01
    BMP_MSG_TYPE_PEER_DOWN_NOTIFICATION =  0x02
    BMP_MSG_TYPE_PEER_UP_NOTIFICATION =  0x03
    BMP_MSG_TYPE_INITIATION = 0x04
    BMP_MSG_TYPE_TERMINATION = 0x05
    BMP_MSG_TYPE_ROUTE_MIRRORING = 0x06
    BMP_MSG_TYPE_ROUTE_POLICY = 0x64

    # initiation message types
    BMP_INIT_INFO_STRING = 0x00
    BMP_INIT_SYSTEM_DESCRIPTION = 0x01
    BMP_INIT_SYSTEM_NAME = 0x02
    BMP_INIT_VRF_TABLE_NAME = 0x03
    BMP_INIT_ADMIN_LABEL = 0x04

    # peer types
    BMP_PEER_GLOBAL_INSTANCE = 0x00
    BMP_PEER_RD_INSTANCE = 0x01
    BMP_PEER_LOCAL_INSTANCE = 0x02
    BMP_PEER_LOC_RIB_INSTANCE = 0x03

    # peer header flags
    BMP_PEER_FLAG_IPV6 = 0x80
    BMP_PEER_FLAG_POST_POLICY = 0x40
    BMP_PEER_FLAG_AS_PATH = 0x20
    BMP_PEER_FLAG_ADJ_RIB_OUT = 0x10

    # peer loc-rib flag
    BMP_PEER_FLAG_LOC_RIB = 0x80
    BMP_PEER_FLAG_LOC_RIB_RES = 0x7F

    # statistics type
    BMP_STAT_PREFIX_REJ = 0x00
    BMP_STAT_PREFIX_DUP = 0x01
    BMP_STAT_WITHDRAW_DUP = 0x02
    BMP_STAT_CLUSTER_LOOP = 0x03
    BMP_STAT_AS_LOOP = 0x04
    BMP_STAT_INV_ORIGINATOR = 0x05
    BMP_STAT_AS_CONFED_LOOP = 0x06
    BMP_STAT_ROUTES_ADJ_RIB_IN = 0x07
    BMP_STAT_ROUTES_LOC_RIB = 0x08
    BMP_STAT_ROUTES_PER_ADJ_RIB_IN = 0x09
    BMP_STAT_ROUTES_PER_LOC_RIB = 0x0A
    BMP_STAT_UPDATE_TREAT = 0x0B
    BMP_STAT_PREFIXES_TREAT = 0x0C
    BMP_STAT_DUPLICATE_UPDATE = 0x0D
    BMP_STAT_ROUTES_PRE_ADJ_RIB_OUT = 0x0E
    BMP_STAT_ROUTES_POST_ADJ_RIB_OUT = 0x0F
    BMP_STAT_ROUTES_PRE_PER_ADJ_RIB_OUT = 0x10
    BMP_STAT_ROUTES_POST_PER_ADJ_RIB_OUT = 0x11

    # peer down reason code
    BMP_PEER_DOWN_LOCAL_NOTIFY = 0x01
    BMP_PEER_DOWN_LOCAL_NO_NOTIFY = 0X02
    BMP_PEER_DOWN_REMOTE_NOTIFY = 0X03
    BMP_PEER_DOWN_REMOTE_NO_NOTIFY = 0X04
    BMP_PEER_DOWN_INFO_NO_LONGER = 0x05
    BMP_PEER_DOWN_SYSTEM_CLOSED = 0X06

    # termincation message types
    BMP_TERM_TYPE_STRING = 0x00
    BMP_TERM_TYPE_REASON = 0X01

    # termination reason code
    BMP_TERM_REASON_ADMIN_CLOSE = 0x00
    BMP_TERM_REASON_UNSPECIFIED = 0x01
    BMP_TERM_REASON_RESOURCES = 0x02
    BMP_TERM_REASON_REDUNDANT = 0x03
    BMP_TERM_REASON_PERM_CLOSE = 0x04

    # policy route tlv
    BMP_ROUTE_POLICY_TLV_VRF = 0x00
    BMP_ROUTE_POLICY_TLV_POLICY= 0x01
    BMP_ROUTE_POLICY_TLV_PRE_POLICY = 0x02
    BMP_ROUTE_POLICY_TLV_POST_POLICY = 0x03
    BMP_ROUTE_POLICY_TLV_STRING = 0x04


#------------------------------------------------------------------------------
class BMPMsg:
    """
    XXX: should we move register_msg_type and look_msg_type
    to generic Type class.
    """
    TYPES = {}
    UNKNOWN_TYPE = None
    HDR_STR = '!BIB'
    MIN_LEN = struct.calcsize(HDR_STR)
    TYPES_STR = {
        BMPCodes.BMP_MSG_TYPE_INITIATION: 'initiation',
        BMPCodes.BMP_MSG_TYPE_PEER_DOWN_NOTIFICATION: 'peer down notification',
        BMPCodes.BMP_MSG_TYPE_PEER_UP_NOTIFICATION: 'peer up notification',
        BMPCodes.BMP_MSG_TYPE_ROUTE_MONITORING: 'route monitoring',
        BMPCodes.BMP_MSG_TYPE_STATISTICS_REPORT: 'statistics report',
        BMPCodes.BMP_MSG_TYPE_TERMINATION: 'termination',
        BMPCodes.BMP_MSG_TYPE_ROUTE_MIRRORING: 'route mirroring',
        BMPCodes.BMP_MSG_TYPE_ROUTE_POLICY: 'route policy',
    }

    @classmethod
    def register_msg_type(cls, msgtype):
        def _register_type(subcls):
            cls.TYPES[msgtype] = subcls
            return subcls
        return _register_type

    @classmethod
    def lookup_msg_type(cls, msgtype):
        return cls.TYPES.get(msgtype, cls.UNKNOWN_TYPE)

    @classmethod
    def dissect_header(cls, data):
        """
        0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |    Version    |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                        Message Length                         |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        | Message Type  |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        """
        if len(data) < cls.MIN_LEN:
            pass
        else:
            _version, _len, _type = struct.unpack(cls.HDR_STR, data[0:cls.MIN_LEN])
            return _version, _len, _type

    @classmethod
    def dissect(cls, data):
        global SEQ
        version, msglen, msgtype = cls.dissect_header(data)

        msg_data = data[cls.MIN_LEN:msglen]
        data = data[msglen:]

        if version != BMPCodes.VERSION:
            # XXX: log something
            return data

        msg_cls = cls.lookup_msg_type(msgtype)
        if msg_cls == cls.UNKNOWN_TYPE:
            # XXX: log something
            return data

        msg_cls.MSG_LEN = msglen - cls.MIN_LEN
        logs = msg_cls.dissect(msg_data)
        logs["seq"] = SEQ
        log2file(logs)
        SEQ += 1

        return data


#------------------------------------------------------------------------------
class BMPPerPeerMessage:
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Peer Type   | Peer Flags    |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                                                               |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                Peer Address (16 bytes)                        |
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                          Peer AS                              |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                         Peer BGP ID                           |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                      Timestamp (seconds)                      |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Timestamp (microseconds)                   |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    PEER_UNPACK_STR = '!BB8s16sI4sII'
    PEER_TYPE_STR = {
        BMPCodes.BMP_PEER_GLOBAL_INSTANCE: 'global instance',
        BMPCodes.BMP_PEER_RD_INSTANCE: 'route distinguisher instance',
        BMPCodes.BMP_PEER_LOCAL_INSTANCE: 'local instance',
        BMPCodes.BMP_PEER_LOC_RIB_INSTANCE: 'loc-rib instance',
    }

    @classmethod
    def dissect(cls, data):
        (peer_type,
         peer_flags,
         peer_distinguisher,
         peer_address,
         peer_asn,
         peer_bgp_id,
         timestamp_secs,
         timestamp_microsecs) = struct.unpack_from(cls.PEER_UNPACK_STR, data)

        msg = {'peer_type': cls.PEER_TYPE_STR[peer_type]}

        if peer_type == 0x03:
            msg['is_filtered'] = bool(peer_flags & IS_FILTERED)
        else:
            # peer_flags = 0x0000 0000
            # ipv6, post-policy, as-path, adj-rib-out, reserverdx4
            is_adj_rib_out = bool(peer_flags & IS_ADJ_RIB_OUT)
            is_as_path = bool(peer_flags & IS_AS_PATH)
            is_post_policy = bool(peer_flags & IS_POST_POLICY)
            is_ipv6 = bool(peer_flags & IS_IPV6)
            msg['post_policy'] = is_post_policy
            msg['ipv6'] = is_ipv6
            msg['peer_ip'] = bin2str_ipaddress(peer_address, is_ipv6)


        peer_bgp_id = bin2str_ipaddress(peer_bgp_id)
        timestamp = float(timestamp_secs) + timestamp_microsecs * (10 ** -6)

        data = data[struct.calcsize(cls.PEER_UNPACK_STR):]
        msg.update({
            'peer_distinguisher': str(RouteDistinguisher(peer_distinguisher)),
            'peer_asn': peer_asn,
            'peer_bgp_id': peer_bgp_id,
            'timestamp': str(datetime.datetime.fromtimestamp(timestamp)),
        })

        return data, msg


#------------------------------------------------------------------------------
@BMPMsg.register_msg_type(BMPCodes.BMP_MSG_TYPE_ROUTE_MONITORING)
class BMPRouteMonitoring(BMPPerPeerMessage):

    @classmethod
    def dissect(cls, data):
        data, peer_msg = super().dissect(data)
        data, update_msg = BGPUpdate.dissect(data)
        return {**peer_msg, **update_msg}


#------------------------------------------------------------------------------
class BMPStatisticsReport:
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                        Stats Count                            |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |         Stat Type             |          Stat Len             |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                        Stat Data                              |
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    pass


#------------------------------------------------------------------------------
class BMPPeerDownNotification:
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |    Reason     |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |            Data (present if Reason = 1, 2 or 3)               |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    pass


#------------------------------------------------------------------------------
@BMPMsg.register_msg_type(BMPCodes.BMP_MSG_TYPE_PEER_UP_NOTIFICATION)
class BMPPeerUpNotification(BMPPerPeerMessage):
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                 Local Address (16 bytes)                      |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |          Local Port           |           Remote Port         |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Sent OPEN Message                         #|
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                  Received OPEN Message                        |
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    UNPACK_STR = '!16sHH'
    MIN_LEN = struct.calcsize(UNPACK_STR)
    MSG_LEN = None

    @classmethod
    def dissect(cls, data):
        data, peer_msg = super().dissect(data)

        (local_addr,
         local_port,
         remote_port) = struct.unpack_from(cls.UNPACK_STR, data)

        msg = {
            **peer_msg,
            **{
                'local_ip': bin2str_ipaddress(local_addr, peer_msg.get('ipv6')),
                'local_port': int(local_port),
                'remote_port': int(remote_port),
            },
        }

        # XXX: dissect the bgp open message

        return msg


#------------------------------------------------------------------------------
@BMPMsg.register_msg_type(BMPCodes.BMP_MSG_TYPE_INITIATION)
class BMPInitiation:
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |      Information Type         |        Information Length     |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                   Information (variable)                      |
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    TLV_STR = '!HH'
    MIN_LEN = struct.calcsize(TLV_STR)
    FIELD_TO_STR = {
        BMPCodes.BMP_INIT_INFO_STRING: 'information',
        BMPCodes.BMP_INIT_ADMIN_LABEL: 'admin_label',
        BMPCodes.BMP_INIT_SYSTEM_DESCRIPTION: 'system_description',
        BMPCodes.BMP_INIT_SYSTEM_NAME: 'system_name',
        BMPCodes.BMP_INIT_VRF_TABLE_NAME: 'vrf_table_name',
    }

    @classmethod
    def dissect(cls, data):
        msg = {}
        while len(data) > cls.MIN_LEN:
            _type, _len = struct.unpack_from(cls.TLV_STR, data[0:cls.MIN_LEN])
            _value = data[cls.MIN_LEN: cls.MIN_LEN + _len].decode()

            msg[cls.FIELD_TO_STR[_type]] = _value
            data = data[cls.MIN_LEN + _len:]

        return msg


#------------------------------------------------------------------------------
class BMPTermination:
    """
    0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |          Information Type     |       Information Length      |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                 Information (variable)                        |
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    """
    pass


#------------------------------------------------------------------------------
class BMPRouteMirroring:
    pass


#------------------------------------------------------------------------------
class BMPRoutePolicy:
    pass