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