Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 376a7fdad232fa7f9d8ab2dd18778d36701ea658)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision 0578271269331fb048f182ef5c3d26dae37c84ea)
@@ -47,5 +47,4 @@
 #include <stdio.h>
 #include <errno.h>
-#include <err.h>
 
 #include <ipc/ipc.h>
@@ -235,5 +234,5 @@
 int tcp_initialize(async_client_conn_t client_connection)
 {
-	ERROR_DECLARE;
+	int rc;
 
 	assert(client_connection);
@@ -246,18 +245,24 @@
 	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
 	    SERVICE_TCP, client_connection);
-	if (tcp_globals.ip_phone < 0)
+	if (tcp_globals.ip_phone < 0) {
+		fibril_rwlock_write_unlock(&tcp_globals.lock);
 		return tcp_globals.ip_phone;
+	}
 	
-	ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
-	if (ERROR_OCCURRED(packet_dimensions_initialize(
-	    &tcp_globals.dimensions))) {
+	rc = socket_ports_initialize(&tcp_globals.sockets);
+	if (rc != EOK)
+		goto out;
+
+	rc = packet_dimensions_initialize(&tcp_globals.dimensions);
+	if (rc != EOK) {
 		socket_ports_destroy(&tcp_globals.sockets);
-		return ERROR_CODE;
+		goto out;
 	}
 
 	tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
+
+out:
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-
-	return EOK;
+	return rc;
 }
 
@@ -266,5 +271,5 @@
     services_t error)
 {
-	ERROR_DECLARE;
+	int rc;
 
 	if (receiver != SERVICE_TCP)
@@ -272,16 +277,15 @@
 
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))
+	rc = tcp_process_packet(device_id, packet, error);
+	if (rc != EOK)
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
 
-	printf("receive %d \n", ERROR_CODE);
-
-	return ERROR_CODE;
+	printf("receive %d \n", rc);
+
+	return rc;
 }
 
 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
 {
-	ERROR_DECLARE;
-
 	size_t length;
 	size_t offset;
@@ -299,4 +303,5 @@
 	struct sockaddr *dest;
 	size_t addrlen;
+	int rc;
 
 	switch (error) {
@@ -311,6 +316,7 @@
 
 		length = (size_t) result;
-		if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
-			return tcp_release_and_return(packet, ERROR_CODE);
+		rc = packet_trim(packet, length, 0);
+		if (rc != EOK)
+			return tcp_release_and_return(packet, rc);
 		break;
 	default:
@@ -333,6 +339,7 @@
 
 	// trim all but TCP header
-	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
-		return tcp_release_and_return(packet, ERROR_CODE);
+	rc = packet_trim(packet, offset, 0);
+	if (rc != EOK)
+		return tcp_release_and_return(packet, rc);
 
 	// get tcp header
@@ -350,7 +357,7 @@
 	addrlen = (size_t) result;
 
-	if (ERROR_OCCURRED(tl_set_address_port(src, addrlen,
-	    ntohs(header->source_port)))) 
-		return tcp_release_and_return(packet, ERROR_CODE);
+	rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
+	if (rc != EOK)
+		return tcp_release_and_return(packet, rc);
 	
 	// find the destination socket
@@ -413,16 +420,19 @@
 		}
 
-		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src,
-		    addrlen, dest, addrlen, total_length,
-		    &socket_data->pseudo_header, &socket_data->headerlen))) {
+		rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen,
+		    dest, addrlen, total_length, &socket_data->pseudo_header,
+		    &socket_data->headerlen);
+		if (rc != EOK) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
-			return tcp_release_and_return(packet, ERROR_CODE);
-		}
-
-	} else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
-	    socket_data->pseudo_header, socket_data->headerlen,
-	    total_length))) {
-		fibril_rwlock_write_unlock(socket_data->local_lock);
-		return tcp_release_and_return(packet, ERROR_CODE);
+			return tcp_release_and_return(packet, rc);
+		}
+	} else {
+		rc = ip_client_set_pseudo_header_data_length(
+		    socket_data->pseudo_header, socket_data->headerlen,
+		    total_length);
+		if (rc != EOK) {
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+			return tcp_release_and_return(packet, rc);
+		}
 	}
 	
