netfilter: nfnetlink: add struct nfnl_info and pass it to callbacks
[linux-block.git] / net / netfilter / xt_DSCP.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
a468701d
YK
2/* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8
3 *
4 * (C) 2002 by Harald Welte <laforge@netfilter.org>
5 * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
6 *
a468701d 7 * See RFC2474 for a description of the DSCP field within the IP Header.
a468701d 8*/
8bee4bad 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
a468701d
YK
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13#include <linux/ipv6.h>
14#include <net/dsfield.h>
15
16#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter/xt_DSCP.h>
18
19MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
2ae15b64 20MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification");
a468701d
YK
21MODULE_LICENSE("GPL");
22MODULE_ALIAS("ipt_DSCP");
23MODULE_ALIAS("ip6t_DSCP");
c9fd4968 24MODULE_ALIAS("ipt_TOS");
5c350e5a 25MODULE_ALIAS("ip6t_TOS");
a468701d 26
d3c5ee6d 27static unsigned int
4b560b44 28dscp_tg(struct sk_buff *skb, const struct xt_action_param *par)
a468701d 29{
7eb35586 30 const struct xt_DSCP_info *dinfo = par->targinfo;
3db05fea 31 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
a468701d
YK
32
33 if (dscp != dinfo->dscp) {
2cf6bffc 34 if (skb_ensure_writable(skb, sizeof(struct iphdr)))
a468701d
YK
35 return NF_DROP;
36
56768644
FW
37 ipv4_change_dsfield(ip_hdr(skb),
38 (__force __u8)(~XT_DSCP_MASK),
a468701d
YK
39 dinfo->dscp << XT_DSCP_SHIFT);
40
41 }
42 return XT_CONTINUE;
43}
44
d3c5ee6d 45static unsigned int
4b560b44 46dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)
a468701d 47{
7eb35586 48 const struct xt_DSCP_info *dinfo = par->targinfo;
3db05fea 49 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
a468701d
YK
50
51 if (dscp != dinfo->dscp) {
2cf6bffc 52 if (skb_ensure_writable(skb, sizeof(struct ipv6hdr)))
a468701d
YK
53 return NF_DROP;
54
56768644
FW
55 ipv6_change_dsfield(ipv6_hdr(skb),
56 (__force __u8)(~XT_DSCP_MASK),
a468701d
YK
57 dinfo->dscp << XT_DSCP_SHIFT);
58 }
59 return XT_CONTINUE;
60}
61
135367b8 62static int dscp_tg_check(const struct xt_tgchk_param *par)
a468701d 63{
af5d6dc2 64 const struct xt_DSCP_info *info = par->targinfo;
a468701d 65
0cc9501f 66 if (info->dscp > XT_DSCP_MAX)
4a5a5c73 67 return -EDOM;
d6b00a53 68 return 0;
a468701d
YK
69}
70
5c350e5a 71static unsigned int
4b560b44 72tos_tg(struct sk_buff *skb, const struct xt_action_param *par)
5c350e5a 73{
7eb35586 74 const struct xt_tos_target_info *info = par->targinfo;
5c350e5a
JE
75 struct iphdr *iph = ip_hdr(skb);
76 u_int8_t orig, nv;
77
78 orig = ipv4_get_dsfield(iph);
9bb268ed 79 nv = (orig & ~info->tos_mask) ^ info->tos_value;
5c350e5a
JE
80
81 if (orig != nv) {
2cf6bffc 82 if (skb_ensure_writable(skb, sizeof(struct iphdr)))
5c350e5a
JE
83 return NF_DROP;
84 iph = ip_hdr(skb);
cdfe8b97 85 ipv4_change_dsfield(iph, 0, nv);
5c350e5a
JE
86 }
87
88 return XT_CONTINUE;
89}
90
91static unsigned int
4b560b44 92tos_tg6(struct sk_buff *skb, const struct xt_action_param *par)
5c350e5a 93{
7eb35586 94 const struct xt_tos_target_info *info = par->targinfo;
5c350e5a
JE
95 struct ipv6hdr *iph = ipv6_hdr(skb);
96 u_int8_t orig, nv;
97
98 orig = ipv6_get_dsfield(iph);
1ed2f73d 99 nv = (orig & ~info->tos_mask) ^ info->tos_value;
5c350e5a
JE
100
101 if (orig != nv) {
2cf6bffc 102 if (skb_ensure_writable(skb, sizeof(struct iphdr)))
5c350e5a
JE
103 return NF_DROP;
104 iph = ipv6_hdr(skb);
cdfe8b97 105 ipv6_change_dsfield(iph, 0, nv);
5c350e5a
JE
106 }
107
108 return XT_CONTINUE;
109}
110
d3c5ee6d 111static struct xt_target dscp_tg_reg[] __read_mostly = {
4470bbc7
PM
112 {
113 .name = "DSCP",
ee999d8b 114 .family = NFPROTO_IPV4,
d3c5ee6d
JE
115 .checkentry = dscp_tg_check,
116 .target = dscp_tg,
4470bbc7
PM
117 .targetsize = sizeof(struct xt_DSCP_info),
118 .table = "mangle",
119 .me = THIS_MODULE,
120 },
121 {
122 .name = "DSCP",
ee999d8b 123 .family = NFPROTO_IPV6,
d3c5ee6d
JE
124 .checkentry = dscp_tg_check,
125 .target = dscp_tg6,
4470bbc7
PM
126 .targetsize = sizeof(struct xt_DSCP_info),
127 .table = "mangle",
128 .me = THIS_MODULE,
129 },
5c350e5a
JE
130 {
131 .name = "TOS",
132 .revision = 1,
ee999d8b 133 .family = NFPROTO_IPV4,
5c350e5a
JE
134 .table = "mangle",
135 .target = tos_tg,
136 .targetsize = sizeof(struct xt_tos_target_info),
137 .me = THIS_MODULE,
138 },
139 {
140 .name = "TOS",
141 .revision = 1,
ee999d8b 142 .family = NFPROTO_IPV6,
5c350e5a
JE
143 .table = "mangle",
144 .target = tos_tg6,
145 .targetsize = sizeof(struct xt_tos_target_info),
146 .me = THIS_MODULE,
147 },
a468701d
YK
148};
149
d3c5ee6d 150static int __init dscp_tg_init(void)
a468701d 151{
d3c5ee6d 152 return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
a468701d
YK
153}
154
d3c5ee6d 155static void __exit dscp_tg_exit(void)
a468701d 156{
d3c5ee6d 157 xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
a468701d
YK
158}
159
d3c5ee6d
JE
160module_init(dscp_tg_init);
161module_exit(dscp_tg_exit);