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 /* 7 interface wildcard support added */
30 #define IPSET_TYPE_REV_MAX 8 /* bucketsize, initval support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:net,iface");
37 /* Type specific function prefix */
38 #define HTYPE hash_netiface
39 #define IP_SET_HASH_WITH_NETS
40 #define IP_SET_HASH_WITH_MULTI
41 #define IP_SET_HASH_WITH_NET0
43 #define STRSCPY(a, b) strscpy(a, b, IFNAMSIZ)
47 struct hash_netiface4_elem_hashed {
56 struct hash_netiface4_elem {
66 /* Common functions */
69 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
70 const struct hash_netiface4_elem *ip2,
73 return ip1->ip == ip2->ip &&
74 ip1->cidr == ip2->cidr &&
76 ip1->physdev == ip2->physdev &&
78 strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
79 strcmp(ip1->iface, ip2->iface) == 0);
83 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
85 return elem->nomatch ? -ENOTEMPTY : 1;
89 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
91 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
95 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
97 swap(*flags, elem->nomatch);
101 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
103 elem->ip &= ip_set_netmask(cidr);
108 hash_netiface4_data_list(struct sk_buff *skb,
109 const struct hash_netiface4_elem *data)
111 u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
112 (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
115 flags |= IPSET_FLAG_NOMATCH;
116 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
117 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
118 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
120 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
121 goto nla_put_failure;
129 hash_netiface4_data_next(struct hash_netiface4_elem *next,
130 const struct hash_netiface4_elem *d)
135 #define MTYPE hash_netiface4
137 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
138 #include "ip_set_hash_gen.h"
140 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
141 static const char *get_physindev_name(const struct sk_buff *skb)
143 struct net_device *dev = nf_bridge_get_physindev(skb);
145 return dev ? dev->name : NULL;
148 static const char *get_physoutdev_name(const struct sk_buff *skb)
150 struct net_device *dev = nf_bridge_get_physoutdev(skb);
152 return dev ? dev->name : NULL;
157 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
158 const struct xt_action_param *par,
159 enum ipset_adt adt, struct ip_set_adt_opt *opt)
161 struct hash_netiface4 *h = set->data;
162 ipset_adtfn adtfn = set->variant->adt[adt];
163 struct hash_netiface4_elem e = {
164 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
167 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
169 if (adt == IPSET_TEST)
172 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
173 e.ip &= ip_set_netmask(e.cidr);
175 #define IFACE(dir) (par->state->dir ? par->state->dir->name : "")
176 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
178 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
179 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
180 const char *eiface = SRCDIR ? get_physindev_name(skb) :
181 get_physoutdev_name(skb);
185 STRSCPY(e.iface, eiface);
189 STRSCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
192 if (strlen(e.iface) == 0)
194 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
198 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
199 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
201 struct hash_netiface4 *h = set->data;
202 ipset_adtfn adtfn = set->variant->adt[adt];
203 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
204 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
205 u32 ip = 0, ip_to = 0, i = 0;
208 if (tb[IPSET_ATTR_LINENO])
209 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
211 if (unlikely(!tb[IPSET_ATTR_IP] ||
212 !tb[IPSET_ATTR_IFACE] ||
213 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
214 return -IPSET_ERR_PROTOCOL;
216 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
220 ret = ip_set_get_extensions(set, tb, &ext);
224 if (tb[IPSET_ATTR_CIDR]) {
225 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
226 if (e.cidr > HOST_MASK)
227 return -IPSET_ERR_INVALID_CIDR;
229 nla_strscpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
231 if (tb[IPSET_ATTR_CADT_FLAGS]) {
232 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
234 if (cadt_flags & IPSET_FLAG_PHYSDEV)
236 if (cadt_flags & IPSET_FLAG_NOMATCH)
237 flags |= (IPSET_FLAG_NOMATCH << 16);
238 if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
241 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
242 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
243 ret = adtfn(set, &e, &ext, &ext, flags);
244 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
245 ip_set_eexist(ret, flags) ? 0 : ret;
248 if (tb[IPSET_ATTR_IP_TO]) {
249 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
254 if (ip + UINT_MAX == ip_to)
255 return -IPSET_ERR_HASH_RANGE;
257 ip_set_mask_from_to(ip, ip_to, e.cidr);
261 ip = ntohl(h->next.ip);
265 if (i > IPSET_MAX_RANGE) {
266 hash_netiface4_data_next(&h->next, &e);
269 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
270 ret = adtfn(set, &e, &ext, &ext, flags);
272 if (ret && !ip_set_eexist(ret, flags))
276 } while (ip++ < ip_to);
282 struct hash_netiface6_elem_hashed {
283 union nf_inet_addr ip;
290 struct hash_netiface6_elem {
291 union nf_inet_addr ip;
297 char iface[IFNAMSIZ];
300 /* Common functions */
303 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
304 const struct hash_netiface6_elem *ip2,
307 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
308 ip1->cidr == ip2->cidr &&
310 ip1->physdev == ip2->physdev &&
312 strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
313 strcmp(ip1->iface, ip2->iface) == 0);
317 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
319 return elem->nomatch ? -ENOTEMPTY : 1;
323 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
325 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
329 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
331 swap(*flags, elem->nomatch);
335 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
337 ip6_netmask(&elem->ip, cidr);
342 hash_netiface6_data_list(struct sk_buff *skb,
343 const struct hash_netiface6_elem *data)
345 u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
346 (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
349 flags |= IPSET_FLAG_NOMATCH;
350 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
351 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
352 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
354 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
355 goto nla_put_failure;
363 hash_netiface6_data_next(struct hash_netiface6_elem *next,
364 const struct hash_netiface6_elem *d)
371 #define MTYPE hash_netiface6
372 #define HOST_MASK 128
373 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
374 #define IP_SET_EMIT_CREATE
375 #include "ip_set_hash_gen.h"
378 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
379 const struct xt_action_param *par,
380 enum ipset_adt adt, struct ip_set_adt_opt *opt)
382 struct hash_netiface6 *h = set->data;
383 ipset_adtfn adtfn = set->variant->adt[adt];
384 struct hash_netiface6_elem e = {
385 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
388 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
390 if (adt == IPSET_TEST)
393 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
394 ip6_netmask(&e.ip, e.cidr);
396 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
397 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
398 const char *eiface = SRCDIR ? get_physindev_name(skb) :
399 get_physoutdev_name(skb);
403 STRSCPY(e.iface, eiface);
407 STRSCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
410 if (strlen(e.iface) == 0)
413 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
417 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
418 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
420 ipset_adtfn adtfn = set->variant->adt[adt];
421 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
422 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
425 if (tb[IPSET_ATTR_LINENO])
426 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
428 if (unlikely(!tb[IPSET_ATTR_IP] ||
429 !tb[IPSET_ATTR_IFACE] ||
430 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
431 return -IPSET_ERR_PROTOCOL;
432 if (unlikely(tb[IPSET_ATTR_IP_TO]))
433 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
435 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
439 ret = ip_set_get_extensions(set, tb, &ext);
443 if (tb[IPSET_ATTR_CIDR]) {
444 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
445 if (e.cidr > HOST_MASK)
446 return -IPSET_ERR_INVALID_CIDR;
449 ip6_netmask(&e.ip, e.cidr);
451 nla_strscpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
453 if (tb[IPSET_ATTR_CADT_FLAGS]) {
454 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
456 if (cadt_flags & IPSET_FLAG_PHYSDEV)
458 if (cadt_flags & IPSET_FLAG_NOMATCH)
459 flags |= (IPSET_FLAG_NOMATCH << 16);
460 if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
464 ret = adtfn(set, &e, &ext, &ext, flags);
466 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
467 ip_set_eexist(ret, flags) ? 0 : ret;
470 static struct ip_set_type hash_netiface_type __read_mostly = {
471 .name = "hash:net,iface",
472 .protocol = IPSET_PROTOCOL,
473 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
475 .dimension = IPSET_DIM_TWO,
476 .family = NFPROTO_UNSPEC,
477 .revision_min = IPSET_TYPE_REV_MIN,
478 .revision_max = IPSET_TYPE_REV_MAX,
479 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
480 .create = hash_netiface_create,
482 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
483 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
484 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
485 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
486 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
487 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
488 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
489 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
492 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
493 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
494 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
495 .len = IFNAMSIZ - 1 },
496 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
497 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
498 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
499 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
500 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
501 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
502 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
503 .len = IPSET_MAX_COMMENT_SIZE },
504 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
505 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
506 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
512 hash_netiface_init(void)
514 return ip_set_type_register(&hash_netiface_type);
518 hash_netiface_fini(void)
521 ip_set_type_unregister(&hash_netiface_type);
524 module_init(hash_netiface_init);
525 module_exit(hash_netiface_fini);