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_dl.h>
53 #include <net/if_types.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
64 #include <netinet/tcp.h>
65 #include <netinet/udp.h>
67 #include <net/pfvar.h>
68 #include <altq/altq.h>
70 #include <altq/altq_conf.h>
73 /* machine dependent clock related includes */
77 #include <sys/eventhandler.h>
78 #include <machine/clock.h>
80 #if defined(__amd64__) || defined(__i386__)
81 #include <machine/cpufunc.h> /* for pentium tsc */
82 #include <machine/specialreg.h> /* for CPUID_TSC */
84 #include <machine/md_var.h> /* for cpu_feature */
85 #elif defined(__NetBSD__) || defined(__OpenBSD__)
86 #include <machine/cpu.h> /* for cpu_feature */
88 #endif /* __amd64 || __i386__ */
91 * internal function prototypes
93 static void tbr_timeout(void *);
94 int (*altq_input)(struct mbuf *, int) = NULL;
95 static struct mbuf *tbr_dequeue(struct ifaltq *, int);
96 static int tbr_timer = 0; /* token bucket regulator timer */
97 #if !defined(__FreeBSD__) || (__FreeBSD_version < 600000)
98 static struct callout tbr_callout = CALLOUT_INITIALIZER;
100 static struct callout tbr_callout;
103 #ifdef ALTQ3_CLFIER_COMPAT
104 static int extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
106 static int extract_ports6(struct mbuf *, struct ip6_hdr *,
107 struct flowinfo_in6 *);
109 static int apply_filter4(u_int32_t, struct flow_filter *,
110 struct flowinfo_in *);
111 static int apply_ppfilter4(u_int32_t, struct flow_filter *,
112 struct flowinfo_in *);
114 static int apply_filter6(u_int32_t, struct flow_filter6 *,
115 struct flowinfo_in6 *);
117 static int apply_tosfilter4(u_int32_t, struct flow_filter *,
118 struct flowinfo_in *);
119 static u_long get_filt_handle(struct acc_classifier *, int);
120 static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
121 static u_int32_t filt2fibmask(struct flow_filter *);
123 static void ip4f_cache(struct ip *, struct flowinfo_in *);
124 static int ip4f_lookup(struct ip *, struct flowinfo_in *);
125 static int ip4f_init(void);
126 static struct ip4_frag *ip4f_alloc(void);
127 static void ip4f_free(struct ip4_frag *);
128 #endif /* ALTQ3_CLFIER_COMPAT */
131 * alternate queueing support routines
134 /* look up the queue state by the interface name and the queueing type. */
136 altq_lookup(name, type)
142 if ((ifp = ifunit(name)) != NULL) {
143 /* read if_snd unlocked */
144 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
145 return (ifp->if_snd.altq_disc);
152 altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
156 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
157 struct mbuf *(*dequeue)(struct ifaltq *, int);
158 int (*request)(struct ifaltq *, int, void *);
160 void *(*classify)(void *, struct mbuf *, int);
163 if (!ALTQ_IS_READY(ifq)) {
170 * pfaltq can override the existing discipline, but altq3 cannot.
171 * check these if clfier is not NULL (which implies altq3).
173 if (clfier != NULL) {
174 if (ALTQ_IS_ENABLED(ifq)) {
178 if (ALTQ_IS_ATTACHED(ifq)) {
184 ifq->altq_type = type;
185 ifq->altq_disc = discipline;
186 ifq->altq_enqueue = enqueue;
187 ifq->altq_dequeue = dequeue;
188 ifq->altq_request = request;
189 ifq->altq_clfier = clfier;
190 ifq->altq_classify = classify;
191 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
194 altq_module_incref(type);
207 if (!ALTQ_IS_READY(ifq)) {
211 if (ALTQ_IS_ENABLED(ifq)) {
215 if (!ALTQ_IS_ATTACHED(ifq)) {
221 altq_module_declref(ifq->altq_type);
225 ifq->altq_type = ALTQT_NONE;
226 ifq->altq_disc = NULL;
227 ifq->altq_enqueue = NULL;
228 ifq->altq_dequeue = NULL;
229 ifq->altq_request = NULL;
230 ifq->altq_clfier = NULL;
231 ifq->altq_classify = NULL;
232 ifq->altq_flags &= ALTQF_CANTCHANGE;
246 if (!ALTQ_IS_READY(ifq)) {
250 if (ALTQ_IS_ENABLED(ifq)) {
260 IFQ_PURGE_NOLOCK(ifq);
261 ASSERT(ifq->ifq_len == 0);
262 ifq->ifq_drv_maxlen = 0; /* disable bulk dequeue */
263 ifq->altq_flags |= ALTQF_ENABLED;
264 if (ifq->altq_clfier != NULL)
265 ifq->altq_flags |= ALTQF_CLASSIFY;
279 if (!ALTQ_IS_ENABLED(ifq)) {
289 IFQ_PURGE_NOLOCK(ifq);
290 ASSERT(ifq->ifq_len == 0);
291 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
300 altq_assert(file, line, failedexpr)
301 const char *file, *failedexpr;
304 (void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
305 failedexpr, file, line);
306 panic("altq assertion");
312 * internal representation of token bucket parameters
313 * rate: byte_per_unittime << 32
314 * (((bits_per_sec) / 8) << 32) / machclk_freq
319 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
320 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
327 struct tb_regulator *tbr;
332 IFQ_LOCK_ASSERT(ifq);
334 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
335 /* if this is a remove after poll, bypass tbr check */
337 /* update token only when it is negative */
338 if (tbr->tbr_token <= 0) {
339 now = read_machclk();
340 interval = now - tbr->tbr_last;
341 if (interval >= tbr->tbr_filluptime)
342 tbr->tbr_token = tbr->tbr_depth;
344 tbr->tbr_token += interval * tbr->tbr_rate;
345 if (tbr->tbr_token > tbr->tbr_depth)
346 tbr->tbr_token = tbr->tbr_depth;
350 /* if token is still negative, don't allow dequeue */
351 if (tbr->tbr_token <= 0)
355 if (ALTQ_IS_ENABLED(ifq))
356 m = (*ifq->altq_dequeue)(ifq, op);
358 if (op == ALTDQ_POLL)
364 if (m != NULL && op == ALTDQ_REMOVE)
365 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
366 tbr->tbr_lastop = op;
371 * set a token bucket regulator.
372 * if the specified rate is zero, the token bucket regulator is deleted.
375 tbr_set(ifq, profile)
377 struct tb_profile *profile;
379 struct tb_regulator *tbr, *otbr;
381 if (tbr_dequeue_ptr == NULL)
382 tbr_dequeue_ptr = tbr_dequeue;
384 if (machclk_freq == 0)
386 if (machclk_freq == 0) {
387 printf("tbr_set: no cpu clock available!\n");
392 if (profile->rate == 0) {
393 /* delete this tbr */
394 if ((tbr = ifq->altq_tbr) == NULL) {
398 ifq->altq_tbr = NULL;
405 tbr = malloc(sizeof(struct tb_regulator),
407 if (tbr == NULL) { /* can not happen */
411 bzero(tbr, sizeof(struct tb_regulator));
413 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
414 tbr->tbr_depth = TBR_SCALE(profile->depth);
415 if (tbr->tbr_rate > 0)
416 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
418 tbr->tbr_filluptime = 0xffffffffffffffffLL;
419 tbr->tbr_token = tbr->tbr_depth;
420 tbr->tbr_last = read_machclk();
421 tbr->tbr_lastop = ALTDQ_REMOVE;
424 otbr = ifq->altq_tbr;
425 ifq->altq_tbr = tbr; /* set the new tbr */
428 free(otbr, M_DEVBUF);
430 if (tbr_timer == 0) {
431 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
440 * tbr_timeout goes through the interface list, and kicks the drivers
450 VNET_ITERATOR_DECL(vnet_iter);
462 IFNET_RLOCK_NOSLEEP();
463 VNET_LIST_RLOCK_NOSLEEP();
464 VNET_FOREACH(vnet_iter) {
465 CURVNET_SET(vnet_iter);
467 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
468 ifp = TAILQ_NEXT(ifp, if_list)) {
469 /* read from if_snd unlocked */
470 if (!TBR_IS_ENABLED(&ifp->if_snd))
473 if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
474 ifp->if_start != NULL)
475 (*ifp->if_start)(ifp);
480 VNET_LIST_RUNLOCK_NOSLEEP();
481 IFNET_RUNLOCK_NOSLEEP();
485 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
487 tbr_timer = 0; /* don't need tbr_timer anymore */
491 * get token bucket regulator profile
494 tbr_get(ifq, profile)
496 struct tb_profile *profile;
498 struct tb_regulator *tbr;
501 if ((tbr = ifq->altq_tbr) == NULL) {
506 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
507 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
514 * attach a discipline to the interface. if one already exists, it is
516 * Locking is done in the discipline specific attach functions. Basically
517 * they call back to altq_attach which takes care of the attach and locking.
520 altq_pfattach(struct pf_altq *a)
524 switch (a->scheduler) {
529 error = cbq_pfattach(a);
534 error = priq_pfattach(a);
539 error = hfsc_pfattach(a);
550 * detach a discipline from the interface.
551 * it is possible that the discipline was already overridden by another
555 altq_pfdetach(struct pf_altq *a)
560 if ((ifp = ifunit(a->ifname)) == NULL)
563 /* if this discipline is no longer referenced, just return */
564 /* read unlocked from if_snd */
565 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
573 /* read unlocked from if_snd, _disable and _detach take care */
574 if (ALTQ_IS_ENABLED(&ifp->if_snd))
575 error = altq_disable(&ifp->if_snd);
577 error = altq_detach(&ifp->if_snd);
584 * add a discipline or a queue
585 * Locking is done in the discipline specific functions with regards to
586 * malloc with WAITOK, also it is not yet clear which lock to use.
589 altq_add(struct pf_altq *a)
593 if (a->qname[0] != 0)
594 return (altq_add_queue(a));
596 if (machclk_freq == 0)
598 if (machclk_freq == 0)
599 panic("altq_add: no cpu clock");
601 switch (a->scheduler) {
604 error = cbq_add_altq(a);
609 error = priq_add_altq(a);
614 error = hfsc_add_altq(a);
625 * remove a discipline or a queue
626 * It is yet unclear what lock to use to protect this operation, the
627 * discipline specific functions will determine and grab it
630 altq_remove(struct pf_altq *a)
634 if (a->qname[0] != 0)
635 return (altq_remove_queue(a));
637 switch (a->scheduler) {
640 error = cbq_remove_altq(a);
645 error = priq_remove_altq(a);
650 error = hfsc_remove_altq(a);
661 * add a queue to the discipline
662 * It is yet unclear what lock to use to protect this operation, the
663 * discipline specific functions will determine and grab it
666 altq_add_queue(struct pf_altq *a)
670 switch (a->scheduler) {
673 error = cbq_add_queue(a);
678 error = priq_add_queue(a);
683 error = hfsc_add_queue(a);
694 * remove a queue from the discipline
695 * It is yet unclear what lock to use to protect this operation, the
696 * discipline specific functions will determine and grab it
699 altq_remove_queue(struct pf_altq *a)
703 switch (a->scheduler) {
706 error = cbq_remove_queue(a);
711 error = priq_remove_queue(a);
716 error = hfsc_remove_queue(a);
727 * get queue statistics
728 * Locking is done in the discipline specific functions with regards to
729 * copyout operations, also it is not yet clear which lock to use.
732 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
736 switch (a->scheduler) {
739 error = cbq_getqstats(a, ubuf, nbytes);
744 error = priq_getqstats(a, ubuf, nbytes);
749 error = hfsc_getqstats(a, ubuf, nbytes);
760 * read and write diffserv field in IPv4 or IPv6 header
763 read_dsfield(m, pktattr)
765 struct altq_pktattr *pktattr;
768 u_int8_t ds_field = 0;
770 if (pktattr == NULL ||
771 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
772 return ((u_int8_t)0);
774 /* verify that pattr_hdr is within the mbuf data */
775 for (m0 = m; m0 != NULL; m0 = m0->m_next)
776 if ((pktattr->pattr_hdr >= m0->m_data) &&
777 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
780 /* ick, pattr_hdr is stale */
781 pktattr->pattr_af = AF_UNSPEC;
783 printf("read_dsfield: can't locate header!\n");
785 return ((u_int8_t)0);
788 if (pktattr->pattr_af == AF_INET) {
789 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
792 return ((u_int8_t)0); /* version mismatch! */
793 ds_field = ip->ip_tos;
796 else if (pktattr->pattr_af == AF_INET6) {
797 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
800 flowlabel = ntohl(ip6->ip6_flow);
801 if ((flowlabel >> 28) != 6)
802 return ((u_int8_t)0); /* version mismatch! */
803 ds_field = (flowlabel >> 20) & 0xff;
810 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
814 if (pktattr == NULL ||
815 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
818 /* verify that pattr_hdr is within the mbuf data */
819 for (m0 = m; m0 != NULL; m0 = m0->m_next)
820 if ((pktattr->pattr_hdr >= m0->m_data) &&
821 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
824 /* ick, pattr_hdr is stale */
825 pktattr->pattr_af = AF_UNSPEC;
827 printf("write_dsfield: can't locate header!\n");
832 if (pktattr->pattr_af == AF_INET) {
833 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
838 return; /* version mismatch! */
840 dsfield |= old & 3; /* leave CU bits */
843 ip->ip_tos = dsfield;
845 * update checksum (from RFC1624)
846 * HC' = ~(~HC + ~m + m')
848 sum = ~ntohs(ip->ip_sum) & 0xffff;
849 sum += 0xff00 + (~old & 0xff) + dsfield;
850 sum = (sum >> 16) + (sum & 0xffff);
851 sum += (sum >> 16); /* add carry */
853 ip->ip_sum = htons(~sum & 0xffff);
856 else if (pktattr->pattr_af == AF_INET6) {
857 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
860 flowlabel = ntohl(ip6->ip6_flow);
861 if ((flowlabel >> 28) != 6)
862 return; /* version mismatch! */
863 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
864 ip6->ip6_flow = htonl(flowlabel);
872 * high resolution clock support taking advantage of a machine dependent
873 * high resolution time counter (e.g., timestamp counter of intel pentium).
875 * - 64-bit-long monotonically-increasing counter
876 * - frequency range is 100M-4GHz (CPU speed)
878 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
879 #define MACHCLK_SHIFT 8
882 u_int32_t machclk_freq;
883 u_int32_t machclk_per_tick;
885 #if defined(__i386__) && defined(__NetBSD__)
886 extern u_int64_t cpu_tsc_freq;
889 #if (__FreeBSD_version >= 700035)
890 /* Update TSC freq with the value indicated by the caller. */
892 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
894 /* If there was an error during the transition, don't do anything. */
898 #if (__FreeBSD_version >= 701102) && (defined(__amd64__) || defined(__i386__))
899 /* If TSC is P-state invariant, don't do anything. */
900 if (tsc_is_invariant)
904 /* Total setting for this level gives the new frequency in MHz. */
907 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
908 EVENTHANDLER_PRI_LAST);
909 #endif /* __FreeBSD_version >= 700035 */
912 init_machclk_setup(void)
914 #if (__FreeBSD_version >= 600000)
915 callout_init(&tbr_callout, 0);
920 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
923 #if defined(__FreeBSD__) && defined(SMP)
926 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
929 #if defined(__amd64__) || defined(__i386__)
930 /* check if TSC is available */
932 if ((cpu_feature & CPUID_TSC) == 0 ||
933 atomic_load_acq_64(&tsc_freq) == 0)
935 if ((cpu_feature & CPUID_TSC) == 0)
946 /* Call one-time initialization function. */
948 init_machclk_setup();
952 if (machclk_usepcc == 0) {
953 /* emulate 256MHz using microtime() */
954 machclk_freq = 1000000 << MACHCLK_SHIFT;
955 machclk_per_tick = machclk_freq / hz;
957 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
963 * if the clock frequency (of Pentium TSC or Alpha PCC) is
964 * accessible, just use it.
966 #if defined(__amd64__) || defined(__i386__)
968 machclk_freq = atomic_load_acq_64(&tsc_freq);
969 #elif defined(__NetBSD__)
970 machclk_freq = (u_int32_t)cpu_tsc_freq;
971 #elif defined(__OpenBSD__) && (defined(I586_CPU) || defined(I686_CPU))
972 machclk_freq = pentium_mhz * 1000000;
977 * if we don't know the clock frequency, measure it.
979 if (machclk_freq == 0) {
981 struct timeval tv_start, tv_end;
982 u_int64_t start, end, diff;
985 microtime(&tv_start);
986 start = read_machclk();
987 timo = hz; /* 1 sec */
988 (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
990 end = read_machclk();
991 diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
992 + tv_end.tv_usec - tv_start.tv_usec;
994 machclk_freq = (u_int)((end - start) * 1000000 / diff);
997 machclk_per_tick = machclk_freq / hz;
1000 printf("altq: CPU clock: %uHz\n", machclk_freq);
1004 #if defined(__OpenBSD__) && defined(__i386__)
1005 static __inline u_int64_t
1009 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
1012 #endif /* __OpenBSD__ && __i386__ */
1019 if (machclk_usepcc) {
1020 #if defined(__amd64__) || defined(__i386__)
1023 panic("read_machclk");
1029 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1030 + tv.tv_usec) << MACHCLK_SHIFT);
1035 #ifdef ALTQ3_CLFIER_COMPAT
1038 #define IPPROTO_ESP 50 /* encapsulating security payload */
1041 #define IPPROTO_AH 51 /* authentication header */
1045 * extract flow information from a given packet.
1046 * filt_mask shows flowinfo fields required.
1047 * we assume the ip header is in one mbuf, and addresses and ports are
1048 * in network byte order.
1051 altq_extractflow(m, af, flow, filt_bmask)
1054 struct flowinfo *flow;
1055 u_int32_t filt_bmask;
1060 struct flowinfo_in *fin;
1063 ip = mtod(m, struct ip *);
1068 fin = (struct flowinfo_in *)flow;
1069 fin->fi_len = sizeof(struct flowinfo_in);
1070 fin->fi_family = AF_INET;
1072 fin->fi_proto = ip->ip_p;
1073 fin->fi_tos = ip->ip_tos;
1075 fin->fi_src.s_addr = ip->ip_src.s_addr;
1076 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1078 if (filt_bmask & FIMB4_PORTS)
1079 /* if port info is required, extract port numbers */
1080 extract_ports4(m, ip, fin);
1091 struct flowinfo_in6 *fin6;
1092 struct ip6_hdr *ip6;
1094 ip6 = mtod(m, struct ip6_hdr *);
1095 /* should we check the ip version? */
1097 fin6 = (struct flowinfo_in6 *)flow;
1098 fin6->fi6_len = sizeof(struct flowinfo_in6);
1099 fin6->fi6_family = AF_INET6;
1101 fin6->fi6_proto = ip6->ip6_nxt;
1102 fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1104 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1105 fin6->fi6_src = ip6->ip6_src;
1106 fin6->fi6_dst = ip6->ip6_dst;
1108 if ((filt_bmask & FIMB6_PORTS) ||
1109 ((filt_bmask & FIMB6_PROTO)
1110 && ip6->ip6_nxt > IPPROTO_IPV6))
1112 * if port info is required, or proto is required
1113 * but there are option headers, extract port
1114 * and protocol numbers.
1116 extract_ports6(m, ip6, fin6);
1118 fin6->fi6_sport = 0;
1119 fin6->fi6_dport = 0;
1131 flow->fi_len = sizeof(struct flowinfo);
1132 flow->fi_family = AF_UNSPEC;
1137 * helper routine to extract port numbers
1139 /* structure for ipsec and ipv6 option header template */
1141 u_int8_t opt6_nxt; /* next header */
1142 u_int8_t opt6_hlen; /* header extension length */
1144 u_int32_t ah_spi; /* security parameter index
1145 for authentication header */
1149 * extract port numbers from a ipv4 packet.
1152 extract_ports4(m, ip, fin)
1155 struct flowinfo_in *fin;
1166 ip_off = ntohs(ip->ip_off);
1167 /* if it is a fragment, try cached fragment info */
1168 if (ip_off & IP_OFFMASK) {
1169 ip4f_lookup(ip, fin);
1173 /* locate the mbuf containing the protocol header */
1174 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1175 if (((caddr_t)ip >= m0->m_data) &&
1176 ((caddr_t)ip < m0->m_data + m0->m_len))
1180 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1184 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1190 while (off >= m0->m_len) {
1194 return (0); /* bogus ip_hl! */
1196 if (m0->m_len < off + 4)
1204 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1205 fin->fi_sport = udp->uh_sport;
1206 fin->fi_dport = udp->uh_dport;
1207 fin->fi_proto = proto;
1213 if (fin->fi_gpi == 0){
1216 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1219 fin->fi_proto = proto;
1223 /* get next header and header length */
1226 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1227 proto = opt6->opt6_nxt;
1228 off += 8 + (opt6->opt6_hlen * 4);
1229 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1230 fin->fi_gpi = opt6->ah_spi;
1232 /* goto the next header */
1234 #endif /* ALTQ_IPSEC */
1237 fin->fi_proto = proto;
1241 /* if this is a first fragment, cache it. */
1243 ip4f_cache(ip, fin);
1250 extract_ports6(m, ip6, fin6)
1252 struct ip6_hdr *ip6;
1253 struct flowinfo_in6 *fin6;
1260 fin6->fi6_sport = 0;
1261 fin6->fi6_dport = 0;
1263 /* locate the mbuf containing the protocol header */
1264 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1265 if (((caddr_t)ip6 >= m0->m_data) &&
1266 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1270 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1274 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1276 proto = ip6->ip6_nxt;
1278 while (off >= m0->m_len) {
1284 if (m0->m_len < off + 4)
1292 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1293 fin6->fi6_sport = udp->uh_sport;
1294 fin6->fi6_dport = udp->uh_dport;
1295 fin6->fi6_proto = proto;
1300 if (fin6->fi6_gpi == 0) {
1303 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1304 fin6->fi6_gpi = *gpi;
1306 fin6->fi6_proto = proto;
1310 /* get next header and header length */
1313 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1314 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1315 fin6->fi6_gpi = opt6->ah_spi;
1316 proto = opt6->opt6_nxt;
1317 off += 8 + (opt6->opt6_hlen * 4);
1318 /* goto the next header */
1322 case IPPROTO_HOPOPTS:
1323 case IPPROTO_ROUTING:
1324 case IPPROTO_DSTOPTS: {
1325 /* get next header and header length */
1328 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1329 proto = opt6->opt6_nxt;
1330 off += (opt6->opt6_hlen + 1) * 8;
1331 /* goto the next header */
1335 case IPPROTO_FRAGMENT:
1336 /* ipv6 fragmentations are not supported yet */
1338 fin6->fi6_proto = proto;
1347 * altq common classifier
1350 acc_add_filter(classifier, filter, class, phandle)
1351 struct acc_classifier *classifier;
1352 struct flow_filter *filter;
1356 struct acc_filter *afp, *prev, *tmp;
1360 if (filter->ff_flow.fi_family != AF_INET &&
1361 filter->ff_flow.fi_family != AF_INET6)
1364 if (filter->ff_flow.fi_family != AF_INET)
1368 afp = malloc(sizeof(struct acc_filter),
1369 M_DEVBUF, M_WAITOK);
1372 bzero(afp, sizeof(struct acc_filter));
1374 afp->f_filter = *filter;
1375 afp->f_class = class;
1377 i = ACC_WILDCARD_INDEX;
1378 if (filter->ff_flow.fi_family == AF_INET) {
1379 struct flow_filter *filter4 = &afp->f_filter;
1382 * if address is 0, it's a wildcard. if address mask
1383 * isn't set, use full mask.
1385 if (filter4->ff_flow.fi_dst.s_addr == 0)
1386 filter4->ff_mask.mask_dst.s_addr = 0;
1387 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1388 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1389 if (filter4->ff_flow.fi_src.s_addr == 0)
1390 filter4->ff_mask.mask_src.s_addr = 0;
1391 else if (filter4->ff_mask.mask_src.s_addr == 0)
1392 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1394 /* clear extra bits in addresses */
1395 filter4->ff_flow.fi_dst.s_addr &=
1396 filter4->ff_mask.mask_dst.s_addr;
1397 filter4->ff_flow.fi_src.s_addr &=
1398 filter4->ff_mask.mask_src.s_addr;
1401 * if dst address is a wildcard, use hash-entry
1402 * ACC_WILDCARD_INDEX.
1404 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1405 i = ACC_WILDCARD_INDEX;
1407 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1410 else if (filter->ff_flow.fi_family == AF_INET6) {
1411 struct flow_filter6 *filter6 =
1412 (struct flow_filter6 *)&afp->f_filter;
1413 #ifndef IN6MASK0 /* taken from kame ipv6 */
1414 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1415 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1416 const struct in6_addr in6mask0 = IN6MASK0;
1417 const struct in6_addr in6mask128 = IN6MASK128;
1420 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1421 filter6->ff_mask6.mask6_dst = in6mask0;
1422 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1423 filter6->ff_mask6.mask6_dst = in6mask128;
1424 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1425 filter6->ff_mask6.mask6_src = in6mask0;
1426 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1427 filter6->ff_mask6.mask6_src = in6mask128;
1429 /* clear extra bits in addresses */
1430 for (i = 0; i < 16; i++)
1431 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1432 filter6->ff_mask6.mask6_dst.s6_addr[i];
1433 for (i = 0; i < 16; i++)
1434 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1435 filter6->ff_mask6.mask6_src.s6_addr[i];
1437 if (filter6->ff_flow6.fi6_flowlabel == 0)
1438 i = ACC_WILDCARD_INDEX;
1440 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1444 afp->f_handle = get_filt_handle(classifier, i);
1446 /* update filter bitmask */
1447 afp->f_fbmask = filt2fibmask(filter);
1448 classifier->acc_fbmask |= afp->f_fbmask;
1451 * add this filter to the filter list.
1452 * filters are ordered from the highest rule number.
1460 LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1461 if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1467 LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1469 LIST_INSERT_AFTER(prev, afp, f_chain);
1472 *phandle = afp->f_handle;
1477 acc_delete_filter(classifier, handle)
1478 struct acc_classifier *classifier;
1481 struct acc_filter *afp;
1484 if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1492 LIST_REMOVE(afp, f_chain);
1495 free(afp, M_DEVBUF);
1497 /* todo: update filt_bmask */
1503 * delete filters referencing to the specified class.
1504 * if the all flag is not 0, delete all the filters.
1507 acc_discard_filters(classifier, class, all)
1508 struct acc_classifier *classifier;
1512 struct acc_filter *afp;
1520 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1522 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1523 if (all || afp->f_class == class) {
1524 LIST_REMOVE(afp, f_chain);
1525 free(afp, M_DEVBUF);
1526 /* start again from the head */
1529 } while (afp != NULL);
1534 classifier->acc_fbmask = 0;
1540 acc_classify(clfier, m, af)
1545 struct acc_classifier *classifier;
1546 struct flowinfo flow;
1547 struct acc_filter *afp;
1550 classifier = (struct acc_classifier *)clfier;
1551 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1553 if (flow.fi_family == AF_INET) {
1554 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1556 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1557 /* only tos is used */
1559 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1561 if (apply_tosfilter4(afp->f_fbmask,
1562 &afp->f_filter, fp))
1563 /* filter matched */
1564 return (afp->f_class);
1565 } else if ((classifier->acc_fbmask &
1566 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1568 /* only proto and ports are used */
1570 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1572 if (apply_ppfilter4(afp->f_fbmask,
1573 &afp->f_filter, fp))
1574 /* filter matched */
1575 return (afp->f_class);
1577 /* get the filter hash entry from its dest address */
1578 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1581 * go through this loop twice. first for dst
1582 * hash, second for wildcards.
1584 LIST_FOREACH(afp, &classifier->acc_filters[i],
1586 if (apply_filter4(afp->f_fbmask,
1587 &afp->f_filter, fp))
1588 /* filter matched */
1589 return (afp->f_class);
1592 * check again for filters with a dst addr
1594 * (daddr == 0 || dmask != 0xffffffff).
1596 if (i != ACC_WILDCARD_INDEX)
1597 i = ACC_WILDCARD_INDEX;
1604 else if (flow.fi_family == AF_INET6) {
1605 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1607 /* get the filter hash entry from its flow ID */
1608 if (fp6->fi6_flowlabel != 0)
1609 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1611 /* flowlable can be zero */
1612 i = ACC_WILDCARD_INDEX;
1614 /* go through this loop twice. first for flow hash, second
1617 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1618 if (apply_filter6(afp->f_fbmask,
1619 (struct flow_filter6 *)&afp->f_filter,
1621 /* filter matched */
1622 return (afp->f_class);
1625 * check again for filters with a wildcard.
1627 if (i != ACC_WILDCARD_INDEX)
1628 i = ACC_WILDCARD_INDEX;
1635 /* no filter matched */
1640 apply_filter4(fbmask, filt, pkt)
1642 struct flow_filter *filt;
1643 struct flowinfo_in *pkt;
1645 if (filt->ff_flow.fi_family != AF_INET)
1647 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1649 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1651 if ((fbmask & FIMB4_DADDR) &&
1652 filt->ff_flow.fi_dst.s_addr !=
1653 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1655 if ((fbmask & FIMB4_SADDR) &&
1656 filt->ff_flow.fi_src.s_addr !=
1657 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1659 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1661 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1662 (pkt->fi_tos & filt->ff_mask.mask_tos))
1664 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1671 * filter matching function optimized for a common case that checks
1672 * only protocol and port numbers
1675 apply_ppfilter4(fbmask, filt, pkt)
1677 struct flow_filter *filt;
1678 struct flowinfo_in *pkt;
1680 if (filt->ff_flow.fi_family != AF_INET)
1682 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1684 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1686 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1693 * filter matching function only for tos field.
1696 apply_tosfilter4(fbmask, filt, pkt)
1698 struct flow_filter *filt;
1699 struct flowinfo_in *pkt;
1701 if (filt->ff_flow.fi_family != AF_INET)
1703 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1704 (pkt->fi_tos & filt->ff_mask.mask_tos))
1712 apply_filter6(fbmask, filt, pkt)
1714 struct flow_filter6 *filt;
1715 struct flowinfo_in6 *pkt;
1719 if (filt->ff_flow6.fi6_family != AF_INET6)
1721 if ((fbmask & FIMB6_FLABEL) &&
1722 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1724 if ((fbmask & FIMB6_PROTO) &&
1725 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1727 if ((fbmask & FIMB6_SPORT) &&
1728 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1730 if ((fbmask & FIMB6_DPORT) &&
1731 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1733 if (fbmask & FIMB6_SADDR) {
1734 for (i = 0; i < 4; i++)
1735 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1736 (pkt->fi6_src.s6_addr32[i] &
1737 filt->ff_mask6.mask6_src.s6_addr32[i]))
1740 if (fbmask & FIMB6_DADDR) {
1741 for (i = 0; i < 4; i++)
1742 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1743 (pkt->fi6_dst.s6_addr32[i] &
1744 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1747 if ((fbmask & FIMB6_TCLASS) &&
1748 filt->ff_flow6.fi6_tclass !=
1749 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1751 if ((fbmask & FIMB6_GPI) &&
1752 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1761 * bit 20-28: index to the filter hash table
1762 * bit 0-19: unique id in the hash bucket.
1765 get_filt_handle(classifier, i)
1766 struct acc_classifier *classifier;
1769 static u_long handle_number = 1;
1771 struct acc_filter *afp;
1774 handle = handle_number++ & 0x000fffff;
1776 if (LIST_EMPTY(&classifier->acc_filters[i]))
1779 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1780 if ((afp->f_handle & 0x000fffff) == handle)
1784 /* this handle is already used, try again */
1787 return ((i << 20) | handle);
1790 /* convert filter handle to filter pointer */
1791 static struct acc_filter *
1792 filth_to_filtp(classifier, handle)
1793 struct acc_classifier *classifier;
1796 struct acc_filter *afp;
1799 i = ACC_GET_HINDEX(handle);
1801 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1802 if (afp->f_handle == handle)
1808 /* create flowinfo bitmask */
1811 struct flow_filter *filt;
1815 struct flow_filter6 *filt6;
1818 switch (filt->ff_flow.fi_family) {
1820 if (filt->ff_flow.fi_proto != 0)
1821 mask |= FIMB4_PROTO;
1822 if (filt->ff_flow.fi_tos != 0)
1824 if (filt->ff_flow.fi_dst.s_addr != 0)
1825 mask |= FIMB4_DADDR;
1826 if (filt->ff_flow.fi_src.s_addr != 0)
1827 mask |= FIMB4_SADDR;
1828 if (filt->ff_flow.fi_sport != 0)
1829 mask |= FIMB4_SPORT;
1830 if (filt->ff_flow.fi_dport != 0)
1831 mask |= FIMB4_DPORT;
1832 if (filt->ff_flow.fi_gpi != 0)
1837 filt6 = (struct flow_filter6 *)filt;
1839 if (filt6->ff_flow6.fi6_proto != 0)
1840 mask |= FIMB6_PROTO;
1841 if (filt6->ff_flow6.fi6_tclass != 0)
1842 mask |= FIMB6_TCLASS;
1843 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1844 mask |= FIMB6_DADDR;
1845 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1846 mask |= FIMB6_SADDR;
1847 if (filt6->ff_flow6.fi6_sport != 0)
1848 mask |= FIMB6_SPORT;
1849 if (filt6->ff_flow6.fi6_dport != 0)
1850 mask |= FIMB6_DPORT;
1851 if (filt6->ff_flow6.fi6_gpi != 0)
1853 if (filt6->ff_flow6.fi6_flowlabel != 0)
1854 mask |= FIMB6_FLABEL;
1863 * helper functions to handle IPv4 fragments.
1864 * currently only in-sequence fragments are handled.
1865 * - fragment info is cached in a LRU list.
1866 * - when a first fragment is found, cache its flow info.
1867 * - when a non-first fragment is found, lookup the cache.
1871 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1874 struct flowinfo_in ip4f_info;
1877 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1879 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1885 struct flowinfo_in *fin;
1887 struct ip4_frag *fp;
1889 if (TAILQ_EMPTY(&ip4f_list)) {
1890 /* first time call, allocate fragment cache entries. */
1891 if (ip4f_init() < 0)
1892 /* allocation failed! */
1897 fp->ip4f_id = ip->ip_id;
1898 fp->ip4f_info.fi_proto = ip->ip_p;
1899 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1900 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1902 /* save port numbers */
1903 fp->ip4f_info.fi_sport = fin->fi_sport;
1904 fp->ip4f_info.fi_dport = fin->fi_dport;
1905 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1909 ip4f_lookup(ip, fin)
1911 struct flowinfo_in *fin;
1913 struct ip4_frag *fp;
1915 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1916 fp = TAILQ_NEXT(fp, ip4f_chain))
1917 if (ip->ip_id == fp->ip4f_id &&
1918 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1919 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1920 ip->ip_p == fp->ip4f_info.fi_proto) {
1922 /* found the matching entry */
1923 fin->fi_sport = fp->ip4f_info.fi_sport;
1924 fin->fi_dport = fp->ip4f_info.fi_dport;
1925 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1927 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1928 /* this is the last fragment,
1929 release the entry. */
1935 /* no matching entry found */
1942 struct ip4_frag *fp;
1945 TAILQ_INIT(&ip4f_list);
1946 for (i=0; i<IP4F_TABSIZE; i++) {
1947 fp = malloc(sizeof(struct ip4_frag),
1948 M_DEVBUF, M_NOWAIT);
1950 printf("ip4f_init: can't alloc %dth entry!\n", i);
1956 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1961 static struct ip4_frag *
1964 struct ip4_frag *fp;
1966 /* reclaim an entry at the tail, put it at the head */
1967 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1968 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1970 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1976 struct ip4_frag *fp;
1978 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1980 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1983 #endif /* ALTQ3_CLFIER_COMPAT */