altq_subr.c revision 1.15 1 1.15 christos /* $NetBSD: altq_subr.c,v 1.15 2006/10/12 01:30:42 christos Exp $ */
2 1.8 itojun /* $KAME: altq_subr.c,v 1.11 2002/01/11 08:11:49 kjc Exp $ */
3 1.1 thorpej
4 1.1 thorpej /*
5 1.8 itojun * Copyright (C) 1997-2002
6 1.1 thorpej * Sony Computer Science Laboratories Inc. All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * Redistribution and use in source and binary forms, with or without
9 1.1 thorpej * modification, are permitted provided that the following conditions
10 1.1 thorpej * are met:
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.1 thorpej * documentation and/or other materials provided with the distribution.
16 1.1 thorpej *
17 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 thorpej * SUCH DAMAGE.
28 1.1 thorpej */
29 1.7 lukem
30 1.7 lukem #include <sys/cdefs.h>
31 1.15 christos __KERNEL_RCSID(0, "$NetBSD: altq_subr.c,v 1.15 2006/10/12 01:30:42 christos Exp $");
32 1.1 thorpej
33 1.1 thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
34 1.1 thorpej #include "opt_altq.h"
35 1.1 thorpej #if (__FreeBSD__ != 2)
36 1.1 thorpej #include "opt_inet.h"
37 1.1 thorpej #ifdef __FreeBSD__
38 1.1 thorpej #include "opt_inet6.h"
39 1.1 thorpej #endif
40 1.1 thorpej #endif
41 1.1 thorpej #endif /* __FreeBSD__ || __NetBSD__ */
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.1 thorpej #include <sys/malloc.h>
45 1.1 thorpej #include <sys/mbuf.h>
46 1.1 thorpej #include <sys/systm.h>
47 1.1 thorpej #include <sys/proc.h>
48 1.1 thorpej #include <sys/socket.h>
49 1.1 thorpej #include <sys/socketvar.h>
50 1.1 thorpej #include <sys/kernel.h>
51 1.1 thorpej #include <sys/errno.h>
52 1.1 thorpej #include <sys/syslog.h>
53 1.1 thorpej #include <sys/sysctl.h>
54 1.1 thorpej #include <sys/queue.h>
55 1.1 thorpej
56 1.1 thorpej #include <net/if.h>
57 1.1 thorpej #include <net/if_dl.h>
58 1.1 thorpej #include <net/if_types.h>
59 1.1 thorpej
60 1.1 thorpej #include <netinet/in.h>
61 1.1 thorpej #include <netinet/in_systm.h>
62 1.1 thorpej #include <netinet/ip.h>
63 1.1 thorpej #ifdef INET6
64 1.1 thorpej #include <netinet/ip6.h>
65 1.1 thorpej #endif
66 1.1 thorpej #include <netinet/tcp.h>
67 1.1 thorpej #include <netinet/udp.h>
68 1.1 thorpej
69 1.1 thorpej #include <altq/altq.h>
70 1.1 thorpej #include <altq/altq_conf.h>
71 1.1 thorpej
72 1.8 itojun /* machine dependent clock related includes */
73 1.1 thorpej #ifdef __FreeBSD__
74 1.1 thorpej #include "opt_cpu.h" /* for FreeBSD-2.2.8 to get i586_ctr_freq */
75 1.1 thorpej #include <machine/clock.h>
76 1.1 thorpej #endif
77 1.8 itojun #if defined(__i386__)
78 1.8 itojun #include <machine/specialreg.h> /* for CPUID_TSC */
79 1.8 itojun #ifdef __FreeBSD__
80 1.8 itojun #include <machine/md_var.h> /* for cpu_feature */
81 1.8 itojun #elif defined(__NetBSD__) || defined(__OpenBSD__)
82 1.8 itojun #include <machine/cpu.h> /* for cpu_feature */
83 1.8 itojun #endif
84 1.8 itojun #endif /* __i386__ */
85 1.1 thorpej
86 1.1 thorpej /*
87 1.1 thorpej * internal function prototypes
88 1.1 thorpej */
89 1.1 thorpej static void tbr_timeout __P((void *));
90 1.1 thorpej static int extract_ports4 __P((struct mbuf *, struct ip *,
91 1.1 thorpej struct flowinfo_in *));
92 1.1 thorpej #ifdef INET6
93 1.1 thorpej static int extract_ports6 __P((struct mbuf *, struct ip6_hdr *,
94 1.1 thorpej struct flowinfo_in6 *));
95 1.1 thorpej #endif
96 1.1 thorpej static int apply_filter4 __P((u_int32_t, struct flow_filter *,
97 1.1 thorpej struct flowinfo_in *));
98 1.1 thorpej static int apply_ppfilter4 __P((u_int32_t, struct flow_filter *,
99 1.1 thorpej struct flowinfo_in *));
100 1.1 thorpej #ifdef INET6
101 1.1 thorpej static int apply_filter6 __P((u_int32_t, struct flow_filter6 *,
102 1.1 thorpej struct flowinfo_in6 *));
103 1.1 thorpej #endif
104 1.1 thorpej static int apply_tosfilter4 __P((u_int32_t, struct flow_filter *,
105 1.1 thorpej struct flowinfo_in *));
106 1.1 thorpej static u_long get_filt_handle __P((struct acc_classifier *, int));
107 1.1 thorpej static struct acc_filter *filth_to_filtp __P((struct acc_classifier *,
108 1.1 thorpej u_long));
109 1.1 thorpej static u_int32_t filt2fibmask __P((struct flow_filter *));
110 1.1 thorpej
111 1.1 thorpej static void ip4f_cache __P((struct ip *, struct flowinfo_in *));
112 1.1 thorpej static int ip4f_lookup __P((struct ip *, struct flowinfo_in *));
113 1.1 thorpej static int ip4f_init __P((void));
114 1.1 thorpej static struct ip4_frag *ip4f_alloc __P((void));
115 1.1 thorpej static void ip4f_free __P((struct ip4_frag *));
116 1.1 thorpej
117 1.1 thorpej int (*altq_input) __P((struct mbuf *, int)) = NULL;
118 1.1 thorpej static int tbr_timer = 0; /* token bucket regulator timer */
119 1.1 thorpej static struct callout tbr_callout = CALLOUT_INITIALIZER;
120 1.1 thorpej
121 1.1 thorpej /*
122 1.1 thorpej * alternate queueing support routines
123 1.1 thorpej */
124 1.1 thorpej
125 1.1 thorpej /* look up the queue state by the interface name and the queuing type. */
126 1.1 thorpej void *
127 1.1 thorpej altq_lookup(name, type)
128 1.1 thorpej char *name;
129 1.1 thorpej int type;
130 1.1 thorpej {
131 1.1 thorpej struct ifnet *ifp;
132 1.1 thorpej
133 1.1 thorpej if ((ifp = ifunit(name)) != NULL) {
134 1.1 thorpej if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
135 1.1 thorpej return (ifp->if_snd.altq_disc);
136 1.1 thorpej }
137 1.1 thorpej
138 1.1 thorpej return NULL;
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej int
142 1.1 thorpej altq_attach(ifq, type, discipline, enqueue, dequeue, request, clfier, classify)
143 1.1 thorpej struct ifaltq *ifq;
144 1.1 thorpej int type;
145 1.1 thorpej void *discipline;
146 1.1 thorpej int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
147 1.1 thorpej struct mbuf *(*dequeue)(struct ifaltq *, int);
148 1.1 thorpej int (*request)(struct ifaltq *, int, void *);
149 1.1 thorpej void *clfier;
150 1.1 thorpej void *(*classify)(void *, struct mbuf *, int);
151 1.1 thorpej {
152 1.1 thorpej if (!ALTQ_IS_READY(ifq))
153 1.1 thorpej return ENXIO;
154 1.1 thorpej if (ALTQ_IS_ENABLED(ifq))
155 1.1 thorpej return EBUSY;
156 1.1 thorpej if (ALTQ_IS_ATTACHED(ifq))
157 1.1 thorpej return EEXIST;
158 1.1 thorpej ifq->altq_type = type;
159 1.1 thorpej ifq->altq_disc = discipline;
160 1.1 thorpej ifq->altq_enqueue = enqueue;
161 1.1 thorpej ifq->altq_dequeue = dequeue;
162 1.1 thorpej ifq->altq_request = request;
163 1.1 thorpej ifq->altq_clfier = clfier;
164 1.1 thorpej ifq->altq_classify = classify;
165 1.1 thorpej ifq->altq_flags &= ALTQF_CANTCHANGE;
166 1.1 thorpej #ifdef ALTQ_KLD
167 1.1 thorpej altq_module_incref(type);
168 1.1 thorpej #endif
169 1.1 thorpej return 0;
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej int
173 1.1 thorpej altq_detach(ifq)
174 1.1 thorpej struct ifaltq *ifq;
175 1.1 thorpej {
176 1.1 thorpej if (!ALTQ_IS_READY(ifq))
177 1.1 thorpej return ENXIO;
178 1.1 thorpej if (ALTQ_IS_ENABLED(ifq))
179 1.1 thorpej return EBUSY;
180 1.1 thorpej if (!ALTQ_IS_ATTACHED(ifq))
181 1.1 thorpej return (0);
182 1.1 thorpej
183 1.1 thorpej #ifdef ALTQ_KLD
184 1.1 thorpej altq_module_declref(ifq->altq_type);
185 1.1 thorpej #endif
186 1.1 thorpej ifq->altq_type = ALTQT_NONE;
187 1.1 thorpej ifq->altq_disc = NULL;
188 1.1 thorpej ifq->altq_enqueue = NULL;
189 1.1 thorpej ifq->altq_dequeue = NULL;
190 1.1 thorpej ifq->altq_request = NULL;
191 1.1 thorpej ifq->altq_clfier = NULL;
192 1.1 thorpej ifq->altq_classify = NULL;
193 1.1 thorpej ifq->altq_flags &= ALTQF_CANTCHANGE;
194 1.1 thorpej return 0;
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej int
198 1.1 thorpej altq_enable(ifq)
199 1.1 thorpej struct ifaltq *ifq;
200 1.1 thorpej {
201 1.1 thorpej int s;
202 1.12 perry
203 1.1 thorpej if (!ALTQ_IS_READY(ifq))
204 1.1 thorpej return ENXIO;
205 1.1 thorpej if (ALTQ_IS_ENABLED(ifq))
206 1.1 thorpej return 0;
207 1.1 thorpej
208 1.5 thorpej s = splnet();
209 1.1 thorpej IFQ_PURGE(ifq);
210 1.1 thorpej ASSERT(ifq->ifq_len == 0);
211 1.1 thorpej ifq->altq_flags |= ALTQF_ENABLED;
212 1.1 thorpej if (ifq->altq_clfier != NULL)
213 1.1 thorpej ifq->altq_flags |= ALTQF_CLASSIFY;
214 1.1 thorpej splx(s);
215 1.1 thorpej
216 1.1 thorpej return 0;
217 1.1 thorpej }
218 1.1 thorpej
219 1.1 thorpej int
220 1.1 thorpej altq_disable(ifq)
221 1.1 thorpej struct ifaltq *ifq;
222 1.1 thorpej {
223 1.1 thorpej int s;
224 1.12 perry
225 1.1 thorpej if (!ALTQ_IS_ENABLED(ifq))
226 1.1 thorpej return 0;
227 1.1 thorpej
228 1.5 thorpej s = splnet();
229 1.1 thorpej IFQ_PURGE(ifq);
230 1.1 thorpej ASSERT(ifq->ifq_len == 0);
231 1.1 thorpej ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
232 1.1 thorpej splx(s);
233 1.1 thorpej return 0;
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej void
237 1.1 thorpej altq_assert(file, line, failedexpr)
238 1.1 thorpej const char *file, *failedexpr;
239 1.1 thorpej int line;
240 1.1 thorpej {
241 1.1 thorpej (void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
242 1.1 thorpej failedexpr, file, line);
243 1.1 thorpej panic("altq assertion");
244 1.1 thorpej /* NOTREACHED */
245 1.1 thorpej }
246 1.1 thorpej
247 1.1 thorpej /*
248 1.1 thorpej * internal representation of token bucket parameters
249 1.1 thorpej * rate: byte_per_unittime << 32
250 1.1 thorpej * (((bits_per_sec) / 8) << 32) / machclk_freq
251 1.1 thorpej * depth: byte << 32
252 1.1 thorpej *
253 1.1 thorpej */
254 1.1 thorpej #define TBR_SHIFT 32
255 1.1 thorpej #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
256 1.1 thorpej #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
257 1.1 thorpej
258 1.1 thorpej struct mbuf *
259 1.1 thorpej tbr_dequeue(ifq, op)
260 1.1 thorpej struct ifaltq *ifq;
261 1.1 thorpej int op;
262 1.1 thorpej {
263 1.1 thorpej struct tb_regulator *tbr;
264 1.1 thorpej struct mbuf *m;
265 1.1 thorpej int64_t interval;
266 1.1 thorpej u_int64_t now;
267 1.1 thorpej
268 1.1 thorpej tbr = ifq->altq_tbr;
269 1.1 thorpej if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
270 1.1 thorpej /* if this is a remove after poll, bypass tbr check */
271 1.1 thorpej } else {
272 1.1 thorpej /* update token only when it is negative */
273 1.1 thorpej if (tbr->tbr_token <= 0) {
274 1.1 thorpej now = read_machclk();
275 1.1 thorpej interval = now - tbr->tbr_last;
276 1.1 thorpej if (interval >= tbr->tbr_filluptime)
277 1.1 thorpej tbr->tbr_token = tbr->tbr_depth;
278 1.1 thorpej else {
279 1.1 thorpej tbr->tbr_token += interval * tbr->tbr_rate;
280 1.1 thorpej if (tbr->tbr_token > tbr->tbr_depth)
281 1.1 thorpej tbr->tbr_token = tbr->tbr_depth;
282 1.1 thorpej }
283 1.1 thorpej tbr->tbr_last = now;
284 1.1 thorpej }
285 1.1 thorpej /* if token is still negative, don't allow dequeue */
286 1.1 thorpej if (tbr->tbr_token <= 0)
287 1.1 thorpej return (NULL);
288 1.1 thorpej }
289 1.1 thorpej
290 1.1 thorpej if (ALTQ_IS_ENABLED(ifq))
291 1.1 thorpej m = (*ifq->altq_dequeue)(ifq, op);
292 1.1 thorpej else {
293 1.1 thorpej if (op == ALTDQ_POLL)
294 1.1 thorpej IF_POLL(ifq, m);
295 1.1 thorpej else
296 1.1 thorpej IF_DEQUEUE(ifq, m);
297 1.1 thorpej }
298 1.1 thorpej
299 1.1 thorpej if (m != NULL && op == ALTDQ_REMOVE)
300 1.1 thorpej tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
301 1.1 thorpej tbr->tbr_lastop = op;
302 1.1 thorpej return (m);
303 1.1 thorpej }
304 1.1 thorpej
305 1.1 thorpej /*
306 1.1 thorpej * set a token bucket regulator.
307 1.1 thorpej * if the specified rate is zero, the token bucket regulator is deleted.
308 1.1 thorpej */
309 1.1 thorpej int
310 1.1 thorpej tbr_set(ifq, profile)
311 1.1 thorpej struct ifaltq *ifq;
312 1.1 thorpej struct tb_profile *profile;
313 1.1 thorpej {
314 1.1 thorpej struct tb_regulator *tbr, *otbr;
315 1.1 thorpej
316 1.1 thorpej if (machclk_freq == 0)
317 1.1 thorpej init_machclk();
318 1.1 thorpej if (machclk_freq == 0) {
319 1.11 wiz printf("tbr_set: no CPU clock available!\n");
320 1.1 thorpej return (ENXIO);
321 1.1 thorpej }
322 1.12 perry
323 1.1 thorpej if (profile->rate == 0) {
324 1.1 thorpej /* delete this tbr */
325 1.1 thorpej if ((tbr = ifq->altq_tbr) == NULL)
326 1.1 thorpej return (ENOENT);
327 1.1 thorpej ifq->altq_tbr = NULL;
328 1.14 christos free(tbr, M_DEVBUF);
329 1.1 thorpej return (0);
330 1.1 thorpej }
331 1.1 thorpej
332 1.14 christos tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_WAITOK|M_ZERO);
333 1.1 thorpej if (tbr == NULL)
334 1.1 thorpej return (ENOMEM);
335 1.1 thorpej
336 1.1 thorpej tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
337 1.1 thorpej tbr->tbr_depth = TBR_SCALE(profile->depth);
338 1.1 thorpej if (tbr->tbr_rate > 0)
339 1.1 thorpej tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
340 1.1 thorpej else
341 1.1 thorpej tbr->tbr_filluptime = 0xffffffffffffffffLL;
342 1.1 thorpej tbr->tbr_token = tbr->tbr_depth;
343 1.1 thorpej tbr->tbr_last = read_machclk();
344 1.1 thorpej tbr->tbr_lastop = ALTDQ_REMOVE;
345 1.1 thorpej
346 1.1 thorpej otbr = ifq->altq_tbr;
347 1.1 thorpej ifq->altq_tbr = tbr; /* set the new tbr */
348 1.1 thorpej
349 1.1 thorpej if (otbr != NULL)
350 1.14 christos free(otbr, M_DEVBUF);
351 1.1 thorpej else {
352 1.1 thorpej if (tbr_timer == 0) {
353 1.1 thorpej CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
354 1.1 thorpej tbr_timer = 1;
355 1.1 thorpej }
356 1.1 thorpej }
357 1.1 thorpej return (0);
358 1.1 thorpej }
359 1.1 thorpej
360 1.1 thorpej /*
361 1.1 thorpej * tbr_timeout goes through the interface list, and kicks the drivers
362 1.1 thorpej * if necessary.
363 1.1 thorpej */
364 1.1 thorpej static void
365 1.15 christos tbr_timeout(void *arg __unused)
366 1.1 thorpej {
367 1.1 thorpej struct ifnet *ifp;
368 1.1 thorpej int active, s;
369 1.1 thorpej
370 1.1 thorpej active = 0;
371 1.5 thorpej s = splnet();
372 1.1 thorpej #ifdef __FreeBSD__
373 1.1 thorpej #if (__FreeBSD_version < 300000)
374 1.1 thorpej for (ifp = ifnet; ifp; ifp = ifp->if_next)
375 1.1 thorpej #else
376 1.1 thorpej for (ifp = ifnet.tqh_first; ifp != NULL; ifp = ifp->if_link.tqe_next)
377 1.1 thorpej #endif
378 1.1 thorpej #else /* !FreeBSD */
379 1.1 thorpej for (ifp = ifnet.tqh_first; ifp != NULL; ifp = ifp->if_list.tqe_next)
380 1.1 thorpej #endif
381 1.1 thorpej {
382 1.1 thorpej if (!TBR_IS_ENABLED(&ifp->if_snd))
383 1.1 thorpej continue;
384 1.1 thorpej active++;
385 1.1 thorpej if (!IFQ_IS_EMPTY(&ifp->if_snd) && ifp->if_start != NULL)
386 1.1 thorpej (*ifp->if_start)(ifp);
387 1.1 thorpej }
388 1.1 thorpej splx(s);
389 1.1 thorpej if (active > 0)
390 1.1 thorpej CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
391 1.1 thorpej else
392 1.1 thorpej tbr_timer = 0; /* don't need tbr_timer anymore */
393 1.1 thorpej #if defined(__alpha__) && !defined(ALTQ_NOPCC)
394 1.1 thorpej {
395 1.1 thorpej /*
396 1.1 thorpej * XXX read out the machine dependent clock once a second
397 1.1 thorpej * to detect counter wrap-around.
398 1.1 thorpej */
399 1.1 thorpej static u_int cnt;
400 1.1 thorpej
401 1.1 thorpej if (++cnt >= hz) {
402 1.1 thorpej (void)read_machclk();
403 1.1 thorpej cnt = 0;
404 1.1 thorpej }
405 1.1 thorpej }
406 1.1 thorpej #endif /* __alpha__ && !ALTQ_NOPCC */
407 1.1 thorpej }
408 1.1 thorpej
409 1.1 thorpej /*
410 1.1 thorpej * get token bucket regulator profile
411 1.1 thorpej */
412 1.1 thorpej int
413 1.1 thorpej tbr_get(ifq, profile)
414 1.1 thorpej struct ifaltq *ifq;
415 1.1 thorpej struct tb_profile *profile;
416 1.1 thorpej {
417 1.1 thorpej struct tb_regulator *tbr;
418 1.1 thorpej
419 1.1 thorpej if ((tbr = ifq->altq_tbr) == NULL) {
420 1.1 thorpej profile->rate = 0;
421 1.1 thorpej profile->depth = 0;
422 1.1 thorpej } else {
423 1.1 thorpej profile->rate =
424 1.1 thorpej (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
425 1.1 thorpej profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
426 1.1 thorpej }
427 1.1 thorpej return (0);
428 1.1 thorpej }
429 1.1 thorpej
430 1.1 thorpej
431 1.1 thorpej #ifndef IPPROTO_ESP
432 1.1 thorpej #define IPPROTO_ESP 50 /* encapsulating security payload */
433 1.1 thorpej #endif
434 1.1 thorpej #ifndef IPPROTO_AH
435 1.1 thorpej #define IPPROTO_AH 51 /* authentication header */
436 1.1 thorpej #endif
437 1.1 thorpej
438 1.12 perry /*
439 1.1 thorpej * extract flow information from a given packet.
440 1.1 thorpej * filt_mask shows flowinfo fields required.
441 1.1 thorpej * we assume the ip header is in one mbuf, and addresses and ports are
442 1.1 thorpej * in network byte order.
443 1.1 thorpej */
444 1.12 perry int
445 1.1 thorpej altq_extractflow(m, af, flow, filt_bmask)
446 1.1 thorpej struct mbuf *m;
447 1.1 thorpej int af;
448 1.1 thorpej struct flowinfo *flow;
449 1.1 thorpej u_int32_t filt_bmask;
450 1.1 thorpej {
451 1.1 thorpej
452 1.1 thorpej switch (af) {
453 1.1 thorpej case PF_INET: {
454 1.1 thorpej struct flowinfo_in *fin;
455 1.1 thorpej struct ip *ip;
456 1.1 thorpej
457 1.1 thorpej ip = mtod(m, struct ip *);
458 1.1 thorpej
459 1.1 thorpej if (ip->ip_v != 4)
460 1.1 thorpej break;
461 1.8 itojun
462 1.1 thorpej fin = (struct flowinfo_in *)flow;
463 1.1 thorpej fin->fi_len = sizeof(struct flowinfo_in);
464 1.1 thorpej fin->fi_family = AF_INET;
465 1.1 thorpej
466 1.1 thorpej fin->fi_proto = ip->ip_p;
467 1.1 thorpej fin->fi_tos = ip->ip_tos;
468 1.1 thorpej
469 1.1 thorpej fin->fi_src.s_addr = ip->ip_src.s_addr;
470 1.1 thorpej fin->fi_dst.s_addr = ip->ip_dst.s_addr;
471 1.8 itojun
472 1.1 thorpej if (filt_bmask & FIMB4_PORTS)
473 1.1 thorpej /* if port info is required, extract port numbers */
474 1.1 thorpej extract_ports4(m, ip, fin);
475 1.1 thorpej else {
476 1.1 thorpej fin->fi_sport = 0;
477 1.1 thorpej fin->fi_dport = 0;
478 1.1 thorpej fin->fi_gpi = 0;
479 1.1 thorpej }
480 1.1 thorpej return (1);
481 1.1 thorpej }
482 1.8 itojun
483 1.1 thorpej #ifdef INET6
484 1.1 thorpej case PF_INET6: {
485 1.1 thorpej struct flowinfo_in6 *fin6;
486 1.1 thorpej struct ip6_hdr *ip6;
487 1.1 thorpej
488 1.1 thorpej ip6 = mtod(m, struct ip6_hdr *);
489 1.1 thorpej /* should we check the ip version? */
490 1.8 itojun
491 1.1 thorpej fin6 = (struct flowinfo_in6 *)flow;
492 1.1 thorpej fin6->fi6_len = sizeof(struct flowinfo_in6);
493 1.1 thorpej fin6->fi6_family = AF_INET6;
494 1.1 thorpej
495 1.1 thorpej fin6->fi6_proto = ip6->ip6_nxt;
496 1.1 thorpej fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
497 1.1 thorpej
498 1.1 thorpej fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
499 1.1 thorpej fin6->fi6_src = ip6->ip6_src;
500 1.1 thorpej fin6->fi6_dst = ip6->ip6_dst;
501 1.1 thorpej
502 1.1 thorpej if ((filt_bmask & FIMB6_PORTS) ||
503 1.1 thorpej ((filt_bmask & FIMB6_PROTO)
504 1.1 thorpej && ip6->ip6_nxt > IPPROTO_IPV6))
505 1.1 thorpej /*
506 1.1 thorpej * if port info is required, or proto is required
507 1.1 thorpej * but there are option headers, extract port
508 1.1 thorpej * and protocol numbers.
509 1.1 thorpej */
510 1.1 thorpej extract_ports6(m, ip6, fin6);
511 1.1 thorpej else {
512 1.1 thorpej fin6->fi6_sport = 0;
513 1.1 thorpej fin6->fi6_dport = 0;
514 1.1 thorpej fin6->fi6_gpi = 0;
515 1.1 thorpej }
516 1.1 thorpej return (1);
517 1.1 thorpej }
518 1.1 thorpej #endif /* INET6 */
519 1.1 thorpej
520 1.1 thorpej default:
521 1.1 thorpej break;
522 1.1 thorpej }
523 1.1 thorpej
524 1.1 thorpej /* failed */
525 1.1 thorpej flow->fi_len = sizeof(struct flowinfo);
526 1.1 thorpej flow->fi_family = AF_UNSPEC;
527 1.1 thorpej return (0);
528 1.1 thorpej }
529 1.1 thorpej
530 1.1 thorpej /*
531 1.1 thorpej * helper routine to extract port numbers
532 1.1 thorpej */
533 1.1 thorpej /* structure for ipsec and ipv6 option header template */
534 1.1 thorpej struct _opt6 {
535 1.1 thorpej u_int8_t opt6_nxt; /* next header */
536 1.1 thorpej u_int8_t opt6_hlen; /* header extension length */
537 1.1 thorpej u_int16_t _pad;
538 1.1 thorpej u_int32_t ah_spi; /* security parameter index
539 1.1 thorpej for authentication header */
540 1.1 thorpej };
541 1.1 thorpej
542 1.1 thorpej /*
543 1.1 thorpej * extract port numbers from a ipv4 packet.
544 1.1 thorpej */
545 1.1 thorpej static int
546 1.1 thorpej extract_ports4(m, ip, fin)
547 1.1 thorpej struct mbuf *m;
548 1.1 thorpej struct ip *ip;
549 1.1 thorpej struct flowinfo_in *fin;
550 1.1 thorpej {
551 1.1 thorpej struct mbuf *m0;
552 1.1 thorpej u_short ip_off;
553 1.1 thorpej u_int8_t proto;
554 1.1 thorpej int off;
555 1.8 itojun
556 1.1 thorpej fin->fi_sport = 0;
557 1.1 thorpej fin->fi_dport = 0;
558 1.1 thorpej fin->fi_gpi = 0;
559 1.8 itojun
560 1.1 thorpej ip_off = ntohs(ip->ip_off);
561 1.1 thorpej /* if it is a fragment, try cached fragment info */
562 1.1 thorpej if (ip_off & IP_OFFMASK) {
563 1.1 thorpej ip4f_lookup(ip, fin);
564 1.1 thorpej return (1);
565 1.1 thorpej }
566 1.1 thorpej
567 1.1 thorpej /* locate the mbuf containing the protocol header */
568 1.1 thorpej for (m0 = m; m0 != NULL; m0 = m0->m_next)
569 1.1 thorpej if (((caddr_t)ip >= m0->m_data) &&
570 1.1 thorpej ((caddr_t)ip < m0->m_data + m0->m_len))
571 1.1 thorpej break;
572 1.1 thorpej if (m0 == NULL) {
573 1.1 thorpej #ifdef ALTQ_DEBUG
574 1.1 thorpej printf("extract_ports4: can't locate header! ip=%p\n", ip);
575 1.1 thorpej #endif
576 1.1 thorpej return (0);
577 1.1 thorpej }
578 1.1 thorpej off = ((caddr_t)ip - m0->m_data) + (ip->ip_hl << 2);
579 1.1 thorpej proto = ip->ip_p;
580 1.1 thorpej
581 1.1 thorpej #ifdef ALTQ_IPSEC
582 1.1 thorpej again:
583 1.1 thorpej #endif
584 1.1 thorpej while (off >= m0->m_len) {
585 1.1 thorpej off -= m0->m_len;
586 1.1 thorpej m0 = m0->m_next;
587 1.8 itojun if (m0 == NULL)
588 1.8 itojun return (0); /* bogus ip_hl! */
589 1.1 thorpej }
590 1.8 itojun if (m0->m_len < off + 4)
591 1.8 itojun return (0);
592 1.1 thorpej
593 1.1 thorpej switch (proto) {
594 1.1 thorpej case IPPROTO_TCP:
595 1.1 thorpej case IPPROTO_UDP: {
596 1.1 thorpej struct udphdr *udp;
597 1.8 itojun
598 1.1 thorpej udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
599 1.1 thorpej fin->fi_sport = udp->uh_sport;
600 1.1 thorpej fin->fi_dport = udp->uh_dport;
601 1.1 thorpej fin->fi_proto = proto;
602 1.1 thorpej }
603 1.1 thorpej break;
604 1.1 thorpej
605 1.1 thorpej #ifdef ALTQ_IPSEC
606 1.1 thorpej case IPPROTO_ESP:
607 1.1 thorpej if (fin->fi_gpi == 0){
608 1.1 thorpej u_int32_t *gpi;
609 1.8 itojun
610 1.1 thorpej gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
611 1.1 thorpej fin->fi_gpi = *gpi;
612 1.1 thorpej }
613 1.1 thorpej fin->fi_proto = proto;
614 1.1 thorpej break;
615 1.1 thorpej
616 1.1 thorpej case IPPROTO_AH: {
617 1.1 thorpej /* get next header and header length */
618 1.1 thorpej struct _opt6 *opt6;
619 1.1 thorpej
620 1.1 thorpej opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
621 1.1 thorpej proto = opt6->opt6_nxt;
622 1.1 thorpej off += 8 + (opt6->opt6_hlen * 4);
623 1.8 itojun if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
624 1.1 thorpej fin->fi_gpi = opt6->ah_spi;
625 1.1 thorpej }
626 1.1 thorpej /* goto the next header */
627 1.1 thorpej goto again;
628 1.1 thorpej #endif /* ALTQ_IPSEC */
629 1.1 thorpej
630 1.1 thorpej default:
631 1.1 thorpej fin->fi_proto = proto;
632 1.1 thorpej return (0);
633 1.1 thorpej }
634 1.1 thorpej
635 1.1 thorpej /* if this is a first fragment, cache it. */
636 1.1 thorpej if (ip_off & IP_MF)
637 1.1 thorpej ip4f_cache(ip, fin);
638 1.1 thorpej
639 1.1 thorpej return (1);
640 1.1 thorpej }
641 1.1 thorpej
642 1.1 thorpej #ifdef INET6
643 1.1 thorpej static int
644 1.1 thorpej extract_ports6(m, ip6, fin6)
645 1.1 thorpej struct mbuf *m;
646 1.1 thorpej struct ip6_hdr *ip6;
647 1.1 thorpej struct flowinfo_in6 *fin6;
648 1.1 thorpej {
649 1.1 thorpej struct mbuf *m0;
650 1.1 thorpej int off;
651 1.1 thorpej u_int8_t proto;
652 1.8 itojun
653 1.1 thorpej fin6->fi6_gpi = 0;
654 1.1 thorpej fin6->fi6_sport = 0;
655 1.1 thorpej fin6->fi6_dport = 0;
656 1.8 itojun
657 1.1 thorpej /* locate the mbuf containing the protocol header */
658 1.1 thorpej for (m0 = m; m0 != NULL; m0 = m0->m_next)
659 1.1 thorpej if (((caddr_t)ip6 >= m0->m_data) &&
660 1.1 thorpej ((caddr_t)ip6 < m0->m_data + m0->m_len))
661 1.1 thorpej break;
662 1.1 thorpej if (m0 == NULL) {
663 1.1 thorpej #ifdef ALTQ_DEBUG
664 1.1 thorpej printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
665 1.1 thorpej #endif
666 1.1 thorpej return (0);
667 1.1 thorpej }
668 1.1 thorpej off = ((caddr_t)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
669 1.1 thorpej
670 1.1 thorpej proto = ip6->ip6_nxt;
671 1.1 thorpej do {
672 1.1 thorpej while (off >= m0->m_len) {
673 1.1 thorpej off -= m0->m_len;
674 1.1 thorpej m0 = m0->m_next;
675 1.8 itojun if (m0 == NULL)
676 1.8 itojun return (0);
677 1.1 thorpej }
678 1.8 itojun if (m0->m_len < off + 4)
679 1.8 itojun return (0);
680 1.1 thorpej
681 1.1 thorpej switch (proto) {
682 1.1 thorpej case IPPROTO_TCP:
683 1.1 thorpej case IPPROTO_UDP: {
684 1.1 thorpej struct udphdr *udp;
685 1.8 itojun
686 1.1 thorpej udp = (struct udphdr *)(mtod(m0, caddr_t) + off);
687 1.1 thorpej fin6->fi6_sport = udp->uh_sport;
688 1.1 thorpej fin6->fi6_dport = udp->uh_dport;
689 1.1 thorpej fin6->fi6_proto = proto;
690 1.1 thorpej }
691 1.1 thorpej return (1);
692 1.8 itojun
693 1.1 thorpej case IPPROTO_ESP:
694 1.1 thorpej if (fin6->fi6_gpi == 0) {
695 1.1 thorpej u_int32_t *gpi;
696 1.8 itojun
697 1.1 thorpej gpi = (u_int32_t *)(mtod(m0, caddr_t) + off);
698 1.1 thorpej fin6->fi6_gpi = *gpi;
699 1.1 thorpej }
700 1.1 thorpej fin6->fi6_proto = proto;
701 1.1 thorpej return (1);
702 1.1 thorpej
703 1.1 thorpej case IPPROTO_AH: {
704 1.1 thorpej /* get next header and header length */
705 1.1 thorpej struct _opt6 *opt6;
706 1.1 thorpej
707 1.1 thorpej opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
708 1.8 itojun if (fin6->fi6_gpi == 0 && m0->m_len >= off + 8)
709 1.1 thorpej fin6->fi6_gpi = opt6->ah_spi;
710 1.1 thorpej proto = opt6->opt6_nxt;
711 1.1 thorpej off += 8 + (opt6->opt6_hlen * 4);
712 1.1 thorpej /* goto the next header */
713 1.1 thorpej break;
714 1.1 thorpej }
715 1.1 thorpej
716 1.1 thorpej case IPPROTO_HOPOPTS:
717 1.1 thorpej case IPPROTO_ROUTING:
718 1.1 thorpej case IPPROTO_DSTOPTS: {
719 1.1 thorpej /* get next header and header length */
720 1.1 thorpej struct _opt6 *opt6;
721 1.1 thorpej
722 1.1 thorpej opt6 = (struct _opt6 *)(mtod(m0, caddr_t) + off);
723 1.1 thorpej proto = opt6->opt6_nxt;
724 1.1 thorpej off += (opt6->opt6_hlen + 1) * 8;
725 1.1 thorpej /* goto the next header */
726 1.1 thorpej break;
727 1.1 thorpej }
728 1.8 itojun
729 1.1 thorpej case IPPROTO_FRAGMENT:
730 1.1 thorpej /* ipv6 fragmentations are not supported yet */
731 1.1 thorpej default:
732 1.1 thorpej fin6->fi6_proto = proto;
733 1.1 thorpej return (0);
734 1.1 thorpej }
735 1.1 thorpej } while (1);
736 1.1 thorpej /*NOTREACHED*/
737 1.1 thorpej }
738 1.1 thorpej #endif /* INET6 */
739 1.1 thorpej
740 1.1 thorpej /*
741 1.1 thorpej * altq common classifier
742 1.1 thorpej */
743 1.1 thorpej int
744 1.1 thorpej acc_add_filter(classifier, filter, class, phandle)
745 1.1 thorpej struct acc_classifier *classifier;
746 1.1 thorpej struct flow_filter *filter;
747 1.1 thorpej void *class;
748 1.1 thorpej u_long *phandle;
749 1.1 thorpej {
750 1.1 thorpej struct acc_filter *afp, *prev, *tmp;
751 1.1 thorpej int i, s;
752 1.1 thorpej
753 1.1 thorpej #ifdef INET6
754 1.1 thorpej if (filter->ff_flow.fi_family != AF_INET &&
755 1.1 thorpej filter->ff_flow.fi_family != AF_INET6)
756 1.1 thorpej return (EINVAL);
757 1.1 thorpej #else
758 1.1 thorpej if (filter->ff_flow.fi_family != AF_INET)
759 1.1 thorpej return (EINVAL);
760 1.1 thorpej #endif
761 1.8 itojun
762 1.14 christos afp = malloc(sizeof(struct acc_filter), M_DEVBUF, M_WAITOK|M_ZERO);
763 1.1 thorpej if (afp == NULL)
764 1.1 thorpej return (ENOMEM);
765 1.1 thorpej
766 1.1 thorpej afp->f_filter = *filter;
767 1.1 thorpej afp->f_class = class;
768 1.1 thorpej
769 1.1 thorpej i = ACC_WILDCARD_INDEX;
770 1.1 thorpej if (filter->ff_flow.fi_family == AF_INET) {
771 1.1 thorpej struct flow_filter *filter4 = &afp->f_filter;
772 1.8 itojun
773 1.1 thorpej /*
774 1.1 thorpej * if address is 0, it's a wildcard. if address mask
775 1.1 thorpej * isn't set, use full mask.
776 1.1 thorpej */
777 1.1 thorpej if (filter4->ff_flow.fi_dst.s_addr == 0)
778 1.1 thorpej filter4->ff_mask.mask_dst.s_addr = 0;
779 1.1 thorpej else if (filter4->ff_mask.mask_dst.s_addr == 0)
780 1.1 thorpej filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
781 1.1 thorpej if (filter4->ff_flow.fi_src.s_addr == 0)
782 1.1 thorpej filter4->ff_mask.mask_src.s_addr = 0;
783 1.1 thorpej else if (filter4->ff_mask.mask_src.s_addr == 0)
784 1.1 thorpej filter4->ff_mask.mask_src.s_addr = 0xffffffff;
785 1.1 thorpej
786 1.1 thorpej /* clear extra bits in addresses */
787 1.1 thorpej filter4->ff_flow.fi_dst.s_addr &=
788 1.1 thorpej filter4->ff_mask.mask_dst.s_addr;
789 1.1 thorpej filter4->ff_flow.fi_src.s_addr &=
790 1.1 thorpej filter4->ff_mask.mask_src.s_addr;
791 1.1 thorpej
792 1.1 thorpej /*
793 1.1 thorpej * if dst address is a wildcard, use hash-entry
794 1.1 thorpej * ACC_WILDCARD_INDEX.
795 1.1 thorpej */
796 1.1 thorpej if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
797 1.1 thorpej i = ACC_WILDCARD_INDEX;
798 1.1 thorpej else
799 1.1 thorpej i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
800 1.1 thorpej }
801 1.1 thorpej #ifdef INET6
802 1.1 thorpej else if (filter->ff_flow.fi_family == AF_INET6) {
803 1.1 thorpej struct flow_filter6 *filter6 =
804 1.1 thorpej (struct flow_filter6 *)&afp->f_filter;
805 1.1 thorpej #ifndef IN6MASK0 /* taken from kame ipv6 */
806 1.1 thorpej #define IN6MASK0 {{{ 0, 0, 0, 0 }}}
807 1.1 thorpej #define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
808 1.1 thorpej const struct in6_addr in6mask0 = IN6MASK0;
809 1.1 thorpej const struct in6_addr in6mask128 = IN6MASK128;
810 1.1 thorpej #endif
811 1.1 thorpej
812 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
813 1.1 thorpej filter6->ff_mask6.mask6_dst = in6mask0;
814 1.1 thorpej else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
815 1.1 thorpej filter6->ff_mask6.mask6_dst = in6mask128;
816 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
817 1.1 thorpej filter6->ff_mask6.mask6_src = in6mask0;
818 1.1 thorpej else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
819 1.1 thorpej filter6->ff_mask6.mask6_src = in6mask128;
820 1.1 thorpej
821 1.1 thorpej /* clear extra bits in addresses */
822 1.1 thorpej for (i = 0; i < 16; i++)
823 1.1 thorpej filter6->ff_flow6.fi6_dst.s6_addr[i] &=
824 1.1 thorpej filter6->ff_mask6.mask6_dst.s6_addr[i];
825 1.1 thorpej for (i = 0; i < 16; i++)
826 1.1 thorpej filter6->ff_flow6.fi6_src.s6_addr[i] &=
827 1.1 thorpej filter6->ff_mask6.mask6_src.s6_addr[i];
828 1.8 itojun
829 1.1 thorpej if (filter6->ff_flow6.fi6_flowlabel == 0)
830 1.1 thorpej i = ACC_WILDCARD_INDEX;
831 1.1 thorpej else
832 1.1 thorpej i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
833 1.1 thorpej }
834 1.1 thorpej #endif /* INET6 */
835 1.1 thorpej
836 1.1 thorpej afp->f_handle = get_filt_handle(classifier, i);
837 1.1 thorpej
838 1.1 thorpej /* update filter bitmask */
839 1.1 thorpej afp->f_fbmask = filt2fibmask(filter);
840 1.1 thorpej classifier->acc_fbmask |= afp->f_fbmask;
841 1.1 thorpej
842 1.1 thorpej /*
843 1.1 thorpej * add this filter to the filter list.
844 1.1 thorpej * filters are ordered from the highest rule number.
845 1.1 thorpej */
846 1.5 thorpej s = splnet();
847 1.1 thorpej prev = NULL;
848 1.1 thorpej LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
849 1.1 thorpej if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
850 1.1 thorpej prev = tmp;
851 1.1 thorpej else
852 1.1 thorpej break;
853 1.1 thorpej }
854 1.1 thorpej if (prev == NULL)
855 1.1 thorpej LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
856 1.1 thorpej else
857 1.1 thorpej LIST_INSERT_AFTER(prev, afp, f_chain);
858 1.1 thorpej splx(s);
859 1.1 thorpej
860 1.1 thorpej *phandle = afp->f_handle;
861 1.1 thorpej return (0);
862 1.1 thorpej }
863 1.1 thorpej
864 1.1 thorpej int
865 1.1 thorpej acc_delete_filter(classifier, handle)
866 1.1 thorpej struct acc_classifier *classifier;
867 1.1 thorpej u_long handle;
868 1.1 thorpej {
869 1.1 thorpej struct acc_filter *afp;
870 1.1 thorpej int s;
871 1.1 thorpej
872 1.1 thorpej if ((afp = filth_to_filtp(classifier, handle)) == NULL)
873 1.1 thorpej return (EINVAL);
874 1.1 thorpej
875 1.5 thorpej s = splnet();
876 1.1 thorpej LIST_REMOVE(afp, f_chain);
877 1.1 thorpej splx(s);
878 1.1 thorpej
879 1.14 christos free(afp, M_DEVBUF);
880 1.1 thorpej
881 1.1 thorpej /* todo: update filt_bmask */
882 1.1 thorpej
883 1.1 thorpej return (0);
884 1.1 thorpej }
885 1.1 thorpej
886 1.1 thorpej /*
887 1.1 thorpej * delete filters referencing to the specified class.
888 1.1 thorpej * if the all flag is not 0, delete all the filters.
889 1.1 thorpej */
890 1.1 thorpej int
891 1.1 thorpej acc_discard_filters(classifier, class, all)
892 1.1 thorpej struct acc_classifier *classifier;
893 1.1 thorpej void *class;
894 1.1 thorpej int all;
895 1.1 thorpej {
896 1.1 thorpej struct acc_filter *afp;
897 1.1 thorpej int i, s;
898 1.1 thorpej
899 1.5 thorpej s = splnet();
900 1.1 thorpej for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
901 1.1 thorpej do {
902 1.1 thorpej LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
903 1.1 thorpej if (all || afp->f_class == class) {
904 1.1 thorpej LIST_REMOVE(afp, f_chain);
905 1.14 christos free(afp, M_DEVBUF);
906 1.1 thorpej /* start again from the head */
907 1.1 thorpej break;
908 1.1 thorpej }
909 1.1 thorpej } while (afp != NULL);
910 1.1 thorpej }
911 1.1 thorpej splx(s);
912 1.1 thorpej
913 1.1 thorpej if (all)
914 1.1 thorpej classifier->acc_fbmask = 0;
915 1.1 thorpej
916 1.1 thorpej return (0);
917 1.1 thorpej }
918 1.1 thorpej
919 1.1 thorpej void *
920 1.1 thorpej acc_classify(clfier, m, af)
921 1.1 thorpej void *clfier;
922 1.1 thorpej struct mbuf *m;
923 1.1 thorpej int af;
924 1.1 thorpej {
925 1.1 thorpej struct acc_classifier *classifier;
926 1.1 thorpej struct flowinfo flow;
927 1.1 thorpej struct acc_filter *afp;
928 1.1 thorpej int i;
929 1.1 thorpej
930 1.1 thorpej classifier = (struct acc_classifier *)clfier;
931 1.1 thorpej altq_extractflow(m, af, &flow, classifier->acc_fbmask);
932 1.1 thorpej
933 1.1 thorpej if (flow.fi_family == AF_INET) {
934 1.1 thorpej struct flowinfo_in *fp = (struct flowinfo_in *)&flow;
935 1.8 itojun
936 1.1 thorpej if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
937 1.1 thorpej /* only tos is used */
938 1.1 thorpej LIST_FOREACH(afp,
939 1.1 thorpej &classifier->acc_filters[ACC_WILDCARD_INDEX],
940 1.1 thorpej f_chain)
941 1.1 thorpej if (apply_tosfilter4(afp->f_fbmask,
942 1.1 thorpej &afp->f_filter, fp))
943 1.1 thorpej /* filter matched */
944 1.1 thorpej return (afp->f_class);
945 1.1 thorpej } else if ((classifier->acc_fbmask &
946 1.1 thorpej (~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
947 1.1 thorpej == 0) {
948 1.1 thorpej /* only proto and ports are used */
949 1.1 thorpej LIST_FOREACH(afp,
950 1.1 thorpej &classifier->acc_filters[ACC_WILDCARD_INDEX],
951 1.1 thorpej f_chain)
952 1.1 thorpej if (apply_ppfilter4(afp->f_fbmask,
953 1.1 thorpej &afp->f_filter, fp))
954 1.1 thorpej /* filter matched */
955 1.1 thorpej return (afp->f_class);
956 1.1 thorpej } else {
957 1.1 thorpej /* get the filter hash entry from its dest address */
958 1.1 thorpej i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
959 1.1 thorpej do {
960 1.1 thorpej /*
961 1.1 thorpej * go through this loop twice. first for dst
962 1.1 thorpej * hash, second for wildcards.
963 1.1 thorpej */
964 1.1 thorpej LIST_FOREACH(afp, &classifier->acc_filters[i],
965 1.1 thorpej f_chain)
966 1.1 thorpej if (apply_filter4(afp->f_fbmask,
967 1.1 thorpej &afp->f_filter, fp))
968 1.1 thorpej /* filter matched */
969 1.1 thorpej return (afp->f_class);
970 1.8 itojun
971 1.1 thorpej /*
972 1.1 thorpej * check again for filters with a dst addr
973 1.1 thorpej * wildcard.
974 1.1 thorpej * (daddr == 0 || dmask != 0xffffffff).
975 1.1 thorpej */
976 1.1 thorpej if (i != ACC_WILDCARD_INDEX)
977 1.1 thorpej i = ACC_WILDCARD_INDEX;
978 1.1 thorpej else
979 1.1 thorpej break;
980 1.1 thorpej } while (1);
981 1.1 thorpej }
982 1.1 thorpej }
983 1.1 thorpej #ifdef INET6
984 1.1 thorpej else if (flow.fi_family == AF_INET6) {
985 1.1 thorpej struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
986 1.8 itojun
987 1.1 thorpej /* get the filter hash entry from its flow ID */
988 1.1 thorpej if (fp6->fi6_flowlabel != 0)
989 1.1 thorpej i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
990 1.1 thorpej else
991 1.1 thorpej /* flowlable can be zero */
992 1.1 thorpej i = ACC_WILDCARD_INDEX;
993 1.1 thorpej
994 1.1 thorpej /* go through this loop twice. first for flow hash, second
995 1.1 thorpej for wildcards. */
996 1.1 thorpej do {
997 1.1 thorpej LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
998 1.1 thorpej if (apply_filter6(afp->f_fbmask,
999 1.1 thorpej (struct flow_filter6 *)&afp->f_filter,
1000 1.1 thorpej fp6))
1001 1.1 thorpej /* filter matched */
1002 1.1 thorpej return (afp->f_class);
1003 1.1 thorpej
1004 1.1 thorpej /*
1005 1.1 thorpej * check again for filters with a wildcard.
1006 1.1 thorpej */
1007 1.1 thorpej if (i != ACC_WILDCARD_INDEX)
1008 1.1 thorpej i = ACC_WILDCARD_INDEX;
1009 1.1 thorpej else
1010 1.1 thorpej break;
1011 1.1 thorpej } while (1);
1012 1.1 thorpej }
1013 1.1 thorpej #endif /* INET6 */
1014 1.1 thorpej
1015 1.1 thorpej /* no filter matched */
1016 1.1 thorpej return (NULL);
1017 1.1 thorpej }
1018 1.1 thorpej
1019 1.1 thorpej static int
1020 1.1 thorpej apply_filter4(fbmask, filt, pkt)
1021 1.1 thorpej u_int32_t fbmask;
1022 1.1 thorpej struct flow_filter *filt;
1023 1.1 thorpej struct flowinfo_in *pkt;
1024 1.1 thorpej {
1025 1.1 thorpej if (filt->ff_flow.fi_family != AF_INET)
1026 1.1 thorpej return (0);
1027 1.1 thorpej if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1028 1.1 thorpej return (0);
1029 1.1 thorpej if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1030 1.1 thorpej return (0);
1031 1.1 thorpej if ((fbmask & FIMB4_DADDR) &&
1032 1.1 thorpej filt->ff_flow.fi_dst.s_addr !=
1033 1.1 thorpej (pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
1034 1.1 thorpej return (0);
1035 1.1 thorpej if ((fbmask & FIMB4_SADDR) &&
1036 1.1 thorpej filt->ff_flow.fi_src.s_addr !=
1037 1.1 thorpej (pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
1038 1.1 thorpej return (0);
1039 1.1 thorpej if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1040 1.1 thorpej return (0);
1041 1.1 thorpej if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1042 1.1 thorpej (pkt->fi_tos & filt->ff_mask.mask_tos))
1043 1.1 thorpej return (0);
1044 1.1 thorpej if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
1045 1.1 thorpej return (0);
1046 1.1 thorpej /* match */
1047 1.1 thorpej return (1);
1048 1.1 thorpej }
1049 1.1 thorpej
1050 1.1 thorpej /*
1051 1.1 thorpej * filter matching function optimized for a common case that checks
1052 1.1 thorpej * only protocol and port numbers
1053 1.1 thorpej */
1054 1.1 thorpej static int
1055 1.1 thorpej apply_ppfilter4(fbmask, filt, pkt)
1056 1.1 thorpej u_int32_t fbmask;
1057 1.1 thorpej struct flow_filter *filt;
1058 1.1 thorpej struct flowinfo_in *pkt;
1059 1.1 thorpej {
1060 1.1 thorpej if (filt->ff_flow.fi_family != AF_INET)
1061 1.1 thorpej return (0);
1062 1.1 thorpej if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
1063 1.1 thorpej return (0);
1064 1.1 thorpej if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
1065 1.1 thorpej return (0);
1066 1.1 thorpej if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
1067 1.1 thorpej return (0);
1068 1.1 thorpej /* match */
1069 1.1 thorpej return (1);
1070 1.1 thorpej }
1071 1.1 thorpej
1072 1.1 thorpej /*
1073 1.1 thorpej * filter matching function only for tos field.
1074 1.1 thorpej */
1075 1.1 thorpej static int
1076 1.1 thorpej apply_tosfilter4(fbmask, filt, pkt)
1077 1.1 thorpej u_int32_t fbmask;
1078 1.1 thorpej struct flow_filter *filt;
1079 1.1 thorpej struct flowinfo_in *pkt;
1080 1.1 thorpej {
1081 1.1 thorpej if (filt->ff_flow.fi_family != AF_INET)
1082 1.1 thorpej return (0);
1083 1.1 thorpej if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
1084 1.1 thorpej (pkt->fi_tos & filt->ff_mask.mask_tos))
1085 1.1 thorpej return (0);
1086 1.1 thorpej /* match */
1087 1.1 thorpej return (1);
1088 1.1 thorpej }
1089 1.1 thorpej
1090 1.1 thorpej #ifdef INET6
1091 1.1 thorpej static int
1092 1.1 thorpej apply_filter6(fbmask, filt, pkt)
1093 1.1 thorpej u_int32_t fbmask;
1094 1.1 thorpej struct flow_filter6 *filt;
1095 1.1 thorpej struct flowinfo_in6 *pkt;
1096 1.1 thorpej {
1097 1.1 thorpej int i;
1098 1.1 thorpej
1099 1.1 thorpej if (filt->ff_flow6.fi6_family != AF_INET6)
1100 1.1 thorpej return (0);
1101 1.1 thorpej if ((fbmask & FIMB6_FLABEL) &&
1102 1.1 thorpej filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
1103 1.1 thorpej return (0);
1104 1.1 thorpej if ((fbmask & FIMB6_PROTO) &&
1105 1.1 thorpej filt->ff_flow6.fi6_proto != pkt->fi6_proto)
1106 1.1 thorpej return (0);
1107 1.1 thorpej if ((fbmask & FIMB6_SPORT) &&
1108 1.1 thorpej filt->ff_flow6.fi6_sport != pkt->fi6_sport)
1109 1.1 thorpej return (0);
1110 1.1 thorpej if ((fbmask & FIMB6_DPORT) &&
1111 1.1 thorpej filt->ff_flow6.fi6_dport != pkt->fi6_dport)
1112 1.1 thorpej return (0);
1113 1.1 thorpej if (fbmask & FIMB6_SADDR) {
1114 1.1 thorpej for (i = 0; i < 4; i++)
1115 1.1 thorpej if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
1116 1.1 thorpej (pkt->fi6_src.s6_addr32[i] &
1117 1.1 thorpej filt->ff_mask6.mask6_src.s6_addr32[i]))
1118 1.1 thorpej return (0);
1119 1.1 thorpej }
1120 1.1 thorpej if (fbmask & FIMB6_DADDR) {
1121 1.1 thorpej for (i = 0; i < 4; i++)
1122 1.1 thorpej if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
1123 1.1 thorpej (pkt->fi6_dst.s6_addr32[i] &
1124 1.1 thorpej filt->ff_mask6.mask6_dst.s6_addr32[i]))
1125 1.1 thorpej return (0);
1126 1.1 thorpej }
1127 1.1 thorpej if ((fbmask & FIMB6_TCLASS) &&
1128 1.1 thorpej filt->ff_flow6.fi6_tclass !=
1129 1.1 thorpej (pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
1130 1.1 thorpej return (0);
1131 1.1 thorpej if ((fbmask & FIMB6_GPI) &&
1132 1.1 thorpej filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
1133 1.1 thorpej return (0);
1134 1.1 thorpej /* match */
1135 1.1 thorpej return (1);
1136 1.1 thorpej }
1137 1.1 thorpej #endif /* INET6 */
1138 1.1 thorpej
1139 1.1 thorpej /*
1140 1.1 thorpej * filter handle:
1141 1.1 thorpej * bit 20-28: index to the filter hash table
1142 1.1 thorpej * bit 0-19: unique id in the hash bucket.
1143 1.1 thorpej */
1144 1.1 thorpej static u_long
1145 1.1 thorpej get_filt_handle(classifier, i)
1146 1.1 thorpej struct acc_classifier *classifier;
1147 1.1 thorpej int i;
1148 1.1 thorpej {
1149 1.1 thorpej static u_long handle_number = 1;
1150 1.1 thorpej u_long handle;
1151 1.1 thorpej struct acc_filter *afp;
1152 1.1 thorpej
1153 1.1 thorpej while (1) {
1154 1.1 thorpej handle = handle_number++ & 0x000fffff;
1155 1.1 thorpej
1156 1.1 thorpej if (LIST_EMPTY(&classifier->acc_filters[i]))
1157 1.1 thorpej break;
1158 1.1 thorpej
1159 1.1 thorpej LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1160 1.1 thorpej if ((afp->f_handle & 0x000fffff) == handle)
1161 1.1 thorpej break;
1162 1.1 thorpej if (afp == NULL)
1163 1.1 thorpej break;
1164 1.1 thorpej /* this handle is already used, try again */
1165 1.1 thorpej }
1166 1.1 thorpej
1167 1.1 thorpej return ((i << 20) | handle);
1168 1.1 thorpej }
1169 1.1 thorpej
1170 1.1 thorpej /* convert filter handle to filter pointer */
1171 1.1 thorpej static struct acc_filter *
1172 1.1 thorpej filth_to_filtp(classifier, handle)
1173 1.1 thorpej struct acc_classifier *classifier;
1174 1.1 thorpej u_long handle;
1175 1.1 thorpej {
1176 1.1 thorpej struct acc_filter *afp;
1177 1.1 thorpej int i;
1178 1.1 thorpej
1179 1.1 thorpej i = ACC_GET_HINDEX(handle);
1180 1.1 thorpej
1181 1.1 thorpej LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
1182 1.1 thorpej if (afp->f_handle == handle)
1183 1.1 thorpej return (afp);
1184 1.1 thorpej
1185 1.1 thorpej return (NULL);
1186 1.1 thorpej }
1187 1.1 thorpej
1188 1.1 thorpej /* create flowinfo bitmask */
1189 1.1 thorpej static u_int32_t
1190 1.1 thorpej filt2fibmask(filt)
1191 1.1 thorpej struct flow_filter *filt;
1192 1.1 thorpej {
1193 1.1 thorpej u_int32_t mask = 0;
1194 1.1 thorpej #ifdef INET6
1195 1.1 thorpej struct flow_filter6 *filt6;
1196 1.1 thorpej #endif
1197 1.1 thorpej
1198 1.1 thorpej switch (filt->ff_flow.fi_family) {
1199 1.1 thorpej case AF_INET:
1200 1.1 thorpej if (filt->ff_flow.fi_proto != 0)
1201 1.1 thorpej mask |= FIMB4_PROTO;
1202 1.1 thorpej if (filt->ff_flow.fi_tos != 0)
1203 1.1 thorpej mask |= FIMB4_TOS;
1204 1.1 thorpej if (filt->ff_flow.fi_dst.s_addr != 0)
1205 1.1 thorpej mask |= FIMB4_DADDR;
1206 1.1 thorpej if (filt->ff_flow.fi_src.s_addr != 0)
1207 1.1 thorpej mask |= FIMB4_SADDR;
1208 1.1 thorpej if (filt->ff_flow.fi_sport != 0)
1209 1.1 thorpej mask |= FIMB4_SPORT;
1210 1.1 thorpej if (filt->ff_flow.fi_dport != 0)
1211 1.1 thorpej mask |= FIMB4_DPORT;
1212 1.1 thorpej if (filt->ff_flow.fi_gpi != 0)
1213 1.1 thorpej mask |= FIMB4_GPI;
1214 1.1 thorpej break;
1215 1.1 thorpej #ifdef INET6
1216 1.1 thorpej case AF_INET6:
1217 1.1 thorpej filt6 = (struct flow_filter6 *)filt;
1218 1.1 thorpej
1219 1.1 thorpej if (filt6->ff_flow6.fi6_proto != 0)
1220 1.1 thorpej mask |= FIMB6_PROTO;
1221 1.1 thorpej if (filt6->ff_flow6.fi6_tclass != 0)
1222 1.1 thorpej mask |= FIMB6_TCLASS;
1223 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
1224 1.1 thorpej mask |= FIMB6_DADDR;
1225 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
1226 1.1 thorpej mask |= FIMB6_SADDR;
1227 1.1 thorpej if (filt6->ff_flow6.fi6_sport != 0)
1228 1.1 thorpej mask |= FIMB6_SPORT;
1229 1.1 thorpej if (filt6->ff_flow6.fi6_dport != 0)
1230 1.1 thorpej mask |= FIMB6_DPORT;
1231 1.1 thorpej if (filt6->ff_flow6.fi6_gpi != 0)
1232 1.1 thorpej mask |= FIMB6_GPI;
1233 1.1 thorpej if (filt6->ff_flow6.fi6_flowlabel != 0)
1234 1.1 thorpej mask |= FIMB6_FLABEL;
1235 1.1 thorpej break;
1236 1.1 thorpej #endif /* INET6 */
1237 1.1 thorpej }
1238 1.1 thorpej return (mask);
1239 1.1 thorpej }
1240 1.1 thorpej
1241 1.1 thorpej
1242 1.1 thorpej /*
1243 1.1 thorpej * helper functions to handle IPv4 fragments.
1244 1.1 thorpej * currently only in-sequence fragments are handled.
1245 1.1 thorpej * - fragment info is cached in a LRU list.
1246 1.1 thorpej * - when a first fragment is found, cache its flow info.
1247 1.1 thorpej * - when a non-first fragment is found, lookup the cache.
1248 1.1 thorpej */
1249 1.1 thorpej
1250 1.1 thorpej struct ip4_frag {
1251 1.1 thorpej TAILQ_ENTRY(ip4_frag) ip4f_chain;
1252 1.1 thorpej char ip4f_valid;
1253 1.1 thorpej u_short ip4f_id;
1254 1.1 thorpej struct flowinfo_in ip4f_info;
1255 1.1 thorpej };
1256 1.1 thorpej
1257 1.1 thorpej static TAILQ_HEAD(ip4f_list, ip4_frag) ip4f_list; /* IPv4 fragment cache */
1258 1.1 thorpej
1259 1.1 thorpej #define IP4F_TABSIZE 16 /* IPv4 fragment cache size */
1260 1.1 thorpej
1261 1.1 thorpej
1262 1.1 thorpej static void
1263 1.1 thorpej ip4f_cache(ip, fin)
1264 1.1 thorpej struct ip *ip;
1265 1.1 thorpej struct flowinfo_in *fin;
1266 1.1 thorpej {
1267 1.1 thorpej struct ip4_frag *fp;
1268 1.1 thorpej
1269 1.1 thorpej if (TAILQ_EMPTY(&ip4f_list)) {
1270 1.1 thorpej /* first time call, allocate fragment cache entries. */
1271 1.1 thorpej if (ip4f_init() < 0)
1272 1.1 thorpej /* allocation failed! */
1273 1.1 thorpej return;
1274 1.1 thorpej }
1275 1.1 thorpej
1276 1.1 thorpej fp = ip4f_alloc();
1277 1.1 thorpej fp->ip4f_id = ip->ip_id;
1278 1.6 itojun fp->ip4f_info.fi_proto = ip->ip_p;
1279 1.6 itojun fp->ip4f_info.fi_src.s_addr = ip->ip_src.s_addr;
1280 1.6 itojun fp->ip4f_info.fi_dst.s_addr = ip->ip_dst.s_addr;
1281 1.1 thorpej
1282 1.1 thorpej /* save port numbers */
1283 1.1 thorpej fp->ip4f_info.fi_sport = fin->fi_sport;
1284 1.1 thorpej fp->ip4f_info.fi_dport = fin->fi_dport;
1285 1.1 thorpej fp->ip4f_info.fi_gpi = fin->fi_gpi;
1286 1.1 thorpej }
1287 1.1 thorpej
1288 1.1 thorpej static int
1289 1.1 thorpej ip4f_lookup(ip, fin)
1290 1.1 thorpej struct ip *ip;
1291 1.1 thorpej struct flowinfo_in *fin;
1292 1.1 thorpej {
1293 1.1 thorpej struct ip4_frag *fp;
1294 1.1 thorpej
1295 1.1 thorpej for (fp = TAILQ_FIRST(&ip4f_list); fp != NULL && fp->ip4f_valid;
1296 1.1 thorpej fp = TAILQ_NEXT(fp, ip4f_chain))
1297 1.1 thorpej if (ip->ip_id == fp->ip4f_id &&
1298 1.1 thorpej ip->ip_src.s_addr == fp->ip4f_info.fi_src.s_addr &&
1299 1.1 thorpej ip->ip_dst.s_addr == fp->ip4f_info.fi_dst.s_addr &&
1300 1.1 thorpej ip->ip_p == fp->ip4f_info.fi_proto) {
1301 1.1 thorpej
1302 1.1 thorpej /* found the matching entry */
1303 1.1 thorpej fin->fi_sport = fp->ip4f_info.fi_sport;
1304 1.1 thorpej fin->fi_dport = fp->ip4f_info.fi_dport;
1305 1.1 thorpej fin->fi_gpi = fp->ip4f_info.fi_gpi;
1306 1.1 thorpej
1307 1.1 thorpej if ((ntohs(ip->ip_off) & IP_MF) == 0)
1308 1.1 thorpej /* this is the last fragment,
1309 1.1 thorpej release the entry. */
1310 1.1 thorpej ip4f_free(fp);
1311 1.1 thorpej
1312 1.1 thorpej return (1);
1313 1.1 thorpej }
1314 1.1 thorpej
1315 1.1 thorpej /* no matching entry found */
1316 1.1 thorpej return (0);
1317 1.1 thorpej }
1318 1.1 thorpej
1319 1.1 thorpej static int
1320 1.1 thorpej ip4f_init(void)
1321 1.1 thorpej {
1322 1.1 thorpej struct ip4_frag *fp;
1323 1.1 thorpej int i;
1324 1.8 itojun
1325 1.1 thorpej TAILQ_INIT(&ip4f_list);
1326 1.1 thorpej for (i=0; i<IP4F_TABSIZE; i++) {
1327 1.14 christos fp = malloc(sizeof(struct ip4_frag), M_DEVBUF, M_NOWAIT);
1328 1.1 thorpej if (fp == NULL) {
1329 1.1 thorpej printf("ip4f_init: can't alloc %dth entry!\n", i);
1330 1.1 thorpej if (i == 0)
1331 1.1 thorpej return (-1);
1332 1.1 thorpej return (0);
1333 1.1 thorpej }
1334 1.1 thorpej fp->ip4f_valid = 0;
1335 1.1 thorpej TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1336 1.1 thorpej }
1337 1.1 thorpej return (0);
1338 1.1 thorpej }
1339 1.1 thorpej
1340 1.1 thorpej static struct ip4_frag *
1341 1.1 thorpej ip4f_alloc(void)
1342 1.1 thorpej {
1343 1.1 thorpej struct ip4_frag *fp;
1344 1.1 thorpej
1345 1.1 thorpej /* reclaim an entry at the tail, put it at the head */
1346 1.1 thorpej fp = TAILQ_LAST(&ip4f_list, ip4f_list);
1347 1.1 thorpej TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1348 1.1 thorpej fp->ip4f_valid = 1;
1349 1.1 thorpej TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
1350 1.1 thorpej return (fp);
1351 1.1 thorpej }
1352 1.1 thorpej
1353 1.1 thorpej static void
1354 1.1 thorpej ip4f_free(fp)
1355 1.1 thorpej struct ip4_frag *fp;
1356 1.1 thorpej {
1357 1.1 thorpej TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
1358 1.1 thorpej fp->ip4f_valid = 0;
1359 1.1 thorpej TAILQ_INSERT_TAIL(&ip4f_list, fp, ip4f_chain);
1360 1.1 thorpej }
1361 1.1 thorpej
1362 1.1 thorpej /*
1363 1.1 thorpej * read and write diffserv field in IPv4 or IPv6 header
1364 1.1 thorpej */
1365 1.1 thorpej u_int8_t
1366 1.1 thorpej read_dsfield(m, pktattr)
1367 1.1 thorpej struct mbuf *m;
1368 1.1 thorpej struct altq_pktattr *pktattr;
1369 1.1 thorpej {
1370 1.1 thorpej struct mbuf *m0;
1371 1.1 thorpej u_int8_t ds_field = 0;
1372 1.8 itojun
1373 1.1 thorpej if (pktattr == NULL ||
1374 1.1 thorpej (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
1375 1.1 thorpej return ((u_int8_t)0);
1376 1.1 thorpej
1377 1.1 thorpej /* verify that pattr_hdr is within the mbuf data */
1378 1.1 thorpej for (m0 = m; m0 != NULL; m0 = m0->m_next)
1379 1.1 thorpej if ((pktattr->pattr_hdr >= m0->m_data) &&
1380 1.1 thorpej (pktattr->pattr_hdr < m0->m_data + m0->m_len))
1381 1.1 thorpej break;
1382 1.1 thorpej if (m0 == NULL) {
1383 1.1 thorpej /* ick, pattr_hdr is stale */
1384 1.1 thorpej pktattr->pattr_af = AF_UNSPEC;
1385 1.1 thorpej #ifdef ALTQ_DEBUG
1386 1.1 thorpej printf("read_dsfield: can't locate header!\n");
1387 1.1 thorpej #endif
1388 1.1 thorpej return ((u_int8_t)0);
1389 1.1 thorpej }
1390 1.1 thorpej
1391 1.1 thorpej if (pktattr->pattr_af == AF_INET) {
1392 1.1 thorpej struct ip *ip = (struct ip *)pktattr->pattr_hdr;
1393 1.8 itojun
1394 1.1 thorpej if (ip->ip_v != 4)
1395 1.1 thorpej return ((u_int8_t)0); /* version mismatch! */
1396 1.1 thorpej ds_field = ip->ip_tos;
1397 1.1 thorpej }
1398 1.1 thorpej #ifdef INET6
1399 1.1 thorpej else if (pktattr->pattr_af == AF_INET6) {
1400 1.1 thorpej struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
1401 1.1 thorpej u_int32_t flowlabel;
1402 1.8 itojun
1403 1.1 thorpej flowlabel = ntohl(ip6->ip6_flow);
1404 1.1 thorpej if ((flowlabel >> 28) != 6)
1405 1.1 thorpej return ((u_int8_t)0); /* version mismatch! */
1406 1.1 thorpej ds_field = (flowlabel >> 20) & 0xff;
1407 1.1 thorpej }
1408 1.1 thorpej #endif
1409 1.1 thorpej return (ds_field);
1410 1.1 thorpej }
1411 1.1 thorpej
1412 1.1 thorpej void
1413 1.1 thorpej write_dsfield(m, pktattr, dsfield)
1414 1.1 thorpej struct mbuf *m;
1415 1.1 thorpej struct altq_pktattr *pktattr;
1416 1.1 thorpej u_int8_t dsfield;
1417 1.1 thorpej {
1418 1.1 thorpej struct mbuf *m0;
1419 1.1 thorpej
1420 1.1 thorpej if (pktattr == NULL ||
1421 1.1 thorpej (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
1422 1.1 thorpej return;
1423 1.1 thorpej
1424 1.1 thorpej /* verify that pattr_hdr is within the mbuf data */
1425 1.1 thorpej for (m0 = m; m0 != NULL; m0 = m0->m_next)
1426 1.1 thorpej if ((pktattr->pattr_hdr >= m0->m_data) &&
1427 1.1 thorpej (pktattr->pattr_hdr < m0->m_data + m0->m_len))
1428 1.1 thorpej break;
1429 1.1 thorpej if (m0 == NULL) {
1430 1.1 thorpej /* ick, pattr_hdr is stale */
1431 1.1 thorpej pktattr->pattr_af = AF_UNSPEC;
1432 1.1 thorpej #ifdef ALTQ_DEBUG
1433 1.1 thorpej printf("write_dsfield: can't locate header!\n");
1434 1.1 thorpej #endif
1435 1.1 thorpej return;
1436 1.1 thorpej }
1437 1.1 thorpej
1438 1.1 thorpej if (pktattr->pattr_af == AF_INET) {
1439 1.1 thorpej struct ip *ip = (struct ip *)pktattr->pattr_hdr;
1440 1.1 thorpej u_int8_t old;
1441 1.1 thorpej int32_t sum;
1442 1.1 thorpej
1443 1.1 thorpej if (ip->ip_v != 4)
1444 1.1 thorpej return; /* version mismatch! */
1445 1.1 thorpej old = ip->ip_tos;
1446 1.1 thorpej dsfield |= old & 3; /* leave CU bits */
1447 1.1 thorpej if (old == dsfield)
1448 1.1 thorpej return;
1449 1.1 thorpej ip->ip_tos = dsfield;
1450 1.1 thorpej /*
1451 1.1 thorpej * update checksum (from RFC1624)
1452 1.1 thorpej * HC' = ~(~HC + ~m + m')
1453 1.1 thorpej */
1454 1.1 thorpej sum = ~ntohs(ip->ip_sum) & 0xffff;
1455 1.1 thorpej sum += 0xff00 + (~old & 0xff) + dsfield;
1456 1.1 thorpej sum = (sum >> 16) + (sum & 0xffff);
1457 1.1 thorpej sum += (sum >> 16); /* add carry */
1458 1.8 itojun
1459 1.1 thorpej ip->ip_sum = htons(~sum & 0xffff);
1460 1.1 thorpej }
1461 1.1 thorpej #ifdef INET6
1462 1.1 thorpej else if (pktattr->pattr_af == AF_INET6) {
1463 1.1 thorpej struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
1464 1.1 thorpej u_int32_t flowlabel;
1465 1.1 thorpej
1466 1.1 thorpej flowlabel = ntohl(ip6->ip6_flow);
1467 1.1 thorpej if ((flowlabel >> 28) != 6)
1468 1.1 thorpej return; /* version mismatch! */
1469 1.1 thorpej flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
1470 1.1 thorpej ip6->ip6_flow = htonl(flowlabel);
1471 1.1 thorpej }
1472 1.1 thorpej #endif
1473 1.1 thorpej return;
1474 1.1 thorpej }
1475 1.1 thorpej
1476 1.1 thorpej
1477 1.1 thorpej /*
1478 1.1 thorpej * high resolution clock support taking advantage of a machine dependent
1479 1.1 thorpej * high resolution time counter (e.g., timestamp counter of intel pentium).
1480 1.1 thorpej * we assume
1481 1.1 thorpej * - 64-bit-long monotonically-increasing counter
1482 1.1 thorpej * - frequency range is 100M-4GHz (CPU speed)
1483 1.1 thorpej */
1484 1.1 thorpej u_int32_t machclk_freq = 0;
1485 1.1 thorpej u_int32_t machclk_per_tick = 0;
1486 1.1 thorpej
1487 1.1 thorpej #if (defined(__i386__) || defined(__alpha__)) && !defined(ALTQ_NOPCC)
1488 1.8 itojun
1489 1.8 itojun #if defined(__FreeBSD__) && defined(SMP)
1490 1.8 itojun #error SMP system! use ALTQ_NOPCC option.
1491 1.8 itojun #endif
1492 1.8 itojun
1493 1.8 itojun #ifdef __alpha__
1494 1.1 thorpej #ifdef __FreeBSD__
1495 1.11 wiz extern u_int32_t cycles_per_sec; /* alpha CPU clock frequency */
1496 1.8 itojun #elif defined(__NetBSD__) || defined(__OpenBSD__)
1497 1.11 wiz extern u_int64_t cycles_per_usec; /* alpha CPU clock frequency */
1498 1.1 thorpej #endif
1499 1.8 itojun #endif /* __alpha__ */
1500 1.8 itojun
1501 1.1 thorpej void
1502 1.12 perry init_machclk(void)
1503 1.1 thorpej {
1504 1.8 itojun /* sanity check */
1505 1.8 itojun #ifdef __i386__
1506 1.8 itojun /* check if TSC is available */
1507 1.8 itojun if ((cpu_feature & CPUID_TSC) == 0) {
1508 1.8 itojun printf("altq: TSC isn't available! use ALTQ_NOPCC option.\n");
1509 1.8 itojun return;
1510 1.8 itojun }
1511 1.8 itojun #endif
1512 1.8 itojun
1513 1.8 itojun /*
1514 1.8 itojun * if the clock frequency (of Pentium TSC or Alpha PCC) is
1515 1.8 itojun * accessible, just use it.
1516 1.8 itojun */
1517 1.8 itojun #ifdef __i386__
1518 1.8 itojun #ifdef __FreeBSD__
1519 1.1 thorpej #if (__FreeBSD_version > 300000)
1520 1.1 thorpej machclk_freq = tsc_freq;
1521 1.1 thorpej #else
1522 1.1 thorpej machclk_freq = i586_ctr_freq;
1523 1.1 thorpej #endif
1524 1.8 itojun #elif defined(__NetBSD__)
1525 1.9 jdolecek machclk_freq = (u_int32_t)curcpu()->ci_tsc_freq;
1526 1.8 itojun #elif defined(__OpenBSD__)
1527 1.8 itojun machclk_freq = pentium_mhz * 1000000;
1528 1.8 itojun #endif
1529 1.1 thorpej #elif defined(__alpha__)
1530 1.8 itojun #ifdef __FreeBSD__
1531 1.1 thorpej machclk_freq = cycles_per_sec;
1532 1.8 itojun #elif defined(__NetBSD__) || defined(__OpenBSD__)
1533 1.8 itojun machclk_freq = (u_int32_t)(cycles_per_usec * 1000000);
1534 1.8 itojun #endif
1535 1.1 thorpej #endif /* __alpha__ */
1536 1.8 itojun
1537 1.8 itojun /*
1538 1.8 itojun * if we don't know the clock frequency, measure it.
1539 1.8 itojun */
1540 1.8 itojun if (machclk_freq == 0) {
1541 1.8 itojun static int wait;
1542 1.8 itojun struct timeval tv_start, tv_end;
1543 1.8 itojun u_int64_t start, end, diff;
1544 1.8 itojun int timo;
1545 1.8 itojun
1546 1.8 itojun microtime(&tv_start);
1547 1.8 itojun start = read_machclk();
1548 1.8 itojun timo = hz; /* 1 sec */
1549 1.8 itojun (void)tsleep(&wait, PWAIT | PCATCH, "init_machclk", timo);
1550 1.8 itojun microtime(&tv_end);
1551 1.8 itojun end = read_machclk();
1552 1.8 itojun diff = (u_int64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
1553 1.8 itojun + tv_end.tv_usec - tv_start.tv_usec;
1554 1.8 itojun if (diff != 0)
1555 1.8 itojun machclk_freq = (u_int)((end - start) * 1000000 / diff);
1556 1.8 itojun }
1557 1.8 itojun
1558 1.1 thorpej machclk_per_tick = machclk_freq / hz;
1559 1.1 thorpej
1560 1.8 itojun #ifdef ALTQ_DEBUG
1561 1.1 thorpej printf("altq: CPU clock: %uHz\n", machclk_freq);
1562 1.8 itojun #endif
1563 1.1 thorpej }
1564 1.8 itojun
1565 1.1 thorpej #ifdef __alpha__
1566 1.1 thorpej /*
1567 1.1 thorpej * make a 64bit counter value out of the 32bit alpha processor cycle counter.
1568 1.1 thorpej * read_machclk must be called within a half of its wrap-around cycle
1569 1.11 wiz * (about 5 sec for 400MHz CPU) to properly detect a counter wrap-around.
1570 1.1 thorpej * tbr_timeout calls read_machclk once a second.
1571 1.1 thorpej */
1572 1.1 thorpej u_int64_t
1573 1.1 thorpej read_machclk(void)
1574 1.1 thorpej {
1575 1.1 thorpej static u_int32_t last_pcc, upper;
1576 1.1 thorpej u_int32_t pcc;
1577 1.1 thorpej
1578 1.1 thorpej pcc = (u_int32_t)alpha_rpcc();
1579 1.1 thorpej if (pcc <= last_pcc)
1580 1.1 thorpej upper++;
1581 1.1 thorpej last_pcc = pcc;
1582 1.1 thorpej return (((u_int64_t)upper << 32) + pcc);
1583 1.1 thorpej }
1584 1.1 thorpej #endif /* __alpha__ */
1585 1.1 thorpej #else /* !i386 && !alpha */
1586 1.1 thorpej /* use microtime() for now */
1587 1.1 thorpej void
1588 1.8 itojun init_machclk(void)
1589 1.1 thorpej {
1590 1.1 thorpej machclk_freq = 1000000 << MACHCLK_SHIFT;
1591 1.1 thorpej machclk_per_tick = machclk_freq / hz;
1592 1.11 wiz printf("altq: emulate %uHz CPU clock\n", machclk_freq);
1593 1.1 thorpej }
1594 1.1 thorpej #endif /* !i386 && !alpha */
1595