1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
31__FBSDID("$FreeBSD: stable/11/usr.sbin/ctld/isns.c 332595 2018-04-16 16:14:05Z trasz $");
32
33#include <sys/types.h>
34#include <sys/time.h>
35#include <sys/socket.h>
36#include <sys/wait.h>
37#include <sys/endian.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <netdb.h>
41#include <stdbool.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "ctld.h"
47#include "isns.h"
48
49struct isns_req *
50isns_req_alloc(void)
51{
52	struct isns_req *req;
53
54	req = calloc(sizeof(struct isns_req), 1);
55	if (req == NULL) {
56		log_err(1, "calloc");
57		return (NULL);
58	}
59	req->ir_buflen = sizeof(struct isns_hdr);
60	req->ir_usedlen = 0;
61	req->ir_buf = calloc(req->ir_buflen, 1);
62	if (req->ir_buf == NULL) {
63		free(req);
64		log_err(1, "calloc");
65		return (NULL);
66	}
67	return (req);
68}
69
70struct isns_req *
71isns_req_create(uint16_t func, uint16_t flags)
72{
73	struct isns_req *req;
74	struct isns_hdr *hdr;
75
76	req = isns_req_alloc();
77	req->ir_usedlen = sizeof(struct isns_hdr);
78	hdr = (struct isns_hdr *)req->ir_buf;
79	be16enc(hdr->ih_version, ISNS_VERSION);
80	be16enc(hdr->ih_function, func);
81	be16enc(hdr->ih_flags, flags);
82	return (req);
83}
84
85void
86isns_req_free(struct isns_req *req)
87{
88
89	free(req->ir_buf);
90	free(req);
91}
92
93static int
94isns_req_getspace(struct isns_req *req, uint32_t len)
95{
96	void *newbuf;
97	int newlen;
98
99	if (req->ir_usedlen + len <= req->ir_buflen)
100		return (0);
101	newlen = 1 << flsl(req->ir_usedlen + len);
102	newbuf = realloc(req->ir_buf, newlen);
103	if (newbuf == NULL) {
104		log_err(1, "realloc");
105		return (1);
106	}
107	req->ir_buf = newbuf;
108	req->ir_buflen = newlen;
109	return (0);
110}
111
112void
113isns_req_add(struct isns_req *req, uint32_t tag, uint32_t len,
114    const void *value)
115{
116	struct isns_tlv *tlv;
117	uint32_t vlen;
118
119	vlen = len + ((len & 3) ? (4 - (len & 3)) : 0);
120	isns_req_getspace(req, sizeof(*tlv) + vlen);
121	tlv = (struct isns_tlv *)&req->ir_buf[req->ir_usedlen];
122	be32enc(tlv->it_tag, tag);
123	be32enc(tlv->it_length, vlen);
124	memcpy(tlv->it_value, value, len);
125	if (vlen != len)
126		memset(&tlv->it_value[len], 0, vlen - len);
127	req->ir_usedlen += sizeof(*tlv) + vlen;
128}
129
130void
131isns_req_add_delim(struct isns_req *req)
132{
133
134	isns_req_add(req, 0, 0, NULL);
135}
136
137void
138isns_req_add_str(struct isns_req *req, uint32_t tag, const char *value)
139{
140
141	isns_req_add(req, tag, strlen(value) + 1, value);
142}
143
144void
145isns_req_add_32(struct isns_req *req, uint32_t tag, uint32_t value)
146{
147	uint32_t beval;
148
149	be32enc(&beval, value);
150	isns_req_add(req, tag, sizeof(value), &beval);
151}
152
153void
154isns_req_add_addr(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
155{
156	struct sockaddr_in *in4;
157	struct sockaddr_in6 *in6;
158	uint8_t buf[16];
159
160	switch (ai->ai_addr->sa_family) {
161	case AF_INET:
162		in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
163		memset(buf, 0, 10);
164		buf[10] = 0xff;
165		buf[11] = 0xff;
166		memcpy(&buf[12], &in4->sin_addr, sizeof(in4->sin_addr));
167		isns_req_add(req, tag, sizeof(buf), buf);
168		break;
169	case AF_INET6:
170		in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
171		isns_req_add(req, tag, sizeof(in6->sin6_addr), &in6->sin6_addr);
172		break;
173	default:
174		log_errx(1, "Unsupported address family %d",
175		    ai->ai_addr->sa_family);
176	}
177}
178
179void
180isns_req_add_port(struct isns_req *req, uint32_t tag, struct addrinfo *ai)
181{
182	struct sockaddr_in *in4;
183	struct sockaddr_in6 *in6;
184	uint32_t buf;
185
186	switch (ai->ai_addr->sa_family) {
187	case AF_INET:
188		in4 = (struct sockaddr_in *)(void *)ai->ai_addr;
189		be32enc(&buf, ntohs(in4->sin_port));
190		isns_req_add(req, tag, sizeof(buf), &buf);
191		break;
192	case AF_INET6:
193		in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
194		be32enc(&buf, ntohs(in6->sin6_port));
195		isns_req_add(req, tag, sizeof(buf), &buf);
196		break;
197	default:
198		log_errx(1, "Unsupported address family %d",
199		    ai->ai_addr->sa_family);
200	}
201}
202
203int
204isns_req_send(int s, struct isns_req *req)
205{
206	struct isns_hdr *hdr;
207	int res;
208
209	hdr = (struct isns_hdr *)req->ir_buf;
210	be16enc(hdr->ih_length, req->ir_usedlen - sizeof(*hdr));
211	be16enc(hdr->ih_flags, be16dec(hdr->ih_flags) |
212	    ISNS_FLAG_LAST | ISNS_FLAG_FIRST);
213	be16enc(hdr->ih_transaction, 0);
214	be16enc(hdr->ih_sequence, 0);
215
216	res = write(s, req->ir_buf, req->ir_usedlen);
217	return ((res < 0) ? -1 : 0);
218}
219
220int
221isns_req_receive(int s, struct isns_req *req)
222{
223	struct isns_hdr *hdr;
224	ssize_t res, len;
225
226	req->ir_usedlen = 0;
227	isns_req_getspace(req, sizeof(*hdr));
228	res = read(s, req->ir_buf, sizeof(*hdr));
229	if (res < (ssize_t)sizeof(*hdr))
230		return (-1);
231	req->ir_usedlen = sizeof(*hdr);
232	hdr = (struct isns_hdr *)req->ir_buf;
233	if (be16dec(hdr->ih_version) != ISNS_VERSION)
234		return (-1);
235	if ((be16dec(hdr->ih_flags) & (ISNS_FLAG_LAST | ISNS_FLAG_FIRST)) !=
236	    (ISNS_FLAG_LAST | ISNS_FLAG_FIRST))
237		return (-1);
238	len = be16dec(hdr->ih_length);
239	isns_req_getspace(req, len);
240	res = read(s, &req->ir_buf[req->ir_usedlen], len);
241	if (res < len)
242		return (-1);
243	req->ir_usedlen += len;
244	return (0);
245}
246
247uint32_t
248isns_req_get_status(struct isns_req *req)
249{
250
251	if (req->ir_usedlen < sizeof(struct isns_hdr) + 4)
252		return (-1);
253	return (be32dec(&req->ir_buf[sizeof(struct isns_hdr)]));
254}
255