@@ -434,6 +444,7 @@
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 
-		if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone,
-		    tcp_globals.icmp_phone, packet, error))) {
+		rc = tl_prepare_icmp_packet(tcp_globals.net_phone,
+		    tcp_globals.icmp_phone, packet, error);
+		if (rc == EOK) {
 			// checksum error ICMP
 			icmp_parameter_problem_msg(tcp_globals.icmp_phone,
@@ -452,14 +463,13 @@
 	switch (socket_data->state) {
 	case TCP_SOCKET_LISTEN:
-		ERROR_CODE = tcp_process_listen(socket, socket_data, header,
-		    packet, src, dest, addrlen);
+		rc = tcp_process_listen(socket, socket_data, header, packet,
+		    src, dest, addrlen);
 		break;
 	case TCP_SOCKET_SYN_RECEIVED:
-		ERROR_CODE = tcp_process_syn_received(socket, socket_data,
-		    header, packet);
+		rc = tcp_process_syn_received(socket, socket_data, header,
+		    packet);
 		break;
 	case TCP_SOCKET_SYN_SENT:
-		ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header,
-		    packet);
+		rc = tcp_process_syn_sent(socket, socket_data, header, packet);
 		break;
 	case TCP_SOCKET_FIN_WAIT_1:
@@ -472,6 +482,6 @@
 		// ack releasing the socket gets processed later
 	case TCP_SOCKET_ESTABLISHED:
-		ERROR_CODE = tcp_process_established(socket, socket_data,
-		    header, packet, fragments, total_length);
+		rc = tcp_process_established(socket, socket_data, header,
+		    packet, fragments, total_length);
 		break;
 	default:
@@ -479,7 +489,7 @@
 	}
 
-	if (ERROR_CODE != EOK) {
-		printf("process %d\n", ERROR_CODE);
+	if (rc != EOK) {
 		fibril_rwlock_write_unlock(socket_data->local_lock);
+		printf("process %d\n", rc);
 	}
 
@@ -491,6 +501,4 @@
     tcp_header_ref header, packet_t packet, int fragments, size_t total_length)
 {
-	ERROR_DECLARE;
-
 	packet_t next_packet;
 	packet_t tmp_packet;
@@ -501,4 +509,5 @@
 	size_t offset;
 	uint32_t new_sequence_number;
+	int rc;
 
 	assert(socket);
@@ -541,7 +550,9 @@
 		}
 
-		if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet,
-		    offset, 0))))
-			return tcp_release_and_return(packet, ERROR_CODE);
+		if (offset > 0) {
+			rc = packet_trim(packet, offset, 0);
+			if (rc != EOK)
+				return tcp_release_and_return(packet, rc);
+		}
 
 		assert(new_sequence_number == socket_data->next_incoming);
@@ -575,8 +586,11 @@
 			if (length <= offset)
 				next_packet = pq_next(next_packet);
-			else if (ERROR_OCCURRED(packet_trim(next_packet, 0,
-			    length - offset)))
-				return tcp_release_and_return(packet,
-				    ERROR_CODE);
+			else {
+				rc = packet_trim(next_packet, 0,
+				    length - offset));
+				if (rc != EOK)
+					return tcp_release_and_return(packet,
+					    rc);
+			}
 			offset -= length;
 			total_length -= length - offset;
