📄 iscsi_target.c
字号:
/* target/iscsi_target.c vi: set autoindent tabstop=4 shiftwidth=4 : This is the iscsi target front-end which interfaces with the STML. The front-end has been written to the specifications of Draft 20 of the iSCSI spec.*//* Copyright (C) 2001-2004 InterOperability Lab (IOL) University of New Hampshier (UNH) Durham, NH 03824 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. The name IOL and/or UNH may not be used to endorse or promote products derived from this software without specific prior written permission.*/#if !defined(MANGLE_INQUIRY_DATA)#define MANGLE_INQUIRY_DATA#endif#include "iscsi_target.h"#include "target_error_rec.h"#include <net/sock.h>#include <linux/netdevice.h>#include <linux/inetdevice.h>#include <net/if_inet6.h>#include <net/ipv6.h>#include <net/addrconf.h>/* added by RDR when rcu_read_lock(), rcu_read_ulock() came in with 2.6.10 * These macros define it the way it was in 2.4 and in earlier versions of 2.6 */#ifndef rcu_read_lock#define rcu_read_lock() read_lock(&inetdev_lock)#endif#ifndef rcu_read_unlock#define rcu_read_unlock() read_unlock(&inetdev_lock)#endifstatic void clean_bad_stuff(void);static int iscsi_release_connection(struct iscsi_conn *conn);static int handle_login(struct iscsi_conn *conn, __u8 *buffer);static int handle_text_request(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int handle_logout(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int handle_nopout(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int handle_cmnd(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int handle_task_mgt_command(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int handle_data(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int send_unsolicited_data(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static void check_queued_cmnd(struct iscsi_session *session);static void free_data_list(struct iscsi_cmnd *cmnd);static struct iscsi_cmnd * __attribute__ ((no_instrument_function))search_iscsi_cmnd(Target_Scsi_Cmnd * cmnd, struct iscsi_session **result_sess);static struct iscsi_cmnd *search_task_mgt_command(Target_Scsi_Message * message);static struct iscsi_cmnd * __attribute__ ((no_instrument_function))search_tags(struct iscsi_conn *conn, __u32 init_task_tag, __u32 target_xfer_tag, int dumpall);static int handle_discovery_rsp(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static int ask_for_more_text(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static int handle_logout_rsp(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static int handle_nopin(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static int generate_nopin(struct iscsi_conn *conn, struct iscsi_session *session);static int handle_iscsi_done(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static int handle_iscsi_mgt_fn_done(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static void iscsi_dequeue(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn);static int iscsi_tx_login_reject(struct iscsi_conn *conn, struct iscsi_init_login_cmnd *pdu, __u8 status_class, __u8 status_detail);static int iscsi_tx_r2t(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);static intdequeue_reject(struct iscsi_conn *conn, int sendit);/* Added for handling SNACK requests - SAI */static int handle_snack(struct iscsi_conn *conn, struct iscsi_session *session, __u8 *buffer);static int send_iscsi_response(struct iscsi_cmnd *cmnd, struct iscsi_conn *conn, struct iscsi_session *session);/* Daren Hayward, darenh@4bridgeworks.com */#if defined(MANGLE_INQUIRY_DATA)static intmangle_inquiry_data(struct iscsi_cmnd *iscsi_command, struct iovec *iov, int start_iov, int limit_iov);#endifstruct iscsi_global *devdata;/* * initialize stuff in devdata. */static intinit_target(struct iscsi_global *devdata){ memset(devdata, 0, sizeof(struct iscsi_global)); INIT_LIST_HEAD(&devdata->session_list); INIT_LIST_HEAD(&devdata->bad_session_list); init_MUTEX(&devdata->session_sem); init_MUTEX(&devdata->session_read_mutex); init_MUTEX_LOCKED(&devdata->server_sem); devdata->param_tbl = my_kmalloc(MAX_CONFIG_PARAMS * sizeof(struct parameter_type), "param_tbl"); if (!(devdata->param_tbl)) { return -1; } /* Copy the default parameters */ param_tbl_init(*devdata->param_tbl); /* chap and srp support - CHONG */ devdata->auth_parameter.chap_local_ctx = CHAP_InitializeContext(); devdata->auth_parameter.chap_peer_ctx = CHAP_InitializeContext(); devdata->auth_parameter.srp_ctx = SRP_InitializeContext(); return 0;}voidbring_down_portals( void ){ int i; struct portal_group *ptr; for (i = 0, ptr = iscsi_portal_groups; i < MAX_PORTAL; i++, ptr++) { if (ptr->in_use == 0) continue; if (devdata->server_thr[i]) { /* Mike Christie mikenc@us.ibm.com */ send_sig(ISCSI_SHUTDOWN_SIGNAL, devdata->server_thr[i], 1); down_interruptible(&devdata->server_sem); } if (devdata->server_socket[i]) { sock_release(devdata->server_socket[i]); devdata->server_socket[i] = NULL; } my_kfree((void **)&ptr->ip_address, "ip_address"); if (ptr->in_use == 2) { my_kfree((void **)&ptr->ip_string, "ip_string"); my_kfree((void **)&ptr->port_string, "port_string"); } ptr->in_use = 0; } if (devdata->param_tbl) { param_tbl_uncpy(*devdata->param_tbl); my_kfree((void**)&devdata->param_tbl, "param_tbl"); } /* chap and srp support - CHONG */ CHAP_FinalizeContext(devdata->auth_parameter.chap_local_ctx); CHAP_FinalizeContext(devdata->auth_parameter.chap_peer_ctx); SRP_FinalizeContext(devdata->auth_parameter.srp_ctx); my_kfree((void **)&devdata, "devdata"); /* check for any unfreed memory */ my_kempty();}int stop_server_thread (char *ip_string, char *port_string){ int i; for (i = 0; i < MAX_PORTAL; i++) { if (iscsi_portal_groups[i].in_use == 0) continue; if ((strcmp(iscsi_portal_groups[i].ip_string, ip_string) == 0) && (strcmp(iscsi_portal_groups[i].port_string, port_string) == 0)) { if (devdata->server_thr[i]) { /* Mike Christie mikenc@us.ibm.com */ send_sig(ISCSI_SHUTDOWN_SIGNAL, devdata->server_thr[i], 1); down_interruptible(&devdata->server_sem); } if (devdata->server_socket[i]) { sock_release(devdata->server_socket[i]); devdata->server_socket[i] = NULL; } my_kfree((void **)&iscsi_portal_groups[i].ip_address, "ip_address"); if (iscsi_portal_groups[i].in_use == 2) { my_kfree((void **)&iscsi_portal_groups[i].ip_string, "ip_string"); my_kfree((void **)&iscsi_portal_groups[i].port_string, "port_string"); } iscsi_portal_groups[i].in_use = 0; return 0; } } TRACE_ERROR("%s Can't find match with ip %s, port %s\n", current->comm, ip_string, port_string); return -1;}/* returns 0 on success, -1 on failure */intbring_up_portal( struct portal_group *ptr ){ int backlog = 5; struct socket *sockptr; TRACE(TRACE_DEBUG, "start_server_thread: ip_string %s, port_string %s\n", ptr->ip_string, ptr->port_string); ptr->family = cnv_string_to_inet( ptr->ip_string, ptr->port_string, &ptr->ip_address, &ptr->ip_length); if (ptr->family < 0) { goto out; } /* create a socket */#ifdef FC2 if (sock_create(ptr->family, SOCK_STREAM, 0, &sockptr, 0) < 0) {#else if (sock_create(ptr->family, SOCK_STREAM, 0, &sockptr) < 0) {#endif TRACE_ERROR("%s Could not create socket on %s:%s\n", current->comm, ptr->ip_string, ptr->port_string); goto out1; } TRACE(TRACE_NET, "Socket %p created\n", sockptr); /* bind */ if (sockptr->ops->bind(sockptr, ptr->ip_address, ptr->ip_length)) { TRACE_ERROR("%s Could not bind socket on %s:%s\n", current->comm, ptr->ip_string, ptr->port_string); goto out2; } TRACE(TRACE_NET, "Socket %p bound\n", sockptr); /* turn on the option to reuse this socket's port quickly */ tcp_reuse_port(sockptr); /* listen */ if ((sockptr->ops->listen(sockptr, backlog))) { TRACE_ERROR("%s Could not listen with socket on %s:%s\n", current->comm, ptr->ip_string, ptr->port_string); goto out2; } devdata->server_socket[ptr - iscsi_portal_groups] = sockptr; /* create a server_thread that can accept connections */ if (kernel_thread(iscsi_server_thread, (void *)ptr, 0) < 0) { TRACE_ERROR("%s Unable to create server thread\n", current->comm); devdata->server_socket[ptr - iscsi_portal_groups] = NULL; goto out2; } TRACE(TRACE_NET, "Server_thread spawned for socket %p\n", sockptr); /* wait for that server_thread to come up before continuing */ if (!down_interruptible(&devdata->server_sem)) return 0; out2: sock_release(sockptr);out1: my_kfree((void **)&ptr->ip_address, "ip_address");out: return -1;}int start_server_thread (char *ip_string, char *port_string, int tag){ int i; struct portal_group *ptr; if (!devdata) { TRACE_ERROR("%s No device available\n", current->comm); goto out; } for (i = 0, ptr = iscsi_portal_groups; i < MAX_PORTAL; i++, ptr++) { if (!ptr->in_use) break; } if (i >= MAX_PORTAL) { TRACE_ERROR("%s No more portals permitted\n", current->comm); goto out; } /* mark this portal in use with dynamic ip strings */ ptr->in_use = 2; ptr->ip_string = (char *)my_kmalloc(strlen(ip_string)+1, "ip_string"); if (ptr->ip_string == NULL) { goto out0; } strcpy(ptr->ip_string, ip_string); ptr->port_string = (char *)my_kmalloc(strlen(port_string)+1,"port_string"); if (ptr->port_string == NULL) { goto out1; } strcpy(ptr->port_string, port_string); ptr->tag = tag; if (bring_up_portal(ptr)) { goto out2; } return 0;out2: my_kfree((void **)&ptr->port_string, "port_string");out1: my_kfree((void **)&ptr->ip_string, "ip_string");out0: ptr->in_use = 0;out: return -1;}/* * iscsi_detect: this function sets up the server so that it can accept * multiple connections. */intiscsi_detect(Scsi_Target_Template * tmpt){ long i; struct portal_group *ptr; TRACE(TRACE_DEBUG, "Entering iSCSI detect\n"); devdata = (struct iscsi_global *)my_kmalloc(sizeof(struct iscsi_global), "devdata"); if (!devdata) { return -1; } /* Initialize devdata */ if (init_target(devdata) < 0) { TRACE_ERROR("%s Initialize devdata failed\n", current->comm); my_kfree((void **)&devdata, "devdata"); return -1; } /* setup the security key hash table */ setup_security_hash_table(); TRACE(TRACE_DEBUG, "iSCSI initialization completed\n"); for (i = 0, ptr = iscsi_portal_groups; i < MAX_PORTAL && ptr->tag != 0; i++, ptr++) { ptr->in_use = 1; if (bring_up_portal(ptr)) { ptr->in_use = 0; goto out; } } /* register the front end now */ devdata->device = register_target_front_end(tmpt); if (!devdata->device) { TRACE_ERROR("%s Device registration failed\n", current->comm); goto out; } devdata->device->dev_specific = (void *) devdata; TRACE(TRACE_DEBUG, "Registration complete\n"); return 1; /* one device detected */out: bring_down_portals(); return -2;}/* * iscsi_release: function to release the iSCSI device as required by * the Mid-Level */intiscsi_release(Scsi_Target_Device * device){ struct iscsi_session *session; struct list_head *list_ptr, *list_temp; int err = 0; TRACE(TRACE_ENTER_LEAVE, "Enter iscsi_release\n"); if ((struct iscsi_global *) device->dev_specific != devdata) { TRACE_ERROR("%s This is not an iscsi device\n", current->comm); err = -1; goto out; } /* destructive access to session lists */ if (!down_interruptible(&devdata->session_sem)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -