]>
Commit | Line | Data |
---|---|---|
1 | /* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8 | |
2 | * | |
3 | * (C) 2002 by Harald Welte <laforge@netfilter.org> | |
4 | * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * See RFC2474 for a description of the DSCP field within the IP Header. | |
11 | */ | |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
13 | #include <linux/module.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/ip.h> | |
16 | #include <linux/ipv6.h> | |
17 | #include <net/dsfield.h> | |
18 | ||
19 | #include <linux/netfilter/x_tables.h> | |
20 | #include <linux/netfilter/xt_DSCP.h> | |
21 | ||
22 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
23 | MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification"); | |
24 | MODULE_LICENSE("GPL"); | |
25 | MODULE_ALIAS("ipt_DSCP"); | |
26 | MODULE_ALIAS("ip6t_DSCP"); | |
27 | MODULE_ALIAS("ipt_TOS"); | |
28 | MODULE_ALIAS("ip6t_TOS"); | |
29 | ||
30 | static unsigned int | |
31 | dscp_tg(struct sk_buff *skb, const struct xt_action_param *par) | |
32 | { | |
33 | const struct xt_DSCP_info *dinfo = par->targinfo; | |
34 | u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT; | |
35 | ||
36 | if (dscp != dinfo->dscp) { | |
37 | if (!skb_make_writable(skb, sizeof(struct iphdr))) | |
38 | return NF_DROP; | |
39 | ||
40 | ipv4_change_dsfield(ip_hdr(skb), | |
41 | (__force __u8)(~XT_DSCP_MASK), | |
42 | dinfo->dscp << XT_DSCP_SHIFT); | |
43 | ||
44 | } | |
45 | return XT_CONTINUE; | |
46 | } | |
47 | ||
48 | static unsigned int | |
49 | dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par) | |
50 | { | |
51 | const struct xt_DSCP_info *dinfo = par->targinfo; | |
52 | u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT; | |
53 | ||
54 | if (dscp != dinfo->dscp) { | |
55 | if (!skb_make_writable(skb, sizeof(struct ipv6hdr))) | |
56 | return NF_DROP; | |
57 | ||
58 | ipv6_change_dsfield(ipv6_hdr(skb), | |
59 | (__force __u8)(~XT_DSCP_MASK), | |
60 | dinfo->dscp << XT_DSCP_SHIFT); | |
61 | } | |
62 | return XT_CONTINUE; | |
63 | } | |
64 | ||
65 | static int dscp_tg_check(const struct xt_tgchk_param *par) | |
66 | { | |
67 | const struct xt_DSCP_info *info = par->targinfo; | |
68 | ||
69 | if (info->dscp > XT_DSCP_MAX) { | |
70 | pr_info("dscp %x out of range\n", info->dscp); | |
71 | return -EDOM; | |
72 | } | |
73 | return 0; | |
74 | } | |
75 | ||
76 | static unsigned int | |
77 | tos_tg(struct sk_buff *skb, const struct xt_action_param *par) | |
78 | { | |
79 | const struct xt_tos_target_info *info = par->targinfo; | |
80 | struct iphdr *iph = ip_hdr(skb); | |
81 | u_int8_t orig, nv; | |
82 | ||
83 | orig = ipv4_get_dsfield(iph); | |
84 | nv = (orig & ~info->tos_mask) ^ info->tos_value; | |
85 | ||
86 | if (orig != nv) { | |
87 | if (!skb_make_writable(skb, sizeof(struct iphdr))) | |
88 | return NF_DROP; | |
89 | iph = ip_hdr(skb); | |
90 | ipv4_change_dsfield(iph, 0, nv); | |
91 | } | |
92 | ||
93 | return XT_CONTINUE; | |
94 | } | |
95 | ||
96 | static unsigned int | |
97 | tos_tg6(struct sk_buff *skb, const struct xt_action_param *par) | |
98 | { | |
99 | const struct xt_tos_target_info *info = par->targinfo; | |
100 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
101 | u_int8_t orig, nv; | |
102 | ||
103 | orig = ipv6_get_dsfield(iph); | |
104 | nv = (orig & ~info->tos_mask) ^ info->tos_value; | |
105 | ||
106 | if (orig != nv) { | |
107 | if (!skb_make_writable(skb, sizeof(struct iphdr))) | |
108 | return NF_DROP; | |
109 | iph = ipv6_hdr(skb); | |
110 | ipv6_change_dsfield(iph, 0, nv); | |
111 | } | |
112 | ||
113 | return XT_CONTINUE; | |
114 | } | |
115 | ||
116 | static struct xt_target dscp_tg_reg[] __read_mostly = { | |
117 | { | |
118 | .name = "DSCP", | |
119 | .family = NFPROTO_IPV4, | |
120 | .checkentry = dscp_tg_check, | |
121 | .target = dscp_tg, | |
122 | .targetsize = sizeof(struct xt_DSCP_info), | |
123 | .table = "mangle", | |
124 | .me = THIS_MODULE, | |
125 | }, | |
126 | { | |
127 | .name = "DSCP", | |
128 | .family = NFPROTO_IPV6, | |
129 | .checkentry = dscp_tg_check, | |
130 | .target = dscp_tg6, | |
131 | .targetsize = sizeof(struct xt_DSCP_info), | |
132 | .table = "mangle", | |
133 | .me = THIS_MODULE, | |
134 | }, | |
135 | { | |
136 | .name = "TOS", | |
137 | .revision = 1, | |
138 | .family = NFPROTO_IPV4, | |
139 | .table = "mangle", | |
140 | .target = tos_tg, | |
141 | .targetsize = sizeof(struct xt_tos_target_info), | |
142 | .me = THIS_MODULE, | |
143 | }, | |
144 | { | |
145 | .name = "TOS", | |
146 | .revision = 1, | |
147 | .family = NFPROTO_IPV6, | |
148 | .table = "mangle", | |
149 | .target = tos_tg6, | |
150 | .targetsize = sizeof(struct xt_tos_target_info), | |
151 | .me = THIS_MODULE, | |
152 | }, | |
153 | }; | |
154 | ||
155 | static int __init dscp_tg_init(void) | |
156 | { | |
157 | return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg)); | |
158 | } | |
159 | ||
160 | static void __exit dscp_tg_exit(void) | |
161 | { | |
162 | xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg)); | |
163 | } | |
164 | ||
165 | module_init(dscp_tg_init); | |
166 | module_exit(dscp_tg_exit); |