xref: /netbsd/external/bsd/ipf/dist/ip_sync.c (revision f52ace7a)
1*f52ace7aSdarrenr /*	$NetBSD: ip_sync.c,v 1.2 2012/07/22 14:27:35 darrenr Exp $	*/
226945a25Schristos 
326945a25Schristos /*
4c50c2f6fSdarrenr  * Copyright (C) 2012 by Darren Reed.
526945a25Schristos  *
626945a25Schristos  * See the IPFILTER.LICENCE file for details on licencing.
726945a25Schristos  */
826945a25Schristos #if defined(KERNEL) || defined(_KERNEL)
926945a25Schristos # undef KERNEL
1026945a25Schristos # undef _KERNEL
1126945a25Schristos # define        KERNEL	1
1226945a25Schristos # define        _KERNEL	1
1326945a25Schristos #endif
1426945a25Schristos #include <sys/errno.h>
1526945a25Schristos #include <sys/types.h>
1626945a25Schristos #include <sys/param.h>
1726945a25Schristos #include <sys/file.h>
1826945a25Schristos #if !defined(_KERNEL) && !defined(__KERNEL__)
1926945a25Schristos # include <stdio.h>
2026945a25Schristos # include <stdlib.h>
2126945a25Schristos # include <string.h>
2226945a25Schristos # define _KERNEL
2326945a25Schristos # define KERNEL
2426945a25Schristos # ifdef __OpenBSD__
2526945a25Schristos struct file;
2626945a25Schristos # endif
2726945a25Schristos # include <sys/uio.h>
2826945a25Schristos # undef _KERNEL
2926945a25Schristos # undef KERNEL
3026945a25Schristos #else
3126945a25Schristos # include <sys/systm.h>
3226945a25Schristos # if !defined(__SVR4) && !defined(__svr4__)
3326945a25Schristos #  include <sys/mbuf.h>
3426945a25Schristos # endif
3526945a25Schristos # include <sys/select.h>
3626945a25Schristos # if __FreeBSD_version >= 500000
3726945a25Schristos #  include <sys/selinfo.h>
3826945a25Schristos # endif
3926945a25Schristos #endif
4026945a25Schristos #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
4126945a25Schristos # include <sys/proc.h>
4226945a25Schristos #endif
4326945a25Schristos #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
4426945a25Schristos # include <sys/filio.h>
4526945a25Schristos # include <sys/fcntl.h>
4626945a25Schristos #else
4726945a25Schristos # include <sys/ioctl.h>
4826945a25Schristos #endif
4926945a25Schristos #include <sys/time.h>
5026945a25Schristos #if !defined(linux)
5126945a25Schristos # include <sys/protosw.h>
5226945a25Schristos #endif
5326945a25Schristos #include <sys/socket.h>
5426945a25Schristos #if defined(__SVR4) || defined(__svr4__)
5526945a25Schristos # include <sys/filio.h>
5626945a25Schristos # include <sys/byteorder.h>
5726945a25Schristos # ifdef _KERNEL
5826945a25Schristos #  include <sys/dditypes.h>
5926945a25Schristos # endif
6026945a25Schristos # include <sys/stream.h>
6126945a25Schristos # include <sys/kmem.h>
6226945a25Schristos #endif
6326945a25Schristos 
6426945a25Schristos #include <net/if.h>
6526945a25Schristos #ifdef sun
6626945a25Schristos # include <net/af.h>
6726945a25Schristos #endif
6826945a25Schristos #include <netinet/in.h>
6926945a25Schristos #include <netinet/in_systm.h>
7026945a25Schristos #include <netinet/ip.h>
7126945a25Schristos #include <netinet/tcp.h>
7226945a25Schristos #if !defined(linux)
7326945a25Schristos # include <netinet/ip_var.h>
7426945a25Schristos #endif
7526945a25Schristos #if !defined(__hpux) && !defined(linux)
7626945a25Schristos # include <netinet/tcp_fsm.h>
7726945a25Schristos #endif
7826945a25Schristos #include <netinet/udp.h>
7926945a25Schristos #include <netinet/ip_icmp.h>
8026945a25Schristos #include "netinet/ip_compat.h"
8126945a25Schristos #include <netinet/tcpip.h>
8226945a25Schristos #include "netinet/ip_fil.h"
8326945a25Schristos #include "netinet/ip_nat.h"
8426945a25Schristos #include "netinet/ip_frag.h"
8526945a25Schristos #include "netinet/ip_state.h"
8626945a25Schristos #include "netinet/ip_proxy.h"
8726945a25Schristos #include "netinet/ip_sync.h"
8826945a25Schristos #ifdef  USE_INET6
8926945a25Schristos #include <netinet/icmp6.h>
9026945a25Schristos #endif
9126945a25Schristos #if (__FreeBSD_version >= 300000)
9226945a25Schristos # include <sys/malloc.h>
9326945a25Schristos # if defined(_KERNEL) && !defined(IPFILTER_LKM)
9426945a25Schristos #  include <sys/libkern.h>
9526945a25Schristos #  include <sys/systm.h>
9626945a25Schristos # endif
9726945a25Schristos #endif
9826945a25Schristos /* END OF INCLUDES */
9926945a25Schristos 
10026945a25Schristos #if !defined(lint)
101*f52ace7aSdarrenr static const char rcsid[] = "@(#)Id: ip_sync.c,v 1.1.1.2 2012/07/22 13:44:24 darrenr Exp $";
10226945a25Schristos #endif
10326945a25Schristos 
10426945a25Schristos #define	SYNC_STATETABSZ	256
10526945a25Schristos #define	SYNC_NATTABSZ	256
10626945a25Schristos 
10726945a25Schristos typedef struct ipf_sync_softc_s {
10826945a25Schristos 	ipfmutex_t	ipf_syncadd;
10926945a25Schristos 	ipfmutex_t	ipsl_mutex;
11026945a25Schristos 	ipfrwlock_t	ipf_syncstate;
11126945a25Schristos 	ipfrwlock_t	ipf_syncnat;
11226945a25Schristos #if SOLARIS && defined(_KERNEL)
11326945a25Schristos 	kcondvar_t	ipslwait;
11426945a25Schristos #endif
11526945a25Schristos #if defined(linux) && defined(_KERNEL)
11626945a25Schristos 	wait_queue_head_t	sl_tail_linux;
11726945a25Schristos #endif
11826945a25Schristos 	synclist_t	**syncstatetab;
11926945a25Schristos 	synclist_t	**syncnattab;
12026945a25Schristos 	synclogent_t	*synclog;
12126945a25Schristos 	syncupdent_t	*syncupd;
12226945a25Schristos 	u_int		ipf_sync_num;
12326945a25Schristos 	u_int		ipf_sync_wrap;
12426945a25Schristos 	u_int		sl_idx;		/* next available sync log entry */
12526945a25Schristos 	u_int		su_idx;		/* next available sync update entry */
12626945a25Schristos 	u_int		sl_tail;	/* next sync log entry to read */
12726945a25Schristos 	u_int		su_tail;	/* next sync update entry to read */
12826945a25Schristos 	int		ipf_sync_log_sz;
12926945a25Schristos 	int		ipf_sync_nat_tab_sz;
13026945a25Schristos 	int		ipf_sync_state_tab_sz;
13126945a25Schristos 	int		ipf_sync_debug;
13226945a25Schristos 	int		ipf_sync_events;
13326945a25Schristos 	u_32_t		ipf_sync_lastwakeup;
13426945a25Schristos 	int		ipf_sync_wake_interval;
13526945a25Schristos 	int		ipf_sync_event_high_wm;
13626945a25Schristos 	int		ipf_sync_queue_high_wm;
13726945a25Schristos 	int		ipf_sync_inited;
13826945a25Schristos } ipf_sync_softc_t;
13926945a25Schristos 
14026945a25Schristos static int ipf_sync_flush_table __P((ipf_sync_softc_t *, int, synclist_t **));
14126945a25Schristos static void ipf_sync_wakeup __P((ipf_main_softc_t *));
14226945a25Schristos static void ipf_sync_del __P((ipf_sync_softc_t *, synclist_t *));
14326945a25Schristos static void ipf_sync_poll_wakeup __P((ipf_main_softc_t *));
14426945a25Schristos static int ipf_sync_nat __P((ipf_main_softc_t *, synchdr_t *, void *));
14526945a25Schristos static int ipf_sync_state __P((ipf_main_softc_t *, synchdr_t *, void *));
14626945a25Schristos 
14726945a25Schristos # if !defined(sparc) && !defined(__hppa)
14826945a25Schristos void ipf_sync_tcporder __P((int, struct tcpdata *));
14926945a25Schristos void ipf_sync_natorder __P((int, struct nat *));
15026945a25Schristos void ipf_sync_storder __P((int, struct ipstate *));
15126945a25Schristos # endif
15226945a25Schristos 
15326945a25Schristos 
15426945a25Schristos void *
ipf_sync_soft_create(softc)15526945a25Schristos ipf_sync_soft_create(softc)
15626945a25Schristos 	ipf_main_softc_t *softc;
15726945a25Schristos {
15826945a25Schristos 	ipf_sync_softc_t *softs;
15926945a25Schristos 
16026945a25Schristos 	KMALLOC(softs, ipf_sync_softc_t *);
161c50c2f6fSdarrenr 	if (softs == NULL) {
162c50c2f6fSdarrenr 		IPFERROR(110024);
16326945a25Schristos 		return NULL;
164c50c2f6fSdarrenr 	}
16526945a25Schristos 
16626945a25Schristos 	bzero((char *)softs, sizeof(*softs));
16726945a25Schristos 
16826945a25Schristos 	softs->ipf_sync_log_sz = SYNCLOG_SZ;
16926945a25Schristos 	softs->ipf_sync_nat_tab_sz = SYNC_STATETABSZ;
17026945a25Schristos 	softs->ipf_sync_state_tab_sz = SYNC_STATETABSZ;
17126945a25Schristos 	softs->ipf_sync_event_high_wm = SYNCLOG_SZ * 100 / 90;	/* 90% */
17226945a25Schristos 	softs->ipf_sync_queue_high_wm = SYNCLOG_SZ * 100 / 90;	/* 90% */
17326945a25Schristos 
17426945a25Schristos 	return softs;
17526945a25Schristos }
17626945a25Schristos 
17726945a25Schristos 
17826945a25Schristos /* ------------------------------------------------------------------------ */
17926945a25Schristos /* Function:    ipf_sync_init                                               */
18026945a25Schristos /* Returns:     int - 0 == success, -1 == failure                           */
18126945a25Schristos /* Parameters:  Nil                                                         */
18226945a25Schristos /*                                                                          */
18326945a25Schristos /* Initialise all of the locks required for the sync code and initialise    */
18426945a25Schristos /* any data structures, as required.                                        */
18526945a25Schristos /* ------------------------------------------------------------------------ */
18626945a25Schristos int
ipf_sync_soft_init(softc,arg)18726945a25Schristos ipf_sync_soft_init(softc, arg)
18826945a25Schristos 	ipf_main_softc_t *softc;
18926945a25Schristos 	void *arg;
19026945a25Schristos {
19126945a25Schristos 	ipf_sync_softc_t *softs = arg;
19226945a25Schristos 
19326945a25Schristos 	KMALLOCS(softs->synclog, synclogent_t *,
19426945a25Schristos 		 softs->ipf_sync_log_sz * sizeof(*softs->synclog));
19526945a25Schristos 	if (softs->synclog == NULL)
19626945a25Schristos 		return -1;
19726945a25Schristos 	bzero((char *)softs->synclog,
19826945a25Schristos 	      softs->ipf_sync_log_sz * sizeof(*softs->synclog));
19926945a25Schristos 
20026945a25Schristos 	KMALLOCS(softs->syncupd, syncupdent_t *,
20126945a25Schristos 		 softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
20226945a25Schristos 	if (softs->syncupd == NULL)
20326945a25Schristos 		return -2;
20426945a25Schristos 	bzero((char *)softs->syncupd,
20526945a25Schristos 	      softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
20626945a25Schristos 
20726945a25Schristos 	KMALLOCS(softs->syncstatetab, synclist_t **,
20826945a25Schristos 		 softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
20926945a25Schristos 	if (softs->syncstatetab == NULL)
21026945a25Schristos 		return -3;
21126945a25Schristos 	bzero((char *)softs->syncstatetab,
21226945a25Schristos 	      softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
21326945a25Schristos 
21426945a25Schristos 	KMALLOCS(softs->syncnattab, synclist_t **,
21526945a25Schristos 		 softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
21626945a25Schristos 	if (softs->syncnattab == NULL)
21726945a25Schristos 		return -3;
21826945a25Schristos 	bzero((char *)softs->syncnattab,
21926945a25Schristos 	      softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
22026945a25Schristos 
22126945a25Schristos 	softs->ipf_sync_num = 1;
22226945a25Schristos 	softs->ipf_sync_wrap = 0;
22326945a25Schristos 	softs->sl_idx = 0;
22426945a25Schristos 	softs->su_idx = 0;
22526945a25Schristos 	softs->sl_tail = 0;
22626945a25Schristos 	softs->su_tail = 0;
22726945a25Schristos 	softs->ipf_sync_events = 0;
22826945a25Schristos 	softs->ipf_sync_lastwakeup = 0;
22926945a25Schristos 
23026945a25Schristos 
23126945a25Schristos # if SOLARIS && defined(_KERNEL)
23226945a25Schristos 	cv_init(&softs->ipslwait, "ipsl condvar", CV_DRIVER, NULL);
23326945a25Schristos # endif
23426945a25Schristos 	RWLOCK_INIT(&softs->ipf_syncstate, "add things to state sync table");
23526945a25Schristos 	RWLOCK_INIT(&softs->ipf_syncnat, "add things to nat sync table");
23626945a25Schristos 	MUTEX_INIT(&softs->ipf_syncadd, "add things to sync table");
23726945a25Schristos 	MUTEX_INIT(&softs->ipsl_mutex, "read ring lock");
23826945a25Schristos 
23926945a25Schristos 	softs->ipf_sync_inited = 1;
24026945a25Schristos 
24126945a25Schristos 	return 0;
24226945a25Schristos }
24326945a25Schristos 
24426945a25Schristos 
24526945a25Schristos /* ------------------------------------------------------------------------ */
24626945a25Schristos /* Function:    ipf_sync_unload                                             */
24726945a25Schristos /* Returns:     int - 0 == success, -1 == failure                           */
24826945a25Schristos /* Parameters:  Nil                                                         */
24926945a25Schristos /*                                                                          */
25026945a25Schristos /* Destroy the locks created when initialising and free any memory in use   */
25126945a25Schristos /* with the synchronisation tables.                                         */
25226945a25Schristos /* ------------------------------------------------------------------------ */
25326945a25Schristos int
ipf_sync_soft_fini(softc,arg)25426945a25Schristos ipf_sync_soft_fini(softc, arg)
25526945a25Schristos 	ipf_main_softc_t *softc;
25626945a25Schristos 	void *arg;
25726945a25Schristos {
25826945a25Schristos 	ipf_sync_softc_t *softs = arg;
25926945a25Schristos 
26026945a25Schristos 	if (softs->syncnattab != NULL) {
26126945a25Schristos 		ipf_sync_flush_table(softs, softs->ipf_sync_nat_tab_sz,
26226945a25Schristos 				     softs->syncnattab);
26326945a25Schristos 		KFREES(softs->syncnattab,
26426945a25Schristos 		       softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
26526945a25Schristos 		softs->syncnattab = NULL;
26626945a25Schristos 	}
26726945a25Schristos 
26826945a25Schristos 	if (softs->syncstatetab != NULL) {
26926945a25Schristos 		ipf_sync_flush_table(softs, softs->ipf_sync_state_tab_sz,
27026945a25Schristos 				     softs->syncstatetab);
27126945a25Schristos 		KFREES(softs->syncstatetab,
27226945a25Schristos 		       softs->ipf_sync_state_tab_sz *
27326945a25Schristos 		       sizeof(*softs->syncstatetab));
27426945a25Schristos 		softs->syncstatetab = NULL;
27526945a25Schristos 	}
27626945a25Schristos 
27726945a25Schristos 	if (softs->syncupd != NULL) {
27826945a25Schristos 		KFREES(softs->syncupd,
27926945a25Schristos 		       softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
28026945a25Schristos 		softs->syncupd = NULL;
28126945a25Schristos 	}
28226945a25Schristos 
28326945a25Schristos 	if (softs->synclog != NULL) {
28426945a25Schristos 		KFREES(softs->synclog,
28526945a25Schristos 		       softs->ipf_sync_log_sz * sizeof(*softs->synclog));
28626945a25Schristos 		softs->synclog = NULL;
28726945a25Schristos 	}
28826945a25Schristos 
28926945a25Schristos 	if (softs->ipf_sync_inited == 1) {
29026945a25Schristos 		MUTEX_DESTROY(&softs->ipsl_mutex);
29126945a25Schristos 		MUTEX_DESTROY(&softs->ipf_syncadd);
29226945a25Schristos 		RW_DESTROY(&softs->ipf_syncnat);
29326945a25Schristos 		RW_DESTROY(&softs->ipf_syncstate);
29426945a25Schristos 		softs->ipf_sync_inited = 0;
29526945a25Schristos 	}
29626945a25Schristos 
29726945a25Schristos 	return 0;
29826945a25Schristos }
29926945a25Schristos 
30026945a25Schristos void
ipf_sync_soft_destroy(softc,arg)30126945a25Schristos ipf_sync_soft_destroy(softc, arg)
30226945a25Schristos 	ipf_main_softc_t *softc;
30326945a25Schristos 	void *arg;
30426945a25Schristos {
30526945a25Schristos 	ipf_sync_softc_t *softs = arg;
30626945a25Schristos 
30726945a25Schristos 	KFREE(softs);
30826945a25Schristos }
30926945a25Schristos 
31026945a25Schristos 
31126945a25Schristos # if !defined(sparc) && !defined(__hppa)
31226945a25Schristos /* ------------------------------------------------------------------------ */
31326945a25Schristos /* Function:    ipf_sync_tcporder                                           */
31426945a25Schristos /* Returns:     Nil                                                         */
31526945a25Schristos /* Parameters:  way(I) - direction of byte order conversion.                */
31626945a25Schristos /*              td(IO) - pointer to data to be converted.                   */
31726945a25Schristos /*                                                                          */
31826945a25Schristos /* Do byte swapping on values in the TCP state information structure that   */
31926945a25Schristos /* need to be used at both ends by the host in their native byte order.     */
32026945a25Schristos /* ------------------------------------------------------------------------ */
32126945a25Schristos void
ipf_sync_tcporder(way,td)32226945a25Schristos ipf_sync_tcporder(way, td)
32326945a25Schristos 	int way;
32426945a25Schristos 	tcpdata_t *td;
32526945a25Schristos {
32626945a25Schristos 	if (way) {
32726945a25Schristos 		td->td_maxwin = htons(td->td_maxwin);
32826945a25Schristos 		td->td_end = htonl(td->td_end);
32926945a25Schristos 		td->td_maxend = htonl(td->td_maxend);
33026945a25Schristos 	} else {
33126945a25Schristos 		td->td_maxwin = ntohs(td->td_maxwin);
33226945a25Schristos 		td->td_end = ntohl(td->td_end);
33326945a25Schristos 		td->td_maxend = ntohl(td->td_maxend);
33426945a25Schristos 	}
33526945a25Schristos }
33626945a25Schristos 
33726945a25Schristos 
33826945a25Schristos /* ------------------------------------------------------------------------ */
33926945a25Schristos /* Function:    ipf_sync_natorder                                           */
34026945a25Schristos /* Returns:     Nil                                                         */
34126945a25Schristos /* Parameters:  way(I)  - direction of byte order conversion.               */
34226945a25Schristos /*              nat(IO) - pointer to data to be converted.                  */
34326945a25Schristos /*                                                                          */
34426945a25Schristos /* Do byte swapping on values in the NAT data structure that need to be     */
34526945a25Schristos /* used at both ends by the host in their native byte order.                */
34626945a25Schristos /* ------------------------------------------------------------------------ */
34726945a25Schristos void
ipf_sync_natorder(way,n)34826945a25Schristos ipf_sync_natorder(way, n)
34926945a25Schristos 	int way;
35026945a25Schristos 	nat_t *n;
35126945a25Schristos {
35226945a25Schristos 	if (way) {
35326945a25Schristos 		n->nat_age = htonl(n->nat_age);
35426945a25Schristos 		n->nat_flags = htonl(n->nat_flags);
35526945a25Schristos 		n->nat_ipsumd = htonl(n->nat_ipsumd);
35626945a25Schristos 		n->nat_use = htonl(n->nat_use);
35726945a25Schristos 		n->nat_dir = htonl(n->nat_dir);
35826945a25Schristos 	} else {
35926945a25Schristos 		n->nat_age = ntohl(n->nat_age);
36026945a25Schristos 		n->nat_flags = ntohl(n->nat_flags);
36126945a25Schristos 		n->nat_ipsumd = ntohl(n->nat_ipsumd);
36226945a25Schristos 		n->nat_use = ntohl(n->nat_use);
36326945a25Schristos 		n->nat_dir = ntohl(n->nat_dir);
36426945a25Schristos 	}
36526945a25Schristos }
36626945a25Schristos 
36726945a25Schristos 
36826945a25Schristos /* ------------------------------------------------------------------------ */
36926945a25Schristos /* Function:    ipf_sync_storder                                            */
37026945a25Schristos /* Returns:     Nil                                                         */
37126945a25Schristos /* Parameters:  way(I)  - direction of byte order conversion.               */
37226945a25Schristos /*              ips(IO) - pointer to data to be converted.                  */
37326945a25Schristos /*                                                                          */
37426945a25Schristos /* Do byte swapping on values in the IP state data structure that need to   */
37526945a25Schristos /* be used at both ends by the host in their native byte order.             */
37626945a25Schristos /* ------------------------------------------------------------------------ */
37726945a25Schristos void
ipf_sync_storder(way,ips)37826945a25Schristos ipf_sync_storder(way, ips)
37926945a25Schristos 	int way;
38026945a25Schristos 	ipstate_t *ips;
38126945a25Schristos {
38226945a25Schristos 	ipf_sync_tcporder(way, &ips->is_tcp.ts_data[0]);
38326945a25Schristos 	ipf_sync_tcporder(way, &ips->is_tcp.ts_data[1]);
38426945a25Schristos 
38526945a25Schristos 	if (way) {
38626945a25Schristos 		ips->is_hv = htonl(ips->is_hv);
38726945a25Schristos 		ips->is_die = htonl(ips->is_die);
38826945a25Schristos 		ips->is_pass = htonl(ips->is_pass);
38926945a25Schristos 		ips->is_flags = htonl(ips->is_flags);
39026945a25Schristos 		ips->is_opt[0] = htonl(ips->is_opt[0]);
39126945a25Schristos 		ips->is_opt[1] = htonl(ips->is_opt[1]);
39226945a25Schristos 		ips->is_optmsk[0] = htonl(ips->is_optmsk[0]);
39326945a25Schristos 		ips->is_optmsk[1] = htonl(ips->is_optmsk[1]);
39426945a25Schristos 		ips->is_sec = htons(ips->is_sec);
39526945a25Schristos 		ips->is_secmsk = htons(ips->is_secmsk);
39626945a25Schristos 		ips->is_auth = htons(ips->is_auth);
39726945a25Schristos 		ips->is_authmsk = htons(ips->is_authmsk);
39826945a25Schristos 		ips->is_s0[0] = htonl(ips->is_s0[0]);
39926945a25Schristos 		ips->is_s0[1] = htonl(ips->is_s0[1]);
40026945a25Schristos 		ips->is_smsk[0] = htons(ips->is_smsk[0]);
40126945a25Schristos 		ips->is_smsk[1] = htons(ips->is_smsk[1]);
40226945a25Schristos 	} else {
40326945a25Schristos 		ips->is_hv = ntohl(ips->is_hv);
40426945a25Schristos 		ips->is_die = ntohl(ips->is_die);
40526945a25Schristos 		ips->is_pass = ntohl(ips->is_pass);
40626945a25Schristos 		ips->is_flags = ntohl(ips->is_flags);
40726945a25Schristos 		ips->is_opt[0] = ntohl(ips->is_opt[0]);
40826945a25Schristos 		ips->is_opt[1] = ntohl(ips->is_opt[1]);
40926945a25Schristos 		ips->is_optmsk[0] = ntohl(ips->is_optmsk[0]);
41026945a25Schristos 		ips->is_optmsk[1] = ntohl(ips->is_optmsk[1]);
41126945a25Schristos 		ips->is_sec = ntohs(ips->is_sec);
41226945a25Schristos 		ips->is_secmsk = ntohs(ips->is_secmsk);
41326945a25Schristos 		ips->is_auth = ntohs(ips->is_auth);
41426945a25Schristos 		ips->is_authmsk = ntohs(ips->is_authmsk);
41526945a25Schristos 		ips->is_s0[0] = ntohl(ips->is_s0[0]);
41626945a25Schristos 		ips->is_s0[1] = ntohl(ips->is_s0[1]);
41726945a25Schristos 		ips->is_smsk[0] = ntohl(ips->is_smsk[0]);
41826945a25Schristos 		ips->is_smsk[1] = ntohl(ips->is_smsk[1]);
41926945a25Schristos 	}
42026945a25Schristos }
42126945a25Schristos # else /* !defined(sparc) && !defined(__hppa) */
42226945a25Schristos #  define	ipf_sync_tcporder(x,y)
42326945a25Schristos #  define	ipf_sync_natorder(x,y)
42426945a25Schristos #  define	ipf_sync_storder(x,y)
42526945a25Schristos # endif /* !defined(sparc) && !defined(__hppa) */
42626945a25Schristos 
42726945a25Schristos 
42826945a25Schristos /* ------------------------------------------------------------------------ */
42926945a25Schristos /* Function:    ipf_sync_write                                              */
43026945a25Schristos /* Returns:     int    - 0 == success, else error value.                    */
43126945a25Schristos /* Parameters:  uio(I) - pointer to information about data to write         */
43226945a25Schristos /*                                                                          */
43326945a25Schristos /* Moves data from user space into the kernel and uses it for updating data */
43426945a25Schristos /* structures in the state/NAT tables.                                      */
43526945a25Schristos /* ------------------------------------------------------------------------ */
43626945a25Schristos int
ipf_sync_write(softc,uio)43726945a25Schristos ipf_sync_write(softc, uio)
43826945a25Schristos 	ipf_main_softc_t *softc;
43926945a25Schristos 	struct uio *uio;
44026945a25Schristos {
44126945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
44226945a25Schristos 	synchdr_t sh;
44326945a25Schristos 
44426945a25Schristos 	/*
44526945a25Schristos 	 * THIS MUST BE SUFFICIENT LARGE TO STORE
44626945a25Schristos 	 * ANY POSSIBLE DATA TYPE
44726945a25Schristos 	 */
44826945a25Schristos 	char data[2048];
44926945a25Schristos 
45026945a25Schristos 	int err = 0;
45126945a25Schristos 
45226945a25Schristos #  if BSD_GE_YEAR(199306) || defined(__FreeBSD__) || defined(__osf__)
45326945a25Schristos 	uio->uio_rw = UIO_WRITE;
45426945a25Schristos #  endif
45526945a25Schristos 
45626945a25Schristos 	/* Try to get bytes */
45726945a25Schristos 	while (uio->uio_resid > 0) {
45826945a25Schristos 
45926945a25Schristos 		if (uio->uio_resid >= sizeof(sh)) {
46026945a25Schristos 
46126945a25Schristos 			err = UIOMOVE(&sh, sizeof(sh), UIO_WRITE, uio);
46226945a25Schristos 
46326945a25Schristos 			if (err) {
46426945a25Schristos 				if (softs->ipf_sync_debug > 2)
46526945a25Schristos 					printf("uiomove(header) failed: %d\n",
46626945a25Schristos 						err);
46726945a25Schristos 				return err;
46826945a25Schristos 			}
46926945a25Schristos 
47026945a25Schristos 			/* convert to host order */
47126945a25Schristos 			sh.sm_magic = ntohl(sh.sm_magic);
47226945a25Schristos 			sh.sm_len = ntohl(sh.sm_len);
47326945a25Schristos 			sh.sm_num = ntohl(sh.sm_num);
47426945a25Schristos 
47526945a25Schristos 			if (softs->ipf_sync_debug > 8)
47626945a25Schristos 				printf("[%d] Read v:%d p:%d cmd:%d table:%d rev:%d len:%d magic:%x\n",
47726945a25Schristos 					sh.sm_num, sh.sm_v, sh.sm_p, sh.sm_cmd,
47826945a25Schristos 					sh.sm_table, sh.sm_rev, sh.sm_len,
47926945a25Schristos 					sh.sm_magic);
48026945a25Schristos 
48126945a25Schristos 			if (sh.sm_magic != SYNHDRMAGIC) {
48226945a25Schristos 				if (softs->ipf_sync_debug > 2)
48326945a25Schristos 					printf("uiomove(header) invalid %s\n",
48426945a25Schristos 						"magic");
48526945a25Schristos 				IPFERROR(110001);
48626945a25Schristos 				return EINVAL;
48726945a25Schristos 			}
48826945a25Schristos 
48926945a25Schristos 			if (sh.sm_v != 4 && sh.sm_v != 6) {
49026945a25Schristos 				if (softs->ipf_sync_debug > 2)
49126945a25Schristos 					printf("uiomove(header) invalid %s\n",
49226945a25Schristos 						"protocol");
49326945a25Schristos 				IPFERROR(110002);
49426945a25Schristos 				return EINVAL;
49526945a25Schristos 			}
49626945a25Schristos 
49726945a25Schristos 			if (sh.sm_cmd > SMC_MAXCMD) {
49826945a25Schristos 				if (softs->ipf_sync_debug > 2)
49926945a25Schristos 					printf("uiomove(header) invalid %s\n",
50026945a25Schristos 						"command");
50126945a25Schristos 				IPFERROR(110003);
50226945a25Schristos 				return EINVAL;
50326945a25Schristos 			}
50426945a25Schristos 
50526945a25Schristos 
50626945a25Schristos 			if (sh.sm_table > SMC_MAXTBL) {
50726945a25Schristos 				if (softs->ipf_sync_debug > 2)
50826945a25Schristos 					printf("uiomove(header) invalid %s\n",
50926945a25Schristos 						"table");
51026945a25Schristos 				IPFERROR(110004);
51126945a25Schristos 				return EINVAL;
51226945a25Schristos 			}
51326945a25Schristos 
51426945a25Schristos 		} else {
51526945a25Schristos 			/* unsufficient data, wait until next call */
51626945a25Schristos 			if (softs->ipf_sync_debug > 2)
51726945a25Schristos 				printf("uiomove(header) insufficient data");
51826945a25Schristos 			IPFERROR(110005);
51926945a25Schristos 			return EAGAIN;
52026945a25Schristos 	 	}
52126945a25Schristos 
52226945a25Schristos 
52326945a25Schristos 		/*
52426945a25Schristos 		 * We have a header, so try to read the amount of data
52526945a25Schristos 		 * needed for the request
52626945a25Schristos 		 */
52726945a25Schristos 
52826945a25Schristos 		/* not supported */
52926945a25Schristos 		if (sh.sm_len == 0) {
53026945a25Schristos 			if (softs->ipf_sync_debug > 2)
53126945a25Schristos 				printf("uiomove(data zero length %s\n",
53226945a25Schristos 					"not supported");
53326945a25Schristos 			IPFERROR(110006);
53426945a25Schristos 			return EINVAL;
53526945a25Schristos 		}
53626945a25Schristos 
53726945a25Schristos 		if (uio->uio_resid >= sh.sm_len) {
53826945a25Schristos 
53926945a25Schristos 			err = UIOMOVE(data, sh.sm_len, UIO_WRITE, uio);
54026945a25Schristos 
54126945a25Schristos 			if (err) {
54226945a25Schristos 				if (softs->ipf_sync_debug > 2)
54326945a25Schristos 					printf("uiomove(data) failed: %d\n",
54426945a25Schristos 						err);
54526945a25Schristos 				return err;
54626945a25Schristos 			}
54726945a25Schristos 
54826945a25Schristos 			if (softs->ipf_sync_debug > 7)
54926945a25Schristos 				printf("uiomove(data) %d bytes read\n",
55026945a25Schristos 					sh.sm_len);
55126945a25Schristos 
55226945a25Schristos 			if (sh.sm_table == SMC_STATE)
55326945a25Schristos 				err = ipf_sync_state(softc, &sh, data);
55426945a25Schristos 			else if (sh.sm_table == SMC_NAT)
55526945a25Schristos 				err = ipf_sync_nat(softc, &sh, data);
55626945a25Schristos 			if (softs->ipf_sync_debug > 7)
55726945a25Schristos 				printf("[%d] Finished with error %d\n",
55826945a25Schristos 					sh.sm_num, err);
55926945a25Schristos 
56026945a25Schristos 		} else {
56126945a25Schristos 			/* insufficient data, wait until next call */
56226945a25Schristos 			if (softs->ipf_sync_debug > 2)
56326945a25Schristos 				printf("uiomove(data) %s %d bytes, got %d\n",
56426945a25Schristos 					"insufficient data, need",
565c50c2f6fSdarrenr 					sh.sm_len, (int)uio->uio_resid);
56626945a25Schristos 			IPFERROR(110007);
56726945a25Schristos 			return EAGAIN;
56826945a25Schristos 		}
56926945a25Schristos 	}
57026945a25Schristos 
57126945a25Schristos 	/* no more data */
57226945a25Schristos 	return 0;
57326945a25Schristos }
57426945a25Schristos 
57526945a25Schristos 
57626945a25Schristos /* ------------------------------------------------------------------------ */
57726945a25Schristos /* Function:    ipf_sync_read                                               */
57826945a25Schristos /* Returns:     int    - 0 == success, else error value.                    */
57926945a25Schristos /* Parameters:  uio(O) - pointer to information about where to store data   */
58026945a25Schristos /*                                                                          */
58126945a25Schristos /* This function is called when a user program wants to read some data      */
58226945a25Schristos /* for pending state/NAT updates.  If no data is available, the caller is   */
58326945a25Schristos /* put to sleep, pending a wakeup from the "lower half" of this code.       */
58426945a25Schristos /* ------------------------------------------------------------------------ */
58526945a25Schristos int
ipf_sync_read(softc,uio)58626945a25Schristos ipf_sync_read(softc, uio)
58726945a25Schristos 	ipf_main_softc_t *softc;
58826945a25Schristos 	struct uio *uio;
58926945a25Schristos {
59026945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
59126945a25Schristos 	syncupdent_t *su;
59226945a25Schristos 	synclogent_t *sl;
59326945a25Schristos 	int err = 0;
59426945a25Schristos 
59526945a25Schristos 	if ((uio->uio_resid & 3) || (uio->uio_resid < 8)) {
59626945a25Schristos 		IPFERROR(110008);
59726945a25Schristos 		return EINVAL;
59826945a25Schristos 	}
59926945a25Schristos 
60026945a25Schristos #  if BSD_GE_YEAR(199306) || defined(__FreeBSD__) || defined(__osf__)
60126945a25Schristos 	uio->uio_rw = UIO_READ;
60226945a25Schristos #  endif
60326945a25Schristos 
60426945a25Schristos 	MUTEX_ENTER(&softs->ipsl_mutex);
60526945a25Schristos 	while ((softs->sl_tail == softs->sl_idx) &&
60626945a25Schristos 	       (softs->su_tail == softs->su_idx)) {
60726945a25Schristos #  if defined(_KERNEL)
60826945a25Schristos #   if SOLARIS
60926945a25Schristos 		if (!cv_wait_sig(&softs->ipslwait, &softs->ipsl_mutex.ipf_lk)) {
61026945a25Schristos 			MUTEX_EXIT(&softs->ipsl_mutex);
61126945a25Schristos 			IPFERROR(110009);
61226945a25Schristos 			return EINTR;
61326945a25Schristos 		}
61426945a25Schristos #   else
61526945a25Schristos #    ifdef __hpux
61626945a25Schristos 		{
61726945a25Schristos 		lock_t *l;
61826945a25Schristos 
61926945a25Schristos 		l = get_sleep_lock(&softs->sl_tail);
62026945a25Schristos 		err = sleep(&softs->sl_tail, PZERO+1);
62126945a25Schristos 		if (err) {
62226945a25Schristos 			MUTEX_EXIT(&softs->ipsl_mutex);
62326945a25Schristos 			IPFERROR(110010);
62426945a25Schristos 			return EINTR;
62526945a25Schristos 		}
62626945a25Schristos 		spinunlock(l);
62726945a25Schristos 		}
62826945a25Schristos #    else /* __hpux */
62926945a25Schristos #     ifdef __osf__
63026945a25Schristos 		err = mpsleep(&softs->sl_tail, PSUSP|PCATCH,  "ipl sleep", 0,
63126945a25Schristos 			      &softs->ipsl_mutex, MS_LOCK_SIMPLE);
63226945a25Schristos 		if (err) {
63326945a25Schristos 			IPFERROR(110011);
63426945a25Schristos 			return EINTR;
63526945a25Schristos 		}
63626945a25Schristos #     else
63726945a25Schristos 		MUTEX_EXIT(&softs->ipsl_mutex);
63826945a25Schristos 		err = SLEEP(&softs->sl_tail, "ipl sleep");
63926945a25Schristos 		if (err) {
64026945a25Schristos 			IPFERROR(110012);
64126945a25Schristos 			return EINTR;
64226945a25Schristos 		}
64326945a25Schristos 		MUTEX_ENTER(&softs->ipsl_mutex);
64426945a25Schristos #     endif /* __osf__ */
64526945a25Schristos #    endif /* __hpux */
64626945a25Schristos #   endif /* SOLARIS */
64726945a25Schristos #  endif /* _KERNEL */
64826945a25Schristos 	}
64926945a25Schristos 
65026945a25Schristos 	while ((softs->sl_tail < softs->sl_idx) &&
65126945a25Schristos 	       (uio->uio_resid > sizeof(*sl))) {
65226945a25Schristos 		sl = softs->synclog + softs->sl_tail++;
65326945a25Schristos 		MUTEX_EXIT(&softs->ipsl_mutex);
65426945a25Schristos 		err = UIOMOVE(sl, sizeof(*sl), UIO_READ, uio);
65526945a25Schristos 		if (err != 0)
65626945a25Schristos 			goto goterror;
65726945a25Schristos 		MUTEX_ENTER(&softs->ipsl_mutex);
65826945a25Schristos 	}
65926945a25Schristos 
66026945a25Schristos 	while ((softs->su_tail < softs->su_idx) &&
66126945a25Schristos 	       (uio->uio_resid > sizeof(*su))) {
66226945a25Schristos 		su = softs->syncupd + softs->su_tail;
66326945a25Schristos 		softs->su_tail++;
66426945a25Schristos 		MUTEX_EXIT(&softs->ipsl_mutex);
66526945a25Schristos 		err = UIOMOVE(su, sizeof(*su), UIO_READ, uio);
66626945a25Schristos 		if (err != 0)
66726945a25Schristos 			goto goterror;
66826945a25Schristos 		MUTEX_ENTER(&softs->ipsl_mutex);
66926945a25Schristos 		if (su->sup_hdr.sm_sl != NULL)
67026945a25Schristos 			su->sup_hdr.sm_sl->sl_idx = -1;
67126945a25Schristos 	}
67226945a25Schristos 	if (softs->sl_tail == softs->sl_idx)
67326945a25Schristos 		softs->sl_tail = softs->sl_idx = 0;
67426945a25Schristos 	if (softs->su_tail == softs->su_idx)
67526945a25Schristos 		softs->su_tail = softs->su_idx = 0;
67626945a25Schristos 	MUTEX_EXIT(&softs->ipsl_mutex);
67726945a25Schristos goterror:
67826945a25Schristos 	return err;
67926945a25Schristos }
68026945a25Schristos 
68126945a25Schristos 
68226945a25Schristos /* ------------------------------------------------------------------------ */
68326945a25Schristos /* Function:    ipf_sync_state                                              */
68426945a25Schristos /* Returns:     int    - 0 == success, else error value.                    */
68526945a25Schristos /* Parameters:  sp(I)  - pointer to sync packet data header                 */
68626945a25Schristos /*              uio(I) - pointer to user data for further information       */
68726945a25Schristos /*                                                                          */
68826945a25Schristos /* Updates the state table according to information passed in the sync      */
68926945a25Schristos /* header.  As required, more data is fetched from the uio structure but    */
69026945a25Schristos /* varies depending on the contents of the sync header.  This function can  */
69126945a25Schristos /* create a new state entry or update one.  Deletion is left to the state   */
69226945a25Schristos /* structures being timed out correctly.                                    */
69326945a25Schristos /* ------------------------------------------------------------------------ */
69426945a25Schristos static int
ipf_sync_state(softc,sp,data)69526945a25Schristos ipf_sync_state(softc, sp, data)
69626945a25Schristos 	ipf_main_softc_t *softc;
69726945a25Schristos 	synchdr_t *sp;
69826945a25Schristos 	void *data;
69926945a25Schristos {
70026945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
70126945a25Schristos 	synctcp_update_t su;
70226945a25Schristos 	ipstate_t *is, sn;
70326945a25Schristos 	synclist_t *sl;
70426945a25Schristos 	frentry_t *fr;
70526945a25Schristos 	u_int hv;
70626945a25Schristos 	int err = 0;
70726945a25Schristos 
70826945a25Schristos 	hv = sp->sm_num & (softs->ipf_sync_state_tab_sz - 1);
70926945a25Schristos 
71026945a25Schristos 	switch (sp->sm_cmd)
71126945a25Schristos 	{
71226945a25Schristos 	case SMC_CREATE :
71326945a25Schristos 
71426945a25Schristos 		bcopy(data, &sn, sizeof(sn));
71526945a25Schristos 		KMALLOC(is, ipstate_t *);
71626945a25Schristos 		if (is == NULL) {
71726945a25Schristos 			IPFERROR(110013);
71826945a25Schristos 			err = ENOMEM;
71926945a25Schristos 			break;
72026945a25Schristos 		}
72126945a25Schristos 
72226945a25Schristos 		KMALLOC(sl, synclist_t *);
72326945a25Schristos 		if (sl == NULL) {
72426945a25Schristos 			IPFERROR(110014);
72526945a25Schristos 			err = ENOMEM;
72626945a25Schristos 			KFREE(is);
72726945a25Schristos 			break;
72826945a25Schristos 		}
72926945a25Schristos 
73026945a25Schristos 		bzero((char *)is, offsetof(ipstate_t, is_die));
73126945a25Schristos 		bcopy((char *)&sn.is_die, (char *)&is->is_die,
73226945a25Schristos 		      sizeof(*is) - offsetof(ipstate_t, is_die));
73326945a25Schristos 		ipf_sync_storder(0, is);
73426945a25Schristos 
73526945a25Schristos 		/*
73626945a25Schristos 		 * We need to find the same rule on the slave as was used on
73726945a25Schristos 		 * the master to create this state entry.
73826945a25Schristos 		 */
73926945a25Schristos 		READ_ENTER(&softc->ipf_mutex);
74026945a25Schristos 		fr = ipf_getrulen(softc, IPL_LOGIPF, sn.is_group, sn.is_rulen);
74126945a25Schristos 		if (fr != NULL) {
74226945a25Schristos 			MUTEX_ENTER(&fr->fr_lock);
74326945a25Schristos 			fr->fr_ref++;
74426945a25Schristos 			fr->fr_statecnt++;
74526945a25Schristos 			MUTEX_EXIT(&fr->fr_lock);
74626945a25Schristos 		}
74726945a25Schristos 		RWLOCK_EXIT(&softc->ipf_mutex);
74826945a25Schristos 
74926945a25Schristos 		if (softs->ipf_sync_debug > 4)
75026945a25Schristos 			printf("[%d] Filter rules = %p\n", sp->sm_num, fr);
75126945a25Schristos 
75226945a25Schristos 		is->is_rule = fr;
75326945a25Schristos 		is->is_sync = sl;
75426945a25Schristos 
75526945a25Schristos 		sl->sl_idx = -1;
75626945a25Schristos 		sl->sl_ips = is;
75726945a25Schristos 		bcopy(sp, &sl->sl_hdr, sizeof(struct synchdr));
75826945a25Schristos 
75926945a25Schristos 		WRITE_ENTER(&softs->ipf_syncstate);
76026945a25Schristos 		WRITE_ENTER(&softc->ipf_state);
76126945a25Schristos 
76226945a25Schristos 		sl->sl_pnext = softs->syncstatetab + hv;
76326945a25Schristos 		sl->sl_next = softs->syncstatetab[hv];
76426945a25Schristos 		if (softs->syncstatetab[hv] != NULL)
76526945a25Schristos 			softs->syncstatetab[hv]->sl_pnext = &sl->sl_next;
76626945a25Schristos 		softs->syncstatetab[hv] = sl;
76726945a25Schristos 		MUTEX_DOWNGRADE(&softs->ipf_syncstate);
76826945a25Schristos 		ipf_state_insert(softc, is, sp->sm_rev);
76926945a25Schristos 		/*
77026945a25Schristos 		 * Do not initialise the interface pointers for the state
77126945a25Schristos 		 * entry as the full complement of interface names may not
77226945a25Schristos 		 * be present.
77326945a25Schristos 		 *
77426945a25Schristos 		 * Put this state entry on its timeout queue.
77526945a25Schristos 		 */
77626945a25Schristos 		/*fr_setstatequeue(is, sp->sm_rev);*/
77726945a25Schristos 		break;
77826945a25Schristos 
77926945a25Schristos 	case SMC_UPDATE :
78026945a25Schristos 		bcopy(data, &su, sizeof(su));
78126945a25Schristos 
78226945a25Schristos 		if (softs->ipf_sync_debug > 4)
78326945a25Schristos 			printf("[%d] Update age %lu state %d/%d \n",
78426945a25Schristos 				sp->sm_num, su.stu_age, su.stu_state[0],
78526945a25Schristos 				su.stu_state[1]);
78626945a25Schristos 
78726945a25Schristos 		READ_ENTER(&softs->ipf_syncstate);
78826945a25Schristos 		for (sl = softs->syncstatetab[hv]; (sl != NULL);
78926945a25Schristos 		     sl = sl->sl_next)
79026945a25Schristos 			if (sl->sl_hdr.sm_num == sp->sm_num)
79126945a25Schristos 				break;
79226945a25Schristos 		if (sl == NULL) {
79326945a25Schristos 			if (softs->ipf_sync_debug > 1)
79426945a25Schristos 				printf("[%d] State not found - can't update\n",
79526945a25Schristos 					sp->sm_num);
79626945a25Schristos 			RWLOCK_EXIT(&softs->ipf_syncstate);
79726945a25Schristos 			IPFERROR(110015);
79826945a25Schristos 			err = ENOENT;
79926945a25Schristos 			break;
80026945a25Schristos 		}
80126945a25Schristos 
80226945a25Schristos 		READ_ENTER(&softc->ipf_state);
80326945a25Schristos 
80426945a25Schristos 		if (softs->ipf_sync_debug > 6)
80526945a25Schristos 			printf("[%d] Data from state v:%d p:%d cmd:%d table:%d rev:%d\n",
80626945a25Schristos 				sp->sm_num, sl->sl_hdr.sm_v, sl->sl_hdr.sm_p,
80726945a25Schristos 				sl->sl_hdr.sm_cmd, sl->sl_hdr.sm_table,
80826945a25Schristos 				sl->sl_hdr.sm_rev);
80926945a25Schristos 
81026945a25Schristos 		is = sl->sl_ips;
81126945a25Schristos 
81226945a25Schristos 		MUTEX_ENTER(&is->is_lock);
81326945a25Schristos 		switch (sp->sm_p)
81426945a25Schristos 		{
81526945a25Schristos 		case IPPROTO_TCP :
81626945a25Schristos 			/* XXX FV --- shouldn't we do ntohl/htonl???? XXX */
81726945a25Schristos 			is->is_send = su.stu_data[0].td_end;
81826945a25Schristos 			is->is_maxsend = su.stu_data[0].td_maxend;
81926945a25Schristos 			is->is_maxswin = su.stu_data[0].td_maxwin;
82026945a25Schristos 			is->is_state[0] = su.stu_state[0];
82126945a25Schristos 			is->is_dend = su.stu_data[1].td_end;
82226945a25Schristos 			is->is_maxdend = su.stu_data[1].td_maxend;
82326945a25Schristos 			is->is_maxdwin = su.stu_data[1].td_maxwin;
82426945a25Schristos 			is->is_state[1] = su.stu_state[1];
82526945a25Schristos 			break;
82626945a25Schristos 		default :
82726945a25Schristos 			break;
82826945a25Schristos 		}
82926945a25Schristos 
83026945a25Schristos 		if (softs->ipf_sync_debug > 6)
83126945a25Schristos 			printf("[%d] Setting timers for state\n", sp->sm_num);
83226945a25Schristos 
83326945a25Schristos 		ipf_state_setqueue(softc, is, sp->sm_rev);
83426945a25Schristos 
83526945a25Schristos 		MUTEX_EXIT(&is->is_lock);
83626945a25Schristos 		break;
83726945a25Schristos 
83826945a25Schristos 	default :
83926945a25Schristos 		IPFERROR(110016);
84026945a25Schristos 		err = EINVAL;
84126945a25Schristos 		break;
84226945a25Schristos 	}
84326945a25Schristos 
84426945a25Schristos 	if (err == 0) {
84526945a25Schristos 		RWLOCK_EXIT(&softc->ipf_state);
84626945a25Schristos 		RWLOCK_EXIT(&softs->ipf_syncstate);
84726945a25Schristos 	}
84826945a25Schristos 
84926945a25Schristos 	if (softs->ipf_sync_debug > 6)
85026945a25Schristos 		printf("[%d] Update completed with error %d\n",
85126945a25Schristos 			sp->sm_num, err);
85226945a25Schristos 
85326945a25Schristos 	return err;
85426945a25Schristos }
85526945a25Schristos 
85626945a25Schristos 
85726945a25Schristos /* ------------------------------------------------------------------------ */
85826945a25Schristos /* Function:    ipf_sync_del                                                */
85926945a25Schristos /* Returns:     Nil                                                         */
86026945a25Schristos /* Parameters:  sl(I) - pointer to synclist object to delete                */
86126945a25Schristos /*                                                                          */
86226945a25Schristos /* Deletes an object from the synclist.                                     */
86326945a25Schristos /* ------------------------------------------------------------------------ */
86426945a25Schristos static void
ipf_sync_del(softs,sl)86526945a25Schristos ipf_sync_del(softs, sl)
86626945a25Schristos 	ipf_sync_softc_t *softs;
86726945a25Schristos 	synclist_t *sl;
86826945a25Schristos {
86926945a25Schristos 	*sl->sl_pnext = sl->sl_next;
87026945a25Schristos 	if (sl->sl_next != NULL)
87126945a25Schristos 		sl->sl_next->sl_pnext = sl->sl_pnext;
87226945a25Schristos 	if (sl->sl_idx != -1)
87326945a25Schristos 		softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
87426945a25Schristos }
87526945a25Schristos 
87626945a25Schristos 
87726945a25Schristos /* ------------------------------------------------------------------------ */
87826945a25Schristos /* Function:    ipf_sync_del_state                                          */
87926945a25Schristos /* Returns:     Nil                                                         */
88026945a25Schristos /* Parameters:  sl(I) - pointer to synclist object to delete                */
88126945a25Schristos /*                                                                          */
88226945a25Schristos /* Deletes an object from the synclist state table and free's its memory.   */
88326945a25Schristos /* ------------------------------------------------------------------------ */
88426945a25Schristos void
ipf_sync_del_state(arg,sl)88526945a25Schristos ipf_sync_del_state(arg, sl)
88626945a25Schristos 	void *arg;
88726945a25Schristos 	synclist_t *sl;
88826945a25Schristos {
88926945a25Schristos 	ipf_sync_softc_t *softs = arg;
89026945a25Schristos 
89126945a25Schristos 	WRITE_ENTER(&softs->ipf_syncstate);
89226945a25Schristos 	ipf_sync_del(softs, sl);
89326945a25Schristos 	RWLOCK_EXIT(&softs->ipf_syncstate);
89426945a25Schristos 	KFREE(sl);
89526945a25Schristos }
89626945a25Schristos 
89726945a25Schristos 
89826945a25Schristos /* ------------------------------------------------------------------------ */
89926945a25Schristos /* Function:    ipf_sync_del_nat                                            */
90026945a25Schristos /* Returns:     Nil                                                         */
90126945a25Schristos /* Parameters:  sl(I) - pointer to synclist object to delete                */
90226945a25Schristos /*                                                                          */
90326945a25Schristos /* Deletes an object from the synclist nat table and free's its memory.     */
90426945a25Schristos /* ------------------------------------------------------------------------ */
90526945a25Schristos void
ipf_sync_del_nat(arg,sl)90626945a25Schristos ipf_sync_del_nat(arg, sl)
90726945a25Schristos 	void *arg;
90826945a25Schristos 	synclist_t *sl;
90926945a25Schristos {
91026945a25Schristos 	ipf_sync_softc_t *softs = arg;
91126945a25Schristos 
91226945a25Schristos 	WRITE_ENTER(&softs->ipf_syncnat);
91326945a25Schristos 	ipf_sync_del(softs, sl);
91426945a25Schristos 	RWLOCK_EXIT(&softs->ipf_syncnat);
91526945a25Schristos 	KFREE(sl);
91626945a25Schristos }
91726945a25Schristos 
91826945a25Schristos 
91926945a25Schristos /* ------------------------------------------------------------------------ */
92026945a25Schristos /* Function:    ipf_sync_nat                                                */
92126945a25Schristos /* Returns:     int    - 0 == success, else error value.                    */
92226945a25Schristos /* Parameters:  sp(I)  - pointer to sync packet data header                 */
92326945a25Schristos /*              uio(I) - pointer to user data for further information       */
92426945a25Schristos /*                                                                          */
92526945a25Schristos /* Updates the NAT  table according to information passed in the sync       */
92626945a25Schristos /* header.  As required, more data is fetched from the uio structure but    */
92726945a25Schristos /* varies depending on the contents of the sync header.  This function can  */
92826945a25Schristos /* create a new NAT entry or update one.  Deletion is left to the NAT       */
92926945a25Schristos /* structures being timed out correctly.                                    */
93026945a25Schristos /* ------------------------------------------------------------------------ */
93126945a25Schristos static int
ipf_sync_nat(softc,sp,data)93226945a25Schristos ipf_sync_nat(softc, sp, data)
93326945a25Schristos 	ipf_main_softc_t *softc;
93426945a25Schristos 	synchdr_t *sp;
93526945a25Schristos 	void *data;
93626945a25Schristos {
93726945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
93826945a25Schristos 	syncupdent_t su;
93926945a25Schristos 	nat_t *n, *nat;
94026945a25Schristos 	synclist_t *sl;
94126945a25Schristos 	u_int hv = 0;
94226945a25Schristos 	int err;
94326945a25Schristos 
94426945a25Schristos 	READ_ENTER(&softs->ipf_syncnat);
94526945a25Schristos 
94626945a25Schristos 	switch (sp->sm_cmd)
94726945a25Schristos 	{
94826945a25Schristos 	case SMC_CREATE :
94926945a25Schristos 		KMALLOC(n, nat_t *);
95026945a25Schristos 		if (n == NULL) {
95126945a25Schristos 			IPFERROR(110017);
95226945a25Schristos 			err = ENOMEM;
95326945a25Schristos 			break;
95426945a25Schristos 		}
95526945a25Schristos 
95626945a25Schristos 		KMALLOC(sl, synclist_t *);
95726945a25Schristos 		if (sl == NULL) {
95826945a25Schristos 			IPFERROR(110018);
95926945a25Schristos 			err = ENOMEM;
96026945a25Schristos 			KFREE(n);
96126945a25Schristos 			break;
96226945a25Schristos 		}
96326945a25Schristos 
96426945a25Schristos 		nat = (nat_t *)data;
96526945a25Schristos 		bzero((char *)n, offsetof(nat_t, nat_age));
96626945a25Schristos 		bcopy((char *)&nat->nat_age, (char *)&n->nat_age,
96726945a25Schristos 		      sizeof(*n) - offsetof(nat_t, nat_age));
96826945a25Schristos 		ipf_sync_natorder(0, n);
96926945a25Schristos 		n->nat_sync = sl;
97026945a25Schristos 		n->nat_rev = sl->sl_rev;
97126945a25Schristos 
97226945a25Schristos 		sl->sl_idx = -1;
97326945a25Schristos 		sl->sl_ipn = n;
97426945a25Schristos 		sl->sl_num = ntohl(sp->sm_num);
97526945a25Schristos 
97626945a25Schristos 		WRITE_ENTER(&softc->ipf_nat);
97726945a25Schristos 		sl->sl_pnext = softs->syncnattab + hv;
97826945a25Schristos 		sl->sl_next = softs->syncnattab[hv];
97926945a25Schristos 		if (softs->syncnattab[hv] != NULL)
98026945a25Schristos 			softs->syncnattab[hv]->sl_pnext = &sl->sl_next;
98126945a25Schristos 		softs->syncnattab[hv] = sl;
98226945a25Schristos 		(void) ipf_nat_insert(softc, softc->ipf_nat_soft, n);
98326945a25Schristos 		RWLOCK_EXIT(&softc->ipf_nat);
98426945a25Schristos 		break;
98526945a25Schristos 
98626945a25Schristos 	case SMC_UPDATE :
98726945a25Schristos 		bcopy(data, &su, sizeof(su));
98826945a25Schristos 
98926945a25Schristos 		for (sl = softs->syncnattab[hv]; (sl != NULL);
99026945a25Schristos 		     sl = sl->sl_next)
99126945a25Schristos 			if (sl->sl_hdr.sm_num == sp->sm_num)
99226945a25Schristos 				break;
99326945a25Schristos 		if (sl == NULL) {
99426945a25Schristos 			IPFERROR(110019);
99526945a25Schristos 			err = ENOENT;
99626945a25Schristos 			break;
99726945a25Schristos 		}
99826945a25Schristos 
99926945a25Schristos 		READ_ENTER(&softc->ipf_nat);
100026945a25Schristos 
100126945a25Schristos 		nat = sl->sl_ipn;
100226945a25Schristos 		nat->nat_rev = sl->sl_rev;
100326945a25Schristos 
100426945a25Schristos 		MUTEX_ENTER(&nat->nat_lock);
100526945a25Schristos 		ipf_nat_setqueue(softc, softc->ipf_nat_soft, nat);
100626945a25Schristos 		MUTEX_EXIT(&nat->nat_lock);
100726945a25Schristos 
100826945a25Schristos 		RWLOCK_EXIT(&softc->ipf_nat);
100926945a25Schristos 
101026945a25Schristos 		break;
101126945a25Schristos 
101226945a25Schristos 	default :
101326945a25Schristos 		IPFERROR(110020);
101426945a25Schristos 		err = EINVAL;
101526945a25Schristos 		break;
101626945a25Schristos 	}
101726945a25Schristos 
101826945a25Schristos 	RWLOCK_EXIT(&softs->ipf_syncnat);
101926945a25Schristos 	return 0;
102026945a25Schristos }
102126945a25Schristos 
102226945a25Schristos 
102326945a25Schristos /* ------------------------------------------------------------------------ */
102426945a25Schristos /* Function:    ipf_sync_new                                                */
102526945a25Schristos /* Returns:     synclist_t* - NULL == failure, else pointer to new synclist */
102626945a25Schristos /*                            data structure.                               */
102726945a25Schristos /* Parameters:  tab(I) - type of synclist_t to create                       */
102826945a25Schristos /*              fin(I) - pointer to packet information                      */
102926945a25Schristos /*              ptr(I) - pointer to owning object                           */
103026945a25Schristos /*                                                                          */
103126945a25Schristos /* Creates a new sync table entry and notifies any sleepers that it's there */
103226945a25Schristos /* waiting to be processed.                                                 */
103326945a25Schristos /* ------------------------------------------------------------------------ */
103426945a25Schristos synclist_t *
ipf_sync_new(softc,tab,fin,ptr)103526945a25Schristos ipf_sync_new(softc, tab, fin, ptr)
103626945a25Schristos 	ipf_main_softc_t *softc;
103726945a25Schristos 	int tab;
103826945a25Schristos 	fr_info_t *fin;
103926945a25Schristos 	void *ptr;
104026945a25Schristos {
104126945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
104226945a25Schristos 	synclist_t *sl, *ss;
104326945a25Schristos 	synclogent_t *sle;
104426945a25Schristos 	u_int hv, sz;
104526945a25Schristos 
104626945a25Schristos 	if (softs->sl_idx == softs->ipf_sync_log_sz)
104726945a25Schristos 		return NULL;
104826945a25Schristos 	KMALLOC(sl, synclist_t *);
104926945a25Schristos 	if (sl == NULL)
105026945a25Schristos 		return NULL;
105126945a25Schristos 
105226945a25Schristos 	MUTEX_ENTER(&softs->ipf_syncadd);
105326945a25Schristos 	/*
105426945a25Schristos 	 * Get a unique number for this synclist_t.  The number is only meant
105526945a25Schristos 	 * to be unique for the lifetime of the structure and may be reused
105626945a25Schristos 	 * later.
105726945a25Schristos 	 */
105826945a25Schristos 	softs->ipf_sync_num++;
105926945a25Schristos 	if (softs->ipf_sync_num == 0) {
106026945a25Schristos 		softs->ipf_sync_num = 1;
106126945a25Schristos 		softs->ipf_sync_wrap++;
106226945a25Schristos 	}
106326945a25Schristos 
106426945a25Schristos 	/*
106526945a25Schristos 	 * Use the synch number of the object as the hash key.  Should end up
106626945a25Schristos 	 * with relatively even distribution over time.
106726945a25Schristos 	 * XXX - an attacker could lunch an DoS attack, of sorts, if they are
106826945a25Schristos 	 * the only one causing new table entries by only keeping open every
106926945a25Schristos 	 * nth connection they make, where n is a value in the interval
107026945a25Schristos 	 * [0, SYNC_STATETABSZ-1].
107126945a25Schristos 	 */
107226945a25Schristos 	switch (tab)
107326945a25Schristos 	{
107426945a25Schristos 	case SMC_STATE :
107526945a25Schristos 		hv = softs->ipf_sync_num & (softs->ipf_sync_state_tab_sz - 1);
107626945a25Schristos 		while (softs->ipf_sync_wrap != 0) {
107726945a25Schristos 			for (ss = softs->syncstatetab[hv]; ss; ss = ss->sl_next)
107826945a25Schristos 				if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
107926945a25Schristos 					break;
108026945a25Schristos 			if (ss == NULL)
108126945a25Schristos 				break;
108226945a25Schristos 			softs->ipf_sync_num++;
108326945a25Schristos 			hv = softs->ipf_sync_num &
108426945a25Schristos 			     (softs->ipf_sync_state_tab_sz - 1);
108526945a25Schristos 		}
108626945a25Schristos 		sl->sl_pnext = softs->syncstatetab + hv;
108726945a25Schristos 		sl->sl_next = softs->syncstatetab[hv];
108826945a25Schristos 		softs->syncstatetab[hv] = sl;
108926945a25Schristos 		break;
109026945a25Schristos 
109126945a25Schristos 	case SMC_NAT :
109226945a25Schristos 		hv = softs->ipf_sync_num & (softs->ipf_sync_nat_tab_sz - 1);
109326945a25Schristos 		while (softs->ipf_sync_wrap != 0) {
109426945a25Schristos 			for (ss = softs->syncnattab[hv]; ss; ss = ss->sl_next)
109526945a25Schristos 				if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
109626945a25Schristos 					break;
109726945a25Schristos 			if (ss == NULL)
109826945a25Schristos 				break;
109926945a25Schristos 			softs->ipf_sync_num++;
110026945a25Schristos 			hv = softs->ipf_sync_num &
110126945a25Schristos 			     (softs->ipf_sync_nat_tab_sz - 1);
110226945a25Schristos 		}
110326945a25Schristos 		sl->sl_pnext = softs->syncnattab + hv;
110426945a25Schristos 		sl->sl_next = softs->syncnattab[hv];
110526945a25Schristos 		softs->syncnattab[hv] = sl;
110626945a25Schristos 		break;
110726945a25Schristos 
110826945a25Schristos 	default :
110926945a25Schristos 		break;
111026945a25Schristos 	}
111126945a25Schristos 
111226945a25Schristos 	sl->sl_num = softs->ipf_sync_num;
111326945a25Schristos 	MUTEX_EXIT(&softs->ipf_syncadd);
111426945a25Schristos 
111526945a25Schristos 	sl->sl_magic = htonl(SYNHDRMAGIC);
111626945a25Schristos 	sl->sl_v = fin->fin_v;
111726945a25Schristos 	sl->sl_p = fin->fin_p;
111826945a25Schristos 	sl->sl_cmd = SMC_CREATE;
111926945a25Schristos 	sl->sl_idx = -1;
112026945a25Schristos 	sl->sl_table = tab;
112126945a25Schristos 	sl->sl_rev = fin->fin_rev;
112226945a25Schristos 	if (tab == SMC_STATE) {
112326945a25Schristos 		sl->sl_ips = ptr;
112426945a25Schristos 		sz = sizeof(*sl->sl_ips);
112526945a25Schristos 	} else if (tab == SMC_NAT) {
112626945a25Schristos 		sl->sl_ipn = ptr;
112726945a25Schristos 		sz = sizeof(*sl->sl_ipn);
112826945a25Schristos 	} else {
112926945a25Schristos 		ptr = NULL;
113026945a25Schristos 		sz = 0;
113126945a25Schristos 	}
113226945a25Schristos 	sl->sl_len = sz;
113326945a25Schristos 
113426945a25Schristos 	/*
113526945a25Schristos 	 * Create the log entry to be read by a user daemon.  When it has been
113626945a25Schristos 	 * finished and put on the queue, send a signal to wakeup any waiters.
113726945a25Schristos 	 */
113826945a25Schristos 	MUTEX_ENTER(&softs->ipf_syncadd);
113926945a25Schristos 	sle = softs->synclog + softs->sl_idx++;
114026945a25Schristos 	bcopy((char *)&sl->sl_hdr, (char *)&sle->sle_hdr,
114126945a25Schristos 	      sizeof(sle->sle_hdr));
114226945a25Schristos 	sle->sle_hdr.sm_num = htonl(sle->sle_hdr.sm_num);
114326945a25Schristos 	sle->sle_hdr.sm_len = htonl(sle->sle_hdr.sm_len);
114426945a25Schristos 	if (ptr != NULL) {
114526945a25Schristos 		bcopy((char *)ptr, (char *)&sle->sle_un, sz);
114626945a25Schristos 		if (tab == SMC_STATE) {
114726945a25Schristos 			ipf_sync_storder(1, &sle->sle_un.sleu_ips);
114826945a25Schristos 		} else if (tab == SMC_NAT) {
114926945a25Schristos 			ipf_sync_natorder(1, &sle->sle_un.sleu_ipn);
115026945a25Schristos 		}
115126945a25Schristos 	}
115226945a25Schristos 	MUTEX_EXIT(&softs->ipf_syncadd);
115326945a25Schristos 
115426945a25Schristos 	ipf_sync_wakeup(softc);
115526945a25Schristos 	return sl;
115626945a25Schristos }
115726945a25Schristos 
115826945a25Schristos 
115926945a25Schristos /* ------------------------------------------------------------------------ */
116026945a25Schristos /* Function:    ipf_sync_update                                             */
116126945a25Schristos /* Returns:     Nil                                                         */
116226945a25Schristos /* Parameters:  tab(I) - type of synclist_t to create                       */
116326945a25Schristos /*              fin(I) - pointer to packet information                      */
116426945a25Schristos /*              sl(I)  - pointer to synchronisation object                  */
116526945a25Schristos /*                                                                          */
116626945a25Schristos /* For outbound packets, only, create an sync update record for the user    */
116726945a25Schristos /* process to read.                                                         */
116826945a25Schristos /* ------------------------------------------------------------------------ */
116926945a25Schristos void
ipf_sync_update(softc,tab,fin,sl)117026945a25Schristos ipf_sync_update(softc, tab, fin, sl)
117126945a25Schristos 	ipf_main_softc_t *softc;
117226945a25Schristos 	int tab;
117326945a25Schristos 	fr_info_t *fin;
117426945a25Schristos 	synclist_t *sl;
117526945a25Schristos {
117626945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
117726945a25Schristos 	synctcp_update_t *st;
117826945a25Schristos 	syncupdent_t *slu;
117926945a25Schristos 	ipstate_t *ips;
118026945a25Schristos 	nat_t *nat;
118126945a25Schristos 	ipfrwlock_t *lock;
118226945a25Schristos 
118326945a25Schristos 	if (fin->fin_out == 0 || sl == NULL)
118426945a25Schristos 		return;
118526945a25Schristos 
118626945a25Schristos 	if (tab == SMC_STATE) {
118726945a25Schristos 		lock = &softs->ipf_syncstate;
118826945a25Schristos 	} else {
118926945a25Schristos 		lock = &softs->ipf_syncnat;
119026945a25Schristos 	}
119126945a25Schristos 
119226945a25Schristos 	READ_ENTER(lock);
119326945a25Schristos 	if (sl->sl_idx == -1) {
119426945a25Schristos 		MUTEX_ENTER(&softs->ipf_syncadd);
119526945a25Schristos 		slu = softs->syncupd + softs->su_idx;
119626945a25Schristos 		sl->sl_idx = softs->su_idx++;
119726945a25Schristos 		MUTEX_EXIT(&softs->ipf_syncadd);
119826945a25Schristos 
119926945a25Schristos 		bcopy((char *)&sl->sl_hdr, (char *)&slu->sup_hdr,
120026945a25Schristos 		      sizeof(slu->sup_hdr));
120126945a25Schristos 		slu->sup_hdr.sm_magic = htonl(SYNHDRMAGIC);
120226945a25Schristos 		slu->sup_hdr.sm_sl = sl;
120326945a25Schristos 		slu->sup_hdr.sm_cmd = SMC_UPDATE;
120426945a25Schristos 		slu->sup_hdr.sm_table = tab;
120526945a25Schristos 		slu->sup_hdr.sm_num = htonl(sl->sl_num);
120626945a25Schristos 		slu->sup_hdr.sm_len = htonl(sizeof(struct synctcp_update));
120726945a25Schristos 		slu->sup_hdr.sm_rev = fin->fin_rev;
120826945a25Schristos # if 0
120926945a25Schristos 		if (fin->fin_p == IPPROTO_TCP) {
121026945a25Schristos 			st->stu_len[0] = 0;
121126945a25Schristos 			st->stu_len[1] = 0;
121226945a25Schristos 		}
121326945a25Schristos # endif
121426945a25Schristos 	} else
121526945a25Schristos 		slu = softs->syncupd + sl->sl_idx;
121626945a25Schristos 
121726945a25Schristos 	/*
121826945a25Schristos 	 * Only TCP has complex timeouts, others just use default timeouts.
121926945a25Schristos 	 * For TCP, we only need to track the connection state and window.
122026945a25Schristos 	 */
122126945a25Schristos 	if (fin->fin_p == IPPROTO_TCP) {
122226945a25Schristos 		st = &slu->sup_tcp;
122326945a25Schristos 		if (tab == SMC_STATE) {
122426945a25Schristos 			ips = sl->sl_ips;
122526945a25Schristos 			st->stu_age = htonl(ips->is_die);
122626945a25Schristos 			st->stu_data[0].td_end = ips->is_send;
122726945a25Schristos 			st->stu_data[0].td_maxend = ips->is_maxsend;
122826945a25Schristos 			st->stu_data[0].td_maxwin = ips->is_maxswin;
122926945a25Schristos 			st->stu_state[0] = ips->is_state[0];
123026945a25Schristos 			st->stu_data[1].td_end = ips->is_dend;
123126945a25Schristos 			st->stu_data[1].td_maxend = ips->is_maxdend;
123226945a25Schristos 			st->stu_data[1].td_maxwin = ips->is_maxdwin;
123326945a25Schristos 			st->stu_state[1] = ips->is_state[1];
123426945a25Schristos 		} else if (tab == SMC_NAT) {
123526945a25Schristos 			nat = sl->sl_ipn;
123626945a25Schristos 			st->stu_age = htonl(nat->nat_age);
123726945a25Schristos 		}
123826945a25Schristos 	}
123926945a25Schristos 	RWLOCK_EXIT(lock);
124026945a25Schristos 
124126945a25Schristos 	ipf_sync_wakeup(softc);
124226945a25Schristos }
124326945a25Schristos 
124426945a25Schristos 
124526945a25Schristos /* ------------------------------------------------------------------------ */
124626945a25Schristos /* Function:    ipf_sync_flush_table                                        */
124726945a25Schristos /* Returns:     int - number of entries freed by flushing table             */
124826945a25Schristos /* Parameters:  tabsize(I) - size of the array pointed to by table          */
124926945a25Schristos /*              table(I)   - pointer to sync table to empty                 */
125026945a25Schristos /*                                                                          */
125126945a25Schristos /* Walk through a table of sync entries and free each one.  It is assumed   */
125226945a25Schristos /* that some lock is held so that nobody else tries to access the table     */
125326945a25Schristos /* during this cleanup.                                                     */
125426945a25Schristos /* ------------------------------------------------------------------------ */
125526945a25Schristos static int
ipf_sync_flush_table(softs,tabsize,table)125626945a25Schristos ipf_sync_flush_table(softs, tabsize, table)
125726945a25Schristos 	ipf_sync_softc_t *softs;
125826945a25Schristos 	int tabsize;
125926945a25Schristos 	synclist_t **table;
126026945a25Schristos {
126126945a25Schristos 	synclist_t *sl;
126226945a25Schristos 	int i, items;
126326945a25Schristos 
126426945a25Schristos 	items = 0;
126526945a25Schristos 
126626945a25Schristos 	for (i = 0; i < tabsize; i++) {
126726945a25Schristos 		while ((sl = table[i]) != NULL) {
126826945a25Schristos 			switch (sl->sl_table) {
126926945a25Schristos 			case SMC_STATE :
127026945a25Schristos 				if (sl->sl_ips != NULL)
127126945a25Schristos 					sl->sl_ips->is_sync = NULL;
127226945a25Schristos 				break;
127326945a25Schristos 			case SMC_NAT :
127426945a25Schristos 				if (sl->sl_ipn != NULL)
127526945a25Schristos 					sl->sl_ipn->nat_sync = NULL;
127626945a25Schristos 				break;
127726945a25Schristos 			}
127826945a25Schristos 			if (sl->sl_next != NULL)
127926945a25Schristos 				sl->sl_next->sl_pnext = sl->sl_pnext;
128026945a25Schristos 			table[i] = sl->sl_next;
128126945a25Schristos 			if (sl->sl_idx != -1)
128226945a25Schristos 				softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
128326945a25Schristos 			KFREE(sl);
128426945a25Schristos 			items++;
128526945a25Schristos 		}
128626945a25Schristos 	}
128726945a25Schristos 
128826945a25Schristos 	return items;
128926945a25Schristos }
129026945a25Schristos 
129126945a25Schristos 
129226945a25Schristos /* ------------------------------------------------------------------------ */
129326945a25Schristos /* Function:    ipf_sync_ioctl                                              */
129426945a25Schristos /* Returns:     int - 0 == success, != 0 == failure                         */
129526945a25Schristos /* Parameters:  data(I) - pointer to ioctl data                             */
129626945a25Schristos /*              cmd(I)  - ioctl command integer                             */
129726945a25Schristos /*              mode(I) - file mode bits used with open                     */
129826945a25Schristos /*                                                                          */
129926945a25Schristos /* This function currently does not handle any ioctls and so just returns   */
130026945a25Schristos /* EINVAL on all occasions.                                                 */
130126945a25Schristos /* ------------------------------------------------------------------------ */
130226945a25Schristos int
ipf_sync_ioctl(softc,data,cmd,mode,uid,ctx)130326945a25Schristos ipf_sync_ioctl(softc, data, cmd, mode, uid, ctx)
130426945a25Schristos 	ipf_main_softc_t *softc;
130526945a25Schristos 	caddr_t data;
130626945a25Schristos 	ioctlcmd_t cmd;
130726945a25Schristos 	int mode, uid;
130826945a25Schristos 	void *ctx;
130926945a25Schristos {
131026945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
131126945a25Schristos 	int error, i;
131226945a25Schristos 	SPL_INT(s);
131326945a25Schristos 
131426945a25Schristos 	switch (cmd)
131526945a25Schristos 	{
131626945a25Schristos         case SIOCIPFFL:
131726945a25Schristos 		error = BCOPYIN(data, &i, sizeof(i));
131826945a25Schristos 		if (error != 0) {
131926945a25Schristos 			IPFERROR(110023);
132026945a25Schristos 			error = EFAULT;
132126945a25Schristos 			break;
132226945a25Schristos 		}
132326945a25Schristos 
132426945a25Schristos 		switch (i)
132526945a25Schristos 		{
132626945a25Schristos 		case SMC_RLOG :
132726945a25Schristos 			SPL_NET(s);
132826945a25Schristos 			MUTEX_ENTER(&softs->ipsl_mutex);
132926945a25Schristos 			i = (softs->sl_tail - softs->sl_idx) +
133026945a25Schristos 			    (softs->su_tail - softs->su_idx);
133126945a25Schristos 			softs->sl_idx = 0;
133226945a25Schristos 			softs->su_idx = 0;
133326945a25Schristos 			softs->sl_tail = 0;
133426945a25Schristos 			softs->su_tail = 0;
133526945a25Schristos 			MUTEX_EXIT(&softs->ipsl_mutex);
133626945a25Schristos 			SPL_X(s);
133726945a25Schristos 			break;
133826945a25Schristos 
133926945a25Schristos 		case SMC_NAT :
134026945a25Schristos 			SPL_NET(s);
134126945a25Schristos 			WRITE_ENTER(&softs->ipf_syncnat);
134226945a25Schristos 			i = ipf_sync_flush_table(softs, SYNC_NATTABSZ,
134326945a25Schristos 						 softs->syncnattab);
134426945a25Schristos 			RWLOCK_EXIT(&softs->ipf_syncnat);
134526945a25Schristos 			SPL_X(s);
134626945a25Schristos 			break;
134726945a25Schristos 
134826945a25Schristos 		case SMC_STATE :
134926945a25Schristos 			SPL_NET(s);
135026945a25Schristos 			WRITE_ENTER(&softs->ipf_syncstate);
135126945a25Schristos 			i = ipf_sync_flush_table(softs, SYNC_STATETABSZ,
135226945a25Schristos 						 softs->syncstatetab);
135326945a25Schristos 			RWLOCK_EXIT(&softs->ipf_syncstate);
135426945a25Schristos 			SPL_X(s);
135526945a25Schristos 			break;
135626945a25Schristos 		}
135726945a25Schristos 
135826945a25Schristos 		error = BCOPYOUT(&i, data, sizeof(i));
135926945a25Schristos 		if (error != 0) {
136026945a25Schristos 			IPFERROR(110022);
136126945a25Schristos 			error = EFAULT;
136226945a25Schristos 		}
136326945a25Schristos 		break;
136426945a25Schristos 
136526945a25Schristos 	default :
136626945a25Schristos 		IPFERROR(110021);
136726945a25Schristos 		error = EINVAL;
136826945a25Schristos 		break;
136926945a25Schristos 	}
137026945a25Schristos 
137126945a25Schristos 	return error;
137226945a25Schristos }
137326945a25Schristos 
137426945a25Schristos 
137526945a25Schristos /* ------------------------------------------------------------------------ */
137626945a25Schristos /* Function:    ipf_sync_canread                                            */
137726945a25Schristos /* Returns:     int - 0 == success, != 0 == failure                         */
137826945a25Schristos /* Parameters:  Nil                                                         */
137926945a25Schristos /*                                                                          */
138026945a25Schristos /* This function provides input to the poll handler about whether or not    */
138126945a25Schristos /* there is data waiting to be read from the /dev/ipsync device.            */
138226945a25Schristos /* ------------------------------------------------------------------------ */
138326945a25Schristos int
ipf_sync_canread(arg)138426945a25Schristos ipf_sync_canread(arg)
138526945a25Schristos 	void *arg;
138626945a25Schristos {
138726945a25Schristos 	ipf_sync_softc_t *softs = arg;
138826945a25Schristos 	return !((softs->sl_tail == softs->sl_idx) &&
138926945a25Schristos 		 (softs->su_tail == softs->su_idx));
139026945a25Schristos }
139126945a25Schristos 
139226945a25Schristos 
139326945a25Schristos /* ------------------------------------------------------------------------ */
139426945a25Schristos /* Function:    ipf_sync_canwrite                                           */
139526945a25Schristos /* Returns:     int - 1 == can always write                                 */
139626945a25Schristos /* Parameters:  Nil                                                         */
139726945a25Schristos /*                                                                          */
139826945a25Schristos /* This function lets the poll handler know that it is always ready willing */
139926945a25Schristos /* to accept write events.                                                  */
140026945a25Schristos /* XXX Maybe this should return false if the sync table is full?            */
140126945a25Schristos /* ------------------------------------------------------------------------ */
140226945a25Schristos int
ipf_sync_canwrite(arg)140326945a25Schristos ipf_sync_canwrite(arg)
140426945a25Schristos 	void *arg;
140526945a25Schristos {
140626945a25Schristos 	return 1;
140726945a25Schristos }
140826945a25Schristos 
140926945a25Schristos 
141026945a25Schristos /* ------------------------------------------------------------------------ */
141126945a25Schristos /* Function:    ipf_sync_wakeup                                             */
141226945a25Schristos /* Parameters:  Nil                                                         */
141326945a25Schristos /* Returns:     Nil                                                         */
141426945a25Schristos /*                                                                          */
141526945a25Schristos /* This function implements the heuristics that decide how often to         */
141626945a25Schristos /* generate a poll wakeup for programs that are waiting for information     */
141726945a25Schristos /* about when they can do a read on /dev/ipsync.                            */
141826945a25Schristos /*                                                                          */
141926945a25Schristos /* There are three different considerations here:                           */
142026945a25Schristos /* - do not keep a program waiting too long: ipf_sync_wake_interval is the  */
142126945a25Schristos /*   maximum number of ipf ticks to let pass by;                            */
142226945a25Schristos /* - do not let the queue of ouststanding things to generate notifies for   */
142326945a25Schristos /*   get too full (ipf_sync_queue_high_wm is the high water mark);          */
142426945a25Schristos /* - do not let too many events get collapsed in before deciding that the   */
142526945a25Schristos /*   other host(s) need an update (ipf_sync_event_high_wm is the high water */
142626945a25Schristos /*   mark for this counter.)                                                */
142726945a25Schristos /* ------------------------------------------------------------------------ */
142826945a25Schristos static void
ipf_sync_wakeup(softc)142926945a25Schristos ipf_sync_wakeup(softc)
143026945a25Schristos 	ipf_main_softc_t *softc;
143126945a25Schristos {
143226945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
143326945a25Schristos 
143426945a25Schristos 	softs->ipf_sync_events++;
143526945a25Schristos 	if ((softc->ipf_ticks >
143626945a25Schristos 	    softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval) ||
143726945a25Schristos 	    (softs->ipf_sync_events > softs->ipf_sync_event_high_wm) ||
143826945a25Schristos 	    ((softs->sl_tail - softs->sl_idx) >
143926945a25Schristos 	     softs->ipf_sync_queue_high_wm) ||
144026945a25Schristos 	    ((softs->su_tail - softs->su_idx) >
144126945a25Schristos 	     softs->ipf_sync_queue_high_wm)) {
144226945a25Schristos 
144326945a25Schristos 		ipf_sync_poll_wakeup(softc);
144426945a25Schristos 	}
144526945a25Schristos }
144626945a25Schristos 
144726945a25Schristos 
144826945a25Schristos /* ------------------------------------------------------------------------ */
144926945a25Schristos /* Function:    ipf_sync_poll_wakeup                                        */
145026945a25Schristos /* Parameters:  Nil                                                         */
145126945a25Schristos /* Returns:     Nil                                                         */
145226945a25Schristos /*                                                                          */
145326945a25Schristos /* Deliver a poll wakeup and reset counters for two of the three heuristics */
145426945a25Schristos /* ------------------------------------------------------------------------ */
145526945a25Schristos static void
ipf_sync_poll_wakeup(softc)145626945a25Schristos ipf_sync_poll_wakeup(softc)
145726945a25Schristos 	ipf_main_softc_t *softc;
145826945a25Schristos {
145926945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
146026945a25Schristos 
146126945a25Schristos 	softs->ipf_sync_events = 0;
146226945a25Schristos 	softs->ipf_sync_lastwakeup = softc->ipf_ticks;
146326945a25Schristos 
146426945a25Schristos # ifdef _KERNEL
146526945a25Schristos #  if SOLARIS
146626945a25Schristos 	MUTEX_ENTER(&softs->ipsl_mutex);
146726945a25Schristos 	cv_signal(&softs->ipslwait);
146826945a25Schristos 	MUTEX_EXIT(&softs->ipsl_mutex);
146926945a25Schristos 	pollwakeup(&softc->ipf_poll_head[IPL_LOGSYNC], POLLIN|POLLRDNORM);
147026945a25Schristos #  else
147126945a25Schristos 	WAKEUP(&softs->sl_tail, 0);
147226945a25Schristos 	POLLWAKEUP(IPL_LOGSYNC);
147326945a25Schristos #  endif
147426945a25Schristos # endif
147526945a25Schristos }
147626945a25Schristos 
147726945a25Schristos 
147826945a25Schristos /* ------------------------------------------------------------------------ */
147926945a25Schristos /* Function:    ipf_sync_expire                                             */
148026945a25Schristos /* Parameters:  Nil                                                         */
148126945a25Schristos /* Returns:     Nil                                                         */
148226945a25Schristos /*                                                                          */
148326945a25Schristos /* This is the function called even ipf_tick.  It implements one of the     */
148426945a25Schristos /* three heuristics above *IF* there are events waiting.                    */
148526945a25Schristos /* ------------------------------------------------------------------------ */
148626945a25Schristos void
ipf_sync_expire(softc)148726945a25Schristos ipf_sync_expire(softc)
148826945a25Schristos 	ipf_main_softc_t *softc;
148926945a25Schristos {
149026945a25Schristos 	ipf_sync_softc_t *softs = softc->ipf_sync_soft;
149126945a25Schristos 
149226945a25Schristos 	if ((softs->ipf_sync_events > 0) &&
149326945a25Schristos 	    (softc->ipf_ticks >
149426945a25Schristos 	     softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval)) {
149526945a25Schristos 		ipf_sync_poll_wakeup(softc);
149626945a25Schristos 	}
149726945a25Schristos }
1498