]> git.ipfire.org Git - thirdparty/ipset.git/blob - kernel/net/netfilter/ipset/ip_set_hash_ipmac.c
ipset: Actually allow destination MAC address for hash:ip,mac sets too
[thirdparty/ipset.git] / kernel / net / netfilter / ipset / ip_set_hash_ipmac.c
1 /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
2 *
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.
6 */
7
8 /* Kernel module implementing an IP set type: the hash:ip,mac type */
9
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.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>
18 #include <net/ip.h>
19 #include <net/ipv6.h>
20 #include <net/netlink.h>
21 #include <net/tcp.h>
22
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>
27
28 #define IPSET_TYPE_REV_MIN 0
29 #define IPSET_TYPE_REV_MAX 0
30
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");
35
36 /* Type specific function prefix */
37 #define HTYPE hash_ipmac
38
39 /* IPv4 variant */
40
41 /* Member elements */
42 struct hash_ipmac4_elem {
43 /* Zero valued IP addresses cannot be stored */
44 __be32 ip;
45 union {
46 unsigned char ether[ETH_ALEN];
47 __be32 foo[2];
48 };
49 };
50
51 /* Common functions */
52
53 static inline bool
54 hash_ipmac4_data_equal(const struct hash_ipmac4_elem *e1,
55 const struct hash_ipmac4_elem *e2,
56 u32 *multi)
57 {
58 return e1->ip == e2->ip && ether_addr_equal(e1->ether, e2->ether);
59 }
60
61 static bool
62 hash_ipmac4_data_list(struct sk_buff *skb, const struct hash_ipmac4_elem *e)
63 {
64 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip) ||
65 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
66 goto nla_put_failure;
67 return false;
68
69 nla_put_failure:
70 return true;
71 }
72
73 static inline void
74 hash_ipmac4_data_next(struct hash_ipmac4_elem *next,
75 const struct hash_ipmac4_elem *e)
76 {
77 next->ip = e->ip;
78 }
79
80 #define MTYPE hash_ipmac4
81 #define PF 4
82 #define HOST_MASK 32
83 #define HKEY_DATALEN sizeof(struct hash_ipmac4_elem)
84 #include "ip_set_hash_gen.h"
85
86 static int
87 hash_ipmac4_kadt(struct ip_set *set, const struct sk_buff *skb,
88 const struct xt_action_param *par,
89 enum ipset_adt adt, struct ip_set_adt_opt *opt)
90 {
91 ipset_adtfn adtfn = set->variant->adt[adt];
92 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
93 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
94
95 if (skb_mac_header(skb) < skb->head ||
96 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
97 return -EINVAL;
98
99 if (opt->flags & IPSET_DIM_ONE_SRC)
100 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
101 else
102 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
103
104 if (is_zero_ether_addr(e.ether))
105 return -EINVAL;
106
107 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
108
109 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
110 }
111
112 static int
113 hash_ipmac4_uadt(struct ip_set *set, struct nlattr *tb[],
114 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
115 {
116 ipset_adtfn adtfn = set->variant->adt[adt];
117 struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
118 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
119 int ret;
120
121 if (unlikely(!tb[IPSET_ATTR_IP] ||
122 !tb[IPSET_ATTR_ETHER] ||
123 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
124 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
125 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
126 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
127 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
128 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
129 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
130 return -IPSET_ERR_PROTOCOL;
131
132 if (tb[IPSET_ATTR_LINENO])
133 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
134
135 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
136 ip_set_get_extensions(set, tb, &ext);
137 if (ret)
138 return ret;
139 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
140 if (is_zero_ether_addr(e.ether))
141 return -IPSET_ERR_HASH_ELEM;
142
143 return adtfn(set, &e, &ext, &ext, flags);
144 }
145
146 /* IPv6 variant */
147
148 /* Member elements */
149 struct hash_ipmac6_elem {
150 /* Zero valued IP addresses cannot be stored */
151 union nf_inet_addr ip;
152 union {
153 unsigned char ether[ETH_ALEN];
154 __be32 foo[2];
155 };
156 };
157
158 /* Common functions */
159
160 static inline bool
161 hash_ipmac6_data_equal(const struct hash_ipmac6_elem *e1,
162 const struct hash_ipmac6_elem *e2,
163 u32 *multi)
164 {
165 return ipv6_addr_equal(&e1->ip.in6, &e2->ip.in6) &&
166 ether_addr_equal(e1->ether, e2->ether);
167 }
168
169 static bool
170 hash_ipmac6_data_list(struct sk_buff *skb, const struct hash_ipmac6_elem *e)
171 {
172 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
173 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
174 goto nla_put_failure;
175 return false;
176
177 nla_put_failure:
178 return true;
179 }
180
181 static inline void
182 hash_ipmac6_data_next(struct hash_ipmac6_elem *next,
183 const struct hash_ipmac6_elem *e)
184 {
185 }
186
187 #undef MTYPE
188 #undef PF
189 #undef HOST_MASK
190 #undef HKEY_DATALEN
191
192 #define MTYPE hash_ipmac6
193 #define PF 6
194 #define HOST_MASK 128
195 #define HKEY_DATALEN sizeof(struct hash_ipmac6_elem)
196 #define IP_SET_EMIT_CREATE
197 #include "ip_set_hash_gen.h"
198
199 static int
200 hash_ipmac6_kadt(struct ip_set *set, const struct sk_buff *skb,
201 const struct xt_action_param *par,
202 enum ipset_adt adt, struct ip_set_adt_opt *opt)
203 {
204 ipset_adtfn adtfn = set->variant->adt[adt];
205 struct hash_ipmac6_elem e = {
206 { .all = { 0 } },
207 { .foo[0] = 0, .foo[1] = 0 }
208 };
209 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
210
211 if (skb_mac_header(skb) < skb->head ||
212 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
213 return -EINVAL;
214
215 if (opt->flags & IPSET_DIM_ONE_SRC)
216 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
217 else
218 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
219
220 if (is_zero_ether_addr(e.ether))
221 return -EINVAL;
222
223 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
224
225 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
226 }
227
228 static int
229 hash_ipmac6_uadt(struct ip_set *set, struct nlattr *tb[],
230 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
231 {
232 ipset_adtfn adtfn = set->variant->adt[adt];
233 struct hash_ipmac6_elem e = {
234 { .all = { 0 } },
235 { .foo[0] = 0, .foo[1] = 0 }
236 };
237 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
238 int ret;
239
240 if (unlikely(!tb[IPSET_ATTR_IP] ||
241 !tb[IPSET_ATTR_ETHER] ||
242 nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
243 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
244 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
245 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
246 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
247 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
248 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
249 return -IPSET_ERR_PROTOCOL;
250
251 if (tb[IPSET_ATTR_LINENO])
252 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
253
254 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
255 ip_set_get_extensions(set, tb, &ext);
256 if (ret)
257 return ret;
258
259 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
260 if (is_zero_ether_addr(e.ether))
261 return -IPSET_ERR_HASH_ELEM;
262
263 return adtfn(set, &e, &ext, &ext, flags);
264 }
265
266 static struct ip_set_type hash_ipmac_type __read_mostly = {
267 .name = "hash:ip,mac",
268 .protocol = IPSET_PROTOCOL,
269 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
270 .dimension = IPSET_DIM_TWO,
271 .family = NFPROTO_UNSPEC,
272 .revision_min = IPSET_TYPE_REV_MIN,
273 .revision_max = IPSET_TYPE_REV_MAX,
274 .create = hash_ipmac_create,
275 .create_policy = {
276 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
277 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
278 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
279 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
280 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
281 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
282 },
283 .adt_policy = {
284 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
285 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
286 .len = ETH_ALEN },
287 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
288 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
289 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
290 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
291 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
292 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
293 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
294 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
295 },
296 .me = THIS_MODULE,
297 };
298
299 static int __init
300 hash_ipmac_init(void)
301 {
302 return ip_set_type_register(&hash_ipmac_type);
303 }
304
305 static void __exit
306 hash_ipmac_fini(void)
307 {
308 ip_set_type_unregister(&hash_ipmac_type);
309 }
310
311 module_init(hash_ipmac_init);
312 module_exit(hash_ipmac_fini);