tools: ynl: regen: cleanup user space header includes
[platform/kernel/linux-rpi.git] / tools / net / ynl / generated / handshake-user.c
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 <string.h>
8 #include "handshake-user.h"
9 #include "ynl.h"
10 #include <linux/handshake.h>
11
12 #include <libmnl/libmnl.h>
13 #include <linux/genetlink.h>
14
15 /* Enums */
16 static const char * const handshake_op_strmap[] = {
17         [HANDSHAKE_CMD_READY] = "ready",
18         [HANDSHAKE_CMD_ACCEPT] = "accept",
19         [HANDSHAKE_CMD_DONE] = "done",
20 };
21
22 const char *handshake_op_str(int op)
23 {
24         if (op < 0 || op >= (int)MNL_ARRAY_SIZE(handshake_op_strmap))
25                 return NULL;
26         return handshake_op_strmap[op];
27 }
28
29 static const char * const handshake_handler_class_strmap[] = {
30         [0] = "none",
31         [1] = "tlshd",
32         [2] = "max",
33 };
34
35 const char *handshake_handler_class_str(enum handshake_handler_class value)
36 {
37         if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_handler_class_strmap))
38                 return NULL;
39         return handshake_handler_class_strmap[value];
40 }
41
42 static const char * const handshake_msg_type_strmap[] = {
43         [0] = "unspec",
44         [1] = "clienthello",
45         [2] = "serverhello",
46 };
47
48 const char *handshake_msg_type_str(enum handshake_msg_type value)
49 {
50         if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_msg_type_strmap))
51                 return NULL;
52         return handshake_msg_type_strmap[value];
53 }
54
55 static const char * const handshake_auth_strmap[] = {
56         [0] = "unspec",
57         [1] = "unauth",
58         [2] = "psk",
59         [3] = "x509",
60 };
61
62 const char *handshake_auth_str(enum handshake_auth value)
63 {
64         if (value < 0 || value >= (int)MNL_ARRAY_SIZE(handshake_auth_strmap))
65                 return NULL;
66         return handshake_auth_strmap[value];
67 }
68
69 /* Policies */
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, },
73 };
74
75 struct ynl_policy_nest handshake_x509_nest = {
76         .max_attr = HANDSHAKE_A_X509_MAX,
77         .table = handshake_x509_policy,
78 };
79
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, },
89 };
90
91 struct ynl_policy_nest handshake_accept_nest = {
92         .max_attr = HANDSHAKE_A_ACCEPT_MAX,
93         .table = handshake_accept_policy,
94 };
95
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, },
100 };
101
102 struct ynl_policy_nest handshake_done_nest = {
103         .max_attr = HANDSHAKE_A_DONE_MAX,
104         .table = handshake_done_policy,
105 };
106
107 /* Common nested types */
108 void handshake_x509_free(struct handshake_x509 *obj)
109 {
110 }
111
112 int handshake_x509_parse(struct ynl_parse_arg *yarg,
113                          const struct nlattr *nested)
114 {
115         struct handshake_x509 *dst = yarg->data;
116         const struct nlattr *attr;
117
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))
121                                 return MNL_CB_ERROR;
122                         dst->_present.cert = 1;
123                         dst->cert = mnl_attr_get_u32(attr);
124                 }
125                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_X509_PRIVKEY) {
126                         if (ynl_attr_validate(yarg, attr))
127                                 return MNL_CB_ERROR;
128                         dst->_present.privkey = 1;
129                         dst->privkey = mnl_attr_get_u32(attr);
130                 }
131         }
132
133         return 0;
134 }
135
136 /* ============== HANDSHAKE_CMD_ACCEPT ============== */
137 /* HANDSHAKE_CMD_ACCEPT - do */
138 void handshake_accept_req_free(struct handshake_accept_req *req)
139 {
140         free(req);
141 }
142
143 void handshake_accept_rsp_free(struct handshake_accept_rsp *rsp)
144 {
145         unsigned int i;
146
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);
151         free(rsp->peername);
152         free(rsp);
153 }
154
155 int handshake_accept_rsp_parse(const struct nlmsghdr *nlh, void *data)
156 {
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;
163         int i;
164
165         dst = yarg->data;
166         parg.ys = yarg->ys;
167
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)");
172
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))
176                                 return MNL_CB_ERROR;
177                         dst->_present.sockfd = 1;
178                         dst->sockfd = mnl_attr_get_u32(attr);
179                 }
180                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_MESSAGE_TYPE) {
181                         if (ynl_attr_validate(yarg, attr))
182                                 return MNL_CB_ERROR;
183                         dst->_present.message_type = 1;
184                         dst->message_type = mnl_attr_get_u32(attr);
185                 }
186                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_TIMEOUT) {
187                         if (ynl_attr_validate(yarg, attr))
188                                 return MNL_CB_ERROR;
189                         dst->_present.timeout = 1;
190                         dst->timeout = mnl_attr_get_u32(attr);
191                 }
192                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_AUTH_MODE) {
193                         if (ynl_attr_validate(yarg, attr))
194                                 return MNL_CB_ERROR;
195                         dst->_present.auth_mode = 1;
196                         dst->auth_mode = mnl_attr_get_u32(attr);
197                 }
198                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEER_IDENTITY) {
199                         n_peer_identity++;
200                 }
201                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_CERTIFICATE) {
202                         n_certificate++;
203                 }
204                 else if (mnl_attr_get_type(attr) == HANDSHAKE_A_ACCEPT_PEERNAME) {
205                         unsigned int len;
206
207                         if (ynl_attr_validate(yarg, attr))
208                                 return MNL_CB_ERROR;
209
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;
215                 }
216         }
217
218         if (n_certificate) {
219                 dst->certificate = calloc(n_certificate, sizeof(*dst->certificate));
220                 dst->n_certificate = n_certificate;
221                 i = 0;
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))
227                                         return MNL_CB_ERROR;
228                                 i++;
229                         }
230                 }
231         }
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;
235                 i = 0;
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);
239                                 i++;
240                         }
241                 }
242         }
243
244         return MNL_CB_OK;
245 }
246
247 struct handshake_accept_rsp *
248 handshake_accept(struct ynl_sock *ys, struct handshake_accept_req *req)
249 {
250         struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
251         struct handshake_accept_rsp *rsp;
252         struct nlmsghdr *nlh;
253         int err;
254
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;
258
259         if (req->_present.handler_class)
260                 mnl_attr_put_u32(nlh, HANDSHAKE_A_ACCEPT_HANDLER_CLASS, req->handler_class);
261
262         rsp = calloc(1, sizeof(*rsp));
263         yrs.yarg.data = rsp;
264         yrs.cb = handshake_accept_rsp_parse;
265         yrs.rsp_cmd = HANDSHAKE_CMD_ACCEPT;
266
267         err = ynl_exec(ys, nlh, &yrs);
268         if (err < 0)
269                 goto err_free;
270
271         return rsp;
272
273 err_free:
274         handshake_accept_rsp_free(rsp);
275         return NULL;
276 }
277
278 /* HANDSHAKE_CMD_ACCEPT - notify */
279 void handshake_accept_ntf_free(struct handshake_accept_ntf *rsp)
280 {
281         unsigned int i;
282
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);
288         free(rsp);
289 }
290
291 /* ============== HANDSHAKE_CMD_DONE ============== */
292 /* HANDSHAKE_CMD_DONE - do */
293 void handshake_done_req_free(struct handshake_done_req *req)
294 {
295         free(req->remote_auth);
296         free(req);
297 }
298
299 int handshake_done(struct ynl_sock *ys, struct handshake_done_req *req)
300 {
301         struct nlmsghdr *nlh;
302         int err;
303
304         nlh = ynl_gemsg_start_req(ys, ys->family_id, HANDSHAKE_CMD_DONE, 1);
305         ys->req_policy = &handshake_done_nest;
306
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]);
313
314         err = ynl_exec(ys, nlh, NULL);
315         if (err < 0)
316                 return -1;
317
318         return 0;
319 }
320
321 /* --------------- Common notification parsing --------------- */
322 struct ynl_ntf_base_type *handshake_ntf_parse(struct ynl_sock *ys)
323 {
324         struct ynl_parse_arg yarg = { .ys = ys, };
325         struct ynl_ntf_base_type *rsp;
326         struct genlmsghdr *genlh;
327         struct nlmsghdr *nlh;
328         mnl_cb_t parse;
329         int len, err;
330
331         len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);
332         if (len < (ssize_t)(sizeof(*nlh) + sizeof(*genlh)))
333                 return NULL;
334
335         nlh = (struct nlmsghdr *)ys->rx_buf;
336         genlh = mnl_nlmsg_get_payload(nlh);
337
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;
344                 break;
345         default:
346                 ynl_error_unknown_notification(ys, genlh->cmd);
347                 return NULL;
348         }
349
350         yarg.data = rsp->data;
351
352         err = mnl_cb_run2(ys->rx_buf, len, 0, 0, parse, &yarg,
353                          ynl_cb_array, NLMSG_MIN_TYPE);
354         if (err < 0)
355                 goto err_free;
356
357         rsp->family = nlh->nlmsg_type;
358         rsp->cmd = genlh->cmd;
359         return rsp;
360
361 err_free:
362         free(rsp);
363         return NULL;
364 }
365
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,
372         },
373 };
374
375 const struct ynl_family ynl_handshake_family =  {
376         .name           = "handshake",
377         .ntf_info       = handshake_ntf_info,
378         .ntf_info_size  = MNL_ARRAY_SIZE(handshake_ntf_info),
379 };