Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*	$NetBSD: ip_auth.c,v 1.6 2018/05/03 07:13:48 maxv Exp $	*/

/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 */
#if defined(KERNEL) || defined(_KERNEL)
# undef KERNEL
# undef _KERNEL
# define        KERNEL	1
# define        _KERNEL	1
#endif
#if defined(__NetBSD__)
#include <sys/cdefs.h>
#endif
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/file.h>
#if !defined(_KERNEL)
# include <stdio.h>
# include <stdlib.h>
# ifdef _STDC_C99
#  include <stdbool.h>
# endif
# include <string.h>
# define _KERNEL
# ifdef __OpenBSD__
struct file;
# endif
# include <sys/uio.h>
# undef _KERNEL
#endif
#if defined(_KERNEL) && (__FreeBSD_version >= 220000)
# include <sys/filio.h>
# include <sys/fcntl.h>
#else
# include <sys/ioctl.h>
#endif
#if !defined(linux)
# include <sys/protosw.h>
#endif
#include <sys/socket.h>
#if defined(_KERNEL)
# include <sys/systm.h>
# if !defined(__SVR4) && !defined(__svr4__) && !defined(linux)
#  include <sys/mbuf.h>
# endif
#endif
#if defined(__SVR4) || defined(__svr4__)
# include <sys/filio.h>
# include <sys/byteorder.h>
# ifdef _KERNEL
#  include <sys/dditypes.h>
# endif
# include <sys/stream.h>
# include <sys/kmem.h>
#endif
#if (defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)) || \
    (defined(__FreeBSD_version) &&(__FreeBSD_version >= 400000))
# include <sys/queue.h>
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
# include <machine/cpu.h>
#endif
#if defined(_KERNEL) && defined(__NetBSD__) && (__NetBSD_Version__ >= 104000000)
# include <sys/proc.h>
#endif
#if defined(__NetBSD_Version__) &&  (__NetBSD_Version__ >= 400000) && \
     !defined(_KERNEL)
# include <stdbool.h>
#endif
#include <net/if.h>
#include <net/route.h>
#ifdef sun
# include <net/af.h>
#endif
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#if !defined(_KERNEL) && !defined(__osf__) && !defined(__sgi)
# define	KERNEL
# define	_KERNEL
# define	NOT_KERNEL
#endif
#if !defined(linux)
# include <netinet/ip_var.h>
#endif
#ifdef	NOT_KERNEL
# undef	_KERNEL
# undef	KERNEL
#endif
#include <netinet/tcp.h>
#if defined(IRIX) && (IRIX < 60516) /* IRIX < 6 */
extern struct ifqueue   ipintrq;		/* ip packet input queue */
#else
# if !defined(__hpux) && !defined(linux)
#  if __FreeBSD_version >= 300000
#   include <net/if_var.h>
#   if __FreeBSD_version >= 500042
#    define IF_QFULL _IF_QFULL
#    define IF_DROP _IF_DROP
#   endif /* __FreeBSD_version >= 500042 */
#  endif
#  include <netinet/in_var.h>
#  include <netinet/tcp_fsm.h>
# endif
#endif
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include "netinet/ip_compat.h"
#include "netinet/ip_fil.h"
#include "netinet/ip_auth.h"
#if !defined(MENTAT) && !defined(linux)
# include <net/netisr.h>
# ifdef __FreeBSD__
#  include <machine/cpufunc.h>
# endif
#endif
#if (__FreeBSD_version >= 300000)
# include <sys/malloc.h>
# if defined(_KERNEL) && !defined(IPFILTER_LKM)
#  include <sys/libkern.h>
#  include <sys/systm.h>
# endif
#endif
/* END OF INCLUDES */

#if !defined(lint)
#if defined(__NetBSD__)
__KERNEL_RCSID(0, "$NetBSD: ip_auth.c,v 1.6 2018/05/03 07:13:48 maxv Exp $");
#else
static const char rcsid[] = "@(#)Id: ip_auth.c,v 1.1.1.2 2012/07/22 13:45:08 darrenr Exp";
#endif
#endif


typedef	struct ipf_auth_softc_s {
#if SOLARIS && defined(_KERNEL)
	kcondvar_t	ipf_auth_wait;
#endif /* SOLARIS */
#if defined(linux) && defined(_KERNEL)
	wait_queue_head_t ipf_auth_next_linux;
#endif
	ipfrwlock_t	ipf_authlk;
	ipfmutex_t	ipf_auth_mx;
	int		ipf_auth_size;
	int		ipf_auth_used;
	int		ipf_auth_replies;
	int		ipf_auth_defaultage;
	int		ipf_auth_lock;
	ipf_authstat_t	ipf_auth_stats;
	frauth_t	*ipf_auth;
	mb_t		**ipf_auth_pkts;
	int		ipf_auth_start;
	int		ipf_auth_end;
	int		ipf_auth_next;
	frauthent_t	*ipf_auth_entries;
	frentry_t	*ipf_auth_ip;
	frentry_t	*ipf_auth_rules;
} ipf_auth_softc_t;


