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_link)) {
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");
1030 struct timeval tv, boottime;
1033 getboottime(&boottime);
1034 val = (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
1035 + tv.tv_usec) << MACHCLK_SHIFT);
1040 #ifdef ALTQ3_CLFIER_COMPAT
1043 #define IPPROTO_ESP 50 /* encapsulating security payload */
1046 #define IPPROTO_AH 51 /* authentication header */
1050 * extract flow information from a given packet.
1051 * filt_mask shows flowinfo fields required.
1052 * we assume the ip header is in one mbuf, and addresses and ports are
1053 * in network byte order.
1056 altq_extractflow(m, af, flow, filt_bmask)
1059 struct flowinfo *flow;
1060 u_int32_t filt_bmask;
1065 struct flowinfo_in *fin;
1068 ip = mtod(m, struct ip *);
1073 fin = (struct flowinfo_in *)flow;
1074 fin->fi_len = sizeof(struct flowinfo_in);
1075 fin->fi_family = AF_INET;
1077 fin->fi_proto = ip->ip_p;
1078 fin->fi_tos = ip->ip_tos;
1080 fin->fi_src.s_addr = ip->ip_src.s_addr;
1081 fin->fi_dst.s_addr = ip->ip_dst.s_addr;
1083 if (filt_bmask & FIMB4_PORTS)
1084 /* if port info is required, extract port numbers */
1085 extract_ports4(m, ip, fin);
1096 struct flowinfo_in6 *fin6;
1097 struct ip6_hdr *ip6;
1099 ip6 = mtod(m, struct ip6_hdr *);
1100 /* should we check the ip version? */
1102 fin6 = (struct flowinfo_in6 *)flow;
1103 fin6->fi6_len = sizeof(struct flowinfo_in6);
1104 fin6->fi6_family = AF_INET6;
1106 fin6->fi6_proto = ip6->ip6_nxt;
1107 fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1109 fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
1110 fin6->fi6_src = ip6->ip6_src;
1111 fin6->fi6_dst = ip6->ip6_dst;
1113 if ((filt_bmask & FIMB6_PORTS) ||
1114 ((filt_bmask & FIMB6_PROTO)
1115 && ip6->ip6_nxt > IPPROTO_IPV6))
1117 * if port info is required, or proto is required
1118 * but there are option headers, extract port
1119 * and protocol numbers.
1121 extract_ports6(m, ip6, fin6);
1123 fin6->fi6_sport = 0;
1124 fin6->fi6_dport = 0;
1136 flow->fi_len = sizeof(struct flowinfo);
1137 flow->fi_family = AF_UNSPEC;
1142 * helper routine to extract port numbers
1144 /* structure for ipsec and ipv6 option header template */
1146 u_int8_t opt6_nxt; /* next header */
1147 u_int8_t opt6_hlen; /* header extension length */
1149 u_int32_t ah_spi; /* security parameter index
1150 for authentication header */
1154 * extract port numbers from a ipv4 packet.
1157 extract_ports4(m, ip, fin)
1160 struct flowinfo_in *fin;
1171 ip_off = ntohs(ip->ip_off);
1172 /* if it is a fragment, try cached fragment info */
1173 if (ip_off & IP_OFFMASK) {
1174 ip4f_lookup(ip, fin);
1178 /* locate the mbuf containing the protocol header */
1179 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1180 if (((caddr_t)ip >= m0->m_data) &&
1181 ((caddr_t)ip < m0->m_data + m0->m_len))
1185 printf("extract_ports4: can't locate header! ip=%p\n", ip);
1189 off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
1195 while (off >= m0->m_len) {
1199 return (0); /* bogus ip_hl! */
1201 if (m0->m_len < off + 4)
1209 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1210 fin->fi_sport = udp->uh_sport;
1211 fin->fi_dport = udp->uh_dport;
1212 fin->fi_proto = proto;
1218 if (fin->fi_gpi == 0){
1221 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1224 fin->fi_proto = proto;
1228 /* get next header and header length */
1231 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1232 proto = opt6->opt6_nxt;
1233 off += 8 + (opt6->opt6_hlen * 4);
1234 if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
1235 fin->fi_gpi = opt6->ah_spi;
1237 /* goto the next header */
1239 #endif /* ALTQ_IPSEC */
1242 fin->fi_proto = proto;
1246 /* if this is a first fragment, cache it. */
1248 ip4f_cache(ip, fin);
1255 extract_ports6(m, ip6, fin6)
1257 struct ip6_hdr *ip6;
1258 struct flowinfo_in6 *fin6;
1265 fin6->fi6_sport = 0;
1266 fin6->fi6_dport = 0;
1268 /* locate the mbuf containing the protocol header */
1269 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1270 if (((caddr_t)ip6 >= m0->m_data) &&
1271 ((caddr_t)ip6 < m0->m_data + m0->m_len))
1275 printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
1279 off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
1281 proto = ip6->ip6_nxt;
1283 while (off >= m0->m_len) {
1289 if (m0->m_len < off + 4)
1297 udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
1298 fin6->fi6_sport = udp->uh_sport;
1299 fin6->fi6_dport = udp->uh_dport;
1300 fin6->fi6_proto = proto;
1305 if (fin6->fi6_gpi == 0) {
1308 gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
1309 fin6->fi6_gpi = *gpi;
1311 fin6->fi6_proto = proto;
1315 /* get next header and header length */
1318 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1319 if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
1320 fin6->fi6_gpi = opt6->ah_spi;
1321 proto = opt6->opt6_nxt;
1322 off += 8 + (opt6->opt6_hlen * 4);
1323 /* goto the next header */
1327 case IPPROTO_HOPOPTS:
1328 case IPPROTO_ROUTING:
1329 case IPPROTO_DSTOPTS: {
1330 /* get next header and header length */
1333 opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
1334 proto = opt6->opt6_nxt;
1335 off += (opt6->opt6_hlen + 1) * 8;
1336 /* goto the next header */
1340 case IPPROTO_FRAGMENT:
1341 /* ipv6 fragmentations are not supported yet */
1343 fin6->fi6_proto = proto;
1352 * altq common classifier
1355 acc_add_filter(classifier, filter, class, phandle)
1356 struct acc_classifier *classifier;
1357 struct flow_filter *filter;
1361 struct acc_filter *afp, *prev, *tmp;
1365 if (filter->ff_flow.fi_family != AF_INET &&
1366 filter->ff_flow.fi_family != AF_INET6)
1369 if (filter->ff_flow.fi_family != AF_INET)
1373 afp = malloc(sizeof(struct acc_filter),
1374 M_DEVBUF, M_WAITOK);
1377 bzero(afp, sizeof(struct acc_filter));
1379 afp->f_filter = *filter;
1380 afp->f_class = class;
1382 i = ACC_WILDCARD_INDEX;
1383 if (filter->ff_flow.fi_family == AF_INET) {
1384 struct flow_filter *filter4 = &afp->f_filter;
1387 * if address is 0, it's a wildcard. if address mask
1388 * isn't set, use full mask.
1390 if (filter4->ff_flow.fi_dst.s_addr == 0)
1391 filter4->ff_mask.mask_dst.s_addr = 0;
1392 else if (filter4->ff_mask.mask_dst.s_addr == 0)
1393 filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
1394 if (filter4->ff_flow.fi_src.s_addr == 0)
1395 filter4->ff_mask.mask_src.s_addr = 0;
1396 else if (filter4->ff_mask.mask_src.s_addr == 0)
1397 filter4->ff_mask.mask_src.s_addr = 0xffffffff;
1399 /* clear extra bits in addresses */
1400 filter4->ff_flow.fi_dst.s_addr &=
1401 filter4->ff_mask.mask_dst.s_addr;
1402 filter4->ff_flow.fi_src.s_addr &=
1403 filter4->ff_mask.mask_src.s_addr;
1406 * if dst address is a wildcard, use hash-entry
1407 * ACC_WILDCARD_INDEX.
1409 if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
1410 i = ACC_WILDCARD_INDEX;
1412 i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
1415 else if (filter->ff_flow.fi_family == AF_INET6) {
1416 struct flow_filter6 *filter6 =
1417 (struct flow_filter6 *)&afp->f_filter;
1418 #ifndef IN6MASK0 /* taken from kame ipv6 */
1419 #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
1420 #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
1421 const struct in6_addr in6mask0 = IN6MASK0;
1422 const struct in6_addr in6mask128 = IN6MASK128;
1425 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
1426 filter6->ff_mask6.mask6_dst = in6mask0;
1427 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
1428 filter6->ff_mask6.mask6_dst = in6mask128;
1429 if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
1430 filter6->ff_mask6.mask6_src = in6mask0;
1431 else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
1432 filter6->ff_mask6.mask6_src = in6mask128;
1434 /* clear extra bits in addresses */
1435 for (i = 0; i < 16; i++)
1436 filter6->ff_flow6.fi6_dst.s6_addr[i] &=
1437 filter6->ff_mask6.mask6_dst.s6_addr[i];
1438 for (i = 0; i < 16; i++)
1439 filter6->ff_flow6.fi6_src.s6_addr[i] &=
1440 filter6->ff_mask6.mask6_src.s6_addr[i];
1442 if (filter6->ff_flow6.fi6_flowlabel == 0)
1443 i = ACC_WILDCARD_INDEX;
1445 i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
1449 afp->f_handle = get_filt_handle(classifier, i);
1451 /* update filter bitmask */
1452 afp->f_fbmask = filt2fibmask(filter);
1453 classifier->acc_fbmask |= afp->f_fbmask;
1456 * add this filter to the filter list.
1457 * filters are ordered from the highest rule number.
1461 LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
1462 if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
1468 LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
1470 LIST_INSERT_AFTER(prev, afp, f_chain);
1473 *phandle = afp->f_handle;
1478 acc_delete_filter(classifier, handle)
1479 struct acc_classifier *classifier;
1482 struct acc_filter *afp;
1485 if ((afp = filth_to_filtp(classifier, handle)) == NULL)
1489 LIST_REMOVE(afp, f_chain);
1492 free(afp, M_DEVBUF);
1494 /* todo: update filt_bmask */
1500 * delete filters referencing to the specified class.
1501 * if the all flag is not 0, delete all the filters.
1504 acc_discard_filters(classifier, class, all)
1505 struct acc_classifier *classifier;
1509 struct acc_filter *afp;
1513 for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
1515 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1516 if (all || afp->f_class == class) {
1517 LIST_REMOVE(afp, f_chain);
1518 free(afp, M_DEVBUF);
1519 /* start again from the head */
1522 } while (afp != NULL);
1527 classifier->acc_fbmask = 0;
1533 acc_classify(clfier, m, af)
1538 struct acc_classifier *classifier;
1539 struct flowinfo flow;
1540 struct acc_filter *afp;
1543 classifier = (struct acc_classifier *)clfier;
1544 altq_extractflow(m, af, &flow, classifier->acc_fbmask);
1546 if (flow.fi_family == AF_INET) {
1547 struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
1549 if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
1550 /* only tos is used */
1552 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1554 if (apply_tosfilter4(afp->f_fbmask,
1555 &afp->f_filter, fp))
1556 /* filter matched */
1557 return (afp->f_class);
1558 } else if ((classifier->acc_fbmask &
1559 (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
1561 /* only proto and ports are used */
1563 &classifier->acc_filters[ACC_WILDCARD_INDEX],
1565 if (apply_ppfilter4(afp->f_fbmask,
1566 &afp->f_filter, fp))
1567 /* filter matched */
1568 return (afp->f_class);
1570 /* get the filter hash entry from its dest address */
1571 i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
1574 * go through this loop twice. first for dst
1575 * hash, second for wildcards.
1577 LIST_FOREACH(afp, &classifier->acc_filters[i],
1579 if (apply_filter4(afp->f_fbmask,
1580 &afp->f_filter, fp))
1581 /* filter matched */
1582 return (afp->f_class);
1585 * check again for filters with a dst addr
1587 * (daddr == 0 || dmask != 0xffffffff).
1589 if (i != ACC_WILDCARD_INDEX)
1590 i = ACC_WILDCARD_INDEX;
1597 else if (flow.fi_family == AF_INET6) {
1598 struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
1600 /* get the filter hash entry from its flow ID */
1601 if (fp6->fi6_flowlabel != 0)
1602 i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
1604 /* flowlable can be zero */
1605 i = ACC_WILDCARD_INDEX;
1607 /* go through this loop twice. first for flow hash, second
1610 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1611 if (apply_filter6(afp->f_fbmask,
1612 (struct flow_filter6 *)&afp->f_filter,
1614 /* filter matched */
1615 return (afp->f_class);
1618 * check again for filters with a wildcard.
1620 if (i != ACC_WILDCARD_INDEX)
1621 i = ACC_WILDCARD_INDEX;
1628 /* no filter matched */
1633 apply_filter4(fbmask, filt, pkt)
1635 struct flow_filter *filt;
1636 struct flowinfo_in *pkt;
1638 if (filt->ff_flow.fi_family != AF_INET)
1640 if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1642 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1644 if ((fbmask & FIMB4_DADDR) &&
1645 filt->ff_flow.fi_dst.s_addr !=
1646 (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1648 if ((fbmask & FIMB4_SADDR) &&
1649 filt->ff_flow.fi_src.s_addr !=
1650 (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1652 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1654 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1655 (pkt->fi_tos & filt->ff_mask.mask_tos))
1657 if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1664 * filter matching function optimized for a common case that checks
1665 * only protocol and port numbers
1668 apply_ppfilter4(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_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1677 if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1679 if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1686 * filter matching function only for tos field.
1689 apply_tosfilter4(fbmask, filt, pkt)
1691 struct flow_filter *filt;
1692 struct flowinfo_in *pkt;
1694 if (filt->ff_flow.fi_family != AF_INET)
1696 if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1697 (pkt->fi_tos & filt->ff_mask.mask_tos))
1705 apply_filter6(fbmask, filt, pkt)
1707 struct flow_filter6 *filt;
1708 struct flowinfo_in6 *pkt;
1712 if (filt->ff_flow6.fi6_family != AF_INET6)
1714 if ((fbmask & FIMB6_FLABEL) &&
1715 filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1717 if ((fbmask & FIMB6_PROTO) &&
1718 filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1720 if ((fbmask & FIMB6_SPORT) &&
1721 filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1723 if ((fbmask & FIMB6_DPORT) &&
1724 filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1726 if (fbmask & FIMB6_SADDR) {
1727 for (i = 0; i < 4; i++)
1728 if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1729 (pkt->fi6_src.s6_addr32[i] &
1730 filt->ff_mask6.mask6_src.s6_addr32[i]))
1733 if (fbmask & FIMB6_DADDR) {
1734 for (i = 0; i < 4; i++)
1735 if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1736 (pkt->fi6_dst.s6_addr32[i] &
1737 filt->ff_mask6.mask6_dst.s6_addr32[i]))
1740 if ((fbmask & FIMB6_TCLASS) &&
1741 filt->ff_flow6.fi6_tclass !=
1742 (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1744 if ((fbmask & FIMB6_GPI) &&
1745 filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1754 * bit 20-28: index to the filter hash table
1755 * bit 0-19: unique id in the hash bucket.
1758 get_filt_handle(classifier, i)
1759 struct acc_classifier *classifier;
1762 static u_long handle_number = 1;
1764 struct acc_filter *afp;
1767 handle = handle_number++ & 0x000fffff;
1769 if (LIST_EMPTY(&classifier->acc_filters[i]))
1772 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1773 if ((afp->f_handle & 0x000fffff) == handle)
1777 /* this handle is already used, try again */
1780 return ((i << 20) | handle);
1783 /* convert filter handle to filter pointer */
1784 static struct acc_filter *
1785 filth_to_filtp(classifier, handle)
1786 struct acc_classifier *classifier;
1789 struct acc_filter *afp;
1792 i = ACC_GET_HINDEX(handle);
1794 LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1795 if (afp->f_handle == handle)
1801 /* create flowinfo bitmask */
1804 struct flow_filter *filt;
1808 struct flow_filter6 *filt6;
1811 switch (filt->ff_flow.fi_family) {
1813 if (filt->ff_flow.fi_proto != 0)
1814 mask |= FIMB4_PROTO;
1815 if (filt->ff_flow.fi_tos != 0)
1817 if (filt->ff_flow.fi_dst.s_addr != 0)
1818 mask |= FIMB4_DADDR;
1819 if (filt->ff_flow.fi_src.s_addr != 0)
1820 mask |= FIMB4_SADDR;
1821 if (filt->ff_flow.fi_sport != 0)
1822 mask |= FIMB4_SPORT;
1823 if (filt->ff_flow.fi_dport != 0)
1824 mask |= FIMB4_DPORT;
1825 if (filt->ff_flow.fi_gpi != 0)
1830 filt6 = (struct flow_filter6 *)filt;
1832 if (filt6->ff_flow6.fi6_proto != 0)
1833 mask |= FIMB6_PROTO;
1834 if (filt6->ff_flow6.fi6_tclass != 0)
1835 mask |= FIMB6_TCLASS;
1836 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1837 mask |= FIMB6_DADDR;
1838 if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1839 mask |= FIMB6_SADDR;
1840 if (filt6->ff_flow6.fi6_sport != 0)
1841 mask |= FIMB6_SPORT;
1842 if (filt6->ff_flow6.fi6_dport != 0)
1843 mask |= FIMB6_DPORT;
1844 if (filt6->ff_flow6.fi6_gpi != 0)
1846 if (filt6->ff_flow6.fi6_flowlabel != 0)
1847 mask |= FIMB6_FLABEL;
1856 * helper functions to handle IPv4 fragments.
1857 * currently only in-sequence fragments are handled.
1858 * - fragment info is cached in a LRU list.
1859 * - when a first fragment is found, cache its flow info.
1860 * - when a non-first fragment is found, lookup the cache.
1864 TAILQ_ENTRY(ip4_frag) ip4f_chain;
1867 struct flowinfo_in ip4f_info;
1870 static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1872 #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1878 struct flowinfo_in *fin;
1880 struct ip4_frag *fp;
1882 if (TAILQ_EMPTY(&ip4f_list)) {
1883 /* first time call, allocate fragment cache entries. */
1884 if (ip4f_init() < 0)
1885 /* allocation failed! */
1890 fp->ip4f_id = ip->ip_id;
1891 fp->ip4f_info.fi_proto = ip->ip_p;
1892 fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1893 fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1895 /* save port numbers */
1896 fp->ip4f_info.fi_sport = fin->fi_sport;
1897 fp->ip4f_info.fi_dport = fin->fi_dport;
1898 fp->ip4f_info.fi_gpi = fin->fi_gpi;
1902 ip4f_lookup(ip, fin)
1904 struct flowinfo_in *fin;
1906 struct ip4_frag *fp;
1908 for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1909 fp = TAILQ_NEXT(fp, ip4f_chain))
1910 if (ip->ip_id == fp->ip4f_id &&
1911 ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1912 ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1913 ip->ip_p == fp->ip4f_info.fi_proto) {
1915 /* found the matching entry */
1916 fin->fi_sport = fp->ip4f_info.fi_sport;
1917 fin->fi_dport = fp->ip4f_info.fi_dport;
1918 fin->fi_gpi = fp->ip4f_info.fi_gpi;
1920 if ((ntohs(ip->ip_off) & IP_MF) == 0)
1921 /* this is the last fragment,
1922 release the entry. */
1928 /* no matching entry found */
1935 struct ip4_frag *fp;
1938 TAILQ_INIT(&ip4f_list);
1939 for (i=0; i<IP4F_TABSIZE; i++) {
1940 fp = malloc(sizeof(struct ip4_frag),
1941 M_DEVBUF, M_NOWAIT);
1943 printf("ip4f_init: can't alloc %dth entry!\n", i);
1949 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1954 static struct ip4_frag *
1957 struct ip4_frag *fp;
1959 /* reclaim an entry at the tail, put it at the head */
1960 fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1961 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1963 TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1969 struct ip4_frag *fp;
1971 TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1973 TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1976 #endif /* ALTQ3_CLFIER_COMPAT */