2 * Copyright (C) 1997-2003
3 * Sony Computer Science Laboratories Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $KAME: altq_subr.c,v 1.21 2003/11/06 06:32:53 kjc Exp $
31 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/kernel.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/sysctl.h>
44 #include <sys/queue.h>
47 #include <net/if_var.h>
48 #include <net/if_private.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
62 #include <netpfil/pf/pf.h>
63 #include <netpfil/pf/pf_altq.h>
64 #include <net/altq/altq.h>
66 /* machine dependent clock related includes */
69 #include <sys/eventhandler.h>
70 #include <machine/clock.h>
71 #if defined(__amd64__) || defined(__i386__)
72 #include <machine/cpufunc.h> /* for pentium tsc */
73 #include <machine/specialreg.h> /* for CPUID_TSC */
74 #include <machine/md_var.h> /* for cpu_feature */
75 #endif /* __amd64 || __i386__ */
78 * internal function prototypes
80 static void tbr_timeout(void *);
81 static struct mbuf *tbr_dequeue(struct ifaltq *, int);
82 static int tbr_timer = 0; /* token bucket regulator timer */
83 static struct callout tbr_callout;
85 #ifdef ALTQ3_CLFIER_COMPAT
86 static int extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
88 static int extract_ports6(struct mbuf *, struct ip6_hdr *,
89 struct flowinfo_in6 *);
91 static int apply_filter4(u_int32_t, struct flow_filter *,
92 struct flowinfo_in *);
93 static int apply_ppfilter4(u_int32_t, struct flow_filter *,
94 struct flowinfo_in *);
96 static int apply_filter6(u_int32_t, struct flow_filter6 *,
97 struct flowinfo_in6 *);
99 static int apply_tosfilter4(u_int32_t, struct flow_filter *,
100 struct flowinfo_in *);
101 static u_long get_filt_handle(struct acc_classifier *, int);
102 static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
103 static u_int32_t filt2fibmask(struct flow_filter *);
105 static void ip4f_cache(struct ip *, struct flowinfo_in *);
106 static int ip4f_lookup(struct ip *, struct flowinfo_in *);
107 static int ip4f_init(void);
108 static struct ip4_frag *ip4f_alloc(void);
109 static void ip4f_free(struct ip4_frag *);
110 #endif /* ALTQ3_CLFIER_COMPAT */
113 SYSCTL_NODE(_kern_features, OID_AUTO, altq, CTLFLAG_RD | CTLFLAG_CAPRD, 0,
114 "ALTQ packet queuing");
116 #define ALTQ_FEATURE(name, desc) \
117 SYSCTL_INT_WITH_LABEL(_kern_features_altq, OID_AUTO, name, \
118 CTLFLAG_RD | CTLFLAG_CAPRD, SYSCTL_NULL_INT_PTR, 1, \
122 ALTQ_FEATURE(cbq, "ALTQ Class Based Queuing discipline");
125 ALTQ_FEATURE(codel, "ALTQ Controlled Delay discipline");
128 ALTQ_FEATURE(red, "ALTQ Random Early Detection discipline");
131 ALTQ_FEATURE(rio, "ALTQ Random Early Drop discipline");
134 ALTQ_FEATURE(hfsc, "ALTQ Hierarchical Packet Scheduler discipline");
137 ALTQ_FEATURE(priq, "ATLQ Priority Queuing discipline");
140 ALTQ_FEATURE(fairq, "ALTQ Fair Queuing discipline");
145 * alternate queueing support routines
148 /* look up the queue state by the interface name and the queueing type. */
150 altq_lookup(char *name, int type)
154 if ((ifp = ifunit(name)) != NULL) {
155 /* read if_snd unlocked */
156 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
157 return (ifp->if_snd.altq_disc);
164 altq_attach(struct ifaltq *ifq, int type, void *discipline,
165 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *),
166 struct mbuf *(*dequeue)(struct ifaltq *, int),
167 int (*request)(struct ifaltq *, int, void *))
170 if (!ALTQ_IS_READY(ifq)) {
175 ifq->altq_type = type;
176 ifq->altq_disc = discipline;
177 ifq->altq_enqueue = enqueue;
178 ifq->altq_dequeue = dequeue;
179 ifq->altq_request = request;
180 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
186 altq_detach(struct ifaltq *ifq)
190 if (!ALTQ_IS_READY(ifq)) {
194 if (ALTQ_IS_ENABLED(ifq)) {
198 if (!ALTQ_IS_ATTACHED(ifq)) {
203 ifq->altq_type = ALTQT_NONE;
204 ifq->altq_disc = NULL;
205 ifq->altq_enqueue = NULL;
206 ifq->altq_dequeue = NULL;
207 ifq->altq_request = NULL;
208 ifq->altq_flags &= ALTQF_CANTCHANGE;
215 altq_enable(struct ifaltq *ifq)
221 if (!ALTQ_IS_READY(ifq)) {
225 if (ALTQ_IS_ENABLED(ifq)) {
231 IFQ_PURGE_NOLOCK(ifq);
232 ASSERT(ifq->ifq_len == 0);
233 ifq->ifq_drv_maxlen = 0; /* disable bulk dequeue */
234 ifq->altq_flags |= ALTQF_ENABLED;
242 altq_disable(struct ifaltq *ifq)
247 if (!ALTQ_IS_ENABLED(ifq)) {
253 IFQ_PURGE_NOLOCK(ifq);
254 ASSERT(ifq->ifq_len == 0);
255 ifq->altq_flags &= ~(ALTQF_ENABLED);
264 altq_assert(const char *file, int line, const char *failedexpr)
266 (void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
267 failedexpr, file, line);
268 panic("altq assertion");
274 * internal representation of token bucket parameters
275 * rate: (byte_per_unittime << TBR_SHIFT) / machclk_freq
276 * (((bits_per_sec) / 8) << TBR_SHIFT) / machclk_freq
277 * depth: byte << TBR_SHIFT
281 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
282 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
285 tbr_dequeue(struct ifaltq *ifq, int op)
287 struct tb_regulator *tbr;
292 IFQ_LOCK_ASSERT(ifq);
294 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
295 /* if this is a remove after poll, bypass tbr check */
297 /* update token only when it is negative */
298 if (tbr->tbr_token <= 0) {
299 now = read_machclk();
300 interval = now - tbr->tbr_last;
301 if (interval >= tbr->tbr_filluptime)
302 tbr->tbr_token = tbr->tbr_depth;
304 tbr->tbr_token += interval * tbr->tbr_rate;
305 if (tbr->tbr_token > tbr->tbr_depth)
306 tbr->tbr_token = tbr->tbr_depth;
310 /* if token is still negative, don't allow dequeue */
311 if (tbr->tbr_token <= 0)
315 if (ALTQ_IS_ENABLED(ifq))
316 m = (*ifq->altq_dequeue)(ifq, op);
318 if (op == ALTDQ_POLL)
324 if (m != NULL && op == ALTDQ_REMOVE)
325 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
326 tbr->tbr_lastop = op;
331 * set a token bucket regulator.
332 * if the specified rate is zero, the token bucket regulator is deleted.
335 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
337 struct tb_regulator *tbr, *otbr;
339 if (tbr_dequeue_ptr == NULL)
340 tbr_dequeue_ptr = tbr_dequeue;
342 if (machclk_freq == 0)
344 if (machclk_freq == 0) {
345 printf("tbr_set: no cpu clock available!\n");
350 if (profile->rate == 0) {
351 /* delete this tbr */
352 if ((tbr = ifq->altq_tbr) == NULL) {
356 ifq->altq_tbr = NULL;
362 tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_NOWAIT | M_ZERO);
368 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
369 tbr->tbr_depth = TBR_SCALE(profile->depth);
370 if (tbr->tbr_rate > 0)
371 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
373 tbr->tbr_filluptime = LLONG_MAX;
375 * The longest time between tbr_dequeue() calls will be about 1
376 * system tick, as the callout that drives it is scheduled once per
377 * tick. The refill-time detection logic in tbr_dequeue() can only
378 * properly detect the passage of up to LLONG_MAX machclk ticks.
379 * Therefore, in order for this logic to function properly in the
380 * extreme case, the maximum value of tbr_filluptime should be
381 * LLONG_MAX less one system tick's worth of machclk ticks less
382 * some additional slop factor (here one more system tick's worth
385 if (tbr->tbr_filluptime > (LLONG_MAX - 2 * machclk_per_tick))
386 tbr->tbr_filluptime = LLONG_MAX - 2 * machclk_per_tick;
387 tbr->tbr_token = tbr->tbr_depth;
388 tbr->tbr_last = read_machclk();
389 tbr->tbr_lastop = ALTDQ_REMOVE;
391 otbr = ifq->altq_tbr;
392 ifq->altq_tbr = tbr; /* set the new tbr */
395 free(otbr, M_DEVBUF);
397 if (tbr_timer == 0) {
398 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
407 * tbr_timeout goes through the interface list, and kicks the drivers
413 tbr_timeout(void *arg)
415 VNET_ITERATOR_DECL(vnet_iter);
417 struct epoch_tracker et;
422 VNET_LIST_RLOCK_NOSLEEP();
423 VNET_FOREACH(vnet_iter) {
424 CURVNET_SET(vnet_iter);
425 for (ifp = CK_STAILQ_FIRST(&V_ifnet); ifp;
426 ifp = CK_STAILQ_NEXT(ifp, if_link)) {
427 /* read from if_snd unlocked */
428 if (!TBR_IS_ENABLED(&ifp->if_snd))
431 if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
432 ifp->if_start != NULL)
433 (*ifp->if_start)(ifp);
437 VNET_LIST_RUNLOCK_NOSLEEP();
440 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
442 tbr_timer = 0; /* don't need tbr_timer anymore */
446 * attach a discipline to the interface. if one already exists, it is
448 * Locking is done in the discipline specific attach functions. Basically
449 * they call back to altq_attach which takes care of the attach and locking.
452 altq_pfattach(struct pf_altq *a)
456 switch (a->scheduler) {
461 error = cbq_pfattach(a);
466 error = priq_pfattach(a);
471 error = hfsc_pfattach(a);
476 error = fairq_pfattach(a);
481 error = codel_pfattach(a);
492 * detach a discipline from the interface.
493 * it is possible that the discipline was already overridden by another
497 altq_pfdetach(struct pf_altq *a)
502 if ((ifp = ifunit(a->ifname)) == NULL)
505 /* if this discipline is no longer referenced, just return */
506 /* read unlocked from if_snd */
507 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
511 /* read unlocked from if_snd, _disable and _detach take care */
512 if (ALTQ_IS_ENABLED(&ifp->if_snd))
513 error = altq_disable(&ifp->if_snd);
515 error = altq_detach(&ifp->if_snd);
522 * add a discipline or a queue
523 * Locking is done in the discipline specific functions with regards to
524 * malloc with WAITOK, also it is not yet clear which lock to use.
527 altq_add(struct ifnet *ifp, struct pf_altq *a)
531 if (a->qname[0] != 0)
532 return (altq_add_queue(a));
534 if (machclk_freq == 0)
536 if (machclk_freq == 0)
537 panic("altq_add: no cpu clock");
539 switch (a->scheduler) {
542 error = cbq_add_altq(ifp, a);
547 error = priq_add_altq(ifp, a);
552 error = hfsc_add_altq(ifp, a);
557 error = fairq_add_altq(ifp, a);
562 error = codel_add_altq(ifp, a);
573 * remove a discipline or a queue
574 * It is yet unclear what lock to use to protect this operation, the
575 * discipline specific functions will determine and grab it
578 altq_remove(struct pf_altq *a)
582 if (a->qname[0] != 0)
583 return (altq_remove_queue(a));
585 switch (a->scheduler) {
588 error = cbq_remove_altq(a);
593 error = priq_remove_altq(a);
598 error = hfsc_remove_altq(a);
603 error = fairq_remove_altq(a);
608 error = codel_remove_altq(a);
619 * add a queue to the discipline
620 * It is yet unclear what lock to use to protect this operation, the
621 * discipline specific functions will determine and grab it
624 altq_add_queue(struct pf_altq *a)
628 switch (a->scheduler) {
631 error = cbq_add_queue(a);
636 error = priq_add_queue(a);
641 error = hfsc_add_queue(a);
646 error = fairq_add_queue(a);
657 * remove a queue from the discipline
658 * It is yet unclear what lock to use to protect this operation, the
659 * discipline specific functions will determine and grab it
662 altq_remove_queue(struct pf_altq *a)
666 switch (a->scheduler) {
669 error = cbq_remove_queue(a);
674 error = priq_remove_queue(a);
679 error = hfsc_remove_queue(a);
684 error = fairq_remove_queue(a);
695 * get queue statistics
696 * Locking is done in the discipline specific functions with regards to
697 * copyout operations, also it is not yet clear which lock to use.
700 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
704 switch (a->scheduler) {
707 error = cbq_getqstats(a, ubuf, nbytes, version);
712 error = priq_getqstats(a, ubuf, nbytes, version);
717 error = hfsc_getqstats(a, ubuf, nbytes, version);
722 error = fairq_getqstats(a, ubuf, nbytes, version);
727 error = codel_getqstats(a, ubuf, nbytes, version);
738 * read and write diffserv field in IPv4 or IPv6 header
741 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
744 u_int8_t ds_field = 0;
746 if (pktattr == NULL ||
747 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
748 return ((u_int8_t)0);
750 /* verify that pattr_hdr is within the mbuf data */
751 for (m0 = m; m0 != NULL; m0 = m0->m_next)
752 if ((pktattr->pattr_hdr >= m0->m_data) &&
753 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
756 /* ick, pattr_hdr is stale */
757 pktattr->pattr_af = AF_UNSPEC;
759 printf("read_dsfield: can't locate header!\n");
761 return ((u_int8_t)0);
764 if (pktattr->pattr_af == AF_INET) {
765 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
768 return ((u_int8_t)0); /* version mismatch! */
769 ds_field = ip->ip_tos;
772 else if (pktattr->pattr_af == AF_INET6) {
773 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
776 flowlabel = ntohl(ip6->ip6_flow);
777 if ((flowlabel >> 28) != 6)
778 return ((u_int8_t)0); /* version mismatch! */
779 ds_field = (flowlabel >> 20) & 0xff;
786 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
790 if (pktattr == NULL ||
791 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
794 /* verify that pattr_hdr is within the mbuf data */
795 for (m0 = m; m0 != NULL; m0 = m0->m_next)
796 if ((pktattr->pattr_hdr >= m0->m_data) &&
797 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
800 /* ick, pattr_hdr is stale */
801 pktattr->pattr_af = AF_UNSPEC;
803 printf("write_dsfield: can't locate header!\n");
808 if (pktattr->pattr_af == AF_INET) {
809 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
814 return; /* version mismatch! */
816 dsfield |= old & 3; /* leave CU bits */
819 ip->ip_tos = dsfield;
821 * update checksum (from RFC1624)
822 * HC' = ~(~HC + ~m + m')
824 sum = ~ntohs(ip->ip_sum) & 0xffff;
825 sum += 0xff00 + (~old & 0xff) + dsfield;
826 sum = (sum >> 16) + (sum & 0xffff);
827 sum += (sum >> 16); /* add carry */
829 ip->ip_sum = htons(~sum & 0xffff);
832 else if (pktattr->pattr_af == AF_INET6) {
833 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
836 flowlabel = ntohl(ip6->ip6_flow);
837 if ((flowlabel >> 28) != 6)
838 return; /* version mismatch! */
839 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
840 ip6->ip6_flow = htonl(flowlabel);
847 * high resolution clock support taking advantage of a machine dependent
848 * high resolution time counter (e.g., timestamp counter of intel pentium).
850 * - 64-bit-long monotonically-increasing counter
851 * - frequency range is 100M-4GHz (CPU speed)
853 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
854 #define MACHCLK_SHIFT 8
857 u_int32_t machclk_freq;
858 u_int32_t machclk_per_tick;
860 #if defined(__i386__) && defined(__NetBSD__)
861 extern u_int64_t cpu_tsc_freq;
864 /* Update TSC freq with the value indicated by the caller. */
866 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
868 /* If there was an error during the transition, don't do anything. */
872 #if defined(__amd64__) || defined(__i386__)
873 /* If TSC is P-state invariant, don't do anything. */
874 if (tsc_is_invariant)
878 /* Total setting for this level gives the new frequency in MHz. */
881 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
882 EVENTHANDLER_PRI_LAST);
885 init_machclk_setup(void)
887 callout_init(&tbr_callout, 1);
891 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
894 #if defined(__FreeBSD__) && defined(SMP)
897 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
900 #if defined(__amd64__) || defined(__i386__)
901 /* check if TSC is available */
902 if ((cpu_feature & CPUID_TSC) == 0 ||
903 atomic_load_acq_64(&tsc_freq) == 0)
913 /* Call one-time initialization function. */
915 init_machclk_setup();
919 if (machclk_usepcc == 0) {
920 /* emulate 256MHz using microtime() */
921 machclk_freq = 1000000 << MACHCLK_SHIFT;
922 machclk_per_tick = machclk_freq / hz;
924 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
930 * if the clock frequency (of Pentium TSC or Alpha PCC) is
931 * accessible, just use it.
933 #if defined(__amd64__) || defined(__i386__)
934 machclk_freq = atomic_load_acq_64(&tsc_freq);
938 * if we don't know the clock frequency, measure it.
940 if (machclk_freq == 0) {
942 struct timeval tv_start, tv_end;
943 u_int64_t start, end, diff;
946 microtime(&tv_start);
947 start = read_machclk();
948 timo = hz; /* 1 sec */
949 (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
951 end = read_machclk();
952 diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
953 + tv_end.tv_usec - tv_start.tv_usec;
955 machclk_freq = (u_int)((end - start) * 1000000 / diff);
958 machclk_per_tick = machclk_freq / hz;
961 printf("altq: CPU clock: %uHz\n", machclk_freq);
965 #if defined(__OpenBSD__) && defined(__i386__)
966 static __inline u_int64_t
970 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
973 #endif /* __OpenBSD__ && __i386__ */
980 if (machclk_usepcc) {
981 #if defined(__amd64__) || defined(__i386__)
984 panic("read_machclk");
987 struct timeval tv, boottime;
990 getboottime(&boottime);
991 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
992 + tv.tv_usec) << MACHCLK_SHIFT);
997 #ifdef ALTQ3_CLFIER_COMPAT
1000 #define IPPROTO_ESP 50 /* encapsulating security payload */
1003 #define IPPROTO_AH 51 /* authentication header */
1007 * extract flow information from a given packet.
1008 * filt_mask shows flowinfo fields required.
1009 * we assume the ip header is in one mbuf, and addresses and ports are
1010 * in network byte order.
1013 altq_extractflow(m, af, flow, filt_bmask)
1016 struct flowinfo *flow;
1017 u_int32_t filt_bmask;
1022 struct flowinfo_in *fin;
1025 ip = mtod(m, struct ip *);
1030 fin = (struct flowinfo_in *)flow;
1031 fin->fi_len = sizeof(struct flowinfo_in);
1032 fin->fi_family = AF_INET;
1034 fin->fi_proto = ip->ip_p;
1035 fin->fi_tos = ip->ip_tos;
1037 fin->fi_src.s_addr = ip->ip_src.s_addr;
1038 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1040 if (filt_bmask & FIMB4_PORTS)
1041 /* if port info is required, extract port numbers */
1042 extract_ports4(m, ip, fin);
1053 struct flowinfo_in6 *fin6;
1054 struct ip6_hdr *ip6;
1056 ip6 = mtod(m, struct ip6_hdr *);
1057 /* should we check the ip version? */
1059 fin6 = (struct flowinfo_in6 *)flow;
1060 fin6->fi6_len = sizeof(struct flowinfo_in6);
1061 fin6->fi6_family = AF_INET6;
1063 fin6->fi6_proto = ip6->ip6_nxt;
1064 fin6->fi6_tclass = IPV6_TRAFFIC_CLASS(ip6);
1066 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1067 fin6->fi6_src = ip6->ip6_src;
1068 fin6->fi6_dst = ip6->ip6_dst;
1070 if ((filt_bmask & FIMB6_PORTS) ||
1071 ((filt_bmask & FIMB6_PROTO)
1072 && ip6->ip6_nxt > IPPROTO_IPV6))
1074 * if port info is required, or proto is required
1075 * but there are option headers, extract port
1076 * and protocol numbers.
1078 extract_ports6(m, ip6, fin6);
1080 fin6->fi6_sport = 0;
1081 fin6->fi6_dport = 0;
1093 flow->fi_len = sizeof(struct flowinfo);
1094 flow->fi_family = AF_UNSPEC;
1099 * helper routine to extract port numbers
1101 /* structure for ipsec and ipv6 option header template */
1103 u_int8_t opt6_nxt; /* next header */
1104 u_int8_t opt6_hlen; /* header extension length */
1106 u_int32_t ah_spi; /* security parameter index
1107 for authentication header */
1111 * extract port numbers from a ipv4 packet.
1114 extract_ports4(m, ip, fin)
1117 struct flowinfo_in *fin;
1128 ip_off = ntohs(ip->ip_off);
1129 /* if it is a fragment, try cached fragment info */
1130 if (ip_off & IP_OFFMASK) {
1131 ip4f_lookup(ip, fin);
1135 /* locate the mbuf containing the protocol header */
1136 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1137 if (((caddr_t)ip >= m0->m_data) &&
1138 ((caddr_t)ip < m0->m_data + m0->m_len))
1142 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1146 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1152 while (off >= m0->m_len) {
1156 return (0); /* bogus ip_hl! */
1158 if (m0->m_len < off + 4)
1166 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1167 fin->fi_sport = udp->uh_sport;
1168 fin->fi_dport = udp->uh_dport;
1169 fin->fi_proto = proto;
1175 if (fin->fi_gpi == 0){
1178 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1181 fin->fi_proto = proto;
1185 /* get next header and header length */
1188 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1189 proto = opt6->opt6_nxt;
1190 off += 8 + (opt6->opt6_hlen * 4);
1191 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1192 fin->fi_gpi = opt6->ah_spi;
1194 /* goto the next header */
1196 #endif /* ALTQ_IPSEC */
1199 fin->fi_proto = proto;
1203 /* if this is a first fragment, cache it. */
1205 ip4f_cache(ip, fin);
1212 extract_ports6(m, ip6, fin6)
1214 struct ip6_hdr *ip6;
1215 struct flowinfo_in6 *fin6;
1222 fin6->fi6_sport = 0;
1223 fin6->fi6_dport = 0;
1225 /* locate the mbuf containing the protocol header */
1226 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1227 if (((caddr_t)ip6 >= m0->m_data) &&
1228 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1232 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1236 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1238 proto = ip6->ip6_nxt;
1240 while (off >= m0->m_len) {
1246 if (m0->m_len < off + 4)
1254 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1255 fin6->fi6_sport = udp->uh_sport;
1256 fin6->fi6_dport = udp->uh_dport;
1257 fin6->fi6_proto = proto;
1262 if (fin6->fi6_gpi == 0) {
1265 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1266 fin6->fi6_gpi = *gpi;
1268 fin6->fi6_proto = proto;
1272 /* get next header and header length */
1275 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1276 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1277 fin6->fi6_gpi = opt6->ah_spi;
1278 proto = opt6->opt6_nxt;
1279 off += 8 + (opt6->opt6_hlen * 4);
1280 /* goto the next header */
1284 case IPPROTO_HOPOPTS:
1285 case IPPROTO_ROUTING:
1286 case IPPROTO_DSTOPTS: {
1287 /* get next header and header length */
1290 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1291 proto = opt6->opt6_nxt;
1292 off += (opt6->opt6_hlen + 1) * 8;
1293 /* goto the next header */
1297 case IPPROTO_FRAGMENT:
1298 /* ipv6 fragmentations are not supported yet */
1300 fin6->fi6_proto = proto;
1309 * altq common classifier
1312 acc_add_filter(classifier, filter, class, phandle)
1313 struct acc_classifier *classifier;
1314 struct flow_filter *filter;
1318 struct acc_filter *afp, *prev, *tmp;
1322 if (filter->ff_flow.fi_family != AF_INET &&
1323 filter->ff_flow.fi_family != AF_INET6)
1326 if (filter->ff_flow.fi_family != AF_INET)
1330 afp = malloc(sizeof(struct acc_filter),
1331 M_DEVBUF, M_WAITOK);
1334 bzero(afp, sizeof(struct acc_filter));
1336 afp->f_filter = *filter;
1337 afp->f_class = class;
1339 i = ACC_WILDCARD_INDEX;
1340 if (filter->ff_flow.fi_family == AF_INET) {
1341 struct flow_filter *filter4 = &afp->f_filter;
1344 * if address is 0, it's a wildcard. if address mask
1345 * isn't set, use full mask.
1347 if (filter4->ff_flow.fi_dst.s_addr == 0)
1348 filter4->ff_mask.mask_dst.s_addr = 0;
1349 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1350 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1351 if (filter4->ff_flow.fi_src.s_addr == 0)
1352 filter4->ff_mask.mask_src.s_addr = 0;
1353 else if (filter4->ff_mask.mask_src.s_addr == 0)
1354 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1356 /* clear extra bits in addresses */
1357 filter4->ff_flow.fi_dst.s_addr &=
1358 filter4->ff_mask.mask_dst.s_addr;
1359 filter4->ff_flow.fi_src.s_addr &=
1360 filter4->ff_mask.mask_src.s_addr;
1363 * if dst address is a wildcard, use hash-entry
1364 * ACC_WILDCARD_INDEX.
1366 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1367 i = ACC_WILDCARD_INDEX;
1369 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1372 else if (filter->ff_flow.fi_family == AF_INET6) {
1373 struct flow_filter6 *filter6 =
1374 (struct flow_filter6 *)&afp->f_filter;
1375 #ifndef IN6MASK0 /* taken from kame ipv6 */
1376 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1377 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1378 const struct in6_addr in6mask0 = IN6MASK0;
1379 const struct in6_addr in6mask128 = IN6MASK128;
1382 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1383 filter6->ff_mask6.mask6_dst = in6mask0;
1384 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1385 filter6->ff_mask6.mask6_dst = in6mask128;
1386 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1387 filter6->ff_mask6.mask6_src = in6mask0;
1388 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1389 filter6->ff_mask6.mask6_src = in6mask128;
1391 /* clear extra bits in addresses */
1392 for (i = 0; i < 16; i++)
1393 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1394 filter6->ff_mask6.mask6_dst.s6_addr[i];
1395 for (i = 0; i < 16; i++)
1396 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1397 filter6->ff_mask6.mask6_src.s6_addr[i];
1399 if (filter6->ff_flow6.fi6_flowlabel == 0)
1400 i = ACC_WILDCARD_INDEX;
1402 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1406 afp->f_handle = get_filt_handle(classifier, i);
1408 /* update filter bitmask */
1409 afp->f_fbmask = filt2fibmask(filter);
1410 classifier->acc_fbmask |= afp->f_fbmask;
1413 * add this filter to the filter list.
1414 * filters are ordered from the highest rule number.
1418 LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1419 if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1425 LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1427 LIST_INSERT_AFTER(prev, afp, f_chain);
1430 *phandle = afp->f_handle;
1435 acc_delete_filter(classifier, handle)
1436 struct acc_classifier *classifier;
1439 struct acc_filter *afp;
1442 if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1446 LIST_REMOVE(afp, f_chain);
1449 free(afp, M_DEVBUF);
1451 /* todo: update filt_bmask */
1457 * delete filters referencing to the specified class.
1458 * if the all flag is not 0, delete all the filters.
1461 acc_discard_filters(classifier, class, all)
1462 struct acc_classifier *classifier;
1466 struct acc_filter *afp;
1470 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1472 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1473 if (all || afp->f_class == class) {
1474 LIST_REMOVE(afp, f_chain);
1475 free(afp, M_DEVBUF);
1476 /* start again from the head */
1479 } while (afp != NULL);
1484 classifier->acc_fbmask = 0;
1490 acc_classify(clfier, m, af)
1495 struct acc_classifier *classifier;
1496 struct flowinfo flow;
1497 struct acc_filter *afp;
1500 classifier = (struct acc_classifier *)clfier;
1501 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1503 if (flow.fi_family == AF_INET) {
1504 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1506 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1507 /* only tos is used */
1509 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1511 if (apply_tosfilter4(afp->f_fbmask,
1512 &afp->f_filter, fp))
1513 /* filter matched */
1514 return (afp->f_class);
1515 } else if ((classifier->acc_fbmask &
1516 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1518 /* only proto and ports are used */
1520 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1522 if (apply_ppfilter4(afp->f_fbmask,
1523 &afp->f_filter, fp))
1524 /* filter matched */
1525 return (afp->f_class);
1527 /* get the filter hash entry from its dest address */
1528 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1531 * go through this loop twice. first for dst
1532 * hash, second for wildcards.
1534 LIST_FOREACH(afp, &classifier->acc_filters[i],
1536 if (apply_filter4(afp->f_fbmask,
1537 &afp->f_filter, fp))
1538 /* filter matched */
1539 return (afp->f_class);
1542 * check again for filters with a dst addr
1544 * (daddr == 0 || dmask != 0xffffffff).
1546 if (i != ACC_WILDCARD_INDEX)
1547 i = ACC_WILDCARD_INDEX;
1554 else if (flow.fi_family == AF_INET6) {
1555 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1557 /* get the filter hash entry from its flow ID */
1558 if (fp6->fi6_flowlabel != 0)
1559 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1561 /* flowlable can be zero */
1562 i = ACC_WILDCARD_INDEX;
1564 /* go through this loop twice. first for flow hash, second
1567 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1568 if (apply_filter6(afp->f_fbmask,
1569 (struct flow_filter6 *)&afp->f_filter,
1571 /* filter matched */
1572 return (afp->f_class);
1575 * check again for filters with a wildcard.
1577 if (i != ACC_WILDCARD_INDEX)
1578 i = ACC_WILDCARD_INDEX;
1585 /* no filter matched */
1590 apply_filter4(fbmask, filt, pkt)
1592 struct flow_filter *filt;
1593 struct flowinfo_in *pkt;
1595 if (filt->ff_flow.fi_family != AF_INET)
1597 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1599 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1601 if ((fbmask & FIMB4_DADDR) &&
1602 filt->ff_flow.fi_dst.s_addr !=
1603 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1605 if ((fbmask & FIMB4_SADDR) &&
1606 filt->ff_flow.fi_src.s_addr !=
1607 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1609 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1611 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1612 (pkt->fi_tos & filt->ff_mask.mask_tos))
1614 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1621 * filter matching function optimized for a common case that checks
1622 * only protocol and port numbers
1625 apply_ppfilter4(fbmask, filt, pkt)
1627 struct flow_filter *filt;
1628 struct flowinfo_in *pkt;
1630 if (filt->ff_flow.fi_family != AF_INET)
1632 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1634 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1636 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1643 * filter matching function only for tos field.
1646 apply_tosfilter4(fbmask, filt, pkt)
1648 struct flow_filter *filt;
1649 struct flowinfo_in *pkt;
1651 if (filt->ff_flow.fi_family != AF_INET)
1653 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1654 (pkt->fi_tos & filt->ff_mask.mask_tos))
1662 apply_filter6(fbmask, filt, pkt)
1664 struct flow_filter6 *filt;
1665 struct flowinfo_in6 *pkt;
1669 if (filt->ff_flow6.fi6_family != AF_INET6)
1671 if ((fbmask & FIMB6_FLABEL) &&
1672 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1674 if ((fbmask & FIMB6_PROTO) &&
1675 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1677 if ((fbmask & FIMB6_SPORT) &&
1678 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1680 if ((fbmask & FIMB6_DPORT) &&
1681 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1683 if (fbmask & FIMB6_SADDR) {
1684 for (i = 0; i < 4; i++)
1685 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1686 (pkt->fi6_src.s6_addr32[i] &
1687 filt->ff_mask6.mask6_src.s6_addr32[i]))
1690 if (fbmask & FIMB6_DADDR) {
1691 for (i = 0; i < 4; i++)
1692 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1693 (pkt->fi6_dst.s6_addr32[i] &
1694 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1697 if ((fbmask & FIMB6_TCLASS) &&
1698 filt->ff_flow6.fi6_tclass !=
1699 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1701 if ((fbmask & FIMB6_GPI) &&
1702 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1711 * bit 20-28: index to the filter hash table
1712 * bit 0-19: unique id in the hash bucket.
1715 get_filt_handle(classifier, i)
1716 struct acc_classifier *classifier;
1719 static u_long handle_number = 1;
1721 struct acc_filter *afp;
1724 handle = handle_number++ & 0x000fffff;
1726 if (LIST_EMPTY(&classifier->acc_filters[i]))
1729 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1730 if ((afp->f_handle & 0x000fffff) == handle)
1734 /* this handle is already used, try again */
1737 return ((i << 20) | handle);
1740 /* convert filter handle to filter pointer */
1741 static struct acc_filter *
1742 filth_to_filtp(classifier, handle)
1743 struct acc_classifier *classifier;
1746 struct acc_filter *afp;
1749 i = ACC_GET_HINDEX(handle);
1751 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1752 if (afp->f_handle == handle)
1758 /* create flowinfo bitmask */
1761 struct flow_filter *filt;
1765 struct flow_filter6 *filt6;
1768 switch (filt->ff_flow.fi_family) {
1770 if (filt->ff_flow.fi_proto != 0)
1771 mask |= FIMB4_PROTO;
1772 if (filt->ff_flow.fi_tos != 0)
1774 if (filt->ff_flow.fi_dst.s_addr != 0)
1775 mask |= FIMB4_DADDR;
1776 if (filt->ff_flow.fi_src.s_addr != 0)
1777 mask |= FIMB4_SADDR;
1778 if (filt->ff_flow.fi_sport != 0)
1779 mask |= FIMB4_SPORT;
1780 if (filt->ff_flow.fi_dport != 0)
1781 mask |= FIMB4_DPORT;
1782 if (filt->ff_flow.fi_gpi != 0)
1787 filt6 = (struct flow_filter6 *)filt;
1789 if (filt6->ff_flow6.fi6_proto != 0)
1790 mask |= FIMB6_PROTO;
1791 if (filt6->ff_flow6.fi6_tclass != 0)
1792 mask |= FIMB6_TCLASS;
1793 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1794 mask |= FIMB6_DADDR;
1795 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1796 mask |= FIMB6_SADDR;
1797 if (filt6->ff_flow6.fi6_sport != 0)
1798 mask |= FIMB6_SPORT;
1799 if (filt6->ff_flow6.fi6_dport != 0)
1800 mask |= FIMB6_DPORT;
1801 if (filt6->ff_flow6.fi6_gpi != 0)
1803 if (filt6->ff_flow6.fi6_flowlabel != 0)
1804 mask |= FIMB6_FLABEL;
1812 * helper functions to handle IPv4 fragments.
1813 * currently only in-sequence fragments are handled.
1814 * - fragment info is cached in a LRU list.
1815 * - when a first fragment is found, cache its flow info.
1816 * - when a non-first fragment is found, lookup the cache.
1820 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1823 struct flowinfo_in ip4f_info;
1826 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1828 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1833 struct flowinfo_in *fin;
1835 struct ip4_frag *fp;
1837 if (TAILQ_EMPTY(&ip4f_list)) {
1838 /* first time call, allocate fragment cache entries. */
1839 if (ip4f_init() < 0)
1840 /* allocation failed! */
1845 fp->ip4f_id = ip->ip_id;
1846 fp->ip4f_info.fi_proto = ip->ip_p;
1847 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1848 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1850 /* save port numbers */
1851 fp->ip4f_info.fi_sport = fin->fi_sport;
1852 fp->ip4f_info.fi_dport = fin->fi_dport;
1853 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1857 ip4f_lookup(ip, fin)
1859 struct flowinfo_in *fin;
1861 struct ip4_frag *fp;
1863 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1864 fp = TAILQ_NEXT(fp, ip4f_chain))
1865 if (ip->ip_id == fp->ip4f_id &&
1866 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1867 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1868 ip->ip_p == fp->ip4f_info.fi_proto) {
1869 /* found the matching entry */
1870 fin->fi_sport = fp->ip4f_info.fi_sport;
1871 fin->fi_dport = fp->ip4f_info.fi_dport;
1872 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1874 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1875 /* this is the last fragment,
1876 release the entry. */
1882 /* no matching entry found */
1889 struct ip4_frag *fp;
1892 TAILQ_INIT(&ip4f_list);
1893 for (i=0; i<IP4F_TABSIZE; i++) {
1894 fp = malloc(sizeof(struct ip4_frag),
1895 M_DEVBUF, M_NOWAIT);
1897 printf("ip4f_init: can't alloc %dth entry!\n", i);
1903 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1908 static struct ip4_frag *
1911 struct ip4_frag *fp;
1913 /* reclaim an entry at the tail, put it at the head */
1914 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1915 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1917 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1923 struct ip4_frag *fp;
1925 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1927 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1930 #endif /* ALTQ3_CLFIER_COMPAT */