@@ -603,11 +617,13 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if (ERROR_OCCURRED(packet_trim(packet,
-		    TCP_HEADER_LENGTH(header), 0)))
-			return tcp_release_and_return(packet, ERROR_CODE);
+		rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
+		if (rc != EOK)
+			return tcp_release_and_return(packet, rc);
 
 		if (total_length) {
-			ERROR_PROPAGATE(tcp_queue_received_packet(socket,
-			    socket_data, packet, fragments, total_length));
+			rc = tcp_queue_received_packet(socket, socket_data,
+			    packet, fragments, total_length);
+			if (rc != EOK)
+				return rc;
 		} else {
 			total_length = 1;
@@ -617,7 +633,6 @@
 		packet = socket_data->incoming;
 		while (packet) {
-
-			if (ERROR_OCCURRED(pq_get_order(socket_data->incoming,
-			    &order, NULL))) {
+			rc = pq_get_order(socket_data->incoming, &order, NULL);
+			if (rc != EOK) {
 				// remove the corrupted packet
 				next_packet = pq_detach(packet);
@@ -656,8 +671,9 @@
 				    socket_data->next_incoming) {
 					// queue received data
-					ERROR_PROPAGATE(
-					    tcp_queue_received_packet(socket,
+					rc = tcp_queue_received_packet(socket,
 					    socket_data, packet, 1,
-					    packet_get_data_length(packet)));
+					    packet_get_data_length(packet));
+					if (rc != EOK)
+						return rc;
 					socket_data->next_incoming =
 					    new_sequence_number;
@@ -677,12 +693,13 @@
 						    new_sequence_number;
 					}
-					if (ERROR_NONE(packet_trim(packet,
-					    length, 0))) {
+					rc = packet_trim(packet,length, 0);
+					if (rc == EOK) {
 						// queue received data
-						ERROR_PROPAGATE(
-						    tcp_queue_received_packet(
+						rc = tcp_queue_received_packet(
 						    socket, socket_data, packet,
 						    1, packet_get_data_length(
-						    packet)));
+						    packet));
+						if (rc != EOK)
+							return rc;
 						socket_data->next_incoming =
 						    new_sequence_number;
@@ -709,12 +726,13 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if (ERROR_OCCURRED(packet_trim(packet,
-		    TCP_HEADER_LENGTH(header), 0)))
-			return tcp_release_and_return(packet, ERROR_CODE);
+		rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
+		if (rc != EOK)
+			return tcp_release_and_return(packet, rc);
 
 		next_packet = pq_detach(packet);
 		length = packet_get_data_length(packet);
-		if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet,
-		    new_sequence_number, length))) {
+		rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
+		    length);
+		if (rc != EOK) {
 			// remove the corrupted packets
 			pq_release_remote(tcp_globals.net_phone,
@@ -727,8 +745,13 @@
 				tmp_packet = pq_detach(next_packet);
 				length = packet_get_data_length(next_packet);
-				if (ERROR_OCCURRED(pq_set_order(next_packet,
-				    new_sequence_number, length)) ||
-				    ERROR_OCCURRED(pq_insert_after(packet,
-				    next_packet))) {
+
+				rc = pq_set_order(next_packet,
+				    new_sequence_number, length);
+				if (rc != EOK) {
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(next_packet));
+				}
+				rc = pq_insert_after(packet, next_packet);
+				if (rc != EOK) {
 					pq_release_remote(tcp_globals.net_phone,
 					    packet_get_id(next_packet));
@@ -762,8 +785,11 @@
 	if (!packet) {
 		// create the notification packet
-		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
-		    socket_data, 0, 0));
-		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data,
-		    packet, 1));
+		rc = tcp_create_notification_packet(&packet, socket,
+		    socket_data, 0, 0);
+		if (rc != EOK)
+			return rc;
+		rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1);
+		if (rc != EOK)
+			return rc;
 		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
 		    socket_data->last_outgoing + 1);
@@ -783,7 +809,6 @@
     size_t total_length)
 {
-	ERROR_DECLARE;
-
 	packet_dimension_ref packet_dimension;
+	int rc;
 
 	assert(socket);
@@ -795,11 +820,12 @@
 
 	// queue the received packet
-	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
-	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
-	    ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	    &tcp_globals.dimensions, socket_data->device_id,
-	    &packet_dimension))) {
-		return tcp_release_and_return(packet, ERROR_CODE);
-	}
+	rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
+	    SOCKET_MAX_RECEIVED_SIZE);
+	if (rc != EOK)
+		return tcp_release_and_return(packet, rc);
+	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
+	if (rc != EOK)
+		return tcp_release_and_return(packet, rc);
 
 	// decrease the window size
