-
Notifications
You must be signed in to change notification settings - Fork 13
/
nl.c
534 lines (427 loc) · 14.8 KB
/
nl.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/
#include <errno.h>
#include "nl.h"
#define ECO_NLMSG_USER_MT "eco{nlmsg-user}"
static int eco_nlmsg_next(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_KER_MT);
struct nlmsghdr *nlh;
if (!msg->nlh) {
msg->nlh = (struct nlmsghdr *)msg->buf;
} else {
if (!NLMSG_OK(msg->nlh, msg->size)) {
lua_pushnil(L);
return 1;
}
msg->nlh = NLMSG_NEXT(msg->nlh, msg->size);
}
nlh = msg->nlh;
if (!NLMSG_OK(nlh, msg->size)) {
lua_pushnil(L);
return 1;
}
lua_newtable(L);
lua_pushinteger(L, nlh->nlmsg_type);
lua_setfield(L, -2, "type");
lua_pushinteger(L, nlh->nlmsg_flags);
lua_setfield(L, -2, "flags");
lua_pushinteger(L, nlh->nlmsg_seq);
lua_setfield(L, -2, "seq");
lua_pushinteger(L, nlh->nlmsg_pid);
lua_setfield(L, -2, "pid");
return 1;
}
static int eco_nlmsg_payload(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_KER_MT);
struct nlmsghdr *nlh = msg->nlh;
if (!NLMSG_OK(nlh, msg->size)) {
lua_pushnil(L);
lua_pushliteral(L, "invalid nlmsg");
return 2;
}
lua_pushlstring(L, NLMSG_DATA(nlh), NLMSG_PAYLOAD(nlh, 0));
return 1;
}
static int eco_nlmsg_parse_attr(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_KER_MT);
size_t offset = luaL_checkinteger(L, 2);
struct nlmsghdr *nlh = msg->nlh;
const struct nlattr *attr;
int rem;
if (!NLMSG_OK(nlh, msg->size)) {
lua_pushnil(L);
lua_pushliteral(L, "invalid nlmsg");
return 2;
}
lua_newtable(L);
nla_for_each_attr(attr, NLMSG_DATA(nlh) + NLMSG_ALIGN(offset), NLMSG_PAYLOAD(nlh, offset), rem) {
lua_pushlstring(L, (const char *)attr, attr->nla_len);
lua_rawseti(L, -2, nla_type(attr));
}
return 1;
}
static int eco_nlmsg_parse_error(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_KER_MT);
struct nlmsghdr *nlh = msg->nlh;
struct nlmsgerr *err;
if (!NLMSG_OK(nlh, msg->size)) {
lua_pushnil(L);
lua_pushliteral(L, "invalid nlmsg");
return 2;
}
if (nlh->nlmsg_type != NLMSG_ERROR) {
lua_pushnil(L);
lua_pushliteral(L, "not a nlmsg with type NLMSG_ERROR");
return 2;
}
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
lua_pushnil(L);
lua_pushliteral(L, "invalid nlmsg");
return 2;
}
err = NLMSG_DATA(nlh);
lua_pushinteger(L, err->error);
return 1;
}
static const struct luaL_Reg nlmsg_ker_methods[] = {
{"next", eco_nlmsg_next},
{"payload", eco_nlmsg_payload},
{"parse_attr", eco_nlmsg_parse_attr},
{"parse_error", eco_nlmsg_parse_error},
{NULL, NULL}
};
static int eco_new_nlmsg_ker(lua_State *L)
{
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
struct eco_nlmsg *msg;
msg = lua_newuserdata(L, sizeof(struct eco_nlmsg) + len);
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
memcpy(msg->buf, data, len);
msg->size = len;
msg->nlh = NULL;
msg->nest = NULL;
return 1;
}
static int eco_nlmsg_to_binary(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
lua_pushlstring(L, (const char *)msg->buf, msg->nlh->nlmsg_len);
return 1;
}
static int eco_nlmsg_put(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
struct nlmsghdr *nlh = msg->nlh;
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
if (nlh->nlmsg_len + NLMSG_ALIGN(len) > msg->size) {
lua_pushnil(L);
lua_pushliteral(L, "buf is full");
return 2;
}
memcpy((char *)msg->nlh + nlh->nlmsg_len, data, len);
nlh->nlmsg_len += NLMSG_ALIGN(len);
lua_settop(L, 1);
return 1;
}
static int __eco_nlmsg_put_attr(lua_State *L, struct eco_nlmsg *msg,
uint16_t type, size_t len, const void *data)
{
struct nlmsghdr *nlh = msg->nlh;
struct nlattr *attr = (void *)nlh + NLMSG_ALIGN(nlh->nlmsg_len);
uint16_t payload_len = NLMSG_ALIGN(sizeof(struct nlattr)) + len;
int pad;
if (nlh->nlmsg_len + NLA_HDRLEN + NLMSG_ALIGN(len) > msg->size) {
lua_pushnil(L);
lua_pushliteral(L, "buf is full");
return 2;
}
attr->nla_type = type;
attr->nla_len = payload_len;
memcpy(nla_data(attr), data, len);
pad = NLMSG_ALIGN(len) - len;
if (pad > 0)
memset(nla_data(attr) + len, 0, pad);
nlh->nlmsg_len += NLMSG_ALIGN(payload_len);
if (type & NLA_F_NESTED)
msg->nest = attr;
else if (msg->nest)
msg->nest->nla_len += NLMSG_ALIGN(payload_len);
lua_settop(L, 1);
return 1;
}
static int eco_nlmsg_put_attr(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
size_t len;
const char *value = luaL_checklstring(L, 3, &len);
return __eco_nlmsg_put_attr(L, msg, type, len, value);
}
static int eco_nlmsg_put_attr_flag(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
return __eco_nlmsg_put_attr(L, msg, type, 0, NULL);
}
static int eco_nlmsg_put_attr_u8(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
int value = luaL_checkinteger(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, sizeof(uint8_t), &value);
}
static int eco_nlmsg_put_attr_u16(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
int value = luaL_checkinteger(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, sizeof(uint16_t), &value);
}
static int eco_nlmsg_put_attr_u32(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
uint32_t value;
if (sizeof(lua_Integer) < 8)
value = (uint32_t)luaL_checknumber(L, 3);
else
value = (uint32_t)luaL_checkinteger(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, sizeof(uint32_t), &value);
}
static int eco_nlmsg_put_attr_u64(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
uint64_t value;
if (sizeof(lua_Integer) < 8)
value = (uint64_t)luaL_checknumber(L, 3);
else
value = (uint64_t)luaL_checkinteger(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, sizeof(uint64_t), &value);
}
static int eco_nlmsg_put_attr_str(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
const char *value = lua_tostring(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, strlen(value), value);
}
static int eco_nlmsg_put_attr_strz(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
const char *value = lua_tostring(L, 3);
return __eco_nlmsg_put_attr(L, msg, type, strlen(value) + 1, value);
}
static int eco_nlmsg_put_attr_nest_start(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
int type = luaL_checkinteger(L, 2);
return __eco_nlmsg_put_attr(L, msg, type | NLA_F_NESTED, 0, NULL);
}
static int eco_nlmsg_put_attr_nest_end(lua_State *L)
{
struct eco_nlmsg *msg = luaL_checkudata(L, 1, ECO_NLMSG_USER_MT);
msg->nest = NULL;
lua_settop(L, 1);
return 1;
}
static const struct luaL_Reg nlmsg_user_methods[] = {
{"binary", eco_nlmsg_to_binary},
{"put", eco_nlmsg_put},
{"put_attr", eco_nlmsg_put_attr},
{"put_attr_flag", eco_nlmsg_put_attr_flag},
{"put_attr_u8", eco_nlmsg_put_attr_u8},
{"put_attr_u16", eco_nlmsg_put_attr_u16},
{"put_attr_u32", eco_nlmsg_put_attr_u32},
{"put_attr_u64", eco_nlmsg_put_attr_u64},
{"put_attr_str", eco_nlmsg_put_attr_str},
{"put_attr_strz", eco_nlmsg_put_attr_strz},
{"put_attr_nest_start", eco_nlmsg_put_attr_nest_start},
{"put_attr_nest_end", eco_nlmsg_put_attr_nest_end},
{NULL, NULL}
};
static int eco_new_nlmsg_user(lua_State *L)
{
int type = luaL_checkinteger(L, 1);
int flags = luaL_checkinteger(L, 2);
int seq = luaL_optinteger(L, 3, 0);
int size = luaL_optinteger(L, 4, 4096);
struct eco_nlmsg *msg;
struct nlmsghdr *nlh;
size = NLMSG_SPACE(size);
msg = lua_newuserdata(L, sizeof(struct eco_nlmsg) + size);
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
memset(msg->buf, 0, NLMSG_ALIGN(sizeof(struct nlmsghdr)));
nlh = (struct nlmsghdr *)msg->buf;
nlh->nlmsg_len = NLMSG_ALIGN(sizeof(struct nlmsghdr));
nlh->nlmsg_type = type;
nlh->nlmsg_flags = flags;
nlh->nlmsg_seq = seq;
msg->nlh = nlh;
msg->size = size;
msg->nest = NULL;
return 1;
}
static int eco_nl_attr_get_u8(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int value = *((uint8_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_s8(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int8_t value = *((int8_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_u16(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int value = *((uint16_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_s16(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int16_t value = *((int16_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_u32(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
uint32_t value = *((uint32_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_s32(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int32_t value = *((int32_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_u64(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
uint64_t value = *((uint64_t *)nla_data(attr));
/* overflow */
if (value > INT64_MAX)
value = INT64_MAX;
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_s64(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
int64_t value = *((int64_t *)nla_data(attr));
lua_pushinteger(L, value);
return 1;
}
static int eco_nl_attr_get_str(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
const char *value = nla_data(attr);
lua_pushstring(L, value);
return 1;
}
static int eco_nl_attr_get_payload(lua_State *L)
{
const struct nlattr *attr = (const struct nlattr *)luaL_checkstring(L, 1);
lua_pushlstring(L, nla_data(attr), nla_len(attr));
return 1;
}
static int eco_nl_parse_attr_nested(lua_State *L)
{
const struct nlattr *nest = (const struct nlattr *)luaL_checkstring(L, 1);
const struct nlattr *attr;
int rem;
lua_newtable(L);
nla_for_each_nested(attr, nest, rem) {
lua_pushlstring(L, (const char *)attr, attr->nla_len);
lua_rawseti(L, -2, nla_type(attr));
}
return 1;
}
static const luaL_Reg funcs[] = {
{"attr_get_u8", eco_nl_attr_get_u8},
{"attr_get_s8", eco_nl_attr_get_s8},
{"attr_get_u16", eco_nl_attr_get_u16},
{"attr_get_s16", eco_nl_attr_get_s16},
{"attr_get_u32", eco_nl_attr_get_u32},
{"attr_get_s32", eco_nl_attr_get_s32},
{"attr_get_u64", eco_nl_attr_get_u64},
{"attr_get_s64", eco_nl_attr_get_s64},
{"attr_get_str", eco_nl_attr_get_str},
{"attr_get_payload", eco_nl_attr_get_payload},
{"parse_attr_nested", eco_nl_parse_attr_nested},
{NULL, NULL}
};
int luaopen_eco_core_nl(lua_State *L)
{
luaL_newlib(L, funcs);
lua_add_constant(L, "NLMSG_NOOP", NLMSG_NOOP);
lua_add_constant(L, "NLMSG_ERROR", NLMSG_ERROR);
lua_add_constant(L, "NLMSG_DONE", NLMSG_DONE);
lua_add_constant(L, "NLMSG_OVERRUN", NLMSG_OVERRUN);
lua_add_constant(L, "NLMSG_MIN_TYPE", NLMSG_MIN_TYPE);
lua_add_constant(L, "NLM_F_REQUEST", NLM_F_REQUEST);
lua_add_constant(L, "NLM_F_MULTI", NLM_F_MULTI);
lua_add_constant(L, "NLM_F_ACK", NLM_F_ACK);
lua_add_constant(L, "NLM_F_ECHO", NLM_F_ECHO);
lua_add_constant(L, "NLM_F_DUMP_INTR", NLM_F_DUMP_INTR);
lua_add_constant(L, "NLM_F_DUMP_FILTERED", NLM_F_DUMP_FILTERED);
lua_add_constant(L, "NLM_F_ROOT", NLM_F_ROOT);
lua_add_constant(L, "NLM_F_MATCH", NLM_F_MATCH);
lua_add_constant(L, "NLM_F_ATOMIC", NLM_F_ATOMIC);
lua_add_constant(L, "NLM_F_DUMP", NLM_F_DUMP);
lua_add_constant(L, "NLM_F_REPLACE", NLM_F_REPLACE);
lua_add_constant(L, "NLM_F_EXCL", NLM_F_EXCL);
lua_add_constant(L, "NLM_F_CREATE", NLM_F_CREATE);
lua_add_constant(L, "NLM_F_APPEND", NLM_F_APPEND);
lua_add_constant(L, "NLM_F_NONREC", NLM_F_NONREC);
lua_add_constant(L, "NLM_F_CAPPED", NLM_F_CAPPED);
lua_add_constant(L, "NLM_F_ACK_TLVS", NLM_F_ACK_TLVS);
lua_add_constant(L, "NLMSGERR_ATTR_MSG", NLMSGERR_ATTR_MSG);
lua_add_constant(L, "NLMSGERR_ATTR_OFFS", NLMSGERR_ATTR_OFFS);
lua_add_constant(L, "NLMSGERR_ATTR_COOKIE", NLMSGERR_ATTR_COOKIE);
lua_add_constant(L, "NLMSGERR_SIZE", sizeof(struct nlmsgerr));
lua_add_constant(L, "NETLINK_ROUTE", NETLINK_ROUTE);
lua_add_constant(L, "NETLINK_UNUSED", NETLINK_UNUSED);
lua_add_constant(L, "NETLINK_USERSOCK", NETLINK_USERSOCK);
lua_add_constant(L, "NETLINK_FIREWALL", NETLINK_FIREWALL);
lua_add_constant(L, "NETLINK_SOCK_DIAG", NETLINK_SOCK_DIAG);
lua_add_constant(L, "NETLINK_NFLOG", NETLINK_NFLOG);
lua_add_constant(L, "NETLINK_XFRM", NETLINK_XFRM);
lua_add_constant(L, "NETLINK_SELINUX", NETLINK_SELINUX);
lua_add_constant(L, "NETLINK_ISCSI", NETLINK_ISCSI);
lua_add_constant(L, "NETLINK_AUDIT", NETLINK_AUDIT);
lua_add_constant(L, "NETLINK_FIB_LOOKUP", NETLINK_FIB_LOOKUP);
lua_add_constant(L, "NETLINK_CONNECTOR", NETLINK_CONNECTOR);
lua_add_constant(L, "NETLINK_NETFILTER", NETLINK_NETFILTER);
lua_add_constant(L, "NETLINK_IP6_FW", NETLINK_IP6_FW);
lua_add_constant(L, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
lua_add_constant(L, "NETLINK_KOBJECT_UEVENT", NETLINK_KOBJECT_UEVENT);
lua_add_constant(L, "NETLINK_GENERIC", NETLINK_GENERIC);
eco_new_metatable(L, ECO_NLMSG_USER_MT, nlmsg_user_methods);
lua_pushcclosure(L, eco_new_nlmsg_user, 1);
lua_setfield(L, -2, "nlmsg");
eco_new_metatable(L, ECO_NLMSG_KER_MT, nlmsg_ker_methods);
lua_pushcclosure(L, eco_new_nlmsg_ker, 1);
lua_setfield(L, -2, "nlmsg_ker");
return 1;
}