1 // SPDX-License-Identifier: GPL-2.0-only
2 /* iptables module for the IPv4 and TCP ECN bits, Version 1.5
4 * (C) 2002 by Harald Welte <laforge@netfilter.org>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
12 #include <linux/tcp.h>
13 #include <net/checksum.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter_ipv4/ipt_ECN.h>
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
21 MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag modification");
23 /* set ECT codepoint from IP header.
24 * return false if there was an error. */
26 set_ect_ip(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
28 struct iphdr *iph = ip_hdr(skb);
30 if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
32 if (!skb_make_writable(skb, sizeof(struct iphdr)))
36 iph->tos &= ~IPT_ECN_IP_MASK;
37 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
38 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
43 /* Return false if there was an error. */
45 set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
47 struct tcphdr _tcph, *tcph;
50 /* Not enough header? */
51 tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
55 if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
56 tcph->ece == einfo->proto.tcp.ece) &&
57 (!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
58 tcph->cwr == einfo->proto.tcp.cwr))
61 if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph)))
63 tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb);
65 oldval = ((__be16 *)tcph)[6];
66 if (einfo->operation & IPT_ECN_OP_SET_ECE)
67 tcph->ece = einfo->proto.tcp.ece;
68 if (einfo->operation & IPT_ECN_OP_SET_CWR)
69 tcph->cwr = einfo->proto.tcp.cwr;
71 inet_proto_csum_replace2(&tcph->check, skb,
72 oldval, ((__be16 *)tcph)[6], false);
77 ecn_tg(struct sk_buff *skb, const struct xt_action_param *par)
79 const struct ipt_ECN_info *einfo = par->targinfo;
81 if (einfo->operation & IPT_ECN_OP_SET_IP)
82 if (!set_ect_ip(skb, einfo))
85 if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR) &&
86 ip_hdr(skb)->protocol == IPPROTO_TCP)
87 if (!set_ect_tcp(skb, einfo))
93 static int ecn_tg_check(const struct xt_tgchk_param *par)
95 const struct ipt_ECN_info *einfo = par->targinfo;
96 const struct ipt_entry *e = par->entryinfo;
98 if (einfo->operation & IPT_ECN_OP_MASK)
101 if (einfo->ip_ect & ~IPT_ECN_IP_MASK)
104 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)) &&
105 (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
106 pr_info_ratelimited("cannot use operation on non-tcp rule\n");
112 static struct xt_target ecn_tg_reg __read_mostly = {
114 .family = NFPROTO_IPV4,
116 .targetsize = sizeof(struct ipt_ECN_info),
118 .checkentry = ecn_tg_check,
122 static int __init ecn_tg_init(void)
124 return xt_register_target(&ecn_tg_reg);
127 static void __exit ecn_tg_exit(void)
129 xt_unregister_target(&ecn_tg_reg);
132 module_init(ecn_tg_init);
133 module_exit(ecn_tg_exit);