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("x_tables DSCP modification module");
25 MODULE_LICENSE("GPL");
26 MODULE_ALIAS("ipt_DSCP");
27 MODULE_ALIAS("ip6t_DSCP");
28 MODULE_ALIAS("ipt_TOS");
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)
35 const struct xt_DSCP_info *dinfo = targinfo;
36 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
38 if (dscp != dinfo->dscp) {
39 if (!skb_make_writable(skb, sizeof(struct iphdr)))
42 ipv4_change_dsfield(ip_hdr(skb), (__u8)(~XT_DSCP_MASK),
43 dinfo->dscp << XT_DSCP_SHIFT);
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)
54 const struct xt_DSCP_info *dinfo = targinfo;
55 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
57 if (dscp != dinfo->dscp) {
58 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
61 ipv6_change_dsfield(ipv6_hdr(skb), (__u8)(~XT_DSCP_MASK),
62 dinfo->dscp << XT_DSCP_SHIFT);
68 dscp_tg_check(const char *tablename, const void *e_void,
69 const struct xt_target *target, void *targinfo,
70 unsigned int hook_mask)
72 const u_int8_t dscp = ((struct xt_DSCP_info *)targinfo)->dscp;
74 if (dscp > XT_DSCP_MAX) {
75 printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp);
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)
86 const struct ipt_tos_target_info *info = targinfo;
87 struct iphdr *iph = ip_hdr(skb);
90 if ((iph->tos & IPTOS_TOS_MASK) != info->tos) {
91 if (!skb_make_writable(skb, sizeof(struct iphdr)))
96 iph->tos = (iph->tos & IPTOS_PREC_MASK) | info->tos;
97 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
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)
108 const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos;
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);
120 static struct xt_target dscp_tg_reg[] __read_mostly = {
124 .checkentry = dscp_tg_check,
126 .targetsize = sizeof(struct xt_DSCP_info),
133 .checkentry = dscp_tg_check,
135 .targetsize = sizeof(struct xt_DSCP_info),
145 .targetsize = sizeof(struct ipt_tos_target_info),
146 .checkentry = tos_tg_check_v0,
151 static int __init dscp_tg_init(void)
153 return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
156 static void __exit dscp_tg_exit(void)
158 xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
161 module_init(dscp_tg_init);
162 module_exit(dscp_tg_exit);