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