summaryrefslogtreecommitdiff
path: root/libbpf/bpf_impl.c
blob: c8a250b06b2cadeebdeaa3a40364e00f2026b409 (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
 /* 
  * Mach Operating System
  * Copyright (c) 1993-1989 Carnegie Mellon University
  * All Rights Reserved.
  * 
  * Permission to use, copy, modify and distribute this software and its
  * documentation is hereby granted, provided that both the copyright
  * notice and this permission notice appear in all copies of the
  * software, derivative works or modified versions, and any portions
  * thereof, and that both notices appear in supporting documentation.
  * 
  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  * 
  * Carnegie Mellon requests users of this software to return to
  * 
  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  *  School of Computer Science
  *  Carnegie Mellon University
  *  Pittsburgh PA 15213-3890
  * 
  * any improvements or extensions that they make and grant Carnegie Mellon
  * the rights to redistribute these changes.
  */
/*
 *	Author: David B. Golub, Carnegie Mellon University
 *	Date:	3/98
 *
 *	Network IO.
 *
 *	Packet filter code taken from vaxif/enet.c written		 
 *		CMU and Stanford. 
 */

/* the code copied from device/net_io.c in Mach */

#include <arpa/inet.h>
#include <string.h>

#include <mach.h>
#include <hurd.h>

#include "bpf_impl.h"
#include "queue.h"
#include "util.h"

static struct net_hash_header filter_hash_header[N_NET_HASH];

/*
 * Execute the filter program starting at pc on the packet p
 * wirelen is the length of the original packet
 * buflen is the amount of data present
 *
 * @p: packet data.
 * @wirelen: data_count (in bytes)
 * @hlen: header len (in bytes)
 */

int
bpf_do_filter(net_rcv_port_t infp, char *p,	unsigned int wirelen, 
		char *header, unsigned int hlen, net_hash_entry_t **hash_headpp,
		net_hash_entry_t *entpp)
{
	register bpf_insn_t pc, pc_end;
	register unsigned int buflen;

	register unsigned long A, X;
	register int k;
	unsigned int mem[BPF_MEMWORDS];

	/* Generic pointer to either HEADER or P according to the specified offset. */
	char *data = NULL;

	pc = ((bpf_insn_t) infp->filter) + 1;
	/* filter[0].code is (NETF_BPF | flags) */
	pc_end = (bpf_insn_t)infp->filter_end;
	buflen = NET_RCV_MAX;
	*entpp = 0;			/* default */

	A = 0;
	X = 0;
	for (; pc < pc_end; ++pc) {
		switch (pc->code) {

			default:
				abort();
			case BPF_RET|BPF_K:
				if (infp->rcv_port == MACH_PORT_NULL &&
						*entpp == 0) {
					return 0;
				}
				return ((u_int)pc->k <= wirelen) ?
					pc->k : wirelen;

			case BPF_RET|BPF_A:
				if (infp->rcv_port == MACH_PORT_NULL &&
						*entpp == 0) {
					return 0;
				}
				return ((u_int)A <= wirelen) ?
					A : wirelen;

			case BPF_RET|BPF_MATCH_IMM:
				if (bpf_match ((net_hash_header_t)infp, pc->jt, mem,
							hash_headpp, entpp)) {
					return ((u_int)pc->k <= wirelen) ?
						pc->k : wirelen;
				}
				return 0;

			case BPF_LD|BPF_W|BPF_ABS:
				k = pc->k;

load_word:
				if ((u_int)k + sizeof(long) <= hlen)
					data = header;
				else if ((u_int)k + sizeof(long) <= buflen) {
					k -= hlen;
					data = p;
				} else
					return 0;

#ifdef BPF_ALIGN
				if (((int)(data + k) & 3) != 0)
					A = EXTRACT_LONG(&data[k]);
				else
#endif
					A = ntohl(*(long *)(data + k));
				continue;

			case BPF_LD|BPF_H|BPF_ABS:
				k = pc->k;

load_half:
				if ((u_int)k + sizeof(short) <= hlen)
					data = header;
				else if ((u_int)k + sizeof(short) <= buflen) {
					k -= hlen;
					data = p;
				} else
					return 0;

				A = EXTRACT_SHORT(&data[k]);
				continue;

			case BPF_LD|BPF_B|BPF_ABS:
				k = pc->k;

load_byte:
				if ((u_int)k < hlen)
					data = header;
				else if ((u_int)k < buflen) {
					data = p;
					k -= hlen;
				} else
					return 0;

				A = data[k];
				continue;

			case BPF_LD|BPF_W|BPF_LEN:
				A = wirelen;
				continue;

			case BPF_LDX|BPF_W|BPF_LEN:
				X = wirelen;
				continue;

			case BPF_LD|BPF_W|BPF_IND:
				k = X + pc->k;
				goto load_word;

			case BPF_LD|BPF_H|BPF_IND:
				k = X + pc->k;
				goto load_half;

			case BPF_LD|BPF_B|BPF_IND:
				k = X + pc->k;
				goto load_byte;

			case BPF_LDX|BPF_MSH|BPF_B:
				k = pc->k;
				if (k < hlen)
					data = header;
				else if (k < buflen) {
					data = p;
					k -= hlen;
				} else
					return 0;

				X = (data[k] & 0xf) << 2;
				continue;

			case BPF_LD|BPF_IMM:
				A = pc->k;
				continue;

			case BPF_LDX|BPF_IMM:
				X = pc->k;
				continue;

			case BPF_LD|BPF_MEM:
				A = mem[pc->k];
				continue;

			case BPF_LDX|BPF_MEM:
				X = mem[pc->k];
				continue;

			case BPF_ST:
				mem[pc->k] = A;
				continue;

			case BPF_STX:
				mem[pc->k] = X;
				continue;

			case BPF_JMP|BPF_JA:
				pc += pc->k;
				continue;

			case BPF_JMP|BPF_JGT|BPF_K:
				pc += (A > pc->k) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JGE|BPF_K:
				pc += (A >= pc->k) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JEQ|BPF_K:
				pc += (A == pc->k) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JSET|BPF_K:
				pc += (A & pc->k) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JGT|BPF_X:
				pc += (A > X) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JGE|BPF_X:
				pc += (A >= X) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JEQ|BPF_X:
				pc += (A == X) ? pc->jt : pc->jf;
				continue;

			case BPF_JMP|BPF_JSET|BPF_X:
				pc += (A & X) ? pc->jt : pc->jf;
				continue;

			case BPF_ALU|BPF_ADD|BPF_X:
				A += X;
				continue;

			case BPF_ALU|BPF_SUB|BPF_X:
				A -= X;
				continue;

			case BPF_ALU|BPF_MUL|BPF_X:
				A *= X;
				continue;

			case BPF_ALU|BPF_DIV|BPF_X:
				if (X == 0)
					return 0;
				A /= X;
				continue;

			case BPF_ALU|BPF_AND|BPF_X:
				A &= X;
				continue;

			case BPF_ALU|BPF_OR|BPF_X:
				A |= X;
				continue;

			case BPF_ALU|BPF_LSH|BPF_X:
				A <<= X;
				continue;

			case BPF_ALU|BPF_RSH|BPF_X:
				A >>= X;
				continue;

			case BPF_ALU|BPF_ADD|BPF_K:
				A += pc->k;
				continue;

			case BPF_ALU|BPF_SUB|BPF_K:
				A -= pc->k;
				continue;

			case BPF_ALU|BPF_MUL|BPF_K:
				A *= pc->k;
				continue;

			case BPF_ALU|BPF_DIV|BPF_K:
				A /= pc->k;
				continue;

			case BPF_ALU|BPF_AND|BPF_K:
				A &= pc->k;
				continue;

			case BPF_ALU|BPF_OR|BPF_K:
				A |= pc->k;
				continue;

			case BPF_ALU|BPF_LSH|BPF_K:
				A <<= pc->k;
				continue;

			case BPF_ALU|BPF_RSH|BPF_K:
				A >>= pc->k;
				continue;

			case BPF_ALU|BPF_NEG:
				A = -A;
				continue;

			case BPF_MISC|BPF_TAX:
				X = A;
				continue;

			case BPF_MISC|BPF_TXA:
				A = X;
				continue;
		}
	}

	return 0;
}

/*
 * Return 1 if the 'f' is a valid filter program without a MATCH
 * instruction. Return 2 if it is a valid filter program with a MATCH
 * instruction. Otherwise, return 0.
 * The constraints are that each jump be forward and to a valid
 * code.  The code must terminate with either an accept or reject. 
 * 'valid' is an array for use by the routine (it must be at least
 * 'len' bytes long).  
 *
 * The kernel needs to be able to verify an application's filter code.
 * Otherwise, a bogus program could easily crash the system.
 */
int
bpf_validate(bpf_insn_t f, int bytes, bpf_insn_t *match)
{
	register int i, j, len;
	register bpf_insn_t p;

	len = BPF_BYTES2LEN(bytes);

	/*
	 * f[0].code is already checked to be (NETF_BPF | flags).
	 * So skip f[0].
	 */

	for (i = 1; i < len; ++i) {
		/*
		 * Check that that jumps are forward, and within 
		 * the code block.
		 */
		p = &f[i];
		if (BPF_CLASS(p->code) == BPF_JMP) {
			register int from = i + 1;

			if (BPF_OP(p->code) == BPF_JA) {
				if (from + p->k >= len)
					return 0;
			}
			else if (from + p->jt >= len || from + p->jf >= len)
				return 0;
		}
		/*
		 * Check that memory operations use valid addresses.
		 */
		if ((BPF_CLASS(p->code) == BPF_ST ||
					(BPF_CLASS(p->code) == BPF_LD && 
					 (p->code & 0xe0) == BPF_MEM)) &&
				(p->k >= BPF_MEMWORDS || p->k < 0)) {
			return 0;
		}
		/*
		 * Check for constant division by 0.
		 */
		if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0) {
			return 0;
		}
		/*
		 * Check for match instruction.
		 * Only one match instruction per filter is allowed.
		 */
		if (p->code == (BPF_RET|BPF_MATCH_IMM)) {
			if (*match != 0 ||
					p->jt == 0 ||
					p->jt > N_NET_HASH_KEYS)
				return 0;
			i += p->jt;		/* skip keys */
			if (i + 1 > len)
				return 0;

			for (j = 1; j <= p->jt; j++) {
				if (p[j].code != (BPF_MISC|BPF_KEY))
					return 0;
			}

			*match = p;
		}
	}
	if (BPF_CLASS(f[len - 1].code) == BPF_RET)
		return ((*match == 0) ? 1 : 2);
	else
		return 0;
}

