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>
36 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
37 # include <sys/proc.h>
39 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
40 # include <sys/filio.h>
41 # include <sys/fcntl.h>
42 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
43 # include "opt_ipfilter.h"
46 # include <sys/ioctl.h>
50 # include <sys/protosw.h>
52 #include <sys/socket.h>
53 #if defined(__SVR4) || defined(__svr4__)
54 # include <sys/filio.h>
55 # include <sys/byteorder.h>
57 # include <sys/dditypes.h>
59 # include <sys/stream.h>
60 # include <sys/kmem.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
73 # include <netinet/ip_var.h>
75 #if !defined(__hpux) && !defined(linux)
76 # include <netinet/tcp_fsm.h>
78 #include <netinet/udp.h>
79 #include <netinet/ip_icmp.h>
80 #include "netinet/ip_compat.h"
81 #include <netinet/tcpip.h>
82 #include "netinet/ip_fil.h"
83 #include "netinet/ip_nat.h"
84 #include "netinet/ip_frag.h"
85 #include "netinet/ip_state.h"
86 #include "netinet/ip_proxy.h"
87 #include "netinet/ip_sync.h"
89 #include <netinet/icmp6.h>
91 #if (__FreeBSD_version >= 300000)
92 # include <sys/malloc.h>
93 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
94 # include <sys/libkern.h>
95 # include <sys/systm.h>
101 static const char rcsid[] = "@(#)$Id: ip_sync.c,v 2.40.2.9 2007/06/02 21:22:28 darrenr Exp $";
104 #define SYNC_STATETABSZ 256
105 #define SYNC_NATTABSZ 256
108 ipfmutex_t ipf_syncadd, ipsl_mutex;
109 ipfrwlock_t ipf_syncstate, ipf_syncnat;
110 #if SOLARIS && defined(_KERNEL)
113 synclist_t *syncstatetab[SYNC_STATETABSZ];
114 synclist_t *syncnattab[SYNC_NATTABSZ];
115 synclogent_t synclog[SYNCLOG_SZ];
116 syncupdent_t syncupd[SYNCLOG_SZ];
117 u_int ipf_syncnum = 1;
118 u_int ipf_syncwrap = 0;
119 u_int sl_idx = 0, /* next available sync log entry */
120 su_idx = 0, /* next available sync update entry */
121 sl_tail = 0, /* next sync log entry to read */
122 su_tail = 0; /* next sync update entry to read */
123 int ipf_sync_debug = 0;
126 # if !defined(sparc) && !defined(__hppa)
127 void ipfsync_tcporder __P((int, struct tcpdata *));
128 void ipfsync_natorder __P((int, struct nat *));
129 void ipfsync_storder __P((int, struct ipstate *));
133 /* ------------------------------------------------------------------------ */
134 /* Function: ipfsync_init */
135 /* Returns: int - 0 == success, -1 == failure */
136 /* Parameters: Nil */
138 /* Initialise all of the locks required for the sync code and initialise */
139 /* any data structures, as required. */
140 /* ------------------------------------------------------------------------ */
143 RWLOCK_INIT(&ipf_syncstate, "add things to state sync table");
144 RWLOCK_INIT(&ipf_syncnat, "add things to nat sync table");
145 MUTEX_INIT(&ipf_syncadd, "add things to sync table");
146 MUTEX_INIT(&ipsl_mutex, "add things to sync table");
147 # if SOLARIS && defined(_KERNEL)
148 cv_init(&ipslwait, "ipsl condvar", CV_DRIVER, NULL);
151 bzero((char *)syncnattab, sizeof(syncnattab));
152 bzero((char *)syncstatetab, sizeof(syncstatetab));
158 # if !defined(sparc) && !defined(__hppa)
159 /* ------------------------------------------------------------------------ */
160 /* Function: ipfsync_tcporder */
162 /* Parameters: way(I) - direction of byte order conversion. */
163 /* td(IO) - pointer to data to be converted. */
165 /* Do byte swapping on values in the TCP state information structure that */
166 /* need to be used at both ends by the host in their native byte order. */
167 /* ------------------------------------------------------------------------ */
168 void ipfsync_tcporder(way, td)
173 td->td_maxwin = htons(td->td_maxwin);
174 td->td_end = htonl(td->td_end);
175 td->td_maxend = htonl(td->td_maxend);
177 td->td_maxwin = ntohs(td->td_maxwin);
178 td->td_end = ntohl(td->td_end);
179 td->td_maxend = ntohl(td->td_maxend);
184 /* ------------------------------------------------------------------------ */
185 /* Function: ipfsync_natorder */
187 /* Parameters: way(I) - direction of byte order conversion. */
188 /* nat(IO) - pointer to data to be converted. */
190 /* Do byte swapping on values in the NAT data structure that need to be */
191 /* used at both ends by the host in their native byte order. */
192 /* ------------------------------------------------------------------------ */
193 void ipfsync_natorder(way, n)
198 n->nat_age = htonl(n->nat_age);
199 n->nat_flags = htonl(n->nat_flags);
200 n->nat_ipsumd = htonl(n->nat_ipsumd);
201 n->nat_use = htonl(n->nat_use);
202 n->nat_dir = htonl(n->nat_dir);
204 n->nat_age = ntohl(n->nat_age);
205 n->nat_flags = ntohl(n->nat_flags);
206 n->nat_ipsumd = ntohl(n->nat_ipsumd);
207 n->nat_use = ntohl(n->nat_use);
208 n->nat_dir = ntohl(n->nat_dir);
213 /* ------------------------------------------------------------------------ */
214 /* Function: ipfsync_storder */
216 /* Parameters: way(I) - direction of byte order conversion. */
217 /* ips(IO) - pointer to data to be converted. */
219 /* Do byte swapping on values in the IP state data structure that need to */
220 /* be used at both ends by the host in their native byte order. */
221 /* ------------------------------------------------------------------------ */
222 void ipfsync_storder(way, ips)
226 ipfsync_tcporder(way, &ips->is_tcp.ts_data[0]);
227 ipfsync_tcporder(way, &ips->is_tcp.ts_data[1]);
230 ips->is_hv = htonl(ips->is_hv);
231 ips->is_die = htonl(ips->is_die);
232 ips->is_pass = htonl(ips->is_pass);
233 ips->is_flags = htonl(ips->is_flags);
234 ips->is_opt[0] = htonl(ips->is_opt[0]);
235 ips->is_opt[1] = htonl(ips->is_opt[1]);
236 ips->is_optmsk[0] = htonl(ips->is_optmsk[0]);
237 ips->is_optmsk[1] = htonl(ips->is_optmsk[1]);
238 ips->is_sec = htons(ips->is_sec);
239 ips->is_secmsk = htons(ips->is_secmsk);
240 ips->is_auth = htons(ips->is_auth);
241 ips->is_authmsk = htons(ips->is_authmsk);
242 ips->is_s0[0] = htonl(ips->is_s0[0]);
243 ips->is_s0[1] = htonl(ips->is_s0[1]);
244 ips->is_smsk[0] = htons(ips->is_smsk[0]);
245 ips->is_smsk[1] = htons(ips->is_smsk[1]);
247 ips->is_hv = ntohl(ips->is_hv);
248 ips->is_die = ntohl(ips->is_die);
249 ips->is_pass = ntohl(ips->is_pass);
250 ips->is_flags = ntohl(ips->is_flags);
251 ips->is_opt[0] = ntohl(ips->is_opt[0]);
252 ips->is_opt[1] = ntohl(ips->is_opt[1]);
253 ips->is_optmsk[0] = ntohl(ips->is_optmsk[0]);
254 ips->is_optmsk[1] = ntohl(ips->is_optmsk[1]);
255 ips->is_sec = ntohs(ips->is_sec);
256 ips->is_secmsk = ntohs(ips->is_secmsk);
257 ips->is_auth = ntohs(ips->is_auth);
258 ips->is_authmsk = ntohs(ips->is_authmsk);
259 ips->is_s0[0] = ntohl(ips->is_s0[0]);
260 ips->is_s0[1] = ntohl(ips->is_s0[1]);
261 ips->is_smsk[0] = ntohl(ips->is_smsk[0]);
262 ips->is_smsk[1] = ntohl(ips->is_smsk[1]);
265 # else /* !defined(sparc) && !defined(__hppa) */
266 # define ipfsync_tcporder(x,y)
267 # define ipfsync_natorder(x,y)
268 # define ipfsync_storder(x,y)
269 # endif /* !defined(sparc) && !defined(__hppa) */
271 /* enable this for debugging */
274 /* ------------------------------------------------------------------------ */
275 /* Function: ipfsync_write */
276 /* Returns: int - 0 == success, else error value. */
277 /* Parameters: uio(I) - pointer to information about data to write */
279 /* Moves data from user space into the kernel and uses it for updating data */
280 /* structures in the state/NAT tables. */
281 /* ------------------------------------------------------------------------ */
282 int ipfsync_write(uio)
288 * THIS MUST BE SUFFICIENT LARGE TO STORE
289 * ANY POSSIBLE DATA TYPE
295 # if (BSD >= 199306) || defined(__FreeBSD__) || defined(__osf__)
296 uio->uio_rw = UIO_WRITE;
299 /* Try to get bytes */
300 while (uio->uio_resid > 0) {
302 if (uio->uio_resid >= sizeof(sh)) {
304 err = UIOMOVE(&sh, sizeof(sh), UIO_WRITE, uio);
307 if (ipf_sync_debug > 2)
308 printf("uiomove(header) failed: %d\n",
313 /* convert to host order */
314 sh.sm_magic = ntohl(sh.sm_magic);
315 sh.sm_len = ntohl(sh.sm_len);
316 sh.sm_num = ntohl(sh.sm_num);
318 if (ipf_sync_debug > 8)
319 printf("[%d] Read v:%d p:%d cmd:%d table:%d rev:%d len:%d magic:%x\n",
320 sh.sm_num, sh.sm_v, sh.sm_p, sh.sm_cmd,
321 sh.sm_table, sh.sm_rev, sh.sm_len,
324 if (sh.sm_magic != SYNHDRMAGIC) {
325 if (ipf_sync_debug > 2)
326 printf("uiomove(header) invalud %s\n",
331 if (sh.sm_v != 4 && sh.sm_v != 6) {
332 if (ipf_sync_debug > 2)
333 printf("uiomove(header) invalid %s\n",
338 if (sh.sm_cmd > SMC_MAXCMD) {
339 if (ipf_sync_debug > 2)
340 printf("uiomove(header) invalid %s\n",
346 if (sh.sm_table > SMC_MAXTBL) {
347 if (ipf_sync_debug > 2)
348 printf("uiomove(header) invalid %s\n",
354 /* unsufficient data, wait until next call */
355 if (ipf_sync_debug > 2)
356 printf("uiomove(header) insufficient data");
362 * We have a header, so try to read the amount of data
363 * needed for the request
367 if (sh.sm_len == 0) {
368 if (ipf_sync_debug > 2)
369 printf("uiomove(data zero length %s\n",
374 if (uio->uio_resid >= sh.sm_len) {
376 err = UIOMOVE(data, sh.sm_len, UIO_WRITE, uio);
379 if (ipf_sync_debug > 2)
380 printf("uiomove(data) failed: %d\n",
385 if (ipf_sync_debug > 7)
386 printf("uiomove(data) %d bytes read\n",
389 if (sh.sm_table == SMC_STATE)
390 err = ipfsync_state(&sh, data);
391 else if (sh.sm_table == SMC_NAT)
392 err = ipfsync_nat(&sh, data);
393 if (ipf_sync_debug > 7)
394 printf("[%d] Finished with error %d\n",
398 /* insufficient data, wait until next call */
399 if (ipf_sync_debug > 2)
400 printf("uiomove(data) %s %d bytes, got %d\n",
401 "insufficient data, need",
402 sh.sm_len, uio->uio_resid);
412 /* ------------------------------------------------------------------------ */
413 /* Function: ipfsync_read */
414 /* Returns: int - 0 == success, else error value. */
415 /* Parameters: uio(O) - pointer to information about where to store data */
417 /* This function is called when a user program wants to read some data */
418 /* for pending state/NAT updates. If no data is available, the caller is */
419 /* put to sleep, pending a wakeup from the "lower half" of this code. */
420 /* ------------------------------------------------------------------------ */
421 int ipfsync_read(uio)
428 if ((uio->uio_resid & 3) || (uio->uio_resid < 8))
431 # if (BSD >= 199306) || defined(__FreeBSD__) || defined(__osf__)
432 uio->uio_rw = UIO_READ;
435 MUTEX_ENTER(&ipsl_mutex);
436 while ((sl_tail == sl_idx) && (su_tail == su_idx)) {
437 # if SOLARIS && defined(_KERNEL)
438 if (!cv_wait_sig(&ipslwait, &ipsl_mutex)) {
439 MUTEX_EXIT(&ipsl_mutex);
447 l = get_sleep_lock(&sl_tail);
448 err = sleep(&sl_tail, PZERO+1);
450 MUTEX_EXIT(&ipsl_mutex);
457 err = mpsleep(&sl_tail, PSUSP|PCATCH, "ipl sleep", 0,
458 &ipsl_mutex, MS_LOCK_SIMPLE);
462 MUTEX_EXIT(&ipsl_mutex);
463 err = SLEEP(&sl_tail, "ipl sleep");
466 MUTEX_ENTER(&ipsl_mutex);
467 # endif /* __osf__ */
469 # endif /* SOLARIS */
471 MUTEX_EXIT(&ipsl_mutex);
473 READ_ENTER(&ipf_syncstate);
474 while ((sl_tail < sl_idx) && (uio->uio_resid > sizeof(*sl))) {
475 sl = synclog + sl_tail++;
476 err = UIOMOVE(sl, sizeof(*sl), UIO_READ, uio);
481 while ((su_tail < su_idx) && (uio->uio_resid > sizeof(*su))) {
482 su = syncupd + su_tail;
484 err = UIOMOVE(su, sizeof(*su), UIO_READ, uio);
487 if (su->sup_hdr.sm_sl != NULL)
488 su->sup_hdr.sm_sl->sl_idx = -1;
491 MUTEX_ENTER(&ipf_syncadd);
492 if (su_tail == su_idx)
493 su_tail = su_idx = 0;
494 if (sl_tail == sl_idx)
495 sl_tail = sl_idx = 0;
496 MUTEX_EXIT(&ipf_syncadd);
497 RWLOCK_EXIT(&ipf_syncstate);
502 /* ------------------------------------------------------------------------ */
503 /* Function: ipfsync_state */
504 /* Returns: int - 0 == success, else error value. */
505 /* Parameters: sp(I) - pointer to sync packet data header */
506 /* uio(I) - pointer to user data for further information */
508 /* Updates the state table according to information passed in the sync */
509 /* header. As required, more data is fetched from the uio structure but */
510 /* varies depending on the contents of the sync header. This function can */
511 /* create a new state entry or update one. Deletion is left to the state */
512 /* structures being timed out correctly. */
513 /* ------------------------------------------------------------------------ */
514 int ipfsync_state(sp, data)
525 hv = sp->sm_num & (SYNC_STATETABSZ - 1);
531 bcopy(data, &sn, sizeof(sn));
532 KMALLOC(is, ipstate_t *);
538 KMALLOC(sl, synclist_t *);
545 bzero((char *)is, offsetof(ipstate_t, is_die));
546 bcopy((char *)&sn.is_die, (char *)&is->is_die,
547 sizeof(*is) - offsetof(ipstate_t, is_die));
548 ipfsync_storder(0, is);
551 * We need to find the same rule on the slave as was used on
552 * the master to create this state entry.
554 READ_ENTER(&ipf_mutex);
555 fr = fr_getrulen(IPL_LOGIPF, sn.is_group, sn.is_rulen);
557 MUTEX_ENTER(&fr->fr_lock);
560 MUTEX_EXIT(&fr->fr_lock);
562 RWLOCK_EXIT(&ipf_mutex);
564 if (ipf_sync_debug > 4)
565 printf("[%d] Filter rules = %p\n", sp->sm_num, fr);
572 bcopy(sp, &sl->sl_hdr, sizeof(struct synchdr));
574 WRITE_ENTER(&ipf_syncstate);
575 WRITE_ENTER(&ipf_state);
577 sl->sl_pnext = syncstatetab + hv;
578 sl->sl_next = syncstatetab[hv];
579 if (syncstatetab[hv] != NULL)
580 syncstatetab[hv]->sl_pnext = &sl->sl_next;
581 syncstatetab[hv] = sl;
582 MUTEX_DOWNGRADE(&ipf_syncstate);
583 fr_stinsert(is, sp->sm_rev);
585 * Do not initialise the interface pointers for the state
586 * entry as the full complement of interface names may not
589 * Put this state entry on its timeout queue.
591 /*fr_setstatequeue(is, sp->sm_rev);*/
595 bcopy(data, &su, sizeof(su));
597 if (ipf_sync_debug > 4)
598 printf("[%d] Update age %lu state %d/%d \n",
599 sp->sm_num, su.stu_age, su.stu_state[0],
602 READ_ENTER(&ipf_syncstate);
603 for (sl = syncstatetab[hv]; (sl != NULL); sl = sl->sl_next)
604 if (sl->sl_hdr.sm_num == sp->sm_num)
607 if (ipf_sync_debug > 1)
608 printf("[%d] State not found - can't update\n",
610 RWLOCK_EXIT(&ipf_syncstate);
615 READ_ENTER(&ipf_state);
617 if (ipf_sync_debug > 6)
618 printf("[%d] Data from state v:%d p:%d cmd:%d table:%d rev:%d\n",
619 sp->sm_num, sl->sl_hdr.sm_v, sl->sl_hdr.sm_p,
620 sl->sl_hdr.sm_cmd, sl->sl_hdr.sm_table,
625 MUTEX_ENTER(&is->is_lock);
629 /* XXX FV --- shouldn't we do ntohl/htonl???? XXX */
630 is->is_send = su.stu_data[0].td_end;
631 is->is_maxsend = su.stu_data[0].td_maxend;
632 is->is_maxswin = su.stu_data[0].td_maxwin;
633 is->is_state[0] = su.stu_state[0];
634 is->is_dend = su.stu_data[1].td_end;
635 is->is_maxdend = su.stu_data[1].td_maxend;
636 is->is_maxdwin = su.stu_data[1].td_maxwin;
637 is->is_state[1] = su.stu_state[1];
643 if (ipf_sync_debug > 6)
644 printf("[%d] Setting timers for state\n", sp->sm_num);
646 fr_setstatequeue(is, sp->sm_rev);
648 MUTEX_EXIT(&is->is_lock);
657 RWLOCK_EXIT(&ipf_state);
658 RWLOCK_EXIT(&ipf_syncstate);
661 if (ipf_sync_debug > 6)
662 printf("[%d] Update completed with error %d\n",
667 # endif /* _KERNEL */
670 /* ------------------------------------------------------------------------ */
671 /* Function: ipfsync_del */
673 /* Parameters: sl(I) - pointer to synclist object to delete */
675 /* Deletes an object from the synclist table and free's its memory. */
676 /* ------------------------------------------------------------------------ */
680 WRITE_ENTER(&ipf_syncstate);
681 *sl->sl_pnext = sl->sl_next;
682 if (sl->sl_next != NULL)
683 sl->sl_next->sl_pnext = sl->sl_pnext;
684 if (sl->sl_idx != -1)
685 syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
686 RWLOCK_EXIT(&ipf_syncstate);
691 /* ------------------------------------------------------------------------ */
692 /* Function: ipfsync_nat */
693 /* Returns: int - 0 == success, else error value. */
694 /* Parameters: sp(I) - pointer to sync packet data header */
695 /* uio(I) - pointer to user data for further information */
697 /* Updates the NAT table according to information passed in the sync */
698 /* header. As required, more data is fetched from the uio structure but */
699 /* varies depending on the contents of the sync header. This function can */
700 /* create a new NAT entry or update one. Deletion is left to the NAT */
701 /* structures being timed out correctly. */
702 /* ------------------------------------------------------------------------ */
703 int ipfsync_nat(sp, data)
713 READ_ENTER(&ipf_syncstate);
724 KMALLOC(sl, synclist_t *);
732 bzero((char *)n, offsetof(nat_t, nat_age));
733 bcopy((char *)&nat->nat_age, (char *)&n->nat_age,
734 sizeof(*n) - offsetof(nat_t, nat_age));
735 ipfsync_natorder(0, n);
740 sl->sl_num = ntohl(sp->sm_num);
742 WRITE_ENTER(&ipf_nat);
743 sl->sl_pnext = syncstatetab + hv;
744 sl->sl_next = syncstatetab[hv];
745 if (syncstatetab[hv] != NULL)
746 syncstatetab[hv]->sl_pnext = &sl->sl_next;
747 syncstatetab[hv] = sl;
748 nat_insert(n, sl->sl_rev);
749 RWLOCK_EXIT(&ipf_nat);
753 bcopy(data, &su, sizeof(su));
755 READ_ENTER(&ipf_syncstate);
756 for (sl = syncstatetab[hv]; (sl != NULL); sl = sl->sl_next)
757 if (sl->sl_hdr.sm_num == sp->sm_num)
764 READ_ENTER(&ipf_nat);
768 MUTEX_ENTER(&nat->nat_lock);
769 fr_setnatqueue(nat, sl->sl_rev);
770 MUTEX_EXIT(&nat->nat_lock);
772 RWLOCK_EXIT(&ipf_nat);
781 RWLOCK_EXIT(&ipf_syncstate);
786 /* ------------------------------------------------------------------------ */
787 /* Function: ipfsync_new */
788 /* Returns: synclist_t* - NULL == failure, else pointer to new synclist */
789 /* data structure. */
790 /* Parameters: tab(I) - type of synclist_t to create */
791 /* fin(I) - pointer to packet information */
792 /* ptr(I) - pointer to owning object */
794 /* Creates a new sync table entry and notifies any sleepers that it's there */
795 /* waiting to be processed. */
796 /* ------------------------------------------------------------------------ */
797 synclist_t *ipfsync_new(tab, fin, ptr)
806 if (sl_idx == SYNCLOG_SZ)
808 KMALLOC(sl, synclist_t *);
812 MUTEX_ENTER(&ipf_syncadd);
814 * Get a unique number for this synclist_t. The number is only meant
815 * to be unique for the lifetime of the structure and may be reused
819 if (ipf_syncnum == 0) {
824 hv = ipf_syncnum & (SYNC_STATETABSZ - 1);
825 while (ipf_syncwrap != 0) {
826 for (ss = syncstatetab[hv]; ss; ss = ss->sl_next)
827 if (ss->sl_hdr.sm_num == ipf_syncnum)
832 hv = ipf_syncnum & (SYNC_STATETABSZ - 1);
835 * Use the synch number of the object as the hash key. Should end up
836 * with relatively even distribution over time.
837 * XXX - an attacker could lunch an DoS attack, of sorts, if they are
838 * the only one causing new table entries by only keeping open every
839 * nth connection they make, where n is a value in the interval
840 * [0, SYNC_STATETABSZ-1].
842 sl->sl_pnext = syncstatetab + hv;
843 sl->sl_next = syncstatetab[hv];
844 syncstatetab[hv] = sl;
845 sl->sl_num = ipf_syncnum;
846 MUTEX_EXIT(&ipf_syncadd);
848 sl->sl_magic = htonl(SYNHDRMAGIC);
849 sl->sl_v = fin->fin_v;
850 sl->sl_p = fin->fin_p;
851 sl->sl_cmd = SMC_CREATE;
854 sl->sl_rev = fin->fin_rev;
855 if (tab == SMC_STATE) {
857 sz = sizeof(*sl->sl_ips);
858 } else if (tab == SMC_NAT) {
860 sz = sizeof(*sl->sl_ipn);
868 * Create the log entry to be read by a user daemon. When it has been
869 * finished and put on the queue, send a signal to wakeup any waiters.
871 MUTEX_ENTER(&ipf_syncadd);
872 sle = synclog + sl_idx++;
873 bcopy((char *)&sl->sl_hdr, (char *)&sle->sle_hdr,
874 sizeof(sle->sle_hdr));
875 sle->sle_hdr.sm_num = htonl(sle->sle_hdr.sm_num);
876 sle->sle_hdr.sm_len = htonl(sle->sle_hdr.sm_len);
878 bcopy((char *)ptr, (char *)&sle->sle_un, sz);
879 if (tab == SMC_STATE) {
880 ipfsync_storder(1, &sle->sle_un.sleu_ips);
881 } else if (tab == SMC_NAT) {
882 ipfsync_natorder(1, &sle->sle_un.sleu_ipn);
885 MUTEX_EXIT(&ipf_syncadd);
887 MUTEX_ENTER(&ipsl_mutex);
890 cv_signal(&ipslwait);
892 MUTEX_EXIT(&ipsl_mutex);
894 MUTEX_EXIT(&ipsl_mutex);
903 /* ------------------------------------------------------------------------ */
904 /* Function: ipfsync_update */
906 /* Parameters: tab(I) - type of synclist_t to create */
907 /* fin(I) - pointer to packet information */
908 /* sl(I) - pointer to synchronisation object */
910 /* For outbound packets, only, create an sync update record for the user */
911 /* process to read. */
912 /* ------------------------------------------------------------------------ */
913 void ipfsync_update(tab, fin, sl)
918 synctcp_update_t *st;
923 if (fin->fin_out == 0 || sl == NULL)
926 WRITE_ENTER(&ipf_syncstate);
927 MUTEX_ENTER(&ipf_syncadd);
928 if (sl->sl_idx == -1) {
929 slu = syncupd + su_idx;
930 sl->sl_idx = su_idx++;
931 bcopy((char *)&sl->sl_hdr, (char *)&slu->sup_hdr,
932 sizeof(slu->sup_hdr));
933 slu->sup_hdr.sm_magic = htonl(SYNHDRMAGIC);
934 slu->sup_hdr.sm_sl = sl;
935 slu->sup_hdr.sm_cmd = SMC_UPDATE;
936 slu->sup_hdr.sm_table = tab;
937 slu->sup_hdr.sm_num = htonl(sl->sl_num);
938 slu->sup_hdr.sm_len = htonl(sizeof(struct synctcp_update));
939 slu->sup_hdr.sm_rev = fin->fin_rev;
941 if (fin->fin_p == IPPROTO_TCP) {
947 slu = syncupd + sl->sl_idx;
948 MUTEX_EXIT(&ipf_syncadd);
949 MUTEX_DOWNGRADE(&ipf_syncstate);
952 * Only TCP has complex timeouts, others just use default timeouts.
953 * For TCP, we only need to track the connection state and window.
955 if (fin->fin_p == IPPROTO_TCP) {
957 if (tab == SMC_STATE) {
959 st->stu_age = htonl(ips->is_die);
960 st->stu_data[0].td_end = ips->is_send;
961 st->stu_data[0].td_maxend = ips->is_maxsend;
962 st->stu_data[0].td_maxwin = ips->is_maxswin;
963 st->stu_state[0] = ips->is_state[0];
964 st->stu_data[1].td_end = ips->is_dend;
965 st->stu_data[1].td_maxend = ips->is_maxdend;
966 st->stu_data[1].td_maxwin = ips->is_maxdwin;
967 st->stu_state[1] = ips->is_state[1];
968 } else if (tab == SMC_NAT) {
970 st->stu_age = htonl(nat->nat_age);
973 RWLOCK_EXIT(&ipf_syncstate);
975 MUTEX_ENTER(&ipsl_mutex);
978 cv_signal(&ipslwait);
980 MUTEX_EXIT(&ipsl_mutex);
982 MUTEX_EXIT(&ipsl_mutex);
990 /* ------------------------------------------------------------------------ */
991 /* Function: fr_sync_ioctl */
992 /* Returns: int - 0 == success, != 0 == failure */
993 /* Parameters: data(I) - pointer to ioctl data */
994 /* cmd(I) - ioctl command integer */
995 /* mode(I) - file mode bits used with open */
997 /* This function currently does not handle any ioctls and so just returns */
998 /* EINVAL on all occasions. */
999 /* ------------------------------------------------------------------------ */
1000 int fr_sync_ioctl(data, cmd, mode, uid, ctx)
1010 int ipfsync_canread()
1012 return !((sl_tail == sl_idx) && (su_tail == su_idx));
1016 int ipfsync_canwrite()
1020 #endif /* IPFILTER_SYNC */