@@ -820,7 +846,6 @@
     tcp_header_ref header, packet_t packet)
 {
-	ERROR_DECLARE;
-
 	packet_t next_packet;
+	int rc;
 
 	assert(socket);
@@ -844,8 +869,9 @@
 	}
 	// trim if longer than the header
-	if ((packet_get_data_length(packet) > sizeof(*header)) &&
-	    ERROR_OCCURRED(packet_trim(packet, 0,
-	    packet_get_data_length(packet) - sizeof(*header)))) {
-		return tcp_release_and_return(packet, ERROR_CODE);
+	if (packet_get_data_length(packet) > sizeof(*header)) {
+		rc = packet_trim(packet, 0,
+		    packet_get_data_length(packet) - sizeof(*header));
+		if (rc != EOK)
+			return tcp_release_and_return(packet, rc);
 	}
 	tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
@@ -876,6 +902,4 @@
     size_t addrlen)
 {
-	ERROR_DECLARE;
-
 	packet_t next_packet;
 	socket_core_ref socket;
@@ -884,4 +908,5 @@
 	int listening_socket_id = listening_socket->socket_id;
 	int listening_port = listening_socket->port;
+	int rc;
 
 	assert(listening_socket);
@@ -913,19 +938,20 @@
 	memcpy(socket_data->addr, src, socket_data->addrlen);
 	socket_data->dest_port = ntohs(header->source_port);
-	if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr,
-	    socket_data->addrlen, socket_data->dest_port))) {
+	rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
+	    socket_data->dest_port);
+	if (rc != EOK) {
 		free(socket_data->addr);
 		free(socket_data);
-		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-		return ERROR_CODE;
+		return tcp_release_and_return(packet, rc);
 	}
 
 	// create a socket
 	socket_id = -1;
-	if (ERROR_OCCURRED(socket_create(socket_data->local_sockets,
-	    listening_socket->phone, socket_data, &socket_id))) {
+	rc = socket_create(socket_data->local_sockets, listening_socket->phone,
+	    socket_data, &socket_id);
+	if (rc != EOK) {
 		free(socket_data->addr);
 		free(socket_data);
-		return tcp_release_and_return(packet, ERROR_CODE);
+		return tcp_release_and_return(packet, rc);
 	}
 
@@ -964,19 +990,19 @@
 	assert(socket_data);
 
-	ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port,
-	    socket, (const char *) socket_data->addr, socket_data->addrlen);
+	rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
+	    (const char *) socket_data->addr, socket_data->addrlen);
 	assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
 	    (const char *) socket_data->addr, socket_data->addrlen));
 
