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