Skip to content

Commit 6614a55

Browse files
committed
Parse dhchap_host_key on controller level
The json config schema declares an 'dhchap_key' element representing the dhchap host key on the controller level, but the implementation was missing. Add the missing parsing elements and ensure to pick the correct one (either host or controller setting) when creating the nvme connect string. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 055639d commit 6614a55

7 files changed

Lines changed: 73 additions & 2 deletions

File tree

libnvme/nvme.i

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ struct nvme_ctrl {
316316
%immutable subsysnqn;
317317
%immutable traddr;
318318
%immutable trsvcid;
319+
%immutable dhchap_host_key;
319320
%immutable dhchap_key;
320321
%immutable cntrltype;
321322
%immutable dctype;
@@ -335,6 +336,7 @@ struct nvme_ctrl {
335336
char *traddr;
336337
char *trsvcid;
337338
%extend {
339+
char *dhchap_host_key:
338340
char *dhchap_key;
339341
}
340342
char *cntrltype;
@@ -672,6 +674,9 @@ struct nvme_ns {
672674
const char *nvme_ctrl_dhchap_key_get(struct nvme_ctrl *c) {
673675
return nvme_ctrl_get_dhchap_key(c);
674676
}
677+
const char *nvme_ctrl_dhchap_host_key_get(struct nvme_ctrl *c) {
678+
return nvme_ctrl_get_dhchap_host_key(c);
679+
}
675680
%};
676681

677682
%extend nvme_ns {

src/libnvme.map

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# SPDX-License-Identifier: LGPL-2.1-or-later
22

3+
LIBNVME_1_2 {
4+
global:
5+
nvme_ctrl_get_dhchap_host_key;
6+
nvme_ctrl_set_dhchap_host_key;
7+
};
8+
39
LIBNVME_1_1 {
410
global:
511
nvme_get_version;

src/nvme/fabrics.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
465465
hostnqn = nvme_host_get_hostnqn(h);
466466
hostid = nvme_host_get_hostid(h);
467467
hostkey = nvme_host_get_dhchap_key(h);
468+
if (!hostkey)
469+
hostkey = nvme_ctrl_get_dhchap_host_key(c);
468470
ctrlkey = nvme_ctrl_get_dhchap_key(c);
469471
if (add_argument(argstr, "transport", transport) ||
470472
add_argument(argstr, "traddr",
@@ -613,14 +615,20 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
613615
nvme_ctrl_get_trsvcid(c),
614616
NULL);
615617
if (fc) {
618+
const char *key;
619+
616620
cfg = merge_config(c, nvme_ctrl_get_config(fc));
617621
/*
618622
* An authentication key might already been set
619623
* in @cfg, so ensure to update @c with the correct
620624
* controller key.
621625
*/
622-
if (fc->dhchap_ctrl_key)
623-
nvme_ctrl_set_dhchap_key(c, fc->dhchap_ctrl_key);
626+
key = nvme_ctrl_get_dhchap_host_key(fc);
627+
if (key)
628+
nvme_ctrl_set_dhchap_host_key(c, key);
629+
key = nvme_ctrl_get_dhchap_key(fc);
630+
if (key)
631+
nvme_ctrl_set_dhchap_key(c, key);
624632
}
625633

626634
}

src/nvme/json.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)
9595
if (!c)
9696
return;
9797
json_update_attributes(c, port_obj);
98+
attr_obj = json_object_object_get(port_obj, "dhchap_key");
99+
if (attr_obj)
100+
nvme_ctrl_set_dhchap_host_key(c, json_object_get_string(attr_obj));
98101
attr_obj = json_object_object_get(port_obj, "dhchap_ctrl_key");
99102
if (attr_obj)
100103
nvme_ctrl_set_dhchap_key(c, json_object_get_string(attr_obj));
@@ -222,6 +225,10 @@ static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c)
222225
if (value)
223226
json_object_object_add(port_obj, "trsvcid",
224227
json_object_new_string(value));
228+
value = nvme_ctrl_get_dhchap_host_key(c);
229+
if (value)
230+
json_object_object_add(port_obj, "dhchap_key",
231+
json_object_new_string(value));
225232
value = nvme_ctrl_get_dhchap_key(c);
226233
if (value)
227234
json_object_object_add(port_obj, "dhchap_ctrl_key",
@@ -365,6 +372,10 @@ static void json_dump_ctrl(struct json_object *ctrl_array, nvme_ctrl_t c)
365372
if (value)
366373
json_object_object_add(ctrl_obj, "trsvcid",
367374
json_object_new_string(value));
375+
value = nvme_ctrl_get_dhchap_host_key(c);
376+
if (value)
377+
json_object_object_add(ctrl_obj, "dhchap_key",
378+
json_object_new_string(value));
368379
value = nvme_ctrl_get_dhchap_key(c);
369380
if (value)
370381
json_object_object_add(ctrl_obj, "dhchap_ctrl_key",

src/nvme/private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct nvme_ctrl {
8282
char *subsysnqn;
8383
char *traddr;
8484
char *trsvcid;
85+
char *dhchap_key;
8586
char *dhchap_ctrl_key;
8687
char *cntrltype;
8788
char *dctype;

src/nvme/tree.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,21 @@ struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c)
810810
return &c->cfg;
811811
}
812812

813+
const char *nvme_ctrl_get_dhchap_host_key(nvme_ctrl_t c)
814+
{
815+
return c->dhchap_key;
816+
}
817+
818+
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key)
819+
{
820+
if (c->dhchap_key) {
821+
free(c->dhchap_key);
822+
c->dhchap_key = NULL;
823+
}
824+
if (key)
825+
c->dhchap_key = strdup(key);
826+
}
827+
813828
const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c)
814829
{
815830
return c->dhchap_ctrl_key;
@@ -897,6 +912,7 @@ void nvme_deconfigure_ctrl(nvme_ctrl_t c)
897912
FREE_CTRL_ATTR(c->queue_count);
898913
FREE_CTRL_ATTR(c->serial);
899914
FREE_CTRL_ATTR(c->sqsize);
915+
FREE_CTRL_ATTR(c->dhchap_key);
900916
FREE_CTRL_ATTR(c->dhchap_ctrl_key);
901917
FREE_CTRL_ATTR(c->address);
902918
FREE_CTRL_ATTR(c->dctype);
@@ -1146,6 +1162,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
11461162
const char *name)
11471163
{
11481164
DIR *d;
1165+
char *host_key;
11491166

11501167
d = opendir(path);
11511168
if (!d) {
@@ -1166,6 +1183,14 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
11661183
c->queue_count = nvme_get_ctrl_attr(c, "queue_count");
11671184
c->serial = nvme_get_ctrl_attr(c, "serial");
11681185
c->sqsize = nvme_get_ctrl_attr(c, "sqsize");
1186+
host_key = nvme_get_ctrl_attr(c, "dhchap_secret");
1187+
if (host_key && (!strcmp(c->s->h->dhchap_key, host_key) ||
1188+
!strcmp("none", host_key))) {
1189+
free(host_key);
1190+
host_key = NULL;
1191+
}
1192+
if (host_key)
1193+
c->dhchap_key = host_key;
11691194
c->dhchap_ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret");
11701195
if (c->dhchap_ctrl_key && !strcmp(c->dhchap_ctrl_key, "none")) {
11711196
free(c->dhchap_ctrl_key);

src/nvme/tree.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,21 @@ const char *nvme_ctrl_get_host_traddr(nvme_ctrl_t c);
875875
*/
876876
const char *nvme_ctrl_get_host_iface(nvme_ctrl_t c);
877877

878+
/**
879+
* nvme_ctrl_get_dhchap_host_key() - Return host key
880+
* @c: Controller to be checked
881+
*
882+
* Return: DH-HMAC-CHAP host key or NULL if not set
883+
*/
884+
const char *nvme_ctrl_get_dhchap_host_key(nvme_ctrl_t c);
885+
886+
/**
887+
* nvme_ctrl_set_dhchap_host_key() - Set host key
888+
* @c: Host for which the key should be set
889+
* @key: DH-HMAC-CHAP Key to set or NULL to clear existing key
890+
*/
891+
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key);
892+
878893
/**
879894
* nvme_ctrl_get_dhchap_key() - Return controller key
880895
* @c: Controller for which the key should be set

0 commit comments

Comments
 (0)