-//	ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket,
+//	rc = socket_bind_free_port(&tcp_globals.sockets, socket,
 //	    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
 //	    tcp_globals.last_used_port);
 //	tcp_globals.last_used_port = socket->port;
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-	if (ERROR_CODE != EOK) {
+	if (rc != EOK) {
 		socket_destroy(tcp_globals.net_phone, socket->socket_id,
 		    socket_data->local_sockets, &tcp_globals.sockets,
 		    tcp_free_socket_data);
-		return tcp_release_and_return(packet, ERROR_CODE);
+		return tcp_release_and_return(packet, rc);
 	}
 
@@ -992,20 +1018,23 @@
 
 	// trim if longer than the header
-	if ((packet_get_data_length(packet) > sizeof(*header)) &&
-	    ERROR_OCCURRED(packet_trim(packet, 0,
-	    packet_get_data_length(packet) - sizeof(*header)))) {
+	if (packet_get_data_length(packet) > sizeof(*header)) {
+		rc = packet_trim(packet, 0,
+		    packet_get_data_length(packet) - sizeof(*header));
+		if (rc != EOK) {
+			socket_destroy(tcp_globals.net_phone, socket->socket_id,
+			    socket_data->local_sockets, &tcp_globals.sockets,
+			    tcp_free_socket_data);
+			return tcp_release_and_return(packet, rc);
+		}
+	}
+
+	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
+
+	rc = tcp_queue_packet(socket, socket_data, packet, 1);
+	if (rc != EOK) {
 		socket_destroy(tcp_globals.net_phone, socket->socket_id,
 		    socket_data->local_sockets, &tcp_globals.sockets,
 		    tcp_free_socket_data);
-		return tcp_release_and_return(packet, ERROR_CODE);
-	}
-
-	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
-
-	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) {
-		socket_destroy(tcp_globals.net_phone, socket->socket_id,
-		    socket_data->local_sockets, &tcp_globals.sockets,
-		    tcp_free_socket_data);
-		return ERROR_CODE;
+		return rc;
 	}
 
@@ -1031,8 +1060,7 @@
     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref listening_socket;
 	tcp_socket_data_ref listening_socket_data;
+	int rc;
 
 	assert(socket);
@@ -1059,8 +1087,7 @@
 
 		// queue the received packet
-		if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted,
-		    (-1 * socket->socket_id),
-		    listening_socket_data->backlog))) {
-
+		rc = dyn_fifo_push(&listening_socket->accepted,
+		    (-1 * socket->socket_id), listening_socket_data->backlog);
+		if (rc == EOK) {
 			// notify the destination socket
 			async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
@@ -1077,9 +1104,12 @@
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
-	    socket_data, 0, 1));
+	rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
+	if (rc != EOK)
+		return rc;
 
 	// send the packet
-	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
+	rc = tcp_queue_packet(socket, socket_data, packet, 1);
+	if (rc != EOK)
+		return rc;
 
 	// flush packets
@@ -1198,7 +1228,6 @@
     ipc_call_t *answer, int *answer_count)
 {
-	ERROR_DECLARE;
-
 	packet_t packet;
+	int rc;
 
 	assert(call);
@@ -1209,13 +1238,15 @@
 	switch (IPC_GET_METHOD(*call)) {
 	case NET_TL_RECEIVED:
-		//fibril_rwlock_read_lock(&tcp_globals.lock);
-		if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone,
-		    &packet, IPC_GET_PACKET(call)))) {
-			ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call),
-			    packet, SERVICE_TCP, IPC_GET_ERROR(call));
-		}
-		//fibril_rwlock_read_unlock(&tcp_globals.lock);
-		return ERROR_CODE;
-
+//		fibril_rwlock_read_lock(&tcp_globals.lock);
+		rc = packet_translate_remote(tcp_globals.net_phone, &packet,
+		    IPC_GET_PACKET(call));
+		if (rc != EOK) {
+//			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			return rc;
+		}
+		rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,
+		    IPC_GET_ERROR(call));
+//		fibril_rwlock_read_unlock(&tcp_globals.lock);
+		return rc;
 	case IPC_M_CONNECT_TO_ME:
 		return tcp_process_client_messages(callid, *call);
@@ -1662,7 +1693,6 @@
     struct sockaddr *addr, socklen_t addrlen)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref socket;
+	int rc;
 
 	assert(local_sockets);
@@ -1675,6 +1705,6 @@
 		return ENOTSOCK;
 	
-	if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr,
-	    addrlen))) {
+	rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
+	if (rc != EOK) {
 		tcp_free_socket_data(socket);
 		// unbind if bound
@@ -1685,5 +1715,5 @@
 		}
 	}
-	return ERROR_CODE;
+	return rc;
 }
 
