summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/ipf/solaris.c
blob: 5ccbfa31885a87441e4487765cacb9a1f0a9fdca (plain)
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
/*
 * Copyright (C) 1993-2001, 2003 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2019 Joyent, Inc.
 */

/*
 * ipfilter kernel module mutexes and locking:
 *
 * Enabling ipfilter creates a per-netstack ipf_stack_t object that is
 * stored in the ipf_stacks list, which is protected by ipf_stack_lock.
 * ipf_stack_t objects are accessed in three contexts:
 *
 * 1) administering that filter (eg: ioctls handled with iplioctl())
 * 2) reading log data (eg: iplread() / iplwrite())
 * 3) filtering packets (eg: ipf_hook4_* and ipf_hook6_* pfhooks
 *    functions)
 *
 * Each ipf_stack_t has a RW lock, ifs_ipf_global, protecting access to the
 * whole structure. The structure also has locks protecting the various
 * data structures used for filtering. The following guidelines should be
 * followed for ipf_stack_t locks:
 *
 * - ipf_stack_lock must be held when accessing the ipf_stacks list
 * - ipf_stack_lock should be held before acquiring ifs_ipf_global for
 *   a stack (the exception to this is ipf_stack_destroy(), which removes
 *   the ipf_stack_t from the list, then drops ipf_stack_lock before
 *   acquiring ifs_ipf_global)
 * - ifs_ipf_global must be held when accessing an ipf_stack_t in that list:
 *   - The write lock is held only during stack creation / destruction
 *   - The read lock should be held for all other accesses
 * - To alter the filtering data in the administrative context, one must:
 *   - acquire the read lock for ifs_ipf_global
 *   - then acquire the write lock for the data in question
 * - In the filtering path, the read lock needs to be held for each type of
 *   filtering data used
 * - ifs_ipf_global does not need to be held in the filtering path:
 *   - The filtering hooks don't need to modify the stack itself
 *   - The ipf_stack_t will not be destroyed until the hooks are unregistered.
 *     This requires a write lock on the hook, ensuring that no active hooks
 *     (eg: the filtering path) are running, and that the hooks won't be run
 *     afterward.
 *
 * Note that there is a deadlock possible when calling net_hook_register()
 * or net_hook_unregister() with ifs_ipf_global held: see the comments in
 * iplattach() and ipldetach() for details.
 */

#include <sys/systm.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/buf.h>
#include <sys/modctl.h>
#include <sys/open.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/cred.h>
#include <sys/dditypes.h>
#include <sys/poll.h>
#include <sys/autoconf.h>
#include <sys/byteorder.h>
#include <sys/socket.h>
#include <sys/dlpi.h>
#include <sys/stropts.h>
#include <sys/kstat.h>
#include <sys/sockio.h>
#include <sys/neti.h>
#include <sys/hook.h>
#include <net/if.h>
#if SOLARIS2 >= 6
#include <net/if_types.h>
#endif
#include <net/af.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/tcpip.h>
#include <netinet/ip_icmp.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include "netinet/ip_compat.h"
#include "netinet/ipl.h"
#include "netinet/ip_fil.h"
#include "netinet/ip_nat.h"
#include "netinet/ip_frag.h"
#include "netinet/ip_auth.h"
#include "netinet/ip_state.h"
#include "netinet/ipf_stack.h"

extern	int	iplwrite __P((dev_t, struct uio *, cred_t *));

static	int	ipf_getinfo __P((dev_info_t *, ddi_info_cmd_t,
		    void *, void **));
#if SOLARIS2 < 10
static	int	ipf_identify __P((dev_info_t *));
#endif
static	int	ipf_attach __P((dev_info_t *, ddi_attach_cmd_t));
static	int	ipf_detach __P((dev_info_t *, ddi_detach_cmd_t));
static	void	*ipf_stack_create __P((const netid_t));
static	void	ipf_stack_destroy __P((const netid_t, void *));
static	void	ipf_stack_shutdown __P((const netid_t, void *));
static	int	ipf_property_g_update __P((dev_info_t *));
static	char	*ipf_devfiles[] = { IPL_NAME, IPNAT_NAME, IPSTATE_NAME,
				    IPAUTH_NAME, IPSYNC_NAME, IPSCAN_NAME,
				    IPLOOKUP_NAME, IPFEV_NAME, NULL };
