1/*-
2 * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: releng/11.0/usr.sbin/ctld/isns.c 281532 2015-04-14 18:13:55Z delphij $");
30
31#include <sys/types.h>
32#include <sys/time.h>
33#include <sys/socket.h>
34#include <sys/wait.h>
35#include <sys/endian.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <netdb.h>
39#include <stdbool.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include "ctld.h"
45#include "isns.h"
46
47struct isns_req *
48isns_req_alloc(void)
49{
50	struct isns_req *req;
51
52	req = calloc(sizeof(struct isns_req), 1);
53	if (req == NULL) {
54		log_err(1, "calloc");
55		return (NULL);
56	}
57	req->ir_buflen = sizeof(struct isns_hdr);
58	req->ir_usedlen = 0;
59	req->ir_buf = calloc(req->ir_buflen, 1);
60	if (req->ir_buf == NULL) {
61		free(req);
62		log_err(1, "calloc");
63		return (NULL);
64	}
65	return (req);
66}
67
68struct isns_req *
69isns_req_create(uint16_t func, uint16_t flags)
70{
71	struct isns_req *req;
72	struct isns_hdr *hdr;
73
74	req = isns_req_alloc();
75	req->ir_usedlen = sizeof(struct isns_hdr);
76	hdr = (struct isns_hdr *)req->ir_buf;
77	be16enc(hdr->ih_version, ISNS_VERSION);
78	be16enc(hdr->ih_function, func);
79	be16enc(hdr->ih_flags, flags);
80	return (req);
81}
82
83void
84isns_req_free(struct isns_req *req)
85{
86
87	free(req->ir_buf);
88	free(req);
89}
90
91static int
92isns_req_getspace(struct isns_req *req, uint32_t len)
93{
94	void *newbuf;
95	int newlen;
96
97	if (req->ir_usedlen + len <= req->ir_buflen)
98		return (0);
99	newlen = 1 << flsl(req->ir_usedlen + len);
100	newbuf = realloc(req->ir_buf, newlen);
101	if (newbuf == NULL) {
102		log_err(1, "realloc");
103		return (1);
104	}
105	req->ir_buf = newbuf;
106	req->ir_buflen = newlen;
107	return (0);
108}
109
110void
111isns_req_add(struct isns_req *req, uint32_t tag, uint32_t len,
112    const void *value)
113{
114	struct isns_tlv *tlv;
115	uint32_t vlen;
116
117	vlen = len + ((len & 3) ? (4 - (len & 3)) : 0);
118	isns_req_getspace(req, sizeof(*tlv) + vlen);
119	tlv = (struct isns_tlv *)&req->ir_buf[req->ir_usedlen];
120	be32enc(tlv->it_tag, tag);
121	be32enc(tlv->it_length, vlen);
122	memcpy(tlv->it_value, value, len);
123	if (vlen != len)
124		memset(&tlv->it_value[len], 0, vlen - len);
125	req->ir_usedlen += sizeof(*tlv) + vlen;
126}
127
128void
129isns_req_add_delim(struct isns_req *req)
130{
131
132	isns_req_add(req, 0, 0, NULL);
133}
134
135void
136isns_req_add_str(struct isns_req *req, uint32_t tag, const char *value)
137{
138
139	isns_req_add(req, tag, strlen(value) + 1, value);
140}
141
142void
143isns_req_add_32(struct isns_req *req, uint32_t tag, uint32_t value)
144{
145	uint32_t beval;
146
147	be32enc(&beval, value);
148	isns_req_add(req, tag, sizeof(value), &beval);
149}
150
151void
152isns_req_add_addr(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
153{
154	struct sockaddr_in *in4;
155	struct sockaddr_in6 *in6;
156	uint8_t buf[16];
157
158	switch (ai->ai_addr->sa_family) {
159	case AF_INET:
160		in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
161		memset(buf, 0, 10);
162		buf[10] = 0xff;
163		buf[11] = 0xff;
164		memcpy(&buf[12], &in4->sin_addr, sizeof(in4->sin_addr));
165		isns_req_add(req, tag, sizeof(buf), buf);
166		break;
167	case AF_INET6:
168		in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
169		isns_req_add(req, tag, sizeof(in6->sin6_addr), &in6->sin6_addr);
170		break;
171	default:
172		log_errx(1, "Unsupported address family %d",
173		    ai->ai_addr->sa_family);
174	}
175}
176
177void
178isns_req_add_port(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
179{
180	struct sockaddr_in *in4;
181	struct sockaddr_in6 *in6;
182	uint32_t buf;
183
184	switch (ai->ai_addr->sa_family) {
185	case AF_INET:
186		in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
187		be32enc(&buf, ntohs(in4->sin_port));
188		isns_req_add(req, tag, sizeof(buf), &buf);
189		break;
190	case AF_INET6:
191		in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
192		be32enc(&buf, ntohs(in6->sin6_port));
193		isns_req_add(req, tag, sizeof(buf), &buf);
194		break;
195	default:
196		log_errx(1, "Unsupported address family %d",
197		    ai->ai_addr->sa_family);
198	}
199}
200
201int
202isns_req_send(int s, struct isns_req *req)
203{
204	struct isns_hdr *hdr;
205	int res;
206
207	hdr = (struct isns_hdr *)req->ir_buf;
208	be16enc(hdr->ih_length, req->ir_usedlen - sizeof(*hdr));
209	be16enc(hdr->ih_flags, be16dec(hdr->ih_flags) |
210	    ISNS_FLAG_LAST | ISNS_FLAG_FIRST);
211	be16enc(hdr->ih_transaction, 0);
212	be16enc(hdr->ih_sequence, 0);
213
214	res = write(s, req->ir_buf, req->ir_usedlen);
215	return ((res < 0) ? -1 : 0);
216}
217
218int
219isns_req_receive(int s, struct isns_req *req)
220{
221	struct isns_hdr *hdr;
222	ssize_t res, len;
223
224	req->ir_usedlen = 0;
225	isns_req_getspace(req, sizeof(*hdr));
226	res = read(s, req->ir_buf, sizeof(*hdr));
227	if (res < (ssize_t)sizeof(*hdr))
228		return (-1);
229	req->ir_usedlen = sizeof(*hdr);
230	hdr = (struct isns_hdr *)req->ir_buf;
231	if (be16dec(hdr->ih_version) != ISNS_VERSION)
232		return (-1);
233	if ((be16dec(hdr->ih_flags) & (ISNS_FLAG_LAST | ISNS_FLAG_FIRST)) !=
234	    (ISNS_FLAG_LAST | ISNS_FLAG_FIRST))
235		return (-1);
236	len = be16dec(hdr->ih_length);
237	isns_req_getspace(req, len);
238	res = read(s, &req->ir_buf[req->ir_usedlen], len);
239	if (res < len)
240		return (-1);
241	req->ir_usedlen += len;
242	return (0);
243}
244
245uint32_t
246isns_req_get_status(struct isns_req *req)
247{
248
249	if (req->ir_usedlen < sizeof(struct isns_hdr) + 4)
250		return (-1);
251	return (be32dec(&req->ir_buf[sizeof(struct isns_hdr)]));
252}
253