discovery.c (d899be7d43d8df9cb485af5705e2724165a461c7) discovery.c (92847ee1f364687be3ec9aed6e1837e0b3c349f5)
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 148 unchanged lines hidden (view full) ---

157 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
158
159 return (response);
160}
161
162static void
163discovery_add_target(struct keys *response_keys, const struct target *targ)
164{
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 148 unchanged lines hidden (view full) ---

157 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
158
159 return (response);
160}
161
162static void
163discovery_add_target(struct keys *response_keys, const struct target *targ)
164{
165 struct port *port;
165 struct portal *portal;
166 char *buf;
167 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
168 struct addrinfo *ai;
169 int ret;
170
171 keys_add(response_keys, "TargetName", targ->t_name);
166 struct portal *portal;
167 char *buf;
168 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
169 struct addrinfo *ai;
170 int ret;
171
172 keys_add(response_keys, "TargetName", targ->t_name);
172 TAILQ_FOREACH(portal, &targ->t_portal_group->pg_portals, p_next) {
173 TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
174 if (port->p_portal_group == NULL)
175 continue;
176 TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
173 ai = portal->p_ai;
174 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
175 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
176 NI_NUMERICHOST | NI_NUMERICSERV);
177 if (ret != 0) {
178 log_warnx("getnameinfo: %s", gai_strerror(ret));
179 continue;
180 }
181 switch (ai->ai_addr->sa_family) {
182 case AF_INET:
183 if (strcmp(hbuf, "0.0.0.0") == 0)
184 continue;
185 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
177 ai = portal->p_ai;
178 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
179 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
180 NI_NUMERICHOST | NI_NUMERICSERV);
181 if (ret != 0) {
182 log_warnx("getnameinfo: %s", gai_strerror(ret));
183 continue;
184 }
185 switch (ai->ai_addr->sa_family) {
186 case AF_INET:
187 if (strcmp(hbuf, "0.0.0.0") == 0)
188 continue;
189 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
186 targ->t_portal_group->pg_tag);
190 port->p_portal_group->pg_tag);
187 break;
188 case AF_INET6:
189 if (strcmp(hbuf, "::") == 0)
190 continue;
191 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
191 break;
192 case AF_INET6:
193 if (strcmp(hbuf, "::") == 0)
194 continue;
195 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
192 targ->t_portal_group->pg_tag);
196 port->p_portal_group->pg_tag);
193 break;
194 default:
195 continue;
196 }
197 if (ret <= 0)
198 log_err(1, "asprintf");
199 keys_add(response_keys, "TargetAddress", buf);
200 free(buf);
197 break;
198 default:
199 continue;
200 }
201 if (ret <= 0)
202 log_err(1, "asprintf");
203 keys_add(response_keys, "TargetAddress", buf);
204 free(buf);
205 }
201 }
202}
203
204static bool
205discovery_target_filtered_out(const struct connection *conn,
206 }
207}
208
209static bool
210discovery_target_filtered_out(const struct connection *conn,
206 const struct target *targ)
211 const struct port *port)
207{
208 const struct auth_group *ag;
209 const struct portal_group *pg;
212{
213 const struct auth_group *ag;
214 const struct portal_group *pg;
215 const struct target *targ;
210 const struct auth *auth;
211 int error;
212
216 const struct auth *auth;
217 int error;
218
213 ag = targ->t_auth_group;
219 targ = port->p_target;
220 ag = port->p_auth_group;
221 if (ag == NULL)
222 ag = targ->t_auth_group;
214 pg = conn->conn_portal->p_portal_group;
215
216 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
217
218 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
219 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
220 log_debugx("initiator does not match initiator portals "
221 "allowed for target \"%s\"; skipping", targ->t_name);

--- 38 unchanged lines hidden (view full) ---

260 return (false);
261}
262
263void
264discovery(struct connection *conn)
265{
266 struct pdu *request, *response;
267 struct keys *request_keys, *response_keys;
223 pg = conn->conn_portal->p_portal_group;
224
225 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
226
227 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
228 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
229 log_debugx("initiator does not match initiator portals "
230 "allowed for target \"%s\"; skipping", targ->t_name);

--- 38 unchanged lines hidden (view full) ---

269 return (false);
270}
271
272void
273discovery(struct connection *conn)
274{
275 struct pdu *request, *response;
276 struct keys *request_keys, *response_keys;
277 const struct port *port;
268 const struct portal_group *pg;
278 const struct portal_group *pg;
269 const struct target *targ;
270 const char *send_targets;
271
272 pg = conn->conn_portal->p_portal_group;
273
274 log_debugx("beginning discovery session; waiting for Text PDU");
275 request = text_receive(conn);
276 request_keys = keys_new();
277 keys_load(request_keys, request);
278
279 send_targets = keys_find(request_keys, "SendTargets");
280 if (send_targets == NULL)
281 log_errx(1, "received Text PDU without SendTargets");
282
283 response = text_new_response(request);
284 response_keys = keys_new();
285
286 if (strcmp(send_targets, "All") == 0) {
279 const char *send_targets;
280
281 pg = conn->conn_portal->p_portal_group;
282
283 log_debugx("beginning discovery session; waiting for Text PDU");
284 request = text_receive(conn);
285 request_keys = keys_new();
286 keys_load(request_keys, request);
287
288 send_targets = keys_find(request_keys, "SendTargets");
289 if (send_targets == NULL)
290 log_errx(1, "received Text PDU without SendTargets");
291
292 response = text_new_response(request);
293 response_keys = keys_new();
294
295 if (strcmp(send_targets, "All") == 0) {
287 TAILQ_FOREACH(targ, &pg->pg_conf->conf_targets, t_next) {
288 if (targ->t_portal_group != pg) {
289 log_debugx("not returning target \"%s\"; "
290 "belongs to a different portal group",
291 targ->t_name);
292 continue;
293 }
294 if (discovery_target_filtered_out(conn, targ)) {
296 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
297 if (discovery_target_filtered_out(conn, port)) {
295 /* Ignore this target. */
296 continue;
297 }
298 /* Ignore this target. */
299 continue;
300 }
298 discovery_add_target(response_keys, targ);
301 discovery_add_target(response_keys, port->p_target);
299 }
300 } else {
302 }
303 } else {
301 targ = target_find(pg->pg_conf, send_targets);
302 if (targ == NULL) {
304 port = port_find_in_pg(pg, send_targets);
305 if (port == NULL) {
303 log_debugx("initiator requested information on unknown "
304 "target \"%s\"; returning nothing", send_targets);
305 } else {
306 log_debugx("initiator requested information on unknown "
307 "target \"%s\"; returning nothing", send_targets);
308 } else {
306 if (discovery_target_filtered_out(conn, targ)) {
309 if (discovery_target_filtered_out(conn, port)) {
307 /* Ignore this target. */
308 } else {
310 /* Ignore this target. */
311 } else {
309 discovery_add_target(response_keys, targ);
312 discovery_add_target(response_keys, port->p_target);
310 }
311 }
312 }
313 keys_save(response_keys, response);
314
315 pdu_send(response);
316 pdu_delete(response);
317 keys_delete(response_keys);

--- 13 unchanged lines hidden ---
313 }
314 }
315 }
316 keys_save(response_keys, response);
317
318 pdu_send(response);
319 pdu_delete(response);
320 keys_delete(response_keys);

--- 13 unchanged lines hidden ---