[NETFILTER]: Merge ipt_TOS into xt_DSCP
[linux-2.6] / 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("x_tables DSCP modification module");
25 MODULE_LICENSE("GPL");
26 MODULE_ALIAS("ipt_DSCP");
27 MODULE_ALIAS("ip6t_DSCP");
28 MODULE_ALIAS("ipt_TOS");
29
30 static unsigned int
31 dscp_tg(struct sk_buff *skb, const struct net_device *in,
32         const struct net_device *out, unsigned int hooknum,
33         const struct xt_target *target, const void *targinfo)
34 {
35         const struct xt_DSCP_info *dinfo = targinfo;
36         u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
37
38         if (dscp != dinfo->dscp) {
39                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
40                         return NF_DROP;
41
42                 ipv4_change_dsfield(ip_hdr(skb), (__u8)(~XT_DSCP_MASK),
43                                     dinfo->dscp << XT_DSCP_SHIFT);
44
45         }
46         return XT_CONTINUE;
47 }
48
49 static unsigned int
50 dscp_tg6(struct sk_buff *skb, const struct net_device *in,
51          const struct net_device *out, unsigned int hooknum,
52          const struct xt_target *target, const void *targinfo)
53 {
54         const struct xt_DSCP_info *dinfo = targinfo;
55         u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
56
57         if (dscp != dinfo->dscp) {
58                 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
59                         return NF_DROP;
60
61                 ipv6_change_dsfield(ipv6_hdr(skb), (__u8)(~XT_DSCP_MASK),
62                                     dinfo->dscp << XT_DSCP_SHIFT);
63         }
64         return XT_CONTINUE;
65 }
66
67 static bool
68 dscp_tg_check(const char *tablename, const void *e_void,
69               const struct xt_target *target, void *targinfo,
70               unsigned int hook_mask)
71 {
72         const u_int8_t dscp = ((struct xt_DSCP_info *)targinfo)->dscp;
73
74         if (dscp > XT_DSCP_MAX) {
75                 printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp);
76                 return false;
77         }
78         return true;
79 }
80
81 static unsigned int
82 tos_tg_v0(struct sk_buff *skb, const struct net_device *in,
83           const struct net_device *out, unsigned int hooknum,
84           const struct xt_target *target, const void *targinfo)
85 {
86         const struct ipt_tos_target_info *info = targinfo;
87         struct iphdr *iph = ip_hdr(skb);
88         u_int8_t oldtos;
89
90         if ((iph->tos & IPTOS_TOS_MASK) != info->tos) {
91                 if (!skb_make_writable(skb, sizeof(struct iphdr)))
92                         return NF_DROP;
93
94                 iph      = ip_hdr(skb);
95                 oldtos   = iph->tos;
96                 iph->tos = (iph->tos & IPTOS_PREC_MASK) | info->tos;
97                 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
98         }
99
100         return XT_CONTINUE;
101 }
102
103 static bool
104 tos_tg_check_v0(const char *tablename, const void *e_void,
105                 const struct xt_target *target, void *targinfo,
106                 unsigned int hook_mask)
107 {
108         const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos;
109
110         if (tos != IPTOS_LOWDELAY && tos != IPTOS_THROUGHPUT &&
111             tos != IPTOS_RELIABILITY && tos != IPTOS_MINCOST &&
112             tos != IPTOS_NORMALSVC) {
113                 printk(KERN_WARNING "TOS: bad tos value %#x\n", tos);
114                 return false;
115         }
116
117         return true;
118 }
119
120 static struct xt_target dscp_tg_reg[] __read_mostly = {
121         {
122                 .name           = "DSCP",
123                 .family         = AF_INET,
124                 .checkentry     = dscp_tg_check,
125                 .target         = dscp_tg,
126                 .targetsize     = sizeof(struct xt_DSCP_info),
127                 .table          = "mangle",
128                 .me             = THIS_MODULE,
129         },
130         {
131                 .name           = "DSCP",
132                 .family         = AF_INET6,
133                 .checkentry     = dscp_tg_check,
134                 .target         = dscp_tg6,
135                 .targetsize     = sizeof(struct xt_DSCP_info),
136                 .table          = "mangle",
137                 .me             = THIS_MODULE,
138         },
139         {
140                 .name           = "TOS",
141                 .revision       = 0,
142                 .family         = AF_INET,
143                 .table          = "mangle",
144                 .target         = tos_tg_v0,
145                 .targetsize     = sizeof(struct ipt_tos_target_info),
146                 .checkentry     = tos_tg_check_v0,
147                 .me             = THIS_MODULE,
148         },
149 };
150
151 static int __init dscp_tg_init(void)
152 {
153         return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
154 }
155
156 static void __exit dscp_tg_exit(void)
157 {
158         xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
159 }
160
161 module_init(dscp_tg_init);
162 module_exit(dscp_tg_exit);