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);
517 error = codel_pfattach(a);
528 * detach a discipline from the interface.
529 * it is possible that the discipline was already overridden by another
533 altq_pfdetach(struct pf_altq *a)
538 if ((ifp = ifunit(a->ifname)) == NULL)
541 /* if this discipline is no longer referenced, just return */
542 /* read unlocked from if_snd */
543 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
547 /* read unlocked from if_snd, _disable and _detach take care */
548 if (ALTQ_IS_ENABLED(&ifp->if_snd))
549 error = altq_disable(&ifp->if_snd);
551 error = altq_detach(&ifp->if_snd);
558 * add a discipline or a queue
559 * Locking is done in the discipline specific functions with regards to
560 * malloc with WAITOK, also it is not yet clear which lock to use.
563 altq_add(struct pf_altq *a)
567 if (a->qname[0] != 0)
568 return (altq_add_queue(a));
570 if (machclk_freq == 0)
572 if (machclk_freq == 0)
573 panic("altq_add: no cpu clock");
575 switch (a->scheduler) {
578 error = cbq_add_altq(a);
583 error = priq_add_altq(a);
588 error = hfsc_add_altq(a);
593 error = fairq_add_altq(a);
598 error = codel_add_altq(a);
609 * remove a discipline or a queue
610 * It is yet unclear what lock to use to protect this operation, the
611 * discipline specific functions will determine and grab it
614 altq_remove(struct pf_altq *a)
618 if (a->qname[0] != 0)
619 return (altq_remove_queue(a));
621 switch (a->scheduler) {
624 error = cbq_remove_altq(a);
629 error = priq_remove_altq(a);
634 error = hfsc_remove_altq(a);
639 error = fairq_remove_altq(a);
644 error = codel_remove_altq(a);
655 * add a queue to the discipline
656 * It is yet unclear what lock to use to protect this operation, the
657 * discipline specific functions will determine and grab it
660 altq_add_queue(struct pf_altq *a)
664 switch (a->scheduler) {
667 error = cbq_add_queue(a);
672 error = priq_add_queue(a);
677 error = hfsc_add_queue(a);
682 error = fairq_add_queue(a);
693 * remove a queue from the discipline
694 * It is yet unclear what lock to use to protect this operation, the
695 * discipline specific functions will determine and grab it
698 altq_remove_queue(struct pf_altq *a)
702 switch (a->scheduler) {
705 error = cbq_remove_queue(a);
710 error = priq_remove_queue(a);
715 error = hfsc_remove_queue(a);
720 error = fairq_remove_queue(a);
731 * get queue statistics
732 * Locking is done in the discipline specific functions with regards to
733 * copyout operations, also it is not yet clear which lock to use.
736 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
740 switch (a->scheduler) {
743 error = cbq_getqstats(a, ubuf, nbytes);
748 error = priq_getqstats(a, ubuf, nbytes);
753 error = hfsc_getqstats(a, ubuf, nbytes);
758 error = fairq_getqstats(a, ubuf, nbytes);
763 error = codel_getqstats(a, ubuf, nbytes);
774 * read and write diffserv field in IPv4 or IPv6 header
777 read_dsfield(m, pktattr)
779 struct altq_pktattr *pktattr;
782 u_int8_t ds_field = 0;
784 if (pktattr == NULL ||
785 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
786 return ((u_int8_t)0);
788 /* verify that pattr_hdr is within the mbuf data */
789 for (m0 = m; m0 != NULL; m0 = m0->m_next)
790 if ((pktattr->pattr_hdr >= m0->m_data) &&
791 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
794 /* ick, pattr_hdr is stale */
795 pktattr->pattr_af = AF_UNSPEC;
797 printf("read_dsfield: can't locate header!\n");
799 return ((u_int8_t)0);
802 if (pktattr->pattr_af == AF_INET) {
803 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
806 return ((u_int8_t)0); /* version mismatch! */
807 ds_field = ip->ip_tos;
810 else if (pktattr->pattr_af == AF_INET6) {
811 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
814 flowlabel = ntohl(ip6->ip6_flow);
815 if ((flowlabel >> 28) != 6)
816 return ((u_int8_t)0); /* version mismatch! */
817 ds_field = (flowlabel >> 20) & 0xff;
824 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
828 if (pktattr == NULL ||
829 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
832 /* verify that pattr_hdr is within the mbuf data */
833 for (m0 = m; m0 != NULL; m0 = m0->m_next)
834 if ((pktattr->pattr_hdr >= m0->m_data) &&
835 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
838 /* ick, pattr_hdr is stale */
839 pktattr->pattr_af = AF_UNSPEC;
841 printf("write_dsfield: can't locate header!\n");
846 if (pktattr->pattr_af == AF_INET) {
847 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
852 return; /* version mismatch! */
854 dsfield |= old & 3; /* leave CU bits */
857 ip->ip_tos = dsfield;
859 * update checksum (from RFC1624)
860 * HC' = ~(~HC + ~m + m')
862 sum = ~ntohs(ip->ip_sum) & 0xffff;
863 sum += 0xff00 + (~old & 0xff) + dsfield;
864 sum = (sum >> 16) + (sum & 0xffff);
865 sum += (sum >> 16); /* add carry */
867 ip->ip_sum = htons(~sum & 0xffff);
870 else if (pktattr->pattr_af == AF_INET6) {
871 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
874 flowlabel = ntohl(ip6->ip6_flow);
875 if ((flowlabel >> 28) != 6)
876 return; /* version mismatch! */
877 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
878 ip6->ip6_flow = htonl(flowlabel);
886 * high resolution clock support taking advantage of a machine dependent
887 * high resolution time counter (e.g., timestamp counter of intel pentium).
889 * - 64-bit-long monotonically-increasing counter
890 * - frequency range is 100M-4GHz (CPU speed)
892 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
893 #define MACHCLK_SHIFT 8
896 u_int32_t machclk_freq;
897 u_int32_t machclk_per_tick;
899 #if defined(__i386__) && defined(__NetBSD__)
900 extern u_int64_t cpu_tsc_freq;
903 #if (__FreeBSD_version >= 700035)
904 /* Update TSC freq with the value indicated by the caller. */
906 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
908 /* If there was an error during the transition, don't do anything. */
912 #if (__FreeBSD_version >= 701102) && (defined(__amd64__) || defined(__i386__))
913 /* If TSC is P-state invariant, don't do anything. */
914 if (tsc_is_invariant)
918 /* Total setting for this level gives the new frequency in MHz. */
921 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
922 EVENTHANDLER_PRI_LAST);
923 #endif /* __FreeBSD_version >= 700035 */
926 init_machclk_setup(void)
928 #if (__FreeBSD_version >= 600000)
929 callout_init(&tbr_callout, 0);
934 #if (!defined(__amd64__) && !defined(__i386__)) || defined(ALTQ_NOPCC)
937 #if defined(__FreeBSD__) && defined(SMP)
940 #if defined(__NetBSD__) && defined(MULTIPROCESSOR)
943 #if defined(__amd64__) || defined(__i386__)
944 /* check if TSC is available */
945 if ((cpu_feature & CPUID_TSC) == 0 ||
946 atomic_load_acq_64(&tsc_freq) == 0)
956 /* Call one-time initialization function. */
958 init_machclk_setup();
962 if (machclk_usepcc == 0) {
963 /* emulate 256MHz using microtime() */
964 machclk_freq = 1000000 << MACHCLK_SHIFT;
965 machclk_per_tick = machclk_freq / hz;
967 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
973 * if the clock frequency (of Pentium TSC or Alpha PCC) is
974 * accessible, just use it.
976 #if defined(__amd64__) || defined(__i386__)
977 machclk_freq = atomic_load_acq_64(&tsc_freq);
981 * if we don't know the clock frequency, measure it.
983 if (machclk_freq == 0) {
985 struct timeval tv_start, tv_end;
986 u_int64_t start, end, diff;
989 microtime(&tv_start);
990 start = read_machclk();
991 timo = hz; /* 1 sec */
992 (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
994 end = read_machclk();
995 diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
996 + tv_end.tv_usec - tv_start.tv_usec;
998 machclk_freq = (u_int)((end - start) * 1000000 / diff);
1001 machclk_per_tick = machclk_freq / hz;
1004 printf("altq: CPU clock: %uHz\n", machclk_freq);
1008 #if defined(__OpenBSD__) && defined(__i386__)
1009 static __inline u_int64_t
1013 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
1016 #endif /* __OpenBSD__ && __i386__ */
1023 if (machclk_usepcc) {
1024 #if defined(__amd64__) || defined(__i386__)
1027 panic("read_machclk");
1033 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1034 + tv.tv_usec) << MACHCLK_SHIFT);
1039 #ifdef ALTQ3_CLFIER_COMPAT
1042 #define IPPROTO_ESP 50 /* encapsulating security payload */
1045 #define IPPROTO_AH 51 /* authentication header */
1049 * extract flow information from a given packet.
1050 * filt_mask shows flowinfo fields required.
1051 * we assume the ip header is in one mbuf, and addresses and ports are
1052 * in network byte order.
1055 altq_extractflow(m, af, flow, filt_bmask)
1058 struct flowinfo *flow;
1059 u_int32_t filt_bmask;
1064 struct flowinfo_in *fin;
1067 ip = mtod(m, struct ip *);
1072 fin = (struct flowinfo_in *)flow;
1073 fin->fi_len = sizeof(struct flowinfo_in);
1074 fin->fi_family = AF_INET;
1076 fin->fi_proto = ip->ip_p;
1077 fin->fi_tos = ip->ip_tos;
1079 fin->fi_src.s_addr = ip->ip_src.s_addr;
1080 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1082 if (filt_bmask & FIMB4_PORTS)
1083 /* if port info is required, extract port numbers */
1084 extract_ports4(m, ip, fin);
1095 struct flowinfo_in6 *fin6;
1096 struct ip6_hdr *ip6;
1098 ip6 = mtod(m, struct ip6_hdr *);
1099 /* should we check the ip version? */
1101 fin6 = (struct flowinfo_in6 *)flow;
1102 fin6->fi6_len = sizeof(struct flowinfo_in6);
1103 fin6->fi6_family = AF_INET6;
1105 fin6->fi6_proto = ip6->ip6_nxt;
1106 fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1108 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1109 fin6->fi6_src = ip6->ip6_src;
1110 fin6->fi6_dst = ip6->ip6_dst;
1112 if ((filt_bmask & FIMB6_PORTS) ||
1113 ((filt_bmask & FIMB6_PROTO)
1114 && ip6->ip6_nxt > IPPROTO_IPV6))
1116 * if port info is required, or proto is required
1117 * but there are option headers, extract port
1118 * and protocol numbers.
1120 extract_ports6(m, ip6, fin6);
1122 fin6->fi6_sport = 0;
1123 fin6->fi6_dport = 0;
1135 flow->fi_len = sizeof(struct flowinfo);
1136 flow->fi_family = AF_UNSPEC;
1141 * helper routine to extract port numbers
1143 /* structure for ipsec and ipv6 option header template */
1145 u_int8_t opt6_nxt; /* next header */
1146 u_int8_t opt6_hlen; /* header extension length */
1148 u_int32_t ah_spi; /* security parameter index
1149 for authentication header */
1153 * extract port numbers from a ipv4 packet.
1156 extract_ports4(m, ip, fin)
1159 struct flowinfo_in *fin;
1170 ip_off = ntohs(ip->ip_off);
1171 /* if it is a fragment, try cached fragment info */
1172 if (ip_off & IP_OFFMASK) {
1173 ip4f_lookup(ip, fin);
1177 /* locate the mbuf containing the protocol header */
1178 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1179 if (((caddr_t)ip >= m0->m_data) &&
1180 ((caddr_t)ip < m0->m_data + m0->m_len))
1184 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1188 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1194 while (off >= m0->m_len) {
1198 return (0); /* bogus ip_hl! */
1200 if (m0->m_len < off + 4)
1208 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1209 fin->fi_sport = udp->uh_sport;
1210 fin->fi_dport = udp->uh_dport;
1211 fin->fi_proto = proto;
1217 if (fin->fi_gpi == 0){
1220 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1223 fin->fi_proto = proto;
1227 /* get next header and header length */
1230 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1231 proto = opt6->opt6_nxt;
1232 off += 8 + (opt6->opt6_hlen * 4);
1233 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1234 fin->fi_gpi = opt6->ah_spi;
1236 /* goto the next header */
1238 #endif /* ALTQ_IPSEC */
1241 fin->fi_proto = proto;
1245 /* if this is a first fragment, cache it. */
1247 ip4f_cache(ip, fin);
1254 extract_ports6(m, ip6, fin6)
1256 struct ip6_hdr *ip6;
1257 struct flowinfo_in6 *fin6;
1264 fin6->fi6_sport = 0;
1265 fin6->fi6_dport = 0;
1267 /* locate the mbuf containing the protocol header */
1268 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1269 if (((caddr_t)ip6 >= m0->m_data) &&
1270 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1274 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1278 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1280 proto = ip6->ip6_nxt;
1282 while (off >= m0->m_len) {
1288 if (m0->m_len < off + 4)
1296 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1297 fin6->fi6_sport = udp->uh_sport;
1298 fin6->fi6_dport = udp->uh_dport;
1299 fin6->fi6_proto = proto;
1304 if (fin6->fi6_gpi == 0) {
1307 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1308 fin6->fi6_gpi = *gpi;
1310 fin6->fi6_proto = proto;
1314 /* get next header and header length */
1317 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1318 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1319 fin6->fi6_gpi = opt6->ah_spi;
1320 proto = opt6->opt6_nxt;
1321 off += 8 + (opt6->opt6_hlen * 4);
1322 /* goto the next header */
1326 case IPPROTO_HOPOPTS:
1327 case IPPROTO_ROUTING:
1328 case IPPROTO_DSTOPTS: {
1329 /* get next header and header length */
1332 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1333 proto = opt6->opt6_nxt;
1334 off += (opt6->opt6_hlen + 1) * 8;
1335 /* goto the next header */
1339 case IPPROTO_FRAGMENT:
1340 /* ipv6 fragmentations are not supported yet */
1342 fin6->fi6_proto = proto;
1351 * altq common classifier
1354 acc_add_filter(classifier, filter, class, phandle)
1355 struct acc_classifier *classifier;
1356 struct flow_filter *filter;
1360 struct acc_filter *afp, *prev, *tmp;
1364 if (filter->ff_flow.fi_family != AF_INET &&
1365 filter->ff_flow.fi_family != AF_INET6)
1368 if (filter->ff_flow.fi_family != AF_INET)
1372 afp = malloc(sizeof(struct acc_filter),
1373 M_DEVBUF, M_WAITOK);
1376 bzero(afp, sizeof(struct acc_filter));
1378 afp->f_filter = *filter;
1379 afp->f_class = class;
1381 i = ACC_WILDCARD_INDEX;
1382 if (filter->ff_flow.fi_family == AF_INET) {
1383 struct flow_filter *filter4 = &afp->f_filter;
1386 * if address is 0, it's a wildcard. if address mask
1387 * isn't set, use full mask.
1389 if (filter4->ff_flow.fi_dst.s_addr == 0)
1390 filter4->ff_mask.mask_dst.s_addr = 0;
1391 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1392 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1393 if (filter4->ff_flow.fi_src.s_addr == 0)
1394 filter4->ff_mask.mask_src.s_addr = 0;
1395 else if (filter4->ff_mask.mask_src.s_addr == 0)
1396 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1398 /* clear extra bits in addresses */
1399 filter4->ff_flow.fi_dst.s_addr &=
1400 filter4->ff_mask.mask_dst.s_addr;
1401 filter4->ff_flow.fi_src.s_addr &=
1402 filter4->ff_mask.mask_src.s_addr;
1405 * if dst address is a wildcard, use hash-entry
1406 * ACC_WILDCARD_INDEX.
1408 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1409 i = ACC_WILDCARD_INDEX;
1411 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1414 else if (filter->ff_flow.fi_family == AF_INET6) {
1415 struct flow_filter6 *filter6 =
1416 (struct flow_filter6 *)&afp->f_filter;
1417 #ifndef IN6MASK0 /* taken from kame ipv6 */
1418 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1419 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1420 const struct in6_addr in6mask0 = IN6MASK0;
1421 const struct in6_addr in6mask128 = IN6MASK128;
1424 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1425 filter6->ff_mask6.mask6_dst = in6mask0;
1426 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1427 filter6->ff_mask6.mask6_dst = in6mask128;
1428 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1429 filter6->ff_mask6.mask6_src = in6mask0;
1430 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1431 filter6->ff_mask6.mask6_src = in6mask128;
1433 /* clear extra bits in addresses */
1434 for (i = 0; i < 16; i++)
1435 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1436 filter6->ff_mask6.mask6_dst.s6_addr[i];
1437 for (i = 0; i < 16; i++)
1438 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1439 filter6->ff_mask6.mask6_src.s6_addr[i];
1441 if (filter6->ff_flow6.fi6_flowlabel == 0)
1442 i = ACC_WILDCARD_INDEX;
1444 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1448 afp->f_handle = get_filt_handle(classifier, i);
1450 /* update filter bitmask */
1451 afp->f_fbmask = filt2fibmask(filter);
1452 classifier->acc_fbmask |= afp->f_fbmask;
1455 * add this filter to the filter list.
1456 * 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)
1488 LIST_REMOVE(afp, f_chain);
1491 free(afp, M_DEVBUF);
1493 /* todo: update filt_bmask */
1499 * delete filters referencing to the specified class.
1500 * if the all flag is not 0, delete all the filters.
1503 acc_discard_filters(classifier, class, all)
1504 struct acc_classifier *classifier;
1508 struct acc_filter *afp;
1512 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1514 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1515 if (all || afp->f_class == class) {
1516 LIST_REMOVE(afp, f_chain);
1517 free(afp, M_DEVBUF);
1518 /* start again from the head */
1521 } while (afp != NULL);
1526 classifier->acc_fbmask = 0;
1532 acc_classify(clfier, m, af)
1537 struct acc_classifier *classifier;
1538 struct flowinfo flow;
1539 struct acc_filter *afp;
1542 classifier = (struct acc_classifier *)clfier;
1543 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1545 if (flow.fi_family == AF_INET) {
1546 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1548 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1549 /* only tos is used */
1551 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1553 if (apply_tosfilter4(afp->f_fbmask,
1554 &afp->f_filter, fp))
1555 /* filter matched */
1556 return (afp->f_class);
1557 } else if ((classifier->acc_fbmask &
1558 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1560 /* only proto and ports are used */
1562 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1564 if (apply_ppfilter4(afp->f_fbmask,
1565 &afp->f_filter, fp))
1566 /* filter matched */
1567 return (afp->f_class);
1569 /* get the filter hash entry from its dest address */
1570 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1573 * go through this loop twice. first for dst
1574 * hash, second for wildcards.
1576 LIST_FOREACH(afp, &classifier->acc_filters[i],
1578 if (apply_filter4(afp->f_fbmask,
1579 &afp->f_filter, fp))
1580 /* filter matched */
1581 return (afp->f_class);
1584 * check again for filters with a dst addr
1586 * (daddr == 0 || dmask != 0xffffffff).
1588 if (i != ACC_WILDCARD_INDEX)
1589 i = ACC_WILDCARD_INDEX;
1596 else if (flow.fi_family == AF_INET6) {
1597 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1599 /* get the filter hash entry from its flow ID */
1600 if (fp6->fi6_flowlabel != 0)
1601 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1603 /* flowlable can be zero */
1604 i = ACC_WILDCARD_INDEX;
1606 /* go through this loop twice. first for flow hash, second
1609 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1610 if (apply_filter6(afp->f_fbmask,
1611 (struct flow_filter6 *)&afp->f_filter,
1613 /* filter matched */
1614 return (afp->f_class);
1617 * check again for filters with a wildcard.
1619 if (i != ACC_WILDCARD_INDEX)
1620 i = ACC_WILDCARD_INDEX;
1627 /* no filter matched */
1632 apply_filter4(fbmask, filt, pkt)
1634 struct flow_filter *filt;
1635 struct flowinfo_in *pkt;
1637 if (filt->ff_flow.fi_family != AF_INET)
1639 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1641 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1643 if ((fbmask & FIMB4_DADDR) &&
1644 filt->ff_flow.fi_dst.s_addr !=
1645 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1647 if ((fbmask & FIMB4_SADDR) &&
1648 filt->ff_flow.fi_src.s_addr !=
1649 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1651 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1653 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1654 (pkt->fi_tos & filt->ff_mask.mask_tos))
1656 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1663 * filter matching function optimized for a common case that checks
1664 * only protocol and port numbers
1667 apply_ppfilter4(fbmask, filt, pkt)
1669 struct flow_filter *filt;
1670 struct flowinfo_in *pkt;
1672 if (filt->ff_flow.fi_family != AF_INET)
1674 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1676 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1678 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1685 * filter matching function only for tos field.
1688 apply_tosfilter4(fbmask, filt, pkt)
1690 struct flow_filter *filt;
1691 struct flowinfo_in *pkt;
1693 if (filt->ff_flow.fi_family != AF_INET)
1695 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1696 (pkt->fi_tos & filt->ff_mask.mask_tos))
1704 apply_filter6(fbmask, filt, pkt)
1706 struct flow_filter6 *filt;
1707 struct flowinfo_in6 *pkt;
1711 if (filt->ff_flow6.fi6_family != AF_INET6)
1713 if ((fbmask & FIMB6_FLABEL) &&
1714 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1716 if ((fbmask & FIMB6_PROTO) &&
1717 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1719 if ((fbmask & FIMB6_SPORT) &&
1720 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1722 if ((fbmask & FIMB6_DPORT) &&
1723 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1725 if (fbmask & FIMB6_SADDR) {
1726 for (i = 0; i < 4; i++)
1727 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1728 (pkt->fi6_src.s6_addr32[i] &
1729 filt->ff_mask6.mask6_src.s6_addr32[i]))
1732 if (fbmask & FIMB6_DADDR) {
1733 for (i = 0; i < 4; i++)
1734 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1735 (pkt->fi6_dst.s6_addr32[i] &
1736 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1739 if ((fbmask & FIMB6_TCLASS) &&
1740 filt->ff_flow6.fi6_tclass !=
1741 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1743 if ((fbmask & FIMB6_GPI) &&
1744 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1753 * bit 20-28: index to the filter hash table
1754 * bit 0-19: unique id in the hash bucket.
1757 get_filt_handle(classifier, i)
1758 struct acc_classifier *classifier;
1761 static u_long handle_number = 1;
1763 struct acc_filter *afp;
1766 handle = handle_number++ & 0x000fffff;
1768 if (LIST_EMPTY(&classifier->acc_filters[i]))
1771 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1772 if ((afp->f_handle & 0x000fffff) == handle)
1776 /* this handle is already used, try again */
1779 return ((i << 20) | handle);
1782 /* convert filter handle to filter pointer */
1783 static struct acc_filter *
1784 filth_to_filtp(classifier, handle)
1785 struct acc_classifier *classifier;
1788 struct acc_filter *afp;
1791 i = ACC_GET_HINDEX(handle);
1793 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1794 if (afp->f_handle == handle)
1800 /* create flowinfo bitmask */
1803 struct flow_filter *filt;
1807 struct flow_filter6 *filt6;
1810 switch (filt->ff_flow.fi_family) {
1812 if (filt->ff_flow.fi_proto != 0)
1813 mask |= FIMB4_PROTO;
1814 if (filt->ff_flow.fi_tos != 0)
1816 if (filt->ff_flow.fi_dst.s_addr != 0)
1817 mask |= FIMB4_DADDR;
1818 if (filt->ff_flow.fi_src.s_addr != 0)
1819 mask |= FIMB4_SADDR;
1820 if (filt->ff_flow.fi_sport != 0)
1821 mask |= FIMB4_SPORT;
1822 if (filt->ff_flow.fi_dport != 0)
1823 mask |= FIMB4_DPORT;
1824 if (filt->ff_flow.fi_gpi != 0)
1829 filt6 = (struct flow_filter6 *)filt;
1831 if (filt6->ff_flow6.fi6_proto != 0)
1832 mask |= FIMB6_PROTO;
1833 if (filt6->ff_flow6.fi6_tclass != 0)
1834 mask |= FIMB6_TCLASS;
1835 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1836 mask |= FIMB6_DADDR;
1837 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1838 mask |= FIMB6_SADDR;
1839 if (filt6->ff_flow6.fi6_sport != 0)
1840 mask |= FIMB6_SPORT;
1841 if (filt6->ff_flow6.fi6_dport != 0)
1842 mask |= FIMB6_DPORT;
1843 if (filt6->ff_flow6.fi6_gpi != 0)
1845 if (filt6->ff_flow6.fi6_flowlabel != 0)
1846 mask |= FIMB6_FLABEL;
1855 * helper functions to handle IPv4 fragments.
1856 * currently only in-sequence fragments are handled.
1857 * - fragment info is cached in a LRU list.
1858 * - when a first fragment is found, cache its flow info.
1859 * - when a non-first fragment is found, lookup the cache.
1863 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1866 struct flowinfo_in ip4f_info;
1869 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1871 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1877 struct flowinfo_in *fin;
1879 struct ip4_frag *fp;
1881 if (TAILQ_EMPTY(&ip4f_list)) {
1882 /* first time call, allocate fragment cache entries. */
1883 if (ip4f_init() < 0)
1884 /* allocation failed! */
1889 fp->ip4f_id = ip->ip_id;
1890 fp->ip4f_info.fi_proto = ip->ip_p;
1891 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1892 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1894 /* save port numbers */
1895 fp->ip4f_info.fi_sport = fin->fi_sport;
1896 fp->ip4f_info.fi_dport = fin->fi_dport;
1897 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1901 ip4f_lookup(ip, fin)
1903 struct flowinfo_in *fin;
1905 struct ip4_frag *fp;
1907 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1908 fp = TAILQ_NEXT(fp, ip4f_chain))
1909 if (ip->ip_id == fp->ip4f_id &&
1910 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1911 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1912 ip->ip_p == fp->ip4f_info.fi_proto) {
1914 /* found the matching entry */
1915 fin->fi_sport = fp->ip4f_info.fi_sport;
1916 fin->fi_dport = fp->ip4f_info.fi_dport;
1917 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1919 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1920 /* this is the last fragment,
1921 release the entry. */
1927 /* no matching entry found */
1934 struct ip4_frag *fp;
1937 TAILQ_INIT(&ip4f_list);
1938 for (i=0; i<IP4F_TABSIZE; i++) {
1939 fp = malloc(sizeof(struct ip4_frag),
1940 M_DEVBUF, M_NOWAIT);
1942 printf("ip4f_init: can't alloc %dth entry!\n", i);
1948 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1953 static struct ip4_frag *
1956 struct ip4_frag *fp;
1958 /* reclaim an entry at the tail, put it at the head */
1959 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1960 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1962 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1968 struct ip4_frag *fp;
1970 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1972 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1975 #endif /* ALTQ3_CLFIER_COMPAT */