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>
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");
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)))
41 ipv4_change_dsfield(ip_hdr(skb), (__u8)(~XT_DSCP_MASK),
42 dinfo->dscp << XT_DSCP_SHIFT);
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)))
58 ipv6_change_dsfield(ipv6_hdr(skb), (__u8)(~XT_DSCP_MASK),
59 dinfo->dscp << XT_DSCP_SHIFT);
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);
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);
82 if ((iph->tos & IPTOS_TOS_MASK) != info->tos) {
83 if (!skb_make_writable(skb, sizeof(struct iphdr)))
88 iph->tos = (iph->tos & IPTOS_PREC_MASK) | info->tos;
89 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
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);
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);
117 orig = ipv4_get_dsfield(iph);
118 nv = (orig & ~info->tos_mask) ^ info->tos_value;
121 if (!skb_make_writable(skb, sizeof(struct iphdr)))
124 ipv4_change_dsfield(iph, 0, nv);
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);
137 orig = ipv6_get_dsfield(iph);
138 nv = (orig & info->tos_mask) ^ info->tos_value;
141 if (!skb_make_writable(skb, sizeof(struct iphdr)))
144 ipv6_change_dsfield(iph, 0, nv);
150 static struct xt_target dscp_tg_reg[] __read_mostly = {
153 .family = NFPROTO_IPV4,
154 .checkentry = dscp_tg_check,
156 .targetsize = sizeof(struct xt_DSCP_info),
162 .family = NFPROTO_IPV6,
163 .checkentry = dscp_tg_check,
165 .targetsize = sizeof(struct xt_DSCP_info),
172 .family = NFPROTO_IPV4,
175 .targetsize = sizeof(struct ipt_tos_target_info),
176 .checkentry = tos_tg_check_v0,
182 .family = NFPROTO_IPV4,
185 .targetsize = sizeof(struct xt_tos_target_info),
191 .family = NFPROTO_IPV6,
194 .targetsize = sizeof(struct xt_tos_target_info),
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);