@@ -1692,8 +1722,7 @@
     struct sockaddr *addr, socklen_t addrlen)
 {
-	ERROR_DECLARE;
-
 	tcp_socket_data_ref socket_data;
 	packet_t packet;
+	int rc;
 
 	assert(socket);
@@ -1711,22 +1740,29 @@
 
 	// get the destination port
-	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen,
-	    &socket_data->dest_port));
+	rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
+	if (rc != EOK)
+		return rc;
+	
 	if (socket->port <= 0) {
 		// try to find a free port
-		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets,
-		    socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
-		    tcp_globals.last_used_port));
+		rc = socket_bind_free_port(&tcp_globals.sockets, socket,
+		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    tcp_globals.last_used_port);
+		if (rc != EOK)
+			return rc;
 		// set the next port as the search starting port number
 		tcp_globals.last_used_port = socket->port;
 	}
 
-	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
+	rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
 	    addr, addrlen, &socket_data->device_id,
-	    &socket_data->pseudo_header, &socket_data->headerlen));
+	    &socket_data->pseudo_header, &socket_data->headerlen);
+	if (rc != EOK)
+		return rc;
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
-	    socket_data, 1, 0));
+	rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
+	if (rc != EOK)
+		return rc;
 
 	// unlock the globals and wait for an operation
@@ -1736,7 +1772,9 @@
 	socket_data->addrlen = addrlen;
 	// send the packet
-	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) ||
-	    ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data,
-	    0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) {
+
+	if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
+	    ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
+	    TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
+	    EOK)) {
 		socket_data->addr = NULL;
 		socket_data->addrlen = 0;
@@ -1754,6 +1792,6 @@
 			fibril_condvar_wait(&socket_data->operation.condvar,
 			    &socket_data->operation.mutex);
-			ERROR_CODE = socket_data->operation.result;
-			if (ERROR_CODE != EOK) {
+			rc = socket_data->operation.result;
+			if (rc != EOK) {
 				socket_data->addr = NULL;
 				socket_data->addrlen = 0;
@@ -1762,5 +1800,5 @@
 			socket_data->addr = NULL;
 			socket_data->addrlen = 0;
-			ERROR_CODE = EINTR;
+			rc = EINTR;
 		}
 	}
@@ -1769,5 +1807,5 @@
 
 	// return the result
-	return ERROR_CODE;
+	return rc;
 }
 
@@ -1776,7 +1814,6 @@
     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
 {
-	ERROR_DECLARE;
-
 	tcp_header_ref header;
+	int rc;
 
 	assert(socket);
@@ -1793,6 +1830,7 @@
 	header->sequence_number = htonl(socket_data->next_outgoing);
 
-	if (ERROR_OCCURRED(packet_set_addr(packet, NULL,
-	    (uint8_t *) socket_data->addr, socket_data->addrlen)))
+	rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
+	    socket_data->addrlen);
+	if (rc != EOK)
 		return tcp_release_and_return(packet, EINVAL);
 
@@ -1808,5 +1846,5 @@
     packet_t packet, size_t data_length)
 {
-	ERROR_DECLARE;
+	int rc;
 
 	assert(socket);
@@ -1814,10 +1852,12 @@
 	assert(socket->specific_data == socket_data);
 
-	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet,
-	    data_length));
-
-	if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet,
-	    socket_data->next_outgoing, data_length)))
-		return tcp_release_and_return(packet, ERROR_CODE);
+	rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
+	if (rc != EOK)
+		return rc;
+
+	rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
+	    data_length);
+	if (rc != EOK)
+		return tcp_release_and_return(packet, rc);
 
 	socket_data->next_outgoing += data_length;
@@ -1828,6 +1868,4 @@
 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
 {
-	ERROR_DECLARE;
-
 	packet_t packet;
 	packet_t copy;
@@ -1835,4 +1873,5 @@
 	packet_t previous = NULL;
 	size_t data_length;
+	int rc;
 
 	assert(socket);
@@ -1859,8 +1898,11 @@
 		if (!sending) {
 			sending = copy;
-		} else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) {
-			pq_release_remote(tcp_globals.net_phone,
-			    packet_get_id(copy));
-			return sending;
+		} else {
+			rc = pq_insert_after(previous, copy);
+			if (rc != EOK) {
+				pq_release_remote(tcp_globals.net_phone,
+				    packet_get_id(copy));
+				return sending;
+			}
 		}
 
