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