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