]>
Commit | Line | Data |
---|---|---|
a468701d YK |
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. | |
a468701d | 11 | */ |
8bee4bad | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
a468701d YK |
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>"); | |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification"); |
a468701d YK |
24 | MODULE_LICENSE("GPL"); |
25 | MODULE_ALIAS("ipt_DSCP"); | |
26 | MODULE_ALIAS("ip6t_DSCP"); | |
c9fd4968 | 27 | MODULE_ALIAS("ipt_TOS"); |
5c350e5a | 28 | MODULE_ALIAS("ip6t_TOS"); |
a468701d | 29 | |
d3c5ee6d | 30 | static unsigned int |
4b560b44 | 31 | dscp_tg(struct sk_buff *skb, const struct xt_action_param *par) |
a468701d | 32 | { |
7eb35586 | 33 | const struct xt_DSCP_info *dinfo = par->targinfo; |
3db05fea | 34 | u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT; |
a468701d YK |
35 | |
36 | if (dscp != dinfo->dscp) { | |
3db05fea | 37 | if (!skb_make_writable(skb, sizeof(struct iphdr))) |
a468701d YK |
38 | return NF_DROP; |
39 | ||
56768644 FW |
40 | ipv4_change_dsfield(ip_hdr(skb), |
41 | (__force __u8)(~XT_DSCP_MASK), | |
a468701d YK |
42 | dinfo->dscp << XT_DSCP_SHIFT); |
43 | ||
44 | } | |
45 | return XT_CONTINUE; | |
46 | } | |
47 | ||
d3c5ee6d | 48 | static unsigned int |
4b560b44 | 49 | dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
a468701d | 50 | { |
7eb35586 | 51 | const struct xt_DSCP_info *dinfo = par->targinfo; |
3db05fea | 52 | u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT; |
a468701d YK |
53 | |
54 | if (dscp != dinfo->dscp) { | |
3db05fea | 55 | if (!skb_make_writable(skb, sizeof(struct ipv6hdr))) |
a468701d YK |
56 | return NF_DROP; |
57 | ||
56768644 FW |
58 | ipv6_change_dsfield(ipv6_hdr(skb), |
59 | (__force __u8)(~XT_DSCP_MASK), | |
a468701d YK |
60 | dinfo->dscp << XT_DSCP_SHIFT); |
61 | } | |
62 | return XT_CONTINUE; | |
63 | } | |
64 | ||
135367b8 | 65 | static int dscp_tg_check(const struct xt_tgchk_param *par) |
a468701d | 66 | { |
af5d6dc2 | 67 | const struct xt_DSCP_info *info = par->targinfo; |
a468701d | 68 | |
af5d6dc2 | 69 | if (info->dscp > XT_DSCP_MAX) { |
8bee4bad | 70 | pr_info("dscp %x out of range\n", info->dscp); |
4a5a5c73 | 71 | return -EDOM; |
a468701d | 72 | } |
d6b00a53 | 73 | return 0; |
a468701d YK |
74 | } |
75 | ||
5c350e5a | 76 | static unsigned int |
4b560b44 | 77 | tos_tg(struct sk_buff *skb, const struct xt_action_param *par) |
5c350e5a | 78 | { |
7eb35586 | 79 | const struct xt_tos_target_info *info = par->targinfo; |
5c350e5a JE |
80 | struct iphdr *iph = ip_hdr(skb); |
81 | u_int8_t orig, nv; | |
82 | ||
83 | orig = ipv4_get_dsfield(iph); | |
9bb268ed | 84 | nv = (orig & ~info->tos_mask) ^ info->tos_value; |
5c350e5a JE |
85 | |
86 | if (orig != nv) { | |
87 | if (!skb_make_writable(skb, sizeof(struct iphdr))) | |
88 | return NF_DROP; | |
89 | iph = ip_hdr(skb); | |
cdfe8b97 | 90 | ipv4_change_dsfield(iph, 0, nv); |
5c350e5a JE |
91 | } |
92 | ||
93 | return XT_CONTINUE; | |
94 | } | |
95 | ||
96 | static unsigned int | |
4b560b44 | 97 | tos_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
5c350e5a | 98 | { |
7eb35586 | 99 | const struct xt_tos_target_info *info = par->targinfo; |
5c350e5a JE |
100 | struct ipv6hdr *iph = ipv6_hdr(skb); |
101 | u_int8_t orig, nv; | |
102 | ||
103 | orig = ipv6_get_dsfield(iph); | |
1ed2f73d | 104 | nv = (orig & ~info->tos_mask) ^ info->tos_value; |
5c350e5a JE |
105 | |
106 | if (orig != nv) { | |
107 | if (!skb_make_writable(skb, sizeof(struct iphdr))) | |
108 | return NF_DROP; | |
109 | iph = ipv6_hdr(skb); | |
cdfe8b97 | 110 | ipv6_change_dsfield(iph, 0, nv); |
5c350e5a JE |
111 | } |
112 | ||
113 | return XT_CONTINUE; | |
114 | } | |
115 | ||
d3c5ee6d | 116 | static struct xt_target dscp_tg_reg[] __read_mostly = { |
4470bbc7 PM |
117 | { |
118 | .name = "DSCP", | |
ee999d8b | 119 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
120 | .checkentry = dscp_tg_check, |
121 | .target = dscp_tg, | |
4470bbc7 PM |
122 | .targetsize = sizeof(struct xt_DSCP_info), |
123 | .table = "mangle", | |
124 | .me = THIS_MODULE, | |
125 | }, | |
126 | { | |
127 | .name = "DSCP", | |
ee999d8b | 128 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
129 | .checkentry = dscp_tg_check, |
130 | .target = dscp_tg6, | |
4470bbc7 PM |
131 | .targetsize = sizeof(struct xt_DSCP_info), |
132 | .table = "mangle", | |
133 | .me = THIS_MODULE, | |
134 | }, | |
5c350e5a JE |
135 | { |
136 | .name = "TOS", | |
137 | .revision = 1, | |
ee999d8b | 138 | .family = NFPROTO_IPV4, |
5c350e5a JE |
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, | |
ee999d8b | 147 | .family = NFPROTO_IPV6, |
5c350e5a JE |
148 | .table = "mangle", |
149 | .target = tos_tg6, | |
150 | .targetsize = sizeof(struct xt_tos_target_info), | |
151 | .me = THIS_MODULE, | |
152 | }, | |
a468701d YK |
153 | }; |
154 | ||
d3c5ee6d | 155 | static int __init dscp_tg_init(void) |
a468701d | 156 | { |
d3c5ee6d | 157 | return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg)); |
a468701d YK |
158 | } |
159 | ||
d3c5ee6d | 160 | static void __exit dscp_tg_exit(void) |
a468701d | 161 | { |
d3c5ee6d | 162 | xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg)); |
a468701d YK |
163 | } |
164 | ||
d3c5ee6d JE |
165 | module_init(dscp_tg_init); |
166 | module_exit(dscp_tg_exit); |