def ndpPing(mac, vlan, ip): src_mac = get_ether_address(network.BRIDGE_NAME) packet = Ether(src=src_mac, dst=mac) if_name = vlan if '.' in vlan: if_name, vlan_id = vlan.rsplit('.', 1) vlan_id = int(vlan_id) if vlan_id: packet = packet / Dot1Q(vlan=vlan_id) local_ip = None for ip6 in netconf.get_current_ipv6(cidr=False)['ips']: if ip6.startswith('fe80'): local_ip = ip6 else: src_ip = ip6 break else: if local_ip is None: src_ip = 'fe80::66:66' # we need a source IP... else: src_ip = local_ip packet = packet / IPv6(src=src_ip, dst=ip) packet = packet / ICMPv6ND_NS(tgt=ip) sendp(packet, iface=if_name)
def check_dad(ipv6address, interface, hw_addr="9c:b6:d0:fe:41:43", dst_a="ff02::1"): if check_iface(interface): exit(1) HW_MAC = get_if_hwaddr(interface) neigh_sol = IPv6(dst=dst_a)/\ ICMPv6ND_NS(tgt=ipv6address)/\ ICMPv6NDOptSrcLLAddr(lladdr=HW_MAC) ans,u = sr(neigh_sol, timeout=1, iface=interface, multi=True, verbose=False) return True if ans else False
def packets_for_test(request, ptfadapter, duthost, config_facts, tbinfo, ip_and_intf_info): ip_version = request.param src_addr_v4, src_addr_v6, ptf_intf = ip_and_intf_info ptf_intf_index = int(ptf_intf.replace('eth', '')) ptf_intf_mac = ptfadapter.dataplane.get_mac(0, ptf_intf_index) vlans = config_facts['VLAN'] topology = tbinfo['topo']['name'] dut_mac = '' for vlan_details in vlans.values(): if 'dualtor' in topology: dut_mac = vlan_details['mac'].lower() else: dut_mac = duthost.shell( 'sonic-cfggen -d -v \'DEVICE_METADATA.localhost.mac\'' )["stdout_lines"][0].decode("utf-8") break if ip_version == 'v4': tgt_addr = increment_ipv4_addr(src_addr_v4) out_pkt = testutils.simple_arp_packet(eth_dst='ff:ff:ff:ff:ff:ff', eth_src=ptf_intf_mac, ip_snd=src_addr_v4, ip_tgt=tgt_addr, arp_op=1, hw_snd=ptf_intf_mac) exp_pkt = testutils.simple_arp_packet(eth_dst=ptf_intf_mac, eth_src=dut_mac, ip_snd=tgt_addr, ip_tgt=src_addr_v4, arp_op=2, hw_snd=dut_mac, hw_tgt=ptf_intf_mac) elif ip_version == 'v6': tgt_addr = increment_ipv6_addr(src_addr_v6) ll_src_addr = generate_link_local_addr(ptf_intf_mac) multicast_tgt_addr = in6_getnsma(inet_pton(socket.AF_INET6, tgt_addr)) multicast_tgt_mac = in6_getnsmac(multicast_tgt_addr) out_pkt = Ether(src=ptf_intf_mac, dst=multicast_tgt_mac) out_pkt /= IPv6(dst=inet_ntop(socket.AF_INET6, multicast_tgt_addr), src=ll_src_addr) out_pkt /= ICMPv6ND_NS(tgt=tgt_addr) out_pkt /= ICMPv6NDOptSrcLLAddr(lladdr=ptf_intf_mac) exp_pkt = Ether(src=dut_mac, dst=ptf_intf_mac) exp_pkt /= IPv6(dst=ll_src_addr, src=generate_link_local_addr(dut_mac)) exp_pkt /= ICMPv6ND_NA(tgt=tgt_addr, S=1, R=1, O=0) exp_pkt /= ICMPv6NDOptSrcLLAddr(type=2, lladdr=dut_mac) exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.IPv6, 'fl') return ip_version, out_pkt, exp_pkt
def build_arp_ns_pkt(family, smac, src_ip, dst_ip): if family == 'IPv4': eth = Ether(src=smac, dst='ff:ff:ff:ff:ff:ff') pkt = eth/ARP(op='who-has', pdst=dst_ip) elif family == 'IPv6': nsma = in6_getnsma(inet_pton(AF_INET6, dst_ip)) mcast_dst_ip = inet_ntop(AF_INET6, nsma) dmac = in6_getnsmac(nsma) eth = Ether(src=smac,dst=dmac) ipv6 = IPv6(src=src_ip, dst=mcast_dst_ip) ns = ICMPv6ND_NS(tgt=dst_ip) ns_opt = ICMPv6NDOptSrcLLAddr(lladdr=smac) pkt = eth/ipv6/ns/ns_opt return pkt
def ipv6_packets_for_test(ip_and_intf_info, fake_src_mac, fake_src_addr): _, _, src_addr_v6, _, _ = ip_and_intf_info fake_src_mac = fake_src_mac tgt_addr = increment_ipv6_addr(src_addr_v6) multicast_tgt_addr = in6_getnsma(inet_pton(socket.AF_INET6, tgt_addr)) multicast_tgt_mac = in6_getnsmac(multicast_tgt_addr) ns_pkt = Ether(src=fake_src_mac, dst=multicast_tgt_mac) ns_pkt /= IPv6(dst=inet_ntop(socket.AF_INET6, multicast_tgt_addr), src=fake_src_addr) ns_pkt /= ICMPv6ND_NS(tgt=tgt_addr) ns_pkt /= ICMPv6NDOptSrcLLAddr(lladdr=fake_src_mac) logging.info(repr(ns_pkt)) return ns_pkt
def get_mac6(ip, interface): """Returns the according MAC address for the provided IPv6 address. Args: ip (str): IPv6 address used to get MAC address. interface (str): Interface used to send neighbor solicitation. Results: According MAC address as string (11:22:33:44:55:66) or None if no answer has been received. """ ans, unans = srp(Ether(dst=ETHER_BROADCAST) / IPv6(dst=ip) / ICMPv6ND_NS(tgt=ip), timeout=2, iface=interface, inter=0.1, verbose=0) for snd, rcv in ans: return rcv.sprintf(r"%Ether.src%")
def scan_IPv6(self, array): if isinstance(array, list) == False: raise TypeError dictIPv6 = {} for params in array: params = ''.join(params) if len(params) != 39: params = IPv6FormatTrans(params) dstMac = "33:33:FF:" + params[32:34] + ":" + params[ 35:37] + ":" + params[37:39] dstAddr = "FF02::1:FF" + params[32:34] + ":" + params[35:39] iface, srcAddr, _ = conf.route6.route(params) ans, unans = srp(Ether(dst=dstMac) / IPv6(src=srcAddr, dst=dstAddr) / ICMPv6ND_NS(tgt=params), timeout=1, verbose=False, iface=iface) for snd, rcv in ans: macAddr = rcv.sprintf("%Ether.src%") IPv6Addr = rcv.sprintf("%IPv6.src%") dictIPv6[IPv6Addr] = macAddr return dictIPv6