Home | History | Annotate | Line # | Download | only in altq
altq_subr.c revision 1.4.2.3
      1 /*	$NetBSD: altq_subr.c,v 1.4.2.3 2001/04/21 17:46:12 bouyer Exp $	*/
      2 /*	$KAME: altq_subr.c,v 1.8 2000/12/14 08:12:46 thorpej Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2000
      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 #if defined(__FreeBSD__) || defined(__NetBSD__)
     31 #include "opt_altq.h"
     32 #if (__FreeBSD__ != 2)
     33 #include "opt_inet.h"
     34 #ifdef __FreeBSD__
     35 #include "opt_inet6.h"
     36 #endif
     37 #endif
     38 #endif /* __FreeBSD__ || __NetBSD__ */
     39 
     40 #include <sys/param.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/socket.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/kernel.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 #include <altq/altq.h>
     67 #include <altq/altq_conf.h>
     68 
     69 #ifdef __FreeBSD__
     70 #include "opt_cpu.h"	/* for FreeBSD-2.2.8 to get i586_ctr_freq */
     71 #include <machine/clock.h>
     72 #endif
     73 
     74 /*
     75  * internal function prototypes
     76  */
     77 static void	tbr_timeout __P((void *));
     78 static int 	extract_ports4 __P((struct mbuf *, struct ip *,
     79 				    struct flowinfo_in *));
     80 #ifdef INET6
     81 static int 	extract_ports6 __P((struct mbuf *, struct ip6_hdr *,
     82 				    struct flowinfo_in6 *));
     83 #endif
     84 static int	apply_filter4 __P((u_int32_t, struct flow_filter *,
     85 				   struct flowinfo_in *));
     86 static int	apply_ppfilter4 __P((u_int32_t, struct flow_filter *,
     87 				     struct flowinfo_in *));
     88 #ifdef INET6
     89 static int	apply_filter6 __P((u_int32_t, struct flow_filter6 *,
     90 					   struct flowinfo_in6 *));
     91 #endif
     92 static int	apply_tosfilter4 __P((u_int32_t, struct flow_filter *,
     93 					     struct flowinfo_in *));
     94 static u_long	get_filt_handle __P((struct acc_classifier *, int));
     95 static struct acc_filter *filth_to_filtp __P((struct acc_classifier *,
     96 					      u_long));
     97 static u_int32_t filt2fibmask __P((struct flow_filter *));
     98 
     99 static void 	ip4f_cache __P((struct ip *, struct flowinfo_in *));
    100 static int 	ip4f_lookup __P((struct ip *, struct flowinfo_in *));
    101 static int 	ip4f_init __P((void));
    102 static struct ip4_frag	*ip4f_alloc __P((void));
    103 static void 	ip4f_free __P((struct ip4_frag *));
    104 
    105 int (*altq_input) __P((struct mbuf *, int)) = NULL;
    106 static int tbr_timer = 0;	/* token bucket regulator timer */
    107 static struct callout tbr_callout = CALLOUT_INITIALIZER;
    108 
    109 /*
    110  * alternate queueing support routines
    111  */
    112 
    113 /* look up the queue state by the interface name and the queuing type. */
    114 void *
    115 altq_lookup(name, type)
    116 	char *name;
    117 	int type;
    118 {
    119 	struct ifnet *ifp;
    120 
    121 	if ((ifp = ifunit(name)) != NULL) {
    122 		if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
    123 			return (ifp->if_snd.altq_disc);
    124 	}
    125 
    126 	return NULL;
    127 }
    128 
    129 int
    130 altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
    131 	struct ifaltq *ifq;
    132 	int type;
    133 	void *discipline;
    134 	int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
    135 	struct mbuf *(*dequeue)(struct ifaltq *, int);
    136 	int (*request)(struct ifaltq *, int, void *);
    137 	void *clfier;
    138 	void *(*classify)(void *, struct mbuf *, int);
    139 {
    140 	if (!ALTQ_IS_READY(ifq))
    141 		return ENXIO;
    142 	if (ALTQ_IS_ENABLED(ifq))
    143 		return EBUSY;
    144 	if (ALTQ_IS_ATTACHED(ifq))
    145 		return EEXIST;
    146 	ifq->altq_type     = type;
    147 	ifq->altq_disc     = discipline;
    148 	ifq->altq_enqueue  = enqueue;
    149 	ifq->altq_dequeue  = dequeue;
    150 	ifq->altq_request  = request;
    151 	ifq->altq_clfier   = clfier;
    152 	ifq->altq_classify = classify;
    153 	ifq->altq_flags &= ALTQF_CANTCHANGE;
    154 #ifdef ALTQ_KLD
    155 	altq_module_incref(type);
    156 #endif
    157 	return 0;
    158 }
    159 
    160 int
    161 altq_detach(ifq)
    162 	struct ifaltq *ifq;
    163 {
    164 	if (!ALTQ_IS_READY(ifq))
    165 		return ENXIO;
    166 	if (ALTQ_IS_ENABLED(ifq))
    167 		return EBUSY;
    168 	if (!ALTQ_IS_ATTACHED(ifq))
    169 		return (0);
    170 
    171 #ifdef ALTQ_KLD
    172 	altq_module_declref(ifq->altq_type);
    173 #endif
    174 	ifq->altq_type     = ALTQT_NONE;
    175 	ifq->altq_disc     = NULL;
    176 	ifq->altq_enqueue  = NULL;
    177 	ifq->altq_dequeue  = NULL;
    178 	ifq->altq_request  = NULL;
    179 	ifq->altq_clfier   = NULL;
    180 	ifq->altq_classify = NULL;
    181 	ifq->altq_flags &= ALTQF_CANTCHANGE;
    182 	return 0;
    183 }
    184 
    185 int
    186 altq_enable(ifq)
    187 	struct ifaltq *ifq;
    188 {
    189 	int s;
    190 
    191 	if (!ALTQ_IS_READY(ifq))
    192 		return ENXIO;
    193 	if (ALTQ_IS_ENABLED(ifq))
    194 		return 0;
    195 
    196 	s = splnet();
    197 	IFQ_PURGE(ifq);
    198 	ASSERT(ifq->ifq_len == 0);
    199 	ifq->altq_flags |= ALTQF_ENABLED;
    200 	if (ifq->altq_clfier != NULL)
    201 		ifq->altq_flags |= ALTQF_CLASSIFY;
    202 	splx(s);
    203 
    204 	return 0;
    205 }
    206 
    207 int
    208 altq_disable(ifq)
    209 	struct ifaltq *ifq;
    210 {
    211 	int s;
    212 
    213 	if (!ALTQ_IS_ENABLED(ifq))
    214 		return 0;
    215 
    216 	s = splnet();
    217 	IFQ_PURGE(ifq);
    218 	ASSERT(ifq->ifq_len == 0);
    219 	ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
    220 	splx(s);
    221 	return 0;
    222 }
    223 
    224 void
    225 altq_assert(file, line, failedexpr)
    226 	const char *file, *failedexpr;
    227 	int line;
    228 {
    229 	(void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
    230 		     failedexpr, file, line);
    231 	panic("altq assertion");
    232 	/* NOTREACHED */
    233 }
    234 
    235 /*
    236  * internal representation of token bucket parameters
    237  *	rate: 	byte_per_unittime << 32
    238  *		(((bits_per_sec) / 8) << 32) / machclk_freq
    239  *	depth:	byte << 32
    240  *
    241  */
    242 #define	TBR_SHIFT	32
    243 #define	TBR_SCALE(x)	((int64_t)(x) << TBR_SHIFT)
    244 #define	TBR_UNSCALE(x)	((x) >> TBR_SHIFT)
    245 
    246 struct mbuf *
    247 tbr_dequeue(ifq, op)
    248 	struct ifaltq *ifq;
    249 	int op;
    250 {
    251 	struct tb_regulator *tbr;
    252 	struct mbuf *m;
    253 	int64_t interval;
    254 	u_int64_t now;
    255 
    256 	tbr = ifq->altq_tbr;
    257 	if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
    258 		/* if this is a remove after poll, bypass tbr check */
    259 	} else {
    260 		/* update token only when it is negative */
    261 		if (tbr->tbr_token <= 0) {
    262 			now = read_machclk();
    263 			interval = now - tbr->tbr_last;
    264 			if (interval >= tbr->tbr_filluptime)
    265 				tbr->tbr_token = tbr->tbr_depth;
    266 			else {
    267 				tbr->tbr_token += interval * tbr->tbr_rate;
    268 				if (tbr->tbr_token > tbr->tbr_depth)
    269 					tbr->tbr_token = tbr->tbr_depth;
    270 			}
    271 			tbr->tbr_last = now;
    272 		}
    273 		/* if token is still negative, don't allow dequeue */
    274 		if (tbr->tbr_token <= 0)
    275 			return (NULL);
    276 	}
    277 
    278 	if (ALTQ_IS_ENABLED(ifq))
    279 		m = (*ifq->altq_dequeue)(ifq, op);
    280 	else {
    281 		if (op == ALTDQ_POLL)
    282 			IF_POLL(ifq, m);
    283 		else
    284 			IF_DEQUEUE(ifq, m);
    285 	}
    286 
    287 	if (m != NULL && op == ALTDQ_REMOVE)
    288 		tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
    289 	tbr->tbr_lastop = op;
    290 	return (m);
    291 }
    292 
    293 /*
    294  * set a token bucket regulator.
    295  * if the specified rate is zero, the token bucket regulator is deleted.
    296  */
    297 int
    298 tbr_set(ifq, profile)
    299 	struct ifaltq *ifq;
    300 	struct tb_profile *profile;
    301 {
    302 	struct tb_regulator *tbr, *otbr;
    303 
    304 	if (machclk_freq == 0)
    305 		init_machclk();
    306 	if (machclk_freq == 0) {
    307 		printf("tbr_set: no cpu clock available!\n");
    308 		return (ENXIO);
    309 	}
    310 
    311 	if (profile->rate == 0) {
    312 		/* delete this tbr */
    313 		if ((tbr = ifq->altq_tbr) == NULL)
    314 			return (ENOENT);
    315 		ifq->altq_tbr = NULL;
    316 		FREE(tbr, M_DEVBUF);
    317 		return (0);
    318 	}
    319 
    320 	MALLOC(tbr, struct tb_regulator *, sizeof(struct tb_regulator),
    321 	       M_DEVBUF, M_WAITOK);
    322 	if (tbr == NULL)
    323 		return (ENOMEM);
    324 	bzero(tbr, sizeof(struct tb_regulator));
    325 
    326 	tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
    327 	tbr->tbr_depth = TBR_SCALE(profile->depth);
    328 	if (tbr->tbr_rate > 0)
    329 		tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
    330 	else
    331 		tbr->tbr_filluptime = 0xffffffffffffffffLL;
    332 	tbr->tbr_token = tbr->tbr_depth;
    333 	tbr->tbr_last = read_machclk();
    334 	tbr->tbr_lastop = ALTDQ_REMOVE;
    335 
    336 	otbr = ifq->altq_tbr;
    337 	ifq->altq_tbr = tbr;	/* set the new tbr */
    338 
    339 	if (otbr != NULL)
    340 		FREE(otbr, M_DEVBUF);
    341 	else {
    342 		if (tbr_timer == 0) {
    343 			CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
    344 			tbr_timer = 1;
    345 		}
    346 	}
    347 	return (0);
    348 }
    349 
    350 /*
    351  * tbr_timeout goes through the interface list, and kicks the drivers
    352  * if necessary.
    353  */
    354 static void
    355 tbr_timeout(arg)
    356 	void *arg;
    357 {
    358 	struct ifnet *ifp;
    359 	int active, s;
    360 
    361 	active = 0;
    362 	s = splnet();
    363 #ifdef __FreeBSD__
    364 #if (__FreeBSD_version < 300000)
    365 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
    366 #else
    367 	for (ifp = ifnet.tqh_first; ifp != NULL; ifp = ifp->if_link.tqe_next)
    368 #endif
    369 #else /* !FreeBSD */
    370 	for (ifp = ifnet.tqh_first; ifp != NULL; ifp = ifp->if_list.tqe_next)
    371 #endif
    372 	{
    373 		if (!TBR_IS_ENABLED(&ifp->if_snd))
    374 			continue;
    375 		active++;
    376 		if (!IFQ_IS_EMPTY(&ifp->if_snd) && ifp->if_start != NULL)
    377 			(*ifp->if_start)(ifp);
    378 	}
    379 	splx(s);
    380 	if (active > 0)
    381 		CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
    382 	else
    383 		tbr_timer = 0;	/* don't need tbr_timer anymore */
    384 #if defined(__alpha__) && !defined(ALTQ_NOPCC)
    385 	{
    386 		/*
    387 		 * XXX read out the machine dependent clock once a second
    388 		 * to detect counter wrap-around.
    389 		 */
    390 		static u_int cnt;
    391 
    392 		if (++cnt >= hz) {
    393 			(void)read_machclk();
    394 			cnt = 0;
    395 		}
    396 	}
    397 #endif /* __alpha__ && !ALTQ_NOPCC */
    398 }
    399 
    400 /*
    401  * get token bucket regulator profile
    402  */
    403 int
    404 tbr_get(ifq, profile)
    405 	struct ifaltq *ifq;
    406 	struct tb_profile *profile;
    407 {
    408 	struct tb_regulator *tbr;
    409 
    410 	if ((tbr = ifq->altq_tbr) == NULL) {
    411 		profile->rate = 0;
    412 		profile->depth = 0;
    413 	} else {
    414 		profile->rate =
    415 		    (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
    416 		profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
    417 	}
    418 	return (0);
    419 }
    420 
    421 
    422 #ifndef IPPROTO_ESP
    423 #define	IPPROTO_ESP	50		/* encapsulating security payload */
    424 #endif
    425 #ifndef IPPROTO_AH
    426 #define	IPPROTO_AH	51		/* authentication header */
    427 #endif
    428 
    429 /*
    430  * extract flow information from a given packet.
    431  * filt_mask shows flowinfo fields required.
    432  * we assume the ip header is in one mbuf, and addresses and ports are
    433  * in network byte order.
    434  */
    435 int
    436 altq_extractflow(m, af, flow, filt_bmask)
    437 	struct mbuf *m;
    438 	int af;
    439 	struct flowinfo *flow;
    440 	u_int32_t	filt_bmask;
    441 {
    442 
    443 	switch (af) {
    444 	case PF_INET: {
    445 		struct flowinfo_in *fin;
    446 		struct ip *ip;
    447 
    448 		ip = mtod(m, struct ip *);
    449 
    450 		if (ip->ip_v != 4)
    451 			break;
    452 
    453 		fin = (struct flowinfo_in *)flow;
    454 		fin->fi_len = sizeof(struct flowinfo_in);
    455 		fin->fi_family = AF_INET;
    456 
    457 		fin->fi_proto = ip->ip_p;
    458 		fin->fi_tos = ip->ip_tos;
    459 
    460 		fin->fi_src.s_addr = ip->ip_src.s_addr;
    461 		fin->fi_dst.s_addr = ip->ip_dst.s_addr;
    462 
    463 		if (filt_bmask & FIMB4_PORTS)
    464 			/* if port info is required, extract port numbers */
    465 			extract_ports4(m, ip, fin);
    466 		else {
    467 			fin->fi_sport = 0;
    468 			fin->fi_dport = 0;
    469 			fin->fi_gpi = 0;
    470 		}
    471 		return (1);
    472 	}
    473 
    474 #ifdef INET6
    475 	case PF_INET6: {
    476 		struct flowinfo_in6 *fin6;
    477 		struct ip6_hdr *ip6;
    478 
    479 		ip6 = mtod(m, struct ip6_hdr *);
    480 		/* should we check the ip version? */
    481 
    482 		fin6 = (struct flowinfo_in6 *)flow;
    483 		fin6->fi6_len = sizeof(struct flowinfo_in6);
    484 		fin6->fi6_family = AF_INET6;
    485 
    486 		fin6->fi6_proto = ip6->ip6_nxt;
    487 		fin6->fi6_tclass   = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    488 
    489 		fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
    490 		fin6->fi6_src = ip6->ip6_src;
    491 		fin6->fi6_dst = ip6->ip6_dst;
    492 
    493 		if ((filt_bmask & FIMB6_PORTS) ||
    494 		    ((filt_bmask & FIMB6_PROTO)
    495 		     && ip6->ip6_nxt > IPPROTO_IPV6))
    496 			/*
    497 			 * if port info is required, or proto is required
    498 			 * but there are option headers, extract port
    499 			 * and protocol numbers.
    500 			 */
    501 			extract_ports6(m, ip6, fin6);
    502 		else {
    503 			fin6->fi6_sport = 0;
    504 			fin6->fi6_dport = 0;
    505 			fin6->fi6_gpi = 0;
    506 		}
    507 		return (1);
    508 	}
    509 #endif /* INET6 */
    510 
    511 	default:
    512 		break;
    513 	}
    514 
    515 	/* failed */
    516 	flow->fi_len = sizeof(struct flowinfo);
    517 	flow->fi_family = AF_UNSPEC;
    518 	return (0);
    519 }
    520 
    521 /*
    522  * helper routine to extract port numbers
    523  */
    524 /* structure for ipsec and ipv6 option header template */
    525 struct _opt6 {
    526 	u_int8_t	opt6_nxt;	/* next header */
    527 	u_int8_t	opt6_hlen;	/* header extension length */
    528 	u_int16_t	_pad;
    529 	u_int32_t	ah_spi;		/* security parameter index
    530 					   for authentication header */
    531 };
    532 
    533 /*
    534  * extract port numbers from a ipv4 packet.
    535  */
    536 static int
    537 extract_ports4(m, ip, fin)
    538 	struct mbuf *m;
    539 	struct ip *ip;
    540 	struct flowinfo_in *fin;
    541 {
    542 	struct mbuf *m0;
    543 	u_short ip_off;
    544 	u_int8_t proto;
    545 	int 	off;
    546 
    547 	fin->fi_sport = 0;
    548 	fin->fi_dport = 0;
    549 	fin->fi_gpi = 0;
    550 
    551 	ip_off = ntohs(ip->ip_off);
    552 	/* if it is a fragment, try cached fragment info */
    553 	if (ip_off & IP_OFFMASK) {
    554 		ip4f_lookup(ip, fin);
    555 		return (1);
    556 	}
    557 
    558 	/* locate the mbuf containing the protocol header */
    559 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
    560 		if (((caddr_t)ip >= m0->m_data) &&
    561 		    ((caddr_t)ip < m0->m_data + m0->m_len))
    562 			break;
    563 	if (m0 == NULL) {
    564 #ifdef ALTQ_DEBUG
    565 		printf("extract_ports4: can't locate header! ip=%p\n", ip);
    566 #endif
    567 		return (0);
    568 	}
    569 	off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
    570 	proto = ip->ip_p;
    571 
    572 #ifdef ALTQ_IPSEC
    573  again:
    574 #endif
    575 	while (off >= m0->m_len) {
    576 		off -= m0->m_len;
    577 		m0 = m0->m_next;
    578 	}
    579 	ASSERT(m0->m_len >= off + 4);
    580 
    581 	switch (proto) {
    582 	case IPPROTO_TCP:
    583 	case IPPROTO_UDP: {
    584 		struct udphdr *udp;
    585 
    586 		udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
    587 		fin->fi_sport = udp->uh_sport;
    588 		fin->fi_dport = udp->uh_dport;
    589 		fin->fi_proto = proto;
    590 		}
    591 		break;
    592 
    593 #ifdef ALTQ_IPSEC
    594 	case IPPROTO_ESP:
    595 		if (fin->fi_gpi == 0){
    596 			u_int32_t *gpi;
    597 
    598 			gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
    599 			fin->fi_gpi   = *gpi;
    600 		}
    601 		fin->fi_proto = proto;
    602 		break;
    603 
    604 	case IPPROTO_AH: {
    605 			/* get next header and header length */
    606 			struct _opt6 *opt6;
    607 
    608 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
    609 			proto = opt6->opt6_nxt;
    610 			off += 8 + (opt6->opt6_hlen * 4);
    611 			if (fin->fi_gpi == 0)
    612 				fin->fi_gpi = opt6->ah_spi;
    613 		}
    614 		/* goto the next header */
    615 		goto again;
    616 #endif  /* ALTQ_IPSEC */
    617 
    618 	default:
    619 		fin->fi_proto = proto;
    620 		return (0);
    621 	}
    622 
    623 	/* if this is a first fragment, cache it. */
    624 	if (ip_off & IP_MF)
    625 		ip4f_cache(ip, fin);
    626 
    627 	return (1);
    628 }
    629 
    630 #ifdef INET6
    631 static int
    632 extract_ports6(m, ip6, fin6)
    633 	struct mbuf *m;
    634 	struct ip6_hdr *ip6;
    635 	struct flowinfo_in6 *fin6;
    636 {
    637 	struct mbuf *m0;
    638 	int	off;
    639 	u_int8_t proto;
    640 
    641 	fin6->fi6_gpi   = 0;
    642 	fin6->fi6_sport = 0;
    643 	fin6->fi6_dport = 0;
    644 
    645 	/* locate the mbuf containing the protocol header */
    646 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
    647 		if (((caddr_t)ip6 >= m0->m_data) &&
    648 		    ((caddr_t)ip6 < m0->m_data + m0->m_len))
    649 			break;
    650 	if (m0 == NULL) {
    651 #ifdef ALTQ_DEBUG
    652 		printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
    653 #endif
    654 		return (0);
    655 	}
    656 	off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
    657 
    658 	proto = ip6->ip6_nxt;
    659 	do {
    660 		while (off >= m0->m_len) {
    661 			off -= m0->m_len;
    662 			m0 = m0->m_next;
    663 		}
    664 		ASSERT(m0->m_len >= off + 4);
    665 
    666 		switch (proto) {
    667 		case IPPROTO_TCP:
    668 		case IPPROTO_UDP: {
    669 			struct udphdr *udp;
    670 
    671 			udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
    672 			fin6->fi6_sport = udp->uh_sport;
    673 			fin6->fi6_dport = udp->uh_dport;
    674 			fin6->fi6_proto = proto;
    675 			}
    676 			return (1);
    677 
    678 		case IPPROTO_ESP:
    679 			if (fin6->fi6_gpi == 0) {
    680 				u_int32_t *gpi;
    681 
    682 				gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
    683 				fin6->fi6_gpi   = *gpi;
    684 			}
    685 			fin6->fi6_proto = proto;
    686 			return (1);
    687 
    688 		case IPPROTO_AH: {
    689 			/* get next header and header length */
    690 			struct _opt6 *opt6;
    691 
    692 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
    693 			if (fin6->fi6_gpi == 0)
    694 				fin6->fi6_gpi = opt6->ah_spi;
    695 			proto = opt6->opt6_nxt;
    696 			off += 8 + (opt6->opt6_hlen * 4);
    697 			/* goto the next header */
    698 			break;
    699 			}
    700 
    701 		case IPPROTO_HOPOPTS:
    702 		case IPPROTO_ROUTING:
    703 		case IPPROTO_DSTOPTS: {
    704 			/* get next header and header length */
    705 			struct _opt6 *opt6;
    706 
    707 			opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
    708 			proto = opt6->opt6_nxt;
    709 			off += (opt6->opt6_hlen + 1) * 8;
    710 			/* goto the next header */
    711 			break;
    712 			}
    713 
    714 		case IPPROTO_FRAGMENT:
    715 			/* ipv6 fragmentations are not supported yet */
    716 		default:
    717 			fin6->fi6_proto = proto;
    718 			return (0);
    719 		}
    720 	} while (1);
    721 	/*NOTREACHED*/
    722 }
    723 #endif /* INET6 */
    724 
    725 /*
    726  * altq common classifier
    727  */
    728 int
    729 acc_add_filter(classifier, filter, class, phandle)
    730 	struct acc_classifier *classifier;
    731 	struct flow_filter *filter;
    732 	void	*class;
    733 	u_long	*phandle;
    734 {
    735 	struct acc_filter *afp, *prev, *tmp;
    736 	int	i, s;
    737 
    738 #ifdef INET6
    739 	if (filter->ff_flow.fi_family != AF_INET &&
    740 	    filter->ff_flow.fi_family != AF_INET6)
    741 		return (EINVAL);
    742 #else
    743 	if (filter->ff_flow.fi_family != AF_INET)
    744 		return (EINVAL);
    745 #endif
    746 
    747 	MALLOC(afp, struct acc_filter *, sizeof(struct acc_filter),
    748 	       M_DEVBUF, M_WAITOK);
    749 	if (afp == NULL)
    750 		return (ENOMEM);
    751 	bzero(afp, sizeof(struct acc_filter));
    752 
    753 	afp->f_filter = *filter;
    754 	afp->f_class = class;
    755 
    756 	i = ACC_WILDCARD_INDEX;
    757 	if (filter->ff_flow.fi_family == AF_INET) {
    758 		struct flow_filter *filter4 = &afp->f_filter;
    759 
    760 		/*
    761 		 * if address is 0, it's a wildcard.  if address mask
    762 		 * isn't set, use full mask.
    763 		 */
    764 		if (filter4->ff_flow.fi_dst.s_addr == 0)
    765 			filter4->ff_mask.mask_dst.s_addr = 0;
    766 		else if (filter4->ff_mask.mask_dst.s_addr == 0)
    767 			filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
    768 		if (filter4->ff_flow.fi_src.s_addr == 0)
    769 			filter4->ff_mask.mask_src.s_addr = 0;
    770 		else if (filter4->ff_mask.mask_src.s_addr == 0)
    771 			filter4->ff_mask.mask_src.s_addr = 0xffffffff;
    772 
    773 		/* clear extra bits in addresses  */
    774 		   filter4->ff_flow.fi_dst.s_addr &=
    775 		       filter4->ff_mask.mask_dst.s_addr;
    776 		   filter4->ff_flow.fi_src.s_addr &=
    777 		       filter4->ff_mask.mask_src.s_addr;
    778 
    779 		/*
    780 		 * if dst address is a wildcard, use hash-entry
    781 		 * ACC_WILDCARD_INDEX.
    782 		 */
    783 		if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
    784 			i = ACC_WILDCARD_INDEX;
    785 		else
    786 			i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
    787 	}
    788 #ifdef INET6
    789 	else if (filter->ff_flow.fi_family == AF_INET6) {
    790 		struct flow_filter6 *filter6 =
    791 			(struct flow_filter6 *)&afp->f_filter;
    792 #ifndef IN6MASK0 /* taken from kame ipv6 */
    793 #define	IN6MASK0	{{{ 0, 0, 0, 0 }}}
    794 #define	IN6MASK128	{{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
    795 		const struct in6_addr in6mask0 = IN6MASK0;
    796 		const struct in6_addr in6mask128 = IN6MASK128;
    797 #endif
    798 
    799 		if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
    800 			filter6->ff_mask6.mask6_dst = in6mask0;
    801 		else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
    802 			filter6->ff_mask6.mask6_dst = in6mask128;
    803 		if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
    804 			filter6->ff_mask6.mask6_src = in6mask0;
    805 		else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
    806 			filter6->ff_mask6.mask6_src = in6mask128;
    807 
    808 		/* clear extra bits in addresses  */
    809 		for (i = 0; i < 16; i++)
    810 			filter6->ff_flow6.fi6_dst.s6_addr[i] &=
    811 			    filter6->ff_mask6.mask6_dst.s6_addr[i];
    812 		for (i = 0; i < 16; i++)
    813 			filter6->ff_flow6.fi6_src.s6_addr[i] &=
    814 			    filter6->ff_mask6.mask6_src.s6_addr[i];
    815 
    816 		if (filter6->ff_flow6.fi6_flowlabel == 0)
    817 			i = ACC_WILDCARD_INDEX;
    818 		else
    819 			i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
    820 	}
    821 #endif /* INET6 */
    822 
    823 	afp->f_handle = get_filt_handle(classifier, i);
    824 
    825 	/* update filter bitmask */
    826 	afp->f_fbmask = filt2fibmask(filter);
    827 	classifier->acc_fbmask |= afp->f_fbmask;
    828 
    829 	/*
    830 	 * add this filter to the filter list.
    831 	 * filters are ordered from the highest rule number.
    832 	 */
    833 	s = splnet();
    834 	prev = NULL;
    835 	LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
    836 		if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
    837 			prev = tmp;
    838 		else
    839 			break;
    840 	}
    841 	if (prev == NULL)
    842 		LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
    843 	else
    844 		LIST_INSERT_AFTER(prev, afp, f_chain);
    845 	splx(s);
    846 
    847 	*phandle = afp->f_handle;
    848 	return (0);
    849 }
    850 
    851 int
    852 acc_delete_filter(classifier, handle)
    853 	struct acc_classifier *classifier;
    854 	u_long handle;
    855 {
    856 	struct acc_filter *afp;
    857 	int	s;
    858 
    859 	if ((afp = filth_to_filtp(classifier, handle)) == NULL)
    860 		return (EINVAL);
    861 
    862 	s = splnet();
    863 	LIST_REMOVE(afp, f_chain);
    864 	splx(s);
    865 
    866 	FREE(afp, M_DEVBUF);
    867 
    868 	/* todo: update filt_bmask */
    869 
    870 	return (0);
    871 }
    872 
    873 /*
    874  * delete filters referencing to the specified class.
    875  * if the all flag is not 0, delete all the filters.
    876  */
    877 int
    878 acc_discard_filters(classifier, class, all)
    879 	struct acc_classifier *classifier;
    880 	void	*class;
    881 	int	all;
    882 {
    883 	struct acc_filter *afp;
    884 	int	i, s;
    885 
    886 	s = splnet();
    887 	for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
    888 		do {
    889 			LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
    890 				if (all || afp->f_class == class) {
    891 					LIST_REMOVE(afp, f_chain);
    892 					FREE(afp, M_DEVBUF);
    893 					/* start again from the head */
    894 					break;
    895 				}
    896 		} while (afp != NULL);
    897 	}
    898 	splx(s);
    899 
    900 	if (all)
    901 		classifier->acc_fbmask = 0;
    902 
    903 	return (0);
    904 }
    905 
    906 void *
    907 acc_classify(clfier, m, af)
    908 	void *clfier;
    909 	struct mbuf *m;
    910 	int af;
    911 {
    912 	struct acc_classifier *classifier;
    913 	struct flowinfo flow;
    914 	struct acc_filter *afp;
    915 	int	i;
    916 
    917 	classifier = (struct acc_classifier *)clfier;
    918 	altq_extractflow(m, af, &flow, classifier->acc_fbmask);
    919 
    920 	if (flow.fi_family == AF_INET) {
    921 		struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
    922 
    923 		if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
    924 			/* only tos is used */
    925 			LIST_FOREACH(afp,
    926 				 &classifier->acc_filters[ACC_WILDCARD_INDEX],
    927 				 f_chain)
    928 				if (apply_tosfilter4(afp->f_fbmask,
    929 						     &afp->f_filter, fp))
    930 					/* filter matched */
    931 					return (afp->f_class);
    932 		} else if ((classifier->acc_fbmask &
    933 			(~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
    934 		    == 0) {
    935 			/* only proto and ports are used */
    936 			LIST_FOREACH(afp,
    937 				 &classifier->acc_filters[ACC_WILDCARD_INDEX],
    938 				 f_chain)
    939 				if (apply_ppfilter4(afp->f_fbmask,
    940 						    &afp->f_filter, fp))
    941 					/* filter matched */
    942 					return (afp->f_class);
    943 		} else {
    944 			/* get the filter hash entry from its dest address */
    945 			i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
    946 			do {
    947 				/*
    948 				 * go through this loop twice.  first for dst
    949 				 * hash, second for wildcards.
    950 				 */
    951 				LIST_FOREACH(afp, &classifier->acc_filters[i],
    952 					     f_chain)
    953 					if (apply_filter4(afp->f_fbmask,
    954 							  &afp->f_filter, fp))
    955 						/* filter matched */
    956 						return (afp->f_class);
    957 
    958 				/*
    959 				 * check again for filters with a dst addr
    960 				 * wildcard.
    961 				 * (daddr == 0 || dmask != 0xffffffff).
    962 				 */
    963 				if (i != ACC_WILDCARD_INDEX)
    964 					i = ACC_WILDCARD_INDEX;
    965 				else
    966 					break;
    967 			} while (1);
    968 		}
    969 	}
    970 #ifdef INET6
    971 	else if (flow.fi_family == AF_INET6) {
    972 		struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
    973 
    974 		/* get the filter hash entry from its flow ID */
    975 		if (fp6->fi6_flowlabel != 0)
    976 			i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
    977 		else
    978 			/* flowlable can be zero */
    979 			i = ACC_WILDCARD_INDEX;
    980 
    981 		/* go through this loop twice.  first for flow hash, second
    982 		   for wildcards. */
    983 		do {
    984 			LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
    985 				if (apply_filter6(afp->f_fbmask,
    986 					(struct flow_filter6 *)&afp->f_filter,
    987 					fp6))
    988 					/* filter matched */
    989 					return (afp->f_class);
    990 
    991 			/*
    992 			 * check again for filters with a wildcard.
    993 			 */
    994 			if (i != ACC_WILDCARD_INDEX)
    995 				i = ACC_WILDCARD_INDEX;
    996 			else
    997 				break;
    998 		} while (1);
    999 	}
   1000 #endif /* INET6 */
   1001 
   1002 	/* no filter matched */
   1003 	return (NULL);
   1004 }
   1005 
   1006 static int
   1007 apply_filter4(fbmask, filt, pkt)
   1008 	u_int32_t	fbmask;
   1009 	struct flow_filter *filt;
   1010 	struct flowinfo_in *pkt;
   1011 {
   1012 	if (filt->ff_flow.fi_family != AF_INET)
   1013 		return (0);
   1014 	if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
   1015 		return (0);
   1016 	if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
   1017 		return (0);
   1018 	if ((fbmask & FIMB4_DADDR) &&
   1019 	    filt->ff_flow.fi_dst.s_addr !=
   1020 	    (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
   1021 		return (0);
   1022 	if ((fbmask & FIMB4_SADDR) &&
   1023 	    filt->ff_flow.fi_src.s_addr !=
   1024 	    (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
   1025 		return (0);
   1026 	if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
   1027 		return (0);
   1028 	if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
   1029 	    (pkt->fi_tos & filt->ff_mask.mask_tos))
   1030 		return (0);
   1031 	if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
   1032 		return (0);
   1033 	/* match */
   1034 	return (1);
   1035 }
   1036 
   1037 /*
   1038  * filter matching function optimized for a common case that checks
   1039  * only protocol and port numbers
   1040  */
   1041 static int
   1042 apply_ppfilter4(fbmask, filt, pkt)
   1043 	u_int32_t	fbmask;
   1044 	struct flow_filter *filt;
   1045 	struct flowinfo_in *pkt;
   1046 {
   1047 	if (filt->ff_flow.fi_family != AF_INET)
   1048 		return (0);
   1049 	if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
   1050 		return (0);
   1051 	if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
   1052 		return (0);
   1053 	if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
   1054 		return (0);
   1055 	/* match */
   1056 	return (1);
   1057 }
   1058 
   1059 /*
   1060  * filter matching function only for tos field.
   1061  */
   1062 static int
   1063 apply_tosfilter4(fbmask, filt, pkt)
   1064 	u_int32_t	fbmask;
   1065 	struct flow_filter *filt;
   1066 	struct flowinfo_in *pkt;
   1067 {
   1068 	if (filt->ff_flow.fi_family != AF_INET)
   1069 		return (0);
   1070 	if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
   1071 	    (pkt->fi_tos & filt->ff_mask.mask_tos))
   1072 		return (0);
   1073 	/* match */
   1074 	return (1);
   1075 }
   1076 
   1077 #ifdef INET6
   1078 static int
   1079 apply_filter6(fbmask, filt, pkt)
   1080 	u_int32_t	fbmask;
   1081 	struct flow_filter6 *filt;
   1082 	struct flowinfo_in6 *pkt;
   1083 {
   1084 	int i;
   1085 
   1086 	if (filt->ff_flow6.fi6_family != AF_INET6)
   1087 		return (0);
   1088 	if ((fbmask & FIMB6_FLABEL) &&
   1089 	    filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
   1090 		return (0);
   1091 	if ((fbmask & FIMB6_PROTO) &&
   1092 	    filt->ff_flow6.fi6_proto != pkt->fi6_proto)
   1093 		return (0);
   1094 	if ((fbmask & FIMB6_SPORT) &&
   1095 	    filt->ff_flow6.fi6_sport != pkt->fi6_sport)
   1096 		return (0);
   1097 	if ((fbmask & FIMB6_DPORT) &&
   1098 	    filt->ff_flow6.fi6_dport != pkt->fi6_dport)
   1099 		return (0);
   1100 	if (fbmask & FIMB6_SADDR) {
   1101 		for (i = 0; i < 4; i++)
   1102 			if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
   1103 			    (pkt->fi6_src.s6_addr32[i] &
   1104 			     filt->ff_mask6.mask6_src.s6_addr32[i]))
   1105 				return (0);
   1106 	}
   1107 	if (fbmask & FIMB6_DADDR) {
   1108 		for (i = 0; i < 4; i++)
   1109 			if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
   1110 			    (pkt->fi6_dst.s6_addr32[i] &
   1111 			     filt->ff_mask6.mask6_dst.s6_addr32[i]))
   1112 				return (0);
   1113 	}
   1114 	if ((fbmask & FIMB6_TCLASS) &&
   1115 	    filt->ff_flow6.fi6_tclass !=
   1116 	    (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
   1117 		return (0);
   1118 	if ((fbmask & FIMB6_GPI) &&
   1119 	    filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
   1120 		return (0);
   1121 	/* match */
   1122 	return (1);
   1123 }
   1124 #endif /* INET6 */
   1125 
   1126 /*
   1127  *  filter handle:
   1128  *	bit 20-28: index to the filter hash table
   1129  *	bit  0-19: unique id in the hash bucket.
   1130  */
   1131 static u_long
   1132 get_filt_handle(classifier, i)
   1133 	struct acc_classifier *classifier;
   1134 	int	i;
   1135 {
   1136 	static u_long handle_number = 1;
   1137 	u_long 	handle;
   1138 	struct acc_filter *afp;
   1139 
   1140 	while (1) {
   1141 		handle = handle_number++ & 0x000fffff;
   1142 
   1143 		if (LIST_EMPTY(&classifier->acc_filters[i]))
   1144 			break;
   1145 
   1146 		LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
   1147 			if ((afp->f_handle & 0x000fffff) == handle)
   1148 				break;
   1149 		if (afp == NULL)
   1150 			break;
   1151 		/* this handle is already used, try again */
   1152 	}
   1153 
   1154 	return ((i << 20) | handle);
   1155 }
   1156 
   1157 /* convert filter handle to filter pointer */
   1158 static struct acc_filter *
   1159 filth_to_filtp(classifier, handle)
   1160 	struct acc_classifier *classifier;
   1161 	u_long handle;
   1162 {
   1163 	struct acc_filter *afp;
   1164 	int	i;
   1165 
   1166 	i = ACC_GET_HINDEX(handle);
   1167 
   1168 	LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
   1169 		if (afp->f_handle == handle)
   1170 			return (afp);
   1171 
   1172 	return (NULL);
   1173 }
   1174 
   1175 /* create flowinfo bitmask */
   1176 static u_int32_t
   1177 filt2fibmask(filt)
   1178 	struct flow_filter *filt;
   1179 {
   1180 	u_int32_t mask = 0;
   1181 #ifdef INET6
   1182 	struct flow_filter6 *filt6;
   1183 #endif
   1184 
   1185 	switch (filt->ff_flow.fi_family) {
   1186 	case AF_INET:
   1187 		if (filt->ff_flow.fi_proto != 0)
   1188 			mask |= FIMB4_PROTO;
   1189 		if (filt->ff_flow.fi_tos != 0)
   1190 			mask |= FIMB4_TOS;
   1191 		if (filt->ff_flow.fi_dst.s_addr != 0)
   1192 			mask |= FIMB4_DADDR;
   1193 		if (filt->ff_flow.fi_src.s_addr != 0)
   1194 			mask |= FIMB4_SADDR;
   1195 		if (filt->ff_flow.fi_sport != 0)
   1196 			mask |= FIMB4_SPORT;
   1197 		if (filt->ff_flow.fi_dport != 0)
   1198 			mask |= FIMB4_DPORT;
   1199 		if (filt->ff_flow.fi_gpi != 0)
   1200 			mask |= FIMB4_GPI;
   1201 		break;
   1202 #ifdef INET6
   1203 	case AF_INET6:
   1204 		filt6 = (struct flow_filter6 *)filt;
   1205 
   1206 		if (filt6->ff_flow6.fi6_proto != 0)
   1207 			mask |= FIMB6_PROTO;
   1208 		if (filt6->ff_flow6.fi6_tclass != 0)
   1209 			mask |= FIMB6_TCLASS;
   1210 		if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
   1211 			mask |= FIMB6_DADDR;
   1212 		if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
   1213 			mask |= FIMB6_SADDR;
   1214 		if (filt6->ff_flow6.fi6_sport != 0)
   1215 			mask |= FIMB6_SPORT;
   1216 		if (filt6->ff_flow6.fi6_dport != 0)
   1217 			mask |= FIMB6_DPORT;
   1218 		if (filt6->ff_flow6.fi6_gpi != 0)
   1219 			mask |= FIMB6_GPI;
   1220 		if (filt6->ff_flow6.fi6_flowlabel != 0)
   1221 			mask |= FIMB6_FLABEL;
   1222 		break;
   1223 #endif /* INET6 */
   1224 	}
   1225 	return (mask);
   1226 }
   1227 
   1228 
   1229 /*
   1230  * helper functions to handle IPv4 fragments.
   1231  * currently only in-sequence fragments are handled.
   1232  *	- fragment info is cached in a LRU list.
   1233  *	- when a first fragment is found, cache its flow info.
   1234  *	- when a non-first fragment is found, lookup the cache.
   1235  */
   1236 
   1237 struct ip4_frag {
   1238     TAILQ_ENTRY(ip4_frag) ip4f_chain;
   1239     char    ip4f_valid;
   1240     u_short ip4f_id;
   1241     struct flowinfo_in ip4f_info;
   1242 };
   1243 
   1244 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
   1245 
   1246 #define	IP4F_TABSIZE		16	/* IPv4 fragment cache size */
   1247 
   1248 
   1249 static void
   1250 ip4f_cache(ip, fin)
   1251 	struct ip *ip;
   1252 	struct flowinfo_in *fin;
   1253 {
   1254 	struct ip4_frag *fp;
   1255 
   1256 	if (TAILQ_EMPTY(&ip4f_list)) {
   1257 		/* first time call, allocate fragment cache entries. */
   1258 		if (ip4f_init() < 0)
   1259 			/* allocation failed! */
   1260 			return;
   1261 	}
   1262 
   1263 	fp = ip4f_alloc();
   1264 	fp->ip4f_id = ip->ip_id;
   1265 
   1266 	/* save port numbers */
   1267 	fp->ip4f_info.fi_sport = fin->fi_sport;
   1268 	fp->ip4f_info.fi_dport = fin->fi_dport;
   1269 	fp->ip4f_info.fi_gpi   = fin->fi_gpi;
   1270 }
   1271 
   1272 static int
   1273 ip4f_lookup(ip, fin)
   1274 	struct ip *ip;
   1275 	struct flowinfo_in *fin;
   1276 {
   1277 	struct ip4_frag *fp;
   1278 
   1279 	for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
   1280 	     fp = TAILQ_NEXT(fp, ip4f_chain))
   1281 		if (ip->ip_id == fp->ip4f_id &&
   1282 		    ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
   1283 		    ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
   1284 		    ip->ip_p == fp->ip4f_info.fi_proto) {
   1285 
   1286 			/* found the matching entry */
   1287 			fin->fi_sport = fp->ip4f_info.fi_sport;
   1288 			fin->fi_dport = fp->ip4f_info.fi_dport;
   1289 			fin->fi_gpi   = fp->ip4f_info.fi_gpi;
   1290 
   1291 			if ((ntohs(ip->ip_off) & IP_MF) == 0)
   1292 				/* this is the last fragment,
   1293 				   release the entry. */
   1294 				ip4f_free(fp);
   1295 
   1296 			return (1);
   1297 		}
   1298 
   1299 	/* no matching entry found */
   1300 	return (0);
   1301 }
   1302 
   1303 static int
   1304 ip4f_init(void)
   1305 {
   1306 	struct ip4_frag *fp;
   1307 	int i;
   1308 
   1309 	TAILQ_INIT(&ip4f_list);
   1310 	for (i=0; i<IP4F_TABSIZE; i++) {
   1311 		MALLOC(fp, struct ip4_frag *, sizeof(struct ip4_frag),
   1312 		       M_DEVBUF, M_NOWAIT);
   1313 		if (fp == NULL) {
   1314 			printf("ip4f_init: can't alloc %dth entry!\n", i);
   1315 			if (i == 0)
   1316 				return (-1);
   1317 			return (0);
   1318 		}
   1319 		fp->ip4f_valid = 0;
   1320 		TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
   1321 	}
   1322 	return (0);
   1323 }
   1324 
   1325 static struct ip4_frag *
   1326 ip4f_alloc(void)
   1327 {
   1328 	struct ip4_frag *fp;
   1329 
   1330 	/* reclaim an entry at the tail, put it at the head */
   1331 	fp = TAILQ_LAST(&ip4f_list, ip4f_list);
   1332 	TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
   1333 	fp->ip4f_valid = 1;
   1334 	TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
   1335 	return (fp);
   1336 }
   1337 
   1338 static void
   1339 ip4f_free(fp)
   1340 	struct ip4_frag *fp;
   1341 {
   1342 	TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
   1343 	fp->ip4f_valid = 0;
   1344 	TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
   1345 }
   1346 
   1347 /*
   1348  * read and write diffserv field in IPv4 or IPv6 header
   1349  */
   1350 u_int8_t
   1351 read_dsfield(m, pktattr)
   1352 	struct mbuf *m;
   1353 	struct altq_pktattr *pktattr;
   1354 {
   1355 	struct mbuf *m0;
   1356 	u_int8_t ds_field = 0;
   1357 
   1358 	if (pktattr == NULL ||
   1359 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
   1360 		return ((u_int8_t)0);
   1361 
   1362 	/* verify that pattr_hdr is within the mbuf data */
   1363 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
   1364 		if ((pktattr->pattr_hdr >= m0->m_data) &&
   1365 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
   1366 			break;
   1367 	if (m0 == NULL) {
   1368 		/* ick, pattr_hdr is stale */
   1369 		pktattr->pattr_af = AF_UNSPEC;
   1370 #ifdef ALTQ_DEBUG
   1371 		printf("read_dsfield: can't locate header!\n");
   1372 #endif
   1373 		return ((u_int8_t)0);
   1374 	}
   1375 
   1376 	if (pktattr->pattr_af == AF_INET) {
   1377 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
   1378 
   1379 		if (ip->ip_v != 4)
   1380 			return ((u_int8_t)0);	/* version mismatch! */
   1381 		ds_field = ip->ip_tos;
   1382 	}
   1383 #ifdef INET6
   1384 	else if (pktattr->pattr_af == AF_INET6) {
   1385 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
   1386 		u_int32_t flowlabel;
   1387 
   1388 		flowlabel = ntohl(ip6->ip6_flow);
   1389 		if ((flowlabel >> 28) != 6)
   1390 			return ((u_int8_t)0);	/* version mismatch! */
   1391 		ds_field = (flowlabel >> 20) & 0xff;
   1392 	}
   1393 #endif
   1394 	return (ds_field);
   1395 }
   1396 
   1397 void
   1398 write_dsfield(m, pktattr, dsfield)
   1399 	struct mbuf *m;
   1400 	struct altq_pktattr *pktattr;
   1401 	u_int8_t dsfield;
   1402 {
   1403 	struct mbuf *m0;
   1404 
   1405 	if (pktattr == NULL ||
   1406 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
   1407 		return;
   1408 
   1409 	/* verify that pattr_hdr is within the mbuf data */
   1410 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
   1411 		if ((pktattr->pattr_hdr >= m0->m_data) &&
   1412 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
   1413 			break;
   1414 	if (m0 == NULL) {
   1415 		/* ick, pattr_hdr is stale */
   1416 		pktattr->pattr_af = AF_UNSPEC;
   1417 #ifdef ALTQ_DEBUG
   1418 		printf("write_dsfield: can't locate header!\n");
   1419 #endif
   1420 		return;
   1421 	}
   1422 
   1423 	if (pktattr->pattr_af == AF_INET) {
   1424 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
   1425 		u_int8_t old;
   1426 		int32_t sum;
   1427 
   1428 		if (ip->ip_v != 4)
   1429 			return;		/* version mismatch! */
   1430 		old = ip->ip_tos;
   1431 		dsfield |= old & 3;	/* leave CU bits */
   1432 		if (old == dsfield)
   1433 			return;
   1434 		ip->ip_tos = dsfield;
   1435 		/*
   1436 		 * update checksum (from RFC1624)
   1437 		 *	   HC' = ~(~HC + ~m + m')
   1438 		 */
   1439 		sum = ~ntohs(ip->ip_sum) & 0xffff;
   1440 		sum += 0xff00 + (~old & 0xff) + dsfield;
   1441 		sum = (sum >> 16) + (sum & 0xffff);
   1442 		sum += (sum >> 16);  /* add carry */
   1443 
   1444 		ip->ip_sum = htons(~sum & 0xffff);
   1445 	}
   1446 #ifdef INET6
   1447 	else if (pktattr->pattr_af == AF_INET6) {
   1448 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
   1449 		u_int32_t flowlabel;
   1450 
   1451 		flowlabel = ntohl(ip6->ip6_flow);
   1452 		if ((flowlabel >> 28) != 6)
   1453 			return;		/* version mismatch! */
   1454 		flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
   1455 		ip6->ip6_flow = htonl(flowlabel);
   1456 	}
   1457 #endif
   1458 	return;
   1459 }
   1460 
   1461 
   1462 /*
   1463  * high resolution clock support taking advantage of a machine dependent
   1464  * high resolution time counter (e.g., timestamp counter of intel pentium).
   1465  * we assume
   1466  *  - 64-bit-long monotonically-increasing counter
   1467  *  - frequency range is 100M-4GHz (CPU speed)
   1468  */
   1469 u_int32_t machclk_freq = 0;
   1470 u_int32_t machclk_per_tick = 0;
   1471 
   1472 #if (defined(__i386__) || defined(__alpha__)) && !defined(ALTQ_NOPCC)
   1473 #ifdef __FreeBSD__
   1474 /* freebsd makes clock frequency accessible */
   1475 #ifdef __alpha__
   1476 extern u_int32_t cycles_per_sec;	/* alpha cpu clock frequency */
   1477 #endif
   1478 void
   1479 init_machclk(void)
   1480 {
   1481 #if defined(__i386__)
   1482 #if (__FreeBSD_version > 300000)
   1483 	machclk_freq = tsc_freq;
   1484 #else
   1485 	machclk_freq = i586_ctr_freq;
   1486 #endif
   1487 #elif defined(__alpha__)
   1488 	machclk_freq = cycles_per_sec;
   1489 #endif /* __alpha__ */
   1490 	machclk_per_tick = machclk_freq / hz;
   1491 }
   1492 #else /* !__FreeBSD__ */
   1493 /*
   1494  * measure Pentium TSC or Alpha PCC clock frequency
   1495  */
   1496 void
   1497 init_machclk(void)
   1498 {
   1499 	static int	wait;
   1500 	struct timeval	tv_start, tv_end;
   1501 	u_int64_t	start, end, diff;
   1502 	int		timo;
   1503 
   1504 	microtime(&tv_start);
   1505 	start = read_machclk();
   1506 	timo = hz;	/* 1 sec */
   1507 	(void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
   1508 	microtime(&tv_end);
   1509 	end = read_machclk();
   1510 	diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
   1511 		+ tv_end.tv_usec - tv_start.tv_usec;
   1512 	if (diff != 0)
   1513 		machclk_freq = (u_int)((end - start) * 1000000 / diff);
   1514 	machclk_per_tick = machclk_freq / hz;
   1515 
   1516 	printf("altq: CPU clock: %uHz\n", machclk_freq);
   1517 }
   1518 #endif /* !__FreeBSD__ */
   1519 #ifdef __alpha__
   1520 /*
   1521  * make a 64bit counter value out of the 32bit alpha processor cycle counter.
   1522  * read_machclk must be called within a half of its wrap-around cycle
   1523  * (about 5 sec for 400MHz cpu) to properly detect a counter wrap-around.
   1524  * tbr_timeout calls read_machclk once a second.
   1525  */
   1526 u_int64_t
   1527 read_machclk(void)
   1528 {
   1529 	static u_int32_t last_pcc, upper;
   1530 	u_int32_t pcc;
   1531 
   1532 	pcc = (u_int32_t)alpha_rpcc();
   1533 	if (pcc <= last_pcc)
   1534 		upper++;
   1535 	last_pcc = pcc;
   1536 	return (((u_int64_t)upper << 32) + pcc);
   1537 }
   1538 #endif /* __alpha__ */
   1539 #else /* !i386  && !alpha */
   1540 /* use microtime() for now */
   1541 void
   1542 init_machclk(void)
   1543 {
   1544 	machclk_freq = 1000000 << MACHCLK_SHIFT;
   1545 	machclk_per_tick = machclk_freq / hz;
   1546 	printf("altq: emulate %uHz cpu clock\n", machclk_freq);
   1547 }
   1548 #endif /* !i386 && !alpha */
   1549