ip_sync.c revision 1.5 1 1.5 martin /* $NetBSD: ip_sync.c,v 1.5 2013/09/14 12:18:06 martin Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.3 darrenr * Copyright (C) 2012 by Darren Reed.
5 1.1 christos *
6 1.1 christos * See the IPFILTER.LICENCE file for details on licencing.
7 1.1 christos */
8 1.1 christos #if defined(KERNEL) || defined(_KERNEL)
9 1.1 christos # undef KERNEL
10 1.1 christos # undef _KERNEL
11 1.1 christos # define KERNEL 1
12 1.1 christos # define _KERNEL 1
13 1.1 christos #endif
14 1.1 christos #include <sys/errno.h>
15 1.1 christos #include <sys/types.h>
16 1.1 christos #include <sys/param.h>
17 1.1 christos #include <sys/file.h>
18 1.1 christos #if !defined(_KERNEL) && !defined(__KERNEL__)
19 1.1 christos # include <stdio.h>
20 1.1 christos # include <stdlib.h>
21 1.1 christos # include <string.h>
22 1.1 christos # define _KERNEL
23 1.1 christos # define KERNEL
24 1.1 christos # ifdef __OpenBSD__
25 1.1 christos struct file;
26 1.1 christos # endif
27 1.1 christos # include <sys/uio.h>
28 1.1 christos # undef _KERNEL
29 1.1 christos # undef KERNEL
30 1.1 christos #else
31 1.1 christos # include <sys/systm.h>
32 1.1 christos # if !defined(__SVR4) && !defined(__svr4__)
33 1.1 christos # include <sys/mbuf.h>
34 1.1 christos # endif
35 1.1 christos # include <sys/select.h>
36 1.1 christos # if __FreeBSD_version >= 500000
37 1.1 christos # include <sys/selinfo.h>
38 1.1 christos # endif
39 1.1 christos #endif
40 1.1 christos #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
41 1.1 christos # include <sys/proc.h>
42 1.1 christos #endif
43 1.1 christos #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
44 1.1 christos # include <sys/filio.h>
45 1.1 christos # include <sys/fcntl.h>
46 1.1 christos #else
47 1.1 christos # include <sys/ioctl.h>
48 1.1 christos #endif
49 1.1 christos #include <sys/time.h>
50 1.1 christos #if !defined(linux)
51 1.1 christos # include <sys/protosw.h>
52 1.1 christos #endif
53 1.1 christos #include <sys/socket.h>
54 1.1 christos #if defined(__SVR4) || defined(__svr4__)
55 1.1 christos # include <sys/filio.h>
56 1.1 christos # include <sys/byteorder.h>
57 1.1 christos # ifdef _KERNEL
58 1.1 christos # include <sys/dditypes.h>
59 1.1 christos # endif
60 1.1 christos # include <sys/stream.h>
61 1.1 christos # include <sys/kmem.h>
62 1.1 christos #endif
63 1.1 christos
64 1.1 christos #include <net/if.h>
65 1.1 christos #ifdef sun
66 1.1 christos # include <net/af.h>
67 1.1 christos #endif
68 1.1 christos #include <netinet/in.h>
69 1.1 christos #include <netinet/in_systm.h>
70 1.1 christos #include <netinet/ip.h>
71 1.1 christos #include <netinet/tcp.h>
72 1.1 christos #if !defined(linux)
73 1.1 christos # include <netinet/ip_var.h>
74 1.1 christos #endif
75 1.1 christos #if !defined(__hpux) && !defined(linux)
76 1.1 christos # include <netinet/tcp_fsm.h>
77 1.1 christos #endif
78 1.1 christos #include <netinet/udp.h>
79 1.1 christos #include <netinet/ip_icmp.h>
80 1.1 christos #include "netinet/ip_compat.h"
81 1.1 christos #include <netinet/tcpip.h>
82 1.1 christos #include "netinet/ip_fil.h"
83 1.1 christos #include "netinet/ip_nat.h"
84 1.1 christos #include "netinet/ip_frag.h"
85 1.1 christos #include "netinet/ip_state.h"
86 1.1 christos #include "netinet/ip_proxy.h"
87 1.1 christos #include "netinet/ip_sync.h"
88 1.1 christos #ifdef USE_INET6
89 1.1 christos #include <netinet/icmp6.h>
90 1.1 christos #endif
91 1.1 christos #if (__FreeBSD_version >= 300000)
92 1.1 christos # include <sys/malloc.h>
93 1.1 christos # if defined(_KERNEL) && !defined(IPFILTER_LKM)
94 1.1 christos # include <sys/libkern.h>
95 1.1 christos # include <sys/systm.h>
96 1.1 christos # endif
97 1.1 christos #endif
98 1.1 christos /* END OF INCLUDES */
99 1.1 christos
100 1.1 christos #if !defined(lint)
101 1.2 christos #if defined(__NetBSD__)
102 1.2 christos #include <sys/cdefs.h>
103 1.5 martin __KERNEL_RCSID(0, "$NetBSD: ip_sync.c,v 1.5 2013/09/14 12:18:06 martin Exp $");
104 1.2 christos #else
105 1.3 darrenr static const char rcsid[] = "@(#)Id: ip_sync.c,v 1.1.1.2 2012/07/22 13:45:38 darrenr Exp";
106 1.2 christos #endif
107 1.1 christos #endif
108 1.1 christos
109 1.1 christos #define SYNC_STATETABSZ 256
110 1.1 christos #define SYNC_NATTABSZ 256
111 1.1 christos
112 1.1 christos typedef struct ipf_sync_softc_s {
113 1.1 christos ipfmutex_t ipf_syncadd;
114 1.1 christos ipfmutex_t ipsl_mutex;
115 1.1 christos ipfrwlock_t ipf_syncstate;
116 1.1 christos ipfrwlock_t ipf_syncnat;
117 1.1 christos #if SOLARIS && defined(_KERNEL)
118 1.1 christos kcondvar_t ipslwait;
119 1.1 christos #endif
120 1.1 christos #if defined(linux) && defined(_KERNEL)
121 1.1 christos wait_queue_head_t sl_tail_linux;
122 1.1 christos #endif
123 1.1 christos synclist_t **syncstatetab;
124 1.1 christos synclist_t **syncnattab;
125 1.1 christos synclogent_t *synclog;
126 1.1 christos syncupdent_t *syncupd;
127 1.1 christos u_int ipf_sync_num;
128 1.1 christos u_int ipf_sync_wrap;
129 1.1 christos u_int sl_idx; /* next available sync log entry */
130 1.1 christos u_int su_idx; /* next available sync update entry */
131 1.1 christos u_int sl_tail; /* next sync log entry to read */
132 1.1 christos u_int su_tail; /* next sync update entry to read */
133 1.1 christos int ipf_sync_log_sz;
134 1.1 christos int ipf_sync_nat_tab_sz;
135 1.1 christos int ipf_sync_state_tab_sz;
136 1.1 christos int ipf_sync_debug;
137 1.1 christos int ipf_sync_events;
138 1.1 christos u_32_t ipf_sync_lastwakeup;
139 1.1 christos int ipf_sync_wake_interval;
140 1.1 christos int ipf_sync_event_high_wm;
141 1.1 christos int ipf_sync_queue_high_wm;
142 1.1 christos int ipf_sync_inited;
143 1.1 christos } ipf_sync_softc_t;
144 1.1 christos
145 1.2 christos static int ipf_sync_flush_table(ipf_sync_softc_t *, int, synclist_t **);
146 1.2 christos static void ipf_sync_wakeup(ipf_main_softc_t *);
147 1.2 christos static void ipf_sync_del(ipf_sync_softc_t *, synclist_t *);
148 1.2 christos static void ipf_sync_poll_wakeup(ipf_main_softc_t *);
149 1.2 christos static int ipf_sync_nat(ipf_main_softc_t *, synchdr_t *, void *);
150 1.2 christos static int ipf_sync_state(ipf_main_softc_t *, synchdr_t *, void *);
151 1.1 christos
152 1.1 christos # if !defined(sparc) && !defined(__hppa)
153 1.2 christos void ipf_sync_tcporder(int, struct tcpdata *);
154 1.2 christos void ipf_sync_natorder(int, struct nat *);
155 1.2 christos void ipf_sync_storder(int, struct ipstate *);
156 1.1 christos # endif
157 1.1 christos
158 1.1 christos
159 1.1 christos void *
160 1.2 christos ipf_sync_soft_create(ipf_main_softc_t *softc)
161 1.1 christos {
162 1.1 christos ipf_sync_softc_t *softs;
163 1.1 christos
164 1.1 christos KMALLOC(softs, ipf_sync_softc_t *);
165 1.3 darrenr if (softs == NULL) {
166 1.3 darrenr IPFERROR(110024);
167 1.1 christos return NULL;
168 1.3 darrenr }
169 1.1 christos
170 1.1 christos bzero((char *)softs, sizeof(*softs));
171 1.1 christos
172 1.1 christos softs->ipf_sync_log_sz = SYNCLOG_SZ;
173 1.1 christos softs->ipf_sync_nat_tab_sz = SYNC_STATETABSZ;
174 1.1 christos softs->ipf_sync_state_tab_sz = SYNC_STATETABSZ;
175 1.1 christos softs->ipf_sync_event_high_wm = SYNCLOG_SZ * 100 / 90; /* 90% */
176 1.1 christos softs->ipf_sync_queue_high_wm = SYNCLOG_SZ * 100 / 90; /* 90% */
177 1.1 christos
178 1.1 christos return softs;
179 1.1 christos }
180 1.1 christos
181 1.1 christos
182 1.1 christos /* ------------------------------------------------------------------------ */
183 1.1 christos /* Function: ipf_sync_init */
184 1.1 christos /* Returns: int - 0 == success, -1 == failure */
185 1.1 christos /* Parameters: Nil */
186 1.1 christos /* */
187 1.1 christos /* Initialise all of the locks required for the sync code and initialise */
188 1.1 christos /* any data structures, as required. */
189 1.1 christos /* ------------------------------------------------------------------------ */
190 1.1 christos int
191 1.2 christos ipf_sync_soft_init(ipf_main_softc_t *softc, void *arg)
192 1.1 christos {
193 1.1 christos ipf_sync_softc_t *softs = arg;
194 1.1 christos
195 1.1 christos KMALLOCS(softs->synclog, synclogent_t *,
196 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->synclog));
197 1.1 christos if (softs->synclog == NULL)
198 1.1 christos return -1;
199 1.1 christos bzero((char *)softs->synclog,
200 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->synclog));
201 1.1 christos
202 1.1 christos KMALLOCS(softs->syncupd, syncupdent_t *,
203 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
204 1.1 christos if (softs->syncupd == NULL)
205 1.1 christos return -2;
206 1.1 christos bzero((char *)softs->syncupd,
207 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
208 1.1 christos
209 1.1 christos KMALLOCS(softs->syncstatetab, synclist_t **,
210 1.1 christos softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
211 1.1 christos if (softs->syncstatetab == NULL)
212 1.1 christos return -3;
213 1.1 christos bzero((char *)softs->syncstatetab,
214 1.1 christos softs->ipf_sync_state_tab_sz * sizeof(*softs->syncstatetab));
215 1.1 christos
216 1.1 christos KMALLOCS(softs->syncnattab, synclist_t **,
217 1.1 christos softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
218 1.1 christos if (softs->syncnattab == NULL)
219 1.1 christos return -3;
220 1.1 christos bzero((char *)softs->syncnattab,
221 1.1 christos softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
222 1.1 christos
223 1.1 christos softs->ipf_sync_num = 1;
224 1.1 christos softs->ipf_sync_wrap = 0;
225 1.1 christos softs->sl_idx = 0;
226 1.1 christos softs->su_idx = 0;
227 1.1 christos softs->sl_tail = 0;
228 1.1 christos softs->su_tail = 0;
229 1.1 christos softs->ipf_sync_events = 0;
230 1.1 christos softs->ipf_sync_lastwakeup = 0;
231 1.1 christos
232 1.1 christos
233 1.1 christos # if SOLARIS && defined(_KERNEL)
234 1.1 christos cv_init(&softs->ipslwait, "ipsl condvar", CV_DRIVER, NULL);
235 1.1 christos # endif
236 1.1 christos RWLOCK_INIT(&softs->ipf_syncstate, "add things to state sync table");
237 1.1 christos RWLOCK_INIT(&softs->ipf_syncnat, "add things to nat sync table");
238 1.1 christos MUTEX_INIT(&softs->ipf_syncadd, "add things to sync table");
239 1.1 christos MUTEX_INIT(&softs->ipsl_mutex, "read ring lock");
240 1.1 christos
241 1.1 christos softs->ipf_sync_inited = 1;
242 1.1 christos
243 1.1 christos return 0;
244 1.1 christos }
245 1.1 christos
246 1.1 christos
247 1.1 christos /* ------------------------------------------------------------------------ */
248 1.1 christos /* Function: ipf_sync_unload */
249 1.1 christos /* Returns: int - 0 == success, -1 == failure */
250 1.1 christos /* Parameters: Nil */
251 1.1 christos /* */
252 1.1 christos /* Destroy the locks created when initialising and free any memory in use */
253 1.1 christos /* with the synchronisation tables. */
254 1.1 christos /* ------------------------------------------------------------------------ */
255 1.1 christos int
256 1.2 christos ipf_sync_soft_fini(ipf_main_softc_t *softc, void *arg)
257 1.1 christos {
258 1.1 christos ipf_sync_softc_t *softs = arg;
259 1.1 christos
260 1.1 christos if (softs->syncnattab != NULL) {
261 1.1 christos ipf_sync_flush_table(softs, softs->ipf_sync_nat_tab_sz,
262 1.1 christos softs->syncnattab);
263 1.1 christos KFREES(softs->syncnattab,
264 1.1 christos softs->ipf_sync_nat_tab_sz * sizeof(*softs->syncnattab));
265 1.1 christos softs->syncnattab = NULL;
266 1.1 christos }
267 1.1 christos
268 1.1 christos if (softs->syncstatetab != NULL) {
269 1.1 christos ipf_sync_flush_table(softs, softs->ipf_sync_state_tab_sz,
270 1.1 christos softs->syncstatetab);
271 1.1 christos KFREES(softs->syncstatetab,
272 1.1 christos softs->ipf_sync_state_tab_sz *
273 1.1 christos sizeof(*softs->syncstatetab));
274 1.1 christos softs->syncstatetab = NULL;
275 1.1 christos }
276 1.1 christos
277 1.1 christos if (softs->syncupd != NULL) {
278 1.1 christos KFREES(softs->syncupd,
279 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->syncupd));
280 1.1 christos softs->syncupd = NULL;
281 1.1 christos }
282 1.1 christos
283 1.1 christos if (softs->synclog != NULL) {
284 1.1 christos KFREES(softs->synclog,
285 1.1 christos softs->ipf_sync_log_sz * sizeof(*softs->synclog));
286 1.1 christos softs->synclog = NULL;
287 1.1 christos }
288 1.1 christos
289 1.1 christos if (softs->ipf_sync_inited == 1) {
290 1.1 christos MUTEX_DESTROY(&softs->ipsl_mutex);
291 1.1 christos MUTEX_DESTROY(&softs->ipf_syncadd);
292 1.1 christos RW_DESTROY(&softs->ipf_syncnat);
293 1.1 christos RW_DESTROY(&softs->ipf_syncstate);
294 1.1 christos softs->ipf_sync_inited = 0;
295 1.1 christos }
296 1.1 christos
297 1.1 christos return 0;
298 1.1 christos }
299 1.1 christos
300 1.1 christos void
301 1.2 christos ipf_sync_soft_destroy(ipf_main_softc_t *softc, void *arg)
302 1.1 christos {
303 1.1 christos ipf_sync_softc_t *softs = arg;
304 1.1 christos
305 1.1 christos KFREE(softs);
306 1.1 christos }
307 1.1 christos
308 1.1 christos
309 1.1 christos # if !defined(sparc) && !defined(__hppa)
310 1.1 christos /* ------------------------------------------------------------------------ */
311 1.1 christos /* Function: ipf_sync_tcporder */
312 1.1 christos /* Returns: Nil */
313 1.1 christos /* Parameters: way(I) - direction of byte order conversion. */
314 1.1 christos /* td(IO) - pointer to data to be converted. */
315 1.1 christos /* */
316 1.1 christos /* Do byte swapping on values in the TCP state information structure that */
317 1.1 christos /* need to be used at both ends by the host in their native byte order. */
318 1.1 christos /* ------------------------------------------------------------------------ */
319 1.1 christos void
320 1.2 christos ipf_sync_tcporder(int way, tcpdata_t *td)
321 1.1 christos {
322 1.1 christos if (way) {
323 1.1 christos td->td_maxwin = htons(td->td_maxwin);
324 1.1 christos td->td_end = htonl(td->td_end);
325 1.1 christos td->td_maxend = htonl(td->td_maxend);
326 1.1 christos } else {
327 1.1 christos td->td_maxwin = ntohs(td->td_maxwin);
328 1.1 christos td->td_end = ntohl(td->td_end);
329 1.1 christos td->td_maxend = ntohl(td->td_maxend);
330 1.1 christos }
331 1.1 christos }
332 1.1 christos
333 1.1 christos
334 1.1 christos /* ------------------------------------------------------------------------ */
335 1.1 christos /* Function: ipf_sync_natorder */
336 1.1 christos /* Returns: Nil */
337 1.1 christos /* Parameters: way(I) - direction of byte order conversion. */
338 1.1 christos /* nat(IO) - pointer to data to be converted. */
339 1.1 christos /* */
340 1.1 christos /* Do byte swapping on values in the NAT data structure that need to be */
341 1.1 christos /* used at both ends by the host in their native byte order. */
342 1.1 christos /* ------------------------------------------------------------------------ */
343 1.1 christos void
344 1.2 christos ipf_sync_natorder(int way, nat_t *n)
345 1.1 christos {
346 1.1 christos if (way) {
347 1.1 christos n->nat_age = htonl(n->nat_age);
348 1.1 christos n->nat_flags = htonl(n->nat_flags);
349 1.1 christos n->nat_ipsumd = htonl(n->nat_ipsumd);
350 1.1 christos n->nat_use = htonl(n->nat_use);
351 1.1 christos n->nat_dir = htonl(n->nat_dir);
352 1.1 christos } else {
353 1.1 christos n->nat_age = ntohl(n->nat_age);
354 1.1 christos n->nat_flags = ntohl(n->nat_flags);
355 1.1 christos n->nat_ipsumd = ntohl(n->nat_ipsumd);
356 1.1 christos n->nat_use = ntohl(n->nat_use);
357 1.1 christos n->nat_dir = ntohl(n->nat_dir);
358 1.1 christos }
359 1.1 christos }
360 1.1 christos
361 1.1 christos
362 1.1 christos /* ------------------------------------------------------------------------ */
363 1.1 christos /* Function: ipf_sync_storder */
364 1.1 christos /* Returns: Nil */
365 1.1 christos /* Parameters: way(I) - direction of byte order conversion. */
366 1.1 christos /* ips(IO) - pointer to data to be converted. */
367 1.1 christos /* */
368 1.1 christos /* Do byte swapping on values in the IP state data structure that need to */
369 1.1 christos /* be used at both ends by the host in their native byte order. */
370 1.1 christos /* ------------------------------------------------------------------------ */
371 1.1 christos void
372 1.2 christos ipf_sync_storder(int way, ipstate_t *ips)
373 1.1 christos {
374 1.1 christos ipf_sync_tcporder(way, &ips->is_tcp.ts_data[0]);
375 1.1 christos ipf_sync_tcporder(way, &ips->is_tcp.ts_data[1]);
376 1.1 christos
377 1.1 christos if (way) {
378 1.1 christos ips->is_hv = htonl(ips->is_hv);
379 1.1 christos ips->is_die = htonl(ips->is_die);
380 1.1 christos ips->is_pass = htonl(ips->is_pass);
381 1.1 christos ips->is_flags = htonl(ips->is_flags);
382 1.1 christos ips->is_opt[0] = htonl(ips->is_opt[0]);
383 1.1 christos ips->is_opt[1] = htonl(ips->is_opt[1]);
384 1.1 christos ips->is_optmsk[0] = htonl(ips->is_optmsk[0]);
385 1.1 christos ips->is_optmsk[1] = htonl(ips->is_optmsk[1]);
386 1.1 christos ips->is_sec = htons(ips->is_sec);
387 1.1 christos ips->is_secmsk = htons(ips->is_secmsk);
388 1.1 christos ips->is_auth = htons(ips->is_auth);
389 1.1 christos ips->is_authmsk = htons(ips->is_authmsk);
390 1.1 christos ips->is_s0[0] = htonl(ips->is_s0[0]);
391 1.1 christos ips->is_s0[1] = htonl(ips->is_s0[1]);
392 1.1 christos ips->is_smsk[0] = htons(ips->is_smsk[0]);
393 1.1 christos ips->is_smsk[1] = htons(ips->is_smsk[1]);
394 1.1 christos } else {
395 1.1 christos ips->is_hv = ntohl(ips->is_hv);
396 1.1 christos ips->is_die = ntohl(ips->is_die);
397 1.1 christos ips->is_pass = ntohl(ips->is_pass);
398 1.1 christos ips->is_flags = ntohl(ips->is_flags);
399 1.1 christos ips->is_opt[0] = ntohl(ips->is_opt[0]);
400 1.1 christos ips->is_opt[1] = ntohl(ips->is_opt[1]);
401 1.1 christos ips->is_optmsk[0] = ntohl(ips->is_optmsk[0]);
402 1.1 christos ips->is_optmsk[1] = ntohl(ips->is_optmsk[1]);
403 1.1 christos ips->is_sec = ntohs(ips->is_sec);
404 1.1 christos ips->is_secmsk = ntohs(ips->is_secmsk);
405 1.1 christos ips->is_auth = ntohs(ips->is_auth);
406 1.1 christos ips->is_authmsk = ntohs(ips->is_authmsk);
407 1.1 christos ips->is_s0[0] = ntohl(ips->is_s0[0]);
408 1.1 christos ips->is_s0[1] = ntohl(ips->is_s0[1]);
409 1.1 christos ips->is_smsk[0] = ntohl(ips->is_smsk[0]);
410 1.1 christos ips->is_smsk[1] = ntohl(ips->is_smsk[1]);
411 1.1 christos }
412 1.1 christos }
413 1.1 christos # else /* !defined(sparc) && !defined(__hppa) */
414 1.1 christos # define ipf_sync_tcporder(x,y)
415 1.1 christos # define ipf_sync_natorder(x,y)
416 1.1 christos # define ipf_sync_storder(x,y)
417 1.1 christos # endif /* !defined(sparc) && !defined(__hppa) */
418 1.1 christos
419 1.1 christos
420 1.1 christos /* ------------------------------------------------------------------------ */
421 1.1 christos /* Function: ipf_sync_write */
422 1.1 christos /* Returns: int - 0 == success, else error value. */
423 1.1 christos /* Parameters: uio(I) - pointer to information about data to write */
424 1.1 christos /* */
425 1.1 christos /* Moves data from user space into the kernel and uses it for updating data */
426 1.1 christos /* structures in the state/NAT tables. */
427 1.1 christos /* ------------------------------------------------------------------------ */
428 1.1 christos int
429 1.2 christos ipf_sync_write(ipf_main_softc_t *softc, struct uio *uio)
430 1.1 christos {
431 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
432 1.1 christos synchdr_t sh;
433 1.1 christos
434 1.1 christos /*
435 1.1 christos * THIS MUST BE SUFFICIENT LARGE TO STORE
436 1.1 christos * ANY POSSIBLE DATA TYPE
437 1.1 christos */
438 1.1 christos char data[2048];
439 1.1 christos
440 1.1 christos int err = 0;
441 1.1 christos
442 1.1 christos # if BSD_GE_YEAR(199306) || defined(__FreeBSD__) || defined(__osf__)
443 1.1 christos uio->uio_rw = UIO_WRITE;
444 1.1 christos # endif
445 1.1 christos
446 1.1 christos /* Try to get bytes */
447 1.1 christos while (uio->uio_resid > 0) {
448 1.1 christos
449 1.1 christos if (uio->uio_resid >= sizeof(sh)) {
450 1.1 christos
451 1.2 christos err = UIOMOVE((void *)&sh, sizeof(sh), UIO_WRITE, uio);
452 1.1 christos
453 1.1 christos if (err) {
454 1.1 christos if (softs->ipf_sync_debug > 2)
455 1.1 christos printf("uiomove(header) failed: %d\n",
456 1.1 christos err);
457 1.1 christos return err;
458 1.1 christos }
459 1.1 christos
460 1.1 christos /* convert to host order */
461 1.1 christos sh.sm_magic = ntohl(sh.sm_magic);
462 1.1 christos sh.sm_len = ntohl(sh.sm_len);
463 1.1 christos sh.sm_num = ntohl(sh.sm_num);
464 1.1 christos
465 1.1 christos if (softs->ipf_sync_debug > 8)
466 1.1 christos printf("[%d] Read v:%d p:%d cmd:%d table:%d rev:%d len:%d magic:%x\n",
467 1.1 christos sh.sm_num, sh.sm_v, sh.sm_p, sh.sm_cmd,
468 1.1 christos sh.sm_table, sh.sm_rev, sh.sm_len,
469 1.1 christos sh.sm_magic);
470 1.1 christos
471 1.1 christos if (sh.sm_magic != SYNHDRMAGIC) {
472 1.1 christos if (softs->ipf_sync_debug > 2)
473 1.1 christos printf("uiomove(header) invalid %s\n",
474 1.1 christos "magic");
475 1.1 christos IPFERROR(110001);
476 1.1 christos return EINVAL;
477 1.1 christos }
478 1.1 christos
479 1.1 christos if (sh.sm_v != 4 && sh.sm_v != 6) {
480 1.1 christos if (softs->ipf_sync_debug > 2)
481 1.1 christos printf("uiomove(header) invalid %s\n",
482 1.1 christos "protocol");
483 1.1 christos IPFERROR(110002);
484 1.1 christos return EINVAL;
485 1.1 christos }
486 1.1 christos
487 1.1 christos if (sh.sm_cmd > SMC_MAXCMD) {
488 1.1 christos if (softs->ipf_sync_debug > 2)
489 1.1 christos printf("uiomove(header) invalid %s\n",
490 1.1 christos "command");
491 1.1 christos IPFERROR(110003);
492 1.1 christos return EINVAL;
493 1.1 christos }
494 1.1 christos
495 1.1 christos
496 1.1 christos if (sh.sm_table > SMC_MAXTBL) {
497 1.1 christos if (softs->ipf_sync_debug > 2)
498 1.1 christos printf("uiomove(header) invalid %s\n",
499 1.1 christos "table");
500 1.1 christos IPFERROR(110004);
501 1.1 christos return EINVAL;
502 1.1 christos }
503 1.1 christos
504 1.1 christos } else {
505 1.1 christos /* unsufficient data, wait until next call */
506 1.1 christos if (softs->ipf_sync_debug > 2)
507 1.1 christos printf("uiomove(header) insufficient data");
508 1.1 christos IPFERROR(110005);
509 1.1 christos return EAGAIN;
510 1.1 christos }
511 1.1 christos
512 1.1 christos
513 1.1 christos /*
514 1.1 christos * We have a header, so try to read the amount of data
515 1.1 christos * needed for the request
516 1.1 christos */
517 1.1 christos
518 1.1 christos /* not supported */
519 1.1 christos if (sh.sm_len == 0) {
520 1.1 christos if (softs->ipf_sync_debug > 2)
521 1.1 christos printf("uiomove(data zero length %s\n",
522 1.1 christos "not supported");
523 1.1 christos IPFERROR(110006);
524 1.1 christos return EINVAL;
525 1.1 christos }
526 1.1 christos
527 1.1 christos if (uio->uio_resid >= sh.sm_len) {
528 1.1 christos
529 1.2 christos err = UIOMOVE((void *)data, sh.sm_len, UIO_WRITE, uio);
530 1.1 christos
531 1.1 christos if (err) {
532 1.1 christos if (softs->ipf_sync_debug > 2)
533 1.1 christos printf("uiomove(data) failed: %d\n",
534 1.1 christos err);
535 1.1 christos return err;
536 1.1 christos }
537 1.1 christos
538 1.1 christos if (softs->ipf_sync_debug > 7)
539 1.1 christos printf("uiomove(data) %d bytes read\n",
540 1.1 christos sh.sm_len);
541 1.1 christos
542 1.1 christos if (sh.sm_table == SMC_STATE)
543 1.1 christos err = ipf_sync_state(softc, &sh, data);
544 1.1 christos else if (sh.sm_table == SMC_NAT)
545 1.1 christos err = ipf_sync_nat(softc, &sh, data);
546 1.1 christos if (softs->ipf_sync_debug > 7)
547 1.1 christos printf("[%d] Finished with error %d\n",
548 1.1 christos sh.sm_num, err);
549 1.1 christos
550 1.1 christos } else {
551 1.1 christos /* insufficient data, wait until next call */
552 1.1 christos if (softs->ipf_sync_debug > 2)
553 1.4 martin printf("uiomove(data) %s %d bytes, got %d\n",
554 1.1 christos "insufficient data, need",
555 1.3 darrenr sh.sm_len, (int)uio->uio_resid);
556 1.1 christos IPFERROR(110007);
557 1.1 christos return EAGAIN;
558 1.1 christos }
559 1.1 christos }
560 1.1 christos
561 1.1 christos /* no more data */
562 1.1 christos return 0;
563 1.1 christos }
564 1.1 christos
565 1.1 christos
566 1.1 christos /* ------------------------------------------------------------------------ */
567 1.1 christos /* Function: ipf_sync_read */
568 1.1 christos /* Returns: int - 0 == success, else error value. */
569 1.1 christos /* Parameters: uio(O) - pointer to information about where to store data */
570 1.1 christos /* */
571 1.1 christos /* This function is called when a user program wants to read some data */
572 1.1 christos /* for pending state/NAT updates. If no data is available, the caller is */
573 1.1 christos /* put to sleep, pending a wakeup from the "lower half" of this code. */
574 1.1 christos /* ------------------------------------------------------------------------ */
575 1.1 christos int
576 1.2 christos ipf_sync_read(ipf_main_softc_t *softc, struct uio *uio)
577 1.1 christos {
578 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
579 1.1 christos syncupdent_t *su;
580 1.1 christos synclogent_t *sl;
581 1.1 christos int err = 0;
582 1.1 christos
583 1.1 christos if ((uio->uio_resid & 3) || (uio->uio_resid < 8)) {
584 1.1 christos IPFERROR(110008);
585 1.1 christos return EINVAL;
586 1.1 christos }
587 1.1 christos
588 1.1 christos # if BSD_GE_YEAR(199306) || defined(__FreeBSD__) || defined(__osf__)
589 1.1 christos uio->uio_rw = UIO_READ;
590 1.1 christos # endif
591 1.1 christos
592 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
593 1.1 christos while ((softs->sl_tail == softs->sl_idx) &&
594 1.1 christos (softs->su_tail == softs->su_idx)) {
595 1.1 christos # if defined(_KERNEL)
596 1.1 christos # if SOLARIS
597 1.1 christos if (!cv_wait_sig(&softs->ipslwait, &softs->ipsl_mutex.ipf_lk)) {
598 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
599 1.1 christos IPFERROR(110009);
600 1.1 christos return EINTR;
601 1.1 christos }
602 1.1 christos # else
603 1.1 christos # ifdef __hpux
604 1.1 christos {
605 1.1 christos lock_t *l;
606 1.1 christos
607 1.1 christos l = get_sleep_lock(&softs->sl_tail);
608 1.1 christos err = sleep(&softs->sl_tail, PZERO+1);
609 1.1 christos if (err) {
610 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
611 1.1 christos IPFERROR(110010);
612 1.1 christos return EINTR;
613 1.1 christos }
614 1.1 christos spinunlock(l);
615 1.1 christos }
616 1.1 christos # else /* __hpux */
617 1.1 christos # ifdef __osf__
618 1.1 christos err = mpsleep(&softs->sl_tail, PSUSP|PCATCH, "ipl sleep", 0,
619 1.1 christos &softs->ipsl_mutex, MS_LOCK_SIMPLE);
620 1.1 christos if (err) {
621 1.1 christos IPFERROR(110011);
622 1.1 christos return EINTR;
623 1.1 christos }
624 1.1 christos # else
625 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
626 1.1 christos err = SLEEP(&softs->sl_tail, "ipl sleep");
627 1.1 christos if (err) {
628 1.1 christos IPFERROR(110012);
629 1.1 christos return EINTR;
630 1.1 christos }
631 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
632 1.1 christos # endif /* __osf__ */
633 1.1 christos # endif /* __hpux */
634 1.1 christos # endif /* SOLARIS */
635 1.1 christos # endif /* _KERNEL */
636 1.1 christos }
637 1.1 christos
638 1.1 christos while ((softs->sl_tail < softs->sl_idx) &&
639 1.1 christos (uio->uio_resid > sizeof(*sl))) {
640 1.1 christos sl = softs->synclog + softs->sl_tail++;
641 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
642 1.1 christos err = UIOMOVE(sl, sizeof(*sl), UIO_READ, uio);
643 1.1 christos if (err != 0)
644 1.1 christos goto goterror;
645 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
646 1.1 christos }
647 1.1 christos
648 1.1 christos while ((softs->su_tail < softs->su_idx) &&
649 1.1 christos (uio->uio_resid > sizeof(*su))) {
650 1.1 christos su = softs->syncupd + softs->su_tail;
651 1.1 christos softs->su_tail++;
652 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
653 1.1 christos err = UIOMOVE(su, sizeof(*su), UIO_READ, uio);
654 1.1 christos if (err != 0)
655 1.1 christos goto goterror;
656 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
657 1.1 christos if (su->sup_hdr.sm_sl != NULL)
658 1.1 christos su->sup_hdr.sm_sl->sl_idx = -1;
659 1.1 christos }
660 1.1 christos if (softs->sl_tail == softs->sl_idx)
661 1.1 christos softs->sl_tail = softs->sl_idx = 0;
662 1.1 christos if (softs->su_tail == softs->su_idx)
663 1.1 christos softs->su_tail = softs->su_idx = 0;
664 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
665 1.1 christos goterror:
666 1.1 christos return err;
667 1.1 christos }
668 1.1 christos
669 1.1 christos
670 1.1 christos /* ------------------------------------------------------------------------ */
671 1.1 christos /* Function: ipf_sync_state */
672 1.1 christos /* Returns: int - 0 == success, else error value. */
673 1.1 christos /* Parameters: sp(I) - pointer to sync packet data header */
674 1.1 christos /* uio(I) - pointer to user data for further information */
675 1.1 christos /* */
676 1.1 christos /* Updates the state table according to information passed in the sync */
677 1.1 christos /* header. As required, more data is fetched from the uio structure but */
678 1.1 christos /* varies depending on the contents of the sync header. This function can */
679 1.1 christos /* create a new state entry or update one. Deletion is left to the state */
680 1.1 christos /* structures being timed out correctly. */
681 1.1 christos /* ------------------------------------------------------------------------ */
682 1.1 christos static int
683 1.2 christos ipf_sync_state(ipf_main_softc_t *softc, synchdr_t *sp, void *data)
684 1.1 christos {
685 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
686 1.1 christos synctcp_update_t su;
687 1.1 christos ipstate_t *is, sn;
688 1.1 christos synclist_t *sl;
689 1.1 christos frentry_t *fr;
690 1.1 christos u_int hv;
691 1.1 christos int err = 0;
692 1.1 christos
693 1.1 christos hv = sp->sm_num & (softs->ipf_sync_state_tab_sz - 1);
694 1.1 christos
695 1.1 christos switch (sp->sm_cmd)
696 1.1 christos {
697 1.1 christos case SMC_CREATE :
698 1.1 christos
699 1.1 christos bcopy(data, &sn, sizeof(sn));
700 1.1 christos KMALLOC(is, ipstate_t *);
701 1.1 christos if (is == NULL) {
702 1.1 christos IPFERROR(110013);
703 1.1 christos err = ENOMEM;
704 1.1 christos break;
705 1.1 christos }
706 1.1 christos
707 1.1 christos KMALLOC(sl, synclist_t *);
708 1.1 christos if (sl == NULL) {
709 1.1 christos IPFERROR(110014);
710 1.1 christos err = ENOMEM;
711 1.1 christos KFREE(is);
712 1.1 christos break;
713 1.1 christos }
714 1.1 christos
715 1.1 christos bzero((char *)is, offsetof(ipstate_t, is_die));
716 1.1 christos bcopy((char *)&sn.is_die, (char *)&is->is_die,
717 1.1 christos sizeof(*is) - offsetof(ipstate_t, is_die));
718 1.1 christos ipf_sync_storder(0, is);
719 1.1 christos
720 1.1 christos /*
721 1.1 christos * We need to find the same rule on the slave as was used on
722 1.1 christos * the master to create this state entry.
723 1.1 christos */
724 1.1 christos READ_ENTER(&softc->ipf_mutex);
725 1.1 christos fr = ipf_getrulen(softc, IPL_LOGIPF, sn.is_group, sn.is_rulen);
726 1.1 christos if (fr != NULL) {
727 1.1 christos MUTEX_ENTER(&fr->fr_lock);
728 1.1 christos fr->fr_ref++;
729 1.1 christos fr->fr_statecnt++;
730 1.1 christos MUTEX_EXIT(&fr->fr_lock);
731 1.1 christos }
732 1.1 christos RWLOCK_EXIT(&softc->ipf_mutex);
733 1.1 christos
734 1.1 christos if (softs->ipf_sync_debug > 4)
735 1.1 christos printf("[%d] Filter rules = %p\n", sp->sm_num, fr);
736 1.1 christos
737 1.1 christos is->is_rule = fr;
738 1.1 christos is->is_sync = sl;
739 1.1 christos
740 1.1 christos sl->sl_idx = -1;
741 1.1 christos sl->sl_ips = is;
742 1.1 christos bcopy(sp, &sl->sl_hdr, sizeof(struct synchdr));
743 1.1 christos
744 1.1 christos WRITE_ENTER(&softs->ipf_syncstate);
745 1.1 christos WRITE_ENTER(&softc->ipf_state);
746 1.1 christos
747 1.1 christos sl->sl_pnext = softs->syncstatetab + hv;
748 1.1 christos sl->sl_next = softs->syncstatetab[hv];
749 1.1 christos if (softs->syncstatetab[hv] != NULL)
750 1.1 christos softs->syncstatetab[hv]->sl_pnext = &sl->sl_next;
751 1.1 christos softs->syncstatetab[hv] = sl;
752 1.1 christos MUTEX_DOWNGRADE(&softs->ipf_syncstate);
753 1.1 christos ipf_state_insert(softc, is, sp->sm_rev);
754 1.1 christos /*
755 1.1 christos * Do not initialise the interface pointers for the state
756 1.1 christos * entry as the full complement of interface names may not
757 1.1 christos * be present.
758 1.1 christos *
759 1.1 christos * Put this state entry on its timeout queue.
760 1.1 christos */
761 1.1 christos /*fr_setstatequeue(is, sp->sm_rev);*/
762 1.1 christos break;
763 1.1 christos
764 1.1 christos case SMC_UPDATE :
765 1.1 christos bcopy(data, &su, sizeof(su));
766 1.1 christos
767 1.1 christos if (softs->ipf_sync_debug > 4)
768 1.1 christos printf("[%d] Update age %lu state %d/%d \n",
769 1.1 christos sp->sm_num, su.stu_age, su.stu_state[0],
770 1.1 christos su.stu_state[1]);
771 1.1 christos
772 1.1 christos READ_ENTER(&softs->ipf_syncstate);
773 1.1 christos for (sl = softs->syncstatetab[hv]; (sl != NULL);
774 1.1 christos sl = sl->sl_next)
775 1.1 christos if (sl->sl_hdr.sm_num == sp->sm_num)
776 1.1 christos break;
777 1.1 christos if (sl == NULL) {
778 1.1 christos if (softs->ipf_sync_debug > 1)
779 1.1 christos printf("[%d] State not found - can't update\n",
780 1.1 christos sp->sm_num);
781 1.1 christos RWLOCK_EXIT(&softs->ipf_syncstate);
782 1.1 christos IPFERROR(110015);
783 1.1 christos err = ENOENT;
784 1.1 christos break;
785 1.1 christos }
786 1.1 christos
787 1.1 christos READ_ENTER(&softc->ipf_state);
788 1.1 christos
789 1.1 christos if (softs->ipf_sync_debug > 6)
790 1.1 christos printf("[%d] Data from state v:%d p:%d cmd:%d table:%d rev:%d\n",
791 1.1 christos sp->sm_num, sl->sl_hdr.sm_v, sl->sl_hdr.sm_p,
792 1.1 christos sl->sl_hdr.sm_cmd, sl->sl_hdr.sm_table,
793 1.1 christos sl->sl_hdr.sm_rev);
794 1.1 christos
795 1.1 christos is = sl->sl_ips;
796 1.1 christos
797 1.1 christos MUTEX_ENTER(&is->is_lock);
798 1.1 christos switch (sp->sm_p)
799 1.1 christos {
800 1.1 christos case IPPROTO_TCP :
801 1.1 christos /* XXX FV --- shouldn't we do ntohl/htonl???? XXX */
802 1.1 christos is->is_send = su.stu_data[0].td_end;
803 1.1 christos is->is_maxsend = su.stu_data[0].td_maxend;
804 1.1 christos is->is_maxswin = su.stu_data[0].td_maxwin;
805 1.1 christos is->is_state[0] = su.stu_state[0];
806 1.1 christos is->is_dend = su.stu_data[1].td_end;
807 1.1 christos is->is_maxdend = su.stu_data[1].td_maxend;
808 1.1 christos is->is_maxdwin = su.stu_data[1].td_maxwin;
809 1.1 christos is->is_state[1] = su.stu_state[1];
810 1.1 christos break;
811 1.1 christos default :
812 1.1 christos break;
813 1.1 christos }
814 1.1 christos
815 1.1 christos if (softs->ipf_sync_debug > 6)
816 1.1 christos printf("[%d] Setting timers for state\n", sp->sm_num);
817 1.1 christos
818 1.1 christos ipf_state_setqueue(softc, is, sp->sm_rev);
819 1.1 christos
820 1.1 christos MUTEX_EXIT(&is->is_lock);
821 1.1 christos break;
822 1.1 christos
823 1.1 christos default :
824 1.1 christos IPFERROR(110016);
825 1.1 christos err = EINVAL;
826 1.1 christos break;
827 1.1 christos }
828 1.1 christos
829 1.1 christos if (err == 0) {
830 1.1 christos RWLOCK_EXIT(&softc->ipf_state);
831 1.1 christos RWLOCK_EXIT(&softs->ipf_syncstate);
832 1.1 christos }
833 1.1 christos
834 1.1 christos if (softs->ipf_sync_debug > 6)
835 1.1 christos printf("[%d] Update completed with error %d\n",
836 1.1 christos sp->sm_num, err);
837 1.1 christos
838 1.1 christos return err;
839 1.1 christos }
840 1.1 christos
841 1.1 christos
842 1.1 christos /* ------------------------------------------------------------------------ */
843 1.1 christos /* Function: ipf_sync_del */
844 1.1 christos /* Returns: Nil */
845 1.1 christos /* Parameters: sl(I) - pointer to synclist object to delete */
846 1.1 christos /* */
847 1.1 christos /* Deletes an object from the synclist. */
848 1.1 christos /* ------------------------------------------------------------------------ */
849 1.1 christos static void
850 1.2 christos ipf_sync_del(ipf_sync_softc_t *softs, synclist_t *sl)
851 1.1 christos {
852 1.1 christos *sl->sl_pnext = sl->sl_next;
853 1.1 christos if (sl->sl_next != NULL)
854 1.1 christos sl->sl_next->sl_pnext = sl->sl_pnext;
855 1.1 christos if (sl->sl_idx != -1)
856 1.1 christos softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
857 1.1 christos }
858 1.1 christos
859 1.1 christos
860 1.1 christos /* ------------------------------------------------------------------------ */
861 1.1 christos /* Function: ipf_sync_del_state */
862 1.1 christos /* Returns: Nil */
863 1.1 christos /* Parameters: sl(I) - pointer to synclist object to delete */
864 1.1 christos /* */
865 1.1 christos /* Deletes an object from the synclist state table and free's its memory. */
866 1.1 christos /* ------------------------------------------------------------------------ */
867 1.1 christos void
868 1.2 christos ipf_sync_del_state(void *arg, synclist_t *sl)
869 1.1 christos {
870 1.1 christos ipf_sync_softc_t *softs = arg;
871 1.1 christos
872 1.1 christos WRITE_ENTER(&softs->ipf_syncstate);
873 1.1 christos ipf_sync_del(softs, sl);
874 1.1 christos RWLOCK_EXIT(&softs->ipf_syncstate);
875 1.1 christos KFREE(sl);
876 1.1 christos }
877 1.1 christos
878 1.1 christos
879 1.1 christos /* ------------------------------------------------------------------------ */
880 1.1 christos /* Function: ipf_sync_del_nat */
881 1.1 christos /* Returns: Nil */
882 1.1 christos /* Parameters: sl(I) - pointer to synclist object to delete */
883 1.1 christos /* */
884 1.1 christos /* Deletes an object from the synclist nat table and free's its memory. */
885 1.1 christos /* ------------------------------------------------------------------------ */
886 1.1 christos void
887 1.2 christos ipf_sync_del_nat(void *arg, synclist_t *sl)
888 1.1 christos {
889 1.1 christos ipf_sync_softc_t *softs = arg;
890 1.1 christos
891 1.1 christos WRITE_ENTER(&softs->ipf_syncnat);
892 1.1 christos ipf_sync_del(softs, sl);
893 1.1 christos RWLOCK_EXIT(&softs->ipf_syncnat);
894 1.1 christos KFREE(sl);
895 1.1 christos }
896 1.1 christos
897 1.1 christos
898 1.1 christos /* ------------------------------------------------------------------------ */
899 1.1 christos /* Function: ipf_sync_nat */
900 1.1 christos /* Returns: int - 0 == success, else error value. */
901 1.1 christos /* Parameters: sp(I) - pointer to sync packet data header */
902 1.1 christos /* uio(I) - pointer to user data for further information */
903 1.1 christos /* */
904 1.1 christos /* Updates the NAT table according to information passed in the sync */
905 1.1 christos /* header. As required, more data is fetched from the uio structure but */
906 1.1 christos /* varies depending on the contents of the sync header. This function can */
907 1.1 christos /* create a new NAT entry or update one. Deletion is left to the NAT */
908 1.1 christos /* structures being timed out correctly. */
909 1.1 christos /* ------------------------------------------------------------------------ */
910 1.1 christos static int
911 1.2 christos ipf_sync_nat(ipf_main_softc_t *softc, synchdr_t *sp, void *data)
912 1.1 christos {
913 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
914 1.1 christos syncupdent_t su;
915 1.1 christos nat_t *n, *nat;
916 1.1 christos synclist_t *sl;
917 1.1 christos u_int hv = 0;
918 1.5 martin int err = 0;
919 1.1 christos
920 1.1 christos READ_ENTER(&softs->ipf_syncnat);
921 1.1 christos
922 1.1 christos switch (sp->sm_cmd)
923 1.1 christos {
924 1.1 christos case SMC_CREATE :
925 1.1 christos KMALLOC(n, nat_t *);
926 1.1 christos if (n == NULL) {
927 1.1 christos IPFERROR(110017);
928 1.1 christos err = ENOMEM;
929 1.1 christos break;
930 1.1 christos }
931 1.1 christos
932 1.1 christos KMALLOC(sl, synclist_t *);
933 1.1 christos if (sl == NULL) {
934 1.1 christos IPFERROR(110018);
935 1.1 christos err = ENOMEM;
936 1.1 christos KFREE(n);
937 1.1 christos break;
938 1.1 christos }
939 1.1 christos
940 1.1 christos nat = (nat_t *)data;
941 1.1 christos bzero((char *)n, offsetof(nat_t, nat_age));
942 1.1 christos bcopy((char *)&nat->nat_age, (char *)&n->nat_age,
943 1.1 christos sizeof(*n) - offsetof(nat_t, nat_age));
944 1.1 christos ipf_sync_natorder(0, n);
945 1.1 christos n->nat_sync = sl;
946 1.1 christos n->nat_rev = sl->sl_rev;
947 1.1 christos
948 1.1 christos sl->sl_idx = -1;
949 1.1 christos sl->sl_ipn = n;
950 1.1 christos sl->sl_num = ntohl(sp->sm_num);
951 1.1 christos
952 1.1 christos WRITE_ENTER(&softc->ipf_nat);
953 1.1 christos sl->sl_pnext = softs->syncnattab + hv;
954 1.1 christos sl->sl_next = softs->syncnattab[hv];
955 1.1 christos if (softs->syncnattab[hv] != NULL)
956 1.1 christos softs->syncnattab[hv]->sl_pnext = &sl->sl_next;
957 1.1 christos softs->syncnattab[hv] = sl;
958 1.1 christos (void) ipf_nat_insert(softc, softc->ipf_nat_soft, n);
959 1.1 christos RWLOCK_EXIT(&softc->ipf_nat);
960 1.1 christos break;
961 1.1 christos
962 1.1 christos case SMC_UPDATE :
963 1.1 christos bcopy(data, &su, sizeof(su));
964 1.1 christos
965 1.1 christos for (sl = softs->syncnattab[hv]; (sl != NULL);
966 1.1 christos sl = sl->sl_next)
967 1.1 christos if (sl->sl_hdr.sm_num == sp->sm_num)
968 1.1 christos break;
969 1.1 christos if (sl == NULL) {
970 1.1 christos IPFERROR(110019);
971 1.1 christos err = ENOENT;
972 1.1 christos break;
973 1.1 christos }
974 1.1 christos
975 1.1 christos READ_ENTER(&softc->ipf_nat);
976 1.1 christos
977 1.1 christos nat = sl->sl_ipn;
978 1.1 christos nat->nat_rev = sl->sl_rev;
979 1.1 christos
980 1.1 christos MUTEX_ENTER(&nat->nat_lock);
981 1.1 christos ipf_nat_setqueue(softc, softc->ipf_nat_soft, nat);
982 1.1 christos MUTEX_EXIT(&nat->nat_lock);
983 1.1 christos
984 1.1 christos RWLOCK_EXIT(&softc->ipf_nat);
985 1.1 christos
986 1.1 christos break;
987 1.1 christos
988 1.1 christos default :
989 1.1 christos IPFERROR(110020);
990 1.1 christos err = EINVAL;
991 1.1 christos break;
992 1.1 christos }
993 1.1 christos
994 1.1 christos RWLOCK_EXIT(&softs->ipf_syncnat);
995 1.5 martin return err;
996 1.1 christos }
997 1.1 christos
998 1.1 christos
999 1.1 christos /* ------------------------------------------------------------------------ */
1000 1.1 christos /* Function: ipf_sync_new */
1001 1.1 christos /* Returns: synclist_t* - NULL == failure, else pointer to new synclist */
1002 1.1 christos /* data structure. */
1003 1.1 christos /* Parameters: tab(I) - type of synclist_t to create */
1004 1.1 christos /* fin(I) - pointer to packet information */
1005 1.1 christos /* ptr(I) - pointer to owning object */
1006 1.1 christos /* */
1007 1.1 christos /* Creates a new sync table entry and notifies any sleepers that it's there */
1008 1.1 christos /* waiting to be processed. */
1009 1.1 christos /* ------------------------------------------------------------------------ */
1010 1.1 christos synclist_t *
1011 1.2 christos ipf_sync_new(ipf_main_softc_t *softc, int tab, fr_info_t *fin, void *ptr)
1012 1.1 christos {
1013 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1014 1.1 christos synclist_t *sl, *ss;
1015 1.1 christos synclogent_t *sle;
1016 1.1 christos u_int hv, sz;
1017 1.1 christos
1018 1.1 christos if (softs->sl_idx == softs->ipf_sync_log_sz)
1019 1.1 christos return NULL;
1020 1.1 christos KMALLOC(sl, synclist_t *);
1021 1.1 christos if (sl == NULL)
1022 1.1 christos return NULL;
1023 1.1 christos
1024 1.1 christos MUTEX_ENTER(&softs->ipf_syncadd);
1025 1.1 christos /*
1026 1.1 christos * Get a unique number for this synclist_t. The number is only meant
1027 1.1 christos * to be unique for the lifetime of the structure and may be reused
1028 1.1 christos * later.
1029 1.1 christos */
1030 1.1 christos softs->ipf_sync_num++;
1031 1.1 christos if (softs->ipf_sync_num == 0) {
1032 1.1 christos softs->ipf_sync_num = 1;
1033 1.1 christos softs->ipf_sync_wrap++;
1034 1.1 christos }
1035 1.1 christos
1036 1.1 christos /*
1037 1.1 christos * Use the synch number of the object as the hash key. Should end up
1038 1.1 christos * with relatively even distribution over time.
1039 1.1 christos * XXX - an attacker could lunch an DoS attack, of sorts, if they are
1040 1.1 christos * the only one causing new table entries by only keeping open every
1041 1.1 christos * nth connection they make, where n is a value in the interval
1042 1.1 christos * [0, SYNC_STATETABSZ-1].
1043 1.1 christos */
1044 1.1 christos switch (tab)
1045 1.1 christos {
1046 1.1 christos case SMC_STATE :
1047 1.1 christos hv = softs->ipf_sync_num & (softs->ipf_sync_state_tab_sz - 1);
1048 1.1 christos while (softs->ipf_sync_wrap != 0) {
1049 1.1 christos for (ss = softs->syncstatetab[hv]; ss; ss = ss->sl_next)
1050 1.1 christos if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
1051 1.1 christos break;
1052 1.1 christos if (ss == NULL)
1053 1.1 christos break;
1054 1.1 christos softs->ipf_sync_num++;
1055 1.1 christos hv = softs->ipf_sync_num &
1056 1.1 christos (softs->ipf_sync_state_tab_sz - 1);
1057 1.1 christos }
1058 1.1 christos sl->sl_pnext = softs->syncstatetab + hv;
1059 1.1 christos sl->sl_next = softs->syncstatetab[hv];
1060 1.1 christos softs->syncstatetab[hv] = sl;
1061 1.1 christos break;
1062 1.1 christos
1063 1.1 christos case SMC_NAT :
1064 1.1 christos hv = softs->ipf_sync_num & (softs->ipf_sync_nat_tab_sz - 1);
1065 1.1 christos while (softs->ipf_sync_wrap != 0) {
1066 1.1 christos for (ss = softs->syncnattab[hv]; ss; ss = ss->sl_next)
1067 1.1 christos if (ss->sl_hdr.sm_num == softs->ipf_sync_num)
1068 1.1 christos break;
1069 1.1 christos if (ss == NULL)
1070 1.1 christos break;
1071 1.1 christos softs->ipf_sync_num++;
1072 1.1 christos hv = softs->ipf_sync_num &
1073 1.1 christos (softs->ipf_sync_nat_tab_sz - 1);
1074 1.1 christos }
1075 1.1 christos sl->sl_pnext = softs->syncnattab + hv;
1076 1.1 christos sl->sl_next = softs->syncnattab[hv];
1077 1.1 christos softs->syncnattab[hv] = sl;
1078 1.1 christos break;
1079 1.1 christos
1080 1.1 christos default :
1081 1.1 christos break;
1082 1.1 christos }
1083 1.1 christos
1084 1.1 christos sl->sl_num = softs->ipf_sync_num;
1085 1.1 christos MUTEX_EXIT(&softs->ipf_syncadd);
1086 1.1 christos
1087 1.1 christos sl->sl_magic = htonl(SYNHDRMAGIC);
1088 1.1 christos sl->sl_v = fin->fin_v;
1089 1.1 christos sl->sl_p = fin->fin_p;
1090 1.1 christos sl->sl_cmd = SMC_CREATE;
1091 1.1 christos sl->sl_idx = -1;
1092 1.1 christos sl->sl_table = tab;
1093 1.1 christos sl->sl_rev = fin->fin_rev;
1094 1.1 christos if (tab == SMC_STATE) {
1095 1.1 christos sl->sl_ips = ptr;
1096 1.1 christos sz = sizeof(*sl->sl_ips);
1097 1.1 christos } else if (tab == SMC_NAT) {
1098 1.1 christos sl->sl_ipn = ptr;
1099 1.1 christos sz = sizeof(*sl->sl_ipn);
1100 1.1 christos } else {
1101 1.1 christos ptr = NULL;
1102 1.1 christos sz = 0;
1103 1.1 christos }
1104 1.1 christos sl->sl_len = sz;
1105 1.1 christos
1106 1.1 christos /*
1107 1.1 christos * Create the log entry to be read by a user daemon. When it has been
1108 1.1 christos * finished and put on the queue, send a signal to wakeup any waiters.
1109 1.1 christos */
1110 1.1 christos MUTEX_ENTER(&softs->ipf_syncadd);
1111 1.1 christos sle = softs->synclog + softs->sl_idx++;
1112 1.1 christos bcopy((char *)&sl->sl_hdr, (char *)&sle->sle_hdr,
1113 1.1 christos sizeof(sle->sle_hdr));
1114 1.1 christos sle->sle_hdr.sm_num = htonl(sle->sle_hdr.sm_num);
1115 1.1 christos sle->sle_hdr.sm_len = htonl(sle->sle_hdr.sm_len);
1116 1.1 christos if (ptr != NULL) {
1117 1.1 christos bcopy((char *)ptr, (char *)&sle->sle_un, sz);
1118 1.1 christos if (tab == SMC_STATE) {
1119 1.1 christos ipf_sync_storder(1, &sle->sle_un.sleu_ips);
1120 1.1 christos } else if (tab == SMC_NAT) {
1121 1.1 christos ipf_sync_natorder(1, &sle->sle_un.sleu_ipn);
1122 1.1 christos }
1123 1.1 christos }
1124 1.1 christos MUTEX_EXIT(&softs->ipf_syncadd);
1125 1.1 christos
1126 1.1 christos ipf_sync_wakeup(softc);
1127 1.1 christos return sl;
1128 1.1 christos }
1129 1.1 christos
1130 1.1 christos
1131 1.1 christos /* ------------------------------------------------------------------------ */
1132 1.1 christos /* Function: ipf_sync_update */
1133 1.1 christos /* Returns: Nil */
1134 1.1 christos /* Parameters: tab(I) - type of synclist_t to create */
1135 1.1 christos /* fin(I) - pointer to packet information */
1136 1.1 christos /* sl(I) - pointer to synchronisation object */
1137 1.1 christos /* */
1138 1.1 christos /* For outbound packets, only, create an sync update record for the user */
1139 1.1 christos /* process to read. */
1140 1.1 christos /* ------------------------------------------------------------------------ */
1141 1.1 christos void
1142 1.2 christos ipf_sync_update(ipf_main_softc_t *softc, int tab, fr_info_t *fin,
1143 1.2 christos synclist_t *sl)
1144 1.1 christos {
1145 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1146 1.1 christos synctcp_update_t *st;
1147 1.1 christos syncupdent_t *slu;
1148 1.1 christos ipstate_t *ips;
1149 1.1 christos nat_t *nat;
1150 1.1 christos ipfrwlock_t *lock;
1151 1.1 christos
1152 1.1 christos if (fin->fin_out == 0 || sl == NULL)
1153 1.1 christos return;
1154 1.1 christos
1155 1.1 christos if (tab == SMC_STATE) {
1156 1.1 christos lock = &softs->ipf_syncstate;
1157 1.1 christos } else {
1158 1.1 christos lock = &softs->ipf_syncnat;
1159 1.1 christos }
1160 1.1 christos
1161 1.1 christos READ_ENTER(lock);
1162 1.1 christos if (sl->sl_idx == -1) {
1163 1.1 christos MUTEX_ENTER(&softs->ipf_syncadd);
1164 1.1 christos slu = softs->syncupd + softs->su_idx;
1165 1.1 christos sl->sl_idx = softs->su_idx++;
1166 1.1 christos MUTEX_EXIT(&softs->ipf_syncadd);
1167 1.1 christos
1168 1.1 christos bcopy((char *)&sl->sl_hdr, (char *)&slu->sup_hdr,
1169 1.1 christos sizeof(slu->sup_hdr));
1170 1.1 christos slu->sup_hdr.sm_magic = htonl(SYNHDRMAGIC);
1171 1.1 christos slu->sup_hdr.sm_sl = sl;
1172 1.1 christos slu->sup_hdr.sm_cmd = SMC_UPDATE;
1173 1.1 christos slu->sup_hdr.sm_table = tab;
1174 1.1 christos slu->sup_hdr.sm_num = htonl(sl->sl_num);
1175 1.1 christos slu->sup_hdr.sm_len = htonl(sizeof(struct synctcp_update));
1176 1.1 christos slu->sup_hdr.sm_rev = fin->fin_rev;
1177 1.1 christos # if 0
1178 1.1 christos if (fin->fin_p == IPPROTO_TCP) {
1179 1.1 christos st->stu_len[0] = 0;
1180 1.1 christos st->stu_len[1] = 0;
1181 1.1 christos }
1182 1.1 christos # endif
1183 1.1 christos } else
1184 1.1 christos slu = softs->syncupd + sl->sl_idx;
1185 1.1 christos
1186 1.1 christos /*
1187 1.1 christos * Only TCP has complex timeouts, others just use default timeouts.
1188 1.1 christos * For TCP, we only need to track the connection state and window.
1189 1.1 christos */
1190 1.1 christos if (fin->fin_p == IPPROTO_TCP) {
1191 1.1 christos st = &slu->sup_tcp;
1192 1.1 christos if (tab == SMC_STATE) {
1193 1.1 christos ips = sl->sl_ips;
1194 1.1 christos st->stu_age = htonl(ips->is_die);
1195 1.1 christos st->stu_data[0].td_end = ips->is_send;
1196 1.1 christos st->stu_data[0].td_maxend = ips->is_maxsend;
1197 1.1 christos st->stu_data[0].td_maxwin = ips->is_maxswin;
1198 1.1 christos st->stu_state[0] = ips->is_state[0];
1199 1.1 christos st->stu_data[1].td_end = ips->is_dend;
1200 1.1 christos st->stu_data[1].td_maxend = ips->is_maxdend;
1201 1.1 christos st->stu_data[1].td_maxwin = ips->is_maxdwin;
1202 1.1 christos st->stu_state[1] = ips->is_state[1];
1203 1.1 christos } else if (tab == SMC_NAT) {
1204 1.1 christos nat = sl->sl_ipn;
1205 1.1 christos st->stu_age = htonl(nat->nat_age);
1206 1.1 christos }
1207 1.1 christos }
1208 1.1 christos RWLOCK_EXIT(lock);
1209 1.1 christos
1210 1.1 christos ipf_sync_wakeup(softc);
1211 1.1 christos }
1212 1.1 christos
1213 1.1 christos
1214 1.1 christos /* ------------------------------------------------------------------------ */
1215 1.1 christos /* Function: ipf_sync_flush_table */
1216 1.1 christos /* Returns: int - number of entries freed by flushing table */
1217 1.1 christos /* Parameters: tabsize(I) - size of the array pointed to by table */
1218 1.1 christos /* table(I) - pointer to sync table to empty */
1219 1.1 christos /* */
1220 1.1 christos /* Walk through a table of sync entries and free each one. It is assumed */
1221 1.1 christos /* that some lock is held so that nobody else tries to access the table */
1222 1.1 christos /* during this cleanup. */
1223 1.1 christos /* ------------------------------------------------------------------------ */
1224 1.1 christos static int
1225 1.2 christos ipf_sync_flush_table(ipf_sync_softc_t *softs, int tabsize, synclist_t **table)
1226 1.1 christos {
1227 1.1 christos synclist_t *sl;
1228 1.1 christos int i, items;
1229 1.1 christos
1230 1.1 christos items = 0;
1231 1.1 christos
1232 1.1 christos for (i = 0; i < tabsize; i++) {
1233 1.1 christos while ((sl = table[i]) != NULL) {
1234 1.1 christos switch (sl->sl_table) {
1235 1.1 christos case SMC_STATE :
1236 1.1 christos if (sl->sl_ips != NULL)
1237 1.1 christos sl->sl_ips->is_sync = NULL;
1238 1.1 christos break;
1239 1.1 christos case SMC_NAT :
1240 1.1 christos if (sl->sl_ipn != NULL)
1241 1.1 christos sl->sl_ipn->nat_sync = NULL;
1242 1.1 christos break;
1243 1.1 christos }
1244 1.1 christos if (sl->sl_next != NULL)
1245 1.1 christos sl->sl_next->sl_pnext = sl->sl_pnext;
1246 1.1 christos table[i] = sl->sl_next;
1247 1.1 christos if (sl->sl_idx != -1)
1248 1.1 christos softs->syncupd[sl->sl_idx].sup_hdr.sm_sl = NULL;
1249 1.1 christos KFREE(sl);
1250 1.1 christos items++;
1251 1.1 christos }
1252 1.1 christos }
1253 1.1 christos
1254 1.1 christos return items;
1255 1.1 christos }
1256 1.1 christos
1257 1.1 christos
1258 1.1 christos /* ------------------------------------------------------------------------ */
1259 1.1 christos /* Function: ipf_sync_ioctl */
1260 1.1 christos /* Returns: int - 0 == success, != 0 == failure */
1261 1.1 christos /* Parameters: data(I) - pointer to ioctl data */
1262 1.1 christos /* cmd(I) - ioctl command integer */
1263 1.1 christos /* mode(I) - file mode bits used with open */
1264 1.1 christos /* */
1265 1.1 christos /* This function currently does not handle any ioctls and so just returns */
1266 1.1 christos /* EINVAL on all occasions. */
1267 1.1 christos /* ------------------------------------------------------------------------ */
1268 1.1 christos int
1269 1.2 christos ipf_sync_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode,
1270 1.2 christos int uid, void *ctx)
1271 1.1 christos {
1272 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1273 1.1 christos int error, i;
1274 1.1 christos SPL_INT(s);
1275 1.1 christos
1276 1.1 christos switch (cmd)
1277 1.1 christos {
1278 1.1 christos case SIOCIPFFL:
1279 1.1 christos error = BCOPYIN(data, &i, sizeof(i));
1280 1.1 christos if (error != 0) {
1281 1.1 christos IPFERROR(110023);
1282 1.1 christos error = EFAULT;
1283 1.1 christos break;
1284 1.1 christos }
1285 1.1 christos
1286 1.1 christos switch (i)
1287 1.1 christos {
1288 1.1 christos case SMC_RLOG :
1289 1.1 christos SPL_NET(s);
1290 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
1291 1.1 christos i = (softs->sl_tail - softs->sl_idx) +
1292 1.1 christos (softs->su_tail - softs->su_idx);
1293 1.1 christos softs->sl_idx = 0;
1294 1.1 christos softs->su_idx = 0;
1295 1.1 christos softs->sl_tail = 0;
1296 1.1 christos softs->su_tail = 0;
1297 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
1298 1.1 christos SPL_X(s);
1299 1.1 christos break;
1300 1.1 christos
1301 1.1 christos case SMC_NAT :
1302 1.1 christos SPL_NET(s);
1303 1.1 christos WRITE_ENTER(&softs->ipf_syncnat);
1304 1.1 christos i = ipf_sync_flush_table(softs, SYNC_NATTABSZ,
1305 1.1 christos softs->syncnattab);
1306 1.1 christos RWLOCK_EXIT(&softs->ipf_syncnat);
1307 1.1 christos SPL_X(s);
1308 1.1 christos break;
1309 1.1 christos
1310 1.1 christos case SMC_STATE :
1311 1.1 christos SPL_NET(s);
1312 1.1 christos WRITE_ENTER(&softs->ipf_syncstate);
1313 1.1 christos i = ipf_sync_flush_table(softs, SYNC_STATETABSZ,
1314 1.1 christos softs->syncstatetab);
1315 1.1 christos RWLOCK_EXIT(&softs->ipf_syncstate);
1316 1.1 christos SPL_X(s);
1317 1.1 christos break;
1318 1.1 christos }
1319 1.1 christos
1320 1.1 christos error = BCOPYOUT(&i, data, sizeof(i));
1321 1.1 christos if (error != 0) {
1322 1.1 christos IPFERROR(110022);
1323 1.1 christos error = EFAULT;
1324 1.1 christos }
1325 1.1 christos break;
1326 1.1 christos
1327 1.1 christos default :
1328 1.1 christos IPFERROR(110021);
1329 1.1 christos error = EINVAL;
1330 1.1 christos break;
1331 1.1 christos }
1332 1.1 christos
1333 1.1 christos return error;
1334 1.1 christos }
1335 1.1 christos
1336 1.1 christos
1337 1.1 christos /* ------------------------------------------------------------------------ */
1338 1.1 christos /* Function: ipf_sync_canread */
1339 1.1 christos /* Returns: int - 0 == success, != 0 == failure */
1340 1.1 christos /* Parameters: Nil */
1341 1.1 christos /* */
1342 1.1 christos /* This function provides input to the poll handler about whether or not */
1343 1.1 christos /* there is data waiting to be read from the /dev/ipsync device. */
1344 1.1 christos /* ------------------------------------------------------------------------ */
1345 1.1 christos int
1346 1.2 christos ipf_sync_canread(void *arg)
1347 1.1 christos {
1348 1.1 christos ipf_sync_softc_t *softs = arg;
1349 1.1 christos return !((softs->sl_tail == softs->sl_idx) &&
1350 1.1 christos (softs->su_tail == softs->su_idx));
1351 1.1 christos }
1352 1.1 christos
1353 1.1 christos
1354 1.1 christos /* ------------------------------------------------------------------------ */
1355 1.1 christos /* Function: ipf_sync_canwrite */
1356 1.1 christos /* Returns: int - 1 == can always write */
1357 1.1 christos /* Parameters: Nil */
1358 1.1 christos /* */
1359 1.1 christos /* This function lets the poll handler know that it is always ready willing */
1360 1.1 christos /* to accept write events. */
1361 1.1 christos /* XXX Maybe this should return false if the sync table is full? */
1362 1.1 christos /* ------------------------------------------------------------------------ */
1363 1.1 christos int
1364 1.2 christos ipf_sync_canwrite(void *arg)
1365 1.1 christos {
1366 1.1 christos return 1;
1367 1.1 christos }
1368 1.1 christos
1369 1.1 christos
1370 1.1 christos /* ------------------------------------------------------------------------ */
1371 1.1 christos /* Function: ipf_sync_wakeup */
1372 1.1 christos /* Parameters: Nil */
1373 1.1 christos /* Returns: Nil */
1374 1.1 christos /* */
1375 1.1 christos /* This function implements the heuristics that decide how often to */
1376 1.1 christos /* generate a poll wakeup for programs that are waiting for information */
1377 1.1 christos /* about when they can do a read on /dev/ipsync. */
1378 1.1 christos /* */
1379 1.1 christos /* There are three different considerations here: */
1380 1.1 christos /* - do not keep a program waiting too long: ipf_sync_wake_interval is the */
1381 1.1 christos /* maximum number of ipf ticks to let pass by; */
1382 1.1 christos /* - do not let the queue of ouststanding things to generate notifies for */
1383 1.1 christos /* get too full (ipf_sync_queue_high_wm is the high water mark); */
1384 1.1 christos /* - do not let too many events get collapsed in before deciding that the */
1385 1.1 christos /* other host(s) need an update (ipf_sync_event_high_wm is the high water */
1386 1.1 christos /* mark for this counter.) */
1387 1.1 christos /* ------------------------------------------------------------------------ */
1388 1.1 christos static void
1389 1.2 christos ipf_sync_wakeup(ipf_main_softc_t *softc)
1390 1.1 christos {
1391 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1392 1.1 christos
1393 1.1 christos softs->ipf_sync_events++;
1394 1.1 christos if ((softc->ipf_ticks >
1395 1.1 christos softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval) ||
1396 1.1 christos (softs->ipf_sync_events > softs->ipf_sync_event_high_wm) ||
1397 1.1 christos ((softs->sl_tail - softs->sl_idx) >
1398 1.1 christos softs->ipf_sync_queue_high_wm) ||
1399 1.1 christos ((softs->su_tail - softs->su_idx) >
1400 1.1 christos softs->ipf_sync_queue_high_wm)) {
1401 1.1 christos
1402 1.1 christos ipf_sync_poll_wakeup(softc);
1403 1.1 christos }
1404 1.1 christos }
1405 1.1 christos
1406 1.1 christos
1407 1.1 christos /* ------------------------------------------------------------------------ */
1408 1.1 christos /* Function: ipf_sync_poll_wakeup */
1409 1.1 christos /* Parameters: Nil */
1410 1.1 christos /* Returns: Nil */
1411 1.1 christos /* */
1412 1.1 christos /* Deliver a poll wakeup and reset counters for two of the three heuristics */
1413 1.1 christos /* ------------------------------------------------------------------------ */
1414 1.1 christos static void
1415 1.2 christos ipf_sync_poll_wakeup(ipf_main_softc_t *softc)
1416 1.1 christos {
1417 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1418 1.1 christos
1419 1.1 christos softs->ipf_sync_events = 0;
1420 1.1 christos softs->ipf_sync_lastwakeup = softc->ipf_ticks;
1421 1.1 christos
1422 1.1 christos # ifdef _KERNEL
1423 1.1 christos # if SOLARIS
1424 1.1 christos MUTEX_ENTER(&softs->ipsl_mutex);
1425 1.1 christos cv_signal(&softs->ipslwait);
1426 1.1 christos MUTEX_EXIT(&softs->ipsl_mutex);
1427 1.1 christos pollwakeup(&softc->ipf_poll_head[IPL_LOGSYNC], POLLIN|POLLRDNORM);
1428 1.1 christos # else
1429 1.1 christos WAKEUP(&softs->sl_tail, 0);
1430 1.1 christos POLLWAKEUP(IPL_LOGSYNC);
1431 1.1 christos # endif
1432 1.1 christos # endif
1433 1.1 christos }
1434 1.1 christos
1435 1.1 christos
1436 1.1 christos /* ------------------------------------------------------------------------ */
1437 1.1 christos /* Function: ipf_sync_expire */
1438 1.1 christos /* Parameters: Nil */
1439 1.1 christos /* Returns: Nil */
1440 1.1 christos /* */
1441 1.1 christos /* This is the function called even ipf_tick. It implements one of the */
1442 1.1 christos /* three heuristics above *IF* there are events waiting. */
1443 1.1 christos /* ------------------------------------------------------------------------ */
1444 1.1 christos void
1445 1.2 christos ipf_sync_expire(ipf_main_softc_t *softc)
1446 1.1 christos {
1447 1.1 christos ipf_sync_softc_t *softs = softc->ipf_sync_soft;
1448 1.1 christos
1449 1.1 christos if ((softs->ipf_sync_events > 0) &&
1450 1.1 christos (softc->ipf_ticks >
1451 1.1 christos softs->ipf_sync_lastwakeup + softs->ipf_sync_wake_interval)) {
1452 1.1 christos ipf_sync_poll_wakeup(softc);
1453 1.1 christos }
1454 1.1 christos }
1455