1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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.
8 /* Kernel module implementing an IP set type: the hash:ip,port type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
18 #include <net/netlink.h>
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>
27 #define REVISION_MIN 0
28 /* 1 SCTP and UDPLITE support added */
29 #define REVISION_MAX 2 /* Counters support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("hash:ip,port", REVISION_MIN, REVISION_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,port");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipport
42 struct hash_ipport4_elem {
49 struct hash_ipport4t_elem {
54 unsigned long timeout;
57 struct hash_ipport4c_elem {
62 struct ip_set_counter counter;
65 struct hash_ipport4ct_elem {
70 struct ip_set_counter counter;
71 unsigned long timeout;
74 /* Common functions */
77 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
78 const struct hash_ipport4_elem *ip2,
81 return ip1->ip == ip2->ip &&
82 ip1->port == ip2->port &&
83 ip1->proto == ip2->proto;
87 hash_ipport4_data_list(struct sk_buff *skb,
88 const struct hash_ipport4_elem *data)
90 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
91 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
92 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
101 hash_ipport4_data_next(struct hash_ipport4_elem *next,
102 const struct hash_ipport4_elem *d)
105 next->port = d->port;
108 #define MTYPE hash_ipport4
111 #define HKEY_DATALEN sizeof(struct hash_ipport4_elem)
112 #include "ip_set_hash_gen.h"
115 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
116 const struct xt_action_param *par,
117 enum ipset_adt adt, struct ip_set_adt_opt *opt)
119 const struct hash_ipport *h = set->data;
120 ipset_adtfn adtfn = set->variant->adt[adt];
121 struct hash_ipport4_elem e = { };
122 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
124 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
128 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
129 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
133 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
134 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
136 const struct hash_ipport *h = set->data;
137 ipset_adtfn adtfn = set->variant->adt[adt];
138 struct hash_ipport4_elem e = { };
139 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
140 u32 ip, ip_to, p = 0, port, port_to;
141 bool with_ports = false;
144 if (unlikely(!tb[IPSET_ATTR_IP] ||
145 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
146 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
147 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
148 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
149 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
150 return -IPSET_ERR_PROTOCOL;
152 if (tb[IPSET_ATTR_LINENO])
153 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
155 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
156 ip_set_get_extensions(set, tb, &ext);
160 if (tb[IPSET_ATTR_PORT])
161 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
163 return -IPSET_ERR_PROTOCOL;
165 if (tb[IPSET_ATTR_PROTO]) {
166 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
167 with_ports = ip_set_proto_with_ports(e.proto);
170 return -IPSET_ERR_INVALID_PROTO;
172 return -IPSET_ERR_MISSING_PROTO;
174 if (!(with_ports || e.proto == IPPROTO_ICMP))
177 if (adt == IPSET_TEST ||
178 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
179 tb[IPSET_ATTR_PORT_TO])) {
180 ret = adtfn(set, &e, &ext, &ext, flags);
181 return ip_set_eexist(ret, flags) ? 0 : ret;
184 ip_to = ip = ntohl(e.ip);
185 if (tb[IPSET_ATTR_IP_TO]) {
186 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
191 } else if (tb[IPSET_ATTR_CIDR]) {
192 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
194 if (!cidr || cidr > 32)
195 return -IPSET_ERR_INVALID_CIDR;
196 ip_set_mask_from_to(ip, ip_to, cidr);
199 port_to = port = ntohs(e.port);
200 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
201 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
207 ip = ntohl(h->next.ip);
208 for (; !before(ip_to, ip); ip++) {
209 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
211 for (; p <= port_to; p++) {
214 ret = adtfn(set, &e, &ext, &ext, flags);
216 if (ret && !ip_set_eexist(ret, flags))
227 struct hash_ipport6_elem {
228 union nf_inet_addr ip;
234 struct hash_ipport6t_elem {
235 union nf_inet_addr ip;
239 unsigned long timeout;
242 struct hash_ipport6c_elem {
243 union nf_inet_addr ip;
247 struct ip_set_counter counter;
250 struct hash_ipport6ct_elem {
251 union nf_inet_addr ip;
255 struct ip_set_counter counter;
256 unsigned long timeout;
259 /* Common functions */
262 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
263 const struct hash_ipport6_elem *ip2,
266 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
267 ip1->port == ip2->port &&
268 ip1->proto == ip2->proto;
272 hash_ipport6_data_list(struct sk_buff *skb,
273 const struct hash_ipport6_elem *data)
275 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
276 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
277 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
278 goto nla_put_failure;
286 hash_ipport6_data_next(struct hash_ipport4_elem *next,
287 const struct hash_ipport6_elem *d)
289 next->port = d->port;
297 #define MTYPE hash_ipport6
299 #define HOST_MASK 128
300 #define HKEY_DATALEN sizeof(struct hash_ipport6_elem)
301 #define IP_SET_EMIT_CREATE
302 #include "ip_set_hash_gen.h"
305 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
306 const struct xt_action_param *par,
307 enum ipset_adt adt, struct ip_set_adt_opt *opt)
309 const struct hash_ipport *h = set->data;
310 ipset_adtfn adtfn = set->variant->adt[adt];
311 struct hash_ipport6_elem e = { };
312 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
314 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
318 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
319 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
323 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
324 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
326 const struct hash_ipport *h = set->data;
327 ipset_adtfn adtfn = set->variant->adt[adt];
328 struct hash_ipport6_elem e = { };
329 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
331 bool with_ports = false;
334 if (unlikely(!tb[IPSET_ATTR_IP] ||
335 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
336 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
337 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
338 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
339 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
340 tb[IPSET_ATTR_IP_TO] ||
341 tb[IPSET_ATTR_CIDR]))
342 return -IPSET_ERR_PROTOCOL;
344 if (tb[IPSET_ATTR_LINENO])
345 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
347 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
348 ip_set_get_extensions(set, tb, &ext);
352 if (tb[IPSET_ATTR_PORT])
353 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
355 return -IPSET_ERR_PROTOCOL;
357 if (tb[IPSET_ATTR_PROTO]) {
358 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
359 with_ports = ip_set_proto_with_ports(e.proto);
362 return -IPSET_ERR_INVALID_PROTO;
364 return -IPSET_ERR_MISSING_PROTO;
366 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
369 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
370 ret = adtfn(set, &e, &ext, &ext, flags);
371 return ip_set_eexist(ret, flags) ? 0 : ret;
374 port = ntohs(e.port);
375 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
380 port = ntohs(h->next.port);
381 for (; port <= port_to; port++) {
382 e.port = htons(port);
383 ret = adtfn(set, &e, &ext, &ext, flags);
385 if (ret && !ip_set_eexist(ret, flags))
393 static struct ip_set_type hash_ipport_type __read_mostly = {
394 .name = "hash:ip,port",
395 .protocol = IPSET_PROTOCOL,
396 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
397 .dimension = IPSET_DIM_TWO,
398 .family = NFPROTO_UNSPEC,
399 .revision_min = REVISION_MIN,
400 .revision_max = REVISION_MAX,
401 .create = hash_ipport_create,
403 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
404 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
405 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
406 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
407 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
408 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
409 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
412 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
413 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
414 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
415 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
416 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
417 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
418 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
419 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
420 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
421 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
427 hash_ipport_init(void)
429 return ip_set_type_register(&hash_ipport_type);
433 hash_ipport_fini(void)
435 ip_set_type_unregister(&hash_ipport_type);
438 module_init(hash_ipport_init);
439 module_exit(hash_ipport_fini);