1 /*
2    Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as published by
6    the Free Software Foundation; either version 2.1 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 
25 #ifdef HAVE_ARPA_INET_H
26 #include <arpa/inet.h>
27 #endif
28 
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 #ifdef HAVE_POLL_H
34 #include <poll.h>
35 #endif
36 
37 #ifdef AROS
38 #include "aros/aros_compat.h"
39 #endif
40 
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 
45 #if defined(_WIN32)
46 #include <winsock2.h>
47 #include <ws2tcpip.h>
48 #include "win32/win32_compat.h"
49 #else
50 #include <strings.h>
51 #include <netdb.h>
52 #include <netinet/in.h>
53 #include <netinet/tcp.h>
54 #include <sys/ioctl.h>
55 #endif
56 
57 #ifdef NEED_SYS_FILIO_H
58 #include <sys/filio.h>
59 #endif
60 
61 #ifdef HAVE_SYS_UIO_H
62 #include <sys/uio.h>
63 #endif
64 
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <time.h>
72 #include "scsi-lowlevel.h"
73 #include "iscsi.h"
74 #include "iscsi-private.h"
75 #include "slist.h"
76 
77 static uint32_t iface_rr = 0;
78 struct iscsi_transport;
79 
80 /* MUST keep in sync with iser.c */
81 union socket_address {
82 	struct sockaddr_in sin;
83 	struct sockaddr_in6 sin6;
84 	struct sockaddr sa;
85 };
86 
87 void
iscsi_add_to_outqueue(struct iscsi_context * iscsi,struct iscsi_pdu * pdu)88 iscsi_add_to_outqueue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
89 {
90 	struct iscsi_pdu *current = iscsi->outqueue;
91 	struct iscsi_pdu *last = NULL;
92 
93 	if (iscsi->scsi_timeout > 0) {
94 		pdu->scsi_timeout = time(NULL) + iscsi->scsi_timeout;
95 	} else {
96 		pdu->scsi_timeout = 0;
97 	}
98 
99 	if (iscsi->outqueue == NULL) {
100 		iscsi->outqueue = pdu;
101 		pdu->next = NULL;
102 		return;
103 	}
104 
105 	/* queue pdus in ascending order of CmdSN.
106 	 * ensure that pakets with the same CmdSN are kept in FIFO order.
107 	 * immediate PDUs are queued in front of queue with the CmdSN
108 	 * of the first element in the outqueue.
109 	 */
110 
111 	if (pdu->outdata.data[0] & ISCSI_PDU_IMMEDIATE) {
112 		iscsi_pdu_set_cmdsn(pdu, current->cmdsn);
113 	}
114 
115 	do {
116 		if (iscsi_serial32_compare(pdu->cmdsn, current->cmdsn) < 0 ||
117 			(pdu->outdata.data[0] & ISCSI_PDU_IMMEDIATE && !(current->outdata.data[0] & ISCSI_PDU_IMMEDIATE))) {
118 			/* insert PDU before the current */
119 			if (last != NULL) {
120 				last->next=pdu;
121 			} else {
122 				iscsi->outqueue=pdu;
123 			}
124 			pdu->next = current;
125 			return;
126 		}
127 		last=current;
128 		current=current->next;
129 	} while (current != NULL);
130 
131 	last->next = pdu;
132 	pdu->next = NULL;
133 }
134 
iscsi_decrement_iface_rr()135 void iscsi_decrement_iface_rr() {
136 	iface_rr--;
137 }
138 
set_nonblocking(int fd)139 static int set_nonblocking(int fd)
140 {
141 #if defined(_WIN32)
142 	unsigned long opt = 1;
143 	return ioctlsocket(fd, FIONBIO, &opt);
144 #else
145 	unsigned v;
146 	v = fcntl(fd, F_GETFL, 0);
147 	return fcntl(fd, F_SETFL, v | O_NONBLOCK);
148 #endif
149 }
150 
set_tcp_sockopt(int sockfd,int optname,int value)151 static int set_tcp_sockopt(int sockfd, int optname, int value)
152 {
153 	int level;
154 
155 	#ifndef SOL_TCP
156 	struct protoent *buf;
157 
158 	if ((buf = getprotobyname("tcp")) != NULL)
159 		level = buf->p_proto;
160 	else
161 		return -1;
162 	#else
163 		level = SOL_TCP;
164 	#endif
165 
166 	return setsockopt(sockfd, level, optname, (char *)&value, sizeof(value));
167 }
168 
169 #ifndef TCP_USER_TIMEOUT
170 #define TCP_USER_TIMEOUT        18
171 #endif
172 
set_tcp_user_timeout(struct iscsi_context * iscsi)173 static int set_tcp_user_timeout(struct iscsi_context *iscsi)
174 {
175 	if (set_tcp_sockopt(iscsi->fd, TCP_USER_TIMEOUT, iscsi->tcp_user_timeout) != 0) {
176 		iscsi_set_error(iscsi, "TCP: Failed to set tcp user timeout. Error %s(%d)", strerror(errno), errno);
177 		return -1;
178 	}
179 	ISCSI_LOG(iscsi, 3, "TCP_USER_TIMEOUT set to %d",iscsi->tcp_user_timeout);
180 	return 0;
181 }
182 
183 #ifndef TCP_SYNCNT
184 #define TCP_SYNCNT        7
185 #endif
186 
set_tcp_syncnt(struct iscsi_context * iscsi)187 static int set_tcp_syncnt(struct iscsi_context *iscsi)
188 {
189 	if (set_tcp_sockopt(iscsi->fd, TCP_SYNCNT, iscsi->tcp_syncnt) != 0) {
190 		iscsi_set_error(iscsi, "TCP: Failed to set tcp syn retries. Error %s(%d)", strerror(errno), errno);
191 		return -1;
192 	}
193 	ISCSI_LOG(iscsi, 3, "TCP_SYNCNT set to %d",iscsi->tcp_syncnt);
194 	return 0;
195 }
196 
iscsi_tcp_connect(struct iscsi_context * iscsi,union socket_address * sa,int ai_family)197 static int iscsi_tcp_connect(struct iscsi_context *iscsi, union socket_address *sa, int ai_family) {
198 
199 	int socksize;
200 
201 	switch (ai_family) {
202 	case AF_INET:
203                 socksize = sizeof(struct sockaddr_in);
204                 break;
205 	case AF_INET6:
206                 socksize = sizeof(struct sockaddr_in6);
207                 break;
208         default:
209 		iscsi_set_error(iscsi, "Unknown address family :%d. "
210 				"Only IPv4/IPv6 supported so far.",
211 				ai_family);
212                 return -1;
213         }
214 
215 	iscsi->fd = socket(ai_family, SOCK_STREAM, 0);
216 	if (iscsi->fd == -1) {
217 		iscsi_set_error(iscsi, "Failed to open iscsi socket. "
218 				"Errno:%s(%d).", strerror(errno), errno);
219 		return -1;
220 	}
221 
222 	if (iscsi->old_iscsi && iscsi->fd != iscsi->old_iscsi->fd) {
223 		if (dup2(iscsi->fd, iscsi->old_iscsi->fd) == -1) {
224 			return -1;
225 		}
226 		close(iscsi->fd);
227 		iscsi->fd = iscsi->old_iscsi->fd;
228 	}
229 
230 	iscsi->tcp_nonblocking = !set_nonblocking(iscsi->fd);
231 
232 	iscsi_set_tcp_keepalive(iscsi, iscsi->tcp_keepidle, iscsi->tcp_keepcnt, iscsi->tcp_keepintvl);
233 
234 	if (iscsi->tcp_user_timeout > 0) {
235 		set_tcp_user_timeout(iscsi);
236 	}
237 
238 	if (iscsi->tcp_syncnt > 0) {
239 		set_tcp_syncnt(iscsi);
240 	}
241 
242 #if __linux
243 	if (iscsi->bind_interfaces[0]) {
244 		char *pchr = iscsi->bind_interfaces, *pchr2;
245 		int iface_n = iface_rr++%iscsi->bind_interfaces_cnt;
246 		int iface_c = 0;
247 		do {
248 			pchr2 = strchr(pchr,',');
249 			if (iface_c == iface_n) {
250 				if (pchr2) pchr2[0]=0x00;
251 				break;
252 			}
253 			if (pchr2) {pchr=pchr2+1;}
254 			iface_c++;
255 		} while (pchr2);
256 
257 		int res = setsockopt(iscsi->fd, SOL_SOCKET, SO_BINDTODEVICE, pchr, strlen(pchr));
258 		if (res < 0) {
259 			ISCSI_LOG(iscsi,1,"failed to bind to interface '%s': %s",pchr,strerror(errno));
260 		} else {
261 			ISCSI_LOG(iscsi,3,"successfully bound to interface '%s'",pchr);
262 		}
263 		if (pchr2) pchr2[0]=',';
264 	}
265 #endif
266 
267 	if (set_tcp_sockopt(iscsi->fd, TCP_NODELAY, 1) != 0) {
268 		ISCSI_LOG(iscsi,1,"failed to set TCP_NODELAY sockopt: %s",strerror(errno));
269 	} else {
270 		ISCSI_LOG(iscsi,3,"TCP_NODELAY set to 1");
271 	}
272 
273 	if (connect(iscsi->fd, &sa->sa, socksize) != 0
274 #if defined(_WIN32)
275             && WSAGetLastError() != WSAEWOULDBLOCK
276 #endif
277             && errno != EINPROGRESS) {
278 		iscsi_set_error(iscsi, "Connect failed with errno : "
279 			"%s(%d)", strerror(errno), errno);
280 		close(iscsi->fd);
281 		iscsi->fd = -1;
282 		return -1;
283 	}
284 
285 	return 0;
286 }
287 
288 
289 
290 int
iscsi_connect_async(struct iscsi_context * iscsi,const char * portal,iscsi_command_cb cb,void * private_data)291 iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
292 		    iscsi_command_cb cb, void *private_data)
293 {
294 	int port = 3260;
295 	char *str;
296 	char *addr, *host;
297 	struct addrinfo *ai = NULL;
298 	union socket_address sa;
299 	int socksize;
300 
301 	ISCSI_LOG(iscsi, 2, "connecting to portal %s",portal);
302 
303 	if (iscsi->fd != -1) {
304 		iscsi_set_error(iscsi,
305 				"Trying to connect but already connected.");
306 		return -1;
307 	}
308 
309 	addr = iscsi_strdup(iscsi, portal);
310 	if (addr == NULL) {
311 		iscsi_set_error(iscsi, "Out-of-memory: "
312 				"Failed to strdup portal address.");
313 		return -1;
314 	}
315 	host = addr;
316 
317 	/* check if we have a target portal group tag */
318 	str = strrchr(host, ',');
319 	if (str != NULL) {
320 		str[0] = 0;
321 	}
322 
323 	str = strrchr(host, ':');
324 	if (str != NULL) {
325 		if (strchr(str, ']') == NULL) {
326 			if (str != NULL) {
327 				port = atoi(str+1);
328 				str[0] = 0;
329 			}
330 		}
331 	}
332 
333 	/* ipv6 in [...] form ? */
334 	if (host[0] == '[') {
335 		host ++;
336 		str = strchr(host, ']');
337 		if (str == NULL) {
338 			iscsi_free(iscsi, addr);
339 			iscsi_set_error(iscsi, "Invalid target:%s  "
340 				"Missing ']' in IPv6 address", portal);
341 			return -1;
342 		}
343 		*str = 0;
344 	}
345 
346 	/* is it a hostname ? */
347 	if (getaddrinfo(host, NULL, NULL, &ai) != 0) {
348 		iscsi_free(iscsi, addr);
349 		iscsi_set_error(iscsi, "Invalid target:%s  "
350 			"Can not resolv into IPv4/v6.", portal);
351 		return -1;
352 	}
353 	iscsi_free(iscsi, addr);
354 
355 	memset(&sa, 0, sizeof(sa));
356 	switch (ai->ai_family) {
357 	case AF_INET:
358 		socksize = sizeof(struct sockaddr_in);
359 		memcpy(&sa.sin, ai->ai_addr, socksize);
360                 sa.sin.sin_family = AF_INET;
361 		sa.sin.sin_port = htons(port);
362 #ifdef HAVE_SOCK_SIN_LEN
363 		sa.sin.sin_len = socksize;
364 #endif
365 		break;
366 #ifdef HAVE_SOCKADDR_IN6
367 	case AF_INET6:
368 		socksize = sizeof(struct sockaddr_in6);
369 		memcpy(&sa.sin6, ai->ai_addr, socksize);
370                 sa.sin6.sin6_family = AF_INET6;
371 		sa.sin6.sin6_port = htons(port);
372 #ifdef HAVE_SOCK_SIN_LEN
373 		sa.sin6.sin6_len = socksize;
374 #endif
375 		break;
376 #endif
377 	default:
378 		iscsi_set_error(iscsi, "Unknown address family :%d. "
379 				"Only IPv4/IPv6 supported so far.",
380 				ai->ai_family);
381 		freeaddrinfo(ai);
382 		return -1;
383 
384 	}
385 
386 	iscsi->socket_status_cb  = cb;
387 	iscsi->connect_data      = private_data;
388 
389 	if (iscsi->drv->connect(iscsi, &sa, ai->ai_family) < 0) {
390 		iscsi_set_error(iscsi, "Couldn't connect transport: %s",
391                                 iscsi_get_error(iscsi));
392 		freeaddrinfo(ai);
393 		return -1;
394 	}
395 
396 	freeaddrinfo(ai);
397 	strncpy(iscsi->connected_portal, portal, MAX_STRING_SIZE);
398 	return 0;
399 }
400 
401 static int
iscsi_tcp_disconnect(struct iscsi_context * iscsi)402 iscsi_tcp_disconnect(struct iscsi_context *iscsi)
403 {
404 	if (iscsi->fd == -1) {
405 		iscsi_set_error(iscsi, "Trying to disconnect "
406 				"but not connected");
407 		return -1;
408 	}
409 
410 	close(iscsi->fd);
411 
412 	if (!(iscsi->pending_reconnect && iscsi->old_iscsi) &&
413 	    iscsi->connected_portal[0]) {
414 		ISCSI_LOG(iscsi, 2, "disconnected from portal %s",iscsi->connected_portal);
415 	}
416 
417 	iscsi->fd  = -1;
418 	iscsi->is_connected = 0;
419 	iscsi->is_corked = 0;
420 
421 	return 0;
422 }
423 
424 
425 int
iscsi_disconnect(struct iscsi_context * iscsi)426 iscsi_disconnect(struct iscsi_context *iscsi)
427 {
428         return iscsi->drv->disconnect(iscsi);
429 }
430 
431 static int
iscsi_tcp_get_fd(struct iscsi_context * iscsi)432 iscsi_tcp_get_fd(struct iscsi_context *iscsi)
433 {
434 	if (iscsi->old_iscsi) {
435 		return iscsi->old_iscsi->fd;
436 	}
437 	return iscsi->fd;
438 }
439 
440 int
iscsi_get_fd(struct iscsi_context * iscsi)441 iscsi_get_fd(struct iscsi_context *iscsi)
442 {
443 	return iscsi->drv->get_fd(iscsi);
444 }
445 
446 static int
iscsi_tcp_which_events(struct iscsi_context * iscsi)447 iscsi_tcp_which_events(struct iscsi_context *iscsi)
448 {
449 	int events = iscsi->is_connected ? POLLIN : POLLOUT;
450 
451 	if (iscsi->pending_reconnect && iscsi->old_iscsi &&
452 		time(NULL) < iscsi->next_reconnect) {
453 		return 0;
454 	}
455 
456 	if (iscsi->outqueue_current != NULL ||
457 	    (iscsi->outqueue != NULL && !iscsi->is_corked &&
458 	     (iscsi_serial32_compare(iscsi->outqueue->cmdsn, iscsi->maxcmdsn) <= 0 ||
459 	      iscsi->outqueue->outdata.data[0] & ISCSI_PDU_IMMEDIATE)
460 	    )
461 	   ) {
462 		events |= POLLOUT;
463 	}
464 	return events;
465 }
466 
467 int
iscsi_which_events(struct iscsi_context * iscsi)468 iscsi_which_events(struct iscsi_context *iscsi)
469 {
470 	return iscsi->drv->which_events(iscsi);
471 }
472 
473 int
iscsi_queue_length(struct iscsi_context * iscsi)474 iscsi_queue_length(struct iscsi_context *iscsi)
475 {
476 	int i = 0;
477 	struct iscsi_pdu *pdu;
478 
479 	for (pdu = iscsi->outqueue; pdu; pdu = pdu->next) {
480 		i++;
481 	}
482 	for (pdu = iscsi->waitpdu; pdu; pdu = pdu->next) {
483 		i++;
484 	}
485 	if (iscsi->is_connected == 0) {
486 		i++;
487 	}
488 
489 	return i;
490 }
491 
492 int
iscsi_out_queue_length(struct iscsi_context * iscsi)493 iscsi_out_queue_length(struct iscsi_context *iscsi)
494 {
495 	int i = 0;
496 	struct iscsi_pdu *pdu;
497 
498 	for (pdu = iscsi->outqueue; pdu; pdu = pdu->next) {
499 		i++;
500 	}
501 
502 	return i;
503 }
504 
505 ssize_t
iscsi_iovector_readv_writev(struct iscsi_context * iscsi,struct scsi_iovector * iovector,uint32_t pos,ssize_t count,int do_write)506 iscsi_iovector_readv_writev(struct iscsi_context *iscsi, struct scsi_iovector *iovector, uint32_t pos, ssize_t count, int do_write)
507 {
508         struct scsi_iovec *iov, *iov2;
509         int niov;
510         uint32_t len2;
511         size_t _len2;
512         ssize_t n;
513 
514         if (iovector->iov == NULL) {
515 		errno = EINVAL;
516 		return -1;
517 	}
518 
519 	if (pos < iovector->offset) {
520 		iscsi_set_error(iscsi, "iovector reset. pos is smaller than"
521 				"current offset");
522 		errno = EINVAL;
523 		return -1;
524 	}
525 
526 	if (iovector->niov <= iovector->consumed) {
527 		/* someone issued a read/write but did not provide enough user buffers for all the data.
528 		 * maybe someone tried to read just 512 bytes off a MMC device?
529 		 */
530 		errno = EINVAL;
531 		return -1;
532 	}
533 
534 	/* iov is a pointer to the first iovec to pass */
535 	iov = &iovector->iov[iovector->consumed];
536 	pos -= iovector->offset;
537 
538 	/* forward until iov points to the first iov to pass */
539 	while (pos >= iov->iov_len) {
540 		iovector->offset += iov->iov_len;
541 		iovector->consumed++;
542 		pos -= iov->iov_len;
543 		if (iovector->niov <= iovector->consumed) {
544 			errno = EINVAL;
545 			return -1;
546 		}
547 		iov = &iovector->iov[iovector->consumed];
548 	}
549 
550 	iov2 = iov;         /* iov2 is a pointer to the last iovec to pass */
551 	niov = 1;           /* number of iovectors to pass */
552 	len2 = pos + count; /* adjust length of iov2 */
553 
554 	/* forward until iov2 points to the last iovec we pass later. it might
555 	   happen that we have a lot of iovectors but are limited by count */
556 	while (len2 > iov2->iov_len) {
557 		niov++;
558 		if (iovector->niov < iovector->consumed + niov) {
559 			errno = EINVAL;
560 			return -1;
561 		}
562 		len2 -= iov2->iov_len;
563 		iov2 = &iovector->iov[iovector->consumed + niov - 1];
564 	}
565 
566 	/* we might limit the length of the last iovec we pass to readv/writev
567 	   store its orignal length to restore it later */
568 	_len2 = iov2->iov_len;
569 
570 	/* adjust base+len of start iovec and len of last iovec */
571 	iov2->iov_len = len2;
572 	iov->iov_base = (void*) ((uintptr_t)iov->iov_base + pos);
573 	iov->iov_len -= pos;
574 
575 	if (do_write) {
576 		n = writev(iscsi->fd, (struct iovec*) iov, niov);
577 	} else {
578 		n = readv(iscsi->fd, (struct iovec*) iov, niov);
579 	}
580 
581 	/* restore original values */
582 	iov->iov_base = (void*) ((uintptr_t)iov->iov_base - pos);
583 	iov->iov_len += pos;
584 	iov2->iov_len = _len2;
585 
586 	if (n > count) {
587 		/* we read/write more bytes than expected, this MUST not happen */
588 		errno = EINVAL;
589 		return -1;
590 	}
591 	return n;
592 }
593 
594 static int
iscsi_read_from_socket(struct iscsi_context * iscsi)595 iscsi_read_from_socket(struct iscsi_context *iscsi)
596 {
597 	struct iscsi_in_pdu *in;
598 	ssize_t hdr_size, data_size, count, padding_size;
599 
600 	do {
601 		hdr_size = ISCSI_HEADER_SIZE(iscsi->header_digest);
602 		if (iscsi->incoming == NULL) {
603 			iscsi->incoming = iscsi_szmalloc(iscsi, sizeof(struct iscsi_in_pdu));
604 			if (iscsi->incoming == NULL) {
605 				iscsi_set_error(iscsi, "Out-of-memory: failed to malloc iscsi_in_pdu");
606 				return -1;
607 			}
608 			iscsi->incoming->hdr = iscsi_smalloc(iscsi, hdr_size);
609 			if (iscsi->incoming->hdr == NULL) {
610 				iscsi_set_error(iscsi, "Out-of-memory");
611 				return -1;
612 			}
613 		}
614 		in = iscsi->incoming;
615 
616 		/* first we must read the header, including any digests */
617 		if (in->hdr_pos < hdr_size) {
618 			/* try to only read the header, the socket is nonblocking, so
619 			 * no need to limit the read to what is available in the socket
620 			 */
621 			count = hdr_size - in->hdr_pos;
622 			count = recv(iscsi->fd, &in->hdr[in->hdr_pos], count, 0);
623 			if (count == 0) {
624 				/* remote side has closed the socket. */
625 				return -1;
626 			}
627 			if (count < 0) {
628 				if (errno == EINTR || errno == EAGAIN) {
629 					break;
630 				}
631 				iscsi_set_error(iscsi, "read from socket failed, "
632 					"errno:%d", errno);
633 				return -1;
634 			}
635 			in->hdr_pos  += count;
636 		}
637 
638 		if (in->hdr_pos < hdr_size) {
639 			/* we don't have the full header yet, so return */
640 			break;
641 		}
642 
643 		padding_size = iscsi_get_pdu_padding_size(&in->hdr[0]);
644 		data_size = iscsi_get_pdu_data_size(&in->hdr[0]) + padding_size;
645 
646 		if (data_size < 0 || data_size > (ssize_t)iscsi->initiator_max_recv_data_segment_length) {
647 			iscsi_set_error(iscsi, "Invalid data size received from target (%d)", (int)data_size);
648 			return -1;
649 		}
650 		if (data_size != 0) {
651 			unsigned char padding_buf[3];
652 			unsigned char *buf = padding_buf;
653 			struct scsi_iovector * iovector_in;
654 
655 			count = data_size - in->data_pos;
656 
657 			/* first try to see if we already have a user buffer */
658 			iovector_in = iscsi_get_scsi_task_iovector_in(iscsi, in);
659 			if (iovector_in != NULL && count > padding_size) {
660 				uint32_t offset = scsi_get_uint32(&in->hdr[40]);
661 				count = iscsi_iovector_readv_writev(iscsi, iovector_in, in->data_pos + offset, count - padding_size, 0);
662 			} else {
663 				if (iovector_in == NULL) {
664 					if (in->data == NULL) {
665 						in->data = iscsi_malloc(iscsi, data_size);
666 						if (in->data == NULL) {
667 							iscsi_set_error(iscsi, "Out-of-memory: failed to malloc iscsi_in_pdu->data(%d)", (int)data_size);
668 							return -1;
669 						}
670 					}
671 					buf = &in->data[in->data_pos];
672 				}
673 				count = recv(iscsi->fd, buf, count, 0);
674 			}
675 			if (count == 0) {
676 				/* remote side has closed the socket. */
677 				return -1;
678 			}
679 			if (count < 0) {
680 				if (errno == EINTR || errno == EAGAIN) {
681 					break;
682 				}
683 				iscsi_set_error(iscsi, "read from socket failed, "
684 						"errno:%d %s", errno,
685 						iscsi_get_error(iscsi));
686 				return -1;
687 			}
688 			in->data_pos += count;
689 		}
690 
691 		if (in->data_pos < data_size) {
692 			break;
693 		}
694 
695 		iscsi->incoming = NULL;
696 		if (iscsi_process_pdu(iscsi, in) != 0) {
697 			iscsi_free_iscsi_in_pdu(iscsi, in);
698 			return -1;
699 		}
700 		iscsi_free_iscsi_in_pdu(iscsi, in);
701 	} while (iscsi->tcp_nonblocking && iscsi->waitpdu && iscsi->is_loggedin);
702 
703 	return 0;
704 }
705 
iscsi_pdu_update_headerdigest(struct iscsi_context * iscsi,struct iscsi_pdu * pdu)706 static int iscsi_pdu_update_headerdigest(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
707 {
708 	uint32_t crc;
709 
710 	if (pdu->outdata.size < ISCSI_RAW_HEADER_SIZE + ISCSI_DIGEST_SIZE) {
711 		iscsi_set_error(iscsi, "PDU too small (%u) to contain header digest",
712 				(unsigned int) pdu->outdata.size);
713 		return -1;
714 	}
715 
716 	crc = crc32c(pdu->outdata.data, ISCSI_RAW_HEADER_SIZE);
717 
718 	pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+3] = (crc >> 24);
719 	pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+2] = (crc >> 16);
720 	pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+1] = (crc >>  8);
721 	pdu->outdata.data[ISCSI_RAW_HEADER_SIZE+0] = (crc);
722 	return 0;
723 }
724 
725 static int
iscsi_write_to_socket(struct iscsi_context * iscsi)726 iscsi_write_to_socket(struct iscsi_context *iscsi)
727 {
728 	ssize_t count;
729 	size_t total;
730 	struct iscsi_pdu *pdu;
731 	static char padding_buf[3];
732 	int socket_flags = 0;
733 
734 #ifdef MSG_NOSIGNAL
735 	socket_flags |= MSG_NOSIGNAL;
736 #elif SO_NOSIGPIPE
737 	socket_flags |= SO_NOSIGPIPE;
738 #endif
739 
740 	if (iscsi->fd == -1) {
741 		iscsi_set_error(iscsi, "trying to write but not connected");
742 		return -1;
743 	}
744 
745 	while (iscsi->outqueue != NULL || iscsi->outqueue_current != NULL) {
746 		if (iscsi->outqueue_current == NULL) {
747 			if (iscsi->is_corked) {
748 				/* connection is corked we are not allowed to send
749 				 * additional PDUs */
750 				ISCSI_LOG(iscsi, 6, "iscsi_write_to_socket: socket is corked");
751 				return 0;
752 			}
753 
754 			if (iscsi_serial32_compare(iscsi->outqueue->cmdsn, iscsi->maxcmdsn) > 0
755 				&& !(iscsi->outqueue->outdata.data[0] & ISCSI_PDU_IMMEDIATE)) {
756 				/* stop sending for non-immediate PDUs. maxcmdsn is reached */
757 				ISCSI_LOG(iscsi, 6,
758 				          "iscsi_write_to_socket: maxcmdsn reached (outqueue[0]->cmdsnd %08x > maxcmdsn %08x)",
759 				          iscsi->outqueue->cmdsn, iscsi->maxcmdsn);
760 				return 0;
761 			}
762 
763 			/* pop first element of the outqueue */
764 			if (iscsi_serial32_compare(iscsi->outqueue->cmdsn, iscsi->expcmdsn) < 0 &&
765 				(iscsi->outqueue->outdata.data[0] & 0x3f) != ISCSI_PDU_DATA_OUT) {
766 				iscsi_set_error(iscsi, "iscsi_write_to_socket: outqueue[0]->cmdsn < expcmdsn (%08x < %08x) opcode %02x",
767 				                iscsi->outqueue->cmdsn, iscsi->expcmdsn, iscsi->outqueue->outdata.data[0] & 0x3f);
768 				return -1;
769 			}
770 			iscsi->outqueue_current = iscsi->outqueue;
771 
772 			/* set exp statsn */
773 			iscsi_pdu_set_expstatsn(iscsi->outqueue_current, iscsi->statsn + 1);
774 
775 			/* calculate header checksum */
776 			if (iscsi->header_digest != ISCSI_HEADER_DIGEST_NONE &&
777 				iscsi_pdu_update_headerdigest(iscsi, iscsi->outqueue_current) != 0) {
778 				return -1;
779 			}
780 
781 			ISCSI_LIST_REMOVE(&iscsi->outqueue, iscsi->outqueue_current);
782 			if (!(iscsi->outqueue_current->flags & ISCSI_PDU_DELETE_WHEN_SENT)) {
783 				/* we have to add the pdu to the waitqueue already here
784 				   since the storage might sent a R2T as soon as it has
785 				   received the header. if we sent immediate data in a
786 				   cmd PDU the R2T might get lost otherwise. */
787 				ISCSI_LIST_ADD_END(&iscsi->waitpdu, iscsi->outqueue_current);
788 			}
789 		}
790 
791 		pdu = iscsi->outqueue_current;
792 		pdu->outdata.size = (pdu->outdata.size + 3) & 0xfffffffc;
793 
794 		/* Write header and any immediate data */
795 		if (pdu->outdata_written < pdu->outdata.size) {
796 			count = send(iscsi->fd,
797 				     pdu->outdata.data + pdu->outdata_written,
798 				     pdu->outdata.size - pdu->outdata_written,
799 				     socket_flags);
800 			if (count == -1) {
801 				if (errno == EAGAIN || errno == EWOULDBLOCK) {
802 					return 0;
803 				}
804 				iscsi_set_error(iscsi, "Error when writing to "
805 						"socket :%d", errno);
806 				return -1;
807 			}
808 			pdu->outdata_written += count;
809 		}
810 		/* if we havent written the full header yet. */
811 		if (pdu->outdata_written != pdu->outdata.size) {
812 			return 0;
813 		}
814 
815 		/* Write any iovectors that might have been passed to us */
816 		while (pdu->payload_written < pdu->payload_len) {
817 			struct scsi_iovector* iovector_out;
818 
819 			iovector_out = iscsi_get_scsi_task_iovector_out(iscsi, pdu);
820 
821 			if (iovector_out == NULL) {
822 				iscsi_set_error(iscsi, "Can't find iovector data for DATA-OUT");
823 				return -1;
824 			}
825 
826 			count = iscsi_iovector_readv_writev(iscsi,
827 				iovector_out,
828 				pdu->payload_offset + pdu->payload_written,
829 				pdu->payload_len - pdu->payload_written, 1);
830 			if (count == -1) {
831 				if (errno == EAGAIN || errno == EWOULDBLOCK) {
832 					return 0;
833 				}
834 				iscsi_set_error(iscsi, "Error when writing to "
835 						"socket :%d %s", errno,
836 						iscsi_get_error(iscsi));
837 				return -1;
838 			}
839 
840 			pdu->payload_written += count;
841 		}
842 
843 		total = pdu->payload_len;
844 		total = (total + 3) & 0xfffffffc;
845 
846 		/* Write padding */
847 		if (pdu->payload_written < total) {
848 			count = send(iscsi->fd, padding_buf, total - pdu->payload_written, socket_flags);
849 			if (count == -1) {
850 				if (errno == EAGAIN || errno == EWOULDBLOCK) {
851 					return 0;
852 				}
853 				iscsi_set_error(iscsi, "Error when writing to "
854 						"socket :%d", errno);
855 				return -1;
856 			}
857 			pdu->payload_written += count;
858 		}
859 		/* if we havent written the full padding yet. */
860 		if (pdu->payload_written != total) {
861 			return 0;
862 		}
863 		if (pdu->flags & ISCSI_PDU_CORK_WHEN_SENT) {
864 			iscsi->is_corked = 1;
865 		}
866 		if (pdu->flags & ISCSI_PDU_DELETE_WHEN_SENT) {
867 			iscsi->drv->free_pdu(iscsi, pdu);
868 		}
869 		iscsi->outqueue_current = NULL;
870 	}
871 	return 0;
872 }
873 
874 int
iscsi_service_reconnect_if_loggedin(struct iscsi_context * iscsi)875 iscsi_service_reconnect_if_loggedin(struct iscsi_context *iscsi)
876 {
877 	if (iscsi->is_loggedin) {
878 		if (iscsi_reconnect(iscsi) == 0) {
879 			return 0;
880 		}
881 	}
882 	if (iscsi->old_iscsi) {
883 		if (!iscsi->pending_reconnect) {
884 			iscsi_reconnect_cb(iscsi, SCSI_STATUS_ERROR, NULL, NULL);
885 		}
886 		return 0;
887 	}
888 	iscsi_set_error(iscsi, "iscsi_service_reconnect_if_loggedin. Can not "
889 			"reconnect right now.\n");
890 	return -1;
891 }
892 
893 static int
iscsi_tcp_service(struct iscsi_context * iscsi,int revents)894 iscsi_tcp_service(struct iscsi_context *iscsi, int revents)
895 {
896 	if (iscsi->fd < 0) {
897 		return 0;
898 	}
899 
900 	if (iscsi->pending_reconnect) {
901 		if (time(NULL) >= iscsi->next_reconnect) {
902 			return iscsi_reconnect(iscsi);
903 		} else {
904 			if (iscsi->old_iscsi) {
905 				return 0;
906 			}
907 		}
908 	}
909 
910 	if (revents & POLLERR) {
911 		int err = 0;
912 		socklen_t err_size = sizeof(err);
913 
914 		if (getsockopt(iscsi->fd, SOL_SOCKET, SO_ERROR,
915 			       (char *)&err, &err_size) != 0 || err != 0) {
916 			if (err == 0) {
917 				err = errno;
918 			}
919 			iscsi_set_error(iscsi, "iscsi_service: socket error "
920 					"%s(%d).",
921 					strerror(err), err);
922 		} else {
923 			iscsi_set_error(iscsi, "iscsi_service: POLLERR, "
924 					"Unknown socket error.");
925 		}
926 		if (iscsi->socket_status_cb) {
927 			iscsi->socket_status_cb(iscsi, SCSI_STATUS_ERROR, NULL,
928 						iscsi->connect_data);
929 			iscsi->socket_status_cb = NULL;
930 		}
931 		return iscsi_service_reconnect_if_loggedin(iscsi);
932 	}
933 	if (revents & POLLHUP) {
934 		iscsi_set_error(iscsi, "iscsi_service: POLLHUP, "
935 				"socket error.");
936 		if (iscsi->socket_status_cb) {
937 			iscsi->socket_status_cb(iscsi, SCSI_STATUS_ERROR, NULL,
938 						iscsi->connect_data);
939 			iscsi->socket_status_cb = NULL;
940 		}
941 		return iscsi_service_reconnect_if_loggedin(iscsi);
942 	}
943 
944 	if (iscsi->is_connected == 0 && revents&POLLOUT) {
945 		int err = 0;
946 		socklen_t err_size = sizeof(err);
947 		struct sockaddr_in local;
948 		socklen_t local_l = sizeof(local);
949 
950 		if (getsockopt(iscsi->fd, SOL_SOCKET, SO_ERROR,
951 			       (char *)&err, &err_size) != 0 || err != 0) {
952 			if (err == 0) {
953 				err = errno;
954 			}
955 			iscsi_set_error(iscsi, "iscsi_service: socket error "
956 					"%s(%d) while connecting.",
957 					strerror(err), err);
958 			if (iscsi->socket_status_cb) {
959 				iscsi->socket_status_cb(iscsi, SCSI_STATUS_ERROR,
960 							NULL, iscsi->connect_data);
961 				iscsi->socket_status_cb = NULL;
962 			}
963 
964 			return iscsi_service_reconnect_if_loggedin(iscsi);
965 		}
966 
967 		if (getsockname(iscsi->fd, (struct sockaddr *) &local, &local_l) == 0) {
968 			ISCSI_LOG(iscsi, 2, "connection established (%s:%u -> %s)", inet_ntoa(local.sin_addr),
969 						(unsigned)ntohs(local.sin_port),iscsi->connected_portal);
970 		}
971 
972 		iscsi->is_connected = 1;
973 		if (iscsi->socket_status_cb) {
974 			iscsi->socket_status_cb(iscsi, SCSI_STATUS_GOOD, NULL,
975 						iscsi->connect_data);
976 			iscsi->socket_status_cb = NULL;
977 		}
978 		return 0;
979 	}
980 
981 	if (revents & POLLIN) {
982 		if (iscsi_read_from_socket(iscsi) != 0) {
983 			return iscsi_service_reconnect_if_loggedin(iscsi);
984 		}
985 	}
986 	if (revents & POLLOUT) {
987 		if (iscsi_write_to_socket(iscsi) != 0) {
988 			return iscsi_service_reconnect_if_loggedin(iscsi);
989 		}
990 	}
991 	iscsi_timeout_scan(iscsi);
992 
993 	return 0;
994 }
995 
996 int
iscsi_service(struct iscsi_context * iscsi,int revents)997 iscsi_service(struct iscsi_context *iscsi, int revents)
998 {
999 	return iscsi->drv->service(iscsi, revents);
1000 }
1001 
iscsi_tcp_queue_pdu(struct iscsi_context * iscsi,struct iscsi_pdu * pdu)1002 static int iscsi_tcp_queue_pdu(struct iscsi_context *iscsi,
1003                                struct iscsi_pdu *pdu)
1004 {
1005 	if (pdu == NULL) {
1006 		iscsi_set_error(iscsi, "trying to queue NULL pdu");
1007 		return -1;
1008 	}
1009 
1010 	iscsi_add_to_outqueue(iscsi, pdu);
1011 
1012 	return 0;
1013 }
1014 
1015 void
iscsi_free_iscsi_in_pdu(struct iscsi_context * iscsi,struct iscsi_in_pdu * in)1016 iscsi_free_iscsi_in_pdu(struct iscsi_context *iscsi, struct iscsi_in_pdu *in)
1017 {
1018 	iscsi_sfree(iscsi, in->hdr);
1019 	iscsi_free(iscsi, in->data);
1020 	in->data=NULL;
1021 	iscsi_sfree(iscsi, in);
1022 	in=NULL;
1023 }
1024 
iscsi_set_tcp_syncnt(struct iscsi_context * iscsi,int value)1025 void iscsi_set_tcp_syncnt(struct iscsi_context *iscsi, int value)
1026 {
1027 	iscsi->tcp_syncnt=value;
1028 	ISCSI_LOG(iscsi, 2, "TCP_SYNCNT will be set to %d on next socket creation",value);
1029 }
1030 
iscsi_set_tcp_user_timeout(struct iscsi_context * iscsi,int value)1031 void iscsi_set_tcp_user_timeout(struct iscsi_context *iscsi, int value)
1032 {
1033 	iscsi->tcp_user_timeout=value;
1034 	ISCSI_LOG(iscsi, 2, "TCP_USER_TIMEOUT will be set to %dms on next socket creation",value);
1035 }
1036 
iscsi_set_tcp_keepidle(struct iscsi_context * iscsi,int value)1037 void iscsi_set_tcp_keepidle(struct iscsi_context *iscsi, int value)
1038 {
1039 	iscsi->tcp_keepidle=value;
1040 	ISCSI_LOG(iscsi, 2, "TCP_KEEPIDLE will be set to %d on next socket creation",value);
1041 }
1042 
iscsi_set_tcp_keepcnt(struct iscsi_context * iscsi,int value)1043 void iscsi_set_tcp_keepcnt(struct iscsi_context *iscsi, int value)
1044 {
1045 	iscsi->tcp_keepcnt=value;
1046 	ISCSI_LOG(iscsi, 2, "TCP_KEEPCNT will be set to %d on next socket creation",value);
1047 }
1048 
iscsi_set_tcp_keepintvl(struct iscsi_context * iscsi,int value)1049 void iscsi_set_tcp_keepintvl(struct iscsi_context *iscsi, int value)
1050 {
1051 	iscsi->tcp_keepintvl=value;
1052 	ISCSI_LOG(iscsi, 2, "TCP_KEEPINTVL will be set to %d on next socket creation",value);
1053 }
1054 
iscsi_set_tcp_keepalive(struct iscsi_context * iscsi,int idle _U_,int count _U_,int interval _U_)1055 int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle _U_, int count _U_, int interval _U_)
1056 {
1057 #ifdef SO_KEEPALIVE
1058 	int value = 1;
1059 	if (setsockopt(iscsi->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&value, sizeof(value)) != 0) {
1060 		iscsi_set_error(iscsi, "TCP: Failed to set socket option SO_KEEPALIVE. Error %s(%d)", strerror(errno), errno);
1061 		return -1;
1062 	}
1063 	ISCSI_LOG(iscsi, 3, "SO_KEEPALIVE set to %d",value);
1064 #ifdef TCP_KEEPCNT
1065 	if (set_tcp_sockopt(iscsi->fd, TCP_KEEPCNT, count) != 0) {
1066 		iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive count. Error %s(%d)", strerror(errno), errno);
1067 		return -1;
1068 	}
1069 	ISCSI_LOG(iscsi, 3, "TCP_KEEPCNT set to %d",count);
1070 #endif
1071 #ifdef TCP_KEEPINTVL
1072 	if (set_tcp_sockopt(iscsi->fd, TCP_KEEPINTVL, interval) != 0) {
1073 		iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive interval. Error %s(%d)", strerror(errno), errno);
1074 		return -1;
1075 	}
1076 	ISCSI_LOG(iscsi, 3, "TCP_KEEPINTVL set to %d",interval);
1077 #endif
1078 #ifdef TCP_KEEPIDLE
1079 	if (set_tcp_sockopt(iscsi->fd, TCP_KEEPIDLE, idle) != 0) {
1080 		iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive idle. Error %s(%d)", strerror(errno), errno);
1081 		return -1;
1082 	}
1083 	ISCSI_LOG(iscsi, 3, "TCP_KEEPIDLE set to %d",idle);
1084 #endif
1085 #endif
1086 
1087 	return 0;
1088 }
1089 
iscsi_set_bind_interfaces(struct iscsi_context * iscsi,char * interfaces _U_)1090 void iscsi_set_bind_interfaces(struct iscsi_context *iscsi, char * interfaces _U_)
1091 {
1092 #if __linux
1093 	strncpy(iscsi->bind_interfaces,interfaces,MAX_STRING_SIZE);
1094 	iscsi->bind_interfaces_cnt=0;
1095 	char * pchr = interfaces;
1096 	char * pchr2 = NULL;
1097 	do {
1098 		pchr2 = strchr(pchr,',');
1099 		if (pchr2) {pchr=pchr2+1;}
1100 		iscsi->bind_interfaces_cnt++;
1101 	} while (pchr2);
1102 	ISCSI_LOG(iscsi,2,"will bind to one of the following %d interface(s) on next socket creation: %s",iscsi->bind_interfaces_cnt,interfaces);
1103 	if (!iface_rr) iface_rr=rand()%iscsi->bind_interfaces_cnt+1;
1104 #else
1105 	ISCSI_LOG(iscsi,1,"binding to an interface is not supported on your OS");
1106 #endif
1107 }
1108 
1109 #if defined(_MSC_VER) && _MSC_VER < 1900
1110 static iscsi_transport iscsi_transport_tcp = {
1111 	iscsi_tcp_connect,
1112 	iscsi_tcp_queue_pdu,
1113 	iscsi_tcp_new_pdu,
1114 	iscsi_tcp_disconnect,
1115 	iscsi_tcp_free_pdu,
1116 	iscsi_tcp_service,
1117 	iscsi_tcp_get_fd,
1118 	iscsi_tcp_which_events,
1119 };
1120 #else
1121 static iscsi_transport iscsi_transport_tcp = {
1122 	.connect      = iscsi_tcp_connect,
1123 	.queue_pdu    = iscsi_tcp_queue_pdu,
1124 	.new_pdu      = iscsi_tcp_new_pdu,
1125 	.disconnect   = iscsi_tcp_disconnect,
1126 	.free_pdu     = iscsi_tcp_free_pdu,
1127 	.service      = iscsi_tcp_service,
1128 	.get_fd       = iscsi_tcp_get_fd,
1129 	.which_events = iscsi_tcp_which_events,
1130 };
1131 #endif
1132 
iscsi_init_tcp_transport(struct iscsi_context * iscsi)1133 void iscsi_init_tcp_transport(struct iscsi_context *iscsi)
1134 {
1135 	iscsi->drv = &iscsi_transport_tcp;
1136 	iscsi->transport = TCP_TRANSPORT;
1137 }
1138