extern void 	*ipf_state;	/* DDI state */
extern vmem_t	*ipf_minor;	/* minor number arena */

static struct cb_ops ipf_cb_ops = {
	iplopen,
	iplclose,
	nodev,		/* strategy */
	nodev,		/* print */
	nodev,		/* dump */
	iplread,
	iplwrite,	/* write */
	iplioctl,	/* ioctl */
	nodev,		/* devmap */
	nodev,		/* mmap */
	nodev,		/* segmap */
	nochpoll,	/* poll */
	ddi_prop_op,
	NULL,
	D_MTSAFE,
#if SOLARIS2 > 4
	CB_REV,
	nodev,		/* aread */
	nodev,		/* awrite */
#endif
};

static struct dev_ops ipf_ops = {
	DEVO_REV,
	0,
	ipf_getinfo,
#if SOLARIS2 >= 10
	nulldev,
#else
	ipf_identify,
#endif
	nulldev,
	ipf_attach,
	ipf_detach,
	nodev,		/* reset */
	&ipf_cb_ops,
	(struct bus_ops *)0,
	NULL,
	ddi_quiesce_not_needed,		/* quiesce */
};


static net_instance_t *ipfncb = NULL;
static ipf_stack_t *ipf_stacks = NULL;
static kmutex_t ipf_stack_lock;
extern struct mod_ops mod_driverops;
static struct modldrv iplmod = {
	&mod_driverops, IPL_VERSION, &ipf_ops };
static struct modlinkage modlink1 = { MODREV_1, &iplmod, NULL };

#if SOLARIS2 >= 6
static	size_t	hdrsizes[57][2] = {
	{ 0, 0 },
	{ IFT_OTHER, 0 },
	{ IFT_1822, 0 },
	{ IFT_HDH1822, 0 },
	{ IFT_X25DDN, 0 },
	{ IFT_X25, 0 },
	{ IFT_ETHER, 14 },
	{ IFT_ISO88023, 0 },
	{ IFT_ISO88024, 0 },
	{ IFT_ISO88025, 0 },
	{ IFT_ISO88026, 0 },
	{ IFT_STARLAN, 0 },
	{ IFT_P10, 0 },
	{ IFT_P80, 0 },
	{ IFT_HY, 0 },
	{ IFT_FDDI, 24 },
	{ IFT_LAPB, 0 },
	{ IFT_SDLC, 0 },
	{ IFT_T1, 0 },
	{ IFT_CEPT, 0 },
	{ IFT_ISDNBASIC, 0 },
	{ IFT_ISDNPRIMARY, 0 },
	{ IFT_PTPSERIAL, 0 },
	{ IFT_PPP, 0 },
	{ IFT_LOOP, 0 },
	{ IFT_EON, 0 },
	{ IFT_XETHER, 0 },
	{ IFT_NSIP, 0 },
	{ IFT_SLIP, 0 },
	{ IFT_ULTRA, 0 },
	{ IFT_DS3, 0 },
	{ IFT_SIP, 0 },
	{ IFT_FRELAY, 0 },
	{ IFT_RS232, 0 },
	{ IFT_PARA, 0 },
	{ IFT_ARCNET, 0 },
	{ IFT_ARCNETPLUS, 0 },
	{ IFT_ATM, 0 },
	{ IFT_MIOX25, 0 },
	{ IFT_SONET, 0 },
	{ IFT_X25PLE, 0 },
	{ IFT_ISO88022LLC, 0 },
	{ IFT_LOCALTALK, 0 },
	{ IFT_SMDSDXI, 0 },
	{ IFT_FRELAYDCE, 0 },
	{ IFT_V35, 0 },
	{ IFT_HSSI, 0 },
	{ IFT_HIPPI, 0 },
	{ IFT_MODEM, 0 },
	{ IFT_AAL5, 0 },
	{ IFT_SONETPATH, 0 },
	{ IFT_SONETVT, 0 },
	{ IFT_SMDSICIP, 0 },
	{ IFT_PROPVIRTUAL, 0 },
	{ IFT_PROPMUX, 0 },
};
#endif /* SOLARIS2 >= 6 */

