4 * Copyright (C) 2012 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__)
28 # include <sys/systm.h>
30 # include <sys/mbuf.h>
32 # include <sys/select.h>
34 # include <sys/selinfo.h>
37 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
38 # include <sys/proc.h>
40 #if defined(_KERNEL) && defined(__FreeBSD__)
41 # include <sys/filio.h>
42 # include <sys/fcntl.h>
44 # include <sys/ioctl.h>
47 # include <sys/protosw.h>
48 #include <sys/socket.h>
50 # include <sys/filio.h>
51 # include <sys/byteorder.h>
53 # include <sys/dditypes.h>
55 # include <sys/stream.h>
56 # include <sys/kmem.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/tcp.h>
67 # include <netinet/ip_var.h>
68 # include <netinet/tcp_fsm.h>
69 #include <netinet/udp.h>
70 #include <netinet/ip_icmp.h>
71 #include "netinet/ip_compat.h"
72 #include <netinet/tcpip.h>
73 #include "netinet/ip_fil.h"
74 #include "netinet/ip_nat.h"
75 #include "netinet/ip_frag.h"
76 #include "netinet/ip_state.h"
77 #include "netinet/ip_proxy.h"
78 #include "netinet/ip_sync.h"
80 #include <netinet/icmp6.h>
82 #if defined(__FreeBSD__)
83 # include <sys/malloc.h>
84 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
85 # include <sys/libkern.h>
86 # include <sys/systm.h>
92 static const char rcsid[] = "@(#)$Id$";
95 #define SYNC_STATETABSZ 256
96 #define SYNC_NATTABSZ 256
98 typedef struct ipf_sync_softc_s {
99 ipfmutex_t ipf_syncadd;
100 ipfmutex_t ipsl_mutex;
101 ipfrwlock_t ipf_syncstate;
102 ipfrwlock_t ipf_syncnat;
103 #if SOLARIS && defined(_KERNEL)
106 synclist_t **syncstatetab;
107 synclist_t **syncnattab;
108 synclogent_t *synclog;
109 syncupdent_t *syncupd;
112 u_int sl_idx; /* next available sync log entry */
113 u_int su_idx; /* next available sync update entry */
114 u_int sl_tail; /* next sync log entry to read */
115 u_int su_tail; /* next sync update entry to read */
117 int ipf_sync_nat_tab_sz;
118 int ipf_sync_state_tab_sz;
121 u_32_t ipf_sync_lastwakeup;
122 int ipf_sync_wake_interval;
123 int ipf_sync_event_high_wm;
124 int ipf_sync_queue_high_wm;
128 static int ipf_sync_flush_table(ipf_sync_softc_t *, int, synclist_t **);
129 static void ipf_sync_wakeup(ipf_main_softc_t *);
130 static void ipf_sync_del(ipf_sync_softc_t *, synclist_t *);
131 static void ipf_sync_poll_wakeup(ipf_main_softc_t *);
132 static int ipf_sync_nat(ipf_main_softc_t *, synchdr_t *, void *);
133 static int ipf_sync_state(ipf_main_softc_t *, synchdr_t *, void *);
135 # if !defined(sparc) && !defined(__hppa)
136 void ipf_sync_tcporder(int, struct tcpdata *);
137 void ipf_sync_natorder(int, struct nat *);
138 void ipf_sync_storder(int, struct ipstate *);
143 ipf_sync_soft_create(softc)
144 ipf_main_softc_t *softc;
146 ipf_sync_softc_t *softs;
148 KMALLOC(softs, ipf_sync_softc_t *);
154 bzero((char *)softs, sizeof(*softs));
156 softs->ipf_sync_log_sz = SYNCLOG_SZ;
157 softs->ipf_sync_nat_tab_sz = SYNC_STATETABSZ;
158 softs->ipf_sync_state_tab_sz = SYNC_STATETABSZ;
159 softs->ipf_sync_event_high_wm = SYNCLOG_SZ * 100 / 90; /* 90% */
160 softs->ipf_sync_queue_high_wm = SYNCLOG_SZ * 100 / 90; /* 90% */
166 /* ------------------------------------------------------------------------ */
167 /* Function: ipf_sync_init */
168 /* Returns: int - 0 == success, -1 == failure */
169 /* Parameters: Nil */
171 /* Initialise all of the locks required for the sync code and initialise */
172 /* any data structures, as required. */
173 /* ------------------------------------------------------------------------ */
175 ipf_sync_soft_init(softc, arg)
176 ipf_main_softc_t *softc;
179 ipf_sync_softc_t *softs = arg;
181 KMALLOCS(softs->synclog, synclogent_t *,
182 softs->ipf_sync_log_sz * sizeof(*softs->synclog));
183 if (softs->synclog == NULL)
185 bzero((char *)softs->synclog,
186 softs->ipf_sync_log_sz * sizeof(*softs->synclog));
188 KMALLOCS(softs->syncupd, syncupdent_t *,
189 softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
190 if (softs->syncupd == NULL)
192 bzero((char *)softs->syncupd,
193 softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
195 KMALLOCS(softs->syncstatetab, synclist_t **,
196 softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
197 if (softs->syncstatetab == NULL)
199 bzero((char *)softs->syncstatetab,
200 softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
202 KMALLOCS(softs->syncnattab, synclist_t **,
203 softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
204 if (softs->syncnattab == NULL)
206 bzero((char *)softs->syncnattab,
207 softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
209 softs->ipf_sync_num = 1;
210 softs->ipf_sync_wrap = 0;
215 softs->ipf_sync_events = 0;
216 softs->ipf_sync_lastwakeup = 0;
219 # if SOLARIS && defined(_KERNEL)
220 cv_init(&softs->ipslwait, "ipsl condvar", CV_DRIVER, NULL);
222 RWLOCK_INIT(&softs->ipf_syncstate, "add things to state sync table");
223 RWLOCK_INIT(&softs->ipf_syncnat, "add things to nat sync table");
224 MUTEX_INIT(&softs->ipf_syncadd, "add things to sync table");
225 MUTEX_INIT(&softs->ipsl_mutex, "read ring lock");
227 softs->ipf_sync_inited = 1;
233 /* ------------------------------------------------------------------------ */
234 /* Function: ipf_sync_unload */
235 /* Returns: int - 0 == success, -1 == failure */
236 /* Parameters: Nil */
238 /* Destroy the locks created when initialising and free any memory in use */
239 /* with the synchronisation tables. */
240 /* ------------------------------------------------------------------------ */
242 ipf_sync_soft_fini(softc, arg)
243 ipf_main_softc_t *softc;
246 ipf_sync_softc_t *softs = arg;
248 if (softs->syncnattab != NULL) {
249 ipf_sync_flush_table(softs, softs->ipf_sync_nat_tab_sz,
251 KFREES(softs->syncnattab,
252 softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
253 softs->syncnattab = NULL;
256 if (softs->syncstatetab != NULL) {
257 ipf_sync_flush_table(softs, softs->ipf_sync_state_tab_sz,
258 softs->syncstatetab);
259 KFREES(softs->syncstatetab,
260 softs->ipf_sync_state_tab_sz *
261 sizeof(*softs->syncstatetab));
262 softs->syncstatetab = NULL;
265 if (softs->syncupd != NULL) {
266 KFREES(softs->syncupd,
267 softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
268 softs->syncupd = NULL;
271 if (softs->synclog != NULL) {
272 KFREES(softs->synclog,
273 softs->ipf_sync_log_sz * sizeof(*softs->synclog));
274 softs->synclog = NULL;
277 if (softs->ipf_sync_inited == 1) {
278 MUTEX_DESTROY(&softs->ipsl_mutex);
279 MUTEX_DESTROY(&softs->ipf_syncadd);
280 RW_DESTROY(&softs->ipf_syncnat);
281 RW_DESTROY(&softs->ipf_syncstate);
282 softs->ipf_sync_inited = 0;
289 ipf_sync_soft_destroy(softc, arg)
290 ipf_main_softc_t *softc;
293 ipf_sync_softc_t *softs = arg;
300 /* ------------------------------------------------------------------------ */
301 /* Function: ipf_sync_tcporder */
303 /* Parameters: way(I) - direction of byte order conversion. */
304 /* td(IO) - pointer to data to be converted. */
306 /* Do byte swapping on values in the TCP state information structure that */
307 /* need to be used at both ends by the host in their native byte order. */
308 /* ------------------------------------------------------------------------ */
310 ipf_sync_tcporder(way, td)
315 td->td_maxwin = htons(td->td_maxwin);
316 td->td_end = htonl(td->td_end);
317 td->td_maxend = htonl(td->td_maxend);
319 td->td_maxwin = ntohs(td->td_maxwin);
320 td->td_end = ntohl(td->td_end);
321 td->td_maxend = ntohl(td->td_maxend);
326 /* ------------------------------------------------------------------------ */
327 /* Function: ipf_sync_natorder */
329 /* Parameters: way(I) - direction of byte order conversion. */
330 /* nat(IO) - pointer to data to be converted. */
332 /* Do byte swapping on values in the NAT data structure that need to be */
333 /* used at both ends by the host in their native byte order. */
334 /* ------------------------------------------------------------------------ */
336 ipf_sync_natorder(way, n)
341 n->nat_age = htonl(n->nat_age);
342 n->nat_flags = htonl(n->nat_flags);
343 n->nat_ipsumd = htonl(n->nat_ipsumd);
344 n->nat_use = htonl(n->nat_use);
345 n->nat_dir = htonl(n->nat_dir);
347 n->nat_age = ntohl(n->nat_age);
348 n->nat_flags = ntohl(n->nat_flags);
349 n->nat_ipsumd = ntohl(n->nat_ipsumd);
350 n->nat_use = ntohl(n->nat_use);
351 n->nat_dir = ntohl(n->nat_dir);
356 /* ------------------------------------------------------------------------ */
357 /* Function: ipf_sync_storder */
359 /* Parameters: way(I) - direction of byte order conversion. */
360 /* ips(IO) - pointer to data to be converted. */
362 /* Do byte swapping on values in the IP state data structure that need to */
363 /* be used at both ends by the host in their native byte order. */
364 /* ------------------------------------------------------------------------ */
366 ipf_sync_storder(way, ips)
370 ipf_sync_tcporder(way, &ips->is_tcp.ts_data[0]);
371 ipf_sync_tcporder(way, &ips->is_tcp.ts_data[1]);
374 ips->is_hv = htonl(ips->is_hv);
375 ips->is_die = htonl(ips->is_die);
376 ips->is_pass = htonl(ips->is_pass);
377 ips->is_flags = htonl(ips->is_flags);
378 ips->is_opt[0] = htonl(ips->is_opt[0]);
379 ips->is_opt[1] = htonl(ips->is_opt[1]);
380 ips->is_optmsk[0] = htonl(ips->is_optmsk[0]);
381 ips->is_optmsk[1] = htonl(ips->is_optmsk[1]);
382 ips->is_sec = htons(ips->is_sec);
383 ips->is_secmsk = htons(ips->is_secmsk);
384 ips->is_auth = htons(ips->is_auth);
385 ips->is_authmsk = htons(ips->is_authmsk);
386 ips->is_s0[0] = htonl(ips->is_s0[0]);
387 ips->is_s0[1] = htonl(ips->is_s0[1]);
388 ips->is_smsk[0] = htons(ips->is_smsk[0]);
389 ips->is_smsk[1] = htons(ips->is_smsk[1]);
391 ips->is_hv = ntohl(ips->is_hv);
392 ips->is_die = ntohl(ips->is_die);
393 ips->is_pass = ntohl(ips->is_pass);
394 ips->is_flags = ntohl(ips->is_flags);
395 ips->is_opt[0] = ntohl(ips->is_opt[0]);
396 ips->is_opt[1] = ntohl(ips->is_opt[1]);
397 ips->is_optmsk[0] = ntohl(ips->is_optmsk[0]);
398 ips->is_optmsk[1] = ntohl(ips->is_optmsk[1]);
399 ips->is_sec = ntohs(ips->is_sec);
400 ips->is_secmsk = ntohs(ips->is_secmsk);
401 ips->is_auth = ntohs(ips->is_auth);
402 ips->is_authmsk = ntohs(ips->is_authmsk);
403 ips->is_s0[0] = ntohl(ips->is_s0[0]);
404 ips->is_s0[1] = ntohl(ips->is_s0[1]);
405 ips->is_smsk[0] = ntohl(ips->is_smsk[0]);
406 ips->is_smsk[1] = ntohl(ips->is_smsk[1]);
409 # else /* !defined(sparc) */
410 # define ipf_sync_tcporder(x,y)
411 # define ipf_sync_natorder(x,y)
412 # define ipf_sync_storder(x,y)
413 # endif /* !defined(sparc) */
416 /* ------------------------------------------------------------------------ */
417 /* Function: ipf_sync_write */
418 /* Returns: int - 0 == success, else error value. */
419 /* Parameters: uio(I) - pointer to information about data to write */
421 /* Moves data from user space into the kernel and uses it for updating data */
422 /* structures in the state/NAT tables. */
423 /* ------------------------------------------------------------------------ */
425 ipf_sync_write(softc, uio)
426 ipf_main_softc_t *softc;
429 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
433 * THIS MUST BE SUFFICIENT LARGE TO STORE
434 * ANY POSSIBLE DATA TYPE
440 # if defined(__NetBSD__) || defined(__FreeBSD__)
441 uio->uio_rw = UIO_WRITE;
444 /* Try to get bytes */
445 while (uio->uio_resid > 0) {
447 if (uio->uio_resid >= sizeof(sh)) {
449 err = UIOMOVE(&sh, sizeof(sh), UIO_WRITE, uio);
452 if (softs->ipf_sync_debug > 2)
453 printf("uiomove(header) failed: %d\n",
458 /* convert to host order */
459 sh.sm_magic = ntohl(sh.sm_magic);
460 sh.sm_len = ntohl(sh.sm_len);
461 sh.sm_num = ntohl(sh.sm_num);
463 if (softs->ipf_sync_debug > 8)
464 printf("[%d] Read v:%d p:%d cmd:%d table:%d rev:%d len:%d magic:%x\n",
465 sh.sm_num, sh.sm_v, sh.sm_p, sh.sm_cmd,
466 sh.sm_table, sh.sm_rev, sh.sm_len,
469 if (sh.sm_magic != SYNHDRMAGIC) {
470 if (softs->ipf_sync_debug > 2)
471 printf("uiomove(header) invalid %s\n",
477 if (sh.sm_v != 4 && sh.sm_v != 6) {
478 if (softs->ipf_sync_debug > 2)
479 printf("uiomove(header) invalid %s\n",
485 if (sh.sm_cmd > SMC_MAXCMD) {
486 if (softs->ipf_sync_debug > 2)
487 printf("uiomove(header) invalid %s\n",
494 if (sh.sm_table > SMC_MAXTBL) {
495 if (softs->ipf_sync_debug > 2)
496 printf("uiomove(header) invalid %s\n",
503 /* unsufficient data, wait until next call */
504 if (softs->ipf_sync_debug > 2)
505 printf("uiomove(header) insufficient data");
512 * We have a header, so try to read the amount of data
513 * needed for the request
517 if (sh.sm_len == 0) {
518 if (softs->ipf_sync_debug > 2)
519 printf("uiomove(data zero length %s\n",
525 if (uio->uio_resid >= sh.sm_len) {
527 err = UIOMOVE(data, sh.sm_len, UIO_WRITE, uio);
530 if (softs->ipf_sync_debug > 2)
531 printf("uiomove(data) failed: %d\n",
536 if (softs->ipf_sync_debug > 7)
537 printf("uiomove(data) %d bytes read\n",
540 if (sh.sm_table == SMC_STATE)
541 err = ipf_sync_state(softc, &sh, data);
542 else if (sh.sm_table == SMC_NAT)
543 err = ipf_sync_nat(softc, &sh, data);
544 if (softs->ipf_sync_debug > 7)
545 printf("[%d] Finished with error %d\n",
549 /* insufficient data, wait until next call */
550 if (softs->ipf_sync_debug > 2)
551 printf("uiomove(data) %s %d bytes, got %d\n",
552 "insufficient data, need",
553 sh.sm_len, (int)uio->uio_resid);
564 /* ------------------------------------------------------------------------ */
565 /* Function: ipf_sync_read */
566 /* Returns: int - 0 == success, else error value. */
567 /* Parameters: uio(O) - pointer to information about where to store data */
569 /* This function is called when a user program wants to read some data */
570 /* for pending state/NAT updates. If no data is available, the caller is */
571 /* put to sleep, pending a wakeup from the "lower half" of this code. */
572 /* ------------------------------------------------------------------------ */
574 ipf_sync_read(softc, uio)
575 ipf_main_softc_t *softc;
578 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
583 if ((uio->uio_resid & 3) || (uio->uio_resid < 8)) {
588 # if defined(__NetBSD__) || defined(__FreeBSD__)
589 uio->uio_rw = UIO_READ;
592 MUTEX_ENTER(&softs->ipsl_mutex);
593 while ((softs->sl_tail == softs->sl_idx) &&
594 (softs->su_tail == softs->su_idx)) {
595 # if defined(_KERNEL)
597 if (!cv_wait_sig(&softs->ipslwait, &softs->ipsl_mutex.ipf_lk)) {
598 MUTEX_EXIT(&softs->ipsl_mutex);
603 MUTEX_EXIT(&softs->ipsl_mutex);
604 err = SLEEP(&softs->sl_tail, "ipl sleep");
609 MUTEX_ENTER(&softs->ipsl_mutex);
610 # endif /* SOLARIS */
611 # endif /* _KERNEL */
614 while ((softs->sl_tail < softs->sl_idx) &&
615 (uio->uio_resid > sizeof(*sl))) {
616 sl = softs->synclog + softs->sl_tail++;
617 MUTEX_EXIT(&softs->ipsl_mutex);
618 err = UIOMOVE(sl, sizeof(*sl), UIO_READ, uio);
621 MUTEX_ENTER(&softs->ipsl_mutex);
624 while ((softs->su_tail < softs->su_idx) &&
625 (uio->uio_resid > sizeof(*su))) {
626 su = softs->syncupd + softs->su_tail;
628 MUTEX_EXIT(&softs->ipsl_mutex);
629 err = UIOMOVE(su, sizeof(*su), UIO_READ, uio);
632 MUTEX_ENTER(&softs->ipsl_mutex);
633 if (su->sup_hdr.sm_sl != NULL)
634 su->sup_hdr.sm_sl->sl_idx = -1;
636 if (softs->sl_tail == softs->sl_idx)
637 softs->sl_tail = softs->sl_idx = 0;
638 if (softs->su_tail == softs->su_idx)
639 softs->su_tail = softs->su_idx = 0;
640 MUTEX_EXIT(&softs->ipsl_mutex);
646 /* ------------------------------------------------------------------------ */
647 /* Function: ipf_sync_state */
648 /* Returns: int - 0 == success, else error value. */
649 /* Parameters: sp(I) - pointer to sync packet data header */
650 /* uio(I) - pointer to user data for further information */
652 /* Updates the state table according to information passed in the sync */
653 /* header. As required, more data is fetched from the uio structure but */
654 /* varies depending on the contents of the sync header. This function can */
655 /* create a new state entry or update one. Deletion is left to the state */
656 /* structures being timed out correctly. */
657 /* ------------------------------------------------------------------------ */
659 ipf_sync_state(softc, sp, data)
660 ipf_main_softc_t *softc;
664 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
672 hv = sp->sm_num & (softs->ipf_sync_state_tab_sz - 1);
678 bcopy(data, &sn, sizeof(sn));
679 KMALLOC(is, ipstate_t *);
686 KMALLOC(sl, synclist_t *);
694 bzero((char *)is, offsetof(ipstate_t, is_die));
695 bcopy((char *)&sn.is_die, (char *)&is->is_die,
696 sizeof(*is) - offsetof(ipstate_t, is_die));
697 ipf_sync_storder(0, is);
700 * We need to find the same rule on the slave as was used on
701 * the master to create this state entry.
703 READ_ENTER(&softc->ipf_mutex);
704 fr = ipf_getrulen(softc, IPL_LOGIPF, sn.is_group, sn.is_rulen);
706 MUTEX_ENTER(&fr->fr_lock);
709 MUTEX_EXIT(&fr->fr_lock);
711 RWLOCK_EXIT(&softc->ipf_mutex);
713 if (softs->ipf_sync_debug > 4)
714 printf("[%d] Filter rules = %p\n", sp->sm_num, fr);
721 bcopy(sp, &sl->sl_hdr, sizeof(struct synchdr));
723 WRITE_ENTER(&softs->ipf_syncstate);
724 WRITE_ENTER(&softc->ipf_state);
726 sl->sl_pnext = softs->syncstatetab + hv;
727 sl->sl_next = softs->syncstatetab[hv];
728 if (softs->syncstatetab[hv] != NULL)
729 softs->syncstatetab[hv]->sl_pnext = &sl->sl_next;
730 softs->syncstatetab[hv] = sl;
731 MUTEX_DOWNGRADE(&softs->ipf_syncstate);
732 ipf_state_insert(softc, is, sp->sm_rev);
734 * Do not initialise the interface pointers for the state
735 * entry as the full complement of interface names may not
738 * Put this state entry on its timeout queue.
740 /*fr_setstatequeue(is, sp->sm_rev);*/
744 bcopy(data, &su, sizeof(su));
746 if (softs->ipf_sync_debug > 4)
747 printf("[%d] Update age %lu state %d/%d \n",
748 sp->sm_num, su.stu_age, su.stu_state[0],
751 READ_ENTER(&softs->ipf_syncstate);
752 for (sl = softs->syncstatetab[hv]; (sl != NULL);
754 if (sl->sl_hdr.sm_num == sp->sm_num)
757 if (softs->ipf_sync_debug > 1)
758 printf("[%d] State not found - can't update\n",
760 RWLOCK_EXIT(&softs->ipf_syncstate);
766 READ_ENTER(&softc->ipf_state);
768 if (softs->ipf_sync_debug > 6)
769 printf("[%d] Data from state v:%d p:%d cmd:%d table:%d rev:%d\n",
770 sp->sm_num, sl->sl_hdr.sm_v, sl->sl_hdr.sm_p,
771 sl->sl_hdr.sm_cmd, sl->sl_hdr.sm_table,
776 MUTEX_ENTER(&is->is_lock);
780 /* XXX FV --- shouldn't we do ntohl/htonl???? XXX */
781 is->is_send = su.stu_data[0].td_end;
782 is->is_maxsend = su.stu_data[0].td_maxend;
783 is->is_maxswin = su.stu_data[0].td_maxwin;
784 is->is_state[0] = su.stu_state[0];
785 is->is_dend = su.stu_data[1].td_end;
786 is->is_maxdend = su.stu_data[1].td_maxend;
787 is->is_maxdwin = su.stu_data[1].td_maxwin;
788 is->is_state[1] = su.stu_state[1];
794 if (softs->ipf_sync_debug > 6)
795 printf("[%d] Setting timers for state\n", sp->sm_num);
797 ipf_state_setqueue(softc, is, sp->sm_rev);
799 MUTEX_EXIT(&is->is_lock);
809 RWLOCK_EXIT(&softc->ipf_state);
810 RWLOCK_EXIT(&softs->ipf_syncstate);
813 if (softs->ipf_sync_debug > 6)
814 printf("[%d] Update completed with error %d\n",
821 /* ------------------------------------------------------------------------ */
822 /* Function: ipf_sync_del */
824 /* Parameters: sl(I) - pointer to synclist object to delete */
826 /* Deletes an object from the synclist. */
827 /* ------------------------------------------------------------------------ */
829 ipf_sync_del(softs, sl)
830 ipf_sync_softc_t *softs;
833 *sl->sl_pnext = sl->sl_next;
834 if (sl->sl_next != NULL)
835 sl->sl_next->sl_pnext = sl->sl_pnext;
836 if (sl->sl_idx != -1)
837 softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
841 /* ------------------------------------------------------------------------ */
842 /* Function: ipf_sync_del_state */
844 /* Parameters: sl(I) - pointer to synclist object to delete */
846 /* Deletes an object from the synclist state table and free's its memory. */
847 /* ------------------------------------------------------------------------ */
849 ipf_sync_del_state(arg, sl)
853 ipf_sync_softc_t *softs = arg;
855 WRITE_ENTER(&softs->ipf_syncstate);
856 ipf_sync_del(softs, sl);
857 RWLOCK_EXIT(&softs->ipf_syncstate);
862 /* ------------------------------------------------------------------------ */
863 /* Function: ipf_sync_del_nat */
865 /* Parameters: sl(I) - pointer to synclist object to delete */
867 /* Deletes an object from the synclist nat table and free's its memory. */
868 /* ------------------------------------------------------------------------ */
870 ipf_sync_del_nat(arg, sl)
874 ipf_sync_softc_t *softs = arg;
876 WRITE_ENTER(&softs->ipf_syncnat);
877 ipf_sync_del(softs, sl);
878 RWLOCK_EXIT(&softs->ipf_syncnat);
883 /* ------------------------------------------------------------------------ */
884 /* Function: ipf_sync_nat */
885 /* Returns: int - 0 == success, else error value. */
886 /* Parameters: sp(I) - pointer to sync packet data header */
887 /* uio(I) - pointer to user data for further information */
889 /* Updates the NAT table according to information passed in the sync */
890 /* header. As required, more data is fetched from the uio structure but */
891 /* varies depending on the contents of the sync header. This function can */
892 /* create a new NAT entry or update one. Deletion is left to the NAT */
893 /* structures being timed out correctly. */
894 /* ------------------------------------------------------------------------ */
896 ipf_sync_nat(softc, sp, data)
897 ipf_main_softc_t *softc;
901 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
908 READ_ENTER(&softs->ipf_syncnat);
920 KMALLOC(sl, synclist_t *);
929 bzero((char *)n, offsetof(nat_t, nat_age));
930 bcopy((char *)&nat->nat_age, (char *)&n->nat_age,
931 sizeof(*n) - offsetof(nat_t, nat_age));
932 ipf_sync_natorder(0, n);
934 n->nat_rev = sl->sl_rev;
938 sl->sl_num = ntohl(sp->sm_num);
940 WRITE_ENTER(&softc->ipf_nat);
941 sl->sl_pnext = softs->syncnattab + hv;
942 sl->sl_next = softs->syncnattab[hv];
943 if (softs->syncnattab[hv] != NULL)
944 softs->syncnattab[hv]->sl_pnext = &sl->sl_next;
945 softs->syncnattab[hv] = sl;
946 (void) ipf_nat_insert(softc, softc->ipf_nat_soft, n);
947 RWLOCK_EXIT(&softc->ipf_nat);
951 bcopy(data, &su, sizeof(su));
953 for (sl = softs->syncnattab[hv]; (sl != NULL);
955 if (sl->sl_hdr.sm_num == sp->sm_num)
963 READ_ENTER(&softc->ipf_nat);
966 nat->nat_rev = sl->sl_rev;
968 MUTEX_ENTER(&nat->nat_lock);
969 ipf_nat_setqueue(softc, softc->ipf_nat_soft, nat);
970 MUTEX_EXIT(&nat->nat_lock);
972 RWLOCK_EXIT(&softc->ipf_nat);
982 RWLOCK_EXIT(&softs->ipf_syncnat);
987 /* ------------------------------------------------------------------------ */
988 /* Function: ipf_sync_new */
989 /* Returns: synclist_t* - NULL == failure, else pointer to new synclist */
990 /* data structure. */
991 /* Parameters: tab(I) - type of synclist_t to create */
992 /* fin(I) - pointer to packet information */
993 /* ptr(I) - pointer to owning object */
995 /* Creates a new sync table entry and notifies any sleepers that it's there */
996 /* waiting to be processed. */
997 /* ------------------------------------------------------------------------ */
999 ipf_sync_new(softc, tab, fin, ptr)
1000 ipf_main_softc_t *softc;
1005 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1006 synclist_t *sl, *ss;
1010 if (softs->sl_idx == softs->ipf_sync_log_sz)
1012 KMALLOC(sl, synclist_t *);
1016 MUTEX_ENTER(&softs->ipf_syncadd);
1018 * Get a unique number for this synclist_t. The number is only meant
1019 * to be unique for the lifetime of the structure and may be reused
1022 softs->ipf_sync_num++;
1023 if (softs->ipf_sync_num == 0) {
1024 softs->ipf_sync_num = 1;
1025 softs->ipf_sync_wrap++;
1029 * Use the synch number of the object as the hash key. Should end up
1030 * with relatively even distribution over time.
1031 * XXX - an attacker could lunch an DoS attack, of sorts, if they are
1032 * the only one causing new table entries by only keeping open every
1033 * nth connection they make, where n is a value in the interval
1034 * [0, SYNC_STATETABSZ-1].
1039 hv = softs->ipf_sync_num & (softs->ipf_sync_state_tab_sz - 1);
1040 while (softs->ipf_sync_wrap != 0) {
1041 for (ss = softs->syncstatetab[hv]; ss; ss = ss->sl_next)
1042 if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
1046 softs->ipf_sync_num++;
1047 hv = softs->ipf_sync_num &
1048 (softs->ipf_sync_state_tab_sz - 1);
1050 sl->sl_pnext = softs->syncstatetab + hv;
1051 sl->sl_next = softs->syncstatetab[hv];
1052 softs->syncstatetab[hv] = sl;
1056 hv = softs->ipf_sync_num & (softs->ipf_sync_nat_tab_sz - 1);
1057 while (softs->ipf_sync_wrap != 0) {
1058 for (ss = softs->syncnattab[hv]; ss; ss = ss->sl_next)
1059 if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
1063 softs->ipf_sync_num++;
1064 hv = softs->ipf_sync_num &
1065 (softs->ipf_sync_nat_tab_sz - 1);
1067 sl->sl_pnext = softs->syncnattab + hv;
1068 sl->sl_next = softs->syncnattab[hv];
1069 softs->syncnattab[hv] = sl;
1076 sl->sl_num = softs->ipf_sync_num;
1077 MUTEX_EXIT(&softs->ipf_syncadd);
1079 sl->sl_magic = htonl(SYNHDRMAGIC);
1080 sl->sl_v = fin->fin_v;
1081 sl->sl_p = fin->fin_p;
1082 sl->sl_cmd = SMC_CREATE;
1085 sl->sl_rev = fin->fin_rev;
1086 if (tab == SMC_STATE) {
1088 sz = sizeof(*sl->sl_ips);
1089 } else if (tab == SMC_NAT) {
1091 sz = sizeof(*sl->sl_ipn);
1099 * Create the log entry to be read by a user daemon. When it has been
1100 * finished and put on the queue, send a signal to wakeup any waiters.
1102 MUTEX_ENTER(&softs->ipf_syncadd);
1103 sle = softs->synclog + softs->sl_idx++;
1104 bcopy((char *)&sl->sl_hdr, (char *)&sle->sle_hdr,
1105 sizeof(sle->sle_hdr));
1106 sle->sle_hdr.sm_num = htonl(sle->sle_hdr.sm_num);
1107 sle->sle_hdr.sm_len = htonl(sle->sle_hdr.sm_len);
1109 bcopy((char *)ptr, (char *)&sle->sle_un, sz);
1110 if (tab == SMC_STATE) {
1111 ipf_sync_storder(1, &sle->sle_un.sleu_ips);
1112 } else if (tab == SMC_NAT) {
1113 ipf_sync_natorder(1, &sle->sle_un.sleu_ipn);
1116 MUTEX_EXIT(&softs->ipf_syncadd);
1118 ipf_sync_wakeup(softc);
1123 /* ------------------------------------------------------------------------ */
1124 /* Function: ipf_sync_update */
1126 /* Parameters: tab(I) - type of synclist_t to create */
1127 /* fin(I) - pointer to packet information */
1128 /* sl(I) - pointer to synchronisation object */
1130 /* For outbound packets, only, create an sync update record for the user */
1131 /* process to read. */
1132 /* ------------------------------------------------------------------------ */
1134 ipf_sync_update(softc, tab, fin, sl)
1135 ipf_main_softc_t *softc;
1140 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1141 synctcp_update_t *st;
1147 if (fin->fin_out == 0 || sl == NULL)
1150 if (tab == SMC_STATE) {
1151 lock = &softs->ipf_syncstate;
1153 lock = &softs->ipf_syncnat;
1157 if (sl->sl_idx == -1) {
1158 MUTEX_ENTER(&softs->ipf_syncadd);
1159 slu = softs->syncupd + softs->su_idx;
1160 sl->sl_idx = softs->su_idx++;
1161 MUTEX_EXIT(&softs->ipf_syncadd);
1163 bcopy((char *)&sl->sl_hdr, (char *)&slu->sup_hdr,
1164 sizeof(slu->sup_hdr));
1165 slu->sup_hdr.sm_magic = htonl(SYNHDRMAGIC);
1166 slu->sup_hdr.sm_sl = sl;
1167 slu->sup_hdr.sm_cmd = SMC_UPDATE;
1168 slu->sup_hdr.sm_table = tab;
1169 slu->sup_hdr.sm_num = htonl(sl->sl_num);
1170 slu->sup_hdr.sm_len = htonl(sizeof(struct synctcp_update));
1171 slu->sup_hdr.sm_rev = fin->fin_rev;
1173 if (fin->fin_p == IPPROTO_TCP) {
1179 slu = softs->syncupd + sl->sl_idx;
1182 * Only TCP has complex timeouts, others just use default timeouts.
1183 * For TCP, we only need to track the connection state and window.
1185 if (fin->fin_p == IPPROTO_TCP) {
1187 if (tab == SMC_STATE) {
1189 st->stu_age = htonl(ips->is_die);
1190 st->stu_data[0].td_end = ips->is_send;
1191 st->stu_data[0].td_maxend = ips->is_maxsend;
1192 st->stu_data[0].td_maxwin = ips->is_maxswin;
1193 st->stu_state[0] = ips->is_state[0];
1194 st->stu_data[1].td_end = ips->is_dend;
1195 st->stu_data[1].td_maxend = ips->is_maxdend;
1196 st->stu_data[1].td_maxwin = ips->is_maxdwin;
1197 st->stu_state[1] = ips->is_state[1];
1198 } else if (tab == SMC_NAT) {
1200 st->stu_age = htonl(nat->nat_age);
1205 ipf_sync_wakeup(softc);
1209 /* ------------------------------------------------------------------------ */
1210 /* Function: ipf_sync_flush_table */
1211 /* Returns: int - number of entries freed by flushing table */
1212 /* Parameters: tabsize(I) - size of the array pointed to by table */
1213 /* table(I) - pointer to sync table to empty */
1215 /* Walk through a table of sync entries and free each one. It is assumed */
1216 /* that some lock is held so that nobody else tries to access the table */
1217 /* during this cleanup. */
1218 /* ------------------------------------------------------------------------ */
1220 ipf_sync_flush_table(softs, tabsize, table)
1221 ipf_sync_softc_t *softs;
1230 for (i = 0; i < tabsize; i++) {
1231 while ((sl = table[i]) != NULL) {
1232 switch (sl->sl_table) {
1234 if (sl->sl_ips != NULL)
1235 sl->sl_ips->is_sync = NULL;
1238 if (sl->sl_ipn != NULL)
1239 sl->sl_ipn->nat_sync = NULL;
1242 if (sl->sl_next != NULL)
1243 sl->sl_next->sl_pnext = sl->sl_pnext;
1244 table[i] = sl->sl_next;
1245 if (sl->sl_idx != -1)
1246 softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
1256 /* ------------------------------------------------------------------------ */
1257 /* Function: ipf_sync_ioctl */
1258 /* Returns: int - 0 == success, != 0 == failure */
1259 /* Parameters: data(I) - pointer to ioctl data */
1260 /* cmd(I) - ioctl command integer */
1261 /* mode(I) - file mode bits used with open */
1263 /* This function currently does not handle any ioctls and so just returns */
1264 /* EINVAL on all occasions. */
1265 /* ------------------------------------------------------------------------ */
1267 ipf_sync_ioctl(softc, data, cmd, mode, uid, ctx)
1268 ipf_main_softc_t *softc;
1274 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1281 error = BCOPYIN(data, &i, sizeof(i));
1292 MUTEX_ENTER(&softs->ipsl_mutex);
1293 i = (softs->sl_tail - softs->sl_idx) +
1294 (softs->su_tail - softs->su_idx);
1299 MUTEX_EXIT(&softs->ipsl_mutex);
1305 WRITE_ENTER(&softs->ipf_syncnat);
1306 i = ipf_sync_flush_table(softs, SYNC_NATTABSZ,
1308 RWLOCK_EXIT(&softs->ipf_syncnat);
1314 WRITE_ENTER(&softs->ipf_syncstate);
1315 i = ipf_sync_flush_table(softs, SYNC_STATETABSZ,
1316 softs->syncstatetab);
1317 RWLOCK_EXIT(&softs->ipf_syncstate);
1322 error = BCOPYOUT(&i, data, sizeof(i));
1339 /* ------------------------------------------------------------------------ */
1340 /* Function: ipf_sync_canread */
1341 /* Returns: int - 0 == success, != 0 == failure */
1342 /* Parameters: Nil */
1344 /* This function provides input to the poll handler about whether or not */
1345 /* there is data waiting to be read from the /dev/ipsync device. */
1346 /* ------------------------------------------------------------------------ */
1348 ipf_sync_canread(arg)
1351 ipf_sync_softc_t *softs = arg;
1352 return !((softs->sl_tail == softs->sl_idx) &&
1353 (softs->su_tail == softs->su_idx));
1357 /* ------------------------------------------------------------------------ */
1358 /* Function: ipf_sync_canwrite */
1359 /* Returns: int - 1 == can always write */
1360 /* Parameters: Nil */
1362 /* This function lets the poll handler know that it is always ready willing */
1363 /* to accept write events. */
1364 /* XXX Maybe this should return false if the sync table is full? */
1365 /* ------------------------------------------------------------------------ */
1367 ipf_sync_canwrite(arg)
1374 /* ------------------------------------------------------------------------ */
1375 /* Function: ipf_sync_wakeup */
1376 /* Parameters: Nil */
1379 /* This function implements the heuristics that decide how often to */
1380 /* generate a poll wakeup for programs that are waiting for information */
1381 /* about when they can do a read on /dev/ipsync. */
1383 /* There are three different considerations here: */
1384 /* - do not keep a program waiting too long: ipf_sync_wake_interval is the */
1385 /* maximum number of ipf ticks to let pass by; */
1386 /* - do not let the queue of ouststanding things to generate notifies for */
1387 /* get too full (ipf_sync_queue_high_wm is the high water mark); */
1388 /* - do not let too many events get collapsed in before deciding that the */
1389 /* other host(s) need an update (ipf_sync_event_high_wm is the high water */
1390 /* mark for this counter.) */
1391 /* ------------------------------------------------------------------------ */
1393 ipf_sync_wakeup(softc)
1394 ipf_main_softc_t *softc;
1396 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1398 softs->ipf_sync_events++;
1399 if ((softc->ipf_ticks >
1400 softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval) ||
1401 (softs->ipf_sync_events > softs->ipf_sync_event_high_wm) ||
1402 ((softs->sl_tail - softs->sl_idx) >
1403 softs->ipf_sync_queue_high_wm) ||
1404 ((softs->su_tail - softs->su_idx) >
1405 softs->ipf_sync_queue_high_wm)) {
1407 ipf_sync_poll_wakeup(softc);
1412 /* ------------------------------------------------------------------------ */
1413 /* Function: ipf_sync_poll_wakeup */
1414 /* Parameters: Nil */
1417 /* Deliver a poll wakeup and reset counters for two of the three heuristics */
1418 /* ------------------------------------------------------------------------ */
1420 ipf_sync_poll_wakeup(softc)
1421 ipf_main_softc_t *softc;
1423 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1425 softs->ipf_sync_events = 0;
1426 softs->ipf_sync_lastwakeup = softc->ipf_ticks;
1430 MUTEX_ENTER(&softs->ipsl_mutex);
1431 cv_signal(&softs->ipslwait);
1432 MUTEX_EXIT(&softs->ipsl_mutex);
1433 pollwakeup(&softc->ipf_poll_head[IPL_LOGSYNC], POLLIN|POLLRDNORM);
1435 WAKEUP(&softs->sl_tail, 0);
1436 POLLWAKEUP(IPL_LOGSYNC);
1442 /* ------------------------------------------------------------------------ */
1443 /* Function: ipf_sync_expire */
1444 /* Parameters: Nil */
1447 /* This is the function called even ipf_tick. It implements one of the */
1448 /* three heuristics above *IF* there are events waiting. */
1449 /* ------------------------------------------------------------------------ */
1451 ipf_sync_expire(softc)
1452 ipf_main_softc_t *softc;
1454 ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1456 if ((softs->ipf_sync_events > 0) &&
1458 softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval)) {
1459 ipf_sync_poll_wakeup(softc);