Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Lsof.8
Original file line number Diff line number Diff line change
Expand Up @@ -1082,9 +1082,19 @@ is limited to IPv6 with
IPv6 host names and addresses may not be specified if network file selection
is limited to IPv4 with
.BR \-i " 4."
When an open IPv4 network file's address is mapped in an IPv6 address,
the open file's type will be IPv6, not IPv4, and its display will be
selected by '6', not '4'.
When an open IPv4 network file's address is represented as an
IPv4-mapped IPv6 address, the open file's type will be IPv6, not IPv4.
When a numeric address is displayed, the IPv4-mapped IPv6 address is
shown in IPv4 form, but that does not make it an IPv4 file: `-i 6'
selects it and `-i 4' does not, including when a service/port is also
specified. Address specifications remain subject to the IPv4/IPv6
rules above. An IPv4-mapped IPv6 address written in bracketed IPv6
form is an IPv6 address for this rule, even without an explicit `6'
qualifier: it selects an IPv6 file, while `-i 4' rejects it.
A dotted IPv4 address selects an IPv4 file, not an IPv6 file whose
mapped address is displayed in IPv4 form. A dual-stack IPv6
socket that accepts an IPv4 connection is such an IPv6 file; the
separate IPv4 client socket is an IPv4 file.
.IP
At least one address component \-
.BR 4,
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \
tests/case-21-exit-Q-status.bash \
tests/case-22-empty-process-name.bash \
tests/case-23-multi-address-hostname.bash \
tests/case-24-dual-stack-socket.bash \
tests/case-30-json-output.bash \
tests/case-31-jsonl-output.bash
TESTS = $(DIALECT_NEUTRAL_TESTS)
Expand Down
17 changes: 14 additions & 3 deletions docs/manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,20 @@ OPTIONS
file selection is limited to IPv6 with -i 6. IPv6 host names
and addresses may not be specified if network file selection
is limited to IPv4 with -i 4. When an open IPv4 network
file's address is mapped in an IPv6 address, the open file's
type will be IPv6, not IPv4, and its display will be selected
by '6', not '4'.
file's address is represented as an IPv4-mapped IPv6 address,
the open file's type will be IPv6, not IPv4. When a numeric
address is displayed, the IPv4-mapped IPv6 address is shown in
IPv4 form, but that does not make it an IPv4 file: `-i 6'
selects it and `-i 4' does not, including when a service/port
is also specified. Address specifications remain subject to
the IPv4/IPv6 rules above. An IPv4-mapped IPv6 address written
in bracketed IPv6 form is an IPv6 address for this rule, even
without an explicit `6' qualifier: it selects an IPv6 file,
while `-i 4' rejects it. A dotted IPv4 address selects an IPv4
file, not an IPv6 file whose mapped address is displayed in
IPv4 form. A dual-stack IPv6 socket that accepts an IPv4
connection is such an IPv6 file; the separate IPv4 client
socket is an IPv4 file.

