1 /* Copyright (C) 2011-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:net,iface 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>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter_bridge.h>
22 #include <linux/netfilter/ipset/pfxlen.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
26 #define IPSET_TYPE_REV_MIN 0
27 /* 1 nomatch flag support added */
28 /* 2 /0 support added */
29 /* 3 Counters support added */
30 /* 4 Comments support added */
31 /* 5 Forceadd support added */
32 #define IPSET_TYPE_REV_MAX 6 /* skbinfo support added */
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
36 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
37 MODULE_ALIAS("ip_set_hash:net,iface");
39 /* Type specific function prefix */
40 #define HTYPE hash_netiface
41 #define IP_SET_HASH_WITH_NETS
42 #define IP_SET_HASH_WITH_MULTI
43 #define IP_SET_HASH_WITH_NET0
45 #define STRLCPY(a, b) strlcpy(a, b, IFNAMSIZ)
49 struct hash_netiface4_elem_hashed {
58 struct hash_netiface4_elem {
67 /* Common functions */
70 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
71 const struct hash_netiface4_elem *ip2,
74 return ip1->ip == ip2->ip &&
75 ip1->cidr == ip2->cidr &&
77 ip1->physdev == ip2->physdev &&
78 strcmp(ip1->iface, ip2->iface) == 0;
82 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
84 return elem->nomatch ? -ENOTEMPTY : 1;
88 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
90 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
94 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
96 swap(*flags, elem->nomatch);
100 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
102 elem->ip &= ip_set_netmask(cidr);
107 hash_netiface4_data_list(struct sk_buff *skb,
108 const struct hash_netiface4_elem *data)
110 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
113 flags |= IPSET_FLAG_NOMATCH;
114 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
115 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
116 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
118 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
119 goto nla_put_failure;
127 hash_netiface4_data_next(struct hash_netiface4_elem *next,
128 const struct hash_netiface4_elem *d)
133 #define MTYPE hash_netiface4
135 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
136 #include "ip_set_hash_gen.h"
138 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
139 static const char *get_physindev_name(const struct sk_buff *skb)
141 struct net_device *dev = nf_bridge_get_physindev(skb);
143 return dev ? dev->name : NULL;
146 static const char *get_physoutdev_name(const struct sk_buff *skb)
148 struct net_device *dev = nf_bridge_get_physoutdev(skb);
150 return dev ? dev->name : NULL;
155 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
156 const struct xt_action_param *par,
157 enum ipset_adt adt, struct ip_set_adt_opt *opt)
159 struct hash_netiface4 *h = set->data;
160 ipset_adtfn adtfn = set->variant->adt[adt];
161 struct hash_netiface4_elem e = {
162 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
165 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
167 if (adt == IPSET_TEST)
170 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
171 e.ip &= ip_set_netmask(e.cidr);
173 #define IFACE(dir) (par->state->dir ? par->state->dir->name : "")
174 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
176 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
177 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
178 const char *eiface = SRCDIR ? get_physindev_name(skb) :
179 get_physoutdev_name(skb);
183 STRLCPY(e.iface, eiface);
187 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
190 if (strlen(e.iface) == 0)
192 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
196 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
197 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
199 struct hash_netiface4 *h = set->data;
200 ipset_adtfn adtfn = set->variant->adt[adt];
201 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
202 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
203 u32 ip = 0, ip_to = 0;
206 if (tb[IPSET_ATTR_LINENO])
207 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
209 if (unlikely(!tb[IPSET_ATTR_IP] ||
210 !tb[IPSET_ATTR_IFACE] ||
211 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
212 return -IPSET_ERR_PROTOCOL;
214 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
218 ret = ip_set_get_extensions(set, tb, &ext);
222 if (tb[IPSET_ATTR_CIDR]) {
223 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
224 if (e.cidr > HOST_MASK)
225 return -IPSET_ERR_INVALID_CIDR;
227 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
229 if (tb[IPSET_ATTR_CADT_FLAGS]) {
230 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
232 if (cadt_flags & IPSET_FLAG_PHYSDEV)
234 if (cadt_flags & IPSET_FLAG_NOMATCH)
235 flags |= (IPSET_FLAG_NOMATCH << 16);
237 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
238 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
239 ret = adtfn(set, &e, &ext, &ext, flags);
240 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
241 ip_set_eexist(ret, flags) ? 0 : ret;
244 if (tb[IPSET_ATTR_IP_TO]) {
245 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
250 if (ip + UINT_MAX == ip_to)
251 return -IPSET_ERR_HASH_RANGE;
253 ip_set_mask_from_to(ip, ip_to, e.cidr);
257 ip = ntohl(h->next.ip);
260 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
261 ret = adtfn(set, &e, &ext, &ext, flags);
263 if (ret && !ip_set_eexist(ret, flags))
267 } while (ip++ < ip_to);
273 struct hash_netiface6_elem_hashed {
274 union nf_inet_addr ip;
281 struct hash_netiface6_elem {
282 union nf_inet_addr ip;
287 char iface[IFNAMSIZ];
290 /* Common functions */
293 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
294 const struct hash_netiface6_elem *ip2,
297 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
298 ip1->cidr == ip2->cidr &&
300 ip1->physdev == ip2->physdev &&
301 strcmp(ip1->iface, ip2->iface) == 0;
305 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
307 return elem->nomatch ? -ENOTEMPTY : 1;
311 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
313 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
317 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
319 swap(*flags, elem->nomatch);
323 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
325 ip6_netmask(&elem->ip, cidr);
330 hash_netiface6_data_list(struct sk_buff *skb,
331 const struct hash_netiface6_elem *data)
333 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
336 flags |= IPSET_FLAG_NOMATCH;
337 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
338 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
339 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
341 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
342 goto nla_put_failure;
350 hash_netiface6_data_next(struct hash_netiface6_elem *next,
351 const struct hash_netiface6_elem *d)
358 #define MTYPE hash_netiface6
359 #define HOST_MASK 128
360 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
361 #define IP_SET_EMIT_CREATE
362 #include "ip_set_hash_gen.h"
365 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
366 const struct xt_action_param *par,
367 enum ipset_adt adt, struct ip_set_adt_opt *opt)
369 struct hash_netiface6 *h = set->data;
370 ipset_adtfn adtfn = set->variant->adt[adt];
371 struct hash_netiface6_elem e = {
372 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
375 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
377 if (adt == IPSET_TEST)
380 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
381 ip6_netmask(&e.ip, e.cidr);
383 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
384 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
385 const char *eiface = SRCDIR ? get_physindev_name(skb) :
386 get_physoutdev_name(skb);
390 STRLCPY(e.iface, eiface);
394 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
397 if (strlen(e.iface) == 0)
400 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
404 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
405 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
407 ipset_adtfn adtfn = set->variant->adt[adt];
408 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
409 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
412 if (tb[IPSET_ATTR_LINENO])
413 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
415 if (unlikely(!tb[IPSET_ATTR_IP] ||
416 !tb[IPSET_ATTR_IFACE] ||
417 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
418 return -IPSET_ERR_PROTOCOL;
419 if (unlikely(tb[IPSET_ATTR_IP_TO]))
420 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
422 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
426 ret = ip_set_get_extensions(set, tb, &ext);
430 if (tb[IPSET_ATTR_CIDR]) {
431 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
432 if (e.cidr > HOST_MASK)
433 return -IPSET_ERR_INVALID_CIDR;
436 ip6_netmask(&e.ip, e.cidr);
438 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
440 if (tb[IPSET_ATTR_CADT_FLAGS]) {
441 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
443 if (cadt_flags & IPSET_FLAG_PHYSDEV)
445 if (cadt_flags & IPSET_FLAG_NOMATCH)
446 flags |= (IPSET_FLAG_NOMATCH << 16);
449 ret = adtfn(set, &e, &ext, &ext, flags);
451 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
452 ip_set_eexist(ret, flags) ? 0 : ret;
455 static struct ip_set_type hash_netiface_type __read_mostly = {
456 .name = "hash:net,iface",
457 .protocol = IPSET_PROTOCOL,
458 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
460 .dimension = IPSET_DIM_TWO,
461 .family = NFPROTO_UNSPEC,
462 .revision_min = IPSET_TYPE_REV_MIN,
463 .revision_max = IPSET_TYPE_REV_MAX,
464 .create = hash_netiface_create,
466 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
467 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
468 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
469 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
470 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
471 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
472 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
475 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
476 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
477 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
478 .len = IFNAMSIZ - 1 },
479 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
480 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
481 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
482 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
483 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
484 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
485 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
486 .len = IPSET_MAX_COMMENT_SIZE },
487 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
488 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
489 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
495 hash_netiface_init(void)
497 return ip_set_type_register(&hash_netiface_type);
501 hash_netiface_fini(void)
504 ip_set_type_unregister(&hash_netiface_type);
507 module_init(hash_netiface_init);
508 module_exit(hash_netiface_fini);