Print this page
OS-7667 IPFilter needs to keep and report state for cloud firewall logging
Portions contributed by: Mike Gerdts <mike.gerdts@joyent.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/ipf/ip_log.c
+++ new/usr/src/uts/common/inet/ipf/ip_log.c
1 1 /*
2 2 * Copyright (C) 1997-2003 by Darren Reed.
3 3 *
4 4 * See the IPFILTER.LICENCE file for details on licencing.
5 5 *
6 6 * $Id: ip_log.c,v 2.75.2.7 2005/06/11 07:47:44 darrenr Exp $
7 7 *
8 8 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
9 9 * Use is subject to license terms.
10 10 *
11 - * Copyright (c) 2014, Joyent, Inc. All rights reserved.
11 + * Copyright 2019 Joyent, Inc.
12 12 */
13 13
14 14 #include <sys/param.h>
15 15 #if defined(KERNEL) || defined(_KERNEL)
16 16 # undef KERNEL
17 17 # undef _KERNEL
18 18 # define KERNEL 1
19 19 # define _KERNEL 1
20 20 #endif
21 21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
22 22 defined(_KERNEL)
23 23 # include "opt_ipfilter_log.h"
24 24 #endif
25 25 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
26 26 # if defined(_KERNEL)
27 27 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
28 28 # include "opt_ipfilter.h"
29 29 # endif
30 30 # else
31 31 # include <osreldate.h>
32 32 # endif
33 33 #endif
34 34 #ifdef SOLARIS
35 35 #undef SOLARIS
36 36 #endif
37 37 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
38 38 #define SOLARIS (1)
39 39 #else
40 40 #define SOLARIS (0)
41 41 #endif
42 42 #include <sys/errno.h>
43 43 #include <sys/types.h>
44 44 #include <sys/file.h>
45 45 #ifndef _KERNEL
46 46 # include <stdio.h>
47 47 # include <string.h>
48 48 # include <stdlib.h>
49 49 # include <ctype.h>
50 50 # define _KERNEL
51 51 # define KERNEL
52 52 # ifdef __OpenBSD__
53 53 struct file;
54 54 # endif
55 55 # include <sys/uio.h>
56 56 # undef _KERNEL
57 57 # undef KERNEL
58 58 #endif
59 59 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
60 60 # include <sys/fcntl.h>
61 61 # include <sys/filio.h>
62 62 #else
63 63 # include <sys/ioctl.h>
64 64 #endif
65 65 #include <sys/time.h>
66 66 #if defined(_KERNEL)
67 67 # include <sys/systm.h>
68 68 # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
69 69 # include <sys/proc.h>
70 70 # endif
71 71 #endif /* _KERNEL */
72 72 #if !defined(SOLARIS) && !defined(__hpux) && !defined(linux)
73 73 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
74 74 # include <sys/dirent.h>
75 75 # else
76 76 # include <sys/dir.h>
77 77 # endif
78 78 # include <sys/mbuf.h>
79 79 #else
80 80 # if !defined(__hpux) && defined(_KERNEL)
81 81 # include <sys/filio.h>
82 82 # include <sys/cred.h>
83 83 # include <sys/ddi.h>
84 84 # include <sys/sunddi.h>
85 85 # include <sys/ksynch.h>
86 86 # include <sys/kmem.h>
87 87 # include <sys/mkdev.h>
88 88 # include <sys/dditypes.h>
89 89 # include <sys/cmn_err.h>
90 90 # endif /* !__hpux */
91 91 #endif /* !SOLARIS && !__hpux */
92 92 #if !defined(linux)
93 93 # include <sys/protosw.h>
94 94 #endif
95 95 #include <sys/socket.h>
96 96
97 97 #include <net/if.h>
98 98 #ifdef sun
99 99 # include <net/af.h>
100 100 #endif
101 101 #if __FreeBSD_version >= 300000
102 102 # include <net/if_var.h>
103 103 #endif
104 104 #include <net/route.h>
105 105 #include <netinet/in.h>
106 106 #ifdef __sgi
107 107 # include <sys/ddi.h>
108 108 # ifdef IFF_DRVRLOCK /* IRIX6 */
109 109 # include <sys/hashing.h>
110 110 # endif
111 111 #endif
112 112 #if !defined(__hpux) && !defined(linux) && \
113 113 !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
114 114 # include <netinet/in_var.h>
115 115 #endif
116 116 #include <netinet/in_systm.h>
117 117 #include <netinet/ip.h>
118 118 #include <netinet/tcp.h>
119 119 #include <netinet/udp.h>
120 120 #include <netinet/ip_icmp.h>
121 121 #ifdef USE_INET6
122 122 # include <netinet/icmp6.h>
123 123 #endif
124 124 #if !defined(linux)
125 125 # include <netinet/ip_var.h>
126 126 #endif
127 127 #ifndef _KERNEL
128 128 # include <syslog.h>
129 129 #endif
130 130 #include "netinet/ip_compat.h"
131 131 #include <netinet/tcpip.h>
132 132 #include "netinet/ip_fil.h"
133 133 #include "netinet/ip_nat.h"
134 134 #include "netinet/ip_frag.h"
135 135 #include "netinet/ip_state.h"
136 136 #include "netinet/ip_auth.h"
137 137 #include "netinet/ipf_stack.h"
138 138 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
139 139 # include <sys/malloc.h>
140 140 #endif
141 141 /* END OF INCLUDES */
142 142
143 143 #ifdef IPFILTER_LOG
144 144
145 145 # if defined(IPL_SELECT)
146 146 # include <machine/sys/user.h>
147 147 # include <sys/kthread_iface.h>
148 148 # define READ_COLLISION 0x001
149 149
150 150 iplog_select_t iplog_ss[IPL_LOGMAX+1];
151 151
152 152 extern int selwait;
153 153 # endif /* IPL_SELECT */
154 154
155 155 /* ipl_magic never changes */
156 156 int ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
157 157 IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
158 158 IPL_MAGIC, IPL_MAGIC };
159 159
160 160 /* ------------------------------------------------------------------------ */
161 161 /* Function: fr_loginit */
162 162 /* Returns: int - 0 == success (always returned) */
163 163 /* Parameters: Nil */
164 164 /* */
165 165 /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
166 166 /* secret for use in calculating the "last log checksum". */
167 167 /* ------------------------------------------------------------------------ */
168 168 int fr_loginit(ifs)
169 169 ipf_stack_t *ifs;
170 170 {
171 171 int i;
172 172
173 173 for (i = IPL_LOGMAX; i >= 0; i--) {
174 174 ifs->ifs_iplt[i] = NULL;
175 175 ifs->ifs_ipll[i] = NULL;
176 176 ifs->ifs_iplh[i] = &ifs->ifs_iplt[i];
177 177 ifs->ifs_iplused[i] = 0;
178 178 bzero((char *)&ifs->ifs_iplcrc[i], sizeof(ifs->ifs_iplcrc[i]));
179 179 # ifdef IPL_SELECT
180 180 iplog_ss[i].read_waiter = 0;
181 181 iplog_ss[i].state = 0;
182 182 # endif
183 183 # if defined(linux) && defined(_KERNEL)
184 184 init_waitqueue_head(iplh_linux + i);
185 185 # endif
186 186 }
187 187
188 188 # if defined(SOLARIS) && defined(_KERNEL)
189 189 cv_init(&ifs->ifs_iplwait, "ipl condvar", CV_DRIVER, NULL);
190 190 # endif
191 191 MUTEX_INIT(&ifs->ifs_ipl_mutex, "ipf log mutex");
192 192
193 193 ifs->ifs_ipl_log_init = 1;
194 194
195 195 return 0;
196 196 }
197 197
198 198
199 199 /* ------------------------------------------------------------------------ */
200 200 /* Function: fr_logunload */
201 201 /* Returns: Nil */
202 202 /* Parameters: Nil */
203 203 /* */
204 204 /* Clean up any log data that has accumulated without being read. */
205 205 /* ------------------------------------------------------------------------ */
206 206 void fr_logunload(ifs)
207 207 ipf_stack_t *ifs;
208 208 {
209 209 int i;
210 210
211 211 if (ifs->ifs_ipl_log_init == 0)
212 212 return;
213 213
214 214 for (i = IPL_LOGMAX; i >= 0; i--)
215 215 (void) ipflog_clear(i, ifs);
216 216
217 217 # if defined(SOLARIS) && defined(_KERNEL)
218 218 cv_destroy(&ifs->ifs_iplwait);
219 219 # endif
220 220 MUTEX_DESTROY(&ifs->ifs_ipl_mutex);
221 221
222 222 ifs->ifs_ipl_log_init = 0;
223 223 }
224 224
225 225
226 226 /* ------------------------------------------------------------------------ */
227 227 /* Function: ipflog */
228 228 /* Returns: int - 0 == success, -1 == failure */
229 229 /* Parameters: fin(I) - pointer to packet information */
230 230 /* flags(I) - flags from filter rules */
231 231 /* */
232 232 /* Create a log record for a packet given that it has been triggered by a */
233 233 /* rule (or the default setting). Calculate the transport protocol header */
234 234 /* size using predetermined size of a couple of popular protocols and thus */
235 235 /* how much data to copy into the log, including part of the data body if */
236 236 /* requested. */
237 237 /* ------------------------------------------------------------------------ */
238 238 int ipflog(fin, flags)
239 239 fr_info_t *fin;
240 240 u_int flags;
241 241 {
242 242 register size_t hlen;
243 243 int types[2], mlen;
244 244 size_t sizes[2];
245 245 void *ptrs[2];
246 246 ipflog_t ipfl;
247 247 u_char p;
248 248 mb_t *m;
249 249 # if defined(SOLARIS) && defined(_KERNEL)
250 250 net_handle_t nif;
251 251 void *ifp;
252 252 # else
253 253 # if defined(__hpux) && defined(_KERNEL)
254 254 qif_t *ifp;
255 255 # else
256 256 struct ifnet *ifp;
257 257 # endif
258 258 # endif /* SOLARIS */
259 259 ipf_stack_t *ifs = fin->fin_ifs;
260 260
261 261 ipfl.fl_nattag.ipt_num[0] = 0;
262 262 m = fin->fin_m;
263 263 ifp = fin->fin_ifp;
264 264 hlen = fin->fin_hlen;
265 265 /*
266 266 * calculate header size.
267 267 */
268 268 if (fin->fin_off == 0) {
269 269 p = fin->fin_fi.fi_p;
270 270 if (p == IPPROTO_TCP)
271 271 hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
272 272 else if (p == IPPROTO_UDP)
273 273 hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
274 274 else if (p == IPPROTO_ICMP) {
275 275 struct icmp *icmp;
276 276
277 277 icmp = (struct icmp *)fin->fin_dp;
278 278
279 279 /*
280 280 * For ICMP, if the packet is an error packet, also
281 281 * include the information about the packet which
282 282 * caused the error.
283 283 */
284 284 switch (icmp->icmp_type)
285 285 {
286 286 case ICMP_UNREACH :
287 287 case ICMP_SOURCEQUENCH :
288 288 case ICMP_REDIRECT :
289 289 case ICMP_TIMXCEED :
290 290 case ICMP_PARAMPROB :
291 291 hlen += MIN(sizeof(struct icmp) + 8,
292 292 fin->fin_dlen);
293 293 break;
294 294 default :
295 295 hlen += MIN(sizeof(struct icmp),
296 296 fin->fin_dlen);
297 297 break;
298 298 }
299 299 }
300 300 # ifdef USE_INET6
301 301 else if (p == IPPROTO_ICMPV6) {
302 302 struct icmp6_hdr *icmp;
303 303
304 304 icmp = (struct icmp6_hdr *)fin->fin_dp;
305 305
306 306 /*
307 307 * For ICMPV6, if the packet is an error packet, also
308 308 * include the information about the packet which
309 309 * caused the error.
310 310 */
311 311 if (icmp->icmp6_type < 128) {
312 312 hlen += MIN(sizeof(struct icmp6_hdr) + 8,
313 313 fin->fin_dlen);
314 314 } else {
315 315 hlen += MIN(sizeof(struct icmp6_hdr),
316 316 fin->fin_dlen);
317 317 }
318 318 }
319 319 # endif
320 320 }
321 321 /*
322 322 * Get the interface number and name to which this packet is
323 323 * currently associated.
324 324 */
325 325 # if defined(SOLARIS) && defined(_KERNEL)
326 326 ipfl.fl_unit = (u_int)0;
327 327 nif = NULL;
328 328 if (fin->fin_fi.fi_v == 4)
329 329 nif = ifs->ifs_ipf_ipv4;
330 330 else if (fin->fin_fi.fi_v == 6)
331 331 nif = ifs->ifs_ipf_ipv6;
332 332 if (nif != NULL) {
333 333 if (net_getifname(nif, (phy_if_t)ifp,
334 334 ipfl.fl_ifname, sizeof(ipfl.fl_ifname)) != 0)
335 335 return (-1);
336 336 }
337 337
338 338 # else
339 339 # if defined(__hpux) && defined(_KERNEL)
340 340 ipfl.fl_unit = (u_int)0;
341 341 (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
342 342 # else
343 343 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
344 344 (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
345 345 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
346 346 COPYIFNAME(ifp, ipfl.fl_ifname);
347 347 # else
348 348 ipfl.fl_unit = (u_int)ifp->if_unit;
349 349 # if defined(_KERNEL)
350 350 if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
351 351 if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
352 352 if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
353 353 ipfl.fl_ifname[3] = ifp->if_name[3];
354 354 # else
355 355 (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
356 356 ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
357 357 # endif
358 358 # endif
359 359 # endif /* __hpux */
360 360 # endif /* SOLARIS */
361 361 mlen = fin->fin_plen - hlen;
362 362 if (!ifs->ifs_ipl_logall) {
363 363 mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
364 364 } else if ((flags & FR_LOGBODY) == 0) {
365 365 mlen = 0;
366 366 }
367 367 if (mlen < 0)
368 368 mlen = 0;
369 369 ipfl.fl_plen = (u_char)mlen;
370 370 ipfl.fl_hlen = (u_char)hlen;
371 371 ipfl.fl_rule = fin->fin_rule;
372 372 (void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
|
↓ open down ↓ |
351 lines elided |
↑ open up ↑ |
373 373 if (fin->fin_fr != NULL) {
374 374 ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
375 375 ipfl.fl_logtag = fin->fin_fr->fr_logtag;
376 376 } else {
377 377 ipfl.fl_loglevel = 0xffff;
378 378 ipfl.fl_logtag = FR_NOLOGTAG;
379 379 }
380 380 if (fin->fin_nattag != NULL)
381 381 bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
382 382 sizeof(ipfl.fl_nattag));
383 + bcopy(fin->fin_fr->fr_uuid, ipfl.fl_uuid, sizeof (ipfl.fl_uuid));
383 384 ipfl.fl_flags = flags;
384 385 ipfl.fl_dir = fin->fin_out;
385 386 ipfl.fl_lflags = fin->fin_flx;
386 387 ptrs[0] = (void *)&ipfl;
387 388 sizes[0] = sizeof(ipfl);
388 389 types[0] = 0;
389 390 # if defined(MENTAT) && defined(_KERNEL)
390 391 /*
391 392 * Are we copied from the mblk or an aligned array ?
392 393 */
393 394 if (fin->fin_ip == (ip_t *)m->b_rptr) {
394 395 ptrs[1] = m;
395 396 sizes[1] = hlen + mlen;
396 397 types[1] = 1;
397 398 } else {
398 399 ptrs[1] = fin->fin_ip;
399 400 sizes[1] = hlen + mlen;
400 401 types[1] = 0;
401 402 }
402 403 # else
403 404 ptrs[1] = m;
404 405 sizes[1] = hlen + mlen;
405 406 types[1] = 1;
406 407 # endif /* MENTAT */
407 408 return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2, fin->fin_ifs);
408 409 }
409 410
410 411
411 412 /* ------------------------------------------------------------------------ */
412 413 /* Function: ipllog */
413 414 /* Returns: int - 0 == success, -1 == failure */
414 415 /* Parameters: dev(I) - device that owns this log record */
415 416 /* fin(I) - pointer to packet information */
416 417 /* items(I) - array of pointers to log data */
417 418 /* itemsz(I) - array of size of valid memory pointed to */
418 419 /* types(I) - type of data pointed to by items pointers */
419 420 /* cnt(I) - number of elements in arrays items/itemsz/types */
420 421 /* */
421 422 /* Takes an array of parameters and constructs one record to include the */
422 423 /* miscellaneous packet information, as well as packet data, for reading */
423 424 /* from the log device. */
424 425 /* ------------------------------------------------------------------------ */
425 426 int ipllog(dev, fin, items, itemsz, types, cnt, ifs)
426 427 int dev;
427 428 fr_info_t *fin;
428 429 void **items;
429 430 size_t *itemsz;
430 431 int *types, cnt;
431 432 ipf_stack_t *ifs;
432 433 {
433 434 caddr_t buf, ptr;
434 435 iplog_t *ipl;
435 436 size_t len;
436 437 int i;
437 438 SPL_INT(s);
438 439
439 440 /*
440 441 * Check to see if this log record has a CRC which matches the last
441 442 * record logged. If it does, just up the count on the previous one
442 443 * rather than create a new one.
443 444 */
444 445 if (ifs->ifs_ipl_suppress) {
445 446 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
446 447 if ((fin != NULL) && (fin->fin_off == 0)) {
447 448 if ((ifs->ifs_ipll[dev] != NULL) &&
448 449 bcmp((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
449 450 FI_LCSIZE) == 0) {
450 451 ifs->ifs_ipll[dev]->ipl_count++;
451 452 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
452 453 return 0;
453 454 }
454 455 bcopy((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
455 456 FI_LCSIZE);
456 457 } else
457 458 bzero((char *)&ifs->ifs_iplcrc[dev], FI_CSIZE);
458 459 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
459 460 }
460 461
461 462 /*
462 463 * Get the total amount of data to be logged.
463 464 */
464 465 for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
465 466 len += itemsz[i];
466 467
467 468 /*
468 469 * check that we have space to record this information and can
469 470 * allocate that much.
470 471 */
471 472 KMALLOCS(buf, caddr_t, len);
472 473 if (buf == NULL)
473 474 return -1;
474 475 SPL_NET(s);
475 476 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
476 477 if ((ifs->ifs_iplused[dev] + len) > IPFILTER_LOGSIZE) {
477 478 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
478 479 SPL_X(s);
479 480 KFREES(buf, len);
480 481 return -1;
481 482 }
482 483 ifs->ifs_iplused[dev] += len;
483 484 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
484 485 SPL_X(s);
485 486
486 487 /*
487 488 * advance the log pointer to the next empty record and deduct the
488 489 * amount of space we're going to use.
489 490 */
490 491 ipl = (iplog_t *)buf;
491 492 ipl->ipl_magic = ipl_magic[dev];
492 493 ipl->ipl_count = 1;
493 494 ipl->ipl_next = NULL;
494 495 ipl->ipl_dsize = len;
495 496 #ifdef _KERNEL
496 497 GETKTIME(&ipl->ipl_sec);
497 498 #else
498 499 ipl->ipl_sec = 0;
499 500 ipl->ipl_usec = 0;
500 501 #endif
501 502
502 503 /*
503 504 * Loop through all the items to be logged, copying each one to the
504 505 * buffer. Use bcopy for normal data or the mb_t copyout routine.
505 506 */
506 507 for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
507 508 if (types[i] == 0) {
508 509 bcopy(items[i], ptr, itemsz[i]);
509 510 } else if (types[i] == 1) {
510 511 COPYDATA(items[i], 0, itemsz[i], ptr);
511 512 }
512 513 ptr += itemsz[i];
513 514 }
514 515 SPL_NET(s);
515 516 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
516 517 ifs->ifs_ipll[dev] = ipl;
517 518 *ifs->ifs_iplh[dev] = ipl;
518 519 ifs->ifs_iplh[dev] = &ipl->ipl_next;
519 520
520 521 /*
521 522 * Now that the log record has been completed and added to the queue,
522 523 * wake up any listeners who may want to read it.
523 524 */
524 525 # if defined(SOLARIS) && defined(_KERNEL)
525 526 cv_signal(&ifs->ifs_iplwait);
526 527 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
527 528 # else
528 529 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
529 530 WAKEUP(&ifs->ifs_iplh, dev);
530 531 # endif
531 532 SPL_X(s);
532 533 # ifdef IPL_SELECT
533 534 iplog_input_ready(dev);
534 535 # endif
535 536 return 0;
536 537 }
537 538
538 539
539 540 /* ------------------------------------------------------------------------ */
540 541 /* Function: ipflog_read */
541 542 /* Returns: int - 0 == success, else error value. */
542 543 /* Parameters: unit(I) - device we are reading from */
543 544 /* uio(O) - pointer to information about where to store data */
544 545 /* */
545 546 /* Called to handle a read on an IPFilter device. Returns only complete */
546 547 /* log messages - will not partially copy a log record out to userland. */
547 548 /* */
548 549 /* NOTE: This function will block and wait for a signal to return data if */
549 550 /* there is none present. Asynchronous I/O is not implemented. */
550 551 /* ------------------------------------------------------------------------ */
551 552 int ipflog_read(unit, uio, ifs)
552 553 minor_t unit;
553 554 struct uio *uio;
554 555 ipf_stack_t *ifs;
555 556 {
556 557 size_t dlen, copied;
557 558 int error = 0;
558 559 iplog_t *ipl;
559 560 SPL_INT(s);
560 561
561 562 /*
562 563 * Sanity checks. Make sure the minor # is valid and we're copying
563 564 * a valid chunk of data.
564 565 */
565 566 if (IPL_LOGMAX < unit)
566 567 return ENXIO;
567 568 if (uio->uio_resid == 0)
568 569 return 0;
569 570 if ((uio->uio_resid < sizeof(iplog_t)) ||
570 571 (uio->uio_resid > ifs->ifs_ipl_logsize))
571 572 return EINVAL;
572 573
573 574 /*
574 575 * Lock the log so we can snapshot the variables. Wait for a signal
575 576 * if the log is empty.
576 577 */
577 578 SPL_NET(s);
578 579 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
579 580
580 581 while (ifs->ifs_iplt[unit] == NULL) {
581 582 # if defined(SOLARIS) && defined(_KERNEL)
582 583 /*
583 584 * Prevent a deadlock with ipldetach() - see the "ipfilter
584 585 * kernel module mutexes and locking" comment block in solaris.c
585 586 * for details.
586 587 */
587 588 RWLOCK_EXIT(&ifs->ifs_ipf_global);
588 589 if (!cv_wait_sig(&ifs->ifs_iplwait, &ifs->ifs_ipl_mutex.ipf_lk)) {
589 590 READ_ENTER(&ifs->ifs_ipf_global);
590 591 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
591 592 return EINTR;
592 593 }
593 594 READ_ENTER(&ifs->ifs_ipf_global);
594 595 # else
595 596 # if defined(__hpux) && defined(_KERNEL)
596 597 lock_t *l;
597 598
598 599 # ifdef IPL_SELECT
599 600 if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
600 601 /* this is no blocking system call */
601 602 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
602 603 return 0;
603 604 }
604 605 # endif
605 606
606 607 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
607 608 l = get_sleep_lock(&ifs->ifs_iplh[unit]);
608 609 error = sleep(&ifs->ifs_iplh[unit], PZERO+1);
609 610 spinunlock(l);
610 611 # else
611 612 # if defined(__osf__) && defined(_KERNEL)
612 613 error = mpsleep(&ifs->ifs_iplh[unit], PSUSP|PCATCH, "iplread", 0,
613 614 &ifs->ifs_ipl_mutex, MS_LOCK_SIMPLE);
614 615 # else
615 616 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
616 617 SPL_X(s);
617 618 error = SLEEP(&ifs->ifs_iplh[unit], "ipl sleep");
618 619 # endif /* __osf__ */
619 620 # endif /* __hpux */
620 621 if (error)
621 622 return error;
622 623 SPL_NET(s);
623 624 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
624 625 # endif /* SOLARIS */
625 626 }
626 627
627 628 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
628 629 uio->uio_rw = UIO_READ;
629 630 # endif
630 631
631 632 for (copied = 0; ((ipl = ifs->ifs_iplt[unit]) != NULL); copied += dlen) {
632 633 dlen = ipl->ipl_dsize;
633 634 if (dlen > uio->uio_resid)
634 635 break;
635 636 /*
636 637 * Don't hold the mutex over the uiomove call.
637 638 */
638 639 ifs->ifs_iplt[unit] = ipl->ipl_next;
639 640 ifs->ifs_iplused[unit] -= dlen;
640 641 if (ifs->ifs_iplt[unit] == NULL) {
641 642 ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
642 643 ifs->ifs_ipll[unit] = NULL;
643 644 }
644 645 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
645 646 SPL_X(s);
646 647 error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
647 648 if (error) {
648 649 SPL_NET(s);
649 650 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
650 651 ifs->ifs_iplused[unit] += dlen;
651 652 ipl->ipl_next = ifs->ifs_iplt[unit];
652 653 ifs->ifs_iplt[unit] = ipl;
653 654 ifs->ifs_ipll[unit] = ipl;
654 655 if (ifs->ifs_iplh[unit] == &ifs->ifs_iplt[unit]) {
655 656 *ifs->ifs_iplh[unit] = ipl;
656 657 ifs->ifs_iplh[unit] = &ipl->ipl_next;
657 658 }
658 659 break;
659 660 }
660 661 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
661 662 KFREES((caddr_t)ipl, dlen);
662 663 SPL_NET(s);
663 664 }
664 665
665 666 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
666 667 SPL_X(s);
667 668 return error;
668 669 }
669 670
670 671
671 672 /* ------------------------------------------------------------------------ */
672 673 /* Function: ipflog_clear */
673 674 /* Returns: int - number of log bytes cleared. */
674 675 /* Parameters: unit(I) - device we are reading from */
675 676 /* */
676 677 /* Deletes all queued up log records for a given output device. */
677 678 /* ------------------------------------------------------------------------ */
678 679 int ipflog_clear(unit, ifs)
679 680 minor_t unit;
680 681 ipf_stack_t *ifs;
681 682 {
682 683 iplog_t *ipl;
683 684 int used;
684 685 SPL_INT(s);
685 686
686 687 SPL_NET(s);
687 688 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
688 689 while ((ipl = ifs->ifs_iplt[unit]) != NULL) {
689 690 ifs->ifs_iplt[unit] = ipl->ipl_next;
690 691 KFREES((caddr_t)ipl, ipl->ipl_dsize);
691 692 }
692 693 ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
693 694 ifs->ifs_ipll[unit] = NULL;
694 695 used = ifs->ifs_iplused[unit];
695 696 ifs->ifs_iplused[unit] = 0;
696 697 bzero((char *)&ifs->ifs_iplcrc[unit], FI_CSIZE);
697 698 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
698 699 SPL_X(s);
699 700 return used;
700 701 }
701 702 #endif /* IPFILTER_LOG */
|
↓ open down ↓ |
309 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX