Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Documentation/nvme-exclusion-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ the entries of that one list.

An entry that can never match is marked '# invalid: never matches'. An
entry is invalid when it uses an unknown key, a token without '=', an
empty value, or a hostname where the transport requires a numeric
address. Add --verbose to log the reason for each one.
empty value, or a hostname instead of a numeric address. Add --verbose
to log the reason for each one.

This command is read-only and does not require root.

Expand Down
87 changes: 58 additions & 29 deletions libnvme/src/nvme/tid.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ static bool fc_wwn_is_valid(const char *in)
return (size_t)len == strlen(in);
}

static int sanitize_ip_addr(char **addr)
{
char *canon;
int rc;

if (!*addr)
return 0;

rc = canon_ip(*addr, &canon);
if (rc)
return rc;

free(*addr);
*addr = canon;

return 0;
}

static int sanitize_fc_addr(char *addr)
{
if (!addr)
return 0;

fc_wwn_normalize(addr);

return fc_wwn_is_valid(addr) ? 0 : -EINVAL;
}

/*
* An address of a partial TID with no transport, e.g. an exclusion entry:
* accept a numeric IP or an FC WWN pair. A hostname is rejected, since it
* never matches a connect TID.
*/
static int sanitize_any_addr(char **addr)
{
int rc;

rc = sanitize_ip_addr(addr);
if (rc != -EINVAL)
return rc;

return sanitize_fc_addr(*addr);
}

/*
* Sanitize the addressing fields once the transport is known. IP
* transports: canonicalize a numeric traddr/host_traddr via canon_ip(); a
Expand All @@ -133,45 +177,30 @@ static bool fc_wwn_is_valid(const char *in)
*/
static int tid_sanitize_addr(struct libnvmf_tid *t)
{
char *canon;
int rc;

if (!t->transport)
return 0;

if (streq(t->transport, "fc")) {
if (t->traddr)
fc_wwn_normalize(t->traddr);
if (t->host_traddr)
fc_wwn_normalize(t->host_traddr);

if (t->traddr && !fc_wwn_is_valid(t->traddr))
return -EINVAL;
if (t->host_traddr && !fc_wwn_is_valid(t->host_traddr))
return -EINVAL;
return 0;
}

if (!streq(t->transport, "tcp") && !streq(t->transport, "rdma"))
return 0;

if (t->traddr) {
rc = canon_ip(t->traddr, &canon);
if (!t->transport) {
rc = sanitize_any_addr(&t->traddr);
if (rc)
return rc;
free(t->traddr);
t->traddr = canon;
return sanitize_any_addr(&t->host_traddr);
}

if (t->host_traddr) {
rc = canon_ip(t->host_traddr, &canon);
if (streq(t->transport, "fc")) {
rc = sanitize_fc_addr(t->traddr);
if (rc)
return rc;
free(t->host_traddr);
t->host_traddr = canon;
return sanitize_fc_addr(t->host_traddr);
}

return 0;
if (!streq(t->transport, "tcp") && !streq(t->transport, "rdma"))
return 0;

rc = sanitize_ip_addr(&t->traddr);
if (rc)
return rc;

return sanitize_ip_addr(&t->host_traddr);
}

__shr_public int libnvmf_tid_from_fields(
Expand Down
5 changes: 3 additions & 2 deletions libnvme/src/nvme/tid.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ struct libnvme_global_ctx;
* Convenience constructor. NULL fields are stored as NULL. For an IP
* transport (tcp, rdma) a numeric traddr/host_traddr is canonicalized; a
* hostname is rejected -- resolving it is the caller's job, not libnvme's.
* Without a transport, an address must be a numeric IP or an FC WWN pair.
*
* Return: 0 on success (*@out is the allocated TID); -EINVAL if @out is
* NULL or traddr/host_traddr is not numeric on an IP transport; -ENOMEM on
* allocation failure.
* NULL or traddr/host_traddr is not numeric on an IP transport or without
* a transport; -ENOMEM on allocation failure.
*/
int libnvmf_tid_from_fields(const char *transport, const char *traddr,
const char *trsvcid, const char *subsysnqn,
Expand Down
1 change: 1 addition & 0 deletions libnvme/tests/exclusion.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ static bool test_malformed_entry(struct libnvme_global_ctx *ctx)
"nqn=",
"subsysnqn=nqn.example",
"transport=tcp;traddr=dc.example.com",
"traddr=dc.example.com",
};
struct libnvmf_tid *tid;
char path[512];
Expand Down
29 changes: 29 additions & 0 deletions libnvme/tests/test-tid.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,35 @@ static bool test_tid_sanitize(void)
pass &= p;
libnvmf_tid_free(t);

/*
* Without a transport (a partial TID, e.g. an exclusion entry), an
* address must still be a numeric IP or an FC WWN pair: a hostname
* would never match a connect TID.
*/
p = libnvmf_tid_parse(ctx, "traddr=array.example.com", &t) == -EINVAL &&
t == NULL;
CHECK(p, "no transport: hostname traddr rejected");
pass &= p;
libnvmf_tid_free(t);

p = libnvmf_tid_parse(ctx, "host-traddr=myhost", &t) == -EINVAL &&
t == NULL;
CHECK(p, "no transport: hostname host_traddr rejected");
pass &= p;
libnvmf_tid_free(t);

p = libnvmf_tid_parse(ctx, "traddr=::ffff:10.0.0.5", &t) == 0 &&
streq(libnvmf_tid_get_traddr(t), "10.0.0.5");
CHECK(p, "no transport: numeric traddr canonicalized");
pass &= p;
libnvmf_tid_free(t);

p = libnvmf_tid_parse(ctx, "traddr=nn-0x1,pn-0x2", &t) == 0 &&
streq(libnvmf_tid_get_traddr(t), "nn-0x1:pn-0x2");
CHECK(p, "no transport: fc traddr normalized");
pass &= p;
libnvmf_tid_free(t);

/* loop has no addressing shape at all: truly untouched. */
libnvmf_tid_from_fields("loop", "anything goes", NULL, "nqn.t",
NULL, NULL, NULL, NULL, &t);
Expand Down