forked from wolfSSL/wolfip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwolfip.c
More file actions
4022 lines (3734 loc) · 130 KB
/
wolfip.c
File metadata and controls
4022 lines (3734 loc) · 130 KB
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
/* wolfip.c
*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfIP TCP/IP stack.
*
* wolfIP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfIP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#ifdef WOLF_POSIX
#include <poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "wolfip.h"
#include "config.h"
#if WOLFIP_ENABLE_LOOPBACK
#define WOLFIP_LOOPBACK_IF_IDX 0U
#define WOLFIP_PRIMARY_IF_IDX 1U
#define WOLFIP_LOOPBACK_IP 0x7F000001U
#define WOLFIP_LOOPBACK_MASK 0xFF000000U
static inline int wolfIP_is_loopback_if(unsigned int if_idx)
{
return if_idx == WOLFIP_LOOPBACK_IF_IDX;
}
#else
#define WOLFIP_LOOPBACK_IF_IDX 0U
#define WOLFIP_PRIMARY_IF_IDX 0U
static inline int wolfIP_is_loopback_if(unsigned int if_idx)
{
(void)if_idx;
return 0;
}
#endif
#define WOLFIP_CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#if WOLFIP_ENABLE_LOOPBACK
static int wolfIP_loopback_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len);
#endif
static void wolfIP_recv_on(struct wolfIP *s, unsigned int if_idx, void *buf, uint32_t len);
struct wolfIP_eth_frame;
struct wolfIP_ip_packet;
struct wolfIP_tcp_seg;
struct wolfIP_udp_datagram;
struct wolfIP_icmp_packet;
/* Fixed size binary heap: each element is a timer. */
#define MAX_TIMERS MAX_TCPSOCKETS * 3
/* Constants */
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
#define ICMP_TTL_EXCEEDED 11
#define WI_IPPROTO_ICMP 0x01
#define WI_IPPROTO_TCP 0x06
#define WI_IPPROTO_UDP 0x11
#define IPADDR_ANY 0x00000000
#define TCP_OPTION_MSS 0x02
#define TCP_OPTION_MSS_LEN 4
#define TCP_OPTION_TS 0x08
#define TCP_OPTION_TS_LEN 10
#define TCP_OPTIONS_LEN 12
#define TCP_OPTION_NOP 0x01
#define TCP_OPTION_EOO 0x00
#define TCP_HEADER_LEN 20
#define IP_HEADER_LEN 20
#define UDP_HEADER_LEN 8
#define ICMP_HEADER_LEN 8
#define ARP_HEADER_LEN 28
#ifdef ETHERNET
#define ETH_HEADER_LEN 14
#else
#define ETH_HEADER_LEN 0
#endif
#define ETH_TYPE_IP 0x0800
#define ETH_TYPE_ARP 0x0806
#define NO_TIMER 0
#define WI_IP_MTU 1500
#define TCP_MSS (WI_IP_MTU - (IP_HEADER_LEN + TCP_HEADER_LEN))
/* Macros */
#define IS_IP_BCAST(ip) (ip == 0xFFFFFFFF)
#define PKT_FLAG_SENT 0x01
#define PKT_FLAG_ACKED 0x02
#define PKT_FLAG_FIN 0x04
/* Random number generator, provided by the user */
//extern uint32_t wolfIP_getrandom(void);
struct PACKED pkt_desc {
uint32_t pos, len;
uint16_t flags, time_sent;
};
struct fifo {
uint32_t head, tail, size, h_wrap;
uint8_t *data;
};
/* TCP TX is a circular buffer and contains an array of full packets */
/* TCP RX only contains application data */
/* FIFO functions
* head: next empty slot
* tail: oldest populated slot
*
* */
/* Initialize a FIFO */
static void fifo_init(struct fifo *f, uint8_t *data, uint32_t size)
{
f->head = 0;
f->tail = 0;
f->h_wrap = 0;
f->size = size;
f->data = data;
}
/* Return the number of bytes available */
static uint32_t fifo_space(struct fifo *f)
{
int ret = 0;
if (f->head == f->tail) {
f->head = 0;
f->tail = 0;
return f->size;
}
if (f->tail == f->h_wrap) {
f->tail = 0;
f->h_wrap = 0;
}
if (f->h_wrap == 0) {
if (f->head >= f->tail) {
ret = f->size - (f->head - f->tail);
} else {
ret = f->tail - f->head;
}
/* Take into account the wraparound to always keep the segment contiguous */
if ((f->size - f->head) < (sizeof(struct pkt_desc) + LINK_MTU)) {
if (f->tail > (sizeof(struct pkt_desc) + LINK_MTU)) {
f->h_wrap = f->head;
f->head = 0;
return f->tail - f->head;
} else return 0;
}
} else {
ret = f->size - (f->tail - f->head);
}
return ret;
}
/* Check the descriptor of the next packet */
static struct pkt_desc *fifo_peek(struct fifo *f)
{
if (f->tail == f->h_wrap) {
f->tail = 0;
f->h_wrap = 0;
}
if (f->tail == f->head)
return NULL;
while (f->tail % 4)
f->tail++;
if ((f->head < f->tail) && ((f->tail + sizeof(struct pkt_desc) + LINK_MTU > f->size)))
f->tail = 0;
return (struct pkt_desc *)((uint8_t *)f->data + f->tail);
}
/* Continue reading starting from a descriptor returned by fifo_peek */
static struct pkt_desc *fifo_next(struct fifo *f, struct pkt_desc *desc)
{
uint32_t len;
if (desc == NULL)
return NULL;
len = sizeof(struct pkt_desc) + desc->len;
if ((desc->pos + len) == f->head)
return NULL;
while ((desc->pos + len) % 4)
len++;
if ((desc->pos + len + sizeof(struct pkt_desc) + LINK_MTU ) >= f->size)
desc = (struct pkt_desc *)((uint8_t *)f->data);
else
desc = (struct pkt_desc *)((uint8_t *)f->data + desc->pos + len);
if ((desc->pos + len) == f->h_wrap) {
desc = (struct pkt_desc *)((uint8_t *)f->data);
}
return desc;
}
/* Return the number of bytes used */
static uint32_t fifo_len(struct fifo *f)
{
while (f->tail % 4)
f->tail++;
f->tail %= f->size;
if (f->tail == f->head)
return 0;
if (f->tail > f->head) {
if (f->h_wrap > 0)
return f->h_wrap - f->tail + f->head;
else
return f->size - (f->tail - f->head);
} else {
return f->head - f->tail;
}
}
/* Insert data into the FIFO */
static int fifo_push(struct fifo *f, void *data, uint32_t len)
{
struct pkt_desc desc;
memset(&desc, 0, sizeof(struct pkt_desc));
/* Ensure 4-byte alignment in the buffer */
if (f->head % 4)
f->head += 4 - (f->head % 4);
if (fifo_space(f) < (sizeof(struct pkt_desc) + len))
return -1;
desc.pos = f->head;
desc.len = len;
memcpy((uint8_t *)f->data + f->head, &desc, sizeof(struct pkt_desc));
f->head += sizeof(struct pkt_desc);
memcpy((uint8_t *)f->data + f->head, data, len);
f->head += len;
return 0;
}
/* Grab the tail packet and advance the tail pointer */
static struct pkt_desc *fifo_pop(struct fifo *f)
{
struct pkt_desc *desc;
if (f->tail == f->head)
return NULL;
while (f->tail % 4)
f->tail++;
f->tail %= f->size;
if (f->tail == f->head)
return NULL;
if ((f->head < f->tail) && ((f->tail + sizeof(struct pkt_desc) + LINK_MTU > f->size)))
f->tail = 0;
desc = (struct pkt_desc *)((uint8_t *)f->data + f->tail);
f->tail += sizeof(struct pkt_desc) + desc->len;
f->tail %= f->size;
return desc;
}
/* Simple queue structure for TCP RX, keeping only the data in the buffer */
struct queue {
uint32_t seq_base, head, tail, size;
uint8_t *data;
};
/* Initialize a queue */
/* head: next empty slot
* tail: oldest populated slot
*/
static void queue_init(struct queue *q, uint8_t *data, uint32_t size, uint32_t seq_base)
{
q->seq_base = seq_base;
q->tail = 0;
q->head = 0;
q->size = size;
q->data = data;
}
/* Return the number of bytes available */
static uint32_t queue_space(struct queue *q)
{
if (q->head >= q->tail) {
return q->size - (q->head - q->tail);
} else {
return q->tail - q->head;
}
}
/* Return the number of bytes used */
static uint32_t queue_len(struct queue *q)
{
return q->size - queue_space(q);
}
/* Insert data into the queue */
static int queue_insert(struct queue *q, void *data, uint32_t seq, uint32_t len)
{
uint32_t pos;
int diff;
if ((len > queue_space(q)) || (len > q->size)) {
return -1;
}
if (queue_len(q) == 0) {
q->tail = q->head = 0;
memcpy(q->data, data, len);
q->head = len;
q->seq_base = seq;
} else {
diff = seq - q->seq_base;
if (diff < 0)
return -1;
pos = (uint32_t)diff;
if (pos > q->size)
return -1;
/* Check if the data is ancient */
if (pos < q->tail)
return 0;
/* Write in two steps: consider wrapping */
if (pos + len > q->size) {
memcpy((uint8_t *)q->data + pos, data, q->size - pos);
memcpy((uint8_t *)q->data, (const uint8_t *)data + q->size - pos, len - (q->size - pos));
} else {
memcpy((uint8_t *)q->data + pos, data, len);
}
if (pos + len > q->head)
q->head = (pos + len) % q->size;
}
return 0;
}
/* Grab the tail packet and advance the tail pointer */
static int queue_pop(struct queue *q, void *data, uint32_t len)
{
uint32_t q_len = queue_len(q);
if (q_len == 0)
return -WOLFIP_EAGAIN;
if (len > q_len)
len = q_len;
memcpy(data, (const uint8_t *)q->data + q->tail, len);
q->tail += len;
q->tail %= q->size;
q->seq_base += len;
return len;
}
/* ARP */
#define ARP_REQUEST 1
#define ARP_REPLY 2
#ifdef ETHERNET
/* Struct to contain an ethernet frame with its header */
struct PACKED wolfIP_eth_frame {
uint8_t dst[6];
uint8_t src[6];
uint16_t type;
uint8_t data[0];
};
#endif
/* Struct to contain a IPv4 packet with its header */
struct PACKED wolfIP_ip_packet {
#ifdef ETHERNET
struct wolfIP_eth_frame eth;
#endif
uint8_t ver_ihl, tos;
uint16_t len, id, flags_fo;
uint8_t ttl, proto;
uint16_t csum;
ip4 src, dst;
uint8_t data[0];
};
/* Describe a TCP segment down to the datalink layer */
struct PACKED wolfIP_tcp_seg {
struct wolfIP_ip_packet ip;
uint16_t src_port, dst_port;
uint32_t seq, ack;
uint8_t hlen, flags;
uint16_t win, csum, urg;
uint8_t data[0];
};
struct PACKED tcp_opt_ts {
/* Timestamp option (10 extra bytes) */
uint8_t opt, len;
uint32_t val, ecr;
uint8_t pad, eoo;
};
struct PACKED tcp_opt_mss {
/* MSS option (4 extra bytes) */
uint8_t opt, len;
uint16_t mss;
};
/* UDP datagram */
struct PACKED wolfIP_udp_datagram {
struct wolfIP_ip_packet ip;
uint16_t src_port, dst_port, len, csum;
uint8_t data[0];
};
/* For Checksums */
union transport_pseudo_header {
struct PACKED ph {
ip4 src, dst;
uint8_t zero, proto;
uint16_t len;
} ph;
uint16_t buf[6];
};
/* ICMP */
#define TTL_EXCEEDED_ORIG_PACKET_SIZE (28)
#define ICMP_TTL_EXCEEDED_SIZE (36)
struct PACKED wolfIP_icmp_packet {
struct wolfIP_ip_packet ip;
uint8_t type, code;
uint16_t csum;
uint8_t unused[4];
};
struct PACKED wolfIP_icmp_ttl_exceeded_packet {
struct wolfIP_ip_packet ip;
uint8_t type, code;
uint16_t csum;
uint8_t unused[4];
uint8_t orig_packet[TTL_EXCEEDED_ORIG_PACKET_SIZE];
};
static uint16_t icmp_echo_id(const struct wolfIP_icmp_packet *icmp)
{
uint16_t net = 0;
memcpy(&net, icmp->unused, sizeof(net));
return ee16(net);
}
static void icmp_set_echo_id(struct wolfIP_icmp_packet *icmp, uint16_t id)
{
uint16_t net = ee16(id);
memcpy(icmp->unused, &net, sizeof(net));
}
#if CONFIG_IPFILTER
static wolfIP_filter_cb wolfip_filter_cb;
static void *wolfip_filter_arg;
static uint32_t wolfip_filter_mask;
static uint32_t wolfip_filter_mask_eth;
static uint32_t wolfip_filter_mask_ip;
static uint32_t wolfip_filter_mask_tcp;
static uint32_t wolfip_filter_mask_udp;
static uint32_t wolfip_filter_mask_icmp;
static int wolfip_filter_lock;
void wolfIP_filter_set_callback(wolfIP_filter_cb cb, void *arg)
{
wolfip_filter_cb = cb;
wolfip_filter_arg = arg;
}
void wolfIP_filter_set_mask(uint32_t mask)
{
wolfip_filter_mask = mask;
}
void wolfIP_filter_set_eth_mask(uint32_t mask)
{
wolfip_filter_mask_eth = mask;
}
void wolfIP_filter_set_ip_mask(uint32_t mask)
{
wolfip_filter_mask_ip = mask;
}
void wolfIP_filter_set_tcp_mask(uint32_t mask)
{
wolfip_filter_mask_tcp = mask;
}
void wolfIP_filter_set_udp_mask(uint32_t mask)
{
wolfip_filter_mask_udp = mask;
}
void wolfIP_filter_set_icmp_mask(uint32_t mask)
{
wolfip_filter_mask_icmp = mask;
}
uint32_t wolfIP_filter_get_mask(void)
{
return wolfip_filter_mask;
}
static void wolfIP_filter_init_metadata(struct wolfIP_filter_metadata *meta)
{
memset(meta, 0, sizeof(*meta));
}
static uint32_t wolfIP_filter_mask_for_proto(uint16_t proto)
{
switch (proto) {
case WOLFIP_FILTER_PROTO_ETH:
return wolfip_filter_mask_eth ? wolfip_filter_mask_eth : wolfip_filter_mask;
case WOLFIP_FILTER_PROTO_IP:
return wolfip_filter_mask_ip ? wolfip_filter_mask_ip : wolfip_filter_mask;
case WOLFIP_FILTER_PROTO_TCP:
return wolfip_filter_mask_tcp ? wolfip_filter_mask_tcp : wolfip_filter_mask;
case WOLFIP_FILTER_PROTO_UDP:
return wolfip_filter_mask_udp ? wolfip_filter_mask_udp : wolfip_filter_mask;
case WOLFIP_FILTER_PROTO_ICMP:
return wolfip_filter_mask_icmp ? wolfip_filter_mask_icmp : wolfip_filter_mask;
default:
return wolfip_filter_mask;
}
}
static int wolfIP_filter_dispatch(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const void *buffer, uint32_t length, const struct wolfIP_filter_metadata *meta)
{
struct wolfIP_filter_event event;
int ret;
uint32_t mask;
if (!wolfip_filter_cb)
return 0;
if (!meta)
mask = wolfip_filter_mask;
else
mask = wolfIP_filter_mask_for_proto(meta->ip_proto);
if ((mask & (1U << reason)) == 0)
return 0;
if (wolfip_filter_lock)
return 0;
event.reason = reason;
event.stack = s;
event.if_idx = if_idx;
event.length = length;
event.buffer = buffer;
if (meta)
event.meta = *meta;
else
wolfIP_filter_init_metadata(&event.meta);
wolfip_filter_lock = 1;
ret = wolfip_filter_cb(wolfip_filter_arg, &event);
wolfip_filter_lock = 0;
return ret;
}
#ifdef ETHERNET
static int wolfIP_filter_notify_eth(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const struct wolfIP_eth_frame *eth, uint32_t len)
{
struct wolfIP_filter_metadata meta;
wolfIP_filter_init_metadata(&meta);
memcpy(meta.src_mac, eth->src, sizeof(meta.src_mac));
memcpy(meta.dst_mac, eth->dst, sizeof(meta.dst_mac));
meta.eth_type = eth->type;
meta.ip_proto = WOLFIP_FILTER_PROTO_ETH;
return wolfIP_filter_dispatch(reason, s, if_idx, eth, len, &meta);
}
#else
#define wolfIP_filter_notify_eth(...) (0)
#endif
static void wolfIP_filter_fill_ip_metadata(struct wolfIP_filter_metadata *meta, const struct wolfIP_ip_packet *ip)
{
meta->src_ip = ip->src;
meta->dst_ip = ip->dst;
meta->ip_proto = (ip->proto == WI_IPPROTO_TCP) ? WOLFIP_FILTER_PROTO_TCP :
(ip->proto == WI_IPPROTO_UDP) ? WOLFIP_FILTER_PROTO_UDP :
(ip->proto == WI_IPPROTO_ICMP) ? WOLFIP_FILTER_PROTO_ICMP :
WOLFIP_FILTER_PROTO_IP;
#ifdef ETHERNET
memcpy(meta->src_mac, ip->eth.src, sizeof(meta->src_mac));
memcpy(meta->dst_mac, ip->eth.dst, sizeof(meta->dst_mac));
meta->eth_type = ip->eth.type;
#endif
}
static int wolfIP_filter_notify_ip(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const struct wolfIP_ip_packet *ip, uint32_t len)
{
struct wolfIP_filter_metadata meta;
wolfIP_filter_init_metadata(&meta);
wolfIP_filter_fill_ip_metadata(&meta, ip);
if (meta.ip_proto == WOLFIP_FILTER_PROTO_TCP ||
meta.ip_proto == WOLFIP_FILTER_PROTO_UDP ||
meta.ip_proto == WOLFIP_FILTER_PROTO_ICMP)
meta.ip_proto = WOLFIP_FILTER_PROTO_IP;
return wolfIP_filter_dispatch(reason, s, if_idx, ip, len, &meta);
}
static int wolfIP_filter_notify_tcp(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const struct wolfIP_tcp_seg *tcp, uint32_t len)
{
struct wolfIP_filter_metadata meta;
wolfIP_filter_init_metadata(&meta);
wolfIP_filter_fill_ip_metadata(&meta, &tcp->ip);
meta.ip_proto = WOLFIP_FILTER_PROTO_TCP;
meta.l4.tcp.src_port = tcp->src_port;
meta.l4.tcp.dst_port = tcp->dst_port;
meta.l4.tcp.flags = tcp->flags;
return wolfIP_filter_dispatch(reason, s, if_idx, tcp, len, &meta);
}
static int wolfIP_filter_notify_udp(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const struct wolfIP_udp_datagram *udp, uint32_t len)
{
struct wolfIP_filter_metadata meta;
wolfIP_filter_init_metadata(&meta);
wolfIP_filter_fill_ip_metadata(&meta, &udp->ip);
meta.ip_proto = WOLFIP_FILTER_PROTO_UDP;
meta.l4.udp.src_port = udp->src_port;
meta.l4.udp.dst_port = udp->dst_port;
return wolfIP_filter_dispatch(reason, s, if_idx, udp, len, &meta);
}
static int wolfIP_filter_notify_icmp(enum wolfIP_filter_reason reason, struct wolfIP *s, unsigned int if_idx, const struct wolfIP_icmp_packet *icmp, uint32_t len)
{
struct wolfIP_filter_metadata meta;
wolfIP_filter_init_metadata(&meta);
wolfIP_filter_fill_ip_metadata(&meta, &icmp->ip);
meta.ip_proto = WOLFIP_FILTER_PROTO_ICMP;
meta.l4.icmp.type = icmp->type;
meta.l4.icmp.code = icmp->code;
return wolfIP_filter_dispatch(reason, s, if_idx, icmp, len, &meta);
}
#else
#define wolfIP_filter_notify_eth(...) (0)
#define wolfIP_filter_notify_ip(...) (0)
#define wolfIP_filter_notify_tcp(...) (0)
#define wolfIP_filter_notify_udp(...) (0)
#define wolfIP_filter_notify_icmp(...) (0)
#endif /* CONFIG_IPFILTER */
/* DHCP */
#define BOOT_REQUEST 1
#define BOOT_REPLY 2
#define DHCP_DISCOVER 1
#define DHCP_OFFER 2
#define DHCP_REQUEST 3
#define DHCP_ACK 5
#define DHCP_MAGIC 0x63825363
#define DHCP_SERVER_PORT 67
#define DHCP_CLIENT_PORT 68
#define DHCP_OPTION_MSG_TYPE 53
#define DHCP_OPTION_SUBNET_MASK 1
#define DHCP_OPTION_ROUTER 3
#define DHCP_OPTION_DNS 6
#define DHCP_OPTION_SERVER_ID 54
#define DHCP_OPTION_PARAM_REQ 55
#define DHCP_OPTION_OFFER_IP 50
#define DHCP_OPTION_END 0xFF
#define DHCP_DISCOVER_TIMEOUT 2000
#define DHCP_DISCOVER_RETRIES 3
#define DHCP_REQUEST_TIMEOUT 2000
#define DHCP_REQUEST_RETRIES 3
enum dhcp_state {
DHCP_OFF = 0,
DHCP_DISCOVER_SENT,
DHCP_REQUEST_SENT,
DHCP_BOUND,
};
#define DHCP_IS_RUNNING(s) \
((s->dhcp_state != DHCP_OFF) && (s->dhcp_state != DHCP_BOUND))
struct PACKED dhcp_msg {
uint8_t op, htype, hlen, hops;
uint32_t xid;
uint16_t secs, flags;
uint32_t ciaddr, yiaddr, siaddr, giaddr;
uint8_t chaddr[16], sname[64], file[128];
uint32_t magic;
uint8_t options[312];
};
#define DHCP_HEADER_LEN 240
struct PACKED dhcp_option {
uint8_t code, len, data[0];
};
/* Sockets */
/* TCP socket */
enum tcp_state {
TCP_CLOSED = 0,
TCP_LISTEN,
TCP_SYN_SENT,
TCP_SYN_RCVD,
TCP_ESTABLISHED,
TCP_FIN_WAIT_1,
TCP_FIN_WAIT_2,
TCP_CLOSING,
TCP_TIME_WAIT,
TCP_CLOSE_WAIT,
TCP_LAST_ACK
};
struct tcpsocket {
enum tcp_state state;
uint32_t last_ts, rtt, rto, cwnd, cwnd_count, ssthresh, tmr_rto, rto_backoff,
seq, ack, last_ack, last;
ip4 local_ip, remote_ip;
struct fifo txbuf;
struct queue rxbuf;
};
/* UDP socket */
struct udpsocket {
struct fifo rxbuf, txbuf;
};
struct tsocket {
union tsocket_sock {
struct tcpsocket tcp;
struct udpsocket udp;
} sock;
uint16_t proto, events;
ip4 local_ip, remote_ip;
ip4 bound_local_ip;
uint16_t src_port, dst_port;
struct wolfIP *S;
#ifdef ETHERNET
uint8_t nexthop_mac[6];
#endif
uint8_t if_idx;
uint8_t recv_ttl;
uint8_t last_pkt_ttl;
uint8_t rxmem[RXBUF_SIZE];
uint8_t txmem[TXBUF_SIZE];
void (*callback)(int sock_fd, uint16_t events, void *arg);
void *callback_arg;
};
static void close_socket(struct tsocket *ts);
#ifdef ETHERNET
struct PACKED arp_packet {
struct wolfIP_eth_frame eth;
uint16_t htype, ptype;
uint8_t hlen, plen;
uint16_t opcode;
uint8_t sma[6];
uint32_t sip;
uint8_t tma[6];
uint32_t tip;
};
struct arp_neighbor {
ip4 ip;
uint8_t mac[6];
uint8_t if_idx;
};
#ifndef WOLFIP_ARP_PENDING_MAX
#define WOLFIP_ARP_PENDING_MAX 4
#endif
struct arp_pending_entry {
ip4 dest;
uint32_t len;
uint8_t if_idx;
uint8_t frame[LINK_MTU];
};
static int arp_lookup(struct wolfIP *s, unsigned int if_idx, ip4 ip, uint8_t *mac);
#if WOLFIP_ENABLE_FORWARDING
static void wolfIP_forward_packet(struct wolfIP *s, unsigned int out_if, struct wolfIP_ip_packet *ip,
uint32_t len, const uint8_t *mac, int broadcast);
#endif
#endif
struct wolfIP;
struct wolfIP_timer {
uint32_t id;
uint64_t expires;
void *arg;
void (*cb)(void *arg);
};
/* Timer binary heap */
struct timers_binheap {
struct wolfIP_timer timers[MAX_TIMERS];
uint32_t size;
};
struct wolfIP
{
struct wolfIP_ll_dev ll_dev[WOLFIP_MAX_INTERFACES];
struct ipconf ipconf[WOLFIP_MAX_INTERFACES];
unsigned int if_count;
enum dhcp_state dhcp_state; /* State machine for DHCP */
uint32_t dhcp_xid; /* DHCP transaction ID while DORA */
int dhcp_udp_sd; /* DHCP socket descriptor. DHCP uses an UDP socket */
uint32_t dhcp_timer; /* Timer for DHCP */
uint32_t dhcp_timeout_count; /* DHCP timeout counter */
ip4 dhcp_server_ip; /* DHCP server IP */
ip4 dhcp_ip; /* IP address assigned by DHCP */
ip4 dns_server;
uint16_t dns_id;
int dns_udp_sd;
uint8_t dns_query_type;
void (*dns_lookup_cb)(ip4 ip);
void (*dns_ptr_cb)(const char *name);
char dns_ptr_name[256];
struct timers_binheap timers;
struct tsocket tcpsockets[MAX_TCPSOCKETS];
struct tsocket udpsockets[MAX_UDPSOCKETS];
struct tsocket icmpsockets[MAX_ICMPSOCKETS];
uint16_t ipcounter;
uint64_t last_tick;
#ifdef ETHERNET
struct wolfIP_arp {
uint64_t last_arp[WOLFIP_MAX_INTERFACES];
struct arp_neighbor neighbors[MAX_NEIGHBORS];
} arp;
struct arp_pending_entry arp_pending[WOLFIP_ARP_PENDING_MAX];
#endif
};
#if WOLFIP_ENABLE_LOOPBACK
static int wolfIP_loopback_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
{
struct wolfIP *s;
uint32_t copy = len;
uint8_t frame[LINK_MTU];
if (!ll || !buf)
return -1;
s = WOLFIP_CONTAINER_OF(ll, struct wolfIP, ll_dev);
if (!s)
return -1;
if (copy > LINK_MTU)
copy = LINK_MTU;
memcpy(frame, buf, copy);
wolfIP_recv_on(s, WOLFIP_LOOPBACK_IF_IDX, frame, copy);
return (int)copy;
}
#endif
/* ***************************** */
/* Implementation */
static inline struct wolfIP_ll_dev *wolfIP_ll_at(struct wolfIP *s, unsigned int if_idx)
{
if (!s || if_idx >= s->if_count)
return NULL;
return &s->ll_dev[if_idx];
}
static inline struct ipconf *wolfIP_ipconf_at(struct wolfIP *s, unsigned int if_idx)
{
if (!s || if_idx >= s->if_count)
return NULL;
return &s->ipconf[if_idx];
}
static inline struct ipconf *wolfIP_primary_ipconf(struct wolfIP *s)
{
return wolfIP_ipconf_at(s, WOLFIP_PRIMARY_IF_IDX);
}
static inline int ip_is_local_conf(const struct ipconf *conf, ip4 addr)
{
if (!conf)
return 0;
if (conf->mask == 0)
return conf->ip == addr;
return ((addr & conf->mask) == (conf->ip & conf->mask));
}
#if WOLFIP_ENABLE_FORWARDING
static int wolfIP_forward_interface(struct wolfIP *s, unsigned int in_if, ip4 dest)
{
int i;
if (!s || s->if_count < 2)
return s ? s->if_count : 0;
for (i = 0; i < (int)s->if_count; i++) {
struct ipconf *conf = &s->ipconf[i];
if (i == (int)in_if)
continue;
if (!conf || conf->ip == IPADDR_ANY)
continue;
if (dest == conf->ip)
return -1;
if (ip_is_local_conf(conf, dest)) {
return i;
}
}
return -1;
}
#endif
static inline ip4 wolfIP_select_nexthop(const struct ipconf *conf, ip4 dest)
{
if (IS_IP_BCAST(dest))
return dest;
if (!conf)
return dest;
if (ip_is_local_conf(conf, dest))
return dest;
if (conf->gw != IPADDR_ANY)
return conf->gw;
return dest;
}
static unsigned int wolfIP_route_for_ip(struct wolfIP *s, ip4 dest)
{
unsigned int default_if = 0;
unsigned int gw_fallback = 0;
unsigned int first_non_loop = 0;
int has_gw_fallback = 0;
int has_non_loop = 0;
unsigned int i;
if (!s || s->if_count == 0)
return 0;
if (WOLFIP_PRIMARY_IF_IDX < s->if_count)
default_if = WOLFIP_PRIMARY_IF_IDX;
if (dest == IPADDR_ANY || IS_IP_BCAST(dest))
return default_if;
for (i = 0; i < s->if_count; i++) {
struct ipconf *conf = &s->ipconf[i];
if (conf->ip == IPADDR_ANY && conf->gw == IPADDR_ANY)
continue;
if (ip_is_local_conf(conf, dest) || conf->ip == dest) {
return i;
}
if (!wolfIP_is_loopback_if(i) && !has_non_loop) {
first_non_loop = i;
has_non_loop = 1;
}
if (!wolfIP_is_loopback_if(i) && !has_gw_fallback && conf->gw != IPADDR_ANY) {
gw_fallback = i;
has_gw_fallback = 1;
}
}
if (has_gw_fallback) {
return gw_fallback;
}
if (has_non_loop) {
return first_non_loop;
}
return default_if;
}
static inline unsigned int wolfIP_socket_if_idx(const struct tsocket *t)
{
if (!t || !t->S || t->if_idx >= t->S->if_count)
return 0;
return t->if_idx;
}
#if CONFIG_IPFILTER
static int wolfIP_filter_notify_socket_event(
enum wolfIP_filter_reason reason,
struct wolfIP *s,
struct tsocket *ts,
ip4 local_ip,
uint16_t local_port,
ip4 remote_ip,