1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2011-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 /* Kernel module implementing an IP set type: the hash:net,iface type */
7 #include <linux/jhash.h>
8 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/errno.h>
12 #include <linux/random.h>
15 #include <net/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter_bridge.h>
19 #include <linux/netfilter/ipset/pfxlen.h>
20 #include <linux/netfilter/ipset/ip_set.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
23 #define IPSET_TYPE_REV_MIN 0
24 /* 1 nomatch flag support added */
25 /* 2 /0 support added */
26 /* 3 Counters support added */
27 /* 4 Comments support added */
28 /* 5 Forceadd support added */
29 #define IPSET_TYPE_REV_MAX 6 /* skbinfo support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
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 {
64 /* Common functions */
67 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
68 const struct hash_netiface4_elem *ip2,
71 return ip1->ip == ip2->ip &&
72 ip1->cidr == ip2->cidr &&
74 ip1->physdev == ip2->physdev &&
75 strcmp(ip1->iface, ip2->iface) == 0;
79 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
81 return elem->nomatch ? -ENOTEMPTY : 1;
85 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
87 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
91 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
93 swap(*flags, elem->nomatch);
97 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
99 elem->ip &= ip_set_netmask(cidr);
104 hash_netiface4_data_list(struct sk_buff *skb,
105 const struct hash_netiface4_elem *data)
107 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
110 flags |= IPSET_FLAG_NOMATCH;
111 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
112 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
113 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
115 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
116 goto nla_put_failure;
124 hash_netiface4_data_next(struct hash_netiface4_elem *next,
125 const struct hash_netiface4_elem *d)
130 #define MTYPE hash_netiface4
132 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
133 #include "ip_set_hash_gen.h"
135 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
136 static const char *get_physindev_name(const struct sk_buff *skb)
138 struct net_device *dev = nf_bridge_get_physindev(skb);
140 return dev ? dev->name : NULL;
143 static const char *get_physoutdev_name(const struct sk_buff *skb)
145 struct net_device *dev = nf_bridge_get_physoutdev(skb);
147 return dev ? dev->name : NULL;
152 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
153 const struct xt_action_param *par,
154 enum ipset_adt adt, struct ip_set_adt_opt *opt)
156 struct hash_netiface4 *h = set->data;
157 ipset_adtfn adtfn = set->variant->adt[adt];
158 struct hash_netiface4_elem e = {
159 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
162 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
164 if (adt == IPSET_TEST)
167 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
168 e.ip &= ip_set_netmask(e.cidr);
170 #define IFACE(dir) (par->state->dir ? par->state->dir->name : "")
171 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
173 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
174 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
175 const char *eiface = SRCDIR ? get_physindev_name(skb) :
176 get_physoutdev_name(skb);
180 STRLCPY(e.iface, eiface);
184 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
187 if (strlen(e.iface) == 0)
189 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
193 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
194 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
196 struct hash_netiface4 *h = set->data;
197 ipset_adtfn adtfn = set->variant->adt[adt];
198 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
199 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
200 u32 ip = 0, ip_to = 0;
203 if (tb[IPSET_ATTR_LINENO])
204 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
206 if (unlikely(!tb[IPSET_ATTR_IP] ||
207 !tb[IPSET_ATTR_IFACE] ||
208 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
209 return -IPSET_ERR_PROTOCOL;
211 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
215 ret = ip_set_get_extensions(set, tb, &ext);
219 if (tb[IPSET_ATTR_CIDR]) {
220 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
221 if (e.cidr > HOST_MASK)
222 return -IPSET_ERR_INVALID_CIDR;
224 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
226 if (tb[IPSET_ATTR_CADT_FLAGS]) {
227 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
229 if (cadt_flags & IPSET_FLAG_PHYSDEV)
231 if (cadt_flags & IPSET_FLAG_NOMATCH)
232 flags |= (IPSET_FLAG_NOMATCH << 16);
234 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
235 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
236 ret = adtfn(set, &e, &ext, &ext, flags);
237 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
238 ip_set_eexist(ret, flags) ? 0 : ret;
241 if (tb[IPSET_ATTR_IP_TO]) {
242 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
247 if (ip + UINT_MAX == ip_to)
248 return -IPSET_ERR_HASH_RANGE;
250 ip_set_mask_from_to(ip, ip_to, e.cidr);
254 ip = ntohl(h->next.ip);
257 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
258 ret = adtfn(set, &e, &ext, &ext, flags);
260 if (ret && !ip_set_eexist(ret, flags))
264 } while (ip++ < ip_to);
270 struct hash_netiface6_elem_hashed {
271 union nf_inet_addr ip;
278 struct hash_netiface6_elem {
279 union nf_inet_addr ip;
284 char iface[IFNAMSIZ];
287 /* Common functions */
290 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
291 const struct hash_netiface6_elem *ip2,
294 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
295 ip1->cidr == ip2->cidr &&
297 ip1->physdev == ip2->physdev &&
298 strcmp(ip1->iface, ip2->iface) == 0;
302 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
304 return elem->nomatch ? -ENOTEMPTY : 1;
308 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
310 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
314 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
316 swap(*flags, elem->nomatch);
320 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
322 ip6_netmask(&elem->ip, cidr);
327 hash_netiface6_data_list(struct sk_buff *skb,
328 const struct hash_netiface6_elem *data)
330 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
333 flags |= IPSET_FLAG_NOMATCH;
334 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
335 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
336 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
338 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
339 goto nla_put_failure;
347 hash_netiface6_data_next(struct hash_netiface6_elem *next,
348 const struct hash_netiface6_elem *d)
355 #define MTYPE hash_netiface6
356 #define HOST_MASK 128
357 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
358 #define IP_SET_EMIT_CREATE
359 #include "ip_set_hash_gen.h"
362 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
363 const struct xt_action_param *par,
364 enum ipset_adt adt, struct ip_set_adt_opt *opt)
366 struct hash_netiface6 *h = set->data;
367 ipset_adtfn adtfn = set->variant->adt[adt];
368 struct hash_netiface6_elem e = {
369 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
372 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
374 if (adt == IPSET_TEST)
377 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
378 ip6_netmask(&e.ip, e.cidr);
380 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
381 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
382 const char *eiface = SRCDIR ? get_physindev_name(skb) :
383 get_physoutdev_name(skb);
387 STRLCPY(e.iface, eiface);
391 STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
394 if (strlen(e.iface) == 0)
397 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
401 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
402 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
404 ipset_adtfn adtfn = set->variant->adt[adt];
405 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
406 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
409 if (tb[IPSET_ATTR_LINENO])
410 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
412 if (unlikely(!tb[IPSET_ATTR_IP] ||
413 !tb[IPSET_ATTR_IFACE] ||
414 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
415 return -IPSET_ERR_PROTOCOL;
416 if (unlikely(tb[IPSET_ATTR_IP_TO]))
417 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
419 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
423 ret = ip_set_get_extensions(set, tb, &ext);
427 if (tb[IPSET_ATTR_CIDR]) {
428 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
429 if (e.cidr > HOST_MASK)
430 return -IPSET_ERR_INVALID_CIDR;
433 ip6_netmask(&e.ip, e.cidr);
435 nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
437 if (tb[IPSET_ATTR_CADT_FLAGS]) {
438 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
440 if (cadt_flags & IPSET_FLAG_PHYSDEV)
442 if (cadt_flags & IPSET_FLAG_NOMATCH)
443 flags |= (IPSET_FLAG_NOMATCH << 16);
446 ret = adtfn(set, &e, &ext, &ext, flags);
448 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
449 ip_set_eexist(ret, flags) ? 0 : ret;
452 static struct ip_set_type hash_netiface_type __read_mostly = {
453 .name = "hash:net,iface",
454 .protocol = IPSET_PROTOCOL,
455 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
457 .dimension = IPSET_DIM_TWO,
458 .family = NFPROTO_UNSPEC,
459 .revision_min = IPSET_TYPE_REV_MIN,
460 .revision_max = IPSET_TYPE_REV_MAX,
461 .create = hash_netiface_create,
463 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
464 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
465 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
466 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
467 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
468 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
469 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
472 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
473 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
474 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
475 .len = IFNAMSIZ - 1 },
476 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
477 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
478 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
479 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
480 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
481 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
482 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
483 .len = IPSET_MAX_COMMENT_SIZE },
484 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
485 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
486 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
492 hash_netiface_init(void)
494 return ip_set_type_register(&hash_netiface_type);
498 hash_netiface_fini(void)
501 ip_set_type_unregister(&hash_netiface_type);
504 module_init(hash_netiface_init);
505 module_exit(hash_netiface_fini);