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