blob: 270765236f5e8cc9e39c02f9b6fa0836f853f96d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* iptables module for the IPv4 and TCP ECN bits, Version 1.5
2 *
3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006 * it under the terms of the GNU General Public License version 2 as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008*/
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080010#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030014#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/tcp.h>
16#include <net/checksum.h>
17
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080018#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/netfilter_ipv4/ip_tables.h>
20#include <linux/netfilter_ipv4/ipt_ECN.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080024MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag modification");
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/* set ECT codepoint from IP header.
Jan Engelhardte1931b72007-07-07 22:16:26 -070027 * return false if there was an error. */
28static inline bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070029set_ect_ip(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Herbert Xu3db05fe2007-10-15 00:53:15 -070031 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Patrick McHardyda878c82006-08-22 00:33:09 -070033 if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
Al Viro43bc0ca2006-11-14 21:43:23 -080034 __u8 oldtos;
Herbert Xu3db05fe2007-10-15 00:53:15 -070035 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Jan Engelhardte1931b72007-07-07 22:16:26 -070036 return false;
Herbert Xu3db05fe2007-10-15 00:53:15 -070037 iph = ip_hdr(skb);
Patrick McHardyda878c82006-08-22 00:33:09 -070038 oldtos = iph->tos;
39 iph->tos &= ~IPT_ECN_IP_MASK;
40 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110041 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090042 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070043 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Jan Engelhardte1931b72007-07-07 22:16:26 -070046/* Return false if there was an error. */
47static inline bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070048set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct tcphdr _tcph, *tcph;
Al Viro6a19d612006-09-28 14:22:24 -070051 __be16 oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020053 /* Not enough header? */
Herbert Xu3db05fe2007-10-15 00:53:15 -070054 tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!tcph)
Jan Engelhardte1931b72007-07-07 22:16:26 -070056 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Patrick McHardyfd841322005-08-20 17:38:40 -070058 if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
59 tcph->ece == einfo->proto.tcp.ece) &&
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070060 (!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
61 tcph->cwr == einfo->proto.tcp.cwr))
Jan Engelhardte1931b72007-07-07 22:16:26 -070062 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Herbert Xu3db05fe2007-10-15 00:53:15 -070064 if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph)))
Jan Engelhardte1931b72007-07-07 22:16:26 -070065 return false;
Herbert Xu3db05fe2007-10-15 00:53:15 -070066 tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Al Viro6a19d612006-09-28 14:22:24 -070068 oldval = ((__be16 *)tcph)[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (einfo->operation & IPT_ECN_OP_SET_ECE)
70 tcph->ece = einfo->proto.tcp.ece;
71 if (einfo->operation & IPT_ECN_OP_SET_CWR)
72 tcph->cwr = einfo->proto.tcp.cwr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110074 inet_proto_csum_replace2(&tcph->check, skb,
Tom Herbert4b048d62015-08-17 13:42:25 -070075 oldval, ((__be16 *)tcph)[6], false);
Jan Engelhardte1931b72007-07-07 22:16:26 -070076 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020080ecn_tg(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020082 const struct ipt_ECN_info *einfo = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 if (einfo->operation & IPT_ECN_OP_SET_IP)
Herbert Xu3db05fe2007-10-15 00:53:15 -070085 if (!set_ect_ip(skb, einfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return NF_DROP;
87
Joe Perches3666ed12009-11-23 23:17:06 +010088 if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR) &&
89 ip_hdr(skb)->protocol == IPPROTO_TCP)
Herbert Xu3db05fe2007-10-15 00:53:15 -070090 if (!set_ect_tcp(skb, einfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return NF_DROP;
92
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080093 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Jan Engelhardt135367b2010-03-19 17:16:42 +010096static int ecn_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020098 const struct ipt_ECN_info *einfo = par->targinfo;
99 const struct ipt_entry *e = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (einfo->operation & IPT_ECN_OP_MASK) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100102 pr_info("unsupported ECN operation %x\n", einfo->operation);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100106 pr_info("new ECT codepoint %x out of mask\n", einfo->ip_ect);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100107 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Joe Perches3666ed12009-11-23 23:17:06 +0100109 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)) &&
110 (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100111 pr_info("cannot use TCP operations on a non-tcp rule\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117static struct xt_target ecn_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .name = "ECN",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200119 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120 .target = ecn_tg,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800121 .targetsize = sizeof(struct ipt_ECN_info),
122 .table = "mangle",
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800123 .checkentry = ecn_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .me = THIS_MODULE,
125};
126
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127static int __init ecn_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800129 return xt_register_target(&ecn_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132static void __exit ecn_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134 xt_unregister_target(&ecn_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800137module_init(ecn_tg_init);
138module_exit(ecn_tg_exit);