static void ipf_auth_deref(frauthent_t **);
static void ipf_auth_deref_unlocked(ipf_auth_softc_t *, frauthent_t **);
static int ipf_auth_geniter(ipf_main_softc_t *, ipftoken_t *,
				 ipfgeniter_t *, ipfobj_t *);
static int ipf_auth_reply(ipf_main_softc_t *, ipf_auth_softc_t *, char *);
static int ipf_auth_wait(ipf_main_softc_t *, ipf_auth_softc_t *, char *);
static int ipf_auth_flush(void *);


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_main_load                                          */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  None                                                        */
/*                                                                          */
/* A null-op function that exists as a placeholder so that the flow in      */
/* other functions is obvious.                                              */
/* ------------------------------------------------------------------------ */
int
ipf_auth_main_load(void)
{
	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_main_unload                                        */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  None                                                        */
/*                                                                          */
/* A null-op function that exists as a placeholder so that the flow in      */
/* other functions is obvious.                                              */
/* ------------------------------------------------------------------------ */
int
ipf_auth_main_unload(void)
{
	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_soft_create                                        */
/* Returns:     int - NULL = failure, else success                          */
/* Parameters:  softc(I) - pointer to soft context data                     */
/*                                                                          */
/* Create a structre to store all of the run-time data for packet auth in   */
/* and initialise some fields to their defaults.                            */
/* ------------------------------------------------------------------------ */
void *
ipf_auth_soft_create(ipf_main_softc_t *softc)
{
	ipf_auth_softc_t *softa;

	KMALLOC(softa, ipf_auth_softc_t *);
	if (softa == NULL)
		return NULL;

	bzero((char *)softa, sizeof(*softa));

	softa->ipf_auth_size = FR_NUMAUTH;
	softa->ipf_auth_defaultage = 600;

	RWLOCK_INIT(&softa->ipf_authlk, "ipf IP User-Auth rwlock");
	MUTEX_INIT(&softa->ipf_auth_mx, "ipf auth log mutex");
#if SOLARIS && defined(_KERNEL)
	cv_init(&softa->ipf_auth_wait, "ipf auth condvar", CV_DRIVER, NULL);
#endif

	return softa;
}

/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_soft_init                                          */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  softc(I) - pointer to soft context data                     */
/*              arg(I)   - opaque pointer to auth context data              */
/*                                                                          */
/* Allocate memory and initialise data structures used in handling auth     */
/* rules.                                                                   */
/* ------------------------------------------------------------------------ */
int
ipf_auth_soft_init(ipf_main_softc_t *softc, void *arg)
{
	ipf_auth_softc_t *softa = arg;

	KMALLOCS(softa->ipf_auth, frauth_t *,
		 softa->ipf_auth_size * sizeof(*softa->ipf_auth));
	if (softa->ipf_auth == NULL)
		return -1;
	bzero((char *)softa->ipf_auth,
	      softa->ipf_auth_size * sizeof(*softa->ipf_auth));

	KMALLOCS(softa->ipf_auth_pkts, mb_t **,
		 softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));
	if (softa->ipf_auth_pkts == NULL)
		return -2;
	bzero((char *)softa->ipf_auth_pkts,
	      softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));

#if defined(linux) && defined(_KERNEL)
	init_waitqueue_head(&softa->ipf_auth_next_linux);
#endif

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_soft_fini                                          */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  softc(I) - pointer to soft context data                     */
/*              arg(I)   - opaque pointer to auth context data              */
/*                                                                          */
/* Free all network buffer memory used to keep saved packets that have been */
/* connectedd to the soft soft context structure *but* do not free that: it */
/* is free'd by _destroy().                                                 */
/* ------------------------------------------------------------------------ */
int
ipf_auth_soft_fini(ipf_main_softc_t *softc, void *arg)
{
	ipf_auth_softc_t *softa = arg;
	frauthent_t *fae, **faep;
	frentry_t *fr, **frp;
	mb_t *m;
	int i;

	if (softa->ipf_auth != NULL) {
		KFREES(softa->ipf_auth,
		       softa->ipf_auth_size * sizeof(*softa->ipf_auth));
		softa->ipf_auth = NULL;
	}

	if (softa->ipf_auth_pkts != NULL) {
		for (i = 0; i < softa->ipf_auth_size; i++) {
			m = softa->ipf_auth_pkts[i];
			if (m != NULL) {
				FREE_MB_T(m);
				softa->ipf_auth_pkts[i] = NULL;
			}
		}
		KFREES(softa->ipf_auth_pkts,
		       softa->ipf_auth_size * sizeof(*softa->ipf_auth_pkts));
		softa->ipf_auth_pkts = NULL;
	}

	faep = &softa->ipf_auth_entries;
	while ((fae = *faep) != NULL) {
		*faep = fae->fae_next;
		KFREE(fae);
	}
	softa->ipf_auth_ip = NULL;

	if (softa->ipf_auth_rules != NULL) {
		for (frp = &softa->ipf_auth_rules; ((fr = *frp) != NULL); ) {
			if (fr->fr_ref == 1) {
				*frp = fr->fr_next;
				MUTEX_DESTROY(&fr->fr_lock);
				KFREE(fr);
			} else
				frp = &fr->fr_next;
		}
	}

	return 0;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_soft_destroy                                       */
/* Returns:     void                                                        */
/* Parameters:  softc(I) - pointer to soft context data                     */
/*              arg(I)   - opaque pointer to auth context data              */
/*                                                                          */
/* Undo what was done in _create() - i.e. free the soft context data.       */
/* ------------------------------------------------------------------------ */
void
ipf_auth_soft_destroy(ipf_main_softc_t *softc, void *arg)
{
	ipf_auth_softc_t *softa = arg;

# if SOLARIS && defined(_KERNEL)
	cv_destroy(&softa->ipf_auth_wait);
# endif
	MUTEX_DESTROY(&softa->ipf_auth_mx);
	RW_DESTROY(&softa->ipf_authlk);

	KFREE(softa);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_setlock                                            */
/* Returns:     void                                                        */
/* Paramters:   arg(I) - pointer to soft context data                       */
/*              tmp(I) - value to assign to auth lock                       */
/*                                                                          */
/* ------------------------------------------------------------------------ */
void
ipf_auth_setlock(void *arg, int tmp)
{
	ipf_auth_softc_t *softa = arg;

	softa->ipf_auth_lock = tmp;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_check                                              */
/* Returns:     frentry_t* - pointer to ipf rule if match found, else NULL  */
/* Parameters:  fin(I)   - pointer to ipftoken structure                    */
/*              passp(I) - pointer to ipfgeniter structure                  */
/*                                                                          */
/* Check if a packet has authorization.  If the packet is found to match an */
/* authorization result and that would result in a feedback loop (i.e. it   */
/* will end up returning FR_AUTH) then return FR_BLOCK instead.             */
/* ------------------------------------------------------------------------ */
frentry_t *
ipf_auth_check(fr_info_t *fin, u_32_t *passp)
{
	ipf_main_softc_t *softc = fin->fin_main_soft;
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
	frentry_t *fr;
	frauth_t *fra;
	u_32_t pass;
	u_short id;
	ip_t *ip;
	int i;

	if (softa->ipf_auth_lock || !softa->ipf_auth_used)
		return NULL;

	ip = fin->fin_ip;
	id = ip->ip_id;

	READ_ENTER(&softa->ipf_authlk);
	for (i = softa->ipf_auth_start; i != softa->ipf_auth_end; ) {
		/*
		 * index becomes -2 only after an SIOCAUTHW.  Check this in
		 * case the same packet gets sent again and it hasn't yet been
		 * auth'd.
		 */
		fra = softa->ipf_auth + i;
		if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
		    !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
			/*
			 * Avoid feedback loop.
			 */
			if (!(pass = fra->fra_pass) || (FR_ISAUTH(pass))) {
				pass = FR_BLOCK;
				fin->fin_reason = FRB_AUTHFEEDBACK;
			}
			/*
			 * Create a dummy rule for the stateful checking to
			 * use and return.  Zero out any values we don't
			 * trust from userland!
			 */
			if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
			     (fin->fin_flx & FI_FRAG))) {
				KMALLOC(fr, frentry_t *);
				if (fr) {
					bcopy((char *)fra->fra_info.fin_fr,
					      (char *)fr, sizeof(*fr));
					fr->fr_grp = NULL;
					fr->fr_ifa = fin->fin_ifp;
					fr->fr_func = NULL;
					fr->fr_ref = 1;
					fr->fr_flags = pass;
					fr->fr_ifas[1] = NULL;
					fr->fr_ifas[2] = NULL;
					fr->fr_ifas[3] = NULL;
					MUTEX_INIT(&fr->fr_lock,
						   "ipf auth rule");
				}
			} else
				fr = fra->fra_info.fin_fr;
			fin->fin_fr = fr;
			fin->fin_flx |= fra->fra_flx;
			RWLOCK_EXIT(&softa->ipf_authlk);

			WRITE_ENTER(&softa->ipf_authlk);
			/*
			 * ipf_auth_rules is populated with the rules malloc'd
			 * above and only those.
			 */
			if ((fr != NULL) && (fr != fra->fra_info.fin_fr)) {
				fr->fr_next = softa->ipf_auth_rules;
				softa->ipf_auth_rules = fr;
			}
			softa->ipf_auth_stats.fas_hits++;
			fra->fra_index = -1;
			softa->ipf_auth_used--;
			softa->ipf_auth_replies--;
			if (i == softa->ipf_auth_start) {
				while (fra->fra_index == -1) {
					i++;
					fra++;
					if (i == softa->ipf_auth_size) {
						i = 0;
						fra = softa->ipf_auth;
					}
					softa->ipf_auth_start = i;
					if (i == softa->ipf_auth_end)
						break;
				}
				if (softa->ipf_auth_start ==
				    softa->ipf_auth_end) {
					softa->ipf_auth_next = 0;
					softa->ipf_auth_start = 0;
					softa->ipf_auth_end = 0;
				}
			}
			RWLOCK_EXIT(&softa->ipf_authlk);
			if (passp != NULL)
				*passp = pass;
			softa->ipf_auth_stats.fas_hits++;
			return fr;
		}
		i++;
		if (i == softa->ipf_auth_size)
			i = 0;
	}
	RWLOCK_EXIT(&softa->ipf_authlk);
	softa->ipf_auth_stats.fas_miss++;
	return NULL;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_new                                                */
/* Returns:     int - 1 == success, 0 = did not put packet on auth queue    */
/* Parameters:  m(I)   - pointer to mb_t with packet in it                  */
/*              fin(I) - pointer to packet information                      */
/*                                                                          */
/* Check if we have room in the auth array to hold details for another      */
/* packet. If we do, store it and wake up any user programs which are       */
/* waiting to hear about these events.                                      */
/* ------------------------------------------------------------------------ */
int
ipf_auth_new(mb_t *m, fr_info_t *fin)
{
	ipf_main_softc_t *softc = fin->fin_main_soft;
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
#if defined(_KERNEL) && defined(MENTAT)
	qpktinfo_t *qpi = fin->fin_qpi;
#endif
	frauth_t *fra;
#if !defined(sparc) && !defined(m68k)
	ip_t *ip;
#endif
	int i;

	if (softa->ipf_auth_lock)
		return 0;

	WRITE_ENTER(&softa->ipf_authlk);
	if (((softa->ipf_auth_end + 1) % softa->ipf_auth_size) ==
	    softa->ipf_auth_start) {
		softa->ipf_auth_stats.fas_nospace++;
		RWLOCK_EXIT(&softa->ipf_authlk);
		return 0;
	}

	softa->ipf_auth_stats.fas_added++;
	softa->ipf_auth_used++;
	i = softa->ipf_auth_end++;
	if (softa->ipf_auth_end == softa->ipf_auth_size)
		softa->ipf_auth_end = 0;

	fra = softa->ipf_auth + i;
	fra->fra_index = i;
	if (fin->fin_fr != NULL)
		fra->fra_pass = fin->fin_fr->fr_flags;
	else
		fra->fra_pass = 0;
	fra->fra_age = softa->ipf_auth_defaultage;
	bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
	fra->fra_flx = fra->fra_info.fin_flx & (FI_STATE|FI_NATED);
	fra->fra_info.fin_flx &= ~(FI_STATE|FI_NATED);
#if !defined(sparc) && !defined(m68k)
	/*
	 * No need to copyback here as we want to undo the changes, not keep
	 * them.
	 */
	ip = fin->fin_ip;
# if defined(MENTAT) && defined(_KERNEL)
	if ((ip == (ip_t *)m->b_rptr) && (fin->fin_v == 4))
# endif
	{
		register u_short bo;

		bo = ip->ip_len;
		ip->ip_len = htons(bo);
		bo = ip->ip_off;
		ip->ip_off = htons(bo);
	}
#endif
#if SOLARIS && defined(_KERNEL)
	COPYIFNAME(fin->fin_v, fin->fin_ifp, fra->fra_info.fin_ifname);
	m->b_rptr -= qpi->qpi_off;
	fra->fra_q = qpi->qpi_q;	/* The queue can disappear! */
	fra->fra_m = *fin->fin_mp;
	fra->fra_info.fin_mp = &fra->fra_m;
	softa->ipf_auth_pkts[i] = *(mblk_t **)fin->fin_mp;
	RWLOCK_EXIT(&softa->ipf_authlk);
	cv_signal(&softa->ipf_auth_wait);
	pollwakeup(&softc->ipf_poll_head[IPL_LOGAUTH], POLLIN|POLLRDNORM);
#else
	softa->ipf_auth_pkts[i] = m;
	RWLOCK_EXIT(&softa->ipf_authlk);
	WAKEUP(&softa->ipf_auth_next, 0);
#endif
	return 1;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_ioctl                                              */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  data(IO) - pointer to ioctl data                            */
/*              cmd(I)   - ioctl command                                    */
/*              mode(I)  - mode flags associated with open descriptor       */
/*              uid(I)   - uid associatd with application making the call   */
/*              ctx(I)   - pointer for context                              */
/*                                                                          */
/* This function handles all of the ioctls recognised by the auth component */
/* in IPFilter - ie ioctls called on an open fd for /dev/ipf_auth           */
/* ------------------------------------------------------------------------ */
int
ipf_auth_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode,
    int uid, void *ctx)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
	int error = 0, i;
	SPL_INT(s);

	switch (cmd)
	{
	case SIOCGENITER :
	    {
		ipftoken_t *token;
		ipfgeniter_t iter;
		ipfobj_t obj;

		error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
		if (error != 0)
			break;

		SPL_SCHED(s);
		token = ipf_token_find(softc, IPFGENITER_AUTH, uid, ctx);
		if (token != NULL)
			error = ipf_auth_geniter(softc, token, &iter, &obj);
		else {
			WRITE_ENTER(&softc->ipf_tokens);
			ipf_token_deref(softc, token);
			RWLOCK_EXIT(&softc->ipf_tokens);
			IPFERROR(10001);
			error = ESRCH;
		}
		SPL_X(s);

		break;
	    }

	case SIOCADAFR :
	case SIOCRMAFR :
		if (!(mode & FWRITE)) {
			IPFERROR(10002);
			error = EPERM;
		} else
			error = frrequest(softc, IPL_LOGAUTH, cmd, data,
					  softc->ipf_active, 1);
		break;

	case SIOCSTLCK :
		if (!(mode & FWRITE)) {
			IPFERROR(10003);
			error = EPERM;
		} else {
			error = ipf_lock(data, &softa->ipf_auth_lock);
		}
		break;

	case SIOCATHST:
		softa->ipf_auth_stats.fas_faelist = softa->ipf_auth_entries;
		error = ipf_outobj(softc, data, &softa->ipf_auth_stats,
				   IPFOBJ_AUTHSTAT);
		break;

	case SIOCIPFFL:
		SPL_NET(s);
		WRITE_ENTER(&softa->ipf_authlk);
		i = ipf_auth_flush(softa);
		RWLOCK_EXIT(&softa->ipf_authlk);
		SPL_X(s);
		error = BCOPYOUT(&i, data, sizeof(i));
		if (error != 0) {
			IPFERROR(10004);
			error = EFAULT;
		}
		break;

	case SIOCAUTHW:
		error = ipf_auth_wait(softc, softa, data);
		break;

	case SIOCAUTHR:
		error = ipf_auth_reply(softc, softa, data);
		break;

	default :
		IPFERROR(10005);
		error = EINVAL;
		break;
	}
	return error;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_expire                                             */
/* Returns:     None                                                        */
/* Parameters:  None                                                        */
/*                                                                          */
/* Slowly expire held auth records.  Timeouts are set in expectation of     */
/* this being called twice per second.                                      */
/* ------------------------------------------------------------------------ */
void
ipf_auth_expire(ipf_main_softc_t *softc)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
	frauthent_t *fae, **faep;
	frentry_t *fr, **frp;
	frauth_t *fra;
	mb_t *m;
	int i;
	SPL_INT(s);

	if (softa->ipf_auth_lock)
		return;
	SPL_NET(s);
	WRITE_ENTER(&softa->ipf_authlk);
	for (i = 0, fra = softa->ipf_auth; i < softa->ipf_auth_size;
	     i++, fra++) {
		fra->fra_age--;
		if ((fra->fra_age == 0) &&
		    (softa->ipf_auth[i].fra_index != -1)) {
			if ((m = softa->ipf_auth_pkts[i]) != NULL) {
				FREE_MB_T(m);
				softa->ipf_auth_pkts[i] = NULL;
			} else if (softa->ipf_auth[i].fra_index == -2) {
				softa->ipf_auth_replies--;
			}
			softa->ipf_auth[i].fra_index = -1;
			softa->ipf_auth_stats.fas_expire++;
			softa->ipf_auth_used--;
		}
	}

	/*
	 * Expire pre-auth rules
	 */
	for (faep = &softa->ipf_auth_entries; ((fae = *faep) != NULL); ) {
		fae->fae_age--;
		if (fae->fae_age == 0) {
			ipf_auth_deref(&fae);
			softa->ipf_auth_stats.fas_expire++;
		} else
			faep = &fae->fae_next;
	}
	if (softa->ipf_auth_entries != NULL)
		softa->ipf_auth_ip = &softa->ipf_auth_entries->fae_fr;
	else
		softa->ipf_auth_ip = NULL;

	for (frp = &softa->ipf_auth_rules; ((fr = *frp) != NULL); ) {
		if (fr->fr_ref == 1) {
			*frp = fr->fr_next;
			MUTEX_DESTROY(&fr->fr_lock);
			KFREE(fr);
		} else
			frp = &fr->fr_next;
	}
	RWLOCK_EXIT(&softa->ipf_authlk);
	SPL_X(s);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_precmd                                             */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  cmd(I)  - ioctl command for rule                            */
/*              fr(I)   - pointer to ipf rule                               */
/*              fptr(I) - pointer to caller's 'fr'                          */
/*                                                                          */
/* ------------------------------------------------------------------------ */
int
ipf_auth_precmd(ipf_main_softc_t *softc, ioctlcmd_t cmd, frentry_t *fr,
    frentry_t **frptr)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
	frauthent_t *fae, **faep;
	int error = 0;
	SPL_INT(s);

	if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
		IPFERROR(10006);
		return EIO;
	}

	for (faep = &softa->ipf_auth_entries; ((fae = *faep) != NULL); ) {
		if (&fae->fae_fr == fr)
			break;
		else
			faep = &fae->fae_next;
	}

	if (cmd == (ioctlcmd_t)SIOCRMAFR) {
		if (fr == NULL || frptr == NULL) {
			IPFERROR(10007);
			error = EINVAL;

		} else if (fae == NULL) {
			IPFERROR(10008);
			error = ESRCH;

		} else {
			SPL_NET(s);
			WRITE_ENTER(&softa->ipf_authlk);
			*faep = fae->fae_next;
			if (softa->ipf_auth_ip == &fae->fae_fr)
				softa->ipf_auth_ip = softa->ipf_auth_entries ?
				    &softa->ipf_auth_entries->fae_fr : NULL;
			RWLOCK_EXIT(&softa->ipf_authlk);
			SPL_X(s);

			KFREE(fae);
		}
	} else if (fr != NULL && frptr != NULL) {
		KMALLOC(fae, frauthent_t *);
		if (fae != NULL) {
			bcopy((char *)fr, (char *)&fae->fae_fr,
			      sizeof(*fr));
			SPL_NET(s);
			WRITE_ENTER(&softa->ipf_authlk);
			fae->fae_age = softa->ipf_auth_defaultage;
			fae->fae_fr.fr_hits = 0;
			fae->fae_fr.fr_next = *frptr;
			fae->fae_ref = 1;
			*frptr = &fae->fae_fr;
			fae->fae_next = *faep;
			*faep = fae;
			softa->ipf_auth_ip = &softa->ipf_auth_entries->fae_fr;
			RWLOCK_EXIT(&softa->ipf_authlk);
			SPL_X(s);
		} else {
			IPFERROR(10009);
			error = ENOMEM;
		}
	} else {
		IPFERROR(10010);
		error = EINVAL;
	}
	return error;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_flush                                              */
/* Returns:     int - number of auth entries flushed                        */
/* Parameters:  None                                                        */
/* Locks:       WRITE(ipf_authlk)                                           */
/*                                                                          */
/* This function flushs the ipf_auth_pkts array of any packet data with     */
/* references still there.                                                  */
/* It is expected that the caller has already acquired the correct locks or */
/* set the priority level correctly for this to block out other code paths  */
/* into these data structures.                                              */
/* ------------------------------------------------------------------------ */
static int
ipf_auth_flush(void *arg)
{
	ipf_auth_softc_t *softa = arg;
	int i, num_flushed;
	mb_t *m;

	if (softa->ipf_auth_lock)
		return -1;

	num_flushed = 0;

	for (i = 0 ; i < softa->ipf_auth_size; i++) {
		if (softa->ipf_auth[i].fra_index != -1) {
			m = softa->ipf_auth_pkts[i];
			if (m != NULL) {
				FREE_MB_T(m);
				softa->ipf_auth_pkts[i] = NULL;
			}

			softa->ipf_auth[i].fra_index = -1;
			/* perhaps add & use a flush counter inst.*/
			softa->ipf_auth_stats.fas_expire++;
			num_flushed++;
		}
	}

	softa->ipf_auth_start = 0;
	softa->ipf_auth_end = 0;
	softa->ipf_auth_next = 0;
	softa->ipf_auth_used = 0;
	softa->ipf_auth_replies = 0;

	return num_flushed;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_waiting                                            */
/* Returns:     int - number of packets in the auth queue                   */
/* Parameters:  None                                                        */
/*                                                                          */
/* Simple truth check to see if there are any packets waiting in the auth   */
/* queue.                                                                   */
/* ------------------------------------------------------------------------ */
int
ipf_auth_waiting(ipf_main_softc_t *softc)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;

	return (softa->ipf_auth_used != 0);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_geniter                                            */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  token(I) - pointer to ipftoken structure                    */
/*              itp(I)   - pointer to ipfgeniter structure                  */
/*              objp(I)  - pointer to ipf object destription                */
/*                                                                          */
/* Iterate through the list of entries in the auth queue list.              */
/* objp is used here to get the location of where to do the copy out to.    */
/* Stomping over various fields with new information will not harm anything */
/* ------------------------------------------------------------------------ */
static int
ipf_auth_geniter(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp,
    ipfobj_t *objp)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;
	frauthent_t *fae, *next, zero;
	int error;

	if (itp->igi_data == NULL) {
		IPFERROR(10011);
		return EFAULT;
	}

	if (itp->igi_type != IPFGENITER_AUTH) {
		IPFERROR(10012);
		return EINVAL;
	}

	objp->ipfo_type = IPFOBJ_FRAUTH;
	objp->ipfo_ptr = itp->igi_data;
	objp->ipfo_size = sizeof(frauth_t);

	READ_ENTER(&softa->ipf_authlk);

	fae = token->ipt_data;
	if (fae == NULL) {
		next = softa->ipf_auth_entries;
	} else {
		next = fae->fae_next;
	}

	/*
	 * If we found an auth entry to use, bump its reference count
	 * so that it can be used for is_next when we come back.
	 */
	if (next != NULL) {
		ATOMIC_INC(next->fae_ref);
		token->ipt_data = next;
	} else {
		bzero(&zero, sizeof(zero));
		next = &zero;
		token->ipt_data = NULL;
	}

	RWLOCK_EXIT(&softa->ipf_authlk);

	error = ipf_outobjk(softc, objp, next);
	if (fae != NULL)
		ipf_auth_deref_unlocked(softa, &fae);

	if (next->fae_next == NULL)
		ipf_token_mark_complete(token);
	return error;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_deref_unlocked                                     */
/* Returns:     None                                                        */
/* Parameters:  faep(IO) - pointer to caller's frauthent_t pointer          */
/*                                                                          */
/* Wrapper for ipf_auth_deref for when a write lock on ipf_authlk is not    */
/* held.                                                                    */
/* ------------------------------------------------------------------------ */
static void
ipf_auth_deref_unlocked(ipf_auth_softc_t *softa, frauthent_t **faep)
{
	WRITE_ENTER(&softa->ipf_authlk);
	ipf_auth_deref(faep);
	RWLOCK_EXIT(&softa->ipf_authlk);
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_deref                                              */
/* Returns:     None                                                        */
/* Parameters:  faep(IO) - pointer to caller's frauthent_t pointer          */
/* Locks:       WRITE(ipf_authlk)                                           */
/*                                                                          */
/* This function unconditionally sets the pointer in the caller to NULL,    */
/* to make it clear that it should no longer use that pointer, and drops    */
/* the reference count on the structure by 1.  If it reaches 0, free it up. */
/* ------------------------------------------------------------------------ */
static void
ipf_auth_deref(frauthent_t **faep)
{
	frauthent_t *fae;

	fae = *faep;
	*faep = NULL;

	fae->fae_ref--;
	if (fae->fae_ref == 0) {
		KFREE(fae);
	}
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_wait_pkt                                           */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  data(I) - pointer to data from ioctl call                   */
/*                                                                          */
/* This function is called when an application is waiting for a packet to   */
/* match an "auth" rule by issuing an SIOCAUTHW ioctl.  If there is already */
/* a packet waiting on the queue then we will return that _one_ immediately.*/
/* If there are no packets present in the queue (ipf_auth_pkts) then we go  */
/* to sleep.                                                                */
/* ------------------------------------------------------------------------ */
static int
ipf_auth_wait(ipf_main_softc_t *softc, ipf_auth_softc_t *softa, char *data)
{
	frauth_t auth, *au = &auth;
	int error, len, i;
	mb_t *m;
	char *t;
	SPL_INT(s);

ipf_auth_ioctlloop:
	error = ipf_inobj(softc, data, NULL, au, IPFOBJ_FRAUTH);
	if (error != 0)
		return error;

	/*
	 * XXX Locks are held below over calls to copyout...a better
	 * solution needs to be found so this isn't necessary.  The situation
	 * we are trying to guard against here is an error in the copyout
	 * steps should not cause the packet to "disappear" from the queue.
	 */
	SPL_NET(s);
	READ_ENTER(&softa->ipf_authlk);

	/*
	 * If ipf_auth_next is not equal to ipf_auth_end it will be because
	 * there is a packet waiting to be delt with in the ipf_auth_pkts
	 * array.  We copy as much of that out to user space as requested.
	 */
	if (softa->ipf_auth_used > 0) {
		while (softa->ipf_auth_pkts[softa->ipf_auth_next] == NULL) {
			softa->ipf_auth_next++;
			if (softa->ipf_auth_next == softa->ipf_auth_size)
				softa->ipf_auth_next = 0;
		}

		error = ipf_outobj(softc, data,
				   &softa->ipf_auth[softa->ipf_auth_next],
				   IPFOBJ_FRAUTH);
		if (error != 0) {
			RWLOCK_EXIT(&softa->ipf_authlk);
			SPL_X(s);
			return error;
		}

		if (auth.fra_len != 0 && auth.fra_buf != NULL) {
			/*
			 * Copy packet contents out to user space if
			 * requested.  Bail on an error.
			 */
			m = softa->ipf_auth_pkts[softa->ipf_auth_next];
			len = MSGDSIZE(m);
			if (len > auth.fra_len)
				len = auth.fra_len;
			auth.fra_len = len;

			for (t = auth.fra_buf; m && (len > 0); ) {
				i = MIN(M_LEN(m), len);
				error = copyoutptr(softc, MTOD(m, char *),
						   &t, i);
				len -= i;
				t += i;
				if (error != 0) {
					RWLOCK_EXIT(&softa->ipf_authlk);
					SPL_X(s);
					return error;
				}
				m = m->m_next;
			}
		}
		RWLOCK_EXIT(&softa->ipf_authlk);

		SPL_NET(s);
		WRITE_ENTER(&softa->ipf_authlk);
		softa->ipf_auth_next++;
		if (softa->ipf_auth_next == softa->ipf_auth_size)
			softa->ipf_auth_next = 0;
		RWLOCK_EXIT(&softa->ipf_authlk);
		SPL_X(s);

		return 0;
	}
	RWLOCK_EXIT(&softa->ipf_authlk);
	SPL_X(s);

	MUTEX_ENTER(&softa->ipf_auth_mx);
#ifdef	_KERNEL
# if	SOLARIS
	error = 0;
	if (!cv_wait_sig(&softa->ipf_auth_wait, &softa->ipf_auth_mx.ipf_lk)) {
		IPFERROR(10014);
		error = EINTR;
	}
# else /* SOLARIS */
#  ifdef __hpux
	{
	lock_t *l;

	l = get_sleep_lock(&softa->ipf_auth_next);
	error = sleep(&softa->ipf_auth_next, PZERO+1);
	spinunlock(l);
	}
#  else
#   ifdef __osf__
	error = mpsleep(&softa->ipf_auth_next, PSUSP|PCATCH, "ipf_auth_next",
			0, &softa->ipf_auth_mx, MS_LOCK_SIMPLE);
#   else
	error = SLEEP(&softa->ipf_auth_next, "ipf_auth_next");
#   endif /* __osf__ */
#  endif /* __hpux */
# endif /* SOLARIS */
#endif
	MUTEX_EXIT(&softa->ipf_auth_mx);
	if (error == 0)
		goto ipf_auth_ioctlloop;
	return error;
}


/* ------------------------------------------------------------------------ */
/* Function:    ipf_auth_reply                                              */
/* Returns:     int - 0 == success, else error                              */
/* Parameters:  data(I) - pointer to data from ioctl call                   */
/*                                                                          */
/* This function is called by an application when it wants to return a      */
/* decision on a packet using the SIOCAUTHR ioctl.  This is after it has    */
/* received information using an SIOCAUTHW.  The decision returned in the   */
/* form of flags, the same as those used in each rule.                      */
/* ------------------------------------------------------------------------ */
static int
ipf_auth_reply(ipf_main_softc_t *softc, ipf_auth_softc_t *softa, char *data)
{
	frauth_t auth, *au = &auth, *fra;
	fr_info_t fin;
	int error, i;
#ifdef _KERNEL
	mb_t *m;
#endif
	SPL_INT(s);

	error = ipf_inobj(softc, data, NULL, &auth, IPFOBJ_FRAUTH);
	if (error != 0)
		return error;

	SPL_NET(s);
	WRITE_ENTER(&softa->ipf_authlk);

	i = au->fra_index;
	fra = softa->ipf_auth + i;
	error = 0;

	/*
	 * Check the validity of the information being returned with two simple
	 * checks.  First, the auth index value should be within the size of
	 * the array and second the packet id being returned should also match.
	 */
	if ((i < 0) || (i >= softa->ipf_auth_size)) {
		RWLOCK_EXIT(&softa->ipf_authlk);
		SPL_X(s);
		IPFERROR(10015);
		return ESRCH;
	}
	if  (fra->fra_info.fin_id != au->fra_info.fin_id) {
		RWLOCK_EXIT(&softa->ipf_authlk);
		SPL_X(s);
		IPFERROR(10019);
		return ESRCH;
	}

	fra->fra_index = -2;
	fra->fra_pass = au->fra_pass;
#ifdef	_KERNEL
	m = softa->ipf_auth_pkts[i];
#endif
	softa->ipf_auth_pkts[i] = NULL;
	softa->ipf_auth_replies++;
	bcopy(&fra->fra_info, &fin, sizeof(fin));

	RWLOCK_EXIT(&softa->ipf_authlk);

	/*
	 * Re-insert the packet back into the packet stream flowing through
	 * the kernel in a manner that will mean IPFilter sees the packet
	 * again.  This is not the same as is done with fastroute,
	 * deliberately, as we want to resume the normal packet processing
	 * path for it.
	 */
#ifdef	_KERNEL
	if ((m != NULL) && (au->fra_info.fin_out != 0)) {
		error = ipf_inject(&fin, m);
		if (error != 0) {
			IPFERROR(10016);
			error = ENOBUFS;
			softa->ipf_auth_stats.fas_sendfail++;
		} else {
			softa->ipf_auth_stats.fas_sendok++;
		}
	} else if (m) {
		error = ipf_inject(&fin, m);
		if (error != 0) {
			IPFERROR(10017);
			error = ENOBUFS;
			softa->ipf_auth_stats.fas_quefail++;
		} else {
			softa->ipf_auth_stats.fas_queok++;
		}
	} else {
		IPFERROR(10018);
		error = EINVAL;
	}

	/*
	 * If we experience an error which will result in the packet
	 * not being processed, make sure we advance to the next one.
	 */
	if (error == ENOBUFS) {
		WRITE_ENTER(&softa->ipf_authlk);
		softa->ipf_auth_used--;
		fra->fra_index = -1;
		fra->fra_pass = 0;
		if (i == softa->ipf_auth_start) {
			while (fra->fra_index == -1) {
				i++;
				if (i == softa->ipf_auth_size)
					i = 0;
				softa->ipf_auth_start = i;
				if (i == softa->ipf_auth_end)
					break;
			}
			if (softa->ipf_auth_start == softa->ipf_auth_end) {
				softa->ipf_auth_next = 0;
				softa->ipf_auth_start = 0;
				softa->ipf_auth_end = 0;
			}
		}
		RWLOCK_EXIT(&softa->ipf_authlk);
	}
#endif /* _KERNEL */
	SPL_X(s);

	return 0;
}


u_32_t
ipf_auth_pre_scanlist(ipf_main_softc_t *softc, fr_info_t *fin, u_32_t pass)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;

	if (softa->ipf_auth_ip != NULL)
		return ipf_scanlist(fin, softc->ipf_pass);

	return pass;
}


frentry_t **
ipf_auth_rulehead(ipf_main_softc_t *softc)
{
	ipf_auth_softc_t *softa = softc->ipf_auth_soft;

	return &softa->ipf_auth_ip;
}