1#!/usr/local/bin/python2.7 2# send Duplicate Address Detection neighbor solicitation 3# expect an neighbor advertisement answer and check it 4 5print "send duplicate address detection neighbor solicitation packet" 6 7import os 8from addr import * 9from scapy.all import * 10 11# link-local solicited-node multicast address 12def nsma(a): 13 n = inet_pton(socket.AF_INET6, a) 14 return inet_ntop(socket.AF_INET6, in6_getnsma(n)) 15 16# ethernet multicast address of multicast address 17def nsmac(a): 18 n = inet_pton(socket.AF_INET6, a) 19 return in6_getnsmac(n) 20 21# ethernet multicast address of solicited-node multicast address 22def nsmamac(a): 23 return nsmac(nsma(a)) 24 25# link-local address 26def lla(m): 27 return "fe80::"+in6_mactoifaceid(m) 28 29ip=IPv6(src="::", dst=nsma(REMOTE_ADDR6))/ICMPv6ND_NS(tgt=REMOTE_ADDR6) 30eth=Ether(src=LOCAL_MAC, dst=nsmamac(REMOTE_ADDR6))/ip 31 32if os.fork() == 0: 33 time.sleep(1) 34 sendp(eth, iface=LOCAL_IF) 35 os._exit(0) 36 37ans=sniff(iface=LOCAL_IF, timeout=3, filter= 38 "ip6 and src net fe80::/64 and dst ff02::1 and icmp6") 39for a in ans: 40 if a and a.type == ETH_P_IPV6 and \ 41 ipv6nh[a.payload.nh] == 'ICMPv6' and \ 42 icmp6types[a.payload.payload.type] == 'Neighbor Advertisement': 43 tgt=a.payload.payload.tgt 44 print "target=%s" % (tgt) 45 if tgt == REMOTE_ADDR6: 46 exit(0) 47 print "TARGET!=%s" % (REMOTE_ADDR6) 48 exit(1) 49print "NO NEIGHBOR ADVERTISEMENT" 50exit(2) 51