1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2011-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
4 /* Kernel module implementing an IP set type: the hash:net,iface type */
6 #include <linux/jhash.h>
7 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
11 #include <linux/random.h>
14 #include <net/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_bridge.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 nomatch flag support added */
24 /* 2 /0 support added */
25 /* 3 Counters support added */
26 /* 4 Comments support added */
27 /* 5 Forceadd support added */
28 /* 6 skbinfo support added */
29 #define IPSET_TYPE_REV_MAX 7 /* interface wildcard support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
33 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:net,iface");
36 /* Type specific function prefix */
37 #define HTYPE hash_netiface
38 #define IP_SET_HASH_WITH_NETS
39 #define IP_SET_HASH_WITH_MULTI
40 #define IP_SET_HASH_WITH_NET0
42 #define STRLCPY(a, b) strlcpy(a, b, IFNAMSIZ)
46 struct hash_netiface4_elem_hashed {
55 struct hash_netiface4_elem {
65 /* Common functions */
68 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
69 const struct hash_netiface4_elem *ip2,
72 return ip1->ip == ip2->ip &&
73 ip1->cidr == ip2->cidr &&
75 ip1->physdev == ip2->physdev &&
77 strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
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) |
111 (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
114 flags |= IPSET_FLAG_NOMATCH;
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
116 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
117 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
119 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
120 goto nla_put_failure;
128 hash_netiface4_data_next(struct hash_netiface4_elem *next,
129 const struct hash_netiface4_elem *d)
134 #define MTYPE hash_netiface4
136 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
137 #include "ip_set_hash_gen.h"
139 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
140 static const char *get_physindev_name(const struct sk_buff *skb)
142 struct net_device *dev = nf_bridge_get_physindev(skb);
144 return dev ? dev->name : NULL;
147 static const char *get_physoutdev_name(const struct sk_buff *skb)
149 struct net_device *dev = nf_bridge_get_physoutdev(skb);
151 return dev ? dev->name : NULL;
156 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
157 const struct xt_action_param *par,
158 enum ipset_adt adt, struct ip_set_adt_opt *opt)
160 struct hash_netiface4 *h = set->data;
161 ipset_adtfn adtfn = set->variant->adt[adt];
162 struct hash_netiface4_elem e = {
163 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
166 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
168 if (adt == IPSET_TEST)
171 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
172 e.ip &= ip_set_netmask(e.cidr);
174 #define IFACE(dir) (par->state->dir ? par->state->dir->name : "")
175 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
177 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
178 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
179 const char *eiface = SRCDIR ? get_physindev_name(skb) :
180 get_physoutdev_name(skb);
184 STRLCPY(e.iface, eiface);
188 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
191 if (strlen(e.iface) == 0)
193 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
197 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
198 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
200 struct hash_netiface4 *h = set->data;
201 ipset_adtfn adtfn = set->variant->adt[adt];
202 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
203 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
204 u32 ip = 0, ip_to = 0;
207 if (tb[IPSET_ATTR_LINENO])
208 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
210 if (unlikely(!tb[IPSET_ATTR_IP] ||
211 !tb[IPSET_ATTR_IFACE] ||
212 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
213 return -IPSET_ERR_PROTOCOL;
215 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
219 ret = ip_set_get_extensions(set, tb, &ext);
223 if (tb[IPSET_ATTR_CIDR]) {
224 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
225 if (e.cidr > HOST_MASK)
226 return -IPSET_ERR_INVALID_CIDR;
228 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
230 if (tb[IPSET_ATTR_CADT_FLAGS]) {
231 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
233 if (cadt_flags & IPSET_FLAG_PHYSDEV)
235 if (cadt_flags & IPSET_FLAG_NOMATCH)
236 flags |= (IPSET_FLAG_NOMATCH << 16);
237 if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
240 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
241 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
242 ret = adtfn(set, &e, &ext, &ext, flags);
243 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
244 ip_set_eexist(ret, flags) ? 0 : ret;
247 if (tb[IPSET_ATTR_IP_TO]) {
248 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
253 if (ip + UINT_MAX == ip_to)
254 return -IPSET_ERR_HASH_RANGE;
256 ip_set_mask_from_to(ip, ip_to, e.cidr);
260 ip = ntohl(h->next.ip);
263 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
264 ret = adtfn(set, &e, &ext, &ext, flags);
266 if (ret && !ip_set_eexist(ret, flags))
270 } while (ip++ < ip_to);
276 struct hash_netiface6_elem_hashed {
277 union nf_inet_addr ip;
284 struct hash_netiface6_elem {
285 union nf_inet_addr ip;
291 char iface[IFNAMSIZ];
294 /* Common functions */
297 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
298 const struct hash_netiface6_elem *ip2,
301 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
302 ip1->cidr == ip2->cidr &&
304 ip1->physdev == ip2->physdev &&
306 strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
307 strcmp(ip1->iface, ip2->iface) == 0);
311 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
313 return elem->nomatch ? -ENOTEMPTY : 1;
317 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
319 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
323 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
325 swap(*flags, elem->nomatch);
329 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
331 ip6_netmask(&elem->ip, cidr);
336 hash_netiface6_data_list(struct sk_buff *skb,
337 const struct hash_netiface6_elem *data)
339 u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
340 (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
343 flags |= IPSET_FLAG_NOMATCH;
344 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
345 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
346 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
348 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
349 goto nla_put_failure;
357 hash_netiface6_data_next(struct hash_netiface6_elem *next,
358 const struct hash_netiface6_elem *d)
365 #define MTYPE hash_netiface6
366 #define HOST_MASK 128
367 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
368 #define IP_SET_EMIT_CREATE
369 #include "ip_set_hash_gen.h"
372 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
373 const struct xt_action_param *par,
374 enum ipset_adt adt, struct ip_set_adt_opt *opt)
376 struct hash_netiface6 *h = set->data;
377 ipset_adtfn adtfn = set->variant->adt[adt];
378 struct hash_netiface6_elem e = {
379 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
382 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
384 if (adt == IPSET_TEST)
387 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
388 ip6_netmask(&e.ip, e.cidr);
390 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
391 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
392 const char *eiface = SRCDIR ? get_physindev_name(skb) :
393 get_physoutdev_name(skb);
397 STRLCPY(e.iface, eiface);
401 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
404 if (strlen(e.iface) == 0)
407 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
411 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
412 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
414 ipset_adtfn adtfn = set->variant->adt[adt];
415 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
416 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
419 if (tb[IPSET_ATTR_LINENO])
420 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
422 if (unlikely(!tb[IPSET_ATTR_IP] ||
423 !tb[IPSET_ATTR_IFACE] ||
424 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
425 return -IPSET_ERR_PROTOCOL;
426 if (unlikely(tb[IPSET_ATTR_IP_TO]))
427 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
429 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
433 ret = ip_set_get_extensions(set, tb, &ext);
437 if (tb[IPSET_ATTR_CIDR]) {
438 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
439 if (e.cidr > HOST_MASK)
440 return -IPSET_ERR_INVALID_CIDR;
443 ip6_netmask(&e.ip, e.cidr);
445 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
447 if (tb[IPSET_ATTR_CADT_FLAGS]) {
448 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
450 if (cadt_flags & IPSET_FLAG_PHYSDEV)
452 if (cadt_flags & IPSET_FLAG_NOMATCH)
453 flags |= (IPSET_FLAG_NOMATCH << 16);
454 if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
458 ret = adtfn(set, &e, &ext, &ext, flags);
460 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
461 ip_set_eexist(ret, flags) ? 0 : ret;
464 static struct ip_set_type hash_netiface_type __read_mostly = {
465 .name = "hash:net,iface",
466 .protocol = IPSET_PROTOCOL,
467 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
469 .dimension = IPSET_DIM_TWO,
470 .family = NFPROTO_UNSPEC,
471 .revision_min = IPSET_TYPE_REV_MIN,
472 .revision_max = IPSET_TYPE_REV_MAX,
473 .create = hash_netiface_create,
475 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
476 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
477 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
478 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
479 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
480 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
481 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
484 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
485 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
486 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
487 .len = IFNAMSIZ - 1 },
488 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
489 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
490 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
491 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
492 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
493 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
494 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
495 .len = IPSET_MAX_COMMENT_SIZE },
496 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
497 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
498 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
504 hash_netiface_init(void)
506 return ip_set_type_register(&hash_netiface_type);
510 hash_netiface_fini(void)
513 ip_set_type_unregister(&hash_netiface_type);
516 module_init(hash_netiface_init);
517 module_exit(hash_netiface_fini);