1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Edward Tomasz Napierala under sponsorship 8 * from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34#ifndef ISCSID_H 35#define ISCSID_H 36 37#include <stdbool.h> 38#include <stdint.h> 39 40#include <iscsi_ioctl.h> 41 42#define DEFAULT_PIDFILE "/var/run/iscsid.pid" 43 44#define CONN_DIGEST_NONE 0 45#define CONN_DIGEST_CRC32C 1 46 47#define CONN_MUTUAL_CHALLENGE_LEN 1024 48#define SOCKBUF_SIZE 1048576 49 50struct connection { 51 int conn_iscsi_fd; 52 int conn_socket; 53 unsigned int conn_session_id; 54 struct iscsi_session_conf conn_conf; 55 struct iscsi_session_limits conn_limits; 56 char conn_target_alias[ISCSI_ADDR_LEN]; 57 uint8_t conn_isid[6]; 58 uint16_t conn_tsih; 59 uint32_t conn_statsn; 60 int conn_protocol_level; 61 int conn_header_digest; 62 int conn_data_digest; 63 bool conn_initial_r2t; 64 bool conn_immediate_data; 65 int conn_max_recv_data_segment_length; 66 int conn_max_send_data_segment_length; 67 int conn_max_burst_length; 68 int conn_first_burst_length; 69 struct chap *conn_mutual_chap; 70}; 71 72struct pdu { 73 struct connection *pdu_connection; 74 struct iscsi_bhs *pdu_bhs; 75 char *pdu_data; 76 size_t pdu_data_len; 77}; 78 79#define KEYS_MAX 1024 80 81struct keys { 82 char *keys_names[KEYS_MAX]; 83 char *keys_values[KEYS_MAX]; 84 char *keys_data; 85 size_t keys_data_len; 86}; 87 88#define CHAP_CHALLENGE_LEN 1024 89#define CHAP_DIGEST_LEN 16 /* Equal to MD5 digest size. */ 90 91struct chap { 92 unsigned char chap_id; 93 char chap_challenge[CHAP_CHALLENGE_LEN]; 94 char chap_response[CHAP_DIGEST_LEN]; 95}; 96 97struct rchap { 98 char *rchap_secret; 99 unsigned char rchap_id; 100 void *rchap_challenge; 101 size_t rchap_challenge_len; 102}; 103 104struct chap *chap_new(void); 105char *chap_get_id(const struct chap *chap); 106char *chap_get_challenge(const struct chap *chap); 107int chap_receive(struct chap *chap, const char *response); 108int chap_authenticate(struct chap *chap, 109 const char *secret); 110void chap_delete(struct chap *chap); 111 112struct rchap *rchap_new(const char *secret); 113int rchap_receive(struct rchap *rchap, 114 const char *id, const char *challenge); 115char *rchap_get_response(struct rchap *rchap); 116void rchap_delete(struct rchap *rchap); 117 118struct keys *keys_new(void); 119void keys_delete(struct keys *key); 120void keys_load(struct keys *keys, const struct pdu *pdu); 121void keys_save(struct keys *keys, struct pdu *pdu); 122const char *keys_find(struct keys *keys, const char *name); 123void keys_add(struct keys *keys, 124 const char *name, const char *value); 125void keys_add_int(struct keys *keys, 126 const char *name, int value); 127 128struct pdu *pdu_new(struct connection *ic); 129struct pdu *pdu_new_response(struct pdu *request); 130void pdu_receive(struct pdu *request); 131void pdu_send(struct pdu *response); 132void pdu_delete(struct pdu *ip); 133 134void login(struct connection *ic); 135 136void discovery(struct connection *ic); 137 138void log_init(int level); 139void log_set_peer_name(const char *name); 140void log_set_peer_addr(const char *addr); 141void log_err(int, const char *, ...) 142 __dead2 __printflike(2, 3); 143void log_errx(int, const char *, ...) 144 __dead2 __printflike(2, 3); 145void log_warn(const char *, ...) __printflike(1, 2); 146void log_warnx(const char *, ...) __printflike(1, 2); 147void log_debugx(const char *, ...) __printflike(1, 2); 148 149char *checked_strdup(const char *); 150bool timed_out(void); 151void fail(const struct connection *, const char *); 152 153#endif /* !ISCSID_H */ 154