xref: /freebsd-13-stable/sys/net/altq/altq_subr.c (revision f7bcf5725fee3b65be9c7ba9cb201b2e469ae504)
1 /*-
2  * Copyright (C) 1997-2003
3  *	Sony Computer Science Laboratories Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $KAME: altq_subr.c,v 1.21 2003/11/06 06:32:53 kjc Exp $
27  */
28 
29 #include "opt_altq.h"
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/kernel.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/sysctl.h>
44 #include <sys/queue.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/vnet.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #ifdef INET6
56 #include <netinet/ip6.h>
57 #endif
58 #include <netinet/tcp.h>
59 #include <netinet/udp.h>
60 
61 #include <netpfil/pf/pf.h>
62 #include <netpfil/pf/pf_altq.h>
63 #include <net/altq/altq.h>
64 
65 /* machine dependent clock related includes */
66 #include <sys/bus.h>
67 #include <sys/cpu.h>
68 #include <sys/eventhandler.h>
69 #include <machine/clock.h>
70 #if defined(__amd64__) || defined(__i386__)
71 #include <machine/cpufunc.h>		/* for pentium tsc */
72 #include <machine/specialreg.h>		/* for CPUID_TSC */
73 #include <machine/md_var.h>		/* for cpu_feature */
74 #endif /* __amd64 || __i386__ */
75 
76 /*
77  * internal function prototypes
78  */
79 static void	tbr_timeout(void *);
80 int (*altq_input)(struct mbuf *, int) = NULL;
81 static struct mbuf *tbr_dequeue(struct ifaltq *, int);
82 static int tbr_timer = 0;	/* token bucket regulator timer */
83 #if !defined(__FreeBSD__) || (__FreeBSD_version < 600000)
84 static struct callout tbr_callout = CALLOUT_INITIALIZER;
85 #else
86 static struct callout tbr_callout;
87 #endif
88 
89 #ifdef ALTQ3_CLFIER_COMPAT
90 static int 	extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
91 #ifdef INET6
92 static int 	extract_ports6(struct mbuf *, struct ip6_hdr *,
93 			       struct flowinfo_in6 *);
94 #endif
95 static int	apply_filter4(u_int32_t, struct flow_filter *,
96 			      struct flowinfo_in *);
97 static int	apply_ppfilter4(u_int32_t, struct flow_filter *,
98 				struct flowinfo_in *);
99 #ifdef INET6
100 static int	apply_filter6(u_int32_t, struct flow_filter6 *,
101 			      struct flowinfo_in6 *);
102 #endif
103 static int	apply_tosfilter4(u_int32_t, struct flow_filter *,
104 				 struct flowinfo_in *);
105 static u_long	get_filt_handle(struct acc_classifier *, int);
106 static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
107 static u_int32_t filt2fibmask(struct flow_filter *);
108 
109 static void 	ip4f_cache(struct ip *, struct flowinfo_in *);
110 static int 	ip4f_lookup(struct ip *, struct flowinfo_in *);
111 static int 	ip4f_init(void);
112 static struct ip4_frag	*ip4f_alloc(void);
113 static void 	ip4f_free(struct ip4_frag *);
114 #endif /* ALTQ3_CLFIER_COMPAT */
115 
116 #ifdef ALTQ
117 SYSCTL_NODE(_kern_features, OID_AUTO, altq, CTLFLAG_RD | CTLFLAG_CAPRD, 0,
118     "ALTQ packet queuing");
119 
120 #define	ALTQ_FEATURE(name, desc)					\
121 	SYSCTL_INT_WITH_LABEL(_kern_features_altq, OID_AUTO, name,	\
122 	    CTLFLAG_RD | CTLFLAG_CAPRD, SYSCTL_NULL_INT_PTR, 1,		\
123 	    desc, "feature")
124 
125 #ifdef ALTQ_CBQ
126 ALTQ_FEATURE(cbq, "ALTQ Class Based Queuing discipline");
127 #endif
128 #ifdef ALTQ_CODEL
129 ALTQ_FEATURE(codel, "ALTQ Controlled Delay discipline");
130 #endif
131 #ifdef ALTQ_RED
132 ALTQ_FEATURE(red, "ALTQ Random Early Detection discipline");
133 #endif
134 #ifdef ALTQ_RIO
135 ALTQ_FEATURE(rio, "ALTQ Random Early Drop discipline");
136 #endif
137 #ifdef ALTQ_HFSC
138 ALTQ_FEATURE(hfsc, "ALTQ Hierarchical Packet Scheduler discipline");
139 #endif
140 #ifdef ALTQ_PRIQ
141 ALTQ_FEATURE(priq, "ATLQ Priority Queuing discipline");
142 #endif
143 #ifdef ALTQ_FAIRQ
144 ALTQ_FEATURE(fairq, "ALTQ Fair Queuing discipline");
145 #endif
146 #endif
147 
148 /*
149  * alternate queueing support routines
150  */
151 
152 /* look up the queue state by the interface name and the queueing type. */
153 void *
altq_lookup(name,type)154 altq_lookup(name, type)
155 	char *name;
156 	int type;
157 {
158 	struct ifnet *ifp;
159 
160 	if ((ifp = ifunit(name)) != NULL) {
161 		/* read if_snd unlocked */
162 		if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
163 			return (ifp->if_snd.altq_disc);
164 	}
165 
166 	return NULL;
167 }
168 
169 int
altq_attach(ifq,type,discipline,enqueue,dequeue,request,clfier,classify)170 altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
171 	struct ifaltq *ifq;
172 	int type;
173 	void *discipline;
174 	int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
175 	struct mbuf *(*dequeue)(struct ifaltq *, int);
176 	int (*request)(struct ifaltq *, int, void *);
177 	void *clfier;
178 	void *(*classify)(void *, struct mbuf *, int);
179 {
180 	IFQ_LOCK(ifq);
181 	if (!ALTQ_IS_READY(ifq)) {
182 		IFQ_UNLOCK(ifq);
183 		return ENXIO;
184 	}
185 
186 	ifq->altq_type     = type;
187 	ifq->altq_disc     = discipline;
188 	ifq->altq_enqueue  = enqueue;
189 	ifq->altq_dequeue  = dequeue;
190 	ifq->altq_request  = request;
191 	ifq->altq_clfier   = clfier;
192 	ifq->altq_classify = classify;
193 	ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
194 	IFQ_UNLOCK(ifq);
195 	return 0;
196 }
197 
198 int
altq_detach(ifq)199 altq_detach(ifq)
200 	struct ifaltq *ifq;
201 {
202 	IFQ_LOCK(ifq);
203 
204 	if (!ALTQ_IS_READY(ifq)) {
205 		IFQ_UNLOCK(ifq);
206 		return ENXIO;
207 	}
208 	if (ALTQ_IS_ENABLED(ifq)) {
209 		IFQ_UNLOCK(ifq);
210 		return EBUSY;
211 	}
212 	if (!ALTQ_IS_ATTACHED(ifq)) {
213 		IFQ_UNLOCK(ifq);
214 		return (0);
215 	}
216 
217 	ifq->altq_type     = ALTQT_NONE;
218 	ifq->altq_disc     = NULL;
219 	ifq->altq_enqueue  = NULL;
220 	ifq->altq_dequeue  = NULL;
221 	ifq->altq_request  = NULL;
222 	ifq->altq_clfier   = NULL;
223 	ifq->altq_classify = NULL;
224 	ifq->altq_flags &= ALTQF_CANTCHANGE;
225 
226 	IFQ_UNLOCK(ifq);
227 	return 0;
228 }
229 
230 int
altq_enable(ifq)231 altq_enable(ifq)
232 	struct ifaltq *ifq;
233 {
234 	int s;
235 
236 	IFQ_LOCK(ifq);
237 
238 	if (!ALTQ_IS_READY(ifq)) {
239 		IFQ_UNLOCK(ifq);
240 		return ENXIO;
241 	}
242 	if (ALTQ_IS_ENABLED(ifq)) {
243 		IFQ_UNLOCK(ifq);
244 		return 0;
245 	}
246 
247 	s = splnet();
248 	IFQ_PURGE_NOLOCK(ifq);
249 	ASSERT(ifq->ifq_len == 0);
250 	ifq->ifq_drv_maxlen = 0;		/* disable bulk dequeue */
251 	ifq->altq_flags |= ALTQF_ENABLED;
252 	if (ifq->altq_clfier != NULL)
253 		ifq->altq_flags |= ALTQF_CLASSIFY;
254 	splx(s);
255 
256 	IFQ_UNLOCK(ifq);
257 	return 0;
258 }
259 
260 int
altq_disable(ifq)261 altq_disable(ifq)
262 	struct ifaltq *ifq;
263 {
264 	int s;
265 
266 	IFQ_LOCK(ifq);
267 	if (!ALTQ_IS_ENABLED(ifq)) {
268 		IFQ_UNLOCK(ifq);
269 		return 0;
270 	}
271 
272 	s = splnet();
273 	IFQ_PURGE_NOLOCK(ifq);
274 	ASSERT(ifq->ifq_len == 0);
275 	ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
276 	splx(s);
277 
278 	IFQ_UNLOCK(ifq);
279 	return 0;
280 }
281 
282 #ifdef ALTQ_DEBUG
283 void
altq_assert(file,line,failedexpr)284 altq_assert(file, line, failedexpr)
285 	const char *file, *failedexpr;
286 	int line;
287 {
288 	(void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
289 		     failedexpr, file, line);
290 	panic("altq assertion");
291 	/* NOTREACHED */
292 }
293 #endif
294 
295 /*
296  * internal representation of token bucket parameters
297  *	rate:	(byte_per_unittime << TBR_SHIFT)  / machclk_freq
298  *		(((bits_per_sec) / 8) << TBR_SHIFT) / machclk_freq
299  *	depth:	byte << TBR_SHIFT
300  *
301  */
302 #define	TBR_SHIFT	29
303 #define	TBR_SCALE(x)	((int64_t)(x) << TBR_SHIFT)
304 #define	TBR_UNSCALE(x)	((x) >> TBR_SHIFT)
305 
306 static struct mbuf *
tbr_dequeue(ifq,op)307 tbr_dequeue(ifq, op)
308 	struct ifaltq *ifq;
309 	int op;
310 {
311 	struct tb_regulator *tbr;
312 	struct mbuf *m;
313 	int64_t interval;
314 	u_int64_t now;
315 
316 	IFQ_LOCK_ASSERT(ifq);
317 	tbr = ifq->altq_tbr;
318 	if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
319 		/* if this is a remove after poll, bypass tbr check */
320 	} else {
321 		/* update token only when it is negative */
322 		if (tbr->tbr_token <= 0) {
323 			now = read_machclk();
324 			interval = now - tbr->tbr_last;
325 			if (interval >= tbr->tbr_filluptime)
326 				tbr->tbr_token = tbr->tbr_depth;
327 			else {
328 				tbr->tbr_token += interval * tbr->tbr_rate;
329 				if (tbr->tbr_token > tbr->tbr_depth)
330 					tbr->tbr_token = tbr->tbr_depth;
331 			}
332 			tbr->tbr_last = now;
333 		}
334 		/* if token is still negative, don't allow dequeue */
335 		if (tbr->tbr_token <= 0)
336 			return (NULL);
337 	}
338 
339 	if (ALTQ_IS_ENABLED(ifq))
340 		m = (*ifq->altq_dequeue)(ifq, op);
341 	else {
342 		if (op == ALTDQ_POLL)
343 			_IF_POLL(ifq, m);
344 		else
345 			_IF_DEQUEUE(ifq, m);
346 	}
347 
348 	if (m != NULL && op == ALTDQ_REMOVE)
349 		tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
350 	tbr->tbr_lastop = op;
351 	return (m);
352 }
353 
354 /*
355  * set a token bucket regulator.
356  * if the specified rate is zero, the token bucket regulator is deleted.
357  */
358 int
tbr_set(ifq,profile)359 tbr_set(ifq, profile)
360 	struct ifaltq *ifq;
361 	struct tb_profile *profile;
362 {
363 	struct tb_regulator *tbr, *otbr;
364 
365 	if (tbr_dequeue_ptr == NULL)
366 		tbr_dequeue_ptr = tbr_dequeue;
367 
368 	if (machclk_freq == 0)
369 		init_machclk();
370 	if (machclk_freq == 0) {
371 		printf("tbr_set: no cpu clock available!\n");
372 		return (ENXIO);
373 	}
374 
375 	IFQ_LOCK(ifq);
376 	if (profile->rate == 0) {
377 		/* delete this tbr */
378 		if ((tbr = ifq->altq_tbr) == NULL) {
379 			IFQ_UNLOCK(ifq);
380 			return (ENOENT);
381 		}
382 		ifq->altq_tbr = NULL;
383 		free(tbr, M_DEVBUF);
384 		IFQ_UNLOCK(ifq);
385 		return (0);
386 	}
387 
388 	tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_NOWAIT | M_ZERO);
389 	if (tbr == NULL) {
390 		IFQ_UNLOCK(ifq);
391 		return (ENOMEM);
392 	}
393 
394 	tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
395 	tbr->tbr_depth = TBR_SCALE(profile->depth);
396 	if (tbr->tbr_rate > 0)
397 		tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
398 	else
399 		tbr->tbr_filluptime = LLONG_MAX;
400 	/*
401 	 *  The longest time between tbr_dequeue() calls will be about 1
402 	 *  system tick, as the callout that drives it is scheduled once per
403 	 *  tick.  The refill-time detection logic in tbr_dequeue() can only
404 	 *  properly detect the passage of up to LLONG_MAX machclk ticks.
405 	 *  Therefore, in order for this logic to function properly in the
406 	 *  extreme case, the maximum value of tbr_filluptime should be
407 	 *  LLONG_MAX less one system tick's worth of machclk ticks less
408 	 *  some additional slop factor (here one more system tick's worth
409 	 *  of machclk ticks).
410 	 */
411 	if (tbr->tbr_filluptime > (LLONG_MAX - 2 * machclk_per_tick))
412 		tbr->tbr_filluptime = LLONG_MAX - 2 * machclk_per_tick;
413 	tbr->tbr_token = tbr->tbr_depth;
414 	tbr->tbr_last = read_machclk();
415 	tbr->tbr_lastop = ALTDQ_REMOVE;
416 
417 	otbr = ifq->altq_tbr;
418 	ifq->altq_tbr = tbr;	/* set the new tbr */
419 
420 	if (otbr != NULL)
421 		free(otbr, M_DEVBUF);
422 	else {
423 		if (tbr_timer == 0) {
424 			CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
425 			tbr_timer = 1;
426 		}
427 	}
428 	IFQ_UNLOCK(ifq);
429 	return (0);
430 }
431 
432 /*
433  * tbr_timeout goes through the interface list, and kicks the drivers
434  * if necessary.
435  *
436  * MPSAFE
437  */
438 static void
tbr_timeout(arg)439 tbr_timeout(arg)
440 	void *arg;
441 {
442 	VNET_ITERATOR_DECL(vnet_iter);
443 	struct ifnet *ifp;
444 	struct epoch_tracker et;
445 	int active;
446 
447 	active = 0;
448 	NET_EPOCH_ENTER(et);
449 	VNET_LIST_RLOCK_NOSLEEP();
450 	VNET_FOREACH(vnet_iter) {
451 		CURVNET_SET(vnet_iter);
452 		for (ifp = CK_STAILQ_FIRST(&V_ifnet); ifp;
453 		    ifp = CK_STAILQ_NEXT(ifp, if_link)) {
454 			/* read from if_snd unlocked */
455 			if (!TBR_IS_ENABLED(&ifp->if_snd))
456 				continue;
457 			active++;
458 			if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
459 			    ifp->if_start != NULL)
460 				(*ifp->if_start)(ifp);
461 		}
462 		CURVNET_RESTORE();
463 	}
464 	VNET_LIST_RUNLOCK_NOSLEEP();
465 	NET_EPOCH_EXIT(et);
466 	if (active > 0)
467 		CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
468 	else
469 		tbr_timer = 0;	/* don't need tbr_timer anymore */
470 }
471 
472 /*
473  * attach a discipline to the interface.  if one already exists, it is
474  * overridden.
475  * Locking is done in the discipline specific attach functions. Basically
476  * they call back to altq_attach which takes care of the attach and locking.
477  */
478 int
altq_pfattach(struct pf_altq * a)479 altq_pfattach(struct pf_altq *a)
480 {
481 	int error = 0;
482 
483 	switch (a->scheduler) {
484 	case ALTQT_NONE:
485 		break;
486 #ifdef ALTQ_CBQ
487 	case ALTQT_CBQ:
488 		error = cbq_pfattach(a);
489 		break;
490 #endif
491 #ifdef ALTQ_PRIQ
492 	case ALTQT_PRIQ:
493 		error = priq_pfattach(a);
494 		break;
495 #endif
496 #ifdef ALTQ_HFSC
497 	case ALTQT_HFSC:
498 		error = hfsc_pfattach(a);
499 		break;
500 #endif
501 #ifdef ALTQ_FAIRQ
502 	case ALTQT_FAIRQ:
503 		error = fairq_pfattach(a);
504 		break;
505 #endif
506 #ifdef ALTQ_CODEL
507 	case ALTQT_CODEL:
508 		error = codel_pfattach(a);
509 		break;
510 #endif
511 	default:
512 		error = ENXIO;
513 	}
514 
515 	return (error);
516 }
517 
518 /*
519  * detach a discipline from the interface.
520  * it is possible that the discipline was already overridden by another
521  * discipline.
522  */
523 int
altq_pfdetach(struct pf_altq * a)524 altq_pfdetach(struct pf_altq *a)
525 {
526 	struct ifnet *ifp;
527 	int s, error = 0;
528 
529 	if ((ifp = ifunit(a->ifname)) == NULL)
530 		return (EINVAL);
531 
532 	/* if this discipline is no longer referenced, just return */
533 	/* read unlocked from if_snd */
534 	if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
535 		return (0);
536 
537 	s = splnet();
538 	/* read unlocked from if_snd, _disable and _detach take care */
539 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
540 		error = altq_disable(&ifp->if_snd);
541 	if (error == 0)
542 		error = altq_detach(&ifp->if_snd);
543 	splx(s);
544 
545 	return (error);
546 }
547 
548 /*
549  * add a discipline or a queue
550  * Locking is done in the discipline specific functions with regards to
551  * malloc with WAITOK, also it is not yet clear which lock to use.
552  */
553 int
altq_add(struct ifnet * ifp,struct pf_altq * a)554 altq_add(struct ifnet *ifp, struct pf_altq *a)
555 {
556 	int error = 0;
557 
558 	if (a->qname[0] != 0)
559 		return (altq_add_queue(a));
560 
561 	if (machclk_freq == 0)
562 		init_machclk();
563 	if (machclk_freq == 0)
564 		panic("altq_add: no cpu clock");
565 
566 	switch (a->scheduler) {
567 #ifdef ALTQ_CBQ
568 	case ALTQT_CBQ:
569 		error = cbq_add_altq(ifp, a);
570 		break;
571 #endif
572 #ifdef ALTQ_PRIQ
573 	case ALTQT_PRIQ:
574 		error = priq_add_altq(ifp, a);
575 		break;
576 #endif
577 #ifdef ALTQ_HFSC
578 	case ALTQT_HFSC:
579 		error = hfsc_add_altq(ifp, a);
580 		break;
581 #endif
582 #ifdef ALTQ_FAIRQ
583         case ALTQT_FAIRQ:
584                 error = fairq_add_altq(ifp, a);
585                 break;
586 #endif
587 #ifdef ALTQ_CODEL
588 	case ALTQT_CODEL:
589 		error = codel_add_altq(ifp, a);
590 		break;
591 #endif
592 	default:
593 		error = ENXIO;
594 	}
595 
596 	return (error);
597 }
598 
599 /*
600  * remove a discipline or a queue
601  * It is yet unclear what lock to use to protect this operation, the
602  * discipline specific functions will determine and grab it
603  */
604 int
altq_remove(struct pf_altq * a)605 altq_remove(struct pf_altq *a)
606 {
607 	int error = 0;
608 
609 	if (a->qname[0] != 0)
610 		return (altq_remove_queue(a));
611 
612 	switch (a->scheduler) {
613 #ifdef ALTQ_CBQ
614 	case ALTQT_CBQ:
615 		error = cbq_remove_altq(a);
616 		break;
617 #endif
618 #ifdef ALTQ_PRIQ
619 	case ALTQT_PRIQ:
620 		error = priq_remove_altq(a);
621 		break;
622 #endif
623 #ifdef ALTQ_HFSC
624 	case ALTQT_HFSC:
625 		error = hfsc_remove_altq(a);
626 		break;
627 #endif
628 #ifdef ALTQ_FAIRQ
629         case ALTQT_FAIRQ:
630                 error = fairq_remove_altq(a);
631                 break;
632 #endif
633 #ifdef ALTQ_CODEL
634 	case ALTQT_CODEL:
635 		error = codel_remove_altq(a);
636 		break;
637 #endif
638 	default:
639 		error = ENXIO;
640 	}
641 
642 	return (error);
643 }
644 
645 /*
646  * add a queue to the discipline
647  * It is yet unclear what lock to use to protect this operation, the
648  * discipline specific functions will determine and grab it
649  */
650 int
altq_add_queue(struct pf_altq * a)651 altq_add_queue(struct pf_altq *a)
652 {
653 	int error = 0;
654 
655 	switch (a->scheduler) {
656 #ifdef ALTQ_CBQ
657 	case ALTQT_CBQ:
658 		error = cbq_add_queue(a);
659 		break;
660 #endif
661 #ifdef ALTQ_PRIQ
662 	case ALTQT_PRIQ:
663 		error = priq_add_queue(a);
664 		break;
665 #endif
666 #ifdef ALTQ_HFSC
667 	case ALTQT_HFSC:
668 		error = hfsc_add_queue(a);
669 		break;
670 #endif
671 #ifdef ALTQ_FAIRQ
672         case ALTQT_FAIRQ:
673                 error = fairq_add_queue(a);
674                 break;
675 #endif
676 	default:
677 		error = ENXIO;
678 	}
679 
680 	return (error);
681 }
682 
683 /*
684  * remove a queue from the discipline
685  * It is yet unclear what lock to use to protect this operation, the
686  * discipline specific functions will determine and grab it
687  */
688 int
altq_remove_queue(struct pf_altq * a)689 altq_remove_queue(struct pf_altq *a)
690 {
691 	int error = 0;
692 
693 	switch (a->scheduler) {
694 #ifdef ALTQ_CBQ
695 	case ALTQT_CBQ:
696 		error = cbq_remove_queue(a);
697 		break;
698 #endif
699 #ifdef ALTQ_PRIQ
700 	case ALTQT_PRIQ:
701 		error = priq_remove_queue(a);
702 		break;
703 #endif
704 #ifdef ALTQ_HFSC
705 	case ALTQT_HFSC:
706 		error = hfsc_remove_queue(a);
707 		break;
708 #endif
709 #ifdef ALTQ_FAIRQ
710         case ALTQT_FAIRQ:
711                 error = fairq_remove_queue(a);
712                 break;
713 #endif
714 	default:
715 		error = ENXIO;
716 	}
717 
718 	return (error);
719 }
720 
721 /*
722  * get queue statistics
723  * Locking is done in the discipline specific functions with regards to
724  * copyout operations, also it is not yet clear which lock to use.
725  */
726 int
altq_getqstats(struct pf_altq * a,void * ubuf,int * nbytes,int version)727 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
728 {
729 	int error = 0;
730 
731 	switch (a->scheduler) {
732 #ifdef ALTQ_CBQ
733 	case ALTQT_CBQ:
734 		error = cbq_getqstats(a, ubuf, nbytes, version);
735 		break;
736 #endif
737 #ifdef ALTQ_PRIQ
738 	case ALTQT_PRIQ:
739 		error = priq_getqstats(a, ubuf, nbytes, version);
740 		break;
741 #endif
742 #ifdef ALTQ_HFSC
743 	case ALTQT_HFSC:
744 		error = hfsc_getqstats(a, ubuf, nbytes, version);
745 		break;
746 #endif
747 #ifdef ALTQ_FAIRQ
748         case ALTQT_FAIRQ:
749                 error = fairq_getqstats(a, ubuf, nbytes, version);
750                 break;
751 #endif
752 #ifdef ALTQ_CODEL
753 	case ALTQT_CODEL:
754 		error = codel_getqstats(a, ubuf, nbytes, version);
755 		break;
756 #endif
757 	default:
758 		error = ENXIO;
759 	}
760 
761 	return (error);
762 }
763 
764 /*
765  * read and write diffserv field in IPv4 or IPv6 header
766  */
767 u_int8_t
read_dsfield(m,pktattr)768 read_dsfield(m, pktattr)
769 	struct mbuf *m;
770 	struct altq_pktattr *pktattr;
771 {
772 	struct mbuf *m0;
773 	u_int8_t ds_field = 0;
774 
775 	if (pktattr == NULL ||
776 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
777 		return ((u_int8_t)0);
778 
779 	/* verify that pattr_hdr is within the mbuf data */
780 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
781 		if ((pktattr->pattr_hdr >= m0->m_data) &&
782 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
783 			break;
784 	if (m0 == NULL) {
785 		/* ick, pattr_hdr is stale */
786 		pktattr->pattr_af = AF_UNSPEC;
787 #ifdef ALTQ_DEBUG
788 		printf("read_dsfield: can't locate header!\n");
789 #endif
790 		return ((u_int8_t)0);
791 	}
792 
793 	if (pktattr->pattr_af == AF_INET) {
794 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
795 
796 		if (ip->ip_v != 4)
797 			return ((u_int8_t)0);	/* version mismatch! */
798 		ds_field = ip->ip_tos;
799 	}
800 #ifdef INET6
801 	else if (pktattr->pattr_af == AF_INET6) {
802 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
803 		u_int32_t flowlabel;
804 
805 		flowlabel = ntohl(ip6->ip6_flow);
806 		if ((flowlabel >> 28) != 6)
807 			return ((u_int8_t)0);	/* version mismatch! */
808 		ds_field = (flowlabel >> 20) & 0xff;
809 	}
810 #endif
811 	return (ds_field);
812 }
813 
814 void
write_dsfield(struct mbuf * m,struct altq_pktattr * pktattr,u_int8_t dsfield)815 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
816 {
817 	struct mbuf *m0;
818 
819 	if (pktattr == NULL ||
820 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
821 		return;
822 
823 	/* verify that pattr_hdr is within the mbuf data */
824 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
825 		if ((pktattr->pattr_hdr >= m0->m_data) &&
826 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
827 			break;
828 	if (m0 == NULL) {
829 		/* ick, pattr_hdr is stale */
830 		pktattr->pattr_af = AF_UNSPEC;
831 #ifdef ALTQ_DEBUG
832 		printf("write_dsfield: can't locate header!\n");
833 #endif
834 		return;
835 	}
836 
837 	if (pktattr->pattr_af == AF_INET) {
838 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
839 		u_int8_t old;
840 		int32_t sum;
841 
842 		if (ip->ip_v != 4)
843 			return;		/* version mismatch! */
844 		old = ip->ip_tos;
845 		dsfield |= old & 3;	/* leave CU bits */
846 		if (old == dsfield)
847 			return;
848 		ip->ip_tos = dsfield;
849 		/*
850 		 * update checksum (from RFC1624)
851 		 *	   HC' = ~(~HC + ~m + m')
852 		 */
853 		sum = ~ntohs(ip->ip_sum) & 0xffff;
854 		sum += 0xff00 + (~old & 0xff) + dsfield;
855 		sum = (sum >> 16) + (sum & 0xffff);
856 		sum += (sum >> 16);  /* add carry */
857 
858 		ip->ip_sum = htons(~sum & 0xffff);
859 	}
860 #ifdef INET6
861 	else if (pktattr->pattr_af == AF_INET6) {
862 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
863 		u_int32_t flowlabel;
864 
865 		flowlabel = ntohl(ip6->ip6_flow);
866 		if ((flowlabel >> 28) != 6)
867 			return;		/* version mismatch! */
868 		flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
869 		ip6->ip6_flow = htonl(flowlabel);
870 	}
871 #endif
872 	return;
873 }
874 
875 /*
876  * high resolution clock support taking advantage of a machine dependent
877  * high resolution time counter (e.g., timestamp counter of intel pentium).
878  * we assume
879  *  - 64-bit-long monotonically-increasing counter
880  *  - frequency range is 100M-4GHz (CPU speed)
881  */
882 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
883 #define	MACHCLK_SHIFT	8
884 
885 int machclk_usepcc;
886 u_int32_t machclk_freq;
887 u_int32_t machclk_per_tick;
888 
889 #if defined(__i386__) && defined(__NetBSD__)
890 extern u_int64_t cpu_tsc_freq;
891 #endif
892 
893 #if (__FreeBSD_version >= 700035)
894 /* Update TSC freq with the value indicated by the caller. */
895 static void
tsc_freq_changed(void * arg,const struct cf_level * level,int status)896 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
897 {
898 	/* If there was an error during the transition, don't do anything. */
899 	if (status != 0)
900 		return;
901 
902 #if (__FreeBSD_version >= 701102) && (defined(__amd64__) || defined(__i386__))
903 	/* If TSC is P-state invariant, don't do anything. */
904 	if (tsc_is_invariant)
905 		return;
906 #endif
907 
908 	/* Total setting for this level gives the new frequency in MHz. */
909 	init_machclk();
910 }
911 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
912     EVENTHANDLER_PRI_LAST);
913 #endif /* __FreeBSD_version >= 700035 */
914 
915 static void
init_machclk_setup(void)916 init_machclk_setup(void)
917 {
918 #if (__FreeBSD_version >= 600000)
919 	callout_init(&tbr_callout, 0);
920 #endif
921 
922 	machclk_usepcc = 1;
923 
924 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
925 	machclk_usepcc = 0;
926 #endif
927 #if defined(__FreeBSD__) && defined(SMP)
928 	machclk_usepcc = 0;
929 #endif
930 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
931 	machclk_usepcc = 0;
932 #endif
933 #if defined(__amd64__) || defined(__i386__)
934 	/* check if TSC is available */
935 	if ((cpu_feature & CPUID_TSC) == 0 ||
936 	    atomic_load_acq_64(&tsc_freq) == 0)
937 		machclk_usepcc = 0;
938 #endif
939 }
940 
941 void
init_machclk(void)942 init_machclk(void)
943 {
944 	static int called;
945 
946 	/* Call one-time initialization function. */
947 	if (!called) {
948 		init_machclk_setup();
949 		called = 1;
950 	}
951 
952 	if (machclk_usepcc == 0) {
953 		/* emulate 256MHz using microtime() */
954 		machclk_freq = 1000000 << MACHCLK_SHIFT;
955 		machclk_per_tick = machclk_freq / hz;
956 #ifdef ALTQ_DEBUG
957 		printf("altq: emulate %uHz cpu clock\n", machclk_freq);
958 #endif
959 		return;
960 	}
961 
962 	/*
963 	 * if the clock frequency (of Pentium TSC or Alpha PCC) is
964 	 * accessible, just use it.
965 	 */
966 #if defined(__amd64__) || defined(__i386__)
967 	machclk_freq = atomic_load_acq_64(&tsc_freq);
968 #endif
969 
970 	/*
971 	 * if we don't know the clock frequency, measure it.
972 	 */
973 	if (machclk_freq == 0) {
974 		static int	wait;
975 		struct timeval	tv_start, tv_end;
976 		u_int64_t	start, end, diff;
977 		int		timo;
978 
979 		microtime(&tv_start);
980 		start = read_machclk();
981 		timo = hz;	/* 1 sec */
982 		(void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
983 		microtime(&tv_end);
984 		end = read_machclk();
985 		diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
986 		    + tv_end.tv_usec - tv_start.tv_usec;
987 		if (diff != 0)
988 			machclk_freq = (u_int)((end - start) * 1000000 / diff);
989 	}
990 
991 	machclk_per_tick = machclk_freq / hz;
992 
993 #ifdef ALTQ_DEBUG
994 	printf("altq: CPU clock: %uHz\n", machclk_freq);
995 #endif
996 }
997 
998 #if defined(__OpenBSD__) && defined(__i386__)
999 static __inline u_int64_t
rdtsc(void)1000 rdtsc(void)
1001 {
1002 	u_int64_t rv;
1003 	__asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
1004 	return (rv);
1005 }
1006 #endif /* __OpenBSD__ && __i386__ */
1007 
1008 u_int64_t
read_machclk(void)1009 read_machclk(void)
1010 {
1011 	u_int64_t val;
1012 
1013 	if (machclk_usepcc) {
1014 #if defined(__amd64__) || defined(__i386__)
1015 		val = rdtsc();
1016 #else
1017 		panic("read_machclk");
1018 #endif
1019 	} else {
1020 		struct timeval tv, boottime;
1021 
1022 		microtime(&tv);
1023 		getboottime(&boottime);
1024 		val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1025 		    + tv.tv_usec) << MACHCLK_SHIFT);
1026 	}
1027 	return (val);
1028 }
1029 
1030 #ifdef ALTQ3_CLFIER_COMPAT
1031 
1032 #ifndef IPPROTO_ESP
1033 #define	IPPROTO_ESP	50		/* encapsulating security payload */
1034 #endif
1035 #ifndef IPPROTO_AH
1036 #define	IPPROTO_AH	51		/* authentication header */
1037 #endif
1038 
1039 /*
1040  * extract flow information from a given packet.
1041  * filt_mask shows flowinfo fields required.
1042  * we assume the ip header is in one mbuf, and addresses and ports are
1043  * in network byte order.
1044  */
1045 int
altq_extractflow(m,af,flow,filt_bmask)1046 altq_extractflow(m, af, flow, filt_bmask)
1047 	struct mbuf *m;
1048 	int af;
1049 	struct flowinfo *flow;
1050 	u_int32_t	filt_bmask;
1051 {
1052 
1053 	switch (af) {
1054 	case PF_INET: {
1055 		struct flowinfo_in *fin;
1056 		struct ip *ip;
1057 
1058 		ip = mtod(m, struct ip *);
1059 
1060 		if (ip->ip_v != 4)
1061 			break;
1062 
1063 		fin = (struct flowinfo_in *)flow;
1064 		fin->fi_len = sizeof(struct flowinfo_in);
1065 		fin->fi_family = AF_INET;
1066 
1067 		fin->fi_proto = ip->ip_p;
1068 		fin->fi_tos = ip->ip_tos;
1069 
1070 		fin->fi_src.s_addr = ip->ip_src.s_addr;
1071 		fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1072 
1073 		if (filt_bmask & FIMB4_PORTS)
1074 			/* if port info is required, extract port numbers */
1075 			extract_ports4(m, ip, fin);
1076 		else {
1077 			fin->fi_sport = 0;
1078 			fin->fi_dport = 0;
1079 			fin->fi_gpi = 0;
1080 		}
1081 		return (1);
1082 	}
1083 
1084 #ifdef INET6
1085 	case PF_INET6: {
1086 		struct flowinfo_in6 *fin6;
1087 		struct ip6_hdr *ip6;
1088 
1089 		ip6 = mtod(m, struct ip6_hdr *);
1090 		/* should we check the ip version? */
1091 
1092 		fin6 = (struct flowinfo_in6 *)flow;
1093 		fin6->fi6_len = sizeof(struct flowinfo_in6);
1094 		fin6->fi6_family = AF_INET6;
1095 
1096 		fin6->fi6_proto = ip6->ip6_nxt;
1097 		fin6->fi6_tclass   = IPV6_TRAFFIC_CLASS(ip6);
1098 
1099 		fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1100 		fin6->fi6_src = ip6->ip6_src;
1101 		fin6->fi6_dst = ip6->ip6_dst;
1102 
1103 		if ((filt_bmask & FIMB6_PORTS) ||
1104 		    ((filt_bmask & FIMB6_PROTO)
1105 		     && ip6->ip6_nxt > IPPROTO_IPV6))
1106 			/*
1107 			 * if port info is required, or proto is required
1108 			 * but there are option headers, extract port
1109 			 * and protocol numbers.
1110 			 */
1111 			extract_ports6(m, ip6, fin6);
1112 		else {
1113 			fin6->fi6_sport = 0;
1114 			fin6->fi6_dport = 0;
1115 			fin6->fi6_gpi = 0;
1116 		}
1117 		return (1);
1118 	}
1119 #endif /* INET6 */
1120 
1121 	default:
1122 		break;
1123 	}
1124 
1125 	/* failed */
1126 	flow->fi_len = sizeof(struct flowinfo);
1127 	flow->fi_family = AF_UNSPEC;
1128 	return (0);
1129 }
1130 
1131 /*
1132  * helper routine to extract port numbers
1133  */
1134 /* structure for ipsec and ipv6 option header template */
1135 struct _opt6 {
1136 	u_int8_t	opt6_nxt;	/* next header */
1137 	u_int8_t	opt6_hlen;	/* header extension length */
1138 	u_int16_t	_pad;
1139 	u_int32_t	ah_spi;		/* security parameter index
1140 					   for authentication header */
1141 };
1142 
1143 /*
1144  * extract port numbers from a ipv4 packet.
1145  */
1146 static int
extract_ports4(m,ip,fin)1147 extract_ports4(m, ip, fin)
1148 	struct mbuf *m;
1149 	struct ip *ip;
1150 	struct flowinfo_in *fin;
1151 {
1152 	struct mbuf *m0;
1153 	u_short ip_off;
1154 	u_int8_t proto;
1155 	int 	off;
1156 
1157 	fin->fi_sport = 0;
1158 	fin->fi_dport = 0;
1159 	fin->fi_gpi = 0;
1160 
1161 	ip_off = ntohs(ip->ip_off);
1162 	/* if it is a fragment, try cached fragment info */
1163 	if (ip_off & IP_OFFMASK) {
1164 		ip4f_lookup(ip, fin);
1165 		return (1);
1166 	}
1167 
1168 	/* locate the mbuf containing the protocol header */
1169 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
1170 		if (((caddr_t)ip >= m0->m_data) &&
1171 		    ((caddr_t)ip < m0->m_data + m0->m_len))
1172 			break;
1173 	if (m0 == NULL) {
1174 #ifdef ALTQ_DEBUG
1175 		printf("extract_ports4: can't locate header! ip=%p\n", ip);
1176 #endif
1177 		return (0);
1178 	}
1179 	off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1180 	proto = ip->ip_p;
1181 
1182 #ifdef ALTQ_IPSEC
1183  again:
1184 #endif
1185 	while (off >= m0->m_len) {
1186 		off -= m0->m_len;
1187 		m0 = m0->m_next;
1188 		if (m0 == NULL)
1189 			return (0);  /* bogus ip_hl! */
1190 	}
1191 	if (m0->m_len < off + 4)
1192 		return (0);
1193 
1194 	switch (proto) {
1195 	case IPPROTO_TCP:
1196 	case IPPROTO_UDP: {
1197 		struct udphdr *udp;
1198 
1199 		udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1200 		fin->fi_sport = udp->uh_sport;
1201 		fin->fi_dport = udp->uh_dport;
1202 		fin->fi_proto = proto;
1203 		}
1204 		break;
1205 
1206 #ifdef ALTQ_IPSEC
1207 	case IPPROTO_ESP:
1208 		if (fin->fi_gpi == 0){
1209 			u_int32_t *gpi;
1210 
1211 			gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1212 			fin->fi_gpi   = *gpi;
1213 		}
1214 		fin->fi_proto = proto;
1215 		break;
1216 
1217 	case IPPROTO_AH: {
1218 			/* get next header and header length */
1219 			struct _opt6 *opt6;
1220 
1221 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1222 			proto = opt6->opt6_nxt;
1223 			off += 8 + (opt6->opt6_hlen * 4);
1224 			if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1225 				fin->fi_gpi = opt6->ah_spi;
1226 		}
1227 		/* goto the next header */
1228 		goto again;
1229 #endif  /* ALTQ_IPSEC */
1230 
1231 	default:
1232 		fin->fi_proto = proto;
1233 		return (0);
1234 	}
1235 
1236 	/* if this is a first fragment, cache it. */
1237 	if (ip_off & IP_MF)
1238 		ip4f_cache(ip, fin);
1239 
1240 	return (1);
1241 }
1242 
1243 #ifdef INET6
1244 static int
extract_ports6(m,ip6,fin6)1245 extract_ports6(m, ip6, fin6)
1246 	struct mbuf *m;
1247 	struct ip6_hdr *ip6;
1248 	struct flowinfo_in6 *fin6;
1249 {
1250 	struct mbuf *m0;
1251 	int	off;
1252 	u_int8_t proto;
1253 
1254 	fin6->fi6_gpi   = 0;
1255 	fin6->fi6_sport = 0;
1256 	fin6->fi6_dport = 0;
1257 
1258 	/* locate the mbuf containing the protocol header */
1259 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
1260 		if (((caddr_t)ip6 >= m0->m_data) &&
1261 		    ((caddr_t)ip6 < m0->m_data + m0->m_len))
1262 			break;
1263 	if (m0 == NULL) {
1264 #ifdef ALTQ_DEBUG
1265 		printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1266 #endif
1267 		return (0);
1268 	}
1269 	off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1270 
1271 	proto = ip6->ip6_nxt;
1272 	do {
1273 		while (off >= m0->m_len) {
1274 			off -= m0->m_len;
1275 			m0 = m0->m_next;
1276 			if (m0 == NULL)
1277 				return (0);
1278 		}
1279 		if (m0->m_len < off + 4)
1280 			return (0);
1281 
1282 		switch (proto) {
1283 		case IPPROTO_TCP:
1284 		case IPPROTO_UDP: {
1285 			struct udphdr *udp;
1286 
1287 			udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1288 			fin6->fi6_sport = udp->uh_sport;
1289 			fin6->fi6_dport = udp->uh_dport;
1290 			fin6->fi6_proto = proto;
1291 			}
1292 			return (1);
1293 
1294 		case IPPROTO_ESP:
1295 			if (fin6->fi6_gpi == 0) {
1296 				u_int32_t *gpi;
1297 
1298 				gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1299 				fin6->fi6_gpi   = *gpi;
1300 			}
1301 			fin6->fi6_proto = proto;
1302 			return (1);
1303 
1304 		case IPPROTO_AH: {
1305 			/* get next header and header length */
1306 			struct _opt6 *opt6;
1307 
1308 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1309 			if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1310 				fin6->fi6_gpi = opt6->ah_spi;
1311 			proto = opt6->opt6_nxt;
1312 			off += 8 + (opt6->opt6_hlen * 4);
1313 			/* goto the next header */
1314 			break;
1315 			}
1316 
1317 		case IPPROTO_HOPOPTS:
1318 		case IPPROTO_ROUTING:
1319 		case IPPROTO_DSTOPTS: {
1320 			/* get next header and header length */
1321 			struct _opt6 *opt6;
1322 
1323 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1324 			proto = opt6->opt6_nxt;
1325 			off += (opt6->opt6_hlen + 1) * 8;
1326 			/* goto the next header */
1327 			break;
1328 			}
1329 
1330 		case IPPROTO_FRAGMENT:
1331 			/* ipv6 fragmentations are not supported yet */
1332 		default:
1333 			fin6->fi6_proto = proto;
1334 			return (0);
1335 		}
1336 	} while (1);
1337 	/*NOTREACHED*/
1338 }
1339 #endif /* INET6 */
1340 
1341 /*
1342  * altq common classifier
1343  */
1344 int
acc_add_filter(classifier,filter,class,phandle)1345 acc_add_filter(classifier, filter, class, phandle)
1346 	struct acc_classifier *classifier;
1347 	struct flow_filter *filter;
1348 	void	*class;
1349 	u_long	*phandle;
1350 {
1351 	struct acc_filter *afp, *prev, *tmp;
1352 	int	i, s;
1353 
1354 #ifdef INET6
1355 	if (filter->ff_flow.fi_family != AF_INET &&
1356 	    filter->ff_flow.fi_family != AF_INET6)
1357 		return (EINVAL);
1358 #else
1359 	if (filter->ff_flow.fi_family != AF_INET)
1360 		return (EINVAL);
1361 #endif
1362 
1363 	afp = malloc(sizeof(*afp), M_DEVBUF, M_WAITOK | M_ZERO);
1364 	afp->f_filter = *filter;
1365 	afp->f_class = class;
1366 
1367 	i = ACC_WILDCARD_INDEX;
1368 	if (filter->ff_flow.fi_family == AF_INET) {
1369 		struct flow_filter *filter4 = &afp->f_filter;
1370 
1371 		/*
1372 		 * if address is 0, it's a wildcard.  if address mask
1373 		 * isn't set, use full mask.
1374 		 */
1375 		if (filter4->ff_flow.fi_dst.s_addr == 0)
1376 			filter4->ff_mask.mask_dst.s_addr = 0;
1377 		else if (filter4->ff_mask.mask_dst.s_addr == 0)
1378 			filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1379 		if (filter4->ff_flow.fi_src.s_addr == 0)
1380 			filter4->ff_mask.mask_src.s_addr = 0;
1381 		else if (filter4->ff_mask.mask_src.s_addr == 0)
1382 			filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1383 
1384 		/* clear extra bits in addresses  */
1385 		   filter4->ff_flow.fi_dst.s_addr &=
1386 		       filter4->ff_mask.mask_dst.s_addr;
1387 		   filter4->ff_flow.fi_src.s_addr &=
1388 		       filter4->ff_mask.mask_src.s_addr;
1389 
1390 		/*
1391 		 * if dst address is a wildcard, use hash-entry
1392 		 * ACC_WILDCARD_INDEX.
1393 		 */
1394 		if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1395 			i = ACC_WILDCARD_INDEX;
1396 		else
1397 			i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1398 	}
1399 #ifdef INET6
1400 	else if (filter->ff_flow.fi_family == AF_INET6) {
1401 		struct flow_filter6 *filter6 =
1402 			(struct flow_filter6 *)&afp->f_filter;
1403 #ifndef IN6MASK0 /* taken from kame ipv6 */
1404 #define	IN6MASK0	{{{ 0, 0, 0, 0 }}}
1405 #define	IN6MASK128	{{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1406 		const struct in6_addr in6mask0 = IN6MASK0;
1407 		const struct in6_addr in6mask128 = IN6MASK128;
1408 #endif
1409 
1410 		if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1411 			filter6->ff_mask6.mask6_dst = in6mask0;
1412 		else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1413 			filter6->ff_mask6.mask6_dst = in6mask128;
1414 		if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1415 			filter6->ff_mask6.mask6_src = in6mask0;
1416 		else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1417 			filter6->ff_mask6.mask6_src = in6mask128;
1418 
1419 		/* clear extra bits in addresses  */
1420 		for (i = 0; i < 16; i++)
1421 			filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1422 			    filter6->ff_mask6.mask6_dst.s6_addr[i];
1423 		for (i = 0; i < 16; i++)
1424 			filter6->ff_flow6.fi6_src.s6_addr[i] &=
1425 			    filter6->ff_mask6.mask6_src.s6_addr[i];
1426 
1427 		if (filter6->ff_flow6.fi6_flowlabel == 0)
1428 			i = ACC_WILDCARD_INDEX;
1429 		else
1430 			i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1431 	}
1432 #endif /* INET6 */
1433 
1434 	afp->f_handle = get_filt_handle(classifier, i);
1435 
1436 	/* update filter bitmask */
1437 	afp->f_fbmask = filt2fibmask(filter);
1438 	classifier->acc_fbmask |= afp->f_fbmask;
1439 
1440 	/*
1441 	 * add this filter to the filter list.
1442 	 * filters are ordered from the highest rule number.
1443 	 */
1444 	s = splnet();
1445 	prev = NULL;
1446 	LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1447 		if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1448 			prev = tmp;
1449 		else
1450 			break;
1451 	}
1452 	if (prev == NULL)
1453 		LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1454 	else
1455 		LIST_INSERT_AFTER(prev, afp, f_chain);
1456 	splx(s);
1457 
1458 	*phandle = afp->f_handle;
1459 	return (0);
1460 }
1461 
1462 int
acc_delete_filter(classifier,handle)1463 acc_delete_filter(classifier, handle)
1464 	struct acc_classifier *classifier;
1465 	u_long handle;
1466 {
1467 	struct acc_filter *afp;
1468 	int	s;
1469 
1470 	if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1471 		return (EINVAL);
1472 
1473 	s = splnet();
1474 	LIST_REMOVE(afp, f_chain);
1475 	splx(s);
1476 
1477 	free(afp, M_DEVBUF);
1478 
1479 	/* todo: update filt_bmask */
1480 
1481 	return (0);
1482 }
1483 
1484 /*
1485  * delete filters referencing to the specified class.
1486  * if the all flag is not 0, delete all the filters.
1487  */
1488 int
acc_discard_filters(classifier,class,all)1489 acc_discard_filters(classifier, class, all)
1490 	struct acc_classifier *classifier;
1491 	void	*class;
1492 	int	all;
1493 {
1494 	struct acc_filter *afp;
1495 	int	i, s;
1496 
1497 	s = splnet();
1498 	for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1499 		do {
1500 			LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1501 				if (all || afp->f_class == class) {
1502 					LIST_REMOVE(afp, f_chain);
1503 					free(afp, M_DEVBUF);
1504 					/* start again from the head */
1505 					break;
1506 				}
1507 		} while (afp != NULL);
1508 	}
1509 	splx(s);
1510 
1511 	if (all)
1512 		classifier->acc_fbmask = 0;
1513 
1514 	return (0);
1515 }
1516 
1517 void *
acc_classify(clfier,m,af)1518 acc_classify(clfier, m, af)
1519 	void *clfier;
1520 	struct mbuf *m;
1521 	int af;
1522 {
1523 	struct acc_classifier *classifier;
1524 	struct flowinfo flow;
1525 	struct acc_filter *afp;
1526 	int	i;
1527 
1528 	classifier = (struct acc_classifier *)clfier;
1529 	altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1530 
1531 	if (flow.fi_family == AF_INET) {
1532 		struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1533 
1534 		if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1535 			/* only tos is used */
1536 			LIST_FOREACH(afp,
1537 				 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1538 				 f_chain)
1539 				if (apply_tosfilter4(afp->f_fbmask,
1540 						     &afp->f_filter, fp))
1541 					/* filter matched */
1542 					return (afp->f_class);
1543 		} else if ((classifier->acc_fbmask &
1544 			(~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1545 		    == 0) {
1546 			/* only proto and ports are used */
1547 			LIST_FOREACH(afp,
1548 				 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1549 				 f_chain)
1550 				if (apply_ppfilter4(afp->f_fbmask,
1551 						    &afp->f_filter, fp))
1552 					/* filter matched */
1553 					return (afp->f_class);
1554 		} else {
1555 			/* get the filter hash entry from its dest address */
1556 			i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1557 			do {
1558 				/*
1559 				 * go through this loop twice.  first for dst
1560 				 * hash, second for wildcards.
1561 				 */
1562 				LIST_FOREACH(afp, &classifier->acc_filters[i],
1563 					     f_chain)
1564 					if (apply_filter4(afp->f_fbmask,
1565 							  &afp->f_filter, fp))
1566 						/* filter matched */
1567 						return (afp->f_class);
1568 
1569 				/*
1570 				 * check again for filters with a dst addr
1571 				 * wildcard.
1572 				 * (daddr == 0 || dmask != 0xffffffff).
1573 				 */
1574 				if (i != ACC_WILDCARD_INDEX)
1575 					i = ACC_WILDCARD_INDEX;
1576 				else
1577 					break;
1578 			} while (1);
1579 		}
1580 	}
1581 #ifdef INET6
1582 	else if (flow.fi_family == AF_INET6) {
1583 		struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1584 
1585 		/* get the filter hash entry from its flow ID */
1586 		if (fp6->fi6_flowlabel != 0)
1587 			i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1588 		else
1589 			/* flowlable can be zero */
1590 			i = ACC_WILDCARD_INDEX;
1591 
1592 		/* go through this loop twice.  first for flow hash, second
1593 		   for wildcards. */
1594 		do {
1595 			LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1596 				if (apply_filter6(afp->f_fbmask,
1597 					(struct flow_filter6 *)&afp->f_filter,
1598 					fp6))
1599 					/* filter matched */
1600 					return (afp->f_class);
1601 
1602 			/*
1603 			 * check again for filters with a wildcard.
1604 			 */
1605 			if (i != ACC_WILDCARD_INDEX)
1606 				i = ACC_WILDCARD_INDEX;
1607 			else
1608 				break;
1609 		} while (1);
1610 	}
1611 #endif /* INET6 */
1612 
1613 	/* no filter matched */
1614 	return (NULL);
1615 }
1616 
1617 static int
apply_filter4(fbmask,filt,pkt)1618 apply_filter4(fbmask, filt, pkt)
1619 	u_int32_t	fbmask;
1620 	struct flow_filter *filt;
1621 	struct flowinfo_in *pkt;
1622 {
1623 	if (filt->ff_flow.fi_family != AF_INET)
1624 		return (0);
1625 	if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1626 		return (0);
1627 	if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1628 		return (0);
1629 	if ((fbmask & FIMB4_DADDR) &&
1630 	    filt->ff_flow.fi_dst.s_addr !=
1631 	    (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1632 		return (0);
1633 	if ((fbmask & FIMB4_SADDR) &&
1634 	    filt->ff_flow.fi_src.s_addr !=
1635 	    (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1636 		return (0);
1637 	if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1638 		return (0);
1639 	if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1640 	    (pkt->fi_tos & filt->ff_mask.mask_tos))
1641 		return (0);
1642 	if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1643 		return (0);
1644 	/* match */
1645 	return (1);
1646 }
1647 
1648 /*
1649  * filter matching function optimized for a common case that checks
1650  * only protocol and port numbers
1651  */
1652 static int
apply_ppfilter4(fbmask,filt,pkt)1653 apply_ppfilter4(fbmask, filt, pkt)
1654 	u_int32_t	fbmask;
1655 	struct flow_filter *filt;
1656 	struct flowinfo_in *pkt;
1657 {
1658 	if (filt->ff_flow.fi_family != AF_INET)
1659 		return (0);
1660 	if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1661 		return (0);
1662 	if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1663 		return (0);
1664 	if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1665 		return (0);
1666 	/* match */
1667 	return (1);
1668 }
1669 
1670 /*
1671  * filter matching function only for tos field.
1672  */
1673 static int
apply_tosfilter4(fbmask,filt,pkt)1674 apply_tosfilter4(fbmask, filt, pkt)
1675 	u_int32_t	fbmask;
1676 	struct flow_filter *filt;
1677 	struct flowinfo_in *pkt;
1678 {
1679 	if (filt->ff_flow.fi_family != AF_INET)
1680 		return (0);
1681 	if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1682 	    (pkt->fi_tos & filt->ff_mask.mask_tos))
1683 		return (0);
1684 	/* match */
1685 	return (1);
1686 }
1687 
1688 #ifdef INET6
1689 static int
apply_filter6(fbmask,filt,pkt)1690 apply_filter6(fbmask, filt, pkt)
1691 	u_int32_t	fbmask;
1692 	struct flow_filter6 *filt;
1693 	struct flowinfo_in6 *pkt;
1694 {
1695 	int i;
1696 
1697 	if (filt->ff_flow6.fi6_family != AF_INET6)
1698 		return (0);
1699 	if ((fbmask & FIMB6_FLABEL) &&
1700 	    filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1701 		return (0);
1702 	if ((fbmask & FIMB6_PROTO) &&
1703 	    filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1704 		return (0);
1705 	if ((fbmask & FIMB6_SPORT) &&
1706 	    filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1707 		return (0);
1708 	if ((fbmask & FIMB6_DPORT) &&
1709 	    filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1710 		return (0);
1711 	if (fbmask & FIMB6_SADDR) {
1712 		for (i = 0; i < 4; i++)
1713 			if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1714 			    (pkt->fi6_src.s6_addr32[i] &
1715 			     filt->ff_mask6.mask6_src.s6_addr32[i]))
1716 				return (0);
1717 	}
1718 	if (fbmask & FIMB6_DADDR) {
1719 		for (i = 0; i < 4; i++)
1720 			if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1721 			    (pkt->fi6_dst.s6_addr32[i] &
1722 			     filt->ff_mask6.mask6_dst.s6_addr32[i]))
1723 				return (0);
1724 	}
1725 	if ((fbmask & FIMB6_TCLASS) &&
1726 	    filt->ff_flow6.fi6_tclass !=
1727 	    (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1728 		return (0);
1729 	if ((fbmask & FIMB6_GPI) &&
1730 	    filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1731 		return (0);
1732 	/* match */
1733 	return (1);
1734 }
1735 #endif /* INET6 */
1736 
1737 /*
1738  *  filter handle:
1739  *	bit 20-28: index to the filter hash table
1740  *	bit  0-19: unique id in the hash bucket.
1741  */
1742 static u_long
get_filt_handle(classifier,i)1743 get_filt_handle(classifier, i)
1744 	struct acc_classifier *classifier;
1745 	int	i;
1746 {
1747 	static u_long handle_number = 1;
1748 	u_long 	handle;
1749 	struct acc_filter *afp;
1750 
1751 	while (1) {
1752 		handle = handle_number++ & 0x000fffff;
1753 
1754 		if (LIST_EMPTY(&classifier->acc_filters[i]))
1755 			break;
1756 
1757 		LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1758 			if ((afp->f_handle & 0x000fffff) == handle)
1759 				break;
1760 		if (afp == NULL)
1761 			break;
1762 		/* this handle is already used, try again */
1763 	}
1764 
1765 	return ((i << 20) | handle);
1766 }
1767 
1768 /* convert filter handle to filter pointer */
1769 static struct acc_filter *
filth_to_filtp(classifier,handle)1770 filth_to_filtp(classifier, handle)
1771 	struct acc_classifier *classifier;
1772 	u_long handle;
1773 {
1774 	struct acc_filter *afp;
1775 	int	i;
1776 
1777 	i = ACC_GET_HINDEX(handle);
1778 
1779 	LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1780 		if (afp->f_handle == handle)
1781 			return (afp);
1782 
1783 	return (NULL);
1784 }
1785 
1786 /* create flowinfo bitmask */
1787 static u_int32_t
filt2fibmask(filt)1788 filt2fibmask(filt)
1789 	struct flow_filter *filt;
1790 {
1791 	u_int32_t mask = 0;
1792 #ifdef INET6
1793 	struct flow_filter6 *filt6;
1794 #endif
1795 
1796 	switch (filt->ff_flow.fi_family) {
1797 	case AF_INET:
1798 		if (filt->ff_flow.fi_proto != 0)
1799 			mask |= FIMB4_PROTO;
1800 		if (filt->ff_flow.fi_tos != 0)
1801 			mask |= FIMB4_TOS;
1802 		if (filt->ff_flow.fi_dst.s_addr != 0)
1803 			mask |= FIMB4_DADDR;
1804 		if (filt->ff_flow.fi_src.s_addr != 0)
1805 			mask |= FIMB4_SADDR;
1806 		if (filt->ff_flow.fi_sport != 0)
1807 			mask |= FIMB4_SPORT;
1808 		if (filt->ff_flow.fi_dport != 0)
1809 			mask |= FIMB4_DPORT;
1810 		if (filt->ff_flow.fi_gpi != 0)
1811 			mask |= FIMB4_GPI;
1812 		break;
1813 #ifdef INET6
1814 	case AF_INET6:
1815 		filt6 = (struct flow_filter6 *)filt;
1816 
1817 		if (filt6->ff_flow6.fi6_proto != 0)
1818 			mask |= FIMB6_PROTO;
1819 		if (filt6->ff_flow6.fi6_tclass != 0)
1820 			mask |= FIMB6_TCLASS;
1821 		if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1822 			mask |= FIMB6_DADDR;
1823 		if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1824 			mask |= FIMB6_SADDR;
1825 		if (filt6->ff_flow6.fi6_sport != 0)
1826 			mask |= FIMB6_SPORT;
1827 		if (filt6->ff_flow6.fi6_dport != 0)
1828 			mask |= FIMB6_DPORT;
1829 		if (filt6->ff_flow6.fi6_gpi != 0)
1830 			mask |= FIMB6_GPI;
1831 		if (filt6->ff_flow6.fi6_flowlabel != 0)
1832 			mask |= FIMB6_FLABEL;
1833 		break;
1834 #endif /* INET6 */
1835 	}
1836 	return (mask);
1837 }
1838 
1839 /*
1840  * helper functions to handle IPv4 fragments.
1841  * currently only in-sequence fragments are handled.
1842  *	- fragment info is cached in a LRU list.
1843  *	- when a first fragment is found, cache its flow info.
1844  *	- when a non-first fragment is found, lookup the cache.
1845  */
1846 
1847 struct ip4_frag {
1848     TAILQ_ENTRY(ip4_frag) ip4f_chain;
1849     char    ip4f_valid;
1850     u_short ip4f_id;
1851     struct flowinfo_in ip4f_info;
1852 };
1853 
1854 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1855 
1856 #define	IP4F_TABSIZE		16	/* IPv4 fragment cache size */
1857 
1858 static void
ip4f_cache(ip,fin)1859 ip4f_cache(ip, fin)
1860 	struct ip *ip;
1861 	struct flowinfo_in *fin;
1862 {
1863 	struct ip4_frag *fp;
1864 
1865 	if (TAILQ_EMPTY(&ip4f_list)) {
1866 		/* first time call, allocate fragment cache entries. */
1867 		if (ip4f_init() < 0)
1868 			/* allocation failed! */
1869 			return;
1870 	}
1871 
1872 	fp = ip4f_alloc();
1873 	fp->ip4f_id = ip->ip_id;
1874 	fp->ip4f_info.fi_proto = ip->ip_p;
1875 	fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1876 	fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1877 
1878 	/* save port numbers */
1879 	fp->ip4f_info.fi_sport = fin->fi_sport;
1880 	fp->ip4f_info.fi_dport = fin->fi_dport;
1881 	fp->ip4f_info.fi_gpi   = fin->fi_gpi;
1882 }
1883 
1884 static int
ip4f_lookup(ip,fin)1885 ip4f_lookup(ip, fin)
1886 	struct ip *ip;
1887 	struct flowinfo_in *fin;
1888 {
1889 	struct ip4_frag *fp;
1890 
1891 	for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1892 	     fp = TAILQ_NEXT(fp, ip4f_chain))
1893 		if (ip->ip_id == fp->ip4f_id &&
1894 		    ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1895 		    ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1896 		    ip->ip_p == fp->ip4f_info.fi_proto) {
1897 			/* found the matching entry */
1898 			fin->fi_sport = fp->ip4f_info.fi_sport;
1899 			fin->fi_dport = fp->ip4f_info.fi_dport;
1900 			fin->fi_gpi   = fp->ip4f_info.fi_gpi;
1901 
1902 			if ((ntohs(ip->ip_off) & IP_MF) == 0)
1903 				/* this is the last fragment,
1904 				   release the entry. */
1905 				ip4f_free(fp);
1906 
1907 			return (1);
1908 		}
1909 
1910 	/* no matching entry found */
1911 	return (0);
1912 }
1913 
1914 static int
ip4f_init(void)1915 ip4f_init(void)
1916 {
1917 	struct ip4_frag *fp;
1918 	int i;
1919 
1920 	TAILQ_INIT(&ip4f_list);
1921 	for (i=0; i<IP4F_TABSIZE; i++) {
1922 		fp = malloc(sizeof(struct ip4_frag),
1923 		       M_DEVBUF, M_NOWAIT);
1924 		if (fp == NULL) {
1925 			printf("ip4f_init: can't alloc %dth entry!\n", i);
1926 			if (i == 0)
1927 				return (-1);
1928 			return (0);
1929 		}
1930 		fp->ip4f_valid = 0;
1931 		TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1932 	}
1933 	return (0);
1934 }
1935 
1936 static struct ip4_frag *
ip4f_alloc(void)1937 ip4f_alloc(void)
1938 {
1939 	struct ip4_frag *fp;
1940 
1941 	/* reclaim an entry at the tail, put it at the head */
1942 	fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1943 	TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1944 	fp->ip4f_valid = 1;
1945 	TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1946 	return (fp);
1947 }
1948 
1949 static void
ip4f_free(fp)1950 ip4f_free(fp)
1951 	struct ip4_frag *fp;
1952 {
1953 	TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1954 	fp->ip4f_valid = 0;
1955 	TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1956 }
1957 
1958 #endif /* ALTQ3_CLFIER_COMPAT */
1959