At least one address component - 4, 6, protocol, hostname,
hostaddr, or service - must be supplied. The `@' character,
Expand Down
63 changes: 42 additions & 21 deletions lib/dialects/darwin/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,30 @@ static char copyright[] = "@(#) Copyright 2005 Apple Computer, Inc. and Purdue "

#include "common.h"

#if defined(HASIPv6)
/*
* IPv6_2_IPv4() -- macro to define the address of an IPv4 address contained
* in an IPv6 address
* normalize_v4compat() - convert IPv4-compatible `::a.b.c.d' addresses to
* the standard IPv4-mapped `::ffff:a.b.c.d' form. Darwin reports accepted
* IPv4 connections in the former form, while lsof's common selection and
* display logic uses the latter.
*/
static void normalize_v4compat(unsigned char *a) {
unsigned char v4[4];
int i;

#define IPv6_2_IPv4(v6) (((uint8_t *)((struct in6_addr *)v6)->s6_addr) + 12)
for (i = 0; i < 12; i++) {
if (a[i])
return;
}
/* Exclude :: and ::1; both have IPv6 meanings. */
if (!(a[12] || a[13] || a[14] || a[15] > 1))
return;
(void)memcpy(v4, a + 12, sizeof(v4));
(void)memset(a, 0, 10);
a[10] = a[11] = 0xff;
(void)memcpy(a + 12, v4, sizeof(v4));
}
#endif /* defined(HASIPv6) */

/*
* process_socket() -- process socket file
Expand All @@ -55,6 +73,9 @@ static void process_socket_common(struct lsof_context *ctx,
unsigned char *fa = (unsigned char *)NULL;
int fam, fp, lp, unl;
unsigned char *la = (unsigned char *)NULL;
#if defined(HASIPv6)
struct in6_addr la6, fa6;
#endif /* defined(HASIPv6) */

/*
* Enter basic socket values.
Expand Down Expand Up @@ -197,10 +218,10 @@ static void process_socket_common(struct lsof_context *ctx,
} else {

/*
* Enter IPv6 address information
* Enter IPv6 address information. Leave IPv4-mapped addresses
* in their IPv6 form: Lsof.8 specifies that such a file is IPv6
* and is selected by '6', not '4'.
*/
int v4mapped = 0;

if (si->psi.soi_kind == SOCKINFO_TCP) {

/*
Expand All @@ -212,9 +233,6 @@ static void process_socket_common(struct lsof_context *ctx,
fa = (unsigned char *)&si->psi.soi_proto.pri_tcp.tcpsi_ini
.insi_faddr.ina_6;
fp = (int)ntohs(si->psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport);
if ((si->psi.soi_proto.pri_tcp.tcpsi_ini.insi_vflag &
INI_IPV4) != 0)
v4mapped = 1;
} else {

/*
Expand All @@ -226,24 +244,27 @@ static void process_socket_common(struct lsof_context *ctx,
fa =
(unsigned char *)&si->psi.soi_proto.pri_in.insi_faddr.ina_6;
fp = (int)ntohs(si->psi.soi_proto.pri_in.insi_fport);
if ((si->psi.soi_proto.pri_in.insi_vflag & INI_IPV4) != 0)
v4mapped = 1;
}
if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)fa) && !fp) {
fa = (unsigned char *)NULL;
fp = 0;
}
if (v4mapped) {

/*
* Adjust IPv4 addresses mapped in IPv6 addresses.
*/
fam = AF_INET;
if (la)
la = (unsigned char *)IPv6_2_IPv4(la);
if (fa)
fa = (unsigned char *)IPv6_2_IPv4(fa);
#if defined(HASIPv6)
/*
* Normalize Darwin's IPv4-compatible form to the standard
* IPv4-mapped form before selection and display.
*/
if (la) {
la6 = *(struct in6_addr *)la;
normalize_v4compat((unsigned char *)&la6);
la = (unsigned char *)&la6;
}
if (fa) {
fa6 = *(struct in6_addr *)fa;
normalize_v4compat((unsigned char *)&fa6);
fa = (unsigned char *)&fa6;
}
#endif /* defined(HASIPv6) */
}
/*
* Enter local and remote addresses by address family.
Expand Down
21 changes: 0 additions & 21 deletions lib/dialects/freebsd/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ static char copyright[] =

#if defined(HASIPv6)

/*
* IPv6_2_IPv4() -- macro to define the address of an IPv4 address contained
* in an IPv6 address
*/

# define IPv6_2_IPv4(v6) (((uint8_t *)((struct in6_addr *)v6)->s6_addr) + 12)

# if defined(HAS_NO_6PORT)
/*
* If the in_pcb structure no longer has the KAME accommodations of
Expand Down Expand Up @@ -531,20 +524,6 @@ void process_socket(struct lsof_context *ctx, struct kinfo_file *kf,
}
}

if ((fam == AF_INET6) &&
((la && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)la)) ||
((fa && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)fa))))) {

/*
* Adjust for IPv4 addresses mapped in IPv6 addresses.
*/
if (la)
la = (unsigned char *)IPv6_2_IPv4(la);
if (fa)
fa = (unsigned char *)IPv6_2_IPv4(fa);
fam = AF_INET;
}

/*
* Enter local and remote addresses by address family.
*/
Expand Down
10 changes: 1 addition & 9 deletions lib/dialects/linux/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3941,15 +3941,7 @@ void process_proc_sock(struct lsof_context *ctx, /* context */
la = (unsigned char *)&tp6->laddr;
else
la = (unsigned char *)NULL;
if ((fa && IN6_IS_ADDR_V4MAPPED(&tp6->faddr)) ||
(la && IN6_IS_ADDR_V4MAPPED(&tp6->laddr))) {
af = AF_INET;
if (fa)
fa += 12;
if (la)
la += 12;
}
ent_inaddr(ctx, la, tp6->lport, fa, tp6->fport, af);
ent_inaddr(ctx, la, tp6->lport, fa, tp6->fport, AF_INET6);
Lf->lts.type = tp6->proto;
Lf->lts.state.i = tp6->state;

Expand Down
26 changes: 0 additions & 26 deletions lib/dialects/netbsd/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ static char copyright[] =

#include "common.h"

#if defined(HASIPv6)

/*
* IPv6_2_IPv4() -- macro to define the address of an IPv4 address contained
* in an IPv6 address
*/

# define IPv6_2_IPv4(v6) (((uint8_t *)((struct in6_addr *)v6)->s6_addr) + 12)
#endif /* defined(HASIPv6) */

/*
* process_socket() - process socket
*/
Expand Down Expand Up @@ -293,22 +283,6 @@ void process_socket(struct lsof_context *ctx, /* context */
#endif /* defined(HASIPv6) && defined(HASINRIAIPv6) */
}

#if defined(HASIPv6)
if ((fam == AF_INET6) &&
((la && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)la)) ||
((fa && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)fa))))) {

/*
* Adjust for IPv4 addresses mapped in IPv6 addresses.
*/
if (la)
la = (unsigned char *)IPv6_2_IPv4(la);
if (fa)
fa = (unsigned char *)IPv6_2_IPv4(fa);
fam = AF_INET;
}
#endif /* defined(HASIPv6) */

/*
* Enter local and remote addresses by address family.
*/
Expand Down
5 changes: 5 additions & 0 deletions lib/dialects/openbsd/dsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ void process_socket(struct lsof_context *ctx, struct kinfo_file *file) {
/* Fill internet address info */
lport = ntohs(file->inp_lport);
fport = ntohs(file->inp_fport);
/*
* Keep the socket family for selection. ent_inaddr() applies the
* IPv4 display conversion for IPv4-mapped addresses only after the
* file has been matched as an IPv6 file.
*/
if (file->inp_fport) {
ent_inaddr(ctx, (unsigned char *)file->inp_laddru, lport,
(unsigned char *)file->inp_faddru, fport,
Expand Down
22 changes: 22 additions & 0 deletions lib/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,28 @@ void ent_inaddr(struct lsof_context *ctx,
if (m)
Lf->sf |= SELNA;
}
#if defined(HASIPv6)
/*
* Selection above uses the socket's original address family. For
* display, preserve lsof's historical IPv4 notation for IPv4-mapped
* IPv6 addresses. This conversion must happen after selection: the
* file remains IPv6 for -i4/-i6 even though its address looks IPv4.
*/
if (af == AF_INET6) {
int i;
for (i = 0; i < 2; i++) {
if (Lf->li[i].af == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&Lf->li[i].ia.a6)) {
struct in_addr a4;
(void)memcpy(&a4,
((unsigned char *)&Lf->li[i].ia.a6) + 12,
sizeof(a4));
Lf->li[i].ia.a4 = a4;
Lf->li[i].af = AF_INET;
}
}
}
#endif /* defined(HASIPv6) */
}

/*
Expand Down
14 changes: 1 addition & 13 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,19 +1390,7 @@ int enter_network_address(struct lsof_context *ctx, /* context */
}
if (!ae)
goto unacc_address;
if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)&n.a[0])) {
if (ft == 6) {
(void)fprintf(stderr,
"%s: IPv4 addresses are prohibited: -i ", Pn);
safestrprt(na, stderr, 1);
goto nwad_exit;
}
for (i = 0; i < 4; i++) {
n.a[i] = n.a[i + 12];
}
n.af = AF_INET;
} else
n.af = AF_INET6;
n.af = AF_INET6;
wa = cp + 1;
#else /* !defined(HASIPv6) */
(void)fprintf(stderr, "%s: unsupported IPv6 address in: -i ", Pn);
Expand Down
Loading
Loading