@@ -1884,8 +1926,7 @@
     packet_t packet, size_t data_length, size_t sequence_number)
 {
-	ERROR_DECLARE;
-
 	tcp_header_ref header;
 	uint32_t checksum;
+	int rc;
 
 	assert(socket);
@@ -1894,7 +1935,7 @@
 
 	// adjust the pseudo header
-	if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
-	    socket_data->pseudo_header, socket_data->headerlen,
-	    packet_get_data_length(packet)))) {
+	rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
+	    socket_data->headerlen, packet_get_data_length(packet));
+	if (rc != EOK) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1921,13 +1962,18 @@
 	checksum = compute_checksum(0, socket_data->pseudo_header,
 	    socket_data->headerlen);
-	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet),
+	checksum = compute_checksum(checksum,
+	    (uint8_t *) packet_get_data(packet),
 	    packet_get_data_length(packet));
 	header->checksum = htons(flip_checksum(compact_checksum(checksum)));
 
 	// prepare the packet
-	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0,
-	    0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket,
-	    socket_data, sequence_number, socket_data->state,
-	    socket_data->timeout, true))) {
+	rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
+	if (rc != EOK) {
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+		return NULL;
+	}
+	rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
+	    sequence_number, socket_data->state, socket_data->timeout, true);
+	if (rc != EOK) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -2038,6 +2084,4 @@
     size_t *addrlen)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -2045,4 +2089,5 @@
 	packet_t packet;
 	size_t length;
+	int rc;
 
 	assert(local_sockets);
@@ -2066,6 +2111,7 @@
 	// send the source address if desired
 	if (addrlen) {
-		ERROR_PROPAGATE(data_reply(socket_data->addr,
-		    socket_data->addrlen));
+		rc = data_reply(socket_data->addr, socket_data->addrlen);
+		if (rc != EOK)
+			return rc;
 		*addrlen = socket_data->addrlen;
 	}
@@ -2076,9 +2122,12 @@
 		return NO_DATA;
 
-	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet,
-	    packet_id));
+	rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
+	if (rc != EOK)
+		return rc;
 
 	// reply the packets
-	ERROR_PROPAGATE(socket_reply_packets(packet, &length));
+	rc = socket_reply_packets(packet, &length);
+	if (rc != EOK)
+		return rc;
 
 	// release the packet
@@ -2093,6 +2142,4 @@
     size_t *data_fragment_size, int flags)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -2103,4 +2150,5 @@
 	int index;
 	int result;
+	int rc;
 
 	assert(local_sockets);
@@ -2123,6 +2171,8 @@
 		return ENOTCONN;
 
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	   &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
+	if (rc != EOK)
+		return rc;
 
 	*data_fragment_size =
@@ -2145,6 +2195,7 @@
 
 		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet,
-		    0));
+		rc = tcp_queue_packet(socket, socket_data, packet, 0);
+		if (rc != EOK)
+			return rc;
 	}
 
@@ -2165,9 +2216,8 @@
 tcp_close_message(socket_cores_ref local_sockets, int socket_id)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_t packet;
+	int rc;
 
 	// find the socket
@@ -2194,11 +2244,12 @@
 	default:
 		// just destroy
-		if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id,
+		rc = socket_destroy(tcp_globals.net_phone, socket_id,
 		    local_sockets, &tcp_globals.sockets,
-		    tcp_free_socket_data))) {
+		    tcp_free_socket_data);
+		if (rc == EOK) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
 			fibril_rwlock_write_unlock(&tcp_globals.lock);
 		}
-		return ERROR_CODE;
+		return rc;
 	}
 
@@ -2207,9 +2258,12 @@
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
-	    socket_data, 0, 1));
+	rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
+	if (rc != EOK)
+		return rc;
 
 	// send the packet
