1 /*
2 * Copyright (C) 1993-2003 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 *
6 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
7 *
8 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
9 */
10
11 #if defined(KERNEL) || defined(_KERNEL)
12 # undef KERNEL
13 # undef _KERNEL
14 # define KERNEL 1
15 # define _KERNEL 1
16 #endif
17 #include <sys/errno.h>
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/time.h>
21 #if defined(__NetBSD__)
22 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
23 # include "opt_ipfilter_log.h"
24 # endif
25 #endif
26 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
27 (__FreeBSD_version >= 220000)
28 # if (__FreeBSD_version >= 400000)
29 # if !defined(IPFILTER_LKM)
30 # include "opt_inet6.h"
31 # endif
32 # if (__FreeBSD_version == 400019)
33 # define CSUM_DELAY_DATA
34 # endif
35 # endif
36 # include <sys/filio.h>
37 #else
38 # include <sys/ioctl.h>
39 #endif
40 #if !defined(_AIX51)
41 # include <sys/fcntl.h>
42 #endif
43 #if defined(_KERNEL)
44 # include <sys/systm.h>
45 # include <sys/file.h>
46 #else
47 # include <stdio.h>
48 # include <string.h>
49 # include <stdlib.h>
50 # include <stddef.h>
51 # include <sys/file.h>
52 # define _KERNEL
53 # ifdef __OpenBSD__
54 struct file;
55 # endif
56 # include <sys/uio.h>
57 # undef _KERNEL
58 #endif
59 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
60 !defined(linux)
61 # include <sys/mbuf.h>
62 #else
63 # if !defined(linux)
64 # include <sys/byteorder.h>
65 # endif
66 # if (SOLARIS2 < 5) && defined(sun)
67 # include <sys/dditypes.h>
68 # endif
69 #endif
70 #ifdef __hpux
71 # define _NET_ROUTE_INCLUDED
72 #endif
73 #if !defined(linux)
74 # include <sys/protosw.h>
75 #endif
76 #include <sys/socket.h>
77 #include <net/if.h>
78 #ifdef sun
79 # include <net/af.h>
80 #endif
81 #if !defined(_KERNEL) && defined(__FreeBSD__)
82 # include "radix_ipf.h"
83 #endif
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #if !defined(linux)
89 # include <netinet/ip_var.h>
90 #endif
91 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
92 # include <sys/hashing.h>
93 # include <netinet/in_var.h>
94 #endif
95 #include <netinet/tcp.h>
96 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
97 # include <netinet/udp.h>
98 # include <netinet/ip_icmp.h>
99 #endif
100 #ifdef __hpux
101 # undef _NET_ROUTE_INCLUDED
102 #endif
103 #include "netinet/ip_compat.h"
104 #ifdef USE_INET6
105 # include <netinet/icmp6.h>
106 # if !defined(SOLARIS) && defined(_KERNEL) && !defined(__osf__) && \
107 !defined(__hpux)
108 # include <netinet6/in6_var.h>
109 # endif
110 #endif
111 #include <netinet/tcpip.h>
112 #include "netinet/ip_fil.h"
113 #include "netinet/ip_nat.h"
114 #include "netinet/ip_frag.h"
115 #include "netinet/ip_state.h"
116 #include "netinet/ip_proxy.h"
117 #include "netinet/ip_auth.h"
118 #include "netinet/ipf_stack.h"
119 #ifdef IPFILTER_SCAN
120 # include "netinet/ip_scan.h"
121 #endif
122 #ifdef IPFILTER_SYNC
123 # include "netinet/ip_sync.h"
124 #endif
125 #include "netinet/ip_pool.h"
126 #include "netinet/ip_htable.h"
127 #ifdef IPFILTER_COMPILED
128 # include "netinet/ip_rules.h"
129 #endif
130 #if defined(IPFILTER_BPF) && defined(_KERNEL)
131 # include <net/bpf.h>
132 #endif
133 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
134 # include <sys/malloc.h>
135 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
136 # include "opt_ipfilter.h"
137 # endif
138 #endif
139 #include "netinet/ipl.h"
140 #if defined(_KERNEL)
141 #include <sys/sunddi.h>
142 #endif
143 /* END OF INCLUDES */
144
145 #if !defined(lint)
146 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
147 static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $";
148 #endif
149
150 #ifndef _KERNEL
151 # include "ipf.h"
152 # include "ipt.h"
153 # include "bpf-ipf.h"
154 extern int opts;
155
156 # define FR_VERBOSE(verb_pr) verbose verb_pr
157 # define FR_DEBUG(verb_pr) debug verb_pr
158 #else /* #ifndef _KERNEL */
159 # define FR_VERBOSE(verb_pr)
160 # define FR_DEBUG(verb_pr)
161 #endif /* _KERNEL */
162
163
164 char ipfilter_version[] = IPL_VERSION;
165 int fr_features = 0
166 #ifdef IPFILTER_LKM
167 | IPF_FEAT_LKM
168 #endif
169 #ifdef IPFILTER_LOG
170 | IPF_FEAT_LOG
171 #endif
172 #ifdef IPFILTER_LOOKUP
173 | IPF_FEAT_LOOKUP
174 #endif
175 #ifdef IPFILTER_BPF
176 | IPF_FEAT_BPF
177 #endif
178 #ifdef IPFILTER_COMPILED
179 | IPF_FEAT_COMPILED
180 #endif
181 #ifdef IPFILTER_CKSUM
182 | IPF_FEAT_CKSUM
183 #endif
184 #ifdef IPFILTER_SYNC
185 | IPF_FEAT_SYNC
186 #endif
187 #ifdef IPFILTER_SCAN
188 | IPF_FEAT_SCAN
189 #endif
190 #ifdef USE_INET6
191 | IPF_FEAT_IPV6
192 #endif
193 ;
194
195 #define IPF_BUMP(x) (x)++
196
197 static INLINE int fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
198 static INLINE int fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
199 static int fr_portcheck __P((frpcmp_t *, u_short *));
200 static int frflushlist __P((int, minor_t, int *, frentry_t **,
201 ipf_stack_t *));
202 static ipfunc_t fr_findfunc __P((ipfunc_t));
203 static frentry_t *fr_firewall __P((fr_info_t *, u_32_t *));
204 static int fr_funcinit __P((frentry_t *fr, ipf_stack_t *));
205 static INLINE void frpr_ah __P((fr_info_t *));
206 static INLINE void frpr_esp __P((fr_info_t *));
207 static INLINE void frpr_gre __P((fr_info_t *));
208 static INLINE void frpr_udp __P((fr_info_t *));
209 static INLINE void frpr_tcp __P((fr_info_t *));
210 static INLINE void frpr_icmp __P((fr_info_t *));
211 static INLINE void frpr_ipv4hdr __P((fr_info_t *));
212 static INLINE int frpr_pullup __P((fr_info_t *, int));
213 static INLINE void frpr_short __P((fr_info_t *, int));
214 static INLINE void frpr_tcpcommon __P((fr_info_t *));
215 static INLINE void frpr_udpcommon __P((fr_info_t *));
216 static INLINE int fr_updateipid __P((fr_info_t *));
217 #ifdef IPFILTER_LOOKUP
218 static int fr_grpmapinit __P((frentry_t *fr, ipf_stack_t *));
219 static INLINE void *fr_resolvelookup __P((u_int, u_int, lookupfunc_t *,
220 ipf_stack_t *));
221 #endif
222 static void frsynclist __P((int, int, void *, char *, frentry_t *,
223 ipf_stack_t *));
224 static void *fr_ifsync __P((int, int, char *, char *,
225 void *, void *, ipf_stack_t *));
226 static ipftuneable_t *fr_findtunebyname __P((const char *, ipf_stack_t *));
227 static ipftuneable_t *fr_findtunebycookie __P((void *, void **, ipf_stack_t *));
228
229 /*
230 * bit values for identifying presence of individual IP options
231 * All of these tables should be ordered by increasing key value on the left
232 * hand side to allow for binary searching of the array and include a trailer
233 * with a 0 for the bitmask for linear searches to easily find the end with.
234 */
235 const struct optlist ipopts[20] = {
236 { IPOPT_NOP, 0x000001 },
237 { IPOPT_RR, 0x000002 },
238 { IPOPT_ZSU, 0x000004 },
239 { IPOPT_MTUP, 0x000008 },
240 { IPOPT_MTUR, 0x000010 },
241 { IPOPT_ENCODE, 0x000020 },
242 { IPOPT_TS, 0x000040 },
243 { IPOPT_TR, 0x000080 },
244 { IPOPT_SECURITY, 0x000100 },
245 { IPOPT_LSRR, 0x000200 },
246 { IPOPT_E_SEC, 0x000400 },
247 { IPOPT_CIPSO, 0x000800 },
248 { IPOPT_SATID, 0x001000 },
249 { IPOPT_SSRR, 0x002000 },
250 { IPOPT_ADDEXT, 0x004000 },
251 { IPOPT_VISA, 0x008000 },
252 { IPOPT_IMITD, 0x010000 },
253 { IPOPT_EIP, 0x020000 },
254 { IPOPT_FINN, 0x040000 },
255 { 0, 0x000000 }
256 };
257
258 #ifdef USE_INET6
259 struct optlist ip6exthdr[] = {
260 { IPPROTO_HOPOPTS, 0x000001 },
261 { IPPROTO_IPV6, 0x000002 },
262 { IPPROTO_ROUTING, 0x000004 },
263 { IPPROTO_FRAGMENT, 0x000008 },
264 { IPPROTO_ESP, 0x000010 },
265 { IPPROTO_AH, 0x000020 },
266 { IPPROTO_NONE, 0x000040 },
267 { IPPROTO_DSTOPTS, 0x000080 },
268 { 0, 0 }
269 };
270 #endif
271
272 struct optlist tcpopts[] = {
273 { TCPOPT_NOP, 0x000001 },
274 { TCPOPT_MAXSEG, 0x000002 },
275 { TCPOPT_WINDOW, 0x000004 },
276 { TCPOPT_SACK_PERMITTED, 0x000008 },
277 { TCPOPT_SACK, 0x000010 },
278 { TCPOPT_TIMESTAMP, 0x000020 },
279 { 0, 0x000000 }
280 };
281
282 /*
283 * bit values for identifying presence of individual IP security options
284 */
285 const struct optlist secopt[8] = {
286 { IPSO_CLASS_RES4, 0x01 },
287 { IPSO_CLASS_TOPS, 0x02 },
288 { IPSO_CLASS_SECR, 0x04 },
289 { IPSO_CLASS_RES3, 0x08 },
290 { IPSO_CLASS_CONF, 0x10 },
291 { IPSO_CLASS_UNCL, 0x20 },
292 { IPSO_CLASS_RES2, 0x40 },
293 { IPSO_CLASS_RES1, 0x80 }
294 };
295
296
297 /*
298 * Table of functions available for use with call rules.
299 */
300 static ipfunc_resolve_t fr_availfuncs[] = {
301 #ifdef IPFILTER_LOOKUP
302 { "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit },
303 { "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit },
304 #endif
305 { "", NULL }
306 };
307
308
309 /*
310 * Below we declare a list of constants used only by the ipf_extraflush()
311 * routine. We are placing it here, instead of in ipf_extraflush() itself,
312 * because we want to make it visible to tools such as mdb, nm etc., so the
313 * values can easily be altered during debugging.
314 */
315 static const int idletime_tab[] = {
316 IPF_TTLVAL(30), /* 30 seconds */
317 IPF_TTLVAL(1800), /* 30 minutes */
318 IPF_TTLVAL(43200), /* 12 hours */
319 IPF_TTLVAL(345600), /* 4 days */
320 };
321
322
323 /*
324 * The next section of code is a a collection of small routines that set
325 * fields in the fr_info_t structure passed based on properties of the
326 * current packet. There are different routines for the same protocol
327 * for each of IPv4 and IPv6. Adding a new protocol, for which there
328 * will "special" inspection for setup, is now more easily done by adding
329 * a new routine and expanding the frpr_ipinit*() function rather than by
330 * adding more code to a growing switch statement.
331 */
332 #ifdef USE_INET6
333 static INLINE int frpr_ah6 __P((fr_info_t *));
334 static INLINE void frpr_esp6 __P((fr_info_t *));
335 static INLINE void frpr_gre6 __P((fr_info_t *));
336 static INLINE void frpr_udp6 __P((fr_info_t *));
337 static INLINE void frpr_tcp6 __P((fr_info_t *));
338 static INLINE void frpr_icmp6 __P((fr_info_t *));
339 static INLINE void frpr_ipv6hdr __P((fr_info_t *));
340 static INLINE void frpr_short6 __P((fr_info_t *, int));
341 static INLINE int frpr_hopopts6 __P((fr_info_t *));
342 static INLINE int frpr_routing6 __P((fr_info_t *));
343 static INLINE int frpr_dstopts6 __P((fr_info_t *));
344 static INLINE int frpr_fragment6 __P((fr_info_t *));
345 static INLINE int frpr_ipv6exthdr __P((fr_info_t *, int, int));
346
347
348 /* ------------------------------------------------------------------------ */
349 /* Function: frpr_short6 */
350 /* Returns: void */
351 /* Parameters: fin(I) - pointer to packet information */
352 /* */
353 /* IPv6 Only */
354 /* This is function enforces the 'is a packet too short to be legit' rule */
355 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
356 /* for frpr_short() for more details. */
357 /* ------------------------------------------------------------------------ */
frpr_short6(fin,xmin)358 static INLINE void frpr_short6(fin, xmin)
359 fr_info_t *fin;
360 int xmin;
361 {
362
363 if (fin->fin_dlen < xmin)
364 fin->fin_flx |= FI_SHORT;
365 }
366
367
368 /* ------------------------------------------------------------------------ */
369 /* Function: frpr_ipv6hdr */
370 /* Returns: Nil */
371 /* Parameters: fin(I) - pointer to packet information */
372 /* */
373 /* IPv6 Only */
374 /* Copy values from the IPv6 header into the fr_info_t struct and call the */
375 /* per-protocol analyzer if it exists. */
376 /* ------------------------------------------------------------------------ */
frpr_ipv6hdr(fin)377 static INLINE void frpr_ipv6hdr(fin)
378 fr_info_t *fin;
379 {
380 ip6_t *ip6 = (ip6_t *)fin->fin_ip;
381 int p, go = 1, i, hdrcount;
382 fr_ip_t *fi = &fin->fin_fi;
383
384 fin->fin_off = 0;
385
386 fi->fi_tos = 0;
387 fi->fi_optmsk = 0;
388 fi->fi_secmsk = 0;
389 fi->fi_auth = 0;
390
391 p = ip6->ip6_nxt;
392 fi->fi_ttl = ip6->ip6_hlim;
393 fi->fi_src.in6 = ip6->ip6_src;
394 fi->fi_dst.in6 = ip6->ip6_dst;
395 fin->fin_id = 0;
396
397 hdrcount = 0;
398 while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) {
399 switch (p)
400 {
401 case IPPROTO_UDP :
402 frpr_udp6(fin);
403 go = 0;
404 break;
405
406 case IPPROTO_TCP :
407 frpr_tcp6(fin);
408 go = 0;
409 break;
410
411 case IPPROTO_ICMPV6 :
412 frpr_icmp6(fin);
413 go = 0;
414 break;
415
416 case IPPROTO_GRE :
417 frpr_gre6(fin);
418 go = 0;
419 break;
420
421 case IPPROTO_HOPOPTS :
422 /*
423 * hop by hop ext header is only allowed
424 * right after IPv6 header.
425 */
426 if (hdrcount != 0) {
427 fin->fin_flx |= FI_BAD;
428 p = IPPROTO_NONE;
429 } else {
430 p = frpr_hopopts6(fin);
431 }
432 break;
433
434 case IPPROTO_DSTOPTS :
435 p = frpr_dstopts6(fin);
436 break;
437
438 case IPPROTO_ROUTING :
439 p = frpr_routing6(fin);
440 break;
441
442 case IPPROTO_AH :
443 p = frpr_ah6(fin);
444 break;
445
446 case IPPROTO_ESP :
447 frpr_esp6(fin);
448 go = 0;
449 break;
450
451 case IPPROTO_IPV6 :
452 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
453 if (ip6exthdr[i].ol_val == p) {
454 fin->fin_flx |= ip6exthdr[i].ol_bit;
455 break;
456 }
457 go = 0;
458 break;
459
460 case IPPROTO_NONE :
461 go = 0;
462 break;
463
464 case IPPROTO_FRAGMENT :
465 p = frpr_fragment6(fin);
466 if (fin->fin_off != 0) /* Not the first frag */
467 go = 0;
468 break;
469
470 default :
471 go = 0;
472 break;
473 }
474 hdrcount++;
475
476 /*
477 * It is important to note that at this point, for the
478 * extension headers (go != 0), the entire header may not have
479 * been pulled up when the code gets to this point. This is
480 * only done for "go != 0" because the other header handlers
481 * will all pullup their complete header. The other indicator
482 * of an incomplete packet is that this was just an extension
483 * header.
484 */
485 if ((go != 0) && (p != IPPROTO_NONE) &&
486 (frpr_pullup(fin, 0) == -1)) {
487 p = IPPROTO_NONE;
488 go = 0;
489 }
490 }
491 fi->fi_p = p;
492 }
493
494
495 /* ------------------------------------------------------------------------ */
496 /* Function: frpr_ipv6exthdr */
497 /* Returns: int - value of the next header or IPPROTO_NONE if error */
498 /* Parameters: fin(I) - pointer to packet information */
499 /* multiple(I) - flag indicating yes/no if multiple occurances */
500 /* of this extension header are allowed. */
501 /* proto(I) - protocol number for this extension header */
502 /* */
503 /* IPv6 Only */
504 /* This function expects to find an IPv6 extension header at fin_dp. */
505 /* There must be at least 8 bytes of data at fin_dp for there to be a valid */
506 /* extension header present. If a good one is found, fin_dp is advanced to */
507 /* point at the first piece of data after the extension header, fin_exthdr */
508 /* points to the start of the extension header and the "protocol" of the */
509 /* *NEXT* header is returned. */
510 /* ------------------------------------------------------------------------ */
frpr_ipv6exthdr(fin,multiple,proto)511 static INLINE int frpr_ipv6exthdr(fin, multiple, proto)
512 fr_info_t *fin;
513 int multiple, proto;
514 {
515 struct ip6_ext *hdr;
516 u_short shift;
517 int i;
518
519 fin->fin_flx |= FI_V6EXTHDR;
520
521 /* 8 is default length of extension hdr */
522 if ((fin->fin_dlen - 8) < 0) {
523 fin->fin_flx |= FI_SHORT;
524 return IPPROTO_NONE;
525 }
526
527 if (frpr_pullup(fin, 8) == -1)
528 return IPPROTO_NONE;
529
530 hdr = fin->fin_dp;
531 shift = 8 + (hdr->ip6e_len << 3);
532 if (shift > fin->fin_dlen) { /* Nasty extension header length? */
533 fin->fin_flx |= FI_BAD;
534 return IPPROTO_NONE;
535 }
536
537 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
538 if (ip6exthdr[i].ol_val == proto) {
539 /*
540 * Most IPv6 extension headers are only allowed once.
541 */
542 if ((multiple == 0) &&
543 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0))
544 fin->fin_flx |= FI_BAD;
545 else
546 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
547 break;
548 }
549
550 fin->fin_dp = (char *)fin->fin_dp + shift;
551 fin->fin_dlen -= shift;
552
553 return hdr->ip6e_nxt;
554 }
555
556
557 /* ------------------------------------------------------------------------ */
558 /* Function: frpr_hopopts6 */
559 /* Returns: int - value of the next header or IPPROTO_NONE if error */
560 /* Parameters: fin(I) - pointer to packet information */
561 /* */
562 /* IPv6 Only */
563 /* This is function checks pending hop by hop options extension header */
564 /* ------------------------------------------------------------------------ */
frpr_hopopts6(fin)565 static INLINE int frpr_hopopts6(fin)
566 fr_info_t *fin;
567 {
568 return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
569 }
570
571
572 /* ------------------------------------------------------------------------ */
573 /* Function: frpr_routing6 */
574 /* Returns: int - value of the next header or IPPROTO_NONE if error */
575 /* Parameters: fin(I) - pointer to packet information */
576 /* */
577 /* IPv6 Only */
578 /* This is function checks pending routing extension header */
579 /* ------------------------------------------------------------------------ */
frpr_routing6(fin)580 static INLINE int frpr_routing6(fin)
581 fr_info_t *fin;
582 {
583 struct ip6_ext *hdr;
584 int shift;
585
586 hdr = fin->fin_dp;
587 if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE)
588 return IPPROTO_NONE;
589
590 shift = 8 + (hdr->ip6e_len << 3);
591 /*
592 * Nasty extension header length?
593 */
594 if ((hdr->ip6e_len << 3) & 15) {
595 fin->fin_flx |= FI_BAD;
596 /*
597 * Compensate for the changes made in frpr_ipv6exthdr()
598 */
599 fin->fin_dlen += shift;
600 fin->fin_dp = (char *)fin->fin_dp - shift;
601 return IPPROTO_NONE;
602 }
603
604 return hdr->ip6e_nxt;
605 }
606
607
608 /* ------------------------------------------------------------------------ */
609 /* Function: frpr_fragment6 */
610 /* Returns: int - value of the next header or IPPROTO_NONE if error */
611 /* Parameters: fin(I) - pointer to packet information */
612 /* */
613 /* IPv6 Only */
614 /* Examine the IPv6 fragment header and extract fragment offset information.*/
615 /* */
616 /* We don't know where the transport layer header (or whatever is next is), */
617 /* as it could be behind destination options (amongst others). Because */
618 /* there is no fragment cache, there is no knowledge about whether or not an*/
619 /* upper layer header has been seen (or where it ends) and thus we are not */
620 /* able to continue processing beyond this header with any confidence. */
621 /* ------------------------------------------------------------------------ */
frpr_fragment6(fin)622 static INLINE int frpr_fragment6(fin)
623 fr_info_t *fin;
624 {
625 struct ip6_frag *frag;
626
627 fin->fin_flx |= FI_FRAG;
628
629 /*
630 * A fragmented IPv6 packet implies that there must be something
631 * else after the fragment.
632 */
633 if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE)
634 return IPPROTO_NONE;
635
636 frag = (struct ip6_frag *)((char *)fin->fin_dp - sizeof(*frag));
637
638 /*
639 * If this fragment isn't the last then the packet length must
640 * be a multiple of 8.
641 */
642 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
643 fin->fin_flx |= FI_MOREFRAG;
644
645 if ((fin->fin_plen & 0x7) != 0)
646 fin->fin_flx |= FI_BAD;
647 }
648
649 fin->fin_id = frag->ip6f_ident;
650 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
651 if (fin->fin_off != 0)
652 fin->fin_flx |= FI_FRAGBODY;
653
654 return frag->ip6f_nxt;
655 }
656
657
658 /* ------------------------------------------------------------------------ */
659 /* Function: frpr_dstopts6 */
660 /* Returns: int - value of the next header or IPPROTO_NONE if error */
661 /* Parameters: fin(I) - pointer to packet information */
662 /* nextheader(I) - stores next header value */
663 /* */
664 /* IPv6 Only */
665 /* This is function checks pending destination options extension header */
666 /* ------------------------------------------------------------------------ */
frpr_dstopts6(fin)667 static INLINE int frpr_dstopts6(fin)
668 fr_info_t *fin;
669 {
670 return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS);
671 }
672
673
674 /* ------------------------------------------------------------------------ */
675 /* Function: frpr_icmp6 */
676 /* Returns: void */
677 /* Parameters: fin(I) - pointer to packet information */
678 /* */
679 /* IPv6 Only */
680 /* This routine is mainly concerned with determining the minimum valid size */
681 /* for an ICMPv6 packet. */
682 /* ------------------------------------------------------------------------ */
frpr_icmp6(fin)683 static INLINE void frpr_icmp6(fin)
684 fr_info_t *fin;
685 {
686 int minicmpsz = sizeof(struct icmp6_hdr);
687 struct icmp6_hdr *icmp6;
688
689 if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1)
690 return;
691
692 if (fin->fin_dlen > 1) {
693 icmp6 = fin->fin_dp;
694
695 fin->fin_data[0] = *(u_short *)icmp6;
696
697 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
698 fin->fin_flx |= FI_ICMPQUERY;
699
700 switch (icmp6->icmp6_type)
701 {
702 case ICMP6_ECHO_REPLY :
703 case ICMP6_ECHO_REQUEST :
704 if (fin->fin_dlen >= 6)
705 fin->fin_data[1] = icmp6->icmp6_id;
706 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
707 break;
708 case ICMP6_DST_UNREACH :
709 case ICMP6_PACKET_TOO_BIG :
710 case ICMP6_TIME_EXCEEDED :
711 case ICMP6_PARAM_PROB :
712 if ((fin->fin_m != NULL) &&
713 (M_LEN(fin->fin_m) < fin->fin_plen)) {
714 if (fr_coalesce(fin) != 1)
715 return;
716 }
717 fin->fin_flx |= FI_ICMPERR;
718 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
719 break;
720 default :
721 break;
722 }
723 }
724
725 frpr_short6(fin, minicmpsz);
726 }
727
728
729 /* ------------------------------------------------------------------------ */
730 /* Function: frpr_udp6 */
731 /* Returns: void */
732 /* Parameters: fin(I) - pointer to packet information */
733 /* */
734 /* IPv6 Only */
735 /* Analyse the packet for IPv6/UDP properties. */
736 /* Is not expected to be called for fragmented packets. */
737 /* ------------------------------------------------------------------------ */
frpr_udp6(fin)738 static INLINE void frpr_udp6(fin)
739 fr_info_t *fin;
740 {
741
742 fr_checkv6sum(fin);
743
744 frpr_short6(fin, sizeof(struct udphdr));
745 if (frpr_pullup(fin, sizeof(struct udphdr)) == -1)
746 return;
747
748 frpr_udpcommon(fin);
749 }
750
751
752 /* ------------------------------------------------------------------------ */
753 /* Function: frpr_tcp6 */
754 /* Returns: void */
755 /* Parameters: fin(I) - pointer to packet information */
756 /* */
757 /* IPv6 Only */
758 /* Analyse the packet for IPv6/TCP properties. */
759 /* Is not expected to be called for fragmented packets. */
760 /* ------------------------------------------------------------------------ */
frpr_tcp6(fin)761 static INLINE void frpr_tcp6(fin)
762 fr_info_t *fin;
763 {
764
765 fr_checkv6sum(fin);
766
767 frpr_short6(fin, sizeof(struct tcphdr));
768 if (frpr_pullup(fin, sizeof(struct tcphdr)) == -1)
769 return;
770
771 frpr_tcpcommon(fin);
772 }
773
774
775 /* ------------------------------------------------------------------------ */
776 /* Function: frpr_esp6 */
777 /* Returns: void */
778 /* Parameters: fin(I) - pointer to packet information */
779 /* */
780 /* IPv6 Only */
781 /* Analyse the packet for ESP properties. */
782 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
783 /* even though the newer ESP packets must also have a sequence number that */
784 /* is 32bits as well, it is not possible(?) to determine the version from a */
785 /* simple packet header. */
786 /* ------------------------------------------------------------------------ */
frpr_esp6(fin)787 static INLINE void frpr_esp6(fin)
788 fr_info_t *fin;
789 {
790 int i;
791 frpr_short6(fin, sizeof(grehdr_t));
792
793 (void) frpr_pullup(fin, 8);
794
795 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
796 if (ip6exthdr[i].ol_val == IPPROTO_ESP) {
797 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
798 break;
799 }
800 }
801
802
803 /* ------------------------------------------------------------------------ */
804 /* Function: frpr_ah6 */
805 /* Returns: void */
806 /* Parameters: fin(I) - pointer to packet information */
807 /* */
808 /* IPv6 Only */
809 /* Analyse the packet for AH properties. */
810 /* The minimum length is taken to be the combination of all fields in the */
811 /* header being present and no authentication data (null algorithm used.) */
812 /* ------------------------------------------------------------------------ */
frpr_ah6(fin)813 static INLINE int frpr_ah6(fin)
814 fr_info_t *fin;
815 {
816 authhdr_t *ah;
817 int i, shift;
818
819 frpr_short6(fin, 12);
820
821 if (frpr_pullup(fin, sizeof(*ah)) == -1)
822 return IPPROTO_NONE;
823
824 for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
825 if (ip6exthdr[i].ol_val == IPPROTO_AH) {
826 fin->fin_optmsk |= ip6exthdr[i].ol_bit;
827 break;
828 }
829
830 ah = (authhdr_t *)fin->fin_dp;
831
832 shift = (ah->ah_plen + 2) * 4;
833 fin->fin_dlen -= shift;
834 fin->fin_dp = (char*)fin->fin_dp + shift;
835
836 return ah->ah_next;
837 }
838
839
840 /* ------------------------------------------------------------------------ */
841 /* Function: frpr_gre6 */
842 /* Returns: void */
843 /* Parameters: fin(I) - pointer to packet information */
844 /* */
845 /* Analyse the packet for GRE properties. */
846 /* ------------------------------------------------------------------------ */
frpr_gre6(fin)847 static INLINE void frpr_gre6(fin)
848 fr_info_t *fin;
849 {
850 grehdr_t *gre;
851
852 frpr_short6(fin, sizeof(grehdr_t));
853
854 if (frpr_pullup(fin, sizeof(grehdr_t)) == -1)
855 return;
856
857 gre = fin->fin_dp;
858 if (GRE_REV(gre->gr_flags) == 1)
859 fin->fin_data[0] = gre->gr_call;
860 }
861 #endif /* USE_INET6 */
862
863
864 /* ------------------------------------------------------------------------ */
865 /* Function: frpr_pullup */
866 /* Returns: int - 0 == pullup succeeded, -1 == failure */
867 /* Parameters: fin(I) - pointer to packet information */
868 /* plen(I) - length (excluding L3 header) to pullup */
869 /* */
870 /* Short inline function to cut down on code duplication to perform a call */
871 /* to fr_pullup to ensure there is the required amount of data, */
872 /* consecutively in the packet buffer. */
873 /* ------------------------------------------------------------------------ */
frpr_pullup(fin,plen)874 static INLINE int frpr_pullup(fin, plen)
875 fr_info_t *fin;
876 int plen;
877 {
878 #if defined(_KERNEL)
879 if (fin->fin_m != NULL) {
880 int ipoff;
881
882 ipoff = (char *)fin->fin_ip - MTOD(fin->fin_m, char *);
883
884 if (fin->fin_dp != NULL)
885 plen += (char *)fin->fin_dp -
886 ((char *)fin->fin_ip + fin->fin_hlen);
887 plen += fin->fin_hlen;
888 /*
889 * We don't do 'plen += ipoff;' here. The fr_pullup() will
890 * do it for us.
891 */
892 if (M_LEN(fin->fin_m) < plen + ipoff) {
893 if (fr_pullup(fin->fin_m, fin, plen) == NULL)
894 return -1;
895 }
896 }
897 #endif
898 return 0;
899 }
900
901
902 /* ------------------------------------------------------------------------ */
903 /* Function: frpr_short */
904 /* Returns: void */
905 /* Parameters: fin(I) - pointer to packet information */
906 /* xmin(I) - minimum header size */
907 /* */
908 /* Check if a packet is "short" as defined by xmin. The rule we are */
909 /* applying here is that the packet must not be fragmented within the layer */
910 /* 4 header. That is, it must not be a fragment that has its offset set to */
911 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */
912 /* entire layer 4 header must be present (min). */
913 /* ------------------------------------------------------------------------ */
frpr_short(fin,xmin)914 static INLINE void frpr_short(fin, xmin)
915 fr_info_t *fin;
916 int xmin;
917 {
918
919 if (fin->fin_off == 0) {
920 if (fin->fin_dlen < xmin)
921 fin->fin_flx |= FI_SHORT;
922 } else if (fin->fin_off < xmin) {
923 fin->fin_flx |= FI_SHORT;
924 }
925 }
926
927
928 /* ------------------------------------------------------------------------ */
929 /* Function: frpr_icmp */
930 /* Returns: void */
931 /* Parameters: fin(I) - pointer to packet information */
932 /* */
933 /* IPv4 Only */
934 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */
935 /* except extrememly bad packets, both type and code will be present. */
936 /* The expected minimum size of an ICMP packet is very much dependent on */
937 /* the type of it. */
938 /* */
939 /* XXX - other ICMP sanity checks? */
940 /* ------------------------------------------------------------------------ */
frpr_icmp(fin)941 static INLINE void frpr_icmp(fin)
942 fr_info_t *fin;
943 {
944 int minicmpsz = sizeof(struct icmp);
945 icmphdr_t *icmp;
946 ip_t *oip;
947 ipf_stack_t *ifs = fin->fin_ifs;
948
949 if (fin->fin_off != 0) {
950 frpr_short(fin, ICMPERR_ICMPHLEN);
951 return;
952 }
953
954 if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1)
955 return;
956
957 fr_checkv4sum(fin);
958
959 /*
960 * This is a right place to set icmp pointer, since the memory
961 * referenced by fin_dp could get reallocated. The code down below can
962 * rely on fact icmp variable always points to ICMP header.
963 */
964 icmp = fin->fin_dp;
965 fin->fin_data[0] = *(u_short *)icmp;
966 fin->fin_data[1] = icmp->icmp_id;
967
968 switch (icmp->icmp_type)
969 {
970 case ICMP_ECHOREPLY :
971 case ICMP_ECHO :
972 /* Router discovery messaes - RFC 1256 */
973 case ICMP_ROUTERADVERT :
974 case ICMP_ROUTERSOLICIT :
975 minicmpsz = ICMP_MINLEN;
976 break;
977 /*
978 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
979 * 3 * timestamp(3 * 4)
980 */
981 case ICMP_TSTAMP :
982 case ICMP_TSTAMPREPLY :
983 minicmpsz = 20;
984 break;
985 /*
986 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
987 * mask(4)
988 */
989 case ICMP_MASKREQ :
990 case ICMP_MASKREPLY :
991 minicmpsz = 12;
992 break;
993 /*
994 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
995 */
996 case ICMP_UNREACH :
997 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
998 if (icmp->icmp_nextmtu < ifs->ifs_fr_icmpminfragmtu)
999 fin->fin_flx |= FI_BAD;
1000 }
1001 /* FALLTHRU */
1002 case ICMP_SOURCEQUENCH :
1003 case ICMP_REDIRECT :
1004 case ICMP_TIMXCEED :
1005 case ICMP_PARAMPROB :
1006 fin->fin_flx |= FI_ICMPERR;
1007 if (fr_coalesce(fin) != 1)
1008 return;
1009 /*
1010 * ICMP error packets should not be generated for IP
1011 * packets that are a fragment that isn't the first
1012 * fragment.
1013 */
1014 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1015 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0)
1016 fin->fin_flx |= FI_BAD;
1017 break;
1018 default :
1019 break;
1020 }
1021
1022 frpr_short(fin, minicmpsz);
1023 }
1024
1025
1026 /* ------------------------------------------------------------------------ */
1027 /* Function: frpr_tcpcommon */
1028 /* Returns: void */
1029 /* Parameters: fin(I) - pointer to packet information */
1030 /* */
1031 /* TCP header sanity checking. Look for bad combinations of TCP flags, */
1032 /* and make some checks with how they interact with other fields. */
1033 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */
1034 /* valid and mark the packet as bad if not. */
1035 /* ------------------------------------------------------------------------ */
frpr_tcpcommon(fin)1036 static INLINE void frpr_tcpcommon(fin)
1037 fr_info_t *fin;
1038 {
1039 int flags, tlen;
1040 tcphdr_t *tcp;
1041
1042 fin->fin_flx |= FI_TCPUDP;
1043 if (fin->fin_off != 0)
1044 return;
1045
1046 if (frpr_pullup(fin, sizeof(*tcp)) == -1)
1047 return;
1048 tcp = fin->fin_dp;
1049
1050 if (fin->fin_dlen > 3) {
1051 fin->fin_sport = ntohs(tcp->th_sport);
1052 fin->fin_dport = ntohs(tcp->th_dport);
1053 }
1054
1055 if ((fin->fin_flx & FI_SHORT) != 0)
1056 return;
1057
1058 /*
1059 * Use of the TCP data offset *must* result in a value that is at
1060 * least the same size as the TCP header.
1061 */
1062 tlen = TCP_OFF(tcp) << 2;
1063 if (tlen < sizeof(tcphdr_t)) {
1064 fin->fin_flx |= FI_BAD;
1065 return;
1066 }
1067
1068 flags = tcp->th_flags;
1069 fin->fin_tcpf = tcp->th_flags;
1070
1071 /*
1072 * If the urgent flag is set, then the urgent pointer must
1073 * also be set and vice versa. Good TCP packets do not have
1074 * just one of these set.
1075 */
1076 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1077 fin->fin_flx |= FI_BAD;
1078 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1079 /* Ignore this case, it shows up in "real" traffic with */
1080 /* bogus values in the urgent pointer field. */
1081 flags = flags; /* LINT */
1082 } else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1083 ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1084 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1085 fin->fin_flx |= FI_BAD;
1086 } else if (!(flags & TH_ACK)) {
1087 /*
1088 * If the ack bit isn't set, then either the SYN or
1089 * RST bit must be set. If the SYN bit is set, then
1090 * we expect the ACK field to be 0. If the ACK is
1091 * not set and if URG, PSH or FIN are set, consdier
1092 * that to indicate a bad TCP packet.
1093 */
1094 if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
1095 /*
1096 * Cisco PIX sets the ACK field to a random value.
1097 * In light of this, do not set FI_BAD until a patch
1098 * is available from Cisco to ensure that
1099 * interoperability between existing systems is
1100 * achieved.
1101 */
1102 /*fin->fin_flx |= FI_BAD*/;
1103 flags = flags; /* LINT */
1104 } else if (!(flags & (TH_RST|TH_SYN))) {
1105 fin->fin_flx |= FI_BAD;
1106 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1107 fin->fin_flx |= FI_BAD;
1108 }
1109 }
1110
1111 /*
1112 * At this point, it's not exactly clear what is to be gained by
1113 * marking up which TCP options are and are not present. The one we
1114 * are most interested in is the TCP window scale. This is only in
1115 * a SYN packet [RFC1323] so we don't need this here...?
1116 * Now if we were to analyse the header for passive fingerprinting,
1117 * then that might add some weight to adding this...
1118 */
1119 if (tlen == sizeof(tcphdr_t))
1120 return;
1121
1122 if (frpr_pullup(fin, tlen) == -1)
1123 return;
1124
1125 #if 0
1126 ip = fin->fin_ip;
1127 s = (u_char *)(tcp + 1);
1128 off = IP_HL(ip) << 2;
1129 # ifdef _KERNEL
1130 if (fin->fin_mp != NULL) {
1131 mb_t *m = *fin->fin_mp;
1132
1133 if (off + tlen > M_LEN(m))
1134 return;
1135 }
1136 # endif
1137 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
1138 opt = *s;
1139 if (opt == '\0')
1140 break;
1141 else if (opt == TCPOPT_NOP)
1142 ol = 1;
1143 else {
1144 if (tlen < 2)
1145 break;
1146 ol = (int)*(s + 1);
1147 if (ol < 2 || ol > tlen)
1148 break;
1149 }
1150
1151 for (i = 9, mv = 4; mv >= 0; ) {
1152 op = ipopts + i;
1153 if (opt == (u_char)op->ol_val) {
1154 optmsk |= op->ol_bit;
1155 break;
1156 }
1157 }
1158 tlen -= ol;
1159 s += ol;
1160 }
1161 #endif /* 0 */
1162 }
1163
1164
1165
1166 /* ------------------------------------------------------------------------ */
1167 /* Function: frpr_udpcommon */
1168 /* Returns: void */
1169 /* Parameters: fin(I) - pointer to packet information */
1170 /* */
1171 /* Extract the UDP source and destination ports, if present. If compiled */
1172 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */
1173 /* ------------------------------------------------------------------------ */
frpr_udpcommon(fin)1174 static INLINE void frpr_udpcommon(fin)
1175 fr_info_t *fin;
1176 {
1177 udphdr_t *udp;
1178
1179 fin->fin_flx |= FI_TCPUDP;
1180
1181 if (!fin->fin_off && (fin->fin_dlen > 3)) {
1182 if (frpr_pullup(fin, sizeof(*udp)) == -1) {
1183 fin->fin_flx |= FI_SHORT;
1184 return;
1185 }
1186
1187 udp = fin->fin_dp;
1188
1189 fin->fin_sport = ntohs(udp->uh_sport);
1190 fin->fin_dport = ntohs(udp->uh_dport);
1191 }
1192 }
1193
1194
1195 /* ------------------------------------------------------------------------ */
1196 /* Function: frpr_tcp */
1197 /* Returns: void */
1198 /* Parameters: fin(I) - pointer to packet information */
1199 /* */
1200 /* IPv4 Only */
1201 /* Analyse the packet for IPv4/TCP properties. */
1202 /* ------------------------------------------------------------------------ */
frpr_tcp(fin)1203 static INLINE void frpr_tcp(fin)
1204 fr_info_t *fin;
1205 {
1206
1207 fr_checkv4sum(fin);
1208
1209 frpr_short(fin, sizeof(tcphdr_t));
1210
1211 frpr_tcpcommon(fin);
1212 }
1213
1214
1215 /* ------------------------------------------------------------------------ */
1216 /* Function: frpr_udp */
1217 /* Returns: void */
1218 /* Parameters: fin(I) - pointer to packet information */
1219 /* */
1220 /* IPv4 Only */
1221 /* Analyse the packet for IPv4/UDP properties. */
1222 /* ------------------------------------------------------------------------ */
frpr_udp(fin)1223 static INLINE void frpr_udp(fin)
1224 fr_info_t *fin;
1225 {
1226
1227 fr_checkv4sum(fin);
1228
1229 frpr_short(fin, sizeof(udphdr_t));
1230
1231 frpr_udpcommon(fin);
1232 }
1233
1234
1235 /* ------------------------------------------------------------------------ */
1236 /* Function: frpr_esp */
1237 /* Returns: void */
1238 /* Parameters: fin(I) - pointer to packet information */
1239 /* */
1240 /* Analyse the packet for ESP properties. */
1241 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
1242 /* even though the newer ESP packets must also have a sequence number that */
1243 /* is 32bits as well, it is not possible(?) to determine the version from a */
1244 /* simple packet header. */
1245 /* ------------------------------------------------------------------------ */
frpr_esp(fin)1246 static INLINE void frpr_esp(fin)
1247 fr_info_t *fin;
1248 {
1249 if ((fin->fin_off == 0) && (frpr_pullup(fin, 8) == -1))
1250 return;
1251
1252 frpr_short(fin, 8);
1253 }
1254
1255
1256 /* ------------------------------------------------------------------------ */
1257 /* Function: frpr_ah */
1258 /* Returns: void */
1259 /* Parameters: fin(I) - pointer to packet information */
1260 /* */
1261 /* Analyse the packet for AH properties. */
1262 /* The minimum length is taken to be the combination of all fields in the */
1263 /* header being present and no authentication data (null algorithm used.) */
1264 /* ------------------------------------------------------------------------ */
frpr_ah(fin)1265 static INLINE void frpr_ah(fin)
1266 fr_info_t *fin;
1267 {
1268 authhdr_t *ah;
1269 int len;
1270
1271 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(*ah)) == -1))
1272 return;
1273
1274 ah = (authhdr_t *)fin->fin_dp;
1275
1276 len = (ah->ah_plen + 2) << 2;
1277 frpr_short(fin, len);
1278 }
1279
1280
1281 /* ------------------------------------------------------------------------ */
1282 /* Function: frpr_gre */
1283 /* Returns: void */
1284 /* Parameters: fin(I) - pointer to packet information */
1285 /* */
1286 /* Analyse the packet for GRE properties. */
1287 /* ------------------------------------------------------------------------ */
frpr_gre(fin)1288 static INLINE void frpr_gre(fin)
1289 fr_info_t *fin;
1290 {
1291 grehdr_t *gre;
1292
1293 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(grehdr_t)) == -1))
1294 return;
1295
1296 frpr_short(fin, sizeof(grehdr_t));
1297
1298 if (fin->fin_off == 0) {
1299 gre = fin->fin_dp;
1300 if (GRE_REV(gre->gr_flags) == 1)
1301 fin->fin_data[0] = gre->gr_call;
1302 }
1303 }
1304
1305
1306 /* ------------------------------------------------------------------------ */
1307 /* Function: frpr_ipv4hdr */
1308 /* Returns: void */
1309 /* Parameters: fin(I) - pointer to packet information */
1310 /* */
1311 /* IPv4 Only */
1312 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */
1313 /* Check all options present and flag their presence if any exist. */
1314 /* ------------------------------------------------------------------------ */
frpr_ipv4hdr(fin)1315 static INLINE void frpr_ipv4hdr(fin)
1316 fr_info_t *fin;
1317 {
1318 u_short optmsk = 0, secmsk = 0, auth = 0;
1319 int hlen, ol, mv, p, i;
1320 const struct optlist *op;
1321 u_char *s, opt;
1322 u_short off;
1323 fr_ip_t *fi;
1324 ip_t *ip;
1325
1326 fi = &fin->fin_fi;
1327 hlen = fin->fin_hlen;
1328
1329 ip = fin->fin_ip;
1330 p = ip->ip_p;
1331 fi->fi_p = p;
1332 fi->fi_tos = ip->ip_tos;
1333 fin->fin_id = ip->ip_id;
1334 off = ip->ip_off;
1335
1336 /* Get both TTL and protocol */
1337 fi->fi_p = ip->ip_p;
1338 fi->fi_ttl = ip->ip_ttl;
1339 #if 0
1340 (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
1341 #endif
1342
1343 /* Zero out bits not used in IPv6 address */
1344 fi->fi_src.i6[1] = 0;
1345 fi->fi_src.i6[2] = 0;
1346 fi->fi_src.i6[3] = 0;
1347 fi->fi_dst.i6[1] = 0;
1348 fi->fi_dst.i6[2] = 0;
1349 fi->fi_dst.i6[3] = 0;
1350
1351 fi->fi_saddr = ip->ip_src.s_addr;
1352 fi->fi_daddr = ip->ip_dst.s_addr;
1353
1354 /*
1355 * set packet attribute flags based on the offset and
1356 * calculate the byte offset that it represents.
1357 */
1358 off &= IP_MF|IP_OFFMASK;
1359 if (off != 0) {
1360 int morefrag = off & IP_MF;
1361
1362 fi->fi_flx |= FI_FRAG;
1363 if (morefrag)
1364 fi->fi_flx |= FI_MOREFRAG;
1365 off &= IP_OFFMASK;
1366 if (off != 0) {
1367 fin->fin_flx |= FI_FRAGBODY;
1368 off <<= 3;
1369 if ((off + fin->fin_dlen > 65535) ||
1370 (fin->fin_dlen == 0) ||
1371 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1372 /*
1373 * The length of the packet, starting at its
1374 * offset cannot exceed 65535 (0xffff) as the
1375 * length of an IP packet is only 16 bits.
1376 *
1377 * Any fragment that isn't the last fragment
1378 * must have a length greater than 0 and it
1379 * must be an even multiple of 8.
1380 */
1381 fi->fi_flx |= FI_BAD;
1382 }
1383 }
1384 }
1385 fin->fin_off = off;
1386
1387 /*
1388 * Call per-protocol setup and checking
1389 */
1390 switch (p)
1391 {
1392 case IPPROTO_UDP :
1393 frpr_udp(fin);
1394 break;
1395 case IPPROTO_TCP :
1396 frpr_tcp(fin);
1397 break;
1398 case IPPROTO_ICMP :
1399 frpr_icmp(fin);
1400 break;
1401 case IPPROTO_AH :
1402 frpr_ah(fin);
1403 break;
1404 case IPPROTO_ESP :
1405 frpr_esp(fin);
1406 break;
1407 case IPPROTO_GRE :
1408 frpr_gre(fin);
1409 break;
1410 }
1411
1412 ip = fin->fin_ip;
1413 if (ip == NULL)
1414 return;
1415
1416 /*
1417 * If it is a standard IP header (no options), set the flag fields
1418 * which relate to options to 0.
1419 */
1420 if (hlen == sizeof(*ip)) {
1421 fi->fi_optmsk = 0;
1422 fi->fi_secmsk = 0;
1423 fi->fi_auth = 0;
1424 return;
1425 }
1426
1427 /*
1428 * So the IP header has some IP options attached. Walk the entire
1429 * list of options present with this packet and set flags to indicate
1430 * which ones are here and which ones are not. For the somewhat out
1431 * of date and obscure security classification options, set a flag to
1432 * represent which classification is present.
1433 */
1434 fi->fi_flx |= FI_OPTIONS;
1435
1436 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
1437 opt = *s;
1438 if (opt == '\0')
1439 break;
1440 else if (opt == IPOPT_NOP)
1441 ol = 1;
1442 else {
1443 if (hlen < 2)
1444 break;
1445 ol = (int)*(s + 1);
1446 if (ol < 2 || ol > hlen)
1447 break;
1448 }
1449 for (i = 9, mv = 4; mv >= 0; ) {
1450 op = ipopts + i;
1451 if ((opt == (u_char)op->ol_val) && (ol > 4)) {
1452 optmsk |= op->ol_bit;
1453 if (opt == IPOPT_SECURITY) {
1454 const struct optlist *sp;
1455 u_char sec;
1456 int j, m;
1457
1458 sec = *(s + 2); /* classification */
1459 for (j = 3, m = 2; m >= 0; ) {
1460 sp = secopt + j;
1461 if (sec == sp->ol_val) {
1462 secmsk |= sp->ol_bit;
1463 auth = *(s + 3);
1464 auth *= 256;
1465 auth += *(s + 4);
1466 break;
1467 }
1468 if (sec < sp->ol_val)
1469 j -= m;
1470 else
1471 j += m;
1472 m--;
1473 }
1474 }
1475 break;
1476 }
1477 if (opt < op->ol_val)
1478 i -= mv;
1479 else
1480 i += mv;
1481 mv--;
1482 }
1483 hlen -= ol;
1484 s += ol;
1485 }
1486
1487 /*
1488 *
1489 */
1490 if (auth && !(auth & 0x0100))
1491 auth &= 0xff00;
1492 fi->fi_optmsk = optmsk;
1493 fi->fi_secmsk = secmsk;
1494 fi->fi_auth = auth;
1495 }
1496
1497
1498 /* ------------------------------------------------------------------------ */
1499 /* Function: fr_makefrip */
1500 /* Returns: int - 1 == hdr checking error, 0 == OK */
1501 /* Parameters: hlen(I) - length of IP packet header */
1502 /* ip(I) - pointer to the IP header */
1503 /* fin(IO) - pointer to packet information */
1504 /* */
1505 /* Compact the IP header into a structure which contains just the info. */
1506 /* which is useful for comparing IP headers with and store this information */
1507 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */
1508 /* this function will be called with either an IPv4 or IPv6 packet. */
1509 /* ------------------------------------------------------------------------ */
fr_makefrip(hlen,ip,fin)1510 int fr_makefrip(hlen, ip, fin)
1511 int hlen;
1512 ip_t *ip;
1513 fr_info_t *fin;
1514 {
1515 int v;
1516
1517 fin->fin_depth = 0;
1518 fin->fin_hlen = (u_short)hlen;
1519 fin->fin_ip = ip;
1520 fin->fin_rule = 0xffffffff;
1521 fin->fin_group[0] = -1;
1522 fin->fin_group[1] = '\0';
1523 fin->fin_dlen = fin->fin_plen - hlen;
1524 fin->fin_dp = (char *)ip + hlen;
1525
1526 v = fin->fin_v;
1527 if (v == 4)
1528 frpr_ipv4hdr(fin);
1529 #ifdef USE_INET6
1530 else if (v == 6)
1531 frpr_ipv6hdr(fin);
1532 #endif
1533 if (fin->fin_ip == NULL)
1534 return -1;
1535 return 0;
1536 }
1537
1538
1539 /* ------------------------------------------------------------------------ */
1540 /* Function: fr_portcheck */
1541 /* Returns: int - 1 == port matched, 0 == port match failed */
1542 /* Parameters: frp(I) - pointer to port check `expression' */
1543 /* pop(I) - pointer to port number to evaluate */
1544 /* */
1545 /* Perform a comparison of a port number against some other(s), using a */
1546 /* structure with compare information stored in it. */
1547 /* ------------------------------------------------------------------------ */
fr_portcheck(frp,pop)1548 static INLINE int fr_portcheck(frp, pop)
1549 frpcmp_t *frp;
1550 u_short *pop;
1551 {
1552 u_short tup, po;
1553 int err = 1;
1554
1555 tup = *pop;
1556 po = frp->frp_port;
1557
1558 /*
1559 * Do opposite test to that required and continue if that succeeds.
1560 */
1561 switch (frp->frp_cmp)
1562 {
1563 case FR_EQUAL :
1564 if (tup != po) /* EQUAL */
1565 err = 0;
1566 break;
1567 case FR_NEQUAL :
1568 if (tup == po) /* NOTEQUAL */
1569 err = 0;
1570 break;
1571 case FR_LESST :
1572 if (tup >= po) /* LESSTHAN */
1573 err = 0;
1574 break;
1575 case FR_GREATERT :
1576 if (tup <= po) /* GREATERTHAN */
1577 err = 0;
1578 break;
1579 case FR_LESSTE :
1580 if (tup > po) /* LT or EQ */
1581 err = 0;
1582 break;
1583 case FR_GREATERTE :
1584 if (tup < po) /* GT or EQ */
1585 err = 0;
1586 break;
1587 case FR_OUTRANGE :
1588 if (tup >= po && tup <= frp->frp_top) /* Out of range */
1589 err = 0;
1590 break;
1591 case FR_INRANGE :
1592 if (tup <= po || tup >= frp->frp_top) /* In range */
1593 err = 0;
1594 break;
1595 case FR_INCRANGE :
1596 if (tup < po || tup > frp->frp_top) /* Inclusive range */
1597 err = 0;
1598 break;
1599 default :
1600 break;
1601 }
1602 return err;
1603 }
1604
1605
1606 /* ------------------------------------------------------------------------ */
1607 /* Function: fr_tcpudpchk */
1608 /* Returns: int - 1 == protocol matched, 0 == check failed */
1609 /* Parameters: fin(I) - pointer to packet information */
1610 /* ft(I) - pointer to structure with comparison data */
1611 /* */
1612 /* Compares the current pcket (assuming it is TCP/UDP) information with a */
1613 /* structure containing information that we want to match against. */
1614 /* ------------------------------------------------------------------------ */
fr_tcpudpchk(fin,ft)1615 int fr_tcpudpchk(fin, ft)
1616 fr_info_t *fin;
1617 frtuc_t *ft;
1618 {
1619 int err = 1;
1620
1621 /*
1622 * Both ports should *always* be in the first fragment.
1623 * So far, I cannot find any cases where they can not be.
1624 *
1625 * compare destination ports
1626 */
1627 if (ft->ftu_dcmp)
1628 err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport);
1629
1630 /*
1631 * compare source ports
1632 */
1633 if (err && ft->ftu_scmp)
1634 err = fr_portcheck(&ft->ftu_src, &fin->fin_sport);
1635
1636 /*
1637 * If we don't have all the TCP/UDP header, then how can we
1638 * expect to do any sort of match on it ? If we were looking for
1639 * TCP flags, then NO match. If not, then match (which should
1640 * satisfy the "short" class too).
1641 */
1642 if (err && (fin->fin_p == IPPROTO_TCP)) {
1643 if (fin->fin_flx & FI_SHORT)
1644 return !(ft->ftu_tcpf | ft->ftu_tcpfm);
1645 /*
1646 * Match the flags ? If not, abort this match.
1647 */
1648 if (ft->ftu_tcpfm &&
1649 ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
1650 FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
1651 ft->ftu_tcpfm, ft->ftu_tcpf));
1652 err = 0;
1653 }
1654 }
1655 return err;
1656 }
1657
1658
1659 /* ------------------------------------------------------------------------ */
1660 /* Function: fr_ipfcheck */
1661 /* Returns: int - 0 == match, 1 == no match */
1662 /* Parameters: fin(I) - pointer to packet information */
1663 /* fr(I) - pointer to filter rule */
1664 /* portcmp(I) - flag indicating whether to attempt matching on */
1665 /* TCP/UDP port data. */
1666 /* */
1667 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */
1668 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
1669 /* this function. */
1670 /* ------------------------------------------------------------------------ */
fr_ipfcheck(fin,fr,portcmp)1671 static INLINE int fr_ipfcheck(fin, fr, portcmp)
1672 fr_info_t *fin;
1673 frentry_t *fr;
1674 int portcmp;
1675 {
1676 u_32_t *ld, *lm, *lip;
1677 fripf_t *fri;
1678 fr_ip_t *fi;
1679 int i;
1680 ipf_stack_t *ifs = fin->fin_ifs;
1681
1682 fi = &fin->fin_fi;
1683 fri = fr->fr_ipf;
1684 lip = (u_32_t *)fi;
1685 lm = (u_32_t *)&fri->fri_mip;
1686 ld = (u_32_t *)&fri->fri_ip;
1687
1688 /*
1689 * first 32 bits to check coversion:
1690 * IP version, TOS, TTL, protocol
1691 */
1692 i = ((*lip & *lm) != *ld);
1693 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
1694 *lip, *lm, *ld));
1695 if (i)
1696 return 1;
1697
1698 /*
1699 * Next 32 bits is a constructed bitmask indicating which IP options
1700 * are present (if any) in this packet.
1701 */
1702 lip++, lm++, ld++;
1703 i |= ((*lip & *lm) != *ld);
1704 FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
1705 *lip, *lm, *ld));
1706 if (i)
1707 return 1;
1708
1709 lip++, lm++, ld++;
1710 /*
1711 * Unrolled loops (4 each, for 32 bits) for address checks.
1712 */
1713 /*
1714 * Check the source address.
1715 */
1716 #ifdef IPFILTER_LOOKUP
1717 if (fr->fr_satype == FRI_LOOKUP) {
1718 fin->fin_flx |= FI_DONTCACHE;
1719 i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip, fin, ifs);
1720 if (i == -1)
1721 return 1;
1722 lip += 3;
1723 lm += 3;
1724 ld += 3;
1725 } else {
1726 #endif
1727 i = ((*lip & *lm) != *ld);
1728 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
1729 *lip, *lm, *ld));
1730 if (fi->fi_v == 6) {
1731 lip++, lm++, ld++;
1732 i |= ((*lip & *lm) != *ld);
1733 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
1734 *lip, *lm, *ld));
1735 lip++, lm++, ld++;
1736 i |= ((*lip & *lm) != *ld);
1737 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
1738 *lip, *lm, *ld));
1739 lip++, lm++, ld++;
1740 i |= ((*lip & *lm) != *ld);
1741 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
1742 *lip, *lm, *ld));
1743 } else {
1744 lip += 3;
1745 lm += 3;
1746 ld += 3;
1747 }
1748 #ifdef IPFILTER_LOOKUP
1749 }
1750 #endif
1751 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
1752 if (i)
1753 return 1;
1754
1755 /*
1756 * Check the destination address.
1757 */
1758 lip++, lm++, ld++;
1759 #ifdef IPFILTER_LOOKUP
1760 if (fr->fr_datype == FRI_LOOKUP) {
1761 fin->fin_flx |= FI_DONTCACHE;
1762 i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip, fin, ifs);
1763 if (i == -1)
1764 return 1;
1765 lip += 3;
1766 lm += 3;
1767 ld += 3;
1768 } else {
1769 #endif
1770 i = ((*lip & *lm) != *ld);
1771 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
1772 *lip, *lm, *ld));
1773 if (fi->fi_v == 6) {
1774 lip++, lm++, ld++;
1775 i |= ((*lip & *lm) != *ld);
1776 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
1777 *lip, *lm, *ld));
1778 lip++, lm++, ld++;
1779 i |= ((*lip & *lm) != *ld);
1780 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
1781 *lip, *lm, *ld));
1782 lip++, lm++, ld++;
1783 i |= ((*lip & *lm) != *ld);
1784 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
1785 *lip, *lm, *ld));
1786 } else {
1787 lip += 3;
1788 lm += 3;
1789 ld += 3;
1790 }
1791 #ifdef IPFILTER_LOOKUP
1792 }
1793 #endif
1794 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
1795 if (i)
1796 return 1;
1797 /*
1798 * IP addresses matched. The next 32bits contains:
1799 * mast of old IP header security & authentication bits.
1800 */
1801 lip++, lm++, ld++;
1802 i |= ((*lip & *lm) != *ld);
1803 FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
1804 *lip, *lm, *ld));
1805
1806 /*
1807 * Next we have 32 bits of packet flags.
1808 */
1809 lip++, lm++, ld++;
1810 i |= ((*lip & *lm) != *ld);
1811 FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
1812 *lip, *lm, *ld));
1813
1814 if (i == 0) {
1815 /*
1816 * If a fragment, then only the first has what we're
1817 * looking for here...
1818 */
1819 if (portcmp) {
1820 if (!fr_tcpudpchk(fin, &fr->fr_tuc))
1821 i = 1;
1822 } else {
1823 if (fr->fr_dcmp || fr->fr_scmp ||
1824 fr->fr_tcpf || fr->fr_tcpfm)
1825 i = 1;
1826 if (fr->fr_icmpm || fr->fr_icmp) {
1827 if (((fi->fi_p != IPPROTO_ICMP) &&
1828 (fi->fi_p != IPPROTO_ICMPV6)) ||
1829 fin->fin_off || (fin->fin_dlen < 2))
1830 i = 1;
1831 else if ((fin->fin_data[0] & fr->fr_icmpm) !=
1832 fr->fr_icmp) {
1833 FR_DEBUG(("i. %#x & %#x != %#x\n",
1834 fin->fin_data[0],
1835 fr->fr_icmpm, fr->fr_icmp));
1836 i = 1;
1837 }
1838 }
1839 }
1840 }
1841 return i;
1842 }
1843
1844
1845 /* ------------------------------------------------------------------------ */
1846 /* Function: fr_scanlist */
1847 /* Returns: int - result flags of scanning filter list */
1848 /* Parameters: fin(I) - pointer to packet information */
1849 /* pass(I) - default result to return for filtering */
1850 /* */
1851 /* Check the input/output list of rules for a match to the current packet. */
1852 /* If a match is found, the value of fr_flags from the rule becomes the */
1853 /* return value and fin->fin_fr points to the matched rule. */
1854 /* */
1855 /* This function may be called recusively upto 16 times (limit inbuilt.) */
1856 /* When unwinding, it should finish up with fin_depth as 0. */
1857 /* */
1858 /* Could be per interface, but this gets real nasty when you don't have, */
1859 /* or can't easily change, the kernel source code to . */
1860 /* ------------------------------------------------------------------------ */
fr_scanlist(fin,pass)1861 int fr_scanlist(fin, pass)
1862 fr_info_t *fin;
1863 u_32_t pass;
1864 {
1865 int rulen, portcmp, off, logged, skip;
1866 struct frentry *fr, *fnext;
1867 u_32_t passt, passo;
1868 ipf_stack_t *ifs = fin->fin_ifs;
1869
1870 /*
1871 * Do not allow nesting deeper than 16 levels.
1872 */
1873 if (fin->fin_depth >= 16)
1874 return pass;
1875
1876 fr = fin->fin_fr;
1877
1878 /*
1879 * If there are no rules in this list, return now.
1880 */
1881 if (fr == NULL)
1882 return pass;
1883
1884 skip = 0;
1885 logged = 0;
1886 portcmp = 0;
1887 fin->fin_depth++;
1888 fin->fin_fr = NULL;
1889 off = fin->fin_off;
1890
1891 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
1892 portcmp = 1;
1893
1894 for (rulen = 0; fr; fr = fnext, rulen++) {
1895 fnext = fr->fr_next;
1896 if (skip != 0) {
1897 FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
1898 skip--;
1899 continue;
1900 }
1901
1902 /*
1903 * In all checks below, a null (zero) value in the
1904 * filter struture is taken to mean a wildcard.
1905 *
1906 * check that we are working for the right interface
1907 */
1908 #ifdef _KERNEL
1909 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
1910 continue;
1911 #else
1912 if (opts & (OPT_VERBOSE|OPT_DEBUG))
1913 printf("\n");
1914 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
1915 FR_ISPASS(pass) ? 'p' :
1916 FR_ISACCOUNT(pass) ? 'A' :
1917 FR_ISAUTH(pass) ? 'a' :
1918 (pass & FR_NOMATCH) ? 'n' :'b'));
1919 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
1920 continue;
1921 FR_VERBOSE((":i"));
1922 #endif
1923
1924 switch (fr->fr_type)
1925 {
1926 case FR_T_IPF :
1927 case FR_T_IPF|FR_T_BUILTIN :
1928 if (fr_ipfcheck(fin, fr, portcmp))
1929 continue;
1930 break;
1931 #if defined(IPFILTER_BPF)
1932 case FR_T_BPFOPC :
1933 case FR_T_BPFOPC|FR_T_BUILTIN :
1934 {
1935 u_char *mc;
1936
1937 if (*fin->fin_mp == NULL)
1938 continue;
1939 if (fin->fin_v != fr->fr_v)
1940 continue;
1941 mc = (u_char *)fin->fin_m;
1942 if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0))
1943 continue;
1944 break;
1945 }
1946 #endif
1947 case FR_T_CALLFUNC|FR_T_BUILTIN :
1948 {
1949 frentry_t *f;
1950
1951 f = (*fr->fr_func)(fin, &pass);
1952 if (f != NULL)
1953 fr = f;
1954 else
1955 continue;
1956 break;
1957 }
1958 default :
1959 break;
1960 }
1961
1962 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
1963 if (fin->fin_nattag == NULL)
1964 continue;
1965 if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
1966 continue;
1967 }
1968 FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen));
1969
1970 passt = fr->fr_flags;
1971
1972 /*
1973 * Allowing a rule with the "keep state" flag set to match
1974 * packets that have been tagged "out of window" by the TCP
1975 * state tracking is foolish as the attempt to add a new
1976 * state entry to the table will fail.
1977 */
1978 if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW))
1979 continue;
1980
1981 /*
1982 * If the rule is a "call now" rule, then call the function
1983 * in the rule, if it exists and use the results from that.
1984 * If the function pointer is bad, just make like we ignore
1985 * it, except for increasing the hit counter.
1986 */
1987 IPF_BUMP(fr->fr_hits);
1988 fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
1989 if ((passt & FR_CALLNOW) != 0) {
1990 if ((fr->fr_func != NULL) &&
1991 (fr->fr_func != (ipfunc_t)-1)) {
1992 frentry_t *frs;
1993
1994 frs = fin->fin_fr;
1995 fin->fin_fr = fr;
1996 fr = (*fr->fr_func)(fin, &passt);
1997 if (fr == NULL) {
1998 fin->fin_fr = frs;
1999 continue;
2000 }
2001 passt = fr->fr_flags;
2002 fin->fin_fr = fr;
2003 }
2004 } else {
2005 fin->fin_fr = fr;
2006 }
2007
2008 #ifdef IPFILTER_LOG
2009 /*
2010 * Just log this packet...
2011 */
2012 if ((passt & FR_LOGMASK) == FR_LOG) {
2013 if (ipflog(fin, passt) == -1) {
2014 if (passt & FR_LOGORBLOCK) {
2015 passt &= ~FR_CMDMASK;
2016 passt |= FR_BLOCK|FR_QUICK;
2017 }
2018 IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_skip);
2019 }
2020 IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_pkl);
2021 logged = 1;
2022 }
2023 #endif /* IPFILTER_LOG */
2024 passo = pass;
2025 if (FR_ISSKIP(passt))
2026 skip = fr->fr_arg;
2027 else if ((passt & FR_LOGMASK) != FR_LOG)
2028 pass = passt;
2029 if (passt & (FR_RETICMP|FR_FAKEICMP))
2030 fin->fin_icode = fr->fr_icode;
2031 FR_DEBUG(("pass %#x\n", pass));
2032 fin->fin_rule = rulen;
2033 (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN);
2034 if (fr->fr_grp != NULL) {
2035 fin->fin_fr = *fr->fr_grp;
2036 pass = fr_scanlist(fin, pass);
2037 if (fin->fin_fr == NULL) {
2038 fin->fin_rule = rulen;
2039 (void) strncpy(fin->fin_group, fr->fr_group,
2040 FR_GROUPLEN);
2041 fin->fin_fr = fr;
2042 }
2043 if (fin->fin_flx & FI_DONTCACHE)
2044 logged = 1;
2045 }
2046
2047 if (pass & FR_QUICK) {
2048 /*
2049 * Finally, if we've asked to track state for this
2050 * packet, set it up. Add state for "quick" rules
2051 * here so that if the action fails we can consider
2052 * the rule to "not match" and keep on processing
2053 * filter rules.
2054 */
2055 if ((pass & FR_KEEPSTATE) &&
2056 !(fin->fin_flx & FI_STATE)) {
2057 int out = fin->fin_out;
2058
2059 if (fr_addstate(fin, NULL, 0) != NULL) {
2060 IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
2061 } else {
2062 IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2063 pass = passo;
2064 continue;
2065 }
2066 }
2067 break;
2068 }
2069 }
2070 if (logged)
2071 fin->fin_flx |= FI_DONTCACHE;
2072 fin->fin_depth--;
2073 return pass;
2074 }
2075
2076
2077 /* ------------------------------------------------------------------------ */
2078 /* Function: fr_acctpkt */
2079 /* Returns: frentry_t* - always returns NULL */
2080 /* Parameters: fin(I) - pointer to packet information */
2081 /* passp(IO) - pointer to current/new filter decision (unused) */
2082 /* */
2083 /* Checks a packet against accounting rules, if there are any for the given */
2084 /* IP protocol version. */
2085 /* */
2086 /* N.B.: this function returns NULL to match the prototype used by other */
2087 /* functions called from the IPFilter "mainline" in fr_check(). */
2088 /* ------------------------------------------------------------------------ */
fr_acctpkt(fin,passp)2089 frentry_t *fr_acctpkt(fin, passp)
2090 fr_info_t *fin;
2091 u_32_t *passp;
2092 {
2093 char group[FR_GROUPLEN];
2094 frentry_t *fr, *frsave;
2095 u_32_t pass, rulen;
2096 ipf_stack_t *ifs = fin->fin_ifs;
2097
2098 passp = passp;
2099 #ifdef USE_INET6
2100 if (fin->fin_v == 6)
2101 fr = ifs->ifs_ipacct6[fin->fin_out][ifs->ifs_fr_active];
2102 else
2103 #endif
2104 fr = ifs->ifs_ipacct[fin->fin_out][ifs->ifs_fr_active];
2105
2106 if (fr != NULL) {
2107 frsave = fin->fin_fr;
2108 bcopy(fin->fin_group, group, FR_GROUPLEN);
2109 rulen = fin->fin_rule;
2110 fin->fin_fr = fr;
2111 pass = fr_scanlist(fin, FR_NOMATCH);
2112 if (FR_ISACCOUNT(pass)) {
2113 IPF_BUMP(ifs->ifs_frstats[0].fr_acct);
2114 }
2115 fin->fin_fr = frsave;
2116 bcopy(group, fin->fin_group, FR_GROUPLEN);
2117 fin->fin_rule = rulen;
2118 }
2119 return NULL;
2120 }
2121
2122
2123 /* ------------------------------------------------------------------------ */
2124 /* Function: fr_firewall */
2125 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */
2126 /* were found, returns NULL. */
2127 /* Parameters: fin(I) - pointer to packet information */
2128 /* passp(IO) - pointer to current/new filter decision (unused) */
2129 /* */
2130 /* Applies an appropriate set of firewall rules to the packet, to see if */
2131 /* there are any matches. The first check is to see if a match can be seen */
2132 /* in the cache. If not, then search an appropriate list of rules. Once a */
2133 /* matching rule is found, take any appropriate actions as defined by the */
2134 /* rule - except logging. */
2135 /* ------------------------------------------------------------------------ */
fr_firewall(fin,passp)2136 static frentry_t *fr_firewall(fin, passp)
2137 fr_info_t *fin;
2138 u_32_t *passp;
2139 {
2140 frentry_t *fr;
2141 fr_info_t *fc;
2142 u_32_t pass;
2143 int out;
2144 ipf_stack_t *ifs = fin->fin_ifs;
2145
2146 out = fin->fin_out;
2147 pass = *passp;
2148
2149 #ifdef USE_INET6
2150 if (fin->fin_v == 6)
2151 fin->fin_fr = ifs->ifs_ipfilter6[out][ifs->ifs_fr_active];
2152 else
2153 #endif
2154 fin->fin_fr = ifs->ifs_ipfilter[out][ifs->ifs_fr_active];
2155
2156 /*
2157 * If there are no rules loaded skip all checks and return.
2158 */
2159 if (fin->fin_fr == NULL) {
2160
2161 if ((pass & FR_NOMATCH)) {
2162 IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
2163 }
2164
2165 return (NULL);
2166 }
2167
2168 fc = &ifs->ifs_frcache[out][CACHE_HASH(fin)];
2169 READ_ENTER(&ifs->ifs_ipf_frcache);
2170 if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
2171 /*
2172 * copy cached data so we can unlock the mutexes earlier.
2173 */
2174 bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
2175 RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2176 IPF_BUMP(ifs->ifs_frstats[out].fr_chit);
2177
2178 if ((fr = fin->fin_fr) != NULL) {
2179 IPF_BUMP(fr->fr_hits);
2180 fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
2181 pass = fr->fr_flags;
2182 }
2183 } else {
2184 RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2185
2186 pass = fr_scanlist(fin, ifs->ifs_fr_pass);
2187
2188 if (((pass & FR_KEEPSTATE) == 0) &&
2189 ((fin->fin_flx & FI_DONTCACHE) == 0)) {
2190 WRITE_ENTER(&ifs->ifs_ipf_frcache);
2191 bcopy((char *)fin, (char *)fc, FI_COPYSIZE);
2192 RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2193 }
2194
2195 fr = fin->fin_fr;
2196 }
2197
2198 if ((pass & FR_NOMATCH)) {
2199 IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
2200 }
2201
2202 /*
2203 * Apply packets per second rate-limiting to a rule as required.
2204 */
2205 if ((fr != NULL) && (fr->fr_pps != 0) &&
2206 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
2207 pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST);
2208 pass |= FR_BLOCK;
2209 IPF_BUMP(ifs->ifs_frstats[out].fr_ppshit);
2210 }
2211
2212 /*
2213 * If we fail to add a packet to the authorization queue, then we
2214 * drop the packet later. However, if it was added then pretend
2215 * we've dropped it already.
2216 */
2217 if (FR_ISAUTH(pass)) {
2218 if (fr_newauth(fin->fin_m, fin) != 0) {
2219 #ifdef _KERNEL
2220 fin->fin_m = *fin->fin_mp = NULL;
2221 #else
2222 ;
2223 #endif
2224 fin->fin_error = 0;
2225 } else
2226 fin->fin_error = ENOSPC;
2227 }
2228
2229 if ((fr != NULL) && (fr->fr_func != NULL) &&
2230 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
2231 (void) (*fr->fr_func)(fin, &pass);
2232
2233 /*
2234 * If a rule is a pre-auth rule, check again in the list of rules
2235 * loaded for authenticated use. It does not particulary matter
2236 * if this search fails because a "preauth" result, from a rule,
2237 * is treated as "not a pass", hence the packet is blocked.
2238 */
2239 if (FR_ISPREAUTH(pass)) {
2240 if ((fin->fin_fr = ifs->ifs_ipauth) != NULL)
2241 pass = fr_scanlist(fin, ifs->ifs_fr_pass);
2242 }
2243
2244 /*
2245 * If the rule has "keep frag" and the packet is actually a fragment,
2246 * then create a fragment state entry.
2247 */
2248 if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
2249 if (fin->fin_flx & FI_FRAG) {
2250 if (fr_newfrag(fin, pass) == -1) {
2251 IPF_BUMP(ifs->ifs_frstats[out].fr_bnfr);
2252 } else {
2253 IPF_BUMP(ifs->ifs_frstats[out].fr_nfr);
2254 }
2255 } else {
2256 IPF_BUMP(ifs->ifs_frstats[out].fr_cfr);
2257 }
2258 }
2259
2260 /*
2261 * Finally, if we've asked to track state for this packet, set it up.
2262 */
2263 if ((pass & FR_KEEPSTATE) && !(fin->fin_flx & FI_STATE)) {
2264 if (fr_addstate(fin, NULL, 0) != NULL) {
2265 IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
2266 } else {
2267 IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2268 if (FR_ISPASS(pass)) {
2269 pass &= ~FR_CMDMASK;
2270 pass |= FR_BLOCK;
2271 }
2272 }
2273 }
2274
2275 fr = fin->fin_fr;
2276
2277 if (passp != NULL)
2278 *passp = pass;
2279
2280 return fr;
2281 }
2282
2283 /* ------------------------------------------------------------------------ */
2284 /* Function: fr_check */
2285 /* Returns: int - 0 == packet allowed through, */
2286 /* User space: */
2287 /* -1 == packet blocked */
2288 /* 1 == packet not matched */
2289 /* -2 == requires authentication */
2290 /* Kernel: */
2291 /* > 0 == filter error # for packet */
2292 /* Parameters: ip(I) - pointer to start of IPv4/6 packet */
2293 /* hlen(I) - length of header */
2294 /* ifp(I) - pointer to interface this packet is on */
2295 /* out(I) - 0 == packet going in, 1 == packet going out */
2296 /* mp(IO) - pointer to caller's buffer pointer that holds this */
2297 /* IP packet. */
2298 /* Solaris & HP-UX ONLY : */
2299 /* qpi(I) - pointer to STREAMS queue information for this */
2300 /* interface & direction. */
2301 /* */
2302 /* fr_check() is the master function for all IPFilter packet processing. */
2303 /* It orchestrates: Network Address Translation (NAT), checking for packet */
2304 /* authorisation (or pre-authorisation), presence of related state info., */
2305 /* generating log entries, IP packet accounting, routing of packets as */
2306 /* directed by firewall rules and of course whether or not to allow the */
2307 /* packet to be further processed by the kernel. */
2308 /* */
2309 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */
2310 /* freed. Packets passed may be returned with the pointer pointed to by */
2311 /* by "mp" changed to a new buffer. */
2312 /* ------------------------------------------------------------------------ */
fr_check(ip,hlen,ifp,out,qif,mp,ifs)2313 int fr_check(ip, hlen, ifp, out
2314 #if defined(_KERNEL) && defined(MENTAT)
2315 , qif, mp, ifs)
2316 void *qif;
2317 #else
2318 , mp, ifs)
2319 #endif
2320 mb_t **mp;
2321 ip_t *ip;
2322 int hlen;
2323 void *ifp;
2324 int out;
2325 ipf_stack_t *ifs;
2326 {
2327 /*
2328 * The above really sucks, but short of writing a diff
2329 */
2330 fr_info_t frinfo;
2331 fr_info_t *fin = &frinfo;
2332 u_32_t pass;
2333 frentry_t *fr = NULL;
2334 int v = IP_V(ip);
2335 mb_t *mc = NULL;
2336 mb_t *m;
2337 #ifdef USE_INET6
2338 ip6_t *ip6;
2339 #endif
2340 #ifdef _KERNEL
2341 # ifdef MENTAT
2342 qpktinfo_t *qpi = qif;
2343 #endif
2344 #endif
2345
2346 SPL_INT(s);
2347 pass = ifs->ifs_fr_pass;
2348
2349 /*
2350 * The first part of fr_check() deals with making sure that what goes
2351 * into the filtering engine makes some sense. Information about the
2352 * the packet is distilled, collected into a fr_info_t structure and
2353 * the an attempt to ensure the buffer the packet is in is big enough
2354 * to hold all the required packet headers.
2355 */
2356 #ifdef _KERNEL
2357 # ifdef MENTAT
2358 if (!OK_32PTR(ip))
2359 return 2;
2360 # endif
2361
2362
2363 if (ifs->ifs_fr_running <= 0) {
2364 return 0;
2365 }
2366
2367 bzero((char *)fin, sizeof(*fin));
2368
2369 # ifdef MENTAT
2370 fin->fin_flx = qpi->qpi_flags & (FI_NOCKSUM|FI_MBCAST|FI_MULTICAST|
2371 FI_BROADCAST);
2372 m = qpi->qpi_m;
2373 fin->fin_qfm = m;
2374 fin->fin_qpi = qpi;
2375 # else /* MENTAT */
2376
2377 m = *mp;
2378
2379 # if defined(M_MCAST)
2380 if ((m->m_flags & M_MCAST) != 0)
2381 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2382 # endif
2383 # if defined(M_MLOOP)
2384 if ((m->m_flags & M_MLOOP) != 0)
2385 fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2386 # endif
2387 # if defined(M_BCAST)
2388 if ((m->m_flags & M_BCAST) != 0)
2389 fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2390 # endif
2391 # ifdef M_CANFASTFWD
2392 /*
2393 * XXX For now, IP Filter and fast-forwarding of cached flows
2394 * XXX are mutually exclusive. Eventually, IP Filter should
2395 * XXX get a "can-fast-forward" filter rule.
2396 */
2397 m->m_flags &= ~M_CANFASTFWD;
2398 # endif /* M_CANFASTFWD */
2399 # ifdef CSUM_DELAY_DATA
2400 /*
2401 * disable delayed checksums.
2402 */
2403 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2404 in_delayed_cksum(m);
2405 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2406 }
2407 # endif /* CSUM_DELAY_DATA */
2408 # endif /* MENTAT */
2409 #else
2410
2411 bzero((char *)fin, sizeof(*fin));
2412 m = *mp;
2413 #endif /* _KERNEL */
2414
2415 fin->fin_v = v;
2416 fin->fin_m = m;
2417 fin->fin_ip = ip;
2418 fin->fin_mp = mp;
2419 fin->fin_out = out;
2420 fin->fin_ifp = ifp;
2421 fin->fin_error = ENETUNREACH;
2422 fin->fin_hlen = (u_short)hlen;
2423 fin->fin_dp = (char *)ip + hlen;
2424 fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2425 fin->fin_ifs = ifs;
2426
2427 SPL_NET(s);
2428
2429 #ifdef USE_INET6
2430 if (v == 6) {
2431 IPF_BUMP(ifs->ifs_frstats[out].fr_ipv6);
2432 /*
2433 * Jumbo grams are quite likely too big for internal buffer
2434 * structures to handle comfortably, for now, so just drop
2435 * them.
2436 */
2437 ip6 = (ip6_t *)ip;
2438 fin->fin_plen = ntohs(ip6->ip6_plen);
2439 if (fin->fin_plen == 0) {
2440 READ_ENTER(&ifs->ifs_ipf_mutex);
2441 pass = FR_BLOCK|FR_NOMATCH;
2442 goto filtered;
2443 }
2444 fin->fin_plen += sizeof(ip6_t);
2445 } else
2446 #endif
2447 {
2448 #if (OpenBSD >= 200311) && defined(_KERNEL)
2449 ip->ip_len = ntohs(ip->ip_len);
2450 ip->ip_off = ntohs(ip->ip_off);
2451 #endif
2452 fin->fin_plen = ip->ip_len;
2453 }
2454
2455 if (fr_makefrip(hlen, ip, fin) == -1) {
2456 READ_ENTER(&ifs->ifs_ipf_mutex);
2457 pass = FR_BLOCK;
2458 goto filtered;
2459 }
2460
2461 /*
2462 * For at least IPv6 packets, if a m_pullup() fails then this pointer
2463 * becomes NULL and so we have no packet to free.
2464 */
2465 if (*fin->fin_mp == NULL)
2466 goto finished;
2467
2468 if (!out) {
2469 if (v == 4) {
2470 #ifdef _KERNEL
2471 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2472 IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
2473 fin->fin_flx |= FI_BADSRC;
2474 }
2475 #endif
2476 if (fin->fin_ip->ip_ttl < ifs->ifs_fr_minttl) {
2477 IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
2478 fin->fin_flx |= FI_LOWTTL;
2479 }
2480 }
2481 #ifdef USE_INET6
2482 else if (v == 6) {
2483 ip6 = (ip6_t *)ip;
2484 #ifdef _KERNEL
2485 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2486 IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
2487 fin->fin_flx |= FI_BADSRC;
2488 }
2489 #endif
2490 if (ip6->ip6_hlim < ifs->ifs_fr_minttl) {
2491 IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
2492 fin->fin_flx |= FI_LOWTTL;
2493 }
2494 }
2495 #endif
2496 }
2497
2498 if (fin->fin_flx & FI_SHORT) {
2499 IPF_BUMP(ifs->ifs_frstats[out].fr_short);
2500 }
2501
2502 READ_ENTER(&ifs->ifs_ipf_mutex);
2503
2504 /*
2505 * Check auth now. This, combined with the check below to see if apass
2506 * is 0 is to ensure that we don't count the packet twice, which can
2507 * otherwise occur when we reprocess it. As it is, we only count it
2508 * after it has no auth. table matchup. This also stops NAT from
2509 * occuring until after the packet has been auth'd.
2510 */
2511 fr = fr_checkauth(fin, &pass);
2512 if (!out) {
2513 switch (fin->fin_v)
2514 {
2515 case 4 :
2516 if (fr_checknatin(fin, &pass) == -1) {
2517 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2518 goto finished;
2519 }
2520 break;
2521 #ifdef USE_INET6
2522 case 6 :
2523 if (fr_checknat6in(fin, &pass) == -1) {
2524 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2525 goto finished;
2526 }
2527 break;
2528 #endif
2529 default :
2530 break;
2531 }
2532 }
2533 if (!out)
2534 (void) fr_acctpkt(fin, NULL);
2535
2536 if (fr == NULL)
2537 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG)
2538 fr = fr_knownfrag(fin, &pass);
2539 if (fr == NULL)
2540 fr = fr_checkstate(fin, &pass);
2541
2542 if ((pass & FR_NOMATCH) || (fr == NULL))
2543 fr = fr_firewall(fin, &pass);
2544
2545 fin->fin_fr = fr;
2546
2547 /*
2548 * Only count/translate packets which will be passed on, out the
2549 * interface.
2550 */
2551 if (out && FR_ISPASS(pass)) {
2552 (void) fr_acctpkt(fin, NULL);
2553
2554 switch (fin->fin_v)
2555 {
2556 case 4 :
2557 if (fr_checknatout(fin, &pass) == -1) {
2558 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2559 goto finished;
2560 }
2561 break;
2562 #ifdef USE_INET6
2563 case 6 :
2564 if (fr_checknat6out(fin, &pass) == -1) {
2565 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2566 goto finished;
2567 }
2568 break;
2569 #endif
2570 default :
2571 break;
2572 }
2573
2574 if ((ifs->ifs_fr_update_ipid != 0) && (v == 4)) {
2575 if (fr_updateipid(fin) == -1) {
2576 IPF_BUMP(ifs->ifs_frstats[1].fr_ipud);
2577 pass &= ~FR_CMDMASK;
2578 pass |= FR_BLOCK;
2579 } else {
2580 IPF_BUMP(ifs->ifs_frstats[0].fr_ipud);
2581 }
2582 }
2583 }
2584
2585 #ifdef IPFILTER_LOG
2586 if ((ifs->ifs_fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
2587 (void) fr_dolog(fin, &pass);
2588 }
2589 #endif
2590
2591 /*
2592 * The FI_STATE flag is cleared here so that calling fr_checkstate
2593 * will work when called from inside of fr_fastroute. Although
2594 * there is a similar flag, FI_NATED, for NAT, it does have the same
2595 * impact on code execution.
2596 */
2597 fin->fin_flx &= ~FI_STATE;
2598
2599 /*
2600 * Only allow FR_DUP to work if a rule matched - it makes no sense to
2601 * set FR_DUP as a "default" as there are no instructions about where
2602 * to send the packet. Use fin_m here because it may have changed
2603 * (without an update of 'm') in prior processing.
2604 */
2605 if ((fr != NULL) && (pass & FR_DUP)) {
2606 mc = M_DUPLICATE(fin->fin_m);
2607 #ifdef _KERNEL
2608 mc->b_rptr += fin->fin_ipoff;
2609 #endif
2610 }
2611
2612 /*
2613 * We don't want to send RST for packets, which are going to be
2614 * dropped, just because they don't fit into TCP window. Those packets
2615 * will be dropped silently. In other words, we want to drop packet,
2616 * while keeping session alive.
2617 */
2618 if ((pass & (FR_RETRST|FR_RETICMP)) && ((fin->fin_flx & FI_OOW) == 0)) {
2619 /*
2620 * Should we return an ICMP packet to indicate error
2621 * status passing through the packet filter ?
2622 * WARNING: ICMP error packets AND TCP RST packets should
2623 * ONLY be sent in repsonse to incoming packets. Sending them
2624 * in response to outbound packets can result in a panic on
2625 * some operating systems.
2626 */
2627 if (!out) {
2628 if (pass & FR_RETICMP) {
2629 int dst;
2630
2631 if ((pass & FR_RETMASK) == FR_FAKEICMP)
2632 dst = 1;
2633 else
2634 dst = 0;
2635 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2636 /*
2637 * Assume it's possible to enter insane rule:
2638 * pass return-icmp in proto udp ...
2639 * then we have no other option than to forward
2640 * packet on loopback and give up any attempt
2641 * to create a fake response.
2642 */
2643 if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2644 FR_ISBLOCK(pass)) {
2645
2646 if (fr_make_icmp(fin) == 0) {
2647 IPF_BUMP(
2648 ifs->ifs_frstats[out].fr_ret);
2649 }
2650 /*
2651 * we drop packet silently in case we
2652 * failed assemble fake response for it
2653 */
2654 else if (*mp != NULL) {
2655 FREE_MB_T(*mp);
2656 m = *mp = NULL;
2657 }
2658
2659 IPF_BUMP(
2660 ifs->ifs_frstats[out].fr_block);
2661 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2662
2663 return (0);
2664 }
2665 #endif /* _KERNEL && SOLARIS2 >= 10 */
2666
2667 (void) fr_send_icmp_err(ICMP_UNREACH, fin, dst);
2668 IPF_BUMP(ifs->ifs_frstats[out].fr_ret);
2669
2670 } else if (((pass & FR_RETMASK) == FR_RETRST) &&
2671 !(fin->fin_flx & FI_SHORT)) {
2672
2673 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2674 /*
2675 * Assume it's possible to enter insane rule:
2676 * pass return-rst in proto tcp ...
2677 * then we have no other option than to forward
2678 * packet on loopback and give up any attempt
2679 * to create a fake response.
2680 */
2681 if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2682 FR_ISBLOCK(pass)) {
2683 if (fr_make_rst(fin) == 0) {
2684 IPF_BUMP(
2685 ifs->ifs_frstats[out].fr_ret);
2686 }
2687 else if (mp != NULL) {
2688 /*
2689 * we drop packet silently in case we
2690 * failed assemble fake response for it
2691 */
2692 FREE_MB_T(*mp);
2693 m = *mp = NULL;
2694 }
2695
2696 IPF_BUMP(
2697 ifs->ifs_frstats[out].fr_block);
2698 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2699
2700 return (0);
2701 }
2702 #endif /* _KERNEL && _SOLARIS2 >= 10 */
2703 if (fr_send_reset(fin) == 0) {
2704 IPF_BUMP(ifs->ifs_frstats[1].fr_ret);
2705 }
2706 }
2707 } else {
2708 if (pass & FR_RETRST)
2709 fin->fin_error = ECONNRESET;
2710 }
2711 }
2712
2713 /*
2714 * If we didn't drop off the bottom of the list of rules (and thus
2715 * the 'current' rule fr is not NULL), then we may have some extra
2716 * instructions about what to do with a packet.
2717 * Once we're finished return to our caller, freeing the packet if
2718 * we are dropping it (* BSD ONLY *).
2719 * Reassign m from fin_m as we may have a new buffer, now.
2720 */
2721 filtered:
2722 m = fin->fin_m;
2723
2724 if (fr != NULL) {
2725 frdest_t *fdp;
2726
2727 fdp = &fr->fr_tifs[fin->fin_rev];
2728
2729 if (!out && (pass & FR_FASTROUTE)) {
2730 /*
2731 * For fastroute rule, no destioation interface defined
2732 * so pass NULL as the frdest_t parameter
2733 */
2734 (void) fr_fastroute(m, mp, fin, NULL);
2735 m = *mp = NULL;
2736 } else if ((fdp->fd_ifp != NULL) &&
2737 (fdp->fd_ifp != (struct ifnet *)-1)) {
2738 /* this is for to rules: */
2739 (void) fr_fastroute(m, mp, fin, fdp);
2740 m = *mp = NULL;
2741 }
2742
2743 /*
2744 * Send a duplicated packet.
2745 */
2746 if (mc != NULL) {
2747 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2748 /*
2749 * We are going to compute chksum for copies of loopback packets
2750 * only. IP stack does not compute chksums at all for loopback
2751 * packets. We want to get it fixed in their copies, since those
2752 * are going to be sent to network.
2753 */
2754 if (IPF_IS_LOOPBACK(qpi->qpi_flags))
2755 fr_calc_chksum(fin, mc);
2756 #endif
2757 (void) fr_fastroute(mc, &mc, fin, &fr->fr_dif);
2758 }
2759 }
2760
2761 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
2762 nat_uncreate(fin);
2763
2764 /*
2765 * This late because the likes of fr_fastroute() use fin_fr.
2766 */
2767 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2768
2769 finished:
2770 if (!FR_ISPASS(pass)) {
2771 IPF_BUMP(ifs->ifs_frstats[out].fr_block);
2772 if (*mp != NULL) {
2773 FREE_MB_T(*mp);
2774 m = *mp = NULL;
2775 }
2776 } else {
2777 IPF_BUMP(ifs->ifs_frstats[out].fr_pass);
2778 #if defined(_KERNEL) && defined(__sgi)
2779 if ((fin->fin_hbuf != NULL) &&
2780 (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
2781 COPYBACK(m, 0, fin->fin_plen, fin->fin_hbuf);
2782 }
2783 #endif
2784 }
2785
2786 SPL_X(s);
2787
2788 #ifdef _KERNEL
2789 # if OpenBSD >= 200311
2790 if (FR_ISPASS(pass) && (v == 4)) {
2791 ip = fin->fin_ip;
2792 ip->ip_len = ntohs(ip->ip_len);
2793 ip->ip_off = ntohs(ip->ip_off);
2794 }
2795 # endif
2796 return (FR_ISPASS(pass)) ? 0 : fin->fin_error;
2797 #else /* _KERNEL */
2798 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
2799 if ((pass & FR_NOMATCH) != 0)
2800 return 1;
2801
2802 if ((pass & FR_RETMASK) != 0)
2803 switch (pass & FR_RETMASK)
2804 {
2805 case FR_RETRST :
2806 return 3;
2807 case FR_RETICMP :
2808 return 4;
2809 case FR_FAKEICMP :
2810 return 5;
2811 }
2812
2813 switch (pass & FR_CMDMASK)
2814 {
2815 case FR_PASS :
2816 return 0;
2817 case FR_BLOCK :
2818 return -1;
2819 case FR_AUTH :
2820 return -2;
2821 case FR_ACCOUNT :
2822 return -3;
2823 case FR_PREAUTH :
2824 return -4;
2825 }
2826 return 2;
2827 #endif /* _KERNEL */
2828 }
2829
2830
2831 #ifdef IPFILTER_LOG
2832 /* ------------------------------------------------------------------------ */
2833 /* Function: fr_dolog */
2834 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */
2835 /* Parameters: fin(I) - pointer to packet information */
2836 /* passp(IO) - pointer to current/new filter decision (unused) */
2837 /* */
2838 /* Checks flags set to see how a packet should be logged, if it is to be */
2839 /* logged. Adjust statistics based on its success or not. */
2840 /* ------------------------------------------------------------------------ */
fr_dolog(fin,passp)2841 frentry_t *fr_dolog(fin, passp)
2842 fr_info_t *fin;
2843 u_32_t *passp;
2844 {
2845 u_32_t pass;
2846 int out;
2847 ipf_stack_t *ifs = fin->fin_ifs;
2848
2849 out = fin->fin_out;
2850 pass = *passp;
2851
2852 if ((ifs->ifs_fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
2853 pass |= FF_LOGNOMATCH;
2854 IPF_BUMP(ifs->ifs_frstats[out].fr_npkl);
2855 goto logit;
2856 } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
2857 (FR_ISPASS(pass) && (ifs->ifs_fr_flags & FF_LOGPASS))) {
2858 if ((pass & FR_LOGMASK) != FR_LOGP)
2859 pass |= FF_LOGPASS;
2860 IPF_BUMP(ifs->ifs_frstats[out].fr_ppkl);
2861 goto logit;
2862 } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
2863 (FR_ISBLOCK(pass) && (ifs->ifs_fr_flags & FF_LOGBLOCK))) {
2864 if ((pass & FR_LOGMASK) != FR_LOGB)
2865 pass |= FF_LOGBLOCK;
2866 IPF_BUMP(ifs->ifs_frstats[out].fr_bpkl);
2867 logit:
2868 if (ipflog(fin, pass) == -1) {
2869 IPF_BUMP(ifs->ifs_frstats[out].fr_skip);
2870
2871 /*
2872 * If the "or-block" option has been used then
2873 * block the packet if we failed to log it.
2874 */
2875 if ((pass & FR_LOGORBLOCK) &&
2876 FR_ISPASS(pass)) {
2877 pass &= ~FR_CMDMASK;
2878 pass |= FR_BLOCK;
2879 }
2880 }
2881 *passp = pass;
2882 }
2883
2884 return fin->fin_fr;
2885 }
2886 #endif /* IPFILTER_LOG */
2887
2888
2889 /* ------------------------------------------------------------------------ */
2890 /* Function: ipf_cksum */
2891 /* Returns: u_short - IP header checksum */
2892 /* Parameters: addr(I) - pointer to start of buffer to checksum */
2893 /* len(I) - length of buffer in bytes */
2894 /* */
2895 /* Calculate the two's complement 16 bit checksum of the buffer passed. */
2896 /* */
2897 /* N.B.: addr should be 16bit aligned. */
2898 /* ------------------------------------------------------------------------ */
ipf_cksum(addr,len)2899 u_short ipf_cksum(addr, len)
2900 u_short *addr;
2901 int len;
2902 {
2903 u_32_t sum = 0;
2904
2905 for (sum = 0; len > 1; len -= 2)
2906 sum += *addr++;
2907
2908 /* mop up an odd byte, if necessary */
2909 if (len == 1)
2910 sum += *(u_char *)addr;
2911
2912 /*
2913 * add back carry outs from top 16 bits to low 16 bits
2914 */
2915 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
2916 sum += (sum >> 16); /* add carry */
2917 return (u_short)(~sum);
2918 }
2919
2920
2921 /* ------------------------------------------------------------------------ */
2922 /* Function: fr_cksum */
2923 /* Returns: u_short - layer 4 checksum */
2924 /* Parameters: m(I ) - pointer to buffer holding packet */
2925 /* ip(I) - pointer to IP header */
2926 /* l4proto(I) - protocol to caclulate checksum for */
2927 /* l4hdr(I) - pointer to layer 4 header */
2928 /* */
2929 /* Calculates the TCP checksum for the packet held in "m", using the data */
2930 /* in the IP header "ip" to seed it. */
2931 /* */
2932 /* NB: This function assumes we've pullup'd enough for all of the IP header */
2933 /* and the TCP header. We also assume that data blocks aren't allocated in */
2934 /* odd sizes. */
2935 /* */
2936 /* Expects ip_len to be in host byte order when called. */
2937 /* ------------------------------------------------------------------------ */
fr_cksum(m,ip,l4proto,l4hdr)2938 u_short fr_cksum(m, ip, l4proto, l4hdr)
2939 mb_t *m;
2940 ip_t *ip;
2941 int l4proto;
2942 void *l4hdr;
2943 {
2944 u_short *sp, slen, sumsave, l4hlen, *csump;
2945 u_int sum, sum2;
2946 int hlen;
2947 #ifdef USE_INET6
2948 ip6_t *ip6;
2949 #endif
2950
2951 csump = NULL;
2952 sumsave = 0;
2953 l4hlen = 0;
2954 sp = NULL;
2955 slen = 0;
2956 hlen = 0;
2957 sum = 0;
2958
2959 /*
2960 * Add up IP Header portion
2961 */
2962 #ifdef USE_INET6
2963 if (IP_V(ip) == 4) {
2964 #endif
2965 hlen = IP_HL(ip) << 2;
2966 slen = ip->ip_len - hlen;
2967 sum = htons((u_short)l4proto);
2968 sum += htons(slen);
2969 sp = (u_short *)&ip->ip_src;
2970 sum += *sp++; /* ip_src */
2971 sum += *sp++;
2972 sum += *sp++; /* ip_dst */
2973 sum += *sp++;
2974 #ifdef USE_INET6
2975 } else if (IP_V(ip) == 6) {
2976 ip6 = (ip6_t *)ip;
2977 hlen = sizeof(*ip6);
2978 slen = ntohs(ip6->ip6_plen);
2979 sum = htons((u_short)l4proto);
2980 sum += htons(slen);
2981 sp = (u_short *)&ip6->ip6_src;
2982 sum += *sp++; /* ip6_src */
2983 sum += *sp++;
2984 sum += *sp++;
2985 sum += *sp++;
2986 sum += *sp++;
2987 sum += *sp++;
2988 sum += *sp++;
2989 sum += *sp++;
2990 sum += *sp++; /* ip6_dst */
2991 sum += *sp++;
2992 sum += *sp++;
2993 sum += *sp++;
2994 sum += *sp++;
2995 sum += *sp++;
2996 sum += *sp++;
2997 sum += *sp++;
2998 }
2999 #endif
3000
3001 switch (l4proto)
3002 {
3003 case IPPROTO_UDP :
3004 csump = &((udphdr_t *)l4hdr)->uh_sum;
3005 l4hlen = sizeof(udphdr_t);
3006 break;
3007
3008 case IPPROTO_TCP :
3009 csump = &((tcphdr_t *)l4hdr)->th_sum;
3010 l4hlen = sizeof(tcphdr_t);
3011 break;
3012 case IPPROTO_ICMP :
3013 csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
3014 l4hlen = 4;
3015 sum = 0;
3016 break;
3017 default :
3018 break;
3019 }
3020
3021 if (csump != NULL) {
3022 sumsave = *csump;
3023 *csump = 0;
3024 }
3025
3026 l4hlen = l4hlen; /* LINT */
3027
3028 #ifdef _KERNEL
3029 # ifdef MENTAT
3030 {
3031 void *rp = m->b_rptr;
3032
3033 if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr)
3034 m->b_rptr = (u_char *)ip;
3035 sum2 = ip_cksum(m, hlen, sum); /* hlen == offset */
3036 m->b_rptr = rp;
3037 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
3038 sum2 = ~sum2 & 0xffff;
3039 }
3040 # else /* MENTAT */
3041 # if defined(BSD) || defined(sun)
3042 # if BSD >= 199103
3043 m->m_data += hlen;
3044 # else
3045 m->m_off += hlen;
3046 # endif
3047 m->m_len -= hlen;
3048 sum2 = in_cksum(m, slen);
3049 m->m_len += hlen;
3050 # if BSD >= 199103
3051 m->m_data -= hlen;
3052 # else
3053 m->m_off -= hlen;
3054 # endif
3055 /*
3056 * Both sum and sum2 are partial sums, so combine them together.
3057 */
3058 sum += ~sum2 & 0xffff;
3059 while (sum > 0xffff)
3060 sum = (sum & 0xffff) + (sum >> 16);
3061 sum2 = ~sum & 0xffff;
3062 # else /* defined(BSD) || defined(sun) */
3063 {
3064 union {
3065 u_char c[2];
3066 u_short s;
3067 } bytes;
3068 u_short len = ip->ip_len;
3069 # if defined(__sgi)
3070 int add;
3071 # endif
3072
3073 /*
3074 * Add up IP Header portion
3075 */
3076 if (sp != (u_short *)l4hdr)
3077 sp = (u_short *)l4hdr;
3078
3079 switch (l4proto)
3080 {
3081 case IPPROTO_UDP :
3082 sum += *sp++; /* sport */
3083 sum += *sp++; /* dport */
3084 sum += *sp++; /* udp length */
3085 sum += *sp++; /* checksum */
3086 break;
3087
3088 case IPPROTO_TCP :
3089 sum += *sp++; /* sport */
3090 sum += *sp++; /* dport */
3091 sum += *sp++; /* seq */
3092 sum += *sp++;
3093 sum += *sp++; /* ack */
3094 sum += *sp++;
3095 sum += *sp++; /* off */
3096 sum += *sp++; /* win */
3097 sum += *sp++; /* checksum */
3098 sum += *sp++; /* urp */
3099 break;
3100 case IPPROTO_ICMP :
3101 sum = *sp++; /* type/code */
3102 sum += *sp++; /* checksum */
3103 break;
3104 }
3105
3106 # ifdef __sgi
3107 /*
3108 * In case we had to copy the IP & TCP header out of mbufs,
3109 * skip over the mbuf bits which are the header
3110 */
3111 if ((caddr_t)ip != mtod(m, caddr_t)) {
3112 hlen = (caddr_t)sp - (caddr_t)ip;
3113 while (hlen) {
3114 add = MIN(hlen, m->m_len);
3115 sp = (u_short *)(mtod(m, caddr_t) + add);
3116 hlen -= add;
3117 if (add == m->m_len) {
3118 m = m->m_next;
3119 if (!hlen) {
3120 if (!m)
3121 break;
3122 sp = mtod(m, u_short *);
3123 }
3124 PANIC((!m),("fr_cksum(1): not enough data"));
3125 }
3126 }
3127 }
3128 # endif
3129
3130 len -= (l4hlen + hlen);
3131 if (len <= 0)
3132 goto nodata;
3133
3134 while (len > 1) {
3135 if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
3136 m = m->m_next;
3137 PANIC((!m),("fr_cksum(2): not enough data"));
3138 sp = mtod(m, u_short *);
3139 }
3140 if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
3141 bytes.c[0] = *(u_char *)sp;
3142 m = m->m_next;
3143 PANIC((!m),("fr_cksum(3): not enough data"));
3144 sp = mtod(m, u_short *);
3145 bytes.c[1] = *(u_char *)sp;
3146 sum += bytes.s;
3147 sp = (u_short *)((u_char *)sp + 1);
3148 }
3149 if ((u_long)sp & 1) {
3150 bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
3151 sum += bytes.s;
3152 } else
3153 sum += *sp++;
3154 len -= 2;
3155 }
3156
3157 if (len != 0)
3158 sum += ntohs(*(u_char *)sp << 8);
3159 nodata:
3160 while (sum > 0xffff)
3161 sum = (sum & 0xffff) + (sum >> 16);
3162 sum2 = (u_short)(~sum & 0xffff);
3163 }
3164 # endif /* defined(BSD) || defined(sun) */
3165 # endif /* MENTAT */
3166 #else /* _KERNEL */
3167 for (; slen > 1; slen -= 2)
3168 sum += *sp++;
3169 if (slen)
3170 sum += ntohs(*(u_char *)sp << 8);
3171 while (sum > 0xffff)
3172 sum = (sum & 0xffff) + (sum >> 16);
3173 sum2 = (u_short)(~sum & 0xffff);
3174 #endif /* _KERNEL */
3175 if (csump != NULL)
3176 *csump = sumsave;
3177 return sum2;
3178 }
3179
3180
3181 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \
3182 defined(__sgi) ) && !defined(linux) && !defined(_AIX51)
3183 /*
3184 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3185 * The Regents of the University of California. All rights reserved.
3186 *
3187 * Redistribution and use in source and binary forms, with or without
3188 * modification, are permitted provided that the following conditions
3189 * are met:
3190 * 1. Redistributions of source code must retain the above copyright
3191 * notice, this list of conditions and the following disclaimer.
3192 * 2. Redistributions in binary form must reproduce the above copyright
3193 * notice, this list of conditions and the following disclaimer in the
3194 * documentation and/or other materials provided with the distribution.
3195 * 3. Neither the name of the University nor the names of its contributors
3196 * may be used to endorse or promote products derived from this software
3197 * without specific prior written permission.
3198 *
3199 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3200 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3201 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3202 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3203 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3204 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3205 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3206 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3207 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3208 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3209 * SUCH DAMAGE.
3210 *
3211 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
3212 * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $
3213 */
3214 /*
3215 * Copy data from an mbuf chain starting "off" bytes from the beginning,
3216 * continuing for "len" bytes, into the indicated buffer.
3217 */
3218 void
m_copydata(m,off,len,cp)3219 m_copydata(m, off, len, cp)
3220 mb_t *m;
3221 int off;
3222 int len;
3223 caddr_t cp;
3224 {
3225 unsigned count;
3226
3227 if (off < 0 || len < 0)
3228 panic("m_copydata");
3229 while (off > 0) {
3230 if (m == 0)
3231 panic("m_copydata");
3232 if (off < m->m_len)
3233 break;
3234 off -= m->m_len;
3235 m = m->m_next;
3236 }
3237 while (len > 0) {
3238 if (m == 0)
3239 panic("m_copydata");
3240 count = MIN(m->m_len - off, len);
3241 bcopy(mtod(m, caddr_t) + off, cp, count);
3242 len -= count;
3243 cp += count;
3244 off = 0;
3245 m = m->m_next;
3246 }
3247 }
3248
3249
3250 /*
3251 * Copy data from a buffer back into the indicated mbuf chain,
3252 * starting "off" bytes from the beginning, extending the mbuf
3253 * chain if necessary.
3254 */
3255 void
m_copyback(m0,off,len,cp)3256 m_copyback(m0, off, len, cp)
3257 struct mbuf *m0;
3258 int off;
3259 int len;
3260 caddr_t cp;
3261 {
3262 int mlen;
3263 struct mbuf *m = m0, *n;
3264 int totlen = 0;
3265
3266 if (m0 == 0)
3267 return;
3268 while (off > (mlen = m->m_len)) {
3269 off -= mlen;
3270 totlen += mlen;
3271 if (m->m_next == 0) {
3272 n = m_getclr(M_DONTWAIT, m->m_type);
3273 if (n == 0)
3274 goto out;
3275 n->m_len = min(MLEN, len + off);
3276 m->m_next = n;
3277 }
3278 m = m->m_next;
3279 }
3280 while (len > 0) {
3281 mlen = min(m->m_len - off, len);
3282 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
3283 cp += mlen;
3284 len -= mlen;
3285 mlen += off;
3286 off = 0;
3287 totlen += mlen;
3288 if (len == 0)
3289 break;
3290 if (m->m_next == 0) {
3291 n = m_get(M_DONTWAIT, m->m_type);
3292 if (n == 0)
3293 break;
3294 n->m_len = min(MLEN, len);
3295 m->m_next = n;
3296 }
3297 m = m->m_next;
3298 }
3299 out:
3300 #if 0
3301 if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
3302 m->m_pkthdr.len = totlen;
3303 #endif
3304 return;
3305 }
3306 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
3307
3308
3309 /* ------------------------------------------------------------------------ */
3310 /* Function: fr_findgroup */
3311 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */
3312 /* Parameters: group(I) - group name to search for */
3313 /* unit(I) - device to which this group belongs */
3314 /* set(I) - which set of rules (inactive/inactive) this is */
3315 /* fgpp(O) - pointer to place to store pointer to the pointer */
3316 /* to where to add the next (last) group or where */
3317 /* to delete group from. */
3318 /* */
3319 /* Search amongst the defined groups for a particular group number. */
3320 /* ------------------------------------------------------------------------ */
fr_findgroup(group,unit,set,fgpp,ifs)3321 frgroup_t *fr_findgroup(group, unit, set, fgpp, ifs)
3322 char *group;
3323 minor_t unit;
3324 int set;
3325 frgroup_t ***fgpp;
3326 ipf_stack_t *ifs;
3327 {
3328 frgroup_t *fg, **fgp;
3329
3330 /*
3331 * Which list of groups to search in is dependent on which list of
3332 * rules are being operated on.
3333 */
3334 fgp = &ifs->ifs_ipfgroups[unit][set];
3335
3336 while ((fg = *fgp) != NULL) {
3337 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
3338 break;
3339 else
3340 fgp = &fg->fg_next;
3341 }
3342 if (fgpp != NULL)
3343 *fgpp = fgp;
3344 return fg;
3345 }
3346
3347
3348 /* ------------------------------------------------------------------------ */
3349 /* Function: fr_addgroup */
3350 /* Returns: frgroup_t * - NULL == did not create group, */
3351 /* != NULL == pointer to the group */
3352 /* Parameters: num(I) - group number to add */
3353 /* head(I) - rule pointer that is using this as the head */
3354 /* flags(I) - rule flags which describe the type of rule it is */
3355 /* unit(I) - device to which this group will belong to */
3356 /* set(I) - which set of rules (inactive/inactive) this is */
3357 /* Write Locks: ipf_mutex */
3358 /* */
3359 /* Add a new group head, or if it already exists, increase the reference */
3360 /* count to it. */
3361 /* ------------------------------------------------------------------------ */
fr_addgroup(group,head,flags,unit,set,ifs)3362 frgroup_t *fr_addgroup(group, head, flags, unit, set, ifs)
3363 char *group;
3364 void *head;
3365 u_32_t flags;
3366 minor_t unit;
3367 int set;
3368 ipf_stack_t *ifs;
3369 {
3370 frgroup_t *fg, **fgp;
3371 u_32_t gflags;
3372
3373 if (group == NULL)
3374 return NULL;
3375
3376 if (unit == IPL_LOGIPF && *group == '\0')
3377 return NULL;
3378
3379 fgp = NULL;
3380 gflags = flags & FR_INOUT;
3381
3382 fg = fr_findgroup(group, unit, set, &fgp, ifs);
3383 if (fg != NULL) {
3384 if (fg->fg_flags == 0)
3385 fg->fg_flags = gflags;
3386 else if (gflags != fg->fg_flags)
3387 return NULL;
3388 fg->fg_ref++;
3389 return fg;
3390 }
3391 KMALLOC(fg, frgroup_t *);
3392 if (fg != NULL) {
3393 fg->fg_head = head;
3394 fg->fg_start = NULL;
3395 fg->fg_next = *fgp;
3396 bcopy(group, fg->fg_name, FR_GROUPLEN);
3397 fg->fg_flags = gflags;
3398 fg->fg_ref = 1;
3399 *fgp = fg;
3400 }
3401 return fg;
3402 }
3403
3404
3405 /* ------------------------------------------------------------------------ */
3406 /* Function: fr_delgroup */
3407 /* Returns: Nil */
3408 /* Parameters: group(I) - group name to delete */
3409 /* unit(I) - device to which this group belongs */
3410 /* set(I) - which set of rules (inactive/inactive) this is */
3411 /* Write Locks: ipf_mutex */
3412 /* */
3413 /* Attempt to delete a group head. */
3414 /* Only do this when its reference count reaches 0. */
3415 /* ------------------------------------------------------------------------ */
fr_delgroup(group,unit,set,ifs)3416 void fr_delgroup(group, unit, set, ifs)
3417 char *group;
3418 minor_t unit;
3419 int set;
3420 ipf_stack_t *ifs;
3421 {
3422 frgroup_t *fg, **fgp;
3423
3424 fg = fr_findgroup(group, unit, set, &fgp, ifs);
3425 if (fg == NULL)
3426 return;
3427
3428 fg->fg_ref--;
3429 if (fg->fg_ref == 0) {
3430 *fgp = fg->fg_next;
3431 KFREE(fg);
3432 }
3433 }
3434
3435
3436 /* ------------------------------------------------------------------------ */
3437 /* Function: fr_getrulen */
3438 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */
3439 /* Parameters: unit(I) - device for which to count the rule's number */
3440 /* flags(I) - which set of rules to find the rule in */
3441 /* group(I) - group name */
3442 /* n(I) - rule number to find */
3443 /* */
3444 /* Find rule # n in group # g and return a pointer to it. Return NULl if */
3445 /* group # g doesn't exist or there are less than n rules in the group. */
3446 /* ------------------------------------------------------------------------ */
fr_getrulen(unit,group,n,ifs)3447 frentry_t *fr_getrulen(unit, group, n, ifs)
3448 int unit;
3449 char *group;
3450 u_32_t n;
3451 ipf_stack_t *ifs;
3452 {
3453 frentry_t *fr;
3454 frgroup_t *fg;
3455
3456 fg = fr_findgroup(group, unit, ifs->ifs_fr_active, NULL, ifs);
3457 if (fg == NULL)
3458 return NULL;
3459 for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--)
3460 ;
3461 if (n != 0)
3462 return NULL;
3463 return fr;
3464 }
3465
3466
3467 /* ------------------------------------------------------------------------ */
3468 /* Function: fr_rulen */
3469 /* Returns: int - >= 0 - rule number, -1 == search failed */
3470 /* Parameters: unit(I) - device for which to count the rule's number */
3471 /* fr(I) - pointer to rule to match */
3472 /* */
3473 /* Return the number for a rule on a specific filtering device. */
3474 /* ------------------------------------------------------------------------ */
fr_rulen(unit,fr,ifs)3475 int fr_rulen(unit, fr, ifs)
3476 int unit;
3477 frentry_t *fr;
3478 ipf_stack_t *ifs;
3479 {
3480 frentry_t *fh;
3481 frgroup_t *fg;
3482 u_32_t n = 0;
3483
3484 if (fr == NULL)
3485 return -1;
3486 fg = fr_findgroup(fr->fr_group, unit, ifs->ifs_fr_active, NULL, ifs);
3487 if (fg == NULL)
3488 return -1;
3489 for (fh = fg->fg_head; fh; n++, fh = fh->fr_next)
3490 if (fh == fr)
3491 break;
3492 if (fh == NULL)
3493 return -1;
3494 return n;
3495 }
3496
3497
3498 /* ------------------------------------------------------------------------ */
3499 /* Function: frflushlist */
3500 /* Returns: int - >= 0 - number of flushed rules */
3501 /* Parameters: set(I) - which set of rules (inactive/inactive) this is */
3502 /* unit(I) - device for which to flush rules */
3503 /* flags(I) - which set of rules to flush */
3504 /* nfreedp(O) - pointer to int where flush count is stored */
3505 /* listp(I) - pointer to list to flush pointer */
3506 /* Write Locks: ipf_mutex */
3507 /* */
3508 /* Recursively flush rules from the list, descending groups as they are */
3509 /* encountered. if a rule is the head of a group and it has lost all its */
3510 /* group members, then also delete the group reference. nfreedp is needed */
3511 /* to store the accumulating count of rules removed, whereas the returned */
3512 /* value is just the number removed from the current list. The latter is */
3513 /* needed to correctly adjust reference counts on rules that define groups. */
3514 /* */
3515 /* NOTE: Rules not loaded from user space cannot be flushed. */
3516 /* ------------------------------------------------------------------------ */
frflushlist(set,unit,nfreedp,listp,ifs)3517 static int frflushlist(set, unit, nfreedp, listp, ifs)
3518 int set;
3519 minor_t unit;
3520 int *nfreedp;
3521 frentry_t **listp;
3522 ipf_stack_t *ifs;
3523 {
3524 int freed = 0;
3525 frentry_t *fp;
3526
3527 while ((fp = *listp) != NULL) {
3528 if ((fp->fr_type & FR_T_BUILTIN) ||
3529 !(fp->fr_flags & FR_COPIED)) {
3530 listp = &fp->fr_next;
3531 continue;
3532 }
3533 *listp = fp->fr_next;
3534 if (fp->fr_grp != NULL) {
3535 (void) frflushlist(set, unit, nfreedp, fp->fr_grp, ifs);
3536 }
3537
3538 if (fp->fr_grhead != NULL) {
3539 fr_delgroup(fp->fr_grhead, unit, set, ifs);
3540 *fp->fr_grhead = '\0';
3541 }
3542
3543 ASSERT(fp->fr_ref > 0);
3544 fp->fr_next = NULL;
3545 if (fr_derefrule(&fp, ifs) == 0)
3546 freed++;
3547 }
3548 *nfreedp += freed;
3549 return freed;
3550 }
3551
3552
3553 /* ------------------------------------------------------------------------ */
3554 /* Function: frflush */
3555 /* Returns: int - >= 0 - number of flushed rules */
3556 /* Parameters: unit(I) - device for which to flush rules */
3557 /* flags(I) - which set of rules to flush */
3558 /* */
3559 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3560 /* and IPv6) as defined by the value of flags. */
3561 /* ------------------------------------------------------------------------ */
frflush(unit,proto,flags,ifs)3562 int frflush(unit, proto, flags, ifs)
3563 minor_t unit;
3564 int proto, flags;
3565 ipf_stack_t *ifs;
3566 {
3567 int flushed = 0, set;
3568
3569 WRITE_ENTER(&ifs->ifs_ipf_mutex);
3570 bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache));
3571
3572 set = ifs->ifs_fr_active;
3573 if ((flags & FR_INACTIVE) == FR_INACTIVE)
3574 set = 1 - set;
3575
3576 if (flags & FR_OUTQUE) {
3577 if (proto == 0 || proto == 6) {
3578 (void) frflushlist(set, unit,
3579 &flushed, &ifs->ifs_ipfilter6[1][set], ifs);
3580 (void) frflushlist(set, unit,
3581 &flushed, &ifs->ifs_ipacct6[1][set], ifs);
3582 }
3583 if (proto == 0 || proto == 4) {
3584 (void) frflushlist(set, unit,
3585 &flushed, &ifs->ifs_ipfilter[1][set], ifs);
3586 (void) frflushlist(set, unit,
3587 &flushed, &ifs->ifs_ipacct[1][set], ifs);
3588 }
3589 }
3590 if (flags & FR_INQUE) {
3591 if (proto == 0 || proto == 6) {
3592 (void) frflushlist(set, unit,
3593 &flushed, &ifs->ifs_ipfilter6[0][set], ifs);
3594 (void) frflushlist(set, unit,
3595 &flushed, &ifs->ifs_ipacct6[0][set], ifs);
3596 }
3597 if (proto == 0 || proto == 4) {
3598 (void) frflushlist(set, unit,
3599 &flushed, &ifs->ifs_ipfilter[0][set], ifs);
3600 (void) frflushlist(set, unit,
3601 &flushed, &ifs->ifs_ipacct[0][set], ifs);
3602 }
3603 }
3604 RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
3605
3606 if (unit == IPL_LOGIPF) {
3607 int tmp;
3608
3609 tmp = frflush(IPL_LOGCOUNT, proto, flags, ifs);
3610 if (tmp >= 0)
3611 flushed += tmp;
3612 }
3613 return flushed;
3614 }
3615
3616
3617 /* ------------------------------------------------------------------------ */
3618 /* Function: memstr */
3619 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */
3620 /* Parameters: src(I) - pointer to byte sequence to match */
3621 /* dst(I) - pointer to byte sequence to search */
3622 /* slen(I) - match length */
3623 /* dlen(I) - length available to search in */
3624 /* */
3625 /* Search dst for a sequence of bytes matching those at src and extend for */
3626 /* slen bytes. */
3627 /* ------------------------------------------------------------------------ */
memstr(src,dst,slen,dlen)3628 char *memstr(src, dst, slen, dlen)
3629 char *src, *dst;
3630 int slen, dlen;
3631 {
3632 char *s = NULL;
3633
3634 while (dlen >= slen) {
3635 if (bcmp(src, dst, slen) == 0) {
3636 s = dst;
3637 break;
3638 }
3639 dst++;
3640 dlen--;
3641 }
3642 return s;
3643 }
3644 /* ------------------------------------------------------------------------ */
3645 /* Function: fr_fixskip */
3646 /* Returns: Nil */
3647 /* Parameters: listp(IO) - pointer to start of list with skip rule */
3648 /* rp(I) - rule added/removed with skip in it. */
3649 /* addremove(I) - adjustment (-1/+1) to make to skip count, */
3650 /* depending on whether a rule was just added */
3651 /* or removed. */
3652 /* */
3653 /* Adjust all the rules in a list which would have skip'd past the position */
3654 /* where we are inserting to skip to the right place given the change. */
3655 /* ------------------------------------------------------------------------ */
fr_fixskip(listp,rp,addremove)3656 void fr_fixskip(listp, rp, addremove)
3657 frentry_t **listp, *rp;
3658 int addremove;
3659 {
3660 int rules, rn;
3661 frentry_t *fp;
3662
3663 rules = 0;
3664 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
3665 rules++;
3666
3667 if (!fp)
3668 return;
3669
3670 for (rn = 0,