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 */
8 #include "handshake-user.h"
10 #include <linux/handshake.h>
12 #include <libmnl/libmnl.h>
13 #include <linux/genetlink.h>
16 static const char * const handshake_op_strmap[] = {
17 [HANDSHAKE_CMD_READY] = "ready",
18 [HANDSHAKE_CMD_ACCEPT] = "accept",
19 [HANDSHAKE_CMD_DONE] = "done",
22 const char *handshake_op_str(int op)
24 if (op < 0 || op >= (int)MNL_ARRAY_SIZE(handshake_op_strmap))
26 return handshake_op_strmap[op];
29 static const char * const handshake_handler_class_strmap[] = {
35 const char *handshake_handler_class_str(enum handshake_handler_class value)
37 if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_handler_class_strmap))
39 return handshake_handler_class_strmap[value];
42 static const char * const handshake_msg_type_strmap[] = {
48 const char *handshake_msg_type_str(enum handshake_msg_type value)
50 if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_msg_type_strmap))
52 return handshake_msg_type_strmap[value];
55 static const char * const handshake_auth_strmap[] = {
62 const char *handshake_auth_str(enum handshake_auth value)
64 if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_auth_strmap))
66 return handshake_auth_strmap[value];
70 struct ynl_policy_attr handshake_x509_policy[HANDSHAKE_A_X509_MAX + 1] = {
71 [HANDSHAKE_A_X509_CERT] = { .name = "cert", .type = YNL_PT_U32, },
72 [HANDSHAKE_A_X509_PRIVKEY] = { .name = "privkey", .type = YNL_PT_U32, },
75 struct ynl_policy_nest handshake_x509_nest = {
76 .max_attr = HANDSHAKE_A_X509_MAX,
77 .table = handshake_x509_policy,
80 struct ynl_policy_attr handshake_accept_policy[HANDSHAKE_A_ACCEPT_MAX + 1] = {
81 [HANDSHAKE_A_ACCEPT_SOCKFD] = { .name = "sockfd", .type = YNL_PT_U32, },
82 [HANDSHAKE_A_ACCEPT_HANDLER_CLASS] = { .name = "handler-class", .type = YNL_PT_U32, },
83 [HANDSHAKE_A_ACCEPT_MESSAGE_TYPE] = { .name = "message-type", .type = YNL_PT_U32, },
84 [HANDSHAKE_A_ACCEPT_TIMEOUT] = { .name = "timeout", .type = YNL_PT_U32, },
85 [HANDSHAKE_A_ACCEPT_AUTH_MODE] = { .name = "auth-mode", .type = YNL_PT_U32, },
86 [HANDSHAKE_A_ACCEPT_PEER_IDENTITY] = { .name = "peer-identity", .type = YNL_PT_U32, },
87 [HANDSHAKE_A_ACCEPT_CERTIFICATE] = { .name = "certificate", .type = YNL_PT_NEST, .nest = &handshake_x509_nest, },
88 [HANDSHAKE_A_ACCEPT_PEERNAME] = { .name = "peername", .type = YNL_PT_NUL_STR, },
91 struct ynl_policy_nest handshake_accept_nest = {
92 .max_attr = HANDSHAKE_A_ACCEPT_MAX,
93 .table = handshake_accept_policy,
96 struct ynl_policy_attr handshake_done_policy[HANDSHAKE_A_DONE_MAX + 1] = {
97 [HANDSHAKE_A_DONE_STATUS] = { .name = "status", .type = YNL_PT_U32, },
98 [HANDSHAKE_A_DONE_SOCKFD] = { .name = "sockfd", .type = YNL_PT_U32, },
99 [HANDSHAKE_A_DONE_REMOTE_AUTH] = { .name = "remote-auth", .type = YNL_PT_U32, },
102 struct ynl_policy_nest handshake_done_nest = {
103 .max_attr = HANDSHAKE_A_DONE_MAX,
104 .table = handshake_done_policy,
107 /* Common nested types */
108 void handshake_x509_free(struct handshake_x509 *obj)
112 int handshake_x509_parse(struct ynl_parse_arg *yarg,
113 const struct nlattr *nested)
115 struct handshake_x509 *dst = yarg->data;
116 const struct nlattr *attr;
118 mnl_attr_for_each_nested(attr, nested) {
119 if (mnl_attr_get_type(attr) == HANDSHAKE_A_X509_CERT) {
120 if (ynl_attr_validate(yarg, attr))
122 dst->_present.cert = 1;
123 dst->cert = mnl_attr_get_u32(attr);
125 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_X509_PRIVKEY) {
126 if (ynl_attr_validate(yarg, attr))
128 dst->_present.privkey = 1;
129 dst->privkey = mnl_attr_get_u32(attr);
136 /* ============== HANDSHAKE_CMD_ACCEPT ============== */
137 /* HANDSHAKE_CMD_ACCEPT - do */
138 void handshake_accept_req_free(struct handshake_accept_req *req)
143 void handshake_accept_rsp_free(struct handshake_accept_rsp *rsp)
147 free(rsp->peer_identity);
148 for (i = 0; i < rsp->n_certificate; i++)
149 handshake_x509_free(&rsp->certificate[i]);
150 free(rsp->certificate);
155 int handshake_accept_rsp_parse(const struct nlmsghdr *nlh, void *data)
157 struct ynl_parse_arg *yarg = data;
158 struct handshake_accept_rsp *dst;
159 unsigned int n_peer_identity = 0;
160 unsigned int n_certificate = 0;
161 const struct nlattr *attr;
162 struct ynl_parse_arg parg;
168 if (dst->certificate)
169 return ynl_error_parse(yarg, "attribute already present (accept.certificate)");
170 if (dst->peer_identity)
171 return ynl_error_parse(yarg, "attribute already present (accept.peer-identity)");
173 mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
174 if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_SOCKFD) {
175 if (ynl_attr_validate(yarg, attr))
177 dst->_present.sockfd = 1;
178 dst->sockfd = mnl_attr_get_u32(attr);
180 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_MESSAGE_TYPE) {
181 if (ynl_attr_validate(yarg, attr))
183 dst->_present.message_type = 1;
184 dst->message_type = mnl_attr_get_u32(attr);
186 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_TIMEOUT) {
187 if (ynl_attr_validate(yarg, attr))
189 dst->_present.timeout = 1;
190 dst->timeout = mnl_attr_get_u32(attr);
192 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_AUTH_MODE) {
193 if (ynl_attr_validate(yarg, attr))
195 dst->_present.auth_mode = 1;
196 dst->auth_mode = mnl_attr_get_u32(attr);
198 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEER_IDENTITY) {
201 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_CERTIFICATE) {
204 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEERNAME) {
207 if (ynl_attr_validate(yarg, attr))
210 len = strnlen(mnl_attr_get_str(attr), mnl_attr_get_payload_len(attr));
211 dst->_present.peername_len = len;
212 dst->peername = malloc(len + 1);
213 memcpy(dst->peername, mnl_attr_get_str(attr), len);
214 dst->peername[len] = 0;
219 dst->certificate = calloc(n_certificate, sizeof(*dst->certificate));
220 dst->n_certificate = n_certificate;
222 parg.rsp_policy = &handshake_x509_nest;
223 mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
224 if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_CERTIFICATE) {
225 parg.data = &dst->certificate[i];
226 if (handshake_x509_parse(&parg, attr))
232 if (n_peer_identity) {
233 dst->peer_identity = calloc(n_peer_identity, sizeof(*dst->peer_identity));
234 dst->n_peer_identity = n_peer_identity;
236 mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
237 if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEER_IDENTITY) {
238 dst->peer_identity[i] = mnl_attr_get_u32(attr);
247 struct handshake_accept_rsp *
248 handshake_accept(struct ynl_sock *ys, struct handshake_accept_req *req)
250 struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
251 struct handshake_accept_rsp *rsp;
252 struct nlmsghdr *nlh;
255 nlh = ynl_gemsg_start_req(ys, ys->family_id, HANDSHAKE_CMD_ACCEPT, 1);
256 ys->req_policy = &handshake_accept_nest;
257 yrs.yarg.rsp_policy = &handshake_accept_nest;
259 if (req->_present.handler_class)
260 mnl_attr_put_u32(nlh, HANDSHAKE_A_ACCEPT_HANDLER_CLASS, req->handler_class);
262 rsp = calloc(1, sizeof(*rsp));
264 yrs.cb = handshake_accept_rsp_parse;
265 yrs.rsp_cmd = HANDSHAKE_CMD_ACCEPT;
267 err = ynl_exec(ys, nlh, &yrs);
274 handshake_accept_rsp_free(rsp);
278 /* HANDSHAKE_CMD_ACCEPT - notify */
279 void handshake_accept_ntf_free(struct handshake_accept_ntf *rsp)
283 free(rsp->obj.peer_identity);
284 for (i = 0; i < rsp->obj.n_certificate; i++)
285 handshake_x509_free(&rsp->obj.certificate[i]);
286 free(rsp->obj.certificate);
287 free(rsp->obj.peername);
291 /* ============== HANDSHAKE_CMD_DONE ============== */
292 /* HANDSHAKE_CMD_DONE - do */
293 void handshake_done_req_free(struct handshake_done_req *req)
295 free(req->remote_auth);
299 int handshake_done(struct ynl_sock *ys, struct handshake_done_req *req)
301 struct nlmsghdr *nlh;
304 nlh = ynl_gemsg_start_req(ys, ys->family_id, HANDSHAKE_CMD_DONE, 1);
305 ys->req_policy = &handshake_done_nest;
307 if (req->_present.status)
308 mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_STATUS, req->status);
309 if (req->_present.sockfd)
310 mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_SOCKFD, req->sockfd);
311 for (unsigned int i = 0; i < req->n_remote_auth; i++)
312 mnl_attr_put_u32(nlh, HANDSHAKE_A_DONE_REMOTE_AUTH, req->remote_auth[i]);
314 err = ynl_exec(ys, nlh, NULL);
321 /* --------------- Common notification parsing --------------- */
322 struct ynl_ntf_base_type *handshake_ntf_parse(struct ynl_sock *ys)
324 struct ynl_parse_arg yarg = { .ys = ys, };
325 struct ynl_ntf_base_type *rsp;
326 struct genlmsghdr *genlh;
327 struct nlmsghdr *nlh;
331 len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);
332 if (len < (ssize_t)(sizeof(*nlh) + sizeof(*genlh)))
335 nlh = (struct nlmsghdr *)ys->rx_buf;
336 genlh = mnl_nlmsg_get_payload(nlh);
338 switch (genlh->cmd) {
339 case HANDSHAKE_CMD_READY:
340 rsp = calloc(1, sizeof(struct handshake_accept_ntf));
341 parse = handshake_accept_rsp_parse;
342 yarg.rsp_policy = &handshake_accept_nest;
343 rsp->free = (void *)handshake_accept_ntf_free;
346 ynl_error_unknown_notification(ys, genlh->cmd);
350 yarg.data = rsp->data;
352 err = mnl_cb_run2(ys->rx_buf, len, 0, 0, parse, &yarg,
353 ynl_cb_array, NLMSG_MIN_TYPE);
357 rsp->family = nlh->nlmsg_type;
358 rsp->cmd = genlh->cmd;
366 static const struct ynl_ntf_info handshake_ntf_info[] = {
367 [HANDSHAKE_CMD_READY] = {
368 .alloc_sz = sizeof(struct handshake_accept_ntf),
369 .cb = handshake_accept_rsp_parse,
370 .policy = &handshake_accept_nest,
371 .free = (void *)handshake_accept_ntf_free,
375 const struct ynl_family ynl_handshake_family = {
377 .ntf_info = handshake_ntf_info,
378 .ntf_info_size = MNL_ARRAY_SIZE(handshake_ntf_info),