1 /* This is a module which is used for setting the TOS field of a packet. */
2 #include <linux/module.h>
3 #include <linux/skbuff.h>
5 #include <net/checksum.h>
7 #include <linux/netfilter_ipv4/ip_tables.h>
8 #include <linux/netfilter_ipv4/ipt_TOS.h>
11 target(struct sk_buff **pskb,
13 const struct net_device *in,
14 const struct net_device *out,
18 struct iphdr *iph = (*pskb)->nh.iph;
19 const struct ipt_tos_target_info *tosinfo = targinfo;
21 if ((iph->tos & IPTOS_TOS_MASK) != tosinfo->tos) {
24 /* raw socket (tcpdump) may have clone of incoming
25 skb: don't disturb it --RR */
26 if (skb_cloned(*pskb) && !(*pskb)->sk) {
27 struct sk_buff *nskb = skb_copy(*pskb, GFP_ATOMIC);
32 iph = (*pskb)->nh.iph;
35 diffs[0] = htons(iph->tos) ^ 0xFFFF;
36 iph->tos = (iph->tos & IPTOS_PREC_MASK) | tosinfo->tos;
37 diffs[1] = htons(iph->tos);
38 iph->check = csum_fold(csum_partial((char *)diffs,
41 (*pskb)->nfcache |= NFC_ALTERED;
47 checkentry(const char *tablename,
48 const struct ipt_entry *e,
50 unsigned int targinfosize,
51 unsigned int hook_mask)
53 const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos;
55 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_tos_target_info))) {
56 printk(KERN_WARNING "TOS: targinfosize %u != %Zu\n",
58 IPT_ALIGN(sizeof(struct ipt_tos_target_info)));
62 if (strcmp(tablename, "mangle") != 0) {
63 printk(KERN_WARNING "TOS: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
67 if (tos != IPTOS_LOWDELAY
68 && tos != IPTOS_THROUGHPUT
69 && tos != IPTOS_RELIABILITY
70 && tos != IPTOS_MINCOST
71 && tos != IPTOS_NORMALSVC) {
72 printk(KERN_WARNING "TOS: bad tos value %#x\n", tos);
79 static struct ipt_target ipt_tos_reg
80 = { { NULL, NULL }, "TOS", target, checkentry, NULL, THIS_MODULE };
82 static int __init init(void)
84 if (ipt_register_target(&ipt_tos_reg))
90 static void __exit fini(void)
92 ipt_unregister_target(&ipt_tos_reg);
97 MODULE_LICENSE("GPL");