xref: /openbmc/linux/tools/net/ynl/generated/handshake-user.c (revision a33682e4e78e249155abbe5e8ee880d5760b5e28)
1 // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
2 /* Do not edit directly, auto-generated from: */
3 /*	Documentation/netlink/specs/handshake.yaml */
4 /* YNL-GEN user source */
5 
6 #include <stdlib.h>
7 #include "handshake-user.h"
8 #include "ynl.h"
9 #include <linux/handshake.h>
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <libmnl/libmnl.h>
15 #include <linux/genetlink.h>
16 
17 /* Enums */
18 static const char * const handshake_op_strmap[] = {
19 	[HANDSHAKE_CMD_READY] = "ready",
20 	[HANDSHAKE_CMD_ACCEPT] = "accept",
21 	[HANDSHAKE_CMD_DONE] = "done",
22 };
23 
24 const char *handshake_op_str(int op)
25 {
26 	if (op < 0 || op >= (int)MNL_ARRAY_SIZE(handshake_op_strmap))
27 		return NULL;
28 	return handshake_op_strmap[op];
29 }
30 
31 static const char * const handshake_handler_class_strmap[] = {
32 	[0] = "none",
33 	[1] = "tlshd",
34 	[2] = "max",
35 };
36 
37 const char *handshake_handler_class_str(enum handshake_handler_class value)
38 {
39 	if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_handler_class_strmap))
40 		return NULL;
41 	return handshake_handler_class_strmap[value];
42 }
43 
44 static const char * const handshake_msg_type_strmap[] = {
45 	[0] = "unspec",
46 	[1] = "clienthello",
47 	[2] = "serverhello",
48 };
49 
50 const char *handshake_msg_type_str(enum handshake_msg_type value)
51 {
52 	if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_msg_type_strmap))
53 		return NULL;
54 	return handshake_msg_type_strmap[value];
55 }
56 
57 static const char * const handshake_auth_strmap[] = {
58 	[0] = "unspec",
59 	[1] = "unauth",
60 	[2] = "psk",
61 	[3] = "x509",
62 };
63 
64 const char *handshake_auth_str(enum handshake_auth value)
65 {
66 	if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_auth_strmap))
67 		return NULL;
68 	return handshake_auth_strmap[value];
69 }
70 
71 /* Policies */
72 extern struct ynl_policy_nest handshake_x509_nest;
73 extern struct ynl_policy_nest handshake_accept_nest;
74 extern struct ynl_policy_nest handshake_done_nest;
75 
76 struct ynl_policy_attr handshake_x509_policy[HANDSHAKE_A_X509_MAX + 1] = {
77 	[HANDSHAKE_A_X509_CERT] = { .name = "cert", .type = YNL_PT_U32, },
78 	[HANDSHAKE_A_X509_PRIVKEY] = { .name = "privkey", .type = YNL_PT_U32, },
79 };
80 
81 struct ynl_policy_nest handshake_x509_nest = {
82 	.max_attr = HANDSHAKE_A_X509_MAX,
83 	.table = handshake_x509_policy,
84 };
85 
86 struct ynl_policy_attr handshake_accept_policy[HANDSHAKE_A_ACCEPT_MAX + 1] = {
87 	[HANDSHAKE_A_ACCEPT_SOCKFD] = { .name = "sockfd", .type = YNL_PT_U32, },
88 	[HANDSHAKE_A_ACCEPT_HANDLER_CLASS] = { .name = "handler-class", .type = YNL_PT_U32, },
89 	[HANDSHAKE_A_ACCEPT_MESSAGE_TYPE] = { .name = "message-type", .type = YNL_PT_U32, },
90 	[HANDSHAKE_A_ACCEPT_TIMEOUT] = { .name = "timeout", .type = YNL_PT_U32, },
91 	[HANDSHAKE_A_ACCEPT_AUTH_MODE] = { .name = "auth-mode", .type = YNL_PT_U32, },
92 	[HANDSHAKE_A_ACCEPT_PEER_IDENTITY] = { .name = "peer-identity", .type = YNL_PT_U32, },
93 	[HANDSHAKE_A_ACCEPT_CERTIFICATE] = { .name = "certificate", .type = YNL_PT_NEST, .nest = &handshake_x509_nest, },
94 	[HANDSHAKE_A_ACCEPT_PEERNAME] = { .name = "peername", .type = YNL_PT_NUL_STR, },
95 };
96 
97 struct ynl_policy_nest handshake_accept_nest = {
98 	.max_attr = HANDSHAKE_A_ACCEPT_MAX,
99 	.table = handshake_accept_policy,
100 };
101 
102 struct ynl_policy_attr handshake_done_policy[HANDSHAKE_A_DONE_MAX + 1] = {
103 	[HANDSHAKE_A_DONE_STATUS] = { .name = "status", .type = YNL_PT_U32, },
104 	[HANDSHAKE_A_DONE_SOCKFD] = { .name = "sockfd", .type = YNL_PT_U32, },
105 	[HANDSHAKE_A_DONE_REMOTE_AUTH] = { .name = "remote-auth", .type = YNL_PT_U32, },
106 };
107 
108 struct ynl_policy_nest handshake_done_nest = {
109 	.max_attr = HANDSHAKE_A_DONE_MAX,
110 	.table = handshake_done_policy,
111 };
112 
113 /* Common nested types */
114 void handshake_x509_free(struct handshake_x509 *obj)
115 {
116 }
117 
118 int handshake_x509_parse(struct ynl_parse_arg *yarg,
119 			 const struct nlattr *nested)
120 {
121 	struct handshake_x509 *dst = yarg->data;
122 	const struct nlattr *attr;
123 
124 	mnl_attr_for_each_nested(attr, nested) {
125 		if (mnl_attr_get_type(attr) == HANDSHAKE_A_X509_CERT) {
126 			if (ynl_attr_validate(yarg, attr))
127 				return MNL_CB_ERROR;
128 			dst->_present.cert = 1;
129 			dst->cert = mnl_attr_get_u32(attr);
130 		}
131 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_X509_PRIVKEY) {
132 			if (ynl_attr_validate(yarg, attr))
133 				return MNL_CB_ERROR;
134 			dst->_present.privkey = 1;
135 			dst->privkey = mnl_attr_get_u32(attr);
136 		}
137 	}
138 
139 	return 0;
140 }
141 
142 /* ============== HANDSHAKE_CMD_ACCEPT ============== */
143 /* HANDSHAKE_CMD_ACCEPT - do */
144 void handshake_accept_req_free(struct handshake_accept_req *req)
145 {
146 	free(req);
147 }
148 
149 void handshake_accept_rsp_free(struct handshake_accept_rsp *rsp)
150 {
151 	unsigned int i;
152 
153 	free(rsp->peer_identity);
154 	for (i = 0; i < rsp->n_certificate; i++)
155 		handshake_x509_free(&rsp->certificate[i]);
156 	free(rsp->certificate);
157 	free(rsp->peername);
158 	free(rsp);
159 }
160 
161 int handshake_accept_rsp_parse(const struct nlmsghdr *nlh, void *data)
162 {
163 	struct ynl_parse_arg *yarg = data;
164 	struct handshake_accept_rsp *dst;
165 	unsigned int n_peer_identity = 0;
166 	unsigned int n_certificate = 0;
167 	const struct nlattr *attr;
168 	struct ynl_parse_arg parg;
169 	int i;
170 
171 	dst = yarg->data;
172 	parg.ys = yarg->ys;
173 
174 	if (dst->certificate)
175 		return ynl_error_parse(yarg, "attribute already present (accept.certificate)");
176 	if (dst->peer_identity)
177 		return ynl_error_parse(yarg, "attribute already present (accept.peer-identity)");
178 
179 	mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
180 		if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_SOCKFD) {
181 			if (ynl_attr_validate(yarg, attr))
182 				return MNL_CB_ERROR;
183 			dst->_present.sockfd = 1;
184 			dst->sockfd = mnl_attr_get_u32(attr);
185 		}
186 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_MESSAGE_TYPE) {
187 			if (ynl_attr_validate(yarg, attr))
188 				return MNL_CB_ERROR;
189 			dst->_present.message_type = 1;
190 			dst->message_type = mnl_attr_get_u32(attr);
191 		}
192 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_TIMEOUT) {
193 			if (ynl_attr_validate(yarg, attr))
194 				return MNL_CB_ERROR;
195 			dst->_present.timeout = 1;
196 			dst->timeout = mnl_attr_get_u32(attr);
197 		}
198 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_AUTH_MODE) {
199 			if (ynl_attr_validate(yarg, attr))
200 				return MNL_CB_ERROR;
201 			dst->_present.auth_mode = 1;
202 			dst->auth_mode = mnl_attr_get_u32(attr);
203 		}
204 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEER_IDENTITY) {
205 			n_peer_identity++;
206 		}
207 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_CERTIFICATE) {
208 			n_certificate++;
209 		}
210 		else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEERNAME) {
211 			unsigned int len;
212 
213 			if (ynl_attr_validate(yarg, attr))
214 				return MNL_CB_ERROR;
215 
216 			len = strnlen(mnl_attr_get_str(attr), mnl_attr_get_payload_len(attr));
217 			dst->_present.peername_len = len;
218 			dst->peername = malloc(len + 1);
219 			memcpy(dst->peername, mnl_attr_get_str(attr), len);
220 			dst->peername[len] = 0;
221 		}
222 	}
223 
224 	if (n_certificate) {
225 		dst->certificate = calloc(n_certificate, sizeof(*dst->certificate));
226 		dst->n_certificate = n_certificate;
227 		i = 0;
228 		parg.rsp_policy = &handshake_x509_nest;
229 		mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
230 			if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_CERTIFICATE) {
231 				parg.data = &dst->certificate[i];
232 				if (handshake_x509_parse(&parg, attr))
233 					return MNL_CB_ERROR;
234 				i++;
235 			}
236 		}
237 	}
238 	if (n_peer_identity) {
239 		dst->peer_identity = calloc(n_peer_identity, sizeof(*dst->peer_identity));
240 		dst->n_peer_identity = n_peer_identity;
241 		i = 0;
242 		mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
243 			if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEER_IDENTITY) {
244 				dst->peer_identity[i] = mnl_attr_get_u32(attr);
245 				i++;
246 			}
247 		}
248 	}
249 
250 	return MNL_CB_OK;
251 }
252 
253 struct handshake_accept_rsp *
254 handshake_accept(struct ynl_sock *ys, struct handshake_accept_req *req)
255 {
256 	struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
257 	struct handshake_accept_rsp *rsp;
258 	struct nlmsghdr *nlh;
259 	int err;
260 
261 	nlh = ynl_gemsg_start_req(ys, ys->family_id, HANDSHAKE_CMD_ACCEPT, 1);
262 	ys->req_policy = &handshake_accept_nest;
263 	yrs.yarg.rsp_policy = &handshake_accept_nest;
264 
265 	if (req->_present.handler_class)
266 		mnl_attr_put_u32(nlh, HANDSHAKE_A_ACCEPT_HANDLER_CLASS, req->handler_class);
267 
268 	rsp = calloc(1, sizeof(*rsp));
269 	yrs.yarg.data = rsp;
270 	yrs.cb = handshake_accept_rsp_parse;
271 	yrs.rsp_cmd = HANDSHAKE_CMD_ACCEPT;
272 
273 	err = ynl_exec(ys, nlh, &yrs);
274 	if (err < 0)
275 		goto err_free;
276 
277 	return rsp;
278 
279 err_free:
280 	handshake_accept_rsp_free(rsp);
281 	return NULL;
282 }
283 
284 /* HANDSHAKE_CMD_ACCEPT - notify */
285 void handshake_accept_ntf_free(struct handshake_accept_ntf *rsp)
286 {
287 	unsigned int i;
288 
289 	free(rsp->obj.peer_identity);
290 	for (i = 0; i < rsp->obj.n_certificate; i++)
291 		handshake_x509_free(&rsp->obj.certificate[i]);
292 	free(rsp->obj.certificate);
293 	free(rsp->obj.peername);
294 	free(rsp);
295 }
296 
297 /* ============== HANDSHAKE_CMD_DONE ============== */
298 /* HANDSHAKE_CMD_DONE - do */
299 void handshake_done_req_free(struct handshake_done_req *req)
300 {
301 	free(req->remote_auth);
302 	free(req);
303 }
304 
305 int handshake_done(struct ynl_sock *ys, struct handshake_done_req *req)
306 {
307 	struct nlmsghdr *nlh;
308 	int err;
309 
310 	nlh = ynl_gemsg_start_req(ys, ys->family_id, HANDSHAKE_CMD_DONE, 1);
311 	ys->req_policy = &handshake_done_nest;
312 
313 	if (req->_present.status)
314 		mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_STATUS, req->status);
315 	if (req->_present.sockfd)
316 		mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_SOCKFD, req->sockfd);
317 	for (unsigned int i = 0; i < req->n_remote_auth; i++)
318 		mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_REMOTE_AUTH, req->remote_auth[i]);
319 
320 	err = ynl_exec(ys, nlh, NULL);
321 	if (err < 0)
322 		return -1;
323 
324 	return 0;
325 }
326 
327 /* --------------- Common notification parsing --------------- */
328 struct ynl_ntf_base_type *handshake_ntf_parse(struct ynl_sock *ys)
329 {
330 	struct ynl_parse_arg yarg = { .ys = ys, };
331 	struct ynl_ntf_base_type *rsp;
332 	struct genlmsghdr *genlh;
333 	struct nlmsghdr *nlh;
334 	mnl_cb_t parse;
335 	int len, err;
336 
337 	len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);
338 	if (len < (ssize_t)(sizeof(*nlh) + sizeof(*genlh)))
339 		return NULL;
340 
341 	nlh = (struct nlmsghdr *)ys->rx_buf;
342 	genlh = mnl_nlmsg_get_payload(nlh);
343 
344 	switch (genlh->cmd) {
345 	case HANDSHAKE_CMD_READY:
346 		rsp = calloc(1, sizeof(struct handshake_accept_ntf));
347 		parse = handshake_accept_rsp_parse;
348 		yarg.rsp_policy = &handshake_accept_nest;
349 		rsp->free = (void *)handshake_accept_ntf_free;
350 		break;
351 	default:
352 		ynl_error_unknown_notification(ys, genlh->cmd);
353 		return NULL;
354 	}
355 
356 	yarg.data = rsp->data;
357 
358 	err = mnl_cb_run2(ys->rx_buf, len, 0, 0, parse, &yarg,
359 			 ynl_cb_array, NLMSG_MIN_TYPE);
360 	if (err < 0)
361 		goto err_free;
362 
363 	rsp->family = nlh->nlmsg_type;
364 	rsp->cmd = genlh->cmd;
365 	return rsp;
366 
367 err_free:
368 	free(rsp);
369 	return NULL;
370 }
371 
372 static const struct ynl_ntf_info handshake_ntf_info[] =  {
373 	[HANDSHAKE_CMD_READY] =  {
374 		.alloc_sz	= sizeof(struct handshake_accept_ntf),
375 		.cb		= handshake_accept_rsp_parse,
376 		.policy		= &handshake_accept_nest,
377 		.free		= (void *)handshake_accept_ntf_free,
378 	},
379 };
380 
381 const struct ynl_family ynl_handshake_family =  {
382 	.name		= "handshake",
383 	.ntf_info	= handshake_ntf_info,
384 	.ntf_info_size	= MNL_ARRAY_SIZE(handshake_ntf_info),
385 };
386