Skip to content

Commit 9728038

Browse files
authored
Merge pull request #461 from hreinecke/dhchap-templating.v2
Allow to get and set DHCHAP host key on controller level
2 parents fec443b + e1a7989 commit 9728038

8 files changed

Lines changed: 101 additions & 10 deletions

File tree

doc/config-schema.json.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"description": "NVMe host ID",
2626
"type": "string"
2727
},
28+
"dhchap_key": {
29+
"description": "Host DH-HMAC-CHAP key",
30+
"type": "string"
31+
},
2832
"hostsymname": {
2933
"description": "NVMe host symbolic name",
3034
"type": "string"

libnvme/nvme.i

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ struct nvme_host {
287287
char *hostnqn;
288288
char *hostid;
289289
char *hostsymname;
290-
char *dhchap_key;
290+
%extend {
291+
char *dhchap_key;
292+
}
291293
};
292294

293295
struct nvme_subsystem {
@@ -314,6 +316,7 @@ struct nvme_ctrl {
314316
%immutable subsysnqn;
315317
%immutable traddr;
316318
%immutable trsvcid;
319+
%immutable dhchap_host_key;
317320
%immutable dhchap_key;
318321
%immutable cntrltype;
319322
%immutable dctype;
@@ -332,7 +335,10 @@ struct nvme_ctrl {
332335
char *subsysnqn;
333336
char *traddr;
334337
char *trsvcid;
335-
char *dhchap_key;
338+
%extend {
339+
char *dhchap_host_key:
340+
char *dhchap_key;
341+
}
336342
char *cntrltype;
337343
char *dctype;
338344
bool discovery_ctrl;
@@ -448,6 +454,15 @@ struct nvme_ns {
448454
}
449455
}
450456

457+
%{
458+
const char *nvme_host_dhchap_key_get(struct nvme_host *h) {
459+
return nvme_host_get_dhchap_key(h);
460+
}
461+
void nvme_host_dhchap_key_set(struct nvme_host *h, char *key) {
462+
nvme_host_set_dhchap_key(h, key);
463+
}
464+
%};
465+
451466
%extend subsystem_iter {
452467
struct subsystem_iter *__iter__() {
453468
return $self;
@@ -656,6 +671,12 @@ struct nvme_ns {
656671
const char *nvme_ctrl_state_get(struct nvme_ctrl *c) {
657672
return nvme_ctrl_get_state(c);
658673
}
674+
const char *nvme_ctrl_dhchap_key_get(struct nvme_ctrl *c) {
675+
return nvme_ctrl_get_dhchap_key(c);
676+
}
677+
const char *nvme_ctrl_dhchap_host_key_get(struct nvme_ctrl *c) {
678+
return nvme_ctrl_get_dhchap_host_key(c);
679+
}
659680
%};
660681

661682
%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_key)
623-
nvme_ctrl_set_dhchap_key(c, fc->dhchap_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
@@ -83,6 +83,7 @@ struct nvme_ctrl {
8383
char *traddr;
8484
char *trsvcid;
8585
char *dhchap_key;
86+
char *dhchap_ctrl_key;
8687
char *cntrltype;
8788
char *dctype;
8889
bool discovery_ctrl;

src/nvme/tree.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,12 @@ 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_key(nvme_ctrl_t c)
813+
const char *nvme_ctrl_get_dhchap_host_key(nvme_ctrl_t c)
814814
{
815815
return c->dhchap_key;
816816
}
817817

818-
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
818+
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key)
819819
{
820820
if (c->dhchap_key) {
821821
free(c->dhchap_key);
@@ -825,6 +825,21 @@ void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
825825
c->dhchap_key = strdup(key);
826826
}
827827

828+
const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c)
829+
{
830+
return c->dhchap_ctrl_key;
831+
}
832+
833+
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
834+
{
835+
if (c->dhchap_ctrl_key) {
836+
free(c->dhchap_ctrl_key);
837+
c->dhchap_ctrl_key = NULL;
838+
}
839+
if (key)
840+
c->dhchap_ctrl_key = strdup(key);
841+
}
842+
828843
void nvme_ctrl_set_discovered(nvme_ctrl_t c, bool discovered)
829844
{
830845
c->discovered = discovered;
@@ -898,6 +913,7 @@ void nvme_deconfigure_ctrl(nvme_ctrl_t c)
898913
FREE_CTRL_ATTR(c->serial);
899914
FREE_CTRL_ATTR(c->sqsize);
900915
FREE_CTRL_ATTR(c->dhchap_key);
916+
FREE_CTRL_ATTR(c->dhchap_ctrl_key);
901917
FREE_CTRL_ATTR(c->address);
902918
FREE_CTRL_ATTR(c->dctype);
903919
FREE_CTRL_ATTR(c->cntrltype);
@@ -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,10 +1183,18 @@ 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");
1169-
c->dhchap_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret");
1170-
if (c->dhchap_key && !strcmp(c->dhchap_key, "none")) {
1171-
free(c->dhchap_key);
1172-
c->dhchap_key = NULL;
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;
1194+
c->dhchap_ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret");
1195+
if (c->dhchap_ctrl_key && !strcmp(c->dhchap_ctrl_key, "none")) {
1196+
free(c->dhchap_ctrl_key);
1197+
c->dhchap_ctrl_key = NULL;
11731198
}
11741199
c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype");
11751200
c->dctype = nvme_get_ctrl_attr(c, "dctype");

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)