CSIT-229: ip4-lispgpe-ip4
[csit.git] / resources / libraries / python / IPFIXUtil.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """IPFIX utilities library. Provides classes that allow scapy to work
15 with IPFIX packets.
16
17  Note:
18  Template and data sets in one packet are not supported.
19  Option template sets (Set_ID = 3) are not supported.
20   """
21
22
23 from scapy.all import Packet, bind_layers
24 from scapy.fields import *
25 from scapy.layers.inet import UDP
26 from scapy.layers.inet6 import IP6Field
27 from scapy.contrib.ppi_geotag import UTCTimeField
28
29
30 class IPFIXHandler(object):
31     """Class for handling IPFIX packets. To use, create instance of class before
32      dissecting IPFIX packets with scapy, then run update_template every time
33      an IPFIX template packet is received."""
34
35     template_elements = {
36         4: ByteField("Protocol_ID", 0x00),
37         7: ShortField("src_port", 0),
38         8: IPField("IPv4_src", ""),
39         11: ShortField("dst_port", 0),
40         12: IPField("IPv4_dst", ""),
41         27: IP6Field("IPv6_src", "::"),
42         28: IP6Field("IPv6_dst", "::"),
43         86: LongField("packetTotalCount", 0),
44         180: ShortField("udp_src_port", 0),
45         181: ShortField("udp_dst_port", 0),
46         182: ShortField("tcp_src_port", 0),
47         183: ShortField("tcp_dst_port", 0),
48         193: ByteField("Next_header", 0x00)
49     }
50
51     def __init__(self):
52         """Initializer, registers IPFIX header and template layers with scapy.
53         """
54         bind_layers(UDP, IPFIXHeader, dport=4739)
55         bind_layers(IPFIXHeader, IPFIXTemplate, Set_ID=2)
56
57     def update_template(self, packet):
58         """Updates IPFIXData class with new data template. Registers IPFIX data
59         layer with scapy using the new template.
60
61         :param packet: Packet containing an IPFIX template.
62         :type packet: scapy.Ether
63         """
64         template_list = packet['IPFIX template'].Template
65         template_id = packet['IPFIX template'].Template_ID
66
67         IPFIXData.fields_desc = []
68         for item in template_list[::2]:
69             try:
70                 IPFIXData.fields_desc.append(self.template_elements[item])
71             except KeyError:
72                 raise KeyError(
73                     "Unknown IPFIX template element with ID {0}".format(item))
74         bind_layers(IPFIXHeader, IPFIXData, Set_ID=template_id)
75         # if the packet doesn't end here, assume it contains more data sets
76         bind_layers(IPFIXData, IPFIXData)
77
78
79 class IPFIXHeader(Packet):
80     """Class for IPFIX header."""
81     name = "IPFIX header"
82     fields_desc = [StrFixedLenField("Version", 0x000a, length=2),
83                    ShortField("Message Length", 0),
84                    UTCTimeField("Timestamp(UTC)", ""),
85                    IntField("Sequence Number", 0),
86                    IntField("Observation Domain ID", 0),
87                    ShortField("Set_ID", 0),
88                    ShortField("Set_Length", 0)
89                    ]
90
91
92 class IPFIXTemplate(Packet):
93     """Class for IPFIX template layer."""
94     name = "IPFIX template"
95     fields_desc = [ShortField("Template_ID", 256),
96                    ShortField("nFields", 2),
97                    FieldListField("Template", [], ShortField("type_len", ""),
98                                   count_from=lambda p: p.nFields*2)
99                    ]
100
101
102 class IPFIXData(Packet):
103     """Class for IPFIX data layer. Needs to be updated with
104     a template before use."""
105     name = "IPFIX flow data"
106     fields_desc = []