int
bpf_eq (bpf_insn_t f1, bpf_insn_t f2, int bytes)
{
	register int count;

	count = BPF_BYTES2LEN(bytes);
	for (; count--; f1++, f2++) {
		if (!BPF_INSN_EQ(f1, f2)) {
			if ( f1->code == (BPF_MISC|BPF_KEY) &&
					f2->code == (BPF_MISC|BPF_KEY) )
				continue;
			return FALSE;
		}
	};
	return TRUE;
}

unsigned int
bpf_hash (int n, unsigned int *keys)
{
	register unsigned int hval = 0;

	while (n--) {
		hval += *keys++;
	}
	return (hval % NET_HASH_SIZE);
}


int
bpf_match (net_hash_header_t hash, int n_keys, unsigned int *keys,
	net_hash_entry_t **hash_headpp, net_hash_entry_t *entpp)
{
	register net_hash_entry_t head, entp;
	register int i;

	if (n_keys != hash->n_keys)
		return FALSE;

	*hash_headpp = &hash->table[bpf_hash(n_keys, keys)];
	head = **hash_headpp;

	if (head == 0)
		return FALSE;

	HASH_ITERATE (head, entp)
	{
		for (i = 0; i < n_keys; i++) {
			if (keys[i] != entp->keys[i])
				break;
		}
		if (i == n_keys) {
			*entpp = entp;
			return TRUE;
		}
	}
	HASH_ITERATE_END (head, entp)
		return FALSE;
}

