-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnfct.c
1399 lines (1176 loc) · 39.4 KB
/
nfct.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011 Wurldtech Security Technologies All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
/*-
** nfct - a binding to netfilter's conntrack subsystem
NOTE I know its confusing that the nfct module has functions that should be
called on different kinds of objects mixed together, but unless I make full
userdata out of one or both of them, thats what it has to be. Don't confuse
them, or you will segfault!
Also, the netfilter libraries use assert() to check for invalid argument
checking, and non-type-safe APIs. The end result is you can absolutely
segfault or abort if you misuse this module.
*/
#define WANT_NF_LUA_PF
#define WANT_NF_LUA_PF_PUSH
#define WANT_NF_LUA_IPPROTO
#include "nflua.h"
#include <alloca.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#define NFCT_REGID "wt.nfct"
static struct nfct_handle* check_cthandle(lua_State*L)
{
struct nfct_handle* cth = lua_touserdata(L, 1);
luaL_argcheck(L, cth, 1, "conntrack handle not provided");
return cth;
}
static struct nf_conntrack* check_ct_argn(lua_State*L, int argn, const char* emsg)
{
struct nf_conntrack* ct = lua_touserdata(L, argn);
luaL_argcheck(L, ct, argn, emsg);
return ct;
}
static struct nf_conntrack* check_ct(lua_State*L)
{
return check_ct_argn(L, 1, "conntrack not provided");
}
static struct nf_expect* check_exp(lua_State* L, int argn)
{
struct nf_expect* exp = lua_touserdata(L, argn);
luaL_argcheck(L, exp, argn, "expect not provided");
return exp;
}
static enum nf_conntrack_msg_type check_ctmsgtype(lua_State* L, int argn, const char* def)
{
static const char* msgtype_opts[] = {
"new", "update", "destroy", "all", "error", "none", NULL
};
static enum nf_conntrack_msg_type msgtype_vals[] = {
NFCT_T_NEW, NFCT_T_UPDATE, NFCT_T_DESTROY, NFCT_T_ALL, NFCT_T_ERROR, 0,
};
int msgtype_opt = luaL_checkoption(L, argn, def, msgtype_opts);
return msgtype_vals[msgtype_opt];
}
static const char* ctmsgtype_string(enum nf_conntrack_msg_type type)
{
switch(type) {
case NFCT_T_NEW: return "new";
case NFCT_T_UPDATE: return "update";
case NFCT_T_DESTROY: return "destroy";
case NFCT_T_ERROR: return "error";
default: return "unknown";
}
}
static const char* ATTR_opts[] = {
"orig-ipv4-src",
"ipv4-src",
"orig-ipv4-dst",
"ipv4-dst",
"repl-ipv4-src",
"repl-ipv4-dst",
"orig-ipv6-src",
"ipv6-src",
"orig-ipv6-dst",
"ipv6-dst",
"repl-ipv6-src",
"repl-ipv6-dst",
"orig-port-src",
"port-src",
"orig-port-dst",
"port-dst",
"repl-port-src",
"repl-port-dst",
"icmp-type",
"icmp-code",
"icmp-id",
"orig-l3proto",
"l3proto",
"repl-l3proto",
"orig-l4proto",
"l4proto",
"repl-l4proto",
"tcp-state",
"snat-ipv4",
"dnat-ipv4",
"snat-port",
"dnat-port",
"timeout",
"mark",
"orig-counter-packets",
"repl-counter-packets",
"orig-counter-bytes",
"repl-counter-bytes",
"use",
"id",
"status",
"tcp-flags-orig",
"tcp-flags-repl",
"tcp-mask-orig",
"tcp-mask-repl",
"master-ipv4-src",
"master-ipv4-dst",
"master-ipv6-src",
"master-ipv6-dst",
"master-port-src",
"master-port-dst",
"master-l3proto",
"master-l4proto",
"secmark",
"orig-nat-seq-correction-pos",
"orig-nat-seq-offset-before",
"orig-nat-seq-offset-after",
"repl-nat-seq-correction-pos",
"repl-nat-seq-offset-before",
"repl-nat-seq-offset-after",
"sctp-state",
"sctp-vtag-orig",
"sctp-vtag-repl",
"helper-name",
"dccp-state",
"dccp-role",
"dccp-handshake-seq",
"grp-orig-ipv4",
"grp-repl-ipv4",
"grp-orig-ipv6",
"grp-repl-ipv6",
"grp-orig-port",
"grp-repl-port",
"grp-icmp",
"grp-master-ipv4",
"grp-master-ipv6",
"grp-master-port",
"grp-orig-counters",
"grp-repl-counters",
"grp-max",
"exp-master",
"exp-expected",
"exp-mask",
"exp-timeout",
"exp-max",
NULL
};
static int ATTR_vals[] = {
ATTR_ORIG_IPV4_SRC,
ATTR_IPV4_SRC,
ATTR_ORIG_IPV4_DST,
ATTR_IPV4_DST,
ATTR_REPL_IPV4_SRC,
ATTR_REPL_IPV4_DST,
ATTR_ORIG_IPV6_SRC,
ATTR_IPV6_SRC,
ATTR_ORIG_IPV6_DST,
ATTR_IPV6_DST,
ATTR_REPL_IPV6_SRC,
ATTR_REPL_IPV6_DST,
ATTR_ORIG_PORT_SRC,
ATTR_PORT_SRC,
ATTR_ORIG_PORT_DST,
ATTR_PORT_DST,
ATTR_REPL_PORT_SRC,
ATTR_REPL_PORT_DST,
ATTR_ICMP_TYPE,
ATTR_ICMP_CODE,
ATTR_ICMP_ID,
ATTR_ORIG_L3PROTO,
ATTR_L3PROTO,
ATTR_REPL_L3PROTO,
ATTR_ORIG_L4PROTO,
ATTR_L4PROTO,
ATTR_REPL_L4PROTO,
ATTR_TCP_STATE,
ATTR_SNAT_IPV4,
ATTR_DNAT_IPV4,
ATTR_SNAT_PORT,
ATTR_DNAT_PORT,
ATTR_TIMEOUT,
ATTR_MARK,
ATTR_ORIG_COUNTER_PACKETS,
ATTR_REPL_COUNTER_PACKETS,
ATTR_ORIG_COUNTER_BYTES,
ATTR_REPL_COUNTER_BYTES,
ATTR_USE,
ATTR_ID,
ATTR_STATUS,
ATTR_TCP_FLAGS_ORIG,
ATTR_TCP_FLAGS_REPL,
ATTR_TCP_MASK_ORIG,
ATTR_TCP_MASK_REPL,
ATTR_MASTER_IPV4_SRC,
ATTR_MASTER_IPV4_DST,
ATTR_MASTER_IPV6_SRC,
ATTR_MASTER_IPV6_DST,
ATTR_MASTER_PORT_SRC,
ATTR_MASTER_PORT_DST,
ATTR_MASTER_L3PROTO,
ATTR_MASTER_L4PROTO,
ATTR_SECMARK,
ATTR_ORIG_NAT_SEQ_CORRECTION_POS,
ATTR_ORIG_NAT_SEQ_OFFSET_BEFORE,
ATTR_ORIG_NAT_SEQ_OFFSET_AFTER,
ATTR_REPL_NAT_SEQ_CORRECTION_POS,
ATTR_REPL_NAT_SEQ_OFFSET_BEFORE,
ATTR_REPL_NAT_SEQ_OFFSET_AFTER,
ATTR_SCTP_STATE,
ATTR_SCTP_VTAG_ORIG,
ATTR_SCTP_VTAG_REPL,
ATTR_HELPER_NAME,
ATTR_DCCP_STATE,
ATTR_DCCP_ROLE,
ATTR_DCCP_HANDSHAKE_SEQ,
ATTR_GRP_ORIG_IPV4,
ATTR_GRP_REPL_IPV4,
ATTR_GRP_ORIG_IPV6,
ATTR_GRP_REPL_IPV6,
ATTR_GRP_ORIG_PORT,
ATTR_GRP_REPL_PORT,
ATTR_GRP_ICMP,
ATTR_GRP_MASTER_IPV4,
ATTR_GRP_MASTER_IPV6,
ATTR_GRP_MASTER_PORT,
ATTR_GRP_ORIG_COUNTERS,
ATTR_GRP_REPL_COUNTERS,
ATTR_GRP_MAX,
ATTR_EXP_MASTER,
ATTR_EXP_EXPECTED,
ATTR_EXP_MASK,
ATTR_EXP_TIMEOUT,
ATTR_EXP_MAX,
0
};
/*static int ATTR_vals_size = 85;*/
static int check_ATTR(lua_State* L, int argn)
{
int opt = luaL_checkoption(L, argn, NULL /* default? */, ATTR_opts);
int val = ATTR_vals[opt];
return val;
}
static enum nf_conntrack_attr check_attr(lua_State* L)
{
return check_ATTR(L, 2);
}
static const char* NFCT_Q_opts[] = {
"create",
"update",
"destroy",
"get",
"flush",
"dump",
"dump-reset",
"create-update",
NULL
};
static int NFCT_Q_vals[] = {
NFCT_Q_CREATE,
NFCT_Q_UPDATE,
NFCT_Q_DESTROY,
NFCT_Q_GET,
NFCT_Q_FLUSH,
NFCT_Q_DUMP,
NFCT_Q_DUMP_RESET,
NFCT_Q_CREATE_UPDATE,
0
};
/*static int NFCT_Q_vals_size = 8;*/
static int check_NFCT_Q(lua_State* L, int argn)
{
int opt = luaL_checkoption(L, argn, NULL, NFCT_Q_opts);
int val = NFCT_Q_vals[opt];
return val;
}
/*-
-- cthandle = nfct.open(subsys, [subscription...])
subsys is "conntrack", "expect", or "both".
subscription is the groups for which notifications are requested, zero or more of
"none", "new", "update", "destroy", or "all" (default is "none").
Returns a conntrack handle on success, or nil,emsg,errno on failure.
There is no garbage collection, nfct.fini() must be called on the handle to
release it's resources.
*/
static int hopen(lua_State *L)
{
static const char* subsys_opts[] = {
"both", "conntrack", "expect", NULL
};
static u_int8_t subsys_vals[] = {
NFNL_SUBSYS_NONE, NFNL_SUBSYS_CTNETLINK, NFNL_SUBSYS_CTNETLINK_EXP,
};
int subsys_opt = luaL_checkoption(L, 1, NULL, subsys_opts);
u_int8_t subsys_val = subsys_vals[subsys_opt];
static const char* subscription_opts[] = {
"none", "new", "update", "destroy", "all", NULL
};
#define NFCT_ALL_EXP_GROUPS \
(NF_NETLINK_CONNTRACK_EXP_NEW|NF_NETLINK_CONNTRACK_EXP_UPDATE|NF_NETLINK_CONNTRACK_EXP_DESTROY)
unsigned subscription_vals[][5] = {
{ /* [0] == "both" */
0, /* none */
NF_NETLINK_CONNTRACK_NEW | NF_NETLINK_CONNTRACK_EXP_NEW,
NF_NETLINK_CONNTRACK_UPDATE | NF_NETLINK_CONNTRACK_EXP_UPDATE,
NF_NETLINK_CONNTRACK_DESTROY | NF_NETLINK_CONNTRACK_EXP_DESTROY,
NFCT_ALL_CT_GROUPS | NFCT_ALL_EXP_GROUPS
},
{ /* [1] == "conntrack" */
0, /* none */
NF_NETLINK_CONNTRACK_NEW,
NF_NETLINK_CONNTRACK_UPDATE,
NF_NETLINK_CONNTRACK_DESTROY,
NFCT_ALL_CT_GROUPS
},
{ /* [2] == "expect" */
0, /* none */
NF_NETLINK_CONNTRACK_EXP_NEW,
NF_NETLINK_CONNTRACK_EXP_UPDATE,
NF_NETLINK_CONNTRACK_EXP_DESTROY,
NFCT_ALL_EXP_GROUPS
}
};
unsigned subscription_val = 0;
int argn = 0;
struct nfct_handle* cthandle = NULL;
for(argn = 2; argn <= lua_gettop(L); argn++) {
int subscription_opt = luaL_checkoption(L, argn, NULL, subscription_opts);
subscription_val |= subscription_vals[subsys_opt][subscription_opt];
}
cthandle = nfct_open(subsys_val, subscription_val);
if(!cthandle) {
push_error(L);
return 3;
}
/* Create the table to hold the callback functions, create keys for
* callbacks that are valid for the subsystem(s). libnfct aborts if we
* register callbacks for subsystems that haven't been initialized, so the
* presence of these keys allows us to avoid that.
*/
/* _ = {} */
/* _.connect = true -- if subsys */
/* _.expect = true -- if subsys */
/* registery[cthandle] = _ */
lua_settop(L, 0);
lua_newtable(L);
if(subsys_val == 0 || subsys_val == NFNL_SUBSYS_CTNETLINK) {
lua_pushboolean(L, 1);
lua_setfield(L, -2, "conntrack");
}
if(subsys_val == 0 || subsys_val == NFNL_SUBSYS_CTNETLINK_EXP) {
lua_pushboolean(L, 1);
lua_setfield(L, -2, "expect");
}
lua_pushlightuserdata(L, cthandle);
lua_insert(L, 1);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, cthandle);
return 1;
}
/*-
-- nfct.close(cthandle)
Close the conntrack handle, freeing its resources.
*/
static int gc(lua_State* L)
{
struct nfct_handle* cthandle = check_cthandle(L);
/* Delete the table holding the callback functions. */
/* registery[cthandle] = nil */
lua_pushlightuserdata(L, cthandle);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
nfct_close(cthandle);
return 0;
}
/*-
-- fd = nfct.fd(cthandle)
Return the underlying fd used by the conntrack handle, useful for
selecting on.
*/
static int fd(lua_State* L)
{
struct nfct_handle* cthandle = check_cthandle(L);
lua_pushinteger(L, nfct_fd(cthandle));
return 1;
}
/*-
-- cthandle = nfct.setblocking(cthandle, [blocking])
blocking is true to set blocking, and false to set non-blocking (default is false)
Return is cthandle on success, or nil,emsg,errno on failure.
*/
static int setblocking(lua_State* L)
{
return nfsetblocking(L, nfct_fd(check_cthandle(L)));
}
static int cb(
const char* subsys,
const struct nlmsghdr *nlh,
enum nf_conntrack_msg_type type,
void* cbobj,
void *data
)
{
static const char* verdict_opts[] = {
"failure", "stop", "continue", "stolen", NULL
};
static int verdict_vals[] = {
NFCT_CB_FAILURE, NFCT_CB_STOP, NFCT_CB_CONTINUE, NFCT_CB_STOLEN
};
int verdict_val = 0;
lua_State* L = data;
/* We expect stack to look like:
* [1] cthandle
* [2] cbtable
* .conntrack = ctcb
* .expect = expcb
*/
lua_getfield(L, 2, subsys);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushstring(L, ctmsgtype_string(type));
lua_pushlightuserdata(L, cbobj);
/* [1] cthandle */
/* [2] cbtable */
/* [3] fn */
/* [4] msgtype */
/* [5] obj */
/* TODO - we should pass the netlink header */
lua_call(L, 2, 1);
/* [1] cthandle */
/* [2] cbtable */
/* [3] verdict */
verdict_val = verdict_vals[
luaL_checkoption(L, 3, "continue", verdict_opts)
];
/* Reset stack, chopping any return value. */
lua_settop(L, 2);
return verdict_val;
}
static int ctcb(
const struct nlmsghdr *nlh,
enum nf_conntrack_msg_type type,
struct nf_conntrack *ct,
void *data
)
{
return cb("conntrack", nlh, type, ct, data);
}
static int expcb(
const struct nlmsghdr *nlh,
enum nf_conntrack_msg_type type,
struct nf_expect *exp,
void *data
)
{
return cb("expect", nlh, type, exp, data);
}
/*-
-- cthandle = nfct.ct_callback_register(cthandle, ctcb, ctmsgtype)
-- cthandle = nfct.exp_callback_register(cthandle, expcb, ctmsgtype)
For each subsystem (conntrack and expect) only one registration can be active
at a time, the latest call replaces any previous ones.
Callbacks can't be registered for a subsystem that wasn't opened.
The callback function will be called as either
verdict = ctcb(ctmsgtype, ct)
verdict = expcb(ctmsgtype, exp)
depending on which register is called. Since you can't know the type of the object,
use different callback functions.
ctmsgtype is one of "new", "update", "destroy", "all", or "error" (default is "all").
The callback can return any of "failure", "stop", "continue", or "stolen" (the
default is "continue"):
"failure" will stop the loop,
"continue" will continue with the next message, and
"stolen" is like continue, except the conntrack or expect object will
not be destroyed (the user must destroy it later with the appropriate
nfct.destroy() or nfct.exp_destroy or resources will be leaked)
Returns cthandle on success, nil,emsg,errno on failure.
*/
/* FIXME - the underlying IPCTNL_MSG_CT_NEW and IPCTNL_MSG_CT_DELETE are
* registered for (always and only), making me think that only "new" and
* "destroy" can actually be received in a callback.
* Since we choose what kind of notifications we want in nfct_open(), I'm not
* sure why libconntrack allows us to filter what arrives at the callback... it
* would make more sense if you could have multiple callbacks, but you can't.
*/
static int callback_register(lua_State* L, const char* subsys,
void (*unreg)(struct nfct_handle*),
int (*reg)(struct nfct_handle*, enum nf_conntrack_msg_type, int(*cbfn)(), void* data),
int (*cbfn)()
)
{
struct nfct_handle* cthandle = check_cthandle(L);
enum nf_conntrack_msg_type msgtype_val = check_ctmsgtype(L, 3, "all");
int ret;
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 3);
/* Get the cbtable */
lua_pushvalue(L, 1);
lua_gettable(L, LUA_REGISTRYINDEX);
/* Check the subsys was opened. */
lua_getfield(L, 4, subsys);
luaL_argcheck(L, !lua_isnoneornil(L, 5), 1, "register failure, handle not open for this subsystem");
lua_settop(L, 4);
/* Save the cbfn */
lua_pushvalue(L, 2);
lua_setfield(L, 4, subsys);
/* Clear any current handler to avoid memory leaks. */
unreg(cthandle);
ret = reg(cthandle, msgtype_val, cbfn, L);
if(ret < 0) {
push_error(L);
return 3;
}
lua_pushvalue(L, 1);
return 1;
}
static int ct_callback_register(lua_State* L)
{
return callback_register(L, "conntrack", nfct_callback_unregister2, nfct_callback_register2, ctcb);
}
static int exp_callback_register(lua_State* L)
{
return callback_register(L, "expect", nfexp_callback_unregister2, nfexp_callback_register2, expcb);
}
/*-
-- cthandle = nfct.catch(cthandle)
Return is the cthandle on success, or nil,emsg,errno on failure.
*/
/* FIXME return the verdict on success ("stop", "continue", or "stolen") */
static int catch(lua_State* L)
{
struct nfct_handle* cthandle = check_cthandle(L);
int ret;
/* Ensure that the callbacks expectations about the stack are met. */
/* Ensure stack contains only cthandle,cbtable */
lua_settop(L, 1);
lua_pushvalue(L, 1);
lua_gettable(L, LUA_REGISTRYINDEX);
ret = nfct_catch(cthandle);
if(ret < 0) {
return push_error(L);
}
/* Leave just the cthandle on the return stack to indicate successs */
lua_settop(L, 1);
return 1;
}
/*-
-- nfct.loop(cthandle, ctmsgtype, ctcb)
Equivalent to
nfct.ct_callback_register(cthandle, ctcb, ctmsgtype)
return nfct.catch(cthandle)
Will probably be removed soon.
*/
static int loop(lua_State* L)
{
int nret = ct_callback_register(L);
if(nret > 1) {
return nret;
}
/* Pop the args that catch doesn't want. */
lua_settop(L, 1);
return catch(L);
}
/*-
-- ct = nfct.new()
Create a new conntrack context (NOT a conntrack handle).
No garbage collection on the context is done, it must be destroyed with
nfct.destroy() or resources will be leaked.
Return is the conntrack context on sucess, and nil,emsg,errno on failure (but
it can only fail if malloc fails).
*/
static int new(lua_State* L)
{
struct nf_conntrack* ct = nfct_new();
if(!ct) {
push_error(L);
return 3;
}
lua_pushlightuserdata(L, ct);
return 1;
}
/*-
-- nfct.destroy(ct)
Destroy a conntrack context.
Note that only contexts created with nfct.new() should be destroyed - in particular,
the ct passed in a nfct.loop() callback should NOT be destroyed.
*/
static int destroy(lua_State* L)
{
struct nf_conntrack* ct = check_ct(L);
nfct_destroy(ct);
return 0;
}
/*-
-- str = nfct.tostring(ct, ctmsgtype)
ctmsgtype is one of "new", "update", "destroy", or nil (meaning msg type is unknown).
Returns a string representation of a conntrack.
*/
static int tostring(lua_State* L)
{
struct nf_conntrack* ct = check_ct(L);
int ctmsgtype = check_ctmsgtype(L, 2, "none");
char* buf = alloca(1); /* nfct asserts with no buf... unlike snprintf */
int bufsz = nfct_snprintf(buf, 1, ct, ctmsgtype, 0, 0);
buf = alloca(bufsz+1);
nfct_snprintf(buf, bufsz+1, ct, ctmsgtype, 0, 0);
lua_pushstring(L, buf);
return 1;
}
/*-
-- ct = nfct.setobjopt(ct, option)
Sets an option on a conntrack context, option is one of:
"undo-snat",
"undo-dnat",
"undo-spat",
"undo-dpat",
"setup-original",
"setup-reply"
Returns ct on success so calls can be chained, and nil,emsg,errno on failure.
*/
static int setobjopt(lua_State* L)
{
struct nf_conntrack* ct = check_ct(L);
static const char* opts[] = {
"undo-snat",
"undo-dnat",
"undo-spat",
"undo-dpat",
"setup-original",
"setup-reply"
};
unsigned vals[] = {
NFCT_SOPT_UNDO_SNAT,
NFCT_SOPT_UNDO_DNAT,
NFCT_SOPT_UNDO_SPAT,
NFCT_SOPT_UNDO_DPAT,
NFCT_SOPT_SETUP_ORIGINAL,
NFCT_SOPT_SETUP_REPLY
};
unsigned option = vals[luaL_checkoption(L, 2, NULL, opts)];
int ret = nfct_setobjopt(ct, option);
if(ret < 0) {
return push_error(L);
}
lua_settop(L, 1);
return 1;
}
/*
TODO
get_attr_ip() -- ip in presentation format
get_attr_port() -- port as a number (cvt to host byte order)
get_attr_l3proto() -- protocol as string (or number if unrecognized)
get_attr_l4proto() -- "" ""
*/
/*-
-- value = nfct.get_attr_u8(ct, attr)
-- value = nfct.get_attr_u16(ct, attr)
-- value = nfct.get_attr_u32(ct, attr)
-- value = nfct.get_attr_n16(ct, attr)
-- value = nfct.get_attr_n32(ct, attr)
-- value = nfct.get_attr_port(ct, attr)
No error checking is done, values of zero will be returned for
attributes that aren't present, and undefined values will be returned
for attributes that aren't actually of the type requested. Also,
the attribute value may be in network byte order.
ct is a conntrack context (NOT a conntrack handle, do not mix the two).
get_attr_n#() is like the "u" version, but it converts the number from network
to host byte order.
get_attr_port() is an alias for get_attr_n16(), since TCP and UDP ports are n16.
attr is one of the enum nf_conntrack_attr values, where some aliases are
provided for the more commonly used origin attributes:
orig-ipv4-src -- ATTR_ORIG_IPV4_SRC, u32 bits
ipv4-src -- ATTR_IPV4_SRC, alias
orig-ipv4-dst -- ATTR_ORIG_IPV4_DST, u32 bits
ipv4-dst -- ATTR_IPV4_DST, alias
repl-ipv4-src -- ATTR_REPL_IPV4_SRC, u32 bits
repl-ipv4-dst -- ATTR_REPL_IPV4_DST, u32 bits
orig-ipv6-src -- ATTR_ORIG_IPV6_SRC, u128 bits
ipv6-src -- ATTR_IPV6_SRC, alias
orig-ipv6-dst -- ATTR_ORIG_IPV6_DST, u128 bits
ipv6-dst -- ATTR_IPV6_DST, alias
repl-ipv6-src -- ATTR_REPL_IPV6_SRC, u128 bits
repl-ipv6-dst -- ATTR_REPL_IPV6_DST, u128 bits
orig-port-src -- ATTR_ORIG_PORT_SRC, u16 bits
port-src -- ATTR_PORT_SRC, alias
orig-port-dst -- ATTR_ORIG_PORT_DST, u16 bits
port-dst -- ATTR_PORT_DST, alias
repl-port-src -- ATTR_REPL_PORT_SRC, u16 bits
repl-port-dst -- ATTR_REPL_PORT_DST, u16 bits
icmp-type -- ATTR_ICMP_TYPE, u8 bits
icmp-code -- ATTR_ICMP_CODE, u8 bits
icmp-id -- ATTR_ICMP_ID, u16 bits
orig-l3proto -- ATTR_ORIG_L3PROTO, u8 bits
l3proto -- ATTR_L3PROTO, alias
repl-l3proto -- ATTR_REPL_L3PROTO, u8 bits
orig-l4proto -- ATTR_ORIG_L4PROTO, u8 bits
l4proto -- ATTR_L4PROTO, alias
repl-l4proto -- ATTR_REPL_L4PROTO, u8 bits
tcp-state -- ATTR_TCP_STATE, u8 bits
snat-ipv4 -- ATTR_SNAT_IPV4, u32 bits
dnat-ipv4 -- ATTR_DNAT_IPV4, u32 bits
snat-port -- ATTR_SNAT_PORT, u16 bits
dnat-port -- ATTR_DNAT_PORT, u16 bits
timeout -- ATTR_TIMEOUT, u32 bits
mark -- ATTR_MARK, u32 bits
orig-counter-packets -- ATTR_ORIG_COUNTER_PACKETS, u32 bits
repl-counter-packets -- ATTR_REPL_COUNTER_PACKETS, u32 bits
orig-counter-bytes -- ATTR_ORIG_COUNTER_BYTES, u32 bits
repl-counter-bytes -- ATTR_REPL_COUNTER_BYTES, u32 bits
use -- ATTR_USE, u32 bits
id -- ATTR_ID, u32 bits
status -- ATTR_STATUS, u32 bits
tcp-flags-orig -- ATTR_TCP_FLAGS_ORIG, u8 bits
tcp-flags-repl -- ATTR_TCP_FLAGS_REPL, u8 bits
tcp-mask-orig -- ATTR_TCP_MASK_ORIG, u8 bits
tcp-mask-repl -- ATTR_TCP_MASK_REPL, u8 bits
master-ipv4-src -- ATTR_MASTER_IPV4_SRC, u32 bits
master-ipv4-dst -- ATTR_MASTER_IPV4_DST, u32 bits
master-ipv6-src -- ATTR_MASTER_IPV6_SRC, u128 bits
master-ipv6-dst -- ATTR_MASTER_IPV6_DST, u128 bits
master-port-src -- ATTR_MASTER_PORT_SRC, u16 bits
master-port-dst -- ATTR_MASTER_PORT_DST, u16 bits
master-l3proto -- ATTR_MASTER_L3PROTO, u8 bits
master-l4proto -- ATTR_MASTER_L4PROTO, u8 bits
secmark -- ATTR_SECMARK, u32 bits
orig-nat-seq-correction-pos -- ATTR_ORIG_NAT_SEQ_CORRECTION_POS, u32 bits
orig-nat-seq-offset-before -- ATTR_ORIG_NAT_SEQ_OFFSET_BEFORE, u32 bits
orig-nat-seq-offset-after -- ATTR_ORIG_NAT_SEQ_OFFSET_AFTER, u32 bits
repl-nat-seq-correction-pos -- ATTR_REPL_NAT_SEQ_CORRECTION_POS, u32 bits
repl-nat-seq-offset-before -- ATTR_REPL_NAT_SEQ_OFFSET_BEFORE, u32 bits
repl-nat-seq-offset-after -- ATTR_REPL_NAT_SEQ_OFFSET_AFTER, u32 bits
sctp-state -- ATTR_SCTP_STATE, u8 bits
sctp-vtag-orig -- ATTR_SCTP_VTAG_ORIG, u32 bits
sctp-vtag-repl -- ATTR_SCTP_VTAG_REPL, u32 bits
helper-name -- ATTR_HELPER_NAME, string (30 bytes max)
dccp-state -- ATTR_DCCP_STATE, u8 bits
dccp-role -- ATTR_DCCP_ROLE, u8 bits
dccp-handshake-seq -- ATTR_DCCP_HANDSHAKE_SEQ, u64 bits
*/
/*-
-- ct = nfct.set_attr_u8(ct, attr, value)
-- ct = nfct.set_attr_u16(ct, attr, value)
-- ct = nfct.set_attr_u32(ct, attr, value)
-- ct = nfct.set_attr_n16(ct, attr, value)
-- ct = nfct.set_attr_n32(ct, attr, value)
-- ct = nfct.set_attr_port(ct, attr, value)
No error checking is done, value will be cast to the necessary type, and who
knows what will happen for values that aren't actually of the correct type for
the attribute. The attribute value may need to be in network byte order.
ct is a conntrack context (NOT a conntrack handle, do not mix the two).
See nfct.get_attr_*() for the supported attr names and types.
Returns the conntrack conntext, so calls can be chained.
*/
/* Pretend nfct implements these, so I can construct setters/getters using my macro. */
static u_int16_t nfct_get_attr_n16(struct nf_conntrack* ct, enum nf_conntrack_attr attr)
{
return ntohs(nfct_get_attr_u16(ct, attr));
}
static u_int32_t nfct_get_attr_n32(struct nf_conntrack* ct, enum nf_conntrack_attr attr)
{
return ntohl(nfct_get_attr_u32(ct, attr));
}
static void nfct_set_attr_n16(struct nf_conntrack* ct, enum nf_conntrack_attr attr, u_int16_t value)
{
nfct_set_attr_u16(ct, attr, htons(value));
}
static void nfct_set_attr_n32(struct nf_conntrack* ct, enum nf_conntrack_attr attr, u_int32_t value)
{
nfct_set_attr_u32(ct, attr, htonl(value));
}
/*
static int get_attr_u8(lua_State* L)
{
lua_pushinteger(L, nfct_get_attr_u8(check_ct(L), check_attr(L)));
return 1;
}
*/
/* TODO get: should I add checks for existence of the attribute? I doubt
* performance is an issue, so why not return nil and emsg when attr isn't
* present.
*/
/* TODO set: can I use true to mean full-width? Useful for masks. */
/* TODO set: can I use nil to mean nfct_attr_unset()? */
#define ATTR_UX(ux) \
static int get_attr_##ux(lua_State* L) \
{ lua_pushinteger(L, nfct_get_attr_##ux(check_ct(L), check_attr(L))); return 1; } \
static int set_attr_##ux(lua_State* L) \
{ nfct_set_attr_##ux(check_ct(L), check_attr(L), luaL_checklong(L,3)); lua_settop(L, 1); return 1; }
ATTR_UX(u8)
ATTR_UX(u16)
ATTR_UX(u32)
ATTR_UX(n16)
ATTR_UX(n32)
/*-
-- ct = nfct.set_attr_ipv4(ct, attr, value)
Get an attribute as a string, the internet address in presentation format.
See inet_ntop(3) for more information.
Return is the presentation address, or nil,emsg,errno on failure.
*/
static int get_attr_ipvx(lua_State* L, int af, const void* src)
{
char dst[INET6_ADDRSTRLEN];
const char* p = inet_ntop(af, src, dst, sizeof(dst));
if(!p) {
return push_error(L);
}
lua_pushstring(L, p);
return 1;
}
static int get_attr_ipv4(lua_State* L)
{
return get_attr_ipvx(L,
AF_INET,
nfct_get_attr(check_ct(L), check_attr(L)));
}
static int get_attr_ipv6(lua_State* L)
{
return get_attr_ipvx(L,
AF_INET6,
nfct_get_attr(check_ct(L), check_attr(L)));
}
/*-
-- ct = nfct.set_attr_ipv4(ct, attr, value)
-- ct = nfct.set_attr_ipv6(ct, attr, value)
Set an attribute as a string, the internet address in presentation format.
See inet_ntop(3) for more information.