3 * SPDX-License-Identifier: BSD-2-Clause
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
32 #include <sys/queue.h>
33 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
48 static struct conf *conf = NULL;
49 static struct auth_group *auth_group = NULL;
50 static struct portal_group *portal_group = NULL;
51 static struct target *target = NULL;
52 static struct lun *lun = NULL;
54 extern void yyerror(const char *);
55 extern void yyrestart(FILE *);
59 %token ALIAS AUTH_GROUP AUTH_TYPE BACKEND BLOCKSIZE CHAP CHAP_MUTUAL
60 %token CLOSING_BRACKET CTL_LUN DEBUG DEVICE_ID DEVICE_TYPE
61 %token DISCOVERY_AUTH_GROUP DISCOVERY_FILTER DSCP FOREIGN
62 %token INITIATOR_NAME INITIATOR_PORTAL ISNS_SERVER ISNS_PERIOD ISNS_TIMEOUT
63 %token LISTEN LISTEN_ISER LUN MAXPROC OFFLOAD OPENING_BRACKET OPTION
64 %token PATH PCP PIDFILE PORT PORTAL_GROUP REDIRECT SEMICOLON SERIAL
65 %token SIZE STR TAG TARGET TIMEOUT
66 %token AF11 AF12 AF13 AF21 AF22 AF23 AF31 AF32 AF33 AF41 AF42 AF43
67 %token BE EF CS0 CS1 CS2 CS3 CS4 CS5 CS6 CS7
82 statements statement SEMICOLON
113 if (expand_number($2, &tmp) != 0) {
114 yyerror("invalid numeric value");
119 conf->conf_debug = tmp;
127 if (expand_number($2, &tmp) != 0) {
128 yyerror("invalid numeric value");
133 conf->conf_timeout = tmp;
141 if (expand_number($2, &tmp) != 0) {
142 yyerror("invalid numeric value");
147 conf->conf_maxproc = tmp;
153 if (conf->conf_pidfile_path != NULL) {
154 log_warnx("pidfile specified more than once");
158 conf->conf_pidfile_path = $2;
162 isns_server: ISNS_SERVER STR
166 error = isns_new(conf, $2);
173 isns_period: ISNS_PERIOD STR
177 if (expand_number($2, &tmp) != 0) {
178 yyerror("invalid numeric value");
183 conf->conf_isns_period = tmp;
187 isns_timeout: ISNS_TIMEOUT STR
191 if (expand_number($2, &tmp) != 0) {
192 yyerror("invalid numeric value");
197 conf->conf_isns_timeout = tmp;
201 auth_group: AUTH_GROUP auth_group_name
202 OPENING_BRACKET auth_group_entries CLOSING_BRACKET
211 * Make it possible to redefine default
212 * auth-group. but only once.
214 if (strcmp($1, "default") == 0 &&
215 conf->conf_default_ag_defined == false) {
216 auth_group = auth_group_find(conf, $1);
217 conf->conf_default_ag_defined = true;
219 auth_group = auth_group_new(conf, $1);
222 if (auth_group == NULL)
229 auth_group_entries auth_group_entry
231 auth_group_entries auth_group_entry SEMICOLON
239 auth_group_chap_mutual
241 auth_group_initiator_name
243 auth_group_initiator_portal
246 auth_group_auth_type: AUTH_TYPE STR
250 error = auth_group_set_type(auth_group, $2);
257 auth_group_chap: CHAP STR STR
259 const struct auth *ca;
261 ca = auth_new_chap(auth_group, $2, $3);
269 auth_group_chap_mutual: CHAP_MUTUAL STR STR STR STR
271 const struct auth *ca;
273 ca = auth_new_chap_mutual(auth_group, $2, $3, $4, $5);
283 auth_group_initiator_name: INITIATOR_NAME STR
285 const struct auth_name *an;
287 an = auth_name_new(auth_group, $2);
294 auth_group_initiator_portal: INITIATOR_PORTAL STR
296 const struct auth_portal *ap;
298 ap = auth_portal_new(auth_group, $2);
305 portal_group: PORTAL_GROUP portal_group_name
306 OPENING_BRACKET portal_group_entries CLOSING_BRACKET
312 portal_group_name: STR
315 * Make it possible to redefine default
316 * portal-group. but only once.
318 if (strcmp($1, "default") == 0 &&
319 conf->conf_default_pg_defined == false) {
320 portal_group = portal_group_find(conf, $1);
321 conf->conf_default_pg_defined = true;
323 portal_group = portal_group_new(conf, $1);
326 if (portal_group == NULL)
331 portal_group_entries:
333 portal_group_entries portal_group_entry
335 portal_group_entries portal_group_entry SEMICOLON
339 portal_group_discovery_auth_group
341 portal_group_discovery_filter
347 portal_group_listen_iser
353 portal_group_redirect
362 portal_group_discovery_auth_group: DISCOVERY_AUTH_GROUP STR
364 if (portal_group->pg_discovery_auth_group != NULL) {
365 log_warnx("discovery-auth-group for portal-group "
366 "\"%s\" specified more than once",
367 portal_group->pg_name);
370 portal_group->pg_discovery_auth_group =
371 auth_group_find(conf, $2);
372 if (portal_group->pg_discovery_auth_group == NULL) {
373 log_warnx("unknown discovery-auth-group \"%s\" "
374 "for portal-group \"%s\"",
375 $2, portal_group->pg_name);
382 portal_group_discovery_filter: DISCOVERY_FILTER STR
386 error = portal_group_set_filter(portal_group, $2);
393 portal_group_foreign: FOREIGN
396 portal_group->pg_foreign = 1;
400 portal_group_listen: LISTEN STR
404 error = portal_group_add_listen(portal_group, $2, false);
411 portal_group_listen_iser: LISTEN_ISER STR
415 error = portal_group_add_listen(portal_group, $2, true);
422 portal_group_offload: OFFLOAD STR
426 error = portal_group_set_offload(portal_group, $2);
433 portal_group_option: OPTION STR STR
437 o = option_new(&portal_group->pg_options, $2, $3);
445 portal_group_redirect: REDIRECT STR
449 error = portal_group_set_redirection(portal_group, $2);
456 portal_group_tag: TAG STR
460 if (expand_number($2, &tmp) != 0) {
461 yyerror("invalid numeric value");
466 portal_group->pg_tag = tmp;
475 if (strcmp($2, "0x") == 0) {
476 tmp = strtol($2 + 2, NULL, 16);
477 } else if (expand_number($2, &tmp) != 0) {
478 yyerror("invalid numeric value");
483 yyerror("invalid dscp value");
487 portal_group->pg_dscp = tmp;
489 | DSCP BE { portal_group->pg_dscp = IPTOS_DSCP_CS0 >> 2 ; }
490 | DSCP EF { portal_group->pg_dscp = IPTOS_DSCP_EF >> 2 ; }
491 | DSCP CS0 { portal_group->pg_dscp = IPTOS_DSCP_CS0 >> 2 ; }
492 | DSCP CS1 { portal_group->pg_dscp = IPTOS_DSCP_CS1 >> 2 ; }
493 | DSCP CS2 { portal_group->pg_dscp = IPTOS_DSCP_CS2 >> 2 ; }
494 | DSCP CS3 { portal_group->pg_dscp = IPTOS_DSCP_CS3 >> 2 ; }
495 | DSCP CS4 { portal_group->pg_dscp = IPTOS_DSCP_CS4 >> 2 ; }
496 | DSCP CS5 { portal_group->pg_dscp = IPTOS_DSCP_CS5 >> 2 ; }
497 | DSCP CS6 { portal_group->pg_dscp = IPTOS_DSCP_CS6 >> 2 ; }
498 | DSCP CS7 { portal_group->pg_dscp = IPTOS_DSCP_CS7 >> 2 ; }
499 | DSCP AF11 { portal_group->pg_dscp = IPTOS_DSCP_AF11 >> 2 ; }
500 | DSCP AF12 { portal_group->pg_dscp = IPTOS_DSCP_AF12 >> 2 ; }
501 | DSCP AF13 { portal_group->pg_dscp = IPTOS_DSCP_AF13 >> 2 ; }
502 | DSCP AF21 { portal_group->pg_dscp = IPTOS_DSCP_AF21 >> 2 ; }
503 | DSCP AF22 { portal_group->pg_dscp = IPTOS_DSCP_AF22 >> 2 ; }
504 | DSCP AF23 { portal_group->pg_dscp = IPTOS_DSCP_AF23 >> 2 ; }
505 | DSCP AF31 { portal_group->pg_dscp = IPTOS_DSCP_AF31 >> 2 ; }
506 | DSCP AF32 { portal_group->pg_dscp = IPTOS_DSCP_AF32 >> 2 ; }
507 | DSCP AF33 { portal_group->pg_dscp = IPTOS_DSCP_AF33 >> 2 ; }
508 | DSCP AF41 { portal_group->pg_dscp = IPTOS_DSCP_AF41 >> 2 ; }
509 | DSCP AF42 { portal_group->pg_dscp = IPTOS_DSCP_AF42 >> 2 ; }
510 | DSCP AF43 { portal_group->pg_dscp = IPTOS_DSCP_AF43 >> 2 ; }
513 portal_group_pcp: PCP STR
517 if (expand_number($2, &tmp) != 0) {
518 yyerror("invalid numeric value");
523 yyerror("invalid pcp value");
528 portal_group->pg_pcp = tmp;
533 OPENING_BRACKET lun_entries CLOSING_BRACKET
541 lun = lun_new(conf, $1);
548 target: TARGET target_name
549 OPENING_BRACKET target_entries CLOSING_BRACKET
557 target = target_new(conf, $1);
566 target_entries target_entry
568 target_entries target_entry SEMICOLON
582 target_initiator_name
584 target_initiator_portal
597 target_alias: ALIAS STR
599 if (target->t_alias != NULL) {
600 log_warnx("alias for target \"%s\" "
601 "specified more than once", target->t_name);
604 target->t_alias = $2;
608 target_auth_group: AUTH_GROUP STR
610 if (target->t_auth_group != NULL) {
611 if (target->t_auth_group->ag_name != NULL)
612 log_warnx("auth-group for target \"%s\" "
613 "specified more than once", target->t_name);
615 log_warnx("cannot use both auth-group and explicit "
616 "authorisations for target \"%s\"",
620 target->t_auth_group = auth_group_find(conf, $2);
621 if (target->t_auth_group == NULL) {
622 log_warnx("unknown auth-group \"%s\" for target "
623 "\"%s\"", $2, target->t_name);
630 target_auth_type: AUTH_TYPE STR
634 if (target->t_auth_group != NULL) {
635 if (target->t_auth_group->ag_name != NULL) {
636 log_warnx("cannot use both auth-group and "
637 "auth-type for target \"%s\"",
642 target->t_auth_group = auth_group_new(conf, NULL);
643 if (target->t_auth_group == NULL) {
647 target->t_auth_group->ag_target = target;
649 error = auth_group_set_type(target->t_auth_group, $2);
656 target_chap: CHAP STR STR
658 const struct auth *ca;
660 if (target->t_auth_group != NULL) {
661 if (target->t_auth_group->ag_name != NULL) {
662 log_warnx("cannot use both auth-group and "
663 "chap for target \"%s\"",
670 target->t_auth_group = auth_group_new(conf, NULL);
671 if (target->t_auth_group == NULL) {
676 target->t_auth_group->ag_target = target;
678 ca = auth_new_chap(target->t_auth_group, $2, $3);
686 target_chap_mutual: CHAP_MUTUAL STR STR STR STR
688 const struct auth *ca;
690 if (target->t_auth_group != NULL) {
691 if (target->t_auth_group->ag_name != NULL) {
692 log_warnx("cannot use both auth-group and "
693 "chap-mutual for target \"%s\"",
702 target->t_auth_group = auth_group_new(conf, NULL);
703 if (target->t_auth_group == NULL) {
710 target->t_auth_group->ag_target = target;
712 ca = auth_new_chap_mutual(target->t_auth_group,
723 target_initiator_name: INITIATOR_NAME STR
725 const struct auth_name *an;
727 if (target->t_auth_group != NULL) {
728 if (target->t_auth_group->ag_name != NULL) {
729 log_warnx("cannot use both auth-group and "
730 "initiator-name for target \"%s\"",
736 target->t_auth_group = auth_group_new(conf, NULL);
737 if (target->t_auth_group == NULL) {
741 target->t_auth_group->ag_target = target;
743 an = auth_name_new(target->t_auth_group, $2);
750 target_initiator_portal: INITIATOR_PORTAL STR
752 const struct auth_portal *ap;
754 if (target->t_auth_group != NULL) {
755 if (target->t_auth_group->ag_name != NULL) {
756 log_warnx("cannot use both auth-group and "
757 "initiator-portal for target \"%s\"",
763 target->t_auth_group = auth_group_new(conf, NULL);
764 if (target->t_auth_group == NULL) {
768 target->t_auth_group->ag_target = target;
770 ap = auth_portal_new(target->t_auth_group, $2);
777 target_portal_group: PORTAL_GROUP STR STR
779 struct portal_group *tpg;
780 struct auth_group *tag;
783 tpg = portal_group_find(conf, $2);
785 log_warnx("unknown portal-group \"%s\" for target "
786 "\"%s\"", $2, target->t_name);
791 tag = auth_group_find(conf, $3);
793 log_warnx("unknown auth-group \"%s\" for target "
794 "\"%s\"", $3, target->t_name);
799 tp = port_new(conf, target, tpg);
801 log_warnx("can't link portal-group \"%s\" to target "
802 "\"%s\"", $2, target->t_name);
806 tp->p_auth_group = tag;
812 struct portal_group *tpg;
815 tpg = portal_group_find(conf, $2);
817 log_warnx("unknown portal-group \"%s\" for target "
818 "\"%s\"", $2, target->t_name);
822 tp = port_new(conf, target, tpg);
824 log_warnx("can't link portal-group \"%s\" to target "
825 "\"%s\"", $2, target->t_name);
833 target_port: PORT STR
837 int ret, i_pp, i_vp = 0;
839 ret = sscanf($2, "ioctl/%d/%d", &i_pp, &i_vp);
841 tp = port_new_ioctl(conf, target, i_pp, i_vp);
843 log_warnx("can't create new ioctl port for "
844 "target \"%s\"", target->t_name);
849 pp = pport_find(conf, $2);
851 log_warnx("unknown port \"%s\" for target \"%s\"",
856 if (!TAILQ_EMPTY(&pp->pp_ports)) {
857 log_warnx("can't link port \"%s\" to target \"%s\", "
858 "port already linked to some target",
863 tp = port_new_pp(conf, target, pp);
865 log_warnx("can't link port \"%s\" to target \"%s\"",
876 target_redirect: REDIRECT STR
880 error = target_set_redirection(target, $2);
887 target_lun: LUN lun_number
888 OPENING_BRACKET lun_entries CLOSING_BRACKET
900 if (expand_number($1, &tmp) != 0) {
901 yyerror("invalid numeric value");
905 if (tmp >= MAX_LUNS) {
906 yyerror("LU number is too big");
911 ret = asprintf(&name, "%s,lun,%ju", target->t_name, tmp);
913 log_err(1, "asprintf");
914 lun = lun_new(conf, name);
918 lun_set_scsiname(lun, name);
919 target->t_luns[tmp] = lun;
923 target_lun_ref: LUN STR STR
927 if (expand_number($2, &tmp) != 0) {
928 yyerror("invalid numeric value");
934 if (tmp >= MAX_LUNS) {
935 yyerror("LU number is too big");
940 lun = lun_find(conf, $3);
945 target->t_luns[tmp] = lun;
951 lun_entries lun_entry
953 lun_entries lun_entry SEMICOLON
976 lun_backend: BACKEND STR
978 if (lun->l_backend != NULL) {
979 log_warnx("backend for lun \"%s\" "
980 "specified more than once",
985 lun_set_backend(lun, $2);
990 lun_blocksize: BLOCKSIZE STR
994 if (expand_number($2, &tmp) != 0) {
995 yyerror("invalid numeric value");
1000 if (lun->l_blocksize != 0) {
1001 log_warnx("blocksize for lun \"%s\" "
1002 "specified more than once",
1006 lun_set_blocksize(lun, tmp);
1010 lun_device_id: DEVICE_ID STR
1012 if (lun->l_device_id != NULL) {
1013 log_warnx("device_id for lun \"%s\" "
1014 "specified more than once",
1019 lun_set_device_id(lun, $2);
1024 lun_device_type: DEVICE_TYPE STR
1028 if (strcasecmp($2, "disk") == 0 ||
1029 strcasecmp($2, "direct") == 0)
1031 else if (strcasecmp($2, "processor") == 0)
1033 else if (strcasecmp($2, "cd") == 0 ||
1034 strcasecmp($2, "cdrom") == 0 ||
1035 strcasecmp($2, "dvd") == 0 ||
1036 strcasecmp($2, "dvdrom") == 0)
1038 else if (expand_number($2, &tmp) != 0 ||
1040 yyerror("invalid numeric value");
1045 lun_set_device_type(lun, tmp);
1049 lun_ctl_lun: CTL_LUN STR
1053 if (expand_number($2, &tmp) != 0) {
1054 yyerror("invalid numeric value");
1059 if (lun->l_ctl_lun >= 0) {
1060 log_warnx("ctl_lun for lun \"%s\" "
1061 "specified more than once",
1065 lun_set_ctl_lun(lun, tmp);
1069 lun_option: OPTION STR STR
1073 o = option_new(&lun->l_options, $2, $3);
1083 if (lun->l_path != NULL) {
1084 log_warnx("path for lun \"%s\" "
1085 "specified more than once",
1090 lun_set_path(lun, $2);
1095 lun_serial: SERIAL STR
1097 if (lun->l_serial != NULL) {
1098 log_warnx("serial for lun \"%s\" "
1099 "specified more than once",
1104 lun_set_serial(lun, $2);
1113 if (expand_number($2, &tmp) != 0) {
1114 yyerror("invalid numeric value");
1119 if (lun->l_size != 0) {
1120 log_warnx("size for lun \"%s\" "
1121 "specified more than once",
1125 lun_set_size(lun, tmp);
1131 yyerror(const char *str)
1134 log_warnx("error in configuration file at line %d near '%s': %s",
1135 lineno, yytext, str);
1139 parse_conf(struct conf *newconf, const char *path)
1144 yyin = fopen(path, "r");
1146 log_warn("unable to open configuration file %s", path);
1154 portal_group = NULL;