]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/netfilter/xt_DSCP.c
netfilter: xtables: move extension arguments into compound structure (4/6)
[linux-2.6-omap-h63xx.git] / net / netfilter / xt_DSCP.c
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
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 #include <linux/netfilter_ipv4/ipt_TOS.h>
22
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");
30
31 static unsigned int
32 dscp_tg(struct sk_buff *skb, const struct xt_target_param *par)
33 {
34         const struct xt_DSCP_info *dinfo = par->targinfo;
35         u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
36
37         if (dscp != dinfo->dscp) {
38                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
39                         return NF_DROP;
40
41                 ipv4_change_dsfield(ip_hdr(skb), (__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_target_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), (__u8)(~XT_DSCP_MASK),
59                                     dinfo->dscp << XT_DSCP_SHIFT);
60         }
61         return XT_CONTINUE;
62 }
63
64 static bool
65 dscp_tg_check(const char *tablename, const void *e_void,
66               const struct xt_target *target, void *targinfo,
67               unsigned int hook_mask)
68 {
69         const u_int8_t dscp = ((struct xt_DSCP_info *)targinfo)->dscp;
70
71         if (dscp > XT_DSCP_MAX) {
72                 printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp);
73                 return false;
74         }
75         return true;
76 }
77
78 static unsigned int
79 tos_tg_v0(struct sk_buff *skb, const struct xt_target_param *par)
80 {
81         const struct ipt_tos_target_info *info = par->targinfo;
82         struct iphdr *iph = ip_hdr(skb);
83         u_int8_t oldtos;
84
85         if ((iph->tos & IPTOS_TOS_MASK) != info->tos) {
86                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
87                         return NF_DROP;
88
89                 iph      = ip_hdr(skb);
90                 oldtos   = iph->tos;
91                 iph->tos = (iph->tos & IPTOS_PREC_MASK) | info->tos;
92                 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
93         }
94
95         return XT_CONTINUE;
96 }
97
98 static bool
99 tos_tg_check_v0(const char *tablename, const void *e_void,
100                 const struct xt_target *target, void *targinfo,
101                 unsigned int hook_mask)
102 {
103         const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos;
104
105         if (tos != IPTOS_LOWDELAY && tos != IPTOS_THROUGHPUT &&
106             tos != IPTOS_RELIABILITY && tos != IPTOS_MINCOST &&
107             tos != IPTOS_NORMALSVC) {
108                 printk(KERN_WARNING "TOS: bad tos value %#x\n", tos);
109                 return false;
110         }
111
112         return true;
113 }
114
115 static unsigned int
116 tos_tg(struct sk_buff *skb, const struct xt_target_param *par)
117 {
118         const struct xt_tos_target_info *info = par->targinfo;
119         struct iphdr *iph = ip_hdr(skb);
120         u_int8_t orig, nv;
121
122         orig = ipv4_get_dsfield(iph);
123         nv   = (orig & ~info->tos_mask) ^ info->tos_value;
124
125         if (orig != nv) {
126                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
127                         return NF_DROP;
128                 iph = ip_hdr(skb);
129                 ipv4_change_dsfield(iph, 0, nv);
130         }
131
132         return XT_CONTINUE;
133 }
134
135 static unsigned int
136 tos_tg6(struct sk_buff *skb, const struct xt_target_param *par)
137 {
138         const struct xt_tos_target_info *info = par->targinfo;
139         struct ipv6hdr *iph = ipv6_hdr(skb);
140         u_int8_t orig, nv;
141
142         orig = ipv6_get_dsfield(iph);
143         nv   = (orig & info->tos_mask) ^ info->tos_value;
144
145         if (orig != nv) {
146                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
147                         return NF_DROP;
148                 iph = ipv6_hdr(skb);
149                 ipv6_change_dsfield(iph, 0, nv);
150         }
151
152         return XT_CONTINUE;
153 }
154
155 static struct xt_target dscp_tg_reg[] __read_mostly = {
156         {
157                 .name           = "DSCP",
158                 .family         = NFPROTO_IPV4,
159                 .checkentry     = dscp_tg_check,
160                 .target         = dscp_tg,
161                 .targetsize     = sizeof(struct xt_DSCP_info),
162                 .table          = "mangle",
163                 .me             = THIS_MODULE,
164         },
165         {
166                 .name           = "DSCP",
167                 .family         = NFPROTO_IPV6,
168                 .checkentry     = dscp_tg_check,
169                 .target         = dscp_tg6,
170                 .targetsize     = sizeof(struct xt_DSCP_info),
171                 .table          = "mangle",
172                 .me             = THIS_MODULE,
173         },
174         {
175                 .name           = "TOS",
176                 .revision       = 0,
177                 .family         = NFPROTO_IPV4,
178                 .table          = "mangle",
179                 .target         = tos_tg_v0,
180                 .targetsize     = sizeof(struct ipt_tos_target_info),
181                 .checkentry     = tos_tg_check_v0,
182                 .me             = THIS_MODULE,
183         },
184         {
185                 .name           = "TOS",
186                 .revision       = 1,
187                 .family         = NFPROTO_IPV4,
188                 .table          = "mangle",
189                 .target         = tos_tg,
190                 .targetsize     = sizeof(struct xt_tos_target_info),
191                 .me             = THIS_MODULE,
192         },
193         {
194                 .name           = "TOS",
195                 .revision       = 1,
196                 .family         = NFPROTO_IPV6,
197                 .table          = "mangle",
198                 .target         = tos_tg6,
199                 .targetsize     = sizeof(struct xt_tos_target_info),
200                 .me             = THIS_MODULE,
201         },
202 };
203
204 static int __init dscp_tg_init(void)
205 {
206         return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
207 }
208
209 static void __exit dscp_tg_exit(void)
210 {
211         xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
212 }
213
214 module_init(dscp_tg_init);
215 module_exit(dscp_tg_exit);