forked from CTSRD-CHERI/cheribsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fil.c
9967 lines (8857 loc) · 315 KB
/
fil.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) 2012 by Darren Reed.
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2008 Sun Microsystems.
*
* $Id$
*
*/
#if defined(KERNEL) || defined(_KERNEL)
# undef KERNEL
# undef _KERNEL
# define KERNEL 1
# define _KERNEL 1
#endif
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#if defined(_KERNEL) && defined(__FreeBSD__)
# if !defined(IPFILTER_LKM)
# include "opt_inet6.h"
# endif
# include <sys/filio.h>
#else
# include <sys/ioctl.h>
#endif
#if defined(__SVR4) || defined(sun) /* SOLARIS */
# include <sys/filio.h>
#endif
# include <sys/fcntl.h>
#if defined(_KERNEL)
# include <sys/systm.h>
# include <sys/file.h>
#else
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <stddef.h>
# include <sys/file.h>
# define _KERNEL
# include <sys/uio.h>
# undef _KERNEL
#endif
#if !defined(__SVR4)
# include <sys/mbuf.h>
#else
# include <sys/byteorder.h>
# if (SOLARIS2 < 5) && defined(sun)
# include <sys/dditypes.h>
# endif
#endif
# include <sys/protosw.h>
#include <sys/socket.h>
#include <net/if.h>
#ifdef sun
# include <net/af.h>
#endif
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
# include <netinet/udp.h>
# include <netinet/ip_icmp.h>
#include "netinet/ip_compat.h"
#ifdef USE_INET6
# include <netinet/icmp6.h>
# if !SOLARIS && defined(_KERNEL)
# include <netinet6/in6_var.h>
# endif
#endif
#include "netinet/ip_fil.h"
#include "netinet/ip_nat.h"
#include "netinet/ip_frag.h"
#include "netinet/ip_state.h"
#include "netinet/ip_proxy.h"
#include "netinet/ip_auth.h"
#ifdef IPFILTER_SCAN
# include "netinet/ip_scan.h"
#endif
#include "netinet/ip_sync.h"
#include "netinet/ip_lookup.h"
#include "netinet/ip_pool.h"
#include "netinet/ip_htable.h"
#ifdef IPFILTER_COMPILED
# include "netinet/ip_rules.h"
#endif
#if defined(IPFILTER_BPF) && defined(_KERNEL)
# include <net/bpf.h>
#endif
#if defined(__FreeBSD__)
# include <sys/malloc.h>
#endif
#include "netinet/ipl.h"
#if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
# include <sys/callout.h>
extern struct callout ipf_slowtimer_ch;
#endif
/* END OF INCLUDES */
#ifndef _KERNEL
# include "ipf.h"
# include "ipt.h"
extern int opts;
extern int blockreason;
#endif /* _KERNEL */
#define FASTROUTE_RECURSION
#define LBUMP(x) softc->x++
#define LBUMPD(x, y) do { softc->x.y++; DT(y); } while (0)
static inline int ipf_check_ipf(fr_info_t *, frentry_t *, int);
static u_32_t ipf_checkcipso(fr_info_t *, u_char *, int);
static u_32_t ipf_checkripso(u_char *);
static u_32_t ipf_decaps(fr_info_t *, u_32_t, int);
#ifdef IPFILTER_LOG
static frentry_t *ipf_dolog(fr_info_t *, u_32_t *);
#endif
static int ipf_flushlist(ipf_main_softc_t *, int *, frentry_t **);
static int ipf_flush_groups(ipf_main_softc_t *, frgroup_t **,
int);
static ipfunc_t ipf_findfunc(ipfunc_t);
static void *ipf_findlookup(ipf_main_softc_t *, int, frentry_t *,
i6addr_t *, i6addr_t *);
static frentry_t *ipf_firewall(fr_info_t *, u_32_t *);
static int ipf_fr_matcharray(fr_info_t *, int *);
static int ipf_frruleiter(ipf_main_softc_t *, void *, int,
void *);
static void ipf_funcfini(ipf_main_softc_t *, frentry_t *);
static int ipf_funcinit(ipf_main_softc_t *, frentry_t *);
static int ipf_geniter(ipf_main_softc_t *, ipftoken_t *,
ipfgeniter_t *);
static void ipf_getstat(ipf_main_softc_t *,
struct friostat *, int);
static int ipf_group_flush(ipf_main_softc_t *, frgroup_t *);
static void ipf_group_free(frgroup_t *);
static int ipf_grpmapfini(struct ipf_main_softc_s *,
frentry_t *);
static int ipf_grpmapinit(struct ipf_main_softc_s *,
frentry_t *);
static frentry_t *ipf_nextrule(ipf_main_softc_t *, int, int,
frentry_t *, int);
static int ipf_portcheck(frpcmp_t *, u_32_t);
static inline int ipf_pr_ah(fr_info_t *);
static inline void ipf_pr_esp(fr_info_t *);
static inline void ipf_pr_gre(fr_info_t *);
static inline void ipf_pr_udp(fr_info_t *);
static inline void ipf_pr_tcp(fr_info_t *);
static inline void ipf_pr_icmp(fr_info_t *);
static inline void ipf_pr_ipv4hdr(fr_info_t *);
static inline void ipf_pr_short(fr_info_t *, int);
static inline int ipf_pr_tcpcommon(fr_info_t *);
static inline int ipf_pr_udpcommon(fr_info_t *);
static void ipf_rule_delete(ipf_main_softc_t *, frentry_t *f,
int, int);
static void ipf_rule_expire_insert(ipf_main_softc_t *,
frentry_t *, int);
static int ipf_synclist(ipf_main_softc_t *, frentry_t *,
void *);
static void ipf_token_flush(ipf_main_softc_t *);
static void ipf_token_unlink(ipf_main_softc_t *,
ipftoken_t *);
static ipftuneable_t *ipf_tune_findbyname(ipftuneable_t *,
const char *);
static ipftuneable_t *ipf_tune_findbycookie(ipftuneable_t **, void *,
void **);
static int ipf_updateipid(fr_info_t *);
static int ipf_settimeout(struct ipf_main_softc_s *,
struct ipftuneable *,
ipftuneval_t *);
#if !defined(_KERNEL) || SOLARIS
static int ppsratecheck(struct timeval *, int *, int);
#endif
/*
* bit values for identifying presence of individual IP options
* All of these tables should be ordered by increasing key value on the left
* hand side to allow for binary searching of the array and include a trailer
* with a 0 for the bitmask for linear searches to easily find the end with.
*/
static const struct optlist ipopts[] = {
{ IPOPT_NOP, 0x000001 },
{ IPOPT_RR, 0x000002 },
{ IPOPT_ZSU, 0x000004 },
{ IPOPT_MTUP, 0x000008 },
{ IPOPT_MTUR, 0x000010 },
{ IPOPT_ENCODE, 0x000020 },
{ IPOPT_TS, 0x000040 },
{ IPOPT_TR, 0x000080 },
{ IPOPT_SECURITY, 0x000100 },
{ IPOPT_LSRR, 0x000200 },
{ IPOPT_E_SEC, 0x000400 },
{ IPOPT_CIPSO, 0x000800 },
{ IPOPT_SATID, 0x001000 },
{ IPOPT_SSRR, 0x002000 },
{ IPOPT_ADDEXT, 0x004000 },
{ IPOPT_VISA, 0x008000 },
{ IPOPT_IMITD, 0x010000 },
{ IPOPT_EIP, 0x020000 },
{ IPOPT_FINN, 0x040000 },
{ 0, 0x000000 }
};
#ifdef USE_INET6
static const struct optlist ip6exthdr[] = {
{ IPPROTO_HOPOPTS, 0x000001 },
{ IPPROTO_IPV6, 0x000002 },
{ IPPROTO_ROUTING, 0x000004 },
{ IPPROTO_FRAGMENT, 0x000008 },
{ IPPROTO_ESP, 0x000010 },
{ IPPROTO_AH, 0x000020 },
{ IPPROTO_NONE, 0x000040 },
{ IPPROTO_DSTOPTS, 0x000080 },
{ IPPROTO_MOBILITY, 0x000100 },
{ 0, 0 }
};
#endif
/*
* bit values for identifying presence of individual IP security options
*/
static const struct optlist secopt[] = {
{ IPSO_CLASS_RES4, 0x01 },
{ IPSO_CLASS_TOPS, 0x02 },
{ IPSO_CLASS_SECR, 0x04 },
{ IPSO_CLASS_RES3, 0x08 },
{ IPSO_CLASS_CONF, 0x10 },
{ IPSO_CLASS_UNCL, 0x20 },
{ IPSO_CLASS_RES2, 0x40 },
{ IPSO_CLASS_RES1, 0x80 }
};
char ipfilter_version[] = IPL_VERSION;
int ipf_features = 0
#ifdef IPFILTER_LKM
| IPF_FEAT_LKM
#endif
#ifdef IPFILTER_LOG
| IPF_FEAT_LOG
#endif
| IPF_FEAT_LOOKUP
#ifdef IPFILTER_BPF
| IPF_FEAT_BPF
#endif
#ifdef IPFILTER_COMPILED
| IPF_FEAT_COMPILED
#endif
#ifdef IPFILTER_CKSUM
| IPF_FEAT_CKSUM
#endif
| IPF_FEAT_SYNC
#ifdef IPFILTER_SCAN
| IPF_FEAT_SCAN
#endif
#ifdef USE_INET6
| IPF_FEAT_IPV6
#endif
;
/*
* Table of functions available for use with call rules.
*/
static ipfunc_resolve_t ipf_availfuncs[] = {
{ "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini },
{ "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini },
{ "", NULL, NULL, NULL }
};
static ipftuneable_t ipf_main_tuneables[] = {
{ { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) },
"ipf_flags", 0, 0xffffffff,
stsizeof(ipf_main_softc_t, ipf_flags),
0, NULL, NULL },
{ { (void *)offsetof(struct ipf_main_softc_s, ipf_active) },
"active", 0, 0,
stsizeof(ipf_main_softc_t, ipf_active),
IPFT_RDONLY, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) },
"control_forwarding", 0, 1,
stsizeof(ipf_main_softc_t, ipf_control_forwarding),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) },
"update_ipid", 0, 1,
stsizeof(ipf_main_softc_t, ipf_update_ipid),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) },
"chksrc", 0, 1,
stsizeof(ipf_main_softc_t, ipf_chksrc),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_minttl) },
"min_ttl", 0, 1,
stsizeof(ipf_main_softc_t, ipf_minttl),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) },
"icmp_minfragmtu", 0, 1,
stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_pass) },
"default_pass", 0, 0xffffffff,
stsizeof(ipf_main_softc_t, ipf_pass),
0, NULL, NULL },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) },
"tcp_idle_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcpidletimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) },
"tcp_close_wait", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcpclosewait),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) },
"tcp_last_ack", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcplastack),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) },
"tcp_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcptimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) },
"tcp_syn_sent", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcpsynsent),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) },
"tcp_syn_received", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcpsynrecv),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) },
"tcp_closed", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcpclosed),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) },
"tcp_half_closed", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcphalfclosed),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) },
"tcp_time_wait", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_tcptimewait),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) },
"udp_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_udptimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) },
"udp_ack_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_udpacktimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) },
"icmp_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_icmptimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) },
"icmp_ack_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_icmpacktimeout),
0, NULL, ipf_settimeout },
{ { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) },
"ip_timeout", 1, 0x7fffffff,
stsizeof(ipf_main_softc_t, ipf_iptimeout),
0, NULL, ipf_settimeout },
#if defined(INSTANCES) && defined(_KERNEL)
{ { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) },
"intercept_loopback", 0, 1,
stsizeof(ipf_main_softc_t, ipf_get_loopback),
0, NULL, ipf_set_loopback },
#endif
{ { 0 },
NULL, 0, 0,
0,
0, NULL, NULL }
};
/*
* The next section of code is a collection of small routines that set
* fields in the fr_info_t structure passed based on properties of the
* current packet. There are different routines for the same protocol
* for each of IPv4 and IPv6. Adding a new protocol, for which there
* will "special" inspection for setup, is now more easily done by adding
* a new routine and expanding the ipf_pr_ipinit*() function rather than by
* adding more code to a growing switch statement.
*/
#ifdef USE_INET6
static inline int ipf_pr_ah6(fr_info_t *);
static inline void ipf_pr_esp6(fr_info_t *);
static inline void ipf_pr_gre6(fr_info_t *);
static inline void ipf_pr_udp6(fr_info_t *);
static inline void ipf_pr_tcp6(fr_info_t *);
static inline void ipf_pr_icmp6(fr_info_t *);
static inline void ipf_pr_ipv6hdr(fr_info_t *);
static inline void ipf_pr_short6(fr_info_t *, int);
static inline int ipf_pr_hopopts6(fr_info_t *);
static inline int ipf_pr_mobility6(fr_info_t *);
static inline int ipf_pr_routing6(fr_info_t *);
static inline int ipf_pr_dstopts6(fr_info_t *);
static inline int ipf_pr_fragment6(fr_info_t *);
static inline struct ip6_ext *ipf_pr_ipv6exthdr(fr_info_t *, int, int);
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_short6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* xmin(I) - minimum header size */
/* */
/* IPv6 Only */
/* This is function enforces the 'is a packet too short to be legit' rule */
/* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
/* for ipf_pr_short() for more details. */
/* ------------------------------------------------------------------------ */
static inline void
ipf_pr_short6(fr_info_t *fin, int xmin)
{
if (fin->fin_dlen < xmin)
fin->fin_flx |= FI_SHORT;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_ipv6hdr */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Copy values from the IPv6 header into the fr_info_t struct and call the */
/* per-protocol analyzer if it exists. In validating the packet, a protocol*/
/* analyzer may pullup or free the packet itself so we need to be vigiliant */
/* of that possibility arising. */
/* ------------------------------------------------------------------------ */
static inline void
ipf_pr_ipv6hdr(fr_info_t *fin)
{
ip6_t *ip6 = (ip6_t *)fin->fin_ip;
int p, go = 1, i, hdrcount;
fr_ip_t *fi = &fin->fin_fi;
fin->fin_off = 0;
fi->fi_tos = 0;
fi->fi_optmsk = 0;
fi->fi_secmsk = 0;
fi->fi_auth = 0;
p = ip6->ip6_nxt;
fin->fin_crc = p;
fi->fi_ttl = ip6->ip6_hlim;
fi->fi_src.in6 = ip6->ip6_src;
fin->fin_crc += fi->fi_src.i6[0];
fin->fin_crc += fi->fi_src.i6[1];
fin->fin_crc += fi->fi_src.i6[2];
fin->fin_crc += fi->fi_src.i6[3];
fi->fi_dst.in6 = ip6->ip6_dst;
fin->fin_crc += fi->fi_dst.i6[0];
fin->fin_crc += fi->fi_dst.i6[1];
fin->fin_crc += fi->fi_dst.i6[2];
fin->fin_crc += fi->fi_dst.i6[3];
fin->fin_id = 0;
if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
hdrcount = 0;
while (go && !(fin->fin_flx & FI_SHORT)) {
switch (p)
{
case IPPROTO_UDP :
ipf_pr_udp6(fin);
go = 0;
break;
case IPPROTO_TCP :
ipf_pr_tcp6(fin);
go = 0;
break;
case IPPROTO_ICMPV6 :
ipf_pr_icmp6(fin);
go = 0;
break;
case IPPROTO_GRE :
ipf_pr_gre6(fin);
go = 0;
break;
case IPPROTO_HOPOPTS :
p = ipf_pr_hopopts6(fin);
break;
case IPPROTO_MOBILITY :
p = ipf_pr_mobility6(fin);
break;
case IPPROTO_DSTOPTS :
p = ipf_pr_dstopts6(fin);
break;
case IPPROTO_ROUTING :
p = ipf_pr_routing6(fin);
break;
case IPPROTO_AH :
p = ipf_pr_ah6(fin);
break;
case IPPROTO_ESP :
ipf_pr_esp6(fin);
go = 0;
break;
case IPPROTO_IPV6 :
for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
if (ip6exthdr[i].ol_val == p) {
fin->fin_flx |= ip6exthdr[i].ol_bit;
break;
}
go = 0;
break;
case IPPROTO_NONE :
go = 0;
break;
case IPPROTO_FRAGMENT :
p = ipf_pr_fragment6(fin);
/*
* Given that the only fragments we want to let through
* (where fin_off != 0) are those where the non-first
* fragments only have data, we can safely stop looking
* at headers if this is a non-leading fragment.
*/
if (fin->fin_off != 0)
go = 0;
break;
default :
go = 0;
break;
}
hdrcount++;
/*
* It is important to note that at this point, for the
* extension headers (go != 0), the entire header may not have
* been pulled up when the code gets to this point. This is
* only done for "go != 0" because the other header handlers
* will all pullup their complete header. The other indicator
* of an incomplete packet is that this was just an extension
* header.
*/
if ((go != 0) && (p != IPPROTO_NONE) &&
(ipf_pr_pullup(fin, 0) == -1)) {
p = IPPROTO_NONE;
break;
}
}
/*
* Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup
* and destroy whatever packet was here. The caller of this function
* expects us to return if there is a problem with ipf_pullup.
*/
if (fin->fin_m == NULL) {
ipf_main_softc_t *softc = fin->fin_main_soft;
LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad);
return;
}
fi->fi_p = p;
/*
* IPv6 fragment case 1 - see comment for ipf_pr_fragment6().
* "go != 0" implies the above loop hasn't arrived at a layer 4 header.
*/
if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) {
ipf_main_softc_t *softc = fin->fin_main_soft;
fin->fin_flx |= FI_BAD;
DT2(ipf_fi_bad_ipv6_frag_1, fr_info_t *, fin, int, go);
LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag);
LBUMP(ipf_stats[fin->fin_out].fr_v6_bad);
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_ipv6exthdr */
/* Returns: struct ip6_ext * - pointer to the start of the next header */
/* or NULL if there is a prolblem. */
/* Parameters: fin(I) - pointer to packet information */
/* multiple(I) - flag indicating yes/no if multiple occurances */
/* of this extension header are allowed. */
/* proto(I) - protocol number for this extension header */
/* */
/* IPv6 Only */
/* This function embodies a number of common checks that all IPv6 extension */
/* headers must be subjected to. For example, making sure the packet is */
/* big enough for it to be in, checking if it is repeated and setting a */
/* flag to indicate its presence. */
/* ------------------------------------------------------------------------ */
static inline struct ip6_ext *
ipf_pr_ipv6exthdr(fr_info_t *fin, int multiple, int proto)
{
ipf_main_softc_t *softc = fin->fin_main_soft;
struct ip6_ext *hdr;
u_short shift;
int i;
fin->fin_flx |= FI_V6EXTHDR;
/* 8 is default length of extension hdr */
if ((fin->fin_dlen - 8) < 0) {
fin->fin_flx |= FI_SHORT;
LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short);
return (NULL);
}
if (ipf_pr_pullup(fin, 8) == -1) {
LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup);
return (NULL);
}
hdr = fin->fin_dp;
switch (proto)
{
case IPPROTO_FRAGMENT :
shift = 8;
break;
default :
shift = 8 + (hdr->ip6e_len << 3);
break;
}
if (shift > fin->fin_dlen) { /* Nasty extension header length? */
fin->fin_flx |= FI_BAD;
DT3(ipf_fi_bad_pr_ipv6exthdr_len, fr_info_t *, fin, u_short, shift, u_short, fin->fin_dlen);
LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen);
return (NULL);
}
fin->fin_dp = (char *)fin->fin_dp + shift;
fin->fin_dlen -= shift;
/*
* If we have seen a fragment header, do not set any flags to indicate
* the presence of this extension header as it has no impact on the
* end result until after it has been defragmented.
*/
if (fin->fin_flx & FI_FRAG)
return (hdr);
for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
if (ip6exthdr[i].ol_val == proto) {
/*
* Most IPv6 extension headers are only allowed once.
*/
if ((multiple == 0) &&
((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) {
fin->fin_flx |= FI_BAD;
DT2(ipf_fi_bad_ipv6exthdr_once, fr_info_t *, fin, u_int, (fin->fin_optmsk & ip6exthdr[i].ol_bit));
} else
fin->fin_optmsk |= ip6exthdr[i].ol_bit;
break;
}
return (hdr);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_hopopts6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks pending hop by hop options extension header */
/* ------------------------------------------------------------------------ */
static inline int
ipf_pr_hopopts6(fr_info_t *fin)
{
struct ip6_ext *hdr;
hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
if (hdr == NULL)
return (IPPROTO_NONE);
return (hdr->ip6e_nxt);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_mobility6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks the IPv6 mobility extension header */
/* ------------------------------------------------------------------------ */
static inline int
ipf_pr_mobility6(fr_info_t *fin)
{
struct ip6_ext *hdr;
hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY);
if (hdr == NULL)
return (IPPROTO_NONE);
return (hdr->ip6e_nxt);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_routing6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks pending routing extension header */
/* ------------------------------------------------------------------------ */
static inline int
ipf_pr_routing6(fr_info_t *fin)
{
struct ip6_routing *hdr;
hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING);
if (hdr == NULL)
return (IPPROTO_NONE);
switch (hdr->ip6r_type)
{
case 0 :
/*
* Nasty extension header length?
*/
if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) ||
(hdr->ip6r_segleft && (hdr->ip6r_len & 1))) {
ipf_main_softc_t *softc = fin->fin_main_soft;
fin->fin_flx |= FI_BAD;
DT1(ipf_fi_bad_routing6, fr_info_t *, fin);
LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad);
return (IPPROTO_NONE);
}
break;
default :
break;
}
return (hdr->ip6r_nxt);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_fragment6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Examine the IPv6 fragment header and extract fragment offset information.*/
/* */
/* Fragments in IPv6 are extraordinarily difficult to deal with - much more */
/* so than in IPv4. There are 5 cases of fragments with IPv6 that all */
/* packets with a fragment header can fit into. They are as follows: */
/* */
/* 1. [IPv6][0-n EH][FH][0-n EH] (no L4HDR present) */
/* 2. [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short) */
/* 3. [IPV6][0-n EH][FH][L4HDR part][0-n data] (short) */
/* 4. [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data] */
/* 5. [IPV6][0-n EH][FH][data] */
/* */
/* IPV6 = IPv6 header, FH = Fragment Header, */
/* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */
/* */
/* Packets that match 1, 2, 3 will be dropped as the only reasonable */
/* scenario in which they happen is in extreme circumstances that are most */
/* likely to be an indication of an attack rather than normal traffic. */
/* A type 3 packet may be sent by an attacked after a type 4 packet. There */
/* are two rules that can be used to guard against type 3 packets: L4 */
/* headers must always be in a packet that has the offset field set to 0 */
/* and no packet is allowed to overlay that where offset = 0. */
/* ------------------------------------------------------------------------ */
static inline int
ipf_pr_fragment6(fr_info_t *fin)
{
ipf_main_softc_t *softc = fin->fin_main_soft;
struct ip6_frag *frag;
fin->fin_flx |= FI_FRAG;
frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT);
if (frag == NULL) {
LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad);
return (IPPROTO_NONE);
}
if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
/*
* Any fragment that isn't the last fragment must have its
* length as a multiple of 8.
*/
if ((fin->fin_plen & 7) != 0) {
fin->fin_flx |= FI_BAD;
DT2(ipf_fi_bad_frag_not_8, fr_info_t *, fin, u_int, (fin->fin_plen & 7));
}
}
fin->fin_fraghdr = frag;
fin->fin_id = frag->ip6f_ident;
fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
if (fin->fin_off != 0)
fin->fin_flx |= FI_FRAGBODY;
/*
* Jumbograms aren't handled, so the max. length is 64k
*/
if ((fin->fin_off << 3) + fin->fin_dlen > 65535) {
fin->fin_flx |= FI_BAD;
DT2(ipf_fi_bad_jumbogram, fr_info_t *, fin, u_int, ((fin->fin_off << 3) + fin->fin_dlen));
}
/*
* We don't know where the transport layer header (or whatever is next
* is), as it could be behind destination options (amongst others) so
* return the fragment header as the type of packet this is. Note that
* this effectively disables the fragment cache for > 1 protocol at a
* time.
*/
return (frag->ip6f_nxt);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_dstopts6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks pending destination options extension header */
/* ------------------------------------------------------------------------ */
static inline int
ipf_pr_dstopts6(fr_info_t *fin)
{
ipf_main_softc_t *softc = fin->fin_main_soft;
struct ip6_ext *hdr;
hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS);
if (hdr == NULL) {
LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad);
return (IPPROTO_NONE);
}
return (hdr->ip6e_nxt);
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_icmp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This routine is mainly concerned with determining the minimum valid size */
/* for an ICMPv6 packet. */
/* ------------------------------------------------------------------------ */
static inline void
ipf_pr_icmp6(fr_info_t *fin)
{
int minicmpsz = sizeof(struct icmp6_hdr);
struct icmp6_hdr *icmp6;
if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) {
ipf_main_softc_t *softc = fin->fin_main_soft;
LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup);
return;
}
if (fin->fin_dlen > 1) {
ip6_t *ip6;
icmp6 = fin->fin_dp;
fin->fin_data[0] = *(u_short *)icmp6;
if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
fin->fin_flx |= FI_ICMPQUERY;
switch (icmp6->icmp6_type)
{
case ICMP6_ECHO_REPLY :
case ICMP6_ECHO_REQUEST :
if (fin->fin_dlen >= 6)
fin->fin_data[1] = icmp6->icmp6_id;
minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
break;
case ICMP6_DST_UNREACH :
case ICMP6_PACKET_TOO_BIG :
case ICMP6_TIME_EXCEEDED :
case ICMP6_PARAM_PROB :
fin->fin_flx |= FI_ICMPERR;
minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
if (fin->fin_plen < ICMP6ERR_IPICMPHLEN)
break;
if (M_LEN(fin->fin_m) < fin->fin_plen) {
if (ipf_coalesce(fin) != 1)
return;
}
if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1)
return;
/*
* If the destination of this packet doesn't match the
* source of the original packet then this packet is
* not correct.
*/
icmp6 = fin->fin_dp;
ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN);
if (IP6_NEQ(&fin->fin_fi.fi_dst,
(i6addr_t *)&ip6->ip6_src)) {
fin->fin_flx |= FI_BAD;
DT1(ipf_fi_bad_icmp6, fr_info_t *, fin);
}
break;
default :
break;
}
}
ipf_pr_short6(fin, minicmpsz);
if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) {
u_char p = fin->fin_p;
fin->fin_p = IPPROTO_ICMPV6;
ipf_checkv6sum(fin);
fin->fin_p = p;
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_udp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Analyse the packet for IPv6/UDP properties. */
/* Is not expected to be called for fragmented packets. */
/* ------------------------------------------------------------------------ */
static inline void
ipf_pr_udp6(fr_info_t *fin)
{
if (ipf_pr_udpcommon(fin) == 0) {
u_char p = fin->fin_p;
fin->fin_p = IPPROTO_UDP;
ipf_checkv6sum(fin);
fin->fin_p = p;
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_tcp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Analyse the packet for IPv6/TCP properties. */
/* Is not expected to be called for fragmented packets. */
/* ------------------------------------------------------------------------ */
static inline void
ipf_pr_tcp6(fr_info_t *fin)
{
if (ipf_pr_tcpcommon(fin) == 0) {
u_char p = fin->fin_p;
fin->fin_p = IPPROTO_TCP;
ipf_checkv6sum(fin);
fin->fin_p = p;
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_pr_esp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Analyse the packet for ESP properties. */
/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */