Repository navigation
Expand file tree
/
Copy pathicmp.c
More file actions
1227 lines (1107 loc) · 28 KB
/
Copy pathicmp.c
File metadata and controls
1227 lines (1107 loc) · 28 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
/*
icmp.c
Copyright 1995 Philip Homburg
*/
#include "inet.h"
#include "buf.h"
#include "event.h"
#include "type.h"
#include "assert.h"
#include "clock.h"
#include "icmp.h"
#include "icmp_lib.h"
#include "io.h"
#include "ip.h"
#include "ip_int.h"
#include "ipr.h"
THIS_FILE
typedef struct icmp_port
{
int icp_flags;
int icp_state;
int icp_ipport;
int icp_ipfd;
unsigned icp_rate_count;
unsigned icp_rate_report;
time_t icp_rate_lasttime;
acc_t *icp_head_queue;
acc_t *icp_tail_queue;
acc_t *icp_write_pack;
event_t icp_event;
} icmp_port_t;
#define ICPF_EMPTY 0x0
#define ICPF_SUSPEND 0x1
#define ICPF_READ_IP 0x2
#define ICPF_READ_SP 0x4
#define ICPF_WRITE_IP 0x8
#define ICPF_WRITE_SP 0x10
#define ICPS_BEGIN 0
#define ICPS_IPOPT 1
#define ICPS_MAIN 2
#define ICPS_ERROR 3
static icmp_port_t *icmp_port_table;
static void icmp_main ARGS(( icmp_port_t *icmp_port ));
static acc_t *icmp_getdata ARGS(( int port, size_t offset,
size_t count, int for_ioctl ));
static int icmp_putdata ARGS(( int port, size_t offset,
acc_t *data, int for_ioctl ));
static void icmp_read ARGS(( icmp_port_t *icmp_port ));
static void process_data ARGS(( icmp_port_t *icmp_port,
acc_t *data ));
static u16_t icmp_pack_oneCsum ARGS(( acc_t *ip_pack ));
static void icmp_echo_request ARGS(( icmp_port_t *icmp_port,
acc_t *ip_pack, int ip_hdr_len, ip_hdr_t *ip_hdr,
acc_t *icmp_pack, int icmp_len, icmp_hdr_t *icmp_hdr ));
static void icmp_dst_unreach ARGS(( icmp_port_t *icmp_port,
acc_t *ip_pack, int ip_hdr_len, ip_hdr_t *ip_hdr,
acc_t *icmp_pack, int icmp_len, icmp_hdr_t *icmp_hdr ));
static void icmp_time_exceeded ARGS(( icmp_port_t *icmp_port,
acc_t *ip_pack, int ip_hdr_len, ip_hdr_t *ip_hdr,
acc_t *icmp_pack, int icmp_len, icmp_hdr_t *icmp_hdr ));
static void icmp_router_advertisement ARGS(( icmp_port_t *icmp_port,
acc_t *icmp_pack, int icmp_len, icmp_hdr_t *icmp_hdr ));
static void icmp_redirect ARGS(( icmp_port_t *icmp_port,
ip_hdr_t *ip_hdr, acc_t *icmp_pack, int icmp_len,
icmp_hdr_t *icmp_hdr ));
static acc_t *make_repl_ip ARGS(( ip_hdr_t *ip_hdr,
int ip_len ));
static void enqueue_pack ARGS(( icmp_port_t *icmp_port,
acc_t *reply_ip_hdr ));
static int icmp_rate_limit ARGS(( icmp_port_t *icmp_port,
acc_t *reply_ip_hdr ));
static void icmp_write ARGS(( event_t *ev, ev_arg_t ev_arg ));
static void icmp_buffree ARGS(( int priority ));
static acc_t *icmp_err_pack ARGS(( acc_t *pack, icmp_hdr_t **icmp_hdr_pp ));
#ifdef BUF_CONSISTENCY_CHECK
static void icmp_bufcheck ARGS(( void ));
#endif
void icmp_prep()
{
icmp_port_table= alloc(ip_conf_nr * sizeof(icmp_port_table[0]));
}
void icmp_init()
{
int i;
icmp_port_t *icmp_port;
assert (BUF_S >= sizeof (nwio_ipopt_t));
for (i= 0, icmp_port= icmp_port_table; i<ip_conf_nr; i++, icmp_port++)
{
icmp_port->icp_flags= ICPF_EMPTY;
icmp_port->icp_state= ICPS_BEGIN;
icmp_port->icp_ipport= i;
icmp_port->icp_rate_count= 0;
icmp_port->icp_rate_report= ICMP_MAX_RATE;
icmp_port->icp_rate_lasttime= 0;
ev_init(&icmp_port->icp_event);
}
#ifndef BUF_CONSISTENCY_CHECK
bf_logon(icmp_buffree);
#else
bf_logon(icmp_buffree, icmp_bufcheck);
#endif
for (i= 0, icmp_port= icmp_port_table; i<ip_conf_nr; i++, icmp_port++)
{
icmp_main (icmp_port);
}
}
static void icmp_main(icmp_port)
icmp_port_t *icmp_port;
{
int result;
switch (icmp_port->icp_state)
{
case ICPS_BEGIN:
icmp_port->icp_head_queue= 0;
icmp_port->icp_ipfd= ip_open(icmp_port->icp_ipport,
icmp_port->icp_ipport, icmp_getdata, icmp_putdata,
0 /* no put_pkt */, 0 /* no select_res */);
if (icmp_port->icp_ipfd<0)
{
DBLOCK(1, printf("unable to open ip_port %d\n",
icmp_port->icp_ipport));
break;
}
icmp_port->icp_state= ICPS_IPOPT;
icmp_port->icp_flags &= ~ICPF_SUSPEND;
result= ip_ioctl (icmp_port->icp_ipfd, NWIOSIPOPT);
if (result == NW_SUSPEND)
{
icmp_port->icp_flags |= ICPF_SUSPEND;
break;
}
assert(result == NW_OK);
/* falls through */
case ICPS_IPOPT:
icmp_port->icp_state= ICPS_MAIN;
icmp_port->icp_flags &= ~ICPF_SUSPEND;
icmp_read(icmp_port);
break;
default:
DBLOCK(1, printf("unknown state %d\n",
icmp_port->icp_state));
break;
}
}
static acc_t *icmp_getdata(port, offset, count, for_ioctl)
int port;
size_t offset, count;
int for_ioctl;
{
icmp_port_t *icmp_port;
nwio_ipopt_t *ipopt;
acc_t *data;
int result;
ev_arg_t ev_arg;
icmp_port= &icmp_port_table[port];
if (icmp_port->icp_flags & ICPF_WRITE_IP)
{
if (!count)
{
bf_afree(icmp_port->icp_write_pack);
icmp_port->icp_write_pack= 0;
result= (int)offset;
if (result<0)
{
DBLOCK(1, printf("got write error %d\n",
result));
}
if (icmp_port->icp_flags & ICPF_WRITE_SP)
{
icmp_port->icp_flags &= ~ICPF_WRITE_SP;
ev_arg.ev_ptr= icmp_port;
ev_enqueue(&icmp_port->icp_event, icmp_write,
ev_arg);
}
return NW_OK;
}
return bf_cut(icmp_port->icp_write_pack, offset, count);
}
switch (icmp_port->icp_state)
{
case ICPS_IPOPT:
if (!count)
{
result= (int)offset;
assert(result == NW_OK);
if (result < 0)
{
icmp_port->icp_state= ICPS_ERROR;
break;
}
if (icmp_port->icp_flags & ICPF_SUSPEND)
icmp_main(icmp_port);
return NW_OK;
}
data= bf_memreq (sizeof (*ipopt));
ipopt= (nwio_ipopt_t *)ptr2acc_data(data);
ipopt->nwio_flags= NWIO_COPY | NWIO_EN_LOC |
NWIO_EN_BROAD |
NWIO_REMANY | NWIO_PROTOSPEC |
NWIO_HDR_O_ANY | NWIO_RWDATALL;
ipopt->nwio_proto= IPPROTO_ICMP;
return data;
default:
break;
}
DBLOCK(1, printf("unknown state %d\n", icmp_port->icp_state));
return NULL;
}
static int icmp_putdata(port, offset, data, for_ioctl)
int port;
size_t offset;
acc_t *data;
int for_ioctl;
{
icmp_port_t *icmp_port;
int result;
icmp_port= &icmp_port_table[port];
if (icmp_port->icp_flags & ICPF_READ_IP)
{
if (!data)
{
result= (int)offset;
if (result<0)
{
DBLOCK(1, printf("got read error %d\n",
result));
}
if (icmp_port->icp_flags & ICPF_READ_SP)
{
icmp_port->icp_flags &=
~(ICPF_READ_IP|ICPF_READ_SP);
icmp_read (icmp_port);
}
return NW_OK;
}
process_data(icmp_port, data);
return NW_OK;
}
switch (icmp_port->icp_state)
{
default:
DBLOCK(1, printf("unknown state %d\n",
icmp_port->icp_state));
return 0;
}
}
static void icmp_read(icmp_port)
icmp_port_t *icmp_port;
{
int result;
for (;;)
{
icmp_port->icp_flags |= ICPF_READ_IP;
icmp_port->icp_flags &= ~ICPF_READ_SP;
result= ip_read(icmp_port->icp_ipfd, ICMP_MAX_DATAGRAM);
if (result == NW_SUSPEND)
{
icmp_port->icp_flags |= ICPF_READ_SP;
return;
}
}
}
void icmp_snd_time_exceeded(port_nr, pack, code)
int port_nr;
acc_t *pack;
int code;
{
icmp_hdr_t *icmp_hdr;
icmp_port_t *icmp_port;
if (port_nr >= 0 && port_nr < ip_conf_nr)
icmp_port= &icmp_port_table[port_nr];
else
{
printf("icmp_snd_time_exceeded: strange port %d\n", port_nr);
bf_afree(pack);
return;
}
pack= icmp_err_pack(pack, &icmp_hdr);
if (pack == NULL)
return;
icmp_hdr->ih_type= ICMP_TYPE_TIME_EXCEEDED;
icmp_hdr->ih_code= code;
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_type, 2);
enqueue_pack(icmp_port, pack);
}
void icmp_snd_redirect(port_nr, pack, code, gw)
int port_nr;
acc_t *pack;
int code;
ipaddr_t gw;
{
icmp_hdr_t *icmp_hdr;
icmp_port_t *icmp_port;
if (port_nr >= 0 && port_nr < ip_conf_nr)
icmp_port= &icmp_port_table[port_nr];
else
{
printf("icmp_snd_redirect: strange port %d\n", port_nr);
bf_afree(pack);
return;
}
pack= icmp_err_pack(pack, &icmp_hdr);
if (pack == NULL)
return;
icmp_hdr->ih_type= ICMP_TYPE_REDIRECT;
icmp_hdr->ih_code= code;
icmp_hdr->ih_hun.ihh_gateway= gw;
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_type, 2);
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_hun.ihh_gateway, 4);
enqueue_pack(icmp_port, pack);
}
void icmp_snd_unreachable(port_nr, pack, code)
int port_nr;
acc_t *pack;
int code;
{
icmp_hdr_t *icmp_hdr;
icmp_port_t *icmp_port;
if (port_nr >= 0 && port_nr < ip_conf_nr)
icmp_port= &icmp_port_table[port_nr];
else
{
printf("icmp_snd_unreachable: strange port %d\n", port_nr);
bf_afree(pack);
return;
}
pack= icmp_err_pack(pack, &icmp_hdr);
if (pack == NULL)
return;
icmp_hdr->ih_type= ICMP_TYPE_DST_UNRCH;
icmp_hdr->ih_code= code;
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_type, 2);
enqueue_pack(icmp_port, pack);
}
void icmp_snd_mtu(
int port_nr,
acc_t *pack,
u16_t mtu
)
{
icmp_hdr_t *icmp_hdr;
icmp_port_t *icmp_port;
if (port_nr >= 0 && port_nr < ip_conf_nr)
icmp_port= &icmp_port_table[port_nr];
else
{
printf("icmp_snd_mtu: strange port %d\n", port_nr);
bf_afree(pack);
return;
}
pack= icmp_err_pack(pack, &icmp_hdr);
if (pack == NULL)
return;
icmp_hdr->ih_type= ICMP_TYPE_DST_UNRCH;
icmp_hdr->ih_code= ICMP_FRAGM_AND_DF;
icmp_hdr->ih_hun.ihh_mtu.im_mtu= htons(mtu);
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_type, 2);
icmp_hdr->ih_chksum= ~oneC_sum(~icmp_hdr->ih_chksum,
(u16_t *)&icmp_hdr->ih_hun.ihh_mtu.im_mtu, 2);
enqueue_pack(icmp_port, pack);
}
static void process_data(icmp_port, data)
icmp_port_t *icmp_port;
acc_t *data;
{
ip_hdr_t *ip_hdr;
icmp_hdr_t *icmp_hdr;
acc_t *icmp_data;
int ip_hdr_len;
size_t pack_len;
/* Align entire packet */
data= bf_align(data, BUF_S, 4);
data= bf_packIffLess(data, IP_MIN_HDR_SIZE);
ip_hdr= (ip_hdr_t *)ptr2acc_data(data);
DIFBLOCK(0x10, (ip_hdr->ih_dst & HTONL(0xf0000000)) == HTONL(0xe0000000),
printf("got multicast packet\n"));
ip_hdr_len= (ip_hdr->ih_vers_ihl & IH_IHL_MASK) << 2;
if (ip_hdr_len>IP_MIN_HDR_SIZE)
{
data= bf_packIffLess(data, ip_hdr_len);
ip_hdr= (ip_hdr_t *)ptr2acc_data(data);
}
pack_len= bf_bufsize(data);
pack_len -= ip_hdr_len;
if (pack_len < ICMP_MIN_HDR_SIZE)
{
if (pack_len == 0 && ip_hdr->ih_proto == 0)
{
/* IP layer reports new ip address, which can be
* ignored.
*/
}
else
DBLOCK(1, printf("got an incomplete icmp packet\n"));
bf_afree(data);
return;
}
icmp_data= bf_cut(data, ip_hdr_len, pack_len);
icmp_data= bf_packIffLess (icmp_data, ICMP_MIN_HDR_SIZE);
icmp_hdr= (icmp_hdr_t *)ptr2acc_data(icmp_data);
if ((u16_t)~icmp_pack_oneCsum(icmp_data))
{
DBLOCK(1, printf(
"got packet with bad checksum (= 0x%x, 0x%x)\n",
icmp_hdr->ih_chksum,
(u16_t)~icmp_pack_oneCsum(icmp_data)));
bf_afree(data);
bf_afree(icmp_data);
return;
}
switch (icmp_hdr->ih_type)
{
case ICMP_TYPE_ECHO_REPL:
break;
case ICMP_TYPE_DST_UNRCH:
icmp_dst_unreach (icmp_port, data, ip_hdr_len, ip_hdr,
icmp_data, pack_len, icmp_hdr);
break;
case ICMP_TYPE_SRC_QUENCH:
/* Ignore src quench ICMPs */
DBLOCK(2, printf("ignoring SRC QUENCH ICMP.\n"));
break;
case ICMP_TYPE_REDIRECT:
icmp_redirect (icmp_port, ip_hdr, icmp_data, pack_len,
icmp_hdr);
break;
case ICMP_TYPE_ECHO_REQ:
icmp_echo_request(icmp_port, data, ip_hdr_len, ip_hdr,
icmp_data, pack_len, icmp_hdr);
return;
case ICMP_TYPE_ROUTER_ADVER:
icmp_router_advertisement(icmp_port, icmp_data, pack_len,
icmp_hdr);
break;
case ICMP_TYPE_ROUTE_SOL:
break; /* Should be handled by a routing deamon. */
case ICMP_TYPE_TIME_EXCEEDED:
icmp_time_exceeded (icmp_port, data, ip_hdr_len, ip_hdr,
icmp_data, pack_len, icmp_hdr);
break;
default:
DBLOCK(1, printf("got an unknown icmp (%d) from ",
icmp_hdr->ih_type);
writeIpAddr(ip_hdr->ih_src); printf("\n"));
break;
}
bf_afree(data);
bf_afree(icmp_data);
}
static void icmp_echo_request(icmp_port, ip_data, ip_len, ip_hdr,
icmp_data, icmp_len, icmp_hdr)
icmp_port_t *icmp_port;
acc_t *ip_data, *icmp_data;
int ip_len, icmp_len;
ip_hdr_t *ip_hdr;
icmp_hdr_t *icmp_hdr;
{
acc_t *repl_ip_hdr, *repl_icmp;
ipaddr_t tmpaddr, locaddr, netmask;
icmp_hdr_t *repl_icmp_hdr;
i32_t tmp_chksum;
ip_port_t *ip_port;
if (icmp_hdr->ih_code != 0)
{
DBLOCK(1,
printf("got an icmp echo request with unknown code (%d)\n",
icmp_hdr->ih_code));
bf_afree(ip_data);
bf_afree(icmp_data);
return;
}
if (icmp_len < ICMP_MIN_HDR_SIZE + sizeof(icmp_id_seq_t))
{
DBLOCK(1, printf("got an incomplete icmp echo request\n"));
bf_afree(ip_data);
bf_afree(icmp_data);
return;
}
tmpaddr= ntohl(ip_hdr->ih_dst);
if ((tmpaddr & 0xe0000000) == 0xe0000000 &&
tmpaddr != 0xffffffff)
{
/* Respond only to the all hosts multicast address until
* a decent listening service has been implemented
*/
if (tmpaddr != 0xe0000001)
{
bf_afree(ip_data);
bf_afree(icmp_data);
return;
}
}
/* Limit subnet broadcasts to the local net */
ip_port= &ip_port_table[icmp_port->icp_ipport];
locaddr= ip_port->ip_ipaddr;
netmask= ip_port->ip_subnetmask;
if (ip_hdr->ih_dst == (locaddr | ~netmask) &&
(ip_port->ip_flags & IPF_SUBNET_BCAST) &&
((ip_hdr->ih_src ^ locaddr) & netmask) != 0)
{
/* Directed broadcast */
bf_afree(ip_data);
bf_afree(icmp_data);
return;
}
repl_ip_hdr= make_repl_ip(ip_hdr, ip_len);
repl_icmp= bf_memreq (ICMP_MIN_HDR_SIZE);
repl_icmp_hdr= (icmp_hdr_t *)ptr2acc_data(repl_icmp);
repl_icmp_hdr->ih_type= ICMP_TYPE_ECHO_REPL;
repl_icmp_hdr->ih_code= 0;
DBLOCK(2,
printf("ih_chksum= 0x%x, ih_type= 0x%x, repl->ih_type= 0x%x\n",
icmp_hdr->ih_chksum, *(u16_t *)&icmp_hdr->ih_type,
*(u16_t *)&repl_icmp_hdr->ih_type));
tmp_chksum= (~icmp_hdr->ih_chksum & 0xffff) -
(i32_t)*(u16_t *)&icmp_hdr->ih_type+
*(u16_t *)&repl_icmp_hdr->ih_type;
tmp_chksum= (tmp_chksum >> 16) + (tmp_chksum & 0xffff);
tmp_chksum= (tmp_chksum >> 16) + (tmp_chksum & 0xffff);
repl_icmp_hdr->ih_chksum= ~tmp_chksum;
DBLOCK(2, printf("sending chksum 0x%x\n", repl_icmp_hdr->ih_chksum));
repl_ip_hdr->acc_next= repl_icmp;
repl_icmp->acc_next= bf_cut (icmp_data, ICMP_MIN_HDR_SIZE,
icmp_len - ICMP_MIN_HDR_SIZE);
bf_afree(ip_data);
bf_afree(icmp_data);
enqueue_pack(icmp_port, repl_ip_hdr);
}
static u16_t icmp_pack_oneCsum(icmp_pack)
acc_t *icmp_pack;
{
u16_t prev;
int odd_byte;
char *data_ptr;
int length;
char byte_buf[2];
prev= 0;
odd_byte= FALSE;
for (; icmp_pack; icmp_pack= icmp_pack->acc_next)
{
data_ptr= ptr2acc_data(icmp_pack);
length= icmp_pack->acc_length;
if (!length)
continue;
if (odd_byte)
{
byte_buf[1]= *data_ptr;
prev= oneC_sum(prev, (u16_t *)byte_buf, 2);
data_ptr++;
length--;
odd_byte= FALSE;
}
if (length & 1)
{
odd_byte= TRUE;
length--;
byte_buf[0]= data_ptr[length];
}
if (!length)
continue;
prev= oneC_sum (prev, (u16_t *)data_ptr, length);
}
if (odd_byte)
prev= oneC_sum (prev, (u16_t *)byte_buf, 1);
return prev;
}
static acc_t *make_repl_ip(ip_hdr, ip_len)
ip_hdr_t *ip_hdr;
int ip_len;
{
ip_hdr_t *repl_ip_hdr;
acc_t *repl;
int repl_hdr_len;
if (ip_len>IP_MIN_HDR_SIZE)
{
DBLOCK(1, printf("ip_hdr options NOT supported (yet?)\n"));
ip_len= IP_MIN_HDR_SIZE;
}
repl_hdr_len= IP_MIN_HDR_SIZE;
repl= bf_memreq(repl_hdr_len);
repl_ip_hdr= (ip_hdr_t *)ptr2acc_data(repl);
repl_ip_hdr->ih_vers_ihl= repl_hdr_len >> 2;
repl_ip_hdr->ih_tos= ip_hdr->ih_tos;
repl_ip_hdr->ih_ttl= ICMP_DEF_TTL;
repl_ip_hdr->ih_proto= IPPROTO_ICMP;
repl_ip_hdr->ih_dst= ip_hdr->ih_src;
repl_ip_hdr->ih_flags_fragoff= 0;
return repl;
}
static void enqueue_pack(icmp_port, reply_ip_hdr)
icmp_port_t *icmp_port;
acc_t *reply_ip_hdr;
{
int r;
ev_arg_t ev_arg;
/* Check rate */
if (icmp_port->icp_rate_count >= ICMP_MAX_RATE)
{
/* Something is going wrong; check policy */
r= icmp_rate_limit(icmp_port, reply_ip_hdr);
if (r == -1)
{
bf_afree(reply_ip_hdr);
return;
}
/* OK, continue */
}
icmp_port->icp_rate_count++;
reply_ip_hdr->acc_ext_link= 0;
if (icmp_port->icp_head_queue)
{
icmp_port->icp_tail_queue->acc_ext_link=
reply_ip_hdr;
}
else
{
icmp_port->icp_head_queue= reply_ip_hdr;
}
reply_ip_hdr->acc_ext_link= NULL;
icmp_port->icp_tail_queue= reply_ip_hdr;
if (!(icmp_port->icp_flags & ICPF_WRITE_IP))
{
icmp_port->icp_flags |= ICPF_WRITE_IP;
ev_arg.ev_ptr= icmp_port;
ev_enqueue(&icmp_port->icp_event, icmp_write, ev_arg);
}
}
static int icmp_rate_limit(icmp_port, reply_ip_hdr)
icmp_port_t *icmp_port;
acc_t *reply_ip_hdr;
{
time_t t;
acc_t *pack;
ip_hdr_t *ip_hdr;
icmp_hdr_t *icmp_hdr;
int hdrlen, icmp_hdr_len, type;
/* Check the time first */
t= get_time();
if (t >= icmp_port->icp_rate_lasttime + ICMP_RATE_INTERVAL)
{
icmp_port->icp_rate_lasttime= t;
icmp_port->icp_rate_count= 0;
return 0;
}
icmp_port->icp_rate_count++;
/* Adjust report limit if necessary */
if (icmp_port->icp_rate_count >
icmp_port->icp_rate_report+ICMP_RATE_WARN)
{
icmp_port->icp_rate_report *= 2;
return -1;
}
/* Do we need to report */
if (icmp_port->icp_rate_count < icmp_port->icp_rate_report)
return -1;
pack= bf_dupacc(reply_ip_hdr);
pack= bf_packIffLess(pack, IP_MIN_HDR_SIZE);
ip_hdr= (ip_hdr_t *)ptr2acc_data(pack);
printf("icmp[%d]: dropping ICMP packet #%d to ",
icmp_port->icp_ipport, icmp_port->icp_rate_count);
writeIpAddr(ip_hdr->ih_dst);
hdrlen= (ip_hdr->ih_vers_ihl & IH_IHL_MASK)*4;
pack= bf_packIffLess(pack, hdrlen+ICMP_MIN_HDR_SIZE);
ip_hdr= (ip_hdr_t *)ptr2acc_data(pack);
icmp_hdr= (icmp_hdr_t *)(ptr2acc_data(pack)+hdrlen);
type= icmp_hdr->ih_type;
printf(" type %d, code %d\n", type, icmp_hdr->ih_code);
switch(type)
{
case ICMP_TYPE_DST_UNRCH:
case ICMP_TYPE_SRC_QUENCH:
case ICMP_TYPE_REDIRECT:
case ICMP_TYPE_TIME_EXCEEDED:
case ICMP_TYPE_PARAM_PROBLEM:
icmp_hdr_len= offsetof(struct icmp_hdr, ih_dun);
pack= bf_packIffLess(pack,
hdrlen+icmp_hdr_len+IP_MIN_HDR_SIZE);
ip_hdr= (ip_hdr_t *)(ptr2acc_data(pack)+hdrlen+icmp_hdr_len);
icmp_hdr= (icmp_hdr_t *)(ptr2acc_data(pack)+hdrlen);
printf("\tinfo %08x, original dst ",
ntohs(icmp_hdr->ih_hun.ihh_unused));
writeIpAddr(ip_hdr->ih_dst);
printf(", proto %d, length %u\n",
ip_hdr->ih_proto, ntohs(ip_hdr->ih_length));
break;
default:
break;
}
bf_afree(pack); pack= NULL;
return -1;
}
static void icmp_write(ev, ev_arg)
event_t *ev;
ev_arg_t ev_arg;
{
int result;
icmp_port_t *icmp_port;
acc_t *data;
icmp_port= ev_arg.ev_ptr;
assert(ev == &icmp_port->icp_event);
assert (icmp_port->icp_flags & ICPF_WRITE_IP);
assert (!(icmp_port->icp_flags & ICPF_WRITE_SP));
while (icmp_port->icp_head_queue != NULL)
{
data= icmp_port->icp_head_queue;
icmp_port->icp_head_queue= data->acc_ext_link;
result= ip_send(icmp_port->icp_ipfd, data,
bf_bufsize(data));
if (result != NW_WOULDBLOCK)
{
if (result == NW_OK)
continue;
DBLOCK(1, printf("icmp_write: error %d\n", result););
continue;
}
assert(icmp_port->icp_write_pack == NULL);
icmp_port->icp_write_pack= data;
result= ip_write(icmp_port->icp_ipfd,
bf_bufsize(icmp_port->icp_write_pack));
if (result == NW_SUSPEND)
{
icmp_port->icp_flags |= ICPF_WRITE_SP;
return;
}
}
icmp_port->icp_flags &= ~ICPF_WRITE_IP;
}
static void icmp_buffree(priority)
int priority;
{
acc_t *tmp_acc;
int i;
icmp_port_t *icmp_port;
if (priority == ICMP_PRI_QUEUE)
{
for (i=0, icmp_port= icmp_port_table; i<ip_conf_nr;
i++, icmp_port++)
{
while(icmp_port->icp_head_queue)
{
tmp_acc= icmp_port->icp_head_queue;
icmp_port->icp_head_queue=
tmp_acc->acc_ext_link;
bf_afree(tmp_acc);
}
}
}
}
#ifdef BUF_CONSISTENCY_CHECK
static void icmp_bufcheck()
{
int i;
icmp_port_t *icmp_port;
acc_t *pack;
for (i= 0, icmp_port= icmp_port_table; i<ip_conf_nr; i++, icmp_port++)
{
for (pack= icmp_port->icp_head_queue; pack;
pack= pack->acc_ext_link)
{
bf_check_acc(pack);
}
bf_check_acc(icmp_port->icp_write_pack);
}
}
#endif
static void icmp_dst_unreach(icmp_port, ip_pack, ip_hdr_len, ip_hdr, icmp_pack,
icmp_len, icmp_hdr)
icmp_port_t *icmp_port;
acc_t *ip_pack;
int ip_hdr_len;
ip_hdr_t *ip_hdr;
acc_t *icmp_pack;
int icmp_len;
icmp_hdr_t *icmp_hdr;
{
acc_t *old_ip_pack;
ip_hdr_t *old_ip_hdr;
int ip_port_nr;
ipaddr_t dst, mask;
size_t old_pack_size;
u16_t new_mtu;
if (icmp_len < 8 + IP_MIN_HDR_SIZE)
{
DBLOCK(1, printf("dest unrch with wrong size\n"));
return;
}
old_ip_pack= bf_cut (icmp_pack, 8, icmp_len-8);
old_ip_pack= bf_packIffLess(old_ip_pack, IP_MIN_HDR_SIZE);
old_ip_hdr= (ip_hdr_t *)ptr2acc_data(old_ip_pack);
if (old_ip_hdr->ih_src != ip_hdr->ih_dst)
{
DBLOCK(1, printf("dest unrch based on wrong packet\n"));
bf_afree(old_ip_pack);
return;
}
ip_port_nr= icmp_port->icp_ipport;
switch(icmp_hdr->ih_code)
{
case ICMP_NET_UNRCH:
dst= old_ip_hdr->ih_dst;
mask= ip_get_netmask(dst);
ipr_destunrch (ip_port_nr, dst & mask, mask,
IPR_UNRCH_TIMEOUT);
break;
case ICMP_HOST_UNRCH:
ipr_destunrch (ip_port_nr, old_ip_hdr->ih_dst, (ipaddr_t)-1,
IPR_UNRCH_TIMEOUT);
break;
case ICMP_PORT_UNRCH:
/* At the moment we don't do anything with this information.
* It should be handed to the appropriate transport layer.
*/
break;
case ICMP_FRAGM_AND_DF:
DBLOCK(1, printf("icmp_dst_unreach: got mtu icmp from ");
writeIpAddr(ip_hdr->ih_src);
printf("; original destination: ");
writeIpAddr(old_ip_hdr->ih_dst);
printf("; protocol: %d\n",
old_ip_hdr->ih_proto));
old_pack_size= ntohs(old_ip_hdr->ih_length);
if (!old_pack_size)
break;
new_mtu= ntohs(icmp_hdr->ih_hun.ihh_mtu.im_mtu);
if (!new_mtu || new_mtu > old_pack_size)
new_mtu= old_pack_size-1;
ipr_mtu(ip_port_nr, old_ip_hdr->ih_dst, new_mtu,
IPR_MTU_TIMEOUT);
break;
default:
DBLOCK(1, printf("icmp_dst_unreach: got strange code %d from ",
icmp_hdr->ih_code);
writeIpAddr(ip_hdr->ih_src);
printf("; original destination: ");
writeIpAddr(old_ip_hdr->ih_dst);
printf("; protocol: %d\n",
old_ip_hdr->ih_proto));
break;
}
bf_afree(old_ip_pack);
}
static void icmp_time_exceeded(icmp_port, ip_pack, ip_hdr_len, ip_hdr,
icmp_pack, icmp_len, icmp_hdr)
icmp_port_t *icmp_port;
acc_t *ip_pack;
int ip_hdr_len;
ip_hdr_t *ip_hdr;
acc_t *icmp_pack;
int icmp_len;
icmp_hdr_t *icmp_hdr;
{
acc_t *old_ip_pack;
ip_hdr_t *old_ip_hdr;
int ip_port_nr;
if (icmp_len < 8 + IP_MIN_HDR_SIZE)
{
DBLOCK(1, printf("time exceeded with wrong size\n"));
return;
}
old_ip_pack= bf_cut (icmp_pack, 8, icmp_len-8);
old_ip_pack= bf_packIffLess(old_ip_pack, IP_MIN_HDR_SIZE);
old_ip_hdr= (ip_hdr_t *)ptr2acc_data(old_ip_pack);
if (old_ip_hdr->ih_src != ip_hdr->ih_dst)
{
DBLOCK(1, printf("time exceeded based on wrong packet\n"));
bf_afree(old_ip_pack);
return;
}
ip_port_nr= icmp_port->icp_ipport;
switch(icmp_hdr->ih_code)
{
case ICMP_TTL_EXC:
ipr_ttl_exc (ip_port_nr, old_ip_hdr->ih_dst, (ipaddr_t)-1,
IPR_TTL_TIMEOUT);
break;
case ICMP_FRAG_REASSEM:
/* Ignore reassembly time-outs. */
break;
default:
DBLOCK(1, printf("got strange code: %d\n",
icmp_hdr->ih_code));
break;
}
bf_afree(old_ip_pack);
}
static void icmp_router_advertisement(icmp_port, icmp_pack, icmp_len, icmp_hdr)
icmp_port_t *icmp_port;
acc_t *icmp_pack;
int icmp_len;
icmp_hdr_t *icmp_hdr;
{
int entries;
int entry_size;