/
altq_subr.c
1661 lines (1454 loc) · 37.1 KB
/
altq_subr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: altq_subr.c,v 1.28 2009/01/04 18:41:36 pooka Exp $ */
/* $KAME: altq_subr.c,v 1.24 2005/04/13 03:44:25 suz Exp $ */
/*
* Copyright (C) 1997-2003
* Sony Computer Science Laboratories Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: altq_subr.c,v 1.28 2009/01/04 18:41:36 pooka Exp $");
#ifdef _KERNEL_OPT
#include "opt_altq.h"
#include "opt_inet.h"
#include "pf.h"
#endif
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/sysctl.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifdef INET6
#include <netinet/ip6.h>
#endif
#include <netinet/tcp.h>
#include <netinet/udp.h>
#if NPF > 0
#include <net/pfvar.h>
#endif
#include <altq/altq.h>
#ifdef ALTQ3_COMPAT
#include <altq/altq_conf.h>
#endif
/*
* internal function prototypes
*/
static void tbr_timeout(void *);
int (*altq_input)(struct mbuf *, int) = NULL;
static int tbr_timer = 0; /* token bucket regulator timer */
static struct callout tbr_callout;
#ifdef ALTQ3_CLFIER_COMPAT
static int extract_ports4(struct mbuf *, struct ip *, struct flowinfo_in *);
#ifdef INET6
static int extract_ports6(struct mbuf *, struct ip6_hdr *,
struct flowinfo_in6 *);
#endif
static int apply_filter4(u_int32_t, struct flow_filter *,
struct flowinfo_in *);
static int apply_ppfilter4(u_int32_t, struct flow_filter *,
struct flowinfo_in *);
#ifdef INET6
static int apply_filter6(u_int32_t, struct flow_filter6 *,
struct flowinfo_in6 *);
#endif
static int apply_tosfilter4(u_int32_t, struct flow_filter *,
struct flowinfo_in *);
static u_long get_filt_handle(struct acc_classifier *, int);
static struct acc_filter *filth_to_filtp(struct acc_classifier *, u_long);
static u_int32_t filt2fibmask(struct flow_filter *);
static void ip4f_cache(struct ip *, struct flowinfo_in *);
static int ip4f_lookup(struct ip *, struct flowinfo_in *);
static int ip4f_init(void);
static struct ip4_frag *ip4f_alloc(void);
static void ip4f_free(struct ip4_frag *);
#endif /* ALTQ3_CLFIER_COMPAT */
/*
* alternate queueing support routines
*/
/* look up the queue state by the interface name and the queueing type. */
void *
altq_lookup(char *name, int type)
{
struct ifnet *ifp;
if ((ifp = ifunit(name)) != NULL) {
if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
return (ifp->if_snd.altq_disc);
}
return NULL;
}
int
altq_attach(struct ifaltq *ifq, int type, void *discipline,
int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *),
struct mbuf *(*dequeue)(struct ifaltq *, int),
int (*request)(struct ifaltq *, int, void *),
void *clfier, void *(*classify)(void *, struct mbuf *, int))
{
if (!ALTQ_IS_READY(ifq))
return ENXIO;
#ifdef ALTQ3_COMPAT
/*
* pfaltq can override the existing discipline, but altq3 cannot.
* check these if clfier is not NULL (which implies altq3).
*/
if (clfier != NULL) {
if (ALTQ_IS_ENABLED(ifq))
return EBUSY;
if (ALTQ_IS_ATTACHED(ifq))
return EEXIST;
}
#endif
ifq->altq_type = type;
ifq->altq_disc = discipline;
ifq->altq_enqueue = enqueue;
ifq->altq_dequeue = dequeue;
ifq->altq_request = request;
ifq->altq_clfier = clfier;
ifq->altq_classify = classify;
ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
#ifdef ALTQ3_COMPAT
#ifdef ALTQ_KLD
altq_module_incref(type);
#endif
#endif
return 0;
}
int
altq_detach(struct ifaltq *ifq)
{
if (!ALTQ_IS_READY(ifq))
return ENXIO;
if (ALTQ_IS_ENABLED(ifq))
return EBUSY;
if (!ALTQ_IS_ATTACHED(ifq))
return (0);
#ifdef ALTQ3_COMPAT
#ifdef ALTQ_KLD
altq_module_declref(ifq->altq_type);
#endif
#endif
ifq->altq_type = ALTQT_NONE;
ifq->altq_disc = NULL;
ifq->altq_enqueue = NULL;
ifq->altq_dequeue = NULL;
ifq->altq_request = NULL;
ifq->altq_clfier = NULL;
ifq->altq_classify = NULL;
ifq->altq_flags &= ALTQF_CANTCHANGE;
return 0;
}
int
altq_enable(struct ifaltq *ifq)
{
int s;
if (!ALTQ_IS_READY(ifq))
return ENXIO;
if (ALTQ_IS_ENABLED(ifq))
return 0;
s = splnet();
IFQ_PURGE(ifq);
ASSERT(ifq->ifq_len == 0);
ifq->altq_flags |= ALTQF_ENABLED;
if (ifq->altq_clfier != NULL)
ifq->altq_flags |= ALTQF_CLASSIFY;
splx(s);
return 0;
}
int
altq_disable(struct ifaltq *ifq)
{
int s;
if (!ALTQ_IS_ENABLED(ifq))
return 0;
s = splnet();
IFQ_PURGE(ifq);
ASSERT(ifq->ifq_len == 0);
ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
splx(s);
return 0;
}
#ifdef ALTQ_DEBUG
void
altq_assert(const char *file, int line, const char *failedexpr)
{
(void)printf("altq assertion \"%s\" failed: file \"%s\", line %d\n",
failedexpr, file, line);
panic("altq assertion");
/* NOTREACHED */
}
#endif
/*
* internal representation of token bucket parameters
* rate: byte_per_unittime << 32
* (((bits_per_sec) / 8) << 32) / machclk_freq
* depth: byte << 32
*
*/
#define TBR_SHIFT 32
#define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
#define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
struct mbuf *
tbr_dequeue(struct ifaltq *ifq, int op)
{
struct tb_regulator *tbr;
struct mbuf *m;
int64_t interval;
u_int64_t now;
tbr = ifq->altq_tbr;
if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
/* if this is a remove after poll, bypass tbr check */
} else {
/* update token only when it is negative */
if (tbr->tbr_token <= 0) {
now = read_machclk();
interval = now - tbr->tbr_last;
if (interval >= tbr->tbr_filluptime)
tbr->tbr_token = tbr->tbr_depth;
else {
tbr->tbr_token += interval * tbr->tbr_rate;
if (tbr->tbr_token > tbr->tbr_depth)
tbr->tbr_token = tbr->tbr_depth;
}
tbr->tbr_last = now;
}
/* if token is still negative, don't allow dequeue */
if (tbr->tbr_token <= 0)
return (NULL);
}
if (ALTQ_IS_ENABLED(ifq))
m = (*ifq->altq_dequeue)(ifq, op);
else {
if (op == ALTDQ_POLL)
IF_POLL(ifq, m);
else
IF_DEQUEUE(ifq, m);
}
if (m != NULL && op == ALTDQ_REMOVE)
tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
tbr->tbr_lastop = op;
return (m);
}
/*
* set a token bucket regulator.
* if the specified rate is zero, the token bucket regulator is deleted.
*/
int
tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
{
struct tb_regulator *tbr, *otbr;
if (machclk_freq == 0)
init_machclk();
if (machclk_freq == 0) {
printf("tbr_set: no CPU clock available!\n");
return (ENXIO);
}
if (profile->rate == 0) {
/* delete this tbr */
if ((tbr = ifq->altq_tbr) == NULL)
return (ENOENT);
ifq->altq_tbr = NULL;
free(tbr, M_DEVBUF);
return (0);
}
tbr = malloc(sizeof(struct tb_regulator), M_DEVBUF, M_WAITOK|M_ZERO);
if (tbr == NULL)
return (ENOMEM);
tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
tbr->tbr_depth = TBR_SCALE(profile->depth);
if (tbr->tbr_rate > 0)
tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
else
tbr->tbr_filluptime = 0xffffffffffffffffLL;
tbr->tbr_token = tbr->tbr_depth;
tbr->tbr_last = read_machclk();
tbr->tbr_lastop = ALTDQ_REMOVE;
otbr = ifq->altq_tbr;
ifq->altq_tbr = tbr; /* set the new tbr */
if (otbr != NULL) {
free(otbr, M_DEVBUF);
} else {
if (tbr_timer == 0) {
CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
tbr_timer = 1;
}
}
return (0);
}
/*
* tbr_timeout goes through the interface list, and kicks the drivers
* if necessary.
*/
static void
tbr_timeout(void *arg)
{
struct ifnet *ifp;
int active, s;
active = 0;
s = splnet();
for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
if (!TBR_IS_ENABLED(&ifp->if_snd))
continue;
active++;
if (!IFQ_IS_EMPTY(&ifp->if_snd) && ifp->if_start != NULL)
(*ifp->if_start)(ifp);
}
splx(s);
if (active > 0)
CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
else
tbr_timer = 0; /* don't need tbr_timer anymore */
}
/*
* get token bucket regulator profile
*/
int
tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
{
struct tb_regulator *tbr;
if ((tbr = ifq->altq_tbr) == NULL) {
profile->rate = 0;
profile->depth = 0;
} else {
profile->rate =
(u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
}
return (0);
}
#if NPF > 0
/*
* attach a discipline to the interface. if one already exists, it is
* overridden.
*/
int
altq_pfattach(struct pf_altq *a)
{
int error = 0;
switch (a->scheduler) {
case ALTQT_NONE:
break;
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_pfattach(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_pfattach(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_pfattach(a);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
/*
* detach a discipline from the interface.
* it is possible that the discipline was already overridden by another
* discipline.
*/
int
altq_pfdetach(struct pf_altq *a)
{
struct ifnet *ifp;
int s, error = 0;
if ((ifp = ifunit(a->ifname)) == NULL)
return (EINVAL);
/* if this discipline is no longer referenced, just return */
if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
return (0);
s = splnet();
if (ALTQ_IS_ENABLED(&ifp->if_snd))
error = altq_disable(&ifp->if_snd);
if (error == 0)
error = altq_detach(&ifp->if_snd);
splx(s);
return (error);
}
/*
* add a discipline or a queue
*/
int
altq_add(struct pf_altq *a)
{
int error = 0;
if (a->qname[0] != 0)
return (altq_add_queue(a));
if (machclk_freq == 0)
init_machclk();
if (machclk_freq == 0)
panic("altq_add: no CPU clock");
switch (a->scheduler) {
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_add_altq(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_add_altq(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_add_altq(a);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
/*
* remove a discipline or a queue
*/
int
altq_remove(struct pf_altq *a)
{
int error = 0;
if (a->qname[0] != 0)
return (altq_remove_queue(a));
switch (a->scheduler) {
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_remove_altq(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_remove_altq(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_remove_altq(a);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
/*
* add a queue to the discipline
*/
int
altq_add_queue(struct pf_altq *a)
{
int error = 0;
switch (a->scheduler) {
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_add_queue(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_add_queue(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_add_queue(a);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
/*
* remove a queue from the discipline
*/
int
altq_remove_queue(struct pf_altq *a)
{
int error = 0;
switch (a->scheduler) {
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_remove_queue(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_remove_queue(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_remove_queue(a);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
/*
* get queue statistics
*/
int
altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
{
int error = 0;
switch (a->scheduler) {
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_getqstats(a, ubuf, nbytes);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_getqstats(a, ubuf, nbytes);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_getqstats(a, ubuf, nbytes);
break;
#endif
default:
error = ENXIO;
}
return (error);
}
#endif /* NPF > 0 */
/*
* read and write diffserv field in IPv4 or IPv6 header
*/
u_int8_t
read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
{
struct mbuf *m0;
u_int8_t ds_field = 0;
if (pktattr == NULL ||
(pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
return ((u_int8_t)0);
/* verify that pattr_hdr is within the mbuf data */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((char *)pktattr->pattr_hdr >= m0->m_data) &&
((char *)pktattr->pattr_hdr < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
/* ick, pattr_hdr is stale */
pktattr->pattr_af = AF_UNSPEC;
#ifdef ALTQ_DEBUG
printf("read_dsfield: can't locate header!\n");
#endif
return ((u_int8_t)0);
}
if (pktattr->pattr_af == AF_INET) {
struct ip *ip = (struct ip *)pktattr->pattr_hdr;
if (ip->ip_v != 4)
return ((u_int8_t)0); /* version mismatch! */
ds_field = ip->ip_tos;
}
#ifdef INET6
else if (pktattr->pattr_af == AF_INET6) {
struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
u_int32_t flowlabel;
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6)
return ((u_int8_t)0); /* version mismatch! */
ds_field = (flowlabel >> 20) & 0xff;
}
#endif
return (ds_field);
}
void
write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, u_int8_t dsfield)
{
struct mbuf *m0;
if (pktattr == NULL ||
(pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
return;
/* verify that pattr_hdr is within the mbuf data */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((char *)pktattr->pattr_hdr >= m0->m_data) &&
((char *)pktattr->pattr_hdr < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
/* ick, pattr_hdr is stale */
pktattr->pattr_af = AF_UNSPEC;
#ifdef ALTQ_DEBUG
printf("write_dsfield: can't locate header!\n");
#endif
return;
}
if (pktattr->pattr_af == AF_INET) {
struct ip *ip = (struct ip *)pktattr->pattr_hdr;
u_int8_t old;
int32_t sum;
if (ip->ip_v != 4)
return; /* version mismatch! */
old = ip->ip_tos;
dsfield |= old & 3; /* leave CU bits */
if (old == dsfield)
return;
ip->ip_tos = dsfield;
/*
* update checksum (from RFC1624)
* HC' = ~(~HC + ~m + m')
*/
sum = ~ntohs(ip->ip_sum) & 0xffff;
sum += 0xff00 + (~old & 0xff) + dsfield;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16); /* add carry */
ip->ip_sum = htons(~sum & 0xffff);
}
#ifdef INET6
else if (pktattr->pattr_af == AF_INET6) {
struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
u_int32_t flowlabel;
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6)
return; /* version mismatch! */
flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
ip6->ip6_flow = htonl(flowlabel);
}
#endif
return;
}
#define BINTIME_SHIFT 2
u_int32_t machclk_freq = 0;
u_int32_t machclk_per_tick = 0;
void
init_machclk(void)
{
callout_init(&tbr_callout, 0);
/*
* Always emulate 1GiHz counter using bintime(9)
* since it has enough resolution via timecounter(9).
* Using machine dependent cpu_counter() is not MP safe
* and it won't work even on UP with Speedstep etc.
*/
machclk_freq = 1024 * 1024 * 1024; /* 2^30 to emulate ~1GHz */
machclk_per_tick = machclk_freq / hz;
#ifdef ALTQ_DEBUG
printf("altq: emulate %uHz CPU clock\n", machclk_freq);
#endif
}
u_int64_t
read_machclk(void)
{
struct bintime bt;
u_int64_t val;
binuptime(&bt);
val = (((u_int64_t)bt.sec << 32) + (bt.frac >> 32)) >> BINTIME_SHIFT;
return (val);
}
#ifdef ALTQ3_CLFIER_COMPAT
#ifndef IPPROTO_ESP
#define IPPROTO_ESP 50 /* encapsulating security payload */
#endif
#ifndef IPPROTO_AH
#define IPPROTO_AH 51 /* authentication header */
#endif
/*
* extract flow information from a given packet.
* filt_mask shows flowinfo fields required.
* we assume the ip header is in one mbuf, and addresses and ports are
* in network byte order.
*/
int
altq_extractflow(struct mbuf *m, int af, struct flowinfo *flow,
u_int32_t filt_bmask)
{
switch (af) {
case PF_INET: {
struct flowinfo_in *fin;
struct ip *ip;
ip = mtod(m, struct ip *);
if (ip->ip_v != 4)
break;
fin = (struct flowinfo_in *)flow;
fin->fi_len = sizeof(struct flowinfo_in);
fin->fi_family = AF_INET;
fin->fi_proto = ip->ip_p;
fin->fi_tos = ip->ip_tos;
fin->fi_src.s_addr = ip->ip_src.s_addr;
fin->fi_dst.s_addr = ip->ip_dst.s_addr;
if (filt_bmask & FIMB4_PORTS)
/* if port info is required, extract port numbers */
extract_ports4(m, ip, fin);
else {
fin->fi_sport = 0;
fin->fi_dport = 0;
fin->fi_gpi = 0;
}
return (1);
}
#ifdef INET6
case PF_INET6: {
struct flowinfo_in6 *fin6;
struct ip6_hdr *ip6;
ip6 = mtod(m, struct ip6_hdr *);
/* should we check the ip version? */
fin6 = (struct flowinfo_in6 *)flow;
fin6->fi6_len = sizeof(struct flowinfo_in6);
fin6->fi6_family = AF_INET6;
fin6->fi6_proto = ip6->ip6_nxt;
fin6->fi6_tclass = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
fin6->fi6_flowlabel = ip6->ip6_flow & htonl(0x000fffff);
fin6->fi6_src = ip6->ip6_src;
fin6->fi6_dst = ip6->ip6_dst;
if ((filt_bmask & FIMB6_PORTS) ||
((filt_bmask & FIMB6_PROTO)
&& ip6->ip6_nxt > IPPROTO_IPV6))
/*
* if port info is required, or proto is required
* but there are option headers, extract port
* and protocol numbers.
*/
extract_ports6(m, ip6, fin6);
else {
fin6->fi6_sport = 0;
fin6->fi6_dport = 0;
fin6->fi6_gpi = 0;
}
return (1);
}
#endif /* INET6 */
default:
break;
}
/* failed */
flow->fi_len = sizeof(struct flowinfo);
flow->fi_family = AF_UNSPEC;
return (0);
}
/*
* helper routine to extract port numbers
*/
/* structure for ipsec and ipv6 option header template */
struct _opt6 {
u_int8_t opt6_nxt; /* next header */
u_int8_t opt6_hlen; /* header extension length */
u_int16_t _pad;
u_int32_t ah_spi; /* security parameter index
for authentication header */
};
/*
* extract port numbers from a ipv4 packet.
*/
static int
extract_ports4(struct mbuf *m, struct ip *ip, struct flowinfo_in *fin)
{
struct mbuf *m0;
u_short ip_off;
u_int8_t proto;
int off;
fin->fi_sport = 0;
fin->fi_dport = 0;
fin->fi_gpi = 0;
ip_off = ntohs(ip->ip_off);
/* if it is a fragment, try cached fragment info */
if (ip_off & IP_OFFMASK) {
ip4f_lookup(ip, fin);
return (1);
}
/* locate the mbuf containing the protocol header */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((char *)ip >= m0->m_data) &&
((char *)ip < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
#ifdef ALTQ_DEBUG
printf("extract_ports4: can't locate header! ip=%p\n", ip);
#endif
return (0);
}
off = ((char *)ip - m0->m_data) + (ip->ip_hl << 2);
proto = ip->ip_p;
#ifdef ALTQ_IPSEC
again:
#endif
while (off >= m0->m_len) {
off -= m0->m_len;
m0 = m0->m_next;
if (m0 == NULL)
return (0); /* bogus ip_hl! */
}
if (m0->m_len < off + 4)
return (0);
switch (proto) {
case IPPROTO_TCP:
case IPPROTO_UDP: {
struct udphdr *udp;
udp = (struct udphdr *)(mtod(m0, char *) + off);
fin->fi_sport = udp->uh_sport;
fin->fi_dport = udp->uh_dport;
fin->fi_proto = proto;
}
break;
#ifdef ALTQ_IPSEC
case IPPROTO_ESP:
if (fin->fi_gpi == 0){
u_int32_t *gpi;
gpi = (u_int32_t *)(mtod(m0, char *) + off);
fin->fi_gpi = *gpi;
}
fin->fi_proto = proto;
break;
case IPPROTO_AH: {
/* get next header and header length */
struct _opt6 *opt6;
opt6 = (struct _opt6 *)(mtod(m0, char *) + off);
proto = opt6->opt6_nxt;
off += 8 + (opt6->opt6_hlen * 4);
if (fin->fi_gpi == 0 && m0->m_len >= off + 8)
fin->fi_gpi = opt6->ah_spi;
}
/* goto the next header */
goto again;
#endif /* ALTQ_IPSEC */
default:
fin->fi_proto = proto;
return (0);
}
/* if this is a first fragment, cache it. */
if (ip_off & IP_MF)
ip4f_cache(ip, fin);
return (1);
}
#ifdef INET6
static int
extract_ports6(struct mbuf *m, struct ip6_hdr *ip6, struct flowinfo_in6 *fin6)
{
struct mbuf *m0;
int off;
u_int8_t proto;
fin6->fi6_gpi = 0;
fin6->fi6_sport = 0;
fin6->fi6_dport = 0;
/* locate the mbuf containing the protocol header */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((char *)ip6 >= m0->m_data) &&
((char *)ip6 < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
#ifdef ALTQ_DEBUG
printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
#endif
return (0);
}
off = ((char *)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
proto = ip6->ip6_nxt;
do {
while (off >= m0->m_len) {