Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2003-2013 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,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> |
| 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_getport.h> |
| 25 | #include <linux/netfilter/ipset/ip_set_hash.h> |
| 26 | |
| 27 | #define IPSET_TYPE_REV_MIN 0 |
Josh Hunt | 07cf8f5 | 2014-02-28 22:14:57 -0500 | [diff] [blame] | 28 | /* 0 Comments support added */ |
Anton Danilov | af33141 | 2014-08-28 10:11:29 +0400 | [diff] [blame] | 29 | /* 1 Forceadd support added */ |
| 30 | #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */ |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 31 | |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>"); |
| 34 | IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); |
| 35 | MODULE_ALIAS("ip_set_hash:net,port,net"); |
| 36 | |
| 37 | /* Type specific function prefix */ |
| 38 | #define HTYPE hash_netportnet |
| 39 | #define IP_SET_HASH_WITH_PROTO |
| 40 | #define IP_SET_HASH_WITH_NETS |
| 41 | #define IPSET_NET_COUNT 2 |
| 42 | |
| 43 | /* IPv4 variant */ |
| 44 | |
| 45 | /* Member elements */ |
| 46 | struct hash_netportnet4_elem { |
| 47 | union { |
| 48 | __be32 ip[2]; |
| 49 | __be64 ipcmp; |
| 50 | }; |
| 51 | __be16 port; |
| 52 | union { |
| 53 | u8 cidr[2]; |
| 54 | u16 ccmp; |
| 55 | }; |
Jozsef Kadlecsik | cac3763 | 2014-11-30 19:56:57 +0100 | [diff] [blame] | 56 | u16 padding; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 57 | u8 nomatch:1; |
| 58 | u8 proto; |
| 59 | }; |
| 60 | |
| 61 | /* Common functions */ |
| 62 | |
| 63 | static inline bool |
| 64 | hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1, |
| 65 | const struct hash_netportnet4_elem *ip2, |
| 66 | u32 *multi) |
| 67 | { |
| 68 | return ip1->ipcmp == ip2->ipcmp && |
| 69 | ip1->ccmp == ip2->ccmp && |
| 70 | ip1->port == ip2->port && |
| 71 | ip1->proto == ip2->proto; |
| 72 | } |
| 73 | |
| 74 | static inline int |
| 75 | hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem) |
| 76 | { |
| 77 | return elem->nomatch ? -ENOTEMPTY : 1; |
| 78 | } |
| 79 | |
| 80 | static inline void |
| 81 | hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags) |
| 82 | { |
| 83 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); |
| 84 | } |
| 85 | |
| 86 | static inline void |
| 87 | hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags) |
| 88 | { |
| 89 | swap(*flags, elem->nomatch); |
| 90 | } |
| 91 | |
| 92 | static inline void |
| 93 | hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem, |
| 94 | struct hash_netportnet4_elem *orig) |
| 95 | { |
| 96 | elem->ip[1] = orig->ip[1]; |
| 97 | } |
| 98 | |
| 99 | static inline void |
| 100 | hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem, |
| 101 | u8 cidr, bool inner) |
| 102 | { |
| 103 | if (inner) { |
| 104 | elem->ip[1] &= ip_set_netmask(cidr); |
| 105 | elem->cidr[1] = cidr; |
| 106 | } else { |
| 107 | elem->ip[0] &= ip_set_netmask(cidr); |
| 108 | elem->cidr[0] = cidr; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static bool |
| 113 | hash_netportnet4_data_list(struct sk_buff *skb, |
| 114 | const struct hash_netportnet4_elem *data) |
| 115 | { |
| 116 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
| 117 | |
| 118 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) || |
| 119 | nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) || |
| 120 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || |
| 121 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || |
| 122 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || |
| 123 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || |
| 124 | (flags && |
| 125 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) |
| 126 | goto nla_put_failure; |
| 127 | return 0; |
| 128 | |
| 129 | nla_put_failure: |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | static inline void |
| 134 | hash_netportnet4_data_next(struct hash_netportnet4_elem *next, |
| 135 | const struct hash_netportnet4_elem *d) |
| 136 | { |
| 137 | next->ipcmp = d->ipcmp; |
| 138 | next->port = d->port; |
| 139 | } |
| 140 | |
| 141 | #define MTYPE hash_netportnet4 |
| 142 | #define PF 4 |
| 143 | #define HOST_MASK 32 |
| 144 | #include "ip_set_hash_gen.h" |
| 145 | |
| 146 | static int |
| 147 | hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb, |
| 148 | const struct xt_action_param *par, |
| 149 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
| 150 | { |
| 151 | const struct hash_netportnet *h = set->data; |
| 152 | ipset_adtfn adtfn = set->variant->adt[adt]; |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 153 | struct hash_netportnet4_elem e = { }; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 154 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
| 155 | |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 156 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
| 157 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 158 | if (adt == IPSET_TEST) |
| 159 | e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK; |
| 160 | |
| 161 | if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
| 162 | &e.port, &e.proto)) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]); |
| 166 | ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]); |
| 167 | e.ip[0] &= ip_set_netmask(e.cidr[0]); |
| 168 | e.ip[1] &= ip_set_netmask(e.cidr[1]); |
| 169 | |
| 170 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
| 171 | } |
| 172 | |
| 173 | static int |
| 174 | hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[], |
| 175 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
| 176 | { |
| 177 | const struct hash_netportnet *h = set->data; |
| 178 | ipset_adtfn adtfn = set->variant->adt[adt]; |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 179 | struct hash_netportnet4_elem e = { }; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 180 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
| 181 | u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to; |
| 182 | u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2; |
| 183 | bool with_ports = false; |
| 184 | u8 cidr, cidr2; |
| 185 | int ret; |
| 186 | |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 187 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 188 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
| 189 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || |
| 190 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || |
| 191 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
| 192 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || |
| 193 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || |
Anton Danilov | af33141 | 2014-08-28 10:11:29 +0400 | [diff] [blame] | 194 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) || |
| 195 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) || |
| 196 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) || |
| 197 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE))) |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 198 | return -IPSET_ERR_PROTOCOL; |
| 199 | |
| 200 | if (tb[IPSET_ATTR_LINENO]) |
| 201 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 202 | |
| 203 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) || |
| 204 | ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) || |
| 205 | ip_set_get_extensions(set, tb, &ext); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
| 209 | if (tb[IPSET_ATTR_CIDR]) { |
| 210 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
| 211 | if (!cidr || cidr > HOST_MASK) |
| 212 | return -IPSET_ERR_INVALID_CIDR; |
| 213 | e.cidr[0] = cidr; |
| 214 | } |
| 215 | |
| 216 | if (tb[IPSET_ATTR_CIDR2]) { |
| 217 | cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]); |
| 218 | if (!cidr || cidr > HOST_MASK) |
| 219 | return -IPSET_ERR_INVALID_CIDR; |
| 220 | e.cidr[1] = cidr; |
| 221 | } |
| 222 | |
| 223 | if (tb[IPSET_ATTR_PORT]) |
| 224 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
| 225 | else |
| 226 | return -IPSET_ERR_PROTOCOL; |
| 227 | |
| 228 | if (tb[IPSET_ATTR_PROTO]) { |
| 229 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
| 230 | with_ports = ip_set_proto_with_ports(e.proto); |
| 231 | |
| 232 | if (e.proto == 0) |
| 233 | return -IPSET_ERR_INVALID_PROTO; |
| 234 | } else |
| 235 | return -IPSET_ERR_MISSING_PROTO; |
| 236 | |
| 237 | if (!(with_ports || e.proto == IPPROTO_ICMP)) |
| 238 | e.port = 0; |
| 239 | |
| 240 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
| 241 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
| 242 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
| 243 | flags |= (IPSET_FLAG_NOMATCH << 16); |
| 244 | } |
| 245 | |
| 246 | with_ports = with_ports && tb[IPSET_ATTR_PORT_TO]; |
| 247 | if (adt == IPSET_TEST || |
| 248 | !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) { |
| 249 | e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0])); |
| 250 | e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1])); |
| 251 | ret = adtfn(set, &e, &ext, &ext, flags); |
| 252 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
| 253 | ip_set_eexist(ret, flags) ? 0 : ret; |
| 254 | } |
| 255 | |
| 256 | ip_to = ip; |
| 257 | if (tb[IPSET_ATTR_IP_TO]) { |
| 258 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | if (ip > ip_to) |
| 262 | swap(ip, ip_to); |
| 263 | if (unlikely(ip + UINT_MAX == ip_to)) |
| 264 | return -IPSET_ERR_HASH_RANGE; |
Sergey Popovich | 6e41ee6 | 2014-05-05 11:07:06 +0300 | [diff] [blame] | 265 | } else |
| 266 | ip_set_mask_from_to(ip, ip_to, e.cidr[0]); |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 267 | |
| 268 | port_to = port = ntohs(e.port); |
| 269 | if (tb[IPSET_ATTR_PORT_TO]) { |
| 270 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
| 271 | if (port > port_to) |
| 272 | swap(port, port_to); |
| 273 | } |
| 274 | |
| 275 | ip2_to = ip2_from; |
| 276 | if (tb[IPSET_ATTR_IP2_TO]) { |
| 277 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | if (ip2_from > ip2_to) |
| 281 | swap(ip2_from, ip2_to); |
| 282 | if (unlikely(ip2_from + UINT_MAX == ip2_to)) |
| 283 | return -IPSET_ERR_HASH_RANGE; |
Sergey Popovich | 6e41ee6 | 2014-05-05 11:07:06 +0300 | [diff] [blame] | 284 | } else |
| 285 | ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]); |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 286 | |
| 287 | if (retried) |
| 288 | ip = ntohl(h->next.ip[0]); |
| 289 | |
| 290 | while (!after(ip, ip_to)) { |
| 291 | e.ip[0] = htonl(ip); |
| 292 | ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr); |
| 293 | e.cidr[0] = cidr; |
| 294 | p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port) |
| 295 | : port; |
| 296 | for (; p <= port_to; p++) { |
| 297 | e.port = htons(p); |
| 298 | ip2 = (retried && ip == ntohl(h->next.ip[0]) && |
| 299 | p == ntohs(h->next.port)) ? ntohl(h->next.ip[1]) |
| 300 | : ip2_from; |
| 301 | while (!after(ip2, ip2_to)) { |
| 302 | e.ip[1] = htonl(ip2); |
| 303 | ip2_last = ip_set_range_to_cidr(ip2, ip2_to, |
| 304 | &cidr2); |
| 305 | e.cidr[1] = cidr2; |
| 306 | ret = adtfn(set, &e, &ext, &ext, flags); |
| 307 | if (ret && !ip_set_eexist(ret, flags)) |
| 308 | return ret; |
| 309 | else |
| 310 | ret = 0; |
| 311 | ip2 = ip2_last + 1; |
| 312 | } |
| 313 | } |
| 314 | ip = ip_last + 1; |
| 315 | } |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | /* IPv6 variant */ |
| 320 | |
| 321 | struct hash_netportnet6_elem { |
| 322 | union nf_inet_addr ip[2]; |
| 323 | __be16 port; |
| 324 | union { |
| 325 | u8 cidr[2]; |
| 326 | u16 ccmp; |
| 327 | }; |
Jozsef Kadlecsik | cac3763 | 2014-11-30 19:56:57 +0100 | [diff] [blame] | 328 | u16 padding; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 329 | u8 nomatch:1; |
| 330 | u8 proto; |
| 331 | }; |
| 332 | |
| 333 | /* Common functions */ |
| 334 | |
| 335 | static inline bool |
| 336 | hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1, |
| 337 | const struct hash_netportnet6_elem *ip2, |
| 338 | u32 *multi) |
| 339 | { |
| 340 | return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) && |
| 341 | ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) && |
| 342 | ip1->ccmp == ip2->ccmp && |
| 343 | ip1->port == ip2->port && |
| 344 | ip1->proto == ip2->proto; |
| 345 | } |
| 346 | |
| 347 | static inline int |
| 348 | hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem) |
| 349 | { |
| 350 | return elem->nomatch ? -ENOTEMPTY : 1; |
| 351 | } |
| 352 | |
| 353 | static inline void |
| 354 | hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags) |
| 355 | { |
| 356 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); |
| 357 | } |
| 358 | |
| 359 | static inline void |
| 360 | hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags) |
| 361 | { |
| 362 | swap(*flags, elem->nomatch); |
| 363 | } |
| 364 | |
| 365 | static inline void |
| 366 | hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem, |
| 367 | struct hash_netportnet6_elem *orig) |
| 368 | { |
| 369 | elem->ip[1] = orig->ip[1]; |
| 370 | } |
| 371 | |
| 372 | static inline void |
| 373 | hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem, |
| 374 | u8 cidr, bool inner) |
| 375 | { |
| 376 | if (inner) { |
| 377 | ip6_netmask(&elem->ip[1], cidr); |
| 378 | elem->cidr[1] = cidr; |
| 379 | } else { |
| 380 | ip6_netmask(&elem->ip[0], cidr); |
| 381 | elem->cidr[0] = cidr; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static bool |
| 386 | hash_netportnet6_data_list(struct sk_buff *skb, |
| 387 | const struct hash_netportnet6_elem *data) |
| 388 | { |
| 389 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
| 390 | |
| 391 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) || |
| 392 | nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) || |
| 393 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || |
| 394 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || |
| 395 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || |
| 396 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || |
| 397 | (flags && |
| 398 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) |
| 399 | goto nla_put_failure; |
| 400 | return 0; |
| 401 | |
| 402 | nla_put_failure: |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | static inline void |
| 407 | hash_netportnet6_data_next(struct hash_netportnet4_elem *next, |
| 408 | const struct hash_netportnet6_elem *d) |
| 409 | { |
| 410 | next->port = d->port; |
| 411 | } |
| 412 | |
| 413 | #undef MTYPE |
| 414 | #undef PF |
| 415 | #undef HOST_MASK |
| 416 | |
| 417 | #define MTYPE hash_netportnet6 |
| 418 | #define PF 6 |
| 419 | #define HOST_MASK 128 |
| 420 | #define IP_SET_EMIT_CREATE |
| 421 | #include "ip_set_hash_gen.h" |
| 422 | |
| 423 | static int |
| 424 | hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb, |
| 425 | const struct xt_action_param *par, |
| 426 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
| 427 | { |
| 428 | const struct hash_netportnet *h = set->data; |
| 429 | ipset_adtfn adtfn = set->variant->adt[adt]; |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 430 | struct hash_netportnet6_elem e = { }; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 431 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
| 432 | |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 433 | e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK); |
| 434 | e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK); |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 435 | if (adt == IPSET_TEST) |
| 436 | e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK; |
| 437 | |
| 438 | if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, |
| 439 | &e.port, &e.proto)) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6); |
| 443 | ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6); |
| 444 | ip6_netmask(&e.ip[0], e.cidr[0]); |
| 445 | ip6_netmask(&e.ip[1], e.cidr[1]); |
| 446 | |
| 447 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
| 448 | } |
| 449 | |
| 450 | static int |
| 451 | hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[], |
| 452 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
| 453 | { |
| 454 | const struct hash_netportnet *h = set->data; |
| 455 | ipset_adtfn adtfn = set->variant->adt[adt]; |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 456 | struct hash_netportnet6_elem e = { }; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 457 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
| 458 | u32 port, port_to; |
| 459 | bool with_ports = false; |
| 460 | int ret; |
| 461 | |
Jozsef Kadlecsik | 1a86920 | 2013-10-18 11:41:57 +0200 | [diff] [blame] | 462 | e.cidr[0] = e.cidr[1] = HOST_MASK; |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 463 | if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] || |
| 464 | !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || |
| 465 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) || |
| 466 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
| 467 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) || |
| 468 | !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) || |
Anton Danilov | af33141 | 2014-08-28 10:11:29 +0400 | [diff] [blame] | 469 | !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) || |
| 470 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) || |
| 471 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) || |
| 472 | !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE))) |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 473 | return -IPSET_ERR_PROTOCOL; |
| 474 | if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO])) |
| 475 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; |
| 476 | |
| 477 | if (tb[IPSET_ATTR_LINENO]) |
| 478 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); |
| 479 | |
| 480 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) || |
| 481 | ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) || |
| 482 | ip_set_get_extensions(set, tb, &ext); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | |
| 486 | if (tb[IPSET_ATTR_CIDR]) |
| 487 | e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
| 488 | |
| 489 | if (tb[IPSET_ATTR_CIDR2]) |
| 490 | e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]); |
| 491 | |
| 492 | if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] || |
| 493 | e.cidr[1] > HOST_MASK)) |
| 494 | return -IPSET_ERR_INVALID_CIDR; |
| 495 | |
| 496 | ip6_netmask(&e.ip[0], e.cidr[0]); |
| 497 | ip6_netmask(&e.ip[1], e.cidr[1]); |
| 498 | |
| 499 | if (tb[IPSET_ATTR_PORT]) |
| 500 | e.port = nla_get_be16(tb[IPSET_ATTR_PORT]); |
| 501 | else |
| 502 | return -IPSET_ERR_PROTOCOL; |
| 503 | |
| 504 | if (tb[IPSET_ATTR_PROTO]) { |
| 505 | e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]); |
| 506 | with_ports = ip_set_proto_with_ports(e.proto); |
| 507 | |
| 508 | if (e.proto == 0) |
| 509 | return -IPSET_ERR_INVALID_PROTO; |
| 510 | } else |
| 511 | return -IPSET_ERR_MISSING_PROTO; |
| 512 | |
| 513 | if (!(with_ports || e.proto == IPPROTO_ICMPV6)) |
| 514 | e.port = 0; |
| 515 | |
| 516 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
| 517 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
| 518 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
| 519 | flags |= (IPSET_FLAG_NOMATCH << 16); |
| 520 | } |
| 521 | |
| 522 | if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) { |
| 523 | ret = adtfn(set, &e, &ext, &ext, flags); |
| 524 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
| 525 | ip_set_eexist(ret, flags) ? 0 : ret; |
| 526 | } |
| 527 | |
| 528 | port = ntohs(e.port); |
| 529 | port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]); |
| 530 | if (port > port_to) |
| 531 | swap(port, port_to); |
| 532 | |
| 533 | if (retried) |
| 534 | port = ntohs(h->next.port); |
| 535 | for (; port <= port_to; port++) { |
| 536 | e.port = htons(port); |
| 537 | ret = adtfn(set, &e, &ext, &ext, flags); |
| 538 | |
| 539 | if (ret && !ip_set_eexist(ret, flags)) |
| 540 | return ret; |
| 541 | else |
| 542 | ret = 0; |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static struct ip_set_type hash_netportnet_type __read_mostly = { |
| 548 | .name = "hash:net,port,net", |
| 549 | .protocol = IPSET_PROTOCOL, |
| 550 | .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 | |
| 551 | IPSET_TYPE_NOMATCH, |
| 552 | .dimension = IPSET_DIM_THREE, |
| 553 | .family = NFPROTO_UNSPEC, |
| 554 | .revision_min = IPSET_TYPE_REV_MIN, |
| 555 | .revision_max = IPSET_TYPE_REV_MAX, |
| 556 | .create = hash_netportnet_create, |
| 557 | .create_policy = { |
| 558 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, |
| 559 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, |
| 560 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, |
| 561 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, |
| 562 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 563 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
| 564 | }, |
| 565 | .adt_policy = { |
| 566 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, |
| 567 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
| 568 | [IPSET_ATTR_IP2] = { .type = NLA_NESTED }, |
| 569 | [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED }, |
| 570 | [IPSET_ATTR_PORT] = { .type = NLA_U16 }, |
| 571 | [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 }, |
| 572 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
| 573 | [IPSET_ATTR_CIDR2] = { .type = NLA_U8 }, |
| 574 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, |
| 575 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
| 576 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 577 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, |
| 578 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, |
| 579 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, |
| 580 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING }, |
Anton Danilov | af33141 | 2014-08-28 10:11:29 +0400 | [diff] [blame] | 581 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
| 582 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, |
| 583 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, |
Oliver Smith | 7c3ad05 | 2013-09-28 20:33:23 +0200 | [diff] [blame] | 584 | }, |
| 585 | .me = THIS_MODULE, |
| 586 | }; |
| 587 | |
| 588 | static int __init |
| 589 | hash_netportnet_init(void) |
| 590 | { |
| 591 | return ip_set_type_register(&hash_netportnet_type); |
| 592 | } |
| 593 | |
| 594 | static void __exit |
| 595 | hash_netportnet_fini(void) |
| 596 | { |
| 597 | ip_set_type_unregister(&hash_netportnet_type); |
| 598 | } |
| 599 | |
| 600 | module_init(hash_netportnet_init); |
| 601 | module_exit(hash_netportnet_fini); |