Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* iptables module for setting the IPv4 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 | * ipt_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 <net/checksum.h> |
| 19 | |
| 20 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 21 | #include <linux/netfilter_ipv4/ipt_DSCP.h> |
| 22 | |
| 23 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 24 | MODULE_DESCRIPTION("iptables DSCP modification module"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
| 27 | static unsigned int |
| 28 | target(struct sk_buff **pskb, |
| 29 | const struct net_device *in, |
| 30 | const struct net_device *out, |
| 31 | unsigned int hooknum, |
| 32 | const void *targinfo, |
| 33 | void *userinfo) |
| 34 | { |
| 35 | const struct ipt_DSCP_info *dinfo = targinfo; |
| 36 | u_int8_t sh_dscp = ((dinfo->dscp << IPT_DSCP_SHIFT) & IPT_DSCP_MASK); |
| 37 | |
| 38 | |
| 39 | if (((*pskb)->nh.iph->tos & IPT_DSCP_MASK) != sh_dscp) { |
| 40 | u_int16_t diffs[2]; |
| 41 | |
| 42 | if (!skb_ip_make_writable(pskb, sizeof(struct iphdr))) |
| 43 | return NF_DROP; |
| 44 | |
| 45 | diffs[0] = htons((*pskb)->nh.iph->tos) ^ 0xFFFF; |
| 46 | (*pskb)->nh.iph->tos = ((*pskb)->nh.iph->tos & ~IPT_DSCP_MASK) |
| 47 | | sh_dscp; |
| 48 | diffs[1] = htons((*pskb)->nh.iph->tos); |
| 49 | (*pskb)->nh.iph->check |
| 50 | = csum_fold(csum_partial((char *)diffs, |
| 51 | sizeof(diffs), |
| 52 | (*pskb)->nh.iph->check |
| 53 | ^ 0xFFFF)); |
| 54 | (*pskb)->nfcache |= NFC_ALTERED; |
| 55 | } |
| 56 | return IPT_CONTINUE; |
| 57 | } |
| 58 | |
| 59 | static int |
| 60 | checkentry(const char *tablename, |
| 61 | const struct ipt_entry *e, |
| 62 | void *targinfo, |
| 63 | unsigned int targinfosize, |
| 64 | unsigned int hook_mask) |
| 65 | { |
| 66 | const u_int8_t dscp = ((struct ipt_DSCP_info *)targinfo)->dscp; |
| 67 | |
| 68 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_DSCP_info))) { |
| 69 | printk(KERN_WARNING "DSCP: targinfosize %u != %Zu\n", |
| 70 | targinfosize, |
| 71 | IPT_ALIGN(sizeof(struct ipt_DSCP_info))); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | if (strcmp(tablename, "mangle") != 0) { |
| 76 | printk(KERN_WARNING "DSCP: can only be called from \"mangle\" table, not \"%s\"\n", tablename); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | if ((dscp > IPT_DSCP_MAX)) { |
| 81 | printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | static struct ipt_target ipt_dscp_reg = { |
| 89 | .name = "DSCP", |
| 90 | .target = target, |
| 91 | .checkentry = checkentry, |
| 92 | .me = THIS_MODULE, |
| 93 | }; |
| 94 | |
| 95 | static int __init init(void) |
| 96 | { |
| 97 | return ipt_register_target(&ipt_dscp_reg); |
| 98 | } |
| 99 | |
| 100 | static void __exit fini(void) |
| 101 | { |
| 102 | ipt_unregister_target(&ipt_dscp_reg); |
| 103 | } |
| 104 | |
| 105 | module_init(init); |
| 106 | module_exit(fini); |