Aquire serializer before calling ioctl
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_frag.c
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * @(#)ip_frag.c    1.11 3/24/96 (C) 1993-2000 Darren Reed
7  * @(#)$Id: ip_frag.c,v 2.10.2.24 2002/08/28 12:41:04 darrenr Exp $
8  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_frag.c,v 1.15.2.7 2004/07/04 09:24:38 darrenr Exp $
9  * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_frag.c,v 1.9 2005/06/05 12:17:46 corecode Exp $
10  */
11 #if defined(KERNEL) && !defined(_KERNEL)
12 # define      _KERNEL
13 #endif
14
15 #if defined(__sgi) && (IRIX > 602)
16 # include <sys/ptimers.h>
17 #endif
18 #include <sys/errno.h>
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/time.h>
22 #include <sys/file.h>
23 #if !defined(_KERNEL) && !defined(KERNEL)
24 # include <stdio.h>
25 # include <string.h>
26 # include <stdlib.h>
27 #endif
28 #if (defined(KERNEL) || defined(_KERNEL)) && (defined(__DragonFly__) || __FreeBSD_version >= 220000)
29 # include <sys/filio.h>
30 # include <sys/fcntl.h>
31 #else
32 # include <sys/ioctl.h>
33 #endif
34 #ifndef linux
35 # include <sys/protosw.h>
36 #endif
37 #include <sys/socket.h>
38 #if defined(_KERNEL) && !defined(linux)
39 # include <sys/systm.h>
40 #endif
41 #if !defined(__SVR4) && !defined(__svr4__)
42 # if defined(_KERNEL) && !defined(__sgi)
43 #  include <sys/kernel.h>
44 # endif
45 # ifndef linux
46 #  include <sys/mbuf.h>
47 # endif
48 #else
49 # include <sys/byteorder.h>
50 # ifdef _KERNEL
51 #  include <sys/dditypes.h>
52 # endif
53 # include <sys/stream.h>
54 # include <sys/kmem.h>
55 #endif
56 #if defined(__DragonFly__) && defined(_KERNEL)
57 # include <sys/thread2.h>
58 #endif
59 #include <net/if.h>
60 #ifdef sun
61 # include <net/af.h>
62 #endif
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #ifndef linux
68 # include <netinet/ip_var.h>
69 #endif
70 #include <netinet/tcp.h>
71 #include <netinet/udp.h>
72 #include <netinet/ip_icmp.h>
73 #include "ip_compat.h"
74 #include <netinet/tcpip.h>
75 #include "ip_fil.h"
76 #include "ip_nat.h"
77 #include "ip_frag.h"
78 #include "ip_state.h"
79 #include "ip_auth.h"
80 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
81 # include <sys/malloc.h>
82 # if (defined(KERNEL) || defined(_KERNEL))
83 #  ifndef IPFILTER_LKM
84 #   include <sys/libkern.h>
85 #   include <sys/systm.h>
86 #  endif
87 extern struct callout ipfr_slowtimer_ch;
88 # endif
89 #endif
90 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
91 # include <sys/callout.h>
92 extern struct callout ipfr_slowtimer_ch;
93 #endif
94 #if defined(__OpenBSD__)
95 # include <sys/timeout.h>
96 extern struct timeout ipfr_slowtimer_ch;
97 #endif
98
99 static const char sccsid[] = "@(#)ip_frag.c    1.11 3/24/96 (C) 1993-2000 Darren Reed";
100
101 static ipfr_t   *ipfr_heads[IPFT_SIZE];
102 static ipfr_t   *ipfr_nattab[IPFT_SIZE];
103 static ipfrstat_t ipfr_stats;
104 static int      ipfr_inuse = 0;
105
106 int     fr_ipfrttl = 120;       /* 60 seconds */
107 int     fr_frag_lock = 0;
108
109 #ifdef _KERNEL
110 # if SOLARIS2 >= 7
111 extern  timeout_id_t    ipfr_timer_id;
112 # else
113 extern  int     ipfr_timer_id;
114 # endif
115 #endif
116 #if     (SOLARIS || defined(__sgi)) && defined(_KERNEL)
117 extern  KRWLOCK_T       ipf_frag, ipf_natfrag, ipf_nat, ipf_mutex;
118 # if    SOLARIS
119 extern  KRWLOCK_T       ipf_solaris;
120 # else
121 KRWLOCK_T       ipf_solaris;
122 # endif
123 extern  kmutex_t        ipf_rw;
124 #endif
125
126
127 static ipfr_t *ipfr_new (ip_t *, fr_info_t *, ipfr_t **);
128 static ipfr_t *ipfr_lookup (ip_t *, fr_info_t *, ipfr_t **);
129 static void ipfr_delete (ipfr_t *);
130
131
132 ipfrstat_t *ipfr_fragstats()
133 {
134         ipfr_stats.ifs_table = ipfr_heads;
135         ipfr_stats.ifs_nattab = ipfr_nattab;
136         ipfr_stats.ifs_inuse = ipfr_inuse;
137         return &ipfr_stats;
138 }
139
140
141 /*
142  * add a new entry to the fragment cache, registering it as having come
143  * through this box, with the result of the filter operation.
144  */
145 static ipfr_t *ipfr_new(ip, fin, table)
146 ip_t *ip;
147 fr_info_t *fin;
148 ipfr_t *table[];
149 {
150         ipfr_t **fp, *fra, frag;
151         u_int idx, off;
152
153         if (ipfr_inuse >= IPFT_SIZE)
154                 return NULL;
155
156         if (!(fin->fin_fi.fi_fl & FI_FRAG))
157                 return NULL;
158
159         frag.ipfr_p = ip->ip_p;
160         idx = ip->ip_p;
161         frag.ipfr_id = ip->ip_id;
162         idx += ip->ip_id;
163         frag.ipfr_tos = ip->ip_tos;
164         frag.ipfr_src.s_addr = ip->ip_src.s_addr;
165         idx += ip->ip_src.s_addr;
166         frag.ipfr_dst.s_addr = ip->ip_dst.s_addr;
167         idx += ip->ip_dst.s_addr;
168         frag.ipfr_ifp = fin->fin_ifp;
169         idx *= 127;
170         idx %= IPFT_SIZE;
171
172         frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
173         frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
174         frag.ipfr_auth = fin->fin_fi.fi_auth;
175
176         /*
177          * first, make sure it isn't already there...
178          */
179         for (fp = &table[idx]; (fra = *fp); fp = &fra->ipfr_next)
180                 if (!bcmp((char *)&frag.ipfr_src, (char *)&fra->ipfr_src,
181                           IPFR_CMPSZ)) {
182                         ATOMIC_INCL(ipfr_stats.ifs_exists);
183                         return NULL;
184                 }
185
186         /*
187          * allocate some memory, if possible, if not, just record that we
188          * failed to do so.
189          */
190         KMALLOC(fra, ipfr_t *);
191         if (fra == NULL) {
192                 ATOMIC_INCL(ipfr_stats.ifs_nomem);
193                 return NULL;
194         }
195
196         if ((fra->ipfr_rule = fin->fin_fr) != NULL) {
197                 ATOMIC_INC32(fin->fin_fr->fr_ref);
198         }
199
200
201         /*
202          * Insert the fragment into the fragment table, copy the struct used
203          * in the search using bcopy rather than reassign each field.
204          * Set the ttl to the default.
205          */
206         if ((fra->ipfr_next = table[idx]))
207                 table[idx]->ipfr_prev = fra;
208         fra->ipfr_prev = NULL;
209         fra->ipfr_data = NULL;
210         table[idx] = fra;
211         bcopy((char *)&frag.ipfr_src, (char *)&fra->ipfr_src, IPFR_CMPSZ);
212         fra->ipfr_ttl = fr_ipfrttl;
213         /*
214          * Compute the offset of the expected start of the next packet.
215          */
216         off = ip->ip_off & IP_OFFMASK;
217         if (!off)
218                 fra->ipfr_seen0 = 1;
219         fra->ipfr_off = off + (fin->fin_dlen >> 3);
220         ATOMIC_INCL(ipfr_stats.ifs_new);
221         ATOMIC_INC32(ipfr_inuse);
222         return fra;
223 }
224
225
226 int ipfr_newfrag(ip, fin)
227 ip_t *ip;
228 fr_info_t *fin;
229 {
230         ipfr_t  *ipf;
231
232         if ((ip->ip_v != 4) || (fr_frag_lock))
233                 return -1;
234         WRITE_ENTER(&ipf_frag);
235         ipf = ipfr_new(ip, fin, ipfr_heads);
236         RWLOCK_EXIT(&ipf_frag);
237         if (ipf == NULL) {
238                 ATOMIC_INCL(frstats[fin->fin_out].fr_bnfr);
239                 return -1;
240         }
241         ATOMIC_INCL(frstats[fin->fin_out].fr_nfr);
242         return 0;
243 }
244
245
246 int ipfr_nat_newfrag(ip, fin, nat)
247 ip_t *ip;
248 fr_info_t *fin;
249 nat_t *nat;
250 {
251         ipfr_t  *ipf;
252         int off;
253
254         if ((ip->ip_v != 4) || (fr_frag_lock))
255                 return -1;
256
257         off = fin->fin_off;
258         off <<= 3;
259         if ((off + fin->fin_dlen) > 0xffff || (fin->fin_dlen == 0))
260                 return -1;
261
262         WRITE_ENTER(&ipf_natfrag);
263         ipf = ipfr_new(ip, fin, ipfr_nattab);
264         if (ipf != NULL) {
265                 ipf->ipfr_data = nat;
266                 nat->nat_data = ipf;
267         }
268         RWLOCK_EXIT(&ipf_natfrag);
269         return ipf ? 0 : -1;
270 }
271
272
273 /*
274  * check the fragment cache to see if there is already a record of this packet
275  * with its filter result known.
276  */
277 static ipfr_t *ipfr_lookup(ip, fin, table)
278 ip_t *ip;
279 fr_info_t *fin;
280 ipfr_t *table[];
281 {
282         ipfr_t  *f, frag;
283         u_int   idx;
284
285         /*
286          * For fragments, we record protocol, packet id, TOS and both IP#'s
287          * (these should all be the same for all fragments of a packet).
288          *
289          * build up a hash value to index the table with.
290          */
291         frag.ipfr_p = ip->ip_p;
292         idx = ip->ip_p;
293         frag.ipfr_id = ip->ip_id;
294         idx += ip->ip_id;
295         frag.ipfr_tos = ip->ip_tos;
296         frag.ipfr_src.s_addr = ip->ip_src.s_addr;
297         idx += ip->ip_src.s_addr;
298         frag.ipfr_dst.s_addr = ip->ip_dst.s_addr;
299         idx += ip->ip_dst.s_addr;
300         frag.ipfr_ifp = fin->fin_ifp;
301         idx *= 127;
302         idx %= IPFT_SIZE;
303
304         frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
305         frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
306         frag.ipfr_auth = fin->fin_fi.fi_auth;
307
308         /*
309          * check the table, careful to only compare the right amount of data
310          */
311         for (f = table[idx]; f; f = f->ipfr_next)
312                 if (!bcmp((char *)&frag.ipfr_src, (char *)&f->ipfr_src,
313                           IPFR_CMPSZ)) {
314                         u_short atoff, off;
315
316                         off = fin->fin_off;
317
318                         /*
319                          * XXX - We really need to be guarding against the
320                          * retransmission of (src,dst,id,offset-range) here
321                          * because a fragmented packet is never resent with
322                          * the same IP ID#.
323                          */
324                         if (f->ipfr_seen0) {
325                                 if (!off || (fin->fin_fl & FI_SHORT))
326                                         continue;
327                         } else if (!off)
328                                 f->ipfr_seen0 = 1;
329
330                         if (f != table[idx]) {
331                                 /*
332                                  * move fragment info. to the top of the list
333                                  * to speed up searches.
334                                  */
335                                 if ((f->ipfr_prev->ipfr_next = f->ipfr_next))
336                                         f->ipfr_next->ipfr_prev = f->ipfr_prev;
337                                 f->ipfr_next = table[idx];
338                                 table[idx]->ipfr_prev = f;
339                                 f->ipfr_prev = NULL;
340                                 table[idx] = f;
341                         }
342                         atoff = off + (fin->fin_dlen >> 3);
343                         /*
344                          * If we've follwed the fragments, and this is the
345                          * last (in order), shrink expiration time.
346                          */
347                         if (off == f->ipfr_off) {
348                                 if (!(ip->ip_off & IP_MF))
349                                         f->ipfr_ttl = 1;
350                                 else
351                                         f->ipfr_off = atoff;
352                         }
353                         ATOMIC_INCL(ipfr_stats.ifs_hits);
354                         return f;
355                 }
356         return NULL;
357 }
358
359
360 /*
361  * functional interface for NAT lookups of the NAT fragment cache
362  */
363 nat_t *ipfr_nat_knownfrag(ip, fin)
364 ip_t *ip;
365 fr_info_t *fin;
366 {
367         ipfr_t *ipf;
368         nat_t *nat;
369         int off;
370
371         if ((fin->fin_v != 4) || (fr_frag_lock))
372                 return NULL;
373
374         off = fin->fin_off;
375         off <<= 3;
376         if ((off + fin->fin_dlen) > 0xffff || (fin->fin_dlen == 0))
377                 return NULL;
378
379         READ_ENTER(&ipf_natfrag);
380         ipf = ipfr_lookup(ip, fin, ipfr_nattab);
381         if (ipf != NULL) {
382                 nat = ipf->ipfr_data;
383                 /*
384                  * This is the last fragment for this packet.
385                  */
386                 if ((ipf->ipfr_ttl == 1) && (nat != NULL)) {
387                         nat->nat_data = NULL;
388                         ipf->ipfr_data = NULL;
389                 }
390         } else
391                 nat = NULL;
392         RWLOCK_EXIT(&ipf_natfrag);
393         return nat;
394 }
395
396
397 /*
398  * functional interface for normal lookups of the fragment cache
399  */
400 frentry_t *ipfr_knownfrag(ip, fin)
401 ip_t *ip;
402 fr_info_t *fin;
403 {
404         frentry_t *fr;
405         ipfr_t *fra;
406         int off;
407
408         if ((fin->fin_v != 4) || (fr_frag_lock))
409                 return NULL;
410
411         off = fin->fin_off;
412         off <<= 3;
413         if ((off + fin->fin_dlen) > 0xffff || (fin->fin_dlen == 0))
414                 return NULL;
415
416         READ_ENTER(&ipf_frag);
417         fra = ipfr_lookup(ip, fin, ipfr_heads);
418         if (fra != NULL)
419                 fr = fra->ipfr_rule;
420         else
421                 fr = NULL;
422         RWLOCK_EXIT(&ipf_frag);
423         return fr;
424 }
425
426
427 /*
428  * forget any references to this external object.
429  */
430 void ipfr_forget(ptr)
431 void *ptr;
432 {
433         ipfr_t  *fr;
434         int     idx;
435
436         WRITE_ENTER(&ipf_frag);
437         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
438                 for (fr = ipfr_heads[idx]; fr; fr = fr->ipfr_next)
439                         if (fr->ipfr_data == ptr)
440                                 fr->ipfr_data = NULL;
441
442         RWLOCK_EXIT(&ipf_frag);
443 }
444
445
446 /*
447  * forget any references to this external object.
448  */
449 void ipfr_forgetnat(nat)
450 void *nat;
451 {
452         ipfr_t  *fr;
453         int     idx;
454
455         WRITE_ENTER(&ipf_natfrag);
456         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
457                 for (fr = ipfr_nattab[idx]; fr; fr = fr->ipfr_next)
458                         if (fr->ipfr_data == nat)
459                                 fr->ipfr_data = NULL;
460
461         RWLOCK_EXIT(&ipf_natfrag);
462 }
463
464
465 static void ipfr_delete(fra)
466 ipfr_t *fra;
467 {
468         frentry_t *fr;
469
470         fr = fra->ipfr_rule;
471         if (fr != NULL) {
472                 ATOMIC_DEC32(fr->fr_ref);
473                 if (fr->fr_ref == 0)
474                         KFREE(fr);
475         }
476         if (fra->ipfr_prev)
477                 fra->ipfr_prev->ipfr_next = fra->ipfr_next;
478         if (fra->ipfr_next)
479                 fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
480         KFREE(fra);
481 }
482
483
484 /*
485  * Free memory in use by fragment state info. kept.
486  */
487 void ipfr_unload()
488 {
489         ipfr_t  **fp, *fra;
490         nat_t   *nat;
491         int     idx;
492
493         WRITE_ENTER(&ipf_frag);
494         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
495                 for (fp = &ipfr_heads[idx]; (fra = *fp); ) {
496                         *fp = fra->ipfr_next;
497                         ipfr_delete(fra);
498                 }
499         RWLOCK_EXIT(&ipf_frag);
500
501         WRITE_ENTER(&ipf_nat);
502         WRITE_ENTER(&ipf_natfrag);
503         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
504                 for (fp = &ipfr_nattab[idx]; (fra = *fp); ) {
505                         *fp = fra->ipfr_next;
506                         nat = fra->ipfr_data;
507                         if (nat != NULL) {
508                                 if (nat->nat_data == fra)
509                                         nat->nat_data = NULL;
510                         }
511                         ipfr_delete(fra);
512                 }
513         RWLOCK_EXIT(&ipf_natfrag);
514         RWLOCK_EXIT(&ipf_nat);
515 }
516
517
518 void ipfr_fragexpire()
519 {
520         ipfr_t  **fp, *fra;
521         nat_t   *nat;
522         int     idx;
523 #if defined(_KERNEL)
524 # if !SOLARIS && !defined(__DragonFly__)
525         int     s;
526 # endif
527 #endif
528
529         if (fr_frag_lock)
530                 return;
531
532         SPL_NET(s);
533         WRITE_ENTER(&ipf_frag);
534
535         /*
536          * Go through the entire table, looking for entries to expire,
537          * decreasing the ttl by one for each entry.  If it reaches 0,
538          * remove it from the chain and free it.
539          */
540         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
541                 for (fp = &ipfr_heads[idx]; (fra = *fp); ) {
542                         --fra->ipfr_ttl;
543                         if (fra->ipfr_ttl == 0) {
544                                 *fp = fra->ipfr_next;
545                                 ipfr_delete(fra);
546                                 ATOMIC_INCL(ipfr_stats.ifs_expire);
547                                 ATOMIC_DEC32(ipfr_inuse);
548                         } else
549                                 fp = &fra->ipfr_next;
550                 }
551         RWLOCK_EXIT(&ipf_frag);
552
553         /*
554          * Same again for the NAT table, except that if the structure also
555          * still points to a NAT structure, and the NAT structure points back
556          * at the one to be free'd, NULL the reference from the NAT struct.
557          * NOTE: We need to grab both mutex's early, and in this order so as
558          * to prevent a deadlock if both try to expire at the same time.
559          */
560         WRITE_ENTER(&ipf_nat);
561         WRITE_ENTER(&ipf_natfrag);
562         for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
563                 for (fp = &ipfr_nattab[idx]; (fra = *fp); ) {
564                         --fra->ipfr_ttl;
565                         if (fra->ipfr_ttl == 0) {
566                                 ATOMIC_INCL(ipfr_stats.ifs_expire);
567                                 ATOMIC_DEC32(ipfr_inuse);
568                                 nat = fra->ipfr_data;
569                                 if (nat != NULL) {
570                                         if (nat->nat_data == fra)
571                                                 nat->nat_data = NULL;
572                                 }
573                                 *fp = fra->ipfr_next;
574                                 ipfr_delete(fra);
575                         } else
576                                 fp = &fra->ipfr_next;
577                 }
578         RWLOCK_EXIT(&ipf_natfrag);
579         RWLOCK_EXIT(&ipf_nat);
580         SPL_X(s);
581 }
582
583
584 /*
585  * Slowly expire held state for fragments.  Timeouts are set * in expectation
586  * of this being called twice per second.
587  */
588 #ifdef _KERNEL
589 # if (BSD >= 199306) || SOLARIS || defined(__sgi)
590 #  if defined(SOLARIS2) && (SOLARIS2 < 7)
591 void ipfr_slowtimer()
592 #  else
593 void ipfr_slowtimer (void *ptr)
594 #  endif
595 # else
596 int ipfr_slowtimer()
597 # endif
598 #else
599 void ipfr_slowtimer()
600 #endif
601 {
602 #if defined(_KERNEL) && SOLARIS
603         extern  int     fr_running;
604
605         if (fr_running <= 0) 
606                 return;
607         READ_ENTER(&ipf_solaris);
608 #endif
609
610 #if defined(__sgi) && defined(_KERNEL)
611         ipfilter_sgi_intfsync();
612 #endif
613
614         ipfr_fragexpire();
615         fr_timeoutstate();
616         ip_natexpire();
617         fr_authexpire();
618 #if defined(_KERNEL)
619 # if    SOLARIS
620         ipfr_timer_id = timeout(ipfr_slowtimer, NULL, drv_usectohz(500000));
621         RWLOCK_EXIT(&ipf_solaris);
622 # else
623 #  if defined(__NetBSD__) && (__NetBSD_Version__ >= 104240000)
624         callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
625 #  else
626 #   if defined(__DragonFly__) || __FreeBSD_version >= 300000
627         callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
628 #   else
629 #    if defined(__OpenBSD__)
630         timeout_add(&ipfr_slowtimer_ch, hz/2);
631 #    else
632         timeout(ipfr_slowtimer, NULL, hz/2);
633 #    endif
634 #   endif
635 #   if (BSD < 199306) && !defined(__sgi)
636         return 0;
637 #   endif /* FreeBSD */
638 #  endif /* NetBSD */
639 # endif /* SOLARIS */
640 #endif /* defined(_KERNEL) */
641 }