2 /* $KAME: altq_subr.c,v 1.21 2003/11/06 06:32:53 kjc Exp $ */
5 * Copyright (C) 1997-2003
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(__FreeBSD__) || defined(__NetBSD__)
34 #include "opt_inet6.h"
36 #endif /* __FreeBSD__ || __NetBSD__ */
38 #include <sys/param.h>
39 #include <sys/malloc.h>
41 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
48 #include <sys/sysctl.h>
49 #include <sys/queue.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
63 #include <netinet/ip6.h>
65 #include <netinet/tcp.h>
66 #include <netinet/udp.h>
68 #include <netpfil/pf/pf.h>
69 #include <netpfil/pf/pf_altq.h>
70 #include <altq/altq.h>
72 #include <altq/altq_conf.h>
75 /* machine dependent clock related includes */
79 #include <sys/eventhandler.h>
80 #include <machine/clock.h>
82 #if defined(__amd64__) || defined(__i386__)
83 #include <machine/cpufunc.h> /* for pentium tsc */
84 #include <machine/specialreg.h> /* for CPUID_TSC */
86 #include <machine/md_var.h> /* for cpu_feature */
87 #elif defined(__NetBSD__) || defined(__OpenBSD__)
88 #include <machine/cpu.h> /* for cpu_feature */
90 #endif /* __amd64 || __i386__ */
93 * internal function prototypes
95 static void tbr_timeout(void *);
96 int (*altq_input)(struct mbuf *, int) = NULL;
97 static struct mbuf *tbr_dequeue(struct ifaltq *, int);
98 static int tbr_timer = 0; /* token bucket regulator timer */
99 #if !defined(__FreeBSD__) || (__FreeBSD_version < 600000)
100 static struct callout tbr_callout = CALLOUT_INITIALIZER;
102 static struct callout tbr_callout;
105 #ifdef ALTQ3_CLFIER_COMPAT
106 static int extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
108 static int extract_ports6(struct mbuf *, struct ip6_hdr *,
109 struct flowinfo_in6 *);
111 static int apply_filter4(u_int32_t, struct flow_filter *,
112 struct flowinfo_in *);
113 static int apply_ppfilter4(u_int32_t, struct flow_filter *,
114 struct flowinfo_in *);
116 static int apply_filter6(u_int32_t, struct flow_filter6 *,
117 struct flowinfo_in6 *);
119 static int apply_tosfilter4(u_int32_t, struct flow_filter *,
120 struct flowinfo_in *);
121 static u_long get_filt_handle(struct acc_classifier *, int);
122 static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
123 static u_int32_t filt2fibmask(struct flow_filter *);
125 static void ip4f_cache(struct ip *, struct flowinfo_in *);
126 static int ip4f_lookup(struct ip *, struct flowinfo_in *);
127 static int ip4f_init(void);
128 static struct ip4_frag *ip4f_alloc(void);
129 static void ip4f_free(struct ip4_frag *);
130 #endif /* ALTQ3_CLFIER_COMPAT */
133 * alternate queueing support routines
136 /* look up the queue state by the interface name and the queueing type. */
138 altq_lookup(name, type)
144 if ((ifp = ifunit(name)) != NULL) {
145 /* read if_snd unlocked */
146 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
147 return (ifp->if_snd.altq_disc);
154 altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
158 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
159 struct mbuf *(*dequeue)(struct ifaltq *, int);
160 int (*request)(struct ifaltq *, int, void *);
162 void *(*classify)(void *, struct mbuf *, int);
165 if (!ALTQ_IS_READY(ifq)) {
172 * pfaltq can override the existing discipline, but altq3 cannot.
173 * check these if clfier is not NULL (which implies altq3).
175 if (clfier != NULL) {
176 if (ALTQ_IS_ENABLED(ifq)) {
180 if (ALTQ_IS_ATTACHED(ifq)) {
186 ifq->altq_type = type;
187 ifq->altq_disc = discipline;
188 ifq->altq_enqueue = enqueue;
189 ifq->altq_dequeue = dequeue;
190 ifq->altq_request = request;
191 ifq->altq_clfier = clfier;
192 ifq->altq_classify = classify;
193 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
196 altq_module_incref(type);
209 if (!ALTQ_IS_READY(ifq)) {
213 if (ALTQ_IS_ENABLED(ifq)) {
217 if (!ALTQ_IS_ATTACHED(ifq)) {
223 altq_module_declref(ifq->altq_type);
227 ifq->altq_type = ALTQT_NONE;
228 ifq->altq_disc = NULL;
229 ifq->altq_enqueue = NULL;
230 ifq->altq_dequeue = NULL;
231 ifq->altq_request = NULL;
232 ifq->altq_clfier = NULL;
233 ifq->altq_classify = NULL;
234 ifq->altq_flags &= ALTQF_CANTCHANGE;
248 if (!ALTQ_IS_READY(ifq)) {
252 if (ALTQ_IS_ENABLED(ifq)) {
262 IFQ_PURGE_NOLOCK(ifq);
263 ASSERT(ifq->ifq_len == 0);
264 ifq->ifq_drv_maxlen = 0; /* disable bulk dequeue */
265 ifq->altq_flags |= ALTQF_ENABLED;
266 if (ifq->altq_clfier != NULL)
267 ifq->altq_flags |= ALTQF_CLASSIFY;
281 if (!ALTQ_IS_ENABLED(ifq)) {
291 IFQ_PURGE_NOLOCK(ifq);
292 ASSERT(ifq->ifq_len == 0);
293 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
302 altq_assert(file, line, failedexpr)
303 const char *file, *failedexpr;
306 (void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
307 failedexpr, file, line);
308 panic("altq assertion");
314 * internal representation of token bucket parameters
315 * rate: byte_per_unittime << 32
316 * (((bits_per_sec) / 8) << 32) / machclk_freq
321 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
322 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
329 struct tb_regulator *tbr;
334 IFQ_LOCK_ASSERT(ifq);
336 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
337 /* if this is a remove after poll, bypass tbr check */
339 /* update token only when it is negative */
340 if (tbr->tbr_token <= 0) {
341 now = read_machclk();
342 interval = now - tbr->tbr_last;
343 if (interval >= tbr->tbr_filluptime)
344 tbr->tbr_token = tbr->tbr_depth;
346 tbr->tbr_token += interval * tbr->tbr_rate;
347 if (tbr->tbr_token > tbr->tbr_depth)
348 tbr->tbr_token = tbr->tbr_depth;
352 /* if token is still negative, don't allow dequeue */
353 if (tbr->tbr_token <= 0)
357 if (ALTQ_IS_ENABLED(ifq))
358 m = (*ifq->altq_dequeue)(ifq, op);
360 if (op == ALTDQ_POLL)
366 if (m != NULL && op == ALTDQ_REMOVE)
367 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
368 tbr->tbr_lastop = op;
373 * set a token bucket regulator.
374 * if the specified rate is zero, the token bucket regulator is deleted.
377 tbr_set(ifq, profile)
379 struct tb_profile *profile;
381 struct tb_regulator *tbr, *otbr;
383 if (tbr_dequeue_ptr == NULL)
384 tbr_dequeue_ptr = tbr_dequeue;
386 if (machclk_freq == 0)
388 if (machclk_freq == 0) {
389 printf("tbr_set: no cpu clock available!\n");
394 if (profile->rate == 0) {
395 /* delete this tbr */
396 if ((tbr = ifq->altq_tbr) == NULL) {
400 ifq->altq_tbr = NULL;
406 tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_NOWAIT | M_ZERO);
412 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
413 tbr->tbr_depth = TBR_SCALE(profile->depth);
414 if (tbr->tbr_rate > 0)
415 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
417 tbr->tbr_filluptime = 0xffffffffffffffffLL;
418 tbr->tbr_token = tbr->tbr_depth;
419 tbr->tbr_last = read_machclk();
420 tbr->tbr_lastop = ALTDQ_REMOVE;
422 otbr = ifq->altq_tbr;
423 ifq->altq_tbr = tbr; /* set the new tbr */
426 free(otbr, M_DEVBUF);
428 if (tbr_timer == 0) {
429 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
438 * tbr_timeout goes through the interface list, and kicks the drivers
448 VNET_ITERATOR_DECL(vnet_iter);
460 IFNET_RLOCK_NOSLEEP();
461 VNET_LIST_RLOCK_NOSLEEP();
462 VNET_FOREACH(vnet_iter) {
463 CURVNET_SET(vnet_iter);
465 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
466 ifp = TAILQ_NEXT(ifp, if_list)) {
467 /* read from if_snd unlocked */
468 if (!TBR_IS_ENABLED(&ifp->if_snd))
471 if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
472 ifp->if_start != NULL)
473 (*ifp->if_start)(ifp);
478 VNET_LIST_RUNLOCK_NOSLEEP();
479 IFNET_RUNLOCK_NOSLEEP();
483 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
485 tbr_timer = 0; /* don't need tbr_timer anymore */
489 * get token bucket regulator profile
492 tbr_get(ifq, profile)
494 struct tb_profile *profile;
496 struct tb_regulator *tbr;
499 if ((tbr = ifq->altq_tbr) == NULL) {
504 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
505 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
512 * attach a discipline to the interface. if one already exists, it is
514 * Locking is done in the discipline specific attach functions. Basically
515 * they call back to altq_attach which takes care of the attach and locking.
518 altq_pfattach(struct pf_altq *a)
522 switch (a->scheduler) {
527 error = cbq_pfattach(a);
532 error = priq_pfattach(a);
537 error = hfsc_pfattach(a);
548 * detach a discipline from the interface.
549 * it is possible that the discipline was already overridden by another
553 altq_pfdetach(struct pf_altq *a)
558 if ((ifp = ifunit(a->ifname)) == NULL)
561 /* if this discipline is no longer referenced, just return */
562 /* read unlocked from if_snd */
563 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
571 /* read unlocked from if_snd, _disable and _detach take care */
572 if (ALTQ_IS_ENABLED(&ifp->if_snd))
573 error = altq_disable(&ifp->if_snd);
575 error = altq_detach(&ifp->if_snd);
582 * add a discipline or a queue
583 * Locking is done in the discipline specific functions with regards to
584 * malloc with WAITOK, also it is not yet clear which lock to use.
587 altq_add(struct pf_altq *a)
591 if (a->qname[0] != 0)
592 return (altq_add_queue(a));
594 if (machclk_freq == 0)
596 if (machclk_freq == 0)
597 panic("altq_add: no cpu clock");
599 switch (a->scheduler) {
602 error = cbq_add_altq(a);
607 error = priq_add_altq(a);
612 error = hfsc_add_altq(a);
623 * remove a discipline or a queue
624 * It is yet unclear what lock to use to protect this operation, the
625 * discipline specific functions will determine and grab it
628 altq_remove(struct pf_altq *a)
632 if (a->qname[0] != 0)
633 return (altq_remove_queue(a));
635 switch (a->scheduler) {
638 error = cbq_remove_altq(a);
643 error = priq_remove_altq(a);
648 error = hfsc_remove_altq(a);
659 * add a queue to the discipline
660 * It is yet unclear what lock to use to protect this operation, the
661 * discipline specific functions will determine and grab it
664 altq_add_queue(struct pf_altq *a)
668 switch (a->scheduler) {
671 error = cbq_add_queue(a);
676 error = priq_add_queue(a);
681 error = hfsc_add_queue(a);
692 * remove a queue from the discipline
693 * It is yet unclear what lock to use to protect this operation, the
694 * discipline specific functions will determine and grab it
697 altq_remove_queue(struct pf_altq *a)
701 switch (a->scheduler) {
704 error = cbq_remove_queue(a);
709 error = priq_remove_queue(a);
714 error = hfsc_remove_queue(a);
725 * get queue statistics
726 * Locking is done in the discipline specific functions with regards to
727 * copyout operations, also it is not yet clear which lock to use.
730 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
734 switch (a->scheduler) {
737 error = cbq_getqstats(a, ubuf, nbytes);
742 error = priq_getqstats(a, ubuf, nbytes);
747 error = hfsc_getqstats(a, ubuf, nbytes);
758 * read and write diffserv field in IPv4 or IPv6 header
761 read_dsfield(m, pktattr)
763 struct altq_pktattr *pktattr;
766 u_int8_t ds_field = 0;
768 if (pktattr == NULL ||
769 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
770 return ((u_int8_t)0);
772 /* verify that pattr_hdr is within the mbuf data */
773 for (m0 = m; m0 != NULL; m0 = m0->m_next)
774 if ((pktattr->pattr_hdr >= m0->m_data) &&
775 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
778 /* ick, pattr_hdr is stale */
779 pktattr->pattr_af = AF_UNSPEC;
781 printf("read_dsfield: can't locate header!\n");
783 return ((u_int8_t)0);
786 if (pktattr->pattr_af == AF_INET) {
787 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
790 return ((u_int8_t)0); /* version mismatch! */
791 ds_field = ip->ip_tos;
794 else if (pktattr->pattr_af == AF_INET6) {
795 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
798 flowlabel = ntohl(ip6->ip6_flow);
799 if ((flowlabel >> 28) != 6)
800 return ((u_int8_t)0); /* version mismatch! */
801 ds_field = (flowlabel >> 20) & 0xff;
808 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
812 if (pktattr == NULL ||
813 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
816 /* verify that pattr_hdr is within the mbuf data */
817 for (m0 = m; m0 != NULL; m0 = m0->m_next)
818 if ((pktattr->pattr_hdr >= m0->m_data) &&
819 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
822 /* ick, pattr_hdr is stale */
823 pktattr->pattr_af = AF_UNSPEC;
825 printf("write_dsfield: can't locate header!\n");
830 if (pktattr->pattr_af == AF_INET) {
831 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
836 return; /* version mismatch! */
838 dsfield |= old & 3; /* leave CU bits */
841 ip->ip_tos = dsfield;
843 * update checksum (from RFC1624)
844 * HC' = ~(~HC + ~m + m')
846 sum = ~ntohs(ip->ip_sum) & 0xffff;
847 sum += 0xff00 + (~old & 0xff) + dsfield;
848 sum = (sum >> 16) + (sum & 0xffff);
849 sum += (sum >> 16); /* add carry */
851 ip->ip_sum = htons(~sum & 0xffff);
854 else if (pktattr->pattr_af == AF_INET6) {
855 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
858 flowlabel = ntohl(ip6->ip6_flow);
859 if ((flowlabel >> 28) != 6)
860 return; /* version mismatch! */
861 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
862 ip6->ip6_flow = htonl(flowlabel);
870 * high resolution clock support taking advantage of a machine dependent
871 * high resolution time counter (e.g., timestamp counter of intel pentium).
873 * - 64-bit-long monotonically-increasing counter
874 * - frequency range is 100M-4GHz (CPU speed)
876 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
877 #define MACHCLK_SHIFT 8
880 u_int32_t machclk_freq;
881 u_int32_t machclk_per_tick;
883 #if defined(__i386__) && defined(__NetBSD__)
884 extern u_int64_t cpu_tsc_freq;
887 #if (__FreeBSD_version >= 700035)
888 /* Update TSC freq with the value indicated by the caller. */
890 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
892 /* If there was an error during the transition, don't do anything. */
896 #if (__FreeBSD_version >= 701102) && (defined(__amd64__) || defined(__i386__))
897 /* If TSC is P-state invariant, don't do anything. */
898 if (tsc_is_invariant)
902 /* Total setting for this level gives the new frequency in MHz. */
905 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
906 EVENTHANDLER_PRI_LAST);
907 #endif /* __FreeBSD_version >= 700035 */
910 init_machclk_setup(void)
912 #if (__FreeBSD_version >= 600000)
913 callout_init(&tbr_callout, 0);
918 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
921 #if defined(__FreeBSD__) && defined(SMP)
924 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
927 #if defined(__amd64__) || defined(__i386__)
928 /* check if TSC is available */
930 if ((cpu_feature & CPUID_TSC) == 0 ||
931 atomic_load_acq_64(&tsc_freq) == 0)
933 if ((cpu_feature & CPUID_TSC) == 0)
944 /* Call one-time initialization function. */
946 init_machclk_setup();
950 if (machclk_usepcc == 0) {
951 /* emulate 256MHz using microtime() */
952 machclk_freq = 1000000 << MACHCLK_SHIFT;
953 machclk_per_tick = machclk_freq / hz;
955 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
961 * if the clock frequency (of Pentium TSC or Alpha PCC) is
962 * accessible, just use it.
964 #if defined(__amd64__) || defined(__i386__)
966 machclk_freq = atomic_load_acq_64(&tsc_freq);
967 #elif defined(__NetBSD__)
968 machclk_freq = (u_int32_t)cpu_tsc_freq;
969 #elif defined(__OpenBSD__) && (defined(I586_CPU) || defined(I686_CPU))
970 machclk_freq = pentium_mhz * 1000000;
975 * if we don't know the clock frequency, measure it.
977 if (machclk_freq == 0) {
979 struct timeval tv_start, tv_end;
980 u_int64_t start, end, diff;
983 microtime(&tv_start);
984 start = read_machclk();
985 timo = hz; /* 1 sec */
986 (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
988 end = read_machclk();
989 diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
990 + tv_end.tv_usec - tv_start.tv_usec;
992 machclk_freq = (u_int)((end - start) * 1000000 / diff);
995 machclk_per_tick = machclk_freq / hz;
998 printf("altq: CPU clock: %uHz\n", machclk_freq);
1002 #if defined(__OpenBSD__) && defined(__i386__)
1003 static __inline u_int64_t
1007 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
1010 #endif /* __OpenBSD__ && __i386__ */
1017 if (machclk_usepcc) {
1018 #if defined(__amd64__) || defined(__i386__)
1021 panic("read_machclk");
1027 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1028 + tv.tv_usec) << MACHCLK_SHIFT);
1033 #ifdef ALTQ3_CLFIER_COMPAT
1036 #define IPPROTO_ESP 50 /* encapsulating security payload */
1039 #define IPPROTO_AH 51 /* authentication header */
1043 * extract flow information from a given packet.
1044 * filt_mask shows flowinfo fields required.
1045 * we assume the ip header is in one mbuf, and addresses and ports are
1046 * in network byte order.
1049 altq_extractflow(m, af, flow, filt_bmask)
1052 struct flowinfo *flow;
1053 u_int32_t filt_bmask;
1058 struct flowinfo_in *fin;
1061 ip = mtod(m, struct ip *);
1066 fin = (struct flowinfo_in *)flow;
1067 fin->fi_len = sizeof(struct flowinfo_in);
1068 fin->fi_family = AF_INET;
1070 fin->fi_proto = ip->ip_p;
1071 fin->fi_tos = ip->ip_tos;
1073 fin->fi_src.s_addr = ip->ip_src.s_addr;
1074 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1076 if (filt_bmask & FIMB4_PORTS)
1077 /* if port info is required, extract port numbers */
1078 extract_ports4(m, ip, fin);
1089 struct flowinfo_in6 *fin6;
1090 struct ip6_hdr *ip6;
1092 ip6 = mtod(m, struct ip6_hdr *);
1093 /* should we check the ip version? */
1095 fin6 = (struct flowinfo_in6 *)flow;
1096 fin6->fi6_len = sizeof(struct flowinfo_in6);
1097 fin6->fi6_family = AF_INET6;
1099 fin6->fi6_proto = ip6->ip6_nxt;
1100 fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1102 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1103 fin6->fi6_src = ip6->ip6_src;
1104 fin6->fi6_dst = ip6->ip6_dst;
1106 if ((filt_bmask & FIMB6_PORTS) ||
1107 ((filt_bmask & FIMB6_PROTO)
1108 && ip6->ip6_nxt > IPPROTO_IPV6))
1110 * if port info is required, or proto is required
1111 * but there are option headers, extract port
1112 * and protocol numbers.
1114 extract_ports6(m, ip6, fin6);
1116 fin6->fi6_sport = 0;
1117 fin6->fi6_dport = 0;
1129 flow->fi_len = sizeof(struct flowinfo);
1130 flow->fi_family = AF_UNSPEC;
1135 * helper routine to extract port numbers
1137 /* structure for ipsec and ipv6 option header template */
1139 u_int8_t opt6_nxt; /* next header */
1140 u_int8_t opt6_hlen; /* header extension length */
1142 u_int32_t ah_spi; /* security parameter index
1143 for authentication header */
1147 * extract port numbers from a ipv4 packet.
1150 extract_ports4(m, ip, fin)
1153 struct flowinfo_in *fin;
1164 ip_off = ntohs(ip->ip_off);
1165 /* if it is a fragment, try cached fragment info */
1166 if (ip_off & IP_OFFMASK) {
1167 ip4f_lookup(ip, fin);
1171 /* locate the mbuf containing the protocol header */
1172 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1173 if (((caddr_t)ip >= m0->m_data) &&
1174 ((caddr_t)ip < m0->m_data + m0->m_len))
1178 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1182 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1188 while (off >= m0->m_len) {
1192 return (0); /* bogus ip_hl! */
1194 if (m0->m_len < off + 4)
1202 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1203 fin->fi_sport = udp->uh_sport;
1204 fin->fi_dport = udp->uh_dport;
1205 fin->fi_proto = proto;
1211 if (fin->fi_gpi == 0){
1214 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1217 fin->fi_proto = proto;
1221 /* get next header and header length */
1224 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1225 proto = opt6->opt6_nxt;
1226 off += 8 + (opt6->opt6_hlen * 4);
1227 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1228 fin->fi_gpi = opt6->ah_spi;
1230 /* goto the next header */
1232 #endif /* ALTQ_IPSEC */
1235 fin->fi_proto = proto;
1239 /* if this is a first fragment, cache it. */
1241 ip4f_cache(ip, fin);
1248 extract_ports6(m, ip6, fin6)
1250 struct ip6_hdr *ip6;
1251 struct flowinfo_in6 *fin6;
1258 fin6->fi6_sport = 0;
1259 fin6->fi6_dport = 0;
1261 /* locate the mbuf containing the protocol header */
1262 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1263 if (((caddr_t)ip6 >= m0->m_data) &&
1264 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1268 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1272 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1274 proto = ip6->ip6_nxt;
1276 while (off >= m0->m_len) {
1282 if (m0->m_len < off + 4)
1290 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1291 fin6->fi6_sport = udp->uh_sport;
1292 fin6->fi6_dport = udp->uh_dport;
1293 fin6->fi6_proto = proto;
1298 if (fin6->fi6_gpi == 0) {
1301 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1302 fin6->fi6_gpi = *gpi;
1304 fin6->fi6_proto = proto;
1308 /* get next header and header length */
1311 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1312 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1313 fin6->fi6_gpi = opt6->ah_spi;
1314 proto = opt6->opt6_nxt;
1315 off += 8 + (opt6->opt6_hlen * 4);
1316 /* goto the next header */
1320 case IPPROTO_HOPOPTS:
1321 case IPPROTO_ROUTING:
1322 case IPPROTO_DSTOPTS: {
1323 /* get next header and header length */
1326 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1327 proto = opt6->opt6_nxt;
1328 off += (opt6->opt6_hlen + 1) * 8;
1329 /* goto the next header */
1333 case IPPROTO_FRAGMENT:
1334 /* ipv6 fragmentations are not supported yet */
1336 fin6->fi6_proto = proto;
1345 * altq common classifier
1348 acc_add_filter(classifier, filter, class, phandle)
1349 struct acc_classifier *classifier;
1350 struct flow_filter *filter;
1354 struct acc_filter *afp, *prev, *tmp;
1358 if (filter->ff_flow.fi_family != AF_INET &&
1359 filter->ff_flow.fi_family != AF_INET6)
1362 if (filter->ff_flow.fi_family != AF_INET)
1366 afp = malloc(sizeof(struct acc_filter),
1367 M_DEVBUF, M_WAITOK);
1370 bzero(afp, sizeof(struct acc_filter));
1372 afp->f_filter = *filter;
1373 afp->f_class = class;
1375 i = ACC_WILDCARD_INDEX;
1376 if (filter->ff_flow.fi_family == AF_INET) {
1377 struct flow_filter *filter4 = &afp->f_filter;
1380 * if address is 0, it's a wildcard. if address mask
1381 * isn't set, use full mask.
1383 if (filter4->ff_flow.fi_dst.s_addr == 0)
1384 filter4->ff_mask.mask_dst.s_addr = 0;
1385 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1386 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1387 if (filter4->ff_flow.fi_src.s_addr == 0)
1388 filter4->ff_mask.mask_src.s_addr = 0;
1389 else if (filter4->ff_mask.mask_src.s_addr == 0)
1390 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1392 /* clear extra bits in addresses */
1393 filter4->ff_flow.fi_dst.s_addr &=
1394 filter4->ff_mask.mask_dst.s_addr;
1395 filter4->ff_flow.fi_src.s_addr &=
1396 filter4->ff_mask.mask_src.s_addr;
1399 * if dst address is a wildcard, use hash-entry
1400 * ACC_WILDCARD_INDEX.
1402 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1403 i = ACC_WILDCARD_INDEX;
1405 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1408 else if (filter->ff_flow.fi_family == AF_INET6) {
1409 struct flow_filter6 *filter6 =
1410 (struct flow_filter6 *)&afp->f_filter;
1411 #ifndef IN6MASK0 /* taken from kame ipv6 */
1412 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1413 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1414 const struct in6_addr in6mask0 = IN6MASK0;
1415 const struct in6_addr in6mask128 = IN6MASK128;
1418 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1419 filter6->ff_mask6.mask6_dst = in6mask0;
1420 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1421 filter6->ff_mask6.mask6_dst = in6mask128;
1422 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1423 filter6->ff_mask6.mask6_src = in6mask0;
1424 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1425 filter6->ff_mask6.mask6_src = in6mask128;
1427 /* clear extra bits in addresses */
1428 for (i = 0; i < 16; i++)
1429 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1430 filter6->ff_mask6.mask6_dst.s6_addr[i];
1431 for (i = 0; i < 16; i++)
1432 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1433 filter6->ff_mask6.mask6_src.s6_addr[i];
1435 if (filter6->ff_flow6.fi6_flowlabel == 0)
1436 i = ACC_WILDCARD_INDEX;
1438 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1442 afp->f_handle = get_filt_handle(classifier, i);
1444 /* update filter bitmask */
1445 afp->f_fbmask = filt2fibmask(filter);
1446 classifier->acc_fbmask |= afp->f_fbmask;
1449 * add this filter to the filter list.
1450 * filters are ordered from the highest rule number.
1458 LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1459 if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1465 LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1467 LIST_INSERT_AFTER(prev, afp, f_chain);
1470 *phandle = afp->f_handle;
1475 acc_delete_filter(classifier, handle)
1476 struct acc_classifier *classifier;
1479 struct acc_filter *afp;
1482 if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1490 LIST_REMOVE(afp, f_chain);
1493 free(afp, M_DEVBUF);
1495 /* todo: update filt_bmask */
1501 * delete filters referencing to the specified class.
1502 * if the all flag is not 0, delete all the filters.
1505 acc_discard_filters(classifier, class, all)
1506 struct acc_classifier *classifier;
1510 struct acc_filter *afp;
1518 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1520 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1521 if (all || afp->f_class == class) {
1522 LIST_REMOVE(afp, f_chain);
1523 free(afp, M_DEVBUF);
1524 /* start again from the head */
1527 } while (afp != NULL);
1532 classifier->acc_fbmask = 0;
1538 acc_classify(clfier, m, af)
1543 struct acc_classifier *classifier;
1544 struct flowinfo flow;
1545 struct acc_filter *afp;
1548 classifier = (struct acc_classifier *)clfier;
1549 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1551 if (flow.fi_family == AF_INET) {
1552 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1554 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1555 /* only tos is used */
1557 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1559 if (apply_tosfilter4(afp->f_fbmask,
1560 &afp->f_filter, fp))
1561 /* filter matched */
1562 return (afp->f_class);
1563 } else if ((classifier->acc_fbmask &
1564 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1566 /* only proto and ports are used */
1568 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1570 if (apply_ppfilter4(afp->f_fbmask,
1571 &afp->f_filter, fp))
1572 /* filter matched */
1573 return (afp->f_class);
1575 /* get the filter hash entry from its dest address */
1576 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1579 * go through this loop twice. first for dst
1580 * hash, second for wildcards.
1582 LIST_FOREACH(afp, &classifier->acc_filters[i],
1584 if (apply_filter4(afp->f_fbmask,
1585 &afp->f_filter, fp))
1586 /* filter matched */
1587 return (afp->f_class);
1590 * check again for filters with a dst addr
1592 * (daddr == 0 || dmask != 0xffffffff).
1594 if (i != ACC_WILDCARD_INDEX)
1595 i = ACC_WILDCARD_INDEX;
1602 else if (flow.fi_family == AF_INET6) {
1603 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1605 /* get the filter hash entry from its flow ID */
1606 if (fp6->fi6_flowlabel != 0)
1607 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1609 /* flowlable can be zero */
1610 i = ACC_WILDCARD_INDEX;
1612 /* go through this loop twice. first for flow hash, second
1615 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1616 if (apply_filter6(afp->f_fbmask,
1617 (struct flow_filter6 *)&afp->f_filter,
1619 /* filter matched */
1620 return (afp->f_class);
1623 * check again for filters with a wildcard.
1625 if (i != ACC_WILDCARD_INDEX)
1626 i = ACC_WILDCARD_INDEX;
1633 /* no filter matched */
1638 apply_filter4(fbmask, filt, pkt)
1640 struct flow_filter *filt;
1641 struct flowinfo_in *pkt;
1643 if (filt->ff_flow.fi_family != AF_INET)
1645 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1647 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1649 if ((fbmask & FIMB4_DADDR) &&
1650 filt->ff_flow.fi_dst.s_addr !=
1651 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1653 if ((fbmask & FIMB4_SADDR) &&
1654 filt->ff_flow.fi_src.s_addr !=
1655 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1657 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1659 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1660 (pkt->fi_tos & filt->ff_mask.mask_tos))
1662 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1669 * filter matching function optimized for a common case that checks
1670 * only protocol and port numbers
1673 apply_ppfilter4(fbmask, filt, pkt)
1675 struct flow_filter *filt;
1676 struct flowinfo_in *pkt;
1678 if (filt->ff_flow.fi_family != AF_INET)
1680 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1682 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1684 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1691 * filter matching function only for tos field.
1694 apply_tosfilter4(fbmask, filt, pkt)
1696 struct flow_filter *filt;
1697 struct flowinfo_in *pkt;
1699 if (filt->ff_flow.fi_family != AF_INET)
1701 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1702 (pkt->fi_tos & filt->ff_mask.mask_tos))
1710 apply_filter6(fbmask, filt, pkt)
1712 struct flow_filter6 *filt;
1713 struct flowinfo_in6 *pkt;
1717 if (filt->ff_flow6.fi6_family != AF_INET6)
1719 if ((fbmask & FIMB6_FLABEL) &&
1720 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1722 if ((fbmask & FIMB6_PROTO) &&
1723 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1725 if ((fbmask & FIMB6_SPORT) &&
1726 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1728 if ((fbmask & FIMB6_DPORT) &&
1729 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1731 if (fbmask & FIMB6_SADDR) {
1732 for (i = 0; i < 4; i++)
1733 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1734 (pkt->fi6_src.s6_addr32[i] &
1735 filt->ff_mask6.mask6_src.s6_addr32[i]))
1738 if (fbmask & FIMB6_DADDR) {
1739 for (i = 0; i < 4; i++)
1740 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1741 (pkt->fi6_dst.s6_addr32[i] &
1742 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1745 if ((fbmask & FIMB6_TCLASS) &&
1746 filt->ff_flow6.fi6_tclass !=
1747 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1749 if ((fbmask & FIMB6_GPI) &&
1750 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1759 * bit 20-28: index to the filter hash table
1760 * bit 0-19: unique id in the hash bucket.
1763 get_filt_handle(classifier, i)
1764 struct acc_classifier *classifier;
1767 static u_long handle_number = 1;
1769 struct acc_filter *afp;
1772 handle = handle_number++ & 0x000fffff;
1774 if (LIST_EMPTY(&classifier->acc_filters[i]))
1777 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1778 if ((afp->f_handle & 0x000fffff) == handle)
1782 /* this handle is already used, try again */
1785 return ((i << 20) | handle);
1788 /* convert filter handle to filter pointer */
1789 static struct acc_filter *
1790 filth_to_filtp(classifier, handle)
1791 struct acc_classifier *classifier;
1794 struct acc_filter *afp;
1797 i = ACC_GET_HINDEX(handle);
1799 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1800 if (afp->f_handle == handle)
1806 /* create flowinfo bitmask */
1809 struct flow_filter *filt;
1813 struct flow_filter6 *filt6;
1816 switch (filt->ff_flow.fi_family) {
1818 if (filt->ff_flow.fi_proto != 0)
1819 mask |= FIMB4_PROTO;
1820 if (filt->ff_flow.fi_tos != 0)
1822 if (filt->ff_flow.fi_dst.s_addr != 0)
1823 mask |= FIMB4_DADDR;
1824 if (filt->ff_flow.fi_src.s_addr != 0)
1825 mask |= FIMB4_SADDR;
1826 if (filt->ff_flow.fi_sport != 0)
1827 mask |= FIMB4_SPORT;
1828 if (filt->ff_flow.fi_dport != 0)
1829 mask |= FIMB4_DPORT;
1830 if (filt->ff_flow.fi_gpi != 0)
1835 filt6 = (struct flow_filter6 *)filt;
1837 if (filt6->ff_flow6.fi6_proto != 0)
1838 mask |= FIMB6_PROTO;
1839 if (filt6->ff_flow6.fi6_tclass != 0)
1840 mask |= FIMB6_TCLASS;
1841 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1842 mask |= FIMB6_DADDR;
1843 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1844 mask |= FIMB6_SADDR;
1845 if (filt6->ff_flow6.fi6_sport != 0)
1846 mask |= FIMB6_SPORT;
1847 if (filt6->ff_flow6.fi6_dport != 0)
1848 mask |= FIMB6_DPORT;
1849 if (filt6->ff_flow6.fi6_gpi != 0)
1851 if (filt6->ff_flow6.fi6_flowlabel != 0)
1852 mask |= FIMB6_FLABEL;
1861 * helper functions to handle IPv4 fragments.
1862 * currently only in-sequence fragments are handled.
1863 * - fragment info is cached in a LRU list.
1864 * - when a first fragment is found, cache its flow info.
1865 * - when a non-first fragment is found, lookup the cache.
1869 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1872 struct flowinfo_in ip4f_info;
1875 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1877 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1883 struct flowinfo_in *fin;
1885 struct ip4_frag *fp;
1887 if (TAILQ_EMPTY(&ip4f_list)) {
1888 /* first time call, allocate fragment cache entries. */
1889 if (ip4f_init() < 0)
1890 /* allocation failed! */
1895 fp->ip4f_id = ip->ip_id;
1896 fp->ip4f_info.fi_proto = ip->ip_p;
1897 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1898 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1900 /* save port numbers */
1901 fp->ip4f_info.fi_sport = fin->fi_sport;
1902 fp->ip4f_info.fi_dport = fin->fi_dport;
1903 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1907 ip4f_lookup(ip, fin)
1909 struct flowinfo_in *fin;
1911 struct ip4_frag *fp;
1913 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1914 fp = TAILQ_NEXT(fp, ip4f_chain))
1915 if (ip->ip_id == fp->ip4f_id &&
1916 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1917 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1918 ip->ip_p == fp->ip4f_info.fi_proto) {
1920 /* found the matching entry */
1921 fin->fi_sport = fp->ip4f_info.fi_sport;
1922 fin->fi_dport = fp->ip4f_info.fi_dport;
1923 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1925 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1926 /* this is the last fragment,
1927 release the entry. */
1933 /* no matching entry found */
1940 struct ip4_frag *fp;
1943 TAILQ_INIT(&ip4f_list);
1944 for (i=0; i<IP4F_TABSIZE; i++) {
1945 fp = malloc(sizeof(struct ip4_frag),
1946 M_DEVBUF, M_NOWAIT);
1948 printf("ip4f_init: can't alloc %dth entry!\n", i);
1954 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1959 static struct ip4_frag *
1962 struct ip4_frag *fp;
1964 /* reclaim an entry at the tail, put it at the head */
1965 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1966 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1968 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1974 struct ip4_frag *fp;
1976 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1978 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1981 #endif /* ALTQ3_CLFIER_COMPAT */