def create_packet_header_IPv6_SRH_L2(self, srcaddr, sidlist, segleft, vlan=0): """Create packet header: L2 encapsulated in SRv6: IPv6 header with SRH, L2 :param int srcaddr: IPv6 source address :param list sidlist: segment list of outer IPv6 SRH :param int segleft: segments-left field of outer IPv6 SRH :param vlan: L2 vlan; if vlan!=0 then add 802.1q header IPv6 source address is set to srcaddr IPv6 destination address is set to sidlist[segleft] """ eth = Ether(src='00:11:22:33:44:55', dst='00:55:44:33:22:11') etype = 0x8137 # IPX if vlan: # add 802.1q layer eth /= Dot1Q(vlan=vlan, type=etype) else: eth.type = etype p = IPv6(src=srcaddr, dst=sidlist[segleft]) / \ IPv6ExtHdrSegmentRouting(addresses=sidlist, segleft=segleft, nh=59) / \ eth return p
def create_packet_header_IPv6_SRH_IPv6( self, srcaddr, sidlist, segleft, insrc="1234::1", indst="4321::1", sport=1234, dport=1234, ): """Create packet header: IPv6 encapsulated in SRv6: IPv6 header with SRH, IPv6 header, UDP header :param int srcaddr: outer source address :param list sidlist: segment list of outer IPv6 SRH :param int segleft: segments-left field of outer IPv6 SRH Outer IPv6 source address is set to srcaddr Outer IPv6 destination address is set to sidlist[segleft] Inner IPv6 source addresses is 1234::1 Inner IPv6 destination address is 4321::1 UDP source port and destination port are 1234 """ p = (IPv6(src=srcaddr, dst=sidlist[segleft]) / IPv6ExtHdrSegmentRouting( addresses=sidlist, segleft=segleft, nh=41) / IPv6(src=insrc, dst=indst) / UDP(sport=sport, dport=dport)) return p
def send_twamp_test_query(self): """Send a TWAMP query to a reflector""" print('sid ist', self.monitored_path) # Get the counter for the color of the previuos interval sender_block_number = self.get_prev_color() sender_transmit_counter = self.hwadapter.read_tx_counter( sender_block_number, self.monitored_path['sidlist']) list_rev = list(self.monitored_path['sidlistrev']) mod_sidlist = utils.set_punt(list_rev) ipv6_packet = IPv6() ipv6_packet.src = 'fcff:1::1' # TODO me li da il controller? ipv6_packet.dst = list_rev[0] # TODO me li da il controller? # ipv6_packet.dst = 'fcff:3::1' #TODO me li da il controller? # print('Dest', ipv6_packet.dst) srv6_header = IPv6ExtHdrSegmentRouting() srv6_header.addresses = mod_sidlist # TODO vedere se funziona con NS variabile srv6_header.segleft = len(mod_sidlist) - 1 # TODO vedere se funziona con NS variabile srv6_header.lastentry = len(mod_sidlist) - 1 ipv6_packet_inside = IPv6() # TODO me li da il controller? ipv6_packet_inside.src = 'fd00:0:13::1' # TODO me li da il controller? ipv6_packet_inside.dst = 'fd00:0:83::2' ipv6_packet_inside.src = 'fcff:1::1' ipv6_packet_inside.dst = mod_sidlist[-1] udp_packet = UDP() udp_packet.dport = self.refl_udp_port udp_packet.sport = self.ss_udp_port # in band response TODO gestire out band nel controller sender_control_code = 1 sender_seq_num = self.monitored_path['txSequenceNumber'] twamp_data = twamp.TWAMPTestQuery( SequenceNumber=sender_seq_num, TransmitCounter=sender_transmit_counter, BlockNumber=sender_block_number, SenderControlCode=sender_control_code) pkt = (ipv6_packet / srv6_header / ipv6_packet_inside / udp_packet / twamp_data) print( 'SS - SEND QUERY SL {sl} - SN {sn} - TXC {txc} - C {col}'.format( sl=mod_sidlist, sn=sender_seq_num, txc=sender_transmit_counter, col=sender_block_number)) send(pkt, count=1, verbose=False) # Increase the SN self.monitored_path['txSequenceNumber'] += 1
def create_packet_header_IPv6_SRH_IPv6(self, sidlist, segleft): """Create packet header: IPv6 encapsulated in SRv6: IPv6 header with SRH, IPv6 header, UDP header :param list sidlist: segment list of outer IPv6 SRH :param int segleft: segments-left field of outer IPv6 SRH Outer IPv6 source address is set to 5678::1 Outer IPv6 destination address is set to sidlist[segleft] IPv6 source addresses is 1234::1 IPv6 destination address is 4321::1 UDP source port and destination port are 1234 """ p = (IPv6(src='5678::1', dst=sidlist[segleft]) / IPv6ExtHdrSegmentRouting( addresses=sidlist, segleft=segleft, nh=41) / IPv6(src='1234::1', dst='4321::1') / UDP(sport=1234, dport=1234)) return p
def create_packet_header_IPv6_SRH_IPv4(self, srcaddr, sidlist, segleft): """Create packet header: IPv4 encapsulated in SRv6: IPv6 header with SRH, IPv4 header, UDP header :param int srcaddr: outer source address :param list sidlist: segment list of outer IPv6 SRH :param int segleft: segments-left field of outer IPv6 SRH Outer IPv6 source address is set to srcaddr Outer IPv6 destination address is set to sidlist[segleft] Inner IPv4 source address is 123.1.1.1 Inner IPv4 destination address is 124.1.1.1 UDP source port and destination port are 1234 """ p = IPv6(src=srcaddr, dst=sidlist[segleft]) / \ IPv6ExtHdrSegmentRouting(addresses=sidlist, segleft=segleft, nh=4) / \ IP(src='123.1.1.1', dst='124.1.1.1') / \ UDP(sport=1234, dport=1234) return p
def main(): """Send, receive and check IPv6 and IPv6ExtHdrSegmentRouting packets.""" args = TrafficScriptArg([ u"tx_src_mac", u"tx_dst_mac", u"rx_src_mac", u"rx_dst_mac", u"src_ip", u"dst_ip", u"dir0_srcsid", u"dir0_dstsid1", u"dir0_dstsid2", u"dir1_srcsid", u"dir1_dstsid1", u"dir1_dstsid2", u"decap", u"dir0_dstsid3", u"dir1_dstsid3", u"static_proxy" ]) tx_txq = TxQueue(args.get_arg(u"tx_if")) tx_rxq = RxQueue(args.get_arg(u"tx_if")) rx_txq = TxQueue(args.get_arg(u"rx_if")) rx_rxq = RxQueue(args.get_arg(u"rx_if")) tx_src_mac = args.get_arg(u"tx_src_mac") tx_dst_mac = args.get_arg(u"tx_dst_mac") rx_src_mac = args.get_arg(u"rx_src_mac") rx_dst_mac = args.get_arg(u"rx_dst_mac") src_ip = args.get_arg(u"src_ip") dst_ip = args.get_arg(u"dst_ip") dir0_srcsid = args.get_arg(u"dir0_srcsid") dir0_dstsid1 = args.get_arg(u"dir0_dstsid1") dir0_dstsid2 = args.get_arg(u"dir0_dstsid2") dir1_srcsid = args.get_arg(u"dir1_srcsid") dir1_dstsid1 = args.get_arg(u"dir1_dstsid1") dir1_dstsid2 = args.get_arg(u"dir1_dstsid2") decap = args.get_arg(u"decap") dir0_dstsid3 = args.get_arg(u"dir0_dstsid3") dir1_dstsid3 = args.get_arg(u"dir1_dstsid3") static_proxy = args.get_arg(u"static_proxy") ip_pkt = IPv6(src=src_ip, dst=dst_ip) sent_packets = list() tx_pkt_send = (Ether(src=tx_src_mac, dst=tx_dst_mac) / ip_pkt) tx_pkt_send /= Raw() size_limit = 78 if len(tx_pkt_send) < size_limit: tx_pkt_send[Raw].load += (b"\0" * (size_limit - len(tx_pkt_send))) sent_packets.append(tx_pkt_send) tx_txq.send(tx_pkt_send) while True: rx_pkt_recv = rx_rxq.recv(2) if rx_pkt_recv is None: raise RuntimeError(f"{IPv6.name} packet Rx timeout") if rx_pkt_recv.haslayer(ICMPv6ND_NS): # read another packet in the queue if the current one is ICMPv6ND_NS continue elif rx_pkt_recv.haslayer(ICMPv6MLReport2): # read another packet in the queue if the current one is # ICMPv6MLReport2 continue elif rx_pkt_recv.haslayer(ICMPv6ND_RA): # read another packet in the queue if the current one is # ICMPv6ND_RA continue # otherwise process the current packet break check_srv6(rx_pkt_recv, rx_src_mac, rx_dst_mac, src_ip, dst_ip, dir0_srcsid, dir0_dstsid1, dir0_dstsid2, dir0_dstsid3, 1) ip_pkt = IPv6(src=dst_ip, dst=src_ip) ip_pkt /= Raw() if len(ip_pkt) < (size_limit - 14): ip_pkt[Raw].load += (b"\0" * (size_limit - 14 - len(ip_pkt))) rx_pkt_send = ( Ether(src=rx_dst_mac, dst=rx_src_mac) / IPv6(src=dir1_srcsid, dst=dir1_dstsid1) / IPv6ExtHdrSegmentRouting( segleft=1 if dir1_dstsid3 == u"None" else 2, lastentry=1 if dir1_dstsid3 == u"None" else 2, addresses=[dir1_dstsid2, dir1_dstsid1] if dir1_dstsid3 == u"None" else [dir1_dstsid3, dir1_dstsid2, dir1_dstsid1]) / ip_pkt) if dir1_dstsid2 != u"None" else ( Ether(src=rx_dst_mac, dst=rx_src_mac) / IPv6(src=dir1_srcsid, dst=dir1_dstsid1) / ip_pkt) rx_txq.send(rx_pkt_send) while True: tx_pkt_recv = tx_rxq.recv(2, ignore=sent_packets) if tx_pkt_recv is None: raise RuntimeError(f"{IPv6.name} packet Rx timeout") if tx_pkt_recv.haslayer(ICMPv6ND_NS): # read another packet in the queue if the current one is ICMPv6ND_NS continue elif tx_pkt_recv.haslayer(ICMPv6MLReport2): # read another packet in the queue if the current one is # ICMPv6MLReport2 continue elif tx_pkt_recv.haslayer(ICMPv6ND_RA): # read another packet in the queue if the current one is # ICMPv6ND_RA continue # otherwise process the current packet break if decap == u"True": check_ip(tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip) else: check_srv6(tx_pkt_recv, tx_dst_mac, tx_src_mac, dst_ip, src_ip, dir1_srcsid, dir1_dstsid1, dir1_dstsid2, dir1_dstsid3, 2, bool(static_proxy == u"True")) sys.exit(0)
def send_twamp_test_response(self, sid_list, sender_block_color, sender_counter, sender_seq_num): """Send a TWAMP response to the sender""" # pylint: disable=too-many-locals # Read the RX counter FW path nopunt_sid_list = utils.rem_punt(sid_list)[:: -1] # no punt and reversed rf_receive_counter = self.hwadapter.read_rx_counter( sender_block_color, nopunt_sid_list) # Reverse path rf_block_number = self.get_prev_color() rf_transmit_counter = self.hwadapter.read_tx_counter( rf_block_number, self.monitored_path['returnsidlist']) ipv6_packet = IPv6() # ipv6_packet.src = 'fcff:5::1' #TODO me li da il controller? # ipv6_packet.dst = 'fcff:4::1' #TODO me li da il controller? ipv6_packet.src = 'fcff:8::1' ipv6_packet.dst = self.monitored_path['returnsidlist'][0] mod_sidlist = utils.set_punt( list(self.monitored_path['returnsidlistrev'])) srv6_header = IPv6ExtHdrSegmentRouting() srv6_header.addresses = mod_sidlist # TODO vedere se funziona con NS variabile srv6_header.segleft = len(mod_sidlist) - 1 # TODO vedere se funziona con NS variabile srv6_header.lastentry = len(mod_sidlist) - 1 ipv6_packet_inside = IPv6() # ipv6_packet_inside.src = 'fcff:5::1' #TODO me li da il controller? # ipv6_packet_inside.dst = 'fcff:2::1' #TODO me li da il controller? ipv6_packet_inside.src = 'fcff:8::1' ipv6_packet_inside.dst = self.monitored_path['returnsidlist'][-1] udp_packet = UDP() udp_packet.dport = self.ss_udp_port udp_packet.sport = self.refl_udp_port # Response sequence number rf_sequence_number = self.monitored_path['revTxSequenceNumber'] # Response control code rf_receiver_control_code = 0 twamp_data = twamp.TWAMPTestResponse( SequenceNumber=rf_sequence_number, TransmitCounter=rf_transmit_counter, BlockNumber=rf_block_number, ReceiveCounter=rf_receive_counter, SenderCounter=sender_counter, SenderBlockNumber=sender_block_color, SenderSequenceNumber=sender_seq_num, ReceverControlCode=rf_receiver_control_code) pkt = (ipv6_packet / srv6_header / ipv6_packet_inside / udp_packet / twamp_data) send(pkt, count=1, verbose=False) # Increse the SequenceNumber self.monitored_path['revTxSequenceNumber'] += 1 print( 'RF - SEND RESP SL {sl} - SN {sn} - TXC {txc} - C {col} - RC {rc}'. format(sl=mod_sidlist, sn=rf_sequence_number, txc=rf_transmit_counter, col=rf_block_number, rc=rf_receive_counter))