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