Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* This is a module which is used for setting the TOS field of a packet. */ |
2 | ||
3 | /* (C) 1999-2001 Paul `Rusty' Russell | |
4 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
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 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/ip.h> | |
14 | #include <net/checksum.h> | |
15 | ||
6709dbbb | 16 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
17 | #include <linux/netfilter_ipv4/ipt_TOS.h> |
18 | ||
19 | MODULE_LICENSE("GPL"); | |
20 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
21 | MODULE_DESCRIPTION("iptables TOS mangling module"); | |
22 | ||
23 | static unsigned int | |
24 | target(struct sk_buff **pskb, | |
25 | const struct net_device *in, | |
26 | const struct net_device *out, | |
27 | unsigned int hooknum, | |
c4986734 | 28 | const struct xt_target *target, |
fe1cb108 | 29 | const void *targinfo) |
1da177e4 LT |
30 | { |
31 | const struct ipt_tos_target_info *tosinfo = targinfo; | |
da878c8e | 32 | struct iphdr *iph = (*pskb)->nh.iph; |
1da177e4 | 33 | |
da878c8e | 34 | if ((iph->tos & IPTOS_TOS_MASK) != tosinfo->tos) { |
43bc0ca7 | 35 | __u8 oldtos; |
089af26c | 36 | if (!skb_make_writable(pskb, sizeof(struct iphdr))) |
1da177e4 | 37 | return NF_DROP; |
da878c8e PM |
38 | iph = (*pskb)->nh.iph; |
39 | oldtos = iph->tos; | |
40 | iph->tos = (iph->tos & IPTOS_PREC_MASK) | tosinfo->tos; | |
43bc0ca7 | 41 | nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos)); |
1da177e4 | 42 | } |
6709dbbb | 43 | return XT_CONTINUE; |
1da177e4 LT |
44 | } |
45 | ||
46 | static int | |
47 | checkentry(const char *tablename, | |
2e4e6a17 | 48 | const void *e_void, |
c4986734 | 49 | const struct xt_target *target, |
e905a9ed YH |
50 | void *targinfo, |
51 | unsigned int hook_mask) | |
1da177e4 LT |
52 | { |
53 | const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos; | |
54 | ||
1da177e4 LT |
55 | if (tos != IPTOS_LOWDELAY |
56 | && tos != IPTOS_THROUGHPUT | |
57 | && tos != IPTOS_RELIABILITY | |
58 | && tos != IPTOS_MINCOST | |
59 | && tos != IPTOS_NORMALSVC) { | |
60 | printk(KERN_WARNING "TOS: bad tos value %#x\n", tos); | |
61 | return 0; | |
62 | } | |
1da177e4 LT |
63 | return 1; |
64 | } | |
65 | ||
6709dbbb | 66 | static struct xt_target ipt_tos_reg = { |
1da177e4 | 67 | .name = "TOS", |
6709dbbb | 68 | .family = AF_INET, |
1da177e4 | 69 | .target = target, |
1d5cd909 PM |
70 | .targetsize = sizeof(struct ipt_tos_target_info), |
71 | .table = "mangle", | |
1da177e4 LT |
72 | .checkentry = checkentry, |
73 | .me = THIS_MODULE, | |
74 | }; | |
75 | ||
65b4b4e8 | 76 | static int __init ipt_tos_init(void) |
1da177e4 | 77 | { |
6709dbbb | 78 | return xt_register_target(&ipt_tos_reg); |
1da177e4 LT |
79 | } |
80 | ||
65b4b4e8 | 81 | static void __exit ipt_tos_fini(void) |
1da177e4 | 82 | { |
6709dbbb | 83 | xt_unregister_target(&ipt_tos_reg); |
1da177e4 LT |
84 | } |
85 | ||
65b4b4e8 AM |
86 | module_init(ipt_tos_init); |
87 | module_exit(ipt_tos_fini); |