dev_info_t *ipf_dev_info = NULL;

static const filter_kstats_t ipf_kstat_tmp = {
	{ "pass",			KSTAT_DATA_ULONG },
	{ "block",			KSTAT_DATA_ULONG },
	{ "nomatch",			KSTAT_DATA_ULONG },
	{ "short",			KSTAT_DATA_ULONG },
	{ "pass, logged",		KSTAT_DATA_ULONG },
	{ "block, logged",		KSTAT_DATA_ULONG },
	{ "nomatch, logged",		KSTAT_DATA_ULONG },
	{ "logged",			KSTAT_DATA_ULONG },
	{ "skip",			KSTAT_DATA_ULONG },
	{ "return sent",		KSTAT_DATA_ULONG },
	{ "acct",			KSTAT_DATA_ULONG },
	{ "bad frag state alloc",	KSTAT_DATA_ULONG },
	{ "new frag state kept",	KSTAT_DATA_ULONG },
	{ "new frag state compl. pkt",	KSTAT_DATA_ULONG },
	{ "bad pkt state alloc",	KSTAT_DATA_ULONG },
	{ "new pkt kept state",		KSTAT_DATA_ULONG },
	{ "cachehit",			KSTAT_DATA_ULONG },
	{ "tcp cksum bad",		KSTAT_DATA_ULONG },
	{{ "pullup ok",			KSTAT_DATA_ULONG },
	{ "pullup nok",			KSTAT_DATA_ULONG }},
	{ "src != route",		KSTAT_DATA_ULONG },
	{ "ttl invalid",		KSTAT_DATA_ULONG },
	{ "bad ip pkt",			KSTAT_DATA_ULONG },
	{ "ipv6 pkt",			KSTAT_DATA_ULONG },
	{ "dropped:pps ceiling",	KSTAT_DATA_ULONG },
	{ "ip upd. fail",		KSTAT_DATA_ULONG }
};


static int	ipf_kstat_update(kstat_t *ksp, int rwflag);

static void
ipf_kstat_init(ipf_stack_t *ifs, boolean_t from_gz)
{
	ifs->ifs_kstatp[0] = net_kstat_create(ifs->ifs_netid,
	    (from_gz ? "ipf_gz" : "ipf"),
	    0, "inbound", "net", KSTAT_TYPE_NAMED,
	    sizeof (filter_kstats_t) / sizeof (kstat_named_t), 0);
	if (ifs->ifs_kstatp[0] != NULL) {
		bcopy(&ipf_kstat_tmp, ifs->ifs_kstatp[0]->ks_data,
		    sizeof (filter_kstats_t));
		ifs->ifs_kstatp[0]->ks_update = ipf_kstat_update;
		ifs->ifs_kstatp[0]->ks_private = &ifs->ifs_frstats[0];
		kstat_install(ifs->ifs_kstatp[0]);
	}

	ifs->ifs_kstatp[1] = net_kstat_create(ifs->ifs_netid,
	    (from_gz ? "ipf_gz" : "ipf"),
	    0, "outbound", "net", KSTAT_TYPE_NAMED,
	    sizeof (filter_kstats_t) / sizeof (kstat_named_t), 0);
	if (ifs->ifs_kstatp[1] != NULL) {
		bcopy(&ipf_kstat_tmp, ifs->ifs_kstatp[1]->ks_data,
		    sizeof (filter_kstats_t));
		ifs->ifs_kstatp[1]->ks_update = ipf_kstat_update;
		ifs->ifs_kstatp[1]->ks_private = &ifs->ifs_frstats[1];
		kstat_install(ifs->ifs_kstatp[1]);
	}

#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: ipf_kstat_init(%p) installed %p, %p",
	    ifs, ifs->ifs_kstatp[0], ifs->ifs_kstatp[1]);
