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 $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/kernel.h>
42 #include <sys/errno.h>
43 #include <sys/syslog.h>
44 #include <sys/sysctl.h>
45 #include <sys/queue.h>
48 #include <net/if_var.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 #include <net/altq/altq_conf.h>
69 /* machine dependent clock related includes */
72 #include <sys/eventhandler.h>
73 #include <machine/clock.h>
74 #if defined(__amd64__) || defined(__i386__)
75 #include <machine/cpufunc.h> /* for pentium tsc */
76 #include <machine/specialreg.h> /* for CPUID_TSC */
77 #include <machine/md_var.h> /* for cpu_feature */
78 #endif /* __amd64 || __i386__ */
81 * internal function prototypes
83 static void tbr_timeout(void *);
84 int (*altq_input)(struct mbuf *, int) = NULL;
85 static struct mbuf *tbr_dequeue(struct ifaltq *, int);
86 static int tbr_timer = 0; /* token bucket regulator timer */
87 #if !defined(__FreeBSD__) || (__FreeBSD_version < 600000)
88 static struct callout tbr_callout = CALLOUT_INITIALIZER;
90 static struct callout tbr_callout;
93 #ifdef ALTQ3_CLFIER_COMPAT
94 static int extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
96 static int extract_ports6(struct mbuf *, struct ip6_hdr *,
97 struct flowinfo_in6 *);
99 static int apply_filter4(u_int32_t, struct flow_filter *,
100 struct flowinfo_in *);
101 static int apply_ppfilter4(u_int32_t, struct flow_filter *,
102 struct flowinfo_in *);
104 static int apply_filter6(u_int32_t, struct flow_filter6 *,
105 struct flowinfo_in6 *);
107 static int apply_tosfilter4(u_int32_t, struct flow_filter *,
108 struct flowinfo_in *);
109 static u_long get_filt_handle(struct acc_classifier *, int);
110 static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
111 static u_int32_t filt2fibmask(struct flow_filter *);
113 static void ip4f_cache(struct ip *, struct flowinfo_in *);
114 static int ip4f_lookup(struct ip *, struct flowinfo_in *);
115 static int ip4f_init(void);
116 static struct ip4_frag *ip4f_alloc(void);
117 static void ip4f_free(struct ip4_frag *);
118 #endif /* ALTQ3_CLFIER_COMPAT */
121 * alternate queueing support routines
124 /* look up the queue state by the interface name and the queueing type. */
126 altq_lookup(name, type)
132 if ((ifp = ifunit(name)) != NULL) {
133 /* read if_snd unlocked */
134 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
135 return (ifp->if_snd.altq_disc);
142 altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
146 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
147 struct mbuf *(*dequeue)(struct ifaltq *, int);
148 int (*request)(struct ifaltq *, int, void *);
150 void *(*classify)(void *, struct mbuf *, int);
153 if (!ALTQ_IS_READY(ifq)) {
160 * pfaltq can override the existing discipline, but altq3 cannot.
161 * check these if clfier is not NULL (which implies altq3).
163 if (clfier != NULL) {
164 if (ALTQ_IS_ENABLED(ifq)) {
168 if (ALTQ_IS_ATTACHED(ifq)) {
174 ifq->altq_type = type;
175 ifq->altq_disc = discipline;
176 ifq->altq_enqueue = enqueue;
177 ifq->altq_dequeue = dequeue;
178 ifq->altq_request = request;
179 ifq->altq_clfier = clfier;
180 ifq->altq_classify = classify;
181 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
184 altq_module_incref(type);
197 if (!ALTQ_IS_READY(ifq)) {
201 if (ALTQ_IS_ENABLED(ifq)) {
205 if (!ALTQ_IS_ATTACHED(ifq)) {
211 altq_module_declref(ifq->altq_type);
215 ifq->altq_type = ALTQT_NONE;
216 ifq->altq_disc = NULL;
217 ifq->altq_enqueue = NULL;
218 ifq->altq_dequeue = NULL;
219 ifq->altq_request = NULL;
220 ifq->altq_clfier = NULL;
221 ifq->altq_classify = NULL;
222 ifq->altq_flags &= ALTQF_CANTCHANGE;
236 if (!ALTQ_IS_READY(ifq)) {
240 if (ALTQ_IS_ENABLED(ifq)) {
246 IFQ_PURGE_NOLOCK(ifq);
247 ASSERT(ifq->ifq_len == 0);
248 ifq->ifq_drv_maxlen = 0; /* disable bulk dequeue */
249 ifq->altq_flags |= ALTQF_ENABLED;
250 if (ifq->altq_clfier != NULL)
251 ifq->altq_flags |= ALTQF_CLASSIFY;
265 if (!ALTQ_IS_ENABLED(ifq)) {
271 IFQ_PURGE_NOLOCK(ifq);
272 ASSERT(ifq->ifq_len == 0);
273 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
282 altq_assert(file, line, failedexpr)
283 const char *file, *failedexpr;
286 (void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
287 failedexpr, file, line);
288 panic("altq assertion");
294 * internal representation of token bucket parameters
295 * rate: byte_per_unittime << 32
296 * (((bits_per_sec) / 8) << 32) / machclk_freq
301 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
302 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
309 struct tb_regulator *tbr;
314 IFQ_LOCK_ASSERT(ifq);
316 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
317 /* if this is a remove after poll, bypass tbr check */
319 /* update token only when it is negative */
320 if (tbr->tbr_token <= 0) {
321 now = read_machclk();
322 interval = now - tbr->tbr_last;
323 if (interval >= tbr->tbr_filluptime)
324 tbr->tbr_token = tbr->tbr_depth;
326 tbr->tbr_token += interval * tbr->tbr_rate;
327 if (tbr->tbr_token > tbr->tbr_depth)
328 tbr->tbr_token = tbr->tbr_depth;
332 /* if token is still negative, don't allow dequeue */
333 if (tbr->tbr_token <= 0)
337 if (ALTQ_IS_ENABLED(ifq))
338 m = (*ifq->altq_dequeue)(ifq, op);
340 if (op == ALTDQ_POLL)
346 if (m != NULL && op == ALTDQ_REMOVE)
347 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
348 tbr->tbr_lastop = op;
353 * set a token bucket regulator.
354 * if the specified rate is zero, the token bucket regulator is deleted.
357 tbr_set(ifq, profile)
359 struct tb_profile *profile;
361 struct tb_regulator *tbr, *otbr;
363 if (tbr_dequeue_ptr == NULL)
364 tbr_dequeue_ptr = tbr_dequeue;
366 if (machclk_freq == 0)
368 if (machclk_freq == 0) {
369 printf("tbr_set: no cpu clock available!\n");
374 if (profile->rate == 0) {
375 /* delete this tbr */
376 if ((tbr = ifq->altq_tbr) == NULL) {
380 ifq->altq_tbr = NULL;
386 tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_NOWAIT | M_ZERO);
392 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
393 tbr->tbr_depth = TBR_SCALE(profile->depth);
394 if (tbr->tbr_rate > 0)
395 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
397 tbr->tbr_filluptime = 0xffffffffffffffffLL;
398 tbr->tbr_token = tbr->tbr_depth;
399 tbr->tbr_last = read_machclk();
400 tbr->tbr_lastop = ALTDQ_REMOVE;
402 otbr = ifq->altq_tbr;
403 ifq->altq_tbr = tbr; /* set the new tbr */
406 free(otbr, M_DEVBUF);
408 if (tbr_timer == 0) {
409 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
418 * tbr_timeout goes through the interface list, and kicks the drivers
427 VNET_ITERATOR_DECL(vnet_iter);
433 IFNET_RLOCK_NOSLEEP();
434 VNET_LIST_RLOCK_NOSLEEP();
435 VNET_FOREACH(vnet_iter) {
436 CURVNET_SET(vnet_iter);
437 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
438 ifp = TAILQ_NEXT(ifp, if_list)) {
439 /* read from if_snd unlocked */
440 if (!TBR_IS_ENABLED(&ifp->if_snd))
443 if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
444 ifp->if_start != NULL)
445 (*ifp->if_start)(ifp);
449 VNET_LIST_RUNLOCK_NOSLEEP();
450 IFNET_RUNLOCK_NOSLEEP();
453 CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
455 tbr_timer = 0; /* don't need tbr_timer anymore */
459 * get token bucket regulator profile
462 tbr_get(ifq, profile)
464 struct tb_profile *profile;
466 struct tb_regulator *tbr;
469 if ((tbr = ifq->altq_tbr) == NULL) {
474 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
475 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
482 * attach a discipline to the interface. if one already exists, it is
484 * Locking is done in the discipline specific attach functions. Basically
485 * they call back to altq_attach which takes care of the attach and locking.
488 altq_pfattach(struct pf_altq *a)
492 switch (a->scheduler) {
497 error = cbq_pfattach(a);
502 error = priq_pfattach(a);
507 error = hfsc_pfattach(a);
512 error = fairq_pfattach(a);
523 * detach a discipline from the interface.
524 * it is possible that the discipline was already overridden by another
528 altq_pfdetach(struct pf_altq *a)
533 if ((ifp = ifunit(a->ifname)) == NULL)
536 /* if this discipline is no longer referenced, just return */
537 /* read unlocked from if_snd */
538 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
542 /* read unlocked from if_snd, _disable and _detach take care */
543 if (ALTQ_IS_ENABLED(&ifp->if_snd))
544 error = altq_disable(&ifp->if_snd);
546 error = altq_detach(&ifp->if_snd);
553 * add a discipline or a queue
554 * Locking is done in the discipline specific functions with regards to
555 * malloc with WAITOK, also it is not yet clear which lock to use.
558 altq_add(struct pf_altq *a)
562 if (a->qname[0] != 0)
563 return (altq_add_queue(a));
565 if (machclk_freq == 0)
567 if (machclk_freq == 0)
568 panic("altq_add: no cpu clock");
570 switch (a->scheduler) {
573 error = cbq_add_altq(a);
578 error = priq_add_altq(a);
583 error = hfsc_add_altq(a);
588 error = fairq_add_altq(a);
599 * remove a discipline or a queue
600 * It is yet unclear what lock to use to protect this operation, the
601 * discipline specific functions will determine and grab it
604 altq_remove(struct pf_altq *a)
608 if (a->qname[0] != 0)
609 return (altq_remove_queue(a));
611 switch (a->scheduler) {
614 error = cbq_remove_altq(a);
619 error = priq_remove_altq(a);
624 error = hfsc_remove_altq(a);
629 error = fairq_remove_altq(a);
640 * add a queue to the discipline
641 * It is yet unclear what lock to use to protect this operation, the
642 * discipline specific functions will determine and grab it
645 altq_add_queue(struct pf_altq *a)
649 switch (a->scheduler) {
652 error = cbq_add_queue(a);
657 error = priq_add_queue(a);
662 error = hfsc_add_queue(a);
667 error = fairq_add_queue(a);
678 * remove a queue from the discipline
679 * It is yet unclear what lock to use to protect this operation, the
680 * discipline specific functions will determine and grab it
683 altq_remove_queue(struct pf_altq *a)
687 switch (a->scheduler) {
690 error = cbq_remove_queue(a);
695 error = priq_remove_queue(a);
700 error = hfsc_remove_queue(a);
705 error = fairq_remove_queue(a);
716 * get queue statistics
717 * Locking is done in the discipline specific functions with regards to
718 * copyout operations, also it is not yet clear which lock to use.
721 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
725 switch (a->scheduler) {
728 error = cbq_getqstats(a, ubuf, nbytes);
733 error = priq_getqstats(a, ubuf, nbytes);
738 error = hfsc_getqstats(a, ubuf, nbytes);
743 error = fairq_getqstats(a, ubuf, nbytes);
754 * read and write diffserv field in IPv4 or IPv6 header
757 read_dsfield(m, pktattr)
759 struct altq_pktattr *pktattr;
762 u_int8_t ds_field = 0;
764 if (pktattr == NULL ||
765 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
766 return ((u_int8_t)0);
768 /* verify that pattr_hdr is within the mbuf data */
769 for (m0 = m; m0 != NULL; m0 = m0->m_next)
770 if ((pktattr->pattr_hdr >= m0->m_data) &&
771 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
774 /* ick, pattr_hdr is stale */
775 pktattr->pattr_af = AF_UNSPEC;
777 printf("read_dsfield: can't locate header!\n");
779 return ((u_int8_t)0);
782 if (pktattr->pattr_af == AF_INET) {
783 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
786 return ((u_int8_t)0); /* version mismatch! */
787 ds_field = ip->ip_tos;
790 else if (pktattr->pattr_af == AF_INET6) {
791 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
794 flowlabel = ntohl(ip6->ip6_flow);
795 if ((flowlabel >> 28) != 6)
796 return ((u_int8_t)0); /* version mismatch! */
797 ds_field = (flowlabel >> 20) & 0xff;
804 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
808 if (pktattr == NULL ||
809 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
812 /* verify that pattr_hdr is within the mbuf data */
813 for (m0 = m; m0 != NULL; m0 = m0->m_next)
814 if ((pktattr->pattr_hdr >= m0->m_data) &&
815 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
818 /* ick, pattr_hdr is stale */
819 pktattr->pattr_af = AF_UNSPEC;
821 printf("write_dsfield: can't locate header!\n");
826 if (pktattr->pattr_af == AF_INET) {
827 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
832 return; /* version mismatch! */
834 dsfield |= old & 3; /* leave CU bits */
837 ip->ip_tos = dsfield;
839 * update checksum (from RFC1624)
840 * HC' = ~(~HC + ~m + m')
842 sum = ~ntohs(ip->ip_sum) & 0xffff;
843 sum += 0xff00 + (~old & 0xff) + dsfield;
844 sum = (sum >> 16) + (sum & 0xffff);
845 sum += (sum >> 16); /* add carry */
847 ip->ip_sum = htons(~sum & 0xffff);
850 else if (pktattr->pattr_af == AF_INET6) {
851 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
854 flowlabel = ntohl(ip6->ip6_flow);
855 if ((flowlabel >> 28) != 6)
856 return; /* version mismatch! */
857 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
858 ip6->ip6_flow = htonl(flowlabel);
866 * high resolution clock support taking advantage of a machine dependent
867 * high resolution time counter (e.g., timestamp counter of intel pentium).
869 * - 64-bit-long monotonically-increasing counter
870 * - frequency range is 100M-4GHz (CPU speed)
872 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
873 #define MACHCLK_SHIFT 8
876 u_int32_t machclk_freq;
877 u_int32_t machclk_per_tick;
879 #if defined(__i386__) && defined(__NetBSD__)
880 extern u_int64_t cpu_tsc_freq;
883 #if (__FreeBSD_version >= 700035)
884 /* Update TSC freq with the value indicated by the caller. */
886 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
888 /* If there was an error during the transition, don't do anything. */
892 #if (__FreeBSD_version >= 701102) && (defined(__amd64__) || defined(__i386__))
893 /* If TSC is P-state invariant, don't do anything. */
894 if (tsc_is_invariant)
898 /* Total setting for this level gives the new frequency in MHz. */
901 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
902 EVENTHANDLER_PRI_LAST);
903 #endif /* __FreeBSD_version >= 700035 */
906 init_machclk_setup(void)
908 #if (__FreeBSD_version >= 600000)
909 callout_init(&tbr_callout, 0);
914 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
917 #if defined(__FreeBSD__) && defined(SMP)
920 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
923 #if defined(__amd64__) || defined(__i386__)
924 /* check if TSC is available */
925 if ((cpu_feature & CPUID_TSC) == 0 ||
926 atomic_load_acq_64(&tsc_freq) == 0)
936 /* Call one-time initialization function. */
938 init_machclk_setup();
942 if (machclk_usepcc == 0) {
943 /* emulate 256MHz using microtime() */
944 machclk_freq = 1000000 << MACHCLK_SHIFT;
945 machclk_per_tick = machclk_freq / hz;
947 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
953 * if the clock frequency (of Pentium TSC or Alpha PCC) is
954 * accessible, just use it.
956 #if defined(__amd64__) || defined(__i386__)
957 machclk_freq = atomic_load_acq_64(&tsc_freq);
961 * if we don't know the clock frequency, measure it.
963 if (machclk_freq == 0) {
965 struct timeval tv_start, tv_end;
966 u_int64_t start, end, diff;
969 microtime(&tv_start);
970 start = read_machclk();
971 timo = hz; /* 1 sec */
972 (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
974 end = read_machclk();
975 diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
976 + tv_end.tv_usec - tv_start.tv_usec;
978 machclk_freq = (u_int)((end - start) * 1000000 / diff);
981 machclk_per_tick = machclk_freq / hz;
984 printf("altq: CPU clock: %uHz\n", machclk_freq);
988 #if defined(__OpenBSD__) && defined(__i386__)
989 static __inline u_int64_t
993 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
996 #endif /* __OpenBSD__ && __i386__ */
1003 if (machclk_usepcc) {
1004 #if defined(__amd64__) || defined(__i386__)
1007 panic("read_machclk");
1013 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1014 + tv.tv_usec) << MACHCLK_SHIFT);
1019 #ifdef ALTQ3_CLFIER_COMPAT
1022 #define IPPROTO_ESP 50 /* encapsulating security payload */
1025 #define IPPROTO_AH 51 /* authentication header */
1029 * extract flow information from a given packet.
1030 * filt_mask shows flowinfo fields required.
1031 * we assume the ip header is in one mbuf, and addresses and ports are
1032 * in network byte order.
1035 altq_extractflow(m, af, flow, filt_bmask)
1038 struct flowinfo *flow;
1039 u_int32_t filt_bmask;
1044 struct flowinfo_in *fin;
1047 ip = mtod(m, struct ip *);
1052 fin = (struct flowinfo_in *)flow;
1053 fin->fi_len = sizeof(struct flowinfo_in);
1054 fin->fi_family = AF_INET;
1056 fin->fi_proto = ip->ip_p;
1057 fin->fi_tos = ip->ip_tos;
1059 fin->fi_src.s_addr = ip->ip_src.s_addr;
1060 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1062 if (filt_bmask & FIMB4_PORTS)
1063 /* if port info is required, extract port numbers */
1064 extract_ports4(m, ip, fin);
1075 struct flowinfo_in6 *fin6;
1076 struct ip6_hdr *ip6;
1078 ip6 = mtod(m, struct ip6_hdr *);
1079 /* should we check the ip version? */
1081 fin6 = (struct flowinfo_in6 *)flow;
1082 fin6->fi6_len = sizeof(struct flowinfo_in6);
1083 fin6->fi6_family = AF_INET6;
1085 fin6->fi6_proto = ip6->ip6_nxt;
1086 fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1088 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1089 fin6->fi6_src = ip6->ip6_src;
1090 fin6->fi6_dst = ip6->ip6_dst;
1092 if ((filt_bmask & FIMB6_PORTS) ||
1093 ((filt_bmask & FIMB6_PROTO)
1094 && ip6->ip6_nxt > IPPROTO_IPV6))
1096 * if port info is required, or proto is required
1097 * but there are option headers, extract port
1098 * and protocol numbers.
1100 extract_ports6(m, ip6, fin6);
1102 fin6->fi6_sport = 0;
1103 fin6->fi6_dport = 0;
1115 flow->fi_len = sizeof(struct flowinfo);
1116 flow->fi_family = AF_UNSPEC;
1121 * helper routine to extract port numbers
1123 /* structure for ipsec and ipv6 option header template */
1125 u_int8_t opt6_nxt; /* next header */
1126 u_int8_t opt6_hlen; /* header extension length */
1128 u_int32_t ah_spi; /* security parameter index
1129 for authentication header */
1133 * extract port numbers from a ipv4 packet.
1136 extract_ports4(m, ip, fin)
1139 struct flowinfo_in *fin;
1150 ip_off = ntohs(ip->ip_off);
1151 /* if it is a fragment, try cached fragment info */
1152 if (ip_off & IP_OFFMASK) {
1153 ip4f_lookup(ip, fin);
1157 /* locate the mbuf containing the protocol header */
1158 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1159 if (((caddr_t)ip >= m0->m_data) &&
1160 ((caddr_t)ip < m0->m_data + m0->m_len))
1164 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1168 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1174 while (off >= m0->m_len) {
1178 return (0); /* bogus ip_hl! */
1180 if (m0->m_len < off + 4)
1188 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1189 fin->fi_sport = udp->uh_sport;
1190 fin->fi_dport = udp->uh_dport;
1191 fin->fi_proto = proto;
1197 if (fin->fi_gpi == 0){
1200 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1203 fin->fi_proto = proto;
1207 /* get next header and header length */
1210 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1211 proto = opt6->opt6_nxt;
1212 off += 8 + (opt6->opt6_hlen * 4);
1213 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1214 fin->fi_gpi = opt6->ah_spi;
1216 /* goto the next header */
1218 #endif /* ALTQ_IPSEC */
1221 fin->fi_proto = proto;
1225 /* if this is a first fragment, cache it. */
1227 ip4f_cache(ip, fin);
1234 extract_ports6(m, ip6, fin6)
1236 struct ip6_hdr *ip6;
1237 struct flowinfo_in6 *fin6;
1244 fin6->fi6_sport = 0;
1245 fin6->fi6_dport = 0;
1247 /* locate the mbuf containing the protocol header */
1248 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1249 if (((caddr_t)ip6 >= m0->m_data) &&
1250 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1254 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1258 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1260 proto = ip6->ip6_nxt;
1262 while (off >= m0->m_len) {
1268 if (m0->m_len < off + 4)
1276 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1277 fin6->fi6_sport = udp->uh_sport;
1278 fin6->fi6_dport = udp->uh_dport;
1279 fin6->fi6_proto = proto;
1284 if (fin6->fi6_gpi == 0) {
1287 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1288 fin6->fi6_gpi = *gpi;
1290 fin6->fi6_proto = proto;
1294 /* get next header and header length */
1297 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1298 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1299 fin6->fi6_gpi = opt6->ah_spi;
1300 proto = opt6->opt6_nxt;
1301 off += 8 + (opt6->opt6_hlen * 4);
1302 /* goto the next header */
1306 case IPPROTO_HOPOPTS:
1307 case IPPROTO_ROUTING:
1308 case IPPROTO_DSTOPTS: {
1309 /* get next header and header length */
1312 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1313 proto = opt6->opt6_nxt;
1314 off += (opt6->opt6_hlen + 1) * 8;
1315 /* goto the next header */
1319 case IPPROTO_FRAGMENT:
1320 /* ipv6 fragmentations are not supported yet */
1322 fin6->fi6_proto = proto;
1331 * altq common classifier
1334 acc_add_filter(classifier, filter, class, phandle)
1335 struct acc_classifier *classifier;
1336 struct flow_filter *filter;
1340 struct acc_filter *afp, *prev, *tmp;
1344 if (filter->ff_flow.fi_family != AF_INET &&
1345 filter->ff_flow.fi_family != AF_INET6)
1348 if (filter->ff_flow.fi_family != AF_INET)
1352 afp = malloc(sizeof(struct acc_filter),
1353 M_DEVBUF, M_WAITOK);
1356 bzero(afp, sizeof(struct acc_filter));
1358 afp->f_filter = *filter;
1359 afp->f_class = class;
1361 i = ACC_WILDCARD_INDEX;
1362 if (filter->ff_flow.fi_family == AF_INET) {
1363 struct flow_filter *filter4 = &afp->f_filter;
1366 * if address is 0, it's a wildcard. if address mask
1367 * isn't set, use full mask.
1369 if (filter4->ff_flow.fi_dst.s_addr == 0)
1370 filter4->ff_mask.mask_dst.s_addr = 0;
1371 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1372 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1373 if (filter4->ff_flow.fi_src.s_addr == 0)
1374 filter4->ff_mask.mask_src.s_addr = 0;
1375 else if (filter4->ff_mask.mask_src.s_addr == 0)
1376 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1378 /* clear extra bits in addresses */
1379 filter4->ff_flow.fi_dst.s_addr &=
1380 filter4->ff_mask.mask_dst.s_addr;
1381 filter4->ff_flow.fi_src.s_addr &=
1382 filter4->ff_mask.mask_src.s_addr;
1385 * if dst address is a wildcard, use hash-entry
1386 * ACC_WILDCARD_INDEX.
1388 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1389 i = ACC_WILDCARD_INDEX;
1391 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1394 else if (filter->ff_flow.fi_family == AF_INET6) {
1395 struct flow_filter6 *filter6 =
1396 (struct flow_filter6 *)&afp->f_filter;
1397 #ifndef IN6MASK0 /* taken from kame ipv6 */
1398 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1399 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1400 const struct in6_addr in6mask0 = IN6MASK0;
1401 const struct in6_addr in6mask128 = IN6MASK128;
1404 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1405 filter6->ff_mask6.mask6_dst = in6mask0;
1406 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1407 filter6->ff_mask6.mask6_dst = in6mask128;
1408 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1409 filter6->ff_mask6.mask6_src = in6mask0;
1410 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1411 filter6->ff_mask6.mask6_src = in6mask128;
1413 /* clear extra bits in addresses */
1414 for (i = 0; i < 16; i++)
1415 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1416 filter6->ff_mask6.mask6_dst.s6_addr[i];
1417 for (i = 0; i < 16; i++)
1418 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1419 filter6->ff_mask6.mask6_src.s6_addr[i];
1421 if (filter6->ff_flow6.fi6_flowlabel == 0)
1422 i = ACC_WILDCARD_INDEX;
1424 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1428 afp->f_handle = get_filt_handle(classifier, i);
1430 /* update filter bitmask */
1431 afp->f_fbmask = filt2fibmask(filter);
1432 classifier->acc_fbmask |= afp->f_fbmask;
1435 * add this filter to the filter list.
1436 * filters are ordered from the highest rule number.
1440 LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1441 if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1447 LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1449 LIST_INSERT_AFTER(prev, afp, f_chain);
1452 *phandle = afp->f_handle;
1457 acc_delete_filter(classifier, handle)
1458 struct acc_classifier *classifier;
1461 struct acc_filter *afp;
1464 if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1468 LIST_REMOVE(afp, f_chain);
1471 free(afp, M_DEVBUF);
1473 /* todo: update filt_bmask */
1479 * delete filters referencing to the specified class.
1480 * if the all flag is not 0, delete all the filters.
1483 acc_discard_filters(classifier, class, all)
1484 struct acc_classifier *classifier;
1488 struct acc_filter *afp;
1492 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1494 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1495 if (all || afp->f_class == class) {
1496 LIST_REMOVE(afp, f_chain);
1497 free(afp, M_DEVBUF);
1498 /* start again from the head */
1501 } while (afp != NULL);
1506 classifier->acc_fbmask = 0;
1512 acc_classify(clfier, m, af)
1517 struct acc_classifier *classifier;
1518 struct flowinfo flow;
1519 struct acc_filter *afp;
1522 classifier = (struct acc_classifier *)clfier;
1523 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1525 if (flow.fi_family == AF_INET) {
1526 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1528 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1529 /* only tos is used */
1531 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1533 if (apply_tosfilter4(afp->f_fbmask,
1534 &afp->f_filter, fp))
1535 /* filter matched */
1536 return (afp->f_class);
1537 } else if ((classifier->acc_fbmask &
1538 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1540 /* only proto and ports are used */
1542 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1544 if (apply_ppfilter4(afp->f_fbmask,
1545 &afp->f_filter, fp))
1546 /* filter matched */
1547 return (afp->f_class);
1549 /* get the filter hash entry from its dest address */
1550 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1553 * go through this loop twice. first for dst
1554 * hash, second for wildcards.
1556 LIST_FOREACH(afp, &classifier->acc_filters[i],
1558 if (apply_filter4(afp->f_fbmask,
1559 &afp->f_filter, fp))
1560 /* filter matched */
1561 return (afp->f_class);
1564 * check again for filters with a dst addr
1566 * (daddr == 0 || dmask != 0xffffffff).
1568 if (i != ACC_WILDCARD_INDEX)
1569 i = ACC_WILDCARD_INDEX;
1576 else if (flow.fi_family == AF_INET6) {
1577 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1579 /* get the filter hash entry from its flow ID */
1580 if (fp6->fi6_flowlabel != 0)
1581 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1583 /* flowlable can be zero */
1584 i = ACC_WILDCARD_INDEX;
1586 /* go through this loop twice. first for flow hash, second
1589 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1590 if (apply_filter6(afp->f_fbmask,
1591 (struct flow_filter6 *)&afp->f_filter,
1593 /* filter matched */
1594 return (afp->f_class);
1597 * check again for filters with a wildcard.
1599 if (i != ACC_WILDCARD_INDEX)
1600 i = ACC_WILDCARD_INDEX;
1607 /* no filter matched */
1612 apply_filter4(fbmask, filt, pkt)
1614 struct flow_filter *filt;
1615 struct flowinfo_in *pkt;
1617 if (filt->ff_flow.fi_family != AF_INET)
1619 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1621 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1623 if ((fbmask & FIMB4_DADDR) &&
1624 filt->ff_flow.fi_dst.s_addr !=
1625 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1627 if ((fbmask & FIMB4_SADDR) &&
1628 filt->ff_flow.fi_src.s_addr !=
1629 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1631 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1633 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1634 (pkt->fi_tos & filt->ff_mask.mask_tos))
1636 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1643 * filter matching function optimized for a common case that checks
1644 * only protocol and port numbers
1647 apply_ppfilter4(fbmask, filt, pkt)
1649 struct flow_filter *filt;
1650 struct flowinfo_in *pkt;
1652 if (filt->ff_flow.fi_family != AF_INET)
1654 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1656 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1658 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1665 * filter matching function only for tos field.
1668 apply_tosfilter4(fbmask, filt, pkt)
1670 struct flow_filter *filt;
1671 struct flowinfo_in *pkt;
1673 if (filt->ff_flow.fi_family != AF_INET)
1675 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1676 (pkt->fi_tos & filt->ff_mask.mask_tos))
1684 apply_filter6(fbmask, filt, pkt)
1686 struct flow_filter6 *filt;
1687 struct flowinfo_in6 *pkt;
1691 if (filt->ff_flow6.fi6_family != AF_INET6)
1693 if ((fbmask & FIMB6_FLABEL) &&
1694 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1696 if ((fbmask & FIMB6_PROTO) &&
1697 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1699 if ((fbmask & FIMB6_SPORT) &&
1700 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1702 if ((fbmask & FIMB6_DPORT) &&
1703 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1705 if (fbmask & FIMB6_SADDR) {
1706 for (i = 0; i < 4; i++)
1707 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1708 (pkt->fi6_src.s6_addr32[i] &
1709 filt->ff_mask6.mask6_src.s6_addr32[i]))
1712 if (fbmask & FIMB6_DADDR) {
1713 for (i = 0; i < 4; i++)
1714 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1715 (pkt->fi6_dst.s6_addr32[i] &
1716 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1719 if ((fbmask & FIMB6_TCLASS) &&
1720 filt->ff_flow6.fi6_tclass !=
1721 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1723 if ((fbmask & FIMB6_GPI) &&
1724 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1733 * bit 20-28: index to the filter hash table
1734 * bit 0-19: unique id in the hash bucket.
1737 get_filt_handle(classifier, i)
1738 struct acc_classifier *classifier;
1741 static u_long handle_number = 1;
1743 struct acc_filter *afp;
1746 handle = handle_number++ & 0x000fffff;
1748 if (LIST_EMPTY(&classifier->acc_filters[i]))
1751 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1752 if ((afp->f_handle & 0x000fffff) == handle)
1756 /* this handle is already used, try again */
1759 return ((i << 20) | handle);
1762 /* convert filter handle to filter pointer */
1763 static struct acc_filter *
1764 filth_to_filtp(classifier, handle)
1765 struct acc_classifier *classifier;
1768 struct acc_filter *afp;
1771 i = ACC_GET_HINDEX(handle);
1773 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1774 if (afp->f_handle == handle)
1780 /* create flowinfo bitmask */
1783 struct flow_filter *filt;
1787 struct flow_filter6 *filt6;
1790 switch (filt->ff_flow.fi_family) {
1792 if (filt->ff_flow.fi_proto != 0)
1793 mask |= FIMB4_PROTO;
1794 if (filt->ff_flow.fi_tos != 0)
1796 if (filt->ff_flow.fi_dst.s_addr != 0)
1797 mask |= FIMB4_DADDR;
1798 if (filt->ff_flow.fi_src.s_addr != 0)
1799 mask |= FIMB4_SADDR;
1800 if (filt->ff_flow.fi_sport != 0)
1801 mask |= FIMB4_SPORT;
1802 if (filt->ff_flow.fi_dport != 0)
1803 mask |= FIMB4_DPORT;
1804 if (filt->ff_flow.fi_gpi != 0)
1809 filt6 = (struct flow_filter6 *)filt;
1811 if (filt6->ff_flow6.fi6_proto != 0)
1812 mask |= FIMB6_PROTO;
1813 if (filt6->ff_flow6.fi6_tclass != 0)
1814 mask |= FIMB6_TCLASS;
1815 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1816 mask |= FIMB6_DADDR;
1817 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1818 mask |= FIMB6_SADDR;
1819 if (filt6->ff_flow6.fi6_sport != 0)
1820 mask |= FIMB6_SPORT;
1821 if (filt6->ff_flow6.fi6_dport != 0)
1822 mask |= FIMB6_DPORT;
1823 if (filt6->ff_flow6.fi6_gpi != 0)
1825 if (filt6->ff_flow6.fi6_flowlabel != 0)
1826 mask |= FIMB6_FLABEL;
1835 * helper functions to handle IPv4 fragments.
1836 * currently only in-sequence fragments are handled.
1837 * - fragment info is cached in a LRU list.
1838 * - when a first fragment is found, cache its flow info.
1839 * - when a non-first fragment is found, lookup the cache.
1843 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1846 struct flowinfo_in ip4f_info;
1849 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1851 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1857 struct flowinfo_in *fin;
1859 struct ip4_frag *fp;
1861 if (TAILQ_EMPTY(&ip4f_list)) {
1862 /* first time call, allocate fragment cache entries. */
1863 if (ip4f_init() < 0)
1864 /* allocation failed! */
1869 fp->ip4f_id = ip->ip_id;
1870 fp->ip4f_info.fi_proto = ip->ip_p;
1871 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1872 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1874 /* save port numbers */
1875 fp->ip4f_info.fi_sport = fin->fi_sport;
1876 fp->ip4f_info.fi_dport = fin->fi_dport;
1877 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1881 ip4f_lookup(ip, fin)
1883 struct flowinfo_in *fin;
1885 struct ip4_frag *fp;
1887 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1888 fp = TAILQ_NEXT(fp, ip4f_chain))
1889 if (ip->ip_id == fp->ip4f_id &&
1890 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1891 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1892 ip->ip_p == fp->ip4f_info.fi_proto) {
1894 /* found the matching entry */
1895 fin->fi_sport = fp->ip4f_info.fi_sport;
1896 fin->fi_dport = fp->ip4f_info.fi_dport;
1897 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1899 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1900 /* this is the last fragment,
1901 release the entry. */
1907 /* no matching entry found */
1914 struct ip4_frag *fp;
1917 TAILQ_INIT(&ip4f_list);
1918 for (i=0; i<IP4F_TABSIZE; i++) {
1919 fp = malloc(sizeof(struct ip4_frag),
1920 M_DEVBUF, M_NOWAIT);
1922 printf("ip4f_init: can't alloc %dth entry!\n", i);
1928 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1933 static struct ip4_frag *
1936 struct ip4_frag *fp;
1938 /* reclaim an entry at the tail, put it at the head */
1939 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1940 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1942 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1948 struct ip4_frag *fp;
1950 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1952 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1955 #endif /* ALTQ3_CLFIER_COMPAT */