/*
 * Removes a hash entry (ENTP) from its queue (HEAD).
 * If the reference count of filter (HP) becomes zero and not USED,
 * HP is removed from the corresponding port lists and is freed.
 */

int
hash_ent_remove (if_filter_list_t *ifp, net_hash_header_t hp, int used,
		net_hash_entry_t *head, net_hash_entry_t entp, queue_entry_t *dead_p)
{
	hp->ref_count--;

	if (*head == entp) {
		if (queue_empty((queue_t) entp)) {
			*head = 0;
			ENQUEUE_DEAD(*dead_p, entp, chain);
			if (hp->ref_count == 0 && !used) {
				if (((net_rcv_port_t)hp)->filter[0] & NETF_IN)
					queue_remove(&ifp->if_rcv_port_list,
							(net_rcv_port_t)hp,
							net_rcv_port_t, input);
				if (((net_rcv_port_t)hp)->filter[0] & NETF_OUT)
					queue_remove(&ifp->if_snd_port_list,
							(net_rcv_port_t)hp,
							net_rcv_port_t, output);
				hp->n_keys = 0;
				return TRUE;
			}
			return FALSE;
		} else {
			*head = (net_hash_entry_t)queue_next((queue_t) entp);
		}
	}

	remqueue((queue_t)*head, (queue_entry_t)entp);
	ENQUEUE_DEAD(*dead_p, entp, chain);
	return FALSE;
}

