blob: a97704a3f95ce13359609ba68252189fac638b24 [file] [log] [blame]
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -07001/* 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 * xt_DSCP.c,v 1.8 2002/08/06 18:41:57 laforge Exp
13*/
14
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <net/dsfield.h>
20
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_DSCP.h>
23
24MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
25MODULE_DESCRIPTION("x_tables DSCP modification module");
26MODULE_LICENSE("GPL");
27MODULE_ALIAS("ipt_DSCP");
28MODULE_ALIAS("ip6t_DSCP");
29
30static unsigned int target(struct sk_buff **pskb,
31 const struct net_device *in,
32 const struct net_device *out,
33 unsigned int hooknum,
34 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070035 const void *targinfo)
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070036{
37 const struct xt_DSCP_info *dinfo = targinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070038 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(*pskb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070039
40 if (dscp != dinfo->dscp) {
41 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
42 return NF_DROP;
43
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070044 ipv4_change_dsfield(ip_hdr(*pskb), (__u8)(~XT_DSCP_MASK),
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070045 dinfo->dscp << XT_DSCP_SHIFT);
46
47 }
48 return XT_CONTINUE;
49}
50
51static unsigned int target6(struct sk_buff **pskb,
52 const struct net_device *in,
53 const struct net_device *out,
54 unsigned int hooknum,
55 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070056 const void *targinfo)
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070057{
58 const struct xt_DSCP_info *dinfo = targinfo;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070059 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(*pskb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070060
61 if (dscp != dinfo->dscp) {
62 if (!skb_make_writable(pskb, sizeof(struct ipv6hdr)))
63 return NF_DROP;
64
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070065 ipv6_change_dsfield(ipv6_hdr(*pskb), (__u8)(~XT_DSCP_MASK),
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070066 dinfo->dscp << XT_DSCP_SHIFT);
67 }
68 return XT_CONTINUE;
69}
70
71static int checkentry(const char *tablename,
72 const void *e_void,
73 const struct xt_target *target,
74 void *targinfo,
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -070075 unsigned int hook_mask)
76{
77 const u_int8_t dscp = ((struct xt_DSCP_info *)targinfo)->dscp;
78
79 if ((dscp > XT_DSCP_MAX)) {
80 printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp);
81 return 0;
82 }
83 return 1;
84}
85
Patrick McHardy4470bbc2006-08-22 00:34:04 -070086static struct xt_target xt_dscp_target[] = {
87 {
88 .name = "DSCP",
89 .family = AF_INET,
90 .checkentry = checkentry,
91 .target = target,
92 .targetsize = sizeof(struct xt_DSCP_info),
93 .table = "mangle",
94 .me = THIS_MODULE,
95 },
96 {
97 .name = "DSCP",
98 .family = AF_INET6,
99 .checkentry = checkentry,
100 .target = target6,
101 .targetsize = sizeof(struct xt_DSCP_info),
102 .table = "mangle",
103 .me = THIS_MODULE,
104 },
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -0700105};
106
107static int __init xt_dscp_target_init(void)
108{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700109 return xt_register_targets(xt_dscp_target, ARRAY_SIZE(xt_dscp_target));
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -0700110}
111
112static void __exit xt_dscp_target_fini(void)
113{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700114 xt_unregister_targets(xt_dscp_target, ARRAY_SIZE(xt_dscp_target));
Yasuyuki Kozakaia4687012006-08-22 00:30:26 -0700115}
116
117module_init(xt_dscp_target_init);
118module_exit(xt_dscp_target_fini);