3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/queue.h>
35 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
50 static struct conf *conf = NULL;
51 static struct auth_group *auth_group = NULL;
52 static struct portal_group *portal_group = NULL;
53 static struct target *target = NULL;
54 static struct lun *lun = NULL;
56 extern void yyerror(const char *);
57 extern void yyrestart(FILE *);
61 %token ALIAS AUTH_GROUP AUTH_TYPE BACKEND BLOCKSIZE CHAP CHAP_MUTUAL
62 %token CLOSING_BRACKET CTL_LUN DEBUG DEVICE_ID DEVICE_TYPE
63 %token DISCOVERY_AUTH_GROUP DISCOVERY_FILTER DSCP FOREIGN
64 %token INITIATOR_NAME INITIATOR_PORTAL ISNS_SERVER ISNS_PERIOD ISNS_TIMEOUT
65 %token LISTEN LISTEN_ISER LUN MAXPROC OFFLOAD OPENING_BRACKET OPTION
66 %token PATH PCP PIDFILE PORT PORTAL_GROUP REDIRECT SEMICOLON SERIAL
67 %token SIZE STR TAG TARGET TIMEOUT
68 %token AF11 AF12 AF13 AF21 AF22 AF23 AF31 AF32 AF33 AF41 AF42 AF43
69 %token BE EF CS0 CS1 CS2 CS3 CS4 CS5 CS6 CS7
84 statements statement SEMICOLON
115 if (expand_number($2, &tmp) != 0) {
116 yyerror("invalid numeric value");
121 conf->conf_debug = tmp;
129 if (expand_number($2, &tmp) != 0) {
130 yyerror("invalid numeric value");
135 conf->conf_timeout = tmp;
143 if (expand_number($2, &tmp) != 0) {
144 yyerror("invalid numeric value");
149 conf->conf_maxproc = tmp;
155 if (conf->conf_pidfile_path != NULL) {
156 log_warnx("pidfile specified more than once");
160 conf->conf_pidfile_path = $2;
164 isns_server: ISNS_SERVER STR
168 error = isns_new(conf, $2);
175 isns_period: ISNS_PERIOD STR
179 if (expand_number($2, &tmp) != 0) {
180 yyerror("invalid numeric value");
185 conf->conf_isns_period = tmp;
189 isns_timeout: ISNS_TIMEOUT STR
193 if (expand_number($2, &tmp) != 0) {
194 yyerror("invalid numeric value");
199 conf->conf_isns_timeout = tmp;
203 auth_group: AUTH_GROUP auth_group_name
204 OPENING_BRACKET auth_group_entries CLOSING_BRACKET
213 * Make it possible to redefine default
214 * auth-group. but only once.
216 if (strcmp($1, "default") == 0 &&
217 conf->conf_default_ag_defined == false) {
218 auth_group = auth_group_find(conf, $1);
219 conf->conf_default_ag_defined = true;
221 auth_group = auth_group_new(conf, $1);
224 if (auth_group == NULL)
231 auth_group_entries auth_group_entry
233 auth_group_entries auth_group_entry SEMICOLON
241 auth_group_chap_mutual
243 auth_group_initiator_name
245 auth_group_initiator_portal
248 auth_group_auth_type: AUTH_TYPE STR
252 error = auth_group_set_type(auth_group, $2);
259 auth_group_chap: CHAP STR STR
261 const struct auth *ca;
263 ca = auth_new_chap(auth_group, $2, $3);
271 auth_group_chap_mutual: CHAP_MUTUAL STR STR STR STR
273 const struct auth *ca;
275 ca = auth_new_chap_mutual(auth_group, $2, $3, $4, $5);
285 auth_group_initiator_name: INITIATOR_NAME STR
287 const struct auth_name *an;
289 an = auth_name_new(auth_group, $2);
296 auth_group_initiator_portal: INITIATOR_PORTAL STR
298 const struct auth_portal *ap;
300 ap = auth_portal_new(auth_group, $2);
307 portal_group: PORTAL_GROUP portal_group_name
308 OPENING_BRACKET portal_group_entries CLOSING_BRACKET
314 portal_group_name: STR
317 * Make it possible to redefine default
318 * portal-group. but only once.
320 if (strcmp($1, "default") == 0 &&
321 conf->conf_default_pg_defined == false) {
322 portal_group = portal_group_find(conf, $1);
323 conf->conf_default_pg_defined = true;
325 portal_group = portal_group_new(conf, $1);
328 if (portal_group == NULL)
333 portal_group_entries:
335 portal_group_entries portal_group_entry
337 portal_group_entries portal_group_entry SEMICOLON
341 portal_group_discovery_auth_group
343 portal_group_discovery_filter
349 portal_group_listen_iser
355 portal_group_redirect
364 portal_group_discovery_auth_group: DISCOVERY_AUTH_GROUP STR
366 if (portal_group->pg_discovery_auth_group != NULL) {
367 log_warnx("discovery-auth-group for portal-group "
368 "\"%s\" specified more than once",
369 portal_group->pg_name);
372 portal_group->pg_discovery_auth_group =
373 auth_group_find(conf, $2);
374 if (portal_group->pg_discovery_auth_group == NULL) {
375 log_warnx("unknown discovery-auth-group \"%s\" "
376 "for portal-group \"%s\"",
377 $2, portal_group->pg_name);
384 portal_group_discovery_filter: DISCOVERY_FILTER STR
388 error = portal_group_set_filter(portal_group, $2);
395 portal_group_foreign: FOREIGN
398 portal_group->pg_foreign = 1;
402 portal_group_listen: LISTEN STR
406 error = portal_group_add_listen(portal_group, $2, false);
413 portal_group_listen_iser: LISTEN_ISER STR
417 error = portal_group_add_listen(portal_group, $2, true);
424 portal_group_offload: OFFLOAD STR
428 error = portal_group_set_offload(portal_group, $2);
435 portal_group_option: OPTION STR STR
439 o = option_new(&portal_group->pg_options, $2, $3);
447 portal_group_redirect: REDIRECT STR
451 error = portal_group_set_redirection(portal_group, $2);
458 portal_group_tag: TAG STR
462 if (expand_number($2, &tmp) != 0) {
463 yyerror("invalid numeric value");
468 portal_group->pg_tag = tmp;
477 if (strcmp($2, "0x") == 0) {
478 tmp = strtol($2 + 2, NULL, 16);
479 } else if (expand_number($2, &tmp) != 0) {
480 yyerror("invalid numeric value");
485 yyerror("invalid dscp value");
489 portal_group->pg_dscp = tmp;
491 | DSCP BE { portal_group->pg_dscp = IPTOS_DSCP_CS0 >> 2 ; }
492 | DSCP EF { portal_group->pg_dscp = IPTOS_DSCP_EF >> 2 ; }
493 | DSCP CS0 { portal_group->pg_dscp = IPTOS_DSCP_CS0 >> 2 ; }
494 | DSCP CS1 { portal_group->pg_dscp = IPTOS_DSCP_CS1 >> 2 ; }
495 | DSCP CS2 { portal_group->pg_dscp = IPTOS_DSCP_CS2 >> 2 ; }
496 | DSCP CS3 { portal_group->pg_dscp = IPTOS_DSCP_CS3 >> 2 ; }
497 | DSCP CS4 { portal_group->pg_dscp = IPTOS_DSCP_CS4 >> 2 ; }
498 | DSCP CS5 { portal_group->pg_dscp = IPTOS_DSCP_CS5 >> 2 ; }
499 | DSCP CS6 { portal_group->pg_dscp = IPTOS_DSCP_CS6 >> 2 ; }
500 | DSCP CS7 { portal_group->pg_dscp = IPTOS_DSCP_CS7 >> 2 ; }
501 | DSCP AF11 { portal_group->pg_dscp = IPTOS_DSCP_AF11 >> 2 ; }
502 | DSCP AF12 { portal_group->pg_dscp = IPTOS_DSCP_AF12 >> 2 ; }
503 | DSCP AF13 { portal_group->pg_dscp = IPTOS_DSCP_AF13 >> 2 ; }
504 | DSCP AF21 { portal_group->pg_dscp = IPTOS_DSCP_AF21 >> 2 ; }
505 | DSCP AF22 { portal_group->pg_dscp = IPTOS_DSCP_AF22 >> 2 ; }
506 | DSCP AF23 { portal_group->pg_dscp = IPTOS_DSCP_AF23 >> 2 ; }
507 | DSCP AF31 { portal_group->pg_dscp = IPTOS_DSCP_AF31 >> 2 ; }
508 | DSCP AF32 { portal_group->pg_dscp = IPTOS_DSCP_AF32 >> 2 ; }
509 | DSCP AF33 { portal_group->pg_dscp = IPTOS_DSCP_AF33 >> 2 ; }
510 | DSCP AF41 { portal_group->pg_dscp = IPTOS_DSCP_AF41 >> 2 ; }
511 | DSCP AF42 { portal_group->pg_dscp = IPTOS_DSCP_AF42 >> 2 ; }
512 | DSCP AF43 { portal_group->pg_dscp = IPTOS_DSCP_AF43 >> 2 ; }
515 portal_group_pcp: PCP STR
519 if (expand_number($2, &tmp) != 0) {
520 yyerror("invalid numeric value");
525 yyerror("invalid pcp value");
530 portal_group->pg_pcp = tmp;
535 OPENING_BRACKET lun_entries CLOSING_BRACKET
543 lun = lun_new(conf, $1);
550 target: TARGET target_name
551 OPENING_BRACKET target_entries CLOSING_BRACKET
559 target = target_new(conf, $1);
568 target_entries target_entry
570 target_entries target_entry SEMICOLON
584 target_initiator_name
586 target_initiator_portal
599 target_alias: ALIAS STR
601 if (target->t_alias != NULL) {
602 log_warnx("alias for target \"%s\" "
603 "specified more than once", target->t_name);
606 target->t_alias = $2;
610 target_auth_group: AUTH_GROUP STR
612 if (target->t_auth_group != NULL) {
613 if (target->t_auth_group->ag_name != NULL)
614 log_warnx("auth-group for target \"%s\" "
615 "specified more than once", target->t_name);
617 log_warnx("cannot use both auth-group and explicit "
618 "authorisations for target \"%s\"",
622 target->t_auth_group = auth_group_find(conf, $2);
623 if (target->t_auth_group == NULL) {
624 log_warnx("unknown auth-group \"%s\" for target "
625 "\"%s\"", $2, target->t_name);
632 target_auth_type: AUTH_TYPE STR
636 if (target->t_auth_group != NULL) {
637 if (target->t_auth_group->ag_name != NULL) {
638 log_warnx("cannot use both auth-group and "
639 "auth-type for target \"%s\"",
644 target->t_auth_group = auth_group_new(conf, NULL);
645 if (target->t_auth_group == NULL) {
649 target->t_auth_group->ag_target = target;
651 error = auth_group_set_type(target->t_auth_group, $2);
658 target_chap: CHAP STR STR
660 const struct auth *ca;
662 if (target->t_auth_group != NULL) {
663 if (target->t_auth_group->ag_name != NULL) {
664 log_warnx("cannot use both auth-group and "
665 "chap for target \"%s\"",
672 target->t_auth_group = auth_group_new(conf, NULL);
673 if (target->t_auth_group == NULL) {
678 target->t_auth_group->ag_target = target;
680 ca = auth_new_chap(target->t_auth_group, $2, $3);
688 target_chap_mutual: CHAP_MUTUAL STR STR STR STR
690 const struct auth *ca;
692 if (target->t_auth_group != NULL) {
693 if (target->t_auth_group->ag_name != NULL) {
694 log_warnx("cannot use both auth-group and "
695 "chap-mutual for target \"%s\"",
704 target->t_auth_group = auth_group_new(conf, NULL);
705 if (target->t_auth_group == NULL) {
712 target->t_auth_group->ag_target = target;
714 ca = auth_new_chap_mutual(target->t_auth_group,
725 target_initiator_name: INITIATOR_NAME STR
727 const struct auth_name *an;
729 if (target->t_auth_group != NULL) {
730 if (target->t_auth_group->ag_name != NULL) {
731 log_warnx("cannot use both auth-group and "
732 "initiator-name for target \"%s\"",
738 target->t_auth_group = auth_group_new(conf, NULL);
739 if (target->t_auth_group == NULL) {
743 target->t_auth_group->ag_target = target;
745 an = auth_name_new(target->t_auth_group, $2);
752 target_initiator_portal: INITIATOR_PORTAL STR
754 const struct auth_portal *ap;
756 if (target->t_auth_group != NULL) {
757 if (target->t_auth_group->ag_name != NULL) {
758 log_warnx("cannot use both auth-group and "
759 "initiator-portal for target \"%s\"",
765 target->t_auth_group = auth_group_new(conf, NULL);
766 if (target->t_auth_group == NULL) {
770 target->t_auth_group->ag_target = target;
772 ap = auth_portal_new(target->t_auth_group, $2);
779 target_portal_group: PORTAL_GROUP STR STR
781 struct portal_group *tpg;
782 struct auth_group *tag;
785 tpg = portal_group_find(conf, $2);
787 log_warnx("unknown portal-group \"%s\" for target "
788 "\"%s\"", $2, target->t_name);
793 tag = auth_group_find(conf, $3);
795 log_warnx("unknown auth-group \"%s\" for target "
796 "\"%s\"", $3, target->t_name);
801 tp = port_new(conf, target, tpg);
803 log_warnx("can't link portal-group \"%s\" to target "
804 "\"%s\"", $2, target->t_name);
808 tp->p_auth_group = tag;
814 struct portal_group *tpg;
817 tpg = portal_group_find(conf, $2);
819 log_warnx("unknown portal-group \"%s\" for target "
820 "\"%s\"", $2, target->t_name);
824 tp = port_new(conf, target, tpg);
826 log_warnx("can't link portal-group \"%s\" to target "
827 "\"%s\"", $2, target->t_name);
835 target_port: PORT STR
839 int ret, i_pp, i_vp = 0;
841 ret = sscanf($2, "ioctl/%d/%d", &i_pp, &i_vp);
843 tp = port_new_ioctl(conf, target, i_pp, i_vp);
845 log_warnx("can't create new ioctl port for "
846 "target \"%s\"", target->t_name);
851 pp = pport_find(conf, $2);
853 log_warnx("unknown port \"%s\" for target \"%s\"",
858 if (!TAILQ_EMPTY(&pp->pp_ports)) {
859 log_warnx("can't link port \"%s\" to target \"%s\", "
860 "port already linked to some target",
865 tp = port_new_pp(conf, target, pp);
867 log_warnx("can't link port \"%s\" to target \"%s\"",
878 target_redirect: REDIRECT STR
882 error = target_set_redirection(target, $2);
889 target_lun: LUN lun_number
890 OPENING_BRACKET lun_entries CLOSING_BRACKET
902 if (expand_number($1, &tmp) != 0) {
903 yyerror("invalid numeric value");
907 if (tmp >= MAX_LUNS) {
908 yyerror("LU number is too big");
913 ret = asprintf(&name, "%s,lun,%ju", target->t_name, tmp);
915 log_err(1, "asprintf");
916 lun = lun_new(conf, name);
920 lun_set_scsiname(lun, name);
921 target->t_luns[tmp] = lun;
925 target_lun_ref: LUN STR STR
929 if (expand_number($2, &tmp) != 0) {
930 yyerror("invalid numeric value");
936 if (tmp >= MAX_LUNS) {
937 yyerror("LU number is too big");
942 lun = lun_find(conf, $3);
947 target->t_luns[tmp] = lun;
953 lun_entries lun_entry
955 lun_entries lun_entry SEMICOLON
978 lun_backend: BACKEND STR
980 if (lun->l_backend != NULL) {
981 log_warnx("backend for lun \"%s\" "
982 "specified more than once",
987 lun_set_backend(lun, $2);
992 lun_blocksize: BLOCKSIZE STR
996 if (expand_number($2, &tmp) != 0) {
997 yyerror("invalid numeric value");
1002 if (lun->l_blocksize != 0) {
1003 log_warnx("blocksize for lun \"%s\" "
1004 "specified more than once",
1008 lun_set_blocksize(lun, tmp);
1012 lun_device_id: DEVICE_ID STR
1014 if (lun->l_device_id != NULL) {
1015 log_warnx("device_id for lun \"%s\" "
1016 "specified more than once",
1021 lun_set_device_id(lun, $2);
1026 lun_device_type: DEVICE_TYPE STR
1030 if (strcasecmp($2, "disk") == 0 ||
1031 strcasecmp($2, "direct") == 0)
1033 else if (strcasecmp($2, "processor") == 0)
1035 else if (strcasecmp($2, "cd") == 0 ||
1036 strcasecmp($2, "cdrom") == 0 ||
1037 strcasecmp($2, "dvd") == 0 ||
1038 strcasecmp($2, "dvdrom") == 0)
1040 else if (expand_number($2, &tmp) != 0 ||
1042 yyerror("invalid numeric value");
1047 lun_set_device_type(lun, tmp);
1051 lun_ctl_lun: CTL_LUN STR
1055 if (expand_number($2, &tmp) != 0) {
1056 yyerror("invalid numeric value");
1061 if (lun->l_ctl_lun >= 0) {
1062 log_warnx("ctl_lun for lun \"%s\" "
1063 "specified more than once",
1067 lun_set_ctl_lun(lun, tmp);
1071 lun_option: OPTION STR STR
1075 o = option_new(&lun->l_options, $2, $3);
1085 if (lun->l_path != NULL) {
1086 log_warnx("path for lun \"%s\" "
1087 "specified more than once",
1092 lun_set_path(lun, $2);
1097 lun_serial: SERIAL STR
1099 if (lun->l_serial != NULL) {
1100 log_warnx("serial for lun \"%s\" "
1101 "specified more than once",
1106 lun_set_serial(lun, $2);
1115 if (expand_number($2, &tmp) != 0) {
1116 yyerror("invalid numeric value");
1121 if (lun->l_size != 0) {
1122 log_warnx("size for lun \"%s\" "
1123 "specified more than once",
1127 lun_set_size(lun, tmp);
1133 yyerror(const char *str)
1136 log_warnx("error in configuration file at line %d near '%s': %s",
1137 lineno, yytext, str);
1141 parse_conf(struct conf *newconf, const char *path)
1146 yyin = fopen(path, "r");
1148 log_warn("unable to open configuration file %s", path);
1156 portal_group = NULL;