#endif
}


static void
ipf_kstat_fini(ipf_stack_t *ifs)
{
	int i;

	for (i = 0; i < 2; i++) {
		if (ifs->ifs_kstatp[i] != NULL) {
			net_kstat_delete(ifs->ifs_netid, ifs->ifs_kstatp[i]);
			ifs->ifs_kstatp[i] = NULL;
		}
	}
}


static int
ipf_kstat_update(kstat_t *ksp, int rwflag)
{
	filter_kstats_t	*fkp;
	filterstats_t	*fsp;

	if (ksp == NULL || ksp->ks_data == NULL)
		return (EIO);

	if (rwflag == KSTAT_WRITE)
		return (EACCES);

	fkp = ksp->ks_data;
	fsp = ksp->ks_private;

	fkp->fks_pass.value.ul		= fsp->fr_pass;
	fkp->fks_block.value.ul		= fsp->fr_block;
	fkp->fks_nom.value.ul		= fsp->fr_nom;
	fkp->fks_short.value.ul		= fsp->fr_short;
	fkp->fks_ppkl.value.ul		= fsp->fr_ppkl;
	fkp->fks_bpkl.value.ul		= fsp->fr_bpkl;
	fkp->fks_npkl.value.ul		= fsp->fr_npkl;
	fkp->fks_pkl.value.ul		= fsp->fr_pkl;
	fkp->fks_skip.value.ul		= fsp->fr_skip;
	fkp->fks_ret.value.ul		= fsp->fr_ret;
	fkp->fks_acct.value.ul		= fsp->fr_acct;
	fkp->fks_bnfr.value.ul		= fsp->fr_bnfr;
	fkp->fks_nfr.value.ul		= fsp->fr_nfr;
	fkp->fks_cfr.value.ul		= fsp->fr_cfr;
	fkp->fks_bads.value.ul		= fsp->fr_bads;
	fkp->fks_ads.value.ul		= fsp->fr_ads;
	fkp->fks_chit.value.ul		= fsp->fr_chit;
	fkp->fks_tcpbad.value.ul 	= fsp->fr_tcpbad;
	fkp->fks_pull[0].value.ul 	= fsp->fr_pull[0];
	fkp->fks_pull[1].value.ul 	= fsp->fr_pull[1];
	fkp->fks_badsrc.value.ul 	= fsp->fr_badsrc;
	fkp->fks_badttl.value.ul 	= fsp->fr_badttl;
	fkp->fks_bad.value.ul		= fsp->fr_bad;
	fkp->fks_ipv6.value.ul		= fsp->fr_ipv6;
	fkp->fks_ppshit.value.ul 	= fsp->fr_ppshit;
	fkp->fks_ipud.value.ul		= fsp->fr_ipud;

	return (0);
}

int
_init()
{
	int ipfinst;

	ipfinst = mod_install(&modlink1);
#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: _init() = %d", ipfinst);
#endif
	mutex_init(&ipf_stack_lock, NULL, MUTEX_DRIVER, NULL);
	return (ipfinst);
}


int
_fini(void)
{
	int ipfinst;

	ipfinst = mod_remove(&modlink1);
#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: _fini() = %d", ipfinst);
#endif
	return (ipfinst);
}


int
_info(modinfop)
struct modinfo *modinfop;
{
	int ipfinst;

	ipfinst = mod_info(&modlink1, modinfop);
#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: _info(%p) = %d", modinfop, ipfinst);
#endif
	return (ipfinst);
}


#if SOLARIS2 < 10
static int ipf_identify(dip)
dev_info_t *dip;
{
#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: ipf_identify(%p)", dip);
#endif
	if (strcmp(ddi_get_name(dip), "ipf") == 0)
		return (DDI_IDENTIFIED);
	return (DDI_NOT_IDENTIFIED);
}
#endif

/*
 * Initialize things for IPF for each stack instance
 */
static void *
ipf_stack_create_one(const netid_t id, const zoneid_t zid, boolean_t from_gz,
    ipf_stack_t *ifs_gz)
{
	ipf_stack_t	*ifs;

#ifdef IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter:stack_create_one id=%d global=%d", id,
	    global);
