4 * Copyright (C) 1995-1998 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
18 #if !defined(_KERNEL) && !defined(__KERNEL__)
31 # include <sys/systm.h>
32 # if !defined(__SVR4) && !defined(__svr4__)
33 # include <sys/mbuf.h>
35 # include <sys/select.h>
36 # if __FreeBSD_version >= 500000
37 # include <sys/selinfo.h>
40 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
41 # include <sys/proc.h>
43 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
44 # include <sys/filio.h>
45 # include <sys/fcntl.h>
46 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
47 # include "opt_ipfilter.h"
50 # include <sys/ioctl.h>
54 # include <sys/protosw.h>
56 #include <sys/socket.h>
57 #if defined(__SVR4) || defined(__svr4__)
58 # include <sys/filio.h>
59 # include <sys/byteorder.h>
61 # include <sys/dditypes.h>
63 # include <sys/stream.h>
64 # include <sys/kmem.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/tcp.h>
76 # include <netinet/ip_var.h>
78 #if !defined(__hpux) && !defined(linux)
79 # include <netinet/tcp_fsm.h>
81 #include <netinet/udp.h>
82 #include <netinet/ip_icmp.h>
83 #include "netinet/ip_compat.h"
84 #include <netinet/tcpip.h>
85 #include "netinet/ip_fil.h"
86 #include "netinet/ip_nat.h"
87 #include "netinet/ip_frag.h"
88 #include "netinet/ip_state.h"
89 #include "netinet/ip_proxy.h"
90 #include "netinet/ip_sync.h"
92 #include <netinet/icmp6.h>
94 #if (__FreeBSD_version >= 300000)
95 # include <sys/malloc.h>
96 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
97 # include <sys/libkern.h>
98 # include <sys/systm.h>
101 /* END OF INCLUDES */
104 #if defined(__NetBSD__)
105 #include <sys/cdefs.h>
106 __KERNEL_RCSID(0, "$NetBSD$");
108 static const char rcsid
[] = "@(#)Id: ip_sync.c,v 2.40.2.16 2009/01/27 08:33:23 darrenr Exp";
112 #define SYNC_STATETABSZ 256
113 #define SYNC_NATTABSZ 256
116 # if SOLARIS && defined(_KERNEL)
117 extern struct pollhead iplpollhead
[IPL_LOGSIZE
];
120 ipfmutex_t ipf_syncadd
, ipsl_mutex
;
121 ipfrwlock_t ipf_syncstate
, ipf_syncnat
;
122 #if SOLARIS && defined(_KERNEL)
125 synclist_t
*syncstatetab
[SYNC_STATETABSZ
];
126 synclist_t
*syncnattab
[SYNC_NATTABSZ
];
127 synclogent_t synclog
[SYNCLOG_SZ
];
128 syncupdent_t syncupd
[SYNCLOG_SZ
];
129 u_int ipf_syncnum
= 1;
130 u_int ipf_syncwrap
= 0;
131 u_int sl_idx
= 0, /* next available sync log entry */
132 su_idx
= 0, /* next available sync update entry */
133 sl_tail
= 0, /* next sync log entry to read */
134 su_tail
= 0; /* next sync update entry to read */
135 int ipf_sync_debug
= 0;
138 # if !defined(sparc) && !defined(__hppa)
139 void ipfsync_tcporder
__P((int, struct tcpdata
*));
140 void ipfsync_natorder
__P((int, struct nat
*));
141 void ipfsync_storder
__P((int, struct ipstate
*));
145 /* ------------------------------------------------------------------------ */
146 /* Function: ipfsync_init */
147 /* Returns: int - 0 == success, -1 == failure */
148 /* Parameters: Nil */
150 /* Initialise all of the locks required for the sync code and initialise */
151 /* any data structures, as required. */
152 /* ------------------------------------------------------------------------ */
155 RWLOCK_INIT(&ipf_syncstate
, "add things to state sync table");
156 RWLOCK_INIT(&ipf_syncnat
, "add things to nat sync table");
157 MUTEX_INIT(&ipf_syncadd
, "add things to sync table");
158 MUTEX_INIT(&ipsl_mutex
, "add things to sync table");
159 # if SOLARIS && defined(_KERNEL)
160 cv_init(&ipslwait
, "ipsl condvar", CV_DRIVER
, NULL
);
163 bzero((char *)syncnattab
, sizeof(syncnattab
));
164 bzero((char *)syncstatetab
, sizeof(syncstatetab
));
170 # if !defined(sparc) && !defined(__hppa)
171 /* ------------------------------------------------------------------------ */
172 /* Function: ipfsync_tcporder */
174 /* Parameters: way(I) - direction of byte order conversion. */
175 /* td(IO) - pointer to data to be converted. */
177 /* Do byte swapping on values in the TCP state information structure that */
178 /* need to be used at both ends by the host in their native byte order. */
179 /* ------------------------------------------------------------------------ */
180 void ipfsync_tcporder(way
, td
)
185 td
->td_maxwin
= htons(td
->td_maxwin
);
186 td
->td_end
= htonl(td
->td_end
);
187 td
->td_maxend
= htonl(td
->td_maxend
);
189 td
->td_maxwin
= ntohs(td
->td_maxwin
);
190 td
->td_end
= ntohl(td
->td_end
);
191 td
->td_maxend
= ntohl(td
->td_maxend
);
196 /* ------------------------------------------------------------------------ */
197 /* Function: ipfsync_natorder */
199 /* Parameters: way(I) - direction of byte order conversion. */
200 /* nat(IO) - pointer to data to be converted. */
202 /* Do byte swapping on values in the NAT data structure that need to be */
203 /* used at both ends by the host in their native byte order. */
204 /* ------------------------------------------------------------------------ */
205 void ipfsync_natorder(way
, n
)
210 n
->nat_age
= htonl(n
->nat_age
);
211 n
->nat_flags
= htonl(n
->nat_flags
);
212 n
->nat_ipsumd
= htonl(n
->nat_ipsumd
);
213 n
->nat_use
= htonl(n
->nat_use
);
214 n
->nat_dir
= htonl(n
->nat_dir
);
216 n
->nat_age
= ntohl(n
->nat_age
);
217 n
->nat_flags
= ntohl(n
->nat_flags
);
218 n
->nat_ipsumd
= ntohl(n
->nat_ipsumd
);
219 n
->nat_use
= ntohl(n
->nat_use
);
220 n
->nat_dir
= ntohl(n
->nat_dir
);
225 /* ------------------------------------------------------------------------ */
226 /* Function: ipfsync_storder */
228 /* Parameters: way(I) - direction of byte order conversion. */
229 /* ips(IO) - pointer to data to be converted. */
231 /* Do byte swapping on values in the IP state data structure that need to */
232 /* be used at both ends by the host in their native byte order. */
233 /* ------------------------------------------------------------------------ */
234 void ipfsync_storder(way
, ips
)
238 ipfsync_tcporder(way
, &ips
->is_tcp
.ts_data
[0]);
239 ipfsync_tcporder(way
, &ips
->is_tcp
.ts_data
[1]);
242 ips
->is_hv
= htonl(ips
->is_hv
);
243 ips
->is_die
= htonl(ips
->is_die
);
244 ips
->is_pass
= htonl(ips
->is_pass
);
245 ips
->is_flags
= htonl(ips
->is_flags
);
246 ips
->is_opt
[0] = htonl(ips
->is_opt
[0]);
247 ips
->is_opt
[1] = htonl(ips
->is_opt
[1]);
248 ips
->is_optmsk
[0] = htonl(ips
->is_optmsk
[0]);
249 ips
->is_optmsk
[1] = htonl(ips
->is_optmsk
[1]);
250 ips
->is_sec
= htons(ips
->is_sec
);
251 ips
->is_secmsk
= htons(ips
->is_secmsk
);
252 ips
->is_auth
= htons(ips
->is_auth
);
253 ips
->is_authmsk
= htons(ips
->is_authmsk
);
254 ips
->is_s0
[0] = htonl(ips
->is_s0
[0]);
255 ips
->is_s0
[1] = htonl(ips
->is_s0
[1]);
256 ips
->is_smsk
[0] = htons(ips
->is_smsk
[0]);
257 ips
->is_smsk
[1] = htons(ips
->is_smsk
[1]);
259 ips
->is_hv
= ntohl(ips
->is_hv
);
260 ips
->is_die
= ntohl(ips
->is_die
);
261 ips
->is_pass
= ntohl(ips
->is_pass
);
262 ips
->is_flags
= ntohl(ips
->is_flags
);
263 ips
->is_opt
[0] = ntohl(ips
->is_opt
[0]);
264 ips
->is_opt
[1] = ntohl(ips
->is_opt
[1]);
265 ips
->is_optmsk
[0] = ntohl(ips
->is_optmsk
[0]);
266 ips
->is_optmsk
[1] = ntohl(ips
->is_optmsk
[1]);
267 ips
->is_sec
= ntohs(ips
->is_sec
);
268 ips
->is_secmsk
= ntohs(ips
->is_secmsk
);
269 ips
->is_auth
= ntohs(ips
->is_auth
);
270 ips
->is_authmsk
= ntohs(ips
->is_authmsk
);
271 ips
->is_s0
[0] = ntohl(ips
->is_s0
[0]);
272 ips
->is_s0
[1] = ntohl(ips
->is_s0
[1]);
273 ips
->is_smsk
[0] = ntohl(ips
->is_smsk
[0]);
274 ips
->is_smsk
[1] = ntohl(ips
->is_smsk
[1]);
277 # else /* !defined(sparc) && !defined(__hppa) */
278 # define ipfsync_tcporder(x,y)
279 # define ipfsync_natorder(x,y)
280 # define ipfsync_storder(x,y)
281 # endif /* !defined(sparc) && !defined(__hppa) */
283 /* enable this for debugging */
286 /* ------------------------------------------------------------------------ */
287 /* Function: ipfsync_write */
288 /* Returns: int - 0 == success, else error value. */
289 /* Parameters: uio(I) - pointer to information about data to write */
291 /* Moves data from user space into the kernel and uses it for updating data */
292 /* structures in the state/NAT tables. */
293 /* ------------------------------------------------------------------------ */
294 int ipfsync_write(uio
)
300 * THIS MUST BE SUFFICIENT LARGE TO STORE
301 * ANY POSSIBLE DATA TYPE
307 # if (BSD >= 199306) || defined(__FreeBSD__) || defined(__osf__)
308 uio
->uio_rw
= UIO_WRITE
;
311 /* Try to get bytes */
312 while (uio
->uio_resid
> 0) {
314 if (uio
->uio_resid
>= sizeof(sh
)) {
316 err
= UIOMOVE((void *)&sh
, sizeof(sh
), UIO_WRITE
, uio
);
319 if (ipf_sync_debug
> 2)
320 printf("uiomove(header) failed: %d\n",
325 /* convert to host order */
326 sh
.sm_magic
= ntohl(sh
.sm_magic
);
327 sh
.sm_len
= ntohl(sh
.sm_len
);
328 sh
.sm_num
= ntohl(sh
.sm_num
);
330 if (ipf_sync_debug
> 8)
331 printf("[%d] Read v:%d p:%d cmd:%d table:%d rev:%d len:%d magic:%x\n",
332 sh
.sm_num
, sh
.sm_v
, sh
.sm_p
, sh
.sm_cmd
,
333 sh
.sm_table
, sh
.sm_rev
, sh
.sm_len
,
336 if (sh
.sm_magic
!= SYNHDRMAGIC
) {
337 if (ipf_sync_debug
> 2)
338 printf("uiomove(header) invalud %s\n",
343 if (sh
.sm_v
!= 4 && sh
.sm_v
!= 6) {
344 if (ipf_sync_debug
> 2)
345 printf("uiomove(header) invalid %s\n",
350 if (sh
.sm_cmd
> SMC_MAXCMD
) {
351 if (ipf_sync_debug
> 2)
352 printf("uiomove(header) invalid %s\n",
358 if (sh
.sm_table
> SMC_MAXTBL
) {
359 if (ipf_sync_debug
> 2)
360 printf("uiomove(header) invalid %s\n",
366 /* unsufficient data, wait until next call */
367 if (ipf_sync_debug
> 2)
368 printf("uiomove(header) insufficient data");
374 * We have a header, so try to read the amount of data
375 * needed for the request
379 if (sh
.sm_len
== 0) {
380 if (ipf_sync_debug
> 2)
381 printf("uiomove(data zero length %s\n",
386 if (uio
->uio_resid
>= sh
.sm_len
) {
388 err
= UIOMOVE((void *)data
, sh
.sm_len
, UIO_WRITE
, uio
);
391 if (ipf_sync_debug
> 2)
392 printf("uiomove(data) failed: %d\n",
397 if (ipf_sync_debug
> 7)
398 printf("uiomove(data) %d bytes read\n",
401 if (sh
.sm_table
== SMC_STATE
)
402 err
= ipfsync_state(&sh
, data
);
403 else if (sh
.sm_table
== SMC_NAT
)
404 err
= ipfsync_nat(&sh
, data
);
405 if (ipf_sync_debug
> 7)
406 printf("[%d] Finished with error %d\n",
410 /* insufficient data, wait until next call */
411 if (ipf_sync_debug
> 2)
412 printf("uiomove(data) %s %d bytes, got %d\n",
413 "insufficient data, need",
414 sh
.sm_len
, uio
->uio_resid
);
424 /* ------------------------------------------------------------------------ */
425 /* Function: ipfsync_read */
426 /* Returns: int - 0 == success, else error value. */
427 /* Parameters: uio(O) - pointer to information about where to store data */
429 /* This function is called when a user program wants to read some data */
430 /* for pending state/NAT updates. If no data is available, the caller is */
431 /* put to sleep, pending a wakeup from the "lower half" of this code. */
432 /* ------------------------------------------------------------------------ */
433 int ipfsync_read(uio
)
440 if ((uio
->uio_resid
& 3) || (uio
->uio_resid
< 8))
443 # if (BSD >= 199306) || defined(__FreeBSD__) || defined(__osf__)
444 uio
->uio_rw
= UIO_READ
;
447 MUTEX_ENTER(&ipsl_mutex
);
448 while ((sl_tail
== sl_idx
) && (su_tail
== su_idx
)) {
449 # if SOLARIS && defined(_KERNEL)
450 if (!cv_wait_sig(&ipslwait
, &ipsl_mutex
)) {
451 MUTEX_EXIT(&ipsl_mutex
);
459 l
= get_sleep_lock(&sl_tail
);
460 err
= sleep(&sl_tail
, PZERO
+1);
462 MUTEX_EXIT(&ipsl_mutex
);
469 err
= mpsleep(&sl_tail
, PSUSP
|PCATCH
, "ipl sleep", 0,
470 &ipsl_mutex
, MS_LOCK_SIMPLE
);
474 MUTEX_EXIT(&ipsl_mutex
);
475 err
= SLEEP(&sl_tail
, "ipl sleep");
478 MUTEX_ENTER(&ipsl_mutex
);
479 # endif /* __osf__ */
481 # endif /* SOLARIS */
483 MUTEX_EXIT(&ipsl_mutex
);
485 READ_ENTER(&ipf_syncstate
);
486 while ((sl_tail
< sl_idx
) && (uio
->uio_resid
> sizeof(*sl
))) {
487 sl
= synclog
+ sl_tail
++;
488 err
= UIOMOVE((void *)sl
, sizeof(*sl
), UIO_READ
, uio
);
493 while ((su_tail
< su_idx
) && (uio
->uio_resid
> sizeof(*su
))) {
494 su
= syncupd
+ su_tail
;
496 err
= UIOMOVE((void *)su
, sizeof(*su
), UIO_READ
, uio
);
499 if (su
->sup_hdr
.sm_sl
!= NULL
)
500 su
->sup_hdr
.sm_sl
->sl_idx
= -1;
503 MUTEX_ENTER(&ipf_syncadd
);
504 if (su_tail
== su_idx
)
505 su_tail
= su_idx
= 0;
506 if (sl_tail
== sl_idx
)
507 sl_tail
= sl_idx
= 0;
508 MUTEX_EXIT(&ipf_syncadd
);
509 RWLOCK_EXIT(&ipf_syncstate
);
514 /* ------------------------------------------------------------------------ */
515 /* Function: ipfsync_state */
516 /* Returns: int - 0 == success, else error value. */
517 /* Parameters: sp(I) - pointer to sync packet data header */
518 /* uio(I) - pointer to user data for further information */
520 /* Updates the state table according to information passed in the sync */
521 /* header. As required, more data is fetched from the uio structure but */
522 /* varies depending on the contents of the sync header. This function can */
523 /* create a new state entry or update one. Deletion is left to the state */
524 /* structures being timed out correctly. */
525 /* ------------------------------------------------------------------------ */
526 int ipfsync_state(sp
, data
)
537 hv
= sp
->sm_num
& (SYNC_STATETABSZ
- 1);
543 bcopy(data
, &sn
, sizeof(sn
));
544 KMALLOC(is
, ipstate_t
*);
550 KMALLOC(sl
, synclist_t
*);
557 bzero((char *)is
, offsetof(ipstate_t
, is_die
));
558 bcopy((char *)&sn
.is_die
, (char *)&is
->is_die
,
559 sizeof(*is
) - offsetof(ipstate_t
, is_die
));
560 ipfsync_storder(0, is
);
563 * We need to find the same rule on the slave as was used on
564 * the master to create this state entry.
566 READ_ENTER(&ipf_mutex
);
567 fr
= fr_getrulen(IPL_LOGIPF
, sn
.is_group
, sn
.is_rulen
);
569 MUTEX_ENTER(&fr
->fr_lock
);
572 MUTEX_EXIT(&fr
->fr_lock
);
574 RWLOCK_EXIT(&ipf_mutex
);
576 if (ipf_sync_debug
> 4)
577 printf("[%d] Filter rules = %p\n", sp
->sm_num
, fr
);
584 bcopy(sp
, &sl
->sl_hdr
, sizeof(struct synchdr
));
586 WRITE_ENTER(&ipf_syncstate
);
587 WRITE_ENTER(&ipf_state
);
589 sl
->sl_pnext
= syncstatetab
+ hv
;
590 sl
->sl_next
= syncstatetab
[hv
];
591 if (syncstatetab
[hv
] != NULL
)
592 syncstatetab
[hv
]->sl_pnext
= &sl
->sl_next
;
593 syncstatetab
[hv
] = sl
;
594 MUTEX_DOWNGRADE(&ipf_syncstate
);
595 fr_stinsert(is
, sp
->sm_rev
);
597 * Do not initialise the interface pointers for the state
598 * entry as the full complement of interface names may not
601 * Put this state entry on its timeout queue.
603 /*fr_setstatequeue(is, sp->sm_rev);*/
607 bcopy(data
, &su
, sizeof(su
));
609 if (ipf_sync_debug
> 4)
610 printf("[%d] Update age %lu state %d/%d \n",
611 sp
->sm_num
, su
.stu_age
, su
.stu_state
[0],
614 READ_ENTER(&ipf_syncstate
);
615 for (sl
= syncstatetab
[hv
]; (sl
!= NULL
); sl
= sl
->sl_next
)
616 if (sl
->sl_hdr
.sm_num
== sp
->sm_num
)
619 if (ipf_sync_debug
> 1)
620 printf("[%d] State not found - can't update\n",
622 RWLOCK_EXIT(&ipf_syncstate
);
627 READ_ENTER(&ipf_state
);
629 if (ipf_sync_debug
> 6)
630 printf("[%d] Data from state v:%d p:%d cmd:%d table:%d rev:%d\n",
631 sp
->sm_num
, sl
->sl_hdr
.sm_v
, sl
->sl_hdr
.sm_p
,
632 sl
->sl_hdr
.sm_cmd
, sl
->sl_hdr
.sm_table
,
637 MUTEX_ENTER(&is
->is_lock
);
641 /* XXX FV --- shouldn't we do ntohl/htonl???? XXX */
642 is
->is_send
= su
.stu_data
[0].td_end
;
643 is
->is_maxsend
= su
.stu_data
[0].td_maxend
;
644 is
->is_maxswin
= su
.stu_data
[0].td_maxwin
;
645 is
->is_state
[0] = su
.stu_state
[0];
646 is
->is_dend
= su
.stu_data
[1].td_end
;
647 is
->is_maxdend
= su
.stu_data
[1].td_maxend
;
648 is
->is_maxdwin
= su
.stu_data
[1].td_maxwin
;
649 is
->is_state
[1] = su
.stu_state
[1];
655 if (ipf_sync_debug
> 6)
656 printf("[%d] Setting timers for state\n", sp
->sm_num
);
658 fr_setstatequeue(is
, sp
->sm_rev
);
660 MUTEX_EXIT(&is
->is_lock
);
669 RWLOCK_EXIT(&ipf_state
);
670 RWLOCK_EXIT(&ipf_syncstate
);
673 if (ipf_sync_debug
> 6)
674 printf("[%d] Update completed with error %d\n",
679 # endif /* _KERNEL */
682 /* ------------------------------------------------------------------------ */
683 /* Function: ipfsync_del */
685 /* Parameters: sl(I) - pointer to synclist object to delete */
687 /* Deletes an object from the synclist table and free's its memory. */
688 /* ------------------------------------------------------------------------ */
692 WRITE_ENTER(&ipf_syncstate
);
693 *sl
->sl_pnext
= sl
->sl_next
;
694 if (sl
->sl_next
!= NULL
)
695 sl
->sl_next
->sl_pnext
= sl
->sl_pnext
;
696 if (sl
->sl_idx
!= -1)
697 syncupd
[sl
->sl_idx
].sup_hdr
.sm_sl
= NULL
;
698 RWLOCK_EXIT(&ipf_syncstate
);
703 /* ------------------------------------------------------------------------ */
704 /* Function: ipfsync_nat */
705 /* Returns: int - 0 == success, else error value. */
706 /* Parameters: sp(I) - pointer to sync packet data header */
707 /* uio(I) - pointer to user data for further information */
709 /* Updates the NAT table according to information passed in the sync */
710 /* header. As required, more data is fetched from the uio structure but */
711 /* varies depending on the contents of the sync header. This function can */
712 /* create a new NAT entry or update one. Deletion is left to the NAT */
713 /* structures being timed out correctly. */
714 /* ------------------------------------------------------------------------ */
715 int ipfsync_nat(sp
, data
)
725 READ_ENTER(&ipf_syncnat
);
736 KMALLOC(sl
, synclist_t
*);
744 bzero((char *)n
, offsetof(nat_t
, nat_age
));
745 bcopy((char *)&nat
->nat_age
, (char *)&n
->nat_age
,
746 sizeof(*n
) - offsetof(nat_t
, nat_age
));
747 ipfsync_natorder(0, n
);
752 sl
->sl_num
= ntohl(sp
->sm_num
);
754 WRITE_ENTER(&ipf_nat
);
755 sl
->sl_pnext
= syncnattab
+ hv
;
756 sl
->sl_next
= syncnattab
[hv
];
757 if (syncnattab
[hv
] != NULL
)
758 syncnattab
[hv
]->sl_pnext
= &sl
->sl_next
;
760 nat_insert(n
, sl
->sl_rev
);
761 RWLOCK_EXIT(&ipf_nat
);
765 bcopy(data
, &su
, sizeof(su
));
767 for (sl
= syncnattab
[hv
]; (sl
!= NULL
); sl
= sl
->sl_next
)
768 if (sl
->sl_hdr
.sm_num
== sp
->sm_num
)
775 READ_ENTER(&ipf_nat
);
779 MUTEX_ENTER(&nat
->nat_lock
);
780 fr_setnatqueue(nat
, sl
->sl_rev
);
781 MUTEX_EXIT(&nat
->nat_lock
);
783 RWLOCK_EXIT(&ipf_nat
);
792 RWLOCK_EXIT(&ipf_syncnat
);
797 /* ------------------------------------------------------------------------ */
798 /* Function: ipfsync_new */
799 /* Returns: synclist_t* - NULL == failure, else pointer to new synclist */
800 /* data structure. */
801 /* Parameters: tab(I) - type of synclist_t to create */
802 /* fin(I) - pointer to packet information */
803 /* ptr(I) - pointer to owning object */
805 /* Creates a new sync table entry and notifies any sleepers that it's there */
806 /* waiting to be processed. */
807 /* ------------------------------------------------------------------------ */
808 synclist_t
*ipfsync_new(tab
, fin
, ptr
)
817 if (sl_idx
== SYNCLOG_SZ
)
819 KMALLOC(sl
, synclist_t
*);
823 MUTEX_ENTER(&ipf_syncadd
);
825 * Get a unique number for this synclist_t. The number is only meant
826 * to be unique for the lifetime of the structure and may be reused
830 if (ipf_syncnum
== 0) {
836 * Use the synch number of the object as the hash key. Should end up
837 * with relatively even distribution over time.
838 * XXX - an attacker could lunch an DoS attack, of sorts, if they are
839 * the only one causing new table entries by only keeping open every
840 * nth connection they make, where n is a value in the interval
841 * [0, SYNC_STATETABSZ-1].
843 if (tab
== SMC_STATE
) {
844 hv
= ipf_syncnum
& (SYNC_STATETABSZ
- 1);
845 while (ipf_syncwrap
!= 0) {
846 for (ss
= syncstatetab
[hv
]; ss
; ss
= ss
->sl_next
)
847 if (ss
->sl_hdr
.sm_num
== ipf_syncnum
)
852 hv
= ipf_syncnum
& (SYNC_STATETABSZ
- 1);
854 sl
->sl_pnext
= syncstatetab
+ hv
;
855 sl
->sl_next
= syncstatetab
[hv
];
856 syncstatetab
[hv
] = sl
;
858 hv
= ipf_syncnum
& (SYNC_NATTABSZ
- 1);
859 while (ipf_syncwrap
!= 0) {
860 for (ss
= syncnattab
[hv
]; ss
; ss
= ss
->sl_next
)
861 if (ss
->sl_hdr
.sm_num
== ipf_syncnum
)
866 hv
= ipf_syncnum
& (SYNC_STATETABSZ
- 1);
868 sl
->sl_pnext
= syncnattab
+ hv
;
869 sl
->sl_next
= syncnattab
[hv
];
872 sl
->sl_num
= ipf_syncnum
;
873 MUTEX_EXIT(&ipf_syncadd
);
875 sl
->sl_magic
= htonl(SYNHDRMAGIC
);
876 sl
->sl_v
= fin
->fin_v
;
877 sl
->sl_p
= fin
->fin_p
;
878 sl
->sl_cmd
= SMC_CREATE
;
881 sl
->sl_rev
= fin
->fin_rev
;
882 if (tab
== SMC_STATE
) {
884 sz
= sizeof(*sl
->sl_ips
);
887 sz
= sizeof(*sl
->sl_ipn
);
892 * Create the log entry to be read by a user daemon. When it has been
893 * finished and put on the queue, send a signal to wakeup any waiters.
895 MUTEX_ENTER(&ipf_syncadd
);
896 sle
= synclog
+ sl_idx
++;
897 bcopy((char *)&sl
->sl_hdr
, (char *)&sle
->sle_hdr
,
898 sizeof(sle
->sle_hdr
));
899 sle
->sle_hdr
.sm_num
= htonl(sle
->sle_hdr
.sm_num
);
900 sle
->sle_hdr
.sm_len
= htonl(sle
->sle_hdr
.sm_len
);
902 bcopy((char *)ptr
, (char *)&sle
->sle_un
, sz
);
903 if (tab
== SMC_STATE
) {
904 ipfsync_storder(1, &sle
->sle_un
.sleu_ips
);
905 } else if (tab
== SMC_NAT
) {
906 ipfsync_natorder(1, &sle
->sle_un
.sleu_ipn
);
909 MUTEX_EXIT(&ipf_syncadd
);
911 MUTEX_ENTER(&ipsl_mutex
);
914 cv_signal(&ipslwait
);
915 pollwakeup(&iplpollhead
[IPL_LOGSYNC
], POLLIN
|POLLRDNORM
);
917 MUTEX_EXIT(&ipsl_mutex
);
919 MUTEX_EXIT(&ipsl_mutex
);
922 POLLWAKEUP(IPL_LOGSYNC
);
929 /* ------------------------------------------------------------------------ */
930 /* Function: ipfsync_update */
932 /* Parameters: tab(I) - type of synclist_t to create */
933 /* fin(I) - pointer to packet information */
934 /* sl(I) - pointer to synchronisation object */
936 /* For outbound packets, only, create an sync update record for the user */
937 /* process to read. */
938 /* ------------------------------------------------------------------------ */
939 void ipfsync_update(tab
, fin
, sl
)
944 synctcp_update_t
*st
;
949 if (fin
->fin_out
== 0 || sl
== NULL
)
952 WRITE_ENTER(&ipf_syncstate
);
953 MUTEX_ENTER(&ipf_syncadd
);
954 if (sl
->sl_idx
== -1) {
955 slu
= syncupd
+ su_idx
;
956 sl
->sl_idx
= su_idx
++;
957 bcopy((char *)&sl
->sl_hdr
, (char *)&slu
->sup_hdr
,
958 sizeof(slu
->sup_hdr
));
959 slu
->sup_hdr
.sm_magic
= htonl(SYNHDRMAGIC
);
960 slu
->sup_hdr
.sm_sl
= sl
;
961 slu
->sup_hdr
.sm_cmd
= SMC_UPDATE
;
962 slu
->sup_hdr
.sm_table
= tab
;
963 slu
->sup_hdr
.sm_num
= htonl(sl
->sl_num
);
964 slu
->sup_hdr
.sm_len
= htonl(sizeof(struct synctcp_update
));
965 slu
->sup_hdr
.sm_rev
= fin
->fin_rev
;
967 if (fin
->fin_p
== IPPROTO_TCP
) {
973 slu
= syncupd
+ sl
->sl_idx
;
974 MUTEX_EXIT(&ipf_syncadd
);
975 MUTEX_DOWNGRADE(&ipf_syncstate
);
978 * Only TCP has complex timeouts, others just use default timeouts.
979 * For TCP, we only need to track the connection state and window.
981 if (fin
->fin_p
== IPPROTO_TCP
) {
983 if (tab
== SMC_STATE
) {
985 st
->stu_age
= htonl(ips
->is_die
);
986 st
->stu_data
[0].td_end
= ips
->is_send
;
987 st
->stu_data
[0].td_maxend
= ips
->is_maxsend
;
988 st
->stu_data
[0].td_maxwin
= ips
->is_maxswin
;
989 st
->stu_state
[0] = ips
->is_state
[0];
990 st
->stu_data
[1].td_end
= ips
->is_dend
;
991 st
->stu_data
[1].td_maxend
= ips
->is_maxdend
;
992 st
->stu_data
[1].td_maxwin
= ips
->is_maxdwin
;
993 st
->stu_state
[1] = ips
->is_state
[1];
994 } else if (tab
== SMC_NAT
) {
996 st
->stu_age
= htonl(nat
->nat_age
);
999 RWLOCK_EXIT(&ipf_syncstate
);
1001 MUTEX_ENTER(&ipsl_mutex
);
1004 cv_signal(&ipslwait
);
1005 pollwakeup(&iplpollhead
[IPL_LOGSYNC
], POLLIN
|POLLRDNORM
);
1007 MUTEX_EXIT(&ipsl_mutex
);
1009 MUTEX_EXIT(&ipsl_mutex
);
1011 WAKEUP(&sl_tail
, 0);
1012 POLLWAKEUP(IPL_LOGSYNC
);
1018 /* ------------------------------------------------------------------------ */
1019 /* Function: fr_sync_ioctl */
1020 /* Returns: int - 0 == success, != 0 == failure */
1021 /* Parameters: data(I) - pointer to ioctl data */
1022 /* cmd(I) - ioctl command integer */
1023 /* mode(I) - file mode bits used with open */
1025 /* This function currently does not handle any ioctls and so just returns */
1026 /* EINVAL on all occasions. */
1027 /* ------------------------------------------------------------------------ */
1028 int fr_sync_ioctl(data
, cmd
, mode
, uid
, ctx
)
1038 int ipfsync_canread()
1040 return !((sl_tail
== sl_idx
) && (su_tail
== su_idx
));
1044 int ipfsync_canwrite()
1048 #endif /* IPFILTER_SYNC */