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