/*
 * net_free_dead_infp (dead_infp)
 *	queue_entry_t dead_infp;	list of dead net_rcv_port_t.
 *
 * Deallocates dead net_rcv_port_t.
 * No locks should be held when called.
 */
void
net_free_dead_infp (queue_entry_t dead_infp)
{
	register net_rcv_port_t infp, nextfp;

	for (infp = (net_rcv_port_t) dead_infp; infp != 0; infp = nextfp) {
		nextfp = (net_rcv_port_t) queue_next(&infp->input);
		mach_port_deallocate(mach_task_self(), infp->rcv_port);
		free(infp);
		debug ("a dead infp is freed\n");
	}	    
}

/*
 * net_free_dead_entp (dead_entp)
 *	queue_entry_t dead_entp;	list of dead net_hash_entry_t.
 *
 * Deallocates dead net_hash_entry_t.
 * No locks should be held when called.
 */
void
net_free_dead_entp (queue_entry_t dead_entp)
{
	register net_hash_entry_t entp, nextentp;

	for (entp = (net_hash_entry_t)dead_entp; entp != 0; entp = nextentp) {
		nextentp = (net_hash_entry_t) queue_next(&entp->chain);

		mach_port_deallocate(mach_task_self(), entp->rcv_port);
		free(entp);
		debug ("a dead entp is freed\n");
	}
}

/*
 * Set a filter for a network interface.
 *
 * We are given a naked send right for the rcv_port.
 * If we are successful, we must consume that right.
 */
