ip_frag.c revision 1.2.2.2 1 /* $NetBSD: ip_frag.c,v 1.2.2.2 2012/04/17 19:25:18 joerg Exp $ */
2
3 /*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8 #if defined(KERNEL) || defined(_KERNEL)
9 # undef KERNEL
10 # undef _KERNEL
11 # define KERNEL 1
12 # define _KERNEL 1
13 #endif
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18 #include <sys/file.h>
19 #ifdef __hpux
20 # include <sys/timeout.h>
21 #endif
22 #if !defined(_KERNEL)
23 # include <stdio.h>
24 # include <string.h>
25 # include <stdlib.h>
26 # define _KERNEL
27 # ifdef __OpenBSD__
28 struct file;
29 # endif
30 # include <sys/uio.h>
31 # undef _KERNEL
32 #endif
33 #if defined(_KERNEL) && \
34 defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)
35 # include <sys/filio.h>
36 # include <sys/fcntl.h>
37 #else
38 # include <sys/ioctl.h>
39 #endif
40 #if !defined(linux)
41 # include <sys/protosw.h>
42 #endif
43 #include <sys/socket.h>
44 #if defined(_KERNEL)
45 # include <sys/systm.h>
46 # if !defined(__SVR4) && !defined(__svr4__)
47 # include <sys/mbuf.h>
48 # endif
49 #endif
50 #if !defined(__SVR4) && !defined(__svr4__)
51 # if defined(_KERNEL) && !defined(__sgi) && !defined(AIX)
52 # include <sys/kernel.h>
53 # endif
54 #else
55 # include <sys/byteorder.h>
56 # ifdef _KERNEL
57 # include <sys/dditypes.h>
58 # endif
59 # include <sys/stream.h>
60 # include <sys/kmem.h>
61 #endif
62 #include <net/if.h>
63 #ifdef sun
64 # include <net/af.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #if !defined(linux)
70 # include <netinet/ip_var.h>
71 #endif
72 #include <netinet/tcp.h>
73 #include <netinet/udp.h>
74 #include <netinet/ip_icmp.h>
75 #include "netinet/ip_compat.h"
76 #include <netinet/tcpip.h>
77 #include "netinet/ip_fil.h"
78 #include "netinet/ip_nat.h"
79 #include "netinet/ip_frag.h"
80 #include "netinet/ip_state.h"
81 #include "netinet/ip_auth.h"
82 #include "netinet/ip_lookup.h"
83 #include "netinet/ip_proxy.h"
84 #include "netinet/ip_sync.h"
85 /* END OF INCLUDES */
86
87 #if !defined(lint)
88 #if defined(__NetBSD__)
89 #include <sys/cdefs.h>
90 __KERNEL_RCSID(0, "$NetBSD: ip_frag.c,v 1.2.2.2 2012/04/17 19:25:18 joerg Exp $");
91 #else
92 static const char sccsid[] = "@(#)ip_frag.c 1.11 3/24/96 (C) 1993-2000 Darren Reed";
93 static const char rcsid[] = "@(#)Id: ip_frag.c,v 2.110.2.9 2012/01/29 05:30:36 darrenr Exp";
94 #endif
95 #endif
96
97
98 typedef struct ipf_frag_softc_s {
99 ipfrwlock_t ipfr_ipidfrag;
100 ipfrwlock_t ipfr_frag;
101 ipfrwlock_t ipfr_natfrag;
102 int ipfr_size;
103 int ipfr_ttl;
104 int ipfr_lock;
105 int ipfr_inited;
106 ipfr_t *ipfr_list;
107 ipfr_t **ipfr_tail;
108 ipfr_t *ipfr_natlist;
109 ipfr_t **ipfr_nattail;
110 ipfr_t *ipfr_ipidlist;
111 ipfr_t **ipfr_ipidtail;
112 ipfr_t **ipfr_heads;
113 ipfr_t **ipfr_nattab;
114 ipfr_t **ipfr_ipidtab;
115 ipfrstat_t ipfr_stats;
116 } ipf_frag_softc_t;
117
118
119 #ifdef USE_MUTEXES
120 static ipfr_t *ipfr_frag_new(ipf_main_softc_t *, ipf_frag_softc_t *,
121 fr_info_t *, u_32_t, ipfr_t **,
122 ipfrwlock_t *);
123 static ipfr_t *ipf_frag_lookup(ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **, ipfrwlock_t *);
124 static void ipf_frag_deref(void *, ipfr_t **, ipfrwlock_t *);
125 static int ipf_frag_next(ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
126 ipfr_t **, ipfrwlock_t *);
127 #else
128 static ipfr_t *ipfr_frag_new(ipf_main_softc_t *, ipf_frag_softc_t *,
129 fr_info_t *, u_32_t, ipfr_t **);
130 static ipfr_t *ipf_frag_lookup(ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **);
131 static void ipf_frag_deref(void *, ipfr_t **);
132 static int ipf_frag_next(ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
133 ipfr_t **);
134 #endif
135 static void ipf_frag_delete(ipf_main_softc_t *, ipfr_t *, ipfr_t ***);
136 static void ipf_frag_free(ipf_frag_softc_t *, ipfr_t *);
137
138 static frentry_t ipfr_block;
139
140 ipftuneable_t ipf_tuneables[] = {
141 { { (void *)offsetof(ipf_frag_softc_t, ipfr_size) },
142 "frag_size", 1, 0x7fffffff,
143 stsizeof(ipf_frag_softc_t, ipfr_size),
144 IPFT_WRDISABLED, NULL, NULL },
145 { { (void *)offsetof(ipf_frag_softc_t, ipfr_ttl) },
146 "frag_ttl", 1, 0x7fffffff,
147 stsizeof(ipf_frag_softc_t, ipfr_ttl),
148 0, NULL, NULL },
149 { { NULL },
150 NULL, 0, 0,
151 0,
152 0, NULL, NULL }
153 };
154
155 #define FBUMP(x) softf->ipfr_stats.x++
156 #define FBUMPD(x) do { softf->ipfr_stats.x++; DT(x); } while (0)
157
158
159 /* ------------------------------------------------------------------------ */
160 /* Function: ipf_frag_main_load */
161 /* Returns: int - 0 == success, -1 == error */
162 /* Parameters: Nil */
163 /* */
164 /* Initialise the filter rule associted with blocked packets - everyone can */
165 /* use it. */
166 /* ------------------------------------------------------------------------ */
167 int
168 ipf_frag_main_load(void)
169 {
170 bzero((char *)&ipfr_block, sizeof(ipfr_block));
171 ipfr_block.fr_flags = FR_BLOCK|FR_QUICK;
172 ipfr_block.fr_ref = 1;
173
174 return 0;
175 }
176
177
178 /* ------------------------------------------------------------------------ */
179 /* Function: ipf_frag_main_unload */
180 /* Returns: int - 0 == success, -1 == error */
181 /* Parameters: Nil */
182 /* */
183 /* A null-op function that exists as a placeholder so that the flow in */
184 /* other functions is obvious. */
185 /* ------------------------------------------------------------------------ */
186 int
187 ipf_frag_main_unload(void)
188 {
189 return 0;
190 }
191
192
193 /* ------------------------------------------------------------------------ */
194 /* Function: ipf_frag_soft_create */
195 /* Returns: void * - NULL = failure, else pointer to local context */
196 /* Parameters: softc(I) - pointer to soft context main structure */
197 /* */
198 /* Allocate a new soft context structure to track fragment related info. */
199 /* ------------------------------------------------------------------------ */
200 /*ARGSUSED*/
201 void *
202 ipf_frag_soft_create(ipf_main_softc_t *softc)
203 {
204 ipf_frag_softc_t *softf;
205
206 KMALLOC(softf, ipf_frag_softc_t *);
207 if (softf == NULL)
208 return NULL;
209
210 bzero((char *)softf, sizeof(*softf));
211
212 RWLOCK_INIT(&softf->ipfr_ipidfrag, "frag ipid lock");
213 RWLOCK_INIT(&softf->ipfr_frag, "ipf fragment rwlock");
214 RWLOCK_INIT(&softf->ipfr_natfrag, "ipf NAT fragment rwlock");
215
216 softf->ipfr_size = IPFT_SIZE;
217 softf->ipfr_ttl = IPF_TTLVAL(60);
218 softf->ipfr_lock = 1;
219 softf->ipfr_tail = &softf->ipfr_list;
220 softf->ipfr_nattail = &softf->ipfr_natlist;
221 softf->ipfr_ipidtail = &softf->ipfr_ipidlist;
222
223 return softf;
224 }
225
226
227 /* ------------------------------------------------------------------------ */
228 /* Function: ipf_frag_soft_destroy */
229 /* Returns: Nil */
230 /* Parameters: softc(I) - pointer to soft context main structure */
231 /* arg(I) - pointer to local context to use */
232 /* */
233 /* Initialise the hash tables for the fragment cache lookups. */
234 /* ------------------------------------------------------------------------ */
235 void
236 ipf_frag_soft_destroy(ipf_main_softc_t *softc, void *arg)
237 {
238 ipf_frag_softc_t *softf = arg;
239
240 RW_DESTROY(&softf->ipfr_ipidfrag);
241 RW_DESTROY(&softf->ipfr_frag);
242 RW_DESTROY(&softf->ipfr_natfrag);
243
244 KFREE(softf);
245 }
246
247
248 /* ------------------------------------------------------------------------ */
249 /* Function: ipf_frag_soft_init */
250 /* Returns: int - 0 == success, -1 == error */
251 /* Parameters: softc(I) - pointer to soft context main structure */
252 /* arg(I) - pointer to local context to use */
253 /* */
254 /* Initialise the hash tables for the fragment cache lookups. */
255 /* ------------------------------------------------------------------------ */
256 /*ARGSUSED*/
257 int
258 ipf_frag_soft_init(ipf_main_softc_t *softc, void *arg)
259 {
260 ipf_frag_softc_t *softf = arg;
261
262 KMALLOCS(softf->ipfr_heads, ipfr_t **,
263 softf->ipfr_size * sizeof(ipfr_t *));
264 if (softf->ipfr_heads == NULL)
265 return -1;
266
267 bzero((char *)softf->ipfr_heads, softf->ipfr_size * sizeof(ipfr_t *));
268
269 KMALLOCS(softf->ipfr_nattab, ipfr_t **,
270 softf->ipfr_size * sizeof(ipfr_t *));
271 if (softf->ipfr_nattab == NULL)
272 return -2;
273
274 bzero((char *)softf->ipfr_nattab, softf->ipfr_size * sizeof(ipfr_t *));
275
276 KMALLOCS(softf->ipfr_ipidtab, ipfr_t **,
277 softf->ipfr_size * sizeof(ipfr_t *));
278 if (softf->ipfr_ipidtab == NULL)
279 return -3;
280
281 bzero((char *)softf->ipfr_ipidtab,
282 softf->ipfr_size * sizeof(ipfr_t *));
283
284 softf->ipfr_lock = 0;
285 softf->ipfr_inited = 1;
286
287 return 0;
288 }
289
290
291 /* ------------------------------------------------------------------------ */
292 /* Function: ipf_frag_soft_fini */
293 /* Returns: int - 0 == success, -1 == error */
294 /* Parameters: softc(I) - pointer to soft context main structure */
295 /* arg(I) - pointer to local context to use */
296 /* */
297 /* Free all memory allocated whilst running and from initialisation. */
298 /* ------------------------------------------------------------------------ */
299 int
300 ipf_frag_soft_fini(ipf_main_softc_t *softc, void *arg)
301 {
302 ipf_frag_softc_t *softf = arg;
303
304 softf->ipfr_lock = 1;
305
306 if (softf->ipfr_inited == 1) {
307 ipf_frag_clear(softc);
308
309 softf->ipfr_inited = 0;
310 }
311
312 if (softf->ipfr_heads != NULL)
313 KFREES(softf->ipfr_heads,
314 softf->ipfr_size * sizeof(ipfr_t *));
315 softf->ipfr_heads = NULL;
316
317 if (softf->ipfr_nattab != NULL)
318 KFREES(softf->ipfr_nattab,
319 softf->ipfr_size * sizeof(ipfr_t *));
320 softf->ipfr_nattab = NULL;
321
322 if (softf->ipfr_ipidtab != NULL)
323 KFREES(softf->ipfr_ipidtab,
324 softf->ipfr_size * sizeof(ipfr_t *));
325 softf->ipfr_ipidtab = NULL;
326
327 return 0;
328 }
329
330
331 /* ------------------------------------------------------------------------ */
332 /* Function: ipf_frag_set_lock */
333 /* Returns: Nil */
334 /* Parameters: arg(I) - pointer to local context to use */
335 /* tmp(I) - new value for lock */
336 /* */
337 /* Stub function that allows for external manipulation of ipfr_lock */
338 /* ------------------------------------------------------------------------ */
339 void
340 ipf_frag_setlock(void *arg, int tmp)
341 {
342 ipf_frag_softc_t *softf = arg;
343
344 softf->ipfr_lock = tmp;
345 }
346
347
348 /* ------------------------------------------------------------------------ */
349 /* Function: ipf_frag_stats */
350 /* Returns: ipfrstat_t* - pointer to struct with current frag stats */
351 /* Parameters: arg(I) - pointer to local context to use */
352 /* */
353 /* Updates ipfr_stats with current information and returns a pointer to it */
354 /* ------------------------------------------------------------------------ */
355 ipfrstat_t *
356 ipf_frag_stats(void *arg)
357 {
358 ipf_frag_softc_t *softf = arg;
359
360 softf->ipfr_stats.ifs_table = softf->ipfr_heads;
361 softf->ipfr_stats.ifs_nattab = softf->ipfr_nattab;
362 return &softf->ipfr_stats;
363 }
364
365
366 /* ------------------------------------------------------------------------ */
367 /* Function: ipfr_frag_new */
368 /* Returns: ipfr_t * - pointer to fragment cache state info or NULL */
369 /* Parameters: fin(I) - pointer to packet information */
370 /* table(I) - pointer to frag table to add to */
371 /* lock(I) - pointer to lock to get a write hold of */
372 /* */
373 /* Add a new entry to the fragment cache, registering it as having come */
374 /* through this box, with the result of the filter operation. */
375 /* */
376 /* If this function succeeds, it returns with a write lock held on "lock". */
377 /* If it fails, no lock is held on return. */
378 /* ------------------------------------------------------------------------ */
379 static ipfr_t *
380 ipfr_frag_new(
381 ipf_main_softc_t *softc,
382 ipf_frag_softc_t *softf,
383 fr_info_t *fin,
384 u_32_t pass,
385 ipfr_t *table[]
386 #ifdef USE_MUTEXES
387 , ipfrwlock_t *lock
388 #endif
389 )
390 {
391 ipfr_t *fra, frag, *fran;
392 u_int idx, off;
393 frentry_t *fr;
394
395 if (softf->ipfr_stats.ifs_inuse >= softf->ipfr_size) {
396 FBUMPD(ifs_maximum);
397 return NULL;
398 }
399
400 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) != FI_FRAG) {
401 FBUMPD(ifs_newbad);
402 return NULL;
403 }
404
405 if (pass & FR_FRSTRICT) {
406 if (fin->fin_off != 0) {
407 FBUMPD(ifs_newrestrictnot0);
408 return NULL;
409 }
410 }
411
412 frag.ipfr_v = fin->fin_v;
413 idx = fin->fin_v;
414 frag.ipfr_p = fin->fin_p;
415 idx += fin->fin_p;
416 frag.ipfr_id = fin->fin_id;
417 idx += fin->fin_id;
418 frag.ipfr_source = fin->fin_fi.fi_src;
419 idx += frag.ipfr_src.s_addr;
420 frag.ipfr_dest = fin->fin_fi.fi_dst;
421 idx += frag.ipfr_dst.s_addr;
422 frag.ipfr_ifp = fin->fin_ifp;
423 idx *= 127;
424 idx %= softf->ipfr_size;
425
426 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
427 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
428 frag.ipfr_auth = fin->fin_fi.fi_auth;
429
430 off = fin->fin_off >> 3;
431 #ifdef USE_INET6
432 if ((off == 0) && (fin->fin_v == 6)) {
433 char *ptr;
434 int end;
435
436 ptr = (char *)fin->fin_fraghdr + sizeof(struct ip6_frag);
437 end = fin->fin_plen - (ptr - (char *)fin->fin_ip);
438 frag.ipfr_firstend = end >> 3;
439 } else
440 #endif
441 frag.ipfr_firstend = 0;
442
443 /*
444 * allocate some memory, if possible, if not, just record that we
445 * failed to do so.
446 */
447 KMALLOC(fran, ipfr_t *);
448 if (fran == NULL) {
449 FBUMPD(ifs_nomem);
450 return NULL;
451 }
452
453 WRITE_ENTER(lock);
454
455 /*
456 * first, make sure it isn't already there...
457 */
458 for (fra = table[idx]; (fra != NULL); fra = fra->ipfr_hnext)
459 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp,
460 IPFR_CMPSZ)) {
461 RWLOCK_EXIT(lock);
462 FBUMPD(ifs_exists);
463 KFREE(fra);
464 return NULL;
465 }
466
467 fra = fran;
468 fran = NULL;
469 fr = fin->fin_fr;
470 fra->ipfr_rule = fr;
471 if (fr != NULL) {
472 MUTEX_ENTER(&fr->fr_lock);
473 fr->fr_ref++;
474 MUTEX_EXIT(&fr->fr_lock);
475 }
476
477 /*
478 * Insert the fragment into the fragment table, copy the struct used
479 * in the search using bcopy rather than reassign each field.
480 * Set the ttl to the default.
481 */
482 if ((fra->ipfr_hnext = table[idx]) != NULL)
483 table[idx]->ipfr_hprev = &fra->ipfr_hnext;
484 fra->ipfr_hprev = table + idx;
485 fra->ipfr_data = NULL;
486 table[idx] = fra;
487 bcopy((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp, IPFR_CMPSZ);
488 fra->ipfr_v = fin->fin_v;
489 fra->ipfr_ttl = softc->ipf_ticks + softf->ipfr_ttl;
490 fra->ipfr_firstend = frag.ipfr_firstend;
491
492 /*
493 * Compute the offset of the expected start of the next packet.
494 */
495 if (off == 0)
496 fra->ipfr_seen0 = 1;
497 fra->ipfr_off = off + (fin->fin_dlen >> 3);
498 fra->ipfr_pass = pass;
499 fra->ipfr_ref = 1;
500 fra->ipfr_pkts = 1;
501 fra->ipfr_bytes = fin->fin_plen;
502 FBUMP(ifs_inuse);
503 FBUMP(ifs_new);
504 return fra;
505 }
506
507
508 /* ------------------------------------------------------------------------ */
509 /* Function: ipf_frag_new */
510 /* Returns: int - 0 == success, -1 == error */
511 /* Parameters: fin(I) - pointer to packet information */
512 /* */
513 /* Add a new entry to the fragment cache table based on the current packet */
514 /* ------------------------------------------------------------------------ */
515 int
516 ipf_frag_new(ipf_main_softc_t *softc, fr_info_t *fin, u_32_t pass)
517 {
518 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
519 ipfr_t *fra;
520
521 if (softf->ipfr_lock != 0)
522 return -1;
523
524 #ifdef USE_MUTEXES
525 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads, &softc->ipf_frag);
526 #else
527 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads);
528 #endif
529 if (fra != NULL) {
530 *softf->ipfr_tail = fra;
531 fra->ipfr_prev = softf->ipfr_tail;
532 softf->ipfr_tail = &fra->ipfr_next;
533 fra->ipfr_next = NULL;
534 RWLOCK_EXIT(&softc->ipf_frag);
535 }
536 return fra ? 0 : -1;
537 }
538
539
540 /* ------------------------------------------------------------------------ */
541 /* Function: ipf_frag_natnew */
542 /* Returns: int - 0 == success, -1 == error */
543 /* Parameters: fin(I) - pointer to packet information */
544 /* nat(I) - pointer to NAT structure */
545 /* */
546 /* Create a new NAT fragment cache entry based on the current packet and */
547 /* the NAT structure for this "session". */
548 /* ------------------------------------------------------------------------ */
549 int
550 ipf_frag_natnew(ipf_main_softc_t *softc, fr_info_t *fin, u_32_t pass,
551 nat_t *nat)
552 {
553 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
554 ipfr_t *fra;
555
556 if ((fin->fin_v != 4) || (softf->ipfr_lock != 0))
557 return 0;
558
559 #ifdef USE_MUTEXES
560 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab,
561 &softf->ipfr_natfrag);
562 #else
563 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab);
564 #endif
565 if (fra != NULL) {
566 fra->ipfr_data = nat;
567 nat->nat_data = fra;
568 *softf->ipfr_nattail = fra;
569 fra->ipfr_prev = softf->ipfr_nattail;
570 softf->ipfr_nattail = &fra->ipfr_next;
571 fra->ipfr_next = NULL;
572 RWLOCK_EXIT(&softf->ipfr_natfrag);
573 }
574 return fra ? 0 : -1;
575 }
576
577
578 /* ------------------------------------------------------------------------ */
579 /* Function: ipf_frag_ipidnew */
580 /* Returns: int - 0 == success, -1 == error */
581 /* Parameters: fin(I) - pointer to packet information */
582 /* ipid(I) - new IP ID for this fragmented packet */
583 /* */
584 /* Create a new fragment cache entry for this packet and store, as a data */
585 /* pointer, the new IP ID value. */
586 /* ------------------------------------------------------------------------ */
587 int
588 ipf_frag_ipidnew(fr_info_t *fin, u_32_t ipid)
589 {
590 ipf_main_softc_t *softc = fin->fin_main_soft;
591 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
592 ipfr_t *fra;
593
594 if (softf->ipfr_lock)
595 return 0;
596
597 #ifdef USE_MUTEXES
598 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab, &softf->ipfr_ipidfrag);
599 #else
600 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab);
601 #endif
602 if (fra != NULL) {
603 fra->ipfr_data = (void *)(intptr_t)ipid;
604 *softf->ipfr_ipidtail = fra;
605 fra->ipfr_prev = softf->ipfr_ipidtail;
606 softf->ipfr_ipidtail = &fra->ipfr_next;
607 fra->ipfr_next = NULL;
608 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
609 }
610 return fra ? 0 : -1;
611 }
612
613
614 /* ------------------------------------------------------------------------ */
615 /* Function: ipf_frag_lookup */
616 /* Returns: ipfr_t * - pointer to ipfr_t structure if there's a */
617 /* matching entry in the frag table, else NULL */
618 /* Parameters: fin(I) - pointer to packet information */
619 /* table(I) - pointer to fragment cache table to search */
620 /* */
621 /* Check the fragment cache to see if there is already a record of this */
622 /* packet with its filter result known. */
623 /* */
624 /* If this function succeeds, it returns with a write lock held on "lock". */
625 /* If it fails, no lock is held on return. */
626 /* ------------------------------------------------------------------------ */
627 static ipfr_t *
628 ipf_frag_lookup(
629 ipf_main_softc_t *softc,
630 ipf_frag_softc_t *softf,
631 fr_info_t *fin,
632 ipfr_t *table[]
633 #ifdef USE_MUTEXES
634 , ipfrwlock_t *lock
635 #endif
636 )
637 {
638 ipfr_t *f, frag;
639 u_int idx;
640
641 /*
642 * We don't want to let short packets match because they could be
643 * compromising the security of other rules that want to match on
644 * layer 4 fields (and can't because they have been fragmented off.)
645 * Why do this check here? The counter acts as an indicator of this
646 * kind of attack, whereas if it was elsewhere, it wouldn't know if
647 * other matching packets had been seen.
648 */
649 if (fin->fin_flx & FI_SHORT) {
650 FBUMPD(ifs_short);
651 return NULL;
652 }
653
654 if ((fin->fin_flx & FI_BAD) != 0) {
655 FBUMPD(ifs_bad);
656 return NULL;
657 }
658
659 /*
660 * For fragments, we record protocol, packet id, TOS and both IP#'s
661 * (these should all be the same for all fragments of a packet).
662 *
663 * build up a hash value to index the table with.
664 */
665 frag.ipfr_v = fin->fin_v;
666 idx = fin->fin_v;
667 frag.ipfr_p = fin->fin_p;
668 idx += fin->fin_p;
669 frag.ipfr_id = fin->fin_id;
670 idx += fin->fin_id;
671 frag.ipfr_source = fin->fin_fi.fi_src;
672 idx += frag.ipfr_src.s_addr;
673 frag.ipfr_dest = fin->fin_fi.fi_dst;
674 idx += frag.ipfr_dst.s_addr;
675 frag.ipfr_ifp = fin->fin_ifp;
676 idx *= 127;
677 idx %= softf->ipfr_size;
678
679 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
680 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
681 frag.ipfr_auth = fin->fin_fi.fi_auth;
682
683 READ_ENTER(lock);
684
685 /*
686 * check the table, careful to only compare the right amount of data
687 */
688 for (f = table[idx]; f; f = f->ipfr_hnext) {
689 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&f->ipfr_ifp,
690 IPFR_CMPSZ)) {
691 u_short off;
692
693 /*
694 * XXX - We really need to be guarding against the
695 * retransmission of (src,dst,id,offset-range) here
696 * because a fragmented packet is never resent with
697 * the same IP ID# (or shouldn't).
698 */
699 off = fin->fin_off >> 3;
700 if (f->ipfr_seen0) {
701 if (off == 0) {
702 FBUMPD(ifs_retrans0);
703 continue;
704 }
705
706 /*
707 * Case 3. See comment for frpr_fragment6.
708 */
709 if ((f->ipfr_firstend != 0) &&
710 (off < f->ipfr_firstend)) {
711 FBUMPD(ifs_overlap);
712 fin->fin_flx |= FI_BAD;
713 break;
714 }
715 } else if (off == 0)
716 f->ipfr_seen0 = 1;
717
718 if (f != table[idx]) {
719 ipfr_t **fp;
720
721 /*
722 * Move fragment info. to the top of the list
723 * to speed up searches. First, delink...
724 */
725 fp = f->ipfr_hprev;
726 (*fp) = f->ipfr_hnext;
727 if (f->ipfr_hnext != NULL)
728 f->ipfr_hnext->ipfr_hprev = fp;
729 /*
730 * Then put back at the top of the chain.
731 */
732 f->ipfr_hnext = table[idx];
733 table[idx]->ipfr_hprev = &f->ipfr_hnext;
734 f->ipfr_hprev = table + idx;
735 table[idx] = f;
736 }
737
738 /*
739 * If we've follwed the fragments, and this is the
740 * last (in order), shrink expiration time.
741 */
742 if (off == f->ipfr_off) {
743 f->ipfr_off = (fin->fin_dlen >> 3) + off;
744
745 /*
746 * Well, we could shrink the expiration time
747 * but only if every fragment has been seen
748 * in order upto this, the last. ipfr_badorder
749 * is used here to count those out of order
750 * and if it equals 0 when we get to the last
751 * fragment then we can assume all of the
752 * fragments have been seen and in order.
753 */
754 #if 0
755 /*
756 * Doing this properly requires moving it to
757 * the head of the list which is infesible.
758 */
759 if ((more == 0) && (f->ipfr_badorder == 0))
760 f->ipfr_ttl = softc->ipf_ticks + 1;
761 #endif
762 } else {
763 f->ipfr_badorder++;
764 FBUMPD(ifs_unordered);
765 if (f->ipfr_pass & FR_FRSTRICT) {
766 FBUMPD(ifs_strict);
767 continue;
768 }
769 }
770 f->ipfr_pkts++;
771 f->ipfr_bytes += fin->fin_plen;
772 FBUMP(ifs_hits);
773 return f;
774 }
775 }
776
777 RWLOCK_EXIT(lock);
778 FBUMP(ifs_miss);
779 return NULL;
780 }
781
782
783 /* ------------------------------------------------------------------------ */
784 /* Function: ipf_frag_natknown */
785 /* Returns: nat_t* - pointer to 'parent' NAT structure if frag table */
786 /* match found, else NULL */
787 /* Parameters: fin(I) - pointer to packet information */
788 /* */
789 /* Functional interface for NAT lookups of the NAT fragment cache */
790 /* ------------------------------------------------------------------------ */
791 nat_t *
792 ipf_frag_natknown(fr_info_t *fin)
793 {
794 ipf_main_softc_t *softc = fin->fin_main_soft;
795 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
796 nat_t *nat;
797 ipfr_t *ipf;
798
799 if ((softf->ipfr_lock) || !softf->ipfr_natlist)
800 return NULL;
801 #ifdef USE_MUTEXES
802 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab,
803 &softf->ipfr_natfrag);
804 #else
805 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab);
806 #endif
807 if (ipf != NULL) {
808 nat = ipf->ipfr_data;
809 /*
810 * This is the last fragment for this packet.
811 */
812 if ((ipf->ipfr_ttl == softc->ipf_ticks + 1) && (nat != NULL)) {
813 nat->nat_data = NULL;
814 ipf->ipfr_data = NULL;
815 }
816 RWLOCK_EXIT(&softf->ipfr_natfrag);
817 } else
818 nat = NULL;
819 return nat;
820 }
821
822
823 /* ------------------------------------------------------------------------ */
824 /* Function: ipf_frag_ipidknown */
825 /* Returns: u_32_t - IPv4 ID for this packet if match found, else */
826 /* return 0xfffffff to indicate no match. */
827 /* Parameters: fin(I) - pointer to packet information */
828 /* */
829 /* Functional interface for IP ID lookups of the IP ID fragment cache */
830 /* ------------------------------------------------------------------------ */
831 u_32_t
832 ipf_frag_ipidknown(fr_info_t *fin)
833 {
834 ipf_main_softc_t *softc = fin->fin_main_soft;
835 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
836 ipfr_t *ipf;
837 u_32_t id;
838
839 if ((fin->fin_v != 4) || (softf->ipfr_lock) ||
840 !softf->ipfr_ipidlist)
841 return 0xffffffff;
842
843 #ifdef USE_MUTEXES
844 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab,
845 &softf->ipfr_ipidfrag);
846 #else
847 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab);
848 #endif
849 if (ipf != NULL) {
850 id = (u_32_t)(intptr_t)ipf->ipfr_data;
851 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
852 } else
853 id = 0xffffffff;
854 return id;
855 }
856
857
858 /* ------------------------------------------------------------------------ */
859 /* Function: ipf_frag_known */
860 /* Returns: frentry_t* - pointer to filter rule if a match is found in */
861 /* the frag cache table, else NULL. */
862 /* Parameters: fin(I) - pointer to packet information */
863 /* passp(O) - pointer to where to store rule flags resturned */
864 /* */
865 /* Functional interface for normal lookups of the fragment cache. If a */
866 /* match is found, return the rule pointer and flags from the rule, except */
867 /* that if FR_LOGFIRST is set, reset FR_LOG. */
868 /* ------------------------------------------------------------------------ */
869 frentry_t *
870 ipf_frag_known(fr_info_t *fin, u_32_t *passp)
871 {
872 ipf_main_softc_t *softc = fin->fin_main_soft;
873 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
874 frentry_t *fr = NULL;
875 ipfr_t *fra;
876 u_32_t pass;
877
878 if ((softf->ipfr_lock) || (softf->ipfr_list == NULL))
879 return NULL;
880
881 #ifdef USE_MUTEXES
882 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads,
883 &softc->ipf_frag);
884 #else
885 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads);
886 #endif
887 if (fra != NULL) {
888 if (fin->fin_flx & FI_BAD) {
889 fr = &ipfr_block;
890 fin->fin_reason = FRB_BADFRAG;
891 } else {
892 fr = fra->ipfr_rule;
893 }
894 fin->fin_fr = fr;
895 if (fr != NULL) {
896 pass = fr->fr_flags;
897 if ((pass & FR_KEEPSTATE) != 0) {
898 fin->fin_flx |= FI_STATE;
899 /*
900 * Reset the keep state flag here so that we
901 * don't try and add a new state entry because
902 * of a match here. That leads to blocking of
903 * the packet later because the add fails.
904 */
905 pass &= ~FR_KEEPSTATE;
906 }
907 if ((pass & FR_LOGFIRST) != 0)
908 pass &= ~(FR_LOGFIRST|FR_LOG);
909 *passp = pass;
910 }
911 RWLOCK_EXIT(&softc->ipf_frag);
912 }
913 return fr;
914 }
915
916
917 /* ------------------------------------------------------------------------ */
918 /* Function: ipf_frag_natforget */
919 /* Returns: Nil */
920 /* Parameters: ptr(I) - pointer to data structure */
921 /* */
922 /* Search through all of the fragment cache entries for NAT and wherever a */
923 /* pointer is found to match ptr, reset it to NULL. */
924 /* ------------------------------------------------------------------------ */
925 void
926 ipf_frag_natforget(ipf_main_softc_t *softc, void *ptr)
927 {
928 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
929 ipfr_t *fr;
930
931 WRITE_ENTER(&softf->ipfr_natfrag);
932 for (fr = softf->ipfr_natlist; fr; fr = fr->ipfr_next)
933 if (fr->ipfr_data == ptr)
934 fr->ipfr_data = NULL;
935 RWLOCK_EXIT(&softf->ipfr_natfrag);
936 }
937
938
939 /* ------------------------------------------------------------------------ */
940 /* Function: ipf_frag_delete */
941 /* Returns: Nil */
942 /* Parameters: fra(I) - pointer to fragment structure to delete */
943 /* tail(IO) - pointer to the pointer to the tail of the frag */
944 /* list */
945 /* */
946 /* Remove a fragment cache table entry from the table & list. Also free */
947 /* the filter rule it is associated with it if it is no longer used as a */
948 /* result of decreasing the reference count. */
949 /* ------------------------------------------------------------------------ */
950 static void
951 ipf_frag_delete(ipf_main_softc_t *softc, ipfr_t *fra, ipfr_t ***tail)
952 {
953 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
954
955 if (fra->ipfr_next)
956 fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
957 *fra->ipfr_prev = fra->ipfr_next;
958 if (*tail == &fra->ipfr_next)
959 *tail = fra->ipfr_prev;
960
961 if (fra->ipfr_hnext)
962 fra->ipfr_hnext->ipfr_hprev = fra->ipfr_hprev;
963 *fra->ipfr_hprev = fra->ipfr_hnext;
964
965 if (fra->ipfr_rule != NULL) {
966 (void) ipf_derefrule(softc, &fra->ipfr_rule);
967 }
968
969 if (fra->ipfr_ref <= 0)
970 ipf_frag_free(softf, fra);
971 }
972
973
974 /* ------------------------------------------------------------------------ */
975 /* Function: ipf_frag_free */
976 /* Returns: Nil */
977 /* */
978 /* ------------------------------------------------------------------------ */
979 static void
980 ipf_frag_free(ipf_frag_softc_t *softf, ipfr_t *fra)
981 {
982 KFREE(fra);
983 FBUMP(ifs_expire);
984 softf->ipfr_stats.ifs_inuse--;
985 }
986
987
988 /* ------------------------------------------------------------------------ */
989 /* Function: ipf_frag_clear */
990 /* Returns: Nil */
991 /* Parameters: Nil */
992 /* */
993 /* Free memory in use by fragment state information kept. Do the normal */
994 /* fragment state stuff first and then the NAT-fragment table. */
995 /* ------------------------------------------------------------------------ */
996 void
997 ipf_frag_clear(ipf_main_softc_t *softc)
998 {
999 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1000 ipfr_t *fra;
1001 nat_t *nat;
1002
1003 WRITE_ENTER(&softc->ipf_frag);
1004 while ((fra = softf->ipfr_list) != NULL) {
1005 fra->ipfr_ref--;
1006 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1007 }
1008 softf->ipfr_tail = &softf->ipfr_list;
1009 RWLOCK_EXIT(&softc->ipf_frag);
1010
1011 WRITE_ENTER(&softc->ipf_nat);
1012 WRITE_ENTER(&softf->ipfr_natfrag);
1013 while ((fra = softf->ipfr_natlist) != NULL) {
1014 nat = fra->ipfr_data;
1015 if (nat != NULL) {
1016 if (nat->nat_data == fra)
1017 nat->nat_data = NULL;
1018 }
1019 fra->ipfr_ref--;
1020 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1021 }
1022 softf->ipfr_nattail = &softf->ipfr_natlist;
1023 RWLOCK_EXIT(&softf->ipfr_natfrag);
1024 RWLOCK_EXIT(&softc->ipf_nat);
1025 }
1026
1027
1028 /* ------------------------------------------------------------------------ */
1029 /* Function: ipf_frag_expire */
1030 /* Returns: Nil */
1031 /* Parameters: Nil */
1032 /* */
1033 /* Expire entries in the fragment cache table that have been there too long */
1034 /* ------------------------------------------------------------------------ */
1035 void
1036 ipf_frag_expire(ipf_main_softc_t *softc)
1037 {
1038 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1039 ipfr_t **fp, *fra;
1040 nat_t *nat;
1041 SPL_INT(s);
1042
1043 if (softf->ipfr_lock)
1044 return;
1045
1046 SPL_NET(s);
1047 WRITE_ENTER(&softc->ipf_frag);
1048 /*
1049 * Go through the entire table, looking for entries to expire,
1050 * which is indicated by the ttl being less than or equal to ipf_ticks.
1051 */
1052 for (fp = &softf->ipfr_list; ((fra = *fp) != NULL); ) {
1053 if (fra->ipfr_ttl > softc->ipf_ticks)
1054 break;
1055 fra->ipfr_ref--;
1056 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1057 }
1058 RWLOCK_EXIT(&softc->ipf_frag);
1059
1060 WRITE_ENTER(&softf->ipfr_ipidfrag);
1061 for (fp = &softf->ipfr_ipidlist; ((fra = *fp) != NULL); ) {
1062 if (fra->ipfr_ttl > softc->ipf_ticks)
1063 break;
1064 fra->ipfr_ref--;
1065 ipf_frag_delete(softc, fra, &softf->ipfr_ipidtail);
1066 }
1067 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
1068
1069 /*
1070 * Same again for the NAT table, except that if the structure also
1071 * still points to a NAT structure, and the NAT structure points back
1072 * at the one to be free'd, NULL the reference from the NAT struct.
1073 * NOTE: We need to grab both mutex's early, and in this order so as
1074 * to prevent a deadlock if both try to expire at the same time.
1075 * The extra if() statement here is because it locks out all NAT
1076 * operations - no need to do that if there are no entries in this
1077 * list, right?
1078 */
1079 if (softf->ipfr_natlist != NULL) {
1080 WRITE_ENTER(&softc->ipf_nat);
1081 WRITE_ENTER(&softf->ipfr_natfrag);
1082 for (fp = &softf->ipfr_natlist; ((fra = *fp) != NULL); ) {
1083 if (fra->ipfr_ttl > softc->ipf_ticks)
1084 break;
1085 nat = fra->ipfr_data;
1086 if (nat != NULL) {
1087 if (nat->nat_data == fra)
1088 nat->nat_data = NULL;
1089 }
1090 fra->ipfr_ref--;
1091 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1092 }
1093 RWLOCK_EXIT(&softf->ipfr_natfrag);
1094 RWLOCK_EXIT(&softc->ipf_nat);
1095 }
1096 SPL_X(s);
1097 }
1098
1099
1100 /* ------------------------------------------------------------------------ */
1101 /* Function: ipf_frag_pkt_next */
1102 /* ------------------------------------------------------------------------ */
1103 int
1104 ipf_frag_pkt_next(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp)
1105 {
1106 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1107
1108 #ifdef USE_MUTEXES
1109 return ipf_frag_next(softc, token, itp, &softf->ipfr_list,
1110 &softf->ipfr_frag);
1111 #else
1112 return ipf_frag_next(softc, token, itp, &softf->ipfr_list);
1113 #endif
1114 }
1115
1116
1117 /* ------------------------------------------------------------------------ */
1118 /* Function: ipf_frag_nat_next */
1119 /* ------------------------------------------------------------------------ */
1120 int
1121 ipf_frag_nat_next(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp)
1122 {
1123 ipf_frag_softc_t *softf = softc->ipf_frag_soft;;
1124
1125 #ifdef USE_MUTEXES
1126 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist,
1127 &softf->ipfr_natfrag);
1128 #else
1129 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist);
1130 #endif
1131 }
1132
1133 /* ------------------------------------------------------------------------ */
1134 /* Function: ipf_frag_next */
1135 /* Returns: int - 0 == success, else error */
1136 /* Parameters: token(I) - pointer to token information for this caller */
1137 /* itp(I) - pointer to generic iterator from caller */
1138 /* top(I) - top of the fragment list */
1139 /* lock(I) - fragment cache lock */
1140 /* */
1141 /* This function is used to interate through the list of entries in the */
1142 /* fragment cache. It increases the reference count on the one currently */
1143 /* being returned so that the caller can come back and resume from it later.*/
1144 /* */
1145 /* This function is used for both the NAT fragment cache as well as the ipf */
1146 /* fragment cache - hence the reason for passing in top and lock. */
1147 /* ------------------------------------------------------------------------ */
1148 static int
1149 ipf_frag_next(
1150 ipf_main_softc_t *softc,
1151 ipftoken_t *token,
1152 ipfgeniter_t *itp,
1153 ipfr_t **top
1154 #ifdef USE_MUTEXES
1155 , ipfrwlock_t *lock
1156 #endif
1157 )
1158 {
1159 ipfr_t *frag, *next, zero;
1160 int error = 0;
1161
1162 if (itp->igi_data == NULL) {
1163 IPFERROR(20001);
1164 return EFAULT;
1165 }
1166
1167 if (itp->igi_nitems != 1) {
1168 IPFERROR(20003);
1169 return EFAULT;
1170 }
1171
1172 frag = token->ipt_data;
1173
1174 READ_ENTER(lock);
1175
1176 if (frag == NULL)
1177 next = *top;
1178 else
1179 next = frag->ipfr_next;
1180
1181 if (next != NULL) {
1182 ATOMIC_INC(next->ipfr_ref);
1183 token->ipt_data = next;
1184 } else {
1185 bzero(&zero, sizeof(zero));
1186 next = &zero;
1187 token->ipt_data = NULL;
1188 }
1189 if (next->ipfr_next == NULL)
1190 ipf_token_mark_complete(token);
1191
1192 RWLOCK_EXIT(lock);
1193
1194 error = COPYOUT(next, itp->igi_data, sizeof(*next));
1195 if (error != 0)
1196 IPFERROR(20002);
1197
1198 if (frag != NULL) {
1199 #ifdef USE_MUTEXES
1200 ipf_frag_deref(softc, &frag, lock);
1201 #else
1202 ipf_frag_deref(softc, &frag);
1203 #endif
1204 }
1205 return error;
1206 }
1207
1208
1209 /* ------------------------------------------------------------------------ */
1210 /* Function: ipf_frag_pkt_deref */
1211 /* Returns: Nil */
1212 /* */
1213 /* ------------------------------------------------------------------------ */
1214 void
1215 ipf_frag_pkt_deref(ipf_main_softc_t *softc, void *data)
1216 {
1217 ipfr_t **frp = data;
1218
1219 #ifdef USE_MUTEXES
1220 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1221
1222 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_frag);
1223 #else
1224 ipf_frag_deref(softc->ipf_frag_soft, frp);
1225 #endif
1226 }
1227
1228
1229 /* ------------------------------------------------------------------------ */
1230 /* Function: ipf_frag_nat_deref */
1231 /* Returns: Nil */
1232 /* */
1233 /* ------------------------------------------------------------------------ */
1234 void
1235 ipf_frag_nat_deref(ipf_main_softc_t *softc, void *data)
1236 {
1237 ipfr_t **frp = data;
1238
1239 #ifdef USE_MUTEXES
1240 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1241
1242 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_natfrag);
1243 #else
1244 ipf_frag_deref(softc->ipf_frag_soft, frp);
1245 #endif
1246 }
1247
1248
1249 /* ------------------------------------------------------------------------ */
1250 /* Function: ipf_frag_deref */
1251 /* Returns: Nil */
1252 /* Parameters: frp(IO) - pointer to fragment structure to deference */
1253 /* lock(I) - lock associated with the fragment */
1254 /* */
1255 /* This function dereferences a fragment structure (ipfr_t). The pointer */
1256 /* passed in will always be reset back to NULL, even if the structure is */
1257 /* not freed, to enforce the notion that the caller is no longer entitled */
1258 /* to use the pointer it is dropping the reference to. */
1259 /* ------------------------------------------------------------------------ */
1260 static void
1261 ipf_frag_deref(void *arg, ipfr_t **frp
1262 #ifdef USE_MUTEXES
1263 , ipfrwlock_t *lock
1264 #endif
1265 )
1266 {
1267 ipf_frag_softc_t *softf = arg;
1268 ipfr_t *fra;
1269
1270 fra = *frp;
1271 *frp = NULL;
1272
1273 WRITE_ENTER(lock);
1274 fra->ipfr_ref--;
1275 if (fra->ipfr_ref <= 0)
1276 ipf_frag_free(softf, fra);
1277 RWLOCK_EXIT(lock);
1278 }
1279