| 1 | |
| 2 | |
| 3 | * Xtables module for matching the value of the IPv4/IPv6 and TCP ECN bits
|
| 4 | *
|
| 5 | * (C) 2002 by Harald Welte <[email protected]>
|
| 6 | * (C) 2011 Patrick McHardy <[email protected]>
|
| 7 |
|
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
| 9 | #include <linux/in.h>
|
| 10 | #include <linux/ip.h>
|
| 11 | #include <net/ip.h>
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/skbuff.h>
|
| 14 | #include <linux/tcp.h>
|
| 15 |
|
| 16 | #include <linux/netfilter/x_tables.h>
|
| 17 | #include <linux/netfilter/xt_ecn.h>
|
| 18 | #include <linux/netfilter_ipv4/ip_tables.h>
|
| 19 | #include <linux/netfilter_ipv6/ip6_tables.h>
|
| 20 |
|
| 21 | MODULE_AUTHOR("Harald Welte <[email protected]>");
|
| 22 | MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match");
|
| 23 | MODULE_LICENSE("GPL");
|
| 24 | MODULE_ALIAS("ipt_ecn");
|
| 25 | MODULE_ALIAS("ip6t_ecn");
|
| 26 |
|
| 27 | static bool match_tcp(const struct sk_buff *skb, struct xt_action_param *par)
|
| 28 | {
|
| 29 | const struct xt_ecn_info *einfo = par->matchinfo;
|
| 30 | struct tcphdr _tcph;
|
| 31 | const struct tcphdr *th;
|
| 32 |
|
| 33 |
|
| 34 | if (par->fragoff)
|
| 35 | return false;
|
| 36 |
|
| 37 | |
| 38 | * be good citizens.
|
| 39 |
|
| 40 | th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
|
| 41 | if (th == NULL)
|
| 42 | return false;
|
| 43 |
|
| 44 | if (einfo->operation & XT_ECN_OP_MATCH_ECE) {
|
| 45 | if (einfo->invert & XT_ECN_OP_MATCH_ECE) {
|
| 46 | if (th->ece == 1)
|
| 47 | return false;
|
| 48 | } else {
|
| 49 | if (th->ece == 0)
|
| 50 | return false;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | if (einfo->operation & XT_ECN_OP_MATCH_CWR) {
|
| 55 | if (einfo->invert & XT_ECN_OP_MATCH_CWR) {
|
| 56 | if (th->cwr == 1)
|
| 57 | return false;
|
| 58 | } else {
|
| 59 | if (th->cwr == 0)
|
| 60 | return false;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | return true;
|
| 65 | }
|
| 66 |
|
| 67 | static inline bool match_ip(const struct sk_buff *skb,
|
| 68 | const struct xt_ecn_info *einfo)
|
| 69 | {
|
| 70 | return ((ip_hdr(skb)->tos & XT_ECN_IP_MASK) == einfo->ip_ect) ^
|
| 71 | !!(einfo->invert & XT_ECN_OP_MATCH_IP);
|
| 72 | }
|
| 73 |
|
| 74 | static bool ecn_mt4(const struct sk_buff *skb, struct xt_action_param *par)
|
| 75 | {
|
| 76 | const struct xt_ecn_info *info = par->matchinfo;
|
| 77 |
|
| 78 | if (info->operation & XT_ECN_OP_MATCH_IP && !match_ip(skb, info))
|
| 79 | return false;
|
| 80 |
|
| 81 | if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
|
| 82 | !match_tcp(skb, par))
|
| 83 | return false;
|
| 84 |
|
| 85 | return true;
|
| 86 | }
|
| 87 |
|
| 88 | static int ecn_mt_check4(const struct xt_mtchk_param *par)
|
| 89 | {
|
| 90 | const struct xt_ecn_info *info = par->matchinfo;
|
| 91 | const struct ipt_ip *ip = par->entryinfo;
|
| 92 |
|
| 93 | if (info->operation & XT_ECN_OP_MATCH_MASK)
|
| 94 | return -EINVAL;
|
| 95 |
|
| 96 | if (info->invert & XT_ECN_OP_MATCH_MASK)
|
| 97 | return -EINVAL;
|
| 98 |
|
| 99 | if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
|
| 100 | (ip->proto != IPPROTO_TCP || ip->invflags & IPT_INV_PROTO)) {
|
| 101 | pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
|
| 102 | return -EINVAL;
|
| 103 | }
|
| 104 |
|
| 105 | return 0;
|
| 106 | }
|
| 107 |
|
| 108 | static inline bool match_ipv6(const struct sk_buff *skb,
|
| 109 | const struct xt_ecn_info *einfo)
|
| 110 | {
|
| 111 | return (((ipv6_hdr(skb)->flow_lbl[0] >> 4) & XT_ECN_IP_MASK) ==
|
| 112 | einfo->ip_ect) ^
|
| 113 | !!(einfo->invert & XT_ECN_OP_MATCH_IP);
|
| 114 | }
|
| 115 |
|
| 116 | static bool ecn_mt6(const struct sk_buff *skb, struct xt_action_param *par)
|
| 117 | {
|
| 118 | const struct xt_ecn_info *info = par->matchinfo;
|
| 119 |
|
| 120 | if (info->operation & XT_ECN_OP_MATCH_IP && !match_ipv6(skb, info))
|
| 121 | return false;
|
| 122 |
|
| 123 | if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
|
| 124 | !match_tcp(skb, par))
|
| 125 | return false;
|
| 126 |
|
| 127 | return true;
|
| 128 | }
|
| 129 |
|
| 130 | static int ecn_mt_check6(const struct xt_mtchk_param *par)
|
| 131 | {
|
| 132 | const struct xt_ecn_info *info = par->matchinfo;
|
| 133 | const struct ip6t_ip6 *ip = par->entryinfo;
|
| 134 |
|
| 135 | if (info->operation & XT_ECN_OP_MATCH_MASK)
|
| 136 | return -EINVAL;
|
| 137 |
|
| 138 | if (info->invert & XT_ECN_OP_MATCH_MASK)
|
| 139 | return -EINVAL;
|
| 140 |
|
| 141 | if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
|
| 142 | (ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
|
| 143 | pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
|
| 144 | return -EINVAL;
|
| 145 | }
|
| 146 |
|
| 147 | return 0;
|
| 148 | }
|
| 149 |
|
| 150 | static struct xt_match ecn_mt_reg[] __read_mostly = {
|
| 151 | {
|
| 152 | .name = "ecn",
|
| 153 | .family = NFPROTO_IPV4,
|
| 154 | .match = ecn_mt4,
|
| 155 | .matchsize = sizeof(struct xt_ecn_info),
|
| 156 | .checkentry = ecn_mt_check4,
|
| 157 | .me = THIS_MODULE,
|
| 158 | },
|
| 159 | {
|
| 160 | .name = "ecn",
|
| 161 | .family = NFPROTO_IPV6,
|
| 162 | .match = ecn_mt6,
|
| 163 | .matchsize = sizeof(struct xt_ecn_info),
|
| 164 | .checkentry = ecn_mt_check6,
|
| 165 | .me = THIS_MODULE,
|
| 166 | },
|
| 167 | };
|
| 168 |
|
| 169 | static int __init ecn_mt_init(void)
|
| 170 | {
|
| 171 | return xt_register_matches(ecn_mt_reg, ARRAY_SIZE(ecn_mt_reg));
|
| 172 | }
|
| 173 |
|
| 174 | static void __exit ecn_mt_exit(void)
|
| 175 | {
|
| 176 | xt_unregister_matches(ecn_mt_reg, ARRAY_SIZE(ecn_mt_reg));
|
| 177 | }
|
| 178 |
|
| 179 | module_init(ecn_mt_init);
|
| 180 | module_exit(ecn_mt_exit);
|