io_return_t
net_set_filter(if_filter_list_t *ifp, mach_port_t rcv_port, int priority,
		filter_t *filter, unsigned int filter_count)
{
	int               filter_bytes;
	bpf_insn_t            match;
	register net_rcv_port_t   infp, my_infp;
	net_rcv_port_t        nextfp;
	net_hash_header_t     hhp;
	register net_hash_entry_t entp, hash_entp=NULL;
	net_hash_entry_t      *head, nextentp;
	queue_entry_t     dead_infp, dead_entp;
	int               i;
	int               ret, is_new_infp;
	io_return_t           rval;
	boolean_t         in, out;

	/* Check the filter syntax. */

	debug ("filter_count: %d, filter[0]: %d\n", filter_count, filter[0]);

	filter_bytes = CSPF_BYTES (filter_count);
	match = (bpf_insn_t) 0;

	if (filter_count == 0) {
		return (D_INVALID_OPERATION);
	} else if (!((filter[0] & NETF_IN) || (filter[0] & NETF_OUT))) {
		return (D_INVALID_OPERATION); /* NETF_IN or NETF_OUT required */
	} else if ((filter[0] & NETF_TYPE_MASK) == NETF_BPF) {
		ret = bpf_validate((bpf_insn_t)filter, filter_bytes, &match);
		if (!ret)
			return (D_INVALID_OPERATION);
	} else {
		return (D_INVALID_OPERATION);
	}
	debug ("net_set_filter: check over\n");

	rval = D_SUCCESS;         /* default return value */
	dead_infp = dead_entp = 0;

	if (match == (bpf_insn_t) 0) {
		/*
		 * If there is no match instruction, we allocate
		 * a normal packet filter structure.
		 */
		my_infp = (net_rcv_port_t) calloc(1, sizeof(struct net_rcv_port));
		my_infp->rcv_port = rcv_port;
		is_new_infp = TRUE;
	} else {
		/*
		 * If there is a match instruction, we assume there will be
		 * multiple sessions with a common substructure and allocate
		 * a hash table to deal with them.
		 */
		my_infp = 0;
		hash_entp = (net_hash_entry_t) calloc(1, sizeof(struct net_hash_entry));
		is_new_infp = FALSE;
	}

	/*
	 * Look for an existing filter on the same reply port.
	 * Look for filters with dead ports (for GC).
	 * Look for a filter with the same code except KEY insns.
	 */
	void check_filter_list(queue_head_t *if_port_list)
	{
		FILTER_ITERATE(if_port_list, infp, nextfp,
				(if_port_list == &ifp->if_rcv_port_list)
				? &infp->input : &infp->output)
		{
			if (infp->rcv_port == MACH_PORT_NULL) {
				if (match != 0
						&& infp->priority == priority
						&& my_infp == 0
						&& (infp->filter_end - infp->filter) == filter_count
						&& bpf_eq((bpf_insn_t)infp->filter,
							(bpf_insn_t)filter, filter_bytes))
					my_infp = infp;

				for (i = 0; i < NET_HASH_SIZE; i++) {
					head = &((net_hash_header_t) infp)->table[i];
					if (*head == 0)
						continue;

					/*
					 * Check each hash entry to make sure the
					 * destination port is still valid.  Remove
					 * any invalid entries.
					 */
					entp = *head;
					do {
						nextentp = (net_hash_entry_t) entp->he_next;

						/* checked without 
						   ip_lock(entp->rcv_port) */
						if (entp->rcv_port == rcv_port) {
							ret = hash_ent_remove (ifp,
									(net_hash_header_t)infp,
									(my_infp == infp),
									head,
									entp,
									&dead_entp);
							if (ret)
								goto hash_loop_end;
						}

						entp = nextentp;
						/* While test checks head since hash_ent_remove
						 * might modify it.
						 */
					} while (*head != 0 && entp != *head);
				}

hash_loop_end:
				;
			} else if (infp->rcv_port == rcv_port) {

				/* Remove the old filter from lists */
				if (infp->filter[0] & NETF_IN)
					queue_remove(&ifp->if_rcv_port_list, infp,
							net_rcv_port_t, input);
				if (infp->filter[0] & NETF_OUT)
					queue_remove(&ifp->if_snd_port_list, infp,
							net_rcv_port_t, output);

				ENQUEUE_DEAD(dead_infp, infp, input);
			}
		}
		FILTER_ITERATE_END
	}

	in = (filter[0] & NETF_IN) != 0;
	out = (filter[0] & NETF_OUT) != 0;

	if (in)
		check_filter_list(&ifp->if_rcv_port_list);
	if (out)
		check_filter_list(&ifp->if_snd_port_list);

	if (my_infp == 0) {
		/* Allocate a dummy infp */
		for (i = 0; i < N_NET_HASH; i++) {
			if (filter_hash_header[i].n_keys == 0)
				break;
		}
		if (i == N_NET_HASH) {
			mach_port_deallocate(mach_task_self() , rcv_port);
			if (match != 0)
				free(hash_entp);

			rval = D_NO_MEMORY;
			goto clean_and_return;
		}

		hhp = &filter_hash_header[i];
		hhp->n_keys = match->jt;

		hhp->ref_count = 0;
		for (i = 0; i < NET_HASH_SIZE; i++)
			hhp->table[i] = 0;

		my_infp = (net_rcv_port_t)hhp;
		my_infp->rcv_port = MACH_PORT_NULL; /* indication of dummy */
		is_new_infp = TRUE;
	}

	if (is_new_infp) {
		my_infp->priority = priority;
		my_infp->rcv_count = 0;

		/* Copy filter program. */
		memcpy (my_infp->filter, filter, filter_bytes);
		my_infp->filter_end =
			(filter_t *)((char *)my_infp->filter + filter_bytes);

		/* Insert my_infp according to priority */
		if (in) {
			queue_iterate(&ifp->if_rcv_port_list, infp, net_rcv_port_t, input)
				if (priority > infp->priority)
					break;

			queue_enter(&ifp->if_rcv_port_list, my_infp, net_rcv_port_t, input);
		}

		if (out) {
			queue_iterate(&ifp->if_snd_port_list, infp, net_rcv_port_t, output)
				if (priority > infp->priority)
					break;

			queue_enter(&ifp->if_snd_port_list, my_infp, net_rcv_port_t, output);
		}
	}

	if (match != 0)
	{
		/* Insert to hash list */
		net_hash_entry_t *p;

		hash_entp->rcv_port = rcv_port;
		for (i = 0; i < match->jt; i++)     /* match->jt is n_keys */
			hash_entp->keys[i] = match[i+1].k;
		p = &((net_hash_header_t)my_infp)->
			table[bpf_hash(match->jt, hash_entp->keys)];

		/* Not checking for the same key values */
		if (*p == 0) {
			queue_init ((queue_t) hash_entp);
			*p = hash_entp;
		} else {
			enqueue_tail((queue_t)*p, (queue_entry_t)hash_entp);
		}

		((net_hash_header_t)my_infp)->ref_count++;
	}

clean_and_return:
	/* No locks are held at this point. */

	if (dead_infp != 0)
		net_free_dead_infp(dead_infp);
	if (dead_entp != 0)
		net_free_dead_entp(dead_entp);

	return (rval);
}