-	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
+	rc = tcp_queue_packet(socket, socket_data, packet, 1);
+	if (rc != EOK)
+		return rc;
 
 	// flush packets
@@ -2230,15 +2284,15 @@
     tcp_socket_data_ref socket_data, int synchronize, int finalize)
 {
-	ERROR_DECLARE;
-
 	packet_dimension_ref packet_dimension;
 	tcp_header_ref header;
+	int rc;
 
 	assert(packet);
 
 	// get the device packet dimension
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	    &tcp_globals.dimensions, socket_data->device_id,
-	    &packet_dimension));
+	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
+	if (rc != EOK)
+		return rc;
 
 	// get a new packet
@@ -2265,10 +2319,9 @@
     int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
 {
-	ERROR_DECLARE;
-
 	socket_core_ref accepted;
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_dimension_ref packet_dimension;
+	int rc;
 
 	assert(local_sockets);
@@ -2304,9 +2357,13 @@
 		// TODO can it be in another state?
 		if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
-			ERROR_PROPAGATE(data_reply(socket_data->addr,
-			    socket_data->addrlen));
-			ERROR_PROPAGATE(tl_get_ip_packet_dimension(
-			    tcp_globals.ip_phone, &tcp_globals.dimensions,
-			    socket_data->device_id, &packet_dimension));
+			rc = data_reply(socket_data->addr,
+			    socket_data->addrlen);
+			if (rc != EOK)
+				return rc;
+			rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+			    &tcp_globals.dimensions, socket_data->device_id,
+			    &packet_dimension);
+			if (rc != EOK)
+				return rc;
 			*addrlen = socket_data->addrlen;
 
@@ -2318,7 +2375,8 @@
 	
 			if (new_socket_id > 0) {
-				ERROR_PROPAGATE(socket_cores_update(
-				    local_sockets, accepted->socket_id,
-				    new_socket_id));
+				rc = socket_cores_update(local_sockets,
+				    accepted->socket_id, new_socket_id);
+				if (rc != EOK)
+					return rc;
 				accepted->socket_id = new_socket_id;
 			}
@@ -2430,13 +2488,8 @@
 main(int argc, char *argv[])
 {
-	ERROR_DECLARE;
-
-	/*
-	   Start the module 
-	 */
-	if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
-		return ERROR_CODE;
-
-	return EOK;
+	int rc;
+
+	rc = tl_module_start_standalone(tl_client_connection);
+	return rc;
 }
 
Index: uspace/srv/net/tl/tcp/tcp_module.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp_module.c	(revision 376a7fdad232fa7f9d8ab2dd18778d36701ea658)
+++ uspace/srv/net/tl/tcp/tcp_module.c	(revision 0578271269331fb048f182ef5c3d26dae37c84ea)
@@ -44,5 +44,5 @@
 #include <async.h>
 #include <stdio.h>
-#include <err.h>
+#include <errno.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
@@ -61,21 +61,27 @@
 int tl_module_start_standalone(async_client_conn_t client_connection)
 {
-	ERROR_DECLARE;
-	
+	ipcarg_t phonehash;
+	int rc;
+
 	async_set_client_connection(client_connection);
 	tcp_globals.net_phone = net_connect_module();
-	ERROR_PROPAGATE(pm_init());
-	
-	ipcarg_t phonehash;
-	if (ERROR_OCCURRED(tcp_initialize(client_connection)) ||
-	    ERROR_OCCURRED(REGISTER_ME(SERVICE_TCP, &phonehash))) {
-		pm_destroy();
-		return ERROR_CODE;
-	}
+
+	rc = pm_init();
+	if (rc != EOK)
+		return rc;
+
+	rc = tcp_initialize(client_connection);
+	if (rc != EOK)
+		goto out;
+
+	rc = REGISTER_ME(SERVICE_TCP, &phonehash);
+	if (rc != EOK)
+		goto out;
 	
 	async_manager();
 	
+out:
 	pm_destroy();
-	return EOK;
+	return rc;
 }
 
