From 652fb944050f237df8986232e4846202e1dc40ff Mon Sep 17 00:00:00 2001 From: david Date: Tue, 27 Mar 2012 21:48:53 +0000 Subject: [PATCH] Make -e assign IPv6 scope ids for everything, not only route_dst. You could use "-e en0" to automatically add scope ids to your IPv6 addresses, so you didn't need the write "fe80::1234%en0". But this only happened for the route_dst calculation, and could lead to later failures in sendmsg when the address didn't have a scope id. --- TargetGroup.cc | 21 ++++++++++++++++++++- tcpip.cc | 24 +++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/TargetGroup.cc b/TargetGroup.cc index 8b34a5942..5dfe058e1 100644 --- a/TargetGroup.cc +++ b/TargetGroup.cc @@ -100,6 +100,7 @@ #include "NmapOps.h" #include "nmap_error.h" #include "global_structures.h" +#include "libnetutil/netutil.h" extern NmapOps o; @@ -385,6 +386,21 @@ int TargetGroup::skip_range(_octet_nums octet) { return hosts_skipped; } +/* Get the sin6_scope_id member of a sockaddr_in6, based on a device name. This + is used to assign scope to all addresses that otherwise lack a scope id when + the -e option is used. */ +static int get_scope_id(const char *devname) { + struct interface_info *ii; + + if (devname == NULL || devname[0] == '\0') + return 0; + ii = getInterfaceByName(devname, AF_INET6); + if (ii != NULL) + return ii->ifindex; + else + return 0; +} + /* Grab the next host from this expression (if any) and updates its internal state to reflect that the IP was given out. Returns 0 and fills in ss if successful. ss must point to a pre-allocated @@ -467,7 +483,10 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { sin6->sin6_len = *sslen; #endif /* SIN_LEN */ memcpy(sin6->sin6_addr.s6_addr, ip6.sin6_addr.s6_addr, 16); - sin6->sin6_scope_id = ip6.sin6_scope_id; + if (ip6.sin6_scope_id == 0) + sin6->sin6_scope_id = get_scope_id(o.device); + else + sin6->sin6_scope_id = ip6.sin6_scope_id; #else fatal("IPV6 not supported on this platform"); #endif // HAVE_IPV6 diff --git a/tcpip.cc b/tcpip.cc index 29fa2834c..ba094568d 100644 --- a/tcpip.cc +++ b/tcpip.cc @@ -2013,34 +2013,16 @@ pcap_if_t *getpcapinterfaces() { -/* Assign the sin6_scope_id member of a sockaddr_in6, based on a device name. - This is used to assign scope to all addresses with the -e option is used. */ -static void assign_scope_id(struct sockaddr_in6 *sin6, const char *devname) { - struct interface_info *ii; - - if (devname == NULL || devname[0] == '\0') - return; - ii = getInterfaceByName(devname, sin6->sin6_family); - if (ii != NULL) - sin6->sin6_scope_id = ii->ifindex; -} - int nmap_route_dst(const struct sockaddr_storage *dst, struct route_nfo *rnfo) { - struct sockaddr_storage dst_mod, spoofss; + struct sockaddr_storage spoofss; size_t spoofsslen; - /* Make a copy that we may modify (only to possibly add a sin6_scope_id). */ - dst_mod = *dst; - - if (dst_mod.ss_family == AF_INET6) - assign_scope_id((struct sockaddr_in6 *) &dst_mod, o.device); - if (o.spoofsource) { o.SourceSockAddr(&spoofss, &spoofsslen); - return route_dst(&dst_mod, rnfo, o.device, &spoofss); + return route_dst(dst, rnfo, o.device, &spoofss); } else { - return route_dst(&dst_mod, rnfo, o.device, NULL); + return route_dst(dst, rnfo, o.device, NULL); } }