void
destroy_filters (if_filter_list_t *ifp)
{
}

void
remove_dead_filter (if_filter_list_t *ifp, queue_head_t *if_port_list,
		mach_port_t dead_port)
{
	net_rcv_port_t infp;
	net_rcv_port_t nextfp;
	net_hash_entry_t *head, nextentp;
	queue_entry_t dead_infp, dead_entp;
	net_hash_entry_t entp = NULL;
	int i, ret;

	dead_infp = dead_entp = 0;
	FILTER_ITERATE (if_port_list, infp, nextfp,
			(if_port_list == &ifp->if_rcv_port_list)
			? &infp->input : &infp->output) {
		if (infp->rcv_port == MACH_PORT_NULL) {
			for (i = 0; i < NET_HASH_SIZE; i++) {
				head = &((net_hash_header_t) infp)->table[i];
				if (*head == 0)
					continue;

				/*
				 * Check each hash entry to make sure the
				 * destination port is still valid.  Remove
				 * any invalid entries.
				 */
				entp = *head;
				do {
					nextentp = (net_hash_entry_t) entp->he_next;

					/* checked without 
					   ip_lock(entp->rcv_port) */
					if (entp->rcv_port == dead_port) {
						ret = hash_ent_remove (ifp,
								(net_hash_header_t) infp,
								0,
								head,
								entp,
								&dead_entp);
						if (ret)
							goto hash_loop_end;
					}

					entp = nextentp;
					/* While test checks head since hash_ent_remove
					 * might modify it.
					 */
				} while (*head != 0 && entp != *head);
			}

hash_loop_end:
			;
		} else if (infp->rcv_port == dead_port) {
			/* Remove the old filter from lists */
			if (infp->filter[0] & NETF_IN)
				queue_remove(&ifp->if_rcv_port_list, infp,
						net_rcv_port_t, input);
			if (infp->filter[0] & NETF_OUT)
				queue_remove(&ifp->if_snd_port_list, infp,
						net_rcv_port_t, output);

			ENQUEUE_DEAD(dead_infp, infp, input);
		}
	}
	FILTER_ITERATE_END

	if (dead_infp != 0)
		net_free_dead_infp(dead_infp);
	if (dead_entp != 0)
		net_free_dead_entp(dead_entp);
}