forked from pfsense/FreeBSD-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctld.c
2898 lines (2557 loc) · 67.4 KB
/
ctld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2012 The FreeBSD Foundation
*
* This software was developed by Edward Tomasz Napierala under sponsorship
* from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ctld.h"
#include "isns.h"
static bool timed_out(void);
#ifdef ICL_KERNEL_PROXY
static void pdu_receive_proxy(struct pdu *pdu);
static void pdu_send_proxy(struct pdu *pdu);
#endif /* ICL_KERNEL_PROXY */
static void pdu_fail(const struct connection *conn, const char *reason);
bool proxy_mode = false;
static volatile bool sighup_received = false;
static volatile bool sigterm_received = false;
static volatile bool sigalrm_received = false;
static int nchildren = 0;
static uint16_t last_portal_group_tag = 0xff;
static struct connection_ops conn_ops = {
.timed_out = timed_out,
#ifdef ICL_KERNEL_PROXY
.pdu_receive_proxy = pdu_receive_proxy,
.pdu_send_proxy = pdu_send_proxy,
#endif
.fail = pdu_fail,
};
static void
usage(void)
{
fprintf(stderr, "usage: ctld [-d][-u][-f config-file]\n");
fprintf(stderr, " ctld -t [-u][-f config-file]\n");
exit(1);
}
struct conf *
conf_new(void)
{
struct conf *conf;
conf = calloc(1, sizeof(*conf));
if (conf == NULL)
log_err(1, "calloc");
TAILQ_INIT(&conf->conf_luns);
TAILQ_INIT(&conf->conf_targets);
TAILQ_INIT(&conf->conf_auth_groups);
TAILQ_INIT(&conf->conf_ports);
TAILQ_INIT(&conf->conf_portal_groups);
TAILQ_INIT(&conf->conf_pports);
TAILQ_INIT(&conf->conf_isns);
conf->conf_isns_period = 900;
conf->conf_isns_timeout = 5;
conf->conf_debug = 0;
conf->conf_timeout = 60;
conf->conf_maxproc = 30;
return (conf);
}
void
conf_delete(struct conf *conf)
{
struct lun *lun, *ltmp;
struct target *targ, *tmp;
struct auth_group *ag, *cagtmp;
struct portal_group *pg, *cpgtmp;
struct pport *pp, *pptmp;
struct isns *is, *istmp;
assert(conf->conf_pidfh == NULL);
TAILQ_FOREACH_SAFE(lun, &conf->conf_luns, l_next, ltmp)
lun_delete(lun);
TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
target_delete(targ);
TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
auth_group_delete(ag);
TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
portal_group_delete(pg);
TAILQ_FOREACH_SAFE(pp, &conf->conf_pports, pp_next, pptmp)
pport_delete(pp);
TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
isns_delete(is);
assert(TAILQ_EMPTY(&conf->conf_ports));
free(conf->conf_pidfile_path);
free(conf);
}
static struct auth *
auth_new(struct auth_group *ag)
{
struct auth *auth;
auth = calloc(1, sizeof(*auth));
if (auth == NULL)
log_err(1, "calloc");
auth->a_auth_group = ag;
TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
return (auth);
}
static void
auth_delete(struct auth *auth)
{
TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
free(auth->a_user);
free(auth->a_secret);
free(auth->a_mutual_user);
free(auth->a_mutual_secret);
free(auth);
}
const struct auth *
auth_find(const struct auth_group *ag, const char *user)
{
const struct auth *auth;
TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
if (strcmp(auth->a_user, user) == 0)
return (auth);
}
return (NULL);
}
static void
auth_check_secret_length(struct auth *auth)
{
size_t len;
len = strlen(auth->a_secret);
if (len > 16) {
if (auth->a_auth_group->ag_name != NULL)
log_warnx("secret for user \"%s\", auth-group \"%s\", "
"is too long; it should be at most 16 characters "
"long", auth->a_user, auth->a_auth_group->ag_name);
else
log_warnx("secret for user \"%s\", target \"%s\", "
"is too long; it should be at most 16 characters "
"long", auth->a_user,
auth->a_auth_group->ag_target->t_name);
}
if (len < 12) {
if (auth->a_auth_group->ag_name != NULL)
log_warnx("secret for user \"%s\", auth-group \"%s\", "
"is too short; it should be at least 12 characters "
"long", auth->a_user,
auth->a_auth_group->ag_name);
else
log_warnx("secret for user \"%s\", target \"%s\", "
"is too short; it should be at least 12 characters "
"long", auth->a_user,
auth->a_auth_group->ag_target->t_name);
}
if (auth->a_mutual_secret != NULL) {
len = strlen(auth->a_mutual_secret);
if (len > 16) {
if (auth->a_auth_group->ag_name != NULL)
log_warnx("mutual secret for user \"%s\", "
"auth-group \"%s\", is too long; it should "
"be at most 16 characters long",
auth->a_user, auth->a_auth_group->ag_name);
else
log_warnx("mutual secret for user \"%s\", "
"target \"%s\", is too long; it should "
"be at most 16 characters long",
auth->a_user,
auth->a_auth_group->ag_target->t_name);
}
if (len < 12) {
if (auth->a_auth_group->ag_name != NULL)
log_warnx("mutual secret for user \"%s\", "
"auth-group \"%s\", is too short; it "
"should be at least 12 characters long",
auth->a_user, auth->a_auth_group->ag_name);
else
log_warnx("mutual secret for user \"%s\", "
"target \"%s\", is too short; it should be "
"at least 12 characters long",
auth->a_user,
auth->a_auth_group->ag_target->t_name);
}
}
}
const struct auth *
auth_new_chap(struct auth_group *ag, const char *user,
const char *secret)
{
struct auth *auth;
if (ag->ag_type == AG_TYPE_UNKNOWN)
ag->ag_type = AG_TYPE_CHAP;
if (ag->ag_type != AG_TYPE_CHAP) {
if (ag->ag_name != NULL)
log_warnx("cannot mix \"chap\" authentication with "
"other types for auth-group \"%s\"", ag->ag_name);
else
log_warnx("cannot mix \"chap\" authentication with "
"other types for target \"%s\"",
ag->ag_target->t_name);
return (NULL);
}
auth = auth_new(ag);
auth->a_user = checked_strdup(user);
auth->a_secret = checked_strdup(secret);
auth_check_secret_length(auth);
return (auth);
}
const struct auth *
auth_new_chap_mutual(struct auth_group *ag, const char *user,
const char *secret, const char *user2, const char *secret2)
{
struct auth *auth;
if (ag->ag_type == AG_TYPE_UNKNOWN)
ag->ag_type = AG_TYPE_CHAP_MUTUAL;
if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
if (ag->ag_name != NULL)
log_warnx("cannot mix \"chap-mutual\" authentication "
"with other types for auth-group \"%s\"",
ag->ag_name);
else
log_warnx("cannot mix \"chap-mutual\" authentication "
"with other types for target \"%s\"",
ag->ag_target->t_name);
return (NULL);
}
auth = auth_new(ag);
auth->a_user = checked_strdup(user);
auth->a_secret = checked_strdup(secret);
auth->a_mutual_user = checked_strdup(user2);
auth->a_mutual_secret = checked_strdup(secret2);
auth_check_secret_length(auth);
return (auth);
}
const struct auth_name *
auth_name_new(struct auth_group *ag, const char *name)
{
struct auth_name *an;
an = calloc(1, sizeof(*an));
if (an == NULL)
log_err(1, "calloc");
an->an_auth_group = ag;
an->an_initiator_name = checked_strdup(name);
TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
return (an);
}
static void
auth_name_delete(struct auth_name *an)
{
TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
free(an->an_initiator_name);
free(an);
}
bool
auth_name_defined(const struct auth_group *ag)
{
if (TAILQ_EMPTY(&ag->ag_names))
return (false);
return (true);
}
const struct auth_name *
auth_name_find(const struct auth_group *ag, const char *name)
{
const struct auth_name *auth_name;
TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
if (strcmp(auth_name->an_initiator_name, name) == 0)
return (auth_name);
}
return (NULL);
}
int
auth_name_check(const struct auth_group *ag, const char *initiator_name)
{
if (!auth_name_defined(ag))
return (0);
if (auth_name_find(ag, initiator_name) == NULL)
return (1);
return (0);
}
const struct auth_portal *
auth_portal_new(struct auth_group *ag, const char *portal)
{
struct auth_portal *ap;
char *net, *mask, *str, *tmp;
int len, dm, m;
ap = calloc(1, sizeof(*ap));
if (ap == NULL)
log_err(1, "calloc");
ap->ap_auth_group = ag;
ap->ap_initiator_portal = checked_strdup(portal);
mask = str = checked_strdup(portal);
net = strsep(&mask, "/");
if (net[0] == '[')
net++;
len = strlen(net);
if (len == 0)
goto error;
if (net[len - 1] == ']')
net[len - 1] = 0;
if (strchr(net, ':') != NULL) {
struct sockaddr_in6 *sin6 =
(struct sockaddr_in6 *)&ap->ap_sa;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_family = AF_INET6;
if (inet_pton(AF_INET6, net, &sin6->sin6_addr) <= 0)
goto error;
dm = 128;
} else {
struct sockaddr_in *sin =
(struct sockaddr_in *)&ap->ap_sa;
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
if (inet_pton(AF_INET, net, &sin->sin_addr) <= 0)
goto error;
dm = 32;
}
if (mask != NULL) {
m = strtol(mask, &tmp, 0);
if (m < 0 || m > dm || tmp[0] != 0)
goto error;
} else
m = dm;
ap->ap_mask = m;
free(str);
TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
return (ap);
error:
free(str);
free(ap);
log_warnx("incorrect initiator portal \"%s\"", portal);
return (NULL);
}
static void
auth_portal_delete(struct auth_portal *ap)
{
TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
free(ap->ap_initiator_portal);
free(ap);
}
bool
auth_portal_defined(const struct auth_group *ag)
{
if (TAILQ_EMPTY(&ag->ag_portals))
return (false);
return (true);
}
const struct auth_portal *
auth_portal_find(const struct auth_group *ag, const struct sockaddr_storage *ss)
{
const struct auth_portal *ap;
const uint8_t *a, *b;
int i;
uint8_t bmask;
TAILQ_FOREACH(ap, &ag->ag_portals, ap_next) {
if (ap->ap_sa.ss_family != ss->ss_family)
continue;
if (ss->ss_family == AF_INET) {
a = (const uint8_t *)
&((const struct sockaddr_in *)ss)->sin_addr;
b = (const uint8_t *)
&((const struct sockaddr_in *)&ap->ap_sa)->sin_addr;
} else {
a = (const uint8_t *)
&((const struct sockaddr_in6 *)ss)->sin6_addr;
b = (const uint8_t *)
&((const struct sockaddr_in6 *)&ap->ap_sa)->sin6_addr;
}
for (i = 0; i < ap->ap_mask / 8; i++) {
if (a[i] != b[i])
goto next;
}
if (ap->ap_mask % 8) {
bmask = 0xff << (8 - (ap->ap_mask % 8));
if ((a[i] & bmask) != (b[i] & bmask))
goto next;
}
return (ap);
next:
;
}
return (NULL);
}
int
auth_portal_check(const struct auth_group *ag, const struct sockaddr_storage *sa)
{
if (!auth_portal_defined(ag))
return (0);
if (auth_portal_find(ag, sa) == NULL)
return (1);
return (0);
}
struct auth_group *
auth_group_new(struct conf *conf, const char *name)
{
struct auth_group *ag;
if (name != NULL) {
ag = auth_group_find(conf, name);
if (ag != NULL) {
log_warnx("duplicated auth-group \"%s\"", name);
return (NULL);
}
}
ag = calloc(1, sizeof(*ag));
if (ag == NULL)
log_err(1, "calloc");
if (name != NULL)
ag->ag_name = checked_strdup(name);
TAILQ_INIT(&ag->ag_auths);
TAILQ_INIT(&ag->ag_names);
TAILQ_INIT(&ag->ag_portals);
ag->ag_conf = conf;
TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
return (ag);
}
void
auth_group_delete(struct auth_group *ag)
{
struct auth *auth, *auth_tmp;
struct auth_name *auth_name, *auth_name_tmp;
struct auth_portal *auth_portal, *auth_portal_tmp;
TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
auth_delete(auth);
TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
auth_name_delete(auth_name);
TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
auth_portal_tmp)
auth_portal_delete(auth_portal);
free(ag->ag_name);
free(ag);
}
struct auth_group *
auth_group_find(const struct conf *conf, const char *name)
{
struct auth_group *ag;
assert(name != NULL);
TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
return (ag);
}
return (NULL);
}
int
auth_group_set_type(struct auth_group *ag, const char *str)
{
int type;
if (strcmp(str, "none") == 0) {
type = AG_TYPE_NO_AUTHENTICATION;
} else if (strcmp(str, "deny") == 0) {
type = AG_TYPE_DENY;
} else if (strcmp(str, "chap") == 0) {
type = AG_TYPE_CHAP;
} else if (strcmp(str, "chap-mutual") == 0) {
type = AG_TYPE_CHAP_MUTUAL;
} else {
if (ag->ag_name != NULL)
log_warnx("invalid auth-type \"%s\" for auth-group "
"\"%s\"", str, ag->ag_name);
else
log_warnx("invalid auth-type \"%s\" for target "
"\"%s\"", str, ag->ag_target->t_name);
return (1);
}
if (ag->ag_type != AG_TYPE_UNKNOWN && ag->ag_type != type) {
if (ag->ag_name != NULL) {
log_warnx("cannot set auth-type to \"%s\" for "
"auth-group \"%s\"; already has a different "
"type", str, ag->ag_name);
} else {
log_warnx("cannot set auth-type to \"%s\" for target "
"\"%s\"; already has a different type",
str, ag->ag_target->t_name);
}
return (1);
}
ag->ag_type = type;
return (0);
}
static struct portal *
portal_new(struct portal_group *pg)
{
struct portal *portal;
portal = calloc(1, sizeof(*portal));
if (portal == NULL)
log_err(1, "calloc");
TAILQ_INIT(&portal->p_targets);
portal->p_portal_group = pg;
TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
return (portal);
}
static void
portal_delete(struct portal *portal)
{
TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
if (portal->p_ai != NULL)
freeaddrinfo(portal->p_ai);
free(portal->p_listen);
free(portal);
}
struct portal_group *
portal_group_new(struct conf *conf, const char *name)
{
struct portal_group *pg;
pg = portal_group_find(conf, name);
if (pg != NULL) {
log_warnx("duplicated portal-group \"%s\"", name);
return (NULL);
}
pg = calloc(1, sizeof(*pg));
if (pg == NULL)
log_err(1, "calloc");
pg->pg_name = checked_strdup(name);
TAILQ_INIT(&pg->pg_options);
TAILQ_INIT(&pg->pg_portals);
TAILQ_INIT(&pg->pg_ports);
pg->pg_conf = conf;
pg->pg_tag = 0; /* Assigned later in conf_apply(). */
pg->pg_dscp = -1;
pg->pg_pcp = -1;
TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
return (pg);
}
void
portal_group_delete(struct portal_group *pg)
{
struct portal *portal, *tmp;
struct port *port, *tport;
struct option *o, *otmp;
TAILQ_FOREACH_SAFE(port, &pg->pg_ports, p_pgs, tport)
port_delete(port);
TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
portal_delete(portal);
TAILQ_FOREACH_SAFE(o, &pg->pg_options, o_next, otmp)
option_delete(&pg->pg_options, o);
free(pg->pg_name);
free(pg->pg_offload);
free(pg->pg_redirection);
free(pg);
}
struct portal_group *
portal_group_find(const struct conf *conf, const char *name)
{
struct portal_group *pg;
TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
if (strcmp(pg->pg_name, name) == 0)
return (pg);
}
return (NULL);
}
static int
parse_addr_port(char *arg, const char *def_port, struct addrinfo **ai)
{
struct addrinfo hints;
char *str, *addr, *ch;
const char *port;
int error, colons = 0;
str = arg = strdup(arg);
if (arg[0] == '[') {
/*
* IPv6 address in square brackets, perhaps with port.
*/
arg++;
addr = strsep(&arg, "]");
if (arg == NULL) {
free(str);
return (1);
}
if (arg[0] == '\0') {
port = def_port;
} else if (arg[0] == ':') {
port = arg + 1;
} else {
free(str);
return (1);
}
} else {
/*
* Either IPv6 address without brackets - and without
* a port - or IPv4 address. Just count the colons.
*/
for (ch = arg; *ch != '\0'; ch++) {
if (*ch == ':')
colons++;
}
if (colons > 1) {
addr = arg;
port = def_port;
} else {
addr = strsep(&arg, ":");
if (arg == NULL)
port = def_port;
else
port = arg;
}
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(addr, port, &hints, ai);
free(str);
return ((error != 0) ? 1 : 0);
}
int
portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
{
struct portal *portal;
portal = portal_new(pg);
portal->p_listen = checked_strdup(value);
portal->p_iser = iser;
if (parse_addr_port(portal->p_listen, "3260", &portal->p_ai)) {
log_warnx("invalid listen address %s", portal->p_listen);
portal_delete(portal);
return (1);
}
/*
* XXX: getaddrinfo(3) may return multiple addresses; we should turn
* those into multiple portals.
*/
return (0);
}
int
isns_new(struct conf *conf, const char *addr)
{
struct isns *isns;
isns = calloc(1, sizeof(*isns));
if (isns == NULL)
log_err(1, "calloc");
isns->i_conf = conf;
TAILQ_INSERT_TAIL(&conf->conf_isns, isns, i_next);
isns->i_addr = checked_strdup(addr);
if (parse_addr_port(isns->i_addr, "3205", &isns->i_ai)) {
log_warnx("invalid iSNS address %s", isns->i_addr);
isns_delete(isns);
return (1);
}
/*
* XXX: getaddrinfo(3) may return multiple addresses; we should turn
* those into multiple servers.
*/
return (0);
}
void
isns_delete(struct isns *isns)
{
TAILQ_REMOVE(&isns->i_conf->conf_isns, isns, i_next);
free(isns->i_addr);
if (isns->i_ai != NULL)
freeaddrinfo(isns->i_ai);
free(isns);
}
static int
isns_do_connect(struct isns *isns)
{
int s;
s = socket(isns->i_ai->ai_family, isns->i_ai->ai_socktype,
isns->i_ai->ai_protocol);
if (s < 0) {
log_warn("socket(2) failed for %s", isns->i_addr);
return (-1);
}
if (connect(s, isns->i_ai->ai_addr, isns->i_ai->ai_addrlen)) {
log_warn("connect(2) failed for %s", isns->i_addr);
close(s);
return (-1);
}
return(s);
}
static int
isns_do_register(struct isns *isns, int s, const char *hostname)
{
struct conf *conf = isns->i_conf;
struct target *target;
struct portal *portal;
struct portal_group *pg;
struct port *port;
struct isns_req *req;
int res = 0;
uint32_t error;
req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
isns_req_add_delim(req);
isns_req_add_str(req, 1, hostname);
isns_req_add_32(req, 2, 2); /* 2 -- iSCSI */
isns_req_add_32(req, 6, conf->conf_isns_period);
TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
if (pg->pg_unassigned)
continue;
TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
isns_req_add_addr(req, 16, portal->p_ai);
isns_req_add_port(req, 17, portal->p_ai);
}
}
TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
isns_req_add_str(req, 32, target->t_name);
isns_req_add_32(req, 33, 1); /* 1 -- Target*/
if (target->t_alias != NULL)
isns_req_add_str(req, 34, target->t_alias);
TAILQ_FOREACH(port, &target->t_ports, p_ts) {
if ((pg = port->p_portal_group) == NULL)
continue;
isns_req_add_32(req, 51, pg->pg_tag);
TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
isns_req_add_addr(req, 49, portal->p_ai);
isns_req_add_port(req, 50, portal->p_ai);
}
}
}
res = isns_req_send(s, req);
if (res < 0) {
log_warn("send(2) failed for %s", isns->i_addr);
goto quit;
}
res = isns_req_receive(s, req);
if (res < 0) {
log_warn("receive(2) failed for %s", isns->i_addr);
goto quit;
}
error = isns_req_get_status(req);
if (error != 0) {
log_warnx("iSNS register error %d for %s", error, isns->i_addr);
res = -1;
}
quit:
isns_req_free(req);
return (res);
}
static int
isns_do_check(struct isns *isns, int s, const char *hostname)
{
struct conf *conf = isns->i_conf;
struct isns_req *req;
int res = 0;
uint32_t error;
req = isns_req_create(ISNS_FUNC_DEVATTRQRY, ISNS_FLAG_CLIENT);
isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
isns_req_add_str(req, 1, hostname);
isns_req_add_delim(req);
isns_req_add(req, 2, 0, NULL);
res = isns_req_send(s, req);
if (res < 0) {
log_warn("send(2) failed for %s", isns->i_addr);
goto quit;
}
res = isns_req_receive(s, req);
if (res < 0) {
log_warn("receive(2) failed for %s", isns->i_addr);
goto quit;
}
error = isns_req_get_status(req);
if (error != 0) {
log_warnx("iSNS check error %d for %s", error, isns->i_addr);
res = -1;
}
quit:
isns_req_free(req);
return (res);
}
static int
isns_do_deregister(struct isns *isns, int s, const char *hostname)
{
struct conf *conf = isns->i_conf;
struct isns_req *req;
int res = 0;
uint32_t error;
req = isns_req_create(ISNS_FUNC_DEVDEREG, ISNS_FLAG_CLIENT);
isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
isns_req_add_delim(req);
isns_req_add_str(req, 1, hostname);
res = isns_req_send(s, req);
if (res < 0) {
log_warn("send(2) failed for %s", isns->i_addr);
goto quit;
}
res = isns_req_receive(s, req);
if (res < 0) {
log_warn("receive(2) failed for %s", isns->i_addr);
goto quit;
}
error = isns_req_get_status(req);
if (error != 0) {
log_warnx("iSNS deregister error %d for %s", error, isns->i_addr);
res = -1;
}
quit:
isns_req_free(req);
return (res);
}
void
isns_register(struct isns *isns, struct isns *oldisns)
{
struct conf *conf = isns->i_conf;
int error, s;
char hostname[256];
if (TAILQ_EMPTY(&conf->conf_targets) ||
TAILQ_EMPTY(&conf->conf_portal_groups))
return;
set_timeout(conf->conf_isns_timeout, false);
s = isns_do_connect(isns);
if (s < 0) {
set_timeout(0, false);
return;
}
error = gethostname(hostname, sizeof(hostname));
if (error != 0)
log_err(1, "gethostname");
if (oldisns == NULL || TAILQ_EMPTY(&oldisns->i_conf->conf_targets))
oldisns = isns;
isns_do_deregister(oldisns, s, hostname);
isns_do_register(isns, s, hostname);
close(s);
set_timeout(0, false);
}
void
isns_check(struct isns *isns)
{
struct conf *conf = isns->i_conf;
int error, s, res;
char hostname[256];
if (TAILQ_EMPTY(&conf->conf_targets) ||
TAILQ_EMPTY(&conf->conf_portal_groups))
return;
set_timeout(conf->conf_isns_timeout, false);
s = isns_do_connect(isns);
if (s < 0) {
set_timeout(0, false);
return;
}
error = gethostname(hostname, sizeof(hostname));
if (error != 0)
log_err(1, "gethostname");
res = isns_do_check(isns, s, hostname);
if (res < 0) {
isns_do_deregister(isns, s, hostname);
isns_do_register(isns, s, hostname);
}
close(s);
set_timeout(0, false);
}
void
isns_deregister(struct isns *isns)
{
struct conf *conf = isns->i_conf;
int error, s;
char hostname[256];
if (TAILQ_EMPTY(&conf->conf_targets) ||
TAILQ_EMPTY(&conf->conf_portal_groups))
return;
set_timeout(conf->conf_isns_timeout, false);