#endif

	ifs = (ipf_stack_t *)kmem_alloc(sizeof (*ifs), KM_SLEEP);
	bzero(ifs, sizeof (*ifs));

	ifs->ifs_hook4_physical_in	= B_FALSE;
	ifs->ifs_hook4_physical_out	= B_FALSE;
	ifs->ifs_hook4_nic_events	= B_FALSE;
	ifs->ifs_hook4_loopback_in	= B_FALSE;
	ifs->ifs_hook4_loopback_out	= B_FALSE;
	ifs->ifs_hook6_physical_in	= B_FALSE;
	ifs->ifs_hook6_physical_out	= B_FALSE;
	ifs->ifs_hook6_nic_events	= B_FALSE;
	ifs->ifs_hook6_loopback_in	= B_FALSE;
	ifs->ifs_hook6_loopback_out	= B_FALSE;

	/*
	 * Initialize mutex's
	 */
	RWLOCK_INIT(&ifs->ifs_ipf_global, "ipf filter load/unload mutex");
	RWLOCK_INIT(&ifs->ifs_ipf_mutex, "ipf filter rwlock");
	RWLOCK_INIT(&ifs->ifs_ipf_frcache, "ipf cache rwlock");
	ifs->ifs_netid = id;
	ifs->ifs_zone = zid;
	ifs->ifs_gz_controlled = from_gz;
	ifs->ifs_gz_cont_ifs = ifs_gz;

	ipf_kstat_init(ifs, from_gz);

#ifdef IPFDEBUG
	cmn_err(CE_CONT, "IP Filter:stack_create zone=%d", ifs->ifs_zone);
#endif

	/*
	 * Lock people out while we set things up.
	 */
	WRITE_ENTER(&ifs->ifs_ipf_global);
	ipftuneable_alloc(ifs);
	RWLOCK_EXIT(&ifs->ifs_ipf_global);

	/* Limit to global stack */
	if (ifs->ifs_zone == GLOBAL_ZONEID)
		cmn_err(CE_CONT, "!%s, running.\n", ipfilter_version);

	mutex_enter(&ipf_stack_lock);
	if (ipf_stacks != NULL)
		ipf_stacks->ifs_pnext = &ifs->ifs_next;
	ifs->ifs_next = ipf_stacks;
	ifs->ifs_pnext = &ipf_stacks;
	ipf_stacks = ifs;
	mutex_exit(&ipf_stack_lock);

	return (ifs);
}

static void *
ipf_stack_create(const netid_t id)
{
	ipf_stack_t	*ifs = NULL;
	zoneid_t	zid = net_getzoneidbynetid(id);

	/*
	 * Create two ipfilter stacks for a zone - the first can only be
	 * controlled from the global zone, and the second is owned by
	 * the zone itself. There is no need to create a GZ-controlled
	 * stack for the global zone, since we're already in the global
	 * zone. See the "GZ-controlled and per-zone stacks" comment block in
	 * ip_fil_solaris.c for details.
	 */
	if (zid != GLOBAL_ZONEID)
		ifs = ipf_stack_create_one(id, zid, B_TRUE, NULL);

	return (ipf_stack_create_one(id, zid, B_FALSE, ifs));
}

/*
 * Find an ipfilter stack for the given zone. Return the GZ-controlled or
 * per-zone stack if set by an earlier SIOCIPFZONESET ioctl call. See the
 * "GZ-controlled and per-zone stacks" comment block in ip_fil_solaris.c for
 * details.
 *
 * This function returns with the ipf_stack_t's ifs_ipf_global
 * read lock held (if the stack is found). See the "ipfilter kernel module
 * mutexes and locking" comment block at the top of this file.
 */
