-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.c
1441 lines (1168 loc) · 37 KB
/
net.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) 2010 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.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "dumbnet.h"
#include "libnet_decode.h"
#include <libnet.h>
#define LUA_LIB /* To pull in LUA_API definitions for modules */
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#undef NET_DUMP
/*
Get field from arg table, errors if argt is not a table, returns
0 if field not found, otherwise pushes field value and returns
index.
*/
static int v_arg(lua_State* L, int argt, const char* field)
{
luaL_checktype(L, argt, LUA_TTABLE);
lua_getfield(L, argt, field);
if(lua_isnil(L, -1)) {
lua_pop(L, 1);
return 0;
}
return lua_gettop(L);
}
static const char* v_arg_lstring(lua_State* L, int argt, const char* field, size_t* size, const char* def)
{
if(!v_arg(L, argt, field))
{
if(def) {
lua_pushstring(L, def);
return lua_tolstring(L, -1, size);
} else {
const char* msg = lua_pushfstring(L, "%s is missing", field);
luaL_argerror(L, argt, msg);
}
}
if(!lua_tostring(L, -1)) {
const char* msg = lua_pushfstring(L, "%s is not a string", field);
luaL_argerror(L, argt, msg);
}
return lua_tolstring(L, -1, size);
}
static const char* v_arg_string(lua_State* L, int argt, const char* field)
{
return v_arg_lstring(L, argt, field, NULL, NULL);
}
static int v_arg_integer_get_(lua_State* L, int argt, const char* field)
{
if(lua_type(L, -1) != LUA_TNUMBER) {
const char* msg = lua_pushfstring(L, "%s is not an integer", field);
luaL_argerror(L, argt, msg);
}
return lua_tointeger(L, -1);
}
static int v_arg_integer(lua_State* L, int argt, const char* field)
{
if(!v_arg(L, argt, field))
{
const char* msg = lua_pushfstring(L, "%s is missing", field);
luaL_argerror(L, argt, msg);
}
return v_arg_integer_get_(L, argt, field);
}
static int v_arg_integer_opt(lua_State* L, int argt, const char* field, int def)
{
if(!v_arg(L, argt, field))
{
lua_pushinteger(L, def);
return lua_tointeger(L, -1);
}
return v_arg_integer_get_(L, argt, field);
}
static void v_obj_metatable(lua_State* L, const char* regid, const struct luaL_reg methods[])
{
/* metatable = { ... methods ... } */
luaL_newmetatable(L, regid);
luaL_register(L, NULL, methods);
/* metatable["__index"] = metatable */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
}
#define L_NET_REGID "wt.net"
static int net_error(lua_State* L, libnet_t* l)
{
return luaL_error(L, "%s", libnet_geterror(l));
}
/* TODO I really regret not using the standard nil,emsg return convention... */
static int check_error(lua_State* L, libnet_t* l, int r)
{
if(r < 0) {
return net_error(L, l);
}
return r;
}
static int pushptag(lua_State* L, libnet_t* l, int ptag)
{
check_error(L, l, ptag);
lua_pushinteger(L, ptag);
return 1;
}
static uint32_t check_ip_pton(lua_State* L, const char* p, const char* name)
{
uint32_t n = 0;
if(ip_pton(p, &n) < 0) {
return luaL_error(L, "ip_pton failed on %s '%s'", name, p);
}
return n;
}
static eth_addr_t check_eth_pton(lua_State* L, const char* p, const char* name)
{
eth_addr_t n;
if(eth_pton(p, &n) < 0) {
luaL_error(L, "eth_pton failed on %s '%s'", name, p);
}
return n;
}
static const char* pushistring(lua_State* L, const char* fmt, int i)
{
char formatted[128];
snprintf(formatted, sizeof(formatted), fmt, i);
formatted[sizeof(formatted)-1] = '\0';
return lua_pushfstring(L, formatted);
}
/* check ptag argument is zero, or refers to a pblock of the expected type */
static int lnet_arg_ptag(lua_State* L, libnet_t* ud, int targ, int type)
{
int ptag = v_arg_integer_opt(L, targ, "ptag", LIBNET_PTAG_INITIALIZER);
if(ptag) {
libnet_pblock_t* pblock = libnet_pblock_find(ud, ptag);
luaL_argcheck(L, pblock, targ,
lua_pushfstring(L, "ptag %d cannot be found", ptag));
luaL_argcheck(L, pblock->type == type, targ,
lua_pushfstring(L, "ptag %d of type %s/%s is not %s/%s",
ptag,
pushistring(L, "%#x", pblock->type), libnet_diag_dump_pblock_type(pblock->type),
pushistring(L, "%#x", type), libnet_diag_dump_pblock_type(type)
));
}
return ptag;
}
static libnet_t* checkudata(lua_State* L)
{
libnet_t** ud = luaL_checkudata(L, 1, L_NET_REGID);
luaL_argcheck(L, *ud, 1, "net has been destroyed");
return *ud;
}
static libnet_pblock_t* checkpblock(lua_State* L, libnet_t* ud, int narg)
{
int ptag = luaL_checkint(L, narg);
libnet_pblock_t* pblock = libnet_pblock_find(ud, ptag);
luaL_argcheck(L, pblock, narg, "ptag cannot be found");
return pblock;
}
static const uint8_t*
checklbuffer(lua_State* L, int argt, const char* field, uint32_t* size)
{
size_t payloadsz = 0;
const char* payload = v_arg_lstring(L, argt, field, &payloadsz, "");
if(payloadsz == 0) {
payload = NULL;
}
*size = payloadsz;
return (const uint8_t*) payload;
}
static const uint8_t*
checkpayload(lua_State* L, int argt, uint32_t* size)
{
return checklbuffer(L, argt, "payload", size);
}
static int pushtagornil(lua_State* L, libnet_pblock_t* pblock)
{
if(pblock)
lua_pushinteger(L, pblock->ptag);
else
lua_pushnil(L);
return 1;
}
static void
setintfield(lua_State* L, int tindex, const char* field, int i)
{
lua_pushinteger(L, i);
lua_setfield(L, tindex, field);
}
static void
setui32field(lua_State* L, int tindex, const char* field, uint32_t i)
{
lua_pushnumber(L, i);
lua_setfield(L, tindex, field);
}
static void
setnsintfield(lua_State* L, int tindex, const char* field, uint16_t i)
{
setintfield(L, tindex, field, ntohs(i));
}
static void
setnlintfield(lua_State* L, int tindex, const char* field, uint32_t i)
{
setui32field(L, tindex, field, ntohl(i));
}
static void
setlstringfield(lua_State* L, int tindex, const char* field, const void* lstr, size_t lsiz)
{
lua_pushlstring(L, lstr, lsiz);
lua_setfield(L, tindex, field);
}
static void
setstringfield(lua_State* L, int tindex, const char* field, const char* str)
{
lua_pushstring(L, str);
lua_setfield(L, tindex, field);
}
static void
setipfield(lua_State* L, int tindex, const char* field, struct in_addr addr)
{
char addrstr[INET_ADDRSTRLEN];
lua_pushstring(L, inet_ntop(AF_INET, &addr, addrstr, sizeof(addrstr)));
lua_setfield(L, tindex, field);
}
static void
setethfield(lua_State* L, int tindex, const char* field, const uint8_t addr[ETHER_ADDR_LEN])
{
char addrstr[] = "11:22:33:44:55:66";
eth_addr_t ethaddr;
memcpy(ethaddr.data, addr, ETHER_ADDR_LEN);
setstringfield(L, tindex, field, eth_ntop(ðaddr, addrstr, sizeof(addrstr)));
}
static const char* type_string(u_int8_t type)
{
return libnet_diag_dump_pblock_type(type);
}
/*-
-- net:destroy()
Manually destroy a net object, freeing it's resources (this will happen
on garbage collection if not done explicitly).
*/
static int lnet_destroy (lua_State *L)
{
libnet_t** ud = luaL_checkudata(L, 1, L_NET_REGID);
if(*ud)
libnet_destroy(*ud);
*ud = NULL;
return 0;
}
/*-
-- net = net:clear()
Clear the current packet and all it's protocol blocks.
Return self.
*/
static int lnet_clear(lua_State* L)
{
libnet_t* ud = checkudata(L);
libnet_clear_packet(ud);
/* TODO - these bugs are fixed in libnet head */
ud->n_pblocks = 0;
ud->ptag_state = 0;
return 1;
}
/*-
-- str = net:block([ptag])
Coalesce the protocol blocks into a single chunk, and return.
If a ptag is provided, just return data of that pblock (no checksums
will be calculated).
*/
/*
FIXME - we should just have a packet function, this fails on single blocks/ptags,
and the :pblock() method is better, anyway.
*/
static int lnet_block(lua_State* L)
{
libnet_t* ud = checkudata(L);
u_int32_t len;
u_int8_t *packet = NULL;
u_int8_t *block;
int r;
r = libnet_pblock_coalesce(ud, &packet, &len);
check_error(L, ud, r);
block = packet;
if(!lua_isnoneornil(L, 2)) {
libnet_pblock_t* end = checkpblock(L, ud, 2);
libnet_pblock_t* p = ud->pblock_end;
while(p != end) {
block += p->b_len;
p = p->prev;
}
assert(p == end);
len = p->b_len;
}
lua_pushlstring(L, (char*) block, len);
libnet_adv_free_packet(ud, packet);
return 1;
}
/*-
-- net:pbuf(ptag)
*/
static int lnet_pbuf(lua_State* L)
{
libnet_t* ud = checkudata(L);
int ptag = luaL_checkint(L, 2); /* checkpblock */
const char* pbuf = (const char*)libnet_getpbuf(ud, ptag);
size_t pbufsz = libnet_getpbuf_size(ud, ptag);
if(!pbuf)
return net_error(L, ud);
lua_pushlstring(L, pbuf, pbufsz);
return 1;
}
/*-
-- net:write()
Write the packet (which must previously have been built up inside the context).
*/
static int lnet_write(lua_State *L)
{
libnet_t* ud = checkudata(L);
#ifdef NET_DUMP
lnet_dump(L);
#endif
int r = libnet_write(ud);
check_error(L, ud, r);
lua_pushinteger(L, r);
return 1;
}
/*-
-- size = net:write_link(link_pdu)
Writes link_pdu raw at the link layer, you are responsible for forming
the link-layer header.
Returns the size written on success.
*/
static int lnet_write_link (lua_State *L)
{
libnet_t* ud = checkudata(L);
size_t payloadsz_ = 0;
const char* payload_ = luaL_checklstring(L, 2, &payloadsz_);
uint32_t payloadsz = (uint32_t) payloadsz_;
const uint8_t* payload = (const uint8_t*) payload_;
int size = libnet_write_link(ud, payload, payloadsz);
check_error(L, ud, size);
lua_pushinteger(L, size);
return 1;
}
/*-
-- net:tag_below(ptag)
tag below ptag, or bottom tag if ptag is nil
*/
static int lnet_tag_below(lua_State* L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = NULL;
if(lua_isnoneornil(L, 2)) {
return pushtagornil(L, ud->pblock_end);
}
pblock = checkpblock(L, ud, 2);
return pushtagornil(L, pblock->next);
}
/*-
-- net:tag_above(ptag)
tag above ptag, or top tag if ptag is nil
*/
static int lnet_tag_above(lua_State* L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = NULL;
if(lua_isnoneornil(L, 2)) {
return pushtagornil(L, ud->protocol_blocks);
}
pblock = checkpblock(L, ud, 2);
return pushtagornil(L, pblock->prev);
}
/*- net:tag_type(ptag)
returns type of ptag, a string
*/
static int lnet_tag_type(lua_State* L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = checkpblock(L, ud, 2);
lua_pushstring(L, libnet_diag_dump_pblock_type(pblock->type));
return 1;
}
/*-
-- net:fd()
Get the fileno of the underlying file descriptor.
*/
static int lnet_getfd(lua_State* L)
{
libnet_t* ud = checkudata(L);
lua_pushinteger(L, libnet_getfd(ud));
return 1;
}
/*-
-- net:device()
Get the device name, maybe be nil.
*/
static int lnet_getdevice(lua_State* L)
{
libnet_t* ud = checkudata(L);
const char* device = libnet_getdevice(ud);
if(device)
lua_pushstring(L, device);
else
lua_pushnil(L);
return 1;
}
/* NOTE all these builders could return self, ptag, so that calls can be chained
* n:udp{...}
* :ipv4{}
* :eth{}
*/
/*-
-- ptag = net:data{payload=STR, ptag=int}
Build generic data packet inside net context.
ptag is optional, defaults to creating a new protocol block
*/
static int lnet_data (lua_State *L)
{
libnet_t* ud = checkudata(L);
uint32_t payloadsz = 0;
const uint8_t* payload = checkpayload(L, 2, &payloadsz);
int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_DATA_H);
ptag = libnet_build_data(payload, payloadsz, ud, ptag);
check_error(L, ud, ptag);
lua_pushinteger(L, ptag);
return 1;
}
/*-
-- ptag = net:igmp{type=NUM, [reserved=NUM,] ip=IP, payload=STR, ptag=int}
Build IGMPv1 packet inside net context.
Reserved is optional, and should always be 0.
ptag is optional, defaults to creating a new protocol block
*/
/* TODO type should allow strings: query, report */
static int lnet_igmp (lua_State *L)
{
libnet_t* ud = checkudata(L);
int type = v_arg_integer(L, 2, "type");
int rsv = v_arg_integer_opt(L, 2, "reserved", 0);
const char* ip = v_arg_string(L, 2, "ip");
uint32_t ip_n = check_ip_pton(L, ip, "ip");
uint32_t payloadsz = 0;
const uint8_t* payload = checkpayload(L, 2, &payloadsz);
int cksum = 0;
int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_IGMP_H);
ptag = libnet_build_igmp(type, rsv, cksum, ip_n, payload, payloadsz, ud, ptag);
check_error(L, ud, ptag);
lua_pushinteger(L, ptag);
return 1;
}
/*-
-- ptag = net:udp{src=NUM, dst=NUM, len=NUM, payload=STR, ptag=int}
Build UDP packet inside net context.
ptag is optional, defaults to creating a new protocol block
*/
static int lnet_udp (lua_State *L)
{
libnet_t* ud = checkudata(L);
int src = v_arg_integer(L, 2, "src");
int dst = v_arg_integer(L, 2, "dst");
uint32_t payloadsz = 0;
const uint8_t* payload = checkpayload(L, 2, &payloadsz);
int len = v_arg_integer_opt(L, 2, "len", LIBNET_UDP_H + payloadsz);
int cksum = 0;
int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_UDP_H);
ptag = libnet_build_udp(src, dst, len, cksum, payload, payloadsz, ud, ptag);
check_error(L, ud, ptag);
lua_pushinteger(L, ptag);
return 1;
}
/* TODO - add to libnet? */
static
libnet_pblock_t *
libnet_pblock_by_type(libnet_t* l, uint8_t type, libnet_pblock_t* p)
{
/* TODO - search from end/link layer upwards? */
if(!p)
p = l->protocol_blocks;
for (; p; p = p->next)
{
if (p->type == type)
{
return (p);
}
}
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s(): couldn't find protocol type\n", __func__);
return NULL;
}
static
libnet_pblock_t*
checkptype(lua_State* L, libnet_t* l, uint8_t type)
{
libnet_pblock_t* pblock = libnet_pblock_by_type(l, type, NULL);
if(!pblock)
luaL_error(L, "pblock type %s (%s) not found",
pushistring(L, "%#x", type), libnet_diag_dump_pblock_type(type));
return pblock;
}
/*-
-- argt = net:get_udp()
Get udp pblock argument table.
*/
/* TODO - optional ptag argument? */
static int lnet_get_udp(lua_State *L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_UDP_H);
struct libnet_udp_hdr* udp = (struct libnet_udp_hdr*) pblock->buf;
uint8_t* data = pblock->buf + LIBNET_UDP_H;
uint32_t datasz = 0;
lua_newtable(L);
setintfield(L, 2, "ptag", pblock->ptag);
setnsintfield(L, 2, "src", udp->uh_sport);
setnsintfield(L, 2, "dst", udp->uh_dport);
setnsintfield(L, 2, "_len", udp->uh_ulen);
setnsintfield(L, 2, "_sum", udp->uh_sum);
if(pblock->b_len > LIBNET_UDP_H) {
datasz = pblock->b_len - LIBNET_UDP_H;
}
setlstringfield(L, 2, "payload", data, (size_t)datasz);
return 1;
}
/*-
-- ptag = n:tcp{
-- required arguments
src=port,
dst=port,
seq=int,
flags=int,
win=int,
-- optional arguments
ack=int,
urg=int,
ptag=int,
payload=str,
options=tcp_options,
}
ptag is optional, defaults to creating a new protocol block
options is optional
*/
static int lnet_tcp (lua_State *L)
{
libnet_t* ud = checkudata(L);
int src = v_arg_integer(L, 2, "src");
int dst = v_arg_integer(L, 2, "dst");
int seq = v_arg_integer(L, 2, "seq");
int ack = v_arg_integer_opt(L, 2, "ack", 0);
int flags = v_arg_integer(L, 2, "flags");
int win = v_arg_integer(L, 2, "win");
int urg = v_arg_integer_opt(L, 2, "urg", 0);
int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_TCP_H);
uint32_t payloadsz = 0;
const uint8_t* payload = checkpayload(L, 2, &payloadsz);
int options_ptag = 0;
uint32_t optionsz = 0;
const uint8_t* options = checklbuffer(L, 2, "options", &optionsz);
int cksum = 0; /* 0 is a flag requesting libnet to fill in correct cksum */
libnet_pblock_t* oblock = NULL;
int len = 0; /* libnet needs len for checksum calculation */
oblock = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;
if(!oblock || oblock->type != LIBNET_PBLOCK_TCPO_H)
oblock = NULL;
else
options_ptag = oblock->ptag;
/* Two initial states possible:
* - has prev ip options block, or not
* Two final states desired:
* - has prev ip options block, or not
*/
if(!options) {
libnet_pblock_delete(ud, oblock);
} else {
options_ptag = libnet_build_tcp_options(options, optionsz, ud, options_ptag);
check_error(L, ud, options_ptag);
if(oblock) {
/* we replaced an existing block that was correctly placed */
} else if(ptag) {
libnet_pblock_insert_before(ud, ptag, options_ptag);
} else {
/* we just pushed a new options block, and are about to push a new ip block */
}
}
/* Rewrite len to be len of tcp pblock + previous blocks. */
{
libnet_pblock_t* p = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;
len = LIBNET_TCP_H + payloadsz;
while(p) {
/* don't count tcpdata pblock... we will replace it payloadsz data below */
if(p->type != LIBNET_PBLOCK_TCPDATA)
len += p->b_len;
p = p->prev;
}
}
ptag = libnet_build_tcp(
src, dst, seq, ack, flags, win, cksum, urg,
len,
payload, payloadsz,
ud, ptag);
check_error(L, ud, ptag);
lua_pushinteger(L, ptag);
return 1;
}
/*-
-- argt = net:get_tcp()
Get tcp pblock argument table.
*/
/* TODO - optional ptag argument? */
static int lnet_get_tcp (lua_State *L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_TCP_H);
struct libnet_tcp_hdr* hdr = (struct libnet_tcp_hdr*) pblock->buf;
libnet_pblock_t* oblock = pblock->prev;
libnet_pblock_t* dblock = pblock->prev;
/* We might have TCP options and/or data blocks, look for them. */
if(oblock && oblock->type != LIBNET_PBLOCK_TCPO_H) {
oblock = NULL;
}
if(dblock && dblock->type == LIBNET_PBLOCK_TCPO_H) {
dblock = dblock->prev;
}
if(dblock && dblock->type != LIBNET_PBLOCK_TCPDATA) {
dblock = NULL;
}
lua_newtable(L);
setintfield(L, 2, "ptag", pblock->ptag);
setnsintfield(L, 2, "src", hdr->th_sport);
setnsintfield(L, 2, "dst", hdr->th_dport);
setnlintfield(L, 2, "seq", hdr->th_seq);
setnlintfield(L, 2, "ack", hdr->th_ack);
setintfield(L, 2, "_rsv", hdr->th_x2);
setintfield(L, 2, "_off", hdr->th_off);
setintfield(L, 2, "flags", hdr->th_flags);
setnsintfield(L, 2, "win", hdr->th_win);
setnsintfield(L, 2, "_sum", hdr->th_sum);
setnsintfield(L, 2, "urg", hdr->th_urp);
if(oblock) {
setlstringfield(L, 2, "options", (const char*)oblock->buf, (size_t)oblock->b_len);
} else {
setlstringfield(L, 2, "options", "", 0);
}
if(dblock) {
setlstringfield(L, 2, "payload", (const char*)dblock->buf, (size_t)dblock->b_len);
}
return 1;
}
/*-
-- ptag = n:ipv4{
-- required arguments
src=ipaddr,
dst=ipaddr,
protocol=int,
-- optional arguments
ptag=int,
payload=str,
options=ip_options,
len=int, -- default is correct length
tos=int,
id=int,
frag=int,
ttl=int, -- defaults to 64
}
ptag is optional, defaults to creating a new protocol block
options is optional
*/
static int lnet_ipv4 (lua_State *L)
{
libnet_t* ud = checkudata(L);
const char* src = v_arg_string(L, 2, "src");
const char* dst = v_arg_string(L, 2, "dst");
uint32_t src_n = check_ip_pton(L, src, "src");
uint32_t dst_n = check_ip_pton(L, dst, "dst");
int protocol = v_arg_integer(L, 2, "protocol"); /* TODO make optional */
int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_IPV4_H);
uint32_t payloadsz = 0;
const uint8_t* payload = checkpayload(L, 2, &payloadsz);
int options_ptag = 0;
uint32_t optionsz = 0;
const uint8_t* options = checklbuffer(L, 2, "options", &optionsz);
int len = v_arg_integer_opt(L, 2, "len", -1);
int tos = v_arg_integer_opt(L, 2, "tos", 0);
int id = v_arg_integer_opt(L, 2, "id", 0);
int frag = v_arg_integer_opt(L, 2, "frag", 0);
int ttl = v_arg_integer_opt(L, 2, "ttl", 64);
int cksum = 0; /* 0 is a flag requesting libnet to fill in correct cksum */
libnet_pblock_t* oblock = NULL;
#ifdef NET_DUMP
printf("net ipv4 src %s dst %s len %d payloadsz %lu ptag %d optionsz %lu\n", src, dst, len, payloadsz, ptag, optionsz);
#endif
oblock = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;
if(!oblock || oblock->type != LIBNET_PBLOCK_IPO_H)
oblock = NULL;
else
options_ptag = oblock->ptag;
#ifdef NET_DUMP
printf(" options_ptag %d optionsz from %lu to %lu\n",
options_ptag, oblock ? oblock->b_len : 0, optionsz);
#endif
/* Two initial states possible:
* - has prev ip options block, or not
* Two final states desired:
* - has prev ip options block, or not
*/
if(!options) {
libnet_pblock_delete(ud, oblock);
} else {
options_ptag = libnet_build_ipv4_options(options, optionsz, ud, options_ptag);
check_error(L, ud, options_ptag);
if(oblock) {
/* we replaced an existing block that was correctly placed */
} else if(ptag) {
libnet_pblock_insert_before(ud, ptag, options_ptag);
} else {
/* we just pushed a new options block, and are about to push a new ip block */
}
}
/* If len unspecified, rewrite it to be len of ipv4 pblock + previous blocks. */
/* FIXME I don't think defaulting to end is correct
-- libnet doesn't have a generic icmp construction api, see bug#1373
local function build_icmp(n, icmp)
local typecode = string.char(assert(icmp.type), assert(icmp.code))
local data = icmp.data or ""
local checksum = net.checksum(typecode, "\0\0", data)
local packet = typecode..checksum..data
return n:ipv4{
src = arg.localip,
dst = arg.dutip,
protocol = 1, -- ICMP is protocol 1 FIXME get from iana.ip.types.icmp
payload = packet,
len = 20 + #packet,
ptag = icmp.ptag
}
end
getmetatable(n).icmp = build_icmp
-- set up the pblock stack, top to bottom
local ptag = n:icmp{type=0, code=0}
n:eth{src=arg.localmac, dst=arg.dutmac}
n:icmp{ptag=ptag, type=type, code=code, payload=data}
print(n:dump())
print(n:get_ipv4())
~/w/wt/achilles-engine/data/Plugins/Grammar % sudo ./icmp-data-grammar-l2 dutip=1.1.1.1 localdev=lo localip=2.2.2.2 dutmac=11:11:11:11:11:11 localmac=22:22:22:22:22:22 pcap=pc.pcap
tag 2 flags 0 type ipdata/0xf buf 0x6541e0 b_len 4 h_len 4 copied 4 prev -1 next 1
tag 1 flags 1 type ipv4/0xd buf 0x6582f0 b_len 20 h_len 20 copied 20 prev 2 next 3
tag 3 flags 0 type eth/0x4 buf 0x647580 b_len 14 h_len 0 copied 14 prev 1 next -1
link_offset 14 aligner 0 total_size 38 nblocks 3
Total:1
Subtest 1: ICMP type 0 code 1 with payload size 1
tag 2 flags 0 type ipdata/0xf buf 0x6541e0 b_len 4 h_len 4 copied 4 prev -1 next 1
tag 1 flags 1 type ipv4/0xd buf 0x6582f0 b_len 20 h_len 20 copied 20 prev 2 next 3
tag 3 flags 0 type eth/0x4 buf 0x647580 b_len 14 h_len 0 copied 14 prev 1 next -1
link_offset 14 aligner 0 total_size 38 nblocks 3
{
ptag = 1, protocol = 1, _iphl = 5, id = 0, options = "", dst = "1.1.1.1", src = "2.2.2.2", _sum = 0, _ipv = 4, tos = 0, _len = 28, ttl = 64, frag = 0
}
============>> note that _len is 28, it should be 24
*/
if(len < 0) {
libnet_pblock_t* p = ptag ? libnet_pblock_find(ud, ptag)->prev : ud->pblock_end;
len = LIBNET_IPV4_H + payloadsz;
while(p) {
len += p->b_len;
p = p->prev;
}
}
ptag = libnet_build_ipv4(
len, tos, id, frag, ttl, protocol, cksum,
src_n, dst_n,
payload, payloadsz,
ud, ptag);
check_error(L, ud, ptag);
lua_pushinteger(L, ptag);
return 1;
}
/*-
-- argt = net:get_ipv4()
Get ipv4 pblock argument table.
*/
/* TODO - optional ptag argument? */
static int lnet_get_ipv4 (lua_State *L)
{
libnet_t* ud = checkudata(L);
libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_IPV4_H);
struct libnet_ipv4_hdr* ip = (struct libnet_ipv4_hdr*) pblock->buf;
libnet_pblock_t* oblock = pblock->prev;
if(oblock && oblock->type != LIBNET_PBLOCK_IPO_H) {
oblock = NULL;
}
lua_newtable(L);
setintfield(L, 2, "ptag", pblock->ptag);