Linux 2.6.31.6
[linux/fpc-iii.git] / net / netfilter / xt_DSCP.c
blob6a347e768f862ed9a9621dfc23f27cf40b648ad2
1 /* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8
3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
4 * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
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.
10 * See RFC2474 for a description of the DSCP field within the IP Header.
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>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter/xt_DSCP.h>
21 #include <linux/netfilter_ipv4/ipt_TOS.h>
23 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
24 MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification");
25 MODULE_LICENSE("GPL");
26 MODULE_ALIAS("ipt_DSCP");
27 MODULE_ALIAS("ip6t_DSCP");
28 MODULE_ALIAS("ipt_TOS");
29 MODULE_ALIAS("ip6t_TOS");
31 static unsigned int
32 dscp_tg(struct sk_buff *skb, const struct xt_target_param *par)
34 const struct xt_DSCP_info *dinfo = par->targinfo;
35 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
37 if (dscp != dinfo->dscp) {
38 if (!skb_make_writable(skb, sizeof(struct iphdr)))
39 return NF_DROP;
41 ipv4_change_dsfield(ip_hdr(skb), (__u8)(~XT_DSCP_MASK),
42 dinfo->dscp << XT_DSCP_SHIFT);
45 return XT_CONTINUE;
48 static unsigned int
49 dscp_tg6(struct sk_buff *skb, const struct xt_target_param *par)
51 const struct xt_DSCP_info *dinfo = par->targinfo;
52 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
54 if (dscp != dinfo->dscp) {
55 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
56 return NF_DROP;
58 ipv6_change_dsfield(ipv6_hdr(skb), (__u8)(~XT_DSCP_MASK),
59 dinfo->dscp << XT_DSCP_SHIFT);
61 return XT_CONTINUE;
64 static bool dscp_tg_check(const struct xt_tgchk_param *par)
66 const struct xt_DSCP_info *info = par->targinfo;
68 if (info->dscp > XT_DSCP_MAX) {
69 printk(KERN_WARNING "DSCP: dscp %x out of range\n", info->dscp);
70 return false;
72 return true;
75 static unsigned int
76 tos_tg_v0(struct sk_buff *skb, const struct xt_target_param *par)
78 const struct ipt_tos_target_info *info = par->targinfo;
79 struct iphdr *iph = ip_hdr(skb);
80 u_int8_t oldtos;
82 if ((iph->tos & IPTOS_TOS_MASK) != info->tos) {
83 if (!skb_make_writable(skb, sizeof(struct iphdr)))
84 return NF_DROP;
86 iph = ip_hdr(skb);
87 oldtos = iph->tos;
88 iph->tos = (iph->tos & IPTOS_PREC_MASK) | info->tos;
89 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
92 return XT_CONTINUE;
95 static bool tos_tg_check_v0(const struct xt_tgchk_param *par)
97 const struct ipt_tos_target_info *info = par->targinfo;
98 const uint8_t tos = info->tos;
100 if (tos != IPTOS_LOWDELAY && tos != IPTOS_THROUGHPUT &&
101 tos != IPTOS_RELIABILITY && tos != IPTOS_MINCOST &&
102 tos != IPTOS_NORMALSVC) {
103 printk(KERN_WARNING "TOS: bad tos value %#x\n", tos);
104 return false;
107 return true;
110 static unsigned int
111 tos_tg(struct sk_buff *skb, const struct xt_target_param *par)
113 const struct xt_tos_target_info *info = par->targinfo;
114 struct iphdr *iph = ip_hdr(skb);
115 u_int8_t orig, nv;
117 orig = ipv4_get_dsfield(iph);
118 nv = (orig & ~info->tos_mask) ^ info->tos_value;
120 if (orig != nv) {
121 if (!skb_make_writable(skb, sizeof(struct iphdr)))
122 return NF_DROP;
123 iph = ip_hdr(skb);
124 ipv4_change_dsfield(iph, 0, nv);
127 return XT_CONTINUE;
130 static unsigned int
131 tos_tg6(struct sk_buff *skb, const struct xt_target_param *par)
133 const struct xt_tos_target_info *info = par->targinfo;
134 struct ipv6hdr *iph = ipv6_hdr(skb);
135 u_int8_t orig, nv;
137 orig = ipv6_get_dsfield(iph);
138 nv = (orig & info->tos_mask) ^ info->tos_value;
140 if (orig != nv) {
141 if (!skb_make_writable(skb, sizeof(struct iphdr)))
142 return NF_DROP;
143 iph = ipv6_hdr(skb);
144 ipv6_change_dsfield(iph, 0, nv);
147 return XT_CONTINUE;
150 static struct xt_target dscp_tg_reg[] __read_mostly = {
152 .name = "DSCP",
153 .family = NFPROTO_IPV4,
154 .checkentry = dscp_tg_check,
155 .target = dscp_tg,
156 .targetsize = sizeof(struct xt_DSCP_info),
157 .table = "mangle",
158 .me = THIS_MODULE,
161 .name = "DSCP",
162 .family = NFPROTO_IPV6,
163 .checkentry = dscp_tg_check,
164 .target = dscp_tg6,
165 .targetsize = sizeof(struct xt_DSCP_info),
166 .table = "mangle",
167 .me = THIS_MODULE,
170 .name = "TOS",
171 .revision = 0,
172 .family = NFPROTO_IPV4,
173 .table = "mangle",
174 .target = tos_tg_v0,
175 .targetsize = sizeof(struct ipt_tos_target_info),
176 .checkentry = tos_tg_check_v0,
177 .me = THIS_MODULE,
180 .name = "TOS",
181 .revision = 1,
182 .family = NFPROTO_IPV4,
183 .table = "mangle",
184 .target = tos_tg,
185 .targetsize = sizeof(struct xt_tos_target_info),
186 .me = THIS_MODULE,
189 .name = "TOS",
190 .revision = 1,
191 .family = NFPROTO_IPV6,
192 .table = "mangle",
193 .target = tos_tg6,
194 .targetsize = sizeof(struct xt_tos_target_info),
195 .me = THIS_MODULE,
199 static int __init dscp_tg_init(void)
201 return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
204 static void __exit dscp_tg_exit(void)
206 xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
209 module_init(dscp_tg_init);
210 module_exit(dscp_tg_exit);