1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
5 /* Kernel module implementing an IP set type: the hash:ip,mac type */
7 #include <linux/jhash.h>
8 #include <linux/module.h>
10 #include <linux/etherdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
14 #include <linux/if_ether.h>
17 #include <net/netlink.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_hash.h>
25 #define IPSET_TYPE_REV_MIN 0
26 #define IPSET_TYPE_REV_MAX 0
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
30 IP_SET_MODULE_DESC("hash:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:ip,mac");
33 /* Type specific function prefix */
34 #define HTYPE hash_ipmac
39 struct hash_ipmac4_elem {
40 /* Zero valued IP addresses cannot be stored */
43 unsigned char ether[ETH_ALEN];
48 /* Common functions */
51 hash_ipmac4_data_equal(const struct hash_ipmac4_elem *e1,
52 const struct hash_ipmac4_elem *e2,
55 return e1->ip == e2->ip && ether_addr_equal(e1->ether, e2->ether);
59 hash_ipmac4_data_list(struct sk_buff *skb, const struct hash_ipmac4_elem *e)
61 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip) ||
62 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
71 hash_ipmac4_data_next(struct hash_ipmac4_elem *next,
72 const struct hash_ipmac4_elem *e)
77 #define MTYPE hash_ipmac4
80 #define HKEY_DATALEN sizeof(struct hash_ipmac4_elem)
81 #include "ip_set_hash_gen.h"
84 hash_ipmac4_kadt(struct ip_set *set, const struct sk_buff *skb,
85 const struct xt_action_param *par,
86 enum ipset_adt adt, struct ip_set_adt_opt *opt)
88 ipset_adtfn adtfn = set->variant->adt[adt];
89 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
90 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
92 /* MAC can be src only */
93 if (!(opt->flags & IPSET_DIM_TWO_SRC))
96 if (skb_mac_header(skb) < skb->head ||
97 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
100 if (opt->flags & IPSET_DIM_ONE_SRC)
101 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
103 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
105 if (is_zero_ether_addr(e.ether))
108 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
110 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
114 hash_ipmac4_uadt(struct ip_set *set, struct nlattr *tb[],
115 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
117 ipset_adtfn adtfn = set->variant->adt[adt];
118 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
119 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
122 if (unlikely(!tb[IPSET_ATTR_IP] ||
123 !tb[IPSET_ATTR_ETHER] ||
124 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
125 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
126 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
127 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
128 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
129 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
130 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
131 return -IPSET_ERR_PROTOCOL;
133 if (tb[IPSET_ATTR_LINENO])
134 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
136 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
137 ip_set_get_extensions(set, tb, &ext);
140 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
141 if (is_zero_ether_addr(e.ether))
142 return -IPSET_ERR_HASH_ELEM;
144 return adtfn(set, &e, &ext, &ext, flags);
149 /* Member elements */
150 struct hash_ipmac6_elem {
151 /* Zero valued IP addresses cannot be stored */
152 union nf_inet_addr ip;
154 unsigned char ether[ETH_ALEN];
159 /* Common functions */
162 hash_ipmac6_data_equal(const struct hash_ipmac6_elem *e1,
163 const struct hash_ipmac6_elem *e2,
166 return ipv6_addr_equal(&e1->ip.in6, &e2->ip.in6) &&
167 ether_addr_equal(e1->ether, e2->ether);
171 hash_ipmac6_data_list(struct sk_buff *skb, const struct hash_ipmac6_elem *e)
173 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
174 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
175 goto nla_put_failure;
183 hash_ipmac6_data_next(struct hash_ipmac6_elem *next,
184 const struct hash_ipmac6_elem *e)
193 #define MTYPE hash_ipmac6
195 #define HOST_MASK 128
196 #define HKEY_DATALEN sizeof(struct hash_ipmac6_elem)
197 #define IP_SET_EMIT_CREATE
198 #include "ip_set_hash_gen.h"
201 hash_ipmac6_kadt(struct ip_set *set, const struct sk_buff *skb,
202 const struct xt_action_param *par,
203 enum ipset_adt adt, struct ip_set_adt_opt *opt)
205 ipset_adtfn adtfn = set->variant->adt[adt];
206 struct hash_ipmac6_elem e = {
208 { .foo[0] = 0, .foo[1] = 0 }
210 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
212 if (skb_mac_header(skb) < skb->head ||
213 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
216 if (opt->flags & IPSET_DIM_ONE_SRC)
217 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
219 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
221 if (is_zero_ether_addr(e.ether))
224 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
226 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
230 hash_ipmac6_uadt(struct ip_set *set, struct nlattr *tb[],
231 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
233 ipset_adtfn adtfn = set->variant->adt[adt];
234 struct hash_ipmac6_elem e = {
236 { .foo[0] = 0, .foo[1] = 0 }
238 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
241 if (unlikely(!tb[IPSET_ATTR_IP] ||
242 !tb[IPSET_ATTR_ETHER] ||
243 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
244 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
245 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
246 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
247 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
248 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
249 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
250 return -IPSET_ERR_PROTOCOL;
252 if (tb[IPSET_ATTR_LINENO])
253 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
255 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
256 ip_set_get_extensions(set, tb, &ext);
260 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
261 if (is_zero_ether_addr(e.ether))
262 return -IPSET_ERR_HASH_ELEM;
264 return adtfn(set, &e, &ext, &ext, flags);
267 static struct ip_set_type hash_ipmac_type __read_mostly = {
268 .name = "hash:ip,mac",
269 .protocol = IPSET_PROTOCOL,
270 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
271 .dimension = IPSET_DIM_TWO,
272 .family = NFPROTO_UNSPEC,
273 .revision_min = IPSET_TYPE_REV_MIN,
274 .revision_max = IPSET_TYPE_REV_MAX,
275 .create = hash_ipmac_create,
277 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
278 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
279 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
280 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
281 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
282 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
285 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
286 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
288 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
289 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
290 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
291 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
292 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
293 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
294 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
295 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
301 hash_ipmac_init(void)
303 return ip_set_type_register(&hash_ipmac_type);
307 hash_ipmac_fini(void)
309 ip_set_type_unregister(&hash_ipmac_type);
312 module_init(hash_ipmac_init);
313 module_exit(hash_ipmac_fini);