ipf_stack_t *
ipf_find_stack(const zoneid_t orig_zone, ipf_devstate_t *isp)
{
	ipf_stack_t *ifs;
	boolean_t gz_stack;
	zoneid_t zone;

	/*
	 * If we're in the GZ, determine if we're acting on a zone's stack,
	 * and whether or not that stack is the GZ-controlled or in-zone
	 * one.  See the "GZ and per-zone stacks" note at the top of this
	 * file.
	 */
	if (orig_zone == GLOBAL_ZONEID &&
	    (isp->ipfs_zoneid != IPFS_ZONE_UNSET)) {
		/* Global zone, and we've set the zoneid for this fd already */

		if (orig_zone == isp->ipfs_zoneid) {
			/* There's only a per-zone stack for the GZ */
			gz_stack = B_FALSE;
		} else {
			gz_stack = isp->ipfs_gz;
		}

		zone = isp->ipfs_zoneid;
	} else {
		/*
		 * Non-global zone or GZ without having set a zoneid: act on
		 * the per-zone stack of the zone that this ioctl originated
		 * from.
		 */
		gz_stack = B_FALSE;
		zone = orig_zone;
	}

	mutex_enter(&ipf_stack_lock);
	for (ifs = ipf_stacks; ifs != NULL; ifs = ifs->ifs_next) {
		if (ifs->ifs_zone == zone && ifs->ifs_gz_controlled == gz_stack)
			break;
	}

	if (ifs != NULL) {
		READ_ENTER(&ifs->ifs_ipf_global);
	}
	mutex_exit(&ipf_stack_lock);
	return (ifs);
}

static int ipf_detach_check_zone(ipf_stack_t *ifs)
{
	/*
	 * Make sure we're the only one's modifying things.  With
	 * this lock others should just fall out of the loop.
	 */
	READ_ENTER(&ifs->ifs_ipf_global);
	if (ifs->ifs_fr_running == 1) {
		RWLOCK_EXIT(&ifs->ifs_ipf_global);
		return (-1);
	}

	/*
	 * Make sure there is no active filter rule.
	 */
	if (ifs->ifs_ipfilter[0][ifs->ifs_fr_active] ||
	    ifs->ifs_ipfilter[1][ifs->ifs_fr_active] ||
	    ifs->ifs_ipfilter6[0][ifs->ifs_fr_active] ||
	    ifs->ifs_ipfilter6[1][ifs->ifs_fr_active]) {
		RWLOCK_EXIT(&ifs->ifs_ipf_global);
		return (-1);
	}

	RWLOCK_EXIT(&ifs->ifs_ipf_global);

	return (0);
}


static int ipf_detach_check_all()
{
	ipf_stack_t *ifs;

	mutex_enter(&ipf_stack_lock);
	for (ifs = ipf_stacks; ifs != NULL; ifs = ifs->ifs_next)
		if (ipf_detach_check_zone(ifs) != 0)
			break;
	mutex_exit(&ipf_stack_lock);
	return ((ifs == NULL) ? 0 : -1);
}


/*
 * Remove ipf kstats for both the per-zone ipf stack and the
 * GZ-controlled stack for the same zone, if it exists.
 */
/* ARGSUSED */
static void
ipf_stack_shutdown(const netid_t id, void *arg)
{
	ipf_stack_t *ifs = (ipf_stack_t *)arg;

	/*
	 * The GZ-controlled stack
	 */
	if (ifs->ifs_gz_cont_ifs != NULL)
		ipf_kstat_fini(ifs->ifs_gz_cont_ifs);

	/*
	 * The per-zone stack
	 */
	ipf_kstat_fini(ifs);
}


/*
 * Destroy things for ipf for one stack.
 */
