2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Traffic script that listens for incoming BGP connections and verifies
16 received GBP Open message."""
21 from scapy.main import load_contrib
22 from scapy.layers.inet import Raw
23 from scapy.contrib.bgp import BGPHeader, BGPOpen
25 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
29 """Open a TCP listener socket on the default BGP port. Accept an incoming
30 connection, receive data and verify if data is a valid BGP Open message."""
31 args = TrafficScriptArg(['rx_ip', 'src_ip', 'rx_port', 'as_number',
34 rx_ip = args.get_arg('rx_ip')
35 src_ip = args.get_arg('src_ip')
36 rx_port = int(args.get_arg('rx_port'))
37 as_number = int(args.get_arg('as_number'))
38 holdtime = int(args.get_arg('holdtime'))
42 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
43 soc.bind((rx_ip, rx_port))
46 print "Listener active, awaiting connection..."
48 conn, addr = soc.accept()
49 print 'Connection established with peer:', addr
55 bgp_layer = (BGPHeader(Raw(data).load))
58 if not bgp_layer.haslayer(BGPOpen):
59 raise RuntimeError("Received data is not a BGP OPEN message.")
60 bgp_open = bgp_layer.getlayer(BGPOpen)
61 if bgp_open.bgp_id != src_ip:
63 "BGP ID mismatch. Received {0} but should be {1}".
64 format(bgp_open.bgp_id, src_ip))
66 print "BGP ID matched."
68 if bgp_open.AS != as_number:
70 "BGP AS number mismatch. Received {0} but should be {1}".
71 format(bgp_open.AS, as_number))
73 print "BGP AS number matched."
75 if bgp_open.hold_time != holdtime:
77 "Hold Time parameter mismatch. Received {0} but should be {1}.".
78 format(bgp_layer.getlayer(BGPOpen).holdtime, holdtime))
80 print "BGP Hold Time parameter matched."
84 if __name__ == "__main__":