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