static void
ipf_stack_destroy_one(const netid_t id, ipf_stack_t *ifs)
{
	timeout_id_t tid;

#ifdef IPFDEBUG
	(void) printf("ipf_stack_destroy_one(%p)\n", (void *)ifs);
#endif

	/*
	 * Make sure we're the only one's modifying things.  With
	 * this lock others should just fall out of the loop.
	 */
	WRITE_ENTER(&ifs->ifs_ipf_global);
	if (ifs->ifs_fr_running == -2) {
		RWLOCK_EXIT(&ifs->ifs_ipf_global);
		return;
	}
	ifs->ifs_fr_running = -2;
	tid = ifs->ifs_fr_timer_id;
	ifs->ifs_fr_timer_id = NULL;
	RWLOCK_EXIT(&ifs->ifs_ipf_global);

	mutex_enter(&ipf_stack_lock);
	if (ifs->ifs_next != NULL)
		ifs->ifs_next->ifs_pnext = ifs->ifs_pnext;
	*ifs->ifs_pnext = ifs->ifs_next;
	mutex_exit(&ipf_stack_lock);

	if (tid != NULL)
		(void) untimeout(tid);

	WRITE_ENTER(&ifs->ifs_ipf_global);
	if (ipldetach(ifs) != 0) {
		printf("ipf_stack_destroy_one: ipldetach failed\n");
	}

	ipftuneable_free(ifs);

	RWLOCK_EXIT(&ifs->ifs_ipf_global);
	RW_DESTROY(&ifs->ifs_ipf_mutex);
	RW_DESTROY(&ifs->ifs_ipf_frcache);
	RW_DESTROY(&ifs->ifs_ipf_global);

	KFREE(ifs);
}


/*
 * Destroy things for ipf for both the per-zone ipf stack and the
 * GZ-controlled stack for the same zone, if it exists. See the "GZ-controlled
 * and per-zone stacks" comment block in ip_fil_solaris.c for details.
 */
/* ARGSUSED */
static void
ipf_stack_destroy(const netid_t id, void *arg)
{
	ipf_stack_t *ifs = (ipf_stack_t *)arg;

	/*
	 * The GZ-controlled stack
	 */
	if (ifs->ifs_gz_cont_ifs != NULL)
		ipf_stack_destroy_one(id, ifs->ifs_gz_cont_ifs);

	/*
	 * The per-zone stack
	 */
	ipf_stack_destroy_one(id, ifs);
}


static int ipf_attach(dip, cmd)
dev_info_t *dip;
ddi_attach_cmd_t cmd;
{
	char *s;
	int i;
	int instance;

#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: ipf_attach(%p,%x)", dip, cmd);
#endif

	switch (cmd)
	{
	case DDI_ATTACH:
		instance = ddi_get_instance(dip);
		/* Only one instance of ipf (instance 0) can be attached. */
		if (instance > 0)
			return (DDI_FAILURE);

#ifdef	IPFDEBUG
		cmn_err(CE_CONT, "IP Filter: attach ipf instance %d", instance);
#endif

		(void) ipf_property_g_update(dip);

		if (ddi_soft_state_init(&ipf_state, sizeof (ipf_devstate_t), 1)
		    != 0) {
			ddi_prop_remove_all(dip);
			return (DDI_FAILURE);
		}

		for (i = 0; ((s = ipf_devfiles[i]) != NULL); i++) {
			s = strrchr(s, '/');
			if (s == NULL)
				continue;
			s++;
			if (ddi_create_minor_node(dip, s, S_IFCHR, i,
			    DDI_PSEUDO, 0) == DDI_FAILURE)
				goto attach_failed;
		}

		ipf_dev_info = dip;

		if (ipf_cfw_ring_resize(IPF_CFW_RING_ALLOCATE) != 0)
			goto attach_failed;

		ipfncb = net_instance_alloc(NETINFO_VERSION);
		if (ipfncb == NULL)
			goto attach_failed;

		ipfncb->nin_name = "ipf";
		ipfncb->nin_create = ipf_stack_create;
		ipfncb->nin_destroy = ipf_stack_destroy;
		ipfncb->nin_shutdown = ipf_stack_shutdown;
		if (net_instance_register(ipfncb) == DDI_FAILURE) {
			net_instance_free(ipfncb);
			goto attach_failed;
		}

		ipf_minor = vmem_create("ipf_minor", (void *)1, UINT32_MAX - 1,
		    1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);

#ifdef IPFDEBUG
		cmn_err(CE_CONT, "IP Filter:stack_create callback_reg=%d", i);
#endif

		return (DDI_SUCCESS);
		/* NOTREACHED */
	default:
		break;
	}

attach_failed:
	(void) ipf_cfw_ring_resize(IPF_CFW_RING_DESTROY);
	ddi_remove_minor_node(dip, NULL);
	ddi_prop_remove_all(dip);
	ddi_soft_state_fini(&ipf_state);
	return (DDI_FAILURE);
}


