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 #define IPSET_TYPE_REV_MAX 6 /* skbinfo support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
33 MODULE_ALIAS("ip_set_hash:net,iface");
35 /* Type specific function prefix */
36 #define HTYPE hash_netiface
37 #define IP_SET_HASH_WITH_NETS
38 #define IP_SET_HASH_WITH_MULTI
39 #define IP_SET_HASH_WITH_NET0
41 #define STRLCPY(a, b) strlcpy(a, b, IFNAMSIZ)
45 struct hash_netiface4_elem_hashed
{
54 struct hash_netiface4_elem
{
63 /* Common functions */
66 hash_netiface4_data_equal(const struct hash_netiface4_elem
*ip1
,
67 const struct hash_netiface4_elem
*ip2
,
70 return ip1
->ip
== ip2
->ip
&&
71 ip1
->cidr
== ip2
->cidr
&&
73 ip1
->physdev
== ip2
->physdev
&&
74 strcmp(ip1
->iface
, ip2
->iface
) == 0;
78 hash_netiface4_do_data_match(const struct hash_netiface4_elem
*elem
)
80 return elem
->nomatch
? -ENOTEMPTY
: 1;
84 hash_netiface4_data_set_flags(struct hash_netiface4_elem
*elem
, u32 flags
)
86 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
90 hash_netiface4_data_reset_flags(struct hash_netiface4_elem
*elem
, u8
*flags
)
92 swap(*flags
, elem
->nomatch
);
96 hash_netiface4_data_netmask(struct hash_netiface4_elem
*elem
, u8 cidr
)
98 elem
->ip
&= ip_set_netmask(cidr
);
103 hash_netiface4_data_list(struct sk_buff
*skb
,
104 const struct hash_netiface4_elem
*data
)
106 u32 flags
= data
->physdev
? IPSET_FLAG_PHYSDEV
: 0;
109 flags
|= IPSET_FLAG_NOMATCH
;
110 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
111 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
112 nla_put_string(skb
, IPSET_ATTR_IFACE
, data
->iface
) ||
114 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
115 goto nla_put_failure
;
123 hash_netiface4_data_next(struct hash_netiface4_elem
*next
,
124 const struct hash_netiface4_elem
*d
)
129 #define MTYPE hash_netiface4
131 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
132 #include "ip_set_hash_gen.h"
134 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
135 static const char *get_physindev_name(const struct sk_buff
*skb
)
137 struct net_device
*dev
= nf_bridge_get_physindev(skb
);
139 return dev
? dev
->name
: NULL
;
142 static const char *get_physoutdev_name(const struct sk_buff
*skb
)
144 struct net_device
*dev
= nf_bridge_get_physoutdev(skb
);
146 return dev
? dev
->name
: NULL
;
151 hash_netiface4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
152 const struct xt_action_param
*par
,
153 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
155 struct hash_netiface4
*h
= set
->data
;
156 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
157 struct hash_netiface4_elem e
= {
158 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
161 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
163 if (adt
== IPSET_TEST
)
166 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
167 e
.ip
&= ip_set_netmask(e
.cidr
);
169 #define IFACE(dir) (par->state->dir ? par->state->dir->name : "")
170 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
172 if (opt
->cmdflags
& IPSET_FLAG_PHYSDEV
) {
173 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
174 const char *eiface
= SRCDIR
? get_physindev_name(skb
) :
175 get_physoutdev_name(skb
);
179 STRLCPY(e
.iface
, eiface
);
183 STRLCPY(e
.iface
, SRCDIR
? IFACE(in
) : IFACE(out
));
186 if (strlen(e
.iface
) == 0)
188 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
192 hash_netiface4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
193 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
195 struct hash_netiface4
*h
= set
->data
;
196 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
197 struct hash_netiface4_elem e
= { .cidr
= HOST_MASK
, .elem
= 1 };
198 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
199 u32 ip
= 0, ip_to
= 0;
202 if (tb
[IPSET_ATTR_LINENO
])
203 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
205 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
206 !tb
[IPSET_ATTR_IFACE
] ||
207 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
208 return -IPSET_ERR_PROTOCOL
;
210 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
214 ret
= ip_set_get_extensions(set
, tb
, &ext
);
218 if (tb
[IPSET_ATTR_CIDR
]) {
219 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
220 if (e
.cidr
> HOST_MASK
)
221 return -IPSET_ERR_INVALID_CIDR
;
223 nla_strlcpy(e
.iface
, tb
[IPSET_ATTR_IFACE
], IFNAMSIZ
);
225 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
226 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
228 if (cadt_flags
& IPSET_FLAG_PHYSDEV
)
230 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
231 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
233 if (adt
== IPSET_TEST
|| !tb
[IPSET_ATTR_IP_TO
]) {
234 e
.ip
= htonl(ip
& ip_set_hostmask(e
.cidr
));
235 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
236 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
237 ip_set_eexist(ret
, flags
) ? 0 : ret
;
240 if (tb
[IPSET_ATTR_IP_TO
]) {
241 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
246 if (ip
+ UINT_MAX
== ip_to
)
247 return -IPSET_ERR_HASH_RANGE
;
249 ip_set_mask_from_to(ip
, ip_to
, e
.cidr
);
253 ip
= ntohl(h
->next
.ip
);
256 ip
= ip_set_range_to_cidr(ip
, ip_to
, &e
.cidr
);
257 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
259 if (ret
&& !ip_set_eexist(ret
, flags
))
263 } while (ip
++ < ip_to
);
269 struct hash_netiface6_elem_hashed
{
270 union nf_inet_addr ip
;
277 struct hash_netiface6_elem
{
278 union nf_inet_addr ip
;
283 char iface
[IFNAMSIZ
];
286 /* Common functions */
289 hash_netiface6_data_equal(const struct hash_netiface6_elem
*ip1
,
290 const struct hash_netiface6_elem
*ip2
,
293 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
294 ip1
->cidr
== ip2
->cidr
&&
296 ip1
->physdev
== ip2
->physdev
&&
297 strcmp(ip1
->iface
, ip2
->iface
) == 0;
301 hash_netiface6_do_data_match(const struct hash_netiface6_elem
*elem
)
303 return elem
->nomatch
? -ENOTEMPTY
: 1;
307 hash_netiface6_data_set_flags(struct hash_netiface6_elem
*elem
, u32 flags
)
309 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
313 hash_netiface6_data_reset_flags(struct hash_netiface6_elem
*elem
, u8
*flags
)
315 swap(*flags
, elem
->nomatch
);
319 hash_netiface6_data_netmask(struct hash_netiface6_elem
*elem
, u8 cidr
)
321 ip6_netmask(&elem
->ip
, cidr
);
326 hash_netiface6_data_list(struct sk_buff
*skb
,
327 const struct hash_netiface6_elem
*data
)
329 u32 flags
= data
->physdev
? IPSET_FLAG_PHYSDEV
: 0;
332 flags
|= IPSET_FLAG_NOMATCH
;
333 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
334 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
335 nla_put_string(skb
, IPSET_ATTR_IFACE
, data
->iface
) ||
337 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
338 goto nla_put_failure
;
346 hash_netiface6_data_next(struct hash_netiface6_elem
*next
,
347 const struct hash_netiface6_elem
*d
)
354 #define MTYPE hash_netiface6
355 #define HOST_MASK 128
356 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
357 #define IP_SET_EMIT_CREATE
358 #include "ip_set_hash_gen.h"
361 hash_netiface6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
362 const struct xt_action_param
*par
,
363 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
365 struct hash_netiface6
*h
= set
->data
;
366 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
367 struct hash_netiface6_elem e
= {
368 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
371 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
373 if (adt
== IPSET_TEST
)
376 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
377 ip6_netmask(&e
.ip
, e
.cidr
);
379 if (opt
->cmdflags
& IPSET_FLAG_PHYSDEV
) {
380 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
381 const char *eiface
= SRCDIR
? get_physindev_name(skb
) :
382 get_physoutdev_name(skb
);
386 STRLCPY(e
.iface
, eiface
);
390 STRLCPY(e
.iface
, SRCDIR
? IFACE(in
) : IFACE(out
));
393 if (strlen(e
.iface
) == 0)
396 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
400 hash_netiface6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
401 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
403 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
404 struct hash_netiface6_elem e
= { .cidr
= HOST_MASK
, .elem
= 1 };
405 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
408 if (tb
[IPSET_ATTR_LINENO
])
409 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
411 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
412 !tb
[IPSET_ATTR_IFACE
] ||
413 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
414 return -IPSET_ERR_PROTOCOL
;
415 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
416 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
418 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
422 ret
= ip_set_get_extensions(set
, tb
, &ext
);
426 if (tb
[IPSET_ATTR_CIDR
]) {
427 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
428 if (e
.cidr
> HOST_MASK
)
429 return -IPSET_ERR_INVALID_CIDR
;
432 ip6_netmask(&e
.ip
, e
.cidr
);
434 nla_strlcpy(e
.iface
, tb
[IPSET_ATTR_IFACE
], IFNAMSIZ
);
436 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
437 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
439 if (cadt_flags
& IPSET_FLAG_PHYSDEV
)
441 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
442 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
445 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
447 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
448 ip_set_eexist(ret
, flags
) ? 0 : ret
;
451 static struct ip_set_type hash_netiface_type __read_mostly
= {
452 .name
= "hash:net,iface",
453 .protocol
= IPSET_PROTOCOL
,
454 .features
= IPSET_TYPE_IP
| IPSET_TYPE_IFACE
|
456 .dimension
= IPSET_DIM_TWO
,
457 .family
= NFPROTO_UNSPEC
,
458 .revision_min
= IPSET_TYPE_REV_MIN
,
459 .revision_max
= IPSET_TYPE_REV_MAX
,
460 .create
= hash_netiface_create
,
462 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
463 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
464 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
465 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
466 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
467 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
468 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
471 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
472 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
473 [IPSET_ATTR_IFACE
] = { .type
= NLA_NUL_STRING
,
474 .len
= IFNAMSIZ
- 1 },
475 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
476 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
477 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
478 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
479 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
480 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
481 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
482 .len
= IPSET_MAX_COMMENT_SIZE
},
483 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
484 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
485 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
491 hash_netiface_init(void)
493 return ip_set_type_register(&hash_netiface_type
);
497 hash_netiface_fini(void)
500 ip_set_type_unregister(&hash_netiface_type
);
503 module_init(hash_netiface_init
);
504 module_exit(hash_netiface_fini
);