1 /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the hash:ip,mac type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/etherdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/random.h>
17 #include <linux/if_ether.h>
20 #include <net/netlink.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
28 #define IPSET_TYPE_REV_MIN 0
29 #define IPSET_TYPE_REV_MAX 0
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
33 IP_SET_MODULE_DESC("hash:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,mac");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipmac
39 /* Zero valued element is not supported */
40 static const unsigned char invalid_ether[ETH_ALEN] = { 0 };
45 struct hash_ipmac4_elem {
46 /* Zero valued IP addresses cannot be stored */
49 unsigned char ether[ETH_ALEN];
54 /* Common functions */
57 hash_ipmac4_data_equal(const struct hash_ipmac4_elem *e1,
58 const struct hash_ipmac4_elem *e2,
61 return e1->ip == e2->ip && ether_addr_equal(e1->ether, e2->ether);
65 hash_ipmac4_data_list(struct sk_buff *skb, const struct hash_ipmac4_elem *e)
67 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip) ||
68 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
77 hash_ipmac4_data_next(struct hash_ipmac4_elem *next,
78 const struct hash_ipmac4_elem *e)
83 #define MTYPE hash_ipmac4
86 #define HKEY_DATALEN sizeof(struct hash_ipmac4_elem)
87 #include "ip_set_hash_gen.h"
90 hash_ipmac4_kadt(struct ip_set *set, const struct sk_buff *skb,
91 const struct xt_action_param *par,
92 enum ipset_adt adt, struct ip_set_adt_opt *opt)
94 ipset_adtfn adtfn = set->variant->adt[adt];
95 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
96 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
98 /* MAC can be src only */
99 if (!(opt->flags & IPSET_DIM_TWO_SRC))
102 if (skb_mac_header(skb) < skb->head ||
103 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
106 memcpy(e.ether, eth_hdr(skb)->h_source, ETH_ALEN);
107 if (ether_addr_equal(e.ether, invalid_ether))
110 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
112 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
116 hash_ipmac4_uadt(struct ip_set *set, struct nlattr *tb[],
117 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
119 ipset_adtfn adtfn = set->variant->adt[adt];
120 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
121 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
124 if (unlikely(!tb[IPSET_ATTR_IP] ||
125 !tb[IPSET_ATTR_ETHER] ||
126 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
127 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
128 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
129 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
130 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
131 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
132 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
133 return -IPSET_ERR_PROTOCOL;
135 if (tb[IPSET_ATTR_LINENO])
136 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
138 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
139 ip_set_get_extensions(set, tb, &ext);
142 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
143 if (ether_addr_equal(e.ether, invalid_ether))
144 return -IPSET_ERR_HASH_ELEM;
146 return adtfn(set, &e, &ext, &ext, flags);
151 /* Member elements */
152 struct hash_ipmac6_elem {
153 /* Zero valued IP addresses cannot be stored */
154 union nf_inet_addr ip;
156 unsigned char ether[ETH_ALEN];
161 /* Common functions */
164 hash_ipmac6_data_equal(const struct hash_ipmac6_elem *e1,
165 const struct hash_ipmac6_elem *e2,
168 return ipv6_addr_equal(&e1->ip.in6, &e2->ip.in6) &&
169 ether_addr_equal(e1->ether, e2->ether);
173 hash_ipmac6_data_list(struct sk_buff *skb, const struct hash_ipmac6_elem *e)
175 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
176 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
177 goto nla_put_failure;
185 hash_ipmac6_data_next(struct hash_ipmac6_elem *next,
186 const struct hash_ipmac6_elem *e)
195 #define MTYPE hash_ipmac6
197 #define HOST_MASK 128
198 #define HKEY_DATALEN sizeof(struct hash_ipmac6_elem)
199 #define IP_SET_EMIT_CREATE
200 #include "ip_set_hash_gen.h"
203 hash_ipmac6_kadt(struct ip_set *set, const struct sk_buff *skb,
204 const struct xt_action_param *par,
205 enum ipset_adt adt, struct ip_set_adt_opt *opt)
207 ipset_adtfn adtfn = set->variant->adt[adt];
208 struct hash_ipmac6_elem e = {
210 { .foo[0] = 0, .foo[1] = 0 }
212 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
214 /* MAC can be src only */
215 if (!(opt->flags & IPSET_DIM_TWO_SRC))
218 if (skb_mac_header(skb) < skb->head ||
219 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
222 memcpy(e.ether, eth_hdr(skb)->h_source, ETH_ALEN);
223 if (ether_addr_equal(e.ether, invalid_ether))
226 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
228 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
232 hash_ipmac6_uadt(struct ip_set *set, struct nlattr *tb[],
233 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
235 ipset_adtfn adtfn = set->variant->adt[adt];
236 struct hash_ipmac6_elem e = {
238 { .foo[0] = 0, .foo[1] = 0 }
240 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
243 if (unlikely(!tb[IPSET_ATTR_IP] ||
244 !tb[IPSET_ATTR_ETHER] ||
245 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
246 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
247 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
248 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
249 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
250 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
251 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
252 return -IPSET_ERR_PROTOCOL;
254 if (tb[IPSET_ATTR_LINENO])
255 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
257 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
258 ip_set_get_extensions(set, tb, &ext);
262 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
263 if (ether_addr_equal(e.ether, invalid_ether))
264 return -IPSET_ERR_HASH_ELEM;
266 return adtfn(set, &e, &ext, &ext, flags);
269 static struct ip_set_type hash_ipmac_type __read_mostly = {
270 .name = "hash:ip,mac",
271 .protocol = IPSET_PROTOCOL,
272 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
273 .dimension = IPSET_DIM_TWO,
274 .family = NFPROTO_UNSPEC,
275 .revision_min = IPSET_TYPE_REV_MIN,
276 .revision_max = IPSET_TYPE_REV_MAX,
277 .create = hash_ipmac_create,
279 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
280 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
281 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
282 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
283 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
284 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
287 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
288 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
290 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
291 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
292 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
293 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
294 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
295 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
296 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
297 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
303 hash_ipmac_init(void)
305 return ip_set_type_register(&hash_ipmac_type);
309 hash_ipmac_fini(void)
311 ip_set_type_unregister(&hash_ipmac_type);
314 module_init(hash_ipmac_init);
315 module_exit(hash_ipmac_fini);