static int ipf_detach(dip, cmd)
dev_info_t *dip;
ddi_detach_cmd_t cmd;
{
	int i;

#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: ipf_detach(%p,%x)", dip, cmd);
#endif
	switch (cmd) {
	case DDI_DETACH:
		if (ipf_detach_check_all() != 0)
			return (DDI_FAILURE);

		/*
		 * Undo what we did in ipf_attach, freeing resources
		 * and removing things we installed.  The system
		 * framework guarantees we are not active with this devinfo
		 * node in any other entry points at this time.
		 */
		(void) ipf_cfw_ring_resize(IPF_CFW_RING_DESTROY);
		ddi_prop_remove_all(dip);
		i = ddi_get_instance(dip);
		ddi_remove_minor_node(dip, NULL);
		if (i > 0) {
			cmn_err(CE_CONT, "IP Filter: still attached (%d)\n", i);
			return (DDI_FAILURE);
		}

		vmem_destroy(ipf_minor);
		ddi_soft_state_fini(&ipf_state);

		(void) net_instance_unregister(ipfncb);
		net_instance_free(ipfncb);

		return (DDI_SUCCESS);
		/* NOTREACHED */
	default:
		break;
	}
	cmn_err(CE_NOTE, "IP Filter: failed to detach\n");
	return (DDI_FAILURE);
}


/*ARGSUSED*/
static int ipf_getinfo(dip, infocmd, arg, result)
dev_info_t *dip;
ddi_info_cmd_t infocmd;
void *arg, **result;
{
	int error;

	error = DDI_FAILURE;
#ifdef	IPFDEBUG
	cmn_err(CE_NOTE, "IP Filter: ipf_getinfo(%p,%x,%p)", dip, infocmd, arg);
#endif
	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		*result = ipf_dev_info;
		error = DDI_SUCCESS;
		break;
	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)0;
		error = DDI_SUCCESS;
		break;
	default:
		break;
	}
	return (error);
}


/*
 * Fetch configuration file values that have been entered into the ipf.conf
 * driver file.
 */
static int ipf_property_g_update(dip)
dev_info_t *dip;
{
#ifdef DDI_NO_AUTODETACH
	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
				DDI_NO_AUTODETACH, 1) != DDI_PROP_SUCCESS) {
		cmn_err(CE_WARN, "!updating DDI_NO_AUTODETACH failed");
		return (DDI_FAILURE);
	}
#else
	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
				"ddi-no-autodetach", 1) != DDI_PROP_SUCCESS) {
		cmn_err(CE_WARN, "!updating ddi-no-autodetach failed");
		return (DDI_FAILURE);
	}
#endif

	return (DDI_SUCCESS);
}

int
ipf_property_update(dip, ifs)
dev_info_t *dip;
ipf_stack_t *ifs;
{
	ipftuneable_t *ipft;
	char *name;
	uint_t one;
	int *i32p;
	int err, rv = 0;

	for (ipft = ifs->ifs_ipf_tuneables;
		(name = ipft->ipft_name) != NULL; ipft++) {
		one = 1;
		i32p = NULL;
		err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
						0, name, &i32p, &one);
		if (err == DDI_PROP_NOT_FOUND)
			continue;
#ifdef	IPFDEBUG
		cmn_err(CE_CONT, "IP Filter: lookup_int(%s) = %d\n",
			name, err);
#endif
		if (err != DDI_PROP_SUCCESS) {
			rv = err;
			continue;
		}

		if (*i32p >= ipft->ipft_min &&
		    *i32p <= ipft->ipft_max) {
			if (ipft->ipft_sz == sizeof (uint32_t)) {
				*ipft->ipft_pint = *i32p;
			} else if (ipft->ipft_sz == sizeof (uint64_t)) {
				*ipft->ipft_plong = *i32p;
			}
		}

		ddi_prop_free(i32p);
	}

	return (rv);
}