-
Notifications
You must be signed in to change notification settings - Fork 0
/
pctest.c
2639 lines (2271 loc) · 75.4 KB
/
pctest.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
/*
* TODO:
* IP:
* - IP options (security, source route, record route, timestamp)
* - traceroute
*
* TCP:
* - TCP options (mss, sack, wscale, timestamp)
* - "connection synchronization using data-carrying segments"
* - URG
* - Better initial seqno generation
* - slow start
* - congestion avoidance
* - tx queue (that obeys remote window size)
* - Grow/shrink windows
* - retransmission/timers (Karn, Jacobson, exp. backoff, etc.)
* - delayed/selective ACKs
* - header prediction
*
* General:
* - make sure not using loopback device
* - better CLI (history would be nice, at least, maybe in color)
* - would be nice if we could drop root privs again (dhcp setting
* host arp late and possibly changing long after we start running,
* and wanting to remove it when quitting, makes that difficult)
*
* And finally, all of the evil things I'd like to do:
* - Increased control over remote window size
*/
#include <libnet.h>
#include <net/if_arp.h>
#include <pcap.h>
/* LIST { */
typedef struct _list {
struct _list *next;
void *data;
} list;
typedef int (*cmpfnc)(const void *, const void *);
static list *
list_new(void *data)
{
list *l = malloc(sizeof (list));
if (!l) return NULL;
l->next = NULL;
l->data = data;
return l;
}
static list *
list_prepend(list *l, void *data)
{
list *s = list_new(data);
s->next = l;
return s;
}
static list *
list_remove(list *l, void *data)
{
list *s = l, *p = NULL;
if (!s) return NULL;
if (s->data == data) {
p = s->next;
free(s);
return p;
}
while (s->next) {
p = s;
s = s->next;
if (s->data == data) {
p->next = s->next;
free(s);
return l;
}
}
return l;
}
static list *
list_insert_sorted(list *l, void *data, cmpfnc f)
{
list *s = l;
if (!s)
return list_prepend(l, data);
if (f(s->data, data) >= 0)
return list_prepend(l, data);
while (s->next) {
if (f(s->next->data, data) < 0) {
s = s->next;
continue;
}
s->next = list_prepend(s->next, data);
return l;
}
s->next = list_new(data);
return l;
}
/* } */
/* TIMER { */
struct timer {
struct timeval end;
void (*func)(void *);
void *arg;
};
static list *timers = NULL;
static int
timer_cmp(const void *x, const void *y)
{
const struct timer *a = x, *b = y;
return timercmp(&a->end, &b->end, -);
}
static struct timer *
timer_start(int ms, void (*func)(void *), void *arg)
{
struct timer *timer;
struct timeval tv;
timer = malloc(sizeof (struct timer));
if (!timer)
return NULL;
gettimeofday(&tv, NULL);
timer->func = func;
timer->arg = arg;
timer->end.tv_sec = tv.tv_sec + (ms / 1000);
timer->end.tv_usec = tv.tv_usec + ((ms % 1000) * 1000);
timers = list_insert_sorted(timers, timer, timer_cmp);
return timer;
}
static void
timer_cancel(struct timer *timer)
{
timers = list_remove(timers, timer);
free(timer);
}
static struct timeval *
timer_sleep_time(struct timeval *rtv)
{
if (timers) {
/* timers are sorted by when they're going to expire (first to last), so
* taking the first on the list should always be the first to expire and
* so we can use it to figure out how long to tell select to sleep */
struct timer *timer = timers->data;
struct timeval tv;
gettimeofday(&tv, NULL);
timersub(&timer->end, &tv, rtv);
if (rtv->tv_sec < 0)
timerclear(rtv);
return rtv;
} else {
return NULL;
}
}
static void
timer_process_pending(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
/* we don't support periodic timers, and since timers are sorted by
* expiration date, if we come across one that doesn't need processing, all
* subsequent ones won't need processing, so we can return. */
while (timers) {
struct timer *timer = timers->data;
/* we get away with this because the timeval is the first element
* of the timer */
if (timer_cmp(timer, &tv) <= 0) {
if (timer->func)
timer->func(timer->arg);
timer_cancel(timer);
} else {
return;
}
}
}
/* } */
/* these are our global variables */
static libnet_t *lnh_link = NULL;
static pcap_t *lph;
static unsigned char src_hw[ETHER_ADDR_LEN];
static uint32_t src_ip = 0;
static uint32_t src_mask = 0;
static uint32_t bcast_ip = 0;
static uint32_t host_ip = 0;
static u_int16_t ip_id;
static const u_int8_t ip_ttl = 64;
static list *sessions = NULL;
static list *open_connections = NULL;
static list *listeners = NULL;
/* ARP { */
struct arp_ent {
uint32_t ip; /* network byte order */
unsigned char hw[ETHER_ADDR_LEN];
} __attribute__ ((__packed__));
static list *arp_cache = NULL;
/* this probably wasn't entirely necessary but it's convenient */
struct arp_pkt {
struct libnet_ethernet_hdr enet;
struct libnet_arp_hdr hdr;
/* everything from here down is IPv4 but that's all we need */
unsigned char src_hw[ETHER_ADDR_LEN];
uint32_t src_ip;
unsigned char dst_hw[ETHER_ADDR_LEN];
uint32_t dst_ip;
} __attribute__ ((__packed__));
static void
cache_arp(uint32_t ip, unsigned char *hw)
{
list *l = arp_cache;
struct arp_ent *ent;
while (l) {
ent = l->data;
if (ent->ip == ip) {
if (memcmp(ent->hw, hw, ETHER_ADDR_LEN)) {
printf("HW addr for %s changed\n",
libnet_addr2name4(ip, LIBNET_DONT_RESOLVE));
memcpy(ent->hw, hw, ETHER_ADDR_LEN);
}
return;
}
l = l->next;
}
printf("caching arp for %s\n", libnet_addr2name4(ip, LIBNET_DONT_RESOLVE));
ent = malloc(sizeof (struct arp_ent));
ent->ip = ip;
memcpy(ent->hw, hw, ETHER_ADDR_LEN);
arp_cache = list_prepend(arp_cache, ent);
}
static unsigned char *
arp_lookup(uint32_t ip)
{
list *l = arp_cache;
while (l) {
struct arp_ent *ent = l->data;
l = l->next;
if (ent->ip == ip) {
return ent->hw;
}
}
return NULL;
}
/* since we're "faking" a client, we need to reply to arp requests as that
* client. we need to make sure that we don't send arp replies to the host,
* because that will just confuse it. sending arp requests to it also confuses
* it, but at least it doesn't do anything. */
static void
arp_reply(uint32_t dst_ip, u_int8_t *dst_hw)
{
printf("replying to arp from %s\n",
libnet_addr2name4(dst_ip, LIBNET_DONT_RESOLVE));
libnet_clear_packet(lnh_link);
if (libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, ETHER_ADDR_LEN, 4,
ARPOP_REPLY, src_hw, (u_char *)&src_ip, dst_hw,
(u_char *)&dst_ip, NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
if (libnet_autobuild_ethernet(dst_hw, ETHERTYPE_ARP, lnh_link) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
if (libnet_write(lnh_link) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
}
static void
process_arp_packet(struct libnet_ethernet_hdr *enet, uint32_t len)
{
struct arp_pkt *arp = (struct arp_pkt *)enet;
if (len < LIBNET_ETH_H + LIBNET_ARP_ETH_IP_H)
return;
if ((ntohs(arp->hdr.ar_hrd) != ARPHRD_ETHER) || /* ethernet*/
(ntohs(arp->hdr.ar_pro) != ETHERTYPE_IP) || /* ipv4 */
(arp->dst_ip != src_ip) || /* for us */
memcmp(src_hw, arp->src_hw, ETHER_ADDR_LEN) == 0) /* not host */
return;
cache_arp(arp->src_ip, arp->src_hw);
if (ntohs(arp->hdr.ar_op) == ARPOP_REQUEST) {
arp_reply(arp->src_ip, arp->src_hw);
}
}
static void
arp_request(uint32_t dst_ip)
{
u_int8_t hw[ETHER_ADDR_LEN];
printf("sending arp request for %s\n",
libnet_addr2name4(dst_ip, LIBNET_DONT_RESOLVE));
libnet_clear_packet(lnh_link);
memset(hw, 0, ETHER_ADDR_LEN);
if (libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, ETHER_ADDR_LEN, 4,
ARPOP_REQUEST, src_hw, (u_char *)&src_ip, hw,
(u_char *)&dst_ip, NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
memset(hw, 0xFF, ETHER_ADDR_LEN);
if (libnet_autobuild_ethernet(hw, ETHERTYPE_ARP, lnh_link) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
if (libnet_write(lnh_link) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return;
}
}
/* } */
/* ROUTING { */
/* everything here is network byte order */
struct route_row {
uint32_t dest;
uint32_t mask;
uint32_t gw;
};
static list *routing_table = NULL;
static int
route_cmp(const void *x, const void *y)
{
const struct route_row *a = x, *b = y;
/* the bigger (i.e. more specific) mask should come first */
if (a->mask > b->mask) {
return -1;
} else if (a->mask < b->mask) {
return 1;
} else {
/* otherwise, just sort by destination */
return a->dest - b->dest;
}
}
static int
route_add(uint32_t dest, uint32_t mask, uint32_t gw)
{
struct route_row *rt;
/* should we validate our input? well, probably. */
uint32_t bit = 1 << 31, hmask = ntohl(mask), i = 1;
while (bit) {
if ((hmask & bit) == 0) {
i = 0;
} else if (i != 1) {
fprintf(stderr, "invalid mask %08x\n", hmask);
return 1;
}
bit >>= 1;
}
if ((dest & ~mask) != 0) {
fprintf(stderr, "invalid dest/mask pair\n");
return 1;
}
rt = malloc(sizeof (struct route_row));
rt->dest = dest;
rt->mask = mask;
rt->gw = gw;
routing_table = list_insert_sorted(routing_table, rt, route_cmp);
if (dest == 0 && mask == 0) {
/* if it's the default gateway, get its mac now. we're likely going to
* have to eventually anyway. also, it's a handy way of seeing
* immediately whether the setup is at least somewhat correct. */
arp_request(gw);
}
return 0;
}
static int
send_ethernet(uint32_t dst_ip)
{
list *l = routing_table;
unsigned char *dst_hw = NULL, *snd_hw = src_hw, broadcast[ETHER_ADDR_LEN];
memset(broadcast, 0xff, ETHER_ADDR_LEN);
if (dst_ip == 0 || dst_ip == 0xFFFFFFFF || dst_ip == bcast_ip) {
dst_hw = broadcast;
} else if (dst_ip == src_ip) {
dst_hw = src_hw;
}
while (dst_hw == NULL && l) {
struct route_row *rt = l->data;
l = l->next;
if ((dst_ip & rt->mask) != rt->dest) {
continue;
}
if ((dst_ip & ~rt->mask) == ~rt->mask) {
dst_hw = broadcast;
} else {
if (rt->gw != 0) {
dst_ip = rt->gw;
} else if (dst_ip == host_ip) {
/* the outbound side of the talk-to-the-host hack. if we're
* sending to the host, send as broadcast to the default
* gateway. this is really ugly, and confusing since it'll make
* the packet show up in tcpdump twice */
snd_hw = broadcast;
continue;
}
dst_hw = arp_lookup(dst_ip);
if (dst_hw == NULL) {
arp_request(dst_ip);
return -1;
}
}
}
if (dst_hw == NULL) {
fprintf(stderr, "no route to host %s\n",
libnet_addr2name4(dst_ip, LIBNET_DONT_RESOLVE));
return -1;
}
if (libnet_build_ethernet(dst_hw, snd_hw, ETHERTYPE_IP,
NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return -1;
}
if (libnet_write(lnh_link) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return -1;
}
return 0;
}
/* } */
static const char *state_names[] = {
"UNKNOWN",
"ESTABLISHED",
"SYN_SENT",
"SYN_RECV",
"FIN_WAIT1",
"FIN_WAIT2",
"TIME_WAIT",
"TCP_CLOSE",
"CLOSE_WAIT",
"LAST_ACK",
"LISTEN",
"CLOSING",
};
struct pkt_q {
uint32_t seqno;
int len; /* -1 means this is the fin */
u_char *data;
};
typedef struct tcp_session {
uint32_t id; /* heh. a more appropriate name might be 'fd'. */
uint32_t state;
/* these identify a unique session. the src_ip is global. libnet_name2addr4
* puts ip addresses in network byte order, so those will always be that
* way. however, ports stored in the session will always be host byte order,
* so when comparing against what comes off the wire, make sure to do a
* proper translation */
uint16_t src_prt;
uint16_t dst_prt;
uint32_t dst_ip;
/* these are stored in host byte order mostly by default */
uint32_t unacked; /* aka SND.UNA */
uint32_t seqno; /* aka SND.NXT */
uint32_t snd_win; /* aka SND.WND */
uint32_t iss;
uint32_t ackno; /* aka RCV.NXT */
uint32_t rcv_win; /* aka RCV.WND */
uint32_t irs;
uint32_t unused;
list *rx;
/* there's really a bunch of other stuff that I should be paying
* attention to */
/* this is where we start getting evil: reimplementing sting */
int sting;
int count;
int sting_acks;
int data_lost;
struct timer *tmr;
#define STING_STRING "GET / HTTP/1.0\n" \
"Accept: text/plain\nAccept: */*\n" \
"User-Agent: Mozilla/4.0 " \
"(compatible; MSIE 5.0; Windows NT; DigExt; Sting)\n\n"
#define STING_COUNT 100
#define STING_DELAY 100
} TCB;
struct ip_pkt {
struct libnet_ipv4_hdr *hdr;
u_char *options;
u_char *data;
int from_host;
int unused;
};
struct icmp_pkt {
struct libnet_ipv4_hdr *ip;
struct libnet_icmpv4_hdr *hdr;
};
struct tcp_pkt {
struct libnet_ipv4_hdr *ip;
u_char *ip_options;
struct libnet_tcp_hdr *hdr;
u_char *tcp_options;
u_char *data;
uint32_t data_len;
int unused;
};
/* TCP UTIL { */
static int
send_tcp(TCB *sess, int flags, u_char *data, uint32_t len)
{
libnet_clear_packet(lnh_link);
if (libnet_build_tcp(sess->src_prt, sess->dst_prt, sess->seqno, sess->ackno,
flags, sess->rcv_win, 0, 0, LIBNET_TCP_H + len, data,
len, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return 1;
}
if (libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + len, IPTOS_RELIABILITY,
ip_id++, 0, ip_ttl, IPPROTO_TCP, 0, src_ip,
sess->dst_ip, NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return 1;
}
if (send_ethernet(sess->dst_ip) == -1) {
return 1;
}
if (flags & TH_SYN)
sess->seqno++;
if (flags & TH_FIN)
sess->seqno++;
sess->seqno += len;
return 0;
}
/* eventually send_tcp is going to do nice things like queuing the data to be
* sent and setting timers so it can retry if it hasn't been ack'd, so it makes
* sense to have a separate function just for sending a RST. or at least, that's
* what I keep telling myself. */
static int
send_rst(uint32_t state, struct tcp_pkt *tcp)
{
uint32_t ackno = ntohl(tcp->hdr->th_seq) + 1;
u_int8_t cntrl = TH_RST;
if (!(tcp->hdr->th_flags & TH_ACK))
cntrl |= TH_ACK;
else
ackno = 0;
libnet_clear_packet(lnh_link);
if (libnet_build_tcp(ntohs(tcp->hdr->th_dport), ntohs(tcp->hdr->th_sport),
ntohl(tcp->hdr->th_ack), ackno, cntrl, 0, 0, 0,
LIBNET_TCP_H, NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return 1;
}
if (libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H, IPTOS_LOWDELAY, ip_id++,
0, ip_ttl, IPPROTO_TCP, 0, src_ip,
tcp->ip->ip_src.s_addr, NULL, 0, lnh_link, 0) == -1) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
return 1;
}
if (send_ethernet(tcp->ip->ip_src.s_addr) == -1) {
return 1;
}
fprintf(stderr, "RST'ing (SRC = %s:%d, DPort = %d (%s), Cntrl = %02x)\n",
libnet_addr2name4(tcp->ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
ntohs(tcp->hdr->th_sport), ntohs(tcp->hdr->th_dport),
state_names[state], tcp->hdr->th_flags);
return 0;
}
static TCB *
find_session(uint32_t dst_ip, uint32_t dst_prt, uint32_t src_prt)
{
list *l = open_connections;
while (l && dst_ip && dst_prt) {
TCB *sess = l->data;
l = l->next;
if (sess->src_prt != src_prt)
continue;
if (sess->dst_ip == dst_ip && sess->dst_prt == dst_prt)
return sess;
}
l = listeners;
/* TODO: we should support listening for specific hosts and/or ports */
while (l) {
TCB *sess = l->data;
l = l->next;
if (sess->src_prt == src_prt)
return sess;
}
return NULL;
}
/* XXX there are probably several problems with this. */
static int
get_port(uint32_t dst_ip, uint16_t dst_prt)
{
uint16_t src_prt;
do {
/* privileged ports, my ass */
src_prt = libnet_get_prand(LIBNET_PRu16);
} while (find_session(dst_ip, dst_prt, src_prt));
return src_prt;
}
static int
get_next_sess_id(void)
{
list *l = sessions;
unsigned int i = 0;
while (l) {
TCB *s = l->data;
l = l->next;
if (s->id != i)
return i;
i++;
}
return i;
}
static int
sess_cmp(const void *x, const void *y)
{
const TCB *a = x, *b = y;
return a->id - b->id;
}
static TCB *
session_setup(void)
{
TCB *sess = calloc(1, sizeof (TCB));
/* sess->state = TCP_CLOSE; */
/* XXX this isn't actually supposed to be random; it's supposed to be
* somewhat internal-clock-based so that if we happen to be using the same
* src_prt/dst_ip/dst_prt tuple that was used in a previous session, the
* remote stack knows that it's for a new session. but I don't care. */
/* I hear I shouldn't use this function for anything real. oh well. */
sess->seqno = libnet_get_prand(LIBNET_PRu32);
/* XXX What you see is what you get. This is the only place the receive
* window is modified. Setting it to 0 makes most hosts annoyed. And who can
* blame them? The person they're talking to isn't listening to them. */
sess->rcv_win = 0xffff;
sess->id = get_next_sess_id();
sessions = list_insert_sorted(sessions, sess, sess_cmp);
printf("creating socket %d\n", sess->id);
return sess;
}
static TCB *
accept_session(TCB *listener, struct tcp_pkt *pkt)
{
TCB *sess = session_setup();
if (!sess)
return NULL;
sess->dst_ip = pkt->ip->ip_src.s_addr;
sess->dst_prt = ntohs(pkt->hdr->th_sport);
sess->src_prt = listener->src_prt;
sess->irs = ntohl(pkt->hdr->th_seq);
sess->ackno = sess->irs + 1;
send_tcp(sess, TH_SYN | TH_ACK, NULL, 0);
sess->state = TCP_SYN_RECV;
printf("%u: %s\n", sess->id, state_names[sess->state]);
open_connections = list_prepend(open_connections, sess);
return sess;
}
static void
free_txrx_queues(TCB *sess)
{
while (sess->rx) {
struct pkt_q *pq = sess->rx->data;
sess->rx = list_remove(sess->rx, pq);
if (pq->data)
free(pq->data);
free(pq);
}
}
/* after this function, sess is no longer valid (obviously) */
static void
remove_session(TCB *sess)
{
if (sess->state == TCP_LISTEN) {
listeners = list_remove(listeners, sess);
} else {
open_connections = list_remove(open_connections, sess);
}
sessions = list_remove(sessions, sess);
free_txrx_queues(sess);
timer_cancel(sess->tmr);
free(sess);
}
/* TODO: maybe eventually you can specify the port to connect from */
static TCB *
create_session(char *host, uint16_t port)
{
TCB *sess;
if (host) {
if ((sess = session_setup()) == NULL)
return NULL;
sess->dst_ip = libnet_name2addr4(lnh_link, host, LIBNET_RESOLVE);
if (sess->dst_ip == 0xffffffff) {
fprintf(stderr, "%s", libnet_geterror(lnh_link));
remove_session(sess);
return NULL;
}
sess->dst_prt = port;
sess->src_prt = get_port(sess->dst_ip, sess->dst_prt);
/* send the SYN, and we're off! */
send_tcp(sess, TH_SYN, NULL, 0);
sess->state = TCP_SYN_SENT;
printf("%u: %s (port %u)\n", sess->id, state_names[sess->state],
sess->src_prt);
open_connections = list_prepend(open_connections, sess);
} else {
/* listening socket */
if ((sess = find_session(0, 0, port)) != NULL) {
fprintf(stderr, "id %d already listening on port %d\n",
sess->id, port);
return sess;
}
if ((sess = session_setup()) == NULL)
return NULL;
sess->dst_ip = 0;
sess->dst_prt = 0;
sess->src_prt = port;
sess->state = TCP_LISTEN;
printf("%u: %s\n", sess->id, state_names[sess->state]);
listeners = list_prepend(listeners, sess);
}
return sess;
}
/* } */
/* STING { */
static void
sting(void *data)
{
TCB *sess = data;
u_char str[] = STING_STRING;
sess->count++;
send_tcp(sess, TH_PUSH | TH_ACK, &str[sess->count], 1);
if (sess->count != STING_COUNT) {
sess->tmr = timer_start(STING_DELAY, sting, data);
} else {
/* so now we send the last byte */
sess->tmr = NULL;
sess->seqno = sess->iss;
send_tcp(sess, TH_PUSH | TH_ACK, &str[0], 1);
sess->seqno = sess->iss + STING_COUNT + 1;
}
}
static void
sting_process(TCB *sess, struct tcp_pkt *pkt)
{
u_char str[] = STING_STRING;
if (sess->state != TCP_ESTABLISHED) {
fprintf(stderr, "sting session stopping?\n");
timer_cancel(sess->tmr);
return;
}
if (pkt->data_len != 0) {
/* if there's data, don't pay attention to the packet */
return;
} else if (ntohl(pkt->hdr->th_ack) == sess->iss) {
/* they're still waiting for our first byte */
sess->sting_acks++;
} else if (ntohl(pkt->hdr->th_ack) == sess->seqno) {
printf("sting to %s:\n",
libnet_addr2name4(pkt->ip->ip_src.s_addr, LIBNET_DONT_RESOLVE));
printf("remote received %d/%d packets\n", STING_COUNT - sess->data_lost,
STING_COUNT);
printf("we received %d/%d acks\n", sess->sting_acks,
STING_COUNT - sess->data_lost);
send_tcp(sess, TH_RST | TH_ACK, NULL, 0);
remove_session(sess);
} else {
/* this indicates that they dropped something and need it resent */
sess->data_lost++;
sess->seqno = ntohl(pkt->hdr->th_ack);
send_tcp(sess, TH_PUSH | TH_ACK, &str[sess->seqno - sess->iss], 1);
sess->seqno = sess->iss + STING_COUNT + 1;
}
}
/* } */
/* TCP SM { */
static TCB *
tcp_process_listen(TCB *sess, struct tcp_pkt *pkt)
{
if (pkt->hdr->th_flags & TH_RST) {
/* we're in a listening state, just drop it */
} else if (pkt->hdr->th_flags & TH_ACK) {
send_rst(sess->state, pkt);
} else if (pkt->hdr->th_flags & TH_SYN) {
return accept_session(sess, pkt);
} else {
/* And I quote:
*
* Any other control or text-bearing segment (not containing SYN)
* must have an ACK and thus would be discarded by the ACK
* processing. An incoming RST segment could not be valid, since it
* could not have been sent in response to anything sent by this
* incarnation of the connection. So you are unlikely to get here,
* but if you do, drop the segment, and return. */
}
return NULL;
}
static void
tcp_process_syn_sent(TCB *sess, struct tcp_pkt *pkt)
{
if (pkt->hdr->th_flags & TH_ACK) {
if (ntohl(pkt->hdr->th_ack) != sess->seqno) {
fprintf(stderr, "Invalid ackno on %d\n", sess->id);
send_rst(sess->state, pkt);
remove_session(sess);
return;
} else {
sess->unacked = ntohl(pkt->hdr->th_ack);
}
}
/* by now we've already handled RST and ACK. any other packet without SYN
* should just be dropped */
if (!(pkt->hdr->th_flags & TH_SYN))
return;
sess->irs = ntohl(pkt->hdr->th_seq);
sess->ackno = sess->irs + 1;
if (sess->unacked == sess->seqno) {
sess->state = TCP_ESTABLISHED;
send_tcp(sess, TH_ACK, NULL, 0);
} else {
sess->state = TCP_SYN_RECV;
/* we're resending our initial syn, so we have to decrement the
* seqno to get at our initial seqno for the syn */
sess->seqno--;
send_tcp(sess, TH_SYN | TH_ACK, NULL, 0);
}
printf("%u: %s\n", sess->id, state_names[sess->state]);
/* we assume that sting will always go this route */
if ((sess->state == TCP_ESTABLISHED) && sess->sting) {
/* bump seqno deliberately despite not having sent anything yet */
sess->seqno++;
sting(sess);
}
}
static int
tcp_check_seqno(TCB *sess, struct tcp_pkt *pkt)
{
unsigned int len = ntohs(pkt->ip->ip_len) -
(pkt->ip->ip_hl << 2) - (pkt->hdr->th_off << 2);
uint32_t rcvnxt = sess->ackno, segseq = ntohl(pkt->hdr->th_seq);
if (len == 0) {
if ((0 <= (int32_t)(segseq - rcvnxt)) &&
/* if sess->rcv_win is 0 then the check is if
* pkt->hdr->th_seq <= sess->ackno, which is acceptable,
* since the above check makes it the same check
* as checking if sess->ackno == pkt->hdr->th_seq */
(0 <= (int32_t)(rcvnxt + sess->rcv_win - segseq))) {
return 0;
}
} else if (sess->rcv_win != 0) {
if (((0 <= (int32_t)(segseq - rcvnxt)) &&
(0 < (int32_t)(rcvnxt + sess->rcv_win - segseq))) ||
((0 <= (int32_t)(segseq + len - 1 - rcvnxt)) &&
(0 < (int32_t)(rcvnxt + sess->rcv_win - (segseq + len - 1))))) {
return 0;
}
}
/* if there's data and our window is 0 then it's unacceptable */
return 1;
}
static int
tcp_process_syn_recv(TCB *sess, struct tcp_pkt *pkt)
{
if (sess->unacked > ntohl(pkt->hdr->th_ack) ||
ntohl(pkt->hdr->th_ack) > sess->seqno) {
fprintf(stderr, "Invalid ackno on %d\n", sess->id);
send_rst(sess->state, pkt);
remove_session(sess);
return 1;
}
sess->state = TCP_ESTABLISHED;
printf("%u: %s\n", sess->id, state_names[sess->state]);
/* and continue procesing */