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