ip_auth.c revision 1.2.4.2 1 1.2.4.2 yamt /* $NetBSD: ip_auth.c,v 1.2.4.2 2012/04/17 00:08:15 yamt Exp $ */
2 1.2.4.2 yamt
3 1.2.4.2 yamt /*
4 1.2.4.2 yamt * Copyright (C) 2012 by Darren Reed.
5 1.2.4.2 yamt *
6 1.2.4.2 yamt * See the IPFILTER.LICENCE file for details on licencing.
7 1.2.4.2 yamt */
8 1.2.4.2 yamt #if defined(KERNEL) || defined(_KERNEL)
9 1.2.4.2 yamt # undef KERNEL
10 1.2.4.2 yamt # undef _KERNEL
11 1.2.4.2 yamt # define KERNEL 1
12 1.2.4.2 yamt # define _KERNEL 1
13 1.2.4.2 yamt #endif
14 1.2.4.2 yamt #include <sys/errno.h>
15 1.2.4.2 yamt #include <sys/types.h>
16 1.2.4.2 yamt #include <sys/param.h>
17 1.2.4.2 yamt #include <sys/time.h>
18 1.2.4.2 yamt #include <sys/file.h>
19 1.2.4.2 yamt #if !defined(_KERNEL)
20 1.2.4.2 yamt # include <stdio.h>
21 1.2.4.2 yamt # include <stdlib.h>
22 1.2.4.2 yamt # ifdef _STDC_C99
23 1.2.4.2 yamt # include <stdbool.h>
24 1.2.4.2 yamt # endif
25 1.2.4.2 yamt # include <string.h>
26 1.2.4.2 yamt # define _KERNEL
27 1.2.4.2 yamt # ifdef __OpenBSD__
28 1.2.4.2 yamt struct file;
29 1.2.4.2 yamt # endif
30 1.2.4.2 yamt # include <sys/uio.h>
31 1.2.4.2 yamt # undef _KERNEL
32 1.2.4.2 yamt #endif
33 1.2.4.2 yamt #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
34 1.2.4.2 yamt # include <sys/filio.h>
35 1.2.4.2 yamt # include <sys/fcntl.h>
36 1.2.4.2 yamt #else
37 1.2.4.2 yamt # include <sys/ioctl.h>
38 1.2.4.2 yamt #endif
39 1.2.4.2 yamt #if !defined(linux)
40 1.2.4.2 yamt # include <sys/protosw.h>
41 1.2.4.2 yamt #endif
42 1.2.4.2 yamt #include <sys/socket.h>
43 1.2.4.2 yamt #if defined(_KERNEL)
44 1.2.4.2 yamt # include <sys/systm.h>
45 1.2.4.2 yamt # if !defined(__SVR4) && !defined(__svr4__) && !defined(linux)
46 1.2.4.2 yamt # include <sys/mbuf.h>
47 1.2.4.2 yamt # endif
48 1.2.4.2 yamt #endif
49 1.2.4.2 yamt #if defined(__SVR4) || defined(__svr4__)
50 1.2.4.2 yamt # include <sys/filio.h>
51 1.2.4.2 yamt # include <sys/byteorder.h>
52 1.2.4.2 yamt # ifdef _KERNEL
53 1.2.4.2 yamt # include <sys/dditypes.h>
54 1.2.4.2 yamt # endif
55 1.2.4.2 yamt # include <sys/stream.h>
56 1.2.4.2 yamt # include <sys/kmem.h>
57 1.2.4.2 yamt #endif
58 1.2.4.2 yamt #if (defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)) || \
59 1.2.4.2 yamt (defined(__FreeBSD_version) &&(__FreeBSD_version >= 400000))
60 1.2.4.2 yamt # include <sys/queue.h>
61 1.2.4.2 yamt #endif
62 1.2.4.2 yamt #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
63 1.2.4.2 yamt # include <machine/cpu.h>
64 1.2.4.2 yamt #endif
65 1.2.4.2 yamt #if defined(_KERNEL) && defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
66 1.2.4.2 yamt # include <sys/proc.h>
67 1.2.4.2 yamt #endif
68 1.2.4.2 yamt #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 400000) && \
69 1.2.4.2 yamt !defined(_KERNEL)
70 1.2.4.2 yamt # include <stdbool.h>
71 1.2.4.2 yamt #endif
72 1.2.4.2 yamt #include <net/if.h>
73 1.2.4.2 yamt #include <net/route.h>
74 1.2.4.2 yamt #ifdef sun
75 1.2.4.2 yamt # include <net/af.h>
76 1.2.4.2 yamt #endif
77 1.2.4.2 yamt #include <netinet/in.h>
78 1.2.4.2 yamt #include <netinet/in_systm.h>
79 1.2.4.2 yamt #include <netinet/ip.h>
80 1.2.4.2 yamt #if !defined(_KERNEL) && !defined(__osf__) && !defined(__sgi)
81 1.2.4.2 yamt # define KERNEL
82 1.2.4.2 yamt # define _KERNEL
83 1.2.4.2 yamt # define NOT_KERNEL
84 1.2.4.2 yamt #endif
85 1.2.4.2 yamt #if !defined(linux)
86 1.2.4.2 yamt # include <netinet/ip_var.h>
87 1.2.4.2 yamt #endif
88 1.2.4.2 yamt #ifdef NOT_KERNEL
89 1.2.4.2 yamt # undef _KERNEL
90 1.2.4.2 yamt # undef KERNEL
91 1.2.4.2 yamt #endif
92 1.2.4.2 yamt #include <netinet/tcp.h>
93 1.2.4.2 yamt #if defined(IRIX) && (IRIX < 60516) /* IRIX < 6 */
94 1.2.4.2 yamt extern struct ifqueue ipintrq; /* ip packet input queue */
95 1.2.4.2 yamt #else
96 1.2.4.2 yamt # if !defined(__hpux) && !defined(linux)
97 1.2.4.2 yamt # if __FreeBSD_version >= 300000
98 1.2.4.2 yamt # include <net/if_var.h>
99 1.2.4.2 yamt # if __FreeBSD_version >= 500042
100 1.2.4.2 yamt # define IF_QFULL _IF_QFULL
101 1.2.4.2 yamt # define IF_DROP _IF_DROP
102 1.2.4.2 yamt # endif /* __FreeBSD_version >= 500042 */
103 1.2.4.2 yamt # endif
104 1.2.4.2 yamt # include <netinet/in_var.h>
105 1.2.4.2 yamt # include <netinet/tcp_fsm.h>
106 1.2.4.2 yamt # endif
107 1.2.4.2 yamt #endif
108 1.2.4.2 yamt #include <netinet/udp.h>
109 1.2.4.2 yamt #include <netinet/ip_icmp.h>
110 1.2.4.2 yamt #include "netinet/ip_compat.h"
111 1.2.4.2 yamt #include <netinet/tcpip.h>
112 1.2.4.2 yamt #include "netinet/ip_fil.h"
113 1.2.4.2 yamt #include "netinet/ip_auth.h"
114 1.2.4.2 yamt #if !defined(MENTAT) && !defined(linux)
115 1.2.4.2 yamt # include <net/netisr.h>
116 1.2.4.2 yamt # ifdef __FreeBSD__
117 1.2.4.2 yamt # include <machine/cpufunc.h>
118 1.2.4.2 yamt # endif
119 1.2.4.2 yamt #endif
120 1.2.4.2 yamt #if (__FreeBSD_version >= 300000)
121 1.2.4.2 yamt # include <sys/malloc.h>
122 1.2.4.2 yamt # if defined(_KERNEL) && !defined(IPFILTER_LKM)
123 1.2.4.2 yamt # include <sys/libkern.h>
124 1.2.4.2 yamt # include <sys/systm.h>
125 1.2.4.2 yamt # endif
126 1.2.4.2 yamt #endif
127 1.2.4.2 yamt /* END OF INCLUDES */
128 1.2.4.2 yamt
129 1.2.4.2 yamt #if !defined(lint)
130 1.2.4.2 yamt #if defined(__NetBSD__)
131 1.2.4.2 yamt #include <sys/cdefs.h>
132 1.2.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: ip_auth.c,v 1.2.4.2 2012/04/17 00:08:15 yamt Exp $");
133 1.2.4.2 yamt #else
134 1.2.4.2 yamt static const char rcsid[] = "@(#)Id: ip_auth.c,v 2.117.2.7 2012/01/29 05:30:35 darrenr Exp";
135 1.2.4.2 yamt #endif
136 1.2.4.2 yamt #endif
137 1.2.4.2 yamt
138 1.2.4.2 yamt
139 1.2.4.2 yamt
140 1.2.4.2 yamt typedef struct ipf_auth_softc_s {
141 1.2.4.2 yamt #if SOLARIS && defined(_KERNEL)
142 1.2.4.2 yamt kcondvar_t ipf_auth_wait;
143 1.2.4.2 yamt #endif /* SOLARIS */
144 1.2.4.2 yamt #if defined(linux) && defined(_KERNEL)
145 1.2.4.2 yamt wait_queue_head_t ipf_auth_next_linux;
146 1.2.4.2 yamt #endif
147 1.2.4.2 yamt ipfrwlock_t ipf_authlk;
148 1.2.4.2 yamt ipfmutex_t ipf_auth_mx;
149 1.2.4.2 yamt int ipf_auth_size;
150 1.2.4.2 yamt int ipf_auth_used;
151 1.2.4.2 yamt int ipf_auth_replies;
152 1.2.4.2 yamt int ipf_auth_defaultage;
153 1.2.4.2 yamt int ipf_auth_lock;
154 1.2.4.2 yamt ipf_authstat_t ipf_auth_stats;
155 1.2.4.2 yamt frauth_t *ipf_auth;
156 1.2.4.2 yamt mb_t **ipf_auth_pkts;
157 1.2.4.2 yamt int ipf_auth_start;
158 1.2.4.2 yamt int ipf_auth_end;
159 1.2.4.2 yamt int ipf_auth_next;
160 1.2.4.2 yamt frauthent_t *ipf_auth_entries;
161 1.2.4.2 yamt frentry_t *ipf_auth_ip;
162 1.2.4.2 yamt frentry_t *ipf_auth_rules;
163 1.2.4.2 yamt } ipf_auth_softc_t;
164 1.2.4.2 yamt
165 1.2.4.2 yamt
166 1.2.4.2 yamt static void ipf_auth_deref(frauthent_t **);
167 1.2.4.2 yamt static void ipf_auth_deref_unlocked(ipf_auth_softc_t *, frauthent_t **);
168 1.2.4.2 yamt static int ipf_auth_geniter(ipf_main_softc_t *, ipftoken_t *,
169 1.2.4.2 yamt ipfgeniter_t *, ipfobj_t *);
170 1.2.4.2 yamt static int ipf_auth_reply(ipf_main_softc_t *, ipf_auth_softc_t *, char *);
171 1.2.4.2 yamt static int ipf_auth_wait(ipf_main_softc_t *, ipf_auth_softc_t *, char *);
172 1.2.4.2 yamt static int ipf_auth_flush(void *);
173 1.2.4.2 yamt
174 1.2.4.2 yamt
175 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
176 1.2.4.2 yamt /* Function: ipf_auth_main_load */
177 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
178 1.2.4.2 yamt /* Parameters: None */
179 1.2.4.2 yamt /* */
180 1.2.4.2 yamt /* A null-op function that exists as a placeholder so that the flow in */
181 1.2.4.2 yamt /* other functions is obvious. */
182 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
183 1.2.4.2 yamt int
184 1.2.4.2 yamt ipf_auth_main_load(void)
185 1.2.4.2 yamt {
186 1.2.4.2 yamt return 0;
187 1.2.4.2 yamt }
188 1.2.4.2 yamt
189 1.2.4.2 yamt
190 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
191 1.2.4.2 yamt /* Function: ipf_auth_main_unload */
192 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
193 1.2.4.2 yamt /* Parameters: None */
194 1.2.4.2 yamt /* */
195 1.2.4.2 yamt /* A null-op function that exists as a placeholder so that the flow in */
196 1.2.4.2 yamt /* other functions is obvious. */
197 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
198 1.2.4.2 yamt int
199 1.2.4.2 yamt ipf_auth_main_unload(void)
200 1.2.4.2 yamt {
201 1.2.4.2 yamt return 0;
202 1.2.4.2 yamt }
203 1.2.4.2 yamt
204 1.2.4.2 yamt
205 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
206 1.2.4.2 yamt /* Function: ipf_auth_soft_create */
207 1.2.4.2 yamt /* Returns: int - NULL = failure, else success */
208 1.2.4.2 yamt /* Parameters: softc(I) - pointer to soft context data */
209 1.2.4.2 yamt /* */
210 1.2.4.2 yamt /* Create a structre to store all of the run-time data for packet auth in */
211 1.2.4.2 yamt /* and initialise some fields to their defaults. */
212 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
213 1.2.4.2 yamt void *
214 1.2.4.2 yamt ipf_auth_soft_create(ipf_main_softc_t *softc)
215 1.2.4.2 yamt {
216 1.2.4.2 yamt ipf_auth_softc_t *softa;
217 1.2.4.2 yamt
218 1.2.4.2 yamt KMALLOC(softa, ipf_auth_softc_t *);
219 1.2.4.2 yamt if (softa == NULL)
220 1.2.4.2 yamt return NULL;
221 1.2.4.2 yamt
222 1.2.4.2 yamt bzero((char *)softa, sizeof(*softa));
223 1.2.4.2 yamt
224 1.2.4.2 yamt softa->ipf_auth_size = FR_NUMAUTH;
225 1.2.4.2 yamt softa->ipf_auth_defaultage = 600;
226 1.2.4.2 yamt
227 1.2.4.2 yamt RWLOCK_INIT(&softa->ipf_authlk, "ipf IP User-Auth rwlock");
228 1.2.4.2 yamt MUTEX_INIT(&softa->ipf_auth_mx, "ipf auth log mutex");
229 1.2.4.2 yamt #if SOLARIS && defined(_KERNEL)
230 1.2.4.2 yamt cv_init(&softa->ipf_auth_wait, "ipf auth condvar", CV_DRIVER, NULL);
231 1.2.4.2 yamt #endif
232 1.2.4.2 yamt
233 1.2.4.2 yamt return softa;
234 1.2.4.2 yamt }
235 1.2.4.2 yamt
236 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
237 1.2.4.2 yamt /* Function: ipf_auth_soft_init */
238 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
239 1.2.4.2 yamt /* Parameters: softc(I) - pointer to soft context data */
240 1.2.4.2 yamt /* arg(I) - opaque pointer to auth context data */
241 1.2.4.2 yamt /* */
242 1.2.4.2 yamt /* Allocate memory and initialise data structures used in handling auth */
243 1.2.4.2 yamt /* rules. */
244 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
245 1.2.4.2 yamt int
246 1.2.4.2 yamt ipf_auth_soft_init(ipf_main_softc_t *softc, void *arg)
247 1.2.4.2 yamt {
248 1.2.4.2 yamt ipf_auth_softc_t *softa = arg;
249 1.2.4.2 yamt
250 1.2.4.2 yamt KMALLOCS(softa->ipf_auth, frauth_t *,
251 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth));
252 1.2.4.2 yamt if (softa->ipf_auth == NULL)
253 1.2.4.2 yamt return -1;
254 1.2.4.2 yamt bzero((char *)softa->ipf_auth,
255 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth));
256 1.2.4.2 yamt
257 1.2.4.2 yamt KMALLOCS(softa->ipf_auth_pkts, mb_t **,
258 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));
259 1.2.4.2 yamt if (softa->ipf_auth_pkts == NULL)
260 1.2.4.2 yamt return -2;
261 1.2.4.2 yamt bzero((char *)softa->ipf_auth_pkts,
262 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));
263 1.2.4.2 yamt
264 1.2.4.2 yamt #if defined(linux) && defined(_KERNEL)
265 1.2.4.2 yamt init_waitqueue_head(&softa->ipf_auth_next_linux);
266 1.2.4.2 yamt #endif
267 1.2.4.2 yamt
268 1.2.4.2 yamt return 0;
269 1.2.4.2 yamt }
270 1.2.4.2 yamt
271 1.2.4.2 yamt
272 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
273 1.2.4.2 yamt /* Function: ipf_auth_soft_fini */
274 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
275 1.2.4.2 yamt /* Parameters: softc(I) - pointer to soft context data */
276 1.2.4.2 yamt /* arg(I) - opaque pointer to auth context data */
277 1.2.4.2 yamt /* */
278 1.2.4.2 yamt /* Free all network buffer memory used to keep saved packets that have been */
279 1.2.4.2 yamt /* connectedd to the soft soft context structure *but* do not free that: it */
280 1.2.4.2 yamt /* is free'd by _destroy(). */
281 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
282 1.2.4.2 yamt int
283 1.2.4.2 yamt ipf_auth_soft_fini(ipf_main_softc_t *softc, void *arg)
284 1.2.4.2 yamt {
285 1.2.4.2 yamt ipf_auth_softc_t *softa = arg;
286 1.2.4.2 yamt frauthent_t *fae, **faep;
287 1.2.4.2 yamt frentry_t *fr, **frp;
288 1.2.4.2 yamt mb_t *m;
289 1.2.4.2 yamt int i;
290 1.2.4.2 yamt
291 1.2.4.2 yamt if (softa->ipf_auth != NULL) {
292 1.2.4.2 yamt KFREES(softa->ipf_auth,
293 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth));
294 1.2.4.2 yamt softa->ipf_auth = NULL;
295 1.2.4.2 yamt }
296 1.2.4.2 yamt
297 1.2.4.2 yamt if (softa->ipf_auth_pkts != NULL) {
298 1.2.4.2 yamt for (i = 0; i < softa->ipf_auth_size; i++) {
299 1.2.4.2 yamt m = softa->ipf_auth_pkts[i];
300 1.2.4.2 yamt if (m != NULL) {
301 1.2.4.2 yamt FREE_MB_T(m);
302 1.2.4.2 yamt softa->ipf_auth_pkts[i] = NULL;
303 1.2.4.2 yamt }
304 1.2.4.2 yamt }
305 1.2.4.2 yamt KFREES(softa->ipf_auth_pkts,
306 1.2.4.2 yamt softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));
307 1.2.4.2 yamt softa->ipf_auth_pkts = NULL;
308 1.2.4.2 yamt }
309 1.2.4.2 yamt
310 1.2.4.2 yamt faep = &softa->ipf_auth_entries;
311 1.2.4.2 yamt while ((fae = *faep) != NULL) {
312 1.2.4.2 yamt *faep = fae->fae_next;
313 1.2.4.2 yamt KFREE(fae);
314 1.2.4.2 yamt }
315 1.2.4.2 yamt softa->ipf_auth_ip = NULL;
316 1.2.4.2 yamt
317 1.2.4.2 yamt if (softa->ipf_auth_rules != NULL) {
318 1.2.4.2 yamt for (frp = &softa->ipf_auth_rules; ((fr = *frp) != NULL); ) {
319 1.2.4.2 yamt if (fr->fr_ref == 1) {
320 1.2.4.2 yamt *frp = fr->fr_next;
321 1.2.4.2 yamt MUTEX_DESTROY(&fr->fr_lock);
322 1.2.4.2 yamt KFREE(fr);
323 1.2.4.2 yamt } else
324 1.2.4.2 yamt frp = &fr->fr_next;
325 1.2.4.2 yamt }
326 1.2.4.2 yamt }
327 1.2.4.2 yamt
328 1.2.4.2 yamt return 0;
329 1.2.4.2 yamt }
330 1.2.4.2 yamt
331 1.2.4.2 yamt
332 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
333 1.2.4.2 yamt /* Function: ipf_auth_soft_destroy */
334 1.2.4.2 yamt /* Returns: void */
335 1.2.4.2 yamt /* Parameters: softc(I) - pointer to soft context data */
336 1.2.4.2 yamt /* arg(I) - opaque pointer to auth context data */
337 1.2.4.2 yamt /* */
338 1.2.4.2 yamt /* Undo what was done in _create() - i.e. free the soft context data. */
339 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
340 1.2.4.2 yamt void
341 1.2.4.2 yamt ipf_auth_soft_destroy(ipf_main_softc_t *softc, void *arg)
342 1.2.4.2 yamt {
343 1.2.4.2 yamt ipf_auth_softc_t *softa = arg;
344 1.2.4.2 yamt
345 1.2.4.2 yamt # if SOLARIS && defined(_KERNEL)
346 1.2.4.2 yamt cv_destroy(&softa->ipf_auth_wait);
347 1.2.4.2 yamt # endif
348 1.2.4.2 yamt MUTEX_DESTROY(&softa->ipf_auth_mx);
349 1.2.4.2 yamt RW_DESTROY(&softa->ipf_authlk);
350 1.2.4.2 yamt
351 1.2.4.2 yamt KFREE(softa);
352 1.2.4.2 yamt }
353 1.2.4.2 yamt
354 1.2.4.2 yamt
355 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
356 1.2.4.2 yamt /* Function: ipf_auth_setlock */
357 1.2.4.2 yamt /* Returns: void */
358 1.2.4.2 yamt /* Paramters: arg(I) - pointer to soft context data */
359 1.2.4.2 yamt /* tmp(I) - value to assign to auth lock */
360 1.2.4.2 yamt /* */
361 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
362 1.2.4.2 yamt void
363 1.2.4.2 yamt ipf_auth_setlock(void *arg, int tmp)
364 1.2.4.2 yamt {
365 1.2.4.2 yamt ipf_auth_softc_t *softa = arg;
366 1.2.4.2 yamt
367 1.2.4.2 yamt softa->ipf_auth_lock = tmp;
368 1.2.4.2 yamt }
369 1.2.4.2 yamt
370 1.2.4.2 yamt
371 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
372 1.2.4.2 yamt /* Function: ipf_auth_check */
373 1.2.4.2 yamt /* Returns: frentry_t* - pointer to ipf rule if match found, else NULL */
374 1.2.4.2 yamt /* Parameters: fin(I) - pointer to ipftoken structure */
375 1.2.4.2 yamt /* passp(I) - pointer to ipfgeniter structure */
376 1.2.4.2 yamt /* */
377 1.2.4.2 yamt /* Check if a packet has authorization. If the packet is found to match an */
378 1.2.4.2 yamt /* authorization result and that would result in a feedback loop (i.e. it */
379 1.2.4.2 yamt /* will end up returning FR_AUTH) then return FR_BLOCK instead. */
380 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
381 1.2.4.2 yamt frentry_t *
382 1.2.4.2 yamt ipf_auth_check(fr_info_t *fin, u_32_t *passp)
383 1.2.4.2 yamt {
384 1.2.4.2 yamt ipf_main_softc_t *softc = fin->fin_main_soft;
385 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
386 1.2.4.2 yamt frentry_t *fr;
387 1.2.4.2 yamt frauth_t *fra;
388 1.2.4.2 yamt u_32_t pass;
389 1.2.4.2 yamt u_short id;
390 1.2.4.2 yamt ip_t *ip;
391 1.2.4.2 yamt int i;
392 1.2.4.2 yamt
393 1.2.4.2 yamt if (softa->ipf_auth_lock || !softa->ipf_auth_used)
394 1.2.4.2 yamt return NULL;
395 1.2.4.2 yamt
396 1.2.4.2 yamt ip = fin->fin_ip;
397 1.2.4.2 yamt id = ip->ip_id;
398 1.2.4.2 yamt
399 1.2.4.2 yamt READ_ENTER(&softa->ipf_authlk);
400 1.2.4.2 yamt for (i = softa->ipf_auth_start; i != softa->ipf_auth_end; ) {
401 1.2.4.2 yamt /*
402 1.2.4.2 yamt * index becomes -2 only after an SIOCAUTHW. Check this in
403 1.2.4.2 yamt * case the same packet gets sent again and it hasn't yet been
404 1.2.4.2 yamt * auth'd.
405 1.2.4.2 yamt */
406 1.2.4.2 yamt fra = softa->ipf_auth + i;
407 1.2.4.2 yamt if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
408 1.2.4.2 yamt !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
409 1.2.4.2 yamt /*
410 1.2.4.2 yamt * Avoid feedback loop.
411 1.2.4.2 yamt */
412 1.2.4.2 yamt if (!(pass = fra->fra_pass) || (FR_ISAUTH(pass))) {
413 1.2.4.2 yamt pass = FR_BLOCK;
414 1.2.4.2 yamt fin->fin_reason = FRB_AUTHFEEDBACK;
415 1.2.4.2 yamt }
416 1.2.4.2 yamt /*
417 1.2.4.2 yamt * Create a dummy rule for the stateful checking to
418 1.2.4.2 yamt * use and return. Zero out any values we don't
419 1.2.4.2 yamt * trust from userland!
420 1.2.4.2 yamt */
421 1.2.4.2 yamt if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
422 1.2.4.2 yamt (fin->fin_flx & FI_FRAG))) {
423 1.2.4.2 yamt KMALLOC(fr, frentry_t *);
424 1.2.4.2 yamt if (fr) {
425 1.2.4.2 yamt bcopy((char *)fra->fra_info.fin_fr,
426 1.2.4.2 yamt (char *)fr, sizeof(*fr));
427 1.2.4.2 yamt fr->fr_grp = NULL;
428 1.2.4.2 yamt fr->fr_ifa = fin->fin_ifp;
429 1.2.4.2 yamt fr->fr_func = NULL;
430 1.2.4.2 yamt fr->fr_ref = 1;
431 1.2.4.2 yamt fr->fr_flags = pass;
432 1.2.4.2 yamt fr->fr_ifas[1] = NULL;
433 1.2.4.2 yamt fr->fr_ifas[2] = NULL;
434 1.2.4.2 yamt fr->fr_ifas[3] = NULL;
435 1.2.4.2 yamt MUTEX_INIT(&fr->fr_lock,
436 1.2.4.2 yamt "ipf auth rule");
437 1.2.4.2 yamt }
438 1.2.4.2 yamt } else
439 1.2.4.2 yamt fr = fra->fra_info.fin_fr;
440 1.2.4.2 yamt fin->fin_fr = fr;
441 1.2.4.2 yamt fin->fin_flx |= fra->fra_flx;
442 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
443 1.2.4.2 yamt
444 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
445 1.2.4.2 yamt /*
446 1.2.4.2 yamt * ipf_auth_rules is populated with the rules malloc'd
447 1.2.4.2 yamt * above and only those.
448 1.2.4.2 yamt */
449 1.2.4.2 yamt if ((fr != NULL) && (fr != fra->fra_info.fin_fr)) {
450 1.2.4.2 yamt fr->fr_next = softa->ipf_auth_rules;
451 1.2.4.2 yamt softa->ipf_auth_rules = fr;
452 1.2.4.2 yamt }
453 1.2.4.2 yamt softa->ipf_auth_stats.fas_hits++;
454 1.2.4.2 yamt fra->fra_index = -1;
455 1.2.4.2 yamt softa->ipf_auth_used--;
456 1.2.4.2 yamt softa->ipf_auth_replies--;
457 1.2.4.2 yamt if (i == softa->ipf_auth_start) {
458 1.2.4.2 yamt while (fra->fra_index == -1) {
459 1.2.4.2 yamt i++;
460 1.2.4.2 yamt fra++;
461 1.2.4.2 yamt if (i == softa->ipf_auth_size) {
462 1.2.4.2 yamt i = 0;
463 1.2.4.2 yamt fra = softa->ipf_auth;
464 1.2.4.2 yamt }
465 1.2.4.2 yamt softa->ipf_auth_start = i;
466 1.2.4.2 yamt if (i == softa->ipf_auth_end)
467 1.2.4.2 yamt break;
468 1.2.4.2 yamt }
469 1.2.4.2 yamt if (softa->ipf_auth_start ==
470 1.2.4.2 yamt softa->ipf_auth_end) {
471 1.2.4.2 yamt softa->ipf_auth_next = 0;
472 1.2.4.2 yamt softa->ipf_auth_start = 0;
473 1.2.4.2 yamt softa->ipf_auth_end = 0;
474 1.2.4.2 yamt }
475 1.2.4.2 yamt }
476 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
477 1.2.4.2 yamt if (passp != NULL)
478 1.2.4.2 yamt *passp = pass;
479 1.2.4.2 yamt softa->ipf_auth_stats.fas_hits++;
480 1.2.4.2 yamt return fr;
481 1.2.4.2 yamt }
482 1.2.4.2 yamt i++;
483 1.2.4.2 yamt if (i == softa->ipf_auth_size)
484 1.2.4.2 yamt i = 0;
485 1.2.4.2 yamt }
486 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
487 1.2.4.2 yamt softa->ipf_auth_stats.fas_miss++;
488 1.2.4.2 yamt return NULL;
489 1.2.4.2 yamt }
490 1.2.4.2 yamt
491 1.2.4.2 yamt
492 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
493 1.2.4.2 yamt /* Function: ipf_auth_new */
494 1.2.4.2 yamt /* Returns: int - 1 == success, 0 = did not put packet on auth queue */
495 1.2.4.2 yamt /* Parameters: m(I) - pointer to mb_t with packet in it */
496 1.2.4.2 yamt /* fin(I) - pointer to packet information */
497 1.2.4.2 yamt /* */
498 1.2.4.2 yamt /* Check if we have room in the auth array to hold details for another */
499 1.2.4.2 yamt /* packet. If we do, store it and wake up any user programs which are */
500 1.2.4.2 yamt /* waiting to hear about these events. */
501 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
502 1.2.4.2 yamt int
503 1.2.4.2 yamt ipf_auth_new(mb_t *m, fr_info_t *fin)
504 1.2.4.2 yamt {
505 1.2.4.2 yamt ipf_main_softc_t *softc = fin->fin_main_soft;
506 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
507 1.2.4.2 yamt #if defined(_KERNEL) && defined(MENTAT)
508 1.2.4.2 yamt qpktinfo_t *qpi = fin->fin_qpi;
509 1.2.4.2 yamt #endif
510 1.2.4.2 yamt frauth_t *fra;
511 1.2.4.2 yamt #if !defined(sparc) && !defined(m68k)
512 1.2.4.2 yamt ip_t *ip;
513 1.2.4.2 yamt #endif
514 1.2.4.2 yamt int i;
515 1.2.4.2 yamt
516 1.2.4.2 yamt if (softa->ipf_auth_lock)
517 1.2.4.2 yamt return 0;
518 1.2.4.2 yamt
519 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
520 1.2.4.2 yamt if (((softa->ipf_auth_end + 1) % softa->ipf_auth_size) ==
521 1.2.4.2 yamt softa->ipf_auth_start) {
522 1.2.4.2 yamt softa->ipf_auth_stats.fas_nospace++;
523 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
524 1.2.4.2 yamt return 0;
525 1.2.4.2 yamt }
526 1.2.4.2 yamt
527 1.2.4.2 yamt softa->ipf_auth_stats.fas_added++;
528 1.2.4.2 yamt softa->ipf_auth_used++;
529 1.2.4.2 yamt i = softa->ipf_auth_end++;
530 1.2.4.2 yamt if (softa->ipf_auth_end == softa->ipf_auth_size)
531 1.2.4.2 yamt softa->ipf_auth_end = 0;
532 1.2.4.2 yamt
533 1.2.4.2 yamt fra = softa->ipf_auth + i;
534 1.2.4.2 yamt fra->fra_index = i;
535 1.2.4.2 yamt if (fin->fin_fr != NULL)
536 1.2.4.2 yamt fra->fra_pass = fin->fin_fr->fr_flags;
537 1.2.4.2 yamt else
538 1.2.4.2 yamt fra->fra_pass = 0;
539 1.2.4.2 yamt fra->fra_age = softa->ipf_auth_defaultage;
540 1.2.4.2 yamt bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
541 1.2.4.2 yamt fra->fra_flx = fra->fra_info.fin_flx & (FI_STATE|FI_NATED);
542 1.2.4.2 yamt fra->fra_info.fin_flx &= ~(FI_STATE|FI_NATED);
543 1.2.4.2 yamt #if !defined(sparc) && !defined(m68k)
544 1.2.4.2 yamt /*
545 1.2.4.2 yamt * No need to copyback here as we want to undo the changes, not keep
546 1.2.4.2 yamt * them.
547 1.2.4.2 yamt */
548 1.2.4.2 yamt ip = fin->fin_ip;
549 1.2.4.2 yamt # if defined(MENTAT) && defined(_KERNEL)
550 1.2.4.2 yamt if ((ip == (ip_t *)m->b_rptr) && (fin->fin_v == 4))
551 1.2.4.2 yamt # endif
552 1.2.4.2 yamt {
553 1.2.4.2 yamt register u_short bo;
554 1.2.4.2 yamt
555 1.2.4.2 yamt bo = ip->ip_len;
556 1.2.4.2 yamt ip->ip_len = htons(bo);
557 1.2.4.2 yamt bo = ip->ip_off;
558 1.2.4.2 yamt ip->ip_off = htons(bo);
559 1.2.4.2 yamt }
560 1.2.4.2 yamt #endif
561 1.2.4.2 yamt #if SOLARIS && defined(_KERNEL)
562 1.2.4.2 yamt COPYIFNAME(fin->fin_v, fin->fin_ifp, fra->fra_info.fin_ifname);
563 1.2.4.2 yamt m->b_rptr -= qpi->qpi_off;
564 1.2.4.2 yamt fra->fra_q = qpi->qpi_q; /* The queue can disappear! */
565 1.2.4.2 yamt fra->fra_m = *fin->fin_mp;
566 1.2.4.2 yamt fra->fra_info.fin_mp = &fra->fra_m;
567 1.2.4.2 yamt softa->ipf_auth_pkts[i] = *(mblk_t **)fin->fin_mp;
568 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
569 1.2.4.2 yamt cv_signal(&softa->ipf_auth_wait);
570 1.2.4.2 yamt pollwakeup(&softc->ipf_poll_head[IPL_LOGAUTH], POLLIN|POLLRDNORM);
571 1.2.4.2 yamt #else
572 1.2.4.2 yamt softa->ipf_auth_pkts[i] = m;
573 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
574 1.2.4.2 yamt WAKEUP(&softa->ipf_auth_next, 0);
575 1.2.4.2 yamt #endif
576 1.2.4.2 yamt return 1;
577 1.2.4.2 yamt }
578 1.2.4.2 yamt
579 1.2.4.2 yamt
580 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
581 1.2.4.2 yamt /* Function: ipf_auth_ioctl */
582 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
583 1.2.4.2 yamt /* Parameters: data(IO) - pointer to ioctl data */
584 1.2.4.2 yamt /* cmd(I) - ioctl command */
585 1.2.4.2 yamt /* mode(I) - mode flags associated with open descriptor */
586 1.2.4.2 yamt /* uid(I) - uid associatd with application making the call */
587 1.2.4.2 yamt /* ctx(I) - pointer for context */
588 1.2.4.2 yamt /* */
589 1.2.4.2 yamt /* This function handles all of the ioctls recognised by the auth component */
590 1.2.4.2 yamt /* in IPFilter - ie ioctls called on an open fd for /dev/ipf_auth */
591 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
592 1.2.4.2 yamt int
593 1.2.4.2 yamt ipf_auth_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode,
594 1.2.4.2 yamt int uid, void *ctx)
595 1.2.4.2 yamt {
596 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
597 1.2.4.2 yamt int error = 0, i;
598 1.2.4.2 yamt SPL_INT(s);
599 1.2.4.2 yamt
600 1.2.4.2 yamt switch (cmd)
601 1.2.4.2 yamt {
602 1.2.4.2 yamt case SIOCGENITER :
603 1.2.4.2 yamt {
604 1.2.4.2 yamt ipftoken_t *token;
605 1.2.4.2 yamt ipfgeniter_t iter;
606 1.2.4.2 yamt ipfobj_t obj;
607 1.2.4.2 yamt
608 1.2.4.2 yamt error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
609 1.2.4.2 yamt if (error != 0)
610 1.2.4.2 yamt break;
611 1.2.4.2 yamt
612 1.2.4.2 yamt SPL_SCHED(s);
613 1.2.4.2 yamt token = ipf_token_find(softc, IPFGENITER_AUTH, uid, ctx);
614 1.2.4.2 yamt if (token != NULL)
615 1.2.4.2 yamt error = ipf_auth_geniter(softc, token, &iter, &obj);
616 1.2.4.2 yamt else {
617 1.2.4.2 yamt WRITE_ENTER(&softc->ipf_tokens);
618 1.2.4.2 yamt if (token->ipt_data == NULL)
619 1.2.4.2 yamt ipf_token_free(softc, token);
620 1.2.4.2 yamt else
621 1.2.4.2 yamt ipf_token_deref(softc, token);
622 1.2.4.2 yamt RWLOCK_EXIT(&softc->ipf_tokens);
623 1.2.4.2 yamt IPFERROR(10001);
624 1.2.4.2 yamt error = ESRCH;
625 1.2.4.2 yamt }
626 1.2.4.2 yamt SPL_X(s);
627 1.2.4.2 yamt
628 1.2.4.2 yamt break;
629 1.2.4.2 yamt }
630 1.2.4.2 yamt
631 1.2.4.2 yamt case SIOCADAFR :
632 1.2.4.2 yamt case SIOCRMAFR :
633 1.2.4.2 yamt if (!(mode & FWRITE)) {
634 1.2.4.2 yamt IPFERROR(10002);
635 1.2.4.2 yamt error = EPERM;
636 1.2.4.2 yamt } else
637 1.2.4.2 yamt error = frrequest(softc, IPL_LOGAUTH, cmd, data,
638 1.2.4.2 yamt softc->ipf_active, 1);
639 1.2.4.2 yamt break;
640 1.2.4.2 yamt
641 1.2.4.2 yamt case SIOCSTLCK :
642 1.2.4.2 yamt if (!(mode & FWRITE)) {
643 1.2.4.2 yamt IPFERROR(10003);
644 1.2.4.2 yamt error = EPERM;
645 1.2.4.2 yamt } else {
646 1.2.4.2 yamt error = ipf_lock(data, &softa->ipf_auth_lock);
647 1.2.4.2 yamt }
648 1.2.4.2 yamt break;
649 1.2.4.2 yamt
650 1.2.4.2 yamt case SIOCATHST:
651 1.2.4.2 yamt softa->ipf_auth_stats.fas_faelist = softa->ipf_auth_entries;
652 1.2.4.2 yamt error = ipf_outobj(softc, data, &softa->ipf_auth_stats,
653 1.2.4.2 yamt IPFOBJ_AUTHSTAT);
654 1.2.4.2 yamt break;
655 1.2.4.2 yamt
656 1.2.4.2 yamt case SIOCIPFFL:
657 1.2.4.2 yamt SPL_NET(s);
658 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
659 1.2.4.2 yamt i = ipf_auth_flush(softa);
660 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
661 1.2.4.2 yamt SPL_X(s);
662 1.2.4.2 yamt error = BCOPYOUT(&i, data, sizeof(i));
663 1.2.4.2 yamt if (error != 0) {
664 1.2.4.2 yamt IPFERROR(10004);
665 1.2.4.2 yamt error = EFAULT;
666 1.2.4.2 yamt }
667 1.2.4.2 yamt break;
668 1.2.4.2 yamt
669 1.2.4.2 yamt case SIOCAUTHW:
670 1.2.4.2 yamt error = ipf_auth_wait(softc, softa, data);
671 1.2.4.2 yamt break;
672 1.2.4.2 yamt
673 1.2.4.2 yamt case SIOCAUTHR:
674 1.2.4.2 yamt error = ipf_auth_reply(softc, softa, data);
675 1.2.4.2 yamt break;
676 1.2.4.2 yamt
677 1.2.4.2 yamt default :
678 1.2.4.2 yamt IPFERROR(10005);
679 1.2.4.2 yamt error = EINVAL;
680 1.2.4.2 yamt break;
681 1.2.4.2 yamt }
682 1.2.4.2 yamt return error;
683 1.2.4.2 yamt }
684 1.2.4.2 yamt
685 1.2.4.2 yamt
686 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
687 1.2.4.2 yamt /* Function: ipf_auth_expire */
688 1.2.4.2 yamt /* Returns: None */
689 1.2.4.2 yamt /* Parameters: None */
690 1.2.4.2 yamt /* */
691 1.2.4.2 yamt /* Slowly expire held auth records. Timeouts are set in expectation of */
692 1.2.4.2 yamt /* this being called twice per second. */
693 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
694 1.2.4.2 yamt void
695 1.2.4.2 yamt ipf_auth_expire(ipf_main_softc_t *softc)
696 1.2.4.2 yamt {
697 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
698 1.2.4.2 yamt frauthent_t *fae, **faep;
699 1.2.4.2 yamt frentry_t *fr, **frp;
700 1.2.4.2 yamt frauth_t *fra;
701 1.2.4.2 yamt mb_t *m;
702 1.2.4.2 yamt int i;
703 1.2.4.2 yamt SPL_INT(s);
704 1.2.4.2 yamt
705 1.2.4.2 yamt if (softa->ipf_auth_lock)
706 1.2.4.2 yamt return;
707 1.2.4.2 yamt SPL_NET(s);
708 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
709 1.2.4.2 yamt for (i = 0, fra = softa->ipf_auth; i < softa->ipf_auth_size;
710 1.2.4.2 yamt i++, fra++) {
711 1.2.4.2 yamt fra->fra_age--;
712 1.2.4.2 yamt if ((fra->fra_age == 0) &&
713 1.2.4.2 yamt (softa->ipf_auth[i].fra_index != -1)) {
714 1.2.4.2 yamt if ((m = softa->ipf_auth_pkts[i]) != NULL) {
715 1.2.4.2 yamt FREE_MB_T(m);
716 1.2.4.2 yamt softa->ipf_auth_pkts[i] = NULL;
717 1.2.4.2 yamt } else if (softa->ipf_auth[i].fra_index == -2) {
718 1.2.4.2 yamt softa->ipf_auth_replies--;
719 1.2.4.2 yamt }
720 1.2.4.2 yamt softa->ipf_auth[i].fra_index = -1;
721 1.2.4.2 yamt softa->ipf_auth_stats.fas_expire++;
722 1.2.4.2 yamt softa->ipf_auth_used--;
723 1.2.4.2 yamt }
724 1.2.4.2 yamt }
725 1.2.4.2 yamt
726 1.2.4.2 yamt /*
727 1.2.4.2 yamt * Expire pre-auth rules
728 1.2.4.2 yamt */
729 1.2.4.2 yamt for (faep = &softa->ipf_auth_entries; ((fae = *faep) != NULL); ) {
730 1.2.4.2 yamt fae->fae_age--;
731 1.2.4.2 yamt if (fae->fae_age == 0) {
732 1.2.4.2 yamt ipf_auth_deref(&fae);
733 1.2.4.2 yamt softa->ipf_auth_stats.fas_expire++;
734 1.2.4.2 yamt } else
735 1.2.4.2 yamt faep = &fae->fae_next;
736 1.2.4.2 yamt }
737 1.2.4.2 yamt if (softa->ipf_auth_entries != NULL)
738 1.2.4.2 yamt softa->ipf_auth_ip = &softa->ipf_auth_entries->fae_fr;
739 1.2.4.2 yamt else
740 1.2.4.2 yamt softa->ipf_auth_ip = NULL;
741 1.2.4.2 yamt
742 1.2.4.2 yamt for (frp = &softa->ipf_auth_rules; ((fr = *frp) != NULL); ) {
743 1.2.4.2 yamt if (fr->fr_ref == 1) {
744 1.2.4.2 yamt *frp = fr->fr_next;
745 1.2.4.2 yamt MUTEX_DESTROY(&fr->fr_lock);
746 1.2.4.2 yamt KFREE(fr);
747 1.2.4.2 yamt } else
748 1.2.4.2 yamt frp = &fr->fr_next;
749 1.2.4.2 yamt }
750 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
751 1.2.4.2 yamt SPL_X(s);
752 1.2.4.2 yamt }
753 1.2.4.2 yamt
754 1.2.4.2 yamt
755 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
756 1.2.4.2 yamt /* Function: ipf_auth_precmd */
757 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
758 1.2.4.2 yamt /* Parameters: cmd(I) - ioctl command for rule */
759 1.2.4.2 yamt /* fr(I) - pointer to ipf rule */
760 1.2.4.2 yamt /* fptr(I) - pointer to caller's 'fr' */
761 1.2.4.2 yamt /* */
762 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
763 1.2.4.2 yamt int
764 1.2.4.2 yamt ipf_auth_precmd(ipf_main_softc_t *softc, ioctlcmd_t cmd, frentry_t *fr,
765 1.2.4.2 yamt frentry_t **frptr)
766 1.2.4.2 yamt {
767 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
768 1.2.4.2 yamt frauthent_t *fae, **faep;
769 1.2.4.2 yamt int error = 0;
770 1.2.4.2 yamt SPL_INT(s);
771 1.2.4.2 yamt
772 1.2.4.2 yamt if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
773 1.2.4.2 yamt IPFERROR(10006);
774 1.2.4.2 yamt return EIO;
775 1.2.4.2 yamt }
776 1.2.4.2 yamt
777 1.2.4.2 yamt for (faep = &softa->ipf_auth_entries; ((fae = *faep) != NULL); ) {
778 1.2.4.2 yamt if (&fae->fae_fr == fr)
779 1.2.4.2 yamt break;
780 1.2.4.2 yamt else
781 1.2.4.2 yamt faep = &fae->fae_next;
782 1.2.4.2 yamt }
783 1.2.4.2 yamt
784 1.2.4.2 yamt if (cmd == (ioctlcmd_t)SIOCRMAFR) {
785 1.2.4.2 yamt if (fr == NULL || frptr == NULL) {
786 1.2.4.2 yamt IPFERROR(10007);
787 1.2.4.2 yamt error = EINVAL;
788 1.2.4.2 yamt
789 1.2.4.2 yamt } else if (fae == NULL) {
790 1.2.4.2 yamt IPFERROR(10008);
791 1.2.4.2 yamt error = ESRCH;
792 1.2.4.2 yamt
793 1.2.4.2 yamt } else {
794 1.2.4.2 yamt SPL_NET(s);
795 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
796 1.2.4.2 yamt *faep = fae->fae_next;
797 1.2.4.2 yamt if (softa->ipf_auth_ip == &fae->fae_fr)
798 1.2.4.2 yamt softa->ipf_auth_ip = softa->ipf_auth_entries ?
799 1.2.4.2 yamt &softa->ipf_auth_entries->fae_fr : NULL;
800 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
801 1.2.4.2 yamt SPL_X(s);
802 1.2.4.2 yamt
803 1.2.4.2 yamt KFREE(fae);
804 1.2.4.2 yamt }
805 1.2.4.2 yamt } else if (fr != NULL && frptr != NULL) {
806 1.2.4.2 yamt KMALLOC(fae, frauthent_t *);
807 1.2.4.2 yamt if (fae != NULL) {
808 1.2.4.2 yamt bcopy((char *)fr, (char *)&fae->fae_fr,
809 1.2.4.2 yamt sizeof(*fr));
810 1.2.4.2 yamt SPL_NET(s);
811 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
812 1.2.4.2 yamt fae->fae_age = softa->ipf_auth_defaultage;
813 1.2.4.2 yamt fae->fae_fr.fr_hits = 0;
814 1.2.4.2 yamt fae->fae_fr.fr_next = *frptr;
815 1.2.4.2 yamt fae->fae_ref = 1;
816 1.2.4.2 yamt *frptr = &fae->fae_fr;
817 1.2.4.2 yamt fae->fae_next = *faep;
818 1.2.4.2 yamt *faep = fae;
819 1.2.4.2 yamt softa->ipf_auth_ip = &softa->ipf_auth_entries->fae_fr;
820 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
821 1.2.4.2 yamt SPL_X(s);
822 1.2.4.2 yamt } else {
823 1.2.4.2 yamt IPFERROR(10009);
824 1.2.4.2 yamt error = ENOMEM;
825 1.2.4.2 yamt }
826 1.2.4.2 yamt } else {
827 1.2.4.2 yamt IPFERROR(10010);
828 1.2.4.2 yamt error = EINVAL;
829 1.2.4.2 yamt }
830 1.2.4.2 yamt return error;
831 1.2.4.2 yamt }
832 1.2.4.2 yamt
833 1.2.4.2 yamt
834 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
835 1.2.4.2 yamt /* Function: ipf_auth_flush */
836 1.2.4.2 yamt /* Returns: int - number of auth entries flushed */
837 1.2.4.2 yamt /* Parameters: None */
838 1.2.4.2 yamt /* Locks: WRITE(ipf_authlk) */
839 1.2.4.2 yamt /* */
840 1.2.4.2 yamt /* This function flushs the ipf_auth_pkts array of any packet data with */
841 1.2.4.2 yamt /* references still there. */
842 1.2.4.2 yamt /* It is expected that the caller has already acquired the correct locks or */
843 1.2.4.2 yamt /* set the priority level correctly for this to block out other code paths */
844 1.2.4.2 yamt /* into these data structures. */
845 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
846 1.2.4.2 yamt static int
847 1.2.4.2 yamt ipf_auth_flush(void *arg)
848 1.2.4.2 yamt {
849 1.2.4.2 yamt ipf_auth_softc_t *softa = arg;
850 1.2.4.2 yamt int i, num_flushed;
851 1.2.4.2 yamt mb_t *m;
852 1.2.4.2 yamt
853 1.2.4.2 yamt if (softa->ipf_auth_lock)
854 1.2.4.2 yamt return -1;
855 1.2.4.2 yamt
856 1.2.4.2 yamt num_flushed = 0;
857 1.2.4.2 yamt
858 1.2.4.2 yamt for (i = 0 ; i < softa->ipf_auth_size; i++) {
859 1.2.4.2 yamt if (softa->ipf_auth[i].fra_index != -1) {
860 1.2.4.2 yamt m = softa->ipf_auth_pkts[i];
861 1.2.4.2 yamt if (m != NULL) {
862 1.2.4.2 yamt FREE_MB_T(m);
863 1.2.4.2 yamt softa->ipf_auth_pkts[i] = NULL;
864 1.2.4.2 yamt }
865 1.2.4.2 yamt
866 1.2.4.2 yamt softa->ipf_auth[i].fra_index = -1;
867 1.2.4.2 yamt /* perhaps add & use a flush counter inst.*/
868 1.2.4.2 yamt softa->ipf_auth_stats.fas_expire++;
869 1.2.4.2 yamt num_flushed++;
870 1.2.4.2 yamt }
871 1.2.4.2 yamt }
872 1.2.4.2 yamt
873 1.2.4.2 yamt softa->ipf_auth_start = 0;
874 1.2.4.2 yamt softa->ipf_auth_end = 0;
875 1.2.4.2 yamt softa->ipf_auth_next = 0;
876 1.2.4.2 yamt softa->ipf_auth_used = 0;
877 1.2.4.2 yamt softa->ipf_auth_replies = 0;
878 1.2.4.2 yamt
879 1.2.4.2 yamt return num_flushed;
880 1.2.4.2 yamt }
881 1.2.4.2 yamt
882 1.2.4.2 yamt
883 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
884 1.2.4.2 yamt /* Function: ipf_auth_waiting */
885 1.2.4.2 yamt /* Returns: int - number of packets in the auth queue */
886 1.2.4.2 yamt /* Parameters: None */
887 1.2.4.2 yamt /* */
888 1.2.4.2 yamt /* Simple truth check to see if there are any packets waiting in the auth */
889 1.2.4.2 yamt /* queue. */
890 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
891 1.2.4.2 yamt int
892 1.2.4.2 yamt ipf_auth_waiting(ipf_main_softc_t *softc)
893 1.2.4.2 yamt {
894 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
895 1.2.4.2 yamt
896 1.2.4.2 yamt return (softa->ipf_auth_used != 0);
897 1.2.4.2 yamt }
898 1.2.4.2 yamt
899 1.2.4.2 yamt
900 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
901 1.2.4.2 yamt /* Function: ipf_auth_geniter */
902 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
903 1.2.4.2 yamt /* Parameters: token(I) - pointer to ipftoken structure */
904 1.2.4.2 yamt /* itp(I) - pointer to ipfgeniter structure */
905 1.2.4.2 yamt /* objp(I) - pointer to ipf object destription */
906 1.2.4.2 yamt /* */
907 1.2.4.2 yamt /* Iterate through the list of entries in the auth queue list. */
908 1.2.4.2 yamt /* objp is used here to get the location of where to do the copy out to. */
909 1.2.4.2 yamt /* Stomping over various fields with new information will not harm anything */
910 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
911 1.2.4.2 yamt static int
912 1.2.4.2 yamt ipf_auth_geniter(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp,
913 1.2.4.2 yamt ipfobj_t *objp)
914 1.2.4.2 yamt {
915 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
916 1.2.4.2 yamt frauthent_t *fae, *next, zero;
917 1.2.4.2 yamt int error;
918 1.2.4.2 yamt
919 1.2.4.2 yamt if (itp->igi_data == NULL) {
920 1.2.4.2 yamt IPFERROR(10011);
921 1.2.4.2 yamt return EFAULT;
922 1.2.4.2 yamt }
923 1.2.4.2 yamt
924 1.2.4.2 yamt if (itp->igi_type != IPFGENITER_AUTH) {
925 1.2.4.2 yamt IPFERROR(10012);
926 1.2.4.2 yamt return EINVAL;
927 1.2.4.2 yamt }
928 1.2.4.2 yamt
929 1.2.4.2 yamt objp->ipfo_type = IPFOBJ_FRAUTH;
930 1.2.4.2 yamt objp->ipfo_ptr = itp->igi_data;
931 1.2.4.2 yamt objp->ipfo_size = sizeof(frauth_t);
932 1.2.4.2 yamt
933 1.2.4.2 yamt READ_ENTER(&softa->ipf_authlk);
934 1.2.4.2 yamt
935 1.2.4.2 yamt fae = token->ipt_data;
936 1.2.4.2 yamt if (fae == NULL) {
937 1.2.4.2 yamt next = softa->ipf_auth_entries;
938 1.2.4.2 yamt } else {
939 1.2.4.2 yamt next = fae->fae_next;
940 1.2.4.2 yamt }
941 1.2.4.2 yamt
942 1.2.4.2 yamt /*
943 1.2.4.2 yamt * If we found an auth entry to use, bump its reference count
944 1.2.4.2 yamt * so that it can be used for is_next when we come back.
945 1.2.4.2 yamt */
946 1.2.4.2 yamt if (next != NULL) {
947 1.2.4.2 yamt ATOMIC_INC(next->fae_ref);
948 1.2.4.2 yamt token->ipt_data = next;
949 1.2.4.2 yamt } else {
950 1.2.4.2 yamt bzero(&zero, sizeof(zero));
951 1.2.4.2 yamt next = &zero;
952 1.2.4.2 yamt token->ipt_data = NULL;
953 1.2.4.2 yamt }
954 1.2.4.2 yamt
955 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
956 1.2.4.2 yamt
957 1.2.4.2 yamt error = ipf_outobjk(softc, objp, next);
958 1.2.4.2 yamt if (fae != NULL)
959 1.2.4.2 yamt ipf_auth_deref_unlocked(softa, &fae);
960 1.2.4.2 yamt
961 1.2.4.2 yamt if (next->fae_next == NULL)
962 1.2.4.2 yamt ipf_token_mark_complete(token);
963 1.2.4.2 yamt return error;
964 1.2.4.2 yamt }
965 1.2.4.2 yamt
966 1.2.4.2 yamt
967 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
968 1.2.4.2 yamt /* Function: ipf_auth_deref_unlocked */
969 1.2.4.2 yamt /* Returns: None */
970 1.2.4.2 yamt /* Parameters: faep(IO) - pointer to caller's frauthent_t pointer */
971 1.2.4.2 yamt /* */
972 1.2.4.2 yamt /* Wrapper for ipf_auth_deref for when a write lock on ipf_authlk is not */
973 1.2.4.2 yamt /* held. */
974 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
975 1.2.4.2 yamt static void
976 1.2.4.2 yamt ipf_auth_deref_unlocked(ipf_auth_softc_t *softa, frauthent_t **faep)
977 1.2.4.2 yamt {
978 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
979 1.2.4.2 yamt ipf_auth_deref(faep);
980 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
981 1.2.4.2 yamt }
982 1.2.4.2 yamt
983 1.2.4.2 yamt
984 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
985 1.2.4.2 yamt /* Function: ipf_auth_deref */
986 1.2.4.2 yamt /* Returns: None */
987 1.2.4.2 yamt /* Parameters: faep(IO) - pointer to caller's frauthent_t pointer */
988 1.2.4.2 yamt /* Locks: WRITE(ipf_authlk) */
989 1.2.4.2 yamt /* */
990 1.2.4.2 yamt /* This function unconditionally sets the pointer in the caller to NULL, */
991 1.2.4.2 yamt /* to make it clear that it should no longer use that pointer, and drops */
992 1.2.4.2 yamt /* the reference count on the structure by 1. If it reaches 0, free it up. */
993 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
994 1.2.4.2 yamt static void
995 1.2.4.2 yamt ipf_auth_deref(frauthent_t **faep)
996 1.2.4.2 yamt {
997 1.2.4.2 yamt frauthent_t *fae;
998 1.2.4.2 yamt
999 1.2.4.2 yamt fae = *faep;
1000 1.2.4.2 yamt *faep = NULL;
1001 1.2.4.2 yamt
1002 1.2.4.2 yamt fae->fae_ref--;
1003 1.2.4.2 yamt if (fae->fae_ref == 0) {
1004 1.2.4.2 yamt KFREE(fae);
1005 1.2.4.2 yamt }
1006 1.2.4.2 yamt }
1007 1.2.4.2 yamt
1008 1.2.4.2 yamt
1009 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
1010 1.2.4.2 yamt /* Function: ipf_auth_wait_pkt */
1011 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
1012 1.2.4.2 yamt /* Parameters: data(I) - pointer to data from ioctl call */
1013 1.2.4.2 yamt /* */
1014 1.2.4.2 yamt /* This function is called when an application is waiting for a packet to */
1015 1.2.4.2 yamt /* match an "auth" rule by issuing an SIOCAUTHW ioctl. If there is already */
1016 1.2.4.2 yamt /* a packet waiting on the queue then we will return that _one_ immediately.*/
1017 1.2.4.2 yamt /* If there are no packets present in the queue (ipf_auth_pkts) then we go */
1018 1.2.4.2 yamt /* to sleep. */
1019 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
1020 1.2.4.2 yamt static int
1021 1.2.4.2 yamt ipf_auth_wait(ipf_main_softc_t *softc, ipf_auth_softc_t *softa, char *data)
1022 1.2.4.2 yamt {
1023 1.2.4.2 yamt frauth_t auth, *au = &auth;
1024 1.2.4.2 yamt int error, len, i;
1025 1.2.4.2 yamt mb_t *m;
1026 1.2.4.2 yamt char *t;
1027 1.2.4.2 yamt SPL_INT(s);
1028 1.2.4.2 yamt
1029 1.2.4.2 yamt ipf_auth_ioctlloop:
1030 1.2.4.2 yamt error = ipf_inobj(softc, data, NULL, au, IPFOBJ_FRAUTH);
1031 1.2.4.2 yamt if (error != 0)
1032 1.2.4.2 yamt return error;
1033 1.2.4.2 yamt
1034 1.2.4.2 yamt /*
1035 1.2.4.2 yamt * XXX Locks are held below over calls to copyout...a better
1036 1.2.4.2 yamt * solution needs to be found so this isn't necessary. The situation
1037 1.2.4.2 yamt * we are trying to guard against here is an error in the copyout
1038 1.2.4.2 yamt * steps should not cause the packet to "disappear" from the queue.
1039 1.2.4.2 yamt */
1040 1.2.4.2 yamt SPL_NET(s);
1041 1.2.4.2 yamt READ_ENTER(&softa->ipf_authlk);
1042 1.2.4.2 yamt
1043 1.2.4.2 yamt /*
1044 1.2.4.2 yamt * If ipf_auth_next is not equal to ipf_auth_end it will be because
1045 1.2.4.2 yamt * there is a packet waiting to be delt with in the ipf_auth_pkts
1046 1.2.4.2 yamt * array. We copy as much of that out to user space as requested.
1047 1.2.4.2 yamt */
1048 1.2.4.2 yamt if (softa->ipf_auth_used > 0) {
1049 1.2.4.2 yamt while (softa->ipf_auth_pkts[softa->ipf_auth_next] == NULL) {
1050 1.2.4.2 yamt softa->ipf_auth_next++;
1051 1.2.4.2 yamt if (softa->ipf_auth_next == softa->ipf_auth_size)
1052 1.2.4.2 yamt softa->ipf_auth_next = 0;
1053 1.2.4.2 yamt }
1054 1.2.4.2 yamt
1055 1.2.4.2 yamt error = ipf_outobj(softc, data,
1056 1.2.4.2 yamt &softa->ipf_auth[softa->ipf_auth_next],
1057 1.2.4.2 yamt IPFOBJ_FRAUTH);
1058 1.2.4.2 yamt if (error != 0) {
1059 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1060 1.2.4.2 yamt SPL_X(s);
1061 1.2.4.2 yamt return error;
1062 1.2.4.2 yamt }
1063 1.2.4.2 yamt
1064 1.2.4.2 yamt if (auth.fra_len != 0 && auth.fra_buf != NULL) {
1065 1.2.4.2 yamt /*
1066 1.2.4.2 yamt * Copy packet contents out to user space if
1067 1.2.4.2 yamt * requested. Bail on an error.
1068 1.2.4.2 yamt */
1069 1.2.4.2 yamt m = softa->ipf_auth_pkts[softa->ipf_auth_next];
1070 1.2.4.2 yamt len = MSGDSIZE(m);
1071 1.2.4.2 yamt if (len > auth.fra_len)
1072 1.2.4.2 yamt len = auth.fra_len;
1073 1.2.4.2 yamt auth.fra_len = len;
1074 1.2.4.2 yamt
1075 1.2.4.2 yamt for (t = auth.fra_buf; m && (len > 0); ) {
1076 1.2.4.2 yamt i = MIN(M_LEN(m), len);
1077 1.2.4.2 yamt error = copyoutptr(softc, MTOD(m, char *),
1078 1.2.4.2 yamt &t, i);
1079 1.2.4.2 yamt len -= i;
1080 1.2.4.2 yamt t += i;
1081 1.2.4.2 yamt if (error != 0) {
1082 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1083 1.2.4.2 yamt SPL_X(s);
1084 1.2.4.2 yamt return error;
1085 1.2.4.2 yamt }
1086 1.2.4.2 yamt m = m->m_next;
1087 1.2.4.2 yamt }
1088 1.2.4.2 yamt }
1089 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1090 1.2.4.2 yamt
1091 1.2.4.2 yamt SPL_NET(s);
1092 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
1093 1.2.4.2 yamt softa->ipf_auth_next++;
1094 1.2.4.2 yamt if (softa->ipf_auth_next == softa->ipf_auth_size)
1095 1.2.4.2 yamt softa->ipf_auth_next = 0;
1096 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1097 1.2.4.2 yamt SPL_X(s);
1098 1.2.4.2 yamt
1099 1.2.4.2 yamt return 0;
1100 1.2.4.2 yamt }
1101 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1102 1.2.4.2 yamt SPL_X(s);
1103 1.2.4.2 yamt
1104 1.2.4.2 yamt MUTEX_ENTER(&softa->ipf_auth_mx);
1105 1.2.4.2 yamt #ifdef _KERNEL
1106 1.2.4.2 yamt # if SOLARIS
1107 1.2.4.2 yamt error = 0;
1108 1.2.4.2 yamt if (!cv_wait_sig(&softa->ipf_auth_wait, &softa->ipf_auth_mx.ipf_lk)) {
1109 1.2.4.2 yamt IPFERROR(10014);
1110 1.2.4.2 yamt error = EINTR;
1111 1.2.4.2 yamt }
1112 1.2.4.2 yamt # else /* SOLARIS */
1113 1.2.4.2 yamt # ifdef __hpux
1114 1.2.4.2 yamt {
1115 1.2.4.2 yamt lock_t *l;
1116 1.2.4.2 yamt
1117 1.2.4.2 yamt l = get_sleep_lock(&softa->ipf_auth_next);
1118 1.2.4.2 yamt error = sleep(&softa->ipf_auth_next, PZERO+1);
1119 1.2.4.2 yamt spinunlock(l);
1120 1.2.4.2 yamt }
1121 1.2.4.2 yamt # else
1122 1.2.4.2 yamt # ifdef __osf__
1123 1.2.4.2 yamt error = mpsleep(&softa->ipf_auth_next, PSUSP|PCATCH, "ipf_auth_next",
1124 1.2.4.2 yamt 0, &softa->ipf_auth_mx, MS_LOCK_SIMPLE);
1125 1.2.4.2 yamt # else
1126 1.2.4.2 yamt error = SLEEP(&softa->ipf_auth_next, "ipf_auth_next");
1127 1.2.4.2 yamt # endif /* __osf__ */
1128 1.2.4.2 yamt # endif /* __hpux */
1129 1.2.4.2 yamt # endif /* SOLARIS */
1130 1.2.4.2 yamt #endif
1131 1.2.4.2 yamt MUTEX_EXIT(&softa->ipf_auth_mx);
1132 1.2.4.2 yamt if (error == 0)
1133 1.2.4.2 yamt goto ipf_auth_ioctlloop;
1134 1.2.4.2 yamt return error;
1135 1.2.4.2 yamt }
1136 1.2.4.2 yamt
1137 1.2.4.2 yamt
1138 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
1139 1.2.4.2 yamt /* Function: ipf_auth_reply */
1140 1.2.4.2 yamt /* Returns: int - 0 == success, else error */
1141 1.2.4.2 yamt /* Parameters: data(I) - pointer to data from ioctl call */
1142 1.2.4.2 yamt /* */
1143 1.2.4.2 yamt /* This function is called by an application when it wants to return a */
1144 1.2.4.2 yamt /* decision on a packet using the SIOCAUTHR ioctl. This is after it has */
1145 1.2.4.2 yamt /* received information using an SIOCAUTHW. The decision returned in the */
1146 1.2.4.2 yamt /* form of flags, the same as those used in each rule. */
1147 1.2.4.2 yamt /* ------------------------------------------------------------------------ */
1148 1.2.4.2 yamt static int
1149 1.2.4.2 yamt ipf_auth_reply(ipf_main_softc_t *softc, ipf_auth_softc_t *softa, char *data)
1150 1.2.4.2 yamt {
1151 1.2.4.2 yamt frauth_t auth, *au = &auth, *fra;
1152 1.2.4.2 yamt fr_info_t fin;
1153 1.2.4.2 yamt int error, i;
1154 1.2.4.2 yamt mb_t *m;
1155 1.2.4.2 yamt SPL_INT(s);
1156 1.2.4.2 yamt
1157 1.2.4.2 yamt error = ipf_inobj(softc, data, NULL, &auth, IPFOBJ_FRAUTH);
1158 1.2.4.2 yamt if (error != 0)
1159 1.2.4.2 yamt return error;
1160 1.2.4.2 yamt
1161 1.2.4.2 yamt SPL_NET(s);
1162 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
1163 1.2.4.2 yamt
1164 1.2.4.2 yamt i = au->fra_index;
1165 1.2.4.2 yamt fra = softa->ipf_auth + i;
1166 1.2.4.2 yamt error = 0;
1167 1.2.4.2 yamt
1168 1.2.4.2 yamt /*
1169 1.2.4.2 yamt * Check the validity of the information being returned with two simple
1170 1.2.4.2 yamt * checks. First, the auth index value should be within the size of
1171 1.2.4.2 yamt * the array and second the packet id being returned should also match.
1172 1.2.4.2 yamt */
1173 1.2.4.2 yamt if ((i < 0) || (i >= softa->ipf_auth_size)) {
1174 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1175 1.2.4.2 yamt SPL_X(s);
1176 1.2.4.2 yamt IPFERROR(10015);
1177 1.2.4.2 yamt return ESRCH;
1178 1.2.4.2 yamt }
1179 1.2.4.2 yamt if (fra->fra_info.fin_id != au->fra_info.fin_id) {
1180 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1181 1.2.4.2 yamt SPL_X(s);
1182 1.2.4.2 yamt IPFERROR(10019);
1183 1.2.4.2 yamt return ESRCH;
1184 1.2.4.2 yamt }
1185 1.2.4.2 yamt
1186 1.2.4.2 yamt m = softa->ipf_auth_pkts[i];
1187 1.2.4.2 yamt fra->fra_index = -2;
1188 1.2.4.2 yamt fra->fra_pass = au->fra_pass;
1189 1.2.4.2 yamt softa->ipf_auth_pkts[i] = NULL;
1190 1.2.4.2 yamt softa->ipf_auth_replies++;
1191 1.2.4.2 yamt bcopy(&fra->fra_info, &fin, sizeof(fin));
1192 1.2.4.2 yamt
1193 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1194 1.2.4.2 yamt
1195 1.2.4.2 yamt /*
1196 1.2.4.2 yamt * Re-insert the packet back into the packet stream flowing through
1197 1.2.4.2 yamt * the kernel in a manner that will mean IPFilter sees the packet
1198 1.2.4.2 yamt * again. This is not the same as is done with fastroute,
1199 1.2.4.2 yamt * deliberately, as we want to resume the normal packet processing
1200 1.2.4.2 yamt * path for it.
1201 1.2.4.2 yamt */
1202 1.2.4.2 yamt #ifdef _KERNEL
1203 1.2.4.2 yamt if ((m != NULL) && (au->fra_info.fin_out != 0)) {
1204 1.2.4.2 yamt error = ipf_inject(&fin, m);
1205 1.2.4.2 yamt if (error != 0) {
1206 1.2.4.2 yamt IPFERROR(10016);
1207 1.2.4.2 yamt error = ENOBUFS;
1208 1.2.4.2 yamt softa->ipf_auth_stats.fas_sendfail++;
1209 1.2.4.2 yamt } else {
1210 1.2.4.2 yamt softa->ipf_auth_stats.fas_sendok++;
1211 1.2.4.2 yamt }
1212 1.2.4.2 yamt } else if (m) {
1213 1.2.4.2 yamt error = ipf_inject(&fin, m);
1214 1.2.4.2 yamt if (error != 0) {
1215 1.2.4.2 yamt IPFERROR(10017);
1216 1.2.4.2 yamt error = ENOBUFS;
1217 1.2.4.2 yamt softa->ipf_auth_stats.fas_quefail++;
1218 1.2.4.2 yamt } else {
1219 1.2.4.2 yamt softa->ipf_auth_stats.fas_queok++;
1220 1.2.4.2 yamt }
1221 1.2.4.2 yamt } else {
1222 1.2.4.2 yamt IPFERROR(10018);
1223 1.2.4.2 yamt error = EINVAL;
1224 1.2.4.2 yamt }
1225 1.2.4.2 yamt
1226 1.2.4.2 yamt /*
1227 1.2.4.2 yamt * If we experience an error which will result in the packet
1228 1.2.4.2 yamt * not being processed, make sure we advance to the next one.
1229 1.2.4.2 yamt */
1230 1.2.4.2 yamt if (error == ENOBUFS) {
1231 1.2.4.2 yamt WRITE_ENTER(&softa->ipf_authlk);
1232 1.2.4.2 yamt softa->ipf_auth_used--;
1233 1.2.4.2 yamt fra->fra_index = -1;
1234 1.2.4.2 yamt fra->fra_pass = 0;
1235 1.2.4.2 yamt if (i == softa->ipf_auth_start) {
1236 1.2.4.2 yamt while (fra->fra_index == -1) {
1237 1.2.4.2 yamt i++;
1238 1.2.4.2 yamt if (i == softa->ipf_auth_size)
1239 1.2.4.2 yamt i = 0;
1240 1.2.4.2 yamt softa->ipf_auth_start = i;
1241 1.2.4.2 yamt if (i == softa->ipf_auth_end)
1242 1.2.4.2 yamt break;
1243 1.2.4.2 yamt }
1244 1.2.4.2 yamt if (softa->ipf_auth_start == softa->ipf_auth_end) {
1245 1.2.4.2 yamt softa->ipf_auth_next = 0;
1246 1.2.4.2 yamt softa->ipf_auth_start = 0;
1247 1.2.4.2 yamt softa->ipf_auth_end = 0;
1248 1.2.4.2 yamt }
1249 1.2.4.2 yamt }
1250 1.2.4.2 yamt RWLOCK_EXIT(&softa->ipf_authlk);
1251 1.2.4.2 yamt }
1252 1.2.4.2 yamt #endif /* _KERNEL */
1253 1.2.4.2 yamt SPL_X(s);
1254 1.2.4.2 yamt
1255 1.2.4.2 yamt return 0;
1256 1.2.4.2 yamt }
1257 1.2.4.2 yamt
1258 1.2.4.2 yamt
1259 1.2.4.2 yamt u_32_t
1260 1.2.4.2 yamt ipf_auth_pre_scanlist(ipf_main_softc_t *softc, fr_info_t *fin, u_32_t pass)
1261 1.2.4.2 yamt {
1262 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
1263 1.2.4.2 yamt
1264 1.2.4.2 yamt if (softa->ipf_auth_ip != NULL)
1265 1.2.4.2 yamt return ipf_scanlist(fin, softc->ipf_pass);
1266 1.2.4.2 yamt
1267 1.2.4.2 yamt return pass;
1268 1.2.4.2 yamt }
1269 1.2.4.2 yamt
1270 1.2.4.2 yamt
1271 1.2.4.2 yamt frentry_t **
1272 1.2.4.2 yamt ipf_auth_rulehead(ipf_main_softc_t *softc)
1273 1.2.4.2 yamt {
1274 1.2.4.2 yamt ipf_auth_softc_t *softa = softc->ipf_auth_soft;
1275 1.2.4.2 yamt
1276 1.2.4.2 yamt return &softa->ipf_auth_ip;
1277 1.2.4.2 yamt }
1278