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, i = 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);
264 if (i > IPSET_MAX_RANGE) {
265 hash_netiface4_data_next(&h->next, &e);
268 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
269 ret = adtfn(set, &e, &ext, &ext, flags);
271 if (ret && !ip_set_eexist(ret, flags))
275 } while (ip++ < ip_to);
281 struct hash_netiface6_elem_hashed {
282 union nf_inet_addr ip;
289 struct hash_netiface6_elem {
290 union nf_inet_addr ip;
296 char iface[IFNAMSIZ];
299 /* Common functions */
302 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
303 const struct hash_netiface6_elem *ip2,
306 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
307 ip1->cidr == ip2->cidr &&
309 ip1->physdev == ip2->physdev &&
311 strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
312 strcmp(ip1->iface, ip2->iface) == 0);
316 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
318 return elem->nomatch ? -ENOTEMPTY : 1;
322 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
324 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
328 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
330 swap(*flags, elem->nomatch);
334 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
336 ip6_netmask(&elem->ip, cidr);
341 hash_netiface6_data_list(struct sk_buff *skb,
342 const struct hash_netiface6_elem *data)
344 u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
345 (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
348 flags |= IPSET_FLAG_NOMATCH;
349 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
350 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
351 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
353 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
354 goto nla_put_failure;
362 hash_netiface6_data_next(struct hash_netiface6_elem *next,
363 const struct hash_netiface6_elem *d)
370 #define MTYPE hash_netiface6
371 #define HOST_MASK 128
372 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
373 #define IP_SET_EMIT_CREATE
374 #include "ip_set_hash_gen.h"
377 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
378 const struct xt_action_param *par,
379 enum ipset_adt adt, struct ip_set_adt_opt *opt)
381 struct hash_netiface6 *h = set->data;
382 ipset_adtfn adtfn = set->variant->adt[adt];
383 struct hash_netiface6_elem e = {
384 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
387 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
389 if (adt == IPSET_TEST)
392 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
393 ip6_netmask(&e.ip, e.cidr);
395 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
396 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
397 const char *eiface = SRCDIR ? get_physindev_name(skb) :
398 get_physoutdev_name(skb);
402 STRLCPY(e.iface, eiface);
406 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
409 if (strlen(e.iface) == 0)
412 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
416 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
417 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
419 ipset_adtfn adtfn = set->variant->adt[adt];
420 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
421 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
424 if (tb[IPSET_ATTR_LINENO])
425 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
427 if (unlikely(!tb[IPSET_ATTR_IP] ||
428 !tb[IPSET_ATTR_IFACE] ||
429 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
430 return -IPSET_ERR_PROTOCOL;
431 if (unlikely(tb[IPSET_ATTR_IP_TO]))
432 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
434 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
438 ret = ip_set_get_extensions(set, tb, &ext);
442 if (tb[IPSET_ATTR_CIDR]) {
443 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
444 if (e.cidr > HOST_MASK)
445 return -IPSET_ERR_INVALID_CIDR;
448 ip6_netmask(&e.ip, e.cidr);
450 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
452 if (tb[IPSET_ATTR_CADT_FLAGS]) {
453 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
455 if (cadt_flags & IPSET_FLAG_PHYSDEV)
457 if (cadt_flags & IPSET_FLAG_NOMATCH)
458 flags |= (IPSET_FLAG_NOMATCH << 16);
459 if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
463 ret = adtfn(set, &e, &ext, &ext, flags);
465 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
466 ip_set_eexist(ret, flags) ? 0 : ret;
469 static struct ip_set_type hash_netiface_type __read_mostly = {
470 .name = "hash:net,iface",
471 .protocol = IPSET_PROTOCOL,
472 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
474 .dimension = IPSET_DIM_TWO,
475 .family = NFPROTO_UNSPEC,
476 .revision_min = IPSET_TYPE_REV_MIN,
477 .revision_max = IPSET_TYPE_REV_MAX,
478 .create = hash_netiface_create,
480 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
481 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
482 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
483 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
484 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
485 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
486 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
489 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
490 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
491 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
492 .len = IFNAMSIZ - 1 },
493 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
494 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
495 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
496 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
497 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
498 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
499 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
500 .len = IPSET_MAX_COMMENT_SIZE },
501 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
502 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
503 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
509 hash_netiface_init(void)
511 return ip_set_type_register(&hash_netiface_type);
515 hash_netiface_fini(void)
518 ip_set_type_unregister(&hash_netiface_type);
521 module_init(hash_netiface_init);
522 module_exit(hash_netiface_fini);