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