Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +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:net 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 | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 15 | #include <linux/random.h> |
| 16 | #include <net/ip.h> |
| 17 | #include <net/ipv6.h> |
| 18 | #include <net/netlink.h> |
| 19 | |
| 20 | #include <linux/netfilter.h> |
| 21 | #include <linux/netfilter/ipset/pfxlen.h> |
| 22 | #include <linux/netfilter/ipset/ip_set.h> |
| 23 | #include <linux/netfilter/ipset/ip_set_timeout.h> |
| 24 | #include <linux/netfilter/ipset/ip_set_hash.h> |
| 25 | |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); |
| 28 | MODULE_DESCRIPTION("hash:net type of IP sets"); |
| 29 | MODULE_ALIAS("ip_set_hash:net"); |
| 30 | |
| 31 | /* Type specific function prefix */ |
| 32 | #define TYPE hash_net |
| 33 | |
| 34 | static bool |
| 35 | hash_net_same_set(const struct ip_set *a, const struct ip_set *b); |
| 36 | |
| 37 | #define hash_net4_same_set hash_net_same_set |
| 38 | #define hash_net6_same_set hash_net_same_set |
| 39 | |
| 40 | /* The type variant functions: IPv4 */ |
| 41 | |
| 42 | /* Member elements without timeout */ |
| 43 | struct hash_net4_elem { |
| 44 | __be32 ip; |
| 45 | u16 padding0; |
| 46 | u8 padding1; |
| 47 | u8 cidr; |
| 48 | }; |
| 49 | |
| 50 | /* Member elements with timeout support */ |
| 51 | struct hash_net4_telem { |
| 52 | __be32 ip; |
| 53 | u16 padding0; |
| 54 | u8 padding1; |
| 55 | u8 cidr; |
| 56 | unsigned long timeout; |
| 57 | }; |
| 58 | |
| 59 | static inline bool |
| 60 | hash_net4_data_equal(const struct hash_net4_elem *ip1, |
| 61 | const struct hash_net4_elem *ip2) |
| 62 | { |
| 63 | return ip1->ip == ip2->ip && ip1->cidr == ip2->cidr; |
| 64 | } |
| 65 | |
| 66 | static inline bool |
| 67 | hash_net4_data_isnull(const struct hash_net4_elem *elem) |
| 68 | { |
| 69 | return elem->cidr == 0; |
| 70 | } |
| 71 | |
| 72 | static inline void |
| 73 | hash_net4_data_copy(struct hash_net4_elem *dst, |
| 74 | const struct hash_net4_elem *src) |
| 75 | { |
| 76 | dst->ip = src->ip; |
| 77 | dst->cidr = src->cidr; |
| 78 | } |
| 79 | |
| 80 | static inline void |
| 81 | hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr) |
| 82 | { |
| 83 | elem->ip &= ip_set_netmask(cidr); |
| 84 | elem->cidr = cidr; |
| 85 | } |
| 86 | |
| 87 | /* Zero CIDR values cannot be stored */ |
| 88 | static inline void |
| 89 | hash_net4_data_zero_out(struct hash_net4_elem *elem) |
| 90 | { |
| 91 | elem->cidr = 0; |
| 92 | } |
| 93 | |
| 94 | static bool |
| 95 | hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data) |
| 96 | { |
| 97 | NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip); |
| 98 | NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr); |
| 99 | return 0; |
| 100 | |
| 101 | nla_put_failure: |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | static bool |
| 106 | hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data) |
| 107 | { |
| 108 | const struct hash_net4_telem *tdata = |
| 109 | (const struct hash_net4_telem *)data; |
| 110 | |
| 111 | NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip); |
| 112 | NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr); |
| 113 | NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, |
| 114 | htonl(ip_set_timeout_get(tdata->timeout))); |
| 115 | |
| 116 | return 0; |
| 117 | |
| 118 | nla_put_failure: |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | #define IP_SET_HASH_WITH_NETS |
| 123 | |
| 124 | #define PF 4 |
| 125 | #define HOST_MASK 32 |
| 126 | #include <linux/netfilter/ipset/ip_set_ahash.h> |
| 127 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 128 | static inline void |
| 129 | hash_net4_data_next(struct ip_set_hash *h, |
| 130 | const struct hash_net4_elem *d) |
| 131 | { |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 132 | h->next.ip = ntohl(d->ip); |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 135 | static int |
| 136 | hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 137 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 138 | { |
| 139 | const struct ip_set_hash *h = set->data; |
| 140 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 141 | struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK }; |
| 142 | |
| 143 | if (data.cidr == 0) |
| 144 | return -EINVAL; |
| 145 | if (adt == IPSET_TEST) |
| 146 | data.cidr = HOST_MASK; |
| 147 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 148 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 149 | data.ip &= ip_set_netmask(data.cidr); |
| 150 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 151 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int |
| 155 | hash_net4_uadt(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 156 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 157 | { |
| 158 | const struct ip_set_hash *h = set->data; |
| 159 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 160 | struct hash_net4_elem data = { .cidr = HOST_MASK }; |
| 161 | u32 timeout = h->timeout; |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 162 | u32 ip = 0, ip_to, last; |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 163 | int ret; |
| 164 | |
| 165 | if (unlikely(!tb[IPSET_ATTR_IP] || |
| 166 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) |
| 167 | return -IPSET_ERR_PROTOCOL; |
| 168 | |
| 169 | if (tb[IPSET_ATTR_LINENO]) |
| 170 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 171 | |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 172 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 173 | if (ret) |
| 174 | return ret; |
| 175 | |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 176 | if (tb[IPSET_ATTR_CIDR]) { |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 177 | data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 178 | if (!data.cidr) |
| 179 | return -IPSET_ERR_INVALID_CIDR; |
| 180 | } |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 181 | |
| 182 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 183 | if (!with_timeout(h->timeout)) |
| 184 | return -IPSET_ERR_TIMEOUT; |
| 185 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 186 | } |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 187 | |
| 188 | if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) { |
| 189 | data.ip = htonl(ip & ip_set_hostmask(data.cidr)); |
| 190 | ret = adtfn(set, &data, timeout, flags); |
| 191 | return ip_set_eexist(ret, flags) ? 0 : ret; |
| 192 | } |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 193 | |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 194 | ip_to = ip; |
| 195 | if (tb[IPSET_ATTR_IP_TO]) { |
| 196 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | if (ip_to < ip) |
| 200 | swap(ip, ip_to); |
| 201 | if (ip + UINT_MAX == ip_to) |
| 202 | return -IPSET_ERR_HASH_RANGE; |
| 203 | } |
| 204 | if (retried) |
| 205 | ip = h->next.ip; |
| 206 | while (!after(ip, ip_to)) { |
| 207 | data.ip = htonl(ip); |
| 208 | last = ip_set_range_to_cidr(ip, ip_to, &data.cidr); |
| 209 | ret = adtfn(set, &data, timeout, flags); |
| 210 | if (ret && !ip_set_eexist(ret, flags)) |
| 211 | return ret; |
| 212 | else |
| 213 | ret = 0; |
| 214 | ip = last + 1; |
| 215 | } |
| 216 | return ret; |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static bool |
| 220 | hash_net_same_set(const struct ip_set *a, const struct ip_set *b) |
| 221 | { |
| 222 | const struct ip_set_hash *x = a->data; |
| 223 | const struct ip_set_hash *y = b->data; |
| 224 | |
| 225 | /* Resizing changes htable_bits, so we ignore it */ |
| 226 | return x->maxelem == y->maxelem && |
| 227 | x->timeout == y->timeout; |
| 228 | } |
| 229 | |
| 230 | /* The type variant functions: IPv6 */ |
| 231 | |
| 232 | struct hash_net6_elem { |
| 233 | union nf_inet_addr ip; |
| 234 | u16 padding0; |
| 235 | u8 padding1; |
| 236 | u8 cidr; |
| 237 | }; |
| 238 | |
| 239 | struct hash_net6_telem { |
| 240 | union nf_inet_addr ip; |
| 241 | u16 padding0; |
| 242 | u8 padding1; |
| 243 | u8 cidr; |
| 244 | unsigned long timeout; |
| 245 | }; |
| 246 | |
| 247 | static inline bool |
| 248 | hash_net6_data_equal(const struct hash_net6_elem *ip1, |
| 249 | const struct hash_net6_elem *ip2) |
| 250 | { |
| 251 | return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 && |
| 252 | ip1->cidr == ip2->cidr; |
| 253 | } |
| 254 | |
| 255 | static inline bool |
| 256 | hash_net6_data_isnull(const struct hash_net6_elem *elem) |
| 257 | { |
| 258 | return elem->cidr == 0; |
| 259 | } |
| 260 | |
| 261 | static inline void |
| 262 | hash_net6_data_copy(struct hash_net6_elem *dst, |
| 263 | const struct hash_net6_elem *src) |
| 264 | { |
| 265 | ipv6_addr_copy(&dst->ip.in6, &src->ip.in6); |
| 266 | dst->cidr = src->cidr; |
| 267 | } |
| 268 | |
| 269 | static inline void |
| 270 | hash_net6_data_zero_out(struct hash_net6_elem *elem) |
| 271 | { |
| 272 | elem->cidr = 0; |
| 273 | } |
| 274 | |
| 275 | static inline void |
| 276 | ip6_netmask(union nf_inet_addr *ip, u8 prefix) |
| 277 | { |
| 278 | ip->ip6[0] &= ip_set_netmask6(prefix)[0]; |
| 279 | ip->ip6[1] &= ip_set_netmask6(prefix)[1]; |
| 280 | ip->ip6[2] &= ip_set_netmask6(prefix)[2]; |
| 281 | ip->ip6[3] &= ip_set_netmask6(prefix)[3]; |
| 282 | } |
| 283 | |
| 284 | static inline void |
| 285 | hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr) |
| 286 | { |
| 287 | ip6_netmask(&elem->ip, cidr); |
| 288 | elem->cidr = cidr; |
| 289 | } |
| 290 | |
| 291 | static bool |
| 292 | hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data) |
| 293 | { |
| 294 | NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip); |
| 295 | NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr); |
| 296 | return 0; |
| 297 | |
| 298 | nla_put_failure: |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | static bool |
| 303 | hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data) |
| 304 | { |
| 305 | const struct hash_net6_telem *e = |
| 306 | (const struct hash_net6_telem *)data; |
| 307 | |
| 308 | NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip); |
| 309 | NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr); |
| 310 | NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, |
| 311 | htonl(ip_set_timeout_get(e->timeout))); |
| 312 | return 0; |
| 313 | |
| 314 | nla_put_failure: |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | #undef PF |
| 319 | #undef HOST_MASK |
| 320 | |
| 321 | #define PF 6 |
| 322 | #define HOST_MASK 128 |
| 323 | #include <linux/netfilter/ipset/ip_set_ahash.h> |
| 324 | |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 325 | static inline void |
| 326 | hash_net6_data_next(struct ip_set_hash *h, |
| 327 | const struct hash_net6_elem *d) |
| 328 | { |
| 329 | } |
| 330 | |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 331 | static int |
| 332 | hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 333 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 334 | { |
| 335 | const struct ip_set_hash *h = set->data; |
| 336 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 337 | struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK }; |
| 338 | |
| 339 | if (data.cidr == 0) |
| 340 | return -EINVAL; |
| 341 | if (adt == IPSET_TEST) |
| 342 | data.cidr = HOST_MASK; |
| 343 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 344 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 345 | ip6_netmask(&data.ip, data.cidr); |
| 346 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 347 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int |
| 351 | hash_net6_uadt(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 352 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 353 | { |
| 354 | const struct ip_set_hash *h = set->data; |
| 355 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 356 | struct hash_net6_elem data = { .cidr = HOST_MASK }; |
| 357 | u32 timeout = h->timeout; |
| 358 | int ret; |
| 359 | |
| 360 | if (unlikely(!tb[IPSET_ATTR_IP] || |
| 361 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) |
| 362 | return -IPSET_ERR_PROTOCOL; |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 363 | if (unlikely(tb[IPSET_ATTR_IP_TO])) |
| 364 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 365 | |
| 366 | if (tb[IPSET_ATTR_LINENO]) |
| 367 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 368 | |
| 369 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip); |
| 370 | if (ret) |
| 371 | return ret; |
| 372 | |
| 373 | if (tb[IPSET_ATTR_CIDR]) |
| 374 | data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
| 375 | |
| 376 | if (!data.cidr) |
| 377 | return -IPSET_ERR_INVALID_CIDR; |
| 378 | |
| 379 | ip6_netmask(&data.ip, data.cidr); |
| 380 | |
| 381 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 382 | if (!with_timeout(h->timeout)) |
| 383 | return -IPSET_ERR_TIMEOUT; |
| 384 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 385 | } |
| 386 | |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 387 | ret = adtfn(set, &data, timeout, flags); |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 388 | |
| 389 | return ip_set_eexist(ret, flags) ? 0 : ret; |
| 390 | } |
| 391 | |
| 392 | /* Create hash:ip type of sets */ |
| 393 | |
| 394 | static int |
| 395 | hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags) |
| 396 | { |
| 397 | u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM; |
| 398 | struct ip_set_hash *h; |
| 399 | u8 hbits; |
| 400 | |
| 401 | if (!(set->family == AF_INET || set->family == AF_INET6)) |
| 402 | return -IPSET_ERR_INVALID_FAMILY; |
| 403 | |
| 404 | if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) || |
| 405 | !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) || |
| 406 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) |
| 407 | return -IPSET_ERR_PROTOCOL; |
| 408 | |
| 409 | if (tb[IPSET_ATTR_HASHSIZE]) { |
| 410 | hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]); |
| 411 | if (hashsize < IPSET_MIMINAL_HASHSIZE) |
| 412 | hashsize = IPSET_MIMINAL_HASHSIZE; |
| 413 | } |
| 414 | |
| 415 | if (tb[IPSET_ATTR_MAXELEM]) |
| 416 | maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]); |
| 417 | |
| 418 | h = kzalloc(sizeof(*h) |
| 419 | + sizeof(struct ip_set_hash_nets) |
| 420 | * (set->family == AF_INET ? 32 : 128), GFP_KERNEL); |
| 421 | if (!h) |
| 422 | return -ENOMEM; |
| 423 | |
| 424 | h->maxelem = maxelem; |
| 425 | get_random_bytes(&h->initval, sizeof(h->initval)); |
| 426 | h->timeout = IPSET_NO_TIMEOUT; |
| 427 | |
| 428 | hbits = htable_bits(hashsize); |
| 429 | h->table = ip_set_alloc( |
| 430 | sizeof(struct htable) |
| 431 | + jhash_size(hbits) * sizeof(struct hbucket)); |
| 432 | if (!h->table) { |
| 433 | kfree(h); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | h->table->htable_bits = hbits; |
| 437 | |
| 438 | set->data = h; |
| 439 | |
| 440 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 441 | h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); |
| 442 | |
| 443 | set->variant = set->family == AF_INET |
| 444 | ? &hash_net4_tvariant : &hash_net6_tvariant; |
| 445 | |
| 446 | if (set->family == AF_INET) |
| 447 | hash_net4_gc_init(set); |
| 448 | else |
| 449 | hash_net6_gc_init(set); |
| 450 | } else { |
| 451 | set->variant = set->family == AF_INET |
| 452 | ? &hash_net4_variant : &hash_net6_variant; |
| 453 | } |
| 454 | |
| 455 | pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n", |
| 456 | set->name, jhash_size(h->table->htable_bits), |
| 457 | h->table->htable_bits, h->maxelem, set->data, h->table); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static struct ip_set_type hash_net_type __read_mostly = { |
| 463 | .name = "hash:net", |
| 464 | .protocol = IPSET_PROTOCOL, |
| 465 | .features = IPSET_TYPE_IP, |
| 466 | .dimension = IPSET_DIM_ONE, |
| 467 | .family = AF_UNSPEC, |
Jozsef Kadlecsik | f1e00b3 | 2011-06-16 18:51:41 +0200 | [diff] [blame] | 468 | .revision_min = 0, |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 469 | .revision_max = 1, /* Range as input support for IPv4 added */ |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 470 | .create = hash_net_create, |
| 471 | .create_policy = { |
| 472 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, |
| 473 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, |
| 474 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, |
| 475 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, |
| 476 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 477 | }, |
| 478 | .adt_policy = { |
| 479 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame^] | 480 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
Jozsef Kadlecsik | b383702 | 2011-02-01 15:52:54 +0100 | [diff] [blame] | 481 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
| 482 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 483 | }, |
| 484 | .me = THIS_MODULE, |
| 485 | }; |
| 486 | |
| 487 | static int __init |
| 488 | hash_net_init(void) |
| 489 | { |
| 490 | return ip_set_type_register(&hash_net_type); |
| 491 | } |
| 492 | |
| 493 | static void __exit |
| 494 | hash_net_fini(void) |
| 495 | { |
| 496 | ip_set_type_unregister(&hash_net_type); |
| 497 | } |
| 498 | |
| 499 | module_init(hash_net_init); |
| 500 | module_exit(hash_net_fini); |