Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 as |
| 5 | * published by the Free Software Foundation. |
| 6 | */ |
| 7 | |
| 8 | /* Kernel module implementing an IP set type: the hash:ip,port type */ |
| 9 | |
| 10 | #include <linux/jhash.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/ip.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/errno.h> |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 15 | #include <linux/random.h> |
| 16 | #include <net/ip.h> |
| 17 | #include <net/ipv6.h> |
| 18 | #include <net/netlink.h> |
| 19 | #include <net/tcp.h> |
| 20 | |
| 21 | #include <linux/netfilter.h> |
| 22 | #include <linux/netfilter/ipset/pfxlen.h> |
| 23 | #include <linux/netfilter/ipset/ip_set.h> |
| 24 | #include <linux/netfilter/ipset/ip_set_timeout.h> |
| 25 | #include <linux/netfilter/ipset/ip_set_getport.h> |
| 26 | #include <linux/netfilter/ipset/ip_set_hash.h> |
| 27 | |
Jozsef Kadlecsik | 10111a6 | 2012-09-21 21:59:32 +0200 | [diff] [blame] | 28 | #define REVISION_MIN 0 |
| 29 | #define REVISION_MAX 1 /* SCTP and UDPLITE support added */ |
| 30 | |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); |
Jozsef Kadlecsik | 10111a6 | 2012-09-21 21:59:32 +0200 | [diff] [blame] | 33 | IP_SET_MODULE_DESC("hash:ip,port", REVISION_MIN, REVISION_MAX); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 34 | MODULE_ALIAS("ip_set_hash:ip,port"); |
| 35 | |
| 36 | /* Type specific function prefix */ |
| 37 | #define TYPE hash_ipport |
| 38 | |
| 39 | static bool |
| 40 | hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b); |
| 41 | |
| 42 | #define hash_ipport4_same_set hash_ipport_same_set |
| 43 | #define hash_ipport6_same_set hash_ipport_same_set |
| 44 | |
| 45 | /* The type variant functions: IPv4 */ |
| 46 | |
| 47 | /* Member elements without timeout */ |
| 48 | struct hash_ipport4_elem { |
| 49 | __be32 ip; |
| 50 | __be16 port; |
| 51 | u8 proto; |
| 52 | u8 padding; |
| 53 | }; |
| 54 | |
| 55 | /* Member elements with timeout support */ |
| 56 | struct hash_ipport4_telem { |
| 57 | __be32 ip; |
| 58 | __be16 port; |
| 59 | u8 proto; |
| 60 | u8 padding; |
| 61 | unsigned long timeout; |
| 62 | }; |
| 63 | |
| 64 | static inline bool |
| 65 | hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1, |
Jozsef Kadlecsik | 89dc79b | 2011-07-21 12:06:18 +0200 | [diff] [blame] | 66 | const struct hash_ipport4_elem *ip2, |
| 67 | u32 *multi) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 68 | { |
| 69 | return ip1->ip == ip2->ip && |
| 70 | ip1->port == ip2->port && |
| 71 | ip1->proto == ip2->proto; |
| 72 | } |
| 73 | |
| 74 | static inline bool |
| 75 | hash_ipport4_data_isnull(const struct hash_ipport4_elem *elem) |
| 76 | { |
| 77 | return elem->proto == 0; |
| 78 | } |
| 79 | |
| 80 | static inline void |
| 81 | hash_ipport4_data_copy(struct hash_ipport4_elem *dst, |
| 82 | const struct hash_ipport4_elem *src) |
| 83 | { |
| 84 | dst->ip = src->ip; |
| 85 | dst->port = src->port; |
| 86 | dst->proto = src->proto; |
| 87 | } |
| 88 | |
| 89 | static inline void |
| 90 | hash_ipport4_data_zero_out(struct hash_ipport4_elem *elem) |
| 91 | { |
| 92 | elem->proto = 0; |
| 93 | } |
| 94 | |
| 95 | static bool |
| 96 | hash_ipport4_data_list(struct sk_buff *skb, |
| 97 | const struct hash_ipport4_elem *data) |
| 98 | { |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 99 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
| 100 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || |
| 101 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto)) |
| 102 | goto nla_put_failure; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 103 | return 0; |
| 104 | |
| 105 | nla_put_failure: |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static bool |
| 110 | hash_ipport4_data_tlist(struct sk_buff *skb, |
| 111 | const struct hash_ipport4_elem *data) |
| 112 | { |
| 113 | const struct hash_ipport4_telem *tdata = |
| 114 | (const struct hash_ipport4_telem *)data; |
| 115 | |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 116 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) || |
| 117 | nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) || |
| 118 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || |
| 119 | nla_put_net32(skb, IPSET_ATTR_TIMEOUT, |
| 120 | htonl(ip_set_timeout_get(tdata->timeout)))) |
| 121 | goto nla_put_failure; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 122 | return 0; |
| 123 | |
| 124 | nla_put_failure: |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | #define PF 4 |
| 129 | #define HOST_MASK 32 |
| 130 | #include <linux/netfilter/ipset/ip_set_ahash.h> |
| 131 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 132 | static inline void |
| 133 | hash_ipport4_data_next(struct ip_set_hash *h, |
| 134 | const struct hash_ipport4_elem *d) |
| 135 | { |
Jozsef Kadlecsik | 6e27c9b | 2012-09-21 21:44:58 +0200 | [diff] [blame] | 136 | h->next.ip = d->ip; |
| 137 | h->next.port = d->port; |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 140 | static int |
| 141 | hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 142 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 143 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 144 | { |
| 145 | const struct ip_set_hash *h = set->data; |
| 146 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 147 | struct hash_ipport4_elem data = { }; |
| 148 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 149 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 150 | &data.port, &data.proto)) |
| 151 | return -EINVAL; |
| 152 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 153 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 154 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 155 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int |
| 159 | hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 160 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 161 | { |
| 162 | const struct ip_set_hash *h = set->data; |
| 163 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 164 | struct hash_ipport4_elem data = { }; |
Jozsef Kadlecsik | 648ae8e | 2011-11-09 19:37:35 +0100 | [diff] [blame] | 165 | u32 ip, ip_to = 0, p = 0, port, port_to; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 166 | u32 timeout = h->timeout; |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 167 | bool with_ports = false; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | if (unlikely(!tb[IPSET_ATTR_IP] || |
| 171 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || |
| 172 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || |
| 173 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) |
| 174 | return -IPSET_ERR_PROTOCOL; |
| 175 | |
| 176 | if (tb[IPSET_ATTR_LINENO]) |
| 177 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 178 | |
| 179 | ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | if (tb[IPSET_ATTR_PORT]) |
| 184 | data.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
| 185 | else |
| 186 | return -IPSET_ERR_PROTOCOL; |
| 187 | |
| 188 | if (tb[IPSET_ATTR_PROTO]) { |
| 189 | data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 190 | with_ports = ip_set_proto_with_ports(data.proto); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 191 | |
| 192 | if (data.proto == 0) |
| 193 | return -IPSET_ERR_INVALID_PROTO; |
| 194 | } else |
| 195 | return -IPSET_ERR_MISSING_PROTO; |
| 196 | |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 197 | if (!(with_ports || data.proto == IPPROTO_ICMP)) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 198 | data.port = 0; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 199 | |
| 200 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 201 | if (!with_timeout(h->timeout)) |
| 202 | return -IPSET_ERR_TIMEOUT; |
| 203 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 204 | } |
| 205 | |
| 206 | if (adt == IPSET_TEST || |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 207 | !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] || |
| 208 | tb[IPSET_ATTR_PORT_TO])) { |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 209 | ret = adtfn(set, &data, timeout, flags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 210 | return ip_set_eexist(ret, flags) ? 0 : ret; |
| 211 | } |
| 212 | |
| 213 | ip = ntohl(data.ip); |
| 214 | if (tb[IPSET_ATTR_IP_TO]) { |
| 215 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | if (ip > ip_to) |
| 219 | swap(ip, ip_to); |
| 220 | } else if (tb[IPSET_ATTR_CIDR]) { |
| 221 | u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
| 222 | |
Jozsef Kadlecsik | b9fed74 | 2012-09-04 17:45:59 +0200 | [diff] [blame] | 223 | if (!cidr || cidr > 32) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 224 | return -IPSET_ERR_INVALID_CIDR; |
Jozsef Kadlecsik | e6146e8 | 2011-06-16 18:55:58 +0200 | [diff] [blame] | 225 | ip_set_mask_from_to(ip, ip_to, cidr); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 226 | } else |
| 227 | ip_to = ip; |
| 228 | |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 229 | port_to = port = ntohs(data.port); |
| 230 | if (with_ports && tb[IPSET_ATTR_PORT_TO]) { |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 231 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
| 232 | if (port > port_to) |
| 233 | swap(port, port_to); |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 234 | } |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 235 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 236 | if (retried) |
Jozsef Kadlecsik | 6e27c9b | 2012-09-21 21:44:58 +0200 | [diff] [blame] | 237 | ip = ntohl(h->next.ip); |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 238 | for (; !before(ip_to, ip); ip++) { |
Jozsef Kadlecsik | 6e27c9b | 2012-09-21 21:44:58 +0200 | [diff] [blame] | 239 | p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port) |
| 240 | : port; |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 241 | for (; p <= port_to; p++) { |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 242 | data.ip = htonl(ip); |
| 243 | data.port = htons(p); |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 244 | ret = adtfn(set, &data, timeout, flags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 245 | |
| 246 | if (ret && !ip_set_eexist(ret, flags)) |
| 247 | return ret; |
| 248 | else |
| 249 | ret = 0; |
| 250 | } |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 251 | } |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | static bool |
| 256 | hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b) |
| 257 | { |
| 258 | const struct ip_set_hash *x = a->data; |
| 259 | const struct ip_set_hash *y = b->data; |
| 260 | |
| 261 | /* Resizing changes htable_bits, so we ignore it */ |
| 262 | return x->maxelem == y->maxelem && |
| 263 | x->timeout == y->timeout; |
| 264 | } |
| 265 | |
| 266 | /* The type variant functions: IPv6 */ |
| 267 | |
| 268 | struct hash_ipport6_elem { |
| 269 | union nf_inet_addr ip; |
| 270 | __be16 port; |
| 271 | u8 proto; |
| 272 | u8 padding; |
| 273 | }; |
| 274 | |
| 275 | struct hash_ipport6_telem { |
| 276 | union nf_inet_addr ip; |
| 277 | __be16 port; |
| 278 | u8 proto; |
| 279 | u8 padding; |
| 280 | unsigned long timeout; |
| 281 | }; |
| 282 | |
| 283 | static inline bool |
| 284 | hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1, |
Jozsef Kadlecsik | 89dc79b | 2011-07-21 12:06:18 +0200 | [diff] [blame] | 285 | const struct hash_ipport6_elem *ip2, |
| 286 | u32 *multi) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 287 | { |
| 288 | return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 && |
| 289 | ip1->port == ip2->port && |
| 290 | ip1->proto == ip2->proto; |
| 291 | } |
| 292 | |
| 293 | static inline bool |
| 294 | hash_ipport6_data_isnull(const struct hash_ipport6_elem *elem) |
| 295 | { |
| 296 | return elem->proto == 0; |
| 297 | } |
| 298 | |
| 299 | static inline void |
| 300 | hash_ipport6_data_copy(struct hash_ipport6_elem *dst, |
| 301 | const struct hash_ipport6_elem *src) |
| 302 | { |
| 303 | memcpy(dst, src, sizeof(*dst)); |
| 304 | } |
| 305 | |
| 306 | static inline void |
| 307 | hash_ipport6_data_zero_out(struct hash_ipport6_elem *elem) |
| 308 | { |
| 309 | elem->proto = 0; |
| 310 | } |
| 311 | |
| 312 | static bool |
| 313 | hash_ipport6_data_list(struct sk_buff *skb, |
| 314 | const struct hash_ipport6_elem *data) |
| 315 | { |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 316 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || |
| 317 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || |
| 318 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto)) |
| 319 | goto nla_put_failure; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | nla_put_failure: |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | static bool |
| 327 | hash_ipport6_data_tlist(struct sk_buff *skb, |
| 328 | const struct hash_ipport6_elem *data) |
| 329 | { |
| 330 | const struct hash_ipport6_telem *e = |
| 331 | (const struct hash_ipport6_telem *)data; |
| 332 | |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 333 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) || |
| 334 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || |
| 335 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || |
| 336 | nla_put_net32(skb, IPSET_ATTR_TIMEOUT, |
| 337 | htonl(ip_set_timeout_get(e->timeout)))) |
| 338 | goto nla_put_failure; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | nla_put_failure: |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | #undef PF |
| 346 | #undef HOST_MASK |
| 347 | |
| 348 | #define PF 6 |
| 349 | #define HOST_MASK 128 |
| 350 | #include <linux/netfilter/ipset/ip_set_ahash.h> |
| 351 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 352 | static inline void |
| 353 | hash_ipport6_data_next(struct ip_set_hash *h, |
| 354 | const struct hash_ipport6_elem *d) |
| 355 | { |
Jozsef Kadlecsik | 6e27c9b | 2012-09-21 21:44:58 +0200 | [diff] [blame] | 356 | h->next.port = d->port; |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 359 | static int |
| 360 | hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 361 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 362 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 363 | { |
| 364 | const struct ip_set_hash *h = set->data; |
| 365 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 366 | struct hash_ipport6_elem data = { }; |
| 367 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 368 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 369 | &data.port, &data.proto)) |
| 370 | return -EINVAL; |
| 371 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 372 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 373 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 374 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int |
| 378 | hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 379 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 380 | { |
| 381 | const struct ip_set_hash *h = set->data; |
| 382 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 383 | struct hash_ipport6_elem data = { }; |
| 384 | u32 port, port_to; |
| 385 | u32 timeout = h->timeout; |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 386 | bool with_ports = false; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 387 | int ret; |
| 388 | |
| 389 | if (unlikely(!tb[IPSET_ATTR_IP] || |
| 390 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || |
| 391 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || |
| 392 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
| 393 | tb[IPSET_ATTR_IP_TO] || |
| 394 | tb[IPSET_ATTR_CIDR])) |
| 395 | return -IPSET_ERR_PROTOCOL; |
| 396 | |
| 397 | if (tb[IPSET_ATTR_LINENO]) |
| 398 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 399 | |
| 400 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | |
| 404 | if (tb[IPSET_ATTR_PORT]) |
| 405 | data.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
| 406 | else |
| 407 | return -IPSET_ERR_PROTOCOL; |
| 408 | |
| 409 | if (tb[IPSET_ATTR_PROTO]) { |
| 410 | data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 411 | with_ports = ip_set_proto_with_ports(data.proto); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 412 | |
| 413 | if (data.proto == 0) |
| 414 | return -IPSET_ERR_INVALID_PROTO; |
| 415 | } else |
| 416 | return -IPSET_ERR_MISSING_PROTO; |
| 417 | |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 418 | if (!(with_ports || data.proto == IPPROTO_ICMPV6)) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 419 | data.port = 0; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 420 | |
| 421 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 422 | if (!with_timeout(h->timeout)) |
| 423 | return -IPSET_ERR_TIMEOUT; |
| 424 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 425 | } |
| 426 | |
Jozsef Kadlecsik | 5e0c1eb | 2011-03-20 15:33:26 +0100 | [diff] [blame] | 427 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 428 | ret = adtfn(set, &data, timeout, flags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 429 | return ip_set_eexist(ret, flags) ? 0 : ret; |
| 430 | } |
| 431 | |
| 432 | port = ntohs(data.port); |
| 433 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
| 434 | if (port > port_to) |
| 435 | swap(port, port_to); |
| 436 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 437 | if (retried) |
Jozsef Kadlecsik | 6e27c9b | 2012-09-21 21:44:58 +0200 | [diff] [blame] | 438 | port = ntohs(h->next.port); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 439 | for (; port <= port_to; port++) { |
| 440 | data.port = htons(port); |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 441 | ret = adtfn(set, &data, timeout, flags); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 442 | |
| 443 | if (ret && !ip_set_eexist(ret, flags)) |
| 444 | return ret; |
| 445 | else |
| 446 | ret = 0; |
| 447 | } |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* Create hash:ip type of sets */ |
| 452 | |
| 453 | static int |
| 454 | hash_ipport_create(struct ip_set *set, struct nlattr *tb[], u32 flags) |
| 455 | { |
| 456 | struct ip_set_hash *h; |
| 457 | u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM; |
| 458 | u8 hbits; |
Jozsef Kadlecsik | 26a5d3c | 2012-05-14 01:47:01 +0000 | [diff] [blame] | 459 | size_t hsize; |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 460 | |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 461 | if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6)) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 462 | return -IPSET_ERR_INVALID_FAMILY; |
| 463 | |
| 464 | if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) || |
| 465 | !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) || |
| 466 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) |
| 467 | return -IPSET_ERR_PROTOCOL; |
| 468 | |
| 469 | if (tb[IPSET_ATTR_HASHSIZE]) { |
| 470 | hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]); |
| 471 | if (hashsize < IPSET_MIMINAL_HASHSIZE) |
| 472 | hashsize = IPSET_MIMINAL_HASHSIZE; |
| 473 | } |
| 474 | |
| 475 | if (tb[IPSET_ATTR_MAXELEM]) |
| 476 | maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]); |
| 477 | |
| 478 | h = kzalloc(sizeof(*h), GFP_KERNEL); |
| 479 | if (!h) |
| 480 | return -ENOMEM; |
| 481 | |
| 482 | h->maxelem = maxelem; |
| 483 | get_random_bytes(&h->initval, sizeof(h->initval)); |
| 484 | h->timeout = IPSET_NO_TIMEOUT; |
| 485 | |
| 486 | hbits = htable_bits(hashsize); |
Jozsef Kadlecsik | 26a5d3c | 2012-05-14 01:47:01 +0000 | [diff] [blame] | 487 | hsize = htable_size(hbits); |
| 488 | if (hsize == 0) { |
| 489 | kfree(h); |
| 490 | return -ENOMEM; |
| 491 | } |
| 492 | h->table = ip_set_alloc(hsize); |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 493 | if (!h->table) { |
| 494 | kfree(h); |
| 495 | return -ENOMEM; |
| 496 | } |
| 497 | h->table->htable_bits = hbits; |
| 498 | |
| 499 | set->data = h; |
| 500 | |
| 501 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 502 | h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 503 | |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 504 | set->variant = set->family == NFPROTO_IPV4 |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 505 | ? &hash_ipport4_tvariant : &hash_ipport6_tvariant; |
| 506 | |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 507 | if (set->family == NFPROTO_IPV4) |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 508 | hash_ipport4_gc_init(set); |
| 509 | else |
| 510 | hash_ipport6_gc_init(set); |
| 511 | } else { |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 512 | set->variant = set->family == NFPROTO_IPV4 |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 513 | ? &hash_ipport4_variant : &hash_ipport6_variant; |
| 514 | } |
| 515 | |
| 516 | pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n", |
| 517 | set->name, jhash_size(h->table->htable_bits), |
| 518 | h->table->htable_bits, h->maxelem, set->data, h->table); |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static struct ip_set_type hash_ipport_type __read_mostly = { |
| 524 | .name = "hash:ip,port", |
| 525 | .protocol = IPSET_PROTOCOL, |
| 526 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT, |
| 527 | .dimension = IPSET_DIM_TWO, |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 528 | .family = NFPROTO_UNSPEC, |
Jozsef Kadlecsik | 10111a6 | 2012-09-21 21:59:32 +0200 | [diff] [blame] | 529 | .revision_min = REVISION_MIN, |
| 530 | .revision_max = REVISION_MAX, |
Jozsef Kadlecsik | 07896ed | 2011-02-01 15:39:52 +0100 | [diff] [blame] | 531 | .create = hash_ipport_create, |
| 532 | .create_policy = { |
| 533 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, |
| 534 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, |
| 535 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, |
| 536 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, |
| 537 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, |
| 538 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 539 | }, |
| 540 | .adt_policy = { |
| 541 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, |
| 542 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
| 543 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, |
| 544 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, |
| 545 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
| 546 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, |
| 547 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 548 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, |
| 549 | }, |
| 550 | .me = THIS_MODULE, |
| 551 | }; |
| 552 | |
| 553 | static int __init |
| 554 | hash_ipport_init(void) |
| 555 | { |
| 556 | return ip_set_type_register(&hash_ipport_type); |
| 557 | } |
| 558 | |
| 559 | static void __exit |
| 560 | hash_ipport_fini(void) |
| 561 | { |
| 562 | ip_set_type_unregister(&hash_ipport_type); |
| 563 | } |
| 564 | |
| 565 | module_init(hash_ipport_init); |
| 566 | module_exit(hash_ipport_fini); |