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